xref: /linux/drivers/gpu/drm/armada/armada_drv.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
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>
9 #include <linux/component.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <drm/drmP.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"
20 
21 #ifdef CONFIG_DRM_ARMADA_TDA1998X
22 #include <drm/i2c/tda998x.h>
23 #include "armada_slave.h"
24 
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]
31 	*/
32 	.swap_a = 2,
33 	.swap_b = 3,
34 	.swap_c = 4,
35 	.swap_d = 5,
36 	.swap_e = 0,
37 	.swap_f = 1,
38 	.audio_cfg = BIT(2),
39 	.audio_frame[1] = 1,
40 	.audio_format = AFMT_SPDIF,
41 	.audio_sample_rate = 44100,
42 };
43 
44 static const struct armada_drm_slave_config tda19988_config = {
45 	.i2c_adapter_id = 0,
46 	.crtcs = 1 << 0, /* Only LCD0 at the moment */
47 	.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT,
48 	.interlace_allowed = true,
49 	.info = {
50 		.type = "tda998x",
51 		.addr = 0x70,
52 		.platform_data = &params,
53 	},
54 };
55 #endif
56 
57 static bool is_componentized(struct device *dev)
58 {
59 	return dev->of_node || dev->platform_data;
60 }
61 
62 static void armada_drm_unref_work(struct work_struct *work)
63 {
64 	struct armada_private *priv =
65 		container_of(work, struct armada_private, fb_unref_work);
66 	struct drm_framebuffer *fb;
67 
68 	while (kfifo_get(&priv->fb_unref, &fb))
69 		drm_framebuffer_unreference(fb);
70 }
71 
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)
75 {
76 	struct armada_private *priv = dev->dev_private;
77 
78 	WARN_ON(!kfifo_put(&priv->fb_unref, fb));
79 	schedule_work(&priv->fb_unref_work);
80 }
81 
82 void armada_drm_queue_unref_work(struct drm_device *dev,
83 	struct drm_framebuffer *fb)
84 {
85 	unsigned long flags;
86 
87 	spin_lock_irqsave(&dev->event_lock, flags);
88 	__armada_drm_queue_unref_work(dev, fb);
89 	spin_unlock_irqrestore(&dev->event_lock, flags);
90 }
91 
92 static int armada_drm_load(struct drm_device *dev, unsigned long flags)
93 {
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;
99 	int ret, n, i;
100 
101 	memset(res, 0, sizeof(res));
102 
103 	for (n = i = 0; ; n++) {
104 		struct resource *r = platform_get_resource(dev->platformdev,
105 							   IORESOURCE_MEM, n);
106 		if (!r)
107 			break;
108 
109 		/* Resources above 64K are graphics memory */
110 		if (resource_size(r) > SZ_64K)
111 			mem = r;
112 		else if (i < ARRAY_SIZE(priv->dcrtc))
113 			res[i++] = r;
114 		else
115 			return -EINVAL;
116 	}
117 
118 	if (!mem)
119 		return -ENXIO;
120 
121 	if (!devm_request_mem_region(dev->dev, mem->start,
122 			resource_size(mem), "armada-drm"))
123 		return -EBUSY;
124 
125 	priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
126 	if (!priv) {
127 		DRM_ERROR("failed to allocate private\n");
128 		return -ENOMEM;
129 	}
130 
131 	platform_set_drvdata(dev->platformdev, dev);
132 	dev->dev_private = priv;
133 
134 	/* Get the implementation specific driver data. */
135 	id = platform_get_device_id(dev->platformdev);
136 	if (!id)
137 		return -ENXIO;
138 
139 	variant = (const struct armada_variant *)id->driver_data;
140 
141 	INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
142 	INIT_KFIFO(priv->fb_unref);
143 
144 	/* Mode setting support */
145 	drm_mode_config_init(dev);
146 	dev->mode_config.min_width = 320;
147 	dev->mode_config.min_height = 200;
148 
149 	/*
150 	 * With vscale enabled, the maximum width is 1920 due to the
151 	 * 1920 by 3 lines RAM
152 	 */
153 	dev->mode_config.max_width = 1920;
154 	dev->mode_config.max_height = 2048;
155 
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));
159 
160 	/* Create all LCD controllers */
161 	for (n = 0; n < ARRAY_SIZE(priv->dcrtc); n++) {
162 		int irq;
163 
164 		if (!res[n])
165 			break;
166 
167 		irq = platform_get_irq(dev->platformdev, n);
168 		if (irq < 0)
169 			goto err_kms;
170 
171 		ret = armada_drm_crtc_create(dev, dev->dev, res[n], irq,
172 					     variant, NULL);
173 		if (ret)
174 			goto err_kms;
175 	}
176 
177 	if (is_componentized(dev->dev)) {
178 		ret = component_bind_all(dev->dev, dev);
179 		if (ret)
180 			goto err_kms;
181 	} else {
182 #ifdef CONFIG_DRM_ARMADA_TDA1998X
183 		ret = armada_drm_connector_slave_create(dev, &tda19988_config);
184 		if (ret)
185 			goto err_kms;
186 #endif
187 	}
188 
189 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
190 	if (ret)
191 		goto err_comp;
192 
193 	dev->irq_enabled = true;
194 	dev->vblank_disable_allowed = 1;
195 
196 	ret = armada_fbdev_init(dev);
197 	if (ret)
198 		goto err_comp;
199 
200 	drm_kms_helper_poll_init(dev);
201 
202 	return 0;
203 
204  err_comp:
205 	if (is_componentized(dev->dev))
206 		component_unbind_all(dev->dev, dev);
207  err_kms:
208 	drm_mode_config_cleanup(dev);
209 	drm_mm_takedown(&priv->linear);
210 	flush_work(&priv->fb_unref_work);
211 
212 	return ret;
213 }
214 
215 static int armada_drm_unload(struct drm_device *dev)
216 {
217 	struct armada_private *priv = dev->dev_private;
218 
219 	drm_kms_helper_poll_fini(dev);
220 	armada_fbdev_fini(dev);
221 
222 	if (is_componentized(dev->dev))
223 		component_unbind_all(dev->dev, dev);
224 
225 	drm_mode_config_cleanup(dev);
226 	drm_mm_takedown(&priv->linear);
227 	flush_work(&priv->fb_unref_work);
228 	dev->dev_private = NULL;
229 
230 	return 0;
231 }
232 
233 void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
234 	struct armada_vbl_event *evt)
235 {
236 	unsigned long flags;
237 
238 	spin_lock_irqsave(&dcrtc->irq_lock, flags);
239 	if (list_empty(&evt->node)) {
240 		list_add_tail(&evt->node, &dcrtc->vbl_list);
241 
242 		drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
243 	}
244 	spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
245 }
246 
247 void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
248 	struct armada_vbl_event *evt)
249 {
250 	if (!list_empty(&evt->node)) {
251 		list_del_init(&evt->node);
252 		drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
253 	}
254 }
255 
256 /* These are called under the vbl_lock. */
257 static int armada_drm_enable_vblank(struct drm_device *dev, int crtc)
258 {
259 	struct armada_private *priv = dev->dev_private;
260 	armada_drm_crtc_enable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA);
261 	return 0;
262 }
263 
264 static void armada_drm_disable_vblank(struct drm_device *dev, int crtc)
265 {
266 	struct armada_private *priv = dev->dev_private;
267 	armada_drm_crtc_disable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA);
268 }
269 
270 static struct drm_ioctl_desc armada_ioctls[] = {
271 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,
272 		DRM_UNLOCKED),
273 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl,
274 		DRM_UNLOCKED),
275 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl,
276 		DRM_UNLOCKED),
277 };
278 
279 static void armada_drm_lastclose(struct drm_device *dev)
280 {
281 	armada_fbdev_lastclose(dev);
282 }
283 
284 static const struct file_operations armada_drm_fops = {
285 	.owner			= THIS_MODULE,
286 	.llseek			= no_llseek,
287 	.read			= drm_read,
288 	.poll			= drm_poll,
289 	.unlocked_ioctl		= drm_ioctl,
290 	.mmap			= drm_gem_mmap,
291 	.open			= drm_open,
292 	.release		= drm_release,
293 };
294 
295 static struct drm_driver armada_drm_driver = {
296 	.load			= armada_drm_load,
297 	.open			= NULL,
298 	.preclose		= NULL,
299 	.postclose		= NULL,
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,
309 #endif
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,
319 	.major			= 1,
320 	.minor			= 0,
321 	.name			= "armada-drm",
322 	.desc			= "Armada SoC DRM",
323 	.date			= "20120730",
324 	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
325 				  DRIVER_HAVE_IRQ | DRIVER_PRIME,
326 	.ioctls			= armada_ioctls,
327 	.fops			= &armada_drm_fops,
328 };
329 
330 static int armada_drm_bind(struct device *dev)
331 {
332 	return drm_platform_init(&armada_drm_driver, to_platform_device(dev));
333 }
334 
335 static void armada_drm_unbind(struct device *dev)
336 {
337 	drm_put_dev(dev_get_drvdata(dev));
338 }
339 
340 static int compare_of(struct device *dev, void *data)
341 {
342 	return dev->of_node == data;
343 }
344 
345 static int compare_dev_name(struct device *dev, void *data)
346 {
347 	const char *name = data;
348 	return !strcmp(dev_name(dev), name);
349 }
350 
351 static void armada_add_endpoints(struct device *dev,
352 	struct component_match **match, struct device_node *port)
353 {
354 	struct device_node *ep, *remote;
355 
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)) {
359 			of_node_put(remote);
360 			continue;
361 		} else if (!of_device_is_available(remote->parent)) {
362 			dev_warn(dev, "parent device of %s is not available\n",
363 				 remote->full_name);
364 			of_node_put(remote);
365 			continue;
366 		}
367 
368 		component_match_add(dev, match, compare_of, remote);
369 		of_node_put(remote);
370 	}
371 }
372 
373 static int armada_drm_find_components(struct device *dev,
374 	struct component_match **match)
375 {
376 	struct device_node *port;
377 	int i;
378 
379 	if (dev->of_node) {
380 		struct device_node *np = dev->of_node;
381 
382 		for (i = 0; ; i++) {
383 			port = of_parse_phandle(np, "ports", i);
384 			if (!port)
385 				break;
386 
387 			component_match_add(dev, match, compare_of, port);
388 			of_node_put(port);
389 		}
390 
391 		if (i == 0) {
392 			dev_err(dev, "missing 'ports' property\n");
393 			return -ENODEV;
394 		}
395 
396 		for (i = 0; ; i++) {
397 			port = of_parse_phandle(np, "ports", i);
398 			if (!port)
399 				break;
400 
401 			armada_add_endpoints(dev, match, port);
402 			of_node_put(port);
403 		}
404 	} else if (dev->platform_data) {
405 		char **devices = dev->platform_data;
406 		struct device *d;
407 
408 		for (i = 0; devices[i]; i++)
409 			component_match_add(dev, match, compare_dev_name,
410 					    devices[i]);
411 
412 		if (i == 0) {
413 			dev_err(dev, "missing 'ports' property\n");
414 			return -ENODEV;
415 		}
416 
417 		for (i = 0; devices[i]; i++) {
418 			d = bus_find_device_by_name(&platform_bus_type, NULL,
419 					devices[i]);
420 			if (d && d->of_node) {
421 				for_each_child_of_node(d->of_node, port)
422 					armada_add_endpoints(dev, match, port);
423 			}
424 			put_device(d);
425 		}
426 	}
427 
428 	return 0;
429 }
430 
431 static const struct component_master_ops armada_master_ops = {
432 	.bind = armada_drm_bind,
433 	.unbind = armada_drm_unbind,
434 };
435 
436 static int armada_drm_probe(struct platform_device *pdev)
437 {
438 	if (is_componentized(&pdev->dev)) {
439 		struct component_match *match = NULL;
440 		int ret;
441 
442 		ret = armada_drm_find_components(&pdev->dev, &match);
443 		if (ret < 0)
444 			return ret;
445 
446 		return component_master_add_with_match(&pdev->dev,
447 				&armada_master_ops, match);
448 	} else {
449 		return drm_platform_init(&armada_drm_driver, pdev);
450 	}
451 }
452 
453 static int armada_drm_remove(struct platform_device *pdev)
454 {
455 	if (is_componentized(&pdev->dev))
456 		component_master_del(&pdev->dev, &armada_master_ops);
457 	else
458 		drm_put_dev(platform_get_drvdata(pdev));
459 	return 0;
460 }
461 
462 static const struct platform_device_id armada_drm_platform_ids[] = {
463 	{
464 		.name		= "armada-drm",
465 		.driver_data	= (unsigned long)&armada510_ops,
466 	}, {
467 		.name		= "armada-510-drm",
468 		.driver_data	= (unsigned long)&armada510_ops,
469 	},
470 	{ },
471 };
472 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
473 
474 static struct platform_driver armada_drm_platform_driver = {
475 	.probe	= armada_drm_probe,
476 	.remove	= armada_drm_remove,
477 	.driver	= {
478 		.name	= "armada-drm",
479 	},
480 	.id_table = armada_drm_platform_ids,
481 };
482 
483 static int __init armada_drm_init(void)
484 {
485 	int ret;
486 
487 	armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
488 
489 	ret = platform_driver_register(&armada_lcd_platform_driver);
490 	if (ret)
491 		return ret;
492 	ret = platform_driver_register(&armada_drm_platform_driver);
493 	if (ret)
494 		platform_driver_unregister(&armada_lcd_platform_driver);
495 	return ret;
496 }
497 module_init(armada_drm_init);
498 
499 static void __exit armada_drm_exit(void)
500 {
501 	platform_driver_unregister(&armada_drm_platform_driver);
502 	platform_driver_unregister(&armada_lcd_platform_driver);
503 }
504 module_exit(armada_drm_exit);
505 
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");
510