blob: 4a979d17ddaa7d899670056739c7306a5c691c40 [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>
Maxime Ripard596afb62017-02-09 17:39:18 +010015#include <linux/of_reserved_mem.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010016
17#include <drm/drmP.h>
18#include <drm/drm_crtc_helper.h>
19#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_gem_cma_helper.h>
Daniel Vetter44adece2016-08-10 18:52:34 +020021#include <drm/drm_fb_helper.h>
Russell King97ac0e42016-10-19 11:28:27 +010022#include <drm/drm_of.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010023
Maxime Ripard9026e0d2015-10-29 09:36:23 +010024#include "sun4i_drv.h"
25#include "sun4i_framebuffer.h"
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +080026#include "sun4i_tcon.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010027
Daniel Vetterd55f7e52017-03-08 15:12:56 +010028DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010029
30static struct drm_driver sun4i_drv_driver = {
31 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
32
33 /* Generic Operations */
34 .fops = &sun4i_drv_fops,
35 .name = "sun4i-drm",
36 .desc = "Allwinner sun4i Display Engine",
37 .date = "20150629",
38 .major = 1,
39 .minor = 0,
40
41 /* GEM Operations */
42 .dumb_create = drm_gem_cma_dumb_create,
43 .dumb_destroy = drm_gem_dumb_destroy,
44 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
Daniel Vetterae2c6222016-05-30 19:53:15 +020045 .gem_free_object_unlocked = drm_gem_cma_free_object,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010046 .gem_vm_ops = &drm_gem_cma_vm_ops,
47
48 /* PRIME Operations */
49 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
50 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
51 .gem_prime_import = drm_gem_prime_import,
52 .gem_prime_export = drm_gem_prime_export,
53 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
54 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
55 .gem_prime_vmap = drm_gem_cma_prime_vmap,
56 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
57 .gem_prime_mmap = drm_gem_cma_prime_mmap,
58
59 /* Frame Buffer Operations */
Maxime Ripard9026e0d2015-10-29 09:36:23 +010060};
61
Maxime Ripard3d6bd902016-05-11 16:52:50 +020062static void sun4i_remove_framebuffers(void)
63{
64 struct apertures_struct *ap;
65
66 ap = alloc_apertures(1);
67 if (!ap)
68 return;
69
70 /* The framebuffer can be located anywhere in RAM */
71 ap->ranges[0].base = 0;
72 ap->ranges[0].size = ~0;
73
Daniel Vetter44adece2016-08-10 18:52:34 +020074 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
Maxime Ripard3d6bd902016-05-11 16:52:50 +020075 kfree(ap);
76}
77
Maxime Ripard9026e0d2015-10-29 09:36:23 +010078static int sun4i_drv_bind(struct device *dev)
79{
80 struct drm_device *drm;
81 struct sun4i_drv *drv;
82 int ret;
83
84 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +020085 if (IS_ERR(drm))
86 return PTR_ERR(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010087
Maxime Ripard9026e0d2015-10-29 09:36:23 +010088 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
89 if (!drv) {
90 ret = -ENOMEM;
91 goto free_drm;
92 }
93 drm->dev_private = drv;
Icenowy Zheng87969332017-05-17 22:47:17 +080094 INIT_LIST_HEAD(&drv->engine_list);
Chen-Yu Tsai80a58242017-04-21 16:38:50 +080095 INIT_LIST_HEAD(&drv->tcon_list);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010096
Maxime Ripard596afb62017-02-09 17:39:18 +010097 ret = of_reserved_mem_device_init(dev);
98 if (ret && ret != -ENODEV) {
99 dev_err(drm->dev, "Couldn't claim our memory region\n");
100 goto free_drm;
101 }
102
Chen-Yu Tsai92b300c2017-02-17 11:13:26 +0800103 /* drm_vblank_init calls kcalloc, which can fail */
104 ret = drm_vblank_init(drm, 1);
105 if (ret)
Maxime Ripard596afb62017-02-09 17:39:18 +0100106 goto free_mem_region;
Chen-Yu Tsai92b300c2017-02-17 11:13:26 +0800107
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100108 drm_mode_config_init(drm);
109
110 ret = component_bind_all(drm->dev, drm);
111 if (ret) {
112 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800113 goto cleanup_mode_config;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100114 }
115
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100116 drm->irq_enabled = true;
117
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200118 /* Remove early framebuffers (ie. simplefb) */
119 sun4i_remove_framebuffers();
120
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100121 /* Create our framebuffer */
122 drv->fbdev = sun4i_framebuffer_init(drm);
123 if (IS_ERR(drv->fbdev)) {
124 dev_err(drm->dev, "Couldn't create our framebuffer\n");
125 ret = PTR_ERR(drv->fbdev);
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800126 goto cleanup_mode_config;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100127 }
128
129 /* Enable connectors polling */
130 drm_kms_helper_poll_init(drm);
131
132 ret = drm_dev_register(drm, 0);
133 if (ret)
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800134 goto finish_poll;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100135
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100136 return 0;
137
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800138finish_poll:
139 drm_kms_helper_poll_fini(drm);
140 sun4i_framebuffer_free(drm);
141cleanup_mode_config:
142 drm_mode_config_cleanup(drm);
143 drm_vblank_cleanup(drm);
Maxime Ripard596afb62017-02-09 17:39:18 +0100144free_mem_region:
145 of_reserved_mem_device_release(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100146free_drm:
147 drm_dev_unref(drm);
148 return ret;
149}
150
151static void sun4i_drv_unbind(struct device *dev)
152{
153 struct drm_device *drm = dev_get_drvdata(dev);
154
155 drm_dev_unregister(drm);
156 drm_kms_helper_poll_fini(drm);
157 sun4i_framebuffer_free(drm);
Chen-Yu Tsai92caf9b2017-02-17 11:13:24 +0800158 drm_mode_config_cleanup(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100159 drm_vblank_cleanup(drm);
Maxime Ripard596afb62017-02-09 17:39:18 +0100160 of_reserved_mem_device_release(dev);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100161 drm_dev_unref(drm);
162}
163
164static const struct component_master_ops sun4i_drv_master_ops = {
165 .bind = sun4i_drv_bind,
166 .unbind = sun4i_drv_unbind,
167};
168
169static bool sun4i_drv_node_is_frontend(struct device_node *node)
170{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100171 return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800172 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100173 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100174}
175
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100176static bool sun4i_drv_node_is_tcon(struct device_node *node)
177{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100178 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800179 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
180 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100181 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100182}
183
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100184static int compare_of(struct device *dev, void *data)
185{
186 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
187 of_node_full_name(dev->of_node),
188 of_node_full_name(data));
189
190 return dev->of_node == data;
191}
192
193static int sun4i_drv_add_endpoints(struct device *dev,
194 struct component_match **match,
195 struct device_node *node)
196{
197 struct device_node *port, *ep, *remote;
198 int count = 0;
199
200 /*
201 * We don't support the frontend for now, so we will never
202 * have a device bound. Just skip over it, but we still want
203 * the rest our pipeline to be added.
204 */
205 if (!sun4i_drv_node_is_frontend(node) &&
206 !of_device_is_available(node))
207 return 0;
208
209 if (!sun4i_drv_node_is_frontend(node)) {
210 /* Add current component */
211 DRM_DEBUG_DRIVER("Adding component %s\n",
212 of_node_full_name(node));
Russell King97ac0e42016-10-19 11:28:27 +0100213 drm_of_component_match_add(dev, match, compare_of, node);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100214 count++;
215 }
216
217 /* Inputs are listed first, then outputs */
218 port = of_graph_get_port_by_id(node, 1);
219 if (!port) {
220 DRM_DEBUG_DRIVER("No output to bind\n");
221 return count;
222 }
223
224 for_each_available_child_of_node(port, ep) {
225 remote = of_graph_get_remote_port_parent(ep);
226 if (!remote) {
227 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
228 of_node_put(remote);
229 continue;
230 }
231
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100232 /*
Maxime Ripard894f5a92016-04-11 12:16:33 +0200233 * If the node is our TCON, the first port is used for
234 * panel or bridges, and will not be part of the
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100235 * component framework.
236 */
237 if (sun4i_drv_node_is_tcon(node)) {
238 struct of_endpoint endpoint;
239
240 if (of_graph_parse_endpoint(ep, &endpoint)) {
241 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
242 continue;
243 }
244
245 if (!endpoint.id) {
246 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
247 continue;
248 }
249 }
250
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100251 /* Walk down our tree */
252 count += sun4i_drv_add_endpoints(dev, match, remote);
253
254 of_node_put(remote);
255 }
256
257 return count;
258}
259
260static int sun4i_drv_probe(struct platform_device *pdev)
261{
262 struct component_match *match = NULL;
263 struct device_node *np = pdev->dev.of_node;
264 int i, count = 0;
265
266 for (i = 0;; i++) {
267 struct device_node *pipeline = of_parse_phandle(np,
268 "allwinner,pipelines",
269 i);
270 if (!pipeline)
271 break;
272
273 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
274 pipeline);
Peter Chen3b8e64f2016-07-05 10:04:53 +0800275 of_node_put(pipeline);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100276
277 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
278 count, i);
279 }
280
281 if (count)
282 return component_master_add_with_match(&pdev->dev,
283 &sun4i_drv_master_ops,
284 match);
285 else
286 return 0;
287}
288
289static int sun4i_drv_remove(struct platform_device *pdev)
290{
291 return 0;
292}
293
294static const struct of_device_id sun4i_drv_of_table[] = {
295 { .compatible = "allwinner,sun5i-a13-display-engine" },
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800296 { .compatible = "allwinner,sun6i-a31-display-engine" },
297 { .compatible = "allwinner,sun6i-a31s-display-engine" },
Maxime Ripard4a408f12016-01-07 12:32:25 +0100298 { .compatible = "allwinner,sun8i-a33-display-engine" },
Icenowy Zheng9df90c22017-05-17 22:47:21 +0800299 { .compatible = "allwinner,sun8i-v3s-display-engine" },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100300 { }
301};
302MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
303
304static struct platform_driver sun4i_drv_platform_driver = {
305 .probe = sun4i_drv_probe,
306 .remove = sun4i_drv_remove,
307 .driver = {
308 .name = "sun4i-drm",
309 .of_match_table = sun4i_drv_of_table,
310 },
311};
312module_platform_driver(sun4i_drv_platform_driver);
313
314MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
315MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
316MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
317MODULE_LICENSE("GPL");