1
2
3
4
5
6
7
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
17static unsigned int spi_max;
18module_param(spi_max, uint, 0400);
19MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36bool 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}
77EXPORT_SYMBOL(tinydrm_merge_clips);
78
79
80
81
82
83
84
85
86void 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}
101EXPORT_SYMBOL(tinydrm_memcpy);
102
103
104
105
106
107
108
109
110void 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
119
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}
136EXPORT_SYMBOL(tinydrm_swab16);
137
138
139
140
141
142
143
144
145
146
147
148
149void 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}
181EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198struct 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}
221EXPORT_SYMBOL(tinydrm_of_find_backlight);
222
223
224
225
226
227
228
229
230int 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}
249EXPORT_SYMBOL(tinydrm_enable_backlight);
250
251
252
253
254
255
256
257
258int 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}
276EXPORT_SYMBOL(tinydrm_disable_backlight);
277
278#if IS_ENABLED(CONFIG_SPI)
279
280
281
282
283
284
285
286
287
288
289
290
291
292size_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}
307EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
308
309
310
311
312
313
314
315
316
317
318
319bool 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}
337EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
338
339static void
340tinydrm_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
358void _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}
374EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394int 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}
458EXPORT_SYMBOL(tinydrm_spi_transfer);
459
460#endif
461