blob: af62b24c39b1b249a295e9a9948b7b653cbb989b [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
4 *
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
13#include <linux/component.h>
14#include <linux/of_graph.h>
15
16#include <drm/drmP.h>
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_fb_cma_helper.h>
19#include <drm/drm_gem_cma_helper.h>
20
21#include "sun4i_crtc.h"
22#include "sun4i_drv.h"
23#include "sun4i_framebuffer.h"
24#include "sun4i_layer.h"
25#include "sun4i_tcon.h"
26
27static int sun4i_drv_connector_plug_all(struct drm_device *drm)
28{
29 struct drm_connector *connector, *failed;
30 int ret;
31
32 mutex_lock(&drm->mode_config.mutex);
33 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
34 ret = drm_connector_register(connector);
35 if (ret) {
36 failed = connector;
37 goto err;
38 }
39 }
40 mutex_unlock(&drm->mode_config.mutex);
41 return 0;
42
43err:
44 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
45 if (failed == connector)
46 break;
47
48 drm_connector_unregister(connector);
49 }
50 mutex_unlock(&drm->mode_config.mutex);
51
52 return ret;
53}
54
55static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
56{
57 struct sun4i_drv *drv = drm->dev_private;
58 struct sun4i_tcon *tcon = drv->tcon;
59
60 DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
61
62 sun4i_tcon_enable_vblank(tcon, true);
63
64 return 0;
65}
66
67static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
68{
69 struct sun4i_drv *drv = drm->dev_private;
70 struct sun4i_tcon *tcon = drv->tcon;
71
72 DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
73
74 sun4i_tcon_enable_vblank(tcon, false);
75}
76
77static const struct file_operations sun4i_drv_fops = {
78 .owner = THIS_MODULE,
79 .open = drm_open,
80 .release = drm_release,
81 .unlocked_ioctl = drm_ioctl,
82#ifdef CONFIG_COMPAT
83 .compat_ioctl = drm_compat_ioctl,
84#endif
85 .poll = drm_poll,
86 .read = drm_read,
87 .llseek = no_llseek,
88 .mmap = drm_gem_cma_mmap,
89};
90
91static struct drm_driver sun4i_drv_driver = {
92 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
93
94 /* Generic Operations */
95 .fops = &sun4i_drv_fops,
96 .name = "sun4i-drm",
97 .desc = "Allwinner sun4i Display Engine",
98 .date = "20150629",
99 .major = 1,
100 .minor = 0,
101
102 /* GEM Operations */
103 .dumb_create = drm_gem_cma_dumb_create,
104 .dumb_destroy = drm_gem_dumb_destroy,
105 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
106 .gem_free_object = drm_gem_cma_free_object,
107 .gem_vm_ops = &drm_gem_cma_vm_ops,
108
109 /* PRIME Operations */
110 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
111 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
112 .gem_prime_import = drm_gem_prime_import,
113 .gem_prime_export = drm_gem_prime_export,
114 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
115 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
116 .gem_prime_vmap = drm_gem_cma_prime_vmap,
117 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
118 .gem_prime_mmap = drm_gem_cma_prime_mmap,
119
120 /* Frame Buffer Operations */
121
122 /* VBlank Operations */
123 .get_vblank_counter = drm_vblank_count,
124 .enable_vblank = sun4i_drv_enable_vblank,
125 .disable_vblank = sun4i_drv_disable_vblank,
126};
127
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200128static void sun4i_remove_framebuffers(void)
129{
130 struct apertures_struct *ap;
131
132 ap = alloc_apertures(1);
133 if (!ap)
134 return;
135
136 /* The framebuffer can be located anywhere in RAM */
137 ap->ranges[0].base = 0;
138 ap->ranges[0].size = ~0;
139
140 remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
141 kfree(ap);
142}
143
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100144static int sun4i_drv_bind(struct device *dev)
145{
146 struct drm_device *drm;
147 struct sun4i_drv *drv;
148 int ret;
149
150 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
151 if (!drm)
152 return -ENOMEM;
153
154 ret = drm_dev_set_unique(drm, dev_name(drm->dev));
155 if (ret)
156 goto free_drm;
157
158 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
159 if (!drv) {
160 ret = -ENOMEM;
161 goto free_drm;
162 }
163 drm->dev_private = drv;
164
165 drm_vblank_init(drm, 1);
166 drm_mode_config_init(drm);
167
168 ret = component_bind_all(drm->dev, drm);
169 if (ret) {
170 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
171 goto free_drm;
172 }
173
174 /* Create our layers */
175 drv->layers = sun4i_layers_init(drm);
176 if (!drv->layers) {
177 dev_err(drm->dev, "Couldn't create the planes\n");
178 ret = -EINVAL;
179 goto free_drm;
180 }
181
182 /* Create our CRTC */
183 drv->crtc = sun4i_crtc_init(drm);
184 if (!drv->crtc) {
185 dev_err(drm->dev, "Couldn't create the CRTC\n");
186 ret = -EINVAL;
187 goto free_drm;
188 }
189 drm->irq_enabled = true;
190
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200191 /* Remove early framebuffers (ie. simplefb) */
192 sun4i_remove_framebuffers();
193
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100194 /* Create our framebuffer */
195 drv->fbdev = sun4i_framebuffer_init(drm);
196 if (IS_ERR(drv->fbdev)) {
197 dev_err(drm->dev, "Couldn't create our framebuffer\n");
198 ret = PTR_ERR(drv->fbdev);
199 goto free_drm;
200 }
201
202 /* Enable connectors polling */
203 drm_kms_helper_poll_init(drm);
204
205 ret = drm_dev_register(drm, 0);
206 if (ret)
207 goto free_drm;
208
209 ret = sun4i_drv_connector_plug_all(drm);
210 if (ret)
211 goto unregister_drm;
212
213 return 0;
214
215unregister_drm:
216 drm_dev_unregister(drm);
217free_drm:
218 drm_dev_unref(drm);
219 return ret;
220}
221
222static void sun4i_drv_unbind(struct device *dev)
223{
224 struct drm_device *drm = dev_get_drvdata(dev);
225
226 drm_dev_unregister(drm);
227 drm_kms_helper_poll_fini(drm);
228 sun4i_framebuffer_free(drm);
229 drm_vblank_cleanup(drm);
230 drm_dev_unref(drm);
231}
232
233static const struct component_master_ops sun4i_drv_master_ops = {
234 .bind = sun4i_drv_bind,
235 .unbind = sun4i_drv_unbind,
236};
237
238static bool sun4i_drv_node_is_frontend(struct device_node *node)
239{
240 return of_device_is_compatible(node,
241 "allwinner,sun5i-a13-display-frontend");
242}
243
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100244static bool sun4i_drv_node_is_tcon(struct device_node *node)
245{
246 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon");
247}
248
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100249static int compare_of(struct device *dev, void *data)
250{
251 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
252 of_node_full_name(dev->of_node),
253 of_node_full_name(data));
254
255 return dev->of_node == data;
256}
257
258static int sun4i_drv_add_endpoints(struct device *dev,
259 struct component_match **match,
260 struct device_node *node)
261{
262 struct device_node *port, *ep, *remote;
263 int count = 0;
264
265 /*
266 * We don't support the frontend for now, so we will never
267 * have a device bound. Just skip over it, but we still want
268 * the rest our pipeline to be added.
269 */
270 if (!sun4i_drv_node_is_frontend(node) &&
271 !of_device_is_available(node))
272 return 0;
273
274 if (!sun4i_drv_node_is_frontend(node)) {
275 /* Add current component */
276 DRM_DEBUG_DRIVER("Adding component %s\n",
277 of_node_full_name(node));
278 component_match_add(dev, match, compare_of, node);
279 count++;
280 }
281
282 /* Inputs are listed first, then outputs */
283 port = of_graph_get_port_by_id(node, 1);
284 if (!port) {
285 DRM_DEBUG_DRIVER("No output to bind\n");
286 return count;
287 }
288
289 for_each_available_child_of_node(port, ep) {
290 remote = of_graph_get_remote_port_parent(ep);
291 if (!remote) {
292 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
293 of_node_put(remote);
294 continue;
295 }
296
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100297 /*
298 * If the node is our TCON, the first port is used for our
299 * panel, and will not be part of the
300 * component framework.
301 */
302 if (sun4i_drv_node_is_tcon(node)) {
303 struct of_endpoint endpoint;
304
305 if (of_graph_parse_endpoint(ep, &endpoint)) {
306 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
307 continue;
308 }
309
310 if (!endpoint.id) {
311 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
312 continue;
313 }
314 }
315
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100316 /* Walk down our tree */
317 count += sun4i_drv_add_endpoints(dev, match, remote);
318
319 of_node_put(remote);
320 }
321
322 return count;
323}
324
325static int sun4i_drv_probe(struct platform_device *pdev)
326{
327 struct component_match *match = NULL;
328 struct device_node *np = pdev->dev.of_node;
329 int i, count = 0;
330
331 for (i = 0;; i++) {
332 struct device_node *pipeline = of_parse_phandle(np,
333 "allwinner,pipelines",
334 i);
335 if (!pipeline)
336 break;
337
338 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
339 pipeline);
340
341 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
342 count, i);
343 }
344
345 if (count)
346 return component_master_add_with_match(&pdev->dev,
347 &sun4i_drv_master_ops,
348 match);
349 else
350 return 0;
351}
352
353static int sun4i_drv_remove(struct platform_device *pdev)
354{
355 return 0;
356}
357
358static const struct of_device_id sun4i_drv_of_table[] = {
359 { .compatible = "allwinner,sun5i-a13-display-engine" },
360 { }
361};
362MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
363
364static struct platform_driver sun4i_drv_platform_driver = {
365 .probe = sun4i_drv_probe,
366 .remove = sun4i_drv_remove,
367 .driver = {
368 .name = "sun4i-drm",
369 .of_match_table = sun4i_drv_of_table,
370 },
371};
372module_platform_driver(sun4i_drv_platform_driver);
373
374MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
375MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
376MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
377MODULE_LICENSE("GPL");