1
2
3
4
5
6
7
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
19static unsigned int spi_max;
20module_param(spi_max, uint, 0400);
21MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38bool 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}
79EXPORT_SYMBOL(tinydrm_merge_clips);
80
81
82
83
84
85
86
87
88void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
89 struct drm_clip_rect *clip)
90{
91 unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
92 unsigned int pitch = fb->pitches[0];
93 void *src = vaddr + (clip->y1 * pitch) + (clip->x1 * cpp);
94 size_t len = (clip->x2 - clip->x1) * cpp;
95 unsigned int y;
96
97 for (y = clip->y1; y < clip->y2; y++) {
98 memcpy(dst, src, len);
99 src += pitch;
100 dst += len;
101 }
102}
103EXPORT_SYMBOL(tinydrm_memcpy);
104
105
106
107
108
109
110
111
112void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
113 struct drm_clip_rect *clip)
114{
115 size_t len = (clip->x2 - clip->x1) * sizeof(u16);
116 unsigned int x, y;
117 u16 *src, *buf;
118
119
120
121
122
123 buf = kmalloc(len, GFP_KERNEL);
124 if (!buf)
125 return;
126
127 for (y = clip->y1; y < clip->y2; y++) {
128 src = vaddr + (y * fb->pitches[0]);
129 src += clip->x1;
130 memcpy(buf, src, len);
131 src = buf;
132 for (x = clip->x1; x < clip->x2; x++)
133 *dst++ = swab16(*src++);
134 }
135
136 kfree(buf);
137}
138EXPORT_SYMBOL(tinydrm_swab16);
139
140
141
142
143
144
145
146
147
148
149
150
151void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
152 struct drm_framebuffer *fb,
153 struct drm_clip_rect *clip, bool swap)
154{
155 size_t len = (clip->x2 - clip->x1) * sizeof(u32);
156 unsigned int x, y;
157 u32 *src, *buf;
158 u16 val16;
159
160 buf = kmalloc(len, GFP_KERNEL);
161 if (!buf)
162 return;
163
164 for (y = clip->y1; y < clip->y2; y++) {
165 src = vaddr + (y * fb->pitches[0]);
166 src += clip->x1;
167 memcpy(buf, src, len);
168 src = buf;
169 for (x = clip->x1; x < clip->x2; x++) {
170 val16 = ((*src & 0x00F80000) >> 8) |
171 ((*src & 0x0000FC00) >> 5) |
172 ((*src & 0x000000F8) >> 3);
173 src++;
174 if (swap)
175 *dst++ = swab16(val16);
176 else
177 *dst++ = val16;
178 }
179 }
180
181 kfree(buf);
182}
183EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201void tinydrm_xrgb8888_to_gray8(u8 *dst, void *vaddr, struct drm_framebuffer *fb,
202 struct drm_clip_rect *clip)
203{
204 unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
205 unsigned int x, y;
206 void *buf;
207 u32 *src;
208
209 if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
210 return;
211
212
213
214
215 buf = kmalloc(len, GFP_KERNEL);
216 if (!buf)
217 return;
218
219 for (y = clip->y1; y < clip->y2; y++) {
220 src = vaddr + (y * fb->pitches[0]);
221 src += clip->x1;
222 memcpy(buf, src, len);
223 src = buf;
224 for (x = clip->x1; x < clip->x2; x++) {
225 u8 r = (*src & 0x00ff0000) >> 16;
226 u8 g = (*src & 0x0000ff00) >> 8;
227 u8 b = *src & 0x000000ff;
228
229
230 *dst++ = (3 * r + 6 * g + b) / 10;
231 src++;
232 }
233 }
234
235 kfree(buf);
236}
237EXPORT_SYMBOL(tinydrm_xrgb8888_to_gray8);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254struct backlight_device *tinydrm_of_find_backlight(struct device *dev)
255{
256 struct backlight_device *backlight;
257 struct device_node *np;
258
259 np = of_parse_phandle(dev->of_node, "backlight", 0);
260 if (!np)
261 return NULL;
262
263 backlight = of_find_backlight_by_node(np);
264 of_node_put(np);
265
266 if (!backlight)
267 return ERR_PTR(-EPROBE_DEFER);
268
269 if (!backlight->props.brightness) {
270 backlight->props.brightness = backlight->props.max_brightness;
271 DRM_DEBUG_KMS("Backlight brightness set to %d\n",
272 backlight->props.brightness);
273 }
274
275 return backlight;
276}
277EXPORT_SYMBOL(tinydrm_of_find_backlight);
278
279
280
281
282
283
284
285
286int tinydrm_enable_backlight(struct backlight_device *backlight)
287{
288 unsigned int old_state;
289 int ret;
290
291 if (!backlight)
292 return 0;
293
294 old_state = backlight->props.state;
295 backlight->props.state &= ~BL_CORE_FBBLANK;
296 DRM_DEBUG_KMS("Backlight state: 0x%x -> 0x%x\n", old_state,
297 backlight->props.state);
298
299 ret = backlight_update_status(backlight);
300 if (ret)
301 DRM_ERROR("Failed to enable backlight %d\n", ret);
302
303 return ret;
304}
305EXPORT_SYMBOL(tinydrm_enable_backlight);
306
307
308
309
310
311
312
313
314int tinydrm_disable_backlight(struct backlight_device *backlight)
315{
316 unsigned int old_state;
317 int ret;
318
319 if (!backlight)
320 return 0;
321
322 old_state = backlight->props.state;
323 backlight->props.state |= BL_CORE_FBBLANK;
324 DRM_DEBUG_KMS("Backlight state: 0x%x -> 0x%x\n", old_state,
325 backlight->props.state);
326 ret = backlight_update_status(backlight);
327 if (ret)
328 DRM_ERROR("Failed to disable backlight %d\n", ret);
329
330 return ret;
331}
332EXPORT_SYMBOL(tinydrm_disable_backlight);
333
334#if IS_ENABLED(CONFIG_SPI)
335
336
337
338
339
340
341
342
343
344
345
346
347
348size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
349{
350 size_t ret;
351
352 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
353 if (max_len)
354 ret = min(ret, max_len);
355 if (spi_max)
356 ret = min_t(size_t, ret, spi_max);
357 ret &= ~0x3;
358 if (ret < 4)
359 ret = 4;
360
361 return ret;
362}
363EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
364
365
366
367
368
369
370
371
372
373
374
375bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
376{
377 u32 bpw_mask = spi->master->bits_per_word_mask;
378
379 if (bpw == 8)
380 return true;
381
382 if (!bpw_mask) {
383 dev_warn_once(&spi->dev,
384 "bits_per_word_mask not set, assume 8-bit only\n");
385 return false;
386 }
387
388 if (bpw_mask & SPI_BPW_MASK(bpw))
389 return true;
390
391 return false;
392}
393EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
394
395static void
396tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
397 const void *buf, int idx, bool tx)
398{
399 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
400 char linebuf[3 * 32];
401
402 hex_dump_to_buffer(buf, tr->len, 16,
403 DIV_ROUND_UP(tr->bits_per_word, 8),
404 linebuf, sizeof(linebuf), false);
405
406 printk(KERN_DEBUG
407 " tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx,
408 speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000,
409 speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len,
410 tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : "");
411}
412
413
414void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
415{
416 struct spi_transfer *tmp;
417 struct list_head *pos;
418 int i = 0;
419
420 list_for_each(pos, &m->transfers) {
421 tmp = list_entry(pos, struct spi_transfer, transfer_list);
422
423 if (tmp->tx_buf)
424 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
425 if (tmp->rx_buf)
426 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
427 i++;
428 }
429}
430EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
451 struct spi_transfer *header, u8 bpw, const void *buf,
452 size_t len)
453{
454 struct spi_transfer tr = {
455 .bits_per_word = bpw,
456 .speed_hz = speed_hz,
457 };
458 struct spi_message m;
459 u16 *swap_buf = NULL;
460 size_t max_chunk;
461 size_t chunk;
462 int ret = 0;
463
464 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
465 return -EINVAL;
466
467 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
468
469 if (drm_debug & DRM_UT_DRIVER)
470 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
471 __func__, bpw, max_chunk);
472
473 if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) {
474 tr.bits_per_word = 8;
475 if (tinydrm_machine_little_endian()) {
476 swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL);
477 if (!swap_buf)
478 return -ENOMEM;
479 }
480 }
481
482 spi_message_init(&m);
483 if (header)
484 spi_message_add_tail(header, &m);
485 spi_message_add_tail(&tr, &m);
486
487 while (len) {
488 chunk = min(len, max_chunk);
489
490 tr.tx_buf = buf;
491 tr.len = chunk;
492
493 if (swap_buf) {
494 const u16 *buf16 = buf;
495 unsigned int i;
496
497 for (i = 0; i < chunk / 2; i++)
498 swap_buf[i] = swab16(buf16[i]);
499
500 tr.tx_buf = swap_buf;
501 }
502
503 buf += chunk;
504 len -= chunk;
505
506 tinydrm_dbg_spi_message(spi, &m);
507 ret = spi_sync(spi, &m);
508 if (ret)
509 return ret;
510 }
511
512 return 0;
513}
514EXPORT_SYMBOL(tinydrm_spi_transfer);
515
516#endif
517