blob: fb2721738a8abddc522f374fb670734681693258 [file] [log] [blame]
Jordan Crousedcdb1672010-05-27 13:40:25 -06001/*
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
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040028#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
Jordan Crousedcdb1672010-05-27 13:40:25 -060030
Lespiau, Damien66cc8b62013-08-20 00:53:10 +010031/*
Jordan Crousedcdb1672010-05-27 13:40:25 -060032 * Register.
33 *
34 * \param platdev - Platform device struture
35 * \return zero on success or a negative number on failure.
36 *
37 * Attempt to gets inter module "drm" information. If we are first
38 * then register the character device and inter module information.
39 * Try and register, if we fail to register, backout previous work.
40 */
41
Lespiau, Damien66cc8b62013-08-20 00:53:10 +010042static int drm_get_platform_dev(struct platform_device *platdev,
43 struct drm_driver *driver)
Jordan Crousedcdb1672010-05-27 13:40:25 -060044{
45 struct drm_device *dev;
46 int ret;
47
48 DRM_DEBUG("\n");
49
David Herrmann1bb72532013-10-02 11:23:34 +020050 dev = drm_dev_alloc(driver, &platdev->dev);
Jordan Crousedcdb1672010-05-27 13:40:25 -060051 if (!dev)
52 return -ENOMEM;
53
54 dev->platformdev = platdev;
Jordan Crousedcdb1672010-05-27 13:40:25 -060055
Dave Airlieb64c1152010-09-14 20:14:38 +100056 mutex_lock(&drm_global_mutex);
57
Jordan Crousedcdb1672010-05-27 13:40:25 -060058 ret = drm_fill_in_dev(dev, NULL, driver);
59
60 if (ret) {
61 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
62 goto err_g1;
63 }
64
65 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jordan Crousedcdb1672010-05-27 13:40:25 -060066 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
67 if (ret)
68 goto err_g1;
69 }
70
David Herrmann17931262013-08-25 18:29:00 +020071 if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
72 ret = drm_get_minor(dev, &dev->render, DRM_MINOR_RENDER);
73 if (ret)
74 goto err_g11;
75 }
76
Jordan Crousedcdb1672010-05-27 13:40:25 -060077 ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
78 if (ret)
79 goto err_g2;
80
81 if (dev->driver->load) {
82 ret = dev->driver->load(dev, 0);
83 if (ret)
84 goto err_g3;
85 }
86
87 /* setup the grouping for the legacy output */
88 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
89 ret = drm_mode_group_init_legacy_group(dev,
90 &dev->primary->mode_group);
91 if (ret)
92 goto err_g3;
93 }
94
95 list_add_tail(&dev->driver_item, &driver->device_list);
96
Dave Airlieb64c1152010-09-14 20:14:38 +100097 mutex_unlock(&drm_global_mutex);
98
Jordan Crousedcdb1672010-05-27 13:40:25 -060099 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
100 driver->name, driver->major, driver->minor, driver->patchlevel,
101 driver->date, dev->primary->index);
102
103 return 0;
104
105err_g3:
106 drm_put_minor(&dev->primary);
107err_g2:
David Herrmann17931262013-08-25 18:29:00 +0200108 if (dev->render)
109 drm_put_minor(&dev->render);
110err_g11:
Jordan Crousedcdb1672010-05-27 13:40:25 -0600111 if (drm_core_check_feature(dev, DRIVER_MODESET))
112 drm_put_minor(&dev->control);
113err_g1:
114 kfree(dev);
Dave Airlieb64c1152010-09-14 20:14:38 +1000115 mutex_unlock(&drm_global_mutex);
Jordan Crousedcdb1672010-05-27 13:40:25 -0600116 return ret;
117}
Jordan Crousedcdb1672010-05-27 13:40:25 -0600118
Dave Airlie8410ea32010-12-15 03:16:38 +1000119static int drm_platform_get_irq(struct drm_device *dev)
120{
121 return platform_get_irq(dev->platformdev, 0);
122}
123
124static const char *drm_platform_get_name(struct drm_device *dev)
125{
126 return dev->platformdev->name;
127}
128
129static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
130{
Rob Clarkb19c19a2012-03-06 10:20:36 -0600131 int len, ret, id;
Dave Airlie8410ea32010-12-15 03:16:38 +1000132
Rob Clark28a4a162011-07-14 03:12:43 +0000133 master->unique_len = 13 + strlen(dev->platformdev->name);
134 master->unique_size = master->unique_len;
Dave Airlie8410ea32010-12-15 03:16:38 +1000135 master->unique = kmalloc(master->unique_len + 1, GFP_KERNEL);
136
137 if (master->unique == NULL)
138 return -ENOMEM;
139
Rob Clarkb19c19a2012-03-06 10:20:36 -0600140 id = dev->platformdev->id;
141
142 /* if only a single instance of the platform device, id will be
143 * set to -1.. use 0 instead to avoid a funny looking bus-id:
144 */
145 if (id == -1)
146 id = 0;
147
Dave Airlie8410ea32010-12-15 03:16:38 +1000148 len = snprintf(master->unique, master->unique_len,
Rob Clarkb19c19a2012-03-06 10:20:36 -0600149 "platform:%s:%02d", dev->platformdev->name, id);
Dave Airlie8410ea32010-12-15 03:16:38 +1000150
151 if (len > master->unique_len) {
152 DRM_ERROR("Unique buffer overflowed\n");
153 ret = -EINVAL;
154 goto err;
155 }
156
157 dev->devname =
158 kmalloc(strlen(dev->platformdev->name) +
159 master->unique_len + 2, GFP_KERNEL);
160
161 if (dev->devname == NULL) {
162 ret = -ENOMEM;
163 goto err;
164 }
165
166 sprintf(dev->devname, "%s@%s", dev->platformdev->name,
167 master->unique);
168 return 0;
169err:
170 return ret;
171}
172
173static struct drm_bus drm_platform_bus = {
174 .bus_type = DRIVER_BUS_PLATFORM,
175 .get_irq = drm_platform_get_irq,
176 .get_name = drm_platform_get_name,
177 .set_busid = drm_platform_set_busid,
178};
179
Jordan Crousedcdb1672010-05-27 13:40:25 -0600180/**
Dave Airlie8410ea32010-12-15 03:16:38 +1000181 * Platform device initialization. Called direct from modules.
Jordan Crousedcdb1672010-05-27 13:40:25 -0600182 *
183 * \return zero on success or a negative number on failure.
184 *
185 * Initializes a drm_device structures,registering the
186 * stubs
187 *
188 * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
189 * after the initialization for driver customization.
190 */
191
Dave Airlie8410ea32010-12-15 03:16:38 +1000192int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device)
Jordan Crousedcdb1672010-05-27 13:40:25 -0600193{
Dave Airlie8410ea32010-12-15 03:16:38 +1000194 DRM_DEBUG("\n");
195
196 driver->kdriver.platform_device = platform_device;
197 driver->bus = &drm_platform_bus;
198 INIT_LIST_HEAD(&driver->device_list);
199 return drm_get_platform_dev(platform_device, driver);
Jordan Crousedcdb1672010-05-27 13:40:25 -0600200}
Dave Airlie8410ea32010-12-15 03:16:38 +1000201EXPORT_SYMBOL(drm_platform_init);
202
203void drm_platform_exit(struct drm_driver *driver, struct platform_device *platform_device)
204{
205 struct drm_device *dev, *tmp;
206 DRM_DEBUG("\n");
207
208 list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
209 drm_put_dev(dev);
210 DRM_INFO("Module unloaded\n");
211}
212EXPORT_SYMBOL(drm_platform_exit);