drm/tinydrm: Use spi_is_bpw_supported()
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / tinydrm / core / tinydrm-helpers.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Noralf Trønnes
4  */
5
6 #include <linux/backlight.h>
7 #include <linux/dma-buf.h>
8 #include <linux/module.h>
9 #include <linux/pm.h>
10 #include <linux/spi/spi.h>
11 #include <linux/swab.h>
12
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>
20
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");
24
25 #if IS_ENABLED(CONFIG_SPI)
26
27 /**
28  * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
29  * @spi: SPI device
30  * @max_len: Maximum buffer size needed (optional)
31  *
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.
35  *
36  * Returns:
37  * Maximum size for SPI transfers
38  */
39 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
40 {
41         size_t ret;
42
43         ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
44         if (max_len)
45                 ret = min(ret, max_len);
46         if (spi_max)
47                 ret = min_t(size_t, ret, spi_max);
48         ret &= ~0x3;
49         if (ret < 4)
50                 ret = 4;
51
52         return ret;
53 }
54 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
55
56 static void
57 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
58                       const void *buf, int idx, bool tx)
59 {
60         u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
61         char linebuf[3 * 32];
62
63         hex_dump_to_buffer(buf, tr->len, 16,
64                            DIV_ROUND_UP(tr->bits_per_word, 8),
65                            linebuf, sizeof(linebuf), false);
66
67         printk(KERN_DEBUG
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 ? " ..." : "");
72 }
73
74 /* called through tinydrm_dbg_spi_message() */
75 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
76 {
77         struct spi_transfer *tmp;
78         int i = 0;
79
80         list_for_each_entry(tmp, &m->transfers, transfer_list) {
81
82                 if (tmp->tx_buf)
83                         tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
84                 if (tmp->rx_buf)
85                         tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
86                 i++;
87         }
88 }
89 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
90
91 /**
92  * tinydrm_spi_transfer - SPI transfer helper
93  * @spi: SPI device
94  * @speed_hz: Override speed (optional)
95  * @header: Optional header transfer
96  * @bpw: Bits per word
97  * @buf: Buffer to transfer
98  * @len: Buffer length
99  *
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.
105  *
106  * Returns:
107  * Zero on success, negative error code on failure.
108  */
109 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
110                          struct spi_transfer *header, u8 bpw, const void *buf,
111                          size_t len)
112 {
113         struct spi_transfer tr = {
114                 .bits_per_word = bpw,
115                 .speed_hz = speed_hz,
116         };
117         struct spi_message m;
118         u16 *swap_buf = NULL;
119         size_t max_chunk;
120         size_t chunk;
121         int ret = 0;
122
123         if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
124                 return -EINVAL;
125
126         max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
127
128         if (drm_debug & DRM_UT_DRIVER)
129                 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
130                          __func__, bpw, max_chunk);
131
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);
136                         if (!swap_buf)
137                                 return -ENOMEM;
138                 }
139         }
140
141         spi_message_init(&m);
142         if (header)
143                 spi_message_add_tail(header, &m);
144         spi_message_add_tail(&tr, &m);
145
146         while (len) {
147                 chunk = min(len, max_chunk);
148
149                 tr.tx_buf = buf;
150                 tr.len = chunk;
151
152                 if (swap_buf) {
153                         const u16 *buf16 = buf;
154                         unsigned int i;
155
156                         for (i = 0; i < chunk / 2; i++)
157                                 swap_buf[i] = swab16(buf16[i]);
158
159                         tr.tx_buf = swap_buf;
160                 }
161
162                 buf += chunk;
163                 len -= chunk;
164
165                 tinydrm_dbg_spi_message(spi, &m);
166                 ret = spi_sync(spi, &m);
167                 if (ret)
168                         return ret;
169         }
170
171         return 0;
172 }
173 EXPORT_SYMBOL(tinydrm_spi_transfer);
174
175 #endif /* CONFIG_SPI */
176
177 MODULE_LICENSE("GPL");