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 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 v4l2_ctrl *exposure; 893ee47cadSRui Miguel Silva struct v4l2_ctrl *gain; 903ee47cadSRui Miguel Silva struct v4l2_ctrl *hflip; 913ee47cadSRui Miguel Silva struct v4l2_ctrl *vflip; 923ee47cadSRui Miguel Silva struct v4l2_ctrl *test_pattern; 933ee47cadSRui Miguel Silva }; 943ee47cadSRui Miguel Silva 953ee47cadSRui Miguel Silva struct ov2680_dev { 963ee47cadSRui Miguel Silva struct i2c_client *i2c_client; 973ee47cadSRui Miguel Silva struct v4l2_subdev sd; 983ee47cadSRui Miguel Silva 993ee47cadSRui Miguel Silva struct media_pad pad; 1003ee47cadSRui Miguel Silva struct clk *xvclk; 1013ee47cadSRui Miguel Silva u32 xvclk_freq; 1023ee47cadSRui Miguel Silva struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES]; 1033ee47cadSRui Miguel Silva 1043ee47cadSRui Miguel Silva struct gpio_desc *reset_gpio; 1053ee47cadSRui Miguel Silva struct mutex lock; /* protect members */ 1063ee47cadSRui Miguel Silva 1073ee47cadSRui Miguel Silva bool mode_pending_changes; 1083ee47cadSRui Miguel Silva bool is_enabled; 1093ee47cadSRui Miguel Silva bool is_streaming; 1103ee47cadSRui Miguel Silva 1113ee47cadSRui Miguel Silva struct ov2680_ctrls ctrls; 1123ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt fmt; 1133ee47cadSRui Miguel Silva struct v4l2_fract frame_interval; 1143ee47cadSRui Miguel Silva 1153ee47cadSRui Miguel Silva const struct ov2680_mode_info *current_mode; 1163ee47cadSRui Miguel Silva }; 1173ee47cadSRui Miguel Silva 1183ee47cadSRui Miguel Silva static const char * const test_pattern_menu[] = { 1193ee47cadSRui Miguel Silva "Disabled", 1203ee47cadSRui Miguel Silva "Color Bars", 1213ee47cadSRui Miguel Silva "Random Data", 1223ee47cadSRui Miguel Silva "Square", 1233ee47cadSRui Miguel Silva "Black Image", 1243ee47cadSRui Miguel Silva }; 1253ee47cadSRui Miguel Silva 1263ee47cadSRui Miguel Silva static const int ov2680_hv_flip_bayer_order[] = { 1273ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SBGGR10_1X10, 1283ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGRBG10_1X10, 1293ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SGBRG10_1X10, 1303ee47cadSRui Miguel Silva MEDIA_BUS_FMT_SRGGB10_1X10, 1313ee47cadSRui Miguel Silva }; 1323ee47cadSRui Miguel Silva 1333ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = { 1343ee47cadSRui Miguel Silva {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20}, 1353ee47cadSRui Miguel Silva {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac}, 1363ee47cadSRui Miguel Silva {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04}, 1373ee47cadSRui Miguel Silva {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00}, 1383ee47cadSRui Miguel Silva {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0}, 1397b5a42e6SHans de Goede {0x3503, 0x03}, 1403ee47cadSRui Miguel Silva }; 1413ee47cadSRui Miguel Silva 1423ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = { 1433ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02}, 1443ee47cadSRui Miguel Silva {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05}, 1453ee47cadSRui Miguel Silva {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11}, 1463ee47cadSRui Miguel Silva {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00}, 1473ee47cadSRui Miguel Silva }; 1483ee47cadSRui Miguel Silva 1493ee47cadSRui Miguel Silva static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = { 1503ee47cadSRui Miguel Silva {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06}, 1513ee47cadSRui Miguel Silva {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06}, 1523ee47cadSRui Miguel Silva {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00}, 1533ee47cadSRui Miguel Silva {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0}, 1543ee47cadSRui Miguel Silva {0x4008, 0x00}, {0x4837, 0x18} 1553ee47cadSRui Miguel Silva }; 1563ee47cadSRui Miguel Silva 1573ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_init_data = { 1583ee47cadSRui Miguel Silva "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600, 1593ee47cadSRui Miguel Silva ov2680_setting_30fps_QUXGA_800_600, 1603ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600), 1613ee47cadSRui Miguel Silva }; 1623ee47cadSRui Miguel Silva 1633ee47cadSRui Miguel Silva static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = { 1643ee47cadSRui Miguel Silva {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 1653ee47cadSRui Miguel Silva 800, 600, ov2680_setting_30fps_QUXGA_800_600, 1663ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)}, 1673ee47cadSRui Miguel Silva {"mode_720p_1280_720", OV2680_MODE_720P_1280_720, 1683ee47cadSRui Miguel Silva 1280, 720, ov2680_setting_30fps_720P_1280_720, 1693ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)}, 1703ee47cadSRui Miguel Silva {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200, 1713ee47cadSRui Miguel Silva 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200, 1723ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)}, 1733ee47cadSRui Miguel Silva }; 1743ee47cadSRui Miguel Silva 1753ee47cadSRui Miguel Silva static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd) 1763ee47cadSRui Miguel Silva { 1773ee47cadSRui Miguel Silva return container_of(sd, struct ov2680_dev, sd); 1783ee47cadSRui Miguel Silva } 1793ee47cadSRui Miguel Silva 1803ee47cadSRui Miguel Silva static struct device *ov2680_to_dev(struct ov2680_dev *sensor) 1813ee47cadSRui Miguel Silva { 1823ee47cadSRui Miguel Silva return &sensor->i2c_client->dev; 1833ee47cadSRui Miguel Silva } 1843ee47cadSRui Miguel Silva 1853ee47cadSRui Miguel Silva static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 1863ee47cadSRui Miguel Silva { 1873ee47cadSRui Miguel Silva return &container_of(ctrl->handler, struct ov2680_dev, 1883ee47cadSRui Miguel Silva ctrls.handler)->sd; 1893ee47cadSRui Miguel Silva } 1903ee47cadSRui Miguel Silva 1913ee47cadSRui Miguel Silva static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg, 1923ee47cadSRui Miguel Silva unsigned int len, u32 val) 1933ee47cadSRui Miguel Silva { 1943ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 1953ee47cadSRui Miguel Silva u8 buf[6]; 1963ee47cadSRui Miguel Silva int ret; 1973ee47cadSRui Miguel Silva 1983ee47cadSRui Miguel Silva if (len > 4) 1993ee47cadSRui Miguel Silva return -EINVAL; 2003ee47cadSRui Miguel Silva 2013ee47cadSRui Miguel Silva put_unaligned_be16(reg, buf); 2023ee47cadSRui Miguel Silva put_unaligned_be32(val << (8 * (4 - len)), buf + 2); 2033ee47cadSRui Miguel Silva ret = i2c_master_send(client, buf, len + 2); 2043ee47cadSRui Miguel Silva if (ret != len + 2) { 2053ee47cadSRui Miguel Silva dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret); 2063ee47cadSRui Miguel Silva return -EIO; 2073ee47cadSRui Miguel Silva } 2083ee47cadSRui Miguel Silva 2093ee47cadSRui Miguel Silva return 0; 2103ee47cadSRui Miguel Silva } 2113ee47cadSRui Miguel Silva 2123ee47cadSRui Miguel Silva #define ov2680_write_reg(s, r, v) \ 2133ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2143ee47cadSRui Miguel Silva 2153ee47cadSRui Miguel Silva #define ov2680_write_reg16(s, r, v) \ 2163ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2173ee47cadSRui Miguel Silva 2183ee47cadSRui Miguel Silva #define ov2680_write_reg24(s, r, v) \ 2193ee47cadSRui Miguel Silva __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2203ee47cadSRui Miguel Silva 2213ee47cadSRui Miguel Silva static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg, 2223ee47cadSRui Miguel Silva unsigned int len, u32 *val) 2233ee47cadSRui Miguel Silva { 2243ee47cadSRui Miguel Silva struct i2c_client *client = sensor->i2c_client; 2253ee47cadSRui Miguel Silva struct i2c_msg msgs[2]; 2263ee47cadSRui Miguel Silva u8 addr_buf[2] = { reg >> 8, reg & 0xff }; 2273ee47cadSRui Miguel Silva u8 data_buf[4] = { 0, }; 2283ee47cadSRui Miguel Silva int ret; 2293ee47cadSRui Miguel Silva 2303ee47cadSRui Miguel Silva if (len > 4) 2313ee47cadSRui Miguel Silva return -EINVAL; 2323ee47cadSRui Miguel Silva 2333ee47cadSRui Miguel Silva msgs[0].addr = client->addr; 2343ee47cadSRui Miguel Silva msgs[0].flags = 0; 2353ee47cadSRui Miguel Silva msgs[0].len = ARRAY_SIZE(addr_buf); 2363ee47cadSRui Miguel Silva msgs[0].buf = addr_buf; 2373ee47cadSRui Miguel Silva 2383ee47cadSRui Miguel Silva msgs[1].addr = client->addr; 2393ee47cadSRui Miguel Silva msgs[1].flags = I2C_M_RD; 2403ee47cadSRui Miguel Silva msgs[1].len = len; 2413ee47cadSRui Miguel Silva msgs[1].buf = &data_buf[4 - len]; 2423ee47cadSRui Miguel Silva 2433ee47cadSRui Miguel Silva ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 2443ee47cadSRui Miguel Silva if (ret != ARRAY_SIZE(msgs)) { 2453ee47cadSRui Miguel Silva dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret); 2463ee47cadSRui Miguel Silva return -EIO; 2473ee47cadSRui Miguel Silva } 2483ee47cadSRui Miguel Silva 2493ee47cadSRui Miguel Silva *val = get_unaligned_be32(data_buf); 2503ee47cadSRui Miguel Silva 2513ee47cadSRui Miguel Silva return 0; 2523ee47cadSRui Miguel Silva } 2533ee47cadSRui Miguel Silva 2543ee47cadSRui Miguel Silva #define ov2680_read_reg(s, r, v) \ 2553ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v) 2563ee47cadSRui Miguel Silva 2573ee47cadSRui Miguel Silva #define ov2680_read_reg16(s, r, v) \ 2583ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v) 2593ee47cadSRui Miguel Silva 2603ee47cadSRui Miguel Silva #define ov2680_read_reg24(s, r, v) \ 2613ee47cadSRui Miguel Silva __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v) 2623ee47cadSRui Miguel Silva 2633ee47cadSRui Miguel Silva static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val) 2643ee47cadSRui Miguel Silva { 2653ee47cadSRui Miguel Silva u32 readval; 2663ee47cadSRui Miguel Silva int ret; 2673ee47cadSRui Miguel Silva 2683ee47cadSRui Miguel Silva ret = ov2680_read_reg(sensor, reg, &readval); 2693ee47cadSRui Miguel Silva if (ret < 0) 2703ee47cadSRui Miguel Silva return ret; 2713ee47cadSRui Miguel Silva 2723ee47cadSRui Miguel Silva readval &= ~mask; 2733ee47cadSRui Miguel Silva val &= mask; 2743ee47cadSRui Miguel Silva val |= readval; 2753ee47cadSRui Miguel Silva 2763ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, reg, val); 2773ee47cadSRui Miguel Silva } 2783ee47cadSRui Miguel Silva 2793ee47cadSRui Miguel Silva static int ov2680_load_regs(struct ov2680_dev *sensor, 2803ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode) 2813ee47cadSRui Miguel Silva { 2823ee47cadSRui Miguel Silva const struct reg_value *regs = mode->reg_data; 2833ee47cadSRui Miguel Silva unsigned int i; 2843ee47cadSRui Miguel Silva int ret = 0; 2853ee47cadSRui Miguel Silva u16 reg_addr; 2863ee47cadSRui Miguel Silva u8 val; 2873ee47cadSRui Miguel Silva 2883ee47cadSRui Miguel Silva for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 2893ee47cadSRui Miguel Silva reg_addr = regs->reg_addr; 2903ee47cadSRui Miguel Silva val = regs->val; 2913ee47cadSRui Miguel Silva 2923ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, reg_addr, val); 2933ee47cadSRui Miguel Silva if (ret) 2943ee47cadSRui Miguel Silva break; 2953ee47cadSRui Miguel Silva } 2963ee47cadSRui Miguel Silva 2973ee47cadSRui Miguel Silva return ret; 2983ee47cadSRui Miguel Silva } 2993ee47cadSRui Miguel Silva 3003ee47cadSRui Miguel Silva static void ov2680_power_up(struct ov2680_dev *sensor) 3013ee47cadSRui Miguel Silva { 3023ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3033ee47cadSRui Miguel Silva return; 3043ee47cadSRui Miguel Silva 3053ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 0); 3063ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3073ee47cadSRui Miguel Silva } 3083ee47cadSRui Miguel Silva 3093ee47cadSRui Miguel Silva static void ov2680_power_down(struct ov2680_dev *sensor) 3103ee47cadSRui Miguel Silva { 3113ee47cadSRui Miguel Silva if (!sensor->reset_gpio) 3123ee47cadSRui Miguel Silva return; 3133ee47cadSRui Miguel Silva 3143ee47cadSRui Miguel Silva gpiod_set_value(sensor->reset_gpio, 1); 3153ee47cadSRui Miguel Silva usleep_range(5000, 10000); 3163ee47cadSRui Miguel Silva } 3173ee47cadSRui Miguel Silva 31850a7bad4SHans de Goede static void ov2680_set_bayer_order(struct ov2680_dev *sensor) 3193ee47cadSRui Miguel Silva { 32050a7bad4SHans de Goede int hv_flip = 0; 3213ee47cadSRui Miguel Silva 32250a7bad4SHans de Goede if (sensor->ctrls.vflip && sensor->ctrls.vflip->val) 32350a7bad4SHans de Goede hv_flip += 1; 3243ee47cadSRui Miguel Silva 32550a7bad4SHans de Goede if (sensor->ctrls.hflip && sensor->ctrls.hflip->val) 32650a7bad4SHans de Goede hv_flip += 2; 3273ee47cadSRui Miguel Silva 3283ee47cadSRui Miguel Silva sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip]; 3293ee47cadSRui Miguel Silva } 3303ee47cadSRui Miguel Silva 331*d5d08ad3SHans de Goede static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val) 3323ee47cadSRui Miguel Silva { 3333ee47cadSRui Miguel Silva int ret; 3343ee47cadSRui Miguel Silva 335*d5d08ad3SHans de Goede if (sensor->is_streaming) 336*d5d08ad3SHans de Goede return -EBUSY; 337*d5d08ad3SHans de Goede 338*d5d08ad3SHans de Goede ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, 339*d5d08ad3SHans de Goede BIT(2), val ? BIT(2) : 0); 3403ee47cadSRui Miguel Silva if (ret < 0) 3413ee47cadSRui Miguel Silva return ret; 3423ee47cadSRui Miguel Silva 34350a7bad4SHans de Goede ov2680_set_bayer_order(sensor); 34450a7bad4SHans de Goede return 0; 3453ee47cadSRui Miguel Silva } 3463ee47cadSRui Miguel Silva 347*d5d08ad3SHans de Goede static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val) 3483ee47cadSRui Miguel Silva { 3493ee47cadSRui Miguel Silva int ret; 3503ee47cadSRui Miguel Silva 351*d5d08ad3SHans de Goede if (sensor->is_streaming) 352*d5d08ad3SHans de Goede return -EBUSY; 3533ee47cadSRui Miguel Silva 354*d5d08ad3SHans de Goede ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, 355*d5d08ad3SHans de Goede BIT(2), val ? BIT(2) : 0); 3563ee47cadSRui Miguel Silva if (ret < 0) 3573ee47cadSRui Miguel Silva return ret; 3583ee47cadSRui Miguel Silva 35950a7bad4SHans de Goede ov2680_set_bayer_order(sensor); 36050a7bad4SHans de Goede return 0; 3613ee47cadSRui Miguel Silva } 3623ee47cadSRui Miguel Silva 3633ee47cadSRui Miguel Silva static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value) 3643ee47cadSRui Miguel Silva { 3653ee47cadSRui Miguel Silva int ret; 3663ee47cadSRui Miguel Silva 3673ee47cadSRui Miguel Silva if (!value) 3683ee47cadSRui Miguel Silva return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0); 3693ee47cadSRui Miguel Silva 3703ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1); 3713ee47cadSRui Miguel Silva if (ret < 0) 3723ee47cadSRui Miguel Silva return ret; 3733ee47cadSRui Miguel Silva 3743ee47cadSRui Miguel Silva ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7)); 3753ee47cadSRui Miguel Silva if (ret < 0) 3763ee47cadSRui Miguel Silva return ret; 3773ee47cadSRui Miguel Silva 3783ee47cadSRui Miguel Silva return 0; 3793ee47cadSRui Miguel Silva } 3803ee47cadSRui Miguel Silva 3817b5a42e6SHans de Goede static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain) 3823ee47cadSRui Miguel Silva { 3837b5a42e6SHans de Goede return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain); 3843ee47cadSRui Miguel Silva } 3853ee47cadSRui Miguel Silva 3867b5a42e6SHans de Goede static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp) 3873ee47cadSRui Miguel Silva { 3887b5a42e6SHans de Goede return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, 3897b5a42e6SHans de Goede exp << 4); 3903ee47cadSRui Miguel Silva } 3913ee47cadSRui Miguel Silva 3923ee47cadSRui Miguel Silva static int ov2680_stream_enable(struct ov2680_dev *sensor) 3933ee47cadSRui Miguel Silva { 3943ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1); 3953ee47cadSRui Miguel Silva } 3963ee47cadSRui Miguel Silva 3973ee47cadSRui Miguel Silva static int ov2680_stream_disable(struct ov2680_dev *sensor) 3983ee47cadSRui Miguel Silva { 3993ee47cadSRui Miguel Silva return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0); 4003ee47cadSRui Miguel Silva } 4013ee47cadSRui Miguel Silva 4023ee47cadSRui Miguel Silva static int ov2680_mode_set(struct ov2680_dev *sensor) 4033ee47cadSRui Miguel Silva { 4043ee47cadSRui Miguel Silva int ret; 4053ee47cadSRui Miguel Silva 4063ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, sensor->current_mode); 4073ee47cadSRui Miguel Silva if (ret < 0) 4083ee47cadSRui Miguel Silva return ret; 4093ee47cadSRui Miguel Silva 4107b5a42e6SHans de Goede /* Restore value of all ctrls */ 4117b5a42e6SHans de Goede ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 4123ee47cadSRui Miguel Silva if (ret < 0) 4133ee47cadSRui Miguel Silva return ret; 4143ee47cadSRui Miguel Silva 4153ee47cadSRui Miguel Silva sensor->mode_pending_changes = false; 4163ee47cadSRui Miguel Silva 4173ee47cadSRui Miguel Silva return 0; 4183ee47cadSRui Miguel Silva } 4193ee47cadSRui Miguel Silva 4203ee47cadSRui Miguel Silva static int ov2680_mode_restore(struct ov2680_dev *sensor) 4213ee47cadSRui Miguel Silva { 4223ee47cadSRui Miguel Silva int ret; 4233ee47cadSRui Miguel Silva 4243ee47cadSRui Miguel Silva ret = ov2680_load_regs(sensor, &ov2680_mode_init_data); 4253ee47cadSRui Miguel Silva if (ret < 0) 4263ee47cadSRui Miguel Silva return ret; 4273ee47cadSRui Miguel Silva 4283ee47cadSRui Miguel Silva return ov2680_mode_set(sensor); 4293ee47cadSRui Miguel Silva } 4303ee47cadSRui Miguel Silva 4313ee47cadSRui Miguel Silva static int ov2680_power_off(struct ov2680_dev *sensor) 4323ee47cadSRui Miguel Silva { 4333ee47cadSRui Miguel Silva if (!sensor->is_enabled) 4343ee47cadSRui Miguel Silva return 0; 4353ee47cadSRui Miguel Silva 4363ee47cadSRui Miguel Silva clk_disable_unprepare(sensor->xvclk); 4373ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4383ee47cadSRui Miguel Silva regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies); 4393ee47cadSRui Miguel Silva sensor->is_enabled = false; 4403ee47cadSRui Miguel Silva 4413ee47cadSRui Miguel Silva return 0; 4423ee47cadSRui Miguel Silva } 4433ee47cadSRui Miguel Silva 4443ee47cadSRui Miguel Silva static int ov2680_power_on(struct ov2680_dev *sensor) 4453ee47cadSRui Miguel Silva { 4463ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 4473ee47cadSRui Miguel Silva int ret; 4483ee47cadSRui Miguel Silva 4493ee47cadSRui Miguel Silva if (sensor->is_enabled) 4503ee47cadSRui Miguel Silva return 0; 4513ee47cadSRui Miguel Silva 4523ee47cadSRui Miguel Silva ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies); 4533ee47cadSRui Miguel Silva if (ret < 0) { 4543ee47cadSRui Miguel Silva dev_err(dev, "failed to enable regulators: %d\n", ret); 4553ee47cadSRui Miguel Silva return ret; 4563ee47cadSRui Miguel Silva } 4573ee47cadSRui Miguel Silva 4583ee47cadSRui Miguel Silva if (!sensor->reset_gpio) { 4593ee47cadSRui Miguel Silva ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01); 4603ee47cadSRui Miguel Silva if (ret != 0) { 4613ee47cadSRui Miguel Silva dev_err(dev, "sensor soft reset failed\n"); 4623ee47cadSRui Miguel Silva return ret; 4633ee47cadSRui Miguel Silva } 4643ee47cadSRui Miguel Silva usleep_range(1000, 2000); 4653ee47cadSRui Miguel Silva } else { 4663ee47cadSRui Miguel Silva ov2680_power_down(sensor); 4673ee47cadSRui Miguel Silva ov2680_power_up(sensor); 4683ee47cadSRui Miguel Silva } 4693ee47cadSRui Miguel Silva 4703ee47cadSRui Miguel Silva ret = clk_prepare_enable(sensor->xvclk); 4713ee47cadSRui Miguel Silva if (ret < 0) 4723ee47cadSRui Miguel Silva return ret; 4733ee47cadSRui Miguel Silva 4743ee47cadSRui Miguel Silva sensor->is_enabled = true; 4753ee47cadSRui Miguel Silva 4763ee47cadSRui Miguel Silva /* Set clock lane into LP-11 state */ 4773ee47cadSRui Miguel Silva ov2680_stream_enable(sensor); 4783ee47cadSRui Miguel Silva usleep_range(1000, 2000); 4793ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 4803ee47cadSRui Miguel Silva 4813ee47cadSRui Miguel Silva return 0; 4823ee47cadSRui Miguel Silva } 4833ee47cadSRui Miguel Silva 4843ee47cadSRui Miguel Silva static int ov2680_s_power(struct v4l2_subdev *sd, int on) 4853ee47cadSRui Miguel Silva { 4863ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 4873ee47cadSRui Miguel Silva int ret = 0; 4883ee47cadSRui Miguel Silva 4893ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 4903ee47cadSRui Miguel Silva 4913ee47cadSRui Miguel Silva if (on) 4923ee47cadSRui Miguel Silva ret = ov2680_power_on(sensor); 4933ee47cadSRui Miguel Silva else 4943ee47cadSRui Miguel Silva ret = ov2680_power_off(sensor); 4953ee47cadSRui Miguel Silva 4967b5a42e6SHans de Goede if (on && ret == 0) 497c45fbdf2SRui Miguel Silva ret = ov2680_mode_restore(sensor); 4987b5a42e6SHans de Goede 4997b5a42e6SHans de Goede mutex_unlock(&sensor->lock); 5003ee47cadSRui Miguel Silva 5013ee47cadSRui Miguel Silva return ret; 5023ee47cadSRui Miguel Silva } 5033ee47cadSRui Miguel Silva 5043ee47cadSRui Miguel Silva static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd, 5053ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval *fi) 5063ee47cadSRui Miguel Silva { 5073ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5083ee47cadSRui Miguel Silva 5093ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5103ee47cadSRui Miguel Silva fi->interval = sensor->frame_interval; 5113ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5123ee47cadSRui Miguel Silva 5133ee47cadSRui Miguel Silva return 0; 5143ee47cadSRui Miguel Silva } 5153ee47cadSRui Miguel Silva 5163ee47cadSRui Miguel Silva static int ov2680_s_stream(struct v4l2_subdev *sd, int enable) 5173ee47cadSRui Miguel Silva { 5183ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5193ee47cadSRui Miguel Silva int ret = 0; 5203ee47cadSRui Miguel Silva 5213ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5223ee47cadSRui Miguel Silva 5233ee47cadSRui Miguel Silva if (sensor->is_streaming == !!enable) 5243ee47cadSRui Miguel Silva goto unlock; 5253ee47cadSRui Miguel Silva 5263ee47cadSRui Miguel Silva if (enable && sensor->mode_pending_changes) { 5273ee47cadSRui Miguel Silva ret = ov2680_mode_set(sensor); 5283ee47cadSRui Miguel Silva if (ret < 0) 5293ee47cadSRui Miguel Silva goto unlock; 5303ee47cadSRui Miguel Silva } 5313ee47cadSRui Miguel Silva 5323ee47cadSRui Miguel Silva if (enable) 5333ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 5343ee47cadSRui Miguel Silva else 5353ee47cadSRui Miguel Silva ret = ov2680_stream_disable(sensor); 5363ee47cadSRui Miguel Silva 5373ee47cadSRui Miguel Silva sensor->is_streaming = !!enable; 5383ee47cadSRui Miguel Silva 5393ee47cadSRui Miguel Silva unlock: 5403ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5413ee47cadSRui Miguel Silva 5423ee47cadSRui Miguel Silva return ret; 5433ee47cadSRui Miguel Silva } 5443ee47cadSRui Miguel Silva 5453ee47cadSRui Miguel Silva static int ov2680_enum_mbus_code(struct v4l2_subdev *sd, 5460d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5473ee47cadSRui Miguel Silva struct v4l2_subdev_mbus_code_enum *code) 5483ee47cadSRui Miguel Silva { 5493ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5503ee47cadSRui Miguel Silva 5513ee47cadSRui Miguel Silva if (code->pad != 0 || code->index != 0) 5523ee47cadSRui Miguel Silva return -EINVAL; 5533ee47cadSRui Miguel Silva 5543ee47cadSRui Miguel Silva code->code = sensor->fmt.code; 5553ee47cadSRui Miguel Silva 5563ee47cadSRui Miguel Silva return 0; 5573ee47cadSRui Miguel Silva } 5583ee47cadSRui Miguel Silva 5593ee47cadSRui Miguel Silva static int ov2680_get_fmt(struct v4l2_subdev *sd, 5600d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5613ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 5623ee47cadSRui Miguel Silva { 5633ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5643ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *fmt = NULL; 5653ee47cadSRui Miguel Silva int ret = 0; 5663ee47cadSRui Miguel Silva 5673ee47cadSRui Miguel Silva if (format->pad != 0) 5683ee47cadSRui Miguel Silva return -EINVAL; 5693ee47cadSRui Miguel Silva 5703ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 5713ee47cadSRui Miguel Silva 5723ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 5733ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 5740d346d2aSTomi Valkeinen fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, 5750d346d2aSTomi Valkeinen format->pad); 5763ee47cadSRui Miguel Silva #else 5772dbcb6fbSHans Verkuil ret = -EINVAL; 5783ee47cadSRui Miguel Silva #endif 5793ee47cadSRui Miguel Silva } else { 5803ee47cadSRui Miguel Silva fmt = &sensor->fmt; 5813ee47cadSRui Miguel Silva } 5823ee47cadSRui Miguel Silva 5833ee47cadSRui Miguel Silva if (fmt) 5843ee47cadSRui Miguel Silva format->format = *fmt; 5853ee47cadSRui Miguel Silva 5863ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 5873ee47cadSRui Miguel Silva 5883ee47cadSRui Miguel Silva return ret; 5893ee47cadSRui Miguel Silva } 5903ee47cadSRui Miguel Silva 5913ee47cadSRui Miguel Silva static int ov2680_set_fmt(struct v4l2_subdev *sd, 5920d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 5933ee47cadSRui Miguel Silva struct v4l2_subdev_format *format) 5943ee47cadSRui Miguel Silva { 5953ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 5963ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *fmt = &format->format; 5973ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 5983ee47cadSRui Miguel Silva struct v4l2_mbus_framefmt *try_fmt; 5993ee47cadSRui Miguel Silva #endif 6003ee47cadSRui Miguel Silva const struct ov2680_mode_info *mode; 6013ee47cadSRui Miguel Silva int ret = 0; 6023ee47cadSRui Miguel Silva 6033ee47cadSRui Miguel Silva if (format->pad != 0) 6043ee47cadSRui Miguel Silva return -EINVAL; 6053ee47cadSRui Miguel Silva 6063ee47cadSRui Miguel Silva mutex_lock(&sensor->lock); 6073ee47cadSRui Miguel Silva 6083ee47cadSRui Miguel Silva if (sensor->is_streaming) { 6093ee47cadSRui Miguel Silva ret = -EBUSY; 6103ee47cadSRui Miguel Silva goto unlock; 6113ee47cadSRui Miguel Silva } 6123ee47cadSRui Miguel Silva 6133ee47cadSRui Miguel Silva mode = v4l2_find_nearest_size(ov2680_mode_data, 6143ee47cadSRui Miguel Silva ARRAY_SIZE(ov2680_mode_data), width, 6153ee47cadSRui Miguel Silva height, fmt->width, fmt->height); 6163ee47cadSRui Miguel Silva if (!mode) { 6173ee47cadSRui Miguel Silva ret = -EINVAL; 6183ee47cadSRui Miguel Silva goto unlock; 6193ee47cadSRui Miguel Silva } 6203ee47cadSRui Miguel Silva 6213ee47cadSRui Miguel Silva if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 6223ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 6230d346d2aSTomi Valkeinen try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0); 6243ee47cadSRui Miguel Silva format->format = *try_fmt; 6253ee47cadSRui Miguel Silva #endif 6263ee47cadSRui Miguel Silva goto unlock; 6273ee47cadSRui Miguel Silva } 6283ee47cadSRui Miguel Silva 6293ee47cadSRui Miguel Silva fmt->width = mode->width; 6303ee47cadSRui Miguel Silva fmt->height = mode->height; 6313ee47cadSRui Miguel Silva fmt->code = sensor->fmt.code; 6323ee47cadSRui Miguel Silva fmt->colorspace = sensor->fmt.colorspace; 6333ee47cadSRui Miguel Silva 6343ee47cadSRui Miguel Silva sensor->current_mode = mode; 6353ee47cadSRui Miguel Silva sensor->fmt = format->format; 6363ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 6373ee47cadSRui Miguel Silva 6383ee47cadSRui Miguel Silva unlock: 6393ee47cadSRui Miguel Silva mutex_unlock(&sensor->lock); 6403ee47cadSRui Miguel Silva 6413ee47cadSRui Miguel Silva return ret; 6423ee47cadSRui Miguel Silva } 6433ee47cadSRui Miguel Silva 6443ee47cadSRui Miguel Silva static int ov2680_init_cfg(struct v4l2_subdev *sd, 6450d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 6463ee47cadSRui Miguel Silva { 6473ee47cadSRui Miguel Silva struct v4l2_subdev_format fmt = { 6480d346d2aSTomi Valkeinen .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY 6493ee47cadSRui Miguel Silva : V4L2_SUBDEV_FORMAT_ACTIVE, 6503ee47cadSRui Miguel Silva .format = { 6513ee47cadSRui Miguel Silva .width = 800, 6523ee47cadSRui Miguel Silva .height = 600, 6533ee47cadSRui Miguel Silva } 6543ee47cadSRui Miguel Silva }; 6553ee47cadSRui Miguel Silva 6560d346d2aSTomi Valkeinen return ov2680_set_fmt(sd, sd_state, &fmt); 6573ee47cadSRui Miguel Silva } 6583ee47cadSRui Miguel Silva 6593ee47cadSRui Miguel Silva static int ov2680_enum_frame_size(struct v4l2_subdev *sd, 6600d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6613ee47cadSRui Miguel Silva struct v4l2_subdev_frame_size_enum *fse) 6623ee47cadSRui Miguel Silva { 6633ee47cadSRui Miguel Silva int index = fse->index; 6643ee47cadSRui Miguel Silva 6653ee47cadSRui Miguel Silva if (index >= OV2680_MODE_MAX || index < 0) 6663ee47cadSRui Miguel Silva return -EINVAL; 6673ee47cadSRui Miguel Silva 6683ee47cadSRui Miguel Silva fse->min_width = ov2680_mode_data[index].width; 6693ee47cadSRui Miguel Silva fse->min_height = ov2680_mode_data[index].height; 6703ee47cadSRui Miguel Silva fse->max_width = ov2680_mode_data[index].width; 6713ee47cadSRui Miguel Silva fse->max_height = ov2680_mode_data[index].height; 6723ee47cadSRui Miguel Silva 6733ee47cadSRui Miguel Silva return 0; 6743ee47cadSRui Miguel Silva } 6753ee47cadSRui Miguel Silva 6763ee47cadSRui Miguel Silva static int ov2680_enum_frame_interval(struct v4l2_subdev *sd, 6770d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 6783ee47cadSRui Miguel Silva struct v4l2_subdev_frame_interval_enum *fie) 6793ee47cadSRui Miguel Silva { 6803ee47cadSRui Miguel Silva struct v4l2_fract tpf; 6813ee47cadSRui Miguel Silva 6823ee47cadSRui Miguel Silva if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX || 6833ee47cadSRui Miguel Silva fie->height > OV2680_HEIGHT_MAX || 6843ee47cadSRui Miguel Silva fie->which > V4L2_SUBDEV_FORMAT_ACTIVE) 6853ee47cadSRui Miguel Silva return -EINVAL; 6863ee47cadSRui Miguel Silva 6873ee47cadSRui Miguel Silva tpf.denominator = OV2680_FRAME_RATE; 6883ee47cadSRui Miguel Silva tpf.numerator = 1; 6893ee47cadSRui Miguel Silva 6903ee47cadSRui Miguel Silva fie->interval = tpf; 6913ee47cadSRui Miguel Silva 6923ee47cadSRui Miguel Silva return 0; 6933ee47cadSRui Miguel Silva } 6943ee47cadSRui Miguel Silva 6953ee47cadSRui Miguel Silva static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl) 6963ee47cadSRui Miguel Silva { 6973ee47cadSRui Miguel Silva struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 6983ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 6993ee47cadSRui Miguel Silva 7003ee47cadSRui Miguel Silva if (!sensor->is_enabled) 7013ee47cadSRui Miguel Silva return 0; 7023ee47cadSRui Miguel Silva 7033ee47cadSRui Miguel Silva switch (ctrl->id) { 7043ee47cadSRui Miguel Silva case V4L2_CID_GAIN: 7057b5a42e6SHans de Goede return ov2680_gain_set(sensor, ctrl->val); 7063ee47cadSRui Miguel Silva case V4L2_CID_EXPOSURE: 7077b5a42e6SHans de Goede return ov2680_exposure_set(sensor, ctrl->val); 7083ee47cadSRui Miguel Silva case V4L2_CID_VFLIP: 709*d5d08ad3SHans de Goede return ov2680_set_vflip(sensor, ctrl->val); 7103ee47cadSRui Miguel Silva case V4L2_CID_HFLIP: 711*d5d08ad3SHans de Goede return ov2680_set_hflip(sensor, ctrl->val); 7123ee47cadSRui Miguel Silva case V4L2_CID_TEST_PATTERN: 7133ee47cadSRui Miguel Silva return ov2680_test_pattern_set(sensor, ctrl->val); 7143ee47cadSRui Miguel Silva default: 7153ee47cadSRui Miguel Silva break; 7163ee47cadSRui Miguel Silva } 7173ee47cadSRui Miguel Silva 7183ee47cadSRui Miguel Silva return -EINVAL; 7193ee47cadSRui Miguel Silva } 7203ee47cadSRui Miguel Silva 7213ee47cadSRui Miguel Silva static const struct v4l2_ctrl_ops ov2680_ctrl_ops = { 7223ee47cadSRui Miguel Silva .s_ctrl = ov2680_s_ctrl, 7233ee47cadSRui Miguel Silva }; 7243ee47cadSRui Miguel Silva 7253ee47cadSRui Miguel Silva static const struct v4l2_subdev_core_ops ov2680_core_ops = { 7263ee47cadSRui Miguel Silva .s_power = ov2680_s_power, 7273ee47cadSRui Miguel Silva }; 7283ee47cadSRui Miguel Silva 7293ee47cadSRui Miguel Silva static const struct v4l2_subdev_video_ops ov2680_video_ops = { 7303ee47cadSRui Miguel Silva .g_frame_interval = ov2680_s_g_frame_interval, 7313ee47cadSRui Miguel Silva .s_frame_interval = ov2680_s_g_frame_interval, 7323ee47cadSRui Miguel Silva .s_stream = ov2680_s_stream, 7333ee47cadSRui Miguel Silva }; 7343ee47cadSRui Miguel Silva 7353ee47cadSRui Miguel Silva static const struct v4l2_subdev_pad_ops ov2680_pad_ops = { 7363ee47cadSRui Miguel Silva .init_cfg = ov2680_init_cfg, 7373ee47cadSRui Miguel Silva .enum_mbus_code = ov2680_enum_mbus_code, 7383ee47cadSRui Miguel Silva .get_fmt = ov2680_get_fmt, 7393ee47cadSRui Miguel Silva .set_fmt = ov2680_set_fmt, 7403ee47cadSRui Miguel Silva .enum_frame_size = ov2680_enum_frame_size, 7413ee47cadSRui Miguel Silva .enum_frame_interval = ov2680_enum_frame_interval, 7423ee47cadSRui Miguel Silva }; 7433ee47cadSRui Miguel Silva 7443ee47cadSRui Miguel Silva static const struct v4l2_subdev_ops ov2680_subdev_ops = { 7453ee47cadSRui Miguel Silva .core = &ov2680_core_ops, 7463ee47cadSRui Miguel Silva .video = &ov2680_video_ops, 7473ee47cadSRui Miguel Silva .pad = &ov2680_pad_ops, 7483ee47cadSRui Miguel Silva }; 7493ee47cadSRui Miguel Silva 7503ee47cadSRui Miguel Silva static int ov2680_mode_init(struct ov2680_dev *sensor) 7513ee47cadSRui Miguel Silva { 7523ee47cadSRui Miguel Silva const struct ov2680_mode_info *init_mode; 7533ee47cadSRui Miguel Silva 7543ee47cadSRui Miguel Silva /* set initial mode */ 7553ee47cadSRui Miguel Silva sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10; 7563ee47cadSRui Miguel Silva sensor->fmt.width = 800; 7573ee47cadSRui Miguel Silva sensor->fmt.height = 600; 7583ee47cadSRui Miguel Silva sensor->fmt.field = V4L2_FIELD_NONE; 7593ee47cadSRui Miguel Silva sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB; 7603ee47cadSRui Miguel Silva 7613ee47cadSRui Miguel Silva sensor->frame_interval.denominator = OV2680_FRAME_RATE; 7623ee47cadSRui Miguel Silva sensor->frame_interval.numerator = 1; 7633ee47cadSRui Miguel Silva 7643ee47cadSRui Miguel Silva init_mode = &ov2680_mode_init_data; 7653ee47cadSRui Miguel Silva 7663ee47cadSRui Miguel Silva sensor->current_mode = init_mode; 7673ee47cadSRui Miguel Silva 7683ee47cadSRui Miguel Silva sensor->mode_pending_changes = true; 7693ee47cadSRui Miguel Silva 7703ee47cadSRui Miguel Silva return 0; 7713ee47cadSRui Miguel Silva } 7723ee47cadSRui Miguel Silva 773e7f4861dSJavier Martinez Canillas static int ov2680_v4l2_register(struct ov2680_dev *sensor) 7743ee47cadSRui Miguel Silva { 7753ee47cadSRui Miguel Silva const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops; 7763ee47cadSRui Miguel Silva struct ov2680_ctrls *ctrls = &sensor->ctrls; 7773ee47cadSRui Miguel Silva struct v4l2_ctrl_handler *hdl = &ctrls->handler; 7783ee47cadSRui Miguel Silva int ret = 0; 7793ee47cadSRui Miguel Silva 7803ee47cadSRui Miguel Silva v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client, 7813ee47cadSRui Miguel Silva &ov2680_subdev_ops); 7823ee47cadSRui Miguel Silva 7833ee47cadSRui Miguel Silva #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API 7843ee47cadSRui Miguel Silva sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 7853ee47cadSRui Miguel Silva #endif 7863ee47cadSRui Miguel Silva sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 7873ee47cadSRui Miguel Silva sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 7883ee47cadSRui Miguel Silva 7893ee47cadSRui Miguel Silva ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 7903ee47cadSRui Miguel Silva if (ret < 0) 7913ee47cadSRui Miguel Silva return ret; 7923ee47cadSRui Miguel Silva 7937b5a42e6SHans de Goede v4l2_ctrl_handler_init(hdl, 5); 7943ee47cadSRui Miguel Silva 7953ee47cadSRui Miguel Silva hdl->lock = &sensor->lock; 7963ee47cadSRui Miguel Silva 7973ee47cadSRui Miguel Silva ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 7983ee47cadSRui Miguel Silva ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 7993ee47cadSRui Miguel Silva 8003ee47cadSRui Miguel Silva ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl, 8013ee47cadSRui Miguel Silva &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN, 8023ee47cadSRui Miguel Silva ARRAY_SIZE(test_pattern_menu) - 1, 8033ee47cadSRui Miguel Silva 0, 0, test_pattern_menu); 8043ee47cadSRui Miguel Silva 8053ee47cadSRui Miguel Silva ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 8063ee47cadSRui Miguel Silva 0, 32767, 1, 0); 8073ee47cadSRui Miguel Silva 8083ee47cadSRui Miguel Silva ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0); 8093ee47cadSRui Miguel Silva 8103ee47cadSRui Miguel Silva if (hdl->error) { 8113ee47cadSRui Miguel Silva ret = hdl->error; 8123ee47cadSRui Miguel Silva goto cleanup_entity; 8133ee47cadSRui Miguel Silva } 8143ee47cadSRui Miguel Silva 81566274280SDave Stevenson ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 81666274280SDave Stevenson ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 8173ee47cadSRui Miguel Silva 8183ee47cadSRui Miguel Silva sensor->sd.ctrl_handler = hdl; 8193ee47cadSRui Miguel Silva 8203ee47cadSRui Miguel Silva ret = v4l2_async_register_subdev(&sensor->sd); 8213ee47cadSRui Miguel Silva if (ret < 0) 8223ee47cadSRui Miguel Silva goto cleanup_entity; 8233ee47cadSRui Miguel Silva 8243ee47cadSRui Miguel Silva return 0; 8253ee47cadSRui Miguel Silva 8263ee47cadSRui Miguel Silva cleanup_entity: 8273ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 8283ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(hdl); 8293ee47cadSRui Miguel Silva 8303ee47cadSRui Miguel Silva return ret; 8313ee47cadSRui Miguel Silva } 8323ee47cadSRui Miguel Silva 8333ee47cadSRui Miguel Silva static int ov2680_get_regulators(struct ov2680_dev *sensor) 8343ee47cadSRui Miguel Silva { 8353ee47cadSRui Miguel Silva int i; 8363ee47cadSRui Miguel Silva 8373ee47cadSRui Miguel Silva for (i = 0; i < OV2680_NUM_SUPPLIES; i++) 8383ee47cadSRui Miguel Silva sensor->supplies[i].supply = ov2680_supply_name[i]; 8393ee47cadSRui Miguel Silva 8403ee47cadSRui Miguel Silva return devm_regulator_bulk_get(&sensor->i2c_client->dev, 8413ee47cadSRui Miguel Silva OV2680_NUM_SUPPLIES, 8423ee47cadSRui Miguel Silva sensor->supplies); 8433ee47cadSRui Miguel Silva } 8443ee47cadSRui Miguel Silva 8453ee47cadSRui Miguel Silva static int ov2680_check_id(struct ov2680_dev *sensor) 8463ee47cadSRui Miguel Silva { 8473ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 8483ee47cadSRui Miguel Silva u32 chip_id; 8493ee47cadSRui Miguel Silva int ret; 8503ee47cadSRui Miguel Silva 8513ee47cadSRui Miguel Silva ov2680_power_on(sensor); 8523ee47cadSRui Miguel Silva 8533ee47cadSRui Miguel Silva ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id); 8543ee47cadSRui Miguel Silva if (ret < 0) { 8553ee47cadSRui Miguel Silva dev_err(dev, "failed to read chip id high\n"); 8563ee47cadSRui Miguel Silva return -ENODEV; 8573ee47cadSRui Miguel Silva } 8583ee47cadSRui Miguel Silva 8593ee47cadSRui Miguel Silva if (chip_id != OV2680_CHIP_ID) { 8603ee47cadSRui Miguel Silva dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n", 8613ee47cadSRui Miguel Silva chip_id, OV2680_CHIP_ID); 8623ee47cadSRui Miguel Silva return -ENODEV; 8633ee47cadSRui Miguel Silva } 8643ee47cadSRui Miguel Silva 8653ee47cadSRui Miguel Silva return 0; 8663ee47cadSRui Miguel Silva } 8673ee47cadSRui Miguel Silva 868913c55a8SChristophe JAILLET static int ov2680_parse_dt(struct ov2680_dev *sensor) 8693ee47cadSRui Miguel Silva { 8703ee47cadSRui Miguel Silva struct device *dev = ov2680_to_dev(sensor); 8713ee47cadSRui Miguel Silva int ret; 8723ee47cadSRui Miguel Silva 8733ee47cadSRui Miguel Silva sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 8743ee47cadSRui Miguel Silva GPIOD_OUT_HIGH); 8753ee47cadSRui Miguel Silva ret = PTR_ERR_OR_ZERO(sensor->reset_gpio); 8763ee47cadSRui Miguel Silva if (ret < 0) { 8773ee47cadSRui Miguel Silva dev_dbg(dev, "error while getting reset gpio: %d\n", ret); 8783ee47cadSRui Miguel Silva return ret; 8793ee47cadSRui Miguel Silva } 8803ee47cadSRui Miguel Silva 8813ee47cadSRui Miguel Silva sensor->xvclk = devm_clk_get(dev, "xvclk"); 8823ee47cadSRui Miguel Silva if (IS_ERR(sensor->xvclk)) { 8833ee47cadSRui Miguel Silva dev_err(dev, "xvclk clock missing or invalid\n"); 8843ee47cadSRui Miguel Silva return PTR_ERR(sensor->xvclk); 8853ee47cadSRui Miguel Silva } 8863ee47cadSRui Miguel Silva 8873ee47cadSRui Miguel Silva sensor->xvclk_freq = clk_get_rate(sensor->xvclk); 8883ee47cadSRui Miguel Silva if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) { 8893ee47cadSRui Miguel Silva dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n", 8903ee47cadSRui Miguel Silva sensor->xvclk_freq, OV2680_XVCLK_VALUE); 8913ee47cadSRui Miguel Silva return -EINVAL; 8923ee47cadSRui Miguel Silva } 8933ee47cadSRui Miguel Silva 8943ee47cadSRui Miguel Silva return 0; 8953ee47cadSRui Miguel Silva } 8963ee47cadSRui Miguel Silva 8973ee47cadSRui Miguel Silva static int ov2680_probe(struct i2c_client *client) 8983ee47cadSRui Miguel Silva { 8993ee47cadSRui Miguel Silva struct device *dev = &client->dev; 9003ee47cadSRui Miguel Silva struct ov2680_dev *sensor; 9013ee47cadSRui Miguel Silva int ret; 9023ee47cadSRui Miguel Silva 9033ee47cadSRui Miguel Silva sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 9043ee47cadSRui Miguel Silva if (!sensor) 9053ee47cadSRui Miguel Silva return -ENOMEM; 9063ee47cadSRui Miguel Silva 9073ee47cadSRui Miguel Silva sensor->i2c_client = client; 9083ee47cadSRui Miguel Silva 909913c55a8SChristophe JAILLET ret = ov2680_parse_dt(sensor); 9103ee47cadSRui Miguel Silva if (ret < 0) 9113ee47cadSRui Miguel Silva return -EINVAL; 9123ee47cadSRui Miguel Silva 9133ee47cadSRui Miguel Silva ret = ov2680_mode_init(sensor); 9143ee47cadSRui Miguel Silva if (ret < 0) 9153ee47cadSRui Miguel Silva return ret; 9163ee47cadSRui Miguel Silva 9173ee47cadSRui Miguel Silva ret = ov2680_get_regulators(sensor); 9183ee47cadSRui Miguel Silva if (ret < 0) { 9193ee47cadSRui Miguel Silva dev_err(dev, "failed to get regulators\n"); 9203ee47cadSRui Miguel Silva return ret; 9213ee47cadSRui Miguel Silva } 9223ee47cadSRui Miguel Silva 9233ee47cadSRui Miguel Silva mutex_init(&sensor->lock); 9243ee47cadSRui Miguel Silva 925b7a41762SJavier Martinez Canillas ret = ov2680_check_id(sensor); 9263ee47cadSRui Miguel Silva if (ret < 0) 9273ee47cadSRui Miguel Silva goto lock_destroy; 9283ee47cadSRui Miguel Silva 929e7f4861dSJavier Martinez Canillas ret = ov2680_v4l2_register(sensor); 9303ee47cadSRui Miguel Silva if (ret < 0) 931b7a41762SJavier Martinez Canillas goto lock_destroy; 9323ee47cadSRui Miguel Silva 9333ee47cadSRui Miguel Silva dev_info(dev, "ov2680 init correctly\n"); 9343ee47cadSRui Miguel Silva 9353ee47cadSRui Miguel Silva return 0; 9363ee47cadSRui Miguel Silva 9373ee47cadSRui Miguel Silva lock_destroy: 938b7a41762SJavier Martinez Canillas dev_err(dev, "ov2680 init fail: %d\n", ret); 9393ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9403ee47cadSRui Miguel Silva 9413ee47cadSRui Miguel Silva return ret; 9423ee47cadSRui Miguel Silva } 9433ee47cadSRui Miguel Silva 944ed5c2f5fSUwe Kleine-König static void ov2680_remove(struct i2c_client *client) 9453ee47cadSRui Miguel Silva { 9463ee47cadSRui Miguel Silva struct v4l2_subdev *sd = i2c_get_clientdata(client); 9473ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9483ee47cadSRui Miguel Silva 9493ee47cadSRui Miguel Silva v4l2_async_unregister_subdev(&sensor->sd); 9503ee47cadSRui Miguel Silva mutex_destroy(&sensor->lock); 9513ee47cadSRui Miguel Silva media_entity_cleanup(&sensor->sd.entity); 9523ee47cadSRui Miguel Silva v4l2_ctrl_handler_free(&sensor->ctrls.handler); 9533ee47cadSRui Miguel Silva } 9543ee47cadSRui Miguel Silva 9553ee47cadSRui Miguel Silva static int __maybe_unused ov2680_suspend(struct device *dev) 9563ee47cadSRui Miguel Silva { 9577519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9583ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9593ee47cadSRui Miguel Silva 9603ee47cadSRui Miguel Silva if (sensor->is_streaming) 9613ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 9623ee47cadSRui Miguel Silva 9633ee47cadSRui Miguel Silva return 0; 9643ee47cadSRui Miguel Silva } 9653ee47cadSRui Miguel Silva 9663ee47cadSRui Miguel Silva static int __maybe_unused ov2680_resume(struct device *dev) 9673ee47cadSRui Miguel Silva { 9687519296dSKrzysztof Kozlowski struct v4l2_subdev *sd = dev_get_drvdata(dev); 9693ee47cadSRui Miguel Silva struct ov2680_dev *sensor = to_ov2680_dev(sd); 9703ee47cadSRui Miguel Silva int ret; 9713ee47cadSRui Miguel Silva 9723ee47cadSRui Miguel Silva if (sensor->is_streaming) { 9733ee47cadSRui Miguel Silva ret = ov2680_stream_enable(sensor); 9743ee47cadSRui Miguel Silva if (ret < 0) 9753ee47cadSRui Miguel Silva goto stream_disable; 9763ee47cadSRui Miguel Silva } 9773ee47cadSRui Miguel Silva 9783ee47cadSRui Miguel Silva return 0; 9793ee47cadSRui Miguel Silva 9803ee47cadSRui Miguel Silva stream_disable: 9813ee47cadSRui Miguel Silva ov2680_stream_disable(sensor); 9823ee47cadSRui Miguel Silva sensor->is_streaming = false; 9833ee47cadSRui Miguel Silva 9843ee47cadSRui Miguel Silva return ret; 9853ee47cadSRui Miguel Silva } 9863ee47cadSRui Miguel Silva 9873ee47cadSRui Miguel Silva static const struct dev_pm_ops ov2680_pm_ops = { 9883ee47cadSRui Miguel Silva SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume) 9893ee47cadSRui Miguel Silva }; 9903ee47cadSRui Miguel Silva 9913ee47cadSRui Miguel Silva static const struct of_device_id ov2680_dt_ids[] = { 9923ee47cadSRui Miguel Silva { .compatible = "ovti,ov2680" }, 9933ee47cadSRui Miguel Silva { /* sentinel */ }, 9943ee47cadSRui Miguel Silva }; 9953ee47cadSRui Miguel Silva MODULE_DEVICE_TABLE(of, ov2680_dt_ids); 9963ee47cadSRui Miguel Silva 9973ee47cadSRui Miguel Silva static struct i2c_driver ov2680_i2c_driver = { 9983ee47cadSRui Miguel Silva .driver = { 9993ee47cadSRui Miguel Silva .name = "ov2680", 10003ee47cadSRui Miguel Silva .pm = &ov2680_pm_ops, 100114155c4fSKrzysztof Kozlowski .of_match_table = ov2680_dt_ids, 10023ee47cadSRui Miguel Silva }, 1003aaeb31c0SUwe Kleine-König .probe = ov2680_probe, 10043ee47cadSRui Miguel Silva .remove = ov2680_remove, 10053ee47cadSRui Miguel Silva }; 10063ee47cadSRui Miguel Silva module_i2c_driver(ov2680_i2c_driver); 10073ee47cadSRui Miguel Silva 10083ee47cadSRui Miguel Silva MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 10093ee47cadSRui Miguel Silva MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver"); 10103ee47cadSRui Miguel Silva MODULE_LICENSE("GPL v2"); 1011