[PATCH v6 4/8] media: tw9900: use runtime PM autosuspend and power down on suspend

From: Matthew Laux

Date: Wed Nov 18 2026 - 15:43:51 EST


Add support for autosuspend to the tw9900 driver. This is so that initial
bursts of register writes for configuration don't suspend after every
write and cause the init to take a long time.

Signed-off-by: Matthew Laux <matthew.laux@xxxxxxxxx>
---
drivers/media/i2c/tw9900.c | 37 ++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c
index d365432e8f89..4d0d437d02f3 100644
--- a/drivers/media/i2c/tw9900.c
+++ b/drivers/media/i2c/tw9900.c
@@ -34,6 +34,9 @@
#define TW9900_REG_CKHY_HSDLY 0x04
#define TW9900_REG_OUT_CTRL_I 0x05
#define TW9900_REG_ANALOG_CTL 0x06
+#define TW9900_REG_ANALOG_CTL_CLK_PDN BIT(3)
+#define TW9900_REG_ANALOG_CTL_Y_PDN BIT(2)
+#define TW9900_REG_ANALOG_CTL_C_PDN BIT(1)
#define TW9900_REG_CROP_HI 0x07
#define TW9900_REG_VDELAY_LO 0x08
#define TW9900_REG_VACTIVE_LO 0x09
@@ -48,6 +51,7 @@
#define TW9900_REG_HUE 0x15
#define TW9900_REG_VBI_CNTL 0x19
#define TW9900_REG_ANAL_CTL_II 0x1A
+#define TW9900_REG_ANAL_CTL_II_PLL_PDN BIT(6)
#define TW9900_REG_OUT_CTRL_II 0x1B
#define TW9900_REG_STD 0x1C
#define TW9900_REG_STD_AUTO_PROGRESS BIT(7)
@@ -64,6 +68,7 @@
#define TW9900_STD_AUTO 7

#define TW9900_VIDEO_POLL_TRIES 20
+#define TW9900_AUTOSUSPEND_DELAY_MS 1000

struct regval {
u8 addr;
@@ -338,14 +343,14 @@ static int tw9900_s_stream(struct v4l2_subdev *sd, int on)

mutex_unlock(&tw9900->mutex);

- pm_runtime_put(&client->dev);
+ pm_runtime_put_autosuspend(&client->dev);
}

return 0;

err_unlock:
mutex_unlock(&tw9900->mutex);
- pm_runtime_put(&client->dev);
+ pm_runtime_put_autosuspend(&client->dev);

return ret;
}
@@ -522,7 +527,7 @@ static int tw9900_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)

out_unlock:
mutex_unlock(&tw9900->mutex);
- pm_runtime_put(&tw9900->client->dev);
+ pm_runtime_put_autosuspend(&tw9900->client->dev);

return ret;
}
@@ -558,7 +563,7 @@ static int tw9900_g_input_status(struct v4l2_subdev *sd, u32 *status)
ret = tw9900_read_reg(tw9900->client, TW9900_REG_CHIP_STATUS);
mutex_unlock(&tw9900->mutex);

- pm_runtime_put(&tw9900->client->dev);
+ pm_runtime_put_autosuspend(&tw9900->client->dev);

if (ret < 0)
return ret;
@@ -613,7 +618,7 @@ static int tw9900_check_id(struct tw9900 *tw9900,
ret = tw9900_read_reg(client, TW9900_REG_CHIP_ID);
mutex_unlock(&tw9900->mutex);

- pm_runtime_put(&tw9900->client->dev);
+ pm_runtime_put_autosuspend(&tw9900->client->dev);

if (ret < 0)
return ret;
@@ -683,6 +688,13 @@ static int tw9900_runtime_suspend(struct device *dev)

mutex_lock(&tw9900->mutex);

+ tw9900_write_reg(tw9900->client, TW9900_REG_ANALOG_CTL,
+ TW9900_REG_ANALOG_CTL_CLK_PDN |
+ TW9900_REG_ANALOG_CTL_Y_PDN |
+ TW9900_REG_ANALOG_CTL_C_PDN);
+ tw9900_write_reg(tw9900->client, TW9900_REG_ANAL_CTL_II,
+ TW9900_REG_ANAL_CTL_II_PLL_PDN);
+
if (tw9900->reset_gpio)
gpiod_set_value_cansleep(tw9900->reset_gpio, 1);

@@ -693,6 +705,15 @@ static int tw9900_runtime_suspend(struct device *dev)
return 0;
}

+static void tw9900_pm_cleanup(struct device *dev)
+{
+ pm_runtime_dont_use_autosuspend(dev);
+ pm_runtime_disable(dev);
+ if (!pm_runtime_status_suspended(dev))
+ tw9900_runtime_suspend(dev);
+ pm_runtime_set_suspended(dev);
+}
+
static int tw9900_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
@@ -757,6 +778,8 @@ static int tw9900_probe(struct i2c_client *client)
goto err_free_handler;

pm_runtime_set_suspended(dev);
+ pm_runtime_set_autosuspend_delay(dev, TW9900_AUTOSUSPEND_DELAY_MS);
+ pm_runtime_use_autosuspend(dev);
pm_runtime_enable(dev);

ret = tw9900_check_id(tw9900, client);
@@ -772,7 +795,7 @@ static int tw9900_probe(struct i2c_client *client)
return 0;

err_disable_pm:
- pm_runtime_disable(dev);
+ tw9900_pm_cleanup(dev);
media_entity_cleanup(&tw9900->subdev.entity);
err_free_handler:
v4l2_ctrl_handler_free(hdl);
@@ -791,7 +814,7 @@ static void tw9900_remove(struct i2c_client *client)
media_entity_cleanup(&sd->entity);
v4l2_ctrl_handler_free(sd->ctrl_handler);

- pm_runtime_disable(&client->dev);
+ tw9900_pm_cleanup(&client->dev);

mutex_destroy(&tw9900->mutex);
}
--
2.47.3