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> 28*63b0cd30SHans de Goede #include <media/v4l2-fwnode.h> 293ee47cadSRui Miguel Silva #include <media/v4l2-subdev.h> 303ee47cadSRui Miguel Silva 313ee47cadSRui Miguel Silva #define OV2680_CHIP_ID 0x2680 323ee47cadSRui Miguel Silva 339289998eSHans de Goede #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100) 349289998eSHans de Goede #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103) 353ee47cadSRui Miguel Silva 369289998eSHans de Goede #define OV2680_REG_CHIP_ID CCI_REG16(0x300a) 37a99a041cSHans de Goede #define OV2680_REG_SC_CMMN_SUB_ID CCI_REG8(0x302a) 388e50a122SHans de Goede #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081) 393ee47cadSRui Miguel Silva 409289998eSHans de Goede #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500) 419289998eSHans de Goede #define OV2680_REG_R_MANUAL CCI_REG8(0x3503) 429289998eSHans de Goede #define OV2680_REG_GAIN_PK CCI_REG16(0x350a) 4363f47529SHans de Goede 4463f47529SHans de Goede #define OV2680_REG_SENSOR_CTRL_0A CCI_REG8(0x370a) 4563f47529SHans de Goede 4663f47529SHans de Goede #define OV2680_REG_HORIZONTAL_START CCI_REG16(0x3800) 4763f47529SHans de Goede #define OV2680_REG_VERTICAL_START CCI_REG16(0x3802) 4863f47529SHans de Goede #define OV2680_REG_HORIZONTAL_END CCI_REG16(0x3804) 4963f47529SHans de Goede #define OV2680_REG_VERTICAL_END CCI_REG16(0x3806) 5063f47529SHans de Goede #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE CCI_REG16(0x3808) 5163f47529SHans de Goede #define OV2680_REG_VERTICAL_OUTPUT_SIZE CCI_REG16(0x380a) 529289998eSHans de Goede #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c) 539289998eSHans de Goede #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e) 5463f47529SHans de Goede #define OV2680_REG_ISP_X_WIN CCI_REG16(0x3810) 5563f47529SHans de Goede #define OV2680_REG_ISP_Y_WIN CCI_REG16(0x3812) 5663f47529SHans de Goede #define OV2680_REG_X_INC CCI_REG8(0x3814) 5763f47529SHans de Goede #define OV2680_REG_Y_INC CCI_REG8(0x3815) 589289998eSHans de Goede #define OV2680_REG_FORMAT1 CCI_REG8(0x3820) 599289998eSHans de Goede #define OV2680_REG_FORMAT2 CCI_REG8(0x3821) 603ee47cadSRui Miguel Silva 619289998eSHans de Goede #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080) 623ee47cadSRui Miguel Silva 6363f47529SHans de Goede #define OV2680_REG_X_WIN CCI_REG16(0x5704) 6463f47529SHans de Goede #define OV2680_REG_Y_WIN CCI_REG16(0x5706) 6563f47529SHans de Goede 663ee47cadSRui Miguel Silva #define OV2680_FRAME_RATE 30 673ee47cadSRui Miguel Silva 6863f47529SHans de Goede #define OV2680_NATIVE_WIDTH 1616 6963f47529SHans de Goede #define OV2680_NATIVE_HEIGHT 1216 703b378b35SHans de Goede #define OV2680_NATIVE_START_LEFT 0 713b378b35SHans de Goede #define OV2680_NATIVE_START_TOP 0 7263f47529SHans de Goede #define OV2680_ACTIVE_WIDTH 1600 7363f47529SHans de Goede #define OV2680_ACTIVE_HEIGHT 1200 743b378b35SHans de Goede #define OV2680_ACTIVE_START_LEFT 8 753b378b35SHans de Goede #define OV2680_ACTIVE_START_TOP 8 763b378b35SHans de Goede #define OV2680_MIN_CROP_WIDTH 2 773b378b35SHans de Goede #define OV2680_MIN_CROP_HEIGHT 2 7863f47529SHans de Goede 7934f9eff2SHans de Goede /* Fixed pre-div of 1/2 */ 8034f9eff2SHans de Goede #define OV2680_PLL_PREDIV0 2 8134f9eff2SHans de Goede 8234f9eff2SHans de Goede /* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */ 8334f9eff2SHans de Goede #define OV2680_PLL_PREDIV 2 8434f9eff2SHans de Goede 8563f47529SHans de Goede /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */ 8663f47529SHans de Goede #define OV2680_PIXELS_PER_LINE 1704 8763f47529SHans de Goede #define OV2680_LINES_PER_FRAME 1294 8863f47529SHans de Goede 8963f47529SHans de Goede /* If possible send 16 extra rows / lines to the ISP as padding */ 9063f47529SHans de Goede #define OV2680_END_MARGIN 16 913ee47cadSRui Miguel Silva 9205d6bd86SHans de Goede /* Max exposure time is VTS - 8 */ 9305d6bd86SHans de Goede #define OV2680_INTEGRATION_TIME_MARGIN 8 9405d6bd86SHans de Goede 956d6849b2SHans de Goede #define OV2680_DEFAULT_WIDTH 800 966d6849b2SHans de Goede #define OV2680_DEFAULT_HEIGHT 600 976d6849b2SHans de Goede 9863f47529SHans de Goede /* For enum_frame_size() full-size + binned-/quarter-size */ 9963f47529SHans de Goede #define OV2680_FRAME_SIZES 2 1003ee47cadSRui Miguel Silva 1013ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = { 1023ee47cadSRui Miguel Silva "DOVDD", 1033ee47cadSRui Miguel Silva "DVDD", 1043ee47cadSRui Miguel Silva "AVDD", 1053ee47cadSRui Miguel Silva }; 1063ee47cadSRui Miguel Silva 1073ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name) 1083ee47cadSRui Miguel Silva 1098e50a122SHans de Goede enum { 1108e50a122SHans de Goede OV2680_19_2_MHZ, 1118e50a122SHans de Goede OV2680_24_MHZ, 1128e50a122SHans de Goede }; 1138e50a122SHans de Goede 1148e50a122SHans de Goede static const unsigned long ov2680_xvclk_freqs[] = { 1158e50a122SHans de Goede [OV2680_19_2_MHZ] = 19200000, 1168e50a122SHans de Goede [OV2680_24_MHZ] = 24000000, 1178e50a122SHans de Goede }; 1188e50a122SHans de Goede 1198e50a122SHans de Goede static const u8 ov2680_pll_multipliers[] = { 1208e50a122SHans de Goede [OV2680_19_2_MHZ] = 69, 1218e50a122SHans de Goede [OV2680_24_MHZ] = 55, 1228e50a122SHans de Goede }; 1238e50a122SHans de Goede 1243ee47cadSRui Miguel Silva struct ov2680_ctrls { 1253ee47cadSRui Miguel Silva struct v4l2_ctrl_handler handler; 1263ee47cadSRui Miguel Silva struct v4l2_ctrl *exposure; 1273ee47cadSRui Miguel Silva struct v4l2_ctrl *gain; 1283ee47cadSRui Miguel Silva struct v4l2_ctrl *hflip; 1293ee47cadSRui Miguel Silva struct v4l2_ctrl *vflip; 1303ee47cadSRui Miguel Silva struct v4l2_ctrl *test_pattern; 13134f9eff2SHans de Goede struct v4l2_ctrl *link_freq; 13234f9eff2SHans de Goede struct v4l2_ctrl *pixel_rate; 1333ee47cadSRui Miguel Silva }; 1343ee47cadSRui Miguel Silva 135f614dfb8SHans de Goede struct ov2680_mode { 1363b378b35SHans de Goede struct v4l2_rect crop; 137f614dfb8SHans de Goede struct v4l2_mbus_framefmt fmt; 138f614dfb8SHans de Goede struct v4l2_fract frame_interval; 13963f47529SHans de Goede bool binning; 14063f47529SHans de Goede u16 h_start; 14163f47529SHans de Goede u16 v_start; 14263f47529SHans de Goede u16 h_end; 14363f47529SHans de Goede u16 v_end; 14463f47529SHans de Goede u16 h_output_size; 14563f47529SHans de Goede u16 v_output_size; 14663f47529SHans de Goede u16 hts; 14763f47529SHans de Goede u16 vts; 148f614dfb8SHans de Goede }; 149f614dfb8SHans de Goede 1503ee47cadSRui Miguel Silva struct ov2680_dev { 1517adfdecbSHans de Goede struct device *dev; 1529289998eSHans de Goede struct regmap *regmap; 1533ee47cadSRui Miguel Silva struct v4l2_subdev sd; 1543ee47cadSRui Miguel Silva 1553ee47cadSRui Miguel Silva struct media_pad pad; 1563ee47cadSRui Miguel Silva struct clk *xvclk; 1573ee47cadSRui Miguel Silva u32 xvclk_freq; 1588e50a122SHans de Goede u8 pll_mult; 15934f9eff2SHans de Goede s64 link_freq[1]; 16034f9eff2SHans de Goede u64 pixel_rate; 1613ee47cadSRui Miguel Silva struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 1623ee47cadSRui Miguel Silva 163e9305a23SHans de Goede struct gpio_desc *pwdn_gpio; 1643ee47cadSRui Miguel Silva struct mutex lock; /* protect members */ 1653ee47cadSRui Miguel Silva 1663ee47cadSRui Miguel Silva bool is_streaming; 1673ee47cadSRui Miguel Silva 1683ee47cadSRui Miguel Silva struct ov2680_ctrls ctrls; 169f614dfb8SHans de Goede struct ov2680_mode mode; 1703ee47cadSRui Miguel Silva }; 1713ee47cadSRui Miguel Silva 1723b378b35SHans de Goede static const struct v4l2_rect ov2680_default_crop = { 1733b378b35SHans de Goede .left = OV2680_ACTIVE_START_LEFT, 1743b378b35SHans de Goede .top = OV2680_ACTIVE_START_TOP, 1753b378b35SHans de Goede .width = OV2680_ACTIVE_WIDTH, 1763b378b35SHans de Goede .height = OV2680_ACTIVE_HEIGHT, 1773b378b35SHans de Goede }; 1783b378b35SHans de Goede 1793ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = { 1803ee47cadSRui Miguel Silva "Disabled", 1813ee47cadSRui Miguel Silva "Color Bars", 1823ee47cadSRui Miguel Silva "Random Data", 1833ee47cadSRui Miguel Silva "Square", 1843ee47cadSRui Miguel Silva "Black Image", 1853ee47cadSRui Miguel Silva }; 1863ee47cadSRui Miguel Silva 1873ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = { 1883ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SBGGR10_1X10, 1893ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGRBG10_1X10, 1903ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGBRG10_1X10, 1913ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SRGGB10_1X10, 1923ee47cadSRui Miguel Silva }; 1933ee47cadSRui Miguel Silva 19463f47529SHans de Goede static const struct reg_sequence ov2680_global_setting[] = { 1959b8e6ee8SHans de Goede /* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */ 1969b8e6ee8SHans de Goede {0x3016, 0x1c}, 1979b8e6ee8SHans de Goede 19863f47529SHans de Goede /* R MANUAL set exposure and gain to manual (hw does not do auto) */ 19963f47529SHans de Goede {0x3503, 0x03}, 2000a61cf33SHans de Goede 2019b8e6ee8SHans de Goede /* Analog control register tweaks */ 2029b8e6ee8SHans de Goede {0x3603, 0x39}, /* Reset value 0x99 */ 2039b8e6ee8SHans de Goede {0x3604, 0x24}, /* Reset value 0x74 */ 2049b8e6ee8SHans de Goede {0x3621, 0x37}, /* Reset value 0x44 */ 2059b8e6ee8SHans de Goede 2069b8e6ee8SHans de Goede /* Sensor control register tweaks */ 2079b8e6ee8SHans de Goede {0x3701, 0x64}, /* Reset value 0x61 */ 2089b8e6ee8SHans de Goede {0x3705, 0x3c}, /* Reset value 0x21 */ 2099b8e6ee8SHans de Goede {0x370c, 0x50}, /* Reset value 0x10 */ 2109b8e6ee8SHans de Goede {0x370d, 0xc0}, /* Reset value 0x00 */ 2119b8e6ee8SHans de Goede {0x3718, 0x88}, /* Reset value 0x80 */ 2129b8e6ee8SHans de Goede 2139b8e6ee8SHans de Goede /* PSRAM tweaks */ 2149b8e6ee8SHans de Goede {0x3781, 0x80}, /* Reset value 0x00 */ 2159b8e6ee8SHans de Goede {0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */ 2169b8e6ee8SHans de Goede {0x3789, 0x60}, /* Reset value 0x50 */ 2179b8e6ee8SHans de Goede 2189b8e6ee8SHans de Goede /* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */ 2199b8e6ee8SHans de Goede {0x4000, 0x81}, 2209b8e6ee8SHans de Goede 2210a61cf33SHans de Goede /* Set black level compensation range to 0 - 3 (default 0 - 11) */ 2220a61cf33SHans de Goede {0x4008, 0x00}, 2230a61cf33SHans de Goede {0x4009, 0x03}, 2240a61cf33SHans de Goede 2259b8e6ee8SHans de Goede /* VFIFO R2 0x00 -> 0x02 set Frame reset enable */ 2269b8e6ee8SHans de Goede {0x4602, 0x02}, 2279b8e6ee8SHans de Goede 2289b8e6ee8SHans de Goede /* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */ 2299b8e6ee8SHans de Goede {0x481f, 0x36}, 2309b8e6ee8SHans de Goede 2319b8e6ee8SHans de Goede /* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */ 2329b8e6ee8SHans de Goede {0x4825, 0x36}, 2339b8e6ee8SHans de Goede 2349b8e6ee8SHans de Goede /* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */ 2359b8e6ee8SHans de Goede {0x5002, 0x30}, 2369b8e6ee8SHans de Goede 23763f47529SHans de Goede /* 23863f47529SHans de Goede * Window CONTROL 0x00 -> 0x01, enable manual window control, 23963f47529SHans de Goede * this is necessary for full size flip and mirror support. 24063f47529SHans de Goede */ 24163f47529SHans de Goede {0x5708, 0x01}, 2429b8e6ee8SHans de Goede 2439b8e6ee8SHans de Goede /* 2449b8e6ee8SHans de Goede * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster 2459b8e6ee8SHans de Goede * and enable_general_tail bits based OV2680_R1A_AM10.ovt. 2469b8e6ee8SHans de Goede */ 2479b8e6ee8SHans de Goede {0x5780, 0x3e}, 2489b8e6ee8SHans de Goede 2499b8e6ee8SHans de Goede /* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */ 2509b8e6ee8SHans de Goede {0x5788, 0x02}, 2519b8e6ee8SHans de Goede 2529b8e6ee8SHans de Goede /* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */ 2539b8e6ee8SHans de Goede {0x578e, 0x08}, 2549b8e6ee8SHans de Goede 2559b8e6ee8SHans de Goede /* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */ 2569b8e6ee8SHans de Goede {0x578f, 0x0c}, 2579b8e6ee8SHans de Goede 2589b8e6ee8SHans de Goede /* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */ 2599b8e6ee8SHans de Goede {0x5792, 0x00}, 2603ee47cadSRui Miguel Silva }; 2613ee47cadSRui Miguel Silva 2623ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 2633ee47cadSRui Miguel Silva { 2643ee47cadSRui Miguel Silva return container_of(sd, struct ov2680_dev, sd); 2653ee47cadSRui Miguel Silva } 2663ee47cadSRui Miguel Silva 2673ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 2683ee47cadSRui Miguel Silva { 2693ee47cadSRui Miguel Silva return &container_of(ctrl->handler, struct ov2680_dev, 2703ee47cadSRui Miguel Silva ctrls.handler)->sd; 2713ee47cadSRui Miguel Silva } 2723ee47cadSRui Miguel Silva 2733ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor) 2743ee47cadSRui Miguel Silva { 275e9305a23SHans de Goede if (!sensor->pwdn_gpio) 2763ee47cadSRui Miguel Silva return; 2773ee47cadSRui Miguel Silva 278e9305a23SHans de Goede gpiod_set_value(sensor->pwdn_gpio, 0); 2793ee47cadSRui Miguel Silva usleep_range(5000, 10000); 2803ee47cadSRui Miguel Silva } 2813ee47cadSRui Miguel Silva 2823ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor) 2833ee47cadSRui Miguel Silva { 284e9305a23SHans de Goede if (!sensor->pwdn_gpio) 2853ee47cadSRui Miguel Silva return; 2863ee47cadSRui Miguel Silva 287e9305a23SHans de Goede gpiod_set_value(sensor->pwdn_gpio, 1); 2883ee47cadSRui Miguel Silva usleep_range(5000, 10000); 2893ee47cadSRui Miguel Silva } 2903ee47cadSRui Miguel Silva 2916d6849b2SHans de Goede static void ov2680_set_bayer_order(struct ov2680_dev *sensor, 2926d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt) 2933ee47cadSRui Miguel Silva { 29450a7bad4SHans de Goede int hv_flip = 0; 2953ee47cadSRui Miguel Silva 29650a7bad4SHans de Goede if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) 29750a7bad4SHans de Goede hv_flip += 1; 2983ee47cadSRui Miguel Silva 29950a7bad4SHans de Goede if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) 30050a7bad4SHans de Goede hv_flip += 2; 3013ee47cadSRui Miguel Silva 3026d6849b2SHans de Goede fmt->code = ov2680_hv_flip_bayer_order[hv_flip]; 3036d6849b2SHans de Goede } 3046d6849b2SHans de Goede 30523321b91SHans de Goede static struct v4l2_mbus_framefmt * 30623321b91SHans de Goede __ov2680_get_pad_format(struct ov2680_dev *sensor, 30723321b91SHans de Goede struct v4l2_subdev_state *state, 30823321b91SHans de Goede unsigned int pad, 30923321b91SHans de Goede enum v4l2_subdev_format_whence which) 31023321b91SHans de Goede { 31123321b91SHans de Goede if (which == V4L2_SUBDEV_FORMAT_TRY) 31223321b91SHans de Goede return v4l2_subdev_get_try_format(&sensor->sd, state, pad); 31323321b91SHans de Goede 31423321b91SHans de Goede return &sensor->mode.fmt; 31523321b91SHans de Goede } 31623321b91SHans de Goede 3173b378b35SHans de Goede static struct v4l2_rect * 3183b378b35SHans de Goede __ov2680_get_pad_crop(struct ov2680_dev *sensor, 3193b378b35SHans de Goede struct v4l2_subdev_state *state, 3203b378b35SHans de Goede unsigned int pad, 3213b378b35SHans de Goede enum v4l2_subdev_format_whence which) 3223b378b35SHans de Goede { 3233b378b35SHans de Goede if (which == V4L2_SUBDEV_FORMAT_TRY) 3243b378b35SHans de Goede return v4l2_subdev_get_try_crop(&sensor->sd, state, pad); 3253b378b35SHans de Goede 3263b378b35SHans de Goede return &sensor->mode.crop; 3273b378b35SHans de Goede } 3283b378b35SHans de Goede 3296d6849b2SHans de Goede static void ov2680_fill_format(struct ov2680_dev *sensor, 3306d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt, 3316d6849b2SHans de Goede unsigned int width, unsigned int height) 3326d6849b2SHans de Goede { 3336d6849b2SHans de Goede memset(fmt, 0, sizeof(*fmt)); 3346d6849b2SHans de Goede fmt->width = width; 3356d6849b2SHans de Goede fmt->height = height; 3366d6849b2SHans de Goede fmt->field = V4L2_FIELD_NONE; 3376d6849b2SHans de Goede fmt->colorspace = V4L2_COLORSPACE_SRGB; 3386d6849b2SHans de Goede ov2680_set_bayer_order(sensor, fmt); 3393ee47cadSRui Miguel Silva } 3403ee47cadSRui Miguel Silva 34163f47529SHans de Goede static void ov2680_calc_mode(struct ov2680_dev *sensor) 34263f47529SHans de Goede { 34363f47529SHans de Goede int width = sensor->mode.fmt.width; 34463f47529SHans de Goede int height = sensor->mode.fmt.height; 34563f47529SHans de Goede int orig_width = width; 34663f47529SHans de Goede int orig_height = height; 34763f47529SHans de Goede 3483b378b35SHans de Goede if (width <= (sensor->mode.crop.width / 2) && 3493b378b35SHans de Goede height <= (sensor->mode.crop.height / 2)) { 35063f47529SHans de Goede sensor->mode.binning = true; 35163f47529SHans de Goede width *= 2; 35263f47529SHans de Goede height *= 2; 35363f47529SHans de Goede } else { 35463f47529SHans de Goede sensor->mode.binning = false; 35563f47529SHans de Goede } 35663f47529SHans de Goede 3573b378b35SHans de Goede sensor->mode.h_start = (sensor->mode.crop.left + 3583b378b35SHans de Goede (sensor->mode.crop.width - width) / 2) & ~1; 3593b378b35SHans de Goede sensor->mode.v_start = (sensor->mode.crop.top + 3603b378b35SHans de Goede (sensor->mode.crop.height - height) / 2) & ~1; 36163f47529SHans de Goede sensor->mode.h_end = 36263f47529SHans de Goede min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1, 36363f47529SHans de Goede OV2680_NATIVE_WIDTH - 1); 36463f47529SHans de Goede sensor->mode.v_end = 36563f47529SHans de Goede min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1, 36663f47529SHans de Goede OV2680_NATIVE_HEIGHT - 1); 36763f47529SHans de Goede sensor->mode.h_output_size = orig_width; 36863f47529SHans de Goede sensor->mode.v_output_size = orig_height; 36963f47529SHans de Goede sensor->mode.hts = OV2680_PIXELS_PER_LINE; 37063f47529SHans de Goede sensor->mode.vts = OV2680_LINES_PER_FRAME; 37163f47529SHans de Goede } 37263f47529SHans de Goede 37363f47529SHans de Goede static int ov2680_set_mode(struct ov2680_dev *sensor) 37463f47529SHans de Goede { 37563f47529SHans de Goede u8 sensor_ctrl_0a, inc, fmt1, fmt2; 37663f47529SHans de Goede int ret = 0; 37763f47529SHans de Goede 37863f47529SHans de Goede if (sensor->mode.binning) { 37963f47529SHans de Goede sensor_ctrl_0a = 0x23; 38063f47529SHans de Goede inc = 0x31; 38163f47529SHans de Goede fmt1 = 0xc2; 38263f47529SHans de Goede fmt2 = 0x01; 38363f47529SHans de Goede } else { 38463f47529SHans de Goede sensor_ctrl_0a = 0x21; 38563f47529SHans de Goede inc = 0x11; 38663f47529SHans de Goede fmt1 = 0xc0; 38763f47529SHans de Goede fmt2 = 0x00; 38863f47529SHans de Goede } 38963f47529SHans de Goede 39063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A, 39163f47529SHans de Goede sensor_ctrl_0a, &ret); 39263f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START, 39363f47529SHans de Goede sensor->mode.h_start, &ret); 39463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_START, 39563f47529SHans de Goede sensor->mode.v_start, &ret); 39663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END, 39763f47529SHans de Goede sensor->mode.h_end, &ret); 39863f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_END, 39963f47529SHans de Goede sensor->mode.v_end, &ret); 40063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE, 40163f47529SHans de Goede sensor->mode.h_output_size, &ret); 40263f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE, 40363f47529SHans de Goede sensor->mode.v_output_size, &ret); 40463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_TIMING_HTS, 40563f47529SHans de Goede sensor->mode.hts, &ret); 40663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_TIMING_VTS, 40763f47529SHans de Goede sensor->mode.vts, &ret); 40863f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret); 40963f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret); 41063f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret); 41163f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret); 41263f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_X_WIN, 41363f47529SHans de Goede sensor->mode.h_output_size, &ret); 41463f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_Y_WIN, 41563f47529SHans de Goede sensor->mode.v_output_size, &ret); 41663f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret); 41763f47529SHans de Goede cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret); 41863f47529SHans de Goede 41963f47529SHans de Goede return ret; 42063f47529SHans de Goede } 42163f47529SHans de Goede 422d5d08ad3SHans de Goede static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) 4233ee47cadSRui Miguel Silva { 4243ee47cadSRui Miguel Silva int ret; 4253ee47cadSRui Miguel Silva 426d5d08ad3SHans de Goede if (sensor->is_streaming) 427d5d08ad3SHans de Goede return -EBUSY; 428d5d08ad3SHans de Goede 4299289998eSHans de Goede ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1, 4309289998eSHans de Goede BIT(2), val ? BIT(2) : 0, NULL); 4313ee47cadSRui Miguel Silva if (ret < 0) 4323ee47cadSRui Miguel Silva return ret; 4333ee47cadSRui Miguel Silva 434f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 43550a7bad4SHans de Goede return 0; 4363ee47cadSRui Miguel Silva } 4373ee47cadSRui Miguel Silva 438d5d08ad3SHans de Goede static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) 4393ee47cadSRui Miguel Silva { 4403ee47cadSRui Miguel Silva int ret; 4413ee47cadSRui Miguel Silva 442d5d08ad3SHans de Goede if (sensor->is_streaming) 443d5d08ad3SHans de Goede return -EBUSY; 4443ee47cadSRui Miguel Silva 4459289998eSHans de Goede ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2, 4469289998eSHans de Goede BIT(2), val ? BIT(2) : 0, NULL); 4473ee47cadSRui Miguel Silva if (ret < 0) 4483ee47cadSRui Miguel Silva return ret; 4493ee47cadSRui Miguel Silva 450f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 45150a7bad4SHans de Goede return 0; 4523ee47cadSRui Miguel Silva } 4533ee47cadSRui Miguel Silva 4543ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 4553ee47cadSRui Miguel Silva { 4569289998eSHans de Goede int ret = 0; 4573ee47cadSRui Miguel Silva 4583ee47cadSRui Miguel Silva if (!value) 4599289998eSHans de Goede return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 4609289998eSHans de Goede BIT(7), 0, NULL); 4613ee47cadSRui Miguel Silva 4629289998eSHans de Goede cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 4639289998eSHans de Goede 0x03, value - 1, &ret); 4649289998eSHans de Goede cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 4659289998eSHans de Goede BIT(7), BIT(7), &ret); 4669289998eSHans de Goede 4673ee47cadSRui Miguel Silva return ret; 4683ee47cadSRui Miguel Silva } 4693ee47cadSRui Miguel Silva 4707b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) 4713ee47cadSRui Miguel Silva { 4729289998eSHans de Goede return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL); 4733ee47cadSRui Miguel Silva } 4743ee47cadSRui Miguel Silva 4757b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) 4763ee47cadSRui Miguel Silva { 4779289998eSHans de Goede return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4, 4789289998eSHans de Goede NULL); 4793ee47cadSRui Miguel Silva } 4803ee47cadSRui Miguel Silva 4813ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor) 4823ee47cadSRui Miguel Silva { 4833ee47cadSRui Miguel Silva int ret; 4843ee47cadSRui Miguel Silva 4858e50a122SHans de Goede ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER, 4868e50a122SHans de Goede sensor->pll_mult, NULL); 4878e50a122SHans de Goede if (ret < 0) 4888e50a122SHans de Goede return ret; 4898e50a122SHans de Goede 4909289998eSHans de Goede ret = regmap_multi_reg_write(sensor->regmap, 49163f47529SHans de Goede ov2680_global_setting, 49263f47529SHans de Goede ARRAY_SIZE(ov2680_global_setting)); 493990732a6SHans de Goede if (ret < 0) 494990732a6SHans de Goede return ret; 495990732a6SHans de Goede 49663f47529SHans de Goede ret = ov2680_set_mode(sensor); 4973ee47cadSRui Miguel Silva if (ret < 0) 4983ee47cadSRui Miguel Silva return ret; 4993ee47cadSRui Miguel Silva 5007b5a42e6SHans de Goede /* Restore value of all ctrls */ 5017b5a42e6SHans de Goede ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 5023ee47cadSRui Miguel Silva if (ret < 0) 5033ee47cadSRui Miguel Silva return ret; 5043ee47cadSRui Miguel Silva 505990732a6SHans de Goede return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL); 5063ee47cadSRui Miguel Silva } 5073ee47cadSRui Miguel Silva 508990732a6SHans de Goede static int ov2680_stream_disable(struct ov2680_dev *sensor) 5093ee47cadSRui Miguel Silva { 510990732a6SHans de Goede return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL); 5113ee47cadSRui Miguel Silva } 5123ee47cadSRui Miguel Silva 5133ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor) 5143ee47cadSRui Miguel Silva { 5153ee47cadSRui Miguel Silva clk_disable_unprepare(sensor->xvclk); 5163ee47cadSRui Miguel Silva ov2680_power_down(sensor); 5173ee47cadSRui Miguel Silva regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 5183ee47cadSRui Miguel Silva return 0; 5193ee47cadSRui Miguel Silva } 5203ee47cadSRui Miguel Silva 5213ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor) 5223ee47cadSRui Miguel Silva { 5233ee47cadSRui Miguel Silva int ret; 5243ee47cadSRui Miguel Silva 5253ee47cadSRui Miguel Silva ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 5263ee47cadSRui Miguel Silva if (ret < 0) { 5277adfdecbSHans de Goede dev_err(sensor->dev, "failed to enable regulators: %d\n", ret); 5283ee47cadSRui Miguel Silva return ret; 5293ee47cadSRui Miguel Silva } 5303ee47cadSRui Miguel Silva 531e9305a23SHans de Goede if (!sensor->pwdn_gpio) { 5329289998eSHans de Goede ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, 5339289998eSHans de Goede NULL); 5343ee47cadSRui Miguel Silva if (ret != 0) { 5357adfdecbSHans de Goede dev_err(sensor->dev, "sensor soft reset failed\n"); 53684b4bd7eSHans de Goede goto err_disable_regulators; 5373ee47cadSRui Miguel Silva } 5383ee47cadSRui Miguel Silva usleep_range(1000, 2000); 5393ee47cadSRui Miguel Silva } else { 5403ee47cadSRui Miguel Silva ov2680_power_down(sensor); 5413ee47cadSRui Miguel Silva ov2680_power_up(sensor); 5423ee47cadSRui Miguel Silva } 5433ee47cadSRui Miguel Silva 5443ee47cadSRui Miguel Silva ret = clk_prepare_enable(sensor->xvclk); 5453ee47cadSRui Miguel Silva if (ret < 0) 54684b4bd7eSHans de Goede goto err_disable_regulators; 5473ee47cadSRui Miguel Silva 5483ee47cadSRui Miguel Silva return 0; 54984b4bd7eSHans de Goede 55084b4bd7eSHans de Goede err_disable_regulators: 55184b4bd7eSHans de Goede regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 55284b4bd7eSHans de Goede return ret; 5533ee47cadSRui Miguel Silva } 5543ee47cadSRui Miguel Silva 5553ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 5563ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval *fi) 5573ee47cadSRui Miguel Silva { 5583ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5593ee47cadSRui Miguel Silva 5603ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 561f614dfb8SHans de Goede fi->interval = sensor->mode.frame_interval; 5623ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5633ee47cadSRui Miguel Silva 5643ee47cadSRui Miguel Silva return 0; 5653ee47cadSRui Miguel Silva } 5663ee47cadSRui Miguel Silva 5673ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 5683ee47cadSRui Miguel Silva { 5693ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5703ee47cadSRui Miguel Silva int ret = 0; 5713ee47cadSRui Miguel Silva 5723ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5733ee47cadSRui Miguel Silva 5743ee47cadSRui Miguel Silva if (sensor->is_streaming == !!enable) 5753ee47cadSRui Miguel Silva goto unlock; 5763ee47cadSRui Miguel Silva 577990732a6SHans de Goede if (enable) { 578990732a6SHans de Goede ret = pm_runtime_resume_and_get(sensor->sd.dev); 5793ee47cadSRui Miguel Silva if (ret < 0) 5803ee47cadSRui Miguel Silva goto unlock; 5813ee47cadSRui Miguel Silva 5823ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 583990732a6SHans de Goede if (ret < 0) { 584990732a6SHans de Goede pm_runtime_put(sensor->sd.dev); 585990732a6SHans de Goede goto unlock; 586990732a6SHans de Goede } 587990732a6SHans de Goede } else { 5883ee47cadSRui Miguel Silva ret = ov2680_stream_disable(sensor); 589990732a6SHans de Goede pm_runtime_put(sensor->sd.dev); 590990732a6SHans de Goede } 5913ee47cadSRui Miguel Silva 5923ee47cadSRui Miguel Silva sensor->is_streaming = !!enable; 5933ee47cadSRui Miguel Silva 5943ee47cadSRui Miguel Silva unlock: 5953ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5963ee47cadSRui Miguel Silva 5973ee47cadSRui Miguel Silva return ret; 5983ee47cadSRui Miguel Silva } 5993ee47cadSRui Miguel Silva 6003ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 6010d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6023ee47cadSRui Miguel Silva struct v4l2_subdev_mbus_code_enum *code) 6033ee47cadSRui Miguel Silva { 6043ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6053ee47cadSRui Miguel Silva 6061259259fSHans de Goede if (code->index != 0) 6073ee47cadSRui Miguel Silva return -EINVAL; 6083ee47cadSRui Miguel Silva 609f614dfb8SHans de Goede code->code = sensor->mode.fmt.code; 6103ee47cadSRui Miguel Silva 6113ee47cadSRui Miguel Silva return 0; 6123ee47cadSRui Miguel Silva } 6133ee47cadSRui Miguel Silva 6143ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd, 6150d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6163ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 6173ee47cadSRui Miguel Silva { 6183ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 61923321b91SHans de Goede struct v4l2_mbus_framefmt *fmt; 6203ee47cadSRui Miguel Silva 62123321b91SHans de Goede fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, 62223321b91SHans de Goede format->which); 62323321b91SHans de Goede 6243ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 6253ee47cadSRui Miguel Silva format->format = *fmt; 6263ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6273ee47cadSRui Miguel Silva 62849c282d5SHans de Goede return 0; 6293ee47cadSRui Miguel Silva } 6303ee47cadSRui Miguel Silva 6313ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd, 6320d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6333ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 6343ee47cadSRui Miguel Silva { 6353ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6363ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *try_fmt; 6373b378b35SHans de Goede const struct v4l2_rect *crop; 63863f47529SHans de Goede unsigned int width, height; 6393ee47cadSRui Miguel Silva int ret = 0; 6403ee47cadSRui Miguel Silva 6413b378b35SHans de Goede crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad, 6423b378b35SHans de Goede format->which); 6433b378b35SHans de Goede 6443b378b35SHans de Goede /* Limit set_fmt max size to crop width / height */ 6453b378b35SHans de Goede width = clamp_val(ALIGN(format->format.width, 2), 6463b378b35SHans de Goede OV2680_MIN_CROP_WIDTH, crop->width); 6473b378b35SHans de Goede height = clamp_val(ALIGN(format->format.height, 2), 6483b378b35SHans de Goede OV2680_MIN_CROP_HEIGHT, crop->height); 6493ee47cadSRui Miguel Silva 65063f47529SHans de Goede ov2680_fill_format(sensor, &format->format, width, height); 651c0e97a4bSHans de Goede 6523ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 6530d346d2aSTomi Valkeinen try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0); 654c0e97a4bSHans de Goede *try_fmt = format->format; 655e521b9ccSHans de Goede return 0; 656e521b9ccSHans de Goede } 657e521b9ccSHans de Goede 658e521b9ccSHans de Goede mutex_lock(&sensor->lock); 659e521b9ccSHans de Goede 660e521b9ccSHans de Goede if (sensor->is_streaming) { 661e521b9ccSHans de Goede ret = -EBUSY; 6623ee47cadSRui Miguel Silva goto unlock; 6633ee47cadSRui Miguel Silva } 6643ee47cadSRui Miguel Silva 665f614dfb8SHans de Goede sensor->mode.fmt = format->format; 66663f47529SHans de Goede ov2680_calc_mode(sensor); 6673ee47cadSRui Miguel Silva 6683ee47cadSRui Miguel Silva unlock: 6693ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6703ee47cadSRui Miguel Silva 6713ee47cadSRui Miguel Silva return ret; 6723ee47cadSRui Miguel Silva } 6733ee47cadSRui Miguel Silva 6743b378b35SHans de Goede static int ov2680_get_selection(struct v4l2_subdev *sd, 6753b378b35SHans de Goede struct v4l2_subdev_state *state, 6763b378b35SHans de Goede struct v4l2_subdev_selection *sel) 6773b378b35SHans de Goede { 6783b378b35SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 6793b378b35SHans de Goede 6803b378b35SHans de Goede switch (sel->target) { 6813b378b35SHans de Goede case V4L2_SEL_TGT_CROP: 6823b378b35SHans de Goede mutex_lock(&sensor->lock); 6833b378b35SHans de Goede sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad, 6843b378b35SHans de Goede sel->which); 6853b378b35SHans de Goede mutex_unlock(&sensor->lock); 6863b378b35SHans de Goede break; 6873b378b35SHans de Goede case V4L2_SEL_TGT_NATIVE_SIZE: 6883b378b35SHans de Goede case V4L2_SEL_TGT_CROP_BOUNDS: 6893b378b35SHans de Goede sel->r.top = 0; 6903b378b35SHans de Goede sel->r.left = 0; 6913b378b35SHans de Goede sel->r.width = OV2680_NATIVE_WIDTH; 6923b378b35SHans de Goede sel->r.height = OV2680_NATIVE_HEIGHT; 6933b378b35SHans de Goede break; 6943b378b35SHans de Goede case V4L2_SEL_TGT_CROP_DEFAULT: 6953b378b35SHans de Goede sel->r = ov2680_default_crop; 6963b378b35SHans de Goede break; 6973b378b35SHans de Goede default: 6983b378b35SHans de Goede return -EINVAL; 6993b378b35SHans de Goede } 7003b378b35SHans de Goede 7013b378b35SHans de Goede return 0; 7023b378b35SHans de Goede } 7033b378b35SHans de Goede 7043b378b35SHans de Goede static int ov2680_set_selection(struct v4l2_subdev *sd, 7053b378b35SHans de Goede struct v4l2_subdev_state *state, 7063b378b35SHans de Goede struct v4l2_subdev_selection *sel) 7073b378b35SHans de Goede { 7083b378b35SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 7093b378b35SHans de Goede struct v4l2_mbus_framefmt *format; 7103b378b35SHans de Goede struct v4l2_rect *crop; 7113b378b35SHans de Goede struct v4l2_rect rect; 7123b378b35SHans de Goede 7133b378b35SHans de Goede if (sel->target != V4L2_SEL_TGT_CROP) 7143b378b35SHans de Goede return -EINVAL; 7153b378b35SHans de Goede 7163b378b35SHans de Goede /* 7173b378b35SHans de Goede * Clamp the boundaries of the crop rectangle to the size of the sensor 7183b378b35SHans de Goede * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't 7193b378b35SHans de Goede * disrupted. 7203b378b35SHans de Goede */ 7213b378b35SHans de Goede rect.left = clamp_val(ALIGN(sel->r.left, 2), 7223b378b35SHans de Goede OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH); 7233b378b35SHans de Goede rect.top = clamp_val(ALIGN(sel->r.top, 2), 7243b378b35SHans de Goede OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT); 7253b378b35SHans de Goede rect.width = clamp_val(ALIGN(sel->r.width, 2), 7263b378b35SHans de Goede OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH); 7273b378b35SHans de Goede rect.height = clamp_val(ALIGN(sel->r.height, 2), 7283b378b35SHans de Goede OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT); 7293b378b35SHans de Goede 7303b378b35SHans de Goede /* Make sure the crop rectangle isn't outside the bounds of the array */ 7313b378b35SHans de Goede rect.width = min_t(unsigned int, rect.width, 7323b378b35SHans de Goede OV2680_NATIVE_WIDTH - rect.left); 7333b378b35SHans de Goede rect.height = min_t(unsigned int, rect.height, 7343b378b35SHans de Goede OV2680_NATIVE_HEIGHT - rect.top); 7353b378b35SHans de Goede 7363b378b35SHans de Goede crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which); 7373b378b35SHans de Goede 7383b378b35SHans de Goede mutex_lock(&sensor->lock); 7393b378b35SHans de Goede if (rect.width != crop->width || rect.height != crop->height) { 7403b378b35SHans de Goede /* 7413b378b35SHans de Goede * Reset the output image size if the crop rectangle size has 7423b378b35SHans de Goede * been modified. 7433b378b35SHans de Goede */ 7443b378b35SHans de Goede format = __ov2680_get_pad_format(sensor, state, sel->pad, 7453b378b35SHans de Goede sel->which); 7463b378b35SHans de Goede format->width = rect.width; 7473b378b35SHans de Goede format->height = rect.height; 7483b378b35SHans de Goede } 7493b378b35SHans de Goede 7503b378b35SHans de Goede *crop = rect; 7513b378b35SHans de Goede mutex_unlock(&sensor->lock); 7523b378b35SHans de Goede 7533b378b35SHans de Goede sel->r = rect; 7543b378b35SHans de Goede 7553b378b35SHans de Goede return 0; 7563b378b35SHans de Goede } 7573b378b35SHans de Goede 7583ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd, 7590d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 7603ee47cadSRui Miguel Silva { 7616d6849b2SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 7623ee47cadSRui Miguel Silva 7633b378b35SHans de Goede sd_state->pads[0].try_crop = ov2680_default_crop; 7643b378b35SHans de Goede 7656d6849b2SHans de Goede ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt, 7666d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 7676d6849b2SHans de Goede return 0; 7683ee47cadSRui Miguel Silva } 7693ee47cadSRui Miguel Silva 7703ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 7710d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 7723ee47cadSRui Miguel Silva struct v4l2_subdev_frame_size_enum *fse) 7733ee47cadSRui Miguel Silva { 7743b378b35SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 7753b378b35SHans de Goede struct v4l2_rect *crop; 7763b378b35SHans de Goede 77763f47529SHans de Goede if (fse->index >= OV2680_FRAME_SIZES) 7783ee47cadSRui Miguel Silva return -EINVAL; 7793ee47cadSRui Miguel Silva 7803b378b35SHans de Goede crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which); 7813b378b35SHans de Goede if (!crop) 7823b378b35SHans de Goede return -EINVAL; 7833b378b35SHans de Goede 7843b378b35SHans de Goede fse->min_width = crop->width / (fse->index + 1); 7853b378b35SHans de Goede fse->min_height = crop->height / (fse->index + 1); 78663f47529SHans de Goede fse->max_width = fse->min_width; 78763f47529SHans de Goede fse->max_height = fse->min_height; 7883ee47cadSRui Miguel Silva 7893ee47cadSRui Miguel Silva return 0; 7903ee47cadSRui Miguel Silva } 7913ee47cadSRui Miguel Silva 79263f47529SHans de Goede static bool ov2680_valid_frame_size(struct v4l2_subdev *sd, 79363f47529SHans de Goede struct v4l2_subdev_state *sd_state, 79463f47529SHans de Goede struct v4l2_subdev_frame_interval_enum *fie) 7954007015eSHans de Goede { 79663f47529SHans de Goede struct v4l2_subdev_frame_size_enum fse = { 79763f47529SHans de Goede .pad = fie->pad, 79863f47529SHans de Goede .which = fie->which, 79963f47529SHans de Goede }; 8004007015eSHans de Goede int i; 8014007015eSHans de Goede 80263f47529SHans de Goede for (i = 0; i < OV2680_FRAME_SIZES; i++) { 80363f47529SHans de Goede fse.index = i; 80463f47529SHans de Goede 80563f47529SHans de Goede if (ov2680_enum_frame_size(sd, sd_state, &fse)) 80663f47529SHans de Goede return false; 80763f47529SHans de Goede 80863f47529SHans de Goede if (fie->width == fse.min_width && 80963f47529SHans de Goede fie->height == fse.min_height) 8104007015eSHans de Goede return true; 8114007015eSHans de Goede } 8124007015eSHans de Goede 8134007015eSHans de Goede return false; 8144007015eSHans de Goede } 8154007015eSHans de Goede 8163ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 8170d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 8183ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval_enum *fie) 8193ee47cadSRui Miguel Silva { 8204007015eSHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 8213ee47cadSRui Miguel Silva 8224007015eSHans de Goede /* Only 1 framerate */ 82363f47529SHans de Goede if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie)) 8243ee47cadSRui Miguel Silva return -EINVAL; 8253ee47cadSRui Miguel Silva 826f614dfb8SHans de Goede fie->interval = sensor->mode.frame_interval; 8273ee47cadSRui Miguel Silva 8283ee47cadSRui Miguel Silva return 0; 8293ee47cadSRui Miguel Silva } 8303ee47cadSRui Miguel Silva 8313ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 8323ee47cadSRui Miguel Silva { 8333ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 8343ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 83537f7e57eSHans de Goede int ret; 8363ee47cadSRui Miguel Silva 83737f7e57eSHans de Goede /* Only apply changes to the controls if the device is powered up */ 83837f7e57eSHans de Goede if (!pm_runtime_get_if_in_use(sensor->sd.dev)) { 839f614dfb8SHans de Goede ov2680_set_bayer_order(sensor, &sensor->mode.fmt); 8403ee47cadSRui Miguel Silva return 0; 84137f7e57eSHans de Goede } 8423ee47cadSRui Miguel Silva 8433ee47cadSRui Miguel Silva switch (ctrl->id) { 84405d6bd86SHans de Goede case V4L2_CID_ANALOGUE_GAIN: 84537f7e57eSHans de Goede ret = ov2680_gain_set(sensor, ctrl->val); 84637f7e57eSHans de Goede break; 8473ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 84837f7e57eSHans de Goede ret = ov2680_exposure_set(sensor, ctrl->val); 84937f7e57eSHans de Goede break; 8503ee47cadSRui Miguel Silva case V4L2_CID_VFLIP: 85137f7e57eSHans de Goede ret = ov2680_set_vflip(sensor, ctrl->val); 85237f7e57eSHans de Goede break; 8533ee47cadSRui Miguel Silva case V4L2_CID_HFLIP: 85437f7e57eSHans de Goede ret = ov2680_set_hflip(sensor, ctrl->val); 85537f7e57eSHans de Goede break; 8563ee47cadSRui Miguel Silva case V4L2_CID_TEST_PATTERN: 85737f7e57eSHans de Goede ret = ov2680_test_pattern_set(sensor, ctrl->val); 85837f7e57eSHans de Goede break; 8593ee47cadSRui Miguel Silva default: 86037f7e57eSHans de Goede ret = -EINVAL; 8613ee47cadSRui Miguel Silva break; 8623ee47cadSRui Miguel Silva } 8633ee47cadSRui Miguel Silva 86437f7e57eSHans de Goede pm_runtime_put(sensor->sd.dev); 86537f7e57eSHans de Goede return ret; 8663ee47cadSRui Miguel Silva } 8673ee47cadSRui Miguel Silva 8683ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 8693ee47cadSRui Miguel Silva .s_ctrl = ov2680_s_ctrl, 8703ee47cadSRui Miguel Silva }; 8713ee47cadSRui Miguel Silva 8723ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = { 8733ee47cadSRui Miguel Silva .g_frame_interval = ov2680_s_g_frame_interval, 8743ee47cadSRui Miguel Silva .s_frame_interval = ov2680_s_g_frame_interval, 8753ee47cadSRui Miguel Silva .s_stream = ov2680_s_stream, 8763ee47cadSRui Miguel Silva }; 8773ee47cadSRui Miguel Silva 8783ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 8793ee47cadSRui Miguel Silva .init_cfg = ov2680_init_cfg, 8803ee47cadSRui Miguel Silva .enum_mbus_code = ov2680_enum_mbus_code, 8813ee47cadSRui Miguel Silva .enum_frame_size = ov2680_enum_frame_size, 8823ee47cadSRui Miguel Silva .enum_frame_interval = ov2680_enum_frame_interval, 8833b378b35SHans de Goede .get_fmt = ov2680_get_fmt, 8843b378b35SHans de Goede .set_fmt = ov2680_set_fmt, 8853b378b35SHans de Goede .get_selection = ov2680_get_selection, 8863b378b35SHans de Goede .set_selection = ov2680_set_selection, 8873ee47cadSRui Miguel Silva }; 8883ee47cadSRui Miguel Silva 8893ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = { 8903ee47cadSRui Miguel Silva .video = &ov2680_video_ops, 8913ee47cadSRui Miguel Silva .pad = &ov2680_pad_ops, 8923ee47cadSRui Miguel Silva }; 8933ee47cadSRui Miguel Silva 8943ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor) 8953ee47cadSRui Miguel Silva { 8963ee47cadSRui Miguel Silva /* set initial mode */ 8973b378b35SHans de Goede sensor->mode.crop = ov2680_default_crop; 898f614dfb8SHans de Goede ov2680_fill_format(sensor, &sensor->mode.fmt, 8996d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 90063f47529SHans de Goede ov2680_calc_mode(sensor); 9013ee47cadSRui Miguel Silva 902f614dfb8SHans de Goede sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE; 903f614dfb8SHans de Goede sensor->mode.frame_interval.numerator = 1; 9043ee47cadSRui Miguel Silva 9053ee47cadSRui Miguel Silva return 0; 9063ee47cadSRui Miguel Silva } 9073ee47cadSRui Miguel Silva 908e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor) 9093ee47cadSRui Miguel Silva { 9107adfdecbSHans de Goede struct i2c_client *client = to_i2c_client(sensor->dev); 9113ee47cadSRui Miguel Silva const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 9123ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 9133ee47cadSRui Miguel Silva struct v4l2_ctrl_handler *hdl = &ctrls->handler; 91405d6bd86SHans de Goede int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN; 9153ee47cadSRui Miguel Silva int ret = 0; 9163ee47cadSRui Miguel Silva 9177adfdecbSHans de Goede v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops); 9183ee47cadSRui Miguel Silva 9193ee47cadSRui Miguel Silva sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 9203ee47cadSRui Miguel Silva sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 9213ee47cadSRui Miguel Silva sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 9223ee47cadSRui Miguel Silva 9233ee47cadSRui Miguel Silva ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 9243ee47cadSRui Miguel Silva if (ret < 0) 9253ee47cadSRui Miguel Silva return ret; 9263ee47cadSRui Miguel Silva 9277b5a42e6SHans de Goede v4l2_ctrl_handler_init(hdl, 5); 9283ee47cadSRui Miguel Silva 9293ee47cadSRui Miguel Silva hdl->lock = &sensor->lock; 9303ee47cadSRui Miguel Silva 9313ee47cadSRui Miguel Silva ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 9323ee47cadSRui Miguel Silva ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 9333ee47cadSRui Miguel Silva 9343ee47cadSRui Miguel Silva ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 9353ee47cadSRui Miguel Silva &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 9363ee47cadSRui Miguel Silva ARRAY_SIZE(test_pattern_menu) - 1, 9373ee47cadSRui Miguel Silva 0, 0, test_pattern_menu); 9383ee47cadSRui Miguel Silva 9393ee47cadSRui Miguel Silva ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 94005d6bd86SHans de Goede 0, exp_max, 1, exp_max); 9413ee47cadSRui Miguel Silva 94205d6bd86SHans de Goede ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN, 94305d6bd86SHans de Goede 0, 1023, 1, 250); 9443ee47cadSRui Miguel Silva 94534f9eff2SHans de Goede ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, NULL, V4L2_CID_LINK_FREQ, 94634f9eff2SHans de Goede 0, 0, sensor->link_freq); 94734f9eff2SHans de Goede ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_PIXEL_RATE, 94834f9eff2SHans de Goede 0, sensor->pixel_rate, 94934f9eff2SHans de Goede 1, sensor->pixel_rate); 95034f9eff2SHans de Goede 9513ee47cadSRui Miguel Silva if (hdl->error) { 9523ee47cadSRui Miguel Silva ret = hdl->error; 9533ee47cadSRui Miguel Silva goto cleanup_entity; 9543ee47cadSRui Miguel Silva } 9553ee47cadSRui Miguel Silva 95666274280SDave Stevenson ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 95766274280SDave Stevenson ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 95834f9eff2SHans de Goede ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 9593ee47cadSRui Miguel Silva 9603ee47cadSRui Miguel Silva sensor->sd.ctrl_handler = hdl; 9613ee47cadSRui Miguel Silva 9623ee47cadSRui Miguel Silva ret = v4l2_async_register_subdev(&sensor->sd); 9633ee47cadSRui Miguel Silva if (ret < 0) 9643ee47cadSRui Miguel Silva goto cleanup_entity; 9653ee47cadSRui Miguel Silva 9663ee47cadSRui Miguel Silva return 0; 9673ee47cadSRui Miguel Silva 9683ee47cadSRui Miguel Silva cleanup_entity: 9693ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 9703ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(hdl); 9713ee47cadSRui Miguel Silva 9723ee47cadSRui Miguel Silva return ret; 9733ee47cadSRui Miguel Silva } 9743ee47cadSRui Miguel Silva 9753ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor) 9763ee47cadSRui Miguel Silva { 9773ee47cadSRui Miguel Silva int i; 9783ee47cadSRui Miguel Silva 9793ee47cadSRui Miguel Silva for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 9803ee47cadSRui Miguel Silva sensor->supplies[i].supply = ov2680_supply_name[i]; 9813ee47cadSRui Miguel Silva 9827adfdecbSHans de Goede return devm_regulator_bulk_get(sensor->dev, 9837adfdecbSHans de Goede OV2680_NUM_SUPPLIES, sensor->supplies); 9843ee47cadSRui Miguel Silva } 9853ee47cadSRui Miguel Silva 9863ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor) 9873ee47cadSRui Miguel Silva { 988a99a041cSHans de Goede u64 chip_id, rev; 989a99a041cSHans de Goede int ret = 0; 9903ee47cadSRui Miguel Silva 991a99a041cSHans de Goede cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret); 992a99a041cSHans de Goede cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret); 9933ee47cadSRui Miguel Silva if (ret < 0) { 9947adfdecbSHans de Goede dev_err(sensor->dev, "failed to read chip id\n"); 995a99a041cSHans de Goede return ret; 9963ee47cadSRui Miguel Silva } 9973ee47cadSRui Miguel Silva 9983ee47cadSRui Miguel Silva if (chip_id != OV2680_CHIP_ID) { 9997adfdecbSHans de Goede dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n", 10003ee47cadSRui Miguel Silva chip_id, OV2680_CHIP_ID); 10013ee47cadSRui Miguel Silva return -ENODEV; 10023ee47cadSRui Miguel Silva } 10033ee47cadSRui Miguel Silva 1004a99a041cSHans de Goede dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n", 1005a99a041cSHans de Goede chip_id, rev & 0x0f); 1006a99a041cSHans de Goede 10073ee47cadSRui Miguel Silva return 0; 10083ee47cadSRui Miguel Silva } 10093ee47cadSRui Miguel Silva 1010913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor) 10113ee47cadSRui Miguel Silva { 1012*63b0cd30SHans de Goede struct v4l2_fwnode_endpoint bus_cfg = { 1013*63b0cd30SHans de Goede .bus_type = V4L2_MBUS_CSI2_DPHY, 1014*63b0cd30SHans de Goede }; 10157adfdecbSHans de Goede struct device *dev = sensor->dev; 101683634470SHans de Goede struct fwnode_handle *ep_fwnode; 1017e9305a23SHans de Goede struct gpio_desc *gpio; 1018ec7dfad5SHans de Goede unsigned int rate = 0; 10198e50a122SHans de Goede int i, ret; 10203ee47cadSRui Miguel Silva 1021e9305a23SHans de Goede /* 102283634470SHans de Goede * Sometimes the fwnode graph is initialized by the bridge driver. 102383634470SHans de Goede * Bridge drivers doing this may also add GPIO mappings, wait for this. 102483634470SHans de Goede */ 102583634470SHans de Goede ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 102683634470SHans de Goede if (!ep_fwnode) 102783634470SHans de Goede return dev_err_probe(dev, -EPROBE_DEFER, 102883634470SHans de Goede "waiting for fwnode graph endpoint\n"); 102983634470SHans de Goede 1030*63b0cd30SHans de Goede ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &bus_cfg); 103183634470SHans de Goede fwnode_handle_put(ep_fwnode); 1032*63b0cd30SHans de Goede if (ret) 1033*63b0cd30SHans de Goede return ret; 103483634470SHans de Goede 103583634470SHans de Goede /* 1036e9305a23SHans de Goede * The pin we want is named XSHUTDN in the datasheet. Linux sensor 1037e9305a23SHans de Goede * drivers have standardized on using "powerdown" as con-id name 1038e9305a23SHans de Goede * for powerdown or shutdown pins. Older DTB files use "reset", 1039e9305a23SHans de Goede * so fallback to that if there is no "powerdown" pin. 1040e9305a23SHans de Goede */ 1041e9305a23SHans de Goede gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH); 1042e9305a23SHans de Goede if (!gpio) 1043e9305a23SHans de Goede gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 1044e9305a23SHans de Goede 1045e9305a23SHans de Goede ret = PTR_ERR_OR_ZERO(gpio); 10463ee47cadSRui Miguel Silva if (ret < 0) { 10473ee47cadSRui Miguel Silva dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 1048*63b0cd30SHans de Goede goto out_free_bus_cfg; 10493ee47cadSRui Miguel Silva } 10503ee47cadSRui Miguel Silva 1051e9305a23SHans de Goede sensor->pwdn_gpio = gpio; 1052e9305a23SHans de Goede 1053ec7dfad5SHans de Goede sensor->xvclk = devm_clk_get_optional(dev, "xvclk"); 10543ee47cadSRui Miguel Silva if (IS_ERR(sensor->xvclk)) { 1055*63b0cd30SHans de Goede ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk), 1056*63b0cd30SHans de Goede "xvclk clock missing or invalid\n"); 1057*63b0cd30SHans de Goede goto out_free_bus_cfg; 10583ee47cadSRui Miguel Silva } 10593ee47cadSRui Miguel Silva 1060ec7dfad5SHans de Goede /* 1061ec7dfad5SHans de Goede * We could have either a 24MHz or 19.2MHz clock rate from either DT or 1062ec7dfad5SHans de Goede * ACPI... but we also need to support the weird IPU3 case which will 1063ec7dfad5SHans de Goede * have an external clock AND a clock-frequency property. Check for the 1064ec7dfad5SHans de Goede * clock-frequency property and if found, set that rate if we managed 1065ec7dfad5SHans de Goede * to acquire a clock. This should cover the ACPI case. If the system 1066ec7dfad5SHans de Goede * uses devicetree then the configured rate should already be set, so 1067ec7dfad5SHans de Goede * we can just read it. 1068ec7dfad5SHans de Goede */ 1069ec7dfad5SHans de Goede ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", 1070ec7dfad5SHans de Goede &rate); 1071*63b0cd30SHans de Goede if (ret && !sensor->xvclk) { 1072*63b0cd30SHans de Goede dev_err_probe(dev, ret, "invalid clock config\n"); 1073*63b0cd30SHans de Goede goto out_free_bus_cfg; 1074*63b0cd30SHans de Goede } 1075ec7dfad5SHans de Goede 1076ec7dfad5SHans de Goede if (!ret && sensor->xvclk) { 1077ec7dfad5SHans de Goede ret = clk_set_rate(sensor->xvclk, rate); 1078*63b0cd30SHans de Goede if (ret) { 1079*63b0cd30SHans de Goede dev_err_probe(dev, ret, "failed to set clock rate\n"); 1080*63b0cd30SHans de Goede goto out_free_bus_cfg; 1081*63b0cd30SHans de Goede } 1082ec7dfad5SHans de Goede } 1083ec7dfad5SHans de Goede 1084ec7dfad5SHans de Goede sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk); 10858e50a122SHans de Goede 10868e50a122SHans de Goede for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) { 10878e50a122SHans de Goede if (sensor->xvclk_freq == ov2680_xvclk_freqs[i]) 10888e50a122SHans de Goede break; 10893ee47cadSRui Miguel Silva } 10903ee47cadSRui Miguel Silva 1091*63b0cd30SHans de Goede if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) { 1092*63b0cd30SHans de Goede ret = dev_err_probe(dev, -EINVAL, 10938e50a122SHans de Goede "unsupported xvclk frequency %d Hz\n", 10948e50a122SHans de Goede sensor->xvclk_freq); 1095*63b0cd30SHans de Goede goto out_free_bus_cfg; 1096*63b0cd30SHans de Goede } 10978e50a122SHans de Goede 10988e50a122SHans de Goede sensor->pll_mult = ov2680_pll_multipliers[i]; 10998e50a122SHans de Goede 110034f9eff2SHans de Goede sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 / 110134f9eff2SHans de Goede OV2680_PLL_PREDIV * sensor->pll_mult; 110234f9eff2SHans de Goede 110334f9eff2SHans de Goede /* CSI-2 is double data rate, bus-format is 10 bpp */ 110434f9eff2SHans de Goede sensor->pixel_rate = sensor->link_freq[0] * 2; 110534f9eff2SHans de Goede do_div(sensor->pixel_rate, 10); 110634f9eff2SHans de Goede 1107*63b0cd30SHans de Goede /* Verify bus cfg */ 1108*63b0cd30SHans de Goede if (bus_cfg.bus.mipi_csi2.num_data_lanes != 1) { 1109*63b0cd30SHans de Goede ret = dev_err_probe(dev, -EINVAL, 1110*63b0cd30SHans de Goede "only a 1-lane CSI2 config is supported"); 1111*63b0cd30SHans de Goede goto out_free_bus_cfg; 1112*63b0cd30SHans de Goede } 1113*63b0cd30SHans de Goede 1114*63b0cd30SHans de Goede for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) 1115*63b0cd30SHans de Goede if (bus_cfg.link_frequencies[i] == sensor->link_freq[0]) 1116*63b0cd30SHans de Goede break; 1117*63b0cd30SHans de Goede 1118*63b0cd30SHans de Goede if (bus_cfg.nr_of_link_frequencies == 0 || 1119*63b0cd30SHans de Goede bus_cfg.nr_of_link_frequencies == i) { 1120*63b0cd30SHans de Goede ret = dev_err_probe(dev, -EINVAL, 1121*63b0cd30SHans de Goede "supported link freq %lld not found\n", 1122*63b0cd30SHans de Goede sensor->link_freq[0]); 1123*63b0cd30SHans de Goede goto out_free_bus_cfg; 1124*63b0cd30SHans de Goede } 1125*63b0cd30SHans de Goede 1126*63b0cd30SHans de Goede out_free_bus_cfg: 1127*63b0cd30SHans de Goede v4l2_fwnode_endpoint_free(&bus_cfg); 1128*63b0cd30SHans de Goede return ret; 11293ee47cadSRui Miguel Silva } 11303ee47cadSRui Miguel Silva 11313ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client) 11323ee47cadSRui Miguel Silva { 11333ee47cadSRui Miguel Silva struct device *dev = &client->dev; 11343ee47cadSRui Miguel Silva struct ov2680_dev *sensor; 11353ee47cadSRui Miguel Silva int ret; 11363ee47cadSRui Miguel Silva 11373ee47cadSRui Miguel Silva sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 11383ee47cadSRui Miguel Silva if (!sensor) 11393ee47cadSRui Miguel Silva return -ENOMEM; 11403ee47cadSRui Miguel Silva 11417adfdecbSHans de Goede sensor->dev = &client->dev; 11423ee47cadSRui Miguel Silva 11439289998eSHans de Goede sensor->regmap = devm_cci_regmap_init_i2c(client, 16); 11449289998eSHans de Goede if (IS_ERR(sensor->regmap)) 11459289998eSHans de Goede return PTR_ERR(sensor->regmap); 11469289998eSHans de Goede 1147913c55a8SChristophe JAILLET ret = ov2680_parse_dt(sensor); 11483ee47cadSRui Miguel Silva if (ret < 0) 114983634470SHans de Goede return ret; 11503ee47cadSRui Miguel Silva 11513ee47cadSRui Miguel Silva ret = ov2680_mode_init(sensor); 11523ee47cadSRui Miguel Silva if (ret < 0) 11533ee47cadSRui Miguel Silva return ret; 11543ee47cadSRui Miguel Silva 11553ee47cadSRui Miguel Silva ret = ov2680_get_regulators(sensor); 11563ee47cadSRui Miguel Silva if (ret < 0) { 11573ee47cadSRui Miguel Silva dev_err(dev, "failed to get regulators\n"); 11583ee47cadSRui Miguel Silva return ret; 11593ee47cadSRui Miguel Silva } 11603ee47cadSRui Miguel Silva 11613ee47cadSRui Miguel Silva mutex_init(&sensor->lock); 11623ee47cadSRui Miguel Silva 1163990732a6SHans de Goede /* 1164990732a6SHans de Goede * Power up and verify the chip now, so that if runtime pm is 1165990732a6SHans de Goede * disabled the chip is left on and streaming will work. 1166990732a6SHans de Goede */ 1167990732a6SHans de Goede ret = ov2680_power_on(sensor); 11683ee47cadSRui Miguel Silva if (ret < 0) 11693ee47cadSRui Miguel Silva goto lock_destroy; 11703ee47cadSRui Miguel Silva 1171990732a6SHans de Goede ret = ov2680_check_id(sensor); 1172990732a6SHans de Goede if (ret < 0) 1173990732a6SHans de Goede goto err_powerdown; 1174990732a6SHans de Goede 1175990732a6SHans de Goede pm_runtime_set_active(&client->dev); 1176990732a6SHans de Goede pm_runtime_get_noresume(&client->dev); 1177990732a6SHans de Goede pm_runtime_enable(&client->dev); 1178990732a6SHans de Goede 1179e7f4861dSJavier Martinez Canillas ret = ov2680_v4l2_register(sensor); 11803ee47cadSRui Miguel Silva if (ret < 0) 1181990732a6SHans de Goede goto err_pm_runtime; 1182990732a6SHans de Goede 1183990732a6SHans de Goede pm_runtime_set_autosuspend_delay(&client->dev, 1000); 1184990732a6SHans de Goede pm_runtime_use_autosuspend(&client->dev); 1185990732a6SHans de Goede pm_runtime_put_autosuspend(&client->dev); 11863ee47cadSRui Miguel Silva 11873ee47cadSRui Miguel Silva return 0; 11883ee47cadSRui Miguel Silva 1189990732a6SHans de Goede err_pm_runtime: 1190990732a6SHans de Goede pm_runtime_disable(&client->dev); 1191990732a6SHans de Goede pm_runtime_put_noidle(&client->dev); 1192990732a6SHans de Goede err_powerdown: 1193990732a6SHans de Goede ov2680_power_off(sensor); 11943ee47cadSRui Miguel Silva lock_destroy: 1195b7a41762SJavier Martinez Canillas dev_err(dev, "ov2680 init fail: %d\n", ret); 11963ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 11973ee47cadSRui Miguel Silva 11983ee47cadSRui Miguel Silva return ret; 11993ee47cadSRui Miguel Silva } 12003ee47cadSRui Miguel Silva 1201ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client) 12023ee47cadSRui Miguel Silva { 12033ee47cadSRui Miguel Silva struct v4l2_subdev *sd = i2c_get_clientdata(client); 12043ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 12053ee47cadSRui Miguel Silva 12063ee47cadSRui Miguel Silva v4l2_async_unregister_subdev(&sensor->sd); 12073ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 12083ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 12093ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(&sensor->ctrls.handler); 1210990732a6SHans de Goede 1211990732a6SHans de Goede /* 1212990732a6SHans de Goede * Disable runtime PM. In case runtime PM is disabled in the kernel, 1213990732a6SHans de Goede * make sure to turn power off manually. 1214990732a6SHans de Goede */ 1215990732a6SHans de Goede pm_runtime_disable(&client->dev); 1216990732a6SHans de Goede if (!pm_runtime_status_suspended(&client->dev)) 1217990732a6SHans de Goede ov2680_power_off(sensor); 1218990732a6SHans de Goede pm_runtime_set_suspended(&client->dev); 12193ee47cadSRui Miguel Silva } 12203ee47cadSRui Miguel Silva 1221990732a6SHans de Goede static int ov2680_suspend(struct device *dev) 12223ee47cadSRui Miguel Silva { 12237519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 12243ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 12253ee47cadSRui Miguel Silva 12263ee47cadSRui Miguel Silva if (sensor->is_streaming) 12273ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 12283ee47cadSRui Miguel Silva 1229990732a6SHans de Goede return ov2680_power_off(sensor); 12303ee47cadSRui Miguel Silva } 12313ee47cadSRui Miguel Silva 1232990732a6SHans de Goede static int ov2680_resume(struct device *dev) 12333ee47cadSRui Miguel Silva { 12347519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 12353ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 12363ee47cadSRui Miguel Silva int ret; 12373ee47cadSRui Miguel Silva 1238990732a6SHans de Goede ret = ov2680_power_on(sensor); 1239990732a6SHans de Goede if (ret < 0) 1240990732a6SHans de Goede goto stream_disable; 1241990732a6SHans de Goede 12423ee47cadSRui Miguel Silva if (sensor->is_streaming) { 12433ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 12443ee47cadSRui Miguel Silva if (ret < 0) 12453ee47cadSRui Miguel Silva goto stream_disable; 12463ee47cadSRui Miguel Silva } 12473ee47cadSRui Miguel Silva 12483ee47cadSRui Miguel Silva return 0; 12493ee47cadSRui Miguel Silva 12503ee47cadSRui Miguel Silva stream_disable: 12513ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 12523ee47cadSRui Miguel Silva sensor->is_streaming = false; 12533ee47cadSRui Miguel Silva 12543ee47cadSRui Miguel Silva return ret; 12553ee47cadSRui Miguel Silva } 12563ee47cadSRui Miguel Silva 1257990732a6SHans de Goede static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, 1258990732a6SHans de Goede NULL); 12593ee47cadSRui Miguel Silva 12603ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = { 12613ee47cadSRui Miguel Silva { .compatible = "ovti,ov2680" }, 12623ee47cadSRui Miguel Silva { /* sentinel */ }, 12633ee47cadSRui Miguel Silva }; 12643ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 12653ee47cadSRui Miguel Silva 1266df3ecab8SHans de Goede static const struct acpi_device_id ov2680_acpi_ids[] = { 1267df3ecab8SHans de Goede { "OVTI2680" }, 1268df3ecab8SHans de Goede { /* sentinel */ } 1269df3ecab8SHans de Goede }; 1270df3ecab8SHans de Goede MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids); 1271df3ecab8SHans de Goede 12723ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = { 12733ee47cadSRui Miguel Silva .driver = { 12743ee47cadSRui Miguel Silva .name = "ov2680", 1275990732a6SHans de Goede .pm = pm_sleep_ptr(&ov2680_pm_ops), 127614155c4fSKrzysztof Kozlowski .of_match_table = ov2680_dt_ids, 1277df3ecab8SHans de Goede .acpi_match_table = ov2680_acpi_ids, 12783ee47cadSRui Miguel Silva }, 1279aaeb31c0SUwe Kleine-König .probe = ov2680_probe, 12803ee47cadSRui Miguel Silva .remove = ov2680_remove, 12813ee47cadSRui Miguel Silva }; 12823ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver); 12833ee47cadSRui Miguel Silva 12843ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 12853ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 12863ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2"); 1287