spi-bcm2835 can handle >64kB buffers now so there is no need to check ->max_dma_len. The tinydrm_spi_max_transfer_size() max_len argument is not used by any callers, so not needed. Then we have the spi_max module parameter. It was added because staging/fbtft has support for it and there was a report that someone used it to set a small buffer size to avoid popping on a USB soundcard on a Raspberry Pi. In hindsight it shouldn't have been added, I should have waited for it to become a problem first. I don't know it anyone is actually using it, but since tinydrm_spi_transfer() is being moved to mipi-dbi, I'm taking the opportunity to remove it. I'll add it back to mipi-dbi if someone complains. With that out of the way, spi_max_transfer_size() can be used instead. The chosen 16kB buffer size for Type C Option 1 (9-bit) interface is somewhat arbitrary, but a bigger buffer will have a miniscule impact on transfer speed, so it's probably fine. Acked-by: Sam Ravnborg <sam@ravnborg.org> Signed-off-by: Noralf Trønnes <noralf@tronnes.org> Link: https://patchwork.freedesktop.org/patch/msgid/20190719155916.62465-6-noralf@tronnes.org
102 lines
2.2 KiB
C
102 lines
2.2 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Copyright (C) 2016 Noralf Trønnes
|
|
*/
|
|
|
|
#include <linux/backlight.h>
|
|
#include <linux/dma-buf.h>
|
|
#include <linux/module.h>
|
|
#include <linux/pm.h>
|
|
#include <linux/spi/spi.h>
|
|
#include <linux/swab.h>
|
|
|
|
#include <drm/drm_device.h>
|
|
#include <drm/drm_drv.h>
|
|
#include <drm/drm_fourcc.h>
|
|
#include <drm/drm_framebuffer.h>
|
|
#include <drm/drm_print.h>
|
|
#include <drm/drm_rect.h>
|
|
#include <drm/tinydrm/tinydrm-helpers.h>
|
|
|
|
#if IS_ENABLED(CONFIG_SPI)
|
|
|
|
/**
|
|
* tinydrm_spi_transfer - SPI transfer helper
|
|
* @spi: SPI device
|
|
* @speed_hz: Override speed (optional)
|
|
* @header: Optional header transfer
|
|
* @bpw: Bits per word
|
|
* @buf: Buffer to transfer
|
|
* @len: Buffer length
|
|
*
|
|
* 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
|
|
* 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:
|
|
* Zero on success, negative error code on failure.
|
|
*/
|
|
int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
|
|
struct spi_transfer *header, u8 bpw, const void *buf,
|
|
size_t len)
|
|
{
|
|
size_t max_chunk = spi_max_transfer_size(spi);
|
|
struct spi_transfer tr = {
|
|
.bits_per_word = bpw,
|
|
.speed_hz = speed_hz,
|
|
};
|
|
struct spi_message m;
|
|
u16 *swap_buf = NULL;
|
|
size_t chunk;
|
|
int ret = 0;
|
|
|
|
if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
|
|
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) {
|
|
chunk = min(len, max_chunk);
|
|
|
|
tr.tx_buf = buf;
|
|
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;
|
|
len -= chunk;
|
|
|
|
ret = spi_sync(spi, &m);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(tinydrm_spi_transfer);
|
|
|
|
#endif /* CONFIG_SPI */
|
|
|
|
MODULE_LICENSE("GPL");
|