sti_cursor.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Vincent Abriou <vincent.abriou@st.com>
  4. * Fabien Dessenne <fabien.dessenne@st.com>
  5. * for STMicroelectronics.
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include <drm/drmP.h>
  9. #include "sti_cursor.h"
  10. #include "sti_layer.h"
  11. #include "sti_vtg.h"
  12. /* Registers */
  13. #define CUR_CTL 0x00
  14. #define CUR_VPO 0x0C
  15. #define CUR_PML 0x14
  16. #define CUR_PMP 0x18
  17. #define CUR_SIZE 0x1C
  18. #define CUR_CML 0x20
  19. #define CUR_AWS 0x28
  20. #define CUR_AWE 0x2C
  21. #define CUR_CTL_CLUT_UPDATE BIT(1)
  22. #define STI_CURS_MIN_SIZE 1
  23. #define STI_CURS_MAX_SIZE 128
  24. /*
  25. * pixmap dma buffer stucture
  26. *
  27. * @paddr: physical address
  28. * @size: buffer size
  29. * @base: virtual address
  30. */
  31. struct dma_pixmap {
  32. dma_addr_t paddr;
  33. size_t size;
  34. void *base;
  35. };
  36. /**
  37. * STI Cursor structure
  38. *
  39. * @layer: layer structure
  40. * @width: cursor width
  41. * @height: cursor height
  42. * @clut: color look up table
  43. * @clut_paddr: color look up table physical address
  44. * @pixmap: pixmap dma buffer (clut8-format cursor)
  45. */
  46. struct sti_cursor {
  47. struct sti_layer layer;
  48. unsigned int width;
  49. unsigned int height;
  50. unsigned short *clut;
  51. dma_addr_t clut_paddr;
  52. struct dma_pixmap pixmap;
  53. };
  54. static const uint32_t cursor_supported_formats[] = {
  55. DRM_FORMAT_ARGB8888,
  56. };
  57. #define to_sti_cursor(x) container_of(x, struct sti_cursor, layer)
  58. static const uint32_t *sti_cursor_get_formats(struct sti_layer *layer)
  59. {
  60. return cursor_supported_formats;
  61. }
  62. static unsigned int sti_cursor_get_nb_formats(struct sti_layer *layer)
  63. {
  64. return ARRAY_SIZE(cursor_supported_formats);
  65. }
  66. static void sti_cursor_argb8888_to_clut8(struct sti_layer *layer)
  67. {
  68. struct sti_cursor *cursor = to_sti_cursor(layer);
  69. u32 *src = layer->vaddr;
  70. u8 *dst = cursor->pixmap.base;
  71. unsigned int i, j;
  72. u32 a, r, g, b;
  73. for (i = 0; i < cursor->height; i++) {
  74. for (j = 0; j < cursor->width; j++) {
  75. /* Pick the 2 higher bits of each component */
  76. a = (*src >> 30) & 3;
  77. r = (*src >> 22) & 3;
  78. g = (*src >> 14) & 3;
  79. b = (*src >> 6) & 3;
  80. *dst = a << 6 | r << 4 | g << 2 | b;
  81. src++;
  82. dst++;
  83. }
  84. }
  85. }
  86. static int sti_cursor_prepare_layer(struct sti_layer *layer, bool first_prepare)
  87. {
  88. struct sti_cursor *cursor = to_sti_cursor(layer);
  89. struct drm_display_mode *mode = layer->mode;
  90. u32 y, x;
  91. u32 val;
  92. DRM_DEBUG_DRIVER("\n");
  93. dev_dbg(layer->dev, "%s %s\n", __func__, sti_layer_to_str(layer));
  94. if (layer->src_w < STI_CURS_MIN_SIZE ||
  95. layer->src_h < STI_CURS_MIN_SIZE ||
  96. layer->src_w > STI_CURS_MAX_SIZE ||
  97. layer->src_h > STI_CURS_MAX_SIZE) {
  98. DRM_ERROR("Invalid cursor size (%dx%d)\n",
  99. layer->src_w, layer->src_h);
  100. return -EINVAL;
  101. }
  102. /* If the cursor size has changed, re-allocated the pixmap */
  103. if (!cursor->pixmap.base ||
  104. (cursor->width != layer->src_w) ||
  105. (cursor->height != layer->src_h)) {
  106. cursor->width = layer->src_w;
  107. cursor->height = layer->src_h;
  108. if (cursor->pixmap.base)
  109. dma_free_writecombine(layer->dev,
  110. cursor->pixmap.size,
  111. cursor->pixmap.base,
  112. cursor->pixmap.paddr);
  113. cursor->pixmap.size = cursor->width * cursor->height;
  114. cursor->pixmap.base = dma_alloc_writecombine(layer->dev,
  115. cursor->pixmap.size,
  116. &cursor->pixmap.paddr,
  117. GFP_KERNEL | GFP_DMA);
  118. if (!cursor->pixmap.base) {
  119. DRM_ERROR("Failed to allocate memory for pixmap\n");
  120. return -ENOMEM;
  121. }
  122. }
  123. /* Convert ARGB8888 to CLUT8 */
  124. sti_cursor_argb8888_to_clut8(layer);
  125. /* AWS and AWE depend on the mode */
  126. y = sti_vtg_get_line_number(*mode, 0);
  127. x = sti_vtg_get_pixel_number(*mode, 0);
  128. val = y << 16 | x;
  129. writel(val, layer->regs + CUR_AWS);
  130. y = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  131. x = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  132. val = y << 16 | x;
  133. writel(val, layer->regs + CUR_AWE);
  134. if (first_prepare) {
  135. /* Set and fetch CLUT */
  136. writel(cursor->clut_paddr, layer->regs + CUR_CML);
  137. writel(CUR_CTL_CLUT_UPDATE, layer->regs + CUR_CTL);
  138. }
  139. return 0;
  140. }
  141. static int sti_cursor_commit_layer(struct sti_layer *layer)
  142. {
  143. struct sti_cursor *cursor = to_sti_cursor(layer);
  144. struct drm_display_mode *mode = layer->mode;
  145. u32 ydo, xdo;
  146. dev_dbg(layer->dev, "%s %s\n", __func__, sti_layer_to_str(layer));
  147. /* Set memory location, size, and position */
  148. writel(cursor->pixmap.paddr, layer->regs + CUR_PML);
  149. writel(cursor->width, layer->regs + CUR_PMP);
  150. writel(cursor->height << 16 | cursor->width, layer->regs + CUR_SIZE);
  151. ydo = sti_vtg_get_line_number(*mode, layer->dst_y);
  152. xdo = sti_vtg_get_pixel_number(*mode, layer->dst_y);
  153. writel((ydo << 16) | xdo, layer->regs + CUR_VPO);
  154. return 0;
  155. }
  156. static int sti_cursor_disable_layer(struct sti_layer *layer)
  157. {
  158. return 0;
  159. }
  160. static void sti_cursor_init(struct sti_layer *layer)
  161. {
  162. struct sti_cursor *cursor = to_sti_cursor(layer);
  163. unsigned short *base = cursor->clut;
  164. unsigned int a, r, g, b;
  165. /* Assign CLUT values, ARGB444 format */
  166. for (a = 0; a < 4; a++)
  167. for (r = 0; r < 4; r++)
  168. for (g = 0; g < 4; g++)
  169. for (b = 0; b < 4; b++)
  170. *base++ = (a * 5) << 12 |
  171. (r * 5) << 8 |
  172. (g * 5) << 4 |
  173. (b * 5);
  174. }
  175. static const struct sti_layer_funcs cursor_ops = {
  176. .get_formats = sti_cursor_get_formats,
  177. .get_nb_formats = sti_cursor_get_nb_formats,
  178. .init = sti_cursor_init,
  179. .prepare = sti_cursor_prepare_layer,
  180. .commit = sti_cursor_commit_layer,
  181. .disable = sti_cursor_disable_layer,
  182. };
  183. struct sti_layer *sti_cursor_create(struct device *dev)
  184. {
  185. struct sti_cursor *cursor;
  186. cursor = devm_kzalloc(dev, sizeof(*cursor), GFP_KERNEL);
  187. if (!cursor) {
  188. DRM_ERROR("Failed to allocate memory for cursor\n");
  189. return NULL;
  190. }
  191. /* Allocate clut buffer */
  192. cursor->clut = dma_alloc_writecombine(dev,
  193. 0x100 * sizeof(unsigned short),
  194. &cursor->clut_paddr,
  195. GFP_KERNEL | GFP_DMA);
  196. if (!cursor->clut) {
  197. DRM_ERROR("Failed to allocate memory for cursor clut\n");
  198. devm_kfree(dev, cursor);
  199. return NULL;
  200. }
  201. cursor->layer.ops = &cursor_ops;
  202. return (struct sti_layer *)cursor;
  203. }