diff options
Diffstat (limited to 'drivers/media/i2c')
-rw-r--r-- | drivers/media/i2c/Kconfig | 12 | ||||
-rw-r--r-- | drivers/media/i2c/Makefile | 1 | ||||
-rw-r--r-- | drivers/media/i2c/adv7511-v4l2.c | 22 | ||||
-rw-r--r-- | drivers/media/i2c/adv7604.c | 18 | ||||
-rw-r--r-- | drivers/media/i2c/adv7842.c | 10 | ||||
-rw-r--r-- | drivers/media/i2c/ccs/ccs-core.c | 12 | ||||
-rw-r--r-- | drivers/media/i2c/cx25840/cx25840-ir.c | 20 | ||||
-rw-r--r-- | drivers/media/i2c/dw9768.c | 6 | ||||
-rw-r--r-- | drivers/media/i2c/hi556.c | 70 | ||||
-rw-r--r-- | drivers/media/i2c/hi846.c | 18 | ||||
-rw-r--r-- | drivers/media/i2c/imx208.c | 82 | ||||
-rw-r--r-- | drivers/media/i2c/imx274.c | 102 | ||||
-rw-r--r-- | drivers/media/i2c/imx290.c | 2 | ||||
-rw-r--r-- | drivers/media/i2c/imx319.c | 2 | ||||
-rw-r--r-- | drivers/media/i2c/imx355.c | 2 | ||||
-rw-r--r-- | drivers/media/i2c/max9286.c | 7 | ||||
-rw-r--r-- | drivers/media/i2c/ov13858.c | 2 | ||||
-rw-r--r-- | drivers/media/i2c/ov2740.c | 69 | ||||
-rw-r--r-- | drivers/media/i2c/ov5670.c | 80 | ||||
-rw-r--r-- | drivers/media/i2c/ov5675.c | 71 | ||||
-rw-r--r-- | drivers/media/i2c/ov5693.c | 1537 | ||||
-rw-r--r-- | drivers/media/i2c/ov8856.c | 162 | ||||
-rw-r--r-- | drivers/media/i2c/ov8865.c | 478 | ||||
-rw-r--r-- | drivers/media/i2c/s5c73m3/s5c73m3-spi.c | 6 |
24 files changed, 2330 insertions, 461 deletions
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index d6a5d4ca439a..69c56e24a612 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -469,6 +469,7 @@ config VIDEO_VPX3220 config VIDEO_MAX9286 tristate "Maxim MAX9286 GMSL deserializer support" depends on I2C && I2C_MUX + depends on VIDEO_V4L2 depends on OF_GPIO select V4L2_FWNODE select VIDEO_V4L2_SUBDEV_API @@ -1058,6 +1059,17 @@ config VIDEO_OV5675 To compile this driver as a module, choose M here: the module will be called ov5675. +config VIDEO_OV5693 + tristate "OmniVision OV5693 sensor support" + depends on I2C && VIDEO_V4L2 + select V4L2_FWNODE + help + This is a Video4Linux2 sensor driver for the OmniVision + OV5693 camera. + + To compile this driver as a module, choose M here: the + module will be called ov5693. + config VIDEO_OV5695 tristate "OmniVision OV5695 sensor support" depends on I2C && VIDEO_V4L2 diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 4d4fe08d7a6a..b01f6cd05ee8 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -75,6 +75,7 @@ obj-$(CONFIG_VIDEO_OV5647) += ov5647.o obj-$(CONFIG_VIDEO_OV5648) += ov5648.o obj-$(CONFIG_VIDEO_OV5670) += ov5670.o obj-$(CONFIG_VIDEO_OV5675) += ov5675.o +obj-$(CONFIG_VIDEO_OV5693) += ov5693.o obj-$(CONFIG_VIDEO_OV5695) += ov5695.o obj-$(CONFIG_VIDEO_OV6650) += ov6650.o obj-$(CONFIG_VIDEO_OV7251) += ov7251.o diff --git a/drivers/media/i2c/adv7511-v4l2.c b/drivers/media/i2c/adv7511-v4l2.c index 41f4e749a859..8e13cae40ec5 100644 --- a/drivers/media/i2c/adv7511-v4l2.c +++ b/drivers/media/i2c/adv7511-v4l2.c @@ -270,28 +270,6 @@ static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg) return adv_smbus_read_byte_data(state->i2c_pktmem, reg); } -static int adv7511_pktmem_wr(struct v4l2_subdev *sd, u8 reg, u8 val) -{ - struct adv7511_state *state = get_adv7511_state(sd); - int ret; - int i; - - for (i = 0; i < 3; i++) { - ret = i2c_smbus_write_byte_data(state->i2c_pktmem, reg, val); - if (ret == 0) - return 0; - } - v4l2_err(sd, "%s: i2c write error\n", __func__); - return ret; -} - -/* To set specific bits in the register, a clear-mask is given (to be AND-ed), - and then the value-mask (to be OR-ed). */ -static inline void adv7511_pktmem_wr_and_or(struct v4l2_subdev *sd, u8 reg, u8 clr_mask, u8 val_mask) -{ - adv7511_pktmem_wr(sd, reg, (adv7511_pktmem_rd(sd, reg) & clr_mask) | val_mask); -} - static inline bool adv7511_have_hotplug(struct v4l2_subdev *sd) { return adv7511_rd(sd, 0x42) & MASK_ADV7511_HPD_DETECT; diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c index 44768b59a6ff..a2fa408d2d9f 100644 --- a/drivers/media/i2c/adv7604.c +++ b/drivers/media/i2c/adv7604.c @@ -398,14 +398,14 @@ static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val); } -static inline int avlink_read(struct v4l2_subdev *sd, u8 reg) +static inline int __always_unused avlink_read(struct v4l2_subdev *sd, u8 reg) { struct adv76xx_state *state = to_state(sd); return adv76xx_read_check(state, ADV7604_PAGE_AVLINK, reg); } -static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val) +static inline int __always_unused avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val) { struct adv76xx_state *state = to_state(sd); @@ -439,14 +439,14 @@ static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg) return adv76xx_read_check(state, ADV76XX_PAGE_INFOFRAME, reg); } -static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val) +static inline int __always_unused infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val) { struct adv76xx_state *state = to_state(sd); return regmap_write(state->regmap[ADV76XX_PAGE_INFOFRAME], reg, val); } -static inline int afe_read(struct v4l2_subdev *sd, u8 reg) +static inline int __always_unused afe_read(struct v4l2_subdev *sd, u8 reg) { struct adv76xx_state *state = to_state(sd); @@ -479,14 +479,14 @@ static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val); } -static inline int edid_read(struct v4l2_subdev *sd, u8 reg) +static inline int __always_unused edid_read(struct v4l2_subdev *sd, u8 reg) { struct adv76xx_state *state = to_state(sd); return adv76xx_read_check(state, ADV76XX_PAGE_EDID, reg); } -static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val) +static inline int __always_unused edid_write(struct v4l2_subdev *sd, u8 reg, u8 val) { struct adv76xx_state *state = to_state(sd); @@ -570,7 +570,7 @@ static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val); } -static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val) +static inline int __always_unused test_write(struct v4l2_subdev *sd, u8 reg, u8 val) { struct adv76xx_state *state = to_state(sd); @@ -601,14 +601,14 @@ static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 v return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val); } -static inline int vdp_read(struct v4l2_subdev *sd, u8 reg) +static inline int __always_unused vdp_read(struct v4l2_subdev *sd, u8 reg) { struct adv76xx_state *state = to_state(sd); return adv76xx_read_check(state, ADV7604_PAGE_VDP, reg); } -static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val) +static inline int __always_unused vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val) { struct adv76xx_state *state = to_state(sd); diff --git a/drivers/media/i2c/adv7842.c b/drivers/media/i2c/adv7842.c index 7f8acbdf0db4..9d6eed0f8281 100644 --- a/drivers/media/i2c/adv7842.c +++ b/drivers/media/i2c/adv7842.c @@ -256,21 +256,11 @@ static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd; } -static inline unsigned hblanking(const struct v4l2_bt_timings *t) -{ - return V4L2_DV_BT_BLANKING_WIDTH(t); -} - static inline unsigned htotal(const struct v4l2_bt_timings *t) { return V4L2_DV_BT_FRAME_WIDTH(t); } -static inline unsigned vblanking(const struct v4l2_bt_timings *t) -{ - return V4L2_DV_BT_BLANKING_HEIGHT(t); -} - static inline unsigned vtotal(const struct v4l2_bt_timings *t) { return V4L2_DV_BT_FRAME_HEIGHT(t); diff --git a/drivers/media/i2c/ccs/ccs-core.c b/drivers/media/i2c/ccs/ccs-core.c index 5363f3bcafe3..9158d3ce45c0 100644 --- a/drivers/media/i2c/ccs/ccs-core.c +++ b/drivers/media/i2c/ccs/ccs-core.c @@ -2758,13 +2758,13 @@ ident_show(struct device *dev, struct device_attribute *attr, char *buf) struct ccs_module_info *minfo = &sensor->minfo; if (minfo->mipi_manufacturer_id) - return snprintf(buf, PAGE_SIZE, "%4.4x%4.4x%2.2x\n", - minfo->mipi_manufacturer_id, minfo->model_id, - minfo->revision_number) + 1; + return sysfs_emit(buf, "%4.4x%4.4x%2.2x\n", + minfo->mipi_manufacturer_id, minfo->model_id, + minfo->revision_number) + 1; else - return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n", - minfo->smia_manufacturer_id, minfo->model_id, - minfo->revision_number) + 1; + return sysfs_emit(buf, "%2.2x%4.4x%2.2x\n", + minfo->smia_manufacturer_id, minfo->model_id, + minfo->revision_number) + 1; } static DEVICE_ATTR_RO(ident); diff --git a/drivers/media/i2c/cx25840/cx25840-ir.c b/drivers/media/i2c/cx25840/cx25840-ir.c index 2cf3e6a1f9e1..9d7d1d149f1a 100644 --- a/drivers/media/i2c/cx25840/cx25840-ir.c +++ b/drivers/media/i2c/cx25840/cx25840-ir.c @@ -136,19 +136,6 @@ static inline u16 count_to_clock_divider(unsigned int d) return (u16) d; } -static inline u16 ns_to_clock_divider(unsigned int ns) -{ - return count_to_clock_divider( - DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ / 1000000 * ns, 1000)); -} - -static inline unsigned int clock_divider_to_ns(unsigned int divider) -{ - /* Period of the Rx or Tx clock in ns */ - return DIV_ROUND_CLOSEST((divider + 1) * 1000, - CX25840_IR_REFCLK_FREQ / 1000000); -} - static inline u16 carrier_freq_to_clock_divider(unsigned int freq) { return count_to_clock_divider( @@ -160,13 +147,6 @@ static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider) return DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, (divider + 1) * 16); } -static inline u16 freq_to_clock_divider(unsigned int freq, - unsigned int rollovers) -{ - return count_to_clock_divider( - DIV_ROUND_CLOSEST(CX25840_IR_REFCLK_FREQ, freq * rollovers)); -} - static inline unsigned int clock_divider_to_freq(unsigned int divider, unsigned int rollovers) { diff --git a/drivers/media/i2c/dw9768.c b/drivers/media/i2c/dw9768.c index c086580efac7..65c6acf3ced9 100644 --- a/drivers/media/i2c/dw9768.c +++ b/drivers/media/i2c/dw9768.c @@ -469,6 +469,11 @@ static int dw9768_probe(struct i2c_client *client) dw9768->sd.entity.function = MEDIA_ENT_F_LENS; + /* + * Device is already turned on by i2c-core with ACPI domain PM. + * Attempt to turn off the device to satisfy the privacy LED concerns. + */ + pm_runtime_set_active(dev); pm_runtime_enable(dev); if (!pm_runtime_enabled(dev)) { ret = dw9768_runtime_resume(dev); @@ -483,6 +488,7 @@ static int dw9768_probe(struct i2c_client *client) dev_err(dev, "failed to register V4L2 subdev: %d", ret); goto err_power_off; } + pm_runtime_idle(dev); return 0; diff --git a/drivers/media/i2c/hi556.c b/drivers/media/i2c/hi556.c index 8db1cbedc1fd..055d1aa8410e 100644 --- a/drivers/media/i2c/hi556.c +++ b/drivers/media/i2c/hi556.c @@ -495,6 +495,9 @@ struct hi556 { /* Streaming on/off */ bool streaming; + + /* True if the device has been identified */ + bool identified; }; static u64 to_pixel_rate(u32 f_index) @@ -757,12 +760,41 @@ static void hi556_assign_pad_format(const struct hi556_mode *mode, fmt->field = V4L2_FIELD_NONE; } +static int hi556_identify_module(struct hi556 *hi556) +{ + struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd); + int ret; + u32 val; + + if (hi556->identified) + return 0; + + ret = hi556_read_reg(hi556, HI556_REG_CHIP_ID, + HI556_REG_VALUE_16BIT, &val); + if (ret) + return ret; + + if (val != HI556_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x", + HI556_CHIP_ID, val); + return -ENXIO; + } + + hi556->identified = true; + + return 0; +} + static int hi556_start_streaming(struct hi556 *hi556) { struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd); const struct hi556_reg_list *reg_list; int link_freq_index, ret; + ret = hi556_identify_module(hi556); + if (ret) + return ret; + link_freq_index = hi556->cur_mode->link_freq_index; reg_list = &link_freq_configs[link_freq_index].reg_list; ret = hi556_write_reg_list(hi556, reg_list); @@ -1001,26 +1033,6 @@ static const struct v4l2_subdev_internal_ops hi556_internal_ops = { .open = hi556_open, }; -static int hi556_identify_module(struct hi556 *hi556) -{ - struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd); - int ret; - u32 val; - - ret = hi556_read_reg(hi556, HI556_REG_CHIP_ID, - HI556_REG_VALUE_16BIT, &val); - if (ret) - return ret; - - if (val != HI556_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x", - HI556_CHIP_ID, val); - return -ENXIO; - } - - return 0; -} - static int hi556_check_hwcfg(struct device *dev) { struct fwnode_handle *ep; @@ -1106,6 +1118,7 @@ static int hi556_remove(struct i2c_client *client) static int hi556_probe(struct i2c_client *client) { struct hi556 *hi556; + bool full_power; int ret; ret = hi556_check_hwcfg(&client->dev); @@ -1120,10 +1133,14 @@ static int hi556_probe(struct i2c_client *client) return -ENOMEM; v4l2_i2c_subdev_init(&hi556->sd, client, &hi556_subdev_ops); - ret = hi556_identify_module(hi556); - if (ret) { - dev_err(&client->dev, "failed to find sensor: %d", ret); - return ret; + + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + ret = hi556_identify_module(hi556); + if (ret) { + dev_err(&client->dev, "failed to find sensor: %d", ret); + return ret; + } } mutex_init(&hi556->mutex); @@ -1152,7 +1169,9 @@ static int hi556_probe(struct i2c_client *client) goto probe_error_media_entity_cleanup; } - pm_runtime_set_active(&client->dev); + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -1189,6 +1208,7 @@ static struct i2c_driver hi556_i2c_driver = { }, .probe_new = hi556_probe, .remove = hi556_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(hi556_i2c_driver); diff --git a/drivers/media/i2c/hi846.c b/drivers/media/i2c/hi846.c index 822ce3021fde..ad35c3ff3611 100644 --- a/drivers/media/i2c/hi846.c +++ b/drivers/media/i2c/hi846.c @@ -7,9 +7,9 @@ #include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/module.h> -#include <linux/of_graph.h> #include <linux/pm_runtime.h> #include <linux/pm.h> +#include <linux/property.h> #include <linux/regulator/consumer.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> @@ -1656,7 +1656,7 @@ err_reg: return ret; } -static void hi846_power_off(struct hi846 *hi846) +static int hi846_power_off(struct hi846 *hi846) { if (hi846->rst_gpio) gpiod_set_value_cansleep(hi846->rst_gpio, 1); @@ -1665,7 +1665,7 @@ static void hi846_power_off(struct hi846 *hi846) gpiod_set_value_cansleep(hi846->shutdown_gpio, 1); clk_disable_unprepare(hi846->clock); - regulator_bulk_disable(HI846_NUM_SUPPLIES, hi846->supplies); + return regulator_bulk_disable(HI846_NUM_SUPPLIES, hi846->supplies); } static int __maybe_unused hi846_suspend(struct device *dev) @@ -1677,9 +1677,7 @@ static int __maybe_unused hi846_suspend(struct device *dev) if (hi846->streaming) hi846_stop_streaming(hi846); - hi846_power_off(hi846); - - return 0; + return hi846_power_off(hi846); } static int __maybe_unused hi846_resume(struct device *dev) @@ -2164,7 +2162,11 @@ static int hi846_remove(struct i2c_client *client) return 0; } -static UNIVERSAL_DEV_PM_OPS(hi846_pm_ops, hi846_suspend, hi846_resume, NULL); +static const struct dev_pm_ops hi846_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, + pm_runtime_force_resume) + SET_RUNTIME_PM_OPS(hi846_suspend, hi846_resume, NULL) +}; static const struct of_device_id hi846_of_match[] = { { .compatible = "hynix,hi846", }, @@ -2176,7 +2178,7 @@ static struct i2c_driver hi846_i2c_driver = { .driver = { .name = "hi846", .pm = &hi846_pm_ops, - .of_match_table = of_match_ptr(hi846_of_match), + .of_match_table = hi846_of_match, }, .probe_new = hi846_probe, .remove = hi846_remove, diff --git a/drivers/media/i2c/imx208.c b/drivers/media/i2c/imx208.c index 6f3d9c1b5879..b9516b2f1c15 100644 --- a/drivers/media/i2c/imx208.c +++ b/drivers/media/i2c/imx208.c @@ -296,6 +296,9 @@ struct imx208 { /* OTP data */ bool otp_read; char otp_data[IMX208_OTP_SIZE]; + + /* True if the device has been identified */ + bool identified; }; static inline struct imx208 *to_imx208(struct v4l2_subdev *_sd) @@ -619,6 +622,34 @@ static int imx208_set_pad_format(struct v4l2_subdev *sd, return 0; } +static int imx208_identify_module(struct imx208 *imx208) +{ + struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); + int ret; + u32 val; + + if (imx208->identified) + return 0; + + ret = imx208_read_reg(imx208, IMX208_REG_CHIP_ID, + 2, &val); + if (ret) { + dev_err(&client->dev, "failed to read chip id %x\n", + IMX208_CHIP_ID); + return ret; + } + + if (val != IMX208_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x\n", + IMX208_CHIP_ID, val); + return -EIO; + } + + imx208->identified = true; + + return 0; +} + /* Start streaming */ static int imx208_start_streaming(struct imx208 *imx208) { @@ -626,6 +657,10 @@ static int imx208_start_streaming(struct imx208 *imx208) const struct imx208_reg_list *reg_list; int ret, link_freq_index; + ret = imx208_identify_module(imx208); + if (ret) + return ret; + /* Setup PLL */ link_freq_index = imx208->cur_mode->link_freq_index; reg_list = &link_freq_configs[link_freq_index].reg_list; @@ -752,29 +787,6 @@ error: } /* Verify chip ID */ -static int imx208_identify_module(struct imx208 *imx208) -{ - struct i2c_client *client = v4l2_get_subdevdata(&imx208->sd); - int ret; - u32 val; - - ret = imx208_read_reg(imx208, IMX208_REG_CHIP_ID, - 2, &val); - if (ret) { - dev_err(&client->dev, "failed to read chip id %x\n", - IMX208_CHIP_ID); - return ret; - } - - if (val != IMX208_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x\n", - IMX208_CHIP_ID, val); - return -EIO; - } - - return 0; -} - static const struct v4l2_subdev_video_ops imx208_video_ops = { .s_stream = imx208_set_stream, }; @@ -813,6 +825,10 @@ static int imx208_read_otp(struct imx208 *imx208) goto out_unlock; } + ret = imx208_identify_module(imx208); + if (ret) + goto out_pm_put; + /* Write register address */ msgs[0].addr = client->addr; msgs[0].flags = 0; @@ -831,6 +847,7 @@ static int imx208_read_otp(struct imx208 *imx208) ret = 0; } +out_pm_put: pm_runtime_put(&client->dev); out_unlock: @@ -961,6 +978,7 @@ static int imx208_probe(struct i2c_client *client) { struct imx208 *imx208; int ret; + bool full_power; u32 val = 0; device_property_read_u32(&client->dev, "clock-frequency", &val); @@ -978,11 +996,14 @@ static int imx208_probe(struct i2c_client *client) /* Initialize subdev */ v4l2_i2c_subdev_init(&imx208->sd, client, &imx208_subdev_ops); - /* Check module identity */ - ret = imx208_identify_module(imx208); - if (ret) { - dev_err(&client->dev, "failed to find sensor: %d", ret); - goto error_probe; + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + /* Check module identity */ + ret = imx208_identify_module(imx208); + if (ret) { + dev_err(&client->dev, "failed to find sensor: %d", ret); + goto error_probe; + } } /* Set default mode to max resolution */ @@ -1017,7 +1038,9 @@ static int imx208_probe(struct i2c_client *client) goto error_async_subdev; } - pm_runtime_set_active(&client->dev); + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -1077,6 +1100,7 @@ static struct i2c_driver imx208_i2c_driver = { }, .probe_new = imx208_probe, .remove = imx208_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(imx208_i2c_driver); diff --git a/drivers/media/i2c/imx274.c b/drivers/media/i2c/imx274.c index 0dce92872176..2aa15b9c23cc 100644 --- a/drivers/media/i2c/imx274.c +++ b/drivers/media/i2c/imx274.c @@ -27,6 +27,7 @@ #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> +#include <media/v4l2-fwnode.h> #include <media/v4l2-subdev.h> /* @@ -1367,6 +1368,10 @@ static int imx274_s_frame_interval(struct v4l2_subdev *sd, int min, max, def; int ret; + ret = pm_runtime_resume_and_get(&imx274->client->dev); + if (ret < 0) + return ret; + mutex_lock(&imx274->lock); ret = imx274_set_frame_interval(imx274, fi->interval); @@ -1398,6 +1403,7 @@ static int imx274_s_frame_interval(struct v4l2_subdev *sd, unlock: mutex_unlock(&imx274->lock); + pm_runtime_put(&imx274->client->dev); return ret; } @@ -1457,7 +1463,7 @@ static int imx274_s_stream(struct v4l2_subdev *sd, int on) goto fail; /* - * update frame rate & expsoure. if the last mode is different, + * update frame rate & exposure. if the last mode is different, * HMAX could be changed. As the result, frame rate & exposure * are changed. * gain is not affected. @@ -1494,7 +1500,7 @@ fail: /* * imx274_get_frame_length - Function for obtaining current frame length * @priv: Pointer to device structure - * @val: Pointer to obainted value + * @val: Pointer to obtained value * * frame_length = vmax x (svr + 1), in unit of hmax. * @@ -1904,7 +1910,21 @@ fail: return err; } +static int imx274_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_mbus_code_enum *code) +{ + if (code->index > 0) + return -EINVAL; + + /* only supported format in the driver is Raw 10 bits SRGGB */ + code->code = MEDIA_BUS_FMT_SRGGB10_1X10; + + return 0; +} + static const struct v4l2_subdev_pad_ops imx274_pad_ops = { + .enum_mbus_code = imx274_enum_mbus_code, .get_fmt = imx274_get_fmt, .set_fmt = imx274_set_fmt, .get_selection = imx274_get_selection, @@ -1938,27 +1958,66 @@ static const struct i2c_device_id imx274_id[] = { }; MODULE_DEVICE_TABLE(i2c, imx274_id); +static int imx274_fwnode_parse(struct device *dev) +{ + struct fwnode_handle *endpoint; + /* Only CSI2 is supported */ + struct v4l2_fwnode_endpoint ep = { + .bus_type = V4L2_MBUS_CSI2_DPHY + }; + int ret; + + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); + if (!endpoint) { + dev_err(dev, "Endpoint node not found\n"); + return -EINVAL; + } + + ret = v4l2_fwnode_endpoint_parse(endpoint, &ep); + fwnode_handle_put(endpoint); + if (ret == -ENXIO) { + dev_err(dev, "Unsupported bus type, should be CSI2\n"); + return ret; + } else if (ret) { + dev_err(dev, "Parsing endpoint node failed %d\n", ret); + return ret; + } + + /* Check number of data lanes, only 4 lanes supported */ + if (ep.bus.mipi_csi2.num_data_lanes != 4) { + dev_err(dev, "Invalid data lanes: %d\n", + ep.bus.mipi_csi2.num_data_lanes); + return -EINVAL; + } + + return 0; +} + static int imx274_probe(struct i2c_client *client) { struct v4l2_subdev *sd; struct stimx274 *imx274; + struct device *dev = &client->dev; int ret; /* initialize imx274 */ - imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL); + imx274 = devm_kzalloc(dev, sizeof(*imx274), GFP_KERNEL); if (!imx274) return -ENOMEM; mutex_init(&imx274->lock); - imx274->inck = devm_clk_get_optional(&client->dev, "inck"); + ret = imx274_fwnode_parse(dev); + if (ret) + return ret; + + imx274->inck = devm_clk_get_optional(dev, "inck"); if (IS_ERR(imx274->inck)) return PTR_ERR(imx274->inck); - ret = imx274_regulators_get(&client->dev, imx274); + ret = imx274_regulators_get(dev, imx274); if (ret) { - dev_err(&client->dev, - "Failed to get power regulators, err: %d\n", ret); + dev_err(dev, "Failed to get power regulators, err: %d\n", ret); return ret; } @@ -1977,7 +2036,7 @@ static int imx274_probe(struct i2c_client *client) /* initialize regmap */ imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config); if (IS_ERR(imx274->regmap)) { - dev_err(&client->dev, + dev_err(dev, "regmap init failed: %ld\n", PTR_ERR(imx274->regmap)); ret = -ENODEV; goto err_regmap; @@ -1994,34 +2053,32 @@ static int imx274_probe(struct i2c_client *client) sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad); if (ret < 0) { - dev_err(&client->dev, + dev_err(dev, "%s : media entity init Failed %d\n", __func__, ret); goto err_regmap; } /* initialize sensor reset gpio */ - imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", + imx274->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); if (IS_ERR(imx274->reset_gpio)) { if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER) - dev_err(&client->dev, "Reset GPIO not setup in DT"); + dev_err(dev, "Reset GPIO not setup in DT"); ret = PTR_ERR(imx274->reset_gpio); goto err_me; } /* power on the sensor */ - ret = imx274_power_on(&client->dev); + ret = imx274_power_on(dev); if (ret < 0) { - dev_err(&client->dev, - "%s : imx274 power on failed\n", __func__); + dev_err(dev, "%s : imx274 power on failed\n", __func__); goto err_me; } /* initialize controls */ ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4); if (ret < 0) { - dev_err(&client->dev, - "%s : ctrl handler init Failed\n", __func__); + dev_err(dev, "%s : ctrl handler init Failed\n", __func__); goto err_power_off; } @@ -2064,23 +2121,22 @@ static int imx274_probe(struct i2c_client *client) /* register subdevice */ ret = v4l2_async_register_subdev(sd); if (ret < 0) { - dev_err(&client->dev, - "%s : v4l2_async_register_subdev failed %d\n", + dev_err(dev, "%s : v4l2_async_register_subdev failed %d\n", __func__, ret); goto err_ctrls; } - pm_runtime_set_active(&client->dev); - pm_runtime_enable(&client->dev); - pm_runtime_idle(&client->dev); + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + pm_runtime_idle(dev); - dev_info(&client->dev, "imx274 : imx274 probe success !\n"); + dev_info(dev, "imx274 : imx274 probe success !\n"); return 0; err_ctrls: v4l2_ctrl_handler_free(&imx274->ctrls.handler); err_power_off: - imx274_power_off(&client->dev); + imx274_power_off(dev); err_me: media_entity_cleanup(&sd->entity); err_regmap: diff --git a/drivers/media/i2c/imx290.c b/drivers/media/i2c/imx290.c index bf7a6c37ca5d..99f2a50d39a4 100644 --- a/drivers/media/i2c/imx290.c +++ b/drivers/media/i2c/imx290.c @@ -363,7 +363,7 @@ static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd) return container_of(_sd, struct imx290, sd); } -static inline int imx290_read_reg(struct imx290 *imx290, u16 addr, u8 *value) +static inline int __always_unused imx290_read_reg(struct imx290 *imx290, u16 addr, u8 *value) { unsigned int regval; int ret; diff --git a/drivers/media/i2c/imx319.c b/drivers/media/i2c/imx319.c index daa976858e29..a2b5a34de76b 100644 --- a/drivers/media/i2c/imx319.c +++ b/drivers/media/i2c/imx319.c @@ -2565,6 +2565,6 @@ module_i2c_driver(imx319_i2c_driver); MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>"); MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>"); MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>"); -MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>"); +MODULE_AUTHOR("Yang, Hyungwoo"); MODULE_DESCRIPTION("Sony imx319 sensor driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/imx355.c b/drivers/media/i2c/imx355.c index cb51c81786bd..3922b9305978 100644 --- a/drivers/media/i2c/imx355.c +++ b/drivers/media/i2c/imx355.c @@ -1851,6 +1851,6 @@ module_i2c_driver(imx355_i2c_driver); MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>"); MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>"); MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>"); -MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>"); +MODULE_AUTHOR("Yang, Hyungwoo"); MODULE_DESCRIPTION("Sony imx355 sensor driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/max9286.c b/drivers/media/i2c/max9286.c index 7c663fd587bb..eb2b8e42335b 100644 --- a/drivers/media/i2c/max9286.c +++ b/drivers/media/i2c/max9286.c @@ -1055,7 +1055,6 @@ static int max9286_register_gpio(struct max9286_priv *priv) gpio->label = dev_name(dev); gpio->parent = dev; gpio->owner = THIS_MODULE; - gpio->of_node = dev->of_node; gpio->ngpio = 2; gpio->base = -1; gpio->set = max9286_gpio_set; @@ -1295,11 +1294,9 @@ static int max9286_probe(struct i2c_client *client) priv->regulator = devm_regulator_get(&client->dev, "poc"); if (IS_ERR(priv->regulator)) { - if (PTR_ERR(priv->regulator) != -EPROBE_DEFER) - dev_err(&client->dev, - "Unable to get PoC regulator (%ld)\n", - PTR_ERR(priv->regulator)); ret = PTR_ERR(priv->regulator); + dev_err_probe(&client->dev, ret, + "Unable to get PoC regulator\n"); goto err_powerdown; } diff --git a/drivers/media/i2c/ov13858.c b/drivers/media/i2c/ov13858.c index b4d22f5d9933..d5fe67c763f7 100644 --- a/drivers/media/i2c/ov13858.c +++ b/drivers/media/i2c/ov13858.c @@ -1818,6 +1818,6 @@ module_i2c_driver(ov13858_i2c_driver); MODULE_AUTHOR("Kan, Chris <chris.kan@intel.com>"); MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>"); -MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>"); +MODULE_AUTHOR("Yang, Hyungwoo"); MODULE_DESCRIPTION("Omnivision ov13858 sensor driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/ov2740.c b/drivers/media/i2c/ov2740.c index 934c9d65cb09..bab720c7c1de 100644 --- a/drivers/media/i2c/ov2740.c +++ b/drivers/media/i2c/ov2740.c @@ -345,6 +345,9 @@ struct ov2740 { /* NVM data inforamtion */ struct nvm_data *nvm; + + /* True if the device has been identified */ + bool identified; }; static inline struct ov2740 *to_ov2740(struct v4l2_subdev *subdev) @@ -440,6 +443,30 @@ static int ov2740_write_reg_list(struct ov2740 *ov2740, return 0; } +static int ov2740_identify_module(struct ov2740 *ov2740) +{ + struct i2c_client *client = v4l2_get_subdevdata(&ov2740->sd); + int ret; + u32 val; + + if (ov2740->identified) + return 0; + + ret = ov2740_read_reg(ov2740, OV2740_REG_CHIP_ID, 3, &val); + if (ret) + return ret; + + if (val != OV2740_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x", + OV2740_CHIP_ID, val); + return -ENXIO; + } + + ov2740->identified = true; + + return 0; +} + static int ov2740_update_digital_gain(struct ov2740 *ov2740, u32 d_gain) { int ret = 0; @@ -724,6 +751,10 @@ static int ov2740_start_streaming(struct ov2740 *ov2740) int link_freq_index; int ret = 0; + ret = ov2740_identify_module(ov2740); + if (ret) + return ret; + ov2740_load_otp_data(nvm); link_freq_index = ov2740->cur_mode->link_freq_index; @@ -956,25 +987,6 @@ static const struct v4l2_subdev_internal_ops ov2740_internal_ops = { .open = ov2740_open, }; -static int ov2740_identify_module(struct ov2740 *ov2740) -{ - struct i2c_client *client = v4l2_get_subdevdata(&ov2740->sd); - int ret; - u32 val; - - ret = ov2740_read_reg(ov2740, OV2740_REG_CHIP_ID, 3, &val); - if (ret) - return ret; - - if (val != OV2740_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x", - OV2740_CHIP_ID, val); - return -ENXIO; - } - - return 0; -} - static int ov2740_check_hwcfg(struct device *dev) { struct fwnode_handle *ep; @@ -1137,6 +1149,7 @@ static int ov2740_probe(struct i2c_client *client) { struct ov2740 *ov2740; int ret = 0; + bool full_power; ret = ov2740_check_hwcfg(&client->dev); if (ret) { @@ -1149,6 +1162,15 @@ static int ov2740_probe(struct i2c_client *client) if (!ov2740) return -ENOMEM; + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + ret = ov2740_identify_module(ov2740); + if (ret) { + dev_err(&client->dev, "failed to find sensor: %d", ret); + return ret; + } + } + v4l2_i2c_subdev_init(&ov2740->sd, client, &ov2740_subdev_ops); ret = ov2740_identify_module(ov2740); if (ret) { @@ -1186,11 +1208,9 @@ static int ov2740_probe(struct i2c_client *client) if (ret) dev_warn(&client->dev, "register nvmem failed, ret %d\n", ret); - /* - * Device is already turned on by i2c-core with ACPI domain PM. - * Enable runtime PM and turn off the device. - */ - pm_runtime_set_active(&client->dev); + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -1225,6 +1245,7 @@ static struct i2c_driver ov2740_i2c_driver = { }, .probe_new = ov2740_probe, .remove = ov2740_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(ov2740_i2c_driver); diff --git a/drivers/media/i2c/ov5670.c b/drivers/media/i2c/ov5670.c index 251f459ab484..02f75c18e480 100644 --- a/drivers/media/i2c/ov5670.c +++ b/drivers/media/i2c/ov5670.c @@ -1833,6 +1833,8 @@ struct ov5670 { /* Streaming on/off */ bool streaming; + /* True if the device has been identified */ + bool identified; }; #define to_ov5670(_sd) container_of(_sd, struct ov5670, sd) @@ -2273,6 +2275,32 @@ static int ov5670_get_skip_frames(struct v4l2_subdev *sd, u32 *frames) return 0; } +/* Verify chip ID */ +static int ov5670_identify_module(struct ov5670 *ov5670) +{ + struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd); + int ret; + u32 val; + + if (ov5670->identified) + return 0; + + ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID, + OV5670_REG_VALUE_24BIT, &val); + if (ret) + return ret; + + if (val != OV5670_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x\n", + OV5670_CHIP_ID, val); + return -ENXIO; + } + + ov5670->identified = true; + + return 0; +} + /* Prepare streaming by writing default values and customized values */ static int ov5670_start_streaming(struct ov5670 *ov5670) { @@ -2281,6 +2309,10 @@ static int ov5670_start_streaming(struct ov5670 *ov5670) int link_freq_index; int ret; + ret = ov5670_identify_module(ov5670); + if (ret) + return ret; + /* Get out of from software reset */ ret = ov5670_write_reg(ov5670, OV5670_REG_SOFTWARE_RST, OV5670_REG_VALUE_08BIT, OV5670_SOFTWARE_RST); @@ -2400,27 +2432,6 @@ static int __maybe_unused ov5670_resume(struct device *dev) return 0; } -/* Verify chip ID */ -static int ov5670_identify_module(struct ov5670 *ov5670) -{ - struct i2c_client *client = v4l2_get_subdevdata(&ov5670->sd); - int ret; - u32 val; - - ret = ov5670_read_reg(ov5670, OV5670_REG_CHIP_ID, - OV5670_REG_VALUE_24BIT, &val); - if (ret) - return ret; - - if (val != OV5670_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x\n", - OV5670_CHIP_ID, val); - return -ENXIO; - } - - return 0; -} - static const struct v4l2_subdev_core_ops ov5670_core_ops = { .log_status = v4l2_ctrl_subdev_log_status, .subscribe_event = v4l2_ctrl_subdev_subscribe_event, @@ -2462,6 +2473,7 @@ static int ov5670_probe(struct i2c_client *client) struct ov5670 *ov5670; const char *err_msg; u32 input_clk = 0; + bool full_power; int ret; device_property_read_u32(&client->dev, "clock-frequency", &input_clk); @@ -2478,11 +2490,14 @@ static int ov5670_probe(struct i2c_client *client) /* Initialize subdev */ v4l2_i2c_subdev_init(&ov5670->sd, client, &ov5670_subdev_ops); - /* Check module identity */ - ret = ov5670_identify_module(ov5670); - if (ret) { - err_msg = "ov5670_identify_module() error"; - goto error_print; + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + /* Check module identity */ + ret = ov5670_identify_module(ov5670); + if (ret) { + err_msg = "ov5670_identify_module() error"; + goto error_print; + } } mutex_init(&ov5670->mutex); @@ -2519,11 +2534,9 @@ static int ov5670_probe(struct i2c_client *client) ov5670->streaming = false; - /* - * Device is already turned on by i2c-core with ACPI domain PM. - * Enable runtime PM and turn off the device. - */ - pm_runtime_set_active(&client->dev); + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -2565,7 +2578,7 @@ static const struct dev_pm_ops ov5670_pm_ops = { #ifdef CONFIG_ACPI static const struct acpi_device_id ov5670_acpi_ids[] = { - {"INT3479"}, + { "INT3479" }, { /* sentinel */ } }; @@ -2580,11 +2593,12 @@ static struct i2c_driver ov5670_i2c_driver = { }, .probe_new = ov5670_probe, .remove = ov5670_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(ov5670_i2c_driver); MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>"); -MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>"); +MODULE_AUTHOR("Yang, Hyungwoo"); MODULE_DESCRIPTION("Omnivision ov5670 sensor driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/media/i2c/ov5675.c b/drivers/media/i2c/ov5675.c index da5850b7ad07..00925850fa7c 100644 --- a/drivers/media/i2c/ov5675.c +++ b/drivers/media/i2c/ov5675.c @@ -493,6 +493,9 @@ struct ov5675 { /* Streaming on/off */ bool streaming; + + /* True if the device has been identified */ + bool identified; }; static u64 to_pixel_rate(u32 f_index) @@ -808,12 +811,41 @@ static void ov5675_update_pad_format(const struct ov5675_mode *mode, fmt->field = V4L2_FIELD_NONE; } +static int ov5675_identify_module(struct ov5675 *ov5675) +{ + struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd); + int ret; + u32 val; + + if (ov5675->identified) + return 0; + + ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID, + OV5675_REG_VALUE_24BIT, &val); + if (ret) + return ret; + + if (val != OV5675_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x", + OV5675_CHIP_ID, val); + return -ENXIO; + } + + ov5675->identified = true; + + return 0; +} + static int ov5675_start_streaming(struct ov5675 *ov5675) { struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd); const struct ov5675_reg_list *reg_list; int link_freq_index, ret; + ret = ov5675_identify_module(ov5675); + if (ret) + return ret; + link_freq_index = ov5675->cur_mode->link_freq_index; reg_list = &link_freq_configs[link_freq_index].reg_list; ret = ov5675_write_reg_list(ov5675, reg_list); @@ -1048,26 +1080,6 @@ static const struct v4l2_subdev_internal_ops ov5675_internal_ops = { .open = ov5675_open, }; -static int ov5675_identify_module(struct ov5675 *ov5675) -{ - struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd); - int ret; - u32 val; - - ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID, - OV5675_REG_VALUE_24BIT, &val); - if (ret) - return ret; - - if (val != OV5675_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x", - OV5675_CHIP_ID, val); - return -ENXIO; - } - - return 0; -} - static int ov5675_check_hwcfg(struct device *dev) { struct fwnode_handle *ep; @@ -1154,6 +1166,7 @@ static int ov5675_remove(struct i2c_client *client) static int ov5675_probe(struct i2c_client *client) { struct ov5675 *ov5675; + bool full_power; int ret; ret = ov5675_check_hwcfg(&client->dev); @@ -1168,10 +1181,14 @@ static int ov5675_probe(struct i2c_client *client) return -ENOMEM; v4l2_i2c_subdev_init(&ov5675->sd, client, &ov5675_subdev_ops); - ret = ov5675_identify_module(ov5675); - if (ret) { - dev_err(&client->dev, "failed to find sensor: %d", ret); - return ret; + + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + ret = ov5675_identify_module(ov5675); + if (ret) { + dev_err(&client->dev, "failed to find sensor: %d", ret); + return ret; + } } mutex_init(&ov5675->mutex); @@ -1204,7 +1221,10 @@ static int ov5675_probe(struct i2c_client *client) * Device is already turned on by i2c-core with ACPI domain PM. * Enable runtime PM and turn off the device. */ - pm_runtime_set_active(&client->dev); + + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -1241,6 +1261,7 @@ static struct i2c_driver ov5675_i2c_driver = { }, .probe_new = ov5675_probe, .remove = ov5675_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(ov5675_i2c_driver); diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c new file mode 100644 index 000000000000..2784fcf67f3b --- /dev/null +++ b/drivers/media/i2c/ov5693.c @@ -0,0 +1,1537 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2013 Intel Corporation. All Rights Reserved. + * + * Adapted from the atomisp-ov5693 driver, with contributions from: + * + * Daniel Scally + * Jean-Michel Hautbois + * Fabian Wuthrich + * Tsuchiya Yuto + * Jordan Hand + * Jake Day + */ + +#include <asm/unaligned.h> +#include <linux/acpi.h> +#include <linux/clk.h> +#include <linux/delay.h> +#include <linux/device.h> +#include <linux/i2c.h> +#include <linux/module.h> +#include <linux/pm_runtime.h> +#include <linux/regulator/consumer.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-device.h> +#include <media/v4l2-fwnode.h> + +#define OV5693_REG_8BIT(n) ((1 << 16) | (n)) +#define OV5693_REG_16BIT(n) ((2 << 16) | (n)) +#define OV5693_REG_24BIT(n) ((3 << 16) | (n)) +#define OV5693_REG_SIZE_SHIFT 16 +#define OV5693_REG_ADDR_MASK 0xffff + +/* System Control */ +#define OV5693_SW_RESET_REG OV5693_REG_8BIT(0x0103) +#define OV5693_SW_STREAM_REG OV5693_REG_8BIT(0x0100) +#define OV5693_START_STREAMING 0x01 +#define OV5693_STOP_STREAMING 0x00 +#define OV5693_SW_RESET 0x01 + +#define OV5693_REG_CHIP_ID OV5693_REG_16BIT(0x300a) +/* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */ +#define OV5693_CHIP_ID 0x5690 + +/* Exposure */ +#define OV5693_EXPOSURE_CTRL_REG OV5693_REG_24BIT(0x3500) +#define OV5693_EXPOSURE_CTRL_MASK GENMASK(19, 4) +#define OV5693_INTEGRATION_TIME_MARGIN 8 +#define OV5693_EXPOSURE_MIN 1 +#define OV5693_EXPOSURE_STEP 1 + +/* Analogue Gain */ +#define OV5693_GAIN_CTRL_REG OV5693_REG_16BIT(0x350a) +#define OV5693_GAIN_CTRL_MASK GENMASK(10, 4) +#define OV5693_GAIN_MIN 1 +#define OV5693_GAIN_MAX 127 +#define OV5693_GAIN_DEF 8 +#define OV5693_GAIN_STEP 1 + +/* Digital Gain */ +#define OV5693_MWB_RED_GAIN_REG OV5693_REG_16BIT(0x3400) +#define OV5693_MWB_GREEN_GAIN_REG OV5693_REG_16BIT(0x3402) +#define OV5693_MWB_BLUE_GAIN_REG OV5693_REG_16BIT(0x3404) +#define OV5693_MWB_GAIN_MASK GENMASK(11, 0) +#define OV5693_MWB_GAIN_MAX 0x0fff +#define OV5693_DIGITAL_GAIN_MIN 1 +#define OV5693_DIGITAL_GAIN_MAX 4095 +#define OV5693_DIGITAL_GAIN_DEF 1024 +#define OV5693_DIGITAL_GAIN_STEP 1 + +/* Timing and Format */ +#define OV5693_CROP_START_X_REG OV5693_REG_16BIT(0x3800) +#define OV5693_CROP_START_Y_REG OV5693_REG_16BIT(0x3802) +#define OV5693_CROP_END_X_REG OV5693_REG_16BIT(0x3804) +#define OV5693_CROP_END_Y_REG OV5693_REG_16BIT(0x3806) +#define OV5693_OUTPUT_SIZE_X_REG OV5693_REG_16BIT(0x3808) +#define OV5693_OUTPUT_SIZE_Y_REG OV5693_REG_16BIT(0x380a) + +#define OV5693_TIMING_HTS_REG OV5693_REG_16BIT(0x380c) +#define OV5693_FIXED_PPL 2688U +#define OV5693_TIMING_VTS_REG OV5693_REG_16BIT(0x380e) +#define OV5693_TIMING_MAX_VTS 0xffff +#define OV5693_TIMING_MIN_VTS 0x04 + +#define OV5693_OFFSET_START_X_REG OV5693_REG_16BIT(0x3810) +#define OV5693_OFFSET_START_Y_REG OV5693_REG_16BIT(0x3812) + +#define OV5693_SUB_INC_X_REG OV5693_REG_8BIT(0x3814) +#define OV5693_SUB_INC_Y_REG OV5693_REG_8BIT(0x3815) + +#define OV5693_FORMAT1_REG OV5693_REG_8BIT(0x3820) +#define OV5693_FORMAT1_FLIP_VERT_ISP_EN BIT(6) +#define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1) +#define OV5693_FORMAT1_VBIN_EN BIT(0) +#define OV5693_FORMAT2_REG OV5693_REG_8BIT(0x3821) +#define OV5693_FORMAT2_HDR_EN BIT(7) +#define OV5693_FORMAT2_FLIP_HORZ_ISP_EN BIT(2) +#define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1) +#define OV5693_FORMAT2_HBIN_EN BIT(0) + +#define OV5693_ISP_CTRL2_REG OV5693_REG_8BIT(0x5002) +#define OV5693_ISP_SCALE_ENABLE BIT(7) + +/* Pixel Array */ +#define OV5693_NATIVE_WIDTH 2624 +#define OV5693_NATIVE_HEIGHT 1956 +#define OV5693_NATIVE_START_LEFT 0 +#define OV5693_NATIVE_START_TOP 0 +#define OV5693_ACTIVE_WIDTH 2592 +#define OV5693_ACTIVE_HEIGHT 1944 +#define OV5693_ACTIVE_START_LEFT 16 +#define OV5693_ACTIVE_START_TOP 6 +#define OV5693_MIN_CROP_WIDTH 2 +#define OV5693_MIN_CROP_HEIGHT 2 + +/* Test Pattern */ +#define OV5693_TEST_PATTERN_REG OV5693_REG_8BIT(0x5e00) +#define OV5693_TEST_PATTERN_ENABLE BIT(7) +#define OV5693_TEST_PATTERN_ROLLING BIT(6) +#define OV5693_TEST_PATTERN_RANDOM 0x01 +#define OV5693_TEST_PATTERN_BARS 0x00 + +/* System Frequencies */ +#define OV5693_XVCLK_FREQ 19200000 +#define OV5693_LINK_FREQ_419_2MHZ 419200000 +#define OV5693_PIXEL_RATE 167680000 + +/* Miscellaneous */ +#define OV5693_NUM_SUPPLIES 2 + +#define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd) + +struct ov5693_reg { + u32 reg; + u8 val; +}; + +struct ov5693_reg_list { + u32 num_regs; + const struct ov5693_reg *regs; +}; + +struct ov5693_device { + struct i2c_client *client; + struct device *dev; + + /* Protect against concurrent changes to controls */ + struct mutex lock; + + struct gpio_desc *reset; + struct gpio_desc *powerdown; + struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES]; + struct clk *clk; + + struct ov5693_mode { + struct v4l2_rect crop; + struct v4l2_mbus_framefmt format; + bool binning_x; + bool binning_y; + unsigned int inc_x_odd; + unsigned int inc_y_odd; + unsigned int vts; + } mode; + bool streaming; + + struct v4l2_subdev sd; + struct media_pad pad; + + struct ov5693_v4l2_ctrls { + struct v4l2_ctrl_handler handler; + struct v4l2_ctrl *link_freq; + struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *exposure; + struct v4l2_ctrl *analogue_gain; + struct v4l2_ctrl *digital_gain; + struct v4l2_ctrl *hflip; + struct v4l2_ctrl *vflip; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *test_pattern; + } ctrls; +}; + +static const struct ov5693_reg ov5693_global_regs[] = { + {OV5693_REG_8BIT(0x3016), 0xf0}, + {OV5693_REG_8BIT(0x3017), 0xf0}, + {OV5693_REG_8BIT(0x3018), 0xf0}, + {OV5693_REG_8BIT(0x3022), 0x01}, + {OV5693_REG_8BIT(0x3028), 0x44}, + {OV5693_REG_8BIT(0x3098), 0x02}, + {OV5693_REG_8BIT(0x3099), 0x19}, + {OV5693_REG_8BIT(0x309a), 0x02}, + {OV5693_REG_8BIT(0x309b), 0x01}, + {OV5693_REG_8BIT(0x309c), 0x00}, + {OV5693_REG_8BIT(0x30a0), 0xd2}, + {OV5693_REG_8BIT(0x30a2), 0x01}, + {OV5693_REG_8BIT(0x30b2), 0x00}, + {OV5693_REG_8BIT(0x30b3), 0x83}, + {OV5693_REG_8BIT(0x30b4), 0x03}, + {OV5693_REG_8BIT(0x30b5), 0x04}, + {OV5693_REG_8BIT(0x30b6), 0x01}, + {OV5693_REG_8BIT(0x3080), 0x01}, + {OV5693_REG_8BIT(0x3104), 0x21}, + {OV5693_REG_8BIT(0x3106), 0x00}, + {OV5693_REG_8BIT(0x3406), 0x01}, + {OV5693_REG_8BIT(0x3503), 0x07}, + {OV5693_REG_8BIT(0x350b), 0x40}, + {OV5693_REG_8BIT(0x3601), 0x0a}, + {OV5693_REG_8BIT(0x3602), 0x38}, + {OV5693_REG_8BIT(0x3612), 0x80}, + {OV5693_REG_8BIT(0x3620), 0x54}, + {OV5693_REG_8BIT(0x3621), 0xc7}, + {OV5693_REG_8BIT(0x3622), 0x0f}, + {OV5693_REG_8BIT(0x3625), 0x10}, + {OV5693_REG_8BIT(0x3630), 0x55}, + {OV5693_REG_8BIT(0x3631), 0xf4}, + {OV5693_REG_8BIT(0x3632), 0x00}, + {OV5693_REG_8BIT(0x3633), 0x34}, + {OV5693_REG_8BIT(0x3634), 0x02}, + {OV5693_REG_8BIT(0x364d), 0x0d}, + {OV5693_REG_8BIT(0x364f), 0xdd}, + {OV5693_REG_8BIT(0x3660), 0x04}, + {OV5693_REG_8BIT(0x3662), 0x10}, + {OV5693_REG_8BIT(0x3663), 0xf1}, + {OV5693_REG_8BIT(0x3665), 0x00}, + {OV5693_REG_8BIT(0x3666), 0x20}, + {OV5693_REG_8BIT(0x3667), 0x00}, + {OV5693_REG_8BIT(0x366a), 0x80}, + {OV5693_REG_8BIT(0x3680), 0xe0}, + {OV5693_REG_8BIT(0x3681), 0x00}, + {OV5693_REG_8BIT(0x3700), 0x42}, + {OV5693_REG_8BIT(0x3701), 0x14}, + {OV5693_REG_8BIT(0x3702), 0xa0}, + {OV5693_REG_8BIT(0x3703), 0xd8}, + {OV5693_REG_8BIT(0x3704), 0x78}, + {OV5693_REG_8BIT(0x3705), 0x02}, + {OV5693_REG_8BIT(0x370a), 0x00}, + {OV5693_REG_8BIT(0x370b), 0x20}, + {OV5693_REG_8BIT(0x370c), 0x0c}, + {OV5693_REG_8BIT(0x370d), 0x11}, + {OV5693_REG_8BIT(0x370e), 0x00}, + {OV5693_REG_8BIT(0x370f), 0x40}, + {OV5693_REG_8BIT(0x3710), 0x00}, + {OV5693_REG_8BIT(0x371a), 0x1c}, + {OV5693_REG_8BIT(0x371b), 0x05}, + {OV5693_REG_8BIT(0x371c), 0x01}, + {OV5693_REG_8BIT(0x371e), 0xa1}, + {OV5693_REG_8BIT(0x371f), 0x0c}, + {OV5693_REG_8BIT(0x3721), 0x00}, + {OV5693_REG_8BIT(0x3724), 0x10}, + {OV5693_REG_8BIT(0x3726), 0x00}, + {OV5693_REG_8BIT(0x372a), 0x01}, + {OV5693_REG_8BIT(0x3730), 0x10}, + {OV5693_REG_8BIT(0x3738), 0x22}, + {OV5693_REG_8BIT(0x3739), 0xe5}, + {OV5693_REG_8BIT(0x373a), 0x50}, + {OV5693_REG_8BIT(0x373b), 0x02}, + {OV5693_REG_8BIT(0x373c), 0x41}, + {OV5693_REG_8BIT(0x373f), 0x02}, + {OV5693_REG_8BIT(0x3740), 0x42}, + {OV5693_REG_8BIT(0x3741), 0x02}, + {OV5693_REG_8BIT(0x3742), 0x18}, + {OV5693_REG_8BIT(0x3743), 0x01}, + {OV5693_REG_8BIT(0x3744), 0x02}, + {OV5693_REG_8BIT(0x3747), 0x10}, + {OV5693_REG_8BIT(0x374c), 0x04}, + {OV5693_REG_8BIT(0x3751), 0xf0}, + {OV5693_REG_8BIT(0x3752), 0x00}, + {OV5693_REG_8BIT(0x3753), 0x00}, + {OV5693_REG_8BIT(0x3754), 0xc0}, + {OV5693_REG_8BIT(0x3755), 0x00}, + {OV5693_REG_8BIT(0x3756), 0x1a}, + {OV5693_REG_8BIT(0x3758), 0x00}, + {OV5693_REG_8BIT(0x3759), 0x0f}, + {OV5693_REG_8BIT(0x376b), 0x44}, + {OV5693_REG_8BIT(0x375c), 0x04}, + {OV5693_REG_8BIT(0x3774), 0x10}, + {OV5693_REG_8BIT(0x3776), 0x00}, + {OV5693_REG_8BIT(0x377f), 0x08}, + {OV5693_REG_8BIT(0x3780), 0x22}, + {OV5693_REG_8BIT(0x3781), 0x0c}, + {OV5693_REG_8BIT(0x3784), 0x2c}, + {OV5693_REG_8BIT(0x3785), 0x1e}, + {OV5693_REG_8BIT(0x378f), 0xf5}, + {OV5693_REG_8BIT(0x3791), 0xb0}, + {OV5693_REG_8BIT(0x3795), 0x00}, + {OV5693_REG_8BIT(0x3796), 0x64}, + {OV5693_REG_8BIT(0x3797), 0x11}, + {OV5693_REG_8BIT(0x3798), 0x30}, + {OV5693_REG_8BIT(0x3799), 0x41}, + {OV5693_REG_8BIT(0x379a), 0x07}, + {OV5693_REG_8BIT(0x379b), 0xb0}, + {OV5693_REG_8BIT(0x379c), 0x0c}, + {OV5693_REG_8BIT(0x3a04), 0x06}, + {OV5693_REG_8BIT(0x3a05), 0x14}, + {OV5693_REG_8BIT(0x3e07), 0x20}, + {OV5693_REG_8BIT(0x4000), 0x08}, + {OV5693_REG_8BIT(0x4001), 0x04}, + {OV5693_REG_8BIT(0x4004), 0x08}, + {OV5693_REG_8BIT(0x4006), 0x20}, + {OV5693_REG_8BIT(0x4008), 0x24}, + {OV5693_REG_8BIT(0x4009), 0x10}, + {OV5693_REG_8BIT(0x4058), 0x00}, + {OV5693_REG_8BIT(0x4101), 0xb2}, + {OV5693_REG_8BIT(0x4307), 0x31}, + {OV5693_REG_8BIT(0x4511), 0x05}, + {OV5693_REG_8BIT(0x4512), 0x01}, + {OV5693_REG_8BIT(0x481f), 0x30}, + {OV5693_REG_8BIT(0x4826), 0x2c}, + {OV5693_REG_8BIT(0x4d02), 0xfd}, + {OV5693_REG_8BIT(0x4d03), 0xf5}, + {OV5693_REG_8BIT(0x4d04), 0x0c}, + {OV5693_REG_8BIT(0x4d05), 0xcc}, + {OV5693_REG_8BIT(0x4837), 0x0a}, + {OV5693_REG_8BIT(0x5003), 0x20}, + {OV5693_REG_8BIT(0x5013), 0x00}, + {OV5693_REG_8BIT(0x5842), 0x01}, + {OV5693_REG_8BIT(0x5843), 0x2b}, + {OV5693_REG_8BIT(0x5844), 0x01}, + {OV5693_REG_8BIT(0x5845), 0x92}, + {OV5693_REG_8BIT(0x5846), 0x01}, + {OV5693_REG_8BIT(0x5847), 0x8f}, + {OV5693_REG_8BIT(0x5848), 0x01}, + {OV5693_REG_8BIT(0x5849), 0x0c}, + {OV5693_REG_8BIT(0x5e10), 0x0c}, + {OV5693_REG_8BIT(0x3820), 0x00}, + {OV5693_REG_8BIT(0x3821), 0x1e}, + {OV5693_REG_8BIT(0x5041), 0x14} +}; + +static const struct ov5693_reg_list ov5693_global_setting = { + .num_regs = ARRAY_SIZE(ov5693_global_regs), + .regs = ov5693_global_regs, +}; + +static const struct v4l2_rect ov5693_default_crop = { + .left = OV5693_ACTIVE_START_LEFT, + .top = OV5693_ACTIVE_START_TOP, + .width = OV5693_ACTIVE_WIDTH, + .height = OV5693_ACTIVE_HEIGHT, +}; + +static const struct v4l2_mbus_framefmt ov5693_default_fmt = { + .width = OV5693_ACTIVE_WIDTH, + .height = OV5693_ACTIVE_HEIGHT, + .code = MEDIA_BUS_FMT_SBGGR10_1X10, +}; + +static const s64 link_freq_menu_items[] = { + OV5693_LINK_FREQ_419_2MHZ +}; + +static const char * const ov5693_supply_names[] = { + "avdd", + "dovdd", +}; + +static const char * const ov5693_test_pattern_menu[] = { + "Disabled", + "Random Data", + "Colour Bars", + "Colour Bars with Rolling Bar" +}; + +static const u8 ov5693_test_pattern_bits[] = { + 0, + OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM, + OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS, + OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS | + OV5693_TEST_PATTERN_ROLLING, +}; + +/* I2C I/O Operations */ + +static int ov5693_read_reg(struct ov5693_device *ov5693, u32 addr, u32 *value) +{ + struct i2c_client *client = ov5693->client; + __be16 reg; + u8 val[4]; + struct i2c_msg msg[] = { + { + .addr = client->addr, + .flags = 0, + .len = 2, + .buf = (u8 *)®, + }, + { + .addr = client->addr, + .flags = I2C_M_RD, + .buf = (u8 *)&val, + }, + }; + unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3); + unsigned int i; + int ret; + + reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK); + + msg[1].len = len; + + ret = i2c_transfer(client->adapter, msg, 2); + if (ret < 0) + return dev_err_probe(&client->dev, ret, + "Failed to read register 0x%04x: %d\n", + addr & OV5693_REG_ADDR_MASK, ret); + + *value = 0; + for (i = 0; i < len; ++i) { + *value <<= 8; + *value |= val[i]; + } + + return 0; +} + +static void ov5693_write_reg(struct ov5693_device *ov5693, u32 addr, u32 value, + int *error) +{ + struct i2c_client *client = ov5693->client; + struct { + __be16 reg; + u8 val[4]; + } __packed buf; + struct i2c_msg msg = { + .addr = client->addr, + .buf = (u8 *)&buf, + }; + unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3); + unsigned int i; + int ret; + + if (*error < 0) + return; + + buf.reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK); + for (i = 0; i < len; ++i) { + buf.val[len - i - 1] = value & 0xff; + value >>= 8; + } + + msg.len = len + 2; + + ret = i2c_transfer(client->adapter, &msg, 1); + if (ret < 0) { + dev_err(&client->dev, "Failed to write register 0x%04x: %d\n", + addr & OV5693_REG_ADDR_MASK, ret); + *error = ret; + } +} + +static int ov5693_write_reg_array(struct ov5693_device *ov5693, + const struct ov5693_reg_list *reglist) +{ + unsigned int i; + int ret = 0; + + for (i = 0; i < reglist->num_regs; i++) + ov5693_write_reg(ov5693, reglist->regs[i].reg, + reglist->regs[i].val, &ret); + + return ret; +} + +static int ov5693_update_bits(struct ov5693_device *ov5693, u32 address, + u32 mask, u32 bits) +{ + u32 value = 0; + int ret; + + ret = ov5693_read_reg(ov5693, address, &value); + if (ret) + return ret; + + value &= ~mask; + value |= bits; + + ov5693_write_reg(ov5693, address, value, &ret); + + return ret; +} + +/* V4L2 Controls Functions */ + +static int ov5693_flip_vert_configure(struct ov5693_device *ov5693, + bool enable) +{ + u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN | + OV5693_FORMAT1_FLIP_VERT_SENSOR_EN; + int ret; + + ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG, bits, + enable ? bits : 0); + if (ret) + return ret; + + return 0; +} + +static int ov5693_flip_horz_configure(struct ov5693_device *ov5693, + bool enable) +{ + u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN | + OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN; + int ret; + + ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG, bits, + enable ? bits : 0); + if (ret) + return ret; + + return 0; +} + +static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value) +{ + u32 exposure; + int ret; + + ret = ov5693_read_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, &exposure); + if (ret) + return ret; + + /* The lowest 4 bits are unsupported fractional bits */ + *value = exposure >> 4; + + return 0; +} + +static int ov5693_exposure_configure(struct ov5693_device *ov5693, + u32 exposure) +{ + int ret = 0; + + exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK; + + ov5693_write_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, exposure, &ret); + + return ret; +} + +static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain) +{ + u32 value; + int ret; + + ret = ov5693_read_reg(ov5693, OV5693_GAIN_CTRL_REG, &value); + if (ret) + return ret; + + /* As with exposure, the lowest 4 bits are fractional bits. */ + *gain = value >> 4; + + return ret; +} + +static int ov5693_digital_gain_configure(struct ov5693_device *ov5693, + u32 gain) +{ + int ret = 0; + + gain &= OV5693_MWB_GAIN_MASK; + + ov5693_write_reg(ov5693, OV5693_MWB_RED_GAIN_REG, gain, &ret); + ov5693_write_reg(ov5693, OV5693_MWB_GREEN_GAIN_REG, gain, &ret); + ov5693_write_reg(ov5693, OV5693_MWB_BLUE_GAIN_REG, gain, &ret); + + return ret; +} + +static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain) +{ + int ret = 0; + + gain = (gain << 4) & OV5693_GAIN_CTRL_MASK; + + ov5693_write_reg(ov5693, OV5693_GAIN_CTRL_REG, gain, &ret); + + return ret; +} + +static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank) +{ + u16 vts = ov5693->mode.format.height + vblank; + int ret = 0; + + ov5693_write_reg(ov5693, OV5693_TIMING_VTS_REG, vts, &ret); + + return ret; +} + +static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx) +{ + int ret = 0; + + ov5693_write_reg(ov5693, OV5693_TEST_PATTERN_REG, + ov5693_test_pattern_bits[idx], &ret); + + return ret; +} + +static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct ov5693_device *ov5693 = + container_of(ctrl->handler, struct ov5693_device, ctrls.handler); + int ret = 0; + + /* If VBLANK is altered we need to update exposure to compensate */ + if (ctrl->id == V4L2_CID_VBLANK) { + int exposure_max; + + exposure_max = ov5693->mode.format.height + ctrl->val - + OV5693_INTEGRATION_TIME_MARGIN; + __v4l2_ctrl_modify_range(ov5693->ctrls.exposure, + ov5693->ctrls.exposure->minimum, + exposure_max, + ov5693->ctrls.exposure->step, + min(ov5693->ctrls.exposure->val, + exposure_max)); + } + + /* Only apply changes to the controls if the device is powered up */ + if (!pm_runtime_get_if_in_use(ov5693->dev)) + return 0; + + switch (ctrl->id) { + case V4L2_CID_EXPOSURE: + ret = ov5693_exposure_configure(ov5693, ctrl->val); + break; + case V4L2_CID_ANALOGUE_GAIN: + ret = ov5693_analog_gain_configure(ov5693, ctrl->val); + break; + case V4L2_CID_DIGITAL_GAIN: + ret = ov5693_digital_gain_configure(ov5693, ctrl->val); + break; + case V4L2_CID_HFLIP: + ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val); + break; + case V4L2_CID_VFLIP: + ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val); + break; + case V4L2_CID_VBLANK: + ret = ov5693_vts_configure(ov5693, ctrl->val); + break; + case V4L2_CID_TEST_PATTERN: + ret = ov5693_test_pattern_configure(ov5693, ctrl->val); + break; + default: + ret = -EINVAL; + } + + pm_runtime_put(ov5693->dev); + + return ret; +} + +static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl) +{ + struct ov5693_device *ov5693 = container_of(ctrl->handler, + struct ov5693_device, + ctrls.handler); + + switch (ctrl->id) { + case V4L2_CID_EXPOSURE_ABSOLUTE: + return ov5693_get_exposure(ov5693, &ctrl->val); + case V4L2_CID_AUTOGAIN: + return ov5693_get_gain(ov5693, &ctrl->val); + default: + return -EINVAL; + } +} + +static const struct v4l2_ctrl_ops ov5693_ctrl_ops = { + .s_ctrl = ov5693_s_ctrl, + .g_volatile_ctrl = ov5693_g_volatile_ctrl +}; + +/* System Control Functions */ + +static int ov5693_mode_configure(struct ov5693_device *ov5693) +{ + const struct ov5693_mode *mode = &ov5693->mode; + int ret = 0; + + /* Crop Start X */ + ov5693_write_reg(ov5693, OV5693_CROP_START_X_REG, mode->crop.left, + &ret); + + /* Offset X */ + ov5693_write_reg(ov5693, OV5693_OFFSET_START_X_REG, 0, &ret); + + /* Output Size X */ + ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_X_REG, mode->format.width, + &ret); + + /* Crop End X */ + ov5693_write_reg(ov5693, OV5693_CROP_END_X_REG, + mode->crop.left + mode->crop.width, &ret); + + /* Horizontal Total Size */ + ov5693_write_reg(ov5693, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL, + &ret); + + /* Crop Start Y */ + ov5693_write_reg(ov5693, OV5693_CROP_START_Y_REG, mode->crop.top, + &ret); + + /* Offset Y */ + ov5693_write_reg(ov5693, OV5693_OFFSET_START_Y_REG, 0, &ret); + + /* Output Size Y */ + ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height, + &ret); + + /* Crop End Y */ + ov5693_write_reg(ov5693, OV5693_CROP_END_Y_REG, + mode->crop.top + mode->crop.height, &ret); + + /* Subsample X increase */ + ov5693_write_reg(ov5693, OV5693_SUB_INC_X_REG, + ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret); + /* Subsample Y increase */ + ov5693_write_reg(ov5693, OV5693_SUB_INC_Y_REG, + ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret); + + if (ret) + return ret; + + /* Binning */ + ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG, + OV5693_FORMAT1_VBIN_EN, + mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0); + if (ret) + return ret; + + ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG, + OV5693_FORMAT2_HBIN_EN, + mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0); + + return ret; +} + +static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable) +{ + int ret = 0; + + ov5693_write_reg(ov5693, OV5693_SW_STREAM_REG, + enable ? OV5693_START_STREAMING : + OV5693_STOP_STREAMING, &ret); + + return ret; +} + +static int ov5693_sw_reset(struct ov5693_device *ov5693) +{ + int ret = 0; + + ov5693_write_reg(ov5693, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret); + + return ret; +} + +static int ov5693_sensor_init(struct ov5693_device *ov5693) +{ + int ret; + + ret = ov5693_sw_reset(ov5693); + if (ret) + return dev_err_probe(ov5693->dev, ret, + "software reset error\n"); + + ret = ov5693_write_reg_array(ov5693, &ov5693_global_setting); + if (ret) + return dev_err_probe(ov5693->dev, ret, + "global settings error\n"); + + ret = ov5693_mode_configure(ov5693); + if (ret) + return dev_err_probe(ov5693->dev, ret, + "mode configure error\n"); + + ret = ov5693_enable_streaming(ov5693, false); + if (ret) + dev_err(ov5693->dev, "stop streaming error\n"); + + return ret; +} + +static void ov5693_sensor_powerdown(struct ov5693_device *ov5693) +{ + gpiod_set_value_cansleep(ov5693->reset, 1); + gpiod_set_value_cansleep(ov5693->powerdown, 1); + + regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies); + + clk_disable_unprepare(ov5693->clk); +} + +static int ov5693_sensor_powerup(struct ov5693_device *ov5693) +{ + int ret; + + gpiod_set_value_cansleep(ov5693->reset, 1); + gpiod_set_value_cansleep(ov5693->powerdown, 1); + + ret = clk_prepare_enable(ov5693->clk); + if (ret) { + dev_err(ov5693->dev, "Failed to enable clk\n"); + goto fail_power; + } + + ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies); + if (ret) { + dev_err(ov5693->dev, "Failed to enable regulators\n"); + goto fail_power; + } + + gpiod_set_value_cansleep(ov5693->powerdown, 0); + gpiod_set_value_cansleep(ov5693->reset, 0); + + usleep_range(5000, 7500); + + return 0; + +fail_power: + ov5693_sensor_powerdown(ov5693); + return ret; +} + +static int __maybe_unused ov5693_sensor_suspend(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + + ov5693_sensor_powerdown(ov5693); + + return 0; +} + +static int __maybe_unused ov5693_sensor_resume(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + int ret; + + mutex_lock(&ov5693->lock); + + ret = ov5693_sensor_powerup(ov5693); + if (ret) + goto out_unlock; + + ret = ov5693_sensor_init(ov5693); + if (ret) { + dev_err(dev, "ov5693 sensor init failure\n"); + goto err_power; + } + + goto out_unlock; + +err_power: + ov5693_sensor_powerdown(ov5693); +out_unlock: + mutex_unlock(&ov5693->lock); + return ret; +} + +static int ov5693_detect(struct ov5693_device *ov5693) +{ + int ret; + u32 id; + + ret = ov5693_read_reg(ov5693, OV5693_REG_CHIP_ID, &id); + if (ret) + return ret; + + if (id != OV5693_CHIP_ID) + return dev_err_probe(ov5693->dev, -ENODEV, + "sensor ID mismatch. Found 0x%04x\n", id); + + return 0; +} + +/* V4L2 Framework callbacks */ + +static unsigned int __ov5693_calc_vts(u32 height) +{ + /* + * We need to set a sensible default VTS for whatever format height we + * happen to be given from set_fmt(). This function just targets + * an even multiple of 30fps. + */ + + unsigned int tgt_fps; + + tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30); + + return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2); +} + +static struct v4l2_mbus_framefmt * +__ov5693_get_pad_format(struct ov5693_device *ov5693, + struct v4l2_subdev_state *state, + unsigned int pad, enum v4l2_subdev_format_whence which) +{ + switch (which) { + case V4L2_SUBDEV_FORMAT_TRY: + return v4l2_subdev_get_try_format(&ov5693->sd, state, pad); + case V4L2_SUBDEV_FORMAT_ACTIVE: + return &ov5693->mode.format; + default: + return NULL; + } +} + +static struct v4l2_rect * +__ov5693_get_pad_crop(struct ov5693_device *ov5693, + struct v4l2_subdev_state *state, + unsigned int pad, enum v4l2_subdev_format_whence which) +{ + switch (which) { + case V4L2_SUBDEV_FORMAT_TRY: + return v4l2_subdev_get_try_crop(&ov5693->sd, state, pad); + case V4L2_SUBDEV_FORMAT_ACTIVE: + return &ov5693->mode.crop; + } + + return NULL; +} + +static int ov5693_get_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *format) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + + format->format = ov5693->mode.format; + + return 0; +} + +static int ov5693_set_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *format) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + const struct v4l2_rect *crop; + struct v4l2_mbus_framefmt *fmt; + unsigned int hratio, vratio; + unsigned int width, height; + unsigned int hblank; + int exposure_max; + int ret = 0; + + crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which); + + /* + * Align to two to simplify the binning calculations below, and clamp + * the requested format at the crop rectangle + */ + width = clamp_t(unsigned int, ALIGN(format->format.width, 2), + OV5693_MIN_CROP_WIDTH, crop->width); + height = clamp_t(unsigned int, ALIGN(format->format.height, 2), + OV5693_MIN_CROP_HEIGHT, crop->height); + + /* + * We can only support setting either the dimensions of the crop rect + * or those dimensions binned (separately) by a factor of two. + */ + hratio = clamp_t(unsigned int, + DIV_ROUND_CLOSEST(crop->width, width), 1, 2); + vratio = clamp_t(unsigned int, + DIV_ROUND_CLOSEST(crop->height, height), 1, 2); + + fmt = __ov5693_get_pad_format(ov5693, state, format->pad, + format->which); + + fmt->width = crop->width / hratio; + fmt->height = crop->height / vratio; + fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10; + + format->format = *fmt; + + if (format->which == V4L2_SUBDEV_FORMAT_TRY) + return ret; + + mutex_lock(&ov5693->lock); + + ov5693->mode.binning_x = hratio > 1 ? true : false; + ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1; + ov5693->mode.binning_y = vratio > 1 ? true : false; + ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1; + + ov5693->mode.vts = __ov5693_calc_vts(fmt->height); + + __v4l2_ctrl_modify_range(ov5693->ctrls.vblank, + OV5693_TIMING_MIN_VTS, + OV5693_TIMING_MAX_VTS - fmt->height, + 1, ov5693->mode.vts - fmt->height); + __v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank, + ov5693->mode.vts - fmt->height); + + hblank = OV5693_FIXED_PPL - fmt->width; + __v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1, + hblank); + + exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN; + __v4l2_ctrl_modify_range(ov5693->ctrls.exposure, + ov5693->ctrls.exposure->minimum, exposure_max, + ov5693->ctrls.exposure->step, + min(ov5693->ctrls.exposure->val, + exposure_max)); + + mutex_unlock(&ov5693->lock); + return ret; +} + +static int ov5693_get_selection(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_selection *sel) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + + switch (sel->target) { + case V4L2_SEL_TGT_CROP: + mutex_lock(&ov5693->lock); + sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad, + sel->which); + mutex_unlock(&ov5693->lock); + break; + case V4L2_SEL_TGT_NATIVE_SIZE: + sel->r.top = 0; + sel->r.left = 0; + sel->r.width = OV5693_NATIVE_WIDTH; + sel->r.height = OV5693_NATIVE_HEIGHT; + break; + case V4L2_SEL_TGT_CROP_BOUNDS: + case V4L2_SEL_TGT_CROP_DEFAULT: + sel->r.top = OV5693_ACTIVE_START_TOP; + sel->r.left = OV5693_ACTIVE_START_LEFT; + sel->r.width = OV5693_ACTIVE_WIDTH; + sel->r.height = OV5693_ACTIVE_HEIGHT; + break; + default: + return -EINVAL; + } + + return 0; +} + +static int ov5693_set_selection(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_selection *sel) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + struct v4l2_mbus_framefmt *format; + struct v4l2_rect *__crop; + struct v4l2_rect rect; + + if (sel->target != V4L2_SEL_TGT_CROP) + return -EINVAL; + + /* + * Clamp the boundaries of the crop rectangle to the size of the sensor + * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't + * disrupted. + */ + rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT, + OV5693_NATIVE_WIDTH); + rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP, + OV5693_NATIVE_HEIGHT); + rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), + OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH); + rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), + OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT); + + /* Make sure the crop rectangle isn't outside the bounds of the array */ + rect.width = min_t(unsigned int, rect.width, + OV5693_NATIVE_WIDTH - rect.left); + rect.height = min_t(unsigned int, rect.height, + OV5693_NATIVE_HEIGHT - rect.top); + + __crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which); + + if (rect.width != __crop->width || rect.height != __crop->height) { + /* + * Reset the output image size if the crop rectangle size has + * been modified. + */ + format = __ov5693_get_pad_format(ov5693, state, sel->pad, + sel->which); + format->width = rect.width; + format->height = rect.height; + } + + *__crop = rect; + sel->r = rect; + + return 0; +} + +static int ov5693_s_stream(struct v4l2_subdev *sd, int enable) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + int ret; + + if (enable) { + ret = pm_runtime_get_sync(ov5693->dev); + if (ret < 0) + goto err_power_down; + + mutex_lock(&ov5693->lock); + ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler); + if (ret) { + mutex_unlock(&ov5693->lock); + goto err_power_down; + } + + ret = ov5693_enable_streaming(ov5693, true); + mutex_unlock(&ov5693->lock); + } else { + mutex_lock(&ov5693->lock); + ret = ov5693_enable_streaming(ov5693, false); + mutex_unlock(&ov5693->lock); + } + if (ret) + goto err_power_down; + + ov5693->streaming = !!enable; + + if (!enable) + pm_runtime_put(ov5693->dev); + + return 0; +err_power_down: + pm_runtime_put_noidle(ov5693->dev); + return ret; +} + +static int ov5693_g_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_frame_interval *interval) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height + + ov5693->ctrls.vblank->val); + unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize); + + interval->interval.numerator = 1; + interval->interval.denominator = fps; + + return 0; +} + +static int ov5693_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_mbus_code_enum *code) +{ + /* Only a single mbus format is supported */ + if (code->index > 0) + return -EINVAL; + + code->code = MEDIA_BUS_FMT_SBGGR10_1X10; + return 0; +} + +static int ov5693_enum_frame_size(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_frame_size_enum *fse) +{ + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + struct v4l2_rect *__crop; + + if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10) + return -EINVAL; + + __crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which); + if (!__crop) + return -EINVAL; + + fse->min_width = __crop->width / (fse->index + 1); + fse->min_height = __crop->height / (fse->index + 1); + fse->max_width = fse->min_width; + fse->max_height = fse->min_height; + + return 0; +} + +static const struct v4l2_subdev_video_ops ov5693_video_ops = { + .s_stream = ov5693_s_stream, + .g_frame_interval = ov5693_g_frame_interval, +}; + +static const struct v4l2_subdev_pad_ops ov5693_pad_ops = { + .enum_mbus_code = ov5693_enum_mbus_code, + .enum_frame_size = ov5693_enum_frame_size, + .get_fmt = ov5693_get_fmt, + .set_fmt = ov5693_set_fmt, + .get_selection = ov5693_get_selection, + .set_selection = ov5693_set_selection, +}; + +static const struct v4l2_subdev_ops ov5693_ops = { + .video = &ov5693_video_ops, + .pad = &ov5693_pad_ops, +}; + +/* Sensor and Driver Configuration Functions */ + +static int ov5693_init_controls(struct ov5693_device *ov5693) +{ + const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops; + struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls; + struct v4l2_fwnode_device_properties props; + int vblank_max, vblank_def; + int exposure_max; + int hblank; + int ret; + + ret = v4l2_ctrl_handler_init(&ctrls->handler, 12); + if (ret) + return ret; + + /* link freq */ + ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler, + NULL, V4L2_CID_LINK_FREQ, + 0, 0, link_freq_menu_items); + if (ctrls->link_freq) + ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + /* pixel rate */ + ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL, + V4L2_CID_PIXEL_RATE, 0, + OV5693_PIXEL_RATE, 1, + OV5693_PIXEL_RATE); + + /* Exposure */ + exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN; + ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_EXPOSURE, + OV5693_EXPOSURE_MIN, exposure_max, + OV5693_EXPOSURE_STEP, exposure_max); + + /* Gain */ + ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler, + ops, V4L2_CID_ANALOGUE_GAIN, + OV5693_GAIN_MIN, + OV5693_GAIN_MAX, + OV5693_GAIN_STEP, + OV5693_GAIN_DEF); + + ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_DIGITAL_GAIN, + OV5693_DIGITAL_GAIN_MIN, + OV5693_DIGITAL_GAIN_MAX, + OV5693_DIGITAL_GAIN_STEP, + OV5693_DIGITAL_GAIN_DEF); + + /* Flip */ + ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_HFLIP, 0, 1, 1, 0); + + ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_VFLIP, 0, 1, 1, 0); + + hblank = OV5693_FIXED_PPL - ov5693->mode.format.width; + ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_HBLANK, hblank, + hblank, 1, hblank); + + if (ctrls->hblank) + ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height; + vblank_def = ov5693->mode.vts - ov5693->mode.format.height; + ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops, + V4L2_CID_VBLANK, + OV5693_TIMING_MIN_VTS, + vblank_max, 1, vblank_def); + + ctrls->test_pattern = v4l2_ctrl_new_std_menu_items( + &ctrls->handler, ops, + V4L2_CID_TEST_PATTERN, + ARRAY_SIZE(ov5693_test_pattern_menu) - 1, + 0, 0, ov5693_test_pattern_menu); + + if (ctrls->handler.error) { + dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n"); + ret = ctrls->handler.error; + goto err_free_handler; + } + + /* set properties from fwnode (e.g. rotation, orientation) */ + ret = v4l2_fwnode_device_parse(ov5693->dev, &props); + if (ret) + goto err_free_handler; + + ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops, + &props); + if (ret) + goto err_free_handler; + + /* Use same lock for controls as for everything else. */ + ctrls->handler.lock = &ov5693->lock; + ov5693->sd.ctrl_handler = &ctrls->handler; + + return 0; + +err_free_handler: + v4l2_ctrl_handler_free(&ctrls->handler); + return ret; +} + +static int ov5693_configure_gpios(struct ov5693_device *ov5693) +{ + ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset", + GPIOD_OUT_HIGH); + if (IS_ERR(ov5693->reset)) { + dev_err(ov5693->dev, "Error fetching reset GPIO\n"); + return PTR_ERR(ov5693->reset); + } + + ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown", + GPIOD_OUT_HIGH); + if (IS_ERR(ov5693->powerdown)) { + dev_err(ov5693->dev, "Error fetching powerdown GPIO\n"); + return PTR_ERR(ov5693->powerdown); + } + + return 0; +} + +static int ov5693_get_regulators(struct ov5693_device *ov5693) +{ + unsigned int i; + + for (i = 0; i < OV5693_NUM_SUPPLIES; i++) + ov5693->supplies[i].supply = ov5693_supply_names[i]; + + return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES, + ov5693->supplies); +} + +static int ov5693_check_hwcfg(struct ov5693_device *ov5693) +{ + struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev); + struct v4l2_fwnode_endpoint bus_cfg = { + .bus_type = V4L2_MBUS_CSI2_DPHY, + }; + struct fwnode_handle *endpoint; + unsigned int i; + int ret; + + endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL); + if (!endpoint) + return -EPROBE_DEFER; /* Could be provided by cio2-bridge */ + + ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg); + fwnode_handle_put(endpoint); + if (ret) + return ret; + + if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) { + dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported"); + ret = -EINVAL; + goto out_free_bus_cfg; + } + + if (!bus_cfg.nr_of_link_frequencies) { + dev_err(ov5693->dev, "no link frequencies defined\n"); + ret = -EINVAL; + goto out_free_bus_cfg; + } + + for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) + if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ) + break; + + if (i == bus_cfg.nr_of_link_frequencies) { + dev_err(ov5693->dev, "supported link freq %ull not found\n", + OV5693_LINK_FREQ_419_2MHZ); + ret = -EINVAL; + goto out_free_bus_cfg; + } + +out_free_bus_cfg: + v4l2_fwnode_endpoint_free(&bus_cfg); + + return ret; +} + +static int ov5693_probe(struct i2c_client *client) +{ + struct ov5693_device *ov5693; + u32 clk_rate; + int ret = 0; + + ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL); + if (!ov5693) + return -ENOMEM; + + ov5693->client = client; + ov5693->dev = &client->dev; + + ret = ov5693_check_hwcfg(ov5693); + if (ret) + return ret; + + mutex_init(&ov5693->lock); + + v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops); + + ov5693->clk = devm_clk_get(&client->dev, "xvclk"); + if (IS_ERR(ov5693->clk)) { + dev_err(&client->dev, "Error getting clock\n"); + return PTR_ERR(ov5693->clk); + } + + clk_rate = clk_get_rate(ov5693->clk); + if (clk_rate != OV5693_XVCLK_FREQ) + dev_warn(&client->dev, "Found clk freq %u, expected %u\n", + clk_rate, OV5693_XVCLK_FREQ); + + ret = ov5693_configure_gpios(ov5693); + if (ret) + return ret; + + ret = ov5693_get_regulators(ov5693); + if (ret) + return dev_err_probe(&client->dev, ret, + "Error fetching regulators\n"); + + ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + ov5693->pad.flags = MEDIA_PAD_FL_SOURCE; + ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; + + ov5693->mode.crop = ov5693_default_crop; + ov5693->mode.format = ov5693_default_fmt; + ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height); + + ret = ov5693_init_controls(ov5693); + if (ret) + return ret; + + ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad); + if (ret) + goto err_ctrl_handler_free; + + /* + * We need the driver to work in the event that pm runtime is disable in + * the kernel, so power up and verify the chip now. In the event that + * runtime pm is disabled this will leave the chip on, so that streaming + * will work. + */ + + ret = ov5693_sensor_powerup(ov5693); + if (ret) + goto err_media_entity_cleanup; + + ret = ov5693_detect(ov5693); + if (ret) + goto err_powerdown; + + pm_runtime_set_active(&client->dev); + pm_runtime_get_noresume(&client->dev); + pm_runtime_enable(&client->dev); + + ret = v4l2_async_register_subdev_sensor(&ov5693->sd); + if (ret) { + dev_err(&client->dev, "failed to register V4L2 subdev: %d", + ret); + goto err_pm_runtime; + } + + pm_runtime_set_autosuspend_delay(&client->dev, 1000); + pm_runtime_use_autosuspend(&client->dev); + pm_runtime_put_autosuspend(&client->dev); + + return ret; + +err_pm_runtime: + pm_runtime_disable(&client->dev); + pm_runtime_put_noidle(&client->dev); +err_powerdown: + ov5693_sensor_powerdown(ov5693); +err_media_entity_cleanup: + media_entity_cleanup(&ov5693->sd.entity); +err_ctrl_handler_free: + v4l2_ctrl_handler_free(&ov5693->ctrls.handler); + + return ret; +} + +static int ov5693_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct ov5693_device *ov5693 = to_ov5693_sensor(sd); + + v4l2_async_unregister_subdev(sd); + media_entity_cleanup(&ov5693->sd.entity); + v4l2_ctrl_handler_free(&ov5693->ctrls.handler); + mutex_destroy(&ov5693->lock); + + /* + * Disable runtime PM. In case runtime PM is disabled in the kernel, + * make sure to turn power off manually. + */ + pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) + ov5693_sensor_powerdown(ov5693); + pm_runtime_set_suspended(&client->dev); + + return 0; +} + +static const struct dev_pm_ops ov5693_pm_ops = { + SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL) +}; + +static const struct acpi_device_id ov5693_acpi_match[] = { + {"INT33BE"}, + {}, +}; +MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match); + +static struct i2c_driver ov5693_driver = { + .driver = { + .name = "ov5693", + .acpi_match_table = ov5693_acpi_match, + .pm = &ov5693_pm_ops, + }, + .probe_new = ov5693_probe, + .remove = ov5693_remove, +}; +module_i2c_driver(ov5693_driver); + +MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/ov8856.c b/drivers/media/i2c/ov8856.c index c6c6050cda1a..8785764b7a74 100644 --- a/drivers/media/i2c/ov8856.c +++ b/drivers/media/i2c/ov8856.c @@ -1445,6 +1445,9 @@ struct ov8856 { const struct ov8856_lane_cfg *priv_lane; u8 modes_size; + + /* True if the device has been identified */ + bool identified; }; struct ov8856_lane_cfg { @@ -1685,6 +1688,71 @@ static int ov8856_write_reg_list(struct ov8856 *ov8856, return 0; } +static int ov8856_identify_module(struct ov8856 *ov8856) +{ + struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd); + int ret; + u32 val; + + if (ov8856->identified) + return 0; + + ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID, + OV8856_REG_VALUE_24BIT, &val); + if (ret) + return ret; + + if (val != OV8856_CHIP_ID) { + dev_err(&client->dev, "chip id mismatch: %x!=%x", + OV8856_CHIP_ID, val); + return -ENXIO; + } + + ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT, + OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING); + if (ret) + return ret; + + ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL, + OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO); + if (ret) { + dev_err(&client->dev, "failed to set otp mode"); + return ret; + } + + ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL, + OV8856_REG_VALUE_08BIT, + OV8856_OTP_LOAD_CTRL_ENABLE); + if (ret) { + dev_err(&client->dev, "failed to enable load control"); + return ret; + } + + ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION, + OV8856_REG_VALUE_08BIT, &val); + if (ret) { + dev_err(&client->dev, "failed to read module revision"); + return ret; + } + + dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n", + val, + val == OV8856_2A_MODULE ? "2A" : + val == OV8856_1B_MODULE ? "1B" : "unknown revision", + client->addr); + + ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT, + OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY); + if (ret) { + dev_err(&client->dev, "failed to exit streaming mode"); + return ret; + } + + ov8856->identified = true; + + return 0; +} + static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain) { int ret; @@ -1969,6 +2037,10 @@ static int ov8856_start_streaming(struct ov8856 *ov8856) const struct ov8856_reg_list *reg_list; int link_freq_index, ret; + ret = ov8856_identify_module(ov8856); + if (ret) + return ret; + link_freq_index = ov8856->cur_mode->link_freq_index; reg_list = &ov8856->priv_lane->link_freq_configs[link_freq_index].reg_list; @@ -2276,65 +2348,6 @@ static const struct v4l2_subdev_internal_ops ov8856_internal_ops = { .open = ov8856_open, }; -static int ov8856_identify_module(struct ov8856 *ov8856) -{ - struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd); - int ret; - u32 val; - - ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID, - OV8856_REG_VALUE_24BIT, &val); - if (ret) - return ret; - - if (val != OV8856_CHIP_ID) { - dev_err(&client->dev, "chip id mismatch: %x!=%x", - OV8856_CHIP_ID, val); - return -ENXIO; - } - - ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT, - OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING); - if (ret) - return ret; - - ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL, - OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO); - if (ret) { - dev_err(&client->dev, "failed to set otp mode"); - return ret; - } - - ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL, - OV8856_REG_VALUE_08BIT, - OV8856_OTP_LOAD_CTRL_ENABLE); - if (ret) { - dev_err(&client->dev, "failed to enable load control"); - return ret; - } - - ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION, - OV8856_REG_VALUE_08BIT, &val); - if (ret) { - dev_err(&client->dev, "failed to read module revision"); - return ret; - } - - dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n", - val, - val == OV8856_2A_MODULE ? "2A" : - val == OV8856_1B_MODULE ? "1B" : "unknown revision", - client->addr); - - ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT, - OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY); - if (ret) { - dev_err(&client->dev, "failed to exit streaming mode"); - return ret; - } - - return 0; -} static int ov8856_get_hwcfg(struct ov8856 *ov8856, struct device *dev) { @@ -2458,6 +2471,7 @@ static int ov8856_probe(struct i2c_client *client) { struct ov8856 *ov8856; int ret; + bool full_power; ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL); if (!ov8856) @@ -2472,16 +2486,19 @@ static int ov8856_probe(struct i2c_client *client) v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops); - ret = __ov8856_power_on(ov8856); - if (ret) { - dev_err(&client->dev, "failed to power on\n"); - return ret; - } + full_power = acpi_dev_state_d0(&client->dev); + if (full_power) { + ret = __ov8856_power_on(ov8856); + if (ret) { + dev_err(&client->dev, "failed to power on\n"); + return ret; + } - ret = ov8856_identify_module(ov8856); - if (ret) { - dev_err(&client->dev, "failed to find sensor: %d", ret); - goto probe_power_off; + ret = ov8856_identify_module(ov8856); + if (ret) { + dev_err(&client->dev, "failed to find sensor: %d", ret); + goto probe_power_off; + } } mutex_init(&ov8856->mutex); @@ -2511,11 +2528,9 @@ static int ov8856_probe(struct i2c_client *client) goto probe_error_media_entity_cleanup; } - /* - * Device is already turned on by i2c-core with ACPI domain PM. - * Enable runtime PM and turn off the device. - */ - pm_runtime_set_active(&client->dev); + /* Set the device's state to active if it's in D0 state. */ + if (full_power) + pm_runtime_set_active(&client->dev); pm_runtime_enable(&client->dev); pm_runtime_idle(&client->dev); @@ -2562,6 +2577,7 @@ static struct i2c_driver ov8856_i2c_driver = { }, .probe_new = ov8856_probe, .remove = ov8856_remove, + .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE, }; module_i2c_driver(ov8856_i2c_driver); diff --git a/drivers/media/i2c/ov8865.c b/drivers/media/i2c/ov8865.c index ce50f3ea87b8..d9d016cfa9ac 100644 --- a/drivers/media/i2c/ov8865.c +++ b/drivers/media/i2c/ov8865.c @@ -9,6 +9,7 @@ #include <linux/delay.h> #include <linux/device.h> #include <linux/i2c.h> +#include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/of_graph.h> #include <linux/pm_runtime.h> @@ -20,10 +21,6 @@ #include <media/v4l2-image-sizes.h> #include <media/v4l2-mediabus.h> -/* Clock rate */ - -#define OV8865_EXTCLK_RATE 24000000 - /* Register definitions */ /* System */ @@ -146,6 +143,7 @@ #define OV8865_EXPOSURE_CTRL_L_REG 0x3502 #define OV8865_EXPOSURE_CTRL_L(v) ((v) & GENMASK(7, 0)) #define OV8865_EXPOSURE_GAIN_MANUAL_REG 0x3503 +#define OV8865_INTEGRATION_TIME_MARGIN 8 #define OV8865_GAIN_CTRL_H_REG 0x3508 #define OV8865_GAIN_CTRL_H(v) (((v) & GENMASK(12, 8)) >> 8) @@ -186,6 +184,8 @@ #define OV8865_VTS_H(v) (((v) & GENMASK(11, 8)) >> 8) #define OV8865_VTS_L_REG 0x380f #define OV8865_VTS_L(v) ((v) & GENMASK(7, 0)) +#define OV8865_TIMING_MAX_VTS 0xffff +#define OV8865_TIMING_MIN_VTS 0x04 #define OV8865_OFFSET_X_H_REG 0x3810 #define OV8865_OFFSET_X_H(v) (((v) & GENMASK(15, 8)) >> 8) #define OV8865_OFFSET_X_L_REG 0x3811 @@ -453,6 +453,15 @@ #define OV8865_PRE_CTRL0_PATTERN_COLOR_SQUARES 2 #define OV8865_PRE_CTRL0_PATTERN_BLACK 3 +/* Pixel Array */ + +#define OV8865_NATIVE_WIDTH 3296 +#define OV8865_NATIVE_HEIGHT 2528 +#define OV8865_ACTIVE_START_TOP 32 +#define OV8865_ACTIVE_START_LEFT 80 +#define OV8865_ACTIVE_WIDTH 3264 +#define OV8865_ACTIVE_HEIGHT 2448 + /* Macros */ #define ov8865_subdev_sensor(s) \ @@ -566,6 +575,25 @@ struct ov8865_sclk_config { unsigned int sclk_div; }; +struct ov8865_pll_configs { + const struct ov8865_pll1_config *pll1_config; + const struct ov8865_pll2_config *pll2_config_native; + const struct ov8865_pll2_config *pll2_config_binning; +}; + +/* Clock rate */ + +enum extclk_rate { + OV8865_19_2_MHZ, + OV8865_24_MHZ, + OV8865_NUM_SUPPORTED_RATES +}; + +static const unsigned long supported_extclk_rates[] = { + [OV8865_19_2_MHZ] = 19200000, + [OV8865_24_MHZ] = 24000000, +}; + /* * General formulas for (array-centered) mode calculation: * - photo_array_width = 3296 @@ -632,11 +660,7 @@ struct ov8865_mode { unsigned int blc_anchor_right_start; unsigned int blc_anchor_right_end; - struct v4l2_fract frame_interval; - - const struct ov8865_pll1_config *pll1_config; - const struct ov8865_pll2_config *pll2_config; - const struct ov8865_sclk_config *sclk_config; + bool pll2_binning; const struct ov8865_register_value *register_values; unsigned int register_values_count; @@ -652,6 +676,9 @@ struct ov8865_state { struct ov8865_ctrls { struct v4l2_ctrl *link_freq; struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *exposure; struct v4l2_ctrl_handler handler; }; @@ -664,6 +691,9 @@ struct ov8865_sensor { struct regulator *avdd; struct regulator *dvdd; struct regulator *dovdd; + + unsigned long extclk_rate; + const struct ov8865_pll_configs *pll_configs; struct clk *extclk; struct v4l2_fwnode_endpoint endpoint; @@ -679,43 +709,70 @@ struct ov8865_sensor { /* Static definitions */ /* - * EXTCLK = 24 MHz * PHY_SCLK = 720 MHz * MIPI_PCLK = 90 MHz */ -static const struct ov8865_pll1_config ov8865_pll1_config_native = { - .pll_pre_div_half = 1, - .pll_pre_div = 0, - .pll_mul = 30, - .m_div = 1, - .mipi_div = 3, - .pclk_div = 1, - .sys_pre_div = 1, - .sys_div = 2, + +static const struct ov8865_pll1_config ov8865_pll1_config_native_19_2mhz = { + .pll_pre_div_half = 1, + .pll_pre_div = 2, + .pll_mul = 75, + .m_div = 1, + .mipi_div = 3, + .pclk_div = 1, + .sys_pre_div = 1, + .sys_div = 2, +}; + +static const struct ov8865_pll1_config ov8865_pll1_config_native_24mhz = { + .pll_pre_div_half = 1, + .pll_pre_div = 0, + .pll_mul = 30, + .m_div = 1, + .mipi_div = 3, + .pclk_div = 1, + .sys_pre_div = 1, + .sys_div = 2, }; /* - * EXTCLK = 24 MHz * DAC_CLK = 360 MHz * SCLK = 144 MHz */ -static const struct ov8865_pll2_config ov8865_pll2_config_native = { - .pll_pre_div_half = 1, - .pll_pre_div = 0, - .pll_mul = 30, - .dac_div = 2, - .sys_pre_div = 5, - .sys_div = 0, +static const struct ov8865_pll2_config ov8865_pll2_config_native_19_2mhz = { + .pll_pre_div_half = 1, + .pll_pre_div = 5, + .pll_mul = 75, + .dac_div = 1, + .sys_pre_div = 1, + .sys_div = 3, +}; + +static const struct ov8865_pll2_config ov8865_pll2_config_native_24mhz = { + .pll_pre_div_half = 1, + .pll_pre_div = 0, + .pll_mul = 30, + .dac_div = 2, + .sys_pre_div = 5, + .sys_div = 0, }; /* - * EXTCLK = 24 MHz * DAC_CLK = 360 MHz - * SCLK = 80 MHz + * SCLK = 72 MHz */ -static const struct ov8865_pll2_config ov8865_pll2_config_binning = { +static const struct ov8865_pll2_config ov8865_pll2_config_binning_19_2mhz = { + .pll_pre_div_half = 1, + .pll_pre_div = 2, + .pll_mul = 75, + .dac_div = 2, + .sys_pre_div = 10, + .sys_div = 0, +}; + +static const struct ov8865_pll2_config ov8865_pll2_config_binning_24mhz = { .pll_pre_div_half = 1, .pll_pre_div = 0, .pll_mul = 30, @@ -724,6 +781,23 @@ static const struct ov8865_pll2_config ov8865_pll2_config_binning = { .sys_div = 0, }; +static const struct ov8865_pll_configs ov8865_pll_configs_19_2mhz = { + .pll1_config = &ov8865_pll1_config_native_19_2mhz, + .pll2_config_native = &ov8865_pll2_config_native_19_2mhz, + .pll2_config_binning = &ov8865_pll2_config_binning_19_2mhz, +}; + +static const struct ov8865_pll_configs ov8865_pll_configs_24mhz = { + .pll1_config = &ov8865_pll1_config_native_24mhz, + .pll2_config_native = &ov8865_pll2_config_native_24mhz, + .pll2_config_binning = &ov8865_pll2_config_binning_24mhz, +}; + +static const struct ov8865_pll_configs *ov8865_pll_configs[] = { + &ov8865_pll_configs_19_2mhz, + &ov8865_pll_configs_24mhz, +}; + static const struct ov8865_sclk_config ov8865_sclk_config_native = { .sys_sel = 1, .sclk_sel = 0, @@ -890,7 +964,7 @@ static const struct ov8865_mode ov8865_modes[] = { { /* Horizontal */ .output_size_x = 3264, - .hts = 1944, + .hts = 3888, /* Vertical */ .output_size_y = 2448, @@ -929,13 +1003,8 @@ static const struct ov8865_mode ov8865_modes[] = { .blc_anchor_right_start = 1984, .blc_anchor_right_end = 2239, - /* Frame Interval */ - .frame_interval = { 1, 30 }, - /* PLL */ - .pll1_config = &ov8865_pll1_config_native, - .pll2_config = &ov8865_pll2_config_native, - .sclk_config = &ov8865_sclk_config_native, + .pll2_binning = false, /* Registers */ .register_values = ov8865_register_values_native, @@ -946,11 +1015,11 @@ static const struct ov8865_mode ov8865_modes[] = { { /* Horizontal */ .output_size_x = 3264, - .hts = 2582, + .hts = 3888, /* Vertical */ .output_size_y = 1836, - .vts = 2002, + .vts = 2470, .size_auto = true, .size_auto_boundary_x = 8, @@ -985,13 +1054,8 @@ static const struct ov8865_mode ov8865_modes[] = { .blc_anchor_right_start = 1984, .blc_anchor_right_end = 2239, - /* Frame Interval */ - .frame_interval = { 1, 30 }, - /* PLL */ - .pll1_config = &ov8865_pll1_config_native, - .pll2_config = &ov8865_pll2_config_native, - .sclk_config = &ov8865_sclk_config_native, + .pll2_binning = false, /* Registers */ .register_values = ov8865_register_values_native, @@ -1045,13 +1109,8 @@ static const struct ov8865_mode ov8865_modes[] = { .blc_anchor_right_start = 992, .blc_anchor_right_end = 1119, - /* Frame Interval */ - .frame_interval = { 1, 30 }, - /* PLL */ - .pll1_config = &ov8865_pll1_config_native, - .pll2_config = &ov8865_pll2_config_binning, - .sclk_config = &ov8865_sclk_config_native, + .pll2_binning = true, /* Registers */ .register_values = ov8865_register_values_binning, @@ -1111,13 +1170,8 @@ static const struct ov8865_mode ov8865_modes[] = { .blc_anchor_right_start = 992, .blc_anchor_right_end = 1119, - /* Frame Interval */ - .frame_interval = { 1, 90 }, - /* PLL */ - .pll1_config = &ov8865_pll1_config_native, - .pll2_config = &ov8865_pll2_config_binning, - .sclk_config = &ov8865_sclk_config_native, + .pll2_binning = true, /* Registers */ .register_values = ov8865_register_values_binning, @@ -1512,12 +1566,11 @@ static int ov8865_isp_configure(struct ov8865_sensor *sensor) static unsigned long ov8865_mode_pll1_rate(struct ov8865_sensor *sensor, const struct ov8865_mode *mode) { - const struct ov8865_pll1_config *config = mode->pll1_config; - unsigned long extclk_rate; + const struct ov8865_pll1_config *config; unsigned long pll1_rate; - extclk_rate = clk_get_rate(sensor->extclk); - pll1_rate = extclk_rate * config->pll_mul / config->pll_pre_div_half; + config = sensor->pll_configs->pll1_config; + pll1_rate = sensor->extclk_rate * config->pll_mul / config->pll_pre_div_half; switch (config->pll_pre_div) { case 0: @@ -1551,10 +1604,12 @@ static int ov8865_mode_pll1_configure(struct ov8865_sensor *sensor, const struct ov8865_mode *mode, u32 mbus_code) { - const struct ov8865_pll1_config *config = mode->pll1_config; + const struct ov8865_pll1_config *config; u8 value; int ret; + config = sensor->pll_configs->pll1_config; + switch (mbus_code) { case MEDIA_BUS_FMT_SBGGR10_1X10: value = OV8865_MIPI_BIT_SEL(10); @@ -1621,9 +1676,12 @@ static int ov8865_mode_pll1_configure(struct ov8865_sensor *sensor, static int ov8865_mode_pll2_configure(struct ov8865_sensor *sensor, const struct ov8865_mode *mode) { - const struct ov8865_pll2_config *config = mode->pll2_config; + const struct ov8865_pll2_config *config; int ret; + config = mode->pll2_binning ? sensor->pll_configs->pll2_config_binning : + sensor->pll_configs->pll2_config_native; + ret = ov8865_write(sensor, OV8865_PLL_CTRL12_REG, OV8865_PLL_CTRL12_PRE_DIV_HALF(config->pll_pre_div_half) | OV8865_PLL_CTRL12_DAC_DIV(config->dac_div)); @@ -1657,7 +1715,7 @@ static int ov8865_mode_pll2_configure(struct ov8865_sensor *sensor, static int ov8865_mode_sclk_configure(struct ov8865_sensor *sensor, const struct ov8865_mode *mode) { - const struct ov8865_sclk_config *config = mode->sclk_config; + const struct ov8865_sclk_config *config = &ov8865_sclk_config_native; int ret; ret = ov8865_write(sensor, OV8865_CLK_SEL0_REG, @@ -2052,9 +2110,11 @@ static int ov8865_mode_configure(struct ov8865_sensor *sensor, static unsigned long ov8865_mode_mipi_clk_rate(struct ov8865_sensor *sensor, const struct ov8865_mode *mode) { - const struct ov8865_pll1_config *config = mode->pll1_config; + const struct ov8865_pll1_config *config; unsigned long pll1_rate; + config = sensor->pll_configs->pll1_config; + pll1_rate = ov8865_mode_pll1_rate(sensor, mode); return pll1_rate / config->m_div / 2; @@ -2066,6 +2126,9 @@ static int ov8865_exposure_configure(struct ov8865_sensor *sensor, u32 exposure) { int ret; + /* The sensor stores exposure in units of 1/16th of a line */ + exposure *= 16; + ret = ov8865_write(sensor, OV8865_EXPOSURE_CTRL_HH_REG, OV8865_EXPOSURE_CTRL_HH(exposure)); if (ret) @@ -2082,7 +2145,7 @@ static int ov8865_exposure_configure(struct ov8865_sensor *sensor, u32 exposure) /* Gain */ -static int ov8865_gain_configure(struct ov8865_sensor *sensor, u32 gain) +static int ov8865_analog_gain_configure(struct ov8865_sensor *sensor, u32 gain) { int ret; @@ -2157,6 +2220,20 @@ static int ov8865_test_pattern_configure(struct ov8865_sensor *sensor, ov8865_test_pattern_bits[index]); } +/* Blanking */ + +static int ov8865_vts_configure(struct ov8865_sensor *sensor, u32 vblank) +{ + u16 vts = sensor->state.mode->output_size_y + vblank; + int ret; + + ret = ov8865_write(sensor, OV8865_VTS_H_REG, OV8865_VTS_H(vts)); + if (ret) + return ret; + + return ov8865_write(sensor, OV8865_VTS_L_REG, OV8865_VTS_L(vts)); +} + /* State */ static int ov8865_state_mipi_configure(struct ov8865_sensor *sensor, @@ -2330,27 +2407,27 @@ static int ov8865_sensor_power(struct ov8865_sensor *sensor, bool on) if (ret) { dev_err(sensor->dev, "failed to enable DOVDD regulator\n"); - goto disable; + return ret; } ret = regulator_enable(sensor->avdd); if (ret) { dev_err(sensor->dev, "failed to enable AVDD regulator\n"); - goto disable; + goto disable_dovdd; } ret = regulator_enable(sensor->dvdd); if (ret) { dev_err(sensor->dev, "failed to enable DVDD regulator\n"); - goto disable; + goto disable_avdd; } ret = clk_prepare_enable(sensor->extclk); if (ret) { dev_err(sensor->dev, "failed to enable EXTCLK clock\n"); - goto disable; + goto disable_dvdd; } gpiod_set_value_cansleep(sensor->reset, 0); @@ -2359,14 +2436,16 @@ static int ov8865_sensor_power(struct ov8865_sensor *sensor, bool on) /* Time to enter streaming mode according to power timings. */ usleep_range(10000, 12000); } else { -disable: gpiod_set_value_cansleep(sensor->powerdown, 1); gpiod_set_value_cansleep(sensor->reset, 1); clk_disable_unprepare(sensor->extclk); +disable_dvdd: regulator_disable(sensor->dvdd); +disable_avdd: regulator_disable(sensor->avdd); +disable_dovdd: regulator_disable(sensor->dovdd); } @@ -2382,6 +2461,20 @@ static int ov8865_s_ctrl(struct v4l2_ctrl *ctrl) unsigned int index; int ret; + /* If VBLANK is altered we need to update exposure to compensate */ + if (ctrl->id == V4L2_CID_VBLANK) { + int exposure_max; + + exposure_max = sensor->state.mode->output_size_y + ctrl->val - + OV8865_INTEGRATION_TIME_MARGIN; + __v4l2_ctrl_modify_range(sensor->ctrls.exposure, + sensor->ctrls.exposure->minimum, + exposure_max, + sensor->ctrls.exposure->step, + min(sensor->ctrls.exposure->val, + exposure_max)); + } + /* Wait for the sensor to be on before setting controls. */ if (pm_runtime_suspended(sensor->dev)) return 0; @@ -2392,8 +2485,8 @@ static int ov8865_s_ctrl(struct v4l2_ctrl *ctrl) if (ret) return ret; break; - case V4L2_CID_GAIN: - ret = ov8865_gain_configure(sensor, ctrl->val); + case V4L2_CID_ANALOGUE_GAIN: + ret = ov8865_analog_gain_configure(sensor, ctrl->val); if (ret) return ret; break; @@ -2408,6 +2501,8 @@ static int ov8865_s_ctrl(struct v4l2_ctrl *ctrl) case V4L2_CID_TEST_PATTERN: index = (unsigned int)ctrl->val; return ov8865_test_pattern_configure(sensor, index); + case V4L2_CID_VBLANK: + return ov8865_vts_configure(sensor, ctrl->val); default: return -EINVAL; } @@ -2424,6 +2519,10 @@ static int ov8865_ctrls_init(struct ov8865_sensor *sensor) struct ov8865_ctrls *ctrls = &sensor->ctrls; struct v4l2_ctrl_handler *handler = &ctrls->handler; const struct v4l2_ctrl_ops *ops = &ov8865_ctrl_ops; + const struct ov8865_mode *mode = &ov8865_modes[0]; + struct v4l2_fwnode_device_properties props; + unsigned int vblank_max, vblank_def; + unsigned int hblank; int ret; v4l2_ctrl_handler_init(handler, 32); @@ -2433,12 +2532,13 @@ static int ov8865_ctrls_init(struct ov8865_sensor *sensor) /* Exposure */ - v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 16, 1048575, 16, - 512); + ctrls->exposure = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 2, + 65535, 1, 32); /* Gain */ - v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 128, 8191, 128, 128); + v4l2_ctrl_new_std(handler, ops, V4L2_CID_ANALOGUE_GAIN, 128, 2048, 128, + 128); /* White Balance */ @@ -2459,6 +2559,20 @@ static int ov8865_ctrls_init(struct ov8865_sensor *sensor) ARRAY_SIZE(ov8865_test_pattern_menu) - 1, 0, 0, ov8865_test_pattern_menu); + /* Blanking */ + hblank = mode->hts - mode->output_size_x; + ctrls->hblank = v4l2_ctrl_new_std(handler, ops, V4L2_CID_HBLANK, hblank, + hblank, 1, hblank); + + if (ctrls->hblank) + ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + vblank_max = OV8865_TIMING_MAX_VTS - mode->output_size_y; + vblank_def = mode->vts - mode->output_size_y; + ctrls->vblank = v4l2_ctrl_new_std(handler, ops, V4L2_CID_VBLANK, + OV8865_TIMING_MIN_VTS, vblank_max, 1, + vblank_def); + /* MIPI CSI-2 */ ctrls->link_freq = @@ -2470,6 +2584,15 @@ static int ov8865_ctrls_init(struct ov8865_sensor *sensor) v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1); + /* set properties from fwnode (e.g. rotation, orientation) */ + ret = v4l2_fwnode_device_parse(sensor->dev, &props); + if (ret) + goto error_ctrls; + + ret = v4l2_ctrl_new_fwnode_properties(handler, ops, &props); + if (ret) + goto error_ctrls; + if (handler->error) { ret = handler->error; goto error_ctrls; @@ -2522,11 +2645,18 @@ static int ov8865_g_frame_interval(struct v4l2_subdev *subdev, { struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev); const struct ov8865_mode *mode; + unsigned int framesize; + unsigned int fps; mutex_lock(&sensor->mutex); mode = sensor->state.mode; - interval->interval = mode->frame_interval; + framesize = mode->hts * (mode->output_size_y + + sensor->ctrls.vblank->val); + fps = DIV_ROUND_CLOSEST(sensor->ctrls.pixel_rate->val, framesize); + + interval->interval.numerator = 1; + interval->interval.denominator = fps; mutex_unlock(&sensor->mutex); @@ -2599,7 +2729,9 @@ static int ov8865_set_fmt(struct v4l2_subdev *subdev, struct v4l2_mbus_framefmt *mbus_format = &format->format; const struct ov8865_mode *mode; u32 mbus_code = 0; + unsigned int hblank; unsigned int index; + int exposure_max; int ret = 0; mutex_lock(&sensor->mutex); @@ -2639,6 +2771,21 @@ static int ov8865_set_fmt(struct v4l2_subdev *subdev, sensor->state.mbus_code != mbus_code) ret = ov8865_state_configure(sensor, mode, mbus_code); + __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV8865_TIMING_MIN_VTS, + OV8865_TIMING_MAX_VTS - mode->output_size_y, + 1, mode->vts - mode->output_size_y); + + hblank = mode->hts - mode->output_size_x; + __v4l2_ctrl_modify_range(sensor->ctrls.hblank, hblank, hblank, 1, + hblank); + + exposure_max = mode->vts - OV8865_INTEGRATION_TIME_MARGIN; + __v4l2_ctrl_modify_range(sensor->ctrls.exposure, + sensor->ctrls.exposure->minimum, exposure_max, + sensor->ctrls.exposure->step, + min(sensor->ctrls.exposure->val, + exposure_max)); + complete: mutex_unlock(&sensor->mutex); @@ -2662,37 +2809,55 @@ static int ov8865_enum_frame_size(struct v4l2_subdev *subdev, return 0; } -static int ov8865_enum_frame_interval(struct v4l2_subdev *subdev, - struct v4l2_subdev_state *sd_state, - struct v4l2_subdev_frame_interval_enum *interval_enum) +static void +__ov8865_get_pad_crop(struct ov8865_sensor *sensor, + struct v4l2_subdev_state *state, unsigned int pad, + enum v4l2_subdev_format_whence which, struct v4l2_rect *r) { - const struct ov8865_mode *mode = NULL; - unsigned int mode_index; - unsigned int interval_index; - - if (interval_enum->index > 0) - return -EINVAL; - /* - * Multiple modes with the same dimensions may have different frame - * intervals, so look up each relevant mode. - */ - for (mode_index = 0, interval_index = 0; - mode_index < ARRAY_SIZE(ov8865_modes); mode_index++) { - mode = &ov8865_modes[mode_index]; - - if (mode->output_size_x == interval_enum->width && - mode->output_size_y == interval_enum->height) { - if (interval_index == interval_enum->index) - break; + const struct ov8865_mode *mode = sensor->state.mode; - interval_index++; - } + switch (which) { + case V4L2_SUBDEV_FORMAT_TRY: + *r = *v4l2_subdev_get_try_crop(&sensor->subdev, state, pad); + break; + case V4L2_SUBDEV_FORMAT_ACTIVE: + r->height = mode->output_size_y; + r->width = mode->output_size_x; + r->top = (OV8865_NATIVE_HEIGHT - mode->output_size_y) / 2; + r->left = (OV8865_NATIVE_WIDTH - mode->output_size_x) / 2; + break; } +} - if (mode_index == ARRAY_SIZE(ov8865_modes)) - return -EINVAL; +static int ov8865_get_selection(struct v4l2_subdev *subdev, + struct v4l2_subdev_state *state, + struct v4l2_subdev_selection *sel) +{ + struct ov8865_sensor *sensor = ov8865_subdev_sensor(subdev); - interval_enum->interval = mode->frame_interval; + switch (sel->target) { + case V4L2_SEL_TGT_CROP: + mutex_lock(&sensor->mutex); + __ov8865_get_pad_crop(sensor, state, sel->pad, + sel->which, &sel->r); + mutex_unlock(&sensor->mutex); + break; + case V4L2_SEL_TGT_NATIVE_SIZE: + sel->r.top = 0; + sel->r.left = 0; + sel->r.width = OV8865_NATIVE_WIDTH; + sel->r.height = OV8865_NATIVE_HEIGHT; + break; + case V4L2_SEL_TGT_CROP_BOUNDS: + case V4L2_SEL_TGT_CROP_DEFAULT: + sel->r.top = OV8865_ACTIVE_START_TOP; + sel->r.left = OV8865_ACTIVE_START_LEFT; + sel->r.width = OV8865_ACTIVE_WIDTH; + sel->r.height = OV8865_ACTIVE_HEIGHT; + break; + default: + return -EINVAL; + } return 0; } @@ -2702,7 +2867,8 @@ static const struct v4l2_subdev_pad_ops ov8865_subdev_pad_ops = { .get_fmt = ov8865_get_fmt, .set_fmt = ov8865_set_fmt, .enum_frame_size = ov8865_enum_frame_size, - .enum_frame_interval = ov8865_enum_frame_interval, + .get_selection = ov8865_get_selection, + .set_selection = ov8865_get_selection, }; static const struct v4l2_subdev_ops ov8865_subdev_ops = { @@ -2782,7 +2948,8 @@ static int ov8865_probe(struct i2c_client *client) struct ov8865_sensor *sensor; struct v4l2_subdev *subdev; struct media_pad *pad; - unsigned long rate; + unsigned int rate = 0; + unsigned int i; int ret; sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); @@ -2792,13 +2959,31 @@ static int ov8865_probe(struct i2c_client *client) sensor->dev = dev; sensor->i2c_client = client; + /* Regulators */ + + /* DVDD: digital core */ + sensor->dvdd = devm_regulator_get(dev, "dvdd"); + if (IS_ERR(sensor->dvdd)) + return dev_err_probe(dev, PTR_ERR(sensor->dvdd), + "cannot get DVDD regulator\n"); + + /* DOVDD: digital I/O */ + sensor->dovdd = devm_regulator_get(dev, "dovdd"); + if (IS_ERR(sensor->dovdd)) + return dev_err_probe(dev, PTR_ERR(sensor->dovdd), + "cannot get DOVDD regulator\n"); + + /* AVDD: analog */ + sensor->avdd = devm_regulator_get(dev, "avdd"); + if (IS_ERR(sensor->avdd)) + return dev_err_probe(dev, PTR_ERR(sensor->avdd), + "cannot get AVDD (analog) regulator\n"); + /* Graph Endpoint */ handle = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); - if (!handle) { - dev_err(dev, "unable to find endpoint node\n"); - return -EINVAL; - } + if (!handle) + return -EPROBE_DEFER; sensor->endpoint.bus_type = V4L2_MBUS_CSI2_DPHY; @@ -2824,48 +3009,54 @@ static int ov8865_probe(struct i2c_client *client) goto error_endpoint; } - /* Regulators */ - - /* DVDD: digital core */ - sensor->dvdd = devm_regulator_get(dev, "dvdd"); - if (IS_ERR(sensor->dvdd)) { - dev_err(dev, "cannot get DVDD (digital core) regulator\n"); - ret = PTR_ERR(sensor->dvdd); - goto error_endpoint; - } + /* External Clock */ - /* DOVDD: digital I/O */ - sensor->dovdd = devm_regulator_get(dev, "dovdd"); - if (IS_ERR(sensor->dovdd)) { - dev_err(dev, "cannot get DOVDD (digital I/O) regulator\n"); - ret = PTR_ERR(sensor->dovdd); + sensor->extclk = devm_clk_get(dev, NULL); + if (PTR_ERR(sensor->extclk) == -ENOENT) { + dev_info(dev, "no external clock found, continuing...\n"); + sensor->extclk = NULL; + } else if (IS_ERR(sensor->extclk)) { + dev_err(dev, "failed to get external clock\n"); + ret = PTR_ERR(sensor->extclk); goto error_endpoint; } - /* AVDD: analog */ - sensor->avdd = devm_regulator_get(dev, "avdd"); - if (IS_ERR(sensor->avdd)) { - dev_err(dev, "cannot get AVDD (analog) regulator\n"); - ret = PTR_ERR(sensor->avdd); - goto error_endpoint; + /* + * We could have either a 24MHz or 19.2MHz clock rate from either dt or + * ACPI...but we also need to support the weird IPU3 case which will + * have an external clock AND a clock-frequency property. Check for the + * clock-frequency property and if found, set that rate if we managed + * to acquire a clock. This should cover the ACPI case. If the system + * uses devicetree then the configured rate should already be set, so + * we can just read it. + */ + ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", + &rate); + if (!ret && sensor->extclk) { + ret = clk_set_rate(sensor->extclk, rate); + if (ret) + return dev_err_probe(dev, ret, + "failed to set clock rate\n"); + } else if (ret && !sensor->extclk) { + return dev_err_probe(dev, ret, "invalid clock config\n"); } - /* External Clock */ + sensor->extclk_rate = rate ? rate : clk_get_rate(sensor->extclk); - sensor->extclk = devm_clk_get(dev, NULL); - if (IS_ERR(sensor->extclk)) { - dev_err(dev, "failed to get external clock\n"); - ret = PTR_ERR(sensor->extclk); - goto error_endpoint; + for (i = 0; i < ARRAY_SIZE(supported_extclk_rates); i++) { + if (sensor->extclk_rate == supported_extclk_rates[i]) + break; } - rate = clk_get_rate(sensor->extclk); - if (rate != OV8865_EXTCLK_RATE) { - dev_err(dev, "clock rate %lu Hz is unsupported\n", rate); + if (i == ARRAY_SIZE(supported_extclk_rates)) { + dev_err(dev, "clock rate %lu Hz is unsupported\n", + sensor->extclk_rate); ret = -EINVAL; goto error_endpoint; } + sensor->pll_configs = ov8865_pll_configs[i]; + /* Subdev, entity and pad */ subdev = &sensor->subdev; @@ -2891,14 +3082,16 @@ static int ov8865_probe(struct i2c_client *client) if (ret) goto error_mutex; + mutex_lock(&sensor->mutex); ret = ov8865_state_init(sensor); + mutex_unlock(&sensor->mutex); if (ret) goto error_ctrls; /* Runtime PM */ - pm_runtime_enable(sensor->dev); pm_runtime_set_suspended(sensor->dev); + pm_runtime_enable(sensor->dev); /* V4L2 subdev register */ @@ -2946,6 +3139,12 @@ static const struct dev_pm_ops ov8865_pm_ops = { SET_RUNTIME_PM_OPS(ov8865_suspend, ov8865_resume, NULL) }; +static const struct acpi_device_id ov8865_acpi_match[] = { + {"INT347A"}, + { } +}; +MODULE_DEVICE_TABLE(acpi, ov8865_acpi_match); + static const struct of_device_id ov8865_of_match[] = { { .compatible = "ovti,ov8865" }, { } @@ -2956,6 +3155,7 @@ static struct i2c_driver ov8865_driver = { .driver = { .name = "ov8865", .of_match_table = ov8865_of_match, + .acpi_match_table = ov8865_acpi_match, .pm = &ov8865_pm_ops, }, .probe_new = ov8865_probe, diff --git a/drivers/media/i2c/s5c73m3/s5c73m3-spi.c b/drivers/media/i2c/s5c73m3/s5c73m3-spi.c index c102c6bbc118..7fe61187a2f8 100644 --- a/drivers/media/i2c/s5c73m3/s5c73m3-spi.c +++ b/drivers/media/i2c/s5c73m3/s5c73m3-spi.c @@ -130,16 +130,10 @@ static int s5c73m3_spi_probe(struct spi_device *spi) return 0; } -static int s5c73m3_spi_remove(struct spi_device *spi) -{ - return 0; -} - int s5c73m3_register_spi_driver(struct s5c73m3 *state) { struct spi_driver *spidrv = &state->spidrv; - spidrv->remove = s5c73m3_spi_remove; spidrv->probe = s5c73m3_spi_probe; spidrv->driver.name = S5C73M3_SPI_DRV_NAME; spidrv->driver.of_match_table = s5c73m3_spi_ids; |