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