4 * Based on sun4i_backend.c, which is:
5 * Copyright (C) 2015 Free Electrons
6 * Copyright (C) 2015 NextThing Co
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.
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_plane_helper.h>
22 #include <linux/component.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/reset.h>
25 #include <linux/of_device.h>
27 #include "sun4i_drv.h"
28 #include "sun8i_mixer.h"
29 #include "sun8i_ui_layer.h"
30 #include "sun8i_vi_layer.h"
31 #include "sunxi_engine.h"
33 static const struct de2_fmt_info de2_formats[] = {
35 .drm_fmt = DRM_FORMAT_ARGB8888,
36 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB8888,
38 .csc = SUN8I_CSC_MODE_OFF,
41 .drm_fmt = DRM_FORMAT_ABGR8888,
42 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR8888,
44 .csc = SUN8I_CSC_MODE_OFF,
47 .drm_fmt = DRM_FORMAT_RGBA8888,
48 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA8888,
50 .csc = SUN8I_CSC_MODE_OFF,
53 .drm_fmt = DRM_FORMAT_BGRA8888,
54 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA8888,
56 .csc = SUN8I_CSC_MODE_OFF,
59 .drm_fmt = DRM_FORMAT_XRGB8888,
60 .de2_fmt = SUN8I_MIXER_FBFMT_XRGB8888,
62 .csc = SUN8I_CSC_MODE_OFF,
65 .drm_fmt = DRM_FORMAT_XBGR8888,
66 .de2_fmt = SUN8I_MIXER_FBFMT_XBGR8888,
68 .csc = SUN8I_CSC_MODE_OFF,
71 .drm_fmt = DRM_FORMAT_RGBX8888,
72 .de2_fmt = SUN8I_MIXER_FBFMT_RGBX8888,
74 .csc = SUN8I_CSC_MODE_OFF,
77 .drm_fmt = DRM_FORMAT_BGRX8888,
78 .de2_fmt = SUN8I_MIXER_FBFMT_BGRX8888,
80 .csc = SUN8I_CSC_MODE_OFF,
83 .drm_fmt = DRM_FORMAT_RGB888,
84 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
86 .csc = SUN8I_CSC_MODE_OFF,
89 .drm_fmt = DRM_FORMAT_BGR888,
90 .de2_fmt = SUN8I_MIXER_FBFMT_BGR888,
92 .csc = SUN8I_CSC_MODE_OFF,
95 .drm_fmt = DRM_FORMAT_RGB565,
96 .de2_fmt = SUN8I_MIXER_FBFMT_RGB565,
98 .csc = SUN8I_CSC_MODE_OFF,
101 .drm_fmt = DRM_FORMAT_BGR565,
102 .de2_fmt = SUN8I_MIXER_FBFMT_BGR565,
104 .csc = SUN8I_CSC_MODE_OFF,
107 .drm_fmt = DRM_FORMAT_ARGB4444,
108 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB4444,
110 .csc = SUN8I_CSC_MODE_OFF,
113 .drm_fmt = DRM_FORMAT_ABGR4444,
114 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR4444,
116 .csc = SUN8I_CSC_MODE_OFF,
119 .drm_fmt = DRM_FORMAT_RGBA4444,
120 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA4444,
122 .csc = SUN8I_CSC_MODE_OFF,
125 .drm_fmt = DRM_FORMAT_BGRA4444,
126 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA4444,
128 .csc = SUN8I_CSC_MODE_OFF,
131 .drm_fmt = DRM_FORMAT_ARGB1555,
132 .de2_fmt = SUN8I_MIXER_FBFMT_ARGB1555,
134 .csc = SUN8I_CSC_MODE_OFF,
137 .drm_fmt = DRM_FORMAT_ABGR1555,
138 .de2_fmt = SUN8I_MIXER_FBFMT_ABGR1555,
140 .csc = SUN8I_CSC_MODE_OFF,
143 .drm_fmt = DRM_FORMAT_RGBA5551,
144 .de2_fmt = SUN8I_MIXER_FBFMT_RGBA5551,
146 .csc = SUN8I_CSC_MODE_OFF,
149 .drm_fmt = DRM_FORMAT_BGRA5551,
150 .de2_fmt = SUN8I_MIXER_FBFMT_BGRA5551,
152 .csc = SUN8I_CSC_MODE_OFF,
155 .drm_fmt = DRM_FORMAT_UYVY,
156 .de2_fmt = SUN8I_MIXER_FBFMT_UYVY,
158 .csc = SUN8I_CSC_MODE_YUV2RGB,
161 .drm_fmt = DRM_FORMAT_VYUY,
162 .de2_fmt = SUN8I_MIXER_FBFMT_VYUY,
164 .csc = SUN8I_CSC_MODE_YUV2RGB,
167 .drm_fmt = DRM_FORMAT_YUYV,
168 .de2_fmt = SUN8I_MIXER_FBFMT_YUYV,
170 .csc = SUN8I_CSC_MODE_YUV2RGB,
173 .drm_fmt = DRM_FORMAT_YVYU,
174 .de2_fmt = SUN8I_MIXER_FBFMT_YVYU,
176 .csc = SUN8I_CSC_MODE_YUV2RGB,
179 .drm_fmt = DRM_FORMAT_NV16,
180 .de2_fmt = SUN8I_MIXER_FBFMT_NV16,
182 .csc = SUN8I_CSC_MODE_YUV2RGB,
185 .drm_fmt = DRM_FORMAT_NV61,
186 .de2_fmt = SUN8I_MIXER_FBFMT_NV61,
188 .csc = SUN8I_CSC_MODE_YUV2RGB,
191 .drm_fmt = DRM_FORMAT_NV12,
192 .de2_fmt = SUN8I_MIXER_FBFMT_NV12,
194 .csc = SUN8I_CSC_MODE_YUV2RGB,
197 .drm_fmt = DRM_FORMAT_NV21,
198 .de2_fmt = SUN8I_MIXER_FBFMT_NV21,
200 .csc = SUN8I_CSC_MODE_YUV2RGB,
203 .drm_fmt = DRM_FORMAT_YUV444,
204 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
206 .csc = SUN8I_CSC_MODE_YUV2RGB,
209 .drm_fmt = DRM_FORMAT_YUV422,
210 .de2_fmt = SUN8I_MIXER_FBFMT_YUV422,
212 .csc = SUN8I_CSC_MODE_YUV2RGB,
215 .drm_fmt = DRM_FORMAT_YUV420,
216 .de2_fmt = SUN8I_MIXER_FBFMT_YUV420,
218 .csc = SUN8I_CSC_MODE_YUV2RGB,
221 .drm_fmt = DRM_FORMAT_YUV411,
222 .de2_fmt = SUN8I_MIXER_FBFMT_YUV411,
224 .csc = SUN8I_CSC_MODE_YUV2RGB,
227 .drm_fmt = DRM_FORMAT_YVU444,
228 .de2_fmt = SUN8I_MIXER_FBFMT_RGB888,
230 .csc = SUN8I_CSC_MODE_YVU2RGB,
233 .drm_fmt = DRM_FORMAT_YVU422,
234 .de2_fmt = SUN8I_MIXER_FBFMT_YUV422,
236 .csc = SUN8I_CSC_MODE_YVU2RGB,
239 .drm_fmt = DRM_FORMAT_YVU420,
240 .de2_fmt = SUN8I_MIXER_FBFMT_YUV420,
242 .csc = SUN8I_CSC_MODE_YVU2RGB,
245 .drm_fmt = DRM_FORMAT_YVU411,
246 .de2_fmt = SUN8I_MIXER_FBFMT_YUV411,
248 .csc = SUN8I_CSC_MODE_YVU2RGB,
252 const struct de2_fmt_info *sun8i_mixer_format_info(u32 format)
256 for (i = 0; i < ARRAY_SIZE(de2_formats); ++i)
257 if (de2_formats[i].drm_fmt == format)
258 return &de2_formats[i];
263 static void sun8i_mixer_commit(struct sunxi_engine *engine)
265 DRM_DEBUG_DRIVER("Committing changes\n");
267 regmap_write(engine->regs, SUN8I_MIXER_GLOBAL_DBUFF,
268 SUN8I_MIXER_GLOBAL_DBUFF_ENABLE);
271 static struct drm_plane **sun8i_layers_init(struct drm_device *drm,
272 struct sunxi_engine *engine)
274 struct drm_plane **planes;
275 struct sun8i_mixer *mixer = engine_to_sun8i_mixer(engine);
278 planes = devm_kcalloc(drm->dev,
279 mixer->cfg->vi_num + mixer->cfg->ui_num + 1,
280 sizeof(*planes), GFP_KERNEL);
282 return ERR_PTR(-ENOMEM);
284 for (i = 0; i < mixer->cfg->vi_num; i++) {
285 struct sun8i_vi_layer *layer;
287 layer = sun8i_vi_layer_init_one(drm, mixer, i);
290 "Couldn't initialize overlay plane\n");
291 return ERR_CAST(layer);
294 planes[i] = &layer->plane;
297 for (i = 0; i < mixer->cfg->ui_num; i++) {
298 struct sun8i_ui_layer *layer;
300 layer = sun8i_ui_layer_init_one(drm, mixer, i);
302 dev_err(drm->dev, "Couldn't initialize %s plane\n",
303 i ? "overlay" : "primary");
304 return ERR_CAST(layer);
307 planes[mixer->cfg->vi_num + i] = &layer->plane;
313 static const struct sunxi_engine_ops sun8i_engine_ops = {
314 .commit = sun8i_mixer_commit,
315 .layers_init = sun8i_layers_init,
318 static struct regmap_config sun8i_mixer_regmap_config = {
322 .max_register = 0xbfffc, /* guessed */
325 static int sun8i_mixer_bind(struct device *dev, struct device *master,
328 struct platform_device *pdev = to_platform_device(dev);
329 struct drm_device *drm = data;
330 struct sun4i_drv *drv = drm->dev_private;
331 struct sun8i_mixer *mixer;
332 struct resource *res;
338 * The mixer uses single 32-bit register to store memory
339 * addresses, so that it cannot deal with 64-bit memory
341 * Restrict the DMA mask so that the mixer won't be
342 * allocated some memory that is too high.
344 ret = dma_set_mask(dev, DMA_BIT_MASK(32));
346 dev_err(dev, "Cannot do 32-bit DMA.\n");
350 mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
353 dev_set_drvdata(dev, mixer);
354 mixer->engine.ops = &sun8i_engine_ops;
355 mixer->engine.node = dev->of_node;
356 /* The ID of the mixer currently doesn't matter */
357 mixer->engine.id = -1;
359 mixer->cfg = of_device_get_match_data(dev);
363 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
364 regs = devm_ioremap_resource(dev, res);
366 return PTR_ERR(regs);
368 mixer->engine.regs = devm_regmap_init_mmio(dev, regs,
369 &sun8i_mixer_regmap_config);
370 if (IS_ERR(mixer->engine.regs)) {
371 dev_err(dev, "Couldn't create the mixer regmap\n");
372 return PTR_ERR(mixer->engine.regs);
375 mixer->reset = devm_reset_control_get(dev, NULL);
376 if (IS_ERR(mixer->reset)) {
377 dev_err(dev, "Couldn't get our reset line\n");
378 return PTR_ERR(mixer->reset);
381 ret = reset_control_deassert(mixer->reset);
383 dev_err(dev, "Couldn't deassert our reset line\n");
387 mixer->bus_clk = devm_clk_get(dev, "bus");
388 if (IS_ERR(mixer->bus_clk)) {
389 dev_err(dev, "Couldn't get the mixer bus clock\n");
390 ret = PTR_ERR(mixer->bus_clk);
391 goto err_assert_reset;
393 clk_prepare_enable(mixer->bus_clk);
395 mixer->mod_clk = devm_clk_get(dev, "mod");
396 if (IS_ERR(mixer->mod_clk)) {
397 dev_err(dev, "Couldn't get the mixer module clock\n");
398 ret = PTR_ERR(mixer->mod_clk);
399 goto err_disable_bus_clk;
403 * It seems that we need to enforce that rate for whatever
404 * reason for the mixer to be functional. Make sure it's the
407 if (mixer->cfg->mod_rate)
408 clk_set_rate(mixer->mod_clk, mixer->cfg->mod_rate);
410 clk_prepare_enable(mixer->mod_clk);
412 list_add_tail(&mixer->engine.list, &drv->engine_list);
414 /* Reset the registers */
415 for (i = 0x0; i < 0x20000; i += 4)
416 regmap_write(mixer->engine.regs, i, 0);
418 /* Enable the mixer */
419 regmap_write(mixer->engine.regs, SUN8I_MIXER_GLOBAL_CTL,
420 SUN8I_MIXER_GLOBAL_CTL_RT_EN);
422 /* Set background color to black */
423 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_BKCOLOR,
424 SUN8I_MIXER_BLEND_COLOR_BLACK);
427 * Set fill color of bottom plane to black. Generally not needed
428 * except when VI plane is at bottom (zpos = 0) and enabled.
430 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_PIPE_CTL,
431 SUN8I_MIXER_BLEND_PIPE_CTL_FC_EN(0));
432 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_ATTR_FCOLOR(0),
433 SUN8I_MIXER_BLEND_COLOR_BLACK);
435 /* Fixed zpos for now */
436 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_ROUTE, 0x43210);
438 plane_cnt = mixer->cfg->vi_num + mixer->cfg->ui_num;
439 for (i = 0; i < plane_cnt; i++)
440 regmap_write(mixer->engine.regs, SUN8I_MIXER_BLEND_MODE(i),
441 SUN8I_MIXER_BLEND_MODE_DEF);
446 clk_disable_unprepare(mixer->bus_clk);
448 reset_control_assert(mixer->reset);
452 static void sun8i_mixer_unbind(struct device *dev, struct device *master,
455 struct sun8i_mixer *mixer = dev_get_drvdata(dev);
457 list_del(&mixer->engine.list);
459 clk_disable_unprepare(mixer->mod_clk);
460 clk_disable_unprepare(mixer->bus_clk);
461 reset_control_assert(mixer->reset);
464 static const struct component_ops sun8i_mixer_ops = {
465 .bind = sun8i_mixer_bind,
466 .unbind = sun8i_mixer_unbind,
469 static int sun8i_mixer_probe(struct platform_device *pdev)
471 return component_add(&pdev->dev, &sun8i_mixer_ops);
474 static int sun8i_mixer_remove(struct platform_device *pdev)
476 component_del(&pdev->dev, &sun8i_mixer_ops);
481 static const struct sun8i_mixer_cfg sun8i_a83t_mixer0_cfg = {
488 static const struct sun8i_mixer_cfg sun8i_a83t_mixer1_cfg = {
495 static const struct sun8i_mixer_cfg sun8i_h3_mixer0_cfg = {
497 .mod_rate = 432000000,
503 static const struct sun8i_mixer_cfg sun8i_v3s_mixer_cfg = {
508 .mod_rate = 150000000,
511 static const struct of_device_id sun8i_mixer_of_table[] = {
513 .compatible = "allwinner,sun8i-a83t-de2-mixer-0",
514 .data = &sun8i_a83t_mixer0_cfg,
517 .compatible = "allwinner,sun8i-a83t-de2-mixer-1",
518 .data = &sun8i_a83t_mixer1_cfg,
521 .compatible = "allwinner,sun8i-h3-de2-mixer-0",
522 .data = &sun8i_h3_mixer0_cfg,
525 .compatible = "allwinner,sun8i-v3s-de2-mixer",
526 .data = &sun8i_v3s_mixer_cfg,
530 MODULE_DEVICE_TABLE(of, sun8i_mixer_of_table);
532 static struct platform_driver sun8i_mixer_platform_driver = {
533 .probe = sun8i_mixer_probe,
534 .remove = sun8i_mixer_remove,
536 .name = "sun8i-mixer",
537 .of_match_table = sun8i_mixer_of_table,
540 module_platform_driver(sun8i_mixer_platform_driver);
543 MODULE_DESCRIPTION("Allwinner DE2 Mixer driver");
544 MODULE_LICENSE("GPL");