]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
media: i2c: ov7251: Use V4L2 sensor clock helper
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Tue, 12 Aug 2025 21:45:50 +0000 (00:45 +0300)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Tue, 9 Sep 2025 13:59:19 +0000 (15:59 +0200)
Several camera sensor drivers access the "clock-frequency" property
directly to retrieve the external clock rate, or modify the clock rate
of the external clock programmatically. Both behaviours are valid on a
subset of ACPI platforms, but are considered deprecated on OF platforms,
and do not support ACPI platforms that implement MIPI DisCo for Imaging.
Implementing them manually in drivers is deprecated, as that can
encourage cargo-cult and lead to differences in behaviour between
drivers. Instead, drivers should use the devm_v4l2_sensor_clk_get()
helper.

This driver supports ACPI and OF platforms. The "clocks" property has
always been specified as mandatory in the DT bindings and the
"clock-frequency" property has always been optional. The driver
retrieves the clock and its rate if present, and falls back to
retrieving the rate from the "clock-frequency" property otherwise. If
the rate does not match one of the supported rates, the driver fails
probing.

If a clock is available and the "clock-frequency" property is set, the
driver sets the rate of the clock to the value of the property. It does
however use the rate initially retrieved from the clock for further
calculations, which is a bug if the rates don't match, and would prevent
the sensor from functioning properly. We can therefore assume that this
case never occurs, and that the driver behaves correctly for ACPI, and
for OF platforms that comply with the documented DT bindings.

Switch to using the devm_v4l2_sensor_clk_get() helper. This does not
change the behaviour on ACPI platforms that specify a clock-frequency
property and don't provide a clock. On ACPI platforms that provide a
clock, the clock rate will be set to the value of the clock-frequency
property. This should not change the behaviour either as this driver
expects the clock to be set to that rate, and wouldn't operate correctly
otherwise.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Mehdi Djait <mehdi.djait@linux.intel.com>
Signed-off-by: Hans Verkuil <hverkuil+cisco@kernel.org>
drivers/media/i2c/ov7251.c

index 31a42d81e97016ff860a7a7b026e514531571bba..27afc3fc01752ee32eff46a6f35f32a5f920fbed 100644 (file)
@@ -1630,7 +1630,6 @@ static int ov7251_probe(struct i2c_client *client)
 {
        struct device *dev = &client->dev;
        struct ov7251 *ov7251;
-       unsigned int rate = 0, clk_rate = 0;
        int ret;
        int i;
 
@@ -1646,33 +1645,12 @@ static int ov7251_probe(struct i2c_client *client)
                return ret;
 
        /* get system clock (xclk) */
-       ov7251->xclk = devm_clk_get_optional(dev, NULL);
+       ov7251->xclk = devm_v4l2_sensor_clk_get(dev, NULL);
        if (IS_ERR(ov7251->xclk))
                return dev_err_probe(dev, PTR_ERR(ov7251->xclk),
                                     "could not get xclk");
 
-       /*
-        * We could have either a 24MHz or 19.2MHz clock rate from either DT or
-        * ACPI. We also need to support the IPU3 case which will have both an
-        * external clock AND a clock-frequency property.
-        */
-       ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
-                                      &rate);
-       if (ret && !ov7251->xclk)
-               return dev_err_probe(dev, ret, "invalid clock config\n");
-
-       clk_rate = clk_get_rate(ov7251->xclk);
-       ov7251->xclk_freq = clk_rate ? clk_rate : rate;
-
-       if (ov7251->xclk_freq == 0)
-               return dev_err_probe(dev, -EINVAL, "invalid clock frequency\n");
-
-       if (!ret && ov7251->xclk) {
-               ret = clk_set_rate(ov7251->xclk, rate);
-               if (ret)
-                       return dev_err_probe(dev, ret,
-                                            "failed to set clock rate\n");
-       }
+       ov7251->xclk_freq = clk_get_rate(ov7251->xclk);
 
        for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++)
                if (ov7251->xclk_freq == supported_xclk_rates[i])