blob: ffcfd73fe8a6abcd2c8ccd885a538c8f694abedf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * platform.c - platform 'pseudo' bus for legacy devices
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 * Please see Documentation/driver-model/platform.txt for more
10 * information.
11 */
12
Andrew Mortondaa41222009-08-06 16:00:44 -070013#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010014#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/dma-mapping.h>
18#include <linux/bootmem.h>
19#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020021#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010023#include "base.h"
24
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080025#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
26 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010029 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050031EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080034 * platform_get_resource - get a resource for a device
35 * @dev: platform device
36 * @type: resource type
37 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080039struct resource *platform_get_resource(struct platform_device *dev,
40 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int i;
43
44 for (i = 0; i < dev->num_resources; i++) {
45 struct resource *r = &dev->resource[i];
46
Magnus Dammc9f66162008-10-15 22:05:15 -070047 if (type == resource_type(r) && num-- == 0)
48 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 return NULL;
51}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050052EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080055 * platform_get_irq - get an IRQ for a device
56 * @dev: platform device
57 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59int platform_get_irq(struct platform_device *dev, unsigned int num)
60{
61 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
62
David Vrabel305b3222006-01-19 17:52:27 +000063 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050065EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080068 * platform_get_resource_byname - get a resource for a device by name
69 * @dev: platform device
70 * @type: resource type
71 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080073struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +020074 unsigned int type,
75 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int i;
78
79 for (i = 0; i < dev->num_resources; i++) {
80 struct resource *r = &dev->resource[i];
81
Magnus Dammc9f66162008-10-15 22:05:15 -070082 if (type == resource_type(r) && !strcmp(r->name, name))
83 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 return NULL;
86}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050087EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080090 * platform_get_irq - get an IRQ for a device
91 * @dev: platform device
92 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +020094int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080096 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
97 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David Vrabel305b3222006-01-19 17:52:27 +000099 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500101EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800104 * platform_add_devices - add a numbers of platform devices
105 * @devs: array of platform devices to add
106 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 */
108int platform_add_devices(struct platform_device **devs, int num)
109{
110 int i, ret = 0;
111
112 for (i = 0; i < num; i++) {
113 ret = platform_device_register(devs[i]);
114 if (ret) {
115 while (--i >= 0)
116 platform_device_unregister(devs[i]);
117 break;
118 }
119 }
120
121 return ret;
122}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500123EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Russell King37c12e72005-11-05 21:19:33 +0000125struct platform_object {
126 struct platform_device pdev;
127 char name[1];
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000131 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800132 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000133 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800134 * Free all memory associated with a platform device. This function must
135 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000136 */
137void platform_device_put(struct platform_device *pdev)
138{
139 if (pdev)
140 put_device(&pdev->dev);
141}
142EXPORT_SYMBOL_GPL(platform_device_put);
143
144static void platform_device_release(struct device *dev)
145{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800146 struct platform_object *pa = container_of(dev, struct platform_object,
147 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000148
149 kfree(pa->pdev.dev.platform_data);
150 kfree(pa->pdev.resource);
151 kfree(pa);
152}
153
154/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000155 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800156 * @name: base name of the device we're adding
157 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000158 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800159 * Create a platform device object which can have other objects attached
160 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000161 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200162struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000163{
164 struct platform_object *pa;
165
166 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
167 if (pa) {
168 strcpy(pa->name, name);
169 pa->pdev.name = pa->name;
170 pa->pdev.id = id;
171 device_initialize(&pa->pdev.dev);
172 pa->pdev.dev.release = platform_device_release;
173 }
174
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500175 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000176}
177EXPORT_SYMBOL_GPL(platform_device_alloc);
178
179/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000180 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181 * @pdev: platform device allocated by platform_device_alloc to add resources to
182 * @res: set of resources that needs to be allocated for the device
183 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000184 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800185 * Add a copy of the resources to the platform device. The memory
186 * associated with the resources will be freed when the platform device is
187 * released.
Russell King37c12e72005-11-05 21:19:33 +0000188 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800189int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100190 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000191{
192 struct resource *r;
193
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200194 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000195 if (r) {
Russell King37c12e72005-11-05 21:19:33 +0000196 pdev->resource = r;
197 pdev->num_resources = num;
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200198 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000199 }
Uwe Kleine-König3e61dfd2010-06-15 10:47:55 +0200200 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000201}
202EXPORT_SYMBOL_GPL(platform_device_add_resources);
203
204/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000205 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800206 * @pdev: platform device allocated by platform_device_alloc to add resources to
207 * @data: platform specific data for this platform device
208 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000209 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800210 * Add a copy of platform specific data to the platform device's
211 * platform_data pointer. The memory associated with the platform data
212 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000213 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214int platform_device_add_data(struct platform_device *pdev, const void *data,
215 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000216{
Andrew Mortondaa41222009-08-06 16:00:44 -0700217 void *d = kmemdup(data, size, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000218
Russell King37c12e72005-11-05 21:19:33 +0000219 if (d) {
Russell King37c12e72005-11-05 21:19:33 +0000220 pdev->dev.platform_data = d;
Andrew Mortondaa41222009-08-06 16:00:44 -0700221 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000222 }
Andrew Mortondaa41222009-08-06 16:00:44 -0700223 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000224}
225EXPORT_SYMBOL_GPL(platform_device_add_data);
226
227/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800228 * platform_device_add - add a platform device to device hierarchy
229 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800231 * This is part 2 of platform_device_register(), though may be called
232 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Russell King37c12e72005-11-05 21:19:33 +0000234int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int i, ret = 0;
237
238 if (!pdev)
239 return -EINVAL;
240
241 if (!pdev->dev.parent)
242 pdev->dev.parent = &platform_bus;
243
244 pdev->dev.bus = &platform_bus_type;
245
246 if (pdev->id != -1)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100247 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700249 dev_set_name(&pdev->dev, "%s", pdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 for (i = 0; i < pdev->num_resources; i++) {
252 struct resource *p, *r = &pdev->resource[i];
253
254 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100255 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 p = r->parent;
258 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700259 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700261 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 p = &ioport_resource;
263 }
264
Kumar Galad960bb42005-11-28 10:15:39 -0600265 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 printk(KERN_ERR
267 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100268 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = -EBUSY;
270 goto failed;
271 }
272 }
273
274 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100275 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Russell Kinge3915532006-05-06 08:15:26 +0100277 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (ret == 0)
279 return ret;
280
281 failed:
Magnus Dammc9f66162008-10-15 22:05:15 -0700282 while (--i >= 0) {
283 struct resource *r = &pdev->resource[i];
284 unsigned long type = resource_type(r);
285
286 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
287 release_resource(r);
288 }
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
Russell King37c12e72005-11-05 21:19:33 +0000292EXPORT_SYMBOL_GPL(platform_device_add);
293
294/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800295 * platform_device_del - remove a platform-level device
296 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500297 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800298 * Note that this function will also release all memory- and port-based
299 * resources owned by the device (@dev->resource). This function must
300 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500301 */
302void platform_device_del(struct platform_device *pdev)
303{
304 int i;
305
306 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200307 device_del(&pdev->dev);
308
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500309 for (i = 0; i < pdev->num_resources; i++) {
310 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700311 unsigned long type = resource_type(r);
312
313 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500314 release_resource(r);
315 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500316 }
317}
318EXPORT_SYMBOL_GPL(platform_device_del);
319
320/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800321 * platform_device_register - add a platform-level device
322 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000323 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800324int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000325{
326 device_initialize(&pdev->dev);
327 return platform_device_add(pdev);
328}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500329EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800332 * platform_device_unregister - unregister a platform-level device
333 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800335 * Unregistration is done in 2 steps. First we release all resources
336 * and remove it from the subsystem, then we drop reference count by
337 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800339void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500341 platform_device_del(pdev);
342 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500344EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/**
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200347 * platform_device_register_resndata - add a platform-level device with
348 * resources and platform-specific data
349 *
350 * @parent: parent device for the device we're adding
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800351 * @name: base name of the device we're adding
352 * @id: instance id
353 * @res: set of resources that needs to be allocated for the device
354 * @num: number of resources
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700355 * @data: platform specific data for this platform device
356 * @size: size of platform specific data
357 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200358 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700359 */
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200360struct platform_device *platform_device_register_resndata(
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700361 struct device *parent,
362 const char *name, int id,
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200363 const struct resource *res, unsigned int num,
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700364 const void *data, size_t size)
365{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200366 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700367 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700368
369 pdev = platform_device_alloc(name, id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200370 if (!pdev)
371 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700372
373 pdev->dev.parent = parent;
374
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200375 if (res) {
376 ret = platform_device_add_resources(pdev, res, num);
377 if (ret)
378 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700379 }
380
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200381 if (data) {
382 ret = platform_device_add_data(pdev, data, size);
383 if (ret)
384 goto err;
385 }
386
387 ret = platform_device_add(pdev);
388 if (ret) {
389err:
390 platform_device_put(pdev);
391 return ERR_PTR(ret);
392 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700393
394 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700395}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200396EXPORT_SYMBOL_GPL(platform_device_register_resndata);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700397
Russell King00d3dcd2005-11-09 17:23:39 +0000398static int platform_drv_probe(struct device *_dev)
399{
400 struct platform_driver *drv = to_platform_driver(_dev->driver);
401 struct platform_device *dev = to_platform_device(_dev);
402
403 return drv->probe(dev);
404}
405
David Brownellc67334f2006-11-16 23:28:47 -0800406static int platform_drv_probe_fail(struct device *_dev)
407{
408 return -ENXIO;
409}
410
Russell King00d3dcd2005-11-09 17:23:39 +0000411static int platform_drv_remove(struct device *_dev)
412{
413 struct platform_driver *drv = to_platform_driver(_dev->driver);
414 struct platform_device *dev = to_platform_device(_dev);
415
416 return drv->remove(dev);
417}
418
419static void platform_drv_shutdown(struct device *_dev)
420{
421 struct platform_driver *drv = to_platform_driver(_dev->driver);
422 struct platform_device *dev = to_platform_device(_dev);
423
424 drv->shutdown(dev);
425}
426
Russell King00d3dcd2005-11-09 17:23:39 +0000427/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000428 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800429 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000430 */
431int platform_driver_register(struct platform_driver *drv)
432{
433 drv->driver.bus = &platform_bus_type;
434 if (drv->probe)
435 drv->driver.probe = platform_drv_probe;
436 if (drv->remove)
437 drv->driver.remove = platform_drv_remove;
438 if (drv->shutdown)
439 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200440
Russell King00d3dcd2005-11-09 17:23:39 +0000441 return driver_register(&drv->driver);
442}
443EXPORT_SYMBOL_GPL(platform_driver_register);
444
445/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000446 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800447 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000448 */
449void platform_driver_unregister(struct platform_driver *drv)
450{
451 driver_unregister(&drv->driver);
452}
453EXPORT_SYMBOL_GPL(platform_driver_unregister);
454
David Brownellc67334f2006-11-16 23:28:47 -0800455/**
456 * platform_driver_probe - register driver for non-hotpluggable device
457 * @drv: platform driver structure
458 * @probe: the driver probe routine, probably from an __init section
459 *
460 * Use this instead of platform_driver_register() when you know the device
461 * is not hotpluggable and has already been registered, and you want to
462 * remove its run-once probe() infrastructure from memory after the driver
463 * has bound to the device.
464 *
465 * One typical use for this would be with drivers for controllers integrated
466 * into system-on-chip processors, where the controller devices have been
467 * configured as part of board setup.
468 *
469 * Returns zero if the driver registered and bound to a device, else returns
470 * a negative error code and with the driver not registered.
471 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800472int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800473 int (*probe)(struct platform_device *))
474{
475 int retval, code;
476
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700477 /* make sure driver won't have bind/unbind attributes */
478 drv->driver.suppress_bind_attrs = true;
479
David Brownellc67334f2006-11-16 23:28:47 -0800480 /* temporary section violation during probe() */
481 drv->probe = probe;
482 retval = code = platform_driver_register(drv);
483
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700484 /*
485 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800486 * the list of drivers in order to probe new devices. Check to see
487 * if the probe was successful, and make sure any forced probes of
488 * new devices fail.
489 */
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700490 spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800491 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800492 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800493 retval = -ENODEV;
494 drv->driver.probe = platform_drv_probe_fail;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700495 spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800496
497 if (code != retval)
498 platform_driver_unregister(drv);
499 return retval;
500}
501EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800503/**
504 * platform_create_bundle - register driver and create corresponding device
505 * @driver: platform driver structure
506 * @probe: the driver probe routine, probably from an __init section
507 * @res: set of resources that needs to be allocated for the device
508 * @n_res: number of resources
509 * @data: platform specific data for this platform device
510 * @size: size of platform specific data
511 *
512 * Use this in legacy-style modules that probe hardware directly and
513 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200514 *
515 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800516 */
517struct platform_device * __init_or_module platform_create_bundle(
518 struct platform_driver *driver,
519 int (*probe)(struct platform_device *),
520 struct resource *res, unsigned int n_res,
521 const void *data, size_t size)
522{
523 struct platform_device *pdev;
524 int error;
525
526 pdev = platform_device_alloc(driver->driver.name, -1);
527 if (!pdev) {
528 error = -ENOMEM;
529 goto err_out;
530 }
531
532 if (res) {
533 error = platform_device_add_resources(pdev, res, n_res);
534 if (error)
535 goto err_pdev_put;
536 }
537
538 if (data) {
539 error = platform_device_add_data(pdev, data, size);
540 if (error)
541 goto err_pdev_put;
542 }
543
544 error = platform_device_add(pdev);
545 if (error)
546 goto err_pdev_put;
547
548 error = platform_driver_probe(driver, probe);
549 if (error)
550 goto err_pdev_del;
551
552 return pdev;
553
554err_pdev_del:
555 platform_device_del(pdev);
556err_pdev_put:
557 platform_device_put(pdev);
558err_out:
559 return ERR_PTR(error);
560}
561EXPORT_SYMBOL_GPL(platform_create_bundle);
562
David Brownella0245f72006-05-29 10:37:33 -0700563/* modalias support enables more hands-off userspace setup:
564 * (a) environment variable lets new-style hotplug events work once system is
565 * fully running: "modprobe $MODALIAS"
566 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
567 * mishandled before system is fully running: "modprobe $(cat modalias)"
568 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800569static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
570 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700571{
572 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200573 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700574
575 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
576}
577
578static struct device_attribute platform_dev_attrs[] = {
579 __ATTR_RO(modalias),
580 __ATTR_NULL,
581};
582
Kay Sievers7eff2e72007-08-14 15:15:12 +0200583static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700584{
585 struct platform_device *pdev = to_platform_device(dev);
586
Eric Miao57fee4a2009-02-04 11:52:40 +0800587 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
588 (pdev->id_entry) ? pdev->id_entry->name : pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700589 return 0;
590}
591
Eric Miao57fee4a2009-02-04 11:52:40 +0800592static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100593 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800594 struct platform_device *pdev)
595{
596 while (id->name[0]) {
597 if (strcmp(pdev->name, id->name) == 0) {
598 pdev->id_entry = id;
599 return id;
600 }
601 id++;
602 }
603 return NULL;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800607 * platform_match - bind platform device to platform driver.
608 * @dev: device.
609 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800611 * Platform device IDs are assumed to be encoded like this:
612 * "<name><instance>", where <name> is a short description of the type of
613 * device, like "pci" or "floppy", and <instance> is the enumerated
614 * instance of the device, like '0' or '42'. Driver IDs are simply
615 * "<name>". So, extract the <name> from the platform_device structure,
616 * and compare it against the name of the driver. Return whether they match
617 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800619static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800621 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800622 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Eric Miao57fee4a2009-02-04 11:52:40 +0800624 /* match against the id table first */
625 if (pdrv->id_table)
626 return platform_match_id(pdrv->id_table, pdev) != NULL;
627
628 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100629 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200632#ifdef CONFIG_PM_SLEEP
633
634static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200636 struct platform_driver *pdrv = to_platform_driver(dev->driver);
637 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int ret = 0;
639
Magnus Damm783ea7d2009-06-04 22:13:33 +0200640 if (dev->driver && pdrv->suspend)
641 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700642
643 return ret;
644}
645
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200646static int platform_legacy_resume(struct device *dev)
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->resume)
653 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return ret;
656}
657
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200658static int platform_pm_prepare(struct device *dev)
659{
660 struct device_driver *drv = dev->driver;
661 int ret = 0;
662
663 if (drv && drv->pm && drv->pm->prepare)
664 ret = drv->pm->prepare(dev);
665
666 return ret;
667}
668
669static void platform_pm_complete(struct device *dev)
670{
671 struct device_driver *drv = dev->driver;
672
673 if (drv && drv->pm && drv->pm->complete)
674 drv->pm->complete(dev);
675}
676
Magnus Damm9d730222009-08-20 20:25:32 +0200677#else /* !CONFIG_PM_SLEEP */
678
679#define platform_pm_prepare NULL
680#define platform_pm_complete NULL
681
682#endif /* !CONFIG_PM_SLEEP */
683
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200684#ifdef CONFIG_SUSPEND
685
Kevin Hilman190e8372010-03-17 16:18:15 -0700686int __weak platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200687{
688 struct device_driver *drv = dev->driver;
689 int ret = 0;
690
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200691 if (!drv)
692 return 0;
693
694 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200695 if (drv->pm->suspend)
696 ret = drv->pm->suspend(dev);
697 } else {
698 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
699 }
700
701 return ret;
702}
703
Kevin Hilman190e8372010-03-17 16:18:15 -0700704int __weak platform_pm_suspend_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200705{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200706 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200707 int ret = 0;
708
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200709 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200710 return 0;
711
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200712 if (drv->pm) {
713 if (drv->pm->suspend_noirq)
714 ret = drv->pm->suspend_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200715 }
716
717 return ret;
718}
719
Kevin Hilman190e8372010-03-17 16:18:15 -0700720int __weak platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200721{
722 struct device_driver *drv = dev->driver;
723 int ret = 0;
724
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200725 if (!drv)
726 return 0;
727
728 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200729 if (drv->pm->resume)
730 ret = drv->pm->resume(dev);
731 } else {
732 ret = platform_legacy_resume(dev);
733 }
734
735 return ret;
736}
737
Kevin Hilman190e8372010-03-17 16:18:15 -0700738int __weak platform_pm_resume_noirq(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200739{
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200740 struct device_driver *drv = dev->driver;
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200741 int ret = 0;
742
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200743 if (!drv)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200744 return 0;
745
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200746 if (drv->pm) {
747 if (drv->pm->resume_noirq)
748 ret = drv->pm->resume_noirq(dev);
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200749 }
750
751 return ret;
752}
753
754#else /* !CONFIG_SUSPEND */
755
756#define platform_pm_suspend NULL
757#define platform_pm_resume NULL
758#define platform_pm_suspend_noirq NULL
759#define platform_pm_resume_noirq NULL
760
761#endif /* !CONFIG_SUSPEND */
762
763#ifdef CONFIG_HIBERNATION
764
765static int platform_pm_freeze(struct device *dev)
766{
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
783static int platform_pm_freeze_noirq(struct device *dev)
784{
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
799static int platform_pm_thaw(struct device *dev)
800{
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
817static int platform_pm_thaw_noirq(struct device *dev)
818{
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
833static int platform_pm_poweroff(struct device *dev)
834{
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
851static int platform_pm_poweroff_noirq(struct device *dev)
852{
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
867static int platform_pm_restore(struct device *dev)
868{
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
885static int platform_pm_restore_noirq(struct device *dev)
886{
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
901#else /* !CONFIG_HIBERNATION */
902
903#define platform_pm_freeze NULL
904#define platform_pm_thaw NULL
905#define platform_pm_poweroff NULL
906#define platform_pm_restore NULL
907#define platform_pm_freeze_noirq NULL
908#define platform_pm_thaw_noirq NULL
909#define platform_pm_poweroff_noirq NULL
910#define platform_pm_restore_noirq NULL
911
912#endif /* !CONFIG_HIBERNATION */
913
Magnus Damm9d730222009-08-20 20:25:32 +0200914#ifdef CONFIG_PM_RUNTIME
915
916int __weak platform_pm_runtime_suspend(struct device *dev)
917{
Mark Brown543f25032010-05-10 23:10:13 +0200918 return pm_generic_runtime_suspend(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200919};
920
921int __weak platform_pm_runtime_resume(struct device *dev)
922{
Mark Brown543f25032010-05-10 23:10:13 +0200923 return pm_generic_runtime_resume(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200924};
925
926int __weak platform_pm_runtime_idle(struct device *dev)
927{
Mark Brown543f25032010-05-10 23:10:13 +0200928 return pm_generic_runtime_idle(dev);
Magnus Damm9d730222009-08-20 20:25:32 +0200929};
930
931#else /* !CONFIG_PM_RUNTIME */
932
933#define platform_pm_runtime_suspend NULL
934#define platform_pm_runtime_resume NULL
935#define platform_pm_runtime_idle NULL
936
937#endif /* !CONFIG_PM_RUNTIME */
938
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200939static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200940 .prepare = platform_pm_prepare,
941 .complete = platform_pm_complete,
942 .suspend = platform_pm_suspend,
943 .resume = platform_pm_resume,
944 .freeze = platform_pm_freeze,
945 .thaw = platform_pm_thaw,
946 .poweroff = platform_pm_poweroff,
947 .restore = platform_pm_restore,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200948 .suspend_noirq = platform_pm_suspend_noirq,
949 .resume_noirq = platform_pm_resume_noirq,
950 .freeze_noirq = platform_pm_freeze_noirq,
951 .thaw_noirq = platform_pm_thaw_noirq,
952 .poweroff_noirq = platform_pm_poweroff_noirq,
953 .restore_noirq = platform_pm_restore_noirq,
Magnus Damm9d730222009-08-20 20:25:32 +0200954 .runtime_suspend = platform_pm_runtime_suspend,
955 .runtime_resume = platform_pm_runtime_resume,
956 .runtime_idle = platform_pm_runtime_idle,
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200957};
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959struct bus_type platform_bus_type = {
960 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700961 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700963 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200964 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500966EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968int __init platform_bus_init(void)
969{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100970 int error;
971
Magnus Damm13977092009-03-30 14:37:25 -0700972 early_platform_cleanup();
973
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100974 error = device_register(&platform_bus);
975 if (error)
976 return error;
977 error = bus_register(&platform_bus_type);
978 if (error)
979 device_unregister(&platform_bus);
980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
984u64 dma_get_required_mask(struct device *dev)
985{
986 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
987 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
988 u64 mask;
989
990 if (!high_totalram) {
991 /* convert to mask just covering totalram */
992 low_totalram = (1 << (fls(low_totalram) - 1));
993 low_totalram += low_totalram - 1;
994 mask = low_totalram;
995 } else {
996 high_totalram = (1 << (fls(high_totalram) - 1));
997 high_totalram += high_totalram - 1;
998 mask = (((u64)high_totalram) << 32) + 0xffffffff;
999 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001000 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002EXPORT_SYMBOL_GPL(dma_get_required_mask);
1003#endif
Magnus Damm13977092009-03-30 14:37:25 -07001004
1005static __initdata LIST_HEAD(early_platform_driver_list);
1006static __initdata LIST_HEAD(early_platform_device_list);
1007
1008/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001009 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001010 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001011 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001012 *
1013 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001014 */
1015int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1016 char *buf)
1017{
Magnus Dammc60e0502009-11-27 17:38:51 +09001018 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001019 int n;
1020
1021 /* Simply add the driver to the end of the global list.
1022 * Drivers will by default be put on the list in compiled-in order.
1023 */
1024 if (!epdrv->list.next) {
1025 INIT_LIST_HEAD(&epdrv->list);
1026 list_add_tail(&epdrv->list, &early_platform_driver_list);
1027 }
1028
1029 /* If the user has specified device then make sure the driver
1030 * gets prioritized. The driver of the last device specified on
1031 * command line will be put first on the list.
1032 */
1033 n = strlen(epdrv->pdrv->driver.name);
1034 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1035 list_move(&epdrv->list, &early_platform_driver_list);
1036
Magnus Dammc60e0502009-11-27 17:38:51 +09001037 /* Allow passing parameters after device name */
1038 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001039 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001040 else {
1041 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1042 &tmp, 10);
1043
1044 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1045 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1046 n = 0;
1047 } else
1048 n += strcspn(&buf[n + 1], ",") + 1;
1049 }
1050
1051 if (buf[n] == ',')
1052 n++;
1053
1054 if (epdrv->bufsize) {
1055 memcpy(epdrv->buffer, &buf[n],
1056 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1057 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1058 }
Magnus Damm13977092009-03-30 14:37:25 -07001059 }
1060
1061 return 0;
1062}
1063
1064/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001065 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001066 * @devs: array of early platform devices to add
1067 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001068 *
1069 * Used by early architecture code to register early platform devices and
1070 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001071 */
1072void __init early_platform_add_devices(struct platform_device **devs, int num)
1073{
1074 struct device *dev;
1075 int i;
1076
1077 /* simply add the devices to list */
1078 for (i = 0; i < num; i++) {
1079 dev = &devs[i]->dev;
1080
1081 if (!dev->devres_head.next) {
1082 INIT_LIST_HEAD(&dev->devres_head);
1083 list_add_tail(&dev->devres_head,
1084 &early_platform_device_list);
1085 }
1086 }
1087}
1088
1089/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001090 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001091 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001092 *
1093 * Used by architecture code to register all early platform drivers
1094 * for a certain class. If omitted then only early platform drivers
1095 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001096 */
1097void __init early_platform_driver_register_all(char *class_str)
1098{
1099 /* The "class_str" parameter may or may not be present on the kernel
1100 * command line. If it is present then there may be more than one
1101 * matching parameter.
1102 *
1103 * Since we register our early platform drivers using early_param()
1104 * we need to make sure that they also get registered in the case
1105 * when the parameter is missing from the kernel command line.
1106 *
1107 * We use parse_early_options() to make sure the early_param() gets
1108 * called at least once. The early_param() may be called more than
1109 * once since the name of the preferred device may be specified on
1110 * the kernel command line. early_platform_driver_register() handles
1111 * this case for us.
1112 */
1113 parse_early_options(class_str);
1114}
1115
1116/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001117 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001118 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001119 * @id: id to match against
1120 */
1121static __init struct platform_device *
1122early_platform_match(struct early_platform_driver *epdrv, int id)
1123{
1124 struct platform_device *pd;
1125
1126 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1127 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1128 if (pd->id == id)
1129 return pd;
1130
1131 return NULL;
1132}
1133
1134/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001135 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001136 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001137 * @id: return true if id or above exists
1138 */
1139static __init int early_platform_left(struct early_platform_driver *epdrv,
1140 int id)
1141{
1142 struct platform_device *pd;
1143
1144 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1145 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1146 if (pd->id >= id)
1147 return 1;
1148
1149 return 0;
1150}
1151
1152/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001153 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001154 * @class_str: string to identify early platform driver class
1155 * @id: id to match against
1156 * @nr_probe: number of platform devices to successfully probe before exiting
1157 */
1158static int __init early_platform_driver_probe_id(char *class_str,
1159 int id,
1160 int nr_probe)
1161{
1162 struct early_platform_driver *epdrv;
1163 struct platform_device *match;
1164 int match_id;
1165 int n = 0;
1166 int left = 0;
1167
1168 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1169 /* only use drivers matching our class_str */
1170 if (strcmp(class_str, epdrv->class_str))
1171 continue;
1172
1173 if (id == -2) {
1174 match_id = epdrv->requested_id;
1175 left = 1;
1176
1177 } else {
1178 match_id = id;
1179 left += early_platform_left(epdrv, id);
1180
1181 /* skip requested id */
1182 switch (epdrv->requested_id) {
1183 case EARLY_PLATFORM_ID_ERROR:
1184 case EARLY_PLATFORM_ID_UNSET:
1185 break;
1186 default:
1187 if (epdrv->requested_id == id)
1188 match_id = EARLY_PLATFORM_ID_UNSET;
1189 }
1190 }
1191
1192 switch (match_id) {
1193 case EARLY_PLATFORM_ID_ERROR:
1194 pr_warning("%s: unable to parse %s parameter\n",
1195 class_str, epdrv->pdrv->driver.name);
1196 /* fall-through */
1197 case EARLY_PLATFORM_ID_UNSET:
1198 match = NULL;
1199 break;
1200 default:
1201 match = early_platform_match(epdrv, match_id);
1202 }
1203
1204 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001205 /*
1206 * Set up a sensible init_name to enable
1207 * dev_name() and others to be used before the
1208 * rest of the driver core is initialized.
1209 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001210 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001211 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001212 match->dev.init_name =
1213 kasprintf(GFP_KERNEL, "%s.%d",
1214 match->name,
1215 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001216 else
Paul Mundtbd050862010-03-29 15:51:35 +09001217 match->dev.init_name =
1218 kasprintf(GFP_KERNEL, "%s",
1219 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001220
Paul Mundta636ee72010-03-09 06:57:53 +00001221 if (!match->dev.init_name)
1222 return -ENOMEM;
1223 }
Paul Mundtbd050862010-03-29 15:51:35 +09001224
Magnus Damm13977092009-03-30 14:37:25 -07001225 if (epdrv->pdrv->probe(match))
1226 pr_warning("%s: unable to probe %s early.\n",
1227 class_str, match->name);
1228 else
1229 n++;
1230 }
1231
1232 if (n >= nr_probe)
1233 break;
1234 }
1235
1236 if (left)
1237 return n;
1238 else
1239 return -ENODEV;
1240}
1241
1242/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001243 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001244 * @class_str: string to identify early platform driver class
1245 * @nr_probe: number of platform devices to successfully probe before exiting
1246 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001247 *
1248 * Used by architecture code to probe registered early platform drivers
1249 * within a certain class. For probe to happen a registered early platform
1250 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001251 */
1252int __init early_platform_driver_probe(char *class_str,
1253 int nr_probe,
1254 int user_only)
1255{
1256 int k, n, i;
1257
1258 n = 0;
1259 for (i = -2; n < nr_probe; i++) {
1260 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1261
1262 if (k < 0)
1263 break;
1264
1265 n += k;
1266
1267 if (user_only)
1268 break;
1269 }
1270
1271 return n;
1272}
1273
1274/**
1275 * early_platform_cleanup - clean up early platform code
1276 */
1277void __init early_platform_cleanup(void)
1278{
1279 struct platform_device *pd, *pd2;
1280
1281 /* clean up the devres list used to chain devices */
1282 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1283 dev.devres_head) {
1284 list_del(&pd->dev.devres_head);
1285 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1286 }
1287}
1288