| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 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 |  | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> | 
 | 15 | #include <linux/init.h> | 
 | 16 | #include <linux/dma-mapping.h> | 
 | 17 | #include <linux/bootmem.h> | 
 | 18 | #include <linux/err.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
| Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 21 | #include "base.h" | 
 | 22 |  | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 23 | #define to_platform_driver(drv)	(container_of((drv), struct platform_driver, \ | 
 | 24 | 				 driver)) | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 25 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | struct device platform_bus = { | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 27 | 	.init_name	= "platform", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 29 | EXPORT_SYMBOL_GPL(platform_bus); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
 | 31 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 32 |  * platform_get_resource - get a resource for a device | 
 | 33 |  * @dev: platform device | 
 | 34 |  * @type: resource type | 
 | 35 |  * @num: resource index | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 37 | struct resource *platform_get_resource(struct platform_device *dev, | 
 | 38 | 				       unsigned int type, unsigned int num) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { | 
 | 40 | 	int i; | 
 | 41 |  | 
 | 42 | 	for (i = 0; i < dev->num_resources; i++) { | 
 | 43 | 		struct resource *r = &dev->resource[i]; | 
 | 44 |  | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 45 | 		if (type == resource_type(r) && num-- == 0) | 
 | 46 | 			return r; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | 	} | 
 | 48 | 	return NULL; | 
 | 49 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 50 | EXPORT_SYMBOL_GPL(platform_get_resource); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
 | 52 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 53 |  * platform_get_irq - get an IRQ for a device | 
 | 54 |  * @dev: platform device | 
 | 55 |  * @num: IRQ number index | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  */ | 
 | 57 | int platform_get_irq(struct platform_device *dev, unsigned int num) | 
 | 58 | { | 
 | 59 | 	struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); | 
 | 60 |  | 
| David Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 61 | 	return r ? r->start : -ENXIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(platform_get_irq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
 | 65 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 66 |  * platform_get_resource_byname - get a resource for a device by name | 
 | 67 |  * @dev: platform device | 
 | 68 |  * @type: resource type | 
 | 69 |  * @name: resource name | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 71 | struct resource *platform_get_resource_byname(struct platform_device *dev, | 
| Linus Walleij | c0afe7b | 2009-04-27 02:38:16 +0200 | [diff] [blame] | 72 | 					      unsigned int type, | 
 | 73 | 					      const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { | 
 | 75 | 	int i; | 
 | 76 |  | 
 | 77 | 	for (i = 0; i < dev->num_resources; i++) { | 
 | 78 | 		struct resource *r = &dev->resource[i]; | 
 | 79 |  | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 80 | 		if (type == resource_type(r) && !strcmp(r->name, name)) | 
 | 81 | 			return r; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | 	} | 
 | 83 | 	return NULL; | 
 | 84 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(platform_get_resource_byname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
 | 87 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 88 |  * platform_get_irq - get an IRQ for a device | 
 | 89 |  * @dev: platform device | 
 | 90 |  * @name: IRQ name | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  */ | 
| Linus Walleij | c0afe7b | 2009-04-27 02:38:16 +0200 | [diff] [blame] | 92 | int platform_get_irq_byname(struct platform_device *dev, const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 94 | 	struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, | 
 | 95 | 							  name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 |  | 
| David Vrabel | 305b322 | 2006-01-19 17:52:27 +0000 | [diff] [blame] | 97 | 	return r ? r->start : -ENXIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 99 | EXPORT_SYMBOL_GPL(platform_get_irq_byname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 |  | 
 | 101 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 102 |  * platform_add_devices - add a numbers of platform devices | 
 | 103 |  * @devs: array of platform devices to add | 
 | 104 |  * @num: number of platform devices in array | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 |  */ | 
 | 106 | int platform_add_devices(struct platform_device **devs, int num) | 
 | 107 | { | 
 | 108 | 	int i, ret = 0; | 
 | 109 |  | 
 | 110 | 	for (i = 0; i < num; i++) { | 
 | 111 | 		ret = platform_device_register(devs[i]); | 
 | 112 | 		if (ret) { | 
 | 113 | 			while (--i >= 0) | 
 | 114 | 				platform_device_unregister(devs[i]); | 
 | 115 | 			break; | 
 | 116 | 		} | 
 | 117 | 	} | 
 | 118 |  | 
 | 119 | 	return ret; | 
 | 120 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(platform_add_devices); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 123 | struct platform_object { | 
 | 124 | 	struct platform_device pdev; | 
 | 125 | 	char name[1]; | 
 | 126 | }; | 
 | 127 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 129 |  * platform_device_put | 
 | 130 |  * @pdev: platform device to free | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 131 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 132 |  * Free all memory associated with a platform device.  This function must | 
 | 133 |  * _only_ be externally called in error cases.  All other usage is a bug. | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 134 |  */ | 
 | 135 | void platform_device_put(struct platform_device *pdev) | 
 | 136 | { | 
 | 137 | 	if (pdev) | 
 | 138 | 		put_device(&pdev->dev); | 
 | 139 | } | 
 | 140 | EXPORT_SYMBOL_GPL(platform_device_put); | 
 | 141 |  | 
 | 142 | static void platform_device_release(struct device *dev) | 
 | 143 | { | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 144 | 	struct platform_object *pa = container_of(dev, struct platform_object, | 
 | 145 | 						  pdev.dev); | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 146 |  | 
 | 147 | 	kfree(pa->pdev.dev.platform_data); | 
 | 148 | 	kfree(pa->pdev.resource); | 
 | 149 | 	kfree(pa); | 
 | 150 | } | 
 | 151 |  | 
 | 152 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 153 |  * platform_device_alloc | 
 | 154 |  * @name: base name of the device we're adding | 
 | 155 |  * @id: instance id | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 156 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 157 |  * Create a platform device object which can have other objects attached | 
 | 158 |  * to it, and which will have attached objects freed when it is released. | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 159 |  */ | 
| Jean Delvare | 1359555 | 2007-09-09 12:54:16 +0200 | [diff] [blame] | 160 | struct platform_device *platform_device_alloc(const char *name, int id) | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 161 | { | 
 | 162 | 	struct platform_object *pa; | 
 | 163 |  | 
 | 164 | 	pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); | 
 | 165 | 	if (pa) { | 
 | 166 | 		strcpy(pa->name, name); | 
 | 167 | 		pa->pdev.name = pa->name; | 
 | 168 | 		pa->pdev.id = id; | 
 | 169 | 		device_initialize(&pa->pdev.dev); | 
 | 170 | 		pa->pdev.dev.release = platform_device_release; | 
 | 171 | 	} | 
 | 172 |  | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 173 | 	return pa ? &pa->pdev : NULL; | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 174 | } | 
 | 175 | EXPORT_SYMBOL_GPL(platform_device_alloc); | 
 | 176 |  | 
 | 177 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 178 |  * platform_device_add_resources | 
 | 179 |  * @pdev: platform device allocated by platform_device_alloc to add resources to | 
 | 180 |  * @res: set of resources that needs to be allocated for the device | 
 | 181 |  * @num: number of resources | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 182 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 183 |  * Add a copy of the resources to the platform device.  The memory | 
 | 184 |  * associated with the resources will be freed when the platform device is | 
 | 185 |  * released. | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 186 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 187 | int platform_device_add_resources(struct platform_device *pdev, | 
 | 188 | 				  struct resource *res, unsigned int num) | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 189 | { | 
 | 190 | 	struct resource *r; | 
 | 191 |  | 
 | 192 | 	r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); | 
 | 193 | 	if (r) { | 
 | 194 | 		memcpy(r, res, sizeof(struct resource) * num); | 
 | 195 | 		pdev->resource = r; | 
 | 196 | 		pdev->num_resources = num; | 
 | 197 | 	} | 
 | 198 | 	return r ? 0 : -ENOMEM; | 
 | 199 | } | 
 | 200 | EXPORT_SYMBOL_GPL(platform_device_add_resources); | 
 | 201 |  | 
 | 202 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 203 |  * platform_device_add_data | 
 | 204 |  * @pdev: platform device allocated by platform_device_alloc to add resources to | 
 | 205 |  * @data: platform specific data for this platform device | 
 | 206 |  * @size: size of platform specific data | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 207 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 208 |  * Add a copy of platform specific data to the platform device's | 
 | 209 |  * platform_data pointer.  The memory associated with the platform data | 
 | 210 |  * will be freed when the platform device is released. | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 211 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 212 | int platform_device_add_data(struct platform_device *pdev, const void *data, | 
 | 213 | 			     size_t size) | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 214 | { | 
 | 215 | 	void *d; | 
 | 216 |  | 
 | 217 | 	d = kmalloc(size, GFP_KERNEL); | 
 | 218 | 	if (d) { | 
 | 219 | 		memcpy(d, data, size); | 
 | 220 | 		pdev->dev.platform_data = d; | 
 | 221 | 	} | 
 | 222 | 	return d ? 0 : -ENOMEM; | 
 | 223 | } | 
 | 224 | EXPORT_SYMBOL_GPL(platform_device_add_data); | 
 | 225 |  | 
 | 226 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 227 |  * platform_device_add - add a platform device to device hierarchy | 
 | 228 |  * @pdev: platform device we're adding | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 230 |  * This is part 2 of platform_device_register(), though may be called | 
 | 231 |  * separately _iff_ pdev was allocated by platform_device_alloc(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 |  */ | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 233 | int platform_device_add(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { | 
 | 235 | 	int i, ret = 0; | 
 | 236 |  | 
 | 237 | 	if (!pdev) | 
 | 238 | 		return -EINVAL; | 
 | 239 |  | 
 | 240 | 	if (!pdev->dev.parent) | 
 | 241 | 		pdev->dev.parent = &platform_bus; | 
 | 242 |  | 
 | 243 | 	pdev->dev.bus = &platform_bus_type; | 
 | 244 |  | 
 | 245 | 	if (pdev->id != -1) | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 246 | 		dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | 	else | 
| Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 248 | 		dev_set_name(&pdev->dev, "%s", pdev->name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 |  | 
 | 250 | 	for (i = 0; i < pdev->num_resources; i++) { | 
 | 251 | 		struct resource *p, *r = &pdev->resource[i]; | 
 | 252 |  | 
 | 253 | 		if (r->name == NULL) | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 254 | 			r->name = dev_name(&pdev->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 |  | 
 | 256 | 		p = r->parent; | 
 | 257 | 		if (!p) { | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 258 | 			if (resource_type(r) == IORESOURCE_MEM) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | 				p = &iomem_resource; | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 260 | 			else if (resource_type(r) == IORESOURCE_IO) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | 				p = &ioport_resource; | 
 | 262 | 		} | 
 | 263 |  | 
| Kumar Gala | d960bb4 | 2005-11-28 10:15:39 -0600 | [diff] [blame] | 264 | 		if (p && insert_resource(p, r)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | 			printk(KERN_ERR | 
 | 266 | 			       "%s: failed to claim resource %d\n", | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 267 | 			       dev_name(&pdev->dev), i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 			ret = -EBUSY; | 
 | 269 | 			goto failed; | 
 | 270 | 		} | 
 | 271 | 	} | 
 | 272 |  | 
 | 273 | 	pr_debug("Registering platform device '%s'. Parent at %s\n", | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 274 | 		 dev_name(&pdev->dev), dev_name(pdev->dev.parent)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 |  | 
| Russell King | e391553 | 2006-05-06 08:15:26 +0100 | [diff] [blame] | 276 | 	ret = device_add(&pdev->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 	if (ret == 0) | 
 | 278 | 		return ret; | 
 | 279 |  | 
 | 280 |  failed: | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 281 | 	while (--i >= 0) { | 
 | 282 | 		struct resource *r = &pdev->resource[i]; | 
 | 283 | 		unsigned long type = resource_type(r); | 
 | 284 |  | 
 | 285 | 		if (type == IORESOURCE_MEM || type == IORESOURCE_IO) | 
 | 286 | 			release_resource(r); | 
 | 287 | 	} | 
 | 288 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | 	return ret; | 
 | 290 | } | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(platform_device_add); | 
 | 292 |  | 
 | 293 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 294 |  * platform_device_del - remove a platform-level device | 
 | 295 |  * @pdev: platform device we're removing | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 296 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 297 |  * Note that this function will also release all memory- and port-based | 
 | 298 |  * resources owned by the device (@dev->resource).  This function must | 
 | 299 |  * _only_ be externally called in error cases.  All other usage is a bug. | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 300 |  */ | 
 | 301 | void platform_device_del(struct platform_device *pdev) | 
 | 302 | { | 
 | 303 | 	int i; | 
 | 304 |  | 
 | 305 | 	if (pdev) { | 
| Jean Delvare | dc4c15d | 2007-05-02 20:55:54 +0200 | [diff] [blame] | 306 | 		device_del(&pdev->dev); | 
 | 307 |  | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 308 | 		for (i = 0; i < pdev->num_resources; i++) { | 
 | 309 | 			struct resource *r = &pdev->resource[i]; | 
| Magnus Damm | c9f6616 | 2008-10-15 22:05:15 -0700 | [diff] [blame] | 310 | 			unsigned long type = resource_type(r); | 
 | 311 |  | 
 | 312 | 			if (type == IORESOURCE_MEM || type == IORESOURCE_IO) | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 313 | 				release_resource(r); | 
 | 314 | 		} | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 315 | 	} | 
 | 316 | } | 
 | 317 | EXPORT_SYMBOL_GPL(platform_device_del); | 
 | 318 |  | 
 | 319 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 320 |  * platform_device_register - add a platform-level device | 
 | 321 |  * @pdev: platform device we're adding | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 322 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 323 | int platform_device_register(struct platform_device *pdev) | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 324 | { | 
 | 325 | 	device_initialize(&pdev->dev); | 
 | 326 | 	return platform_device_add(pdev); | 
 | 327 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 328 | EXPORT_SYMBOL_GPL(platform_device_register); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 |  | 
 | 330 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 331 |  * platform_device_unregister - unregister a platform-level device | 
 | 332 |  * @pdev: platform device we're unregistering | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 334 |  * Unregistration is done in 2 steps. First we release all resources | 
 | 335 |  * and remove it from the subsystem, then we drop reference count by | 
 | 336 |  * calling platform_device_put(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 338 | void platform_device_unregister(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { | 
| Dmitry Torokhov | 93ce306 | 2005-12-10 01:36:27 -0500 | [diff] [blame] | 340 | 	platform_device_del(pdev); | 
 | 341 | 	platform_device_put(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(platform_device_unregister); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 346 |  * platform_device_register_simple | 
 | 347 |  * @name: base name of the device we're adding | 
 | 348 |  * @id: instance id | 
 | 349 |  * @res: set of resources that needs to be allocated for the device | 
 | 350 |  * @num: number of resources | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 352 |  * This function creates a simple platform device that requires minimal | 
 | 353 |  * resource and memory management. Canned release function freeing memory | 
 | 354 |  * allocated for the device allows drivers using such devices to be | 
 | 355 |  * unloaded without waiting for the last reference to the device to be | 
 | 356 |  * dropped. | 
| David Brownell | 49a4ec1 | 2007-05-08 00:29:39 -0700 | [diff] [blame] | 357 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 358 |  * This interface is primarily intended for use with legacy drivers which | 
 | 359 |  * probe hardware directly.  Because such drivers create sysfs device nodes | 
 | 360 |  * themselves, rather than letting system infrastructure handle such device | 
 | 361 |  * enumeration tasks, they don't fully conform to the Linux driver model. | 
 | 362 |  * In particular, when such drivers are built as modules, they can't be | 
 | 363 |  * "hotplugged". | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 365 | struct platform_device *platform_device_register_simple(const char *name, | 
 | 366 | 							int id, | 
 | 367 | 							struct resource *res, | 
 | 368 | 							unsigned int num) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 370 | 	struct platform_device *pdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 	int retval; | 
 | 372 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 373 | 	pdev = platform_device_alloc(name, id); | 
 | 374 | 	if (!pdev) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | 		retval = -ENOMEM; | 
 | 376 | 		goto error; | 
 | 377 | 	} | 
 | 378 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | 	if (num) { | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 380 | 		retval = platform_device_add_resources(pdev, res, num); | 
 | 381 | 		if (retval) | 
 | 382 | 			goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | 	} | 
 | 384 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 385 | 	retval = platform_device_add(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | 	if (retval) | 
 | 387 | 		goto error; | 
 | 388 |  | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 389 | 	return pdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
 | 391 | error: | 
| Russell King | 37c12e7 | 2005-11-05 21:19:33 +0000 | [diff] [blame] | 392 | 	platform_device_put(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | 	return ERR_PTR(retval); | 
 | 394 | } | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 395 | EXPORT_SYMBOL_GPL(platform_device_register_simple); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 |  | 
| Dmitry Baryshkov | d8bf254 | 2008-09-22 14:41:40 -0700 | [diff] [blame] | 397 | /** | 
 | 398 |  * platform_device_register_data | 
 | 399 |  * @parent: parent device for the device we're adding | 
 | 400 |  * @name: base name of the device we're adding | 
 | 401 |  * @id: instance id | 
 | 402 |  * @data: platform specific data for this platform device | 
 | 403 |  * @size: size of platform specific data | 
 | 404 |  * | 
 | 405 |  * This function creates a simple platform device that requires minimal | 
 | 406 |  * resource and memory management. Canned release function freeing memory | 
 | 407 |  * allocated for the device allows drivers using such devices to be | 
 | 408 |  * unloaded without waiting for the last reference to the device to be | 
 | 409 |  * dropped. | 
 | 410 |  */ | 
 | 411 | struct platform_device *platform_device_register_data( | 
 | 412 | 		struct device *parent, | 
 | 413 | 		const char *name, int id, | 
 | 414 | 		const void *data, size_t size) | 
 | 415 | { | 
 | 416 | 	struct platform_device *pdev; | 
 | 417 | 	int retval; | 
 | 418 |  | 
 | 419 | 	pdev = platform_device_alloc(name, id); | 
 | 420 | 	if (!pdev) { | 
 | 421 | 		retval = -ENOMEM; | 
 | 422 | 		goto error; | 
 | 423 | 	} | 
 | 424 |  | 
 | 425 | 	pdev->dev.parent = parent; | 
 | 426 |  | 
 | 427 | 	if (size) { | 
 | 428 | 		retval = platform_device_add_data(pdev, data, size); | 
 | 429 | 		if (retval) | 
 | 430 | 			goto error; | 
 | 431 | 	} | 
 | 432 |  | 
 | 433 | 	retval = platform_device_add(pdev); | 
 | 434 | 	if (retval) | 
 | 435 | 		goto error; | 
 | 436 |  | 
 | 437 | 	return pdev; | 
 | 438 |  | 
 | 439 | error: | 
 | 440 | 	platform_device_put(pdev); | 
 | 441 | 	return ERR_PTR(retval); | 
 | 442 | } | 
 | 443 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 444 | static int platform_drv_probe(struct device *_dev) | 
 | 445 | { | 
 | 446 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 447 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 448 |  | 
 | 449 | 	return drv->probe(dev); | 
 | 450 | } | 
 | 451 |  | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 452 | static int platform_drv_probe_fail(struct device *_dev) | 
 | 453 | { | 
 | 454 | 	return -ENXIO; | 
 | 455 | } | 
 | 456 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 457 | static int platform_drv_remove(struct device *_dev) | 
 | 458 | { | 
 | 459 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 460 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 461 |  | 
 | 462 | 	return drv->remove(dev); | 
 | 463 | } | 
 | 464 |  | 
 | 465 | static void platform_drv_shutdown(struct device *_dev) | 
 | 466 | { | 
 | 467 | 	struct platform_driver *drv = to_platform_driver(_dev->driver); | 
 | 468 | 	struct platform_device *dev = to_platform_device(_dev); | 
 | 469 |  | 
 | 470 | 	drv->shutdown(dev); | 
 | 471 | } | 
 | 472 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 473 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 474 |  * platform_driver_register | 
 | 475 |  * @drv: platform driver structure | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 476 |  */ | 
 | 477 | int platform_driver_register(struct platform_driver *drv) | 
 | 478 | { | 
 | 479 | 	drv->driver.bus = &platform_bus_type; | 
 | 480 | 	if (drv->probe) | 
 | 481 | 		drv->driver.probe = platform_drv_probe; | 
 | 482 | 	if (drv->remove) | 
 | 483 | 		drv->driver.remove = platform_drv_remove; | 
 | 484 | 	if (drv->shutdown) | 
 | 485 | 		drv->driver.shutdown = platform_drv_shutdown; | 
| Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 486 | 	if (drv->suspend || drv->resume) | 
 | 487 | 		pr_warning("Platform driver '%s' needs updating - please use " | 
 | 488 | 			"dev_pm_ops\n", drv->driver.name); | 
 | 489 |  | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 490 | 	return driver_register(&drv->driver); | 
 | 491 | } | 
 | 492 | EXPORT_SYMBOL_GPL(platform_driver_register); | 
 | 493 |  | 
 | 494 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 495 |  * platform_driver_unregister | 
 | 496 |  * @drv: platform driver structure | 
| Russell King | 00d3dcd | 2005-11-09 17:23:39 +0000 | [diff] [blame] | 497 |  */ | 
 | 498 | void platform_driver_unregister(struct platform_driver *drv) | 
 | 499 | { | 
 | 500 | 	driver_unregister(&drv->driver); | 
 | 501 | } | 
 | 502 | EXPORT_SYMBOL_GPL(platform_driver_unregister); | 
 | 503 |  | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 504 | /** | 
 | 505 |  * platform_driver_probe - register driver for non-hotpluggable device | 
 | 506 |  * @drv: platform driver structure | 
 | 507 |  * @probe: the driver probe routine, probably from an __init section | 
 | 508 |  * | 
 | 509 |  * Use this instead of platform_driver_register() when you know the device | 
 | 510 |  * is not hotpluggable and has already been registered, and you want to | 
 | 511 |  * remove its run-once probe() infrastructure from memory after the driver | 
 | 512 |  * has bound to the device. | 
 | 513 |  * | 
 | 514 |  * One typical use for this would be with drivers for controllers integrated | 
 | 515 |  * into system-on-chip processors, where the controller devices have been | 
 | 516 |  * configured as part of board setup. | 
 | 517 |  * | 
 | 518 |  * Returns zero if the driver registered and bound to a device, else returns | 
 | 519 |  * a negative error code and with the driver not registered. | 
 | 520 |  */ | 
| Andrew Morton | c63e078 | 2006-12-04 14:56:36 -0800 | [diff] [blame] | 521 | int __init_or_module platform_driver_probe(struct platform_driver *drv, | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 522 | 		int (*probe)(struct platform_device *)) | 
 | 523 | { | 
 | 524 | 	int retval, code; | 
 | 525 |  | 
 | 526 | 	/* temporary section violation during probe() */ | 
 | 527 | 	drv->probe = probe; | 
 | 528 | 	retval = code = platform_driver_register(drv); | 
 | 529 |  | 
 | 530 | 	/* Fixup that section violation, being paranoid about code scanning | 
 | 531 | 	 * the list of drivers in order to probe new devices.  Check to see | 
 | 532 | 	 * if the probe was successful, and make sure any forced probes of | 
 | 533 | 	 * new devices fail. | 
 | 534 | 	 */ | 
| Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 535 | 	spin_lock(&platform_bus_type.p->klist_drivers.k_lock); | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 536 | 	drv->probe = NULL; | 
| Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 537 | 	if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 538 | 		retval = -ENODEV; | 
 | 539 | 	drv->driver.probe = platform_drv_probe_fail; | 
| Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 540 | 	spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); | 
| David Brownell | c67334f | 2006-11-16 23:28:47 -0800 | [diff] [blame] | 541 |  | 
 | 542 | 	if (code != retval) | 
 | 543 | 		platform_driver_unregister(drv); | 
 | 544 | 	return retval; | 
 | 545 | } | 
 | 546 | EXPORT_SYMBOL_GPL(platform_driver_probe); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 |  | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 548 | /* modalias support enables more hands-off userspace setup: | 
 | 549 |  * (a) environment variable lets new-style hotplug events work once system is | 
 | 550 |  *     fully running:  "modprobe $MODALIAS" | 
 | 551 |  * (b) sysfs attribute lets new-style coldplug recover from hotplug events | 
 | 552 |  *     mishandled before system is fully running:  "modprobe $(cat modalias)" | 
 | 553 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 554 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, | 
 | 555 | 			     char *buf) | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 556 | { | 
 | 557 | 	struct platform_device	*pdev = to_platform_device(dev); | 
| Kay Sievers | 43cc71e | 2007-08-18 04:40:39 +0200 | [diff] [blame] | 558 | 	int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 559 |  | 
 | 560 | 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; | 
 | 561 | } | 
 | 562 |  | 
 | 563 | static struct device_attribute platform_dev_attrs[] = { | 
 | 564 | 	__ATTR_RO(modalias), | 
 | 565 | 	__ATTR_NULL, | 
 | 566 | }; | 
 | 567 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 568 | static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 569 | { | 
 | 570 | 	struct platform_device	*pdev = to_platform_device(dev); | 
 | 571 |  | 
| Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 572 | 	add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, | 
 | 573 | 		(pdev->id_entry) ? pdev->id_entry->name : pdev->name); | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 574 | 	return 0; | 
 | 575 | } | 
 | 576 |  | 
| Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 577 | static const struct platform_device_id *platform_match_id( | 
 | 578 | 			struct platform_device_id *id, | 
 | 579 | 			struct platform_device *pdev) | 
 | 580 | { | 
 | 581 | 	while (id->name[0]) { | 
 | 582 | 		if (strcmp(pdev->name, id->name) == 0) { | 
 | 583 | 			pdev->id_entry = id; | 
 | 584 | 			return id; | 
 | 585 | 		} | 
 | 586 | 		id++; | 
 | 587 | 	} | 
 | 588 | 	return NULL; | 
 | 589 | } | 
 | 590 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | /** | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 592 |  * platform_match - bind platform device to platform driver. | 
 | 593 |  * @dev: device. | 
 | 594 |  * @drv: driver. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 |  * | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 596 |  * Platform device IDs are assumed to be encoded like this: | 
 | 597 |  * "<name><instance>", where <name> is a short description of the type of | 
 | 598 |  * device, like "pci" or "floppy", and <instance> is the enumerated | 
 | 599 |  * instance of the device, like '0' or '42'.  Driver IDs are simply | 
 | 600 |  * "<name>".  So, extract the <name> from the platform_device structure, | 
 | 601 |  * and compare it against the name of the driver. Return whether they match | 
 | 602 |  * or not. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 |  */ | 
| Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 604 | static int platform_match(struct device *dev, struct device_driver *drv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { | 
| Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 606 | 	struct platform_device *pdev = to_platform_device(dev); | 
| Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 607 | 	struct platform_driver *pdrv = to_platform_driver(drv); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 |  | 
| Eric Miao | 57fee4a | 2009-02-04 11:52:40 +0800 | [diff] [blame] | 609 | 	/* match against the id table first */ | 
 | 610 | 	if (pdrv->id_table) | 
 | 611 | 		return platform_match_id(pdrv->id_table, pdev) != NULL; | 
 | 612 |  | 
 | 613 | 	/* fall-back to driver name match */ | 
| Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 614 | 	return (strcmp(pdev->name, drv->name) == 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } | 
 | 616 |  | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 617 | #ifdef CONFIG_PM_SLEEP | 
 | 618 |  | 
 | 619 | static int platform_legacy_suspend(struct device *dev, pm_message_t mesg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { | 
| Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 621 | 	struct platform_driver *pdrv = to_platform_driver(dev->driver); | 
 | 622 | 	struct platform_device *pdev = to_platform_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | 	int ret = 0; | 
 | 624 |  | 
| Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 625 | 	if (dev->driver && pdrv->suspend) | 
 | 626 | 		ret = pdrv->suspend(pdev, mesg); | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 627 |  | 
 | 628 | 	return ret; | 
 | 629 | } | 
 | 630 |  | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 631 | static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg) | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 632 | { | 
| Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 633 | 	struct platform_driver *pdrv = to_platform_driver(dev->driver); | 
 | 634 | 	struct platform_device *pdev = to_platform_device(dev); | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 635 | 	int ret = 0; | 
 | 636 |  | 
| Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 637 | 	if (dev->driver && pdrv->suspend_late) | 
 | 638 | 		ret = pdrv->suspend_late(pdev, mesg); | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 639 |  | 
 | 640 | 	return ret; | 
 | 641 | } | 
 | 642 |  | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 643 | static int platform_legacy_resume_early(struct device *dev) | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 644 | { | 
| Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 645 | 	struct platform_driver *pdrv = to_platform_driver(dev->driver); | 
 | 646 | 	struct platform_device *pdev = to_platform_device(dev); | 
| David Brownell | 386415d8 | 2006-09-03 13:16:45 -0700 | [diff] [blame] | 647 | 	int ret = 0; | 
 | 648 |  | 
| Eric Miao | 71b3e0c | 2009-01-31 22:47:44 +0800 | [diff] [blame] | 649 | 	if (dev->driver && pdrv->resume_early) | 
 | 650 | 		ret = pdrv->resume_early(pdev); | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 651 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | 	return ret; | 
 | 653 | } | 
 | 654 |  | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 655 | static int platform_legacy_resume(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { | 
| Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 657 | 	struct platform_driver *pdrv = to_platform_driver(dev->driver); | 
 | 658 | 	struct platform_device *pdev = to_platform_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | 	int ret = 0; | 
 | 660 |  | 
| Magnus Damm | 783ea7d | 2009-06-04 22:13:33 +0200 | [diff] [blame] | 661 | 	if (dev->driver && pdrv->resume) | 
 | 662 | 		ret = pdrv->resume(pdev); | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 663 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | 	return ret; | 
 | 665 | } | 
 | 666 |  | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 667 | static int platform_pm_prepare(struct device *dev) | 
 | 668 | { | 
 | 669 | 	struct device_driver *drv = dev->driver; | 
 | 670 | 	int ret = 0; | 
 | 671 |  | 
 | 672 | 	if (drv && drv->pm && drv->pm->prepare) | 
 | 673 | 		ret = drv->pm->prepare(dev); | 
 | 674 |  | 
 | 675 | 	return ret; | 
 | 676 | } | 
 | 677 |  | 
 | 678 | static void platform_pm_complete(struct device *dev) | 
 | 679 | { | 
 | 680 | 	struct device_driver *drv = dev->driver; | 
 | 681 |  | 
 | 682 | 	if (drv && drv->pm && drv->pm->complete) | 
 | 683 | 		drv->pm->complete(dev); | 
 | 684 | } | 
 | 685 |  | 
 | 686 | #ifdef CONFIG_SUSPEND | 
 | 687 |  | 
 | 688 | static int platform_pm_suspend(struct device *dev) | 
 | 689 | { | 
 | 690 | 	struct device_driver *drv = dev->driver; | 
 | 691 | 	int ret = 0; | 
 | 692 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 693 | 	if (!drv) | 
 | 694 | 		return 0; | 
 | 695 |  | 
 | 696 | 	if (drv->pm) { | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 697 | 		if (drv->pm->suspend) | 
 | 698 | 			ret = drv->pm->suspend(dev); | 
 | 699 | 	} else { | 
 | 700 | 		ret = platform_legacy_suspend(dev, PMSG_SUSPEND); | 
 | 701 | 	} | 
 | 702 |  | 
 | 703 | 	return ret; | 
 | 704 | } | 
 | 705 |  | 
 | 706 | static int platform_pm_suspend_noirq(struct device *dev) | 
 | 707 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 708 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 709 | 	int ret = 0; | 
 | 710 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 711 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 712 | 		return 0; | 
 | 713 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 714 | 	if (drv->pm) { | 
 | 715 | 		if (drv->pm->suspend_noirq) | 
 | 716 | 			ret = drv->pm->suspend_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 717 | 	} else { | 
 | 718 | 		ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND); | 
 | 719 | 	} | 
 | 720 |  | 
 | 721 | 	return ret; | 
 | 722 | } | 
 | 723 |  | 
 | 724 | static int platform_pm_resume(struct device *dev) | 
 | 725 | { | 
 | 726 | 	struct device_driver *drv = dev->driver; | 
 | 727 | 	int ret = 0; | 
 | 728 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 729 | 	if (!drv) | 
 | 730 | 		return 0; | 
 | 731 |  | 
 | 732 | 	if (drv->pm) { | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 733 | 		if (drv->pm->resume) | 
 | 734 | 			ret = drv->pm->resume(dev); | 
 | 735 | 	} else { | 
 | 736 | 		ret = platform_legacy_resume(dev); | 
 | 737 | 	} | 
 | 738 |  | 
 | 739 | 	return ret; | 
 | 740 | } | 
 | 741 |  | 
 | 742 | static int platform_pm_resume_noirq(struct device *dev) | 
 | 743 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 744 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 745 | 	int ret = 0; | 
 | 746 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 747 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 748 | 		return 0; | 
 | 749 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 750 | 	if (drv->pm) { | 
 | 751 | 		if (drv->pm->resume_noirq) | 
 | 752 | 			ret = drv->pm->resume_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 753 | 	} else { | 
 | 754 | 		ret = platform_legacy_resume_early(dev); | 
 | 755 | 	} | 
 | 756 |  | 
 | 757 | 	return ret; | 
 | 758 | } | 
 | 759 |  | 
 | 760 | #else /* !CONFIG_SUSPEND */ | 
 | 761 |  | 
 | 762 | #define platform_pm_suspend		NULL | 
 | 763 | #define platform_pm_resume		NULL | 
 | 764 | #define platform_pm_suspend_noirq	NULL | 
 | 765 | #define platform_pm_resume_noirq	NULL | 
 | 766 |  | 
 | 767 | #endif /* !CONFIG_SUSPEND */ | 
 | 768 |  | 
 | 769 | #ifdef CONFIG_HIBERNATION | 
 | 770 |  | 
 | 771 | static int platform_pm_freeze(struct device *dev) | 
 | 772 | { | 
 | 773 | 	struct device_driver *drv = dev->driver; | 
 | 774 | 	int ret = 0; | 
 | 775 |  | 
 | 776 | 	if (!drv) | 
 | 777 | 		return 0; | 
 | 778 |  | 
 | 779 | 	if (drv->pm) { | 
 | 780 | 		if (drv->pm->freeze) | 
 | 781 | 			ret = drv->pm->freeze(dev); | 
 | 782 | 	} else { | 
 | 783 | 		ret = platform_legacy_suspend(dev, PMSG_FREEZE); | 
 | 784 | 	} | 
 | 785 |  | 
 | 786 | 	return ret; | 
 | 787 | } | 
 | 788 |  | 
 | 789 | static int platform_pm_freeze_noirq(struct device *dev) | 
 | 790 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 791 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 792 | 	int ret = 0; | 
 | 793 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 794 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 795 | 		return 0; | 
 | 796 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 797 | 	if (drv->pm) { | 
 | 798 | 		if (drv->pm->freeze_noirq) | 
 | 799 | 			ret = drv->pm->freeze_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 800 | 	} else { | 
 | 801 | 		ret = platform_legacy_suspend_late(dev, PMSG_FREEZE); | 
 | 802 | 	} | 
 | 803 |  | 
 | 804 | 	return ret; | 
 | 805 | } | 
 | 806 |  | 
 | 807 | static int platform_pm_thaw(struct device *dev) | 
 | 808 | { | 
 | 809 | 	struct device_driver *drv = dev->driver; | 
 | 810 | 	int ret = 0; | 
 | 811 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 812 | 	if (!drv) | 
 | 813 | 		return 0; | 
 | 814 |  | 
 | 815 | 	if (drv->pm) { | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 816 | 		if (drv->pm->thaw) | 
 | 817 | 			ret = drv->pm->thaw(dev); | 
 | 818 | 	} else { | 
 | 819 | 		ret = platform_legacy_resume(dev); | 
 | 820 | 	} | 
 | 821 |  | 
 | 822 | 	return ret; | 
 | 823 | } | 
 | 824 |  | 
 | 825 | static int platform_pm_thaw_noirq(struct device *dev) | 
 | 826 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 827 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 828 | 	int ret = 0; | 
 | 829 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 830 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 831 | 		return 0; | 
 | 832 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 833 | 	if (drv->pm) { | 
 | 834 | 		if (drv->pm->thaw_noirq) | 
 | 835 | 			ret = drv->pm->thaw_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 836 | 	} else { | 
 | 837 | 		ret = platform_legacy_resume_early(dev); | 
 | 838 | 	} | 
 | 839 |  | 
 | 840 | 	return ret; | 
 | 841 | } | 
 | 842 |  | 
 | 843 | static int platform_pm_poweroff(struct device *dev) | 
 | 844 | { | 
 | 845 | 	struct device_driver *drv = dev->driver; | 
 | 846 | 	int ret = 0; | 
 | 847 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 848 | 	if (!drv) | 
 | 849 | 		return 0; | 
 | 850 |  | 
 | 851 | 	if (drv->pm) { | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 852 | 		if (drv->pm->poweroff) | 
 | 853 | 			ret = drv->pm->poweroff(dev); | 
 | 854 | 	} else { | 
 | 855 | 		ret = platform_legacy_suspend(dev, PMSG_HIBERNATE); | 
 | 856 | 	} | 
 | 857 |  | 
 | 858 | 	return ret; | 
 | 859 | } | 
 | 860 |  | 
 | 861 | static int platform_pm_poweroff_noirq(struct device *dev) | 
 | 862 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 863 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 864 | 	int ret = 0; | 
 | 865 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 866 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 867 | 		return 0; | 
 | 868 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 869 | 	if (drv->pm) { | 
 | 870 | 		if (drv->pm->poweroff_noirq) | 
 | 871 | 			ret = drv->pm->poweroff_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 872 | 	} else { | 
 | 873 | 		ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE); | 
 | 874 | 	} | 
 | 875 |  | 
 | 876 | 	return ret; | 
 | 877 | } | 
 | 878 |  | 
 | 879 | static int platform_pm_restore(struct device *dev) | 
 | 880 | { | 
 | 881 | 	struct device_driver *drv = dev->driver; | 
 | 882 | 	int ret = 0; | 
 | 883 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 884 | 	if (!drv) | 
 | 885 | 		return 0; | 
 | 886 |  | 
 | 887 | 	if (drv->pm) { | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 888 | 		if (drv->pm->restore) | 
 | 889 | 			ret = drv->pm->restore(dev); | 
 | 890 | 	} else { | 
 | 891 | 		ret = platform_legacy_resume(dev); | 
 | 892 | 	} | 
 | 893 |  | 
 | 894 | 	return ret; | 
 | 895 | } | 
 | 896 |  | 
 | 897 | static int platform_pm_restore_noirq(struct device *dev) | 
 | 898 | { | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 899 | 	struct device_driver *drv = dev->driver; | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 900 | 	int ret = 0; | 
 | 901 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 902 | 	if (!drv) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 903 | 		return 0; | 
 | 904 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 905 | 	if (drv->pm) { | 
 | 906 | 		if (drv->pm->restore_noirq) | 
 | 907 | 			ret = drv->pm->restore_noirq(dev); | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 908 | 	} else { | 
 | 909 | 		ret = platform_legacy_resume_early(dev); | 
 | 910 | 	} | 
 | 911 |  | 
 | 912 | 	return ret; | 
 | 913 | } | 
 | 914 |  | 
 | 915 | #else /* !CONFIG_HIBERNATION */ | 
 | 916 |  | 
 | 917 | #define platform_pm_freeze		NULL | 
 | 918 | #define platform_pm_thaw		NULL | 
 | 919 | #define platform_pm_poweroff		NULL | 
 | 920 | #define platform_pm_restore		NULL | 
 | 921 | #define platform_pm_freeze_noirq	NULL | 
 | 922 | #define platform_pm_thaw_noirq		NULL | 
 | 923 | #define platform_pm_poweroff_noirq	NULL | 
 | 924 | #define platform_pm_restore_noirq	NULL | 
 | 925 |  | 
 | 926 | #endif /* !CONFIG_HIBERNATION */ | 
 | 927 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 928 | static struct dev_pm_ops platform_dev_pm_ops = { | 
 | 929 | 	.prepare = platform_pm_prepare, | 
 | 930 | 	.complete = platform_pm_complete, | 
 | 931 | 	.suspend = platform_pm_suspend, | 
 | 932 | 	.resume = platform_pm_resume, | 
 | 933 | 	.freeze = platform_pm_freeze, | 
 | 934 | 	.thaw = platform_pm_thaw, | 
 | 935 | 	.poweroff = platform_pm_poweroff, | 
 | 936 | 	.restore = platform_pm_restore, | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 937 | 	.suspend_noirq = platform_pm_suspend_noirq, | 
 | 938 | 	.resume_noirq = platform_pm_resume_noirq, | 
 | 939 | 	.freeze_noirq = platform_pm_freeze_noirq, | 
 | 940 | 	.thaw_noirq = platform_pm_thaw_noirq, | 
 | 941 | 	.poweroff_noirq = platform_pm_poweroff_noirq, | 
 | 942 | 	.restore_noirq = platform_pm_restore_noirq, | 
 | 943 | }; | 
 | 944 |  | 
| Rafael J. Wysocki | adf0949 | 2008-10-06 22:46:05 +0200 | [diff] [blame] | 945 | #define PLATFORM_PM_OPS_PTR	(&platform_dev_pm_ops) | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 946 |  | 
 | 947 | #else /* !CONFIG_PM_SLEEP */ | 
 | 948 |  | 
 | 949 | #define PLATFORM_PM_OPS_PTR	NULL | 
 | 950 |  | 
 | 951 | #endif /* !CONFIG_PM_SLEEP */ | 
 | 952 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | struct bus_type platform_bus_type = { | 
 | 954 | 	.name		= "platform", | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 955 | 	.dev_attrs	= platform_dev_attrs, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | 	.match		= platform_match, | 
| David Brownell | a0245f7 | 2006-05-29 10:37:33 -0700 | [diff] [blame] | 957 | 	.uevent		= platform_uevent, | 
| Rafael J. Wysocki | 25e1849 | 2008-05-21 01:40:43 +0200 | [diff] [blame] | 958 | 	.pm		= PLATFORM_PM_OPS_PTR, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | }; | 
| Dmitry Torokhov | a96b204 | 2005-12-10 01:36:28 -0500 | [diff] [blame] | 960 | EXPORT_SYMBOL_GPL(platform_bus_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 |  | 
 | 962 | int __init platform_bus_init(void) | 
 | 963 | { | 
| Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 964 | 	int error; | 
 | 965 |  | 
| Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 966 | 	early_platform_cleanup(); | 
 | 967 |  | 
| Cornelia Huck | fbfb144 | 2006-11-27 10:35:08 +0100 | [diff] [blame] | 968 | 	error = device_register(&platform_bus); | 
 | 969 | 	if (error) | 
 | 970 | 		return error; | 
 | 971 | 	error =  bus_register(&platform_bus_type); | 
 | 972 | 	if (error) | 
 | 973 | 		device_unregister(&platform_bus); | 
 | 974 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } | 
 | 976 |  | 
 | 977 | #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK | 
 | 978 | u64 dma_get_required_mask(struct device *dev) | 
 | 979 | { | 
 | 980 | 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); | 
 | 981 | 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); | 
 | 982 | 	u64 mask; | 
 | 983 |  | 
 | 984 | 	if (!high_totalram) { | 
 | 985 | 		/* convert to mask just covering totalram */ | 
 | 986 | 		low_totalram = (1 << (fls(low_totalram) - 1)); | 
 | 987 | 		low_totalram += low_totalram - 1; | 
 | 988 | 		mask = low_totalram; | 
 | 989 | 	} else { | 
 | 990 | 		high_totalram = (1 << (fls(high_totalram) - 1)); | 
 | 991 | 		high_totalram += high_totalram - 1; | 
 | 992 | 		mask = (((u64)high_totalram) << 32) + 0xffffffff; | 
 | 993 | 	} | 
| James Bottomley | e88a0c2 | 2008-03-09 11:57:56 -0500 | [diff] [blame] | 994 | 	return mask; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } | 
 | 996 | EXPORT_SYMBOL_GPL(dma_get_required_mask); | 
 | 997 | #endif | 
| Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 998 |  | 
 | 999 | static __initdata LIST_HEAD(early_platform_driver_list); | 
 | 1000 | static __initdata LIST_HEAD(early_platform_device_list); | 
 | 1001 |  | 
 | 1002 | /** | 
 | 1003 |  * early_platform_driver_register | 
| Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 1004 |  * @epdrv: early_platform driver structure | 
| Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 1005 |  * @buf: string passed from early_param() | 
 | 1006 |  */ | 
 | 1007 | int __init early_platform_driver_register(struct early_platform_driver *epdrv, | 
 | 1008 | 					  char *buf) | 
 | 1009 | { | 
 | 1010 | 	unsigned long index; | 
 | 1011 | 	int n; | 
 | 1012 |  | 
 | 1013 | 	/* Simply add the driver to the end of the global list. | 
 | 1014 | 	 * Drivers will by default be put on the list in compiled-in order. | 
 | 1015 | 	 */ | 
 | 1016 | 	if (!epdrv->list.next) { | 
 | 1017 | 		INIT_LIST_HEAD(&epdrv->list); | 
 | 1018 | 		list_add_tail(&epdrv->list, &early_platform_driver_list); | 
 | 1019 | 	} | 
 | 1020 |  | 
 | 1021 | 	/* If the user has specified device then make sure the driver | 
 | 1022 | 	 * gets prioritized. The driver of the last device specified on | 
 | 1023 | 	 * command line will be put first on the list. | 
 | 1024 | 	 */ | 
 | 1025 | 	n = strlen(epdrv->pdrv->driver.name); | 
 | 1026 | 	if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) { | 
 | 1027 | 		list_move(&epdrv->list, &early_platform_driver_list); | 
 | 1028 |  | 
 | 1029 | 		if (!strcmp(buf, epdrv->pdrv->driver.name)) | 
 | 1030 | 			epdrv->requested_id = -1; | 
 | 1031 | 		else if (buf[n] == '.' && strict_strtoul(&buf[n + 1], 10, | 
 | 1032 | 							 &index) == 0) | 
 | 1033 | 			epdrv->requested_id = index; | 
 | 1034 | 		else | 
 | 1035 | 			epdrv->requested_id = EARLY_PLATFORM_ID_ERROR; | 
 | 1036 | 	} | 
 | 1037 |  | 
 | 1038 | 	return 0; | 
 | 1039 | } | 
 | 1040 |  | 
 | 1041 | /** | 
 | 1042 |  * early_platform_add_devices - add a numbers of early platform devices | 
 | 1043 |  * @devs: array of early platform devices to add | 
 | 1044 |  * @num: number of early platform devices in array | 
 | 1045 |  */ | 
 | 1046 | void __init early_platform_add_devices(struct platform_device **devs, int num) | 
 | 1047 | { | 
 | 1048 | 	struct device *dev; | 
 | 1049 | 	int i; | 
 | 1050 |  | 
 | 1051 | 	/* simply add the devices to list */ | 
 | 1052 | 	for (i = 0; i < num; i++) { | 
 | 1053 | 		dev = &devs[i]->dev; | 
 | 1054 |  | 
 | 1055 | 		if (!dev->devres_head.next) { | 
 | 1056 | 			INIT_LIST_HEAD(&dev->devres_head); | 
 | 1057 | 			list_add_tail(&dev->devres_head, | 
 | 1058 | 				      &early_platform_device_list); | 
 | 1059 | 		} | 
 | 1060 | 	} | 
 | 1061 | } | 
 | 1062 |  | 
 | 1063 | /** | 
 | 1064 |  * early_platform_driver_register_all | 
 | 1065 |  * @class_str: string to identify early platform driver class | 
 | 1066 |  */ | 
 | 1067 | void __init early_platform_driver_register_all(char *class_str) | 
 | 1068 | { | 
 | 1069 | 	/* The "class_str" parameter may or may not be present on the kernel | 
 | 1070 | 	 * command line. If it is present then there may be more than one | 
 | 1071 | 	 * matching parameter. | 
 | 1072 | 	 * | 
 | 1073 | 	 * Since we register our early platform drivers using early_param() | 
 | 1074 | 	 * we need to make sure that they also get registered in the case | 
 | 1075 | 	 * when the parameter is missing from the kernel command line. | 
 | 1076 | 	 * | 
 | 1077 | 	 * We use parse_early_options() to make sure the early_param() gets | 
 | 1078 | 	 * called at least once. The early_param() may be called more than | 
 | 1079 | 	 * once since the name of the preferred device may be specified on | 
 | 1080 | 	 * the kernel command line. early_platform_driver_register() handles | 
 | 1081 | 	 * this case for us. | 
 | 1082 | 	 */ | 
 | 1083 | 	parse_early_options(class_str); | 
 | 1084 | } | 
 | 1085 |  | 
 | 1086 | /** | 
 | 1087 |  * early_platform_match | 
| Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 1088 |  * @epdrv: early platform driver structure | 
| Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 1089 |  * @id: id to match against | 
 | 1090 |  */ | 
 | 1091 | static  __init struct platform_device * | 
 | 1092 | early_platform_match(struct early_platform_driver *epdrv, int id) | 
 | 1093 | { | 
 | 1094 | 	struct platform_device *pd; | 
 | 1095 |  | 
 | 1096 | 	list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | 
 | 1097 | 		if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | 
 | 1098 | 			if (pd->id == id) | 
 | 1099 | 				return pd; | 
 | 1100 |  | 
 | 1101 | 	return NULL; | 
 | 1102 | } | 
 | 1103 |  | 
 | 1104 | /** | 
 | 1105 |  * early_platform_left | 
| Randy Dunlap | d86c130 | 2009-04-21 07:22:53 -0700 | [diff] [blame] | 1106 |  * @epdrv: early platform driver structure | 
| Magnus Damm | 1397709 | 2009-03-30 14:37:25 -0700 | [diff] [blame] | 1107 |  * @id: return true if id or above exists | 
 | 1108 |  */ | 
 | 1109 | static  __init int early_platform_left(struct early_platform_driver *epdrv, | 
 | 1110 | 				       int id) | 
 | 1111 | { | 
 | 1112 | 	struct platform_device *pd; | 
 | 1113 |  | 
 | 1114 | 	list_for_each_entry(pd, &early_platform_device_list, dev.devres_head) | 
 | 1115 | 		if (platform_match(&pd->dev, &epdrv->pdrv->driver)) | 
 | 1116 | 			if (pd->id >= id) | 
 | 1117 | 				return 1; | 
 | 1118 |  | 
 | 1119 | 	return 0; | 
 | 1120 | } | 
 | 1121 |  | 
 | 1122 | /** | 
 | 1123 |  * early_platform_driver_probe_id | 
 | 1124 |  * @class_str: string to identify early platform driver class | 
 | 1125 |  * @id: id to match against | 
 | 1126 |  * @nr_probe: number of platform devices to successfully probe before exiting | 
 | 1127 |  */ | 
 | 1128 | static int __init early_platform_driver_probe_id(char *class_str, | 
 | 1129 | 						 int id, | 
 | 1130 | 						 int nr_probe) | 
 | 1131 | { | 
 | 1132 | 	struct early_platform_driver *epdrv; | 
 | 1133 | 	struct platform_device *match; | 
 | 1134 | 	int match_id; | 
 | 1135 | 	int n = 0; | 
 | 1136 | 	int left = 0; | 
 | 1137 |  | 
 | 1138 | 	list_for_each_entry(epdrv, &early_platform_driver_list, list) { | 
 | 1139 | 		/* only use drivers matching our class_str */ | 
 | 1140 | 		if (strcmp(class_str, epdrv->class_str)) | 
 | 1141 | 			continue; | 
 | 1142 |  | 
 | 1143 | 		if (id == -2) { | 
 | 1144 | 			match_id = epdrv->requested_id; | 
 | 1145 | 			left = 1; | 
 | 1146 |  | 
 | 1147 | 		} else { | 
 | 1148 | 			match_id = id; | 
 | 1149 | 			left += early_platform_left(epdrv, id); | 
 | 1150 |  | 
 | 1151 | 			/* skip requested id */ | 
 | 1152 | 			switch (epdrv->requested_id) { | 
 | 1153 | 			case EARLY_PLATFORM_ID_ERROR: | 
 | 1154 | 			case EARLY_PLATFORM_ID_UNSET: | 
 | 1155 | 				break; | 
 | 1156 | 			default: | 
 | 1157 | 				if (epdrv->requested_id == id) | 
 | 1158 | 					match_id = EARLY_PLATFORM_ID_UNSET; | 
 | 1159 | 			} | 
 | 1160 | 		} | 
 | 1161 |  | 
 | 1162 | 		switch (match_id) { | 
 | 1163 | 		case EARLY_PLATFORM_ID_ERROR: | 
 | 1164 | 			pr_warning("%s: unable to parse %s parameter\n", | 
 | 1165 | 				   class_str, epdrv->pdrv->driver.name); | 
 | 1166 | 			/* fall-through */ | 
 | 1167 | 		case EARLY_PLATFORM_ID_UNSET: | 
 | 1168 | 			match = NULL; | 
 | 1169 | 			break; | 
 | 1170 | 		default: | 
 | 1171 | 			match = early_platform_match(epdrv, match_id); | 
 | 1172 | 		} | 
 | 1173 |  | 
 | 1174 | 		if (match) { | 
 | 1175 | 			if (epdrv->pdrv->probe(match)) | 
 | 1176 | 				pr_warning("%s: unable to probe %s early.\n", | 
 | 1177 | 					   class_str, match->name); | 
 | 1178 | 			else | 
 | 1179 | 				n++; | 
 | 1180 | 		} | 
 | 1181 |  | 
 | 1182 | 		if (n >= nr_probe) | 
 | 1183 | 			break; | 
 | 1184 | 	} | 
 | 1185 |  | 
 | 1186 | 	if (left) | 
 | 1187 | 		return n; | 
 | 1188 | 	else | 
 | 1189 | 		return -ENODEV; | 
 | 1190 | } | 
 | 1191 |  | 
 | 1192 | /** | 
 | 1193 |  * early_platform_driver_probe | 
 | 1194 |  * @class_str: string to identify early platform driver class | 
 | 1195 |  * @nr_probe: number of platform devices to successfully probe before exiting | 
 | 1196 |  * @user_only: only probe user specified early platform devices | 
 | 1197 |  */ | 
 | 1198 | int __init early_platform_driver_probe(char *class_str, | 
 | 1199 | 				       int nr_probe, | 
 | 1200 | 				       int user_only) | 
 | 1201 | { | 
 | 1202 | 	int k, n, i; | 
 | 1203 |  | 
 | 1204 | 	n = 0; | 
 | 1205 | 	for (i = -2; n < nr_probe; i++) { | 
 | 1206 | 		k = early_platform_driver_probe_id(class_str, i, nr_probe - n); | 
 | 1207 |  | 
 | 1208 | 		if (k < 0) | 
 | 1209 | 			break; | 
 | 1210 |  | 
 | 1211 | 		n += k; | 
 | 1212 |  | 
 | 1213 | 		if (user_only) | 
 | 1214 | 			break; | 
 | 1215 | 	} | 
 | 1216 |  | 
 | 1217 | 	return n; | 
 | 1218 | } | 
 | 1219 |  | 
 | 1220 | /** | 
 | 1221 |  * early_platform_cleanup - clean up early platform code | 
 | 1222 |  */ | 
 | 1223 | void __init early_platform_cleanup(void) | 
 | 1224 | { | 
 | 1225 | 	struct platform_device *pd, *pd2; | 
 | 1226 |  | 
 | 1227 | 	/* clean up the devres list used to chain devices */ | 
 | 1228 | 	list_for_each_entry_safe(pd, pd2, &early_platform_device_list, | 
 | 1229 | 				 dev.devres_head) { | 
 | 1230 | 		list_del(&pd->dev.devres_head); | 
 | 1231 | 		memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head)); | 
 | 1232 | 	} | 
 | 1233 | } | 
 | 1234 |  |