13ee47cadSRui Miguel Silva // SPDX-License-Identifier: GPL-2.0 23ee47cadSRui Miguel Silva /* 33ee47cadSRui Miguel Silva * Omnivision OV2680 CMOS Image Sensor driver 43ee47cadSRui Miguel Silva * 53ee47cadSRui Miguel Silva * Copyright (C) 2018 Linaro Ltd 63ee47cadSRui Miguel Silva * 73ee47cadSRui Miguel Silva * Based on OV5640 Sensor Driver 83ee47cadSRui Miguel Silva * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved. 93ee47cadSRui Miguel Silva * Copyright (C) 2014-2017 Mentor Graphics Inc. 103ee47cadSRui Miguel Silva * 113ee47cadSRui Miguel Silva */ 123ee47cadSRui Miguel Silva 133ee47cadSRui Miguel Silva #include <linux/clk.h> 143ee47cadSRui Miguel Silva #include <linux/delay.h> 153ee47cadSRui Miguel Silva #include <linux/err.h> 169289998eSHans de Goede #include <linux/gpio/consumer.h> 173ee47cadSRui Miguel Silva #include <linux/i2c.h> 183ee47cadSRui Miguel Silva #include <linux/init.h> 197c7e33b7SRob Herring #include <linux/mod_devicetable.h> 203ee47cadSRui Miguel Silva #include <linux/module.h> 21990732a6SHans de Goede #include <linux/pm_runtime.h> 229289998eSHans de Goede #include <linux/regmap.h> 233ee47cadSRui Miguel Silva #include <linux/regulator/consumer.h> 243ee47cadSRui Miguel Silva 259289998eSHans de Goede #include <media/v4l2-cci.h> 263ee47cadSRui Miguel Silva #include <media/v4l2-common.h> 273ee47cadSRui Miguel Silva #include <media/v4l2-ctrls.h> 283ee47cadSRui Miguel Silva #include <media/v4l2-subdev.h> 293ee47cadSRui Miguel Silva 303ee47cadSRui Miguel Silva #define OV2680_CHIP_ID 0x2680 313ee47cadSRui Miguel Silva 329289998eSHans de Goede #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100) 339289998eSHans de Goede #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103) 343ee47cadSRui Miguel Silva 359289998eSHans de Goede #define OV2680_REG_CHIP_ID CCI_REG16(0x300a) 368e50a122SHans de Goede #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081) 373ee47cadSRui Miguel Silva 389289998eSHans de Goede #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500) 399289998eSHans de Goede #define OV2680_REG_R_MANUAL CCI_REG8(0x3503) 409289998eSHans de Goede #define OV2680_REG_GAIN_PK CCI_REG16(0x350a) 4163f47529SHans de Goede 4263f47529SHans de Goede #define OV2680_REG_SENSOR_CTRL_0A CCI_REG8(0x370a) 4363f47529SHans de Goede 4463f47529SHans de Goede #define OV2680_REG_HORIZONTAL_START CCI_REG16(0x3800) 4563f47529SHans de Goede #define OV2680_REG_VERTICAL_START CCI_REG16(0x3802) 4663f47529SHans de Goede #define OV2680_REG_HORIZONTAL_END CCI_REG16(0x3804) 4763f47529SHans de Goede #define OV2680_REG_VERTICAL_END CCI_REG16(0x3806) 4863f47529SHans de Goede #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE CCI_REG16(0x3808) 4963f47529SHans de Goede #define OV2680_REG_VERTICAL_OUTPUT_SIZE CCI_REG16(0x380a) 509289998eSHans de Goede #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c) 519289998eSHans de Goede #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e) 5263f47529SHans de Goede #define OV2680_REG_ISP_X_WIN CCI_REG16(0x3810) 5363f47529SHans de Goede #define OV2680_REG_ISP_Y_WIN CCI_REG16(0x3812) 5463f47529SHans de Goede #define OV2680_REG_X_INC CCI_REG8(0x3814) 5563f47529SHans de Goede #define OV2680_REG_Y_INC CCI_REG8(0x3815) 569289998eSHans de Goede #define OV2680_REG_FORMAT1 CCI_REG8(0x3820) 579289998eSHans de Goede #define OV2680_REG_FORMAT2 CCI_REG8(0x3821) 583ee47cadSRui Miguel Silva 599289998eSHans de Goede #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080) 603ee47cadSRui Miguel Silva 6163f47529SHans de Goede #define OV2680_REG_X_WIN CCI_REG16(0x5704) 6263f47529SHans de Goede #define OV2680_REG_Y_WIN CCI_REG16(0x5706) 6363f47529SHans de Goede 643ee47cadSRui Miguel Silva #define OV2680_FRAME_RATE 30 653ee47cadSRui Miguel Silva 6663f47529SHans de Goede #define OV2680_NATIVE_WIDTH 1616 6763f47529SHans de Goede #define OV2680_NATIVE_HEIGHT 1216 6863f47529SHans de Goede #define OV2680_ACTIVE_WIDTH 1600 6963f47529SHans de Goede #define OV2680_ACTIVE_HEIGHT 1200 7063f47529SHans de Goede 7163f47529SHans de Goede /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */ 7263f47529SHans de Goede #define OV2680_PIXELS_PER_LINE 1704 7363f47529SHans de Goede #define OV2680_LINES_PER_FRAME 1294 7463f47529SHans de Goede 7563f47529SHans de Goede /* If possible send 16 extra rows / lines to the ISP as padding */ 7663f47529SHans de Goede #define OV2680_END_MARGIN 16 773ee47cadSRui Miguel Silva 786d6849b2SHans de Goede #define OV2680_DEFAULT_WIDTH 800 796d6849b2SHans de Goede #define OV2680_DEFAULT_HEIGHT 600 806d6849b2SHans de Goede 8163f47529SHans de Goede /* For enum_frame_size() full-size + binned-/quarter-size */ 8263f47529SHans de Goede #define OV2680_FRAME_SIZES 2 833ee47cadSRui Miguel Silva 843ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = { 853ee47cadSRui Miguel Silva "DOVDD", 863ee47cadSRui Miguel Silva "DVDD", 873ee47cadSRui Miguel Silva "AVDD", 883ee47cadSRui Miguel Silva }; 893ee47cadSRui Miguel Silva 903ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name) 913ee47cadSRui Miguel Silva 928e50a122SHans de Goede enum { 938e50a122SHans de Goede OV2680_19_2_MHZ, 948e50a122SHans de Goede OV2680_24_MHZ, 958e50a122SHans de Goede }; 968e50a122SHans de Goede 978e50a122SHans de Goede static const unsigned long ov2680_xvclk_freqs[] = { 988e50a122SHans de Goede [OV2680_19_2_MHZ] = 19200000, 998e50a122SHans de Goede [OV2680_24_MHZ] = 24000000, 1008e50a122SHans de Goede }; 1018e50a122SHans de Goede 1028e50a122SHans de Goede static const u8 ov2680_pll_multipliers[] = { 1038e50a122SHans de Goede [OV2680_19_2_MHZ] = 69, 1048e50a122SHans de Goede [OV2680_24_MHZ] = 55, 1058e50a122SHans de Goede }; 1068e50a122SHans de Goede 1073ee47cadSRui Miguel Silva struct ov2680_ctrls { 1083ee47cadSRui Miguel Silva struct v4l2_ctrl_handler handler; 1093ee47cadSRui Miguel Silva struct v4l2_ctrl *exposure; 1103ee47cadSRui Miguel Silva struct v4l2_ctrl *gain; 1113ee47cadSRui Miguel Silva struct v4l2_ctrl *hflip; 1123ee47cadSRui Miguel Silva struct v4l2_ctrl *vflip; 1133ee47cadSRui Miguel Silva struct v4l2_ctrl *test_pattern; 1143ee47cadSRui Miguel Silva }; 1153ee47cadSRui Miguel Silva 116f614dfb8SHans de Goede struct ov2680_mode { 117f614dfb8SHans de Goede struct v4l2_mbus_framefmt fmt; 118f614dfb8SHans de Goede struct v4l2_fract frame_interval; 11963f47529SHans de Goede bool binning; 12063f47529SHans de Goede u16 h_start; 12163f47529SHans de Goede u16 v_start; 12263f47529SHans de Goede u16 h_end; 12363f47529SHans de Goede u16 v_end; 12463f47529SHans de Goede u16 h_output_size; 12563f47529SHans de Goede u16 v_output_size; 12663f47529SHans de Goede u16 hts; 12763f47529SHans de Goede u16 vts; 128f614dfb8SHans de Goede }; 129f614dfb8SHans de Goede 1303ee47cadSRui Miguel Silva struct ov2680_dev { 1317adfdecbSHans de Goede struct device *dev; 1329289998eSHans de Goede struct regmap *regmap; 1333ee47cadSRui Miguel Silva struct v4l2_subdev sd; 1343ee47cadSRui Miguel Silva 1353ee47cadSRui Miguel Silva struct media_pad pad; 1363ee47cadSRui Miguel Silva struct clk *xvclk; 1373ee47cadSRui Miguel Silva u32 xvclk_freq; 1388e50a122SHans de Goede u8 pll_mult; 1393ee47cadSRui Miguel Silva struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 1403ee47cadSRui Miguel Silva 141e9305a23SHans de Goede struct gpio_desc *pwdn_gpio; 1423ee47cadSRui Miguel Silva struct mutex lock; /* protect members */ 1433ee47cadSRui Miguel Silva 1443ee47cadSRui Miguel Silva bool is_streaming; 1453ee47cadSRui Miguel Silva 1463ee47cadSRui Miguel Silva struct ov2680_ctrls ctrls; 147f614dfb8SHans de Goede struct ov2680_mode mode; 1483ee47cadSRui Miguel Silva }; 1493ee47cadSRui Miguel Silva 1503ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = { 1513ee47cadSRui Miguel Silva "Disabled", 1523ee47cadSRui Miguel Silva "Color Bars", 1533ee47cadSRui Miguel Silva "Random Data", 1543ee47cadSRui Miguel Silva "Square", 1553ee47cadSRui Miguel Silva "Black Image", 1563ee47cadSRui Miguel Silva }; 1573ee47cadSRui Miguel Silva 1583ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = { 1593ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SBGGR10_1X10, 1603ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGRBG10_1X10, 1613ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGBRG10_1X10, 1623ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SRGGB10_1X10, 1633ee47cadSRui Miguel Silva }; 1643ee47cadSRui Miguel Silva 16563f47529SHans de Goede static const struct reg_sequence ov2680_global_setting[] = { 16663f47529SHans de Goede /* R MANUAL set exposure and gain to manual (hw does not do auto) */ 16763f47529SHans de Goede {0x3503, 0x03}, 1680a61cf33SHans de Goede 1690a61cf33SHans de Goede /* Set black level compensation range to 0 - 3 (default 0 - 11) */ 1700a61cf33SHans de Goede {0x4008, 0x00}, 1710a61cf33SHans de Goede {0x4009, 0x03}, 1720a61cf33SHans de Goede 17363f47529SHans de Goede /* 17463f47529SHans de Goede * Window CONTROL 0x00 -> 0x01, enable manual window control, 17563f47529SHans de Goede * this is necessary for full size flip and mirror support. 17663f47529SHans de Goede */ 17763f47529SHans de Goede {0x5708, 0x01}, 1783ee47cadSRui Miguel Silva }; 1793ee47cadSRui Miguel Silva 1803ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 1813ee47cadSRui Miguel Silva { 1823ee47cadSRui Miguel Silva return container_of(sd, struct ov2680_dev, sd); 1833ee47cadSRui Miguel Silva } 1843ee47cadSRui Miguel Silva 1853ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 1863ee47cadSRui Miguel Silva { 1873ee47cadSRui Miguel Silva return &container_of(ctrl->handler, struct ov2680_dev, 1883ee47cadSRui Miguel Silva ctrls.handler)->sd; 1893ee47cadSRui Miguel Silva } 1903ee47cadSRui Miguel Silva 1913ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor) 1923ee47cadSRui Miguel Silva { 193e9305a23SHans de Goede if (!sensor->pwdn_gpio) 1943ee47cadSRui Miguel Silva return; 1953ee47cadSRui Miguel Silva 196e9305a23SHans de Goede gpiod_set_value(sensor->pwdn_gpio, 0); 1973ee47cadSRui Miguel Silva usleep_range(5000, 10000); 1983ee47cadSRui Miguel Silva } 1993ee47cadSRui Miguel Silva 2003ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor) 2013ee47cadSRui Miguel Silva { 202e9305a23SHans de Goede if (!sensor->pwdn_gpio) 2033ee47cadSRui Miguel Silva return; 2043ee47cadSRui Miguel Silva 205e9305a23SHans de Goede gpiod_set_value(sensor->pwdn_gpio, 1); 2063ee47cadSRui Miguel Silva usleep_range(5000, 10000); 2073ee47cadSRui Miguel Silva } 2083ee47cadSRui Miguel Silva 2096d6849b2SHans de Goede static void ov2680_set_bayer_order(struct ov2680_dev *sensor, 2106d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt) 2113ee47cadSRui Miguel Silva { 21250a7bad4SHans de Goede int hv_flip = 0; 2133ee47cadSRui Miguel Silva 21450a7bad4SHans de Goede if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) 21550a7bad4SHans de Goede hv_flip += 1; 2163ee47cadSRui Miguel Silva 21750a7bad4SHans de Goede if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) 21850a7bad4SHans de Goede hv_flip += 2; 2193ee47cadSRui Miguel Silva 2206d6849b2SHans de Goede fmt->code = ov2680_hv_flip_bayer_order[hv_flip]; 2216d6849b2SHans de Goede } 2226d6849b2SHans de Goede 223*23321b91SHans de Goede static struct v4l2_mbus_framefmt * 224*23321b91SHans de Goede __ov2680_get_pad_format(struct ov2680_dev *sensor, 225*23321b91SHans de Goede struct v4l2_subdev_state *state, 226*23321b91SHans de Goede unsigned int pad, 227*23321b91SHans de Goede enum v4l2_subdev_format_whence which) 228*23321b91SHans de Goede { 229*23321b91SHans de Goede if (which == V4L2_SUBDEV_FORMAT_TRY) 230*23321b91SHans de Goede return v4l2_subdev_get_try_format(&sensor->sd, state, pad); 231*23321b91SHans de Goede 232*23321b91SHans de Goede return &sensor->mode.fmt; 233*23321b91SHans de Goede } 234*23321b91SHans de Goede 2356d6849b2SHans de Goede static void ov2680_fill_format(struct ov2680_dev *sensor, 2366d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt, 2376d6849b2SHans de Goede unsigned int width, unsigned int height) 2386d6849b2SHans de Goede { 2396d6849b2SHans de Goede memset(fmt, 0, sizeof(*fmt)); 2406d6849b2SHans de Goede fmt->width = width; 2416d6849b2SHans de Goede fmt->height = height; 2426d6849b2SHans de Goede fmt->field = V4L2_FIELD_NONE; 2436d6849b2SHans de Goede fmt->colorspace = V4L2_COLORSPACE_SRGB; 2446d6849b2SHans de Goede ov2680_set_bayer_order(sensor, fmt); 2453ee47cadSRui Miguel Silva } 2463ee47cadSRui Miguel Silva 24763f47529SHans de Goede static void ov2680_calc_mode(struct ov2680_dev *sensor) 24863f47529SHans de Goede { 24963f47529SHans de Goede int width = sensor->mode.fmt.width; 25063f47529SHans de Goede int height = sensor->mode.fmt.height; 25163f47529SHans de Goede int orig_width = width; 25263f47529SHans de Goede int orig_height = height; 25363f47529SHans de Goede 25463f47529SHans de Goede if (width <= (OV2680_NATIVE_WIDTH / 2) && 25563f47529SHans de Goede height <= (OV2680_NATIVE_HEIGHT / 2)) { 25663f47529SHans de Goede sensor->mode.binning = true; 25763f47529SHans de Goede width *= 2; 25863f47529SHans de Goede height *= 2; 25963f47529SHans de Goede } else { 26063f47529SHans de Goede sensor->mode.binning = false; 26163f47529SHans de Goede } 26263f47529SHans de Goede 26363f47529SHans de Goede sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1; 26463f47529SHans de Goede sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1; 26563f47529SHans de Goede sensor->mode.h_end = 26663f47529SHans de Goede min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1, 26763f47529SHans de Goede OV2680_NATIVE_WIDTH - 1); 26863f47529SHans de Goede sensor->mode.v_end = 26963f47529SHans de Goede min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1, 27063f47529SHans de Goede OV2680_NATIVE_HEIGHT - 1); 27163f47529SHans de Goede sensor->mode.h_output_size = orig_width; 27263f47529SHans de Goede sensor->mode.v_output_size = orig_height; 27363f47529SHans de Goede sensor->mode.hts = OV2680_PIXELS_PER_LINE; 27463f47529SHans de Goede sensor->mode.vts = OV2680_LINES_PER_FRAME; 27563f47529SHans de Goede } 27663f47529SHans de Goede 27763f47529SHans de Goede static int ov2680_set_mode(struct ov2680_dev *sensor) 27863f47529SHans de Goede { 27963f47529SHans de Goede u8 sensor_ctrl_0a, inc, fmt1, fmt2; 28063f47529SHans de Goede int ret = 0; 28163f47529SHans de Goede 28263f47529SHans de Goede if (sensor->mode.binning) { 28363f47529SHans de Goede sensor_ctrl_0a = 0x23; 28463f47529SHans de Goede inc = 0x31; 28563f47529SHans de Goede fmt1 = 0xc2; 28663f47529SHans de Goede fmt2 = 0x01; 28763f47529SHans de Goede } else { 28863f47529SHans de Goede sensor_ctrl_0a = 0x21; 28963f47529SHans de Goede inc = 0x11; 29063f47529SHans de Goede fmt1 = 0xc0; 29163f47529SHans de Goede fmt2 = 0x00; 29263f47529SHans de Goede } 29363f47529SHans de Goede 29463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A, 29563f47529SHans de Goede sensor_ctrl_0a, &ret); 29663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START, 29763f47529SHans de Goede sensor->mode.h_start, &ret); 29863f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_START, 29963f47529SHans de Goede sensor->mode.v_start, &ret); 30063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END, 30163f47529SHans de Goede sensor->mode.h_end, &ret); 30263f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_END, 30363f47529SHans de Goede sensor->mode.v_end, &ret); 30463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE, 30563f47529SHans de Goede sensor->mode.h_output_size, &ret); 30663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE, 30763f47529SHans de Goede sensor->mode.v_output_size, &ret); 30863f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_TIMING_HTS, 30963f47529SHans de Goede sensor->mode.hts, &ret); 31063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_TIMING_VTS, 31163f47529SHans de Goede sensor->mode.vts, &ret); 31263f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret); 31363f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret); 31463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret); 31563f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret); 31663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_X_WIN, 31763f47529SHans de Goede sensor->mode.h_output_size, &ret); 31863f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_Y_WIN, 31963f47529SHans de Goede sensor->mode.v_output_size, &ret); 32063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret); 32163f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret); 32263f47529SHans de Goede 32363f47529SHans de Goede return ret; 32463f47529SHans de Goede } 32563f47529SHans de Goede 326d5d08ad3SHans de Goede static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) 3273ee47cadSRui Miguel Silva { 3283ee47cadSRui Miguel Silva int ret; 3293ee47cadSRui Miguel Silva 330d5d08ad3SHans de Goede if (sensor->is_streaming) 331d5d08ad3SHans de Goede return -EBUSY; 332d5d08ad3SHans de Goede 3339289998eSHans de Goede ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1, 3349289998eSHans de Goede BIT(2), val ? BIT(2) : 0, NULL); 3353ee47cadSRui Miguel Silva if (ret < 0) 3363ee47cadSRui Miguel Silva return ret; 3373ee47cadSRui Miguel Silva 338f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 33950a7bad4SHans de Goede return 0; 3403ee47cadSRui Miguel Silva } 3413ee47cadSRui Miguel Silva 342d5d08ad3SHans de Goede static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) 3433ee47cadSRui Miguel Silva { 3443ee47cadSRui Miguel Silva int ret; 3453ee47cadSRui Miguel Silva 346d5d08ad3SHans de Goede if (sensor->is_streaming) 347d5d08ad3SHans de Goede return -EBUSY; 3483ee47cadSRui Miguel Silva 3499289998eSHans de Goede ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2, 3509289998eSHans de Goede BIT(2), val ? BIT(2) : 0, NULL); 3513ee47cadSRui Miguel Silva if (ret < 0) 3523ee47cadSRui Miguel Silva return ret; 3533ee47cadSRui Miguel Silva 354f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 35550a7bad4SHans de Goede return 0; 3563ee47cadSRui Miguel Silva } 3573ee47cadSRui Miguel Silva 3583ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 3593ee47cadSRui Miguel Silva { 3609289998eSHans de Goede int ret = 0; 3613ee47cadSRui Miguel Silva 3623ee47cadSRui Miguel Silva if (!value) 3639289998eSHans de Goede return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 3649289998eSHans de Goede BIT(7), 0, NULL); 3653ee47cadSRui Miguel Silva 3669289998eSHans de Goede cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 3679289998eSHans de Goede 0x03, value - 1, &ret); 3689289998eSHans de Goede cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 3699289998eSHans de Goede BIT(7), BIT(7), &ret); 3709289998eSHans de Goede 3713ee47cadSRui Miguel Silva return ret; 3723ee47cadSRui Miguel Silva } 3733ee47cadSRui Miguel Silva 3747b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) 3753ee47cadSRui Miguel Silva { 3769289998eSHans de Goede return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL); 3773ee47cadSRui Miguel Silva } 3783ee47cadSRui Miguel Silva 3797b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) 3803ee47cadSRui Miguel Silva { 3819289998eSHans de Goede return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4, 3829289998eSHans de Goede NULL); 3833ee47cadSRui Miguel Silva } 3843ee47cadSRui Miguel Silva 3853ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor) 3863ee47cadSRui Miguel Silva { 3873ee47cadSRui Miguel Silva int ret; 3883ee47cadSRui Miguel Silva 3898e50a122SHans de Goede ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER, 3908e50a122SHans de Goede sensor->pll_mult, NULL); 3918e50a122SHans de Goede if (ret < 0) 3928e50a122SHans de Goede return ret; 3938e50a122SHans de Goede 3949289998eSHans de Goede ret = regmap_multi_reg_write(sensor->regmap, 39563f47529SHans de Goede ov2680_global_setting, 39663f47529SHans de Goede ARRAY_SIZE(ov2680_global_setting)); 397990732a6SHans de Goede if (ret < 0) 398990732a6SHans de Goede return ret; 399990732a6SHans de Goede 40063f47529SHans de Goede ret = ov2680_set_mode(sensor); 4013ee47cadSRui Miguel Silva if (ret < 0) 4023ee47cadSRui Miguel Silva return ret; 4033ee47cadSRui Miguel Silva 4047b5a42e6SHans de Goede /* Restore value of all ctrls */ 4057b5a42e6SHans de Goede ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 4063ee47cadSRui Miguel Silva if (ret < 0) 4073ee47cadSRui Miguel Silva return ret; 4083ee47cadSRui Miguel Silva 409990732a6SHans de Goede return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL); 4103ee47cadSRui Miguel Silva } 4113ee47cadSRui Miguel Silva 412990732a6SHans de Goede static int ov2680_stream_disable(struct ov2680_dev *sensor) 4133ee47cadSRui Miguel Silva { 414990732a6SHans de Goede return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL); 4153ee47cadSRui Miguel Silva } 4163ee47cadSRui Miguel Silva 4173ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor) 4183ee47cadSRui Miguel Silva { 4193ee47cadSRui Miguel Silva clk_disable_unprepare(sensor->xvclk); 4203ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4213ee47cadSRui Miguel Silva regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 4223ee47cadSRui Miguel Silva return 0; 4233ee47cadSRui Miguel Silva } 4243ee47cadSRui Miguel Silva 4253ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor) 4263ee47cadSRui Miguel Silva { 4273ee47cadSRui Miguel Silva int ret; 4283ee47cadSRui Miguel Silva 4293ee47cadSRui Miguel Silva ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 4303ee47cadSRui Miguel Silva if (ret < 0) { 4317adfdecbSHans de Goede dev_err(sensor->dev, "failed to enable regulators: %d\n", ret); 4323ee47cadSRui Miguel Silva return ret; 4333ee47cadSRui Miguel Silva } 4343ee47cadSRui Miguel Silva 435e9305a23SHans de Goede if (!sensor->pwdn_gpio) { 4369289998eSHans de Goede ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, 4379289998eSHans de Goede NULL); 4383ee47cadSRui Miguel Silva if (ret != 0) { 4397adfdecbSHans de Goede dev_err(sensor->dev, "sensor soft reset failed\n"); 44084b4bd7eSHans de Goede goto err_disable_regulators; 4413ee47cadSRui Miguel Silva } 4423ee47cadSRui Miguel Silva usleep_range(1000, 2000); 4433ee47cadSRui Miguel Silva } else { 4443ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4453ee47cadSRui Miguel Silva ov2680_power_up(sensor); 4463ee47cadSRui Miguel Silva } 4473ee47cadSRui Miguel Silva 4483ee47cadSRui Miguel Silva ret = clk_prepare_enable(sensor->xvclk); 4493ee47cadSRui Miguel Silva if (ret < 0) 45084b4bd7eSHans de Goede goto err_disable_regulators; 4513ee47cadSRui Miguel Silva 4523ee47cadSRui Miguel Silva return 0; 45384b4bd7eSHans de Goede 45484b4bd7eSHans de Goede err_disable_regulators: 45584b4bd7eSHans de Goede regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 45684b4bd7eSHans de Goede return ret; 4573ee47cadSRui Miguel Silva } 4583ee47cadSRui Miguel Silva 4593ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 4603ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval *fi) 4613ee47cadSRui Miguel Silva { 4623ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 4633ee47cadSRui Miguel Silva 4643ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 465f614dfb8SHans de Goede fi->interval = sensor->mode.frame_interval; 4663ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 4673ee47cadSRui Miguel Silva 4683ee47cadSRui Miguel Silva return 0; 4693ee47cadSRui Miguel Silva } 4703ee47cadSRui Miguel Silva 4713ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 4723ee47cadSRui Miguel Silva { 4733ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 4743ee47cadSRui Miguel Silva int ret = 0; 4753ee47cadSRui Miguel Silva 4763ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 4773ee47cadSRui Miguel Silva 4783ee47cadSRui Miguel Silva if (sensor->is_streaming == !!enable) 4793ee47cadSRui Miguel Silva goto unlock; 4803ee47cadSRui Miguel Silva 481990732a6SHans de Goede if (enable) { 482990732a6SHans de Goede ret = pm_runtime_resume_and_get(sensor->sd.dev); 4833ee47cadSRui Miguel Silva if (ret < 0) 4843ee47cadSRui Miguel Silva goto unlock; 4853ee47cadSRui Miguel Silva 4863ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 487990732a6SHans de Goede if (ret < 0) { 488990732a6SHans de Goede pm_runtime_put(sensor->sd.dev); 489990732a6SHans de Goede goto unlock; 490990732a6SHans de Goede } 491990732a6SHans de Goede } else { 4923ee47cadSRui Miguel Silva ret = ov2680_stream_disable(sensor); 493990732a6SHans de Goede pm_runtime_put(sensor->sd.dev); 494990732a6SHans de Goede } 4953ee47cadSRui Miguel Silva 4963ee47cadSRui Miguel Silva sensor->is_streaming = !!enable; 4973ee47cadSRui Miguel Silva 4983ee47cadSRui Miguel Silva unlock: 4993ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5003ee47cadSRui Miguel Silva 5013ee47cadSRui Miguel Silva return ret; 5023ee47cadSRui Miguel Silva } 5033ee47cadSRui Miguel Silva 5043ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 5050d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5063ee47cadSRui Miguel Silva struct v4l2_subdev_mbus_code_enum *code) 5073ee47cadSRui Miguel Silva { 5083ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5093ee47cadSRui Miguel Silva 5103ee47cadSRui Miguel Silva if (code->pad != 0 || code->index != 0) 5113ee47cadSRui Miguel Silva return -EINVAL; 5123ee47cadSRui Miguel Silva 513f614dfb8SHans de Goede code->code = sensor->mode.fmt.code; 5143ee47cadSRui Miguel Silva 5153ee47cadSRui Miguel Silva return 0; 5163ee47cadSRui Miguel Silva } 5173ee47cadSRui Miguel Silva 5183ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd, 5190d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5203ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 5213ee47cadSRui Miguel Silva { 5223ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 523*23321b91SHans de Goede struct v4l2_mbus_framefmt *fmt; 5243ee47cadSRui Miguel Silva 5253ee47cadSRui Miguel Silva if (format->pad != 0) 5263ee47cadSRui Miguel Silva return -EINVAL; 5273ee47cadSRui Miguel Silva 528*23321b91SHans de Goede fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, 529*23321b91SHans de Goede format->which); 530*23321b91SHans de Goede 5313ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5323ee47cadSRui Miguel Silva format->format = *fmt; 5333ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5343ee47cadSRui Miguel Silva 53549c282d5SHans de Goede return 0; 5363ee47cadSRui Miguel Silva } 5373ee47cadSRui Miguel Silva 5383ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd, 5390d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5403ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 5413ee47cadSRui Miguel Silva { 5423ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5433ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *try_fmt; 54463f47529SHans de Goede unsigned int width, height; 5453ee47cadSRui Miguel Silva int ret = 0; 5463ee47cadSRui Miguel Silva 5473ee47cadSRui Miguel Silva if (format->pad != 0) 5483ee47cadSRui Miguel Silva return -EINVAL; 5493ee47cadSRui Miguel Silva 55063f47529SHans de Goede width = min_t(unsigned int, ALIGN(format->format.width, 2), 55163f47529SHans de Goede OV2680_NATIVE_WIDTH); 55263f47529SHans de Goede height = min_t(unsigned int, ALIGN(format->format.height, 2), 55363f47529SHans de Goede OV2680_NATIVE_HEIGHT); 5543ee47cadSRui Miguel Silva 55563f47529SHans de Goede ov2680_fill_format(sensor, &format->format, width, height); 556c0e97a4bSHans de Goede 5573ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 5580d346d2aSTomi Valkeinen try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0); 559c0e97a4bSHans de Goede *try_fmt = format->format; 560e521b9ccSHans de Goede return 0; 561e521b9ccSHans de Goede } 562e521b9ccSHans de Goede 563e521b9ccSHans de Goede mutex_lock(&sensor->lock); 564e521b9ccSHans de Goede 565e521b9ccSHans de Goede if (sensor->is_streaming) { 566e521b9ccSHans de Goede ret = -EBUSY; 5673ee47cadSRui Miguel Silva goto unlock; 5683ee47cadSRui Miguel Silva } 5693ee47cadSRui Miguel Silva 570f614dfb8SHans de Goede sensor->mode.fmt = format->format; 57163f47529SHans de Goede ov2680_calc_mode(sensor); 5723ee47cadSRui Miguel Silva 5733ee47cadSRui Miguel Silva unlock: 5743ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5753ee47cadSRui Miguel Silva 5763ee47cadSRui Miguel Silva return ret; 5773ee47cadSRui Miguel Silva } 5783ee47cadSRui Miguel Silva 5793ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd, 5800d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 5813ee47cadSRui Miguel Silva { 5826d6849b2SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 5833ee47cadSRui Miguel Silva 5846d6849b2SHans de Goede ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt, 5856d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 5866d6849b2SHans de Goede return 0; 5873ee47cadSRui Miguel Silva } 5883ee47cadSRui Miguel Silva 5893ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 5900d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5913ee47cadSRui Miguel Silva struct v4l2_subdev_frame_size_enum *fse) 5923ee47cadSRui Miguel Silva { 59363f47529SHans de Goede if (fse->index >= OV2680_FRAME_SIZES) 5943ee47cadSRui Miguel Silva return -EINVAL; 5953ee47cadSRui Miguel Silva 59663f47529SHans de Goede fse->min_width = OV2680_ACTIVE_WIDTH / (fse->index + 1); 59763f47529SHans de Goede fse->min_height = OV2680_ACTIVE_HEIGHT / (fse->index + 1); 59863f47529SHans de Goede fse->max_width = fse->min_width; 59963f47529SHans de Goede fse->max_height = fse->min_height; 6003ee47cadSRui Miguel Silva 6013ee47cadSRui Miguel Silva return 0; 6023ee47cadSRui Miguel Silva } 6033ee47cadSRui Miguel Silva 60463f47529SHans de Goede static bool ov2680_valid_frame_size(struct v4l2_subdev *sd, 60563f47529SHans de Goede struct v4l2_subdev_state *sd_state, 60663f47529SHans de Goede struct v4l2_subdev_frame_interval_enum *fie) 6074007015eSHans de Goede { 60863f47529SHans de Goede struct v4l2_subdev_frame_size_enum fse = { 60963f47529SHans de Goede .pad = fie->pad, 61063f47529SHans de Goede .which = fie->which, 61163f47529SHans de Goede }; 6124007015eSHans de Goede int i; 6134007015eSHans de Goede 61463f47529SHans de Goede for (i = 0; i < OV2680_FRAME_SIZES; i++) { 61563f47529SHans de Goede fse.index = i; 61663f47529SHans de Goede 61763f47529SHans de Goede if (ov2680_enum_frame_size(sd, sd_state, &fse)) 61863f47529SHans de Goede return false; 61963f47529SHans de Goede 62063f47529SHans de Goede if (fie->width == fse.min_width && 62163f47529SHans de Goede fie->height == fse.min_height) 6224007015eSHans de Goede return true; 6234007015eSHans de Goede } 6244007015eSHans de Goede 6254007015eSHans de Goede return false; 6264007015eSHans de Goede } 6274007015eSHans de Goede 6283ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 6290d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6303ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval_enum *fie) 6313ee47cadSRui Miguel Silva { 6324007015eSHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 6333ee47cadSRui Miguel Silva 6344007015eSHans de Goede /* Only 1 framerate */ 63563f47529SHans de Goede if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie)) 6363ee47cadSRui Miguel Silva return -EINVAL; 6373ee47cadSRui Miguel Silva 638f614dfb8SHans de Goede fie->interval = sensor->mode.frame_interval; 6393ee47cadSRui Miguel Silva 6403ee47cadSRui Miguel Silva return 0; 6413ee47cadSRui Miguel Silva } 6423ee47cadSRui Miguel Silva 6433ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 6443ee47cadSRui Miguel Silva { 6453ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 6463ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 64737f7e57eSHans de Goede int ret; 6483ee47cadSRui Miguel Silva 64937f7e57eSHans de Goede /* Only apply changes to the controls if the device is powered up */ 65037f7e57eSHans de Goede if (!pm_runtime_get_if_in_use(sensor->sd.dev)) { 651f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 6523ee47cadSRui Miguel Silva return 0; 65337f7e57eSHans de Goede } 6543ee47cadSRui Miguel Silva 6553ee47cadSRui Miguel Silva switch (ctrl->id) { 6563ee47cadSRui Miguel Silva case V4L2_CID_GAIN: 65737f7e57eSHans de Goede ret = ov2680_gain_set(sensor, ctrl->val); 65837f7e57eSHans de Goede break; 6593ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 66037f7e57eSHans de Goede ret = ov2680_exposure_set(sensor, ctrl->val); 66137f7e57eSHans de Goede break; 6623ee47cadSRui Miguel Silva case V4L2_CID_VFLIP: 66337f7e57eSHans de Goede ret = ov2680_set_vflip(sensor, ctrl->val); 66437f7e57eSHans de Goede break; 6653ee47cadSRui Miguel Silva case V4L2_CID_HFLIP: 66637f7e57eSHans de Goede ret = ov2680_set_hflip(sensor, ctrl->val); 66737f7e57eSHans de Goede break; 6683ee47cadSRui Miguel Silva case V4L2_CID_TEST_PATTERN: 66937f7e57eSHans de Goede ret = ov2680_test_pattern_set(sensor, ctrl->val); 67037f7e57eSHans de Goede break; 6713ee47cadSRui Miguel Silva default: 67237f7e57eSHans de Goede ret = -EINVAL; 6733ee47cadSRui Miguel Silva break; 6743ee47cadSRui Miguel Silva } 6753ee47cadSRui Miguel Silva 67637f7e57eSHans de Goede pm_runtime_put(sensor->sd.dev); 67737f7e57eSHans de Goede return ret; 6783ee47cadSRui Miguel Silva } 6793ee47cadSRui Miguel Silva 6803ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 6813ee47cadSRui Miguel Silva .s_ctrl = ov2680_s_ctrl, 6823ee47cadSRui Miguel Silva }; 6833ee47cadSRui Miguel Silva 6843ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = { 6853ee47cadSRui Miguel Silva .g_frame_interval = ov2680_s_g_frame_interval, 6863ee47cadSRui Miguel Silva .s_frame_interval = ov2680_s_g_frame_interval, 6873ee47cadSRui Miguel Silva .s_stream = ov2680_s_stream, 6883ee47cadSRui Miguel Silva }; 6893ee47cadSRui Miguel Silva 6903ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 6913ee47cadSRui Miguel Silva .init_cfg = ov2680_init_cfg, 6923ee47cadSRui Miguel Silva .enum_mbus_code = ov2680_enum_mbus_code, 6933ee47cadSRui Miguel Silva .get_fmt = ov2680_get_fmt, 6943ee47cadSRui Miguel Silva .set_fmt = ov2680_set_fmt, 6953ee47cadSRui Miguel Silva .enum_frame_size = ov2680_enum_frame_size, 6963ee47cadSRui Miguel Silva .enum_frame_interval = ov2680_enum_frame_interval, 6973ee47cadSRui Miguel Silva }; 6983ee47cadSRui Miguel Silva 6993ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = { 7003ee47cadSRui Miguel Silva .video = &ov2680_video_ops, 7013ee47cadSRui Miguel Silva .pad = &ov2680_pad_ops, 7023ee47cadSRui Miguel Silva }; 7033ee47cadSRui Miguel Silva 7043ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor) 7053ee47cadSRui Miguel Silva { 7063ee47cadSRui Miguel Silva /* set initial mode */ 707f614dfb8SHans de Goede ov2680_fill_format(sensor, &sensor->mode.fmt, 7086d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 70963f47529SHans de Goede ov2680_calc_mode(sensor); 7103ee47cadSRui Miguel Silva 711f614dfb8SHans de Goede sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE; 712f614dfb8SHans de Goede sensor->mode.frame_interval.numerator = 1; 7133ee47cadSRui Miguel Silva 7143ee47cadSRui Miguel Silva return 0; 7153ee47cadSRui Miguel Silva } 7163ee47cadSRui Miguel Silva 717e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor) 7183ee47cadSRui Miguel Silva { 7197adfdecbSHans de Goede struct i2c_client *client = to_i2c_client(sensor->dev); 7203ee47cadSRui Miguel Silva const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 7213ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 7223ee47cadSRui Miguel Silva struct v4l2_ctrl_handler *hdl = &ctrls->handler; 7233ee47cadSRui Miguel Silva int ret = 0; 7243ee47cadSRui Miguel Silva 7257adfdecbSHans de Goede v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops); 7263ee47cadSRui Miguel Silva 7273ee47cadSRui Miguel Silva sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 7283ee47cadSRui Miguel Silva sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 7293ee47cadSRui Miguel Silva sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 7303ee47cadSRui Miguel Silva 7313ee47cadSRui Miguel Silva ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 7323ee47cadSRui Miguel Silva if (ret < 0) 7333ee47cadSRui Miguel Silva return ret; 7343ee47cadSRui Miguel Silva 7357b5a42e6SHans de Goede v4l2_ctrl_handler_init(hdl, 5); 7363ee47cadSRui Miguel Silva 7373ee47cadSRui Miguel Silva hdl->lock = &sensor->lock; 7383ee47cadSRui Miguel Silva 7393ee47cadSRui Miguel Silva ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 7403ee47cadSRui Miguel Silva ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 7413ee47cadSRui Miguel Silva 7423ee47cadSRui Miguel Silva ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 7433ee47cadSRui Miguel Silva &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 7443ee47cadSRui Miguel Silva ARRAY_SIZE(test_pattern_menu) - 1, 7453ee47cadSRui Miguel Silva 0, 0, test_pattern_menu); 7463ee47cadSRui Miguel Silva 7473ee47cadSRui Miguel Silva ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 7483ee47cadSRui Miguel Silva 0, 32767, 1, 0); 7493ee47cadSRui Miguel Silva 7503ee47cadSRui Miguel Silva ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0); 7513ee47cadSRui Miguel Silva 7523ee47cadSRui Miguel Silva if (hdl->error) { 7533ee47cadSRui Miguel Silva ret = hdl->error; 7543ee47cadSRui Miguel Silva goto cleanup_entity; 7553ee47cadSRui Miguel Silva } 7563ee47cadSRui Miguel Silva 75766274280SDave Stevenson ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 75866274280SDave Stevenson ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 7593ee47cadSRui Miguel Silva 7603ee47cadSRui Miguel Silva sensor->sd.ctrl_handler = hdl; 7613ee47cadSRui Miguel Silva 7623ee47cadSRui Miguel Silva ret = v4l2_async_register_subdev(&sensor->sd); 7633ee47cadSRui Miguel Silva if (ret < 0) 7643ee47cadSRui Miguel Silva goto cleanup_entity; 7653ee47cadSRui Miguel Silva 7663ee47cadSRui Miguel Silva return 0; 7673ee47cadSRui Miguel Silva 7683ee47cadSRui Miguel Silva cleanup_entity: 7693ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 7703ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(hdl); 7713ee47cadSRui Miguel Silva 7723ee47cadSRui Miguel Silva return ret; 7733ee47cadSRui Miguel Silva } 7743ee47cadSRui Miguel Silva 7753ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor) 7763ee47cadSRui Miguel Silva { 7773ee47cadSRui Miguel Silva int i; 7783ee47cadSRui Miguel Silva 7793ee47cadSRui Miguel Silva for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 7803ee47cadSRui Miguel Silva sensor->supplies[i].supply = ov2680_supply_name[i]; 7813ee47cadSRui Miguel Silva 7827adfdecbSHans de Goede return devm_regulator_bulk_get(sensor->dev, 7837adfdecbSHans de Goede OV2680_NUM_SUPPLIES, sensor->supplies); 7843ee47cadSRui Miguel Silva } 7853ee47cadSRui Miguel Silva 7863ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor) 7873ee47cadSRui Miguel Silva { 7889289998eSHans de Goede u64 chip_id; 7893ee47cadSRui Miguel Silva int ret; 7903ee47cadSRui Miguel Silva 7919289998eSHans de Goede ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL); 7923ee47cadSRui Miguel Silva if (ret < 0) { 7937adfdecbSHans de Goede dev_err(sensor->dev, "failed to read chip id\n"); 7943ee47cadSRui Miguel Silva return -ENODEV; 7953ee47cadSRui Miguel Silva } 7963ee47cadSRui Miguel Silva 7973ee47cadSRui Miguel Silva if (chip_id != OV2680_CHIP_ID) { 7987adfdecbSHans de Goede dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n", 7993ee47cadSRui Miguel Silva chip_id, OV2680_CHIP_ID); 8003ee47cadSRui Miguel Silva return -ENODEV; 8013ee47cadSRui Miguel Silva } 8023ee47cadSRui Miguel Silva 8033ee47cadSRui Miguel Silva return 0; 8043ee47cadSRui Miguel Silva } 8053ee47cadSRui Miguel Silva 806913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor) 8073ee47cadSRui Miguel Silva { 8087adfdecbSHans de Goede struct device *dev = sensor->dev; 80983634470SHans de Goede struct fwnode_handle *ep_fwnode; 810e9305a23SHans de Goede struct gpio_desc *gpio; 811ec7dfad5SHans de Goede unsigned int rate = 0; 8128e50a122SHans de Goede int i, ret; 8133ee47cadSRui Miguel Silva 814e9305a23SHans de Goede /* 81583634470SHans de Goede * Sometimes the fwnode graph is initialized by the bridge driver. 81683634470SHans de Goede * Bridge drivers doing this may also add GPIO mappings, wait for this. 81783634470SHans de Goede */ 81883634470SHans de Goede ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 81983634470SHans de Goede if (!ep_fwnode) 82083634470SHans de Goede return dev_err_probe(dev, -EPROBE_DEFER, 82183634470SHans de Goede "waiting for fwnode graph endpoint\n"); 82283634470SHans de Goede 82383634470SHans de Goede fwnode_handle_put(ep_fwnode); 82483634470SHans de Goede 82583634470SHans de Goede /* 826e9305a23SHans de Goede * The pin we want is named XSHUTDN in the datasheet. Linux sensor 827e9305a23SHans de Goede * drivers have standardized on using "powerdown" as con-id name 828e9305a23SHans de Goede * for powerdown or shutdown pins. Older DTB files use "reset", 829e9305a23SHans de Goede * so fallback to that if there is no "powerdown" pin. 830e9305a23SHans de Goede */ 831e9305a23SHans de Goede gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH); 832e9305a23SHans de Goede if (!gpio) 833e9305a23SHans de Goede gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 834e9305a23SHans de Goede 835e9305a23SHans de Goede ret = PTR_ERR_OR_ZERO(gpio); 8363ee47cadSRui Miguel Silva if (ret < 0) { 8373ee47cadSRui Miguel Silva dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 8383ee47cadSRui Miguel Silva return ret; 8393ee47cadSRui Miguel Silva } 8403ee47cadSRui Miguel Silva 841e9305a23SHans de Goede sensor->pwdn_gpio = gpio; 842e9305a23SHans de Goede 843ec7dfad5SHans de Goede sensor->xvclk = devm_clk_get_optional(dev, "xvclk"); 8443ee47cadSRui Miguel Silva if (IS_ERR(sensor->xvclk)) { 8453ee47cadSRui Miguel Silva dev_err(dev, "xvclk clock missing or invalid\n"); 8463ee47cadSRui Miguel Silva return PTR_ERR(sensor->xvclk); 8473ee47cadSRui Miguel Silva } 8483ee47cadSRui Miguel Silva 849ec7dfad5SHans de Goede /* 850ec7dfad5SHans de Goede * We could have either a 24MHz or 19.2MHz clock rate from either DT or 851ec7dfad5SHans de Goede * ACPI... but we also need to support the weird IPU3 case which will 852ec7dfad5SHans de Goede * have an external clock AND a clock-frequency property. Check for the 853ec7dfad5SHans de Goede * clock-frequency property and if found, set that rate if we managed 854ec7dfad5SHans de Goede * to acquire a clock. This should cover the ACPI case. If the system 855ec7dfad5SHans de Goede * uses devicetree then the configured rate should already be set, so 856ec7dfad5SHans de Goede * we can just read it. 857ec7dfad5SHans de Goede */ 858ec7dfad5SHans de Goede ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", 859ec7dfad5SHans de Goede &rate); 860ec7dfad5SHans de Goede if (ret && !sensor->xvclk) 861ec7dfad5SHans de Goede return dev_err_probe(dev, ret, "invalid clock config\n"); 862ec7dfad5SHans de Goede 863ec7dfad5SHans de Goede if (!ret && sensor->xvclk) { 864ec7dfad5SHans de Goede ret = clk_set_rate(sensor->xvclk, rate); 865ec7dfad5SHans de Goede if (ret) 866ec7dfad5SHans de Goede return dev_err_probe(dev, ret, 867ec7dfad5SHans de Goede "failed to set clock rate\n"); 868ec7dfad5SHans de Goede } 869ec7dfad5SHans de Goede 870ec7dfad5SHans de Goede sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk); 8718e50a122SHans de Goede 8728e50a122SHans de Goede for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) { 8738e50a122SHans de Goede if (sensor->xvclk_freq == ov2680_xvclk_freqs[i]) 8748e50a122SHans de Goede break; 8753ee47cadSRui Miguel Silva } 8763ee47cadSRui Miguel Silva 8778e50a122SHans de Goede if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) 8788e50a122SHans de Goede return dev_err_probe(dev, -EINVAL, 8798e50a122SHans de Goede "unsupported xvclk frequency %d Hz\n", 8808e50a122SHans de Goede sensor->xvclk_freq); 8818e50a122SHans de Goede 8828e50a122SHans de Goede sensor->pll_mult = ov2680_pll_multipliers[i]; 8838e50a122SHans de Goede 8843ee47cadSRui Miguel Silva return 0; 8853ee47cadSRui Miguel Silva } 8863ee47cadSRui Miguel Silva 8873ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client) 8883ee47cadSRui Miguel Silva { 8893ee47cadSRui Miguel Silva struct device *dev = &client->dev; 8903ee47cadSRui Miguel Silva struct ov2680_dev *sensor; 8913ee47cadSRui Miguel Silva int ret; 8923ee47cadSRui Miguel Silva 8933ee47cadSRui Miguel Silva sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 8943ee47cadSRui Miguel Silva if (!sensor) 8953ee47cadSRui Miguel Silva return -ENOMEM; 8963ee47cadSRui Miguel Silva 8977adfdecbSHans de Goede sensor->dev = &client->dev; 8983ee47cadSRui Miguel Silva 8999289998eSHans de Goede sensor->regmap = devm_cci_regmap_init_i2c(client, 16); 9009289998eSHans de Goede if (IS_ERR(sensor->regmap)) 9019289998eSHans de Goede return PTR_ERR(sensor->regmap); 9029289998eSHans de Goede 903913c55a8SChristophe JAILLET ret = ov2680_parse_dt(sensor); 9043ee47cadSRui Miguel Silva if (ret < 0) 90583634470SHans de Goede return ret; 9063ee47cadSRui Miguel Silva 9073ee47cadSRui Miguel Silva ret = ov2680_mode_init(sensor); 9083ee47cadSRui Miguel Silva if (ret < 0) 9093ee47cadSRui Miguel Silva return ret; 9103ee47cadSRui Miguel Silva 9113ee47cadSRui Miguel Silva ret = ov2680_get_regulators(sensor); 9123ee47cadSRui Miguel Silva if (ret < 0) { 9133ee47cadSRui Miguel Silva dev_err(dev, "failed to get regulators\n"); 9143ee47cadSRui Miguel Silva return ret; 9153ee47cadSRui Miguel Silva } 9163ee47cadSRui Miguel Silva 9173ee47cadSRui Miguel Silva mutex_init(&sensor->lock); 9183ee47cadSRui Miguel Silva 919990732a6SHans de Goede /* 920990732a6SHans de Goede * Power up and verify the chip now, so that if runtime pm is 921990732a6SHans de Goede * disabled the chip is left on and streaming will work. 922990732a6SHans de Goede */ 923990732a6SHans de Goede ret = ov2680_power_on(sensor); 9243ee47cadSRui Miguel Silva if (ret < 0) 9253ee47cadSRui Miguel Silva goto lock_destroy; 9263ee47cadSRui Miguel Silva 927990732a6SHans de Goede ret = ov2680_check_id(sensor); 928990732a6SHans de Goede if (ret < 0) 929990732a6SHans de Goede goto err_powerdown; 930990732a6SHans de Goede 931990732a6SHans de Goede pm_runtime_set_active(&client->dev); 932990732a6SHans de Goede pm_runtime_get_noresume(&client->dev); 933990732a6SHans de Goede pm_runtime_enable(&client->dev); 934990732a6SHans de Goede 935e7f4861dSJavier Martinez Canillas ret = ov2680_v4l2_register(sensor); 9363ee47cadSRui Miguel Silva if (ret < 0) 937990732a6SHans de Goede goto err_pm_runtime; 938990732a6SHans de Goede 939990732a6SHans de Goede pm_runtime_set_autosuspend_delay(&client->dev, 1000); 940990732a6SHans de Goede pm_runtime_use_autosuspend(&client->dev); 941990732a6SHans de Goede pm_runtime_put_autosuspend(&client->dev); 9423ee47cadSRui Miguel Silva 9433ee47cadSRui Miguel Silva dev_info(dev, "ov2680 init correctly\n"); 9443ee47cadSRui Miguel Silva 9453ee47cadSRui Miguel Silva return 0; 9463ee47cadSRui Miguel Silva 947990732a6SHans de Goede err_pm_runtime: 948990732a6SHans de Goede pm_runtime_disable(&client->dev); 949990732a6SHans de Goede pm_runtime_put_noidle(&client->dev); 950990732a6SHans de Goede err_powerdown: 951990732a6SHans de Goede ov2680_power_off(sensor); 9523ee47cadSRui Miguel Silva lock_destroy: 953b7a41762SJavier Martinez Canillas dev_err(dev, "ov2680 init fail: %d\n", ret); 9543ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9553ee47cadSRui Miguel Silva 9563ee47cadSRui Miguel Silva return ret; 9573ee47cadSRui Miguel Silva } 9583ee47cadSRui Miguel Silva 959ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client) 9603ee47cadSRui Miguel Silva { 9613ee47cadSRui Miguel Silva struct v4l2_subdev *sd = i2c_get_clientdata(client); 9623ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9633ee47cadSRui Miguel Silva 9643ee47cadSRui Miguel Silva v4l2_async_unregister_subdev(&sensor->sd); 9653ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9663ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 9673ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(&sensor->ctrls.handler); 968990732a6SHans de Goede 969990732a6SHans de Goede /* 970990732a6SHans de Goede * Disable runtime PM. In case runtime PM is disabled in the kernel, 971990732a6SHans de Goede * make sure to turn power off manually. 972990732a6SHans de Goede */ 973990732a6SHans de Goede pm_runtime_disable(&client->dev); 974990732a6SHans de Goede if (!pm_runtime_status_suspended(&client->dev)) 975990732a6SHans de Goede ov2680_power_off(sensor); 976990732a6SHans de Goede pm_runtime_set_suspended(&client->dev); 9773ee47cadSRui Miguel Silva } 9783ee47cadSRui Miguel Silva 979990732a6SHans de Goede static int ov2680_suspend(struct device *dev) 9803ee47cadSRui Miguel Silva { 9817519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9823ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9833ee47cadSRui Miguel Silva 9843ee47cadSRui Miguel Silva if (sensor->is_streaming) 9853ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 9863ee47cadSRui Miguel Silva 987990732a6SHans de Goede return ov2680_power_off(sensor); 9883ee47cadSRui Miguel Silva } 9893ee47cadSRui Miguel Silva 990990732a6SHans de Goede static int ov2680_resume(struct device *dev) 9913ee47cadSRui Miguel Silva { 9927519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9933ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9943ee47cadSRui Miguel Silva int ret; 9953ee47cadSRui Miguel Silva 996990732a6SHans de Goede ret = ov2680_power_on(sensor); 997990732a6SHans de Goede if (ret < 0) 998990732a6SHans de Goede goto stream_disable; 999990732a6SHans de Goede 10003ee47cadSRui Miguel Silva if (sensor->is_streaming) { 10013ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 10023ee47cadSRui Miguel Silva if (ret < 0) 10033ee47cadSRui Miguel Silva goto stream_disable; 10043ee47cadSRui Miguel Silva } 10053ee47cadSRui Miguel Silva 10063ee47cadSRui Miguel Silva return 0; 10073ee47cadSRui Miguel Silva 10083ee47cadSRui Miguel Silva stream_disable: 10093ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 10103ee47cadSRui Miguel Silva sensor->is_streaming = false; 10113ee47cadSRui Miguel Silva 10123ee47cadSRui Miguel Silva return ret; 10133ee47cadSRui Miguel Silva } 10143ee47cadSRui Miguel Silva 1015990732a6SHans de Goede static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, 1016990732a6SHans de Goede NULL); 10173ee47cadSRui Miguel Silva 10183ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = { 10193ee47cadSRui Miguel Silva { .compatible = "ovti,ov2680" }, 10203ee47cadSRui Miguel Silva { /* sentinel */ }, 10213ee47cadSRui Miguel Silva }; 10223ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 10233ee47cadSRui Miguel Silva 1024df3ecab8SHans de Goede static const struct acpi_device_id ov2680_acpi_ids[] = { 1025df3ecab8SHans de Goede { "OVTI2680" }, 1026df3ecab8SHans de Goede { /* sentinel */ } 1027df3ecab8SHans de Goede }; 1028df3ecab8SHans de Goede MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids); 1029df3ecab8SHans de Goede 10303ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = { 10313ee47cadSRui Miguel Silva .driver = { 10323ee47cadSRui Miguel Silva .name = "ov2680", 1033990732a6SHans de Goede .pm = pm_sleep_ptr(&ov2680_pm_ops), 103414155c4fSKrzysztof Kozlowski .of_match_table = ov2680_dt_ids, 1035df3ecab8SHans de Goede .acpi_match_table = ov2680_acpi_ids, 10363ee47cadSRui Miguel Silva }, 1037aaeb31c0SUwe Kleine-König .probe = ov2680_probe, 10383ee47cadSRui Miguel Silva .remove = ov2680_remove, 10393ee47cadSRui Miguel Silva }; 10403ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver); 10413ee47cadSRui Miguel Silva 10423ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 10433ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 10443ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2"); 1045