2 * Support for OmniVision ov8858 camera sensor.
4 * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
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.
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.
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
22 #include <linux/delay.h>
23 #include <linux/module.h>
24 #include <media/v4l2-device.h>
25 #include <linux/acpi.h>
26 #include "../include/linux/atomisp_gmin_platform.h"
27 #ifdef CONFIG_PLATFORM_BTNS
28 #include "ov8858_btns.h"
32 static int ov8858_i2c_read(struct i2c_client
*client
, u16 len
, u16 addr
,
35 struct i2c_msg msg
[2];
39 if (!client
->adapter
) {
40 dev_err(&client
->dev
, "%s error, no adapter\n", __func__
);
44 dev_dbg(&client
->dev
, "%s: len = %d, addr = 0x%04x\n",
47 memset(msg
, 0, sizeof(msg
));
49 address
[0] = (addr
>> 8) & 0xff;
50 address
[1] = addr
& 0xff;
52 msg
[0].addr
= client
->addr
;
54 msg
[0].len
= I2C_MSG_LENGTH
;
57 msg
[1].addr
= client
->addr
;
59 msg
[1].flags
= I2C_M_RD
;
62 err
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
71 dev_err(&client
->dev
, "reading from address 0x%x error %d", addr
, err
);
75 static int ov8858_read_reg(struct i2c_client
*client
, u16 type
, u16 reg
,
78 u8 data
[OV8858_SHORT_MAX
];
81 dev_dbg(&client
->dev
, "%s: type = %d, reg = 0x%04x\n",
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",
91 memset(data
, 0, sizeof(data
));
93 err
= ov8858_i2c_read(client
, type
, reg
, data
);
97 /* high byte comes first */
98 if (type
== OV8858_8BIT
)
101 *val
= data
[0] << 8 | data
[1];
103 dev_dbg(&client
->dev
, "%s: val = 0x%04x\n", __func__
, *val
);
108 dev_err(&client
->dev
, "read from offset 0x%x error %d", reg
, err
);
112 static int ov8858_i2c_write(struct i2c_client
*client
, u16 len
, u8
*data
)
115 const int num_msg
= 1;
118 msg
.addr
= client
->addr
;
123 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
125 return ret
== num_msg
? 0 : -EIO
;
129 ov8858_write_reg(struct i2c_client
*client
, u16 data_length
, u16 reg
, u16 val
)
132 unsigned char data
[4] = {0};
134 const u16 len
= data_length
+ sizeof(u16
); /* 16-bit address + data */
136 dev_dbg(&client
->dev
,
137 "%s: data_length = %d, reg = 0x%04x, val = 0x%04x\n",
138 __func__
, data_length
, reg
, val
);
140 if (!client
->adapter
) {
141 dev_err(&client
->dev
, "%s error, no adapter\n", __func__
);
145 if (data_length
!= OV8858_8BIT
&& data_length
!= OV8858_16BIT
) {
146 dev_err(&client
->dev
, "%s error, invalid length\n", __func__
);
150 /* high byte goes out first */
152 *wreg
= cpu_to_be16(reg
);
154 if (data_length
== OV8858_8BIT
) {
158 u16
*wdata
= (u16
*)&data
[2];
159 *wdata
= be16_to_cpu(val
);
162 ret
= ov8858_i2c_write(client
, len
, data
);
164 dev_err(&client
->dev
,
165 "write error: wrote 0x%x to offset 0x%x error %d",
172 * ov8858_write_reg_array - Initializes a list of registers
173 * @client: i2c driver client structure
174 * @reglist: list of registers to be written
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().
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.
185 static int __ov8858_flush_reg_array(struct i2c_client
*client
,
186 struct ov8858_write_ctrl
*ctrl
)
189 if (ctrl
->index
== 0)
192 size
= sizeof(u16
) + ctrl
->index
; /* 16-bit address + data */
193 ctrl
->buffer
.addr
= cpu_to_be16(ctrl
->buffer
.addr
);
196 return ov8858_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
199 static int __ov8858_buf_reg_array(struct i2c_client
*client
,
200 struct ov8858_write_ctrl
*ctrl
,
201 const struct ov8858_reg
*next
)
206 switch (next
->type
) {
209 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
213 data16
= (u16
*)&ctrl
->buffer
.data
[ctrl
->index
];
214 *data16
= cpu_to_be16((u16
)next
->val
);
220 /* When first item is added, we need to store its starting address */
221 if (ctrl
->index
== 0)
222 ctrl
->buffer
.addr
= next
->sreg
;
227 * Buffer cannot guarantee free space for u32? Better flush it to avoid
228 * possible lack of memory for next item.
230 if (ctrl
->index
+ sizeof(u16
) >= OV8858_MAX_WRITE_BUF_SIZE
)
231 __ov8858_flush_reg_array(client
, ctrl
);
237 __ov8858_write_reg_is_consecutive(struct i2c_client
*client
,
238 struct ov8858_write_ctrl
*ctrl
,
239 const struct ov8858_reg
*next
)
241 if (ctrl
->index
== 0)
244 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->sreg
;
247 static int ov8858_write_reg_array(struct i2c_client
*client
,
248 const struct ov8858_reg
*reglist
)
250 const struct ov8858_reg
*next
= reglist
;
251 struct ov8858_write_ctrl ctrl
;
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
);
266 * If next address is not consecutive, data needs to be
267 * flushed before proceeding
269 if (!__ov8858_write_reg_is_consecutive(client
,
271 err
= __ov8858_flush_reg_array(client
, &ctrl
);
275 err
= __ov8858_buf_reg_array(client
, &ctrl
, next
);
277 dev_err(&client
->dev
, "%s: write error\n",
285 return __ov8858_flush_reg_array(client
, &ctrl
);
288 static int __ov8858_min_fps_diff(int fps
,
289 const struct ov8858_fps_setting
*fps_list
)
297 for (i
= 0; i
< MAX_FPS_OPTIONS_SUPPORTED
; i
++) {
298 if (!fps_list
[i
].fps
)
300 if (abs(fps_list
[i
].fps
- fps
) < diff
)
301 diff
= abs(fps_list
[i
].fps
- fps
);
307 static int __ov8858_nearest_fps_index(int fps
,
308 const struct ov8858_fps_setting
*fps_list
)
313 for (i
= 0; i
< MAX_FPS_OPTIONS_SUPPORTED
; i
++) {
314 if (!fps_list
[i
].fps
)
316 if (abs(fps_list
[i
].fps
- fps
)
317 < abs(fps_list
[fps_index
].fps
- fps
))
323 static int __ov8858_update_frame_timing(struct v4l2_subdev
*sd
,
326 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
330 dev_dbg(&client
->dev
, "%s OV8858_TIMING_HTS=0x%04x\n",
333 /* HTS = pixel_per_line / 2 */
334 ret
= ov8858_write_reg(client
, OV8858_16BIT
,
335 OV8858_TIMING_HTS
, *hts
>> 1);
338 dev_dbg(&client
->dev
, "%s OV8858_TIMING_VTS=0x%04x\n",
341 return ov8858_write_reg(client
, OV8858_16BIT
, OV8858_TIMING_VTS
, *vts
);
344 static int __ov8858_set_exposure(struct v4l2_subdev
*sd
, int exposure
, int gain
,
345 int dig_gain
, u16
*hts
, u16
*vts
)
347 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
348 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
350 dev_dbg(&client
->dev
, "%s, exposure = %d, gain=%d, dig_gain=%d\n",
351 __func__
, exposure
, gain
, dig_gain
);
353 if (dev
->limit_exposure_flag
) {
354 if (exposure
> *vts
- OV8858_INTEGRATION_TIME_MARGIN
)
355 exposure
= *vts
- OV8858_INTEGRATION_TIME_MARGIN
;
357 if (*vts
< exposure
+ OV8858_INTEGRATION_TIME_MARGIN
)
358 *vts
= (u16
) exposure
+ OV8858_INTEGRATION_TIME_MARGIN
;
361 ret
= __ov8858_update_frame_timing(sd
, hts
, vts
);
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);
372 ret
= ov8858_write_reg(client
, OV8858_8BIT
,
373 OV8858_LONG_EXPO
+1, (exp_val
>> 8) & 0xFF);
377 ret
= ov8858_write_reg(client
, OV8858_8BIT
,
378 OV8858_LONG_EXPO
, (exp_val
>> 16) & 0x0F);
382 /* Digital gain : to all MWB channel gains */
384 ret
= ov8858_write_reg(client
, OV8858_16BIT
,
385 OV8858_MWB_RED_GAIN_H
, dig_gain
);
389 ret
= ov8858_write_reg(client
, OV8858_16BIT
,
390 OV8858_MWB_GREEN_GAIN_H
, dig_gain
);
394 ret
= ov8858_write_reg(client
, OV8858_16BIT
,
395 OV8858_MWB_BLUE_GAIN_H
, dig_gain
);
400 ret
= ov8858_write_reg(client
, OV8858_16BIT
, OV8858_LONG_GAIN
,
406 dev
->exposure
= exposure
;
407 dev
->digital_gain
= dig_gain
;
412 static int ov8858_set_exposure(struct v4l2_subdev
*sd
, int exposure
, int gain
,
415 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
416 const struct ov8858_resolution
*res
;
420 mutex_lock(&dev
->input_lock
);
422 /* Validate exposure: cannot exceed 16bit value */
423 exposure
= clamp_t(int, exposure
, 0, OV8858_MAX_EXPOSURE_VALUE
);
425 /* Validate gain: must not exceed maximum 8bit value */
426 gain
= clamp_t(int, gain
, 0, OV8858_MAX_GAIN_VALUE
);
428 /* Validate digital gain: must not exceed 12 bit value*/
429 dig_gain
= clamp_t(int, dig_gain
, 0, OV8858_MWB_GAIN_MAX
);
431 res
= &dev
->curr_res_table
[dev
->fmt_idx
];
433 * Vendor: HTS reg value is half the total pixel line
435 hts
= res
->fps_options
[dev
->fps_index
].pixels_per_line
;
436 vts
= res
->fps_options
[dev
->fps_index
].lines_per_frame
;
438 ret
= __ov8858_set_exposure(sd
, exposure
, gain
, dig_gain
, &hts
, &vts
);
440 mutex_unlock(&dev
->input_lock
);
446 When exposure gain value set to sensor, the sensor changed value.
447 So we need the function to get real value
449 static int ov8858_g_update_exposure(struct v4l2_subdev
*sd
,
450 struct atomisp_update_exposure
*exposure
)
452 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
453 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
454 int gain
= exposure
->gain
;
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);
465 static int ov8858_s_exposure(struct v4l2_subdev
*sd
,
466 struct atomisp_exposure
*exposure
)
468 return ov8858_set_exposure(sd
, exposure
->integration_time
[0],
469 exposure
->gain
[0], exposure
->gain
[1]);
472 static int ov8858_priv_int_data_init(struct v4l2_subdev
*sd
)
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;
480 if (!dev
->otp_data
) {
481 dev
->otp_data
= devm_kzalloc(&client
->dev
, size
, GFP_KERNEL
);
482 if (!dev
->otp_data
) {
487 /* Streaming has to be on */
488 r
= ov8858_write_reg(client
, OV8858_8BIT
, OV8858_STREAM_MODE
,
493 /* Turn off Dead Pixel Correction */
494 r
= ov8858_read_reg(client
, OV8858_8BIT
,
495 OV8858_OTP_ISP_CTRL2
, &isp_ctrl2
);
499 r
= ov8858_write_reg(client
, OV8858_8BIT
, OV8858_OTP_ISP_CTRL2
,
500 isp_ctrl2
& ~OV8858_OTP_DPC_ENABLE
);
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
);
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
);
518 r
= ov8858_write_reg(client
, OV8858_16BIT
,
519 OV8858_OTP_END_ADDR_REG
,
520 OV8858_OTP_END_ADDR
);
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
);
530 /* Wait for the data to load into the buffer */
531 usleep_range(5000, 5500);
533 /* Read the OTP data from the buffer */
534 r
= ov8858_i2c_read(client
, size
, OV8858_OTP_START_ADDR
,
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
);
546 r
= ov8858_write_reg(client
, 1, OV8858_STREAM_MODE
, 0x00);
548 dev_err(&client
->dev
, "%s: cannot turn off streaming\n",
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);
563 devm_kfree(&client
->dev
, dev
->otp_data
);
564 dev
->otp_data
= NULL
;
566 dev_err(&client
->dev
, "%s: OTP reading failed\n", __func__
);
570 static int ov8858_g_priv_int_data(struct v4l2_subdev
*sd
,
571 struct v4l2_private_int_data
*priv
)
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;
578 mutex_lock(&dev
->input_lock
);
580 if (!dev
->otp_data
) {
581 dev_err(&client
->dev
, "%s: otp data is NULL\n", __func__
);
582 mutex_unlock(&dev
->input_lock
);
586 if (copy_to_user(priv
->data
, dev
->otp_data
,
587 min_t(__u32
, priv
->size
, size
))) {
589 dev_err(&client
->dev
, "%s: OTP reading failed\n", __func__
);
590 mutex_unlock(&dev
->input_lock
);
595 mutex_unlock(&dev
->input_lock
);
600 static int __ov8858_init(struct v4l2_subdev
*sd
)
602 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
603 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
605 dev_dbg(&client
->dev
, "%s\n", __func__
);
607 /* Sets the default FPS */
610 /* Set default exposure values (initially start values) */
613 dev
->digital_gain
= 1024;
614 dev
->limit_exposure_flag
= false;
616 dev_dbg(&client
->dev
, "%s: Writing basic settings to ov8858\n",
618 ret
= ov8858_write_reg_array(client
, ov8858_BasicSettings
);
622 return ov8858_priv_int_data_init(sd
);
625 static int ov8858_init(struct v4l2_subdev
*sd
, u32 val
)
627 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
630 mutex_lock(&dev
->input_lock
);
631 ret
= __ov8858_init(sd
);
632 mutex_unlock(&dev
->input_lock
);
637 static void ov8858_uninit(struct v4l2_subdev
*sd
)
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__
);
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
);
652 v4l2_ctrl_s_ctrl(ctrl
, V4L2_EXPOSURE_AUTO
);
653 mutex_lock(&dev
->input_lock
);
656 static int ov8858_g_comp_delay(struct v4l2_subdev
*sd
, unsigned int *usec
)
658 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
659 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
660 int ret
= 0, exposure
;
663 if (dev
->exposure
== 0) {
664 ret
= ov8858_read_reg(client
, OV8858_16BIT
,
665 OV8858_LONG_EXPO
+ 1, &data
);
671 exposure
= dev
->exposure
;
674 ret
= ov8858_read_reg(client
, OV8858_16BIT
, OV8858_TIMING_VTS
, &vts
);
676 vts
= OV8858_DEPTH_VTS_CONST
;
678 *usec
= (exposure
* 33333 / vts
);
679 if (*usec
> OV8858_DEPTH_COMP_CONST
)
680 *usec
= *usec
- OV8858_DEPTH_COMP_CONST
;
682 *usec
= OV8858_DEPTH_COMP_CONST
;
687 static long ov8858_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
689 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
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
);
701 dev_dbg(&client
->dev
, "Unhandled command 0x%X\n", cmd
);
706 static int __power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
709 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
710 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
712 if (!dev
|| !dev
->platform_data
)
715 /* Non-gmin platforms use the legacy callback */
716 if (dev
->platform_data
->power_ctrl
)
717 return dev
->platform_data
->power_ctrl(sd
, flag
);
719 if (dev
->platform_data
->v1p2_ctrl
) {
720 ret
= dev
->platform_data
->v1p2_ctrl(sd
, flag
);
722 dev_err(&client
->dev
,
723 "failed to power %s 1.2v power rail\n",
724 flag
? "up" : "down");
729 if (dev
->platform_data
->v2p8_ctrl
) {
730 ret
= dev
->platform_data
->v2p8_ctrl(sd
, flag
);
732 dev_err(&client
->dev
,
733 "failed to power %s 2.8v power rail\n",
734 flag
? "up" : "down");
739 if (dev
->platform_data
->v1p8_ctrl
) {
740 ret
= dev
->platform_data
->v1p8_ctrl(sd
, flag
);
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);
752 msleep(20); /* Wait for power lines to stabilize */
756 static int __gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
758 struct i2c_client
*client
;
759 struct ov8858_device
*dev
;
764 client
= v4l2_get_subdevdata(sd
);
765 dev
= to_ov8858_sensor(sd
);
767 if (!client
|| !dev
|| !dev
->platform_data
)
770 /* Non-gmin platforms use the legacy callback */
771 if (dev
->platform_data
->gpio_ctrl
)
772 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
774 if (dev
->platform_data
->gpio0_ctrl
)
775 return dev
->platform_data
->gpio0_ctrl(sd
, flag
);
777 dev_err(&client
->dev
, "failed to find platform gpio callback\n");
782 static int power_up(struct v4l2_subdev
*sd
)
784 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
785 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
787 dev_dbg(&client
->dev
, "%s\n", __func__
);
790 ret
= __power_ctrl(sd
, 1);
792 dev_err(&client
->dev
, "power rail on failed %d.\n", ret
);
797 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
799 dev_err(&client
->dev
, "flisclk on failed %d\n", ret
);
804 ret
= __gpio_ctrl(sd
, 1);
806 dev_err(&client
->dev
, "gpio on failed %d\n", ret
);
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);
816 dev
->platform_data
->flisclk_ctrl(sd
, 0);
820 dev_err(&client
->dev
, "Sensor power-up failed\n");
825 static int power_down(struct v4l2_subdev
*sd
)
827 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
828 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
830 dev_dbg(&client
->dev
, "%s\n", __func__
);
832 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
834 dev_err(&client
->dev
, "flisclk off failed\n");
836 ret
= __gpio_ctrl(sd
, 0);
838 dev_err(&client
->dev
, "gpio off failed\n");
840 ret
= __power_ctrl(sd
, 0);
842 dev_err(&client
->dev
, "power rail off failed.\n");
847 static int __ov8858_s_power(struct v4l2_subdev
*sd
, int on
)
849 struct ov8858_device
*dev
= to_ov8858_sensor(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)
863 if (dev
->vcm_driver
&& dev
->vcm_driver
->power_up
) {
864 ret
= dev
->vcm_driver
->power_up(sd
);
870 return __ov8858_init(sd
);
876 static int ov8858_s_power(struct v4l2_subdev
*sd
, int on
)
879 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
881 mutex_lock(&dev
->input_lock
);
882 ret
= __ov8858_s_power(sd
, on
);
883 mutex_unlock(&dev
->input_lock
);
886 * FIXME: Compatibility with old behaviour: return to preview
887 * when the device is power cycled.
890 v4l2_ctrl_s_ctrl(dev
->run_mode
, ATOMISP_RUN_MODE_PREVIEW
);
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.
899 static int ov8858_get_register(struct v4l2_subdev
*sd
, int reg
, int type
,
900 const struct ov8858_reg
*reglist
)
902 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
903 const struct ov8858_reg
*next
;
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
)
912 if (type
== OV8858_16BIT
&&
913 next
[1].type
!= OV8858_TOK_TERM
)
914 return next
[0].val
<< 8 | next
[1].val
;
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
);
927 static inline int ov8858_get_register_16bit(struct v4l2_subdev
*sd
, int reg
,
928 const struct ov8858_reg
*reglist
)
930 return ov8858_get_register(sd
, reg
, OV8858_16BIT
, reglist
);
933 static inline int ov8858_get_register_8bit(struct v4l2_subdev
*sd
, int reg
,
934 const struct ov8858_reg
*reglist
)
936 return ov8858_get_register(sd
, reg
, OV8858_8BIT
, reglist
);
939 static int __ov8858_get_pll1_values(struct v4l2_subdev
*sd
,
941 const struct ov8858_reg
*reglist
)
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};
950 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL1_PREDIV0
, reglist
);
954 if (ret
& OV8858_PLL1_PREDIV0_MASK
)
957 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL1_PREDIV
, reglist
);
962 prediv_idx
= ret
& OV8858_PLL1_PREDIV_MASK
;
963 *value
= *value
* 2 / prediv_coef
[prediv_idx
];
965 ret
= ov8858_get_register_16bit(sd
, OV8858_PLL1_MULTIPLIER
, reglist
);
970 *value
*= multiplier
& OV8858_PLL1_MULTIPLIER_MASK
;
971 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL1_SYS_PRE_DIV
, reglist
);
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
);
983 if (ret
& OV8858_PLL1_SYS_DIVIDER_MASK
)
986 dev_dbg(&client
->dev
, "%s: *value: %d\n", __func__
, *value
);
991 static int __ov8858_get_pll2a_values(struct v4l2_subdev
*sd
, int *value
,
992 const struct ov8858_reg
*reglist
)
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};
1000 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL2_PREDIV0
, reglist
);
1004 if (ret
& OV8858_PLL2_PREDIV0_MASK
)
1007 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL2_PREDIV
, reglist
);
1011 prediv_idx
= (ret
& OV8858_PLL2_PREDIV_MASK
);
1012 *value
= *value
* 2 / prediv_coef
[prediv_idx
];
1014 ret
= ov8858_get_register_16bit(sd
, OV8858_PLL2_MULTIPLIER
, reglist
);
1019 *value
*= multiplier
& OV8858_PLL2_MULTIPLIER_MASK
;
1020 dev_dbg(&client
->dev
, "%s: *value: %d\n", __func__
, *value
);
1024 static int __ov8858_get_pll2b_values(struct v4l2_subdev
*sd
, int *value
,
1025 const struct ov8858_reg
*reglist
)
1027 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1028 unsigned int dac_divider
;
1031 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL2_DAC_DIVIDER
, reglist
);
1035 dac_divider
= (ret
& OV8858_PLL2_DAC_DIVIDER_MASK
) + 1;
1036 *value
/= dac_divider
;
1038 dev_dbg(&client
->dev
, "%s: *value: %d\n", __func__
, *value
);
1042 static int __ov8858_get_pll2c_values(struct v4l2_subdev
*sd
, int *value
,
1043 const struct ov8858_reg
*reglist
)
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};
1051 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL2_SYS_PRE_DIV
, reglist
);
1055 sys_pre_div
= (ret
& OV8858_PLL2_SYS_PRE_DIV_MASK
) + 1;
1056 *value
/= sys_pre_div
;
1058 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL2_SYS_DIVIDER
, reglist
);
1062 sys_divider_idx
= ret
& OV8858_PLL2_SYS_DIVIDER_MASK
;
1063 *value
*= 2 / sys_divider_coef
[sys_divider_idx
];
1065 dev_dbg(&client
->dev
, "%s: *value: %d\n", __func__
, *value
);
1070 static int ov8858_get_intg_factor(struct v4l2_subdev
*sd
,
1071 struct camera_mipi_info
*info
,
1072 const struct ov8858_reg
*reglist
)
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
;
1089 memset(&info
->data
, 0, sizeof(info
->data
));
1091 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL_SCLKSEL1
, reglist
);
1095 dev_dbg(d
, "%s: OV8858_PLL_SCLKSEL1: 0x%02x\n", __func__
, ret
);
1096 pll_sclksel1
= ret
& OV8858_PLL_SCLKSEL1_MASK
;
1098 ret
= ov8858_get_register_8bit(sd
, OV8858_PLL_SCLKSEL2
, reglist
);
1102 dev_dbg(d
, "%s: OV8858_PLL_SCLKSEL2: 0x%02x\n", __func__
, ret
);
1103 pll_sclksel2
= ret
& OV8858_PLL_SCLKSEL2_MASK
;
1106 ret
= __ov8858_get_pll2a_values(sd
, &sclk
, reglist
);
1109 ret
= __ov8858_get_pll2b_values(sd
, &sclk
, reglist
);
1112 } else if (pll_sclksel1
) {
1113 ret
= __ov8858_get_pll2a_values(sd
, &sclk
, reglist
);
1116 ret
= __ov8858_get_pll2c_values(sd
, &sclk
, reglist
);
1120 ret
= __ov8858_get_pll1_values(sd
, &sclk
, reglist
);
1125 ret
= ov8858_get_register_8bit(sd
, OV8858_SRB_HOST_INPUT_DIS
, reglist
);
1129 dev_dbg(d
, "%s: OV8858_SRB_HOST_INPUT_DIS: 0x%02x\n", __func__
, ret
);
1131 sys_pre_div
= ret
& OV8858_SYS_PRE_DIV_MASK
;
1132 sys_pre_div
>>= OV8858_SYS_PRE_DIV_OFFSET
;
1134 if (sys_pre_div
== 1)
1136 else if (sys_pre_div
== 2)
1139 sclk_pdiv
= ret
& OV8858_SCLK_PDIV_MASK
;
1140 sclk_pdiv
>>= OV8858_SCLK_PDIV_OFFSET
;
1145 dev_dbg(d
, "%s: sclk: %d\n", __func__
, sclk
);
1147 dev
->vt_pix_clk_freq_mhz
= sclk
;
1148 m
->vt_pix_clk_freq_mhz
= sclk
;
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
;
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
);
1161 dev_dbg(&client
->dev
, "%s: get HTS %d\n", __func__
, hts
);
1163 /* OV Sensor do not use fine integration time. */
1164 m
->fine_integration_time_min
= 0;
1165 m
->fine_integration_time_max_margin
= 0;
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.
1172 m
->read_mode
= res
->bin_factor_x
?
1173 OV8858_READ_MODE_BINNING_ON
: OV8858_READ_MODE_BINNING_OFF
;
1175 ret
= ov8858_get_register_8bit(sd
, OV8858_H_INC_ODD
, res
->regs
);
1178 m
->binning_factor_x
= (ret
+ 1) / 2;
1180 ret
= ov8858_get_register_8bit(sd
, OV8858_V_INC_ODD
, res
->regs
);
1183 m
->binning_factor_y
= (ret
+ 1) / 2;
1185 /* Get the cropping and output resolution to ISP for this mode. */
1186 ret
= ov8858_get_register_16bit(sd
, OV8858_HORIZONTAL_START_H
,
1191 m
->crop_horizontal_start
= ret
;
1193 ret
= ov8858_get_register_16bit(sd
, OV8858_VERTICAL_START_H
, res
->regs
);
1197 m
->crop_vertical_start
= ret
;
1199 ret
= ov8858_get_register_16bit(sd
, OV8858_HORIZONTAL_END_H
, res
->regs
);
1203 m
->crop_horizontal_end
= ret
;
1205 ret
= ov8858_get_register_16bit(sd
, OV8858_VERTICAL_END_H
, res
->regs
);
1209 m
->crop_vertical_end
= ret
;
1211 ret
= ov8858_get_register_16bit(sd
, OV8858_HORIZONTAL_OUTPUT_SIZE_H
,
1216 m
->output_width
= ret
;
1218 ret
= ov8858_get_register_16bit(sd
, OV8858_VERTICAL_OUTPUT_SIZE_H
,
1223 m
->output_height
= ret
;
1229 * distance - calculate the distance
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.
1240 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1241 static int distance(struct ov8858_resolution
const *res
, const u32 w
,
1247 if (w
== 0 || h
== 0 ||
1248 res
->width
< w
|| res
->height
< h
)
1251 ratio
= res
->width
<< 13;
1254 ratio
/= res
->height
;
1256 distance
= abs(ratio
- 8192);
1258 if (distance
> LARGEST_ALLOWED_RATIO_MISMATCH
)
1264 * Returns the nearest higher resolution index.
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.
1271 static int nearest_resolution_index(struct v4l2_subdev
*sd
, int w
, int h
)
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
);
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
);
1293 if (dist
< min_dist
) {
1295 min_res_w
= tmp_res
->width
;
1296 min_fps_diff
= __ov8858_min_fps_diff(dev
->fps
,
1297 tmp_res
->fps_options
);
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
;
1307 if (tmp_res
->width
< min_res_w
) {
1308 min_res_w
= tmp_res
->width
;
1317 static int ov8858_set_fmt(struct v4l2_subdev
*sd
,
1318 struct v4l2_subdev_pad_config
*cfg
,
1319 struct v4l2_subdev_format
*format
)
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
;
1333 ov8858_info
= v4l2_get_subdev_hostdata(sd
);
1334 if (ov8858_info
== NULL
)
1337 mutex_lock(&dev
->input_lock
);
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
;
1344 idx
= nearest_resolution_index(sd
, fmt
->width
, fmt
->height
);
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.
1353 idx
= dev
->entries_curr_table
- 1;
1355 fmt
->width
= dev
->curr_res_table
[idx
].width
;
1356 fmt
->height
= dev
->curr_res_table
[idx
].height
;
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
);
1366 dev
->fmt_idx
= nearest_resolution_index(sd
, fmt
->width
, fmt
->height
);
1367 if (dev
->fmt_idx
== -1) {
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
);
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
;
1380 dev
->regs
= res
->regs
;
1382 ret
= ov8858_write_reg_array(client
, dev
->regs
);
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
;
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
;
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
);
1401 ret
= ov8858_get_intg_factor(sd
, ov8858_info
, dev
->regs
);
1404 mutex_unlock(&dev
->input_lock
);
1409 static int ov8858_get_fmt(struct v4l2_subdev
*sd
,
1410 struct v4l2_subdev_pad_config
*cfg
,
1411 struct v4l2_subdev_format
*format
)
1413 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1414 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
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
);
1430 static int ov8858_detect(struct i2c_client
*client
, u16
*id
)
1432 struct i2c_adapter
*adapter
= client
->adapter
;
1438 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
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
);
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
);
1449 dev_dbg(&client
->dev
, "%s: id_low = 0x%04x\n", __func__
, id_low
);
1450 *id
= (id_hi
<< 8) | id_low
;
1452 dev_dbg(&client
->dev
, "%s: chip_id = 0x%04x\n", __func__
, *id
);
1454 dev_info(&client
->dev
, "%s: chip_id = 0x%04x\n", __func__
, *id
);
1455 if (*id
!= OV8858_CHIP_ID
)
1458 /* Stream off now. */
1459 return ov8858_write_reg(client
, OV8858_8BIT
, OV8858_STREAM_MODE
, 0);
1462 static void __ov8858_print_timing(struct v4l2_subdev
*sd
)
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
;
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));
1488 * ov8858 stream on/off
1490 static int ov8858_s_stream(struct v4l2_subdev
*sd
, int enable
)
1492 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1493 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1496 dev_dbg(&client
->dev
, "%s: enable = %d\n", __func__
, enable
);
1498 /* Set orientation */
1499 ret
= ov8858_read_reg(client
, OV8858_8BIT
, OV8858_FORMAT2
, &val
);
1503 ret
= ov8858_write_reg(client
, OV8858_8BIT
, OV8858_FORMAT2
,
1504 dev
->hflip
? val
| OV8858_FLIP_ENABLE
:
1505 val
& ~OV8858_FLIP_ENABLE
);
1509 ret
= ov8858_read_reg(client
, OV8858_8BIT
, OV8858_FORMAT1
, &val
);
1513 ret
= ov8858_write_reg(client
, OV8858_8BIT
, OV8858_FORMAT1
,
1514 dev
->vflip
? val
| OV8858_FLIP_ENABLE
:
1515 val
& ~OV8858_FLIP_ENABLE
);
1519 mutex_lock(&dev
->input_lock
);
1521 __ov8858_print_timing(sd
);
1522 ret
= ov8858_write_reg_array(client
, ov8858_streaming
);
1524 dev_err(&client
->dev
, "write_reg_array err\n");
1529 ret
= ov8858_write_reg_array(client
, ov8858_soft_standby
);
1531 dev_err(&client
->dev
, "write_reg_array err\n");
1539 mutex_unlock(&dev
->input_lock
);
1543 static int __update_ov8858_device_settings(struct ov8858_device
*dev
,
1546 if (sensor_id
== OV8858_CHIP_ID
)
1547 #ifdef CONFIG_PLATFORM_BTNS
1548 dev
->vcm_driver
= &ov8858_vcms
[OV8858_ID_DEFAULT
];
1550 dev
->vcm_driver
= &ov8858_vcms
[OV8858_SUNNY
];
1555 if (dev
->vcm_driver
&& dev
->vcm_driver
->init
)
1556 return dev
->vcm_driver
->init(&dev
->sd
);
1561 static int ov8858_s_config(struct v4l2_subdev
*sd
,
1562 int irq
, void *pdata
)
1564 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1565 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1572 dev
->platform_data
= pdata
;
1574 mutex_lock(&dev
->input_lock
);
1576 if (dev
->platform_data
->platform_init
) {
1577 ret
= dev
->platform_data
->platform_init(client
);
1579 mutex_unlock(&dev
->input_lock
);
1580 dev_err(&client
->dev
, "platform init error %d!\n", ret
);
1585 ret
= __ov8858_s_power(sd
, 1);
1587 dev_err(&client
->dev
, "power-up error %d!\n", ret
);
1588 mutex_unlock(&dev
->input_lock
);
1592 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1596 /* config & detect sensor */
1597 ret
= ov8858_detect(client
, &sensor_id
);
1599 dev_err(&client
->dev
, "detect error %d!\n", ret
);
1603 dev
->sensor_id
= sensor_id
;
1605 /* power off sensor */
1606 ret
= __ov8858_s_power(sd
, 0);
1608 dev
->platform_data
->csi_cfg(sd
, 0);
1609 dev_err(&client
->dev
, "__ov8858_s_power-down error %d!\n", ret
);
1613 /* Resolution settings depend on sensor type and platform */
1614 ret
= __update_ov8858_device_settings(dev
, dev
->sensor_id
);
1616 dev
->platform_data
->csi_cfg(sd
, 0);
1617 dev_err(&client
->dev
, "__update_ov8858_device_settings error %d!\n", ret
);
1621 mutex_unlock(&dev
->input_lock
);
1625 dev
->platform_data
->csi_cfg(sd
, 0);
1627 __ov8858_s_power(sd
, 0);
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");
1637 ov8858_enum_mbus_code(struct v4l2_subdev
*sd
,
1638 struct v4l2_subdev_pad_config
*cfg
,
1639 struct v4l2_subdev_mbus_code_enum
*code
)
1643 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1649 ov8858_enum_frame_size(struct v4l2_subdev
*sd
,
1650 struct v4l2_subdev_pad_config
*cfg
,
1651 struct v4l2_subdev_frame_size_enum
*fse
)
1653 int index
= fse
->index
;
1654 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1656 mutex_lock(&dev
->input_lock
);
1657 if (index
>= dev
->entries_curr_table
) {
1658 mutex_unlock(&dev
->input_lock
);
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
);
1671 static int ov8858_s_ctrl(struct v4l2_ctrl
*ctrl
)
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
);
1677 /* input_lock is taken by the control framework, so it
1678 * doesn't need to be taken here.
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
);
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
);
1693 dev
->curr_res_table
= ov8858_res_preview
;
1694 dev
->entries_curr_table
=
1695 ARRAY_SIZE(ov8858_res_preview
);
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
,
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;
1713 case V4L2_CID_HFLIP
:
1714 dev
->hflip
= ctrl
->val
;
1716 case V4L2_CID_VFLIP
:
1717 dev
->vflip
= ctrl
->val
;
1720 dev_err(&client
->dev
, "%s: Error: Invalid ctrl: 0x%X\n",
1721 __func__
, ctrl
->id
);
1726 static int ov8858_g_ctrl(struct v4l2_ctrl
*ctrl
)
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
);
1732 int i
= dev
->fmt_idx
;
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
,
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
);
1745 r_even
= ov8858_get_register_8bit(&dev
->sd
, OV8858_H_INC_EVEN
,
1746 dev
->curr_res_table
[i
].regs
);
1749 ctrl
->val
= fls(r_odd
+ (r_even
)) - 2;
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
);
1757 r_even
= ov8858_get_register_8bit(&dev
->sd
, OV8858_V_INC_EVEN
,
1758 dev
->curr_res_table
[i
].regs
);
1761 ctrl
->val
= fls(r_odd
+ (r_even
)) - 2;
1763 case V4L2_CID_HFLIP
:
1764 ctrl
->val
= dev
->hflip
;
1766 case V4L2_CID_VFLIP
:
1767 ctrl
->val
= dev
->vflip
;
1769 case V4L2_CID_EXPOSURE_ABSOLUTE
:
1770 ctrl
->val
= dev
->exposure
;
1773 dev_warn(&client
->dev
,
1774 "%s: Error: Invalid ctrl: 0x%X\n", __func__
, ctrl
->id
);
1782 ov8858_g_frame_interval(struct v4l2_subdev
*sd
,
1783 struct v4l2_subdev_frame_interval
*interval
)
1785 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1786 const struct ov8858_resolution
*res
=
1787 &dev
->curr_res_table
[dev
->fmt_idx
];
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
);
1796 static int __ov8858_s_frame_interval(struct v4l2_subdev
*sd
,
1797 struct v4l2_subdev_frame_interval
*interval
)
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
;
1808 info
= v4l2_get_subdev_hostdata(sd
);
1812 if (!interval
->interval
.numerator
)
1813 interval
->interval
.numerator
= 1;
1815 fps
= interval
->interval
.denominator
/ interval
->interval
.numerator
;
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 */
1824 /* Ignore if we are already using the required FPS. */
1825 if (fps
== res
->fps_options
[dev
->fps_index
].fps
)
1828 fps_index
= __ov8858_nearest_fps_index(fps
, res
->fps_options
);
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");
1837 dev
->fps_index
= fps_index
;
1838 dev
->fps
= res
->fps_options
[dev
->fps_index
].fps
;
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
;
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
);
1852 /* Update the new values so that user side knows the current settings */
1853 ret
= ov8858_get_intg_factor(sd
, info
, dev
->regs
);
1857 interval
->interval
.denominator
= res
->fps_options
[dev
->fps_index
].fps
;
1858 interval
->interval
.numerator
= 1;
1859 __ov8858_print_timing(sd
);
1864 static int ov8858_s_frame_interval(struct v4l2_subdev
*sd
,
1865 struct v4l2_subdev_frame_interval
*interval
)
1867 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1870 mutex_lock(&dev
->input_lock
);
1871 ret
= __ov8858_s_frame_interval(sd
, interval
);
1872 mutex_unlock(&dev
->input_lock
);
1877 static int ov8858_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1879 struct ov8858_device
*dev
= to_ov8858_sensor(sd
);
1881 mutex_lock(&dev
->input_lock
);
1882 *frames
= dev
->curr_res_table
[dev
->fmt_idx
].skip_frames
;
1883 mutex_unlock(&dev
->input_lock
);
1888 static const struct v4l2_subdev_sensor_ops ov8858_sensor_ops
= {
1889 .g_skip_frames
= ov8858_g_skip_frames
,
1892 static const struct v4l2_ctrl_ops ctrl_ops
= {
1893 .s_ctrl
= ov8858_s_ctrl
,
1894 .g_volatile_ctrl
= ov8858_g_ctrl
,
1897 static 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
,
1903 static const struct v4l2_subdev_core_ops ov8858_core_ops
= {
1904 .s_power
= ov8858_s_power
,
1905 .ioctl
= ov8858_ioctl
,
1906 .init
= ov8858_init
,
1909 static 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
,
1916 static 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
,
1923 static const struct media_entity_operations ov_entity_ops
= {
1927 static int ov8858_remove(struct i2c_client
*client
)
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();
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
);
1943 static const char * const ctrl_run_mode_menu
[] = {
1947 "Continuous capture",
1951 static const struct v4l2_ctrl_config ctrl_run_mode
= {
1953 .id
= V4L2_CID_RUN_MODE
,
1955 .type
= V4L2_CTRL_TYPE_MENU
,
1959 .qmenu
= ctrl_run_mode_menu
,
1962 static const struct v4l2_ctrl_config ctrls
[] = {
1965 .id
= V4L2_CID_VFLIP
,
1966 .name
= "Vertical flip",
1967 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1973 .id
= V4L2_CID_HFLIP
,
1974 .name
= "Horizontal flip",
1975 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1981 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
1982 .name
= "Absolute exposure",
1983 .type
= V4L2_CTRL_TYPE_INTEGER
,
1988 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_VOLATILE
,
1991 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
1992 .name
= "Focus absolute",
1993 .type
= V4L2_CTRL_TYPE_INTEGER
,
1995 .max
= OV8858_MAX_FOCUS_POS
,
1997 /* This one is junk: see the spec for proper use of this CID. */
1999 .id
= V4L2_CID_FOCUS_STATUS
,
2000 .name
= "Focus status",
2001 .type
= V4L2_CTRL_TYPE_INTEGER
,
2004 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_VOLATILE
,
2006 /* This is crap. For compatibility use only. */
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
,
2016 .def
= (OV8858_FOCAL_LENGTH_NUM
<< 16) |
2017 OV8858_FOCAL_LENGTH_DEM
,
2018 .flags
= V4L2_CTRL_FLAG_READ_ONLY
,
2020 /* This one is crap, too. For compatibility use only. */
2022 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
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
,
2030 .def
= (OV8858_F_NUMBER_DEFAULT_NUM
<< 16) |
2031 OV8858_F_NUMBER_DEM
,
2032 .flags
= V4L2_CTRL_FLAG_READ_ONLY
,
2035 * The most utter crap. _Never_ use this, even for
2036 * compatibility reasons!
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
,
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
,
2058 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
2059 .name
= "Horizontal binning factor",
2060 .type
= V4L2_CTRL_TYPE_INTEGER
,
2061 .max
= OV8858_BIN_FACTOR_MAX
,
2063 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_VOLATILE
,
2066 .id
= V4L2_CID_BIN_FACTOR_VERT
,
2067 .name
= "Vertical binning factor",
2068 .type
= V4L2_CTRL_TYPE_INTEGER
,
2069 .max
= OV8858_BIN_FACTOR_MAX
,
2071 .flags
= V4L2_CTRL_FLAG_READ_ONLY
| V4L2_CTRL_FLAG_VOLATILE
,
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
,
2083 static int ov8858_probe(struct i2c_client
*client
,
2084 const struct i2c_device_id
*id
)
2086 struct ov8858_device
*dev
;
2089 struct camera_sensor_platform_data
*pdata
;
2091 dev_dbg(&client
->dev
, "%s:\n", __func__
);
2093 /* allocate sensor device & init sub device */
2094 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2098 mutex_init(&dev
->input_lock
);
2101 dev
->i2c_id
= id
->driver_data
;
2103 dev
->sensor_id
= OV_ID_DEFAULT
;
2104 dev
->vcm_driver
= &ov8858_vcms
[OV8858_ID_DEFAULT
];
2106 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &ov8858_ops
);
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
);
2113 dev_err(&client
->dev
,
2114 "%s: failed to get acpi platform data\n",
2118 ret
= ov8858_s_config(&dev
->sd
, client
->irq
, pdata
);
2120 dev_err(&client
->dev
,
2121 "%s: failed to set config\n", __func__
);
2124 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
2126 dev_err(&client
->dev
,
2127 "%s: failed to register subdev\n", __func__
);
2132 * sd->name is updated with sensor driver name by the v4l2.
2133 * change it to sensor name in this case.
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
);
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
;
2145 ret
= v4l2_ctrl_handler_init(&dev
->ctrl_handler
, ARRAY_SIZE(ctrls
) + 1);
2147 ov8858_remove(client
);
2151 dev
->run_mode
= v4l2_ctrl_new_custom(&dev
->ctrl_handler
,
2152 &ctrl_run_mode
, NULL
);
2154 for (i
= 0; i
< ARRAY_SIZE(ctrls
); i
++)
2155 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &ctrls
[i
], NULL
);
2157 if (dev
->ctrl_handler
.error
) {
2158 ov8858_remove(client
);
2159 return dev
->ctrl_handler
.error
;
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
);
2167 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
2169 ov8858_remove(client
);
2176 v4l2_device_unregister_subdev(&dev
->sd
);
2181 static const struct i2c_device_id ov8858_id
[] = {
2186 MODULE_DEVICE_TABLE(i2c
, ov8858_id
);
2188 static const struct acpi_device_id ov8858_acpi_match
[] = {
2193 static struct i2c_driver ov8858_driver
= {
2195 .name
= OV8858_NAME
,
2196 .acpi_match_table
= ACPI_PTR(ov8858_acpi_match
),
2198 .probe
= ov8858_probe
,
2199 .remove
= ov8858_remove
,
2200 .id_table
= ov8858_id
,
2202 module_i2c_driver(ov8858_driver
);
2204 MODULE_DESCRIPTION("A low-level driver for Omnivision OV8858 sensors");
2205 MODULE_LICENSE("GPL");