tinydrm-helpers.c 11 KB

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