blob: 3966e62ad01950921c8dd497b233642caaef4a5f [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 Delvare13595552007-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
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400195 if (!res)
196 return 0;
197
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200198 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000199 if (r) {
Russell King37c12e72005-11-05 21:19:33 +0000200 pdev->resource = r;
201 pdev->num_resources = num;
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200202 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000203 }
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200204 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000205}
206EXPORT_SYMBOL_GPL(platform_device_add_resources);
207
208/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000209 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800210 * @pdev: platform device allocated by platform_device_alloc to add resources to
211 * @data: platform specific data for this platform device
212 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000213 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214 * Add a copy of platform specific data to the platform device's
215 * platform_data pointer. The memory associated with the platform data
216 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000217 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800218int platform_device_add_data(struct platform_device *pdev, const void *data,
219 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000220{
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400221 void *d;
Russell King37c12e72005-11-05 21:19:33 +0000222
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400223 if (!data)
224 return 0;
225
226 d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000227 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000228 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700229 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000230 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700231 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000232}
233EXPORT_SYMBOL_GPL(platform_device_add_data);
234
235/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800236 * platform_device_add - add a platform device to device hierarchy
237 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800239 * This is part 2 of platform_device_register(), though may be called
240 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Russell King37c12e72005-11-05 21:19:33 +0000242int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i, ret = 0;
245
246 if (!pdev)
247 return -EINVAL;
248
249 if (!pdev->dev.parent)
250 pdev->dev.parent = &platform_bus;
251
252 pdev->dev.bus = &platform_bus_type;
253
254 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100255 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700257 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 for (i = 0; i < pdev->num_resources; i++) {
260 struct resource *p, *r = &pdev->resource[i];
261
262 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100263 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 p = r->parent;
266 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700267 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700269 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 p = &ioport_resource;
271 }
272
Kumar Galad960bb42005-11-28 10:15:39 -0600273 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 printk(KERN_ERR
275 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100276 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 ret = -EBUSY;
278 goto failed;
279 }
280 }
281
282 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100283 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Russell Kinge3915532006-05-06 08:15:26 +0100285 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (ret == 0)
287 return ret;
288
289 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700290 while (--i >= 0) {
291 struct resource *r = &pdev->resource[i];
292 unsigned long type = resource_type(r);
293
294 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
295 release_resource(r);
296 }
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return ret;
299}
Russell King37c12e72005-11-05 21:19:33 +0000300EXPORT_SYMBOL_GPL(platform_device_add);
301
302/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800303 * platform_device_del - remove a platform-level device
304 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500305 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800306 * Note that this function will also release all memory- and port-based
307 * resources owned by the device (@dev->resource). This function must
308 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500309 */
310void platform_device_del(struct platform_device *pdev)
311{
312 int i;
313
314 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200315 device_del(&pdev->dev);
316
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500317 for (i = 0; i < pdev->num_resources; i++) {
318 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700319 unsigned long type = resource_type(r);
320
321 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500322 release_resource(r);
323 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500324 }
325}
326EXPORT_SYMBOL_GPL(platform_device_del);
327
328/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800329 * platform_device_register - add a platform-level device
330 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000331 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800332int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000333{
334 device_initialize(&pdev->dev);
335 return platform_device_add(pdev);
336}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500337EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800340 * platform_device_unregister - unregister a platform-level device
341 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800343 * Unregistration is done in 2 steps. First we release all resources
344 * and remove it from the subsystem, then we drop reference count by
345 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800347void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500349 platform_device_del(pdev);
350 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500352EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/**
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200355 * platform_device_register_resndata - add a platform-level device with
356 * resources and platform-specific data
357 *
358 * @parent: parent device for the device we're adding
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800359 * @name: base name of the device we're adding
360 * @id: instance id
361 * @res: set of resources that needs to be allocated for the device
362 * @num: number of resources
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700363 * @data: platform specific data for this platform device
364 * @size: size of platform specific data
365 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200366 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700367 */
Uwe Kleine-König737a3bb2010-06-21 16:11:45 +0200368struct platform_device *__init_or_module platform_device_register_resndata(
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700369 struct device *parent,
370 const char *name, int id,
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200371 const struct resource *res, unsigned int num,
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700372 const void *data, size_t size)
373{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200374 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700375 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700376
377 pdev = platform_device_alloc(name, id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200378 if (!pdev)
379 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700380
381 pdev->dev.parent = parent;
382
Anton Vorontsov807508c2010-09-07 17:31:54 +0400383 ret = platform_device_add_resources(pdev, res, num);
384 if (ret)
385 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700386
Anton Vorontsov807508c2010-09-07 17:31:54 +0400387 ret = platform_device_add_data(pdev, data, size);
388 if (ret)
389 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200390
391 ret = platform_device_add(pdev);
392 if (ret) {
393err:
394 platform_device_put(pdev);
395 return ERR_PTR(ret);
396 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700397
398 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700399}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200400EXPORT_SYMBOL_GPL(platform_device_register_resndata);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700401
Russell King00d3dcd2005-11-09 17:23:39 +0000402static int platform_drv_probe(struct device *_dev)
403{
404 struct platform_driver *drv = to_platform_driver(_dev->driver);
405 struct platform_device *dev = to_platform_device(_dev);
406
407 return drv->probe(dev);
408}
409
David Brownellc67334f2006-11-16 23:28:47 -0800410static int platform_drv_probe_fail(struct device *_dev)
411{
412 return -ENXIO;
413}
414
Russell King00d3dcd2005-11-09 17:23:39 +0000415static int platform_drv_remove(struct device *_dev)
416{
417 struct platform_driver *drv = to_platform_driver(_dev->driver);
418 struct platform_device *dev = to_platform_device(_dev);
419
420 return drv->remove(dev);
421}
422
423static void platform_drv_shutdown(struct device *_dev)
424{
425 struct platform_driver *drv = to_platform_driver(_dev->driver);
426 struct platform_device *dev = to_platform_device(_dev);
427
428 drv->shutdown(dev);
429}
430
Russell King00d3dcd2005-11-09 17:23:39 +0000431/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000432 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800433 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000434 */
435int platform_driver_register(struct platform_driver *drv)
436{
437 drv->driver.bus = &platform_bus_type;
438 if (drv->probe)
439 drv->driver.probe = platform_drv_probe;
440 if (drv->remove)
441 drv->driver.remove = platform_drv_remove;
442 if (drv->shutdown)
443 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200444
Russell King00d3dcd2005-11-09 17:23:39 +0000445 return driver_register(&drv->driver);
446}
447EXPORT_SYMBOL_GPL(platform_driver_register);
448
449/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000450 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800451 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000452 */
453void platform_driver_unregister(struct platform_driver *drv)
454{
455 driver_unregister(&drv->driver);
456}
457EXPORT_SYMBOL_GPL(platform_driver_unregister);
458
David Brownellc67334f2006-11-16 23:28:47 -0800459/**
460 * platform_driver_probe - register driver for non-hotpluggable device
461 * @drv: platform driver structure
462 * @probe: the driver probe routine, probably from an __init section
463 *
464 * Use this instead of platform_driver_register() when you know the device
465 * is not hotpluggable and has already been registered, and you want to
466 * remove its run-once probe() infrastructure from memory after the driver
467 * has bound to the device.
468 *
469 * One typical use for this would be with drivers for controllers integrated
470 * into system-on-chip processors, where the controller devices have been
471 * configured as part of board setup.
472 *
473 * Returns zero if the driver registered and bound to a device, else returns
474 * a negative error code and with the driver not registered.
475 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800476int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800477 int (*probe)(struct platform_device *))
478{
479 int retval, code;
480
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700481 /* make sure driver won't have bind/unbind attributes */
482 drv->driver.suppress_bind_attrs = true;
483
David Brownellc67334f2006-11-16 23:28:47 -0800484 /* temporary section violation during probe() */
485 drv->probe = probe;
486 retval = code = platform_driver_register(drv);
487
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700488 /*
489 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800490 * the list of drivers in order to probe new devices. Check to see
491 * if the probe was successful, and make sure any forced probes of
492 * new devices fail.
493 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700494 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800495 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800496 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800497 retval = -ENODEV;
498 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700499 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800500
501 if (code != retval)
502 platform_driver_unregister(drv);
503 return retval;
504}
505EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800507/**
508 * platform_create_bundle - register driver and create corresponding device
509 * @driver: platform driver structure
510 * @probe: the driver probe routine, probably from an __init section
511 * @res: set of resources that needs to be allocated for the device
512 * @n_res: number of resources
513 * @data: platform specific data for this platform device
514 * @size: size of platform specific data
515 *
516 * Use this in legacy-style modules that probe hardware directly and
517 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200518 *
519 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800520 */
521struct platform_device * __init_or_module platform_create_bundle(
522 struct platform_driver *driver,
523 int (*probe)(struct platform_device *),
524 struct resource *res, unsigned int n_res,
525 const void *data, size_t size)
526{
527 struct platform_device *pdev;
528 int error;
529
530 pdev = platform_device_alloc(driver->driver.name, -1);
531 if (!pdev) {
532 error = -ENOMEM;
533 goto err_out;
534 }
535
Anton Vorontsov807508c2010-09-07 17:31:54 +0400536 error = platform_device_add_resources(pdev, res, n_res);
537 if (error)
538 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800539
Anton Vorontsov807508c2010-09-07 17:31:54 +0400540 error = platform_device_add_data(pdev, data, size);
541 if (error)
542 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800543
544 error = platform_device_add(pdev);
545 if (error)
546 goto err_pdev_put;
547
548 error = platform_driver_probe(driver, probe);
549 if (error)
550 goto err_pdev_del;
551
552 return pdev;
553
554err_pdev_del:
555 platform_device_del(pdev);
556err_pdev_put:
557 platform_device_put(pdev);
558err_out:
559 return ERR_PTR(error);
560}
561EXPORT_SYMBOL_GPL(platform_create_bundle);
562
David Brownella0245f72006-05-29 10:37:33 -0700563/* modalias support enables more hands-off userspace setup:
564 * (a) environment variable lets new-style hotplug events work once system is
565 * fully running: "modprobe $MODALIAS"
566 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
567 * mishandled before system is fully running: "modprobe $(cat modalias)"
568 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800569static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
570 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700571{
572 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200573 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700574
575 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
576}
577
578static struct device_attribute platform_dev_attrs[] = {
579 __ATTR_RO(modalias),
580 __ATTR_NULL,
581};
582
Kay Sievers7eff2e72007-08-14 15:15:12 +0200583static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700584{
585 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600586 int rc;
587
588 /* Some devices have extra OF data and an OF-style MODALIAS */
589 rc = of_device_uevent(dev,env);
590 if (rc != -ENODEV)
591 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700592
Eric Miao57fee4a2009-02-04 11:52:40 +0800593 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
594 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700595 return 0;
596}
597
Eric Miao57fee4a2009-02-04 11:52:40 +0800598static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100599 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800600 struct platform_device *pdev)
601{
602 while (id->name[0]) {
603 if (strcmp(pdev->name, id->name) == 0) {
604 pdev->id_entry = id;
605 return id;
606 }
607 id++;
608 }
609 return NULL;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800613 * platform_match - bind platform device to platform driver.
614 * @dev: device.
615 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800617 * Platform device IDs are assumed to be encoded like this:
618 * "<name><instance>", where <name> is a short description of the type of
619 * device, like "pci" or "floppy", and <instance> is the enumerated
620 * instance of the device, like '0' or '42'. Driver IDs are simply
621 * "<name>". So, extract the <name> from the platform_device structure,
622 * and compare it against the name of the driver. Return whether they match
623 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800625static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800627 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800628 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Grant Likely05212152010-06-08 07:48:20 -0600630 /* Attempt an OF style match first */
631 if (of_driver_match_device(dev, drv))
632 return 1;
633
634 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800635 if (pdrv->id_table)
636 return platform_match_id(pdrv->id_table, pdev) != NULL;
637
638 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100639 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200642#ifdef CONFIG_PM_SLEEP
643
644static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200646 struct platform_driver *pdrv = to_platform_driver(dev->driver);
647 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int ret = 0;
649
Magnus Damm783ea7d2009-06-04 22:13:33 +0200650 if (dev->driver && pdrv->suspend)
651 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700652
653 return ret;
654}
655
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200656static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200658 struct platform_driver *pdrv = to_platform_driver(dev->driver);
659 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int ret = 0;
661
Magnus Damm783ea7d2009-06-04 22:13:33 +0200662 if (dev->driver && pdrv->resume)
663 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return ret;
666}
667
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200668static int platform_pm_prepare(struct device *dev)
669{
670 struct device_driver *drv = dev->driver;
671 int ret = 0;
672
673 if (drv && drv->pm && drv->pm->prepare)
674 ret = drv->pm->prepare(dev);
675
676 return ret;
677}
678
679static void platform_pm_complete(struct device *dev)
680{
681 struct device_driver *drv = dev->driver;
682
683 if (drv && drv->pm && drv->pm->complete)
684 drv->pm->complete(dev);
685}
686
Magnus Damm9d730222009-08-20 20:25:32 +0200687#else /* !CONFIG_PM_SLEEP */
688
689#define platform_pm_prepare NULL
690#define platform_pm_complete NULL
691
692#endif /* !CONFIG_PM_SLEEP */
693
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200694#ifdef CONFIG_SUSPEND
695
Kevin Hilman190e8372010-03-17 16:18:15 -0700696int __weak platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200697{
698 struct device_driver *drv = dev->driver;
699 int ret = 0;
700
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200701 if (!drv)
702 return 0;
703
704 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200705 if (drv->pm->suspend)
706 ret = drv->pm->suspend(dev);
707 } else {
708 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
709 }
710
711 return ret;
712}
713
Kevin Hilman190e8372010-03-17 16:18:15 -0700714int __weak platform_pm_suspend_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200715{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200716 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200717 int ret = 0;
718
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200719 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200720 return 0;
721
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200722 if (drv->pm) {
723 if (drv->pm->suspend_noirq)
724 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200725 }
726
727 return ret;
728}
729
Kevin Hilman190e8372010-03-17 16:18:15 -0700730int __weak platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200731{
732 struct device_driver *drv = dev->driver;
733 int ret = 0;
734
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200735 if (!drv)
736 return 0;
737
738 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200739 if (drv->pm->resume)
740 ret = drv->pm->resume(dev);
741 } else {
742 ret = platform_legacy_resume(dev);
743 }
744
745 return ret;
746}
747
Kevin Hilman190e8372010-03-17 16:18:15 -0700748int __weak platform_pm_resume_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200749{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200750 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200751 int ret = 0;
752
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200753 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200754 return 0;
755
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200756 if (drv->pm) {
757 if (drv->pm->resume_noirq)
758 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200759 }
760
761 return ret;
762}
763
764#else /* !CONFIG_SUSPEND */
765
766#define platform_pm_suspend NULL
767#define platform_pm_resume NULL
768#define platform_pm_suspend_noirq NULL
769#define platform_pm_resume_noirq NULL
770
771#endif /* !CONFIG_SUSPEND */
772
773#ifdef CONFIG_HIBERNATION
774
775static int platform_pm_freeze(struct device *dev)
776{
777 struct device_driver *drv = dev->driver;
778 int ret = 0;
779
780 if (!drv)
781 return 0;
782
783 if (drv->pm) {
784 if (drv->pm->freeze)
785 ret = drv->pm->freeze(dev);
786 } else {
787 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
788 }
789
790 return ret;
791}
792
793static int platform_pm_freeze_noirq(struct device *dev)
794{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200795 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200796 int ret = 0;
797
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200798 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200799 return 0;
800
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200801 if (drv->pm) {
802 if (drv->pm->freeze_noirq)
803 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200804 }
805
806 return ret;
807}
808
809static int platform_pm_thaw(struct device *dev)
810{
811 struct device_driver *drv = dev->driver;
812 int ret = 0;
813
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200814 if (!drv)
815 return 0;
816
817 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200818 if (drv->pm->thaw)
819 ret = drv->pm->thaw(dev);
820 } else {
821 ret = platform_legacy_resume(dev);
822 }
823
824 return ret;
825}
826
827static int platform_pm_thaw_noirq(struct device *dev)
828{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200829 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200830 int ret = 0;
831
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200832 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200833 return 0;
834
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200835 if (drv->pm) {
836 if (drv->pm->thaw_noirq)
837 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200838 }
839
840 return ret;
841}
842
843static int platform_pm_poweroff(struct device *dev)
844{
845 struct device_driver *drv = dev->driver;
846 int ret = 0;
847
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200848 if (!drv)
849 return 0;
850
851 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200852 if (drv->pm->poweroff)
853 ret = drv->pm->poweroff(dev);
854 } else {
855 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
856 }
857
858 return ret;
859}
860
861static int platform_pm_poweroff_noirq(struct device *dev)
862{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200863 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200864 int ret = 0;
865
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200866 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200867 return 0;
868
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200869 if (drv->pm) {
870 if (drv->pm->poweroff_noirq)
871 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200872 }
873
874 return ret;
875}
876
877static int platform_pm_restore(struct device *dev)
878{
879 struct device_driver *drv = dev->driver;
880 int ret = 0;
881
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200882 if (!drv)
883 return 0;
884
885 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200886 if (drv->pm->restore)
887 ret = drv->pm->restore(dev);
888 } else {
889 ret = platform_legacy_resume(dev);
890 }
891
892 return ret;
893}
894
895static int platform_pm_restore_noirq(struct device *dev)
896{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200897 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200898 int ret = 0;
899
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200900 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200901 return 0;
902
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200903 if (drv->pm) {
904 if (drv->pm->restore_noirq)
905 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200906 }
907
908 return ret;
909}
910
911#else /* !CONFIG_HIBERNATION */
912
913#define platform_pm_freeze NULL
914#define platform_pm_thaw NULL
915#define platform_pm_poweroff NULL
916#define platform_pm_restore NULL
917#define platform_pm_freeze_noirq NULL
918#define platform_pm_thaw_noirq NULL
919#define platform_pm_poweroff_noirq NULL
920#define platform_pm_restore_noirq NULL
921
922#endif /* !CONFIG_HIBERNATION */
923
Magnus Damm9d730222009-08-20 20:25:32 +0200924#ifdef CONFIG_PM_RUNTIME
925
926int __weak platform_pm_runtime_suspend(struct device *dev)
927{
Mark Brown543f25032010-05-10 23:10:13 +0200928 return pm_generic_runtime_suspend(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200929};
930
931int __weak platform_pm_runtime_resume(struct device *dev)
932{
Mark Brown543f25032010-05-10 23:10:13 +0200933 return pm_generic_runtime_resume(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200934};
935
936int __weak platform_pm_runtime_idle(struct device *dev)
937{
Mark Brown543f25032010-05-10 23:10:13 +0200938 return pm_generic_runtime_idle(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200939};
940
941#else /* !CONFIG_PM_RUNTIME */
942
943#define platform_pm_runtime_suspend NULL
944#define platform_pm_runtime_resume NULL
945#define platform_pm_runtime_idle NULL
946
947#endif /* !CONFIG_PM_RUNTIME */
948
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200949static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200950 .prepare = platform_pm_prepare,
951 .complete = platform_pm_complete,
952 .suspend = platform_pm_suspend,
953 .resume = platform_pm_resume,
954 .freeze = platform_pm_freeze,
955 .thaw = platform_pm_thaw,
956 .poweroff = platform_pm_poweroff,
957 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200958 .suspend_noirq = platform_pm_suspend_noirq,
959 .resume_noirq = platform_pm_resume_noirq,
960 .freeze_noirq = platform_pm_freeze_noirq,
961 .thaw_noirq = platform_pm_thaw_noirq,
962 .poweroff_noirq = platform_pm_poweroff_noirq,
963 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +0200964 .runtime_suspend = platform_pm_runtime_suspend,
965 .runtime_resume = platform_pm_runtime_resume,
966 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200967};
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969struct bus_type platform_bus_type = {
970 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700971 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700973 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200974 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500976EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Kevin Hilmanc64a0922010-08-25 12:50:00 -0700978/**
979 * platform_bus_get_pm_ops() - return pointer to busses dev_pm_ops
980 *
981 * This function can be used by platform code to get the current
982 * set of dev_pm_ops functions used by the platform_bus_type.
983 */
984const struct dev_pm_ops * __init platform_bus_get_pm_ops(void)
985{
986 return platform_bus_type.pm;
987}
988
989/**
990 * platform_bus_set_pm_ops() - update dev_pm_ops for the platform_bus_type
991 *
992 * @pm: pointer to new dev_pm_ops struct to be used for platform_bus_type
993 *
994 * Platform code can override the dev_pm_ops methods of
995 * platform_bus_type by using this function. It is expected that
996 * platform code will first do a platform_bus_get_pm_ops(), then
997 * kmemdup it, then customize selected methods and pass a pointer to
998 * the new struct dev_pm_ops to this function.
999 *
1000 * Since platform-specific code is customizing methods for *all*
1001 * devices (not just platform-specific devices) it is expected that
1002 * any custom overrides of these functions will keep existing behavior
1003 * and simply extend it. For example, any customization of the
1004 * runtime PM methods should continue to call the pm_generic_*
1005 * functions as the default ones do in addition to the
1006 * platform-specific behavior.
1007 */
1008void __init platform_bus_set_pm_ops(const struct dev_pm_ops *pm)
1009{
1010 platform_bus_type.pm = pm;
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013int __init platform_bus_init(void)
1014{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001015 int error;
1016
Magnus Damm13977092009-03-30 14:37:25 -07001017 early_platform_cleanup();
1018
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001019 error = device_register(&platform_bus);
1020 if (error)
1021 return error;
1022 error = bus_register(&platform_bus_type);
1023 if (error)
1024 device_unregister(&platform_bus);
1025 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
1029u64 dma_get_required_mask(struct device *dev)
1030{
1031 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1032 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1033 u64 mask;
1034
1035 if (!high_totalram) {
1036 /* convert to mask just covering totalram */
1037 low_totalram = (1 << (fls(low_totalram) - 1));
1038 low_totalram += low_totalram - 1;
1039 mask = low_totalram;
1040 } else {
1041 high_totalram = (1 << (fls(high_totalram) - 1));
1042 high_totalram += high_totalram - 1;
1043 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1044 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001045 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047EXPORT_SYMBOL_GPL(dma_get_required_mask);
1048#endif
Magnus Damm13977092009-03-30 14:37:25 -07001049
1050static __initdata LIST_HEAD(early_platform_driver_list);
1051static __initdata LIST_HEAD(early_platform_device_list);
1052
1053/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001054 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001055 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001056 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001057 *
1058 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001059 */
1060int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1061 char *buf)
1062{
Magnus Dammc60e0502009-11-27 17:38:51 +09001063 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001064 int n;
1065
1066 /* Simply add the driver to the end of the global list.
1067 * Drivers will by default be put on the list in compiled-in order.
1068 */
1069 if (!epdrv->list.next) {
1070 INIT_LIST_HEAD(&epdrv->list);
1071 list_add_tail(&epdrv->list, &early_platform_driver_list);
1072 }
1073
1074 /* If the user has specified device then make sure the driver
1075 * gets prioritized. The driver of the last device specified on
1076 * command line will be put first on the list.
1077 */
1078 n = strlen(epdrv->pdrv->driver.name);
1079 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1080 list_move(&epdrv->list, &early_platform_driver_list);
1081
Magnus Dammc60e0502009-11-27 17:38:51 +09001082 /* Allow passing parameters after device name */
1083 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001084 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001085 else {
1086 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1087 &tmp, 10);
1088
1089 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1090 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1091 n = 0;
1092 } else
1093 n += strcspn(&buf[n + 1], ",") + 1;
1094 }
1095
1096 if (buf[n] == ',')
1097 n++;
1098
1099 if (epdrv->bufsize) {
1100 memcpy(epdrv->buffer, &buf[n],
1101 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1102 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1103 }
Magnus Damm13977092009-03-30 14:37:25 -07001104 }
1105
1106 return 0;
1107}
1108
1109/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001110 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001111 * @devs: array of early platform devices to add
1112 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001113 *
1114 * Used by early architecture code to register early platform devices and
1115 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001116 */
1117void __init early_platform_add_devices(struct platform_device **devs, int num)
1118{
1119 struct device *dev;
1120 int i;
1121
1122 /* simply add the devices to list */
1123 for (i = 0; i < num; i++) {
1124 dev = &devs[i]->dev;
1125
1126 if (!dev->devres_head.next) {
1127 INIT_LIST_HEAD(&dev->devres_head);
1128 list_add_tail(&dev->devres_head,
1129 &early_platform_device_list);
1130 }
1131 }
1132}
1133
1134/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001135 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001136 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001137 *
1138 * Used by architecture code to register all early platform drivers
1139 * for a certain class. If omitted then only early platform drivers
1140 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001141 */
1142void __init early_platform_driver_register_all(char *class_str)
1143{
1144 /* The "class_str" parameter may or may not be present on the kernel
1145 * command line. If it is present then there may be more than one
1146 * matching parameter.
1147 *
1148 * Since we register our early platform drivers using early_param()
1149 * we need to make sure that they also get registered in the case
1150 * when the parameter is missing from the kernel command line.
1151 *
1152 * We use parse_early_options() to make sure the early_param() gets
1153 * called at least once. The early_param() may be called more than
1154 * once since the name of the preferred device may be specified on
1155 * the kernel command line. early_platform_driver_register() handles
1156 * this case for us.
1157 */
1158 parse_early_options(class_str);
1159}
1160
1161/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001162 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001163 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001164 * @id: id to match against
1165 */
1166static __init struct platform_device *
1167early_platform_match(struct early_platform_driver *epdrv, int id)
1168{
1169 struct platform_device *pd;
1170
1171 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1172 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1173 if (pd->id == id)
1174 return pd;
1175
1176 return NULL;
1177}
1178
1179/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001180 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001181 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001182 * @id: return true if id or above exists
1183 */
1184static __init int early_platform_left(struct early_platform_driver *epdrv,
1185 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 1;
1193
1194 return 0;
1195}
1196
1197/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001198 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001199 * @class_str: string to identify early platform driver class
1200 * @id: id to match against
1201 * @nr_probe: number of platform devices to successfully probe before exiting
1202 */
1203static int __init early_platform_driver_probe_id(char *class_str,
1204 int id,
1205 int nr_probe)
1206{
1207 struct early_platform_driver *epdrv;
1208 struct platform_device *match;
1209 int match_id;
1210 int n = 0;
1211 int left = 0;
1212
1213 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1214 /* only use drivers matching our class_str */
1215 if (strcmp(class_str, epdrv->class_str))
1216 continue;
1217
1218 if (id == -2) {
1219 match_id = epdrv->requested_id;
1220 left = 1;
1221
1222 } else {
1223 match_id = id;
1224 left += early_platform_left(epdrv, id);
1225
1226 /* skip requested id */
1227 switch (epdrv->requested_id) {
1228 case EARLY_PLATFORM_ID_ERROR:
1229 case EARLY_PLATFORM_ID_UNSET:
1230 break;
1231 default:
1232 if (epdrv->requested_id == id)
1233 match_id = EARLY_PLATFORM_ID_UNSET;
1234 }
1235 }
1236
1237 switch (match_id) {
1238 case EARLY_PLATFORM_ID_ERROR:
1239 pr_warning("%s: unable to parse %s parameter\n",
1240 class_str, epdrv->pdrv->driver.name);
1241 /* fall-through */
1242 case EARLY_PLATFORM_ID_UNSET:
1243 match = NULL;
1244 break;
1245 default:
1246 match = early_platform_match(epdrv, match_id);
1247 }
1248
1249 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001250 /*
1251 * Set up a sensible init_name to enable
1252 * dev_name() and others to be used before the
1253 * rest of the driver core is initialized.
1254 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001255 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001256 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001257 match->dev.init_name =
1258 kasprintf(GFP_KERNEL, "%s.%d",
1259 match->name,
1260 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001261 else
Paul Mundtbd050862010-03-29 15:51:35 +09001262 match->dev.init_name =
1263 kasprintf(GFP_KERNEL, "%s",
1264 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001265
Paul Mundta636ee72010-03-09 06:57:53 +00001266 if (!match->dev.init_name)
1267 return -ENOMEM;
1268 }
Paul Mundtbd050862010-03-29 15:51:35 +09001269
Magnus Damm13977092009-03-30 14:37:25 -07001270 if (epdrv->pdrv->probe(match))
1271 pr_warning("%s: unable to probe %s early.\n",
1272 class_str, match->name);
1273 else
1274 n++;
1275 }
1276
1277 if (n >= nr_probe)
1278 break;
1279 }
1280
1281 if (left)
1282 return n;
1283 else
1284 return -ENODEV;
1285}
1286
1287/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001288 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001289 * @class_str: string to identify early platform driver class
1290 * @nr_probe: number of platform devices to successfully probe before exiting
1291 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001292 *
1293 * Used by architecture code to probe registered early platform drivers
1294 * within a certain class. For probe to happen a registered early platform
1295 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001296 */
1297int __init early_platform_driver_probe(char *class_str,
1298 int nr_probe,
1299 int user_only)
1300{
1301 int k, n, i;
1302
1303 n = 0;
1304 for (i = -2; n < nr_probe; i++) {
1305 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1306
1307 if (k < 0)
1308 break;
1309
1310 n += k;
1311
1312 if (user_only)
1313 break;
1314 }
1315
1316 return n;
1317}
1318
1319/**
1320 * early_platform_cleanup - clean up early platform code
1321 */
1322void __init early_platform_cleanup(void)
1323{
1324 struct platform_device *pd, *pd2;
1325
1326 /* clean up the devres list used to chain devices */
1327 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1328 dev.devres_head) {
1329 list_del(&pd->dev.devres_head);
1330 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1331 }
1332}
1333