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 <drm/tinydrm/tinydrm.h>
11 #include <drm/tinydrm/tinydrm-helpers.h>
12 #include <linux/backlight.h>
14 #include <linux/spi/spi.h>
15 #include <linux/swab.h>
17 static unsigned int spi_max;
18 module_param(spi_max, uint, 0400);
19 MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
22 * tinydrm_merge_clips - Merge clip rectangles
23 * @dst: Destination clip rectangle
24 * @src: Source clip rectangle(s)
25 * @num_clips: Number of @src clip rectangles
26 * @flags: Dirty fb ioctl flags
27 * @max_width: Maximum width of @dst
28 * @max_height: Maximum height of @dst
30 * This function merges @src clip rectangle(s) into @dst. If @src is NULL,
31 * @max_width and @min_width is used to set a full @dst clip rectangle.
34 * true if it's a full clip, false otherwise
36 bool 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)
42 if (!src || !num_clips) {
55 for (i = 0; i < num_clips; i++) {
56 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY)
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);
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);
74 return (dst->x2 - dst->x1) == max_width &&
75 (dst->y2 - dst->y1) == max_height;
77 EXPORT_SYMBOL(tinydrm_merge_clips);
80 * tinydrm_memcpy - Copy clip buffer
81 * @dst: Destination buffer
82 * @vaddr: Source buffer
83 * @fb: DRM framebuffer
84 * @clip: Clip rectangle area to copy
86 void tinydrm_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
87 struct drm_clip_rect *clip)
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;
95 for (y = clip->y1; y < clip->y2; y++) {
96 memcpy(dst, src, len);
101 EXPORT_SYMBOL(tinydrm_memcpy);
104 * tinydrm_swab16 - Swap bytes into clip buffer
105 * @dst: RGB565 destination buffer
106 * @vaddr: RGB565 source buffer
107 * @fb: DRM framebuffer
108 * @clip: Clip rectangle area to copy
110 void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
111 struct drm_clip_rect *clip)
113 size_t len = (clip->x2 - clip->x1) * sizeof(u16);
118 * The cma memory is write-combined so reads are uncached.
119 * Speed up by fetching one line at a time.
121 buf = kmalloc(len, GFP_KERNEL);
125 for (y = clip->y1; y < clip->y2; y++) {
126 src = vaddr + (y * fb->pitches[0]);
128 memcpy(buf, src, len);
130 for (x = clip->x1; x < clip->x2; x++)
131 *dst++ = swab16(*src++);
136 EXPORT_SYMBOL(tinydrm_swab16);
139 * tinydrm_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
140 * @dst: RGB565 destination buffer
141 * @vaddr: XRGB8888 source buffer
142 * @fb: DRM framebuffer
143 * @clip: Clip rectangle area to copy
146 * Drivers can use this function for RGB565 devices that don't natively
149 void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
150 struct drm_framebuffer *fb,
151 struct drm_clip_rect *clip, bool swap)
153 size_t len = (clip->x2 - clip->x1) * sizeof(u32);
158 buf = kmalloc(len, GFP_KERNEL);
162 for (y = clip->y1; y < clip->y2; y++) {
163 src = vaddr + (y * fb->pitches[0]);
165 memcpy(buf, src, len);
167 for (x = clip->x1; x < clip->x2; x++) {
168 val16 = ((*src & 0x00F80000) >> 8) |
169 ((*src & 0x0000FC00) >> 5) |
170 ((*src & 0x000000F8) >> 3);
173 *dst++ = swab16(val16);
181 EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
184 * tinydrm_of_find_backlight - Find backlight device in device-tree
187 * This function looks for a DT node pointed to by a property named 'backlight'
188 * and uses of_find_backlight_by_node() to get the backlight device.
189 * Additionally if the brightness property is zero, it is set to
193 * NULL if there's no backlight property.
194 * Error pointer -EPROBE_DEFER if the DT node is found, but no backlight device
196 * If the backlight device is found, a pointer to the structure is returned.
198 struct backlight_device *tinydrm_of_find_backlight(struct device *dev)
200 struct backlight_device *backlight;
201 struct device_node *np;
203 np = of_parse_phandle(dev->of_node, "backlight", 0);
207 backlight = of_find_backlight_by_node(np);
211 return ERR_PTR(-EPROBE_DEFER);
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);
221 EXPORT_SYMBOL(tinydrm_of_find_backlight);
224 * tinydrm_enable_backlight - Enable backlight helper
225 * @backlight: Backlight device
228 * Zero on success, negative error code on failure.
230 int tinydrm_enable_backlight(struct backlight_device *backlight)
232 unsigned int old_state;
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);
243 ret = backlight_update_status(backlight);
245 DRM_ERROR("Failed to enable backlight %d\n", ret);
249 EXPORT_SYMBOL(tinydrm_enable_backlight);
252 * tinydrm_disable_backlight - Disable backlight helper
253 * @backlight: Backlight device
256 * Zero on success, negative error code on failure.
258 int tinydrm_disable_backlight(struct backlight_device *backlight)
260 unsigned int old_state;
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);
272 DRM_ERROR("Failed to disable backlight %d\n", ret);
276 EXPORT_SYMBOL(tinydrm_disable_backlight);
278 #if IS_ENABLED(CONFIG_SPI)
281 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size
283 * @max_len: Maximum buffer size needed (optional)
285 * This function returns the maximum size to use for SPI transfers. It checks
286 * the SPI master, the optional @max_len and the module parameter spi_max and
287 * returns the smallest.
290 * Maximum size for SPI transfers
292 size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len)
296 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len);
298 ret = min(ret, max_len);
300 ret = min_t(size_t, ret, spi_max);
307 EXPORT_SYMBOL(tinydrm_spi_max_transfer_size);
310 * tinydrm_spi_bpw_supported - Check if bits per word is supported
312 * @bpw: Bits per word
314 * This function checks to see if the SPI master driver supports @bpw.
317 * True if @bpw is supported, false otherwise.
319 bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw)
321 u32 bpw_mask = spi->master->bits_per_word_mask;
327 dev_warn_once(&spi->dev,
328 "bits_per_word_mask not set, assume 8-bit only\n");
332 if (bpw_mask & SPI_BPW_MASK(bpw))
337 EXPORT_SYMBOL(tinydrm_spi_bpw_supported);
340 tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr,
341 const void *buf, int idx, bool tx)
343 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz;
344 char linebuf[3 * 32];
346 hex_dump_to_buffer(buf, tr->len, 16,
347 DIV_ROUND_UP(tr->bits_per_word, 8),
348 linebuf, sizeof(linebuf), false);
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 ? " ..." : "");
357 /* called through tinydrm_dbg_spi_message() */
358 void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m)
360 struct spi_transfer *tmp;
361 struct list_head *pos;
364 list_for_each(pos, &m->transfers) {
365 tmp = list_entry(pos, struct spi_transfer, transfer_list);
368 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true);
370 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false);
374 EXPORT_SYMBOL(_tinydrm_dbg_spi_message);
377 * tinydrm_spi_transfer - SPI transfer helper
379 * @speed_hz: Override speed (optional)
380 * @header: Optional header transfer
381 * @bpw: Bits per word
382 * @buf: Buffer to transfer
383 * @len: Buffer length
385 * This SPI transfer helper breaks up the transfer of @buf into chunks which
386 * the SPI master driver can handle. If the machine is Little Endian and the
387 * SPI master driver doesn't support 16 bits per word, it swaps the bytes and
388 * does a 8-bit transfer.
389 * If @header is set, it is prepended to each SPI message.
392 * Zero on success, negative error code on failure.
394 int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz,
395 struct spi_transfer *header, u8 bpw, const void *buf,
398 struct spi_transfer tr = {
399 .bits_per_word = bpw,
400 .speed_hz = speed_hz,
402 struct spi_message m;
403 u16 *swap_buf = NULL;
408 if (WARN_ON_ONCE(bpw != 8 && bpw != 16))
411 max_chunk = tinydrm_spi_max_transfer_size(spi, 0);
413 if (drm_debug & DRM_UT_DRIVER)
414 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n",
415 __func__, bpw, max_chunk);
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);
426 spi_message_init(&m);
428 spi_message_add_tail(header, &m);
429 spi_message_add_tail(&tr, &m);
432 chunk = min(len, max_chunk);
438 const u16 *buf16 = buf;
441 for (i = 0; i < chunk / 2; i++)
442 swap_buf[i] = swab16(buf16[i]);
444 tr.tx_buf = swap_buf;
450 tinydrm_dbg_spi_message(spi, &m);
451 ret = spi_sync(spi, &m);
458 EXPORT_SYMBOL(tinydrm_spi_transfer);
460 #endif /* CONFIG_SPI */