hdlcd_crtc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 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_enable(struct drm_crtc *crtc)
  140. {
  141. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  142. clk_prepare_enable(hdlcd->clk);
  143. hdlcd_crtc_mode_set_nofb(crtc);
  144. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
  145. drm_crtc_vblank_on(crtc);
  146. }
  147. static void hdlcd_crtc_disable(struct drm_crtc *crtc)
  148. {
  149. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  150. drm_crtc_vblank_off(crtc);
  151. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  152. clk_disable_unprepare(hdlcd->clk);
  153. }
  154. static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
  155. struct drm_crtc_state *state)
  156. {
  157. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  158. struct drm_display_mode *mode = &state->adjusted_mode;
  159. long rate, clk_rate = mode->clock * 1000;
  160. rate = clk_round_rate(hdlcd->clk, clk_rate);
  161. if (rate != clk_rate) {
  162. /* clock required by mode not supported by hardware */
  163. return -EINVAL;
  164. }
  165. return 0;
  166. }
  167. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  168. struct drm_crtc_state *state)
  169. {
  170. struct drm_pending_vblank_event *event = crtc->state->event;
  171. if (event) {
  172. crtc->state->event = NULL;
  173. spin_lock_irq(&crtc->dev->event_lock);
  174. if (drm_crtc_vblank_get(crtc) == 0)
  175. drm_crtc_arm_vblank_event(crtc, event);
  176. else
  177. drm_crtc_send_vblank_event(crtc, event);
  178. spin_unlock_irq(&crtc->dev->event_lock);
  179. }
  180. }
  181. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  182. .enable = hdlcd_crtc_enable,
  183. .disable = hdlcd_crtc_disable,
  184. .atomic_check = hdlcd_crtc_atomic_check,
  185. .atomic_begin = hdlcd_crtc_atomic_begin,
  186. };
  187. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  188. struct drm_plane_state *state)
  189. {
  190. u32 src_w, src_h;
  191. src_w = state->src_w >> 16;
  192. src_h = state->src_h >> 16;
  193. /* we can't do any scaling of the plane source */
  194. if ((src_w != state->crtc_w) || (src_h != state->crtc_h))
  195. return -EINVAL;
  196. return 0;
  197. }
  198. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  199. struct drm_plane_state *state)
  200. {
  201. struct drm_framebuffer *fb = plane->state->fb;
  202. struct hdlcd_drm_private *hdlcd;
  203. struct drm_gem_cma_object *gem;
  204. u32 src_w, src_h, dest_w, dest_h;
  205. dma_addr_t scanout_start;
  206. if (!fb)
  207. return;
  208. src_w = plane->state->src_w >> 16;
  209. src_h = plane->state->src_h >> 16;
  210. dest_w = plane->state->crtc_w;
  211. dest_h = plane->state->crtc_h;
  212. gem = drm_fb_cma_get_gem_obj(fb, 0);
  213. scanout_start = gem->paddr + fb->offsets[0] +
  214. plane->state->crtc_y * fb->pitches[0] +
  215. plane->state->crtc_x *
  216. fb->format->cpp[0];
  217. hdlcd = plane->dev->dev_private;
  218. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, fb->pitches[0]);
  219. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, fb->pitches[0]);
  220. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1);
  221. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  222. }
  223. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  224. .atomic_check = hdlcd_plane_atomic_check,
  225. .atomic_update = hdlcd_plane_atomic_update,
  226. };
  227. static void hdlcd_plane_destroy(struct drm_plane *plane)
  228. {
  229. drm_plane_helper_disable(plane);
  230. drm_plane_cleanup(plane);
  231. }
  232. static const struct drm_plane_funcs hdlcd_plane_funcs = {
  233. .update_plane = drm_atomic_helper_update_plane,
  234. .disable_plane = drm_atomic_helper_disable_plane,
  235. .destroy = hdlcd_plane_destroy,
  236. .reset = drm_atomic_helper_plane_reset,
  237. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  238. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  239. };
  240. static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
  241. {
  242. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  243. struct drm_plane *plane = NULL;
  244. u32 formats[ARRAY_SIZE(supported_formats)], i;
  245. int ret;
  246. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  247. if (!plane)
  248. return ERR_PTR(-ENOMEM);
  249. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  250. formats[i] = supported_formats[i].fourcc;
  251. ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs,
  252. formats, ARRAY_SIZE(formats),
  253. DRM_PLANE_TYPE_PRIMARY, NULL);
  254. if (ret) {
  255. devm_kfree(drm->dev, plane);
  256. return ERR_PTR(ret);
  257. }
  258. drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
  259. hdlcd->plane = plane;
  260. return plane;
  261. }
  262. int hdlcd_setup_crtc(struct drm_device *drm)
  263. {
  264. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  265. struct drm_plane *primary;
  266. int ret;
  267. primary = hdlcd_plane_init(drm);
  268. if (IS_ERR(primary))
  269. return PTR_ERR(primary);
  270. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  271. &hdlcd_crtc_funcs, NULL);
  272. if (ret) {
  273. hdlcd_plane_destroy(primary);
  274. devm_kfree(drm->dev, primary);
  275. return ret;
  276. }
  277. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  278. return 0;
  279. }