2 * Copyright (c) 2017 MediaTek Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/component.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
21 #include "mtk_drm_crtc.h"
22 #include "mtk_drm_ddp_comp.h"
24 #define DISP_COLOR_CFG_MAIN 0x0400
25 #define DISP_COLOR_START_MT2701 0x0f00
26 #define DISP_COLOR_START_MT8173 0x0c00
27 #define DISP_COLOR_START(comp) ((comp)->data->color_offset)
28 #define DISP_COLOR_WIDTH(comp) (DISP_COLOR_START(comp) + 0x50)
29 #define DISP_COLOR_HEIGHT(comp) (DISP_COLOR_START(comp) + 0x54)
31 #define COLOR_BYPASS_ALL BIT(7)
32 #define COLOR_SEQ_SEL BIT(13)
34 struct mtk_disp_color_data {
35 unsigned int color_offset;
39 * struct mtk_disp_color - DISP_COLOR driver structure
40 * @ddp_comp - structure containing type enum and hardware resources
41 * @crtc - associated crtc to report irq events to
43 struct mtk_disp_color {
44 struct mtk_ddp_comp ddp_comp;
45 struct drm_crtc *crtc;
46 const struct mtk_disp_color_data *data;
49 static inline struct mtk_disp_color *comp_to_color(struct mtk_ddp_comp *comp)
51 return container_of(comp, struct mtk_disp_color, ddp_comp);
54 static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
55 unsigned int h, unsigned int vrefresh,
58 struct mtk_disp_color *color = comp_to_color(comp);
60 writel(w, comp->regs + DISP_COLOR_WIDTH(color));
61 writel(h, comp->regs + DISP_COLOR_HEIGHT(color));
64 static void mtk_color_start(struct mtk_ddp_comp *comp)
66 struct mtk_disp_color *color = comp_to_color(comp);
68 writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
69 comp->regs + DISP_COLOR_CFG_MAIN);
70 writel(0x1, comp->regs + DISP_COLOR_START(color));
73 static const struct mtk_ddp_comp_funcs mtk_disp_color_funcs = {
74 .config = mtk_color_config,
75 .start = mtk_color_start,
78 static int mtk_disp_color_bind(struct device *dev, struct device *master,
81 struct mtk_disp_color *priv = dev_get_drvdata(dev);
82 struct drm_device *drm_dev = data;
85 ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
87 dev_err(dev, "Failed to register component %pOF: %d\n",
95 static void mtk_disp_color_unbind(struct device *dev, struct device *master,
98 struct mtk_disp_color *priv = dev_get_drvdata(dev);
99 struct drm_device *drm_dev = data;
101 mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
104 static const struct component_ops mtk_disp_color_component_ops = {
105 .bind = mtk_disp_color_bind,
106 .unbind = mtk_disp_color_unbind,
109 static int mtk_disp_color_probe(struct platform_device *pdev)
111 struct device *dev = &pdev->dev;
112 struct mtk_disp_color *priv;
116 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
120 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_COLOR);
122 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
126 ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
127 &mtk_disp_color_funcs);
129 dev_err(dev, "Failed to initialize component: %d\n", ret);
133 priv->data = of_device_get_match_data(dev);
135 platform_set_drvdata(pdev, priv);
137 ret = component_add(dev, &mtk_disp_color_component_ops);
139 dev_err(dev, "Failed to add component: %d\n", ret);
144 static int mtk_disp_color_remove(struct platform_device *pdev)
146 component_del(&pdev->dev, &mtk_disp_color_component_ops);
151 static const struct mtk_disp_color_data mt2701_color_driver_data = {
152 .color_offset = DISP_COLOR_START_MT2701,
155 static const struct mtk_disp_color_data mt8173_color_driver_data = {
156 .color_offset = DISP_COLOR_START_MT8173,
159 static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
160 { .compatible = "mediatek,mt2701-disp-color",
161 .data = &mt2701_color_driver_data},
162 { .compatible = "mediatek,mt8173-disp-color",
163 .data = &mt8173_color_driver_data},
166 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
168 struct platform_driver mtk_disp_color_driver = {
169 .probe = mtk_disp_color_probe,
170 .remove = mtk_disp_color_remove,
172 .name = "mediatek-disp-color",
173 .owner = THIS_MODULE,
174 .of_match_table = mtk_disp_color_driver_dt_match,