1d30bb512STodor Tomov // SPDX-License-Identifier: GPL-2.0 2d30bb512STodor Tomov /* 3d30bb512STodor Tomov * Driver for the OV7251 camera sensor. 4d30bb512STodor Tomov * 5d30bb512STodor Tomov * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. 6d30bb512STodor Tomov * Copyright (c) 2017-2018, Linaro Ltd. 7d30bb512STodor Tomov */ 8d30bb512STodor Tomov 9d30bb512STodor Tomov #include <linux/bitops.h> 10d30bb512STodor Tomov #include <linux/clk.h> 11d30bb512STodor Tomov #include <linux/delay.h> 12d30bb512STodor Tomov #include <linux/device.h> 13d30bb512STodor Tomov #include <linux/gpio/consumer.h> 14d30bb512STodor Tomov #include <linux/i2c.h> 15d30bb512STodor Tomov #include <linux/init.h> 16d30bb512STodor Tomov #include <linux/module.h> 176766cff6SDaniel Scally #include <linux/mod_devicetable.h> 18*207f4162SDaniel Scally #include <linux/pm_runtime.h> 19d30bb512STodor Tomov #include <linux/regulator/consumer.h> 20d30bb512STodor Tomov #include <linux/slab.h> 21d30bb512STodor Tomov #include <linux/types.h> 22d30bb512STodor Tomov #include <media/v4l2-ctrls.h> 23d30bb512STodor Tomov #include <media/v4l2-fwnode.h> 24d30bb512STodor Tomov #include <media/v4l2-subdev.h> 25d30bb512STodor Tomov 26d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT 0x0100 27d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0 28d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_STREAMING 0x1 29d30bb512STodor Tomov 30d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH 0x300a 31d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH_BYTE 0x77 32d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW 0x300b 33d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW_BYTE 0x50 34d30bb512STodor Tomov #define OV7251_SC_GP_IO_IN1 0x3029 35d30bb512STodor Tomov #define OV7251_AEC_EXPO_0 0x3500 36d30bb512STodor Tomov #define OV7251_AEC_EXPO_1 0x3501 37d30bb512STodor Tomov #define OV7251_AEC_EXPO_2 0x3502 38d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_0 0x350a 39d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_1 0x350b 40d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1 0x3820 41d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1_VFLIP BIT(2) 42d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2 0x3821 43d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2_MIRROR BIT(2) 44d30bb512STodor Tomov #define OV7251_PRE_ISP_00 0x5e00 45d30bb512STodor Tomov #define OV7251_PRE_ISP_00_TEST_PATTERN BIT(7) 46df057b0dSDaniel Scally #define OV7251_PLL1_PRE_DIV_REG 0x30b4 47df057b0dSDaniel Scally #define OV7251_PLL1_MULT_REG 0x30b3 48df057b0dSDaniel Scally #define OV7251_PLL1_DIVIDER_REG 0x30b1 49df057b0dSDaniel Scally #define OV7251_PLL1_PIX_DIV_REG 0x30b0 50df057b0dSDaniel Scally #define OV7251_PLL1_MIPI_DIV_REG 0x30b5 51df057b0dSDaniel Scally #define OV7251_PLL2_PRE_DIV_REG 0x3098 52df057b0dSDaniel Scally #define OV7251_PLL2_MULT_REG 0x3099 53df057b0dSDaniel Scally #define OV7251_PLL2_DIVIDER_REG 0x309d 54df057b0dSDaniel Scally #define OV7251_PLL2_SYS_DIV_REG 0x309a 55df057b0dSDaniel Scally #define OV7251_PLL2_ADC_DIV_REG 0x309b 56d30bb512STodor Tomov 57d30bb512STodor Tomov struct reg_value { 58d30bb512STodor Tomov u16 reg; 59d30bb512STodor Tomov u8 val; 60d30bb512STodor Tomov }; 61d30bb512STodor Tomov 62d30bb512STodor Tomov struct ov7251_mode_info { 63d30bb512STodor Tomov u32 width; 64d30bb512STodor Tomov u32 height; 65d30bb512STodor Tomov const struct reg_value *data; 66d30bb512STodor Tomov u32 data_size; 67d30bb512STodor Tomov u32 pixel_clock; 68d30bb512STodor Tomov u32 link_freq; 69d30bb512STodor Tomov u16 exposure_max; 70d30bb512STodor Tomov u16 exposure_def; 71d30bb512STodor Tomov struct v4l2_fract timeperframe; 72d30bb512STodor Tomov }; 73d30bb512STodor Tomov 74df057b0dSDaniel Scally struct ov7251_pll1_cfg { 75df057b0dSDaniel Scally unsigned int pre_div; 76df057b0dSDaniel Scally unsigned int mult; 77df057b0dSDaniel Scally unsigned int div; 78df057b0dSDaniel Scally unsigned int pix_div; 79df057b0dSDaniel Scally unsigned int mipi_div; 80df057b0dSDaniel Scally }; 81df057b0dSDaniel Scally 82df057b0dSDaniel Scally struct ov7251_pll2_cfg { 83df057b0dSDaniel Scally unsigned int pre_div; 84df057b0dSDaniel Scally unsigned int mult; 85df057b0dSDaniel Scally unsigned int div; 86df057b0dSDaniel Scally unsigned int sys_div; 87df057b0dSDaniel Scally unsigned int adc_div; 88df057b0dSDaniel Scally }; 89df057b0dSDaniel Scally 90df057b0dSDaniel Scally /* 91df057b0dSDaniel Scally * Rubbish ordering, but only PLL1 needs to have a separate configuration per 92df057b0dSDaniel Scally * link frequency and the array member needs to be last. 93df057b0dSDaniel Scally */ 94df057b0dSDaniel Scally struct ov7251_pll_cfgs { 95df057b0dSDaniel Scally const struct ov7251_pll2_cfg *pll2; 96df057b0dSDaniel Scally const struct ov7251_pll1_cfg *pll1[]; 97df057b0dSDaniel Scally }; 98df057b0dSDaniel Scally 99df057b0dSDaniel Scally enum xclk_rate { 100ed9566ceSDaniel Scally OV7251_19_2_MHZ, 101df057b0dSDaniel Scally OV7251_24_MHZ, 102df057b0dSDaniel Scally OV7251_NUM_SUPPORTED_RATES 103df057b0dSDaniel Scally }; 104df057b0dSDaniel Scally 105cc125aaaSDaniel Scally enum supported_link_freqs { 106cc125aaaSDaniel Scally OV7251_LINK_FREQ_240_MHZ, 107ed9566ceSDaniel Scally OV7251_LINK_FREQ_319_2_MHZ, 108cc125aaaSDaniel Scally OV7251_NUM_SUPPORTED_LINK_FREQS 109cc125aaaSDaniel Scally }; 110cc125aaaSDaniel Scally 111d30bb512STodor Tomov struct ov7251 { 112d30bb512STodor Tomov struct i2c_client *i2c_client; 113d30bb512STodor Tomov struct device *dev; 114d30bb512STodor Tomov struct v4l2_subdev sd; 115d30bb512STodor Tomov struct media_pad pad; 116d30bb512STodor Tomov struct v4l2_fwnode_endpoint ep; 117d30bb512STodor Tomov struct v4l2_mbus_framefmt fmt; 118d30bb512STodor Tomov struct v4l2_rect crop; 119d30bb512STodor Tomov struct clk *xclk; 120d30bb512STodor Tomov u32 xclk_freq; 121d30bb512STodor Tomov 122d30bb512STodor Tomov struct regulator *io_regulator; 123d30bb512STodor Tomov struct regulator *core_regulator; 124d30bb512STodor Tomov struct regulator *analog_regulator; 125d30bb512STodor Tomov 126df057b0dSDaniel Scally const struct ov7251_pll_cfgs *pll_cfgs; 127cc125aaaSDaniel Scally enum supported_link_freqs link_freq_idx; 128d30bb512STodor Tomov const struct ov7251_mode_info *current_mode; 129d30bb512STodor Tomov 130d30bb512STodor Tomov struct v4l2_ctrl_handler ctrls; 131d30bb512STodor Tomov struct v4l2_ctrl *pixel_clock; 132d30bb512STodor Tomov struct v4l2_ctrl *link_freq; 133d30bb512STodor Tomov struct v4l2_ctrl *exposure; 134d30bb512STodor Tomov struct v4l2_ctrl *gain; 135d30bb512STodor Tomov 136d30bb512STodor Tomov /* Cached register values */ 137d30bb512STodor Tomov u8 aec_pk_manual; 138d30bb512STodor Tomov u8 pre_isp_00; 139d30bb512STodor Tomov u8 timing_format1; 140d30bb512STodor Tomov u8 timing_format2; 141d30bb512STodor Tomov 142d30bb512STodor Tomov struct mutex lock; /* lock to protect power state, ctrls and mode */ 143d30bb512STodor Tomov bool power_on; 144d30bb512STodor Tomov 145d30bb512STodor Tomov struct gpio_desc *enable_gpio; 146d30bb512STodor Tomov }; 147d30bb512STodor Tomov 148d30bb512STodor Tomov static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd) 149d30bb512STodor Tomov { 150d30bb512STodor Tomov return container_of(sd, struct ov7251, sd); 151d30bb512STodor Tomov } 152d30bb512STodor Tomov 153ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_240_mhz = { 154ed9566ceSDaniel Scally .pre_div = 0x03, 155ed9566ceSDaniel Scally .mult = 0x4b, 156ed9566ceSDaniel Scally .div = 0x01, 157ed9566ceSDaniel Scally .pix_div = 0x0a, 158ed9566ceSDaniel Scally .mipi_div = 0x05, 159ed9566ceSDaniel Scally }; 160ed9566ceSDaniel Scally 161ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_319_2_mhz = { 162ed9566ceSDaniel Scally .pre_div = 0x01, 163ed9566ceSDaniel Scally .mult = 0x85, 164ed9566ceSDaniel Scally .div = 0x04, 165ed9566ceSDaniel Scally .pix_div = 0x0a, 166ed9566ceSDaniel Scally .mipi_div = 0x05, 167ed9566ceSDaniel Scally }; 168ed9566ceSDaniel Scally 169df057b0dSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_240_mhz = { 170df057b0dSDaniel Scally .pre_div = 0x03, 171df057b0dSDaniel Scally .mult = 0x64, 172df057b0dSDaniel Scally .div = 0x01, 173df057b0dSDaniel Scally .pix_div = 0x0a, 174df057b0dSDaniel Scally .mipi_div = 0x05, 175df057b0dSDaniel Scally }; 176df057b0dSDaniel Scally 177ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_319_2_mhz = { 178ed9566ceSDaniel Scally .pre_div = 0x05, 179ed9566ceSDaniel Scally .mult = 0x85, 180ed9566ceSDaniel Scally .div = 0x02, 181ed9566ceSDaniel Scally .pix_div = 0x0a, 182ed9566ceSDaniel Scally .mipi_div = 0x05, 183ed9566ceSDaniel Scally }; 184ed9566ceSDaniel Scally 185ed9566ceSDaniel Scally static const struct ov7251_pll2_cfg ov7251_pll2_cfg_19_2_mhz = { 186ed9566ceSDaniel Scally .pre_div = 0x04, 187ed9566ceSDaniel Scally .mult = 0x32, 188ed9566ceSDaniel Scally .div = 0x00, 189ed9566ceSDaniel Scally .sys_div = 0x05, 190ed9566ceSDaniel Scally .adc_div = 0x04, 191ed9566ceSDaniel Scally }; 192ed9566ceSDaniel Scally 193df057b0dSDaniel Scally static const struct ov7251_pll2_cfg ov7251_pll2_cfg_24_mhz = { 194df057b0dSDaniel Scally .pre_div = 0x04, 195df057b0dSDaniel Scally .mult = 0x28, 196df057b0dSDaniel Scally .div = 0x00, 197df057b0dSDaniel Scally .sys_div = 0x05, 198df057b0dSDaniel Scally .adc_div = 0x04, 199df057b0dSDaniel Scally }; 200df057b0dSDaniel Scally 201ed9566ceSDaniel Scally static const struct ov7251_pll_cfgs ov7251_pll_cfgs_19_2_mhz = { 202ed9566ceSDaniel Scally .pll2 = &ov7251_pll2_cfg_19_2_mhz, 203ed9566ceSDaniel Scally .pll1 = { 204ed9566ceSDaniel Scally [OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_19_2_mhz_240_mhz, 205ed9566ceSDaniel Scally [OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_19_2_mhz_319_2_mhz, 206ed9566ceSDaniel Scally }, 207ed9566ceSDaniel Scally }; 208ed9566ceSDaniel Scally 209df057b0dSDaniel Scally static const struct ov7251_pll_cfgs ov7251_pll_cfgs_24_mhz = { 210df057b0dSDaniel Scally .pll2 = &ov7251_pll2_cfg_24_mhz, 211df057b0dSDaniel Scally .pll1 = { 212df057b0dSDaniel Scally [OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_24_mhz_240_mhz, 213ed9566ceSDaniel Scally [OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_24_mhz_319_2_mhz, 214df057b0dSDaniel Scally }, 215df057b0dSDaniel Scally }; 216df057b0dSDaniel Scally 217df057b0dSDaniel Scally static const struct ov7251_pll_cfgs *ov7251_pll_cfgs[] = { 218ed9566ceSDaniel Scally [OV7251_19_2_MHZ] = &ov7251_pll_cfgs_19_2_mhz, 219df057b0dSDaniel Scally [OV7251_24_MHZ] = &ov7251_pll_cfgs_24_mhz, 220df057b0dSDaniel Scally }; 221df057b0dSDaniel Scally 222d30bb512STodor Tomov static const struct reg_value ov7251_global_init_setting[] = { 223d30bb512STodor Tomov { 0x0103, 0x01 }, 224d30bb512STodor Tomov { 0x303b, 0x02 }, 225d30bb512STodor Tomov }; 226d30bb512STodor Tomov 227d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_30fps[] = { 228d30bb512STodor Tomov { 0x3005, 0x00 }, 229d30bb512STodor Tomov { 0x3012, 0xc0 }, 230d30bb512STodor Tomov { 0x3013, 0xd2 }, 231d30bb512STodor Tomov { 0x3014, 0x04 }, 232d30bb512STodor Tomov { 0x3016, 0xf0 }, 233d30bb512STodor Tomov { 0x3017, 0xf0 }, 234d30bb512STodor Tomov { 0x3018, 0xf0 }, 235d30bb512STodor Tomov { 0x301a, 0xf0 }, 236d30bb512STodor Tomov { 0x301b, 0xf0 }, 237d30bb512STodor Tomov { 0x301c, 0xf0 }, 238d30bb512STodor Tomov { 0x3023, 0x05 }, 239d30bb512STodor Tomov { 0x3037, 0xf0 }, 240d30bb512STodor Tomov { 0x3106, 0xda }, 241d30bb512STodor Tomov { 0x3503, 0x07 }, 242d30bb512STodor Tomov { 0x3509, 0x10 }, 243d30bb512STodor Tomov { 0x3600, 0x1c }, 244d30bb512STodor Tomov { 0x3602, 0x62 }, 245d30bb512STodor Tomov { 0x3620, 0xb7 }, 246d30bb512STodor Tomov { 0x3622, 0x04 }, 247d30bb512STodor Tomov { 0x3626, 0x21 }, 248d30bb512STodor Tomov { 0x3627, 0x30 }, 249d30bb512STodor Tomov { 0x3630, 0x44 }, 250d30bb512STodor Tomov { 0x3631, 0x35 }, 251d30bb512STodor Tomov { 0x3634, 0x60 }, 252d30bb512STodor Tomov { 0x3636, 0x00 }, 253d30bb512STodor Tomov { 0x3662, 0x01 }, 254d30bb512STodor Tomov { 0x3663, 0x70 }, 255d30bb512STodor Tomov { 0x3664, 0x50 }, 256d30bb512STodor Tomov { 0x3666, 0x0a }, 257d30bb512STodor Tomov { 0x3669, 0x1a }, 258d30bb512STodor Tomov { 0x366a, 0x00 }, 259d30bb512STodor Tomov { 0x366b, 0x50 }, 260d30bb512STodor Tomov { 0x3673, 0x01 }, 261d30bb512STodor Tomov { 0x3674, 0xff }, 262d30bb512STodor Tomov { 0x3675, 0x03 }, 263d30bb512STodor Tomov { 0x3705, 0xc1 }, 264d30bb512STodor Tomov { 0x3709, 0x40 }, 265d30bb512STodor Tomov { 0x373c, 0x08 }, 266d30bb512STodor Tomov { 0x3742, 0x00 }, 267d30bb512STodor Tomov { 0x3757, 0xb3 }, 268d30bb512STodor Tomov { 0x3788, 0x00 }, 269d30bb512STodor Tomov { 0x37a8, 0x01 }, 270d30bb512STodor Tomov { 0x37a9, 0xc0 }, 271d30bb512STodor Tomov { 0x3800, 0x00 }, 272d30bb512STodor Tomov { 0x3801, 0x04 }, 273d30bb512STodor Tomov { 0x3802, 0x00 }, 274d30bb512STodor Tomov { 0x3803, 0x04 }, 275d30bb512STodor Tomov { 0x3804, 0x02 }, 276d30bb512STodor Tomov { 0x3805, 0x8b }, 277d30bb512STodor Tomov { 0x3806, 0x01 }, 278d30bb512STodor Tomov { 0x3807, 0xeb }, 279d30bb512STodor Tomov { 0x3808, 0x02 }, /* width high */ 280d30bb512STodor Tomov { 0x3809, 0x80 }, /* width low */ 281d30bb512STodor Tomov { 0x380a, 0x01 }, /* height high */ 282d30bb512STodor Tomov { 0x380b, 0xe0 }, /* height low */ 283d30bb512STodor Tomov { 0x380c, 0x03 }, /* total horiz timing high */ 284d30bb512STodor Tomov { 0x380d, 0xa0 }, /* total horiz timing low */ 285d30bb512STodor Tomov { 0x380e, 0x06 }, /* total vertical timing high */ 286d30bb512STodor Tomov { 0x380f, 0xbc }, /* total vertical timing low */ 287d30bb512STodor Tomov { 0x3810, 0x00 }, 288d30bb512STodor Tomov { 0x3811, 0x04 }, 289d30bb512STodor Tomov { 0x3812, 0x00 }, 290d30bb512STodor Tomov { 0x3813, 0x05 }, 291d30bb512STodor Tomov { 0x3814, 0x11 }, 292d30bb512STodor Tomov { 0x3815, 0x11 }, 293d30bb512STodor Tomov { 0x3820, 0x40 }, 294d30bb512STodor Tomov { 0x3821, 0x00 }, 295d30bb512STodor Tomov { 0x382f, 0x0e }, 296d30bb512STodor Tomov { 0x3832, 0x00 }, 297d30bb512STodor Tomov { 0x3833, 0x05 }, 298d30bb512STodor Tomov { 0x3834, 0x00 }, 299d30bb512STodor Tomov { 0x3835, 0x0c }, 300d30bb512STodor Tomov { 0x3837, 0x00 }, 301d30bb512STodor Tomov { 0x3b80, 0x00 }, 302d30bb512STodor Tomov { 0x3b81, 0xa5 }, 303d30bb512STodor Tomov { 0x3b82, 0x10 }, 304d30bb512STodor Tomov { 0x3b83, 0x00 }, 305d30bb512STodor Tomov { 0x3b84, 0x08 }, 306d30bb512STodor Tomov { 0x3b85, 0x00 }, 307d30bb512STodor Tomov { 0x3b86, 0x01 }, 308d30bb512STodor Tomov { 0x3b87, 0x00 }, 309d30bb512STodor Tomov { 0x3b88, 0x00 }, 310d30bb512STodor Tomov { 0x3b89, 0x00 }, 311d30bb512STodor Tomov { 0x3b8a, 0x00 }, 312d30bb512STodor Tomov { 0x3b8b, 0x05 }, 313d30bb512STodor Tomov { 0x3b8c, 0x00 }, 314d30bb512STodor Tomov { 0x3b8d, 0x00 }, 315d30bb512STodor Tomov { 0x3b8e, 0x00 }, 316d30bb512STodor Tomov { 0x3b8f, 0x1a }, 317d30bb512STodor Tomov { 0x3b94, 0x05 }, 318d30bb512STodor Tomov { 0x3b95, 0xf2 }, 319d30bb512STodor Tomov { 0x3b96, 0x40 }, 320d30bb512STodor Tomov { 0x3c00, 0x89 }, 321d30bb512STodor Tomov { 0x3c01, 0x63 }, 322d30bb512STodor Tomov { 0x3c02, 0x01 }, 323d30bb512STodor Tomov { 0x3c03, 0x00 }, 324d30bb512STodor Tomov { 0x3c04, 0x00 }, 325d30bb512STodor Tomov { 0x3c05, 0x03 }, 326d30bb512STodor Tomov { 0x3c06, 0x00 }, 327d30bb512STodor Tomov { 0x3c07, 0x06 }, 328d30bb512STodor Tomov { 0x3c0c, 0x01 }, 329d30bb512STodor Tomov { 0x3c0d, 0xd0 }, 330d30bb512STodor Tomov { 0x3c0e, 0x02 }, 331d30bb512STodor Tomov { 0x3c0f, 0x0a }, 332d30bb512STodor Tomov { 0x4001, 0x42 }, 333d30bb512STodor Tomov { 0x4004, 0x04 }, 334d30bb512STodor Tomov { 0x4005, 0x00 }, 335d30bb512STodor Tomov { 0x404e, 0x01 }, 336d30bb512STodor Tomov { 0x4300, 0xff }, 337d30bb512STodor Tomov { 0x4301, 0x00 }, 338d30bb512STodor Tomov { 0x4315, 0x00 }, 339d30bb512STodor Tomov { 0x4501, 0x48 }, 340d30bb512STodor Tomov { 0x4600, 0x00 }, 341d30bb512STodor Tomov { 0x4601, 0x4e }, 342d30bb512STodor Tomov { 0x4801, 0x0f }, 343d30bb512STodor Tomov { 0x4806, 0x0f }, 344d30bb512STodor Tomov { 0x4819, 0xaa }, 345d30bb512STodor Tomov { 0x4823, 0x3e }, 346d30bb512STodor Tomov { 0x4837, 0x19 }, 347d30bb512STodor Tomov { 0x4a0d, 0x00 }, 348d30bb512STodor Tomov { 0x4a47, 0x7f }, 349d30bb512STodor Tomov { 0x4a49, 0xf0 }, 350d30bb512STodor Tomov { 0x4a4b, 0x30 }, 351d30bb512STodor Tomov { 0x5000, 0x85 }, 352d30bb512STodor Tomov { 0x5001, 0x80 }, 353d30bb512STodor Tomov }; 354d30bb512STodor Tomov 355d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_60fps[] = { 356d30bb512STodor Tomov { 0x3005, 0x00 }, 357d30bb512STodor Tomov { 0x3012, 0xc0 }, 358d30bb512STodor Tomov { 0x3013, 0xd2 }, 359d30bb512STodor Tomov { 0x3014, 0x04 }, 360d30bb512STodor Tomov { 0x3016, 0x10 }, 361d30bb512STodor Tomov { 0x3017, 0x00 }, 362d30bb512STodor Tomov { 0x3018, 0x00 }, 363d30bb512STodor Tomov { 0x301a, 0x00 }, 364d30bb512STodor Tomov { 0x301b, 0x00 }, 365d30bb512STodor Tomov { 0x301c, 0x00 }, 366d30bb512STodor Tomov { 0x3023, 0x05 }, 367d30bb512STodor Tomov { 0x3037, 0xf0 }, 368d30bb512STodor Tomov { 0x3106, 0xda }, 369d30bb512STodor Tomov { 0x3503, 0x07 }, 370d30bb512STodor Tomov { 0x3509, 0x10 }, 371d30bb512STodor Tomov { 0x3600, 0x1c }, 372d30bb512STodor Tomov { 0x3602, 0x62 }, 373d30bb512STodor Tomov { 0x3620, 0xb7 }, 374d30bb512STodor Tomov { 0x3622, 0x04 }, 375d30bb512STodor Tomov { 0x3626, 0x21 }, 376d30bb512STodor Tomov { 0x3627, 0x30 }, 377d30bb512STodor Tomov { 0x3630, 0x44 }, 378d30bb512STodor Tomov { 0x3631, 0x35 }, 379d30bb512STodor Tomov { 0x3634, 0x60 }, 380d30bb512STodor Tomov { 0x3636, 0x00 }, 381d30bb512STodor Tomov { 0x3662, 0x01 }, 382d30bb512STodor Tomov { 0x3663, 0x70 }, 383d30bb512STodor Tomov { 0x3664, 0x50 }, 384d30bb512STodor Tomov { 0x3666, 0x0a }, 385d30bb512STodor Tomov { 0x3669, 0x1a }, 386d30bb512STodor Tomov { 0x366a, 0x00 }, 387d30bb512STodor Tomov { 0x366b, 0x50 }, 388d30bb512STodor Tomov { 0x3673, 0x01 }, 389d30bb512STodor Tomov { 0x3674, 0xff }, 390d30bb512STodor Tomov { 0x3675, 0x03 }, 391d30bb512STodor Tomov { 0x3705, 0xc1 }, 392d30bb512STodor Tomov { 0x3709, 0x40 }, 393d30bb512STodor Tomov { 0x373c, 0x08 }, 394d30bb512STodor Tomov { 0x3742, 0x00 }, 395d30bb512STodor Tomov { 0x3757, 0xb3 }, 396d30bb512STodor Tomov { 0x3788, 0x00 }, 397d30bb512STodor Tomov { 0x37a8, 0x01 }, 398d30bb512STodor Tomov { 0x37a9, 0xc0 }, 399d30bb512STodor Tomov { 0x3800, 0x00 }, 400d30bb512STodor Tomov { 0x3801, 0x04 }, 401d30bb512STodor Tomov { 0x3802, 0x00 }, 402d30bb512STodor Tomov { 0x3803, 0x04 }, 403d30bb512STodor Tomov { 0x3804, 0x02 }, 404d30bb512STodor Tomov { 0x3805, 0x8b }, 405d30bb512STodor Tomov { 0x3806, 0x01 }, 406d30bb512STodor Tomov { 0x3807, 0xeb }, 407d30bb512STodor Tomov { 0x3808, 0x02 }, /* width high */ 408d30bb512STodor Tomov { 0x3809, 0x80 }, /* width low */ 409d30bb512STodor Tomov { 0x380a, 0x01 }, /* height high */ 410d30bb512STodor Tomov { 0x380b, 0xe0 }, /* height low */ 411d30bb512STodor Tomov { 0x380c, 0x03 }, /* total horiz timing high */ 412d30bb512STodor Tomov { 0x380d, 0xa0 }, /* total horiz timing low */ 413d30bb512STodor Tomov { 0x380e, 0x03 }, /* total vertical timing high */ 414d30bb512STodor Tomov { 0x380f, 0x5c }, /* total vertical timing low */ 415d30bb512STodor Tomov { 0x3810, 0x00 }, 416d30bb512STodor Tomov { 0x3811, 0x04 }, 417d30bb512STodor Tomov { 0x3812, 0x00 }, 418d30bb512STodor Tomov { 0x3813, 0x05 }, 419d30bb512STodor Tomov { 0x3814, 0x11 }, 420d30bb512STodor Tomov { 0x3815, 0x11 }, 421d30bb512STodor Tomov { 0x3820, 0x40 }, 422d30bb512STodor Tomov { 0x3821, 0x00 }, 423d30bb512STodor Tomov { 0x382f, 0x0e }, 424d30bb512STodor Tomov { 0x3832, 0x00 }, 425d30bb512STodor Tomov { 0x3833, 0x05 }, 426d30bb512STodor Tomov { 0x3834, 0x00 }, 427d30bb512STodor Tomov { 0x3835, 0x0c }, 428d30bb512STodor Tomov { 0x3837, 0x00 }, 429d30bb512STodor Tomov { 0x3b80, 0x00 }, 430d30bb512STodor Tomov { 0x3b81, 0xa5 }, 431d30bb512STodor Tomov { 0x3b82, 0x10 }, 432d30bb512STodor Tomov { 0x3b83, 0x00 }, 433d30bb512STodor Tomov { 0x3b84, 0x08 }, 434d30bb512STodor Tomov { 0x3b85, 0x00 }, 435d30bb512STodor Tomov { 0x3b86, 0x01 }, 436d30bb512STodor Tomov { 0x3b87, 0x00 }, 437d30bb512STodor Tomov { 0x3b88, 0x00 }, 438d30bb512STodor Tomov { 0x3b89, 0x00 }, 439d30bb512STodor Tomov { 0x3b8a, 0x00 }, 440d30bb512STodor Tomov { 0x3b8b, 0x05 }, 441d30bb512STodor Tomov { 0x3b8c, 0x00 }, 442d30bb512STodor Tomov { 0x3b8d, 0x00 }, 443d30bb512STodor Tomov { 0x3b8e, 0x00 }, 444d30bb512STodor Tomov { 0x3b8f, 0x1a }, 445d30bb512STodor Tomov { 0x3b94, 0x05 }, 446d30bb512STodor Tomov { 0x3b95, 0xf2 }, 447d30bb512STodor Tomov { 0x3b96, 0x40 }, 448d30bb512STodor Tomov { 0x3c00, 0x89 }, 449d30bb512STodor Tomov { 0x3c01, 0x63 }, 450d30bb512STodor Tomov { 0x3c02, 0x01 }, 451d30bb512STodor Tomov { 0x3c03, 0x00 }, 452d30bb512STodor Tomov { 0x3c04, 0x00 }, 453d30bb512STodor Tomov { 0x3c05, 0x03 }, 454d30bb512STodor Tomov { 0x3c06, 0x00 }, 455d30bb512STodor Tomov { 0x3c07, 0x06 }, 456d30bb512STodor Tomov { 0x3c0c, 0x01 }, 457d30bb512STodor Tomov { 0x3c0d, 0xd0 }, 458d30bb512STodor Tomov { 0x3c0e, 0x02 }, 459d30bb512STodor Tomov { 0x3c0f, 0x0a }, 460d30bb512STodor Tomov { 0x4001, 0x42 }, 461d30bb512STodor Tomov { 0x4004, 0x04 }, 462d30bb512STodor Tomov { 0x4005, 0x00 }, 463d30bb512STodor Tomov { 0x404e, 0x01 }, 464d30bb512STodor Tomov { 0x4300, 0xff }, 465d30bb512STodor Tomov { 0x4301, 0x00 }, 466d30bb512STodor Tomov { 0x4315, 0x00 }, 467d30bb512STodor Tomov { 0x4501, 0x48 }, 468d30bb512STodor Tomov { 0x4600, 0x00 }, 469d30bb512STodor Tomov { 0x4601, 0x4e }, 470d30bb512STodor Tomov { 0x4801, 0x0f }, 471d30bb512STodor Tomov { 0x4806, 0x0f }, 472d30bb512STodor Tomov { 0x4819, 0xaa }, 473d30bb512STodor Tomov { 0x4823, 0x3e }, 474d30bb512STodor Tomov { 0x4837, 0x19 }, 475d30bb512STodor Tomov { 0x4a0d, 0x00 }, 476d30bb512STodor Tomov { 0x4a47, 0x7f }, 477d30bb512STodor Tomov { 0x4a49, 0xf0 }, 478d30bb512STodor Tomov { 0x4a4b, 0x30 }, 479d30bb512STodor Tomov { 0x5000, 0x85 }, 480d30bb512STodor Tomov { 0x5001, 0x80 }, 481d30bb512STodor Tomov }; 482d30bb512STodor Tomov 483d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_90fps[] = { 484d30bb512STodor Tomov { 0x3005, 0x00 }, 485d30bb512STodor Tomov { 0x3012, 0xc0 }, 486d30bb512STodor Tomov { 0x3013, 0xd2 }, 487d30bb512STodor Tomov { 0x3014, 0x04 }, 488d30bb512STodor Tomov { 0x3016, 0x10 }, 489d30bb512STodor Tomov { 0x3017, 0x00 }, 490d30bb512STodor Tomov { 0x3018, 0x00 }, 491d30bb512STodor Tomov { 0x301a, 0x00 }, 492d30bb512STodor Tomov { 0x301b, 0x00 }, 493d30bb512STodor Tomov { 0x301c, 0x00 }, 494d30bb512STodor Tomov { 0x3023, 0x05 }, 495d30bb512STodor Tomov { 0x3037, 0xf0 }, 496d30bb512STodor Tomov { 0x3106, 0xda }, 497d30bb512STodor Tomov { 0x3503, 0x07 }, 498d30bb512STodor Tomov { 0x3509, 0x10 }, 499d30bb512STodor Tomov { 0x3600, 0x1c }, 500d30bb512STodor Tomov { 0x3602, 0x62 }, 501d30bb512STodor Tomov { 0x3620, 0xb7 }, 502d30bb512STodor Tomov { 0x3622, 0x04 }, 503d30bb512STodor Tomov { 0x3626, 0x21 }, 504d30bb512STodor Tomov { 0x3627, 0x30 }, 505d30bb512STodor Tomov { 0x3630, 0x44 }, 506d30bb512STodor Tomov { 0x3631, 0x35 }, 507d30bb512STodor Tomov { 0x3634, 0x60 }, 508d30bb512STodor Tomov { 0x3636, 0x00 }, 509d30bb512STodor Tomov { 0x3662, 0x01 }, 510d30bb512STodor Tomov { 0x3663, 0x70 }, 511d30bb512STodor Tomov { 0x3664, 0x50 }, 512d30bb512STodor Tomov { 0x3666, 0x0a }, 513d30bb512STodor Tomov { 0x3669, 0x1a }, 514d30bb512STodor Tomov { 0x366a, 0x00 }, 515d30bb512STodor Tomov { 0x366b, 0x50 }, 516d30bb512STodor Tomov { 0x3673, 0x01 }, 517d30bb512STodor Tomov { 0x3674, 0xff }, 518d30bb512STodor Tomov { 0x3675, 0x03 }, 519d30bb512STodor Tomov { 0x3705, 0xc1 }, 520d30bb512STodor Tomov { 0x3709, 0x40 }, 521d30bb512STodor Tomov { 0x373c, 0x08 }, 522d30bb512STodor Tomov { 0x3742, 0x00 }, 523d30bb512STodor Tomov { 0x3757, 0xb3 }, 524d30bb512STodor Tomov { 0x3788, 0x00 }, 525d30bb512STodor Tomov { 0x37a8, 0x01 }, 526d30bb512STodor Tomov { 0x37a9, 0xc0 }, 527d30bb512STodor Tomov { 0x3800, 0x00 }, 528d30bb512STodor Tomov { 0x3801, 0x04 }, 529d30bb512STodor Tomov { 0x3802, 0x00 }, 530d30bb512STodor Tomov { 0x3803, 0x04 }, 531d30bb512STodor Tomov { 0x3804, 0x02 }, 532d30bb512STodor Tomov { 0x3805, 0x8b }, 533d30bb512STodor Tomov { 0x3806, 0x01 }, 534d30bb512STodor Tomov { 0x3807, 0xeb }, 535d30bb512STodor Tomov { 0x3808, 0x02 }, /* width high */ 536d30bb512STodor Tomov { 0x3809, 0x80 }, /* width low */ 537d30bb512STodor Tomov { 0x380a, 0x01 }, /* height high */ 538d30bb512STodor Tomov { 0x380b, 0xe0 }, /* height low */ 539d30bb512STodor Tomov { 0x380c, 0x03 }, /* total horiz timing high */ 540d30bb512STodor Tomov { 0x380d, 0xa0 }, /* total horiz timing low */ 541d30bb512STodor Tomov { 0x380e, 0x02 }, /* total vertical timing high */ 542d30bb512STodor Tomov { 0x380f, 0x3c }, /* total vertical timing low */ 543d30bb512STodor Tomov { 0x3810, 0x00 }, 544d30bb512STodor Tomov { 0x3811, 0x04 }, 545d30bb512STodor Tomov { 0x3812, 0x00 }, 546d30bb512STodor Tomov { 0x3813, 0x05 }, 547d30bb512STodor Tomov { 0x3814, 0x11 }, 548d30bb512STodor Tomov { 0x3815, 0x11 }, 549d30bb512STodor Tomov { 0x3820, 0x40 }, 550d30bb512STodor Tomov { 0x3821, 0x00 }, 551d30bb512STodor Tomov { 0x382f, 0x0e }, 552d30bb512STodor Tomov { 0x3832, 0x00 }, 553d30bb512STodor Tomov { 0x3833, 0x05 }, 554d30bb512STodor Tomov { 0x3834, 0x00 }, 555d30bb512STodor Tomov { 0x3835, 0x0c }, 556d30bb512STodor Tomov { 0x3837, 0x00 }, 557d30bb512STodor Tomov { 0x3b80, 0x00 }, 558d30bb512STodor Tomov { 0x3b81, 0xa5 }, 559d30bb512STodor Tomov { 0x3b82, 0x10 }, 560d30bb512STodor Tomov { 0x3b83, 0x00 }, 561d30bb512STodor Tomov { 0x3b84, 0x08 }, 562d30bb512STodor Tomov { 0x3b85, 0x00 }, 563d30bb512STodor Tomov { 0x3b86, 0x01 }, 564d30bb512STodor Tomov { 0x3b87, 0x00 }, 565d30bb512STodor Tomov { 0x3b88, 0x00 }, 566d30bb512STodor Tomov { 0x3b89, 0x00 }, 567d30bb512STodor Tomov { 0x3b8a, 0x00 }, 568d30bb512STodor Tomov { 0x3b8b, 0x05 }, 569d30bb512STodor Tomov { 0x3b8c, 0x00 }, 570d30bb512STodor Tomov { 0x3b8d, 0x00 }, 571d30bb512STodor Tomov { 0x3b8e, 0x00 }, 572d30bb512STodor Tomov { 0x3b8f, 0x1a }, 573d30bb512STodor Tomov { 0x3b94, 0x05 }, 574d30bb512STodor Tomov { 0x3b95, 0xf2 }, 575d30bb512STodor Tomov { 0x3b96, 0x40 }, 576d30bb512STodor Tomov { 0x3c00, 0x89 }, 577d30bb512STodor Tomov { 0x3c01, 0x63 }, 578d30bb512STodor Tomov { 0x3c02, 0x01 }, 579d30bb512STodor Tomov { 0x3c03, 0x00 }, 580d30bb512STodor Tomov { 0x3c04, 0x00 }, 581d30bb512STodor Tomov { 0x3c05, 0x03 }, 582d30bb512STodor Tomov { 0x3c06, 0x00 }, 583d30bb512STodor Tomov { 0x3c07, 0x06 }, 584d30bb512STodor Tomov { 0x3c0c, 0x01 }, 585d30bb512STodor Tomov { 0x3c0d, 0xd0 }, 586d30bb512STodor Tomov { 0x3c0e, 0x02 }, 587d30bb512STodor Tomov { 0x3c0f, 0x0a }, 588d30bb512STodor Tomov { 0x4001, 0x42 }, 589d30bb512STodor Tomov { 0x4004, 0x04 }, 590d30bb512STodor Tomov { 0x4005, 0x00 }, 591d30bb512STodor Tomov { 0x404e, 0x01 }, 592d30bb512STodor Tomov { 0x4300, 0xff }, 593d30bb512STodor Tomov { 0x4301, 0x00 }, 594d30bb512STodor Tomov { 0x4315, 0x00 }, 595d30bb512STodor Tomov { 0x4501, 0x48 }, 596d30bb512STodor Tomov { 0x4600, 0x00 }, 597d30bb512STodor Tomov { 0x4601, 0x4e }, 598d30bb512STodor Tomov { 0x4801, 0x0f }, 599d30bb512STodor Tomov { 0x4806, 0x0f }, 600d30bb512STodor Tomov { 0x4819, 0xaa }, 601d30bb512STodor Tomov { 0x4823, 0x3e }, 602d30bb512STodor Tomov { 0x4837, 0x19 }, 603d30bb512STodor Tomov { 0x4a0d, 0x00 }, 604d30bb512STodor Tomov { 0x4a47, 0x7f }, 605d30bb512STodor Tomov { 0x4a49, 0xf0 }, 606d30bb512STodor Tomov { 0x4a4b, 0x30 }, 607d30bb512STodor Tomov { 0x5000, 0x85 }, 608d30bb512STodor Tomov { 0x5001, 0x80 }, 609d30bb512STodor Tomov }; 610d30bb512STodor Tomov 611df057b0dSDaniel Scally static const unsigned long supported_xclk_rates[] = { 612ed9566ceSDaniel Scally [OV7251_19_2_MHZ] = 19200000, 613df057b0dSDaniel Scally [OV7251_24_MHZ] = 24000000, 614df057b0dSDaniel Scally }; 615df057b0dSDaniel Scally 616d30bb512STodor Tomov static const s64 link_freq[] = { 6171757b44eSDaniel Scally [OV7251_LINK_FREQ_240_MHZ] = 240000000, 618ed9566ceSDaniel Scally [OV7251_LINK_FREQ_319_2_MHZ] = 319200000, 6191757b44eSDaniel Scally }; 6201757b44eSDaniel Scally 6211757b44eSDaniel Scally static const s64 pixel_rates[] = { 6221757b44eSDaniel Scally [OV7251_LINK_FREQ_240_MHZ] = 48000000, 623ed9566ceSDaniel Scally [OV7251_LINK_FREQ_319_2_MHZ] = 63840000, 624d30bb512STodor Tomov }; 625d30bb512STodor Tomov 626d30bb512STodor Tomov static const struct ov7251_mode_info ov7251_mode_info_data[] = { 627d30bb512STodor Tomov { 628d30bb512STodor Tomov .width = 640, 629d30bb512STodor Tomov .height = 480, 630d30bb512STodor Tomov .data = ov7251_setting_vga_30fps, 631d30bb512STodor Tomov .data_size = ARRAY_SIZE(ov7251_setting_vga_30fps), 632d30bb512STodor Tomov .exposure_max = 1704, 633d30bb512STodor Tomov .exposure_def = 504, 634d30bb512STodor Tomov .timeperframe = { 635d30bb512STodor Tomov .numerator = 100, 636d30bb512STodor Tomov .denominator = 3000 637d30bb512STodor Tomov } 638d30bb512STodor Tomov }, 639d30bb512STodor Tomov { 640d30bb512STodor Tomov .width = 640, 641d30bb512STodor Tomov .height = 480, 642d30bb512STodor Tomov .data = ov7251_setting_vga_60fps, 643d30bb512STodor Tomov .data_size = ARRAY_SIZE(ov7251_setting_vga_60fps), 644d30bb512STodor Tomov .exposure_max = 840, 645d30bb512STodor Tomov .exposure_def = 504, 646d30bb512STodor Tomov .timeperframe = { 647d30bb512STodor Tomov .numerator = 100, 648d30bb512STodor Tomov .denominator = 6014 649d30bb512STodor Tomov } 650d30bb512STodor Tomov }, 651d30bb512STodor Tomov { 652d30bb512STodor Tomov .width = 640, 653d30bb512STodor Tomov .height = 480, 654d30bb512STodor Tomov .data = ov7251_setting_vga_90fps, 655d30bb512STodor Tomov .data_size = ARRAY_SIZE(ov7251_setting_vga_90fps), 656d30bb512STodor Tomov .exposure_max = 552, 657d30bb512STodor Tomov .exposure_def = 504, 658d30bb512STodor Tomov .timeperframe = { 659d30bb512STodor Tomov .numerator = 100, 660d30bb512STodor Tomov .denominator = 9043 661d30bb512STodor Tomov } 662d30bb512STodor Tomov }, 663d30bb512STodor Tomov }; 664d30bb512STodor Tomov 665d30bb512STodor Tomov static int ov7251_regulators_enable(struct ov7251 *ov7251) 666d30bb512STodor Tomov { 667d30bb512STodor Tomov int ret; 668d30bb512STodor Tomov 669d30bb512STodor Tomov /* OV7251 power up sequence requires core regulator 670d30bb512STodor Tomov * to be enabled not earlier than io regulator 671d30bb512STodor Tomov */ 672d30bb512STodor Tomov 673d30bb512STodor Tomov ret = regulator_enable(ov7251->io_regulator); 674d30bb512STodor Tomov if (ret < 0) { 675d30bb512STodor Tomov dev_err(ov7251->dev, "set io voltage failed\n"); 676d30bb512STodor Tomov return ret; 677d30bb512STodor Tomov } 678d30bb512STodor Tomov 679d30bb512STodor Tomov ret = regulator_enable(ov7251->analog_regulator); 680d30bb512STodor Tomov if (ret) { 681d30bb512STodor Tomov dev_err(ov7251->dev, "set analog voltage failed\n"); 682d30bb512STodor Tomov goto err_disable_io; 683d30bb512STodor Tomov } 684d30bb512STodor Tomov 685d30bb512STodor Tomov ret = regulator_enable(ov7251->core_regulator); 686d30bb512STodor Tomov if (ret) { 687d30bb512STodor Tomov dev_err(ov7251->dev, "set core voltage failed\n"); 688d30bb512STodor Tomov goto err_disable_analog; 689d30bb512STodor Tomov } 690d30bb512STodor Tomov 691d30bb512STodor Tomov return 0; 692d30bb512STodor Tomov 693d30bb512STodor Tomov err_disable_analog: 694d30bb512STodor Tomov regulator_disable(ov7251->analog_regulator); 695d30bb512STodor Tomov 696d30bb512STodor Tomov err_disable_io: 697d30bb512STodor Tomov regulator_disable(ov7251->io_regulator); 698d30bb512STodor Tomov 699d30bb512STodor Tomov return ret; 700d30bb512STodor Tomov } 701d30bb512STodor Tomov 702d30bb512STodor Tomov static void ov7251_regulators_disable(struct ov7251 *ov7251) 703d30bb512STodor Tomov { 704d30bb512STodor Tomov int ret; 705d30bb512STodor Tomov 706d30bb512STodor Tomov ret = regulator_disable(ov7251->core_regulator); 707d30bb512STodor Tomov if (ret < 0) 708d30bb512STodor Tomov dev_err(ov7251->dev, "core regulator disable failed\n"); 709d30bb512STodor Tomov 710d30bb512STodor Tomov ret = regulator_disable(ov7251->analog_regulator); 711d30bb512STodor Tomov if (ret < 0) 712d30bb512STodor Tomov dev_err(ov7251->dev, "analog regulator disable failed\n"); 713d30bb512STodor Tomov 714d30bb512STodor Tomov ret = regulator_disable(ov7251->io_regulator); 715d30bb512STodor Tomov if (ret < 0) 716d30bb512STodor Tomov dev_err(ov7251->dev, "io regulator disable failed\n"); 717d30bb512STodor Tomov } 718d30bb512STodor Tomov 719d30bb512STodor Tomov static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val) 720d30bb512STodor Tomov { 721d30bb512STodor Tomov u8 regbuf[3]; 722d30bb512STodor Tomov int ret; 723d30bb512STodor Tomov 724d30bb512STodor Tomov regbuf[0] = reg >> 8; 725d30bb512STodor Tomov regbuf[1] = reg & 0xff; 726d30bb512STodor Tomov regbuf[2] = val; 727d30bb512STodor Tomov 728d30bb512STodor Tomov ret = i2c_master_send(ov7251->i2c_client, regbuf, 3); 729d30bb512STodor Tomov if (ret < 0) { 730d30bb512STodor Tomov dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n", 731d30bb512STodor Tomov __func__, ret, reg, val); 732d30bb512STodor Tomov return ret; 733d30bb512STodor Tomov } 734d30bb512STodor Tomov 735d30bb512STodor Tomov return 0; 736d30bb512STodor Tomov } 737d30bb512STodor Tomov 738d30bb512STodor Tomov static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val, 739d30bb512STodor Tomov u8 num) 740d30bb512STodor Tomov { 741d30bb512STodor Tomov u8 regbuf[5]; 742d30bb512STodor Tomov u8 nregbuf = sizeof(reg) + num * sizeof(*val); 743d30bb512STodor Tomov int ret = 0; 744d30bb512STodor Tomov 745d30bb512STodor Tomov if (nregbuf > sizeof(regbuf)) 746d30bb512STodor Tomov return -EINVAL; 747d30bb512STodor Tomov 748d30bb512STodor Tomov regbuf[0] = reg >> 8; 749d30bb512STodor Tomov regbuf[1] = reg & 0xff; 750d30bb512STodor Tomov 751d30bb512STodor Tomov memcpy(regbuf + 2, val, num); 752d30bb512STodor Tomov 753d30bb512STodor Tomov ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf); 754d30bb512STodor Tomov if (ret < 0) { 755d30bb512STodor Tomov dev_err(ov7251->dev, 756d30bb512STodor Tomov "%s: write seq regs error %d: first reg=%x\n", 757d30bb512STodor Tomov __func__, ret, reg); 758d30bb512STodor Tomov return ret; 759d30bb512STodor Tomov } 760d30bb512STodor Tomov 761d30bb512STodor Tomov return 0; 762d30bb512STodor Tomov } 763d30bb512STodor Tomov 764d30bb512STodor Tomov static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val) 765d30bb512STodor Tomov { 766d30bb512STodor Tomov u8 regbuf[2]; 767d30bb512STodor Tomov int ret; 768d30bb512STodor Tomov 769d30bb512STodor Tomov regbuf[0] = reg >> 8; 770d30bb512STodor Tomov regbuf[1] = reg & 0xff; 771d30bb512STodor Tomov 772d30bb512STodor Tomov ret = i2c_master_send(ov7251->i2c_client, regbuf, 2); 773d30bb512STodor Tomov if (ret < 0) { 774d30bb512STodor Tomov dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n", 775d30bb512STodor Tomov __func__, ret, reg); 776d30bb512STodor Tomov return ret; 777d30bb512STodor Tomov } 778d30bb512STodor Tomov 779d30bb512STodor Tomov ret = i2c_master_recv(ov7251->i2c_client, val, 1); 780d30bb512STodor Tomov if (ret < 0) { 781d30bb512STodor Tomov dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n", 782d30bb512STodor Tomov __func__, ret, reg); 783d30bb512STodor Tomov return ret; 784d30bb512STodor Tomov } 785d30bb512STodor Tomov 786d30bb512STodor Tomov return 0; 787d30bb512STodor Tomov } 788d30bb512STodor Tomov 789df057b0dSDaniel Scally static int ov7251_pll_configure(struct ov7251 *ov7251) 790df057b0dSDaniel Scally { 791df057b0dSDaniel Scally const struct ov7251_pll_cfgs *configs; 792df057b0dSDaniel Scally int ret; 793df057b0dSDaniel Scally 794df057b0dSDaniel Scally configs = ov7251->pll_cfgs; 795df057b0dSDaniel Scally 796df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL1_PRE_DIV_REG, 797df057b0dSDaniel Scally configs->pll1[ov7251->link_freq_idx]->pre_div); 798df057b0dSDaniel Scally if (ret < 0) 799df057b0dSDaniel Scally return ret; 800df057b0dSDaniel Scally 801df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL1_MULT_REG, 802df057b0dSDaniel Scally configs->pll1[ov7251->link_freq_idx]->mult); 803df057b0dSDaniel Scally if (ret < 0) 804df057b0dSDaniel Scally return ret; 805df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL1_DIVIDER_REG, 806df057b0dSDaniel Scally configs->pll1[ov7251->link_freq_idx]->div); 807df057b0dSDaniel Scally if (ret < 0) 808df057b0dSDaniel Scally return ret; 809df057b0dSDaniel Scally 810df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL1_PIX_DIV_REG, 811df057b0dSDaniel Scally configs->pll1[ov7251->link_freq_idx]->pix_div); 812df057b0dSDaniel Scally if (ret < 0) 813df057b0dSDaniel Scally return ret; 814df057b0dSDaniel Scally 815df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL1_MIPI_DIV_REG, 816df057b0dSDaniel Scally configs->pll1[ov7251->link_freq_idx]->mipi_div); 817df057b0dSDaniel Scally if (ret < 0) 818df057b0dSDaniel Scally return ret; 819df057b0dSDaniel Scally 820df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL2_PRE_DIV_REG, 821df057b0dSDaniel Scally configs->pll2->pre_div); 822df057b0dSDaniel Scally if (ret < 0) 823df057b0dSDaniel Scally return ret; 824df057b0dSDaniel Scally 825df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL2_MULT_REG, 826df057b0dSDaniel Scally configs->pll2->mult); 827df057b0dSDaniel Scally if (ret < 0) 828df057b0dSDaniel Scally return ret; 829df057b0dSDaniel Scally 830df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL2_DIVIDER_REG, 831df057b0dSDaniel Scally configs->pll2->div); 832df057b0dSDaniel Scally if (ret < 0) 833df057b0dSDaniel Scally return ret; 834df057b0dSDaniel Scally 835df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL2_SYS_DIV_REG, 836df057b0dSDaniel Scally configs->pll2->sys_div); 837df057b0dSDaniel Scally if (ret < 0) 838df057b0dSDaniel Scally return ret; 839df057b0dSDaniel Scally 840df057b0dSDaniel Scally ret = ov7251_write_reg(ov7251, OV7251_PLL2_ADC_DIV_REG, 841df057b0dSDaniel Scally configs->pll2->adc_div); 842df057b0dSDaniel Scally 843df057b0dSDaniel Scally return ret; 844df057b0dSDaniel Scally } 845df057b0dSDaniel Scally 846d30bb512STodor Tomov static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure) 847d30bb512STodor Tomov { 848d30bb512STodor Tomov u16 reg; 849d30bb512STodor Tomov u8 val[3]; 850d30bb512STodor Tomov 851d30bb512STodor Tomov reg = OV7251_AEC_EXPO_0; 852d30bb512STodor Tomov val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */ 853d30bb512STodor Tomov val[1] = (exposure & 0x0ff0) >> 4; /* goes to OV7251_AEC_EXPO_1 */ 854d30bb512STodor Tomov val[2] = (exposure & 0x000f) << 4; /* goes to OV7251_AEC_EXPO_2 */ 855d30bb512STodor Tomov 856d30bb512STodor Tomov return ov7251_write_seq_regs(ov7251, reg, val, 3); 857d30bb512STodor Tomov } 858d30bb512STodor Tomov 859d30bb512STodor Tomov static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain) 860d30bb512STodor Tomov { 861d30bb512STodor Tomov u16 reg; 862d30bb512STodor Tomov u8 val[2]; 863d30bb512STodor Tomov 864d30bb512STodor Tomov reg = OV7251_AEC_AGC_ADJ_0; 865d30bb512STodor Tomov val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */ 866d30bb512STodor Tomov val[1] = gain & 0xff; /* goes to OV7251_AEC_AGC_ADJ_1 */ 867d30bb512STodor Tomov 868d30bb512STodor Tomov return ov7251_write_seq_regs(ov7251, reg, val, 2); 869d30bb512STodor Tomov } 870d30bb512STodor Tomov 871d30bb512STodor Tomov static int ov7251_set_register_array(struct ov7251 *ov7251, 872d30bb512STodor Tomov const struct reg_value *settings, 873d30bb512STodor Tomov unsigned int num_settings) 874d30bb512STodor Tomov { 875d30bb512STodor Tomov unsigned int i; 876d30bb512STodor Tomov int ret; 877d30bb512STodor Tomov 878d30bb512STodor Tomov for (i = 0; i < num_settings; ++i, ++settings) { 879d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, settings->reg, settings->val); 880d30bb512STodor Tomov if (ret < 0) 881d30bb512STodor Tomov return ret; 882d30bb512STodor Tomov } 883d30bb512STodor Tomov 884d30bb512STodor Tomov return 0; 885d30bb512STodor Tomov } 886d30bb512STodor Tomov 887*207f4162SDaniel Scally static int ov7251_set_power_on(struct device *dev) 888d30bb512STodor Tomov { 889*207f4162SDaniel Scally struct i2c_client *client = container_of(dev, struct i2c_client, dev); 890*207f4162SDaniel Scally struct v4l2_subdev *sd = i2c_get_clientdata(client); 891*207f4162SDaniel Scally struct ov7251 *ov7251 = to_ov7251(sd); 892d30bb512STodor Tomov int ret; 893d30bb512STodor Tomov u32 wait_us; 894d30bb512STodor Tomov 895d30bb512STodor Tomov ret = ov7251_regulators_enable(ov7251); 896d30bb512STodor Tomov if (ret < 0) 897d30bb512STodor Tomov return ret; 898d30bb512STodor Tomov 899d30bb512STodor Tomov ret = clk_prepare_enable(ov7251->xclk); 900d30bb512STodor Tomov if (ret < 0) { 901d30bb512STodor Tomov dev_err(ov7251->dev, "clk prepare enable failed\n"); 902d30bb512STodor Tomov ov7251_regulators_disable(ov7251); 903d30bb512STodor Tomov return ret; 904d30bb512STodor Tomov } 905d30bb512STodor Tomov 906d30bb512STodor Tomov gpiod_set_value_cansleep(ov7251->enable_gpio, 1); 907d30bb512STodor Tomov 908d30bb512STodor Tomov /* wait at least 65536 external clock cycles */ 909d30bb512STodor Tomov wait_us = DIV_ROUND_UP(65536 * 1000, 910d30bb512STodor Tomov DIV_ROUND_UP(ov7251->xclk_freq, 1000)); 911d30bb512STodor Tomov usleep_range(wait_us, wait_us + 1000); 912d30bb512STodor Tomov 913d30bb512STodor Tomov return 0; 914d30bb512STodor Tomov } 915d30bb512STodor Tomov 916*207f4162SDaniel Scally static int ov7251_set_power_off(struct device *dev) 917d30bb512STodor Tomov { 918*207f4162SDaniel Scally struct i2c_client *client = container_of(dev, struct i2c_client, dev); 919*207f4162SDaniel Scally struct v4l2_subdev *sd = i2c_get_clientdata(client); 920*207f4162SDaniel Scally struct ov7251 *ov7251 = to_ov7251(sd); 921*207f4162SDaniel Scally 922d30bb512STodor Tomov clk_disable_unprepare(ov7251->xclk); 923d30bb512STodor Tomov gpiod_set_value_cansleep(ov7251->enable_gpio, 0); 924d30bb512STodor Tomov ov7251_regulators_disable(ov7251); 925*207f4162SDaniel Scally 926*207f4162SDaniel Scally return 0; 927d30bb512STodor Tomov } 928d30bb512STodor Tomov 929d30bb512STodor Tomov static int ov7251_s_power(struct v4l2_subdev *sd, int on) 930d30bb512STodor Tomov { 931d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(sd); 932d30bb512STodor Tomov int ret = 0; 933d30bb512STodor Tomov 934d30bb512STodor Tomov mutex_lock(&ov7251->lock); 935d30bb512STodor Tomov 936d30bb512STodor Tomov /* If the power state is not modified - no work to do. */ 937d30bb512STodor Tomov if (ov7251->power_on == !!on) 938d30bb512STodor Tomov goto exit; 939d30bb512STodor Tomov 940d30bb512STodor Tomov if (on) { 941*207f4162SDaniel Scally ret = ov7251_set_power_on(ov7251->dev); 942d30bb512STodor Tomov if (ret < 0) 943d30bb512STodor Tomov goto exit; 944d30bb512STodor Tomov 945d30bb512STodor Tomov ret = ov7251_set_register_array(ov7251, 946d30bb512STodor Tomov ov7251_global_init_setting, 947d30bb512STodor Tomov ARRAY_SIZE(ov7251_global_init_setting)); 948d30bb512STodor Tomov if (ret < 0) { 949d30bb512STodor Tomov dev_err(ov7251->dev, "could not set init registers\n"); 950*207f4162SDaniel Scally ov7251_set_power_off(ov7251->dev); 951d30bb512STodor Tomov goto exit; 952d30bb512STodor Tomov } 953d30bb512STodor Tomov 954d30bb512STodor Tomov ov7251->power_on = true; 955d30bb512STodor Tomov } else { 956*207f4162SDaniel Scally ov7251_set_power_off(ov7251->dev); 957d30bb512STodor Tomov ov7251->power_on = false; 958d30bb512STodor Tomov } 959d30bb512STodor Tomov 960d30bb512STodor Tomov exit: 961d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 962d30bb512STodor Tomov 963d30bb512STodor Tomov return ret; 964d30bb512STodor Tomov } 965d30bb512STodor Tomov 966d30bb512STodor Tomov static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value) 967d30bb512STodor Tomov { 968d30bb512STodor Tomov u8 val = ov7251->timing_format2; 969d30bb512STodor Tomov int ret; 970d30bb512STodor Tomov 971d30bb512STodor Tomov if (value) 972d30bb512STodor Tomov val |= OV7251_TIMING_FORMAT2_MIRROR; 973d30bb512STodor Tomov else 974d30bb512STodor Tomov val &= ~OV7251_TIMING_FORMAT2_MIRROR; 975d30bb512STodor Tomov 976d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val); 977d30bb512STodor Tomov if (!ret) 978d30bb512STodor Tomov ov7251->timing_format2 = val; 979d30bb512STodor Tomov 980d30bb512STodor Tomov return ret; 981d30bb512STodor Tomov } 982d30bb512STodor Tomov 983d30bb512STodor Tomov static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value) 984d30bb512STodor Tomov { 985d30bb512STodor Tomov u8 val = ov7251->timing_format1; 986d30bb512STodor Tomov int ret; 987d30bb512STodor Tomov 988d30bb512STodor Tomov if (value) 989d30bb512STodor Tomov val |= OV7251_TIMING_FORMAT1_VFLIP; 990d30bb512STodor Tomov else 991d30bb512STodor Tomov val &= ~OV7251_TIMING_FORMAT1_VFLIP; 992d30bb512STodor Tomov 993d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val); 994d30bb512STodor Tomov if (!ret) 995d30bb512STodor Tomov ov7251->timing_format1 = val; 996d30bb512STodor Tomov 997d30bb512STodor Tomov return ret; 998d30bb512STodor Tomov } 999d30bb512STodor Tomov 1000d30bb512STodor Tomov static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value) 1001d30bb512STodor Tomov { 1002d30bb512STodor Tomov u8 val = ov7251->pre_isp_00; 1003d30bb512STodor Tomov int ret; 1004d30bb512STodor Tomov 1005d30bb512STodor Tomov if (value) 1006d30bb512STodor Tomov val |= OV7251_PRE_ISP_00_TEST_PATTERN; 1007d30bb512STodor Tomov else 1008d30bb512STodor Tomov val &= ~OV7251_PRE_ISP_00_TEST_PATTERN; 1009d30bb512STodor Tomov 1010d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val); 1011d30bb512STodor Tomov if (!ret) 1012d30bb512STodor Tomov ov7251->pre_isp_00 = val; 1013d30bb512STodor Tomov 1014d30bb512STodor Tomov return ret; 1015d30bb512STodor Tomov } 1016d30bb512STodor Tomov 1017d30bb512STodor Tomov static const char * const ov7251_test_pattern_menu[] = { 1018d30bb512STodor Tomov "Disabled", 1019d30bb512STodor Tomov "Vertical Pattern Bars", 1020d30bb512STodor Tomov }; 1021d30bb512STodor Tomov 1022d30bb512STodor Tomov static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl) 1023d30bb512STodor Tomov { 1024d30bb512STodor Tomov struct ov7251 *ov7251 = container_of(ctrl->handler, 1025d30bb512STodor Tomov struct ov7251, ctrls); 1026d30bb512STodor Tomov int ret; 1027d30bb512STodor Tomov 1028d30bb512STodor Tomov /* v4l2_ctrl_lock() locks our mutex */ 1029d30bb512STodor Tomov 1030*207f4162SDaniel Scally if (!pm_runtime_get_if_in_use(ov7251->dev)) 1031d30bb512STodor Tomov return 0; 1032d30bb512STodor Tomov 1033d30bb512STodor Tomov switch (ctrl->id) { 1034d30bb512STodor Tomov case V4L2_CID_EXPOSURE: 1035d30bb512STodor Tomov ret = ov7251_set_exposure(ov7251, ctrl->val); 1036d30bb512STodor Tomov break; 1037d30bb512STodor Tomov case V4L2_CID_GAIN: 1038d30bb512STodor Tomov ret = ov7251_set_gain(ov7251, ctrl->val); 1039d30bb512STodor Tomov break; 1040d30bb512STodor Tomov case V4L2_CID_TEST_PATTERN: 1041d30bb512STodor Tomov ret = ov7251_set_test_pattern(ov7251, ctrl->val); 1042d30bb512STodor Tomov break; 1043d30bb512STodor Tomov case V4L2_CID_HFLIP: 1044d30bb512STodor Tomov ret = ov7251_set_hflip(ov7251, ctrl->val); 1045d30bb512STodor Tomov break; 1046d30bb512STodor Tomov case V4L2_CID_VFLIP: 1047d30bb512STodor Tomov ret = ov7251_set_vflip(ov7251, ctrl->val); 1048d30bb512STodor Tomov break; 1049d30bb512STodor Tomov default: 1050d30bb512STodor Tomov ret = -EINVAL; 1051d30bb512STodor Tomov break; 1052d30bb512STodor Tomov } 1053d30bb512STodor Tomov 1054*207f4162SDaniel Scally pm_runtime_put(ov7251->dev); 1055*207f4162SDaniel Scally 1056d30bb512STodor Tomov return ret; 1057d30bb512STodor Tomov } 1058d30bb512STodor Tomov 1059d30bb512STodor Tomov static const struct v4l2_ctrl_ops ov7251_ctrl_ops = { 1060d30bb512STodor Tomov .s_ctrl = ov7251_s_ctrl, 1061d30bb512STodor Tomov }; 1062d30bb512STodor Tomov 1063d30bb512STodor Tomov static int ov7251_enum_mbus_code(struct v4l2_subdev *sd, 10640d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1065d30bb512STodor Tomov struct v4l2_subdev_mbus_code_enum *code) 1066d30bb512STodor Tomov { 1067d30bb512STodor Tomov if (code->index > 0) 1068d30bb512STodor Tomov return -EINVAL; 1069d30bb512STodor Tomov 1070d30bb512STodor Tomov code->code = MEDIA_BUS_FMT_Y10_1X10; 1071d30bb512STodor Tomov 1072d30bb512STodor Tomov return 0; 1073d30bb512STodor Tomov } 1074d30bb512STodor Tomov 1075d30bb512STodor Tomov static int ov7251_enum_frame_size(struct v4l2_subdev *subdev, 10760d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1077d30bb512STodor Tomov struct v4l2_subdev_frame_size_enum *fse) 1078d30bb512STodor Tomov { 1079d30bb512STodor Tomov if (fse->code != MEDIA_BUS_FMT_Y10_1X10) 1080d30bb512STodor Tomov return -EINVAL; 1081d30bb512STodor Tomov 1082d30bb512STodor Tomov if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data)) 1083d30bb512STodor Tomov return -EINVAL; 1084d30bb512STodor Tomov 1085d30bb512STodor Tomov fse->min_width = ov7251_mode_info_data[fse->index].width; 1086d30bb512STodor Tomov fse->max_width = ov7251_mode_info_data[fse->index].width; 1087d30bb512STodor Tomov fse->min_height = ov7251_mode_info_data[fse->index].height; 1088d30bb512STodor Tomov fse->max_height = ov7251_mode_info_data[fse->index].height; 1089d30bb512STodor Tomov 1090d30bb512STodor Tomov return 0; 1091d30bb512STodor Tomov } 1092d30bb512STodor Tomov 1093d30bb512STodor Tomov static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev, 10940d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1095d30bb512STodor Tomov struct v4l2_subdev_frame_interval_enum *fie) 1096d30bb512STodor Tomov { 1097d30bb512STodor Tomov unsigned int index = fie->index; 1098d30bb512STodor Tomov unsigned int i; 1099d30bb512STodor Tomov 1100d30bb512STodor Tomov for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) { 1101d30bb512STodor Tomov if (fie->width != ov7251_mode_info_data[i].width || 1102d30bb512STodor Tomov fie->height != ov7251_mode_info_data[i].height) 1103d30bb512STodor Tomov continue; 1104d30bb512STodor Tomov 1105d30bb512STodor Tomov if (index-- == 0) { 1106d30bb512STodor Tomov fie->interval = ov7251_mode_info_data[i].timeperframe; 1107d30bb512STodor Tomov return 0; 1108d30bb512STodor Tomov } 1109d30bb512STodor Tomov } 1110d30bb512STodor Tomov 1111d30bb512STodor Tomov return -EINVAL; 1112d30bb512STodor Tomov } 1113d30bb512STodor Tomov 1114d30bb512STodor Tomov static struct v4l2_mbus_framefmt * 1115d30bb512STodor Tomov __ov7251_get_pad_format(struct ov7251 *ov7251, 11160d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1117d30bb512STodor Tomov unsigned int pad, 1118d30bb512STodor Tomov enum v4l2_subdev_format_whence which) 1119d30bb512STodor Tomov { 1120d30bb512STodor Tomov switch (which) { 1121d30bb512STodor Tomov case V4L2_SUBDEV_FORMAT_TRY: 11220d346d2aSTomi Valkeinen return v4l2_subdev_get_try_format(&ov7251->sd, sd_state, pad); 1123d30bb512STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE: 1124d30bb512STodor Tomov return &ov7251->fmt; 1125d30bb512STodor Tomov default: 1126d30bb512STodor Tomov return NULL; 1127d30bb512STodor Tomov } 1128d30bb512STodor Tomov } 1129d30bb512STodor Tomov 1130d30bb512STodor Tomov static int ov7251_get_format(struct v4l2_subdev *sd, 11310d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1132d30bb512STodor Tomov struct v4l2_subdev_format *format) 1133d30bb512STodor Tomov { 1134d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(sd); 1135d30bb512STodor Tomov 1136d30bb512STodor Tomov mutex_lock(&ov7251->lock); 11370d346d2aSTomi Valkeinen format->format = *__ov7251_get_pad_format(ov7251, sd_state, 11380d346d2aSTomi Valkeinen format->pad, 1139d30bb512STodor Tomov format->which); 1140d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1141d30bb512STodor Tomov 1142d30bb512STodor Tomov return 0; 1143d30bb512STodor Tomov } 1144d30bb512STodor Tomov 1145d30bb512STodor Tomov static struct v4l2_rect * 11460d346d2aSTomi Valkeinen __ov7251_get_pad_crop(struct ov7251 *ov7251, 11470d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1148d30bb512STodor Tomov unsigned int pad, enum v4l2_subdev_format_whence which) 1149d30bb512STodor Tomov { 1150d30bb512STodor Tomov switch (which) { 1151d30bb512STodor Tomov case V4L2_SUBDEV_FORMAT_TRY: 11520d346d2aSTomi Valkeinen return v4l2_subdev_get_try_crop(&ov7251->sd, sd_state, pad); 1153d30bb512STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE: 1154d30bb512STodor Tomov return &ov7251->crop; 1155d30bb512STodor Tomov default: 1156d30bb512STodor Tomov return NULL; 1157d30bb512STodor Tomov } 1158d30bb512STodor Tomov } 1159d30bb512STodor Tomov 1160d30bb512STodor Tomov static inline u32 avg_fps(const struct v4l2_fract *t) 1161d30bb512STodor Tomov { 1162d30bb512STodor Tomov return (t->denominator + (t->numerator >> 1)) / t->numerator; 1163d30bb512STodor Tomov } 1164d30bb512STodor Tomov 1165d30bb512STodor Tomov static const struct ov7251_mode_info * 1166d30bb512STodor Tomov ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe) 1167d30bb512STodor Tomov { 1168d30bb512STodor Tomov const struct ov7251_mode_info *mode = ov7251->current_mode; 1169d30bb512STodor Tomov unsigned int fps_req = avg_fps(timeperframe); 1170d30bb512STodor Tomov unsigned int max_dist_match = (unsigned int) -1; 1171d30bb512STodor Tomov unsigned int i, n = 0; 1172d30bb512STodor Tomov 1173d30bb512STodor Tomov for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) { 1174d30bb512STodor Tomov unsigned int dist; 1175d30bb512STodor Tomov unsigned int fps_tmp; 1176d30bb512STodor Tomov 1177d30bb512STodor Tomov if (mode->width != ov7251_mode_info_data[i].width || 1178d30bb512STodor Tomov mode->height != ov7251_mode_info_data[i].height) 1179d30bb512STodor Tomov continue; 1180d30bb512STodor Tomov 1181d30bb512STodor Tomov fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe); 1182d30bb512STodor Tomov 1183d30bb512STodor Tomov dist = abs(fps_req - fps_tmp); 1184d30bb512STodor Tomov 1185d30bb512STodor Tomov if (dist < max_dist_match) { 1186d30bb512STodor Tomov n = i; 1187d30bb512STodor Tomov max_dist_match = dist; 1188d30bb512STodor Tomov } 1189d30bb512STodor Tomov } 1190d30bb512STodor Tomov 1191d30bb512STodor Tomov return &ov7251_mode_info_data[n]; 1192d30bb512STodor Tomov } 1193d30bb512STodor Tomov 1194d30bb512STodor Tomov static int ov7251_set_format(struct v4l2_subdev *sd, 11950d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1196d30bb512STodor Tomov struct v4l2_subdev_format *format) 1197d30bb512STodor Tomov { 1198d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(sd); 1199d30bb512STodor Tomov struct v4l2_mbus_framefmt *__format; 1200d30bb512STodor Tomov struct v4l2_rect *__crop; 1201d30bb512STodor Tomov const struct ov7251_mode_info *new_mode; 1202d30bb512STodor Tomov int ret = 0; 1203d30bb512STodor Tomov 1204d30bb512STodor Tomov mutex_lock(&ov7251->lock); 1205d30bb512STodor Tomov 12060d346d2aSTomi Valkeinen __crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad, 12070d346d2aSTomi Valkeinen format->which); 1208d30bb512STodor Tomov 1209d30bb512STodor Tomov new_mode = v4l2_find_nearest_size(ov7251_mode_info_data, 1210d30bb512STodor Tomov ARRAY_SIZE(ov7251_mode_info_data), 1211d30bb512STodor Tomov width, height, 1212d30bb512STodor Tomov format->format.width, format->format.height); 1213d30bb512STodor Tomov 1214d30bb512STodor Tomov __crop->width = new_mode->width; 1215d30bb512STodor Tomov __crop->height = new_mode->height; 1216d30bb512STodor Tomov 1217d30bb512STodor Tomov if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 1218d30bb512STodor Tomov ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1219d30bb512STodor Tomov 1, new_mode->exposure_max, 1220d30bb512STodor Tomov 1, new_mode->exposure_def); 1221d30bb512STodor Tomov if (ret < 0) 1222d30bb512STodor Tomov goto exit; 1223d30bb512STodor Tomov 1224d30bb512STodor Tomov ret = __v4l2_ctrl_s_ctrl(ov7251->exposure, 1225d30bb512STodor Tomov new_mode->exposure_def); 1226d30bb512STodor Tomov if (ret < 0) 1227d30bb512STodor Tomov goto exit; 1228d30bb512STodor Tomov 1229d30bb512STodor Tomov ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16); 1230d30bb512STodor Tomov if (ret < 0) 1231d30bb512STodor Tomov goto exit; 1232d30bb512STodor Tomov 1233d30bb512STodor Tomov ov7251->current_mode = new_mode; 1234d30bb512STodor Tomov } 1235d30bb512STodor Tomov 12360d346d2aSTomi Valkeinen __format = __ov7251_get_pad_format(ov7251, sd_state, format->pad, 1237d30bb512STodor Tomov format->which); 1238d30bb512STodor Tomov __format->width = __crop->width; 1239d30bb512STodor Tomov __format->height = __crop->height; 1240d30bb512STodor Tomov __format->code = MEDIA_BUS_FMT_Y10_1X10; 1241d30bb512STodor Tomov __format->field = V4L2_FIELD_NONE; 1242d30bb512STodor Tomov __format->colorspace = V4L2_COLORSPACE_SRGB; 1243d30bb512STodor Tomov __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace); 1244d30bb512STodor Tomov __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, 1245d30bb512STodor Tomov __format->colorspace, __format->ycbcr_enc); 1246d30bb512STodor Tomov __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace); 1247d30bb512STodor Tomov 1248d30bb512STodor Tomov format->format = *__format; 1249d30bb512STodor Tomov 1250d30bb512STodor Tomov exit: 1251d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1252d30bb512STodor Tomov 1253d30bb512STodor Tomov return ret; 1254d30bb512STodor Tomov } 1255d30bb512STodor Tomov 1256d30bb512STodor Tomov static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev, 12570d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 1258d30bb512STodor Tomov { 1259d30bb512STodor Tomov struct v4l2_subdev_format fmt = { 12600d346d2aSTomi Valkeinen .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY 1261d30bb512STodor Tomov : V4L2_SUBDEV_FORMAT_ACTIVE, 1262d30bb512STodor Tomov .format = { 1263d30bb512STodor Tomov .width = 640, 1264d30bb512STodor Tomov .height = 480 1265d30bb512STodor Tomov } 1266d30bb512STodor Tomov }; 1267d30bb512STodor Tomov 12680d346d2aSTomi Valkeinen ov7251_set_format(subdev, sd_state, &fmt); 1269d30bb512STodor Tomov 1270d30bb512STodor Tomov return 0; 1271d30bb512STodor Tomov } 1272d30bb512STodor Tomov 1273d30bb512STodor Tomov static int ov7251_get_selection(struct v4l2_subdev *sd, 12740d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 1275d30bb512STodor Tomov struct v4l2_subdev_selection *sel) 1276d30bb512STodor Tomov { 1277d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(sd); 1278d30bb512STodor Tomov 1279d30bb512STodor Tomov if (sel->target != V4L2_SEL_TGT_CROP) 1280d30bb512STodor Tomov return -EINVAL; 1281d30bb512STodor Tomov 1282d30bb512STodor Tomov mutex_lock(&ov7251->lock); 12830d346d2aSTomi Valkeinen sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad, 1284d30bb512STodor Tomov sel->which); 1285d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1286d30bb512STodor Tomov 1287d30bb512STodor Tomov return 0; 1288d30bb512STodor Tomov } 1289d30bb512STodor Tomov 1290d30bb512STodor Tomov static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable) 1291d30bb512STodor Tomov { 1292d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(subdev); 1293d30bb512STodor Tomov int ret; 1294d30bb512STodor Tomov 1295d30bb512STodor Tomov mutex_lock(&ov7251->lock); 1296d30bb512STodor Tomov 1297d30bb512STodor Tomov if (enable) { 1298*207f4162SDaniel Scally ret = pm_runtime_get_sync(ov7251->dev); 1299*207f4162SDaniel Scally if (ret < 0) 1300*207f4162SDaniel Scally goto unlock_out; 1301*207f4162SDaniel Scally 1302df057b0dSDaniel Scally ret = ov7251_pll_configure(ov7251); 1303*207f4162SDaniel Scally if (ret) { 1304*207f4162SDaniel Scally dev_err(ov7251->dev, "error configuring PLLs\n"); 1305*207f4162SDaniel Scally goto err_power_down; 1306*207f4162SDaniel Scally } 1307df057b0dSDaniel Scally 1308d30bb512STodor Tomov ret = ov7251_set_register_array(ov7251, 1309d30bb512STodor Tomov ov7251->current_mode->data, 1310d30bb512STodor Tomov ov7251->current_mode->data_size); 1311d30bb512STodor Tomov if (ret < 0) { 1312d30bb512STodor Tomov dev_err(ov7251->dev, "could not set mode %dx%d\n", 1313d30bb512STodor Tomov ov7251->current_mode->width, 1314d30bb512STodor Tomov ov7251->current_mode->height); 1315*207f4162SDaniel Scally goto err_power_down; 1316d30bb512STodor Tomov } 1317d30bb512STodor Tomov ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls); 1318d30bb512STodor Tomov if (ret < 0) { 1319d30bb512STodor Tomov dev_err(ov7251->dev, "could not sync v4l2 controls\n"); 1320*207f4162SDaniel Scally goto err_power_down; 1321d30bb512STodor Tomov } 1322d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT, 1323d30bb512STodor Tomov OV7251_SC_MODE_SELECT_STREAMING); 1324*207f4162SDaniel Scally if (ret) 1325*207f4162SDaniel Scally goto err_power_down; 1326d30bb512STodor Tomov } else { 1327d30bb512STodor Tomov ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT, 1328d30bb512STodor Tomov OV7251_SC_MODE_SELECT_SW_STANDBY); 1329*207f4162SDaniel Scally pm_runtime_put(ov7251->dev); 1330d30bb512STodor Tomov } 1331d30bb512STodor Tomov 1332*207f4162SDaniel Scally unlock_out: 1333d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1334*207f4162SDaniel Scally return ret; 1335d30bb512STodor Tomov 1336*207f4162SDaniel Scally err_power_down: 1337*207f4162SDaniel Scally pm_runtime_put_noidle(ov7251->dev); 1338d30bb512STodor Tomov return ret; 1339d30bb512STodor Tomov } 1340d30bb512STodor Tomov 1341d30bb512STodor Tomov static int ov7251_get_frame_interval(struct v4l2_subdev *subdev, 1342d30bb512STodor Tomov struct v4l2_subdev_frame_interval *fi) 1343d30bb512STodor Tomov { 1344d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(subdev); 1345d30bb512STodor Tomov 1346d30bb512STodor Tomov mutex_lock(&ov7251->lock); 1347d30bb512STodor Tomov fi->interval = ov7251->current_mode->timeperframe; 1348d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1349d30bb512STodor Tomov 1350d30bb512STodor Tomov return 0; 1351d30bb512STodor Tomov } 1352d30bb512STodor Tomov 1353d30bb512STodor Tomov static int ov7251_set_frame_interval(struct v4l2_subdev *subdev, 1354d30bb512STodor Tomov struct v4l2_subdev_frame_interval *fi) 1355d30bb512STodor Tomov { 1356d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(subdev); 1357d30bb512STodor Tomov const struct ov7251_mode_info *new_mode; 1358d30bb512STodor Tomov int ret = 0; 1359d30bb512STodor Tomov 1360d30bb512STodor Tomov mutex_lock(&ov7251->lock); 1361d30bb512STodor Tomov new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval); 1362d30bb512STodor Tomov 1363d30bb512STodor Tomov if (new_mode != ov7251->current_mode) { 1364d30bb512STodor Tomov ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1365d30bb512STodor Tomov 1, new_mode->exposure_max, 1366d30bb512STodor Tomov 1, new_mode->exposure_def); 1367d30bb512STodor Tomov if (ret < 0) 1368d30bb512STodor Tomov goto exit; 1369d30bb512STodor Tomov 1370d30bb512STodor Tomov ret = __v4l2_ctrl_s_ctrl(ov7251->exposure, 1371d30bb512STodor Tomov new_mode->exposure_def); 1372d30bb512STodor Tomov if (ret < 0) 1373d30bb512STodor Tomov goto exit; 1374d30bb512STodor Tomov 1375d30bb512STodor Tomov ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16); 1376d30bb512STodor Tomov if (ret < 0) 1377d30bb512STodor Tomov goto exit; 1378d30bb512STodor Tomov 1379d30bb512STodor Tomov ov7251->current_mode = new_mode; 1380d30bb512STodor Tomov } 1381d30bb512STodor Tomov 1382d30bb512STodor Tomov fi->interval = ov7251->current_mode->timeperframe; 1383d30bb512STodor Tomov 1384d30bb512STodor Tomov exit: 1385d30bb512STodor Tomov mutex_unlock(&ov7251->lock); 1386d30bb512STodor Tomov 1387d30bb512STodor Tomov return ret; 1388d30bb512STodor Tomov } 1389d30bb512STodor Tomov 1390d30bb512STodor Tomov static const struct v4l2_subdev_core_ops ov7251_core_ops = { 1391d30bb512STodor Tomov .s_power = ov7251_s_power, 1392d30bb512STodor Tomov }; 1393d30bb512STodor Tomov 1394d30bb512STodor Tomov static const struct v4l2_subdev_video_ops ov7251_video_ops = { 1395d30bb512STodor Tomov .s_stream = ov7251_s_stream, 1396d30bb512STodor Tomov .g_frame_interval = ov7251_get_frame_interval, 1397d30bb512STodor Tomov .s_frame_interval = ov7251_set_frame_interval, 1398d30bb512STodor Tomov }; 1399d30bb512STodor Tomov 1400d30bb512STodor Tomov static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = { 1401d30bb512STodor Tomov .init_cfg = ov7251_entity_init_cfg, 1402d30bb512STodor Tomov .enum_mbus_code = ov7251_enum_mbus_code, 1403d30bb512STodor Tomov .enum_frame_size = ov7251_enum_frame_size, 1404d30bb512STodor Tomov .enum_frame_interval = ov7251_enum_frame_ival, 1405d30bb512STodor Tomov .get_fmt = ov7251_get_format, 1406d30bb512STodor Tomov .set_fmt = ov7251_set_format, 1407d30bb512STodor Tomov .get_selection = ov7251_get_selection, 1408d30bb512STodor Tomov }; 1409d30bb512STodor Tomov 1410d30bb512STodor Tomov static const struct v4l2_subdev_ops ov7251_subdev_ops = { 1411d30bb512STodor Tomov .core = &ov7251_core_ops, 1412d30bb512STodor Tomov .video = &ov7251_video_ops, 1413d30bb512STodor Tomov .pad = &ov7251_subdev_pad_ops, 1414d30bb512STodor Tomov }; 1415d30bb512STodor Tomov 1416cc125aaaSDaniel Scally static int ov7251_check_hwcfg(struct ov7251 *ov7251) 1417cc125aaaSDaniel Scally { 1418cc125aaaSDaniel Scally struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev); 1419cc125aaaSDaniel Scally struct v4l2_fwnode_endpoint bus_cfg = { 1420cc125aaaSDaniel Scally .bus_type = V4L2_MBUS_CSI2_DPHY, 1421cc125aaaSDaniel Scally }; 1422cc125aaaSDaniel Scally struct fwnode_handle *endpoint; 1423cc125aaaSDaniel Scally unsigned int i, j; 1424cc125aaaSDaniel Scally int ret; 1425cc125aaaSDaniel Scally 1426cc125aaaSDaniel Scally endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL); 1427cc125aaaSDaniel Scally if (!endpoint) 1428cc125aaaSDaniel Scally return -EPROBE_DEFER; /* could be provided by cio2-bridge */ 1429cc125aaaSDaniel Scally 1430cc125aaaSDaniel Scally ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg); 1431cc125aaaSDaniel Scally fwnode_handle_put(endpoint); 1432cc125aaaSDaniel Scally if (ret) 1433cc125aaaSDaniel Scally return dev_err_probe(ov7251->dev, ret, 1434cc125aaaSDaniel Scally "parsing endpoint node failed\n"); 1435cc125aaaSDaniel Scally 1436cc125aaaSDaniel Scally if (!bus_cfg.nr_of_link_frequencies) { 1437cc125aaaSDaniel Scally ret = dev_err_probe(ov7251->dev, -EINVAL, 1438cc125aaaSDaniel Scally "no link frequencies defined\n"); 1439cc125aaaSDaniel Scally goto out_free_bus_cfg; 1440cc125aaaSDaniel Scally } 1441cc125aaaSDaniel Scally 1442cc125aaaSDaniel Scally for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) { 1443cc125aaaSDaniel Scally for (j = 0; j < ARRAY_SIZE(link_freq); j++) 1444cc125aaaSDaniel Scally if (bus_cfg.link_frequencies[i] == link_freq[j]) 1445cc125aaaSDaniel Scally break; 1446cc125aaaSDaniel Scally 1447cc125aaaSDaniel Scally if (j < ARRAY_SIZE(link_freq)) 1448cc125aaaSDaniel Scally break; 1449cc125aaaSDaniel Scally } 1450cc125aaaSDaniel Scally 1451cc125aaaSDaniel Scally if (i == bus_cfg.nr_of_link_frequencies) { 1452cc125aaaSDaniel Scally ret = dev_err_probe(ov7251->dev, -EINVAL, 1453cc125aaaSDaniel Scally "no supported link freq found\n"); 1454cc125aaaSDaniel Scally goto out_free_bus_cfg; 1455cc125aaaSDaniel Scally } 1456cc125aaaSDaniel Scally 1457cc125aaaSDaniel Scally ov7251->link_freq_idx = i; 1458cc125aaaSDaniel Scally 1459cc125aaaSDaniel Scally out_free_bus_cfg: 1460cc125aaaSDaniel Scally v4l2_fwnode_endpoint_free(&bus_cfg); 1461cc125aaaSDaniel Scally 1462cc125aaaSDaniel Scally return ret; 1463cc125aaaSDaniel Scally } 1464cc125aaaSDaniel Scally 1465e92932c3SDaniel Scally static int ov7251_detect_chip(struct ov7251 *ov7251) 1466e92932c3SDaniel Scally { 1467e92932c3SDaniel Scally u8 chip_id_high, chip_id_low, chip_rev; 1468e92932c3SDaniel Scally int ret; 1469e92932c3SDaniel Scally 1470e92932c3SDaniel Scally ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high); 1471e92932c3SDaniel Scally if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE) 1472e92932c3SDaniel Scally return dev_err_probe(ov7251->dev, -ENODEV, 1473e92932c3SDaniel Scally "could not read ID high\n"); 1474e92932c3SDaniel Scally 1475e92932c3SDaniel Scally ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low); 1476e92932c3SDaniel Scally if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE) 1477e92932c3SDaniel Scally return dev_err_probe(ov7251->dev, -ENODEV, 1478e92932c3SDaniel Scally "could not read ID low\n"); 1479e92932c3SDaniel Scally 1480e92932c3SDaniel Scally ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev); 1481e92932c3SDaniel Scally if (ret < 0) 1482e92932c3SDaniel Scally return dev_err_probe(ov7251->dev, -ENODEV, 1483e92932c3SDaniel Scally "could not read revision\n"); 1484e92932c3SDaniel Scally chip_rev >>= 4; 1485e92932c3SDaniel Scally 1486e92932c3SDaniel Scally dev_info(ov7251->dev, 1487e92932c3SDaniel Scally "OV7251 revision %x (%s) detected at address 0x%02x\n", 1488e92932c3SDaniel Scally chip_rev, 1489e92932c3SDaniel Scally chip_rev == 0x4 ? "1A / 1B" : 1490e92932c3SDaniel Scally chip_rev == 0x5 ? "1C / 1D" : 1491e92932c3SDaniel Scally chip_rev == 0x6 ? "1E" : 1492e92932c3SDaniel Scally chip_rev == 0x7 ? "1F" : "unknown", 1493e92932c3SDaniel Scally ov7251->i2c_client->addr); 1494e92932c3SDaniel Scally 1495e92932c3SDaniel Scally return 0; 1496e92932c3SDaniel Scally } 1497e92932c3SDaniel Scally 1498d30bb512STodor Tomov static int ov7251_probe(struct i2c_client *client) 1499d30bb512STodor Tomov { 1500d30bb512STodor Tomov struct device *dev = &client->dev; 1501d30bb512STodor Tomov struct ov7251 *ov7251; 1502ed9566ceSDaniel Scally unsigned int rate = 0, clk_rate = 0; 15031757b44eSDaniel Scally s64 pixel_rate; 1504d30bb512STodor Tomov int ret; 1505df057b0dSDaniel Scally int i; 1506d30bb512STodor Tomov 1507d30bb512STodor Tomov ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL); 1508d30bb512STodor Tomov if (!ov7251) 1509d30bb512STodor Tomov return -ENOMEM; 1510d30bb512STodor Tomov 1511d30bb512STodor Tomov ov7251->i2c_client = client; 1512d30bb512STodor Tomov ov7251->dev = dev; 1513d30bb512STodor Tomov 1514cc125aaaSDaniel Scally ret = ov7251_check_hwcfg(ov7251); 1515cc125aaaSDaniel Scally if (ret) 1516d30bb512STodor Tomov return ret; 1517d30bb512STodor Tomov 1518d30bb512STodor Tomov /* get system clock (xclk) */ 1519ed9566ceSDaniel Scally ov7251->xclk = devm_clk_get_optional(dev, NULL); 1520ed9566ceSDaniel Scally if (IS_ERR(ov7251->xclk)) 1521ed9566ceSDaniel Scally return dev_err_probe(dev, PTR_ERR(ov7251->xclk), 1522ed9566ceSDaniel Scally "could not get xclk"); 1523d30bb512STodor Tomov 1524ed9566ceSDaniel Scally /* 1525ed9566ceSDaniel Scally * We could have either a 24MHz or 19.2MHz clock rate from either DT or 1526ed9566ceSDaniel Scally * ACPI. We also need to support the IPU3 case which will have both an 1527ed9566ceSDaniel Scally * external clock AND a clock-frequency property. 1528ed9566ceSDaniel Scally */ 1529d30bb512STodor Tomov ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", 1530ed9566ceSDaniel Scally &rate); 1531ed9566ceSDaniel Scally if (ret && !ov7251->xclk) 1532ed9566ceSDaniel Scally return dev_err_probe(dev, ret, "invalid clock config\n"); 1533ed9566ceSDaniel Scally 1534ed9566ceSDaniel Scally clk_rate = clk_get_rate(ov7251->xclk); 1535ed9566ceSDaniel Scally ov7251->xclk_freq = clk_rate ? clk_rate : rate; 1536ed9566ceSDaniel Scally 1537ed9566ceSDaniel Scally if (ov7251->xclk_freq == 0) 1538ed9566ceSDaniel Scally return dev_err_probe(dev, -EINVAL, "invalid clock frequency\n"); 1539ed9566ceSDaniel Scally 1540ed9566ceSDaniel Scally if (!ret && ov7251->xclk) { 1541ed9566ceSDaniel Scally ret = clk_set_rate(ov7251->xclk, rate); 1542ed9566ceSDaniel Scally if (ret) 1543ed9566ceSDaniel Scally return dev_err_probe(dev, ret, 1544ed9566ceSDaniel Scally "failed to set clock rate\n"); 1545d30bb512STodor Tomov } 1546d30bb512STodor Tomov 1547df057b0dSDaniel Scally for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++) 1548df057b0dSDaniel Scally if (ov7251->xclk_freq == supported_xclk_rates[i]) 1549df057b0dSDaniel Scally break; 1550df057b0dSDaniel Scally 1551df057b0dSDaniel Scally if (i == ARRAY_SIZE(supported_xclk_rates)) 1552df057b0dSDaniel Scally return dev_err_probe(dev, -EINVAL, 1553df057b0dSDaniel Scally "clock rate %u Hz is unsupported\n", 1554df057b0dSDaniel Scally ov7251->xclk_freq); 1555df057b0dSDaniel Scally 1556df057b0dSDaniel Scally ov7251->pll_cfgs = ov7251_pll_cfgs[i]; 1557d30bb512STodor Tomov 1558d30bb512STodor Tomov ov7251->io_regulator = devm_regulator_get(dev, "vdddo"); 1559d30bb512STodor Tomov if (IS_ERR(ov7251->io_regulator)) { 1560d30bb512STodor Tomov dev_err(dev, "cannot get io regulator\n"); 1561d30bb512STodor Tomov return PTR_ERR(ov7251->io_regulator); 1562d30bb512STodor Tomov } 1563d30bb512STodor Tomov 1564d30bb512STodor Tomov ov7251->core_regulator = devm_regulator_get(dev, "vddd"); 1565d30bb512STodor Tomov if (IS_ERR(ov7251->core_regulator)) { 1566d30bb512STodor Tomov dev_err(dev, "cannot get core regulator\n"); 1567d30bb512STodor Tomov return PTR_ERR(ov7251->core_regulator); 1568d30bb512STodor Tomov } 1569d30bb512STodor Tomov 1570d30bb512STodor Tomov ov7251->analog_regulator = devm_regulator_get(dev, "vdda"); 1571d30bb512STodor Tomov if (IS_ERR(ov7251->analog_regulator)) { 1572d30bb512STodor Tomov dev_err(dev, "cannot get analog regulator\n"); 1573d30bb512STodor Tomov return PTR_ERR(ov7251->analog_regulator); 1574d30bb512STodor Tomov } 1575d30bb512STodor Tomov 1576d30bb512STodor Tomov ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH); 1577d30bb512STodor Tomov if (IS_ERR(ov7251->enable_gpio)) { 1578d30bb512STodor Tomov dev_err(dev, "cannot get enable gpio\n"); 1579d30bb512STodor Tomov return PTR_ERR(ov7251->enable_gpio); 1580d30bb512STodor Tomov } 1581d30bb512STodor Tomov 1582d30bb512STodor Tomov mutex_init(&ov7251->lock); 1583d30bb512STodor Tomov 1584d30bb512STodor Tomov v4l2_ctrl_handler_init(&ov7251->ctrls, 7); 1585d30bb512STodor Tomov ov7251->ctrls.lock = &ov7251->lock; 1586d30bb512STodor Tomov 1587d30bb512STodor Tomov v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops, 1588d30bb512STodor Tomov V4L2_CID_HFLIP, 0, 1, 1, 0); 1589d30bb512STodor Tomov v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops, 1590d30bb512STodor Tomov V4L2_CID_VFLIP, 0, 1, 1, 0); 1591d30bb512STodor Tomov ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops, 1592d30bb512STodor Tomov V4L2_CID_EXPOSURE, 1, 32, 1, 32); 1593d30bb512STodor Tomov ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops, 1594d30bb512STodor Tomov V4L2_CID_GAIN, 16, 1023, 1, 16); 1595d30bb512STodor Tomov v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops, 1596d30bb512STodor Tomov V4L2_CID_TEST_PATTERN, 1597d30bb512STodor Tomov ARRAY_SIZE(ov7251_test_pattern_menu) - 1, 1598d30bb512STodor Tomov 0, 0, ov7251_test_pattern_menu); 15991757b44eSDaniel Scally 16001757b44eSDaniel Scally pixel_rate = pixel_rates[ov7251->link_freq_idx]; 1601d30bb512STodor Tomov ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls, 1602d30bb512STodor Tomov &ov7251_ctrl_ops, 1603d30bb512STodor Tomov V4L2_CID_PIXEL_RATE, 16041757b44eSDaniel Scally pixel_rate, INT_MAX, 16051757b44eSDaniel Scally pixel_rate, pixel_rate); 1606d30bb512STodor Tomov ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls, 1607d30bb512STodor Tomov &ov7251_ctrl_ops, 1608d30bb512STodor Tomov V4L2_CID_LINK_FREQ, 1609d30bb512STodor Tomov ARRAY_SIZE(link_freq) - 1, 16101757b44eSDaniel Scally ov7251->link_freq_idx, 16111757b44eSDaniel Scally link_freq); 1612d30bb512STodor Tomov if (ov7251->link_freq) 1613d30bb512STodor Tomov ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 16141757b44eSDaniel Scally if (ov7251->pixel_clock) 16151757b44eSDaniel Scally ov7251->pixel_clock->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1616d30bb512STodor Tomov 1617d30bb512STodor Tomov ov7251->sd.ctrl_handler = &ov7251->ctrls; 1618d30bb512STodor Tomov 1619d30bb512STodor Tomov if (ov7251->ctrls.error) { 1620d30bb512STodor Tomov dev_err(dev, "%s: control initialization error %d\n", 1621d30bb512STodor Tomov __func__, ov7251->ctrls.error); 1622d30bb512STodor Tomov ret = ov7251->ctrls.error; 1623d30bb512STodor Tomov goto free_ctrl; 1624d30bb512STodor Tomov } 1625d30bb512STodor Tomov 1626d30bb512STodor Tomov v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops); 1627d30bb512STodor Tomov ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1628d30bb512STodor Tomov ov7251->pad.flags = MEDIA_PAD_FL_SOURCE; 1629d30bb512STodor Tomov ov7251->sd.dev = &client->dev; 1630d30bb512STodor Tomov ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1631d30bb512STodor Tomov 1632d30bb512STodor Tomov ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad); 1633d30bb512STodor Tomov if (ret < 0) { 1634d30bb512STodor Tomov dev_err(dev, "could not register media entity\n"); 1635d30bb512STodor Tomov goto free_ctrl; 1636d30bb512STodor Tomov } 1637d30bb512STodor Tomov 1638*207f4162SDaniel Scally ret = ov7251_set_power_on(ov7251->dev); 1639*207f4162SDaniel Scally if (ret) 1640d30bb512STodor Tomov goto free_entity; 1641d30bb512STodor Tomov 1642e92932c3SDaniel Scally ret = ov7251_detect_chip(ov7251); 1643e92932c3SDaniel Scally if (ret) 1644d30bb512STodor Tomov goto power_down; 1645d30bb512STodor Tomov 1646*207f4162SDaniel Scally pm_runtime_set_active(&client->dev); 1647*207f4162SDaniel Scally pm_runtime_get_noresume(&client->dev); 1648*207f4162SDaniel Scally pm_runtime_enable(&client->dev); 1649d30bb512STodor Tomov 1650d30bb512STodor Tomov ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00, 1651d30bb512STodor Tomov &ov7251->pre_isp_00); 1652d30bb512STodor Tomov if (ret < 0) { 1653d30bb512STodor Tomov dev_err(dev, "could not read test pattern value\n"); 1654d30bb512STodor Tomov ret = -ENODEV; 1655*207f4162SDaniel Scally goto err_pm_runtime; 1656d30bb512STodor Tomov } 1657d30bb512STodor Tomov 1658d30bb512STodor Tomov ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1, 1659d30bb512STodor Tomov &ov7251->timing_format1); 1660d30bb512STodor Tomov if (ret < 0) { 1661d30bb512STodor Tomov dev_err(dev, "could not read vflip value\n"); 1662d30bb512STodor Tomov ret = -ENODEV; 1663*207f4162SDaniel Scally goto err_pm_runtime; 1664d30bb512STodor Tomov } 1665d30bb512STodor Tomov 1666d30bb512STodor Tomov ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2, 1667d30bb512STodor Tomov &ov7251->timing_format2); 1668d30bb512STodor Tomov if (ret < 0) { 1669d30bb512STodor Tomov dev_err(dev, "could not read hflip value\n"); 1670d30bb512STodor Tomov ret = -ENODEV; 1671*207f4162SDaniel Scally goto err_pm_runtime; 1672d30bb512STodor Tomov } 1673d30bb512STodor Tomov 1674*207f4162SDaniel Scally pm_runtime_set_autosuspend_delay(&client->dev, 1000); 1675*207f4162SDaniel Scally pm_runtime_use_autosuspend(&client->dev); 1676*207f4162SDaniel Scally pm_runtime_put_autosuspend(&client->dev); 1677d30bb512STodor Tomov 1678d30bb512STodor Tomov ret = v4l2_async_register_subdev(&ov7251->sd); 1679d30bb512STodor Tomov if (ret < 0) { 1680d30bb512STodor Tomov dev_err(dev, "could not register v4l2 device\n"); 1681d30bb512STodor Tomov goto free_entity; 1682d30bb512STodor Tomov } 1683d30bb512STodor Tomov 1684d30bb512STodor Tomov ov7251_entity_init_cfg(&ov7251->sd, NULL); 1685d30bb512STodor Tomov 1686d30bb512STodor Tomov return 0; 1687d30bb512STodor Tomov 1688*207f4162SDaniel Scally err_pm_runtime: 1689*207f4162SDaniel Scally pm_runtime_disable(ov7251->dev); 1690*207f4162SDaniel Scally pm_runtime_put_noidle(ov7251->dev); 1691d30bb512STodor Tomov power_down: 1692*207f4162SDaniel Scally ov7251_set_power_off(ov7251->dev); 1693d30bb512STodor Tomov free_entity: 1694d30bb512STodor Tomov media_entity_cleanup(&ov7251->sd.entity); 1695d30bb512STodor Tomov free_ctrl: 1696d30bb512STodor Tomov v4l2_ctrl_handler_free(&ov7251->ctrls); 1697d30bb512STodor Tomov mutex_destroy(&ov7251->lock); 1698d30bb512STodor Tomov 1699d30bb512STodor Tomov return ret; 1700d30bb512STodor Tomov } 1701d30bb512STodor Tomov 1702d30bb512STodor Tomov static int ov7251_remove(struct i2c_client *client) 1703d30bb512STodor Tomov { 1704d30bb512STodor Tomov struct v4l2_subdev *sd = i2c_get_clientdata(client); 1705d30bb512STodor Tomov struct ov7251 *ov7251 = to_ov7251(sd); 1706d30bb512STodor Tomov 1707d30bb512STodor Tomov v4l2_async_unregister_subdev(&ov7251->sd); 1708d30bb512STodor Tomov media_entity_cleanup(&ov7251->sd.entity); 1709d30bb512STodor Tomov v4l2_ctrl_handler_free(&ov7251->ctrls); 1710d30bb512STodor Tomov mutex_destroy(&ov7251->lock); 1711d30bb512STodor Tomov 1712*207f4162SDaniel Scally pm_runtime_disable(ov7251->dev); 1713*207f4162SDaniel Scally if (!pm_runtime_status_suspended(ov7251->dev)) 1714*207f4162SDaniel Scally ov7251_set_power_off(ov7251->dev); 1715*207f4162SDaniel Scally pm_runtime_set_suspended(ov7251->dev); 1716*207f4162SDaniel Scally 1717d30bb512STodor Tomov return 0; 1718d30bb512STodor Tomov } 1719d30bb512STodor Tomov 1720*207f4162SDaniel Scally static const struct dev_pm_ops ov7251_pm_ops = { 1721*207f4162SDaniel Scally SET_RUNTIME_PM_OPS(ov7251_set_power_off, ov7251_set_power_on, NULL) 1722*207f4162SDaniel Scally }; 1723*207f4162SDaniel Scally 1724d30bb512STodor Tomov static const struct of_device_id ov7251_of_match[] = { 1725d30bb512STodor Tomov { .compatible = "ovti,ov7251" }, 1726d30bb512STodor Tomov { /* sentinel */ } 1727d30bb512STodor Tomov }; 1728d30bb512STodor Tomov MODULE_DEVICE_TABLE(of, ov7251_of_match); 1729d30bb512STodor Tomov 17306766cff6SDaniel Scally static const struct acpi_device_id ov7251_acpi_match[] = { 17316766cff6SDaniel Scally { "INT347E" }, 17326766cff6SDaniel Scally { } 17336766cff6SDaniel Scally }; 17346766cff6SDaniel Scally MODULE_DEVICE_TABLE(acpi, ov7251_acpi_match); 17356766cff6SDaniel Scally 1736d30bb512STodor Tomov static struct i2c_driver ov7251_i2c_driver = { 1737d30bb512STodor Tomov .driver = { 1738d30bb512STodor Tomov .of_match_table = ov7251_of_match, 17396766cff6SDaniel Scally .acpi_match_table = ov7251_acpi_match, 1740d30bb512STodor Tomov .name = "ov7251", 1741*207f4162SDaniel Scally .pm = &ov7251_pm_ops, 1742d30bb512STodor Tomov }, 1743d30bb512STodor Tomov .probe_new = ov7251_probe, 1744d30bb512STodor Tomov .remove = ov7251_remove, 1745d30bb512STodor Tomov }; 1746d30bb512STodor Tomov 1747d30bb512STodor Tomov module_i2c_driver(ov7251_i2c_driver); 1748d30bb512STodor Tomov 1749d30bb512STodor Tomov MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver"); 1750d30bb512STodor Tomov MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>"); 1751d30bb512STodor Tomov MODULE_LICENSE("GPL v2"); 1752