fimc-isp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. *
  6. * Authors: Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. * Younghwan Joo <yhwan.joo@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/printk.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <media/v4l2-device.h>
  25. #include "media-dev.h"
  26. #include "fimc-isp-video.h"
  27. #include "fimc-is-command.h"
  28. #include "fimc-is-param.h"
  29. #include "fimc-is-regs.h"
  30. #include "fimc-is.h"
  31. int fimc_isp_debug;
  32. module_param_named(debug_isp, fimc_isp_debug, int, S_IRUGO | S_IWUSR);
  33. static const struct fimc_fmt fimc_isp_formats[FIMC_ISP_NUM_FORMATS] = {
  34. {
  35. .name = "RAW8 (GRBG)",
  36. .fourcc = V4L2_PIX_FMT_SGRBG8,
  37. .depth = { 8 },
  38. .color = FIMC_FMT_RAW8,
  39. .memplanes = 1,
  40. .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
  41. }, {
  42. .name = "RAW10 (GRBG)",
  43. .fourcc = V4L2_PIX_FMT_SGRBG10,
  44. .depth = { 10 },
  45. .color = FIMC_FMT_RAW10,
  46. .memplanes = 1,
  47. .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
  48. }, {
  49. .name = "RAW12 (GRBG)",
  50. .fourcc = V4L2_PIX_FMT_SGRBG12,
  51. .depth = { 12 },
  52. .color = FIMC_FMT_RAW12,
  53. .memplanes = 1,
  54. .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
  55. },
  56. };
  57. /**
  58. * fimc_isp_find_format - lookup color format by fourcc or media bus code
  59. * @pixelformat: fourcc to match, ignored if null
  60. * @mbus_code: media bus code to match, ignored if null
  61. * @index: index to the fimc_isp_formats array, ignored if negative
  62. */
  63. const struct fimc_fmt *fimc_isp_find_format(const u32 *pixelformat,
  64. const u32 *mbus_code, int index)
  65. {
  66. const struct fimc_fmt *fmt, *def_fmt = NULL;
  67. unsigned int i;
  68. int id = 0;
  69. if (index >= (int)ARRAY_SIZE(fimc_isp_formats))
  70. return NULL;
  71. for (i = 0; i < ARRAY_SIZE(fimc_isp_formats); ++i) {
  72. fmt = &fimc_isp_formats[i];
  73. if (pixelformat && fmt->fourcc == *pixelformat)
  74. return fmt;
  75. if (mbus_code && fmt->mbus_code == *mbus_code)
  76. return fmt;
  77. if (index == id)
  78. def_fmt = fmt;
  79. id++;
  80. }
  81. return def_fmt;
  82. }
  83. void fimc_isp_irq_handler(struct fimc_is *is)
  84. {
  85. is->i2h_cmd.args[0] = mcuctl_read(is, MCUCTL_REG_ISSR(20));
  86. is->i2h_cmd.args[1] = mcuctl_read(is, MCUCTL_REG_ISSR(21));
  87. fimc_is_fw_clear_irq1(is, FIMC_IS_INT_FRAME_DONE_ISP);
  88. fimc_isp_video_irq_handler(is);
  89. wake_up(&is->irq_queue);
  90. }
  91. /* Capture subdev media entity operations */
  92. static int fimc_is_link_setup(struct media_entity *entity,
  93. const struct media_pad *local,
  94. const struct media_pad *remote, u32 flags)
  95. {
  96. return 0;
  97. }
  98. static const struct media_entity_operations fimc_is_subdev_media_ops = {
  99. .link_setup = fimc_is_link_setup,
  100. };
  101. static int fimc_is_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  102. struct v4l2_subdev_pad_config *cfg,
  103. struct v4l2_subdev_mbus_code_enum *code)
  104. {
  105. const struct fimc_fmt *fmt;
  106. fmt = fimc_isp_find_format(NULL, NULL, code->index);
  107. if (!fmt)
  108. return -EINVAL;
  109. code->code = fmt->mbus_code;
  110. return 0;
  111. }
  112. static int fimc_isp_subdev_get_fmt(struct v4l2_subdev *sd,
  113. struct v4l2_subdev_pad_config *cfg,
  114. struct v4l2_subdev_format *fmt)
  115. {
  116. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  117. struct v4l2_mbus_framefmt *mf = &fmt->format;
  118. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  119. *mf = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  120. return 0;
  121. }
  122. mf->colorspace = V4L2_COLORSPACE_SRGB;
  123. mutex_lock(&isp->subdev_lock);
  124. if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
  125. /* ISP OTF input image format */
  126. *mf = isp->sink_fmt;
  127. } else {
  128. /* ISP OTF output image format */
  129. *mf = isp->src_fmt;
  130. if (fmt->pad == FIMC_ISP_SD_PAD_SRC_FIFO) {
  131. mf->colorspace = V4L2_COLORSPACE_JPEG;
  132. mf->code = MEDIA_BUS_FMT_YUV10_1X30;
  133. }
  134. }
  135. mutex_unlock(&isp->subdev_lock);
  136. isp_dbg(1, sd, "%s: pad%d: fmt: 0x%x, %dx%d\n", __func__,
  137. fmt->pad, mf->code, mf->width, mf->height);
  138. return 0;
  139. }
  140. static void __isp_subdev_try_format(struct fimc_isp *isp,
  141. struct v4l2_subdev_pad_config *cfg,
  142. struct v4l2_subdev_format *fmt)
  143. {
  144. struct v4l2_mbus_framefmt *mf = &fmt->format;
  145. struct v4l2_mbus_framefmt *format;
  146. mf->colorspace = V4L2_COLORSPACE_SRGB;
  147. if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
  148. v4l_bound_align_image(&mf->width, FIMC_ISP_SINK_WIDTH_MIN,
  149. FIMC_ISP_SINK_WIDTH_MAX, 0,
  150. &mf->height, FIMC_ISP_SINK_HEIGHT_MIN,
  151. FIMC_ISP_SINK_HEIGHT_MAX, 0, 0);
  152. mf->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  153. } else {
  154. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  155. format = v4l2_subdev_get_try_format(&isp->subdev, cfg,
  156. FIMC_ISP_SD_PAD_SINK);
  157. else
  158. format = &isp->sink_fmt;
  159. /* Allow changing format only on sink pad */
  160. mf->width = format->width - FIMC_ISP_CAC_MARGIN_WIDTH;
  161. mf->height = format->height - FIMC_ISP_CAC_MARGIN_HEIGHT;
  162. if (fmt->pad == FIMC_ISP_SD_PAD_SRC_FIFO) {
  163. mf->code = MEDIA_BUS_FMT_YUV10_1X30;
  164. mf->colorspace = V4L2_COLORSPACE_JPEG;
  165. } else {
  166. mf->code = format->code;
  167. }
  168. }
  169. }
  170. static int fimc_isp_subdev_set_fmt(struct v4l2_subdev *sd,
  171. struct v4l2_subdev_pad_config *cfg,
  172. struct v4l2_subdev_format *fmt)
  173. {
  174. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  175. struct fimc_is *is = fimc_isp_to_is(isp);
  176. struct v4l2_mbus_framefmt *mf = &fmt->format;
  177. int ret = 0;
  178. isp_dbg(1, sd, "%s: pad%d: code: 0x%x, %dx%d\n",
  179. __func__, fmt->pad, mf->code, mf->width, mf->height);
  180. mutex_lock(&isp->subdev_lock);
  181. __isp_subdev_try_format(isp, cfg, fmt);
  182. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  183. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  184. *mf = fmt->format;
  185. /* Propagate format to the source pads */
  186. if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
  187. struct v4l2_subdev_format format = *fmt;
  188. unsigned int pad;
  189. for (pad = FIMC_ISP_SD_PAD_SRC_FIFO;
  190. pad < FIMC_ISP_SD_PADS_NUM; pad++) {
  191. format.pad = pad;
  192. __isp_subdev_try_format(isp, cfg, &format);
  193. mf = v4l2_subdev_get_try_format(sd, cfg, pad);
  194. *mf = format.format;
  195. }
  196. }
  197. } else {
  198. if (sd->entity.stream_count == 0) {
  199. if (fmt->pad == FIMC_ISP_SD_PAD_SINK) {
  200. struct v4l2_subdev_format format = *fmt;
  201. isp->sink_fmt = *mf;
  202. format.pad = FIMC_ISP_SD_PAD_SRC_DMA;
  203. __isp_subdev_try_format(isp, cfg, &format);
  204. isp->src_fmt = format.format;
  205. __is_set_frame_size(is, &isp->src_fmt);
  206. } else {
  207. isp->src_fmt = *mf;
  208. }
  209. } else {
  210. ret = -EBUSY;
  211. }
  212. }
  213. mutex_unlock(&isp->subdev_lock);
  214. return ret;
  215. }
  216. static int fimc_isp_subdev_s_stream(struct v4l2_subdev *sd, int on)
  217. {
  218. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  219. struct fimc_is *is = fimc_isp_to_is(isp);
  220. int ret;
  221. isp_dbg(1, sd, "%s: on: %d\n", __func__, on);
  222. if (!test_bit(IS_ST_INIT_DONE, &is->state))
  223. return -EBUSY;
  224. fimc_is_mem_barrier();
  225. if (on) {
  226. if (__get_pending_param_count(is)) {
  227. ret = fimc_is_itf_s_param(is, true);
  228. if (ret < 0)
  229. return ret;
  230. }
  231. isp_dbg(1, sd, "changing mode to %d\n", is->config_index);
  232. ret = fimc_is_itf_mode_change(is);
  233. if (ret)
  234. return -EINVAL;
  235. clear_bit(IS_ST_STREAM_ON, &is->state);
  236. fimc_is_hw_stream_on(is);
  237. ret = fimc_is_wait_event(is, IS_ST_STREAM_ON, 1,
  238. FIMC_IS_CONFIG_TIMEOUT);
  239. if (ret < 0) {
  240. v4l2_err(sd, "stream on timeout\n");
  241. return ret;
  242. }
  243. } else {
  244. clear_bit(IS_ST_STREAM_OFF, &is->state);
  245. fimc_is_hw_stream_off(is);
  246. ret = fimc_is_wait_event(is, IS_ST_STREAM_OFF, 1,
  247. FIMC_IS_CONFIG_TIMEOUT);
  248. if (ret < 0) {
  249. v4l2_err(sd, "stream off timeout\n");
  250. return ret;
  251. }
  252. is->setfile.sub_index = 0;
  253. }
  254. return 0;
  255. }
  256. static int fimc_isp_subdev_s_power(struct v4l2_subdev *sd, int on)
  257. {
  258. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  259. struct fimc_is *is = fimc_isp_to_is(isp);
  260. int ret = 0;
  261. pr_debug("on: %d\n", on);
  262. if (on) {
  263. ret = pm_runtime_get_sync(&is->pdev->dev);
  264. if (ret < 0) {
  265. pm_runtime_put(&is->pdev->dev);
  266. return ret;
  267. }
  268. set_bit(IS_ST_PWR_ON, &is->state);
  269. ret = fimc_is_start_firmware(is);
  270. if (ret < 0) {
  271. v4l2_err(sd, "firmware booting failed\n");
  272. pm_runtime_put(&is->pdev->dev);
  273. return ret;
  274. }
  275. set_bit(IS_ST_PWR_SUBIP_ON, &is->state);
  276. ret = fimc_is_hw_initialize(is);
  277. } else {
  278. /* Close sensor */
  279. if (!test_bit(IS_ST_PWR_ON, &is->state)) {
  280. fimc_is_hw_close_sensor(is, 0);
  281. ret = fimc_is_wait_event(is, IS_ST_OPEN_SENSOR, 0,
  282. FIMC_IS_CONFIG_TIMEOUT);
  283. if (ret < 0) {
  284. v4l2_err(sd, "sensor close timeout\n");
  285. return ret;
  286. }
  287. }
  288. /* SUB IP power off */
  289. if (test_bit(IS_ST_PWR_SUBIP_ON, &is->state)) {
  290. fimc_is_hw_subip_power_off(is);
  291. ret = fimc_is_wait_event(is, IS_ST_PWR_SUBIP_ON, 0,
  292. FIMC_IS_CONFIG_TIMEOUT);
  293. if (ret < 0) {
  294. v4l2_err(sd, "sub-IP power off timeout\n");
  295. return ret;
  296. }
  297. }
  298. fimc_is_cpu_set_power(is, 0);
  299. pm_runtime_put_sync(&is->pdev->dev);
  300. clear_bit(IS_ST_PWR_ON, &is->state);
  301. clear_bit(IS_ST_INIT_DONE, &is->state);
  302. is->state = 0;
  303. is->config[is->config_index].p_region_index[0] = 0;
  304. is->config[is->config_index].p_region_index[1] = 0;
  305. set_bit(IS_ST_IDLE, &is->state);
  306. wmb();
  307. }
  308. return ret;
  309. }
  310. static int fimc_isp_subdev_open(struct v4l2_subdev *sd,
  311. struct v4l2_subdev_fh *fh)
  312. {
  313. struct v4l2_mbus_framefmt *format;
  314. struct v4l2_mbus_framefmt fmt = {
  315. .colorspace = V4L2_COLORSPACE_SRGB,
  316. .code = fimc_isp_formats[0].mbus_code,
  317. .width = DEFAULT_PREVIEW_STILL_WIDTH + FIMC_ISP_CAC_MARGIN_WIDTH,
  318. .height = DEFAULT_PREVIEW_STILL_HEIGHT + FIMC_ISP_CAC_MARGIN_HEIGHT,
  319. .field = V4L2_FIELD_NONE,
  320. };
  321. format = v4l2_subdev_get_try_format(sd, fh->pad, FIMC_ISP_SD_PAD_SINK);
  322. *format = fmt;
  323. format = v4l2_subdev_get_try_format(sd, fh->pad, FIMC_ISP_SD_PAD_SRC_FIFO);
  324. fmt.width = DEFAULT_PREVIEW_STILL_WIDTH;
  325. fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT;
  326. *format = fmt;
  327. format = v4l2_subdev_get_try_format(sd, fh->pad, FIMC_ISP_SD_PAD_SRC_DMA);
  328. *format = fmt;
  329. return 0;
  330. }
  331. static int fimc_isp_subdev_registered(struct v4l2_subdev *sd)
  332. {
  333. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  334. int ret;
  335. /* Use pipeline object allocated by the media device. */
  336. isp->video_capture.ve.pipe = v4l2_get_subdev_hostdata(sd);
  337. ret = fimc_isp_video_device_register(isp, sd->v4l2_dev,
  338. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  339. if (ret < 0)
  340. isp->video_capture.ve.pipe = NULL;
  341. return ret;
  342. }
  343. static void fimc_isp_subdev_unregistered(struct v4l2_subdev *sd)
  344. {
  345. struct fimc_isp *isp = v4l2_get_subdevdata(sd);
  346. fimc_isp_video_device_unregister(isp,
  347. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  348. }
  349. static const struct v4l2_subdev_internal_ops fimc_is_subdev_internal_ops = {
  350. .registered = fimc_isp_subdev_registered,
  351. .unregistered = fimc_isp_subdev_unregistered,
  352. .open = fimc_isp_subdev_open,
  353. };
  354. static const struct v4l2_subdev_pad_ops fimc_is_subdev_pad_ops = {
  355. .enum_mbus_code = fimc_is_subdev_enum_mbus_code,
  356. .get_fmt = fimc_isp_subdev_get_fmt,
  357. .set_fmt = fimc_isp_subdev_set_fmt,
  358. };
  359. static const struct v4l2_subdev_video_ops fimc_is_subdev_video_ops = {
  360. .s_stream = fimc_isp_subdev_s_stream,
  361. };
  362. static const struct v4l2_subdev_core_ops fimc_is_core_ops = {
  363. .s_power = fimc_isp_subdev_s_power,
  364. };
  365. static const struct v4l2_subdev_ops fimc_is_subdev_ops = {
  366. .core = &fimc_is_core_ops,
  367. .video = &fimc_is_subdev_video_ops,
  368. .pad = &fimc_is_subdev_pad_ops,
  369. };
  370. static int __ctrl_set_white_balance(struct fimc_is *is, int value)
  371. {
  372. switch (value) {
  373. case V4L2_WHITE_BALANCE_AUTO:
  374. __is_set_isp_awb(is, ISP_AWB_COMMAND_AUTO, 0);
  375. break;
  376. case V4L2_WHITE_BALANCE_DAYLIGHT:
  377. __is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
  378. ISP_AWB_ILLUMINATION_DAYLIGHT);
  379. break;
  380. case V4L2_WHITE_BALANCE_CLOUDY:
  381. __is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
  382. ISP_AWB_ILLUMINATION_CLOUDY);
  383. break;
  384. case V4L2_WHITE_BALANCE_INCANDESCENT:
  385. __is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
  386. ISP_AWB_ILLUMINATION_TUNGSTEN);
  387. break;
  388. case V4L2_WHITE_BALANCE_FLUORESCENT:
  389. __is_set_isp_awb(is, ISP_AWB_COMMAND_ILLUMINATION,
  390. ISP_AWB_ILLUMINATION_FLUORESCENT);
  391. break;
  392. default:
  393. return -EINVAL;
  394. }
  395. return 0;
  396. }
  397. static int __ctrl_set_aewb_lock(struct fimc_is *is,
  398. struct v4l2_ctrl *ctrl)
  399. {
  400. bool awb_lock = ctrl->val & V4L2_LOCK_WHITE_BALANCE;
  401. bool ae_lock = ctrl->val & V4L2_LOCK_EXPOSURE;
  402. struct isp_param *isp = &is->is_p_region->parameter.isp;
  403. int cmd, ret;
  404. cmd = ae_lock ? ISP_AA_COMMAND_STOP : ISP_AA_COMMAND_START;
  405. isp->aa.cmd = cmd;
  406. isp->aa.target = ISP_AA_TARGET_AE;
  407. fimc_is_set_param_bit(is, PARAM_ISP_AA);
  408. is->af.ae_lock_state = ae_lock;
  409. wmb();
  410. ret = fimc_is_itf_s_param(is, false);
  411. if (ret < 0)
  412. return ret;
  413. cmd = awb_lock ? ISP_AA_COMMAND_STOP : ISP_AA_COMMAND_START;
  414. isp->aa.cmd = cmd;
  415. isp->aa.target = ISP_AA_TARGET_AE;
  416. fimc_is_set_param_bit(is, PARAM_ISP_AA);
  417. is->af.awb_lock_state = awb_lock;
  418. wmb();
  419. return fimc_is_itf_s_param(is, false);
  420. }
  421. /* Supported manual ISO values */
  422. static const s64 iso_qmenu[] = {
  423. 50, 100, 200, 400, 800,
  424. };
  425. static int __ctrl_set_iso(struct fimc_is *is, int value)
  426. {
  427. unsigned int idx, iso;
  428. if (value == V4L2_ISO_SENSITIVITY_AUTO) {
  429. __is_set_isp_iso(is, ISP_ISO_COMMAND_AUTO, 0);
  430. return 0;
  431. }
  432. idx = is->isp.ctrls.iso->val;
  433. if (idx >= ARRAY_SIZE(iso_qmenu))
  434. return -EINVAL;
  435. iso = iso_qmenu[idx];
  436. __is_set_isp_iso(is, ISP_ISO_COMMAND_MANUAL, iso);
  437. return 0;
  438. }
  439. static int __ctrl_set_metering(struct fimc_is *is, unsigned int value)
  440. {
  441. unsigned int val;
  442. switch (value) {
  443. case V4L2_EXPOSURE_METERING_AVERAGE:
  444. val = ISP_METERING_COMMAND_AVERAGE;
  445. break;
  446. case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  447. val = ISP_METERING_COMMAND_CENTER;
  448. break;
  449. case V4L2_EXPOSURE_METERING_SPOT:
  450. val = ISP_METERING_COMMAND_SPOT;
  451. break;
  452. case V4L2_EXPOSURE_METERING_MATRIX:
  453. val = ISP_METERING_COMMAND_MATRIX;
  454. break;
  455. default:
  456. return -EINVAL;
  457. }
  458. __is_set_isp_metering(is, IS_METERING_CONFIG_CMD, val);
  459. return 0;
  460. }
  461. static int __ctrl_set_afc(struct fimc_is *is, int value)
  462. {
  463. switch (value) {
  464. case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  465. __is_set_isp_afc(is, ISP_AFC_COMMAND_DISABLE, 0);
  466. break;
  467. case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  468. __is_set_isp_afc(is, ISP_AFC_COMMAND_MANUAL, 50);
  469. break;
  470. case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  471. __is_set_isp_afc(is, ISP_AFC_COMMAND_MANUAL, 60);
  472. break;
  473. case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  474. __is_set_isp_afc(is, ISP_AFC_COMMAND_AUTO, 0);
  475. break;
  476. default:
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. static int __ctrl_set_image_effect(struct fimc_is *is, int value)
  482. {
  483. static const u8 effects[][2] = {
  484. { V4L2_COLORFX_NONE, ISP_IMAGE_EFFECT_DISABLE },
  485. { V4L2_COLORFX_BW, ISP_IMAGE_EFFECT_MONOCHROME },
  486. { V4L2_COLORFX_SEPIA, ISP_IMAGE_EFFECT_SEPIA },
  487. { V4L2_COLORFX_NEGATIVE, ISP_IMAGE_EFFECT_NEGATIVE_MONO },
  488. { 16 /* TODO */, ISP_IMAGE_EFFECT_NEGATIVE_COLOR },
  489. };
  490. int i;
  491. for (i = 0; i < ARRAY_SIZE(effects); i++) {
  492. if (effects[i][0] != value)
  493. continue;
  494. __is_set_isp_effect(is, effects[i][1]);
  495. return 0;
  496. }
  497. return -EINVAL;
  498. }
  499. static int fimc_is_s_ctrl(struct v4l2_ctrl *ctrl)
  500. {
  501. struct fimc_isp *isp = ctrl_to_fimc_isp(ctrl);
  502. struct fimc_is *is = fimc_isp_to_is(isp);
  503. bool set_param = true;
  504. int ret = 0;
  505. switch (ctrl->id) {
  506. case V4L2_CID_CONTRAST:
  507. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_CONTRAST,
  508. ctrl->val);
  509. break;
  510. case V4L2_CID_SATURATION:
  511. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_SATURATION,
  512. ctrl->val);
  513. break;
  514. case V4L2_CID_SHARPNESS:
  515. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_SHARPNESS,
  516. ctrl->val);
  517. break;
  518. case V4L2_CID_EXPOSURE_ABSOLUTE:
  519. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_EXPOSURE,
  520. ctrl->val);
  521. break;
  522. case V4L2_CID_BRIGHTNESS:
  523. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_BRIGHTNESS,
  524. ctrl->val);
  525. break;
  526. case V4L2_CID_HUE:
  527. __is_set_isp_adjust(is, ISP_ADJUST_COMMAND_MANUAL_HUE,
  528. ctrl->val);
  529. break;
  530. case V4L2_CID_EXPOSURE_METERING:
  531. ret = __ctrl_set_metering(is, ctrl->val);
  532. break;
  533. case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
  534. ret = __ctrl_set_white_balance(is, ctrl->val);
  535. break;
  536. case V4L2_CID_3A_LOCK:
  537. ret = __ctrl_set_aewb_lock(is, ctrl);
  538. set_param = false;
  539. break;
  540. case V4L2_CID_ISO_SENSITIVITY_AUTO:
  541. ret = __ctrl_set_iso(is, ctrl->val);
  542. break;
  543. case V4L2_CID_POWER_LINE_FREQUENCY:
  544. ret = __ctrl_set_afc(is, ctrl->val);
  545. break;
  546. case V4L2_CID_COLORFX:
  547. __ctrl_set_image_effect(is, ctrl->val);
  548. break;
  549. default:
  550. ret = -EINVAL;
  551. break;
  552. }
  553. if (ret < 0) {
  554. v4l2_err(&isp->subdev, "Failed to set control: %s (%d)\n",
  555. ctrl->name, ctrl->val);
  556. return ret;
  557. }
  558. if (set_param && test_bit(IS_ST_STREAM_ON, &is->state))
  559. return fimc_is_itf_s_param(is, true);
  560. return 0;
  561. }
  562. static const struct v4l2_ctrl_ops fimc_isp_ctrl_ops = {
  563. .s_ctrl = fimc_is_s_ctrl,
  564. };
  565. static void __isp_subdev_set_default_format(struct fimc_isp *isp)
  566. {
  567. struct fimc_is *is = fimc_isp_to_is(isp);
  568. isp->sink_fmt.width = DEFAULT_PREVIEW_STILL_WIDTH +
  569. FIMC_ISP_CAC_MARGIN_WIDTH;
  570. isp->sink_fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT +
  571. FIMC_ISP_CAC_MARGIN_HEIGHT;
  572. isp->sink_fmt.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  573. isp->src_fmt.width = DEFAULT_PREVIEW_STILL_WIDTH;
  574. isp->src_fmt.height = DEFAULT_PREVIEW_STILL_HEIGHT;
  575. isp->src_fmt.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  576. __is_set_frame_size(is, &isp->src_fmt);
  577. }
  578. int fimc_isp_subdev_create(struct fimc_isp *isp)
  579. {
  580. const struct v4l2_ctrl_ops *ops = &fimc_isp_ctrl_ops;
  581. struct v4l2_ctrl_handler *handler = &isp->ctrls.handler;
  582. struct v4l2_subdev *sd = &isp->subdev;
  583. struct fimc_isp_ctrls *ctrls = &isp->ctrls;
  584. int ret;
  585. mutex_init(&isp->subdev_lock);
  586. v4l2_subdev_init(sd, &fimc_is_subdev_ops);
  587. sd->owner = THIS_MODULE;
  588. sd->grp_id = GRP_ID_FIMC_IS;
  589. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  590. snprintf(sd->name, sizeof(sd->name), "FIMC-IS-ISP");
  591. sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
  592. isp->subdev_pads[FIMC_ISP_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  593. isp->subdev_pads[FIMC_ISP_SD_PAD_SRC_FIFO].flags = MEDIA_PAD_FL_SOURCE;
  594. isp->subdev_pads[FIMC_ISP_SD_PAD_SRC_DMA].flags = MEDIA_PAD_FL_SOURCE;
  595. ret = media_entity_pads_init(&sd->entity, FIMC_ISP_SD_PADS_NUM,
  596. isp->subdev_pads);
  597. if (ret)
  598. return ret;
  599. v4l2_ctrl_handler_init(handler, 20);
  600. ctrls->saturation = v4l2_ctrl_new_std(handler, ops, V4L2_CID_SATURATION,
  601. -2, 2, 1, 0);
  602. ctrls->brightness = v4l2_ctrl_new_std(handler, ops, V4L2_CID_BRIGHTNESS,
  603. -4, 4, 1, 0);
  604. ctrls->contrast = v4l2_ctrl_new_std(handler, ops, V4L2_CID_CONTRAST,
  605. -2, 2, 1, 0);
  606. ctrls->sharpness = v4l2_ctrl_new_std(handler, ops, V4L2_CID_SHARPNESS,
  607. -2, 2, 1, 0);
  608. ctrls->hue = v4l2_ctrl_new_std(handler, ops, V4L2_CID_HUE,
  609. -2, 2, 1, 0);
  610. ctrls->auto_wb = v4l2_ctrl_new_std_menu(handler, ops,
  611. V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  612. 8, ~0x14e, V4L2_WHITE_BALANCE_AUTO);
  613. ctrls->exposure = v4l2_ctrl_new_std(handler, ops,
  614. V4L2_CID_EXPOSURE_ABSOLUTE,
  615. -4, 4, 1, 0);
  616. ctrls->exp_metering = v4l2_ctrl_new_std_menu(handler, ops,
  617. V4L2_CID_EXPOSURE_METERING, 3,
  618. ~0xf, V4L2_EXPOSURE_METERING_AVERAGE);
  619. v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_POWER_LINE_FREQUENCY,
  620. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  621. V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
  622. /* ISO sensitivity */
  623. ctrls->auto_iso = v4l2_ctrl_new_std_menu(handler, ops,
  624. V4L2_CID_ISO_SENSITIVITY_AUTO, 1, 0,
  625. V4L2_ISO_SENSITIVITY_AUTO);
  626. ctrls->iso = v4l2_ctrl_new_int_menu(handler, ops,
  627. V4L2_CID_ISO_SENSITIVITY, ARRAY_SIZE(iso_qmenu) - 1,
  628. ARRAY_SIZE(iso_qmenu)/2 - 1, iso_qmenu);
  629. ctrls->aewb_lock = v4l2_ctrl_new_std(handler, ops,
  630. V4L2_CID_3A_LOCK, 0, 0x3, 0, 0);
  631. /* TODO: Add support for NEGATIVE_COLOR option */
  632. ctrls->colorfx = v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_COLORFX,
  633. V4L2_COLORFX_SET_CBCR + 1, ~0x1000f, V4L2_COLORFX_NONE);
  634. if (handler->error) {
  635. media_entity_cleanup(&sd->entity);
  636. return handler->error;
  637. }
  638. v4l2_ctrl_auto_cluster(2, &ctrls->auto_iso,
  639. V4L2_ISO_SENSITIVITY_MANUAL, false);
  640. sd->ctrl_handler = handler;
  641. sd->internal_ops = &fimc_is_subdev_internal_ops;
  642. sd->entity.ops = &fimc_is_subdev_media_ops;
  643. v4l2_set_subdevdata(sd, isp);
  644. __isp_subdev_set_default_format(isp);
  645. return 0;
  646. }
  647. void fimc_isp_subdev_destroy(struct fimc_isp *isp)
  648. {
  649. struct v4l2_subdev *sd = &isp->subdev;
  650. v4l2_device_unregister_subdev(sd);
  651. media_entity_cleanup(&sd->entity);
  652. v4l2_ctrl_handler_free(&isp->ctrls.handler);
  653. v4l2_set_subdevdata(sd, NULL);
  654. }