]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/media/atomisp/i2c/atomisp-ov2722.c
media: staging: atomisp: Remove unneeded gpio.h inclusion
[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
680 /* Non-gmin platforms use the legacy callback */
681 if (dev->platform_data->gpio_ctrl)
682 return dev->platform_data->gpio_ctrl(sd, flag);
683
684 /* Note: the GPIO order is asymmetric: always RESET#
e17a17a0
VR
685 * before PWDN# when turning it on or off.
686 */
a49d2536
AC
687 ret = dev->platform_data->gpio0_ctrl(sd, flag);
688 /*
689 *ov2722 PWDN# active high when pull down,opposite to the convention
690 */
691 ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
692 return ret;
693}
694
695static int power_up(struct v4l2_subdev *sd)
696{
697 struct ov2722_device *dev = to_ov2722_sensor(sd);
698 struct i2c_client *client = v4l2_get_subdevdata(sd);
699 int ret;
700
c9d9602f 701 if (!dev->platform_data) {
a49d2536
AC
702 dev_err(&client->dev,
703 "no camera_sensor_platform_data");
704 return -ENODEV;
705 }
706
707 /* power control */
708 ret = power_ctrl(sd, 1);
709 if (ret)
710 goto fail_power;
711
712 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
713 usleep_range(5000, 6000);
714
715 /* gpio ctrl */
716 ret = gpio_ctrl(sd, 1);
717 if (ret) {
718 ret = gpio_ctrl(sd, 0);
719 if (ret)
720 goto fail_power;
721 }
722
723 /* flis clock control */
724 ret = dev->platform_data->flisclk_ctrl(sd, 1);
725 if (ret)
726 goto fail_clk;
727
728 /* according to DS, 20ms is needed between PWDN and i2c access */
729 msleep(20);
730
731 return 0;
732
733fail_clk:
734 gpio_ctrl(sd, 0);
735fail_power:
736 power_ctrl(sd, 0);
737 dev_err(&client->dev, "sensor power-up failed\n");
738
739 return ret;
740}
741
742static int power_down(struct v4l2_subdev *sd)
743{
744 struct ov2722_device *dev = to_ov2722_sensor(sd);
745 struct i2c_client *client = v4l2_get_subdevdata(sd);
746 int ret = 0;
747
c9d9602f 748 if (!dev->platform_data) {
a49d2536
AC
749 dev_err(&client->dev,
750 "no camera_sensor_platform_data");
751 return -ENODEV;
752 }
753
754 ret = dev->platform_data->flisclk_ctrl(sd, 0);
755 if (ret)
756 dev_err(&client->dev, "flisclk failed\n");
757
758 /* gpio ctrl */
759 ret = gpio_ctrl(sd, 0);
760 if (ret) {
761 ret = gpio_ctrl(sd, 0);
762 if (ret)
763 dev_err(&client->dev, "gpio failed 2\n");
764 }
765
766 /* power control */
767 ret = power_ctrl(sd, 0);
768 if (ret)
769 dev_err(&client->dev, "vprog failed.\n");
770
771 return ret;
772}
773
774static int ov2722_s_power(struct v4l2_subdev *sd, int on)
775{
776 int ret;
777 if (on == 0)
778 return power_down(sd);
779 else {
780 ret = power_up(sd);
781 if (!ret)
782 return ov2722_init(sd);
783 }
784 return ret;
785}
786
787/*
788 * distance - calculate the distance
789 * @res: resolution
790 * @w: width
791 * @h: height
792 *
793 * Get the gap between resolution and w/h.
794 * res->width/height smaller than w/h wouldn't be considered.
795 * Returns the value of gap or -1 if fail.
796 */
797#define LARGEST_ALLOWED_RATIO_MISMATCH 800
798static int distance(struct ov2722_resolution *res, u32 w, u32 h)
799{
56b41836 800 unsigned int w_ratio = (res->width << 13) / w;
a49d2536
AC
801 unsigned int h_ratio;
802 int match;
803
804 if (h == 0)
805 return -1;
6c492211 806 h_ratio = (res->height << 13) / h;
a49d2536
AC
807 if (h_ratio == 0)
808 return -1;
bf5d0300 809 match = abs(((w_ratio << 13) / h_ratio) - 8192);
a49d2536 810
8284d205
VR
811 if ((w_ratio < 8192) || (h_ratio < 8192) ||
812 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
a49d2536
AC
813 return -1;
814
815 return w_ratio + h_ratio;
816}
817
818/* Return the nearest higher resolution index */
819static int nearest_resolution_index(int w, int h)
820{
821 int i;
822 int idx = -1;
823 int dist;
824 int min_dist = INT_MAX;
825 struct ov2722_resolution *tmp_res = NULL;
826
827 for (i = 0; i < N_RES; i++) {
828 tmp_res = &ov2722_res[i];
829 dist = distance(tmp_res, w, h);
830 if (dist == -1)
831 continue;
832 if (dist < min_dist) {
833 min_dist = dist;
834 idx = i;
835 }
836 }
837
838 return idx;
839}
840
841static int get_resolution_index(int w, int h)
842{
843 int i;
844
845 for (i = 0; i < N_RES; i++) {
846 if (w != ov2722_res[i].width)
847 continue;
848 if (h != ov2722_res[i].height)
849 continue;
850
851 return i;
852 }
853
854 return -1;
855}
856
857/* TODO: remove it. */
858static int startup(struct v4l2_subdev *sd)
859{
860 struct ov2722_device *dev = to_ov2722_sensor(sd);
861 struct i2c_client *client = v4l2_get_subdevdata(sd);
862 int ret = 0;
863
864 ret = ov2722_write_reg(client, OV2722_8BIT,
865 OV2722_SW_RESET, 0x01);
866 if (ret) {
867 dev_err(&client->dev, "ov2722 reset err.\n");
868 return ret;
869 }
870
871 ret = ov2722_write_reg_array(client, ov2722_res[dev->fmt_idx].regs);
872 if (ret) {
873 dev_err(&client->dev, "ov2722 write register err.\n");
874 return ret;
875 }
876
877 return ret;
878}
879
880static int ov2722_set_fmt(struct v4l2_subdev *sd,
881 struct v4l2_subdev_pad_config *cfg,
882 struct v4l2_subdev_format *format)
883{
884 struct v4l2_mbus_framefmt *fmt = &format->format;
885 struct ov2722_device *dev = to_ov2722_sensor(sd);
886 struct i2c_client *client = v4l2_get_subdevdata(sd);
887 struct camera_mipi_info *ov2722_info = NULL;
888 int ret = 0;
889 int idx;
890 if (format->pad)
891 return -EINVAL;
892 if (!fmt)
893 return -EINVAL;
894 ov2722_info = v4l2_get_subdev_hostdata(sd);
c9d9602f 895 if (!ov2722_info)
a49d2536
AC
896 return -EINVAL;
897
898 mutex_lock(&dev->input_lock);
899 idx = nearest_resolution_index(fmt->width, fmt->height);
900 if (idx == -1) {
901 /* return the largest resolution */
902 fmt->width = ov2722_res[N_RES - 1].width;
903 fmt->height = ov2722_res[N_RES - 1].height;
904 } else {
905 fmt->width = ov2722_res[idx].width;
906 fmt->height = ov2722_res[idx].height;
907 }
908 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
909 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
910 cfg->try_fmt = *fmt;
911 mutex_unlock(&dev->input_lock);
912 return 0;
913 }
914
915 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
916 if (dev->fmt_idx == -1) {
917 dev_err(&client->dev, "get resolution fail\n");
918 mutex_unlock(&dev->input_lock);
919 return -EINVAL;
920 }
921
922 dev->pixels_per_line = ov2722_res[dev->fmt_idx].pixels_per_line;
923 dev->lines_per_frame = ov2722_res[dev->fmt_idx].lines_per_frame;
924
925 ret = startup(sd);
926 if (ret) {
927 int i = 0;
928 dev_err(&client->dev, "ov2722 startup err, retry to power up\n");
929 for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) {
930 dev_err(&client->dev,
931 "ov2722 retry to power up %d/%d times, result: ",
56b41836 932 i + 1, OV2722_POWER_UP_RETRY_NUM);
a49d2536
AC
933 power_down(sd);
934 ret = power_up(sd);
935 if (ret) {
936 dev_err(&client->dev, "power up failed, continue\n");
937 continue;
938 }
939 ret = startup(sd);
940 if (ret) {
941 dev_err(&client->dev, " startup FAILED!\n");
942 } else {
943 dev_err(&client->dev, " startup SUCCESS!\n");
944 break;
945 }
946 }
947 if (ret) {
948 dev_err(&client->dev, "ov2722 startup err\n");
949 goto err;
950 }
951 }
952
953 ret = ov2722_get_intg_factor(client, ov2722_info,
954 &ov2722_res[dev->fmt_idx]);
955 if (ret)
956 dev_err(&client->dev, "failed to get integration_factor\n");
957
958err:
959 mutex_unlock(&dev->input_lock);
960 return ret;
961}
962static int ov2722_get_fmt(struct v4l2_subdev *sd,
963 struct v4l2_subdev_pad_config *cfg,
964 struct v4l2_subdev_format *format)
965{
966 struct v4l2_mbus_framefmt *fmt = &format->format;
967 struct ov2722_device *dev = to_ov2722_sensor(sd);
968
969 if (format->pad)
970 return -EINVAL;
971 if (!fmt)
972 return -EINVAL;
973
974 fmt->width = ov2722_res[dev->fmt_idx].width;
975 fmt->height = ov2722_res[dev->fmt_idx].height;
976 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
977
978 return 0;
979}
980
981static int ov2722_detect(struct i2c_client *client)
982{
983 struct i2c_adapter *adapter = client->adapter;
984 u16 high, low;
985 int ret;
986 u16 id;
987 u8 revision;
988
989 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
990 return -ENODEV;
991
992 ret = ov2722_read_reg(client, OV2722_8BIT,
993 OV2722_SC_CMMN_CHIP_ID_H, &high);
994 if (ret) {
995 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
996 return -ENODEV;
997 }
998 ret = ov2722_read_reg(client, OV2722_8BIT,
999 OV2722_SC_CMMN_CHIP_ID_L, &low);
bf5d0300 1000 id = (high << 8) | low;
a49d2536
AC
1001
1002 if ((id != OV2722_ID) && (id != OV2720_ID)) {
1003 dev_err(&client->dev, "sensor ID error\n");
1004 return -ENODEV;
1005 }
1006
1007 ret = ov2722_read_reg(client, OV2722_8BIT,
1008 OV2722_SC_CMMN_SUB_ID, &high);
1009 revision = (u8) high & 0x0f;
1010
1011 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1012 dev_dbg(&client->dev, "detect ov2722 success\n");
1013 return 0;
1014}
1015
1016static int ov2722_s_stream(struct v4l2_subdev *sd, int enable)
1017{
1018 struct ov2722_device *dev = to_ov2722_sensor(sd);
1019 struct i2c_client *client = v4l2_get_subdevdata(sd);
1020 int ret;
1021
1022 mutex_lock(&dev->input_lock);
1023
1024 ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM,
1025 enable ? OV2722_START_STREAMING :
1026 OV2722_STOP_STREAMING);
1027
1028 mutex_unlock(&dev->input_lock);
1029 return ret;
1030}
1031
1032static int ov2722_s_config(struct v4l2_subdev *sd,
1033 int irq, void *platform_data)
1034{
1035 struct ov2722_device *dev = to_ov2722_sensor(sd);
1036 struct i2c_client *client = v4l2_get_subdevdata(sd);
1037 int ret = 0;
1038
c9d9602f 1039 if (!platform_data)
a49d2536
AC
1040 return -ENODEV;
1041
1042 dev->platform_data =
1043 (struct camera_sensor_platform_data *)platform_data;
1044
1045 mutex_lock(&dev->input_lock);
1046 if (dev->platform_data->platform_init) {
1047 ret = dev->platform_data->platform_init(client);
1048 if (ret) {
1049 dev_err(&client->dev, "platform init err\n");
1050 goto platform_init_failed;
1051 }
1052 }
1053
1054 /* power off the module, then power on it in future
1055 * as first power on by board may not fulfill the
1056 * power on sequqence needed by the module
1057 */
1058 ret = power_down(sd);
1059 if (ret) {
1060 dev_err(&client->dev, "ov2722 power-off err.\n");
1061 goto fail_power_off;
1062 }
1063
1064 ret = power_up(sd);
1065 if (ret) {
1066 dev_err(&client->dev, "ov2722 power-up err.\n");
1067 goto fail_power_on;
1068 }
1069
1070 ret = dev->platform_data->csi_cfg(sd, 1);
1071 if (ret)
1072 goto fail_csi_cfg;
1073
1074 /* config & detect sensor */
1075 ret = ov2722_detect(client);
1076 if (ret) {
1077 dev_err(&client->dev, "ov2722_detect err s_config.\n");
1078 goto fail_csi_cfg;
1079 }
1080
1081 /* turn off sensor, after probed */
1082 ret = power_down(sd);
1083 if (ret) {
1084 dev_err(&client->dev, "ov2722 power-off err.\n");
1085 goto fail_csi_cfg;
1086 }
1087 mutex_unlock(&dev->input_lock);
1088
1089 return 0;
1090
1091fail_csi_cfg:
1092 dev->platform_data->csi_cfg(sd, 0);
1093fail_power_on:
1094 power_down(sd);
1095 dev_err(&client->dev, "sensor power-gating failed\n");
1096fail_power_off:
1097 if (dev->platform_data->platform_deinit)
1098 dev->platform_data->platform_deinit();
1099platform_init_failed:
1100 mutex_unlock(&dev->input_lock);
1101 return ret;
1102}
1103
1104static int ov2722_g_parm(struct v4l2_subdev *sd,
1105 struct v4l2_streamparm *param)
1106{
1107 struct ov2722_device *dev = to_ov2722_sensor(sd);
1108 struct i2c_client *client = v4l2_get_subdevdata(sd);
1109
1110 if (!param)
1111 return -EINVAL;
1112
1113 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1114 dev_err(&client->dev, "unsupported buffer type.\n");
1115 return -EINVAL;
1116 }
1117
1118 memset(param, 0, sizeof(*param));
1119 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1120
1121 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1122 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1123 param->parm.capture.timeperframe.numerator = 1;
1124 param->parm.capture.capturemode = dev->run_mode;
1125 param->parm.capture.timeperframe.denominator =
1126 ov2722_res[dev->fmt_idx].fps;
1127 }
1128 return 0;
1129}
1130
1131static int ov2722_s_parm(struct v4l2_subdev *sd,
1132 struct v4l2_streamparm *param)
1133{
1134 struct ov2722_device *dev = to_ov2722_sensor(sd);
1135 dev->run_mode = param->parm.capture.capturemode;
1136
1137 mutex_lock(&dev->input_lock);
1138 switch (dev->run_mode) {
1139 case CI_MODE_VIDEO:
1140 ov2722_res = ov2722_res_video;
1141 N_RES = N_RES_VIDEO;
1142 break;
1143 case CI_MODE_STILL_CAPTURE:
1144 ov2722_res = ov2722_res_still;
1145 N_RES = N_RES_STILL;
1146 break;
1147 default:
1148 ov2722_res = ov2722_res_preview;
1149 N_RES = N_RES_PREVIEW;
1150 }
1151 mutex_unlock(&dev->input_lock);
1152 return 0;
1153}
1154
1155static int ov2722_g_frame_interval(struct v4l2_subdev *sd,
1156 struct v4l2_subdev_frame_interval *interval)
1157{
1158 struct ov2722_device *dev = to_ov2722_sensor(sd);
1159
1160 interval->interval.numerator = 1;
1161 interval->interval.denominator = ov2722_res[dev->fmt_idx].fps;
1162
1163 return 0;
1164}
1165
1166static int ov2722_enum_mbus_code(struct v4l2_subdev *sd,
1167 struct v4l2_subdev_pad_config *cfg,
1168 struct v4l2_subdev_mbus_code_enum *code)
1169{
1170 if (code->index >= MAX_FMTS)
1171 return -EINVAL;
1172
1173 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1174 return 0;
1175}
1176
1177static int ov2722_enum_frame_size(struct v4l2_subdev *sd,
1178 struct v4l2_subdev_pad_config *cfg,
1179 struct v4l2_subdev_frame_size_enum *fse)
1180{
1181 int index = fse->index;
1182
1183 if (index >= N_RES)
1184 return -EINVAL;
1185
1186 fse->min_width = ov2722_res[index].width;
1187 fse->min_height = ov2722_res[index].height;
1188 fse->max_width = ov2722_res[index].width;
1189 fse->max_height = ov2722_res[index].height;
1190
1191 return 0;
1192
1193}
1194
1195
1196static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1197{
1198 struct ov2722_device *dev = to_ov2722_sensor(sd);
1199
1200 mutex_lock(&dev->input_lock);
1201 *frames = ov2722_res[dev->fmt_idx].skip_frames;
1202 mutex_unlock(&dev->input_lock);
1203
1204 return 0;
1205}
1206
1207static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = {
1208 .g_skip_frames = ov2722_g_skip_frames,
1209};
1210
1211static const struct v4l2_subdev_video_ops ov2722_video_ops = {
1212 .s_stream = ov2722_s_stream,
1213 .g_parm = ov2722_g_parm,
1214 .s_parm = ov2722_s_parm,
1215 .g_frame_interval = ov2722_g_frame_interval,
1216};
1217
1218static const struct v4l2_subdev_core_ops ov2722_core_ops = {
1219 .s_power = ov2722_s_power,
1220 .ioctl = ov2722_ioctl,
1221};
1222
1223static const struct v4l2_subdev_pad_ops ov2722_pad_ops = {
1224 .enum_mbus_code = ov2722_enum_mbus_code,
1225 .enum_frame_size = ov2722_enum_frame_size,
1226 .get_fmt = ov2722_get_fmt,
1227 .set_fmt = ov2722_set_fmt,
1228};
1229
1230static const struct v4l2_subdev_ops ov2722_ops = {
1231 .core = &ov2722_core_ops,
1232 .video = &ov2722_video_ops,
1233 .pad = &ov2722_pad_ops,
1234 .sensor = &ov2722_sensor_ops,
1235};
1236
1237static int ov2722_remove(struct i2c_client *client)
1238{
1239 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1240 struct ov2722_device *dev = to_ov2722_sensor(sd);
1241 dev_dbg(&client->dev, "ov2722_remove...\n");
1242
1243 if (dev->platform_data->platform_deinit)
1244 dev->platform_data->platform_deinit();
1245
1246 dev->platform_data->csi_cfg(sd, 0);
1247 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1248 v4l2_device_unregister_subdev(sd);
1249
1250 atomisp_gmin_remove_subdev(sd);
1251
1252 media_entity_cleanup(&dev->sd.entity);
1253 kfree(dev);
1254
1255 return 0;
1256}
1257
1258static int __ov2722_init_ctrl_handler(struct ov2722_device *dev)
1259{
1260 struct v4l2_ctrl_handler *hdl;
1261 unsigned int i;
1262 hdl = &dev->ctrl_handler;
1263 v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls));
1264 for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++)
1265 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i],
1266 NULL);
1267
1268 dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ);
1269
c9d9602f 1270 if (dev->ctrl_handler.error || !dev->link_freq)
a49d2536
AC
1271 return dev->ctrl_handler.error;
1272
1273 dev->sd.ctrl_handler = hdl;
1274
1275 return 0;
1276}
1277
e19c9205 1278static int ov2722_probe(struct i2c_client *client)
a49d2536
AC
1279{
1280 struct ov2722_device *dev;
1281 void *ovpdev;
1282 int ret;
1283 struct acpi_device *adev;
1284
1285 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
309167b9 1286 if (!dev)
a49d2536 1287 return -ENOMEM;
a49d2536
AC
1288
1289 mutex_init(&dev->input_lock);
1290
1291 dev->fmt_idx = 0;
1292 v4l2_i2c_subdev_init(&(dev->sd), client, &ov2722_ops);
1293
1294 ovpdev = client->dev.platform_data;
1295 adev = ACPI_COMPANION(&client->dev);
1296 if (adev) {
1297 adev->power.flags.power_resources = 0;
1298 ovpdev = gmin_camera_platform_data(&dev->sd,
1299 ATOMISP_INPUT_FORMAT_RAW_10,
1300 atomisp_bayer_order_grbg);
1301 }
1302
1303 ret = ov2722_s_config(&dev->sd, client->irq, ovpdev);
1304 if (ret)
1305 goto out_free;
1306
1307 ret = __ov2722_init_ctrl_handler(dev);
1308 if (ret)
1309 goto out_ctrl_handler_free;
1310
1311 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1312 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1313 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1314 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1315
1316 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1317 if (ret)
1318 ov2722_remove(client);
1319
1320 if (ACPI_HANDLE(&client->dev))
1321 ret = atomisp_register_i2c_module(&dev->sd, ovpdev, RAW_CAMERA);
1322
1323 return ret;
1324
1325out_ctrl_handler_free:
1326 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1327
1328out_free:
1329 v4l2_device_unregister_subdev(&dev->sd);
1330 kfree(dev);
1331 return ret;
1332}
1333
22461d77 1334static const struct acpi_device_id ov2722_acpi_match[] = {
a49d2536
AC
1335 { "INT33FB" },
1336 {},
1337};
a49d2536
AC
1338MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match);
1339
1340static struct i2c_driver ov2722_driver = {
1341 .driver = {
e19c9205
AS
1342 .name = "ov2722",
1343 .acpi_match_table = ov2722_acpi_match,
a49d2536 1344 },
e19c9205 1345 .probe_new = ov2722_probe,
a49d2536 1346 .remove = ov2722_remove,
a49d2536 1347};
2cb63c4c 1348module_i2c_driver(ov2722_driver);
a49d2536
AC
1349
1350MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>");
1351MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors");
1352MODULE_LICENSE("GPL");