]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/media/atomisp/i2c/ov8858.c
media: staging: atomisp: Use module_i2c_driver() macro
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / media / atomisp / i2c / ov8858.c
CommitLineData
a49d2536
AC
1/*
2 * Support for OmniVision ov8858 camera sensor.
3 *
4 * Copyright (c) 2014 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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 *
20 */
21
22#include <linux/delay.h>
23#include <linux/module.h>
24#include <media/v4l2-device.h>
25#include <linux/acpi.h>
25016567 26#include "../include/linux/atomisp_gmin_platform.h"
a49d2536
AC
27#ifdef CONFIG_PLATFORM_BTNS
28#include "ov8858_btns.h"
29#else
30#include "ov8858.h"
31#endif
32static int ov8858_i2c_read(struct i2c_client *client, u16 len, u16 addr,
33 u8 *buf)
34{
35 struct i2c_msg msg[2];
36 u8 address[2];
37 int err;
38
39 if (!client->adapter) {
40 dev_err(&client->dev, "%s error, no adapter\n", __func__);
41 return -ENODEV;
42 }
43
44 dev_dbg(&client->dev, "%s: len = %d, addr = 0x%04x\n",
45 __func__, len, addr);
46
47 memset(msg, 0, sizeof(msg));
48
49 address[0] = (addr >> 8) & 0xff;
50 address[1] = addr & 0xff;
51
52 msg[0].addr = client->addr;
53 msg[0].flags = 0;
54 msg[0].len = I2C_MSG_LENGTH;
55 msg[0].buf = address;
56
57 msg[1].addr = client->addr;
58 msg[1].len = len;
59 msg[1].flags = I2C_M_RD;
60 msg[1].buf = buf;
61
62 err = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
63 if (err != 2) {
64 if (err >= 0)
65 err = -EIO;
66 goto error;
67 }
68
69 return 0;
70error:
71 dev_err(&client->dev, "reading from address 0x%x error %d", addr, err);
72 return err;
73}
74
75static int ov8858_read_reg(struct i2c_client *client, u16 type, u16 reg,
76 u16 *val)
77{
78 u8 data[OV8858_SHORT_MAX];
79 int err;
80
81 dev_dbg(&client->dev, "%s: type = %d, reg = 0x%04x\n",
82 __func__, type, reg);
83
84 /* read only 8 and 16 bit values */
85 if (type != OV8858_8BIT && type != OV8858_16BIT) {
86 dev_err(&client->dev, "%s error, invalid data length\n",
87 __func__);
88 return -EINVAL;
89 }
90
91 memset(data, 0, sizeof(data));
92
93 err = ov8858_i2c_read(client, type, reg, data);
94 if (err)
95 goto error;
96
97 /* high byte comes first */
98 if (type == OV8858_8BIT)
99 *val = (u8)data[0];
100 else
101 *val = data[0] << 8 | data[1];
102
103 dev_dbg(&client->dev, "%s: val = 0x%04x\n", __func__, *val);
104
105 return 0;
106
107error:
108 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
109 return err;
110}
111
112static int ov8858_i2c_write(struct i2c_client *client, u16 len, u8 *data)
113{
114 struct i2c_msg msg;
115 const int num_msg = 1;
116 int ret;
117
118 msg.addr = client->addr;
119 msg.flags = 0;
120 msg.len = len;
121 msg.buf = data;
122
123 ret = i2c_transfer(client->adapter, &msg, 1);
124
125 return ret == num_msg ? 0 : -EIO;
126}
127
128static int
129ov8858_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u16 val)
130{
131 int ret;
132 unsigned char data[4] = {0};
133 u16 *wreg;
134 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
135
136 dev_dbg(&client->dev,
137 "%s: data_length = %d, reg = 0x%04x, val = 0x%04x\n",
138 __func__, data_length, reg, val);
139
140 if (!client->adapter) {
141 dev_err(&client->dev, "%s error, no adapter\n", __func__);
142 return -ENODEV;
143 }
144
145 if (data_length != OV8858_8BIT && data_length != OV8858_16BIT) {
146 dev_err(&client->dev, "%s error, invalid length\n", __func__);
147 return -EINVAL;
148 }
149
150 /* high byte goes out first */
151 wreg = (u16 *)data;
152 *wreg = cpu_to_be16(reg);
153
154 if (data_length == OV8858_8BIT) {
155 data[2] = (u8)(val);
156 } else {
157 /* OV8858_16BIT */
158 u16 *wdata = (u16 *)&data[2];
159 *wdata = be16_to_cpu(val);
160 }
161
162 ret = ov8858_i2c_write(client, len, data);
163 if (ret)
164 dev_err(&client->dev,
165 "write error: wrote 0x%x to offset 0x%x error %d",
166 val, reg, ret);
167
168 return ret;
169}
170
171/*
172 * ov8858_write_reg_array - Initializes a list of registers
173 * @client: i2c driver client structure
174 * @reglist: list of registers to be written
175 *
176 * This function initializes a list of registers. When consecutive addresses
177 * are found in a row on the list, this function creates a buffer and sends
178 * consecutive data in a single i2c_transfer().
179 *
180 * __ov8858_flush_reg_array(), __ov8858_buf_reg_array() and
181 * __ov8858_write_reg_is_consecutive() are internal functions to
182 * ov8858_write_reg_array() and should be not used anywhere else.
183 *
184 */
185static int __ov8858_flush_reg_array(struct i2c_client *client,
186 struct ov8858_write_ctrl *ctrl)
187{
188 u16 size;
189 if (ctrl->index == 0)
190 return 0;
191
192 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
193 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
194 ctrl->index = 0;
195
196 return ov8858_i2c_write(client, size, (u8 *)&ctrl->buffer);
197}
198
199static int __ov8858_buf_reg_array(struct i2c_client *client,
200 struct ov8858_write_ctrl *ctrl,
201 const struct ov8858_reg *next)
202{
203 int size;
204 u16 *data16;
205
206 switch (next->type) {
207 case OV8858_8BIT:
208 size = 1;
209 ctrl->buffer.data[ctrl->index] = (u8)next->val;
210 break;
211 case OV8858_16BIT:
212 size = 2;
213 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
214 *data16 = cpu_to_be16((u16)next->val);
215 break;
216 default:
217 return -EINVAL;
218 }
219
220 /* When first item is added, we need to store its starting address */
221 if (ctrl->index == 0)
222 ctrl->buffer.addr = next->sreg;
223
224 ctrl->index += size;
225
226 /*
227 * Buffer cannot guarantee free space for u32? Better flush it to avoid
228 * possible lack of memory for next item.
229 */
230 if (ctrl->index + sizeof(u16) >= OV8858_MAX_WRITE_BUF_SIZE)
231 __ov8858_flush_reg_array(client, ctrl);
232
233 return 0;
234}
235
236static int
237__ov8858_write_reg_is_consecutive(struct i2c_client *client,
238 struct ov8858_write_ctrl *ctrl,
239 const struct ov8858_reg *next)
240{
241 if (ctrl->index == 0)
242 return 1;
243
244 return ctrl->buffer.addr + ctrl->index == next->sreg;
245}
246
247static int ov8858_write_reg_array(struct i2c_client *client,
248 const struct ov8858_reg *reglist)
249{
250 const struct ov8858_reg *next = reglist;
251 struct ov8858_write_ctrl ctrl;
252 int err;
253
254 ctrl.index = 0;
255 for (; next->type != OV8858_TOK_TERM; next++) {
256 switch (next->type & OV8858_TOK_MASK) {
257 case OV8858_TOK_DELAY:
258 err = __ov8858_flush_reg_array(client, &ctrl);
259 if (err)
260 return err;
261 msleep(next->val);
262 break;
263
264 default:
265 /*
266 * If next address is not consecutive, data needs to be
267 * flushed before proceeding
268 */
269 if (!__ov8858_write_reg_is_consecutive(client,
270 &ctrl, next)) {
271 err = __ov8858_flush_reg_array(client, &ctrl);
272 if (err)
273 return err;
274 }
275 err = __ov8858_buf_reg_array(client, &ctrl, next);
276 if (err) {
277 dev_err(&client->dev, "%s: write error\n",
278 __func__);
279 return err;
280 }
281 break;
282 }
283 }
284
285 return __ov8858_flush_reg_array(client, &ctrl);
286}
287
288static int __ov8858_min_fps_diff(int fps,
289 const struct ov8858_fps_setting *fps_list)
290{
291 int diff = INT_MAX;
292 int i;
293
294 if (fps == 0)
295 return 0;
296
297 for (i = 0; i < MAX_FPS_OPTIONS_SUPPORTED; i++) {
298 if (!fps_list[i].fps)
299 break;
300 if (abs(fps_list[i].fps - fps) < diff)
301 diff = abs(fps_list[i].fps - fps);
302 }
303
304 return diff;
305}
306
307static int __ov8858_nearest_fps_index(int fps,
308 const struct ov8858_fps_setting *fps_list)
309{
310 int fps_index = 0;
311 int i;
312
313 for (i = 0; i < MAX_FPS_OPTIONS_SUPPORTED; i++) {
314 if (!fps_list[i].fps)
315 break;
316 if (abs(fps_list[i].fps - fps)
317 < abs(fps_list[fps_index].fps - fps))
318 fps_index = i;
319 }
320 return fps_index;
321}
322
323static int __ov8858_update_frame_timing(struct v4l2_subdev *sd,
324 u16 *hts, u16 *vts)
325{
326 struct i2c_client *client = v4l2_get_subdevdata(sd);
327 int ret;
328
329
330 dev_dbg(&client->dev, "%s OV8858_TIMING_HTS=0x%04x\n",
331 __func__, *hts);
332
333 /* HTS = pixel_per_line / 2 */
334 ret = ov8858_write_reg(client, OV8858_16BIT,
335 OV8858_TIMING_HTS, *hts >> 1);
336 if (ret)
337 return ret;
338 dev_dbg(&client->dev, "%s OV8858_TIMING_VTS=0x%04x\n",
339 __func__, *vts);
340
341 return ov8858_write_reg(client, OV8858_16BIT, OV8858_TIMING_VTS, *vts);
342}
343
344static int __ov8858_set_exposure(struct v4l2_subdev *sd, int exposure, int gain,
345 int dig_gain, u16 *hts, u16 *vts)
346{
347 struct ov8858_device *dev = to_ov8858_sensor(sd);
348 struct i2c_client *client = v4l2_get_subdevdata(sd);
349 int exp_val, ret;
350 dev_dbg(&client->dev, "%s, exposure = %d, gain=%d, dig_gain=%d\n",
351 __func__, exposure, gain, dig_gain);
352
353 if (dev->limit_exposure_flag) {
354 if (exposure > *vts - OV8858_INTEGRATION_TIME_MARGIN)
355 exposure = *vts - OV8858_INTEGRATION_TIME_MARGIN;
356 } else {
357 if (*vts < exposure + OV8858_INTEGRATION_TIME_MARGIN)
358 *vts = (u16) exposure + OV8858_INTEGRATION_TIME_MARGIN;
359 }
360
361 ret = __ov8858_update_frame_timing(sd, hts, vts);
362 if (ret)
363 return ret;
364
365 /* For ov8858, the low 4 bits are fraction bits and must be kept 0 */
366 exp_val = exposure << 4;
367 ret = ov8858_write_reg(client, OV8858_8BIT,
368 OV8858_LONG_EXPO+2, exp_val & 0xFF);
369 if (ret)
370 return ret;
371
372 ret = ov8858_write_reg(client, OV8858_8BIT,
373 OV8858_LONG_EXPO+1, (exp_val >> 8) & 0xFF);
374 if (ret)
375 return ret;
376
377 ret = ov8858_write_reg(client, OV8858_8BIT,
378 OV8858_LONG_EXPO, (exp_val >> 16) & 0x0F);
379 if (ret)
380 return ret;
381
382 /* Digital gain : to all MWB channel gains */
383 if (dig_gain) {
384 ret = ov8858_write_reg(client, OV8858_16BIT,
385 OV8858_MWB_RED_GAIN_H, dig_gain);
386 if (ret)
387 return ret;
388
389 ret = ov8858_write_reg(client, OV8858_16BIT,
390 OV8858_MWB_GREEN_GAIN_H, dig_gain);
391 if (ret)
392 return ret;
393
394 ret = ov8858_write_reg(client, OV8858_16BIT,
395 OV8858_MWB_BLUE_GAIN_H, dig_gain);
396 if (ret)
397 return ret;
398 }
399
400 ret = ov8858_write_reg(client, OV8858_16BIT, OV8858_LONG_GAIN,
401 gain & 0x07ff);
402 if (ret)
403 return ret;
404
405 dev->gain = gain;
406 dev->exposure = exposure;
407 dev->digital_gain = dig_gain;
408
409 return 0;
410}
411
412static int ov8858_set_exposure(struct v4l2_subdev *sd, int exposure, int gain,
413 int dig_gain)
414{
415 struct ov8858_device *dev = to_ov8858_sensor(sd);
416 const struct ov8858_resolution *res;
417 u16 hts, vts;
418 int ret;
419
420 mutex_lock(&dev->input_lock);
421
422 /* Validate exposure: cannot exceed 16bit value */
423 exposure = clamp_t(int, exposure, 0, OV8858_MAX_EXPOSURE_VALUE);
424
425 /* Validate gain: must not exceed maximum 8bit value */
426 gain = clamp_t(int, gain, 0, OV8858_MAX_GAIN_VALUE);
427
428 /* Validate digital gain: must not exceed 12 bit value*/
429 dig_gain = clamp_t(int, dig_gain, 0, OV8858_MWB_GAIN_MAX);
430
431 res = &dev->curr_res_table[dev->fmt_idx];
432 /*
433 * Vendor: HTS reg value is half the total pixel line
434 */
435 hts = res->fps_options[dev->fps_index].pixels_per_line;
436 vts = res->fps_options[dev->fps_index].lines_per_frame;
437
438 ret = __ov8858_set_exposure(sd, exposure, gain, dig_gain, &hts, &vts);
439
440 mutex_unlock(&dev->input_lock);
441
442 return ret;
443}
444
445/*
446 When exposure gain value set to sensor, the sensor changed value.
447 So we need the function to get real value
448 */
449static int ov8858_g_update_exposure(struct v4l2_subdev *sd,
450 struct atomisp_update_exposure *exposure)
451{
452 struct ov8858_device *dev = to_ov8858_sensor(sd);
453 struct i2c_client *client = v4l2_get_subdevdata(sd);
454 int gain = exposure->gain;
455
456 dev_dbg(&client->dev, "%s: gain: %d, digi_gain: %d\n", __func__,
457 exposure->gain, exposure->digi_gain);
458 exposure->update_digi_gain = dev->digital_gain;
459 /* This real gain value fetching function is provided by vendor */
460 exposure->update_gain = (((gain & 0x700) >> 8) + 1) * (gain & 0xFF);
461
462 return 0;
463}
464
465static int ov8858_s_exposure(struct v4l2_subdev *sd,
466 struct atomisp_exposure *exposure)
467{
468 return ov8858_set_exposure(sd, exposure->integration_time[0],
469 exposure->gain[0], exposure->gain[1]);
470}
471
472static int ov8858_priv_int_data_init(struct v4l2_subdev *sd)
473{
474 struct ov8858_device *dev = to_ov8858_sensor(sd);
475 struct i2c_client *client = v4l2_get_subdevdata(sd);
476 u32 size = OV8858_OTP_END_ADDR - OV8858_OTP_START_ADDR + 1;
477 int r;
478 u16 isp_ctrl2 = 0;
479
480 if (!dev->otp_data) {
481 dev->otp_data = devm_kzalloc(&client->dev, size, GFP_KERNEL);
482 if (!dev->otp_data) {
a49d2536
AC
483 r = -ENOMEM;
484 goto error3;
485 }
486
487 /* Streaming has to be on */
488 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_STREAM_MODE,
489 0x01);
490 if (r)
491 goto error2;
492
493 /* Turn off Dead Pixel Correction */
494 r = ov8858_read_reg(client, OV8858_8BIT,
495 OV8858_OTP_ISP_CTRL2, &isp_ctrl2);
496 if (r)
497 goto error1;
498
499 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
500 isp_ctrl2 & ~OV8858_OTP_DPC_ENABLE);
501 if (r)
502 goto error1;
503
504 /* Enable partial OTP read mode */
505 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_MODE_CTRL,
506 OV8858_OTP_MODE_PROGRAM_DISABLE |
507 OV8858_OTP_MODE_MANUAL);
508 if (r)
509 goto error1;
510
511 /* Set address range of OTP memory to read */
512 r = ov8858_write_reg(client, OV8858_16BIT,
513 OV8858_OTP_START_ADDR_REG,
514 OV8858_OTP_START_ADDR);
515 if (r)
516 goto error1;
517
518 r = ov8858_write_reg(client, OV8858_16BIT,
519 OV8858_OTP_END_ADDR_REG,
520 OV8858_OTP_END_ADDR);
521 if (r)
522 goto error1;
523
524 /* Load the OTP data into the OTP buffer */
525 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_LOAD_CTRL,
526 OV8858_OTP_LOAD_ENABLE);
527 if (r)
528 goto error1;
529
530 /* Wait for the data to load into the buffer */
531 usleep_range(5000, 5500);
532
533 /* Read the OTP data from the buffer */
534 r = ov8858_i2c_read(client, size, OV8858_OTP_START_ADDR,
535 dev->otp_data);
536 if (r)
537 goto error1;
538
539 /* Turn on Dead Pixel Correction */
540 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
541 isp_ctrl2 | OV8858_OTP_DPC_ENABLE);
542 if (r)
543 goto error1;
544
545 /* Stop streaming */
546 r = ov8858_write_reg(client, 1, OV8858_STREAM_MODE, 0x00);
547 if (r) {
548 dev_err(&client->dev, "%s: cannot turn off streaming\n",
549 __func__);
550 goto error1;
551 }
552 }
553
554
555 return 0;
556
557error1:
558 /* Turn on Dead Pixel Correction and set streaming off */
559 ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
560 isp_ctrl2 | OV8858_OTP_DPC_ENABLE);
561 ov8858_write_reg(client, 1, OV8858_STREAM_MODE, 0x00);
562error2:
563 devm_kfree(&client->dev, dev->otp_data);
564 dev->otp_data = NULL;
565error3:
566 dev_err(&client->dev, "%s: OTP reading failed\n", __func__);
567 return r;
568}
569
570static int ov8858_g_priv_int_data(struct v4l2_subdev *sd,
571 struct v4l2_private_int_data *priv)
572{
573 struct ov8858_device *dev = to_ov8858_sensor(sd);
574 struct i2c_client *client = v4l2_get_subdevdata(sd);
575 u32 size = OV8858_OTP_END_ADDR - OV8858_OTP_START_ADDR + 1;
576 int r;
577
578 mutex_lock(&dev->input_lock);
579
580 if (!dev->otp_data) {
581 dev_err(&client->dev, "%s: otp data is NULL\n", __func__);
582 mutex_unlock(&dev->input_lock);
583 return -EFAULT;
584 }
585
586 if (copy_to_user(priv->data, dev->otp_data,
587 min_t(__u32, priv->size, size))) {
588 r = -EFAULT;
589 dev_err(&client->dev, "%s: OTP reading failed\n", __func__);
590 mutex_unlock(&dev->input_lock);
591 return r;
592 }
593
594 priv->size = size;
595 mutex_unlock(&dev->input_lock);
596
597 return 0;
598}
599
600static int __ov8858_init(struct v4l2_subdev *sd)
601{
602 struct i2c_client *client = v4l2_get_subdevdata(sd);
603 struct ov8858_device *dev = to_ov8858_sensor(sd);
604 int ret;
605 dev_dbg(&client->dev, "%s\n", __func__);
606
607 /* Sets the default FPS */
608 dev->fps_index = 0;
609
610 /* Set default exposure values (initially start values) */
611 dev->exposure = 256;
612 dev->gain = 16;
613 dev->digital_gain = 1024;
614 dev->limit_exposure_flag = false;
615
616 dev_dbg(&client->dev, "%s: Writing basic settings to ov8858\n",
617 __func__);
618 ret = ov8858_write_reg_array(client, ov8858_BasicSettings);
619 if (ret)
620 return ret;
621
622 return ov8858_priv_int_data_init(sd);
623}
624
625static int ov8858_init(struct v4l2_subdev *sd, u32 val)
626{
627 struct ov8858_device *dev = to_ov8858_sensor(sd);
628 int ret;
629
630 mutex_lock(&dev->input_lock);
631 ret = __ov8858_init(sd);
632 mutex_unlock(&dev->input_lock);
633
634 return ret;
635}
636
637static void ov8858_uninit(struct v4l2_subdev *sd)
638{
639 struct i2c_client *client = v4l2_get_subdevdata(sd);
640 struct ov8858_device *dev = to_ov8858_sensor(sd);
641 struct v4l2_ctrl *ctrl;
642 dev_dbg(&client->dev, "%s:\n", __func__);
643
644 dev->exposure = 0;
645 dev->gain = 0;
646 dev->digital_gain = 0;
647 dev->limit_exposure_flag = false;
648 mutex_unlock(&dev->input_lock);
649 ctrl = v4l2_ctrl_find(sd->ctrl_handler,
650 V4L2_CID_EXPOSURE_AUTO_PRIORITY);
651 if (ctrl)
652 v4l2_ctrl_s_ctrl(ctrl, V4L2_EXPOSURE_AUTO);
653 mutex_lock(&dev->input_lock);
654}
655
656static int ov8858_g_comp_delay(struct v4l2_subdev *sd, unsigned int *usec)
657{
658 struct i2c_client *client = v4l2_get_subdevdata(sd);
659 struct ov8858_device *dev = to_ov8858_sensor(sd);
660 int ret = 0, exposure;
661 u16 vts, data;
662
663 if (dev->exposure == 0) {
664 ret = ov8858_read_reg(client, OV8858_16BIT,
665 OV8858_LONG_EXPO + 1, &data);
666 if (ret)
667 return ret;
668 exposure = data;
669 exposure >>= 4;
670 } else {
671 exposure = dev->exposure;
672 }
673
674 ret = ov8858_read_reg(client, OV8858_16BIT, OV8858_TIMING_VTS, &vts);
675 if (ret || vts == 0)
676 vts = OV8858_DEPTH_VTS_CONST;
677
678 *usec = (exposure * 33333 / vts);
679 if (*usec > OV8858_DEPTH_COMP_CONST)
680 *usec = *usec - OV8858_DEPTH_COMP_CONST;
681 else
682 *usec = OV8858_DEPTH_COMP_CONST;
683
684 return 0;
685}
686
687static long ov8858_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
688{
689 struct i2c_client *client = v4l2_get_subdevdata(sd);
690 switch (cmd) {
691 case ATOMISP_IOC_S_EXPOSURE:
692 return ov8858_s_exposure(sd, (struct atomisp_exposure *)arg);
693 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
694 return ov8858_g_priv_int_data(sd, arg);
695 case ATOMISP_IOC_G_DEPTH_SYNC_COMP:
696 return ov8858_g_comp_delay(sd, (unsigned int *)arg);
697 case ATOMISP_IOC_G_UPDATE_EXPOSURE:
698 return ov8858_g_update_exposure(sd,
699 (struct atomisp_update_exposure *)arg);
700 default:
701 dev_dbg(&client->dev, "Unhandled command 0x%X\n", cmd);
702 return -EINVAL;
703 }
704}
705
706static int __power_ctrl(struct v4l2_subdev *sd, bool flag)
707{
708 int ret = 0;
709 struct ov8858_device *dev = to_ov8858_sensor(sd);
710 struct i2c_client *client = v4l2_get_subdevdata(sd);
711
712 if (!dev || !dev->platform_data)
713 return -ENODEV;
714
715 /* Non-gmin platforms use the legacy callback */
716 if (dev->platform_data->power_ctrl)
717 return dev->platform_data->power_ctrl(sd, flag);
718
719 if (dev->platform_data->v1p2_ctrl) {
720 ret = dev->platform_data->v1p2_ctrl(sd, flag);
721 if (ret) {
722 dev_err(&client->dev,
723 "failed to power %s 1.2v power rail\n",
724 flag ? "up" : "down");
725 return ret;
726 }
727 }
728
729 if (dev->platform_data->v2p8_ctrl) {
730 ret = dev->platform_data->v2p8_ctrl(sd, flag);
731 if (ret) {
732 dev_err(&client->dev,
733 "failed to power %s 2.8v power rail\n",
734 flag ? "up" : "down");
735 return ret;
736 }
737 }
738
739 if (dev->platform_data->v1p8_ctrl) {
740 ret = dev->platform_data->v1p8_ctrl(sd, flag);
741 if (ret) {
742 dev_err(&client->dev,
743 "failed to power %s 1.8v power rail\n",
744 flag ? "up" : "down");
745 if (dev->platform_data->v2p8_ctrl)
746 dev->platform_data->v2p8_ctrl(sd, 0);
747 return ret;
748 }
749 }
750
751 if (flag)
752 msleep(20); /* Wait for power lines to stabilize */
753 return ret;
754}
755
756static int __gpio_ctrl(struct v4l2_subdev *sd, bool flag)
757{
758 struct i2c_client *client;
759 struct ov8858_device *dev;
760
761 if (!sd)
762 return -EINVAL;
763
764 client = v4l2_get_subdevdata(sd);
765 dev = to_ov8858_sensor(sd);
766
767 if (!client || !dev || !dev->platform_data)
768 return -ENODEV;
769
770 /* Non-gmin platforms use the legacy callback */
771 if (dev->platform_data->gpio_ctrl)
772 return dev->platform_data->gpio_ctrl(sd, flag);
773
774 if (dev->platform_data->gpio0_ctrl)
775 return dev->platform_data->gpio0_ctrl(sd, flag);
776
777 dev_err(&client->dev, "failed to find platform gpio callback\n");
778
779 return -EINVAL;
780}
781
782static int power_up(struct v4l2_subdev *sd)
783{
784 struct i2c_client *client = v4l2_get_subdevdata(sd);
785 struct ov8858_device *dev = to_ov8858_sensor(sd);
786 int ret;
787 dev_dbg(&client->dev, "%s\n", __func__);
788
789 /* Enable power */
790 ret = __power_ctrl(sd, 1);
791 if (ret) {
792 dev_err(&client->dev, "power rail on failed %d.\n", ret);
793 goto fail_power;
794 }
795
796 /* Enable clock */
797 ret = dev->platform_data->flisclk_ctrl(sd, 1);
798 if (ret) {
799 dev_err(&client->dev, "flisclk on failed %d\n", ret);
800 goto fail_clk;
801 }
802
803 /* Release reset */
804 ret = __gpio_ctrl(sd, 1);
805 if (ret) {
806 dev_err(&client->dev, "gpio on failed %d\n", ret);
807 goto fail_gpio;
808 }
809
810 /* Minumum delay is 8192 clock cycles before first i2c transaction,
811 * which is 1.37 ms at the lowest allowed clock rate 6 MHz */
812 usleep_range(2000, 2500);
813 return 0;
814
815fail_gpio:
816 dev->platform_data->flisclk_ctrl(sd, 0);
817fail_clk:
818 __power_ctrl(sd, 0);
819fail_power:
820 dev_err(&client->dev, "Sensor power-up failed\n");
821
822 return ret;
823}
824
825static int power_down(struct v4l2_subdev *sd)
826{
827 struct ov8858_device *dev = to_ov8858_sensor(sd);
828 struct i2c_client *client = v4l2_get_subdevdata(sd);
829 int ret;
830 dev_dbg(&client->dev, "%s\n", __func__);
831
832 ret = dev->platform_data->flisclk_ctrl(sd, 0);
833 if (ret)
834 dev_err(&client->dev, "flisclk off failed\n");
835
836 ret = __gpio_ctrl(sd, 0);
837 if (ret)
838 dev_err(&client->dev, "gpio off failed\n");
839
840 ret = __power_ctrl(sd, 0);
841 if (ret)
842 dev_err(&client->dev, "power rail off failed.\n");
843
844 return ret;
845}
846
847static int __ov8858_s_power(struct v4l2_subdev *sd, int on)
848{
849 struct ov8858_device *dev = to_ov8858_sensor(sd);
850 int ret, r = 0;
851
852 if (on == 0) {
853 ov8858_uninit(sd);
854 if (dev->vcm_driver && dev->vcm_driver->power_down)
855 r = dev->vcm_driver->power_down(sd);
856 ret = power_down(sd);
857 if (r != 0 && ret == 0)
858 ret = r;
859 } else {
860 ret = power_up(sd);
861 if (ret)
862 power_down(sd);
863 if (dev->vcm_driver && dev->vcm_driver->power_up) {
864 ret = dev->vcm_driver->power_up(sd);
865 if (ret) {
866 power_down(sd);
867 return ret;
868 }
869 }
870 return __ov8858_init(sd);
871 }
872
873 return ret;
874}
875
876static int ov8858_s_power(struct v4l2_subdev *sd, int on)
877{
878 int ret;
879 struct ov8858_device *dev = to_ov8858_sensor(sd);
880
881 mutex_lock(&dev->input_lock);
882 ret = __ov8858_s_power(sd, on);
883 mutex_unlock(&dev->input_lock);
884
885 /*
886 * FIXME: Compatibility with old behaviour: return to preview
887 * when the device is power cycled.
888 */
889 if (!ret && on)
890 v4l2_ctrl_s_ctrl(dev->run_mode, ATOMISP_RUN_MODE_PREVIEW);
891
892 return ret;
893}
894
895/*
896 * Return value of the specified register, first try getting it from
897 * the register list and if not found, get from the sensor via i2c.
898 */
899static int ov8858_get_register(struct v4l2_subdev *sd, int reg, int type,
900 const struct ov8858_reg *reglist)
901{
902 struct i2c_client *client = v4l2_get_subdevdata(sd);
903 const struct ov8858_reg *next;
904 u16 val;
905
906 /* Try if the values are in the register list */
907 for (next = reglist; next->type != OV8858_TOK_TERM; next++) {
908 if (next->sreg == reg) {
909 if (type == OV8858_8BIT)
910 return next->val;
911
912 if (type == OV8858_16BIT &&
913 next[1].type != OV8858_TOK_TERM)
914 return next[0].val << 8 | next[1].val;
915 }
916 }
917
918 /* If not, read from sensor */
919 if (ov8858_read_reg(client, type, reg, &val)) {
920 dev_err(&client->dev, "failed to read register 0x%08x\n", reg);
921 return -EIO;
922 }
923
924 return val;
925}
926
927static inline int ov8858_get_register_16bit(struct v4l2_subdev *sd, int reg,
928 const struct ov8858_reg *reglist)
929{
930 return ov8858_get_register(sd, reg, OV8858_16BIT, reglist);
931}
932
933static inline int ov8858_get_register_8bit(struct v4l2_subdev *sd, int reg,
934 const struct ov8858_reg *reglist)
935{
936 return ov8858_get_register(sd, reg, OV8858_8BIT, reglist);
937}
938
939static int __ov8858_get_pll1_values(struct v4l2_subdev *sd,
940 int *value,
941 const struct ov8858_reg *reglist)
942{
943 struct i2c_client *client = v4l2_get_subdevdata(sd);
944 unsigned int prediv_idx;
945 unsigned int multiplier;
946 unsigned int sys_prediv;
947 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
948 int ret;
949
950 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV0, reglist);
951 if (ret < 0)
952 return ret;
953
954 if (ret & OV8858_PLL1_PREDIV0_MASK)
955 *value /= 2;
956
957 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV, reglist);
958
959 if (ret < 0)
960 return ret;
961
962 prediv_idx = ret & OV8858_PLL1_PREDIV_MASK;
963 *value = *value * 2 / prediv_coef[prediv_idx];
964
965 ret = ov8858_get_register_16bit(sd, OV8858_PLL1_MULTIPLIER, reglist);
966 if (ret < 0)
967 return ret;
968
969 multiplier = ret;
970 *value *= multiplier & OV8858_PLL1_MULTIPLIER_MASK;
971 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_PRE_DIV, reglist);
972
973 if (ret < 0)
974 return ret;
975
976 sys_prediv = ret & OV8858_PLL1_SYS_PRE_DIV_MASK;
977 *value /= (sys_prediv + 3);
978 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_DIVIDER, reglist);
979
980 if (ret < 0)
981 return ret;
982
983 if (ret & OV8858_PLL1_SYS_DIVIDER_MASK)
984 *value /= 2;
985
986 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
987
988 return 0;
989}
990
991static int __ov8858_get_pll2a_values(struct v4l2_subdev *sd, int *value,
992 const struct ov8858_reg *reglist)
993{
994 struct i2c_client *client = v4l2_get_subdevdata(sd);
995 unsigned int prediv_idx;
996 unsigned int multiplier;
997 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
998 int ret;
999
1000 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV0, reglist);
1001 if (ret < 0)
1002 return ret;
1003
1004 if (ret & OV8858_PLL2_PREDIV0_MASK)
1005 *value /= 2;
1006
1007 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV, reglist);
1008 if (ret < 0)
1009 return ret;
1010
1011 prediv_idx = (ret & OV8858_PLL2_PREDIV_MASK);
1012 *value = *value * 2 / prediv_coef[prediv_idx];
1013
1014 ret = ov8858_get_register_16bit(sd, OV8858_PLL2_MULTIPLIER, reglist);
1015 if (ret < 0)
1016 return ret;
1017
1018 multiplier = ret;
1019 *value *= multiplier & OV8858_PLL2_MULTIPLIER_MASK;
1020 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1021
1022 return 0;
1023}
1024static int __ov8858_get_pll2b_values(struct v4l2_subdev *sd, int *value,
1025 const struct ov8858_reg *reglist)
1026{
1027 struct i2c_client *client = v4l2_get_subdevdata(sd);
1028 unsigned int dac_divider;
1029 int ret;
1030
1031 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_DAC_DIVIDER, reglist);
1032 if (ret < 0)
1033 return ret;
1034
1035 dac_divider = (ret & OV8858_PLL2_DAC_DIVIDER_MASK) + 1;
1036 *value /= dac_divider;
1037
1038 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1039
1040 return 0;
1041}
1042static int __ov8858_get_pll2c_values(struct v4l2_subdev *sd, int *value,
1043 const struct ov8858_reg *reglist)
1044{
1045 struct i2c_client *client = v4l2_get_subdevdata(sd);
1046 unsigned int sys_pre_div;
1047 unsigned int sys_divider_idx;
1048 unsigned int sys_divider_coef[] = {2, 3, 4, 5, 6, 7, 8, 10};
1049 int ret;
1050
1051 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_PRE_DIV, reglist);
1052 if (ret < 0)
1053 return ret;
1054
1055 sys_pre_div = (ret & OV8858_PLL2_SYS_PRE_DIV_MASK) + 1;
1056 *value /= sys_pre_div;
1057
1058 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_DIVIDER, reglist);
1059 if (ret < 0)
1060 return ret;
1061
1062 sys_divider_idx = ret & OV8858_PLL2_SYS_DIVIDER_MASK;
1063 *value *= 2 / sys_divider_coef[sys_divider_idx];
1064
1065 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1066
1067 return 0;
1068}
1069
1070static int ov8858_get_intg_factor(struct v4l2_subdev *sd,
1071 struct camera_mipi_info *info,
1072 const struct ov8858_reg *reglist)
1073{
1074 const unsigned int ext_clk = 19200000; /* Hz */
1075 struct atomisp_sensor_mode_data *m = &info->data;
1076 struct ov8858_device *dev = to_ov8858_sensor(sd);
1077 struct i2c_client *client = v4l2_get_subdevdata(sd);
1078 struct device *d = &client->dev;
1079 const struct ov8858_resolution *res =
1080 &dev->curr_res_table[dev->fmt_idx];
1081 unsigned int pll_sclksel1;
1082 unsigned int pll_sclksel2;
1083 unsigned int sys_pre_div;
1084 unsigned int sclk_pdiv;
1085 unsigned int sclk = ext_clk;
1086 u16 hts;
1087 int ret;
1088
1089 memset(&info->data, 0, sizeof(info->data));
1090
1091 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL1, reglist);
1092 if (ret < 0)
1093 return ret;
1094
1095 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL1: 0x%02x\n", __func__, ret);
1096 pll_sclksel1 = ret & OV8858_PLL_SCLKSEL1_MASK;
1097
1098 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL2, reglist);
1099 if (ret < 0)
1100 return ret;
1101
1102 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL2: 0x%02x\n", __func__, ret);
1103 pll_sclksel2 = ret & OV8858_PLL_SCLKSEL2_MASK;
1104
1105 if (pll_sclksel2) {
1106 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1107 if (ret < 0)
1108 return ret;
1109 ret = __ov8858_get_pll2b_values(sd, &sclk, reglist);
1110 if (ret < 0)
1111 return ret;
1112 } else if (pll_sclksel1) {
1113 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1114 if (ret < 0)
1115 return ret;
1116 ret = __ov8858_get_pll2c_values(sd, &sclk, reglist);
1117 if (ret < 0)
1118 return ret;
1119 } else {
1120 ret = __ov8858_get_pll1_values(sd, &sclk, reglist);
1121 if (ret < 0)
1122 return ret;
1123 }
1124
1125 ret = ov8858_get_register_8bit(sd, OV8858_SRB_HOST_INPUT_DIS, reglist);
1126 if (ret < 0)
1127 return ret;
1128
1129 dev_dbg(d, "%s: OV8858_SRB_HOST_INPUT_DIS: 0x%02x\n", __func__, ret);
1130
1131 sys_pre_div = ret & OV8858_SYS_PRE_DIV_MASK;
1132 sys_pre_div >>= OV8858_SYS_PRE_DIV_OFFSET;
1133
1134 if (sys_pre_div == 1)
1135 sclk /= 2;
1136 else if (sys_pre_div == 2)
1137 sclk /= 4;
1138
1139 sclk_pdiv = ret & OV8858_SCLK_PDIV_MASK;
1140 sclk_pdiv >>= OV8858_SCLK_PDIV_OFFSET;
1141
1142 if (sclk_pdiv > 1)
1143 sclk /= sclk_pdiv;
1144
1145 dev_dbg(d, "%s: sclk: %d\n", __func__, sclk);
1146
1147 dev->vt_pix_clk_freq_mhz = sclk;
1148 m->vt_pix_clk_freq_mhz = sclk;
1149
1150 /* HTS and VTS */
1151 m->frame_length_lines =
1152 res->fps_options[dev->fps_index].lines_per_frame;
1153 m->line_length_pck = res->fps_options[dev->fps_index].pixels_per_line;
1154
1155 m->coarse_integration_time_min = 0;
1156 m->coarse_integration_time_max_margin = OV8858_INTEGRATION_TIME_MARGIN;
1157 ret = ov8858_read_reg(client, OV8858_16BIT, OV8858_TIMING_HTS, &hts);
1158 if (ret < 0)
1159 return ret;
1160 m->hts = hts;
1161 dev_dbg(&client->dev, "%s: get HTS %d\n", __func__, hts);
1162
1163 /* OV Sensor do not use fine integration time. */
1164 m->fine_integration_time_min = 0;
1165 m->fine_integration_time_max_margin = 0;
1166
1167 /*
1168 * read_mode indicate whether binning is used for calculating
1169 * the correct exposure value from the user side. So adapt the
1170 * read mode values accordingly.
1171 */
1172 m->read_mode = res->bin_factor_x ?
1173 OV8858_READ_MODE_BINNING_ON : OV8858_READ_MODE_BINNING_OFF;
1174
1175 ret = ov8858_get_register_8bit(sd, OV8858_H_INC_ODD, res->regs);
1176 if (ret < 0)
1177 return ret;
1178 m->binning_factor_x = (ret + 1) / 2;
1179
1180 ret = ov8858_get_register_8bit(sd, OV8858_V_INC_ODD, res->regs);
1181 if (ret < 0)
1182 return ret;
1183 m->binning_factor_y = (ret + 1) / 2;
1184
1185 /* Get the cropping and output resolution to ISP for this mode. */
1186 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_START_H,
1187 res->regs);
1188 if (ret < 0)
1189 return ret;
1190
1191 m->crop_horizontal_start = ret;
1192
1193 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_START_H, res->regs);
1194 if (ret < 0)
1195 return ret;
1196
1197 m->crop_vertical_start = ret;
1198
1199 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_END_H, res->regs);
1200 if (ret < 0)
1201 return ret;
1202
1203 m->crop_horizontal_end = ret;
1204
1205 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_END_H, res->regs);
1206 if (ret < 0)
1207 return ret;
1208
1209 m->crop_vertical_end = ret;
1210
1211 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_OUTPUT_SIZE_H,
1212 res->regs);
1213 if (ret < 0)
1214 return ret;
1215
1216 m->output_width = ret;
1217
1218 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_OUTPUT_SIZE_H,
1219 res->regs);
1220 if (ret < 0)
1221 return ret;
1222
1223 m->output_height = ret;
1224
1225 return 0;
1226}
1227
1228/*
1229 * distance - calculate the distance
1230 * @res: resolution
1231 * @w: width
1232 * @h: height
1233 *
1234 * Get the gap between res_w/res_h and w/h.
1235 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1236 * res->width/height smaller than w/h wouldn't be considered.
1237 * The gap of ratio larger than 1/8 wouldn't be considered.
1238 * Returns the value of gap or -1 if fail.
1239 */
1240#define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1241static int distance(struct ov8858_resolution const *res, const u32 w,
1242 const u32 h)
1243{
1244 int ratio;
1245 int distance;
1246
1247 if (w == 0 || h == 0 ||
1248 res->width < w || res->height < h)
1249 return -1;
1250
6c492211 1251 ratio = res->width << 13;
a49d2536
AC
1252 ratio /= w;
1253 ratio *= h;
1254 ratio /= res->height;
1255
1256 distance = abs(ratio - 8192);
1257
1258 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1259 return -1;
1260 return distance;
1261}
1262
1263/*
1264 * Returns the nearest higher resolution index.
1265 * @w: width
1266 * @h: height
1267 * matching is done based on enveloping resolution and
1268 * aspect ratio. If the aspect ratio cannot be matched
1269 * to any index, -1 is returned.
1270 */
1271static int nearest_resolution_index(struct v4l2_subdev *sd, int w, int h)
1272{
1273 int i;
1274 int idx = -1;
1275 int dist;
1276 int fps_diff;
1277 int min_fps_diff = INT_MAX;
1278 int min_dist = INT_MAX;
1279 int min_res_w = INT_MAX;
1280 const struct ov8858_resolution *tmp_res = NULL;
1281 struct i2c_client *client = v4l2_get_subdevdata(sd);
1282 struct ov8858_device *dev = to_ov8858_sensor(sd);
1283 dev_dbg(&client->dev, "%s: w=%d, h=%d\n", __func__, w, h);
1284
1285 for (i = 0; i < dev->entries_curr_table; i++) {
1286 tmp_res = &dev->curr_res_table[i];
1287 dist = distance(tmp_res, w, h);
1288 dev_dbg(&client->dev,
1289 "%s[%d]: %dx%d distance=%d\n", tmp_res->desc,
1290 i, tmp_res->width, tmp_res->height, dist);
1291 if (dist == -1)
1292 continue;
1293 if (dist < min_dist) {
1294 min_dist = dist;
1295 min_res_w = tmp_res->width;
1296 min_fps_diff = __ov8858_min_fps_diff(dev->fps,
1297 tmp_res->fps_options);
1298 idx = i;
1299 }
1300 if (dist == min_dist) {
1301 fps_diff = __ov8858_min_fps_diff(dev->fps,
1302 tmp_res->fps_options);
1303 if (fps_diff < min_fps_diff) {
1304 min_fps_diff = fps_diff;
1305 idx = i;
1306 }
1307 if (tmp_res->width < min_res_w) {
1308 min_res_w = tmp_res->width;
1309 idx = i;
1310 }
1311 }
1312 }
1313
1314 return idx;
1315}
1316
1317static int ov8858_set_fmt(struct v4l2_subdev *sd,
1318 struct v4l2_subdev_pad_config *cfg,
1319 struct v4l2_subdev_format *format)
1320{
1321 struct v4l2_mbus_framefmt *fmt = &format->format;
1322 struct ov8858_device *dev = to_ov8858_sensor(sd);
1323 struct camera_mipi_info *ov8858_info = NULL;
1324 struct i2c_client *client = v4l2_get_subdevdata(sd);
1325 const struct ov8858_resolution *res;
1326 int ret;
1327 int idx;
1328 if (format->pad)
1329 return -EINVAL;
1330 if (!fmt)
1331 return -EINVAL;
1332
1333 ov8858_info = v4l2_get_subdev_hostdata(sd);
1334 if (ov8858_info == NULL)
1335 return -EINVAL;
1336
1337 mutex_lock(&dev->input_lock);
1338
1339 if ((fmt->width > OV8858_RES_WIDTH_MAX) ||
1340 (fmt->height > OV8858_RES_HEIGHT_MAX)) {
1341 fmt->width = OV8858_RES_WIDTH_MAX;
1342 fmt->height = OV8858_RES_HEIGHT_MAX;
1343 } else {
1344 idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1345
1346 /*
1347 * nearest_resolution_index() doesn't return smaller
1348 * resolutions. If it fails, it means the requested resolution
1349 * is higher than we can support. Fallback to highest possible
1350 * resolution in this case.
1351 */
1352 if (idx == -1)
1353 idx = dev->entries_curr_table - 1;
1354
1355 fmt->width = dev->curr_res_table[idx].width;
1356 fmt->height = dev->curr_res_table[idx].height;
1357 }
1358
1359 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1360 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1361 cfg->try_fmt = *fmt;
1362 mutex_unlock(&dev->input_lock);
1363 return 0;
1364 }
1365
1366 dev->fmt_idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1367 if (dev->fmt_idx == -1) {
1368 ret = -EINVAL;
1369 goto out;
1370 }
1371 res = &dev->curr_res_table[dev->fmt_idx];
1372 dev_dbg(&client->dev, "%s: selected width = %d, height = %d\n",
1373 __func__, res->width, res->height);
1374
1375 /* Adjust the FPS selection based on the resolution selected */
1376 dev->fps_index = __ov8858_nearest_fps_index(dev->fps, res->fps_options);
1377 dev->fps = res->fps_options[dev->fps_index].fps;
1378 dev->regs = res->fps_options[dev->fps_index].regs;
1379 if (!dev->regs)
1380 dev->regs = res->regs;
1381
1382 ret = ov8858_write_reg_array(client, dev->regs);
1383 if (ret)
1384 goto out;
1385
1386 dev->pixels_per_line = res->fps_options[dev->fps_index].pixels_per_line;
1387 dev->lines_per_frame = res->fps_options[dev->fps_index].lines_per_frame;
1388
1389 /* ov8858 only support RGB RAW10 output */
1390 ov8858_info->metadata_width = res->width * 10 / 8;
1391 ov8858_info->metadata_height = 2;
1392 ov8858_info->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1393
1394 /* Set the initial exposure */
1395 ret = __ov8858_set_exposure(sd, dev->exposure, dev->gain,
1396 dev->digital_gain, &dev->pixels_per_line,
1397 &dev->lines_per_frame);
1398 if (ret)
1399 goto out;
1400
1401 ret = ov8858_get_intg_factor(sd, ov8858_info, dev->regs);
1402
1403out:
1404 mutex_unlock(&dev->input_lock);
1405
1406 return ret;
1407}
1408
1409static int ov8858_get_fmt(struct v4l2_subdev *sd,
1410 struct v4l2_subdev_pad_config *cfg,
1411 struct v4l2_subdev_format *format)
1412{
1413 struct v4l2_mbus_framefmt *fmt = &format->format;
1414 struct ov8858_device *dev = to_ov8858_sensor(sd);
1415
1416 if (format->pad)
1417 return -EINVAL;
1418 if (!fmt)
1419 return -EINVAL;
1420
1421 mutex_lock(&dev->input_lock);
1422 fmt->width = dev->curr_res_table[dev->fmt_idx].width;
1423 fmt->height = dev->curr_res_table[dev->fmt_idx].height;
1424 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1425 mutex_unlock(&dev->input_lock);
1426
1427 return 0;
1428}
1429
1430static int ov8858_detect(struct i2c_client *client, u16 *id)
1431{
1432 struct i2c_adapter *adapter = client->adapter;
1433 u16 id_hi = 0;
1434 u16 id_low = 0;
1435 int ret;
1436
1437 /* i2c check */
1438 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1439 return -ENODEV;
1440
1441 dev_dbg(&client->dev, "%s: I2C functionality ok\n", __func__);
1442 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_HIGH, &id_hi);
1443 if (ret)
1444 return ret;
1445 dev_dbg(&client->dev, "%s: id_high = 0x%04x\n", __func__, id_hi);
1446 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_LOW, &id_low);
1447 if (ret)
1448 return ret;
1449 dev_dbg(&client->dev, "%s: id_low = 0x%04x\n", __func__, id_low);
1450 *id = (id_hi << 8) | id_low;
1451
1452 dev_dbg(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1453
1454 dev_info(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1455 if (*id != OV8858_CHIP_ID)
1456 return -ENODEV;
1457
1458 /* Stream off now. */
1459 return ov8858_write_reg(client, OV8858_8BIT, OV8858_STREAM_MODE, 0);
1460}
1461
1462static void __ov8858_print_timing(struct v4l2_subdev *sd)
1463{
1464 struct ov8858_device *dev = to_ov8858_sensor(sd);
1465 struct i2c_client *client = v4l2_get_subdevdata(sd);
1466 u16 width = dev->curr_res_table[dev->fmt_idx].width;
1467 u16 height = dev->curr_res_table[dev->fmt_idx].height;
1468
1469 dev_dbg(&client->dev, "Dump ov8858 timing in stream on:\n");
1470 dev_dbg(&client->dev, "width: %d:\n", width);
1471 dev_dbg(&client->dev, "height: %d:\n", height);
1472 dev_dbg(&client->dev, "pixels_per_line: %d:\n", dev->pixels_per_line);
1473 dev_dbg(&client->dev, "line per frame: %d:\n", dev->lines_per_frame);
1474 dev_dbg(&client->dev, "pix freq: %d:\n", dev->vt_pix_clk_freq_mhz);
1475 /* updated formula: pixels_per_line = 2 * HTS */
1476 /* updated formula: fps = SCLK / (VTS * HTS) */
1477 dev_dbg(&client->dev, "init fps: %d:\n", dev->vt_pix_clk_freq_mhz /
1478 (dev->pixels_per_line / 2) / dev->lines_per_frame);
1479 dev_dbg(&client->dev, "HBlank: %d nS:\n",
1480 1000 * (dev->pixels_per_line - width) /
1481 (dev->vt_pix_clk_freq_mhz / 1000000));
1482 dev_dbg(&client->dev, "VBlank: %d uS:\n",
1483 (dev->lines_per_frame - height) * dev->pixels_per_line /
1484 (dev->vt_pix_clk_freq_mhz / 1000000));
1485}
1486
1487/*
1488 * ov8858 stream on/off
1489 */
1490static int ov8858_s_stream(struct v4l2_subdev *sd, int enable)
1491{
1492 struct ov8858_device *dev = to_ov8858_sensor(sd);
1493 struct i2c_client *client = v4l2_get_subdevdata(sd);
1494 int ret;
1495 u16 val;
1496 dev_dbg(&client->dev, "%s: enable = %d\n", __func__, enable);
1497
1498 /* Set orientation */
1499 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT2, &val);
1500 if (ret)
1501 return ret;
1502
1503 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT2,
1504 dev->hflip ? val | OV8858_FLIP_ENABLE :
1505 val & ~OV8858_FLIP_ENABLE);
1506 if (ret)
1507 return ret;
1508
1509 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT1, &val);
1510 if (ret)
1511 return ret;
1512
1513 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT1,
1514 dev->vflip ? val | OV8858_FLIP_ENABLE :
1515 val & ~OV8858_FLIP_ENABLE);
1516 if (ret)
1517 return ret;
1518
1519 mutex_lock(&dev->input_lock);
1520 if (enable) {
1521 __ov8858_print_timing(sd);
1522 ret = ov8858_write_reg_array(client, ov8858_streaming);
1523 if (ret != 0) {
1524 dev_err(&client->dev, "write_reg_array err\n");
1525 goto out;
1526 }
1527 dev->streaming = 1;
1528 } else {
1529 ret = ov8858_write_reg_array(client, ov8858_soft_standby);
1530 if (ret != 0) {
1531 dev_err(&client->dev, "write_reg_array err\n");
1532 goto out;
1533 }
1534 dev->streaming = 0;
1535 dev->fps_index = 0;
1536 dev->fps = 0;
1537 }
1538out:
1539 mutex_unlock(&dev->input_lock);
1540 return ret;
1541}
1542
1543static int __update_ov8858_device_settings(struct ov8858_device *dev,
1544 u16 sensor_id)
1545{
1546 if (sensor_id == OV8858_CHIP_ID)
1547#ifdef CONFIG_PLATFORM_BTNS
1548 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
1549#else
1550 dev->vcm_driver = &ov8858_vcms[OV8858_SUNNY];
1551#endif
1552 else
1553 return -ENODEV;
1554
1555 if (dev->vcm_driver && dev->vcm_driver->init)
1556 return dev->vcm_driver->init(&dev->sd);
1557
1558 return 0;
1559}
1560
1561static int ov8858_s_config(struct v4l2_subdev *sd,
1562 int irq, void *pdata)
1563{
1564 struct ov8858_device *dev = to_ov8858_sensor(sd);
1565 struct i2c_client *client = v4l2_get_subdevdata(sd);
1566 u16 sensor_id;
1567 int ret;
1568
1569 if (pdata == NULL)
1570 return -ENODEV;
1571
1572 dev->platform_data = pdata;
1573
1574 mutex_lock(&dev->input_lock);
1575
1576 if (dev->platform_data->platform_init) {
1577 ret = dev->platform_data->platform_init(client);
1578 if (ret) {
1579 mutex_unlock(&dev->input_lock);
1580 dev_err(&client->dev, "platform init error %d!\n", ret);
1581 return ret;
1582 }
1583 }
1584
1585 ret = __ov8858_s_power(sd, 1);
1586 if (ret) {
1587 dev_err(&client->dev, "power-up error %d!\n", ret);
1588 mutex_unlock(&dev->input_lock);
1589 return ret;
1590 }
1591
1592 ret = dev->platform_data->csi_cfg(sd, 1);
1593 if (ret)
1594 goto fail_csi_cfg;
1595
1596 /* config & detect sensor */
1597 ret = ov8858_detect(client, &sensor_id);
1598 if (ret) {
1599 dev_err(&client->dev, "detect error %d!\n", ret);
1600 goto fail_detect;
1601 }
1602
1603 dev->sensor_id = sensor_id;
1604
1605 /* power off sensor */
1606 ret = __ov8858_s_power(sd, 0);
1607 if (ret) {
1608 dev->platform_data->csi_cfg(sd, 0);
1609 dev_err(&client->dev, "__ov8858_s_power-down error %d!\n", ret);
1610 goto fail_update;
1611 }
1612
1613 /* Resolution settings depend on sensor type and platform */
1614 ret = __update_ov8858_device_settings(dev, dev->sensor_id);
1615 if (ret) {
1616 dev->platform_data->csi_cfg(sd, 0);
1617 dev_err(&client->dev, "__update_ov8858_device_settings error %d!\n", ret);
1618 goto fail_update;
1619 }
1620
1621 mutex_unlock(&dev->input_lock);
1622 return ret;
1623
1624fail_detect:
1625 dev->platform_data->csi_cfg(sd, 0);
1626fail_csi_cfg:
1627 __ov8858_s_power(sd, 0);
1628fail_update:
1629 if (dev->platform_data->platform_deinit)
1630 dev->platform_data->platform_deinit();
1631 mutex_unlock(&dev->input_lock);
1632 dev_err(&client->dev, "sensor power-gating failed\n");
1633 return ret;
1634}
1635
1636static int
1637ov8858_enum_mbus_code(struct v4l2_subdev *sd,
1638 struct v4l2_subdev_pad_config *cfg,
1639 struct v4l2_subdev_mbus_code_enum *code)
1640{
1641 if (code->index)
1642 return -EINVAL;
1643 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1644
1645 return 0;
1646}
1647
1648static int
1649ov8858_enum_frame_size(struct v4l2_subdev *sd,
1650 struct v4l2_subdev_pad_config *cfg,
1651 struct v4l2_subdev_frame_size_enum *fse)
1652{
1653 int index = fse->index;
1654 struct ov8858_device *dev = to_ov8858_sensor(sd);
1655
1656 mutex_lock(&dev->input_lock);
1657 if (index >= dev->entries_curr_table) {
1658 mutex_unlock(&dev->input_lock);
1659 return -EINVAL;
1660 }
1661
1662 fse->min_width = dev->curr_res_table[index].width;
1663 fse->min_height = dev->curr_res_table[index].height;
1664 fse->max_width = dev->curr_res_table[index].width;
1665 fse->max_height = dev->curr_res_table[index].height;
1666 mutex_unlock(&dev->input_lock);
1667
1668 return 0;
1669}
1670
1671static int ov8858_s_ctrl(struct v4l2_ctrl *ctrl)
1672{
1673 struct ov8858_device *dev = container_of(
1674 ctrl->handler, struct ov8858_device, ctrl_handler);
1675 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1676
1677 /* input_lock is taken by the control framework, so it
1678 * doesn't need to be taken here.
1679 */
1680
1681 switch (ctrl->id) {
1682 case V4L2_CID_RUN_MODE:
1683 switch (ctrl->val) {
1684 case ATOMISP_RUN_MODE_VIDEO:
1685 dev->curr_res_table = ov8858_res_video;
1686 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_video);
1687 break;
1688 case ATOMISP_RUN_MODE_STILL_CAPTURE:
1689 dev->curr_res_table = ov8858_res_still;
1690 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_still);
1691 break;
1692 default:
1693 dev->curr_res_table = ov8858_res_preview;
1694 dev->entries_curr_table =
1695 ARRAY_SIZE(ov8858_res_preview);
1696 }
1697
1698 dev->fmt_idx = 0;
1699 dev->fps_index = 0;
1700
1701 return 0;
1702 case V4L2_CID_FOCUS_ABSOLUTE:
1703 if (dev->vcm_driver && dev->vcm_driver->t_focus_abs)
1704 return dev->vcm_driver->t_focus_abs(&dev->sd,
1705 ctrl->val);
1706 return 0;
1707 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1708 if (ctrl->val == V4L2_EXPOSURE_AUTO)
1709 dev->limit_exposure_flag = false;
1710 else if (ctrl->val == V4L2_EXPOSURE_APERTURE_PRIORITY)
1711 dev->limit_exposure_flag = true;
1712 return 0;
1713 case V4L2_CID_HFLIP:
1714 dev->hflip = ctrl->val;
1715 return 0;
1716 case V4L2_CID_VFLIP:
1717 dev->vflip = ctrl->val;
1718 return 0;
1719 default:
1720 dev_err(&client->dev, "%s: Error: Invalid ctrl: 0x%X\n",
1721 __func__, ctrl->id);
1722 return -EINVAL;
1723 }
1724}
1725
1726static int ov8858_g_ctrl(struct v4l2_ctrl *ctrl)
1727{
1728 struct ov8858_device *dev = container_of(
1729 ctrl->handler, struct ov8858_device, ctrl_handler);
1730 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1731 int r_odd, r_even;
1732 int i = dev->fmt_idx;
1733
1734 switch (ctrl->id) {
1735 case V4L2_CID_FOCUS_STATUS:
1736 if (dev->vcm_driver && dev->vcm_driver->q_focus_status)
1737 return dev->vcm_driver->q_focus_status(&dev->sd,
1738 &(ctrl->val));
1739 return 0;
1740 case V4L2_CID_BIN_FACTOR_HORZ:
1741 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_ODD,
1742 dev->curr_res_table[i].regs);
1743 if (r_odd < 0)
1744 return r_odd;
1745 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_EVEN,
1746 dev->curr_res_table[i].regs);
1747 if (r_even < 0)
1748 return r_even;
1749 ctrl->val = fls(r_odd + (r_even)) - 2;
1750 return 0;
1751
1752 case V4L2_CID_BIN_FACTOR_VERT:
1753 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_ODD,
1754 dev->curr_res_table[i].regs);
1755 if (r_odd < 0)
1756 return r_odd;
1757 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_EVEN,
1758 dev->curr_res_table[i].regs);
1759 if (r_even < 0)
1760 return r_even;
1761 ctrl->val = fls(r_odd + (r_even)) - 2;
1762 return 0;
1763 case V4L2_CID_HFLIP:
1764 ctrl->val = dev->hflip;
1765 break;
1766 case V4L2_CID_VFLIP:
1767 ctrl->val = dev->vflip;
1768 break;
1769 case V4L2_CID_EXPOSURE_ABSOLUTE:
1770 ctrl->val = dev->exposure;
1771 break;
1772 default:
1773 dev_warn(&client->dev,
1774 "%s: Error: Invalid ctrl: 0x%X\n", __func__, ctrl->id);
1775 return -EINVAL;
1776 }
1777
1778 return 0;
1779}
1780
1781static int
1782ov8858_g_frame_interval(struct v4l2_subdev *sd,
1783 struct v4l2_subdev_frame_interval *interval)
1784{
1785 struct ov8858_device *dev = to_ov8858_sensor(sd);
1786 const struct ov8858_resolution *res =
1787 &dev->curr_res_table[dev->fmt_idx];
1788
1789 mutex_lock(&dev->input_lock);
1790 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1791 interval->interval.numerator = 1;
1792 mutex_unlock(&dev->input_lock);
1793 return 0;
1794}
1795
1796static int __ov8858_s_frame_interval(struct v4l2_subdev *sd,
1797 struct v4l2_subdev_frame_interval *interval)
1798{
1799 struct ov8858_device *dev = to_ov8858_sensor(sd);
1800 struct i2c_client *client = v4l2_get_subdevdata(sd);
1801 const struct ov8858_resolution *res =
1802 &dev->curr_res_table[dev->fmt_idx];
1803 struct camera_mipi_info *info = NULL;
1804 unsigned int fps_index;
1805 int ret = 0;
1806 int fps;
1807
1808 info = v4l2_get_subdev_hostdata(sd);
1809 if (info == NULL)
1810 return -EINVAL;
1811
1812 if (!interval->interval.numerator)
1813 interval->interval.numerator = 1;
1814
1815 fps = interval->interval.denominator / interval->interval.numerator;
1816
1817 /* No need to proceed further if we are not streaming */
1818 if (!dev->streaming) {
1819 /* Save the new FPS and use it while selecting setting */
1820 dev->fps = fps;
1821 return 0;
1822 }
1823
1824 /* Ignore if we are already using the required FPS. */
1825 if (fps == res->fps_options[dev->fps_index].fps)
1826 return 0;
1827
1828 fps_index = __ov8858_nearest_fps_index(fps, res->fps_options);
1829
1830 if (res->fps_options[fps_index].regs &&
1831 res->fps_options[fps_index].regs != dev->regs) {
1832 dev_err(&client->dev,
1833 "Sensor is streaming, can't apply new configuration\n");
1834 return -EBUSY;
1835 }
1836
1837 dev->fps_index = fps_index;
1838 dev->fps = res->fps_options[dev->fps_index].fps;
1839
1840 /* Update the new frametimings based on FPS */
1841 dev->pixels_per_line =
1842 res->fps_options[dev->fps_index].pixels_per_line;
1843 dev->lines_per_frame =
1844 res->fps_options[dev->fps_index].lines_per_frame;
1845
1846 /* update frametiming. Conside the curren exposure/gain as well */
1847 ret = __ov8858_update_frame_timing(sd,
1848 &dev->pixels_per_line, &dev->lines_per_frame);
1849 if (ret)
1850 return ret;
1851
1852 /* Update the new values so that user side knows the current settings */
1853 ret = ov8858_get_intg_factor(sd, info, dev->regs);
1854 if (ret)
1855 return ret;
1856
1857 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1858 interval->interval.numerator = 1;
1859 __ov8858_print_timing(sd);
1860
1861 return ret;
1862}
1863
1864static int ov8858_s_frame_interval(struct v4l2_subdev *sd,
1865 struct v4l2_subdev_frame_interval *interval)
1866{
1867 struct ov8858_device *dev = to_ov8858_sensor(sd);
1868 int ret;
1869
1870 mutex_lock(&dev->input_lock);
1871 ret = __ov8858_s_frame_interval(sd, interval);
1872 mutex_unlock(&dev->input_lock);
1873
1874 return ret;
1875}
1876
1877static int ov8858_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1878{
1879 struct ov8858_device *dev = to_ov8858_sensor(sd);
1880
1881 mutex_lock(&dev->input_lock);
1882 *frames = dev->curr_res_table[dev->fmt_idx].skip_frames;
1883 mutex_unlock(&dev->input_lock);
1884
1885 return 0;
1886}
1887
1888static const struct v4l2_subdev_sensor_ops ov8858_sensor_ops = {
1889 .g_skip_frames = ov8858_g_skip_frames,
1890};
1891
1892static const struct v4l2_ctrl_ops ctrl_ops = {
1893 .s_ctrl = ov8858_s_ctrl,
1894 .g_volatile_ctrl = ov8858_g_ctrl,
1895};
1896
1897static const struct v4l2_subdev_video_ops ov8858_video_ops = {
1898 .s_stream = ov8858_s_stream,
1899 .g_frame_interval = ov8858_g_frame_interval,
1900 .s_frame_interval = ov8858_s_frame_interval,
1901};
1902
1903static const struct v4l2_subdev_core_ops ov8858_core_ops = {
1904 .s_power = ov8858_s_power,
1905 .ioctl = ov8858_ioctl,
1906 .init = ov8858_init,
1907};
1908
1909static const struct v4l2_subdev_pad_ops ov8858_pad_ops = {
1910 .enum_mbus_code = ov8858_enum_mbus_code,
1911 .enum_frame_size = ov8858_enum_frame_size,
1912 .get_fmt = ov8858_get_fmt,
1913 .set_fmt = ov8858_set_fmt,
1914};
1915
1916static const struct v4l2_subdev_ops ov8858_ops = {
1917 .core = &ov8858_core_ops,
1918 .video = &ov8858_video_ops,
1919 .pad = &ov8858_pad_ops,
1920 .sensor = &ov8858_sensor_ops,
1921};
1922
1923static const struct media_entity_operations ov_entity_ops = {
1924 .link_setup = NULL,
1925};
1926
1927static int ov8858_remove(struct i2c_client *client)
1928{
1929 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1930 struct ov8858_device *dev = to_ov8858_sensor(sd);
1931 if (dev->platform_data->platform_deinit)
1932 dev->platform_data->platform_deinit();
1933
1934 media_entity_cleanup(&dev->sd.entity);
1935 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1936 dev->platform_data->csi_cfg(sd, 0);
1937 v4l2_device_unregister_subdev(sd);
1938 kfree(dev);
1939
1940 return 0;
1941}
1942
1943static const char * const ctrl_run_mode_menu[] = {
1944 NULL,
1945 "Video",
1946 "Still capture",
1947 "Continuous capture",
1948 "Preview",
1949};
1950
1951static const struct v4l2_ctrl_config ctrl_run_mode = {
1952 .ops = &ctrl_ops,
1953 .id = V4L2_CID_RUN_MODE,
1954 .name = "run mode",
1955 .type = V4L2_CTRL_TYPE_MENU,
1956 .min = 1,
1957 .def = 4,
1958 .max = 4,
1959 .qmenu = ctrl_run_mode_menu,
1960};
1961
1962static const struct v4l2_ctrl_config ctrls[] = {
1963 {
1964 .ops = &ctrl_ops,
1965 .id = V4L2_CID_VFLIP,
1966 .name = "Vertical flip",
1967 .type = V4L2_CTRL_TYPE_BOOLEAN,
1968 .min = false,
1969 .max = true,
1970 .step = 1,
1971 }, {
1972 .ops = &ctrl_ops,
1973 .id = V4L2_CID_HFLIP,
1974 .name = "Horizontal flip",
1975 .type = V4L2_CTRL_TYPE_BOOLEAN,
1976 .min = false,
1977 .max = true,
1978 .step = 1,
1979 }, {
1980 .ops = &ctrl_ops,
1981 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1982 .name = "Absolute exposure",
1983 .type = V4L2_CTRL_TYPE_INTEGER,
1984 .max = 0xffff,
1985 .min = 0x0,
1986 .step = 1,
1987 .def = 0x00,
1988 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
1989 }, {
1990 .ops = &ctrl_ops,
1991 .id = V4L2_CID_FOCUS_ABSOLUTE,
1992 .name = "Focus absolute",
1993 .type = V4L2_CTRL_TYPE_INTEGER,
1994 .step = 1,
1995 .max = OV8858_MAX_FOCUS_POS,
1996 }, {
1997 /* This one is junk: see the spec for proper use of this CID. */
1998 .ops = &ctrl_ops,
1999 .id = V4L2_CID_FOCUS_STATUS,
2000 .name = "Focus status",
2001 .type = V4L2_CTRL_TYPE_INTEGER,
2002 .step = 1,
2003 .max = 100,
2004 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2005 }, {
2006 /* This is crap. For compatibility use only. */
2007 .ops = &ctrl_ops,
2008 .id = V4L2_CID_FOCAL_ABSOLUTE,
2009 .name = "Focal lenght",
2010 .type = V4L2_CTRL_TYPE_INTEGER,
2011 .min = (OV8858_FOCAL_LENGTH_NUM << 16) |
2012 OV8858_FOCAL_LENGTH_DEM,
2013 .max = (OV8858_FOCAL_LENGTH_NUM << 16) |
2014 OV8858_FOCAL_LENGTH_DEM,
2015 .step = 1,
2016 .def = (OV8858_FOCAL_LENGTH_NUM << 16) |
2017 OV8858_FOCAL_LENGTH_DEM,
2018 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2019 }, {
2020 /* This one is crap, too. For compatibility use only. */
2021 .ops = &ctrl_ops,
2022 .id = V4L2_CID_FNUMBER_ABSOLUTE,
2023 .name = "F-number",
2024 .type = V4L2_CTRL_TYPE_INTEGER,
2025 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2026 OV8858_F_NUMBER_DEM,
2027 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2028 OV8858_F_NUMBER_DEM,
2029 .step = 1,
2030 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2031 OV8858_F_NUMBER_DEM,
2032 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2033 }, {
2034 /*
2035 * The most utter crap. _Never_ use this, even for
2036 * compatibility reasons!
2037 */
2038 .ops = &ctrl_ops,
2039 .id = V4L2_CID_FNUMBER_RANGE,
2040 .name = "F-number range",
2041 .type = V4L2_CTRL_TYPE_INTEGER,
2042 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2043 (OV8858_F_NUMBER_DEM << 16) |
2044 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2045 OV8858_F_NUMBER_DEM,
2046 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2047 (OV8858_F_NUMBER_DEM << 16) |
2048 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2049 OV8858_F_NUMBER_DEM,
2050 .step = 1,
2051 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2052 (OV8858_F_NUMBER_DEM << 16) |
2053 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2054 OV8858_F_NUMBER_DEM,
2055 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2056 }, {
2057 .ops = &ctrl_ops,
2058 .id = V4L2_CID_BIN_FACTOR_HORZ,
2059 .name = "Horizontal binning factor",
2060 .type = V4L2_CTRL_TYPE_INTEGER,
2061 .max = OV8858_BIN_FACTOR_MAX,
2062 .step = 1,
2063 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2064 }, {
2065 .ops = &ctrl_ops,
2066 .id = V4L2_CID_BIN_FACTOR_VERT,
2067 .name = "Vertical binning factor",
2068 .type = V4L2_CTRL_TYPE_INTEGER,
2069 .max = OV8858_BIN_FACTOR_MAX,
2070 .step = 1,
2071 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2072 }, {
2073 .ops = &ctrl_ops,
2074 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
2075 .name = "Exposure auto priority",
2076 .type = V4L2_CTRL_TYPE_INTEGER,
2077 .min = V4L2_EXPOSURE_AUTO,
2078 .max = V4L2_EXPOSURE_APERTURE_PRIORITY,
2079 .step = 1,
2080 }
2081};
2082
2083static int ov8858_probe(struct i2c_client *client,
2084 const struct i2c_device_id *id)
2085{
2086 struct ov8858_device *dev;
2087 unsigned int i;
2088 int ret = 0;
2089 struct camera_sensor_platform_data *pdata;
2090
2091 dev_dbg(&client->dev, "%s:\n", __func__);
2092
2093 /* allocate sensor device & init sub device */
2094 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
309167b9 2095 if (!dev)
a49d2536 2096 return -ENOMEM;
a49d2536
AC
2097
2098 mutex_init(&dev->input_lock);
2099
2100 if (id)
2101 dev->i2c_id = id->driver_data;
2102 dev->fmt_idx = 0;
2103 dev->sensor_id = OV_ID_DEFAULT;
2104 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
2105
2106 v4l2_i2c_subdev_init(&(dev->sd), client, &ov8858_ops);
2107
2108 if (ACPI_COMPANION(&client->dev)) {
2109 pdata = gmin_camera_platform_data(&dev->sd,
2110 ATOMISP_INPUT_FORMAT_RAW_10,
2111 atomisp_bayer_order_bggr);
2112 if (!pdata) {
2113 dev_err(&client->dev,
2114 "%s: failed to get acpi platform data\n",
2115 __func__);
2116 goto out_free;
2117 }
2118 ret = ov8858_s_config(&dev->sd, client->irq, pdata);
2119 if (ret) {
2120 dev_err(&client->dev,
2121 "%s: failed to set config\n", __func__);
2122 goto out_free;
2123 }
2124 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
2125 if (ret) {
2126 dev_err(&client->dev,
2127 "%s: failed to register subdev\n", __func__);
2128 goto out_free;
2129 }
2130 }
2131 /*
2132 * sd->name is updated with sensor driver name by the v4l2.
2133 * change it to sensor name in this case.
2134 */
2135 snprintf(dev->sd.name, sizeof(dev->sd.name), "%s%x %d-%04x",
2136 OV_SUBDEV_PREFIX, dev->sensor_id,
2137 i2c_adapter_id(client->adapter), client->addr);
2138
2139 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2140 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
2141 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
2142 dev->sd.entity.ops = &ov_entity_ops;
2143 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2144
2145 ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ctrls) + 1);
2146 if (ret) {
2147 ov8858_remove(client);
2148 return ret;
2149 }
2150
2151 dev->run_mode = v4l2_ctrl_new_custom(&dev->ctrl_handler,
2152 &ctrl_run_mode, NULL);
2153
2154 for (i = 0; i < ARRAY_SIZE(ctrls); i++)
2155 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ctrls[i], NULL);
2156
2157 if (dev->ctrl_handler.error) {
2158 ov8858_remove(client);
2159 return dev->ctrl_handler.error;
2160 }
2161
2162 /* Use same lock for controls as for everything else. */
2163 dev->ctrl_handler.lock = &dev->input_lock;
2164 dev->sd.ctrl_handler = &dev->ctrl_handler;
2165 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2166
2167 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
2168 if (ret) {
2169 ov8858_remove(client);
2170 return ret;
2171 }
2172
2173 return 0;
2174
2175out_free:
2176 v4l2_device_unregister_subdev(&dev->sd);
2177 kfree(dev);
2178 return ret;
2179}
2180
2181static const struct i2c_device_id ov8858_id[] = {
2182 {OV8858_NAME, 0},
2183 {}
2184};
2185
2186MODULE_DEVICE_TABLE(i2c, ov8858_id);
2187
09571dfd 2188static const struct acpi_device_id ov8858_acpi_match[] = {
a49d2536
AC
2189 {"INT3477"},
2190 {},
2191};
2192
2193static struct i2c_driver ov8858_driver = {
2194 .driver = {
a49d2536
AC
2195 .name = OV8858_NAME,
2196 .acpi_match_table = ACPI_PTR(ov8858_acpi_match),
2197 },
2198 .probe = ov8858_probe,
2199 .remove = ov8858_remove,
2200 .id_table = ov8858_id,
2201};
2cb63c4c 2202module_i2c_driver(ov8858_driver);
a49d2536
AC
2203
2204MODULE_DESCRIPTION("A low-level driver for Omnivision OV8858 sensors");
2205MODULE_LICENSE("GPL");