hdlcd_crtc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <linux/clk.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/platform_data/simplefb.h>
  14. #include <video/videomode.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc.h>
  18. #include <drm/drm_fb_dma_helper.h>
  19. #include <drm/drm_framebuffer.h>
  20. #include <drm/drm_gem_dma_helper.h>
  21. #include <drm/drm_of.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_vblank.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 int hdlcd_crtc_enable_vblank(struct drm_crtc *crtc)
  40. {
  41. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  42. unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  43. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask | HDLCD_INTERRUPT_VSYNC);
  44. return 0;
  45. }
  46. static void hdlcd_crtc_disable_vblank(struct drm_crtc *crtc)
  47. {
  48. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  49. unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  50. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask & ~HDLCD_INTERRUPT_VSYNC);
  51. }
  52. static const struct drm_crtc_funcs hdlcd_crtc_funcs = {
  53. .destroy = hdlcd_crtc_cleanup,
  54. .set_config = drm_atomic_helper_set_config,
  55. .page_flip = drm_atomic_helper_page_flip,
  56. .reset = drm_atomic_helper_crtc_reset,
  57. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  58. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  59. .enable_vblank = hdlcd_crtc_enable_vblank,
  60. .disable_vblank = hdlcd_crtc_disable_vblank,
  61. };
  62. static struct simplefb_format supported_formats[] = SIMPLEFB_FORMATS;
  63. /*
  64. * Setup the HDLCD registers for decoding the pixels out of the framebuffer
  65. */
  66. static int hdlcd_set_pxl_fmt(struct drm_crtc *crtc)
  67. {
  68. unsigned int btpp;
  69. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  70. const struct drm_framebuffer *fb = crtc->primary->state->fb;
  71. uint32_t pixel_format;
  72. struct simplefb_format *format = NULL;
  73. int i;
  74. pixel_format = fb->format->format;
  75. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  76. if (supported_formats[i].fourcc == pixel_format)
  77. format = &supported_formats[i];
  78. }
  79. if (WARN_ON(!format))
  80. return 0;
  81. /* HDLCD uses 'bytes per pixel', zero means 1 byte */
  82. btpp = (format->bits_per_pixel + 7) / 8;
  83. hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3);
  84. /*
  85. * The format of the HDLCD_REG_<color>_SELECT register is:
  86. * - bits[23:16] - default value for that color component
  87. * - bits[11:8] - number of bits to extract for each color component
  88. * - bits[4:0] - index of the lowest bit to extract
  89. *
  90. * The default color value is used when bits[11:8] are zero, when the
  91. * pixel is outside the visible frame area or when there is a
  92. * buffer underrun.
  93. */
  94. hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset |
  95. #ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN
  96. 0x00ff0000 | /* show underruns in red */
  97. #endif
  98. ((format->red.length & 0xf) << 8));
  99. hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset |
  100. ((format->green.length & 0xf) << 8));
  101. hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset |
  102. ((format->blue.length & 0xf) << 8));
  103. return 0;
  104. }
  105. static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc)
  106. {
  107. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  108. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  109. struct videomode vm;
  110. unsigned int polarities, err;
  111. vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay;
  112. vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end;
  113. vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start;
  114. vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay;
  115. vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end;
  116. vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start;
  117. polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;
  118. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  119. polarities |= HDLCD_POLARITY_HSYNC;
  120. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  121. polarities |= HDLCD_POLARITY_VSYNC;
  122. /* Allow max number of outstanding requests and largest burst size */
  123. hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
  124. HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);
  125. hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1);
  126. hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1);
  127. hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1);
  128. hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1);
  129. hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1);
  130. hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1);
  131. hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1);
  132. hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1);
  133. hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);
  134. err = hdlcd_set_pxl_fmt(crtc);
  135. if (err)
  136. return;
  137. clk_set_rate(hdlcd->clk, m->crtc_clock * 1000);
  138. }
  139. static void hdlcd_crtc_atomic_enable(struct drm_crtc *crtc,
  140. struct drm_atomic_state *state)
  141. {
  142. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  143. clk_prepare_enable(hdlcd->clk);
  144. hdlcd_crtc_mode_set_nofb(crtc);
  145. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
  146. drm_crtc_vblank_on(crtc);
  147. }
  148. static void hdlcd_crtc_atomic_disable(struct drm_crtc *crtc,
  149. struct drm_atomic_state *state)
  150. {
  151. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  152. drm_crtc_vblank_off(crtc);
  153. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  154. clk_disable_unprepare(hdlcd->clk);
  155. }
  156. static enum drm_mode_status hdlcd_crtc_mode_valid(struct drm_crtc *crtc,
  157. const struct drm_display_mode *mode)
  158. {
  159. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  160. long rate, clk_rate = mode->clock * 1000;
  161. rate = clk_round_rate(hdlcd->clk, clk_rate);
  162. /* 0.1% seems a close enough tolerance for the TDA19988 on Juno */
  163. if (abs(rate - clk_rate) * 1000 > clk_rate) {
  164. /* clock required by mode not supported by hardware */
  165. return MODE_NOCLOCK;
  166. }
  167. return MODE_OK;
  168. }
  169. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  170. struct drm_atomic_state *state)
  171. {
  172. struct drm_pending_vblank_event *event = crtc->state->event;
  173. if (event) {
  174. crtc->state->event = NULL;
  175. spin_lock_irq(&crtc->dev->event_lock);
  176. if (drm_crtc_vblank_get(crtc) == 0)
  177. drm_crtc_arm_vblank_event(crtc, event);
  178. else
  179. drm_crtc_send_vblank_event(crtc, event);
  180. spin_unlock_irq(&crtc->dev->event_lock);
  181. }
  182. }
  183. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  184. .mode_valid = hdlcd_crtc_mode_valid,
  185. .atomic_begin = hdlcd_crtc_atomic_begin,
  186. .atomic_enable = hdlcd_crtc_atomic_enable,
  187. .atomic_disable = hdlcd_crtc_atomic_disable,
  188. };
  189. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  190. struct drm_atomic_state *state)
  191. {
  192. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  193. plane);
  194. int i;
  195. struct drm_crtc *crtc;
  196. struct drm_crtc_state *crtc_state;
  197. u32 src_h = new_plane_state->src_h >> 16;
  198. /* only the HDLCD_REG_FB_LINE_COUNT register has a limit */
  199. if (src_h >= HDLCD_MAX_YRES) {
  200. DRM_DEBUG_KMS("Invalid source width: %d\n", src_h);
  201. return -EINVAL;
  202. }
  203. for_each_new_crtc_in_state(state, crtc, crtc_state,
  204. i) {
  205. /* we cannot disable the plane while the CRTC is active */
  206. if (!new_plane_state->fb && crtc_state->active)
  207. return -EINVAL;
  208. return drm_atomic_helper_check_plane_state(new_plane_state,
  209. crtc_state,
  210. DRM_PLANE_NO_SCALING,
  211. DRM_PLANE_NO_SCALING,
  212. false, true);
  213. }
  214. return 0;
  215. }
  216. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  217. struct drm_atomic_state *state)
  218. {
  219. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  220. plane);
  221. struct drm_framebuffer *fb = new_plane_state->fb;
  222. struct hdlcd_drm_private *hdlcd;
  223. u32 dest_h;
  224. dma_addr_t scanout_start;
  225. if (!fb)
  226. return;
  227. dest_h = drm_rect_height(&new_plane_state->dst);
  228. scanout_start = drm_fb_dma_get_gem_addr(fb, new_plane_state, 0);
  229. hdlcd = drm_to_hdlcd_priv(plane->dev);
  230. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, fb->pitches[0]);
  231. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, fb->pitches[0]);
  232. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1);
  233. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  234. }
  235. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  236. .atomic_check = hdlcd_plane_atomic_check,
  237. .atomic_update = hdlcd_plane_atomic_update,
  238. };
  239. static const struct drm_plane_funcs hdlcd_plane_funcs = {
  240. .update_plane = drm_atomic_helper_update_plane,
  241. .disable_plane = drm_atomic_helper_disable_plane,
  242. .reset = drm_atomic_helper_plane_reset,
  243. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  244. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  245. };
  246. static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
  247. {
  248. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  249. struct drm_plane *plane = NULL;
  250. u32 formats[ARRAY_SIZE(supported_formats)], i;
  251. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  252. formats[i] = supported_formats[i].fourcc;
  253. plane = drmm_universal_plane_alloc(drm, struct drm_plane, dev, 0xff,
  254. &hdlcd_plane_funcs,
  255. formats, ARRAY_SIZE(formats),
  256. NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
  257. if (IS_ERR(plane))
  258. return plane;
  259. drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
  260. hdlcd->plane = plane;
  261. return plane;
  262. }
  263. int hdlcd_setup_crtc(struct drm_device *drm)
  264. {
  265. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  266. struct drm_plane *primary;
  267. int ret;
  268. primary = hdlcd_plane_init(drm);
  269. if (IS_ERR(primary))
  270. return PTR_ERR(primary);
  271. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  272. &hdlcd_crtc_funcs, NULL);
  273. if (ret)
  274. return ret;
  275. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  276. return 0;
  277. }