arcpgu_crtc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. const struct drm_framebuffer *fb = crtc->primary->state->fb;
  34. uint32_t pixel_format = fb->format->format;
  35. struct simplefb_format *format = NULL;
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
  38. if (supported_formats[i].fourcc == pixel_format)
  39. format = &supported_formats[i];
  40. }
  41. if (WARN_ON(!format))
  42. return;
  43. if (format->fourcc == DRM_FORMAT_RGB888)
  44. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  45. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
  46. ARCPGU_MODE_RGB888_MASK);
  47. }
  48. static const struct drm_crtc_funcs arc_pgu_crtc_funcs = {
  49. .destroy = drm_crtc_cleanup,
  50. .set_config = drm_atomic_helper_set_config,
  51. .page_flip = drm_atomic_helper_page_flip,
  52. .reset = drm_atomic_helper_crtc_reset,
  53. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  54. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  55. };
  56. static enum drm_mode_status arc_pgu_crtc_mode_valid(struct drm_crtc *crtc,
  57. const struct drm_display_mode *mode)
  58. {
  59. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  60. long rate, clk_rate = mode->clock * 1000;
  61. rate = clk_round_rate(arcpgu->clk, clk_rate);
  62. if (rate != clk_rate)
  63. return MODE_NOCLOCK;
  64. return MODE_OK;
  65. }
  66. static void arc_pgu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  67. {
  68. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  69. struct drm_display_mode *m = &crtc->state->adjusted_mode;
  70. u32 val;
  71. arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
  72. ENCODE_PGU_XY(m->crtc_htotal, m->crtc_vtotal));
  73. arc_pgu_write(arcpgu, ARCPGU_REG_HSYNC,
  74. ENCODE_PGU_XY(m->crtc_hsync_start - m->crtc_hdisplay,
  75. m->crtc_hsync_end - m->crtc_hdisplay));
  76. arc_pgu_write(arcpgu, ARCPGU_REG_VSYNC,
  77. ENCODE_PGU_XY(m->crtc_vsync_start - m->crtc_vdisplay,
  78. m->crtc_vsync_end - m->crtc_vdisplay));
  79. arc_pgu_write(arcpgu, ARCPGU_REG_ACTIVE,
  80. ENCODE_PGU_XY(m->crtc_hblank_end - m->crtc_hblank_start,
  81. m->crtc_vblank_end - m->crtc_vblank_start));
  82. val = arc_pgu_read(arcpgu, ARCPGU_REG_CTRL);
  83. if (m->flags & DRM_MODE_FLAG_PVSYNC)
  84. val |= ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST;
  85. else
  86. val &= ~(ARCPGU_CTRL_VS_POL_MASK << ARCPGU_CTRL_VS_POL_OFST);
  87. if (m->flags & DRM_MODE_FLAG_PHSYNC)
  88. val |= ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST;
  89. else
  90. val &= ~(ARCPGU_CTRL_HS_POL_MASK << ARCPGU_CTRL_HS_POL_OFST);
  91. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL, val);
  92. arc_pgu_write(arcpgu, ARCPGU_REG_STRIDE, 0);
  93. arc_pgu_write(arcpgu, ARCPGU_REG_START_SET, 1);
  94. arc_pgu_set_pxl_fmt(crtc);
  95. clk_set_rate(arcpgu->clk, m->crtc_clock * 1000);
  96. }
  97. static void arc_pgu_crtc_atomic_enable(struct drm_crtc *crtc,
  98. struct drm_crtc_state *old_state)
  99. {
  100. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  101. clk_prepare_enable(arcpgu->clk);
  102. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  103. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) |
  104. ARCPGU_CTRL_ENABLE_MASK);
  105. }
  106. static void arc_pgu_crtc_atomic_disable(struct drm_crtc *crtc,
  107. struct drm_crtc_state *old_state)
  108. {
  109. struct arcpgu_drm_private *arcpgu = crtc_to_arcpgu_priv(crtc);
  110. if (!crtc->primary->fb)
  111. return;
  112. clk_disable_unprepare(arcpgu->clk);
  113. arc_pgu_write(arcpgu, ARCPGU_REG_CTRL,
  114. arc_pgu_read(arcpgu, ARCPGU_REG_CTRL) &
  115. ~ARCPGU_CTRL_ENABLE_MASK);
  116. }
  117. static void arc_pgu_crtc_atomic_begin(struct drm_crtc *crtc,
  118. struct drm_crtc_state *state)
  119. {
  120. struct drm_pending_vblank_event *event = crtc->state->event;
  121. if (event) {
  122. crtc->state->event = NULL;
  123. spin_lock_irq(&crtc->dev->event_lock);
  124. drm_crtc_send_vblank_event(crtc, event);
  125. spin_unlock_irq(&crtc->dev->event_lock);
  126. }
  127. }
  128. static const struct drm_crtc_helper_funcs arc_pgu_crtc_helper_funcs = {
  129. .mode_valid = arc_pgu_crtc_mode_valid,
  130. .mode_set = drm_helper_crtc_mode_set,
  131. .mode_set_base = drm_helper_crtc_mode_set_base,
  132. .mode_set_nofb = arc_pgu_crtc_mode_set_nofb,
  133. .atomic_begin = arc_pgu_crtc_atomic_begin,
  134. .atomic_enable = arc_pgu_crtc_atomic_enable,
  135. .atomic_disable = arc_pgu_crtc_atomic_disable,
  136. };
  137. static void arc_pgu_plane_atomic_update(struct drm_plane *plane,
  138. struct drm_plane_state *state)
  139. {
  140. struct arcpgu_drm_private *arcpgu;
  141. struct drm_gem_cma_object *gem;
  142. if (!plane->state->crtc || !plane->state->fb)
  143. return;
  144. arcpgu = crtc_to_arcpgu_priv(plane->state->crtc);
  145. gem = drm_fb_cma_get_gem_obj(plane->state->fb, 0);
  146. arc_pgu_write(arcpgu, ARCPGU_REG_BUF0_ADDR, gem->paddr);
  147. }
  148. static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = {
  149. .atomic_update = arc_pgu_plane_atomic_update,
  150. };
  151. static void arc_pgu_plane_destroy(struct drm_plane *plane)
  152. {
  153. drm_plane_helper_disable(plane);
  154. drm_plane_cleanup(plane);
  155. }
  156. static const struct drm_plane_funcs arc_pgu_plane_funcs = {
  157. .update_plane = drm_atomic_helper_update_plane,
  158. .disable_plane = drm_atomic_helper_disable_plane,
  159. .destroy = arc_pgu_plane_destroy,
  160. .reset = drm_atomic_helper_plane_reset,
  161. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  162. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  163. };
  164. static struct drm_plane *arc_pgu_plane_init(struct drm_device *drm)
  165. {
  166. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  167. struct drm_plane *plane = NULL;
  168. u32 formats[ARRAY_SIZE(supported_formats)], i;
  169. int ret;
  170. plane = devm_kzalloc(drm->dev, sizeof(*plane), GFP_KERNEL);
  171. if (!plane)
  172. return ERR_PTR(-ENOMEM);
  173. for (i = 0; i < ARRAY_SIZE(supported_formats); i++)
  174. formats[i] = supported_formats[i].fourcc;
  175. ret = drm_universal_plane_init(drm, plane, 0xff, &arc_pgu_plane_funcs,
  176. formats, ARRAY_SIZE(formats),
  177. NULL,
  178. DRM_PLANE_TYPE_PRIMARY, NULL);
  179. if (ret)
  180. return ERR_PTR(ret);
  181. drm_plane_helper_add(plane, &arc_pgu_plane_helper_funcs);
  182. arcpgu->plane = plane;
  183. return plane;
  184. }
  185. int arc_pgu_setup_crtc(struct drm_device *drm)
  186. {
  187. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  188. struct drm_plane *primary;
  189. int ret;
  190. primary = arc_pgu_plane_init(drm);
  191. if (IS_ERR(primary))
  192. return PTR_ERR(primary);
  193. ret = drm_crtc_init_with_planes(drm, &arcpgu->crtc, primary, NULL,
  194. &arc_pgu_crtc_funcs, NULL);
  195. if (ret) {
  196. arc_pgu_plane_destroy(primary);
  197. return ret;
  198. }
  199. drm_crtc_helper_add(&arcpgu->crtc, &arc_pgu_crtc_helper_funcs);
  200. return 0;
  201. }