Commit b2c98153 authored by Alexandru Ardelean's avatar Alexandru Ardelean Committed by Mark Brown

spi: introduce spi_delay struct as "value + unit" & spi_delay_exec()

There are plenty of delays that have been introduced in SPI core. Most of
them are in micro-seconds, some need to be in nano-seconds, and some in
clock-cycles.

For some of these delays (related to transfers & CS timing) it may make
sense to have a `spi_delay` struct that abstracts these a bit.

The important element of these delays [for unification] seems to be the
`unit` of the delay.
It looks like micro-seconds is good enough for most people, but every-once
in a while, some delays seem to require other units of measurement.

This change adds the `spi_delay` struct & a `spi_delay_exec()` function
that processes a `spi_delay` object/struct to execute the delay.
It's a copy of the `cs_change_delay` mechanism, but without the default
for 10 uS.

The clock-cycle delay unit is a bit special, as it needs to be bound to an
`spi_transfer` object to execute.
Signed-off-by: default avatarAlexandru Ardelean <alexandru.ardelean@analog.com>
Link: https://lore.kernel.org/r/20190926105147.7839-3-alexandru.ardelean@analog.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 6b3f236a
...@@ -1106,6 +1106,57 @@ static void _spi_transfer_delay_ns(u32 ns) ...@@ -1106,6 +1106,57 @@ static void _spi_transfer_delay_ns(u32 ns)
} }
} }
static int _spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
{
u32 delay = _delay->value;
u32 unit = _delay->unit;
u32 hz;
if (!delay)
return 0;
switch (unit) {
case SPI_DELAY_UNIT_USECS:
delay *= 1000;
break;
case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
break;
case SPI_DELAY_UNIT_SCK:
/* clock cycles need to be obtained from spi_transfer */
if (!xfer)
return -EINVAL;
/* if there is no effective speed know, then approximate
* by underestimating with half the requested hz
*/
hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
if (!hz)
return -EINVAL;
delay *= DIV_ROUND_UP(1000000000, hz);
break;
default:
return -EINVAL;
}
return delay;
}
int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
{
int delay;
if (!_delay)
return -EINVAL;
delay = _spi_delay_to_ns(_delay, xfer);
if (delay < 0)
return delay;
_spi_transfer_delay_ns(delay);
return 0;
}
EXPORT_SYMBOL_GPL(spi_delay_exec);
static void _spi_transfer_cs_change_delay(struct spi_message *msg, static void _spi_transfer_cs_change_delay(struct spi_message *msg,
struct spi_transfer *xfer) struct spi_transfer *xfer)
{ {
......
...@@ -90,6 +90,21 @@ void spi_statistics_add_transfer_stats(struct spi_statistics *stats, ...@@ -90,6 +90,21 @@ void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
#define SPI_STATISTICS_INCREMENT_FIELD(stats, field) \ #define SPI_STATISTICS_INCREMENT_FIELD(stats, field) \
SPI_STATISTICS_ADD_TO_FIELD(stats, field, 1) SPI_STATISTICS_ADD_TO_FIELD(stats, field, 1)
/**
* struct spi_delay - SPI delay information
* @value: Value for the delay
* @unit: Unit for the delay
*/
struct spi_delay {
#define SPI_DELAY_UNIT_USECS 0
#define SPI_DELAY_UNIT_NSECS 1
#define SPI_DELAY_UNIT_SCK 2
u16 value;
u8 unit;
};
extern int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer);
/** /**
* struct spi_device - Controller side proxy for an SPI slave device * struct spi_device - Controller side proxy for an SPI slave device
* @dev: Driver model representation of the device. * @dev: Driver model representation of the device.
...@@ -887,9 +902,6 @@ struct spi_transfer { ...@@ -887,9 +902,6 @@ struct spi_transfer {
u16 delay_usecs; u16 delay_usecs;
u16 cs_change_delay; u16 cs_change_delay;
u8 cs_change_delay_unit; u8 cs_change_delay_unit;
#define SPI_DELAY_UNIT_USECS 0
#define SPI_DELAY_UNIT_NSECS 1
#define SPI_DELAY_UNIT_SCK 2
u32 speed_hz; u32 speed_hz;
u16 word_delay; u16 word_delay;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment