1e2842570SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0
2d219673dSBenjamin Gaignard /*
3d219673dSBenjamin Gaignard * Copyright (C) STMicroelectronics SA 2014
4d219673dSBenjamin Gaignard * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5d219673dSBenjamin Gaignard * Fabien Dessenne <fabien.dessenne@st.com>
6d219673dSBenjamin Gaignard * for STMicroelectronics.
7d219673dSBenjamin Gaignard */
8d219673dSBenjamin Gaignard
9d219673dSBenjamin Gaignard #include <linux/component.h>
105e2f97a9SSam Ravnborg #include <linux/io.h>
11d219673dSBenjamin Gaignard #include <linux/module.h>
1273289afeSVille Syrjälä #include <linux/of.h>
13d219673dSBenjamin Gaignard #include <linux/platform_device.h>
14d219673dSBenjamin Gaignard #include <linux/reset.h>
15d219673dSBenjamin Gaignard
165e2f97a9SSam Ravnborg #include <drm/drm_device.h>
175e2f97a9SSam Ravnborg #include <drm/drm_print.h>
185e2f97a9SSam Ravnborg #include <drm/drm_vblank.h>
19d219673dSBenjamin Gaignard
20d219673dSBenjamin Gaignard #include "sti_compositor.h"
219e1f05b2SVincent Abriou #include "sti_crtc.h"
22871bcdfeSVincent Abriou #include "sti_cursor.h"
239e1f05b2SVincent Abriou #include "sti_drv.h"
24d219673dSBenjamin Gaignard #include "sti_gdp.h"
259e1f05b2SVincent Abriou #include "sti_plane.h"
26871bcdfeSVincent Abriou #include "sti_vid.h"
27d219673dSBenjamin Gaignard #include "sti_vtg.h"
28d219673dSBenjamin Gaignard
29d219673dSBenjamin Gaignard /*
30d219673dSBenjamin Gaignard * stiH407 compositor properties
31d219673dSBenjamin Gaignard */
32bdfd36efSVille Syrjälä static const struct sti_compositor_data stih407_compositor_data = {
3396006a77SBenjamin Gaignard .nb_subdev = 8,
34d219673dSBenjamin Gaignard .subdev_desc = {
3596006a77SBenjamin Gaignard {STI_CURSOR_SUBDEV, (int)STI_CURSOR, 0x000},
36d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_0, 0x100},
37d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_1, 0x200},
38d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_2, 0x300},
39d219673dSBenjamin Gaignard {STI_GPD_SUBDEV, (int)STI_GDP_3, 0x400},
40871bcdfeSVincent Abriou {STI_VID_SUBDEV, (int)STI_HQVDP_0, 0x700},
415e03abc5SBenjamin Gaignard {STI_MIXER_MAIN_SUBDEV, STI_MIXER_MAIN, 0xC00},
425e03abc5SBenjamin Gaignard {STI_MIXER_AUX_SUBDEV, STI_MIXER_AUX, 0xD00},
43d219673dSBenjamin Gaignard },
44d219673dSBenjamin Gaignard };
45d219673dSBenjamin Gaignard
sti_compositor_debugfs_init(struct sti_compositor * compo,struct drm_minor * minor)4654ac836bSWambui Karuga void sti_compositor_debugfs_init(struct sti_compositor *compo,
4783af0a48SBenjamin Gaignard struct drm_minor *minor)
4883af0a48SBenjamin Gaignard {
4938fdb8d9SVincent Abriou unsigned int i;
5083af0a48SBenjamin Gaignard
5138fdb8d9SVincent Abriou for (i = 0; i < STI_MAX_VID; i++)
5238fdb8d9SVincent Abriou if (compo->vid[i])
5338fdb8d9SVincent Abriou vid_debugfs_init(compo->vid[i], minor);
5483af0a48SBenjamin Gaignard
5538fdb8d9SVincent Abriou for (i = 0; i < STI_MAX_MIXER; i++)
5638fdb8d9SVincent Abriou if (compo->mixer[i])
5738fdb8d9SVincent Abriou sti_mixer_debugfs_init(compo->mixer[i], minor);
5883af0a48SBenjamin Gaignard }
5983af0a48SBenjamin Gaignard
sti_compositor_bind(struct device * dev,struct device * master,void * data)60871bcdfeSVincent Abriou static int sti_compositor_bind(struct device *dev,
61871bcdfeSVincent Abriou struct device *master,
62871bcdfeSVincent Abriou void *data)
63d219673dSBenjamin Gaignard {
64871bcdfeSVincent Abriou struct sti_compositor *compo = dev_get_drvdata(dev);
65871bcdfeSVincent Abriou struct drm_device *drm_dev = data;
6629d1dc62SVincent Abriou unsigned int i, mixer_id = 0, vid_id = 0, crtc_id = 0;
679e1f05b2SVincent Abriou struct sti_private *dev_priv = drm_dev->dev_private;
68871bcdfeSVincent Abriou struct drm_plane *cursor = NULL;
69871bcdfeSVincent Abriou struct drm_plane *primary = NULL;
70871bcdfeSVincent Abriou struct sti_compositor_subdev_descriptor *desc = compo->data.subdev_desc;
71871bcdfeSVincent Abriou unsigned int array_size = compo->data.nb_subdev;
72d219673dSBenjamin Gaignard
73871bcdfeSVincent Abriou dev_priv->compo = compo;
74871bcdfeSVincent Abriou
75871bcdfeSVincent Abriou /* Register mixer subdev and video subdev first */
76d219673dSBenjamin Gaignard for (i = 0; i < array_size; i++) {
77d219673dSBenjamin Gaignard switch (desc[i].type) {
78871bcdfeSVincent Abriou case STI_VID_SUBDEV:
79871bcdfeSVincent Abriou compo->vid[vid_id++] =
8090dffef5SVincent Abriou sti_vid_create(compo->dev, drm_dev, desc[i].id,
81871bcdfeSVincent Abriou compo->regs + desc[i].offset);
82871bcdfeSVincent Abriou break;
83d219673dSBenjamin Gaignard case STI_MIXER_MAIN_SUBDEV:
84d219673dSBenjamin Gaignard case STI_MIXER_AUX_SUBDEV:
85d219673dSBenjamin Gaignard compo->mixer[mixer_id++] =
86a5f81078SVincent Abriou sti_mixer_create(compo->dev, drm_dev, desc[i].id,
87d219673dSBenjamin Gaignard compo->regs + desc[i].offset);
88d219673dSBenjamin Gaignard break;
89d219673dSBenjamin Gaignard case STI_GPD_SUBDEV:
9096006a77SBenjamin Gaignard case STI_CURSOR_SUBDEV:
91871bcdfeSVincent Abriou /* Nothing to do, wait for the second round */
92d219673dSBenjamin Gaignard break;
93d219673dSBenjamin Gaignard default:
940a95160eSMasanari Iida DRM_ERROR("Unknown subdev component type\n");
95d219673dSBenjamin Gaignard return 1;
96d219673dSBenjamin Gaignard }
97d219673dSBenjamin Gaignard }
98d219673dSBenjamin Gaignard
99871bcdfeSVincent Abriou /* Register the other subdevs, create crtc and planes */
100871bcdfeSVincent Abriou for (i = 0; i < array_size; i++) {
1019bbf86feSBenjamin Gaignard enum drm_plane_type plane_type = DRM_PLANE_TYPE_OVERLAY;
1029bbf86feSBenjamin Gaignard
103871bcdfeSVincent Abriou if (crtc_id < mixer_id)
1049bbf86feSBenjamin Gaignard plane_type = DRM_PLANE_TYPE_PRIMARY;
1059bbf86feSBenjamin Gaignard
106871bcdfeSVincent Abriou switch (desc[i].type) {
107871bcdfeSVincent Abriou case STI_MIXER_MAIN_SUBDEV:
108871bcdfeSVincent Abriou case STI_MIXER_AUX_SUBDEV:
109871bcdfeSVincent Abriou case STI_VID_SUBDEV:
110871bcdfeSVincent Abriou /* Nothing to do, already done at the first round */
1119bbf86feSBenjamin Gaignard break;
112871bcdfeSVincent Abriou case STI_CURSOR_SUBDEV:
11329d1dc62SVincent Abriou cursor = sti_cursor_create(drm_dev, compo->dev,
11429d1dc62SVincent Abriou desc[i].id,
11529d1dc62SVincent Abriou compo->regs + desc[i].offset,
11629d1dc62SVincent Abriou 1);
11729d1dc62SVincent Abriou if (!cursor) {
118871bcdfeSVincent Abriou DRM_ERROR("Can't create CURSOR plane\n");
119871bcdfeSVincent Abriou break;
120871bcdfeSVincent Abriou }
121871bcdfeSVincent Abriou break;
122871bcdfeSVincent Abriou case STI_GPD_SUBDEV:
12329d1dc62SVincent Abriou primary = sti_gdp_create(drm_dev, compo->dev,
12429d1dc62SVincent Abriou desc[i].id,
12529d1dc62SVincent Abriou compo->regs + desc[i].offset,
12629d1dc62SVincent Abriou (1 << mixer_id) - 1,
12729d1dc62SVincent Abriou plane_type);
12829d1dc62SVincent Abriou if (!primary) {
129871bcdfeSVincent Abriou DRM_ERROR("Can't create GDP plane\n");
130871bcdfeSVincent Abriou break;
131871bcdfeSVincent Abriou }
1329bbf86feSBenjamin Gaignard break;
133871bcdfeSVincent Abriou default:
13452d9d38cSColin Ian King DRM_ERROR("Unknown subdev component type\n");
135871bcdfeSVincent Abriou return 1;
1369bbf86feSBenjamin Gaignard }
1379bbf86feSBenjamin Gaignard
1389bbf86feSBenjamin Gaignard /* The first planes are reserved for primary planes*/
139871bcdfeSVincent Abriou if (crtc_id < mixer_id && primary) {
1409e1f05b2SVincent Abriou sti_crtc_init(drm_dev, compo->mixer[crtc_id],
1419bbf86feSBenjamin Gaignard primary, cursor);
142871bcdfeSVincent Abriou crtc_id++;
1439bbf86feSBenjamin Gaignard cursor = NULL;
14496006a77SBenjamin Gaignard primary = NULL;
1459bbf86feSBenjamin Gaignard }
1469bbf86feSBenjamin Gaignard }
147d219673dSBenjamin Gaignard
148871bcdfeSVincent Abriou drm_vblank_init(drm_dev, crtc_id);
149d219673dSBenjamin Gaignard
150d219673dSBenjamin Gaignard return 0;
151d219673dSBenjamin Gaignard }
152d219673dSBenjamin Gaignard
sti_compositor_unbind(struct device * dev,struct device * master,void * data)153d219673dSBenjamin Gaignard static void sti_compositor_unbind(struct device *dev, struct device *master,
154d219673dSBenjamin Gaignard void *data)
155d219673dSBenjamin Gaignard {
156d219673dSBenjamin Gaignard /* do nothing */
157d219673dSBenjamin Gaignard }
158d219673dSBenjamin Gaignard
159d219673dSBenjamin Gaignard static const struct component_ops sti_compositor_ops = {
160d219673dSBenjamin Gaignard .bind = sti_compositor_bind,
161d219673dSBenjamin Gaignard .unbind = sti_compositor_unbind,
162d219673dSBenjamin Gaignard };
163d219673dSBenjamin Gaignard
164d219673dSBenjamin Gaignard static const struct of_device_id compositor_of_match[] = {
165d219673dSBenjamin Gaignard {
166d219673dSBenjamin Gaignard .compatible = "st,stih407-compositor",
167d219673dSBenjamin Gaignard .data = &stih407_compositor_data,
168d219673dSBenjamin Gaignard }, {
169d219673dSBenjamin Gaignard /* end node */
170d219673dSBenjamin Gaignard }
171d219673dSBenjamin Gaignard };
172d219673dSBenjamin Gaignard MODULE_DEVICE_TABLE(of, compositor_of_match);
173d219673dSBenjamin Gaignard
sti_compositor_probe(struct platform_device * pdev)174d219673dSBenjamin Gaignard static int sti_compositor_probe(struct platform_device *pdev)
175d219673dSBenjamin Gaignard {
176d219673dSBenjamin Gaignard struct device *dev = &pdev->dev;
177d219673dSBenjamin Gaignard struct device_node *np = dev->of_node;
178d219673dSBenjamin Gaignard struct device_node *vtg_np;
179d219673dSBenjamin Gaignard struct sti_compositor *compo;
180d219673dSBenjamin Gaignard struct resource *res;
181ae217850SFabien Dessenne unsigned int i;
182d219673dSBenjamin Gaignard
183d219673dSBenjamin Gaignard compo = devm_kzalloc(dev, sizeof(*compo), GFP_KERNEL);
184d219673dSBenjamin Gaignard if (!compo) {
185d219673dSBenjamin Gaignard DRM_ERROR("Failed to allocate compositor context\n");
186d219673dSBenjamin Gaignard return -ENOMEM;
187d219673dSBenjamin Gaignard }
188d219673dSBenjamin Gaignard compo->dev = dev;
189ae217850SFabien Dessenne for (i = 0; i < STI_MAX_MIXER; i++)
190ae217850SFabien Dessenne compo->vtg_vblank_nb[i].notifier_call = sti_crtc_vblank_cb;
191d219673dSBenjamin Gaignard
192d219673dSBenjamin Gaignard /* populate data structure depending on compatibility */
193d219673dSBenjamin Gaignard BUG_ON(!of_match_node(compositor_of_match, np)->data);
194d219673dSBenjamin Gaignard
195d219673dSBenjamin Gaignard memcpy(&compo->data, of_match_node(compositor_of_match, np)->data,
196d219673dSBenjamin Gaignard sizeof(struct sti_compositor_data));
197d219673dSBenjamin Gaignard
198d219673dSBenjamin Gaignard /* Get Memory ressources */
199d219673dSBenjamin Gaignard res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200d219673dSBenjamin Gaignard if (res == NULL) {
201d219673dSBenjamin Gaignard DRM_ERROR("Get memory resource failed\n");
202d219673dSBenjamin Gaignard return -ENXIO;
203d219673dSBenjamin Gaignard }
204d219673dSBenjamin Gaignard compo->regs = devm_ioremap(dev, res->start, resource_size(res));
205d219673dSBenjamin Gaignard if (compo->regs == NULL) {
206d219673dSBenjamin Gaignard DRM_ERROR("Register mapping failed\n");
207d219673dSBenjamin Gaignard return -ENXIO;
208d219673dSBenjamin Gaignard }
209d219673dSBenjamin Gaignard
210d219673dSBenjamin Gaignard /* Get clock resources */
211d219673dSBenjamin Gaignard compo->clk_compo_main = devm_clk_get(dev, "compo_main");
212d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_compo_main)) {
213d219673dSBenjamin Gaignard DRM_ERROR("Cannot get compo_main clock\n");
214d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_compo_main);
215d219673dSBenjamin Gaignard }
216d219673dSBenjamin Gaignard
217d219673dSBenjamin Gaignard compo->clk_compo_aux = devm_clk_get(dev, "compo_aux");
218d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_compo_aux)) {
219d219673dSBenjamin Gaignard DRM_ERROR("Cannot get compo_aux clock\n");
220d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_compo_aux);
221d219673dSBenjamin Gaignard }
222d219673dSBenjamin Gaignard
223d219673dSBenjamin Gaignard compo->clk_pix_main = devm_clk_get(dev, "pix_main");
224d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_pix_main)) {
225d219673dSBenjamin Gaignard DRM_ERROR("Cannot get pix_main clock\n");
226d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_pix_main);
227d219673dSBenjamin Gaignard }
228d219673dSBenjamin Gaignard
229d219673dSBenjamin Gaignard compo->clk_pix_aux = devm_clk_get(dev, "pix_aux");
230d219673dSBenjamin Gaignard if (IS_ERR(compo->clk_pix_aux)) {
231d219673dSBenjamin Gaignard DRM_ERROR("Cannot get pix_aux clock\n");
232d219673dSBenjamin Gaignard return PTR_ERR(compo->clk_pix_aux);
233d219673dSBenjamin Gaignard }
234d219673dSBenjamin Gaignard
235d219673dSBenjamin Gaignard /* Get reset resources */
236c084c8daSLee Jones compo->rst_main = devm_reset_control_get_shared(dev, "compo-main");
237d219673dSBenjamin Gaignard /* Take compo main out of reset */
238d219673dSBenjamin Gaignard if (!IS_ERR(compo->rst_main))
239d219673dSBenjamin Gaignard reset_control_deassert(compo->rst_main);
240d219673dSBenjamin Gaignard
241c084c8daSLee Jones compo->rst_aux = devm_reset_control_get_shared(dev, "compo-aux");
242d219673dSBenjamin Gaignard /* Take compo aux out of reset */
243d219673dSBenjamin Gaignard if (!IS_ERR(compo->rst_aux))
244d219673dSBenjamin Gaignard reset_control_deassert(compo->rst_aux);
245d219673dSBenjamin Gaignard
246d219673dSBenjamin Gaignard vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
247d219673dSBenjamin Gaignard if (vtg_np)
248ffdbb82cSFabien Dessenne compo->vtg[STI_MIXER_MAIN] = of_vtg_find(vtg_np);
2499897f79bSPeter Chen of_node_put(vtg_np);
250d219673dSBenjamin Gaignard
251d219673dSBenjamin Gaignard vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 1);
252d219673dSBenjamin Gaignard if (vtg_np)
253ffdbb82cSFabien Dessenne compo->vtg[STI_MIXER_AUX] = of_vtg_find(vtg_np);
2549897f79bSPeter Chen of_node_put(vtg_np);
255d219673dSBenjamin Gaignard
256d219673dSBenjamin Gaignard platform_set_drvdata(pdev, compo);
257d219673dSBenjamin Gaignard
258d219673dSBenjamin Gaignard return component_add(&pdev->dev, &sti_compositor_ops);
259d219673dSBenjamin Gaignard }
260d219673dSBenjamin Gaignard
sti_compositor_remove(struct platform_device * pdev)261*9a865e45SUwe Kleine-König static void sti_compositor_remove(struct platform_device *pdev)
262d219673dSBenjamin Gaignard {
263d219673dSBenjamin Gaignard component_del(&pdev->dev, &sti_compositor_ops);
264d219673dSBenjamin Gaignard }
265d219673dSBenjamin Gaignard
266dcec16efSThierry Reding struct platform_driver sti_compositor_driver = {
267d219673dSBenjamin Gaignard .driver = {
268d219673dSBenjamin Gaignard .name = "sti-compositor",
269d219673dSBenjamin Gaignard .of_match_table = compositor_of_match,
270d219673dSBenjamin Gaignard },
271d219673dSBenjamin Gaignard .probe = sti_compositor_probe,
272*9a865e45SUwe Kleine-König .remove_new = sti_compositor_remove,
273d219673dSBenjamin Gaignard };
274d219673dSBenjamin Gaignard
275d219673dSBenjamin Gaignard MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
276d219673dSBenjamin Gaignard MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
277d219673dSBenjamin Gaignard MODULE_LICENSE("GPL");
278