blob: 4fa954b07ac4ada384d27ffba6b293c22651febd [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/dma-mapping.h>
18#include <linux/bootmem.h>
19#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020021#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010023#include "base.h"
24
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080025#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
26 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010029 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050031EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080034 * platform_get_resource - get a resource for a device
35 * @dev: platform device
36 * @type: resource type
37 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080039struct resource *platform_get_resource(struct platform_device *dev,
40 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int i;
43
44 for (i = 0; i < dev->num_resources; i++) {
45 struct resource *r = &dev->resource[i];
46
Magnus Dammc9f66162008-10-15 22:05:15 -070047 if (type == resource_type(r) && num-- == 0)
48 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 return NULL;
51}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050052EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080055 * platform_get_irq - get an IRQ for a device
56 * @dev: platform device
57 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59int platform_get_irq(struct platform_device *dev, unsigned int num)
60{
61 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
62
David Vrabel305b3222006-01-19 17:52:27 +000063 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050065EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080068 * platform_get_resource_byname - get a resource for a device by name
69 * @dev: platform device
70 * @type: resource type
71 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080073struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +020074 unsigned int type,
75 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int i;
78
79 for (i = 0; i < dev->num_resources; i++) {
80 struct resource *r = &dev->resource[i];
81
Magnus Dammc9f66162008-10-15 22:05:15 -070082 if (type == resource_type(r) && !strcmp(r->name, name))
83 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 return NULL;
86}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050087EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090 * platform_get_irq - get an IRQ for a device
91 * @dev: platform device
92 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +020094int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080096 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
97 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David Vrabel305b3222006-01-19 17:52:27 +000099 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500101EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800104 * platform_add_devices - add a numbers of platform devices
105 * @devs: array of platform devices to add
106 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
108int platform_add_devices(struct platform_device **devs, int num)
109{
110 int i, ret = 0;
111
112 for (i = 0; i < num; i++) {
113 ret = platform_device_register(devs[i]);
114 if (ret) {
115 while (--i >= 0)
116 platform_device_unregister(devs[i]);
117 break;
118 }
119 }
120
121 return ret;
122}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500123EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Russell King37c12e72005-11-05 21:19:33 +0000125struct platform_object {
126 struct platform_device pdev;
127 char name[1];
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800131 * platform_device_put
132 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000133 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800134 * Free all memory associated with a platform device. This function must
135 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000136 */
137void platform_device_put(struct platform_device *pdev)
138{
139 if (pdev)
140 put_device(&pdev->dev);
141}
142EXPORT_SYMBOL_GPL(platform_device_put);
143
144static void platform_device_release(struct device *dev)
145{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800146 struct platform_object *pa = container_of(dev, struct platform_object,
147 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000148
149 kfree(pa->pdev.dev.platform_data);
150 kfree(pa->pdev.resource);
151 kfree(pa);
152}
153
154/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800155 * platform_device_alloc
156 * @name: base name of the device we're adding
157 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000158 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800159 * Create a platform device object which can have other objects attached
160 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000161 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200162struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000163{
164 struct platform_object *pa;
165
166 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
167 if (pa) {
168 strcpy(pa->name, name);
169 pa->pdev.name = pa->name;
170 pa->pdev.id = id;
171 device_initialize(&pa->pdev.dev);
172 pa->pdev.dev.release = platform_device_release;
173 }
174
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500175 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000176}
177EXPORT_SYMBOL_GPL(platform_device_alloc);
178
179/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800180 * platform_device_add_resources
181 * @pdev: platform device allocated by platform_device_alloc to add resources to
182 * @res: set of resources that needs to be allocated for the device
183 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000184 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800185 * Add a copy of the resources to the platform device. The memory
186 * associated with the resources will be freed when the platform device is
187 * released.
Russell King37c12e72005-11-05 21:19:33 +0000188 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800189int platform_device_add_resources(struct platform_device *pdev,
190 struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000191{
192 struct resource *r;
193
194 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
195 if (r) {
196 memcpy(r, res, sizeof(struct resource) * num);
197 pdev->resource = r;
198 pdev->num_resources = num;
199 }
200 return r ? 0 : -ENOMEM;
201}
202EXPORT_SYMBOL_GPL(platform_device_add_resources);
203
204/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800205 * platform_device_add_data
206 * @pdev: platform device allocated by platform_device_alloc to add resources to
207 * @data: platform specific data for this platform device
208 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000209 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800210 * Add a copy of platform specific data to the platform device's
211 * platform_data pointer. The memory associated with the platform data
212 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000213 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214int platform_device_add_data(struct platform_device *pdev, const void *data,
215 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000216{
Andrew Mortondaa41222009-08-06 16:00:44 -0700217 void *d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000218
Russell King37c12e72005-11-05 21:19:33 +0000219 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000220 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700221 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000222 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700223 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000224}
225EXPORT_SYMBOL_GPL(platform_device_add_data);
226
227/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800228 * platform_device_add - add a platform device to device hierarchy
229 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800231 * This is part 2 of platform_device_register(), though may be called
232 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Russell King37c12e72005-11-05 21:19:33 +0000234int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int i, ret = 0;
237
238 if (!pdev)
239 return -EINVAL;
240
241 if (!pdev->dev.parent)
242 pdev->dev.parent = &platform_bus;
243
244 pdev->dev.bus = &platform_bus_type;
245
246 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100247 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700249 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 for (i = 0; i < pdev->num_resources; i++) {
252 struct resource *p, *r = &pdev->resource[i];
253
254 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100255 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 p = r->parent;
258 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700259 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700261 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 p = &ioport_resource;
263 }
264
Kumar Galad960bb42005-11-28 10:15:39 -0600265 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 printk(KERN_ERR
267 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100268 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = -EBUSY;
270 goto failed;
271 }
272 }
273
274 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100275 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Russell Kinge3915532006-05-06 08:15:26 +0100277 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (ret == 0)
279 return ret;
280
281 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700282 while (--i >= 0) {
283 struct resource *r = &pdev->resource[i];
284 unsigned long type = resource_type(r);
285
286 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
287 release_resource(r);
288 }
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
Russell King37c12e72005-11-05 21:19:33 +0000292EXPORT_SYMBOL_GPL(platform_device_add);
293
294/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800295 * platform_device_del - remove a platform-level device
296 * @pdev: platform device we're removing
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500297 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800298 * Note that this function will also release all memory- and port-based
299 * resources owned by the device (@dev->resource). This function must
300 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500301 */
302void platform_device_del(struct platform_device *pdev)
303{
304 int i;
305
306 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200307 device_del(&pdev->dev);
308
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500309 for (i = 0; i < pdev->num_resources; i++) {
310 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700311 unsigned long type = resource_type(r);
312
313 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500314 release_resource(r);
315 }
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500316 }
317}
318EXPORT_SYMBOL_GPL(platform_device_del);
319
320/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800321 * platform_device_register - add a platform-level device
322 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000323 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800324int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000325{
326 device_initialize(&pdev->dev);
327 return platform_device_add(pdev);
328}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500329EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800332 * platform_device_unregister - unregister a platform-level device
333 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800335 * Unregistration is done in 2 steps. First we release all resources
336 * and remove it from the subsystem, then we drop reference count by
337 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800339void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dmitry Torokhov93ce30612005-12-10 01:36:27 -0500341 platform_device_del(pdev);
342 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500344EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800347 * platform_device_register_simple
348 * @name: base name of the device we're adding
349 * @id: instance id
350 * @res: set of resources that needs to be allocated for the device
351 * @num: number of resources
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800353 * This function creates a simple platform device that requires minimal
354 * resource and memory management. Canned release function freeing memory
355 * allocated for the device allows drivers using such devices to be
356 * unloaded without waiting for the last reference to the device to be
357 * dropped.
David Brownell49a4ec12007-05-08 00:29:39 -0700358 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800359 * This interface is primarily intended for use with legacy drivers which
360 * probe hardware directly. Because such drivers create sysfs device nodes
361 * themselves, rather than letting system infrastructure handle such device
362 * enumeration tasks, they don't fully conform to the Linux driver model.
363 * In particular, when such drivers are built as modules, they can't be
364 * "hotplugged".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800366struct platform_device *platform_device_register_simple(const char *name,
367 int id,
368 struct resource *res,
369 unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Russell King37c12e72005-11-05 21:19:33 +0000371 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int retval;
373
Russell King37c12e72005-11-05 21:19:33 +0000374 pdev = platform_device_alloc(name, id);
375 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 retval = -ENOMEM;
377 goto error;
378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000381 retval = platform_device_add_resources(pdev, res, num);
382 if (retval)
383 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Russell King37c12e72005-11-05 21:19:33 +0000386 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (retval)
388 goto error;
389
Russell King37c12e72005-11-05 21:19:33 +0000390 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392error:
Russell King37c12e72005-11-05 21:19:33 +0000393 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return ERR_PTR(retval);
395}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500396EXPORT_SYMBOL_GPL(platform_device_register_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700398/**
399 * platform_device_register_data
400 * @parent: parent device for the device we're adding
401 * @name: base name of the device we're adding
402 * @id: instance id
403 * @data: platform specific data for this platform device
404 * @size: size of platform specific data
405 *
406 * This function creates a simple platform device that requires minimal
407 * resource and memory management. Canned release function freeing memory
408 * allocated for the device allows drivers using such devices to be
409 * unloaded without waiting for the last reference to the device to be
410 * dropped.
411 */
412struct platform_device *platform_device_register_data(
413 struct device *parent,
414 const char *name, int id,
415 const void *data, size_t size)
416{
417 struct platform_device *pdev;
418 int retval;
419
420 pdev = platform_device_alloc(name, id);
421 if (!pdev) {
422 retval = -ENOMEM;
423 goto error;
424 }
425
426 pdev->dev.parent = parent;
427
428 if (size) {
429 retval = platform_device_add_data(pdev, data, size);
430 if (retval)
431 goto error;
432 }
433
434 retval = platform_device_add(pdev);
435 if (retval)
436 goto error;
437
438 return pdev;
439
440error:
441 platform_device_put(pdev);
442 return ERR_PTR(retval);
443}
444
Russell King00d3dcd2005-11-09 17:23:39 +0000445static int platform_drv_probe(struct device *_dev)
446{
447 struct platform_driver *drv = to_platform_driver(_dev->driver);
448 struct platform_device *dev = to_platform_device(_dev);
449
450 return drv->probe(dev);
451}
452
David Brownellc67334f2006-11-16 23:28:47 -0800453static int platform_drv_probe_fail(struct device *_dev)
454{
455 return -ENXIO;
456}
457
Russell King00d3dcd2005-11-09 17:23:39 +0000458static int platform_drv_remove(struct device *_dev)
459{
460 struct platform_driver *drv = to_platform_driver(_dev->driver);
461 struct platform_device *dev = to_platform_device(_dev);
462
463 return drv->remove(dev);
464}
465
466static void platform_drv_shutdown(struct device *_dev)
467{
468 struct platform_driver *drv = to_platform_driver(_dev->driver);
469 struct platform_device *dev = to_platform_device(_dev);
470
471 drv->shutdown(dev);
472}
473
Russell King00d3dcd2005-11-09 17:23:39 +0000474/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800475 * platform_driver_register
476 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000477 */
478int platform_driver_register(struct platform_driver *drv)
479{
480 drv->driver.bus = &platform_bus_type;
481 if (drv->probe)
482 drv->driver.probe = platform_drv_probe;
483 if (drv->remove)
484 drv->driver.remove = platform_drv_remove;
485 if (drv->shutdown)
486 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200487
Russell King00d3dcd2005-11-09 17:23:39 +0000488 return driver_register(&drv->driver);
489}
490EXPORT_SYMBOL_GPL(platform_driver_register);
491
492/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800493 * platform_driver_unregister
494 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000495 */
496void platform_driver_unregister(struct platform_driver *drv)
497{
498 driver_unregister(&drv->driver);
499}
500EXPORT_SYMBOL_GPL(platform_driver_unregister);
501
David Brownellc67334f2006-11-16 23:28:47 -0800502/**
503 * platform_driver_probe - register driver for non-hotpluggable device
504 * @drv: platform driver structure
505 * @probe: the driver probe routine, probably from an __init section
506 *
507 * Use this instead of platform_driver_register() when you know the device
508 * is not hotpluggable and has already been registered, and you want to
509 * remove its run-once probe() infrastructure from memory after the driver
510 * has bound to the device.
511 *
512 * One typical use for this would be with drivers for controllers integrated
513 * into system-on-chip processors, where the controller devices have been
514 * configured as part of board setup.
515 *
516 * Returns zero if the driver registered and bound to a device, else returns
517 * a negative error code and with the driver not registered.
518 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800519int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800520 int (*probe)(struct platform_device *))
521{
522 int retval, code;
523
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700524 /* make sure driver won't have bind/unbind attributes */
525 drv->driver.suppress_bind_attrs = true;
526
David Brownellc67334f2006-11-16 23:28:47 -0800527 /* temporary section violation during probe() */
528 drv->probe = probe;
529 retval = code = platform_driver_register(drv);
530
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700531 /*
532 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800533 * the list of drivers in order to probe new devices. Check to see
534 * if the probe was successful, and make sure any forced probes of
535 * new devices fail.
536 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700537 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800538 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800539 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800540 retval = -ENODEV;
541 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700542 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800543
544 if (code != retval)
545 platform_driver_unregister(drv);
546 return retval;
547}
548EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
David Brownella0245f72006-05-29 10:37:33 -0700550/* modalias support enables more hands-off userspace setup:
551 * (a) environment variable lets new-style hotplug events work once system is
552 * fully running: "modprobe $MODALIAS"
553 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
554 * mishandled before system is fully running: "modprobe $(cat modalias)"
555 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800556static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
557 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700558{
559 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200560 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700561
562 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
563}
564
565static struct device_attribute platform_dev_attrs[] = {
566 __ATTR_RO(modalias),
567 __ATTR_NULL,
568};
569
Kay Sievers7eff2e72007-08-14 15:15:12 +0200570static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700571{
572 struct platform_device *pdev = to_platform_device(dev);
573
Eric Miao57fee4a2009-02-04 11:52:40 +0800574 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
575 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700576 return 0;
577}
578
Eric Miao57fee4a2009-02-04 11:52:40 +0800579static const struct platform_device_id *platform_match_id(
580 struct platform_device_id *id,
581 struct platform_device *pdev)
582{
583 while (id->name[0]) {
584 if (strcmp(pdev->name, id->name) == 0) {
585 pdev->id_entry = id;
586 return id;
587 }
588 id++;
589 }
590 return NULL;
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800594 * platform_match - bind platform device to platform driver.
595 * @dev: device.
596 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800598 * Platform device IDs are assumed to be encoded like this:
599 * "<name><instance>", where <name> is a short description of the type of
600 * device, like "pci" or "floppy", and <instance> is the enumerated
601 * instance of the device, like '0' or '42'. Driver IDs are simply
602 * "<name>". So, extract the <name> from the platform_device structure,
603 * and compare it against the name of the driver. Return whether they match
604 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800606static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800608 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800609 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Miao57fee4a2009-02-04 11:52:40 +0800611 /* match against the id table first */
612 if (pdrv->id_table)
613 return platform_match_id(pdrv->id_table, pdev) != NULL;
614
615 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100616 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200619#ifdef CONFIG_PM_SLEEP
620
621static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200623 struct platform_driver *pdrv = to_platform_driver(dev->driver);
624 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int ret = 0;
626
Magnus Damm783ea7d2009-06-04 22:13:33 +0200627 if (dev->driver && pdrv->suspend)
628 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -0700629
630 return ret;
631}
632
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200633static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200635 struct platform_driver *pdrv = to_platform_driver(dev->driver);
636 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int ret = 0;
638
Magnus Damm783ea7d2009-06-04 22:13:33 +0200639 if (dev->driver && pdrv->resume)
640 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return ret;
643}
644
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200645static int platform_pm_prepare(struct device *dev)
646{
647 struct device_driver *drv = dev->driver;
648 int ret = 0;
649
650 if (drv && drv->pm && drv->pm->prepare)
651 ret = drv->pm->prepare(dev);
652
653 return ret;
654}
655
656static void platform_pm_complete(struct device *dev)
657{
658 struct device_driver *drv = dev->driver;
659
660 if (drv && drv->pm && drv->pm->complete)
661 drv->pm->complete(dev);
662}
663
Magnus Damm9d730222009-08-20 20:25:32 +0200664#else /* !CONFIG_PM_SLEEP */
665
666#define platform_pm_prepare NULL
667#define platform_pm_complete NULL
668
669#endif /* !CONFIG_PM_SLEEP */
670
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200671#ifdef CONFIG_SUSPEND
672
673static int platform_pm_suspend(struct device *dev)
674{
675 struct device_driver *drv = dev->driver;
676 int ret = 0;
677
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200678 if (!drv)
679 return 0;
680
681 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200682 if (drv->pm->suspend)
683 ret = drv->pm->suspend(dev);
684 } else {
685 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
686 }
687
688 return ret;
689}
690
691static int platform_pm_suspend_noirq(struct device *dev)
692{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200693 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200694 int ret = 0;
695
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200696 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200697 return 0;
698
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200699 if (drv->pm) {
700 if (drv->pm->suspend_noirq)
701 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200702 }
703
704 return ret;
705}
706
707static int platform_pm_resume(struct device *dev)
708{
709 struct device_driver *drv = dev->driver;
710 int ret = 0;
711
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200712 if (!drv)
713 return 0;
714
715 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200716 if (drv->pm->resume)
717 ret = drv->pm->resume(dev);
718 } else {
719 ret = platform_legacy_resume(dev);
720 }
721
722 return ret;
723}
724
725static int platform_pm_resume_noirq(struct device *dev)
726{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200727 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200728 int ret = 0;
729
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200730 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200731 return 0;
732
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200733 if (drv->pm) {
734 if (drv->pm->resume_noirq)
735 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200736 }
737
738 return ret;
739}
740
741#else /* !CONFIG_SUSPEND */
742
743#define platform_pm_suspend NULL
744#define platform_pm_resume NULL
745#define platform_pm_suspend_noirq NULL
746#define platform_pm_resume_noirq NULL
747
748#endif /* !CONFIG_SUSPEND */
749
750#ifdef CONFIG_HIBERNATION
751
752static int platform_pm_freeze(struct device *dev)
753{
754 struct device_driver *drv = dev->driver;
755 int ret = 0;
756
757 if (!drv)
758 return 0;
759
760 if (drv->pm) {
761 if (drv->pm->freeze)
762 ret = drv->pm->freeze(dev);
763 } else {
764 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
765 }
766
767 return ret;
768}
769
770static int platform_pm_freeze_noirq(struct device *dev)
771{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200772 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200773 int ret = 0;
774
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200775 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200776 return 0;
777
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200778 if (drv->pm) {
779 if (drv->pm->freeze_noirq)
780 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200781 }
782
783 return ret;
784}
785
786static int platform_pm_thaw(struct device *dev)
787{
788 struct device_driver *drv = dev->driver;
789 int ret = 0;
790
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200791 if (!drv)
792 return 0;
793
794 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200795 if (drv->pm->thaw)
796 ret = drv->pm->thaw(dev);
797 } else {
798 ret = platform_legacy_resume(dev);
799 }
800
801 return ret;
802}
803
804static int platform_pm_thaw_noirq(struct device *dev)
805{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200806 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200807 int ret = 0;
808
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200809 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200810 return 0;
811
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200812 if (drv->pm) {
813 if (drv->pm->thaw_noirq)
814 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200815 }
816
817 return ret;
818}
819
820static int platform_pm_poweroff(struct device *dev)
821{
822 struct device_driver *drv = dev->driver;
823 int ret = 0;
824
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200825 if (!drv)
826 return 0;
827
828 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200829 if (drv->pm->poweroff)
830 ret = drv->pm->poweroff(dev);
831 } else {
832 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
833 }
834
835 return ret;
836}
837
838static int platform_pm_poweroff_noirq(struct device *dev)
839{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200840 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200841 int ret = 0;
842
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200843 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200844 return 0;
845
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200846 if (drv->pm) {
847 if (drv->pm->poweroff_noirq)
848 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200849 }
850
851 return ret;
852}
853
854static int platform_pm_restore(struct device *dev)
855{
856 struct device_driver *drv = dev->driver;
857 int ret = 0;
858
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200859 if (!drv)
860 return 0;
861
862 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200863 if (drv->pm->restore)
864 ret = drv->pm->restore(dev);
865 } else {
866 ret = platform_legacy_resume(dev);
867 }
868
869 return ret;
870}
871
872static int platform_pm_restore_noirq(struct device *dev)
873{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200874 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200875 int ret = 0;
876
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200877 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200878 return 0;
879
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200880 if (drv->pm) {
881 if (drv->pm->restore_noirq)
882 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200883 }
884
885 return ret;
886}
887
888#else /* !CONFIG_HIBERNATION */
889
890#define platform_pm_freeze NULL
891#define platform_pm_thaw NULL
892#define platform_pm_poweroff NULL
893#define platform_pm_restore NULL
894#define platform_pm_freeze_noirq NULL
895#define platform_pm_thaw_noirq NULL
896#define platform_pm_poweroff_noirq NULL
897#define platform_pm_restore_noirq NULL
898
899#endif /* !CONFIG_HIBERNATION */
900
Magnus Damm9d730222009-08-20 20:25:32 +0200901#ifdef CONFIG_PM_RUNTIME
902
903int __weak platform_pm_runtime_suspend(struct device *dev)
904{
905 return -ENOSYS;
906};
907
908int __weak platform_pm_runtime_resume(struct device *dev)
909{
910 return -ENOSYS;
911};
912
913int __weak platform_pm_runtime_idle(struct device *dev)
914{
915 return -ENOSYS;
916};
917
918#else /* !CONFIG_PM_RUNTIME */
919
920#define platform_pm_runtime_suspend NULL
921#define platform_pm_runtime_resume NULL
922#define platform_pm_runtime_idle NULL
923
924#endif /* !CONFIG_PM_RUNTIME */
925
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200926static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200927 .prepare = platform_pm_prepare,
928 .complete = platform_pm_complete,
929 .suspend = platform_pm_suspend,
930 .resume = platform_pm_resume,
931 .freeze = platform_pm_freeze,
932 .thaw = platform_pm_thaw,
933 .poweroff = platform_pm_poweroff,
934 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200935 .suspend_noirq = platform_pm_suspend_noirq,
936 .resume_noirq = platform_pm_resume_noirq,
937 .freeze_noirq = platform_pm_freeze_noirq,
938 .thaw_noirq = platform_pm_thaw_noirq,
939 .poweroff_noirq = platform_pm_poweroff_noirq,
940 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +0200941 .runtime_suspend = platform_pm_runtime_suspend,
942 .runtime_resume = platform_pm_runtime_resume,
943 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200944};
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946struct bus_type platform_bus_type = {
947 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700948 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700950 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200951 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500953EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955int __init platform_bus_init(void)
956{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100957 int error;
958
Magnus Damm13977092009-03-30 14:37:25 -0700959 early_platform_cleanup();
960
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100961 error = device_register(&platform_bus);
962 if (error)
963 return error;
964 error = bus_register(&platform_bus_type);
965 if (error)
966 device_unregister(&platform_bus);
967 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
971u64 dma_get_required_mask(struct device *dev)
972{
973 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
974 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
975 u64 mask;
976
977 if (!high_totalram) {
978 /* convert to mask just covering totalram */
979 low_totalram = (1 << (fls(low_totalram) - 1));
980 low_totalram += low_totalram - 1;
981 mask = low_totalram;
982 } else {
983 high_totalram = (1 << (fls(high_totalram) - 1));
984 high_totalram += high_totalram - 1;
985 mask = (((u64)high_totalram) << 32) + 0xffffffff;
986 }
James Bottomleye88a0c22008-03-09 11:57:56 -0500987 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989EXPORT_SYMBOL_GPL(dma_get_required_mask);
990#endif
Magnus Damm13977092009-03-30 14:37:25 -0700991
992static __initdata LIST_HEAD(early_platform_driver_list);
993static __initdata LIST_HEAD(early_platform_device_list);
994
995/**
996 * early_platform_driver_register
Randy Dunlapd86c1302009-04-21 07:22:53 -0700997 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -0700998 * @buf: string passed from early_param()
999 */
1000int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1001 char *buf)
1002{
1003 unsigned long index;
1004 int n;
1005
1006 /* Simply add the driver to the end of the global list.
1007 * Drivers will by default be put on the list in compiled-in order.
1008 */
1009 if (!epdrv->list.next) {
1010 INIT_LIST_HEAD(&epdrv->list);
1011 list_add_tail(&epdrv->list, &early_platform_driver_list);
1012 }
1013
1014 /* If the user has specified device then make sure the driver
1015 * gets prioritized. The driver of the last device specified on
1016 * command line will be put first on the list.
1017 */
1018 n = strlen(epdrv->pdrv->driver.name);
1019 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1020 list_move(&epdrv->list, &early_platform_driver_list);
1021
1022 if (!strcmp(buf, epdrv->pdrv->driver.name))
1023 epdrv->requested_id = -1;
1024 else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10,
1025 &index) == 0)
1026 epdrv->requested_id = index;
1027 else
1028 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1029 }
1030
1031 return 0;
1032}
1033
1034/**
1035 * early_platform_add_devices - add a numbers of early platform devices
1036 * @devs: array of early platform devices to add
1037 * @num: number of early platform devices in array
1038 */
1039void __init early_platform_add_devices(struct platform_device **devs, int num)
1040{
1041 struct device *dev;
1042 int i;
1043
1044 /* simply add the devices to list */
1045 for (i = 0; i < num; i++) {
1046 dev = &devs[i]->dev;
1047
1048 if (!dev->devres_head.next) {
1049 INIT_LIST_HEAD(&dev->devres_head);
1050 list_add_tail(&dev->devres_head,
1051 &early_platform_device_list);
1052 }
1053 }
1054}
1055
1056/**
1057 * early_platform_driver_register_all
1058 * @class_str: string to identify early platform driver class
1059 */
1060void __init early_platform_driver_register_all(char *class_str)
1061{
1062 /* The "class_str" parameter may or may not be present on the kernel
1063 * command line. If it is present then there may be more than one
1064 * matching parameter.
1065 *
1066 * Since we register our early platform drivers using early_param()
1067 * we need to make sure that they also get registered in the case
1068 * when the parameter is missing from the kernel command line.
1069 *
1070 * We use parse_early_options() to make sure the early_param() gets
1071 * called at least once. The early_param() may be called more than
1072 * once since the name of the preferred device may be specified on
1073 * the kernel command line. early_platform_driver_register() handles
1074 * this case for us.
1075 */
1076 parse_early_options(class_str);
1077}
1078
1079/**
1080 * early_platform_match
Randy Dunlapd86c1302009-04-21 07:22:53 -07001081 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001082 * @id: id to match against
1083 */
1084static __init struct platform_device *
1085early_platform_match(struct early_platform_driver *epdrv, int id)
1086{
1087 struct platform_device *pd;
1088
1089 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1090 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1091 if (pd->id == id)
1092 return pd;
1093
1094 return NULL;
1095}
1096
1097/**
1098 * early_platform_left
Randy Dunlapd86c1302009-04-21 07:22:53 -07001099 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001100 * @id: return true if id or above exists
1101 */
1102static __init int early_platform_left(struct early_platform_driver *epdrv,
1103 int id)
1104{
1105 struct platform_device *pd;
1106
1107 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1108 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1109 if (pd->id >= id)
1110 return 1;
1111
1112 return 0;
1113}
1114
1115/**
1116 * early_platform_driver_probe_id
1117 * @class_str: string to identify early platform driver class
1118 * @id: id to match against
1119 * @nr_probe: number of platform devices to successfully probe before exiting
1120 */
1121static int __init early_platform_driver_probe_id(char *class_str,
1122 int id,
1123 int nr_probe)
1124{
1125 struct early_platform_driver *epdrv;
1126 struct platform_device *match;
1127 int match_id;
1128 int n = 0;
1129 int left = 0;
1130
1131 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1132 /* only use drivers matching our class_str */
1133 if (strcmp(class_str, epdrv->class_str))
1134 continue;
1135
1136 if (id == -2) {
1137 match_id = epdrv->requested_id;
1138 left = 1;
1139
1140 } else {
1141 match_id = id;
1142 left += early_platform_left(epdrv, id);
1143
1144 /* skip requested id */
1145 switch (epdrv->requested_id) {
1146 case EARLY_PLATFORM_ID_ERROR:
1147 case EARLY_PLATFORM_ID_UNSET:
1148 break;
1149 default:
1150 if (epdrv->requested_id == id)
1151 match_id = EARLY_PLATFORM_ID_UNSET;
1152 }
1153 }
1154
1155 switch (match_id) {
1156 case EARLY_PLATFORM_ID_ERROR:
1157 pr_warning("%s: unable to parse %s parameter\n",
1158 class_str, epdrv->pdrv->driver.name);
1159 /* fall-through */
1160 case EARLY_PLATFORM_ID_UNSET:
1161 match = NULL;
1162 break;
1163 default:
1164 match = early_platform_match(epdrv, match_id);
1165 }
1166
1167 if (match) {
1168 if (epdrv->pdrv->probe(match))
1169 pr_warning("%s: unable to probe %s early.\n",
1170 class_str, match->name);
1171 else
1172 n++;
1173 }
1174
1175 if (n >= nr_probe)
1176 break;
1177 }
1178
1179 if (left)
1180 return n;
1181 else
1182 return -ENODEV;
1183}
1184
1185/**
1186 * early_platform_driver_probe
1187 * @class_str: string to identify early platform driver class
1188 * @nr_probe: number of platform devices to successfully probe before exiting
1189 * @user_only: only probe user specified early platform devices
1190 */
1191int __init early_platform_driver_probe(char *class_str,
1192 int nr_probe,
1193 int user_only)
1194{
1195 int k, n, i;
1196
1197 n = 0;
1198 for (i = -2; n < nr_probe; i++) {
1199 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1200
1201 if (k < 0)
1202 break;
1203
1204 n += k;
1205
1206 if (user_only)
1207 break;
1208 }
1209
1210 return n;
1211}
1212
1213/**
1214 * early_platform_cleanup - clean up early platform code
1215 */
1216void __init early_platform_cleanup(void)
1217{
1218 struct platform_device *pd, *pd2;
1219
1220 /* clean up the devres list used to chain devices */
1221 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1222 dev.devres_head) {
1223 list_del(&pd->dev.devres_head);
1224 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1225 }
1226}
1227