OSDN Git Service

4f44113871fe10f7a288ac199498312b1cc33d7b
[tomoyo/tomoyo-test1.git] / drivers / staging / media / atomisp / i2c / atomisp-ov2680.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2680 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 <asm/unaligned.h>
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmod.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/moduleparam.h>
33 #include <media/ov_16bit_addr_reg_helpers.h>
34 #include <media/v4l2-device.h>
35 #include <linux/io.h>
36 #include <linux/acpi.h>
37 #include "../include/linux/atomisp_gmin_platform.h"
38
39 #include "ov2680.h"
40
41 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
42         atomisp_bayer_order_bggr,
43         atomisp_bayer_order_grbg,
44         atomisp_bayer_order_gbrg,
45         atomisp_bayer_order_rggb,
46 };
47
48 static int ov2680_write_reg_array(struct i2c_client *client,
49                                   const struct ov2680_reg *reglist)
50 {
51         const struct ov2680_reg *next = reglist;
52         int ret;
53
54         for (; next->reg != 0; next++) {
55                 ret = ov_write_reg8(client, next->reg, next->val);
56                 if (ret)
57                         return ret;
58         }
59
60         return 0;
61 }
62
63 static void ov2680_set_bayer_order(struct ov2680_device *sensor, struct v4l2_mbus_framefmt *fmt)
64 {
65         static const int ov2680_hv_flip_bayer_order[] = {
66                 MEDIA_BUS_FMT_SBGGR10_1X10,
67                 MEDIA_BUS_FMT_SGRBG10_1X10,
68                 MEDIA_BUS_FMT_SGBRG10_1X10,
69                 MEDIA_BUS_FMT_SRGGB10_1X10,
70         };
71         struct camera_mipi_info *ov2680_info;
72         int hv_flip = 0;
73
74         if (sensor->ctrls.vflip->val)
75                 hv_flip += 1;
76
77         if (sensor->ctrls.hflip->val)
78                 hv_flip += 2;
79
80         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
81
82         /* TODO atomisp specific custom API, should be removed */
83         ov2680_info = v4l2_get_subdev_hostdata(&sensor->sd);
84         if (ov2680_info)
85                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[hv_flip];
86 }
87
88 static int ov2680_set_vflip(struct ov2680_device *sensor, s32 val)
89 {
90         int ret;
91
92         if (sensor->is_streaming)
93                 return -EBUSY;
94
95         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
96         if (ret < 0)
97                 return ret;
98
99         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
100         return 0;
101 }
102
103 static int ov2680_set_hflip(struct ov2680_device *sensor, s32 val)
104 {
105         int ret;
106
107         if (sensor->is_streaming)
108                 return -EBUSY;
109
110         ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
111         if (ret < 0)
112                 return ret;
113
114         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
115         return 0;
116 }
117
118 static int ov2680_exposure_set(struct ov2680_device *sensor, u32 exp)
119 {
120         return ov_write_reg24(sensor->client, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
121 }
122
123 static int ov2680_gain_set(struct ov2680_device *sensor, u32 gain)
124 {
125         return ov_write_reg16(sensor->client, OV2680_REG_GAIN_PK, gain);
126 }
127
128 static int ov2680_test_pattern_set(struct ov2680_device *sensor, int value)
129 {
130         int ret;
131
132         if (!value)
133                 return ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), 0);
134
135         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
136         if (ret < 0)
137                 return ret;
138
139         ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
140         if (ret < 0)
141                 return ret;
142
143         return 0;
144 }
145
146 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
147 {
148         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
149         struct ov2680_device *sensor = to_ov2680_sensor(sd);
150         int ret;
151
152         if (!sensor->power_on) {
153                 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
154                 return 0;
155         }
156
157         switch (ctrl->id) {
158         case V4L2_CID_VFLIP:
159                 ret = ov2680_set_vflip(sensor, ctrl->val);
160                 break;
161         case V4L2_CID_HFLIP:
162                 ret = ov2680_set_hflip(sensor, ctrl->val);
163                 break;
164         case V4L2_CID_EXPOSURE:
165                 ret = ov2680_exposure_set(sensor, ctrl->val);
166                 break;
167         case V4L2_CID_GAIN:
168                 ret = ov2680_gain_set(sensor, ctrl->val);
169                 break;
170         case V4L2_CID_TEST_PATTERN:
171                 ret = ov2680_test_pattern_set(sensor, ctrl->val);
172                 break;
173         default:
174                 ret = -EINVAL;
175         }
176         return ret;
177 }
178
179 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
180         .s_ctrl = ov2680_s_ctrl,
181 };
182
183 static int ov2680_init_registers(struct v4l2_subdev *sd)
184 {
185         struct i2c_client *client = v4l2_get_subdevdata(sd);
186         int ret;
187
188         ret = ov_write_reg8(client, OV2680_SW_RESET, 0x01);
189         ret |= ov2680_write_reg_array(client, ov2680_global_setting);
190
191         return ret;
192 }
193
194 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
195 {
196         int ret = 0;
197         struct ov2680_device *dev = to_ov2680_sensor(sd);
198         struct i2c_client *client = v4l2_get_subdevdata(sd);
199
200         if (!dev || !dev->platform_data)
201                 return -ENODEV;
202
203         dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
204
205         if (flag) {
206                 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
207                 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
208                 usleep_range(10000, 15000);
209         }
210
211         if (!flag || ret) {
212                 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
213                 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
214         }
215         return ret;
216 }
217
218 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
219 {
220         int ret;
221         struct ov2680_device *dev = to_ov2680_sensor(sd);
222
223         if (!dev || !dev->platform_data)
224                 return -ENODEV;
225
226         /*
227          * The OV2680 documents only one GPIO input (#XSHUTDN), but
228          * existing integrations often wire two (reset/power_down)
229          * because that is the way other sensors work.  There is no
230          * way to tell how it is wired internally, so existing
231          * firmwares expose both and we drive them symmetrically.
232          */
233         if (flag) {
234                 ret = dev->platform_data->gpio0_ctrl(sd, 1);
235                 usleep_range(10000, 15000);
236                 /* Ignore return from second gpio, it may not be there */
237                 dev->platform_data->gpio1_ctrl(sd, 1);
238                 usleep_range(10000, 15000);
239         } else {
240                 dev->platform_data->gpio1_ctrl(sd, 0);
241                 ret = dev->platform_data->gpio0_ctrl(sd, 0);
242         }
243         return ret;
244 }
245
246 static int power_up(struct v4l2_subdev *sd)
247 {
248         struct ov2680_device *dev = to_ov2680_sensor(sd);
249         struct i2c_client *client = v4l2_get_subdevdata(sd);
250         int ret;
251
252         if (!dev->platform_data) {
253                 dev_err(&client->dev,
254                         "no camera_sensor_platform_data");
255                 return -ENODEV;
256         }
257
258         if (dev->power_on)
259                 return 0; /* Already on */
260
261         /* power control */
262         ret = power_ctrl(sd, 1);
263         if (ret)
264                 goto fail_power;
265
266         /* according to DS, at least 5ms is needed between DOVDD and PWDN */
267         usleep_range(5000, 6000);
268
269         /* gpio ctrl */
270         ret = gpio_ctrl(sd, 1);
271         if (ret) {
272                 ret = gpio_ctrl(sd, 1);
273                 if (ret)
274                         goto fail_power;
275         }
276
277         /* flis clock control */
278         ret = dev->platform_data->flisclk_ctrl(sd, 1);
279         if (ret)
280                 goto fail_clk;
281
282         /* according to DS, 20ms is needed between PWDN and i2c access */
283         msleep(20);
284
285         ret = ov2680_init_registers(sd);
286         if (ret)
287                 goto fail_init_registers;
288
289         dev->power_on = true;
290         return 0;
291
292 fail_init_registers:
293         dev->platform_data->flisclk_ctrl(sd, 0);
294 fail_clk:
295         gpio_ctrl(sd, 0);
296 fail_power:
297         power_ctrl(sd, 0);
298         dev_err(&client->dev, "sensor power-up failed\n");
299
300         return ret;
301 }
302
303 static int power_down(struct v4l2_subdev *sd)
304 {
305         struct ov2680_device *dev = to_ov2680_sensor(sd);
306         struct i2c_client *client = v4l2_get_subdevdata(sd);
307         int ret = 0;
308
309         if (!dev->platform_data) {
310                 dev_err(&client->dev,
311                         "no camera_sensor_platform_data");
312                 return -ENODEV;
313         }
314
315         if (!dev->power_on)
316                 return 0; /* Already off */
317
318         ret = dev->platform_data->flisclk_ctrl(sd, 0);
319         if (ret)
320                 dev_err(&client->dev, "flisclk failed\n");
321
322         /* gpio ctrl */
323         ret = gpio_ctrl(sd, 0);
324         if (ret) {
325                 ret = gpio_ctrl(sd, 0);
326                 if (ret)
327                         dev_err(&client->dev, "gpio failed 2\n");
328         }
329
330         /* power control */
331         ret = power_ctrl(sd, 0);
332         if (ret) {
333                 dev_err(&client->dev, "vprog failed.\n");
334                 return ret;
335         }
336
337         dev->power_on = false;
338         return 0;
339 }
340
341 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
342 {
343         struct ov2680_device *dev = to_ov2680_sensor(sd);
344         int ret;
345
346         mutex_lock(&dev->input_lock);
347
348         if (on == 0) {
349                 ret = power_down(sd);
350         } else {
351                 ret = power_up(sd);
352         }
353
354         mutex_unlock(&dev->input_lock);
355
356         return ret;
357 }
358
359 static struct v4l2_mbus_framefmt *
360 __ov2680_get_pad_format(struct ov2680_device *sensor,
361                         struct v4l2_subdev_state *state,
362                         unsigned int pad, enum v4l2_subdev_format_whence which)
363 {
364         if (which == V4L2_SUBDEV_FORMAT_TRY)
365                 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
366
367         return &sensor->mode.fmt;
368 }
369
370 static void ov2680_fill_format(struct ov2680_device *sensor,
371                                struct v4l2_mbus_framefmt *fmt,
372                                unsigned int width, unsigned int height)
373 {
374         memset(fmt, 0, sizeof(*fmt));
375         fmt->width = width;
376         fmt->height = height;
377         fmt->field = V4L2_FIELD_NONE;
378         ov2680_set_bayer_order(sensor, fmt);
379 }
380
381 static void ov2680_calc_mode(struct ov2680_device *sensor, int width, int height)
382 {
383         int orig_width = width;
384         int orig_height = height;
385
386         if (width  <= (OV2680_NATIVE_WIDTH / 2) &&
387             height <= (OV2680_NATIVE_HEIGHT / 2)) {
388                 sensor->mode.binning = true;
389                 width *= 2;
390                 height *= 2;
391         } else {
392                 sensor->mode.binning = false;
393         }
394
395         sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
396         sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
397         sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
398                                  OV2680_NATIVE_WIDTH - 1);
399         sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
400                                  OV2680_NATIVE_HEIGHT - 1);
401         sensor->mode.h_output_size = orig_width;
402         sensor->mode.v_output_size = orig_height;
403         sensor->mode.hts = OV2680_PIXELS_PER_LINE;
404         sensor->mode.vts = OV2680_LINES_PER_FRAME;
405 }
406
407 static int ov2680_set_mode(struct ov2680_device *sensor, int width, int height)
408 {
409         struct i2c_client *client = sensor->client;
410         u8 pll_div, unknown, inc, fmt1, fmt2;
411         int ret;
412
413         ov2680_calc_mode(sensor, width, height);
414
415         if (sensor->mode.binning) {
416                 pll_div = 1;
417                 unknown = 0x23;
418                 inc = 0x31;
419                 fmt1 = 0xc2;
420                 fmt2 = 0x01;
421         } else {
422                 pll_div = 0;
423                 unknown = 0x21;
424                 inc = 0x11;
425                 fmt1 = 0xc0;
426                 fmt2 = 0x00;
427         }
428
429         ret = ov_write_reg8(client, 0x3086, pll_div);
430         if (ret)
431                 return ret;
432
433         ret = ov_write_reg8(client, 0x370a, unknown);
434         if (ret)
435                 return ret;
436
437         ret = ov_write_reg16(client, OV2680_HORIZONTAL_START_H, sensor->mode.h_start);
438         if (ret)
439                 return ret;
440
441         ret = ov_write_reg16(client, OV2680_VERTICAL_START_H, sensor->mode.v_start);
442         if (ret)
443                 return ret;
444
445         ret = ov_write_reg16(client, OV2680_HORIZONTAL_END_H, sensor->mode.h_end);
446         if (ret)
447                 return ret;
448
449         ret = ov_write_reg16(client, OV2680_VERTICAL_END_H, sensor->mode.v_end);
450         if (ret)
451                 return ret;
452
453         ret = ov_write_reg16(client, OV2680_HORIZONTAL_OUTPUT_SIZE_H,
454                                  sensor->mode.h_output_size);
455         if (ret)
456                 return ret;
457
458         ret = ov_write_reg16(client, OV2680_VERTICAL_OUTPUT_SIZE_H,
459                                  sensor->mode.v_output_size);
460         if (ret)
461                 return ret;
462
463         ret = ov_write_reg16(client, OV2680_HTS, sensor->mode.hts);
464         if (ret)
465                 return ret;
466
467         ret = ov_write_reg16(client, OV2680_VTS, sensor->mode.vts);
468         if (ret)
469                 return ret;
470
471         ret = ov_write_reg16(client, OV2680_ISP_X_WIN, 0);
472         if (ret)
473                 return ret;
474
475         ret = ov_write_reg16(client, OV2680_ISP_Y_WIN, 0);
476         if (ret)
477                 return ret;
478
479         ret = ov_write_reg8(client, OV2680_X_INC, inc);
480         if (ret)
481                 return ret;
482
483         ret = ov_write_reg8(client, OV2680_Y_INC, inc);
484         if (ret)
485                 return ret;
486
487         ret = ov_write_reg16(client, OV2680_X_WIN, sensor->mode.h_output_size);
488         if (ret)
489                 return ret;
490
491         ret = ov_write_reg16(client, OV2680_Y_WIN, sensor->mode.v_output_size);
492         if (ret)
493                 return ret;
494
495         ret = ov_write_reg8(client, OV2680_REG_FORMAT1, fmt1);
496         if (ret)
497                 return ret;
498
499         ret = ov_write_reg8(client, OV2680_REG_FORMAT2, fmt2);
500         if (ret)
501                 return ret;
502
503         return 0;
504 }
505
506 static int ov2680_set_fmt(struct v4l2_subdev *sd,
507                           struct v4l2_subdev_state *sd_state,
508                           struct v4l2_subdev_format *format)
509 {
510         struct ov2680_device *dev = to_ov2680_sensor(sd);
511         struct i2c_client *client = v4l2_get_subdevdata(sd);
512         struct v4l2_mbus_framefmt *fmt;
513         unsigned int width, height;
514         int ret = 0;
515
516         width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
517         height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
518
519         fmt = __ov2680_get_pad_format(dev, sd_state, format->pad, format->which);
520         ov2680_fill_format(dev, fmt, width, height);
521
522         format->format = *fmt;
523
524         if (format->which == V4L2_SUBDEV_FORMAT_TRY)
525                 return 0;
526
527         dev_dbg(&client->dev, "%s: %dx%d\n",
528                 __func__, fmt->width, fmt->height);
529
530         mutex_lock(&dev->input_lock);
531
532         /* s_power has not been called yet for std v4l2 clients (camorama) */
533         power_up(sd);
534
535         ret = ov2680_set_mode(dev, fmt->width, fmt->height);
536         if (ret < 0)
537                 goto err;
538
539         /* Restore value of all ctrls */
540         ret = __v4l2_ctrl_handler_setup(&dev->ctrls.handler);
541 err:
542         mutex_unlock(&dev->input_lock);
543         return ret;
544 }
545
546 static int ov2680_get_fmt(struct v4l2_subdev *sd,
547                           struct v4l2_subdev_state *sd_state,
548                           struct v4l2_subdev_format *format)
549 {
550         struct ov2680_device *dev = to_ov2680_sensor(sd);
551         struct v4l2_mbus_framefmt *fmt;
552
553         fmt = __ov2680_get_pad_format(dev, sd_state, format->pad, format->which);
554         format->format = *fmt;
555         return 0;
556 }
557
558 static int ov2680_detect(struct i2c_client *client)
559 {
560         struct i2c_adapter *adapter = client->adapter;
561         u32 high, low;
562         int ret;
563         u16 id;
564         u8 revision;
565
566         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
567                 return -ENODEV;
568
569         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_H, &high);
570         if (ret) {
571                 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
572                 return -ENODEV;
573         }
574         ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_L, &low);
575         id = ((((u16)high) << 8) | (u16)low);
576
577         if (id != OV2680_ID) {
578                 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
579                 return -ENODEV;
580         }
581
582         ret = ov_read_reg8(client, OV2680_SC_CMMN_SUB_ID, &high);
583         revision = (u8)high & 0x0f;
584
585         dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
586                  id, revision);
587
588         return 0;
589 }
590
591 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
592 {
593         struct ov2680_device *dev = to_ov2680_sensor(sd);
594         struct i2c_client *client = v4l2_get_subdevdata(sd);
595         int ret;
596
597         mutex_lock(&dev->input_lock);
598         if (enable)
599                 dev_dbg(&client->dev, "ov2680_s_stream one\n");
600         else
601                 dev_dbg(&client->dev, "ov2680_s_stream off\n");
602
603         ret = ov_write_reg8(client, OV2680_SW_STREAM,
604                                 enable ? OV2680_START_STREAMING : OV2680_STOP_STREAMING);
605         if (ret == 0) {
606                 dev->is_streaming = enable;
607                 v4l2_ctrl_activate(dev->ctrls.vflip, !enable);
608                 v4l2_ctrl_activate(dev->ctrls.hflip, !enable);
609         }
610
611         //otp valid at stream on state
612         //if(!dev->otp_data)
613         //      dev->otp_data = ov2680_otp_read(sd);
614
615         mutex_unlock(&dev->input_lock);
616
617         return ret;
618 }
619
620 static int ov2680_s_config(struct v4l2_subdev *sd,
621                            int irq, void *platform_data)
622 {
623         struct ov2680_device *dev = to_ov2680_sensor(sd);
624         struct i2c_client *client = v4l2_get_subdevdata(sd);
625         int ret = 0;
626
627         if (!platform_data)
628                 return -ENODEV;
629
630         dev->platform_data =
631             (struct camera_sensor_platform_data *)platform_data;
632
633         mutex_lock(&dev->input_lock);
634
635         ret = power_up(sd);
636         if (ret) {
637                 dev_err(&client->dev, "ov2680 power-up err.\n");
638                 goto fail_power_on;
639         }
640
641         ret = dev->platform_data->csi_cfg(sd, 1);
642         if (ret)
643                 goto fail_csi_cfg;
644
645         /* config & detect sensor */
646         ret = ov2680_detect(client);
647         if (ret) {
648                 dev_err(&client->dev, "ov2680_detect err s_config.\n");
649                 goto fail_csi_cfg;
650         }
651
652         /* turn off sensor, after probed */
653         ret = power_down(sd);
654         if (ret) {
655                 dev_err(&client->dev, "ov2680 power-off err.\n");
656                 goto fail_csi_cfg;
657         }
658         mutex_unlock(&dev->input_lock);
659
660         return 0;
661
662 fail_csi_cfg:
663         dev->platform_data->csi_cfg(sd, 0);
664 fail_power_on:
665         power_down(sd);
666         dev_err(&client->dev, "sensor power-gating failed\n");
667         mutex_unlock(&dev->input_lock);
668         return ret;
669 }
670
671 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
672                                    struct v4l2_subdev_frame_interval *interval)
673 {
674         interval->interval.numerator = 1;
675         interval->interval.denominator = OV2680_FPS;
676         return 0;
677 }
678
679 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
680                                  struct v4l2_subdev_state *sd_state,
681                                  struct v4l2_subdev_mbus_code_enum *code)
682 {
683         if (code->index >= MAX_FMTS)
684                 return -EINVAL;
685
686         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
687         return 0;
688 }
689
690 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
691                                   struct v4l2_subdev_state *sd_state,
692                                   struct v4l2_subdev_frame_size_enum *fse)
693 {
694         static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
695                 { 1616, 1216 },
696                 { 1616, 1082 },
697                 { 1616,  916 },
698                 { 1456, 1096 },
699                 { 1296,  976 },
700                 { 1296,  736 },
701                 {  800,  600 },
702                 {  720,  592 },
703                 {  656,  496 },
704                 {  336,  256 },
705                 {  352,  288 },
706                 {  176,  144 },
707         };
708         int index = fse->index;
709
710         if (index >= ARRAY_SIZE(ov2680_frame_sizes))
711                 return -EINVAL;
712
713         fse->min_width = ov2680_frame_sizes[index].width;
714         fse->min_height = ov2680_frame_sizes[index].height;
715         fse->max_width = ov2680_frame_sizes[index].width;
716         fse->max_height = ov2680_frame_sizes[index].height;
717
718         return 0;
719 }
720
721 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
722                                       struct v4l2_subdev_state *sd_state,
723                                       struct v4l2_subdev_frame_interval_enum *fie)
724 {
725         /* Only 1 framerate */
726         if (fie->index)
727                 return -EINVAL;
728
729         fie->interval.numerator = 1;
730         fie->interval.denominator = OV2680_FPS;
731         return 0;
732 }
733
734 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
735 {
736         *frames = OV2680_SKIP_FRAMES;
737         return 0;
738 }
739
740 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
741         .s_stream = ov2680_s_stream,
742         .g_frame_interval = ov2680_g_frame_interval,
743 };
744
745 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
746         .g_skip_frames  = ov2680_g_skip_frames,
747 };
748
749 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
750         .s_power = ov2680_s_power,
751 };
752
753 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
754         .enum_mbus_code = ov2680_enum_mbus_code,
755         .enum_frame_size = ov2680_enum_frame_size,
756         .enum_frame_interval = ov2680_enum_frame_interval,
757         .get_fmt = ov2680_get_fmt,
758         .set_fmt = ov2680_set_fmt,
759 };
760
761 static const struct v4l2_subdev_ops ov2680_ops = {
762         .core = &ov2680_core_ops,
763         .video = &ov2680_video_ops,
764         .pad = &ov2680_pad_ops,
765         .sensor = &ov2680_sensor_ops,
766 };
767
768 static int ov2680_init_controls(struct ov2680_device *sensor)
769 {
770         static const char * const test_pattern_menu[] = {
771                 "Disabled",
772                 "Color Bars",
773                 "Random Data",
774                 "Square",
775                 "Black Image",
776         };
777         const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
778         struct ov2680_ctrls *ctrls = &sensor->ctrls;
779         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
780         int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
781
782         v4l2_ctrl_handler_init(hdl, 4);
783
784         hdl->lock = &sensor->input_lock;
785
786         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
787         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
788         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
789                                             0, exp_max, 1, exp_max);
790         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
791         ctrls->test_pattern =
792                 v4l2_ctrl_new_std_menu_items(hdl,
793                                              &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
794                                              ARRAY_SIZE(test_pattern_menu) - 1,
795                                              0, 0, test_pattern_menu);
796
797         ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
798         ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
799
800         if (hdl->error)
801                 return hdl->error;
802
803         sensor->sd.ctrl_handler = hdl;
804         return 0;
805 }
806
807 static void ov2680_remove(struct i2c_client *client)
808 {
809         struct v4l2_subdev *sd = i2c_get_clientdata(client);
810         struct ov2680_device *dev = to_ov2680_sensor(sd);
811
812         dev_dbg(&client->dev, "ov2680_remove...\n");
813
814         dev->platform_data->csi_cfg(sd, 0);
815
816         v4l2_device_unregister_subdev(sd);
817         media_entity_cleanup(&dev->sd.entity);
818         v4l2_ctrl_handler_free(&dev->ctrls.handler);
819         kfree(dev);
820 }
821
822 static int ov2680_probe(struct i2c_client *client)
823 {
824         struct ov2680_device *dev;
825         int ret;
826         void *pdata;
827
828         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
829         if (!dev)
830                 return -ENOMEM;
831
832         mutex_init(&dev->input_lock);
833
834         dev->client = client;
835         v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
836
837         pdata = gmin_camera_platform_data(&dev->sd,
838                                           ATOMISP_INPUT_FORMAT_RAW_10,
839                                           atomisp_bayer_order_bggr);
840         if (!pdata) {
841                 ret = -EINVAL;
842                 goto out_free;
843         }
844
845         ret = ov2680_s_config(&dev->sd, client->irq, pdata);
846         if (ret)
847                 goto out_free;
848
849         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
850         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
851         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
852
853         ret = ov2680_init_controls(dev);
854         if (ret) {
855                 ov2680_remove(client);
856                 return ret;
857         }
858
859         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
860         if (ret) {
861                 ov2680_remove(client);
862                 return ret;
863         }
864
865         ov2680_fill_format(dev, &dev->mode.fmt, OV2680_NATIVE_WIDTH, OV2680_NATIVE_HEIGHT);
866
867         ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
868         if (ret) {
869                 ov2680_remove(client);
870                 return ret;
871         }
872
873         return 0;
874 out_free:
875         dev_dbg(&client->dev, "+++ out free\n");
876         v4l2_device_unregister_subdev(&dev->sd);
877         kfree(dev);
878         return ret;
879 }
880
881 static const struct acpi_device_id ov2680_acpi_match[] = {
882         {"XXOV2680"},
883         {"OVTI2680"},
884         {},
885 };
886 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
887
888 static struct i2c_driver ov2680_driver = {
889         .driver = {
890                 .name = "ov2680",
891                 .acpi_match_table = ov2680_acpi_match,
892         },
893         .probe_new = ov2680_probe,
894         .remove = ov2680_remove,
895 };
896 module_i2c_driver(ov2680_driver);
897
898 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
899 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
900 MODULE_LICENSE("GPL");