]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/media/atomisp/i2c/ov8858.c
media: staging: atomisp: Remove unused members of camera_sensor_platform_data
[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
a49d2536
AC
715 if (dev->platform_data->v1p2_ctrl) {
716 ret = dev->platform_data->v1p2_ctrl(sd, flag);
717 if (ret) {
718 dev_err(&client->dev,
719 "failed to power %s 1.2v power rail\n",
720 flag ? "up" : "down");
721 return ret;
722 }
723 }
724
725 if (dev->platform_data->v2p8_ctrl) {
726 ret = dev->platform_data->v2p8_ctrl(sd, flag);
727 if (ret) {
728 dev_err(&client->dev,
729 "failed to power %s 2.8v power rail\n",
730 flag ? "up" : "down");
731 return ret;
732 }
733 }
734
735 if (dev->platform_data->v1p8_ctrl) {
736 ret = dev->platform_data->v1p8_ctrl(sd, flag);
737 if (ret) {
738 dev_err(&client->dev,
739 "failed to power %s 1.8v power rail\n",
740 flag ? "up" : "down");
741 if (dev->platform_data->v2p8_ctrl)
742 dev->platform_data->v2p8_ctrl(sd, 0);
743 return ret;
744 }
745 }
746
747 if (flag)
748 msleep(20); /* Wait for power lines to stabilize */
749 return ret;
750}
751
752static int __gpio_ctrl(struct v4l2_subdev *sd, bool flag)
753{
754 struct i2c_client *client;
755 struct ov8858_device *dev;
756
757 if (!sd)
758 return -EINVAL;
759
760 client = v4l2_get_subdevdata(sd);
761 dev = to_ov8858_sensor(sd);
762
763 if (!client || !dev || !dev->platform_data)
764 return -ENODEV;
765
a49d2536
AC
766 if (dev->platform_data->gpio0_ctrl)
767 return dev->platform_data->gpio0_ctrl(sd, flag);
768
769 dev_err(&client->dev, "failed to find platform gpio callback\n");
770
771 return -EINVAL;
772}
773
774static int power_up(struct v4l2_subdev *sd)
775{
776 struct i2c_client *client = v4l2_get_subdevdata(sd);
777 struct ov8858_device *dev = to_ov8858_sensor(sd);
778 int ret;
779 dev_dbg(&client->dev, "%s\n", __func__);
780
781 /* Enable power */
782 ret = __power_ctrl(sd, 1);
783 if (ret) {
784 dev_err(&client->dev, "power rail on failed %d.\n", ret);
785 goto fail_power;
786 }
787
788 /* Enable clock */
789 ret = dev->platform_data->flisclk_ctrl(sd, 1);
790 if (ret) {
791 dev_err(&client->dev, "flisclk on failed %d\n", ret);
792 goto fail_clk;
793 }
794
795 /* Release reset */
796 ret = __gpio_ctrl(sd, 1);
797 if (ret) {
798 dev_err(&client->dev, "gpio on failed %d\n", ret);
799 goto fail_gpio;
800 }
801
802 /* Minumum delay is 8192 clock cycles before first i2c transaction,
803 * which is 1.37 ms at the lowest allowed clock rate 6 MHz */
804 usleep_range(2000, 2500);
805 return 0;
806
807fail_gpio:
808 dev->platform_data->flisclk_ctrl(sd, 0);
809fail_clk:
810 __power_ctrl(sd, 0);
811fail_power:
812 dev_err(&client->dev, "Sensor power-up failed\n");
813
814 return ret;
815}
816
817static int power_down(struct v4l2_subdev *sd)
818{
819 struct ov8858_device *dev = to_ov8858_sensor(sd);
820 struct i2c_client *client = v4l2_get_subdevdata(sd);
821 int ret;
822 dev_dbg(&client->dev, "%s\n", __func__);
823
824 ret = dev->platform_data->flisclk_ctrl(sd, 0);
825 if (ret)
826 dev_err(&client->dev, "flisclk off failed\n");
827
828 ret = __gpio_ctrl(sd, 0);
829 if (ret)
830 dev_err(&client->dev, "gpio off failed\n");
831
832 ret = __power_ctrl(sd, 0);
833 if (ret)
834 dev_err(&client->dev, "power rail off failed.\n");
835
836 return ret;
837}
838
839static int __ov8858_s_power(struct v4l2_subdev *sd, int on)
840{
841 struct ov8858_device *dev = to_ov8858_sensor(sd);
842 int ret, r = 0;
843
844 if (on == 0) {
845 ov8858_uninit(sd);
846 if (dev->vcm_driver && dev->vcm_driver->power_down)
847 r = dev->vcm_driver->power_down(sd);
848 ret = power_down(sd);
849 if (r != 0 && ret == 0)
850 ret = r;
851 } else {
852 ret = power_up(sd);
853 if (ret)
854 power_down(sd);
855 if (dev->vcm_driver && dev->vcm_driver->power_up) {
856 ret = dev->vcm_driver->power_up(sd);
857 if (ret) {
858 power_down(sd);
859 return ret;
860 }
861 }
862 return __ov8858_init(sd);
863 }
864
865 return ret;
866}
867
868static int ov8858_s_power(struct v4l2_subdev *sd, int on)
869{
870 int ret;
871 struct ov8858_device *dev = to_ov8858_sensor(sd);
872
873 mutex_lock(&dev->input_lock);
874 ret = __ov8858_s_power(sd, on);
875 mutex_unlock(&dev->input_lock);
876
877 /*
878 * FIXME: Compatibility with old behaviour: return to preview
879 * when the device is power cycled.
880 */
881 if (!ret && on)
882 v4l2_ctrl_s_ctrl(dev->run_mode, ATOMISP_RUN_MODE_PREVIEW);
883
884 return ret;
885}
886
887/*
888 * Return value of the specified register, first try getting it from
889 * the register list and if not found, get from the sensor via i2c.
890 */
891static int ov8858_get_register(struct v4l2_subdev *sd, int reg, int type,
892 const struct ov8858_reg *reglist)
893{
894 struct i2c_client *client = v4l2_get_subdevdata(sd);
895 const struct ov8858_reg *next;
896 u16 val;
897
898 /* Try if the values are in the register list */
899 for (next = reglist; next->type != OV8858_TOK_TERM; next++) {
900 if (next->sreg == reg) {
901 if (type == OV8858_8BIT)
902 return next->val;
903
904 if (type == OV8858_16BIT &&
905 next[1].type != OV8858_TOK_TERM)
906 return next[0].val << 8 | next[1].val;
907 }
908 }
909
910 /* If not, read from sensor */
911 if (ov8858_read_reg(client, type, reg, &val)) {
912 dev_err(&client->dev, "failed to read register 0x%08x\n", reg);
913 return -EIO;
914 }
915
916 return val;
917}
918
919static inline int ov8858_get_register_16bit(struct v4l2_subdev *sd, int reg,
920 const struct ov8858_reg *reglist)
921{
922 return ov8858_get_register(sd, reg, OV8858_16BIT, reglist);
923}
924
925static inline int ov8858_get_register_8bit(struct v4l2_subdev *sd, int reg,
926 const struct ov8858_reg *reglist)
927{
928 return ov8858_get_register(sd, reg, OV8858_8BIT, reglist);
929}
930
931static int __ov8858_get_pll1_values(struct v4l2_subdev *sd,
932 int *value,
933 const struct ov8858_reg *reglist)
934{
935 struct i2c_client *client = v4l2_get_subdevdata(sd);
936 unsigned int prediv_idx;
937 unsigned int multiplier;
938 unsigned int sys_prediv;
939 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
940 int ret;
941
942 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV0, reglist);
943 if (ret < 0)
944 return ret;
945
946 if (ret & OV8858_PLL1_PREDIV0_MASK)
947 *value /= 2;
948
949 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV, reglist);
950
951 if (ret < 0)
952 return ret;
953
954 prediv_idx = ret & OV8858_PLL1_PREDIV_MASK;
955 *value = *value * 2 / prediv_coef[prediv_idx];
956
957 ret = ov8858_get_register_16bit(sd, OV8858_PLL1_MULTIPLIER, reglist);
958 if (ret < 0)
959 return ret;
960
961 multiplier = ret;
962 *value *= multiplier & OV8858_PLL1_MULTIPLIER_MASK;
963 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_PRE_DIV, reglist);
964
965 if (ret < 0)
966 return ret;
967
968 sys_prediv = ret & OV8858_PLL1_SYS_PRE_DIV_MASK;
969 *value /= (sys_prediv + 3);
970 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_DIVIDER, reglist);
971
972 if (ret < 0)
973 return ret;
974
975 if (ret & OV8858_PLL1_SYS_DIVIDER_MASK)
976 *value /= 2;
977
978 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
979
980 return 0;
981}
982
983static int __ov8858_get_pll2a_values(struct v4l2_subdev *sd, int *value,
984 const struct ov8858_reg *reglist)
985{
986 struct i2c_client *client = v4l2_get_subdevdata(sd);
987 unsigned int prediv_idx;
988 unsigned int multiplier;
989 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
990 int ret;
991
992 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV0, reglist);
993 if (ret < 0)
994 return ret;
995
996 if (ret & OV8858_PLL2_PREDIV0_MASK)
997 *value /= 2;
998
999 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV, reglist);
1000 if (ret < 0)
1001 return ret;
1002
1003 prediv_idx = (ret & OV8858_PLL2_PREDIV_MASK);
1004 *value = *value * 2 / prediv_coef[prediv_idx];
1005
1006 ret = ov8858_get_register_16bit(sd, OV8858_PLL2_MULTIPLIER, reglist);
1007 if (ret < 0)
1008 return ret;
1009
1010 multiplier = ret;
1011 *value *= multiplier & OV8858_PLL2_MULTIPLIER_MASK;
1012 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1013
1014 return 0;
1015}
1016static int __ov8858_get_pll2b_values(struct v4l2_subdev *sd, int *value,
1017 const struct ov8858_reg *reglist)
1018{
1019 struct i2c_client *client = v4l2_get_subdevdata(sd);
1020 unsigned int dac_divider;
1021 int ret;
1022
1023 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_DAC_DIVIDER, reglist);
1024 if (ret < 0)
1025 return ret;
1026
1027 dac_divider = (ret & OV8858_PLL2_DAC_DIVIDER_MASK) + 1;
1028 *value /= dac_divider;
1029
1030 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1031
1032 return 0;
1033}
1034static int __ov8858_get_pll2c_values(struct v4l2_subdev *sd, int *value,
1035 const struct ov8858_reg *reglist)
1036{
1037 struct i2c_client *client = v4l2_get_subdevdata(sd);
1038 unsigned int sys_pre_div;
1039 unsigned int sys_divider_idx;
1040 unsigned int sys_divider_coef[] = {2, 3, 4, 5, 6, 7, 8, 10};
1041 int ret;
1042
1043 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_PRE_DIV, reglist);
1044 if (ret < 0)
1045 return ret;
1046
1047 sys_pre_div = (ret & OV8858_PLL2_SYS_PRE_DIV_MASK) + 1;
1048 *value /= sys_pre_div;
1049
1050 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_DIVIDER, reglist);
1051 if (ret < 0)
1052 return ret;
1053
1054 sys_divider_idx = ret & OV8858_PLL2_SYS_DIVIDER_MASK;
1055 *value *= 2 / sys_divider_coef[sys_divider_idx];
1056
1057 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1058
1059 return 0;
1060}
1061
1062static int ov8858_get_intg_factor(struct v4l2_subdev *sd,
1063 struct camera_mipi_info *info,
1064 const struct ov8858_reg *reglist)
1065{
1066 const unsigned int ext_clk = 19200000; /* Hz */
1067 struct atomisp_sensor_mode_data *m = &info->data;
1068 struct ov8858_device *dev = to_ov8858_sensor(sd);
1069 struct i2c_client *client = v4l2_get_subdevdata(sd);
1070 struct device *d = &client->dev;
1071 const struct ov8858_resolution *res =
1072 &dev->curr_res_table[dev->fmt_idx];
1073 unsigned int pll_sclksel1;
1074 unsigned int pll_sclksel2;
1075 unsigned int sys_pre_div;
1076 unsigned int sclk_pdiv;
1077 unsigned int sclk = ext_clk;
1078 u16 hts;
1079 int ret;
1080
1081 memset(&info->data, 0, sizeof(info->data));
1082
1083 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL1, reglist);
1084 if (ret < 0)
1085 return ret;
1086
1087 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL1: 0x%02x\n", __func__, ret);
1088 pll_sclksel1 = ret & OV8858_PLL_SCLKSEL1_MASK;
1089
1090 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL2, reglist);
1091 if (ret < 0)
1092 return ret;
1093
1094 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL2: 0x%02x\n", __func__, ret);
1095 pll_sclksel2 = ret & OV8858_PLL_SCLKSEL2_MASK;
1096
1097 if (pll_sclksel2) {
1098 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1099 if (ret < 0)
1100 return ret;
1101 ret = __ov8858_get_pll2b_values(sd, &sclk, reglist);
1102 if (ret < 0)
1103 return ret;
1104 } else if (pll_sclksel1) {
1105 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1106 if (ret < 0)
1107 return ret;
1108 ret = __ov8858_get_pll2c_values(sd, &sclk, reglist);
1109 if (ret < 0)
1110 return ret;
1111 } else {
1112 ret = __ov8858_get_pll1_values(sd, &sclk, reglist);
1113 if (ret < 0)
1114 return ret;
1115 }
1116
1117 ret = ov8858_get_register_8bit(sd, OV8858_SRB_HOST_INPUT_DIS, reglist);
1118 if (ret < 0)
1119 return ret;
1120
1121 dev_dbg(d, "%s: OV8858_SRB_HOST_INPUT_DIS: 0x%02x\n", __func__, ret);
1122
1123 sys_pre_div = ret & OV8858_SYS_PRE_DIV_MASK;
1124 sys_pre_div >>= OV8858_SYS_PRE_DIV_OFFSET;
1125
1126 if (sys_pre_div == 1)
1127 sclk /= 2;
1128 else if (sys_pre_div == 2)
1129 sclk /= 4;
1130
1131 sclk_pdiv = ret & OV8858_SCLK_PDIV_MASK;
1132 sclk_pdiv >>= OV8858_SCLK_PDIV_OFFSET;
1133
1134 if (sclk_pdiv > 1)
1135 sclk /= sclk_pdiv;
1136
1137 dev_dbg(d, "%s: sclk: %d\n", __func__, sclk);
1138
1139 dev->vt_pix_clk_freq_mhz = sclk;
1140 m->vt_pix_clk_freq_mhz = sclk;
1141
1142 /* HTS and VTS */
1143 m->frame_length_lines =
1144 res->fps_options[dev->fps_index].lines_per_frame;
1145 m->line_length_pck = res->fps_options[dev->fps_index].pixels_per_line;
1146
1147 m->coarse_integration_time_min = 0;
1148 m->coarse_integration_time_max_margin = OV8858_INTEGRATION_TIME_MARGIN;
1149 ret = ov8858_read_reg(client, OV8858_16BIT, OV8858_TIMING_HTS, &hts);
1150 if (ret < 0)
1151 return ret;
1152 m->hts = hts;
1153 dev_dbg(&client->dev, "%s: get HTS %d\n", __func__, hts);
1154
1155 /* OV Sensor do not use fine integration time. */
1156 m->fine_integration_time_min = 0;
1157 m->fine_integration_time_max_margin = 0;
1158
1159 /*
1160 * read_mode indicate whether binning is used for calculating
1161 * the correct exposure value from the user side. So adapt the
1162 * read mode values accordingly.
1163 */
1164 m->read_mode = res->bin_factor_x ?
1165 OV8858_READ_MODE_BINNING_ON : OV8858_READ_MODE_BINNING_OFF;
1166
1167 ret = ov8858_get_register_8bit(sd, OV8858_H_INC_ODD, res->regs);
1168 if (ret < 0)
1169 return ret;
1170 m->binning_factor_x = (ret + 1) / 2;
1171
1172 ret = ov8858_get_register_8bit(sd, OV8858_V_INC_ODD, res->regs);
1173 if (ret < 0)
1174 return ret;
1175 m->binning_factor_y = (ret + 1) / 2;
1176
1177 /* Get the cropping and output resolution to ISP for this mode. */
1178 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_START_H,
1179 res->regs);
1180 if (ret < 0)
1181 return ret;
1182
1183 m->crop_horizontal_start = ret;
1184
1185 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_START_H, res->regs);
1186 if (ret < 0)
1187 return ret;
1188
1189 m->crop_vertical_start = ret;
1190
1191 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_END_H, res->regs);
1192 if (ret < 0)
1193 return ret;
1194
1195 m->crop_horizontal_end = ret;
1196
1197 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_END_H, res->regs);
1198 if (ret < 0)
1199 return ret;
1200
1201 m->crop_vertical_end = ret;
1202
1203 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_OUTPUT_SIZE_H,
1204 res->regs);
1205 if (ret < 0)
1206 return ret;
1207
1208 m->output_width = ret;
1209
1210 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_OUTPUT_SIZE_H,
1211 res->regs);
1212 if (ret < 0)
1213 return ret;
1214
1215 m->output_height = ret;
1216
1217 return 0;
1218}
1219
1220/*
1221 * distance - calculate the distance
1222 * @res: resolution
1223 * @w: width
1224 * @h: height
1225 *
1226 * Get the gap between res_w/res_h and w/h.
1227 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1228 * res->width/height smaller than w/h wouldn't be considered.
1229 * The gap of ratio larger than 1/8 wouldn't be considered.
1230 * Returns the value of gap or -1 if fail.
1231 */
1232#define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1233static int distance(struct ov8858_resolution const *res, const u32 w,
1234 const u32 h)
1235{
1236 int ratio;
1237 int distance;
1238
1239 if (w == 0 || h == 0 ||
1240 res->width < w || res->height < h)
1241 return -1;
1242
6c492211 1243 ratio = res->width << 13;
a49d2536
AC
1244 ratio /= w;
1245 ratio *= h;
1246 ratio /= res->height;
1247
1248 distance = abs(ratio - 8192);
1249
1250 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1251 return -1;
1252 return distance;
1253}
1254
1255/*
1256 * Returns the nearest higher resolution index.
1257 * @w: width
1258 * @h: height
1259 * matching is done based on enveloping resolution and
1260 * aspect ratio. If the aspect ratio cannot be matched
1261 * to any index, -1 is returned.
1262 */
1263static int nearest_resolution_index(struct v4l2_subdev *sd, int w, int h)
1264{
1265 int i;
1266 int idx = -1;
1267 int dist;
1268 int fps_diff;
1269 int min_fps_diff = INT_MAX;
1270 int min_dist = INT_MAX;
1271 int min_res_w = INT_MAX;
1272 const struct ov8858_resolution *tmp_res = NULL;
1273 struct i2c_client *client = v4l2_get_subdevdata(sd);
1274 struct ov8858_device *dev = to_ov8858_sensor(sd);
1275 dev_dbg(&client->dev, "%s: w=%d, h=%d\n", __func__, w, h);
1276
1277 for (i = 0; i < dev->entries_curr_table; i++) {
1278 tmp_res = &dev->curr_res_table[i];
1279 dist = distance(tmp_res, w, h);
1280 dev_dbg(&client->dev,
1281 "%s[%d]: %dx%d distance=%d\n", tmp_res->desc,
1282 i, tmp_res->width, tmp_res->height, dist);
1283 if (dist == -1)
1284 continue;
1285 if (dist < min_dist) {
1286 min_dist = dist;
1287 min_res_w = tmp_res->width;
1288 min_fps_diff = __ov8858_min_fps_diff(dev->fps,
1289 tmp_res->fps_options);
1290 idx = i;
1291 }
1292 if (dist == min_dist) {
1293 fps_diff = __ov8858_min_fps_diff(dev->fps,
1294 tmp_res->fps_options);
1295 if (fps_diff < min_fps_diff) {
1296 min_fps_diff = fps_diff;
1297 idx = i;
1298 }
1299 if (tmp_res->width < min_res_w) {
1300 min_res_w = tmp_res->width;
1301 idx = i;
1302 }
1303 }
1304 }
1305
1306 return idx;
1307}
1308
1309static int ov8858_set_fmt(struct v4l2_subdev *sd,
1310 struct v4l2_subdev_pad_config *cfg,
1311 struct v4l2_subdev_format *format)
1312{
1313 struct v4l2_mbus_framefmt *fmt = &format->format;
1314 struct ov8858_device *dev = to_ov8858_sensor(sd);
1315 struct camera_mipi_info *ov8858_info = NULL;
1316 struct i2c_client *client = v4l2_get_subdevdata(sd);
1317 const struct ov8858_resolution *res;
1318 int ret;
1319 int idx;
1320 if (format->pad)
1321 return -EINVAL;
1322 if (!fmt)
1323 return -EINVAL;
1324
1325 ov8858_info = v4l2_get_subdev_hostdata(sd);
1326 if (ov8858_info == NULL)
1327 return -EINVAL;
1328
1329 mutex_lock(&dev->input_lock);
1330
1331 if ((fmt->width > OV8858_RES_WIDTH_MAX) ||
1332 (fmt->height > OV8858_RES_HEIGHT_MAX)) {
1333 fmt->width = OV8858_RES_WIDTH_MAX;
1334 fmt->height = OV8858_RES_HEIGHT_MAX;
1335 } else {
1336 idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1337
1338 /*
1339 * nearest_resolution_index() doesn't return smaller
1340 * resolutions. If it fails, it means the requested resolution
1341 * is higher than we can support. Fallback to highest possible
1342 * resolution in this case.
1343 */
1344 if (idx == -1)
1345 idx = dev->entries_curr_table - 1;
1346
1347 fmt->width = dev->curr_res_table[idx].width;
1348 fmt->height = dev->curr_res_table[idx].height;
1349 }
1350
1351 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1352 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1353 cfg->try_fmt = *fmt;
1354 mutex_unlock(&dev->input_lock);
1355 return 0;
1356 }
1357
1358 dev->fmt_idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1359 if (dev->fmt_idx == -1) {
1360 ret = -EINVAL;
1361 goto out;
1362 }
1363 res = &dev->curr_res_table[dev->fmt_idx];
1364 dev_dbg(&client->dev, "%s: selected width = %d, height = %d\n",
1365 __func__, res->width, res->height);
1366
1367 /* Adjust the FPS selection based on the resolution selected */
1368 dev->fps_index = __ov8858_nearest_fps_index(dev->fps, res->fps_options);
1369 dev->fps = res->fps_options[dev->fps_index].fps;
1370 dev->regs = res->fps_options[dev->fps_index].regs;
1371 if (!dev->regs)
1372 dev->regs = res->regs;
1373
1374 ret = ov8858_write_reg_array(client, dev->regs);
1375 if (ret)
1376 goto out;
1377
1378 dev->pixels_per_line = res->fps_options[dev->fps_index].pixels_per_line;
1379 dev->lines_per_frame = res->fps_options[dev->fps_index].lines_per_frame;
1380
1381 /* ov8858 only support RGB RAW10 output */
1382 ov8858_info->metadata_width = res->width * 10 / 8;
1383 ov8858_info->metadata_height = 2;
1384 ov8858_info->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1385
1386 /* Set the initial exposure */
1387 ret = __ov8858_set_exposure(sd, dev->exposure, dev->gain,
1388 dev->digital_gain, &dev->pixels_per_line,
1389 &dev->lines_per_frame);
1390 if (ret)
1391 goto out;
1392
1393 ret = ov8858_get_intg_factor(sd, ov8858_info, dev->regs);
1394
1395out:
1396 mutex_unlock(&dev->input_lock);
1397
1398 return ret;
1399}
1400
1401static int ov8858_get_fmt(struct v4l2_subdev *sd,
1402 struct v4l2_subdev_pad_config *cfg,
1403 struct v4l2_subdev_format *format)
1404{
1405 struct v4l2_mbus_framefmt *fmt = &format->format;
1406 struct ov8858_device *dev = to_ov8858_sensor(sd);
1407
1408 if (format->pad)
1409 return -EINVAL;
1410 if (!fmt)
1411 return -EINVAL;
1412
1413 mutex_lock(&dev->input_lock);
1414 fmt->width = dev->curr_res_table[dev->fmt_idx].width;
1415 fmt->height = dev->curr_res_table[dev->fmt_idx].height;
1416 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1417 mutex_unlock(&dev->input_lock);
1418
1419 return 0;
1420}
1421
1422static int ov8858_detect(struct i2c_client *client, u16 *id)
1423{
1424 struct i2c_adapter *adapter = client->adapter;
1425 u16 id_hi = 0;
1426 u16 id_low = 0;
1427 int ret;
1428
1429 /* i2c check */
1430 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1431 return -ENODEV;
1432
1433 dev_dbg(&client->dev, "%s: I2C functionality ok\n", __func__);
1434 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_HIGH, &id_hi);
1435 if (ret)
1436 return ret;
1437 dev_dbg(&client->dev, "%s: id_high = 0x%04x\n", __func__, id_hi);
1438 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_LOW, &id_low);
1439 if (ret)
1440 return ret;
1441 dev_dbg(&client->dev, "%s: id_low = 0x%04x\n", __func__, id_low);
1442 *id = (id_hi << 8) | id_low;
1443
1444 dev_dbg(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1445
1446 dev_info(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1447 if (*id != OV8858_CHIP_ID)
1448 return -ENODEV;
1449
1450 /* Stream off now. */
1451 return ov8858_write_reg(client, OV8858_8BIT, OV8858_STREAM_MODE, 0);
1452}
1453
1454static void __ov8858_print_timing(struct v4l2_subdev *sd)
1455{
1456 struct ov8858_device *dev = to_ov8858_sensor(sd);
1457 struct i2c_client *client = v4l2_get_subdevdata(sd);
1458 u16 width = dev->curr_res_table[dev->fmt_idx].width;
1459 u16 height = dev->curr_res_table[dev->fmt_idx].height;
1460
1461 dev_dbg(&client->dev, "Dump ov8858 timing in stream on:\n");
1462 dev_dbg(&client->dev, "width: %d:\n", width);
1463 dev_dbg(&client->dev, "height: %d:\n", height);
1464 dev_dbg(&client->dev, "pixels_per_line: %d:\n", dev->pixels_per_line);
1465 dev_dbg(&client->dev, "line per frame: %d:\n", dev->lines_per_frame);
1466 dev_dbg(&client->dev, "pix freq: %d:\n", dev->vt_pix_clk_freq_mhz);
1467 /* updated formula: pixels_per_line = 2 * HTS */
1468 /* updated formula: fps = SCLK / (VTS * HTS) */
1469 dev_dbg(&client->dev, "init fps: %d:\n", dev->vt_pix_clk_freq_mhz /
1470 (dev->pixels_per_line / 2) / dev->lines_per_frame);
1471 dev_dbg(&client->dev, "HBlank: %d nS:\n",
1472 1000 * (dev->pixels_per_line - width) /
1473 (dev->vt_pix_clk_freq_mhz / 1000000));
1474 dev_dbg(&client->dev, "VBlank: %d uS:\n",
1475 (dev->lines_per_frame - height) * dev->pixels_per_line /
1476 (dev->vt_pix_clk_freq_mhz / 1000000));
1477}
1478
1479/*
1480 * ov8858 stream on/off
1481 */
1482static int ov8858_s_stream(struct v4l2_subdev *sd, int enable)
1483{
1484 struct ov8858_device *dev = to_ov8858_sensor(sd);
1485 struct i2c_client *client = v4l2_get_subdevdata(sd);
1486 int ret;
1487 u16 val;
1488 dev_dbg(&client->dev, "%s: enable = %d\n", __func__, enable);
1489
1490 /* Set orientation */
1491 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT2, &val);
1492 if (ret)
1493 return ret;
1494
1495 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT2,
1496 dev->hflip ? val | OV8858_FLIP_ENABLE :
1497 val & ~OV8858_FLIP_ENABLE);
1498 if (ret)
1499 return ret;
1500
1501 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT1, &val);
1502 if (ret)
1503 return ret;
1504
1505 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT1,
1506 dev->vflip ? val | OV8858_FLIP_ENABLE :
1507 val & ~OV8858_FLIP_ENABLE);
1508 if (ret)
1509 return ret;
1510
1511 mutex_lock(&dev->input_lock);
1512 if (enable) {
1513 __ov8858_print_timing(sd);
1514 ret = ov8858_write_reg_array(client, ov8858_streaming);
1515 if (ret != 0) {
1516 dev_err(&client->dev, "write_reg_array err\n");
1517 goto out;
1518 }
1519 dev->streaming = 1;
1520 } else {
1521 ret = ov8858_write_reg_array(client, ov8858_soft_standby);
1522 if (ret != 0) {
1523 dev_err(&client->dev, "write_reg_array err\n");
1524 goto out;
1525 }
1526 dev->streaming = 0;
1527 dev->fps_index = 0;
1528 dev->fps = 0;
1529 }
1530out:
1531 mutex_unlock(&dev->input_lock);
1532 return ret;
1533}
1534
1535static int __update_ov8858_device_settings(struct ov8858_device *dev,
1536 u16 sensor_id)
1537{
1538 if (sensor_id == OV8858_CHIP_ID)
1539#ifdef CONFIG_PLATFORM_BTNS
1540 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
1541#else
1542 dev->vcm_driver = &ov8858_vcms[OV8858_SUNNY];
1543#endif
1544 else
1545 return -ENODEV;
1546
1547 if (dev->vcm_driver && dev->vcm_driver->init)
1548 return dev->vcm_driver->init(&dev->sd);
1549
1550 return 0;
1551}
1552
1553static int ov8858_s_config(struct v4l2_subdev *sd,
1554 int irq, void *pdata)
1555{
1556 struct ov8858_device *dev = to_ov8858_sensor(sd);
1557 struct i2c_client *client = v4l2_get_subdevdata(sd);
1558 u16 sensor_id;
1559 int ret;
1560
1561 if (pdata == NULL)
1562 return -ENODEV;
1563
1564 dev->platform_data = pdata;
1565
1566 mutex_lock(&dev->input_lock);
1567
a49d2536
AC
1568 ret = __ov8858_s_power(sd, 1);
1569 if (ret) {
1570 dev_err(&client->dev, "power-up error %d!\n", ret);
1571 mutex_unlock(&dev->input_lock);
1572 return ret;
1573 }
1574
1575 ret = dev->platform_data->csi_cfg(sd, 1);
1576 if (ret)
1577 goto fail_csi_cfg;
1578
1579 /* config & detect sensor */
1580 ret = ov8858_detect(client, &sensor_id);
1581 if (ret) {
1582 dev_err(&client->dev, "detect error %d!\n", ret);
1583 goto fail_detect;
1584 }
1585
1586 dev->sensor_id = sensor_id;
1587
1588 /* power off sensor */
1589 ret = __ov8858_s_power(sd, 0);
1590 if (ret) {
1591 dev->platform_data->csi_cfg(sd, 0);
1592 dev_err(&client->dev, "__ov8858_s_power-down error %d!\n", ret);
1593 goto fail_update;
1594 }
1595
1596 /* Resolution settings depend on sensor type and platform */
1597 ret = __update_ov8858_device_settings(dev, dev->sensor_id);
1598 if (ret) {
1599 dev->platform_data->csi_cfg(sd, 0);
1600 dev_err(&client->dev, "__update_ov8858_device_settings error %d!\n", ret);
1601 goto fail_update;
1602 }
1603
1604 mutex_unlock(&dev->input_lock);
1605 return ret;
1606
1607fail_detect:
1608 dev->platform_data->csi_cfg(sd, 0);
1609fail_csi_cfg:
1610 __ov8858_s_power(sd, 0);
1611fail_update:
a49d2536
AC
1612 mutex_unlock(&dev->input_lock);
1613 dev_err(&client->dev, "sensor power-gating failed\n");
1614 return ret;
1615}
1616
1617static int
1618ov8858_enum_mbus_code(struct v4l2_subdev *sd,
1619 struct v4l2_subdev_pad_config *cfg,
1620 struct v4l2_subdev_mbus_code_enum *code)
1621{
1622 if (code->index)
1623 return -EINVAL;
1624 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1625
1626 return 0;
1627}
1628
1629static int
1630ov8858_enum_frame_size(struct v4l2_subdev *sd,
1631 struct v4l2_subdev_pad_config *cfg,
1632 struct v4l2_subdev_frame_size_enum *fse)
1633{
1634 int index = fse->index;
1635 struct ov8858_device *dev = to_ov8858_sensor(sd);
1636
1637 mutex_lock(&dev->input_lock);
1638 if (index >= dev->entries_curr_table) {
1639 mutex_unlock(&dev->input_lock);
1640 return -EINVAL;
1641 }
1642
1643 fse->min_width = dev->curr_res_table[index].width;
1644 fse->min_height = dev->curr_res_table[index].height;
1645 fse->max_width = dev->curr_res_table[index].width;
1646 fse->max_height = dev->curr_res_table[index].height;
1647 mutex_unlock(&dev->input_lock);
1648
1649 return 0;
1650}
1651
1652static int ov8858_s_ctrl(struct v4l2_ctrl *ctrl)
1653{
1654 struct ov8858_device *dev = container_of(
1655 ctrl->handler, struct ov8858_device, ctrl_handler);
1656 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1657
1658 /* input_lock is taken by the control framework, so it
1659 * doesn't need to be taken here.
1660 */
1661
1662 switch (ctrl->id) {
1663 case V4L2_CID_RUN_MODE:
1664 switch (ctrl->val) {
1665 case ATOMISP_RUN_MODE_VIDEO:
1666 dev->curr_res_table = ov8858_res_video;
1667 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_video);
1668 break;
1669 case ATOMISP_RUN_MODE_STILL_CAPTURE:
1670 dev->curr_res_table = ov8858_res_still;
1671 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_still);
1672 break;
1673 default:
1674 dev->curr_res_table = ov8858_res_preview;
1675 dev->entries_curr_table =
1676 ARRAY_SIZE(ov8858_res_preview);
1677 }
1678
1679 dev->fmt_idx = 0;
1680 dev->fps_index = 0;
1681
1682 return 0;
1683 case V4L2_CID_FOCUS_ABSOLUTE:
1684 if (dev->vcm_driver && dev->vcm_driver->t_focus_abs)
1685 return dev->vcm_driver->t_focus_abs(&dev->sd,
1686 ctrl->val);
1687 return 0;
1688 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1689 if (ctrl->val == V4L2_EXPOSURE_AUTO)
1690 dev->limit_exposure_flag = false;
1691 else if (ctrl->val == V4L2_EXPOSURE_APERTURE_PRIORITY)
1692 dev->limit_exposure_flag = true;
1693 return 0;
1694 case V4L2_CID_HFLIP:
1695 dev->hflip = ctrl->val;
1696 return 0;
1697 case V4L2_CID_VFLIP:
1698 dev->vflip = ctrl->val;
1699 return 0;
1700 default:
1701 dev_err(&client->dev, "%s: Error: Invalid ctrl: 0x%X\n",
1702 __func__, ctrl->id);
1703 return -EINVAL;
1704 }
1705}
1706
1707static int ov8858_g_ctrl(struct v4l2_ctrl *ctrl)
1708{
1709 struct ov8858_device *dev = container_of(
1710 ctrl->handler, struct ov8858_device, ctrl_handler);
1711 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1712 int r_odd, r_even;
1713 int i = dev->fmt_idx;
1714
1715 switch (ctrl->id) {
1716 case V4L2_CID_FOCUS_STATUS:
1717 if (dev->vcm_driver && dev->vcm_driver->q_focus_status)
1718 return dev->vcm_driver->q_focus_status(&dev->sd,
1719 &(ctrl->val));
1720 return 0;
1721 case V4L2_CID_BIN_FACTOR_HORZ:
1722 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_ODD,
1723 dev->curr_res_table[i].regs);
1724 if (r_odd < 0)
1725 return r_odd;
1726 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_EVEN,
1727 dev->curr_res_table[i].regs);
1728 if (r_even < 0)
1729 return r_even;
1730 ctrl->val = fls(r_odd + (r_even)) - 2;
1731 return 0;
1732
1733 case V4L2_CID_BIN_FACTOR_VERT:
1734 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_ODD,
1735 dev->curr_res_table[i].regs);
1736 if (r_odd < 0)
1737 return r_odd;
1738 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_EVEN,
1739 dev->curr_res_table[i].regs);
1740 if (r_even < 0)
1741 return r_even;
1742 ctrl->val = fls(r_odd + (r_even)) - 2;
1743 return 0;
1744 case V4L2_CID_HFLIP:
1745 ctrl->val = dev->hflip;
1746 break;
1747 case V4L2_CID_VFLIP:
1748 ctrl->val = dev->vflip;
1749 break;
1750 case V4L2_CID_EXPOSURE_ABSOLUTE:
1751 ctrl->val = dev->exposure;
1752 break;
1753 default:
1754 dev_warn(&client->dev,
1755 "%s: Error: Invalid ctrl: 0x%X\n", __func__, ctrl->id);
1756 return -EINVAL;
1757 }
1758
1759 return 0;
1760}
1761
1762static int
1763ov8858_g_frame_interval(struct v4l2_subdev *sd,
1764 struct v4l2_subdev_frame_interval *interval)
1765{
1766 struct ov8858_device *dev = to_ov8858_sensor(sd);
1767 const struct ov8858_resolution *res =
1768 &dev->curr_res_table[dev->fmt_idx];
1769
1770 mutex_lock(&dev->input_lock);
1771 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1772 interval->interval.numerator = 1;
1773 mutex_unlock(&dev->input_lock);
1774 return 0;
1775}
1776
1777static int __ov8858_s_frame_interval(struct v4l2_subdev *sd,
1778 struct v4l2_subdev_frame_interval *interval)
1779{
1780 struct ov8858_device *dev = to_ov8858_sensor(sd);
1781 struct i2c_client *client = v4l2_get_subdevdata(sd);
1782 const struct ov8858_resolution *res =
1783 &dev->curr_res_table[dev->fmt_idx];
1784 struct camera_mipi_info *info = NULL;
1785 unsigned int fps_index;
1786 int ret = 0;
1787 int fps;
1788
1789 info = v4l2_get_subdev_hostdata(sd);
1790 if (info == NULL)
1791 return -EINVAL;
1792
1793 if (!interval->interval.numerator)
1794 interval->interval.numerator = 1;
1795
1796 fps = interval->interval.denominator / interval->interval.numerator;
1797
1798 /* No need to proceed further if we are not streaming */
1799 if (!dev->streaming) {
1800 /* Save the new FPS and use it while selecting setting */
1801 dev->fps = fps;
1802 return 0;
1803 }
1804
1805 /* Ignore if we are already using the required FPS. */
1806 if (fps == res->fps_options[dev->fps_index].fps)
1807 return 0;
1808
1809 fps_index = __ov8858_nearest_fps_index(fps, res->fps_options);
1810
1811 if (res->fps_options[fps_index].regs &&
1812 res->fps_options[fps_index].regs != dev->regs) {
1813 dev_err(&client->dev,
1814 "Sensor is streaming, can't apply new configuration\n");
1815 return -EBUSY;
1816 }
1817
1818 dev->fps_index = fps_index;
1819 dev->fps = res->fps_options[dev->fps_index].fps;
1820
1821 /* Update the new frametimings based on FPS */
1822 dev->pixels_per_line =
1823 res->fps_options[dev->fps_index].pixels_per_line;
1824 dev->lines_per_frame =
1825 res->fps_options[dev->fps_index].lines_per_frame;
1826
1827 /* update frametiming. Conside the curren exposure/gain as well */
1828 ret = __ov8858_update_frame_timing(sd,
1829 &dev->pixels_per_line, &dev->lines_per_frame);
1830 if (ret)
1831 return ret;
1832
1833 /* Update the new values so that user side knows the current settings */
1834 ret = ov8858_get_intg_factor(sd, info, dev->regs);
1835 if (ret)
1836 return ret;
1837
1838 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1839 interval->interval.numerator = 1;
1840 __ov8858_print_timing(sd);
1841
1842 return ret;
1843}
1844
1845static int ov8858_s_frame_interval(struct v4l2_subdev *sd,
1846 struct v4l2_subdev_frame_interval *interval)
1847{
1848 struct ov8858_device *dev = to_ov8858_sensor(sd);
1849 int ret;
1850
1851 mutex_lock(&dev->input_lock);
1852 ret = __ov8858_s_frame_interval(sd, interval);
1853 mutex_unlock(&dev->input_lock);
1854
1855 return ret;
1856}
1857
1858static int ov8858_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1859{
1860 struct ov8858_device *dev = to_ov8858_sensor(sd);
1861
1862 mutex_lock(&dev->input_lock);
1863 *frames = dev->curr_res_table[dev->fmt_idx].skip_frames;
1864 mutex_unlock(&dev->input_lock);
1865
1866 return 0;
1867}
1868
1869static const struct v4l2_subdev_sensor_ops ov8858_sensor_ops = {
1870 .g_skip_frames = ov8858_g_skip_frames,
1871};
1872
1873static const struct v4l2_ctrl_ops ctrl_ops = {
1874 .s_ctrl = ov8858_s_ctrl,
1875 .g_volatile_ctrl = ov8858_g_ctrl,
1876};
1877
1878static const struct v4l2_subdev_video_ops ov8858_video_ops = {
1879 .s_stream = ov8858_s_stream,
1880 .g_frame_interval = ov8858_g_frame_interval,
1881 .s_frame_interval = ov8858_s_frame_interval,
1882};
1883
1884static const struct v4l2_subdev_core_ops ov8858_core_ops = {
1885 .s_power = ov8858_s_power,
1886 .ioctl = ov8858_ioctl,
1887 .init = ov8858_init,
1888};
1889
1890static const struct v4l2_subdev_pad_ops ov8858_pad_ops = {
1891 .enum_mbus_code = ov8858_enum_mbus_code,
1892 .enum_frame_size = ov8858_enum_frame_size,
1893 .get_fmt = ov8858_get_fmt,
1894 .set_fmt = ov8858_set_fmt,
1895};
1896
1897static const struct v4l2_subdev_ops ov8858_ops = {
1898 .core = &ov8858_core_ops,
1899 .video = &ov8858_video_ops,
1900 .pad = &ov8858_pad_ops,
1901 .sensor = &ov8858_sensor_ops,
1902};
1903
1904static const struct media_entity_operations ov_entity_ops = {
1905 .link_setup = NULL,
1906};
1907
1908static int ov8858_remove(struct i2c_client *client)
1909{
1910 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1911 struct ov8858_device *dev = to_ov8858_sensor(sd);
a49d2536
AC
1912
1913 media_entity_cleanup(&dev->sd.entity);
1914 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1915 dev->platform_data->csi_cfg(sd, 0);
1916 v4l2_device_unregister_subdev(sd);
1917 kfree(dev);
1918
1919 return 0;
1920}
1921
1922static const char * const ctrl_run_mode_menu[] = {
1923 NULL,
1924 "Video",
1925 "Still capture",
1926 "Continuous capture",
1927 "Preview",
1928};
1929
1930static const struct v4l2_ctrl_config ctrl_run_mode = {
1931 .ops = &ctrl_ops,
1932 .id = V4L2_CID_RUN_MODE,
1933 .name = "run mode",
1934 .type = V4L2_CTRL_TYPE_MENU,
1935 .min = 1,
1936 .def = 4,
1937 .max = 4,
1938 .qmenu = ctrl_run_mode_menu,
1939};
1940
1941static const struct v4l2_ctrl_config ctrls[] = {
1942 {
1943 .ops = &ctrl_ops,
1944 .id = V4L2_CID_VFLIP,
1945 .name = "Vertical flip",
1946 .type = V4L2_CTRL_TYPE_BOOLEAN,
1947 .min = false,
1948 .max = true,
1949 .step = 1,
1950 }, {
1951 .ops = &ctrl_ops,
1952 .id = V4L2_CID_HFLIP,
1953 .name = "Horizontal flip",
1954 .type = V4L2_CTRL_TYPE_BOOLEAN,
1955 .min = false,
1956 .max = true,
1957 .step = 1,
1958 }, {
1959 .ops = &ctrl_ops,
1960 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1961 .name = "Absolute exposure",
1962 .type = V4L2_CTRL_TYPE_INTEGER,
1963 .max = 0xffff,
1964 .min = 0x0,
1965 .step = 1,
1966 .def = 0x00,
1967 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
1968 }, {
1969 .ops = &ctrl_ops,
1970 .id = V4L2_CID_FOCUS_ABSOLUTE,
1971 .name = "Focus absolute",
1972 .type = V4L2_CTRL_TYPE_INTEGER,
1973 .step = 1,
1974 .max = OV8858_MAX_FOCUS_POS,
1975 }, {
1976 /* This one is junk: see the spec for proper use of this CID. */
1977 .ops = &ctrl_ops,
1978 .id = V4L2_CID_FOCUS_STATUS,
1979 .name = "Focus status",
1980 .type = V4L2_CTRL_TYPE_INTEGER,
1981 .step = 1,
1982 .max = 100,
1983 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
1984 }, {
1985 /* This is crap. For compatibility use only. */
1986 .ops = &ctrl_ops,
1987 .id = V4L2_CID_FOCAL_ABSOLUTE,
1988 .name = "Focal lenght",
1989 .type = V4L2_CTRL_TYPE_INTEGER,
1990 .min = (OV8858_FOCAL_LENGTH_NUM << 16) |
1991 OV8858_FOCAL_LENGTH_DEM,
1992 .max = (OV8858_FOCAL_LENGTH_NUM << 16) |
1993 OV8858_FOCAL_LENGTH_DEM,
1994 .step = 1,
1995 .def = (OV8858_FOCAL_LENGTH_NUM << 16) |
1996 OV8858_FOCAL_LENGTH_DEM,
1997 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1998 }, {
1999 /* This one is crap, too. For compatibility use only. */
2000 .ops = &ctrl_ops,
2001 .id = V4L2_CID_FNUMBER_ABSOLUTE,
2002 .name = "F-number",
2003 .type = V4L2_CTRL_TYPE_INTEGER,
2004 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2005 OV8858_F_NUMBER_DEM,
2006 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2007 OV8858_F_NUMBER_DEM,
2008 .step = 1,
2009 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2010 OV8858_F_NUMBER_DEM,
2011 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2012 }, {
2013 /*
2014 * The most utter crap. _Never_ use this, even for
2015 * compatibility reasons!
2016 */
2017 .ops = &ctrl_ops,
2018 .id = V4L2_CID_FNUMBER_RANGE,
2019 .name = "F-number range",
2020 .type = V4L2_CTRL_TYPE_INTEGER,
2021 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2022 (OV8858_F_NUMBER_DEM << 16) |
2023 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2024 OV8858_F_NUMBER_DEM,
2025 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2026 (OV8858_F_NUMBER_DEM << 16) |
2027 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2028 OV8858_F_NUMBER_DEM,
2029 .step = 1,
2030 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2031 (OV8858_F_NUMBER_DEM << 16) |
2032 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2033 OV8858_F_NUMBER_DEM,
2034 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2035 }, {
2036 .ops = &ctrl_ops,
2037 .id = V4L2_CID_BIN_FACTOR_HORZ,
2038 .name = "Horizontal binning factor",
2039 .type = V4L2_CTRL_TYPE_INTEGER,
2040 .max = OV8858_BIN_FACTOR_MAX,
2041 .step = 1,
2042 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2043 }, {
2044 .ops = &ctrl_ops,
2045 .id = V4L2_CID_BIN_FACTOR_VERT,
2046 .name = "Vertical binning factor",
2047 .type = V4L2_CTRL_TYPE_INTEGER,
2048 .max = OV8858_BIN_FACTOR_MAX,
2049 .step = 1,
2050 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2051 }, {
2052 .ops = &ctrl_ops,
2053 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
2054 .name = "Exposure auto priority",
2055 .type = V4L2_CTRL_TYPE_INTEGER,
2056 .min = V4L2_EXPOSURE_AUTO,
2057 .max = V4L2_EXPOSURE_APERTURE_PRIORITY,
2058 .step = 1,
2059 }
2060};
2061
e19c9205 2062static int ov8858_probe(struct i2c_client *client)
a49d2536
AC
2063{
2064 struct ov8858_device *dev;
2065 unsigned int i;
2066 int ret = 0;
2067 struct camera_sensor_platform_data *pdata;
2068
2069 dev_dbg(&client->dev, "%s:\n", __func__);
2070
2071 /* allocate sensor device & init sub device */
2072 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
309167b9 2073 if (!dev)
a49d2536 2074 return -ENOMEM;
a49d2536
AC
2075
2076 mutex_init(&dev->input_lock);
2077
a49d2536
AC
2078 dev->fmt_idx = 0;
2079 dev->sensor_id = OV_ID_DEFAULT;
2080 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
2081
2082 v4l2_i2c_subdev_init(&(dev->sd), client, &ov8858_ops);
2083
2084 if (ACPI_COMPANION(&client->dev)) {
2085 pdata = gmin_camera_platform_data(&dev->sd,
2086 ATOMISP_INPUT_FORMAT_RAW_10,
2087 atomisp_bayer_order_bggr);
2088 if (!pdata) {
2089 dev_err(&client->dev,
2090 "%s: failed to get acpi platform data\n",
2091 __func__);
2092 goto out_free;
2093 }
2094 ret = ov8858_s_config(&dev->sd, client->irq, pdata);
2095 if (ret) {
2096 dev_err(&client->dev,
2097 "%s: failed to set config\n", __func__);
2098 goto out_free;
2099 }
2100 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
2101 if (ret) {
2102 dev_err(&client->dev,
2103 "%s: failed to register subdev\n", __func__);
2104 goto out_free;
2105 }
2106 }
2107 /*
2108 * sd->name is updated with sensor driver name by the v4l2.
2109 * change it to sensor name in this case.
2110 */
2111 snprintf(dev->sd.name, sizeof(dev->sd.name), "%s%x %d-%04x",
2112 OV_SUBDEV_PREFIX, dev->sensor_id,
2113 i2c_adapter_id(client->adapter), client->addr);
2114
2115 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2116 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
2117 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
2118 dev->sd.entity.ops = &ov_entity_ops;
2119 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2120
2121 ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ctrls) + 1);
2122 if (ret) {
2123 ov8858_remove(client);
2124 return ret;
2125 }
2126
2127 dev->run_mode = v4l2_ctrl_new_custom(&dev->ctrl_handler,
2128 &ctrl_run_mode, NULL);
2129
2130 for (i = 0; i < ARRAY_SIZE(ctrls); i++)
2131 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ctrls[i], NULL);
2132
2133 if (dev->ctrl_handler.error) {
2134 ov8858_remove(client);
2135 return dev->ctrl_handler.error;
2136 }
2137
2138 /* Use same lock for controls as for everything else. */
2139 dev->ctrl_handler.lock = &dev->input_lock;
2140 dev->sd.ctrl_handler = &dev->ctrl_handler;
2141 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2142
2143 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
2144 if (ret) {
2145 ov8858_remove(client);
2146 return ret;
2147 }
2148
2149 return 0;
2150
2151out_free:
2152 v4l2_device_unregister_subdev(&dev->sd);
2153 kfree(dev);
2154 return ret;
2155}
2156
09571dfd 2157static const struct acpi_device_id ov8858_acpi_match[] = {
a49d2536
AC
2158 {"INT3477"},
2159 {},
2160};
e19c9205 2161MODULE_DEVICE_TABLE(acpi, ov8858_acpi_match);
a49d2536
AC
2162
2163static struct i2c_driver ov8858_driver = {
2164 .driver = {
e19c9205
AS
2165 .name = "ov8858",
2166 .acpi_match_table = ov8858_acpi_match,
a49d2536 2167 },
e19c9205 2168 .probe_new = ov8858_probe,
a49d2536 2169 .remove = ov8858_remove,
a49d2536 2170};
2cb63c4c 2171module_i2c_driver(ov8858_driver);
a49d2536
AC
2172
2173MODULE_DESCRIPTION("A low-level driver for Omnivision OV8858 sensors");
2174MODULE_LICENSE("GPL");