blob: ae5c4aa6d269c3b44dd325e7bf0882aa7615770d [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
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/dma-mapping.h>
17#include <linux/bootmem.h>
18#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010021#include "base.h"
22
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080023#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
24 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010027 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050029EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080032 * platform_get_resource - get a resource for a device
33 * @dev: platform device
34 * @type: resource type
35 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080037struct resource *platform_get_resource(struct platform_device *dev,
38 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 int i;
41
42 for (i = 0; i < dev->num_resources; i++) {
43 struct resource *r = &dev->resource[i];
44
Magnus Dammc9f66162008-10-15 22:05:15 -070045 if (type == resource_type(r) && num-- == 0)
46 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 return NULL;
49}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050050EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080053 * platform_get_irq - get an IRQ for a device
54 * @dev: platform device
55 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57int platform_get_irq(struct platform_device *dev, unsigned int num)
58{
59 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
60
David Vrabel305b3222006-01-19 17:52:27 +000061 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050063EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080066 * platform_get_resource_byname - get a resource for a device by name
67 * @dev: platform device
68 * @type: resource type
69 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080071struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +020072 unsigned int type,
73 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 int i;
76
77 for (i = 0; i < dev->num_resources; i++) {
78 struct resource *r = &dev->resource[i];
79
Magnus Dammc9f66162008-10-15 22:05:15 -070080 if (type == resource_type(r) && !strcmp(r->name, name))
81 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 return NULL;
84}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050085EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080088 * platform_get_irq - get an IRQ for a device
89 * @dev: platform device
90 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +020092int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080094 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
95 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David Vrabel305b3222006-01-19 17:52:27 +000097 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050099EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800102 * platform_add_devices - add a numbers of platform devices
103 * @devs: array of platform devices to add
104 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
106int platform_add_devices(struct platform_device **devs, int num)
107{
108 int i, ret = 0;
109
110 for (i = 0; i < num; i++) {
111 ret = platform_device_register(devs[i]);
112 if (ret) {
113 while (--i >= 0)
114 platform_device_unregister(devs[i]);
115 break;
116 }
117 }
118
119 return ret;
120}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500121EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Russell King37c12e72005-11-05 21:19:33 +0000123struct platform_object {
124 struct platform_device pdev;
125 char name[1];
126};
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800129 * platform_device_put
130 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000131 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800132 * Free all memory associated with a platform device. This function must
133 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000134 */
135void platform_device_put(struct platform_device *pdev)
136{
137 if (pdev)
138 put_device(&pdev->dev);
139}
140EXPORT_SYMBOL_GPL(platform_device_put);
141
142static void platform_device_release(struct device *dev)
143{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800144 struct platform_object *pa = container_of(dev, struct platform_object,
145 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000146
147 kfree(pa->pdev.dev.platform_data);
148 kfree(pa->pdev.resource);
149 kfree(pa);
150}
151
152/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800153 * platform_device_alloc
154 * @name: base name of the device we're adding
155 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000156 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800157 * Create a platform device object which can have other objects attached
158 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000159 */
Jean Delvare13595552007-09-09 12:54:16 +0200160struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000161{
162 struct platform_object *pa;
163
164 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
165 if (pa) {
166 strcpy(pa->name, name);
167 pa->pdev.name = pa->name;
168 pa->pdev.id = id;
169 device_initialize(&pa->pdev.dev);
170 pa->pdev.dev.release = platform_device_release;
171 }
172
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500173 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000174}
175EXPORT_SYMBOL_GPL(platform_device_alloc);
176
177/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800178 * platform_device_add_resources
179 * @pdev: platform device allocated by platform_device_alloc to add resources to
180 * @res: set of resources that needs to be allocated for the device
181 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000182 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800183 * Add a copy of the resources to the platform device. The memory
184 * associated with the resources will be freed when the platform device is
185 * released.
Russell King37c12e72005-11-05 21:19:33 +0000186 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800187int platform_device_add_resources(struct platform_device *pdev,
188 struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000189{
190 struct resource *r;
191
192 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
193 if (r) {
194 memcpy(r, res, sizeof(struct resource) * num);
195 pdev->resource = r;
196 pdev->num_resources = num;
197 }
198 return r ? 0 : -ENOMEM;
199}
200EXPORT_SYMBOL_GPL(platform_device_add_resources);
201
202/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800203 * platform_device_add_data
204 * @pdev: platform device allocated by platform_device_alloc to add resources to
205 * @data: platform specific data for this platform device
206 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000207 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800208 * Add a copy of platform specific data to the platform device's
209 * platform_data pointer. The memory associated with the platform data
210 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000211 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800212int platform_device_add_data(struct platform_device *pdev, const void *data,
213 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000214{
215 void *d;
216
217 d = kmalloc(size, GFP_KERNEL);
218 if (d) {
219 memcpy(d, data, size);
220 pdev->dev.platform_data = d;
221 }
222 return d ? 0 : -ENOMEM;
223}
224EXPORT_SYMBOL_GPL(platform_device_add_data);
225
226/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800227 * platform_device_add - add a platform device to device hierarchy
228 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800230 * This is part 2 of platform_device_register(), though may be called
231 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Russell King37c12e72005-11-05 21:19:33 +0000233int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i, ret = 0;
236
237 if (!pdev)
238 return -EINVAL;
239
240 if (!pdev->dev.parent)
241 pdev->dev.parent = &platform_bus;
242
243 pdev->dev.bus = &platform_bus_type;
244
245 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100246 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700248 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 for (i = 0; i < pdev->num_resources; i++) {
251 struct resource *p, *r = &pdev->resource[i];
252
253 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100254 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 p = r->parent;
257 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700258 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700260 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 p = &ioport_resource;
262 }
263
Kumar Galad960bb42005-11-28 10:15:39 -0600264 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk(KERN_ERR
266 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100267 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ret = -EBUSY;
269 goto failed;
270 }
271 }
272
273 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100274 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Russell Kinge3915532006-05-06 08:15:26 +0100276 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ret == 0)
278 return ret;
279
280 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700281 while (--i >= 0) {
282 struct resource *r = &pdev->resource[i];
283 unsigned long type = resource_type(r);
284
285 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
286 release_resource(r);
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return ret;
290}
Russell King37c12e72005-11-05 21:19:33 +0000291EXPORT_SYMBOL_GPL(platform_device_add);
292
293/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800294 * platform_device_del - remove a platform-level device
295 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500296 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800297 * Note that this function will also release all memory- and port-based
298 * resources owned by the device (@dev->resource). This function must
299 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500300 */
301void platform_device_del(struct platform_device *pdev)
302{
303 int i;
304
305 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200306 device_del(&pdev->dev);
307
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500308 for (i = 0; i < pdev->num_resources; i++) {
309 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700310 unsigned long type = resource_type(r);
311
312 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500313 release_resource(r);
314 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500315 }
316}
317EXPORT_SYMBOL_GPL(platform_device_del);
318
319/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800320 * platform_device_register - add a platform-level device
321 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000322 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800323int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000324{
325 device_initialize(&pdev->dev);
326 return platform_device_add(pdev);
327}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500328EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800331 * platform_device_unregister - unregister a platform-level device
332 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800334 * Unregistration is done in 2 steps. First we release all resources
335 * and remove it from the subsystem, then we drop reference count by
336 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800338void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500340 platform_device_del(pdev);
341 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500343EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800346 * platform_device_register_simple
347 * @name: base name of the device we're adding
348 * @id: instance id
349 * @res: set of resources that needs to be allocated for the device
350 * @num: number of resources
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800352 * This function creates a simple platform device that requires minimal
353 * resource and memory management. Canned release function freeing memory
354 * allocated for the device allows drivers using such devices to be
355 * unloaded without waiting for the last reference to the device to be
356 * dropped.
David Brownell49a4ec12007-05-08 00:29:39 -0700357 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800358 * This interface is primarily intended for use with legacy drivers which
359 * probe hardware directly. Because such drivers create sysfs device nodes
360 * themselves, rather than letting system infrastructure handle such device
361 * enumeration tasks, they don't fully conform to the Linux driver model.
362 * In particular, when such drivers are built as modules, they can't be
363 * "hotplugged".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800365struct platform_device *platform_device_register_simple(const char *name,
366 int id,
367 struct resource *res,
368 unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Russell King37c12e72005-11-05 21:19:33 +0000370 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int retval;
372
Russell King37c12e72005-11-05 21:19:33 +0000373 pdev = platform_device_alloc(name, id);
374 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 retval = -ENOMEM;
376 goto error;
377 }
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000380 retval = platform_device_add_resources(pdev, res, num);
381 if (retval)
382 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Russell King37c12e72005-11-05 21:19:33 +0000385 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (retval)
387 goto error;
388
Russell King37c12e72005-11-05 21:19:33 +0000389 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391error:
Russell King37c12e72005-11-05 21:19:33 +0000392 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return ERR_PTR(retval);
394}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500395EXPORT_SYMBOL_GPL(platform_device_register_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700397/**
398 * platform_device_register_data
399 * @parent: parent device for the device we're adding
400 * @name: base name of the device we're adding
401 * @id: instance id
402 * @data: platform specific data for this platform device
403 * @size: size of platform specific data
404 *
405 * This function creates a simple platform device that requires minimal
406 * resource and memory management. Canned release function freeing memory
407 * allocated for the device allows drivers using such devices to be
408 * unloaded without waiting for the last reference to the device to be
409 * dropped.
410 */
411struct platform_device *platform_device_register_data(
412 struct device *parent,
413 const char *name, int id,
414 const void *data, size_t size)
415{
416 struct platform_device *pdev;
417 int retval;
418
419 pdev = platform_device_alloc(name, id);
420 if (!pdev) {
421 retval = -ENOMEM;
422 goto error;
423 }
424
425 pdev->dev.parent = parent;
426
427 if (size) {
428 retval = platform_device_add_data(pdev, data, size);
429 if (retval)
430 goto error;
431 }
432
433 retval = platform_device_add(pdev);
434 if (retval)
435 goto error;
436
437 return pdev;
438
439error:
440 platform_device_put(pdev);
441 return ERR_PTR(retval);
442}
443
Russell King00d3dcd2005-11-09 17:23:39 +0000444static int platform_drv_probe(struct device *_dev)
445{
446 struct platform_driver *drv = to_platform_driver(_dev->driver);
447 struct platform_device *dev = to_platform_device(_dev);
448
449 return drv->probe(dev);
450}
451
David Brownellc67334f2006-11-16 23:28:47 -0800452static int platform_drv_probe_fail(struct device *_dev)
453{
454 return -ENXIO;
455}
456
Russell King00d3dcd2005-11-09 17:23:39 +0000457static int platform_drv_remove(struct device *_dev)
458{
459 struct platform_driver *drv = to_platform_driver(_dev->driver);
460 struct platform_device *dev = to_platform_device(_dev);
461
462 return drv->remove(dev);
463}
464
465static void platform_drv_shutdown(struct device *_dev)
466{
467 struct platform_driver *drv = to_platform_driver(_dev->driver);
468 struct platform_device *dev = to_platform_device(_dev);
469
470 drv->shutdown(dev);
471}
472
Russell King00d3dcd2005-11-09 17:23:39 +0000473/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800474 * platform_driver_register
475 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000476 */
477int platform_driver_register(struct platform_driver *drv)
478{
479 drv->driver.bus = &platform_bus_type;
480 if (drv->probe)
481 drv->driver.probe = platform_drv_probe;
482 if (drv->remove)
483 drv->driver.remove = platform_drv_remove;
484 if (drv->shutdown)
485 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200486 if (drv->suspend || drv->resume)
487 pr_warning("Platform driver '%s' needs updating - please use "
488 "dev_pm_ops\n", drv->driver.name);
489
Russell King00d3dcd2005-11-09 17:23:39 +0000490 return driver_register(&drv->driver);
491}
492EXPORT_SYMBOL_GPL(platform_driver_register);
493
494/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800495 * platform_driver_unregister
496 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000497 */
498void platform_driver_unregister(struct platform_driver *drv)
499{
500 driver_unregister(&drv->driver);
501}
502EXPORT_SYMBOL_GPL(platform_driver_unregister);
503
David Brownellc67334f2006-11-16 23:28:47 -0800504/**
505 * platform_driver_probe - register driver for non-hotpluggable device
506 * @drv: platform driver structure
507 * @probe: the driver probe routine, probably from an __init section
508 *
509 * Use this instead of platform_driver_register() when you know the device
510 * is not hotpluggable and has already been registered, and you want to
511 * remove its run-once probe() infrastructure from memory after the driver
512 * has bound to the device.
513 *
514 * One typical use for this would be with drivers for controllers integrated
515 * into system-on-chip processors, where the controller devices have been
516 * configured as part of board setup.
517 *
518 * Returns zero if the driver registered and bound to a device, else returns
519 * a negative error code and with the driver not registered.
520 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800521int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800522 int (*probe)(struct platform_device *))
523{
524 int retval, code;
525
526 /* temporary section violation during probe() */
527 drv->probe = probe;
528 retval = code = platform_driver_register(drv);
529
530 /* Fixup that section violation, being paranoid about code scanning
531 * the list of drivers in order to probe new devices. Check to see
532 * if the probe was successful, and make sure any forced probes of
533 * new devices fail.
534 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700535 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800536 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800537 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800538 retval = -ENODEV;
539 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700540 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800541
542 if (code != retval)
543 platform_driver_unregister(drv);
544 return retval;
545}
546EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
David Brownella0245f72006-05-29 10:37:33 -0700548/* modalias support enables more hands-off userspace setup:
549 * (a) environment variable lets new-style hotplug events work once system is
550 * fully running: "modprobe $MODALIAS"
551 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
552 * mishandled before system is fully running: "modprobe $(cat modalias)"
553 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800554static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
555 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700556{
557 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200558 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700559
560 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
561}
562
563static struct device_attribute platform_dev_attrs[] = {
564 __ATTR_RO(modalias),
565 __ATTR_NULL,
566};
567
Kay Sievers7eff2e72007-08-14 15:15:12 +0200568static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700569{
570 struct platform_device *pdev = to_platform_device(dev);
571
Eric Miao57fee4a2009-02-04 11:52:40 +0800572 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
573 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700574 return 0;
575}
576
Eric Miao57fee4a2009-02-04 11:52:40 +0800577static const struct platform_device_id *platform_match_id(
578 struct platform_device_id *id,
579 struct platform_device *pdev)
580{
581 while (id->name[0]) {
582 if (strcmp(pdev->name, id->name) == 0) {
583 pdev->id_entry = id;
584 return id;
585 }
586 id++;
587 }
588 return NULL;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800592 * platform_match - bind platform device to platform driver.
593 * @dev: device.
594 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800596 * Platform device IDs are assumed to be encoded like this:
597 * "<name><instance>", where <name> is a short description of the type of
598 * device, like "pci" or "floppy", and <instance> is the enumerated
599 * instance of the device, like '0' or '42'. Driver IDs are simply
600 * "<name>". So, extract the <name> from the platform_device structure,
601 * and compare it against the name of the driver. Return whether they match
602 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800604static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800606 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800607 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Eric Miao57fee4a2009-02-04 11:52:40 +0800609 /* match against the id table first */
610 if (pdrv->id_table)
611 return platform_match_id(pdrv->id_table, pdev) != NULL;
612
613 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100614 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200617#ifdef CONFIG_PM_SLEEP
618
619static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200621 struct platform_driver *pdrv = to_platform_driver(dev->driver);
622 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int ret = 0;
624
Magnus Damm783ea7d2009-06-04 22:13:33 +0200625 if (dev->driver && pdrv->suspend)
626 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700627
628 return ret;
629}
630
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200631static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200633 struct platform_driver *pdrv = to_platform_driver(dev->driver);
634 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int ret = 0;
636
Magnus Damm783ea7d2009-06-04 22:13:33 +0200637 if (dev->driver && pdrv->resume)
638 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ret;
641}
642
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200643static int platform_pm_prepare(struct device *dev)
644{
645 struct device_driver *drv = dev->driver;
646 int ret = 0;
647
648 if (drv && drv->pm && drv->pm->prepare)
649 ret = drv->pm->prepare(dev);
650
651 return ret;
652}
653
654static void platform_pm_complete(struct device *dev)
655{
656 struct device_driver *drv = dev->driver;
657
658 if (drv && drv->pm && drv->pm->complete)
659 drv->pm->complete(dev);
660}
661
662#ifdef CONFIG_SUSPEND
663
664static int platform_pm_suspend(struct device *dev)
665{
666 struct device_driver *drv = dev->driver;
667 int ret = 0;
668
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200669 if (!drv)
670 return 0;
671
672 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200673 if (drv->pm->suspend)
674 ret = drv->pm->suspend(dev);
675 } else {
676 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
677 }
678
679 return ret;
680}
681
682static int platform_pm_suspend_noirq(struct device *dev)
683{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200684 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200685 int ret = 0;
686
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200687 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200688 return 0;
689
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200690 if (drv->pm) {
691 if (drv->pm->suspend_noirq)
692 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200693 }
694
695 return ret;
696}
697
698static int platform_pm_resume(struct device *dev)
699{
700 struct device_driver *drv = dev->driver;
701 int ret = 0;
702
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200703 if (!drv)
704 return 0;
705
706 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200707 if (drv->pm->resume)
708 ret = drv->pm->resume(dev);
709 } else {
710 ret = platform_legacy_resume(dev);
711 }
712
713 return ret;
714}
715
716static int platform_pm_resume_noirq(struct device *dev)
717{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200718 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200719 int ret = 0;
720
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200721 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200722 return 0;
723
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200724 if (drv->pm) {
725 if (drv->pm->resume_noirq)
726 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200727 }
728
729 return ret;
730}
731
732#else /* !CONFIG_SUSPEND */
733
734#define platform_pm_suspend NULL
735#define platform_pm_resume NULL
736#define platform_pm_suspend_noirq NULL
737#define platform_pm_resume_noirq NULL
738
739#endif /* !CONFIG_SUSPEND */
740
741#ifdef CONFIG_HIBERNATION
742
743static int platform_pm_freeze(struct device *dev)
744{
745 struct device_driver *drv = dev->driver;
746 int ret = 0;
747
748 if (!drv)
749 return 0;
750
751 if (drv->pm) {
752 if (drv->pm->freeze)
753 ret = drv->pm->freeze(dev);
754 } else {
755 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
756 }
757
758 return ret;
759}
760
761static int platform_pm_freeze_noirq(struct device *dev)
762{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200763 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200764 int ret = 0;
765
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200766 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200767 return 0;
768
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200769 if (drv->pm) {
770 if (drv->pm->freeze_noirq)
771 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200772 }
773
774 return ret;
775}
776
777static int platform_pm_thaw(struct device *dev)
778{
779 struct device_driver *drv = dev->driver;
780 int ret = 0;
781
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200782 if (!drv)
783 return 0;
784
785 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200786 if (drv->pm->thaw)
787 ret = drv->pm->thaw(dev);
788 } else {
789 ret = platform_legacy_resume(dev);
790 }
791
792 return ret;
793}
794
795static int platform_pm_thaw_noirq(struct device *dev)
796{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200797 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200798 int ret = 0;
799
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200800 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200801 return 0;
802
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200803 if (drv->pm) {
804 if (drv->pm->thaw_noirq)
805 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200806 }
807
808 return ret;
809}
810
811static int platform_pm_poweroff(struct device *dev)
812{
813 struct device_driver *drv = dev->driver;
814 int ret = 0;
815
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200816 if (!drv)
817 return 0;
818
819 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200820 if (drv->pm->poweroff)
821 ret = drv->pm->poweroff(dev);
822 } else {
823 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
824 }
825
826 return ret;
827}
828
829static int platform_pm_poweroff_noirq(struct device *dev)
830{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200831 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200832 int ret = 0;
833
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200834 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200835 return 0;
836
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200837 if (drv->pm) {
838 if (drv->pm->poweroff_noirq)
839 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200840 }
841
842 return ret;
843}
844
845static int platform_pm_restore(struct device *dev)
846{
847 struct device_driver *drv = dev->driver;
848 int ret = 0;
849
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200850 if (!drv)
851 return 0;
852
853 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200854 if (drv->pm->restore)
855 ret = drv->pm->restore(dev);
856 } else {
857 ret = platform_legacy_resume(dev);
858 }
859
860 return ret;
861}
862
863static int platform_pm_restore_noirq(struct device *dev)
864{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200865 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200866 int ret = 0;
867
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200868 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200869 return 0;
870
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200871 if (drv->pm) {
872 if (drv->pm->restore_noirq)
873 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200874 }
875
876 return ret;
877}
878
879#else /* !CONFIG_HIBERNATION */
880
881#define platform_pm_freeze NULL
882#define platform_pm_thaw NULL
883#define platform_pm_poweroff NULL
884#define platform_pm_restore NULL
885#define platform_pm_freeze_noirq NULL
886#define platform_pm_thaw_noirq NULL
887#define platform_pm_poweroff_noirq NULL
888#define platform_pm_restore_noirq NULL
889
890#endif /* !CONFIG_HIBERNATION */
891
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200892static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200893 .prepare = platform_pm_prepare,
894 .complete = platform_pm_complete,
895 .suspend = platform_pm_suspend,
896 .resume = platform_pm_resume,
897 .freeze = platform_pm_freeze,
898 .thaw = platform_pm_thaw,
899 .poweroff = platform_pm_poweroff,
900 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200901 .suspend_noirq = platform_pm_suspend_noirq,
902 .resume_noirq = platform_pm_resume_noirq,
903 .freeze_noirq = platform_pm_freeze_noirq,
904 .thaw_noirq = platform_pm_thaw_noirq,
905 .poweroff_noirq = platform_pm_poweroff_noirq,
906 .restore_noirq = platform_pm_restore_noirq,
907};
908
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200909#define PLATFORM_PM_OPS_PTR (&platform_dev_pm_ops)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200910
911#else /* !CONFIG_PM_SLEEP */
912
913#define PLATFORM_PM_OPS_PTR NULL
914
915#endif /* !CONFIG_PM_SLEEP */
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917struct bus_type platform_bus_type = {
918 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700919 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700921 .uevent = platform_uevent,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200922 .pm = PLATFORM_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500924EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926int __init platform_bus_init(void)
927{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100928 int error;
929
Magnus Damm13977092009-03-30 14:37:25 -0700930 early_platform_cleanup();
931
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100932 error = device_register(&platform_bus);
933 if (error)
934 return error;
935 error = bus_register(&platform_bus_type);
936 if (error)
937 device_unregister(&platform_bus);
938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
942u64 dma_get_required_mask(struct device *dev)
943{
944 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
945 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
946 u64 mask;
947
948 if (!high_totalram) {
949 /* convert to mask just covering totalram */
950 low_totalram = (1 << (fls(low_totalram) - 1));
951 low_totalram += low_totalram - 1;
952 mask = low_totalram;
953 } else {
954 high_totalram = (1 << (fls(high_totalram) - 1));
955 high_totalram += high_totalram - 1;
956 mask = (((u64)high_totalram) << 32) + 0xffffffff;
957 }
James Bottomleye88a0c22008-03-09 11:57:56 -0500958 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960EXPORT_SYMBOL_GPL(dma_get_required_mask);
961#endif
Magnus Damm13977092009-03-30 14:37:25 -0700962
963static __initdata LIST_HEAD(early_platform_driver_list);
964static __initdata LIST_HEAD(early_platform_device_list);
965
966/**
967 * early_platform_driver_register
Randy Dunlapd86c1302009-04-21 07:22:53 -0700968 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -0700969 * @buf: string passed from early_param()
970 */
971int __init early_platform_driver_register(struct early_platform_driver *epdrv,
972 char *buf)
973{
974 unsigned long index;
975 int n;
976
977 /* Simply add the driver to the end of the global list.
978 * Drivers will by default be put on the list in compiled-in order.
979 */
980 if (!epdrv->list.next) {
981 INIT_LIST_HEAD(&epdrv->list);
982 list_add_tail(&epdrv->list, &early_platform_driver_list);
983 }
984
985 /* If the user has specified device then make sure the driver
986 * gets prioritized. The driver of the last device specified on
987 * command line will be put first on the list.
988 */
989 n = strlen(epdrv->pdrv->driver.name);
990 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
991 list_move(&epdrv->list, &early_platform_driver_list);
992
993 if (!strcmp(buf, epdrv->pdrv->driver.name))
994 epdrv->requested_id = -1;
995 else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
996 &index) == 0)
997 epdrv->requested_id = index;
998 else
999 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1000 }
1001
1002 return 0;
1003}
1004
1005/**
1006 * early_platform_add_devices - add a numbers of early platform devices
1007 * @devs: array of early platform devices to add
1008 * @num: number of early platform devices in array
1009 */
1010void __init early_platform_add_devices(struct platform_device **devs, int num)
1011{
1012 struct device *dev;
1013 int i;
1014
1015 /* simply add the devices to list */
1016 for (i = 0; i < num; i++) {
1017 dev = &devs[i]->dev;
1018
1019 if (!dev->devres_head.next) {
1020 INIT_LIST_HEAD(&dev->devres_head);
1021 list_add_tail(&dev->devres_head,
1022 &early_platform_device_list);
1023 }
1024 }
1025}
1026
1027/**
1028 * early_platform_driver_register_all
1029 * @class_str: string to identify early platform driver class
1030 */
1031void __init early_platform_driver_register_all(char *class_str)
1032{
1033 /* The "class_str" parameter may or may not be present on the kernel
1034 * command line. If it is present then there may be more than one
1035 * matching parameter.
1036 *
1037 * Since we register our early platform drivers using early_param()
1038 * we need to make sure that they also get registered in the case
1039 * when the parameter is missing from the kernel command line.
1040 *
1041 * We use parse_early_options() to make sure the early_param() gets
1042 * called at least once. The early_param() may be called more than
1043 * once since the name of the preferred device may be specified on
1044 * the kernel command line. early_platform_driver_register() handles
1045 * this case for us.
1046 */
1047 parse_early_options(class_str);
1048}
1049
1050/**
1051 * early_platform_match
Randy Dunlapd86c1302009-04-21 07:22:53 -07001052 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001053 * @id: id to match against
1054 */
1055static __init struct platform_device *
1056early_platform_match(struct early_platform_driver *epdrv, int id)
1057{
1058 struct platform_device *pd;
1059
1060 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1061 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1062 if (pd->id == id)
1063 return pd;
1064
1065 return NULL;
1066}
1067
1068/**
1069 * early_platform_left
Randy Dunlapd86c1302009-04-21 07:22:53 -07001070 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001071 * @id: return true if id or above exists
1072 */
1073static __init int early_platform_left(struct early_platform_driver *epdrv,
1074 int id)
1075{
1076 struct platform_device *pd;
1077
1078 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1079 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1080 if (pd->id >= id)
1081 return 1;
1082
1083 return 0;
1084}
1085
1086/**
1087 * early_platform_driver_probe_id
1088 * @class_str: string to identify early platform driver class
1089 * @id: id to match against
1090 * @nr_probe: number of platform devices to successfully probe before exiting
1091 */
1092static int __init early_platform_driver_probe_id(char *class_str,
1093 int id,
1094 int nr_probe)
1095{
1096 struct early_platform_driver *epdrv;
1097 struct platform_device *match;
1098 int match_id;
1099 int n = 0;
1100 int left = 0;
1101
1102 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1103 /* only use drivers matching our class_str */
1104 if (strcmp(class_str, epdrv->class_str))
1105 continue;
1106
1107 if (id == -2) {
1108 match_id = epdrv->requested_id;
1109 left = 1;
1110
1111 } else {
1112 match_id = id;
1113 left += early_platform_left(epdrv, id);
1114
1115 /* skip requested id */
1116 switch (epdrv->requested_id) {
1117 case EARLY_PLATFORM_ID_ERROR:
1118 case EARLY_PLATFORM_ID_UNSET:
1119 break;
1120 default:
1121 if (epdrv->requested_id == id)
1122 match_id = EARLY_PLATFORM_ID_UNSET;
1123 }
1124 }
1125
1126 switch (match_id) {
1127 case EARLY_PLATFORM_ID_ERROR:
1128 pr_warning("%s: unable to parse %s parameter\n",
1129 class_str, epdrv->pdrv->driver.name);
1130 /* fall-through */
1131 case EARLY_PLATFORM_ID_UNSET:
1132 match = NULL;
1133 break;
1134 default:
1135 match = early_platform_match(epdrv, match_id);
1136 }
1137
1138 if (match) {
1139 if (epdrv->pdrv->probe(match))
1140 pr_warning("%s: unable to probe %s early.\n",
1141 class_str, match->name);
1142 else
1143 n++;
1144 }
1145
1146 if (n >= nr_probe)
1147 break;
1148 }
1149
1150 if (left)
1151 return n;
1152 else
1153 return -ENODEV;
1154}
1155
1156/**
1157 * early_platform_driver_probe
1158 * @class_str: string to identify early platform driver class
1159 * @nr_probe: number of platform devices to successfully probe before exiting
1160 * @user_only: only probe user specified early platform devices
1161 */
1162int __init early_platform_driver_probe(char *class_str,
1163 int nr_probe,
1164 int user_only)
1165{
1166 int k, n, i;
1167
1168 n = 0;
1169 for (i = -2; n < nr_probe; i++) {
1170 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1171
1172 if (k < 0)
1173 break;
1174
1175 n += k;
1176
1177 if (user_only)
1178 break;
1179 }
1180
1181 return n;
1182}
1183
1184/**
1185 * early_platform_cleanup - clean up early platform code
1186 */
1187void __init early_platform_cleanup(void)
1188{
1189 struct platform_device *pd, *pd2;
1190
1191 /* clean up the devres list used to chain devices */
1192 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1193 dev.devres_head) {
1194 list_del(&pd->dev.devres_head);
1195 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1196 }
1197}
1198