armada_drv.c 8.1 KB
Newer Older
R
Russell King 已提交
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2012 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/clk.h>
9
#include <linux/component.h>
R
Russell King 已提交
10
#include <linux/module.h>
11
#include <linux/of_graph.h>
R
Russell King 已提交
12
#include <drm/drm_crtc_helper.h>
13
#include <drm/drm_fb_helper.h>
14
#include <drm/drm_of.h>
R
Russell King 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include "armada_crtc.h"
#include "armada_drm.h"
#include "armada_gem.h"
#include "armada_hw.h"
#include <drm/armada_drm.h>
#include "armada_ioctlP.h"

static void armada_drm_unref_work(struct work_struct *work)
{
	struct armada_private *priv =
		container_of(work, struct armada_private, fb_unref_work);
	struct drm_framebuffer *fb;

	while (kfifo_get(&priv->fb_unref, &fb))
29
		drm_framebuffer_put(fb);
R
Russell King 已提交
30 31 32 33 34 35 36 37
}

/* Must be called with dev->event_lock held */
void __armada_drm_queue_unref_work(struct drm_device *dev,
	struct drm_framebuffer *fb)
{
	struct armada_private *priv = dev->dev_private;

38
	WARN_ON(!kfifo_put(&priv->fb_unref, fb));
R
Russell King 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
	schedule_work(&priv->fb_unref_work);
}

void armada_drm_queue_unref_work(struct drm_device *dev,
	struct drm_framebuffer *fb)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->event_lock, flags);
	__armada_drm_queue_unref_work(dev, fb);
	spin_unlock_irqrestore(&dev->event_lock, flags);
}

static struct drm_ioctl_desc armada_ioctls[] = {
53 54 55
	DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
	DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
	DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
R
Russell King 已提交
56 57
};

D
Daniel Vetter 已提交
58
DEFINE_DRM_GEM_FOPS(armada_drm_fops);
R
Russell King 已提交
59 60

static struct drm_driver armada_drm_driver = {
61
	.lastclose		= drm_fb_helper_lastclose,
62
	.gem_free_object_unlocked = armada_gem_free_object,
R
Russell King 已提交
63 64 65 66 67 68 69 70 71 72 73 74
	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
	.gem_prime_export	= armada_gem_prime_export,
	.gem_prime_import	= armada_gem_prime_import,
	.dumb_create		= armada_gem_dumb_create,
	.gem_vm_ops		= &armada_gem_vm_ops,
	.major			= 1,
	.minor			= 0,
	.name			= "armada-drm",
	.desc			= "Armada SoC DRM",
	.date			= "20120730",
	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
75
				  DRIVER_PRIME,
R
Russell King 已提交
76 77 78 79
	.ioctls			= armada_ioctls,
	.fops			= &armada_drm_fops,
};

80 81
static int armada_drm_bind(struct device *dev)
{
R
Russell King 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	struct armada_private *priv;
	struct resource *mem = NULL;
	int ret, n;

	for (n = 0; ; n++) {
		struct resource *r = platform_get_resource(to_platform_device(dev),
							   IORESOURCE_MEM, n);
		if (!r)
			break;

		/* Resources above 64K are graphics memory */
		if (resource_size(r) > SZ_64K)
			mem = r;
		else
			return -EINVAL;
	}

	if (!mem)
		return -ENXIO;

	if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
				     "armada-drm"))
		return -EBUSY;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	/*
	 * The drm_device structure must be at the start of
	 * armada_private for drm_dev_unref() to work correctly.
	 */
	BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);

	ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
	if (ret) {
		dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
			__func__, ret);
		kfree(priv);
		return ret;
	}

	priv->drm.dev_private = priv;

126
	dev_set_drvdata(dev, &priv->drm);
R
Russell King 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

	INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
	INIT_KFIFO(priv->fb_unref);

	/* Mode setting support */
	drm_mode_config_init(&priv->drm);
	priv->drm.mode_config.min_width = 320;
	priv->drm.mode_config.min_height = 200;

	/*
	 * With vscale enabled, the maximum width is 1920 due to the
	 * 1920 by 3 lines RAM
	 */
	priv->drm.mode_config.max_width = 1920;
	priv->drm.mode_config.max_height = 2048;

	priv->drm.mode_config.preferred_depth = 24;
	priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
	drm_mm_init(&priv->linear, mem->start, resource_size(mem));
	mutex_init(&priv->linear_lock);

	ret = component_bind_all(dev, &priv->drm);
	if (ret)
		goto err_kms;

	ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
	if (ret)
		goto err_comp;

	priv->drm.irq_enabled = true;

	ret = armada_fbdev_init(&priv->drm);
	if (ret)
		goto err_comp;

	drm_kms_helper_poll_init(&priv->drm);

	ret = drm_dev_register(&priv->drm, 0);
	if (ret)
		goto err_poll;

#ifdef CONFIG_DEBUG_FS
	armada_drm_debugfs_init(priv->drm.primary);
#endif

	return 0;

 err_poll:
	drm_kms_helper_poll_fini(&priv->drm);
	armada_fbdev_fini(&priv->drm);
 err_comp:
	component_unbind_all(dev, &priv->drm);
 err_kms:
	drm_mode_config_cleanup(&priv->drm);
	drm_mm_takedown(&priv->linear);
	flush_work(&priv->fb_unref_work);
	drm_dev_unref(&priv->drm);
	return ret;
185 186 187 188
}

static void armada_drm_unbind(struct device *dev)
{
R
Russell King 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	struct drm_device *drm = dev_get_drvdata(dev);
	struct armada_private *priv = drm->dev_private;

	drm_kms_helper_poll_fini(&priv->drm);
	armada_fbdev_fini(&priv->drm);

	drm_dev_unregister(&priv->drm);

	component_unbind_all(dev, &priv->drm);

	drm_mode_config_cleanup(&priv->drm);
	drm_mm_takedown(&priv->linear);
	flush_work(&priv->fb_unref_work);

	drm_dev_unref(&priv->drm);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}

static int compare_of(struct device *dev, void *data)
{
	return dev->of_node == data;
}

static int compare_dev_name(struct device *dev, void *data)
{
	const char *name = data;
	return !strcmp(dev_name(dev), name);
}

static void armada_add_endpoints(struct device *dev,
	struct component_match **match, struct device_node *port)
{
	struct device_node *ep, *remote;

	for_each_child_of_node(port, ep) {
		remote = of_graph_get_remote_port_parent(ep);
		if (!remote || !of_device_is_available(remote)) {
			of_node_put(remote);
			continue;
		} else if (!of_device_is_available(remote->parent)) {
228 229
			dev_warn(dev, "parent device of %pOF is not available\n",
				 remote);
230 231 232 233
			of_node_put(remote);
			continue;
		}

234
		drm_of_component_match_add(dev, match, compare_of, remote);
235 236 237 238
		of_node_put(remote);
	}
}

239 240 241 242
static const struct component_master_ops armada_master_ops = {
	.bind = armada_drm_bind,
	.unbind = armada_drm_unbind,
};
243

244 245 246 247 248
static int armada_drm_probe(struct platform_device *pdev)
{
	struct component_match *match = NULL;
	struct device *dev = &pdev->dev;
	int ret;
249

250 251 252
	ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
	if (ret != -EINVAL)
		return ret;
253

254
	if (dev->platform_data) {
255
		char **devices = dev->platform_data;
256
		struct device_node *port;
257
		struct device *d;
258
		int i;
259 260

		for (i = 0; devices[i]; i++)
261
			component_match_add(dev, &match, compare_dev_name,
262 263 264 265 266 267 268 269 270
					    devices[i]);

		if (i == 0) {
			dev_err(dev, "missing 'ports' property\n");
			return -ENODEV;
		}

		for (i = 0; devices[i]; i++) {
			d = bus_find_device_by_name(&platform_bus_type, NULL,
271
						    devices[i]);
272 273
			if (d && d->of_node) {
				for_each_child_of_node(d->of_node, port)
274
					armada_add_endpoints(dev, &match, port);
275 276 277 278 279
			}
			put_device(d);
		}
	}

280 281
	return component_master_add_with_match(&pdev->dev, &armada_master_ops,
					       match);
R
Russell King 已提交
282 283 284 285
}

static int armada_drm_remove(struct platform_device *pdev)
{
286
	component_master_del(&pdev->dev, &armada_master_ops);
R
Russell King 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	return 0;
}

static const struct platform_device_id armada_drm_platform_ids[] = {
	{
		.name		= "armada-drm",
	}, {
		.name		= "armada-510-drm",
	},
	{ },
};
MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);

static struct platform_driver armada_drm_platform_driver = {
	.probe	= armada_drm_probe,
	.remove	= armada_drm_remove,
	.driver	= {
		.name	= "armada-drm",
	},
	.id_table = armada_drm_platform_ids,
};

static int __init armada_drm_init(void)
{
311 312
	int ret;

313
	armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
314 315 316 317 318 319 320 321

	ret = platform_driver_register(&armada_lcd_platform_driver);
	if (ret)
		return ret;
	ret = platform_driver_register(&armada_drm_platform_driver);
	if (ret)
		platform_driver_unregister(&armada_lcd_platform_driver);
	return ret;
R
Russell King 已提交
322 323 324 325 326 327
}
module_init(armada_drm_init);

static void __exit armada_drm_exit(void)
{
	platform_driver_unregister(&armada_drm_platform_driver);
328
	platform_driver_unregister(&armada_lcd_platform_driver);
R
Russell King 已提交
329 330 331 332 333 334 335
}
module_exit(armada_drm_exit);

MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
MODULE_DESCRIPTION("Armada DRM Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:armada-drm");