]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c
drm/tinydrm: Use damage helper for dirtyfb
[linux.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 /**
30  * tinydrm_memcpy - Copy clip buffer
31  * @dst: Destination buffer
32  * @vaddr: Source buffer
33  * @fb: DRM framebuffer
34  * @clip: Clip rectangle area to copy
35  */
36 void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
37                     struct drm_rect *clip)
38 {
39         unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
40         unsigned int pitch = fb->pitches[0];
41         void *src = vaddr + (clip->y1 * pitch) + (clip->x1 * cpp);
42         size_t len = (clip->x2 - clip->x1) * cpp;
43         unsigned int y;
44
45         for (y = clip->y1; y < clip->y2; y++) {
46                 memcpy(dst, src, len);
47                 src += pitch;
48                 dst += len;
49         }
50 }
51 EXPORT_SYMBOL(tinydrm_memcpy);
52
53 /**
54  * tinydrm_swab16 - Swap bytes into clip buffer
55  * @dst: RGB565 destination buffer
56  * @vaddr: RGB565 source buffer
57  * @fb: DRM framebuffer
58  * @clip: Clip rectangle area to copy
59  */
60 void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
61                     struct drm_rect *clip)
62 {
63         size_t len = (clip->x2 - clip->x1) * sizeof(u16);
64         unsigned int x, y;
65         u16 *src, *buf;
66
67         /*
68          * The cma memory is write-combined so reads are uncached.
69          * Speed up by fetching one line at a time.
70          */
71         buf = kmalloc(len, GFP_KERNEL);
72         if (!buf)
73                 return;
74
75         for (y = clip->y1; y < clip->y2; y++) {
76                 src = vaddr + (y * fb->pitches[0]);
77                 src += clip->x1;
78                 memcpy(buf, src, len);
79                 src = buf;
80                 for (x = clip->x1; x < clip->x2; x++)
81                         *dst++ = swab16(*src++);
82         }
83
84         kfree(buf);
85 }
86 EXPORT_SYMBOL(tinydrm_swab16);
87
88 /**
89  * tinydrm_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
90  * @dst: RGB565 destination buffer
91  * @vaddr: XRGB8888 source buffer
92  * @fb: DRM framebuffer
93  * @clip: Clip rectangle area to copy
94  * @swap: Swap bytes
95  *
96  * Drivers can use this function for RGB565 devices that don't natively
97  * support XRGB8888.
98  */
99 void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
100                                 struct drm_framebuffer *fb,
101                                 struct drm_rect *clip, bool swap)
102 {
103         size_t len = (clip->x2 - clip->x1) * sizeof(u32);
104         unsigned int x, y;
105         u32 *src, *buf;
106         u16 val16;
107
108         buf = kmalloc(len, GFP_KERNEL);
109         if (!buf)
110                 return;
111
112         for (y = clip->y1; y < clip->y2; y++) {
113                 src = vaddr + (y * fb->pitches[0]);
114                 src += clip->x1;
115                 memcpy(buf, src, len);
116                 src = buf;
117                 for (x = clip->x1; x < clip->x2; x++) {
118                         val16 = ((*src & 0x00F80000) >> 8) |
119                                 ((*src & 0x0000FC00) >> 5) |
120                                 ((*src & 0x000000F8) >> 3);
121                         src++;
122                         if (swap)
123                                 *dst++ = swab16(val16);
124                         else
125                                 *dst++ = val16;
126                 }
127         }
128
129         kfree(buf);
130 }
131 EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
132
133 /**
134  * tinydrm_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
135  * @dst: 8-bit grayscale destination buffer
136  * @vaddr: XRGB8888 source buffer
137  * @fb: DRM framebuffer
138  * @clip: Clip rectangle area to copy
139  *
140  * Drm doesn't have native monochrome or grayscale support.
141  * Such drivers can announce the commonly supported XR24 format to userspace
142  * and use this function to convert to the native format.
143  *
144  * Monochrome drivers will use the most significant bit,
145  * where 1 means foreground color and 0 background color.
146  *
147  * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
148  */
149 void tinydrm_xrgb8888_to_gray8(u8 *dst, void *vaddr, struct drm_framebuffer *fb,
150                                struct drm_rect *clip)
151 {
152         unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
153         unsigned int x, y;
154         void *buf;
155         u32 *src;
156
157         if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
158                 return;
159         /*
160          * The cma memory is write-combined so reads are uncached.
161          * Speed up by fetching one line at a time.
162          */
163         buf = kmalloc(len, GFP_KERNEL);
164         if (!buf)
165                 return;
166
167         for (y = clip->y1; y < clip->y2; y++) {
168                 src = vaddr + (y * fb->pitches[0]);
169                 src += clip->x1;
170                 memcpy(buf, src, len);
171                 src = buf;
172                 for (x = clip->x1; x < clip->x2; x++) {
173                         u8 r = (*src & 0x00ff0000) >> 16;
174                         u8 g = (*src & 0x0000ff00) >> 8;
175                         u8 b =  *src & 0x000000ff;
176
177                         /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
178                         *dst++ = (3 * r + 6 * g + b) / 10;
179                         src++;
180                 }
181         }
182
183         kfree(buf);
184 }
185 EXPORT_SYMBOL(tinydrm_xrgb8888_to_gray8);
186
187 #if IS_ENABLED(CONFIG_SPI)
188
189 /**
190  * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
191  * @spi: SPI device
192  * @max_len: Maximum buffer size needed (optional)
193  *
194  * This function returns the maximum size to use for SPI transfers. It checks
195  * the SPI master, the optional @max_len and the module parameter spi_max and
196  * returns the smallest.
197  *
198  * Returns:
199  * Maximum size for SPI transfers
200  */
201 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
202 {
203         size_t ret;
204
205         ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
206         if (max_len)
207                 ret = min(ret, max_len);
208         if (spi_max)
209                 ret = min_t(size_t, ret, spi_max);
210         ret &= ~0x3;
211         if (ret < 4)
212                 ret = 4;
213
214         return ret;
215 }
216 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
217
218 /**
219  * tinydrm_spi_bpw_supported - Check if bits per word is supported
220  * @spi: SPI device
221  * @bpw: Bits per word
222  *
223  * This function checks to see if the SPI master driver supports @bpw.
224  *
225  * Returns:
226  * True if @bpw is supported, false otherwise.
227  */
228 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
229 {
230         u32 bpw_mask = spi->master->bits_per_word_mask;
231
232         if (bpw == 8)
233                 return true;
234
235         if (!bpw_mask) {
236                 dev_warn_once(&spi->dev,
237                               "bits_per_word_mask not set, assume 8-bit only\n");
238                 return false;
239         }
240
241         if (bpw_mask & SPI_BPW_MASK(bpw))
242                 return true;
243
244         return false;
245 }
246 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
247
248 static void
249 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
250                       const void *buf, int idx, bool tx)
251 {
252         u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
253         char linebuf[3 * 32];
254
255         hex_dump_to_buffer(buf, tr->len, 16,
256                            DIV_ROUND_UP(tr->bits_per_word, 8),
257                            linebuf, sizeof(linebuf), false);
258
259         printk(KERN_DEBUG
260                "    tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
261                speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
262                speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
263                tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
264 }
265
266 /* called through tinydrm_dbg_spi_message() */
267 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
268 {
269         struct spi_transfer *tmp;
270         int i = 0;
271
272         list_for_each_entry(tmp, &m->transfers, transfer_list) {
273
274                 if (tmp->tx_buf)
275                         tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
276                 if (tmp->rx_buf)
277                         tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
278                 i++;
279         }
280 }
281 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
282
283 /**
284  * tinydrm_spi_transfer - SPI transfer helper
285  * @spi: SPI device
286  * @speed_hz: Override speed (optional)
287  * @header: Optional header transfer
288  * @bpw: Bits per word
289  * @buf: Buffer to transfer
290  * @len: Buffer length
291  *
292  * This SPI transfer helper breaks up the transfer of @buf into chunks which
293  * the SPI master driver can handle. If the machine is Little Endian and the
294  * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
295  * does a 8-bit transfer.
296  * If @header is set, it is prepended to each SPI message.
297  *
298  * Returns:
299  * Zero on success, negative error code on failure.
300  */
301 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
302                          struct spi_transfer *header, u8 bpw, const void *buf,
303                          size_t len)
304 {
305         struct spi_transfer tr = {
306                 .bits_per_word = bpw,
307                 .speed_hz = speed_hz,
308         };
309         struct spi_message m;
310         u16 *swap_buf = NULL;
311         size_t max_chunk;
312         size_t chunk;
313         int ret = 0;
314
315         if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
316                 return -EINVAL;
317
318         max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
319
320         if (drm_debug & DRM_UT_DRIVER)
321                 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
322                          __func__, bpw, max_chunk);
323
324         if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
325                 tr.bits_per_word = 8;
326                 if (tinydrm_machine_little_endian()) {
327                         swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
328                         if (!swap_buf)
329                                 return -ENOMEM;
330                 }
331         }
332
333         spi_message_init(&m);
334         if (header)
335                 spi_message_add_tail(header, &m);
336         spi_message_add_tail(&tr, &m);
337
338         while (len) {
339                 chunk = min(len, max_chunk);
340
341                 tr.tx_buf = buf;
342                 tr.len = chunk;
343
344                 if (swap_buf) {
345                         const u16 *buf16 = buf;
346                         unsigned int i;
347
348                         for (i = 0; i < chunk / 2; i++)
349                                 swap_buf[i] = swab16(buf16[i]);
350
351                         tr.tx_buf = swap_buf;
352                 }
353
354                 buf += chunk;
355                 len -= chunk;
356
357                 tinydrm_dbg_spi_message(spi, &m);
358                 ret = spi_sync(spi, &m);
359                 if (ret)
360                         return ret;
361         }
362
363         return 0;
364 }
365 EXPORT_SYMBOL(tinydrm_spi_transfer);
366
367 #endif /* CONFIG_SPI */