]> rtime.felk.cvut.cz Git - hercules2020/nv-tegra/linux-4.4.git/blob - drivers/media/i2c/ov5693.c
WAR:media:i2c:ov5693: add flip and mirror setting
[hercules2020/nv-tegra/linux-4.4.git] / drivers / media / i2c / ov5693.c
1 /*
2  * ov5693_v4l2.c - ov5693 sensor driver
3  *
4  * Copyright (c) 2013-2017, 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/ov5693.h>
31
32 #include "../platform/tegra/camera/camera_gpio.h"
33
34 #include "ov5693_mode_tbls.h"
35
36 #define OV5693_MAX_COARSE_DIFF          6
37
38 #define OV5693_GAIN_SHIFT               8
39 #define OV5693_REAL_GAIN_SHIFT          4
40 #define OV5693_MIN_GAIN         (1 << OV5693_GAIN_SHIFT)
41 #define OV5693_MAX_GAIN         (16 << OV5693_GAIN_SHIFT)
42 #define OV5693_MAX_UNREAL_GAIN  (0x0F80)
43 #define OV5693_MIN_FRAME_LENGTH (0x0)
44 #define OV5693_MAX_FRAME_LENGTH (0x7fff)
45 #define OV5693_MIN_EXPOSURE_COARSE      (0x0002)
46 #define OV5693_MAX_EXPOSURE_COARSE      \
47         (OV5693_MAX_FRAME_LENGTH-OV5693_MAX_COARSE_DIFF)
48 #define OV5693_DEFAULT_LINE_LENGTH      (0xA80)
49 #define OV5693_DEFAULT_PIXEL_CLOCK      (160)
50
51 #define OV5693_DEFAULT_GAIN             OV5693_MIN_GAIN
52 #define OV5693_DEFAULT_FRAME_LENGTH     (0x07C0)
53 #define OV5693_DEFAULT_EXPOSURE_COARSE  \
54         (OV5693_DEFAULT_FRAME_LENGTH-OV5693_MAX_COARSE_DIFF)
55
56 #define OV5693_DEFAULT_MODE     OV5693_MODE_2592X1944
57 #define OV5693_DEFAULT_HDR_MODE OV5693_MODE_2592X1944_HDR
58 #define OV5693_DEFAULT_WIDTH    2592
59 #define OV5693_DEFAULT_HEIGHT   1944
60 #define OV5693_DEFAULT_DATAFMT  MEDIA_BUS_FMT_SRGGB10_1X10
61 #define OV5693_DEFAULT_CLK_FREQ 24000000
62
63 struct ov5693 {
64         struct camera_common_power_rail power;
65         int                             numctrls;
66         struct v4l2_ctrl_handler        ctrl_handler;
67         struct camera_common_eeprom_data eeprom[OV5693_EEPROM_NUM_BLOCKS];
68         u8                              eeprom_buf[OV5693_EEPROM_SIZE];
69         struct i2c_client               *i2c_client;
70         struct v4l2_subdev              *subdev;
71         struct media_pad                pad;
72
73         int                             reg_offset;
74
75         s32                             group_hold_prev;
76         u32                             frame_length;
77         bool                            group_hold_en;
78         struct regmap                   *regmap;
79         struct camera_common_data       *s_data;
80         struct camera_common_pdata      *pdata;
81         struct v4l2_ctrl                *ctrls[];
82 };
83
84 static struct regmap_config ov5693_regmap_config = {
85         .reg_bits = 16,
86         .val_bits = 8,
87 };
88
89 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
90 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl);
91 static void ov5693_update_ctrl_range(struct ov5693 *priv, s32 frame_length);
92
93 static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
94         .g_volatile_ctrl = ov5693_g_volatile_ctrl,
95         .s_ctrl         = ov5693_s_ctrl,
96 };
97
98 static struct v4l2_ctrl_config ctrl_config_list[] = {
99 /* Do not change the name field for the controls! */
100         {
101                 .ops = &ov5693_ctrl_ops,
102                 .id = V4L2_CID_GAIN,
103                 .name = "Gain",
104                 .type = V4L2_CTRL_TYPE_INTEGER,
105                 .flags = V4L2_CTRL_FLAG_SLIDER,
106                 .min = OV5693_MIN_GAIN,
107                 .max = OV5693_MAX_GAIN,
108                 .def = OV5693_DEFAULT_GAIN,
109                 .step = 1,
110         },
111         {
112                 .ops = &ov5693_ctrl_ops,
113                 .id = V4L2_CID_FRAME_LENGTH,
114                 .name = "Frame Length",
115                 .type = V4L2_CTRL_TYPE_INTEGER,
116                 .flags = V4L2_CTRL_FLAG_SLIDER,
117                 .min = OV5693_MIN_FRAME_LENGTH,
118                 .max = OV5693_MAX_FRAME_LENGTH,
119                 .def = OV5693_DEFAULT_FRAME_LENGTH,
120                 .step = 1,
121         },
122         {
123                 .ops = &ov5693_ctrl_ops,
124                 .id = V4L2_CID_COARSE_TIME,
125                 .name = "Coarse Time",
126                 .type = V4L2_CTRL_TYPE_INTEGER,
127                 .flags = V4L2_CTRL_FLAG_SLIDER,
128                 .min = OV5693_MIN_EXPOSURE_COARSE,
129                 .max = OV5693_MAX_EXPOSURE_COARSE,
130                 .def = OV5693_DEFAULT_EXPOSURE_COARSE,
131                 .step = 1,
132         },
133         {
134                 .ops = &ov5693_ctrl_ops,
135                 .id = V4L2_CID_COARSE_TIME_SHORT,
136                 .name = "Coarse Time Short",
137                 .type = V4L2_CTRL_TYPE_INTEGER,
138                 .flags = V4L2_CTRL_FLAG_SLIDER,
139                 .min = OV5693_MIN_EXPOSURE_COARSE,
140                 .max = OV5693_MAX_EXPOSURE_COARSE,
141                 .def = OV5693_DEFAULT_EXPOSURE_COARSE,
142                 .step = 1,
143         },
144         {
145                 .ops = &ov5693_ctrl_ops,
146                 .id = V4L2_CID_GROUP_HOLD,
147                 .name = "Group Hold",
148                 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
149                 .min = 0,
150                 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
151                 .menu_skip_mask = 0,
152                 .def = 0,
153                 .qmenu_int = switch_ctrl_qmenu,
154         },
155         {
156                 .ops = &ov5693_ctrl_ops,
157                 .id = V4L2_CID_HDR_EN,
158                 .name = "HDR enable",
159                 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
160                 .min = 0,
161                 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
162                 .menu_skip_mask = 0,
163                 .def = 0,
164                 .qmenu_int = switch_ctrl_qmenu,
165         },
166         {
167                 .ops = &ov5693_ctrl_ops,
168                 .id = V4L2_CID_EEPROM_DATA,
169                 .name = "EEPROM Data",
170                 .type = V4L2_CTRL_TYPE_STRING,
171                 .flags = V4L2_CTRL_FLAG_VOLATILE,
172                 .min = 0,
173                 .max = OV5693_EEPROM_STR_SIZE,
174                 .step = 2,
175         },
176         {
177                 .ops = &ov5693_ctrl_ops,
178                 .id = V4L2_CID_OTP_DATA,
179                 .name = "OTP Data",
180                 .type = V4L2_CTRL_TYPE_STRING,
181                 .flags = V4L2_CTRL_FLAG_READ_ONLY,
182                 .min = 0,
183                 .max = OV5693_OTP_STR_SIZE,
184                 .step = 2,
185         },
186         {
187                 .ops = &ov5693_ctrl_ops,
188                 .id = V4L2_CID_FUSE_ID,
189                 .name = "Fuse ID",
190                 .type = V4L2_CTRL_TYPE_STRING,
191                 .flags = V4L2_CTRL_FLAG_READ_ONLY,
192                 .min = 0,
193                 .max = OV5693_FUSE_ID_STR_SIZE,
194                 .step = 2,
195         },
196 };
197
198 static inline void ov5693_get_frame_length_regs(ov5693_reg *regs,
199                                 u32 frame_length)
200 {
201         regs->addr = OV5693_FRAME_LENGTH_ADDR_MSB;
202         regs->val = (frame_length >> 8) & 0xff;
203         (regs + 1)->addr = OV5693_FRAME_LENGTH_ADDR_LSB;
204         (regs + 1)->val = (frame_length) & 0xff;
205 }
206
207
208 static inline void ov5693_get_coarse_time_regs(ov5693_reg *regs,
209                                 u32 coarse_time)
210 {
211         regs->addr = OV5693_COARSE_TIME_ADDR_1;
212         regs->val = (coarse_time >> 12) & 0xff;
213         (regs + 1)->addr = OV5693_COARSE_TIME_ADDR_2;
214         (regs + 1)->val = (coarse_time >> 4) & 0xff;
215         (regs + 2)->addr = OV5693_COARSE_TIME_ADDR_3;
216         (regs + 2)->val = (coarse_time & 0xf) << 4;
217 }
218
219 static inline void ov5693_get_coarse_time_short_regs(ov5693_reg *regs,
220                                 u32 coarse_time)
221 {
222         regs->addr = OV5693_COARSE_TIME_SHORT_ADDR_1;
223         regs->val = (coarse_time >> 12) & 0xff;
224         (regs + 1)->addr = OV5693_COARSE_TIME_SHORT_ADDR_2;
225         (regs + 1)->val = (coarse_time >> 4) & 0xff;
226         (regs + 2)->addr = OV5693_COARSE_TIME_SHORT_ADDR_3;
227         (regs + 2)->val = (coarse_time & 0xf) << 4;
228 }
229
230 static inline void ov5693_get_gain_regs(ov5693_reg *regs,
231                                 u16 gain)
232 {
233         regs->addr = OV5693_GAIN_ADDR_MSB;
234         regs->val = (gain >> 8) & 0xff;
235
236         (regs + 1)->addr = OV5693_GAIN_ADDR_LSB;
237         (regs + 1)->val = (gain) & 0xff;
238 }
239
240 static int test_mode;
241 module_param(test_mode, int, 0644);
242
243 static inline int ov5693_read_reg(struct camera_common_data *s_data,
244                                 u16 addr, u8 *val)
245 {
246         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
247         int err = 0;
248         u32 reg_val = 0;
249
250         err = regmap_read(priv->regmap, addr, &reg_val);
251         *val = reg_val & 0xFF;
252
253         return err;
254 }
255
256 static int ov5693_write_reg(struct camera_common_data *s_data, u16 addr, u8 val)
257 {
258         int err;
259         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
260
261         err = regmap_write(priv->regmap, addr, val);
262         if (err)
263                 pr_err("%s:i2c write failed, %x = %x\n",
264                         __func__, addr, val);
265
266         return err;
267 }
268
269 static int ov5693_write_table(struct ov5693 *priv,
270                               const ov5693_reg table[])
271 {
272         return regmap_util_write_table_8(priv->regmap,
273                                          table,
274                                          NULL, 0,
275                                          OV5693_TABLE_WAIT_MS,
276                                          OV5693_TABLE_END);
277 }
278
279 static void ov5693_gpio_set(struct ov5693 *priv,
280                             unsigned int gpio, int val)
281 {
282         if (priv->pdata && priv->pdata->use_cam_gpio)
283                 cam_gpio_ctrl(priv->i2c_client, gpio, val, 1);
284         else {
285                 if (gpio_cansleep(gpio))
286                         gpio_set_value_cansleep(gpio, val);
287                 else
288                         gpio_set_value(gpio, val);
289         }
290 }
291
292 static int ov5693_power_on(struct camera_common_data *s_data)
293 {
294         int err = 0;
295         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
296         struct camera_common_power_rail *pw = &priv->power;
297
298         dev_dbg(&priv->i2c_client->dev, "%s: power on\n", __func__);
299
300         if (priv->pdata && priv->pdata->power_on) {
301                 err = priv->pdata->power_on(pw);
302                 if (err)
303                         pr_err("%s failed.\n", __func__);
304                 else
305                         pw->state = SWITCH_ON;
306                 return err;
307         }
308
309         /* sleeps calls in the sequence below are for internal device
310          * signal propagation as specified by sensor vendor */
311
312         if (pw->avdd)
313                 err = regulator_enable(pw->avdd);
314         if (err)
315                 goto ov5693_avdd_fail;
316
317         if (pw->iovdd)
318                 err = regulator_enable(pw->iovdd);
319         if (err)
320                 goto ov5693_iovdd_fail;
321
322         usleep_range(1, 2);
323         if (pw->pwdn_gpio)
324                 ov5693_gpio_set(priv, pw->pwdn_gpio, 1);
325
326         /*
327          * datasheet 2.9: reset requires ~2ms settling time
328          * a power on reset is generated after core power becomes stable
329          */
330         usleep_range(2000, 2010);
331
332         if (pw->reset_gpio)
333                 ov5693_gpio_set(priv, pw->reset_gpio, 1);
334
335         /* datasheet fig 2-9: t3 */
336         usleep_range(2000, 2010);
337
338         pw->state = SWITCH_ON;
339         ov5693_write_reg(s_data, 0x0100, 0x1);
340         ov5693_write_reg(s_data, 0x0100, 0x0);
341         return 0;
342
343 ov5693_iovdd_fail:
344         regulator_disable(pw->avdd);
345
346 ov5693_avdd_fail:
347         pr_err("%s failed.\n", __func__);
348         return -ENODEV;
349 }
350
351 static int ov5693_power_off(struct camera_common_data *s_data)
352 {
353         int err = 0;
354         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
355         struct camera_common_power_rail *pw = &priv->power;
356
357         dev_dbg(&priv->i2c_client->dev, "%s: power off\n", __func__);
358
359         if (priv->pdata && priv->pdata->power_off) {
360                 err = priv->pdata->power_off(pw);
361                 if (!err) {
362                         goto power_off_done;
363                 } else {
364                         pr_err("%s failed.\n", __func__);
365                         return err;
366                 }
367         }
368
369         /* sleeps calls in the sequence below are for internal device
370          * signal propagation as specified by sensor vendor */
371
372         usleep_range(21, 25);
373         if (pw->pwdn_gpio)
374                 ov5693_gpio_set(priv, pw->pwdn_gpio, 0);
375         usleep_range(1, 2);
376         if (pw->reset_gpio)
377                 ov5693_gpio_set(priv, pw->reset_gpio, 0);
378
379         /* datasheet 2.9: reset requires ~2ms settling time*/
380         usleep_range(2000, 2010);
381
382         if (pw->iovdd)
383                 regulator_disable(pw->iovdd);
384         if (pw->avdd)
385                 regulator_disable(pw->avdd);
386
387 power_off_done:
388         pw->state = SWITCH_OFF;
389         return 0;
390 }
391
392 static int ov5693_power_put(struct ov5693 *priv)
393 {
394         struct camera_common_power_rail *pw = &priv->power;
395
396         if (unlikely(!pw))
397                 return -EFAULT;
398
399         if (likely(pw->avdd))
400                 regulator_put(pw->avdd);
401
402         if (likely(pw->iovdd))
403                 regulator_put(pw->iovdd);
404
405         pw->avdd = NULL;
406         pw->iovdd = NULL;
407
408         if (priv->pdata && priv->pdata->use_cam_gpio)
409                 cam_gpio_deregister(priv->i2c_client, pw->pwdn_gpio);
410         else {
411                 gpio_free(pw->pwdn_gpio);
412                 gpio_free(pw->reset_gpio);
413         }
414
415         return 0;
416 }
417
418 static int ov5693_power_get(struct ov5693 *priv)
419 {
420         struct camera_common_power_rail *pw = &priv->power;
421         struct camera_common_pdata *pdata = priv->pdata;
422         const char *mclk_name;
423         const char *parentclk_name;
424         struct clk *parent;
425         int err = 0, ret = 0;
426
427         if (!pdata) {
428                 dev_err(&priv->i2c_client->dev, "pdata missing\n");
429                 return -EFAULT;
430         }
431
432         mclk_name = pdata->mclk_name ?
433                     pdata->mclk_name : "cam_mclk1";
434         pw->mclk = devm_clk_get(&priv->i2c_client->dev, mclk_name);
435         if (IS_ERR(pw->mclk)) {
436                 dev_err(&priv->i2c_client->dev,
437                         "unable to get clock %s\n", mclk_name);
438                 return PTR_ERR(pw->mclk);
439         }
440
441         parentclk_name = pdata->parentclk_name;
442         if (parentclk_name) {
443                 parent = devm_clk_get(&priv->i2c_client->dev, parentclk_name);
444                 if (IS_ERR(parent)) {
445                         dev_err(&priv->i2c_client->dev,
446                                 "unable to get parent clcok %s",
447                                 parentclk_name);
448                 } else
449                         clk_set_parent(pw->mclk, parent);
450         }
451
452
453         /* analog 2.8v */
454         err |= camera_common_regulator_get(priv->i2c_client,
455                         &pw->avdd, pdata->regulators.avdd);
456         /* IO 1.8v */
457         err |= camera_common_regulator_get(priv->i2c_client,
458                         &pw->iovdd, pdata->regulators.iovdd);
459
460         if (!err) {
461                 pw->reset_gpio = pdata->reset_gpio;
462                 pw->pwdn_gpio = pdata->pwdn_gpio;
463         }
464
465         if (pdata->use_cam_gpio) {
466                 err = cam_gpio_register(priv->i2c_client, pw->pwdn_gpio);
467                 if (err)
468                         dev_err(&priv->i2c_client->dev,
469                                 "%s ERR can't register cam gpio %u!\n",
470                                  __func__, pw->pwdn_gpio);
471         } else {
472                 ret = gpio_request(pw->pwdn_gpio, "cam_pwdn_gpio");
473                 if (ret < 0)
474                         dev_dbg(&priv->i2c_client->dev,
475                                 "%s can't request pwdn_gpio %d\n",
476                                 __func__, ret);
477                 ret = gpio_request(pw->reset_gpio, "cam_reset_gpio");
478                 if (ret < 0)
479                         dev_dbg(&priv->i2c_client->dev,
480                                 "%s can't request reset_gpio %d\n",
481                                 __func__, ret);
482         }
483
484
485         pw->state = SWITCH_OFF;
486         return err;
487 }
488
489 static int ov5693_set_gain(struct ov5693 *priv, s32 val);
490 static int ov5693_set_frame_length(struct ov5693 *priv, s32 val);
491 static int ov5693_set_coarse_time(struct ov5693 *priv, s32 val);
492 static int ov5693_set_coarse_time_short(struct ov5693 *priv, s32 val);
493
494 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
495 {
496         struct i2c_client *client = v4l2_get_subdevdata(sd);
497         struct camera_common_data *s_data = to_camera_common_data(client);
498         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
499         struct v4l2_control control;
500         int err;
501         u32 frame_time;
502         u8 val;
503
504         dev_dbg(&client->dev, "%s++\n", __func__);
505
506         if (!enable) {
507                 ov5693_update_ctrl_range(priv, OV5693_MAX_FRAME_LENGTH);
508
509                 err = ov5693_write_table(priv,
510                         mode_table[OV5693_MODE_STOP_STREAM]);
511                 if (err)
512                         return err;
513
514                 /*
515                  * Wait for one frame to make sure sensor is set to
516                  * software standby in V-blank
517                  *
518                  * frame_time = frame length rows * Tline
519                  * Tline = line length / pixel clock (in MHz)
520                  */
521                 frame_time = priv->frame_length *
522                         OV5693_DEFAULT_LINE_LENGTH / OV5693_DEFAULT_PIXEL_CLOCK;
523
524                 usleep_range(frame_time, frame_time + 1000);
525                 return 0;
526         }
527
528         err = ov5693_write_table(priv, mode_table[s_data->mode]);
529         if (err)
530                 goto exit;
531
532
533         if (s_data->override_enable) {
534                 /*
535                  * write list of override regs for the asking frame length,
536                  * coarse integration time, and gain. Failures to write
537                  * overrides are non-fatal
538                  */
539                 control.id = V4L2_CID_GAIN;
540                 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
541                 err |= ov5693_set_gain(priv, control.value);
542                 if (err)
543                         dev_dbg(&client->dev, "%s: warning gain override failed\n",
544                                 __func__);
545
546                 control.id = V4L2_CID_FRAME_LENGTH;
547                 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
548                 err |= ov5693_set_frame_length(priv, control.value);
549                 if (err)
550                         dev_dbg(&client->dev,
551                                 "%s: warning frame length override failed\n",
552                                 __func__);
553
554                 control.id = V4L2_CID_COARSE_TIME;
555                 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
556                 err |= ov5693_set_coarse_time(priv, control.value);
557                 if (err)
558                         dev_dbg(&client->dev,
559                                 "%s: warning coarse time override failed\n",
560                                 __func__);
561
562                 control.id = V4L2_CID_COARSE_TIME_SHORT;
563                 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
564                 err |= ov5693_set_coarse_time_short(priv, control.value);
565                 if (err)
566                         dev_dbg(&client->dev,
567                                 "%s: warning coarse time short override failed\n",
568                                 __func__);
569         }
570
571         err = ov5693_write_table(priv, mode_table[OV5693_MODE_START_STREAM]);
572         if (err)
573                 goto exit;
574         if (priv->pdata->v_flip) {
575                 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG20, &val);
576                 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG20,
577                                  val | VERTICAL_FLIP);
578         }
579         if (priv->pdata->h_mirror) {
580                 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG21, &val);
581                 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG21,
582                                  val | HORIZONTAL_MIRROR_MASK);
583         } else {
584                 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG21, &val);
585                 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG21,
586                                  val & (~HORIZONTAL_MIRROR_MASK));
587         }
588         if (test_mode)
589                 err = ov5693_write_table(priv,
590                         mode_table[OV5693_MODE_TEST_PATTERN]);
591
592         dev_dbg(&client->dev, "%s--\n", __func__);
593         return 0;
594 exit:
595         dev_dbg(&client->dev, "%s: error setting stream\n", __func__);
596         return err;
597 }
598
599 static int ov5693_g_input_status(struct v4l2_subdev *sd, u32 *status)
600 {
601         struct i2c_client *client = v4l2_get_subdevdata(sd);
602         struct camera_common_data *s_data = to_camera_common_data(client);
603         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
604         struct camera_common_power_rail *pw = &priv->power;
605
606         *status = pw->state == SWITCH_ON;
607         return 0;
608 }
609
610 static struct v4l2_subdev_video_ops ov5693_subdev_video_ops = {
611         .s_stream       = ov5693_s_stream,
612         .g_mbus_config  = camera_common_g_mbus_config,
613         .g_input_status = ov5693_g_input_status,
614 };
615
616 static struct v4l2_subdev_core_ops ov5693_subdev_core_ops = {
617         .s_power        = camera_common_s_power,
618 };
619
620 static int ov5693_get_fmt(struct v4l2_subdev *sd,
621                 struct v4l2_subdev_pad_config *cfg,
622                 struct v4l2_subdev_format *format)
623 {
624         return camera_common_g_fmt(sd, &format->format);
625 }
626
627 static int ov5693_set_fmt(struct v4l2_subdev *sd,
628                 struct v4l2_subdev_pad_config *cfg,
629                 struct v4l2_subdev_format *format)
630 {
631         int ret;
632
633         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
634                 ret = camera_common_try_fmt(sd, &format->format);
635         else
636                 ret = camera_common_s_fmt(sd, &format->format);
637
638         return ret;
639 }
640
641 static struct v4l2_subdev_pad_ops ov5693_subdev_pad_ops = {
642         .set_fmt = ov5693_set_fmt,
643         .get_fmt = ov5693_get_fmt,
644         .enum_mbus_code = camera_common_enum_mbus_code,
645         .enum_frame_size        = camera_common_enum_framesizes,
646         .enum_frame_interval    = camera_common_enum_frameintervals,
647 };
648
649 static struct v4l2_subdev_ops ov5693_subdev_ops = {
650         .core   = &ov5693_subdev_core_ops,
651         .video  = &ov5693_subdev_video_ops,
652         .pad    = &ov5693_subdev_pad_ops,
653 };
654
655 static struct of_device_id ov5693_of_match[] = {
656         { .compatible = "nvidia,ov5693", },
657         { },
658 };
659
660 static struct camera_common_sensor_ops ov5693_common_ops = {
661         .power_on = ov5693_power_on,
662         .power_off = ov5693_power_off,
663         .write_reg = ov5693_write_reg,
664         .read_reg = ov5693_read_reg,
665 };
666
667 static int ov5693_set_group_hold(struct ov5693 *priv)
668 {
669         int err;
670         int gh_prev = switch_ctrl_qmenu[priv->group_hold_prev];
671
672         if (priv->group_hold_en == true && gh_prev == SWITCH_OFF) {
673                 /* enter group hold */
674                 err = ov5693_write_reg(priv->s_data,
675                                        OV5693_GROUP_HOLD_ADDR, 0x01);
676                 if (err)
677                         goto fail;
678
679                 priv->group_hold_prev = 1;
680
681                 dev_dbg(&priv->i2c_client->dev,
682                          "%s: enter group hold\n", __func__);
683         } else if (priv->group_hold_en == false && gh_prev == SWITCH_ON) {
684                 /* leave group hold */
685                 err = ov5693_write_reg(priv->s_data,
686                                        OV5693_GROUP_HOLD_ADDR, 0x11);
687                 if (err)
688                         goto fail;
689
690                 err = ov5693_write_reg(priv->s_data,
691                                        OV5693_GROUP_HOLD_ADDR, 0x61);
692                 if (err)
693                         goto fail;
694
695                 priv->group_hold_prev = 0;
696
697                 dev_dbg(&priv->i2c_client->dev,
698                          "%s: leave group hold\n", __func__);
699         }
700
701         return 0;
702
703 fail:
704         dev_dbg(&priv->i2c_client->dev,
705                  "%s: Group hold control error\n", __func__);
706         return err;
707 }
708
709 static u16 ov5693_to_real_gain(u32 rep, int shift)
710 {
711         u16 gain;
712         int gain_int;
713         int gain_dec;
714         int min_int = (1 << shift);
715
716         if (rep < OV5693_MIN_GAIN)
717                 rep = OV5693_MIN_GAIN;
718         else if (rep > OV5693_MAX_GAIN)
719                 rep = OV5693_MAX_GAIN;
720
721         gain_int = (int)(rep >> shift);
722         gain_dec = (int)(rep & ~(0xffff << shift));
723
724         /* derived from formulat gain = (x * 16 + 0.5) */
725         gain = ((gain_int * min_int + gain_dec) * 32 + min_int) / (2 * min_int);
726
727         return gain;
728 }
729
730 static int ov5693_set_gain(struct ov5693 *priv, s32 val)
731 {
732         ov5693_reg reg_list[2];
733         int err;
734         u16 gain;
735         int i;
736
737         if (!priv->group_hold_prev)
738                 ov5693_set_group_hold(priv);
739
740         /* translate value */
741         gain = ov5693_to_real_gain((u32)val, OV5693_GAIN_SHIFT);
742
743         ov5693_get_gain_regs(reg_list, gain);
744         dev_dbg(&priv->i2c_client->dev,
745                  "%s: gain %04x val: %04x\n", __func__, val, gain);
746
747         for (i = 0; i < 2; i++) {
748                 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
749                          reg_list[i].val);
750                 if (err)
751                         goto fail;
752         }
753
754         return 0;
755
756 fail:
757         dev_dbg(&priv->i2c_client->dev,
758                  "%s: GAIN control error\n", __func__);
759         return err;
760 }
761
762 static void ov5693_update_ctrl_range(struct ov5693 *priv, s32 frame_length)
763 {
764         struct v4l2_ctrl *ctrl = NULL;
765         int ctrl_ids[2] = {V4L2_CID_COARSE_TIME,
766                         V4L2_CID_COARSE_TIME_SHORT};
767         s32 max, min, def;
768         int i, j;
769
770         for (i = 0; i < ARRAY_SIZE(ctrl_ids); i++) {
771                 for (j = 0; j < priv->numctrls; j++) {
772                         if (priv->ctrls[j]->id == ctrl_ids[i]) {
773                                 ctrl = priv->ctrls[j];
774                                 break;
775                         }
776                 }
777
778                 if (j == priv->numctrls) {
779                         dev_err(&priv->i2c_client->dev,
780                                 "could not find ctrl %x\n",
781                                 ctrl_ids[i]);
782                         continue;
783                 }
784
785                 max = frame_length - OV5693_MAX_COARSE_DIFF;
786                 /* clamp the value in case above is negative */
787                 max = clamp_val(max, OV5693_MIN_EXPOSURE_COARSE,
788                         OV5693_MAX_EXPOSURE_COARSE);
789                 min = OV5693_MIN_EXPOSURE_COARSE;
790                 def = clamp_val(OV5693_DEFAULT_EXPOSURE_COARSE, min, max);
791                 if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def))
792                         dev_err(&priv->i2c_client->dev,
793                                 "ctrl %x: range update failed\n",
794                                 ctrl_ids[i]);
795         }
796
797 }
798
799 static int ov5693_set_frame_length(struct ov5693 *priv, s32 val)
800 {
801         ov5693_reg reg_list[2];
802         int err;
803         u32 frame_length;
804         int i;
805
806         if (!priv->group_hold_prev)
807                 ov5693_set_group_hold(priv);
808
809         frame_length = (u32)val;
810
811         ov5693_get_frame_length_regs(reg_list, frame_length);
812         dev_dbg(&priv->i2c_client->dev,
813                  "%s: val: %d\n", __func__, frame_length);
814
815         for (i = 0; i < 2; i++) {
816                 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
817                          reg_list[i].val);
818                 if (err)
819                         goto fail;
820         }
821
822         priv->frame_length = frame_length;
823
824         ov5693_update_ctrl_range(priv, val);
825         return 0;
826
827 fail:
828         dev_dbg(&priv->i2c_client->dev,
829                  "%s: FRAME_LENGTH control error\n", __func__);
830         return err;
831 }
832
833 static int ov5693_set_coarse_time(struct ov5693 *priv, s32 val)
834 {
835         ov5693_reg reg_list[3];
836         int err;
837         u32 coarse_time;
838         int i;
839
840         if (!priv->group_hold_prev)
841                 ov5693_set_group_hold(priv);
842
843         coarse_time = (u32)val;
844
845         ov5693_get_coarse_time_regs(reg_list, coarse_time);
846         dev_dbg(&priv->i2c_client->dev,
847                  "%s: val: %d\n", __func__, coarse_time);
848
849         for (i = 0; i < 3; i++) {
850                 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
851                          reg_list[i].val);
852                 if (err)
853                         goto fail;
854         }
855
856         return 0;
857
858 fail:
859         dev_dbg(&priv->i2c_client->dev,
860                  "%s: COARSE_TIME control error\n", __func__);
861         return err;
862 }
863
864 static int ov5693_set_coarse_time_short(struct ov5693 *priv, s32 val)
865 {
866         ov5693_reg reg_list[3];
867         int err;
868         struct v4l2_control hdr_control;
869         int hdr_en;
870         u32 coarse_time_short;
871         int i;
872
873         if (!priv->group_hold_prev)
874                 ov5693_set_group_hold(priv);
875
876         /* check hdr enable ctrl */
877         hdr_control.id = V4L2_CID_HDR_EN;
878
879         err = camera_common_g_ctrl(priv->s_data, &hdr_control);
880         if (err < 0) {
881                 dev_err(&priv->i2c_client->dev,
882                         "could not find device ctrl.\n");
883                 return err;
884         }
885
886         hdr_en = switch_ctrl_qmenu[hdr_control.value];
887         if (hdr_en == SWITCH_OFF)
888                 return 0;
889
890         coarse_time_short = (u32)val;
891
892         ov5693_get_coarse_time_short_regs(reg_list, coarse_time_short);
893         dev_dbg(&priv->i2c_client->dev,
894                  "%s: val: %d\n", __func__, coarse_time_short);
895
896         for (i = 0; i < 3; i++) {
897                 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
898                          reg_list[i].val);
899                 if (err)
900                         goto fail;
901         }
902
903         return 0;
904
905 fail:
906         dev_dbg(&priv->i2c_client->dev,
907                  "%s: COARSE_TIME_SHORT control error\n", __func__);
908         return err;
909 }
910
911 static int ov5693_eeprom_device_release(struct ov5693 *priv)
912 {
913         int i;
914
915         for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
916                 if (priv->eeprom[i].i2c_client != NULL) {
917                         i2c_unregister_device(priv->eeprom[i].i2c_client);
918                         priv->eeprom[i].i2c_client = NULL;
919                 }
920         }
921
922         return 0;
923 }
924
925 static int ov5693_eeprom_device_init(struct ov5693 *priv)
926 {
927         char *dev_name = "eeprom_ov5693";
928         static struct regmap_config eeprom_regmap_config = {
929                 .reg_bits = 8,
930                 .val_bits = 8,
931         };
932         int i;
933         int err;
934
935         if (!priv->pdata->has_eeprom)
936                 return -EINVAL;
937
938         for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
939                 priv->eeprom[i].adap = i2c_get_adapter(
940                                 priv->i2c_client->adapter->nr);
941                 memset(&priv->eeprom[i].brd, 0, sizeof(priv->eeprom[i].brd));
942                 strncpy(priv->eeprom[i].brd.type, dev_name,
943                                 sizeof(priv->eeprom[i].brd.type));
944                 priv->eeprom[i].brd.addr = OV5693_EEPROM_ADDRESS + i;
945                 priv->eeprom[i].i2c_client = i2c_new_device(
946                                 priv->eeprom[i].adap, &priv->eeprom[i].brd);
947
948                 priv->eeprom[i].regmap = devm_regmap_init_i2c(
949                         priv->eeprom[i].i2c_client, &eeprom_regmap_config);
950                 if (IS_ERR(priv->eeprom[i].regmap)) {
951                         err = PTR_ERR(priv->eeprom[i].regmap);
952                         ov5693_eeprom_device_release(priv);
953                         return err;
954                 }
955         }
956
957         return 0;
958 }
959
960 static int ov5693_read_eeprom(struct ov5693 *priv,
961                                 struct v4l2_ctrl *ctrl)
962 {
963         int err, i;
964
965         for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
966                 err = regmap_bulk_read(priv->eeprom[i].regmap, 0,
967                         &priv->eeprom_buf[i * OV5693_EEPROM_BLOCK_SIZE],
968                         OV5693_EEPROM_BLOCK_SIZE);
969                 if (err)
970                         return err;
971         }
972
973         for (i = 0; i < OV5693_EEPROM_SIZE; i++)
974                 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
975                         priv->eeprom_buf[i]);
976         return 0;
977 }
978
979 static int ov5693_write_eeprom(struct ov5693 *priv,
980                                 char *string)
981 {
982         int err;
983         int i;
984         u8 curr[3];
985         unsigned long data;
986
987         for (i = 0; i < OV5693_EEPROM_SIZE; i++) {
988                 curr[0] = string[i*2];
989                 curr[1] = string[i*2+1];
990                 curr[2] = '\0';
991
992                 err = kstrtol(curr, 16, &data);
993                 if (err) {
994                         dev_err(&priv->i2c_client->dev,
995                                 "invalid eeprom string\n");
996                         return -EINVAL;
997                 }
998
999                 priv->eeprom_buf[i] = (u8)data;
1000                 err = regmap_write(priv->eeprom[i >> 8].regmap,
1001                                    i & 0xFF, (u8)data);
1002                 if (err)
1003                         return err;
1004                 msleep(20);
1005         }
1006         return 0;
1007 }
1008
1009 static int ov5693_read_otp_bank(struct ov5693 *priv,
1010                                 u8 *buf, int bank, u16 addr, int size)
1011 {
1012         int err;
1013
1014         /* sleeps calls in the sequence below are for internal device
1015          * signal propagation as specified by sensor vendor */
1016
1017         usleep_range(10000, 11000);
1018         err = ov5693_write_table(priv, mode_table[OV5693_MODE_START_STREAM]);
1019         if (err)
1020                 return err;
1021
1022         err = ov5693_write_reg(priv->s_data, OV5693_OTP_BANK_SELECT_ADDR,
1023                                0xC0 | bank);
1024         if (err)
1025                 return err;
1026
1027         err = ov5693_write_reg(priv->s_data, OV5693_OTP_LOAD_CTRL_ADDR, 0x01);
1028         if (err)
1029                 return err;
1030
1031         usleep_range(10000, 11000);
1032         err = regmap_bulk_read(priv->regmap, addr, buf, size);
1033         if (err)
1034                 return err;
1035
1036         err = ov5693_write_table(priv,
1037                         mode_table[OV5693_MODE_STOP_STREAM]);
1038         if (err)
1039                 return err;
1040
1041         return 0;
1042 }
1043
1044 static int ov5693_otp_setup(struct ov5693 *priv)
1045 {
1046         int err;
1047         int i;
1048         struct v4l2_ctrl *ctrl;
1049         u8 otp_buf[OV5693_OTP_SIZE];
1050
1051         err = camera_common_s_power(priv->subdev, true);
1052         if (err)
1053                 return -ENODEV;
1054
1055         for (i = 0; i < OV5693_OTP_NUM_BANKS; i++) {
1056                 err = ov5693_read_otp_bank(priv,
1057                                         &otp_buf[i * OV5693_OTP_BANK_SIZE],
1058                                         i,
1059                                         OV5693_OTP_BANK_START_ADDR,
1060                                         OV5693_OTP_BANK_SIZE);
1061                 if (err) {
1062                         dev_err(&priv->i2c_client->dev,
1063                                 "could not read otp bank\n");
1064                         goto ret;
1065                 }
1066         }
1067
1068         ctrl = v4l2_ctrl_find(&priv->ctrl_handler, V4L2_CID_OTP_DATA);
1069         if (!ctrl) {
1070                 dev_err(&priv->i2c_client->dev,
1071                         "could not find device ctrl.\n");
1072                 err = -EINVAL;
1073                 goto ret;
1074         }
1075
1076         for (i = 0; i < OV5693_OTP_SIZE; i++)
1077                 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
1078                         otp_buf[i]);
1079         ctrl->p_cur.p_char = ctrl->p_new.p_char;
1080
1081 ret:
1082         camera_common_s_power(priv->subdev, false);
1083
1084         return err;
1085 }
1086
1087 static int ov5693_fuse_id_setup(struct ov5693 *priv)
1088 {
1089         int err;
1090         int i;
1091         struct v4l2_ctrl *ctrl;
1092         u8 fuse_id[OV5693_FUSE_ID_SIZE];
1093
1094         err = camera_common_s_power(priv->subdev, true);
1095         if (err)
1096                 return -ENODEV;
1097
1098         err = ov5693_read_otp_bank(priv,
1099                                 &fuse_id[0],
1100                                 OV5693_FUSE_ID_OTP_BANK,
1101                                 OV5693_FUSE_ID_OTP_START_ADDR,
1102                                 OV5693_FUSE_ID_SIZE);
1103         if (err) {
1104                 dev_err(&priv->i2c_client->dev,
1105                         "could not read otp bank\n");
1106                 goto ret;
1107         }
1108
1109         ctrl = v4l2_ctrl_find(&priv->ctrl_handler, V4L2_CID_FUSE_ID);
1110         if (!ctrl) {
1111                 dev_err(&priv->i2c_client->dev,
1112                         "could not find device ctrl.\n");
1113                 err = -EINVAL;
1114                 goto ret;
1115         }
1116
1117         for (i = 0; i < OV5693_FUSE_ID_SIZE; i++)
1118                 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
1119                         fuse_id[i]);
1120         ctrl->p_cur.p_char = ctrl->p_new.p_char;
1121
1122 ret:
1123         camera_common_s_power(priv->subdev, false);
1124
1125         return err;
1126 }
1127
1128 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1129 {
1130         struct ov5693 *priv =
1131                 container_of(ctrl->handler, struct ov5693, ctrl_handler);
1132         int err = 0;
1133
1134         if (priv->power.state == SWITCH_OFF)
1135                 return 0;
1136
1137         switch (ctrl->id) {
1138         case V4L2_CID_EEPROM_DATA:
1139                 err = ov5693_read_eeprom(priv, ctrl);
1140                 if (err)
1141                         return err;
1142                 break;
1143         default:
1144                         pr_err("%s: unknown ctrl id.\n", __func__);
1145                         return -EINVAL;
1146         }
1147
1148         return err;
1149 }
1150
1151 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
1152 {
1153         struct ov5693 *priv =
1154                 container_of(ctrl->handler, struct ov5693, ctrl_handler);
1155         int err = 0;
1156
1157         if (priv->power.state == SWITCH_OFF)
1158                 return 0;
1159
1160         switch (ctrl->id) {
1161         case V4L2_CID_GAIN:
1162                 err = ov5693_set_gain(priv, ctrl->val);
1163                 break;
1164         case V4L2_CID_FRAME_LENGTH:
1165                 err = ov5693_set_frame_length(priv, ctrl->val);
1166                 break;
1167         case V4L2_CID_COARSE_TIME:
1168                 err = ov5693_set_coarse_time(priv, ctrl->val);
1169                 break;
1170         case V4L2_CID_COARSE_TIME_SHORT:
1171                 err = ov5693_set_coarse_time_short(priv, ctrl->val);
1172                 break;
1173         case V4L2_CID_GROUP_HOLD:
1174                 if (switch_ctrl_qmenu[ctrl->val] == SWITCH_ON) {
1175                         priv->group_hold_en = true;
1176                 } else {
1177                         priv->group_hold_en = false;
1178                         err = ov5693_set_group_hold(priv);
1179                 }
1180                 break;
1181         case V4L2_CID_EEPROM_DATA:
1182                 if (!ctrl->p_new.p_char[0])
1183                         break;
1184                 err = ov5693_write_eeprom(priv, ctrl->p_new.p_char);
1185                 if (err)
1186                         return err;
1187                 break;
1188         case V4L2_CID_HDR_EN:
1189                 break;
1190         default:
1191                 pr_err("%s: unknown ctrl id.\n", __func__);
1192                 return -EINVAL;
1193         }
1194
1195         return err;
1196 }
1197
1198 static int ov5693_ctrls_init(struct ov5693 *priv, bool eeprom_ctrl)
1199 {
1200         struct i2c_client *client = priv->i2c_client;
1201         struct camera_common_data *common_data = priv->s_data;
1202         struct v4l2_ctrl *ctrl;
1203         int numctrls;
1204         int err;
1205         int i;
1206
1207         dev_dbg(&client->dev, "%s++\n", __func__);
1208
1209         numctrls = ARRAY_SIZE(ctrl_config_list);
1210         v4l2_ctrl_handler_init(&priv->ctrl_handler, numctrls);
1211
1212         for (i = 0; i < numctrls; i++) {
1213                 /* Skip control 'V4L2_CID_EEPROM_DATA' if eeprom inint err */
1214                 if (ctrl_config_list[i].id == V4L2_CID_EEPROM_DATA) {
1215                         if (!eeprom_ctrl) {
1216                                 common_data->numctrls -= 1;
1217                                 continue;
1218                         }
1219                 }
1220
1221                 ctrl = v4l2_ctrl_new_custom(&priv->ctrl_handler,
1222                         &ctrl_config_list[i], NULL);
1223                 if (ctrl == NULL) {
1224                         dev_err(&client->dev, "Failed to init %s ctrl\n",
1225                                 ctrl_config_list[i].name);
1226                         continue;
1227                 }
1228
1229                 if (ctrl_config_list[i].type == V4L2_CTRL_TYPE_STRING &&
1230                         ctrl_config_list[i].flags & V4L2_CTRL_FLAG_READ_ONLY) {
1231                         ctrl->p_new.p_char = devm_kzalloc(&client->dev,
1232                                 ctrl_config_list[i].max + 1, GFP_KERNEL);
1233                         if (!ctrl->p_new.p_char)
1234                                 return -ENOMEM;
1235                 }
1236                 priv->ctrls[i] = ctrl;
1237         }
1238
1239         priv->numctrls = numctrls;
1240         priv->subdev->ctrl_handler = &priv->ctrl_handler;
1241         if (priv->ctrl_handler.error) {
1242                 dev_err(&client->dev, "Error %d adding controls\n",
1243                         priv->ctrl_handler.error);
1244                 err = priv->ctrl_handler.error;
1245                 goto error;
1246         }
1247
1248         err = v4l2_ctrl_handler_setup(&priv->ctrl_handler);
1249         if (err) {
1250                 dev_err(&client->dev,
1251                         "Error %d setting default controls\n", err);
1252                 goto error;
1253         }
1254
1255         err = ov5693_otp_setup(priv);
1256         if (err) {
1257                 dev_err(&client->dev,
1258                         "Error %d reading otp data\n", err);
1259                 goto error;
1260         }
1261
1262         err = ov5693_fuse_id_setup(priv);
1263         if (err) {
1264                 dev_err(&client->dev,
1265                         "Error %d reading fuse id data\n", err);
1266                 goto error;
1267         }
1268
1269         return 0;
1270
1271 error:
1272         v4l2_ctrl_handler_free(&priv->ctrl_handler);
1273         return err;
1274 }
1275
1276 MODULE_DEVICE_TABLE(of, ov5693_of_match);
1277
1278 static struct camera_common_pdata *ov5693_parse_dt(struct i2c_client *client)
1279 {
1280         struct device_node *node = client->dev.of_node;
1281         struct camera_common_pdata *board_priv_pdata;
1282         const struct of_device_id *match;
1283         int gpio;
1284         int err;
1285         struct camera_common_pdata *ret = NULL;
1286
1287         if (!node)
1288                 return NULL;
1289
1290         match = of_match_device(ov5693_of_match, &client->dev);
1291         if (!match) {
1292                 dev_err(&client->dev, "Failed to find matching dt id\n");
1293                 return NULL;
1294         }
1295
1296         board_priv_pdata = devm_kzalloc(&client->dev,
1297                            sizeof(*board_priv_pdata), GFP_KERNEL);
1298         if (!board_priv_pdata)
1299                 return NULL;
1300
1301         err = camera_common_parse_clocks(client, board_priv_pdata);
1302         if (err) {
1303                 dev_err(&client->dev, "Failed to find clocks\n");
1304                 goto error;
1305         }
1306
1307         gpio = of_get_named_gpio(node, "pwdn-gpios", 0);
1308         if (gpio < 0) {
1309                 if (gpio == -EPROBE_DEFER) {
1310                         ret = ERR_PTR(-EPROBE_DEFER);
1311                         goto error;
1312                 }
1313                 dev_err(&client->dev, "pwdn gpios not in DT\n");
1314                 goto error;
1315         }
1316         board_priv_pdata->pwdn_gpio = (unsigned int)gpio;
1317
1318         gpio = of_get_named_gpio(node, "reset-gpios", 0);
1319         if (gpio < 0) {
1320                 /* reset-gpio is not absolutely needed */
1321                 if (gpio == -EPROBE_DEFER) {
1322                         ret = ERR_PTR(-EPROBE_DEFER);
1323                         goto error;
1324                 }
1325                 dev_dbg(&client->dev, "reset gpios not in DT\n");
1326                 gpio = 0;
1327         }
1328         board_priv_pdata->reset_gpio = (unsigned int)gpio;
1329
1330         board_priv_pdata->use_cam_gpio =
1331                 of_property_read_bool(node, "cam,use-cam-gpio");
1332
1333         err = of_property_read_string(node, "avdd-reg",
1334                         &board_priv_pdata->regulators.avdd);
1335         if (err) {
1336                 dev_err(&client->dev, "avdd-reg not in DT\n");
1337                 goto error;
1338         }
1339         err = of_property_read_string(node, "iovdd-reg",
1340                         &board_priv_pdata->regulators.iovdd);
1341         if (err) {
1342                 dev_err(&client->dev, "iovdd-reg not in DT\n");
1343                 goto error;
1344         }
1345
1346         board_priv_pdata->has_eeprom =
1347                 of_property_read_bool(node, "has-eeprom");
1348         board_priv_pdata->v_flip= of_property_read_bool(node, "vertical-flip");
1349         board_priv_pdata->h_mirror = of_property_read_bool(node,
1350                                                          "horizontal-mirror");
1351         return board_priv_pdata;
1352
1353 error:
1354         devm_kfree(&client->dev, board_priv_pdata);
1355         return ret;
1356 }
1357
1358 static int ov5693_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1359 {
1360         struct i2c_client *client = v4l2_get_subdevdata(sd);
1361
1362         dev_dbg(&client->dev, "%s:\n", __func__);
1363         return 0;
1364 }
1365
1366 static const struct v4l2_subdev_internal_ops ov5693_subdev_internal_ops = {
1367         .open = ov5693_open,
1368 };
1369
1370 static const struct media_entity_operations ov5693_media_ops = {
1371         .link_validate = v4l2_subdev_link_validate,
1372 };
1373
1374 static int ov5693_probe(struct i2c_client *client,
1375                         const struct i2c_device_id *id)
1376 {
1377         struct camera_common_data *common_data;
1378         struct device_node *node = client->dev.of_node;
1379         struct ov5693 *priv;
1380         char debugfs_name[10];
1381         int err;
1382
1383         pr_info("[OV5693]: probing v4l2 sensor.\n");
1384
1385         if (!IS_ENABLED(CONFIG_OF) || !node)
1386                 return -EINVAL;
1387
1388         common_data = devm_kzalloc(&client->dev,
1389                             sizeof(struct camera_common_data), GFP_KERNEL);
1390         if (!common_data)
1391                 return -ENOMEM;
1392
1393         priv = devm_kzalloc(&client->dev,
1394                             sizeof(struct ov5693) + sizeof(struct v4l2_ctrl *) *
1395                             ARRAY_SIZE(ctrl_config_list),
1396                             GFP_KERNEL);
1397         if (!priv)
1398                 return -ENOMEM;
1399
1400         priv->regmap = devm_regmap_init_i2c(client, &ov5693_regmap_config);
1401         if (IS_ERR(priv->regmap)) {
1402                 dev_err(&client->dev,
1403                         "regmap init failed: %ld\n", PTR_ERR(priv->regmap));
1404                 return -ENODEV;
1405         }
1406
1407         priv->pdata = ov5693_parse_dt(client);
1408         if (PTR_ERR(priv->pdata) == -EPROBE_DEFER)
1409                 return -EPROBE_DEFER;
1410         if (!priv->pdata) {
1411                 dev_err(&client->dev, "unable to get platform data\n");
1412                 return -EFAULT;
1413         }
1414
1415         common_data->ops                = &ov5693_common_ops;
1416         common_data->ctrl_handler       = &priv->ctrl_handler;
1417         common_data->i2c_client         = client;
1418         common_data->frmfmt             = ov5693_frmfmt;
1419         common_data->colorfmt           = camera_common_find_datafmt(
1420                                           OV5693_DEFAULT_DATAFMT);
1421         common_data->power              = &priv->power;
1422         common_data->ctrls              = priv->ctrls;
1423         common_data->priv               = (void *)priv;
1424         common_data->numctrls           = ARRAY_SIZE(ctrl_config_list);
1425         common_data->numfmts            = ARRAY_SIZE(ov5693_frmfmt);
1426         common_data->def_mode           = OV5693_DEFAULT_MODE;
1427         common_data->def_width          = OV5693_DEFAULT_WIDTH;
1428         common_data->def_height         = OV5693_DEFAULT_HEIGHT;
1429         common_data->fmt_width          = common_data->def_width;
1430         common_data->fmt_height         = common_data->def_height;
1431         common_data->def_clk_freq       = OV5693_DEFAULT_CLK_FREQ;
1432
1433         priv->i2c_client = client;
1434         priv->s_data                    = common_data;
1435         priv->subdev                    = &common_data->subdev;
1436         priv->subdev->dev               = &client->dev;
1437         priv->s_data->dev               = &client->dev;
1438
1439         err = ov5693_power_get(priv);
1440         if (err)
1441                 return err;
1442
1443         err = camera_common_parse_ports(client, common_data);
1444         if (err) {
1445                 dev_err(&client->dev, "Failed to find port info\n");
1446                 return err;
1447         }
1448         sprintf(debugfs_name, "ov5693_%c", common_data->csi_port + 'a');
1449         dev_dbg(&client->dev, "%s: name %s\n", __func__, debugfs_name);
1450         camera_common_create_debugfs(common_data, debugfs_name);
1451
1452         v4l2_i2c_subdev_init(priv->subdev, client, &ov5693_subdev_ops);
1453
1454         /* eeprom interface */
1455         err = ov5693_eeprom_device_init(priv);
1456         if (err && priv->pdata->has_eeprom)
1457                 dev_err(&client->dev,
1458                         "Failed to allocate eeprom reg map: %d\n", err);
1459
1460         err = ov5693_ctrls_init(priv, !err);
1461         if (err)
1462                 return err;
1463
1464         priv->subdev->internal_ops = &ov5693_subdev_internal_ops;
1465         priv->subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1466                                V4L2_SUBDEV_FL_HAS_EVENTS;
1467
1468 #if defined(CONFIG_MEDIA_CONTROLLER)
1469         priv->pad.flags = MEDIA_PAD_FL_SOURCE;
1470         priv->subdev->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1471         priv->subdev->entity.ops = &ov5693_media_ops;
1472         err = media_entity_init(&priv->subdev->entity, 1, &priv->pad, 0);
1473         if (err < 0) {
1474                 dev_err(&client->dev, "unable to init media entity\n");
1475                 return err;
1476         }
1477 #endif
1478
1479         err = v4l2_async_register_subdev(priv->subdev);
1480         if (err)
1481                 return err;
1482
1483         dev_dbg(&client->dev, "Detected OV5693 sensor\n");
1484
1485
1486         return 0;
1487 }
1488
1489 static int
1490 ov5693_remove(struct i2c_client *client)
1491 {
1492         struct camera_common_data *s_data = to_camera_common_data(client);
1493         struct ov5693 *priv = (struct ov5693 *)s_data->priv;
1494
1495         v4l2_async_unregister_subdev(priv->subdev);
1496 #if defined(CONFIG_MEDIA_CONTROLLER)
1497         media_entity_cleanup(&priv->subdev->entity);
1498 #endif
1499
1500         v4l2_ctrl_handler_free(&priv->ctrl_handler);
1501         ov5693_power_put(priv);
1502         camera_common_remove_debugfs(s_data);
1503
1504         return 0;
1505 }
1506
1507 static const struct i2c_device_id ov5693_id[] = {
1508         { "ov5693", 0 },
1509         { }
1510 };
1511
1512 MODULE_DEVICE_TABLE(i2c, ov5693_id);
1513
1514 static struct i2c_driver ov5693_i2c_driver = {
1515         .driver = {
1516                 .name = "ov5693",
1517                 .owner = THIS_MODULE,
1518                 .of_match_table = of_match_ptr(ov5693_of_match),
1519         },
1520         .probe = ov5693_probe,
1521         .remove = ov5693_remove,
1522         .id_table = ov5693_id,
1523 };
1524
1525 module_i2c_driver(ov5693_i2c_driver);
1526
1527 MODULE_DESCRIPTION("SoC Camera driver for Sony OV5693");
1528 MODULE_AUTHOR("David Wang <davidw@nvidia.com>");
1529 MODULE_LICENSE("GPL v2");
1530