2 * Copyright (C) 2016 Noralf Trønnes
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/backlight.h>
11 #include <linux/dma-buf.h>
12 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/swab.h>
17 #include <drm/drm_device.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_framebuffer.h>
21 #include <drm/drm_print.h>
22 #include <drm/drm_rect.h>
23 #include <drm/tinydrm/tinydrm-helpers.h>
25 static unsigned int spi_max;
26 module_param(spi_max, uint, 0400);
27 MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
29 #if IS_ENABLED(CONFIG_SPI)
32 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
34 * @max_len: Maximum buffer size needed (optional)
36 * This function returns the maximum size to use for SPI transfers. It checks
37 * the SPI master, the optional @max_len and the module parameter spi_max and
38 * returns the smallest.
41 * Maximum size for SPI transfers
43 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
47 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
49 ret = min(ret, max_len);
51 ret = min_t(size_t, ret, spi_max);
58 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
61 * tinydrm_spi_bpw_supported - Check if bits per word is supported
65 * This function checks to see if the SPI master driver supports @bpw.
68 * True if @bpw is supported, false otherwise.
70 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
72 u32 bpw_mask = spi->master->bits_per_word_mask;
78 dev_warn_once(&spi->dev,
79 "bits_per_word_mask not set, assume 8-bit only\n");
83 if (bpw_mask & SPI_BPW_MASK(bpw))
88 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
91 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
92 const void *buf, int idx, bool tx)
94 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
97 hex_dump_to_buffer(buf, tr->len, 16,
98 DIV_ROUND_UP(tr->bits_per_word, 8),
99 linebuf, sizeof(linebuf), false);
102 " tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
103 speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
104 speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
105 tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
108 /* called through tinydrm_dbg_spi_message() */
109 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
111 struct spi_transfer *tmp;
114 list_for_each_entry(tmp, &m->transfers, transfer_list) {
117 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
119 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
123 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
126 * tinydrm_spi_transfer - SPI transfer helper
128 * @speed_hz: Override speed (optional)
129 * @header: Optional header transfer
130 * @bpw: Bits per word
131 * @buf: Buffer to transfer
132 * @len: Buffer length
134 * This SPI transfer helper breaks up the transfer of @buf into chunks which
135 * the SPI master driver can handle. If the machine is Little Endian and the
136 * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
137 * does a 8-bit transfer.
138 * If @header is set, it is prepended to each SPI message.
141 * Zero on success, negative error code on failure.
143 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
144 struct spi_transfer *header, u8 bpw, const void *buf,
147 struct spi_transfer tr = {
148 .bits_per_word = bpw,
149 .speed_hz = speed_hz,
151 struct spi_message m;
152 u16 *swap_buf = NULL;
157 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
160 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
162 if (drm_debug & DRM_UT_DRIVER)
163 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
164 __func__, bpw, max_chunk);
166 if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
167 tr.bits_per_word = 8;
168 if (tinydrm_machine_little_endian()) {
169 swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
175 spi_message_init(&m);
177 spi_message_add_tail(header, &m);
178 spi_message_add_tail(&tr, &m);
181 chunk = min(len, max_chunk);
187 const u16 *buf16 = buf;
190 for (i = 0; i < chunk / 2; i++)
191 swap_buf[i] = swab16(buf16[i]);
193 tr.tx_buf = swap_buf;
199 tinydrm_dbg_spi_message(spi, &m);
200 ret = spi_sync(spi, &m);
207 EXPORT_SYMBOL(tinydrm_spi_transfer);
209 #endif /* CONFIG_SPI */
211 MODULE_LICENSE("GPL");