blob: 3f8077ce585ca52ea99bf7251b446fb53f1cc7ca [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>
Jean Delvare689ae232012-07-27 22:14:59 +020023#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010025#include "base.h"
26
Jean Delvare689ae232012-07-27 22:14:59 +020027/* For automatically allocated device IDs */
28static DEFINE_IDA(platform_devid_ida);
29
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080030#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
31 driver))
Russell King00d3dcd2005-11-09 17:23:39 +000032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010034 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050036EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/**
Kumar Galaa77ce812011-06-10 01:52:57 -050039 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
Randy Dunlap7de636f2011-07-27 12:11:25 -070040 * @pdev: platform device
Kumar Galaa77ce812011-06-10 01:52:57 -050041 *
42 * This is called before platform_device_add() such that any pdev_archdata may
43 * be setup before the platform_notifier is called. So if a user needs to
44 * manipulate any relevant information in the pdev_archdata they can do:
45 *
46 * platform_devic_alloc()
47 * ... manipulate ...
48 * platform_device_add()
49 *
50 * And if they don't care they can just call platform_device_register() and
51 * everything will just work out.
52 */
53void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
54{
55}
56
57/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080058 * platform_get_resource - get a resource for a device
59 * @dev: platform device
60 * @type: resource type
61 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080063struct resource *platform_get_resource(struct platform_device *dev,
64 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 int i;
67
68 for (i = 0; i < dev->num_resources; i++) {
69 struct resource *r = &dev->resource[i];
70
Magnus Dammc9f66162008-10-15 22:05:15 -070071 if (type == resource_type(r) && num-- == 0)
72 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 return NULL;
75}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050076EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080079 * platform_get_irq - get an IRQ for a device
80 * @dev: platform device
81 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83int platform_get_irq(struct platform_device *dev, unsigned int num)
84{
85 struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
86
David Vrabel305b3222006-01-19 17:52:27 +000087 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050089EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080092 * platform_get_resource_byname - get a resource for a device by name
93 * @dev: platform device
94 * @type: resource type
95 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080097struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +020098 unsigned int type,
99 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int i;
102
103 for (i = 0; i < dev->num_resources; i++) {
104 struct resource *r = &dev->resource[i];
105
Magnus Dammc9f66162008-10-15 22:05:15 -0700106 if (type == resource_type(r) && !strcmp(r->name, name))
107 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 return NULL;
110}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500111EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800114 * platform_get_irq - get an IRQ for a device
115 * @dev: platform device
116 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200118int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800120 struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
121 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
David Vrabel305b3222006-01-19 17:52:27 +0000123 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500125EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800128 * platform_add_devices - add a numbers of platform devices
129 * @devs: array of platform devices to add
130 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132int platform_add_devices(struct platform_device **devs, int num)
133{
134 int i, ret = 0;
135
136 for (i = 0; i < num; i++) {
137 ret = platform_device_register(devs[i]);
138 if (ret) {
139 while (--i >= 0)
140 platform_device_unregister(devs[i]);
141 break;
142 }
143 }
144
145 return ret;
146}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500147EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Russell King37c12e72005-11-05 21:19:33 +0000149struct platform_object {
150 struct platform_device pdev;
151 char name[1];
152};
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000155 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800156 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000157 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800158 * Free all memory associated with a platform device. This function must
159 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000160 */
161void platform_device_put(struct platform_device *pdev)
162{
163 if (pdev)
164 put_device(&pdev->dev);
165}
166EXPORT_SYMBOL_GPL(platform_device_put);
167
168static void platform_device_release(struct device *dev)
169{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800170 struct platform_object *pa = container_of(dev, struct platform_object,
171 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000172
Grant Likely7096d042010-10-20 11:45:13 -0600173 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000174 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200175 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000176 kfree(pa->pdev.resource);
177 kfree(pa);
178}
179
180/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000181 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800182 * @name: base name of the device we're adding
183 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000184 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800185 * Create a platform device object which can have other objects attached
186 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000187 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200188struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000189{
190 struct platform_object *pa;
191
192 pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
193 if (pa) {
194 strcpy(pa->name, name);
195 pa->pdev.name = pa->name;
196 pa->pdev.id = id;
197 device_initialize(&pa->pdev.dev);
198 pa->pdev.dev.release = platform_device_release;
Kumar Galaa77ce812011-06-10 01:52:57 -0500199 arch_setup_pdev_archdata(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000200 }
201
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500202 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000203}
204EXPORT_SYMBOL_GPL(platform_device_alloc);
205
206/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000207 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800208 * @pdev: platform device allocated by platform_device_alloc to add resources to
209 * @res: set of resources that needs to be allocated for the device
210 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000211 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800212 * Add a copy of the resources to the platform device. The memory
213 * associated with the resources will be freed when the platform device is
214 * released.
Russell King37c12e72005-11-05 21:19:33 +0000215 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100217 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000218{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200219 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000220
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200221 if (res) {
222 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
223 if (!r)
224 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000225 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200226
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200227 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200228 pdev->resource = r;
229 pdev->num_resources = num;
230 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000231}
232EXPORT_SYMBOL_GPL(platform_device_add_resources);
233
234/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000235 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800236 * @pdev: platform device allocated by platform_device_alloc to add resources to
237 * @data: platform specific data for this platform device
238 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000239 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800240 * Add a copy of platform specific data to the platform device's
241 * platform_data pointer. The memory associated with the platform data
242 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000243 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244int platform_device_add_data(struct platform_device *pdev, const void *data,
245 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000246{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200247 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000248
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200249 if (data) {
250 d = kmemdup(data, size, GFP_KERNEL);
251 if (!d)
252 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000253 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200254
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200255 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200256 pdev->dev.platform_data = d;
257 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000258}
259EXPORT_SYMBOL_GPL(platform_device_add_data);
260
261/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800262 * platform_device_add - add a platform device to device hierarchy
263 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800265 * This is part 2 of platform_device_register(), though may be called
266 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Russell King37c12e72005-11-05 21:19:33 +0000268int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Jean Delvare689ae232012-07-27 22:14:59 +0200270 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!pdev)
273 return -EINVAL;
274
275 if (!pdev->dev.parent)
276 pdev->dev.parent = &platform_bus;
277
278 pdev->dev.bus = &platform_bus_type;
279
Jean Delvare689ae232012-07-27 22:14:59 +0200280 switch (pdev->id) {
281 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100282 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200283 break;
284 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700285 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200286 break;
287 case PLATFORM_DEVID_AUTO:
288 /*
289 * Automatically allocated device ID. We mark it as such so
290 * that we remember it must be freed, and we append a suffix
291 * to avoid namespace collision with explicit IDs.
292 */
293 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
294 if (ret < 0)
295 goto err_out;
296 pdev->id = ret;
297 pdev->id_auto = true;
298 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
299 break;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 for (i = 0; i < pdev->num_resources; i++) {
303 struct resource *p, *r = &pdev->resource[i];
304
305 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100306 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 p = r->parent;
309 if (!p) {
Magnus Dammc9f66162008-10-15 22:05:15 -0700310 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 p = &iomem_resource;
Magnus Dammc9f66162008-10-15 22:05:15 -0700312 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 p = &ioport_resource;
314 }
315
Kumar Galad960bb42005-11-28 10:15:39 -0600316 if (p && insert_resource(p, r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 printk(KERN_ERR
318 "%s: failed to claim resource %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100319 dev_name(&pdev->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ret = -EBUSY;
321 goto failed;
322 }
323 }
324
325 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100326 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Russell Kinge3915532006-05-06 08:15:26 +0100328 ret = device_add(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (ret == 0)
330 return ret;
331
332 failed:
Jean Delvare689ae232012-07-27 22:14:59 +0200333 if (pdev->id_auto) {
334 ida_simple_remove(&platform_devid_ida, pdev->id);
335 pdev->id = PLATFORM_DEVID_AUTO;
336 }
337
Magnus Dammc9f66162008-10-15 22:05:15 -0700338 while (--i >= 0) {
339 struct resource *r = &pdev->resource[i];
340 unsigned long type = resource_type(r);
341
342 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
343 release_resource(r);
344 }
345
Jean Delvare689ae232012-07-27 22:14:59 +0200346 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return ret;
348}
Russell King37c12e72005-11-05 21:19:33 +0000349EXPORT_SYMBOL_GPL(platform_device_add);
350
351/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800352 * platform_device_del - remove a platform-level device
353 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500354 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800355 * Note that this function will also release all memory- and port-based
356 * resources owned by the device (@dev->resource). This function must
357 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500358 */
359void platform_device_del(struct platform_device *pdev)
360{
361 int i;
362
363 if (pdev) {
Jean Delvaredc4c15d2007-05-02 20:55:54 +0200364 device_del(&pdev->dev);
365
Jean Delvare689ae232012-07-27 22:14:59 +0200366 if (pdev->id_auto) {
367 ida_simple_remove(&platform_devid_ida, pdev->id);
368 pdev->id = PLATFORM_DEVID_AUTO;
369 }
370
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500371 for (i = 0; i < pdev->num_resources; i++) {
372 struct resource *r = &pdev->resource[i];
Magnus Dammc9f66162008-10-15 22:05:15 -0700373 unsigned long type = resource_type(r);
374
375 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500376 release_resource(r);
377 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500378 }
379}
380EXPORT_SYMBOL_GPL(platform_device_del);
381
382/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800383 * platform_device_register - add a platform-level device
384 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000385 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800386int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000387{
388 device_initialize(&pdev->dev);
Kumar Galaa77ce812011-06-10 01:52:57 -0500389 arch_setup_pdev_archdata(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000390 return platform_device_add(pdev);
391}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500392EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800395 * platform_device_unregister - unregister a platform-level device
396 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800398 * Unregistration is done in 2 steps. First we release all resources
399 * and remove it from the subsystem, then we drop reference count by
400 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800402void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500404 platform_device_del(pdev);
405 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500407EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200410 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200411 * resources and platform-specific data
412 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200413 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700414 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200415 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700416 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200417struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100418 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700419{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200420 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700421 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700422
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200423 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200424 if (!pdev)
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200425 goto err_alloc;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700426
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200427 pdev->dev.parent = pdevinfo->parent;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700428
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200429 if (pdevinfo->dma_mask) {
430 /*
431 * This memory isn't freed when the device is put,
432 * I don't have a nice idea for that though. Conceptually
433 * dma_mask in struct device should not be a pointer.
434 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
435 */
436 pdev->dev.dma_mask =
437 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
438 if (!pdev->dev.dma_mask)
439 goto err;
440
441 *pdev->dev.dma_mask = pdevinfo->dma_mask;
442 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
443 }
444
445 ret = platform_device_add_resources(pdev,
446 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400447 if (ret)
448 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700449
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200450 ret = platform_device_add_data(pdev,
451 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400452 if (ret)
453 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200454
455 ret = platform_device_add(pdev);
456 if (ret) {
457err:
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200458 kfree(pdev->dev.dma_mask);
459
460err_alloc:
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200461 platform_device_put(pdev);
462 return ERR_PTR(ret);
463 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700464
465 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700466}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200467EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700468
Russell King00d3dcd2005-11-09 17:23:39 +0000469static int platform_drv_probe(struct device *_dev)
470{
471 struct platform_driver *drv = to_platform_driver(_dev->driver);
472 struct platform_device *dev = to_platform_device(_dev);
473
474 return drv->probe(dev);
475}
476
David Brownellc67334f2006-11-16 23:28:47 -0800477static int platform_drv_probe_fail(struct device *_dev)
478{
479 return -ENXIO;
480}
481
Russell King00d3dcd2005-11-09 17:23:39 +0000482static int platform_drv_remove(struct device *_dev)
483{
484 struct platform_driver *drv = to_platform_driver(_dev->driver);
485 struct platform_device *dev = to_platform_device(_dev);
486
487 return drv->remove(dev);
488}
489
490static void platform_drv_shutdown(struct device *_dev)
491{
492 struct platform_driver *drv = to_platform_driver(_dev->driver);
493 struct platform_device *dev = to_platform_device(_dev);
494
495 drv->shutdown(dev);
496}
497
Russell King00d3dcd2005-11-09 17:23:39 +0000498/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000499 * platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800500 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000501 */
502int platform_driver_register(struct platform_driver *drv)
503{
504 drv->driver.bus = &platform_bus_type;
505 if (drv->probe)
506 drv->driver.probe = platform_drv_probe;
507 if (drv->remove)
508 drv->driver.remove = platform_drv_remove;
509 if (drv->shutdown)
510 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200511
Russell King00d3dcd2005-11-09 17:23:39 +0000512 return driver_register(&drv->driver);
513}
514EXPORT_SYMBOL_GPL(platform_driver_register);
515
516/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000517 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800518 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000519 */
520void platform_driver_unregister(struct platform_driver *drv)
521{
522 driver_unregister(&drv->driver);
523}
524EXPORT_SYMBOL_GPL(platform_driver_unregister);
525
David Brownellc67334f2006-11-16 23:28:47 -0800526/**
527 * platform_driver_probe - register driver for non-hotpluggable device
528 * @drv: platform driver structure
529 * @probe: the driver probe routine, probably from an __init section
530 *
531 * Use this instead of platform_driver_register() when you know the device
532 * is not hotpluggable and has already been registered, and you want to
533 * remove its run-once probe() infrastructure from memory after the driver
534 * has bound to the device.
535 *
536 * One typical use for this would be with drivers for controllers integrated
537 * into system-on-chip processors, where the controller devices have been
538 * configured as part of board setup.
539 *
540 * Returns zero if the driver registered and bound to a device, else returns
541 * a negative error code and with the driver not registered.
542 */
Andrew Mortonc63e0782006-12-04 14:56:36 -0800543int __init_or_module platform_driver_probe(struct platform_driver *drv,
David Brownellc67334f2006-11-16 23:28:47 -0800544 int (*probe)(struct platform_device *))
545{
546 int retval, code;
547
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700548 /* make sure driver won't have bind/unbind attributes */
549 drv->driver.suppress_bind_attrs = true;
550
David Brownellc67334f2006-11-16 23:28:47 -0800551 /* temporary section violation during probe() */
552 drv->probe = probe;
553 retval = code = platform_driver_register(drv);
554
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700555 /*
556 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800557 * the list of drivers in order to probe new devices. Check to see
558 * if the probe was successful, and make sure any forced probes of
559 * new devices fail.
560 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700561 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800562 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800563 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800564 retval = -ENODEV;
565 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700566 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800567
568 if (code != retval)
569 platform_driver_unregister(drv);
570 return retval;
571}
572EXPORT_SYMBOL_GPL(platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800574/**
575 * platform_create_bundle - register driver and create corresponding device
576 * @driver: platform driver structure
577 * @probe: the driver probe routine, probably from an __init section
578 * @res: set of resources that needs to be allocated for the device
579 * @n_res: number of resources
580 * @data: platform specific data for this platform device
581 * @size: size of platform specific data
582 *
583 * Use this in legacy-style modules that probe hardware directly and
584 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200585 *
586 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800587 */
588struct platform_device * __init_or_module platform_create_bundle(
589 struct platform_driver *driver,
590 int (*probe)(struct platform_device *),
591 struct resource *res, unsigned int n_res,
592 const void *data, size_t size)
593{
594 struct platform_device *pdev;
595 int error;
596
597 pdev = platform_device_alloc(driver->driver.name, -1);
598 if (!pdev) {
599 error = -ENOMEM;
600 goto err_out;
601 }
602
Anton Vorontsov807508c2010-09-07 17:31:54 +0400603 error = platform_device_add_resources(pdev, res, n_res);
604 if (error)
605 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800606
Anton Vorontsov807508c2010-09-07 17:31:54 +0400607 error = platform_device_add_data(pdev, data, size);
608 if (error)
609 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800610
611 error = platform_device_add(pdev);
612 if (error)
613 goto err_pdev_put;
614
615 error = platform_driver_probe(driver, probe);
616 if (error)
617 goto err_pdev_del;
618
619 return pdev;
620
621err_pdev_del:
622 platform_device_del(pdev);
623err_pdev_put:
624 platform_device_put(pdev);
625err_out:
626 return ERR_PTR(error);
627}
628EXPORT_SYMBOL_GPL(platform_create_bundle);
629
David Brownella0245f72006-05-29 10:37:33 -0700630/* modalias support enables more hands-off userspace setup:
631 * (a) environment variable lets new-style hotplug events work once system is
632 * fully running: "modprobe $MODALIAS"
633 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
634 * mishandled before system is fully running: "modprobe $(cat modalias)"
635 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800636static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
637 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700638{
639 struct platform_device *pdev = to_platform_device(dev);
Kay Sievers43cc71e2007-08-18 04:40:39 +0200640 int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700641
642 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
643}
644
645static struct device_attribute platform_dev_attrs[] = {
646 __ATTR_RO(modalias),
647 __ATTR_NULL,
648};
649
Kay Sievers7eff2e72007-08-14 15:15:12 +0200650static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700651{
652 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600653 int rc;
654
655 /* Some devices have extra OF data and an OF-style MODALIAS */
Grant Likely07d57a32012-02-01 11:22:22 -0700656 rc = of_device_uevent_modalias(dev,env);
Grant Likelyeca39302010-06-08 07:48:21 -0600657 if (rc != -ENODEV)
658 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700659
Eric Miao57fee4a2009-02-04 11:52:40 +0800660 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Sebastian Andrzej Siewior0a268132011-08-15 16:51:22 +0200661 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700662 return 0;
663}
664
Eric Miao57fee4a2009-02-04 11:52:40 +0800665static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100666 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800667 struct platform_device *pdev)
668{
669 while (id->name[0]) {
670 if (strcmp(pdev->name, id->name) == 0) {
671 pdev->id_entry = id;
672 return id;
673 }
674 id++;
675 }
676 return NULL;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800680 * platform_match - bind platform device to platform driver.
681 * @dev: device.
682 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800684 * Platform device IDs are assumed to be encoded like this:
685 * "<name><instance>", where <name> is a short description of the type of
686 * device, like "pci" or "floppy", and <instance> is the enumerated
687 * instance of the device, like '0' or '42'. Driver IDs are simply
688 * "<name>". So, extract the <name> from the platform_device structure,
689 * and compare it against the name of the driver. Return whether they match
690 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800692static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800694 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800695 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Grant Likely05212152010-06-08 07:48:20 -0600697 /* Attempt an OF style match first */
698 if (of_driver_match_device(dev, drv))
699 return 1;
700
701 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800702 if (pdrv->id_table)
703 return platform_match_id(pdrv->id_table, pdev) != NULL;
704
705 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100706 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200709#ifdef CONFIG_PM_SLEEP
710
711static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200713 struct platform_driver *pdrv = to_platform_driver(dev->driver);
714 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int ret = 0;
716
Magnus Damm783ea7d2009-06-04 22:13:33 +0200717 if (dev->driver && pdrv->suspend)
718 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d2006-09-03 13:16:45 -0700719
720 return ret;
721}
722
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200723static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200725 struct platform_driver *pdrv = to_platform_driver(dev->driver);
726 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int ret = 0;
728
Magnus Damm783ea7d2009-06-04 22:13:33 +0200729 if (dev->driver && pdrv->resume)
730 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -0700731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return ret;
733}
734
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200735#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +0200736
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200737#ifdef CONFIG_SUSPEND
738
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200739int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200740{
741 struct device_driver *drv = dev->driver;
742 int ret = 0;
743
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200744 if (!drv)
745 return 0;
746
747 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200748 if (drv->pm->suspend)
749 ret = drv->pm->suspend(dev);
750 } else {
751 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
752 }
753
754 return ret;
755}
756
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200757int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200758{
759 struct device_driver *drv = dev->driver;
760 int ret = 0;
761
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200762 if (!drv)
763 return 0;
764
765 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200766 if (drv->pm->resume)
767 ret = drv->pm->resume(dev);
768 } else {
769 ret = platform_legacy_resume(dev);
770 }
771
772 return ret;
773}
774
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200775#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200776
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200777#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200778
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200779int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200780{
781 struct device_driver *drv = dev->driver;
782 int ret = 0;
783
784 if (!drv)
785 return 0;
786
787 if (drv->pm) {
788 if (drv->pm->freeze)
789 ret = drv->pm->freeze(dev);
790 } else {
791 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
792 }
793
794 return ret;
795}
796
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200797int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200798{
799 struct device_driver *drv = dev->driver;
800 int ret = 0;
801
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200802 if (!drv)
803 return 0;
804
805 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200806 if (drv->pm->thaw)
807 ret = drv->pm->thaw(dev);
808 } else {
809 ret = platform_legacy_resume(dev);
810 }
811
812 return ret;
813}
814
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200815int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200816{
817 struct device_driver *drv = dev->driver;
818 int ret = 0;
819
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200820 if (!drv)
821 return 0;
822
823 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200824 if (drv->pm->poweroff)
825 ret = drv->pm->poweroff(dev);
826 } else {
827 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
828 }
829
830 return ret;
831}
832
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200833int platform_pm_restore(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->restore)
843 ret = drv->pm->restore(dev);
844 } else {
845 ret = platform_legacy_resume(dev);
846 }
847
848 return ret;
849}
850
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200851#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200852
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +0200853static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +0200854 .runtime_suspend = pm_generic_runtime_suspend,
855 .runtime_resume = pm_generic_runtime_resume,
856 .runtime_idle = pm_generic_runtime_idle,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200857 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200858};
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860struct bus_type platform_bus_type = {
861 .name = "platform",
David Brownella0245f72006-05-29 10:37:33 -0700862 .dev_attrs = platform_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -0700864 .uevent = platform_uevent,
Magnus Damm9d730222009-08-20 20:25:32 +0200865 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500867EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869int __init platform_bus_init(void)
870{
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100871 int error;
872
Magnus Damm13977092009-03-30 14:37:25 -0700873 early_platform_cleanup();
874
Cornelia Huckfbfb1442006-11-27 10:35:08 +0100875 error = device_register(&platform_bus);
876 if (error)
877 return error;
878 error = bus_register(&platform_bus_type);
879 if (error)
880 device_unregister(&platform_bus);
881 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
885u64 dma_get_required_mask(struct device *dev)
886{
887 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
888 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
889 u64 mask;
890
891 if (!high_totalram) {
892 /* convert to mask just covering totalram */
893 low_totalram = (1 << (fls(low_totalram) - 1));
894 low_totalram += low_totalram - 1;
895 mask = low_totalram;
896 } else {
897 high_totalram = (1 << (fls(high_totalram) - 1));
898 high_totalram += high_totalram - 1;
899 mask = (((u64)high_totalram) << 32) + 0xffffffff;
900 }
James Bottomleye88a0c22008-03-09 11:57:56 -0500901 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903EXPORT_SYMBOL_GPL(dma_get_required_mask);
904#endif
Magnus Damm13977092009-03-30 14:37:25 -0700905
906static __initdata LIST_HEAD(early_platform_driver_list);
907static __initdata LIST_HEAD(early_platform_device_list);
908
909/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900910 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -0700911 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -0700912 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +0900913 *
914 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -0700915 */
916int __init early_platform_driver_register(struct early_platform_driver *epdrv,
917 char *buf)
918{
Magnus Dammc60e0502009-11-27 17:38:51 +0900919 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -0700920 int n;
921
922 /* Simply add the driver to the end of the global list.
923 * Drivers will by default be put on the list in compiled-in order.
924 */
925 if (!epdrv->list.next) {
926 INIT_LIST_HEAD(&epdrv->list);
927 list_add_tail(&epdrv->list, &early_platform_driver_list);
928 }
929
930 /* If the user has specified device then make sure the driver
931 * gets prioritized. The driver of the last device specified on
932 * command line will be put first on the list.
933 */
934 n = strlen(epdrv->pdrv->driver.name);
935 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
936 list_move(&epdrv->list, &early_platform_driver_list);
937
Magnus Dammc60e0502009-11-27 17:38:51 +0900938 /* Allow passing parameters after device name */
939 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -0700940 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +0900941 else {
942 epdrv->requested_id = simple_strtoul(&buf[n + 1],
943 &tmp, 10);
944
945 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
946 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
947 n = 0;
948 } else
949 n += strcspn(&buf[n + 1], ",") + 1;
950 }
951
952 if (buf[n] == ',')
953 n++;
954
955 if (epdrv->bufsize) {
956 memcpy(epdrv->buffer, &buf[n],
957 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
958 epdrv->buffer[epdrv->bufsize - 1] = '\0';
959 }
Magnus Damm13977092009-03-30 14:37:25 -0700960 }
961
962 return 0;
963}
964
965/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900966 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -0700967 * @devs: array of early platform devices to add
968 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +0900969 *
970 * Used by early architecture code to register early platform devices and
971 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -0700972 */
973void __init early_platform_add_devices(struct platform_device **devs, int num)
974{
975 struct device *dev;
976 int i;
977
978 /* simply add the devices to list */
979 for (i = 0; i < num; i++) {
980 dev = &devs[i]->dev;
981
982 if (!dev->devres_head.next) {
983 INIT_LIST_HEAD(&dev->devres_head);
984 list_add_tail(&dev->devres_head,
985 &early_platform_device_list);
986 }
987 }
988}
989
990/**
Magnus Damm4d26e1392010-03-10 20:50:38 +0900991 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -0700992 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +0900993 *
994 * Used by architecture code to register all early platform drivers
995 * for a certain class. If omitted then only early platform drivers
996 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -0700997 */
998void __init early_platform_driver_register_all(char *class_str)
999{
1000 /* The "class_str" parameter may or may not be present on the kernel
1001 * command line. If it is present then there may be more than one
1002 * matching parameter.
1003 *
1004 * Since we register our early platform drivers using early_param()
1005 * we need to make sure that they also get registered in the case
1006 * when the parameter is missing from the kernel command line.
1007 *
1008 * We use parse_early_options() to make sure the early_param() gets
1009 * called at least once. The early_param() may be called more than
1010 * once since the name of the preferred device may be specified on
1011 * the kernel command line. early_platform_driver_register() handles
1012 * this case for us.
1013 */
1014 parse_early_options(class_str);
1015}
1016
1017/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001018 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001019 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001020 * @id: id to match against
1021 */
1022static __init struct platform_device *
1023early_platform_match(struct early_platform_driver *epdrv, int id)
1024{
1025 struct platform_device *pd;
1026
1027 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1028 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1029 if (pd->id == id)
1030 return pd;
1031
1032 return NULL;
1033}
1034
1035/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001036 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001037 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001038 * @id: return true if id or above exists
1039 */
1040static __init int early_platform_left(struct early_platform_driver *epdrv,
1041 int id)
1042{
1043 struct platform_device *pd;
1044
1045 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1046 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1047 if (pd->id >= id)
1048 return 1;
1049
1050 return 0;
1051}
1052
1053/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001054 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001055 * @class_str: string to identify early platform driver class
1056 * @id: id to match against
1057 * @nr_probe: number of platform devices to successfully probe before exiting
1058 */
1059static int __init early_platform_driver_probe_id(char *class_str,
1060 int id,
1061 int nr_probe)
1062{
1063 struct early_platform_driver *epdrv;
1064 struct platform_device *match;
1065 int match_id;
1066 int n = 0;
1067 int left = 0;
1068
1069 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1070 /* only use drivers matching our class_str */
1071 if (strcmp(class_str, epdrv->class_str))
1072 continue;
1073
1074 if (id == -2) {
1075 match_id = epdrv->requested_id;
1076 left = 1;
1077
1078 } else {
1079 match_id = id;
1080 left += early_platform_left(epdrv, id);
1081
1082 /* skip requested id */
1083 switch (epdrv->requested_id) {
1084 case EARLY_PLATFORM_ID_ERROR:
1085 case EARLY_PLATFORM_ID_UNSET:
1086 break;
1087 default:
1088 if (epdrv->requested_id == id)
1089 match_id = EARLY_PLATFORM_ID_UNSET;
1090 }
1091 }
1092
1093 switch (match_id) {
1094 case EARLY_PLATFORM_ID_ERROR:
1095 pr_warning("%s: unable to parse %s parameter\n",
1096 class_str, epdrv->pdrv->driver.name);
1097 /* fall-through */
1098 case EARLY_PLATFORM_ID_UNSET:
1099 match = NULL;
1100 break;
1101 default:
1102 match = early_platform_match(epdrv, match_id);
1103 }
1104
1105 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001106 /*
1107 * Set up a sensible init_name to enable
1108 * dev_name() and others to be used before the
1109 * rest of the driver core is initialized.
1110 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001111 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001112 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001113 match->dev.init_name =
1114 kasprintf(GFP_KERNEL, "%s.%d",
1115 match->name,
1116 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001117 else
Paul Mundtbd050862010-03-29 15:51:35 +09001118 match->dev.init_name =
1119 kasprintf(GFP_KERNEL, "%s",
1120 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001121
Paul Mundta636ee72010-03-09 06:57:53 +00001122 if (!match->dev.init_name)
1123 return -ENOMEM;
1124 }
Paul Mundtbd050862010-03-29 15:51:35 +09001125
Magnus Damm13977092009-03-30 14:37:25 -07001126 if (epdrv->pdrv->probe(match))
1127 pr_warning("%s: unable to probe %s early.\n",
1128 class_str, match->name);
1129 else
1130 n++;
1131 }
1132
1133 if (n >= nr_probe)
1134 break;
1135 }
1136
1137 if (left)
1138 return n;
1139 else
1140 return -ENODEV;
1141}
1142
1143/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001144 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001145 * @class_str: string to identify early platform driver class
1146 * @nr_probe: number of platform devices to successfully probe before exiting
1147 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001148 *
1149 * Used by architecture code to probe registered early platform drivers
1150 * within a certain class. For probe to happen a registered early platform
1151 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001152 */
1153int __init early_platform_driver_probe(char *class_str,
1154 int nr_probe,
1155 int user_only)
1156{
1157 int k, n, i;
1158
1159 n = 0;
1160 for (i = -2; n < nr_probe; i++) {
1161 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1162
1163 if (k < 0)
1164 break;
1165
1166 n += k;
1167
1168 if (user_only)
1169 break;
1170 }
1171
1172 return n;
1173}
1174
1175/**
1176 * early_platform_cleanup - clean up early platform code
1177 */
1178void __init early_platform_cleanup(void)
1179{
1180 struct platform_device *pd, *pd2;
1181
1182 /* clean up the devres list used to chain devices */
1183 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1184 dev.devres_head) {
1185 list_del(&pd->dev.devres_head);
1186 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1187 }
1188}
1189