1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017 MediaTek Inc.
8 #include <linux/component.h>
9 #include <linux/of_device.h>
10 #include <linux/of_irq.h>
11 #include <linux/platform_device.h>
13 #include "mtk_drm_crtc.h"
14 #include "mtk_drm_ddp_comp.h"
16 #define DISP_COLOR_CFG_MAIN 0x0400
17 #define DISP_COLOR_START_MT2701 0x0f00
18 #define DISP_COLOR_START_MT8173 0x0c00
19 #define DISP_COLOR_START(comp) ((comp)->data->color_offset)
20 #define DISP_COLOR_WIDTH(comp) (DISP_COLOR_START(comp) + 0x50)
21 #define DISP_COLOR_HEIGHT(comp) (DISP_COLOR_START(comp) + 0x54)
23 #define COLOR_BYPASS_ALL BIT(7)
24 #define COLOR_SEQ_SEL BIT(13)
26 struct mtk_disp_color_data {
27 unsigned int color_offset;
31 * struct mtk_disp_color - DISP_COLOR driver structure
32 * @ddp_comp - structure containing type enum and hardware resources
33 * @crtc - associated crtc to report irq events to
35 struct mtk_disp_color {
36 struct mtk_ddp_comp ddp_comp;
37 struct drm_crtc *crtc;
38 const struct mtk_disp_color_data *data;
41 static inline struct mtk_disp_color *comp_to_color(struct mtk_ddp_comp *comp)
43 return container_of(comp, struct mtk_disp_color, ddp_comp);
46 static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
47 unsigned int h, unsigned int vrefresh,
50 struct mtk_disp_color *color = comp_to_color(comp);
52 writel(w, comp->regs + DISP_COLOR_WIDTH(color));
53 writel(h, comp->regs + DISP_COLOR_HEIGHT(color));
56 static void mtk_color_start(struct mtk_ddp_comp *comp)
58 struct mtk_disp_color *color = comp_to_color(comp);
60 writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
61 comp->regs + DISP_COLOR_CFG_MAIN);
62 writel(0x1, comp->regs + DISP_COLOR_START(color));
65 static const struct mtk_ddp_comp_funcs mtk_disp_color_funcs = {
66 .config = mtk_color_config,
67 .start = mtk_color_start,
70 static int mtk_disp_color_bind(struct device *dev, struct device *master,
73 struct mtk_disp_color *priv = dev_get_drvdata(dev);
74 struct drm_device *drm_dev = data;
77 ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
79 dev_err(dev, "Failed to register component %pOF: %d\n",
87 static void mtk_disp_color_unbind(struct device *dev, struct device *master,
90 struct mtk_disp_color *priv = dev_get_drvdata(dev);
91 struct drm_device *drm_dev = data;
93 mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
96 static const struct component_ops mtk_disp_color_component_ops = {
97 .bind = mtk_disp_color_bind,
98 .unbind = mtk_disp_color_unbind,
101 static int mtk_disp_color_probe(struct platform_device *pdev)
103 struct device *dev = &pdev->dev;
104 struct mtk_disp_color *priv;
108 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
112 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_COLOR);
114 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
118 ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
119 &mtk_disp_color_funcs);
121 dev_err(dev, "Failed to initialize component: %d\n", ret);
125 priv->data = of_device_get_match_data(dev);
127 platform_set_drvdata(pdev, priv);
129 ret = component_add(dev, &mtk_disp_color_component_ops);
131 dev_err(dev, "Failed to add component: %d\n", ret);
136 static int mtk_disp_color_remove(struct platform_device *pdev)
138 component_del(&pdev->dev, &mtk_disp_color_component_ops);
143 static const struct mtk_disp_color_data mt2701_color_driver_data = {
144 .color_offset = DISP_COLOR_START_MT2701,
147 static const struct mtk_disp_color_data mt8173_color_driver_data = {
148 .color_offset = DISP_COLOR_START_MT8173,
151 static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
152 { .compatible = "mediatek,mt2701-disp-color",
153 .data = &mt2701_color_driver_data},
154 { .compatible = "mediatek,mt8173-disp-color",
155 .data = &mt8173_color_driver_data},
158 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
160 struct platform_driver mtk_disp_color_driver = {
161 .probe = mtk_disp_color_probe,
162 .remove = mtk_disp_color_remove,
164 .name = "mediatek-disp-color",
165 .owner = THIS_MODULE,
166 .of_match_table = mtk_disp_color_driver_dt_match,