hdlcd_crtc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2013-2015 ARM Limited
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file COPYING in the main directory of this archive
  7. * for more details.
  8. *
  9. * Implementation of a CRTC class for the HDLCD driver.
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_crtc.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_fb_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include <drm/drm_gem_cma_helper.h>
  18. #include <drm/drm_of.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <linux/clk.h>
  21. #include <linux/of_graph.h>
  22. #include <linux/platform_data/simplefb.h>
  23. #include <video/videomode.h>
  24. #include "hdlcd_drv.h"
  25. #include "hdlcd_regs.h"
  26. /*
  27. * The HDLCD controller is a dumb RGB streamer that gets connected to
  28. * a single HDMI transmitter or in the case of the ARM Models it gets
  29. * emulated by the software that does the actual rendering.
  30. *
  31. */
  32. static void hdlcd_crtc_cleanup(struct drm_crtc *crtc)
  33. {
  34. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  35. /* stop the controller on cleanup */
  36. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  37. drm_crtc_cleanup(crtc);
  38. }
  39. static const struct drm_crtc_funcs hdlcd_crtc_funcs = {
  40. .destroy = hdlcd_crtc_cleanup,
  41. .set_config = drm_atomic_helper_set_config,
  42. .page_flip = drm_atomic_helper_page_flip,
  43. .reset = drm_atomic_helper_crtc_reset,
  44. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  45. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  46. };
  47. static struct simplefb_format supported_formats[] = SIMPLEFB_FORMATS;
  48. /*
  49. * Setup the HDLCD registers for decoding the pixels out of the framebuffer
  50. */
  51. static int hdlcd_set_pxl_fmt(struct drm_crtc *crtc)
  52. {
  53. unsigned int btpp;
  54. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  55. uint32_t pixel_format;
  56. struct simplefb_format *format = NULL;
  57. int i;
  58. pixel_format = crtc->primary->state->fb->pixel_format;
  59. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  60. if (supported_formats[i].fourcc == pixel_format)
  61. format = &supported_formats[i];
  62. }
  63. if (WARN_ON(!format))
  64. return 0;
  65. /* HDLCD uses 'bytes per pixel', zero means 1 byte */
  66. btpp = (format->bits_per_pixel + 7) / 8;
  67. hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3);
  68. /*
  69. * The format of the HDLCD_REG_<color>_SELECT register is:
  70. * - bits[23:16] - default value for that color component
  71. * - bits[11:8] - number of bits to extract for each color component
  72. * - bits[4:0] - index of the lowest bit to extract
  73. *
  74. * The default color value is used when bits[11:8] are zero, when the
  75. * pixel is outside the visible frame area or when there is a
  76. * buffer underrun.
  77. */
  78. hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset |
  79. #ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN
  80. 0x00ff0000 | /* show underruns in red */
  81. #endif
  82. ((format->red.length & 0xf) << 8));
  83. hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset |
  84. ((format->green.length & 0xf) << 8));
  85. hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset |
  86. ((format->blue.length & 0xf) << 8));
  87. return 0;
  88. }
  89. static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc)
  90. {
  91. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  92. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  93. struct videomode vm;
  94. unsigned int polarities, err;
  95. vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay;
  96. vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end;
  97. vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start;
  98. vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay;
  99. vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end;
  100. vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start;
  101. polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;
  102. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  103. polarities |= HDLCD_POLARITY_HSYNC;
  104. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  105. polarities |= HDLCD_POLARITY_VSYNC;
  106. /* Allow max number of outstanding requests and largest burst size */
  107. hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
  108. HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);
  109. hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1);
  110. hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1);
  111. hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1);
  112. hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1);
  113. hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1);
  114. hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1);
  115. hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1);
  116. hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1);
  117. hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);
  118. err = hdlcd_set_pxl_fmt(crtc);
  119. if (err)
  120. return;
  121. clk_set_rate(hdlcd->clk, m->crtc_clock * 1000);
  122. }
  123. static void hdlcd_crtc_enable(struct drm_crtc *crtc)
  124. {
  125. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  126. clk_prepare_enable(hdlcd->clk);
  127. hdlcd_crtc_mode_set_nofb(crtc);
  128. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
  129. drm_crtc_vblank_on(crtc);
  130. }
  131. static void hdlcd_crtc_disable(struct drm_crtc *crtc)
  132. {
  133. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  134. drm_crtc_vblank_off(crtc);
  135. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  136. clk_disable_unprepare(hdlcd->clk);
  137. }
  138. static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
  139. struct drm_crtc_state *state)
  140. {
  141. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  142. struct drm_display_mode *mode = &state->adjusted_mode;
  143. long rate, clk_rate = mode->clock * 1000;
  144. rate = clk_round_rate(hdlcd->clk, clk_rate);
  145. if (rate != clk_rate) {
  146. /* clock required by mode not supported by hardware */
  147. return -EINVAL;
  148. }
  149. return 0;
  150. }
  151. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  152. struct drm_crtc_state *state)
  153. {
  154. struct drm_pending_vblank_event *event = crtc->state->event;
  155. if (event) {
  156. crtc->state->event = NULL;
  157. spin_lock_irq(&crtc->dev->event_lock);
  158. if (drm_crtc_vblank_get(crtc) == 0)
  159. drm_crtc_arm_vblank_event(crtc, event);
  160. else
  161. drm_crtc_send_vblank_event(crtc, event);
  162. spin_unlock_irq(&crtc->dev->event_lock);
  163. }
  164. }
  165. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  166. .enable = hdlcd_crtc_enable,
  167. .disable = hdlcd_crtc_disable,
  168. .atomic_check = hdlcd_crtc_atomic_check,
  169. .atomic_begin = hdlcd_crtc_atomic_begin,
  170. };
  171. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  172. struct drm_plane_state *state)
  173. {
  174. u32 src_w, src_h;
  175. src_w = state->src_w >> 16;
  176. src_h = state->src_h >> 16;
  177. /* we can't do any scaling of the plane source */
  178. if ((src_w != state->crtc_w) || (src_h != state->crtc_h))
  179. return -EINVAL;
  180. return 0;
  181. }
  182. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  183. struct drm_plane_state *state)
  184. {
  185. struct hdlcd_drm_private *hdlcd;
  186. struct drm_gem_cma_object *gem;
  187. unsigned int depth, bpp;
  188. u32 src_w, src_h, dest_w, dest_h;
  189. dma_addr_t scanout_start;
  190. if (!plane->state->fb)
  191. return;
  192. drm_fb_get_bpp_depth(plane->state->fb->pixel_format, &depth, &bpp);
  193. src_w = plane->state->src_w >> 16;
  194. src_h = plane->state->src_h >> 16;
  195. dest_w = plane->state->crtc_w;
  196. dest_h = plane->state->crtc_h;
  197. gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
  198. scanout_start = gem->paddr + plane->state->fb->offsets[0] +
  199. plane->state->crtc_y * plane->state->fb->pitches[0] +
  200. plane->state->crtc_x * bpp / 8;
  201. hdlcd = plane->dev->dev_private;
  202. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, plane->state->fb->pitches[0]);
  203. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, plane->state->fb->pitches[0]);
  204. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1);
  205. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  206. }
  207. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  208. .atomic_check = hdlcd_plane_atomic_check,
  209. .atomic_update = hdlcd_plane_atomic_update,
  210. };
  211. static void hdlcd_plane_destroy(struct drm_plane *plane)
  212. {
  213. drm_plane_helper_disable(plane);
  214. drm_plane_cleanup(plane);
  215. }
  216. static const struct drm_plane_funcs hdlcd_plane_funcs = {
  217. .update_plane = drm_atomic_helper_update_plane,
  218. .disable_plane = drm_atomic_helper_disable_plane,
  219. .destroy = hdlcd_plane_destroy,
  220. .reset = drm_atomic_helper_plane_reset,
  221. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  222. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  223. };
  224. static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
  225. {
  226. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  227. struct drm_plane *plane = NULL;
  228. u32 formats[ARRAY_SIZE(supported_formats)], i;
  229. int ret;
  230. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  231. if (!plane)
  232. return ERR_PTR(-ENOMEM);
  233. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  234. formats[i] = supported_formats[i].fourcc;
  235. ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs,
  236. formats, ARRAY_SIZE(formats),
  237. DRM_PLANE_TYPE_PRIMARY, NULL);
  238. if (ret) {
  239. devm_kfree(drm->dev, plane);
  240. return ERR_PTR(ret);
  241. }
  242. drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
  243. hdlcd->plane = plane;
  244. return plane;
  245. }
  246. int hdlcd_setup_crtc(struct drm_device *drm)
  247. {
  248. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  249. struct drm_plane *primary;
  250. int ret;
  251. primary = hdlcd_plane_init(drm);
  252. if (IS_ERR(primary))
  253. return PTR_ERR(primary);
  254. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  255. &hdlcd_crtc_funcs, NULL);
  256. if (ret) {
  257. hdlcd_plane_destroy(primary);
  258. devm_kfree(drm->dev, primary);
  259. return ret;
  260. }
  261. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  262. return 0;
  263. }