hdlcd_crtc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 const struct drm_crtc_funcs hdlcd_crtc_funcs = {
  33. .destroy = drm_crtc_cleanup,
  34. .set_config = drm_atomic_helper_set_config,
  35. .page_flip = drm_atomic_helper_page_flip,
  36. .reset = drm_atomic_helper_crtc_reset,
  37. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  38. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  39. };
  40. static struct simplefb_format supported_formats[] = SIMPLEFB_FORMATS;
  41. /*
  42. * Setup the HDLCD registers for decoding the pixels out of the framebuffer
  43. */
  44. static int hdlcd_set_pxl_fmt(struct drm_crtc *crtc)
  45. {
  46. unsigned int btpp;
  47. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  48. uint32_t pixel_format;
  49. struct simplefb_format *format = NULL;
  50. int i;
  51. pixel_format = crtc->primary->state->fb->pixel_format;
  52. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  53. if (supported_formats[i].fourcc == pixel_format)
  54. format = &supported_formats[i];
  55. }
  56. if (WARN_ON(!format))
  57. return 0;
  58. /* HDLCD uses 'bytes per pixel', zero means 1 byte */
  59. btpp = (format->bits_per_pixel + 7) / 8;
  60. hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (btpp - 1) << 3);
  61. /*
  62. * The format of the HDLCD_REG_<color>_SELECT register is:
  63. * - bits[23:16] - default value for that color component
  64. * - bits[11:8] - number of bits to extract for each color component
  65. * - bits[4:0] - index of the lowest bit to extract
  66. *
  67. * The default color value is used when bits[11:8] are zero, when the
  68. * pixel is outside the visible frame area or when there is a
  69. * buffer underrun.
  70. */
  71. hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, format->red.offset |
  72. #ifdef CONFIG_DRM_HDLCD_SHOW_UNDERRUN
  73. 0x00ff0000 | /* show underruns in red */
  74. #endif
  75. ((format->red.length & 0xf) << 8));
  76. hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, format->green.offset |
  77. ((format->green.length & 0xf) << 8));
  78. hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, format->blue.offset |
  79. ((format->blue.length & 0xf) << 8));
  80. return 0;
  81. }
  82. static void hdlcd_crtc_mode_set_nofb(struct drm_crtc *crtc)
  83. {
  84. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  85. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  86. struct videomode vm;
  87. unsigned int polarities, line_length, err;
  88. vm.vfront_porch = m->crtc_vsync_start - m->crtc_vdisplay;
  89. vm.vback_porch = m->crtc_vtotal - m->crtc_vsync_end;
  90. vm.vsync_len = m->crtc_vsync_end - m->crtc_vsync_start;
  91. vm.hfront_porch = m->crtc_hsync_start - m->crtc_hdisplay;
  92. vm.hback_porch = m->crtc_htotal - m->crtc_hsync_end;
  93. vm.hsync_len = m->crtc_hsync_end - m->crtc_hsync_start;
  94. polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;
  95. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  96. polarities |= HDLCD_POLARITY_HSYNC;
  97. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  98. polarities |= HDLCD_POLARITY_VSYNC;
  99. line_length = crtc->primary->state->fb->pitches[0];
  100. /* Allow max number of outstanding requests and largest burst size */
  101. hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
  102. HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);
  103. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, line_length);
  104. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, line_length);
  105. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, m->crtc_vdisplay - 1);
  106. hdlcd_write(hdlcd, HDLCD_REG_V_DATA, m->crtc_vdisplay - 1);
  107. hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH, vm.vback_porch - 1);
  108. hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH, vm.vfront_porch - 1);
  109. hdlcd_write(hdlcd, HDLCD_REG_V_SYNC, vm.vsync_len - 1);
  110. hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH, vm.hback_porch - 1);
  111. hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH, vm.hfront_porch - 1);
  112. hdlcd_write(hdlcd, HDLCD_REG_H_SYNC, vm.hsync_len - 1);
  113. hdlcd_write(hdlcd, HDLCD_REG_H_DATA, m->crtc_hdisplay - 1);
  114. hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);
  115. err = hdlcd_set_pxl_fmt(crtc);
  116. if (err)
  117. return;
  118. clk_set_rate(hdlcd->clk, m->crtc_clock * 1000);
  119. }
  120. static void hdlcd_crtc_enable(struct drm_crtc *crtc)
  121. {
  122. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  123. clk_prepare_enable(hdlcd->clk);
  124. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
  125. drm_crtc_vblank_on(crtc);
  126. }
  127. static void hdlcd_crtc_disable(struct drm_crtc *crtc)
  128. {
  129. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  130. if (!crtc->primary->fb)
  131. return;
  132. clk_disable_unprepare(hdlcd->clk);
  133. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  134. drm_crtc_vblank_off(crtc);
  135. }
  136. static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
  137. struct drm_crtc_state *state)
  138. {
  139. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  140. struct drm_display_mode *mode = &state->adjusted_mode;
  141. long rate, clk_rate = mode->clock * 1000;
  142. rate = clk_round_rate(hdlcd->clk, clk_rate);
  143. if (rate != clk_rate) {
  144. /* clock required by mode not supported by hardware */
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  150. struct drm_crtc_state *state)
  151. {
  152. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  153. unsigned long flags;
  154. if (crtc->state->event) {
  155. struct drm_pending_vblank_event *event = crtc->state->event;
  156. crtc->state->event = NULL;
  157. event->pipe = drm_crtc_index(crtc);
  158. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  159. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  160. list_add_tail(&event->base.link, &hdlcd->event_list);
  161. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  162. }
  163. }
  164. static void hdlcd_crtc_atomic_flush(struct drm_crtc *crtc,
  165. struct drm_crtc_state *state)
  166. {
  167. }
  168. static bool hdlcd_crtc_mode_fixup(struct drm_crtc *crtc,
  169. const struct drm_display_mode *mode,
  170. struct drm_display_mode *adjusted_mode)
  171. {
  172. return true;
  173. }
  174. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  175. .mode_fixup = hdlcd_crtc_mode_fixup,
  176. .mode_set = drm_helper_crtc_mode_set,
  177. .mode_set_base = drm_helper_crtc_mode_set_base,
  178. .mode_set_nofb = hdlcd_crtc_mode_set_nofb,
  179. .enable = hdlcd_crtc_enable,
  180. .disable = hdlcd_crtc_disable,
  181. .prepare = hdlcd_crtc_disable,
  182. .commit = hdlcd_crtc_enable,
  183. .atomic_check = hdlcd_crtc_atomic_check,
  184. .atomic_begin = hdlcd_crtc_atomic_begin,
  185. .atomic_flush = hdlcd_crtc_atomic_flush,
  186. };
  187. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  188. struct drm_plane_state *state)
  189. {
  190. return 0;
  191. }
  192. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  193. struct drm_plane_state *state)
  194. {
  195. struct hdlcd_drm_private *hdlcd;
  196. struct drm_gem_cma_object *gem;
  197. dma_addr_t scanout_start;
  198. if (!plane->state->crtc || !plane->state->fb)
  199. return;
  200. hdlcd = crtc_to_hdlcd_priv(plane->state->crtc);
  201. gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
  202. scanout_start = gem->paddr;
  203. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  204. }
  205. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  206. .prepare_fb = NULL,
  207. .cleanup_fb = NULL,
  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. void hdlcd_crtc_suspend(struct drm_crtc *crtc)
  247. {
  248. hdlcd_crtc_disable(crtc);
  249. }
  250. void hdlcd_crtc_resume(struct drm_crtc *crtc)
  251. {
  252. hdlcd_crtc_enable(crtc);
  253. }
  254. int hdlcd_setup_crtc(struct drm_device *drm)
  255. {
  256. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  257. struct drm_plane *primary;
  258. int ret;
  259. primary = hdlcd_plane_init(drm);
  260. if (IS_ERR(primary))
  261. return PTR_ERR(primary);
  262. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  263. &hdlcd_crtc_funcs, NULL);
  264. if (ret) {
  265. hdlcd_plane_destroy(primary);
  266. devm_kfree(drm->dev, primary);
  267. return ret;
  268. }
  269. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  270. return 0;
  271. }