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__FBSDID("$FreeBSD: stable/11/sys/dev/drm2/drm_platform.c 317011 2017-04-16 08:07:38Z mmel $");
29
30#include <dev/drm2/drmP.h>
31
32static void drm_platform_free_irq(struct drm_device *dev)
33{
34	if (dev->irqr == NULL)
35		return;
36
37	bus_release_resource(dev->dev, SYS_RES_IRQ,
38	    dev->irqrid, dev->irqr);
39
40	dev->irqr = NULL;
41	dev->irq = 0;
42}
43
44static const char *drm_platform_get_name(struct drm_device *dev)
45{
46	return dev->driver->name;
47}
48
49static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
50{
51	int len, ret, id;
52
53	master->unique_len = 13 + strlen(dev->driver->name);
54	master->unique_size = master->unique_len;
55	master->unique = malloc(master->unique_len + 1, DRM_MEM_DRIVER, M_NOWAIT);
56
57	if (master->unique == NULL)
58		return -ENOMEM;
59
60	id = 0; // XXX dev->driver->id;
61
62	/* if only a single instance of the platform device, id will be
63	 * set to -1.. use 0 instead to avoid a funny looking bus-id:
64	 */
65	if (id == -1)
66		id = 0;
67
68	len = snprintf(master->unique, master->unique_len,
69			"platform:%s:%02d", dev->driver->name, id);
70
71	if (len > master->unique_len) {
72		DRM_ERROR("Unique buffer overflowed\n");
73		ret = -EINVAL;
74		goto err;
75	}
76
77	return 0;
78err:
79	return ret;
80}
81
82static int drm_platform_get_irq(struct drm_device *dev)
83{
84	if (dev->irqr)
85		return (dev->irq);
86
87	dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
88	    &dev->irqrid, RF_SHAREABLE);
89	if (!dev->irqr) {
90		dev_err(dev->dev, "Failed to allocate IRQ\n");
91		return (0);
92	}
93
94	dev->irq = (int) rman_get_start(dev->irqr);
95
96	return (dev->irq);
97}
98
99static struct drm_bus drm_platform_bus = {
100	.bus_type = DRIVER_BUS_PLATFORM,
101	.get_irq = drm_platform_get_irq,
102	.free_irq = drm_platform_free_irq,
103	.get_name = drm_platform_get_name,
104	.set_busid = drm_platform_set_busid,
105};
106
107/**
108 * Register.
109 *
110 * \param platdev - Platform device struture
111 * \return zero on success or a negative number on failure.
112 *
113 * Attempt to gets inter module "drm" information. If we are first
114 * then register the character device and inter module information.
115 * Try and register, if we fail to register, backout previous work.
116 */
117
118int drm_get_platform_dev(device_t kdev, struct drm_device *dev,
119			 struct drm_driver *driver)
120{
121	int ret;
122
123	DRM_DEBUG("\n");
124
125	driver->bus = &drm_platform_bus;
126
127	dev->dev = kdev;
128
129	sx_xlock(&drm_global_mutex);
130
131	ret = drm_fill_in_dev(dev, driver);
132
133	if (ret) {
134		printf("DRM: Fill_in_dev failed.\n");
135		goto err_g1;
136	}
137
138	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
139		ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
140		if (ret)
141			goto err_g1;
142	}
143
144	ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
145	if (ret)
146		goto err_g2;
147
148	if (dev->driver->load) {
149		ret = dev->driver->load(dev, 0);
150		if (ret)
151			goto err_g3;
152	}
153
154	/* setup the grouping for the legacy output */
155	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
156		ret = drm_mode_group_init_legacy_group(dev,
157				&dev->primary->mode_group);
158		if (ret)
159			goto err_g3;
160	}
161
162#ifdef FREEBSD_NOTYET
163	list_add_tail(&dev->driver_item, &driver->device_list);
164#endif /* FREEBSD_NOTYET */
165
166	sx_xunlock(&drm_global_mutex);
167
168	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
169		 driver->name, driver->major, driver->minor, driver->patchlevel,
170		 driver->date, dev->primary->index);
171
172	return 0;
173
174err_g3:
175	drm_put_minor(&dev->primary);
176err_g2:
177	if (drm_core_check_feature(dev, DRIVER_MODESET))
178		drm_put_minor(&dev->control);
179err_g1:
180	sx_xunlock(&drm_global_mutex);
181	return ret;
182}
183EXPORT_SYMBOL(drm_get_platform_dev);
184