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) {
483 dev_err(&client->dev, "%s: can't allocate memory",
489 /* Streaming has to be on */
490 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_STREAM_MODE,
495 /* Turn off Dead Pixel Correction */
496 r = ov8858_read_reg(client, OV8858_8BIT,
497 OV8858_OTP_ISP_CTRL2, &isp_ctrl2);
501 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
502 isp_ctrl2 & ~OV8858_OTP_DPC_ENABLE);
506 /* Enable partial OTP read mode */
507 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_MODE_CTRL,
508 OV8858_OTP_MODE_PROGRAM_DISABLE |
509 OV8858_OTP_MODE_MANUAL);
513 /* Set address range of OTP memory to read */
514 r = ov8858_write_reg(client, OV8858_16BIT,
515 OV8858_OTP_START_ADDR_REG,
516 OV8858_OTP_START_ADDR);
520 r = ov8858_write_reg(client, OV8858_16BIT,
521 OV8858_OTP_END_ADDR_REG,
522 OV8858_OTP_END_ADDR);
526 /* Load the OTP data into the OTP buffer */
527 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_LOAD_CTRL,
528 OV8858_OTP_LOAD_ENABLE);
532 /* Wait for the data to load into the buffer */
533 usleep_range(5000, 5500);
535 /* Read the OTP data from the buffer */
536 r = ov8858_i2c_read(client, size, OV8858_OTP_START_ADDR,
541 /* Turn on Dead Pixel Correction */
542 r = ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
543 isp_ctrl2 | OV8858_OTP_DPC_ENABLE);
548 r = ov8858_write_reg(client, 1, OV8858_STREAM_MODE, 0x00);
550 dev_err(&client->dev, "%s: cannot turn off streaming\n",
560 /* Turn on Dead Pixel Correction and set streaming off */
561 ov8858_write_reg(client, OV8858_8BIT, OV8858_OTP_ISP_CTRL2,
562 isp_ctrl2 | OV8858_OTP_DPC_ENABLE);
563 ov8858_write_reg(client, 1, OV8858_STREAM_MODE, 0x00);
565 devm_kfree(&client->dev, dev->otp_data);
566 dev->otp_data = NULL;
568 dev_err(&client->dev, "%s: OTP reading failed\n", __func__);
572 static int ov8858_g_priv_int_data(struct v4l2_subdev *sd,
573 struct v4l2_private_int_data *priv)
575 struct ov8858_device *dev = to_ov8858_sensor(sd);
576 struct i2c_client *client = v4l2_get_subdevdata(sd);
577 u32 size = OV8858_OTP_END_ADDR - OV8858_OTP_START_ADDR + 1;
580 mutex_lock(&dev->input_lock);
582 if (!dev->otp_data) {
583 dev_err(&client->dev, "%s: otp data is NULL\n", __func__);
584 mutex_unlock(&dev->input_lock);
588 if (copy_to_user(priv->data, dev->otp_data,
589 min_t(__u32, priv->size, size))) {
591 dev_err(&client->dev, "%s: OTP reading failed\n", __func__);
592 mutex_unlock(&dev->input_lock);
597 mutex_unlock(&dev->input_lock);
602 static int __ov8858_init(struct v4l2_subdev *sd)
604 struct i2c_client *client = v4l2_get_subdevdata(sd);
605 struct ov8858_device *dev = to_ov8858_sensor(sd);
607 dev_dbg(&client->dev, "%s\n", __func__);
609 /* Sets the default FPS */
612 /* Set default exposure values (initially start values) */
615 dev->digital_gain = 1024;
616 dev->limit_exposure_flag = false;
618 dev_dbg(&client->dev, "%s: Writing basic settings to ov8858\n",
620 ret = ov8858_write_reg_array(client, ov8858_BasicSettings);
624 return ov8858_priv_int_data_init(sd);
627 static int ov8858_init(struct v4l2_subdev *sd, u32 val)
629 struct ov8858_device *dev = to_ov8858_sensor(sd);
632 mutex_lock(&dev->input_lock);
633 ret = __ov8858_init(sd);
634 mutex_unlock(&dev->input_lock);
639 static void ov8858_uninit(struct v4l2_subdev *sd)
641 struct i2c_client *client = v4l2_get_subdevdata(sd);
642 struct ov8858_device *dev = to_ov8858_sensor(sd);
643 struct v4l2_ctrl *ctrl;
644 dev_dbg(&client->dev, "%s:\n", __func__);
648 dev->digital_gain = 0;
649 dev->limit_exposure_flag = false;
650 mutex_unlock(&dev->input_lock);
651 ctrl = v4l2_ctrl_find(sd->ctrl_handler,
652 V4L2_CID_EXPOSURE_AUTO_PRIORITY);
654 v4l2_ctrl_s_ctrl(ctrl, V4L2_EXPOSURE_AUTO);
655 mutex_lock(&dev->input_lock);
658 static int ov8858_g_comp_delay(struct v4l2_subdev *sd, unsigned int *usec)
660 struct i2c_client *client = v4l2_get_subdevdata(sd);
661 struct ov8858_device *dev = to_ov8858_sensor(sd);
662 int ret = 0, exposure;
665 if (dev->exposure == 0) {
666 ret = ov8858_read_reg(client, OV8858_16BIT,
667 OV8858_LONG_EXPO + 1, &data);
673 exposure = dev->exposure;
676 ret = ov8858_read_reg(client, OV8858_16BIT, OV8858_TIMING_VTS, &vts);
678 vts = OV8858_DEPTH_VTS_CONST;
680 *usec = (exposure * 33333 / vts);
681 if (*usec > OV8858_DEPTH_COMP_CONST)
682 *usec = *usec - OV8858_DEPTH_COMP_CONST;
684 *usec = OV8858_DEPTH_COMP_CONST;
689 static long ov8858_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
691 struct i2c_client *client = v4l2_get_subdevdata(sd);
693 case ATOMISP_IOC_S_EXPOSURE:
694 return ov8858_s_exposure(sd, (struct atomisp_exposure *)arg);
695 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
696 return ov8858_g_priv_int_data(sd, arg);
697 case ATOMISP_IOC_G_DEPTH_SYNC_COMP:
698 return ov8858_g_comp_delay(sd, (unsigned int *)arg);
699 case ATOMISP_IOC_G_UPDATE_EXPOSURE:
700 return ov8858_g_update_exposure(sd,
701 (struct atomisp_update_exposure *)arg);
703 dev_dbg(&client->dev, "Unhandled command 0x%X\n", cmd);
708 static int __power_ctrl(struct v4l2_subdev *sd, bool flag)
711 struct ov8858_device *dev = to_ov8858_sensor(sd);
712 struct i2c_client *client = v4l2_get_subdevdata(sd);
714 if (!dev || !dev->platform_data)
717 /* Non-gmin platforms use the legacy callback */
718 if (dev->platform_data->power_ctrl)
719 return dev->platform_data->power_ctrl(sd, flag);
721 if (dev->platform_data->v1p2_ctrl) {
722 ret = dev->platform_data->v1p2_ctrl(sd, flag);
724 dev_err(&client->dev,
725 "failed to power %s 1.2v power rail\n",
726 flag ? "up" : "down");
731 if (dev->platform_data->v2p8_ctrl) {
732 ret = dev->platform_data->v2p8_ctrl(sd, flag);
734 dev_err(&client->dev,
735 "failed to power %s 2.8v power rail\n",
736 flag ? "up" : "down");
741 if (dev->platform_data->v1p8_ctrl) {
742 ret = dev->platform_data->v1p8_ctrl(sd, flag);
744 dev_err(&client->dev,
745 "failed to power %s 1.8v power rail\n",
746 flag ? "up" : "down");
747 if (dev->platform_data->v2p8_ctrl)
748 dev->platform_data->v2p8_ctrl(sd, 0);
754 msleep(20); /* Wait for power lines to stabilize */
758 static int __gpio_ctrl(struct v4l2_subdev *sd, bool flag)
760 struct i2c_client *client;
761 struct ov8858_device *dev;
766 client = v4l2_get_subdevdata(sd);
767 dev = to_ov8858_sensor(sd);
769 if (!client || !dev || !dev->platform_data)
772 /* Non-gmin platforms use the legacy callback */
773 if (dev->platform_data->gpio_ctrl)
774 return dev->platform_data->gpio_ctrl(sd, flag);
776 if (dev->platform_data->gpio0_ctrl)
777 return dev->platform_data->gpio0_ctrl(sd, flag);
779 dev_err(&client->dev, "failed to find platform gpio callback\n");
784 static int power_up(struct v4l2_subdev *sd)
786 struct i2c_client *client = v4l2_get_subdevdata(sd);
787 struct ov8858_device *dev = to_ov8858_sensor(sd);
789 dev_dbg(&client->dev, "%s\n", __func__);
792 ret = __power_ctrl(sd, 1);
794 dev_err(&client->dev, "power rail on failed %d.\n", ret);
799 ret = dev->platform_data->flisclk_ctrl(sd, 1);
801 dev_err(&client->dev, "flisclk on failed %d\n", ret);
806 ret = __gpio_ctrl(sd, 1);
808 dev_err(&client->dev, "gpio on failed %d\n", ret);
812 /* Minumum delay is 8192 clock cycles before first i2c transaction,
813 * which is 1.37 ms at the lowest allowed clock rate 6 MHz */
814 usleep_range(2000, 2500);
818 dev->platform_data->flisclk_ctrl(sd, 0);
822 dev_err(&client->dev, "Sensor power-up failed\n");
827 static int power_down(struct v4l2_subdev *sd)
829 struct ov8858_device *dev = to_ov8858_sensor(sd);
830 struct i2c_client *client = v4l2_get_subdevdata(sd);
832 dev_dbg(&client->dev, "%s\n", __func__);
834 ret = dev->platform_data->flisclk_ctrl(sd, 0);
836 dev_err(&client->dev, "flisclk off failed\n");
838 ret = __gpio_ctrl(sd, 0);
840 dev_err(&client->dev, "gpio off failed\n");
842 ret = __power_ctrl(sd, 0);
844 dev_err(&client->dev, "power rail off failed.\n");
849 static int __ov8858_s_power(struct v4l2_subdev *sd, int on)
851 struct ov8858_device *dev = to_ov8858_sensor(sd);
856 if (dev->vcm_driver && dev->vcm_driver->power_down)
857 r = dev->vcm_driver->power_down(sd);
858 ret = power_down(sd);
859 if (r != 0 && ret == 0)
865 if (dev->vcm_driver && dev->vcm_driver->power_up) {
866 ret = dev->vcm_driver->power_up(sd);
872 return __ov8858_init(sd);
878 static int ov8858_s_power(struct v4l2_subdev *sd, int on)
881 struct ov8858_device *dev = to_ov8858_sensor(sd);
883 mutex_lock(&dev->input_lock);
884 ret = __ov8858_s_power(sd, on);
885 mutex_unlock(&dev->input_lock);
888 * FIXME: Compatibility with old behaviour: return to preview
889 * when the device is power cycled.
892 v4l2_ctrl_s_ctrl(dev->run_mode, ATOMISP_RUN_MODE_PREVIEW);
898 * Return value of the specified register, first try getting it from
899 * the register list and if not found, get from the sensor via i2c.
901 static int ov8858_get_register(struct v4l2_subdev *sd, int reg, int type,
902 const struct ov8858_reg *reglist)
904 struct i2c_client *client = v4l2_get_subdevdata(sd);
905 const struct ov8858_reg *next;
908 /* Try if the values are in the register list */
909 for (next = reglist; next->type != OV8858_TOK_TERM; next++) {
910 if (next->sreg == reg) {
911 if (type == OV8858_8BIT)
914 if (type == OV8858_16BIT &&
915 next[1].type != OV8858_TOK_TERM)
916 return next[0].val << 8 | next[1].val;
920 /* If not, read from sensor */
921 if (ov8858_read_reg(client, type, reg, &val)) {
922 dev_err(&client->dev, "failed to read register 0x%08x\n", reg);
929 static inline int ov8858_get_register_16bit(struct v4l2_subdev *sd, int reg,
930 const struct ov8858_reg *reglist)
932 return ov8858_get_register(sd, reg, OV8858_16BIT, reglist);
935 static inline int ov8858_get_register_8bit(struct v4l2_subdev *sd, int reg,
936 const struct ov8858_reg *reglist)
938 return ov8858_get_register(sd, reg, OV8858_8BIT, reglist);
941 static int __ov8858_get_pll1_values(struct v4l2_subdev *sd,
943 const struct ov8858_reg *reglist)
945 struct i2c_client *client = v4l2_get_subdevdata(sd);
946 unsigned int prediv_idx;
947 unsigned int multiplier;
948 unsigned int sys_prediv;
949 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
952 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV0, reglist);
956 if (ret & OV8858_PLL1_PREDIV0_MASK)
959 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_PREDIV, reglist);
964 prediv_idx = ret & OV8858_PLL1_PREDIV_MASK;
965 *value = *value * 2 / prediv_coef[prediv_idx];
967 ret = ov8858_get_register_16bit(sd, OV8858_PLL1_MULTIPLIER, reglist);
972 *value *= multiplier & OV8858_PLL1_MULTIPLIER_MASK;
973 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_PRE_DIV, reglist);
978 sys_prediv = ret & OV8858_PLL1_SYS_PRE_DIV_MASK;
979 *value /= (sys_prediv + 3);
980 ret = ov8858_get_register_8bit(sd, OV8858_PLL1_SYS_DIVIDER, reglist);
985 if (ret & OV8858_PLL1_SYS_DIVIDER_MASK)
988 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
993 static int __ov8858_get_pll2a_values(struct v4l2_subdev *sd, int *value,
994 const struct ov8858_reg *reglist)
996 struct i2c_client *client = v4l2_get_subdevdata(sd);
997 unsigned int prediv_idx;
998 unsigned int multiplier;
999 unsigned int prediv_coef[] = {2, 3, 4, 5, 6, 8, 12, 16};
1002 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV0, reglist);
1006 if (ret & OV8858_PLL2_PREDIV0_MASK)
1009 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_PREDIV, reglist);
1013 prediv_idx = (ret & OV8858_PLL2_PREDIV_MASK);
1014 *value = *value * 2 / prediv_coef[prediv_idx];
1016 ret = ov8858_get_register_16bit(sd, OV8858_PLL2_MULTIPLIER, reglist);
1021 *value *= multiplier & OV8858_PLL2_MULTIPLIER_MASK;
1022 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1026 static int __ov8858_get_pll2b_values(struct v4l2_subdev *sd, int *value,
1027 const struct ov8858_reg *reglist)
1029 struct i2c_client *client = v4l2_get_subdevdata(sd);
1030 unsigned int dac_divider;
1033 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_DAC_DIVIDER, reglist);
1037 dac_divider = (ret & OV8858_PLL2_DAC_DIVIDER_MASK) + 1;
1038 *value /= dac_divider;
1040 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1044 static int __ov8858_get_pll2c_values(struct v4l2_subdev *sd, int *value,
1045 const struct ov8858_reg *reglist)
1047 struct i2c_client *client = v4l2_get_subdevdata(sd);
1048 unsigned int sys_pre_div;
1049 unsigned int sys_divider_idx;
1050 unsigned int sys_divider_coef[] = {2, 3, 4, 5, 6, 7, 8, 10};
1053 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_PRE_DIV, reglist);
1057 sys_pre_div = (ret & OV8858_PLL2_SYS_PRE_DIV_MASK) + 1;
1058 *value /= sys_pre_div;
1060 ret = ov8858_get_register_8bit(sd, OV8858_PLL2_SYS_DIVIDER, reglist);
1064 sys_divider_idx = ret & OV8858_PLL2_SYS_DIVIDER_MASK;
1065 *value *= 2 / sys_divider_coef[sys_divider_idx];
1067 dev_dbg(&client->dev, "%s: *value: %d\n", __func__, *value);
1072 static int ov8858_get_intg_factor(struct v4l2_subdev *sd,
1073 struct camera_mipi_info *info,
1074 const struct ov8858_reg *reglist)
1076 const unsigned int ext_clk = 19200000; /* Hz */
1077 struct atomisp_sensor_mode_data *m = &info->data;
1078 struct ov8858_device *dev = to_ov8858_sensor(sd);
1079 struct i2c_client *client = v4l2_get_subdevdata(sd);
1080 struct device *d = &client->dev;
1081 const struct ov8858_resolution *res =
1082 &dev->curr_res_table[dev->fmt_idx];
1083 unsigned int pll_sclksel1;
1084 unsigned int pll_sclksel2;
1085 unsigned int sys_pre_div;
1086 unsigned int sclk_pdiv;
1087 unsigned int sclk = ext_clk;
1091 memset(&info->data, 0, sizeof(info->data));
1093 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL1, reglist);
1097 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL1: 0x%02x\n", __func__, ret);
1098 pll_sclksel1 = ret & OV8858_PLL_SCLKSEL1_MASK;
1100 ret = ov8858_get_register_8bit(sd, OV8858_PLL_SCLKSEL2, reglist);
1104 dev_dbg(d, "%s: OV8858_PLL_SCLKSEL2: 0x%02x\n", __func__, ret);
1105 pll_sclksel2 = ret & OV8858_PLL_SCLKSEL2_MASK;
1108 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1111 ret = __ov8858_get_pll2b_values(sd, &sclk, reglist);
1114 } else if (pll_sclksel1) {
1115 ret = __ov8858_get_pll2a_values(sd, &sclk, reglist);
1118 ret = __ov8858_get_pll2c_values(sd, &sclk, reglist);
1122 ret = __ov8858_get_pll1_values(sd, &sclk, reglist);
1127 ret = ov8858_get_register_8bit(sd, OV8858_SRB_HOST_INPUT_DIS, reglist);
1131 dev_dbg(d, "%s: OV8858_SRB_HOST_INPUT_DIS: 0x%02x\n", __func__, ret);
1133 sys_pre_div = ret & OV8858_SYS_PRE_DIV_MASK;
1134 sys_pre_div >>= OV8858_SYS_PRE_DIV_OFFSET;
1136 if (sys_pre_div == 1)
1138 else if (sys_pre_div == 2)
1141 sclk_pdiv = ret & OV8858_SCLK_PDIV_MASK;
1142 sclk_pdiv >>= OV8858_SCLK_PDIV_OFFSET;
1147 dev_dbg(d, "%s: sclk: %d\n", __func__, sclk);
1149 dev->vt_pix_clk_freq_mhz = sclk;
1150 m->vt_pix_clk_freq_mhz = sclk;
1153 m->frame_length_lines =
1154 res->fps_options[dev->fps_index].lines_per_frame;
1155 m->line_length_pck = res->fps_options[dev->fps_index].pixels_per_line;
1157 m->coarse_integration_time_min = 0;
1158 m->coarse_integration_time_max_margin = OV8858_INTEGRATION_TIME_MARGIN;
1159 ret = ov8858_read_reg(client, OV8858_16BIT, OV8858_TIMING_HTS, &hts);
1163 dev_dbg(&client->dev, "%s: get HTS %d\n", __func__, hts);
1165 /* OV Sensor do not use fine integration time. */
1166 m->fine_integration_time_min = 0;
1167 m->fine_integration_time_max_margin = 0;
1170 * read_mode indicate whether binning is used for calculating
1171 * the correct exposure value from the user side. So adapt the
1172 * read mode values accordingly.
1174 m->read_mode = res->bin_factor_x ?
1175 OV8858_READ_MODE_BINNING_ON : OV8858_READ_MODE_BINNING_OFF;
1177 ret = ov8858_get_register_8bit(sd, OV8858_H_INC_ODD, res->regs);
1180 m->binning_factor_x = (ret + 1) / 2;
1182 ret = ov8858_get_register_8bit(sd, OV8858_V_INC_ODD, res->regs);
1185 m->binning_factor_y = (ret + 1) / 2;
1187 /* Get the cropping and output resolution to ISP for this mode. */
1188 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_START_H,
1193 m->crop_horizontal_start = ret;
1195 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_START_H, res->regs);
1199 m->crop_vertical_start = ret;
1201 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_END_H, res->regs);
1205 m->crop_horizontal_end = ret;
1207 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_END_H, res->regs);
1211 m->crop_vertical_end = ret;
1213 ret = ov8858_get_register_16bit(sd, OV8858_HORIZONTAL_OUTPUT_SIZE_H,
1218 m->output_width = ret;
1220 ret = ov8858_get_register_16bit(sd, OV8858_VERTICAL_OUTPUT_SIZE_H,
1225 m->output_height = ret;
1231 * distance - calculate the distance
1236 * Get the gap between res_w/res_h and w/h.
1237 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1238 * res->width/height smaller than w/h wouldn't be considered.
1239 * The gap of ratio larger than 1/8 wouldn't be considered.
1240 * Returns the value of gap or -1 if fail.
1242 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1243 static int distance(struct ov8858_resolution const *res, const u32 w,
1249 if (w == 0 || h == 0 ||
1250 res->width < w || res->height < h)
1253 ratio = res->width << 13;
1256 ratio /= res->height;
1258 distance = abs(ratio - 8192);
1260 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1266 * Returns the nearest higher resolution index.
1269 * matching is done based on enveloping resolution and
1270 * aspect ratio. If the aspect ratio cannot be matched
1271 * to any index, -1 is returned.
1273 static int nearest_resolution_index(struct v4l2_subdev *sd, int w, int h)
1279 int min_fps_diff = INT_MAX;
1280 int min_dist = INT_MAX;
1281 int min_res_w = INT_MAX;
1282 const struct ov8858_resolution *tmp_res = NULL;
1283 struct i2c_client *client = v4l2_get_subdevdata(sd);
1284 struct ov8858_device *dev = to_ov8858_sensor(sd);
1285 dev_dbg(&client->dev, "%s: w=%d, h=%d\n", __func__, w, h);
1287 for (i = 0; i < dev->entries_curr_table; i++) {
1288 tmp_res = &dev->curr_res_table[i];
1289 dist = distance(tmp_res, w, h);
1290 dev_dbg(&client->dev,
1291 "%s[%d]: %dx%d distance=%d\n", tmp_res->desc,
1292 i, tmp_res->width, tmp_res->height, dist);
1295 if (dist < min_dist) {
1297 min_res_w = tmp_res->width;
1298 min_fps_diff = __ov8858_min_fps_diff(dev->fps,
1299 tmp_res->fps_options);
1302 if (dist == min_dist) {
1303 fps_diff = __ov8858_min_fps_diff(dev->fps,
1304 tmp_res->fps_options);
1305 if (fps_diff < min_fps_diff) {
1306 min_fps_diff = fps_diff;
1309 if (tmp_res->width < min_res_w) {
1310 min_res_w = tmp_res->width;
1319 static int ov8858_set_fmt(struct v4l2_subdev *sd,
1320 struct v4l2_subdev_pad_config *cfg,
1321 struct v4l2_subdev_format *format)
1323 struct v4l2_mbus_framefmt *fmt = &format->format;
1324 struct ov8858_device *dev = to_ov8858_sensor(sd);
1325 struct camera_mipi_info *ov8858_info = NULL;
1326 struct i2c_client *client = v4l2_get_subdevdata(sd);
1327 const struct ov8858_resolution *res;
1335 ov8858_info = v4l2_get_subdev_hostdata(sd);
1336 if (ov8858_info == NULL)
1339 mutex_lock(&dev->input_lock);
1341 if ((fmt->width > OV8858_RES_WIDTH_MAX) ||
1342 (fmt->height > OV8858_RES_HEIGHT_MAX)) {
1343 fmt->width = OV8858_RES_WIDTH_MAX;
1344 fmt->height = OV8858_RES_HEIGHT_MAX;
1346 idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1349 * nearest_resolution_index() doesn't return smaller
1350 * resolutions. If it fails, it means the requested resolution
1351 * is higher than we can support. Fallback to highest possible
1352 * resolution in this case.
1355 idx = dev->entries_curr_table - 1;
1357 fmt->width = dev->curr_res_table[idx].width;
1358 fmt->height = dev->curr_res_table[idx].height;
1361 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1362 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1363 cfg->try_fmt = *fmt;
1364 mutex_unlock(&dev->input_lock);
1368 dev->fmt_idx = nearest_resolution_index(sd, fmt->width, fmt->height);
1369 if (dev->fmt_idx == -1) {
1373 res = &dev->curr_res_table[dev->fmt_idx];
1374 dev_dbg(&client->dev, "%s: selected width = %d, height = %d\n",
1375 __func__, res->width, res->height);
1377 /* Adjust the FPS selection based on the resolution selected */
1378 dev->fps_index = __ov8858_nearest_fps_index(dev->fps, res->fps_options);
1379 dev->fps = res->fps_options[dev->fps_index].fps;
1380 dev->regs = res->fps_options[dev->fps_index].regs;
1382 dev->regs = res->regs;
1384 ret = ov8858_write_reg_array(client, dev->regs);
1388 dev->pixels_per_line = res->fps_options[dev->fps_index].pixels_per_line;
1389 dev->lines_per_frame = res->fps_options[dev->fps_index].lines_per_frame;
1391 /* ov8858 only support RGB RAW10 output */
1392 ov8858_info->metadata_width = res->width * 10 / 8;
1393 ov8858_info->metadata_height = 2;
1394 ov8858_info->metadata_format = ATOMISP_INPUT_FORMAT_EMBEDDED;
1396 /* Set the initial exposure */
1397 ret = __ov8858_set_exposure(sd, dev->exposure, dev->gain,
1398 dev->digital_gain, &dev->pixels_per_line,
1399 &dev->lines_per_frame);
1403 ret = ov8858_get_intg_factor(sd, ov8858_info, dev->regs);
1406 mutex_unlock(&dev->input_lock);
1411 static int ov8858_get_fmt(struct v4l2_subdev *sd,
1412 struct v4l2_subdev_pad_config *cfg,
1413 struct v4l2_subdev_format *format)
1415 struct v4l2_mbus_framefmt *fmt = &format->format;
1416 struct ov8858_device *dev = to_ov8858_sensor(sd);
1423 mutex_lock(&dev->input_lock);
1424 fmt->width = dev->curr_res_table[dev->fmt_idx].width;
1425 fmt->height = dev->curr_res_table[dev->fmt_idx].height;
1426 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1427 mutex_unlock(&dev->input_lock);
1432 static int ov8858_detect(struct i2c_client *client, u16 *id)
1434 struct i2c_adapter *adapter = client->adapter;
1440 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1443 dev_dbg(&client->dev, "%s: I2C functionality ok\n", __func__);
1444 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_HIGH, &id_hi);
1447 dev_dbg(&client->dev, "%s: id_high = 0x%04x\n", __func__, id_hi);
1448 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_CHIP_ID_LOW, &id_low);
1451 dev_dbg(&client->dev, "%s: id_low = 0x%04x\n", __func__, id_low);
1452 *id = (id_hi << 8) | id_low;
1454 dev_dbg(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1456 dev_info(&client->dev, "%s: chip_id = 0x%04x\n", __func__, *id);
1457 if (*id != OV8858_CHIP_ID)
1460 /* Stream off now. */
1461 return ov8858_write_reg(client, OV8858_8BIT, OV8858_STREAM_MODE, 0);
1464 static void __ov8858_print_timing(struct v4l2_subdev *sd)
1466 struct ov8858_device *dev = to_ov8858_sensor(sd);
1467 struct i2c_client *client = v4l2_get_subdevdata(sd);
1468 u16 width = dev->curr_res_table[dev->fmt_idx].width;
1469 u16 height = dev->curr_res_table[dev->fmt_idx].height;
1471 dev_dbg(&client->dev, "Dump ov8858 timing in stream on:\n");
1472 dev_dbg(&client->dev, "width: %d:\n", width);
1473 dev_dbg(&client->dev, "height: %d:\n", height);
1474 dev_dbg(&client->dev, "pixels_per_line: %d:\n", dev->pixels_per_line);
1475 dev_dbg(&client->dev, "line per frame: %d:\n", dev->lines_per_frame);
1476 dev_dbg(&client->dev, "pix freq: %d:\n", dev->vt_pix_clk_freq_mhz);
1477 /* updated formula: pixels_per_line = 2 * HTS */
1478 /* updated formula: fps = SCLK / (VTS * HTS) */
1479 dev_dbg(&client->dev, "init fps: %d:\n", dev->vt_pix_clk_freq_mhz /
1480 (dev->pixels_per_line / 2) / dev->lines_per_frame);
1481 dev_dbg(&client->dev, "HBlank: %d nS:\n",
1482 1000 * (dev->pixels_per_line - width) /
1483 (dev->vt_pix_clk_freq_mhz / 1000000));
1484 dev_dbg(&client->dev, "VBlank: %d uS:\n",
1485 (dev->lines_per_frame - height) * dev->pixels_per_line /
1486 (dev->vt_pix_clk_freq_mhz / 1000000));
1490 * ov8858 stream on/off
1492 static int ov8858_s_stream(struct v4l2_subdev *sd, int enable)
1494 struct ov8858_device *dev = to_ov8858_sensor(sd);
1495 struct i2c_client *client = v4l2_get_subdevdata(sd);
1498 dev_dbg(&client->dev, "%s: enable = %d\n", __func__, enable);
1500 /* Set orientation */
1501 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT2, &val);
1505 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT2,
1506 dev->hflip ? val | OV8858_FLIP_ENABLE :
1507 val & ~OV8858_FLIP_ENABLE);
1511 ret = ov8858_read_reg(client, OV8858_8BIT, OV8858_FORMAT1, &val);
1515 ret = ov8858_write_reg(client, OV8858_8BIT, OV8858_FORMAT1,
1516 dev->vflip ? val | OV8858_FLIP_ENABLE :
1517 val & ~OV8858_FLIP_ENABLE);
1521 mutex_lock(&dev->input_lock);
1523 __ov8858_print_timing(sd);
1524 ret = ov8858_write_reg_array(client, ov8858_streaming);
1526 dev_err(&client->dev, "write_reg_array err\n");
1531 ret = ov8858_write_reg_array(client, ov8858_soft_standby);
1533 dev_err(&client->dev, "write_reg_array err\n");
1541 mutex_unlock(&dev->input_lock);
1545 static int __update_ov8858_device_settings(struct ov8858_device *dev,
1548 if (sensor_id == OV8858_CHIP_ID)
1549 #ifdef CONFIG_PLATFORM_BTNS
1550 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
1552 dev->vcm_driver = &ov8858_vcms[OV8858_SUNNY];
1557 if (dev->vcm_driver && dev->vcm_driver->init)
1558 return dev->vcm_driver->init(&dev->sd);
1563 static int ov8858_s_config(struct v4l2_subdev *sd,
1564 int irq, void *pdata)
1566 struct ov8858_device *dev = to_ov8858_sensor(sd);
1567 struct i2c_client *client = v4l2_get_subdevdata(sd);
1574 dev->platform_data = pdata;
1576 mutex_lock(&dev->input_lock);
1578 if (dev->platform_data->platform_init) {
1579 ret = dev->platform_data->platform_init(client);
1581 mutex_unlock(&dev->input_lock);
1582 dev_err(&client->dev, "platform init error %d!\n", ret);
1587 ret = __ov8858_s_power(sd, 1);
1589 dev_err(&client->dev, "power-up error %d!\n", ret);
1590 mutex_unlock(&dev->input_lock);
1594 ret = dev->platform_data->csi_cfg(sd, 1);
1598 /* config & detect sensor */
1599 ret = ov8858_detect(client, &sensor_id);
1601 dev_err(&client->dev, "detect error %d!\n", ret);
1605 dev->sensor_id = sensor_id;
1607 /* power off sensor */
1608 ret = __ov8858_s_power(sd, 0);
1610 dev->platform_data->csi_cfg(sd, 0);
1611 dev_err(&client->dev, "__ov8858_s_power-down error %d!\n", ret);
1615 /* Resolution settings depend on sensor type and platform */
1616 ret = __update_ov8858_device_settings(dev, dev->sensor_id);
1618 dev->platform_data->csi_cfg(sd, 0);
1619 dev_err(&client->dev, "__update_ov8858_device_settings error %d!\n", ret);
1623 mutex_unlock(&dev->input_lock);
1627 dev->platform_data->csi_cfg(sd, 0);
1629 __ov8858_s_power(sd, 0);
1631 if (dev->platform_data->platform_deinit)
1632 dev->platform_data->platform_deinit();
1633 mutex_unlock(&dev->input_lock);
1634 dev_err(&client->dev, "sensor power-gating failed\n");
1639 ov8858_enum_mbus_code(struct v4l2_subdev *sd,
1640 struct v4l2_subdev_pad_config *cfg,
1641 struct v4l2_subdev_mbus_code_enum *code)
1645 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1651 ov8858_enum_frame_size(struct v4l2_subdev *sd,
1652 struct v4l2_subdev_pad_config *cfg,
1653 struct v4l2_subdev_frame_size_enum *fse)
1655 int index = fse->index;
1656 struct ov8858_device *dev = to_ov8858_sensor(sd);
1658 mutex_lock(&dev->input_lock);
1659 if (index >= dev->entries_curr_table) {
1660 mutex_unlock(&dev->input_lock);
1664 fse->min_width = dev->curr_res_table[index].width;
1665 fse->min_height = dev->curr_res_table[index].height;
1666 fse->max_width = dev->curr_res_table[index].width;
1667 fse->max_height = dev->curr_res_table[index].height;
1668 mutex_unlock(&dev->input_lock);
1673 static int ov8858_s_ctrl(struct v4l2_ctrl *ctrl)
1675 struct ov8858_device *dev = container_of(
1676 ctrl->handler, struct ov8858_device, ctrl_handler);
1677 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1679 /* input_lock is taken by the control framework, so it
1680 * doesn't need to be taken here.
1684 case V4L2_CID_RUN_MODE:
1685 switch (ctrl->val) {
1686 case ATOMISP_RUN_MODE_VIDEO:
1687 dev->curr_res_table = ov8858_res_video;
1688 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_video);
1690 case ATOMISP_RUN_MODE_STILL_CAPTURE:
1691 dev->curr_res_table = ov8858_res_still;
1692 dev->entries_curr_table = ARRAY_SIZE(ov8858_res_still);
1695 dev->curr_res_table = ov8858_res_preview;
1696 dev->entries_curr_table =
1697 ARRAY_SIZE(ov8858_res_preview);
1704 case V4L2_CID_FOCUS_ABSOLUTE:
1705 if (dev->vcm_driver && dev->vcm_driver->t_focus_abs)
1706 return dev->vcm_driver->t_focus_abs(&dev->sd,
1709 case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
1710 if (ctrl->val == V4L2_EXPOSURE_AUTO)
1711 dev->limit_exposure_flag = false;
1712 else if (ctrl->val == V4L2_EXPOSURE_APERTURE_PRIORITY)
1713 dev->limit_exposure_flag = true;
1715 case V4L2_CID_HFLIP:
1716 dev->hflip = ctrl->val;
1718 case V4L2_CID_VFLIP:
1719 dev->vflip = ctrl->val;
1722 dev_err(&client->dev, "%s: Error: Invalid ctrl: 0x%X\n",
1723 __func__, ctrl->id);
1728 static int ov8858_g_ctrl(struct v4l2_ctrl *ctrl)
1730 struct ov8858_device *dev = container_of(
1731 ctrl->handler, struct ov8858_device, ctrl_handler);
1732 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1734 int i = dev->fmt_idx;
1737 case V4L2_CID_FOCUS_STATUS:
1738 if (dev->vcm_driver && dev->vcm_driver->q_focus_status)
1739 return dev->vcm_driver->q_focus_status(&dev->sd,
1742 case V4L2_CID_BIN_FACTOR_HORZ:
1743 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_ODD,
1744 dev->curr_res_table[i].regs);
1747 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_H_INC_EVEN,
1748 dev->curr_res_table[i].regs);
1751 ctrl->val = fls(r_odd + (r_even)) - 2;
1754 case V4L2_CID_BIN_FACTOR_VERT:
1755 r_odd = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_ODD,
1756 dev->curr_res_table[i].regs);
1759 r_even = ov8858_get_register_8bit(&dev->sd, OV8858_V_INC_EVEN,
1760 dev->curr_res_table[i].regs);
1763 ctrl->val = fls(r_odd + (r_even)) - 2;
1765 case V4L2_CID_HFLIP:
1766 ctrl->val = dev->hflip;
1768 case V4L2_CID_VFLIP:
1769 ctrl->val = dev->vflip;
1771 case V4L2_CID_EXPOSURE_ABSOLUTE:
1772 ctrl->val = dev->exposure;
1775 dev_warn(&client->dev,
1776 "%s: Error: Invalid ctrl: 0x%X\n", __func__, ctrl->id);
1784 ov8858_g_frame_interval(struct v4l2_subdev *sd,
1785 struct v4l2_subdev_frame_interval *interval)
1787 struct ov8858_device *dev = to_ov8858_sensor(sd);
1788 const struct ov8858_resolution *res =
1789 &dev->curr_res_table[dev->fmt_idx];
1791 mutex_lock(&dev->input_lock);
1792 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1793 interval->interval.numerator = 1;
1794 mutex_unlock(&dev->input_lock);
1798 static int __ov8858_s_frame_interval(struct v4l2_subdev *sd,
1799 struct v4l2_subdev_frame_interval *interval)
1801 struct ov8858_device *dev = to_ov8858_sensor(sd);
1802 struct i2c_client *client = v4l2_get_subdevdata(sd);
1803 const struct ov8858_resolution *res =
1804 &dev->curr_res_table[dev->fmt_idx];
1805 struct camera_mipi_info *info = NULL;
1806 unsigned int fps_index;
1810 info = v4l2_get_subdev_hostdata(sd);
1814 if (!interval->interval.numerator)
1815 interval->interval.numerator = 1;
1817 fps = interval->interval.denominator / interval->interval.numerator;
1819 /* No need to proceed further if we are not streaming */
1820 if (!dev->streaming) {
1821 /* Save the new FPS and use it while selecting setting */
1826 /* Ignore if we are already using the required FPS. */
1827 if (fps == res->fps_options[dev->fps_index].fps)
1830 fps_index = __ov8858_nearest_fps_index(fps, res->fps_options);
1832 if (res->fps_options[fps_index].regs &&
1833 res->fps_options[fps_index].regs != dev->regs) {
1834 dev_err(&client->dev,
1835 "Sensor is streaming, can't apply new configuration\n");
1839 dev->fps_index = fps_index;
1840 dev->fps = res->fps_options[dev->fps_index].fps;
1842 /* Update the new frametimings based on FPS */
1843 dev->pixels_per_line =
1844 res->fps_options[dev->fps_index].pixels_per_line;
1845 dev->lines_per_frame =
1846 res->fps_options[dev->fps_index].lines_per_frame;
1848 /* update frametiming. Conside the curren exposure/gain as well */
1849 ret = __ov8858_update_frame_timing(sd,
1850 &dev->pixels_per_line, &dev->lines_per_frame);
1854 /* Update the new values so that user side knows the current settings */
1855 ret = ov8858_get_intg_factor(sd, info, dev->regs);
1859 interval->interval.denominator = res->fps_options[dev->fps_index].fps;
1860 interval->interval.numerator = 1;
1861 __ov8858_print_timing(sd);
1866 static int ov8858_s_frame_interval(struct v4l2_subdev *sd,
1867 struct v4l2_subdev_frame_interval *interval)
1869 struct ov8858_device *dev = to_ov8858_sensor(sd);
1872 mutex_lock(&dev->input_lock);
1873 ret = __ov8858_s_frame_interval(sd, interval);
1874 mutex_unlock(&dev->input_lock);
1879 static int ov8858_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1881 struct ov8858_device *dev = to_ov8858_sensor(sd);
1883 mutex_lock(&dev->input_lock);
1884 *frames = dev->curr_res_table[dev->fmt_idx].skip_frames;
1885 mutex_unlock(&dev->input_lock);
1890 static const struct v4l2_subdev_sensor_ops ov8858_sensor_ops = {
1891 .g_skip_frames = ov8858_g_skip_frames,
1894 static const struct v4l2_ctrl_ops ctrl_ops = {
1895 .s_ctrl = ov8858_s_ctrl,
1896 .g_volatile_ctrl = ov8858_g_ctrl,
1899 static const struct v4l2_subdev_video_ops ov8858_video_ops = {
1900 .s_stream = ov8858_s_stream,
1901 .g_frame_interval = ov8858_g_frame_interval,
1902 .s_frame_interval = ov8858_s_frame_interval,
1905 static const struct v4l2_subdev_core_ops ov8858_core_ops = {
1906 .s_power = ov8858_s_power,
1907 .ioctl = ov8858_ioctl,
1908 .init = ov8858_init,
1911 static const struct v4l2_subdev_pad_ops ov8858_pad_ops = {
1912 .enum_mbus_code = ov8858_enum_mbus_code,
1913 .enum_frame_size = ov8858_enum_frame_size,
1914 .get_fmt = ov8858_get_fmt,
1915 .set_fmt = ov8858_set_fmt,
1918 static const struct v4l2_subdev_ops ov8858_ops = {
1919 .core = &ov8858_core_ops,
1920 .video = &ov8858_video_ops,
1921 .pad = &ov8858_pad_ops,
1922 .sensor = &ov8858_sensor_ops,
1925 static const struct media_entity_operations ov_entity_ops = {
1929 static int ov8858_remove(struct i2c_client *client)
1931 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1932 struct ov8858_device *dev = to_ov8858_sensor(sd);
1933 if (dev->platform_data->platform_deinit)
1934 dev->platform_data->platform_deinit();
1936 media_entity_cleanup(&dev->sd.entity);
1937 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1938 dev->platform_data->csi_cfg(sd, 0);
1939 v4l2_device_unregister_subdev(sd);
1945 static const char * const ctrl_run_mode_menu[] = {
1949 "Continuous capture",
1953 static const struct v4l2_ctrl_config ctrl_run_mode = {
1955 .id = V4L2_CID_RUN_MODE,
1957 .type = V4L2_CTRL_TYPE_MENU,
1961 .qmenu = ctrl_run_mode_menu,
1964 static const struct v4l2_ctrl_config ctrls[] = {
1967 .id = V4L2_CID_VFLIP,
1968 .name = "Vertical flip",
1969 .type = V4L2_CTRL_TYPE_BOOLEAN,
1975 .id = V4L2_CID_HFLIP,
1976 .name = "Horizontal flip",
1977 .type = V4L2_CTRL_TYPE_BOOLEAN,
1983 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1984 .name = "Absolute exposure",
1985 .type = V4L2_CTRL_TYPE_INTEGER,
1990 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
1993 .id = V4L2_CID_FOCUS_ABSOLUTE,
1994 .name = "Focus absolute",
1995 .type = V4L2_CTRL_TYPE_INTEGER,
1997 .max = OV8858_MAX_FOCUS_POS,
1999 /* This one is junk: see the spec for proper use of this CID. */
2001 .id = V4L2_CID_FOCUS_STATUS,
2002 .name = "Focus status",
2003 .type = V4L2_CTRL_TYPE_INTEGER,
2006 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2008 /* This is crap. For compatibility use only. */
2010 .id = V4L2_CID_FOCAL_ABSOLUTE,
2011 .name = "Focal lenght",
2012 .type = V4L2_CTRL_TYPE_INTEGER,
2013 .min = (OV8858_FOCAL_LENGTH_NUM << 16) |
2014 OV8858_FOCAL_LENGTH_DEM,
2015 .max = (OV8858_FOCAL_LENGTH_NUM << 16) |
2016 OV8858_FOCAL_LENGTH_DEM,
2018 .def = (OV8858_FOCAL_LENGTH_NUM << 16) |
2019 OV8858_FOCAL_LENGTH_DEM,
2020 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2022 /* This one is crap, too. For compatibility use only. */
2024 .id = V4L2_CID_FNUMBER_ABSOLUTE,
2026 .type = V4L2_CTRL_TYPE_INTEGER,
2027 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2028 OV8858_F_NUMBER_DEM,
2029 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2030 OV8858_F_NUMBER_DEM,
2032 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 16) |
2033 OV8858_F_NUMBER_DEM,
2034 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2037 * The most utter crap. _Never_ use this, even for
2038 * compatibility reasons!
2041 .id = V4L2_CID_FNUMBER_RANGE,
2042 .name = "F-number range",
2043 .type = V4L2_CTRL_TYPE_INTEGER,
2044 .min = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2045 (OV8858_F_NUMBER_DEM << 16) |
2046 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2047 OV8858_F_NUMBER_DEM,
2048 .max = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2049 (OV8858_F_NUMBER_DEM << 16) |
2050 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2051 OV8858_F_NUMBER_DEM,
2053 .def = (OV8858_F_NUMBER_DEFAULT_NUM << 24) |
2054 (OV8858_F_NUMBER_DEM << 16) |
2055 (OV8858_F_NUMBER_DEFAULT_NUM << 8) |
2056 OV8858_F_NUMBER_DEM,
2057 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2060 .id = V4L2_CID_BIN_FACTOR_HORZ,
2061 .name = "Horizontal binning factor",
2062 .type = V4L2_CTRL_TYPE_INTEGER,
2063 .max = OV8858_BIN_FACTOR_MAX,
2065 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2068 .id = V4L2_CID_BIN_FACTOR_VERT,
2069 .name = "Vertical binning factor",
2070 .type = V4L2_CTRL_TYPE_INTEGER,
2071 .max = OV8858_BIN_FACTOR_MAX,
2073 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
2076 .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
2077 .name = "Exposure auto priority",
2078 .type = V4L2_CTRL_TYPE_INTEGER,
2079 .min = V4L2_EXPOSURE_AUTO,
2080 .max = V4L2_EXPOSURE_APERTURE_PRIORITY,
2085 static int ov8858_probe(struct i2c_client *client,
2086 const struct i2c_device_id *id)
2088 struct ov8858_device *dev;
2091 struct camera_sensor_platform_data *pdata;
2093 dev_dbg(&client->dev, "%s:\n", __func__);
2095 /* allocate sensor device & init sub device */
2096 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2098 dev_err(&client->dev, "%s: out of memory\n", __func__);
2102 mutex_init(&dev->input_lock);
2105 dev->i2c_id = id->driver_data;
2107 dev->sensor_id = OV_ID_DEFAULT;
2108 dev->vcm_driver = &ov8858_vcms[OV8858_ID_DEFAULT];
2110 v4l2_i2c_subdev_init(&(dev->sd), client, &ov8858_ops);
2112 if (ACPI_COMPANION(&client->dev)) {
2113 pdata = gmin_camera_platform_data(&dev->sd,
2114 ATOMISP_INPUT_FORMAT_RAW_10,
2115 atomisp_bayer_order_bggr);
2117 dev_err(&client->dev,
2118 "%s: failed to get acpi platform data\n",
2122 ret = ov8858_s_config(&dev->sd, client->irq, pdata);
2124 dev_err(&client->dev,
2125 "%s: failed to set config\n", __func__);
2128 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
2130 dev_err(&client->dev,
2131 "%s: failed to register subdev\n", __func__);
2136 * sd->name is updated with sensor driver name by the v4l2.
2137 * change it to sensor name in this case.
2139 snprintf(dev->sd.name, sizeof(dev->sd.name), "%s%x %d-%04x",
2140 OV_SUBDEV_PREFIX, dev->sensor_id,
2141 i2c_adapter_id(client->adapter), client->addr);
2143 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2144 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
2145 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
2146 dev->sd.entity.ops = &ov_entity_ops;
2147 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2149 ret = v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ctrls) + 1);
2151 ov8858_remove(client);
2155 dev->run_mode = v4l2_ctrl_new_custom(&dev->ctrl_handler,
2156 &ctrl_run_mode, NULL);
2158 for (i = 0; i < ARRAY_SIZE(ctrls); i++)
2159 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ctrls[i], NULL);
2161 if (dev->ctrl_handler.error) {
2162 ov8858_remove(client);
2163 return dev->ctrl_handler.error;
2166 /* Use same lock for controls as for everything else. */
2167 dev->ctrl_handler.lock = &dev->input_lock;
2168 dev->sd.ctrl_handler = &dev->ctrl_handler;
2169 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2171 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
2173 ov8858_remove(client);
2180 v4l2_device_unregister_subdev(&dev->sd);
2185 static const struct i2c_device_id ov8858_id[] = {
2190 MODULE_DEVICE_TABLE(i2c, ov8858_id);
2192 static const struct acpi_device_id ov8858_acpi_match[] = {
2197 static struct i2c_driver ov8858_driver = {
2199 .name = OV8858_NAME,
2200 .acpi_match_table = ACPI_PTR(ov8858_acpi_match),
2202 .probe = ov8858_probe,
2203 .remove = ov8858_remove,
2204 .id_table = ov8858_id,
2207 static __init int ov8858_init_mod(void)
2209 return i2c_add_driver(&ov8858_driver);
2212 static __exit void ov8858_exit_mod(void)
2214 i2c_del_driver(&ov8858_driver);
2217 module_init(ov8858_init_mod);
2218 module_exit(ov8858_exit_mod);
2220 MODULE_DESCRIPTION("A low-level driver for Omnivision OV8858 sensors");
2221 MODULE_LICENSE("GPL");