fimc-isp.c 19 KB

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