hdlcd_crtc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_fb_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_of.h>
  20. #include <drm/drm_plane_helper.h>
  21. #include <linux/clk.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/platform_data/simplefb.h>
  24. #include <video/videomode.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_enable(struct drm_crtc *crtc)
  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_disable(struct drm_crtc *crtc)
  149. {
  150. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  151. drm_crtc_vblank_off(crtc);
  152. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  153. clk_disable_unprepare(hdlcd->clk);
  154. }
  155. static int hdlcd_crtc_atomic_check(struct drm_crtc *crtc,
  156. struct drm_crtc_state *state)
  157. {
  158. struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
  159. struct drm_display_mode *mode = &state->adjusted_mode;
  160. long rate, clk_rate = mode->clock * 1000;
  161. rate = clk_round_rate(hdlcd->clk, clk_rate);
  162. if (rate != clk_rate) {
  163. /* clock required by mode not supported by hardware */
  164. return -EINVAL;
  165. }
  166. return 0;
  167. }
  168. static void hdlcd_crtc_atomic_begin(struct drm_crtc *crtc,
  169. struct drm_crtc_state *state)
  170. {
  171. struct drm_pending_vblank_event *event = crtc->state->event;
  172. if (event) {
  173. crtc->state->event = NULL;
  174. spin_lock_irq(&crtc->dev->event_lock);
  175. if (drm_crtc_vblank_get(crtc) == 0)
  176. drm_crtc_arm_vblank_event(crtc, event);
  177. else
  178. drm_crtc_send_vblank_event(crtc, event);
  179. spin_unlock_irq(&crtc->dev->event_lock);
  180. }
  181. }
  182. static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
  183. .enable = hdlcd_crtc_enable,
  184. .disable = hdlcd_crtc_disable,
  185. .atomic_check = hdlcd_crtc_atomic_check,
  186. .atomic_begin = hdlcd_crtc_atomic_begin,
  187. };
  188. static int hdlcd_plane_atomic_check(struct drm_plane *plane,
  189. struct drm_plane_state *state)
  190. {
  191. struct drm_rect clip = { 0 };
  192. struct drm_crtc_state *crtc_state;
  193. u32 src_h = state->src_h >> 16;
  194. /* only the HDLCD_REG_FB_LINE_COUNT register has a limit */
  195. if (src_h >= HDLCD_MAX_YRES) {
  196. DRM_DEBUG_KMS("Invalid source width: %d\n", src_h);
  197. return -EINVAL;
  198. }
  199. if (!state->fb || !state->crtc)
  200. return 0;
  201. crtc_state = drm_atomic_get_existing_crtc_state(state->state,
  202. state->crtc);
  203. if (!crtc_state) {
  204. DRM_DEBUG_KMS("Invalid crtc state\n");
  205. return -EINVAL;
  206. }
  207. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  208. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  209. return drm_plane_helper_check_state(state, &clip,
  210. DRM_PLANE_HELPER_NO_SCALING,
  211. DRM_PLANE_HELPER_NO_SCALING,
  212. false, true);
  213. }
  214. static void hdlcd_plane_atomic_update(struct drm_plane *plane,
  215. struct drm_plane_state *state)
  216. {
  217. struct drm_framebuffer *fb = plane->state->fb;
  218. struct hdlcd_drm_private *hdlcd;
  219. struct drm_gem_cma_object *gem;
  220. u32 src_x, src_y, dest_h;
  221. dma_addr_t scanout_start;
  222. if (!fb)
  223. return;
  224. src_x = plane->state->src.x1 >> 16;
  225. src_y = plane->state->src.y1 >> 16;
  226. dest_h = drm_rect_height(&plane->state->dst);
  227. gem = drm_fb_cma_get_gem_obj(fb, 0);
  228. scanout_start = gem->paddr + fb->offsets[0] +
  229. src_y * fb->pitches[0] +
  230. src_x * fb->format->cpp[0];
  231. hdlcd = plane->dev->dev_private;
  232. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, fb->pitches[0]);
  233. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, fb->pitches[0]);
  234. hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, dest_h - 1);
  235. hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);
  236. }
  237. static const struct drm_plane_helper_funcs hdlcd_plane_helper_funcs = {
  238. .atomic_check = hdlcd_plane_atomic_check,
  239. .atomic_update = hdlcd_plane_atomic_update,
  240. };
  241. static void hdlcd_plane_destroy(struct drm_plane *plane)
  242. {
  243. drm_plane_helper_disable(plane);
  244. drm_plane_cleanup(plane);
  245. }
  246. static const struct drm_plane_funcs hdlcd_plane_funcs = {
  247. .update_plane = drm_atomic_helper_update_plane,
  248. .disable_plane = drm_atomic_helper_disable_plane,
  249. .destroy = hdlcd_plane_destroy,
  250. .reset = drm_atomic_helper_plane_reset,
  251. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  252. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  253. };
  254. static struct drm_plane *hdlcd_plane_init(struct drm_device *drm)
  255. {
  256. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  257. struct drm_plane *plane = NULL;
  258. u32 formats[ARRAY_SIZE(supported_formats)], i;
  259. int ret;
  260. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  261. if (!plane)
  262. return ERR_PTR(-ENOMEM);
  263. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  264. formats[i] = supported_formats[i].fourcc;
  265. ret = drm_universal_plane_init(drm, plane, 0xff, &hdlcd_plane_funcs,
  266. formats, ARRAY_SIZE(formats),
  267. DRM_PLANE_TYPE_PRIMARY, NULL);
  268. if (ret) {
  269. return ERR_PTR(ret);
  270. }
  271. drm_plane_helper_add(plane, &hdlcd_plane_helper_funcs);
  272. hdlcd->plane = plane;
  273. return plane;
  274. }
  275. int hdlcd_setup_crtc(struct drm_device *drm)
  276. {
  277. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  278. struct drm_plane *primary;
  279. int ret;
  280. primary = hdlcd_plane_init(drm);
  281. if (IS_ERR(primary))
  282. return PTR_ERR(primary);
  283. ret = drm_crtc_init_with_planes(drm, &hdlcd->crtc, primary, NULL,
  284. &hdlcd_crtc_funcs, NULL);
  285. if (ret) {
  286. hdlcd_plane_destroy(primary);
  287. return ret;
  288. }
  289. drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);
  290. return 0;
  291. }