hdlcd_crtc.c 10 KB

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