19746b117SMartina Krasteva // SPDX-License-Identifier: GPL-2.0-only 29746b117SMartina Krasteva /* 39746b117SMartina Krasteva * Sony imx334 sensor driver 49746b117SMartina Krasteva * 59746b117SMartina Krasteva * Copyright (C) 2021 Intel Corporation 69746b117SMartina Krasteva */ 79746b117SMartina Krasteva #include <asm/unaligned.h> 89746b117SMartina Krasteva 99746b117SMartina Krasteva #include <linux/clk.h> 109746b117SMartina Krasteva #include <linux/delay.h> 119746b117SMartina Krasteva #include <linux/i2c.h> 129746b117SMartina Krasteva #include <linux/module.h> 139746b117SMartina Krasteva #include <linux/pm_runtime.h> 149746b117SMartina Krasteva 159746b117SMartina Krasteva #include <media/v4l2-ctrls.h> 169746b117SMartina Krasteva #include <media/v4l2-fwnode.h> 179746b117SMartina Krasteva #include <media/v4l2-subdev.h> 189746b117SMartina Krasteva 199746b117SMartina Krasteva /* Streaming Mode */ 209746b117SMartina Krasteva #define IMX334_REG_MODE_SELECT 0x3000 219746b117SMartina Krasteva #define IMX334_MODE_STANDBY 0x01 229746b117SMartina Krasteva #define IMX334_MODE_STREAMING 0x00 239746b117SMartina Krasteva 249746b117SMartina Krasteva /* Lines per frame */ 259746b117SMartina Krasteva #define IMX334_REG_LPFR 0x3030 269746b117SMartina Krasteva 279746b117SMartina Krasteva /* Chip ID */ 289746b117SMartina Krasteva #define IMX334_REG_ID 0x3044 299746b117SMartina Krasteva #define IMX334_ID 0x1e 309746b117SMartina Krasteva 319746b117SMartina Krasteva /* Exposure control */ 329746b117SMartina Krasteva #define IMX334_REG_SHUTTER 0x3058 339746b117SMartina Krasteva #define IMX334_EXPOSURE_MIN 1 349746b117SMartina Krasteva #define IMX334_EXPOSURE_OFFSET 5 359746b117SMartina Krasteva #define IMX334_EXPOSURE_STEP 1 369746b117SMartina Krasteva #define IMX334_EXPOSURE_DEFAULT 0x0648 379746b117SMartina Krasteva 389746b117SMartina Krasteva /* Analog gain control */ 399746b117SMartina Krasteva #define IMX334_REG_AGAIN 0x30e8 409746b117SMartina Krasteva #define IMX334_AGAIN_MIN 0 419746b117SMartina Krasteva #define IMX334_AGAIN_MAX 240 429746b117SMartina Krasteva #define IMX334_AGAIN_STEP 1 439746b117SMartina Krasteva #define IMX334_AGAIN_DEFAULT 0 449746b117SMartina Krasteva 459746b117SMartina Krasteva /* Group hold register */ 469746b117SMartina Krasteva #define IMX334_REG_HOLD 0x3001 479746b117SMartina Krasteva 489746b117SMartina Krasteva /* Input clock rate */ 499746b117SMartina Krasteva #define IMX334_INCLK_RATE 24000000 509746b117SMartina Krasteva 519746b117SMartina Krasteva /* CSI2 HW configuration */ 529746b117SMartina Krasteva #define IMX334_LINK_FREQ 891000000 539746b117SMartina Krasteva #define IMX334_NUM_DATA_LANES 4 549746b117SMartina Krasteva 559746b117SMartina Krasteva #define IMX334_REG_MIN 0x00 569746b117SMartina Krasteva #define IMX334_REG_MAX 0xfffff 579746b117SMartina Krasteva 589746b117SMartina Krasteva /** 599746b117SMartina Krasteva * struct imx334_reg - imx334 sensor register 609746b117SMartina Krasteva * @address: Register address 619746b117SMartina Krasteva * @val: Register value 629746b117SMartina Krasteva */ 639746b117SMartina Krasteva struct imx334_reg { 649746b117SMartina Krasteva u16 address; 659746b117SMartina Krasteva u8 val; 669746b117SMartina Krasteva }; 679746b117SMartina Krasteva 689746b117SMartina Krasteva /** 699746b117SMartina Krasteva * struct imx334_reg_list - imx334 sensor register list 709746b117SMartina Krasteva * @num_of_regs: Number of registers in the list 719746b117SMartina Krasteva * @regs: Pointer to register list 729746b117SMartina Krasteva */ 739746b117SMartina Krasteva struct imx334_reg_list { 749746b117SMartina Krasteva u32 num_of_regs; 759746b117SMartina Krasteva const struct imx334_reg *regs; 769746b117SMartina Krasteva }; 779746b117SMartina Krasteva 789746b117SMartina Krasteva /** 799746b117SMartina Krasteva * struct imx334_mode - imx334 sensor mode structure 809746b117SMartina Krasteva * @width: Frame width 819746b117SMartina Krasteva * @height: Frame height 829746b117SMartina Krasteva * @code: Format code 839746b117SMartina Krasteva * @hblank: Horizontal blanking in lines 849746b117SMartina Krasteva * @vblank: Vertical blanking in lines 859746b117SMartina Krasteva * @vblank_min: Minimal vertical blanking in lines 869746b117SMartina Krasteva * @vblank_max: Maximum vertical blanking in lines 879746b117SMartina Krasteva * @pclk: Sensor pixel clock 889746b117SMartina Krasteva * @link_freq_idx: Link frequency index 899746b117SMartina Krasteva * @reg_list: Register list for sensor mode 909746b117SMartina Krasteva */ 919746b117SMartina Krasteva struct imx334_mode { 929746b117SMartina Krasteva u32 width; 939746b117SMartina Krasteva u32 height; 949746b117SMartina Krasteva u32 code; 959746b117SMartina Krasteva u32 hblank; 969746b117SMartina Krasteva u32 vblank; 979746b117SMartina Krasteva u32 vblank_min; 989746b117SMartina Krasteva u32 vblank_max; 999746b117SMartina Krasteva u64 pclk; 1009746b117SMartina Krasteva u32 link_freq_idx; 1019746b117SMartina Krasteva struct imx334_reg_list reg_list; 1029746b117SMartina Krasteva }; 1039746b117SMartina Krasteva 1049746b117SMartina Krasteva /** 1059746b117SMartina Krasteva * struct imx334 - imx334 sensor device structure 1069746b117SMartina Krasteva * @dev: Pointer to generic device 1079746b117SMartina Krasteva * @client: Pointer to i2c client 1089746b117SMartina Krasteva * @sd: V4L2 sub-device 1099746b117SMartina Krasteva * @pad: Media pad. Only one pad supported 1109746b117SMartina Krasteva * @reset_gpio: Sensor reset gpio 1119746b117SMartina Krasteva * @inclk: Sensor input clock 1129746b117SMartina Krasteva * @ctrl_handler: V4L2 control handler 1139746b117SMartina Krasteva * @link_freq_ctrl: Pointer to link frequency control 1149746b117SMartina Krasteva * @pclk_ctrl: Pointer to pixel clock control 1159746b117SMartina Krasteva * @hblank_ctrl: Pointer to horizontal blanking control 1169746b117SMartina Krasteva * @vblank_ctrl: Pointer to vertical blanking control 1179746b117SMartina Krasteva * @exp_ctrl: Pointer to exposure control 1189746b117SMartina Krasteva * @again_ctrl: Pointer to analog gain control 1199746b117SMartina Krasteva * @vblank: Vertical blanking in lines 1209746b117SMartina Krasteva * @cur_mode: Pointer to current selected sensor mode 1219746b117SMartina Krasteva * @mutex: Mutex for serializing sensor controls 1229746b117SMartina Krasteva * @streaming: Flag indicating streaming state 1239746b117SMartina Krasteva */ 1249746b117SMartina Krasteva struct imx334 { 1259746b117SMartina Krasteva struct device *dev; 1269746b117SMartina Krasteva struct i2c_client *client; 1279746b117SMartina Krasteva struct v4l2_subdev sd; 1289746b117SMartina Krasteva struct media_pad pad; 1299746b117SMartina Krasteva struct gpio_desc *reset_gpio; 1309746b117SMartina Krasteva struct clk *inclk; 1319746b117SMartina Krasteva struct v4l2_ctrl_handler ctrl_handler; 1329746b117SMartina Krasteva struct v4l2_ctrl *link_freq_ctrl; 1339746b117SMartina Krasteva struct v4l2_ctrl *pclk_ctrl; 1349746b117SMartina Krasteva struct v4l2_ctrl *hblank_ctrl; 1359746b117SMartina Krasteva struct v4l2_ctrl *vblank_ctrl; 1369746b117SMartina Krasteva struct { 1379746b117SMartina Krasteva struct v4l2_ctrl *exp_ctrl; 1389746b117SMartina Krasteva struct v4l2_ctrl *again_ctrl; 1399746b117SMartina Krasteva }; 1409746b117SMartina Krasteva u32 vblank; 1419746b117SMartina Krasteva const struct imx334_mode *cur_mode; 1429746b117SMartina Krasteva struct mutex mutex; 1439746b117SMartina Krasteva bool streaming; 1449746b117SMartina Krasteva }; 1459746b117SMartina Krasteva 1469746b117SMartina Krasteva static const s64 link_freq[] = { 1479746b117SMartina Krasteva IMX334_LINK_FREQ, 1489746b117SMartina Krasteva }; 1499746b117SMartina Krasteva 1509746b117SMartina Krasteva /* Sensor mode registers */ 1519746b117SMartina Krasteva static const struct imx334_reg mode_3840x2160_regs[] = { 1529746b117SMartina Krasteva {0x3000, 0x01}, 1539746b117SMartina Krasteva {0x3002, 0x00}, 1549746b117SMartina Krasteva {0x3018, 0x04}, 1559746b117SMartina Krasteva {0x37b0, 0x36}, 1569746b117SMartina Krasteva {0x304c, 0x00}, 1579746b117SMartina Krasteva {0x300c, 0x3b}, 1589746b117SMartina Krasteva {0x300d, 0x2a}, 1599746b117SMartina Krasteva {0x3034, 0x26}, 1609746b117SMartina Krasteva {0x3035, 0x02}, 1619746b117SMartina Krasteva {0x314c, 0x29}, 1629746b117SMartina Krasteva {0x314d, 0x01}, 1639746b117SMartina Krasteva {0x315a, 0x02}, 1649746b117SMartina Krasteva {0x3168, 0xa0}, 1659746b117SMartina Krasteva {0x316a, 0x7e}, 1669746b117SMartina Krasteva {0x3288, 0x21}, 1679746b117SMartina Krasteva {0x328a, 0x02}, 1689746b117SMartina Krasteva {0x302c, 0x3c}, 1699746b117SMartina Krasteva {0x302e, 0x00}, 1709746b117SMartina Krasteva {0x302f, 0x0f}, 1719746b117SMartina Krasteva {0x3076, 0x70}, 1729746b117SMartina Krasteva {0x3077, 0x08}, 1739746b117SMartina Krasteva {0x3090, 0x70}, 1749746b117SMartina Krasteva {0x3091, 0x08}, 1759746b117SMartina Krasteva {0x30d8, 0x20}, 1769746b117SMartina Krasteva {0x30d9, 0x12}, 1779746b117SMartina Krasteva {0x3308, 0x70}, 1789746b117SMartina Krasteva {0x3309, 0x08}, 1799746b117SMartina Krasteva {0x3414, 0x05}, 1809746b117SMartina Krasteva {0x3416, 0x18}, 1819746b117SMartina Krasteva {0x35ac, 0x0e}, 1829746b117SMartina Krasteva {0x3648, 0x01}, 1839746b117SMartina Krasteva {0x364a, 0x04}, 1849746b117SMartina Krasteva {0x364c, 0x04}, 1859746b117SMartina Krasteva {0x3678, 0x01}, 1869746b117SMartina Krasteva {0x367c, 0x31}, 1879746b117SMartina Krasteva {0x367e, 0x31}, 1889746b117SMartina Krasteva {0x3708, 0x02}, 1899746b117SMartina Krasteva {0x3714, 0x01}, 1909746b117SMartina Krasteva {0x3715, 0x02}, 1919746b117SMartina Krasteva {0x3716, 0x02}, 1929746b117SMartina Krasteva {0x3717, 0x02}, 1939746b117SMartina Krasteva {0x371c, 0x3d}, 1949746b117SMartina Krasteva {0x371d, 0x3f}, 1959746b117SMartina Krasteva {0x372c, 0x00}, 1969746b117SMartina Krasteva {0x372d, 0x00}, 1979746b117SMartina Krasteva {0x372e, 0x46}, 1989746b117SMartina Krasteva {0x372f, 0x00}, 1999746b117SMartina Krasteva {0x3730, 0x89}, 2009746b117SMartina Krasteva {0x3731, 0x00}, 2019746b117SMartina Krasteva {0x3732, 0x08}, 2029746b117SMartina Krasteva {0x3733, 0x01}, 2039746b117SMartina Krasteva {0x3734, 0xfe}, 2049746b117SMartina Krasteva {0x3735, 0x05}, 2059746b117SMartina Krasteva {0x375d, 0x00}, 2069746b117SMartina Krasteva {0x375e, 0x00}, 2079746b117SMartina Krasteva {0x375f, 0x61}, 2089746b117SMartina Krasteva {0x3760, 0x06}, 2099746b117SMartina Krasteva {0x3768, 0x1b}, 2109746b117SMartina Krasteva {0x3769, 0x1b}, 2119746b117SMartina Krasteva {0x376a, 0x1a}, 2129746b117SMartina Krasteva {0x376b, 0x19}, 2139746b117SMartina Krasteva {0x376c, 0x18}, 2149746b117SMartina Krasteva {0x376d, 0x14}, 2159746b117SMartina Krasteva {0x376e, 0x0f}, 2169746b117SMartina Krasteva {0x3776, 0x00}, 2179746b117SMartina Krasteva {0x3777, 0x00}, 2189746b117SMartina Krasteva {0x3778, 0x46}, 2199746b117SMartina Krasteva {0x3779, 0x00}, 2209746b117SMartina Krasteva {0x377a, 0x08}, 2219746b117SMartina Krasteva {0x377b, 0x01}, 2229746b117SMartina Krasteva {0x377c, 0x45}, 2239746b117SMartina Krasteva {0x377d, 0x01}, 2249746b117SMartina Krasteva {0x377e, 0x23}, 2259746b117SMartina Krasteva {0x377f, 0x02}, 2269746b117SMartina Krasteva {0x3780, 0xd9}, 2279746b117SMartina Krasteva {0x3781, 0x03}, 2289746b117SMartina Krasteva {0x3782, 0xf5}, 2299746b117SMartina Krasteva {0x3783, 0x06}, 2309746b117SMartina Krasteva {0x3784, 0xa5}, 2319746b117SMartina Krasteva {0x3788, 0x0f}, 2329746b117SMartina Krasteva {0x378a, 0xd9}, 2339746b117SMartina Krasteva {0x378b, 0x03}, 2349746b117SMartina Krasteva {0x378c, 0xeb}, 2359746b117SMartina Krasteva {0x378d, 0x05}, 2369746b117SMartina Krasteva {0x378e, 0x87}, 2379746b117SMartina Krasteva {0x378f, 0x06}, 2389746b117SMartina Krasteva {0x3790, 0xf5}, 2399746b117SMartina Krasteva {0x3792, 0x43}, 2409746b117SMartina Krasteva {0x3794, 0x7a}, 2419746b117SMartina Krasteva {0x3796, 0xa1}, 2429746b117SMartina Krasteva {0x3e04, 0x0e}, 2439746b117SMartina Krasteva {0x3a00, 0x01}, 2449746b117SMartina Krasteva }; 2459746b117SMartina Krasteva 2469746b117SMartina Krasteva /* Supported sensor mode configurations */ 2479746b117SMartina Krasteva static const struct imx334_mode supported_mode = { 2489746b117SMartina Krasteva .width = 3840, 2499746b117SMartina Krasteva .height = 2160, 2509746b117SMartina Krasteva .hblank = 560, 2519746b117SMartina Krasteva .vblank = 2340, 2529746b117SMartina Krasteva .vblank_min = 90, 2539746b117SMartina Krasteva .vblank_max = 132840, 2549746b117SMartina Krasteva .pclk = 594000000, 2559746b117SMartina Krasteva .link_freq_idx = 0, 2569746b117SMartina Krasteva .code = MEDIA_BUS_FMT_SRGGB12_1X12, 2579746b117SMartina Krasteva .reg_list = { 2589746b117SMartina Krasteva .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs), 2599746b117SMartina Krasteva .regs = mode_3840x2160_regs, 2609746b117SMartina Krasteva }, 2619746b117SMartina Krasteva }; 2629746b117SMartina Krasteva 2639746b117SMartina Krasteva /** 2649746b117SMartina Krasteva * to_imx334() - imv334 V4L2 sub-device to imx334 device. 2659746b117SMartina Krasteva * @subdev: pointer to imx334 V4L2 sub-device 2669746b117SMartina Krasteva * 2679746b117SMartina Krasteva * Return: pointer to imx334 device 2689746b117SMartina Krasteva */ 2699746b117SMartina Krasteva static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev) 2709746b117SMartina Krasteva { 2719746b117SMartina Krasteva return container_of(subdev, struct imx334, sd); 2729746b117SMartina Krasteva } 2739746b117SMartina Krasteva 2749746b117SMartina Krasteva /** 2759746b117SMartina Krasteva * imx334_read_reg() - Read registers. 2769746b117SMartina Krasteva * @imx334: pointer to imx334 device 2779746b117SMartina Krasteva * @reg: register address 2789746b117SMartina Krasteva * @len: length of bytes to read. Max supported bytes is 4 2799746b117SMartina Krasteva * @val: pointer to register value to be filled. 2809746b117SMartina Krasteva * 2819746b117SMartina Krasteva * Big endian register addresses with little endian values. 2829746b117SMartina Krasteva * 2839746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 2849746b117SMartina Krasteva */ 2859746b117SMartina Krasteva static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val) 2869746b117SMartina Krasteva { 2879746b117SMartina Krasteva struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd); 2889746b117SMartina Krasteva struct i2c_msg msgs[2] = {0}; 2899746b117SMartina Krasteva u8 addr_buf[2] = {0}; 2909746b117SMartina Krasteva u8 data_buf[4] = {0}; 2919746b117SMartina Krasteva int ret; 2929746b117SMartina Krasteva 2939746b117SMartina Krasteva if (WARN_ON(len > 4)) 2949746b117SMartina Krasteva return -EINVAL; 2959746b117SMartina Krasteva 2969746b117SMartina Krasteva put_unaligned_be16(reg, addr_buf); 2979746b117SMartina Krasteva 2989746b117SMartina Krasteva /* Write register address */ 2999746b117SMartina Krasteva msgs[0].addr = client->addr; 3009746b117SMartina Krasteva msgs[0].flags = 0; 3019746b117SMartina Krasteva msgs[0].len = ARRAY_SIZE(addr_buf); 3029746b117SMartina Krasteva msgs[0].buf = addr_buf; 3039746b117SMartina Krasteva 3049746b117SMartina Krasteva /* Read data from register */ 3059746b117SMartina Krasteva msgs[1].addr = client->addr; 3069746b117SMartina Krasteva msgs[1].flags = I2C_M_RD; 3079746b117SMartina Krasteva msgs[1].len = len; 3089746b117SMartina Krasteva msgs[1].buf = data_buf; 3099746b117SMartina Krasteva 3109746b117SMartina Krasteva ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 3119746b117SMartina Krasteva if (ret != ARRAY_SIZE(msgs)) 3129746b117SMartina Krasteva return -EIO; 3139746b117SMartina Krasteva 3149746b117SMartina Krasteva *val = get_unaligned_le32(data_buf); 3159746b117SMartina Krasteva 3169746b117SMartina Krasteva return 0; 3179746b117SMartina Krasteva } 3189746b117SMartina Krasteva 3199746b117SMartina Krasteva /** 3209746b117SMartina Krasteva * imx334_write_reg() - Write register 3219746b117SMartina Krasteva * @imx334: pointer to imx334 device 3229746b117SMartina Krasteva * @reg: register address 3239746b117SMartina Krasteva * @len: length of bytes. Max supported bytes is 4 3249746b117SMartina Krasteva * @val: register value 3259746b117SMartina Krasteva * 3269746b117SMartina Krasteva * Big endian register addresses with little endian values. 3279746b117SMartina Krasteva * 3289746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 3299746b117SMartina Krasteva */ 3309746b117SMartina Krasteva static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val) 3319746b117SMartina Krasteva { 3329746b117SMartina Krasteva struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd); 3339746b117SMartina Krasteva u8 buf[6] = {0}; 3349746b117SMartina Krasteva 3359746b117SMartina Krasteva if (WARN_ON(len > 4)) 3369746b117SMartina Krasteva return -EINVAL; 3379746b117SMartina Krasteva 3389746b117SMartina Krasteva put_unaligned_be16(reg, buf); 3399746b117SMartina Krasteva put_unaligned_le32(val, buf + 2); 3409746b117SMartina Krasteva if (i2c_master_send(client, buf, len + 2) != len + 2) 3419746b117SMartina Krasteva return -EIO; 3429746b117SMartina Krasteva 3439746b117SMartina Krasteva return 0; 3449746b117SMartina Krasteva } 3459746b117SMartina Krasteva 3469746b117SMartina Krasteva /** 3479746b117SMartina Krasteva * imx334_write_regs() - Write a list of registers 3489746b117SMartina Krasteva * @imx334: pointer to imx334 device 3499746b117SMartina Krasteva * @regs: list of registers to be written 3509746b117SMartina Krasteva * @len: length of registers array 3519746b117SMartina Krasteva * 3529746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 3539746b117SMartina Krasteva */ 3549746b117SMartina Krasteva static int imx334_write_regs(struct imx334 *imx334, 3559746b117SMartina Krasteva const struct imx334_reg *regs, u32 len) 3569746b117SMartina Krasteva { 3579746b117SMartina Krasteva unsigned int i; 3589746b117SMartina Krasteva int ret; 3599746b117SMartina Krasteva 3609746b117SMartina Krasteva for (i = 0; i < len; i++) { 3619746b117SMartina Krasteva ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val); 3629746b117SMartina Krasteva if (ret) 3639746b117SMartina Krasteva return ret; 3649746b117SMartina Krasteva } 3659746b117SMartina Krasteva 3669746b117SMartina Krasteva return 0; 3679746b117SMartina Krasteva } 3689746b117SMartina Krasteva 3699746b117SMartina Krasteva /** 3709746b117SMartina Krasteva * imx334_update_controls() - Update control ranges based on streaming mode 3719746b117SMartina Krasteva * @imx334: pointer to imx334 device 3729746b117SMartina Krasteva * @mode: pointer to imx334_mode sensor mode 3739746b117SMartina Krasteva * 3749746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 3759746b117SMartina Krasteva */ 3769746b117SMartina Krasteva static int imx334_update_controls(struct imx334 *imx334, 3779746b117SMartina Krasteva const struct imx334_mode *mode) 3789746b117SMartina Krasteva { 3799746b117SMartina Krasteva int ret; 3809746b117SMartina Krasteva 3819746b117SMartina Krasteva ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx); 3829746b117SMartina Krasteva if (ret) 3839746b117SMartina Krasteva return ret; 3849746b117SMartina Krasteva 3859746b117SMartina Krasteva ret = __v4l2_ctrl_s_ctrl(imx334->hblank_ctrl, mode->hblank); 3869746b117SMartina Krasteva if (ret) 3879746b117SMartina Krasteva return ret; 3889746b117SMartina Krasteva 3899746b117SMartina Krasteva return __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min, 3909746b117SMartina Krasteva mode->vblank_max, 1, mode->vblank); 3919746b117SMartina Krasteva } 3929746b117SMartina Krasteva 3939746b117SMartina Krasteva /** 3949746b117SMartina Krasteva * imx334_update_exp_gain() - Set updated exposure and gain 3959746b117SMartina Krasteva * @imx334: pointer to imx334 device 3969746b117SMartina Krasteva * @exposure: updated exposure value 3979746b117SMartina Krasteva * @gain: updated analog gain value 3989746b117SMartina Krasteva * 3999746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 4009746b117SMartina Krasteva */ 4019746b117SMartina Krasteva static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain) 4029746b117SMartina Krasteva { 4039746b117SMartina Krasteva u32 lpfr, shutter; 4049746b117SMartina Krasteva int ret; 4059746b117SMartina Krasteva 4069746b117SMartina Krasteva lpfr = imx334->vblank + imx334->cur_mode->height; 4079746b117SMartina Krasteva shutter = lpfr - exposure; 4089746b117SMartina Krasteva 4099746b117SMartina Krasteva dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u", 4109746b117SMartina Krasteva exposure, gain, shutter, lpfr); 4119746b117SMartina Krasteva 4129746b117SMartina Krasteva ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1); 4139746b117SMartina Krasteva if (ret) 4149746b117SMartina Krasteva return ret; 4159746b117SMartina Krasteva 4169746b117SMartina Krasteva ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr); 4179746b117SMartina Krasteva if (ret) 4189746b117SMartina Krasteva goto error_release_group_hold; 4199746b117SMartina Krasteva 4209746b117SMartina Krasteva ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter); 4219746b117SMartina Krasteva if (ret) 4229746b117SMartina Krasteva goto error_release_group_hold; 4239746b117SMartina Krasteva 4249746b117SMartina Krasteva ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain); 4259746b117SMartina Krasteva 4269746b117SMartina Krasteva error_release_group_hold: 4279746b117SMartina Krasteva imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0); 4289746b117SMartina Krasteva 4299746b117SMartina Krasteva return ret; 4309746b117SMartina Krasteva } 4319746b117SMartina Krasteva 4329746b117SMartina Krasteva /** 4339746b117SMartina Krasteva * imx334_set_ctrl() - Set subdevice control 4349746b117SMartina Krasteva * @ctrl: pointer to v4l2_ctrl structure 4359746b117SMartina Krasteva * 4369746b117SMartina Krasteva * Supported controls: 4379746b117SMartina Krasteva * - V4L2_CID_VBLANK 4389746b117SMartina Krasteva * - cluster controls: 4399746b117SMartina Krasteva * - V4L2_CID_ANALOGUE_GAIN 4409746b117SMartina Krasteva * - V4L2_CID_EXPOSURE 4419746b117SMartina Krasteva * 4429746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 4439746b117SMartina Krasteva */ 4449746b117SMartina Krasteva static int imx334_set_ctrl(struct v4l2_ctrl *ctrl) 4459746b117SMartina Krasteva { 4469746b117SMartina Krasteva struct imx334 *imx334 = 4479746b117SMartina Krasteva container_of(ctrl->handler, struct imx334, ctrl_handler); 4489746b117SMartina Krasteva u32 analog_gain; 4499746b117SMartina Krasteva u32 exposure; 4509746b117SMartina Krasteva int ret; 4519746b117SMartina Krasteva 4529746b117SMartina Krasteva switch (ctrl->id) { 4539746b117SMartina Krasteva case V4L2_CID_VBLANK: 4549746b117SMartina Krasteva imx334->vblank = imx334->vblank_ctrl->val; 4559746b117SMartina Krasteva 4569746b117SMartina Krasteva dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u", 4579746b117SMartina Krasteva imx334->vblank, 4589746b117SMartina Krasteva imx334->vblank + imx334->cur_mode->height); 4599746b117SMartina Krasteva 4609746b117SMartina Krasteva ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl, 4619746b117SMartina Krasteva IMX334_EXPOSURE_MIN, 4629746b117SMartina Krasteva imx334->vblank + 4639746b117SMartina Krasteva imx334->cur_mode->height - 4649746b117SMartina Krasteva IMX334_EXPOSURE_OFFSET, 4659746b117SMartina Krasteva 1, IMX334_EXPOSURE_DEFAULT); 4669746b117SMartina Krasteva break; 4679746b117SMartina Krasteva case V4L2_CID_EXPOSURE: 4689746b117SMartina Krasteva 4699746b117SMartina Krasteva /* Set controls only if sensor is in power on state */ 4709746b117SMartina Krasteva if (!pm_runtime_get_if_in_use(imx334->dev)) 4719746b117SMartina Krasteva return 0; 4729746b117SMartina Krasteva 4739746b117SMartina Krasteva exposure = ctrl->val; 4749746b117SMartina Krasteva analog_gain = imx334->again_ctrl->val; 4759746b117SMartina Krasteva 4769746b117SMartina Krasteva dev_dbg(imx334->dev, "Received exp %u analog gain %u", 4779746b117SMartina Krasteva exposure, analog_gain); 4789746b117SMartina Krasteva 4799746b117SMartina Krasteva ret = imx334_update_exp_gain(imx334, exposure, analog_gain); 4809746b117SMartina Krasteva 4819746b117SMartina Krasteva pm_runtime_put(imx334->dev); 4829746b117SMartina Krasteva 4839746b117SMartina Krasteva break; 4849746b117SMartina Krasteva default: 4859746b117SMartina Krasteva dev_err(imx334->dev, "Invalid control %d", ctrl->id); 4869746b117SMartina Krasteva ret = -EINVAL; 4879746b117SMartina Krasteva } 4889746b117SMartina Krasteva 4899746b117SMartina Krasteva return ret; 4909746b117SMartina Krasteva } 4919746b117SMartina Krasteva 4929746b117SMartina Krasteva /* V4l2 subdevice control ops*/ 4939746b117SMartina Krasteva static const struct v4l2_ctrl_ops imx334_ctrl_ops = { 4949746b117SMartina Krasteva .s_ctrl = imx334_set_ctrl, 4959746b117SMartina Krasteva }; 4969746b117SMartina Krasteva 4979746b117SMartina Krasteva /** 4989746b117SMartina Krasteva * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes 4999746b117SMartina Krasteva * @sd: pointer to imx334 V4L2 sub-device structure 5009746b117SMartina Krasteva * @cfg: V4L2 sub-device pad configuration 5019746b117SMartina Krasteva * @code: V4L2 sub-device code enumeration need to be filled 5029746b117SMartina Krasteva * 5039746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 5049746b117SMartina Krasteva */ 5059746b117SMartina Krasteva static int imx334_enum_mbus_code(struct v4l2_subdev *sd, 5069746b117SMartina Krasteva struct v4l2_subdev_pad_config *cfg, 5079746b117SMartina Krasteva struct v4l2_subdev_mbus_code_enum *code) 5089746b117SMartina Krasteva { 5099746b117SMartina Krasteva if (code->index > 0) 5109746b117SMartina Krasteva return -EINVAL; 5119746b117SMartina Krasteva 5129746b117SMartina Krasteva code->code = supported_mode.code; 5139746b117SMartina Krasteva 5149746b117SMartina Krasteva return 0; 5159746b117SMartina Krasteva } 5169746b117SMartina Krasteva 5179746b117SMartina Krasteva /** 5189746b117SMartina Krasteva * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes 5199746b117SMartina Krasteva * @sd: pointer to imx334 V4L2 sub-device structure 5209746b117SMartina Krasteva * @cfg: V4L2 sub-device pad configuration 5219746b117SMartina Krasteva * @fsize: V4L2 sub-device size enumeration need to be filled 5229746b117SMartina Krasteva * 5239746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 5249746b117SMartina Krasteva */ 5259746b117SMartina Krasteva static int imx334_enum_frame_size(struct v4l2_subdev *sd, 5269746b117SMartina Krasteva struct v4l2_subdev_pad_config *cfg, 5279746b117SMartina Krasteva struct v4l2_subdev_frame_size_enum *fsize) 5289746b117SMartina Krasteva { 5299746b117SMartina Krasteva if (fsize->index > 0) 5309746b117SMartina Krasteva return -EINVAL; 5319746b117SMartina Krasteva 5329746b117SMartina Krasteva if (fsize->code != supported_mode.code) 5339746b117SMartina Krasteva return -EINVAL; 5349746b117SMartina Krasteva 5359746b117SMartina Krasteva fsize->min_width = supported_mode.width; 5369746b117SMartina Krasteva fsize->max_width = fsize->min_width; 5379746b117SMartina Krasteva fsize->min_height = supported_mode.height; 5389746b117SMartina Krasteva fsize->max_height = fsize->min_height; 5399746b117SMartina Krasteva 5409746b117SMartina Krasteva return 0; 5419746b117SMartina Krasteva } 5429746b117SMartina Krasteva 5439746b117SMartina Krasteva /** 5449746b117SMartina Krasteva * imx334_fill_pad_format() - Fill subdevice pad format 5459746b117SMartina Krasteva * from selected sensor mode 5469746b117SMartina Krasteva * @imx334: pointer to imx334 device 5479746b117SMartina Krasteva * @mode: pointer to imx334_mode sensor mode 5489746b117SMartina Krasteva * @fmt: V4L2 sub-device format need to be filled 5499746b117SMartina Krasteva */ 5509746b117SMartina Krasteva static void imx334_fill_pad_format(struct imx334 *imx334, 5519746b117SMartina Krasteva const struct imx334_mode *mode, 5529746b117SMartina Krasteva struct v4l2_subdev_format *fmt) 5539746b117SMartina Krasteva { 5549746b117SMartina Krasteva fmt->format.width = mode->width; 5559746b117SMartina Krasteva fmt->format.height = mode->height; 5569746b117SMartina Krasteva fmt->format.code = mode->code; 5579746b117SMartina Krasteva fmt->format.field = V4L2_FIELD_NONE; 5589746b117SMartina Krasteva fmt->format.colorspace = V4L2_COLORSPACE_RAW; 5599746b117SMartina Krasteva fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 5609746b117SMartina Krasteva fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; 5619746b117SMartina Krasteva fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; 5629746b117SMartina Krasteva } 5639746b117SMartina Krasteva 5649746b117SMartina Krasteva /** 5659746b117SMartina Krasteva * imx334_get_pad_format() - Get subdevice pad format 5669746b117SMartina Krasteva * @sd: pointer to imx334 V4L2 sub-device structure 5679746b117SMartina Krasteva * @cfg: V4L2 sub-device pad configuration 5689746b117SMartina Krasteva * @fmt: V4L2 sub-device format need to be set 5699746b117SMartina Krasteva * 5709746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 5719746b117SMartina Krasteva */ 5729746b117SMartina Krasteva static int imx334_get_pad_format(struct v4l2_subdev *sd, 5739746b117SMartina Krasteva struct v4l2_subdev_pad_config *cfg, 5749746b117SMartina Krasteva struct v4l2_subdev_format *fmt) 5759746b117SMartina Krasteva { 5769746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 5779746b117SMartina Krasteva 5789746b117SMartina Krasteva mutex_lock(&imx334->mutex); 5799746b117SMartina Krasteva 5809746b117SMartina Krasteva if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 5819746b117SMartina Krasteva struct v4l2_mbus_framefmt *framefmt; 5829746b117SMartina Krasteva 5839746b117SMartina Krasteva framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); 5849746b117SMartina Krasteva fmt->format = *framefmt; 5859746b117SMartina Krasteva } else { 5869746b117SMartina Krasteva imx334_fill_pad_format(imx334, imx334->cur_mode, fmt); 5879746b117SMartina Krasteva } 5889746b117SMartina Krasteva 5899746b117SMartina Krasteva mutex_unlock(&imx334->mutex); 5909746b117SMartina Krasteva 5919746b117SMartina Krasteva return 0; 5929746b117SMartina Krasteva } 5939746b117SMartina Krasteva 5949746b117SMartina Krasteva /** 5959746b117SMartina Krasteva * imx334_set_pad_format() - Set subdevice pad format 5969746b117SMartina Krasteva * @sd: pointer to imx334 V4L2 sub-device structure 5979746b117SMartina Krasteva * @cfg: V4L2 sub-device pad configuration 5989746b117SMartina Krasteva * @fmt: V4L2 sub-device format need to be set 5999746b117SMartina Krasteva * 6009746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 6019746b117SMartina Krasteva */ 6029746b117SMartina Krasteva static int imx334_set_pad_format(struct v4l2_subdev *sd, 6039746b117SMartina Krasteva struct v4l2_subdev_pad_config *cfg, 6049746b117SMartina Krasteva struct v4l2_subdev_format *fmt) 6059746b117SMartina Krasteva { 6069746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 6079746b117SMartina Krasteva const struct imx334_mode *mode; 6089746b117SMartina Krasteva int ret = 0; 6099746b117SMartina Krasteva 6109746b117SMartina Krasteva mutex_lock(&imx334->mutex); 6119746b117SMartina Krasteva 6129746b117SMartina Krasteva mode = &supported_mode; 6139746b117SMartina Krasteva imx334_fill_pad_format(imx334, mode, fmt); 6149746b117SMartina Krasteva 6159746b117SMartina Krasteva if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 6169746b117SMartina Krasteva struct v4l2_mbus_framefmt *framefmt; 6179746b117SMartina Krasteva 6189746b117SMartina Krasteva framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); 6199746b117SMartina Krasteva *framefmt = fmt->format; 6209746b117SMartina Krasteva } else { 6219746b117SMartina Krasteva ret = imx334_update_controls(imx334, mode); 6229746b117SMartina Krasteva if (!ret) 6239746b117SMartina Krasteva imx334->cur_mode = mode; 6249746b117SMartina Krasteva } 6259746b117SMartina Krasteva 6269746b117SMartina Krasteva mutex_unlock(&imx334->mutex); 6279746b117SMartina Krasteva 6289746b117SMartina Krasteva return ret; 6299746b117SMartina Krasteva } 6309746b117SMartina Krasteva 6319746b117SMartina Krasteva /** 6329746b117SMartina Krasteva * imx334_init_pad_cfg() - Initialize sub-device pad configuration 6339746b117SMartina Krasteva * @sd: pointer to imx334 V4L2 sub-device structure 6349746b117SMartina Krasteva * @cfg: V4L2 sub-device pad configuration 6359746b117SMartina Krasteva * 6369746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 6379746b117SMartina Krasteva */ 6389746b117SMartina Krasteva static int imx334_init_pad_cfg(struct v4l2_subdev *sd, 6399746b117SMartina Krasteva struct v4l2_subdev_pad_config *cfg) 6409746b117SMartina Krasteva { 6419746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 6429746b117SMartina Krasteva struct v4l2_subdev_format fmt = { 0 }; 6439746b117SMartina Krasteva 6449746b117SMartina Krasteva fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 6459746b117SMartina Krasteva imx334_fill_pad_format(imx334, &supported_mode, &fmt); 6469746b117SMartina Krasteva 6479746b117SMartina Krasteva return imx334_set_pad_format(sd, cfg, &fmt); 6489746b117SMartina Krasteva } 6499746b117SMartina Krasteva 6509746b117SMartina Krasteva /** 6519746b117SMartina Krasteva * imx334_start_streaming() - Start sensor stream 6529746b117SMartina Krasteva * @imx334: pointer to imx334 device 6539746b117SMartina Krasteva * 6549746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 6559746b117SMartina Krasteva */ 6569746b117SMartina Krasteva static int imx334_start_streaming(struct imx334 *imx334) 6579746b117SMartina Krasteva { 6589746b117SMartina Krasteva const struct imx334_reg_list *reg_list; 6599746b117SMartina Krasteva int ret; 6609746b117SMartina Krasteva 6619746b117SMartina Krasteva /* Write sensor mode registers */ 6629746b117SMartina Krasteva reg_list = &imx334->cur_mode->reg_list; 6639746b117SMartina Krasteva ret = imx334_write_regs(imx334, reg_list->regs, 6649746b117SMartina Krasteva reg_list->num_of_regs); 6659746b117SMartina Krasteva if (ret) { 6669746b117SMartina Krasteva dev_err(imx334->dev, "fail to write initial registers"); 6679746b117SMartina Krasteva return ret; 6689746b117SMartina Krasteva } 6699746b117SMartina Krasteva 6709746b117SMartina Krasteva /* Setup handler will write actual exposure and gain */ 6719746b117SMartina Krasteva ret = __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler); 6729746b117SMartina Krasteva if (ret) { 6739746b117SMartina Krasteva dev_err(imx334->dev, "fail to setup handler"); 6749746b117SMartina Krasteva return ret; 6759746b117SMartina Krasteva } 6769746b117SMartina Krasteva 6779746b117SMartina Krasteva /* Start streaming */ 6789746b117SMartina Krasteva ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT, 6799746b117SMartina Krasteva 1, IMX334_MODE_STREAMING); 6809746b117SMartina Krasteva if (ret) { 6819746b117SMartina Krasteva dev_err(imx334->dev, "fail to start streaming"); 6829746b117SMartina Krasteva return ret; 6839746b117SMartina Krasteva } 6849746b117SMartina Krasteva 6859746b117SMartina Krasteva return 0; 6869746b117SMartina Krasteva } 6879746b117SMartina Krasteva 6889746b117SMartina Krasteva /** 6899746b117SMartina Krasteva * imx334_stop_streaming() - Stop sensor stream 6909746b117SMartina Krasteva * @imx334: pointer to imx334 device 6919746b117SMartina Krasteva * 6929746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 6939746b117SMartina Krasteva */ 6949746b117SMartina Krasteva static int imx334_stop_streaming(struct imx334 *imx334) 6959746b117SMartina Krasteva { 6969746b117SMartina Krasteva return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT, 6979746b117SMartina Krasteva 1, IMX334_MODE_STANDBY); 6989746b117SMartina Krasteva } 6999746b117SMartina Krasteva 7009746b117SMartina Krasteva /** 7019746b117SMartina Krasteva * imx334_set_stream() - Enable sensor streaming 7029746b117SMartina Krasteva * @sd: pointer to imx334 subdevice 7039746b117SMartina Krasteva * @enable: set to enable sensor streaming 7049746b117SMartina Krasteva * 7059746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 7069746b117SMartina Krasteva */ 7079746b117SMartina Krasteva static int imx334_set_stream(struct v4l2_subdev *sd, int enable) 7089746b117SMartina Krasteva { 7099746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 7109746b117SMartina Krasteva int ret; 7119746b117SMartina Krasteva 7129746b117SMartina Krasteva mutex_lock(&imx334->mutex); 7139746b117SMartina Krasteva 7149746b117SMartina Krasteva if (imx334->streaming == enable) { 7159746b117SMartina Krasteva mutex_unlock(&imx334->mutex); 7169746b117SMartina Krasteva return 0; 7179746b117SMartina Krasteva } 7189746b117SMartina Krasteva 7199746b117SMartina Krasteva if (enable) { 720*62c90446SMauro Carvalho Chehab ret = pm_runtime_resume_and_get(imx334->dev); 721*62c90446SMauro Carvalho Chehab if (ret < 0) 722*62c90446SMauro Carvalho Chehab goto error_unlock; 7239746b117SMartina Krasteva 7249746b117SMartina Krasteva ret = imx334_start_streaming(imx334); 7259746b117SMartina Krasteva if (ret) 7269746b117SMartina Krasteva goto error_power_off; 7279746b117SMartina Krasteva } else { 7289746b117SMartina Krasteva imx334_stop_streaming(imx334); 7299746b117SMartina Krasteva pm_runtime_put(imx334->dev); 7309746b117SMartina Krasteva } 7319746b117SMartina Krasteva 7329746b117SMartina Krasteva imx334->streaming = enable; 7339746b117SMartina Krasteva 7349746b117SMartina Krasteva mutex_unlock(&imx334->mutex); 7359746b117SMartina Krasteva 7369746b117SMartina Krasteva return 0; 7379746b117SMartina Krasteva 7389746b117SMartina Krasteva error_power_off: 7399746b117SMartina Krasteva pm_runtime_put(imx334->dev); 740*62c90446SMauro Carvalho Chehab error_unlock: 7419746b117SMartina Krasteva mutex_unlock(&imx334->mutex); 7429746b117SMartina Krasteva 7439746b117SMartina Krasteva return ret; 7449746b117SMartina Krasteva } 7459746b117SMartina Krasteva 7469746b117SMartina Krasteva /** 7479746b117SMartina Krasteva * imx334_detect() - Detect imx334 sensor 7489746b117SMartina Krasteva * @imx334: pointer to imx334 device 7499746b117SMartina Krasteva * 7509746b117SMartina Krasteva * Return: 0 if successful, -EIO if sensor id does not match 7519746b117SMartina Krasteva */ 7529746b117SMartina Krasteva static int imx334_detect(struct imx334 *imx334) 7539746b117SMartina Krasteva { 7549746b117SMartina Krasteva int ret; 7559746b117SMartina Krasteva u32 val; 7569746b117SMartina Krasteva 7579746b117SMartina Krasteva ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val); 7589746b117SMartina Krasteva if (ret) 7599746b117SMartina Krasteva return ret; 7609746b117SMartina Krasteva 7619746b117SMartina Krasteva if (val != IMX334_ID) { 7629746b117SMartina Krasteva dev_err(imx334->dev, "chip id mismatch: %x!=%x", 7639746b117SMartina Krasteva IMX334_ID, val); 7649746b117SMartina Krasteva return -ENXIO; 7659746b117SMartina Krasteva } 7669746b117SMartina Krasteva 7679746b117SMartina Krasteva return 0; 7689746b117SMartina Krasteva } 7699746b117SMartina Krasteva 7709746b117SMartina Krasteva /** 7719746b117SMartina Krasteva * imx334_parse_hw_config() - Parse HW configuration and check if supported 7729746b117SMartina Krasteva * @imx334: pointer to imx334 device 7739746b117SMartina Krasteva * 7749746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 7759746b117SMartina Krasteva */ 7769746b117SMartina Krasteva static int imx334_parse_hw_config(struct imx334 *imx334) 7779746b117SMartina Krasteva { 7789746b117SMartina Krasteva struct fwnode_handle *fwnode = dev_fwnode(imx334->dev); 7799746b117SMartina Krasteva struct v4l2_fwnode_endpoint bus_cfg = { 7809746b117SMartina Krasteva .bus_type = V4L2_MBUS_CSI2_DPHY 7819746b117SMartina Krasteva }; 7829746b117SMartina Krasteva struct fwnode_handle *ep; 7839746b117SMartina Krasteva unsigned long rate; 7849746b117SMartina Krasteva int ret; 7859746b117SMartina Krasteva int i; 7869746b117SMartina Krasteva 7879746b117SMartina Krasteva if (!fwnode) 7889746b117SMartina Krasteva return -ENXIO; 7899746b117SMartina Krasteva 7909746b117SMartina Krasteva /* Request optional reset pin */ 7919746b117SMartina Krasteva imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset", 7929746b117SMartina Krasteva GPIOD_OUT_LOW); 7939746b117SMartina Krasteva if (IS_ERR(imx334->reset_gpio)) { 794c702e2f7SHans Verkuil dev_err(imx334->dev, "failed to get reset gpio %ld", 795c702e2f7SHans Verkuil PTR_ERR(imx334->reset_gpio)); 7969746b117SMartina Krasteva return PTR_ERR(imx334->reset_gpio); 7979746b117SMartina Krasteva } 7989746b117SMartina Krasteva 7999746b117SMartina Krasteva /* Get sensor input clock */ 8009746b117SMartina Krasteva imx334->inclk = devm_clk_get(imx334->dev, NULL); 8019746b117SMartina Krasteva if (IS_ERR(imx334->inclk)) { 8029746b117SMartina Krasteva dev_err(imx334->dev, "could not get inclk"); 8039746b117SMartina Krasteva return PTR_ERR(imx334->inclk); 8049746b117SMartina Krasteva } 8059746b117SMartina Krasteva 8069746b117SMartina Krasteva rate = clk_get_rate(imx334->inclk); 8079746b117SMartina Krasteva if (rate != IMX334_INCLK_RATE) { 8089746b117SMartina Krasteva dev_err(imx334->dev, "inclk frequency mismatch"); 8099746b117SMartina Krasteva return -EINVAL; 8109746b117SMartina Krasteva } 8119746b117SMartina Krasteva 8129746b117SMartina Krasteva ep = fwnode_graph_get_next_endpoint(fwnode, NULL); 8139746b117SMartina Krasteva if (!ep) 8149746b117SMartina Krasteva return -ENXIO; 8159746b117SMartina Krasteva 8169746b117SMartina Krasteva ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); 8179746b117SMartina Krasteva fwnode_handle_put(ep); 8189746b117SMartina Krasteva if (ret) 8199746b117SMartina Krasteva return ret; 8209746b117SMartina Krasteva 8219746b117SMartina Krasteva if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) { 8229746b117SMartina Krasteva dev_err(imx334->dev, 8239746b117SMartina Krasteva "number of CSI2 data lanes %d is not supported", 8249746b117SMartina Krasteva bus_cfg.bus.mipi_csi2.num_data_lanes); 8259746b117SMartina Krasteva ret = -EINVAL; 8269746b117SMartina Krasteva goto done_endpoint_free; 8279746b117SMartina Krasteva } 8289746b117SMartina Krasteva 8299746b117SMartina Krasteva if (!bus_cfg.nr_of_link_frequencies) { 8309746b117SMartina Krasteva dev_err(imx334->dev, "no link frequencies defined"); 8319746b117SMartina Krasteva ret = -EINVAL; 8329746b117SMartina Krasteva goto done_endpoint_free; 8339746b117SMartina Krasteva } 8349746b117SMartina Krasteva 8359746b117SMartina Krasteva for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) 8369746b117SMartina Krasteva if (bus_cfg.link_frequencies[i] == IMX334_LINK_FREQ) 8379746b117SMartina Krasteva goto done_endpoint_free; 8389746b117SMartina Krasteva 8399746b117SMartina Krasteva ret = -EINVAL; 8409746b117SMartina Krasteva 8419746b117SMartina Krasteva done_endpoint_free: 8429746b117SMartina Krasteva v4l2_fwnode_endpoint_free(&bus_cfg); 8439746b117SMartina Krasteva 8449746b117SMartina Krasteva return ret; 8459746b117SMartina Krasteva } 8469746b117SMartina Krasteva 8479746b117SMartina Krasteva /* V4l2 subdevice ops */ 8489746b117SMartina Krasteva static const struct v4l2_subdev_video_ops imx334_video_ops = { 8499746b117SMartina Krasteva .s_stream = imx334_set_stream, 8509746b117SMartina Krasteva }; 8519746b117SMartina Krasteva 8529746b117SMartina Krasteva static const struct v4l2_subdev_pad_ops imx334_pad_ops = { 8539746b117SMartina Krasteva .init_cfg = imx334_init_pad_cfg, 8549746b117SMartina Krasteva .enum_mbus_code = imx334_enum_mbus_code, 8559746b117SMartina Krasteva .enum_frame_size = imx334_enum_frame_size, 8569746b117SMartina Krasteva .get_fmt = imx334_get_pad_format, 8579746b117SMartina Krasteva .set_fmt = imx334_set_pad_format, 8589746b117SMartina Krasteva }; 8599746b117SMartina Krasteva 8609746b117SMartina Krasteva static const struct v4l2_subdev_ops imx334_subdev_ops = { 8619746b117SMartina Krasteva .video = &imx334_video_ops, 8629746b117SMartina Krasteva .pad = &imx334_pad_ops, 8639746b117SMartina Krasteva }; 8649746b117SMartina Krasteva 8659746b117SMartina Krasteva /** 8669746b117SMartina Krasteva * imx334_power_on() - Sensor power on sequence 8679746b117SMartina Krasteva * @dev: pointer to i2c device 8689746b117SMartina Krasteva * 8699746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 8709746b117SMartina Krasteva */ 8719746b117SMartina Krasteva static int imx334_power_on(struct device *dev) 8729746b117SMartina Krasteva { 8739746b117SMartina Krasteva struct v4l2_subdev *sd = dev_get_drvdata(dev); 8749746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 8759746b117SMartina Krasteva int ret; 8769746b117SMartina Krasteva 8779746b117SMartina Krasteva gpiod_set_value_cansleep(imx334->reset_gpio, 1); 8789746b117SMartina Krasteva 8799746b117SMartina Krasteva ret = clk_prepare_enable(imx334->inclk); 8809746b117SMartina Krasteva if (ret) { 8819746b117SMartina Krasteva dev_err(imx334->dev, "fail to enable inclk"); 8829746b117SMartina Krasteva goto error_reset; 8839746b117SMartina Krasteva } 8849746b117SMartina Krasteva 8859746b117SMartina Krasteva usleep_range(18000, 20000); 8869746b117SMartina Krasteva 8879746b117SMartina Krasteva return 0; 8889746b117SMartina Krasteva 8899746b117SMartina Krasteva error_reset: 8909746b117SMartina Krasteva gpiod_set_value_cansleep(imx334->reset_gpio, 0); 8919746b117SMartina Krasteva 8929746b117SMartina Krasteva return ret; 8939746b117SMartina Krasteva } 8949746b117SMartina Krasteva 8959746b117SMartina Krasteva /** 8969746b117SMartina Krasteva * imx334_power_off() - Sensor power off sequence 8979746b117SMartina Krasteva * @dev: pointer to i2c device 8989746b117SMartina Krasteva * 8999746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 9009746b117SMartina Krasteva */ 9019746b117SMartina Krasteva static int imx334_power_off(struct device *dev) 9029746b117SMartina Krasteva { 9039746b117SMartina Krasteva struct v4l2_subdev *sd = dev_get_drvdata(dev); 9049746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 9059746b117SMartina Krasteva 9069746b117SMartina Krasteva gpiod_set_value_cansleep(imx334->reset_gpio, 0); 9079746b117SMartina Krasteva 9089746b117SMartina Krasteva clk_disable_unprepare(imx334->inclk); 9099746b117SMartina Krasteva 9109746b117SMartina Krasteva return 0; 9119746b117SMartina Krasteva } 9129746b117SMartina Krasteva 9139746b117SMartina Krasteva /** 9149746b117SMartina Krasteva * imx334_init_controls() - Initialize sensor subdevice controls 9159746b117SMartina Krasteva * @imx334: pointer to imx334 device 9169746b117SMartina Krasteva * 9179746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 9189746b117SMartina Krasteva */ 9199746b117SMartina Krasteva static int imx334_init_controls(struct imx334 *imx334) 9209746b117SMartina Krasteva { 9219746b117SMartina Krasteva struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler; 9229746b117SMartina Krasteva const struct imx334_mode *mode = imx334->cur_mode; 9239746b117SMartina Krasteva u32 lpfr; 9249746b117SMartina Krasteva int ret; 9259746b117SMartina Krasteva 9269746b117SMartina Krasteva ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6); 9279746b117SMartina Krasteva if (ret) 9289746b117SMartina Krasteva return ret; 9299746b117SMartina Krasteva 9309746b117SMartina Krasteva /* Serialize controls with sensor device */ 9319746b117SMartina Krasteva ctrl_hdlr->lock = &imx334->mutex; 9329746b117SMartina Krasteva 9339746b117SMartina Krasteva /* Initialize exposure and gain */ 9349746b117SMartina Krasteva lpfr = mode->vblank + mode->height; 9359746b117SMartina Krasteva imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 9369746b117SMartina Krasteva &imx334_ctrl_ops, 9379746b117SMartina Krasteva V4L2_CID_EXPOSURE, 9389746b117SMartina Krasteva IMX334_EXPOSURE_MIN, 9399746b117SMartina Krasteva lpfr - IMX334_EXPOSURE_OFFSET, 9409746b117SMartina Krasteva IMX334_EXPOSURE_STEP, 9419746b117SMartina Krasteva IMX334_EXPOSURE_DEFAULT); 9429746b117SMartina Krasteva 9439746b117SMartina Krasteva imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 9449746b117SMartina Krasteva &imx334_ctrl_ops, 9459746b117SMartina Krasteva V4L2_CID_ANALOGUE_GAIN, 9469746b117SMartina Krasteva IMX334_AGAIN_MIN, 9479746b117SMartina Krasteva IMX334_AGAIN_MAX, 9489746b117SMartina Krasteva IMX334_AGAIN_STEP, 9499746b117SMartina Krasteva IMX334_AGAIN_DEFAULT); 9509746b117SMartina Krasteva 9519746b117SMartina Krasteva v4l2_ctrl_cluster(2, &imx334->exp_ctrl); 9529746b117SMartina Krasteva 9539746b117SMartina Krasteva imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 9549746b117SMartina Krasteva &imx334_ctrl_ops, 9559746b117SMartina Krasteva V4L2_CID_VBLANK, 9569746b117SMartina Krasteva mode->vblank_min, 9579746b117SMartina Krasteva mode->vblank_max, 9589746b117SMartina Krasteva 1, mode->vblank); 9599746b117SMartina Krasteva 9609746b117SMartina Krasteva /* Read only controls */ 9619746b117SMartina Krasteva imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 9629746b117SMartina Krasteva &imx334_ctrl_ops, 9639746b117SMartina Krasteva V4L2_CID_PIXEL_RATE, 9649746b117SMartina Krasteva mode->pclk, mode->pclk, 9659746b117SMartina Krasteva 1, mode->pclk); 9669746b117SMartina Krasteva 9679746b117SMartina Krasteva imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, 9689746b117SMartina Krasteva &imx334_ctrl_ops, 9699746b117SMartina Krasteva V4L2_CID_LINK_FREQ, 9709746b117SMartina Krasteva ARRAY_SIZE(link_freq) - 9719746b117SMartina Krasteva 1, 9729746b117SMartina Krasteva mode->link_freq_idx, 9739746b117SMartina Krasteva link_freq); 9749746b117SMartina Krasteva if (imx334->link_freq_ctrl) 9759746b117SMartina Krasteva imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 9769746b117SMartina Krasteva 9779746b117SMartina Krasteva imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, 9789746b117SMartina Krasteva &imx334_ctrl_ops, 9799746b117SMartina Krasteva V4L2_CID_HBLANK, 9809746b117SMartina Krasteva IMX334_REG_MIN, 9819746b117SMartina Krasteva IMX334_REG_MAX, 9829746b117SMartina Krasteva 1, mode->hblank); 9839746b117SMartina Krasteva if (imx334->hblank_ctrl) 9849746b117SMartina Krasteva imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 9859746b117SMartina Krasteva 9869746b117SMartina Krasteva if (ctrl_hdlr->error) { 9879746b117SMartina Krasteva dev_err(imx334->dev, "control init failed: %d", 9889746b117SMartina Krasteva ctrl_hdlr->error); 9899746b117SMartina Krasteva v4l2_ctrl_handler_free(ctrl_hdlr); 9909746b117SMartina Krasteva return ctrl_hdlr->error; 9919746b117SMartina Krasteva } 9929746b117SMartina Krasteva 9939746b117SMartina Krasteva imx334->sd.ctrl_handler = ctrl_hdlr; 9949746b117SMartina Krasteva 9959746b117SMartina Krasteva return 0; 9969746b117SMartina Krasteva } 9979746b117SMartina Krasteva 9989746b117SMartina Krasteva /** 9999746b117SMartina Krasteva * imx334_probe() - I2C client device binding 10009746b117SMartina Krasteva * @client: pointer to i2c client device 10019746b117SMartina Krasteva * 10029746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 10039746b117SMartina Krasteva */ 10049746b117SMartina Krasteva static int imx334_probe(struct i2c_client *client) 10059746b117SMartina Krasteva { 10069746b117SMartina Krasteva struct imx334 *imx334; 10079746b117SMartina Krasteva int ret; 10089746b117SMartina Krasteva 10099746b117SMartina Krasteva imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL); 10109746b117SMartina Krasteva if (!imx334) 10119746b117SMartina Krasteva return -ENOMEM; 10129746b117SMartina Krasteva 10139746b117SMartina Krasteva imx334->dev = &client->dev; 10149746b117SMartina Krasteva 10159746b117SMartina Krasteva /* Initialize subdev */ 10169746b117SMartina Krasteva v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops); 10179746b117SMartina Krasteva 10189746b117SMartina Krasteva ret = imx334_parse_hw_config(imx334); 10199746b117SMartina Krasteva if (ret) { 10209746b117SMartina Krasteva dev_err(imx334->dev, "HW configuration is not supported"); 10219746b117SMartina Krasteva return ret; 10229746b117SMartina Krasteva } 10239746b117SMartina Krasteva 10249746b117SMartina Krasteva mutex_init(&imx334->mutex); 10259746b117SMartina Krasteva 10269746b117SMartina Krasteva ret = imx334_power_on(imx334->dev); 10279746b117SMartina Krasteva if (ret) { 10289746b117SMartina Krasteva dev_err(imx334->dev, "failed to power-on the sensor"); 10299746b117SMartina Krasteva goto error_mutex_destroy; 10309746b117SMartina Krasteva } 10319746b117SMartina Krasteva 10329746b117SMartina Krasteva /* Check module identity */ 10339746b117SMartina Krasteva ret = imx334_detect(imx334); 10349746b117SMartina Krasteva if (ret) { 10359746b117SMartina Krasteva dev_err(imx334->dev, "failed to find sensor: %d", ret); 10369746b117SMartina Krasteva goto error_power_off; 10379746b117SMartina Krasteva } 10389746b117SMartina Krasteva 10399746b117SMartina Krasteva /* Set default mode to max resolution */ 10409746b117SMartina Krasteva imx334->cur_mode = &supported_mode; 10419746b117SMartina Krasteva imx334->vblank = imx334->cur_mode->vblank; 10429746b117SMartina Krasteva 10439746b117SMartina Krasteva ret = imx334_init_controls(imx334); 10449746b117SMartina Krasteva if (ret) { 10459746b117SMartina Krasteva dev_err(imx334->dev, "failed to init controls: %d", ret); 10469746b117SMartina Krasteva goto error_power_off; 10479746b117SMartina Krasteva } 10489746b117SMartina Krasteva 10499746b117SMartina Krasteva /* Initialize subdev */ 10509746b117SMartina Krasteva imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 10519746b117SMartina Krasteva imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 10529746b117SMartina Krasteva 10539746b117SMartina Krasteva /* Initialize source pad */ 10549746b117SMartina Krasteva imx334->pad.flags = MEDIA_PAD_FL_SOURCE; 10559746b117SMartina Krasteva ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad); 10569746b117SMartina Krasteva if (ret) { 10579746b117SMartina Krasteva dev_err(imx334->dev, "failed to init entity pads: %d", ret); 10589746b117SMartina Krasteva goto error_handler_free; 10599746b117SMartina Krasteva } 10609746b117SMartina Krasteva 106115786f7bSSakari Ailus ret = v4l2_async_register_subdev_sensor(&imx334->sd); 10629746b117SMartina Krasteva if (ret < 0) { 10639746b117SMartina Krasteva dev_err(imx334->dev, 10649746b117SMartina Krasteva "failed to register async subdev: %d", ret); 10659746b117SMartina Krasteva goto error_media_entity; 10669746b117SMartina Krasteva } 10679746b117SMartina Krasteva 10689746b117SMartina Krasteva pm_runtime_set_active(imx334->dev); 10699746b117SMartina Krasteva pm_runtime_enable(imx334->dev); 10709746b117SMartina Krasteva pm_runtime_idle(imx334->dev); 10719746b117SMartina Krasteva 10729746b117SMartina Krasteva return 0; 10739746b117SMartina Krasteva 10749746b117SMartina Krasteva error_media_entity: 10759746b117SMartina Krasteva media_entity_cleanup(&imx334->sd.entity); 10769746b117SMartina Krasteva error_handler_free: 10779746b117SMartina Krasteva v4l2_ctrl_handler_free(imx334->sd.ctrl_handler); 10789746b117SMartina Krasteva error_power_off: 10799746b117SMartina Krasteva imx334_power_off(imx334->dev); 10809746b117SMartina Krasteva error_mutex_destroy: 10819746b117SMartina Krasteva mutex_destroy(&imx334->mutex); 10829746b117SMartina Krasteva 10839746b117SMartina Krasteva return ret; 10849746b117SMartina Krasteva } 10859746b117SMartina Krasteva 10869746b117SMartina Krasteva /** 10879746b117SMartina Krasteva * imx334_remove() - I2C client device unbinding 10889746b117SMartina Krasteva * @client: pointer to I2C client device 10899746b117SMartina Krasteva * 10909746b117SMartina Krasteva * Return: 0 if successful, error code otherwise. 10919746b117SMartina Krasteva */ 10929746b117SMartina Krasteva static int imx334_remove(struct i2c_client *client) 10939746b117SMartina Krasteva { 10949746b117SMartina Krasteva struct v4l2_subdev *sd = i2c_get_clientdata(client); 10959746b117SMartina Krasteva struct imx334 *imx334 = to_imx334(sd); 10969746b117SMartina Krasteva 10979746b117SMartina Krasteva v4l2_async_unregister_subdev(sd); 10989746b117SMartina Krasteva media_entity_cleanup(&sd->entity); 10999746b117SMartina Krasteva v4l2_ctrl_handler_free(sd->ctrl_handler); 11009746b117SMartina Krasteva 11019746b117SMartina Krasteva pm_runtime_disable(&client->dev); 11029746b117SMartina Krasteva pm_runtime_suspended(&client->dev); 11039746b117SMartina Krasteva 11049746b117SMartina Krasteva mutex_destroy(&imx334->mutex); 11059746b117SMartina Krasteva 11069746b117SMartina Krasteva return 0; 11079746b117SMartina Krasteva } 11089746b117SMartina Krasteva 11099746b117SMartina Krasteva static const struct dev_pm_ops imx334_pm_ops = { 11109746b117SMartina Krasteva SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL) 11119746b117SMartina Krasteva }; 11129746b117SMartina Krasteva 11139746b117SMartina Krasteva static const struct of_device_id imx334_of_match[] = { 11149746b117SMartina Krasteva { .compatible = "sony,imx334" }, 11159746b117SMartina Krasteva { } 11169746b117SMartina Krasteva }; 11179746b117SMartina Krasteva 11189746b117SMartina Krasteva MODULE_DEVICE_TABLE(of, imx334_of_match); 11199746b117SMartina Krasteva 11209746b117SMartina Krasteva static struct i2c_driver imx334_driver = { 11219746b117SMartina Krasteva .probe_new = imx334_probe, 11229746b117SMartina Krasteva .remove = imx334_remove, 11239746b117SMartina Krasteva .driver = { 11249746b117SMartina Krasteva .name = "imx334", 11259746b117SMartina Krasteva .pm = &imx334_pm_ops, 11269746b117SMartina Krasteva .of_match_table = imx334_of_match, 11279746b117SMartina Krasteva }, 11289746b117SMartina Krasteva }; 11299746b117SMartina Krasteva 11309746b117SMartina Krasteva module_i2c_driver(imx334_driver); 11319746b117SMartina Krasteva 11329746b117SMartina Krasteva MODULE_DESCRIPTION("Sony imx334 sensor driver"); 11339746b117SMartina Krasteva MODULE_LICENSE("GPL"); 1134