xref: /freebsd/sys/dev/drm2/drm_platform.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * Derived from drm_pci.c
3  *
4  * Copyright 2003 José Fonseca.
5  * Copyright 2003 Leif Delgass.
6  * Copyright (c) 2009, Code Aurora Forum.
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
23  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #include <sys/cdefs.h>
28 #include <dev/drm2/drmP.h>
29 
30 static void drm_platform_free_irq(struct drm_device *dev)
31 {
32 	if (dev->irqr == NULL)
33 		return;
34 
35 	bus_release_resource(dev->dev, SYS_RES_IRQ,
36 	    dev->irqrid, dev->irqr);
37 
38 	dev->irqr = NULL;
39 	dev->irq = 0;
40 }
41 
42 static const char *drm_platform_get_name(struct drm_device *dev)
43 {
44 	return dev->driver->name;
45 }
46 
47 static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
48 {
49 	int len, ret, id;
50 
51 	master->unique_len = 13 + strlen(dev->driver->name);
52 	master->unique_size = master->unique_len;
53 	master->unique = malloc(master->unique_len + 1, DRM_MEM_DRIVER, M_NOWAIT);
54 
55 	if (master->unique == NULL)
56 		return -ENOMEM;
57 
58 	id = 0; // XXX dev->driver->id;
59 
60 	/* if only a single instance of the platform device, id will be
61 	 * set to -1.. use 0 instead to avoid a funny looking bus-id:
62 	 */
63 	if (id == -1)
64 		id = 0;
65 
66 	len = snprintf(master->unique, master->unique_len,
67 			"platform:%s:%02d", dev->driver->name, id);
68 
69 	if (len > master->unique_len) {
70 		DRM_ERROR("Unique buffer overflowed\n");
71 		ret = -EINVAL;
72 		goto err;
73 	}
74 
75 	return 0;
76 err:
77 	return ret;
78 }
79 
80 static int drm_platform_get_irq(struct drm_device *dev)
81 {
82 	if (dev->irqr)
83 		return (dev->irq);
84 
85 	dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
86 	    &dev->irqrid, RF_SHAREABLE);
87 	if (!dev->irqr) {
88 		dev_err(dev->dev, "Failed to allocate IRQ\n");
89 		return (0);
90 	}
91 
92 	dev->irq = (int) rman_get_start(dev->irqr);
93 
94 	return (dev->irq);
95 }
96 
97 static struct drm_bus drm_platform_bus = {
98 	.bus_type = DRIVER_BUS_PLATFORM,
99 	.get_irq = drm_platform_get_irq,
100 	.free_irq = drm_platform_free_irq,
101 	.get_name = drm_platform_get_name,
102 	.set_busid = drm_platform_set_busid,
103 };
104 
105 /**
106  * Register.
107  *
108  * \param platdev - Platform device structure
109  * \return zero on success or a negative number on failure.
110  *
111  * Attempt to gets inter module "drm" information. If we are first
112  * then register the character device and inter module information.
113  * Try and register, if we fail to register, backout previous work.
114  */
115 
116 int drm_get_platform_dev(device_t kdev, struct drm_device *dev,
117 			 struct drm_driver *driver)
118 {
119 	int ret;
120 
121 	DRM_DEBUG("\n");
122 
123 	driver->bus = &drm_platform_bus;
124 
125 	dev->dev = kdev;
126 
127 	sx_xlock(&drm_global_mutex);
128 
129 	ret = drm_fill_in_dev(dev, driver);
130 
131 	if (ret) {
132 		printf("DRM: Fill_in_dev failed.\n");
133 		goto err_g1;
134 	}
135 
136 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
137 		ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
138 		if (ret)
139 			goto err_g1;
140 	}
141 
142 	ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
143 	if (ret)
144 		goto err_g2;
145 
146 	if (dev->driver->load) {
147 		ret = dev->driver->load(dev, 0);
148 		if (ret)
149 			goto err_g3;
150 	}
151 
152 	/* setup the grouping for the legacy output */
153 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
154 		ret = drm_mode_group_init_legacy_group(dev,
155 				&dev->primary->mode_group);
156 		if (ret)
157 			goto err_g3;
158 	}
159 
160 #ifdef FREEBSD_NOTYET
161 	list_add_tail(&dev->driver_item, &driver->device_list);
162 #endif /* FREEBSD_NOTYET */
163 
164 	sx_xunlock(&drm_global_mutex);
165 
166 	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
167 		 driver->name, driver->major, driver->minor, driver->patchlevel,
168 		 driver->date, dev->primary->index);
169 
170 	return 0;
171 
172 err_g3:
173 	drm_put_minor(&dev->primary);
174 err_g2:
175 	if (drm_core_check_feature(dev, DRIVER_MODESET))
176 		drm_put_minor(&dev->control);
177 err_g1:
178 	sx_xunlock(&drm_global_mutex);
179 	return ret;
180 }
181 EXPORT_SYMBOL(drm_get_platform_dev);
182