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.
6 * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/acpi.h>
20 #include <linux/device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/machine.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/types.h>
28 #include <media/ov_16bit_addr_reg_helpers.h>
29 #include <media/v4l2-device.h>
31 #include "../include/linux/atomisp_gmin_platform.h"
35 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
36 atomisp_bayer_order_bggr,
37 atomisp_bayer_order_grbg,
38 atomisp_bayer_order_gbrg,
39 atomisp_bayer_order_rggb,
42 static int ov2680_write_reg_array(struct i2c_client *client,
43 const struct ov2680_reg *reglist)
45 const struct ov2680_reg *next = reglist;
48 for (; next->reg != 0; next++) {
49 ret = ov_write_reg8(client, next->reg, next->val);
57 static void ov2680_set_bayer_order(struct ov2680_device *sensor, struct v4l2_mbus_framefmt *fmt)
59 static const int ov2680_hv_flip_bayer_order[] = {
60 MEDIA_BUS_FMT_SBGGR10_1X10,
61 MEDIA_BUS_FMT_SGRBG10_1X10,
62 MEDIA_BUS_FMT_SGBRG10_1X10,
63 MEDIA_BUS_FMT_SRGGB10_1X10,
65 struct camera_mipi_info *ov2680_info;
68 if (sensor->ctrls.vflip->val)
71 if (sensor->ctrls.hflip->val)
74 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
76 /* TODO atomisp specific custom API, should be removed */
77 ov2680_info = v4l2_get_subdev_hostdata(&sensor->sd);
79 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[hv_flip];
82 static int ov2680_set_vflip(struct ov2680_device *sensor, s32 val)
86 if (sensor->is_streaming)
89 ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
93 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
97 static int ov2680_set_hflip(struct ov2680_device *sensor, s32 val)
101 if (sensor->is_streaming)
104 ret = ov_update_reg(sensor->client, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
108 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
112 static int ov2680_exposure_set(struct ov2680_device *sensor, u32 exp)
114 return ov_write_reg24(sensor->client, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
117 static int ov2680_gain_set(struct ov2680_device *sensor, u32 gain)
119 return ov_write_reg16(sensor->client, OV2680_REG_GAIN_PK, gain);
122 static int ov2680_test_pattern_set(struct ov2680_device *sensor, int value)
127 return ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), 0);
129 ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
133 ret = ov_update_reg(sensor->client, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
140 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
142 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
143 struct ov2680_device *sensor = to_ov2680_sensor(sd);
146 /* Only apply changes to the controls if the device is powered up */
147 if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
148 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
154 ret = ov2680_set_vflip(sensor, ctrl->val);
157 ret = ov2680_set_hflip(sensor, ctrl->val);
159 case V4L2_CID_EXPOSURE:
160 ret = ov2680_exposure_set(sensor, ctrl->val);
163 ret = ov2680_gain_set(sensor, ctrl->val);
165 case V4L2_CID_TEST_PATTERN:
166 ret = ov2680_test_pattern_set(sensor, ctrl->val);
172 pm_runtime_put(sensor->sd.dev);
176 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
177 .s_ctrl = ov2680_s_ctrl,
180 static int ov2680_init_registers(struct v4l2_subdev *sd)
182 struct i2c_client *client = v4l2_get_subdevdata(sd);
185 ret = ov_write_reg8(client, OV2680_SW_RESET, 0x01);
186 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
191 static struct v4l2_mbus_framefmt *
192 __ov2680_get_pad_format(struct ov2680_device *sensor,
193 struct v4l2_subdev_state *state,
194 unsigned int pad, enum v4l2_subdev_format_whence which)
196 if (which == V4L2_SUBDEV_FORMAT_TRY)
197 return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
199 return &sensor->mode.fmt;
202 static void ov2680_fill_format(struct ov2680_device *sensor,
203 struct v4l2_mbus_framefmt *fmt,
204 unsigned int width, unsigned int height)
206 memset(fmt, 0, sizeof(*fmt));
208 fmt->height = height;
209 fmt->field = V4L2_FIELD_NONE;
210 ov2680_set_bayer_order(sensor, fmt);
213 static void ov2680_calc_mode(struct ov2680_device *sensor, int width, int height)
215 int orig_width = width;
216 int orig_height = height;
218 if (width <= (OV2680_NATIVE_WIDTH / 2) &&
219 height <= (OV2680_NATIVE_HEIGHT / 2)) {
220 sensor->mode.binning = true;
224 sensor->mode.binning = false;
227 sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
228 sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
229 sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
230 OV2680_NATIVE_WIDTH - 1);
231 sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
232 OV2680_NATIVE_HEIGHT - 1);
233 sensor->mode.h_output_size = orig_width;
234 sensor->mode.v_output_size = orig_height;
235 sensor->mode.hts = OV2680_PIXELS_PER_LINE;
236 sensor->mode.vts = OV2680_LINES_PER_FRAME;
239 static int ov2680_set_mode(struct ov2680_device *sensor)
241 struct i2c_client *client = sensor->client;
242 u8 pll_div, unknown, inc, fmt1, fmt2;
245 if (sensor->mode.binning) {
259 ret = ov_write_reg8(client, 0x3086, pll_div);
263 ret = ov_write_reg8(client, 0x370a, unknown);
267 ret = ov_write_reg16(client, OV2680_HORIZONTAL_START_H, sensor->mode.h_start);
271 ret = ov_write_reg16(client, OV2680_VERTICAL_START_H, sensor->mode.v_start);
275 ret = ov_write_reg16(client, OV2680_HORIZONTAL_END_H, sensor->mode.h_end);
279 ret = ov_write_reg16(client, OV2680_VERTICAL_END_H, sensor->mode.v_end);
283 ret = ov_write_reg16(client, OV2680_HORIZONTAL_OUTPUT_SIZE_H,
284 sensor->mode.h_output_size);
288 ret = ov_write_reg16(client, OV2680_VERTICAL_OUTPUT_SIZE_H,
289 sensor->mode.v_output_size);
293 ret = ov_write_reg16(client, OV2680_HTS, sensor->mode.hts);
297 ret = ov_write_reg16(client, OV2680_VTS, sensor->mode.vts);
301 ret = ov_write_reg16(client, OV2680_ISP_X_WIN, 0);
305 ret = ov_write_reg16(client, OV2680_ISP_Y_WIN, 0);
309 ret = ov_write_reg8(client, OV2680_X_INC, inc);
313 ret = ov_write_reg8(client, OV2680_Y_INC, inc);
317 ret = ov_write_reg16(client, OV2680_X_WIN, sensor->mode.h_output_size);
321 ret = ov_write_reg16(client, OV2680_Y_WIN, sensor->mode.v_output_size);
325 ret = ov_write_reg8(client, OV2680_REG_FORMAT1, fmt1);
329 ret = ov_write_reg8(client, OV2680_REG_FORMAT2, fmt2);
336 static int ov2680_set_fmt(struct v4l2_subdev *sd,
337 struct v4l2_subdev_state *sd_state,
338 struct v4l2_subdev_format *format)
340 struct ov2680_device *sensor = to_ov2680_sensor(sd);
341 struct v4l2_mbus_framefmt *fmt;
342 unsigned int width, height;
344 width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
345 height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
347 fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
348 ov2680_fill_format(sensor, fmt, width, height);
350 format->format = *fmt;
352 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
355 mutex_lock(&sensor->input_lock);
356 ov2680_calc_mode(sensor, fmt->width, fmt->height);
357 mutex_unlock(&sensor->input_lock);
361 static int ov2680_get_fmt(struct v4l2_subdev *sd,
362 struct v4l2_subdev_state *sd_state,
363 struct v4l2_subdev_format *format)
365 struct ov2680_device *sensor = to_ov2680_sensor(sd);
366 struct v4l2_mbus_framefmt *fmt;
368 fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
369 format->format = *fmt;
373 static int ov2680_detect(struct i2c_client *client)
375 struct i2c_adapter *adapter = client->adapter;
376 u32 high = 0, low = 0;
381 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
384 ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_H, &high);
386 dev_err(&client->dev, "sensor_id_high read failed (%d)\n", ret);
389 ret = ov_read_reg8(client, OV2680_SC_CMMN_CHIP_ID_L, &low);
390 id = ((((u16)high) << 8) | (u16)low);
392 if (id != OV2680_ID) {
393 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
397 ret = ov_read_reg8(client, OV2680_SC_CMMN_SUB_ID, &high);
398 revision = (u8)high & 0x0f;
400 dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
406 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
408 struct ov2680_device *sensor = to_ov2680_sensor(sd);
409 struct i2c_client *client = v4l2_get_subdevdata(sd);
412 mutex_lock(&sensor->input_lock);
414 if (sensor->is_streaming == enable) {
415 dev_warn(&client->dev, "stream already %s\n", enable ? "started" : "stopped");
420 ret = pm_runtime_get_sync(sensor->sd.dev);
422 goto error_power_down;
424 ret = ov2680_set_mode(sensor);
426 goto error_power_down;
428 /* Restore value of all ctrls */
429 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
431 goto error_power_down;
433 ret = ov_write_reg8(client, OV2680_SW_STREAM, OV2680_START_STREAMING);
435 goto error_power_down;
437 ov_write_reg8(client, OV2680_SW_STREAM, OV2680_STOP_STREAMING);
438 pm_runtime_put(sensor->sd.dev);
441 sensor->is_streaming = enable;
442 v4l2_ctrl_activate(sensor->ctrls.vflip, !enable);
443 v4l2_ctrl_activate(sensor->ctrls.hflip, !enable);
445 mutex_unlock(&sensor->input_lock);
449 pm_runtime_put(sensor->sd.dev);
450 sensor->is_streaming = false;
452 mutex_unlock(&sensor->input_lock);
456 static int ov2680_s_config(struct v4l2_subdev *sd)
458 struct i2c_client *client = v4l2_get_subdevdata(sd);
461 ret = pm_runtime_get_sync(&client->dev);
463 dev_err(&client->dev, "ov2680 power-up err.\n");
467 /* config & detect sensor */
468 ret = ov2680_detect(client);
470 dev_err(&client->dev, "ov2680_detect err s_config.\n");
473 pm_runtime_put(&client->dev);
477 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
478 struct v4l2_subdev_frame_interval *interval)
480 interval->interval.numerator = 1;
481 interval->interval.denominator = OV2680_FPS;
485 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
486 struct v4l2_subdev_state *sd_state,
487 struct v4l2_subdev_mbus_code_enum *code)
489 /* We support only a single format */
493 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
497 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
498 struct v4l2_subdev_state *sd_state,
499 struct v4l2_subdev_frame_size_enum *fse)
501 static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
511 int index = fse->index;
513 if (index >= ARRAY_SIZE(ov2680_frame_sizes))
516 fse->min_width = ov2680_frame_sizes[index].width;
517 fse->min_height = ov2680_frame_sizes[index].height;
518 fse->max_width = ov2680_frame_sizes[index].width;
519 fse->max_height = ov2680_frame_sizes[index].height;
524 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
525 struct v4l2_subdev_state *sd_state,
526 struct v4l2_subdev_frame_interval_enum *fie)
528 /* Only 1 framerate */
532 fie->interval.numerator = 1;
533 fie->interval.denominator = OV2680_FPS;
537 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
539 *frames = OV2680_SKIP_FRAMES;
543 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
544 .s_stream = ov2680_s_stream,
545 .g_frame_interval = ov2680_g_frame_interval,
548 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
549 .g_skip_frames = ov2680_g_skip_frames,
552 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
553 .enum_mbus_code = ov2680_enum_mbus_code,
554 .enum_frame_size = ov2680_enum_frame_size,
555 .enum_frame_interval = ov2680_enum_frame_interval,
556 .get_fmt = ov2680_get_fmt,
557 .set_fmt = ov2680_set_fmt,
560 static const struct v4l2_subdev_ops ov2680_ops = {
561 .video = &ov2680_video_ops,
562 .pad = &ov2680_pad_ops,
563 .sensor = &ov2680_sensor_ops,
566 static int ov2680_init_controls(struct ov2680_device *sensor)
568 static const char * const test_pattern_menu[] = {
575 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
576 struct ov2680_ctrls *ctrls = &sensor->ctrls;
577 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
578 int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
580 v4l2_ctrl_handler_init(hdl, 4);
582 hdl->lock = &sensor->input_lock;
584 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
585 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
586 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
587 0, exp_max, 1, exp_max);
588 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
589 ctrls->test_pattern =
590 v4l2_ctrl_new_std_menu_items(hdl,
591 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
592 ARRAY_SIZE(test_pattern_menu) - 1,
593 0, 0, test_pattern_menu);
595 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
596 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
601 sensor->sd.ctrl_handler = hdl;
605 static void ov2680_remove(struct i2c_client *client)
607 struct v4l2_subdev *sd = i2c_get_clientdata(client);
608 struct ov2680_device *sensor = to_ov2680_sensor(sd);
610 dev_dbg(&client->dev, "ov2680_remove...\n");
612 atomisp_unregister_subdev(sd);
613 v4l2_device_unregister_subdev(sd);
614 media_entity_cleanup(&sensor->sd.entity);
615 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
616 pm_runtime_disable(&client->dev);
619 static int ov2680_probe(struct i2c_client *client)
621 struct device *dev = &client->dev;
622 struct ov2680_device *sensor;
625 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
629 mutex_init(&sensor->input_lock);
631 sensor->client = client;
632 v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_ops);
634 ret = v4l2_get_acpi_sensor_info(dev, NULL);
638 sensor->powerdown = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
639 if (IS_ERR(sensor->powerdown))
640 return dev_err_probe(dev, PTR_ERR(sensor->powerdown), "getting powerdown GPIO\n");
642 pm_runtime_set_suspended(dev);
643 pm_runtime_enable(dev);
644 pm_runtime_set_autosuspend_delay(dev, 1000);
645 pm_runtime_use_autosuspend(dev);
647 ret = ov2680_s_config(&sensor->sd);
649 ov2680_remove(client);
653 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
654 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
655 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
657 ret = ov2680_init_controls(sensor);
659 ov2680_remove(client);
663 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
665 ov2680_remove(client);
669 ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_NATIVE_WIDTH, OV2680_NATIVE_HEIGHT);
671 ret = atomisp_register_sensor_no_gmin(&sensor->sd, 1, ATOMISP_INPUT_FORMAT_RAW_10,
672 atomisp_bayer_order_bggr);
674 ov2680_remove(client);
681 static int ov2680_suspend(struct device *dev)
683 struct v4l2_subdev *sd = dev_get_drvdata(dev);
684 struct ov2680_device *sensor = to_ov2680_sensor(sd);
686 gpiod_set_value_cansleep(sensor->powerdown, 1);
690 static int ov2680_resume(struct device *dev)
692 struct v4l2_subdev *sd = dev_get_drvdata(dev);
693 struct ov2680_device *sensor = to_ov2680_sensor(sd);
695 /* according to DS, at least 5ms is needed after DOVDD (enabled by ACPI) */
696 usleep_range(5000, 6000);
698 gpiod_set_value_cansleep(sensor->powerdown, 0);
700 /* according to DS, 20ms is needed between PWDN and i2c access */
703 ov2680_init_registers(sd);
707 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, NULL);
709 static const struct acpi_device_id ov2680_acpi_match[] = {
714 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
716 static struct i2c_driver ov2680_driver = {
719 .pm = pm_sleep_ptr(&ov2680_pm_ops),
720 .acpi_match_table = ov2680_acpi_match,
722 .probe_new = ov2680_probe,
723 .remove = ov2680_remove,
725 module_i2c_driver(ov2680_driver);
727 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
728 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
729 MODULE_LICENSE("GPL");