]> www.infradead.org Git - users/hch/misc.git/commitdiff
media: i2c: hi847: Use V4L2 sensor clock helper
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Tue, 12 Aug 2025 21:45:21 +0000 (00:45 +0300)
committerMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Tue, 9 Sep 2025 13:59:17 +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 platforms only. It retrieves the clock rate
from the "clock-frequency" property. If the rate does not match the
expected rate, the driver fails probing. This is correct behaviour for
ACPI.

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/hi847.c

index 384ccfc171c23803f29dad99b386f9d05ad40920..def01aa07b2f5013315ac0c96fa0efaff2b10fb0 100644 (file)
@@ -2,6 +2,7 @@
 // Copyright (c) 2022 Intel Corporation.
 
 #include <linux/acpi.h>
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
@@ -2168,6 +2169,7 @@ static const struct hi847_mode supported_modes[] = {
 
 struct hi847 {
        struct device *dev;
+       struct clk *clk;
 
        struct v4l2_subdev sd;
        struct media_pad pad;
@@ -2789,24 +2791,12 @@ static int hi847_check_hwcfg(struct device *dev)
        struct v4l2_fwnode_endpoint bus_cfg = {
                .bus_type = V4L2_MBUS_CSI2_DPHY
        };
-       u32 mclk;
        int ret;
        unsigned int i, j;
 
        if (!fwnode)
                return -ENXIO;
 
-       ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
-       if (ret) {
-               dev_err(dev, "can't get clock frequency");
-               return ret;
-       }
-
-       if (mclk != HI847_MCLK) {
-               dev_err(dev, "external clock %d is not supported", mclk);
-               return -EINVAL;
-       }
-
        ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
        if (!ep)
                return -ENXIO;
@@ -2865,6 +2855,7 @@ static void hi847_remove(struct i2c_client *client)
 static int hi847_probe(struct i2c_client *client)
 {
        struct hi847 *hi847;
+       unsigned long freq;
        int ret;
 
        hi847 = devm_kzalloc(&client->dev, sizeof(*hi847), GFP_KERNEL);
@@ -2873,6 +2864,17 @@ static int hi847_probe(struct i2c_client *client)
 
        hi847->dev = &client->dev;
 
+       hi847->clk = devm_v4l2_sensor_clk_get(hi847->dev, NULL);
+       if (IS_ERR(hi847->clk))
+               return dev_err_probe(hi847->dev, PTR_ERR(hi847->clk),
+                                    "failed to get clock\n");
+
+       freq = clk_get_rate(hi847->clk);
+       if (freq != HI847_MCLK)
+               return dev_err_probe(hi847->dev, -EINVAL,
+                                    "external clock %lu is not supported\n",
+                                    freq);
+
        ret = hi847_check_hwcfg(hi847->dev);
        if (ret) {
                dev_err(hi847->dev, "failed to get HW configuration: %d",