Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: i2c: ov7251: 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 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>

authored by

Laurent Pinchart and committed by
Mauro Carvalho Chehab
5b428a40 063f5989

+2 -24
+2 -24
drivers/media/i2c/ov7251.c
··· 1630 1630 { 1631 1631 struct device *dev = &client->dev; 1632 1632 struct ov7251 *ov7251; 1633 - unsigned int rate = 0, clk_rate = 0; 1634 1633 int ret; 1635 1634 int i; 1636 1635 ··· 1645 1646 return ret; 1646 1647 1647 1648 /* get system clock (xclk) */ 1648 - ov7251->xclk = devm_clk_get_optional(dev, NULL); 1649 + ov7251->xclk = devm_v4l2_sensor_clk_get(dev, NULL); 1649 1650 if (IS_ERR(ov7251->xclk)) 1650 1651 return dev_err_probe(dev, PTR_ERR(ov7251->xclk), 1651 1652 "could not get xclk"); 1652 1653 1653 - /* 1654 - * We could have either a 24MHz or 19.2MHz clock rate from either DT or 1655 - * ACPI. We also need to support the IPU3 case which will have both an 1656 - * external clock AND a clock-frequency property. 1657 - */ 1658 - ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", 1659 - &rate); 1660 - if (ret && !ov7251->xclk) 1661 - return dev_err_probe(dev, ret, "invalid clock config\n"); 1662 - 1663 - clk_rate = clk_get_rate(ov7251->xclk); 1664 - ov7251->xclk_freq = clk_rate ? clk_rate : rate; 1665 - 1666 - if (ov7251->xclk_freq == 0) 1667 - return dev_err_probe(dev, -EINVAL, "invalid clock frequency\n"); 1668 - 1669 - if (!ret && ov7251->xclk) { 1670 - ret = clk_set_rate(ov7251->xclk, rate); 1671 - if (ret) 1672 - return dev_err_probe(dev, ret, 1673 - "failed to set clock rate\n"); 1674 - } 1654 + ov7251->xclk_freq = clk_get_rate(ov7251->xclk); 1675 1655 1676 1656 for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++) 1677 1657 if (ov7251->xclk_freq == supported_xclk_rates[i])