blob: 079c18a5e471e2040173e68c064001e7371b4028 [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);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200152 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000153 kfree(pa->pdev.resource);
154 kfree(pa);
155}
156
157/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000158 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800159 * @name: base name of the device we're adding
160 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000161 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800162 * Create a platform device object which can have other objects attached
163 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000164 */
Jean Delvare13595552007-09-09 12:54:16 +0200165struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000166{
167 struct platform_object *pa;
168
169 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
170 if (pa) {
171 strcpy(pa->name, name);
172 pa->pdev.name = pa->name;
173 pa->pdev.id = id;
174 device_initialize(&pa->pdev.dev);
175 pa->pdev.dev.release = platform_device_release;
176 }
177
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500178 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000179}
180EXPORT_SYMBOL_GPL(platform_device_alloc);
181
182/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000183 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800184 * @pdev: platform device allocated by platform_device_alloc to add resources to
185 * @res: set of resources that needs to be allocated for the device
186 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000187 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800188 * Add a copy of the resources to the platform device. The memory
189 * associated with the resources will be freed when the platform device is
190 * released.
Russell King37c12e72005-11-05 21:19:33 +0000191 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800192int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100193 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000194{
195 struct resource *r;
196
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400197 if (!res)
198 return 0;
199
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200200 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000201 if (r) {
Russell King37c12e72005-11-05 21:19:33 +0000202 pdev->resource = r;
203 pdev->num_resources = num;
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200204 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000205 }
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200206 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000207}
208EXPORT_SYMBOL_GPL(platform_device_add_resources);
209
210/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000211 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800212 * @pdev: platform device allocated by platform_device_alloc to add resources to
213 * @data: platform specific data for this platform device
214 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000215 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216 * Add a copy of platform specific data to the platform device's
217 * platform_data pointer. The memory associated with the platform data
218 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000219 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800220int platform_device_add_data(struct platform_device *pdev, const void *data,
221 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000222{
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400223 void *d;
Russell King37c12e72005-11-05 21:19:33 +0000224
Anton Vorontsov5cfc64c2010-09-07 17:31:49 +0400225 if (!data)
226 return 0;
227
228 d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000229 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000230 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700231 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000232 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700233 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000234}
235EXPORT_SYMBOL_GPL(platform_device_add_data);
236
237/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800238 * platform_device_add - add a platform device to device hierarchy
239 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800241 * This is part 2 of platform_device_register(), though may be called
242 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Russell King37c12e72005-11-05 21:19:33 +0000244int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int i, ret = 0;
247
248 if (!pdev)
249 return -EINVAL;
250
251 if (!pdev->dev.parent)
252 pdev->dev.parent = &platform_bus;
253
254 pdev->dev.bus = &platform_bus_type;
255
256 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100257 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700259 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 for (i = 0; i < pdev->num_resources; i++) {
262 struct resource *p, *r = &pdev->resource[i];
263
264 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100265 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 p = r->parent;
268 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700269 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700271 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 p = &ioport_resource;
273 }
274
Kumar Galad960bb42005-11-28 10:15:39 -0600275 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 printk(KERN_ERR
277 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100278 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ret = -EBUSY;
280 goto failed;
281 }
282 }
283
284 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100285 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Russell Kinge3915532006-05-06 08:15:26 +0100287 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (ret == 0)
289 return ret;
290
291 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700292 while (--i >= 0) {
293 struct resource *r = &pdev->resource[i];
294 unsigned long type = resource_type(r);
295
296 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
297 release_resource(r);
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return ret;
301}
Russell King37c12e72005-11-05 21:19:33 +0000302EXPORT_SYMBOL_GPL(platform_device_add);
303
304/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800305 * platform_device_del - remove a platform-level device
306 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500307 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800308 * Note that this function will also release all memory- and port-based
309 * resources owned by the device (@dev->resource). This function must
310 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500311 */
312void platform_device_del(struct platform_device *pdev)
313{
314 int i;
315
316 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200317 device_del(&pdev->dev);
318
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500319 for (i = 0; i < pdev->num_resources; i++) {
320 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700321 unsigned long type = resource_type(r);
322
323 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500324 release_resource(r);
325 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500326 }
327}
328EXPORT_SYMBOL_GPL(platform_device_del);
329
330/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800331 * platform_device_register - add a platform-level device
332 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000333 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800334int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000335{
336 device_initialize(&pdev->dev);
337 return platform_device_add(pdev);
338}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500339EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800342 * platform_device_unregister - unregister a platform-level device
343 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800345 * Unregistration is done in 2 steps. First we release all resources
346 * and remove it from the subsystem, then we drop reference count by
347 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800349void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500351 platform_device_del(pdev);
352 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500354EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200357 * platform_device_register_resndata - add a platform-level device with
358 * resources and platform-specific data
359 *
360 * @parent: parent device for the device we're adding
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800361 * @name: base name of the device we're adding
362 * @id: instance id
363 * @res: set of resources that needs to be allocated for the device
364 * @num: number of resources
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700365 * @data: platform specific data for this platform device
366 * @size: size of platform specific data
367 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200368 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700369 */
Uwe Kleine-König737a3bb2010-06-21 16:11:45 +0200370struct platform_device *__init_or_module platform_device_register_resndata(
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700371 struct device *parent,
372 const char *name, int id,
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200373 const struct resource *res, unsigned int num,
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700374 const void *data, size_t size)
375{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200376 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700377 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700378
379 pdev = platform_device_alloc(name, id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200380 if (!pdev)
381 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700382
383 pdev->dev.parent = parent;
384
Anton Vorontsov807508c2010-09-07 17:31:54 +0400385 ret = platform_device_add_resources(pdev, res, num);
386 if (ret)
387 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700388
Anton Vorontsov807508c2010-09-07 17:31:54 +0400389 ret = platform_device_add_data(pdev, data, size);
390 if (ret)
391 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200392
393 ret = platform_device_add(pdev);
394 if (ret) {
395err:
396 platform_device_put(pdev);
397 return ERR_PTR(ret);
398 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700399
400 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700401}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200402EXPORT_SYMBOL_GPL(platform_device_register_resndata);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700403
Russell King00d3dcd2005-11-09 17:23:39 +0000404static int platform_drv_probe(struct device *_dev)
405{
406 struct platform_driver *drv = to_platform_driver(_dev->driver);
407 struct platform_device *dev = to_platform_device(_dev);
408
409 return drv->probe(dev);
410}
411
David Brownellc67334f2006-11-16 23:28:47 -0800412static int platform_drv_probe_fail(struct device *_dev)
413{
414 return -ENXIO;
415}
416
Russell King00d3dcd2005-11-09 17:23:39 +0000417static int platform_drv_remove(struct device *_dev)
418{
419 struct platform_driver *drv = to_platform_driver(_dev->driver);
420 struct platform_device *dev = to_platform_device(_dev);
421
422 return drv->remove(dev);
423}
424
425static void platform_drv_shutdown(struct device *_dev)
426{
427 struct platform_driver *drv = to_platform_driver(_dev->driver);
428 struct platform_device *dev = to_platform_device(_dev);
429
430 drv->shutdown(dev);
431}
432
Russell King00d3dcd2005-11-09 17:23:39 +0000433/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000434 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800435 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000436 */
437int platform_driver_register(struct platform_driver *drv)
438{
439 drv->driver.bus = &platform_bus_type;
440 if (drv->probe)
441 drv->driver.probe = platform_drv_probe;
442 if (drv->remove)
443 drv->driver.remove = platform_drv_remove;
444 if (drv->shutdown)
445 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200446
Russell King00d3dcd2005-11-09 17:23:39 +0000447 return driver_register(&drv->driver);
448}
449EXPORT_SYMBOL_GPL(platform_driver_register);
450
451/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000452 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800453 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000454 */
455void platform_driver_unregister(struct platform_driver *drv)
456{
457 driver_unregister(&drv->driver);
458}
459EXPORT_SYMBOL_GPL(platform_driver_unregister);
460
David Brownellc67334f2006-11-16 23:28:47 -0800461/**
462 * platform_driver_probe - register driver for non-hotpluggable device
463 * @drv: platform driver structure
464 * @probe: the driver probe routine, probably from an __init section
465 *
466 * Use this instead of platform_driver_register() when you know the device
467 * is not hotpluggable and has already been registered, and you want to
468 * remove its run-once probe() infrastructure from memory after the driver
469 * has bound to the device.
470 *
471 * One typical use for this would be with drivers for controllers integrated
472 * into system-on-chip processors, where the controller devices have been
473 * configured as part of board setup.
474 *
475 * Returns zero if the driver registered and bound to a device, else returns
476 * a negative error code and with the driver not registered.
477 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800478int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800479 int (*probe)(struct platform_device *))
480{
481 int retval, code;
482
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700483 /* make sure driver won't have bind/unbind attributes */
484 drv->driver.suppress_bind_attrs = true;
485
David Brownellc67334f2006-11-16 23:28:47 -0800486 /* temporary section violation during probe() */
487 drv->probe = probe;
488 retval = code = platform_driver_register(drv);
489
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700490 /*
491 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800492 * the list of drivers in order to probe new devices. Check to see
493 * if the probe was successful, and make sure any forced probes of
494 * new devices fail.
495 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700496 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800497 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800498 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800499 retval = -ENODEV;
500 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700501 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800502
503 if (code != retval)
504 platform_driver_unregister(drv);
505 return retval;
506}
507EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800509/**
510 * platform_create_bundle - register driver and create corresponding device
511 * @driver: platform driver structure
512 * @probe: the driver probe routine, probably from an __init section
513 * @res: set of resources that needs to be allocated for the device
514 * @n_res: number of resources
515 * @data: platform specific data for this platform device
516 * @size: size of platform specific data
517 *
518 * Use this in legacy-style modules that probe hardware directly and
519 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200520 *
521 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800522 */
523struct platform_device * __init_or_module platform_create_bundle(
524 struct platform_driver *driver,
525 int (*probe)(struct platform_device *),
526 struct resource *res, unsigned int n_res,
527 const void *data, size_t size)
528{
529 struct platform_device *pdev;
530 int error;
531
532 pdev = platform_device_alloc(driver->driver.name, -1);
533 if (!pdev) {
534 error = -ENOMEM;
535 goto err_out;
536 }
537
Anton Vorontsov807508c2010-09-07 17:31:54 +0400538 error = platform_device_add_resources(pdev, res, n_res);
539 if (error)
540 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800541
Anton Vorontsov807508c2010-09-07 17:31:54 +0400542 error = platform_device_add_data(pdev, data, size);
543 if (error)
544 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800545
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. Wysocki69c9dd12011-04-29 00:36:05 +0200670int platform_pm_prepare(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200671{
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
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200681void platform_pm_complete(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200682{
683 struct device_driver *drv = dev->driver;
684
685 if (drv && drv->pm && drv->pm->complete)
686 drv->pm->complete(dev);
687}
688
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200689#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +0200690
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200691#ifdef CONFIG_SUSPEND
692
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200693int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200694{
695 struct device_driver *drv = dev->driver;
696 int ret = 0;
697
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200698 if (!drv)
699 return 0;
700
701 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200702 if (drv->pm->suspend)
703 ret = drv->pm->suspend(dev);
704 } else {
705 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
706 }
707
708 return ret;
709}
710
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200711int platform_pm_suspend_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200712{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200713 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200714 int ret = 0;
715
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200716 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200717 return 0;
718
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200719 if (drv->pm) {
720 if (drv->pm->suspend_noirq)
721 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200722 }
723
724 return ret;
725}
726
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200727int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200728{
729 struct device_driver *drv = dev->driver;
730 int ret = 0;
731
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200732 if (!drv)
733 return 0;
734
735 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200736 if (drv->pm->resume)
737 ret = drv->pm->resume(dev);
738 } else {
739 ret = platform_legacy_resume(dev);
740 }
741
742 return ret;
743}
744
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200745int platform_pm_resume_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200746{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200747 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200748 int ret = 0;
749
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200750 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200751 return 0;
752
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200753 if (drv->pm) {
754 if (drv->pm->resume_noirq)
755 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200756 }
757
758 return ret;
759}
760
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200761#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200762
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200763#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200764
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200765int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200766{
767 struct device_driver *drv = dev->driver;
768 int ret = 0;
769
770 if (!drv)
771 return 0;
772
773 if (drv->pm) {
774 if (drv->pm->freeze)
775 ret = drv->pm->freeze(dev);
776 } else {
777 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
778 }
779
780 return ret;
781}
782
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200783int platform_pm_freeze_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200784{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200785 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200786 int ret = 0;
787
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200788 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200789 return 0;
790
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200791 if (drv->pm) {
792 if (drv->pm->freeze_noirq)
793 ret = drv->pm->freeze_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200794 }
795
796 return ret;
797}
798
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200799int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200800{
801 struct device_driver *drv = dev->driver;
802 int ret = 0;
803
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200804 if (!drv)
805 return 0;
806
807 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200808 if (drv->pm->thaw)
809 ret = drv->pm->thaw(dev);
810 } else {
811 ret = platform_legacy_resume(dev);
812 }
813
814 return ret;
815}
816
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200817int platform_pm_thaw_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200818{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200819 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200820 int ret = 0;
821
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200822 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200823 return 0;
824
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200825 if (drv->pm) {
826 if (drv->pm->thaw_noirq)
827 ret = drv->pm->thaw_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200828 }
829
830 return ret;
831}
832
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200833int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200834{
835 struct device_driver *drv = dev->driver;
836 int ret = 0;
837
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200838 if (!drv)
839 return 0;
840
841 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200842 if (drv->pm->poweroff)
843 ret = drv->pm->poweroff(dev);
844 } else {
845 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
846 }
847
848 return ret;
849}
850
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200851int platform_pm_poweroff_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200852{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200853 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200854 int ret = 0;
855
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200856 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200857 return 0;
858
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200859 if (drv->pm) {
860 if (drv->pm->poweroff_noirq)
861 ret = drv->pm->poweroff_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200862 }
863
864 return ret;
865}
866
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200867int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200868{
869 struct device_driver *drv = dev->driver;
870 int ret = 0;
871
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200872 if (!drv)
873 return 0;
874
875 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200876 if (drv->pm->restore)
877 ret = drv->pm->restore(dev);
878 } else {
879 ret = platform_legacy_resume(dev);
880 }
881
882 return ret;
883}
884
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200885int platform_pm_restore_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200886{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200887 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200888 int ret = 0;
889
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200890 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200891 return 0;
892
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200893 if (drv->pm) {
894 if (drv->pm->restore_noirq)
895 ret = drv->pm->restore_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200896 }
897
898 return ret;
899}
900
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200901#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200902
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200903static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +0200904 .runtime_suspend = pm_generic_runtime_suspend,
905 .runtime_resume = pm_generic_runtime_resume,
906 .runtime_idle = pm_generic_runtime_idle,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200907 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200908};
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910struct bus_type platform_bus_type = {
911 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700912 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700914 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200915 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500917EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Kevin Hilmanc64a0922010-08-25 12:50:00 -0700919/**
920 * platform_bus_get_pm_ops() - return pointer to busses dev_pm_ops
921 *
922 * This function can be used by platform code to get the current
923 * set of dev_pm_ops functions used by the platform_bus_type.
924 */
925const struct dev_pm_ops * __init platform_bus_get_pm_ops(void)
926{
927 return platform_bus_type.pm;
928}
929
930/**
931 * platform_bus_set_pm_ops() - update dev_pm_ops for the platform_bus_type
932 *
933 * @pm: pointer to new dev_pm_ops struct to be used for platform_bus_type
934 *
935 * Platform code can override the dev_pm_ops methods of
936 * platform_bus_type by using this function. It is expected that
937 * platform code will first do a platform_bus_get_pm_ops(), then
938 * kmemdup it, then customize selected methods and pass a pointer to
939 * the new struct dev_pm_ops to this function.
940 *
941 * Since platform-specific code is customizing methods for *all*
942 * devices (not just platform-specific devices) it is expected that
943 * any custom overrides of these functions will keep existing behavior
944 * and simply extend it. For example, any customization of the
945 * runtime PM methods should continue to call the pm_generic_*
946 * functions as the default ones do in addition to the
947 * platform-specific behavior.
948 */
949void __init platform_bus_set_pm_ops(const struct dev_pm_ops *pm)
950{
951 platform_bus_type.pm = pm;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954int __init platform_bus_init(void)
955{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100956 int error;
957
Magnus Damm13977092009-03-30 14:37:25 -0700958 early_platform_cleanup();
959
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100960 error = device_register(&platform_bus);
961 if (error)
962 return error;
963 error = bus_register(&platform_bus_type);
964 if (error)
965 device_unregister(&platform_bus);
966 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
970u64 dma_get_required_mask(struct device *dev)
971{
972 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
973 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
974 u64 mask;
975
976 if (!high_totalram) {
977 /* convert to mask just covering totalram */
978 low_totalram = (1 << (fls(low_totalram) - 1));
979 low_totalram += low_totalram - 1;
980 mask = low_totalram;
981 } else {
982 high_totalram = (1 << (fls(high_totalram) - 1));
983 high_totalram += high_totalram - 1;
984 mask = (((u64)high_totalram) << 32) + 0xffffffff;
985 }
James Bottomleye88a0c22008-03-09 11:57:56 -0500986 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988EXPORT_SYMBOL_GPL(dma_get_required_mask);
989#endif
Magnus Damm13977092009-03-30 14:37:25 -0700990
991static __initdata LIST_HEAD(early_platform_driver_list);
992static __initdata LIST_HEAD(early_platform_device_list);
993
994/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900995 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -0700996 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -0700997 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +0900998 *
999 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001000 */
1001int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1002 char *buf)
1003{
Magnus Dammc60e0502009-11-27 17:38:51 +09001004 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001005 int n;
1006
1007 /* Simply add the driver to the end of the global list.
1008 * Drivers will by default be put on the list in compiled-in order.
1009 */
1010 if (!epdrv->list.next) {
1011 INIT_LIST_HEAD(&epdrv->list);
1012 list_add_tail(&epdrv->list, &early_platform_driver_list);
1013 }
1014
1015 /* If the user has specified device then make sure the driver
1016 * gets prioritized. The driver of the last device specified on
1017 * command line will be put first on the list.
1018 */
1019 n = strlen(epdrv->pdrv->driver.name);
1020 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1021 list_move(&epdrv->list, &early_platform_driver_list);
1022
Magnus Dammc60e0502009-11-27 17:38:51 +09001023 /* Allow passing parameters after device name */
1024 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001025 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001026 else {
1027 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1028 &tmp, 10);
1029
1030 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1031 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1032 n = 0;
1033 } else
1034 n += strcspn(&buf[n + 1], ",") + 1;
1035 }
1036
1037 if (buf[n] == ',')
1038 n++;
1039
1040 if (epdrv->bufsize) {
1041 memcpy(epdrv->buffer, &buf[n],
1042 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1043 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1044 }
Magnus Damm13977092009-03-30 14:37:25 -07001045 }
1046
1047 return 0;
1048}
1049
1050/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001051 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001052 * @devs: array of early platform devices to add
1053 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001054 *
1055 * Used by early architecture code to register early platform devices and
1056 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001057 */
1058void __init early_platform_add_devices(struct platform_device **devs, int num)
1059{
1060 struct device *dev;
1061 int i;
1062
1063 /* simply add the devices to list */
1064 for (i = 0; i < num; i++) {
1065 dev = &devs[i]->dev;
1066
1067 if (!dev->devres_head.next) {
1068 INIT_LIST_HEAD(&dev->devres_head);
1069 list_add_tail(&dev->devres_head,
1070 &early_platform_device_list);
1071 }
1072 }
1073}
1074
1075/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001076 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001077 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001078 *
1079 * Used by architecture code to register all early platform drivers
1080 * for a certain class. If omitted then only early platform drivers
1081 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001082 */
1083void __init early_platform_driver_register_all(char *class_str)
1084{
1085 /* The "class_str" parameter may or may not be present on the kernel
1086 * command line. If it is present then there may be more than one
1087 * matching parameter.
1088 *
1089 * Since we register our early platform drivers using early_param()
1090 * we need to make sure that they also get registered in the case
1091 * when the parameter is missing from the kernel command line.
1092 *
1093 * We use parse_early_options() to make sure the early_param() gets
1094 * called at least once. The early_param() may be called more than
1095 * once since the name of the preferred device may be specified on
1096 * the kernel command line. early_platform_driver_register() handles
1097 * this case for us.
1098 */
1099 parse_early_options(class_str);
1100}
1101
1102/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001103 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001104 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001105 * @id: id to match against
1106 */
1107static __init struct platform_device *
1108early_platform_match(struct early_platform_driver *epdrv, int id)
1109{
1110 struct platform_device *pd;
1111
1112 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1113 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1114 if (pd->id == id)
1115 return pd;
1116
1117 return NULL;
1118}
1119
1120/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001121 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001122 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001123 * @id: return true if id or above exists
1124 */
1125static __init int early_platform_left(struct early_platform_driver *epdrv,
1126 int id)
1127{
1128 struct platform_device *pd;
1129
1130 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1131 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1132 if (pd->id >= id)
1133 return 1;
1134
1135 return 0;
1136}
1137
1138/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001139 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001140 * @class_str: string to identify early platform driver class
1141 * @id: id to match against
1142 * @nr_probe: number of platform devices to successfully probe before exiting
1143 */
1144static int __init early_platform_driver_probe_id(char *class_str,
1145 int id,
1146 int nr_probe)
1147{
1148 struct early_platform_driver *epdrv;
1149 struct platform_device *match;
1150 int match_id;
1151 int n = 0;
1152 int left = 0;
1153
1154 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1155 /* only use drivers matching our class_str */
1156 if (strcmp(class_str, epdrv->class_str))
1157 continue;
1158
1159 if (id == -2) {
1160 match_id = epdrv->requested_id;
1161 left = 1;
1162
1163 } else {
1164 match_id = id;
1165 left += early_platform_left(epdrv, id);
1166
1167 /* skip requested id */
1168 switch (epdrv->requested_id) {
1169 case EARLY_PLATFORM_ID_ERROR:
1170 case EARLY_PLATFORM_ID_UNSET:
1171 break;
1172 default:
1173 if (epdrv->requested_id == id)
1174 match_id = EARLY_PLATFORM_ID_UNSET;
1175 }
1176 }
1177
1178 switch (match_id) {
1179 case EARLY_PLATFORM_ID_ERROR:
1180 pr_warning("%s: unable to parse %s parameter\n",
1181 class_str, epdrv->pdrv->driver.name);
1182 /* fall-through */
1183 case EARLY_PLATFORM_ID_UNSET:
1184 match = NULL;
1185 break;
1186 default:
1187 match = early_platform_match(epdrv, match_id);
1188 }
1189
1190 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001191 /*
1192 * Set up a sensible init_name to enable
1193 * dev_name() and others to be used before the
1194 * rest of the driver core is initialized.
1195 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001196 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001197 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001198 match->dev.init_name =
1199 kasprintf(GFP_KERNEL, "%s.%d",
1200 match->name,
1201 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001202 else
Paul Mundtbd050862010-03-29 15:51:35 +09001203 match->dev.init_name =
1204 kasprintf(GFP_KERNEL, "%s",
1205 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001206
Paul Mundta636ee72010-03-09 06:57:53 +00001207 if (!match->dev.init_name)
1208 return -ENOMEM;
1209 }
Paul Mundtbd050862010-03-29 15:51:35 +09001210
Magnus Damm13977092009-03-30 14:37:25 -07001211 if (epdrv->pdrv->probe(match))
1212 pr_warning("%s: unable to probe %s early.\n",
1213 class_str, match->name);
1214 else
1215 n++;
1216 }
1217
1218 if (n >= nr_probe)
1219 break;
1220 }
1221
1222 if (left)
1223 return n;
1224 else
1225 return -ENODEV;
1226}
1227
1228/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001229 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001230 * @class_str: string to identify early platform driver class
1231 * @nr_probe: number of platform devices to successfully probe before exiting
1232 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001233 *
1234 * Used by architecture code to probe registered early platform drivers
1235 * within a certain class. For probe to happen a registered early platform
1236 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001237 */
1238int __init early_platform_driver_probe(char *class_str,
1239 int nr_probe,
1240 int user_only)
1241{
1242 int k, n, i;
1243
1244 n = 0;
1245 for (i = -2; n < nr_probe; i++) {
1246 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1247
1248 if (k < 0)
1249 break;
1250
1251 n += k;
1252
1253 if (user_only)
1254 break;
1255 }
1256
1257 return n;
1258}
1259
1260/**
1261 * early_platform_cleanup - clean up early platform code
1262 */
1263void __init early_platform_cleanup(void)
1264{
1265 struct platform_device *pd, *pd2;
1266
1267 /* clean up the devres list used to chain devices */
1268 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1269 dev.devres_head) {
1270 list_del(&pd->dev.devres_head);
1271 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1272 }
1273}
1274