pktgen: do not sleep with the thread lock held.
[sfrench/cifs-2.6.git] / drivers / gpu / drm / tinydrm / core / tinydrm-helpers.c
1 /*
2  * Copyright (C) 2016 Noralf Trønnes
3  *
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.
8  */
9
10 #include <linux/backlight.h>
11 #include <linux/dma-buf.h>
12 #include <linux/module.h>
13 #include <linux/pm.h>
14 #include <linux/spi/spi.h>
15 #include <linux/swab.h>
16
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>
24
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");
28
29 #if IS_ENABLED(CONFIG_SPI)
30
31 /**
32  * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
33  * @spi: SPI device
34  * @max_len: Maximum buffer size needed (optional)
35  *
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.
39  *
40  * Returns:
41  * Maximum size for SPI transfers
42  */
43 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
44 {
45         size_t ret;
46
47         ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
48         if (max_len)
49                 ret = min(ret, max_len);
50         if (spi_max)
51                 ret = min_t(size_t, ret, spi_max);
52         ret &= ~0x3;
53         if (ret < 4)
54                 ret = 4;
55
56         return ret;
57 }
58 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
59
60 /**
61  * tinydrm_spi_bpw_supported - Check if bits per word is supported
62  * @spi: SPI device
63  * @bpw: Bits per word
64  *
65  * This function checks to see if the SPI master driver supports @bpw.
66  *
67  * Returns:
68  * True if @bpw is supported, false otherwise.
69  */
70 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
71 {
72         u32 bpw_mask = spi->master->bits_per_word_mask;
73
74         if (bpw == 8)
75                 return true;
76
77         if (!bpw_mask) {
78                 dev_warn_once(&spi->dev,
79                               "bits_per_word_mask not set, assume 8-bit only\n");
80                 return false;
81         }
82
83         if (bpw_mask & SPI_BPW_MASK(bpw))
84                 return true;
85
86         return false;
87 }
88 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
89
90 static void
91 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
92                       const void *buf, int idx, bool tx)
93 {
94         u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
95         char linebuf[3 * 32];
96
97         hex_dump_to_buffer(buf, tr->len, 16,
98                            DIV_ROUND_UP(tr->bits_per_word, 8),
99                            linebuf, sizeof(linebuf), false);
100
101         printk(KERN_DEBUG
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 ? " ..." : "");
106 }
107
108 /* called through tinydrm_dbg_spi_message() */
109 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
110 {
111         struct spi_transfer *tmp;
112         int i = 0;
113
114         list_for_each_entry(tmp, &m->transfers, transfer_list) {
115
116                 if (tmp->tx_buf)
117                         tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
118                 if (tmp->rx_buf)
119                         tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
120                 i++;
121         }
122 }
123 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
124
125 /**
126  * tinydrm_spi_transfer - SPI transfer helper
127  * @spi: SPI device
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
133  *
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.
139  *
140  * Returns:
141  * Zero on success, negative error code on failure.
142  */
143 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
144                          struct spi_transfer *header, u8 bpw, const void *buf,
145                          size_t len)
146 {
147         struct spi_transfer tr = {
148                 .bits_per_word = bpw,
149                 .speed_hz = speed_hz,
150         };
151         struct spi_message m;
152         u16 *swap_buf = NULL;
153         size_t max_chunk;
154         size_t chunk;
155         int ret = 0;
156
157         if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
158                 return -EINVAL;
159
160         max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
161
162         if (drm_debug & DRM_UT_DRIVER)
163                 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
164                          __func__, bpw, max_chunk);
165
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);
170                         if (!swap_buf)
171                                 return -ENOMEM;
172                 }
173         }
174
175         spi_message_init(&m);
176         if (header)
177                 spi_message_add_tail(header, &m);
178         spi_message_add_tail(&tr, &m);
179
180         while (len) {
181                 chunk = min(len, max_chunk);
182
183                 tr.tx_buf = buf;
184                 tr.len = chunk;
185
186                 if (swap_buf) {
187                         const u16 *buf16 = buf;
188                         unsigned int i;
189
190                         for (i = 0; i < chunk / 2; i++)
191                                 swap_buf[i] = swab16(buf16[i]);
192
193                         tr.tx_buf = swap_buf;
194                 }
195
196                 buf += chunk;
197                 len -= chunk;
198
199                 tinydrm_dbg_spi_message(spi, &m);
200                 ret = spi_sync(spi, &m);
201                 if (ret)
202                         return ret;
203         }
204
205         return 0;
206 }
207 EXPORT_SYMBOL(tinydrm_spi_transfer);
208
209 #endif /* CONFIG_SPI */
210
211 MODULE_LICENSE("GPL");