Re: [PATCH v5 07/11] iio: adc: ad4170: Add clock provider support
From: David Lechner
Date: Mon Jun 16 2025 - 17:11:57 EST
On 6/10/25 3:33 PM, Marcelo Schmitt wrote:
> The AD4170 chip can use an externally supplied clock at the XTAL2 pin, or
> an external crystal connected to the XTAL1 and XTAL2 pins. Alternatively,
> the AD4170 can provide its 16 MHz internal clock at the XTAL2 pin. In
> addition, the chip has a programmable clock divider that allows dividing
> the external or internal clock frequency, however, control for that is not
> provided in this patch. Extend the AD4170 driver so it effectively uses the
> provided external clock, if any, or supplies its own clock as a clock
> provider.
>
...
> +static int ad4170_clock_select(struct iio_dev *indio_dev)
> +{
> + struct ad4170_state *st = iio_priv(indio_dev);
> + struct device *dev = &st->spi->dev;
> + struct clk *ext_clk;
> + int ret;
> +
> + ext_clk = devm_clk_get_optional_enabled(dev, NULL);
> + if (IS_ERR(ext_clk))
> + return dev_err_probe(dev, PTR_ERR(ext_clk),
> + "Failed to get external clock\n");
> +
> + if (!ext_clk) {
> + /* Use internal clock reference */
> + st->mclk_hz = AD4170_INT_CLOCK_16MHZ;
> + st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK,
> + AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT);
> +
> + if (!device_property_read_bool(&st->spi->dev, "#clock-cells"))
This isn't a flag, so device_property_present() is probably more correct.
> + return 0;
> +
> + return ad4170_register_clk_provider(indio_dev);
> + }
> +
> + /* Read optional clock-names prop to specify the external clock type */
> + ret = device_property_match_property_string(dev, "clock-names",
> + ad4170_clk_sel,
> + ARRAY_SIZE(ad4170_clk_sel));
> +
> + ret = ret < 0 ? 0 : ret; /* Default to external clock if no clock-names */
> + st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK,
> + AD4170_CLOCK_CTRL_CLOCKSEL_EXT + ret);
> +
> + st->mclk_hz = clk_get_rate(ext_clk);
> + if (st->mclk_hz < AD4170_EXT_CLOCK_MHZ_MIN ||
> + st->mclk_hz > AD4170_EXT_CLOCK_MHZ_MAX) {
> + return dev_err_probe(dev, -EINVAL,
> + "Invalid external clock frequency %u\n",
> + st->mclk_hz);
> + }
> +
> + return 0;
> +}