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