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