2 * Copyright (C) 2016 Noralf Trønnes
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.
10 #include <linux/backlight.h>
11 #include <linux/dma-buf.h>
12 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/swab.h>
17 #include <drm/drm_device.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_print.h>
21 #include <drm/tinydrm/tinydrm.h>
22 #include <drm/tinydrm/tinydrm-helpers.h>
23 #include <uapi/drm/drm.h>
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");
30 * tinydrm_merge_clips - Merge clip rectangles
31 * @dst: Destination clip rectangle
32 * @src: Source clip rectangle(s)
33 * @num_clips: Number of @src clip rectangles
34 * @flags: Dirty fb ioctl flags
35 * @max_width: Maximum width of @dst
36 * @max_height: Maximum height of @dst
38 * This function merges @src clip rectangle(s) into @dst. If @src is NULL,
39 * @max_width and @min_width is used to set a full @dst clip rectangle.
42 * true if it's a full clip, false otherwise
44 bool tinydrm_merge_clips(struct drm_clip_rect *dst,
45 struct drm_clip_rect *src, unsigned int num_clips,
46 unsigned int flags, u32 max_width, u32 max_height)
50 if (!src || !num_clips) {
63 for (i = 0; i < num_clips; i++) {
64 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY)
66 dst->x1 = min(dst->x1, src[i].x1);
67 dst->x2 = max(dst->x2, src[i].x2);
68 dst->y1 = min(dst->y1, src[i].y1);
69 dst->y2 = max(dst->y2, src[i].y2);
72 if (dst->x2 > max_width || dst->y2 > max_height ||
73 dst->x1 >= dst->x2 || dst->y1 >= dst->y2) {
74 DRM_DEBUG_KMS("Illegal clip: x1=%u, x2=%u, y1=%u, y2=%u\n",
75 dst->x1, dst->x2, dst->y1, dst->y2);
82 return (dst->x2 - dst->x1) == max_width &&
83 (dst->y2 - dst->y1) == max_height;
85 EXPORT_SYMBOL(tinydrm_merge_clips);
87 int tinydrm_fb_dirty(struct drm_framebuffer *fb,
88 struct drm_file *file_priv,
89 unsigned int flags, unsigned int color,
90 struct drm_clip_rect *clips,
91 unsigned int num_clips)
93 struct tinydrm_device *tdev = fb->dev->dev_private;
94 struct drm_plane *plane = &tdev->pipe.plane;
97 drm_modeset_lock(&plane->mutex, NULL);
99 /* fbdev can flush even when we're not interested */
100 if (plane->state->fb == fb) {
101 mutex_lock(&tdev->dirty_lock);
102 ret = tdev->fb_dirty(fb, file_priv, flags,
103 color, clips, num_clips);
104 mutex_unlock(&tdev->dirty_lock);
107 drm_modeset_unlock(&plane->mutex);
110 dev_err_once(fb->dev->dev,
111 "Failed to update display %d\n", ret);
115 EXPORT_SYMBOL(tinydrm_fb_dirty);
118 * tinydrm_memcpy - Copy clip buffer
119 * @dst: Destination buffer
120 * @vaddr: Source buffer
121 * @fb: DRM framebuffer
122 * @clip: Clip rectangle area to copy
124 void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
125 struct drm_clip_rect *clip)
127 unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
128 unsigned int pitch = fb->pitches[0];
129 void *src = vaddr + (clip->y1 * pitch) + (clip->x1 * cpp);
130 size_t len = (clip->x2 - clip->x1) * cpp;
133 for (y = clip->y1; y < clip->y2; y++) {
134 memcpy(dst, src, len);
139 EXPORT_SYMBOL(tinydrm_memcpy);
142 * tinydrm_swab16 - Swap bytes into clip buffer
143 * @dst: RGB565 destination buffer
144 * @vaddr: RGB565 source buffer
145 * @fb: DRM framebuffer
146 * @clip: Clip rectangle area to copy
148 void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
149 struct drm_clip_rect *clip)
151 size_t len = (clip->x2 - clip->x1) * sizeof(u16);
156 * The cma memory is write-combined so reads are uncached.
157 * Speed up by fetching one line at a time.
159 buf = kmalloc(len, GFP_KERNEL);
163 for (y = clip->y1; y < clip->y2; y++) {
164 src = vaddr + (y * fb->pitches[0]);
166 memcpy(buf, src, len);
168 for (x = clip->x1; x < clip->x2; x++)
169 *dst++ = swab16(*src++);
174 EXPORT_SYMBOL(tinydrm_swab16);
177 * tinydrm_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
178 * @dst: RGB565 destination buffer
179 * @vaddr: XRGB8888 source buffer
180 * @fb: DRM framebuffer
181 * @clip: Clip rectangle area to copy
184 * Drivers can use this function for RGB565 devices that don't natively
187 void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
188 struct drm_framebuffer *fb,
189 struct drm_clip_rect *clip, bool swap)
191 size_t len = (clip->x2 - clip->x1) * sizeof(u32);
196 buf = kmalloc(len, GFP_KERNEL);
200 for (y = clip->y1; y < clip->y2; y++) {
201 src = vaddr + (y * fb->pitches[0]);
203 memcpy(buf, src, len);
205 for (x = clip->x1; x < clip->x2; x++) {
206 val16 = ((*src & 0x00F80000) >> 8) |
207 ((*src & 0x0000FC00) >> 5) |
208 ((*src & 0x000000F8) >> 3);
211 *dst++ = swab16(val16);
219 EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
222 * tinydrm_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
223 * @dst: 8-bit grayscale destination buffer
224 * @vaddr: XRGB8888 source buffer
225 * @fb: DRM framebuffer
226 * @clip: Clip rectangle area to copy
228 * Drm doesn't have native monochrome or grayscale support.
229 * Such drivers can announce the commonly supported XR24 format to userspace
230 * and use this function to convert to the native format.
232 * Monochrome drivers will use the most significant bit,
233 * where 1 means foreground color and 0 background color.
235 * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
237 void tinydrm_xrgb8888_to_gray8(u8 *dst, void *vaddr, struct drm_framebuffer *fb,
238 struct drm_clip_rect *clip)
240 unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
245 if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
248 * The cma memory is write-combined so reads are uncached.
249 * Speed up by fetching one line at a time.
251 buf = kmalloc(len, GFP_KERNEL);
255 for (y = clip->y1; y < clip->y2; y++) {
256 src = vaddr + (y * fb->pitches[0]);
258 memcpy(buf, src, len);
260 for (x = clip->x1; x < clip->x2; x++) {
261 u8 r = (*src & 0x00ff0000) >> 16;
262 u8 g = (*src & 0x0000ff00) >> 8;
263 u8 b = *src & 0x000000ff;
265 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
266 *dst++ = (3 * r + 6 * g + b) / 10;
273 EXPORT_SYMBOL(tinydrm_xrgb8888_to_gray8);
275 #if IS_ENABLED(CONFIG_SPI)
278 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
280 * @max_len: Maximum buffer size needed (optional)
282 * This function returns the maximum size to use for SPI transfers. It checks
283 * the SPI master, the optional @max_len and the module parameter spi_max and
284 * returns the smallest.
287 * Maximum size for SPI transfers
289 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
293 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
295 ret = min(ret, max_len);
297 ret = min_t(size_t, ret, spi_max);
304 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
307 * tinydrm_spi_bpw_supported - Check if bits per word is supported
309 * @bpw: Bits per word
311 * This function checks to see if the SPI master driver supports @bpw.
314 * True if @bpw is supported, false otherwise.
316 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
318 u32 bpw_mask = spi->master->bits_per_word_mask;
324 dev_warn_once(&spi->dev,
325 "bits_per_word_mask not set, assume 8-bit only\n");
329 if (bpw_mask & SPI_BPW_MASK(bpw))
334 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
337 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
338 const void *buf, int idx, bool tx)
340 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
341 char linebuf[3 * 32];
343 hex_dump_to_buffer(buf, tr->len, 16,
344 DIV_ROUND_UP(tr->bits_per_word, 8),
345 linebuf, sizeof(linebuf), false);
348 " tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
349 speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
350 speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
351 tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
354 /* called through tinydrm_dbg_spi_message() */
355 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
357 struct spi_transfer *tmp;
360 list_for_each_entry(tmp, &m->transfers, transfer_list) {
363 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
365 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
369 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
372 * tinydrm_spi_transfer - SPI transfer helper
374 * @speed_hz: Override speed (optional)
375 * @header: Optional header transfer
376 * @bpw: Bits per word
377 * @buf: Buffer to transfer
378 * @len: Buffer length
380 * This SPI transfer helper breaks up the transfer of @buf into chunks which
381 * the SPI master driver can handle. If the machine is Little Endian and the
382 * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
383 * does a 8-bit transfer.
384 * If @header is set, it is prepended to each SPI message.
387 * Zero on success, negative error code on failure.
389 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
390 struct spi_transfer *header, u8 bpw, const void *buf,
393 struct spi_transfer tr = {
394 .bits_per_word = bpw,
395 .speed_hz = speed_hz,
397 struct spi_message m;
398 u16 *swap_buf = NULL;
403 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
406 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
408 if (drm_debug & DRM_UT_DRIVER)
409 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
410 __func__, bpw, max_chunk);
412 if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
413 tr.bits_per_word = 8;
414 if (tinydrm_machine_little_endian()) {
415 swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
421 spi_message_init(&m);
423 spi_message_add_tail(header, &m);
424 spi_message_add_tail(&tr, &m);
427 chunk = min(len, max_chunk);
433 const u16 *buf16 = buf;
436 for (i = 0; i < chunk / 2; i++)
437 swap_buf[i] = swab16(buf16[i]);
439 tr.tx_buf = swap_buf;
445 tinydrm_dbg_spi_message(spi, &m);
446 ret = spi_sync(spi, &m);
453 EXPORT_SYMBOL(tinydrm_spi_transfer);
455 #endif /* CONFIG_SPI */