1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) STMicroelectronics SA 2014
4 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5 * Fabien Dessenne <fabien.dessenne@st.com>
6 * for STMicroelectronics.
7 */
8
9 #include <linux/component.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset.h>
15
16 #include <drm/drm_device.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_vblank.h>
19
20 #include "sti_compositor.h"
21 #include "sti_crtc.h"
22 #include "sti_cursor.h"
23 #include "sti_drv.h"
24 #include "sti_gdp.h"
25 #include "sti_plane.h"
26 #include "sti_vid.h"
27 #include "sti_vtg.h"
28
29 /*
30 * stiH407 compositor properties
31 */
32 static const struct sti_compositor_data stih407_compositor_data = {
33 .nb_subdev = 8,
34 .subdev_desc = {
35 {STI_CURSOR_SUBDEV, (int)STI_CURSOR, 0x000},
36 {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100},
37 {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200},
38 {STI_GPD_SUBDEV, (int)STI_GDP_2, 0x300},
39 {STI_GPD_SUBDEV, (int)STI_GDP_3, 0x400},
40 {STI_VID_SUBDEV, (int)STI_HQVDP_0, 0x700},
41 {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00},
42 {STI_MIXER_AUX_SUBDEV, STI_MIXER_AUX, 0xD00},
43 },
44 };
45
sti_compositor_debugfs_init(struct sti_compositor * compo,struct drm_minor * minor)46 void sti_compositor_debugfs_init(struct sti_compositor *compo,
47 struct drm_minor *minor)
48 {
49 unsigned int i;
50
51 for (i = 0; i < STI_MAX_VID; i++)
52 if (compo->vid[i])
53 vid_debugfs_init(compo->vid[i], minor);
54
55 for (i = 0; i < STI_MAX_MIXER; i++)
56 if (compo->mixer[i])
57 sti_mixer_debugfs_init(compo->mixer[i], minor);
58 }
59
sti_compositor_bind(struct device * dev,struct device * master,void * data)60 static int sti_compositor_bind(struct device *dev,
61 struct device *master,
62 void *data)
63 {
64 struct sti_compositor *compo = dev_get_drvdata(dev);
65 struct drm_device *drm_dev = data;
66 unsigned int i, mixer_id = 0, vid_id = 0, crtc_id = 0;
67 struct sti_private *dev_priv = drm_dev->dev_private;
68 struct drm_plane *cursor = NULL;
69 struct drm_plane *primary = NULL;
70 struct sti_compositor_subdev_descriptor *desc = compo->data.subdev_desc;
71 unsigned int array_size = compo->data.nb_subdev;
72
73 dev_priv->compo = compo;
74
75 /* Register mixer subdev and video subdev first */
76 for (i = 0; i < array_size; i++) {
77 switch (desc[i].type) {
78 case STI_VID_SUBDEV:
79 compo->vid[vid_id++] =
80 sti_vid_create(compo->dev, drm_dev, desc[i].id,
81 compo->regs + desc[i].offset);
82 break;
83 case STI_MIXER_MAIN_SUBDEV:
84 case STI_MIXER_AUX_SUBDEV:
85 compo->mixer[mixer_id++] =
86 sti_mixer_create(compo->dev, drm_dev, desc[i].id,
87 compo->regs + desc[i].offset);
88 break;
89 case STI_GPD_SUBDEV:
90 case STI_CURSOR_SUBDEV:
91 /* Nothing to do, wait for the second round */
92 break;
93 default:
94 DRM_ERROR("Unknown subdev component type\n");
95 return 1;
96 }
97 }
98
99 /* Register the other subdevs, create crtc and planes */
100 for (i = 0; i < array_size; i++) {
101 enum drm_plane_type plane_type = DRM_PLANE_TYPE_OVERLAY;
102
103 if (crtc_id < mixer_id)
104 plane_type = DRM_PLANE_TYPE_PRIMARY;
105
106 switch (desc[i].type) {
107 case STI_MIXER_MAIN_SUBDEV:
108 case STI_MIXER_AUX_SUBDEV:
109 case STI_VID_SUBDEV:
110 /* Nothing to do, already done at the first round */
111 break;
112 case STI_CURSOR_SUBDEV:
113 cursor = sti_cursor_create(drm_dev, compo->dev,
114 desc[i].id,
115 compo->regs + desc[i].offset,
116 1);
117 if (!cursor) {
118 DRM_ERROR("Can't create CURSOR plane\n");
119 break;
120 }
121 break;
122 case STI_GPD_SUBDEV:
123 primary = sti_gdp_create(drm_dev, compo->dev,
124 desc[i].id,
125 compo->regs + desc[i].offset,
126 (1 << mixer_id) - 1,
127 plane_type);
128 if (!primary) {
129 DRM_ERROR("Can't create GDP plane\n");
130 break;
131 }
132 break;
133 default:
134 DRM_ERROR("Unknown subdev component type\n");
135 return 1;
136 }
137
138 /* The first planes are reserved for primary planes*/
139 if (crtc_id < mixer_id && primary) {
140 sti_crtc_init(drm_dev, compo->mixer[crtc_id],
141 primary, cursor);
142 crtc_id++;
143 cursor = NULL;
144 primary = NULL;
145 }
146 }
147
148 drm_vblank_init(drm_dev, crtc_id);
149
150 return 0;
151 }
152
sti_compositor_unbind(struct device * dev,struct device * master,void * data)153 static void sti_compositor_unbind(struct device *dev, struct device *master,
154 void *data)
155 {
156 /* do nothing */
157 }
158
159 static const struct component_ops sti_compositor_ops = {
160 .bind = sti_compositor_bind,
161 .unbind = sti_compositor_unbind,
162 };
163
164 static const struct of_device_id compositor_of_match[] = {
165 {
166 .compatible = "st,stih407-compositor",
167 .data = &stih407_compositor_data,
168 }, {
169 /* end node */
170 }
171 };
172 MODULE_DEVICE_TABLE(of, compositor_of_match);
173
sti_compositor_probe(struct platform_device * pdev)174 static int sti_compositor_probe(struct platform_device *pdev)
175 {
176 struct device *dev = &pdev->dev;
177 struct device_node *np = dev->of_node;
178 struct device_node *vtg_np;
179 struct sti_compositor *compo;
180 unsigned int i;
181
182 compo = devm_kzalloc(dev, sizeof(*compo), GFP_KERNEL);
183 if (!compo) {
184 DRM_ERROR("Failed to allocate compositor context\n");
185 return -ENOMEM;
186 }
187 compo->dev = dev;
188 for (i = 0; i < STI_MAX_MIXER; i++)
189 compo->vtg_vblank_nb[i].notifier_call = sti_crtc_vblank_cb;
190
191 /* populate data structure depending on compatibility */
192 BUG_ON(!of_match_node(compositor_of_match, np)->data);
193
194 memcpy(&compo->data, of_match_node(compositor_of_match, np)->data,
195 sizeof(struct sti_compositor_data));
196 compo->regs = devm_platform_ioremap_resource(pdev, 0);
197 if (IS_ERR(compo->regs)) {
198 DRM_ERROR("Register mapping failed\n");
199 return PTR_ERR(compo->regs);
200 }
201
202 /* Get clock resources */
203 compo->clk_compo_main = devm_clk_get(dev, "compo_main");
204 if (IS_ERR(compo->clk_compo_main)) {
205 DRM_ERROR("Cannot get compo_main clock\n");
206 return PTR_ERR(compo->clk_compo_main);
207 }
208
209 compo->clk_compo_aux = devm_clk_get(dev, "compo_aux");
210 if (IS_ERR(compo->clk_compo_aux)) {
211 DRM_ERROR("Cannot get compo_aux clock\n");
212 return PTR_ERR(compo->clk_compo_aux);
213 }
214
215 compo->clk_pix_main = devm_clk_get(dev, "pix_main");
216 if (IS_ERR(compo->clk_pix_main)) {
217 DRM_ERROR("Cannot get pix_main clock\n");
218 return PTR_ERR(compo->clk_pix_main);
219 }
220
221 compo->clk_pix_aux = devm_clk_get(dev, "pix_aux");
222 if (IS_ERR(compo->clk_pix_aux)) {
223 DRM_ERROR("Cannot get pix_aux clock\n");
224 return PTR_ERR(compo->clk_pix_aux);
225 }
226
227 /* Get reset resources */
228 compo->rst_main = devm_reset_control_get_shared(dev, "compo-main");
229 /* Take compo main out of reset */
230 if (!IS_ERR(compo->rst_main))
231 reset_control_deassert(compo->rst_main);
232
233 compo->rst_aux = devm_reset_control_get_shared(dev, "compo-aux");
234 /* Take compo aux out of reset */
235 if (!IS_ERR(compo->rst_aux))
236 reset_control_deassert(compo->rst_aux);
237
238 vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
239 if (vtg_np)
240 compo->vtg[STI_MIXER_MAIN] = of_vtg_find(vtg_np);
241 of_node_put(vtg_np);
242
243 vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 1);
244 if (vtg_np)
245 compo->vtg[STI_MIXER_AUX] = of_vtg_find(vtg_np);
246 of_node_put(vtg_np);
247
248 platform_set_drvdata(pdev, compo);
249
250 return component_add(&pdev->dev, &sti_compositor_ops);
251 }
252
sti_compositor_remove(struct platform_device * pdev)253 static void sti_compositor_remove(struct platform_device *pdev)
254 {
255 component_del(&pdev->dev, &sti_compositor_ops);
256 }
257
258 struct platform_driver sti_compositor_driver = {
259 .driver = {
260 .name = "sti-compositor",
261 .of_match_table = compositor_of_match,
262 },
263 .probe = sti_compositor_probe,
264 .remove = sti_compositor_remove,
265 };
266
267 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
268 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
269 MODULE_LICENSE("GPL");
270