]> Git Repo - linux.git/blob - drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c
Merge branch 'fix-uio' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[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 <drm/tinydrm/tinydrm.h>
11 #include <drm/tinydrm/tinydrm-helpers.h>
12 #include <linux/backlight.h>
13 #include <linux/pm.h>
14 #include <linux/spi/spi.h>
15 #include <linux/swab.h>
16
17 static unsigned int spi_max;
18 module_param(spi_max, uint, 0400);
19 MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
20
21 /**
22  * tinydrm_merge_clips - Merge clip rectangles
23  * @dst: Destination clip rectangle
24  * @src: Source clip rectangle(s)
25  * @num_clips: Number of @src clip rectangles
26  * @flags: Dirty fb ioctl flags
27  * @max_width: Maximum width of @dst
28  * @max_height: Maximum height of @dst
29  *
30  * This function merges @src clip rectangle(s) into @dst. If @src is NULL,
31  * @max_width and @min_width is used to set a full @dst clip rectangle.
32  *
33  * Returns:
34  * true if it's a full clip, false otherwise
35  */
36 bool tinydrm_merge_clips(struct drm_clip_rect *dst,
37                          struct drm_clip_rect *src, unsigned int num_clips,
38                          unsigned int flags, u32 max_width, u32 max_height)
39 {
40         unsigned int i;
41
42         if (!src || !num_clips) {
43                 dst->x1 = 0;
44                 dst->x2 = max_width;
45                 dst->y1 = 0;
46                 dst->y2 = max_height;
47                 return true;
48         }
49
50         dst->x1 = ~0;
51         dst->y1 = ~0;
52         dst->x2 = 0;
53         dst->y2 = 0;
54
55         for (i = 0; i < num_clips; i++) {
56                 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY)
57                         i++;
58                 dst->x1 = min(dst->x1, src[i].x1);
59                 dst->x2 = max(dst->x2, src[i].x2);
60                 dst->y1 = min(dst->y1, src[i].y1);
61                 dst->y2 = max(dst->y2, src[i].y2);
62         }
63
64         if (dst->x2 > max_width || dst->y2 > max_height ||
65             dst->x1 >= dst->x2 || dst->y1 >= dst->y2) {
66                 DRM_DEBUG_KMS("Illegal clip: x1=%u, x2=%u, y1=%u, y2=%u\n",
67                               dst->x1, dst->x2, dst->y1, dst->y2);
68                 dst->x1 = 0;
69                 dst->y1 = 0;
70                 dst->x2 = max_width;
71                 dst->y2 = max_height;
72         }
73
74         return (dst->x2 - dst->x1) == max_width &&
75                (dst->y2 - dst->y1) == max_height;
76 }
77 EXPORT_SYMBOL(tinydrm_merge_clips);
78
79 /**
80  * tinydrm_memcpy - Copy clip buffer
81  * @dst: Destination buffer
82  * @vaddr: Source buffer
83  * @fb: DRM framebuffer
84  * @clip: Clip rectangle area to copy
85  */
86 void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
87                     struct drm_clip_rect *clip)
88 {
89         unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
90         unsigned int pitch = fb->pitches[0];
91         void *src = vaddr + (clip->y1 * pitch) + (clip->x1 * cpp);
92         size_t len = (clip->x2 - clip->x1) * cpp;
93         unsigned int y;
94
95         for (y = clip->y1; y < clip->y2; y++) {
96                 memcpy(dst, src, len);
97                 src += pitch;
98                 dst += len;
99         }
100 }
101 EXPORT_SYMBOL(tinydrm_memcpy);
102
103 /**
104  * tinydrm_swab16 - Swap bytes into clip buffer
105  * @dst: RGB565 destination buffer
106  * @vaddr: RGB565 source buffer
107  * @fb: DRM framebuffer
108  * @clip: Clip rectangle area to copy
109  */
110 void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
111                     struct drm_clip_rect *clip)
112 {
113         size_t len = (clip->x2 - clip->x1) * sizeof(u16);
114         unsigned int x, y;
115         u16 *src, *buf;
116
117         /*
118          * The cma memory is write-combined so reads are uncached.
119          * Speed up by fetching one line at a time.
120          */
121         buf = kmalloc(len, GFP_KERNEL);
122         if (!buf)
123                 return;
124
125         for (y = clip->y1; y < clip->y2; y++) {
126                 src = vaddr + (y * fb->pitches[0]);
127                 src += clip->x1;
128                 memcpy(buf, src, len);
129                 src = buf;
130                 for (x = clip->x1; x < clip->x2; x++)
131                         *dst++ = swab16(*src++);
132         }
133
134         kfree(buf);
135 }
136 EXPORT_SYMBOL(tinydrm_swab16);
137
138 /**
139  * tinydrm_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
140  * @dst: RGB565 destination buffer
141  * @vaddr: XRGB8888 source buffer
142  * @fb: DRM framebuffer
143  * @clip: Clip rectangle area to copy
144  * @swap: Swap bytes
145  *
146  * Drivers can use this function for RGB565 devices that don't natively
147  * support XRGB8888.
148  */
149 void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
150                                 struct drm_framebuffer *fb,
151                                 struct drm_clip_rect *clip, bool swap)
152 {
153         size_t len = (clip->x2 - clip->x1) * sizeof(u32);
154         unsigned int x, y;
155         u32 *src, *buf;
156         u16 val16;
157
158         buf = kmalloc(len, GFP_KERNEL);
159         if (!buf)
160                 return;
161
162         for (y = clip->y1; y < clip->y2; y++) {
163                 src = vaddr + (y * fb->pitches[0]);
164                 src += clip->x1;
165                 memcpy(buf, src, len);
166                 src = buf;
167                 for (x = clip->x1; x < clip->x2; x++) {
168                         val16 = ((*src & 0x00F80000) >> 8) |
169                                 ((*src & 0x0000FC00) >> 5) |
170                                 ((*src & 0x000000F8) >> 3);
171                         src++;
172                         if (swap)
173                                 *dst++ = swab16(val16);
174                         else
175                                 *dst++ = val16;
176                 }
177         }
178
179         kfree(buf);
180 }
181 EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
182
183 /**
184  * tinydrm_of_find_backlight - Find backlight device in device-tree
185  * @dev: Device
186  *
187  * This function looks for a DT node pointed to by a property named 'backlight'
188  * and uses of_find_backlight_by_node() to get the backlight device.
189  * Additionally if the brightness property is zero, it is set to
190  * max_brightness.
191  *
192  * Returns:
193  * NULL if there's no backlight property.
194  * Error pointer -EPROBE_DEFER if the DT node is found, but no backlight device
195  * is found.
196  * If the backlight device is found, a pointer to the structure is returned.
197  */
198 struct backlight_device *tinydrm_of_find_backlight(struct device *dev)
199 {
200         struct backlight_device *backlight;
201         struct device_node *np;
202
203         np = of_parse_phandle(dev->of_node, "backlight", 0);
204         if (!np)
205                 return NULL;
206
207         backlight = of_find_backlight_by_node(np);
208         of_node_put(np);
209
210         if (!backlight)
211                 return ERR_PTR(-EPROBE_DEFER);
212
213         if (!backlight->props.brightness) {
214                 backlight->props.brightness = backlight->props.max_brightness;
215                 DRM_DEBUG_KMS("Backlight brightness set to %d\n",
216                               backlight->props.brightness);
217         }
218
219         return backlight;
220 }
221 EXPORT_SYMBOL(tinydrm_of_find_backlight);
222
223 /**
224  * tinydrm_enable_backlight - Enable backlight helper
225  * @backlight: Backlight device
226  *
227  * Returns:
228  * Zero on success, negative error code on failure.
229  */
230 int tinydrm_enable_backlight(struct backlight_device *backlight)
231 {
232         unsigned int old_state;
233         int ret;
234
235         if (!backlight)
236                 return 0;
237
238         old_state = backlight->props.state;
239         backlight->props.state &= ~BL_CORE_FBBLANK;
240         DRM_DEBUG_KMS("Backlight state: 0x%x -> 0x%x\n", old_state,
241                       backlight->props.state);
242
243         ret = backlight_update_status(backlight);
244         if (ret)
245                 DRM_ERROR("Failed to enable backlight %d\n", ret);
246
247         return ret;
248 }
249 EXPORT_SYMBOL(tinydrm_enable_backlight);
250
251 /**
252  * tinydrm_disable_backlight - Disable backlight helper
253  * @backlight: Backlight device
254  *
255  * Returns:
256  * Zero on success, negative error code on failure.
257  */
258 int tinydrm_disable_backlight(struct backlight_device *backlight)
259 {
260         unsigned int old_state;
261         int ret;
262
263         if (!backlight)
264                 return 0;
265
266         old_state = backlight->props.state;
267         backlight->props.state |= BL_CORE_FBBLANK;
268         DRM_DEBUG_KMS("Backlight state: 0x%x -> 0x%x\n", old_state,
269                       backlight->props.state);
270         ret = backlight_update_status(backlight);
271         if (ret)
272                 DRM_ERROR("Failed to disable backlight %d\n", ret);
273
274         return ret;
275 }
276 EXPORT_SYMBOL(tinydrm_disable_backlight);
277
278 #if IS_ENABLED(CONFIG_SPI)
279
280 /**
281  * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
282  * @spi: SPI device
283  * @max_len: Maximum buffer size needed (optional)
284  *
285  * This function returns the maximum size to use for SPI transfers. It checks
286  * the SPI master, the optional @max_len and the module parameter spi_max and
287  * returns the smallest.
288  *
289  * Returns:
290  * Maximum size for SPI transfers
291  */
292 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
293 {
294         size_t ret;
295
296         ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
297         if (max_len)
298                 ret = min(ret, max_len);
299         if (spi_max)
300                 ret = min_t(size_t, ret, spi_max);
301         ret &= ~0x3;
302         if (ret < 4)
303                 ret = 4;
304
305         return ret;
306 }
307 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
308
309 /**
310  * tinydrm_spi_bpw_supported - Check if bits per word is supported
311  * @spi: SPI device
312  * @bpw: Bits per word
313  *
314  * This function checks to see if the SPI master driver supports @bpw.
315  *
316  * Returns:
317  * True if @bpw is supported, false otherwise.
318  */
319 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
320 {
321         u32 bpw_mask = spi->master->bits_per_word_mask;
322
323         if (bpw == 8)
324                 return true;
325
326         if (!bpw_mask) {
327                 dev_warn_once(&spi->dev,
328                               "bits_per_word_mask not set, assume 8-bit only\n");
329                 return false;
330         }
331
332         if (bpw_mask & SPI_BPW_MASK(bpw))
333                 return true;
334
335         return false;
336 }
337 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
338
339 static void
340 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
341                       const void *buf, int idx, bool tx)
342 {
343         u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
344         char linebuf[3 * 32];
345
346         hex_dump_to_buffer(buf, tr->len, 16,
347                            DIV_ROUND_UP(tr->bits_per_word, 8),
348                            linebuf, sizeof(linebuf), false);
349
350         printk(KERN_DEBUG
351                "    tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
352                speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
353                speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
354                tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
355 }
356
357 /* called through tinydrm_dbg_spi_message() */
358 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
359 {
360         struct spi_transfer *tmp;
361         struct list_head *pos;
362         int i = 0;
363
364         list_for_each(pos, &m->transfers) {
365                 tmp = list_entry(pos, struct spi_transfer, transfer_list);
366
367                 if (tmp->tx_buf)
368                         tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
369                 if (tmp->rx_buf)
370                         tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
371                 i++;
372         }
373 }
374 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
375
376 /**
377  * tinydrm_spi_transfer - SPI transfer helper
378  * @spi: SPI device
379  * @speed_hz: Override speed (optional)
380  * @header: Optional header transfer
381  * @bpw: Bits per word
382  * @buf: Buffer to transfer
383  * @len: Buffer length
384  *
385  * This SPI transfer helper breaks up the transfer of @buf into chunks which
386  * the SPI master driver can handle. If the machine is Little Endian and the
387  * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
388  * does a 8-bit transfer.
389  * If @header is set, it is prepended to each SPI message.
390  *
391  * Returns:
392  * Zero on success, negative error code on failure.
393  */
394 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
395                          struct spi_transfer *header, u8 bpw, const void *buf,
396                          size_t len)
397 {
398         struct spi_transfer tr = {
399                 .bits_per_word = bpw,
400                 .speed_hz = speed_hz,
401         };
402         struct spi_message m;
403         u16 *swap_buf = NULL;
404         size_t max_chunk;
405         size_t chunk;
406         int ret = 0;
407
408         if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
409                 return -EINVAL;
410
411         max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
412
413         if (drm_debug & DRM_UT_DRIVER)
414                 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
415                          __func__, bpw, max_chunk);
416
417         if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
418                 tr.bits_per_word = 8;
419                 if (tinydrm_machine_little_endian()) {
420                         swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
421                         if (!swap_buf)
422                                 return -ENOMEM;
423                 }
424         }
425
426         spi_message_init(&m);
427         if (header)
428                 spi_message_add_tail(header, &m);
429         spi_message_add_tail(&tr, &m);
430
431         while (len) {
432                 chunk = min(len, max_chunk);
433
434                 tr.tx_buf = buf;
435                 tr.len = chunk;
436
437                 if (swap_buf) {
438                         const u16 *buf16 = buf;
439                         unsigned int i;
440
441                         for (i = 0; i < chunk / 2; i++)
442                                 swap_buf[i] = swab16(buf16[i]);
443
444                         tr.tx_buf = swap_buf;
445                 }
446
447                 buf += chunk;
448                 len -= chunk;
449
450                 tinydrm_dbg_spi_message(spi, &m);
451                 ret = spi_sync(spi, &m);
452                 if (ret)
453                         return ret;
454         }
455
456         return 0;
457 }
458 EXPORT_SYMBOL(tinydrm_spi_transfer);
459
460 #endif /* CONFIG_SPI */
This page took 0.073396 seconds and 4 git commands to generate.