blob: f699fabf403bedd931fd457a2c200faef58ad7ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * platform.c - platform 'pseudo' bus for legacy devices
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 * Please see Documentation/driver-model/platform.txt for more
10 * information.
11 */
12
Andrew Mortondaa41222009-08-06 16:00:44 -070013#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010014#include <linux/platform_device.h>
Grant Likely05212152010-06-08 07:48:20 -060015#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
19#include <linux/bootmem.h>
20#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020022#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010024#include "base.h"
25
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080026#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
27 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010030 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050032EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080035 * platform_get_resource - get a resource for a device
36 * @dev: platform device
37 * @type: resource type
38 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080040struct resource *platform_get_resource(struct platform_device *dev,
41 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 int i;
44
45 for (i = 0; i < dev->num_resources; i++) {
46 struct resource *r = &dev->resource[i];
47
Magnus Dammc9f66162008-10-15 22:05:15 -070048 if (type == resource_type(r) && num-- == 0)
49 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51 return NULL;
52}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050053EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080056 * platform_get_irq - get an IRQ for a device
57 * @dev: platform device
58 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60int platform_get_irq(struct platform_device *dev, unsigned int num)
61{
62 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
63
David Vrabel305b3222006-01-19 17:52:27 +000064 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050066EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080069 * platform_get_resource_byname - get a resource for a device by name
70 * @dev: platform device
71 * @type: resource type
72 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080074struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +020075 unsigned int type,
76 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int i;
79
80 for (i = 0; i < dev->num_resources; i++) {
81 struct resource *r = &dev->resource[i];
82
Magnus Dammc9f66162008-10-15 22:05:15 -070083 if (type == resource_type(r) && !strcmp(r->name, name))
84 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 return NULL;
87}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050088EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080091 * platform_get_irq - get an IRQ for a device
92 * @dev: platform device
93 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +020095int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080097 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
98 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Vrabel305b3222006-01-19 17:52:27 +0000100 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500102EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800105 * platform_add_devices - add a numbers of platform devices
106 * @devs: array of platform devices to add
107 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
109int platform_add_devices(struct platform_device **devs, int num)
110{
111 int i, ret = 0;
112
113 for (i = 0; i < num; i++) {
114 ret = platform_device_register(devs[i]);
115 if (ret) {
116 while (--i >= 0)
117 platform_device_unregister(devs[i]);
118 break;
119 }
120 }
121
122 return ret;
123}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500124EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Russell King37c12e72005-11-05 21:19:33 +0000126struct platform_object {
127 struct platform_device pdev;
128 char name[1];
129};
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000132 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800133 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000134 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800135 * Free all memory associated with a platform device. This function must
136 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000137 */
138void platform_device_put(struct platform_device *pdev)
139{
140 if (pdev)
141 put_device(&pdev->dev);
142}
143EXPORT_SYMBOL_GPL(platform_device_put);
144
145static void platform_device_release(struct device *dev)
146{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800147 struct platform_object *pa = container_of(dev, struct platform_object,
148 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000149
150 kfree(pa->pdev.dev.platform_data);
151 kfree(pa->pdev.resource);
152 kfree(pa);
153}
154
155/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000156 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800157 * @name: base name of the device we're adding
158 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000159 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800160 * Create a platform device object which can have other objects attached
161 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000162 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200163struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000164{
165 struct platform_object *pa;
166
167 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
168 if (pa) {
169 strcpy(pa->name, name);
170 pa->pdev.name = pa->name;
171 pa->pdev.id = id;
172 device_initialize(&pa->pdev.dev);
173 pa->pdev.dev.release = platform_device_release;
174 }
175
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500176 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000177}
178EXPORT_SYMBOL_GPL(platform_device_alloc);
179
180/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000181 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800182 * @pdev: platform device allocated by platform_device_alloc to add resources to
183 * @res: set of resources that needs to be allocated for the device
184 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000185 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800186 * Add a copy of the resources to the platform device. The memory
187 * associated with the resources will be freed when the platform device is
188 * released.
Russell King37c12e72005-11-05 21:19:33 +0000189 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800190int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100191 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000192{
193 struct resource *r;
194
195 r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
196 if (r) {
197 memcpy(r, res, sizeof(struct resource) * num);
198 pdev->resource = r;
199 pdev->num_resources = num;
200 }
201 return r ? 0 : -ENOMEM;
202}
203EXPORT_SYMBOL_GPL(platform_device_add_resources);
204
205/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000206 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800207 * @pdev: platform device allocated by platform_device_alloc to add resources to
208 * @data: platform specific data for this platform device
209 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000210 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800211 * Add a copy of platform specific data to the platform device's
212 * platform_data pointer. The memory associated with the platform data
213 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000214 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800215int platform_device_add_data(struct platform_device *pdev, const void *data,
216 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000217{
Andrew Mortondaa41222009-08-06 16:00:44 -0700218 void *d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000219
Russell King37c12e72005-11-05 21:19:33 +0000220 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000221 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700222 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000223 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700224 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000225}
226EXPORT_SYMBOL_GPL(platform_device_add_data);
227
228/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800229 * platform_device_add - add a platform device to device hierarchy
230 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800232 * This is part 2 of platform_device_register(), though may be called
233 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Russell King37c12e72005-11-05 21:19:33 +0000235int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i, ret = 0;
238
239 if (!pdev)
240 return -EINVAL;
241
242 if (!pdev->dev.parent)
243 pdev->dev.parent = &platform_bus;
244
245 pdev->dev.bus = &platform_bus_type;
246
247 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100248 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700250 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 for (i = 0; i < pdev->num_resources; i++) {
253 struct resource *p, *r = &pdev->resource[i];
254
255 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100256 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 p = r->parent;
259 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700260 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700262 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 p = &ioport_resource;
264 }
265
Kumar Galad960bb42005-11-28 10:15:39 -0600266 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ERR
268 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100269 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ret = -EBUSY;
271 goto failed;
272 }
273 }
274
275 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100276 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Russell Kinge3915532006-05-06 08:15:26 +0100278 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ret == 0)
280 return ret;
281
282 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700283 while (--i >= 0) {
284 struct resource *r = &pdev->resource[i];
285 unsigned long type = resource_type(r);
286
287 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
288 release_resource(r);
289 }
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292}
Russell King37c12e72005-11-05 21:19:33 +0000293EXPORT_SYMBOL_GPL(platform_device_add);
294
295/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800296 * platform_device_del - remove a platform-level device
297 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500298 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800299 * Note that this function will also release all memory- and port-based
300 * resources owned by the device (@dev->resource). This function must
301 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500302 */
303void platform_device_del(struct platform_device *pdev)
304{
305 int i;
306
307 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200308 device_del(&pdev->dev);
309
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500310 for (i = 0; i < pdev->num_resources; i++) {
311 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700312 unsigned long type = resource_type(r);
313
314 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500315 release_resource(r);
316 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500317 }
318}
319EXPORT_SYMBOL_GPL(platform_device_del);
320
321/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800322 * platform_device_register - add a platform-level device
323 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000324 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800325int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000326{
327 device_initialize(&pdev->dev);
328 return platform_device_add(pdev);
329}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500330EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800333 * platform_device_unregister - unregister a platform-level device
334 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800336 * Unregistration is done in 2 steps. First we release all resources
337 * and remove it from the subsystem, then we drop reference count by
338 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800340void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500342 platform_device_del(pdev);
343 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500345EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000348 * platform_device_register_simple - add a platform-level device and its resources
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800349 * @name: base name of the device we're adding
350 * @id: instance id
351 * @res: set of resources that needs to be allocated for the device
352 * @num: number of resources
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800354 * This function creates a simple platform device that requires minimal
355 * resource and memory management. Canned release function freeing memory
356 * allocated for the device allows drivers using such devices to be
357 * unloaded without waiting for the last reference to the device to be
358 * dropped.
David Brownell49a4ec12007-05-08 00:29:39 -0700359 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800360 * This interface is primarily intended for use with legacy drivers which
361 * probe hardware directly. Because such drivers create sysfs device nodes
362 * themselves, rather than letting system infrastructure handle such device
363 * enumeration tasks, they don't fully conform to the Linux driver model.
364 * In particular, when such drivers are built as modules, they can't be
365 * "hotplugged".
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200366 *
367 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800369struct platform_device *platform_device_register_simple(const char *name,
370 int id,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100371 const struct resource *res,
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800372 unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Russell King37c12e72005-11-05 21:19:33 +0000374 struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int retval;
376
Russell King37c12e72005-11-05 21:19:33 +0000377 pdev = platform_device_alloc(name, id);
378 if (!pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 retval = -ENOMEM;
380 goto error;
381 }
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (num) {
Russell King37c12e72005-11-05 21:19:33 +0000384 retval = platform_device_add_resources(pdev, res, num);
385 if (retval)
386 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Russell King37c12e72005-11-05 21:19:33 +0000389 retval = platform_device_add(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (retval)
391 goto error;
392
Russell King37c12e72005-11-05 21:19:33 +0000393 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395error:
Russell King37c12e72005-11-05 21:19:33 +0000396 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return ERR_PTR(retval);
398}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500399EXPORT_SYMBOL_GPL(platform_device_register_simple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700401/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000402 * platform_device_register_data - add a platform-level device with platform-specific data
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700403 * @parent: parent device for the device we're adding
404 * @name: base name of the device we're adding
405 * @id: instance id
406 * @data: platform specific data for this platform device
407 * @size: size of platform specific data
408 *
409 * This function creates a simple platform device that requires minimal
410 * resource and memory management. Canned release function freeing memory
411 * allocated for the device allows drivers using such devices to be
412 * unloaded without waiting for the last reference to the device to be
413 * dropped.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200414 *
415 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700416 */
417struct platform_device *platform_device_register_data(
418 struct device *parent,
419 const char *name, int id,
420 const void *data, size_t size)
421{
422 struct platform_device *pdev;
423 int retval;
424
425 pdev = platform_device_alloc(name, id);
426 if (!pdev) {
427 retval = -ENOMEM;
428 goto error;
429 }
430
431 pdev->dev.parent = parent;
432
433 if (size) {
434 retval = platform_device_add_data(pdev, data, size);
435 if (retval)
436 goto error;
437 }
438
439 retval = platform_device_add(pdev);
440 if (retval)
441 goto error;
442
443 return pdev;
444
445error:
446 platform_device_put(pdev);
447 return ERR_PTR(retval);
448}
Michael Hennerich0787fdf2009-12-21 11:41:08 -0500449EXPORT_SYMBOL_GPL(platform_device_register_data);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700450
Russell King00d3dcd2005-11-09 17:23:39 +0000451static int platform_drv_probe(struct device *_dev)
452{
453 struct platform_driver *drv = to_platform_driver(_dev->driver);
454 struct platform_device *dev = to_platform_device(_dev);
455
456 return drv->probe(dev);
457}
458
David Brownellc67334f2006-11-16 23:28:47 -0800459static int platform_drv_probe_fail(struct device *_dev)
460{
461 return -ENXIO;
462}
463
Russell King00d3dcd2005-11-09 17:23:39 +0000464static int platform_drv_remove(struct device *_dev)
465{
466 struct platform_driver *drv = to_platform_driver(_dev->driver);
467 struct platform_device *dev = to_platform_device(_dev);
468
469 return drv->remove(dev);
470}
471
472static void platform_drv_shutdown(struct device *_dev)
473{
474 struct platform_driver *drv = to_platform_driver(_dev->driver);
475 struct platform_device *dev = to_platform_device(_dev);
476
477 drv->shutdown(dev);
478}
479
Russell King00d3dcd2005-11-09 17:23:39 +0000480/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000481 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800482 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000483 */
484int platform_driver_register(struct platform_driver *drv)
485{
486 drv->driver.bus = &platform_bus_type;
487 if (drv->probe)
488 drv->driver.probe = platform_drv_probe;
489 if (drv->remove)
490 drv->driver.remove = platform_drv_remove;
491 if (drv->shutdown)
492 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200493
Russell King00d3dcd2005-11-09 17:23:39 +0000494 return driver_register(&drv->driver);
495}
496EXPORT_SYMBOL_GPL(platform_driver_register);
497
498/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000499 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800500 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000501 */
502void platform_driver_unregister(struct platform_driver *drv)
503{
504 driver_unregister(&drv->driver);
505}
506EXPORT_SYMBOL_GPL(platform_driver_unregister);
507
David Brownellc67334f2006-11-16 23:28:47 -0800508/**
509 * platform_driver_probe - register driver for non-hotpluggable device
510 * @drv: platform driver structure
511 * @probe: the driver probe routine, probably from an __init section
512 *
513 * Use this instead of platform_driver_register() when you know the device
514 * is not hotpluggable and has already been registered, and you want to
515 * remove its run-once probe() infrastructure from memory after the driver
516 * has bound to the device.
517 *
518 * One typical use for this would be with drivers for controllers integrated
519 * into system-on-chip processors, where the controller devices have been
520 * configured as part of board setup.
521 *
522 * Returns zero if the driver registered and bound to a device, else returns
523 * a negative error code and with the driver not registered.
524 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800525int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800526 int (*probe)(struct platform_device *))
527{
528 int retval, code;
529
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700530 /* make sure driver won't have bind/unbind attributes */
531 drv->driver.suppress_bind_attrs = true;
532
David Brownellc67334f2006-11-16 23:28:47 -0800533 /* temporary section violation during probe() */
534 drv->probe = probe;
535 retval = code = platform_driver_register(drv);
536
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700537 /*
538 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800539 * the list of drivers in order to probe new devices. Check to see
540 * if the probe was successful, and make sure any forced probes of
541 * new devices fail.
542 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700543 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800544 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800545 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800546 retval = -ENODEV;
547 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700548 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800549
550 if (code != retval)
551 platform_driver_unregister(drv);
552 return retval;
553}
554EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800556/**
557 * platform_create_bundle - register driver and create corresponding device
558 * @driver: platform driver structure
559 * @probe: the driver probe routine, probably from an __init section
560 * @res: set of resources that needs to be allocated for the device
561 * @n_res: number of resources
562 * @data: platform specific data for this platform device
563 * @size: size of platform specific data
564 *
565 * Use this in legacy-style modules that probe hardware directly and
566 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200567 *
568 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800569 */
570struct platform_device * __init_or_module platform_create_bundle(
571 struct platform_driver *driver,
572 int (*probe)(struct platform_device *),
573 struct resource *res, unsigned int n_res,
574 const void *data, size_t size)
575{
576 struct platform_device *pdev;
577 int error;
578
579 pdev = platform_device_alloc(driver->driver.name, -1);
580 if (!pdev) {
581 error = -ENOMEM;
582 goto err_out;
583 }
584
585 if (res) {
586 error = platform_device_add_resources(pdev, res, n_res);
587 if (error)
588 goto err_pdev_put;
589 }
590
591 if (data) {
592 error = platform_device_add_data(pdev, data, size);
593 if (error)
594 goto err_pdev_put;
595 }
596
597 error = platform_device_add(pdev);
598 if (error)
599 goto err_pdev_put;
600
601 error = platform_driver_probe(driver, probe);
602 if (error)
603 goto err_pdev_del;
604
605 return pdev;
606
607err_pdev_del:
608 platform_device_del(pdev);
609err_pdev_put:
610 platform_device_put(pdev);
611err_out:
612 return ERR_PTR(error);
613}
614EXPORT_SYMBOL_GPL(platform_create_bundle);
615
David Brownella0245f72006-05-29 10:37:33 -0700616/* modalias support enables more hands-off userspace setup:
617 * (a) environment variable lets new-style hotplug events work once system is
618 * fully running: "modprobe $MODALIAS"
619 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
620 * mishandled before system is fully running: "modprobe $(cat modalias)"
621 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800622static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
623 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700624{
625 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200626 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700627
628 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
629}
630
631static struct device_attribute platform_dev_attrs[] = {
632 __ATTR_RO(modalias),
633 __ATTR_NULL,
634};
635
Kay Sievers7eff2e72007-08-14 15:15:12 +0200636static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700637{
638 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600639 int rc;
640
641 /* Some devices have extra OF data and an OF-style MODALIAS */
642 rc = of_device_uevent(dev,env);
643 if (rc != -ENODEV)
644 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700645
Eric Miao57fee4a2009-02-04 11:52:40 +0800646 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
647 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700648 return 0;
649}
650
Eric Miao57fee4a2009-02-04 11:52:40 +0800651static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100652 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800653 struct platform_device *pdev)
654{
655 while (id->name[0]) {
656 if (strcmp(pdev->name, id->name) == 0) {
657 pdev->id_entry = id;
658 return id;
659 }
660 id++;
661 }
662 return NULL;
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800666 * platform_match - bind platform device to platform driver.
667 * @dev: device.
668 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800670 * Platform device IDs are assumed to be encoded like this:
671 * "<name><instance>", where <name> is a short description of the type of
672 * device, like "pci" or "floppy", and <instance> is the enumerated
673 * instance of the device, like '0' or '42'. Driver IDs are simply
674 * "<name>". So, extract the <name> from the platform_device structure,
675 * and compare it against the name of the driver. Return whether they match
676 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800678static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800680 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800681 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Grant Likely05212152010-06-08 07:48:20 -0600683 /* Attempt an OF style match first */
684 if (of_driver_match_device(dev, drv))
685 return 1;
686
687 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800688 if (pdrv->id_table)
689 return platform_match_id(pdrv->id_table, pdev) != NULL;
690
691 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100692 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200695#ifdef CONFIG_PM_SLEEP
696
697static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200699 struct platform_driver *pdrv = to_platform_driver(dev->driver);
700 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int ret = 0;
702
Magnus Damm783ea7d2009-06-04 22:13:33 +0200703 if (dev->driver && pdrv->suspend)
704 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700705
706 return ret;
707}
708
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200709static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200711 struct platform_driver *pdrv = to_platform_driver(dev->driver);
712 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int ret = 0;
714
Magnus Damm783ea7d2009-06-04 22:13:33 +0200715 if (dev->driver && pdrv->resume)
716 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ret;
719}
720
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200721static int platform_pm_prepare(struct device *dev)
722{
723 struct device_driver *drv = dev->driver;
724 int ret = 0;
725
726 if (drv && drv->pm && drv->pm->prepare)
727 ret = drv->pm->prepare(dev);
728
729 return ret;
730}
731
732static void platform_pm_complete(struct device *dev)
733{
734 struct device_driver *drv = dev->driver;
735
736 if (drv && drv->pm && drv->pm->complete)
737 drv->pm->complete(dev);
738}
739
Magnus Damm9d730222009-08-20 20:25:32 +0200740#else /* !CONFIG_PM_SLEEP */
741
742#define platform_pm_prepare NULL
743#define platform_pm_complete NULL
744
745#endif /* !CONFIG_PM_SLEEP */
746
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200747#ifdef CONFIG_SUSPEND
748
Kevin Hilman190e8372010-03-17 16:18:15 -0700749int __weak platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200750{
751 struct device_driver *drv = dev->driver;
752 int ret = 0;
753
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200754 if (!drv)
755 return 0;
756
757 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200758 if (drv->pm->suspend)
759 ret = drv->pm->suspend(dev);
760 } else {
761 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
762 }
763
764 return ret;
765}
766
Kevin Hilman190e8372010-03-17 16:18:15 -0700767int __weak platform_pm_suspend_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200768{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200769 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200770 int ret = 0;
771
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200772 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200773 return 0;
774
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200775 if (drv->pm) {
776 if (drv->pm->suspend_noirq)
777 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200778 }
779
780 return ret;
781}
782
Kevin Hilman190e8372010-03-17 16:18:15 -0700783int __weak platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200784{
785 struct device_driver *drv = dev->driver;
786 int ret = 0;
787
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200788 if (!drv)
789 return 0;
790
791 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200792 if (drv->pm->resume)
793 ret = drv->pm->resume(dev);
794 } else {
795 ret = platform_legacy_resume(dev);
796 }
797
798 return ret;
799}
800
Kevin Hilman190e8372010-03-17 16:18:15 -0700801int __weak platform_pm_resume_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200802{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200803 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200804 int ret = 0;
805
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200806 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200807 return 0;
808
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200809 if (drv->pm) {
810 if (drv->pm->resume_noirq)
811 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200812 }
813
814 return ret;
815}
816
817#else /* !CONFIG_SUSPEND */
818
819#define platform_pm_suspend NULL
820#define platform_pm_resume NULL
821#define platform_pm_suspend_noirq NULL
822#define platform_pm_resume_noirq NULL
823
824#endif /* !CONFIG_SUSPEND */
825
826#ifdef CONFIG_HIBERNATION
827
828static int platform_pm_freeze(struct device *dev)
829{
830 struct device_driver *drv = dev->driver;
831 int ret = 0;
832
833 if (!drv)
834 return 0;
835
836 if (drv->pm) {
837 if (drv->pm->freeze)
838 ret = drv->pm->freeze(dev);
839 } else {
840 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
841 }
842
843 return ret;
844}
845
846static int platform_pm_freeze_noirq(struct device *dev)
847{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200848 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200849 int ret = 0;
850
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200851 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200852 return 0;
853
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200854 if (drv->pm) {
855 if (drv->pm->freeze_noirq)
856 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200857 }
858
859 return ret;
860}
861
862static int platform_pm_thaw(struct device *dev)
863{
864 struct device_driver *drv = dev->driver;
865 int ret = 0;
866
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200867 if (!drv)
868 return 0;
869
870 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200871 if (drv->pm->thaw)
872 ret = drv->pm->thaw(dev);
873 } else {
874 ret = platform_legacy_resume(dev);
875 }
876
877 return ret;
878}
879
880static int platform_pm_thaw_noirq(struct device *dev)
881{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200882 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200883 int ret = 0;
884
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200885 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200886 return 0;
887
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200888 if (drv->pm) {
889 if (drv->pm->thaw_noirq)
890 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200891 }
892
893 return ret;
894}
895
896static int platform_pm_poweroff(struct device *dev)
897{
898 struct device_driver *drv = dev->driver;
899 int ret = 0;
900
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200901 if (!drv)
902 return 0;
903
904 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200905 if (drv->pm->poweroff)
906 ret = drv->pm->poweroff(dev);
907 } else {
908 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
909 }
910
911 return ret;
912}
913
914static int platform_pm_poweroff_noirq(struct device *dev)
915{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200916 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200917 int ret = 0;
918
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200919 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200920 return 0;
921
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200922 if (drv->pm) {
923 if (drv->pm->poweroff_noirq)
924 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200925 }
926
927 return ret;
928}
929
930static int platform_pm_restore(struct device *dev)
931{
932 struct device_driver *drv = dev->driver;
933 int ret = 0;
934
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200935 if (!drv)
936 return 0;
937
938 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200939 if (drv->pm->restore)
940 ret = drv->pm->restore(dev);
941 } else {
942 ret = platform_legacy_resume(dev);
943 }
944
945 return ret;
946}
947
948static int platform_pm_restore_noirq(struct device *dev)
949{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200950 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200951 int ret = 0;
952
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200953 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200954 return 0;
955
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200956 if (drv->pm) {
957 if (drv->pm->restore_noirq)
958 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200959 }
960
961 return ret;
962}
963
964#else /* !CONFIG_HIBERNATION */
965
966#define platform_pm_freeze NULL
967#define platform_pm_thaw NULL
968#define platform_pm_poweroff NULL
969#define platform_pm_restore NULL
970#define platform_pm_freeze_noirq NULL
971#define platform_pm_thaw_noirq NULL
972#define platform_pm_poweroff_noirq NULL
973#define platform_pm_restore_noirq NULL
974
975#endif /* !CONFIG_HIBERNATION */
976
Magnus Damm9d730222009-08-20 20:25:32 +0200977#ifdef CONFIG_PM_RUNTIME
978
979int __weak platform_pm_runtime_suspend(struct device *dev)
980{
Mark Brown543f25032010-05-10 23:10:13 +0200981 return pm_generic_runtime_suspend(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200982};
983
984int __weak platform_pm_runtime_resume(struct device *dev)
985{
Mark Brown543f25032010-05-10 23:10:13 +0200986 return pm_generic_runtime_resume(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200987};
988
989int __weak platform_pm_runtime_idle(struct device *dev)
990{
Mark Brown543f25032010-05-10 23:10:13 +0200991 return pm_generic_runtime_idle(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200992};
993
994#else /* !CONFIG_PM_RUNTIME */
995
996#define platform_pm_runtime_suspend NULL
997#define platform_pm_runtime_resume NULL
998#define platform_pm_runtime_idle NULL
999
1000#endif /* !CONFIG_PM_RUNTIME */
1001
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001002static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001003 .prepare = platform_pm_prepare,
1004 .complete = platform_pm_complete,
1005 .suspend = platform_pm_suspend,
1006 .resume = platform_pm_resume,
1007 .freeze = platform_pm_freeze,
1008 .thaw = platform_pm_thaw,
1009 .poweroff = platform_pm_poweroff,
1010 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001011 .suspend_noirq = platform_pm_suspend_noirq,
1012 .resume_noirq = platform_pm_resume_noirq,
1013 .freeze_noirq = platform_pm_freeze_noirq,
1014 .thaw_noirq = platform_pm_thaw_noirq,
1015 .poweroff_noirq = platform_pm_poweroff_noirq,
1016 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +02001017 .runtime_suspend = platform_pm_runtime_suspend,
1018 .runtime_resume = platform_pm_runtime_resume,
1019 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001020};
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022struct bus_type platform_bus_type = {
1023 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -07001024 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001026 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +02001027 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001029EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031int __init platform_bus_init(void)
1032{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001033 int error;
1034
Magnus Damm13977092009-03-30 14:37:25 -07001035 early_platform_cleanup();
1036
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001037 error = device_register(&platform_bus);
1038 if (error)
1039 return error;
1040 error = bus_register(&platform_bus_type);
1041 if (error)
1042 device_unregister(&platform_bus);
1043 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
1047u64 dma_get_required_mask(struct device *dev)
1048{
1049 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1050 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1051 u64 mask;
1052
1053 if (!high_totalram) {
1054 /* convert to mask just covering totalram */
1055 low_totalram = (1 << (fls(low_totalram) - 1));
1056 low_totalram += low_totalram - 1;
1057 mask = low_totalram;
1058 } else {
1059 high_totalram = (1 << (fls(high_totalram) - 1));
1060 high_totalram += high_totalram - 1;
1061 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1062 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001063 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065EXPORT_SYMBOL_GPL(dma_get_required_mask);
1066#endif
Magnus Damm13977092009-03-30 14:37:25 -07001067
1068static __initdata LIST_HEAD(early_platform_driver_list);
1069static __initdata LIST_HEAD(early_platform_device_list);
1070
1071/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001072 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001073 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001074 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001075 *
1076 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001077 */
1078int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1079 char *buf)
1080{
Magnus Dammc60e0502009-11-27 17:38:51 +09001081 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001082 int n;
1083
1084 /* Simply add the driver to the end of the global list.
1085 * Drivers will by default be put on the list in compiled-in order.
1086 */
1087 if (!epdrv->list.next) {
1088 INIT_LIST_HEAD(&epdrv->list);
1089 list_add_tail(&epdrv->list, &early_platform_driver_list);
1090 }
1091
1092 /* If the user has specified device then make sure the driver
1093 * gets prioritized. The driver of the last device specified on
1094 * command line will be put first on the list.
1095 */
1096 n = strlen(epdrv->pdrv->driver.name);
1097 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1098 list_move(&epdrv->list, &early_platform_driver_list);
1099
Magnus Dammc60e0502009-11-27 17:38:51 +09001100 /* Allow passing parameters after device name */
1101 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001102 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001103 else {
1104 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1105 &tmp, 10);
1106
1107 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1108 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1109 n = 0;
1110 } else
1111 n += strcspn(&buf[n + 1], ",") + 1;
1112 }
1113
1114 if (buf[n] == ',')
1115 n++;
1116
1117 if (epdrv->bufsize) {
1118 memcpy(epdrv->buffer, &buf[n],
1119 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1120 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1121 }
Magnus Damm13977092009-03-30 14:37:25 -07001122 }
1123
1124 return 0;
1125}
1126
1127/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001128 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001129 * @devs: array of early platform devices to add
1130 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001131 *
1132 * Used by early architecture code to register early platform devices and
1133 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001134 */
1135void __init early_platform_add_devices(struct platform_device **devs, int num)
1136{
1137 struct device *dev;
1138 int i;
1139
1140 /* simply add the devices to list */
1141 for (i = 0; i < num; i++) {
1142 dev = &devs[i]->dev;
1143
1144 if (!dev->devres_head.next) {
1145 INIT_LIST_HEAD(&dev->devres_head);
1146 list_add_tail(&dev->devres_head,
1147 &early_platform_device_list);
1148 }
1149 }
1150}
1151
1152/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001153 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001154 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001155 *
1156 * Used by architecture code to register all early platform drivers
1157 * for a certain class. If omitted then only early platform drivers
1158 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001159 */
1160void __init early_platform_driver_register_all(char *class_str)
1161{
1162 /* The "class_str" parameter may or may not be present on the kernel
1163 * command line. If it is present then there may be more than one
1164 * matching parameter.
1165 *
1166 * Since we register our early platform drivers using early_param()
1167 * we need to make sure that they also get registered in the case
1168 * when the parameter is missing from the kernel command line.
1169 *
1170 * We use parse_early_options() to make sure the early_param() gets
1171 * called at least once. The early_param() may be called more than
1172 * once since the name of the preferred device may be specified on
1173 * the kernel command line. early_platform_driver_register() handles
1174 * this case for us.
1175 */
1176 parse_early_options(class_str);
1177}
1178
1179/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001180 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001181 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001182 * @id: id to match against
1183 */
1184static __init struct platform_device *
1185early_platform_match(struct early_platform_driver *epdrv, int id)
1186{
1187 struct platform_device *pd;
1188
1189 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1190 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1191 if (pd->id == id)
1192 return pd;
1193
1194 return NULL;
1195}
1196
1197/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001198 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001199 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001200 * @id: return true if id or above exists
1201 */
1202static __init int early_platform_left(struct early_platform_driver *epdrv,
1203 int id)
1204{
1205 struct platform_device *pd;
1206
1207 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1208 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1209 if (pd->id >= id)
1210 return 1;
1211
1212 return 0;
1213}
1214
1215/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001216 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001217 * @class_str: string to identify early platform driver class
1218 * @id: id to match against
1219 * @nr_probe: number of platform devices to successfully probe before exiting
1220 */
1221static int __init early_platform_driver_probe_id(char *class_str,
1222 int id,
1223 int nr_probe)
1224{
1225 struct early_platform_driver *epdrv;
1226 struct platform_device *match;
1227 int match_id;
1228 int n = 0;
1229 int left = 0;
1230
1231 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1232 /* only use drivers matching our class_str */
1233 if (strcmp(class_str, epdrv->class_str))
1234 continue;
1235
1236 if (id == -2) {
1237 match_id = epdrv->requested_id;
1238 left = 1;
1239
1240 } else {
1241 match_id = id;
1242 left += early_platform_left(epdrv, id);
1243
1244 /* skip requested id */
1245 switch (epdrv->requested_id) {
1246 case EARLY_PLATFORM_ID_ERROR:
1247 case EARLY_PLATFORM_ID_UNSET:
1248 break;
1249 default:
1250 if (epdrv->requested_id == id)
1251 match_id = EARLY_PLATFORM_ID_UNSET;
1252 }
1253 }
1254
1255 switch (match_id) {
1256 case EARLY_PLATFORM_ID_ERROR:
1257 pr_warning("%s: unable to parse %s parameter\n",
1258 class_str, epdrv->pdrv->driver.name);
1259 /* fall-through */
1260 case EARLY_PLATFORM_ID_UNSET:
1261 match = NULL;
1262 break;
1263 default:
1264 match = early_platform_match(epdrv, match_id);
1265 }
1266
1267 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001268 /*
1269 * Set up a sensible init_name to enable
1270 * dev_name() and others to be used before the
1271 * rest of the driver core is initialized.
1272 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001273 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001274 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001275 match->dev.init_name =
1276 kasprintf(GFP_KERNEL, "%s.%d",
1277 match->name,
1278 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001279 else
Paul Mundtbd050862010-03-29 15:51:35 +09001280 match->dev.init_name =
1281 kasprintf(GFP_KERNEL, "%s",
1282 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001283
Paul Mundta636ee72010-03-09 06:57:53 +00001284 if (!match->dev.init_name)
1285 return -ENOMEM;
1286 }
Paul Mundtbd050862010-03-29 15:51:35 +09001287
Magnus Damm13977092009-03-30 14:37:25 -07001288 if (epdrv->pdrv->probe(match))
1289 pr_warning("%s: unable to probe %s early.\n",
1290 class_str, match->name);
1291 else
1292 n++;
1293 }
1294
1295 if (n >= nr_probe)
1296 break;
1297 }
1298
1299 if (left)
1300 return n;
1301 else
1302 return -ENODEV;
1303}
1304
1305/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001306 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001307 * @class_str: string to identify early platform driver class
1308 * @nr_probe: number of platform devices to successfully probe before exiting
1309 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001310 *
1311 * Used by architecture code to probe registered early platform drivers
1312 * within a certain class. For probe to happen a registered early platform
1313 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001314 */
1315int __init early_platform_driver_probe(char *class_str,
1316 int nr_probe,
1317 int user_only)
1318{
1319 int k, n, i;
1320
1321 n = 0;
1322 for (i = -2; n < nr_probe; i++) {
1323 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1324
1325 if (k < 0)
1326 break;
1327
1328 n += k;
1329
1330 if (user_only)
1331 break;
1332 }
1333
1334 return n;
1335}
1336
1337/**
1338 * early_platform_cleanup - clean up early platform code
1339 */
1340void __init early_platform_cleanup(void)
1341{
1342 struct platform_device *pd, *pd2;
1343
1344 /* clean up the devres list used to chain devices */
1345 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1346 dev.devres_head) {
1347 list_del(&pd->dev.devres_head);
1348 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1349 }
1350}
1351