blob: 21b1427fc91859a3881da9d2aff04e0853f3d2e1 [file] [log] [blame]
Liviu Dudau8e22d792015-04-02 19:48:39 +01001/*
2 * Copyright (C) 2013-2015 ARM Limited
3 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4 *
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file COPYING in the main directory of this archive
7 * for more details.
8 *
9 * ARM HDLCD Driver
10 */
11
12#include <linux/module.h>
13#include <linux/spinlock.h>
14#include <linux/clk.h>
15#include <linux/component.h>
16#include <linux/list.h>
17#include <linux/of_graph.h>
18#include <linux/of_reserved_mem.h>
19#include <linux/pm_runtime.h>
20
21#include <drm/drmP.h>
22#include <drm/drm_atomic_helper.h>
23#include <drm/drm_crtc.h>
24#include <drm/drm_crtc_helper.h>
25#include <drm/drm_fb_helper.h>
26#include <drm/drm_fb_cma_helper.h>
27#include <drm/drm_gem_cma_helper.h>
28#include <drm/drm_of.h>
29
30#include "hdlcd_drv.h"
31#include "hdlcd_regs.h"
32
33static int hdlcd_load(struct drm_device *drm, unsigned long flags)
34{
35 struct hdlcd_drm_private *hdlcd = drm->dev_private;
36 struct platform_device *pdev = to_platform_device(drm->dev);
37 struct resource *res;
38 u32 version;
39 int ret;
40
41 hdlcd->clk = devm_clk_get(drm->dev, "pxlclk");
42 if (IS_ERR(hdlcd->clk))
43 return PTR_ERR(hdlcd->clk);
44
45#ifdef CONFIG_DEBUG_FS
46 atomic_set(&hdlcd->buffer_underrun_count, 0);
47 atomic_set(&hdlcd->bus_error_count, 0);
48 atomic_set(&hdlcd->vsync_count, 0);
49 atomic_set(&hdlcd->dma_end_count, 0);
50#endif
51
52 INIT_LIST_HEAD(&hdlcd->event_list);
53
54 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
55 hdlcd->mmio = devm_ioremap_resource(drm->dev, res);
56 if (IS_ERR(hdlcd->mmio)) {
57 DRM_ERROR("failed to map control registers area\n");
Dan Carpenter69c25652016-04-02 08:42:24 +030058 ret = PTR_ERR(hdlcd->mmio);
Liviu Dudau8e22d792015-04-02 19:48:39 +010059 hdlcd->mmio = NULL;
Dan Carpenter69c25652016-04-02 08:42:24 +030060 return ret;
Liviu Dudau8e22d792015-04-02 19:48:39 +010061 }
62
63 version = hdlcd_read(hdlcd, HDLCD_REG_VERSION);
64 if ((version & HDLCD_PRODUCT_MASK) != HDLCD_PRODUCT_ID) {
65 DRM_ERROR("unknown product id: 0x%x\n", version);
Alexey Brodkin61a6dcd2016-02-19 11:15:01 +030066 return -EINVAL;
Liviu Dudau8e22d792015-04-02 19:48:39 +010067 }
68 DRM_INFO("found ARM HDLCD version r%dp%d\n",
69 (version & HDLCD_VERSION_MAJOR_MASK) >> 8,
70 version & HDLCD_VERSION_MINOR_MASK);
71
72 /* Get the optional framebuffer memory resource */
73 ret = of_reserved_mem_device_init(drm->dev);
74 if (ret && ret != -ENODEV)
Alexey Brodkin61a6dcd2016-02-19 11:15:01 +030075 return ret;
Liviu Dudau8e22d792015-04-02 19:48:39 +010076
77 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
78 if (ret)
79 goto setup_fail;
80
81 ret = hdlcd_setup_crtc(drm);
82 if (ret < 0) {
83 DRM_ERROR("failed to create crtc\n");
84 goto setup_fail;
85 }
86
Liviu Dudau8e22d792015-04-02 19:48:39 +010087 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
Liviu Dudau8e22d792015-04-02 19:48:39 +010088 if (ret < 0) {
89 DRM_ERROR("failed to install IRQ handler\n");
90 goto irq_fail;
91 }
92
93 return 0;
94
95irq_fail:
96 drm_crtc_cleanup(&hdlcd->crtc);
97setup_fail:
98 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +010099
100 return ret;
101}
102
103static void hdlcd_fb_output_poll_changed(struct drm_device *drm)
104{
105 struct hdlcd_drm_private *hdlcd = drm->dev_private;
106
107 if (hdlcd->fbdev)
108 drm_fbdev_cma_hotplug_event(hdlcd->fbdev);
109}
110
111static int hdlcd_atomic_commit(struct drm_device *dev,
Maarten Lankhorst51b6bef2016-04-26 16:11:36 +0200112 struct drm_atomic_state *state, bool nonblock)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100113{
114 return drm_atomic_helper_commit(dev, state, false);
115}
116
117static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
118 .fb_create = drm_fb_cma_create,
119 .output_poll_changed = hdlcd_fb_output_poll_changed,
120 .atomic_check = drm_atomic_helper_check,
121 .atomic_commit = hdlcd_atomic_commit,
122};
123
124static void hdlcd_setup_mode_config(struct drm_device *drm)
125{
126 drm_mode_config_init(drm);
127 drm->mode_config.min_width = 0;
128 drm->mode_config.min_height = 0;
129 drm->mode_config.max_width = HDLCD_MAX_XRES;
130 drm->mode_config.max_height = HDLCD_MAX_YRES;
131 drm->mode_config.funcs = &hdlcd_mode_config_funcs;
132}
133
134static void hdlcd_lastclose(struct drm_device *drm)
135{
136 struct hdlcd_drm_private *hdlcd = drm->dev_private;
137
138 drm_fbdev_cma_restore_mode(hdlcd->fbdev);
139}
140
141static irqreturn_t hdlcd_irq(int irq, void *arg)
142{
143 struct drm_device *drm = arg;
144 struct hdlcd_drm_private *hdlcd = drm->dev_private;
145 unsigned long irq_status;
146
147 irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
148
149#ifdef CONFIG_DEBUG_FS
150 if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
151 atomic_inc(&hdlcd->buffer_underrun_count);
152
153 if (irq_status & HDLCD_INTERRUPT_DMA_END)
154 atomic_inc(&hdlcd->dma_end_count);
155
156 if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
157 atomic_inc(&hdlcd->bus_error_count);
158
159 if (irq_status & HDLCD_INTERRUPT_VSYNC)
160 atomic_inc(&hdlcd->vsync_count);
161
162#endif
163 if (irq_status & HDLCD_INTERRUPT_VSYNC) {
164 bool events_sent = false;
165 unsigned long flags;
166 struct drm_pending_vblank_event *e, *t;
167
168 drm_crtc_handle_vblank(&hdlcd->crtc);
169
170 spin_lock_irqsave(&drm->event_lock, flags);
171 list_for_each_entry_safe(e, t, &hdlcd->event_list, base.link) {
172 list_del(&e->base.link);
173 drm_crtc_send_vblank_event(&hdlcd->crtc, e);
174 events_sent = true;
175 }
176 if (events_sent)
177 drm_crtc_vblank_put(&hdlcd->crtc);
178 spin_unlock_irqrestore(&drm->event_lock, flags);
179 }
180
181 /* acknowledge interrupt(s) */
182 hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
183
184 return IRQ_HANDLED;
185}
186
187static void hdlcd_irq_preinstall(struct drm_device *drm)
188{
189 struct hdlcd_drm_private *hdlcd = drm->dev_private;
190 /* Ensure interrupts are disabled */
191 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
192 hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
193}
194
195static int hdlcd_irq_postinstall(struct drm_device *drm)
196{
197#ifdef CONFIG_DEBUG_FS
198 struct hdlcd_drm_private *hdlcd = drm->dev_private;
199 unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
200
201 /* enable debug interrupts */
202 irq_mask |= HDLCD_DEBUG_INT_MASK;
203
204 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
205#endif
206 return 0;
207}
208
209static void hdlcd_irq_uninstall(struct drm_device *drm)
210{
211 struct hdlcd_drm_private *hdlcd = drm->dev_private;
212 /* disable all the interrupts that we might have enabled */
213 unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
214
215#ifdef CONFIG_DEBUG_FS
216 /* disable debug interrupts */
217 irq_mask &= ~HDLCD_DEBUG_INT_MASK;
218#endif
219
220 /* disable vsync interrupts */
221 irq_mask &= ~HDLCD_INTERRUPT_VSYNC;
222
223 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
224}
225
226static int hdlcd_enable_vblank(struct drm_device *drm, unsigned int crtc)
227{
228 struct hdlcd_drm_private *hdlcd = drm->dev_private;
229 unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
230
231 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask | HDLCD_INTERRUPT_VSYNC);
232
233 return 0;
234}
235
236static void hdlcd_disable_vblank(struct drm_device *drm, unsigned int crtc)
237{
238 struct hdlcd_drm_private *hdlcd = drm->dev_private;
239 unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
240
241 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask & ~HDLCD_INTERRUPT_VSYNC);
242}
243
244#ifdef CONFIG_DEBUG_FS
245static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
246{
247 struct drm_info_node *node = (struct drm_info_node *)m->private;
248 struct drm_device *drm = node->minor->dev;
249 struct hdlcd_drm_private *hdlcd = drm->dev_private;
250
251 seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
252 seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
253 seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
254 seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
255 return 0;
256}
257
258static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
259{
260 struct drm_info_node *node = (struct drm_info_node *)m->private;
261 struct drm_device *drm = node->minor->dev;
262 struct hdlcd_drm_private *hdlcd = drm->dev_private;
263 unsigned long clkrate = clk_get_rate(hdlcd->clk);
264 unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
265
266 seq_printf(m, "hw : %lu\n", clkrate);
267 seq_printf(m, "mode: %lu\n", mode_clock);
268 return 0;
269}
270
271static struct drm_info_list hdlcd_debugfs_list[] = {
272 { "interrupt_count", hdlcd_show_underrun_count, 0 },
273 { "clocks", hdlcd_show_pxlclock, 0 },
274};
275
276static int hdlcd_debugfs_init(struct drm_minor *minor)
277{
278 return drm_debugfs_create_files(hdlcd_debugfs_list,
279 ARRAY_SIZE(hdlcd_debugfs_list), minor->debugfs_root, minor);
280}
281
282static void hdlcd_debugfs_cleanup(struct drm_minor *minor)
283{
284 drm_debugfs_remove_files(hdlcd_debugfs_list,
285 ARRAY_SIZE(hdlcd_debugfs_list), minor);
286}
287#endif
288
289static const struct file_operations fops = {
290 .owner = THIS_MODULE,
291 .open = drm_open,
292 .release = drm_release,
293 .unlocked_ioctl = drm_ioctl,
294#ifdef CONFIG_COMPAT
295 .compat_ioctl = drm_compat_ioctl,
296#endif
297 .poll = drm_poll,
298 .read = drm_read,
299 .llseek = noop_llseek,
300 .mmap = drm_gem_cma_mmap,
301};
302
303static struct drm_driver hdlcd_driver = {
304 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
305 DRIVER_MODESET | DRIVER_PRIME |
306 DRIVER_ATOMIC,
307 .lastclose = hdlcd_lastclose,
308 .irq_handler = hdlcd_irq,
309 .irq_preinstall = hdlcd_irq_preinstall,
310 .irq_postinstall = hdlcd_irq_postinstall,
311 .irq_uninstall = hdlcd_irq_uninstall,
312 .get_vblank_counter = drm_vblank_no_hw_counter,
313 .enable_vblank = hdlcd_enable_vblank,
314 .disable_vblank = hdlcd_disable_vblank,
315 .gem_free_object = drm_gem_cma_free_object,
316 .gem_vm_ops = &drm_gem_cma_vm_ops,
317 .dumb_create = drm_gem_cma_dumb_create,
318 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
319 .dumb_destroy = drm_gem_dumb_destroy,
320 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
321 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
322 .gem_prime_export = drm_gem_prime_export,
323 .gem_prime_import = drm_gem_prime_import,
324 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
325 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
326 .gem_prime_vmap = drm_gem_cma_prime_vmap,
327 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
328 .gem_prime_mmap = drm_gem_cma_prime_mmap,
329#ifdef CONFIG_DEBUG_FS
330 .debugfs_init = hdlcd_debugfs_init,
331 .debugfs_cleanup = hdlcd_debugfs_cleanup,
332#endif
333 .fops = &fops,
334 .name = "hdlcd",
335 .desc = "ARM HDLCD Controller DRM",
336 .date = "20151021",
337 .major = 1,
338 .minor = 0,
339};
340
341static int hdlcd_drm_bind(struct device *dev)
342{
343 struct drm_device *drm;
344 struct hdlcd_drm_private *hdlcd;
345 int ret;
346
347 hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
348 if (!hdlcd)
349 return -ENOMEM;
350
351 drm = drm_dev_alloc(&hdlcd_driver, dev);
352 if (!drm)
353 return -ENOMEM;
354
355 drm->dev_private = hdlcd;
Liviu Dudaua95acec2016-05-17 10:06:54 +0100356 dev_set_drvdata(dev, drm);
357
Liviu Dudau8e22d792015-04-02 19:48:39 +0100358 hdlcd_setup_mode_config(drm);
359 ret = hdlcd_load(drm, 0);
360 if (ret)
361 goto err_free;
362
363 ret = drm_dev_register(drm, 0);
364 if (ret)
365 goto err_unload;
366
Liviu Dudau8e22d792015-04-02 19:48:39 +0100367 ret = component_bind_all(dev, drm);
368 if (ret) {
369 DRM_ERROR("Failed to bind all components\n");
370 goto err_unregister;
371 }
372
Liviu Dudaua95acec2016-05-17 10:06:54 +0100373 ret = pm_runtime_set_active(dev);
374 if (ret)
375 goto err_pm_active;
376
377 pm_runtime_enable(dev);
378
Liviu Dudau8e22d792015-04-02 19:48:39 +0100379 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
380 if (ret < 0) {
381 DRM_ERROR("failed to initialise vblank\n");
382 goto err_vblank;
383 }
Liviu Dudau8e22d792015-04-02 19:48:39 +0100384
385 drm_mode_config_reset(drm);
386 drm_kms_helper_poll_init(drm);
387
388 hdlcd->fbdev = drm_fbdev_cma_init(drm, 32, drm->mode_config.num_crtc,
389 drm->mode_config.num_connector);
390
391 if (IS_ERR(hdlcd->fbdev)) {
392 ret = PTR_ERR(hdlcd->fbdev);
393 hdlcd->fbdev = NULL;
394 goto err_fbdev;
395 }
396
397 return 0;
398
399err_fbdev:
400 drm_kms_helper_poll_fini(drm);
401 drm_mode_config_cleanup(drm);
402 drm_vblank_cleanup(drm);
403err_vblank:
Liviu Dudaua95acec2016-05-17 10:06:54 +0100404 pm_runtime_disable(drm->dev);
405err_pm_active:
Liviu Dudau8e22d792015-04-02 19:48:39 +0100406 component_unbind_all(dev, drm);
407err_unregister:
408 drm_dev_unregister(drm);
409err_unload:
Liviu Dudau8e22d792015-04-02 19:48:39 +0100410 drm_irq_uninstall(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100411 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100412err_free:
Liviu Dudaua95acec2016-05-17 10:06:54 +0100413 dev_set_drvdata(dev, NULL);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100414 drm_dev_unref(drm);
415
416 return ret;
417}
418
419static void hdlcd_drm_unbind(struct device *dev)
420{
421 struct drm_device *drm = dev_get_drvdata(dev);
422 struct hdlcd_drm_private *hdlcd = drm->dev_private;
423
424 if (hdlcd->fbdev) {
425 drm_fbdev_cma_fini(hdlcd->fbdev);
426 hdlcd->fbdev = NULL;
427 }
428 drm_kms_helper_poll_fini(drm);
429 component_unbind_all(dev, drm);
430 drm_vblank_cleanup(drm);
431 pm_runtime_get_sync(drm->dev);
432 drm_irq_uninstall(drm);
433 pm_runtime_put_sync(drm->dev);
434 pm_runtime_disable(drm->dev);
435 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100436 drm_mode_config_cleanup(drm);
437 drm_dev_unregister(drm);
438 drm_dev_unref(drm);
439 drm->dev_private = NULL;
440 dev_set_drvdata(dev, NULL);
441}
442
443static const struct component_master_ops hdlcd_master_ops = {
444 .bind = hdlcd_drm_bind,
445 .unbind = hdlcd_drm_unbind,
446};
447
448static int compare_dev(struct device *dev, void *data)
449{
450 return dev->of_node == data;
451}
452
453static int hdlcd_probe(struct platform_device *pdev)
454{
455 struct device_node *port, *ep;
456 struct component_match *match = NULL;
457
458 if (!pdev->dev.of_node)
459 return -ENODEV;
460
461 /* there is only one output port inside each device, find it */
462 ep = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
463 if (!ep)
464 return -ENODEV;
465
466 if (!of_device_is_available(ep)) {
467 of_node_put(ep);
468 return -ENODEV;
469 }
470
471 /* add the remote encoder port as component */
472 port = of_graph_get_remote_port_parent(ep);
473 of_node_put(ep);
474 if (!port || !of_device_is_available(port)) {
475 of_node_put(port);
476 return -EAGAIN;
477 }
478
479 component_match_add(&pdev->dev, &match, compare_dev, port);
480
481 return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
482 match);
483}
484
485static int hdlcd_remove(struct platform_device *pdev)
486{
487 component_master_del(&pdev->dev, &hdlcd_master_ops);
488 return 0;
489}
490
491static const struct of_device_id hdlcd_of_match[] = {
492 { .compatible = "arm,hdlcd" },
493 {},
494};
495MODULE_DEVICE_TABLE(of, hdlcd_of_match);
496
497static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
498{
499 struct drm_device *drm = dev_get_drvdata(dev);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100500 struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100501
Liviu Dudaua95acec2016-05-17 10:06:54 +0100502 if (!hdlcd)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100503 return 0;
504
Liviu Dudaua95acec2016-05-17 10:06:54 +0100505 drm_kms_helper_poll_disable(drm);
506
507 hdlcd->state = drm_atomic_helper_suspend(drm);
508 if (IS_ERR(hdlcd->state)) {
509 drm_kms_helper_poll_enable(drm);
510 return PTR_ERR(hdlcd->state);
511 }
512
Liviu Dudau8e22d792015-04-02 19:48:39 +0100513 return 0;
514}
515
516static int __maybe_unused hdlcd_pm_resume(struct device *dev)
517{
518 struct drm_device *drm = dev_get_drvdata(dev);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100519 struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100520
Liviu Dudaua95acec2016-05-17 10:06:54 +0100521 if (!hdlcd)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100522 return 0;
523
Liviu Dudaua95acec2016-05-17 10:06:54 +0100524 drm_atomic_helper_resume(drm, hdlcd->state);
525 drm_kms_helper_poll_enable(drm);
526 pm_runtime_set_active(dev);
527
Liviu Dudau8e22d792015-04-02 19:48:39 +0100528 return 0;
529}
530
531static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
532
533static struct platform_driver hdlcd_platform_driver = {
534 .probe = hdlcd_probe,
535 .remove = hdlcd_remove,
536 .driver = {
537 .name = "hdlcd",
538 .pm = &hdlcd_pm_ops,
539 .of_match_table = hdlcd_of_match,
540 },
541};
542
543module_platform_driver(hdlcd_platform_driver);
544
545MODULE_AUTHOR("Liviu Dudau");
546MODULE_DESCRIPTION("ARM HDLCD DRM driver");
547MODULE_LICENSE("GPL v2");