arcpgu_crtc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * ARC PGU DRM driver.
  3. *
  4. * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_plane_helper.h>
  21. #include <linux/clk.h>
  22. #include <linux/platform_data/simplefb.h>
  23. #include "arcpgu.h"
  24. #include "arcpgu_regs.h"
  25. #define ENCODE_PGU_XY(x, y) ((((x) - 1) << 16) | ((y) - 1))
  26. static struct simplefb_format supported_formats[] = {
  27. { "r5g6b5", 16, {11, 5}, {5, 6}, {0, 5}, {0, 0}, DRM_FORMAT_RGB565 },
  28. { "r8g8b8", 24, {16, 8}, {8, 8}, {0, 8}, {0, 0}, DRM_FORMAT_RGB888 },
  29. };
  30. static void arc_pgu_set_pxl_fmt(struct drm_crtc *crtc)
  31. {
  32. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  33. uint32_t pixel_format = crtc->primary->state->fb->pixel_format;
  34. struct simplefb_format *format = NULL;
  35. int i;
  36. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  37. if (supported_formats[i].fourcc == pixel_format)
  38. format = &supported_formats[i];
  39. }
  40. if (WARN_ON(!format))
  41. return;
  42. if (format->fourcc == DRM_FORMAT_RGB888)
  43. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  44. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
  45. ARCPGU_MODE_RGB888_MASK);
  46. }
  47. static const struct drm_crtc_funcs arc_pgu_crtc_funcs = {
  48. .destroy = drm_crtc_cleanup,
  49. .set_config = drm_atomic_helper_set_config,
  50. .page_flip = drm_atomic_helper_page_flip,
  51. .reset = drm_atomic_helper_crtc_reset,
  52. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  53. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  54. };
  55. static void arc_pgu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  56. {
  57. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  58. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  59. u32 val;
  60. arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
  61. ENCODE_PGU_XY(m->crtc_htotal, m->crtc_vtotal));
  62. arc_pgu_write(arcpgu, ARCPGU_REG_HSYNC,
  63. ENCODE_PGU_XY(m->crtc_hsync_start - m->crtc_hdisplay,
  64. m->crtc_hsync_end - m->crtc_hdisplay));
  65. arc_pgu_write(arcpgu, ARCPGU_REG_VSYNC,
  66. ENCODE_PGU_XY(m->crtc_vsync_start - m->crtc_vdisplay,
  67. m->crtc_vsync_end - m->crtc_vdisplay));
  68. arc_pgu_write(arcpgu, ARCPGU_REG_ACTIVE,
  69. ENCODE_PGU_XY(m->crtc_hblank_end - m->crtc_hblank_start,
  70. m->crtc_vblank_end - m->crtc_vblank_start));
  71. val = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
  72. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  73. val |= ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST;
  74. else
  75. val &= ~(ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST);
  76. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  77. val |= ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST;
  78. else
  79. val &= ~(ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST);
  80. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, val);
  81. arc_pgu_write(arcpgu, ARCPGU_REG_STRIDE, 0);
  82. arc_pgu_write(arcpgu, ARCPGU_REG_START_SET, 1);
  83. arc_pgu_set_pxl_fmt(crtc);
  84. clk_set_rate(arcpgu->clk, m->crtc_clock * 1000);
  85. }
  86. static void arc_pgu_crtc_enable(struct drm_crtc *crtc)
  87. {
  88. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  89. clk_prepare_enable(arcpgu->clk);
  90. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  91. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
  92. ARCPGU_CTRL_ENABLE_MASK);
  93. }
  94. static void arc_pgu_crtc_disable(struct drm_crtc *crtc)
  95. {
  96. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  97. if (!crtc->primary->fb)
  98. return;
  99. clk_disable_unprepare(arcpgu->clk);
  100. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  101. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) &
  102. ~ARCPGU_CTRL_ENABLE_MASK);
  103. }
  104. static int arc_pgu_crtc_atomic_check(struct drm_crtc *crtc,
  105. struct drm_crtc_state *state)
  106. {
  107. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  108. struct drm_display_mode *mode = &state->adjusted_mode;
  109. long rate, clk_rate = mode->clock * 1000;
  110. rate = clk_round_rate(arcpgu->clk, clk_rate);
  111. if (rate != clk_rate)
  112. return -EINVAL;
  113. return 0;
  114. }
  115. static void arc_pgu_crtc_atomic_begin(struct drm_crtc *crtc,
  116. struct drm_crtc_state *state)
  117. {
  118. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  119. unsigned long flags;
  120. if (crtc->state->event) {
  121. struct drm_pending_vblank_event *event = crtc->state->event;
  122. crtc->state->event = NULL;
  123. event->pipe = drm_crtc_index(crtc);
  124. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  125. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  126. list_add_tail(&event->base.link, &arcpgu->event_list);
  127. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  128. }
  129. }
  130. static const struct drm_crtc_helper_funcs arc_pgu_crtc_helper_funcs = {
  131. .mode_set = drm_helper_crtc_mode_set,
  132. .mode_set_base = drm_helper_crtc_mode_set_base,
  133. .mode_set_nofb = arc_pgu_crtc_mode_set_nofb,
  134. .enable = arc_pgu_crtc_enable,
  135. .disable = arc_pgu_crtc_disable,
  136. .prepare = arc_pgu_crtc_disable,
  137. .commit = arc_pgu_crtc_enable,
  138. .atomic_check = arc_pgu_crtc_atomic_check,
  139. .atomic_begin = arc_pgu_crtc_atomic_begin,
  140. };
  141. static void arc_pgu_plane_atomic_update(struct drm_plane *plane,
  142. struct drm_plane_state *state)
  143. {
  144. struct arcpgu_drm_private *arcpgu;
  145. struct drm_gem_cma_object *gem;
  146. if (!plane->state->crtc || !plane->state->fb)
  147. return;
  148. arcpgu = crtc_to_arcpgu_priv(plane->state->crtc);
  149. gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
  150. arc_pgu_write(arcpgu, ARCPGU_REG_BUF0_ADDR, gem->paddr);
  151. }
  152. static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = {
  153. .prepare_fb = NULL,
  154. .cleanup_fb = NULL,
  155. .atomic_update = arc_pgu_plane_atomic_update,
  156. };
  157. static void arc_pgu_plane_destroy(struct drm_plane *plane)
  158. {
  159. drm_plane_helper_disable(plane);
  160. drm_plane_cleanup(plane);
  161. }
  162. static const struct drm_plane_funcs arc_pgu_plane_funcs = {
  163. .update_plane = drm_atomic_helper_update_plane,
  164. .disable_plane = drm_atomic_helper_disable_plane,
  165. .destroy = arc_pgu_plane_destroy,
  166. .reset = drm_atomic_helper_plane_reset,
  167. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  168. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  169. };
  170. static struct drm_plane *arc_pgu_plane_init(struct drm_device *drm)
  171. {
  172. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  173. struct drm_plane *plane = NULL;
  174. u32 formats[ARRAY_SIZE(supported_formats)], i;
  175. int ret;
  176. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  177. if (!plane)
  178. return ERR_PTR(-ENOMEM);
  179. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  180. formats[i] = supported_formats[i].fourcc;
  181. ret = drm_universal_plane_init(drm, plane, 0xff, &arc_pgu_plane_funcs,
  182. formats, ARRAY_SIZE(formats),
  183. DRM_PLANE_TYPE_PRIMARY, NULL);
  184. if (ret)
  185. return ERR_PTR(ret);
  186. drm_plane_helper_add(plane, &arc_pgu_plane_helper_funcs);
  187. arcpgu->plane = plane;
  188. return plane;
  189. }
  190. int arc_pgu_setup_crtc(struct drm_device *drm)
  191. {
  192. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  193. struct drm_plane *primary;
  194. int ret;
  195. primary = arc_pgu_plane_init(drm);
  196. if (IS_ERR(primary))
  197. return PTR_ERR(primary);
  198. ret = drm_crtc_init_with_planes(drm, &arcpgu->crtc, primary, NULL,
  199. &arc_pgu_crtc_funcs, NULL);
  200. if (ret) {
  201. arc_pgu_plane_destroy(primary);
  202. return ret;
  203. }
  204. drm_crtc_helper_add(&arcpgu->crtc, &arc_pgu_crtc_helper_funcs);
  205. return 0;
  206. }