sun8i_vi_layer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. */
  9. #include <drm/drm_atomic.h>
  10. #include <drm/drm_atomic_helper.h>
  11. #include <drm/drm_crtc.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include <drm/drm_fb_cma_helper.h>
  14. #include <drm/drm_gem_cma_helper.h>
  15. #include <drm/drm_plane_helper.h>
  16. #include <drm/drmP.h>
  17. #include "sun8i_vi_layer.h"
  18. #include "sun8i_mixer.h"
  19. #include "sun8i_vi_scaler.h"
  20. static void sun8i_vi_layer_enable(struct sun8i_mixer *mixer, int channel,
  21. int overlay, bool enable)
  22. {
  23. u32 val;
  24. DRM_DEBUG_DRIVER("%sabling VI channel %d overlay %d\n",
  25. enable ? "En" : "Dis", channel, overlay);
  26. if (enable)
  27. val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN;
  28. else
  29. val = 0;
  30. regmap_update_bits(mixer->engine.regs,
  31. SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
  32. SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN, val);
  33. if (enable)
  34. val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(channel);
  35. else
  36. val = 0;
  37. regmap_update_bits(mixer->engine.regs,
  38. SUN8I_MIXER_BLEND_PIPE_CTL,
  39. SUN8I_MIXER_BLEND_PIPE_CTL_EN(channel), val);
  40. }
  41. static int sun8i_vi_layer_update_coord(struct sun8i_mixer *mixer, int channel,
  42. int overlay, struct drm_plane *plane)
  43. {
  44. struct drm_plane_state *state = plane->state;
  45. const struct drm_format_info *format = state->fb->format;
  46. u32 src_w, src_h, dst_w, dst_h;
  47. u32 outsize, insize;
  48. u32 hphase, vphase;
  49. bool subsampled;
  50. DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n",
  51. channel, overlay);
  52. src_w = drm_rect_width(&state->src) >> 16;
  53. src_h = drm_rect_height(&state->src) >> 16;
  54. dst_w = drm_rect_width(&state->dst);
  55. dst_h = drm_rect_height(&state->dst);
  56. hphase = state->src.x1 & 0xffff;
  57. vphase = state->src.y1 & 0xffff;
  58. /* make coordinates dividable by subsampling factor */
  59. if (format->hsub > 1) {
  60. int mask, remainder;
  61. mask = format->hsub - 1;
  62. remainder = (state->src.x1 >> 16) & mask;
  63. src_w = (src_w + remainder) & ~mask;
  64. hphase += remainder << 16;
  65. }
  66. if (format->vsub > 1) {
  67. int mask, remainder;
  68. mask = format->vsub - 1;
  69. remainder = (state->src.y1 >> 16) & mask;
  70. src_h = (src_h + remainder) & ~mask;
  71. vphase += remainder << 16;
  72. }
  73. insize = SUN8I_MIXER_SIZE(src_w, src_h);
  74. outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
  75. /* Set height and width */
  76. DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
  77. (state->src.x1 >> 16) & ~(format->hsub - 1),
  78. (state->src.y1 >> 16) & ~(format->vsub - 1));
  79. DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
  80. regmap_write(mixer->engine.regs,
  81. SUN8I_MIXER_CHAN_VI_LAYER_SIZE(channel, overlay),
  82. insize);
  83. regmap_write(mixer->engine.regs,
  84. SUN8I_MIXER_CHAN_VI_OVL_SIZE(channel),
  85. insize);
  86. /*
  87. * Scaler must be enabled for subsampled formats, so it scales
  88. * chroma to same size as luma.
  89. */
  90. subsampled = format->hsub > 1 || format->vsub > 1;
  91. if (insize != outsize || subsampled || hphase || vphase) {
  92. u32 hscale, vscale;
  93. DRM_DEBUG_DRIVER("HW scaling is enabled\n");
  94. hscale = state->src_w / state->crtc_w;
  95. vscale = state->src_h / state->crtc_h;
  96. sun8i_vi_scaler_setup(mixer, channel, src_w, src_h, dst_w,
  97. dst_h, hscale, vscale, hphase, vphase,
  98. format);
  99. sun8i_vi_scaler_enable(mixer, channel, true);
  100. } else {
  101. DRM_DEBUG_DRIVER("HW scaling is not needed\n");
  102. sun8i_vi_scaler_enable(mixer, channel, false);
  103. }
  104. /* Set base coordinates */
  105. DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
  106. state->dst.x1, state->dst.y1);
  107. DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
  108. regmap_write(mixer->engine.regs,
  109. SUN8I_MIXER_BLEND_ATTR_COORD(channel),
  110. SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
  111. regmap_write(mixer->engine.regs,
  112. SUN8I_MIXER_BLEND_ATTR_INSIZE(channel),
  113. outsize);
  114. return 0;
  115. }
  116. static int sun8i_vi_layer_update_formats(struct sun8i_mixer *mixer, int channel,
  117. int overlay, struct drm_plane *plane)
  118. {
  119. struct drm_plane_state *state = plane->state;
  120. const struct de2_fmt_info *fmt_info;
  121. u32 val;
  122. fmt_info = sun8i_mixer_format_info(state->fb->format->format);
  123. if (!fmt_info) {
  124. DRM_DEBUG_DRIVER("Invalid format\n");
  125. return -EINVAL;
  126. }
  127. val = fmt_info->de2_fmt << SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET;
  128. regmap_update_bits(mixer->engine.regs,
  129. SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
  130. SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK, val);
  131. if (fmt_info->csc != SUN8I_CSC_MODE_OFF) {
  132. sun8i_csc_set_ccsc_coefficients(mixer, channel, fmt_info->csc);
  133. sun8i_csc_enable_ccsc(mixer, channel, true);
  134. } else {
  135. sun8i_csc_enable_ccsc(mixer, channel, false);
  136. }
  137. if (fmt_info->rgb)
  138. val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE;
  139. else
  140. val = 0;
  141. regmap_update_bits(mixer->engine.regs,
  142. SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
  143. SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE, val);
  144. return 0;
  145. }
  146. static int sun8i_vi_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
  147. int overlay, struct drm_plane *plane)
  148. {
  149. struct drm_plane_state *state = plane->state;
  150. struct drm_framebuffer *fb = state->fb;
  151. const struct drm_format_info *format = fb->format;
  152. struct drm_gem_cma_object *gem;
  153. u32 dx, dy, src_x, src_y;
  154. dma_addr_t paddr;
  155. int i;
  156. /* Adjust x and y to be dividable by subsampling factor */
  157. src_x = (state->src.x1 >> 16) & ~(format->hsub - 1);
  158. src_y = (state->src.y1 >> 16) & ~(format->vsub - 1);
  159. for (i = 0; i < format->num_planes; i++) {
  160. /* Get the physical address of the buffer in memory */
  161. gem = drm_fb_cma_get_gem_obj(fb, i);
  162. DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
  163. /* Compute the start of the displayed memory */
  164. paddr = gem->paddr + fb->offsets[i];
  165. dx = src_x;
  166. dy = src_y;
  167. if (i > 0) {
  168. dx /= format->hsub;
  169. dy /= format->vsub;
  170. }
  171. /* Fixup framebuffer address for src coordinates */
  172. paddr += dx * format->cpp[i];
  173. paddr += dy * fb->pitches[i];
  174. /* Set the line width */
  175. DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
  176. i + 1, fb->pitches[i]);
  177. regmap_write(mixer->engine.regs,
  178. SUN8I_MIXER_CHAN_VI_LAYER_PITCH(channel,
  179. overlay, i),
  180. fb->pitches[i]);
  181. DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n",
  182. i + 1, &paddr);
  183. regmap_write(mixer->engine.regs,
  184. SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(channel,
  185. overlay, i),
  186. lower_32_bits(paddr));
  187. }
  188. return 0;
  189. }
  190. static int sun8i_vi_layer_atomic_check(struct drm_plane *plane,
  191. struct drm_plane_state *state)
  192. {
  193. struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
  194. struct drm_crtc *crtc = state->crtc;
  195. struct drm_crtc_state *crtc_state;
  196. int min_scale, max_scale;
  197. struct drm_rect clip;
  198. if (!crtc)
  199. return 0;
  200. crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
  201. if (WARN_ON(!crtc_state))
  202. return -EINVAL;
  203. clip.x1 = 0;
  204. clip.y1 = 0;
  205. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  206. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  207. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  208. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  209. if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
  210. min_scale = SUN8I_VI_SCALER_SCALE_MIN;
  211. max_scale = SUN8I_VI_SCALER_SCALE_MAX;
  212. }
  213. return drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
  214. min_scale, max_scale,
  215. true, true);
  216. }
  217. static void sun8i_vi_layer_atomic_disable(struct drm_plane *plane,
  218. struct drm_plane_state *old_state)
  219. {
  220. struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
  221. struct sun8i_mixer *mixer = layer->mixer;
  222. sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, false);
  223. }
  224. static void sun8i_vi_layer_atomic_update(struct drm_plane *plane,
  225. struct drm_plane_state *old_state)
  226. {
  227. struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
  228. struct sun8i_mixer *mixer = layer->mixer;
  229. if (!plane->state->visible) {
  230. sun8i_vi_layer_enable(mixer, layer->channel,
  231. layer->overlay, false);
  232. return;
  233. }
  234. sun8i_vi_layer_update_coord(mixer, layer->channel,
  235. layer->overlay, plane);
  236. sun8i_vi_layer_update_formats(mixer, layer->channel,
  237. layer->overlay, plane);
  238. sun8i_vi_layer_update_buffer(mixer, layer->channel,
  239. layer->overlay, plane);
  240. sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, true);
  241. }
  242. static struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = {
  243. .atomic_check = sun8i_vi_layer_atomic_check,
  244. .atomic_disable = sun8i_vi_layer_atomic_disable,
  245. .atomic_update = sun8i_vi_layer_atomic_update,
  246. };
  247. static const struct drm_plane_funcs sun8i_vi_layer_funcs = {
  248. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  249. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  250. .destroy = drm_plane_cleanup,
  251. .disable_plane = drm_atomic_helper_disable_plane,
  252. .reset = drm_atomic_helper_plane_reset,
  253. .update_plane = drm_atomic_helper_update_plane,
  254. };
  255. /*
  256. * While all RGB formats are supported, VI planes don't support
  257. * alpha blending, so there is no point having formats with alpha
  258. * channel if their opaque analog exist.
  259. */
  260. static const u32 sun8i_vi_layer_formats[] = {
  261. DRM_FORMAT_ABGR1555,
  262. DRM_FORMAT_ABGR4444,
  263. DRM_FORMAT_ARGB1555,
  264. DRM_FORMAT_ARGB4444,
  265. DRM_FORMAT_BGR565,
  266. DRM_FORMAT_BGR888,
  267. DRM_FORMAT_BGRA5551,
  268. DRM_FORMAT_BGRA4444,
  269. DRM_FORMAT_BGRX8888,
  270. DRM_FORMAT_RGB565,
  271. DRM_FORMAT_RGB888,
  272. DRM_FORMAT_RGBA4444,
  273. DRM_FORMAT_RGBA5551,
  274. DRM_FORMAT_RGBX8888,
  275. DRM_FORMAT_XBGR8888,
  276. DRM_FORMAT_XRGB8888,
  277. DRM_FORMAT_NV16,
  278. DRM_FORMAT_NV12,
  279. DRM_FORMAT_NV21,
  280. DRM_FORMAT_NV61,
  281. DRM_FORMAT_UYVY,
  282. DRM_FORMAT_VYUY,
  283. DRM_FORMAT_YUYV,
  284. DRM_FORMAT_YVYU,
  285. DRM_FORMAT_YUV411,
  286. DRM_FORMAT_YUV420,
  287. DRM_FORMAT_YUV422,
  288. DRM_FORMAT_YUV444,
  289. DRM_FORMAT_YVU411,
  290. DRM_FORMAT_YVU420,
  291. DRM_FORMAT_YVU422,
  292. DRM_FORMAT_YVU444,
  293. };
  294. struct sun8i_vi_layer *sun8i_vi_layer_init_one(struct drm_device *drm,
  295. struct sun8i_mixer *mixer,
  296. int index)
  297. {
  298. struct sun8i_vi_layer *layer;
  299. int ret;
  300. layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
  301. if (!layer)
  302. return ERR_PTR(-ENOMEM);
  303. /* possible crtcs are set later */
  304. ret = drm_universal_plane_init(drm, &layer->plane, 0,
  305. &sun8i_vi_layer_funcs,
  306. sun8i_vi_layer_formats,
  307. ARRAY_SIZE(sun8i_vi_layer_formats),
  308. NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
  309. if (ret) {
  310. dev_err(drm->dev, "Couldn't initialize layer\n");
  311. return ERR_PTR(ret);
  312. }
  313. /* fixed zpos for now */
  314. ret = drm_plane_create_zpos_immutable_property(&layer->plane, index);
  315. if (ret) {
  316. dev_err(drm->dev, "Couldn't add zpos property\n");
  317. return ERR_PTR(ret);
  318. }
  319. drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs);
  320. layer->mixer = mixer;
  321. layer->channel = index;
  322. layer->overlay = 0;
  323. return layer;
  324. }