]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/media/atomisp/i2c/ap1302.c
staging/atomisp: fix platform_no_drv_owner.cocci warnings
[karo-tx-linux.git] / drivers / staging / media / atomisp / i2c / ap1302.c
1 /*
2  *
3  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version
7  * 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  */
20
21 #include <linux/atomisp.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-device.h>
32 #include "ap1302.h"
33
34 #define to_ap1302_device(sub_dev) \
35                 container_of(sub_dev, struct ap1302_device, sd)
36
37 /* Static definitions */
38 static struct regmap_config ap1302_reg16_config = {
39         .reg_bits = 16,
40         .val_bits = 16,
41         .reg_format_endian = REGMAP_ENDIAN_BIG,
42         .val_format_endian = REGMAP_ENDIAN_BIG,
43 };
44
45 static struct regmap_config ap1302_reg32_config = {
46         .reg_bits = 16,
47         .val_bits = 32,
48         .reg_format_endian = REGMAP_ENDIAN_BIG,
49         .val_format_endian = REGMAP_ENDIAN_BIG,
50 };
51
52 static enum ap1302_contexts ap1302_cntx_mapping[] = {
53         CONTEXT_PREVIEW,        /* Invalid atomisp run mode */
54         CONTEXT_VIDEO,          /* ATOMISP_RUN_MODE_VIDEO */
55         CONTEXT_SNAPSHOT,       /* ATOMISP_RUN_MODE_STILL_CAPTURE */
56         CONTEXT_SNAPSHOT,       /* ATOMISP_RUN_MODE_CONTINUOUS_CAPTURE */
57         CONTEXT_PREVIEW,        /* ATOMISP_RUN_MODE_PREVIEW */
58 };
59
60 static struct ap1302_res_struct ap1302_preview_res[] = {
61         {
62                 .width = 640,
63                 .height = 480,
64                 .fps = 30,
65         },
66         {
67                 .width = 720,
68                 .height = 480,
69                 .fps = 30,
70         },
71         {
72                 .width = 1280,
73                 .height = 720,
74                 .fps = 30,
75         },
76         {
77                 .width = 1920,
78                 .height = 1080,
79                 .fps = 30,
80         }
81 };
82
83 static struct ap1302_res_struct ap1302_snapshot_res[] = {
84         {
85                 .width = 640,
86                 .height = 480,
87                 .fps = 30,
88         },
89         {
90                 .width = 720,
91                 .height = 480,
92                 .fps = 30,
93         },
94         {
95                 .width = 1280,
96                 .height = 720,
97                 .fps = 30,
98         },
99         {
100                 .width = 1920,
101                 .height = 1080,
102                 .fps = 30,
103         }
104 };
105
106 static struct ap1302_res_struct ap1302_video_res[] = {
107         {
108                 .width = 640,
109                 .height = 480,
110                 .fps = 30,
111         },
112         {
113                 .width = 720,
114                 .height = 480,
115                 .fps = 30,
116         },
117         {
118                 .width = 1280,
119                 .height = 720,
120                 .fps = 30,
121         },
122         {
123                 .width = 1920,
124                 .height = 1080,
125                 .fps = 30,
126         }
127 };
128
129 static enum ap1302_contexts stream_to_context[] = {
130         CONTEXT_SNAPSHOT,
131         CONTEXT_PREVIEW,
132         CONTEXT_PREVIEW,
133         CONTEXT_VIDEO
134 };
135
136 static u16 aux_stream_config[CONTEXT_NUM][CONTEXT_NUM] = {
137         {0, 0, 0},      /* Preview: No aux streams. */
138         {1, 0, 2},      /* Snapshot: 1 for postview. 2 for video */
139         {1, 0, 0},      /* Video: 1 for preview. */
140 };
141
142 static struct ap1302_context_info context_info[] = {
143         {CNTX_WIDTH, AP1302_REG16, "width"},
144         {CNTX_HEIGHT, AP1302_REG16, "height"},
145         {CNTX_ROI_X0, AP1302_REG16, "roi_x0"},
146         {CNTX_ROI_X1, AP1302_REG16, "roi_x1"},
147         {CNTX_ROI_Y0, AP1302_REG16, "roi_y0"},
148         {CNTX_ROI_Y1, AP1302_REG16, "roi_y1"},
149         {CNTX_ASPECT, AP1302_REG16, "aspect"},
150         {CNTX_LOCK, AP1302_REG16, "lock"},
151         {CNTX_ENABLE, AP1302_REG16, "enable"},
152         {CNTX_OUT_FMT, AP1302_REG16, "out_fmt"},
153         {CNTX_SENSOR_MODE, AP1302_REG16, "sensor_mode"},
154         {CNTX_MIPI_CTRL, AP1302_REG16, "mipi_ctrl"},
155         {CNTX_MIPI_II_CTRL, AP1302_REG16, "mipi_ii_ctrl"},
156         {CNTX_LINE_TIME, AP1302_REG32, "line_time"},
157         {CNTX_MAX_FPS, AP1302_REG16, "max_fps"},
158         {CNTX_AE_USG, AP1302_REG16, "ae_usg"},
159         {CNTX_AE_UPPER_ET, AP1302_REG32, "ae_upper_et"},
160         {CNTX_AE_MAX_ET, AP1302_REG32, "ae_max_et"},
161         {CNTX_SS, AP1302_REG16, "ss"},
162         {CNTX_S1_SENSOR_MODE, AP1302_REG16, "s1_sensor_mode"},
163         {CNTX_HINF_CTRL, AP1302_REG16, "hinf_ctrl"},
164 };
165
166 /* This array stores the description list for metadata.
167    The metadata contains exposure settings and face
168    detection results. */
169 static u16 ap1302_ss_list[] = {
170         0xb01c, /* From 0x0186 with size 0x1C are exposure settings. */
171         0x0186,
172         0xb002, /* 0x71c0 is for F-number */
173         0x71c0,
174         0xb010, /* From 0x03dc with size 0x10 are face general infos. */
175         0x03dc,
176         0xb0a0, /* From 0x03e4 with size 0xa0 are face detail infos. */
177         0x03e4,
178         0xb020, /* From 0x0604 with size 0x20 are smile rate infos. */
179         0x0604,
180         0x0000
181 };
182
183 /* End of static definitions */
184
185 static int ap1302_i2c_read_reg(struct v4l2_subdev *sd,
186                                 u16 reg, u16 len, void *val)
187 {
188         struct ap1302_device *dev = to_ap1302_device(sd);
189         struct i2c_client *client = v4l2_get_subdevdata(sd);
190         int ret;
191
192         if (len == AP1302_REG16)
193                 ret = regmap_read(dev->regmap16, reg, val);
194         else if (len == AP1302_REG32)
195                 ret = regmap_read(dev->regmap32, reg, val);
196         else
197                 ret = -EINVAL;
198         if (ret) {
199                 dev_dbg(&client->dev, "Read reg failed. reg=0x%04X\n", reg);
200                 return ret;
201         }
202         if (len == AP1302_REG16)
203                 dev_dbg(&client->dev, "read_reg[0x%04X] = 0x%04X\n",
204                         reg, *(u16 *)val);
205         else
206                 dev_dbg(&client->dev, "read_reg[0x%04X] = 0x%08X\n",
207                         reg, *(u32 *)val);
208         return ret;
209 }
210
211 static int ap1302_i2c_write_reg(struct v4l2_subdev *sd,
212                                 u16 reg, u16 len, u32 val)
213 {
214         struct ap1302_device *dev = to_ap1302_device(sd);
215         struct i2c_client *client = v4l2_get_subdevdata(sd);
216         int ret;
217         if (len == AP1302_REG16)
218                 ret = regmap_write(dev->regmap16, reg, val);
219         else if (len == AP1302_REG32)
220                 ret = regmap_write(dev->regmap32, reg, val);
221         else
222                 ret = -EINVAL;
223         if (ret) {
224                 dev_dbg(&client->dev, "Write reg failed. reg=0x%04X\n", reg);
225                 return ret;
226         }
227         if (len == AP1302_REG16)
228                 dev_dbg(&client->dev, "write_reg[0x%04X] = 0x%04X\n",
229                         reg, (u16)val);
230         else
231                 dev_dbg(&client->dev, "write_reg[0x%04X] = 0x%08X\n",
232                         reg, (u32)val);
233         return ret;
234 }
235
236 static u16
237 ap1302_calculate_context_reg_addr(enum ap1302_contexts context, u16 offset)
238 {
239         u16 reg_addr;
240         /* The register offset is defined according to preview/video registers.
241            Preview and video context have the same register definition.
242            But snapshot context does not have register S1_SENSOR_MODE.
243            When setting snapshot registers, if the offset exceeds
244            S1_SENSOR_MODE, the actual offset needs to minus 2. */
245         if (context == CONTEXT_SNAPSHOT) {
246                 if (offset == CNTX_S1_SENSOR_MODE)
247                         return 0;
248                 if (offset > CNTX_S1_SENSOR_MODE)
249                         offset -= 2;
250         }
251         if (context == CONTEXT_PREVIEW)
252                 reg_addr = REG_PREVIEW_BASE + offset;
253         else if (context == CONTEXT_VIDEO)
254                 reg_addr = REG_VIDEO_BASE + offset;
255         else
256                 reg_addr = REG_SNAPSHOT_BASE + offset;
257         return reg_addr;
258 }
259
260 static int ap1302_read_context_reg(struct v4l2_subdev *sd,
261                 enum ap1302_contexts context, u16 offset, u16 len)
262 {
263         struct ap1302_device *dev = to_ap1302_device(sd);
264         u16 reg_addr = ap1302_calculate_context_reg_addr(context, offset);
265         if (reg_addr == 0)
266                 return -EINVAL;
267         return ap1302_i2c_read_reg(sd, reg_addr, len,
268                             ((u8 *)&dev->cntx_config[context]) + offset);
269 }
270
271 static int ap1302_write_context_reg(struct v4l2_subdev *sd,
272                 enum ap1302_contexts context, u16 offset, u16 len)
273 {
274         struct ap1302_device *dev = to_ap1302_device(sd);
275         u16 reg_addr = ap1302_calculate_context_reg_addr(context, offset);
276         if (reg_addr == 0)
277                 return -EINVAL;
278         return ap1302_i2c_write_reg(sd, reg_addr, len,
279                         *(u32 *)(((u8 *)&dev->cntx_config[context]) + offset));
280 }
281
282 static int ap1302_dump_context_reg(struct v4l2_subdev *sd,
283                                    enum ap1302_contexts context)
284 {
285         struct i2c_client *client = v4l2_get_subdevdata(sd);
286         struct ap1302_device *dev = to_ap1302_device(sd);
287         int i;
288         dev_dbg(&client->dev, "Dump registers for context[%d]:\n", context);
289         for (i = 0; i < ARRAY_SIZE(context_info); i++) {
290                 struct ap1302_context_info *info = &context_info[i];
291                 u8 *var = (u8 *)&dev->cntx_config[context] + info->offset;
292                 /* Snapshot context does not have s1_sensor_mode register. */
293                 if (context == CONTEXT_SNAPSHOT &&
294                         info->offset == CNTX_S1_SENSOR_MODE)
295                         continue;
296                 ap1302_read_context_reg(sd, context, info->offset, info->len);
297                 if (info->len == AP1302_REG16)
298                         dev_dbg(&client->dev, "context.%s = 0x%04X (%d)\n",
299                                 info->name, *(u16 *)var, *(u16 *)var);
300                 else
301                         dev_dbg(&client->dev, "context.%s = 0x%08X (%d)\n",
302                                 info->name, *(u32 *)var, *(u32 *)var);
303         }
304         return 0;
305 }
306
307 static int ap1302_request_firmware(struct v4l2_subdev *sd)
308 {
309         struct i2c_client *client = v4l2_get_subdevdata(sd);
310         struct ap1302_device *dev = to_ap1302_device(sd);
311         int ret;
312         ret = request_firmware(&dev->fw, "ap1302_fw.bin", &client->dev);
313         if (ret)
314                 dev_err(&client->dev,
315                         "ap1302_request_firmware failed. ret=%d\n", ret);
316         return ret;
317 }
318
319 /* When loading firmware, host writes firmware data from address 0x8000.
320    When the address reaches 0x9FFF, the next address should return to 0x8000.
321    This function handles this address window and load firmware data to AP1302.
322    win_pos indicates the offset within this window. Firmware loading procedure
323    may call this function several times. win_pos records the current position
324    that has been written to.*/
325 static int ap1302_write_fw_window(struct v4l2_subdev *sd,
326                                   u16 *win_pos, const u8 *buf, u32 len)
327 {
328         struct ap1302_device *dev = to_ap1302_device(sd);
329         int ret;
330         u32 pos;
331         u32 sub_len;
332         for (pos = 0; pos < len; pos += sub_len) {
333                 if (len - pos < AP1302_FW_WINDOW_SIZE - *win_pos)
334                         sub_len = len - pos;
335                 else
336                         sub_len = AP1302_FW_WINDOW_SIZE - *win_pos;
337                 ret = regmap_raw_write(dev->regmap16,
338                                         *win_pos + AP1302_FW_WINDOW_OFFSET,
339                                         buf + pos, sub_len);
340                 if (ret)
341                         return ret;
342                 *win_pos += sub_len;
343                 if (*win_pos >= AP1302_FW_WINDOW_SIZE)
344                         *win_pos = 0;
345         }
346         return 0;
347 }
348
349 static int ap1302_load_firmware(struct v4l2_subdev *sd)
350 {
351         struct i2c_client *client = v4l2_get_subdevdata(sd);
352         struct ap1302_device *dev = to_ap1302_device(sd);
353         const struct ap1302_firmware *fw;
354         const u8 *fw_data;
355         u16 reg_val = 0;
356         u16 win_pos = 0;
357         int ret;
358
359         dev_info(&client->dev, "Start to load firmware.\n");
360         if (!dev->fw) {
361                 dev_err(&client->dev, "firmware not requested.\n");
362                 return -EINVAL;
363         }
364         fw = (const struct ap1302_firmware *) dev->fw->data;
365         if (dev->fw->size != (sizeof(*fw) + fw->total_size)) {
366                 dev_err(&client->dev, "firmware size does not match.\n");
367                 return -EINVAL;
368         }
369         /* The fw binary contains a header of struct ap1302_firmware.
370            Following the header is the bootdata of AP1302.
371            The bootdata pointer can be referenced as &fw[1]. */
372         fw_data = (u8 *)&fw[1];
373
374         /* Clear crc register. */
375         ret = ap1302_i2c_write_reg(sd, REG_SIP_CRC, AP1302_REG16, 0xFFFF);
376         if (ret)
377                 return ret;
378
379         /* Load FW data for PLL init stage. */
380         ret = ap1302_write_fw_window(sd, &win_pos, fw_data, fw->pll_init_size);
381         if (ret)
382                 return ret;
383
384         /* Write 2 to bootdata_stage register to apply basic_init_hp
385            settings and enable PLL. */
386         ret = ap1302_i2c_write_reg(sd, REG_BOOTDATA_STAGE,
387                                    AP1302_REG16, 0x0002);
388         if (ret)
389                 return ret;
390
391         /* Wait 1ms for PLL to lock. */
392         msleep(20);
393
394         /* Load the rest of bootdata content. */
395         ret = ap1302_write_fw_window(sd, &win_pos, fw_data + fw->pll_init_size,
396                                      fw->total_size - fw->pll_init_size);
397         if (ret)
398                 return ret;
399
400         /* Check crc. */
401         ret = ap1302_i2c_read_reg(sd, REG_SIP_CRC, AP1302_REG16, &reg_val);
402         if (ret)
403                 return ret;
404         if (reg_val != fw->crc) {
405                 dev_err(&client->dev,
406                         "crc does not match. T:0x%04X F:0x%04X\n",
407                         fw->crc, reg_val);
408                 return -EAGAIN;
409         }
410
411         /* Write 0xFFFF to bootdata_stage register to indicate AP1302 that
412            the whole bootdata content has been loaded. */
413         ret = ap1302_i2c_write_reg(sd, REG_BOOTDATA_STAGE,
414                                    AP1302_REG16, 0xFFFF);
415         if (ret)
416                 return ret;
417         dev_info(&client->dev, "Load firmware successfully.\n");
418
419         return 0;
420 }
421
422 static int __ap1302_s_power(struct v4l2_subdev *sd, int on, int load_fw)
423 {
424         struct ap1302_device *dev = to_ap1302_device(sd);
425         struct i2c_client *client = v4l2_get_subdevdata(sd);
426         int ret, i;
427         u16 ss_ptr;
428
429         dev_info(&client->dev, "ap1302_s_power is called.\n");
430         ret = dev->platform_data->power_ctrl(sd, on);
431         if (ret) {
432                 dev_err(&client->dev,
433                         "ap1302_s_power error. on=%d ret=%d\n", on, ret);
434                 return ret;
435         }
436         dev->power_on = on;
437         if (!on || !load_fw)
438                 return 0;
439         /* Load firmware after power on. */
440         ret = ap1302_load_firmware(sd);
441         if (ret) {
442                 dev_err(&client->dev,
443                         "ap1302_load_firmware failed. ret=%d\n", ret);
444                 return ret;
445         }
446         ret = ap1302_i2c_read_reg(sd, REG_SS_HEAD_PT0, AP1302_REG16, &ss_ptr);
447         if (ret)
448                 return ret;
449         for (i = 0; i < ARRAY_SIZE(ap1302_ss_list); i++) {
450                 ret = ap1302_i2c_write_reg(sd, ss_ptr + i * 2,
451                         AP1302_REG16, ap1302_ss_list[i]);
452                 if (ret)
453                         return ret;
454         }
455         return ret;
456 }
457
458 static int ap1302_s_power(struct v4l2_subdev *sd, int on)
459 {
460         struct ap1302_device *dev = to_ap1302_device(sd);
461         int ret;
462
463         mutex_lock(&dev->input_lock);
464         ret = __ap1302_s_power(sd, on, 1);
465         dev->sys_activated = 0;
466         mutex_unlock(&dev->input_lock);
467
468         return ret;
469 }
470
471 static int ap1302_s_config(struct v4l2_subdev *sd, void *pdata)
472 {
473         struct ap1302_device *dev = to_ap1302_device(sd);
474         struct i2c_client *client = v4l2_get_subdevdata(sd);
475         struct camera_mipi_info *mipi_info;
476         u16 reg_val = 0;
477         int ret;
478
479         dev_info(&client->dev, "ap1302_s_config is called.\n");
480         if (pdata == NULL)
481                 return -ENODEV;
482
483         dev->platform_data = pdata;
484
485         mutex_lock(&dev->input_lock);
486
487         if (dev->platform_data->platform_init) {
488                 ret = dev->platform_data->platform_init(client);
489                 if (ret)
490                         goto fail_power;
491         }
492
493         ret = __ap1302_s_power(sd, 1, 0);
494         if (ret)
495                 goto fail_power;
496
497         /* Detect for AP1302 */
498         ret = ap1302_i2c_read_reg(sd, REG_CHIP_VERSION, AP1302_REG16, &reg_val);
499         if (ret || (reg_val != AP1302_CHIP_ID)) {
500                 dev_err(&client->dev,
501                         "Chip version does no match. ret=%d ver=0x%04x\n",
502                         ret, reg_val);
503                 goto fail_config;
504         }
505         dev_info(&client->dev, "AP1302 Chip ID is 0x%X\n", reg_val);
506
507         /* Detect revision for AP1302 */
508         ret = ap1302_i2c_read_reg(sd, REG_CHIP_REV, AP1302_REG16, &reg_val);
509         if (ret)
510                 goto fail_config;
511         dev_info(&client->dev, "AP1302 Chip Rev is 0x%X\n", reg_val);
512         ret = dev->platform_data->csi_cfg(sd, 1);
513         if (ret)
514                 goto fail_config;
515
516         mipi_info = v4l2_get_subdev_hostdata(sd);
517         if (!mipi_info)
518                 goto fail_config;
519         dev->num_lanes = mipi_info->num_lanes;
520
521         ret = __ap1302_s_power(sd, 0, 0);
522         if (ret)
523                 goto fail_power;
524
525         mutex_unlock(&dev->input_lock);
526
527         return ret;
528
529 fail_config:
530         __ap1302_s_power(sd, 0, 0);
531 fail_power:
532         mutex_unlock(&dev->input_lock);
533         dev_err(&client->dev, "ap1302_s_config failed\n");
534         return ret;
535 }
536
537 static enum ap1302_contexts ap1302_get_context(struct v4l2_subdev *sd)
538 {
539         struct ap1302_device *dev = to_ap1302_device(sd);
540         return dev->cur_context;
541 }
542
543 static int ap1302_enum_mbus_code(struct v4l2_subdev *sd,
544                                  struct v4l2_subdev_pad_config *cfg,
545                                  struct v4l2_subdev_mbus_code_enum *code)
546 {
547         if (code->index)
548                 return -EINVAL;
549
550         code->code = MEDIA_BUS_FMT_UYVY8_1X16;
551
552         return 0;
553 }
554
555 static int ap1302_match_resolution(struct ap1302_context_res *res,
556                                    struct v4l2_mbus_framefmt *fmt)
557 {
558         s32 w0, h0, mismatch, distance;
559         s32 w1 = fmt->width;
560         s32 h1 = fmt->height;
561         s32 min_distance = INT_MAX;
562         s32 i, idx = -1;
563
564         if (w1 == 0 || h1 == 0)
565                 return -1;
566
567         for (i = 0; i < res->res_num; i++) {
568                 w0 = res->res_table[i].width;
569                 h0 = res->res_table[i].height;
570                 if (w0 < w1 || h0 < h1)
571                         continue;
572                 mismatch = abs(w0 * h1 - w1 * h0) * 8192 / w1 / h0;
573                 if (mismatch > 8192 * AP1302_MAX_RATIO_MISMATCH / 100)
574                         continue;
575                 distance = (w0 * h1 + w1 * h0) * 8192 / w1 / h1;
576                 if (distance < min_distance) {
577                         min_distance = distance;
578                         idx = i;
579                 }
580         }
581
582         return idx;
583 }
584
585 static s32 ap1302_try_mbus_fmt_locked(struct v4l2_subdev *sd,
586                                 enum ap1302_contexts context,
587                                 struct v4l2_mbus_framefmt *fmt)
588 {
589         struct ap1302_device *dev = to_ap1302_device(sd);
590         struct ap1302_res_struct *res_table;
591         s32 res_num, idx = -1;
592
593         res_table = dev->cntx_res[context].res_table;
594         res_num = dev->cntx_res[context].res_num;
595
596         if ((fmt->width <= res_table[res_num - 1].width) &&
597                 (fmt->height <= res_table[res_num - 1].height))
598                 idx = ap1302_match_resolution(&dev->cntx_res[context], fmt);
599         if (idx == -1)
600                 idx = res_num - 1;
601
602         fmt->width = res_table[idx].width;
603         fmt->height = res_table[idx].height;
604         fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
605         return idx;
606 }
607
608
609 static int ap1302_get_fmt(struct v4l2_subdev *sd,
610                          struct v4l2_subdev_pad_config *cfg,
611                                          struct v4l2_subdev_format *format)
612
613 {
614     struct v4l2_mbus_framefmt *fmt = &format->format;
615     struct ap1302_device *dev = to_ap1302_device(sd);
616         enum ap1302_contexts context;
617         struct ap1302_res_struct *res_table;
618         s32 cur_res;
619      if (format->pad)
620                 return -EINVAL;
621         mutex_lock(&dev->input_lock);
622         context = ap1302_get_context(sd);
623         res_table = dev->cntx_res[context].res_table;
624         cur_res = dev->cntx_res[context].cur_res;
625         fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
626         fmt->width = res_table[cur_res].width;
627         fmt->height = res_table[cur_res].height;
628         mutex_unlock(&dev->input_lock);
629         return 0;
630 }
631
632 static int ap1302_set_fmt(struct v4l2_subdev *sd,
633                           struct v4l2_subdev_pad_config *cfg,
634                           struct v4l2_subdev_format *format)
635 {
636         struct v4l2_mbus_framefmt *fmt = &format->format;
637         struct ap1302_device *dev = to_ap1302_device(sd);
638         struct i2c_client *client = v4l2_get_subdevdata(sd);
639         struct atomisp_input_stream_info *stream_info =
640                 (struct atomisp_input_stream_info *)fmt->reserved;
641         enum ap1302_contexts context, main_context;
642         if (format->pad)
643                 return -EINVAL;
644         if (!fmt)
645                 return -EINVAL;
646         mutex_lock(&dev->input_lock);
647         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
648                 context = ap1302_get_context(sd);
649                 ap1302_try_mbus_fmt_locked(sd, context, fmt);
650                 cfg->try_fmt = *fmt;
651             mutex_unlock(&dev->input_lock);
652                 return 0;
653                 }
654         context = stream_to_context[stream_info->stream];
655         dev_dbg(&client->dev, "ap1302_set_mbus_fmt. stream=%d context=%d\n",
656                 stream_info->stream, context);
657         dev->cntx_res[context].cur_res =
658                 ap1302_try_mbus_fmt_locked(sd, context, fmt);
659         dev->cntx_config[context].width = fmt->width;
660         dev->cntx_config[context].height = fmt->height;
661         ap1302_write_context_reg(sd, context, CNTX_WIDTH, AP1302_REG16);
662         ap1302_write_context_reg(sd, context, CNTX_HEIGHT, AP1302_REG16);
663         ap1302_read_context_reg(sd, context, CNTX_OUT_FMT, AP1302_REG16);
664         dev->cntx_config[context].out_fmt &= ~OUT_FMT_TYPE_MASK;
665         dev->cntx_config[context].out_fmt |= AP1302_FMT_UYVY422;
666         ap1302_write_context_reg(sd, context, CNTX_OUT_FMT, AP1302_REG16);
667
668         main_context = ap1302_get_context(sd);
669         if (context == main_context) {
670                 ap1302_read_context_reg(sd, context,
671                         CNTX_MIPI_CTRL, AP1302_REG16);
672                 dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_IMGVC_MASK;
673                 dev->cntx_config[context].mipi_ctrl |=
674                         (context << MIPI_CTRL_IMGVC_OFFSET);
675                 dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_SSVC_MASK;
676                 dev->cntx_config[context].mipi_ctrl |=
677                         (context << MIPI_CTRL_SSVC_OFFSET);
678                 dev->cntx_config[context].mipi_ctrl &= ~MIPI_CTRL_SSTYPE_MASK;
679                 dev->cntx_config[context].mipi_ctrl |=
680                         (0x12 << MIPI_CTRL_SSTYPE_OFFSET);
681                 ap1302_write_context_reg(sd, context,
682                         CNTX_MIPI_CTRL, AP1302_REG16);
683                 ap1302_read_context_reg(sd, context,
684                         CNTX_SS, AP1302_REG16);
685                 dev->cntx_config[context].ss = AP1302_SS_CTRL;
686                 ap1302_write_context_reg(sd, context,
687                         CNTX_SS, AP1302_REG16);
688         } else {
689                 /* Configure aux stream */
690                 ap1302_read_context_reg(sd, context,
691                         CNTX_MIPI_II_CTRL, AP1302_REG16);
692                 dev->cntx_config[context].mipi_ii_ctrl &= ~MIPI_CTRL_IMGVC_MASK;
693                 dev->cntx_config[context].mipi_ii_ctrl |=
694                         (context << MIPI_CTRL_IMGVC_OFFSET);
695                 ap1302_write_context_reg(sd, context,
696                         CNTX_MIPI_II_CTRL, AP1302_REG16);
697                 if (stream_info->enable) {
698                         ap1302_read_context_reg(sd, main_context,
699                                 CNTX_OUT_FMT, AP1302_REG16);
700                         dev->cntx_config[context].out_fmt |=
701                                 (aux_stream_config[main_context][context]
702                                  << OUT_FMT_IIS_OFFSET);
703                         ap1302_write_context_reg(sd, main_context,
704                                 CNTX_OUT_FMT, AP1302_REG16);
705                 }
706         }
707         stream_info->ch_id = context;
708         mutex_unlock(&dev->input_lock);
709
710         return 0;
711 }
712
713
714 static int ap1302_g_frame_interval(struct v4l2_subdev *sd,
715                         struct v4l2_subdev_frame_interval *interval)
716 {
717         struct ap1302_device *dev = to_ap1302_device(sd);
718         enum ap1302_contexts context;
719         struct ap1302_res_struct *res_table;
720         u32 cur_res;
721
722         mutex_lock(&dev->input_lock);
723         context = ap1302_get_context(sd);
724         res_table = dev->cntx_res[context].res_table;
725         cur_res = dev->cntx_res[context].cur_res;
726         interval->interval.denominator = res_table[cur_res].fps;
727         interval->interval.numerator = 1;
728         mutex_unlock(&dev->input_lock);
729         return 0;
730 }
731
732 static int ap1302_enum_frame_size(struct v4l2_subdev *sd,
733                                   struct v4l2_subdev_pad_config *cfg,
734                                   struct v4l2_subdev_frame_size_enum *fse)
735 {
736         struct ap1302_device *dev = to_ap1302_device(sd);
737         enum ap1302_contexts context;
738         struct ap1302_res_struct *res_table;
739         int index = fse->index;
740
741         mutex_lock(&dev->input_lock);
742         context = ap1302_get_context(sd);
743         if (index >= dev->cntx_res[context].res_num) {
744                 mutex_unlock(&dev->input_lock);
745                 return -EINVAL;
746         }
747
748         res_table = dev->cntx_res[context].res_table;
749         fse->min_width = res_table[index].width;
750         fse->min_height = res_table[index].height;
751         fse->max_width = res_table[index].width;
752         fse->max_height = res_table[index].height;
753         mutex_unlock(&dev->input_lock);
754
755         return 0;
756 }
757
758
759 static int ap1302_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
760 {
761         *frames = 0;
762         return 0;
763 }
764
765 static int ap1302_s_stream(struct v4l2_subdev *sd, int enable)
766 {
767         struct ap1302_device *dev = to_ap1302_device(sd);
768         struct i2c_client *client = v4l2_get_subdevdata(sd);
769         enum ap1302_contexts context;
770         u32 reg_val;
771         int ret;
772
773         mutex_lock(&dev->input_lock);
774         context = ap1302_get_context(sd);
775         dev_dbg(&client->dev, "ap1302_s_stream. context=%d enable=%d\n",
776                         context, enable);
777         /* Switch context */
778         ap1302_i2c_read_reg(sd, REG_CTRL,
779                             AP1302_REG16, &reg_val);
780         reg_val &= ~CTRL_CNTX_MASK;
781         reg_val |= (context<<CTRL_CNTX_OFFSET);
782         ap1302_i2c_write_reg(sd, REG_CTRL,
783                             AP1302_REG16, reg_val);
784         /* Select sensor */
785         ap1302_i2c_read_reg(sd, REG_SENSOR_SELECT,
786                             AP1302_REG16, &reg_val);
787         reg_val &= ~SENSOR_SELECT_MASK;
788         reg_val |= (AP1302_SENSOR_PRI<<SENSOR_SELECT_OFFSET);
789         ap1302_i2c_write_reg(sd, REG_SENSOR_SELECT,
790                             AP1302_REG16, reg_val);
791         if (enable) {
792                 dev_info(&client->dev, "Start stream. context=%d\n", context);
793                 ap1302_dump_context_reg(sd, context);
794                 if (!dev->sys_activated) {
795                         reg_val = AP1302_SYS_ACTIVATE;
796                         dev->sys_activated = 1;
797                 } else {
798                         reg_val = AP1302_SYS_SWITCH;
799                 }
800         } else {
801                 dev_info(&client->dev, "Stop stream. context=%d\n", context);
802                 reg_val = AP1302_SYS_SWITCH;
803         }
804         ret = ap1302_i2c_write_reg(sd, REG_SYS_START, AP1302_REG16, reg_val);
805         if (ret)
806                 dev_err(&client->dev,
807                         "AP1302 set stream failed. enable=%d\n", enable);
808         mutex_unlock(&dev->input_lock);
809         return ret;
810 }
811
812 static u16 ap1302_ev_values[] = {0xfd00, 0xfe80, 0x0, 0x180, 0x300};
813
814 static int ap1302_set_exposure_off(struct v4l2_subdev *sd, s32 val)
815 {
816         val -= AP1302_MIN_EV;
817         return ap1302_i2c_write_reg(sd, REG_AE_BV_OFF, AP1302_REG16,
818                                 ap1302_ev_values[val]);
819 }
820
821 static u16 ap1302_wb_values[] = {
822         0, /* V4L2_WHITE_BALANCE_MANUAL */
823         0xf, /* V4L2_WHITE_BALANCE_AUTO */
824         0x2, /* V4L2_WHITE_BALANCE_INCANDESCENT */
825         0x4, /* V4L2_WHITE_BALANCE_FLUORESCENT */
826         0x5, /* V4L2_WHITE_BALANCE_FLUORESCENT_H */
827         0x1, /* V4L2_WHITE_BALANCE_HORIZON */
828         0x5, /* V4L2_WHITE_BALANCE_DAYLIGHT */
829         0xf, /* V4L2_WHITE_BALANCE_FLASH */
830         0x6, /* V4L2_WHITE_BALANCE_CLOUDY */
831         0x6, /* V4L2_WHITE_BALANCE_SHADE */
832 };
833
834 static int ap1302_set_wb_mode(struct v4l2_subdev *sd, s32 val)
835 {
836         int ret = 0;
837         u16 reg_val;
838
839         ret = ap1302_i2c_read_reg(sd, REG_AWB_CTRL, AP1302_REG16, &reg_val);
840         if (ret)
841                 return ret;
842         reg_val &= ~AWB_CTRL_MODE_MASK;
843         reg_val |= ap1302_wb_values[val] << AWB_CTRL_MODE_OFFSET;
844         if (val == V4L2_WHITE_BALANCE_FLASH)
845                 reg_val |= AWB_CTRL_FLASH_MASK;
846         else
847                 reg_val &= ~AWB_CTRL_FLASH_MASK;
848         ret = ap1302_i2c_write_reg(sd, REG_AWB_CTRL, AP1302_REG16, reg_val);
849         return ret;
850 }
851
852 static int ap1302_set_zoom(struct v4l2_subdev *sd, s32 val)
853 {
854         ap1302_i2c_write_reg(sd, REG_DZ_TGT_FCT, AP1302_REG16,
855                 val * 4 + 0x100);
856         return 0;
857 }
858
859 static u16 ap1302_sfx_values[] = {
860         0x00, /* V4L2_COLORFX_NONE */
861         0x03, /* V4L2_COLORFX_BW */
862         0x0d, /* V4L2_COLORFX_SEPIA */
863         0x07, /* V4L2_COLORFX_NEGATIVE */
864         0x04, /* V4L2_COLORFX_EMBOSS */
865         0x0f, /* V4L2_COLORFX_SKETCH */
866         0x08, /* V4L2_COLORFX_SKY_BLUE */
867         0x09, /* V4L2_COLORFX_GRASS_GREEN */
868         0x0a, /* V4L2_COLORFX_SKIN_WHITEN */
869         0x00, /* V4L2_COLORFX_VIVID */
870         0x00, /* V4L2_COLORFX_AQUA */
871         0x00, /* V4L2_COLORFX_ART_FREEZE */
872         0x00, /* V4L2_COLORFX_SILHOUETTE */
873         0x10, /* V4L2_COLORFX_SOLARIZATION */
874         0x02, /* V4L2_COLORFX_ANTIQUE */
875         0x00, /* V4L2_COLORFX_SET_CBCR */
876 };
877
878 static int ap1302_set_special_effect(struct v4l2_subdev *sd, s32 val)
879 {
880         ap1302_i2c_write_reg(sd, REG_SFX_MODE, AP1302_REG16,
881                 ap1302_sfx_values[val]);
882         return 0;
883 }
884
885 static u16 ap1302_scene_mode_values[] = {
886         0x00, /* V4L2_SCENE_MODE_NONE */
887         0x07, /* V4L2_SCENE_MODE_BACKLIGHT */
888         0x0a, /* V4L2_SCENE_MODE_BEACH_SNOW */
889         0x06, /* V4L2_SCENE_MODE_CANDLE_LIGHT */
890         0x00, /* V4L2_SCENE_MODE_DAWN_DUSK */
891         0x00, /* V4L2_SCENE_MODE_FALL_COLORS */
892         0x0d, /* V4L2_SCENE_MODE_FIREWORKS */
893         0x02, /* V4L2_SCENE_MODE_LANDSCAPE */
894         0x05, /* V4L2_SCENE_MODE_NIGHT */
895         0x0c, /* V4L2_SCENE_MODE_PARTY_INDOOR */
896         0x01, /* V4L2_SCENE_MODE_PORTRAIT */
897         0x03, /* V4L2_SCENE_MODE_SPORTS */
898         0x0e, /* V4L2_SCENE_MODE_SUNSET */
899         0x0b, /* V4L2_SCENE_MODE_TEXT */
900 };
901
902 static int ap1302_set_scene_mode(struct v4l2_subdev *sd, s32 val)
903 {
904         ap1302_i2c_write_reg(sd, REG_SCENE_CTRL, AP1302_REG16,
905                 ap1302_scene_mode_values[val]);
906         return 0;
907 }
908
909 static u16 ap1302_flicker_values[] = {
910         0x0,    /* OFF */
911         0x3201, /* 50HZ */
912         0x3c01, /* 60HZ */
913         0x2     /* AUTO */
914 };
915
916 static int ap1302_set_flicker_freq(struct v4l2_subdev *sd, s32 val)
917 {
918         ap1302_i2c_write_reg(sd, REG_FLICK_CTRL, AP1302_REG16,
919                 ap1302_flicker_values[val]);
920         return 0;
921 }
922
923 static int ap1302_s_ctrl(struct v4l2_ctrl *ctrl)
924 {
925         struct ap1302_device *dev = container_of(
926                 ctrl->handler, struct ap1302_device, ctrl_handler);
927
928         switch (ctrl->id) {
929         case V4L2_CID_RUN_MODE:
930                 dev->cur_context = ap1302_cntx_mapping[ctrl->val];
931                 break;
932         case V4L2_CID_EXPOSURE:
933                 ap1302_set_exposure_off(&dev->sd, ctrl->val);
934                 break;
935         case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
936                 ap1302_set_wb_mode(&dev->sd, ctrl->val);
937                 break;
938         case V4L2_CID_ZOOM_ABSOLUTE:
939                 ap1302_set_zoom(&dev->sd, ctrl->val);
940                 break;
941         case V4L2_CID_COLORFX:
942                 ap1302_set_special_effect(&dev->sd, ctrl->val);
943                 break;
944         case V4L2_CID_SCENE_MODE:
945                 ap1302_set_scene_mode(&dev->sd, ctrl->val);
946                 break;
947         case V4L2_CID_POWER_LINE_FREQUENCY:
948                 ap1302_set_flicker_freq(&dev->sd, ctrl->val);
949                 break;
950         default:
951                 return -EINVAL;
952         }
953
954         return 0;
955 }
956
957 static int ap1302_g_register(struct v4l2_subdev *sd,
958                              struct v4l2_dbg_register *reg)
959 {
960         struct ap1302_device *dev = to_ap1302_device(sd);
961         int ret;
962         u32 reg_val;
963
964         if (reg->size != AP1302_REG16 &&
965             reg->size != AP1302_REG32)
966                 return -EINVAL;
967
968         mutex_lock(&dev->input_lock);
969         if (dev->power_on)
970                 ret = ap1302_i2c_read_reg(sd, reg->reg, reg->size, &reg_val);
971         else
972                 ret = -EIO;
973         mutex_unlock(&dev->input_lock);
974         if (ret)
975                 return ret;
976
977         reg->val = reg_val;
978
979         return 0;
980 }
981
982 static int ap1302_s_register(struct v4l2_subdev *sd,
983                              const struct v4l2_dbg_register *reg)
984 {
985         struct ap1302_device *dev = to_ap1302_device(sd);
986         int ret;
987
988         if (reg->size != AP1302_REG16 &&
989             reg->size != AP1302_REG32)
990                 return -EINVAL;
991
992         mutex_lock(&dev->input_lock);
993         if (dev->power_on)
994                 ret = ap1302_i2c_write_reg(sd, reg->reg, reg->size, reg->val);
995         else
996                 ret = -EIO;
997         mutex_unlock(&dev->input_lock);
998         return ret;
999 }
1000
1001 static long ap1302_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1002 {
1003         long ret = 0;
1004         switch (cmd) {
1005         case VIDIOC_DBG_G_REGISTER:
1006                 ret = ap1302_g_register(sd, arg);
1007                 break;
1008         case VIDIOC_DBG_S_REGISTER:
1009                 ret = ap1302_s_register(sd, arg);
1010                 break;
1011         default:
1012                 ret = -EINVAL;
1013         }
1014         return ret;
1015 }
1016
1017 static const struct v4l2_ctrl_ops ctrl_ops = {
1018         .s_ctrl = ap1302_s_ctrl,
1019 };
1020
1021 static const char * const ctrl_run_mode_menu[] = {
1022         NULL,
1023         "Video",
1024         "Still capture",
1025         "Continuous capture",
1026         "Preview",
1027 };
1028
1029 static const struct v4l2_ctrl_config ctrls[] = {
1030         {
1031                 .ops = &ctrl_ops,
1032                 .id = V4L2_CID_RUN_MODE,
1033                 .name = "Run Mode",
1034                 .type = V4L2_CTRL_TYPE_MENU,
1035                 .min = 1,
1036                 .def = 4,
1037                 .max = 4,
1038                 .qmenu = ctrl_run_mode_menu,
1039         },
1040         {
1041                 .ops = &ctrl_ops,
1042                 .id = V4L2_CID_EXPOSURE,
1043                 .name = "Exposure",
1044                 .type = V4L2_CTRL_TYPE_INTEGER,
1045                 .min = AP1302_MIN_EV,
1046                 .def = 0,
1047                 .max = AP1302_MAX_EV,
1048                 .step = 1,
1049         },
1050         {
1051                 .ops = &ctrl_ops,
1052                 .id = V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
1053                 .name = "White Balance",
1054                 .type = V4L2_CTRL_TYPE_INTEGER,
1055                 .min = 0,
1056                 .def = 0,
1057                 .max = 9,
1058                 .step = 1,
1059         },
1060         {
1061                 .ops = &ctrl_ops,
1062                 .id = V4L2_CID_ZOOM_ABSOLUTE,
1063                 .name = "Zoom Absolute",
1064                 .type = V4L2_CTRL_TYPE_INTEGER,
1065                 .min = 0,
1066                 .def = 0,
1067                 .max = 1024,
1068                 .step = 1,
1069         },
1070         {
1071                 .ops = &ctrl_ops,
1072                 .id = V4L2_CID_COLORFX,
1073                 .name = "Color Special Effect",
1074                 .type = V4L2_CTRL_TYPE_INTEGER,
1075                 .min = 0,
1076                 .def = 0,
1077                 .max = 15,
1078                 .step = 1,
1079         },
1080         {
1081                 .ops = &ctrl_ops,
1082                 .id = V4L2_CID_SCENE_MODE,
1083                 .name = "Scene Mode",
1084                 .type = V4L2_CTRL_TYPE_INTEGER,
1085                 .min = 0,
1086                 .def = 0,
1087                 .max = 13,
1088                 .step = 1,
1089         },
1090         {
1091                 .ops = &ctrl_ops,
1092                 .id = V4L2_CID_POWER_LINE_FREQUENCY,
1093                 .name = "Light frequency filter",
1094                 .type = V4L2_CTRL_TYPE_INTEGER,
1095                 .min = 0,
1096                 .def = 3,
1097                 .max = 3,
1098                 .step = 1,
1099         },
1100 };
1101
1102 static struct v4l2_subdev_sensor_ops ap1302_sensor_ops = {
1103         .g_skip_frames  = ap1302_g_skip_frames,
1104 };
1105
1106 static const struct v4l2_subdev_video_ops ap1302_video_ops = {
1107         .s_stream = ap1302_s_stream,
1108         .g_frame_interval = ap1302_g_frame_interval,
1109 };
1110
1111 static const struct v4l2_subdev_core_ops ap1302_core_ops = {
1112         .s_power = ap1302_s_power,
1113         .ioctl = ap1302_ioctl,
1114 #ifdef CONFIG_VIDEO_ADV_DEBUG
1115         .g_register = ap1302_g_register,
1116         .s_register = ap1302_s_register,
1117 #endif
1118 };
1119
1120 static const struct v4l2_subdev_pad_ops ap1302_pad_ops = {
1121         .enum_mbus_code = ap1302_enum_mbus_code,
1122         .enum_frame_size = ap1302_enum_frame_size,
1123         .get_fmt = ap1302_get_fmt,
1124         .set_fmt = ap1302_set_fmt,
1125 };
1126
1127 static const struct v4l2_subdev_ops ap1302_ops = {
1128         .core = &ap1302_core_ops,
1129         .pad = &ap1302_pad_ops,
1130         .video = &ap1302_video_ops,
1131         .sensor = &ap1302_sensor_ops
1132 };
1133
1134 static int ap1302_remove(struct i2c_client *client)
1135 {
1136         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1137         struct ap1302_device *dev = to_ap1302_device(sd);
1138
1139         if (dev->platform_data->platform_deinit)
1140                 dev->platform_data->platform_deinit();
1141
1142         release_firmware(dev->fw);
1143
1144         media_entity_cleanup(&dev->sd.entity);
1145         dev->platform_data->csi_cfg(sd, 0);
1146         v4l2_device_unregister_subdev(sd);
1147
1148         return 0;
1149 }
1150
1151 static int ap1302_probe(struct i2c_client *client,
1152                         const struct i2c_device_id *id)
1153 {
1154         struct ap1302_device *dev;
1155         int ret;
1156         unsigned int i;
1157
1158         dev_info(&client->dev, "ap1302 probe called.\n");
1159
1160         /* allocate device & init sub device */
1161         dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
1162         if (!dev) {
1163                 dev_err(&client->dev, "%s: out of memory\n", __func__);
1164                 return -ENOMEM;
1165         }
1166
1167         mutex_init(&dev->input_lock);
1168
1169         v4l2_i2c_subdev_init(&(dev->sd), client, &ap1302_ops);
1170
1171         ret = ap1302_request_firmware(&(dev->sd));
1172         if (ret) {
1173                 dev_err(&client->dev, "Cannot request ap1302 firmware.\n");
1174                 goto out_free;
1175         }
1176
1177         dev->regmap16 = devm_regmap_init_i2c(client, &ap1302_reg16_config);
1178         if (IS_ERR(dev->regmap16)) {
1179                 ret = PTR_ERR(dev->regmap16);
1180                 dev_err(&client->dev,
1181                         "Failed to allocate 16bit register map: %d\n", ret);
1182                 return ret;
1183         }
1184
1185         dev->regmap32 = devm_regmap_init_i2c(client, &ap1302_reg32_config);
1186         if (IS_ERR(dev->regmap32)) {
1187                 ret = PTR_ERR(dev->regmap32);
1188                 dev_err(&client->dev,
1189                         "Failed to allocate 32bit register map: %d\n", ret);
1190                 return ret;
1191         }
1192
1193         if (client->dev.platform_data) {
1194                 ret = ap1302_s_config(&dev->sd, client->dev.platform_data);
1195                 if (ret)
1196                         goto out_free;
1197         }
1198
1199         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1200         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1201         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1202
1203         dev->cntx_res[CONTEXT_PREVIEW].res_num = ARRAY_SIZE(ap1302_preview_res);
1204         dev->cntx_res[CONTEXT_PREVIEW].res_table = ap1302_preview_res;
1205         dev->cntx_res[CONTEXT_SNAPSHOT].res_num =
1206                 ARRAY_SIZE(ap1302_snapshot_res);
1207         dev->cntx_res[CONTEXT_SNAPSHOT].res_table = ap1302_snapshot_res;
1208         dev->cntx_res[CONTEXT_VIDEO].res_num = ARRAY_SIZE(ap1302_video_res);
1209         dev->cntx_res[CONTEXT_VIDEO].res_table = ap1302_video_res;
1210
1211         ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ctrls));
1212         if (ret) {
1213                 ap1302_remove(client);
1214                 return ret;
1215         }
1216
1217         for (i = 0; i < ARRAY_SIZE(ctrls); i++)
1218                 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ctrls[i], NULL);
1219
1220         if (dev->ctrl_handler.error) {
1221                 ap1302_remove(client);
1222                 return dev->ctrl_handler.error;
1223         }
1224
1225         /* Use same lock for controls as for everything else. */
1226         dev->ctrl_handler.lock = &dev->input_lock;
1227         dev->sd.ctrl_handler = &dev->ctrl_handler;
1228         v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1229
1230         dev->run_mode = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RUN_MODE);
1231         v4l2_ctrl_s_ctrl(dev->run_mode, ATOMISP_RUN_MODE_PREVIEW);
1232
1233         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1234         if (ret)
1235                 ap1302_remove(client);
1236         return ret;
1237 out_free:
1238         v4l2_device_unregister_subdev(&dev->sd);
1239         return ret;
1240 }
1241
1242 static const struct i2c_device_id ap1302_id[] = {
1243         {AP1302_NAME, 0},
1244         {}
1245 };
1246 MODULE_DEVICE_TABLE(i2c, ap1302_id);
1247
1248 static struct i2c_driver ap1302_driver = {
1249         .driver = {
1250                 .name = AP1302_NAME,
1251         },
1252         .probe = ap1302_probe,
1253         .remove = ap1302_remove,
1254         .id_table = ap1302_id,
1255 };
1256
1257 module_i2c_driver(ap1302_driver);
1258
1259 MODULE_AUTHOR("Tianshu Qiu <tian.shu.qiu@intel.com>");
1260 MODULE_DESCRIPTION("AP1302 Driver");
1261 MODULE_LICENSE("GPL");