Commit 083a6c23 authored by Noralf Trønnes's avatar Noralf Trønnes

drm/tinydrm: Clean up tinydrm_spi_transfer()

Prep work before moving the function to mipi-dbi.

tinydrm_spi_transfer() was made to support one class of drivers in
drivers/staging/fbtft that has not been converted to DRM yet, so strip
away the unused functionality:
- Start byte (header) is not used.
- No driver relies on the automatic 16-bit byte swapping on little endian
  machines with SPI controllers only supporting 8 bits per word.

Other changes:
- No need to initialize ret
- No need for the WARN since mipi-dbi only uses 8 and 16 bpw.
- Use spi_message_init_with_transfers()

Cc: David Lechner <david@lechnology.com>
Acked-by: default avatar: David Lechner <david@lechnology.com>
Signed-off-by: default avatarNoralf Trønnes <noralf@tronnes.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20190719155916.62465-7-noralf@tronnes.org
parent e143364b
...@@ -24,23 +24,18 @@ ...@@ -24,23 +24,18 @@
* tinydrm_spi_transfer - SPI transfer helper * tinydrm_spi_transfer - SPI transfer helper
* @spi: SPI device * @spi: SPI device
* @speed_hz: Override speed (optional) * @speed_hz: Override speed (optional)
* @header: Optional header transfer
* @bpw: Bits per word * @bpw: Bits per word
* @buf: Buffer to transfer * @buf: Buffer to transfer
* @len: Buffer length * @len: Buffer length
* *
* This SPI transfer helper breaks up the transfer of @buf into chunks which * This SPI transfer helper breaks up the transfer of @buf into chunks which
* the SPI master driver can handle. If the machine is Little Endian and the * the SPI controller driver can handle.
* SPI master driver doesn't support 16 bits per word, it swaps the bytes and
* does a 8-bit transfer.
* If @header is set, it is prepended to each SPI message.
* *
* Returns: * Returns:
* Zero on success, negative error code on failure. * Zero on success, negative error code on failure.
*/ */
int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz, int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
struct spi_transfer *header, u8 bpw, const void *buf, u8 bpw, const void *buf, size_t len)
size_t len)
{ {
size_t max_chunk = spi_max_transfer_size(spi); size_t max_chunk = spi_max_transfer_size(spi);
struct spi_transfer tr = { struct spi_transfer tr = {
...@@ -48,43 +43,16 @@ int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz, ...@@ -48,43 +43,16 @@ int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
.speed_hz = speed_hz, .speed_hz = speed_hz,
}; };
struct spi_message m; struct spi_message m;
u16 *swap_buf = NULL;
size_t chunk; size_t chunk;
int ret = 0; int ret;
if (WARN_ON_ONCE(bpw != 8 && bpw != 16)) spi_message_init_with_transfers(&m, &tr, 1);
return -EINVAL;
if (bpw == 16 && !spi_is_bpw_supported(spi, 16)) {
tr.bits_per_word = 8;
if (tinydrm_machine_little_endian()) {
swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
if (!swap_buf)
return -ENOMEM;
}
}
spi_message_init(&m);
if (header)
spi_message_add_tail(header, &m);
spi_message_add_tail(&tr, &m);
while (len) { while (len) {
chunk = min(len, max_chunk); chunk = min(len, max_chunk);
tr.tx_buf = buf; tr.tx_buf = buf;
tr.len = chunk; tr.len = chunk;
if (swap_buf) {
const u16 *buf16 = buf;
unsigned int i;
for (i = 0; i < chunk / 2; i++)
swap_buf[i] = swab16(buf16[i]);
tr.tx_buf = swap_buf;
}
buf += chunk; buf += chunk;
len -= chunk; len -= chunk;
......
...@@ -323,7 +323,7 @@ static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par, ...@@ -323,7 +323,7 @@ static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
gpiod_set_value_cansleep(mipi->dc, 0); gpiod_set_value_cansleep(mipi->dc, 0);
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1); speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, cmd, 1); ret = tinydrm_spi_transfer(spi, speed_hz, 8, cmd, 1);
if (ret || !num) if (ret || !num)
return ret; return ret;
...@@ -333,7 +333,7 @@ static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par, ...@@ -333,7 +333,7 @@ static int ili9225_dbi_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
gpiod_set_value_cansleep(mipi->dc, 1); gpiod_set_value_cansleep(mipi->dc, 1);
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num); speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num); return tinydrm_spi_transfer(spi, speed_hz, bpw, par, num);
} }
static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = { static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
......
...@@ -926,7 +926,7 @@ static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 *cmd, ...@@ -926,7 +926,7 @@ static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 *cmd,
gpiod_set_value_cansleep(mipi->dc, 0); gpiod_set_value_cansleep(mipi->dc, 0);
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1); speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
ret = tinydrm_spi_transfer(spi, speed_hz, NULL, 8, cmd, 1); ret = tinydrm_spi_transfer(spi, speed_hz, 8, cmd, 1);
if (ret || !num) if (ret || !num)
return ret; return ret;
...@@ -936,7 +936,7 @@ static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 *cmd, ...@@ -936,7 +936,7 @@ static int mipi_dbi_typec3_command(struct mipi_dbi *mipi, u8 *cmd,
gpiod_set_value_cansleep(mipi->dc, 1); gpiod_set_value_cansleep(mipi->dc, 1);
speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num); speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
return tinydrm_spi_transfer(spi, speed_hz, NULL, bpw, par, num); return tinydrm_spi_transfer(spi, speed_hz, bpw, par, num);
} }
/** /**
......
...@@ -42,7 +42,6 @@ int tinydrm_display_pipe_init(struct drm_device *drm, ...@@ -42,7 +42,6 @@ int tinydrm_display_pipe_init(struct drm_device *drm,
unsigned int rotation); unsigned int rotation);
int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz, int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
struct spi_transfer *header, u8 bpw, const void *buf, u8 bpw, const void *buf, size_t len);
size_t len);
#endif /* __LINUX_TINYDRM_HELPERS_H */ #endif /* __LINUX_TINYDRM_HELPERS_H */
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