2 * Copyright (C) 2012 Russell King
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.
9 #include <linux/component.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
13 #include <drm/drm_crtc_helper.h>
14 #include "armada_crtc.h"
15 #include "armada_drm.h"
16 #include "armada_gem.h"
17 #include "armada_hw.h"
18 #include <drm/armada_drm.h>
19 #include "armada_ioctlP.h"
21 #ifdef CONFIG_DRM_ARMADA_TDA1998X
22 #include <drm/i2c/tda998x.h>
23 #include "armada_slave.h"
25 static struct tda998x_encoder_params params
= {
26 /* With 0x24, there is no translation between vp_out and int_vp
27 FB LCD out Pins VIP Int Vp
28 R:23:16 R:7:0 VPC7:0 7:0 7:0[R]
29 G:15:8 G:15:8 VPB7:0 23:16 23:16[G]
30 B:7:0 B:23:16 VPA7:0 15:8 15:8[B]
40 .audio_format
= AFMT_SPDIF
,
41 .audio_sample_rate
= 44100,
44 static const struct armada_drm_slave_config tda19988_config
= {
46 .crtcs
= 1 << 0, /* Only LCD0 at the moment */
47 .polled
= DRM_CONNECTOR_POLL_CONNECT
| DRM_CONNECTOR_POLL_DISCONNECT
,
48 .interlace_allowed
= true,
52 .platform_data
= ¶ms
,
57 static bool is_componentized(struct device
*dev
)
59 return dev
->of_node
|| dev
->platform_data
;
62 static void armada_drm_unref_work(struct work_struct
*work
)
64 struct armada_private
*priv
=
65 container_of(work
, struct armada_private
, fb_unref_work
);
66 struct drm_framebuffer
*fb
;
68 while (kfifo_get(&priv
->fb_unref
, &fb
))
69 drm_framebuffer_unreference(fb
);
72 /* Must be called with dev->event_lock held */
73 void __armada_drm_queue_unref_work(struct drm_device
*dev
,
74 struct drm_framebuffer
*fb
)
76 struct armada_private
*priv
= dev
->dev_private
;
78 WARN_ON(!kfifo_put(&priv
->fb_unref
, fb
));
79 schedule_work(&priv
->fb_unref_work
);
82 void armada_drm_queue_unref_work(struct drm_device
*dev
,
83 struct drm_framebuffer
*fb
)
87 spin_lock_irqsave(&dev
->event_lock
, flags
);
88 __armada_drm_queue_unref_work(dev
, fb
);
89 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
92 static int armada_drm_load(struct drm_device
*dev
, unsigned long flags
)
94 const struct platform_device_id
*id
;
95 const struct armada_variant
*variant
;
96 struct armada_private
*priv
;
97 struct resource
*res
[ARRAY_SIZE(priv
->dcrtc
)];
98 struct resource
*mem
= NULL
;
101 memset(res
, 0, sizeof(res
));
103 for (n
= i
= 0; ; n
++) {
104 struct resource
*r
= platform_get_resource(dev
->platformdev
,
109 /* Resources above 64K are graphics memory */
110 if (resource_size(r
) > SZ_64K
)
112 else if (i
< ARRAY_SIZE(priv
->dcrtc
))
121 if (!devm_request_mem_region(dev
->dev
, mem
->start
,
122 resource_size(mem
), "armada-drm"))
125 priv
= devm_kzalloc(dev
->dev
, sizeof(*priv
), GFP_KERNEL
);
127 DRM_ERROR("failed to allocate private\n");
131 platform_set_drvdata(dev
->platformdev
, dev
);
132 dev
->dev_private
= priv
;
134 /* Get the implementation specific driver data. */
135 id
= platform_get_device_id(dev
->platformdev
);
139 variant
= (const struct armada_variant
*)id
->driver_data
;
141 INIT_WORK(&priv
->fb_unref_work
, armada_drm_unref_work
);
142 INIT_KFIFO(priv
->fb_unref
);
144 /* Mode setting support */
145 drm_mode_config_init(dev
);
146 dev
->mode_config
.min_width
= 320;
147 dev
->mode_config
.min_height
= 200;
150 * With vscale enabled, the maximum width is 1920 due to the
151 * 1920 by 3 lines RAM
153 dev
->mode_config
.max_width
= 1920;
154 dev
->mode_config
.max_height
= 2048;
156 dev
->mode_config
.preferred_depth
= 24;
157 dev
->mode_config
.funcs
= &armada_drm_mode_config_funcs
;
158 drm_mm_init(&priv
->linear
, mem
->start
, resource_size(mem
));
160 /* Create all LCD controllers */
161 for (n
= 0; n
< ARRAY_SIZE(priv
->dcrtc
); n
++) {
167 irq
= platform_get_irq(dev
->platformdev
, n
);
171 ret
= armada_drm_crtc_create(dev
, dev
->dev
, res
[n
], irq
,
177 if (is_componentized(dev
->dev
)) {
178 ret
= component_bind_all(dev
->dev
, dev
);
182 #ifdef CONFIG_DRM_ARMADA_TDA1998X
183 ret
= armada_drm_connector_slave_create(dev
, &tda19988_config
);
189 ret
= drm_vblank_init(dev
, dev
->mode_config
.num_crtc
);
193 dev
->irq_enabled
= true;
194 dev
->vblank_disable_allowed
= 1;
196 ret
= armada_fbdev_init(dev
);
200 drm_kms_helper_poll_init(dev
);
205 if (is_componentized(dev
->dev
))
206 component_unbind_all(dev
->dev
, dev
);
208 drm_mode_config_cleanup(dev
);
209 drm_mm_takedown(&priv
->linear
);
210 flush_work(&priv
->fb_unref_work
);
215 static int armada_drm_unload(struct drm_device
*dev
)
217 struct armada_private
*priv
= dev
->dev_private
;
219 drm_kms_helper_poll_fini(dev
);
220 armada_fbdev_fini(dev
);
222 if (is_componentized(dev
->dev
))
223 component_unbind_all(dev
->dev
, dev
);
225 drm_mode_config_cleanup(dev
);
226 drm_mm_takedown(&priv
->linear
);
227 flush_work(&priv
->fb_unref_work
);
228 dev
->dev_private
= NULL
;
233 void armada_drm_vbl_event_add(struct armada_crtc
*dcrtc
,
234 struct armada_vbl_event
*evt
)
238 spin_lock_irqsave(&dcrtc
->irq_lock
, flags
);
239 if (list_empty(&evt
->node
)) {
240 list_add_tail(&evt
->node
, &dcrtc
->vbl_list
);
242 drm_vblank_get(dcrtc
->crtc
.dev
, dcrtc
->num
);
244 spin_unlock_irqrestore(&dcrtc
->irq_lock
, flags
);
247 void armada_drm_vbl_event_remove(struct armada_crtc
*dcrtc
,
248 struct armada_vbl_event
*evt
)
250 if (!list_empty(&evt
->node
)) {
251 list_del_init(&evt
->node
);
252 drm_vblank_put(dcrtc
->crtc
.dev
, dcrtc
->num
);
256 /* These are called under the vbl_lock. */
257 static int armada_drm_enable_vblank(struct drm_device
*dev
, int crtc
)
259 struct armada_private
*priv
= dev
->dev_private
;
260 armada_drm_crtc_enable_irq(priv
->dcrtc
[crtc
], VSYNC_IRQ_ENA
);
264 static void armada_drm_disable_vblank(struct drm_device
*dev
, int crtc
)
266 struct armada_private
*priv
= dev
->dev_private
;
267 armada_drm_crtc_disable_irq(priv
->dcrtc
[crtc
], VSYNC_IRQ_ENA
);
270 static struct drm_ioctl_desc armada_ioctls
[] = {
271 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE
, armada_gem_create_ioctl
,
273 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP
, armada_gem_mmap_ioctl
,
275 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE
, armada_gem_pwrite_ioctl
,
279 static void armada_drm_lastclose(struct drm_device
*dev
)
281 armada_fbdev_lastclose(dev
);
284 static const struct file_operations armada_drm_fops
= {
285 .owner
= THIS_MODULE
,
289 .unlocked_ioctl
= drm_ioctl
,
290 .mmap
= drm_gem_mmap
,
292 .release
= drm_release
,
295 static struct drm_driver armada_drm_driver
= {
296 .load
= armada_drm_load
,
300 .lastclose
= armada_drm_lastclose
,
301 .unload
= armada_drm_unload
,
302 .set_busid
= drm_platform_set_busid
,
303 .get_vblank_counter
= drm_vblank_count
,
304 .enable_vblank
= armada_drm_enable_vblank
,
305 .disable_vblank
= armada_drm_disable_vblank
,
306 #ifdef CONFIG_DEBUG_FS
307 .debugfs_init
= armada_drm_debugfs_init
,
308 .debugfs_cleanup
= armada_drm_debugfs_cleanup
,
310 .gem_free_object
= armada_gem_free_object
,
311 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
312 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
313 .gem_prime_export
= armada_gem_prime_export
,
314 .gem_prime_import
= armada_gem_prime_import
,
315 .dumb_create
= armada_gem_dumb_create
,
316 .dumb_map_offset
= armada_gem_dumb_map_offset
,
317 .dumb_destroy
= armada_gem_dumb_destroy
,
318 .gem_vm_ops
= &armada_gem_vm_ops
,
321 .name
= "armada-drm",
322 .desc
= "Armada SoC DRM",
324 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
|
325 DRIVER_HAVE_IRQ
| DRIVER_PRIME
,
326 .ioctls
= armada_ioctls
,
327 .fops
= &armada_drm_fops
,
330 static int armada_drm_bind(struct device
*dev
)
332 return drm_platform_init(&armada_drm_driver
, to_platform_device(dev
));
335 static void armada_drm_unbind(struct device
*dev
)
337 drm_put_dev(dev_get_drvdata(dev
));
340 static int compare_of(struct device
*dev
, void *data
)
342 return dev
->of_node
== data
;
345 static int compare_dev_name(struct device
*dev
, void *data
)
347 const char *name
= data
;
348 return !strcmp(dev_name(dev
), name
);
351 static void armada_add_endpoints(struct device
*dev
,
352 struct component_match
**match
, struct device_node
*port
)
354 struct device_node
*ep
, *remote
;
356 for_each_child_of_node(port
, ep
) {
357 remote
= of_graph_get_remote_port_parent(ep
);
358 if (!remote
|| !of_device_is_available(remote
)) {
361 } else if (!of_device_is_available(remote
->parent
)) {
362 dev_warn(dev
, "parent device of %s is not available\n",
368 component_match_add(dev
, match
, compare_of
, remote
);
373 static int armada_drm_find_components(struct device
*dev
,
374 struct component_match
**match
)
376 struct device_node
*port
;
380 struct device_node
*np
= dev
->of_node
;
383 port
= of_parse_phandle(np
, "ports", i
);
387 component_match_add(dev
, match
, compare_of
, port
);
392 dev_err(dev
, "missing 'ports' property\n");
397 port
= of_parse_phandle(np
, "ports", i
);
401 armada_add_endpoints(dev
, match
, port
);
404 } else if (dev
->platform_data
) {
405 char **devices
= dev
->platform_data
;
408 for (i
= 0; devices
[i
]; i
++)
409 component_match_add(dev
, match
, compare_dev_name
,
413 dev_err(dev
, "missing 'ports' property\n");
417 for (i
= 0; devices
[i
]; i
++) {
418 d
= bus_find_device_by_name(&platform_bus_type
, NULL
,
420 if (d
&& d
->of_node
) {
421 for_each_child_of_node(d
->of_node
, port
)
422 armada_add_endpoints(dev
, match
, port
);
431 static const struct component_master_ops armada_master_ops
= {
432 .bind
= armada_drm_bind
,
433 .unbind
= armada_drm_unbind
,
436 static int armada_drm_probe(struct platform_device
*pdev
)
438 if (is_componentized(&pdev
->dev
)) {
439 struct component_match
*match
= NULL
;
442 ret
= armada_drm_find_components(&pdev
->dev
, &match
);
446 return component_master_add_with_match(&pdev
->dev
,
447 &armada_master_ops
, match
);
449 return drm_platform_init(&armada_drm_driver
, pdev
);
453 static int armada_drm_remove(struct platform_device
*pdev
)
455 if (is_componentized(&pdev
->dev
))
456 component_master_del(&pdev
->dev
, &armada_master_ops
);
458 drm_put_dev(platform_get_drvdata(pdev
));
462 static const struct platform_device_id armada_drm_platform_ids
[] = {
464 .name
= "armada-drm",
465 .driver_data
= (unsigned long)&armada510_ops
,
467 .name
= "armada-510-drm",
468 .driver_data
= (unsigned long)&armada510_ops
,
472 MODULE_DEVICE_TABLE(platform
, armada_drm_platform_ids
);
474 static struct platform_driver armada_drm_platform_driver
= {
475 .probe
= armada_drm_probe
,
476 .remove
= armada_drm_remove
,
478 .name
= "armada-drm",
480 .id_table
= armada_drm_platform_ids
,
483 static int __init
armada_drm_init(void)
487 armada_drm_driver
.num_ioctls
= ARRAY_SIZE(armada_ioctls
);
489 ret
= platform_driver_register(&armada_lcd_platform_driver
);
492 ret
= platform_driver_register(&armada_drm_platform_driver
);
494 platform_driver_unregister(&armada_lcd_platform_driver
);
497 module_init(armada_drm_init
);
499 static void __exit
armada_drm_exit(void)
501 platform_driver_unregister(&armada_drm_platform_driver
);
502 platform_driver_unregister(&armada_lcd_platform_driver
);
504 module_exit(armada_drm_exit
);
506 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
507 MODULE_DESCRIPTION("Armada DRM Driver");
508 MODULE_LICENSE("GPL");
509 MODULE_ALIAS("platform:armada-drm");