sun8i_mixer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
  3. *
  4. * Based on sun4i_backend.c, which is:
  5. * Copyright (C) 2015 Free Electrons
  6. * Copyright (C) 2015 NextThing Co
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <linux/component.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/reset.h>
  23. #include <linux/of_device.h>
  24. #include "sun4i_drv.h"
  25. #include "sun8i_mixer.h"
  26. #include "sun8i_layer.h"
  27. #include "sunxi_engine.h"
  28. static void sun8i_mixer_commit(struct sunxi_engine *engine)
  29. {
  30. DRM_DEBUG_DRIVER("Committing changes\n");
  31. regmap_write(engine->regs, SUN8I_MIXER_GLOBAL_DBUFF,
  32. SUN8I_MIXER_GLOBAL_DBUFF_ENABLE);
  33. }
  34. void sun8i_mixer_layer_enable(struct sun8i_mixer *mixer,
  35. int layer, bool enable)
  36. {
  37. u32 val;
  38. /* Currently the first UI channel is used */
  39. int chan = mixer->cfg->vi_num;
  40. DRM_DEBUG_DRIVER("Enabling layer %d in channel %d\n", layer, chan);
  41. if (enable)
  42. val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN;
  43. else
  44. val = 0;
  45. regmap_update_bits(mixer->engine.regs,
  46. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(chan, layer),
  47. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN, val);
  48. /* Set the alpha configuration */
  49. regmap_update_bits(mixer->engine.regs,
  50. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(chan, layer),
  51. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_MASK,
  52. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_DEF);
  53. regmap_update_bits(mixer->engine.regs,
  54. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(chan, layer),
  55. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MASK,
  56. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_DEF);
  57. }
  58. static int sun8i_mixer_drm_format_to_layer(struct drm_plane *plane,
  59. u32 format, u32 *mode)
  60. {
  61. switch (format) {
  62. case DRM_FORMAT_ARGB8888:
  63. *mode = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_ARGB8888;
  64. break;
  65. case DRM_FORMAT_XRGB8888:
  66. *mode = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_XRGB8888;
  67. break;
  68. case DRM_FORMAT_RGB888:
  69. *mode = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_RGB888;
  70. break;
  71. default:
  72. return -EINVAL;
  73. }
  74. return 0;
  75. }
  76. int sun8i_mixer_update_layer_coord(struct sun8i_mixer *mixer,
  77. int layer, struct drm_plane *plane)
  78. {
  79. struct drm_plane_state *state = plane->state;
  80. struct drm_framebuffer *fb = state->fb;
  81. /* Currently the first UI channel is used */
  82. int chan = mixer->cfg->vi_num;
  83. DRM_DEBUG_DRIVER("Updating layer %d\n", layer);
  84. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  85. DRM_DEBUG_DRIVER("Primary layer, updating global size W: %u H: %u\n",
  86. state->crtc_w, state->crtc_h);
  87. regmap_write(mixer->engine.regs, SUN8I_MIXER_GLOBAL_SIZE,
  88. SUN8I_MIXER_SIZE(state->crtc_w,
  89. state->crtc_h));
  90. DRM_DEBUG_DRIVER("Updating blender size\n");
  91. regmap_write(mixer->engine.regs,
  92. SUN8I_MIXER_BLEND_ATTR_INSIZE(0),
  93. SUN8I_MIXER_SIZE(state->crtc_w,
  94. state->crtc_h));
  95. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_OUTSIZE,
  96. SUN8I_MIXER_SIZE(state->crtc_w,
  97. state->crtc_h));
  98. DRM_DEBUG_DRIVER("Updating channel size\n");
  99. regmap_write(mixer->engine.regs,
  100. SUN8I_MIXER_CHAN_UI_OVL_SIZE(chan),
  101. SUN8I_MIXER_SIZE(state->crtc_w,
  102. state->crtc_h));
  103. }
  104. /* Set the line width */
  105. DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
  106. regmap_write(mixer->engine.regs,
  107. SUN8I_MIXER_CHAN_UI_LAYER_PITCH(chan, layer),
  108. fb->pitches[0]);
  109. /* Set height and width */
  110. DRM_DEBUG_DRIVER("Layer size W: %u H: %u\n",
  111. state->crtc_w, state->crtc_h);
  112. regmap_write(mixer->engine.regs,
  113. SUN8I_MIXER_CHAN_UI_LAYER_SIZE(chan, layer),
  114. SUN8I_MIXER_SIZE(state->crtc_w, state->crtc_h));
  115. /* Set base coordinates */
  116. DRM_DEBUG_DRIVER("Layer coordinates X: %d Y: %d\n",
  117. state->crtc_x, state->crtc_y);
  118. regmap_write(mixer->engine.regs,
  119. SUN8I_MIXER_CHAN_UI_LAYER_COORD(chan, layer),
  120. SUN8I_MIXER_COORD(state->crtc_x, state->crtc_y));
  121. return 0;
  122. }
  123. int sun8i_mixer_update_layer_formats(struct sun8i_mixer *mixer,
  124. int layer, struct drm_plane *plane)
  125. {
  126. struct drm_plane_state *state = plane->state;
  127. struct drm_framebuffer *fb = state->fb;
  128. bool interlaced = false;
  129. u32 val;
  130. /* Currently the first UI channel is used */
  131. int chan = mixer->cfg->vi_num;
  132. int ret;
  133. if (plane->state->crtc)
  134. interlaced = plane->state->crtc->state->adjusted_mode.flags
  135. & DRM_MODE_FLAG_INTERLACE;
  136. regmap_update_bits(mixer->engine.regs, SUN8I_MIXER_BLEND_OUTCTL,
  137. SUN8I_MIXER_BLEND_OUTCTL_INTERLACED,
  138. interlaced ?
  139. SUN8I_MIXER_BLEND_OUTCTL_INTERLACED : 0);
  140. DRM_DEBUG_DRIVER("Switching display mixer interlaced mode %s\n",
  141. interlaced ? "on" : "off");
  142. ret = sun8i_mixer_drm_format_to_layer(plane, fb->format->format,
  143. &val);
  144. if (ret) {
  145. DRM_DEBUG_DRIVER("Invalid format\n");
  146. return ret;
  147. }
  148. regmap_update_bits(mixer->engine.regs,
  149. SUN8I_MIXER_CHAN_UI_LAYER_ATTR(chan, layer),
  150. SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_MASK, val);
  151. return 0;
  152. }
  153. int sun8i_mixer_update_layer_buffer(struct sun8i_mixer *mixer,
  154. int layer, struct drm_plane *plane)
  155. {
  156. struct drm_plane_state *state = plane->state;
  157. struct drm_framebuffer *fb = state->fb;
  158. struct drm_gem_cma_object *gem;
  159. dma_addr_t paddr;
  160. /* Currently the first UI channel is used */
  161. int chan = mixer->cfg->vi_num;
  162. int bpp;
  163. /* Get the physical address of the buffer in memory */
  164. gem = drm_fb_cma_get_gem_obj(fb, 0);
  165. DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
  166. /* Compute the start of the displayed memory */
  167. bpp = fb->format->cpp[0];
  168. paddr = gem->paddr + fb->offsets[0];
  169. /* Fixup framebuffer address for src coordinates */
  170. paddr += (state->src_x >> 16) * bpp;
  171. paddr += (state->src_y >> 16) * fb->pitches[0];
  172. /*
  173. * The hardware cannot correctly deal with negative crtc
  174. * coordinates, the display is cropped to the requested size,
  175. * but the display content is not moved.
  176. * Manually move the display content by fixup the framebuffer
  177. * address when crtc_x or crtc_y is negative, like what we
  178. * have did for src_x and src_y.
  179. */
  180. if (state->crtc_x < 0)
  181. paddr += -state->crtc_x * bpp;
  182. if (state->crtc_y < 0)
  183. paddr += -state->crtc_y * fb->pitches[0];
  184. DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &paddr);
  185. regmap_write(mixer->engine.regs,
  186. SUN8I_MIXER_CHAN_UI_LAYER_TOP_LADDR(chan, layer),
  187. lower_32_bits(paddr));
  188. return 0;
  189. }
  190. static const struct sunxi_engine_ops sun8i_engine_ops = {
  191. .commit = sun8i_mixer_commit,
  192. .layers_init = sun8i_layers_init,
  193. };
  194. static struct regmap_config sun8i_mixer_regmap_config = {
  195. .reg_bits = 32,
  196. .val_bits = 32,
  197. .reg_stride = 4,
  198. .max_register = 0xbfffc, /* guessed */
  199. };
  200. static int sun8i_mixer_bind(struct device *dev, struct device *master,
  201. void *data)
  202. {
  203. struct platform_device *pdev = to_platform_device(dev);
  204. struct drm_device *drm = data;
  205. struct sun4i_drv *drv = drm->dev_private;
  206. struct sun8i_mixer *mixer;
  207. struct resource *res;
  208. void __iomem *regs;
  209. int i, ret;
  210. /*
  211. * The mixer uses single 32-bit register to store memory
  212. * addresses, so that it cannot deal with 64-bit memory
  213. * addresses.
  214. * Restrict the DMA mask so that the mixer won't be
  215. * allocated some memory that is too high.
  216. */
  217. ret = dma_set_mask(dev, DMA_BIT_MASK(32));
  218. if (ret) {
  219. dev_err(dev, "Cannot do 32-bit DMA.\n");
  220. return ret;
  221. }
  222. mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
  223. if (!mixer)
  224. return -ENOMEM;
  225. dev_set_drvdata(dev, mixer);
  226. mixer->engine.ops = &sun8i_engine_ops;
  227. mixer->engine.node = dev->of_node;
  228. /* The ID of the mixer currently doesn't matter */
  229. mixer->engine.id = -1;
  230. mixer->cfg = of_device_get_match_data(dev);
  231. if (!mixer->cfg)
  232. return -EINVAL;
  233. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  234. regs = devm_ioremap_resource(dev, res);
  235. if (IS_ERR(regs))
  236. return PTR_ERR(regs);
  237. mixer->engine.regs = devm_regmap_init_mmio(dev, regs,
  238. &sun8i_mixer_regmap_config);
  239. if (IS_ERR(mixer->engine.regs)) {
  240. dev_err(dev, "Couldn't create the mixer regmap\n");
  241. return PTR_ERR(mixer->engine.regs);
  242. }
  243. mixer->reset = devm_reset_control_get(dev, NULL);
  244. if (IS_ERR(mixer->reset)) {
  245. dev_err(dev, "Couldn't get our reset line\n");
  246. return PTR_ERR(mixer->reset);
  247. }
  248. ret = reset_control_deassert(mixer->reset);
  249. if (ret) {
  250. dev_err(dev, "Couldn't deassert our reset line\n");
  251. return ret;
  252. }
  253. mixer->bus_clk = devm_clk_get(dev, "bus");
  254. if (IS_ERR(mixer->bus_clk)) {
  255. dev_err(dev, "Couldn't get the mixer bus clock\n");
  256. ret = PTR_ERR(mixer->bus_clk);
  257. goto err_assert_reset;
  258. }
  259. clk_prepare_enable(mixer->bus_clk);
  260. mixer->mod_clk = devm_clk_get(dev, "mod");
  261. if (IS_ERR(mixer->mod_clk)) {
  262. dev_err(dev, "Couldn't get the mixer module clock\n");
  263. ret = PTR_ERR(mixer->mod_clk);
  264. goto err_disable_bus_clk;
  265. }
  266. clk_prepare_enable(mixer->mod_clk);
  267. list_add_tail(&mixer->engine.list, &drv->engine_list);
  268. /* Reset the registers */
  269. for (i = 0x0; i < 0x20000; i += 4)
  270. regmap_write(mixer->engine.regs, i, 0);
  271. /* Enable the mixer */
  272. regmap_write(mixer->engine.regs, SUN8I_MIXER_GLOBAL_CTL,
  273. SUN8I_MIXER_GLOBAL_CTL_RT_EN);
  274. /* Initialize blender */
  275. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_FCOLOR_CTL,
  276. SUN8I_MIXER_BLEND_FCOLOR_CTL_DEF);
  277. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_PREMULTIPLY,
  278. SUN8I_MIXER_BLEND_PREMULTIPLY_DEF);
  279. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_BKCOLOR,
  280. SUN8I_MIXER_BLEND_BKCOLOR_DEF);
  281. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_MODE(0),
  282. SUN8I_MIXER_BLEND_MODE_DEF);
  283. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_CK_CTL,
  284. SUN8I_MIXER_BLEND_CK_CTL_DEF);
  285. regmap_write(mixer->engine.regs,
  286. SUN8I_MIXER_BLEND_ATTR_FCOLOR(0),
  287. SUN8I_MIXER_BLEND_ATTR_FCOLOR_DEF);
  288. /* Select the first UI channel */
  289. DRM_DEBUG_DRIVER("Selecting channel %d (first UI channel)\n",
  290. mixer->cfg->vi_num);
  291. regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_ROUTE,
  292. mixer->cfg->vi_num);
  293. return 0;
  294. err_disable_bus_clk:
  295. clk_disable_unprepare(mixer->bus_clk);
  296. err_assert_reset:
  297. reset_control_assert(mixer->reset);
  298. return ret;
  299. }
  300. static void sun8i_mixer_unbind(struct device *dev, struct device *master,
  301. void *data)
  302. {
  303. struct sun8i_mixer *mixer = dev_get_drvdata(dev);
  304. list_del(&mixer->engine.list);
  305. clk_disable_unprepare(mixer->mod_clk);
  306. clk_disable_unprepare(mixer->bus_clk);
  307. reset_control_assert(mixer->reset);
  308. }
  309. static const struct component_ops sun8i_mixer_ops = {
  310. .bind = sun8i_mixer_bind,
  311. .unbind = sun8i_mixer_unbind,
  312. };
  313. static int sun8i_mixer_probe(struct platform_device *pdev)
  314. {
  315. return component_add(&pdev->dev, &sun8i_mixer_ops);
  316. }
  317. static int sun8i_mixer_remove(struct platform_device *pdev)
  318. {
  319. component_del(&pdev->dev, &sun8i_mixer_ops);
  320. return 0;
  321. }
  322. static const struct sun8i_mixer_cfg sun8i_v3s_mixer_cfg = {
  323. .vi_num = 2,
  324. .ui_num = 1,
  325. };
  326. static const struct of_device_id sun8i_mixer_of_table[] = {
  327. {
  328. .compatible = "allwinner,sun8i-v3s-de2-mixer",
  329. .data = &sun8i_v3s_mixer_cfg,
  330. },
  331. { }
  332. };
  333. MODULE_DEVICE_TABLE(of, sun8i_mixer_of_table);
  334. static struct platform_driver sun8i_mixer_platform_driver = {
  335. .probe = sun8i_mixer_probe,
  336. .remove = sun8i_mixer_remove,
  337. .driver = {
  338. .name = "sun8i-mixer",
  339. .of_match_table = sun8i_mixer_of_table,
  340. },
  341. };
  342. module_platform_driver(sun8i_mixer_platform_driver);
  343. MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
  344. MODULE_DESCRIPTION("Allwinner DE2 Mixer driver");
  345. MODULE_LICENSE("GPL");