PageRenderTime 74ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/media/video/samsung/fimc/fimc_capture.c

https://github.com/dastin1015/android_kernel_samsung_smdk4210
C | 3475 lines | 2789 code | 511 blank | 175 comment | 619 complexity | d33bd73fb31f967958d3b8e70bb8acec MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* linux/drivers/media/video/samsung/fimc_capture.c
  2. *
  3. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. *
  6. * V4L2 Capture device support file for Samsung Camera Interface (FIMC) driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/string.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/videodev2_exynos_media.h>
  18. #include <linux/videodev2_exynos_camera.h>
  19. #include <linux/clk.h>
  20. #include <linux/mm.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/io.h>
  23. #include <linux/uaccess.h>
  24. #include <plat/media.h>
  25. #include <plat/clock.h>
  26. #include <plat/fimc.h>
  27. #include <linux/delay.h>
  28. #include <asm/cacheflush.h>
  29. #include <linux/pm_qos_params.h>
  30. #include "fimc.h"
  31. static struct pm_qos_request_list bus_qos_pm_qos_req;
  32. static const struct v4l2_fmtdesc capture_fmts[] = {
  33. {
  34. .index = 0,
  35. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  36. .flags = FORMAT_FLAGS_PACKED,
  37. .description = "RGB-5-6-5",
  38. .pixelformat = V4L2_PIX_FMT_RGB565,
  39. }, {
  40. .index = 1,
  41. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  42. .flags = FORMAT_FLAGS_PACKED,
  43. .description = "RGB-8-8-8, unpacked 24 bpp",
  44. .pixelformat = V4L2_PIX_FMT_RGB32,
  45. }, {
  46. .index = 2,
  47. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  48. .flags = FORMAT_FLAGS_PACKED,
  49. .description = "YUV 4:2:2 packed, YCbYCr",
  50. .pixelformat = V4L2_PIX_FMT_YUYV,
  51. }, {
  52. .index = 3,
  53. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  54. .flags = FORMAT_FLAGS_PACKED,
  55. .description = "YUV 4:2:2 packed, CbYCrY",
  56. .pixelformat = V4L2_PIX_FMT_UYVY,
  57. }, {
  58. .index = 4,
  59. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  60. .flags = FORMAT_FLAGS_PACKED,
  61. .description = "YUV 4:2:2 packed, CrYCbY",
  62. .pixelformat = V4L2_PIX_FMT_VYUY,
  63. }, {
  64. .index = 5,
  65. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  66. .flags = FORMAT_FLAGS_PACKED,
  67. .description = "YUV 4:2:2 packed, YCrYCb",
  68. .pixelformat = V4L2_PIX_FMT_YVYU,
  69. }, {
  70. .index = 6,
  71. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  72. .flags = FORMAT_FLAGS_PLANAR,
  73. .description = "YUV 4:2:2 planar, Y/Cb/Cr",
  74. .pixelformat = V4L2_PIX_FMT_YUV422P,
  75. }, {
  76. .index = 7,
  77. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  78. .flags = FORMAT_FLAGS_PLANAR,
  79. .description = "YUV 4:2:0 planar, Y/CbCr",
  80. .pixelformat = V4L2_PIX_FMT_NV12,
  81. }, {
  82. .index = 8,
  83. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  84. .flags = FORMAT_FLAGS_PLANAR,
  85. .description = "YUV 4:2:0 planar, Y/CbCr, Tiled",
  86. .pixelformat = V4L2_PIX_FMT_NV12T,
  87. }, {
  88. .index = 9,
  89. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  90. .flags = FORMAT_FLAGS_PLANAR,
  91. .description = "YUV 4:2:0 planar, Y/CrCb",
  92. .pixelformat = V4L2_PIX_FMT_NV21,
  93. }, {
  94. .index = 10,
  95. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  96. .flags = FORMAT_FLAGS_PLANAR,
  97. .description = "YUV 4:2:2 planar, Y/CbCr",
  98. .pixelformat = V4L2_PIX_FMT_NV16,
  99. }, {
  100. .index = 11,
  101. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  102. .flags = FORMAT_FLAGS_PLANAR,
  103. .description = "YUV 4:2:2 planar, Y/CrCb",
  104. .pixelformat = V4L2_PIX_FMT_NV61,
  105. }, {
  106. .index = 12,
  107. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  108. .flags = FORMAT_FLAGS_PLANAR,
  109. .description = "YUV 4:2:0 planar, Y/Cb/Cr",
  110. .pixelformat = V4L2_PIX_FMT_YUV420,
  111. }, {
  112. .index = 13,
  113. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  114. .flags = FORMAT_FLAGS_PLANAR,
  115. .description = "YUV 4:2:0 planar, Y/Cr/Cb",
  116. .pixelformat = V4L2_PIX_FMT_YVU420,
  117. }, {
  118. .index = 14,
  119. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  120. .description = "JPEG encoded data",
  121. .pixelformat = V4L2_PIX_FMT_JPEG,
  122. }, {
  123. .index = 15,
  124. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  125. .description = "Interleaved data",
  126. .pixelformat = V4L2_PIX_FMT_INTERLEAVED,
  127. },
  128. };
  129. static const struct v4l2_queryctrl fimc_controls[] = {
  130. {
  131. .id = V4L2_CID_ROTATION,
  132. .type = V4L2_CTRL_TYPE_BOOLEAN,
  133. .name = "Roataion",
  134. .minimum = 0,
  135. .maximum = 270,
  136. .step = 90,
  137. .default_value = 0,
  138. }, {
  139. .id = V4L2_CID_HFLIP,
  140. .type = V4L2_CTRL_TYPE_BOOLEAN,
  141. .name = "Horizontal Flip",
  142. .minimum = 0,
  143. .maximum = 1,
  144. .step = 1,
  145. .default_value = 0,
  146. }, {
  147. .id = V4L2_CID_VFLIP,
  148. .type = V4L2_CTRL_TYPE_BOOLEAN,
  149. .name = "Vertical Flip",
  150. .minimum = 0,
  151. .maximum = 1,
  152. .step = 1,
  153. .default_value = 0,
  154. }, {
  155. .id = V4L2_CID_PADDR_Y,
  156. .type = V4L2_CTRL_TYPE_BOOLEAN,
  157. .name = "Physical address Y",
  158. .minimum = 0,
  159. .maximum = 1,
  160. .step = 1,
  161. .default_value = 0,
  162. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  163. }, {
  164. .id = V4L2_CID_PADDR_CB,
  165. .type = V4L2_CTRL_TYPE_BOOLEAN,
  166. .name = "Physical address Cb",
  167. .minimum = 0,
  168. .maximum = 1,
  169. .step = 1,
  170. .default_value = 0,
  171. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  172. }, {
  173. .id = V4L2_CID_PADDR_CR,
  174. .type = V4L2_CTRL_TYPE_BOOLEAN,
  175. .name = "Physical address Cr",
  176. .minimum = 0,
  177. .maximum = 1,
  178. .step = 1,
  179. .default_value = 0,
  180. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  181. }, {
  182. .id = V4L2_CID_PADDR_CBCR,
  183. .type = V4L2_CTRL_TYPE_BOOLEAN,
  184. .name = "Physical address CbCr",
  185. .minimum = 0,
  186. .maximum = 1,
  187. .step = 1,
  188. .default_value = 0,
  189. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  190. }, {
  191. .id = V4L2_CID_CACHEABLE,
  192. .type = V4L2_CTRL_TYPE_BOOLEAN,
  193. .name = "Cacheable",
  194. .minimum = 0,
  195. .maximum = 1,
  196. .step = 1,
  197. .default_value = 0,
  198. },
  199. };
  200. #ifdef CONFIG_MACH_GC1
  201. static bool leave_power;
  202. #endif
  203. #ifndef CONFIG_VIDEO_FIMC_MIPI
  204. void s3c_csis_start(int csis_id, int lanes, int settle, \
  205. int align, int width, int height, int pixel_format) {}
  206. void s3c_csis_stop(int csis_id) {}
  207. void s3c_csis_enable_pktdata(int csis_id, bool enable) {}
  208. #endif
  209. static int fimc_init_camera(struct fimc_control *ctrl)
  210. {
  211. struct fimc_global *fimc = get_fimc_dev();
  212. struct s3c_platform_fimc *pdata;
  213. struct s3c_platform_camera *cam;
  214. int ret = 0, retry_cnt = 0;
  215. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  216. struct platform_device *pdev = to_platform_device(ctrl->dev);
  217. #endif
  218. pdata = to_fimc_plat(ctrl->dev);
  219. cam = ctrl->cam;
  220. /* do nothing if already initialized */
  221. if (ctrl->cam->initialized)
  222. return 0;
  223. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  224. if (ctrl->power_status == FIMC_POWER_OFF) {
  225. pm_runtime_get_sync(&pdev->dev);
  226. }
  227. #endif
  228. /*
  229. * WriteBack mode doesn't need to set clock and power,
  230. * but it needs to set source width, height depend on LCD resolution.
  231. */
  232. if ((cam->id == CAMERA_WB) || (cam->id == CAMERA_WB_B)) {
  233. ret = s3cfb_direct_ioctl(0, S3CFB_GET_LCD_WIDTH,
  234. (unsigned long)&cam->width);
  235. if (ret) {
  236. fimc_err("fail to get LCD size\n");
  237. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  238. pm_runtime_put_sync(&pdev->dev);
  239. #endif
  240. return ret;
  241. }
  242. ret = s3cfb_direct_ioctl(0, S3CFB_GET_LCD_HEIGHT,
  243. (unsigned long)&cam->height);
  244. if (ret) {
  245. fimc_err("fail to get LCD size\n");
  246. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  247. pm_runtime_put_sync(&pdev->dev);
  248. #endif
  249. return ret;
  250. }
  251. cam->window.width = cam->width;
  252. cam->window.height = cam->height;
  253. cam->initialized = 1;
  254. return ret;
  255. }
  256. retry:
  257. /* set rate for mclk */
  258. #ifndef CONFIG_MACH_GC1
  259. if ((clk_get_rate(cam->clk)) && (fimc->mclk_status == CAM_MCLK_OFF)) {
  260. #else
  261. if ((clk_get_rate(cam->clk)) && (fimc->mclk_status == CAM_MCLK_OFF)
  262. && !leave_power) {
  263. #endif
  264. clk_set_rate(cam->clk, cam->clk_rate);
  265. clk_enable(cam->clk);
  266. fimc->mclk_status = CAM_MCLK_ON;
  267. fimc_info1("clock for camera: %d\n", cam->clk_rate);
  268. }
  269. /* enable camera power if needed */
  270. if (cam->cam_power) {
  271. #ifndef CONFIG_MACH_GC1
  272. ret = cam->cam_power(1);
  273. #else
  274. if (!leave_power)
  275. ret = cam->cam_power(1);
  276. leave_power = false;
  277. #endif
  278. if (unlikely(ret < 0)) {
  279. fimc_err("\nfail to power on\n");
  280. if (fimc->mclk_status == CAM_MCLK_ON) {
  281. clk_disable(ctrl->cam->clk);
  282. fimc->mclk_status = CAM_MCLK_OFF;
  283. }
  284. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  285. pm_runtime_put_sync(&pdev->dev);
  286. #endif
  287. return ret;
  288. }
  289. }
  290. #ifndef CONFIG_MACH_GC1
  291. /* "0" argument means preview init for s5k4ea */
  292. ret = v4l2_subdev_call(cam->sd, core, init, 0);
  293. #endif
  294. /* Retry camera power-up if first i2c fails. */
  295. if (unlikely(ret < 0)) {
  296. if (cam->cam_power)
  297. cam->cam_power(0);
  298. if (fimc->mclk_status == CAM_MCLK_ON) {
  299. clk_disable(ctrl->cam->clk);
  300. fimc->mclk_status = CAM_MCLK_OFF;
  301. }
  302. if (retry_cnt++ < 3) {
  303. msleep(100);
  304. fimc_err("Retry power on(%d/3)\n\n", retry_cnt);
  305. goto retry;
  306. } else {
  307. fimc_err("Camera power/init failed!!!!\n\n");
  308. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  309. if (ctrl->power_status == FIMC_POWER_ON) {
  310. pm_runtime_put_sync(&pdev->dev);
  311. }
  312. #endif
  313. }
  314. } else {
  315. /* Apply things to interface register */
  316. fimc_hwset_reset(ctrl);
  317. cam->initialized = 1;
  318. }
  319. return ret;
  320. }
  321. static int fimc_camera_get_jpeg_memsize(struct fimc_control *ctrl)
  322. {
  323. int ret = 0;
  324. struct v4l2_control cam_ctrl;
  325. cam_ctrl.id = V4L2_CID_CAM_JPEG_MEMSIZE;
  326. ret = v4l2_subdev_call(ctrl->cam->sd, core, g_ctrl, &cam_ctrl);
  327. if (ret < 0) {
  328. fimc_err("%s: Subdev doesn't support JEPG encoding.\n", \
  329. __func__);
  330. return 0;
  331. }
  332. return cam_ctrl.value;
  333. }
  334. static int fimc_capture_scaler_info(struct fimc_control *ctrl)
  335. {
  336. struct fimc_scaler *sc = &ctrl->sc;
  337. struct v4l2_rect *window = &ctrl->cam->window;
  338. int tx, ty, sx, sy;
  339. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  340. int rot = 0;
  341. if (!ctrl->cam->use_isp) {
  342. sx = window->width;
  343. sy = window->height;
  344. } else {
  345. sx = ctrl->is.fmt.width;
  346. sy = ctrl->is.fmt.height;
  347. }
  348. sc->real_width = sx;
  349. sc->real_height = sy;
  350. rot = fimc_mapping_rot_flip(ctrl->cap->rotate, ctrl->cap->flip);
  351. if (rot & FIMC_ROT) {
  352. tx = ctrl->cap->fmt.height;
  353. ty = ctrl->cap->fmt.width;
  354. } else {
  355. tx = ctrl->cap->fmt.width;
  356. ty = ctrl->cap->fmt.height;
  357. }
  358. fimc_dbg("%s: CamOut (%d, %d), TargetOut (%d, %d)\n",
  359. __func__, sx, sy, tx, ty);
  360. if (sx <= 0 || sy <= 0) {
  361. fimc_err("%s: invalid source size\n", __func__);
  362. return -EINVAL;
  363. }
  364. if (tx <= 0 || ty <= 0) {
  365. fimc_err("%s: invalid target size\n", __func__);
  366. return -EINVAL;
  367. }
  368. fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
  369. fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
  370. if (sx == sy) {
  371. if (sx*10/tx >= 15 && sx*10/tx < 20) {
  372. sc->pre_hratio = 2;
  373. sc->hfactor = 1;
  374. }
  375. if (sy*10/ty >= 15 && sy*10/ty < 20) {
  376. sc->pre_vratio = 2;
  377. sc->vfactor = 1;
  378. }
  379. }
  380. sc->pre_dst_width = sx / sc->pre_hratio;
  381. sc->pre_dst_height = sy / sc->pre_vratio;
  382. if (pdata->hw_ver >= 0x50) {
  383. sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
  384. sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
  385. } else {
  386. sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
  387. sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
  388. }
  389. sc->scaleup_h = (tx >= sx) ? 1 : 0;
  390. sc->scaleup_v = (ty >= sy) ? 1 : 0;
  391. return 0;
  392. }
  393. static int fimc_capture_change_scaler_info(struct fimc_control *ctrl)
  394. {
  395. struct fimc_scaler *sc = &ctrl->sc;
  396. struct v4l2_rect *window = &ctrl->cam->window;
  397. int tx, ty, sx, sy;
  398. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  399. int rot = 0;
  400. if (!ctrl->cam->use_isp) {
  401. sx = window->width;
  402. sy = window->height;
  403. } else {
  404. sx = ctrl->is.zoom_in_width;
  405. sy = ctrl->is.zoom_in_height;
  406. }
  407. sc->real_width = sx;
  408. sc->real_height = sy;
  409. rot = fimc_mapping_rot_flip(ctrl->cap->rotate, ctrl->cap->flip);
  410. if (rot & FIMC_ROT) {
  411. tx = ctrl->cap->fmt.height;
  412. ty = ctrl->cap->fmt.width;
  413. } else {
  414. tx = ctrl->cap->fmt.width;
  415. ty = ctrl->cap->fmt.height;
  416. }
  417. fimc_dbg("%s: CamOut (%d, %d), TargetOut (%d, %d)\n",
  418. __func__, sx, sy, tx, ty);
  419. if (sx <= 0 || sy <= 0) {
  420. fimc_err("%s: invalid source size\n", __func__);
  421. return -EINVAL;
  422. }
  423. if (tx <= 0 || ty <= 0) {
  424. fimc_err("%s: invalid target size\n", __func__);
  425. return -EINVAL;
  426. }
  427. fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
  428. fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
  429. sc->pre_dst_width = sx / sc->pre_hratio;
  430. sc->pre_dst_height = sy / sc->pre_vratio;
  431. if (pdata->hw_ver >= 0x50) {
  432. sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
  433. sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
  434. } else {
  435. sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
  436. sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
  437. }
  438. sc->scaleup_h = (tx >= sx) ? 1 : 0;
  439. sc->scaleup_v = (ty >= sy) ? 1 : 0;
  440. return 0;
  441. }
  442. int fimc_start_zoom_capture(struct fimc_control *ctrl)
  443. {
  444. fimc_dbg("%s\n", __func__);
  445. fimc_hwset_start_scaler(ctrl);
  446. fimc_hwset_enable_capture(ctrl, ctrl->sc.bypass);
  447. fimc_hwset_disable_frame_end_irq(ctrl);
  448. return 0;
  449. }
  450. int fimc_stop_zoom_capture(struct fimc_control *ctrl)
  451. {
  452. fimc_dbg("%s\n", __func__);
  453. if (!ctrl->cam) {
  454. fimc_err("%s: No capture device.\n", __func__);
  455. return -ENODEV;
  456. }
  457. if (!ctrl->cap) {
  458. fimc_err("%s: No cappure format.\n", __func__);
  459. return -ENODEV;
  460. }
  461. if (ctrl->cap->lastirq) {
  462. fimc_hwset_enable_lastirq(ctrl);
  463. fimc_hwset_disable_capture(ctrl);
  464. fimc_hwset_disable_lastirq(ctrl);
  465. } else {
  466. fimc_hwset_disable_capture(ctrl);
  467. fimc_hwset_enable_frame_end_irq(ctrl);
  468. }
  469. fimc_hwset_stop_scaler(ctrl);
  470. return 0;
  471. }
  472. static int fimc_add_inqueue(struct fimc_control *ctrl, int i)
  473. {
  474. struct fimc_capinfo *cap = ctrl->cap;
  475. struct fimc_buf_set *tmp_buf;
  476. struct list_head *count;
  477. /* PINGPONG_2ADDR_MODE Only */
  478. list_for_each(count, &cap->inq) {
  479. tmp_buf = list_entry(count, struct fimc_buf_set, list);
  480. /* skip list_add_tail if already buffer is in cap->inq list*/
  481. if (tmp_buf->id == i)
  482. return 0;
  483. }
  484. list_add_tail(&cap->bufs[i].list, &cap->inq);
  485. return 0;
  486. }
  487. static int fimc_add_outqueue(struct fimc_control *ctrl, int i)
  488. {
  489. struct fimc_capinfo *cap = ctrl->cap;
  490. struct fimc_buf_set *buf;
  491. unsigned int mask = 0x2;
  492. /* PINGPONG_2ADDR_MODE Only */
  493. /* pair_buf_index stands for pair index of i. (0<->2) (1<->3) */
  494. int pair_buf_index = (i^mask);
  495. /* FIMC have 4 h/w registers */
  496. if (i < 0 || i >= FIMC_PHYBUFS) {
  497. fimc_err("%s: invalid queue index : %d\n", __func__, i);
  498. return -ENOENT;
  499. }
  500. if (list_empty(&cap->inq))
  501. return -ENOENT;
  502. buf = list_first_entry(&cap->inq, struct fimc_buf_set, list);
  503. /* pair index buffer should be allocated first */
  504. cap->outq[pair_buf_index] = buf->id;
  505. fimc_hwset_output_address(ctrl, buf, pair_buf_index);
  506. cap->outq[i] = buf->id;
  507. fimc_hwset_output_address(ctrl, buf, i);
  508. list_del(&buf->list);
  509. return 0;
  510. }
  511. int fimc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  512. {
  513. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  514. int ret = 0;
  515. fimc_dbg("%s\n", __func__);
  516. /* WriteBack doesn't have subdev_call */
  517. if ((ctrl->cam->id == CAMERA_WB) || (ctrl->cam->id == CAMERA_WB_B))
  518. return 0;
  519. mutex_lock(&ctrl->v4l2_lock);
  520. ret = v4l2_subdev_call(ctrl->cam->sd, video, g_parm, a);
  521. mutex_unlock(&ctrl->v4l2_lock);
  522. return ret;
  523. }
  524. int fimc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  525. {
  526. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  527. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  528. int ret = 0;
  529. int new_fps = a->parm.capture.timeperframe.denominator /
  530. a->parm.capture.timeperframe.numerator;
  531. fimc_info2("%s fimc%d, %d\n", __func__, ctrl->id, new_fps);
  532. /* WriteBack doesn't have subdev_call */
  533. if ((ctrl->cam->id == CAMERA_WB) || (ctrl->cam->id == CAMERA_WB_B))
  534. return 0;
  535. mutex_lock(&ctrl->v4l2_lock);
  536. if (ctrl->cam->sd && fimc_cam_use)
  537. ret = v4l2_subdev_call(ctrl->cam->sd, video, s_parm, a);
  538. else if (ctrl->is.sd && fimc_cam_use)
  539. ret = v4l2_subdev_call(ctrl->is.sd, video, s_parm, a);
  540. mutex_unlock(&ctrl->v4l2_lock);
  541. return ret;
  542. }
  543. /* Enumerate controls */
  544. int fimc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qc)
  545. {
  546. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  547. int i, ret;
  548. fimc_dbg("%s\n", __func__);
  549. for (i = 0; i < ARRAY_SIZE(fimc_controls); i++) {
  550. if (fimc_controls[i].id == qc->id) {
  551. memcpy(qc, &fimc_controls[i], sizeof(struct v4l2_queryctrl));
  552. return 0;
  553. }
  554. }
  555. mutex_lock(&ctrl->v4l2_lock);
  556. ret = v4l2_subdev_call(ctrl->cam->sd, core, queryctrl, qc);
  557. mutex_unlock(&ctrl->v4l2_lock);
  558. return ret;
  559. }
  560. /* Menu control items */
  561. int fimc_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qm)
  562. {
  563. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  564. int ret = 0;
  565. fimc_dbg("%s\n", __func__);
  566. mutex_lock(&ctrl->v4l2_lock);
  567. ret = v4l2_subdev_call(ctrl->cam->sd, core, querymenu, qm);
  568. mutex_unlock(&ctrl->v4l2_lock);
  569. return ret;
  570. }
  571. int fimc_enum_input(struct file *file, void *fh, struct v4l2_input *inp)
  572. {
  573. struct fimc_global *fimc = get_fimc_dev();
  574. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  575. fimc_dbg("%s: index %d\n", __func__, inp->index);
  576. if (inp->index >= FIMC_MAXCAMS) {
  577. fimc_err("%s: invalid input index, received = %d\n",
  578. __func__, inp->index);
  579. return -EINVAL;
  580. }
  581. if (!fimc->camera_isvalid[inp->index])
  582. return -EINVAL;
  583. mutex_lock(&ctrl->v4l2_lock);
  584. if (fimc->camera[inp->index]->use_isp && !(fimc->camera[inp->index]->info))
  585. strcpy(inp->name, "ISP Camera");
  586. else
  587. strcpy(inp->name, fimc->camera[inp->index]->info->type);
  588. inp->type = V4L2_INPUT_TYPE_CAMERA;
  589. mutex_unlock(&ctrl->v4l2_lock);
  590. return 0;
  591. }
  592. int fimc_g_input(struct file *file, void *fh, unsigned int *i)
  593. {
  594. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  595. struct fimc_global *fimc = get_fimc_dev();
  596. /* In case of isueing g_input before s_input */
  597. if (!ctrl->cam) {
  598. fimc_err("no camera device selected yet. do VIDIOC_S_INPUT first\n");
  599. return -ENODEV;
  600. }
  601. mutex_lock(&ctrl->v4l2_lock);
  602. *i = (unsigned int) fimc->active_camera;
  603. mutex_unlock(&ctrl->v4l2_lock);
  604. fimc_dbg("%s: index %d\n", __func__, *i);
  605. return 0;
  606. }
  607. int fimc_release_subdev(struct fimc_control *ctrl)
  608. {
  609. struct fimc_global *fimc = get_fimc_dev();
  610. struct i2c_client *client;
  611. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  612. int ret;
  613. if (ctrl->cam->sd && fimc_cam_use) {
  614. fimc_dbg("%s called\n", __func__);
  615. /* WriteBack doesn't need clock setting */
  616. if ((ctrl->cam->id == CAMERA_WB) ||
  617. (ctrl->cam->id == CAMERA_WB_B)) {
  618. ctrl->cam->initialized = 0;
  619. ctrl->cam = NULL;
  620. fimc->active_camera = -1;
  621. return 0;
  622. }
  623. client = v4l2_get_subdevdata(ctrl->cam->sd);
  624. i2c_unregister_device(client);
  625. ctrl->cam->sd = NULL;
  626. #ifndef CONFIG_MACH_GC1
  627. if (ctrl->cam->cam_power)
  628. #else
  629. if (ctrl->cam->cam_power && !leave_power)
  630. #endif
  631. ctrl->cam->cam_power(0);
  632. /* shutdown the MCLK */
  633. #ifndef CONFIG_MACH_GC1
  634. if (fimc->mclk_status == CAM_MCLK_ON) {
  635. #else
  636. if (fimc->mclk_status == CAM_MCLK_ON && !leave_power) {
  637. #endif
  638. clk_disable(ctrl->cam->clk);
  639. fimc->mclk_status = CAM_MCLK_OFF;
  640. }
  641. ctrl->cam->initialized = 0;
  642. ctrl->cam = NULL;
  643. fimc->active_camera = -1;
  644. } else if (ctrl->cam->sd) {
  645. ctrl->cam->initialized = 0;
  646. ctrl->cam = NULL;
  647. fimc->active_camera = -1;
  648. }
  649. if (ctrl->flite_sd && fimc_cam_use) {
  650. ret = v4l2_subdev_call(ctrl->flite_sd, core, s_power, 0);
  651. if (ret)
  652. fimc_err("s_power failed: %d", ret);
  653. ctrl->flite_sd = NULL;
  654. }
  655. return 0;
  656. }
  657. static int fimc_configure_subdev(struct fimc_control *ctrl)
  658. {
  659. struct i2c_adapter *i2c_adap;
  660. struct i2c_board_info *i2c_info;
  661. struct i2c_client *client;
  662. struct v4l2_subdev *sd;
  663. unsigned short addr;
  664. char *name;
  665. int ret = 0;
  666. i2c_adap = i2c_get_adapter(ctrl->cam->get_i2c_busnum());
  667. if (!i2c_adap) {
  668. fimc_err("subdev i2c_adapter missing-skip registration\n");
  669. return -ENODEV;
  670. }
  671. i2c_info = ctrl->cam->info;
  672. if (!i2c_info) {
  673. fimc_err("%s: subdev i2c board info missing\n", __func__);
  674. return -ENODEV;
  675. }
  676. name = i2c_info->type;
  677. if (!name) {
  678. fimc_err("subdev i2c driver name missing-skip registration\n");
  679. return -ENODEV;
  680. }
  681. addr = i2c_info->addr;
  682. if (!addr) {
  683. fimc_err("subdev i2c address missing-skip registration\n");
  684. return -ENODEV;
  685. }
  686. /*
  687. * NOTE: first time subdev being registered,
  688. * s_config is called and try to initialize subdev device
  689. * but in this point, we are not giving MCLK and power to subdev
  690. * so nothing happens but pass platform data through
  691. */
  692. sd = v4l2_i2c_new_subdev_board(&ctrl->v4l2_dev, i2c_adap,
  693. i2c_info, &addr);
  694. if (!sd) {
  695. fimc_err("%s: v4l2 subdev board registering failed\n",
  696. __func__);
  697. }
  698. /* Assign subdev to proper camera device pointer */
  699. ctrl->cam->sd = sd;
  700. if (!ctrl->cam->initialized) {
  701. ret = fimc_init_camera(ctrl);
  702. if (ret < 0) {
  703. fimc_err("%s: fail to initialize subdev\n", __func__);
  704. #ifndef CONFIG_MACH_GC1
  705. client = v4l2_get_subdevdata(sd);
  706. i2c_unregister_device(client);
  707. ctrl->cam->sd = NULL;
  708. #endif
  709. return ret;
  710. }
  711. }
  712. return 0;
  713. }
  714. static int flite_register_callback(struct device *dev, void *p)
  715. {
  716. struct v4l2_subdev **sd_list = p;
  717. struct v4l2_subdev *sd = NULL;
  718. sd = dev_get_drvdata(dev);
  719. if (sd) {
  720. struct platform_device *pdev = v4l2_get_subdev_hostdata(sd);
  721. *(sd_list + pdev->id) = sd;
  722. }
  723. return 0; /* non-zero value stops iteration */
  724. }
  725. static struct v4l2_subdev *exynos_flite_get_subdev(int id)
  726. {
  727. const char *module_name = "exynos-fimc-lite";
  728. struct device_driver *drv;
  729. struct v4l2_subdev *sd[FLITE_MAX_NUM] = {NULL,};
  730. int ret;
  731. drv = driver_find(module_name, &platform_bus_type);
  732. if (!drv) {
  733. request_module(module_name);
  734. drv = driver_find(module_name, &platform_bus_type);
  735. }
  736. if (!drv)
  737. return ERR_PTR(-ENODEV);
  738. ret = driver_for_each_device(drv, NULL, &sd[0],
  739. flite_register_callback);
  740. put_driver(drv);
  741. return ret ? NULL : sd[id];
  742. }
  743. int fimc_subdev_attatch(struct fimc_control *ctrl)
  744. {
  745. int ret = 0;
  746. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  747. ctrl->flite_sd = exynos_flite_get_subdev(ctrl->cam->flite_id);
  748. if (IS_ERR_OR_NULL(ctrl->flite_sd)) {
  749. ctrl->flite_sd = NULL;
  750. return PTR_ERR(ctrl->flite_sd);
  751. } else {
  752. if (fimc_cam_use) {
  753. ret = v4l2_subdev_call(ctrl->flite_sd, core, s_power, 1);
  754. if (ret)
  755. fimc_err("s_power failed: %d", ret);
  756. }
  757. }
  758. return 0;
  759. }
  760. static int fimc_is_register_callback(struct device *dev, void *p)
  761. {
  762. struct v4l2_subdev **sd = p;
  763. *sd = dev_get_drvdata(dev);
  764. if (!*sd)
  765. return -EINVAL;
  766. return 0; /* non-zero value stops iteration */
  767. }
  768. int fimc_is_release_subdev(struct fimc_control *ctrl)
  769. {
  770. int ret;
  771. struct fimc_global *fimc = get_fimc_dev();
  772. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  773. if (ctrl->is.sd && ctrl->cam && fimc_cam_use) {
  774. if (ctrl->cam->cam_power)
  775. ctrl->cam->cam_power(0);
  776. /* shutdown the MCLK */
  777. if (fimc->mclk_status == CAM_MCLK_ON) {
  778. clk_disable(ctrl->cam->clk);
  779. fimc->mclk_status = CAM_MCLK_OFF;
  780. }
  781. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, 0);
  782. if (ret < 0) {
  783. fimc_dbg("FIMC-IS init failed");
  784. return -ENODEV;
  785. }
  786. v4l2_device_unregister_subdev(ctrl->is.sd);
  787. ctrl->is.sd = NULL;
  788. ctrl->cam->initialized = 0;
  789. ctrl->cam = NULL;
  790. fimc->active_camera = -1;
  791. } else if (ctrl->is.sd && ctrl->cam) {
  792. ctrl->is.sd = NULL;
  793. ctrl->cam->initialized = 0;
  794. ctrl->cam = NULL;
  795. fimc->active_camera = -1;
  796. }
  797. return 0;
  798. }
  799. static struct v4l2_subdev *fimc_is_get_subdev(int id)
  800. {
  801. const char *module_name = "exynos4-fimc-is";
  802. struct device_driver *drv;
  803. struct v4l2_subdev *sd = NULL;
  804. int ret;
  805. drv = driver_find(module_name, &platform_bus_type);
  806. if (!drv) {
  807. request_module(module_name);
  808. drv = driver_find(module_name, &platform_bus_type);
  809. }
  810. if (!drv)
  811. return ERR_PTR(-ENODEV);
  812. ret = driver_for_each_device(drv, NULL, &sd,
  813. fimc_is_register_callback);
  814. put_driver(drv);
  815. return ret ? NULL : sd;
  816. }
  817. static int fimc_is_init_cam(struct fimc_control *ctrl)
  818. {
  819. struct fimc_global *fimc = get_fimc_dev();
  820. struct s3c_platform_camera *cam;
  821. int ret = 0;
  822. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  823. struct platform_device *pdev = to_platform_device(ctrl->dev);
  824. #endif
  825. cam = ctrl->cam;
  826. /* Do noting if already initialized */
  827. if (ctrl->cam->initialized)
  828. return 0;
  829. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  830. if (ctrl->power_status == FIMC_POWER_OFF)
  831. pm_runtime_get_sync(&pdev->dev);
  832. #endif
  833. /* set rate for mclk */
  834. if ((clk_get_rate(cam->clk)) && (fimc->mclk_status == CAM_MCLK_OFF)) {
  835. clk_set_rate(cam->clk, cam->clk_rate);
  836. clk_enable(cam->clk);
  837. fimc->mclk_status = CAM_MCLK_ON;
  838. fimc_info1("clock for camera (FIMC-IS): %d\n", cam->clk_rate);
  839. }
  840. /* enable camera power if needed */
  841. if (cam->cam_power) {
  842. ret = cam->cam_power(1);
  843. if (unlikely(ret < 0))
  844. fimc_err("\nfail to power on\n");
  845. }
  846. /* Retry camera power-up if first i2c fails. */
  847. if (unlikely(ret < 0)) {
  848. if (cam->cam_power)
  849. cam->cam_power(0);
  850. if (fimc->mclk_status == CAM_MCLK_ON) {
  851. clk_disable(ctrl->cam->clk);
  852. fimc->mclk_status = CAM_MCLK_OFF;
  853. }
  854. fimc_err("Camera power/init failed!!!!\n\n");
  855. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  856. if (ctrl->power_status == FIMC_POWER_ON)
  857. pm_runtime_put_sync(&pdev->dev);
  858. #endif
  859. } else {
  860. /* Apply things to interface register */
  861. fimc_hwset_reset(ctrl);
  862. cam->initialized = 1;
  863. }
  864. return ret;
  865. }
  866. int fimc_s_input(struct file *file, void *fh, unsigned int i)
  867. {
  868. struct fimc_global *fimc = get_fimc_dev();
  869. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  870. struct fimc_capinfo *cap = ctrl->cap;
  871. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  872. int ret = 0;
  873. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  874. struct platform_device *pdev = to_platform_device(ctrl->dev);
  875. #endif
  876. fimc_dbg("%s: index %d\n", __func__, i);
  877. if (i >= FIMC_MAXCAMS) {
  878. fimc_err("%s: invalid input index\n", __func__);
  879. return -EINVAL;
  880. }
  881. if (!fimc->camera_isvalid[i])
  882. return -EINVAL;
  883. if (fimc->camera[i]->sd && fimc_cam_use) {
  884. fimc_err("%s: Camera already in use.\n", __func__);
  885. return -EBUSY;
  886. }
  887. mutex_lock(&ctrl->v4l2_lock);
  888. /* If ctrl->cam is not NULL, there is one subdev already registered.
  889. * We need to unregister that subdev first. */
  890. if (i != fimc->active_camera) {
  891. fimc_info1("\n\nfimc_s_input activating subdev\n");
  892. if (ctrl->cam && (ctrl->cam->sd || ctrl->flite_sd))
  893. fimc_release_subdev(ctrl);
  894. else if (ctrl->is.sd)
  895. fimc_is_release_subdev(ctrl);
  896. ctrl->cam = fimc->camera[i];
  897. if ((ctrl->cam->id != CAMERA_WB) && (ctrl->cam->id !=
  898. CAMERA_WB_B) && (!ctrl->cam->use_isp) && fimc_cam_use) {
  899. ret = fimc_configure_subdev(ctrl);
  900. if (ret < 0) {
  901. #ifdef CONFIG_MACH_GC1
  902. if (ret == -ENOSYS) {
  903. /* return no error If firmware is bad.
  904. Because F/W update app should access the sensor through HAL instance */
  905. fimc_err("%s: please update the F/W\n", __func__);
  906. } else {
  907. ctrl->cam = NULL;
  908. mutex_unlock(&ctrl->v4l2_lock);
  909. fimc_err("%s: Could not register camera" \
  910. " sensor with V4L2.\n", __func__);
  911. return -ENODEV;
  912. }
  913. #else
  914. ctrl->cam = NULL;
  915. mutex_unlock(&ctrl->v4l2_lock);
  916. fimc_err("%s: Could not register camera" \
  917. " sensor with V4L2.\n", __func__);
  918. return -ENODEV;
  919. #endif
  920. }
  921. }
  922. fimc->active_camera = i;
  923. fimc_info2("fimc_s_input activated subdev = %d\n", i);
  924. }
  925. if (!fimc_cam_use) {
  926. if (i == fimc->active_camera) {
  927. ctrl->cam = fimc->camera[i];
  928. fimc_info2("fimc_s_input activating subdev FIMC%d\n",
  929. ctrl->id);
  930. } else {
  931. mutex_unlock(&ctrl->v4l2_lock);
  932. return -EINVAL;
  933. }
  934. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  935. #ifdef CONFIG_DRM_EXYNOS_FIMD_WB
  936. if ((ctrl->cam->id != CAMERA_WB) &&
  937. (ctrl->cam->id != CAMERA_WB_B) &&
  938. (ctrl->power_status == FIMC_POWER_OFF)) {
  939. #else
  940. if (ctrl->power_status == FIMC_POWER_OFF) {
  941. #endif
  942. pm_runtime_get_sync(&pdev->dev);
  943. }
  944. fimc_hwset_reset(ctrl);
  945. #endif
  946. }
  947. if (ctrl->cam->use_isp) {
  948. /* fimc-lite attatch */
  949. ret = fimc_subdev_attatch(ctrl);
  950. if (ret) {
  951. fimc_err("subdev_attatch failed\n");
  952. mutex_unlock(&ctrl->v4l2_lock);
  953. return -ENODEV;
  954. }
  955. /* fimc-is attatch */
  956. ctrl->is.sd = fimc_is_get_subdev(i);
  957. if (IS_ERR_OR_NULL(ctrl->is.sd)) {
  958. fimc_err("fimc-is subdev_attatch failed\n");
  959. mutex_unlock(&ctrl->v4l2_lock);
  960. return -ENODEV;
  961. }
  962. ctrl->is.fmt.width = ctrl->cam->width;
  963. ctrl->is.fmt.height = ctrl->cam->height;
  964. ctrl->is.frame_count = 0;
  965. if (fimc_cam_use) {
  966. ret = fimc_is_init_cam(ctrl);
  967. if (ret < 0) {
  968. fimc_dbg("FIMC-IS init clock failed");
  969. mutex_unlock(&ctrl->v4l2_lock);
  970. return -ENODEV;
  971. }
  972. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, 1);
  973. if (ret < 0) {
  974. fimc_err("FIMC-IS init s_power failed");
  975. mutex_unlock(&ctrl->v4l2_lock);
  976. return -ENODEV;
  977. }
  978. ret = v4l2_subdev_call(ctrl->is.sd, core, load_fw);
  979. if (ret < 0) {
  980. fimc_err("FIMC-IS init load_fw failed");
  981. mutex_unlock(&ctrl->v4l2_lock);
  982. if (!cap) {
  983. cap = kzalloc(sizeof(*cap), GFP_KERNEL);
  984. if (!cap) {
  985. fimc_err("%s: no memory for "
  986. "capture device info\n", __func__);
  987. return -ENOMEM;
  988. }
  989. /* assign to ctrl */
  990. ctrl->cap = cap;
  991. }
  992. return -ENODEV;
  993. }
  994. ret = v4l2_subdev_call(ctrl->is.sd, core, init, ctrl->cam->sensor_index);
  995. if (ret < 0) {
  996. fimc_err("FIMC-IS init failed");
  997. mutex_unlock(&ctrl->v4l2_lock);
  998. if (!cap) {
  999. cap = kzalloc(sizeof(*cap), GFP_KERNEL);
  1000. if (!cap) {
  1001. fimc_err("%s: no memory for "
  1002. "capture device info\n", __func__);
  1003. return -ENOMEM;
  1004. }
  1005. /* assign to ctrl */
  1006. ctrl->cap = cap;
  1007. }
  1008. return -ENODEV;
  1009. }
  1010. }
  1011. }
  1012. /*
  1013. * The first time alloc for struct cap_info, and will be
  1014. * released at the file close.
  1015. * Anyone has better idea to do this?
  1016. */
  1017. if (!cap) {
  1018. cap = kzalloc(sizeof(*cap), GFP_KERNEL);
  1019. if (!cap) {
  1020. fimc_err("%s: no memory for "
  1021. "capture device info\n", __func__);
  1022. return -ENOMEM;
  1023. }
  1024. /* assign to ctrl */
  1025. ctrl->cap = cap;
  1026. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  1027. if (ctrl->power_status == FIMC_POWER_OFF)
  1028. pm_runtime_get_sync(&pdev->dev);
  1029. #endif
  1030. } else {
  1031. memset(cap, 0, sizeof(*cap));
  1032. }
  1033. mutex_unlock(&ctrl->v4l2_lock);
  1034. return 0;
  1035. }
  1036. int fimc_enum_fmt_vid_capture(struct file *file, void *fh,
  1037. struct v4l2_fmtdesc *f)
  1038. {
  1039. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  1040. int i = f->index;
  1041. fimc_dbg("%s\n", __func__);
  1042. if (i >= ARRAY_SIZE(capture_fmts)) {
  1043. fimc_err("%s: There is no support format index %d\n", __func__, i);
  1044. return -EINVAL;
  1045. }
  1046. mutex_lock(&ctrl->v4l2_lock);
  1047. memset(f, 0, sizeof(*f));
  1048. memcpy(f, &capture_fmts[i], sizeof(*f));
  1049. mutex_unlock(&ctrl->v4l2_lock);
  1050. return 0;
  1051. }
  1052. #ifdef CONFIG_SLP_DMABUF
  1053. /*
  1054. * figures out the depth of requested format
  1055. */
  1056. static int fimc_fmt_depth_mplane(struct fimc_control *ctrl,
  1057. struct v4l2_format *f, int depth[])
  1058. {
  1059. int ret = 0;
  1060. /* handles only supported pixelformats */
  1061. switch (f->fmt.pix_mp.pixelformat) {
  1062. case V4L2_PIX_FMT_NV12:
  1063. case V4L2_PIX_FMT_NV12T:
  1064. case V4L2_PIX_FMT_NV21:
  1065. f->fmt.pix_mp.num_planes = 2;
  1066. depth[0] = 8;
  1067. depth[1] = 4;
  1068. break;
  1069. case V4L2_PIX_FMT_YUV420:
  1070. case V4L2_PIX_FMT_YVU420:
  1071. f->fmt.pix_mp.num_planes = 3;
  1072. depth[0] = 8;
  1073. depth[1] = 2;
  1074. depth[2] = 2;
  1075. break;
  1076. case V4L2_PIX_FMT_JPEG:
  1077. case V4L2_PIX_FMT_INTERLEAVED:
  1078. f->fmt.pix_mp.num_planes = 1;
  1079. depth[0] = -1;
  1080. fimc_dbg("Compressed format.\n");
  1081. break;
  1082. default:
  1083. fimc_dbg("why am I here?\n");
  1084. ret = -EINVAL;
  1085. break;
  1086. }
  1087. return ret;
  1088. }
  1089. static int fimc_g_fmt_vid_capture_mplane(struct fimc_control *ctrl,
  1090. struct v4l2_format *f)
  1091. {
  1092. int depth[VIDEO_MAX_PLANES];
  1093. int ret;
  1094. int i;
  1095. /*
  1096. * Note that expecting format only can be with
  1097. * available output format from FIMC
  1098. * Following items should be handled in driver
  1099. * bytesperline = width * depth / 8
  1100. * sizeimage = bytesperline * height
  1101. */
  1102. f->fmt.pix_mp.pixelformat = ctrl->cap->fmt.pixelformat;
  1103. f->fmt.pix_mp.width = ctrl->cap->fmt.width;
  1104. f->fmt.pix_mp.height = ctrl->cap->fmt.height;
  1105. ret = fimc_fmt_depth_mplane(ctrl, f, depth);
  1106. if (ret < 0) {
  1107. fimc_err("Invaild format\n");
  1108. return ret;
  1109. }
  1110. for (i = 0; i < f->fmt.pix_mp.num_planes; ++i) {
  1111. f->fmt.pix_mp.plane_fmt[i].bytesperline = (f->fmt.pix_mp.width *
  1112. depth[i]) >> 3;
  1113. f->fmt.pix_mp.plane_fmt[i].sizeimage =
  1114. (f->fmt.pix_mp.plane_fmt[i].bytesperline *
  1115. f->fmt.pix_mp.width);
  1116. }
  1117. return 0;
  1118. }
  1119. #endif
  1120. int fimc_g_fmt_vid_capture(struct file *file, void *fh, struct v4l2_format *f)
  1121. {
  1122. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  1123. fimc_dbg("%s\n", __func__);
  1124. if (!ctrl->cap) {
  1125. fimc_err("%s: no capture device info\n", __func__);
  1126. return -EINVAL;
  1127. }
  1128. mutex_lock(&ctrl->v4l2_lock);
  1129. #ifdef CONFIG_SLP_DMABUF
  1130. if (V4L2_TYPE_IS_MULTIPLANAR(f->type))
  1131. if (fimc_g_fmt_vid_capture_mplane(ctrl, f) < 0) {
  1132. mutex_unlock(&ctrl->v4l2_lock);
  1133. return -EINVAL;
  1134. }
  1135. else {
  1136. memset(&f->fmt.pix, 0, sizeof(f->fmt.pix));
  1137. memcpy(&f->fmt.pix, &ctrl->cap->fmt, sizeof(f->fmt.pix));
  1138. }
  1139. #else
  1140. memset(&f->fmt.pix, 0, sizeof(f->fmt.pix));
  1141. memcpy(&f->fmt.pix, &ctrl->cap->fmt, sizeof(f->fmt.pix));
  1142. #endif
  1143. mutex_unlock(&ctrl->v4l2_lock);
  1144. return 0;
  1145. }
  1146. /*
  1147. * Check for whether the requested format
  1148. * can be streamed out from FIMC
  1149. * depends on FIMC node
  1150. */
  1151. static int fimc_fmt_avail(struct fimc_control *ctrl,
  1152. struct v4l2_pix_format *f)
  1153. {
  1154. int i;
  1155. /*
  1156. * TODO: check for which FIMC is used.
  1157. * Available fmt should be varied for each FIMC
  1158. */
  1159. for (i = 0; i < ARRAY_SIZE(capture_fmts); i++) {
  1160. if (capture_fmts[i].pixelformat == f->pixelformat)
  1161. return 0;
  1162. }
  1163. fimc_info1("Not supported pixelformat requested\n");
  1164. return -1;
  1165. }
  1166. /*
  1167. * figures out the depth of requested format
  1168. */
  1169. static int fimc_fmt_depth(struct fimc_control *ctrl, struct v4l2_pix_format *f)
  1170. {
  1171. int err, depth = 0;
  1172. /* First check for available format or not */
  1173. err = fimc_fmt_avail(ctrl, f);
  1174. if (err < 0)
  1175. return -1;
  1176. /* handles only supported pixelformats */
  1177. switch (f->pixelformat) {
  1178. case V4L2_PIX_FMT_RGB32:
  1179. depth = 32;
  1180. fimc_dbg("32bpp\n");
  1181. break;
  1182. case V4L2_PIX_FMT_RGB565:
  1183. case V4L2_PIX_FMT_YUYV:
  1184. case V4L2_PIX_FMT_UYVY:
  1185. case V4L2_PIX_FMT_VYUY:
  1186. case V4L2_PIX_FMT_YVYU:
  1187. case V4L2_PIX_FMT_YUV422P:
  1188. case V4L2_PIX_FMT_NV16:
  1189. case V4L2_PIX_FMT_NV61:
  1190. depth = 16;
  1191. fimc_dbg("16bpp\n");
  1192. break;
  1193. case V4L2_PIX_FMT_NV12:
  1194. case V4L2_PIX_FMT_NV12T:
  1195. case V4L2_PIX_FMT_NV21:
  1196. case V4L2_PIX_FMT_YUV420:
  1197. case V4L2_PIX_FMT_YVU420:
  1198. depth = 12;
  1199. fimc_dbg("12bpp\n");
  1200. break;
  1201. case V4L2_PIX_FMT_JPEG:
  1202. case V4L2_PIX_FMT_INTERLEAVED:
  1203. depth = -1;
  1204. fimc_dbg("Compressed format.\n");
  1205. break;
  1206. default:
  1207. fimc_dbg("why am I here?\n");
  1208. break;
  1209. }
  1210. return depth;
  1211. }
  1212. int fimc_s_fmt_vid_private(struct file *file, void *fh, struct v4l2_format *f)
  1213. {
  1214. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  1215. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  1216. struct v4l2_mbus_framefmt *mbus_fmt;
  1217. int ret = 0;
  1218. fimc_dbg("%s\n", __func__);
  1219. if (ctrl->cam->sd) {
  1220. struct v4l2_pix_format *pix = &f->fmt.pix;
  1221. int depth;
  1222. fimc_info1("%s %d:\n", __func__, __LINE__);
  1223. mbus_fmt = &ctrl->cap->mbus_fmt;
  1224. mbus_fmt->width = pix->width;
  1225. mbus_fmt->height = pix->height;
  1226. #ifdef CONFIG_MACH_P4NOTE
  1227. /* Unfortuntely, we have to use pix->field (not pix->priv) since
  1228. * pix.field is already used in the below else condtion statement
  1229. * (in case that sub-devices are not registered)
  1230. */
  1231. mbus_fmt->field = pix->field;
  1232. #endif
  1233. #if defined(CONFIG_MACH_GC1)
  1234. mbus_fmt->field = pix->priv;
  1235. #endif
  1236. printk(KERN_INFO "%s mbus_fmt->width = %d, height = %d,\n",
  1237. __func__,mbus_fmt->width ,mbus_fmt->height);
  1238. depth = fimc_fmt_depth(ctrl, pix);
  1239. if (depth == 0) {
  1240. fimc_err("%s: Invalid pixel format\n", __func__);
  1241. return -EINVAL;
  1242. } else if (depth < 0) { /* JPEG */
  1243. mbus_fmt->code = V4L2_MBUS_FMT_JPEG_1X8;
  1244. mbus_fmt->colorspace = V4L2_COLORSPACE_JPEG;
  1245. } else {
  1246. mbus_fmt->code = V4L2_MBUS_FMT_VYUY8_2X8;
  1247. }
  1248. if (fimc_cam_use) {
  1249. ret = v4l2_subdev_call(ctrl->cam->sd, video,
  1250. s_mbus_fmt, mbus_fmt);
  1251. if (ret) {
  1252. fimc_err("%s: fail to s_mbus_fmt\n", __func__);
  1253. return ret;
  1254. }
  1255. }
  1256. return 0;
  1257. } else {
  1258. mbus_fmt = kzalloc(sizeof(*mbus_fmt), GFP_KERNEL);
  1259. if (!mbus_fmt) {
  1260. fimc_err("%s: no memory for "
  1261. "mbus_fmt\n", __func__);
  1262. return -ENOMEM;
  1263. }
  1264. ctrl->is.fmt.width = f->fmt.pix.width;
  1265. ctrl->is.fmt.height = f->fmt.pix.height;
  1266. ctrl->is.fmt.pixelformat = f->fmt.pix.pixelformat;
  1267. mbus_fmt->width = f->fmt.pix.width;
  1268. mbus_fmt->height = f->fmt.pix.height;
  1269. mbus_fmt->code = V4L2_MBUS_FMT_YUYV8_2X8; /*dummy*/
  1270. mbus_fmt->field = f->fmt.pix.field;
  1271. mbus_fmt->colorspace = V4L2_COLORSPACE_SRGB;
  1272. printk(KERN_INFO "%s mbus_fmt->width = %d, height = %d, \n",
  1273. __func__,mbus_fmt->width ,mbus_fmt->height);
  1274. if (fimc_cam_use)
  1275. ret = v4l2_subdev_call(ctrl->is.sd, video,
  1276. s_mbus_fmt, mbus_fmt);
  1277. kfree(mbus_fmt);
  1278. return ret;
  1279. }
  1280. return -EINVAL;
  1281. }
  1282. int fimc_s_fmt_vid_capture(struct file *file, void *fh, struct v4l2_format *f)
  1283. {
  1284. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  1285. struct fimc_capinfo *cap = ctrl->cap;
  1286. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  1287. int ret = 0;
  1288. int depth;
  1289. struct v4l2_control is_ctrl;
  1290. is_ctrl.id = 0;
  1291. is_ctrl.value = 0;
  1292. printk(KERN_INFO "%s\n", __func__);
  1293. if (!ctrl->cap) {
  1294. fimc_err("%s: No capture structure." \
  1295. "you have to call s_input first.\n", __func__);
  1296. return -ENODEV;
  1297. }
  1298. /* rotaton, flip, dtp_mode, movie_mode and vt_mode,
  1299. * sensor_output_width,height should be maintained.(by TN) */
  1300. memset(cap, 0, sizeof(*cap) - sizeof(u32) * 7);
  1301. mutex_lock(&ctrl->v4l2_lock);
  1302. memset(&cap->fmt, 0, sizeof(cap->fmt));
  1303. #ifdef CONFIG_SLP_DMABUF
  1304. if (V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
  1305. cap->fmt.pixelformat = f->fmt.pix_mp.pixelformat;
  1306. cap->fmt.width = f->fmt.pix_mp.width;
  1307. cap->fmt.height = f->fmt.pix_mp.height;
  1308. cap->fmt.pixelformat = f->fmt.pix_mp.pixelformat;
  1309. } else
  1310. memcpy(&cap->fmt, &f->fmt.pix, sizeof(cap->fmt));
  1311. #else
  1312. memcpy(&cap->fmt, &f->fmt.pix, sizeof(cap->fmt));
  1313. #endif
  1314. /*
  1315. * Note that expecting format only can be with
  1316. * available output format from FIMC
  1317. * Following items should be handled in driver
  1318. * bytesperline = width * depth / 8
  1319. * sizeimage = bytesperline * height
  1320. */
  1321. /* This function may return 0 or -1 in case of error,
  1322. * hence need to check here.
  1323. */
  1324. depth = fimc_fmt_depth(ctrl, &cap->fmt);
  1325. if (depth == 0) {
  1326. mutex_unlock(&ctrl->v4l2_lock);
  1327. fimc_err("%s: Invalid pixel format\n", __func__);
  1328. return -EINVAL;
  1329. } else if (depth < 0) {
  1330. /*
  1331. * When the pixelformat is JPEG,
  1332. * the application is requesting for data
  1333. * in JPEG compressed format
  1334. */
  1335. cap->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  1336. } else {
  1337. cap->fmt.bytesperline = (cap->fmt.width * depth) >> 3;
  1338. cap->fmt.sizeimage = (cap->fmt.bytesperline * cap->fmt.height);
  1339. }
  1340. if (cap->fmt.pixelformat == V4L2_PIX_FMT_JPEG ||
  1341. cap->fmt.pixelformat == V4L2_PIX_FMT_INTERLEAVED) {
  1342. ctrl->sc.bypass = 1;
  1343. cap->lastirq = 0;
  1344. fimc_info1("fimc_s_fmt_vid_capture V4L2_COLORSPACE_JPEG or INTERLEAVED\n");
  1345. } else {
  1346. #ifdef CONFIG_MACH_GC1
  1347. /*
  1348. Fimc scaler input Hsize is restricted to 4224 pixels.
  1349. So, GC1 has to bypass fimc scaler to use more than 12M YUV.
  1350. */
  1351. if (cap->fmt.width > ctrl->limit->pre_dst_w)
  1352. ctrl->sc.bypass = 1;
  1353. else
  1354. ctrl->sc.bypass = 0;
  1355. #else
  1356. ctrl->sc.bypass = 0;
  1357. #endif
  1358. cap->lastirq = 0;
  1359. }
  1360. printk(KERN_INFO "fimc%d s_fmt width = %d, height = %d\n", ctrl->id, \
  1361. cap->fmt.width, cap->fmt.height);
  1362. /* WriteBack doesn't have subdev_call */
  1363. if (ctrl->cam->id == CAMERA_WB || ctrl->cam->id == CAMERA_WB_B) {
  1364. mutex_unlock(&ctrl->v4l2_lock);
  1365. return 0;
  1366. }
  1367. if (ctrl->is.sd && fimc_cam_use) {
  1368. ctrl->is.mbus_fmt.code = V4L2_MBUS_FMT_SGRBG10_1X10;
  1369. is_ctrl.id = V4L2_CID_IS_GET_SENSOR_WIDTH;
  1370. is_ctrl.value = 0;
  1371. v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, &is_ctrl);
  1372. ctrl->is.fmt.width = ctrl->is.mbus_fmt.width = is_ctrl.value;
  1373. is_ctrl.id = V4L2_CID_IS_GET_SENSOR_HEIGHT;
  1374. is_ctrl.value = 0;
  1375. v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, &is_ctrl);
  1376. ctrl->is.fmt.height = ctrl->is.mbus_fmt.height = is_ctrl.value;
  1377. /* default offset values */
  1378. ctrl->is.offset_x = 16;
  1379. ctrl->is.offset_y = 12;
  1380. }
  1381. fimc_hwset_reset(ctrl);
  1382. mutex_unlock(&ctrl->v4l2_lock);
  1383. printk(KERN_INFO "%s -- FIMC%d\n", __func__, ctrl->id);
  1384. return ret;
  1385. }
  1386. int fimc_try_fmt_vid_capture(struct file *file, void *fh, struct v4l2_format *f)
  1387. {
  1388. /* Not implement */
  1389. return -ENOTTY;
  1390. }
  1391. static int fimc_alloc_buffers(struct fimc_control *ctrl,
  1392. int plane, int size, int align, int bpp, int use_paddingbuf, int pad_size)
  1393. {
  1394. struct fimc_capinfo *cap = ctrl->cap;
  1395. int i, j;
  1396. int plane_length[4] = {0, };
  1397. #ifdef CONFIG_VIDEO_SAMSUNG_USE_DMA_MEM
  1398. int alloc_size, err;
  1399. struct cma_info mem_info;
  1400. #endif
  1401. switch (plane) {
  1402. case 1:
  1403. if (align) {
  1404. plane_length[0] = PAGE_ALIGN((size*bpp) >> 3);
  1405. plane_length[1] = 0;
  1406. plane_length[2] = 0;
  1407. } else {
  1408. plane_length[0] = (size*bpp) >> 3;
  1409. plane_length[1] = 0;
  1410. plane_length[2] = 0;
  1411. }
  1412. break;
  1413. /* In case of 2, only NV12 and NV12T is supported. */
  1414. case 2:
  1415. if (align) {
  1416. plane_length[0] = PAGE_ALIGN((size*8) >> 3);
  1417. plane_length[1] = PAGE_ALIGN((size*(bpp-8)) >> 3);
  1418. plane_length[2] = 0;
  1419. fimc_info2("plane_length[0] = %d, plane_length[1] = %d\n" \
  1420. , plane_length[0], plane_length[1]);
  1421. } else {
  1422. plane_length[0] = ((size*8) >> 3);
  1423. plane_length[1] = ((size*(bpp-8)) >> 3);
  1424. plane_length[2] = 0;
  1425. fimc_info2("plane_length[0] = %d, plane_length[1] = %d\n" \
  1426. , plane_length[0], plane_length[1]);
  1427. }
  1428. break;
  1429. /* In case of 3
  1430. * YUV422 : 8 / 4 / 4 (bits)
  1431. * YUV420 : 8 / 2 / 2 (bits)
  1432. * 3rd plane have to consider page align for mmap */
  1433. case 3:
  1434. if (align) {
  1435. plane_length[0] = (size*8) >> 3;
  1436. plane_length[1] = (size*((bpp-8)/2)) >> 3;
  1437. plane_length[2] = PAGE_ALIGN((size*bpp)>>3) - plane_length[0]
  1438. - plane_length[1];
  1439. } else {
  1440. plane_length[0] = (size*8) >> 3;
  1441. plane_length[1] = (size*((bpp-8)/2)) >> 3;
  1442. plane_length[2] = ((size*bpp)>>3) - plane_length[0]
  1443. - plane_length[1];
  1444. }
  1445. break;
  1446. default:
  1447. fimc_err("impossible!\n");
  1448. return -ENOMEM;
  1449. }
  1450. if (use_paddingbuf) {
  1451. plane_length[plane] = pad_size;
  1452. cap->pktdata_plane = plane;
  1453. } else
  1454. plane_length[plane] = 0;
  1455. #ifdef CONFIG_VIDEO_SAMSUNG_USE_DMA_MEM
  1456. if (align) {
  1457. alloc_size = (ALIGN(plane_length[0], align) +
  1458. ALIGN(plane_length[1], align)
  1459. + ALIGN(plane_length[2], align))
  1460. * cap->nr_bufs;
  1461. } else {
  1462. alloc_size = (plane_length[0] + plane_length[1] +
  1463. plane_length[2]) * cap->nr_bufs;
  1464. }
  1465. err = cma_info(&mem_info, ctrl->dev, 0);
  1466. printk(KERN_DEBUG "%s : [cma_info] start_addr : 0x%x, end_addr : 0x%x, "
  1467. "total_size : 0x%x, free_size : 0x%x req_size : 0x%x\n",
  1468. __func__, mem_info.lower_bound, mem_info.upper_bound,
  1469. mem_info.total_size, mem_info.free_size, alloc_size);
  1470. if (err || (mem_info.free_size < alloc_size)) {
  1471. fimc_err("%s: get cma info failed\n", __func__);
  1472. ctrl->mem.size = 0;
  1473. ctrl->mem.base = 0;
  1474. return -ENOMEM;
  1475. } else {
  1476. ctrl->mem.size = alloc_size;
  1477. ctrl->mem.base = (dma_addr_t)cma_alloc
  1478. (ctrl->dev, ctrl->cma_name, (size_t) alloc_size, align);
  1479. }
  1480. ctrl->mem.curr = ctrl->mem.base;
  1481. #endif
  1482. for (i = 0; i < cap->nr_bufs; i++) {
  1483. for (j = 0; j < plane; j++) {
  1484. cap->bufs[i].length[j] = plane_length[j];
  1485. fimc_dma_alloc(ctrl, &cap->bufs[i], j, align);
  1486. if (!cap->bufs[i].base[j])
  1487. goto err_alloc;
  1488. }
  1489. if (use_paddingbuf) {
  1490. cap->bufs[i].length[plane] = plane_length[plane];
  1491. fimc_dma_alloc(ctrl, &cap->bufs[i], plane, align);
  1492. cap->bufs[i].vaddr_pktdata = phys_to_virt(cap->bufs[i].base[plane]);
  1493. /* printk(KERN_INFO "pktdata address = 0x%x, 0x%x\n"
  1494. ,cap->bufs[i].base[1], cap->bufs[i].vaddr_pktdata ); */
  1495. if (!cap->bufs[i].base[plane])
  1496. goto err_alloc;
  1497. }
  1498. cap->bufs[i].state = VIDEOBUF_PREPARED;
  1499. }
  1500. return 0;
  1501. err_alloc:
  1502. for (i = 0; i < cap->nr_bufs; i++) {
  1503. for (j = 0; j < plane; j++) {
  1504. if (cap->bufs[i].base[j])
  1505. fimc_dma_free(ctrl, &cap->bufs[i], j);
  1506. }
  1507. if (use_paddingbuf) {
  1508. if (cap->bufs[i].base[plane])
  1509. fimc_dma_free(ctrl, &cap->bufs[i], plane);
  1510. }
  1511. memset(&cap->bufs[i], 0, sizeof(cap->bufs[i]));
  1512. }
  1513. return -ENOMEM;
  1514. }
  1515. static void fimc_free_buffers(struct fimc_control *ctrl)
  1516. {
  1517. struct fimc_capinfo *cap;
  1518. int i;
  1519. if (ctrl && ctrl->cap)
  1520. cap = ctrl->cap;
  1521. else
  1522. return;
  1523. for (i = 0; i < FIMC_PHYBUFS; i++) {
  1524. memset(&cap->bufs[i], 0, sizeof(cap->bufs[i]));
  1525. cap->bufs[i].state = VIDEOBUF_NEEDS_INIT;
  1526. }
  1527. ctrl->mem.curr = ctrl->mem.base;
  1528. }
  1529. #ifdef CONFIG_SLP_DMABUF
  1530. static int fimc_set_cap_num_plane(struct fimc_control *ctrl)
  1531. {
  1532. struct fimc_capinfo *cap = ctrl->cap;
  1533. switch (cap->fmt.pixelformat) {
  1534. case V4L2_PIX_FMT_RGB32: /* fall through */
  1535. case V4L2_PIX_FMT_RGB565: /* fall through */
  1536. case V4L2_PIX_FMT_YUYV: /* fall through */
  1537. case V4L2_PIX_FMT_UYVY: /* fall through */
  1538. case V4L2_PIX_FMT_VYUY: /* fall through */
  1539. case V4L2_PIX_FMT_YVYU: /* fall through */
  1540. case V4L2_PIX_FMT_NV16: /* fall through */
  1541. case V4L2_PIX_FMT_NV61: /* fall through */
  1542. case V4L2_PIX_FMT_JPEG:
  1543. case V4L2_PIX_FMT_INTERLEAVED:
  1544. return PLANE_1;
  1545. case V4L2_PIX_FMT_NV21:
  1546. case V4L2_PIX_FMT_NV12:
  1547. case V4L2_PIX_FMT_NV12T:
  1548. return PLANE_2;
  1549. case V4L2_PIX_FMT_YUV422P: /* fall through */
  1550. case V4L2_PIX_FMT_YUV420:
  1551. case V4L2_PIX_FMT_YVU420:
  1552. return PLANE_3;
  1553. default:
  1554. fimc_err("%s: Undefined format\n", __func__);
  1555. break;
  1556. }
  1557. return 0;
  1558. }
  1559. #endif
  1560. int fimc_reqbufs_capture_mmap(void *fh, struct v4l2_requestbuffers *b)
  1561. {
  1562. struct fimc_control *ctrl = fh;
  1563. struct fimc_capinfo *cap = ctrl->cap;
  1564. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  1565. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  1566. struct platform_device *pdev = to_platform_device(ctrl->dev);
  1567. #endif
  1568. int ret = 0, i;
  1569. int bpp = 0;
  1570. int size = 0;
  1571. if (!cap) {
  1572. fimc_err("%s: no capture device info\n", __func__);
  1573. return -ENODEV;
  1574. }
  1575. mutex_lock(&ctrl->v4l2_lock);
  1576. /* A count value of zero frees all buffers */
  1577. if ((b->count == 0) || (b->count >= FIMC_CAPBUFS)) {
  1578. /* aborting or finishing any DMA in progress */
  1579. if (ctrl->status == FIMC_STREAMON)
  1580. fimc_streamoff_capture(fh);
  1581. for (i = 0; i < FIMC_CAPBUFS; i++) {
  1582. fimc_dma_free(ctrl, &ctrl->cap->bufs[i], 0);
  1583. fimc_dma_free(ctrl, &ctrl->cap->bufs[i], 1);
  1584. fimc_dma_free(ctrl, &ctrl->cap->bufs[i], 2);
  1585. }
  1586. #ifdef CONFIG_VIDEO_SAMSUNG_USE_DMA_MEM
  1587. if (ctrl->mem.base) {
  1588. cma_free(ctrl->mem.base);
  1589. ctrl->mem.base = 0;
  1590. ctrl->mem.size = 0;
  1591. }
  1592. #endif
  1593. mutex_unlock(&ctrl->v4l2_lock);
  1594. return 0;
  1595. }
  1596. /* free previous buffers */
  1597. if ((cap->nr_bufs >= 0) && (cap->nr_bufs < FIMC_CAPBUFS)) {
  1598. fimc_info1("%s : remained previous buffer count is %d\n", __func__,
  1599. cap->nr_bufs);
  1600. for (i = 0; i < cap->nr_bufs; i++) {
  1601. fimc_dma_free(ctrl, &cap->bufs[i], 0);
  1602. fimc_dma_free(ctrl, &cap->bufs[i], 1);
  1603. fimc_dma_free(ctrl, &cap->bufs[i], 2);
  1604. fimc_dma_free(ctrl, &cap->bufs[i], 3);
  1605. }
  1606. #ifdef CONFIG_VIDEO_SAMSUNG_USE_DMA_MEM
  1607. if (ctrl->mem.base) {
  1608. cma_free(ctrl->mem.base);
  1609. ctrl->mem.base = 0;
  1610. ctrl->mem.size = 0;
  1611. }
  1612. #endif
  1613. }
  1614. fimc_free_buffers(ctrl);
  1615. cap->nr_bufs = b->count;
  1616. if (pdata->hw_ver >= 0x51) {
  1617. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  1618. if (ctrl->power_status == FIMC_POWER_OFF) {
  1619. pm_runtime_get_sync(&pdev->dev);
  1620. }
  1621. #endif
  1622. fimc_hw_reset_output_buf_sequence(ctrl);
  1623. for (i = 0; i < cap->nr_bufs; i++) {
  1624. fimc_hwset_output_buf_sequence(ctrl, i, 1);
  1625. cap->bufs[i].id = i;
  1626. cap->bufs[i].state = VIDEOBUF_NEEDS_INIT;
  1627. /* initialize list */
  1628. INIT_LIST_HEAD(&cap->bufs[i].list);
  1629. }
  1630. fimc_info1("%s: requested %d buffers\n", __func__, b->count);
  1631. fimc_info1("%s: sequence[%d]\n", __func__,
  1632. fimc_hwget_output_buf_sequence(ctrl));
  1633. INIT_LIST_HEAD(&cap->outgoing_q);
  1634. }
  1635. if (pdata->hw_ver < 0x51) {
  1636. INIT_LIST_HEAD(&cap->inq);
  1637. for (i = 0; i < cap->nr_bufs; i++) {
  1638. cap->bufs[i].id = i;
  1639. cap->bufs[i].state = VIDEOBUF_NEEDS_INIT;
  1640. /* initialize list */
  1641. INIT_LIST_HEAD(&cap->bufs[i].list);
  1642. }
  1643. }
  1644. if (cap->pktdata_enable)
  1645. cap->pktdata_size = 0x1000;
  1646. bpp = fimc_fmt_depth(ctrl, &cap->fmt);
  1647. switch (cap->fmt.pixelformat) {
  1648. case V4L2_PIX_FMT_RGB32: /* fall through */
  1649. case V4L2_PIX_FMT_RGB565: /* fall through */
  1650. case V4L2_PIX_FMT_YUYV: /* fall through */
  1651. case V4L2_PIX_FMT_UYVY: /* fall through */
  1652. case V4L2_PIX_FMT_VYUY: /* fall through */
  1653. case V4L2_PIX_FMT_YVYU: /* fall through */
  1654. case V4L2_PIX_FMT_NV16: /* fall through */
  1655. case V4L2_PIX_FMT_NV61: /* fall through */
  1656. fimc_info1("%s : 1plane\n", __func__);
  1657. ret = fimc_alloc_buffers(ctrl, 1,
  1658. cap->fmt.width * cap->fmt.height, SZ_4K, bpp, cap->pktdata_enable, cap->pktdata_size);
  1659. break;
  1660. case V4L2_PIX_FMT_NV21:
  1661. fimc_info1("%s : 2plane for NV21 w %d h %d\n", __func__,
  1662. cap->fmt.width, cap->fmt.height);
  1663. ret = fimc_alloc_buffers(ctrl, 2,
  1664. cap->fmt.width * cap->fmt.height, 0, bpp, cap->pktdata_enable, cap->pktdata_size);
  1665. break;
  1666. case V4L2_PIX_FMT_NV12:
  1667. fimc_info1("%s : 2plane for NV12\n", __func__);
  1668. ret = fimc_alloc_buffers(ctrl, 2,
  1669. cap->fmt.width * cap->fmt.height, SZ_64K, bpp, cap->pktdata_enable, cap->pktdata_size);
  1670. break;
  1671. case V4L2_PIX_FMT_NV12T:
  1672. fimc_info1("%s : 2plane for NV12T\n", __func__);
  1673. ret = fimc_alloc_buffers(ctrl, 2,
  1674. ALIGN(cap->fmt.width, 128) * ALIGN(cap->fmt.height, 32),
  1675. SZ_64K, bpp, cap->pktdata_enable, cap->pktdata_size);
  1676. break;
  1677. case V4L2_PIX_FMT_YUV422P: /* fall through */
  1678. case V4L2_PIX_FMT_YUV420:
  1679. case V4L2_PIX_FMT_YVU420:
  1680. fimc_info1("%s : 3plane\n", __func__);
  1681. ret = fimc_alloc_buffers(ctrl, 3,
  1682. cap->fmt.width * cap->fmt.height, 0, bpp, cap->pktdata_enable, cap->pktdata_size);
  1683. break;
  1684. case V4L2_PIX_FMT_JPEG:
  1685. fimc_info1("%s : JPEG 1plane\n", __func__);
  1686. size = fimc_camera_get_jpeg_memsize(ctrl);
  1687. fimc_info2("%s : JPEG 1plane size = %x\n", __func__, size);
  1688. ret = fimc_alloc_buffers(ctrl, 1, size, 0, 8, cap->pktdata_enable, cap->pktdata_size);
  1689. break;
  1690. case V4L2_PIX_FMT_INTERLEAVED:
  1691. fimc_info1("%s : Interleaved Format\n", __func__);
  1692. size = fimc_camera_get_jpeg_memsize(ctrl); /*0xA00000*/
  1693. fimc_info2("%s : Interleaved size = %x\n", __func__, size);
  1694. ret = fimc_alloc_buffers(ctrl, 1, size, 0, 8, cap->pktdata_enable, cap->pktdata_size);
  1695. break;
  1696. default:
  1697. break;
  1698. }
  1699. if (ret) {
  1700. fimc_err("%s: no memory for capture buffer\n", __func__);
  1701. mutex_unlock(&ctrl->v4l2_lock);
  1702. return -ENOMEM;
  1703. }
  1704. mutex_unlock(&ctrl->v4l2_lock);
  1705. return 0;
  1706. }
  1707. int fimc_reqbufs_capture_userptr(void *fh, struct v4l2_requestbuffers *b)
  1708. {
  1709. struct fimc_control *ctrl = fh;
  1710. struct fimc_capinfo *cap = ctrl->cap;
  1711. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  1712. struct platform_device *pdev = to_platform_device(ctrl->dev);
  1713. #endif
  1714. int i;
  1715. if (!cap) {
  1716. fimc_err("%s: no capture device info\n", __func__);
  1717. return -ENODEV;
  1718. }
  1719. mutex_lock(&ctrl->v4l2_lock);
  1720. /* A count value of zero frees all buffers */
  1721. if ((b->count == 0) || (b->count >= FIMC_CAPBUFS)) {
  1722. /* aborting or finishing any DMA in progress */
  1723. if (ctrl->status == FIMC_STREAMON)
  1724. fimc_streamoff_capture(fh);
  1725. fimc_free_buffers(ctrl);
  1726. #ifdef CONFIG_SLP_DMABUF
  1727. if (b->memory == V4L2_MEMORY_DMABUF)
  1728. _fimc_queue_free(ctrl, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1729. #endif
  1730. mutex_unlock(&ctrl->v4l2_lock);
  1731. return 0;
  1732. }
  1733. /* free previous buffers */
  1734. if ((cap->nr_bufs >= 0) && (cap->nr_bufs < FIMC_CAPBUFS)) {
  1735. fimc_info1("%s: prev buf cnt(%d)\n", __func__, cap->nr_bufs);
  1736. fimc_free_buffers(ctrl);
  1737. }
  1738. cap->nr_bufs = b->count;
  1739. #ifdef CONFIG_SLP_DMABUF
  1740. if (b->memory == V4L2_MEMORY_DMABUF) {
  1741. int num_planes;
  1742. num_planes = fimc_set_cap_num_plane(ctrl);
  1743. if (!num_planes) {
  1744. mutex_unlock(&ctrl->v4l2_lock);
  1745. return 0;
  1746. }
  1747. fimc_queue_alloc(ctrl, b->type, b->memory,
  1748. cap->nr_bufs, num_planes);
  1749. }
  1750. #endif
  1751. #if (defined(CONFIG_EXYNOS_DEV_PD) && defined(CONFIG_PM_RUNTIME))
  1752. if (ctrl->power_status == FIMC_POWER_OFF) {
  1753. pm_runtime_get_sync(&pdev->dev);
  1754. }
  1755. #endif
  1756. fimc_hw_reset_output_buf_sequence(ctrl);
  1757. for (i = 0; i < cap->nr_bufs; i++) {
  1758. fimc_hwset_output_buf_sequence(ctrl, i, 1);
  1759. cap->bufs[i].id = i;
  1760. cap->bufs[i].state = VIDEOBUF_IDLE;
  1761. /* initialize list */
  1762. INIT_LIST_HEAD(&cap->bufs[i].list);
  1763. }
  1764. fimc_info1("%s: requested %d buffers\n", __func__, b->count);
  1765. fimc_info1("%s: sequence[%d]\n", __func__,
  1766. fimc_hwget_output_buf_sequence(ctrl));
  1767. INIT_LIST_HEAD(&cap->outgoing_q);
  1768. mutex_unlock(&ctrl->v4l2_lock);
  1769. return 0;
  1770. }
  1771. int fimc_reqbufs_capture(void *fh, struct v4l2_requestbuffers *b)
  1772. {
  1773. int ret = 0;
  1774. if (b->memory == V4L2_MEMORY_MMAP)
  1775. ret = fimc_reqbufs_capture_mmap(fh, b);
  1776. else
  1777. ret = fimc_reqbufs_capture_userptr(fh, b);
  1778. return ret;
  1779. }
  1780. int fimc_querybuf_capture(void *fh, struct v4l2_buffer *b)
  1781. {
  1782. struct fimc_control *ctrl = fh;
  1783. struct fimc_capinfo *cap = ctrl->cap;
  1784. if (ctrl->status != FIMC_STREAMOFF) {
  1785. fimc_err("fimc is running\n");
  1786. return -EBUSY;
  1787. }
  1788. mutex_lock(&ctrl->v4l2_lock);
  1789. switch (cap->fmt.pixelformat) {
  1790. case V4L2_PIX_FMT_JPEG: /* fall through */
  1791. case V4L2_PIX_FMT_RGB32: /* fall through */
  1792. case V4L2_PIX_FMT_RGB565: /* fall through */
  1793. case V4L2_PIX_FMT_YUYV: /* fall through */
  1794. case V4L2_PIX_FMT_UYVY: /* fall through */
  1795. case V4L2_PIX_FMT_VYUY: /* fall through */
  1796. case V4L2_PIX_FMT_YVYU: /* fall through */
  1797. case V4L2_PIX_FMT_NV16: /* fall through */
  1798. case V4L2_PIX_FMT_NV61:
  1799. case V4L2_PIX_FMT_INTERLEAVED:
  1800. b->length = cap->bufs[b->index].length[0];
  1801. break;
  1802. case V4L2_PIX_FMT_NV21:
  1803. b->length = ctrl->cap->bufs[b->index].length[0]
  1804. + ctrl->cap->bufs[b->index].length[1];
  1805. break;
  1806. case V4L2_PIX_FMT_NV12: /* fall through */
  1807. case V4L2_PIX_FMT_NV12T:
  1808. b->length = ALIGN(ctrl->cap->bufs[b->index].length[0], SZ_64K)
  1809. + ALIGN(ctrl->cap->bufs[b->index].length[1], SZ_64K);
  1810. break;
  1811. case V4L2_PIX_FMT_YUV422P: /* fall through */
  1812. case V4L2_PIX_FMT_YUV420: /* fall through */
  1813. case V4L2_PIX_FMT_YVU420:
  1814. b->length = ctrl->cap->bufs[b->index].length[0]
  1815. + ctrl->cap->bufs[b->index].length[1]
  1816. + ctrl->cap->bufs[b->index].length[2];
  1817. break;
  1818. default:
  1819. b->length = cap->bufs[b->index].length[0];
  1820. break;
  1821. }
  1822. if (cap->pktdata_enable)
  1823. b->length += ctrl->cap->bufs[b->index].length[cap->pktdata_plane];
  1824. b->m.offset = b->index * PAGE_SIZE;
  1825. /* memory field should filled V4L2_MEMORY_MMAP */
  1826. b->memory = V4L2_MEMORY_MMAP;
  1827. ctrl->cap->bufs[b->index].state = VIDEOBUF_IDLE;
  1828. fimc_dbg("%s: %d bytes with offset: %d\n",
  1829. __func__, b->length, b->m.offset);
  1830. mutex_unlock(&ctrl->v4l2_lock);
  1831. return 0;
  1832. }
  1833. int fimc_g_ctrl_capture(void *fh, struct v4l2_control *c)
  1834. {
  1835. struct fimc_control *ctrl = fh;
  1836. int ret = 0;
  1837. fimc_dbg("%s\n", __func__);
  1838. switch (c->id) {
  1839. case V4L2_CID_ROTATION:
  1840. c->value = ctrl->cap->rotate;
  1841. break;
  1842. case V4L2_CID_HFLIP:
  1843. c->value = (ctrl->cap->flip & FIMC_XFLIP) ? 1 : 0;
  1844. break;
  1845. case V4L2_CID_VFLIP:
  1846. c->value = (ctrl->cap->flip & FIMC_YFLIP) ? 1 : 0;
  1847. break;
  1848. case V4L2_CID_CACHEABLE:
  1849. c->value = ctrl->cap->cacheable;
  1850. break;
  1851. default:
  1852. /* get ctrl supported by subdev */
  1853. /* WriteBack doesn't have subdev_call */
  1854. if ((ctrl->cam->id == CAMERA_WB) || (ctrl->cam->id == CAMERA_WB_B))
  1855. break;
  1856. if (ctrl->cam->sd)
  1857. ret = v4l2_subdev_call(ctrl->cam->sd, core, g_ctrl, c);
  1858. if (ctrl->is.sd)
  1859. ret = v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, c);
  1860. break;
  1861. }
  1862. return ret;
  1863. }
  1864. int fimc_s_ctrl_capture(void *fh, struct v4l2_control *c)
  1865. {
  1866. struct fimc_control *ctrl = fh;
  1867. struct fimc_global *fimc = get_fimc_dev();
  1868. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  1869. int ret = 0;
  1870. fimc_dbg("%s\n", __func__);
  1871. if (!ctrl->cam || !ctrl->cap ||
  1872. ((ctrl->cam->id != CAMERA_WB && ctrl->cam->id != CAMERA_WB_B) &&
  1873. (!ctrl->cam->sd) && (!ctrl->is.sd))) {
  1874. fimc_err("%s: No capture device.\n", __func__);
  1875. return -ENODEV;
  1876. }
  1877. switch (c->id) {
  1878. #ifdef CONFIG_MACH_GC1
  1879. case V4L2_CID_CAMERA_HOLD_LENS:
  1880. leave_power = true;
  1881. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ctrl, c);
  1882. break;
  1883. case V4L2_CID_CAM_UPDATE_FW:
  1884. if (c->value == FW_MODE_UPDATE || c->value == FW_MODE_DUMP) {
  1885. if (fimc->mclk_status == CAM_MCLK_ON) {
  1886. if (ctrl->cam->cam_power)
  1887. ctrl->cam->cam_power(0);
  1888. /* shutdown the MCLK */
  1889. clk_disable(ctrl->cam->clk);
  1890. fimc->mclk_status = CAM_MCLK_OFF;
  1891. mdelay(5);
  1892. }
  1893. if ((clk_get_rate(ctrl->cam->clk)) &&
  1894. (fimc->mclk_status == CAM_MCLK_OFF)) {
  1895. clk_set_rate(ctrl->cam->clk,
  1896. ctrl->cam->clk_rate);
  1897. clk_enable(ctrl->cam->clk);
  1898. fimc->mclk_status = CAM_MCLK_ON;
  1899. fimc_info1("clock for camera: %d\n",
  1900. ctrl->cam->clk_rate);
  1901. if (ctrl->cam->cam_power)
  1902. ctrl->cam->cam_power(1);
  1903. }
  1904. if (c->value == FW_MODE_UPDATE)
  1905. ret = v4l2_subdev_call(ctrl->cam->sd, core, load_fw);
  1906. else
  1907. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ctrl, c);
  1908. } else {
  1909. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ctrl, c);
  1910. }
  1911. break;
  1912. #endif
  1913. case V4L2_CID_CAMERA_RESET:
  1914. fimc_warn("ESD: reset the camera sensor\n");
  1915. if (ctrl->cam->initialized) {
  1916. if (ctrl->cam->cam_power)
  1917. ctrl->cam->cam_power(0);
  1918. /* shutdown the MCLK */
  1919. clk_disable(ctrl->cam->clk);
  1920. fimc->mclk_status = CAM_MCLK_OFF;
  1921. ctrl->cam->initialized = 0;
  1922. #ifdef CONFIG_MACH_P4NOTE
  1923. /* 100ms: increase delay.
  1924. * There are cases that sensor doesn't get revived
  1925. * inspite of doing power reset.*/
  1926. msleep(100);
  1927. #else
  1928. msleep(5);
  1929. #endif
  1930. }
  1931. if (ctrl->cam->sd) {
  1932. fimc_warn("ESD: init external sensor\n");
  1933. ret = fimc_init_camera(ctrl);
  1934. }
  1935. if (ctrl->is.sd && ctrl->cam->use_isp) {
  1936. fimc_warn("ESD: init FIMC-IS\n");
  1937. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, 0);
  1938. if (ret < 0) {
  1939. fimc_err("ESD : FIMC-IS power off failed");
  1940. return -EINVAL;
  1941. }
  1942. ret = fimc_is_init_cam(ctrl);
  1943. if (ret < 0) {
  1944. fimc_err("ESD : FIMC-IS init clock failed");
  1945. return -EINVAL;
  1946. }
  1947. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, 1);
  1948. if (ret < 0) {
  1949. fimc_err("ESD : FIMC-IS power on failed");
  1950. return -EINVAL;
  1951. }
  1952. ret = v4l2_subdev_call(ctrl->is.sd, core, load_fw);
  1953. if (ret < 0) {
  1954. fimc_dbg("ESD : FIMC-IS load FW failed");
  1955. return -EINVAL;
  1956. }
  1957. ret = v4l2_subdev_call(ctrl->is.sd, core,
  1958. init, ctrl->cam->sensor_index);
  1959. if (ret < 0) {
  1960. fimc_err("ESD : FIMC-IS init failed");
  1961. return -EINVAL;
  1962. }
  1963. }
  1964. break;
  1965. case V4L2_CID_ROTATION:
  1966. ctrl->cap->rotate = c->value;
  1967. break;
  1968. case V4L2_CID_HFLIP:
  1969. if (c->value)
  1970. ctrl->cap->flip |= FIMC_YFLIP;
  1971. else
  1972. ctrl->cap->flip &= ~FIMC_YFLIP;
  1973. break;
  1974. case V4L2_CID_VFLIP:
  1975. if (c->value)
  1976. ctrl->cap->flip |= FIMC_XFLIP;
  1977. else
  1978. ctrl->cap->flip &= ~FIMC_XFLIP;
  1979. break;
  1980. case V4L2_CID_PADDR_Y:
  1981. if (ctrl->cap->bufs[c->value].length[FIMC_ADDR_Y])
  1982. c->value = ctrl->cap->bufs[c->value].base[FIMC_ADDR_Y];
  1983. break;
  1984. case V4L2_CID_PADDR_CB: /* fall through */
  1985. case V4L2_CID_PADDR_CBCR:
  1986. if (ctrl->cap->bufs[c->value].length[FIMC_ADDR_CB])
  1987. c->value = ctrl->cap->bufs[c->value].base[FIMC_ADDR_CB];
  1988. break;
  1989. case V4L2_CID_PADDR_CR:
  1990. if (ctrl->cap->bufs[c->value].length[FIMC_ADDR_CR])
  1991. c->value = ctrl->cap->bufs[c->value].base[FIMC_ADDR_CR];
  1992. break;
  1993. /* Implementation as per C100 FIMC driver */
  1994. case V4L2_CID_STREAM_PAUSE:
  1995. fimc_hwset_stop_processing(ctrl);
  1996. break;
  1997. case V4L2_CID_IMAGE_EFFECT_APPLY:
  1998. ctrl->fe.ie_on = c->value ? 1 : 0;
  1999. ctrl->fe.ie_after_sc = 0;
  2000. ret = fimc_hwset_image_effect(ctrl);
  2001. break;
  2002. case V4L2_CID_IMAGE_EFFECT_FN:
  2003. if (c->value < 0 || c->value > FIMC_EFFECT_FIN_SILHOUETTE)
  2004. return -EINVAL;
  2005. ctrl->fe.fin = c->value;
  2006. ret = 0;
  2007. break;
  2008. case V4L2_CID_IMAGE_EFFECT_CB:
  2009. ctrl->fe.pat_cb = c->value & 0xFF;
  2010. ret = 0;
  2011. break;
  2012. case V4L2_CID_IMAGE_EFFECT_CR:
  2013. ctrl->fe.pat_cr = c->value & 0xFF;
  2014. ret = 0;
  2015. break;
  2016. case V4L2_CID_IS_LOAD_FW:
  2017. if (ctrl->is.sd && fimc_cam_use)
  2018. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, c->value);
  2019. break;
  2020. case V4L2_CID_IS_RESET:
  2021. if (ctrl->is.sd && fimc_cam_use)
  2022. ret = v4l2_subdev_call(ctrl->is.sd, core, reset, c->value);
  2023. break;
  2024. case V4L2_CID_IS_S_POWER:
  2025. if (ctrl->is.sd && fimc_cam_use)
  2026. ret = v4l2_subdev_call(ctrl->is.sd, core, s_power, c->value);
  2027. break;
  2028. case V4L2_CID_IS_S_STREAM:
  2029. if (ctrl->is.sd && fimc_cam_use)
  2030. ret = v4l2_subdev_call(ctrl->is.sd, video, s_stream, c->value);
  2031. break;
  2032. case V4L2_CID_CACHEABLE:
  2033. ctrl->cap->cacheable = c->value;
  2034. ret = 0;
  2035. break;
  2036. case V4L2_CID_EMBEDDEDDATA_ENABLE:
  2037. ctrl->cap->pktdata_enable = c->value;
  2038. ret = 0;
  2039. break;
  2040. case V4L2_CID_IS_ZOOM:
  2041. fimc_is_set_zoom(ctrl, c);
  2042. break;
  2043. case V4L2_CID_CAMERA_SENSOR_MODE:
  2044. ctrl->cap->movie_mode = c->value;
  2045. if (ctrl->cam->sd && fimc_cam_use)
  2046. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ctrl, c);
  2047. break;
  2048. case V4L2_CID_CAMERA_VT_MODE:
  2049. ctrl->cap->vt_mode = c->value;
  2050. if (fimc_cam_use) {
  2051. if (ctrl->cam->sd)
  2052. ret = v4l2_subdev_call(ctrl->cam->sd,
  2053. core, s_ctrl, c);
  2054. if (ctrl->is.sd && ctrl->cam->use_isp)
  2055. ret = v4l2_subdev_call(ctrl->is.sd,
  2056. core, s_ctrl, c);
  2057. }
  2058. break;
  2059. case V4L2_CID_CAMERA_SENSOR_OUTPUT_SIZE:
  2060. ctrl->cap->sensor_output_width = (u32)c->value >> 16;
  2061. ctrl->cap->sensor_output_height = (u32)c->value & 0x0FFFF;
  2062. printk(KERN_DEBUG "sensor output size: %dx%d\n",
  2063. ctrl->cap->sensor_output_width,
  2064. ctrl->cap->sensor_output_height);
  2065. break;
  2066. #if defined(CONFIG_BUSFREQ_OPP) || defined(CONFIG_BUSFREQ_LOCK_WRAPPER)
  2067. case V4L2_CID_CAMERA_BUSFREQ_LOCK:
  2068. /* lock bus frequency */
  2069. dev_lock(ctrl->bus_dev, ctrl->dev, (unsigned long)c->value);
  2070. break;
  2071. case V4L2_CID_CAMERA_BUSFREQ_UNLOCK:
  2072. /* unlock bus frequency */
  2073. dev_unlock(ctrl->bus_dev, ctrl->dev);
  2074. break;
  2075. #endif
  2076. case V4L2_CID_CAMERA_SET_DUAL_CAPTURE:
  2077. case V4L2_CID_CAMERA_DUAL_CAPTURE:
  2078. case V4L2_CID_CAMERA_DUAL_POSTVIEW:
  2079. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ctrl, c);
  2080. break;
  2081. case V4L2_CID_IS_CAMERA_FLASH_MODE:
  2082. case V4L2_CID_CAMERA_SCENE_MODE:
  2083. default:
  2084. /* try on subdev */
  2085. /* WriteBack doesn't have subdev_call */
  2086. if ((ctrl->cam->id == CAMERA_WB) || \
  2087. (ctrl->cam->id == CAMERA_WB_B))
  2088. break;
  2089. if (fimc_cam_use) {
  2090. if (ctrl->cam->sd)
  2091. ret = v4l2_subdev_call(ctrl->cam->sd,
  2092. core, s_ctrl, c);
  2093. if (ctrl->is.sd && ctrl->cam->use_isp)
  2094. ret = v4l2_subdev_call(ctrl->is.sd,
  2095. core, s_ctrl, c);
  2096. } else
  2097. ret = 0;
  2098. break;
  2099. }
  2100. return ret;
  2101. }
  2102. int fimc_g_ext_ctrls_capture(void *fh, struct v4l2_ext_controls *c)
  2103. {
  2104. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  2105. int ret = 0;
  2106. mutex_lock(&ctrl->v4l2_lock);
  2107. if (ctrl->cam->sd)
  2108. /* try on subdev */
  2109. ret = v4l2_subdev_call(ctrl->cam->sd, core, g_ext_ctrls, c);
  2110. if (ctrl->is.sd)
  2111. /* try on subdev */
  2112. ret = v4l2_subdev_call(ctrl->is.sd, core, g_ext_ctrls, c);
  2113. mutex_unlock(&ctrl->v4l2_lock);
  2114. return ret;
  2115. }
  2116. int fimc_s_ext_ctrls_capture(void *fh, struct v4l2_ext_controls *c)
  2117. {
  2118. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  2119. int ret = 0;
  2120. mutex_lock(&ctrl->v4l2_lock);
  2121. if (ctrl->cam->sd)
  2122. /* try on subdev */
  2123. ret = v4l2_subdev_call(ctrl->cam->sd, core, s_ext_ctrls, c);
  2124. else if (ctrl->is.sd)
  2125. ret = v4l2_subdev_call(ctrl->is.sd, core, s_ext_ctrls, c);
  2126. mutex_unlock(&ctrl->v4l2_lock);
  2127. return ret;
  2128. }
  2129. int fimc_cropcap_capture(void *fh, struct v4l2_cropcap *a)
  2130. {
  2131. struct fimc_control *ctrl = fh;
  2132. struct fimc_capinfo *cap = ctrl->cap;
  2133. struct fimc_global *fimc = get_fimc_dev();
  2134. struct s3c_platform_fimc *pdata;
  2135. fimc_dbg("%s\n", __func__);
  2136. if (!ctrl->cam || !ctrl->cam->sd || !ctrl->cap) {
  2137. fimc_err("%s: No capture device.\n", __func__);
  2138. return -ENODEV;
  2139. }
  2140. mutex_lock(&ctrl->v4l2_lock);
  2141. pdata = to_fimc_plat(ctrl->dev);
  2142. if (!ctrl->cam)
  2143. ctrl->cam = fimc->camera[pdata->default_cam];
  2144. if (!cap) {
  2145. cap = kzalloc(sizeof(*cap), GFP_KERNEL);
  2146. if (!cap) {
  2147. fimc_err("%s: no memory for "
  2148. "capture device info\n", __func__);
  2149. return -ENOMEM;
  2150. }
  2151. /* assign to ctrl */
  2152. ctrl->cap = cap;
  2153. }
  2154. /* crop limitations */
  2155. cap->cropcap.bounds.left = 0;
  2156. cap->cropcap.bounds.top = 0;
  2157. cap->cropcap.bounds.width = ctrl->cam->width;
  2158. cap->cropcap.bounds.height = ctrl->cam->height;
  2159. /* crop default values */
  2160. cap->cropcap.defrect.left = 0;
  2161. cap->cropcap.defrect.top = 0;
  2162. cap->cropcap.defrect.width = ctrl->cam->width;
  2163. cap->cropcap.defrect.height = ctrl->cam->height;
  2164. a->bounds = cap->cropcap.bounds;
  2165. a->defrect = cap->cropcap.defrect;
  2166. mutex_unlock(&ctrl->v4l2_lock);
  2167. return 0;
  2168. }
  2169. int fimc_g_crop_capture(void *fh, struct v4l2_crop *a)
  2170. {
  2171. struct fimc_control *ctrl = fh;
  2172. fimc_dbg("%s\n", __func__);
  2173. if (!ctrl->cap) {
  2174. fimc_err("%s: No capture device.\n", __func__);
  2175. return -ENODEV;
  2176. }
  2177. mutex_lock(&ctrl->v4l2_lock);
  2178. a->c = ctrl->cap->crop;
  2179. mutex_unlock(&ctrl->v4l2_lock);
  2180. return 0;
  2181. }
  2182. int fimc_s_crop_capture(void *fh, struct v4l2_crop *a)
  2183. {
  2184. struct fimc_control *ctrl = fh;
  2185. fimc_dbg("%s\n", __func__);
  2186. mutex_lock(&ctrl->v4l2_lock);
  2187. ctrl->cap->crop = a->c;
  2188. mutex_unlock(&ctrl->v4l2_lock);
  2189. return 0;
  2190. }
  2191. int fimc_start_capture(struct fimc_control *ctrl)
  2192. {
  2193. fimc_dbg("%s\n", __func__);
  2194. fimc_reset_status_reg(ctrl);
  2195. if (!ctrl->sc.bypass)
  2196. fimc_hwset_start_scaler(ctrl);
  2197. fimc_hwset_enable_capture(ctrl, ctrl->sc.bypass);
  2198. fimc_hwset_disable_frame_end_irq(ctrl);
  2199. return 0;
  2200. }
  2201. int fimc_stop_capture(struct fimc_control *ctrl)
  2202. {
  2203. fimc_dbg("%s\n", __func__);
  2204. if (!ctrl->cam) {
  2205. fimc_err("%s: No capture device.\n", __func__);
  2206. return -ENODEV;
  2207. }
  2208. if (!ctrl->cap) {
  2209. fimc_err("%s: No cappure format.\n", __func__);
  2210. return -ENODEV;
  2211. }
  2212. if (ctrl->cap->lastirq) {
  2213. fimc_hwset_enable_lastirq(ctrl);
  2214. fimc_hwset_disable_capture(ctrl);
  2215. fimc_hwset_disable_lastirq(ctrl);
  2216. } else {
  2217. fimc_hwset_disable_capture(ctrl);
  2218. fimc_hwset_enable_frame_end_irq(ctrl);
  2219. }
  2220. fimc_hwset_stop_scaler(ctrl);
  2221. return 0;
  2222. }
  2223. static int fimc_check_capture_source(struct fimc_control *ctrl)
  2224. {
  2225. if(!ctrl->cam)
  2226. return -ENODEV;
  2227. if (ctrl->cam->sd || ctrl->is.sd || !ctrl->flite_sd)
  2228. return 0;
  2229. if (ctrl->cam->id == CAMERA_WB || ctrl->cam->id == CAMERA_WB_B)
  2230. return 0;
  2231. return -ENODEV;
  2232. }
  2233. static int is_scale_up(struct fimc_control *ctrl)
  2234. {
  2235. struct v4l2_mbus_framefmt *mbus_fmt = &ctrl->cap->mbus_fmt;
  2236. struct v4l2_pix_format *pix = &ctrl->cap->fmt;
  2237. if (!mbus_fmt->width) {
  2238. fimc_err("%s: sensor resolution isn't selected.\n", __func__);
  2239. return -EINVAL;
  2240. }
  2241. if (ctrl->cap->rotate == 90 || ctrl->cap->rotate == 270) {
  2242. if (pix->width > mbus_fmt->height ||
  2243. pix->height > mbus_fmt->width) {
  2244. fimc_err("%s: ScaleUp isn't supported.\n", __func__);
  2245. return -EINVAL;
  2246. }
  2247. } else {
  2248. if (pix->width > mbus_fmt->width ||
  2249. pix->height > mbus_fmt->height) {
  2250. fimc_err("%s: ScaleUp isn't supported.\n", __func__);
  2251. return -EINVAL;
  2252. }
  2253. }
  2254. return 0;
  2255. }
  2256. int fimc_streamon_capture(void *fh)
  2257. {
  2258. struct fimc_control *ctrl = fh;
  2259. struct fimc_capinfo *cap = ctrl->cap;
  2260. struct v4l2_frmsizeenum cam_frmsize;
  2261. struct v4l2_control is_ctrl;
  2262. void __iomem *qos_regs;
  2263. int rot = 0, i;
  2264. int ret = 0;
  2265. struct s3c_platform_camera *cam = NULL;
  2266. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  2267. printk(KERN_INFO "%s++ fimc%d\n", __func__, ctrl->id);
  2268. #ifdef CONFIG_SLP
  2269. cam_frmsize.index = -1;
  2270. #endif
  2271. cam_frmsize.discrete.width = 0;
  2272. cam_frmsize.discrete.height = 0;
  2273. is_ctrl.id = 0;
  2274. is_ctrl.value = 0;
  2275. if (!ctrl->cam) {
  2276. fimc_err("%s: ctrl->cam is null\n", __func__);
  2277. return -EINVAL;
  2278. } else {
  2279. cam = ctrl->cam;
  2280. }
  2281. if (fimc_check_capture_source(ctrl)) {
  2282. fimc_err("%s: No capture device.\n", __func__);
  2283. return -ENODEV;
  2284. }
  2285. if (cam->sd) {
  2286. if (is_scale_up(ctrl))
  2287. return -EINVAL;
  2288. }
  2289. if (pdata->hw_ver < 0x51)
  2290. fimc_hw_reset_camera(ctrl);
  2291. #if (!defined(CONFIG_EXYNOS_DEV_PD) && !defined(CONFIG_PM_RUNTIME))
  2292. ctrl->status = FIMC_READY_ON;
  2293. #endif
  2294. cap->irq = 0;
  2295. fimc_hwset_enable_irq(ctrl, 0, 1);
  2296. if ((cam->id != CAMERA_WB) && (cam->id != CAMERA_WB_B)) {
  2297. if (fimc_cam_use && cam->sd) {
  2298. ret = v4l2_subdev_call(cam->sd, video, enum_framesizes,
  2299. &cam_frmsize);
  2300. if (ret < 0) {
  2301. dev_err(ctrl->dev, "%s: enum_framesizes failed\n",
  2302. __func__);
  2303. if (ret != -ENOIOCTLCMD)
  2304. return ret;
  2305. } else {
  2306. if (cam_frmsize.discrete.width > 0
  2307. && cam_frmsize.discrete.height > 0) {
  2308. cam->window.left = 0;
  2309. cam->window.top = 0;
  2310. cam->width = cam->window.width
  2311. = cam_frmsize.discrete.width;
  2312. cam->height
  2313. = cam->window.height
  2314. = cam_frmsize.discrete.height;
  2315. printk(KERN_INFO "%s cam real size width = %d,"
  2316. "height = %d\n",__func__, ctrl->cam->width,
  2317. ctrl->cam->height);
  2318. }
  2319. }
  2320. #ifdef CONFIG_MACH_P4NOTE
  2321. #ifdef CONFIG_VIDEO_IMPROVE_STREAMOFF
  2322. v4l2_subdev_call(cam->sd, video, s_stream,
  2323. STREAM_MODE_WAIT_OFF);
  2324. #endif /* CONFIG_VIDEO_IMPROVE_STREAMOFF */
  2325. #else /* !CONFIG_MACH_P4NOTE */
  2326. if (cap->fmt.priv == V4L2_PIX_FMT_MODE_CAPTURE) {
  2327. ret = v4l2_subdev_call(cam->sd, video, s_stream, 1);
  2328. if (ret < 0) {
  2329. dev_err(ctrl->dev, "%s: s_stream failed\n",
  2330. __func__);
  2331. return ret;
  2332. }
  2333. }
  2334. #endif
  2335. if (cam->type == CAM_TYPE_MIPI) {
  2336. if (cam->id == CAMERA_CSI_C) {
  2337. s3c_csis_enable_pktdata(CSI_CH_0, cap->pktdata_enable);
  2338. s3c_csis_start(CSI_CH_0, cam->mipi_lanes,
  2339. cam->mipi_settle, cam->mipi_align,
  2340. cam->width, cam->height,
  2341. cap->fmt.pixelformat);
  2342. } else {
  2343. s3c_csis_enable_pktdata(CSI_CH_1, cap->pktdata_enable);
  2344. s3c_csis_start(CSI_CH_1, cam->mipi_lanes,
  2345. cam->mipi_settle, cam->mipi_align,
  2346. cam->width, cam->height,
  2347. cap->fmt.pixelformat);
  2348. }
  2349. }
  2350. #ifdef CONFIG_MACH_P4NOTE
  2351. if (1) {
  2352. #else
  2353. if (cap->fmt.priv != V4L2_PIX_FMT_MODE_CAPTURE) {
  2354. #endif
  2355. ret = v4l2_subdev_call(cam->sd, video, s_stream, 1);
  2356. if (ret < 0) {
  2357. dev_err(ctrl->dev, "%s: s_stream failed\n",
  2358. __func__);
  2359. if (cam->id == CAMERA_CSI_C)
  2360. s3c_csis_stop(CSI_CH_0);
  2361. else
  2362. s3c_csis_stop(CSI_CH_1);
  2363. return ret;
  2364. }
  2365. }
  2366. }
  2367. }
  2368. /* Set FIMD to write back */
  2369. if ((cam->id == CAMERA_WB) || (cam->id == CAMERA_WB_B)) {
  2370. if (cam->id == CAMERA_WB)
  2371. fimc_hwset_sysreg_camblk_fimd0_wb(ctrl);
  2372. else
  2373. fimc_hwset_sysreg_camblk_fimd1_wb(ctrl);
  2374. ret = s3cfb_direct_ioctl(0, S3CFB_SET_WRITEBACK, 1);
  2375. if (ret) {
  2376. fimc_err("failed set writeback\n");
  2377. return ret;
  2378. }
  2379. }
  2380. if (ctrl->is.sd && fimc_cam_use) {
  2381. struct platform_device *pdev = to_platform_device(ctrl->dev);
  2382. struct clk *pxl_async = NULL;
  2383. is_ctrl.id = V4L2_CID_IS_GET_SENSOR_OFFSET_X;
  2384. is_ctrl.value = 0;
  2385. v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, &is_ctrl);
  2386. ctrl->is.offset_x = is_ctrl.value;
  2387. is_ctrl.id = V4L2_CID_IS_GET_SENSOR_OFFSET_Y;
  2388. is_ctrl.value = 0;
  2389. v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, &is_ctrl);
  2390. ctrl->is.offset_y = is_ctrl.value;
  2391. fimc_dbg("CSI setting width = %d, height = %d\n",
  2392. ctrl->is.fmt.width + ctrl->is.offset_x,
  2393. ctrl->is.fmt.height + ctrl->is.offset_y);
  2394. if (ctrl->flite_sd && fimc_cam_use) {
  2395. ctrl->is.mbus_fmt.width += ctrl->is.offset_x;
  2396. ctrl->is.mbus_fmt.height += ctrl->is.offset_y;
  2397. ret = v4l2_subdev_call(ctrl->flite_sd, video,
  2398. s_mbus_fmt, &ctrl->is.mbus_fmt);
  2399. }
  2400. if (cam->id == CAMERA_CSI_C)
  2401. s3c_csis_start(CSI_CH_0, cam->mipi_lanes,
  2402. cam->mipi_settle, cam->mipi_align,
  2403. ctrl->is.fmt.width + ctrl->is.offset_x,
  2404. ctrl->is.fmt.height + ctrl->is.offset_y,
  2405. V4L2_PIX_FMT_SGRBG10);
  2406. else if (cam->id == CAMERA_CSI_D)
  2407. s3c_csis_start(CSI_CH_1, cam->mipi_lanes,
  2408. cam->mipi_settle, cam->mipi_align,
  2409. ctrl->is.fmt.width + ctrl->is.offset_x,
  2410. ctrl->is.fmt.height + ctrl->is.offset_y,
  2411. V4L2_PIX_FMT_SGRBG10);
  2412. pxl_async = clk_get(&pdev->dev, "pxl_async1");
  2413. if (IS_ERR(pxl_async)) {
  2414. dev_err(&pdev->dev, "failed to get pxl_async\n");
  2415. return -ENODEV;
  2416. }
  2417. clk_enable(pxl_async);
  2418. clk_put(pxl_async);
  2419. fimc_hwset_sysreg_camblk_isp_wb(ctrl);
  2420. }
  2421. if (ctrl->flite_sd && fimc_cam_use)
  2422. v4l2_subdev_call(ctrl->flite_sd, video, s_stream, 1);
  2423. if (!ctrl->is.sd && cap->movie_mode &&
  2424. !((cam->width == 880 && cam->height == 720))) {
  2425. printk(KERN_INFO "\n\n\n%s pm_qos_req is called..\n", __func__ );
  2426. /*dev_lock(ctrl->bus_dev, ctrl->dev, (unsigned long)400200);*/
  2427. pm_qos_add_request(&bus_qos_pm_qos_req, PM_QOS_BUS_QOS, 1);
  2428. /* ioremap for register block */
  2429. qos_regs = ioremap(0x11a00400, 0x10);
  2430. if (!qos_regs) {
  2431. fimc_err("%s: failed to remap io region\n", __func__);
  2432. return -1;
  2433. }
  2434. writel(0x3, qos_regs + 0x0);
  2435. writel(0x1, qos_regs + 0x4);
  2436. fimc_err("0x11a00400 = 0x%x , 0x11a00404 = 0x%x \n", readl(qos_regs + 0), readl(qos_regs + 4));
  2437. iounmap(qos_regs);
  2438. }
  2439. fimc_hwset_camera_type(ctrl);
  2440. fimc_hwset_camera_polarity(ctrl);
  2441. fimc_hwset_enable_lastend(ctrl);
  2442. if (cap->fmt.pixelformat != V4L2_PIX_FMT_JPEG &&
  2443. cap->fmt.pixelformat != V4L2_PIX_FMT_INTERLEAVED) {
  2444. fimc_hwset_camera_source(ctrl);
  2445. fimc_hwset_camera_offset(ctrl);
  2446. fimc_capture_scaler_info(ctrl);
  2447. fimc_hwset_prescaler(ctrl, &ctrl->sc);
  2448. fimc_hwset_scaler(ctrl, &ctrl->sc);
  2449. fimc_hwset_output_colorspace(ctrl, cap->fmt.pixelformat);
  2450. fimc_hwset_output_addr_style(ctrl, cap->fmt.pixelformat);
  2451. if (cap->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
  2452. cap->fmt.pixelformat == V4L2_PIX_FMT_RGB565)
  2453. fimc_hwset_output_rgb(ctrl, cap->fmt.pixelformat);
  2454. else
  2455. fimc_hwset_output_yuv(ctrl, cap->fmt.pixelformat);
  2456. fimc_hwset_output_area(ctrl, cap->fmt.width, cap->fmt.height);
  2457. fimc_hwset_output_scan(ctrl, &cap->fmt);
  2458. fimc_hwset_output_rot_flip(ctrl, cap->rotate, cap->flip);
  2459. rot = fimc_mapping_rot_flip(cap->rotate, cap->flip);
  2460. if (rot & FIMC_ROT) {
  2461. fimc_hwset_org_output_size(ctrl, cap->fmt.width,
  2462. cap->fmt.height);
  2463. fimc_hwset_output_size(ctrl, cap->fmt.height,
  2464. cap->fmt.width);
  2465. } else {
  2466. fimc_hwset_org_output_size(ctrl, cap->fmt.width,
  2467. cap->fmt.height);
  2468. fimc_hwset_output_size(ctrl, cap->fmt.width,
  2469. cap->fmt.height);
  2470. }
  2471. fimc_hwset_jpeg_mode(ctrl, false);
  2472. } else {
  2473. fimc_hwset_output_size(ctrl,
  2474. cap->fmt.width, cap->fmt.height);
  2475. if (rot & FIMC_ROT)
  2476. fimc_hwset_org_output_size(ctrl,
  2477. cap->fmt.height, cap->fmt.width);
  2478. else
  2479. fimc_hwset_org_output_size(ctrl,
  2480. cap->fmt.width, cap->fmt.height);
  2481. if (cap->fmt.pixelformat == V4L2_PIX_FMT_JPEG)
  2482. fimc_hwset_output_area_size(ctrl,
  2483. fimc_camera_get_jpeg_memsize(ctrl));
  2484. else if (cap->fmt.pixelformat == V4L2_PIX_FMT_INTERLEAVED)
  2485. fimc_hwset_output_area_size(ctrl,
  2486. 0xc00000);
  2487. fimc_hwset_jpeg_mode(ctrl, true);
  2488. }
  2489. if (pdata->hw_ver >= 0x51) {
  2490. for (i = 0; i < cap->nr_bufs; i++)
  2491. fimc_hwset_output_address(ctrl, &cap->bufs[i], i);
  2492. } else {
  2493. for (i = 0; i < FIMC_PINGPONG; i++)
  2494. fimc_add_outqueue(ctrl, i);
  2495. }
  2496. if (cap->fmt.pixelformat == V4L2_PIX_FMT_JPEG ||
  2497. cap->fmt.pixelformat == V4L2_PIX_FMT_INTERLEAVED) {
  2498. fimc_hwset_scaler_bypass(ctrl);
  2499. }
  2500. ctrl->cap->cnt = 0;
  2501. fimc_start_capture(ctrl);
  2502. ctrl->status = FIMC_STREAMON;
  2503. if (ctrl->is.sd && fimc_cam_use)
  2504. ret = v4l2_subdev_call(ctrl->is.sd, video, s_stream, 1);
  2505. printk(KERN_INFO "%s-- fimc%d\n", __func__, ctrl->id);
  2506. /* if available buffer did not remained */
  2507. return 0;
  2508. }
  2509. int fimc_streamoff_capture(void *fh)
  2510. {
  2511. struct fimc_control *ctrl = fh;
  2512. struct fimc_capinfo *cap = ctrl->cap;
  2513. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  2514. int ret = 0;
  2515. void __iomem *qos_regs;
  2516. printk(KERN_INFO "%s++ fimc%d\n", __func__, ctrl->id);
  2517. if (ctrl->status == FIMC_STREAMOFF) {
  2518. fimc_err("%s: fimc%d already stopped.\n", __func__, ctrl->id);
  2519. return -ENODEV;
  2520. }
  2521. if (fimc_check_capture_source(ctrl)) {
  2522. fimc_err("%s: No capture device.\n", __func__);
  2523. return -ENODEV;
  2524. }
  2525. ctrl->status = FIMC_READY_OFF;
  2526. fimc_stop_capture(ctrl);
  2527. #ifdef CONFIG_VIDEO_IMPROVE_STREAMOFF
  2528. if ((get_fimc_dev()->active_camera == 0) &&
  2529. fimc_cam_use && ctrl->cam->sd)
  2530. v4l2_subdev_call(ctrl->cam->sd, video, s_stream, 0);
  2531. #endif
  2532. /* Set FIMD to write back */
  2533. if ((ctrl->cam->id == CAMERA_WB) || (ctrl->cam->id == CAMERA_WB_B)) {
  2534. ret = s3cfb_direct_ioctl(0, S3CFB_SET_WRITEBACK, 0);
  2535. if (ret) {
  2536. fimc_err("failed set writeback\n");
  2537. return ret;
  2538. }
  2539. }
  2540. /* wait for stop hardware */
  2541. fimc_wait_disable_capture(ctrl);
  2542. fimc_hwset_disable_irq(ctrl);
  2543. if (pdata->hw_ver < 0x51)
  2544. INIT_LIST_HEAD(&cap->inq);
  2545. ctrl->status = FIMC_STREAMOFF;
  2546. if (fimc_cam_use) {
  2547. if (ctrl->is.sd)
  2548. v4l2_subdev_call(ctrl->is.sd, video, s_stream, 0);
  2549. if (ctrl->flite_sd)
  2550. v4l2_subdev_call(ctrl->flite_sd, video, s_stream, 0);
  2551. if (ctrl->cam->type == CAM_TYPE_MIPI) {
  2552. if (ctrl->cam->id == CAMERA_CSI_C)
  2553. s3c_csis_stop(CSI_CH_0);
  2554. else
  2555. s3c_csis_stop(CSI_CH_1);
  2556. }
  2557. fimc_err("fimc_hwset_reset in\n");
  2558. fimc_hwset_reset(ctrl);
  2559. fimc_err("fimc_hwset_reset out\n");
  2560. #ifdef CONFIG_VIDEO_IMPROVE_STREAMOFF
  2561. if (ctrl->cam->sd && (get_fimc_dev()->active_camera != 0))
  2562. #else
  2563. if (ctrl->cam->sd)
  2564. #endif
  2565. v4l2_subdev_call(ctrl->cam->sd, video, s_stream, 0);
  2566. } else {
  2567. fimc_hwset_reset(ctrl);
  2568. }
  2569. if (!ctrl->is.sd && cap->movie_mode &&
  2570. !(ctrl->cam->width == 880 && ctrl->cam->height == 720)) {
  2571. printk(KERN_INFO "\n\n\n%s pm_qos_req is removed..\n", __func__ );
  2572. pm_qos_remove_request(&bus_qos_pm_qos_req);
  2573. /*dev_unlock(ctrl->bus_dev, ctrl->dev);*/
  2574. /* ioremap for register block */
  2575. qos_regs = ioremap(0x11a00400, 0x10);
  2576. if (!qos_regs) {
  2577. fimc_err("%s: failed to remap io region\n", __func__);
  2578. return -1;
  2579. }
  2580. writel(0x0, qos_regs + 0x0);
  2581. writel(0x0, qos_regs + 0x4);
  2582. fimc_err("0x11a00400 = 0x%x , 0x11a00404 = 0x%x \n", readl(qos_regs + 0), readl(qos_regs + 4));
  2583. iounmap(qos_regs);
  2584. }
  2585. /* disable camera power */
  2586. /* cam power off should call in the subdev release function */
  2587. if (fimc_cam_use) {
  2588. if (ctrl->cam->reset_camera) {
  2589. if (ctrl->cam->cam_power)
  2590. ctrl->cam->cam_power(0);
  2591. if (ctrl->power_status != FIMC_POWER_SUSPEND)
  2592. ctrl->cam->initialized = 0;
  2593. }
  2594. }
  2595. printk(KERN_INFO "%s-- fimc%d\n", __func__, ctrl->id);
  2596. return 0;
  2597. }
  2598. int fimc_is_set_zoom(struct fimc_control *ctrl, struct v4l2_control *c)
  2599. {
  2600. struct v4l2_control is_ctrl;
  2601. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  2602. struct s3c_platform_camera *cam = NULL;
  2603. int ret = 0;
  2604. is_ctrl.id = 0;
  2605. is_ctrl.value = 0;
  2606. if (ctrl->cam)
  2607. cam = ctrl->cam;
  2608. else
  2609. return -ENODEV;
  2610. /* 0. Check zoom width and height */
  2611. if (!c->value) {
  2612. ctrl->is.zoom_in_width = ctrl->is.fmt.width;
  2613. ctrl->is.zoom_in_height = ctrl->is.fmt.height;
  2614. } else {
  2615. ctrl->is.zoom_in_width = ctrl->is.fmt.width - (16 * c->value);
  2616. ctrl->is.zoom_in_height =
  2617. (ctrl->is.zoom_in_width * ctrl->is.fmt.height)
  2618. / ctrl->is.fmt.width;
  2619. /* bayer crop contraint */
  2620. switch (ctrl->is.zoom_in_height%4) {
  2621. case 1:
  2622. ctrl->is.zoom_in_height--;
  2623. break;
  2624. case 2:
  2625. ctrl->is.zoom_in_height += 2;
  2626. break;
  2627. case 3:
  2628. ctrl->is.zoom_in_height++;
  2629. break;
  2630. }
  2631. if ((ctrl->is.zoom_in_width < (ctrl->is.fmt.width/4))
  2632. || (ctrl->is.zoom_in_height < (ctrl->is.fmt.height/4))) {
  2633. ctrl->is.zoom_in_width = ctrl->is.fmt.width/4;
  2634. ctrl->is.zoom_in_height = ctrl->is.fmt.height/4;
  2635. }
  2636. }
  2637. /* 1. fimc stop */
  2638. fimc_stop_zoom_capture(ctrl);
  2639. /* 2. Set zoom and calculate new width and height */
  2640. if (ctrl->is.sd && fimc_cam_use) {
  2641. ret = v4l2_subdev_call(ctrl->is.sd, core, s_ctrl, c);
  2642. /* 2. Set zoom */
  2643. is_ctrl.id = V4L2_CID_IS_ZOOM_STATE;
  2644. is_ctrl.value = 0;
  2645. while (!is_ctrl.value) {
  2646. v4l2_subdev_call(ctrl->is.sd, core, g_ctrl, &is_ctrl);
  2647. fimc_dbg("V4L2_CID_IS_ZOOM_STATE - %d", is_ctrl.value);
  2648. }
  2649. }
  2650. /* 2. Change soruce size of FIMC */
  2651. fimc_hwset_camera_change_source(ctrl);
  2652. fimc_capture_change_scaler_info(ctrl);
  2653. fimc_hwset_prescaler(ctrl, &ctrl->sc);
  2654. fimc_hwset_scaler(ctrl, &ctrl->sc);
  2655. /* 4. Start FIMC */
  2656. fimc_start_zoom_capture(ctrl);
  2657. /* 5. FIMC-IS stream on */
  2658. if (ctrl->is.sd && fimc_cam_use)
  2659. ret = v4l2_subdev_call(ctrl->is.sd, video, s_stream, 1);
  2660. return 0;
  2661. }
  2662. static void fimc_buf2bs(struct fimc_buf_set *bs, struct fimc_buf *buf)
  2663. {
  2664. bs->base[FIMC_ADDR_Y] = buf->base[FIMC_ADDR_Y];
  2665. bs->length[FIMC_ADDR_Y] = buf->length[FIMC_ADDR_Y];
  2666. bs->base[FIMC_ADDR_CB] = buf->base[FIMC_ADDR_CB];
  2667. bs->length[FIMC_ADDR_CB] = buf->length[FIMC_ADDR_CB];
  2668. bs->base[FIMC_ADDR_CR] = buf->base[FIMC_ADDR_CR];
  2669. bs->length[FIMC_ADDR_CR] = buf->length[FIMC_ADDR_CR];
  2670. }
  2671. int fimc_qbuf_capture(void *fh, struct v4l2_buffer *b)
  2672. {
  2673. struct fimc_control *ctrl = fh;
  2674. struct fimc_buf *buf;
  2675. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  2676. struct fimc_capinfo *cap = ctrl->cap;
  2677. int idx = b->index;
  2678. int framecnt_seq;
  2679. int available_bufnum;
  2680. size_t length = 0;
  2681. int i;
  2682. unsigned long spin_flags;
  2683. if (!cap || !ctrl->cam) {
  2684. fimc_err("%s: No capture device.\n", __func__);
  2685. return -ENODEV;
  2686. }
  2687. if (pdata->hw_ver >= 0x51) {
  2688. if (cap->bufs[idx].state != VIDEOBUF_IDLE) {
  2689. fimc_err("%s: invalid state idx : %d\n", __func__, idx);
  2690. return -EINVAL;
  2691. } else {
  2692. if (b->memory == V4L2_MEMORY_USERPTR) {
  2693. buf = (struct fimc_buf *)b->m.userptr;
  2694. fimc_buf2bs(&cap->bufs[idx], buf);
  2695. fimc_hwset_output_address(ctrl, &cap->bufs[idx], idx);
  2696. #ifdef CONFIG_SLP_DMABUF
  2697. } else if (b->memory == V4L2_MEMORY_DMABUF) {
  2698. struct vb2_buffer *vb;
  2699. struct sg_table *sg;
  2700. struct dma_buf_attachment *dba;
  2701. struct fimc_buf fimc_buf;
  2702. int ret;
  2703. unsigned int size;
  2704. vb = ctrl->cap_bufs[b->index];
  2705. ret = _qbuf_dmabuf(ctrl, vb, b);
  2706. if (ret < 0) {
  2707. fimc_err("%s: _qbuf_dmabuf error.\n",
  2708. __func__);
  2709. return -ENODEV;
  2710. }
  2711. for (i = 0; i < vb->num_planes; i++) {
  2712. dba = vb->planes[i].mem_priv;
  2713. sg = dba->priv;
  2714. if (sg) {
  2715. fimc_buf.base[i] =
  2716. sg_dma_address(sg->sgl);
  2717. fimc_buf.length[i] =
  2718. sg_dma_len(sg->sgl);
  2719. } else {
  2720. fimc_err("%s: Wrong sg value.\n",
  2721. __func__);
  2722. return -ENODEV;
  2723. }
  2724. }
  2725. fimc_buf2bs(&cap->bufs[idx], &fimc_buf);
  2726. fimc_hwset_output_address(ctrl, &cap->bufs[idx],
  2727. idx);
  2728. if (cap->pktdata_enable) {
  2729. size =
  2730. fimc_buf.length[vb->num_planes-1];
  2731. cap->bufs[b->index].vaddr_pktdata =
  2732. phys_to_virt(
  2733. cap->bufs[b->index].base
  2734. [vb->num_planes-1] + size);
  2735. cap->pktdata_plane = vb->num_planes;
  2736. /* Size of packet data is fixed */
  2737. cap->pktdata_size = 0x1000;
  2738. }
  2739. #endif
  2740. }
  2741. spin_lock_irqsave(&ctrl->inq_lock, spin_flags);
  2742. fimc_hwset_output_buf_sequence(ctrl, idx, FIMC_FRAMECNT_SEQ_ENABLE);
  2743. cap->bufs[idx].state = VIDEOBUF_QUEUED;
  2744. if (ctrl->status == FIMC_BUFFER_STOP) {
  2745. framecnt_seq = fimc_hwget_output_buf_sequence(ctrl);
  2746. available_bufnum =
  2747. fimc_hwget_number_of_bits(framecnt_seq);
  2748. if (available_bufnum >= 2) {
  2749. printk(KERN_INFO "fimc_qbuf_capture start again\n");
  2750. cap->cnt = 0;
  2751. fimc_start_capture(ctrl);
  2752. ctrl->status = FIMC_STREAMON;
  2753. ctrl->restart = true;
  2754. }
  2755. }
  2756. spin_unlock_irqrestore(&ctrl->inq_lock, spin_flags);
  2757. }
  2758. } else {
  2759. fimc_add_inqueue(ctrl, b->index);
  2760. }
  2761. if (!cap->cacheable)
  2762. return 0;
  2763. for (i = 0; i < 3; i++) {
  2764. if (cap->bufs[b->index].base[i])
  2765. length += cap->bufs[b->index].length[i];
  2766. else
  2767. break;
  2768. }
  2769. if (length > (unsigned long) L2_FLUSH_ALL) {
  2770. flush_cache_all(); /* L1 */
  2771. smp_call_function((smp_call_func_t)__cpuc_flush_kern_all, NULL, 1);
  2772. outer_flush_all(); /* L2 */
  2773. } else if (length > (unsigned long) L1_FLUSH_ALL) {
  2774. flush_cache_all(); /* L1 */
  2775. smp_call_function((smp_call_func_t)__cpuc_flush_kern_all, NULL, 1);
  2776. for (i = 0; i < 3; i++) {
  2777. phys_addr_t start = cap->bufs[b->index].base[i];
  2778. phys_addr_t end = cap->bufs[b->index].base[i] +
  2779. cap->bufs[b->index].length[i] - 1;
  2780. if (!start)
  2781. break;
  2782. outer_flush_range(start, end); /* L2 */
  2783. }
  2784. } else {
  2785. for (i = 0; i < 3; i++) {
  2786. phys_addr_t start = cap->bufs[b->index].base[i];
  2787. phys_addr_t end = cap->bufs[b->index].base[i] +
  2788. cap->bufs[b->index].length[i] - 1;
  2789. if (!start)
  2790. break;
  2791. dmac_flush_range(phys_to_virt(start), phys_to_virt(end));
  2792. outer_flush_range(start, end); /* L2 */
  2793. }
  2794. }
  2795. return 0;
  2796. }
  2797. static void fimc_bs2buf(struct fimc_buf *buf, struct fimc_buf_set *bs)
  2798. {
  2799. buf->base[FIMC_ADDR_Y] = bs->base[FIMC_ADDR_Y];
  2800. buf->length[FIMC_ADDR_Y] = bs->length[FIMC_ADDR_Y];
  2801. buf->base[FIMC_ADDR_CB] = bs->base[FIMC_ADDR_CB];
  2802. buf->length[FIMC_ADDR_CB] = bs->length[FIMC_ADDR_CB];
  2803. buf->base[FIMC_ADDR_CR] = bs->base[FIMC_ADDR_CR];
  2804. buf->length[FIMC_ADDR_CR] = bs->length[FIMC_ADDR_CR];
  2805. }
  2806. int fimc_dqbuf_capture(void *fh, struct v4l2_buffer *b)
  2807. {
  2808. unsigned long spin_flags;
  2809. struct fimc_control *ctrl = fh;
  2810. struct fimc_capinfo *cap = ctrl->cap;
  2811. struct fimc_buf_set *bs;
  2812. struct fimc_buf *buf = (struct fimc_buf *)b->m.userptr;
  2813. size_t length = 0;
  2814. int i, pp, ret = 0;
  2815. phys_addr_t start, end;
  2816. struct s3c_platform_fimc *pdata = to_fimc_plat(ctrl->dev);
  2817. if (!cap || !ctrl->cam) {
  2818. fimc_err("%s: No capture device.\n", __func__);
  2819. return -ENODEV;
  2820. }
  2821. if (pdata->hw_ver >= 0x51) {
  2822. #ifdef CONFIG_MACH_GC1
  2823. if (cap->outgoing_q.next == NULL) {
  2824. fimc_err("%s: No cap->outgoing_q.\n", __func__);
  2825. return -ENODEV;
  2826. }
  2827. #endif
  2828. spin_lock_irqsave(&ctrl->outq_lock, spin_flags);
  2829. if (list_empty(&cap->outgoing_q)) {
  2830. fimc_info2("%s: outgoing_q is empty\n", __func__);
  2831. spin_unlock_irqrestore(&ctrl->outq_lock, spin_flags);
  2832. return -EAGAIN;
  2833. } else {
  2834. bs = list_first_entry(&cap->outgoing_q, struct fimc_buf_set,
  2835. list);
  2836. fimc_info2("%s[%d]: bs->id : %d\n", __func__, ctrl->id, bs->id);
  2837. b->index = bs->id;
  2838. bs->state = VIDEOBUF_IDLE;
  2839. if (b->memory == V4L2_MEMORY_USERPTR) {
  2840. fimc_bs2buf(buf, bs);
  2841. #ifdef CONFIG_SLP_DMABUF
  2842. } else if (b->memory == V4L2_MEMORY_DMABUF) {
  2843. struct vb2_buffer *vb;
  2844. vb = ctrl->cap_bufs[b->index];
  2845. ret = _dqbuf_dmabuf(ctrl, vb, b);
  2846. if (ret < 0) {
  2847. fimc_err("%s: _qbuf_dmabuf error.\n",
  2848. __func__);
  2849. spin_unlock_irqrestore(&ctrl->outq_lock,
  2850. spin_flags);
  2851. return -ENODEV;
  2852. }
  2853. #endif
  2854. }
  2855. list_del(&bs->list);
  2856. }
  2857. spin_unlock_irqrestore(&ctrl->outq_lock, spin_flags);
  2858. } else {
  2859. pp = ((fimc_hwget_frame_count(ctrl) + 2) % 4);
  2860. if (cap->fmt.field == V4L2_FIELD_INTERLACED_TB)
  2861. pp &= ~0x1;
  2862. b->index = cap->outq[pp];
  2863. fimc_info2("%s: buffer(%d) outq[%d]\n", __func__, b->index, pp);
  2864. ret = fimc_add_outqueue(ctrl, pp);
  2865. if (ret) {
  2866. b->index = -1;
  2867. fimc_err("%s: no inqueue buffer\n", __func__);
  2868. }
  2869. }
  2870. if (cap->pktdata_enable) {
  2871. flush_cache_all(); /* L1 */
  2872. smp_call_function((smp_call_func_t)__cpuc_flush_kern_all, NULL, 1);
  2873. start = cap->bufs[b->index].base[cap->pktdata_plane];
  2874. end = cap->bufs[b->index].base[cap->pktdata_plane] +
  2875. cap->bufs[b->index].length[cap->pktdata_plane] - 1;
  2876. fimc_info2("fimc_dqbuf interleaved mode cache flush... start 0x%x, size 0x%x\n",
  2877. start, cap->bufs[b->index].length[cap->pktdata_plane] );
  2878. outer_flush_range(start, end); /* L2 */
  2879. }
  2880. if (!cap->cacheable)
  2881. return ret;
  2882. for (i = 0; i < 3; i++) {
  2883. if (cap->bufs[b->index].base[i])
  2884. length += cap->bufs[b->index].length[i];
  2885. else
  2886. break;
  2887. }
  2888. if (length > (unsigned long) L2_FLUSH_ALL) {
  2889. flush_cache_all(); /* L1 */
  2890. smp_call_function((smp_call_func_t)__cpuc_flush_kern_all, NULL, 1);
  2891. outer_flush_all(); /* L2 */
  2892. } else if (length > (unsigned long) L1_FLUSH_ALL) {
  2893. flush_cache_all(); /* L1 */
  2894. smp_call_function((smp_call_func_t)__cpuc_flush_kern_all, NULL, 1);
  2895. for (i = 0; i < 3; i++) {
  2896. phys_addr_t start = cap->bufs[b->index].base[i];
  2897. phys_addr_t end = cap->bufs[b->index].base[i] +
  2898. cap->bufs[b->index].length[i] - 1;
  2899. if (!start)
  2900. break;
  2901. outer_flush_range(start, end); /* L2 */
  2902. }
  2903. } else {
  2904. for (i = 0; i < 3; i++) {
  2905. phys_addr_t start = cap->bufs[b->index].base[i];
  2906. phys_addr_t end = cap->bufs[b->index].base[i] +
  2907. cap->bufs[b->index].length[i] - 1;
  2908. if (!start)
  2909. break;
  2910. dmac_flush_range(phys_to_virt(start), phys_to_virt(end));
  2911. outer_flush_range(start, end); /* L2 */
  2912. }
  2913. }
  2914. return ret;
  2915. }
  2916. int fimc_enum_framesizes(struct file *filp, void *fh, struct v4l2_frmsizeenum *fsize)
  2917. {
  2918. struct fimc_control *ctrl = ((struct fimc_prv_data *)fh)->ctrl;
  2919. int i;
  2920. u32 index = 0;
  2921. #ifdef CONFIG_SLP
  2922. if (ctrl->cam && ctrl->cam->sd)
  2923. return v4l2_subdev_call(ctrl->cam->sd, video,
  2924. enum_framesizes, fsize);
  2925. #endif
  2926. for (i = 0; i < ARRAY_SIZE(capture_fmts); i++) {
  2927. if (fsize->pixel_format != capture_fmts[i].pixelformat)
  2928. continue;
  2929. if (fsize->index == index) {
  2930. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  2931. /* this is camera sensor's width, height.
  2932. * originally this should be filled each file format
  2933. */
  2934. fsize->discrete.width = ctrl->cam->width;
  2935. fsize->discrete.height = ctrl->cam->height;
  2936. return 0;
  2937. }
  2938. index++;
  2939. }
  2940. return -EINVAL;
  2941. }
  2942. int fimc_enum_frameintervals(struct file *filp, void *fh,
  2943. struct v4l2_frmivalenum *fival)
  2944. {
  2945. if (fival->index > 0)
  2946. return -EINVAL;
  2947. /* temporary only support 30fps */
  2948. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  2949. fival->discrete.numerator = 1000;
  2950. fival->discrete.denominator = 30000;
  2951. return 0;
  2952. }
  2953. /*
  2954. * only used at mipi power func.
  2955. */
  2956. struct device *fimc_get_active_device(void)
  2957. {
  2958. struct fimc_global *fimc = get_fimc_dev();
  2959. struct fimc_control *ctrl;
  2960. if (!fimc || (fimc->active_camera < 0))
  2961. return NULL;
  2962. ctrl = get_fimc_ctrl(fimc->active_camera);
  2963. return ctrl->dev;
  2964. }