sun4i_backend.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Free Electrons
  4. * Copyright (C) 2015 NextThing Co
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. */
  8. #include <linux/component.h>
  9. #include <linux/list.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_blend.h>
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_fb_dma_helper.h>
  21. #include <drm/drm_fourcc.h>
  22. #include <drm/drm_framebuffer.h>
  23. #include <drm/drm_gem_dma_helper.h>
  24. #include <drm/drm_probe_helper.h>
  25. #include "sun4i_backend.h"
  26. #include "sun4i_drv.h"
  27. #include "sun4i_frontend.h"
  28. #include "sun4i_layer.h"
  29. #include "sunxi_engine.h"
  30. struct sun4i_backend_quirks {
  31. /* backend <-> TCON muxing selection done in backend */
  32. bool needs_output_muxing;
  33. /* alpha at the lowest z position is not always supported */
  34. bool supports_lowest_plane_alpha;
  35. };
  36. static const u32 sunxi_rgb2yuv_coef[12] = {
  37. 0x00000107, 0x00000204, 0x00000064, 0x00000108,
  38. 0x00003f69, 0x00003ed6, 0x000001c1, 0x00000808,
  39. 0x000001c1, 0x00003e88, 0x00003fb8, 0x00000808
  40. };
  41. static void sun4i_backend_apply_color_correction(struct sunxi_engine *engine)
  42. {
  43. int i;
  44. DRM_DEBUG_DRIVER("Applying RGB to YUV color correction\n");
  45. /* Set color correction */
  46. regmap_write(engine->regs, SUN4I_BACKEND_OCCTL_REG,
  47. SUN4I_BACKEND_OCCTL_ENABLE);
  48. for (i = 0; i < 12; i++)
  49. regmap_write(engine->regs, SUN4I_BACKEND_OCRCOEF_REG(i),
  50. sunxi_rgb2yuv_coef[i]);
  51. }
  52. static void sun4i_backend_disable_color_correction(struct sunxi_engine *engine)
  53. {
  54. DRM_DEBUG_DRIVER("Disabling color correction\n");
  55. /* Disable color correction */
  56. regmap_update_bits(engine->regs, SUN4I_BACKEND_OCCTL_REG,
  57. SUN4I_BACKEND_OCCTL_ENABLE, 0);
  58. }
  59. static void sun4i_backend_commit(struct sunxi_engine *engine,
  60. struct drm_crtc *crtc,
  61. struct drm_atomic_state *state)
  62. {
  63. DRM_DEBUG_DRIVER("Committing changes\n");
  64. regmap_write(engine->regs, SUN4I_BACKEND_REGBUFFCTL_REG,
  65. SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS |
  66. SUN4I_BACKEND_REGBUFFCTL_LOADCTL);
  67. }
  68. void sun4i_backend_layer_enable(struct sun4i_backend *backend,
  69. int layer, bool enable)
  70. {
  71. u32 val;
  72. DRM_DEBUG_DRIVER("%sabling layer %d\n", enable ? "En" : "Dis",
  73. layer);
  74. if (enable)
  75. val = SUN4I_BACKEND_MODCTL_LAY_EN(layer);
  76. else
  77. val = 0;
  78. regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG,
  79. SUN4I_BACKEND_MODCTL_LAY_EN(layer), val);
  80. }
  81. static int sun4i_backend_drm_format_to_layer(u32 format, u32 *mode)
  82. {
  83. switch (format) {
  84. case DRM_FORMAT_ARGB8888:
  85. *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB8888;
  86. break;
  87. case DRM_FORMAT_ARGB4444:
  88. *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB4444;
  89. break;
  90. case DRM_FORMAT_ARGB1555:
  91. *mode = SUN4I_BACKEND_LAY_FBFMT_ARGB1555;
  92. break;
  93. case DRM_FORMAT_RGBA5551:
  94. *mode = SUN4I_BACKEND_LAY_FBFMT_RGBA5551;
  95. break;
  96. case DRM_FORMAT_RGBA4444:
  97. *mode = SUN4I_BACKEND_LAY_FBFMT_RGBA4444;
  98. break;
  99. case DRM_FORMAT_XRGB8888:
  100. *mode = SUN4I_BACKEND_LAY_FBFMT_XRGB8888;
  101. break;
  102. case DRM_FORMAT_RGB888:
  103. *mode = SUN4I_BACKEND_LAY_FBFMT_RGB888;
  104. break;
  105. case DRM_FORMAT_RGB565:
  106. *mode = SUN4I_BACKEND_LAY_FBFMT_RGB565;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. return 0;
  112. }
  113. static const uint32_t sun4i_backend_formats[] = {
  114. DRM_FORMAT_ARGB1555,
  115. DRM_FORMAT_ARGB4444,
  116. DRM_FORMAT_ARGB8888,
  117. DRM_FORMAT_RGB565,
  118. DRM_FORMAT_RGB888,
  119. DRM_FORMAT_RGBA4444,
  120. DRM_FORMAT_RGBA5551,
  121. DRM_FORMAT_UYVY,
  122. DRM_FORMAT_VYUY,
  123. DRM_FORMAT_XRGB8888,
  124. DRM_FORMAT_YUYV,
  125. DRM_FORMAT_YVYU,
  126. };
  127. bool sun4i_backend_format_is_supported(uint32_t fmt, uint64_t modifier)
  128. {
  129. unsigned int i;
  130. if (modifier != DRM_FORMAT_MOD_LINEAR)
  131. return false;
  132. for (i = 0; i < ARRAY_SIZE(sun4i_backend_formats); i++)
  133. if (sun4i_backend_formats[i] == fmt)
  134. return true;
  135. return false;
  136. }
  137. int sun4i_backend_update_layer_coord(struct sun4i_backend *backend,
  138. int layer, struct drm_plane *plane)
  139. {
  140. struct drm_plane_state *state = plane->state;
  141. DRM_DEBUG_DRIVER("Updating layer %d\n", layer);
  142. /* Set height and width */
  143. DRM_DEBUG_DRIVER("Layer size W: %u H: %u\n",
  144. state->crtc_w, state->crtc_h);
  145. regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYSIZE_REG(layer),
  146. SUN4I_BACKEND_LAYSIZE(state->crtc_w,
  147. state->crtc_h));
  148. /* Set base coordinates */
  149. DRM_DEBUG_DRIVER("Layer coordinates X: %d Y: %d\n",
  150. state->crtc_x, state->crtc_y);
  151. regmap_write(backend->engine.regs, SUN4I_BACKEND_LAYCOOR_REG(layer),
  152. SUN4I_BACKEND_LAYCOOR(state->crtc_x,
  153. state->crtc_y));
  154. return 0;
  155. }
  156. static int sun4i_backend_update_yuv_format(struct sun4i_backend *backend,
  157. int layer, struct drm_plane *plane)
  158. {
  159. struct drm_plane_state *state = plane->state;
  160. struct drm_framebuffer *fb = state->fb;
  161. const struct drm_format_info *format = fb->format;
  162. const uint32_t fmt = format->format;
  163. u32 val = SUN4I_BACKEND_IYUVCTL_EN;
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(sunxi_bt601_yuv2rgb_coef); i++)
  166. regmap_write(backend->engine.regs,
  167. SUN4I_BACKEND_YGCOEF_REG(i),
  168. sunxi_bt601_yuv2rgb_coef[i]);
  169. /*
  170. * We should do that only for a single plane, but the
  171. * framebuffer's atomic_check has our back on this.
  172. */
  173. regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_ATTCTL_REG0(layer),
  174. SUN4I_BACKEND_ATTCTL_REG0_LAY_YUVEN,
  175. SUN4I_BACKEND_ATTCTL_REG0_LAY_YUVEN);
  176. /* TODO: Add support for the multi-planar YUV formats */
  177. if (drm_format_info_is_yuv_packed(format) &&
  178. drm_format_info_is_yuv_sampling_422(format))
  179. val |= SUN4I_BACKEND_IYUVCTL_FBFMT_PACKED_YUV422;
  180. else
  181. DRM_DEBUG_DRIVER("Unsupported YUV format (0x%x)\n", fmt);
  182. /*
  183. * Allwinner seems to list the pixel sequence from right to left, while
  184. * DRM lists it from left to right.
  185. */
  186. switch (fmt) {
  187. case DRM_FORMAT_YUYV:
  188. val |= SUN4I_BACKEND_IYUVCTL_FBPS_VYUY;
  189. break;
  190. case DRM_FORMAT_YVYU:
  191. val |= SUN4I_BACKEND_IYUVCTL_FBPS_UYVY;
  192. break;
  193. case DRM_FORMAT_UYVY:
  194. val |= SUN4I_BACKEND_IYUVCTL_FBPS_YVYU;
  195. break;
  196. case DRM_FORMAT_VYUY:
  197. val |= SUN4I_BACKEND_IYUVCTL_FBPS_YUYV;
  198. break;
  199. default:
  200. DRM_DEBUG_DRIVER("Unsupported YUV pixel sequence (0x%x)\n",
  201. fmt);
  202. }
  203. regmap_write(backend->engine.regs, SUN4I_BACKEND_IYUVCTL_REG, val);
  204. return 0;
  205. }
  206. int sun4i_backend_update_layer_formats(struct sun4i_backend *backend,
  207. int layer, struct drm_plane *plane)
  208. {
  209. struct drm_plane_state *state = plane->state;
  210. struct drm_framebuffer *fb = state->fb;
  211. u32 val;
  212. int ret;
  213. /* Clear the YUV mode */
  214. regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_ATTCTL_REG0(layer),
  215. SUN4I_BACKEND_ATTCTL_REG0_LAY_YUVEN, 0);
  216. val = SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA(state->alpha >> 8);
  217. if (state->alpha != DRM_BLEND_ALPHA_OPAQUE)
  218. val |= SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA_EN;
  219. regmap_update_bits(backend->engine.regs,
  220. SUN4I_BACKEND_ATTCTL_REG0(layer),
  221. SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA_MASK |
  222. SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA_EN,
  223. val);
  224. if (fb->format->is_yuv)
  225. return sun4i_backend_update_yuv_format(backend, layer, plane);
  226. ret = sun4i_backend_drm_format_to_layer(fb->format->format, &val);
  227. if (ret) {
  228. DRM_DEBUG_DRIVER("Invalid format\n");
  229. return ret;
  230. }
  231. regmap_update_bits(backend->engine.regs,
  232. SUN4I_BACKEND_ATTCTL_REG1(layer),
  233. SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT, val);
  234. return 0;
  235. }
  236. int sun4i_backend_update_layer_frontend(struct sun4i_backend *backend,
  237. int layer, uint32_t fmt)
  238. {
  239. u32 val;
  240. int ret;
  241. ret = sun4i_backend_drm_format_to_layer(fmt, &val);
  242. if (ret) {
  243. DRM_DEBUG_DRIVER("Invalid format\n");
  244. return ret;
  245. }
  246. regmap_update_bits(backend->engine.regs,
  247. SUN4I_BACKEND_ATTCTL_REG0(layer),
  248. SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN,
  249. SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN);
  250. regmap_update_bits(backend->engine.regs,
  251. SUN4I_BACKEND_ATTCTL_REG1(layer),
  252. SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT, val);
  253. return 0;
  254. }
  255. static int sun4i_backend_update_yuv_buffer(struct sun4i_backend *backend,
  256. struct drm_framebuffer *fb,
  257. dma_addr_t paddr)
  258. {
  259. /* TODO: Add support for the multi-planar YUV formats */
  260. DRM_DEBUG_DRIVER("Setting packed YUV buffer address to %pad\n", &paddr);
  261. regmap_write(backend->engine.regs, SUN4I_BACKEND_IYUVADD_REG(0), paddr);
  262. DRM_DEBUG_DRIVER("Layer line width: %d bits\n", fb->pitches[0] * 8);
  263. regmap_write(backend->engine.regs, SUN4I_BACKEND_IYUVLINEWIDTH_REG(0),
  264. fb->pitches[0] * 8);
  265. return 0;
  266. }
  267. int sun4i_backend_update_layer_buffer(struct sun4i_backend *backend,
  268. int layer, struct drm_plane *plane)
  269. {
  270. struct drm_plane_state *state = plane->state;
  271. struct drm_framebuffer *fb = state->fb;
  272. u32 lo_paddr, hi_paddr;
  273. dma_addr_t dma_addr;
  274. /* Set the line width */
  275. DRM_DEBUG_DRIVER("Layer line width: %d bits\n", fb->pitches[0] * 8);
  276. regmap_write(backend->engine.regs,
  277. SUN4I_BACKEND_LAYLINEWIDTH_REG(layer),
  278. fb->pitches[0] * 8);
  279. /* Get the start of the displayed memory */
  280. dma_addr = drm_fb_dma_get_gem_addr(fb, state, 0);
  281. DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &dma_addr);
  282. if (fb->format->is_yuv)
  283. return sun4i_backend_update_yuv_buffer(backend, fb, dma_addr);
  284. /* Write the 32 lower bits of the address (in bits) */
  285. lo_paddr = dma_addr << 3;
  286. DRM_DEBUG_DRIVER("Setting address lower bits to 0x%x\n", lo_paddr);
  287. regmap_write(backend->engine.regs,
  288. SUN4I_BACKEND_LAYFB_L32ADD_REG(layer),
  289. lo_paddr);
  290. /* And the upper bits */
  291. hi_paddr = dma_addr >> 29;
  292. DRM_DEBUG_DRIVER("Setting address high bits to 0x%x\n", hi_paddr);
  293. regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_LAYFB_H4ADD_REG,
  294. SUN4I_BACKEND_LAYFB_H4ADD_MSK(layer),
  295. SUN4I_BACKEND_LAYFB_H4ADD(layer, hi_paddr));
  296. return 0;
  297. }
  298. int sun4i_backend_update_layer_zpos(struct sun4i_backend *backend, int layer,
  299. struct drm_plane *plane)
  300. {
  301. struct drm_plane_state *state = plane->state;
  302. struct sun4i_layer_state *p_state = state_to_sun4i_layer_state(state);
  303. unsigned int priority = state->normalized_zpos;
  304. unsigned int pipe = p_state->pipe;
  305. DRM_DEBUG_DRIVER("Setting layer %d's priority to %d and pipe %d\n",
  306. layer, priority, pipe);
  307. regmap_update_bits(backend->engine.regs, SUN4I_BACKEND_ATTCTL_REG0(layer),
  308. SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK |
  309. SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL_MASK,
  310. SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(p_state->pipe) |
  311. SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL(priority));
  312. return 0;
  313. }
  314. void sun4i_backend_cleanup_layer(struct sun4i_backend *backend,
  315. int layer)
  316. {
  317. regmap_update_bits(backend->engine.regs,
  318. SUN4I_BACKEND_ATTCTL_REG0(layer),
  319. SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN |
  320. SUN4I_BACKEND_ATTCTL_REG0_LAY_YUVEN, 0);
  321. }
  322. static bool sun4i_backend_plane_uses_scaler(struct drm_plane_state *state)
  323. {
  324. u16 src_h = state->src_h >> 16;
  325. u16 src_w = state->src_w >> 16;
  326. DRM_DEBUG_DRIVER("Input size %dx%d, output size %dx%d\n",
  327. src_w, src_h, state->crtc_w, state->crtc_h);
  328. if ((state->crtc_h != src_h) || (state->crtc_w != src_w))
  329. return true;
  330. return false;
  331. }
  332. static bool sun4i_backend_plane_uses_frontend(struct drm_plane_state *state)
  333. {
  334. struct sun4i_layer *layer = plane_to_sun4i_layer(state->plane);
  335. struct sun4i_backend *backend = layer->backend;
  336. uint32_t format = state->fb->format->format;
  337. uint64_t modifier = state->fb->modifier;
  338. if (IS_ERR(backend->frontend))
  339. return false;
  340. if (!sun4i_frontend_format_is_supported(format, modifier))
  341. return false;
  342. if (!sun4i_backend_format_is_supported(format, modifier))
  343. return true;
  344. /*
  345. * TODO: The backend alone allows 2x and 4x integer scaling, including
  346. * support for an alpha component (which the frontend doesn't support).
  347. * Use the backend directly instead of the frontend in this case, with
  348. * another test to return false.
  349. */
  350. if (sun4i_backend_plane_uses_scaler(state))
  351. return true;
  352. /*
  353. * Here the format is supported by both the frontend and the backend
  354. * and no frontend scaling is required, so use the backend directly.
  355. */
  356. return false;
  357. }
  358. static bool sun4i_backend_plane_is_supported(struct drm_plane_state *state,
  359. bool *uses_frontend)
  360. {
  361. if (sun4i_backend_plane_uses_frontend(state)) {
  362. *uses_frontend = true;
  363. return true;
  364. }
  365. *uses_frontend = false;
  366. /* Scaling is not supported without the frontend. */
  367. if (sun4i_backend_plane_uses_scaler(state))
  368. return false;
  369. return true;
  370. }
  371. static void sun4i_backend_atomic_begin(struct sunxi_engine *engine,
  372. struct drm_crtc_state *old_state)
  373. {
  374. u32 val;
  375. WARN_ON(regmap_read_poll_timeout(engine->regs,
  376. SUN4I_BACKEND_REGBUFFCTL_REG,
  377. val, !(val & SUN4I_BACKEND_REGBUFFCTL_LOADCTL),
  378. 100, 50000));
  379. }
  380. static int sun4i_backend_atomic_check(struct sunxi_engine *engine,
  381. struct drm_crtc_state *crtc_state)
  382. {
  383. struct drm_plane_state *plane_states[SUN4I_BACKEND_NUM_LAYERS] = { 0 };
  384. struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
  385. struct drm_atomic_state *state = crtc_state->state;
  386. struct drm_device *drm = state->dev;
  387. struct drm_plane *plane;
  388. unsigned int num_planes = 0;
  389. unsigned int num_alpha_planes = 0;
  390. unsigned int num_frontend_planes = 0;
  391. unsigned int num_alpha_planes_max = 1;
  392. unsigned int num_yuv_planes = 0;
  393. unsigned int current_pipe = 0;
  394. unsigned int i;
  395. DRM_DEBUG_DRIVER("Starting checking our planes\n");
  396. if (!crtc_state->planes_changed)
  397. return 0;
  398. drm_for_each_plane_mask(plane, drm, crtc_state->plane_mask) {
  399. struct drm_plane_state *plane_state =
  400. drm_atomic_get_plane_state(state, plane);
  401. struct sun4i_layer_state *layer_state =
  402. state_to_sun4i_layer_state(plane_state);
  403. struct drm_framebuffer *fb = plane_state->fb;
  404. if (!sun4i_backend_plane_is_supported(plane_state,
  405. &layer_state->uses_frontend))
  406. return -EINVAL;
  407. if (layer_state->uses_frontend) {
  408. DRM_DEBUG_DRIVER("Using the frontend for plane %d\n",
  409. plane->index);
  410. num_frontend_planes++;
  411. } else {
  412. if (fb->format->is_yuv) {
  413. DRM_DEBUG_DRIVER("Plane FB format is YUV\n");
  414. num_yuv_planes++;
  415. }
  416. }
  417. DRM_DEBUG_DRIVER("Plane FB format is %p4cc\n",
  418. &fb->format->format);
  419. if (fb->format->has_alpha || (plane_state->alpha != DRM_BLEND_ALPHA_OPAQUE))
  420. num_alpha_planes++;
  421. DRM_DEBUG_DRIVER("Plane zpos is %d\n",
  422. plane_state->normalized_zpos);
  423. /* Sort our planes by Zpos */
  424. plane_states[plane_state->normalized_zpos] = plane_state;
  425. num_planes++;
  426. }
  427. /* All our planes were disabled, bail out */
  428. if (!num_planes)
  429. return 0;
  430. /*
  431. * The hardware is a bit unusual here.
  432. *
  433. * Even though it supports 4 layers, it does the composition
  434. * in two separate steps.
  435. *
  436. * The first one is assigning a layer to one of its two
  437. * pipes. If more that 1 layer is assigned to the same pipe,
  438. * and if pixels overlaps, the pipe will take the pixel from
  439. * the layer with the highest priority.
  440. *
  441. * The second step is the actual alpha blending, that takes
  442. * the two pipes as input, and uses the potential alpha
  443. * component to do the transparency between the two.
  444. *
  445. * This two-step scenario makes us unable to guarantee a
  446. * robust alpha blending between the 4 layers in all
  447. * situations, since this means that we need to have one layer
  448. * with alpha at the lowest position of our two pipes.
  449. *
  450. * However, we cannot even do that on every platform, since
  451. * the hardware has a bug where the lowest plane of the lowest
  452. * pipe (pipe 0, priority 0), if it has any alpha, will
  453. * discard the pixel data entirely and just display the pixels
  454. * in the background color (black by default).
  455. *
  456. * This means that on the affected platforms, we effectively
  457. * have only three valid configurations with alpha, all of
  458. * them with the alpha being on pipe1 with the lowest
  459. * position, which can be 1, 2 or 3 depending on the number of
  460. * planes and their zpos.
  461. */
  462. /* For platforms that are not affected by the issue described above. */
  463. if (backend->quirks->supports_lowest_plane_alpha)
  464. num_alpha_planes_max++;
  465. if (num_alpha_planes > num_alpha_planes_max) {
  466. DRM_DEBUG_DRIVER("Too many planes with alpha, rejecting...\n");
  467. return -EINVAL;
  468. }
  469. /* We can't have an alpha plane at the lowest position */
  470. if (!backend->quirks->supports_lowest_plane_alpha &&
  471. (plane_states[0]->alpha != DRM_BLEND_ALPHA_OPAQUE))
  472. return -EINVAL;
  473. for (i = 1; i < num_planes; i++) {
  474. struct drm_plane_state *p_state = plane_states[i];
  475. struct drm_framebuffer *fb = p_state->fb;
  476. struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(p_state);
  477. /*
  478. * The only alpha position is the lowest plane of the
  479. * second pipe.
  480. */
  481. if (fb->format->has_alpha || (p_state->alpha != DRM_BLEND_ALPHA_OPAQUE))
  482. current_pipe++;
  483. s_state->pipe = current_pipe;
  484. }
  485. /* We can only have a single YUV plane at a time */
  486. if (num_yuv_planes > SUN4I_BACKEND_NUM_YUV_PLANES) {
  487. DRM_DEBUG_DRIVER("Too many planes with YUV, rejecting...\n");
  488. return -EINVAL;
  489. }
  490. if (num_frontend_planes > SUN4I_BACKEND_NUM_FRONTEND_LAYERS) {
  491. DRM_DEBUG_DRIVER("Too many planes going through the frontend, rejecting\n");
  492. return -EINVAL;
  493. }
  494. DRM_DEBUG_DRIVER("State valid with %u planes, %u alpha, %u video, %u YUV\n",
  495. num_planes, num_alpha_planes, num_frontend_planes,
  496. num_yuv_planes);
  497. return 0;
  498. }
  499. static void sun4i_backend_vblank_quirk(struct sunxi_engine *engine)
  500. {
  501. struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
  502. struct sun4i_frontend *frontend = backend->frontend;
  503. if (!frontend)
  504. return;
  505. /*
  506. * In a teardown scenario with the frontend involved, we have
  507. * to keep the frontend enabled until the next vblank, and
  508. * only then disable it.
  509. *
  510. * This is due to the fact that the backend will not take into
  511. * account the new configuration (with the plane that used to
  512. * be fed by the frontend now disabled) until we write to the
  513. * commit bit and the hardware fetches the new configuration
  514. * during the next vblank.
  515. *
  516. * So we keep the frontend around in order to prevent any
  517. * visual artifacts.
  518. */
  519. spin_lock(&backend->frontend_lock);
  520. if (backend->frontend_teardown) {
  521. sun4i_frontend_exit(frontend);
  522. backend->frontend_teardown = false;
  523. }
  524. spin_unlock(&backend->frontend_lock);
  525. };
  526. static void sun4i_backend_mode_set(struct sunxi_engine *engine,
  527. const struct drm_display_mode *mode)
  528. {
  529. bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  530. DRM_DEBUG_DRIVER("Updating global size W: %u H: %u\n",
  531. mode->hdisplay, mode->vdisplay);
  532. regmap_write(engine->regs, SUN4I_BACKEND_DISSIZE_REG,
  533. SUN4I_BACKEND_DISSIZE(mode->hdisplay, mode->vdisplay));
  534. regmap_update_bits(engine->regs, SUN4I_BACKEND_MODCTL_REG,
  535. SUN4I_BACKEND_MODCTL_ITLMOD_EN,
  536. interlaced ? SUN4I_BACKEND_MODCTL_ITLMOD_EN : 0);
  537. DRM_DEBUG_DRIVER("Switching display backend interlaced mode %s\n",
  538. interlaced ? "on" : "off");
  539. }
  540. static int sun4i_backend_init_sat(struct device *dev) {
  541. struct sun4i_backend *backend = dev_get_drvdata(dev);
  542. int ret;
  543. backend->sat_reset = devm_reset_control_get(dev, "sat");
  544. if (IS_ERR(backend->sat_reset)) {
  545. dev_err(dev, "Couldn't get the SAT reset line\n");
  546. return PTR_ERR(backend->sat_reset);
  547. }
  548. ret = reset_control_deassert(backend->sat_reset);
  549. if (ret) {
  550. dev_err(dev, "Couldn't deassert the SAT reset line\n");
  551. return ret;
  552. }
  553. backend->sat_clk = devm_clk_get(dev, "sat");
  554. if (IS_ERR(backend->sat_clk)) {
  555. dev_err(dev, "Couldn't get our SAT clock\n");
  556. ret = PTR_ERR(backend->sat_clk);
  557. goto err_assert_reset;
  558. }
  559. ret = clk_prepare_enable(backend->sat_clk);
  560. if (ret) {
  561. dev_err(dev, "Couldn't enable the SAT clock\n");
  562. return ret;
  563. }
  564. return 0;
  565. err_assert_reset:
  566. reset_control_assert(backend->sat_reset);
  567. return ret;
  568. }
  569. static int sun4i_backend_free_sat(struct device *dev) {
  570. struct sun4i_backend *backend = dev_get_drvdata(dev);
  571. clk_disable_unprepare(backend->sat_clk);
  572. reset_control_assert(backend->sat_reset);
  573. return 0;
  574. }
  575. /*
  576. * The display backend can take video output from the display frontend, or
  577. * the display enhancement unit on the A80, as input for one it its layers.
  578. * This relationship within the display pipeline is encoded in the device
  579. * tree with of_graph, and we use it here to figure out which backend, if
  580. * there are 2 or more, we are currently probing. The number would be in
  581. * the "reg" property of the upstream output port endpoint.
  582. */
  583. static int sun4i_backend_of_get_id(struct device_node *node)
  584. {
  585. struct device_node *ep, *remote;
  586. struct of_endpoint of_ep;
  587. /* Input port is 0, and we want the first endpoint. */
  588. ep = of_graph_get_endpoint_by_regs(node, 0, -1);
  589. if (!ep)
  590. return -EINVAL;
  591. remote = of_graph_get_remote_endpoint(ep);
  592. of_node_put(ep);
  593. if (!remote)
  594. return -EINVAL;
  595. of_graph_parse_endpoint(remote, &of_ep);
  596. of_node_put(remote);
  597. return of_ep.id;
  598. }
  599. /* TODO: This needs to take multiple pipelines into account */
  600. static struct sun4i_frontend *sun4i_backend_find_frontend(struct sun4i_drv *drv,
  601. struct device_node *node)
  602. {
  603. struct device_node *port, *ep, *remote;
  604. struct sun4i_frontend *frontend;
  605. port = of_graph_get_port_by_id(node, 0);
  606. if (!port)
  607. return ERR_PTR(-EINVAL);
  608. for_each_available_child_of_node(port, ep) {
  609. remote = of_graph_get_remote_port_parent(ep);
  610. if (!remote)
  611. continue;
  612. of_node_put(remote);
  613. /* does this node match any registered engines? */
  614. list_for_each_entry(frontend, &drv->frontend_list, list) {
  615. if (remote == frontend->node) {
  616. of_node_put(port);
  617. of_node_put(ep);
  618. return frontend;
  619. }
  620. }
  621. }
  622. of_node_put(port);
  623. return ERR_PTR(-EINVAL);
  624. }
  625. static const struct sunxi_engine_ops sun4i_backend_engine_ops = {
  626. .atomic_begin = sun4i_backend_atomic_begin,
  627. .atomic_check = sun4i_backend_atomic_check,
  628. .commit = sun4i_backend_commit,
  629. .layers_init = sun4i_layers_init,
  630. .apply_color_correction = sun4i_backend_apply_color_correction,
  631. .disable_color_correction = sun4i_backend_disable_color_correction,
  632. .vblank_quirk = sun4i_backend_vblank_quirk,
  633. .mode_set = sun4i_backend_mode_set,
  634. };
  635. static const struct regmap_config sun4i_backend_regmap_config = {
  636. .reg_bits = 32,
  637. .val_bits = 32,
  638. .reg_stride = 4,
  639. .max_register = 0x5800,
  640. };
  641. static int sun4i_backend_bind(struct device *dev, struct device *master,
  642. void *data)
  643. {
  644. struct platform_device *pdev = to_platform_device(dev);
  645. struct drm_device *drm = data;
  646. struct sun4i_drv *drv = drm->dev_private;
  647. struct sun4i_backend *backend;
  648. const struct sun4i_backend_quirks *quirks;
  649. void __iomem *regs;
  650. int i, ret;
  651. backend = devm_kzalloc(dev, sizeof(*backend), GFP_KERNEL);
  652. if (!backend)
  653. return -ENOMEM;
  654. dev_set_drvdata(dev, backend);
  655. spin_lock_init(&backend->frontend_lock);
  656. if (of_property_present(dev->of_node, "interconnects")) {
  657. /*
  658. * This assume we have the same DMA constraints for all our the
  659. * devices in our pipeline (all the backends, but also the
  660. * frontends). This sounds bad, but it has always been the case
  661. * for us, and DRM doesn't do per-device allocation either, so
  662. * we would need to fix DRM first...
  663. */
  664. ret = of_dma_configure(drm->dev, dev->of_node, true);
  665. if (ret)
  666. return ret;
  667. }
  668. backend->engine.node = dev->of_node;
  669. backend->engine.ops = &sun4i_backend_engine_ops;
  670. backend->engine.id = sun4i_backend_of_get_id(dev->of_node);
  671. if (backend->engine.id < 0)
  672. return backend->engine.id;
  673. backend->frontend = sun4i_backend_find_frontend(drv, dev->of_node);
  674. if (IS_ERR(backend->frontend))
  675. dev_warn(dev, "Couldn't find matching frontend, frontend features disabled\n");
  676. regs = devm_platform_ioremap_resource(pdev, 0);
  677. if (IS_ERR(regs))
  678. return PTR_ERR(regs);
  679. backend->reset = devm_reset_control_get(dev, NULL);
  680. if (IS_ERR(backend->reset)) {
  681. dev_err(dev, "Couldn't get our reset line\n");
  682. return PTR_ERR(backend->reset);
  683. }
  684. ret = reset_control_deassert(backend->reset);
  685. if (ret) {
  686. dev_err(dev, "Couldn't deassert our reset line\n");
  687. return ret;
  688. }
  689. backend->bus_clk = devm_clk_get(dev, "ahb");
  690. if (IS_ERR(backend->bus_clk)) {
  691. dev_err(dev, "Couldn't get the backend bus clock\n");
  692. ret = PTR_ERR(backend->bus_clk);
  693. goto err_assert_reset;
  694. }
  695. clk_prepare_enable(backend->bus_clk);
  696. backend->mod_clk = devm_clk_get(dev, "mod");
  697. if (IS_ERR(backend->mod_clk)) {
  698. dev_err(dev, "Couldn't get the backend module clock\n");
  699. ret = PTR_ERR(backend->mod_clk);
  700. goto err_disable_bus_clk;
  701. }
  702. ret = clk_set_rate_exclusive(backend->mod_clk, 300000000);
  703. if (ret) {
  704. dev_err(dev, "Couldn't set the module clock frequency\n");
  705. goto err_disable_bus_clk;
  706. }
  707. clk_prepare_enable(backend->mod_clk);
  708. backend->ram_clk = devm_clk_get(dev, "ram");
  709. if (IS_ERR(backend->ram_clk)) {
  710. dev_err(dev, "Couldn't get the backend RAM clock\n");
  711. ret = PTR_ERR(backend->ram_clk);
  712. goto err_disable_mod_clk;
  713. }
  714. clk_prepare_enable(backend->ram_clk);
  715. if (of_device_is_compatible(dev->of_node,
  716. "allwinner,sun8i-a33-display-backend")) {
  717. ret = sun4i_backend_init_sat(dev);
  718. if (ret) {
  719. dev_err(dev, "Couldn't init SAT resources\n");
  720. goto err_disable_ram_clk;
  721. }
  722. }
  723. backend->engine.regs = devm_regmap_init_mmio(dev, regs,
  724. &sun4i_backend_regmap_config);
  725. if (IS_ERR(backend->engine.regs)) {
  726. dev_err(dev, "Couldn't create the backend regmap\n");
  727. return PTR_ERR(backend->engine.regs);
  728. }
  729. list_add_tail(&backend->engine.list, &drv->engine_list);
  730. /*
  731. * Many of the backend's layer configuration registers have
  732. * undefined default values. This poses a risk as we use
  733. * regmap_update_bits in some places, and don't overwrite
  734. * the whole register.
  735. *
  736. * Clear the registers here to have something predictable.
  737. */
  738. for (i = 0x800; i < 0x1000; i += 4)
  739. regmap_write(backend->engine.regs, i, 0);
  740. /* Disable registers autoloading */
  741. regmap_write(backend->engine.regs, SUN4I_BACKEND_REGBUFFCTL_REG,
  742. SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS);
  743. /* Enable the backend */
  744. regmap_write(backend->engine.regs, SUN4I_BACKEND_MODCTL_REG,
  745. SUN4I_BACKEND_MODCTL_DEBE_EN |
  746. SUN4I_BACKEND_MODCTL_START_CTL);
  747. /* Set output selection if needed */
  748. quirks = of_device_get_match_data(dev);
  749. if (quirks->needs_output_muxing) {
  750. /*
  751. * We assume there is no dynamic muxing of backends
  752. * and TCONs, so we select the backend with same ID.
  753. *
  754. * While dynamic selection might be interesting, since
  755. * the CRTC is tied to the TCON, while the layers are
  756. * tied to the backends, this means, we will need to
  757. * switch between groups of layers. There might not be
  758. * a way to represent this constraint in DRM.
  759. */
  760. regmap_update_bits(backend->engine.regs,
  761. SUN4I_BACKEND_MODCTL_REG,
  762. SUN4I_BACKEND_MODCTL_OUT_SEL,
  763. (backend->engine.id
  764. ? SUN4I_BACKEND_MODCTL_OUT_LCD1
  765. : SUN4I_BACKEND_MODCTL_OUT_LCD0));
  766. }
  767. backend->quirks = quirks;
  768. return 0;
  769. err_disable_ram_clk:
  770. clk_disable_unprepare(backend->ram_clk);
  771. err_disable_mod_clk:
  772. clk_rate_exclusive_put(backend->mod_clk);
  773. clk_disable_unprepare(backend->mod_clk);
  774. err_disable_bus_clk:
  775. clk_disable_unprepare(backend->bus_clk);
  776. err_assert_reset:
  777. reset_control_assert(backend->reset);
  778. return ret;
  779. }
  780. static void sun4i_backend_unbind(struct device *dev, struct device *master,
  781. void *data)
  782. {
  783. struct sun4i_backend *backend = dev_get_drvdata(dev);
  784. list_del(&backend->engine.list);
  785. if (of_device_is_compatible(dev->of_node,
  786. "allwinner,sun8i-a33-display-backend"))
  787. sun4i_backend_free_sat(dev);
  788. clk_disable_unprepare(backend->ram_clk);
  789. clk_rate_exclusive_put(backend->mod_clk);
  790. clk_disable_unprepare(backend->mod_clk);
  791. clk_disable_unprepare(backend->bus_clk);
  792. reset_control_assert(backend->reset);
  793. }
  794. static const struct component_ops sun4i_backend_ops = {
  795. .bind = sun4i_backend_bind,
  796. .unbind = sun4i_backend_unbind,
  797. };
  798. static int sun4i_backend_probe(struct platform_device *pdev)
  799. {
  800. return component_add(&pdev->dev, &sun4i_backend_ops);
  801. }
  802. static void sun4i_backend_remove(struct platform_device *pdev)
  803. {
  804. component_del(&pdev->dev, &sun4i_backend_ops);
  805. }
  806. static const struct sun4i_backend_quirks sun4i_backend_quirks = {
  807. .needs_output_muxing = true,
  808. };
  809. static const struct sun4i_backend_quirks sun5i_backend_quirks = {
  810. };
  811. static const struct sun4i_backend_quirks sun6i_backend_quirks = {
  812. };
  813. static const struct sun4i_backend_quirks sun7i_backend_quirks = {
  814. .needs_output_muxing = true,
  815. };
  816. static const struct sun4i_backend_quirks sun8i_a33_backend_quirks = {
  817. .supports_lowest_plane_alpha = true,
  818. };
  819. static const struct sun4i_backend_quirks sun9i_backend_quirks = {
  820. };
  821. static const struct of_device_id sun4i_backend_of_table[] = {
  822. {
  823. .compatible = "allwinner,sun4i-a10-display-backend",
  824. .data = &sun4i_backend_quirks,
  825. },
  826. {
  827. .compatible = "allwinner,sun5i-a13-display-backend",
  828. .data = &sun5i_backend_quirks,
  829. },
  830. {
  831. .compatible = "allwinner,sun6i-a31-display-backend",
  832. .data = &sun6i_backend_quirks,
  833. },
  834. {
  835. .compatible = "allwinner,sun7i-a20-display-backend",
  836. .data = &sun7i_backend_quirks,
  837. },
  838. {
  839. .compatible = "allwinner,sun8i-a23-display-backend",
  840. .data = &sun8i_a33_backend_quirks,
  841. },
  842. {
  843. .compatible = "allwinner,sun8i-a33-display-backend",
  844. .data = &sun8i_a33_backend_quirks,
  845. },
  846. {
  847. .compatible = "allwinner,sun9i-a80-display-backend",
  848. .data = &sun9i_backend_quirks,
  849. },
  850. { }
  851. };
  852. MODULE_DEVICE_TABLE(of, sun4i_backend_of_table);
  853. static struct platform_driver sun4i_backend_platform_driver = {
  854. .probe = sun4i_backend_probe,
  855. .remove_new = sun4i_backend_remove,
  856. .driver = {
  857. .name = "sun4i-backend",
  858. .of_match_table = sun4i_backend_of_table,
  859. },
  860. };
  861. module_platform_driver(sun4i_backend_platform_driver);
  862. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  863. MODULE_DESCRIPTION("Allwinner A10 Display Backend Driver");
  864. MODULE_LICENSE("GPL");