1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Noralf Trønnes
6 #include <linux/backlight.h>
7 #include <linux/dma-buf.h>
8 #include <linux/module.h>
10 #include <linux/spi/spi.h>
11 #include <linux/swab.h>
13 #include <drm/drm_device.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fourcc.h>
16 #include <drm/drm_framebuffer.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_rect.h>
19 #include <drm/tinydrm/tinydrm-helpers.h>
21 static unsigned int spi_max;
22 module_param(spi_max, uint, 0400);
23 MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
25 #if IS_ENABLED(CONFIG_SPI)
28 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
30 * @max_len: Maximum buffer size needed (optional)
32 * This function returns the maximum size to use for SPI transfers. It checks
33 * the SPI master, the optional @max_len and the module parameter spi_max and
34 * returns the smallest.
37 * Maximum size for SPI transfers
39 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
43 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
45 ret = min(ret, max_len);
47 ret = min_t(size_t, ret, spi_max);
54 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
57 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
58 const void *buf, int idx, bool tx)
60 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
63 hex_dump_to_buffer(buf, tr->len, 16,
64 DIV_ROUND_UP(tr->bits_per_word, 8),
65 linebuf, sizeof(linebuf), false);
68 " tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
69 speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
70 speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
71 tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
74 /* called through tinydrm_dbg_spi_message() */
75 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
77 struct spi_transfer *tmp;
80 list_for_each_entry(tmp, &m->transfers, transfer_list) {
83 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
85 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
89 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
92 * tinydrm_spi_transfer - SPI transfer helper
94 * @speed_hz: Override speed (optional)
95 * @header: Optional header transfer
97 * @buf: Buffer to transfer
100 * This SPI transfer helper breaks up the transfer of @buf into chunks which
101 * the SPI master driver can handle. If the machine is Little Endian and the
102 * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
103 * does a 8-bit transfer.
104 * If @header is set, it is prepended to each SPI message.
107 * Zero on success, negative error code on failure.
109 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
110 struct spi_transfer *header, u8 bpw, const void *buf,
113 struct spi_transfer tr = {
114 .bits_per_word = bpw,
115 .speed_hz = speed_hz,
117 struct spi_message m;
118 u16 *swap_buf = NULL;
123 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
126 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
128 if (drm_debug & DRM_UT_DRIVER)
129 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
130 __func__, bpw, max_chunk);
132 if (bpw == 16 && !spi_is_bpw_supported(spi, 16)) {
133 tr.bits_per_word = 8;
134 if (tinydrm_machine_little_endian()) {
135 swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
141 spi_message_init(&m);
143 spi_message_add_tail(header, &m);
144 spi_message_add_tail(&tr, &m);
147 chunk = min(len, max_chunk);
153 const u16 *buf16 = buf;
156 for (i = 0; i < chunk / 2; i++)
157 swap_buf[i] = swab16(buf16[i]);
159 tr.tx_buf = swap_buf;
165 tinydrm_dbg_spi_message(spi, &m);
166 ret = spi_sync(spi, &m);
173 EXPORT_SYMBOL(tinydrm_spi_transfer);
175 #endif /* CONFIG_SPI */
177 MODULE_LICENSE("GPL");