1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/platform_device.h>
27#include <linux/component.h>
28#include <linux/of_graph.h>
29
30#include <drm/drmP.h>
31#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33#include <drm/drm_flip_work.h>
34#include <drm/drm_crtc_helper.h>
35#include <drm/drm_plane_helper.h>
36#include <drm/drm_gem_cma_helper.h>
37#include <drm/drm_gem_framebuffer_helper.h>
38#include <drm/drm_fb_cma_helper.h>
39#include <drm/drm_rect.h>
40#include <drm/drm_fb_helper.h>
41
42#include "meson_drv.h"
43#include "meson_plane.h"
44#include "meson_crtc.h"
45#include "meson_venc_cvbs.h"
46
47#include "meson_vpp.h"
48#include "meson_viu.h"
49#include "meson_venc.h"
50#include "meson_canvas.h"
51#include "meson_registers.h"
52
53#define DRIVER_NAME "meson"
54#define DRIVER_DESC "Amlogic Meson DRM driver"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71static void meson_fb_output_poll_changed(struct drm_device *dev)
72{
73 struct meson_drm *priv = dev->dev_private;
74
75 drm_fbdev_cma_hotplug_event(priv->fbdev);
76}
77
78static const struct drm_mode_config_funcs meson_mode_config_funcs = {
79 .output_poll_changed = meson_fb_output_poll_changed,
80 .atomic_check = drm_atomic_helper_check,
81 .atomic_commit = drm_atomic_helper_commit,
82 .fb_create = drm_gem_fb_create,
83};
84
85static irqreturn_t meson_irq(int irq, void *arg)
86{
87 struct drm_device *dev = arg;
88 struct meson_drm *priv = dev->dev_private;
89
90 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
91
92 meson_crtc_irq(priv);
93
94 return IRQ_HANDLED;
95}
96
97DEFINE_DRM_GEM_CMA_FOPS(fops);
98
99static struct drm_driver meson_driver = {
100 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
101 DRIVER_MODESET | DRIVER_PRIME |
102 DRIVER_ATOMIC,
103
104
105 .irq_handler = meson_irq,
106
107
108 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
109 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
110 .gem_prime_import = drm_gem_prime_import,
111 .gem_prime_export = drm_gem_prime_export,
112 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
113 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
114 .gem_prime_vmap = drm_gem_cma_prime_vmap,
115 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
116 .gem_prime_mmap = drm_gem_cma_prime_mmap,
117
118
119 .dumb_create = drm_gem_cma_dumb_create,
120 .gem_free_object_unlocked = drm_gem_cma_free_object,
121 .gem_vm_ops = &drm_gem_cma_vm_ops,
122
123
124 .fops = &fops,
125 .name = DRIVER_NAME,
126 .desc = DRIVER_DESC,
127 .date = "20161109",
128 .major = 1,
129 .minor = 0,
130};
131
132static bool meson_vpu_has_available_connectors(struct device *dev)
133{
134 struct device_node *ep, *remote;
135
136
137 for_each_endpoint_of_node(dev->of_node, ep) {
138
139 remote = of_graph_get_remote_port(ep);
140 if (remote)
141 return true;
142 }
143
144 return false;
145}
146
147static struct regmap_config meson_regmap_config = {
148 .reg_bits = 32,
149 .val_bits = 32,
150 .reg_stride = 4,
151 .max_register = 0x1000,
152};
153
154static void meson_vpu_init(struct meson_drm *priv)
155{
156 writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
157 writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
158 writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
159 writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
160}
161
162static int meson_drv_bind_master(struct device *dev, bool has_components)
163{
164 struct platform_device *pdev = to_platform_device(dev);
165 struct meson_drm *priv;
166 struct drm_device *drm;
167 struct resource *res;
168 void __iomem *regs;
169 int ret;
170
171
172 if (!meson_vpu_has_available_connectors(dev)) {
173 dev_err(dev, "No output connector available\n");
174 return -ENODEV;
175 }
176
177 drm = drm_dev_alloc(&meson_driver, dev);
178 if (IS_ERR(drm))
179 return PTR_ERR(drm);
180
181 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
182 if (!priv) {
183 ret = -ENOMEM;
184 goto free_drm;
185 }
186 drm->dev_private = priv;
187 priv->drm = drm;
188 priv->dev = dev;
189
190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
191 regs = devm_ioremap_resource(dev, res);
192 if (IS_ERR(regs)) {
193 ret = PTR_ERR(regs);
194 goto free_drm;
195 }
196
197 priv->io_base = regs;
198
199 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
200 if (!res) {
201 ret = -EINVAL;
202 goto free_drm;
203 }
204
205 regs = devm_ioremap(dev, res->start, resource_size(res));
206 if (!regs) {
207 ret = -EADDRNOTAVAIL;
208 goto free_drm;
209 }
210
211 priv->hhi = devm_regmap_init_mmio(dev, regs,
212 &meson_regmap_config);
213 if (IS_ERR(priv->hhi)) {
214 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
215 ret = PTR_ERR(priv->hhi);
216 goto free_drm;
217 }
218
219 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
220 if (!res) {
221 ret = -EINVAL;
222 goto free_drm;
223 }
224
225 regs = devm_ioremap(dev, res->start, resource_size(res));
226 if (!regs) {
227 ret = -EADDRNOTAVAIL;
228 goto free_drm;
229 }
230
231 priv->dmc = devm_regmap_init_mmio(dev, regs,
232 &meson_regmap_config);
233 if (IS_ERR(priv->dmc)) {
234 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
235 ret = PTR_ERR(priv->dmc);
236 goto free_drm;
237 }
238
239 priv->vsync_irq = platform_get_irq(pdev, 0);
240
241 ret = drm_vblank_init(drm, 1);
242 if (ret)
243 goto free_drm;
244
245 drm_mode_config_init(drm);
246 drm->mode_config.max_width = 3840;
247 drm->mode_config.max_height = 2160;
248 drm->mode_config.funcs = &meson_mode_config_funcs;
249
250
251
252 meson_vpu_init(priv);
253 meson_venc_init(priv);
254 meson_vpp_init(priv);
255 meson_viu_init(priv);
256
257
258
259 ret = meson_venc_cvbs_create(priv);
260 if (ret)
261 goto free_drm;
262
263 if (has_components) {
264 ret = component_bind_all(drm->dev, drm);
265 if (ret) {
266 dev_err(drm->dev, "Couldn't bind all components\n");
267 goto free_drm;
268 }
269 }
270
271 ret = meson_plane_create(priv);
272 if (ret)
273 goto free_drm;
274
275 ret = meson_crtc_create(priv);
276 if (ret)
277 goto free_drm;
278
279 ret = drm_irq_install(drm, priv->vsync_irq);
280 if (ret)
281 goto free_drm;
282
283 drm_mode_config_reset(drm);
284
285 priv->fbdev = drm_fbdev_cma_init(drm, 32,
286 drm->mode_config.num_connector);
287 if (IS_ERR(priv->fbdev)) {
288 ret = PTR_ERR(priv->fbdev);
289 goto free_drm;
290 }
291
292 drm_kms_helper_poll_init(drm);
293
294 platform_set_drvdata(pdev, priv);
295
296 ret = drm_dev_register(drm, 0);
297 if (ret)
298 goto free_drm;
299
300 return 0;
301
302free_drm:
303 drm_dev_put(drm);
304
305 return ret;
306}
307
308static int meson_drv_bind(struct device *dev)
309{
310 return meson_drv_bind_master(dev, true);
311}
312
313static void meson_drv_unbind(struct device *dev)
314{
315 struct drm_device *drm = dev_get_drvdata(dev);
316 struct meson_drm *priv = drm->dev_private;
317
318 drm_dev_unregister(drm);
319 drm_kms_helper_poll_fini(drm);
320 drm_fbdev_cma_fini(priv->fbdev);
321 drm_mode_config_cleanup(drm);
322 drm_dev_put(drm);
323
324}
325
326static const struct component_master_ops meson_drv_master_ops = {
327 .bind = meson_drv_bind,
328 .unbind = meson_drv_unbind,
329};
330
331static int compare_of(struct device *dev, void *data)
332{
333 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
334 dev->of_node, data);
335
336 return dev->of_node == data;
337}
338
339
340static const struct of_device_id connectors_match[] = {
341 { .compatible = "composite-video-connector" },
342 { .compatible = "svideo-connector" },
343 { .compatible = "hdmi-connector" },
344 { .compatible = "dvi-connector" },
345 {}
346};
347
348static int meson_probe_remote(struct platform_device *pdev,
349 struct component_match **match,
350 struct device_node *parent,
351 struct device_node *remote)
352{
353 struct device_node *ep, *remote_node;
354 int count = 1;
355
356
357 if (of_match_node(connectors_match, remote))
358 return 1;
359
360 component_match_add(&pdev->dev, match, compare_of, remote);
361
362 for_each_endpoint_of_node(remote, ep) {
363 remote_node = of_graph_get_remote_port_parent(ep);
364 if (!remote_node ||
365 remote_node == parent ||
366 !of_device_is_available(remote_node))
367 continue;
368
369 count += meson_probe_remote(pdev, match, remote, remote_node);
370
371 of_node_put(remote_node);
372 }
373
374 return count;
375}
376
377static int meson_drv_probe(struct platform_device *pdev)
378{
379 struct component_match *match = NULL;
380 struct device_node *np = pdev->dev.of_node;
381 struct device_node *ep, *remote;
382 int count = 0;
383
384 for_each_endpoint_of_node(np, ep) {
385 remote = of_graph_get_remote_port_parent(ep);
386 if (!remote || !of_device_is_available(remote))
387 continue;
388
389 count += meson_probe_remote(pdev, &match, np, remote);
390 }
391
392 if (count && !match)
393 return meson_drv_bind_master(&pdev->dev, false);
394
395
396 if (count) {
397 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
398
399 return component_master_add_with_match(&pdev->dev,
400 &meson_drv_master_ops,
401 match);
402 }
403
404
405 return 0;
406};
407
408static const struct of_device_id dt_match[] = {
409 { .compatible = "amlogic,meson-gxbb-vpu" },
410 { .compatible = "amlogic,meson-gxl-vpu" },
411 { .compatible = "amlogic,meson-gxm-vpu" },
412 {}
413};
414MODULE_DEVICE_TABLE(of, dt_match);
415
416static struct platform_driver meson_drm_platform_driver = {
417 .probe = meson_drv_probe,
418 .driver = {
419 .name = "meson-drm",
420 .of_match_table = dt_match,
421 },
422};
423
424module_platform_driver(meson_drm_platform_driver);
425
426MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
427MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
428MODULE_DESCRIPTION(DRIVER_DESC);
429MODULE_LICENSE("GPL");
430