]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/media/atomisp/i2c/atomisp-ov2680.c
c81e80e7bdea50e13af9ea2d59ae6b5fc4c5db6c
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / media / atomisp / i2c / atomisp-ov2680.c
1 /*
2 * Support for OmniVision OV2680 1080p HD camera sensor.
3 *
4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
32 #include <linux/io.h>
33 #include <linux/acpi.h>
34 #include "../include/linux/atomisp_gmin_platform.h"
35
36 #include "ov2680.h"
37
38 static int h_flag = 0;
39 static int v_flag = 0;
40 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
41 atomisp_bayer_order_bggr,
42 atomisp_bayer_order_grbg,
43 atomisp_bayer_order_gbrg,
44 atomisp_bayer_order_rggb,
45 };
46
47 /* i2c read/write stuff */
48 static int ov2680_read_reg(struct i2c_client *client,
49 u16 data_length, u16 reg, u16 *val)
50 {
51 int err;
52 struct i2c_msg msg[2];
53 unsigned char data[6];
54
55 if (!client->adapter) {
56 dev_err(&client->dev, "%s error, no client->adapter\n",
57 __func__);
58 return -ENODEV;
59 }
60
61 if (data_length != OV2680_8BIT && data_length != OV2680_16BIT
62 && data_length != OV2680_32BIT) {
63 dev_err(&client->dev, "%s error, invalid data length\n",
64 __func__);
65 return -EINVAL;
66 }
67
68 memset(msg, 0 , sizeof(msg));
69
70 msg[0].addr = client->addr;
71 msg[0].flags = 0;
72 msg[0].len = I2C_MSG_LENGTH;
73 msg[0].buf = data;
74
75 /* high byte goes out first */
76 data[0] = (u8)(reg >> 8);
77 data[1] = (u8)(reg & 0xff);
78
79 msg[1].addr = client->addr;
80 msg[1].len = data_length;
81 msg[1].flags = I2C_M_RD;
82 msg[1].buf = data;
83
84 err = i2c_transfer(client->adapter, msg, 2);
85 if (err != 2) {
86 if (err >= 0)
87 err = -EIO;
88 dev_err(&client->dev,
89 "read from offset 0x%x error %d", reg, err);
90 return err;
91 }
92
93 *val = 0;
94 /* high byte comes first */
95 if (data_length == OV2680_8BIT)
96 *val = (u8)data[0];
97 else if (data_length == OV2680_16BIT)
98 *val = be16_to_cpu(*(u16 *)&data[0]);
99 else
100 *val = be32_to_cpu(*(u32 *)&data[0]);
101 //dev_dbg(&client->dev, "++++i2c read adr%x = %x\n", reg,*val);
102 return 0;
103 }
104
105 static int ov2680_i2c_write(struct i2c_client *client, u16 len, u8 *data)
106 {
107 struct i2c_msg msg;
108 const int num_msg = 1;
109 int ret;
110
111 msg.addr = client->addr;
112 msg.flags = 0;
113 msg.len = len;
114 msg.buf = data;
115 ret = i2c_transfer(client->adapter, &msg, 1);
116 //dev_dbg(&client->dev, "+++i2c write reg=%x->%x\n", data[0]*256 +data[1],data[2]);
117 return ret == num_msg ? 0 : -EIO;
118 }
119
120 static int ov2680_write_reg(struct i2c_client *client, u16 data_length,
121 u16 reg, u16 val)
122 {
123 int ret;
124 unsigned char data[4] = {0};
125 u16 *wreg = (u16 *)data;
126 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
127
128 if (data_length != OV2680_8BIT && data_length != OV2680_16BIT) {
129 dev_err(&client->dev,
130 "%s error, invalid data_length\n", __func__);
131 return -EINVAL;
132 }
133
134 /* high byte goes out first */
135 *wreg = cpu_to_be16(reg);
136
137 if (data_length == OV2680_8BIT) {
138 data[2] = (u8)(val);
139 } else {
140 /* OV2680_16BIT */
141 u16 *wdata = (u16 *)&data[2];
142 *wdata = cpu_to_be16(val);
143 }
144
145 ret = ov2680_i2c_write(client, len, data);
146 if (ret)
147 dev_err(&client->dev,
148 "write error: wrote 0x%x to offset 0x%x error %d",
149 val, reg, ret);
150
151 return ret;
152 }
153
154 /*
155 * ov2680_write_reg_array - Initializes a list of OV2680 registers
156 * @client: i2c driver client structure
157 * @reglist: list of registers to be written
158 *
159 * This function initializes a list of registers. When consecutive addresses
160 * are found in a row on the list, this function creates a buffer and sends
161 * consecutive data in a single i2c_transfer().
162 *
163 * __ov2680_flush_reg_array, __ov2680_buf_reg_array() and
164 * __ov2680_write_reg_is_consecutive() are internal functions to
165 * ov2680_write_reg_array_fast() and should be not used anywhere else.
166 *
167 */
168
169 static int __ov2680_flush_reg_array(struct i2c_client *client,
170 struct ov2680_write_ctrl *ctrl)
171 {
172 u16 size;
173
174 if (ctrl->index == 0)
175 return 0;
176
177 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
178 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
179 ctrl->index = 0;
180
181 return ov2680_i2c_write(client, size, (u8 *)&ctrl->buffer);
182 }
183
184 static int __ov2680_buf_reg_array(struct i2c_client *client,
185 struct ov2680_write_ctrl *ctrl,
186 const struct ov2680_reg *next)
187 {
188 int size;
189 u16 *data16;
190
191 switch (next->type) {
192 case OV2680_8BIT:
193 size = 1;
194 ctrl->buffer.data[ctrl->index] = (u8)next->val;
195 break;
196 case OV2680_16BIT:
197 size = 2;
198 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
199 *data16 = cpu_to_be16((u16)next->val);
200 break;
201 default:
202 return -EINVAL;
203 }
204
205 /* When first item is added, we need to store its starting address */
206 if (ctrl->index == 0)
207 ctrl->buffer.addr = next->reg;
208
209 ctrl->index += size;
210
211 /*
212 * Buffer cannot guarantee free space for u32? Better flush it to avoid
213 * possible lack of memory for next item.
214 */
215 if (ctrl->index + sizeof(u16) >= OV2680_MAX_WRITE_BUF_SIZE)
216 return __ov2680_flush_reg_array(client, ctrl);
217
218 return 0;
219 }
220
221 static int __ov2680_write_reg_is_consecutive(struct i2c_client *client,
222 struct ov2680_write_ctrl *ctrl,
223 const struct ov2680_reg *next)
224 {
225 if (ctrl->index == 0)
226 return 1;
227
228 return ctrl->buffer.addr + ctrl->index == next->reg;
229 }
230
231 static int ov2680_write_reg_array(struct i2c_client *client,
232 const struct ov2680_reg *reglist)
233 {
234 const struct ov2680_reg *next = reglist;
235 struct ov2680_write_ctrl ctrl;
236 int err;
237 dev_dbg(&client->dev, "++++write reg array\n");
238 ctrl.index = 0;
239 for (; next->type != OV2680_TOK_TERM; next++) {
240 switch (next->type & OV2680_TOK_MASK) {
241 case OV2680_TOK_DELAY:
242 err = __ov2680_flush_reg_array(client, &ctrl);
243 if (err)
244 return err;
245 msleep(next->val);
246 break;
247 default:
248 /*
249 * If next address is not consecutive, data needs to be
250 * flushed before proceed.
251 */
252 dev_dbg(&client->dev, "+++ov2680_write_reg_array reg=%x->%x\n", next->reg,next->val);
253 if (!__ov2680_write_reg_is_consecutive(client, &ctrl,
254 next)) {
255 err = __ov2680_flush_reg_array(client, &ctrl);
256 if (err)
257 return err;
258 }
259 err = __ov2680_buf_reg_array(client, &ctrl, next);
260 if (err) {
261 dev_err(&client->dev, "%s: write error, aborted\n",
262 __func__);
263 return err;
264 }
265 break;
266 }
267 }
268
269 return __ov2680_flush_reg_array(client, &ctrl);
270 }
271 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
272 {
273
274 *val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
275 return 0;
276 }
277
278 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
279 {
280 /*const f number for ov2680*/
281
282 *val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
283 return 0;
284 }
285
286 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
287 {
288 *val = (OV2680_F_NUMBER_DEFAULT_NUM << 24) |
289 (OV2680_F_NUMBER_DEM << 16) |
290 (OV2680_F_NUMBER_DEFAULT_NUM << 8) | OV2680_F_NUMBER_DEM;
291 return 0;
292 }
293
294 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
295 {
296 struct ov2680_device *dev = to_ov2680_sensor(sd);
297 struct i2c_client *client = v4l2_get_subdevdata(sd);
298 dev_dbg(&client->dev, "++++ov2680_g_bin_factor_x\n");
299 *val = ov2680_res[dev->fmt_idx].bin_factor_x;
300
301 return 0;
302 }
303
304 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
305 {
306 struct ov2680_device *dev = to_ov2680_sensor(sd);
307 struct i2c_client *client = v4l2_get_subdevdata(sd);
308
309 *val = ov2680_res[dev->fmt_idx].bin_factor_y;
310 dev_dbg(&client->dev, "++++ov2680_g_bin_factor_y\n");
311 return 0;
312 }
313
314
315 static int ov2680_get_intg_factor(struct i2c_client *client,
316 struct camera_mipi_info *info,
317 const struct ov2680_resolution *res)
318 {
319 struct v4l2_subdev *sd = i2c_get_clientdata(client);
320 struct ov2680_device *dev = to_ov2680_sensor(sd);
321 struct atomisp_sensor_mode_data *buf = &info->data;
322 unsigned int pix_clk_freq_hz;
323 u16 reg_val;
324 int ret;
325 dev_dbg(&client->dev, "++++ov2680_get_intg_factor\n");
326 if (!info)
327 return -EINVAL;
328
329 /* pixel clock */
330 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
331
332 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
333 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
334
335 /* get integration time */
336 buf->coarse_integration_time_min = OV2680_COARSE_INTG_TIME_MIN;
337 buf->coarse_integration_time_max_margin =
338 OV2680_COARSE_INTG_TIME_MAX_MARGIN;
339
340 buf->fine_integration_time_min = OV2680_FINE_INTG_TIME_MIN;
341 buf->fine_integration_time_max_margin =
342 OV2680_FINE_INTG_TIME_MAX_MARGIN;
343
344 buf->fine_integration_time_def = OV2680_FINE_INTG_TIME_MIN;
345 buf->frame_length_lines = res->lines_per_frame;
346 buf->line_length_pck = res->pixels_per_line;
347 buf->read_mode = res->bin_mode;
348
349 /* get the cropping and output resolution to ISP for this mode. */
350 ret = ov2680_read_reg(client, OV2680_16BIT,
351 OV2680_HORIZONTAL_START_H, &reg_val);
352 if (ret)
353 return ret;
354 buf->crop_horizontal_start = reg_val;
355
356 ret = ov2680_read_reg(client, OV2680_16BIT,
357 OV2680_VERTICAL_START_H, &reg_val);
358 if (ret)
359 return ret;
360 buf->crop_vertical_start = reg_val;
361
362 ret = ov2680_read_reg(client, OV2680_16BIT,
363 OV2680_HORIZONTAL_END_H, &reg_val);
364 if (ret)
365 return ret;
366 buf->crop_horizontal_end = reg_val;
367
368 ret = ov2680_read_reg(client, OV2680_16BIT,
369 OV2680_VERTICAL_END_H, &reg_val);
370 if (ret)
371 return ret;
372 buf->crop_vertical_end = reg_val;
373
374 ret = ov2680_read_reg(client, OV2680_16BIT,
375 OV2680_HORIZONTAL_OUTPUT_SIZE_H, &reg_val);
376 if (ret)
377 return ret;
378 buf->output_width = reg_val;
379
380 ret = ov2680_read_reg(client, OV2680_16BIT,
381 OV2680_VERTICAL_OUTPUT_SIZE_H, &reg_val);
382 if (ret)
383 return ret;
384 buf->output_height = reg_val;
385
386 buf->binning_factor_x = res->bin_factor_x ?
387 (res->bin_factor_x * 2) : 1;
388 buf->binning_factor_y = res->bin_factor_y ?
389 (res->bin_factor_y * 2) : 1;
390 return 0;
391 }
392
393 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
394 int gain, int digitgain)
395
396 {
397 struct i2c_client *client = v4l2_get_subdevdata(sd);
398 struct ov2680_device *dev = to_ov2680_sensor(sd);
399 u16 vts,hts;
400 int ret,exp_val;
401
402 dev_dbg(&client->dev, "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",coarse_itg, gain, digitgain);
403
404 hts = ov2680_res[dev->fmt_idx].pixels_per_line;
405 vts = ov2680_res[dev->fmt_idx].lines_per_frame;
406
407 /* group hold */
408 ret = ov2680_write_reg(client, OV2680_8BIT,
409 OV2680_GROUP_ACCESS, 0x00);
410 if (ret) {
411 dev_err(&client->dev, "%s: write %x error, aborted\n",
412 __func__, OV2680_GROUP_ACCESS);
413 return ret;
414 }
415
416 /* Increase the VTS to match exposure + MARGIN */
417 if (coarse_itg > vts - OV2680_INTEGRATION_TIME_MARGIN)
418 vts = (u16) coarse_itg + OV2680_INTEGRATION_TIME_MARGIN;
419
420 ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_TIMING_VTS_H, vts);
421 if (ret) {
422 dev_err(&client->dev, "%s: write %x error, aborted\n",
423 __func__, OV2680_TIMING_VTS_H);
424 return ret;
425 }
426
427 /* set exposure */
428
429 /* Lower four bit should be 0*/
430 exp_val = coarse_itg << 4;
431 ret = ov2680_write_reg(client, OV2680_8BIT,
432 OV2680_EXPOSURE_L, exp_val & 0xFF);
433 if (ret) {
434 dev_err(&client->dev, "%s: write %x error, aborted\n",
435 __func__, OV2680_EXPOSURE_L);
436 return ret;
437 }
438
439 ret = ov2680_write_reg(client, OV2680_8BIT,
440 OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
441 if (ret) {
442 dev_err(&client->dev, "%s: write %x error, aborted\n",
443 __func__, OV2680_EXPOSURE_M);
444 return ret;
445 }
446
447 ret = ov2680_write_reg(client, OV2680_8BIT,
448 OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
449 if (ret) {
450 dev_err(&client->dev, "%s: write %x error, aborted\n",
451 __func__, OV2680_EXPOSURE_H);
452 return ret;
453 }
454
455 /* Analog gain */
456 ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_AGC_H, gain);
457 if (ret) {
458 dev_err(&client->dev, "%s: write %x error, aborted\n",
459 __func__, OV2680_AGC_H);
460 return ret;
461 }
462 /* Digital gain */
463 if (digitgain) {
464 ret = ov2680_write_reg(client, OV2680_16BIT,
465 OV2680_MWB_RED_GAIN_H, digitgain);
466 if (ret) {
467 dev_err(&client->dev, "%s: write %x error, aborted\n",
468 __func__, OV2680_MWB_RED_GAIN_H);
469 return ret;
470 }
471
472 ret = ov2680_write_reg(client, OV2680_16BIT,
473 OV2680_MWB_GREEN_GAIN_H, digitgain);
474 if (ret) {
475 dev_err(&client->dev, "%s: write %x error, aborted\n",
476 __func__, OV2680_MWB_RED_GAIN_H);
477 return ret;
478 }
479
480 ret = ov2680_write_reg(client, OV2680_16BIT,
481 OV2680_MWB_BLUE_GAIN_H, digitgain);
482 if (ret) {
483 dev_err(&client->dev, "%s: write %x error, aborted\n",
484 __func__, OV2680_MWB_RED_GAIN_H);
485 return ret;
486 }
487 }
488
489 /* End group */
490 ret = ov2680_write_reg(client, OV2680_8BIT,
491 OV2680_GROUP_ACCESS, 0x10);
492 if (ret)
493 return ret;
494
495 /* Delay launch group */
496 ret = ov2680_write_reg(client, OV2680_8BIT,
497 OV2680_GROUP_ACCESS, 0xa0);
498 if (ret)
499 return ret;
500 return ret;
501 }
502
503 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
504 int gain, int digitgain)
505 {
506 struct ov2680_device *dev = to_ov2680_sensor(sd);
507 int ret;
508
509 mutex_lock(&dev->input_lock);
510 ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
511 mutex_unlock(&dev->input_lock);
512
513 return ret;
514 }
515
516 static long ov2680_s_exposure(struct v4l2_subdev *sd,
517 struct atomisp_exposure *exposure)
518 {
519 u16 coarse_itg = exposure->integration_time[0];
520 u16 analog_gain = exposure->gain[0];
521 u16 digital_gain = exposure->gain[1];
522
523 /* we should not accept the invalid value below */
524 if (analog_gain == 0) {
525 struct i2c_client *client = v4l2_get_subdevdata(sd);
526 v4l2_err(client, "%s: invalid value\n", __func__);
527 return -EINVAL;
528 }
529
530 // EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
531 return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
532 }
533
534
535
536
537
538 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
539 {
540
541 switch (cmd) {
542 case ATOMISP_IOC_S_EXPOSURE:
543 return ov2680_s_exposure(sd, arg);
544
545 default:
546 return -EINVAL;
547 }
548 return 0;
549 }
550
551 /* This returns the exposure time being used. This should only be used
552 * for filling in EXIF data, not for actual image processing.
553 */
554 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
555 {
556 struct i2c_client *client = v4l2_get_subdevdata(sd);
557 u16 reg_v, reg_v2;
558 int ret;
559
560 /* get exposure */
561 ret = ov2680_read_reg(client, OV2680_8BIT,
562 OV2680_EXPOSURE_L,
563 &reg_v);
564 if (ret)
565 goto err;
566
567 ret = ov2680_read_reg(client, OV2680_8BIT,
568 OV2680_EXPOSURE_M,
569 &reg_v2);
570 if (ret)
571 goto err;
572
573 reg_v += reg_v2 << 8;
574 ret = ov2680_read_reg(client, OV2680_8BIT,
575 OV2680_EXPOSURE_H,
576 &reg_v2);
577 if (ret)
578 goto err;
579
580 *value = reg_v + (((u32)reg_v2 << 16));
581 err:
582 return ret;
583 }
584
585 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order code)
586 {
587 switch (code) {
588 case atomisp_bayer_order_rggb:
589 return MEDIA_BUS_FMT_SRGGB10_1X10;
590 case atomisp_bayer_order_grbg:
591 return MEDIA_BUS_FMT_SGRBG10_1X10;
592 case atomisp_bayer_order_bggr:
593 return MEDIA_BUS_FMT_SBGGR10_1X10;
594 case atomisp_bayer_order_gbrg:
595 return MEDIA_BUS_FMT_SGBRG10_1X10;
596 }
597 return 0;
598 }
599
600 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
601 {
602 struct ov2680_device *dev = to_ov2680_sensor(sd);
603 struct camera_mipi_info *ov2680_info = NULL;
604 struct i2c_client *client = v4l2_get_subdevdata(sd);
605 int ret;
606 u16 val;
607 u8 index;
608 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
609 ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_FLIP_REG, &val);
610 if (ret)
611 return ret;
612 if (value) {
613 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
614 } else {
615 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
616 }
617 ret = ov2680_write_reg(client, OV2680_8BIT,
618 OV2680_FLIP_REG, val);
619 if (ret)
620 return ret;
621 index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
622 ov2680_info = v4l2_get_subdev_hostdata(sd);
623 if (ov2680_info) {
624 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
625 dev->format.code = ov2680_translate_bayer_order(
626 ov2680_info->raw_bayer_order);
627 }
628 return ret;
629 }
630
631 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
632 {
633 struct ov2680_device *dev = to_ov2680_sensor(sd);
634 struct camera_mipi_info *ov2680_info = NULL;
635 struct i2c_client *client = v4l2_get_subdevdata(sd);
636 int ret;
637 u16 val;
638 u8 index;
639 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
640
641 ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_MIRROR_REG, &val);
642 if (ret)
643 return ret;
644 if (value) {
645 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
646 } else {
647 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
648 }
649 ret = ov2680_write_reg(client, OV2680_8BIT,
650 OV2680_MIRROR_REG, val);
651 if (ret)
652 return ret;
653 index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
654 ov2680_info = v4l2_get_subdev_hostdata(sd);
655 if (ov2680_info) {
656 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
657 dev->format.code = ov2680_translate_bayer_order(
658 ov2680_info->raw_bayer_order);
659 }
660 return ret;
661 }
662
663 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
664 {
665 struct ov2680_device *dev =
666 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
667 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
668 int ret = 0;
669
670 switch (ctrl->id) {
671 case V4L2_CID_VFLIP:
672 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
673 __func__, ctrl->val);
674 ret = ov2680_v_flip(&dev->sd, ctrl->val);
675 break;
676 case V4L2_CID_HFLIP:
677 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
678 __func__, ctrl->val);
679 ret = ov2680_h_flip(&dev->sd, ctrl->val);
680 break;
681 default:
682 ret = -EINVAL;
683 }
684 return ret;
685 }
686
687 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
688 {
689 struct ov2680_device *dev =
690 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
691 int ret = 0;
692
693 switch (ctrl->id) {
694 case V4L2_CID_EXPOSURE_ABSOLUTE:
695 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
696 break;
697 case V4L2_CID_FOCAL_ABSOLUTE:
698 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
699 break;
700 case V4L2_CID_FNUMBER_ABSOLUTE:
701 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
702 break;
703 case V4L2_CID_FNUMBER_RANGE:
704 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
705 break;
706 case V4L2_CID_BIN_FACTOR_HORZ:
707 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
708 break;
709 case V4L2_CID_BIN_FACTOR_VERT:
710 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
711 break;
712 default:
713 ret = -EINVAL;
714 }
715
716 return ret;
717 }
718
719 static const struct v4l2_ctrl_ops ctrl_ops = {
720 .s_ctrl = ov2680_s_ctrl,
721 .g_volatile_ctrl = ov2680_g_volatile_ctrl
722 };
723
724 struct v4l2_ctrl_config ov2680_controls[] = {
725 {
726 .ops = &ctrl_ops,
727 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
728 .type = V4L2_CTRL_TYPE_INTEGER,
729 .name = "exposure",
730 .min = 0x0,
731 .max = 0xffff,
732 .step = 0x01,
733 .def = 0x00,
734 .flags = 0,
735 },
736 {
737 .ops = &ctrl_ops,
738 .id = V4L2_CID_FOCAL_ABSOLUTE,
739 .type = V4L2_CTRL_TYPE_INTEGER,
740 .name = "focal length",
741 .min = OV2680_FOCAL_LENGTH_DEFAULT,
742 .max = OV2680_FOCAL_LENGTH_DEFAULT,
743 .step = 0x01,
744 .def = OV2680_FOCAL_LENGTH_DEFAULT,
745 .flags = 0,
746 },
747 {
748 .ops = &ctrl_ops,
749 .id = V4L2_CID_FNUMBER_ABSOLUTE,
750 .type = V4L2_CTRL_TYPE_INTEGER,
751 .name = "f-number",
752 .min = OV2680_F_NUMBER_DEFAULT,
753 .max = OV2680_F_NUMBER_DEFAULT,
754 .step = 0x01,
755 .def = OV2680_F_NUMBER_DEFAULT,
756 .flags = 0,
757 },
758 {
759 .ops = &ctrl_ops,
760 .id = V4L2_CID_FNUMBER_RANGE,
761 .type = V4L2_CTRL_TYPE_INTEGER,
762 .name = "f-number range",
763 .min = OV2680_F_NUMBER_RANGE,
764 .max = OV2680_F_NUMBER_RANGE,
765 .step = 0x01,
766 .def = OV2680_F_NUMBER_RANGE,
767 .flags = 0,
768 },
769 {
770 .ops = &ctrl_ops,
771 .id = V4L2_CID_BIN_FACTOR_HORZ,
772 .type = V4L2_CTRL_TYPE_INTEGER,
773 .name = "horizontal binning factor",
774 .min = 0,
775 .max = OV2680_BIN_FACTOR_MAX,
776 .step = 1,
777 .def = 0,
778 .flags = 0,
779 },
780 {
781 .ops = &ctrl_ops,
782 .id = V4L2_CID_BIN_FACTOR_VERT,
783 .type = V4L2_CTRL_TYPE_INTEGER,
784 .name = "vertical binning factor",
785 .min = 0,
786 .max = OV2680_BIN_FACTOR_MAX,
787 .step = 1,
788 .def = 0,
789 .flags = 0,
790 },
791 {
792 .ops = &ctrl_ops,
793 .id = V4L2_CID_VFLIP,
794 .type = V4L2_CTRL_TYPE_BOOLEAN,
795 .name = "Flip",
796 .min = 0,
797 .max = 1,
798 .step = 1,
799 .def = 0,
800 },
801 {
802 .ops = &ctrl_ops,
803 .id = V4L2_CID_HFLIP,
804 .type = V4L2_CTRL_TYPE_BOOLEAN,
805 .name = "Mirror",
806 .min = 0,
807 .max = 1,
808 .step = 1,
809 .def = 0,
810 },
811 };
812
813 static int ov2680_init_registers(struct v4l2_subdev *sd)
814 {
815 struct i2c_client *client = v4l2_get_subdevdata(sd);
816 int ret;
817
818 ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_RESET, 0x01);
819 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
820
821 return ret;
822 }
823
824 static int ov2680_init(struct v4l2_subdev *sd)
825 {
826 struct ov2680_device *dev = to_ov2680_sensor(sd);
827
828 int ret;
829
830 mutex_lock(&dev->input_lock);
831
832 /* restore settings */
833 ov2680_res = ov2680_res_preview;
834 N_RES = N_RES_PREVIEW;
835
836 ret = ov2680_init_registers(sd);
837
838 mutex_unlock(&dev->input_lock);
839
840 return ret;
841 }
842
843 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
844 {
845 int ret = 0;
846 struct ov2680_device *dev = to_ov2680_sensor(sd);
847 if (!dev || !dev->platform_data)
848 return -ENODEV;
849
850 /* Non-gmin platforms use the legacy callback */
851 if (dev->platform_data->power_ctrl)
852 return dev->platform_data->power_ctrl(sd, flag);
853
854 if (flag) {
855 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
856 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
857 usleep_range(10000, 15000);
858 }
859
860 if (!flag || ret) {
861 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
862 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
863 }
864 return ret;
865 }
866
867 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
868 {
869 int ret;
870 struct ov2680_device *dev = to_ov2680_sensor(sd);
871
872 if (!dev || !dev->platform_data)
873 return -ENODEV;
874
875 /* Non-gmin platforms use the legacy callback */
876 if (dev->platform_data->gpio_ctrl)
877 return dev->platform_data->gpio_ctrl(sd, flag);
878
879 /* The OV2680 documents only one GPIO input (#XSHUTDN), but
880 * existing integrations often wire two (reset/power_down)
881 * because that is the way other sensors work. There is no
882 * way to tell how it is wired internally, so existing
883 * firmwares expose both and we drive them symmetrically. */
884 if (flag) {
885 ret = dev->platform_data->gpio0_ctrl(sd, 1);
886 usleep_range(10000, 15000);
887 /* Ignore return from second gpio, it may not be there */
888 dev->platform_data->gpio1_ctrl(sd, 1);
889 usleep_range(10000, 15000);
890 } else {
891 dev->platform_data->gpio1_ctrl(sd, 0);
892 ret = dev->platform_data->gpio0_ctrl(sd, 0);
893 }
894 return ret;
895 }
896
897 static int power_up(struct v4l2_subdev *sd)
898 {
899 struct ov2680_device *dev = to_ov2680_sensor(sd);
900 struct i2c_client *client = v4l2_get_subdevdata(sd);
901 int ret;
902
903 if (!dev->platform_data) {
904 dev_err(&client->dev,
905 "no camera_sensor_platform_data");
906 return -ENODEV;
907 }
908
909 /* power control */
910 ret = power_ctrl(sd, 1);
911 if (ret)
912 goto fail_power;
913
914 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
915 usleep_range(5000, 6000);
916
917 /* gpio ctrl */
918 ret = gpio_ctrl(sd, 1);
919 if (ret) {
920 ret = gpio_ctrl(sd, 1);
921 if (ret)
922 goto fail_power;
923 }
924
925 /* flis clock control */
926 ret = dev->platform_data->flisclk_ctrl(sd, 1);
927 if (ret)
928 goto fail_clk;
929
930 /* according to DS, 20ms is needed between PWDN and i2c access */
931 msleep(20);
932
933 return 0;
934
935 fail_clk:
936 gpio_ctrl(sd, 0);
937 fail_power:
938 power_ctrl(sd, 0);
939 dev_err(&client->dev, "sensor power-up failed\n");
940
941 return ret;
942 }
943
944 static int power_down(struct v4l2_subdev *sd)
945 {
946 struct ov2680_device *dev = to_ov2680_sensor(sd);
947 struct i2c_client *client = v4l2_get_subdevdata(sd);
948 int ret = 0;
949
950 h_flag = 0;
951 v_flag = 0;
952 if (!dev->platform_data) {
953 dev_err(&client->dev,
954 "no camera_sensor_platform_data");
955 return -ENODEV;
956 }
957
958 ret = dev->platform_data->flisclk_ctrl(sd, 0);
959 if (ret)
960 dev_err(&client->dev, "flisclk failed\n");
961
962 /* gpio ctrl */
963 ret = gpio_ctrl(sd, 0);
964 if (ret) {
965 ret = gpio_ctrl(sd, 0);
966 if (ret)
967 dev_err(&client->dev, "gpio failed 2\n");
968 }
969
970 /* power control */
971 ret = power_ctrl(sd, 0);
972 if (ret)
973 dev_err(&client->dev, "vprog failed.\n");
974
975 return ret;
976 }
977
978 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
979 {
980 int ret;
981
982 if (on == 0){
983 ret = power_down(sd);
984 } else {
985 ret = power_up(sd);
986 if (!ret)
987 return ov2680_init(sd);
988 }
989 return ret;
990 }
991
992 /*
993 * distance - calculate the distance
994 * @res: resolution
995 * @w: width
996 * @h: height
997 *
998 * Get the gap between resolution and w/h.
999 * res->width/height smaller than w/h wouldn't be considered.
1000 * Returns the value of gap or -1 if fail.
1001 */
1002 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
1003 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
1004 {
1005 unsigned int w_ratio = (res->width << 13) / w;
1006 unsigned int h_ratio;
1007 int match;
1008
1009 if (h == 0)
1010 return -1;
1011 h_ratio = (res->height << 13) / h;
1012 if (h_ratio == 0)
1013 return -1;
1014 match = abs(((w_ratio << 13) / h_ratio) - ((int)8192));
1015
1016
1017 if ((w_ratio < (int)8192) || (h_ratio < (int)8192) ||
1018 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
1019 return -1;
1020
1021 return w_ratio + h_ratio;
1022 }
1023
1024 /* Return the nearest higher resolution index */
1025 static int nearest_resolution_index(int w, int h)
1026 {
1027 int i;
1028 int idx = -1;
1029 int dist;
1030 int min_dist = INT_MAX;
1031 struct ov2680_resolution *tmp_res = NULL;
1032
1033 for (i = 0; i < N_RES; i++) {
1034 tmp_res = &ov2680_res[i];
1035 dist = distance(tmp_res, w, h);
1036 if (dist == -1)
1037 continue;
1038 if (dist < min_dist) {
1039 min_dist = dist;
1040 idx = i;
1041 }
1042 }
1043
1044 return idx;
1045 }
1046
1047 static int get_resolution_index(int w, int h)
1048 {
1049 int i;
1050
1051 for (i = 0; i < N_RES; i++) {
1052 if (w != ov2680_res[i].width)
1053 continue;
1054 if (h != ov2680_res[i].height)
1055 continue;
1056
1057 return i;
1058 }
1059
1060 return -1;
1061 }
1062
1063 static int ov2680_set_fmt(struct v4l2_subdev *sd,
1064 struct v4l2_subdev_pad_config *cfg,
1065 struct v4l2_subdev_format *format)
1066 {
1067 struct v4l2_mbus_framefmt *fmt = &format->format;
1068 struct ov2680_device *dev = to_ov2680_sensor(sd);
1069 struct i2c_client *client = v4l2_get_subdevdata(sd);
1070 struct camera_mipi_info *ov2680_info = NULL;
1071 int ret = 0;
1072 int idx = 0;
1073 dev_dbg(&client->dev, "+++++ov2680_s_mbus_fmt+++++l\n");
1074 if (format->pad)
1075 return -EINVAL;
1076
1077 if (!fmt)
1078 return -EINVAL;
1079
1080 ov2680_info = v4l2_get_subdev_hostdata(sd);
1081 if (!ov2680_info)
1082 return -EINVAL;
1083
1084 mutex_lock(&dev->input_lock);
1085 idx = nearest_resolution_index(fmt->width, fmt->height);
1086 if (idx == -1) {
1087 /* return the largest resolution */
1088 fmt->width = ov2680_res[N_RES - 1].width;
1089 fmt->height = ov2680_res[N_RES - 1].height;
1090 } else {
1091 fmt->width = ov2680_res[idx].width;
1092 fmt->height = ov2680_res[idx].height;
1093 }
1094 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1095 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1096 cfg->try_fmt = *fmt;
1097 mutex_unlock(&dev->input_lock);
1098 return 0;
1099 }
1100 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1101 dev_dbg(&client->dev, "+++++get_resolution_index=%d+++++l\n",
1102 dev->fmt_idx);
1103 if (dev->fmt_idx == -1) {
1104 dev_err(&client->dev, "get resolution fail\n");
1105 mutex_unlock(&dev->input_lock);
1106 return -EINVAL;
1107 }
1108 v4l2_info(client, "__s_mbus_fmt i=%d, w=%d, h=%d\n", dev->fmt_idx,
1109 fmt->width, fmt->height);
1110 dev_dbg(&client->dev, "__s_mbus_fmt i=%d, w=%d, h=%d\n",
1111 dev->fmt_idx, fmt->width, fmt->height);
1112
1113 ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
1114 if (ret)
1115 dev_err(&client->dev, "ov2680 write resolution register err\n");
1116
1117 ret = ov2680_get_intg_factor(client, ov2680_info,
1118 &ov2680_res[dev->fmt_idx]);
1119 if (ret) {
1120 dev_err(&client->dev, "failed to get integration_factor\n");
1121 goto err;
1122 }
1123
1124 /*recall flip functions to avoid flip registers
1125 * were overridden by default setting
1126 */
1127 if (h_flag)
1128 ov2680_h_flip(sd, h_flag);
1129 if (v_flag)
1130 ov2680_v_flip(sd, v_flag);
1131
1132 v4l2_info(client, "\n%s idx %d \n", __func__, dev->fmt_idx);
1133
1134 /*ret = startup(sd);
1135 * if (ret)
1136 * dev_err(&client->dev, "ov2680 startup err\n");
1137 */
1138 err:
1139 mutex_unlock(&dev->input_lock);
1140 return ret;
1141 }
1142
1143 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1144 struct v4l2_subdev_pad_config *cfg,
1145 struct v4l2_subdev_format *format)
1146 {
1147 struct v4l2_mbus_framefmt *fmt = &format->format;
1148 struct ov2680_device *dev = to_ov2680_sensor(sd);
1149
1150 if (format->pad)
1151 return -EINVAL;
1152
1153 if (!fmt)
1154 return -EINVAL;
1155
1156 fmt->width = ov2680_res[dev->fmt_idx].width;
1157 fmt->height = ov2680_res[dev->fmt_idx].height;
1158 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1159
1160 return 0;
1161 }
1162
1163 static int ov2680_detect(struct i2c_client *client)
1164 {
1165 struct i2c_adapter *adapter = client->adapter;
1166 u16 high, low;
1167 int ret;
1168 u16 id;
1169 u8 revision;
1170
1171 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1172 return -ENODEV;
1173
1174 ret = ov2680_read_reg(client, OV2680_8BIT,
1175 OV2680_SC_CMMN_CHIP_ID_H, &high);
1176 if (ret) {
1177 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1178 return -ENODEV;
1179 }
1180 ret = ov2680_read_reg(client, OV2680_8BIT,
1181 OV2680_SC_CMMN_CHIP_ID_L, &low);
1182 id = ((((u16) high) << 8) | (u16) low);
1183
1184 if (id != OV2680_ID) {
1185 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1186 return -ENODEV;
1187 }
1188
1189 ret = ov2680_read_reg(client, OV2680_8BIT,
1190 OV2680_SC_CMMN_SUB_ID, &high);
1191 revision = (u8) high & 0x0f;
1192
1193 dev_info(&client->dev, "sensor_revision id = 0x%x\n", id);
1194
1195 return 0;
1196 }
1197
1198 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1199 {
1200 struct ov2680_device *dev = to_ov2680_sensor(sd);
1201 struct i2c_client *client = v4l2_get_subdevdata(sd);
1202 int ret;
1203
1204 mutex_lock(&dev->input_lock);
1205 if(enable )
1206 dev_dbg(&client->dev, "ov2680_s_stream one \n");
1207 else
1208 dev_dbg(&client->dev, "ov2680_s_stream off \n");
1209
1210 ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_STREAM,
1211 enable ? OV2680_START_STREAMING :
1212 OV2680_STOP_STREAMING);
1213 #if 0
1214 /* restore settings */
1215 ov2680_res = ov2680_res_preview;
1216 N_RES = N_RES_PREVIEW;
1217 #endif
1218
1219 //otp valid at stream on state
1220 //if(!dev->otp_data)
1221 // dev->otp_data = ov2680_otp_read(sd);
1222
1223 mutex_unlock(&dev->input_lock);
1224
1225 return ret;
1226 }
1227
1228
1229 static int ov2680_s_config(struct v4l2_subdev *sd,
1230 int irq, void *platform_data)
1231 {
1232 struct ov2680_device *dev = to_ov2680_sensor(sd);
1233 struct i2c_client *client = v4l2_get_subdevdata(sd);
1234 int ret = 0;
1235
1236 if (!platform_data)
1237 return -ENODEV;
1238
1239 dev->platform_data =
1240 (struct camera_sensor_platform_data *)platform_data;
1241
1242 mutex_lock(&dev->input_lock);
1243 /* power off the module, then power on it in future
1244 * as first power on by board may not fulfill the
1245 * power on sequqence needed by the module
1246 */
1247 ret = power_down(sd);
1248 if (ret) {
1249 dev_err(&client->dev, "ov2680 power-off err.\n");
1250 goto fail_power_off;
1251 }
1252
1253 ret = power_up(sd);
1254 if (ret) {
1255 dev_err(&client->dev, "ov2680 power-up err.\n");
1256 goto fail_power_on;
1257 }
1258
1259 ret = dev->platform_data->csi_cfg(sd, 1);
1260 if (ret)
1261 goto fail_csi_cfg;
1262
1263 /* config & detect sensor */
1264 ret = ov2680_detect(client);
1265 if (ret) {
1266 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1267 goto fail_csi_cfg;
1268 }
1269
1270 /* turn off sensor, after probed */
1271 ret = power_down(sd);
1272 if (ret) {
1273 dev_err(&client->dev, "ov2680 power-off err.\n");
1274 goto fail_csi_cfg;
1275 }
1276 mutex_unlock(&dev->input_lock);
1277
1278 return 0;
1279
1280 fail_csi_cfg:
1281 dev->platform_data->csi_cfg(sd, 0);
1282 fail_power_on:
1283 power_down(sd);
1284 dev_err(&client->dev, "sensor power-gating failed\n");
1285 fail_power_off:
1286 mutex_unlock(&dev->input_lock);
1287 return ret;
1288 }
1289
1290 static int ov2680_g_parm(struct v4l2_subdev *sd,
1291 struct v4l2_streamparm *param)
1292 {
1293 struct ov2680_device *dev = to_ov2680_sensor(sd);
1294 struct i2c_client *client = v4l2_get_subdevdata(sd);
1295
1296 if (!param)
1297 return -EINVAL;
1298
1299 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1300 dev_err(&client->dev, "unsupported buffer type.\n");
1301 return -EINVAL;
1302 }
1303
1304 memset(param, 0, sizeof(*param));
1305 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306
1307 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1308 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1309 param->parm.capture.timeperframe.numerator = 1;
1310 param->parm.capture.capturemode = dev->run_mode;
1311 param->parm.capture.timeperframe.denominator =
1312 ov2680_res[dev->fmt_idx].fps;
1313 }
1314 return 0;
1315 }
1316
1317 static int ov2680_s_parm(struct v4l2_subdev *sd,
1318 struct v4l2_streamparm *param)
1319 {
1320 struct ov2680_device *dev = to_ov2680_sensor(sd);
1321 struct i2c_client *client = v4l2_get_subdevdata(sd);
1322 dev->run_mode = param->parm.capture.capturemode;
1323
1324 v4l2_info(client, "\n%s:run_mode :%x\n", __func__, dev->run_mode);
1325
1326 mutex_lock(&dev->input_lock);
1327 switch (dev->run_mode) {
1328 case CI_MODE_VIDEO:
1329 ov2680_res = ov2680_res_video;
1330 N_RES = N_RES_VIDEO;
1331 break;
1332 case CI_MODE_STILL_CAPTURE:
1333 ov2680_res = ov2680_res_still;
1334 N_RES = N_RES_STILL;
1335 break;
1336 default:
1337 ov2680_res = ov2680_res_preview;
1338 N_RES = N_RES_PREVIEW;
1339 }
1340 mutex_unlock(&dev->input_lock);
1341 return 0;
1342 }
1343
1344 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1345 struct v4l2_subdev_frame_interval *interval)
1346 {
1347 struct ov2680_device *dev = to_ov2680_sensor(sd);
1348
1349 interval->interval.numerator = 1;
1350 interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
1351
1352 return 0;
1353 }
1354
1355 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1356 struct v4l2_subdev_pad_config *cfg,
1357 struct v4l2_subdev_mbus_code_enum *code)
1358 {
1359 if (code->index >= MAX_FMTS)
1360 return -EINVAL;
1361
1362 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1363 return 0;
1364 }
1365
1366 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1367 struct v4l2_subdev_pad_config *cfg,
1368 struct v4l2_subdev_frame_size_enum *fse)
1369 {
1370 int index = fse->index;
1371
1372 if (index >= N_RES)
1373 return -EINVAL;
1374
1375 fse->min_width = ov2680_res[index].width;
1376 fse->min_height = ov2680_res[index].height;
1377 fse->max_width = ov2680_res[index].width;
1378 fse->max_height = ov2680_res[index].height;
1379
1380 return 0;
1381
1382 }
1383
1384 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1385 {
1386 struct ov2680_device *dev = to_ov2680_sensor(sd);
1387
1388 mutex_lock(&dev->input_lock);
1389 *frames = ov2680_res[dev->fmt_idx].skip_frames;
1390 mutex_unlock(&dev->input_lock);
1391
1392 return 0;
1393 }
1394
1395 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1396 .s_stream = ov2680_s_stream,
1397 .g_parm = ov2680_g_parm,
1398 .s_parm = ov2680_s_parm,
1399 .g_frame_interval = ov2680_g_frame_interval,
1400 };
1401
1402 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1403 .g_skip_frames = ov2680_g_skip_frames,
1404 };
1405
1406 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1407 .s_power = ov2680_s_power,
1408 .ioctl = ov2680_ioctl,
1409 };
1410
1411 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1412 .enum_mbus_code = ov2680_enum_mbus_code,
1413 .enum_frame_size = ov2680_enum_frame_size,
1414 .get_fmt = ov2680_get_fmt,
1415 .set_fmt = ov2680_set_fmt,
1416 };
1417
1418 static const struct v4l2_subdev_ops ov2680_ops = {
1419 .core = &ov2680_core_ops,
1420 .video = &ov2680_video_ops,
1421 .pad = &ov2680_pad_ops,
1422 .sensor = &ov2680_sensor_ops,
1423 };
1424
1425 static int ov2680_remove(struct i2c_client *client)
1426 {
1427 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1428 struct ov2680_device *dev = to_ov2680_sensor(sd);
1429 dev_dbg(&client->dev, "ov2680_remove...\n");
1430
1431 dev->platform_data->csi_cfg(sd, 0);
1432
1433 v4l2_device_unregister_subdev(sd);
1434 media_entity_cleanup(&dev->sd.entity);
1435 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1436 kfree(dev);
1437
1438 return 0;
1439 }
1440
1441 static int ov2680_probe(struct i2c_client *client,
1442 const struct i2c_device_id *id)
1443 {
1444 struct ov2680_device *dev;
1445 int ret;
1446 void *pdata;
1447 unsigned int i;
1448
1449 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1450 if (!dev)
1451 return -ENOMEM;
1452
1453 mutex_init(&dev->input_lock);
1454
1455 dev->fmt_idx = 0;
1456 v4l2_i2c_subdev_init(&(dev->sd), client, &ov2680_ops);
1457
1458 if (ACPI_COMPANION(&client->dev))
1459 pdata = gmin_camera_platform_data(&dev->sd,
1460 ATOMISP_INPUT_FORMAT_RAW_10,
1461 atomisp_bayer_order_bggr);
1462 else
1463 pdata = client->dev.platform_data;
1464
1465 if (!pdata) {
1466 ret = -EINVAL;
1467 goto out_free;
1468 }
1469
1470 ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1471 if (ret)
1472 goto out_free;
1473
1474 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1475 if (ret)
1476 goto out_free;
1477
1478 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1479 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1480 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1481 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1482 ret =
1483 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1484 ARRAY_SIZE(ov2680_controls));
1485 if (ret) {
1486 ov2680_remove(client);
1487 return ret;
1488 }
1489
1490 for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1491 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1492 NULL);
1493
1494 if (dev->ctrl_handler.error) {
1495 ov2680_remove(client);
1496 return dev->ctrl_handler.error;
1497 }
1498
1499 /* Use same lock for controls as for everything else. */
1500 dev->ctrl_handler.lock = &dev->input_lock;
1501 dev->sd.ctrl_handler = &dev->ctrl_handler;
1502
1503 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1504 if (ret)
1505 {
1506 ov2680_remove(client);
1507 dev_dbg(&client->dev, "+++ remove ov2680 \n");
1508 }
1509 return ret;
1510 out_free:
1511 dev_dbg(&client->dev, "+++ out free \n");
1512 v4l2_device_unregister_subdev(&dev->sd);
1513 kfree(dev);
1514 return ret;
1515 }
1516
1517 static const struct acpi_device_id ov2680_acpi_match[] = {
1518 {"XXOV2680"},
1519 {"OVTI2680"},
1520 {},
1521 };
1522 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1523
1524
1525 MODULE_DEVICE_TABLE(i2c, ov2680_id);
1526 static struct i2c_driver ov2680_driver = {
1527 .driver = {
1528 .owner = THIS_MODULE,
1529 .name = OV2680_NAME,
1530 .acpi_match_table = ACPI_PTR(ov2680_acpi_match),
1531
1532 },
1533 .probe = ov2680_probe,
1534 .remove = ov2680_remove,
1535 .id_table = ov2680_id,
1536 };
1537
1538 static int init_ov2680(void)
1539 {
1540 return i2c_add_driver(&ov2680_driver);
1541 }
1542
1543 static void exit_ov2680(void)
1544 {
1545
1546 i2c_del_driver(&ov2680_driver);
1547 }
1548
1549 module_init(init_ov2680);
1550 module_exit(exit_ov2680);
1551
1552 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1553 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1554 MODULE_LICENSE("GPL");
1555