Re: [PATCH v4 4/4] media: i2c: imx258: get clock from device properties and enable it via runtime PM

From: Sakari Ailus
Date: Tue Sep 29 2020 - 05:17:14 EST


Hi Krzysztof,

On Wed, Sep 23, 2020 at 05:21:29PM +0200, Krzysztof Kozlowski wrote:
> The IMX258 sensor driver checked in device properties for a
> clock-frequency property which actually does not mean that the clock is
> really running such frequency or is it even enabled.
>
> Get the provided clock and check it frequency. If none is provided,
> fall back to old property.
>
> Enable the clock when accessing the IMX258 registers and when streaming
> starts with runtime PM.
>
> Signed-off-by: Krzysztof Kozlowski <krzk@xxxxxxxxxx>
>
> ---
>
> Changes since v3:
> 1. None
>
> Changes since v2:
> 1. Do not try to set drvdata, wrap lines.
> 2. Use dev_dbg.
>
> Changes since v1:
> 1. Use runtime PM for clock toggling
> ---
> drivers/media/i2c/imx258.c | 71 +++++++++++++++++++++++++++++++++-----
> 1 file changed, 62 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/media/i2c/imx258.c b/drivers/media/i2c/imx258.c
> index ae183b0dbba9..7bedbfe5c4d6 100644
> --- a/drivers/media/i2c/imx258.c
> +++ b/drivers/media/i2c/imx258.c
> @@ -2,6 +2,7 @@
> // Copyright (C) 2018 Intel Corporation
>
> #include <linux/acpi.h>
> +#include <linux/clk.h>
> #include <linux/delay.h>
> #include <linux/i2c.h>
> #include <linux/module.h>
> @@ -68,6 +69,9 @@
> #define REG_CONFIG_MIRROR_FLIP 0x03
> #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
>
> +/* Input clock frequency in Hz */
> +#define IMX258_INPUT_CLOCK_FREQ 19200000
> +
> struct imx258_reg {
> u16 address;
> u8 val;
> @@ -610,6 +614,8 @@ struct imx258 {
>
> /* Streaming on/off */
> bool streaming;
> +
> + struct clk *clk;
> };
>
> static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
> @@ -972,6 +978,29 @@ static int imx258_stop_streaming(struct imx258 *imx258)
> return 0;
> }
>
> +static int imx258_power_on(struct device *dev)
> +{
> + struct v4l2_subdev *sd = dev_get_drvdata(dev);
> + struct imx258 *imx258 = to_imx258(sd);
> + int ret;
> +
> + ret = clk_prepare_enable(imx258->clk);
> + if (ret)
> + dev_err(dev, "failed to enable clock\n");
> +
> + return ret;
> +}
> +
> +static int imx258_power_off(struct device *dev)
> +{
> + struct v4l2_subdev *sd = dev_get_drvdata(dev);
> + struct imx258 *imx258 = to_imx258(sd);
> +
> + clk_disable_unprepare(imx258->clk);
> +
> + return 0;
> +}
> +
> static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
> {
> struct imx258 *imx258 = to_imx258(sd);
> @@ -1199,9 +1228,28 @@ static int imx258_probe(struct i2c_client *client)
> int ret;
> u32 val = 0;
>
> - device_property_read_u32(&client->dev, "clock-frequency", &val);
> - if (val != 19200000)
> - return -EINVAL;
> + imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
> + if (!imx258)
> + return -ENOMEM;
> +
> + imx258->clk = devm_clk_get_optional(&client->dev, NULL);
> + if (!imx258->clk) {
> + dev_dbg(&client->dev,
> + "no clock provided, using clock-frequency property\n");
> +
> + device_property_read_u32(&client->dev, "clock-frequency", &val);
> + if (val != IMX258_INPUT_CLOCK_FREQ)
> + return -EINVAL;
> + } else if (IS_ERR(imx258->clk)) {
> + return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
> + "error getting clock\n");
> + } else {
> + if (clk_get_rate(imx258->clk) != IMX258_INPUT_CLOCK_FREQ) {
> + dev_err(&client->dev,
> + "input clock frequency not supported\n");
> + return -EINVAL;
> + }
> + }
>
> /*
> * Check that the device is mounted upside down. The driver only
> @@ -1211,24 +1259,25 @@ static int imx258_probe(struct i2c_client *client)
> if (ret || val != 180)
> return -EINVAL;
>
> - imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
> - if (!imx258)
> - return -ENOMEM;
> -
> /* Initialize subdev */
> v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
>
> + /* Will be powered off via pm_runtime_idle */
> + ret = imx258_power_on(&client->dev);
> + if (ret)
> + return ret;
> +
> /* Check module identity */
> ret = imx258_identify_module(imx258);
> if (ret)
> - return ret;
> + goto error_identify;
>
> /* Set default mode to max resolution */
> imx258->cur_mode = &supported_modes[0];
>
> ret = imx258_init_controls(imx258);
> if (ret)
> - return ret;
> + goto error_identify;
>
> /* Initialize subdev */
> imx258->sd.internal_ops = &imx258_internal_ops;
> @@ -1258,6 +1307,9 @@ static int imx258_probe(struct i2c_client *client)
> error_handler_free:
> imx258_free_controls(imx258);
>
> +error_identify:
> + imx258_power_off(&client->dev);

You'll need this in remove callback, too.

> +
> return ret;
> }
>
> @@ -1278,6 +1330,7 @@ static int imx258_remove(struct i2c_client *client)
>
> static const struct dev_pm_ops imx258_pm_ops = {
> SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
> + SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
> };
>
> #ifdef CONFIG_ACPI

--
Kind regards,

Sakari Ailus