1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
5 * Based on sun4i_backend.c, which is:
6 * Copyright (C) 2015 Free Electrons
7 * Copyright (C) 2015 NextThing Co
10 #include <linux/component.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_graph.h>
15 #include <linux/reset.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_plane_helper.h>
22 #include <drm/drm_probe_helper.h>
24 #include "sun4i_drv.h"
25 #include "sun8i_mixer.h"
26 #include "sun8i_ui_layer.h"
27 #include "sun8i_vi_layer.h"
28 #include "sunxi_engine.h"
30 static const struct de2_fmt_info de2_formats[] = {
32 .drm_fmt = DRM_FORMAT_ARGB8888,
33 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB8888,
35 .csc = SUN8I_CSC_MODE_OFF,
38 .drm_fmt = DRM_FORMAT_ABGR8888,
39 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR8888,
41 .csc = SUN8I_CSC_MODE_OFF,
44 .drm_fmt = DRM_FORMAT_RGBA8888,
45 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA8888,
47 .csc = SUN8I_CSC_MODE_OFF,
50 .drm_fmt = DRM_FORMAT_BGRA8888,
51 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA8888,
53 .csc = SUN8I_CSC_MODE_OFF,
56 .drm_fmt = DRM_FORMAT_XRGB8888,
57 .de2_fmt = SUN8I_MIXER_FBFMT_XRGB8888,
59 .csc = SUN8I_CSC_MODE_OFF,
62 .drm_fmt = DRM_FORMAT_XBGR8888,
63 .de2_fmt = SUN8I_MIXER_FBFMT_XBGR8888,
65 .csc = SUN8I_CSC_MODE_OFF,
68 .drm_fmt = DRM_FORMAT_RGBX8888,
69 .de2_fmt = SUN8I_MIXER_FBFMT_RGBX8888,
71 .csc = SUN8I_CSC_MODE_OFF,
74 .drm_fmt = DRM_FORMAT_BGRX8888,
75 .de2_fmt = SUN8I_MIXER_FBFMT_BGRX8888,
77 .csc = SUN8I_CSC_MODE_OFF,
80 .drm_fmt = DRM_FORMAT_RGB888,
81 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
83 .csc = SUN8I_CSC_MODE_OFF,
86 .drm_fmt = DRM_FORMAT_BGR888,
87 .de2_fmt = SUN8I_MIXER_FBFMT_BGR888,
89 .csc = SUN8I_CSC_MODE_OFF,
92 .drm_fmt = DRM_FORMAT_RGB565,
93 .de2_fmt = SUN8I_MIXER_FBFMT_RGB565,
95 .csc = SUN8I_CSC_MODE_OFF,
98 .drm_fmt = DRM_FORMAT_BGR565,
99 .de2_fmt = SUN8I_MIXER_FBFMT_BGR565,
101 .csc = SUN8I_CSC_MODE_OFF,
104 .drm_fmt = DRM_FORMAT_ARGB4444,
105 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB4444,
107 .csc = SUN8I_CSC_MODE_OFF,
110 .drm_fmt = DRM_FORMAT_ABGR4444,
111 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR4444,
113 .csc = SUN8I_CSC_MODE_OFF,
116 .drm_fmt = DRM_FORMAT_RGBA4444,
117 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA4444,
119 .csc = SUN8I_CSC_MODE_OFF,
122 .drm_fmt = DRM_FORMAT_BGRA4444,
123 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA4444,
125 .csc = SUN8I_CSC_MODE_OFF,
128 .drm_fmt = DRM_FORMAT_ARGB1555,
129 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB1555,
131 .csc = SUN8I_CSC_MODE_OFF,
134 .drm_fmt = DRM_FORMAT_ABGR1555,
135 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR1555,
137 .csc = SUN8I_CSC_MODE_OFF,
140 .drm_fmt = DRM_FORMAT_RGBA5551,
141 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA5551,
143 .csc = SUN8I_CSC_MODE_OFF,
146 .drm_fmt = DRM_FORMAT_BGRA5551,
147 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA5551,
149 .csc = SUN8I_CSC_MODE_OFF,
152 .drm_fmt = DRM_FORMAT_UYVY,
153 .de2_fmt = SUN8I_MIXER_FBFMT_UYVY,
155 .csc = SUN8I_CSC_MODE_YUV2RGB,
158 .drm_fmt = DRM_FORMAT_VYUY,
159 .de2_fmt = SUN8I_MIXER_FBFMT_VYUY,
161 .csc = SUN8I_CSC_MODE_YUV2RGB,
164 .drm_fmt = DRM_FORMAT_YUYV,
165 .de2_fmt = SUN8I_MIXER_FBFMT_YUYV,
167 .csc = SUN8I_CSC_MODE_YUV2RGB,
170 .drm_fmt = DRM_FORMAT_YVYU,
171 .de2_fmt = SUN8I_MIXER_FBFMT_YVYU,
173 .csc = SUN8I_CSC_MODE_YUV2RGB,
176 .drm_fmt = DRM_FORMAT_NV16,
177 .de2_fmt = SUN8I_MIXER_FBFMT_NV16,
179 .csc = SUN8I_CSC_MODE_YUV2RGB,
182 .drm_fmt = DRM_FORMAT_NV61,
183 .de2_fmt = SUN8I_MIXER_FBFMT_NV61,
185 .csc = SUN8I_CSC_MODE_YUV2RGB,
188 .drm_fmt = DRM_FORMAT_NV12,
189 .de2_fmt = SUN8I_MIXER_FBFMT_NV12,
191 .csc = SUN8I_CSC_MODE_YUV2RGB,
194 .drm_fmt = DRM_FORMAT_NV21,
195 .de2_fmt = SUN8I_MIXER_FBFMT_NV21,
197 .csc = SUN8I_CSC_MODE_YUV2RGB,
200 .drm_fmt = DRM_FORMAT_YUV444,
201 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
203 .csc = SUN8I_CSC_MODE_YUV2RGB,
206 .drm_fmt = DRM_FORMAT_YUV422,
207 .de2_fmt = SUN8I_MIXER_FBFMT_YUV422,
209 .csc = SUN8I_CSC_MODE_YUV2RGB,
212 .drm_fmt = DRM_FORMAT_YUV420,
213 .de2_fmt = SUN8I_MIXER_FBFMT_YUV420,
215 .csc = SUN8I_CSC_MODE_YUV2RGB,
218 .drm_fmt = DRM_FORMAT_YUV411,
219 .de2_fmt = SUN8I_MIXER_FBFMT_YUV411,
221 .csc = SUN8I_CSC_MODE_YUV2RGB,
224 .drm_fmt = DRM_FORMAT_YVU444,
225 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
227 .csc = SUN8I_CSC_MODE_YVU2RGB,
230 .drm_fmt = DRM_FORMAT_YVU422,
231 .de2_fmt = SUN8I_MIXER_FBFMT_YUV422,
233 .csc = SUN8I_CSC_MODE_YVU2RGB,
236 .drm_fmt = DRM_FORMAT_YVU420,
237 .de2_fmt = SUN8I_MIXER_FBFMT_YUV420,
239 .csc = SUN8I_CSC_MODE_YVU2RGB,
242 .drm_fmt = DRM_FORMAT_YVU411,
243 .de2_fmt = SUN8I_MIXER_FBFMT_YUV411,
245 .csc = SUN8I_CSC_MODE_YVU2RGB,
249 const struct de2_fmt_info *sun8i_mixer_format_info(u32 format)
253 for (i = 0; i < ARRAY_SIZE(de2_formats); ++i)
254 if (de2_formats[i].drm_fmt == format)
255 return &de2_formats[i];
260 static void sun8i_mixer_commit(struct sunxi_engine *engine)
262 DRM_DEBUG_DRIVER("Committing changes\n");
264 regmap_write(engine->regs, SUN8I_MIXER_GLOBAL_DBUFF,
265 SUN8I_MIXER_GLOBAL_DBUFF_ENABLE);
268 static struct drm_plane **sun8i_layers_init(struct drm_device *drm,
269 struct sunxi_engine *engine)
271 struct drm_plane **planes;
272 struct sun8i_mixer *mixer = engine_to_sun8i_mixer(engine);
275 planes = devm_kcalloc(drm->dev,
276 mixer->cfg->vi_num + mixer->cfg->ui_num + 1,
277 sizeof(*planes), GFP_KERNEL);
279 return ERR_PTR(-ENOMEM);
281 for (i = 0; i < mixer->cfg->vi_num; i++) {
282 struct sun8i_vi_layer *layer;
284 layer = sun8i_vi_layer_init_one(drm, mixer, i);
287 "Couldn't initialize overlay plane\n");
288 return ERR_CAST(layer);
291 planes[i] = &layer->plane;
294 for (i = 0; i < mixer->cfg->ui_num; i++) {
295 struct sun8i_ui_layer *layer;
297 layer = sun8i_ui_layer_init_one(drm, mixer, i);
299 dev_err(drm->dev, "Couldn't initialize %s plane\n",
300 i ? "overlay" : "primary");
301 return ERR_CAST(layer);
304 planes[mixer->cfg->vi_num + i] = &layer->plane;
310 static const struct sunxi_engine_ops sun8i_engine_ops = {
311 .commit = sun8i_mixer_commit,
312 .layers_init = sun8i_layers_init,
315 static struct regmap_config sun8i_mixer_regmap_config = {
319 .max_register = 0xbfffc, /* guessed */
322 static int sun8i_mixer_of_get_id(struct device_node *node)
324 struct device_node *ep, *remote;
325 struct of_endpoint of_ep;
327 /* Output port is 1, and we want the first endpoint. */
328 ep = of_graph_get_endpoint_by_regs(node, 1, -1);
332 remote = of_graph_get_remote_endpoint(ep);
337 of_graph_parse_endpoint(remote, &of_ep);
342 static int sun8i_mixer_bind(struct device *dev, struct device *master,
345 struct platform_device *pdev = to_platform_device(dev);
346 struct drm_device *drm = data;
347 struct sun4i_drv *drv = drm->dev_private;
348 struct sun8i_mixer *mixer;
349 struct resource *res;
356 * The mixer uses single 32-bit register to store memory
357 * addresses, so that it cannot deal with 64-bit memory
359 * Restrict the DMA mask so that the mixer won't be
360 * allocated some memory that is too high.
362 ret = dma_set_mask(dev, DMA_BIT_MASK(32));
364 dev_err(dev, "Cannot do 32-bit DMA.\n");
368 mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
371 dev_set_drvdata(dev, mixer);
372 mixer->engine.ops = &sun8i_engine_ops;
373 mixer->engine.node = dev->of_node;
376 * While this function can fail, we shouldn't do anything
377 * if this happens. Some early DE2 DT entries don't provide
378 * mixer id but work nevertheless because matching between
379 * TCON and mixer is done by comparing node pointers (old
380 * way) instead comparing ids. If this function fails and
381 * id is needed, it will fail during id matching anyway.
383 mixer->engine.id = sun8i_mixer_of_get_id(dev->of_node);
385 mixer->cfg = of_device_get_match_data(dev);
389 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
390 regs = devm_ioremap_resource(dev, res);
392 return PTR_ERR(regs);
394 mixer->engine.regs = devm_regmap_init_mmio(dev, regs,
395 &sun8i_mixer_regmap_config);
396 if (IS_ERR(mixer->engine.regs)) {
397 dev_err(dev, "Couldn't create the mixer regmap\n");
398 return PTR_ERR(mixer->engine.regs);
401 mixer->reset = devm_reset_control_get(dev, NULL);
402 if (IS_ERR(mixer->reset)) {
403 dev_err(dev, "Couldn't get our reset line\n");
404 return PTR_ERR(mixer->reset);
407 ret = reset_control_deassert(mixer->reset);
409 dev_err(dev, "Couldn't deassert our reset line\n");
413 mixer->bus_clk = devm_clk_get(dev, "bus");
414 if (IS_ERR(mixer->bus_clk)) {
415 dev_err(dev, "Couldn't get the mixer bus clock\n");
416 ret = PTR_ERR(mixer->bus_clk);
417 goto err_assert_reset;
419 clk_prepare_enable(mixer->bus_clk);
421 mixer->mod_clk = devm_clk_get(dev, "mod");
422 if (IS_ERR(mixer->mod_clk)) {
423 dev_err(dev, "Couldn't get the mixer module clock\n");
424 ret = PTR_ERR(mixer->mod_clk);
425 goto err_disable_bus_clk;
429 * It seems that we need to enforce that rate for whatever
430 * reason for the mixer to be functional. Make sure it's the
433 if (mixer->cfg->mod_rate)
434 clk_set_rate(mixer->mod_clk, mixer->cfg->mod_rate);
436 clk_prepare_enable(mixer->mod_clk);
438 list_add_tail(&mixer->engine.list, &drv->engine_list);
440 base = sun8i_blender_base(mixer);
442 /* Reset registers and disable unused sub-engines */
443 if (mixer->cfg->is_de3) {
444 for (i = 0; i < DE3_MIXER_UNIT_SIZE; i += 4)
445 regmap_write(mixer->engine.regs, i, 0);
447 regmap_write(mixer->engine.regs, SUN50I_MIXER_FCE_EN, 0);
448 regmap_write(mixer->engine.regs, SUN50I_MIXER_PEAK_EN, 0);
449 regmap_write(mixer->engine.regs, SUN50I_MIXER_LCTI_EN, 0);
450 regmap_write(mixer->engine.regs, SUN50I_MIXER_BLS_EN, 0);
451 regmap_write(mixer->engine.regs, SUN50I_MIXER_FCC_EN, 0);
452 regmap_write(mixer->engine.regs, SUN50I_MIXER_DNS_EN, 0);
453 regmap_write(mixer->engine.regs, SUN50I_MIXER_DRC_EN, 0);
454 regmap_write(mixer->engine.regs, SUN50I_MIXER_FMT_EN, 0);
455 regmap_write(mixer->engine.regs, SUN50I_MIXER_CDC0_EN, 0);
456 regmap_write(mixer->engine.regs, SUN50I_MIXER_CDC1_EN, 0);
458 for (i = 0; i < DE2_MIXER_UNIT_SIZE; i += 4)
459 regmap_write(mixer->engine.regs, i, 0);
461 regmap_write(mixer->engine.regs, SUN8I_MIXER_FCE_EN, 0);
462 regmap_write(mixer->engine.regs, SUN8I_MIXER_BWS_EN, 0);
463 regmap_write(mixer->engine.regs, SUN8I_MIXER_LTI_EN, 0);
464 regmap_write(mixer->engine.regs, SUN8I_MIXER_PEAK_EN, 0);
465 regmap_write(mixer->engine.regs, SUN8I_MIXER_ASE_EN, 0);
466 regmap_write(mixer->engine.regs, SUN8I_MIXER_FCC_EN, 0);
467 regmap_write(mixer->engine.regs, SUN8I_MIXER_DCSC_EN, 0);
470 /* Enable the mixer */
471 regmap_write(mixer->engine.regs, SUN8I_MIXER_GLOBAL_CTL,
472 SUN8I_MIXER_GLOBAL_CTL_RT_EN);
474 /* Set background color to black */
475 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_BKCOLOR(base),
476 SUN8I_MIXER_BLEND_COLOR_BLACK);
479 * Set fill color of bottom plane to black. Generally not needed
480 * except when VI plane is at bottom (zpos = 0) and enabled.
482 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_PIPE_CTL(base),
483 SUN8I_MIXER_BLEND_PIPE_CTL_FC_EN(0));
484 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_ATTR_FCOLOR(base, 0),
485 SUN8I_MIXER_BLEND_COLOR_BLACK);
487 plane_cnt = mixer->cfg->vi_num + mixer->cfg->ui_num;
488 for (i = 0; i < plane_cnt; i++)
489 regmap_write(mixer->engine.regs,
490 SUN8I_MIXER_BLEND_MODE(base, i),
491 SUN8I_MIXER_BLEND_MODE_DEF);
493 regmap_update_bits(mixer->engine.regs, SUN8I_MIXER_BLEND_PIPE_CTL(base),
494 SUN8I_MIXER_BLEND_PIPE_CTL_EN_MSK, 0);
499 clk_disable_unprepare(mixer->bus_clk);
501 reset_control_assert(mixer->reset);
505 static void sun8i_mixer_unbind(struct device *dev, struct device *master,
508 struct sun8i_mixer *mixer = dev_get_drvdata(dev);
510 list_del(&mixer->engine.list);
512 clk_disable_unprepare(mixer->mod_clk);
513 clk_disable_unprepare(mixer->bus_clk);
514 reset_control_assert(mixer->reset);
517 static const struct component_ops sun8i_mixer_ops = {
518 .bind = sun8i_mixer_bind,
519 .unbind = sun8i_mixer_unbind,
522 static int sun8i_mixer_probe(struct platform_device *pdev)
524 return component_add(&pdev->dev, &sun8i_mixer_ops);
527 static int sun8i_mixer_remove(struct platform_device *pdev)
529 component_del(&pdev->dev, &sun8i_mixer_ops);
534 static const struct sun8i_mixer_cfg sun8i_a83t_mixer0_cfg = {
537 .scanline_yuv = 2048,
542 static const struct sun8i_mixer_cfg sun8i_a83t_mixer1_cfg = {
545 .scanline_yuv = 2048,
550 static const struct sun8i_mixer_cfg sun8i_h3_mixer0_cfg = {
552 .mod_rate = 432000000,
554 .scanline_yuv = 2048,
559 static const struct sun8i_mixer_cfg sun8i_r40_mixer0_cfg = {
561 .mod_rate = 297000000,
563 .scanline_yuv = 2048,
568 static const struct sun8i_mixer_cfg sun8i_r40_mixer1_cfg = {
570 .mod_rate = 297000000,
572 .scanline_yuv = 2048,
577 static const struct sun8i_mixer_cfg sun8i_v3s_mixer_cfg = {
581 .scanline_yuv = 2048,
583 .mod_rate = 150000000,
586 static const struct sun8i_mixer_cfg sun50i_a64_mixer0_cfg = {
588 .mod_rate = 297000000,
590 .scanline_yuv = 4096,
595 static const struct sun8i_mixer_cfg sun50i_a64_mixer1_cfg = {
597 .mod_rate = 297000000,
599 .scanline_yuv = 2048,
604 static const struct sun8i_mixer_cfg sun50i_h6_mixer0_cfg = {
607 .mod_rate = 600000000,
609 .scanline_yuv = 4096,
614 static const struct of_device_id sun8i_mixer_of_table[] = {
616 .compatible = "allwinner,sun8i-a83t-de2-mixer-0",
617 .data = &sun8i_a83t_mixer0_cfg,
620 .compatible = "allwinner,sun8i-a83t-de2-mixer-1",
621 .data = &sun8i_a83t_mixer1_cfg,
624 .compatible = "allwinner,sun8i-h3-de2-mixer-0",
625 .data = &sun8i_h3_mixer0_cfg,
628 .compatible = "allwinner,sun8i-r40-de2-mixer-0",
629 .data = &sun8i_r40_mixer0_cfg,
632 .compatible = "allwinner,sun8i-r40-de2-mixer-1",
633 .data = &sun8i_r40_mixer1_cfg,
636 .compatible = "allwinner,sun8i-v3s-de2-mixer",
637 .data = &sun8i_v3s_mixer_cfg,
640 .compatible = "allwinner,sun50i-a64-de2-mixer-0",
641 .data = &sun50i_a64_mixer0_cfg,
644 .compatible = "allwinner,sun50i-a64-de2-mixer-1",
645 .data = &sun50i_a64_mixer1_cfg,
648 .compatible = "allwinner,sun50i-h6-de3-mixer-0",
649 .data = &sun50i_h6_mixer0_cfg,
653 MODULE_DEVICE_TABLE(of, sun8i_mixer_of_table);
655 static struct platform_driver sun8i_mixer_platform_driver = {
656 .probe = sun8i_mixer_probe,
657 .remove = sun8i_mixer_remove,
659 .name = "sun8i-mixer",
660 .of_match_table = sun8i_mixer_of_table,
663 module_platform_driver(sun8i_mixer_platform_driver);
665 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
666 MODULE_DESCRIPTION("Allwinner DE2 Mixer driver");
667 MODULE_LICENSE("GPL");