blob: 2fff59cef5051cc54366c3e11a8db0c990584d25 [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
Grant Likely7096d042010-10-20 11:45:13 -0600150 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000151 kfree(pa->pdev.dev.platform_data);
152 kfree(pa->pdev.resource);
153 kfree(pa);
154}
155
156/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000157 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800158 * @name: base name of the device we're adding
159 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000160 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800161 * Create a platform device object which can have other objects attached
162 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000163 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200164struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000165{
166 struct platform_object *pa;
167
168 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
169 if (pa) {
170 strcpy(pa->name, name);
171 pa->pdev.name = pa->name;
172 pa->pdev.id = id;
173 device_initialize(&pa->pdev.dev);
174 pa->pdev.dev.release = platform_device_release;
175 }
176
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500177 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000178}
179EXPORT_SYMBOL_GPL(platform_device_alloc);
180
181/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000182 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800183 * @pdev: platform device allocated by platform_device_alloc to add resources to
184 * @res: set of resources that needs to be allocated for the device
185 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000186 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800187 * Add a copy of the resources to the platform device. The memory
188 * associated with the resources will be freed when the platform device is
189 * released.
Russell King37c12e72005-11-05 21:19:33 +0000190 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800191int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100192 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000193{
194 struct resource *r;
195
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200196 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000197 if (r) {
Russell King37c12e72005-11-05 21:19:33 +0000198 pdev->resource = r;
199 pdev->num_resources = num;
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200200 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000201 }
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200202 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000203}
204EXPORT_SYMBOL_GPL(platform_device_add_resources);
205
206/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000207 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800208 * @pdev: platform device allocated by platform_device_alloc to add resources to
209 * @data: platform specific data for this platform device
210 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000211 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800212 * Add a copy of platform specific data to the platform device's
213 * platform_data pointer. The memory associated with the platform data
214 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000215 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216int platform_device_add_data(struct platform_device *pdev, const void *data,
217 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000218{
Andrew Mortondaa41222009-08-06 16:00:44 -0700219 void *d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000220
Russell King37c12e72005-11-05 21:19:33 +0000221 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000222 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700223 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000224 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700225 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000226}
227EXPORT_SYMBOL_GPL(platform_device_add_data);
228
229/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800230 * platform_device_add - add a platform device to device hierarchy
231 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800233 * This is part 2 of platform_device_register(), though may be called
234 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Russell King37c12e72005-11-05 21:19:33 +0000236int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int i, ret = 0;
239
240 if (!pdev)
241 return -EINVAL;
242
243 if (!pdev->dev.parent)
244 pdev->dev.parent = &platform_bus;
245
246 pdev->dev.bus = &platform_bus_type;
247
248 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100249 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700251 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 for (i = 0; i < pdev->num_resources; i++) {
254 struct resource *p, *r = &pdev->resource[i];
255
256 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100257 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 p = r->parent;
260 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700261 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700263 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 p = &ioport_resource;
265 }
266
Kumar Galad960bb42005-11-28 10:15:39 -0600267 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 printk(KERN_ERR
269 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100270 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ret = -EBUSY;
272 goto failed;
273 }
274 }
275
276 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100277 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Russell Kinge3915532006-05-06 08:15:26 +0100279 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (ret == 0)
281 return ret;
282
283 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700284 while (--i >= 0) {
285 struct resource *r = &pdev->resource[i];
286 unsigned long type = resource_type(r);
287
288 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
289 release_resource(r);
290 }
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return ret;
293}
Russell King37c12e72005-11-05 21:19:33 +0000294EXPORT_SYMBOL_GPL(platform_device_add);
295
296/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800297 * platform_device_del - remove a platform-level device
298 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500299 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800300 * Note that this function will also release all memory- and port-based
301 * resources owned by the device (@dev->resource). This function must
302 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500303 */
304void platform_device_del(struct platform_device *pdev)
305{
306 int i;
307
308 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200309 device_del(&pdev->dev);
310
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500311 for (i = 0; i < pdev->num_resources; i++) {
312 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700313 unsigned long type = resource_type(r);
314
315 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500316 release_resource(r);
317 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500318 }
319}
320EXPORT_SYMBOL_GPL(platform_device_del);
321
322/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800323 * platform_device_register - add a platform-level device
324 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000325 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800326int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000327{
328 device_initialize(&pdev->dev);
329 return platform_device_add(pdev);
330}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500331EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800334 * platform_device_unregister - unregister a platform-level device
335 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800337 * Unregistration is done in 2 steps. First we release all resources
338 * and remove it from the subsystem, then we drop reference count by
339 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800341void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500343 platform_device_del(pdev);
344 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500346EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/**
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200349 * platform_device_register_resndata - add a platform-level device with
350 * resources and platform-specific data
351 *
352 * @parent: parent device for the device we're adding
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800353 * @name: base name of the device we're adding
354 * @id: instance id
355 * @res: set of resources that needs to be allocated for the device
356 * @num: number of resources
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700357 * @data: platform specific data for this platform device
358 * @size: size of platform specific data
359 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200360 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700361 */
Uwe Kleine-König737a3bb2010-06-21 16:11:45 +0200362struct platform_device *__init_or_module platform_device_register_resndata(
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700363 struct device *parent,
364 const char *name, int id,
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200365 const struct resource *res, unsigned int num,
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700366 const void *data, size_t size)
367{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200368 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700369 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700370
371 pdev = platform_device_alloc(name, id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200372 if (!pdev)
373 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700374
375 pdev->dev.parent = parent;
376
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200377 if (res) {
378 ret = platform_device_add_resources(pdev, res, num);
379 if (ret)
380 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700381 }
382
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200383 if (data) {
384 ret = platform_device_add_data(pdev, data, size);
385 if (ret)
386 goto err;
387 }
388
389 ret = platform_device_add(pdev);
390 if (ret) {
391err:
392 platform_device_put(pdev);
393 return ERR_PTR(ret);
394 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700395
396 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700397}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200398EXPORT_SYMBOL_GPL(platform_device_register_resndata);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700399
Russell King00d3dcd2005-11-09 17:23:39 +0000400static int platform_drv_probe(struct device *_dev)
401{
402 struct platform_driver *drv = to_platform_driver(_dev->driver);
403 struct platform_device *dev = to_platform_device(_dev);
404
405 return drv->probe(dev);
406}
407
David Brownellc67334f2006-11-16 23:28:47 -0800408static int platform_drv_probe_fail(struct device *_dev)
409{
410 return -ENXIO;
411}
412
Russell King00d3dcd2005-11-09 17:23:39 +0000413static int platform_drv_remove(struct device *_dev)
414{
415 struct platform_driver *drv = to_platform_driver(_dev->driver);
416 struct platform_device *dev = to_platform_device(_dev);
417
418 return drv->remove(dev);
419}
420
421static void platform_drv_shutdown(struct device *_dev)
422{
423 struct platform_driver *drv = to_platform_driver(_dev->driver);
424 struct platform_device *dev = to_platform_device(_dev);
425
426 drv->shutdown(dev);
427}
428
Russell King00d3dcd2005-11-09 17:23:39 +0000429/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000430 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800431 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000432 */
433int platform_driver_register(struct platform_driver *drv)
434{
435 drv->driver.bus = &platform_bus_type;
436 if (drv->probe)
437 drv->driver.probe = platform_drv_probe;
438 if (drv->remove)
439 drv->driver.remove = platform_drv_remove;
440 if (drv->shutdown)
441 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200442
Russell King00d3dcd2005-11-09 17:23:39 +0000443 return driver_register(&drv->driver);
444}
445EXPORT_SYMBOL_GPL(platform_driver_register);
446
447/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000448 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800449 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000450 */
451void platform_driver_unregister(struct platform_driver *drv)
452{
453 driver_unregister(&drv->driver);
454}
455EXPORT_SYMBOL_GPL(platform_driver_unregister);
456
David Brownellc67334f2006-11-16 23:28:47 -0800457/**
458 * platform_driver_probe - register driver for non-hotpluggable device
459 * @drv: platform driver structure
460 * @probe: the driver probe routine, probably from an __init section
461 *
462 * Use this instead of platform_driver_register() when you know the device
463 * is not hotpluggable and has already been registered, and you want to
464 * remove its run-once probe() infrastructure from memory after the driver
465 * has bound to the device.
466 *
467 * One typical use for this would be with drivers for controllers integrated
468 * into system-on-chip processors, where the controller devices have been
469 * configured as part of board setup.
470 *
471 * Returns zero if the driver registered and bound to a device, else returns
472 * a negative error code and with the driver not registered.
473 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800474int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800475 int (*probe)(struct platform_device *))
476{
477 int retval, code;
478
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700479 /* make sure driver won't have bind/unbind attributes */
480 drv->driver.suppress_bind_attrs = true;
481
David Brownellc67334f2006-11-16 23:28:47 -0800482 /* temporary section violation during probe() */
483 drv->probe = probe;
484 retval = code = platform_driver_register(drv);
485
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700486 /*
487 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800488 * the list of drivers in order to probe new devices. Check to see
489 * if the probe was successful, and make sure any forced probes of
490 * new devices fail.
491 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700492 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800493 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800494 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800495 retval = -ENODEV;
496 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700497 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800498
499 if (code != retval)
500 platform_driver_unregister(drv);
501 return retval;
502}
503EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800505/**
506 * platform_create_bundle - register driver and create corresponding device
507 * @driver: platform driver structure
508 * @probe: the driver probe routine, probably from an __init section
509 * @res: set of resources that needs to be allocated for the device
510 * @n_res: number of resources
511 * @data: platform specific data for this platform device
512 * @size: size of platform specific data
513 *
514 * Use this in legacy-style modules that probe hardware directly and
515 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200516 *
517 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800518 */
519struct platform_device * __init_or_module platform_create_bundle(
520 struct platform_driver *driver,
521 int (*probe)(struct platform_device *),
522 struct resource *res, unsigned int n_res,
523 const void *data, size_t size)
524{
525 struct platform_device *pdev;
526 int error;
527
528 pdev = platform_device_alloc(driver->driver.name, -1);
529 if (!pdev) {
530 error = -ENOMEM;
531 goto err_out;
532 }
533
534 if (res) {
535 error = platform_device_add_resources(pdev, res, n_res);
536 if (error)
537 goto err_pdev_put;
538 }
539
540 if (data) {
541 error = platform_device_add_data(pdev, data, size);
542 if (error)
543 goto err_pdev_put;
544 }
545
546 error = platform_device_add(pdev);
547 if (error)
548 goto err_pdev_put;
549
550 error = platform_driver_probe(driver, probe);
551 if (error)
552 goto err_pdev_del;
553
554 return pdev;
555
556err_pdev_del:
557 platform_device_del(pdev);
558err_pdev_put:
559 platform_device_put(pdev);
560err_out:
561 return ERR_PTR(error);
562}
563EXPORT_SYMBOL_GPL(platform_create_bundle);
564
David Brownella0245f72006-05-29 10:37:33 -0700565/* modalias support enables more hands-off userspace setup:
566 * (a) environment variable lets new-style hotplug events work once system is
567 * fully running: "modprobe $MODALIAS"
568 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
569 * mishandled before system is fully running: "modprobe $(cat modalias)"
570 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800571static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
572 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700573{
574 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200575 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700576
577 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
578}
579
580static struct device_attribute platform_dev_attrs[] = {
581 __ATTR_RO(modalias),
582 __ATTR_NULL,
583};
584
Kay Sievers7eff2e72007-08-14 15:15:12 +0200585static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700586{
587 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600588 int rc;
589
590 /* Some devices have extra OF data and an OF-style MODALIAS */
591 rc = of_device_uevent(dev,env);
592 if (rc != -ENODEV)
593 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700594
Eric Miao57fee4a2009-02-04 11:52:40 +0800595 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
596 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700597 return 0;
598}
599
Eric Miao57fee4a2009-02-04 11:52:40 +0800600static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100601 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800602 struct platform_device *pdev)
603{
604 while (id->name[0]) {
605 if (strcmp(pdev->name, id->name) == 0) {
606 pdev->id_entry = id;
607 return id;
608 }
609 id++;
610 }
611 return NULL;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800615 * platform_match - bind platform device to platform driver.
616 * @dev: device.
617 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800619 * Platform device IDs are assumed to be encoded like this:
620 * "<name><instance>", where <name> is a short description of the type of
621 * device, like "pci" or "floppy", and <instance> is the enumerated
622 * instance of the device, like '0' or '42'. Driver IDs are simply
623 * "<name>". So, extract the <name> from the platform_device structure,
624 * and compare it against the name of the driver. Return whether they match
625 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800627static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800629 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800630 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Grant Likely05212152010-06-08 07:48:20 -0600632 /* Attempt an OF style match first */
633 if (of_driver_match_device(dev, drv))
634 return 1;
635
636 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800637 if (pdrv->id_table)
638 return platform_match_id(pdrv->id_table, pdev) != NULL;
639
640 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100641 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200644#ifdef CONFIG_PM_SLEEP
645
646static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200648 struct platform_driver *pdrv = to_platform_driver(dev->driver);
649 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int ret = 0;
651
Magnus Damm783ea7d2009-06-04 22:13:33 +0200652 if (dev->driver && pdrv->suspend)
653 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700654
655 return ret;
656}
657
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200658static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200660 struct platform_driver *pdrv = to_platform_driver(dev->driver);
661 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int ret = 0;
663
Magnus Damm783ea7d2009-06-04 22:13:33 +0200664 if (dev->driver && pdrv->resume)
665 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return ret;
668}
669
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200670static int platform_pm_prepare(struct device *dev)
671{
672 struct device_driver *drv = dev->driver;
673 int ret = 0;
674
675 if (drv && drv->pm && drv->pm->prepare)
676 ret = drv->pm->prepare(dev);
677
678 return ret;
679}
680
681static void platform_pm_complete(struct device *dev)
682{
683 struct device_driver *drv = dev->driver;
684
685 if (drv && drv->pm && drv->pm->complete)
686 drv->pm->complete(dev);
687}
688
Magnus Damm9d730222009-08-20 20:25:32 +0200689#else /* !CONFIG_PM_SLEEP */
690
691#define platform_pm_prepare NULL
692#define platform_pm_complete NULL
693
694#endif /* !CONFIG_PM_SLEEP */
695
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200696#ifdef CONFIG_SUSPEND
697
Kevin Hilman190e8372010-03-17 16:18:15 -0700698int __weak platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200699{
700 struct device_driver *drv = dev->driver;
701 int ret = 0;
702
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200703 if (!drv)
704 return 0;
705
706 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200707 if (drv->pm->suspend)
708 ret = drv->pm->suspend(dev);
709 } else {
710 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
711 }
712
713 return ret;
714}
715
Kevin Hilman190e8372010-03-17 16:18:15 -0700716int __weak platform_pm_suspend_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200717{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200718 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200719 int ret = 0;
720
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200721 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200722 return 0;
723
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200724 if (drv->pm) {
725 if (drv->pm->suspend_noirq)
726 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200727 }
728
729 return ret;
730}
731
Kevin Hilman190e8372010-03-17 16:18:15 -0700732int __weak platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200733{
734 struct device_driver *drv = dev->driver;
735 int ret = 0;
736
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200737 if (!drv)
738 return 0;
739
740 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200741 if (drv->pm->resume)
742 ret = drv->pm->resume(dev);
743 } else {
744 ret = platform_legacy_resume(dev);
745 }
746
747 return ret;
748}
749
Kevin Hilman190e8372010-03-17 16:18:15 -0700750int __weak platform_pm_resume_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200751{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200752 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200753 int ret = 0;
754
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200755 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200756 return 0;
757
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200758 if (drv->pm) {
759 if (drv->pm->resume_noirq)
760 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200761 }
762
763 return ret;
764}
765
766#else /* !CONFIG_SUSPEND */
767
768#define platform_pm_suspend NULL
769#define platform_pm_resume NULL
770#define platform_pm_suspend_noirq NULL
771#define platform_pm_resume_noirq NULL
772
773#endif /* !CONFIG_SUSPEND */
774
775#ifdef CONFIG_HIBERNATION
776
777static int platform_pm_freeze(struct device *dev)
778{
779 struct device_driver *drv = dev->driver;
780 int ret = 0;
781
782 if (!drv)
783 return 0;
784
785 if (drv->pm) {
786 if (drv->pm->freeze)
787 ret = drv->pm->freeze(dev);
788 } else {
789 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
790 }
791
792 return ret;
793}
794
795static int platform_pm_freeze_noirq(struct device *dev)
796{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200797 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200798 int ret = 0;
799
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200800 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200801 return 0;
802
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200803 if (drv->pm) {
804 if (drv->pm->freeze_noirq)
805 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200806 }
807
808 return ret;
809}
810
811static int platform_pm_thaw(struct device *dev)
812{
813 struct device_driver *drv = dev->driver;
814 int ret = 0;
815
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200816 if (!drv)
817 return 0;
818
819 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200820 if (drv->pm->thaw)
821 ret = drv->pm->thaw(dev);
822 } else {
823 ret = platform_legacy_resume(dev);
824 }
825
826 return ret;
827}
828
829static int platform_pm_thaw_noirq(struct device *dev)
830{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200831 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200832 int ret = 0;
833
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200834 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200835 return 0;
836
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200837 if (drv->pm) {
838 if (drv->pm->thaw_noirq)
839 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200840 }
841
842 return ret;
843}
844
845static int platform_pm_poweroff(struct device *dev)
846{
847 struct device_driver *drv = dev->driver;
848 int ret = 0;
849
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200850 if (!drv)
851 return 0;
852
853 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200854 if (drv->pm->poweroff)
855 ret = drv->pm->poweroff(dev);
856 } else {
857 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
858 }
859
860 return ret;
861}
862
863static int platform_pm_poweroff_noirq(struct device *dev)
864{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200865 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200866 int ret = 0;
867
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200868 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200869 return 0;
870
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200871 if (drv->pm) {
872 if (drv->pm->poweroff_noirq)
873 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200874 }
875
876 return ret;
877}
878
879static int platform_pm_restore(struct device *dev)
880{
881 struct device_driver *drv = dev->driver;
882 int ret = 0;
883
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200884 if (!drv)
885 return 0;
886
887 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200888 if (drv->pm->restore)
889 ret = drv->pm->restore(dev);
890 } else {
891 ret = platform_legacy_resume(dev);
892 }
893
894 return ret;
895}
896
897static int platform_pm_restore_noirq(struct device *dev)
898{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200899 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200900 int ret = 0;
901
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200902 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200903 return 0;
904
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200905 if (drv->pm) {
906 if (drv->pm->restore_noirq)
907 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200908 }
909
910 return ret;
911}
912
913#else /* !CONFIG_HIBERNATION */
914
915#define platform_pm_freeze NULL
916#define platform_pm_thaw NULL
917#define platform_pm_poweroff NULL
918#define platform_pm_restore NULL
919#define platform_pm_freeze_noirq NULL
920#define platform_pm_thaw_noirq NULL
921#define platform_pm_poweroff_noirq NULL
922#define platform_pm_restore_noirq NULL
923
924#endif /* !CONFIG_HIBERNATION */
925
Magnus Damm9d730222009-08-20 20:25:32 +0200926#ifdef CONFIG_PM_RUNTIME
927
928int __weak platform_pm_runtime_suspend(struct device *dev)
929{
Mark Brown543f25032010-05-10 23:10:13 +0200930 return pm_generic_runtime_suspend(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200931};
932
933int __weak platform_pm_runtime_resume(struct device *dev)
934{
Mark Brown543f25032010-05-10 23:10:13 +0200935 return pm_generic_runtime_resume(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200936};
937
938int __weak platform_pm_runtime_idle(struct device *dev)
939{
Mark Brown543f25032010-05-10 23:10:13 +0200940 return pm_generic_runtime_idle(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200941};
942
943#else /* !CONFIG_PM_RUNTIME */
944
945#define platform_pm_runtime_suspend NULL
946#define platform_pm_runtime_resume NULL
947#define platform_pm_runtime_idle NULL
948
949#endif /* !CONFIG_PM_RUNTIME */
950
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200951static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200952 .prepare = platform_pm_prepare,
953 .complete = platform_pm_complete,
954 .suspend = platform_pm_suspend,
955 .resume = platform_pm_resume,
956 .freeze = platform_pm_freeze,
957 .thaw = platform_pm_thaw,
958 .poweroff = platform_pm_poweroff,
959 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200960 .suspend_noirq = platform_pm_suspend_noirq,
961 .resume_noirq = platform_pm_resume_noirq,
962 .freeze_noirq = platform_pm_freeze_noirq,
963 .thaw_noirq = platform_pm_thaw_noirq,
964 .poweroff_noirq = platform_pm_poweroff_noirq,
965 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +0200966 .runtime_suspend = platform_pm_runtime_suspend,
967 .runtime_resume = platform_pm_runtime_resume,
968 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200969};
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971struct bus_type platform_bus_type = {
972 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700973 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700975 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200976 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500978EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980int __init platform_bus_init(void)
981{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100982 int error;
983
Magnus Damm13977092009-03-30 14:37:25 -0700984 early_platform_cleanup();
985
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100986 error = device_register(&platform_bus);
987 if (error)
988 return error;
989 error = bus_register(&platform_bus_type);
990 if (error)
991 device_unregister(&platform_bus);
992 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
996u64 dma_get_required_mask(struct device *dev)
997{
998 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
999 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1000 u64 mask;
1001
1002 if (!high_totalram) {
1003 /* convert to mask just covering totalram */
1004 low_totalram = (1 << (fls(low_totalram) - 1));
1005 low_totalram += low_totalram - 1;
1006 mask = low_totalram;
1007 } else {
1008 high_totalram = (1 << (fls(high_totalram) - 1));
1009 high_totalram += high_totalram - 1;
1010 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1011 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001012 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014EXPORT_SYMBOL_GPL(dma_get_required_mask);
1015#endif
Magnus Damm13977092009-03-30 14:37:25 -07001016
1017static __initdata LIST_HEAD(early_platform_driver_list);
1018static __initdata LIST_HEAD(early_platform_device_list);
1019
1020/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001021 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001022 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001023 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001024 *
1025 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001026 */
1027int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1028 char *buf)
1029{
Magnus Dammc60e0502009-11-27 17:38:51 +09001030 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001031 int n;
1032
1033 /* Simply add the driver to the end of the global list.
1034 * Drivers will by default be put on the list in compiled-in order.
1035 */
1036 if (!epdrv->list.next) {
1037 INIT_LIST_HEAD(&epdrv->list);
1038 list_add_tail(&epdrv->list, &early_platform_driver_list);
1039 }
1040
1041 /* If the user has specified device then make sure the driver
1042 * gets prioritized. The driver of the last device specified on
1043 * command line will be put first on the list.
1044 */
1045 n = strlen(epdrv->pdrv->driver.name);
1046 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1047 list_move(&epdrv->list, &early_platform_driver_list);
1048
Magnus Dammc60e0502009-11-27 17:38:51 +09001049 /* Allow passing parameters after device name */
1050 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001051 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001052 else {
1053 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1054 &tmp, 10);
1055
1056 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1057 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1058 n = 0;
1059 } else
1060 n += strcspn(&buf[n + 1], ",") + 1;
1061 }
1062
1063 if (buf[n] == ',')
1064 n++;
1065
1066 if (epdrv->bufsize) {
1067 memcpy(epdrv->buffer, &buf[n],
1068 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1069 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1070 }
Magnus Damm13977092009-03-30 14:37:25 -07001071 }
1072
1073 return 0;
1074}
1075
1076/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001077 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001078 * @devs: array of early platform devices to add
1079 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001080 *
1081 * Used by early architecture code to register early platform devices and
1082 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001083 */
1084void __init early_platform_add_devices(struct platform_device **devs, int num)
1085{
1086 struct device *dev;
1087 int i;
1088
1089 /* simply add the devices to list */
1090 for (i = 0; i < num; i++) {
1091 dev = &devs[i]->dev;
1092
1093 if (!dev->devres_head.next) {
1094 INIT_LIST_HEAD(&dev->devres_head);
1095 list_add_tail(&dev->devres_head,
1096 &early_platform_device_list);
1097 }
1098 }
1099}
1100
1101/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001102 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001103 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001104 *
1105 * Used by architecture code to register all early platform drivers
1106 * for a certain class. If omitted then only early platform drivers
1107 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001108 */
1109void __init early_platform_driver_register_all(char *class_str)
1110{
1111 /* The "class_str" parameter may or may not be present on the kernel
1112 * command line. If it is present then there may be more than one
1113 * matching parameter.
1114 *
1115 * Since we register our early platform drivers using early_param()
1116 * we need to make sure that they also get registered in the case
1117 * when the parameter is missing from the kernel command line.
1118 *
1119 * We use parse_early_options() to make sure the early_param() gets
1120 * called at least once. The early_param() may be called more than
1121 * once since the name of the preferred device may be specified on
1122 * the kernel command line. early_platform_driver_register() handles
1123 * this case for us.
1124 */
1125 parse_early_options(class_str);
1126}
1127
1128/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001129 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001130 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001131 * @id: id to match against
1132 */
1133static __init struct platform_device *
1134early_platform_match(struct early_platform_driver *epdrv, int id)
1135{
1136 struct platform_device *pd;
1137
1138 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1139 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1140 if (pd->id == id)
1141 return pd;
1142
1143 return NULL;
1144}
1145
1146/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001147 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001148 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001149 * @id: return true if id or above exists
1150 */
1151static __init int early_platform_left(struct early_platform_driver *epdrv,
1152 int id)
1153{
1154 struct platform_device *pd;
1155
1156 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1157 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1158 if (pd->id >= id)
1159 return 1;
1160
1161 return 0;
1162}
1163
1164/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001165 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001166 * @class_str: string to identify early platform driver class
1167 * @id: id to match against
1168 * @nr_probe: number of platform devices to successfully probe before exiting
1169 */
1170static int __init early_platform_driver_probe_id(char *class_str,
1171 int id,
1172 int nr_probe)
1173{
1174 struct early_platform_driver *epdrv;
1175 struct platform_device *match;
1176 int match_id;
1177 int n = 0;
1178 int left = 0;
1179
1180 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1181 /* only use drivers matching our class_str */
1182 if (strcmp(class_str, epdrv->class_str))
1183 continue;
1184
1185 if (id == -2) {
1186 match_id = epdrv->requested_id;
1187 left = 1;
1188
1189 } else {
1190 match_id = id;
1191 left += early_platform_left(epdrv, id);
1192
1193 /* skip requested id */
1194 switch (epdrv->requested_id) {
1195 case EARLY_PLATFORM_ID_ERROR:
1196 case EARLY_PLATFORM_ID_UNSET:
1197 break;
1198 default:
1199 if (epdrv->requested_id == id)
1200 match_id = EARLY_PLATFORM_ID_UNSET;
1201 }
1202 }
1203
1204 switch (match_id) {
1205 case EARLY_PLATFORM_ID_ERROR:
1206 pr_warning("%s: unable to parse %s parameter\n",
1207 class_str, epdrv->pdrv->driver.name);
1208 /* fall-through */
1209 case EARLY_PLATFORM_ID_UNSET:
1210 match = NULL;
1211 break;
1212 default:
1213 match = early_platform_match(epdrv, match_id);
1214 }
1215
1216 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001217 /*
1218 * Set up a sensible init_name to enable
1219 * dev_name() and others to be used before the
1220 * rest of the driver core is initialized.
1221 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001222 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001223 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001224 match->dev.init_name =
1225 kasprintf(GFP_KERNEL, "%s.%d",
1226 match->name,
1227 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001228 else
Paul Mundtbd050862010-03-29 15:51:35 +09001229 match->dev.init_name =
1230 kasprintf(GFP_KERNEL, "%s",
1231 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001232
Paul Mundta636ee72010-03-09 06:57:53 +00001233 if (!match->dev.init_name)
1234 return -ENOMEM;
1235 }
Paul Mundtbd050862010-03-29 15:51:35 +09001236
Magnus Damm13977092009-03-30 14:37:25 -07001237 if (epdrv->pdrv->probe(match))
1238 pr_warning("%s: unable to probe %s early.\n",
1239 class_str, match->name);
1240 else
1241 n++;
1242 }
1243
1244 if (n >= nr_probe)
1245 break;
1246 }
1247
1248 if (left)
1249 return n;
1250 else
1251 return -ENODEV;
1252}
1253
1254/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001255 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001256 * @class_str: string to identify early platform driver class
1257 * @nr_probe: number of platform devices to successfully probe before exiting
1258 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001259 *
1260 * Used by architecture code to probe registered early platform drivers
1261 * within a certain class. For probe to happen a registered early platform
1262 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001263 */
1264int __init early_platform_driver_probe(char *class_str,
1265 int nr_probe,
1266 int user_only)
1267{
1268 int k, n, i;
1269
1270 n = 0;
1271 for (i = -2; n < nr_probe; i++) {
1272 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1273
1274 if (k < 0)
1275 break;
1276
1277 n += k;
1278
1279 if (user_only)
1280 break;
1281 }
1282
1283 return n;
1284}
1285
1286/**
1287 * early_platform_cleanup - clean up early platform code
1288 */
1289void __init early_platform_cleanup(void)
1290{
1291 struct platform_device *pd, *pd2;
1292
1293 /* clean up the devres list used to chain devices */
1294 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1295 dev.devres_head) {
1296 list_del(&pd->dev.devres_head);
1297 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1298 }
1299}
1300