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. const struct drm_framebuffer *fb = crtc->primary->state->fb;
  56. uint32_t pixel_format;
  57. struct simplefb_format *format = NULL;
  58. int i;
  59. pixel_format = fb->format->format;
  60. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  61. if (supported_formats[i].fourcc == pixel_format)
  62. format = &supported_formats[i];
  63. }
  64. if (WARN_ON(!format))
  65. return 0;
  66. /* HDLCD uses 'bytes per pixel', zero means 1 byte */
  67. btpp = (format->bits_per_pixel + 7) / 8;
  68. hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3);
  69. /*
  70. * The format of the HDLCD_REG_<color>_SELECT register is:
  71. * - bits[23:16] - default value for that color component
  72. * - bits[11:8] - number of bits to extract for each color component
  73. * - bits[4:0] - index of the lowest bit to extract
  74. *
  75. * The default color value is used when bits[11:8] are zero, when the
  76. * pixel is outside the visible frame area or when there is a
  77. * buffer underrun.
  78. */
  79. hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset |
  80. #ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN
  81. 0x00ff0000 | /* show underruns in red */
  82. #endif
  83. ((format->red.length & 0xf) << 8));
  84. hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset |
  85. ((format->green.length & 0xf) << 8));
  86. hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset |
  87. ((format->blue.length & 0xf) << 8));
  88. return 0;
  89. }
  90. static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc)
  91. {
  92. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  93. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  94. struct videomode vm;
  95. unsigned int polarities, err;
  96. vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay;
  97. vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end;
  98. vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start;
  99. vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay;
  100. vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end;
  101. vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start;
  102. polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;
  103. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  104. polarities |= HDLCD_POLARITY_HSYNC;
  105. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  106. polarities |= HDLCD_POLARITY_VSYNC;
  107. /* Allow max number of outstanding requests and largest burst size */
  108. hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
  109. HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);
  110. hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1);
  111. hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1);
  112. hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1);
  113. hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1);
  114. hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1);
  115. hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1);
  116. hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1);
  117. hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1);
  118. hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);
  119. err = hdlcd_set_pxl_fmt(crtc);
  120. if (err)
  121. return;
  122. clk_set_rate(hdlcd->clk, m->crtc_clock * 1000);
  123. }
  124. static void hdlcd_crtc_enable(struct drm_crtc *crtc)
  125. {
  126. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  127. clk_prepare_enable(hdlcd->clk);
  128. hdlcd_crtc_mode_set_nofb(crtc);
  129. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
  130. drm_crtc_vblank_on(crtc);
  131. }
  132. static void hdlcd_crtc_disable(struct drm_crtc *crtc)
  133. {
  134. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  135. drm_crtc_vblank_off(crtc);
  136. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  137. clk_disable_unprepare(hdlcd->clk);
  138. }
  139. static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
  140. struct drm_crtc_state *state)
  141. {
  142. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  143. struct drm_display_mode *mode = &state->adjusted_mode;
  144. long rate, clk_rate = mode->clock * 1000;
  145. rate = clk_round_rate(hdlcd->clk, clk_rate);
  146. if (rate != clk_rate) {
  147. /* clock required by mode not supported by hardware */
  148. return -EINVAL;
  149. }
  150. return 0;
  151. }
  152. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  153. struct drm_crtc_state *state)
  154. {
  155. struct drm_pending_vblank_event *event = crtc->state->event;
  156. if (event) {
  157. crtc->state->event = NULL;
  158. spin_lock_irq(&crtc->dev->event_lock);
  159. if (drm_crtc_vblank_get(crtc) == 0)
  160. drm_crtc_arm_vblank_event(crtc, event);
  161. else
  162. drm_crtc_send_vblank_event(crtc, event);
  163. spin_unlock_irq(&crtc->dev->event_lock);
  164. }
  165. }
  166. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  167. .enable = hdlcd_crtc_enable,
  168. .disable = hdlcd_crtc_disable,
  169. .atomic_check = hdlcd_crtc_atomic_check,
  170. .atomic_begin = hdlcd_crtc_atomic_begin,
  171. };
  172. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  173. struct drm_plane_state *state)
  174. {
  175. u32 src_w, src_h;
  176. src_w = state->src_w >> 16;
  177. src_h = state->src_h >> 16;
  178. /* we can't do any scaling of the plane source */
  179. if ((src_w != state->crtc_w) || (src_h != state->crtc_h))
  180. return -EINVAL;
  181. return 0;
  182. }
  183. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  184. struct drm_plane_state *state)
  185. {
  186. struct drm_framebuffer *fb = plane->state->fb;
  187. struct hdlcd_drm_private *hdlcd;
  188. struct drm_gem_cma_object *gem;
  189. u32 src_w, src_h, dest_w, dest_h;
  190. dma_addr_t scanout_start;
  191. if (!fb)
  192. return;
  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(fb, 0);
  198. scanout_start = gem->paddr + fb->offsets[0] +
  199. plane->state->crtc_y * fb->pitches[0] +
  200. plane->state->crtc_x *
  201. fb->format->cpp[0];
  202. hdlcd = plane->dev->dev_private;
  203. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, fb->pitches[0]);
  204. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, fb->pitches[0]);
  205. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1);
  206. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  207. }
  208. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  209. .atomic_check = hdlcd_plane_atomic_check,
  210. .atomic_update = hdlcd_plane_atomic_update,
  211. };
  212. static void hdlcd_plane_destroy(struct drm_plane *plane)
  213. {
  214. drm_plane_helper_disable(plane);
  215. drm_plane_cleanup(plane);
  216. }
  217. static const struct drm_plane_funcs hdlcd_plane_funcs = {
  218. .update_plane = drm_atomic_helper_update_plane,
  219. .disable_plane = drm_atomic_helper_disable_plane,
  220. .destroy = hdlcd_plane_destroy,
  221. .reset = drm_atomic_helper_plane_reset,
  222. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  223. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  224. };
  225. static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
  226. {
  227. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  228. struct drm_plane *plane = NULL;
  229. u32 formats[ARRAY_SIZE(supported_formats)], i;
  230. int ret;
  231. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  232. if (!plane)
  233. return ERR_PTR(-ENOMEM);
  234. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  235. formats[i] = supported_formats[i].fourcc;
  236. ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs,
  237. formats, ARRAY_SIZE(formats),
  238. DRM_PLANE_TYPE_PRIMARY, NULL);
  239. if (ret) {
  240. devm_kfree(drm->dev, plane);
  241. return ERR_PTR(ret);
  242. }
  243. drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
  244. hdlcd->plane = plane;
  245. return plane;
  246. }
  247. int hdlcd_setup_crtc(struct drm_device *drm)
  248. {
  249. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  250. struct drm_plane *primary;
  251. int ret;
  252. primary = hdlcd_plane_init(drm);
  253. if (IS_ERR(primary))
  254. return PTR_ERR(primary);
  255. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  256. &hdlcd_crtc_funcs, NULL);
  257. if (ret) {
  258. hdlcd_plane_destroy(primary);
  259. devm_kfree(drm->dev, primary);
  260. return ret;
  261. }
  262. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  263. return 0;
  264. }