]> Git Repo - linux.git/blob - drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c
Merge tag 'media/v5.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux.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 /**
57  * tinydrm_spi_bpw_supported - Check if bits per word is supported
58  * @spi: SPI device
59  * @bpw: Bits per word
60  *
61  * This function checks to see if the SPI master driver supports @bpw.
62  *
63  * Returns:
64  * True if @bpw is supported, false otherwise.
65  */
66 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
67 {
68         u32 bpw_mask = spi->master->bits_per_word_mask;
69
70         if (bpw == 8)
71                 return true;
72
73         if (!bpw_mask) {
74                 dev_warn_once(&spi->dev,
75                               "bits_per_word_mask not set, assume 8-bit only\n");
76                 return false;
77         }
78
79         if (bpw_mask & SPI_BPW_MASK(bpw))
80                 return true;
81
82         return false;
83 }
84 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
85
86 static void
87 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
88                       const void *buf, int idx, bool tx)
89 {
90         u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
91         char linebuf[3 * 32];
92
93         hex_dump_to_buffer(buf, tr->len, 16,
94                            DIV_ROUND_UP(tr->bits_per_word, 8),
95                            linebuf, sizeof(linebuf), false);
96
97         printk(KERN_DEBUG
98                "    tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
99                speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
100                speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
101                tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
102 }
103
104 /* called through tinydrm_dbg_spi_message() */
105 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
106 {
107         struct spi_transfer *tmp;
108         int i = 0;
109
110         list_for_each_entry(tmp, &m->transfers, transfer_list) {
111
112                 if (tmp->tx_buf)
113                         tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
114                 if (tmp->rx_buf)
115                         tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
116                 i++;
117         }
118 }
119 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
120
121 /**
122  * tinydrm_spi_transfer - SPI transfer helper
123  * @spi: SPI device
124  * @speed_hz: Override speed (optional)
125  * @header: Optional header transfer
126  * @bpw: Bits per word
127  * @buf: Buffer to transfer
128  * @len: Buffer length
129  *
130  * This SPI transfer helper breaks up the transfer of @buf into chunks which
131  * the SPI master driver can handle. If the machine is Little Endian and the
132  * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
133  * does a 8-bit transfer.
134  * If @header is set, it is prepended to each SPI message.
135  *
136  * Returns:
137  * Zero on success, negative error code on failure.
138  */
139 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
140                          struct spi_transfer *header, u8 bpw, const void *buf,
141                          size_t len)
142 {
143         struct spi_transfer tr = {
144                 .bits_per_word = bpw,
145                 .speed_hz = speed_hz,
146         };
147         struct spi_message m;
148         u16 *swap_buf = NULL;
149         size_t max_chunk;
150         size_t chunk;
151         int ret = 0;
152
153         if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
154                 return -EINVAL;
155
156         max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
157
158         if (drm_debug & DRM_UT_DRIVER)
159                 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
160                          __func__, bpw, max_chunk);
161
162         if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
163                 tr.bits_per_word = 8;
164                 if (tinydrm_machine_little_endian()) {
165                         swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
166                         if (!swap_buf)
167                                 return -ENOMEM;
168                 }
169         }
170
171         spi_message_init(&m);
172         if (header)
173                 spi_message_add_tail(header, &m);
174         spi_message_add_tail(&tr, &m);
175
176         while (len) {
177                 chunk = min(len, max_chunk);
178
179                 tr.tx_buf = buf;
180                 tr.len = chunk;
181
182                 if (swap_buf) {
183                         const u16 *buf16 = buf;
184                         unsigned int i;
185
186                         for (i = 0; i < chunk / 2; i++)
187                                 swap_buf[i] = swab16(buf16[i]);
188
189                         tr.tx_buf = swap_buf;
190                 }
191
192                 buf += chunk;
193                 len -= chunk;
194
195                 tinydrm_dbg_spi_message(spi, &m);
196                 ret = spi_sync(spi, &m);
197                 if (ret)
198                         return ret;
199         }
200
201         return 0;
202 }
203 EXPORT_SYMBOL(tinydrm_spi_transfer);
204
205 #endif /* CONFIG_SPI */
206
207 MODULE_LICENSE("GPL");
This page took 0.044346 seconds and 4 git commands to generate.