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 <asm/unaligned.h> 143ee47cadSRui Miguel Silva #include <linux/clk.h> 153ee47cadSRui Miguel Silva #include <linux/delay.h> 163ee47cadSRui Miguel Silva #include <linux/err.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> 213ee47cadSRui Miguel Silva #include <linux/gpio/consumer.h> 223ee47cadSRui Miguel Silva #include <linux/regulator/consumer.h> 233ee47cadSRui Miguel Silva 243ee47cadSRui Miguel Silva #include <media/v4l2-common.h> 253ee47cadSRui Miguel Silva #include <media/v4l2-ctrls.h> 263ee47cadSRui Miguel Silva #include <media/v4l2-subdev.h> 273ee47cadSRui Miguel Silva 283ee47cadSRui Miguel Silva #define OV2680_XVCLK_VALUE 24000000 293ee47cadSRui Miguel Silva 303ee47cadSRui Miguel Silva #define OV2680_CHIP_ID 0x2680 313ee47cadSRui Miguel Silva 323ee47cadSRui Miguel Silva #define OV2680_REG_STREAM_CTRL 0x0100 333ee47cadSRui Miguel Silva #define OV2680_REG_SOFT_RESET 0x0103 343ee47cadSRui Miguel Silva 353ee47cadSRui Miguel Silva #define OV2680_REG_CHIP_ID_HIGH 0x300a 363ee47cadSRui Miguel Silva #define OV2680_REG_CHIP_ID_LOW 0x300b 373ee47cadSRui Miguel Silva 383ee47cadSRui Miguel Silva #define OV2680_REG_R_MANUAL 0x3503 393ee47cadSRui Miguel Silva #define OV2680_REG_GAIN_PK 0x350a 403ee47cadSRui Miguel Silva #define OV2680_REG_EXPOSURE_PK_HIGH 0x3500 413ee47cadSRui Miguel Silva #define OV2680_REG_TIMING_HTS 0x380c 423ee47cadSRui Miguel Silva #define OV2680_REG_TIMING_VTS 0x380e 433ee47cadSRui Miguel Silva #define OV2680_REG_FORMAT1 0x3820 443ee47cadSRui Miguel Silva #define OV2680_REG_FORMAT2 0x3821 453ee47cadSRui Miguel Silva 463ee47cadSRui Miguel Silva #define OV2680_REG_ISP_CTRL00 0x5080 473ee47cadSRui Miguel Silva 483ee47cadSRui Miguel Silva #define OV2680_FRAME_RATE 30 493ee47cadSRui Miguel Silva 503ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_8BIT 1 513ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_16BIT 2 523ee47cadSRui Miguel Silva #define OV2680_REG_VALUE_24BIT 3 533ee47cadSRui Miguel Silva 543ee47cadSRui Miguel Silva #define OV2680_WIDTH_MAX 1600 553ee47cadSRui Miguel Silva #define OV2680_HEIGHT_MAX 1200 563ee47cadSRui Miguel Silva 576d6849b2SHans de Goede #define OV2680_DEFAULT_WIDTH 800 586d6849b2SHans de Goede #define OV2680_DEFAULT_HEIGHT 600 596d6849b2SHans de Goede 603ee47cadSRui Miguel Silva enum ov2680_mode_id { 613ee47cadSRui Miguel Silva OV2680_MODE_QUXGA_800_600, 623ee47cadSRui Miguel Silva OV2680_MODE_720P_1280_720, 633ee47cadSRui Miguel Silva OV2680_MODE_UXGA_1600_1200, 643ee47cadSRui Miguel Silva OV2680_MODE_MAX, 653ee47cadSRui Miguel Silva }; 663ee47cadSRui Miguel Silva 673ee47cadSRui Miguel Silva struct reg_value { 683ee47cadSRui Miguel Silva u16 reg_addr; 693ee47cadSRui Miguel Silva u8 val; 703ee47cadSRui Miguel Silva }; 713ee47cadSRui Miguel Silva 723ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = { 733ee47cadSRui Miguel Silva "DOVDD", 743ee47cadSRui Miguel Silva "DVDD", 753ee47cadSRui Miguel Silva "AVDD", 763ee47cadSRui Miguel Silva }; 773ee47cadSRui Miguel Silva 783ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name) 793ee47cadSRui Miguel Silva 803ee47cadSRui Miguel Silva struct ov2680_mode_info { 813ee47cadSRui Miguel Silva const char *name; 823ee47cadSRui Miguel Silva enum ov2680_mode_id id; 833ee47cadSRui Miguel Silva u32 width; 843ee47cadSRui Miguel Silva u32 height; 853ee47cadSRui Miguel Silva const struct reg_value *reg_data; 863ee47cadSRui Miguel Silva u32 reg_data_size; 873ee47cadSRui Miguel Silva }; 883ee47cadSRui Miguel Silva 893ee47cadSRui Miguel Silva struct ov2680_ctrls { 903ee47cadSRui Miguel Silva struct v4l2_ctrl_handler handler; 913ee47cadSRui Miguel Silva struct v4l2_ctrl *exposure; 923ee47cadSRui Miguel Silva struct v4l2_ctrl *gain; 933ee47cadSRui Miguel Silva struct v4l2_ctrl *hflip; 943ee47cadSRui Miguel Silva struct v4l2_ctrl *vflip; 953ee47cadSRui Miguel Silva struct v4l2_ctrl *test_pattern; 963ee47cadSRui Miguel Silva }; 973ee47cadSRui Miguel Silva 983ee47cadSRui Miguel Silva struct ov2680_dev { 993ee47cadSRui Miguel Silva struct i2c_client *i2c_client; 1003ee47cadSRui Miguel Silva struct v4l2_subdev sd; 1013ee47cadSRui Miguel Silva 1023ee47cadSRui Miguel Silva struct media_pad pad; 1033ee47cadSRui Miguel Silva struct clk *xvclk; 1043ee47cadSRui Miguel Silva u32 xvclk_freq; 1053ee47cadSRui Miguel Silva struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 1063ee47cadSRui Miguel Silva 1073ee47cadSRui Miguel Silva struct gpio_desc *reset_gpio; 1083ee47cadSRui Miguel Silva struct mutex lock; /* protect members */ 1093ee47cadSRui Miguel Silva 1103ee47cadSRui Miguel Silva bool mode_pending_changes; 1113ee47cadSRui Miguel Silva bool is_enabled; 1123ee47cadSRui Miguel Silva bool is_streaming; 1133ee47cadSRui Miguel Silva 1143ee47cadSRui Miguel Silva struct ov2680_ctrls ctrls; 1153ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt fmt; 1163ee47cadSRui Miguel Silva struct v4l2_fract frame_interval; 1173ee47cadSRui Miguel Silva 1183ee47cadSRui Miguel Silva const struct ov2680_mode_info *current_mode; 1193ee47cadSRui Miguel Silva }; 1203ee47cadSRui Miguel Silva 1213ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = { 1223ee47cadSRui Miguel Silva "Disabled", 1233ee47cadSRui Miguel Silva "Color Bars", 1243ee47cadSRui Miguel Silva "Random Data", 1253ee47cadSRui Miguel Silva "Square", 1263ee47cadSRui Miguel Silva "Black Image", 1273ee47cadSRui Miguel Silva }; 1283ee47cadSRui Miguel Silva 1293ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = { 1303ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SBGGR10_1X10, 1313ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGRBG10_1X10, 1323ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGBRG10_1X10, 1333ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SRGGB10_1X10, 1343ee47cadSRui Miguel Silva }; 1353ee47cadSRui Miguel Silva 1363ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = { 1373ee47cadSRui Miguel Silva {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20}, 1383ee47cadSRui Miguel Silva {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac}, 1393ee47cadSRui Miguel Silva {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04}, 1403ee47cadSRui Miguel Silva {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00}, 1413ee47cadSRui Miguel Silva {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0}, 1427b5a42e6SHans de Goede {0x3503, 0x03}, 1433ee47cadSRui Miguel Silva }; 1443ee47cadSRui Miguel Silva 1453ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = { 1463ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02}, 1473ee47cadSRui Miguel Silva {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05}, 1483ee47cadSRui Miguel Silva {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11}, 1493ee47cadSRui Miguel Silva {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00}, 1503ee47cadSRui Miguel Silva }; 1513ee47cadSRui Miguel Silva 1523ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = { 1533ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06}, 1543ee47cadSRui Miguel Silva {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06}, 1553ee47cadSRui Miguel Silva {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00}, 1563ee47cadSRui Miguel Silva {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0}, 1573ee47cadSRui Miguel Silva {0x4008, 0x00}, {0x4837, 0x18} 1583ee47cadSRui Miguel Silva }; 1593ee47cadSRui Miguel Silva 1603ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_init_data = { 1613ee47cadSRui Miguel Silva "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600, 1623ee47cadSRui Miguel Silva ov2680_setting_30fps_QUXGA_800_600, 1633ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600), 1643ee47cadSRui Miguel Silva }; 1653ee47cadSRui Miguel Silva 1663ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = { 1673ee47cadSRui Miguel Silva {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 1683ee47cadSRui Miguel Silva 800, 600, ov2680_setting_30fps_QUXGA_800_600, 1693ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)}, 1703ee47cadSRui Miguel Silva {"mode_720p_1280_720", OV2680_MODE_720P_1280_720, 1713ee47cadSRui Miguel Silva 1280, 720, ov2680_setting_30fps_720P_1280_720, 1723ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)}, 1733ee47cadSRui Miguel Silva {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200, 1743ee47cadSRui Miguel Silva 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200, 1753ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)}, 1763ee47cadSRui Miguel Silva }; 1773ee47cadSRui Miguel Silva 1783ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 1793ee47cadSRui Miguel Silva { 1803ee47cadSRui Miguel Silva return container_of(sd, struct ov2680_dev, sd); 1813ee47cadSRui Miguel Silva } 1823ee47cadSRui Miguel Silva 1833ee47cadSRui Miguel Silva static struct device *ov2680_to_dev(struct ov2680_dev *sensor) 1843ee47cadSRui Miguel Silva { 1853ee47cadSRui Miguel Silva return &sensor->i2c_client->dev; 1863ee47cadSRui Miguel Silva } 1873ee47cadSRui Miguel Silva 1883ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 1893ee47cadSRui Miguel Silva { 1903ee47cadSRui Miguel Silva return &container_of(ctrl->handler, struct ov2680_dev, 1913ee47cadSRui Miguel Silva ctrls.handler)->sd; 1923ee47cadSRui Miguel Silva } 1933ee47cadSRui Miguel Silva 1943ee47cadSRui Miguel Silva static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg, 1953ee47cadSRui Miguel Silva unsigned int len, u32 val) 1963ee47cadSRui Miguel Silva { 1973ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 1983ee47cadSRui Miguel Silva u8 buf[6]; 1993ee47cadSRui Miguel Silva int ret; 2003ee47cadSRui Miguel Silva 2013ee47cadSRui Miguel Silva if (len > 4) 2023ee47cadSRui Miguel Silva return -EINVAL; 2033ee47cadSRui Miguel Silva 2043ee47cadSRui Miguel Silva put_unaligned_be16(reg, buf); 2053ee47cadSRui Miguel Silva put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 2063ee47cadSRui Miguel Silva ret = i2c_master_send(client, buf, len + 2); 2073ee47cadSRui Miguel Silva if (ret != len + 2) { 2083ee47cadSRui Miguel Silva dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret); 2093ee47cadSRui Miguel Silva return -EIO; 2103ee47cadSRui Miguel Silva } 2113ee47cadSRui Miguel Silva 2123ee47cadSRui Miguel Silva return 0; 2133ee47cadSRui Miguel Silva } 2143ee47cadSRui Miguel Silva 2153ee47cadSRui Miguel Silva #define ov2680_write_reg(s, r, v) \ 2163ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2173ee47cadSRui Miguel Silva 2183ee47cadSRui Miguel Silva #define ov2680_write_reg16(s, r, v) \ 2193ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2203ee47cadSRui Miguel Silva 2213ee47cadSRui Miguel Silva #define ov2680_write_reg24(s, r, v) \ 2223ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2233ee47cadSRui Miguel Silva 2243ee47cadSRui Miguel Silva static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg, 2253ee47cadSRui Miguel Silva unsigned int len, u32 *val) 2263ee47cadSRui Miguel Silva { 2273ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 2283ee47cadSRui Miguel Silva struct i2c_msg msgs[2]; 2293ee47cadSRui Miguel Silva u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 2303ee47cadSRui Miguel Silva u8 data_buf[4] = { 0, }; 2313ee47cadSRui Miguel Silva int ret; 2323ee47cadSRui Miguel Silva 2333ee47cadSRui Miguel Silva if (len > 4) 2343ee47cadSRui Miguel Silva return -EINVAL; 2353ee47cadSRui Miguel Silva 2363ee47cadSRui Miguel Silva msgs[0].addr = client->addr; 2373ee47cadSRui Miguel Silva msgs[0].flags = 0; 2383ee47cadSRui Miguel Silva msgs[0].len = ARRAY_SIZE(addr_buf); 2393ee47cadSRui Miguel Silva msgs[0].buf = addr_buf; 2403ee47cadSRui Miguel Silva 2413ee47cadSRui Miguel Silva msgs[1].addr = client->addr; 2423ee47cadSRui Miguel Silva msgs[1].flags = I2C_M_RD; 2433ee47cadSRui Miguel Silva msgs[1].len = len; 2443ee47cadSRui Miguel Silva msgs[1].buf = &data_buf[4 - len]; 2453ee47cadSRui Miguel Silva 2463ee47cadSRui Miguel Silva ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 2473ee47cadSRui Miguel Silva if (ret != ARRAY_SIZE(msgs)) { 2483ee47cadSRui Miguel Silva dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret); 2493ee47cadSRui Miguel Silva return -EIO; 2503ee47cadSRui Miguel Silva } 2513ee47cadSRui Miguel Silva 2523ee47cadSRui Miguel Silva *val = get_unaligned_be32(data_buf); 2533ee47cadSRui Miguel Silva 2543ee47cadSRui Miguel Silva return 0; 2553ee47cadSRui Miguel Silva } 2563ee47cadSRui Miguel Silva 2573ee47cadSRui Miguel Silva #define ov2680_read_reg(s, r, v) \ 2583ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2593ee47cadSRui Miguel Silva 2603ee47cadSRui Miguel Silva #define ov2680_read_reg16(s, r, v) \ 2613ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2623ee47cadSRui Miguel Silva 2633ee47cadSRui Miguel Silva #define ov2680_read_reg24(s, r, v) \ 2643ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2653ee47cadSRui Miguel Silva 2663ee47cadSRui Miguel Silva static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val) 2673ee47cadSRui Miguel Silva { 2683ee47cadSRui Miguel Silva u32 readval; 2693ee47cadSRui Miguel Silva int ret; 2703ee47cadSRui Miguel Silva 2713ee47cadSRui Miguel Silva ret = ov2680_read_reg(sensor, reg, &readval); 2723ee47cadSRui Miguel Silva if (ret < 0) 2733ee47cadSRui Miguel Silva return ret; 2743ee47cadSRui Miguel Silva 2753ee47cadSRui Miguel Silva readval &= ~mask; 2763ee47cadSRui Miguel Silva val &= mask; 2773ee47cadSRui Miguel Silva val |= readval; 2783ee47cadSRui Miguel Silva 2793ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, reg, val); 2803ee47cadSRui Miguel Silva } 2813ee47cadSRui Miguel Silva 2823ee47cadSRui Miguel Silva static int ov2680_load_regs(struct ov2680_dev *sensor, 2833ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode) 2843ee47cadSRui Miguel Silva { 2853ee47cadSRui Miguel Silva const struct reg_value *regs = mode->reg_data; 2863ee47cadSRui Miguel Silva unsigned int i; 2873ee47cadSRui Miguel Silva int ret = 0; 2883ee47cadSRui Miguel Silva u16 reg_addr; 2893ee47cadSRui Miguel Silva u8 val; 2903ee47cadSRui Miguel Silva 2913ee47cadSRui Miguel Silva for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 2923ee47cadSRui Miguel Silva reg_addr = regs->reg_addr; 2933ee47cadSRui Miguel Silva val = regs->val; 2943ee47cadSRui Miguel Silva 2953ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, reg_addr, val); 2963ee47cadSRui Miguel Silva if (ret) 2973ee47cadSRui Miguel Silva break; 2983ee47cadSRui Miguel Silva } 2993ee47cadSRui Miguel Silva 3003ee47cadSRui Miguel Silva return ret; 3013ee47cadSRui Miguel Silva } 3023ee47cadSRui Miguel Silva 3033ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor) 3043ee47cadSRui Miguel Silva { 3053ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3063ee47cadSRui Miguel Silva return; 3073ee47cadSRui Miguel Silva 3083ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 0); 3093ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3103ee47cadSRui Miguel Silva } 3113ee47cadSRui Miguel Silva 3123ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor) 3133ee47cadSRui Miguel Silva { 3143ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3153ee47cadSRui Miguel Silva return; 3163ee47cadSRui Miguel Silva 3173ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 1); 3183ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3193ee47cadSRui Miguel Silva } 3203ee47cadSRui Miguel Silva 3216d6849b2SHans de Goede static void ov2680_set_bayer_order(struct ov2680_dev *sensor, 3226d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt) 3233ee47cadSRui Miguel Silva { 32450a7bad4SHans de Goede int hv_flip = 0; 3253ee47cadSRui Miguel Silva 32650a7bad4SHans de Goede if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) 32750a7bad4SHans de Goede hv_flip += 1; 3283ee47cadSRui Miguel Silva 32950a7bad4SHans de Goede if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) 33050a7bad4SHans de Goede hv_flip += 2; 3313ee47cadSRui Miguel Silva 3326d6849b2SHans de Goede fmt->code = ov2680_hv_flip_bayer_order[hv_flip]; 3336d6849b2SHans de Goede } 3346d6849b2SHans de Goede 3356d6849b2SHans de Goede static void ov2680_fill_format(struct ov2680_dev *sensor, 3366d6849b2SHans de Goede struct v4l2_mbus_framefmt *fmt, 3376d6849b2SHans de Goede unsigned int width, unsigned int height) 3386d6849b2SHans de Goede { 3396d6849b2SHans de Goede memset(fmt, 0, sizeof(*fmt)); 3406d6849b2SHans de Goede fmt->width = width; 3416d6849b2SHans de Goede fmt->height = height; 3426d6849b2SHans de Goede fmt->field = V4L2_FIELD_NONE; 3436d6849b2SHans de Goede fmt->colorspace = V4L2_COLORSPACE_SRGB; 3446d6849b2SHans de Goede ov2680_set_bayer_order(sensor, fmt); 3453ee47cadSRui Miguel Silva } 3463ee47cadSRui Miguel Silva 347d5d08ad3SHans de Goede static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) 3483ee47cadSRui Miguel Silva { 3493ee47cadSRui Miguel Silva int ret; 3503ee47cadSRui Miguel Silva 351d5d08ad3SHans de Goede if (sensor->is_streaming) 352d5d08ad3SHans de Goede return -EBUSY; 353d5d08ad3SHans de Goede 354d5d08ad3SHans de Goede ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, 355d5d08ad3SHans de Goede BIT(2), val ? BIT(2) : 0); 3563ee47cadSRui Miguel Silva if (ret < 0) 3573ee47cadSRui Miguel Silva return ret; 3583ee47cadSRui Miguel Silva 3596d6849b2SHans de Goede ov2680_set_bayer_order(sensor, &sensor->fmt); 36050a7bad4SHans de Goede return 0; 3613ee47cadSRui Miguel Silva } 3623ee47cadSRui Miguel Silva 363d5d08ad3SHans de Goede static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) 3643ee47cadSRui Miguel Silva { 3653ee47cadSRui Miguel Silva int ret; 3663ee47cadSRui Miguel Silva 367d5d08ad3SHans de Goede if (sensor->is_streaming) 368d5d08ad3SHans de Goede return -EBUSY; 3693ee47cadSRui Miguel Silva 370d5d08ad3SHans de Goede ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, 371d5d08ad3SHans de Goede BIT(2), val ? BIT(2) : 0); 3723ee47cadSRui Miguel Silva if (ret < 0) 3733ee47cadSRui Miguel Silva return ret; 3743ee47cadSRui Miguel Silva 3756d6849b2SHans de Goede ov2680_set_bayer_order(sensor, &sensor->fmt); 37650a7bad4SHans de Goede return 0; 3773ee47cadSRui Miguel Silva } 3783ee47cadSRui Miguel Silva 3793ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 3803ee47cadSRui Miguel Silva { 3813ee47cadSRui Miguel Silva int ret; 3823ee47cadSRui Miguel Silva 3833ee47cadSRui Miguel Silva if (!value) 3843ee47cadSRui Miguel Silva return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0); 3853ee47cadSRui Miguel Silva 3863ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1); 3873ee47cadSRui Miguel Silva if (ret < 0) 3883ee47cadSRui Miguel Silva return ret; 3893ee47cadSRui Miguel Silva 3903ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7)); 3913ee47cadSRui Miguel Silva if (ret < 0) 3923ee47cadSRui Miguel Silva return ret; 3933ee47cadSRui Miguel Silva 3943ee47cadSRui Miguel Silva return 0; 3953ee47cadSRui Miguel Silva } 3963ee47cadSRui Miguel Silva 3977b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) 3983ee47cadSRui Miguel Silva { 3997b5a42e6SHans de Goede return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain); 4003ee47cadSRui Miguel Silva } 4013ee47cadSRui Miguel Silva 4027b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) 4033ee47cadSRui Miguel Silva { 4047b5a42e6SHans de Goede return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, 4057b5a42e6SHans de Goede exp << 4); 4063ee47cadSRui Miguel Silva } 4073ee47cadSRui Miguel Silva 4083ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor) 4093ee47cadSRui Miguel Silva { 4103ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1); 4113ee47cadSRui Miguel Silva } 4123ee47cadSRui Miguel Silva 4133ee47cadSRui Miguel Silva static int ov2680_stream_disable(struct ov2680_dev *sensor) 4143ee47cadSRui Miguel Silva { 4153ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0); 4163ee47cadSRui Miguel Silva } 4173ee47cadSRui Miguel Silva 4183ee47cadSRui Miguel Silva static int ov2680_mode_set(struct ov2680_dev *sensor) 4193ee47cadSRui Miguel Silva { 4203ee47cadSRui Miguel Silva int ret; 4213ee47cadSRui Miguel Silva 4223ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, sensor->current_mode); 4233ee47cadSRui Miguel Silva if (ret < 0) 4243ee47cadSRui Miguel Silva return ret; 4253ee47cadSRui Miguel Silva 4267b5a42e6SHans de Goede /* Restore value of all ctrls */ 4277b5a42e6SHans de Goede ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 4283ee47cadSRui Miguel Silva if (ret < 0) 4293ee47cadSRui Miguel Silva return ret; 4303ee47cadSRui Miguel Silva 4313ee47cadSRui Miguel Silva sensor->mode_pending_changes = false; 4323ee47cadSRui Miguel Silva 4333ee47cadSRui Miguel Silva return 0; 4343ee47cadSRui Miguel Silva } 4353ee47cadSRui Miguel Silva 4363ee47cadSRui Miguel Silva static int ov2680_mode_restore(struct ov2680_dev *sensor) 4373ee47cadSRui Miguel Silva { 4383ee47cadSRui Miguel Silva int ret; 4393ee47cadSRui Miguel Silva 4403ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, &ov2680_mode_init_data); 4413ee47cadSRui Miguel Silva if (ret < 0) 4423ee47cadSRui Miguel Silva return ret; 4433ee47cadSRui Miguel Silva 4443ee47cadSRui Miguel Silva return ov2680_mode_set(sensor); 4453ee47cadSRui Miguel Silva } 4463ee47cadSRui Miguel Silva 4473ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor) 4483ee47cadSRui Miguel Silva { 4493ee47cadSRui Miguel Silva if (!sensor->is_enabled) 4503ee47cadSRui Miguel Silva return 0; 4513ee47cadSRui Miguel Silva 4523ee47cadSRui Miguel Silva clk_disable_unprepare(sensor->xvclk); 4533ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4543ee47cadSRui Miguel Silva regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 4553ee47cadSRui Miguel Silva sensor->is_enabled = false; 4563ee47cadSRui Miguel Silva 4573ee47cadSRui Miguel Silva return 0; 4583ee47cadSRui Miguel Silva } 4593ee47cadSRui Miguel Silva 4603ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor) 4613ee47cadSRui Miguel Silva { 4623ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 4633ee47cadSRui Miguel Silva int ret; 4643ee47cadSRui Miguel Silva 4653ee47cadSRui Miguel Silva if (sensor->is_enabled) 4663ee47cadSRui Miguel Silva return 0; 4673ee47cadSRui Miguel Silva 4683ee47cadSRui Miguel Silva ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 4693ee47cadSRui Miguel Silva if (ret < 0) { 4703ee47cadSRui Miguel Silva dev_err(dev, "failed to enable regulators: %d\n", ret); 4713ee47cadSRui Miguel Silva return ret; 4723ee47cadSRui Miguel Silva } 4733ee47cadSRui Miguel Silva 4743ee47cadSRui Miguel Silva if (!sensor->reset_gpio) { 4753ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01); 4763ee47cadSRui Miguel Silva if (ret != 0) { 4773ee47cadSRui Miguel Silva dev_err(dev, "sensor soft reset failed\n"); 478*84b4bd7eSHans de Goede goto err_disable_regulators; 4793ee47cadSRui Miguel Silva } 4803ee47cadSRui Miguel Silva usleep_range(1000, 2000); 4813ee47cadSRui Miguel Silva } else { 4823ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4833ee47cadSRui Miguel Silva ov2680_power_up(sensor); 4843ee47cadSRui Miguel Silva } 4853ee47cadSRui Miguel Silva 4863ee47cadSRui Miguel Silva ret = clk_prepare_enable(sensor->xvclk); 4873ee47cadSRui Miguel Silva if (ret < 0) 488*84b4bd7eSHans de Goede goto err_disable_regulators; 4893ee47cadSRui Miguel Silva 4903ee47cadSRui Miguel Silva sensor->is_enabled = true; 4913ee47cadSRui Miguel Silva 4923ee47cadSRui Miguel Silva /* Set clock lane into LP-11 state */ 4933ee47cadSRui Miguel Silva ov2680_stream_enable(sensor); 4943ee47cadSRui Miguel Silva usleep_range(1000, 2000); 4953ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 4963ee47cadSRui Miguel Silva 4973ee47cadSRui Miguel Silva return 0; 498*84b4bd7eSHans de Goede 499*84b4bd7eSHans de Goede err_disable_regulators: 500*84b4bd7eSHans de Goede regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 501*84b4bd7eSHans de Goede return ret; 5023ee47cadSRui Miguel Silva } 5033ee47cadSRui Miguel Silva 5043ee47cadSRui Miguel Silva static int ov2680_s_power(struct v4l2_subdev *sd, int on) 5053ee47cadSRui Miguel Silva { 5063ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5073ee47cadSRui Miguel Silva int ret = 0; 5083ee47cadSRui Miguel Silva 5093ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5103ee47cadSRui Miguel Silva 5113ee47cadSRui Miguel Silva if (on) 5123ee47cadSRui Miguel Silva ret = ov2680_power_on(sensor); 5133ee47cadSRui Miguel Silva else 5143ee47cadSRui Miguel Silva ret = ov2680_power_off(sensor); 5153ee47cadSRui Miguel Silva 5167b5a42e6SHans de Goede if (on && ret == 0) 517c45fbdf2SRui Miguel Silva ret = ov2680_mode_restore(sensor); 5187b5a42e6SHans de Goede 5197b5a42e6SHans de Goede mutex_unlock(&sensor->lock); 5203ee47cadSRui Miguel Silva 5213ee47cadSRui Miguel Silva return ret; 5223ee47cadSRui Miguel Silva } 5233ee47cadSRui Miguel Silva 5243ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 5253ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval *fi) 5263ee47cadSRui Miguel Silva { 5273ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5283ee47cadSRui Miguel Silva 5293ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5303ee47cadSRui Miguel Silva fi->interval = sensor->frame_interval; 5313ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5323ee47cadSRui Miguel Silva 5333ee47cadSRui Miguel Silva return 0; 5343ee47cadSRui Miguel Silva } 5353ee47cadSRui Miguel Silva 5363ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 5373ee47cadSRui Miguel Silva { 5383ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5393ee47cadSRui Miguel Silva int ret = 0; 5403ee47cadSRui Miguel Silva 5413ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5423ee47cadSRui Miguel Silva 5433ee47cadSRui Miguel Silva if (sensor->is_streaming == !!enable) 5443ee47cadSRui Miguel Silva goto unlock; 5453ee47cadSRui Miguel Silva 5463ee47cadSRui Miguel Silva if (enable && sensor->mode_pending_changes) { 5473ee47cadSRui Miguel Silva ret = ov2680_mode_set(sensor); 5483ee47cadSRui Miguel Silva if (ret < 0) 5493ee47cadSRui Miguel Silva goto unlock; 5503ee47cadSRui Miguel Silva } 5513ee47cadSRui Miguel Silva 5523ee47cadSRui Miguel Silva if (enable) 5533ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 5543ee47cadSRui Miguel Silva else 5553ee47cadSRui Miguel Silva ret = ov2680_stream_disable(sensor); 5563ee47cadSRui Miguel Silva 5573ee47cadSRui Miguel Silva sensor->is_streaming = !!enable; 5583ee47cadSRui Miguel Silva 5593ee47cadSRui Miguel Silva unlock: 5603ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5613ee47cadSRui Miguel Silva 5623ee47cadSRui Miguel Silva return ret; 5633ee47cadSRui Miguel Silva } 5643ee47cadSRui Miguel Silva 5653ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 5660d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5673ee47cadSRui Miguel Silva struct v4l2_subdev_mbus_code_enum *code) 5683ee47cadSRui Miguel Silva { 5693ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5703ee47cadSRui Miguel Silva 5713ee47cadSRui Miguel Silva if (code->pad != 0 || code->index != 0) 5723ee47cadSRui Miguel Silva return -EINVAL; 5733ee47cadSRui Miguel Silva 5743ee47cadSRui Miguel Silva code->code = sensor->fmt.code; 5753ee47cadSRui Miguel Silva 5763ee47cadSRui Miguel Silva return 0; 5773ee47cadSRui Miguel Silva } 5783ee47cadSRui Miguel Silva 5793ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd, 5800d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5813ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 5823ee47cadSRui Miguel Silva { 5833ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5843ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *fmt = NULL; 5853ee47cadSRui Miguel Silva 5863ee47cadSRui Miguel Silva if (format->pad != 0) 5873ee47cadSRui Miguel Silva return -EINVAL; 5883ee47cadSRui Miguel Silva 5893ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5903ee47cadSRui Miguel Silva 5913ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 5920d346d2aSTomi Valkeinen fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 5930d346d2aSTomi Valkeinen format->pad); 5943ee47cadSRui Miguel Silva } else { 5953ee47cadSRui Miguel Silva fmt = &sensor->fmt; 5963ee47cadSRui Miguel Silva } 5973ee47cadSRui Miguel Silva 5983ee47cadSRui Miguel Silva format->format = *fmt; 5993ee47cadSRui Miguel Silva 6003ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6013ee47cadSRui Miguel Silva 60249c282d5SHans de Goede return 0; 6033ee47cadSRui Miguel Silva } 6043ee47cadSRui Miguel Silva 6053ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd, 6060d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6073ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 6083ee47cadSRui Miguel Silva { 6093ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6103ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *try_fmt; 6113ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode; 6123ee47cadSRui Miguel Silva int ret = 0; 6133ee47cadSRui Miguel Silva 6143ee47cadSRui Miguel Silva if (format->pad != 0) 6153ee47cadSRui Miguel Silva return -EINVAL; 6163ee47cadSRui Miguel Silva 6173ee47cadSRui Miguel Silva mode = v4l2_find_nearest_size(ov2680_mode_data, 618c0e97a4bSHans de Goede ARRAY_SIZE(ov2680_mode_data), 619c0e97a4bSHans de Goede width, height, 620c0e97a4bSHans de Goede format->format.width, 621c0e97a4bSHans de Goede format->format.height); 622e521b9ccSHans de Goede if (!mode) 623e521b9ccSHans de Goede return -EINVAL; 6243ee47cadSRui Miguel Silva 625c0e97a4bSHans de Goede ov2680_fill_format(sensor, &format->format, mode->width, mode->height); 626c0e97a4bSHans de Goede 6273ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 6280d346d2aSTomi Valkeinen try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0); 629c0e97a4bSHans de Goede *try_fmt = format->format; 630e521b9ccSHans de Goede return 0; 631e521b9ccSHans de Goede } 632e521b9ccSHans de Goede 633e521b9ccSHans de Goede mutex_lock(&sensor->lock); 634e521b9ccSHans de Goede 635e521b9ccSHans de Goede if (sensor->is_streaming) { 636e521b9ccSHans de Goede ret = -EBUSY; 6373ee47cadSRui Miguel Silva goto unlock; 6383ee47cadSRui Miguel Silva } 6393ee47cadSRui Miguel Silva 6403ee47cadSRui Miguel Silva sensor->current_mode = mode; 6413ee47cadSRui Miguel Silva sensor->fmt = format->format; 6423ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 6433ee47cadSRui Miguel Silva 6443ee47cadSRui Miguel Silva unlock: 6453ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6463ee47cadSRui Miguel Silva 6473ee47cadSRui Miguel Silva return ret; 6483ee47cadSRui Miguel Silva } 6493ee47cadSRui Miguel Silva 6503ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd, 6510d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 6523ee47cadSRui Miguel Silva { 6536d6849b2SHans de Goede struct ov2680_dev *sensor = to_ov2680_dev(sd); 6543ee47cadSRui Miguel Silva 6556d6849b2SHans de Goede ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt, 6566d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 6576d6849b2SHans de Goede return 0; 6583ee47cadSRui Miguel Silva } 6593ee47cadSRui Miguel Silva 6603ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 6610d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6623ee47cadSRui Miguel Silva struct v4l2_subdev_frame_size_enum *fse) 6633ee47cadSRui Miguel Silva { 6643ee47cadSRui Miguel Silva int index = fse->index; 6653ee47cadSRui Miguel Silva 6663ee47cadSRui Miguel Silva if (index >= OV2680_MODE_MAX || index < 0) 6673ee47cadSRui Miguel Silva return -EINVAL; 6683ee47cadSRui Miguel Silva 6693ee47cadSRui Miguel Silva fse->min_width = ov2680_mode_data[index].width; 6703ee47cadSRui Miguel Silva fse->min_height = ov2680_mode_data[index].height; 6713ee47cadSRui Miguel Silva fse->max_width = ov2680_mode_data[index].width; 6723ee47cadSRui Miguel Silva fse->max_height = ov2680_mode_data[index].height; 6733ee47cadSRui Miguel Silva 6743ee47cadSRui Miguel Silva return 0; 6753ee47cadSRui Miguel Silva } 6763ee47cadSRui Miguel Silva 6773ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 6780d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6793ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval_enum *fie) 6803ee47cadSRui Miguel Silva { 6813ee47cadSRui Miguel Silva struct v4l2_fract tpf; 6823ee47cadSRui Miguel Silva 6833ee47cadSRui Miguel Silva if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX || 6843ee47cadSRui Miguel Silva fie->height > OV2680_HEIGHT_MAX || 6853ee47cadSRui Miguel Silva fie->which > V4L2_SUBDEV_FORMAT_ACTIVE) 6863ee47cadSRui Miguel Silva return -EINVAL; 6873ee47cadSRui Miguel Silva 6883ee47cadSRui Miguel Silva tpf.denominator = OV2680_FRAME_RATE; 6893ee47cadSRui Miguel Silva tpf.numerator = 1; 6903ee47cadSRui Miguel Silva 6913ee47cadSRui Miguel Silva fie->interval = tpf; 6923ee47cadSRui Miguel Silva 6933ee47cadSRui Miguel Silva return 0; 6943ee47cadSRui Miguel Silva } 6953ee47cadSRui Miguel Silva 6963ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 6973ee47cadSRui Miguel Silva { 6983ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 6993ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 7003ee47cadSRui Miguel Silva 7013ee47cadSRui Miguel Silva if (!sensor->is_enabled) 7023ee47cadSRui Miguel Silva return 0; 7033ee47cadSRui Miguel Silva 7043ee47cadSRui Miguel Silva switch (ctrl->id) { 7053ee47cadSRui Miguel Silva case V4L2_CID_GAIN: 7067b5a42e6SHans de Goede return ov2680_gain_set(sensor, ctrl->val); 7073ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 7087b5a42e6SHans de Goede return ov2680_exposure_set(sensor, ctrl->val); 7093ee47cadSRui Miguel Silva case V4L2_CID_VFLIP: 710d5d08ad3SHans de Goede return ov2680_set_vflip(sensor, ctrl->val); 7113ee47cadSRui Miguel Silva case V4L2_CID_HFLIP: 712d5d08ad3SHans de Goede return ov2680_set_hflip(sensor, ctrl->val); 7133ee47cadSRui Miguel Silva case V4L2_CID_TEST_PATTERN: 7143ee47cadSRui Miguel Silva return ov2680_test_pattern_set(sensor, ctrl->val); 7153ee47cadSRui Miguel Silva default: 7163ee47cadSRui Miguel Silva break; 7173ee47cadSRui Miguel Silva } 7183ee47cadSRui Miguel Silva 7193ee47cadSRui Miguel Silva return -EINVAL; 7203ee47cadSRui Miguel Silva } 7213ee47cadSRui Miguel Silva 7223ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 7233ee47cadSRui Miguel Silva .s_ctrl = ov2680_s_ctrl, 7243ee47cadSRui Miguel Silva }; 7253ee47cadSRui Miguel Silva 7263ee47cadSRui Miguel Silva static const struct v4l2_subdev_core_ops ov2680_core_ops = { 7273ee47cadSRui Miguel Silva .s_power = ov2680_s_power, 7283ee47cadSRui Miguel Silva }; 7293ee47cadSRui Miguel Silva 7303ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = { 7313ee47cadSRui Miguel Silva .g_frame_interval = ov2680_s_g_frame_interval, 7323ee47cadSRui Miguel Silva .s_frame_interval = ov2680_s_g_frame_interval, 7333ee47cadSRui Miguel Silva .s_stream = ov2680_s_stream, 7343ee47cadSRui Miguel Silva }; 7353ee47cadSRui Miguel Silva 7363ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 7373ee47cadSRui Miguel Silva .init_cfg = ov2680_init_cfg, 7383ee47cadSRui Miguel Silva .enum_mbus_code = ov2680_enum_mbus_code, 7393ee47cadSRui Miguel Silva .get_fmt = ov2680_get_fmt, 7403ee47cadSRui Miguel Silva .set_fmt = ov2680_set_fmt, 7413ee47cadSRui Miguel Silva .enum_frame_size = ov2680_enum_frame_size, 7423ee47cadSRui Miguel Silva .enum_frame_interval = ov2680_enum_frame_interval, 7433ee47cadSRui Miguel Silva }; 7443ee47cadSRui Miguel Silva 7453ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = { 7463ee47cadSRui Miguel Silva .core = &ov2680_core_ops, 7473ee47cadSRui Miguel Silva .video = &ov2680_video_ops, 7483ee47cadSRui Miguel Silva .pad = &ov2680_pad_ops, 7493ee47cadSRui Miguel Silva }; 7503ee47cadSRui Miguel Silva 7513ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor) 7523ee47cadSRui Miguel Silva { 7533ee47cadSRui Miguel Silva const struct ov2680_mode_info *init_mode; 7543ee47cadSRui Miguel Silva 7553ee47cadSRui Miguel Silva /* set initial mode */ 7566d6849b2SHans de Goede ov2680_fill_format(sensor, &sensor->fmt, 7576d6849b2SHans de Goede OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT); 7583ee47cadSRui Miguel Silva 7593ee47cadSRui Miguel Silva sensor->frame_interval.denominator = OV2680_FRAME_RATE; 7603ee47cadSRui Miguel Silva sensor->frame_interval.numerator = 1; 7613ee47cadSRui Miguel Silva 7623ee47cadSRui Miguel Silva init_mode = &ov2680_mode_init_data; 7633ee47cadSRui Miguel Silva 7643ee47cadSRui Miguel Silva sensor->current_mode = init_mode; 7653ee47cadSRui Miguel Silva 7663ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 7673ee47cadSRui Miguel Silva 7683ee47cadSRui Miguel Silva return 0; 7693ee47cadSRui Miguel Silva } 7703ee47cadSRui Miguel Silva 771e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor) 7723ee47cadSRui Miguel Silva { 7733ee47cadSRui Miguel Silva const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 7743ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 7753ee47cadSRui Miguel Silva struct v4l2_ctrl_handler *hdl = &ctrls->handler; 7763ee47cadSRui Miguel Silva int ret = 0; 7773ee47cadSRui Miguel Silva 7783ee47cadSRui Miguel Silva v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client, 7793ee47cadSRui Miguel Silva &ov2680_subdev_ops); 7803ee47cadSRui Miguel Silva 7813ee47cadSRui Miguel Silva sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 7823ee47cadSRui Miguel Silva sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 7833ee47cadSRui Miguel Silva sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 7843ee47cadSRui Miguel Silva 7853ee47cadSRui Miguel Silva ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 7863ee47cadSRui Miguel Silva if (ret < 0) 7873ee47cadSRui Miguel Silva return ret; 7883ee47cadSRui Miguel Silva 7897b5a42e6SHans de Goede v4l2_ctrl_handler_init(hdl, 5); 7903ee47cadSRui Miguel Silva 7913ee47cadSRui Miguel Silva hdl->lock = &sensor->lock; 7923ee47cadSRui Miguel Silva 7933ee47cadSRui Miguel Silva ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 7943ee47cadSRui Miguel Silva ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 7953ee47cadSRui Miguel Silva 7963ee47cadSRui Miguel Silva ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 7973ee47cadSRui Miguel Silva &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 7983ee47cadSRui Miguel Silva ARRAY_SIZE(test_pattern_menu) - 1, 7993ee47cadSRui Miguel Silva 0, 0, test_pattern_menu); 8003ee47cadSRui Miguel Silva 8013ee47cadSRui Miguel Silva ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 8023ee47cadSRui Miguel Silva 0, 32767, 1, 0); 8033ee47cadSRui Miguel Silva 8043ee47cadSRui Miguel Silva ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0); 8053ee47cadSRui Miguel Silva 8063ee47cadSRui Miguel Silva if (hdl->error) { 8073ee47cadSRui Miguel Silva ret = hdl->error; 8083ee47cadSRui Miguel Silva goto cleanup_entity; 8093ee47cadSRui Miguel Silva } 8103ee47cadSRui Miguel Silva 81166274280SDave Stevenson ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 81266274280SDave Stevenson ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 8133ee47cadSRui Miguel Silva 8143ee47cadSRui Miguel Silva sensor->sd.ctrl_handler = hdl; 8153ee47cadSRui Miguel Silva 8163ee47cadSRui Miguel Silva ret = v4l2_async_register_subdev(&sensor->sd); 8173ee47cadSRui Miguel Silva if (ret < 0) 8183ee47cadSRui Miguel Silva goto cleanup_entity; 8193ee47cadSRui Miguel Silva 8203ee47cadSRui Miguel Silva return 0; 8213ee47cadSRui Miguel Silva 8223ee47cadSRui Miguel Silva cleanup_entity: 8233ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 8243ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(hdl); 8253ee47cadSRui Miguel Silva 8263ee47cadSRui Miguel Silva return ret; 8273ee47cadSRui Miguel Silva } 8283ee47cadSRui Miguel Silva 8293ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor) 8303ee47cadSRui Miguel Silva { 8313ee47cadSRui Miguel Silva int i; 8323ee47cadSRui Miguel Silva 8333ee47cadSRui Miguel Silva for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 8343ee47cadSRui Miguel Silva sensor->supplies[i].supply = ov2680_supply_name[i]; 8353ee47cadSRui Miguel Silva 8363ee47cadSRui Miguel Silva return devm_regulator_bulk_get(&sensor->i2c_client->dev, 8373ee47cadSRui Miguel Silva OV2680_NUM_SUPPLIES, 8383ee47cadSRui Miguel Silva sensor->supplies); 8393ee47cadSRui Miguel Silva } 8403ee47cadSRui Miguel Silva 8413ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor) 8423ee47cadSRui Miguel Silva { 8433ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 8443ee47cadSRui Miguel Silva u32 chip_id; 8453ee47cadSRui Miguel Silva int ret; 8463ee47cadSRui Miguel Silva 8473ee47cadSRui Miguel Silva ov2680_power_on(sensor); 8483ee47cadSRui Miguel Silva 8493ee47cadSRui Miguel Silva ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id); 8503ee47cadSRui Miguel Silva if (ret < 0) { 8513ee47cadSRui Miguel Silva dev_err(dev, "failed to read chip id high\n"); 8523ee47cadSRui Miguel Silva return -ENODEV; 8533ee47cadSRui Miguel Silva } 8543ee47cadSRui Miguel Silva 8553ee47cadSRui Miguel Silva if (chip_id != OV2680_CHIP_ID) { 8563ee47cadSRui Miguel Silva dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n", 8573ee47cadSRui Miguel Silva chip_id, OV2680_CHIP_ID); 8583ee47cadSRui Miguel Silva return -ENODEV; 8593ee47cadSRui Miguel Silva } 8603ee47cadSRui Miguel Silva 8613ee47cadSRui Miguel Silva return 0; 8623ee47cadSRui Miguel Silva } 8633ee47cadSRui Miguel Silva 864913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor) 8653ee47cadSRui Miguel Silva { 8663ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 8673ee47cadSRui Miguel Silva int ret; 8683ee47cadSRui Miguel Silva 8693ee47cadSRui Miguel Silva sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 8703ee47cadSRui Miguel Silva GPIOD_OUT_HIGH); 8713ee47cadSRui Miguel Silva ret = PTR_ERR_OR_ZERO(sensor->reset_gpio); 8723ee47cadSRui Miguel Silva if (ret < 0) { 8733ee47cadSRui Miguel Silva dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 8743ee47cadSRui Miguel Silva return ret; 8753ee47cadSRui Miguel Silva } 8763ee47cadSRui Miguel Silva 8773ee47cadSRui Miguel Silva sensor->xvclk = devm_clk_get(dev, "xvclk"); 8783ee47cadSRui Miguel Silva if (IS_ERR(sensor->xvclk)) { 8793ee47cadSRui Miguel Silva dev_err(dev, "xvclk clock missing or invalid\n"); 8803ee47cadSRui Miguel Silva return PTR_ERR(sensor->xvclk); 8813ee47cadSRui Miguel Silva } 8823ee47cadSRui Miguel Silva 8833ee47cadSRui Miguel Silva sensor->xvclk_freq = clk_get_rate(sensor->xvclk); 8843ee47cadSRui Miguel Silva if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) { 8853ee47cadSRui Miguel Silva dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n", 8863ee47cadSRui Miguel Silva sensor->xvclk_freq, OV2680_XVCLK_VALUE); 8873ee47cadSRui Miguel Silva return -EINVAL; 8883ee47cadSRui Miguel Silva } 8893ee47cadSRui Miguel Silva 8903ee47cadSRui Miguel Silva return 0; 8913ee47cadSRui Miguel Silva } 8923ee47cadSRui Miguel Silva 8933ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client) 8943ee47cadSRui Miguel Silva { 8953ee47cadSRui Miguel Silva struct device *dev = &client->dev; 8963ee47cadSRui Miguel Silva struct ov2680_dev *sensor; 8973ee47cadSRui Miguel Silva int ret; 8983ee47cadSRui Miguel Silva 8993ee47cadSRui Miguel Silva sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 9003ee47cadSRui Miguel Silva if (!sensor) 9013ee47cadSRui Miguel Silva return -ENOMEM; 9023ee47cadSRui Miguel Silva 9033ee47cadSRui Miguel Silva sensor->i2c_client = client; 9043ee47cadSRui Miguel Silva 905913c55a8SChristophe JAILLET ret = ov2680_parse_dt(sensor); 9063ee47cadSRui Miguel Silva if (ret < 0) 9073ee47cadSRui Miguel Silva return -EINVAL; 9083ee47cadSRui Miguel Silva 9093ee47cadSRui Miguel Silva ret = ov2680_mode_init(sensor); 9103ee47cadSRui Miguel Silva if (ret < 0) 9113ee47cadSRui Miguel Silva return ret; 9123ee47cadSRui Miguel Silva 9133ee47cadSRui Miguel Silva ret = ov2680_get_regulators(sensor); 9143ee47cadSRui Miguel Silva if (ret < 0) { 9153ee47cadSRui Miguel Silva dev_err(dev, "failed to get regulators\n"); 9163ee47cadSRui Miguel Silva return ret; 9173ee47cadSRui Miguel Silva } 9183ee47cadSRui Miguel Silva 9193ee47cadSRui Miguel Silva mutex_init(&sensor->lock); 9203ee47cadSRui Miguel Silva 921b7a41762SJavier Martinez Canillas ret = ov2680_check_id(sensor); 9223ee47cadSRui Miguel Silva if (ret < 0) 9233ee47cadSRui Miguel Silva goto lock_destroy; 9243ee47cadSRui Miguel Silva 925e7f4861dSJavier Martinez Canillas ret = ov2680_v4l2_register(sensor); 9263ee47cadSRui Miguel Silva if (ret < 0) 927b7a41762SJavier Martinez Canillas goto lock_destroy; 9283ee47cadSRui Miguel Silva 9293ee47cadSRui Miguel Silva dev_info(dev, "ov2680 init correctly\n"); 9303ee47cadSRui Miguel Silva 9313ee47cadSRui Miguel Silva return 0; 9323ee47cadSRui Miguel Silva 9333ee47cadSRui Miguel Silva lock_destroy: 934b7a41762SJavier Martinez Canillas dev_err(dev, "ov2680 init fail: %d\n", ret); 9353ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9363ee47cadSRui Miguel Silva 9373ee47cadSRui Miguel Silva return ret; 9383ee47cadSRui Miguel Silva } 9393ee47cadSRui Miguel Silva 940ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client) 9413ee47cadSRui Miguel Silva { 9423ee47cadSRui Miguel Silva struct v4l2_subdev *sd = i2c_get_clientdata(client); 9433ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9443ee47cadSRui Miguel Silva 9453ee47cadSRui Miguel Silva v4l2_async_unregister_subdev(&sensor->sd); 9463ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9473ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 9483ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(&sensor->ctrls.handler); 9493ee47cadSRui Miguel Silva } 9503ee47cadSRui Miguel Silva 9513ee47cadSRui Miguel Silva static int __maybe_unused ov2680_suspend(struct device *dev) 9523ee47cadSRui Miguel Silva { 9537519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9543ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9553ee47cadSRui Miguel Silva 9563ee47cadSRui Miguel Silva if (sensor->is_streaming) 9573ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 9583ee47cadSRui Miguel Silva 9593ee47cadSRui Miguel Silva return 0; 9603ee47cadSRui Miguel Silva } 9613ee47cadSRui Miguel Silva 9623ee47cadSRui Miguel Silva static int __maybe_unused ov2680_resume(struct device *dev) 9633ee47cadSRui Miguel Silva { 9647519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9653ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9663ee47cadSRui Miguel Silva int ret; 9673ee47cadSRui Miguel Silva 9683ee47cadSRui Miguel Silva if (sensor->is_streaming) { 9693ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 9703ee47cadSRui Miguel Silva if (ret < 0) 9713ee47cadSRui Miguel Silva goto stream_disable; 9723ee47cadSRui Miguel Silva } 9733ee47cadSRui Miguel Silva 9743ee47cadSRui Miguel Silva return 0; 9753ee47cadSRui Miguel Silva 9763ee47cadSRui Miguel Silva stream_disable: 9773ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 9783ee47cadSRui Miguel Silva sensor->is_streaming = false; 9793ee47cadSRui Miguel Silva 9803ee47cadSRui Miguel Silva return ret; 9813ee47cadSRui Miguel Silva } 9823ee47cadSRui Miguel Silva 9833ee47cadSRui Miguel Silva static const struct dev_pm_ops ov2680_pm_ops = { 9843ee47cadSRui Miguel Silva SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume) 9853ee47cadSRui Miguel Silva }; 9863ee47cadSRui Miguel Silva 9873ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = { 9883ee47cadSRui Miguel Silva { .compatible = "ovti,ov2680" }, 9893ee47cadSRui Miguel Silva { /* sentinel */ }, 9903ee47cadSRui Miguel Silva }; 9913ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 9923ee47cadSRui Miguel Silva 9933ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = { 9943ee47cadSRui Miguel Silva .driver = { 9953ee47cadSRui Miguel Silva .name = "ov2680", 9963ee47cadSRui Miguel Silva .pm = &ov2680_pm_ops, 99714155c4fSKrzysztof Kozlowski .of_match_table = ov2680_dt_ids, 9983ee47cadSRui Miguel Silva }, 999aaeb31c0SUwe Kleine-König .probe = ov2680_probe, 10003ee47cadSRui Miguel Silva .remove = ov2680_remove, 10013ee47cadSRui Miguel Silva }; 10023ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver); 10033ee47cadSRui Miguel Silva 10043ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 10053ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 10063ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2"); 1007