1 /*
2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2014 Endless Mobile
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Written by:
20 * Jasper St. Pierre <jstpierre@mecheye.net>
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 * DOC: Video Processing Unit
58 *
59 * VPU Handles the Global Video Processing, it includes management of the
60 * clocks gates, blocks reset lines and power domains.
61 *
62 * What is missing :
63 *
64 * - Full reset of entire video processing HW blocks
65 * - Scaling and setup of the VPU clock
66 * - Bus clock gates
67 * - Powering up video processing HW blocks
68 * - Powering Up HDMI controller and PHY
69 */
70
meson_fb_output_poll_changed(struct drm_device * dev)71 static 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
78 static 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
85 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = {
86 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
87 };
88
meson_irq(int irq,void * arg)89 static irqreturn_t meson_irq(int irq, void *arg)
90 {
91 struct drm_device *dev = arg;
92 struct meson_drm *priv = dev->dev_private;
93
94 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
95
96 meson_crtc_irq(priv);
97
98 return IRQ_HANDLED;
99 }
100
101 DEFINE_DRM_GEM_CMA_FOPS(fops);
102
103 static struct drm_driver meson_driver = {
104 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
105 DRIVER_MODESET | DRIVER_PRIME |
106 DRIVER_ATOMIC,
107
108 /* IRQ */
109 .irq_handler = meson_irq,
110
111 /* PRIME Ops */
112 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
113 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
114 .gem_prime_import = drm_gem_prime_import,
115 .gem_prime_export = drm_gem_prime_export,
116 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
117 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
118 .gem_prime_vmap = drm_gem_cma_prime_vmap,
119 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
120 .gem_prime_mmap = drm_gem_cma_prime_mmap,
121
122 /* GEM Ops */
123 .dumb_create = drm_gem_cma_dumb_create,
124 .gem_free_object_unlocked = drm_gem_cma_free_object,
125 .gem_vm_ops = &drm_gem_cma_vm_ops,
126
127 /* Misc */
128 .fops = &fops,
129 .name = DRIVER_NAME,
130 .desc = DRIVER_DESC,
131 .date = "20161109",
132 .major = 1,
133 .minor = 0,
134 };
135
meson_vpu_has_available_connectors(struct device * dev)136 static bool meson_vpu_has_available_connectors(struct device *dev)
137 {
138 struct device_node *ep, *remote;
139
140 /* Parses each endpoint and check if remote exists */
141 for_each_endpoint_of_node(dev->of_node, ep) {
142 /* If the endpoint node exists, consider it enabled */
143 remote = of_graph_get_remote_port(ep);
144 if (remote)
145 return true;
146 }
147
148 return false;
149 }
150
151 static struct regmap_config meson_regmap_config = {
152 .reg_bits = 32,
153 .val_bits = 32,
154 .reg_stride = 4,
155 .max_register = 0x1000,
156 };
157
meson_vpu_init(struct meson_drm * priv)158 static void meson_vpu_init(struct meson_drm *priv)
159 {
160 writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
161 writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
162 writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
163 writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
164 }
165
meson_drv_bind_master(struct device * dev,bool has_components)166 static int meson_drv_bind_master(struct device *dev, bool has_components)
167 {
168 struct platform_device *pdev = to_platform_device(dev);
169 struct meson_drm *priv;
170 struct drm_device *drm;
171 struct resource *res;
172 void __iomem *regs;
173 int ret;
174
175 /* Checks if an output connector is available */
176 if (!meson_vpu_has_available_connectors(dev)) {
177 dev_err(dev, "No output connector available\n");
178 return -ENODEV;
179 }
180
181 drm = drm_dev_alloc(&meson_driver, dev);
182 if (IS_ERR(drm))
183 return PTR_ERR(drm);
184
185 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
186 if (!priv) {
187 ret = -ENOMEM;
188 goto free_drm;
189 }
190 drm->dev_private = priv;
191 priv->drm = drm;
192 priv->dev = dev;
193
194 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
195 regs = devm_ioremap_resource(dev, res);
196 if (IS_ERR(regs)) {
197 ret = PTR_ERR(regs);
198 goto free_drm;
199 }
200
201 priv->io_base = regs;
202
203 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
204 if (!res) {
205 ret = -EINVAL;
206 goto free_drm;
207 }
208 /* Simply ioremap since it may be a shared register zone */
209 regs = devm_ioremap(dev, res->start, resource_size(res));
210 if (!regs) {
211 ret = -EADDRNOTAVAIL;
212 goto free_drm;
213 }
214
215 priv->hhi = devm_regmap_init_mmio(dev, regs,
216 &meson_regmap_config);
217 if (IS_ERR(priv->hhi)) {
218 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
219 ret = PTR_ERR(priv->hhi);
220 goto free_drm;
221 }
222
223 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
224 if (!res) {
225 ret = -EINVAL;
226 goto free_drm;
227 }
228 /* Simply ioremap since it may be a shared register zone */
229 regs = devm_ioremap(dev, res->start, resource_size(res));
230 if (!regs) {
231 ret = -EADDRNOTAVAIL;
232 goto free_drm;
233 }
234
235 priv->dmc = devm_regmap_init_mmio(dev, regs,
236 &meson_regmap_config);
237 if (IS_ERR(priv->dmc)) {
238 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
239 ret = PTR_ERR(priv->dmc);
240 goto free_drm;
241 }
242
243 priv->vsync_irq = platform_get_irq(pdev, 0);
244
245 ret = drm_vblank_init(drm, 1);
246 if (ret)
247 goto free_drm;
248
249 drm_mode_config_init(drm);
250 drm->mode_config.max_width = 3840;
251 drm->mode_config.max_height = 2160;
252 drm->mode_config.funcs = &meson_mode_config_funcs;
253 drm->mode_config.helper_private = &meson_mode_config_helpers;
254
255 /* Hardware Initialization */
256
257 meson_vpu_init(priv);
258 meson_venc_init(priv);
259 meson_vpp_init(priv);
260 meson_viu_init(priv);
261
262 /* Encoder Initialization */
263
264 ret = meson_venc_cvbs_create(priv);
265 if (ret)
266 goto free_drm;
267
268 if (has_components) {
269 ret = component_bind_all(drm->dev, drm);
270 if (ret) {
271 dev_err(drm->dev, "Couldn't bind all components\n");
272 goto free_drm;
273 }
274 }
275
276 ret = meson_plane_create(priv);
277 if (ret)
278 goto free_drm;
279
280 ret = meson_crtc_create(priv);
281 if (ret)
282 goto free_drm;
283
284 ret = drm_irq_install(drm, priv->vsync_irq);
285 if (ret)
286 goto free_drm;
287
288 drm_mode_config_reset(drm);
289
290 priv->fbdev = drm_fbdev_cma_init(drm, 32,
291 drm->mode_config.num_connector);
292 if (IS_ERR(priv->fbdev)) {
293 ret = PTR_ERR(priv->fbdev);
294 goto free_drm;
295 }
296
297 drm_kms_helper_poll_init(drm);
298
299 platform_set_drvdata(pdev, priv);
300
301 ret = drm_dev_register(drm, 0);
302 if (ret)
303 goto uninstall_irq;
304
305 return 0;
306
307 uninstall_irq:
308 drm_irq_uninstall(drm);
309 free_drm:
310 drm_dev_put(drm);
311
312 return ret;
313 }
314
meson_drv_bind(struct device * dev)315 static int meson_drv_bind(struct device *dev)
316 {
317 return meson_drv_bind_master(dev, true);
318 }
319
meson_drv_unbind(struct device * dev)320 static void meson_drv_unbind(struct device *dev)
321 {
322 struct meson_drm *priv = dev_get_drvdata(dev);
323 struct drm_device *drm = priv->drm;
324
325 drm_dev_unregister(drm);
326 drm_irq_uninstall(drm);
327 drm_kms_helper_poll_fini(drm);
328 drm_fbdev_cma_fini(priv->fbdev);
329 drm_mode_config_cleanup(drm);
330 drm_dev_put(drm);
331
332 }
333
334 static const struct component_master_ops meson_drv_master_ops = {
335 .bind = meson_drv_bind,
336 .unbind = meson_drv_unbind,
337 };
338
compare_of(struct device * dev,void * data)339 static int compare_of(struct device *dev, void *data)
340 {
341 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
342 dev->of_node, data);
343
344 return dev->of_node == data;
345 }
346
347 /* Possible connectors nodes to ignore */
348 static const struct of_device_id connectors_match[] = {
349 { .compatible = "composite-video-connector" },
350 { .compatible = "svideo-connector" },
351 { .compatible = "hdmi-connector" },
352 { .compatible = "dvi-connector" },
353 {}
354 };
355
meson_probe_remote(struct platform_device * pdev,struct component_match ** match,struct device_node * parent,struct device_node * remote)356 static int meson_probe_remote(struct platform_device *pdev,
357 struct component_match **match,
358 struct device_node *parent,
359 struct device_node *remote)
360 {
361 struct device_node *ep, *remote_node;
362 int count = 1;
363
364 /* If node is a connector, return and do not add to match table */
365 if (of_match_node(connectors_match, remote))
366 return 1;
367
368 component_match_add(&pdev->dev, match, compare_of, remote);
369
370 for_each_endpoint_of_node(remote, ep) {
371 remote_node = of_graph_get_remote_port_parent(ep);
372 if (!remote_node ||
373 remote_node == parent || /* Ignore parent endpoint */
374 !of_device_is_available(remote_node)) {
375 of_node_put(remote_node);
376 continue;
377 }
378
379 count += meson_probe_remote(pdev, match, remote, remote_node);
380
381 of_node_put(remote_node);
382 }
383
384 return count;
385 }
386
meson_drv_probe(struct platform_device * pdev)387 static int meson_drv_probe(struct platform_device *pdev)
388 {
389 struct component_match *match = NULL;
390 struct device_node *np = pdev->dev.of_node;
391 struct device_node *ep, *remote;
392 int count = 0;
393
394 for_each_endpoint_of_node(np, ep) {
395 remote = of_graph_get_remote_port_parent(ep);
396 if (!remote || !of_device_is_available(remote)) {
397 of_node_put(remote);
398 continue;
399 }
400
401 count += meson_probe_remote(pdev, &match, np, remote);
402 of_node_put(remote);
403 }
404
405 if (count && !match)
406 return meson_drv_bind_master(&pdev->dev, false);
407
408 /* If some endpoints were found, initialize the nodes */
409 if (count) {
410 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
411
412 return component_master_add_with_match(&pdev->dev,
413 &meson_drv_master_ops,
414 match);
415 }
416
417 /* If no output endpoints were available, simply bail out */
418 return 0;
419 };
420
421 static const struct of_device_id dt_match[] = {
422 { .compatible = "amlogic,meson-gxbb-vpu" },
423 { .compatible = "amlogic,meson-gxl-vpu" },
424 { .compatible = "amlogic,meson-gxm-vpu" },
425 {}
426 };
427 MODULE_DEVICE_TABLE(of, dt_match);
428
429 static struct platform_driver meson_drm_platform_driver = {
430 .probe = meson_drv_probe,
431 .driver = {
432 .name = "meson-drm",
433 .of_match_table = dt_match,
434 },
435 };
436
437 module_platform_driver(meson_drm_platform_driver);
438
439 MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
440 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
441 MODULE_DESCRIPTION(DRIVER_DESC);
442 MODULE_LICENSE("GPL");
443