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_framebuffer.h>
21 #include <drm/drm_print.h>
22 #include <drm/drm_rect.h>
23 #include <drm/tinydrm/tinydrm-helpers.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_memcpy - Copy clip buffer
31 * @dst: Destination buffer
32 * @vaddr: Source buffer
33 * @fb: DRM framebuffer
34 * @clip: Clip rectangle area to copy
36 void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
37 struct drm_rect *clip)
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;
45 for (y = clip->y1; y < clip->y2; y++) {
46 memcpy(dst, src, len);
51 EXPORT_SYMBOL(tinydrm_memcpy);
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
60 void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
61 struct drm_rect *clip)
63 size_t len = (clip->x2 - clip->x1) * sizeof(u16);
68 * The cma memory is write-combined so reads are uncached.
69 * Speed up by fetching one line at a time.
71 buf = kmalloc(len, GFP_KERNEL);
75 for (y = clip->y1; y < clip->y2; y++) {
76 src = vaddr + (y * fb->pitches[0]);
78 memcpy(buf, src, len);
80 for (x = clip->x1; x < clip->x2; x++)
81 *dst++ = swab16(*src++);
86 EXPORT_SYMBOL(tinydrm_swab16);
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
96 * Drivers can use this function for RGB565 devices that don't natively
99 void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
100 struct drm_framebuffer *fb,
101 struct drm_rect *clip, bool swap)
103 size_t len = (clip->x2 - clip->x1) * sizeof(u32);
108 buf = kmalloc(len, GFP_KERNEL);
112 for (y = clip->y1; y < clip->y2; y++) {
113 src = vaddr + (y * fb->pitches[0]);
115 memcpy(buf, src, len);
117 for (x = clip->x1; x < clip->x2; x++) {
118 val16 = ((*src & 0x00F80000) >> 8) |
119 ((*src & 0x0000FC00) >> 5) |
120 ((*src & 0x000000F8) >> 3);
123 *dst++ = swab16(val16);
131 EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
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
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.
144 * Monochrome drivers will use the most significant bit,
145 * where 1 means foreground color and 0 background color.
147 * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
149 void tinydrm_xrgb8888_to_gray8(u8 *dst, void *vaddr, struct drm_framebuffer *fb,
150 struct drm_rect *clip)
152 unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
157 if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
160 * The cma memory is write-combined so reads are uncached.
161 * Speed up by fetching one line at a time.
163 buf = kmalloc(len, GFP_KERNEL);
167 for (y = clip->y1; y < clip->y2; y++) {
168 src = vaddr + (y * fb->pitches[0]);
170 memcpy(buf, src, len);
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;
177 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
178 *dst++ = (3 * r + 6 * g + b) / 10;
185 EXPORT_SYMBOL(tinydrm_xrgb8888_to_gray8);
187 #if IS_ENABLED(CONFIG_SPI)
190 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
192 * @max_len: Maximum buffer size needed (optional)
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.
199 * Maximum size for SPI transfers
201 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
205 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
207 ret = min(ret, max_len);
209 ret = min_t(size_t, ret, spi_max);
216 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
219 * tinydrm_spi_bpw_supported - Check if bits per word is supported
221 * @bpw: Bits per word
223 * This function checks to see if the SPI master driver supports @bpw.
226 * True if @bpw is supported, false otherwise.
228 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
230 u32 bpw_mask = spi->master->bits_per_word_mask;
236 dev_warn_once(&spi->dev,
237 "bits_per_word_mask not set, assume 8-bit only\n");
241 if (bpw_mask & SPI_BPW_MASK(bpw))
246 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
249 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
250 const void *buf, int idx, bool tx)
252 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
253 char linebuf[3 * 32];
255 hex_dump_to_buffer(buf, tr->len, 16,
256 DIV_ROUND_UP(tr->bits_per_word, 8),
257 linebuf, sizeof(linebuf), false);
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 ? " ..." : "");
266 /* called through tinydrm_dbg_spi_message() */
267 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
269 struct spi_transfer *tmp;
272 list_for_each_entry(tmp, &m->transfers, transfer_list) {
275 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
277 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
281 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
284 * tinydrm_spi_transfer - SPI transfer helper
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
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.
299 * Zero on success, negative error code on failure.
301 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
302 struct spi_transfer *header, u8 bpw, const void *buf,
305 struct spi_transfer tr = {
306 .bits_per_word = bpw,
307 .speed_hz = speed_hz,
309 struct spi_message m;
310 u16 *swap_buf = NULL;
315 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
318 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
320 if (drm_debug & DRM_UT_DRIVER)
321 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
322 __func__, bpw, max_chunk);
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);
333 spi_message_init(&m);
335 spi_message_add_tail(header, &m);
336 spi_message_add_tail(&tr, &m);
339 chunk = min(len, max_chunk);
345 const u16 *buf16 = buf;
348 for (i = 0; i < chunk / 2; i++)
349 swap_buf[i] = swab16(buf16[i]);
351 tr.tx_buf = swap_buf;
357 tinydrm_dbg_spi_message(spi, &m);
358 ret = spi_sync(spi, &m);
365 EXPORT_SYMBOL(tinydrm_spi_transfer);
367 #endif /* CONFIG_SPI */