blob: 63f42d001f33d9ac1737d358c21ccdb4184083ff [file] [log] [blame]
Russell King96f60e32012-08-15 13:59:49 +01001/*
2 * Copyright (C) 2012 Russell King
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/clk.h>
Russell King0101fd02014-02-04 11:57:06 +00009#include <linux/component.h>
Russell King96f60e32012-08-15 13:59:49 +010010#include <linux/module.h>
Russell King0101fd02014-02-04 11:57:06 +000011#include <linux/of_graph.h>
Russell King96f60e32012-08-15 13:59:49 +010012#include <drm/drmP.h>
13#include <drm/drm_crtc_helper.h>
Liviu Dudau7fc7e862015-10-20 10:23:15 +010014#include <drm/drm_of.h>
Russell King96f60e32012-08-15 13:59:49 +010015#include "armada_crtc.h"
16#include "armada_drm.h"
17#include "armada_gem.h"
18#include "armada_hw.h"
19#include <drm/armada_drm.h>
20#include "armada_ioctlP.h"
21
22static void armada_drm_unref_work(struct work_struct *work)
23{
24 struct armada_private *priv =
25 container_of(work, struct armada_private, fb_unref_work);
26 struct drm_framebuffer *fb;
27
28 while (kfifo_get(&priv->fb_unref, &fb))
29 drm_framebuffer_unreference(fb);
30}
31
32/* Must be called with dev->event_lock held */
33void __armada_drm_queue_unref_work(struct drm_device *dev,
34 struct drm_framebuffer *fb)
35{
36 struct armada_private *priv = dev->dev_private;
37
Russell Kingd13c46c2014-03-03 14:49:51 +000038 WARN_ON(!kfifo_put(&priv->fb_unref, fb));
Russell King96f60e32012-08-15 13:59:49 +010039 schedule_work(&priv->fb_unref_work);
40}
41
42void armada_drm_queue_unref_work(struct drm_device *dev,
43 struct drm_framebuffer *fb)
44{
45 unsigned long flags;
46
47 spin_lock_irqsave(&dev->event_lock, flags);
48 __armada_drm_queue_unref_work(dev, fb);
49 spin_unlock_irqrestore(&dev->event_lock, flags);
50}
51
Russell King96f60e32012-08-15 13:59:49 +010052/* These are called under the vbl_lock. */
Thierry Reding88e72712015-09-24 18:35:31 +020053static int armada_drm_enable_vblank(struct drm_device *dev, unsigned int pipe)
Russell King96f60e32012-08-15 13:59:49 +010054{
55 struct armada_private *priv = dev->dev_private;
Thierry Reding88e72712015-09-24 18:35:31 +020056 armada_drm_crtc_enable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
Russell King96f60e32012-08-15 13:59:49 +010057 return 0;
58}
59
Thierry Reding88e72712015-09-24 18:35:31 +020060static void armada_drm_disable_vblank(struct drm_device *dev, unsigned int pipe)
Russell King96f60e32012-08-15 13:59:49 +010061{
62 struct armada_private *priv = dev->dev_private;
Thierry Reding88e72712015-09-24 18:35:31 +020063 armada_drm_crtc_disable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
Russell King96f60e32012-08-15 13:59:49 +010064}
65
Russell King96f60e32012-08-15 13:59:49 +010066static struct drm_ioctl_desc armada_ioctls[] = {
Daniel Vetterf8c47142015-09-08 13:56:30 +020067 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
68 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
69 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
Russell King96f60e32012-08-15 13:59:49 +010070};
71
Russell King2f5ae492013-10-27 15:26:33 +000072static void armada_drm_lastclose(struct drm_device *dev)
73{
74 armada_fbdev_lastclose(dev);
75}
76
Russell King96f60e32012-08-15 13:59:49 +010077static const struct file_operations armada_drm_fops = {
78 .owner = THIS_MODULE,
79 .llseek = no_llseek,
80 .read = drm_read,
81 .poll = drm_poll,
82 .unlocked_ioctl = drm_ioctl,
83 .mmap = drm_gem_mmap,
84 .open = drm_open,
85 .release = drm_release,
86};
87
88static struct drm_driver armada_drm_driver = {
Russell King2f5ae492013-10-27 15:26:33 +000089 .lastclose = armada_drm_lastclose,
Ville Syrjäläb44f8402015-09-30 16:46:48 +030090 .get_vblank_counter = drm_vblank_no_hw_counter,
Russell King96f60e32012-08-15 13:59:49 +010091 .enable_vblank = armada_drm_enable_vblank,
92 .disable_vblank = armada_drm_disable_vblank,
Daniel Vetter87adc632016-05-30 19:52:53 +020093 .gem_free_object_unlocked = armada_gem_free_object,
Russell King96f60e32012-08-15 13:59:49 +010094 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
95 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
96 .gem_prime_export = armada_gem_prime_export,
97 .gem_prime_import = armada_gem_prime_import,
98 .dumb_create = armada_gem_dumb_create,
99 .dumb_map_offset = armada_gem_dumb_map_offset,
100 .dumb_destroy = armada_gem_dumb_destroy,
101 .gem_vm_ops = &armada_gem_vm_ops,
102 .major = 1,
103 .minor = 0,
104 .name = "armada-drm",
105 .desc = "Armada SoC DRM",
106 .date = "20120730",
107 .driver_features = DRIVER_GEM | DRIVER_MODESET |
Shawn Guoe1f96ef2016-08-16 15:06:08 +0800108 DRIVER_PRIME,
Russell King96f60e32012-08-15 13:59:49 +0100109 .ioctls = armada_ioctls,
110 .fops = &armada_drm_fops,
111};
112
Russell King0101fd02014-02-04 11:57:06 +0000113static int armada_drm_bind(struct device *dev)
114{
Russell King917a3cb2016-11-01 20:05:47 +0000115 struct armada_private *priv;
116 struct resource *mem = NULL;
117 int ret, n;
118
119 for (n = 0; ; n++) {
120 struct resource *r = platform_get_resource(to_platform_device(dev),
121 IORESOURCE_MEM, n);
122 if (!r)
123 break;
124
125 /* Resources above 64K are graphics memory */
126 if (resource_size(r) > SZ_64K)
127 mem = r;
128 else
129 return -EINVAL;
130 }
131
132 if (!mem)
133 return -ENXIO;
134
135 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
136 "armada-drm"))
137 return -EBUSY;
138
139 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
140 if (!priv)
141 return -ENOMEM;
142
143 /*
144 * The drm_device structure must be at the start of
145 * armada_private for drm_dev_unref() to work correctly.
146 */
147 BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
148
149 ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
150 if (ret) {
151 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
152 __func__, ret);
153 kfree(priv);
154 return ret;
155 }
156
157 priv->drm.platformdev = to_platform_device(dev);
158 priv->drm.dev_private = priv;
159
160 platform_set_drvdata(priv->drm.platformdev, &priv->drm);
161
162 INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
163 INIT_KFIFO(priv->fb_unref);
164
165 /* Mode setting support */
166 drm_mode_config_init(&priv->drm);
167 priv->drm.mode_config.min_width = 320;
168 priv->drm.mode_config.min_height = 200;
169
170 /*
171 * With vscale enabled, the maximum width is 1920 due to the
172 * 1920 by 3 lines RAM
173 */
174 priv->drm.mode_config.max_width = 1920;
175 priv->drm.mode_config.max_height = 2048;
176
177 priv->drm.mode_config.preferred_depth = 24;
178 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
179 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
180 mutex_init(&priv->linear_lock);
181
182 ret = component_bind_all(dev, &priv->drm);
183 if (ret)
184 goto err_kms;
185
186 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
187 if (ret)
188 goto err_comp;
189
190 priv->drm.irq_enabled = true;
191
192 ret = armada_fbdev_init(&priv->drm);
193 if (ret)
194 goto err_comp;
195
196 drm_kms_helper_poll_init(&priv->drm);
197
198 ret = drm_dev_register(&priv->drm, 0);
199 if (ret)
200 goto err_poll;
201
202#ifdef CONFIG_DEBUG_FS
203 armada_drm_debugfs_init(priv->drm.primary);
204#endif
205
Russell King917a3cb2016-11-01 20:05:47 +0000206 return 0;
207
208 err_poll:
209 drm_kms_helper_poll_fini(&priv->drm);
210 armada_fbdev_fini(&priv->drm);
211 err_comp:
212 component_unbind_all(dev, &priv->drm);
213 err_kms:
214 drm_mode_config_cleanup(&priv->drm);
215 drm_mm_takedown(&priv->linear);
216 flush_work(&priv->fb_unref_work);
217 drm_dev_unref(&priv->drm);
218 return ret;
Russell King0101fd02014-02-04 11:57:06 +0000219}
220
221static void armada_drm_unbind(struct device *dev)
222{
Russell King917a3cb2016-11-01 20:05:47 +0000223 struct drm_device *drm = dev_get_drvdata(dev);
224 struct armada_private *priv = drm->dev_private;
225
226 drm_kms_helper_poll_fini(&priv->drm);
227 armada_fbdev_fini(&priv->drm);
228
229#ifdef CONFIG_DEBUG_FS
230 armada_drm_debugfs_cleanup(priv->drm.primary);
231#endif
232 drm_dev_unregister(&priv->drm);
233
234 component_unbind_all(dev, &priv->drm);
235
236 drm_mode_config_cleanup(&priv->drm);
237 drm_mm_takedown(&priv->linear);
238 flush_work(&priv->fb_unref_work);
239
240 drm_dev_unref(&priv->drm);
Russell King0101fd02014-02-04 11:57:06 +0000241}
242
243static int compare_of(struct device *dev, void *data)
244{
245 return dev->of_node == data;
246}
247
248static int compare_dev_name(struct device *dev, void *data)
249{
250 const char *name = data;
251 return !strcmp(dev_name(dev), name);
252}
253
254static void armada_add_endpoints(struct device *dev,
255 struct component_match **match, struct device_node *port)
256{
257 struct device_node *ep, *remote;
258
259 for_each_child_of_node(port, ep) {
260 remote = of_graph_get_remote_port_parent(ep);
261 if (!remote || !of_device_is_available(remote)) {
262 of_node_put(remote);
263 continue;
264 } else if (!of_device_is_available(remote->parent)) {
265 dev_warn(dev, "parent device of %s is not available\n",
266 remote->full_name);
267 of_node_put(remote);
268 continue;
269 }
270
Russell King97ac0e42016-10-19 11:28:27 +0100271 drm_of_component_match_add(dev, match, compare_of, remote);
Russell King0101fd02014-02-04 11:57:06 +0000272 of_node_put(remote);
273 }
274}
275
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100276static const struct component_master_ops armada_master_ops = {
277 .bind = armada_drm_bind,
278 .unbind = armada_drm_unbind,
279};
280
281static int armada_drm_probe(struct platform_device *pdev)
Russell King0101fd02014-02-04 11:57:06 +0000282{
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100283 struct component_match *match = NULL;
284 struct device *dev = &pdev->dev;
285 int ret;
Russell King0101fd02014-02-04 11:57:06 +0000286
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100287 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
288 if (ret != -EINVAL)
289 return ret;
Russell King0101fd02014-02-04 11:57:06 +0000290
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100291 if (dev->platform_data) {
Russell King0101fd02014-02-04 11:57:06 +0000292 char **devices = dev->platform_data;
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100293 struct device_node *port;
Russell King0101fd02014-02-04 11:57:06 +0000294 struct device *d;
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100295 int i;
Russell King0101fd02014-02-04 11:57:06 +0000296
297 for (i = 0; devices[i]; i++)
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100298 component_match_add(dev, &match, compare_dev_name,
Russell King0101fd02014-02-04 11:57:06 +0000299 devices[i]);
300
301 if (i == 0) {
302 dev_err(dev, "missing 'ports' property\n");
303 return -ENODEV;
304 }
305
306 for (i = 0; devices[i]; i++) {
307 d = bus_find_device_by_name(&platform_bus_type, NULL,
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100308 devices[i]);
Russell King0101fd02014-02-04 11:57:06 +0000309 if (d && d->of_node) {
310 for_each_child_of_node(d->of_node, port)
Liviu Dudau7fc7e862015-10-20 10:23:15 +0100311 armada_add_endpoints(dev, &match, port);
Russell King0101fd02014-02-04 11:57:06 +0000312 }
313 put_device(d);
314 }
315 }
316
Russell King0fb29702015-06-06 21:46:53 +0100317 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
318 match);
Russell King96f60e32012-08-15 13:59:49 +0100319}
320
321static int armada_drm_remove(struct platform_device *pdev)
322{
Russell King0fb29702015-06-06 21:46:53 +0100323 component_master_del(&pdev->dev, &armada_master_ops);
Russell King96f60e32012-08-15 13:59:49 +0100324 return 0;
325}
326
327static const struct platform_device_id armada_drm_platform_ids[] = {
328 {
329 .name = "armada-drm",
Russell King96f60e32012-08-15 13:59:49 +0100330 }, {
331 .name = "armada-510-drm",
Russell King96f60e32012-08-15 13:59:49 +0100332 },
333 { },
334};
335MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
336
337static struct platform_driver armada_drm_platform_driver = {
338 .probe = armada_drm_probe,
339 .remove = armada_drm_remove,
340 .driver = {
341 .name = "armada-drm",
Russell King96f60e32012-08-15 13:59:49 +0100342 },
343 .id_table = armada_drm_platform_ids,
344};
345
346static int __init armada_drm_init(void)
347{
Russell Kingd8c96082014-04-22 11:10:15 +0100348 int ret;
349
Damien Lespiauf95aeb12014-06-09 14:39:49 +0100350 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
Russell Kingd8c96082014-04-22 11:10:15 +0100351
352 ret = platform_driver_register(&armada_lcd_platform_driver);
353 if (ret)
354 return ret;
355 ret = platform_driver_register(&armada_drm_platform_driver);
356 if (ret)
357 platform_driver_unregister(&armada_lcd_platform_driver);
358 return ret;
Russell King96f60e32012-08-15 13:59:49 +0100359}
360module_init(armada_drm_init);
361
362static void __exit armada_drm_exit(void)
363{
364 platform_driver_unregister(&armada_drm_platform_driver);
Russell Kingd8c96082014-04-22 11:10:15 +0100365 platform_driver_unregister(&armada_lcd_platform_driver);
Russell King96f60e32012-08-15 13:59:49 +0100366}
367module_exit(armada_drm_exit);
368
369MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
370MODULE_DESCRIPTION("Armada DRM Driver");
371MODULE_LICENSE("GPL");
372MODULE_ALIAS("platform:armada-drm");