From: Laurent Pinchart Date: Tue, 12 Aug 2025 21:45:31 +0000 (+0300) Subject: media: i2c: ov02c10: Use V4L2 sensor clock helper X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=baa59320ee73d87a138881f57f7e5e9fd8eeadda;p=users%2Fhch%2Fmisc.git media: i2c: ov02c10: Use V4L2 sensor clock helper 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 is specified as mandatory in the DT bindings and the "clock-frequency" property is not allowed. 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 the expected rate, the driver fails probing. This is correct behaviour 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. The behaviour is also unchanged on OF platforms that comply with the DT bindings. Non-compliant platforms are not expected, but any regression could easily be handled by switching to the devm_v4l2_sensor_clk_get_legacy() helper designed to preserve non-compliant behaviour. Signed-off-by: Laurent Pinchart Signed-off-by: Sakari Ailus Reviewed-by: Mehdi Djait Signed-off-by: Hans Verkuil --- diff --git a/drivers/media/i2c/ov02c10.c b/drivers/media/i2c/ov02c10.c index e65a972b619f..8c4d85dc7922 100644 --- a/drivers/media/i2c/ov02c10.c +++ b/drivers/media/i2c/ov02c10.c @@ -799,7 +799,6 @@ static int ov02c10_check_hwcfg(struct ov02c10 *ov02c10) struct device *dev = ov02c10->dev; struct fwnode_handle *ep, *fwnode = dev_fwnode(dev); unsigned long link_freq_bitmap; - u32 mclk; int ret; /* @@ -811,31 +810,6 @@ static int ov02c10_check_hwcfg(struct ov02c10 *ov02c10) return dev_err_probe(dev, -EPROBE_DEFER, "waiting for fwnode graph endpoint\n"); - ov02c10->img_clk = devm_clk_get_optional(dev, NULL); - if (IS_ERR(ov02c10->img_clk)) { - fwnode_handle_put(ep); - return dev_err_probe(dev, PTR_ERR(ov02c10->img_clk), - "failed to get imaging clock\n"); - } - - if (ov02c10->img_clk) { - mclk = clk_get_rate(ov02c10->img_clk); - } else { - ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk); - if (ret) { - fwnode_handle_put(ep); - return dev_err_probe(dev, ret, - "reading clock-frequency property\n"); - } - } - - if (mclk != OV02C10_MCLK) { - fwnode_handle_put(ep); - return dev_err_probe(dev, -EINVAL, - "external clock %u is not supported\n", - mclk); - } - ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg); fwnode_handle_put(ep); if (ret) @@ -886,6 +860,7 @@ static void ov02c10_remove(struct i2c_client *client) static int ov02c10_probe(struct i2c_client *client) { struct ov02c10 *ov02c10; + unsigned long freq; int ret; ov02c10 = devm_kzalloc(&client->dev, sizeof(*ov02c10), GFP_KERNEL); @@ -894,6 +869,17 @@ static int ov02c10_probe(struct i2c_client *client) ov02c10->dev = &client->dev; + ov02c10->img_clk = devm_v4l2_sensor_clk_get(ov02c10->dev, NULL); + if (IS_ERR(ov02c10->img_clk)) + return dev_err_probe(ov02c10->dev, PTR_ERR(ov02c10->img_clk), + "failed to get imaging clock\n"); + + freq = clk_get_rate(ov02c10->img_clk); + if (freq != OV02C10_MCLK) + return dev_err_probe(ov02c10->dev, -EINVAL, + "external clock %lu is not supported", + freq); + v4l2_i2c_subdev_init(&ov02c10->sd, client, &ov02c10_subdev_ops); /* Check HW config */