Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.17-rc6 419 lines 10 kB view raw
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 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 * 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 */ 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 * tinydrm_memcpy - Copy clip buffer 83 * @dst: Destination buffer 84 * @vaddr: Source buffer 85 * @fb: DRM framebuffer 86 * @clip: Clip rectangle area to copy 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 * tinydrm_swab16 - Swap bytes into clip buffer 107 * @dst: RGB565 destination buffer 108 * @vaddr: RGB565 source buffer 109 * @fb: DRM framebuffer 110 * @clip: Clip rectangle area to copy 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 * The cma memory is write-combined so reads are uncached. 121 * Speed up by fetching one line at a time. 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 * tinydrm_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer 142 * @dst: RGB565 destination buffer 143 * @vaddr: XRGB8888 source buffer 144 * @fb: DRM framebuffer 145 * @clip: Clip rectangle area to copy 146 * @swap: Swap bytes 147 * 148 * Drivers can use this function for RGB565 devices that don't natively 149 * support XRGB8888. 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 * tinydrm_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale 187 * @dst: 8-bit grayscale destination buffer 188 * @vaddr: XRGB8888 source buffer 189 * @fb: DRM framebuffer 190 * @clip: Clip rectangle area to copy 191 * 192 * Drm doesn't have native monochrome or grayscale support. 193 * Such drivers can announce the commonly supported XR24 format to userspace 194 * and use this function to convert to the native format. 195 * 196 * Monochrome drivers will use the most significant bit, 197 * where 1 means foreground color and 0 background color. 198 * 199 * ITU BT.601 is used for the RGB -> luma (brightness) conversion. 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 * The cma memory is write-combined so reads are uncached. 213 * Speed up by fetching one line at a time. 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 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */ 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#if IS_ENABLED(CONFIG_SPI) 240 241/** 242 * tinydrm_spi_max_transfer_size - Determine max SPI transfer size 243 * @spi: SPI device 244 * @max_len: Maximum buffer size needed (optional) 245 * 246 * This function returns the maximum size to use for SPI transfers. It checks 247 * the SPI master, the optional @max_len and the module parameter spi_max and 248 * returns the smallest. 249 * 250 * Returns: 251 * Maximum size for SPI transfers 252 */ 253size_t tinydrm_spi_max_transfer_size(struct spi_device *spi, size_t max_len) 254{ 255 size_t ret; 256 257 ret = min(spi_max_transfer_size(spi), spi->master->max_dma_len); 258 if (max_len) 259 ret = min(ret, max_len); 260 if (spi_max) 261 ret = min_t(size_t, ret, spi_max); 262 ret &= ~0x3; 263 if (ret < 4) 264 ret = 4; 265 266 return ret; 267} 268EXPORT_SYMBOL(tinydrm_spi_max_transfer_size); 269 270/** 271 * tinydrm_spi_bpw_supported - Check if bits per word is supported 272 * @spi: SPI device 273 * @bpw: Bits per word 274 * 275 * This function checks to see if the SPI master driver supports @bpw. 276 * 277 * Returns: 278 * True if @bpw is supported, false otherwise. 279 */ 280bool tinydrm_spi_bpw_supported(struct spi_device *spi, u8 bpw) 281{ 282 u32 bpw_mask = spi->master->bits_per_word_mask; 283 284 if (bpw == 8) 285 return true; 286 287 if (!bpw_mask) { 288 dev_warn_once(&spi->dev, 289 "bits_per_word_mask not set, assume 8-bit only\n"); 290 return false; 291 } 292 293 if (bpw_mask & SPI_BPW_MASK(bpw)) 294 return true; 295 296 return false; 297} 298EXPORT_SYMBOL(tinydrm_spi_bpw_supported); 299 300static void 301tinydrm_dbg_spi_print(struct spi_device *spi, struct spi_transfer *tr, 302 const void *buf, int idx, bool tx) 303{ 304 u32 speed_hz = tr->speed_hz ? tr->speed_hz : spi->max_speed_hz; 305 char linebuf[3 * 32]; 306 307 hex_dump_to_buffer(buf, tr->len, 16, 308 DIV_ROUND_UP(tr->bits_per_word, 8), 309 linebuf, sizeof(linebuf), false); 310 311 printk(KERN_DEBUG 312 " tr(%i): speed=%u%s, bpw=%i, len=%u, %s_buf=[%s%s]\n", idx, 313 speed_hz > 1000000 ? speed_hz / 1000000 : speed_hz / 1000, 314 speed_hz > 1000000 ? "MHz" : "kHz", tr->bits_per_word, tr->len, 315 tx ? "tx" : "rx", linebuf, tr->len > 16 ? " ..." : ""); 316} 317 318/* called through tinydrm_dbg_spi_message() */ 319void _tinydrm_dbg_spi_message(struct spi_device *spi, struct spi_message *m) 320{ 321 struct spi_transfer *tmp; 322 int i = 0; 323 324 list_for_each_entry(tmp, &m->transfers, transfer_list) { 325 326 if (tmp->tx_buf) 327 tinydrm_dbg_spi_print(spi, tmp, tmp->tx_buf, i, true); 328 if (tmp->rx_buf) 329 tinydrm_dbg_spi_print(spi, tmp, tmp->rx_buf, i, false); 330 i++; 331 } 332} 333EXPORT_SYMBOL(_tinydrm_dbg_spi_message); 334 335/** 336 * tinydrm_spi_transfer - SPI transfer helper 337 * @spi: SPI device 338 * @speed_hz: Override speed (optional) 339 * @header: Optional header transfer 340 * @bpw: Bits per word 341 * @buf: Buffer to transfer 342 * @len: Buffer length 343 * 344 * This SPI transfer helper breaks up the transfer of @buf into chunks which 345 * the SPI master driver can handle. If the machine is Little Endian and the 346 * SPI master driver doesn't support 16 bits per word, it swaps the bytes and 347 * does a 8-bit transfer. 348 * If @header is set, it is prepended to each SPI message. 349 * 350 * Returns: 351 * Zero on success, negative error code on failure. 352 */ 353int tinydrm_spi_transfer(struct spi_device *spi, u32 speed_hz, 354 struct spi_transfer *header, u8 bpw, const void *buf, 355 size_t len) 356{ 357 struct spi_transfer tr = { 358 .bits_per_word = bpw, 359 .speed_hz = speed_hz, 360 }; 361 struct spi_message m; 362 u16 *swap_buf = NULL; 363 size_t max_chunk; 364 size_t chunk; 365 int ret = 0; 366 367 if (WARN_ON_ONCE(bpw != 8 && bpw != 16)) 368 return -EINVAL; 369 370 max_chunk = tinydrm_spi_max_transfer_size(spi, 0); 371 372 if (drm_debug & DRM_UT_DRIVER) 373 pr_debug("[drm:%s] bpw=%u, max_chunk=%zu, transfers:\n", 374 __func__, bpw, max_chunk); 375 376 if (bpw == 16 && !tinydrm_spi_bpw_supported(spi, 16)) { 377 tr.bits_per_word = 8; 378 if (tinydrm_machine_little_endian()) { 379 swap_buf = kmalloc(min(len, max_chunk), GFP_KERNEL); 380 if (!swap_buf) 381 return -ENOMEM; 382 } 383 } 384 385 spi_message_init(&m); 386 if (header) 387 spi_message_add_tail(header, &m); 388 spi_message_add_tail(&tr, &m); 389 390 while (len) { 391 chunk = min(len, max_chunk); 392 393 tr.tx_buf = buf; 394 tr.len = chunk; 395 396 if (swap_buf) { 397 const u16 *buf16 = buf; 398 unsigned int i; 399 400 for (i = 0; i < chunk / 2; i++) 401 swap_buf[i] = swab16(buf16[i]); 402 403 tr.tx_buf = swap_buf; 404 } 405 406 buf += chunk; 407 len -= chunk; 408 409 tinydrm_dbg_spi_message(spi, &m); 410 ret = spi_sync(spi, &m); 411 if (ret) 412 return ret; 413 } 414 415 return 0; 416} 417EXPORT_SYMBOL(tinydrm_spi_transfer); 418 419#endif /* CONFIG_SPI */