sun8i_vi_layer.c 11 KB

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