GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / media / atomisp / i2c / atomisp-ov2722.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2722 1080p HD camera sensor.
4  *
5  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
32 #include "../include/linux/atomisp_gmin_platform.h"
33 #include <linux/acpi.h>
34 #include <linux/io.h>
35
36 #include "ov2722.h"
37
38 /* i2c read/write stuff */
39 static int ov2722_read_reg(struct i2c_client *client,
40                            u16 data_length, u16 reg, u16 *val)
41 {
42         int err;
43         struct i2c_msg msg[2];
44         unsigned char data[6];
45
46         if (!client->adapter) {
47                 dev_err(&client->dev, "%s error, no client->adapter\n",
48                         __func__);
49                 return -ENODEV;
50         }
51
52         if (data_length != OV2722_8BIT && data_length != OV2722_16BIT &&
53             data_length != OV2722_32BIT) {
54                 dev_err(&client->dev, "%s error, invalid data length\n",
55                         __func__);
56                 return -EINVAL;
57         }
58
59         memset(msg, 0, sizeof(msg));
60
61         msg[0].addr = client->addr;
62         msg[0].flags = 0;
63         msg[0].len = I2C_MSG_LENGTH;
64         msg[0].buf = data;
65
66         /* high byte goes out first */
67         data[0] = (u8)(reg >> 8);
68         data[1] = (u8)(reg & 0xff);
69
70         msg[1].addr = client->addr;
71         msg[1].len = data_length;
72         msg[1].flags = I2C_M_RD;
73         msg[1].buf = data;
74
75         err = i2c_transfer(client->adapter, msg, 2);
76         if (err != 2) {
77                 if (err >= 0)
78                         err = -EIO;
79                 dev_err(&client->dev,
80                         "read from offset 0x%x error %d", reg, err);
81                 return err;
82         }
83
84         *val = 0;
85         /* high byte comes first */
86         if (data_length == OV2722_8BIT)
87                 *val = (u8)data[0];
88         else if (data_length == OV2722_16BIT)
89                 *val = be16_to_cpu(*(__be16 *)&data[0]);
90         else
91                 *val = be32_to_cpu(*(__be32 *)&data[0]);
92
93         return 0;
94 }
95
96 static int ov2722_i2c_write(struct i2c_client *client, u16 len, u8 *data)
97 {
98         struct i2c_msg msg;
99         const int num_msg = 1;
100         int ret;
101
102         msg.addr = client->addr;
103         msg.flags = 0;
104         msg.len = len;
105         msg.buf = data;
106         ret = i2c_transfer(client->adapter, &msg, 1);
107
108         return ret == num_msg ? 0 : -EIO;
109 }
110
111 static int ov2722_write_reg(struct i2c_client *client, u16 data_length,
112                             u16 reg, u16 val)
113 {
114         int ret;
115         unsigned char data[4] = {0};
116         __be16 *wreg = (__be16 *)data;
117         const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
118
119         if (data_length != OV2722_8BIT && data_length != OV2722_16BIT) {
120                 dev_err(&client->dev,
121                         "%s error, invalid data_length\n", __func__);
122                 return -EINVAL;
123         }
124
125         /* high byte goes out first */
126         *wreg = cpu_to_be16(reg);
127
128         if (data_length == OV2722_8BIT) {
129                 data[2] = (u8)(val);
130         } else {
131                 /* OV2722_16BIT */
132                 __be16 *wdata = (__be16 *)&data[2];
133
134                 *wdata = cpu_to_be16(val);
135         }
136
137         ret = ov2722_i2c_write(client, len, data);
138         if (ret)
139                 dev_err(&client->dev,
140                         "write error: wrote 0x%x to offset 0x%x error %d",
141                         val, reg, ret);
142
143         return ret;
144 }
145
146 /*
147  * ov2722_write_reg_array - Initializes a list of OV2722 registers
148  * @client: i2c driver client structure
149  * @reglist: list of registers to be written
150  *
151  * This function initializes a list of registers. When consecutive addresses
152  * are found in a row on the list, this function creates a buffer and sends
153  * consecutive data in a single i2c_transfer().
154  *
155  * __ov2722_flush_reg_array, __ov2722_buf_reg_array() and
156  * __ov2722_write_reg_is_consecutive() are internal functions to
157  * ov2722_write_reg_array_fast() and should be not used anywhere else.
158  *
159  */
160
161 static int __ov2722_flush_reg_array(struct i2c_client *client,
162                                     struct ov2722_write_ctrl *ctrl)
163 {
164         u16 size;
165         __be16 *data16 = (void *)&ctrl->buffer.addr;
166
167         if (ctrl->index == 0)
168                 return 0;
169
170         size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
171         *data16 = cpu_to_be16(ctrl->buffer.addr);
172         ctrl->index = 0;
173
174         return ov2722_i2c_write(client, size, (u8 *)&ctrl->buffer);
175 }
176
177 static int __ov2722_buf_reg_array(struct i2c_client *client,
178                                   struct ov2722_write_ctrl *ctrl,
179                                   const struct ov2722_reg *next)
180 {
181         int size;
182         __be16 *data16;
183
184         switch (next->type) {
185         case OV2722_8BIT:
186                 size = 1;
187                 ctrl->buffer.data[ctrl->index] = (u8)next->val;
188                 break;
189         case OV2722_16BIT:
190                 size = 2;
191                 data16 = (void *)&ctrl->buffer.data[ctrl->index];
192                 *data16 = cpu_to_be16((u16)next->val);
193                 break;
194         default:
195                 return -EINVAL;
196         }
197
198         /* When first item is added, we need to store its starting address */
199         if (ctrl->index == 0)
200                 ctrl->buffer.addr = next->reg;
201
202         ctrl->index += size;
203
204         /*
205          * Buffer cannot guarantee free space for u32? Better flush it to avoid
206          * possible lack of memory for next item.
207          */
208         if (ctrl->index + sizeof(u16) >= OV2722_MAX_WRITE_BUF_SIZE)
209                 return __ov2722_flush_reg_array(client, ctrl);
210
211         return 0;
212 }
213
214 static int __ov2722_write_reg_is_consecutive(struct i2c_client *client,
215                                              struct ov2722_write_ctrl *ctrl,
216                                              const struct ov2722_reg *next)
217 {
218         if (ctrl->index == 0)
219                 return 1;
220
221         return ctrl->buffer.addr + ctrl->index == next->reg;
222 }
223
224 static int ov2722_write_reg_array(struct i2c_client *client,
225                                   const struct ov2722_reg *reglist)
226 {
227         const struct ov2722_reg *next = reglist;
228         struct ov2722_write_ctrl ctrl;
229         int err;
230
231         ctrl.index = 0;
232         for (; next->type != OV2722_TOK_TERM; next++) {
233                 switch (next->type & OV2722_TOK_MASK) {
234                 case OV2722_TOK_DELAY:
235                         err = __ov2722_flush_reg_array(client, &ctrl);
236                         if (err)
237                                 return err;
238                         msleep(next->val);
239                         break;
240                 default:
241                         /*
242                          * If next address is not consecutive, data needs to be
243                          * flushed before proceed.
244                          */
245                         if (!__ov2722_write_reg_is_consecutive(client, &ctrl,
246                                                                next)) {
247                                 err = __ov2722_flush_reg_array(client, &ctrl);
248                                 if (err)
249                                         return err;
250                         }
251                         err = __ov2722_buf_reg_array(client, &ctrl, next);
252                         if (err) {
253                                 dev_err(&client->dev, "%s: write error, aborted\n",
254                                         __func__);
255                                 return err;
256                         }
257                         break;
258                 }
259         }
260
261         return __ov2722_flush_reg_array(client, &ctrl);
262 }
263
264 static int ov2722_g_focal(struct v4l2_subdev *sd, s32 *val)
265 {
266         *val = (OV2722_FOCAL_LENGTH_NUM << 16) | OV2722_FOCAL_LENGTH_DEM;
267         return 0;
268 }
269
270 static int ov2722_g_fnumber(struct v4l2_subdev *sd, s32 *val)
271 {
272         /*const f number for imx*/
273         *val = (OV2722_F_NUMBER_DEFAULT_NUM << 16) | OV2722_F_NUMBER_DEM;
274         return 0;
275 }
276
277 static int ov2722_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
278 {
279         *val = (OV2722_F_NUMBER_DEFAULT_NUM << 24) |
280                (OV2722_F_NUMBER_DEM << 16) |
281                (OV2722_F_NUMBER_DEFAULT_NUM << 8) | OV2722_F_NUMBER_DEM;
282         return 0;
283 }
284
285 static int ov2722_get_intg_factor(struct i2c_client *client,
286                                   struct camera_mipi_info *info,
287                                   const struct ov2722_resolution *res)
288 {
289         struct v4l2_subdev *sd = i2c_get_clientdata(client);
290         struct ov2722_device *dev = NULL;
291         struct atomisp_sensor_mode_data *buf = &info->data;
292         const unsigned int ext_clk_freq_hz = 19200000;
293         const unsigned int pll_invariant_div = 10;
294         unsigned int pix_clk_freq_hz;
295         u16 pre_pll_clk_div;
296         u16 pll_multiplier;
297         u16 op_pix_clk_div;
298         u16 reg_val;
299         int ret;
300
301         if (!info)
302                 return -EINVAL;
303
304         dev = to_ov2722_sensor(sd);
305
306         /* pixel clock calculattion */
307         ret =  ov2722_read_reg(client, OV2722_8BIT,
308                                OV2722_SC_CMMN_PLL_CTRL3, &pre_pll_clk_div);
309         if (ret)
310                 return ret;
311
312         ret =  ov2722_read_reg(client, OV2722_8BIT,
313                                OV2722_SC_CMMN_PLL_MULTIPLIER, &pll_multiplier);
314         if (ret)
315                 return ret;
316
317         ret =  ov2722_read_reg(client, OV2722_8BIT,
318                                OV2722_SC_CMMN_PLL_DEBUG_OPT, &op_pix_clk_div);
319         if (ret)
320                 return ret;
321
322         pre_pll_clk_div = (pre_pll_clk_div & 0x70) >> 4;
323         if (!pre_pll_clk_div)
324                 return -EINVAL;
325
326         pll_multiplier = pll_multiplier & 0x7f;
327         op_pix_clk_div = op_pix_clk_div & 0x03;
328         pix_clk_freq_hz = ext_clk_freq_hz / pre_pll_clk_div * pll_multiplier
329                           * op_pix_clk_div / pll_invariant_div;
330
331         dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
332         buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
333
334         /* get integration time */
335         buf->coarse_integration_time_min = OV2722_COARSE_INTG_TIME_MIN;
336         buf->coarse_integration_time_max_margin =
337             OV2722_COARSE_INTG_TIME_MAX_MARGIN;
338
339         buf->fine_integration_time_min = OV2722_FINE_INTG_TIME_MIN;
340         buf->fine_integration_time_max_margin =
341             OV2722_FINE_INTG_TIME_MAX_MARGIN;
342
343         buf->fine_integration_time_def = OV2722_FINE_INTG_TIME_MIN;
344         buf->frame_length_lines = res->lines_per_frame;
345         buf->line_length_pck = res->pixels_per_line;
346         buf->read_mode = res->bin_mode;
347
348         /* get the cropping and output resolution to ISP for this mode. */
349         ret =  ov2722_read_reg(client, OV2722_16BIT,
350                                OV2722_H_CROP_START_H, &reg_val);
351         if (ret)
352                 return ret;
353         buf->crop_horizontal_start = reg_val;
354
355         ret =  ov2722_read_reg(client, OV2722_16BIT,
356                                OV2722_V_CROP_START_H, &reg_val);
357         if (ret)
358                 return ret;
359         buf->crop_vertical_start = reg_val;
360
361         ret = ov2722_read_reg(client, OV2722_16BIT,
362                               OV2722_H_CROP_END_H, &reg_val);
363         if (ret)
364                 return ret;
365         buf->crop_horizontal_end = reg_val;
366
367         ret = ov2722_read_reg(client, OV2722_16BIT,
368                               OV2722_V_CROP_END_H, &reg_val);
369         if (ret)
370                 return ret;
371         buf->crop_vertical_end = reg_val;
372
373         ret = ov2722_read_reg(client, OV2722_16BIT,
374                               OV2722_H_OUTSIZE_H, &reg_val);
375         if (ret)
376                 return ret;
377         buf->output_width = reg_val;
378
379         ret = ov2722_read_reg(client, OV2722_16BIT,
380                               OV2722_V_OUTSIZE_H, &reg_val);
381         if (ret)
382                 return ret;
383         buf->output_height = reg_val;
384
385         buf->binning_factor_x = res->bin_factor_x ?
386                                 res->bin_factor_x : 1;
387         buf->binning_factor_y = res->bin_factor_y ?
388                                 res->bin_factor_y : 1;
389         return 0;
390 }
391
392 static long __ov2722_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
393                                   int gain, int digitgain)
394
395 {
396         struct i2c_client *client = v4l2_get_subdevdata(sd);
397         struct ov2722_device *dev = to_ov2722_sensor(sd);
398         u16 hts, vts;
399         int ret;
400
401         dev_dbg(&client->dev, "set_exposure without group hold\n");
402
403         /* clear VTS_DIFF on manual mode */
404         ret = ov2722_write_reg(client, OV2722_16BIT, OV2722_VTS_DIFF_H, 0);
405         if (ret)
406                 return ret;
407
408         hts = dev->pixels_per_line;
409         vts = dev->lines_per_frame;
410
411         if ((coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN) > vts)
412                 vts = coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN;
413
414         coarse_itg <<= 4;
415         digitgain <<= 2;
416
417         ret = ov2722_write_reg(client, OV2722_16BIT,
418                                OV2722_VTS_H, vts);
419         if (ret)
420                 return ret;
421
422         ret = ov2722_write_reg(client, OV2722_16BIT,
423                                OV2722_HTS_H, hts);
424         if (ret)
425                 return ret;
426
427         /* set exposure */
428         ret = ov2722_write_reg(client, OV2722_8BIT,
429                                OV2722_AEC_PK_EXPO_L,
430                                coarse_itg & 0xff);
431         if (ret)
432                 return ret;
433
434         ret = ov2722_write_reg(client, OV2722_16BIT,
435                                OV2722_AEC_PK_EXPO_H,
436                                (coarse_itg >> 8) & 0xfff);
437         if (ret)
438                 return ret;
439
440         /* set analog gain */
441         ret = ov2722_write_reg(client, OV2722_16BIT,
442                                OV2722_AGC_ADJ_H, gain);
443         if (ret)
444                 return ret;
445
446         /* set digital gain */
447         ret = ov2722_write_reg(client, OV2722_16BIT,
448                                OV2722_MWB_GAIN_R_H, digitgain);
449         if (ret)
450                 return ret;
451
452         ret = ov2722_write_reg(client, OV2722_16BIT,
453                                OV2722_MWB_GAIN_G_H, digitgain);
454         if (ret)
455                 return ret;
456
457         ret = ov2722_write_reg(client, OV2722_16BIT,
458                                OV2722_MWB_GAIN_B_H, digitgain);
459
460         return ret;
461 }
462
463 static int ov2722_set_exposure(struct v4l2_subdev *sd, int exposure,
464                                int gain, int digitgain)
465 {
466         struct ov2722_device *dev = to_ov2722_sensor(sd);
467         int ret;
468
469         mutex_lock(&dev->input_lock);
470         ret = __ov2722_set_exposure(sd, exposure, gain, digitgain);
471         mutex_unlock(&dev->input_lock);
472
473         return ret;
474 }
475
476 static long ov2722_s_exposure(struct v4l2_subdev *sd,
477                               struct atomisp_exposure *exposure)
478 {
479         int exp = exposure->integration_time[0];
480         int gain = exposure->gain[0];
481         int digitgain = exposure->gain[1];
482
483         /* we should not accept the invalid value below. */
484         if (gain == 0) {
485                 struct i2c_client *client = v4l2_get_subdevdata(sd);
486
487                 v4l2_err(client, "%s: invalid value\n", __func__);
488                 return -EINVAL;
489         }
490
491         return ov2722_set_exposure(sd, exp, gain, digitgain);
492 }
493
494 static long ov2722_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
495 {
496         switch (cmd) {
497         case ATOMISP_IOC_S_EXPOSURE:
498                 return ov2722_s_exposure(sd, arg);
499         default:
500                 return -EINVAL;
501         }
502         return 0;
503 }
504
505 /* This returns the exposure time being used. This should only be used
506  * for filling in EXIF data, not for actual image processing.
507  */
508 static int ov2722_q_exposure(struct v4l2_subdev *sd, s32 *value)
509 {
510         struct i2c_client *client = v4l2_get_subdevdata(sd);
511         u16 reg_v, reg_v2;
512         int ret;
513
514         /* get exposure */
515         ret = ov2722_read_reg(client, OV2722_8BIT,
516                               OV2722_AEC_PK_EXPO_L,
517                               &reg_v);
518         if (ret)
519                 goto err;
520
521         ret = ov2722_read_reg(client, OV2722_8BIT,
522                               OV2722_AEC_PK_EXPO_M,
523                               &reg_v2);
524         if (ret)
525                 goto err;
526
527         reg_v += reg_v2 << 8;
528         ret = ov2722_read_reg(client, OV2722_8BIT,
529                               OV2722_AEC_PK_EXPO_H,
530                               &reg_v2);
531         if (ret)
532                 goto err;
533
534         *value = reg_v + (((u32)reg_v2 << 16));
535 err:
536         return ret;
537 }
538
539 static int ov2722_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
540 {
541         struct ov2722_device *dev =
542             container_of(ctrl->handler, struct ov2722_device, ctrl_handler);
543         int ret = 0;
544         unsigned int val;
545
546         switch (ctrl->id) {
547         case V4L2_CID_EXPOSURE_ABSOLUTE:
548                 ret = ov2722_q_exposure(&dev->sd, &ctrl->val);
549                 break;
550         case V4L2_CID_FOCAL_ABSOLUTE:
551                 ret = ov2722_g_focal(&dev->sd, &ctrl->val);
552                 break;
553         case V4L2_CID_FNUMBER_ABSOLUTE:
554                 ret = ov2722_g_fnumber(&dev->sd, &ctrl->val);
555                 break;
556         case V4L2_CID_FNUMBER_RANGE:
557                 ret = ov2722_g_fnumber_range(&dev->sd, &ctrl->val);
558                 break;
559         case V4L2_CID_LINK_FREQ:
560                 val = dev->res->mipi_freq;
561                 if (val == 0)
562                         return -EINVAL;
563
564                 ctrl->val = val * 1000; /* To Hz */
565                 break;
566         default:
567                 ret = -EINVAL;
568         }
569
570         return ret;
571 }
572
573 static const struct v4l2_ctrl_ops ctrl_ops = {
574         .g_volatile_ctrl = ov2722_g_volatile_ctrl
575 };
576
577 static const struct v4l2_ctrl_config ov2722_controls[] = {
578         {
579                 .ops = &ctrl_ops,
580                 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
581                 .type = V4L2_CTRL_TYPE_INTEGER,
582                 .name = "exposure",
583                 .min = 0x0,
584                 .max = 0xffff,
585                 .step = 0x01,
586                 .def = 0x00,
587                 .flags = 0,
588         },
589         {
590                 .ops = &ctrl_ops,
591                 .id = V4L2_CID_FOCAL_ABSOLUTE,
592                 .type = V4L2_CTRL_TYPE_INTEGER,
593                 .name = "focal length",
594                 .min = OV2722_FOCAL_LENGTH_DEFAULT,
595                 .max = OV2722_FOCAL_LENGTH_DEFAULT,
596                 .step = 0x01,
597                 .def = OV2722_FOCAL_LENGTH_DEFAULT,
598                 .flags = 0,
599         },
600         {
601                 .ops = &ctrl_ops,
602                 .id = V4L2_CID_FNUMBER_ABSOLUTE,
603                 .type = V4L2_CTRL_TYPE_INTEGER,
604                 .name = "f-number",
605                 .min = OV2722_F_NUMBER_DEFAULT,
606                 .max = OV2722_F_NUMBER_DEFAULT,
607                 .step = 0x01,
608                 .def = OV2722_F_NUMBER_DEFAULT,
609                 .flags = 0,
610         },
611         {
612                 .ops = &ctrl_ops,
613                 .id = V4L2_CID_FNUMBER_RANGE,
614                 .type = V4L2_CTRL_TYPE_INTEGER,
615                 .name = "f-number range",
616                 .min = OV2722_F_NUMBER_RANGE,
617                 .max = OV2722_F_NUMBER_RANGE,
618                 .step = 0x01,
619                 .def = OV2722_F_NUMBER_RANGE,
620                 .flags = 0,
621         },
622         {
623                 .ops = &ctrl_ops,
624                 .id = V4L2_CID_LINK_FREQ,
625                 .name = "Link Frequency",
626                 .type = V4L2_CTRL_TYPE_INTEGER,
627                 .min = 1,
628                 .max = 1500000 * 1000,
629                 .step = 1,
630                 .def = 1,
631                 .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY,
632         },
633 };
634
635 static int ov2722_init(struct v4l2_subdev *sd)
636 {
637         struct ov2722_device *dev = to_ov2722_sensor(sd);
638
639         mutex_lock(&dev->input_lock);
640
641         /* restore settings */
642         ov2722_res = ov2722_res_preview;
643         N_RES = N_RES_PREVIEW;
644
645         mutex_unlock(&dev->input_lock);
646
647         return 0;
648 }
649
650 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
651 {
652         int ret = -1;
653         struct ov2722_device *dev = to_ov2722_sensor(sd);
654
655         if (!dev || !dev->platform_data)
656                 return -ENODEV;
657
658         if (flag) {
659                 ret = dev->platform_data->v1p8_ctrl(sd, 1);
660                 if (ret == 0) {
661                         ret = dev->platform_data->v2p8_ctrl(sd, 1);
662                         if (ret)
663                                 dev->platform_data->v1p8_ctrl(sd, 0);
664                 }
665         } else {
666                 ret = dev->platform_data->v1p8_ctrl(sd, 0);
667                 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
668         }
669
670         return ret;
671 }
672
673 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
674 {
675         struct ov2722_device *dev = to_ov2722_sensor(sd);
676         int ret = -1;
677
678         if (!dev || !dev->platform_data)
679                 return -ENODEV;
680
681         /* Note: the GPIO order is asymmetric: always RESET#
682          * before PWDN# when turning it on or off.
683          */
684         ret = dev->platform_data->gpio0_ctrl(sd, flag);
685         /*
686          *ov2722 PWDN# active high when pull down,opposite to the convention
687          */
688         ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
689         return ret;
690 }
691
692 static int power_up(struct v4l2_subdev *sd)
693 {
694         struct ov2722_device *dev = to_ov2722_sensor(sd);
695         struct i2c_client *client = v4l2_get_subdevdata(sd);
696         int ret;
697
698         if (!dev->platform_data) {
699                 dev_err(&client->dev,
700                         "no camera_sensor_platform_data");
701                 return -ENODEV;
702         }
703
704         /* power control */
705         ret = power_ctrl(sd, 1);
706         if (ret)
707                 goto fail_power;
708
709         /* according to DS, at least 5ms is needed between DOVDD and PWDN */
710         usleep_range(5000, 6000);
711
712         /* gpio ctrl */
713         ret = gpio_ctrl(sd, 1);
714         if (ret) {
715                 ret = gpio_ctrl(sd, 0);
716                 if (ret)
717                         goto fail_power;
718         }
719
720         /* flis clock control */
721         ret = dev->platform_data->flisclk_ctrl(sd, 1);
722         if (ret)
723                 goto fail_clk;
724
725         /* according to DS, 20ms is needed between PWDN and i2c access */
726         msleep(20);
727
728         return 0;
729
730 fail_clk:
731         gpio_ctrl(sd, 0);
732 fail_power:
733         power_ctrl(sd, 0);
734         dev_err(&client->dev, "sensor power-up failed\n");
735
736         return ret;
737 }
738
739 static int power_down(struct v4l2_subdev *sd)
740 {
741         struct ov2722_device *dev = to_ov2722_sensor(sd);
742         struct i2c_client *client = v4l2_get_subdevdata(sd);
743         int ret = 0;
744
745         if (!dev->platform_data) {
746                 dev_err(&client->dev,
747                         "no camera_sensor_platform_data");
748                 return -ENODEV;
749         }
750
751         ret = dev->platform_data->flisclk_ctrl(sd, 0);
752         if (ret)
753                 dev_err(&client->dev, "flisclk failed\n");
754
755         /* gpio ctrl */
756         ret = gpio_ctrl(sd, 0);
757         if (ret) {
758                 ret = gpio_ctrl(sd, 0);
759                 if (ret)
760                         dev_err(&client->dev, "gpio failed 2\n");
761         }
762
763         /* power control */
764         ret = power_ctrl(sd, 0);
765         if (ret)
766                 dev_err(&client->dev, "vprog failed.\n");
767
768         return ret;
769 }
770
771 static int ov2722_s_power(struct v4l2_subdev *sd, int on)
772 {
773         int ret;
774
775         if (on == 0)
776                 return power_down(sd);
777
778         ret = power_up(sd);
779         if (!ret)
780                 return ov2722_init(sd);
781
782         return ret;
783 }
784
785 /* TODO: remove it. */
786 static int startup(struct v4l2_subdev *sd)
787 {
788         struct ov2722_device *dev = to_ov2722_sensor(sd);
789         struct i2c_client *client = v4l2_get_subdevdata(sd);
790         int ret = 0;
791
792         ret = ov2722_write_reg(client, OV2722_8BIT,
793                                OV2722_SW_RESET, 0x01);
794         if (ret) {
795                 dev_err(&client->dev, "ov2722 reset err.\n");
796                 return ret;
797         }
798
799         ret = ov2722_write_reg_array(client, dev->res->regs);
800         if (ret) {
801                 dev_err(&client->dev, "ov2722 write register err.\n");
802                 return ret;
803         }
804
805         return ret;
806 }
807
808 static int ov2722_set_fmt(struct v4l2_subdev *sd,
809                           struct v4l2_subdev_state *sd_state,
810                           struct v4l2_subdev_format *format)
811 {
812         struct v4l2_mbus_framefmt *fmt = &format->format;
813         struct ov2722_device *dev = to_ov2722_sensor(sd);
814         struct i2c_client *client = v4l2_get_subdevdata(sd);
815         struct ov2722_resolution *res;
816         struct camera_mipi_info *ov2722_info = NULL;
817         int ret = 0;
818
819         if (format->pad)
820                 return -EINVAL;
821         if (!fmt)
822                 return -EINVAL;
823         ov2722_info = v4l2_get_subdev_hostdata(sd);
824         if (!ov2722_info)
825                 return -EINVAL;
826
827         mutex_lock(&dev->input_lock);
828         res = v4l2_find_nearest_size(ov2722_res_preview,
829                                      ARRAY_SIZE(ov2722_res_preview), width,
830                                      height, fmt->width, fmt->height);
831         if (!res)
832                 res = &ov2722_res_preview[N_RES - 1];
833
834         fmt->width = res->width;
835         fmt->height = res->height;
836         dev->res = res;
837
838         fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
839         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
840                 sd_state->pads->try_fmt = *fmt;
841                 mutex_unlock(&dev->input_lock);
842                 return 0;
843         }
844
845
846         dev->pixels_per_line = dev->res->pixels_per_line;
847         dev->lines_per_frame = dev->res->lines_per_frame;
848
849         ret = startup(sd);
850         if (ret) {
851                 int i = 0;
852
853                 dev_err(&client->dev, "ov2722 startup err, retry to power up\n");
854                 for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) {
855                         dev_err(&client->dev,
856                                 "ov2722 retry to power up %d/%d times, result: ",
857                                 i + 1, OV2722_POWER_UP_RETRY_NUM);
858                         power_down(sd);
859                         ret = power_up(sd);
860                         if (ret) {
861                                 dev_err(&client->dev, "power up failed, continue\n");
862                                 continue;
863                         }
864                         ret = startup(sd);
865                         if (ret) {
866                                 dev_err(&client->dev, " startup FAILED!\n");
867                         } else {
868                                 dev_err(&client->dev, " startup SUCCESS!\n");
869                                 break;
870                         }
871                 }
872                 if (ret) {
873                         dev_err(&client->dev, "ov2722 startup err\n");
874                         goto err;
875                 }
876         }
877
878         ret = ov2722_get_intg_factor(client, ov2722_info, dev->res);
879         if (ret)
880                 dev_err(&client->dev, "failed to get integration_factor\n");
881
882 err:
883         mutex_unlock(&dev->input_lock);
884         return ret;
885 }
886
887 static int ov2722_get_fmt(struct v4l2_subdev *sd,
888                           struct v4l2_subdev_state *sd_state,
889                           struct v4l2_subdev_format *format)
890 {
891         struct v4l2_mbus_framefmt *fmt = &format->format;
892         struct ov2722_device *dev = to_ov2722_sensor(sd);
893
894         if (format->pad)
895                 return -EINVAL;
896         if (!fmt)
897                 return -EINVAL;
898
899         fmt->width = dev->res->width;
900         fmt->height = dev->res->height;
901         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
902
903         return 0;
904 }
905
906 static int ov2722_detect(struct i2c_client *client)
907 {
908         struct i2c_adapter *adapter = client->adapter;
909         u16 high, low;
910         int ret;
911         u16 id;
912         u8 revision;
913
914         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
915                 return -ENODEV;
916
917         ret = ov2722_read_reg(client, OV2722_8BIT,
918                               OV2722_SC_CMMN_CHIP_ID_H, &high);
919         if (ret) {
920                 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
921                 return -ENODEV;
922         }
923         ret = ov2722_read_reg(client, OV2722_8BIT,
924                               OV2722_SC_CMMN_CHIP_ID_L, &low);
925         id = (high << 8) | low;
926
927         if ((id != OV2722_ID) && (id != OV2720_ID)) {
928                 dev_err(&client->dev, "sensor ID error\n");
929                 return -ENODEV;
930         }
931
932         ret = ov2722_read_reg(client, OV2722_8BIT,
933                               OV2722_SC_CMMN_SUB_ID, &high);
934         revision = (u8)high & 0x0f;
935
936         dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
937         dev_dbg(&client->dev, "detect ov2722 success\n");
938         return 0;
939 }
940
941 static int ov2722_s_stream(struct v4l2_subdev *sd, int enable)
942 {
943         struct ov2722_device *dev = to_ov2722_sensor(sd);
944         struct i2c_client *client = v4l2_get_subdevdata(sd);
945         int ret;
946
947         mutex_lock(&dev->input_lock);
948
949         ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM,
950                                enable ? OV2722_START_STREAMING :
951                                OV2722_STOP_STREAMING);
952
953         mutex_unlock(&dev->input_lock);
954         return ret;
955 }
956
957 static int ov2722_s_config(struct v4l2_subdev *sd,
958                            int irq, void *platform_data)
959 {
960         struct ov2722_device *dev = to_ov2722_sensor(sd);
961         struct i2c_client *client = v4l2_get_subdevdata(sd);
962         int ret = 0;
963
964         if (!platform_data)
965                 return -ENODEV;
966
967         dev->platform_data =
968             (struct camera_sensor_platform_data *)platform_data;
969
970         mutex_lock(&dev->input_lock);
971
972         /* power off the module, then power on it in future
973          * as first power on by board may not fulfill the
974          * power on sequqence needed by the module
975          */
976         ret = power_down(sd);
977         if (ret) {
978                 dev_err(&client->dev, "ov2722 power-off err.\n");
979                 goto fail_power_off;
980         }
981
982         ret = power_up(sd);
983         if (ret) {
984                 dev_err(&client->dev, "ov2722 power-up err.\n");
985                 goto fail_power_on;
986         }
987
988         ret = dev->platform_data->csi_cfg(sd, 1);
989         if (ret)
990                 goto fail_csi_cfg;
991
992         /* config & detect sensor */
993         ret = ov2722_detect(client);
994         if (ret) {
995                 dev_err(&client->dev, "ov2722_detect err s_config.\n");
996                 goto fail_csi_cfg;
997         }
998
999         /* turn off sensor, after probed */
1000         ret = power_down(sd);
1001         if (ret) {
1002                 dev_err(&client->dev, "ov2722 power-off err.\n");
1003                 goto fail_csi_cfg;
1004         }
1005         mutex_unlock(&dev->input_lock);
1006
1007         return 0;
1008
1009 fail_csi_cfg:
1010         dev->platform_data->csi_cfg(sd, 0);
1011 fail_power_on:
1012         power_down(sd);
1013         dev_err(&client->dev, "sensor power-gating failed\n");
1014 fail_power_off:
1015         mutex_unlock(&dev->input_lock);
1016         return ret;
1017 }
1018
1019 static int ov2722_g_frame_interval(struct v4l2_subdev *sd,
1020                                    struct v4l2_subdev_frame_interval *interval)
1021 {
1022         struct ov2722_device *dev = to_ov2722_sensor(sd);
1023
1024         interval->interval.numerator = 1;
1025         interval->interval.denominator = dev->res->fps;
1026
1027         return 0;
1028 }
1029
1030 static int ov2722_enum_mbus_code(struct v4l2_subdev *sd,
1031                                  struct v4l2_subdev_state *sd_state,
1032                                  struct v4l2_subdev_mbus_code_enum *code)
1033 {
1034         if (code->index >= MAX_FMTS)
1035                 return -EINVAL;
1036
1037         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1038         return 0;
1039 }
1040
1041 static int ov2722_enum_frame_size(struct v4l2_subdev *sd,
1042                                   struct v4l2_subdev_state *sd_state,
1043                                   struct v4l2_subdev_frame_size_enum *fse)
1044 {
1045         int index = fse->index;
1046
1047         if (index >= N_RES)
1048                 return -EINVAL;
1049
1050         fse->min_width = ov2722_res[index].width;
1051         fse->min_height = ov2722_res[index].height;
1052         fse->max_width = ov2722_res[index].width;
1053         fse->max_height = ov2722_res[index].height;
1054
1055         return 0;
1056 }
1057
1058 static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1059 {
1060         struct ov2722_device *dev = to_ov2722_sensor(sd);
1061
1062         mutex_lock(&dev->input_lock);
1063         *frames = dev->res->skip_frames;
1064         mutex_unlock(&dev->input_lock);
1065
1066         return 0;
1067 }
1068
1069 static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = {
1070         .g_skip_frames  = ov2722_g_skip_frames,
1071 };
1072
1073 static const struct v4l2_subdev_video_ops ov2722_video_ops = {
1074         .s_stream = ov2722_s_stream,
1075         .g_frame_interval = ov2722_g_frame_interval,
1076 };
1077
1078 static const struct v4l2_subdev_core_ops ov2722_core_ops = {
1079         .s_power = ov2722_s_power,
1080         .ioctl = ov2722_ioctl,
1081 };
1082
1083 static const struct v4l2_subdev_pad_ops ov2722_pad_ops = {
1084         .enum_mbus_code = ov2722_enum_mbus_code,
1085         .enum_frame_size = ov2722_enum_frame_size,
1086         .get_fmt = ov2722_get_fmt,
1087         .set_fmt = ov2722_set_fmt,
1088 };
1089
1090 static const struct v4l2_subdev_ops ov2722_ops = {
1091         .core = &ov2722_core_ops,
1092         .video = &ov2722_video_ops,
1093         .pad = &ov2722_pad_ops,
1094         .sensor = &ov2722_sensor_ops,
1095 };
1096
1097 static int ov2722_remove(struct i2c_client *client)
1098 {
1099         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1100         struct ov2722_device *dev = to_ov2722_sensor(sd);
1101
1102         dev->platform_data->csi_cfg(sd, 0);
1103         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1104         v4l2_device_unregister_subdev(sd);
1105
1106         atomisp_gmin_remove_subdev(sd);
1107
1108         media_entity_cleanup(&dev->sd.entity);
1109         kfree(dev);
1110
1111         return 0;
1112 }
1113
1114 static int __ov2722_init_ctrl_handler(struct ov2722_device *dev)
1115 {
1116         struct v4l2_ctrl_handler *hdl;
1117         unsigned int i;
1118
1119         hdl = &dev->ctrl_handler;
1120         v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls));
1121         for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++)
1122                 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i],
1123                                      NULL);
1124
1125         dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ);
1126
1127         if (dev->ctrl_handler.error || !dev->link_freq)
1128                 return dev->ctrl_handler.error;
1129
1130         dev->sd.ctrl_handler = hdl;
1131
1132         return 0;
1133 }
1134
1135 static int ov2722_probe(struct i2c_client *client)
1136 {
1137         struct ov2722_device *dev;
1138         void *ovpdev;
1139         int ret;
1140
1141         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1142         if (!dev)
1143                 return -ENOMEM;
1144
1145         mutex_init(&dev->input_lock);
1146
1147         dev->res = &ov2722_res_preview[0];
1148         v4l2_i2c_subdev_init(&dev->sd, client, &ov2722_ops);
1149
1150         ovpdev = gmin_camera_platform_data(&dev->sd,
1151                                            ATOMISP_INPUT_FORMAT_RAW_10,
1152                                            atomisp_bayer_order_grbg);
1153
1154         ret = ov2722_s_config(&dev->sd, client->irq, ovpdev);
1155         if (ret)
1156                 goto out_free;
1157
1158         ret = __ov2722_init_ctrl_handler(dev);
1159         if (ret)
1160                 goto out_ctrl_handler_free;
1161
1162         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1163         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1164         dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1165         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1166
1167         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1168         if (ret)
1169                 ov2722_remove(client);
1170
1171         return atomisp_register_i2c_module(&dev->sd, ovpdev, RAW_CAMERA);
1172
1173 out_ctrl_handler_free:
1174         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1175
1176 out_free:
1177         v4l2_device_unregister_subdev(&dev->sd);
1178         kfree(dev);
1179         return ret;
1180 }
1181
1182 static const struct acpi_device_id ov2722_acpi_match[] = {
1183         { "INT33FB" },
1184         {},
1185 };
1186 MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match);
1187
1188 static struct i2c_driver ov2722_driver = {
1189         .driver = {
1190                 .name = "ov2722",
1191                 .acpi_match_table = ov2722_acpi_match,
1192         },
1193         .probe_new = ov2722_probe,
1194         .remove = ov2722_remove,
1195 };
1196 module_i2c_driver(ov2722_driver);
1197
1198 MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>");
1199 MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors");
1200 MODULE_LICENSE("GPL");