blob: d2d4926c5c4c1dd3ac6110447fe325f6f789b01c [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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000131 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800132 * @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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000155 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800156 * @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 Delvare13595552007-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 Torokhov93ce3062005-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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000180 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181 * @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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000205 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800206 * @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 Torokhov93ce3062005-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 Torokhov93ce3062005-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 Torokhov93ce3062005-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 Torokhov93ce3062005-12-10 01:36:27 -0500314 release_resource(r);
315 }
Dmitry Torokhov93ce3062005-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 Torokhov93ce3062005-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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000347 * platform_device_register_simple - add a platform-level device and its resources
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800348 * @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/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000399 * platform_device_register_data - add a platform-level device with platform-specific data
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700400 * @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}
Michael Hennerich0787fdf2009-12-21 11:41:08 -0500444EXPORT_SYMBOL_GPL(platform_device_register_data);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700445
Russell King00d3dcd2005-11-09 17:23:39 +0000446static int platform_drv_probe(struct device *_dev)
447{
448 struct platform_driver *drv = to_platform_driver(_dev->driver);
449 struct platform_device *dev = to_platform_device(_dev);
450
451 return drv->probe(dev);
452}
453
David Brownellc67334f2006-11-16 23:28:47 -0800454static int platform_drv_probe_fail(struct device *_dev)
455{
456 return -ENXIO;
457}
458
Russell King00d3dcd2005-11-09 17:23:39 +0000459static int platform_drv_remove(struct device *_dev)
460{
461 struct platform_driver *drv = to_platform_driver(_dev->driver);
462 struct platform_device *dev = to_platform_device(_dev);
463
464 return drv->remove(dev);
465}
466
467static void platform_drv_shutdown(struct device *_dev)
468{
469 struct platform_driver *drv = to_platform_driver(_dev->driver);
470 struct platform_device *dev = to_platform_device(_dev);
471
472 drv->shutdown(dev);
473}
474
Russell King00d3dcd2005-11-09 17:23:39 +0000475/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000476 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800477 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000478 */
479int platform_driver_register(struct platform_driver *drv)
480{
481 drv->driver.bus = &platform_bus_type;
482 if (drv->probe)
483 drv->driver.probe = platform_drv_probe;
484 if (drv->remove)
485 drv->driver.remove = platform_drv_remove;
486 if (drv->shutdown)
487 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200488
Russell King00d3dcd2005-11-09 17:23:39 +0000489 return driver_register(&drv->driver);
490}
491EXPORT_SYMBOL_GPL(platform_driver_register);
492
493/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000494 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800495 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000496 */
497void platform_driver_unregister(struct platform_driver *drv)
498{
499 driver_unregister(&drv->driver);
500}
501EXPORT_SYMBOL_GPL(platform_driver_unregister);
502
David Brownellc67334f2006-11-16 23:28:47 -0800503/**
504 * platform_driver_probe - register driver for non-hotpluggable device
505 * @drv: platform driver structure
506 * @probe: the driver probe routine, probably from an __init section
507 *
508 * Use this instead of platform_driver_register() when you know the device
509 * is not hotpluggable and has already been registered, and you want to
510 * remove its run-once probe() infrastructure from memory after the driver
511 * has bound to the device.
512 *
513 * One typical use for this would be with drivers for controllers integrated
514 * into system-on-chip processors, where the controller devices have been
515 * configured as part of board setup.
516 *
517 * Returns zero if the driver registered and bound to a device, else returns
518 * a negative error code and with the driver not registered.
519 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800520int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800521 int (*probe)(struct platform_device *))
522{
523 int retval, code;
524
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700525 /* make sure driver won't have bind/unbind attributes */
526 drv->driver.suppress_bind_attrs = true;
527
David Brownellc67334f2006-11-16 23:28:47 -0800528 /* temporary section violation during probe() */
529 drv->probe = probe;
530 retval = code = platform_driver_register(drv);
531
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700532 /*
533 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800534 * the list of drivers in order to probe new devices. Check to see
535 * if the probe was successful, and make sure any forced probes of
536 * new devices fail.
537 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700538 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800539 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800540 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800541 retval = -ENODEV;
542 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700543 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800544
545 if (code != retval)
546 platform_driver_unregister(drv);
547 return retval;
548}
549EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800551/**
552 * platform_create_bundle - register driver and create corresponding device
553 * @driver: platform driver structure
554 * @probe: the driver probe routine, probably from an __init section
555 * @res: set of resources that needs to be allocated for the device
556 * @n_res: number of resources
557 * @data: platform specific data for this platform device
558 * @size: size of platform specific data
559 *
560 * Use this in legacy-style modules that probe hardware directly and
561 * register a single platform device and corresponding platform driver.
562 */
563struct platform_device * __init_or_module platform_create_bundle(
564 struct platform_driver *driver,
565 int (*probe)(struct platform_device *),
566 struct resource *res, unsigned int n_res,
567 const void *data, size_t size)
568{
569 struct platform_device *pdev;
570 int error;
571
572 pdev = platform_device_alloc(driver->driver.name, -1);
573 if (!pdev) {
574 error = -ENOMEM;
575 goto err_out;
576 }
577
578 if (res) {
579 error = platform_device_add_resources(pdev, res, n_res);
580 if (error)
581 goto err_pdev_put;
582 }
583
584 if (data) {
585 error = platform_device_add_data(pdev, data, size);
586 if (error)
587 goto err_pdev_put;
588 }
589
590 error = platform_device_add(pdev);
591 if (error)
592 goto err_pdev_put;
593
594 error = platform_driver_probe(driver, probe);
595 if (error)
596 goto err_pdev_del;
597
598 return pdev;
599
600err_pdev_del:
601 platform_device_del(pdev);
602err_pdev_put:
603 platform_device_put(pdev);
604err_out:
605 return ERR_PTR(error);
606}
607EXPORT_SYMBOL_GPL(platform_create_bundle);
608
David Brownella0245f72006-05-29 10:37:33 -0700609/* modalias support enables more hands-off userspace setup:
610 * (a) environment variable lets new-style hotplug events work once system is
611 * fully running: "modprobe $MODALIAS"
612 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
613 * mishandled before system is fully running: "modprobe $(cat modalias)"
614 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800615static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
616 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700617{
618 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200619 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700620
621 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
622}
623
624static struct device_attribute platform_dev_attrs[] = {
625 __ATTR_RO(modalias),
626 __ATTR_NULL,
627};
628
Kay Sievers7eff2e72007-08-14 15:15:12 +0200629static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700630{
631 struct platform_device *pdev = to_platform_device(dev);
632
Eric Miao57fee4a2009-02-04 11:52:40 +0800633 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
634 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700635 return 0;
636}
637
Eric Miao57fee4a2009-02-04 11:52:40 +0800638static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100639 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800640 struct platform_device *pdev)
641{
642 while (id->name[0]) {
643 if (strcmp(pdev->name, id->name) == 0) {
644 pdev->id_entry = id;
645 return id;
646 }
647 id++;
648 }
649 return NULL;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800653 * platform_match - bind platform device to platform driver.
654 * @dev: device.
655 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800657 * Platform device IDs are assumed to be encoded like this:
658 * "<name><instance>", where <name> is a short description of the type of
659 * device, like "pci" or "floppy", and <instance> is the enumerated
660 * instance of the device, like '0' or '42'. Driver IDs are simply
661 * "<name>". So, extract the <name> from the platform_device structure,
662 * and compare it against the name of the driver. Return whether they match
663 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800665static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800667 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800668 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Eric Miao57fee4a2009-02-04 11:52:40 +0800670 /* match against the id table first */
671 if (pdrv->id_table)
672 return platform_match_id(pdrv->id_table, pdev) != NULL;
673
674 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100675 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200678#ifdef CONFIG_PM_SLEEP
679
680static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200682 struct platform_driver *pdrv = to_platform_driver(dev->driver);
683 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int ret = 0;
685
Magnus Damm783ea7d2009-06-04 22:13:33 +0200686 if (dev->driver && pdrv->suspend)
687 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700688
689 return ret;
690}
691
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200692static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200694 struct platform_driver *pdrv = to_platform_driver(dev->driver);
695 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int ret = 0;
697
Magnus Damm783ea7d2009-06-04 22:13:33 +0200698 if (dev->driver && pdrv->resume)
699 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
703
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200704static int platform_pm_prepare(struct device *dev)
705{
706 struct device_driver *drv = dev->driver;
707 int ret = 0;
708
709 if (drv && drv->pm && drv->pm->prepare)
710 ret = drv->pm->prepare(dev);
711
712 return ret;
713}
714
715static void platform_pm_complete(struct device *dev)
716{
717 struct device_driver *drv = dev->driver;
718
719 if (drv && drv->pm && drv->pm->complete)
720 drv->pm->complete(dev);
721}
722
Magnus Damm9d730222009-08-20 20:25:32 +0200723#else /* !CONFIG_PM_SLEEP */
724
725#define platform_pm_prepare NULL
726#define platform_pm_complete NULL
727
728#endif /* !CONFIG_PM_SLEEP */
729
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200730#ifdef CONFIG_SUSPEND
731
732static int platform_pm_suspend(struct device *dev)
733{
734 struct device_driver *drv = dev->driver;
735 int ret = 0;
736
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200737 if (!drv)
738 return 0;
739
740 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200741 if (drv->pm->suspend)
742 ret = drv->pm->suspend(dev);
743 } else {
744 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
745 }
746
747 return ret;
748}
749
750static int platform_pm_suspend_noirq(struct device *dev)
751{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200752 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200753 int ret = 0;
754
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200755 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200756 return 0;
757
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200758 if (drv->pm) {
759 if (drv->pm->suspend_noirq)
760 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200761 }
762
763 return ret;
764}
765
766static int platform_pm_resume(struct device *dev)
767{
768 struct device_driver *drv = dev->driver;
769 int ret = 0;
770
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200771 if (!drv)
772 return 0;
773
774 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200775 if (drv->pm->resume)
776 ret = drv->pm->resume(dev);
777 } else {
778 ret = platform_legacy_resume(dev);
779 }
780
781 return ret;
782}
783
784static int platform_pm_resume_noirq(struct device *dev)
785{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200786 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200787 int ret = 0;
788
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200789 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200790 return 0;
791
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200792 if (drv->pm) {
793 if (drv->pm->resume_noirq)
794 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200795 }
796
797 return ret;
798}
799
800#else /* !CONFIG_SUSPEND */
801
802#define platform_pm_suspend NULL
803#define platform_pm_resume NULL
804#define platform_pm_suspend_noirq NULL
805#define platform_pm_resume_noirq NULL
806
807#endif /* !CONFIG_SUSPEND */
808
809#ifdef CONFIG_HIBERNATION
810
811static int platform_pm_freeze(struct device *dev)
812{
813 struct device_driver *drv = dev->driver;
814 int ret = 0;
815
816 if (!drv)
817 return 0;
818
819 if (drv->pm) {
820 if (drv->pm->freeze)
821 ret = drv->pm->freeze(dev);
822 } else {
823 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
824 }
825
826 return ret;
827}
828
829static int platform_pm_freeze_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->freeze_noirq)
839 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200840 }
841
842 return ret;
843}
844
845static int platform_pm_thaw(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->thaw)
855 ret = drv->pm->thaw(dev);
856 } else {
857 ret = platform_legacy_resume(dev);
858 }
859
860 return ret;
861}
862
863static int platform_pm_thaw_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->thaw_noirq)
873 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200874 }
875
876 return ret;
877}
878
879static int platform_pm_poweroff(struct device *dev)
880{
881 struct device_driver *drv = dev->driver;
882 int ret = 0;
883
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200884 if (!drv)
885 return 0;
886
887 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200888 if (drv->pm->poweroff)
889 ret = drv->pm->poweroff(dev);
890 } else {
891 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
892 }
893
894 return ret;
895}
896
897static int platform_pm_poweroff_noirq(struct device *dev)
898{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200899 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200900 int ret = 0;
901
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200902 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200903 return 0;
904
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200905 if (drv->pm) {
906 if (drv->pm->poweroff_noirq)
907 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200908 }
909
910 return ret;
911}
912
913static int platform_pm_restore(struct device *dev)
914{
915 struct device_driver *drv = dev->driver;
916 int ret = 0;
917
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200918 if (!drv)
919 return 0;
920
921 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200922 if (drv->pm->restore)
923 ret = drv->pm->restore(dev);
924 } else {
925 ret = platform_legacy_resume(dev);
926 }
927
928 return ret;
929}
930
931static int platform_pm_restore_noirq(struct device *dev)
932{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200933 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200934 int ret = 0;
935
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200936 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200937 return 0;
938
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200939 if (drv->pm) {
940 if (drv->pm->restore_noirq)
941 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200942 }
943
944 return ret;
945}
946
947#else /* !CONFIG_HIBERNATION */
948
949#define platform_pm_freeze NULL
950#define platform_pm_thaw NULL
951#define platform_pm_poweroff NULL
952#define platform_pm_restore NULL
953#define platform_pm_freeze_noirq NULL
954#define platform_pm_thaw_noirq NULL
955#define platform_pm_poweroff_noirq NULL
956#define platform_pm_restore_noirq NULL
957
958#endif /* !CONFIG_HIBERNATION */
959
Magnus Damm9d730222009-08-20 20:25:32 +0200960#ifdef CONFIG_PM_RUNTIME
961
962int __weak platform_pm_runtime_suspend(struct device *dev)
963{
964 return -ENOSYS;
965};
966
967int __weak platform_pm_runtime_resume(struct device *dev)
968{
969 return -ENOSYS;
970};
971
972int __weak platform_pm_runtime_idle(struct device *dev)
973{
974 return -ENOSYS;
975};
976
977#else /* !CONFIG_PM_RUNTIME */
978
979#define platform_pm_runtime_suspend NULL
980#define platform_pm_runtime_resume NULL
981#define platform_pm_runtime_idle NULL
982
983#endif /* !CONFIG_PM_RUNTIME */
984
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200985static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200986 .prepare = platform_pm_prepare,
987 .complete = platform_pm_complete,
988 .suspend = platform_pm_suspend,
989 .resume = platform_pm_resume,
990 .freeze = platform_pm_freeze,
991 .thaw = platform_pm_thaw,
992 .poweroff = platform_pm_poweroff,
993 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200994 .suspend_noirq = platform_pm_suspend_noirq,
995 .resume_noirq = platform_pm_resume_noirq,
996 .freeze_noirq = platform_pm_freeze_noirq,
997 .thaw_noirq = platform_pm_thaw_noirq,
998 .poweroff_noirq = platform_pm_poweroff_noirq,
999 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +02001000 .runtime_suspend = platform_pm_runtime_suspend,
1001 .runtime_resume = platform_pm_runtime_resume,
1002 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001003};
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005struct bus_type platform_bus_type = {
1006 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -07001007 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001009 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +02001010 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001012EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014int __init platform_bus_init(void)
1015{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001016 int error;
1017
Magnus Damm13977092009-03-30 14:37:25 -07001018 early_platform_cleanup();
1019
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001020 error = device_register(&platform_bus);
1021 if (error)
1022 return error;
1023 error = bus_register(&platform_bus_type);
1024 if (error)
1025 device_unregister(&platform_bus);
1026 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
1030u64 dma_get_required_mask(struct device *dev)
1031{
1032 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1033 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1034 u64 mask;
1035
1036 if (!high_totalram) {
1037 /* convert to mask just covering totalram */
1038 low_totalram = (1 << (fls(low_totalram) - 1));
1039 low_totalram += low_totalram - 1;
1040 mask = low_totalram;
1041 } else {
1042 high_totalram = (1 << (fls(high_totalram) - 1));
1043 high_totalram += high_totalram - 1;
1044 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1045 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001046 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048EXPORT_SYMBOL_GPL(dma_get_required_mask);
1049#endif
Magnus Damm13977092009-03-30 14:37:25 -07001050
1051static __initdata LIST_HEAD(early_platform_driver_list);
1052static __initdata LIST_HEAD(early_platform_device_list);
1053
1054/**
1055 * early_platform_driver_register
Randy Dunlapd86c1302009-04-21 07:22:53 -07001056 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001057 * @buf: string passed from early_param()
1058 */
1059int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1060 char *buf)
1061{
Magnus Dammc60e0502009-11-27 17:38:51 +09001062 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001063 int n;
1064
1065 /* Simply add the driver to the end of the global list.
1066 * Drivers will by default be put on the list in compiled-in order.
1067 */
1068 if (!epdrv->list.next) {
1069 INIT_LIST_HEAD(&epdrv->list);
1070 list_add_tail(&epdrv->list, &early_platform_driver_list);
1071 }
1072
1073 /* If the user has specified device then make sure the driver
1074 * gets prioritized. The driver of the last device specified on
1075 * command line will be put first on the list.
1076 */
1077 n = strlen(epdrv->pdrv->driver.name);
1078 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1079 list_move(&epdrv->list, &early_platform_driver_list);
1080
Magnus Dammc60e0502009-11-27 17:38:51 +09001081 /* Allow passing parameters after device name */
1082 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001083 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001084 else {
1085 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1086 &tmp, 10);
1087
1088 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1089 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1090 n = 0;
1091 } else
1092 n += strcspn(&buf[n + 1], ",") + 1;
1093 }
1094
1095 if (buf[n] == ',')
1096 n++;
1097
1098 if (epdrv->bufsize) {
1099 memcpy(epdrv->buffer, &buf[n],
1100 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1101 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1102 }
Magnus Damm13977092009-03-30 14:37:25 -07001103 }
1104
1105 return 0;
1106}
1107
1108/**
1109 * early_platform_add_devices - add a numbers of early platform devices
1110 * @devs: array of early platform devices to add
1111 * @num: number of early platform devices in array
1112 */
1113void __init early_platform_add_devices(struct platform_device **devs, int num)
1114{
1115 struct device *dev;
1116 int i;
1117
1118 /* simply add the devices to list */
1119 for (i = 0; i < num; i++) {
1120 dev = &devs[i]->dev;
1121
1122 if (!dev->devres_head.next) {
1123 INIT_LIST_HEAD(&dev->devres_head);
1124 list_add_tail(&dev->devres_head,
1125 &early_platform_device_list);
1126 }
1127 }
1128}
1129
1130/**
1131 * early_platform_driver_register_all
1132 * @class_str: string to identify early platform driver class
1133 */
1134void __init early_platform_driver_register_all(char *class_str)
1135{
1136 /* The "class_str" parameter may or may not be present on the kernel
1137 * command line. If it is present then there may be more than one
1138 * matching parameter.
1139 *
1140 * Since we register our early platform drivers using early_param()
1141 * we need to make sure that they also get registered in the case
1142 * when the parameter is missing from the kernel command line.
1143 *
1144 * We use parse_early_options() to make sure the early_param() gets
1145 * called at least once. The early_param() may be called more than
1146 * once since the name of the preferred device may be specified on
1147 * the kernel command line. early_platform_driver_register() handles
1148 * this case for us.
1149 */
1150 parse_early_options(class_str);
1151}
1152
1153/**
1154 * early_platform_match
Randy Dunlapd86c1302009-04-21 07:22:53 -07001155 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001156 * @id: id to match against
1157 */
1158static __init struct platform_device *
1159early_platform_match(struct early_platform_driver *epdrv, int id)
1160{
1161 struct platform_device *pd;
1162
1163 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1164 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1165 if (pd->id == id)
1166 return pd;
1167
1168 return NULL;
1169}
1170
1171/**
1172 * early_platform_left
Randy Dunlapd86c1302009-04-21 07:22:53 -07001173 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001174 * @id: return true if id or above exists
1175 */
1176static __init int early_platform_left(struct early_platform_driver *epdrv,
1177 int id)
1178{
1179 struct platform_device *pd;
1180
1181 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1182 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1183 if (pd->id >= id)
1184 return 1;
1185
1186 return 0;
1187}
1188
1189/**
1190 * early_platform_driver_probe_id
1191 * @class_str: string to identify early platform driver class
1192 * @id: id to match against
1193 * @nr_probe: number of platform devices to successfully probe before exiting
1194 */
1195static int __init early_platform_driver_probe_id(char *class_str,
1196 int id,
1197 int nr_probe)
1198{
1199 struct early_platform_driver *epdrv;
1200 struct platform_device *match;
1201 int match_id;
1202 int n = 0;
1203 int left = 0;
1204
1205 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1206 /* only use drivers matching our class_str */
1207 if (strcmp(class_str, epdrv->class_str))
1208 continue;
1209
1210 if (id == -2) {
1211 match_id = epdrv->requested_id;
1212 left = 1;
1213
1214 } else {
1215 match_id = id;
1216 left += early_platform_left(epdrv, id);
1217
1218 /* skip requested id */
1219 switch (epdrv->requested_id) {
1220 case EARLY_PLATFORM_ID_ERROR:
1221 case EARLY_PLATFORM_ID_UNSET:
1222 break;
1223 default:
1224 if (epdrv->requested_id == id)
1225 match_id = EARLY_PLATFORM_ID_UNSET;
1226 }
1227 }
1228
1229 switch (match_id) {
1230 case EARLY_PLATFORM_ID_ERROR:
1231 pr_warning("%s: unable to parse %s parameter\n",
1232 class_str, epdrv->pdrv->driver.name);
1233 /* fall-through */
1234 case EARLY_PLATFORM_ID_UNSET:
1235 match = NULL;
1236 break;
1237 default:
1238 match = early_platform_match(epdrv, match_id);
1239 }
1240
1241 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001242 /*
1243 * Set up a sensible init_name to enable
1244 * dev_name() and others to be used before the
1245 * rest of the driver core is initialized.
1246 */
1247 if (!match->dev.init_name) {
1248 char buf[32];
1249
1250 if (match->id != -1)
1251 snprintf(buf, sizeof(buf), "%s.%d",
1252 match->name, match->id);
1253 else
1254 snprintf(buf, sizeof(buf), "%s",
1255 match->name);
1256
1257 match->dev.init_name = kstrdup(buf, GFP_KERNEL);
1258 if (!match->dev.init_name)
1259 return -ENOMEM;
1260 }
Magnus Damm13977092009-03-30 14:37:25 -07001261 if (epdrv->pdrv->probe(match))
1262 pr_warning("%s: unable to probe %s early.\n",
1263 class_str, match->name);
1264 else
1265 n++;
1266 }
1267
1268 if (n >= nr_probe)
1269 break;
1270 }
1271
1272 if (left)
1273 return n;
1274 else
1275 return -ENODEV;
1276}
1277
1278/**
1279 * early_platform_driver_probe
1280 * @class_str: string to identify early platform driver class
1281 * @nr_probe: number of platform devices to successfully probe before exiting
1282 * @user_only: only probe user specified early platform devices
1283 */
1284int __init early_platform_driver_probe(char *class_str,
1285 int nr_probe,
1286 int user_only)
1287{
1288 int k, n, i;
1289
1290 n = 0;
1291 for (i = -2; n < nr_probe; i++) {
1292 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1293
1294 if (k < 0)
1295 break;
1296
1297 n += k;
1298
1299 if (user_only)
1300 break;
1301 }
1302
1303 return n;
1304}
1305
1306/**
1307 * early_platform_cleanup - clean up early platform code
1308 */
1309void __init early_platform_cleanup(void)
1310{
1311 struct platform_device *pd, *pd2;
1312
1313 /* clean up the devres list used to chain devices */
1314 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1315 dev.devres_head) {
1316 list_del(&pd->dev.devres_head);
1317 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1318 }
1319}
1320