sun8i_ui_layer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (C) Icenowy Zheng <icenowy@aosc.io>
  3. *
  4. * Based on sun4i_layer.h, which is:
  5. * Copyright (C) 2015 Free Electrons
  6. * Copyright (C) 2015 NextThing Co
  7. *
  8. * Maxime Ripard <maxime.ripard@free-electrons.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. */
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_fb_cma_helper.h>
  20. #include <drm/drm_gem_cma_helper.h>
  21. #include <drm/drm_plane_helper.h>
  22. #include <drm/drmP.h>
  23. #include "sun8i_ui_layer.h"
  24. #include "sun8i_mixer.h"
  25. #include "sun8i_ui_scaler.h"
  26. static void sun8i_ui_layer_enable(struct sun8i_mixer *mixer, int channel,
  27. int overlay, bool enable, unsigned int zpos,
  28. unsigned int old_zpos)
  29. {
  30. u32 val;
  31. DRM_DEBUG_DRIVER("%sabling channel %d overlay %d\n",
  32. enable ? "En" : "Dis", channel, overlay);
  33. if (enable)
  34. val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN;
  35. else
  36. val = 0;
  37. regmap_update_bits(mixer->engine.regs,
  38. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(channel, overlay),
  39. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN, val);
  40. if (!enable || zpos != old_zpos) {
  41. regmap_update_bits(mixer->engine.regs,
  42. SUN8I_MIXER_BLEND_PIPE_CTL,
  43. SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos),
  44. 0);
  45. regmap_update_bits(mixer->engine.regs,
  46. SUN8I_MIXER_BLEND_ROUTE,
  47. SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos),
  48. 0);
  49. }
  50. if (enable) {
  51. val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos);
  52. regmap_update_bits(mixer->engine.regs,
  53. SUN8I_MIXER_BLEND_PIPE_CTL, val, val);
  54. val = channel << SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos);
  55. regmap_update_bits(mixer->engine.regs,
  56. SUN8I_MIXER_BLEND_ROUTE,
  57. SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos),
  58. val);
  59. }
  60. }
  61. static int sun8i_ui_layer_update_coord(struct sun8i_mixer *mixer, int channel,
  62. int overlay, struct drm_plane *plane,
  63. unsigned int zpos)
  64. {
  65. struct drm_plane_state *state = plane->state;
  66. u32 src_w, src_h, dst_w, dst_h;
  67. u32 outsize, insize;
  68. u32 hphase, vphase;
  69. DRM_DEBUG_DRIVER("Updating UI channel %d overlay %d\n",
  70. channel, overlay);
  71. src_w = drm_rect_width(&state->src) >> 16;
  72. src_h = drm_rect_height(&state->src) >> 16;
  73. dst_w = drm_rect_width(&state->dst);
  74. dst_h = drm_rect_height(&state->dst);
  75. hphase = state->src.x1 & 0xffff;
  76. vphase = state->src.y1 & 0xffff;
  77. insize = SUN8I_MIXER_SIZE(src_w, src_h);
  78. outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
  79. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  80. bool interlaced = false;
  81. u32 val;
  82. DRM_DEBUG_DRIVER("Primary layer, updating global size W: %u H: %u\n",
  83. dst_w, dst_h);
  84. regmap_write(mixer->engine.regs,
  85. SUN8I_MIXER_GLOBAL_SIZE,
  86. outsize);
  87. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_OUTSIZE,
  88. outsize);
  89. if (state->crtc)
  90. interlaced = state->crtc->state->adjusted_mode.flags
  91. & DRM_MODE_FLAG_INTERLACE;
  92. if (interlaced)
  93. val = SUN8I_MIXER_BLEND_OUTCTL_INTERLACED;
  94. else
  95. val = 0;
  96. regmap_update_bits(mixer->engine.regs,
  97. SUN8I_MIXER_BLEND_OUTCTL,
  98. SUN8I_MIXER_BLEND_OUTCTL_INTERLACED,
  99. val);
  100. DRM_DEBUG_DRIVER("Switching display mixer interlaced mode %s\n",
  101. interlaced ? "on" : "off");
  102. }
  103. /* Set height and width */
  104. DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
  105. state->src.x1 >> 16, state->src.y1 >> 16);
  106. DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
  107. regmap_write(mixer->engine.regs,
  108. SUN8I_MIXER_CHAN_UI_LAYER_SIZE(channel, overlay),
  109. insize);
  110. regmap_write(mixer->engine.regs,
  111. SUN8I_MIXER_CHAN_UI_OVL_SIZE(channel),
  112. insize);
  113. if (insize != outsize || hphase || vphase) {
  114. u32 hscale, vscale;
  115. DRM_DEBUG_DRIVER("HW scaling is enabled\n");
  116. hscale = state->src_w / state->crtc_w;
  117. vscale = state->src_h / state->crtc_h;
  118. sun8i_ui_scaler_setup(mixer, channel, src_w, src_h, dst_w,
  119. dst_h, hscale, vscale, hphase, vphase);
  120. sun8i_ui_scaler_enable(mixer, channel, true);
  121. } else {
  122. DRM_DEBUG_DRIVER("HW scaling is not needed\n");
  123. sun8i_ui_scaler_enable(mixer, channel, false);
  124. }
  125. /* Set base coordinates */
  126. DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
  127. state->dst.x1, state->dst.y1);
  128. DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
  129. regmap_write(mixer->engine.regs,
  130. SUN8I_MIXER_BLEND_ATTR_COORD(zpos),
  131. SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
  132. regmap_write(mixer->engine.regs,
  133. SUN8I_MIXER_BLEND_ATTR_INSIZE(zpos),
  134. outsize);
  135. return 0;
  136. }
  137. static int sun8i_ui_layer_update_formats(struct sun8i_mixer *mixer, int channel,
  138. int overlay, struct drm_plane *plane)
  139. {
  140. struct drm_plane_state *state = plane->state;
  141. const struct de2_fmt_info *fmt_info;
  142. u32 val;
  143. fmt_info = sun8i_mixer_format_info(state->fb->format->format);
  144. if (!fmt_info || !fmt_info->rgb) {
  145. DRM_DEBUG_DRIVER("Invalid format\n");
  146. return -EINVAL;
  147. }
  148. val = fmt_info->de2_fmt << SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_OFFSET;
  149. regmap_update_bits(mixer->engine.regs,
  150. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(channel, overlay),
  151. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_MASK, val);
  152. return 0;
  153. }
  154. static int sun8i_ui_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
  155. int overlay, struct drm_plane *plane)
  156. {
  157. struct drm_plane_state *state = plane->state;
  158. struct drm_framebuffer *fb = state->fb;
  159. struct drm_gem_cma_object *gem;
  160. dma_addr_t paddr;
  161. int bpp;
  162. /* Get the physical address of the buffer in memory */
  163. gem = drm_fb_cma_get_gem_obj(fb, 0);
  164. DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
  165. /* Compute the start of the displayed memory */
  166. bpp = fb->format->cpp[0];
  167. paddr = gem->paddr + fb->offsets[0];
  168. /* Fixup framebuffer address for src coordinates */
  169. paddr += (state->src.x1 >> 16) * bpp;
  170. paddr += (state->src.y1 >> 16) * fb->pitches[0];
  171. /* Set the line width */
  172. DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
  173. regmap_write(mixer->engine.regs,
  174. SUN8I_MIXER_CHAN_UI_LAYER_PITCH(channel, overlay),
  175. fb->pitches[0]);
  176. DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &paddr);
  177. regmap_write(mixer->engine.regs,
  178. SUN8I_MIXER_CHAN_UI_LAYER_TOP_LADDR(channel, overlay),
  179. lower_32_bits(paddr));
  180. return 0;
  181. }
  182. static int sun8i_ui_layer_atomic_check(struct drm_plane *plane,
  183. struct drm_plane_state *state)
  184. {
  185. struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
  186. struct drm_crtc *crtc = state->crtc;
  187. struct drm_crtc_state *crtc_state;
  188. int min_scale, max_scale;
  189. if (!crtc)
  190. return 0;
  191. crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
  192. if (WARN_ON(!crtc_state))
  193. return -EINVAL;
  194. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  195. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  196. if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
  197. min_scale = SUN8I_UI_SCALER_SCALE_MIN;
  198. max_scale = SUN8I_UI_SCALER_SCALE_MAX;
  199. }
  200. return drm_atomic_helper_check_plane_state(state, crtc_state,
  201. min_scale, max_scale,
  202. true, true);
  203. }
  204. static void sun8i_ui_layer_atomic_disable(struct drm_plane *plane,
  205. struct drm_plane_state *old_state)
  206. {
  207. struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
  208. unsigned int old_zpos = old_state->normalized_zpos;
  209. struct sun8i_mixer *mixer = layer->mixer;
  210. sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay, false, 0,
  211. old_zpos);
  212. }
  213. static void sun8i_ui_layer_atomic_update(struct drm_plane *plane,
  214. struct drm_plane_state *old_state)
  215. {
  216. struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
  217. unsigned int zpos = plane->state->normalized_zpos;
  218. unsigned int old_zpos = old_state->normalized_zpos;
  219. struct sun8i_mixer *mixer = layer->mixer;
  220. if (!plane->state->visible) {
  221. sun8i_ui_layer_enable(mixer, layer->channel,
  222. layer->overlay, false, 0, old_zpos);
  223. return;
  224. }
  225. sun8i_ui_layer_update_coord(mixer, layer->channel,
  226. layer->overlay, plane, zpos);
  227. sun8i_ui_layer_update_formats(mixer, layer->channel,
  228. layer->overlay, plane);
  229. sun8i_ui_layer_update_buffer(mixer, layer->channel,
  230. layer->overlay, plane);
  231. sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay,
  232. true, zpos, old_zpos);
  233. }
  234. static struct drm_plane_helper_funcs sun8i_ui_layer_helper_funcs = {
  235. .atomic_check = sun8i_ui_layer_atomic_check,
  236. .atomic_disable = sun8i_ui_layer_atomic_disable,
  237. .atomic_update = sun8i_ui_layer_atomic_update,
  238. };
  239. static const struct drm_plane_funcs sun8i_ui_layer_funcs = {
  240. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  241. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  242. .destroy = drm_plane_cleanup,
  243. .disable_plane = drm_atomic_helper_disable_plane,
  244. .reset = drm_atomic_helper_plane_reset,
  245. .update_plane = drm_atomic_helper_update_plane,
  246. };
  247. static const u32 sun8i_ui_layer_formats[] = {
  248. DRM_FORMAT_ABGR1555,
  249. DRM_FORMAT_ABGR4444,
  250. DRM_FORMAT_ABGR8888,
  251. DRM_FORMAT_ARGB1555,
  252. DRM_FORMAT_ARGB4444,
  253. DRM_FORMAT_ARGB8888,
  254. DRM_FORMAT_BGR565,
  255. DRM_FORMAT_BGR888,
  256. DRM_FORMAT_BGRA5551,
  257. DRM_FORMAT_BGRA4444,
  258. DRM_FORMAT_BGRA8888,
  259. DRM_FORMAT_BGRX8888,
  260. DRM_FORMAT_RGB565,
  261. DRM_FORMAT_RGB888,
  262. DRM_FORMAT_RGBA4444,
  263. DRM_FORMAT_RGBA5551,
  264. DRM_FORMAT_RGBA8888,
  265. DRM_FORMAT_RGBX8888,
  266. DRM_FORMAT_XBGR8888,
  267. DRM_FORMAT_XRGB8888,
  268. };
  269. struct sun8i_ui_layer *sun8i_ui_layer_init_one(struct drm_device *drm,
  270. struct sun8i_mixer *mixer,
  271. int index)
  272. {
  273. enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
  274. int channel = mixer->cfg->vi_num + index;
  275. struct sun8i_ui_layer *layer;
  276. unsigned int plane_cnt;
  277. int ret;
  278. layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
  279. if (!layer)
  280. return ERR_PTR(-ENOMEM);
  281. if (index == 0)
  282. type = DRM_PLANE_TYPE_PRIMARY;
  283. /* possible crtcs are set later */
  284. ret = drm_universal_plane_init(drm, &layer->plane, 0,
  285. &sun8i_ui_layer_funcs,
  286. sun8i_ui_layer_formats,
  287. ARRAY_SIZE(sun8i_ui_layer_formats),
  288. NULL, type, NULL);
  289. if (ret) {
  290. dev_err(drm->dev, "Couldn't initialize layer\n");
  291. return ERR_PTR(ret);
  292. }
  293. plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
  294. ret = drm_plane_create_zpos_property(&layer->plane, channel,
  295. 0, plane_cnt - 1);
  296. if (ret) {
  297. dev_err(drm->dev, "Couldn't add zpos property\n");
  298. return ERR_PTR(ret);
  299. }
  300. drm_plane_helper_add(&layer->plane, &sun8i_ui_layer_helper_funcs);
  301. layer->mixer = mixer;
  302. layer->channel = channel;
  303. layer->overlay = 0;
  304. return layer;
  305. }