]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/media/i2c/imx185.c
Remove unused variables and labels
[sojka/nv-tegra/linux-3.10.git] / drivers / media / i2c / imx185.c
1 /*
2  * imx185.c - imx185 sensor driver
3  *
4  * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/gpio.h>
22 #include <linux/module.h>
23
24 #include <linux/seq_file.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28
29 #include <media/camera_common.h>
30 #include <media/imx185.h>
31 #include "imx185_mode_tbls.h"
32
33
34 #define IMX185_DEFAULT_MODE     IMX185_MODE_1920X1080_CROP_HDR_30FPS
35 #define IMX185_DEFAULT_DATAFMT  V4L2_MBUS_FMT_SRGGB12_1X12
36
37 #define IMX185_MAX_COARSE_DIFF 2
38 #define IMX185_MAX_COARSE_DIFF_HDR 5
39 #define IMX185_GAIN_SHIFT               8
40 #define IMX185_MIN_GAIN         (1 << IMX185_GAIN_SHIFT)
41 #define IMX185_MAX_GAIN         (255 << IMX185_GAIN_SHIFT)
42 #define IMX185_MIN_FRAME_LENGTH (1125)
43 #define IMX185_MAX_FRAME_LENGTH (0xFFFF)
44 #define IMX185_MIN_EXPOSURE_COARSE      (0x0001)
45 #define IMX185_MAX_EXPOSURE_COARSE      \
46         (IMX185_MAX_FRAME_LENGTH-IMX185_MAX_COARSE_DIFF)
47
48 #define IMX185_MIN_FRAME_LENGTH_1080P_HDR       (1125)
49 #define IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS1       (5)
50 #define IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS1       (70)
51 #define IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS2       (80)
52 #define IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS2       (1120)
53
54
55 #define IMX185_DEFAULT_WIDTH    1920
56 #define IMX185_DEFAULT_HEIGHT   1080
57 #define IMX185_DEFAULT_GAIN             IMX185_MIN_GAIN
58 #define IMX185_DEFAULT_FRAME_LENGTH     (1125)
59 #define IMX185_DEFAULT_EXPOSURE_COARSE  \
60         (IMX185_DEFAULT_FRAME_LENGTH-IMX185_MAX_COARSE_DIFF)
61 #define IMX185_DEFAULT_EXPOSURE_COARSE_SHORT_HDR        \
62         (IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS1- \
63         IMX185_MAX_COARSE_DIFF_HDR)
64
65
66 #define IMX185_DEFAULT_CLK_FREQ 37125000
67
68
69 struct imx185 {
70         struct camera_common_power_rail power;
71         int                             num_ctrls;
72         struct v4l2_ctrl_handler        ctrl_handler;
73         struct i2c_client               *i2c_client;
74         struct v4l2_subdev              *subdev;
75         struct media_pad                pad;
76
77         s32 group_hold_prev;
78         bool group_hold_en;
79         u32     frame_length;
80
81         struct regmap                   *regmap;
82         struct camera_common_data       *s_data;
83         struct camera_common_pdata      *pdata;
84         struct v4l2_ctrl                *ctrls[];
85 };
86
87 static const struct regmap_config sensor_regmap_config = {
88         .reg_bits = 16,
89         .val_bits = 8,
90         .cache_type = REGCACHE_RBTREE,
91 };
92
93 static int imx185_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
94 static int imx185_s_ctrl(struct v4l2_ctrl *ctrl);
95
96 static const struct v4l2_ctrl_ops imx185_ctrl_ops = {
97         .g_volatile_ctrl = imx185_g_volatile_ctrl,
98         .s_ctrl = imx185_s_ctrl,
99 };
100
101 static struct v4l2_ctrl_config ctrl_config_list[] = {
102 /* Do not change the name field for the controls! */
103         {
104                 .ops = &imx185_ctrl_ops,
105                 .id = V4L2_CID_GAIN,
106                 .name = "Gain",
107                 .type = V4L2_CTRL_TYPE_INTEGER,
108                 .flags = V4L2_CTRL_FLAG_SLIDER,
109                 .min = IMX185_MIN_GAIN,
110                 .max = IMX185_MAX_GAIN,
111                 .def = IMX185_DEFAULT_GAIN,
112                 .step = 1,
113         },
114         {
115                 .ops = &imx185_ctrl_ops,
116                 .id = V4L2_CID_FRAME_LENGTH,
117                 .name = "Frame Length",
118                 .type = V4L2_CTRL_TYPE_INTEGER,
119                 .flags = V4L2_CTRL_FLAG_SLIDER,
120                 .min = IMX185_MIN_FRAME_LENGTH,
121                 .max = IMX185_MAX_FRAME_LENGTH,
122                 .def = IMX185_DEFAULT_FRAME_LENGTH,
123                 .step = 1,
124         },
125         {
126                 .ops = &imx185_ctrl_ops,
127                 .id = V4L2_CID_COARSE_TIME,
128                 .name = "Coarse Time",
129                 .type = V4L2_CTRL_TYPE_INTEGER,
130                 .flags = V4L2_CTRL_FLAG_SLIDER,
131                 .min = IMX185_MIN_EXPOSURE_COARSE,
132                 .max = IMX185_MAX_EXPOSURE_COARSE,
133                 .def = IMX185_DEFAULT_EXPOSURE_COARSE,
134                 .step = 1,
135         },
136         {
137                 .ops = &imx185_ctrl_ops,
138                 .id = V4L2_CID_COARSE_TIME_SHORT,
139                 .name = "Coarse Time Short",
140                 .type = V4L2_CTRL_TYPE_INTEGER,
141                 .flags = V4L2_CTRL_FLAG_SLIDER,
142                 .min = IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS1,
143                 .max = IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS1,
144                 .def = IMX185_DEFAULT_EXPOSURE_COARSE_SHORT_HDR,
145                 .step = 1,
146         },
147         {
148                 .ops = &imx185_ctrl_ops,
149                 .id = V4L2_CID_GROUP_HOLD,
150                 .name = "Group Hold",
151                 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
152                 .min = 0,
153                 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
154                 .menu_skip_mask = 0,
155                 .def = 0,
156                 .qmenu_int = switch_ctrl_qmenu,
157         },
158         {
159                 .ops = &imx185_ctrl_ops,
160                 .id = V4L2_CID_HDR_EN,
161                 .name = "HDR enable",
162                 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
163                 .min = 0,
164                 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
165                 .menu_skip_mask = 0,
166                 .def = 0,
167                 .qmenu_int = switch_ctrl_qmenu,
168         },
169         {
170                 .ops = &imx185_ctrl_ops,
171                 .id = V4L2_CID_FUSE_ID,
172                 .name = "Fuse ID",
173                 .type = V4L2_CTRL_TYPE_STRING,
174                 .flags = V4L2_CTRL_FLAG_READ_ONLY,
175                 .min = 0,
176                 .max = IMX185_FUSE_ID_STR_SIZE,
177                 .step = 2,
178         },
179 };
180
181 static inline void imx185_get_frame_length_regs(imx185_reg *regs,
182                                 u16 frame_length)
183 {
184         regs->addr = IMX185_FRAME_LENGTH_ADDR_MID;
185         regs->val = (frame_length >> 8) & 0xff;
186
187         (regs + 1)->addr = IMX185_FRAME_LENGTH_ADDR_LSB;
188         (regs + 1)->val = (frame_length) & 0xff;
189 }
190
191 static inline void imx185_get_coarse_time_regs_shs1(imx185_reg *regs,
192                                 u16 coarse_time)
193 {
194         regs->addr = IMX185_COARSE_TIME_SHS1_ADDR_MID;
195         regs->val = (coarse_time >> 8) & 0xff;
196
197         (regs + 1)->addr = IMX185_COARSE_TIME_SHS1_ADDR_LSB;
198         (regs + 1)->val = (coarse_time) & 0xff;
199 }
200
201 static inline void imx185_get_coarse_time_regs_shs2(imx185_reg *regs,
202                                 u16 coarse_time)
203 {
204         regs->addr = IMX185_COARSE_TIME_SHS2_ADDR_MID;
205         regs->val = (coarse_time >> 8) & 0xff;
206
207         (regs + 1)->addr = IMX185_COARSE_TIME_SHS2_ADDR_LSB;
208         (regs + 1)->val = (coarse_time) & 0xff;
209 }
210
211 static inline void imx185_get_gain_reg(imx185_reg *regs,
212                                 u8 gain)
213 {
214         regs->addr = IMX185_GAIN_ADDR;
215         regs->val = (gain) & 0xff;
216 }
217
218 static int test_mode;
219 module_param(test_mode, int, 0644);
220
221 static inline int imx185_read_reg(struct camera_common_data *s_data,
222                                 u16 addr, u8 *val)
223 {
224         struct imx185 *priv = (struct imx185 *)s_data->priv;
225         return regmap_read(priv->regmap, addr, (unsigned int *) val);
226 }
227
228 static int imx185_write_reg(struct camera_common_data *s_data,
229                                 u16 addr, u8 val)
230 {
231         int err;
232         struct imx185 *priv = (struct imx185 *)s_data->priv;
233
234         err = regmap_write(priv->regmap, addr, val);
235         if (err)
236                 pr_err("%s:i2c write failed, 0x%x = %x\n",
237                         __func__, addr, val);
238
239         return err;
240 }
241
242
243
244 static int imx185_write_table(struct imx185 *priv,
245                                 const imx185_reg table[])
246 {
247         return regmap_util_write_table_8(priv->regmap,
248                                          table,
249                                          NULL, 0,
250                                          IMX185_TABLE_WAIT_MS,
251                                          IMX185_TABLE_END);
252 }
253
254 static int imx185_power_on(struct camera_common_data *s_data)
255 {
256         int err = 0;
257         struct imx185 *priv = (struct imx185 *)s_data->priv;
258         struct camera_common_power_rail *pw = &priv->power;
259
260         dev_dbg(&priv->i2c_client->dev, "%s: power on\n", __func__);
261         if (priv->pdata && priv->pdata->power_on) {
262                 err = priv->pdata->power_on(pw);
263                 if (err) {
264                         pr_err("%s failed.\n", __func__);
265                         return err;
266                 }
267         }
268
269         /*exit reset mode: XCLR */
270         if (pw->reset_gpio) {
271                 gpio_set_value(pw->reset_gpio, 0);
272                 usleep_range(30, 50);
273                 gpio_set_value(pw->reset_gpio, 1);
274                 usleep_range(30, 50);
275         }
276
277         pw->state = SWITCH_ON;
278         return 0;
279
280 }
281
282 static int imx185_power_off(struct camera_common_data *s_data)
283 {
284         int err = 0;
285         struct imx185 *priv = (struct imx185 *)s_data->priv;
286         struct camera_common_power_rail *pw = &priv->power;
287
288         dev_dbg(&priv->i2c_client->dev, "%s: power off\n", __func__);
289         if (priv->pdata && priv->pdata->power_off) {
290                 err = priv->pdata->power_off(pw);
291                 if (err) {
292                         pr_err("%s failed.\n", __func__);
293                         return err;
294                 }
295         }
296         /* enter reset mode: XCLR */
297         usleep_range(1, 2);
298         if (pw->reset_gpio)
299                 gpio_set_value(pw->reset_gpio, 0);
300
301         pw->state = SWITCH_OFF;
302
303         return 0;
304 }
305
306 static int imx185_power_put(struct imx185 *priv)
307 {
308         struct camera_common_power_rail *pw = &priv->power;
309         if (unlikely(!pw))
310                 return -EFAULT;
311         return 0;
312 }
313
314 static int imx185_power_get(struct imx185 *priv)
315 {
316         struct camera_common_power_rail *pw = &priv->power;
317         struct camera_common_pdata *pdata = priv->pdata;
318         const char *mclk_name;
319         const char *parentclk_name;
320         struct clk *parent;
321         int err = 0;
322
323         mclk_name = priv->pdata->mclk_name ?
324                                 priv->pdata->mclk_name : "cam_mclk1";
325         pw->mclk = devm_clk_get(&priv->i2c_client->dev, mclk_name);
326         if (IS_ERR(pw->mclk)) {
327                 dev_err(&priv->i2c_client->dev,
328                         "unable to get clock %s\n", mclk_name);
329                 return PTR_ERR(pw->mclk);
330         }
331
332         parentclk_name = priv->pdata->parentclk_name;
333         if (parentclk_name) {
334                 parent = devm_clk_get(&priv->i2c_client->dev, parentclk_name);
335                 if (IS_ERR(parent)) {
336                         dev_err(&priv->i2c_client->dev,
337                                 "unable to get parent clcok %s",
338                                 parentclk_name);
339                 } else
340                         clk_set_parent(pw->mclk, parent);
341         }
342
343         pw->reset_gpio = pdata->reset_gpio;
344         pw->state = SWITCH_OFF;
345         return err;
346 }
347
348 static int imx185_set_gain(struct imx185 *priv, s32 val);
349 static int imx185_set_frame_length(struct imx185 *priv, s32 val);
350 static int imx185_set_coarse_time(struct imx185 *priv, s32 val);
351 static int imx185_set_coarse_time_shs1(struct imx185 *priv, s32 val);
352 static int imx185_set_coarse_time_hdr_shs2(struct imx185 *priv, s32 val);
353
354 static int imx185_s_stream(struct v4l2_subdev *sd, int enable)
355 {
356         struct i2c_client *client = v4l2_get_subdevdata(sd);
357         struct camera_common_data *s_data = to_camera_common_data(client);
358         struct imx185 *priv = (struct imx185 *)s_data->priv;
359         struct v4l2_control control;
360         int err;
361
362         dev_dbg(&client->dev, "%s++ enable %d\n", __func__, enable);
363
364         err =  imx185_write_table(priv, mode_table[IMX185_MODE_STOP_STREAM]);
365         if (err)
366                 goto exit;
367         if (!enable)
368                 return err;
369
370         err = imx185_write_table(priv, mode_table[s_data->mode]);
371         if (err)
372                 goto exit;
373         /* write list of override regs for the asking frame length, */
374         /* coarse integration time, and gain. Failures to write
375          * overrides are non-fatal */
376         control.id = V4L2_CID_GAIN;
377         err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
378         err |= imx185_set_gain(priv, control.value);
379         if (err)
380                 dev_dbg(&client->dev, "%s: warning gain override failed\n",
381                         __func__);
382
383         control.id = V4L2_CID_FRAME_LENGTH;
384         err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
385         err |= imx185_set_frame_length(priv, control.value);
386         if (err)
387                 dev_dbg(&client->dev,
388                         "%s: warning frame length override failed\n", __func__);
389
390         control.id = V4L2_CID_COARSE_TIME;
391         err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
392         err |= imx185_set_coarse_time(priv, control.value);
393         if (err)
394                 dev_dbg(&client->dev,
395                         "%s: warning coarse time override failed\n", __func__);
396
397         control.id = V4L2_CID_COARSE_TIME_SHORT;
398         err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
399         err |= imx185_set_coarse_time_shs1(priv, control.value);
400         if (err)
401                 dev_dbg(&client->dev,
402                         "%s: warning coarse time short override failed\n",
403                         __func__);
404         if (test_mode)
405                 err = imx185_write_table(priv,
406                         mode_table[IMX185_MODE_TEST_PATTERN]);
407
408         err = imx185_write_table(priv, mode_table[IMX185_MODE_START_STREAM]);
409         if (err)
410                 goto exit;
411
412         return 0;
413 exit:
414         dev_dbg(&client->dev, "%s: error setting stream\n", __func__);
415         return err;
416 }
417
418 static int imx185_g_input_status(struct v4l2_subdev *sd, u32 *status)
419 {
420         struct i2c_client *client = v4l2_get_subdevdata(sd);
421         struct camera_common_data *s_data = to_camera_common_data(client);
422         struct imx185 *priv = (struct imx185 *)s_data->priv;
423         struct camera_common_power_rail *pw = &priv->power;
424
425         *status = pw->state == SWITCH_ON;
426         return 0;
427 }
428
429 static struct v4l2_subdev_video_ops imx185_subdev_video_ops = {
430         .s_stream       = imx185_s_stream,
431         .s_mbus_fmt     = camera_common_s_fmt,
432         .g_mbus_fmt     = camera_common_g_fmt,
433         .try_mbus_fmt   = camera_common_try_fmt,
434         .enum_mbus_fmt  = camera_common_enum_fmt,
435         .g_mbus_config  = camera_common_g_mbus_config,
436         .g_input_status = imx185_g_input_status,
437         .enum_framesizes        = camera_common_enum_framesizes,
438         .enum_frameintervals    = camera_common_enum_frameintervals,
439 };
440
441 static struct v4l2_subdev_core_ops imx185_subdev_core_ops = {
442         .s_power        = camera_common_s_power,
443 };
444
445 static int imx185_get_fmt(struct v4l2_subdev *sd,
446                 struct v4l2_subdev_fh *fh,
447                 struct v4l2_subdev_format *format)
448 {
449         return camera_common_g_fmt(sd, &format->format);
450 }
451
452 static int imx185_set_fmt(struct v4l2_subdev *sd,
453                 struct v4l2_subdev_fh *fh,
454         struct v4l2_subdev_format *format)
455 {
456         int ret;
457
458         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
459                 ret = camera_common_try_fmt(sd, &format->format);
460         else
461                 ret = camera_common_s_fmt(sd, &format->format);
462
463         return ret;
464 }
465
466 static struct v4l2_subdev_pad_ops imx185_subdev_pad_ops = {
467         .enum_mbus_code = camera_common_enum_mbus_code,
468         .set_fmt = imx185_set_fmt,
469         .get_fmt = imx185_get_fmt,
470 };
471
472 static struct v4l2_subdev_ops imx185_subdev_ops = {
473         .core   = &imx185_subdev_core_ops,
474         .video  = &imx185_subdev_video_ops,
475         .pad = &imx185_subdev_pad_ops,
476 };
477
478 static struct of_device_id imx185_of_match[] = {
479         { .compatible = "nvidia,imx185", },
480         { },
481 };
482
483 static struct camera_common_sensor_ops imx185_common_ops = {
484         .power_on = imx185_power_on,
485         .power_off = imx185_power_off,
486         .write_reg = imx185_write_reg,
487         .read_reg = imx185_read_reg,
488 };
489
490 static int imx185_set_group_hold(struct imx185 *priv, s32 val)
491 {
492         int err;
493         int gh_en = switch_ctrl_qmenu[val];
494
495         priv->group_hold_prev = val;
496         if (gh_en == SWITCH_ON) {
497
498                 err = imx185_write_reg(priv->s_data,
499                                        IMX185_GROUP_HOLD_ADDR, 0x1);
500                 if (err)
501                         goto fail;
502         } else if (gh_en == SWITCH_OFF) {
503                 err = imx185_write_reg(priv->s_data,
504                                        IMX185_GROUP_HOLD_ADDR, 0x0);
505                 if (err)
506                         goto fail;
507         }
508         return 0;
509 fail:
510         dev_dbg(&priv->i2c_client->dev,
511                  "%s: Group hold control error\n", __func__);
512         return err;
513 }
514
515 static int imx185_set_gain(struct imx185 *priv, s32 val)
516 {
517         imx185_reg reg_list[1];
518         int err;
519         u8 gain;
520
521         /* translate value */
522         gain = (u8)(val * 160 / (1 << IMX185_GAIN_SHIFT) / 48);
523
524         dev_dbg(&priv->i2c_client->dev,
525                  "%s:  gain: %d\n", __func__, gain);
526
527         imx185_get_gain_reg(reg_list, gain);
528
529         err = imx185_write_table(priv, reg_list);
530         if (err)
531                 goto fail;
532
533         return 0;
534
535 fail:
536         dev_dbg(&priv->i2c_client->dev,
537                  "%s: GAIN control error\n", __func__);
538         return err;
539 }
540
541 static int imx185_set_frame_length(struct imx185 *priv, s32 val)
542 {
543         imx185_reg reg_list[2];
544         int err;
545         u16 frame_length;
546
547         frame_length = val;
548
549         dev_dbg(&priv->i2c_client->dev,
550                  "%s: val: %d\n", __func__, frame_length);
551
552         priv->frame_length = frame_length;
553         imx185_get_frame_length_regs(reg_list, frame_length);
554         err = imx185_write_table(priv, reg_list);
555         if (err)
556                 goto fail;
557
558         return 0;
559
560 fail:
561         dev_dbg(&priv->i2c_client->dev,
562                  "%s: FRAME_LENGTH control error\n", __func__);
563         return err;
564 }
565
566 static int imx185_set_coarse_time(struct imx185 *priv, s32 val)
567 {
568         int err;
569         struct v4l2_control control;
570         int hdr_en;
571
572         /* check hdr enable ctrl */
573         control.id = V4L2_CID_HDR_EN;
574         err = camera_common_g_ctrl(priv->s_data, &control);
575         if (err < 0) {
576                 dev_err(&priv->i2c_client->dev,
577                         "could not find device ctrl.\n");
578                 return err;
579         }
580
581         hdr_en = switch_ctrl_qmenu[control.value];
582         if (hdr_en == SWITCH_OFF) {
583                 /*no WDR, update SHS1 as ET*/
584                 err = imx185_set_coarse_time_shs1(priv, val);
585                 if (err)
586                         dev_dbg(&priv->i2c_client->dev,
587                         "%s: error coarse time SHS1 override\n", __func__);
588         } else if (hdr_en == SWITCH_ON) {
589                 /*WDR, update SHS2 as long ET*/
590                 err = imx185_set_coarse_time_hdr_shs2(priv, val);
591                 if (err)
592                         dev_dbg(&priv->i2c_client->dev,
593                         "%s: error coarse time SHS2 override\n", __func__);
594         }
595         return err;
596 }
597
598 static int imx185_set_coarse_time_shs1(struct imx185 *priv, s32 val)
599 {
600         imx185_reg reg_list[2];
601         int err;
602         u16 coarse_shs1;
603         struct v4l2_control control;
604         int hdr_en;
605
606         coarse_shs1 = val;
607
608         if (priv->frame_length == 0)
609                 priv->frame_length = IMX185_MIN_FRAME_LENGTH;
610
611         /* check hdr enable ctrl */
612         control.id = V4L2_CID_HDR_EN;
613         err = camera_common_g_ctrl(priv->s_data, &control);
614         if (err < 0) {
615                 dev_err(&priv->i2c_client->dev,
616                         "could not find device ctrl.\n");
617                 return err;
618         }
619
620         hdr_en = switch_ctrl_qmenu[control.value];
621         if (hdr_en == SWITCH_ON) {
622                 if (coarse_shs1 < IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS1)
623                         coarse_shs1 = IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS1;
624
625                 if (coarse_shs1 > IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS1)
626                         coarse_shs1 = IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS1;
627
628                 priv->frame_length = IMX185_MIN_FRAME_LENGTH;
629         }
630
631         dev_dbg(&priv->i2c_client->dev,
632                  "%s: val: %d,  shs1=%d, frame_length: %d\n", __func__,
633                  coarse_shs1,
634                  priv->frame_length - coarse_shs1 - 1,
635                  priv->frame_length);
636
637         imx185_get_coarse_time_regs_shs1(reg_list,
638                         priv->frame_length - coarse_shs1 - 1);
639
640         err = imx185_write_table(priv, reg_list);
641         if (err)
642                 goto fail;
643
644         return 0;
645
646 fail:
647         dev_dbg(&priv->i2c_client->dev,
648                  "%s: COARSE_TIME control error\n", __func__);
649         return err;
650 }
651
652 static int imx185_set_coarse_time_hdr_shs2(struct imx185 *priv, s32 val)
653 {
654         imx185_reg reg_list[2];
655         int err;
656         u16 coarse_shs2;
657
658         coarse_shs2 = val;
659         if (coarse_shs2 < IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS2)
660                 coarse_shs2 = IMX185_MIN_EXPOSURE_COARSE_1080P_HDR_SHS2;
661
662         if (coarse_shs2 > IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS2)
663                 coarse_shs2 = IMX185_MAX_EXPOSURE_COARSE_1080P_HDR_SHS2;
664
665         priv->frame_length = IMX185_MIN_FRAME_LENGTH;
666
667         dev_dbg(&priv->i2c_client->dev,
668                  "%s: val: %d,  shs2=%d, frame_length: %d\n", __func__,
669                  coarse_shs2,
670                  priv->frame_length - coarse_shs2 - 1,
671                  priv->frame_length);
672
673
674         imx185_get_coarse_time_regs_shs2(reg_list,
675                         priv->frame_length - coarse_shs2 - 1);
676
677         err = imx185_write_table(priv, reg_list);
678         if (err)
679                 goto fail;
680
681         return 0;
682
683 fail:
684         dev_dbg(&priv->i2c_client->dev,
685                  "%s: COARSE_TIME_SHORT control error\n", __func__);
686         return err;
687 }
688
689 static int imx185_fuse_id_setup(struct imx185 *priv)
690 {
691         int err;
692         int i;
693         struct i2c_client *client = v4l2_get_subdevdata(priv->subdev);
694         struct camera_common_data *s_data = to_camera_common_data(client);
695         struct camera_common_power_rail *pw = &priv->power;
696
697         struct v4l2_ctrl *ctrl;
698         u8 fuse_id[IMX185_FUSE_ID_SIZE];
699         u8 bak = 0;
700
701         err = camera_common_s_power(priv->subdev, true);
702         if (err)
703                 return -ENODEV;
704
705         for (i = 0; i < IMX185_FUSE_ID_SIZE; i++) {
706                 err |= imx185_read_reg(s_data,
707                         IMX185_FUSE_ID_ADDR + i, (unsigned int *) &bak);
708                 if (!err)
709                         fuse_id[i] = bak;
710                 else {
711                         pr_err("%s: can not read fuse id\n", __func__);
712                         return -EINVAL;
713                 }
714         }
715
716         ctrl = v4l2_ctrl_find(&priv->ctrl_handler, V4L2_CID_FUSE_ID);
717         if (!ctrl) {
718                 dev_err(&priv->i2c_client->dev,
719                         "could not find device ctrl.\n");
720                 return -EINVAL;
721         }
722
723         for (i = 0; i < IMX185_FUSE_ID_SIZE; i++)
724                 sprintf(&ctrl->string[i*2], "%02x",
725                         fuse_id[i]);
726         ctrl->cur.string = ctrl->string;
727         pr_info("%s,  fuse id: %s\n", __func__, ctrl->cur.string);
728
729         err = camera_common_s_power(priv->subdev, false);
730         if (err)
731                 return -ENODEV;
732
733         return 0;
734 }
735
736 static int imx185_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
737 {
738         struct imx185 *priv =
739                 container_of(ctrl->handler, struct imx185, ctrl_handler);
740         int err = 0;
741
742         if (priv->power.state == SWITCH_OFF)
743                 return 0;
744
745         switch (ctrl->id) {
746
747         default:
748                         pr_err("%s: unknown ctrl id.\n", __func__);
749                         return -EINVAL;
750         }
751
752         return err;
753 }
754
755 static int imx185_s_ctrl(struct v4l2_ctrl *ctrl)
756 {
757         struct imx185 *priv =
758                 container_of(ctrl->handler, struct imx185, ctrl_handler);
759         int err = 0;
760
761         if (priv->power.state == SWITCH_OFF)
762                 return 0;
763
764         switch (ctrl->id) {
765         case V4L2_CID_GAIN:
766                 err = imx185_set_gain(priv, ctrl->val);
767                 break;
768         case V4L2_CID_FRAME_LENGTH:
769                 err = imx185_set_frame_length(priv, ctrl->val);
770                 break;
771         case V4L2_CID_COARSE_TIME:
772                 err = imx185_set_coarse_time(priv, ctrl->val);
773                 break;
774         case V4L2_CID_COARSE_TIME_SHORT:
775                 err = imx185_set_coarse_time_shs1(priv, ctrl->val);
776                 break;
777         case V4L2_CID_GROUP_HOLD:
778                 err = imx185_set_group_hold(priv, ctrl->val);
779                 break;
780         case V4L2_CID_HDR_EN:
781
782                 break;
783         default:
784                 pr_err("%s: unknown ctrl id.\n", __func__);
785                 return -EINVAL;
786         }
787
788         return err;
789 }
790
791 static int imx185_ctrls_init(struct imx185 *priv)
792 {
793         struct i2c_client *client = priv->i2c_client;
794         struct v4l2_ctrl *ctrl;
795         int num_ctrls;
796         int err;
797         int i;
798
799         dev_dbg(&client->dev, "%s++\n", __func__);
800
801         num_ctrls = ARRAY_SIZE(ctrl_config_list);
802         v4l2_ctrl_handler_init(&priv->ctrl_handler, num_ctrls);
803
804         for (i = 0; i < num_ctrls; i++) {
805                 ctrl = v4l2_ctrl_new_custom(&priv->ctrl_handler,
806                         &ctrl_config_list[i], NULL);
807                 if (ctrl == NULL) {
808                         dev_err(&client->dev, "Failed to init %s ctrl\n",
809                                 ctrl_config_list[i].name);
810                         continue;
811                 }
812
813                 if (ctrl_config_list[i].type == V4L2_CTRL_TYPE_STRING &&
814                         ctrl_config_list[i].flags & V4L2_CTRL_FLAG_READ_ONLY) {
815                         ctrl->string = devm_kzalloc(&client->dev,
816                                 ctrl_config_list[i].max + 1, GFP_KERNEL);
817                         if (!ctrl->string) {
818                                 dev_err(&client->dev,
819                                         "Failed to allocate otp data\n");
820                                 return -ENOMEM;
821                         }
822                 }
823                 priv->ctrls[i] = ctrl;
824         }
825
826         priv->num_ctrls = num_ctrls;
827         priv->subdev->ctrl_handler = &priv->ctrl_handler;
828         if (priv->ctrl_handler.error) {
829                 dev_err(&client->dev, "Error %d adding controls\n",
830                         priv->ctrl_handler.error);
831                 err = priv->ctrl_handler.error;
832                 goto error;
833         }
834
835         err = v4l2_ctrl_handler_setup(&priv->ctrl_handler);
836         if (err) {
837                 dev_err(&client->dev,
838                         "Error %d setting default controls\n", err);
839                 goto error;
840         }
841
842         err = imx185_fuse_id_setup(priv);
843         if (err) {
844                 dev_err(&client->dev,
845                         "Error %d reading fuse id data\n", err);
846                 goto error;
847         }
848
849         return 0;
850
851 error:
852         v4l2_ctrl_handler_free(&priv->ctrl_handler);
853         return err;
854 }
855
856 MODULE_DEVICE_TABLE(of, imx185_of_match);
857
858 static struct camera_common_pdata *imx185_parse_dt(struct imx185 *priv,
859                                 struct i2c_client *client)
860 {
861         struct device_node *np = client->dev.of_node;
862         struct camera_common_pdata *board_priv_pdata;
863         const struct of_device_id *match;
864         int sts;
865
866         if (!np)
867                 return NULL;
868
869         match = of_match_device(imx185_of_match, &client->dev);
870         if (!match) {
871                 dev_err(&client->dev, "Failed to find matching dt id\n");
872                 return NULL;
873         }
874
875         board_priv_pdata = devm_kzalloc(&client->dev,
876                            sizeof(*board_priv_pdata), GFP_KERNEL);
877         if (!board_priv_pdata) {
878                 dev_err(&client->dev, "Failed to allocate pdata\n");
879                 return NULL;
880         }
881
882         sts = camera_common_parse_clocks(client, board_priv_pdata);
883         if (sts)
884                 dev_err(&client->dev, "Failed to find clocks\n");
885
886         sts = of_property_read_string(np, "mclk",
887                                       &board_priv_pdata->mclk_name);
888         if (sts)
889                 dev_err(&client->dev, "mclk not in DT\n");
890
891         board_priv_pdata->reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
892         if (sts) {
893                 dev_err(&client->dev, "reset-gpios not found %d\n", sts);
894                 board_priv_pdata->reset_gpio = 0;
895         }
896
897         return board_priv_pdata;
898 }
899
900 static int imx185_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
901 {
902         struct i2c_client *client = v4l2_get_subdevdata(sd);
903         dev_dbg(&client->dev, "%s:\n", __func__);
904
905         return 0;
906 }
907
908 static const struct v4l2_subdev_internal_ops imx185_subdev_internal_ops = {
909         .open = imx185_open,
910 };
911
912 static const struct media_entity_operations imx185_media_ops = {
913         .link_validate = v4l2_subdev_link_validate,
914 };
915
916 static int imx185_probe(struct i2c_client *client,
917                         const struct i2c_device_id *id)
918 {
919         struct camera_common_data *common_data;
920         struct imx185 *priv;
921         char debugfs_name[10];
922         int err;
923
924         pr_info("[IMX185]: probing v4l2 sensor at addr 0x%0x.\n", client->addr);
925
926         if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
927                 return -EINVAL;
928
929         common_data = devm_kzalloc(&client->dev,
930                             sizeof(struct camera_common_data), GFP_KERNEL);
931         if (!common_data) {
932                 dev_err(&client->dev, "unable to allocate memory!\n");
933                 return -ENOMEM;
934         }
935
936         priv = devm_kzalloc(&client->dev,
937                             sizeof(struct imx185) + sizeof(struct v4l2_ctrl *) *
938                             ARRAY_SIZE(ctrl_config_list),
939                             GFP_KERNEL);
940         if (!priv) {
941                 dev_err(&client->dev, "unable to allocate memory!\n");
942                 return -ENOMEM;
943         }
944
945         priv->regmap = devm_regmap_init_i2c(client, &sensor_regmap_config);
946         if (IS_ERR(priv->regmap)) {
947                 dev_err(&client->dev,
948                         "regmap init failed: %ld\n", PTR_ERR(priv->regmap));
949                 return -ENODEV;
950         }
951
952         if (client->dev.of_node)
953                 priv->pdata = imx185_parse_dt(priv, client);
954         if (!priv->pdata) {
955                 dev_err(&client->dev, "unable to get platform data\n");
956                 return -EFAULT;
957         }
958
959         common_data->ops = &imx185_common_ops;
960         common_data->ctrl_handler = &priv->ctrl_handler;
961         common_data->i2c_client = client;
962         common_data->frmfmt = &imx185_frmfmt[0];
963         common_data->colorfmt = camera_common_find_datafmt(
964                                           IMX185_DEFAULT_DATAFMT);
965         common_data->power = &priv->power;
966         common_data->ctrls = priv->ctrls;
967         common_data->priv = (void *)priv;
968         common_data->numctrls = ARRAY_SIZE(ctrl_config_list);
969         common_data->numfmts = ARRAY_SIZE(imx185_frmfmt);
970         common_data->def_mode = IMX185_DEFAULT_MODE;
971         common_data->def_width = IMX185_DEFAULT_WIDTH;
972         common_data->def_height = IMX185_DEFAULT_HEIGHT;
973         common_data->fmt_width = common_data->def_width;
974         common_data->fmt_height = common_data->def_height;
975         common_data->def_clk_freq = IMX185_DEFAULT_CLK_FREQ;
976
977         priv->i2c_client = client;
978         priv->s_data = common_data;
979         priv->subdev = &common_data->subdev;
980         priv->subdev->dev = &client->dev;
981         priv->s_data->dev = &client->dev;
982
983         err = imx185_power_get(priv);
984         if (err)
985                 return err;
986
987         err = camera_common_parse_ports(client, common_data);
988         if (err) {
989                 dev_err(&client->dev, "Failed to find port info\n");
990                 return err;
991         }
992         sprintf(debugfs_name, "imx185_%c", common_data->csi_port + 'a');
993         dev_dbg(&client->dev, "%s: name %s\n", __func__, debugfs_name);
994
995         camera_common_create_debugfs(common_data, debugfs_name);
996
997         v4l2_i2c_subdev_init(priv->subdev, client, &imx185_subdev_ops);
998
999         err = imx185_ctrls_init(priv);
1000         if (err)
1001                 return err;
1002
1003         priv->subdev->internal_ops = &imx185_subdev_internal_ops;
1004         priv->subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1005                      V4L2_SUBDEV_FL_HAS_EVENTS;
1006
1007 #if defined(CONFIG_MEDIA_CONTROLLER)
1008         priv->pad.flags = MEDIA_PAD_FL_SOURCE;
1009         priv->subdev->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1010         priv->subdev->entity.ops = &imx185_media_ops;
1011         err = media_entity_init(&priv->subdev->entity, 1, &priv->pad, 0);
1012         if (err < 0) {
1013                 dev_err(&client->dev, "unable to init media entity\n");
1014                 return err;
1015         }
1016 #endif
1017
1018         err = v4l2_async_register_subdev(priv->subdev);
1019         if (err)
1020                 return err;
1021
1022         dev_info(&client->dev, "Detected IMX185 sensor\n");
1023
1024         return 0;
1025 }
1026
1027 static int
1028 imx185_remove(struct i2c_client *client)
1029 {
1030         struct camera_common_data *s_data = to_camera_common_data(client);
1031         struct imx185 *priv = (struct imx185 *)s_data->priv;
1032
1033         v4l2_async_unregister_subdev(priv->subdev);
1034 #if defined(CONFIG_MEDIA_CONTROLLER)
1035         media_entity_cleanup(&priv->subdev->entity);
1036 #endif
1037
1038         v4l2_ctrl_handler_free(&priv->ctrl_handler);
1039         imx185_power_put(priv);
1040         camera_common_remove_debugfs(s_data);
1041
1042         return 0;
1043 }
1044
1045 static const struct i2c_device_id imx185_id[] = {
1046         { "imx185", 0 },
1047         { }
1048 };
1049
1050 MODULE_DEVICE_TABLE(i2c, imx185_id);
1051
1052 static struct i2c_driver imx185_i2c_driver = {
1053         .driver = {
1054                 .name = "imx185",
1055                 .owner = THIS_MODULE,
1056         },
1057         .probe = imx185_probe,
1058         .remove = imx185_remove,
1059         .id_table = imx185_id,
1060 };
1061
1062 module_i2c_driver(imx185_i2c_driver);
1063
1064 MODULE_DESCRIPTION("Media Controller driver for Sony IMX185");
1065 MODULE_AUTHOR("NVIDIA Corporation");
1066 MODULE_LICENSE("GPL v2");