mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Radu Sabau via B4 Relay <devnull+radu.sabau.analog.com@kernel.org>
Cc: radu.sabau@analog.com, Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	Nuno Sa <nuno.sa@analog.com>,
	David Lechner <dlechner@baylibre.com>,
	Andy Shevchenko <andy@kernel.org>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org, linux@analog.com
Subject: Re: [PATCH v4 7/8] iio: imu: adis16607: Add external clock sync support
Date: Mon, 26 Oct 2026 00:11:32 +0000	[thread overview]
Message-ID: <20261026001132.3c76529a@jic23-hlaptop> (raw)
In-Reply-To: <20261023-adis16607-v4-7-a4d541ebfd13@analog.com>

On Fri, 23 Oct 2026 12:30:24 +0300
Radu Sabau via B4 Relay <devnull+radu.sabau.analog.com@kernel.org> wrote:

> From: Radu Sabau <radu.sabau@analog.com>
> 
> Add support for an optional external clock (clocks property),
> selecting between SYNC_DIRECT and SYNC_SCALED modes depending on the
> supplied frequency, with the internal 8 kHz clock used as the default
> when no clock is provided.
> 
> Signed-off-by: Radu Sabau <radu.sabau@analog.com>
> ---

> -/*
> - * No external clock sync support yet: always run on the internal clock.
> - * Default dec=0 after reset, so period = 1 / clk_freq_mhz.
> - */
>  static int adis16607_config_clk(struct adis16607 *st)
>  {
> -	st->clk_freq_mhz = st->info->int_clk_hz * MILLI;
> -	st->sample_period_ns =
> -		DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * MILLI, st->clk_freq_mhz);
> +	struct device *dev = &st->adis.spi->dev;
> +	const struct adis16607_sync *sync_mode_data = NULL;
> +	unsigned long ext_clk_hz = 0;
> +	u16 sync_scale = 0;
> +	u16 dec;
> +	struct clk *clk;
> +	int ret, i;

where there is no other particular reason for ordering, reverse xmas tree
preferred.


> +
> +	clk = devm_clk_get_optional_enabled(dev, NULL);
> +	if (IS_ERR(clk))
> +		return PTR_ERR(clk);
> +
> +	if (clk) {
> +		ext_clk_hz = clk_get_rate(clk);

Hmm. What if it is a controllable clock?

> +
> +		for (i = 1; i < st->info->num_sync; i++) {

		for (unsigned int i = 1; ...

> +			if (ext_clk_hz >= st->info->sync_mode[i].min_rate &&
> +			    ext_clk_hz <= st->info->sync_mode[i].max_rate) {
> +				sync_mode_data = &st->info->sync_mode[i];
> +				break;
> +			}
> +		}
> +
> +		if (!sync_mode_data)
> +			return dev_err_probe(dev, -EINVAL,
> +					     "Clk rate: %lu not in a valid range\n",
> +					     ext_clk_hz);
> +
> +		st->sync_mode = sync_mode_data->sync_mode;
> +	} else {
> +		st->sync_mode = ADIS16607_SYNC_DEFAULT;
> +	}
> +
> +	/*
> +	 * Write the full desired sync state explicitly for every mode,
> +	 * rather than only when enabling non-default sync: USER_SYNC's
> +	 * documented reset default (0x0000, direct mode / scale 0) means
> +	 * this isn't fixing a known reset-state issue, but each mode
> +	 * establishing its own complete state here, instead of only
> +	 * touching bits conditionally, is simpler to reason about and
> +	 * doesn't depend on that default holding true.
> +	 */
> +	ret = adis_update_bits_base(&st->adis, ADIS16607_REG_USER_GPIO_CFG,
> +				    ADIS16607_SYNC_GPIO_MASK,
> +				    FIELD_PREP(ADIS16607_SYNC_GPIO_MASK,
> +					       st->sync_mode != ADIS16607_SYNC_DEFAULT), 2);
> +	if (ret)
> +		return ret;
> +
> +	if (st->sync_mode == ADIS16607_SYNC_SCALED)
> +		sync_scale = DIV_ROUND_CLOSEST(st->info->int_clk_hz, ext_clk_hz);
> +
> +	ret = adis_write_reg_16(&st->adis, ADIS16607_REG_USER_SYNC,
> +				(st->sync_mode == ADIS16607_SYNC_SCALED) ?
> +				ADIS16607_SYNC_MODE_MASK | sync_scale : 0);
> +	if (ret)
> +		return ret;
> +
> +	switch (st->sync_mode) {
> +	case ADIS16607_SYNC_SCALED:
> +		/*
> +		 * sync_scale is a rounded integer, so the hardware's actual
> +		 * effective rate is ext_clk_hz * sync_scale, not necessarily
> +		 * int_clk_hz - use the real value here so sample_period_ns (and
> +		 * therefore FIFO sample timestamps) don't drift from it.
> +		 */
> +		st->clk_freq_mhz = (unsigned long)ext_clk_hz * sync_scale * 1000;
> +		break;
> +	case ADIS16607_SYNC_DIRECT:
> +		/* DIRECT: external clock is the effective internal rate */
> +		st->clk_freq_mhz = ext_clk_hz * 1000;
> +		break;
> +	default:
> +		st->clk_freq_mhz = (unsigned long)st->info->int_clk_hz * 1000;
> +		break;
> +	}
> +
> +	ret = adis_read_reg_16(&st->adis, ADIS16607_REG_DEC_RATE, &dec);
> +	if (ret)
> +		return ret;
> +
> +	st->sample_period_ns = DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * 1000ULL * (dec + 1),
> +						     st->clk_freq_mhz);
> +
>  	return 0;
>  }
>  
> 


  reply	other threads:[~2026-10-26  0:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23  9:30 [PATCH v4 0/8] iio: imu: Add support for the ADI ADIS16607 Radu Sabau via B4 Relay
2026-10-23  9:30 ` [PATCH v4 1/8] iio: imu: adis: Add optional self_test callback Radu Sabau via B4 Relay
2026-10-23  9:30 ` [PATCH v4 2/8] iio: imu: adis: Fix custom reset dispatch in adislib Radu Sabau via B4 Relay
2026-10-23  9:30 ` [PATCH v4 3/8] iio: imu: adis: Add optional post_reset_setup callback Radu Sabau via B4 Relay
2026-10-25 23:52   ` Jonathan Cameron
2026-10-23  9:30 ` [PATCH v4 4/8] dt-bindings: iio: imu: Add bindings for ADI ADIS16607 Radu Sabau via B4 Relay
2026-10-25 23:54   ` Jonathan Cameron
2026-10-23  9:30 ` [PATCH v4 5/8] iio: imu: Add driver for the " Radu Sabau via B4 Relay
2026-10-23 10:26   ` Andy Shevchenko
2026-10-23  9:30 ` [PATCH v4 6/8] iio: imu: adis16607: Add FIFO-based buffered/triggered capture Radu Sabau via B4 Relay
2026-10-26  0:09   ` Jonathan Cameron
2026-10-23  9:30 ` [PATCH v4 7/8] iio: imu: adis16607: Add external clock sync support Radu Sabau via B4 Relay
2026-10-26  0:11   ` Jonathan Cameron [this message]
2026-10-23  9:30 ` [PATCH v4 8/8] iio: imu: adis16607: Add calibration bias support for gyro/accel Radu Sabau via B4 Relay
2026-10-26  0:15   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026001132.3c76529a@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=alexandru.ardelean@analog.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=devnull+radu.sabau.analog.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@analog.com \
    --cc=nuno.sa@analog.com \
    --cc=radu.sabau@analog.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®