i2c: qcom-cci, media: ov02c10: Fix camera power cycling
Subsystems (6)
cover i2c: qcom-cci, media: ov02c10: Fix camera power cycling
The OV02C10 camera on the Lenovo Yoga Slim 7x can fail to restart after streaming stops. The CCI runtime PM callbacks gate the controller clocks but leave the bus pins in their default state while suspended. The X1E80100 device tree already defines a CCI sleep state with pull-downs for the bus pins, so select that state before suspending the controller and restore the default state on resume.
The OV02C10 driver also enables the sensor supplies before MCLK and disables MCLK before the supplies, with the required reset and startup timing. This series fixes the power transitions without adding a regulator-off delay or runtime autosuspend workaround.
Tested on Lenovo Yoga Slim 7x (Snapdragon X Elite): 20 consecutive Kamoso camera open/close cycles. The sensor entered runtime-active on each open and returned to runtime-suspended on each close. Both modified driver objects build cleanly against the running kernel configuration.
Saikiran (2):
i2c: qcom-cci: Select pinctrl states during runtime PM
media: ov02c10: Fix sensor power sequencing
drivers/i2c/busses/i2c-qcom-cci.c | 14 +++++++++++++- drivers/media/i2c/ov02c10.c | 20 +++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/2 i2c: qcom-cci: Select pinctrl states during runtime PM
The CCI runtime PM callbacks only gate the controller clocks, leaving the bus pins in their default state while the controller is suspended. On the Lenovo Yoga Slim 7x, this can back-power the camera sensor through SDA and SCL after its supplies have been disabled, contributing to brownouts on subsequent power cycles.
Select the sleep pinctrl state before disabling the clocks so that the configured pull-downs can take effect. Restore the default state before enabling the clocks on resume, and return to the sleep state if enabling the clocks fails.
Tested-on: Lenovo Yoga Slim 7x (Snapdragon X Elite)
--- drivers/i2c/busses/i2c-qcom-cci.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-qcom-cci.c +13 -1
@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> +#include <linux/pinctrl/consumer.h> #include <linux/pm_runtime.h> #define CCI_HW_VERSION 0x0
@@ -474,6 +475,11 @@ static void cci_disable_clocks(struct cci *cci)static int __maybe_unused cci_suspend_runtime (struct device *dev) {struct cci *cci =dev_get_drvdata (dev); +int ret; + + ret =pinctrl_pm_select_sleep_state (dev); +if (ret) +return ret;cci_disable_clocks (cci);return 0 ;
@@ -484,10 +490,16 @@ static int __maybe_unused cci_resume_runtime(struct device *dev)struct cci *cci =dev_get_drvdata (dev);int ret; - ret =cci_enable_clocks (cci); + ret =pinctrl_pm_select_default_state (dev);if (ret)return ret; + ret =cci_enable_clocks (cci); +if (ret) { +pinctrl_pm_select_sleep_state (dev); +return ret; + } +cci_init (cci);return 0 ; }
Signature · 1 line
2.56.0
Loic Poulain
maintainerRe: [PATCH v1 1/2] i2c: qcom-cci: Select pinctrl states during runtime PM
Quoted 31 lines · Saikiran
The CCI runtime PM callbacks only gate the controller clocks, leaving the bus pins in their default state while the controller is suspended. On the Lenovo Yoga Slim 7x, this can back-power the camera sensor through SDA and SCL after its supplies have been disabled, contributing to brownouts on subsequent power cycles.
Select the sleep pinctrl state before disabling the clocks so that the configured pull-downs can take effect. Restore the default state before enabling the clocks on resume, and return to the sleep state if enabling the clocks fails.
Tested-on: Lenovo Yoga Slim 7x (Snapdragon X Elite)
Signed-off-by Saikiran <[email protected]>--- drivers/i2c/busses/i2c-qcom-cci.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)drivers/i2c/busses/i2c-qcom-cci.c +1
-0@@ -10,6 +10,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> +#include <linux/pinctrl/consumer.h> #include <linux/pm_runtime.h> #define CCI_HW_VERSION 0x0
drivers/i2c/busses/i2c-qcom-cci.c static void cci_disable_clocks(struct cci *cci) · line 482
Quoted 8 lines
drivers/i2c/busses/i2c-qcom-cci.c +5
-0@@ -474,6 +475,11 @@ static void cci_disable_clocks(struct cci *cci)static int __maybe_unused cci_suspend_runtime (struct device *dev) {struct cci *cci =dev_get_drvdata (dev); +int ret; + + ret =pinctrl_pm_select_sleep_state (dev); +if (ret) +return ret;
You should perform the do/undo symmetrically in suspend/resume, so it would probably be better to move the pinctrl handling to the end of the suspend function after clock disabling.
Quoted 24 lines
drivers/i2c/busses/i2c-qcom-cci.c +7
-2cci_disable_clocks (cci);return 0 ;@@ -484,10 +490,16 @@ static int __maybe_unused cci_resume_runtime(struct device *dev)struct cci *cci =dev_get_drvdata (dev);int ret; - ret =cci_enable_clocks (cci); + ret =pinctrl_pm_select_default_state (dev);if (ret)return ret; + ret =cci_enable_clocks (cci); +if (ret) { +pinctrl_pm_select_sleep_state (dev); +return ret; + } +cci_init (cci);return 0 ; } --2.56.0
PATCH 2/2 media: ov02c10: Fix sensor power sequencing
In reply to SaikiranThe OV02C10 driver enables MCLK before the sensor supplies and disables it after the supplies. This can back-power the sensor through MCLK while its supplies are off and contributes to camera brownouts on the Lenovo Yoga Slim 7x.
Follow the power sequence used by the firmware on this platform: enable the supplies before MCLK and disable MCLK before the supplies. Hold reset for at least 1 ms before disabling MCLK on power-off, and wait at least 10 ms after releasing reset on power-on to allow the sensor to start up.
Update the power-on error path to disable the supplies if enabling MCLK fails.
Tested-on: Lenovo Yoga Slim 7x (Snapdragon X Elite)
--- drivers/media/i2c/ov02c10.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/media/i2c/ov02c10.c +11 -9
@@ -661,12 +661,13 @@ static int ov02c10_power_off(struct device *dev)struct ov02c10 *ov02c10 =to_ov02c10 (sd);gpiod_set_value_cansleep (ov02c10->reset,1 ); +usleep_range (1000 ,1100 ); + +clk_disable_unprepare (ov02c10->img_clk);regulator_bulk_disable (ARRAY_SIZE (ov02c10_supply_names), ov02c10->supplies); -clk_disable_unprepare (ov02c10->img_clk); -return 0 ; }
@@ -682,23 +683,24 @@ static int ov02c10_power_on(struct device *dev)usleep_range (5000 ,6000 ); } - ret =clk_prepare_enable (ov02c10->img_clk); + ret =regulator_bulk_enable (ARRAY_SIZE (ov02c10_supply_names), + ov02c10->supplies);if (ret <0 ) { -dev_err (dev,"failed to enable imaging clock: %d" , ret); +dev_err (dev,"failed to enable regulators: %d" , ret);return ret; } - ret =regulator_bulk_enable (ARRAY_SIZE (ov02c10_supply_names), - ov02c10->supplies); + ret =clk_prepare_enable (ov02c10->img_clk);if (ret <0 ) { -dev_err (dev,"failed to enable regulators: %d" , ret); -clk_disable_unprepare (ov02c10->img_clk); +dev_err (dev,"failed to enable imaging clock: %d" , ret); +regulator_bulk_disable (ARRAY_SIZE (ov02c10_supply_names), + ov02c10->supplies);return ret; }if (ov02c10->reset) {gpiod_set_value_cansleep (ov02c10->reset,0 ); -usleep_range (5000 ,5500 ); +usleep_range (10000 ,11000 ); }return 0 ;
Signature · 1 line
2.56.0
Re: [PATCH v1 2/2] media: ov02c10: Fix sensor power sequencing
Quoted 11 lines · Saikiran
The OV02C10 driver enables MCLK before the sensor supplies and disables it after the supplies. This can back-power the sensor through MCLK while its supplies are off and contributes to camera brownouts on the Lenovo Yoga Slim 7x.
Follow the power sequence used by the firmware on this platform: enable the supplies before MCLK and disable MCLK before the supplies. Hold reset for at least 1 ms before disabling MCLK on power-off, and wait at least 10 ms after releasing reset on power-on to allow the sensor to start up.
Quoted 5 lines
Update the power-on error path to disable the supplies if enabling MCLK fails.
Tested-on: Lenovo Yoga Slim 7x (Snapdragon X Elite)
Signed-off-by Saikiran <[email protected]>
Should it come with a Fixes tag?
Quoted 59 lines
--- drivers/media/i2c/ov02c10.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)drivers/media/i2c/ov02c10.c +11
-10@@ -661,12 +661,13 @@ static int ov02c10_power_off(struct device *dev)struct ov02c10 *ov02c10 =to_ov02c10 (sd);gpiod_set_value_cansleep (ov02c10->reset,1 ); +usleep_range (1000 ,1100 ); + +clk_disable_unprepare (ov02c10->img_clk);regulator_bulk_disable (ARRAY_SIZE (ov02c10_supply_names), ov02c10->supplies); -clk_disable_unprepare (ov02c10->img_clk); -return 0 ; }@@ -682,23 +683,24 @@ static int ov02c10_power_on(struct device *dev)usleep_range (5000 ,6000 ); } - ret =clk_prepare_enable (ov02c10->img_clk); + ret =regulator_bulk_enable (ARRAY_SIZE (ov02c10_supply_names), + ov02c10->supplies);if (ret <0 ) { -dev_err (dev,"failed to enable imaging clock: %d" , ret); +dev_err (dev,"failed to enable regulators: %d" , ret);return ret; } - ret =regulator_bulk_enable (ARRAY_SIZE (ov02c10_supply_names), - ov02c10->supplies); + ret =clk_prepare_enable (ov02c10->img_clk);if (ret <0 ) { -dev_err (dev,"failed to enable regulators: %d" , ret); -clk_disable_unprepare (ov02c10->img_clk); +dev_err (dev,"failed to enable imaging clock: %d" , ret); +regulator_bulk_disable (ARRAY_SIZE (ov02c10_supply_names), + ov02c10->supplies);return ret; }if (ov02c10->reset) {gpiod_set_value_cansleep (ov02c10->reset,0 ); -usleep_range (5000 ,5500 ); +usleep_range (10000 ,11000 ); }return 0 ; --2.56.0
Quoted 3 lines · Saikiran
Tested on Lenovo Yoga Slim 7x (Snapdragon X Elite): 20 consecutive Kamoso camera open/close cycles. The sensor entered runtime-active on each open and returned to runtime-suspended on each close.
I ran this series through the restart test from my CCI series [1] on a Yoga Slim 7x with 7.3. 1/2 applies as posted. 2/2 is against a tree with an extra delay at the top of ov02c10_power_on(), so I made its three changes on 7.3 by hand. Each cycle streams five frames with v4l2-ctl, waits, and starts again, 20 cycles per pause:
pause 0.05 s 0.1 s 0.2 s 0.5 s 1 s 2 s 7.3 19/20 19/20 19/20 19/20 15/20 0/20 + 1/2 19/20 19/20 19/20 19/20 12/20 0/20 + 1/2, 2/2 18/20 19/20 18/20 19/20 17/20 0/20
Every failure is the first CCI write timing out ("master 1 queue 0 timeout"). 1/2 does select the sleep state while the controller is runtime suspended (pinctrl-handles shows "sleep", and "default" once it resumes), but with the 1 s autosuspend the controller never suspends when the camera is closed and opened again within a second. After a longer pause the restart works without the series as well, which is the case 20 Kamoso cycles cover.
On this machine both bus lines read low while the sensor is off, with the pull-ups on, so the unpowered sensor clamps them, and both read high once it is powered again. The CCI queue still stays on its first command until cci_reset(). The pin levels and queue registers are in my reply to Loic [2], where a reset on resume plus one retry of a message after a timeout made 120 of 120 restarts work.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]