1 // SPDX-License-Identifier: GPL-2.0
3 * Support for OmniVision OV2680 1080p HD camera sensor.
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <asm/unaligned.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmod.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/moduleparam.h>
33 #include <media/ov_16bit_addr_reg_helpers.h>
34 #include <media/v4l2-device.h>
36 #include <linux/acpi.h>
37 #include "../include/linux/atomisp_gmin_platform.h"
41 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
42 atomisp_bayer_order_bggr,
43 atomisp_bayer_order_grbg,
44 atomisp_bayer_order_gbrg,
45 atomisp_bayer_order_rggb,
48 static int ov2680_write_reg_array(struct i2c_client *client,
49 const struct ov2680_reg *reglist)
51 const struct ov2680_reg *next = reglist;
54 for (; next->reg != 0; next++) {
55 ret = ov_write_reg8(client, next->reg, next->val);
63 static void ov2680_set_bayer_order(struct ov2680_device *sensor, struct v4l2_mbus_framefmt *fmt)
65 static const int ov2680_hv_flip_bayer_order[] = {
66 MEDIA_BUS_FMT_SBGGR10_1X10,
67 MEDIA_BUS_FMT_SGRBG10_1X10,
68 MEDIA_BUS_FMT_SGBRG10_1X10,
69 MEDIA_BUS_FMT_SRGGB10_1X10,
71 struct camera_mipi_info *ov2680_info;
74 if (sensor->ctrls.vflip->val)
77 if (sensor->ctrls.hflip->val)
80 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
82 /* TODO atomisp specific custom API, should be removed */
83 ov2680_info = v4l2_get_subdev_hostdata(&sensor->sd);
85 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[hv_flip];
88 static int ov2680_set_vflip(struct ov2680_device *sensor, s32 val)
92 if (sensor->is_streaming)
95 ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
99 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
103 static int ov2680_set_hflip(struct ov2680_device *sensor, s32 val)
107 if (sensor->is_streaming)
110 ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
114 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
118 static int ov2680_exposure_set(struct ov2680_device *sensor, u32 exp)
120 return ov_write_reg24(sensor->client, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
123 static int ov2680_gain_set(struct ov2680_device *sensor, u32 gain)
125 return ov_write_reg16(sensor->client, OV2680_REG_GAIN_PK, gain);
128 static int ov2680_test_pattern_set(struct ov2680_device *sensor, int value)
133 return ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), 0);
135 ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
139 ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
146 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
148 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
149 struct ov2680_device *sensor = to_ov2680_sensor(sd);
152 if (!sensor->power_on) {
153 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
159 ret = ov2680_set_vflip(sensor, ctrl->val);
162 ret = ov2680_set_hflip(sensor, ctrl->val);
164 case V4L2_CID_EXPOSURE:
165 ret = ov2680_exposure_set(sensor, ctrl->val);
168 ret = ov2680_gain_set(sensor, ctrl->val);
170 case V4L2_CID_TEST_PATTERN:
171 ret = ov2680_test_pattern_set(sensor, ctrl->val);
179 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
180 .s_ctrl = ov2680_s_ctrl,
183 static int ov2680_init_registers(struct v4l2_subdev *sd)
185 struct i2c_client *client = v4l2_get_subdevdata(sd);
188 ret = ov_write_reg8(client, OV2680_SW_RESET, 0x01);
189 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
194 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
197 struct ov2680_device *dev = to_ov2680_sensor(sd);
198 struct i2c_client *client = v4l2_get_subdevdata(sd);
200 if (!dev || !dev->platform_data)
203 dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
206 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
207 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
208 usleep_range(10000, 15000);
212 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
213 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
218 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
221 struct ov2680_device *dev = to_ov2680_sensor(sd);
223 if (!dev || !dev->platform_data)
227 * The OV2680 documents only one GPIO input (#XSHUTDN), but
228 * existing integrations often wire two (reset/power_down)
229 * because that is the way other sensors work. There is no
230 * way to tell how it is wired internally, so existing
231 * firmwares expose both and we drive them symmetrically.
234 ret = dev->platform_data->gpio0_ctrl(sd, 1);
235 usleep_range(10000, 15000);
236 /* Ignore return from second gpio, it may not be there */
237 dev->platform_data->gpio1_ctrl(sd, 1);
238 usleep_range(10000, 15000);
240 dev->platform_data->gpio1_ctrl(sd, 0);
241 ret = dev->platform_data->gpio0_ctrl(sd, 0);
246 static int power_up(struct v4l2_subdev *sd)
248 struct ov2680_device *dev = to_ov2680_sensor(sd);
249 struct i2c_client *client = v4l2_get_subdevdata(sd);
252 if (!dev->platform_data) {
253 dev_err(&client->dev,
254 "no camera_sensor_platform_data");
259 return 0; /* Already on */
262 ret = power_ctrl(sd, 1);
266 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
267 usleep_range(5000, 6000);
270 ret = gpio_ctrl(sd, 1);
272 ret = gpio_ctrl(sd, 1);
277 /* flis clock control */
278 ret = dev->platform_data->flisclk_ctrl(sd, 1);
282 /* according to DS, 20ms is needed between PWDN and i2c access */
285 ret = ov2680_init_registers(sd);
287 goto fail_init_registers;
289 dev->power_on = true;
293 dev->platform_data->flisclk_ctrl(sd, 0);
298 dev_err(&client->dev, "sensor power-up failed\n");
303 static int power_down(struct v4l2_subdev *sd)
305 struct ov2680_device *dev = to_ov2680_sensor(sd);
306 struct i2c_client *client = v4l2_get_subdevdata(sd);
309 if (!dev->platform_data) {
310 dev_err(&client->dev,
311 "no camera_sensor_platform_data");
316 return 0; /* Already off */
318 ret = dev->platform_data->flisclk_ctrl(sd, 0);
320 dev_err(&client->dev, "flisclk failed\n");
323 ret = gpio_ctrl(sd, 0);
325 ret = gpio_ctrl(sd, 0);
327 dev_err(&client->dev, "gpio failed 2\n");
331 ret = power_ctrl(sd, 0);
333 dev_err(&client->dev, "vprog failed.\n");
337 dev->power_on = false;
341 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
343 struct ov2680_device *dev = to_ov2680_sensor(sd);
346 mutex_lock(&dev->input_lock);
349 ret = power_down(sd);
354 mutex_unlock(&dev->input_lock);
359 static struct v4l2_mbus_framefmt *
360 __ov2680_get_pad_format(struct ov2680_device *sensor,
361 struct v4l2_subdev_state *state,
362 unsigned int pad, enum v4l2_subdev_format_whence which)
364 if (which == V4L2_SUBDEV_FORMAT_TRY)
365 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
367 return &sensor->mode.fmt;
370 static void ov2680_fill_format(struct ov2680_device *sensor,
371 struct v4l2_mbus_framefmt *fmt,
372 unsigned int width, unsigned int height)
374 memset(fmt, 0, sizeof(*fmt));
376 fmt->height = height;
377 fmt->field = V4L2_FIELD_NONE;
378 ov2680_set_bayer_order(sensor, fmt);
381 static void ov2680_calc_mode(struct ov2680_device *sensor, int width, int height)
383 int orig_width = width;
384 int orig_height = height;
386 if (width <= (OV2680_NATIVE_WIDTH / 2) &&
387 height <= (OV2680_NATIVE_HEIGHT / 2)) {
388 sensor->mode.binning = true;
392 sensor->mode.binning = false;
395 sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
396 sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
397 sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
398 OV2680_NATIVE_WIDTH - 1);
399 sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
400 OV2680_NATIVE_HEIGHT - 1);
401 sensor->mode.h_output_size = orig_width;
402 sensor->mode.v_output_size = orig_height;
403 sensor->mode.hts = OV2680_PIXELS_PER_LINE;
404 sensor->mode.vts = OV2680_LINES_PER_FRAME;
407 static int ov2680_set_mode(struct ov2680_device *sensor, int width, int height)
409 struct i2c_client *client = sensor->client;
410 u8 pll_div, unknown, inc, fmt1, fmt2;
413 ov2680_calc_mode(sensor, width, height);
415 if (sensor->mode.binning) {
429 ret = ov_write_reg8(client, 0x3086, pll_div);
433 ret = ov_write_reg8(client, 0x370a, unknown);
437 ret = ov_write_reg16(client, OV2680_HORIZONTAL_START_H, sensor->mode.h_start);
441 ret = ov_write_reg16(client, OV2680_VERTICAL_START_H, sensor->mode.v_start);
445 ret = ov_write_reg16(client, OV2680_HORIZONTAL_END_H, sensor->mode.h_end);
449 ret = ov_write_reg16(client, OV2680_VERTICAL_END_H, sensor->mode.v_end);
453 ret = ov_write_reg16(client, OV2680_HORIZONTAL_OUTPUT_SIZE_H,
454 sensor->mode.h_output_size);
458 ret = ov_write_reg16(client, OV2680_VERTICAL_OUTPUT_SIZE_H,
459 sensor->mode.v_output_size);
463 ret = ov_write_reg16(client, OV2680_HTS, sensor->mode.hts);
467 ret = ov_write_reg16(client, OV2680_VTS, sensor->mode.vts);
471 ret = ov_write_reg16(client, OV2680_ISP_X_WIN, 0);
475 ret = ov_write_reg16(client, OV2680_ISP_Y_WIN, 0);
479 ret = ov_write_reg8(client, OV2680_X_INC, inc);
483 ret = ov_write_reg8(client, OV2680_Y_INC, inc);
487 ret = ov_write_reg16(client, OV2680_X_WIN, sensor->mode.h_output_size);
491 ret = ov_write_reg16(client, OV2680_Y_WIN, sensor->mode.v_output_size);
495 ret = ov_write_reg8(client, OV2680_REG_FORMAT1, fmt1);
499 ret = ov_write_reg8(client, OV2680_REG_FORMAT2, fmt2);
506 static int ov2680_set_fmt(struct v4l2_subdev *sd,
507 struct v4l2_subdev_state *sd_state,
508 struct v4l2_subdev_format *format)
510 struct ov2680_device *dev = to_ov2680_sensor(sd);
511 struct i2c_client *client = v4l2_get_subdevdata(sd);
512 struct v4l2_mbus_framefmt *fmt;
513 unsigned int width, height;
516 width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
517 height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
519 fmt = __ov2680_get_pad_format(dev, sd_state, format->pad, format->which);
520 ov2680_fill_format(dev, fmt, width, height);
522 format->format = *fmt;
524 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
527 dev_dbg(&client->dev, "%s: %dx%d\n",
528 __func__, fmt->width, fmt->height);
530 mutex_lock(&dev->input_lock);
532 /* s_power has not been called yet for std v4l2 clients (camorama) */
535 ret = ov2680_set_mode(dev, fmt->width, fmt->height);
539 /* Restore value of all ctrls */
540 ret = __v4l2_ctrl_handler_setup(&dev->ctrls.handler);
542 mutex_unlock(&dev->input_lock);
546 static int ov2680_get_fmt(struct v4l2_subdev *sd,
547 struct v4l2_subdev_state *sd_state,
548 struct v4l2_subdev_format *format)
550 struct ov2680_device *dev = to_ov2680_sensor(sd);
551 struct v4l2_mbus_framefmt *fmt;
553 fmt = __ov2680_get_pad_format(dev, sd_state, format->pad, format->which);
554 format->format = *fmt;
558 static int ov2680_detect(struct i2c_client *client)
560 struct i2c_adapter *adapter = client->adapter;
566 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
569 ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_H, &high);
571 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
574 ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_L, &low);
575 id = ((((u16)high) << 8) | (u16)low);
577 if (id != OV2680_ID) {
578 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
582 ret = ov_read_reg8(client, OV2680_SC_CMMN_SUB_ID, &high);
583 revision = (u8)high & 0x0f;
585 dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
591 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
593 struct ov2680_device *dev = to_ov2680_sensor(sd);
594 struct i2c_client *client = v4l2_get_subdevdata(sd);
597 mutex_lock(&dev->input_lock);
599 dev_dbg(&client->dev, "ov2680_s_stream one\n");
601 dev_dbg(&client->dev, "ov2680_s_stream off\n");
603 ret = ov_write_reg8(client, OV2680_SW_STREAM,
604 enable ? OV2680_START_STREAMING : OV2680_STOP_STREAMING);
606 dev->is_streaming = enable;
607 v4l2_ctrl_activate(dev->ctrls.vflip, !enable);
608 v4l2_ctrl_activate(dev->ctrls.hflip, !enable);
611 //otp valid at stream on state
613 // dev->otp_data = ov2680_otp_read(sd);
615 mutex_unlock(&dev->input_lock);
620 static int ov2680_s_config(struct v4l2_subdev *sd,
621 int irq, void *platform_data)
623 struct ov2680_device *dev = to_ov2680_sensor(sd);
624 struct i2c_client *client = v4l2_get_subdevdata(sd);
631 (struct camera_sensor_platform_data *)platform_data;
633 mutex_lock(&dev->input_lock);
637 dev_err(&client->dev, "ov2680 power-up err.\n");
641 ret = dev->platform_data->csi_cfg(sd, 1);
645 /* config & detect sensor */
646 ret = ov2680_detect(client);
648 dev_err(&client->dev, "ov2680_detect err s_config.\n");
652 /* turn off sensor, after probed */
653 ret = power_down(sd);
655 dev_err(&client->dev, "ov2680 power-off err.\n");
658 mutex_unlock(&dev->input_lock);
663 dev->platform_data->csi_cfg(sd, 0);
666 dev_err(&client->dev, "sensor power-gating failed\n");
667 mutex_unlock(&dev->input_lock);
671 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
672 struct v4l2_subdev_frame_interval *interval)
674 interval->interval.numerator = 1;
675 interval->interval.denominator = OV2680_FPS;
679 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
680 struct v4l2_subdev_state *sd_state,
681 struct v4l2_subdev_mbus_code_enum *code)
683 if (code->index >= MAX_FMTS)
686 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
690 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
691 struct v4l2_subdev_state *sd_state,
692 struct v4l2_subdev_frame_size_enum *fse)
694 static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
708 int index = fse->index;
710 if (index >= ARRAY_SIZE(ov2680_frame_sizes))
713 fse->min_width = ov2680_frame_sizes[index].width;
714 fse->min_height = ov2680_frame_sizes[index].height;
715 fse->max_width = ov2680_frame_sizes[index].width;
716 fse->max_height = ov2680_frame_sizes[index].height;
721 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
722 struct v4l2_subdev_state *sd_state,
723 struct v4l2_subdev_frame_interval_enum *fie)
725 /* Only 1 framerate */
729 fie->interval.numerator = 1;
730 fie->interval.denominator = OV2680_FPS;
734 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
736 *frames = OV2680_SKIP_FRAMES;
740 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
741 .s_stream = ov2680_s_stream,
742 .g_frame_interval = ov2680_g_frame_interval,
745 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
746 .g_skip_frames = ov2680_g_skip_frames,
749 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
750 .s_power = ov2680_s_power,
753 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
754 .enum_mbus_code = ov2680_enum_mbus_code,
755 .enum_frame_size = ov2680_enum_frame_size,
756 .enum_frame_interval = ov2680_enum_frame_interval,
757 .get_fmt = ov2680_get_fmt,
758 .set_fmt = ov2680_set_fmt,
761 static const struct v4l2_subdev_ops ov2680_ops = {
762 .core = &ov2680_core_ops,
763 .video = &ov2680_video_ops,
764 .pad = &ov2680_pad_ops,
765 .sensor = &ov2680_sensor_ops,
768 static int ov2680_init_controls(struct ov2680_device *sensor)
770 static const char * const test_pattern_menu[] = {
777 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
778 struct ov2680_ctrls *ctrls = &sensor->ctrls;
779 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
780 int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
782 v4l2_ctrl_handler_init(hdl, 4);
784 hdl->lock = &sensor->input_lock;
786 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
787 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
788 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
789 0, exp_max, 1, exp_max);
790 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
791 ctrls->test_pattern =
792 v4l2_ctrl_new_std_menu_items(hdl,
793 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
794 ARRAY_SIZE(test_pattern_menu) - 1,
795 0, 0, test_pattern_menu);
797 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
798 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
803 sensor->sd.ctrl_handler = hdl;
807 static void ov2680_remove(struct i2c_client *client)
809 struct v4l2_subdev *sd = i2c_get_clientdata(client);
810 struct ov2680_device *dev = to_ov2680_sensor(sd);
812 dev_dbg(&client->dev, "ov2680_remove...\n");
814 dev->platform_data->csi_cfg(sd, 0);
816 v4l2_device_unregister_subdev(sd);
817 media_entity_cleanup(&dev->sd.entity);
818 v4l2_ctrl_handler_free(&dev->ctrls.handler);
822 static int ov2680_probe(struct i2c_client *client)
824 struct ov2680_device *dev;
828 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
832 mutex_init(&dev->input_lock);
834 dev->client = client;
835 v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
837 pdata = gmin_camera_platform_data(&dev->sd,
838 ATOMISP_INPUT_FORMAT_RAW_10,
839 atomisp_bayer_order_bggr);
845 ret = ov2680_s_config(&dev->sd, client->irq, pdata);
849 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
850 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
851 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
853 ret = ov2680_init_controls(dev);
855 ov2680_remove(client);
859 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
861 ov2680_remove(client);
865 ov2680_fill_format(dev, &dev->mode.fmt, OV2680_NATIVE_WIDTH, OV2680_NATIVE_HEIGHT);
867 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
869 ov2680_remove(client);
875 dev_dbg(&client->dev, "+++ out free\n");
876 v4l2_device_unregister_subdev(&dev->sd);
881 static const struct acpi_device_id ov2680_acpi_match[] = {
886 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
888 static struct i2c_driver ov2680_driver = {
891 .acpi_match_table = ov2680_acpi_match,
893 .probe_new = ov2680_probe,
894 .remove = ov2680_remove,
896 module_i2c_driver(ov2680_driver);
898 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
899 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
900 MODULE_LICENSE("GPL");