Commit | Line | Data |
---|---|---|
bbbe775e NA |
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> | |
a41e82e6 | 27 | #include <linux/component.h> |
bbbe775e NA |
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> | |
24ef8157 | 37 | #include <drm/drm_gem_framebuffer_helper.h> |
bbbe775e NA |
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 | ||
2021d5b7 NA |
56 | /** |
57 | * DOC: Video Processing Unit | |
bbbe775e NA |
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 : | |
2021d5b7 | 63 | * |
bbbe775e NA |
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 | ||
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, | |
24ef8157 | 82 | .fb_create = drm_gem_fb_create, |
bbbe775e NA |
83 | }; |
84 | ||
bbbe775e NA |
85 | static 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 | ||
d55f7e5d | 97 | DEFINE_DRM_GEM_CMA_FOPS(fops); |
bbbe775e NA |
98 | |
99 | static struct drm_driver meson_driver = { | |
100 | .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | | |
101 | DRIVER_MODESET | DRIVER_PRIME | | |
102 | DRIVER_ATOMIC, | |
103 | ||
bbbe775e NA |
104 | /* IRQ */ |
105 | .irq_handler = meson_irq, | |
106 | ||
107 | /* PRIME Ops */ | |
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 | /* GEM Ops */ | |
119 | .dumb_create = drm_gem_cma_dumb_create, | |
bbbe775e NA |
120 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
121 | .gem_vm_ops = &drm_gem_cma_vm_ops, | |
122 | ||
123 | /* Misc */ | |
124 | .fops = &fops, | |
125 | .name = DRIVER_NAME, | |
126 | .desc = DRIVER_DESC, | |
127 | .date = "20161109", | |
128 | .major = 1, | |
129 | .minor = 0, | |
130 | }; | |
131 | ||
132 | static bool meson_vpu_has_available_connectors(struct device *dev) | |
133 | { | |
134 | struct device_node *ep, *remote; | |
135 | ||
136 | /* Parses each endpoint and check if remote exists */ | |
137 | for_each_endpoint_of_node(dev->of_node, ep) { | |
138 | /* If the endpoint node exists, consider it enabled */ | |
139 | remote = of_graph_get_remote_port(ep); | |
140 | if (remote) | |
141 | return true; | |
142 | } | |
143 | ||
144 | return false; | |
145 | } | |
146 | ||
147 | static struct regmap_config meson_regmap_config = { | |
148 | .reg_bits = 32, | |
149 | .val_bits = 32, | |
150 | .reg_stride = 4, | |
151 | .max_register = 0x1000, | |
152 | }; | |
153 | ||
09762525 NA |
154 | static 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 | ||
8604889f | 162 | static int meson_drv_bind_master(struct device *dev, bool has_components) |
bbbe775e | 163 | { |
a41e82e6 | 164 | struct platform_device *pdev = to_platform_device(dev); |
bbbe775e NA |
165 | struct meson_drm *priv; |
166 | struct drm_device *drm; | |
167 | struct resource *res; | |
168 | void __iomem *regs; | |
169 | int ret; | |
170 | ||
171 | /* Checks if an output connector is available */ | |
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 | return PTR_ERR(regs); | |
194 | ||
195 | priv->io_base = regs; | |
196 | ||
197 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi"); | |
198 | /* Simply ioremap since it may be a shared register zone */ | |
199 | regs = devm_ioremap(dev, res->start, resource_size(res)); | |
200 | if (!regs) | |
201 | return -EADDRNOTAVAIL; | |
202 | ||
203 | priv->hhi = devm_regmap_init_mmio(dev, regs, | |
204 | &meson_regmap_config); | |
205 | if (IS_ERR(priv->hhi)) { | |
206 | dev_err(&pdev->dev, "Couldn't create the HHI regmap\n"); | |
207 | return PTR_ERR(priv->hhi); | |
208 | } | |
209 | ||
210 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc"); | |
211 | /* Simply ioremap since it may be a shared register zone */ | |
212 | regs = devm_ioremap(dev, res->start, resource_size(res)); | |
213 | if (!regs) | |
214 | return -EADDRNOTAVAIL; | |
215 | ||
216 | priv->dmc = devm_regmap_init_mmio(dev, regs, | |
217 | &meson_regmap_config); | |
218 | if (IS_ERR(priv->dmc)) { | |
219 | dev_err(&pdev->dev, "Couldn't create the DMC regmap\n"); | |
220 | return PTR_ERR(priv->dmc); | |
221 | } | |
222 | ||
223 | priv->vsync_irq = platform_get_irq(pdev, 0); | |
224 | ||
e770f6bf CJ |
225 | ret = drm_vblank_init(drm, 1); |
226 | if (ret) | |
227 | goto free_drm; | |
228 | ||
bbbe775e | 229 | drm_mode_config_init(drm); |
a41e82e6 NA |
230 | drm->mode_config.max_width = 3840; |
231 | drm->mode_config.max_height = 2160; | |
232 | drm->mode_config.funcs = &meson_mode_config_funcs; | |
233 | ||
234 | /* Hardware Initialization */ | |
235 | ||
09762525 | 236 | meson_vpu_init(priv); |
a41e82e6 NA |
237 | meson_venc_init(priv); |
238 | meson_vpp_init(priv); | |
239 | meson_viu_init(priv); | |
bbbe775e NA |
240 | |
241 | /* Encoder Initialization */ | |
242 | ||
243 | ret = meson_venc_cvbs_create(priv); | |
244 | if (ret) | |
245 | goto free_drm; | |
246 | ||
8604889f NA |
247 | if (has_components) { |
248 | ret = component_bind_all(drm->dev, drm); | |
249 | if (ret) { | |
250 | dev_err(drm->dev, "Couldn't bind all components\n"); | |
251 | goto free_drm; | |
252 | } | |
a41e82e6 | 253 | } |
bbbe775e NA |
254 | |
255 | ret = meson_plane_create(priv); | |
256 | if (ret) | |
257 | goto free_drm; | |
258 | ||
259 | ret = meson_crtc_create(priv); | |
260 | if (ret) | |
261 | goto free_drm; | |
262 | ||
263 | ret = drm_irq_install(drm, priv->vsync_irq); | |
264 | if (ret) | |
265 | goto free_drm; | |
266 | ||
267 | drm_mode_config_reset(drm); | |
bbbe775e NA |
268 | |
269 | priv->fbdev = drm_fbdev_cma_init(drm, 32, | |
bbbe775e NA |
270 | drm->mode_config.num_connector); |
271 | if (IS_ERR(priv->fbdev)) { | |
272 | ret = PTR_ERR(priv->fbdev); | |
273 | goto free_drm; | |
274 | } | |
275 | ||
276 | drm_kms_helper_poll_init(drm); | |
277 | ||
278 | platform_set_drvdata(pdev, priv); | |
279 | ||
280 | ret = drm_dev_register(drm, 0); | |
281 | if (ret) | |
282 | goto free_drm; | |
283 | ||
284 | return 0; | |
285 | ||
286 | free_drm: | |
287 | drm_dev_unref(drm); | |
288 | ||
289 | return ret; | |
290 | } | |
291 | ||
8604889f NA |
292 | static int meson_drv_bind(struct device *dev) |
293 | { | |
294 | return meson_drv_bind_master(dev, true); | |
295 | } | |
296 | ||
a41e82e6 | 297 | static void meson_drv_unbind(struct device *dev) |
bbbe775e | 298 | { |
a41e82e6 | 299 | struct drm_device *drm = dev_get_drvdata(dev); |
bbbe775e NA |
300 | struct meson_drm *priv = drm->dev_private; |
301 | ||
302 | drm_dev_unregister(drm); | |
303 | drm_kms_helper_poll_fini(drm); | |
304 | drm_fbdev_cma_fini(priv->fbdev); | |
305 | drm_mode_config_cleanup(drm); | |
bbbe775e NA |
306 | drm_dev_unref(drm); |
307 | ||
bbbe775e NA |
308 | } |
309 | ||
a41e82e6 NA |
310 | static const struct component_master_ops meson_drv_master_ops = { |
311 | .bind = meson_drv_bind, | |
312 | .unbind = meson_drv_unbind, | |
313 | }; | |
314 | ||
315 | static int compare_of(struct device *dev, void *data) | |
316 | { | |
4bf99144 RH |
317 | DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n", |
318 | dev->of_node, data); | |
a41e82e6 NA |
319 | |
320 | return dev->of_node == data; | |
321 | } | |
322 | ||
323 | /* Possible connectors nodes to ignore */ | |
324 | static const struct of_device_id connectors_match[] = { | |
325 | { .compatible = "composite-video-connector" }, | |
326 | { .compatible = "svideo-connector" }, | |
327 | { .compatible = "hdmi-connector" }, | |
328 | { .compatible = "dvi-connector" }, | |
329 | {} | |
330 | }; | |
331 | ||
332 | static int meson_probe_remote(struct platform_device *pdev, | |
333 | struct component_match **match, | |
334 | struct device_node *parent, | |
335 | struct device_node *remote) | |
336 | { | |
337 | struct device_node *ep, *remote_node; | |
338 | int count = 1; | |
339 | ||
340 | /* If node is a connector, return and do not add to match table */ | |
341 | if (of_match_node(connectors_match, remote)) | |
342 | return 1; | |
343 | ||
344 | component_match_add(&pdev->dev, match, compare_of, remote); | |
345 | ||
346 | for_each_endpoint_of_node(remote, ep) { | |
347 | remote_node = of_graph_get_remote_port_parent(ep); | |
348 | if (!remote_node || | |
349 | remote_node == parent || /* Ignore parent endpoint */ | |
350 | !of_device_is_available(remote_node)) | |
351 | continue; | |
352 | ||
353 | count += meson_probe_remote(pdev, match, remote, remote_node); | |
354 | ||
355 | of_node_put(remote_node); | |
356 | } | |
357 | ||
358 | return count; | |
359 | } | |
360 | ||
361 | static int meson_drv_probe(struct platform_device *pdev) | |
362 | { | |
363 | struct component_match *match = NULL; | |
364 | struct device_node *np = pdev->dev.of_node; | |
365 | struct device_node *ep, *remote; | |
366 | int count = 0; | |
367 | ||
368 | for_each_endpoint_of_node(np, ep) { | |
369 | remote = of_graph_get_remote_port_parent(ep); | |
370 | if (!remote || !of_device_is_available(remote)) | |
371 | continue; | |
372 | ||
373 | count += meson_probe_remote(pdev, &match, np, remote); | |
374 | } | |
375 | ||
8604889f NA |
376 | if (count && !match) |
377 | return meson_drv_bind_master(&pdev->dev, false); | |
378 | ||
a41e82e6 NA |
379 | /* If some endpoints were found, initialize the nodes */ |
380 | if (count) { | |
381 | dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count); | |
382 | ||
383 | return component_master_add_with_match(&pdev->dev, | |
384 | &meson_drv_master_ops, | |
385 | match); | |
386 | } | |
387 | ||
388 | /* If no output endpoints were available, simply bail out */ | |
389 | return 0; | |
390 | }; | |
391 | ||
bbbe775e NA |
392 | static const struct of_device_id dt_match[] = { |
393 | { .compatible = "amlogic,meson-gxbb-vpu" }, | |
394 | { .compatible = "amlogic,meson-gxl-vpu" }, | |
395 | { .compatible = "amlogic,meson-gxm-vpu" }, | |
396 | {} | |
397 | }; | |
398 | MODULE_DEVICE_TABLE(of, dt_match); | |
399 | ||
400 | static struct platform_driver meson_drm_platform_driver = { | |
401 | .probe = meson_drv_probe, | |
bbbe775e | 402 | .driver = { |
8aaacbc0 | 403 | .name = "meson-drm", |
bbbe775e NA |
404 | .of_match_table = dt_match, |
405 | }, | |
406 | }; | |
407 | ||
408 | module_platform_driver(meson_drm_platform_driver); | |
409 | ||
410 | MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>"); | |
411 | MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); | |
412 | MODULE_DESCRIPTION(DRIVER_DESC); | |
413 | MODULE_LICENSE("GPL"); |