blob: 764d0c83710ca563554672d06332fc10883b5de7 [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>
Noralf Trønnes39ffd902017-08-13 15:31:48 +020028#include <drm/drm_gem_framebuffer_helper.h>
Liviu Dudau8e22d792015-04-02 19:48:39 +010029#include <drm/drm_of.h>
30
31#include "hdlcd_drv.h"
32#include "hdlcd_regs.h"
33
34static int hdlcd_load(struct drm_device *drm, unsigned long flags)
35{
36 struct hdlcd_drm_private *hdlcd = drm->dev_private;
37 struct platform_device *pdev = to_platform_device(drm->dev);
38 struct resource *res;
39 u32 version;
40 int ret;
41
42 hdlcd->clk = devm_clk_get(drm->dev, "pxlclk");
43 if (IS_ERR(hdlcd->clk))
44 return PTR_ERR(hdlcd->clk);
45
46#ifdef CONFIG_DEBUG_FS
47 atomic_set(&hdlcd->buffer_underrun_count, 0);
48 atomic_set(&hdlcd->bus_error_count, 0);
49 atomic_set(&hdlcd->vsync_count, 0);
50 atomic_set(&hdlcd->dma_end_count, 0);
51#endif
52
Liviu Dudau8e22d792015-04-02 19:48:39 +010053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
54 hdlcd->mmio = devm_ioremap_resource(drm->dev, res);
55 if (IS_ERR(hdlcd->mmio)) {
56 DRM_ERROR("failed to map control registers area\n");
Dan Carpenter69c25652016-04-02 08:42:24 +030057 ret = PTR_ERR(hdlcd->mmio);
Liviu Dudau8e22d792015-04-02 19:48:39 +010058 hdlcd->mmio = NULL;
Dan Carpenter69c25652016-04-02 08:42:24 +030059 return ret;
Liviu Dudau8e22d792015-04-02 19:48:39 +010060 }
61
62 version = hdlcd_read(hdlcd, HDLCD_REG_VERSION);
63 if ((version & HDLCD_PRODUCT_MASK) != HDLCD_PRODUCT_ID) {
64 DRM_ERROR("unknown product id: 0x%x\n", version);
Alexey Brodkin61a6dcd2016-02-19 11:15:01 +030065 return -EINVAL;
Liviu Dudau8e22d792015-04-02 19:48:39 +010066 }
67 DRM_INFO("found ARM HDLCD version r%dp%d\n",
68 (version & HDLCD_VERSION_MAJOR_MASK) >> 8,
69 version & HDLCD_VERSION_MINOR_MASK);
70
71 /* Get the optional framebuffer memory resource */
72 ret = of_reserved_mem_device_init(drm->dev);
73 if (ret && ret != -ENODEV)
Alexey Brodkin61a6dcd2016-02-19 11:15:01 +030074 return ret;
Liviu Dudau8e22d792015-04-02 19:48:39 +010075
76 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
77 if (ret)
78 goto setup_fail;
79
80 ret = hdlcd_setup_crtc(drm);
81 if (ret < 0) {
82 DRM_ERROR("failed to create crtc\n");
83 goto setup_fail;
84 }
85
Liviu Dudau8e22d792015-04-02 19:48:39 +010086 ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
Liviu Dudau8e22d792015-04-02 19:48:39 +010087 if (ret < 0) {
88 DRM_ERROR("failed to install IRQ handler\n");
89 goto irq_fail;
90 }
91
92 return 0;
93
94irq_fail:
95 drm_crtc_cleanup(&hdlcd->crtc);
96setup_fail:
97 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +010098
99 return ret;
100}
101
102static void hdlcd_fb_output_poll_changed(struct drm_device *drm)
103{
104 struct hdlcd_drm_private *hdlcd = drm->dev_private;
105
Markus Elfring536cea62016-07-16 09:10:40 +0200106 drm_fbdev_cma_hotplug_event(hdlcd->fbdev);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100107}
108
Liviu Dudau8e22d792015-04-02 19:48:39 +0100109static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
Noralf Trønnes39ffd902017-08-13 15:31:48 +0200110 .fb_create = drm_gem_fb_create,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100111 .output_poll_changed = hdlcd_fb_output_poll_changed,
112 .atomic_check = drm_atomic_helper_check,
Daniel Vetter2bd6cc82016-06-08 14:19:04 +0200113 .atomic_commit = drm_atomic_helper_commit,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100114};
115
116static void hdlcd_setup_mode_config(struct drm_device *drm)
117{
118 drm_mode_config_init(drm);
119 drm->mode_config.min_width = 0;
120 drm->mode_config.min_height = 0;
121 drm->mode_config.max_width = HDLCD_MAX_XRES;
122 drm->mode_config.max_height = HDLCD_MAX_YRES;
123 drm->mode_config.funcs = &hdlcd_mode_config_funcs;
124}
125
126static void hdlcd_lastclose(struct drm_device *drm)
127{
128 struct hdlcd_drm_private *hdlcd = drm->dev_private;
129
130 drm_fbdev_cma_restore_mode(hdlcd->fbdev);
131}
132
133static irqreturn_t hdlcd_irq(int irq, void *arg)
134{
135 struct drm_device *drm = arg;
136 struct hdlcd_drm_private *hdlcd = drm->dev_private;
137 unsigned long irq_status;
138
139 irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
140
141#ifdef CONFIG_DEBUG_FS
142 if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
143 atomic_inc(&hdlcd->buffer_underrun_count);
144
145 if (irq_status & HDLCD_INTERRUPT_DMA_END)
146 atomic_inc(&hdlcd->dma_end_count);
147
148 if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
149 atomic_inc(&hdlcd->bus_error_count);
150
151 if (irq_status & HDLCD_INTERRUPT_VSYNC)
152 atomic_inc(&hdlcd->vsync_count);
153
154#endif
Daniel Vetter38c8c22c2016-05-31 18:21:13 +0200155 if (irq_status & HDLCD_INTERRUPT_VSYNC)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100156 drm_crtc_handle_vblank(&hdlcd->crtc);
157
Liviu Dudau8e22d792015-04-02 19:48:39 +0100158 /* acknowledge interrupt(s) */
159 hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
160
161 return IRQ_HANDLED;
162}
163
164static void hdlcd_irq_preinstall(struct drm_device *drm)
165{
166 struct hdlcd_drm_private *hdlcd = drm->dev_private;
167 /* Ensure interrupts are disabled */
168 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
169 hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
170}
171
172static int hdlcd_irq_postinstall(struct drm_device *drm)
173{
174#ifdef CONFIG_DEBUG_FS
175 struct hdlcd_drm_private *hdlcd = drm->dev_private;
176 unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
177
178 /* enable debug interrupts */
179 irq_mask |= HDLCD_DEBUG_INT_MASK;
180
181 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
182#endif
183 return 0;
184}
185
186static void hdlcd_irq_uninstall(struct drm_device *drm)
187{
188 struct hdlcd_drm_private *hdlcd = drm->dev_private;
189 /* disable all the interrupts that we might have enabled */
190 unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
191
192#ifdef CONFIG_DEBUG_FS
193 /* disable debug interrupts */
194 irq_mask &= ~HDLCD_DEBUG_INT_MASK;
195#endif
196
197 /* disable vsync interrupts */
198 irq_mask &= ~HDLCD_INTERRUPT_VSYNC;
199
200 hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
201}
202
Liviu Dudau8e22d792015-04-02 19:48:39 +0100203#ifdef CONFIG_DEBUG_FS
204static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
205{
206 struct drm_info_node *node = (struct drm_info_node *)m->private;
207 struct drm_device *drm = node->minor->dev;
208 struct hdlcd_drm_private *hdlcd = drm->dev_private;
209
210 seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
211 seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
212 seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
213 seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
214 return 0;
215}
216
217static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
218{
219 struct drm_info_node *node = (struct drm_info_node *)m->private;
220 struct drm_device *drm = node->minor->dev;
221 struct hdlcd_drm_private *hdlcd = drm->dev_private;
222 unsigned long clkrate = clk_get_rate(hdlcd->clk);
223 unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
224
225 seq_printf(m, "hw : %lu\n", clkrate);
226 seq_printf(m, "mode: %lu\n", mode_clock);
227 return 0;
228}
229
230static struct drm_info_list hdlcd_debugfs_list[] = {
231 { "interrupt_count", hdlcd_show_underrun_count, 0 },
232 { "clocks", hdlcd_show_pxlclock, 0 },
Liviu Dudauf6c68b42016-06-01 15:07:02 +0100233 { "fb", drm_fb_cma_debugfs_show, 0 },
Liviu Dudau8e22d792015-04-02 19:48:39 +0100234};
235
236static int hdlcd_debugfs_init(struct drm_minor *minor)
237{
238 return drm_debugfs_create_files(hdlcd_debugfs_list,
239 ARRAY_SIZE(hdlcd_debugfs_list), minor->debugfs_root, minor);
240}
Liviu Dudau8e22d792015-04-02 19:48:39 +0100241#endif
242
Daniel Vetterd55f7e52017-03-08 15:12:56 +0100243DEFINE_DRM_GEM_CMA_FOPS(fops);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100244
245static struct drm_driver hdlcd_driver = {
246 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
247 DRIVER_MODESET | DRIVER_PRIME |
248 DRIVER_ATOMIC,
249 .lastclose = hdlcd_lastclose,
250 .irq_handler = hdlcd_irq,
251 .irq_preinstall = hdlcd_irq_preinstall,
252 .irq_postinstall = hdlcd_irq_postinstall,
253 .irq_uninstall = hdlcd_irq_uninstall,
Daniel Vetter6d910bf2016-05-30 19:53:17 +0200254 .gem_free_object_unlocked = drm_gem_cma_free_object,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100255 .gem_vm_ops = &drm_gem_cma_vm_ops,
256 .dumb_create = drm_gem_cma_dumb_create,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100257 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
258 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
259 .gem_prime_export = drm_gem_prime_export,
260 .gem_prime_import = drm_gem_prime_import,
261 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
262 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
263 .gem_prime_vmap = drm_gem_cma_prime_vmap,
264 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
265 .gem_prime_mmap = drm_gem_cma_prime_mmap,
266#ifdef CONFIG_DEBUG_FS
267 .debugfs_init = hdlcd_debugfs_init,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100268#endif
269 .fops = &fops,
270 .name = "hdlcd",
271 .desc = "ARM HDLCD Controller DRM",
272 .date = "20151021",
273 .major = 1,
274 .minor = 0,
275};
276
277static int hdlcd_drm_bind(struct device *dev)
278{
279 struct drm_device *drm;
280 struct hdlcd_drm_private *hdlcd;
281 int ret;
282
283 hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
284 if (!hdlcd)
285 return -ENOMEM;
286
287 drm = drm_dev_alloc(&hdlcd_driver, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200288 if (IS_ERR(drm))
289 return PTR_ERR(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100290
291 drm->dev_private = hdlcd;
Liviu Dudaua95acec2016-05-17 10:06:54 +0100292 dev_set_drvdata(dev, drm);
293
Liviu Dudau8e22d792015-04-02 19:48:39 +0100294 hdlcd_setup_mode_config(drm);
295 ret = hdlcd_load(drm, 0);
296 if (ret)
297 goto err_free;
298
Liviu Dudaude5cc812017-06-06 15:05:21 +0100299 /* Set the CRTC's port so that the encoder component can find it */
300 hdlcd->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
301
Liviu Dudau8e22d792015-04-02 19:48:39 +0100302 ret = component_bind_all(dev, drm);
303 if (ret) {
304 DRM_ERROR("Failed to bind all components\n");
Brian Starkey90731c22016-10-24 15:27:59 +0100305 goto err_unload;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100306 }
307
Liviu Dudaua95acec2016-05-17 10:06:54 +0100308 ret = pm_runtime_set_active(dev);
309 if (ret)
310 goto err_pm_active;
311
312 pm_runtime_enable(dev);
313
Liviu Dudau8e22d792015-04-02 19:48:39 +0100314 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
315 if (ret < 0) {
316 DRM_ERROR("failed to initialise vblank\n");
317 goto err_vblank;
318 }
Liviu Dudau8e22d792015-04-02 19:48:39 +0100319
320 drm_mode_config_reset(drm);
321 drm_kms_helper_poll_init(drm);
322
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200323 hdlcd->fbdev = drm_fbdev_cma_init(drm, 32,
Liviu Dudau8e22d792015-04-02 19:48:39 +0100324 drm->mode_config.num_connector);
325
326 if (IS_ERR(hdlcd->fbdev)) {
327 ret = PTR_ERR(hdlcd->fbdev);
328 hdlcd->fbdev = NULL;
329 goto err_fbdev;
330 }
331
Brian Starkey90731c22016-10-24 15:27:59 +0100332 ret = drm_dev_register(drm, 0);
333 if (ret)
334 goto err_register;
335
Liviu Dudau8e22d792015-04-02 19:48:39 +0100336 return 0;
337
Brian Starkey90731c22016-10-24 15:27:59 +0100338err_register:
339 if (hdlcd->fbdev) {
340 drm_fbdev_cma_fini(hdlcd->fbdev);
341 hdlcd->fbdev = NULL;
342 }
Liviu Dudau8e22d792015-04-02 19:48:39 +0100343err_fbdev:
344 drm_kms_helper_poll_fini(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100345err_vblank:
Liviu Dudaua95acec2016-05-17 10:06:54 +0100346 pm_runtime_disable(drm->dev);
347err_pm_active:
Liviu Dudau8e22d792015-04-02 19:48:39 +0100348 component_unbind_all(dev, drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100349err_unload:
Liviu Dudaude5cc812017-06-06 15:05:21 +0100350 of_node_put(hdlcd->crtc.port);
351 hdlcd->crtc.port = NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100352 drm_irq_uninstall(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100353 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100354err_free:
Robin Murphy747e5a52016-11-24 14:40:50 +0000355 drm_mode_config_cleanup(drm);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100356 dev_set_drvdata(dev, NULL);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100357 drm_dev_unref(drm);
358
359 return ret;
360}
361
362static void hdlcd_drm_unbind(struct device *dev)
363{
364 struct drm_device *drm = dev_get_drvdata(dev);
365 struct hdlcd_drm_private *hdlcd = drm->dev_private;
366
Brian Starkey90731c22016-10-24 15:27:59 +0100367 drm_dev_unregister(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100368 if (hdlcd->fbdev) {
369 drm_fbdev_cma_fini(hdlcd->fbdev);
370 hdlcd->fbdev = NULL;
371 }
372 drm_kms_helper_poll_fini(drm);
373 component_unbind_all(dev, drm);
Liviu Dudaude5cc812017-06-06 15:05:21 +0100374 of_node_put(hdlcd->crtc.port);
375 hdlcd->crtc.port = NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100376 pm_runtime_get_sync(drm->dev);
377 drm_irq_uninstall(drm);
378 pm_runtime_put_sync(drm->dev);
379 pm_runtime_disable(drm->dev);
380 of_reserved_mem_device_release(drm->dev);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100381 drm_mode_config_cleanup(drm);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100382 drm_dev_unref(drm);
383 drm->dev_private = NULL;
384 dev_set_drvdata(dev, NULL);
385}
386
387static const struct component_master_ops hdlcd_master_ops = {
388 .bind = hdlcd_drm_bind,
389 .unbind = hdlcd_drm_unbind,
390};
391
392static int compare_dev(struct device *dev, void *data)
393{
394 return dev->of_node == data;
395}
396
397static int hdlcd_probe(struct platform_device *pdev)
398{
Rob Herring86418f92017-03-22 08:26:06 -0500399 struct device_node *port;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100400 struct component_match *match = NULL;
401
Liviu Dudau8e22d792015-04-02 19:48:39 +0100402 /* there is only one output port inside each device, find it */
Rob Herring86418f92017-03-22 08:26:06 -0500403 port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
404 if (!port)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100405 return -ENODEV;
406
Russell King97ac0e42016-10-19 11:28:27 +0100407 drm_of_component_match_add(&pdev->dev, &match, compare_dev, port);
408 of_node_put(port);
Liviu Dudau8e22d792015-04-02 19:48:39 +0100409
410 return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
411 match);
412}
413
414static int hdlcd_remove(struct platform_device *pdev)
415{
416 component_master_del(&pdev->dev, &hdlcd_master_ops);
417 return 0;
418}
419
420static const struct of_device_id hdlcd_of_match[] = {
421 { .compatible = "arm,hdlcd" },
422 {},
423};
424MODULE_DEVICE_TABLE(of, hdlcd_of_match);
425
426static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
427{
428 struct drm_device *drm = dev_get_drvdata(dev);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100429 struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100430
Liviu Dudaua95acec2016-05-17 10:06:54 +0100431 if (!hdlcd)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100432 return 0;
433
Liviu Dudaua95acec2016-05-17 10:06:54 +0100434 drm_kms_helper_poll_disable(drm);
435
436 hdlcd->state = drm_atomic_helper_suspend(drm);
437 if (IS_ERR(hdlcd->state)) {
438 drm_kms_helper_poll_enable(drm);
439 return PTR_ERR(hdlcd->state);
440 }
441
Liviu Dudau8e22d792015-04-02 19:48:39 +0100442 return 0;
443}
444
445static int __maybe_unused hdlcd_pm_resume(struct device *dev)
446{
447 struct drm_device *drm = dev_get_drvdata(dev);
Liviu Dudaua95acec2016-05-17 10:06:54 +0100448 struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
Liviu Dudau8e22d792015-04-02 19:48:39 +0100449
Liviu Dudaua95acec2016-05-17 10:06:54 +0100450 if (!hdlcd)
Liviu Dudau8e22d792015-04-02 19:48:39 +0100451 return 0;
452
Liviu Dudaua95acec2016-05-17 10:06:54 +0100453 drm_atomic_helper_resume(drm, hdlcd->state);
454 drm_kms_helper_poll_enable(drm);
455 pm_runtime_set_active(dev);
456
Liviu Dudau8e22d792015-04-02 19:48:39 +0100457 return 0;
458}
459
460static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
461
462static struct platform_driver hdlcd_platform_driver = {
463 .probe = hdlcd_probe,
464 .remove = hdlcd_remove,
465 .driver = {
466 .name = "hdlcd",
467 .pm = &hdlcd_pm_ops,
468 .of_match_table = hdlcd_of_match,
469 },
470};
471
472module_platform_driver(hdlcd_platform_driver);
473
474MODULE_AUTHOR("Liviu Dudau");
475MODULE_DESCRIPTION("ARM HDLCD DRM driver");
476MODULE_LICENSE("GPL v2");