blob: d5e3b45a81f9ada6da145956595ab5ed8e17a0bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * platform.c - platform 'pseudo' bus for legacy devices
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 * Please see Documentation/driver-model/platform.txt for more
10 * information.
11 */
12
Andrew Mortondaa41222009-08-06 16:00:44 -070013#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010014#include <linux/platform_device.h>
Grant Likely05212152010-06-08 07:48:20 -060015#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
19#include <linux/bootmem.h>
20#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020022#include <linux/pm_runtime.h>
Jean Delvare689ae232012-07-27 22:14:59 +020023#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010025#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020026#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010027
Jean Delvare689ae232012-07-27 22:14:59 +020028/* For automatically allocated device IDs */
29static DEFINE_IDA(platform_devid_ida);
30
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080031#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
32 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010035 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050037EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/**
Kumar Galaa77ce812011-06-10 01:52:57 -050040 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
Randy Dunlap7de636f2011-07-27 12:11:25 -070041 * @pdev: platform device
Kumar Galaa77ce812011-06-10 01:52:57 -050042 *
43 * This is called before platform_device_add() such that any pdev_archdata may
44 * be setup before the platform_notifier is called. So if a user needs to
45 * manipulate any relevant information in the pdev_archdata they can do:
46 *
Sebastian Andrzej Siewiorb1d6d822012-10-29 18:04:53 +010047 * platform_device_alloc()
Kumar Galaa77ce812011-06-10 01:52:57 -050048 * ... manipulate ...
49 * platform_device_add()
50 *
51 * And if they don't care they can just call platform_device_register() and
52 * everything will just work out.
53 */
54void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
55{
56}
57
58/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080059 * platform_get_resource - get a resource for a device
60 * @dev: platform device
61 * @type: resource type
62 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080064struct resource *platform_get_resource(struct platform_device *dev,
65 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 int i;
68
69 for (i = 0; i < dev->num_resources; i++) {
70 struct resource *r = &dev->resource[i];
71
Magnus Dammc9f66162008-10-15 22:05:15 -070072 if (type == resource_type(r) && num-- == 0)
73 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 return NULL;
76}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050077EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080080 * platform_get_irq - get an IRQ for a device
81 * @dev: platform device
82 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84int platform_get_irq(struct platform_device *dev, unsigned int num)
85{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +000086#ifdef CONFIG_SPARC
87 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
88 if (!dev || num >= dev->archdata.num_irqs)
89 return -ENXIO;
90 return dev->archdata.irqs[num];
91#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
93
David Vrabel305b3222006-01-19 17:52:27 +000094 return r ? r->start : -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +000095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050097EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800100 * platform_get_resource_byname - get a resource for a device by name
101 * @dev: platform device
102 * @type: resource type
103 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800105struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200106 unsigned int type,
107 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int i;
110
111 for (i = 0; i < dev->num_resources; i++) {
112 struct resource *r = &dev->resource[i];
113
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300114 if (unlikely(!r->name))
115 continue;
116
Magnus Dammc9f66162008-10-15 22:05:15 -0700117 if (type == resource_type(r) && !strcmp(r->name, name))
118 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 return NULL;
121}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500122EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/**
Wolfram Sangd6ff8552012-11-13 17:47:13 +0100125 * platform_get_irq_byname - get an IRQ for a device by name
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800126 * @dev: platform device
127 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200129int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800131 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
132 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David Vrabel305b3222006-01-19 17:52:27 +0000134 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500136EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800139 * platform_add_devices - add a numbers of platform devices
140 * @devs: array of platform devices to add
141 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143int platform_add_devices(struct platform_device **devs, int num)
144{
145 int i, ret = 0;
146
147 for (i = 0; i < num; i++) {
148 ret = platform_device_register(devs[i]);
149 if (ret) {
150 while (--i >= 0)
151 platform_device_unregister(devs[i]);
152 break;
153 }
154 }
155
156 return ret;
157}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500158EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Russell King37c12e72005-11-05 21:19:33 +0000160struct platform_object {
161 struct platform_device pdev;
162 char name[1];
163};
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000166 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800167 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000168 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800169 * Free all memory associated with a platform device. This function must
170 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000171 */
172void platform_device_put(struct platform_device *pdev)
173{
174 if (pdev)
175 put_device(&pdev->dev);
176}
177EXPORT_SYMBOL_GPL(platform_device_put);
178
179static void platform_device_release(struct device *dev)
180{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181 struct platform_object *pa = container_of(dev, struct platform_object,
182 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000183
Grant Likely7096d042010-10-20 11:45:13 -0600184 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000185 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200186 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000187 kfree(pa->pdev.resource);
188 kfree(pa);
189}
190
191/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000192 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800193 * @name: base name of the device we're adding
194 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000195 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800196 * Create a platform device object which can have other objects attached
197 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000198 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200199struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000200{
201 struct platform_object *pa;
202
203 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
204 if (pa) {
205 strcpy(pa->name, name);
206 pa->pdev.name = pa->name;
207 pa->pdev.id = id;
208 device_initialize(&pa->pdev.dev);
209 pa->pdev.dev.release = platform_device_release;
Kumar Galaa77ce812011-06-10 01:52:57 -0500210 arch_setup_pdev_archdata(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000211 }
212
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500213 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000214}
215EXPORT_SYMBOL_GPL(platform_device_alloc);
216
217/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000218 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800219 * @pdev: platform device allocated by platform_device_alloc to add resources to
220 * @res: set of resources that needs to be allocated for the device
221 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000222 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800223 * Add a copy of the resources to the platform device. The memory
224 * associated with the resources will be freed when the platform device is
225 * released.
Russell King37c12e72005-11-05 21:19:33 +0000226 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100228 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000229{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200230 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000231
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200232 if (res) {
233 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
234 if (!r)
235 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000236 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200237
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200238 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200239 pdev->resource = r;
240 pdev->num_resources = num;
241 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000242}
243EXPORT_SYMBOL_GPL(platform_device_add_resources);
244
245/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000246 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800247 * @pdev: platform device allocated by platform_device_alloc to add resources to
248 * @data: platform specific data for this platform device
249 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000250 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800251 * Add a copy of platform specific data to the platform device's
252 * platform_data pointer. The memory associated with the platform data
253 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000254 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800255int platform_device_add_data(struct platform_device *pdev, const void *data,
256 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000257{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200258 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000259
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200260 if (data) {
261 d = kmemdup(data, size, GFP_KERNEL);
262 if (!d)
263 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000264 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200265
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200266 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200267 pdev->dev.platform_data = d;
268 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000269}
270EXPORT_SYMBOL_GPL(platform_device_add_data);
271
272/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800273 * platform_device_add - add a platform device to device hierarchy
274 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800276 * This is part 2 of platform_device_register(), though may be called
277 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Russell King37c12e72005-11-05 21:19:33 +0000279int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jean Delvare689ae232012-07-27 22:14:59 +0200281 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (!pdev)
284 return -EINVAL;
285
286 if (!pdev->dev.parent)
287 pdev->dev.parent = &platform_bus;
288
289 pdev->dev.bus = &platform_bus_type;
290
Jean Delvare689ae232012-07-27 22:14:59 +0200291 switch (pdev->id) {
292 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100293 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200294 break;
295 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700296 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200297 break;
298 case PLATFORM_DEVID_AUTO:
299 /*
300 * Automatically allocated device ID. We mark it as such so
301 * that we remember it must be freed, and we append a suffix
302 * to avoid namespace collision with explicit IDs.
303 */
304 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
305 if (ret < 0)
306 goto err_out;
307 pdev->id = ret;
308 pdev->id_auto = true;
309 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
310 break;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 for (i = 0; i < pdev->num_resources; i++) {
314 struct resource *p, *r = &pdev->resource[i];
315
316 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100317 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 p = r->parent;
320 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700321 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700323 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 p = &ioport_resource;
325 }
326
Kumar Galad960bb42005-11-28 10:15:39 -0600327 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 printk(KERN_ERR
329 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100330 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ret = -EBUSY;
332 goto failed;
333 }
334 }
335
336 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100337 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Russell Kinge3915532006-05-06 08:15:26 +0100339 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ret == 0)
341 return ret;
342
343 failed:
Jean Delvare689ae232012-07-27 22:14:59 +0200344 if (pdev->id_auto) {
345 ida_simple_remove(&platform_devid_ida, pdev->id);
346 pdev->id = PLATFORM_DEVID_AUTO;
347 }
348
Magnus Dammc9f66162008-10-15 22:05:15 -0700349 while (--i >= 0) {
350 struct resource *r = &pdev->resource[i];
351 unsigned long type = resource_type(r);
352
353 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
354 release_resource(r);
355 }
356
Jean Delvare689ae232012-07-27 22:14:59 +0200357 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return ret;
359}
Russell King37c12e72005-11-05 21:19:33 +0000360EXPORT_SYMBOL_GPL(platform_device_add);
361
362/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800363 * platform_device_del - remove a platform-level device
364 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500365 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800366 * Note that this function will also release all memory- and port-based
367 * resources owned by the device (@dev->resource). This function must
368 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500369 */
370void platform_device_del(struct platform_device *pdev)
371{
372 int i;
373
374 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200375 device_del(&pdev->dev);
376
Jean Delvare689ae232012-07-27 22:14:59 +0200377 if (pdev->id_auto) {
378 ida_simple_remove(&platform_devid_ida, pdev->id);
379 pdev->id = PLATFORM_DEVID_AUTO;
380 }
381
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500382 for (i = 0; i < pdev->num_resources; i++) {
383 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700384 unsigned long type = resource_type(r);
385
386 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500387 release_resource(r);
388 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500389 }
390}
391EXPORT_SYMBOL_GPL(platform_device_del);
392
393/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800394 * platform_device_register - add a platform-level device
395 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000396 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800397int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000398{
399 device_initialize(&pdev->dev);
Kumar Galaa77ce812011-06-10 01:52:57 -0500400 arch_setup_pdev_archdata(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000401 return platform_device_add(pdev);
402}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500403EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800406 * platform_device_unregister - unregister a platform-level device
407 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800409 * Unregistration is done in 2 steps. First we release all resources
410 * and remove it from the subsystem, then we drop reference count by
411 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800413void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500415 platform_device_del(pdev);
416 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500418EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200421 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200422 * resources and platform-specific data
423 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200424 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700425 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200426 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700427 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200428struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100429 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700430{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200431 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700432 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700433
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200434 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200435 if (!pdev)
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200436 goto err_alloc;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700437
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200438 pdev->dev.parent = pdevinfo->parent;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700439
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200440 if (pdevinfo->dma_mask) {
441 /*
442 * This memory isn't freed when the device is put,
443 * I don't have a nice idea for that though. Conceptually
444 * dma_mask in struct device should not be a pointer.
445 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
446 */
447 pdev->dev.dma_mask =
448 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
449 if (!pdev->dev.dma_mask)
450 goto err;
451
452 *pdev->dev.dma_mask = pdevinfo->dma_mask;
453 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
454 }
455
456 ret = platform_device_add_resources(pdev,
457 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400458 if (ret)
459 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700460
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200461 ret = platform_device_add_data(pdev,
462 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400463 if (ret)
464 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200465
466 ret = platform_device_add(pdev);
467 if (ret) {
468err:
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200469 kfree(pdev->dev.dma_mask);
470
471err_alloc:
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200472 platform_device_put(pdev);
473 return ERR_PTR(ret);
474 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700475
476 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700477}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200478EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700479
Russell King00d3dcd2005-11-09 17:23:39 +0000480static int platform_drv_probe(struct device *_dev)
481{
482 struct platform_driver *drv = to_platform_driver(_dev->driver);
483 struct platform_device *dev = to_platform_device(_dev);
484
485 return drv->probe(dev);
486}
487
David Brownellc67334f2006-11-16 23:28:47 -0800488static int platform_drv_probe_fail(struct device *_dev)
489{
490 return -ENXIO;
491}
492
Russell King00d3dcd2005-11-09 17:23:39 +0000493static int platform_drv_remove(struct device *_dev)
494{
495 struct platform_driver *drv = to_platform_driver(_dev->driver);
496 struct platform_device *dev = to_platform_device(_dev);
497
498 return drv->remove(dev);
499}
500
501static void platform_drv_shutdown(struct device *_dev)
502{
503 struct platform_driver *drv = to_platform_driver(_dev->driver);
504 struct platform_device *dev = to_platform_device(_dev);
505
506 drv->shutdown(dev);
507}
508
Russell King00d3dcd2005-11-09 17:23:39 +0000509/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000510 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800511 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000512 */
513int platform_driver_register(struct platform_driver *drv)
514{
515 drv->driver.bus = &platform_bus_type;
516 if (drv->probe)
517 drv->driver.probe = platform_drv_probe;
518 if (drv->remove)
519 drv->driver.remove = platform_drv_remove;
520 if (drv->shutdown)
521 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200522
Russell King00d3dcd2005-11-09 17:23:39 +0000523 return driver_register(&drv->driver);
524}
525EXPORT_SYMBOL_GPL(platform_driver_register);
526
527/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000528 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800529 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000530 */
531void platform_driver_unregister(struct platform_driver *drv)
532{
533 driver_unregister(&drv->driver);
534}
535EXPORT_SYMBOL_GPL(platform_driver_unregister);
536
David Brownellc67334f2006-11-16 23:28:47 -0800537/**
538 * platform_driver_probe - register driver for non-hotpluggable device
539 * @drv: platform driver structure
540 * @probe: the driver probe routine, probably from an __init section
541 *
542 * Use this instead of platform_driver_register() when you know the device
543 * is not hotpluggable and has already been registered, and you want to
544 * remove its run-once probe() infrastructure from memory after the driver
545 * has bound to the device.
546 *
547 * One typical use for this would be with drivers for controllers integrated
548 * into system-on-chip processors, where the controller devices have been
549 * configured as part of board setup.
550 *
551 * Returns zero if the driver registered and bound to a device, else returns
552 * a negative error code and with the driver not registered.
553 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800554int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800555 int (*probe)(struct platform_device *))
556{
557 int retval, code;
558
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700559 /* make sure driver won't have bind/unbind attributes */
560 drv->driver.suppress_bind_attrs = true;
561
David Brownellc67334f2006-11-16 23:28:47 -0800562 /* temporary section violation during probe() */
563 drv->probe = probe;
564 retval = code = platform_driver_register(drv);
565
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700566 /*
567 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800568 * the list of drivers in order to probe new devices. Check to see
569 * if the probe was successful, and make sure any forced probes of
570 * new devices fail.
571 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700572 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800573 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800574 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800575 retval = -ENODEV;
576 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700577 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800578
579 if (code != retval)
580 platform_driver_unregister(drv);
581 return retval;
582}
583EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800585/**
586 * platform_create_bundle - register driver and create corresponding device
587 * @driver: platform driver structure
588 * @probe: the driver probe routine, probably from an __init section
589 * @res: set of resources that needs to be allocated for the device
590 * @n_res: number of resources
591 * @data: platform specific data for this platform device
592 * @size: size of platform specific data
593 *
594 * Use this in legacy-style modules that probe hardware directly and
595 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200596 *
597 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800598 */
599struct platform_device * __init_or_module platform_create_bundle(
600 struct platform_driver *driver,
601 int (*probe)(struct platform_device *),
602 struct resource *res, unsigned int n_res,
603 const void *data, size_t size)
604{
605 struct platform_device *pdev;
606 int error;
607
608 pdev = platform_device_alloc(driver->driver.name, -1);
609 if (!pdev) {
610 error = -ENOMEM;
611 goto err_out;
612 }
613
Anton Vorontsov807508c2010-09-07 17:31:54 +0400614 error = platform_device_add_resources(pdev, res, n_res);
615 if (error)
616 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800617
Anton Vorontsov807508c2010-09-07 17:31:54 +0400618 error = platform_device_add_data(pdev, data, size);
619 if (error)
620 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800621
622 error = platform_device_add(pdev);
623 if (error)
624 goto err_pdev_put;
625
626 error = platform_driver_probe(driver, probe);
627 if (error)
628 goto err_pdev_del;
629
630 return pdev;
631
632err_pdev_del:
633 platform_device_del(pdev);
634err_pdev_put:
635 platform_device_put(pdev);
636err_out:
637 return ERR_PTR(error);
638}
639EXPORT_SYMBOL_GPL(platform_create_bundle);
640
David Brownella0245f72006-05-29 10:37:33 -0700641/* modalias support enables more hands-off userspace setup:
642 * (a) environment variable lets new-style hotplug events work once system is
643 * fully running: "modprobe $MODALIAS"
644 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
645 * mishandled before system is fully running: "modprobe $(cat modalias)"
646 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800647static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
648 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700649{
650 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200651 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700652
653 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
654}
655
656static struct device_attribute platform_dev_attrs[] = {
657 __ATTR_RO(modalias),
658 __ATTR_NULL,
659};
660
Kay Sievers7eff2e72007-08-14 15:15:12 +0200661static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700662{
663 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600664 int rc;
665
666 /* Some devices have extra OF data and an OF-style MODALIAS */
Grant Likely07d57a32012-02-01 11:22:22 -0700667 rc = of_device_uevent_modalias(dev,env);
Grant Likelyeca39302010-06-08 07:48:21 -0600668 if (rc != -ENODEV)
669 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700670
Eric Miao57fee4a2009-02-04 11:52:40 +0800671 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Sebastian Andrzej Siewior0a268132011-08-15 16:51:22 +0200672 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700673 return 0;
674}
675
Eric Miao57fee4a2009-02-04 11:52:40 +0800676static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100677 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800678 struct platform_device *pdev)
679{
680 while (id->name[0]) {
681 if (strcmp(pdev->name, id->name) == 0) {
682 pdev->id_entry = id;
683 return id;
684 }
685 id++;
686 }
687 return NULL;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800691 * platform_match - bind platform device to platform driver.
692 * @dev: device.
693 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800695 * Platform device IDs are assumed to be encoded like this:
696 * "<name><instance>", where <name> is a short description of the type of
697 * device, like "pci" or "floppy", and <instance> is the enumerated
698 * instance of the device, like '0' or '42'. Driver IDs are simply
699 * "<name>". So, extract the <name> from the platform_device structure,
700 * and compare it against the name of the driver. Return whether they match
701 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800703static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800705 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800706 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Grant Likely05212152010-06-08 07:48:20 -0600708 /* Attempt an OF style match first */
709 if (of_driver_match_device(dev, drv))
710 return 1;
711
712 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800713 if (pdrv->id_table)
714 return platform_match_id(pdrv->id_table, pdev) != NULL;
715
716 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100717 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200720#ifdef CONFIG_PM_SLEEP
721
722static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200724 struct platform_driver *pdrv = to_platform_driver(dev->driver);
725 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int ret = 0;
727
Magnus Damm783ea7d2009-06-04 22:13:33 +0200728 if (dev->driver && pdrv->suspend)
729 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700730
731 return ret;
732}
733
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200734static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200736 struct platform_driver *pdrv = to_platform_driver(dev->driver);
737 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int ret = 0;
739
Magnus Damm783ea7d2009-06-04 22:13:33 +0200740 if (dev->driver && pdrv->resume)
741 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200746#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +0200747
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200748#ifdef CONFIG_SUSPEND
749
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200750int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200751{
752 struct device_driver *drv = dev->driver;
753 int ret = 0;
754
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200755 if (!drv)
756 return 0;
757
758 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200759 if (drv->pm->suspend)
760 ret = drv->pm->suspend(dev);
761 } else {
762 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
763 }
764
765 return ret;
766}
767
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200768int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200769{
770 struct device_driver *drv = dev->driver;
771 int ret = 0;
772
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200773 if (!drv)
774 return 0;
775
776 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200777 if (drv->pm->resume)
778 ret = drv->pm->resume(dev);
779 } else {
780 ret = platform_legacy_resume(dev);
781 }
782
783 return ret;
784}
785
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200786#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200787
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200788#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200789
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200790int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200791{
792 struct device_driver *drv = dev->driver;
793 int ret = 0;
794
795 if (!drv)
796 return 0;
797
798 if (drv->pm) {
799 if (drv->pm->freeze)
800 ret = drv->pm->freeze(dev);
801 } else {
802 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
803 }
804
805 return ret;
806}
807
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200808int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200809{
810 struct device_driver *drv = dev->driver;
811 int ret = 0;
812
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200813 if (!drv)
814 return 0;
815
816 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200817 if (drv->pm->thaw)
818 ret = drv->pm->thaw(dev);
819 } else {
820 ret = platform_legacy_resume(dev);
821 }
822
823 return ret;
824}
825
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200826int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200827{
828 struct device_driver *drv = dev->driver;
829 int ret = 0;
830
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200831 if (!drv)
832 return 0;
833
834 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200835 if (drv->pm->poweroff)
836 ret = drv->pm->poweroff(dev);
837 } else {
838 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
839 }
840
841 return ret;
842}
843
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200844int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200845{
846 struct device_driver *drv = dev->driver;
847 int ret = 0;
848
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200849 if (!drv)
850 return 0;
851
852 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200853 if (drv->pm->restore)
854 ret = drv->pm->restore(dev);
855 } else {
856 ret = platform_legacy_resume(dev);
857 }
858
859 return ret;
860}
861
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200862#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200863
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200864static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +0200865 .runtime_suspend = pm_generic_runtime_suspend,
866 .runtime_resume = pm_generic_runtime_resume,
867 .runtime_idle = pm_generic_runtime_idle,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200868 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200869};
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871struct bus_type platform_bus_type = {
872 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700873 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700875 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200876 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500878EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880int __init platform_bus_init(void)
881{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100882 int error;
883
Magnus Damm13977092009-03-30 14:37:25 -0700884 early_platform_cleanup();
885
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100886 error = device_register(&platform_bus);
887 if (error)
888 return error;
889 error = bus_register(&platform_bus_type);
890 if (error)
891 device_unregister(&platform_bus);
892 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
896u64 dma_get_required_mask(struct device *dev)
897{
898 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
899 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
900 u64 mask;
901
902 if (!high_totalram) {
903 /* convert to mask just covering totalram */
904 low_totalram = (1 << (fls(low_totalram) - 1));
905 low_totalram += low_totalram - 1;
906 mask = low_totalram;
907 } else {
908 high_totalram = (1 << (fls(high_totalram) - 1));
909 high_totalram += high_totalram - 1;
910 mask = (((u64)high_totalram) << 32) + 0xffffffff;
911 }
James Bottomleye88a0c22008-03-09 11:57:56 -0500912 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914EXPORT_SYMBOL_GPL(dma_get_required_mask);
915#endif
Magnus Damm13977092009-03-30 14:37:25 -0700916
917static __initdata LIST_HEAD(early_platform_driver_list);
918static __initdata LIST_HEAD(early_platform_device_list);
919
920/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900921 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -0700922 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -0700923 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +0900924 *
925 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -0700926 */
927int __init early_platform_driver_register(struct early_platform_driver *epdrv,
928 char *buf)
929{
Magnus Dammc60e0502009-11-27 17:38:51 +0900930 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -0700931 int n;
932
933 /* Simply add the driver to the end of the global list.
934 * Drivers will by default be put on the list in compiled-in order.
935 */
936 if (!epdrv->list.next) {
937 INIT_LIST_HEAD(&epdrv->list);
938 list_add_tail(&epdrv->list, &early_platform_driver_list);
939 }
940
941 /* If the user has specified device then make sure the driver
942 * gets prioritized. The driver of the last device specified on
943 * command line will be put first on the list.
944 */
945 n = strlen(epdrv->pdrv->driver.name);
946 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
947 list_move(&epdrv->list, &early_platform_driver_list);
948
Magnus Dammc60e0502009-11-27 17:38:51 +0900949 /* Allow passing parameters after device name */
950 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -0700951 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +0900952 else {
953 epdrv->requested_id = simple_strtoul(&buf[n + 1],
954 &tmp, 10);
955
956 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
957 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
958 n = 0;
959 } else
960 n += strcspn(&buf[n + 1], ",") + 1;
961 }
962
963 if (buf[n] == ',')
964 n++;
965
966 if (epdrv->bufsize) {
967 memcpy(epdrv->buffer, &buf[n],
968 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
969 epdrv->buffer[epdrv->bufsize - 1] = '\0';
970 }
Magnus Damm13977092009-03-30 14:37:25 -0700971 }
972
973 return 0;
974}
975
976/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900977 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -0700978 * @devs: array of early platform devices to add
979 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +0900980 *
981 * Used by early architecture code to register early platform devices and
982 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -0700983 */
984void __init early_platform_add_devices(struct platform_device **devs, int num)
985{
986 struct device *dev;
987 int i;
988
989 /* simply add the devices to list */
990 for (i = 0; i < num; i++) {
991 dev = &devs[i]->dev;
992
993 if (!dev->devres_head.next) {
Rafael J. Wysockibed2b422012-08-06 01:45:11 +0200994 pm_runtime_early_init(dev);
Magnus Damm13977092009-03-30 14:37:25 -0700995 INIT_LIST_HEAD(&dev->devres_head);
996 list_add_tail(&dev->devres_head,
997 &early_platform_device_list);
998 }
999 }
1000}
1001
1002/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001003 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001004 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001005 *
1006 * Used by architecture code to register all early platform drivers
1007 * for a certain class. If omitted then only early platform drivers
1008 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001009 */
1010void __init early_platform_driver_register_all(char *class_str)
1011{
1012 /* The "class_str" parameter may or may not be present on the kernel
1013 * command line. If it is present then there may be more than one
1014 * matching parameter.
1015 *
1016 * Since we register our early platform drivers using early_param()
1017 * we need to make sure that they also get registered in the case
1018 * when the parameter is missing from the kernel command line.
1019 *
1020 * We use parse_early_options() to make sure the early_param() gets
1021 * called at least once. The early_param() may be called more than
1022 * once since the name of the preferred device may be specified on
1023 * the kernel command line. early_platform_driver_register() handles
1024 * this case for us.
1025 */
1026 parse_early_options(class_str);
1027}
1028
1029/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001030 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001031 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001032 * @id: id to match against
1033 */
1034static __init struct platform_device *
1035early_platform_match(struct early_platform_driver *epdrv, int id)
1036{
1037 struct platform_device *pd;
1038
1039 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1040 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1041 if (pd->id == id)
1042 return pd;
1043
1044 return NULL;
1045}
1046
1047/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001048 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001049 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001050 * @id: return true if id or above exists
1051 */
1052static __init int early_platform_left(struct early_platform_driver *epdrv,
1053 int id)
1054{
1055 struct platform_device *pd;
1056
1057 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1058 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1059 if (pd->id >= id)
1060 return 1;
1061
1062 return 0;
1063}
1064
1065/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001066 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001067 * @class_str: string to identify early platform driver class
1068 * @id: id to match against
1069 * @nr_probe: number of platform devices to successfully probe before exiting
1070 */
1071static int __init early_platform_driver_probe_id(char *class_str,
1072 int id,
1073 int nr_probe)
1074{
1075 struct early_platform_driver *epdrv;
1076 struct platform_device *match;
1077 int match_id;
1078 int n = 0;
1079 int left = 0;
1080
1081 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1082 /* only use drivers matching our class_str */
1083 if (strcmp(class_str, epdrv->class_str))
1084 continue;
1085
1086 if (id == -2) {
1087 match_id = epdrv->requested_id;
1088 left = 1;
1089
1090 } else {
1091 match_id = id;
1092 left += early_platform_left(epdrv, id);
1093
1094 /* skip requested id */
1095 switch (epdrv->requested_id) {
1096 case EARLY_PLATFORM_ID_ERROR:
1097 case EARLY_PLATFORM_ID_UNSET:
1098 break;
1099 default:
1100 if (epdrv->requested_id == id)
1101 match_id = EARLY_PLATFORM_ID_UNSET;
1102 }
1103 }
1104
1105 switch (match_id) {
1106 case EARLY_PLATFORM_ID_ERROR:
1107 pr_warning("%s: unable to parse %s parameter\n",
1108 class_str, epdrv->pdrv->driver.name);
1109 /* fall-through */
1110 case EARLY_PLATFORM_ID_UNSET:
1111 match = NULL;
1112 break;
1113 default:
1114 match = early_platform_match(epdrv, match_id);
1115 }
1116
1117 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001118 /*
1119 * Set up a sensible init_name to enable
1120 * dev_name() and others to be used before the
1121 * rest of the driver core is initialized.
1122 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001123 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001124 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001125 match->dev.init_name =
1126 kasprintf(GFP_KERNEL, "%s.%d",
1127 match->name,
1128 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001129 else
Paul Mundtbd050862010-03-29 15:51:35 +09001130 match->dev.init_name =
1131 kasprintf(GFP_KERNEL, "%s",
1132 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001133
Paul Mundta636ee72010-03-09 06:57:53 +00001134 if (!match->dev.init_name)
1135 return -ENOMEM;
1136 }
Paul Mundtbd050862010-03-29 15:51:35 +09001137
Magnus Damm13977092009-03-30 14:37:25 -07001138 if (epdrv->pdrv->probe(match))
1139 pr_warning("%s: unable to probe %s early.\n",
1140 class_str, match->name);
1141 else
1142 n++;
1143 }
1144
1145 if (n >= nr_probe)
1146 break;
1147 }
1148
1149 if (left)
1150 return n;
1151 else
1152 return -ENODEV;
1153}
1154
1155/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001156 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001157 * @class_str: string to identify early platform driver class
1158 * @nr_probe: number of platform devices to successfully probe before exiting
1159 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001160 *
1161 * Used by architecture code to probe registered early platform drivers
1162 * within a certain class. For probe to happen a registered early platform
1163 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001164 */
1165int __init early_platform_driver_probe(char *class_str,
1166 int nr_probe,
1167 int user_only)
1168{
1169 int k, n, i;
1170
1171 n = 0;
1172 for (i = -2; n < nr_probe; i++) {
1173 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1174
1175 if (k < 0)
1176 break;
1177
1178 n += k;
1179
1180 if (user_only)
1181 break;
1182 }
1183
1184 return n;
1185}
1186
1187/**
1188 * early_platform_cleanup - clean up early platform code
1189 */
1190void __init early_platform_cleanup(void)
1191{
1192 struct platform_device *pd, *pd2;
1193
1194 /* clean up the devres list used to chain devices */
1195 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1196 dev.devres_head) {
1197 list_del(&pd->dev.devres_head);
1198 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1199 }
1200}
1201