1 /*
2 * Copyright (c) 2017 MediaTek Inc.
3 *
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.
7 *
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.
12 */
13
14 #include <drm/drmP.h>
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>
20
21 #include "mtk_drm_crtc.h"
22 #include "mtk_drm_ddp_comp.h"
23
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)
30
31 #define COLOR_BYPASS_ALL BIT(7)
32 #define COLOR_SEQ_SEL BIT(13)
33
34 struct mtk_disp_color_data {
35 unsigned int color_offset;
36 };
37
38 /**
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
42 */
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;
47 };
48
comp_to_color(struct mtk_ddp_comp * comp)49 static inline struct mtk_disp_color *comp_to_color(struct mtk_ddp_comp *comp)
50 {
51 return container_of(comp, struct mtk_disp_color, ddp_comp);
52 }
53
mtk_color_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc)54 static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
55 unsigned int h, unsigned int vrefresh,
56 unsigned int bpc)
57 {
58 struct mtk_disp_color *color = comp_to_color(comp);
59
60 writel(w, comp->regs + DISP_COLOR_WIDTH(color));
61 writel(h, comp->regs + DISP_COLOR_HEIGHT(color));
62 }
63
mtk_color_start(struct mtk_ddp_comp * comp)64 static void mtk_color_start(struct mtk_ddp_comp *comp)
65 {
66 struct mtk_disp_color *color = comp_to_color(comp);
67
68 writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
69 comp->regs + DISP_COLOR_CFG_MAIN);
70 writel(0x1, comp->regs + DISP_COLOR_START(color));
71 }
72
73 static const struct mtk_ddp_comp_funcs mtk_disp_color_funcs = {
74 .config = mtk_color_config,
75 .start = mtk_color_start,
76 };
77
mtk_disp_color_bind(struct device * dev,struct device * master,void * data)78 static int mtk_disp_color_bind(struct device *dev, struct device *master,
79 void *data)
80 {
81 struct mtk_disp_color *priv = dev_get_drvdata(dev);
82 struct drm_device *drm_dev = data;
83 int ret;
84
85 ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
86 if (ret < 0) {
87 dev_err(dev, "Failed to register component %pOF: %d\n",
88 dev->of_node, ret);
89 return ret;
90 }
91
92 return 0;
93 }
94
mtk_disp_color_unbind(struct device * dev,struct device * master,void * data)95 static void mtk_disp_color_unbind(struct device *dev, struct device *master,
96 void *data)
97 {
98 struct mtk_disp_color *priv = dev_get_drvdata(dev);
99 struct drm_device *drm_dev = data;
100
101 mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
102 }
103
104 static const struct component_ops mtk_disp_color_component_ops = {
105 .bind = mtk_disp_color_bind,
106 .unbind = mtk_disp_color_unbind,
107 };
108
mtk_disp_color_probe(struct platform_device * pdev)109 static int mtk_disp_color_probe(struct platform_device *pdev)
110 {
111 struct device *dev = &pdev->dev;
112 struct mtk_disp_color *priv;
113 int comp_id;
114 int ret;
115
116 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
117 if (!priv)
118 return -ENOMEM;
119
120 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_COLOR);
121 if (comp_id < 0) {
122 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
123 return comp_id;
124 }
125
126 ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
127 &mtk_disp_color_funcs);
128 if (ret) {
129 dev_err(dev, "Failed to initialize component: %d\n", ret);
130 return ret;
131 }
132
133 priv->data = of_device_get_match_data(dev);
134
135 platform_set_drvdata(pdev, priv);
136
137 ret = component_add(dev, &mtk_disp_color_component_ops);
138 if (ret)
139 dev_err(dev, "Failed to add component: %d\n", ret);
140
141 return ret;
142 }
143
mtk_disp_color_remove(struct platform_device * pdev)144 static int mtk_disp_color_remove(struct platform_device *pdev)
145 {
146 component_del(&pdev->dev, &mtk_disp_color_component_ops);
147
148 return 0;
149 }
150
151 static const struct mtk_disp_color_data mt2701_color_driver_data = {
152 .color_offset = DISP_COLOR_START_MT2701,
153 };
154
155 static const struct mtk_disp_color_data mt8173_color_driver_data = {
156 .color_offset = DISP_COLOR_START_MT8173,
157 };
158
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},
164 {},
165 };
166 MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
167
168 struct platform_driver mtk_disp_color_driver = {
169 .probe = mtk_disp_color_probe,
170 .remove = mtk_disp_color_remove,
171 .driver = {
172 .name = "mediatek-disp-color",
173 .owner = THIS_MODULE,
174 .of_match_table = mtk_disp_color_driver_dt_match,
175 },
176 };
177