sun8i_vi_layer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if (!crtc)
  198. return 0;
  199. crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
  200. if (WARN_ON(!crtc_state))
  201. return -EINVAL;
  202. min_scale = DRM_PLANE_HELPER_NO_SCALING;
  203. max_scale = DRM_PLANE_HELPER_NO_SCALING;
  204. if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
  205. min_scale = SUN8I_VI_SCALER_SCALE_MIN;
  206. max_scale = SUN8I_VI_SCALER_SCALE_MAX;
  207. }
  208. return drm_atomic_helper_check_plane_state(state, crtc_state,
  209. min_scale, max_scale,
  210. true, true);
  211. }
  212. static void sun8i_vi_layer_atomic_disable(struct drm_plane *plane,
  213. struct drm_plane_state *old_state)
  214. {
  215. struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
  216. struct sun8i_mixer *mixer = layer->mixer;
  217. sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, false);
  218. }
  219. static void sun8i_vi_layer_atomic_update(struct drm_plane *plane,
  220. struct drm_plane_state *old_state)
  221. {
  222. struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
  223. struct sun8i_mixer *mixer = layer->mixer;
  224. if (!plane->state->visible) {
  225. sun8i_vi_layer_enable(mixer, layer->channel,
  226. layer->overlay, false);
  227. return;
  228. }
  229. sun8i_vi_layer_update_coord(mixer, layer->channel,
  230. layer->overlay, plane);
  231. sun8i_vi_layer_update_formats(mixer, layer->channel,
  232. layer->overlay, plane);
  233. sun8i_vi_layer_update_buffer(mixer, layer->channel,
  234. layer->overlay, plane);
  235. sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, true);
  236. }
  237. static struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = {
  238. .atomic_check = sun8i_vi_layer_atomic_check,
  239. .atomic_disable = sun8i_vi_layer_atomic_disable,
  240. .atomic_update = sun8i_vi_layer_atomic_update,
  241. };
  242. static const struct drm_plane_funcs sun8i_vi_layer_funcs = {
  243. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  244. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  245. .destroy = drm_plane_cleanup,
  246. .disable_plane = drm_atomic_helper_disable_plane,
  247. .reset = drm_atomic_helper_plane_reset,
  248. .update_plane = drm_atomic_helper_update_plane,
  249. };
  250. /*
  251. * While all RGB formats are supported, VI planes don't support
  252. * alpha blending, so there is no point having formats with alpha
  253. * channel if their opaque analog exist.
  254. */
  255. static const u32 sun8i_vi_layer_formats[] = {
  256. DRM_FORMAT_ABGR1555,
  257. DRM_FORMAT_ABGR4444,
  258. DRM_FORMAT_ARGB1555,
  259. DRM_FORMAT_ARGB4444,
  260. DRM_FORMAT_BGR565,
  261. DRM_FORMAT_BGR888,
  262. DRM_FORMAT_BGRA5551,
  263. DRM_FORMAT_BGRA4444,
  264. DRM_FORMAT_BGRX8888,
  265. DRM_FORMAT_RGB565,
  266. DRM_FORMAT_RGB888,
  267. DRM_FORMAT_RGBA4444,
  268. DRM_FORMAT_RGBA5551,
  269. DRM_FORMAT_RGBX8888,
  270. DRM_FORMAT_XBGR8888,
  271. DRM_FORMAT_XRGB8888,
  272. DRM_FORMAT_NV16,
  273. DRM_FORMAT_NV12,
  274. DRM_FORMAT_NV21,
  275. DRM_FORMAT_NV61,
  276. DRM_FORMAT_UYVY,
  277. DRM_FORMAT_VYUY,
  278. DRM_FORMAT_YUYV,
  279. DRM_FORMAT_YVYU,
  280. DRM_FORMAT_YUV411,
  281. DRM_FORMAT_YUV420,
  282. DRM_FORMAT_YUV422,
  283. DRM_FORMAT_YUV444,
  284. DRM_FORMAT_YVU411,
  285. DRM_FORMAT_YVU420,
  286. DRM_FORMAT_YVU422,
  287. DRM_FORMAT_YVU444,
  288. };
  289. struct sun8i_vi_layer *sun8i_vi_layer_init_one(struct drm_device *drm,
  290. struct sun8i_mixer *mixer,
  291. int index)
  292. {
  293. struct sun8i_vi_layer *layer;
  294. int ret;
  295. layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
  296. if (!layer)
  297. return ERR_PTR(-ENOMEM);
  298. /* possible crtcs are set later */
  299. ret = drm_universal_plane_init(drm, &layer->plane, 0,
  300. &sun8i_vi_layer_funcs,
  301. sun8i_vi_layer_formats,
  302. ARRAY_SIZE(sun8i_vi_layer_formats),
  303. NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
  304. if (ret) {
  305. dev_err(drm->dev, "Couldn't initialize layer\n");
  306. return ERR_PTR(ret);
  307. }
  308. /* fixed zpos for now */
  309. ret = drm_plane_create_zpos_immutable_property(&layer->plane, index);
  310. if (ret) {
  311. dev_err(drm->dev, "Couldn't add zpos property\n");
  312. return ERR_PTR(ret);
  313. }
  314. drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs);
  315. layer->mixer = mixer;
  316. layer->channel = index;
  317. layer->overlay = 0;
  318. return layer;
  319. }