media: i2c: ov13858: add regulator, clock and reset GPIO handling
Subsystems (4)
PATCH v4 1/1 media: i2c: ov13858: add regulator, clock and reset GPIO handling
The driver assumes the sensor is already powered when probe() reads its chip ID. That holds where the rails and clock are ACPI power resources, but not where an INT3472 companion device registers them as regulators, a clock and a reset GPIO for the sensor driver to consume, which this driver does not do.
Request the three supplies and the reset GPIO, and sequence them along with the clock the driver already looks up, in runtime PM callbacks.
---
Changes in v4:
- runtime PM ops only, without the system sleep handlers that DEFINE_RUNTIME_DEV_PM_OPS() adds, as camera-sensor.rst asks. With them, a system suspend tried during a stream powered the sensor off before the IPU6 ISYS refused the suspend, and the stream stalled; now it goes on.
- German's and Simon's Tested-by from v3 kept: the change touches only system sleep, which neither reported testing. Simon's was not threaded under v3:
https://lore.kernel.org/all/[email protected]/
Link to v3: https://patch.msgid.link/[email protected]
George Neacsu's device tree support for this sensor builds on it:
https://lore.kernel.org/all/[email protected]/
Changes in v3, from Sakari Ailus's review of v2:
- wrapped the one line over 80 columns
- dropped an unrelated hunk in ov13858_id_table. It was not deliberate: v2 was regenerated against mainline from a tree based on a distribution kernel, and that difference came along with it.
Link to v2: https://patch.msgid.link/[email protected]
Changes in v2, from the review of v1:
- commit message cut down; the detail below
- dropped the comment on the supply names; it is a KAPI with int3472
- ARRAY_SIZE() directly instead of a local macro
- fsleep() instead of usleep_range()
- one function each for power on and off, used as the PM callbacks directly, instead of a pair of wrappers
- removed the now-useless dev_err() in the probe error path
- i declared inside its loop
Link to v1: https://patch.msgid.link/[email protected]
Without the patch the first I2C transaction fails:
ov13858 i2c-OVTID858:00: failed to find sensor: -5
The power sequence follows ov02c10: supplies, then clock, then release reset, and the reverse on the way down.
Measured on a Microsoft Surface Pro 11 for Business (Intel Lunar Lake, IPU7), with the parts isolated one at a time:
supplies enabled, clock enabled sensor identifies, driver binds supplies enabled, clock left off -EIO supplies left off, clock enabled -EIO
so both are needed; a longer settling delay alone is not enough. Verified across five module unload/load cycles with no probe failure, and again after this rework, with the sensor streaming each time.
This needs POWER1 GPIO support in int3472 to be useful on that machine: the dvdd rail is described there as an INT3472 GPIO of type 0x08, and without that patch the rail is never registered.
The same sensor on the Surface Pro 10 was made to work downstream by forcing the regulators on for the driver's lifetime, which the people who did it called too broad for upstream. Runtime PM keeps them off while the sensor is idle instead.
dovdd is not described on this machine and resolves to a dummy regulator. It is listed because it is one of the three supplies these sensors normally take.
A working camera also needs an ipu-bridge entry for OVTID858, separately.
drivers/media/i2c/ov13858.c | 91 +++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-)
drivers/media/i2c/ov13858.c +88 -3
@@ -3,9 +3,12 @@#include <linux/acpi.h> #include <linux/clk.h> +#include <linux/delay.h> +#include <linux/gpio/consumer.h> #include <linux/i2c.h> #include <linux/module.h> #include <linux/pm_runtime.h> +#include <linux/regulator/consumer.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> #include <media/v4l2-event.h>
@@ -1034,9 +1037,17 @@ static const struct ov13858_mode supported_modes[] = { } }; +static const char *const ov13858_supply_names[] = { +"dovdd" ,/* Digital I/O power */ +"avdd" ,/* Analog power */ +"dvdd" ,/* Digital core power */ +}; +struct ov13858 {struct device *dev;struct clk *clk; +struct regulator_bulk_data supplies[ARRAY_SIZE (ov13858_supply_names)]; +struct gpio_desc *reset_gpio;struct v4l2_subdev sd;struct media_pad pad;
@@ -1706,6 +1717,54 @@ static void ov13858_free_controls(struct ov13858 *ov13858)mutex_destroy (&ov13858->mutex); } +static int ov13858_power_on (struct device *dev) +{ +struct v4l2_subdev *sd =dev_get_drvdata (dev); +struct ov13858 *ov13858 =to_ov13858 (sd); +int ret; + + ret =regulator_bulk_enable (ARRAY_SIZE (ov13858_supply_names), + ov13858->supplies); +if (ret) { +dev_err (ov13858->dev,"failed to enable regulators: %d\n" , ret); +return ret; + } + + ret =clk_prepare_enable (ov13858->clk); +if (ret) { +dev_err (ov13858->dev,"failed to enable clock: %d\n" , ret); +regulator_bulk_disable (ARRAY_SIZE (ov13858_supply_names), ov13858->supplies); +return ret; + } + +if (ov13858->reset_gpio) { +/* Hold reset for at least 1 ms on a back to back off-on */ +fsleep (1000 ); +gpiod_set_value_cansleep (ov13858->reset_gpio,0 ); + } + +/* t4: 8192 XVCLK cycles after reset is released, before the first I2C */ +fsleep (5000 ); + +return 0 ; +} + +static int ov13858_power_off (struct device *dev) +{ +struct v4l2_subdev *sd =dev_get_drvdata (dev); +struct ov13858 *ov13858 =to_ov13858 (sd); + +gpiod_set_value_cansleep (ov13858->reset_gpio,1 ); +regulator_bulk_disable (ARRAY_SIZE (ov13858_supply_names), ov13858->supplies); +clk_disable_unprepare (ov13858->clk); + +return 0 ; +} + +static const struct dev_pm_ops ov13858_pm_ops = { +RUNTIME_PM_OPS (ov13858_power_off, ov13858_power_on,NULL ) +}; +static int ov13858_probe (struct i2c_client *client) {struct ov13858 *ov13858;
@@ -1729,14 +1788,34 @@ static int ov13858_probe(struct i2c_client *client)"external clock %lu is not supported\n" , freq); +for (unsigned int i =0 ; i <ARRAY_SIZE (ov13858_supply_names); i++) + ov13858->supplies[i].supply = ov13858_supply_names[i]; + + ret =devm_regulator_bulk_get (ov13858->dev,ARRAY_SIZE (ov13858_supply_names), + ov13858->supplies); +if (ret) +return dev_err_probe (ov13858->dev, ret, +"failed to get regulators\n" ); + + ov13858->reset_gpio =devm_gpiod_get_optional (ov13858->dev,"reset" , +GPIOD_OUT_HIGH ); +if (IS_ERR (ov13858->reset_gpio)) +return dev_err_probe (ov13858->dev, +PTR_ERR (ov13858->reset_gpio), +"failed to get reset GPIO\n" ); +/* Initialize subdev */ v4l2_i2c_subdev_init (&ov13858->sd, client, &ov13858_subdev_ops); + ret =ov13858_power_on (ov13858->dev); +if (ret) +return ret; +/* Check module identity */ ret =ov13858_identify_module (ov13858);if (ret) {dev_err (ov13858->dev,"failed to find sensor: %d\n" , ret); -return ret; + goto error_power_off; }/* Set default mode to max resolution */
@@ -1744,7 +1823,7 @@ static int ov13858_probe(struct i2c_client *client) ret =ov13858_init_controls (ov13858);if (ret) -return ret; + goto error_power_off;/* Initialize subdev */ ov13858->sd.internal_ops = &ov13858_internal_ops;
@@ -1780,7 +1859,9 @@ static int ov13858_probe(struct i2c_client *client) error_handler_free:ov13858_free_controls (ov13858); -dev_err (ov13858->dev,"%s failed:%d\n" , __func__, ret); + +error_power_off: +ov13858_power_off (ov13858->dev);return ret; }
@@ -1795,6 +1876,9 @@ static void ov13858_remove(struct i2c_client *client)ov13858_free_controls (ov13858);pm_runtime_disable (ov13858->dev); +if (!pm_runtime_status_suspended (ov13858->dev)) +ov13858_power_off (ov13858->dev); +pm_runtime_set_suspended (ov13858->dev); }static const struct i2c_device_id ov13858_id_table[] = {
@@ -1817,6 +1901,7 @@ static struct i2c_driver ov13858_i2c_driver = { .driver = { .name = "ov13858", .acpi_match_table = ACPI_PTR(ov13858_acpi_ids), + .pm = pm_ptr(&ov13858_pm_ops), }, .probe = ov13858_probe, .remove = ov13858_remove,
Signature · 1 line
2.54.0
Hi Sakari,
Is there anything you would like changed in v4? It applies to v7.4-rc1, before or after the ov13858 analogue gain v2:
https://lore.kernel.org/all/[email protected]/
Both carry an Assisted-by tag, which 7.4-rc1 no longer accepts; please drop it when applying, or I will resend without it.
Sergey
Hi Sergey,
Surface Pro 8, Tiger Lake, IPU6 8086:9a19, OV13858 at OVTID858:00 (\_SB.PC00.I2C3.CAMR). Kernel v7.4-rc2 with this patch applied and nothing else changed from mainline.
Without it:
ov13858 i2c-OVTID858:00: failed to find sensor: -5
ov13858 i2c-OVTID858:00: probe with driver ov13858 failed with error -5
With it the driver binds and libcamera finds the camera:
ov13858 24576 1
v4l-subdev10: ov13858 3-0010
$ cam -l 3: Internal back camera (\_SB_.PC00.I2C3.CAMR)
I captured 1056x784 SGRBG10 through the ISYS capture node and the frame has a picture in it, not a black or noisy buffer. Colour is off, which is the missing ov13858.yaml rather than this patch.
One detail that might be worth a line in the commit message: this machine only has two rails. The log says
ov13858 i2c-OVTID858:00: supply dovdd not found, using dummy regulator
because INT3472:01 registers avdd and dvdd and there is no dovdd here. The driver works fine on the dummy, so there is nothing to fix, but a tester on another machine could read that line as a misconfiguration.
Unrelated to this patch: the rear sensor here is mounted upside down and its SSDB reports rotation 0, so frames come out rotated 180 degrees. That is the same situation the Pro 9 is already listed for in upside_down_sensor_dmi_ids. Adding a DMI entry for the Pro 8 there makes the frame come out upright, so I will send that separately; it does not affect this patch.
Thanks,
Emfox
Quoted 8 lines · Sergey Lebedev
The driver assumes the sensor is already powered when
probe()reads its chip ID. That holds where the rails and clock are ACPI power resources, but not where an INT3472 companion device registers them as regulators, a clock and a reset GPIO for the sensor driver to consume, which this driver does not do.Request the three supplies and the reset GPIO, and sequence them along with the clock the driver already looks up, in runtime PM callbacks.
[...]
Hi Sergey,
Tested on a Surface Pro 11 (X1P-64-100, LCD, 5G) on a v7.3.4-based tree with out-of-tree CCI/CAMSS and SP11 camera DT nodes and George's DT v2 patch on top:
- avdd, dovdd, dvdd, clock (19.2 MHz) and reset GPIO all come from DT; the sensor probes and streams 4224x3136 at ~30 fps through CAMSS and libcamera 0.7.2;
- All three supply rails are off when idle, on when streaming.
- v4l2-compliance on the subdev passes 46/46.
I did not test system suspend while streaming.
Thanks,
Haipeng