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> 193ee47cadSRui Miguel Silva #include <linux/module.h> 203ee47cadSRui Miguel Silva #include <linux/of_device.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 573ee47cadSRui Miguel Silva enum ov2680_mode_id { 583ee47cadSRui Miguel Silva OV2680_MODE_QUXGA_800_600, 593ee47cadSRui Miguel Silva OV2680_MODE_720P_1280_720, 603ee47cadSRui Miguel Silva OV2680_MODE_UXGA_1600_1200, 613ee47cadSRui Miguel Silva OV2680_MODE_MAX, 623ee47cadSRui Miguel Silva }; 633ee47cadSRui Miguel Silva 643ee47cadSRui Miguel Silva struct reg_value { 653ee47cadSRui Miguel Silva u16 reg_addr; 663ee47cadSRui Miguel Silva u8 val; 673ee47cadSRui Miguel Silva }; 683ee47cadSRui Miguel Silva 693ee47cadSRui Miguel Silva static const char * const ov2680_supply_name[] = { 703ee47cadSRui Miguel Silva "DOVDD", 713ee47cadSRui Miguel Silva "DVDD", 723ee47cadSRui Miguel Silva "AVDD", 733ee47cadSRui Miguel Silva }; 743ee47cadSRui Miguel Silva 753ee47cadSRui Miguel Silva #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name) 763ee47cadSRui Miguel Silva 773ee47cadSRui Miguel Silva struct ov2680_mode_info { 783ee47cadSRui Miguel Silva const char *name; 793ee47cadSRui Miguel Silva enum ov2680_mode_id id; 803ee47cadSRui Miguel Silva u32 width; 813ee47cadSRui Miguel Silva u32 height; 823ee47cadSRui Miguel Silva const struct reg_value *reg_data; 833ee47cadSRui Miguel Silva u32 reg_data_size; 843ee47cadSRui Miguel Silva }; 853ee47cadSRui Miguel Silva 863ee47cadSRui Miguel Silva struct ov2680_ctrls { 873ee47cadSRui Miguel Silva struct v4l2_ctrl_handler handler; 883ee47cadSRui Miguel Silva struct { 893ee47cadSRui Miguel Silva struct v4l2_ctrl *auto_exp; 903ee47cadSRui Miguel Silva struct v4l2_ctrl *exposure; 913ee47cadSRui Miguel Silva }; 923ee47cadSRui Miguel Silva struct { 933ee47cadSRui Miguel Silva struct v4l2_ctrl *auto_gain; 943ee47cadSRui Miguel Silva struct v4l2_ctrl *gain; 953ee47cadSRui Miguel Silva }; 963ee47cadSRui Miguel Silva 973ee47cadSRui Miguel Silva struct v4l2_ctrl *hflip; 983ee47cadSRui Miguel Silva struct v4l2_ctrl *vflip; 993ee47cadSRui Miguel Silva struct v4l2_ctrl *test_pattern; 1003ee47cadSRui Miguel Silva }; 1013ee47cadSRui Miguel Silva 1023ee47cadSRui Miguel Silva struct ov2680_dev { 1033ee47cadSRui Miguel Silva struct i2c_client *i2c_client; 1043ee47cadSRui Miguel Silva struct v4l2_subdev sd; 1053ee47cadSRui Miguel Silva 1063ee47cadSRui Miguel Silva struct media_pad pad; 1073ee47cadSRui Miguel Silva struct clk *xvclk; 1083ee47cadSRui Miguel Silva u32 xvclk_freq; 1093ee47cadSRui Miguel Silva struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 1103ee47cadSRui Miguel Silva 1113ee47cadSRui Miguel Silva struct gpio_desc *reset_gpio; 1123ee47cadSRui Miguel Silva struct mutex lock; /* protect members */ 1133ee47cadSRui Miguel Silva 1143ee47cadSRui Miguel Silva bool mode_pending_changes; 1153ee47cadSRui Miguel Silva bool is_enabled; 1163ee47cadSRui Miguel Silva bool is_streaming; 1173ee47cadSRui Miguel Silva 1183ee47cadSRui Miguel Silva struct ov2680_ctrls ctrls; 1193ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt fmt; 1203ee47cadSRui Miguel Silva struct v4l2_fract frame_interval; 1213ee47cadSRui Miguel Silva 1223ee47cadSRui Miguel Silva const struct ov2680_mode_info *current_mode; 1233ee47cadSRui Miguel Silva }; 1243ee47cadSRui Miguel Silva 1253ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = { 1263ee47cadSRui Miguel Silva "Disabled", 1273ee47cadSRui Miguel Silva "Color Bars", 1283ee47cadSRui Miguel Silva "Random Data", 1293ee47cadSRui Miguel Silva "Square", 1303ee47cadSRui Miguel Silva "Black Image", 1313ee47cadSRui Miguel Silva }; 1323ee47cadSRui Miguel Silva 1333ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = { 1343ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SBGGR10_1X10, 1353ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGRBG10_1X10, 1363ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGBRG10_1X10, 1373ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SRGGB10_1X10, 1383ee47cadSRui Miguel Silva }; 1393ee47cadSRui Miguel Silva 1403ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = { 1413ee47cadSRui Miguel Silva {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20}, 1423ee47cadSRui Miguel Silva {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac}, 1433ee47cadSRui Miguel Silva {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04}, 1443ee47cadSRui Miguel Silva {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00}, 1453ee47cadSRui Miguel Silva {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0}, 1463ee47cadSRui Miguel Silva }; 1473ee47cadSRui Miguel Silva 1483ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = { 1493ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02}, 1503ee47cadSRui Miguel Silva {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05}, 1513ee47cadSRui Miguel Silva {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11}, 1523ee47cadSRui Miguel Silva {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00}, 1533ee47cadSRui Miguel Silva }; 1543ee47cadSRui Miguel Silva 1553ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = { 1563ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06}, 1573ee47cadSRui Miguel Silva {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06}, 1583ee47cadSRui Miguel Silva {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00}, 1593ee47cadSRui Miguel Silva {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0}, 1603ee47cadSRui Miguel Silva {0x4008, 0x00}, {0x4837, 0x18} 1613ee47cadSRui Miguel Silva }; 1623ee47cadSRui Miguel Silva 1633ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_init_data = { 1643ee47cadSRui Miguel Silva "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600, 1653ee47cadSRui Miguel Silva ov2680_setting_30fps_QUXGA_800_600, 1663ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600), 1673ee47cadSRui Miguel Silva }; 1683ee47cadSRui Miguel Silva 1693ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = { 1703ee47cadSRui Miguel Silva {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 1713ee47cadSRui Miguel Silva 800, 600, ov2680_setting_30fps_QUXGA_800_600, 1723ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)}, 1733ee47cadSRui Miguel Silva {"mode_720p_1280_720", OV2680_MODE_720P_1280_720, 1743ee47cadSRui Miguel Silva 1280, 720, ov2680_setting_30fps_720P_1280_720, 1753ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)}, 1763ee47cadSRui Miguel Silva {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200, 1773ee47cadSRui Miguel Silva 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200, 1783ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)}, 1793ee47cadSRui Miguel Silva }; 1803ee47cadSRui Miguel Silva 1813ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 1823ee47cadSRui Miguel Silva { 1833ee47cadSRui Miguel Silva return container_of(sd, struct ov2680_dev, sd); 1843ee47cadSRui Miguel Silva } 1853ee47cadSRui Miguel Silva 1863ee47cadSRui Miguel Silva static struct device *ov2680_to_dev(struct ov2680_dev *sensor) 1873ee47cadSRui Miguel Silva { 1883ee47cadSRui Miguel Silva return &sensor->i2c_client->dev; 1893ee47cadSRui Miguel Silva } 1903ee47cadSRui Miguel Silva 1913ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 1923ee47cadSRui Miguel Silva { 1933ee47cadSRui Miguel Silva return &container_of(ctrl->handler, struct ov2680_dev, 1943ee47cadSRui Miguel Silva ctrls.handler)->sd; 1953ee47cadSRui Miguel Silva } 1963ee47cadSRui Miguel Silva 1973ee47cadSRui Miguel Silva static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg, 1983ee47cadSRui Miguel Silva unsigned int len, u32 val) 1993ee47cadSRui Miguel Silva { 2003ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 2013ee47cadSRui Miguel Silva u8 buf[6]; 2023ee47cadSRui Miguel Silva int ret; 2033ee47cadSRui Miguel Silva 2043ee47cadSRui Miguel Silva if (len > 4) 2053ee47cadSRui Miguel Silva return -EINVAL; 2063ee47cadSRui Miguel Silva 2073ee47cadSRui Miguel Silva put_unaligned_be16(reg, buf); 2083ee47cadSRui Miguel Silva put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 2093ee47cadSRui Miguel Silva ret = i2c_master_send(client, buf, len + 2); 2103ee47cadSRui Miguel Silva if (ret != len + 2) { 2113ee47cadSRui Miguel Silva dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret); 2123ee47cadSRui Miguel Silva return -EIO; 2133ee47cadSRui Miguel Silva } 2143ee47cadSRui Miguel Silva 2153ee47cadSRui Miguel Silva return 0; 2163ee47cadSRui Miguel Silva } 2173ee47cadSRui Miguel Silva 2183ee47cadSRui Miguel Silva #define ov2680_write_reg(s, r, v) \ 2193ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2203ee47cadSRui Miguel Silva 2213ee47cadSRui Miguel Silva #define ov2680_write_reg16(s, r, v) \ 2223ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2233ee47cadSRui Miguel Silva 2243ee47cadSRui Miguel Silva #define ov2680_write_reg24(s, r, v) \ 2253ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2263ee47cadSRui Miguel Silva 2273ee47cadSRui Miguel Silva static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg, 2283ee47cadSRui Miguel Silva unsigned int len, u32 *val) 2293ee47cadSRui Miguel Silva { 2303ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 2313ee47cadSRui Miguel Silva struct i2c_msg msgs[2]; 2323ee47cadSRui Miguel Silva u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 2333ee47cadSRui Miguel Silva u8 data_buf[4] = { 0, }; 2343ee47cadSRui Miguel Silva int ret; 2353ee47cadSRui Miguel Silva 2363ee47cadSRui Miguel Silva if (len > 4) 2373ee47cadSRui Miguel Silva return -EINVAL; 2383ee47cadSRui Miguel Silva 2393ee47cadSRui Miguel Silva msgs[0].addr = client->addr; 2403ee47cadSRui Miguel Silva msgs[0].flags = 0; 2413ee47cadSRui Miguel Silva msgs[0].len = ARRAY_SIZE(addr_buf); 2423ee47cadSRui Miguel Silva msgs[0].buf = addr_buf; 2433ee47cadSRui Miguel Silva 2443ee47cadSRui Miguel Silva msgs[1].addr = client->addr; 2453ee47cadSRui Miguel Silva msgs[1].flags = I2C_M_RD; 2463ee47cadSRui Miguel Silva msgs[1].len = len; 2473ee47cadSRui Miguel Silva msgs[1].buf = &data_buf[4 - len]; 2483ee47cadSRui Miguel Silva 2493ee47cadSRui Miguel Silva ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 2503ee47cadSRui Miguel Silva if (ret != ARRAY_SIZE(msgs)) { 2513ee47cadSRui Miguel Silva dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret); 2523ee47cadSRui Miguel Silva return -EIO; 2533ee47cadSRui Miguel Silva } 2543ee47cadSRui Miguel Silva 2553ee47cadSRui Miguel Silva *val = get_unaligned_be32(data_buf); 2563ee47cadSRui Miguel Silva 2573ee47cadSRui Miguel Silva return 0; 2583ee47cadSRui Miguel Silva } 2593ee47cadSRui Miguel Silva 2603ee47cadSRui Miguel Silva #define ov2680_read_reg(s, r, v) \ 2613ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2623ee47cadSRui Miguel Silva 2633ee47cadSRui Miguel Silva #define ov2680_read_reg16(s, r, v) \ 2643ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2653ee47cadSRui Miguel Silva 2663ee47cadSRui Miguel Silva #define ov2680_read_reg24(s, r, v) \ 2673ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2683ee47cadSRui Miguel Silva 2693ee47cadSRui Miguel Silva static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val) 2703ee47cadSRui Miguel Silva { 2713ee47cadSRui Miguel Silva u32 readval; 2723ee47cadSRui Miguel Silva int ret; 2733ee47cadSRui Miguel Silva 2743ee47cadSRui Miguel Silva ret = ov2680_read_reg(sensor, reg, &readval); 2753ee47cadSRui Miguel Silva if (ret < 0) 2763ee47cadSRui Miguel Silva return ret; 2773ee47cadSRui Miguel Silva 2783ee47cadSRui Miguel Silva readval &= ~mask; 2793ee47cadSRui Miguel Silva val &= mask; 2803ee47cadSRui Miguel Silva val |= readval; 2813ee47cadSRui Miguel Silva 2823ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, reg, val); 2833ee47cadSRui Miguel Silva } 2843ee47cadSRui Miguel Silva 2853ee47cadSRui Miguel Silva static int ov2680_load_regs(struct ov2680_dev *sensor, 2863ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode) 2873ee47cadSRui Miguel Silva { 2883ee47cadSRui Miguel Silva const struct reg_value *regs = mode->reg_data; 2893ee47cadSRui Miguel Silva unsigned int i; 2903ee47cadSRui Miguel Silva int ret = 0; 2913ee47cadSRui Miguel Silva u16 reg_addr; 2923ee47cadSRui Miguel Silva u8 val; 2933ee47cadSRui Miguel Silva 2943ee47cadSRui Miguel Silva for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 2953ee47cadSRui Miguel Silva reg_addr = regs->reg_addr; 2963ee47cadSRui Miguel Silva val = regs->val; 2973ee47cadSRui Miguel Silva 2983ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, reg_addr, val); 2993ee47cadSRui Miguel Silva if (ret) 3003ee47cadSRui Miguel Silva break; 3013ee47cadSRui Miguel Silva } 3023ee47cadSRui Miguel Silva 3033ee47cadSRui Miguel Silva return ret; 3043ee47cadSRui Miguel Silva } 3053ee47cadSRui Miguel Silva 3063ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor) 3073ee47cadSRui Miguel Silva { 3083ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3093ee47cadSRui Miguel Silva return; 3103ee47cadSRui Miguel Silva 3113ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 0); 3123ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3133ee47cadSRui Miguel Silva } 3143ee47cadSRui Miguel Silva 3153ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor) 3163ee47cadSRui Miguel Silva { 3173ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3183ee47cadSRui Miguel Silva return; 3193ee47cadSRui Miguel Silva 3203ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 1); 3213ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3223ee47cadSRui Miguel Silva } 3233ee47cadSRui Miguel Silva 3243ee47cadSRui Miguel Silva static int ov2680_bayer_order(struct ov2680_dev *sensor) 3253ee47cadSRui Miguel Silva { 3263ee47cadSRui Miguel Silva u32 format1; 3273ee47cadSRui Miguel Silva u32 format2; 3283ee47cadSRui Miguel Silva u32 hv_flip; 3293ee47cadSRui Miguel Silva int ret; 3303ee47cadSRui Miguel Silva 3313ee47cadSRui Miguel Silva ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1); 3323ee47cadSRui Miguel Silva if (ret < 0) 3333ee47cadSRui Miguel Silva return ret; 3343ee47cadSRui Miguel Silva 3353ee47cadSRui Miguel Silva ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2); 3363ee47cadSRui Miguel Silva if (ret < 0) 3373ee47cadSRui Miguel Silva return ret; 3383ee47cadSRui Miguel Silva 3393ee47cadSRui Miguel Silva hv_flip = (format2 & BIT(2) << 1) | (format1 & BIT(2)); 3403ee47cadSRui Miguel Silva 3413ee47cadSRui Miguel Silva sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip]; 3423ee47cadSRui Miguel Silva 3433ee47cadSRui Miguel Silva return 0; 3443ee47cadSRui Miguel Silva } 3453ee47cadSRui Miguel Silva 3463ee47cadSRui Miguel Silva static int ov2680_vflip_enable(struct ov2680_dev *sensor) 3473ee47cadSRui Miguel Silva { 3483ee47cadSRui Miguel Silva int ret; 3493ee47cadSRui Miguel Silva 3503ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2)); 3513ee47cadSRui Miguel Silva if (ret < 0) 3523ee47cadSRui Miguel Silva return ret; 3533ee47cadSRui Miguel Silva 3543ee47cadSRui Miguel Silva return ov2680_bayer_order(sensor); 3553ee47cadSRui Miguel Silva } 3563ee47cadSRui Miguel Silva 3573ee47cadSRui Miguel Silva static int ov2680_vflip_disable(struct ov2680_dev *sensor) 3583ee47cadSRui Miguel Silva { 3593ee47cadSRui Miguel Silva int ret; 3603ee47cadSRui Miguel Silva 3613ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0)); 3623ee47cadSRui Miguel Silva if (ret < 0) 3633ee47cadSRui Miguel Silva return ret; 3643ee47cadSRui Miguel Silva 3653ee47cadSRui Miguel Silva return ov2680_bayer_order(sensor); 3663ee47cadSRui Miguel Silva } 3673ee47cadSRui Miguel Silva 3683ee47cadSRui Miguel Silva static int ov2680_hflip_enable(struct ov2680_dev *sensor) 3693ee47cadSRui Miguel Silva { 3703ee47cadSRui Miguel Silva int ret; 3713ee47cadSRui Miguel Silva 3723ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2)); 3733ee47cadSRui Miguel Silva if (ret < 0) 3743ee47cadSRui Miguel Silva return ret; 3753ee47cadSRui Miguel Silva 3763ee47cadSRui Miguel Silva return ov2680_bayer_order(sensor); 3773ee47cadSRui Miguel Silva } 3783ee47cadSRui Miguel Silva 3793ee47cadSRui Miguel Silva static int ov2680_hflip_disable(struct ov2680_dev *sensor) 3803ee47cadSRui Miguel Silva { 3813ee47cadSRui Miguel Silva int ret; 3823ee47cadSRui Miguel Silva 3833ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0)); 3843ee47cadSRui Miguel Silva if (ret < 0) 3853ee47cadSRui Miguel Silva return ret; 3863ee47cadSRui Miguel Silva 3873ee47cadSRui Miguel Silva return ov2680_bayer_order(sensor); 3883ee47cadSRui Miguel Silva } 3893ee47cadSRui Miguel Silva 3903ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 3913ee47cadSRui Miguel Silva { 3923ee47cadSRui Miguel Silva int ret; 3933ee47cadSRui Miguel Silva 3943ee47cadSRui Miguel Silva if (!value) 3953ee47cadSRui Miguel Silva return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0); 3963ee47cadSRui Miguel Silva 3973ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1); 3983ee47cadSRui Miguel Silva if (ret < 0) 3993ee47cadSRui Miguel Silva return ret; 4003ee47cadSRui Miguel Silva 4013ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7)); 4023ee47cadSRui Miguel Silva if (ret < 0) 4033ee47cadSRui Miguel Silva return ret; 4043ee47cadSRui Miguel Silva 4053ee47cadSRui Miguel Silva return 0; 4063ee47cadSRui Miguel Silva } 4073ee47cadSRui Miguel Silva 4083ee47cadSRui Miguel Silva static int ov2680_gain_set(struct ov2680_dev *sensor, bool auto_gain) 4093ee47cadSRui Miguel Silva { 4103ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 4113ee47cadSRui Miguel Silva u32 gain; 4123ee47cadSRui Miguel Silva int ret; 4133ee47cadSRui Miguel Silva 4143ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(1), 4153ee47cadSRui Miguel Silva auto_gain ? 0 : BIT(1)); 4163ee47cadSRui Miguel Silva if (ret < 0) 4173ee47cadSRui Miguel Silva return ret; 4183ee47cadSRui Miguel Silva 4193ee47cadSRui Miguel Silva if (auto_gain || !ctrls->gain->is_new) 4203ee47cadSRui Miguel Silva return 0; 4213ee47cadSRui Miguel Silva 4223ee47cadSRui Miguel Silva gain = ctrls->gain->val; 4233ee47cadSRui Miguel Silva 4243ee47cadSRui Miguel Silva ret = ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain); 4253ee47cadSRui Miguel Silva 4263ee47cadSRui Miguel Silva return 0; 4273ee47cadSRui Miguel Silva } 4283ee47cadSRui Miguel Silva 4293ee47cadSRui Miguel Silva static int ov2680_gain_get(struct ov2680_dev *sensor) 4303ee47cadSRui Miguel Silva { 4313ee47cadSRui Miguel Silva u32 gain; 4323ee47cadSRui Miguel Silva int ret; 4333ee47cadSRui Miguel Silva 4343ee47cadSRui Miguel Silva ret = ov2680_read_reg16(sensor, OV2680_REG_GAIN_PK, &gain); 4353ee47cadSRui Miguel Silva if (ret) 4363ee47cadSRui Miguel Silva return ret; 4373ee47cadSRui Miguel Silva 4383ee47cadSRui Miguel Silva return gain; 4393ee47cadSRui Miguel Silva } 4403ee47cadSRui Miguel Silva 4413ee47cadSRui Miguel Silva static int ov2680_exposure_set(struct ov2680_dev *sensor, bool auto_exp) 4423ee47cadSRui Miguel Silva { 4433ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 4443ee47cadSRui Miguel Silva u32 exp; 4453ee47cadSRui Miguel Silva int ret; 4463ee47cadSRui Miguel Silva 4473ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(0), 4483ee47cadSRui Miguel Silva auto_exp ? 0 : BIT(0)); 4493ee47cadSRui Miguel Silva if (ret < 0) 4503ee47cadSRui Miguel Silva return ret; 4513ee47cadSRui Miguel Silva 4523ee47cadSRui Miguel Silva if (auto_exp || !ctrls->exposure->is_new) 4533ee47cadSRui Miguel Silva return 0; 4543ee47cadSRui Miguel Silva 4553ee47cadSRui Miguel Silva exp = (u32)ctrls->exposure->val; 4563ee47cadSRui Miguel Silva exp <<= 4; 4573ee47cadSRui Miguel Silva 4583ee47cadSRui Miguel Silva return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp); 4593ee47cadSRui Miguel Silva } 4603ee47cadSRui Miguel Silva 4613ee47cadSRui Miguel Silva static int ov2680_exposure_get(struct ov2680_dev *sensor) 4623ee47cadSRui Miguel Silva { 4633ee47cadSRui Miguel Silva int ret; 4643ee47cadSRui Miguel Silva u32 exp; 4653ee47cadSRui Miguel Silva 4663ee47cadSRui Miguel Silva ret = ov2680_read_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, &exp); 4673ee47cadSRui Miguel Silva if (ret) 4683ee47cadSRui Miguel Silva return ret; 4693ee47cadSRui Miguel Silva 4703ee47cadSRui Miguel Silva return exp >> 4; 4713ee47cadSRui Miguel Silva } 4723ee47cadSRui Miguel Silva 4733ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor) 4743ee47cadSRui Miguel Silva { 4753ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1); 4763ee47cadSRui Miguel Silva } 4773ee47cadSRui Miguel Silva 4783ee47cadSRui Miguel Silva static int ov2680_stream_disable(struct ov2680_dev *sensor) 4793ee47cadSRui Miguel Silva { 4803ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0); 4813ee47cadSRui Miguel Silva } 4823ee47cadSRui Miguel Silva 4833ee47cadSRui Miguel Silva static int ov2680_mode_set(struct ov2680_dev *sensor) 4843ee47cadSRui Miguel Silva { 4853ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 4863ee47cadSRui Miguel Silva int ret; 4873ee47cadSRui Miguel Silva 4883ee47cadSRui Miguel Silva ret = ov2680_gain_set(sensor, false); 4893ee47cadSRui Miguel Silva if (ret < 0) 4903ee47cadSRui Miguel Silva return ret; 4913ee47cadSRui Miguel Silva 4923ee47cadSRui Miguel Silva ret = ov2680_exposure_set(sensor, false); 4933ee47cadSRui Miguel Silva if (ret < 0) 4943ee47cadSRui Miguel Silva return ret; 4953ee47cadSRui Miguel Silva 4963ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, sensor->current_mode); 4973ee47cadSRui Miguel Silva if (ret < 0) 4983ee47cadSRui Miguel Silva return ret; 4993ee47cadSRui Miguel Silva 5003ee47cadSRui Miguel Silva if (ctrls->auto_gain->val) { 5013ee47cadSRui Miguel Silva ret = ov2680_gain_set(sensor, true); 5023ee47cadSRui Miguel Silva if (ret < 0) 5033ee47cadSRui Miguel Silva return ret; 5043ee47cadSRui Miguel Silva } 5053ee47cadSRui Miguel Silva 5063ee47cadSRui Miguel Silva if (ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO) { 5073ee47cadSRui Miguel Silva ret = ov2680_exposure_set(sensor, true); 5083ee47cadSRui Miguel Silva if (ret < 0) 5093ee47cadSRui Miguel Silva return ret; 5103ee47cadSRui Miguel Silva } 5113ee47cadSRui Miguel Silva 5123ee47cadSRui Miguel Silva sensor->mode_pending_changes = false; 5133ee47cadSRui Miguel Silva 5143ee47cadSRui Miguel Silva return 0; 5153ee47cadSRui Miguel Silva } 5163ee47cadSRui Miguel Silva 5173ee47cadSRui Miguel Silva static int ov2680_mode_restore(struct ov2680_dev *sensor) 5183ee47cadSRui Miguel Silva { 5193ee47cadSRui Miguel Silva int ret; 5203ee47cadSRui Miguel Silva 5213ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, &ov2680_mode_init_data); 5223ee47cadSRui Miguel Silva if (ret < 0) 5233ee47cadSRui Miguel Silva return ret; 5243ee47cadSRui Miguel Silva 5253ee47cadSRui Miguel Silva return ov2680_mode_set(sensor); 5263ee47cadSRui Miguel Silva } 5273ee47cadSRui Miguel Silva 5283ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor) 5293ee47cadSRui Miguel Silva { 5303ee47cadSRui Miguel Silva if (!sensor->is_enabled) 5313ee47cadSRui Miguel Silva return 0; 5323ee47cadSRui Miguel Silva 5333ee47cadSRui Miguel Silva clk_disable_unprepare(sensor->xvclk); 5343ee47cadSRui Miguel Silva ov2680_power_down(sensor); 5353ee47cadSRui Miguel Silva regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 5363ee47cadSRui Miguel Silva sensor->is_enabled = false; 5373ee47cadSRui Miguel Silva 5383ee47cadSRui Miguel Silva return 0; 5393ee47cadSRui Miguel Silva } 5403ee47cadSRui Miguel Silva 5413ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor) 5423ee47cadSRui Miguel Silva { 5433ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 5443ee47cadSRui Miguel Silva int ret; 5453ee47cadSRui Miguel Silva 5463ee47cadSRui Miguel Silva if (sensor->is_enabled) 5473ee47cadSRui Miguel Silva return 0; 5483ee47cadSRui Miguel Silva 5493ee47cadSRui Miguel Silva ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 5503ee47cadSRui Miguel Silva if (ret < 0) { 5513ee47cadSRui Miguel Silva dev_err(dev, "failed to enable regulators: %d\n", ret); 5523ee47cadSRui Miguel Silva return ret; 5533ee47cadSRui Miguel Silva } 5543ee47cadSRui Miguel Silva 5553ee47cadSRui Miguel Silva if (!sensor->reset_gpio) { 5563ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01); 5573ee47cadSRui Miguel Silva if (ret != 0) { 5583ee47cadSRui Miguel Silva dev_err(dev, "sensor soft reset failed\n"); 5593ee47cadSRui Miguel Silva return ret; 5603ee47cadSRui Miguel Silva } 5613ee47cadSRui Miguel Silva usleep_range(1000, 2000); 5623ee47cadSRui Miguel Silva } else { 5633ee47cadSRui Miguel Silva ov2680_power_down(sensor); 5643ee47cadSRui Miguel Silva ov2680_power_up(sensor); 5653ee47cadSRui Miguel Silva } 5663ee47cadSRui Miguel Silva 5673ee47cadSRui Miguel Silva ret = clk_prepare_enable(sensor->xvclk); 5683ee47cadSRui Miguel Silva if (ret < 0) 5693ee47cadSRui Miguel Silva return ret; 5703ee47cadSRui Miguel Silva 5713ee47cadSRui Miguel Silva sensor->is_enabled = true; 5723ee47cadSRui Miguel Silva 5733ee47cadSRui Miguel Silva /* Set clock lane into LP-11 state */ 5743ee47cadSRui Miguel Silva ov2680_stream_enable(sensor); 5753ee47cadSRui Miguel Silva usleep_range(1000, 2000); 5763ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 5773ee47cadSRui Miguel Silva 5783ee47cadSRui Miguel Silva return 0; 5793ee47cadSRui Miguel Silva } 5803ee47cadSRui Miguel Silva 5813ee47cadSRui Miguel Silva static int ov2680_s_power(struct v4l2_subdev *sd, int on) 5823ee47cadSRui Miguel Silva { 5833ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5843ee47cadSRui Miguel Silva int ret = 0; 5853ee47cadSRui Miguel Silva 5863ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5873ee47cadSRui Miguel Silva 5883ee47cadSRui Miguel Silva if (on) 5893ee47cadSRui Miguel Silva ret = ov2680_power_on(sensor); 5903ee47cadSRui Miguel Silva else 5913ee47cadSRui Miguel Silva ret = ov2680_power_off(sensor); 5923ee47cadSRui Miguel Silva 5933ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5943ee47cadSRui Miguel Silva 5953ee47cadSRui Miguel Silva if (on && ret == 0) { 5963ee47cadSRui Miguel Silva ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 5973ee47cadSRui Miguel Silva if (ret < 0) 5983ee47cadSRui Miguel Silva return ret; 599c45fbdf2SRui Miguel Silva 600c45fbdf2SRui Miguel Silva ret = ov2680_mode_restore(sensor); 6013ee47cadSRui Miguel Silva } 6023ee47cadSRui Miguel Silva 6033ee47cadSRui Miguel Silva return ret; 6043ee47cadSRui Miguel Silva } 6053ee47cadSRui Miguel Silva 6063ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 6073ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval *fi) 6083ee47cadSRui Miguel Silva { 6093ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6103ee47cadSRui Miguel Silva 6113ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 6123ee47cadSRui Miguel Silva fi->interval = sensor->frame_interval; 6133ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6143ee47cadSRui Miguel Silva 6153ee47cadSRui Miguel Silva return 0; 6163ee47cadSRui Miguel Silva } 6173ee47cadSRui Miguel Silva 6183ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 6193ee47cadSRui Miguel Silva { 6203ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6213ee47cadSRui Miguel Silva int ret = 0; 6223ee47cadSRui Miguel Silva 6233ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 6243ee47cadSRui Miguel Silva 6253ee47cadSRui Miguel Silva if (sensor->is_streaming == !!enable) 6263ee47cadSRui Miguel Silva goto unlock; 6273ee47cadSRui Miguel Silva 6283ee47cadSRui Miguel Silva if (enable && sensor->mode_pending_changes) { 6293ee47cadSRui Miguel Silva ret = ov2680_mode_set(sensor); 6303ee47cadSRui Miguel Silva if (ret < 0) 6313ee47cadSRui Miguel Silva goto unlock; 6323ee47cadSRui Miguel Silva } 6333ee47cadSRui Miguel Silva 6343ee47cadSRui Miguel Silva if (enable) 6353ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 6363ee47cadSRui Miguel Silva else 6373ee47cadSRui Miguel Silva ret = ov2680_stream_disable(sensor); 6383ee47cadSRui Miguel Silva 6393ee47cadSRui Miguel Silva sensor->is_streaming = !!enable; 6403ee47cadSRui Miguel Silva 6413ee47cadSRui Miguel Silva unlock: 6423ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6433ee47cadSRui Miguel Silva 6443ee47cadSRui Miguel Silva return ret; 6453ee47cadSRui Miguel Silva } 6463ee47cadSRui Miguel Silva 6473ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 6483ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg, 6493ee47cadSRui Miguel Silva struct v4l2_subdev_mbus_code_enum *code) 6503ee47cadSRui Miguel Silva { 6513ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6523ee47cadSRui Miguel Silva 6533ee47cadSRui Miguel Silva if (code->pad != 0 || code->index != 0) 6543ee47cadSRui Miguel Silva return -EINVAL; 6553ee47cadSRui Miguel Silva 6563ee47cadSRui Miguel Silva code->code = sensor->fmt.code; 6573ee47cadSRui Miguel Silva 6583ee47cadSRui Miguel Silva return 0; 6593ee47cadSRui Miguel Silva } 6603ee47cadSRui Miguel Silva 6613ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd, 6623ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg, 6633ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 6643ee47cadSRui Miguel Silva { 6653ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6663ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *fmt = NULL; 6673ee47cadSRui Miguel Silva int ret = 0; 6683ee47cadSRui Miguel Silva 6693ee47cadSRui Miguel Silva if (format->pad != 0) 6703ee47cadSRui Miguel Silva return -EINVAL; 6713ee47cadSRui Miguel Silva 6723ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 6733ee47cadSRui Miguel Silva 6743ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 6753ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 6763ee47cadSRui Miguel Silva fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, format->pad); 6773ee47cadSRui Miguel Silva #else 6782dbcb6fbSHans Verkuil ret = -EINVAL; 6793ee47cadSRui Miguel Silva #endif 6803ee47cadSRui Miguel Silva } else { 6813ee47cadSRui Miguel Silva fmt = &sensor->fmt; 6823ee47cadSRui Miguel Silva } 6833ee47cadSRui Miguel Silva 6843ee47cadSRui Miguel Silva if (fmt) 6853ee47cadSRui Miguel Silva format->format = *fmt; 6863ee47cadSRui Miguel Silva 6873ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6883ee47cadSRui Miguel Silva 6893ee47cadSRui Miguel Silva return ret; 6903ee47cadSRui Miguel Silva } 6913ee47cadSRui Miguel Silva 6923ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd, 6933ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg, 6943ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 6953ee47cadSRui Miguel Silva { 6963ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6973ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *fmt = &format->format; 6983ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 6993ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *try_fmt; 7003ee47cadSRui Miguel Silva #endif 7013ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode; 7023ee47cadSRui Miguel Silva int ret = 0; 7033ee47cadSRui Miguel Silva 7043ee47cadSRui Miguel Silva if (format->pad != 0) 7053ee47cadSRui Miguel Silva return -EINVAL; 7063ee47cadSRui Miguel Silva 7073ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 7083ee47cadSRui Miguel Silva 7093ee47cadSRui Miguel Silva if (sensor->is_streaming) { 7103ee47cadSRui Miguel Silva ret = -EBUSY; 7113ee47cadSRui Miguel Silva goto unlock; 7123ee47cadSRui Miguel Silva } 7133ee47cadSRui Miguel Silva 7143ee47cadSRui Miguel Silva mode = v4l2_find_nearest_size(ov2680_mode_data, 7153ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_mode_data), width, 7163ee47cadSRui Miguel Silva height, fmt->width, fmt->height); 7173ee47cadSRui Miguel Silva if (!mode) { 7183ee47cadSRui Miguel Silva ret = -EINVAL; 7193ee47cadSRui Miguel Silva goto unlock; 7203ee47cadSRui Miguel Silva } 7213ee47cadSRui Miguel Silva 7223ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 7233ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 7243ee47cadSRui Miguel Silva try_fmt = v4l2_subdev_get_try_format(sd, cfg, 0); 7253ee47cadSRui Miguel Silva format->format = *try_fmt; 7263ee47cadSRui Miguel Silva #endif 7273ee47cadSRui Miguel Silva goto unlock; 7283ee47cadSRui Miguel Silva } 7293ee47cadSRui Miguel Silva 7303ee47cadSRui Miguel Silva fmt->width = mode->width; 7313ee47cadSRui Miguel Silva fmt->height = mode->height; 7323ee47cadSRui Miguel Silva fmt->code = sensor->fmt.code; 7333ee47cadSRui Miguel Silva fmt->colorspace = sensor->fmt.colorspace; 7343ee47cadSRui Miguel Silva 7353ee47cadSRui Miguel Silva sensor->current_mode = mode; 7363ee47cadSRui Miguel Silva sensor->fmt = format->format; 7373ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 7383ee47cadSRui Miguel Silva 7393ee47cadSRui Miguel Silva unlock: 7403ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 7413ee47cadSRui Miguel Silva 7423ee47cadSRui Miguel Silva return ret; 7433ee47cadSRui Miguel Silva } 7443ee47cadSRui Miguel Silva 7453ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd, 7463ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg) 7473ee47cadSRui Miguel Silva { 7483ee47cadSRui Miguel Silva struct v4l2_subdev_format fmt = { 7493ee47cadSRui Miguel Silva .which = cfg ? V4L2_SUBDEV_FORMAT_TRY 7503ee47cadSRui Miguel Silva : V4L2_SUBDEV_FORMAT_ACTIVE, 7513ee47cadSRui Miguel Silva .format = { 7523ee47cadSRui Miguel Silva .width = 800, 7533ee47cadSRui Miguel Silva .height = 600, 7543ee47cadSRui Miguel Silva } 7553ee47cadSRui Miguel Silva }; 7563ee47cadSRui Miguel Silva 7573ee47cadSRui Miguel Silva return ov2680_set_fmt(sd, cfg, &fmt); 7583ee47cadSRui Miguel Silva } 7593ee47cadSRui Miguel Silva 7603ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 7613ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg, 7623ee47cadSRui Miguel Silva struct v4l2_subdev_frame_size_enum *fse) 7633ee47cadSRui Miguel Silva { 7643ee47cadSRui Miguel Silva int index = fse->index; 7653ee47cadSRui Miguel Silva 7663ee47cadSRui Miguel Silva if (index >= OV2680_MODE_MAX || index < 0) 7673ee47cadSRui Miguel Silva return -EINVAL; 7683ee47cadSRui Miguel Silva 7693ee47cadSRui Miguel Silva fse->min_width = ov2680_mode_data[index].width; 7703ee47cadSRui Miguel Silva fse->min_height = ov2680_mode_data[index].height; 7713ee47cadSRui Miguel Silva fse->max_width = ov2680_mode_data[index].width; 7723ee47cadSRui Miguel Silva fse->max_height = ov2680_mode_data[index].height; 7733ee47cadSRui Miguel Silva 7743ee47cadSRui Miguel Silva return 0; 7753ee47cadSRui Miguel Silva } 7763ee47cadSRui Miguel Silva 7773ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 7783ee47cadSRui Miguel Silva struct v4l2_subdev_pad_config *cfg, 7793ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval_enum *fie) 7803ee47cadSRui Miguel Silva { 7813ee47cadSRui Miguel Silva struct v4l2_fract tpf; 7823ee47cadSRui Miguel Silva 7833ee47cadSRui Miguel Silva if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX || 7843ee47cadSRui Miguel Silva fie->height > OV2680_HEIGHT_MAX || 7853ee47cadSRui Miguel Silva fie->which > V4L2_SUBDEV_FORMAT_ACTIVE) 7863ee47cadSRui Miguel Silva return -EINVAL; 7873ee47cadSRui Miguel Silva 7883ee47cadSRui Miguel Silva tpf.denominator = OV2680_FRAME_RATE; 7893ee47cadSRui Miguel Silva tpf.numerator = 1; 7903ee47cadSRui Miguel Silva 7913ee47cadSRui Miguel Silva fie->interval = tpf; 7923ee47cadSRui Miguel Silva 7933ee47cadSRui Miguel Silva return 0; 7943ee47cadSRui Miguel Silva } 7953ee47cadSRui Miguel Silva 7963ee47cadSRui Miguel Silva static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 7973ee47cadSRui Miguel Silva { 7983ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 7993ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 8003ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 8013ee47cadSRui Miguel Silva int val; 8023ee47cadSRui Miguel Silva 8033ee47cadSRui Miguel Silva if (!sensor->is_enabled) 8043ee47cadSRui Miguel Silva return 0; 8053ee47cadSRui Miguel Silva 8063ee47cadSRui Miguel Silva switch (ctrl->id) { 8073ee47cadSRui Miguel Silva case V4L2_CID_GAIN: 8083ee47cadSRui Miguel Silva val = ov2680_gain_get(sensor); 8093ee47cadSRui Miguel Silva if (val < 0) 8103ee47cadSRui Miguel Silva return val; 8113ee47cadSRui Miguel Silva ctrls->gain->val = val; 8123ee47cadSRui Miguel Silva break; 8133ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 8143ee47cadSRui Miguel Silva val = ov2680_exposure_get(sensor); 8153ee47cadSRui Miguel Silva if (val < 0) 8163ee47cadSRui Miguel Silva return val; 8173ee47cadSRui Miguel Silva ctrls->exposure->val = val; 8183ee47cadSRui Miguel Silva break; 8193ee47cadSRui Miguel Silva } 8203ee47cadSRui Miguel Silva 8213ee47cadSRui Miguel Silva return 0; 8223ee47cadSRui Miguel Silva } 8233ee47cadSRui Miguel Silva 8243ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 8253ee47cadSRui Miguel Silva { 8263ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 8273ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 8283ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 8293ee47cadSRui Miguel Silva 8303ee47cadSRui Miguel Silva if (!sensor->is_enabled) 8313ee47cadSRui Miguel Silva return 0; 8323ee47cadSRui Miguel Silva 8333ee47cadSRui Miguel Silva switch (ctrl->id) { 8343ee47cadSRui Miguel Silva case V4L2_CID_AUTOGAIN: 8353ee47cadSRui Miguel Silva return ov2680_gain_set(sensor, !!ctrl->val); 8363ee47cadSRui Miguel Silva case V4L2_CID_GAIN: 8373ee47cadSRui Miguel Silva return ov2680_gain_set(sensor, !!ctrls->auto_gain->val); 8383ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE_AUTO: 8393ee47cadSRui Miguel Silva return ov2680_exposure_set(sensor, !!ctrl->val); 8403ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 8413ee47cadSRui Miguel Silva return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val); 8423ee47cadSRui Miguel Silva case V4L2_CID_VFLIP: 8433ee47cadSRui Miguel Silva if (sensor->is_streaming) 8443ee47cadSRui Miguel Silva return -EBUSY; 8453ee47cadSRui Miguel Silva if (ctrl->val) 8463ee47cadSRui Miguel Silva return ov2680_vflip_enable(sensor); 8473ee47cadSRui Miguel Silva else 8483ee47cadSRui Miguel Silva return ov2680_vflip_disable(sensor); 8493ee47cadSRui Miguel Silva case V4L2_CID_HFLIP: 8503ee47cadSRui Miguel Silva if (sensor->is_streaming) 8513ee47cadSRui Miguel Silva return -EBUSY; 8523ee47cadSRui Miguel Silva if (ctrl->val) 8533ee47cadSRui Miguel Silva return ov2680_hflip_enable(sensor); 8543ee47cadSRui Miguel Silva else 8553ee47cadSRui Miguel Silva return ov2680_hflip_disable(sensor); 8563ee47cadSRui Miguel Silva case V4L2_CID_TEST_PATTERN: 8573ee47cadSRui Miguel Silva return ov2680_test_pattern_set(sensor, ctrl->val); 8583ee47cadSRui Miguel Silva default: 8593ee47cadSRui Miguel Silva break; 8603ee47cadSRui Miguel Silva } 8613ee47cadSRui Miguel Silva 8623ee47cadSRui Miguel Silva return -EINVAL; 8633ee47cadSRui Miguel Silva } 8643ee47cadSRui Miguel Silva 8653ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 8663ee47cadSRui Miguel Silva .g_volatile_ctrl = ov2680_g_volatile_ctrl, 8673ee47cadSRui Miguel Silva .s_ctrl = ov2680_s_ctrl, 8683ee47cadSRui Miguel Silva }; 8693ee47cadSRui Miguel Silva 8703ee47cadSRui Miguel Silva static const struct v4l2_subdev_core_ops ov2680_core_ops = { 8713ee47cadSRui Miguel Silva .s_power = ov2680_s_power, 8723ee47cadSRui Miguel Silva }; 8733ee47cadSRui Miguel Silva 8743ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = { 8753ee47cadSRui Miguel Silva .g_frame_interval = ov2680_s_g_frame_interval, 8763ee47cadSRui Miguel Silva .s_frame_interval = ov2680_s_g_frame_interval, 8773ee47cadSRui Miguel Silva .s_stream = ov2680_s_stream, 8783ee47cadSRui Miguel Silva }; 8793ee47cadSRui Miguel Silva 8803ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 8813ee47cadSRui Miguel Silva .init_cfg = ov2680_init_cfg, 8823ee47cadSRui Miguel Silva .enum_mbus_code = ov2680_enum_mbus_code, 8833ee47cadSRui Miguel Silva .get_fmt = ov2680_get_fmt, 8843ee47cadSRui Miguel Silva .set_fmt = ov2680_set_fmt, 8853ee47cadSRui Miguel Silva .enum_frame_size = ov2680_enum_frame_size, 8863ee47cadSRui Miguel Silva .enum_frame_interval = ov2680_enum_frame_interval, 8873ee47cadSRui Miguel Silva }; 8883ee47cadSRui Miguel Silva 8893ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = { 8903ee47cadSRui Miguel Silva .core = &ov2680_core_ops, 8913ee47cadSRui Miguel Silva .video = &ov2680_video_ops, 8923ee47cadSRui Miguel Silva .pad = &ov2680_pad_ops, 8933ee47cadSRui Miguel Silva }; 8943ee47cadSRui Miguel Silva 8953ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor) 8963ee47cadSRui Miguel Silva { 8973ee47cadSRui Miguel Silva const struct ov2680_mode_info *init_mode; 8983ee47cadSRui Miguel Silva 8993ee47cadSRui Miguel Silva /* set initial mode */ 9003ee47cadSRui Miguel Silva sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10; 9013ee47cadSRui Miguel Silva sensor->fmt.width = 800; 9023ee47cadSRui Miguel Silva sensor->fmt.height = 600; 9033ee47cadSRui Miguel Silva sensor->fmt.field = V4L2_FIELD_NONE; 9043ee47cadSRui Miguel Silva sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB; 9053ee47cadSRui Miguel Silva 9063ee47cadSRui Miguel Silva sensor->frame_interval.denominator = OV2680_FRAME_RATE; 9073ee47cadSRui Miguel Silva sensor->frame_interval.numerator = 1; 9083ee47cadSRui Miguel Silva 9093ee47cadSRui Miguel Silva init_mode = &ov2680_mode_init_data; 9103ee47cadSRui Miguel Silva 9113ee47cadSRui Miguel Silva sensor->current_mode = init_mode; 9123ee47cadSRui Miguel Silva 9133ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 9143ee47cadSRui Miguel Silva 9153ee47cadSRui Miguel Silva return 0; 9163ee47cadSRui Miguel Silva } 9173ee47cadSRui Miguel Silva 918e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor) 9193ee47cadSRui Miguel Silva { 9203ee47cadSRui Miguel Silva const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 9213ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 9223ee47cadSRui Miguel Silva struct v4l2_ctrl_handler *hdl = &ctrls->handler; 9233ee47cadSRui Miguel Silva int ret = 0; 9243ee47cadSRui Miguel Silva 9253ee47cadSRui Miguel Silva v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client, 9263ee47cadSRui Miguel Silva &ov2680_subdev_ops); 9273ee47cadSRui Miguel Silva 9283ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 9293ee47cadSRui Miguel Silva sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 9303ee47cadSRui Miguel Silva #endif 9313ee47cadSRui Miguel Silva sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 9323ee47cadSRui Miguel Silva sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 9333ee47cadSRui Miguel Silva 9343ee47cadSRui Miguel Silva ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 9353ee47cadSRui Miguel Silva if (ret < 0) 9363ee47cadSRui Miguel Silva return ret; 9373ee47cadSRui Miguel Silva 9383ee47cadSRui Miguel Silva v4l2_ctrl_handler_init(hdl, 7); 9393ee47cadSRui Miguel Silva 9403ee47cadSRui Miguel Silva hdl->lock = &sensor->lock; 9413ee47cadSRui Miguel Silva 9423ee47cadSRui Miguel Silva ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 9433ee47cadSRui Miguel Silva ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 9443ee47cadSRui Miguel Silva 9453ee47cadSRui Miguel Silva ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 9463ee47cadSRui Miguel Silva &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 9473ee47cadSRui Miguel Silva ARRAY_SIZE(test_pattern_menu) - 1, 9483ee47cadSRui Miguel Silva 0, 0, test_pattern_menu); 9493ee47cadSRui Miguel Silva 9503ee47cadSRui Miguel Silva ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, 9513ee47cadSRui Miguel Silva V4L2_CID_EXPOSURE_AUTO, 9523ee47cadSRui Miguel Silva V4L2_EXPOSURE_MANUAL, 0, 9533ee47cadSRui Miguel Silva V4L2_EXPOSURE_AUTO); 9543ee47cadSRui Miguel Silva 9553ee47cadSRui Miguel Silva ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 9563ee47cadSRui Miguel Silva 0, 32767, 1, 0); 9573ee47cadSRui Miguel Silva 9583ee47cadSRui Miguel Silva ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, 9593ee47cadSRui Miguel Silva 0, 1, 1, 1); 9603ee47cadSRui Miguel Silva ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0); 9613ee47cadSRui Miguel Silva 9623ee47cadSRui Miguel Silva if (hdl->error) { 9633ee47cadSRui Miguel Silva ret = hdl->error; 9643ee47cadSRui Miguel Silva goto cleanup_entity; 9653ee47cadSRui Miguel Silva } 9663ee47cadSRui Miguel Silva 9673ee47cadSRui Miguel Silva ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; 9683ee47cadSRui Miguel Silva ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; 9693ee47cadSRui Miguel Silva 9703ee47cadSRui Miguel Silva v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); 9713ee47cadSRui Miguel Silva v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); 9723ee47cadSRui Miguel Silva 9733ee47cadSRui Miguel Silva sensor->sd.ctrl_handler = hdl; 9743ee47cadSRui Miguel Silva 9753ee47cadSRui Miguel Silva ret = v4l2_async_register_subdev(&sensor->sd); 9763ee47cadSRui Miguel Silva if (ret < 0) 9773ee47cadSRui Miguel Silva goto cleanup_entity; 9783ee47cadSRui Miguel Silva 9793ee47cadSRui Miguel Silva return 0; 9803ee47cadSRui Miguel Silva 9813ee47cadSRui Miguel Silva cleanup_entity: 9823ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 9833ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(hdl); 9843ee47cadSRui Miguel Silva 9853ee47cadSRui Miguel Silva return ret; 9863ee47cadSRui Miguel Silva } 9873ee47cadSRui Miguel Silva 9883ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor) 9893ee47cadSRui Miguel Silva { 9903ee47cadSRui Miguel Silva int i; 9913ee47cadSRui Miguel Silva 9923ee47cadSRui Miguel Silva for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 9933ee47cadSRui Miguel Silva sensor->supplies[i].supply = ov2680_supply_name[i]; 9943ee47cadSRui Miguel Silva 9953ee47cadSRui Miguel Silva return devm_regulator_bulk_get(&sensor->i2c_client->dev, 9963ee47cadSRui Miguel Silva OV2680_NUM_SUPPLIES, 9973ee47cadSRui Miguel Silva sensor->supplies); 9983ee47cadSRui Miguel Silva } 9993ee47cadSRui Miguel Silva 10003ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor) 10013ee47cadSRui Miguel Silva { 10023ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 10033ee47cadSRui Miguel Silva u32 chip_id; 10043ee47cadSRui Miguel Silva int ret; 10053ee47cadSRui Miguel Silva 10063ee47cadSRui Miguel Silva ov2680_power_on(sensor); 10073ee47cadSRui Miguel Silva 10083ee47cadSRui Miguel Silva ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id); 10093ee47cadSRui Miguel Silva if (ret < 0) { 10103ee47cadSRui Miguel Silva dev_err(dev, "failed to read chip id high\n"); 10113ee47cadSRui Miguel Silva return -ENODEV; 10123ee47cadSRui Miguel Silva } 10133ee47cadSRui Miguel Silva 10143ee47cadSRui Miguel Silva if (chip_id != OV2680_CHIP_ID) { 10153ee47cadSRui Miguel Silva dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n", 10163ee47cadSRui Miguel Silva chip_id, OV2680_CHIP_ID); 10173ee47cadSRui Miguel Silva return -ENODEV; 10183ee47cadSRui Miguel Silva } 10193ee47cadSRui Miguel Silva 10203ee47cadSRui Miguel Silva return 0; 10213ee47cadSRui Miguel Silva } 10223ee47cadSRui Miguel Silva 1023913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor) 10243ee47cadSRui Miguel Silva { 10253ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 10263ee47cadSRui Miguel Silva int ret; 10273ee47cadSRui Miguel Silva 10283ee47cadSRui Miguel Silva sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 10293ee47cadSRui Miguel Silva GPIOD_OUT_HIGH); 10303ee47cadSRui Miguel Silva ret = PTR_ERR_OR_ZERO(sensor->reset_gpio); 10313ee47cadSRui Miguel Silva if (ret < 0) { 10323ee47cadSRui Miguel Silva dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 10333ee47cadSRui Miguel Silva return ret; 10343ee47cadSRui Miguel Silva } 10353ee47cadSRui Miguel Silva 10363ee47cadSRui Miguel Silva sensor->xvclk = devm_clk_get(dev, "xvclk"); 10373ee47cadSRui Miguel Silva if (IS_ERR(sensor->xvclk)) { 10383ee47cadSRui Miguel Silva dev_err(dev, "xvclk clock missing or invalid\n"); 10393ee47cadSRui Miguel Silva return PTR_ERR(sensor->xvclk); 10403ee47cadSRui Miguel Silva } 10413ee47cadSRui Miguel Silva 10423ee47cadSRui Miguel Silva sensor->xvclk_freq = clk_get_rate(sensor->xvclk); 10433ee47cadSRui Miguel Silva if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) { 10443ee47cadSRui Miguel Silva dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n", 10453ee47cadSRui Miguel Silva sensor->xvclk_freq, OV2680_XVCLK_VALUE); 10463ee47cadSRui Miguel Silva return -EINVAL; 10473ee47cadSRui Miguel Silva } 10483ee47cadSRui Miguel Silva 10493ee47cadSRui Miguel Silva return 0; 10503ee47cadSRui Miguel Silva } 10513ee47cadSRui Miguel Silva 10523ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client) 10533ee47cadSRui Miguel Silva { 10543ee47cadSRui Miguel Silva struct device *dev = &client->dev; 10553ee47cadSRui Miguel Silva struct ov2680_dev *sensor; 10563ee47cadSRui Miguel Silva int ret; 10573ee47cadSRui Miguel Silva 10583ee47cadSRui Miguel Silva sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 10593ee47cadSRui Miguel Silva if (!sensor) 10603ee47cadSRui Miguel Silva return -ENOMEM; 10613ee47cadSRui Miguel Silva 10623ee47cadSRui Miguel Silva sensor->i2c_client = client; 10633ee47cadSRui Miguel Silva 1064913c55a8SChristophe JAILLET ret = ov2680_parse_dt(sensor); 10653ee47cadSRui Miguel Silva if (ret < 0) 10663ee47cadSRui Miguel Silva return -EINVAL; 10673ee47cadSRui Miguel Silva 10683ee47cadSRui Miguel Silva ret = ov2680_mode_init(sensor); 10693ee47cadSRui Miguel Silva if (ret < 0) 10703ee47cadSRui Miguel Silva return ret; 10713ee47cadSRui Miguel Silva 10723ee47cadSRui Miguel Silva ret = ov2680_get_regulators(sensor); 10733ee47cadSRui Miguel Silva if (ret < 0) { 10743ee47cadSRui Miguel Silva dev_err(dev, "failed to get regulators\n"); 10753ee47cadSRui Miguel Silva return ret; 10763ee47cadSRui Miguel Silva } 10773ee47cadSRui Miguel Silva 10783ee47cadSRui Miguel Silva mutex_init(&sensor->lock); 10793ee47cadSRui Miguel Silva 1080b7a41762SJavier Martinez Canillas ret = ov2680_check_id(sensor); 10813ee47cadSRui Miguel Silva if (ret < 0) 10823ee47cadSRui Miguel Silva goto lock_destroy; 10833ee47cadSRui Miguel Silva 1084e7f4861dSJavier Martinez Canillas ret = ov2680_v4l2_register(sensor); 10853ee47cadSRui Miguel Silva if (ret < 0) 1086b7a41762SJavier Martinez Canillas goto lock_destroy; 10873ee47cadSRui Miguel Silva 10883ee47cadSRui Miguel Silva dev_info(dev, "ov2680 init correctly\n"); 10893ee47cadSRui Miguel Silva 10903ee47cadSRui Miguel Silva return 0; 10913ee47cadSRui Miguel Silva 10923ee47cadSRui Miguel Silva lock_destroy: 1093b7a41762SJavier Martinez Canillas dev_err(dev, "ov2680 init fail: %d\n", ret); 10943ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 10953ee47cadSRui Miguel Silva 10963ee47cadSRui Miguel Silva return ret; 10973ee47cadSRui Miguel Silva } 10983ee47cadSRui Miguel Silva 10993ee47cadSRui Miguel Silva static int ov2680_remove(struct i2c_client *client) 11003ee47cadSRui Miguel Silva { 11013ee47cadSRui Miguel Silva struct v4l2_subdev *sd = i2c_get_clientdata(client); 11023ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 11033ee47cadSRui Miguel Silva 11043ee47cadSRui Miguel Silva v4l2_async_unregister_subdev(&sensor->sd); 11053ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 11063ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 11073ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(&sensor->ctrls.handler); 11083ee47cadSRui Miguel Silva 11093ee47cadSRui Miguel Silva return 0; 11103ee47cadSRui Miguel Silva } 11113ee47cadSRui Miguel Silva 11123ee47cadSRui Miguel Silva static int __maybe_unused ov2680_suspend(struct device *dev) 11133ee47cadSRui Miguel Silva { 1114*7519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 11153ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 11163ee47cadSRui Miguel Silva 11173ee47cadSRui Miguel Silva if (sensor->is_streaming) 11183ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 11193ee47cadSRui Miguel Silva 11203ee47cadSRui Miguel Silva return 0; 11213ee47cadSRui Miguel Silva } 11223ee47cadSRui Miguel Silva 11233ee47cadSRui Miguel Silva static int __maybe_unused ov2680_resume(struct device *dev) 11243ee47cadSRui Miguel Silva { 1125*7519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 11263ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 11273ee47cadSRui Miguel Silva int ret; 11283ee47cadSRui Miguel Silva 11293ee47cadSRui Miguel Silva if (sensor->is_streaming) { 11303ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 11313ee47cadSRui Miguel Silva if (ret < 0) 11323ee47cadSRui Miguel Silva goto stream_disable; 11333ee47cadSRui Miguel Silva } 11343ee47cadSRui Miguel Silva 11353ee47cadSRui Miguel Silva return 0; 11363ee47cadSRui Miguel Silva 11373ee47cadSRui Miguel Silva stream_disable: 11383ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 11393ee47cadSRui Miguel Silva sensor->is_streaming = false; 11403ee47cadSRui Miguel Silva 11413ee47cadSRui Miguel Silva return ret; 11423ee47cadSRui Miguel Silva } 11433ee47cadSRui Miguel Silva 11443ee47cadSRui Miguel Silva static const struct dev_pm_ops ov2680_pm_ops = { 11453ee47cadSRui Miguel Silva SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume) 11463ee47cadSRui Miguel Silva }; 11473ee47cadSRui Miguel Silva 11483ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = { 11493ee47cadSRui Miguel Silva { .compatible = "ovti,ov2680" }, 11503ee47cadSRui Miguel Silva { /* sentinel */ }, 11513ee47cadSRui Miguel Silva }; 11523ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 11533ee47cadSRui Miguel Silva 11543ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = { 11553ee47cadSRui Miguel Silva .driver = { 11563ee47cadSRui Miguel Silva .name = "ov2680", 11573ee47cadSRui Miguel Silva .pm = &ov2680_pm_ops, 11583ee47cadSRui Miguel Silva .of_match_table = of_match_ptr(ov2680_dt_ids), 11593ee47cadSRui Miguel Silva }, 11603ee47cadSRui Miguel Silva .probe_new = ov2680_probe, 11613ee47cadSRui Miguel Silva .remove = ov2680_remove, 11623ee47cadSRui Miguel Silva }; 11633ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver); 11643ee47cadSRui Miguel Silva 11653ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 11663ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 11673ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2"); 1168