GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / media / atomisp / i2c / atomisp-gc2235.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for GalaxyCore GC2235 2M camera sensor.
4  *
5  * Copyright (c) 2014 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 "gc2235.h"
37
38 /* i2c read/write stuff */
39 static int gc2235_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 != GC2235_8BIT) {
53                 dev_err(&client->dev, "%s error, invalid data length\n",
54                         __func__);
55                 return -EINVAL;
56         }
57
58         memset(msg, 0, sizeof(msg));
59
60         msg[0].addr = client->addr;
61         msg[0].flags = 0;
62         msg[0].len = 1;
63         msg[0].buf = data;
64
65         /* high byte goes out first */
66         data[0] = (u8)(reg & 0xff);
67
68         msg[1].addr = client->addr;
69         msg[1].len = data_length;
70         msg[1].flags = I2C_M_RD;
71         msg[1].buf = data;
72
73         err = i2c_transfer(client->adapter, msg, 2);
74         if (err != 2) {
75                 if (err >= 0)
76                         err = -EIO;
77                 dev_err(&client->dev,
78                         "read from offset 0x%x error %d", reg, err);
79                 return err;
80         }
81
82         *val = 0;
83         /* high byte comes first */
84         if (data_length == GC2235_8BIT)
85                 *val = (u8)data[0];
86
87         return 0;
88 }
89
90 static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
91 {
92         struct i2c_msg msg;
93         const int num_msg = 1;
94         int ret;
95
96         msg.addr = client->addr;
97         msg.flags = 0;
98         msg.len = len;
99         msg.buf = data;
100         ret = i2c_transfer(client->adapter, &msg, 1);
101
102         return ret == num_msg ? 0 : -EIO;
103 }
104
105 static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
106                             u8 reg, u8 val)
107 {
108         int ret;
109         unsigned char data[4] = {0};
110         const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
111
112         if (data_length != GC2235_8BIT) {
113                 dev_err(&client->dev,
114                         "%s error, invalid data_length\n", __func__);
115                 return -EINVAL;
116         }
117
118         /* high byte goes out first */
119         data[0] = reg;
120         data[1] = val;
121
122         ret = gc2235_i2c_write(client, len, data);
123         if (ret)
124                 dev_err(&client->dev,
125                         "write error: wrote 0x%x to offset 0x%x error %d",
126                         val, reg, ret);
127
128         return ret;
129 }
130
131 static int __gc2235_flush_reg_array(struct i2c_client *client,
132                                     struct gc2235_write_ctrl *ctrl)
133 {
134         u16 size;
135
136         if (ctrl->index == 0)
137                 return 0;
138
139         size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
140         ctrl->index = 0;
141
142         return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
143 }
144
145 static int __gc2235_buf_reg_array(struct i2c_client *client,
146                                   struct gc2235_write_ctrl *ctrl,
147                                   const struct gc2235_reg *next)
148 {
149         int size;
150
151         if (next->type != GC2235_8BIT)
152                 return -EINVAL;
153
154         size = 1;
155         ctrl->buffer.data[ctrl->index] = (u8)next->val;
156
157         /* When first item is added, we need to store its starting address */
158         if (ctrl->index == 0)
159                 ctrl->buffer.addr = next->reg;
160
161         ctrl->index += size;
162
163         /*
164          * Buffer cannot guarantee free space for u32? Better flush it to avoid
165          * possible lack of memory for next item.
166          */
167         if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
168                 return __gc2235_flush_reg_array(client, ctrl);
169
170         return 0;
171 }
172
173 static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
174                                              struct gc2235_write_ctrl *ctrl,
175                                              const struct gc2235_reg *next)
176 {
177         if (ctrl->index == 0)
178                 return 1;
179
180         return ctrl->buffer.addr + ctrl->index == next->reg;
181 }
182
183 static int gc2235_write_reg_array(struct i2c_client *client,
184                                   const struct gc2235_reg *reglist)
185 {
186         const struct gc2235_reg *next = reglist;
187         struct gc2235_write_ctrl ctrl;
188         int err;
189
190         ctrl.index = 0;
191         for (; next->type != GC2235_TOK_TERM; next++) {
192                 switch (next->type & GC2235_TOK_MASK) {
193                 case GC2235_TOK_DELAY:
194                         err = __gc2235_flush_reg_array(client, &ctrl);
195                         if (err)
196                                 return err;
197                         msleep(next->val);
198                         break;
199                 default:
200                         /*
201                          * If next address is not consecutive, data needs to be
202                          * flushed before proceed.
203                          */
204                         if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
205                                                                next)) {
206                                 err = __gc2235_flush_reg_array(client, &ctrl);
207                                 if (err)
208                                         return err;
209                         }
210                         err = __gc2235_buf_reg_array(client, &ctrl, next);
211                         if (err) {
212                                 dev_err(&client->dev, "%s: write error, aborted\n",
213                                         __func__);
214                                 return err;
215                         }
216                         break;
217                 }
218         }
219
220         return __gc2235_flush_reg_array(client, &ctrl);
221 }
222
223 static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val)
224 {
225         *val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM;
226         return 0;
227 }
228
229 static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val)
230 {
231         /* const f number for imx */
232         *val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM;
233         return 0;
234 }
235
236 static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
237 {
238         *val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) |
239                (GC2235_F_NUMBER_DEM << 16) |
240                (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM;
241         return 0;
242 }
243
244 static int gc2235_get_intg_factor(struct i2c_client *client,
245                                   struct camera_mipi_info *info,
246                                   const struct gc2235_resolution *res)
247 {
248         struct v4l2_subdev *sd = i2c_get_clientdata(client);
249         struct gc2235_device *dev = to_gc2235_sensor(sd);
250         struct atomisp_sensor_mode_data *buf = &info->data;
251         u16 reg_val, reg_val_h;
252         int ret;
253
254         if (!info)
255                 return -EINVAL;
256
257         /* pixel clock calculattion */
258         buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000;
259
260         /* get integration time */
261         buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN;
262         buf->coarse_integration_time_max_margin =
263             GC2235_COARSE_INTG_TIME_MAX_MARGIN;
264
265         buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN;
266         buf->fine_integration_time_max_margin =
267             GC2235_FINE_INTG_TIME_MAX_MARGIN;
268
269         buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN;
270         buf->frame_length_lines = res->lines_per_frame;
271         buf->line_length_pck = res->pixels_per_line;
272         buf->read_mode = res->bin_mode;
273
274         /* get the cropping and output resolution to ISP for this mode. */
275         ret =  gc2235_read_reg(client, GC2235_8BIT,
276                                GC2235_H_CROP_START_H, &reg_val_h);
277         ret =  gc2235_read_reg(client, GC2235_8BIT,
278                                GC2235_H_CROP_START_L, &reg_val);
279         if (ret)
280                 return ret;
281
282         buf->crop_horizontal_start = (reg_val_h << 8) | reg_val;
283
284         ret =  gc2235_read_reg(client, GC2235_8BIT,
285                                GC2235_V_CROP_START_H, &reg_val_h);
286         ret =  gc2235_read_reg(client, GC2235_8BIT,
287                                GC2235_V_CROP_START_L, &reg_val);
288         if (ret)
289                 return ret;
290
291         buf->crop_vertical_start = (reg_val_h << 8) | reg_val;
292
293         ret = gc2235_read_reg(client, GC2235_8BIT,
294                               GC2235_H_OUTSIZE_H, &reg_val_h);
295         ret = gc2235_read_reg(client, GC2235_8BIT,
296                               GC2235_H_OUTSIZE_L, &reg_val);
297         if (ret)
298                 return ret;
299         buf->output_width = (reg_val_h << 8) | reg_val;
300
301         ret = gc2235_read_reg(client, GC2235_8BIT,
302                               GC2235_V_OUTSIZE_H, &reg_val_h);
303         ret = gc2235_read_reg(client, GC2235_8BIT,
304                               GC2235_V_OUTSIZE_L, &reg_val);
305         if (ret)
306                 return ret;
307         buf->output_height = (reg_val_h << 8) | reg_val;
308
309         buf->crop_horizontal_end = buf->crop_horizontal_start +
310                                    buf->output_width - 1;
311         buf->crop_vertical_end = buf->crop_vertical_start +
312                                  buf->output_height - 1;
313
314         ret = gc2235_read_reg(client, GC2235_8BIT,
315                               GC2235_HB_H, &reg_val_h);
316         ret = gc2235_read_reg(client, GC2235_8BIT,
317                               GC2235_HB_L, &reg_val);
318         if (ret)
319                 return ret;
320
321 #if 0
322         u16 dummy = (reg_val_h << 8) | reg_val;
323 #endif
324
325         ret = gc2235_read_reg(client, GC2235_8BIT,
326                               GC2235_SH_DELAY_H, &reg_val_h);
327         ret = gc2235_read_reg(client, GC2235_8BIT,
328                               GC2235_SH_DELAY_L, &reg_val);
329
330 #if 0
331         buf->line_length_pck = buf->output_width + 16 + dummy +
332                                (((u16)reg_val_h << 8) | (u16)reg_val) + 4;
333 #endif
334         ret = gc2235_read_reg(client, GC2235_8BIT,
335                               GC2235_VB_H, &reg_val_h);
336         ret = gc2235_read_reg(client, GC2235_8BIT,
337                               GC2235_VB_L, &reg_val);
338         if (ret)
339                 return ret;
340
341 #if 0
342         buf->frame_length_lines = buf->output_height + 32 +
343                                   (((u16)reg_val_h << 8) | (u16)reg_val);
344 #endif
345         buf->binning_factor_x = res->bin_factor_x ?
346                                 res->bin_factor_x : 1;
347         buf->binning_factor_y = res->bin_factor_y ?
348                                 res->bin_factor_y : 1;
349         return 0;
350 }
351
352 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
353                                   int gain, int digitgain)
354
355 {
356         struct i2c_client *client = v4l2_get_subdevdata(sd);
357         u16 coarse_integration = (u16)coarse_itg;
358         int ret = 0;
359         u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
360
361         expo_coarse_h = coarse_integration >> 8;
362         expo_coarse_l = coarse_integration & 0xff;
363
364         ret = gc2235_write_reg(client, GC2235_8BIT,
365                                GC2235_EXPOSURE_H, expo_coarse_h);
366         ret = gc2235_write_reg(client, GC2235_8BIT,
367                                GC2235_EXPOSURE_L, expo_coarse_l);
368
369         if (gain <= 0x58) {
370                 gain_val = 0x40;
371                 gain_val2 = 0x58;
372         } else if (gain < 256) {
373                 gain_val = 0x40;
374                 gain_val2 = gain;
375         } else {
376                 gain_val2 = 64 * gain / 256;
377                 gain_val = 0xff;
378         }
379
380         ret = gc2235_write_reg(client, GC2235_8BIT,
381                                GC2235_GLOBAL_GAIN, (u8)gain_val);
382         ret = gc2235_write_reg(client, GC2235_8BIT,
383                                GC2235_PRE_GAIN, (u8)gain_val2);
384
385         return ret;
386 }
387
388 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
389                                int gain, int digitgain)
390 {
391         struct gc2235_device *dev = to_gc2235_sensor(sd);
392         int ret;
393
394         mutex_lock(&dev->input_lock);
395         ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
396         mutex_unlock(&dev->input_lock);
397
398         return ret;
399 }
400
401 static long gc2235_s_exposure(struct v4l2_subdev *sd,
402                               struct atomisp_exposure *exposure)
403 {
404         int exp = exposure->integration_time[0];
405         int gain = exposure->gain[0];
406         int digitgain = exposure->gain[1];
407
408         /* we should not accept the invalid value below. */
409         if (gain == 0) {
410                 struct i2c_client *client = v4l2_get_subdevdata(sd);
411
412                 v4l2_err(client, "%s: invalid value\n", __func__);
413                 return -EINVAL;
414         }
415
416         return gc2235_set_exposure(sd, exp, gain, digitgain);
417 }
418
419 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
420 {
421         switch (cmd) {
422         case ATOMISP_IOC_S_EXPOSURE:
423                 return gc2235_s_exposure(sd, arg);
424         default:
425                 return -EINVAL;
426         }
427         return 0;
428 }
429
430 /*
431  * This returns the exposure time being used. This should only be used
432  * for filling in EXIF data, not for actual image processing.
433  */
434 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
435 {
436         struct i2c_client *client = v4l2_get_subdevdata(sd);
437         u16 reg_v, reg_v2;
438         int ret;
439
440         /* get exposure */
441         ret = gc2235_read_reg(client, GC2235_8BIT,
442                               GC2235_EXPOSURE_L,
443                               &reg_v);
444         if (ret)
445                 goto err;
446
447         ret = gc2235_read_reg(client, GC2235_8BIT,
448                               GC2235_EXPOSURE_H,
449                               &reg_v2);
450         if (ret)
451                 goto err;
452
453         reg_v += reg_v2 << 8;
454
455         *value = reg_v;
456 err:
457         return ret;
458 }
459
460 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
461 {
462         struct gc2235_device *dev =
463             container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
464         int ret = 0;
465
466         switch (ctrl->id) {
467         case V4L2_CID_EXPOSURE_ABSOLUTE:
468                 ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
469                 break;
470         case V4L2_CID_FOCAL_ABSOLUTE:
471                 ret = gc2235_g_focal(&dev->sd, &ctrl->val);
472                 break;
473         case V4L2_CID_FNUMBER_ABSOLUTE:
474                 ret = gc2235_g_fnumber(&dev->sd, &ctrl->val);
475                 break;
476         case V4L2_CID_FNUMBER_RANGE:
477                 ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val);
478                 break;
479         default:
480                 ret = -EINVAL;
481         }
482
483         return ret;
484 }
485
486 static const struct v4l2_ctrl_ops ctrl_ops = {
487         .g_volatile_ctrl = gc2235_g_volatile_ctrl
488 };
489
490 static struct v4l2_ctrl_config gc2235_controls[] = {
491         {
492                 .ops = &ctrl_ops,
493                 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
494                 .type = V4L2_CTRL_TYPE_INTEGER,
495                 .name = "exposure",
496                 .min = 0x0,
497                 .max = 0xffff,
498                 .step = 0x01,
499                 .def = 0x00,
500                 .flags = 0,
501         },
502         {
503                 .ops = &ctrl_ops,
504                 .id = V4L2_CID_FOCAL_ABSOLUTE,
505                 .type = V4L2_CTRL_TYPE_INTEGER,
506                 .name = "focal length",
507                 .min = GC2235_FOCAL_LENGTH_DEFAULT,
508                 .max = GC2235_FOCAL_LENGTH_DEFAULT,
509                 .step = 0x01,
510                 .def = GC2235_FOCAL_LENGTH_DEFAULT,
511                 .flags = 0,
512         },
513         {
514                 .ops = &ctrl_ops,
515                 .id = V4L2_CID_FNUMBER_ABSOLUTE,
516                 .type = V4L2_CTRL_TYPE_INTEGER,
517                 .name = "f-number",
518                 .min = GC2235_F_NUMBER_DEFAULT,
519                 .max = GC2235_F_NUMBER_DEFAULT,
520                 .step = 0x01,
521                 .def = GC2235_F_NUMBER_DEFAULT,
522                 .flags = 0,
523         },
524         {
525                 .ops = &ctrl_ops,
526                 .id = V4L2_CID_FNUMBER_RANGE,
527                 .type = V4L2_CTRL_TYPE_INTEGER,
528                 .name = "f-number range",
529                 .min = GC2235_F_NUMBER_RANGE,
530                 .max = GC2235_F_NUMBER_RANGE,
531                 .step = 0x01,
532                 .def = GC2235_F_NUMBER_RANGE,
533                 .flags = 0,
534         },
535 };
536
537 static int __gc2235_init(struct v4l2_subdev *sd)
538 {
539         struct i2c_client *client = v4l2_get_subdevdata(sd);
540
541         /* restore settings */
542         gc2235_res = gc2235_res_preview;
543         N_RES = N_RES_PREVIEW;
544
545         return gc2235_write_reg_array(client, gc2235_init_settings);
546 }
547
548 static int is_init;
549
550 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
551 {
552         int ret = -1;
553         struct gc2235_device *dev = to_gc2235_sensor(sd);
554
555         if (!dev || !dev->platform_data)
556                 return -ENODEV;
557
558         if (flag) {
559                 ret = dev->platform_data->v1p8_ctrl(sd, 1);
560                 usleep_range(60, 90);
561                 if (ret == 0)
562                         ret |= dev->platform_data->v2p8_ctrl(sd, 1);
563         } else {
564                 ret = dev->platform_data->v1p8_ctrl(sd, 0);
565                 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
566         }
567         return ret;
568 }
569
570 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
571 {
572         struct gc2235_device *dev = to_gc2235_sensor(sd);
573         int ret;
574
575         if (!dev || !dev->platform_data)
576                 return -ENODEV;
577
578         ret = dev->platform_data->gpio1_ctrl(sd, !flag);
579         usleep_range(60, 90);
580         ret |= dev->platform_data->gpio0_ctrl(sd, flag);
581
582         return ret;
583 }
584
585 static int power_up(struct v4l2_subdev *sd)
586 {
587         struct gc2235_device *dev = to_gc2235_sensor(sd);
588         struct i2c_client *client = v4l2_get_subdevdata(sd);
589         int ret;
590
591         if (!dev->platform_data) {
592                 dev_err(&client->dev,
593                         "no camera_sensor_platform_data");
594                 return -ENODEV;
595         }
596         /* power control */
597         ret = power_ctrl(sd, 1);
598         if (ret)
599                 goto fail_power;
600
601         /* according to DS, at least 5ms is needed between DOVDD and PWDN */
602         usleep_range(5000, 6000);
603
604         ret = dev->platform_data->flisclk_ctrl(sd, 1);
605         if (ret)
606                 goto fail_clk;
607         usleep_range(5000, 6000);
608
609         /* gpio ctrl */
610         ret = gpio_ctrl(sd, 1);
611         if (ret) {
612                 ret = gpio_ctrl(sd, 1);
613                 if (ret)
614                         goto fail_power;
615         }
616
617         msleep(5);
618         return 0;
619
620 fail_clk:
621         gpio_ctrl(sd, 0);
622 fail_power:
623         power_ctrl(sd, 0);
624         dev_err(&client->dev, "sensor power-up failed\n");
625
626         return ret;
627 }
628
629 static int power_down(struct v4l2_subdev *sd)
630 {
631         struct gc2235_device *dev = to_gc2235_sensor(sd);
632         struct i2c_client *client = v4l2_get_subdevdata(sd);
633         int ret = 0;
634
635         if (!dev->platform_data) {
636                 dev_err(&client->dev,
637                         "no camera_sensor_platform_data");
638                 return -ENODEV;
639         }
640         /* gpio ctrl */
641         ret = gpio_ctrl(sd, 0);
642         if (ret) {
643                 ret = gpio_ctrl(sd, 0);
644                 if (ret)
645                         dev_err(&client->dev, "gpio failed 2\n");
646         }
647
648         ret = dev->platform_data->flisclk_ctrl(sd, 0);
649         if (ret)
650                 dev_err(&client->dev, "flisclk failed\n");
651
652         /* power control */
653         ret = power_ctrl(sd, 0);
654         if (ret)
655                 dev_err(&client->dev, "vprog failed.\n");
656
657         return ret;
658 }
659
660 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
661 {
662         int ret;
663
664         if (on == 0) {
665                 ret = power_down(sd);
666         } else {
667                 ret = power_up(sd);
668                 if (!ret)
669                         ret = __gc2235_init(sd);
670                 is_init = 1;
671         }
672         return ret;
673 }
674
675 static int startup(struct v4l2_subdev *sd)
676 {
677         struct gc2235_device *dev = to_gc2235_sensor(sd);
678         struct i2c_client *client = v4l2_get_subdevdata(sd);
679         int ret = 0;
680
681         if (is_init == 0) {
682                 /*
683                  * force gc2235 to do a reset in res change, otherwise it
684                  * can not output normal after switching res. and it is not
685                  * necessary for first time run up after power on, for the sack
686                  * of performance
687                  */
688                 power_down(sd);
689                 power_up(sd);
690                 gc2235_write_reg_array(client, gc2235_init_settings);
691         }
692
693         ret = gc2235_write_reg_array(client, dev->res->regs);
694         if (ret) {
695                 dev_err(&client->dev, "gc2235 write register err.\n");
696                 return ret;
697         }
698         is_init = 0;
699
700         return ret;
701 }
702
703 static int gc2235_set_fmt(struct v4l2_subdev *sd,
704                           struct v4l2_subdev_state *sd_state,
705                           struct v4l2_subdev_format *format)
706 {
707         struct v4l2_mbus_framefmt *fmt = &format->format;
708         struct gc2235_device *dev = to_gc2235_sensor(sd);
709         struct i2c_client *client = v4l2_get_subdevdata(sd);
710         struct camera_mipi_info *gc2235_info = NULL;
711         struct gc2235_resolution *res;
712         int ret = 0;
713
714         gc2235_info = v4l2_get_subdev_hostdata(sd);
715         if (!gc2235_info)
716                 return -EINVAL;
717         if (format->pad)
718                 return -EINVAL;
719         if (!fmt)
720                 return -EINVAL;
721
722         mutex_lock(&dev->input_lock);
723         res = v4l2_find_nearest_size(gc2235_res_preview,
724                                      ARRAY_SIZE(gc2235_res_preview), width,
725                                      height, fmt->width, fmt->height);
726         if (!res)
727                 res = &gc2235_res_preview[N_RES - 1];
728
729         fmt->width = res->width;
730         fmt->height = res->height;
731         dev->res = res;
732
733         fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
734         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
735                 sd_state->pads->try_fmt = *fmt;
736                 mutex_unlock(&dev->input_lock);
737                 return 0;
738         }
739
740         ret = startup(sd);
741         if (ret) {
742                 dev_err(&client->dev, "gc2235 startup err\n");
743                 goto err;
744         }
745
746         ret = gc2235_get_intg_factor(client, gc2235_info,
747                                      dev->res);
748         if (ret)
749                 dev_err(&client->dev, "failed to get integration_factor\n");
750
751 err:
752         mutex_unlock(&dev->input_lock);
753         return ret;
754 }
755
756 static int gc2235_get_fmt(struct v4l2_subdev *sd,
757                           struct v4l2_subdev_state *sd_state,
758                           struct v4l2_subdev_format *format)
759 {
760         struct v4l2_mbus_framefmt *fmt = &format->format;
761         struct gc2235_device *dev = to_gc2235_sensor(sd);
762
763         if (format->pad)
764                 return -EINVAL;
765
766         if (!fmt)
767                 return -EINVAL;
768
769         fmt->width = dev->res->width;
770         fmt->height = dev->res->height;
771         fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
772
773         return 0;
774 }
775
776 static int gc2235_detect(struct i2c_client *client)
777 {
778         struct i2c_adapter *adapter = client->adapter;
779         u16 high = 0, low = 0;
780         u16 id;
781
782         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
783                 return -ENODEV;
784
785         gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high);
786         gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low);
787         id = ((high << 8) | low);
788
789         if (id != GC2235_ID) {
790                 dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
791                 return -ENODEV;
792         }
793
794         dev_info(&client->dev, "detect gc2235 success\n");
795         return 0;
796 }
797
798 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
799 {
800         struct gc2235_device *dev = to_gc2235_sensor(sd);
801         struct i2c_client *client = v4l2_get_subdevdata(sd);
802         int ret;
803
804         mutex_lock(&dev->input_lock);
805
806         if (enable)
807                 ret = gc2235_write_reg_array(client, gc2235_stream_on);
808         else
809                 ret = gc2235_write_reg_array(client, gc2235_stream_off);
810
811         mutex_unlock(&dev->input_lock);
812         return ret;
813 }
814
815 static int gc2235_s_config(struct v4l2_subdev *sd,
816                            int irq, void *platform_data)
817 {
818         struct gc2235_device *dev = to_gc2235_sensor(sd);
819         struct i2c_client *client = v4l2_get_subdevdata(sd);
820         int ret = 0;
821
822         if (!platform_data)
823                 return -ENODEV;
824
825         dev->platform_data =
826             (struct camera_sensor_platform_data *)platform_data;
827
828         mutex_lock(&dev->input_lock);
829         /*
830          * power off the module, then power on it in future
831          * as first power on by board may not fulfill the
832          * power on sequqence needed by the module
833          */
834         ret = power_down(sd);
835         if (ret) {
836                 dev_err(&client->dev, "gc2235 power-off err.\n");
837                 goto fail_power_off;
838         }
839
840         ret = power_up(sd);
841         if (ret) {
842                 dev_err(&client->dev, "gc2235 power-up err.\n");
843                 goto fail_power_on;
844         }
845
846         ret = dev->platform_data->csi_cfg(sd, 1);
847         if (ret)
848                 goto fail_csi_cfg;
849
850         /* config & detect sensor */
851         ret = gc2235_detect(client);
852         if (ret) {
853                 dev_err(&client->dev, "gc2235_detect err s_config.\n");
854                 goto fail_csi_cfg;
855         }
856
857         /* turn off sensor, after probed */
858         ret = power_down(sd);
859         if (ret) {
860                 dev_err(&client->dev, "gc2235 power-off err.\n");
861                 goto fail_csi_cfg;
862         }
863         mutex_unlock(&dev->input_lock);
864
865         return 0;
866
867 fail_csi_cfg:
868         dev->platform_data->csi_cfg(sd, 0);
869 fail_power_on:
870         power_down(sd);
871         dev_err(&client->dev, "sensor power-gating failed\n");
872 fail_power_off:
873         mutex_unlock(&dev->input_lock);
874         return ret;
875 }
876
877 static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
878                                    struct v4l2_subdev_frame_interval *interval)
879 {
880         struct gc2235_device *dev = to_gc2235_sensor(sd);
881
882         interval->interval.numerator = 1;
883         interval->interval.denominator = dev->res->fps;
884
885         return 0;
886 }
887
888 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
889                                  struct v4l2_subdev_state *sd_state,
890                                  struct v4l2_subdev_mbus_code_enum *code)
891 {
892         if (code->index >= MAX_FMTS)
893                 return -EINVAL;
894
895         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
896         return 0;
897 }
898
899 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
900                                   struct v4l2_subdev_state *sd_state,
901                                   struct v4l2_subdev_frame_size_enum *fse)
902 {
903         int index = fse->index;
904
905         if (index >= N_RES)
906                 return -EINVAL;
907
908         fse->min_width = gc2235_res[index].width;
909         fse->min_height = gc2235_res[index].height;
910         fse->max_width = gc2235_res[index].width;
911         fse->max_height = gc2235_res[index].height;
912
913         return 0;
914 }
915
916 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
917 {
918         struct gc2235_device *dev = to_gc2235_sensor(sd);
919
920         mutex_lock(&dev->input_lock);
921         *frames = dev->res->skip_frames;
922         mutex_unlock(&dev->input_lock);
923
924         return 0;
925 }
926
927 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
928         .g_skip_frames  = gc2235_g_skip_frames,
929 };
930
931 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
932         .s_stream = gc2235_s_stream,
933         .g_frame_interval = gc2235_g_frame_interval,
934 };
935
936 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
937         .s_power = gc2235_s_power,
938         .ioctl = gc2235_ioctl,
939 };
940
941 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
942         .enum_mbus_code = gc2235_enum_mbus_code,
943         .enum_frame_size = gc2235_enum_frame_size,
944         .get_fmt = gc2235_get_fmt,
945         .set_fmt = gc2235_set_fmt,
946 };
947
948 static const struct v4l2_subdev_ops gc2235_ops = {
949         .core = &gc2235_core_ops,
950         .video = &gc2235_video_ops,
951         .pad = &gc2235_pad_ops,
952         .sensor = &gc2235_sensor_ops,
953 };
954
955 static int gc2235_remove(struct i2c_client *client)
956 {
957         struct v4l2_subdev *sd = i2c_get_clientdata(client);
958         struct gc2235_device *dev = to_gc2235_sensor(sd);
959
960         dev_dbg(&client->dev, "gc2235_remove...\n");
961
962         dev->platform_data->csi_cfg(sd, 0);
963
964         v4l2_device_unregister_subdev(sd);
965         media_entity_cleanup(&dev->sd.entity);
966         v4l2_ctrl_handler_free(&dev->ctrl_handler);
967         kfree(dev);
968
969         return 0;
970 }
971
972 static int gc2235_probe(struct i2c_client *client)
973 {
974         struct gc2235_device *dev;
975         void *gcpdev;
976         int ret;
977         unsigned int i;
978
979         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
980         if (!dev)
981                 return -ENOMEM;
982
983         mutex_init(&dev->input_lock);
984
985         dev->res = &gc2235_res_preview[0];
986         v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops);
987
988         gcpdev = gmin_camera_platform_data(&dev->sd,
989                                            ATOMISP_INPUT_FORMAT_RAW_10,
990                                            atomisp_bayer_order_grbg);
991
992         ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
993         if (ret)
994                 goto out_free;
995
996         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
997         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
998         dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
999         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1000         ret =
1001             v4l2_ctrl_handler_init(&dev->ctrl_handler,
1002                                    ARRAY_SIZE(gc2235_controls));
1003         if (ret) {
1004                 gc2235_remove(client);
1005                 return ret;
1006         }
1007
1008         for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1009                 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1010                                      NULL);
1011
1012         if (dev->ctrl_handler.error) {
1013                 gc2235_remove(client);
1014                 return dev->ctrl_handler.error;
1015         }
1016
1017         /* Use same lock for controls as for everything else. */
1018         dev->ctrl_handler.lock = &dev->input_lock;
1019         dev->sd.ctrl_handler = &dev->ctrl_handler;
1020
1021         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1022         if (ret)
1023                 gc2235_remove(client);
1024
1025         return atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1026
1027 out_free:
1028         v4l2_device_unregister_subdev(&dev->sd);
1029         kfree(dev);
1030
1031         return ret;
1032 }
1033
1034 static const struct acpi_device_id gc2235_acpi_match[] = {
1035         { "INT33F8" },
1036         {},
1037 };
1038 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1039
1040 static struct i2c_driver gc2235_driver = {
1041         .driver = {
1042                 .name = "gc2235",
1043                 .acpi_match_table = gc2235_acpi_match,
1044         },
1045         .probe_new = gc2235_probe,
1046         .remove = gc2235_remove,
1047 };
1048 module_i2c_driver(gc2235_driver);
1049
1050 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1051 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1052 MODULE_LICENSE("GPL");