11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * platform.c - platform 'pseudo' bus for legacy devices 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2002-3 Patrick Mochel 51da177e4SLinus Torvalds * Copyright (c) 2002-3 Open Source Development Labs 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file is released under the GPLv2 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Please see Documentation/driver-model/platform.txt for more 101da177e4SLinus Torvalds * information. 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13d052d1beSRussell King #include <linux/platform_device.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds #include <linux/dma-mapping.h> 171da177e4SLinus Torvalds #include <linux/bootmem.h> 181da177e4SLinus Torvalds #include <linux/err.h> 194e57b681STim Schmielau #include <linux/slab.h> 201da177e4SLinus Torvalds 21a1bdc7aaSBen Dooks #include "base.h" 22a1bdc7aaSBen Dooks 2300d3dcddSRussell King #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver)) 2400d3dcddSRussell King 251da177e4SLinus Torvalds struct device platform_bus = { 261da177e4SLinus Torvalds .bus_id = "platform", 271da177e4SLinus Torvalds }; 28a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_bus); 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /** 311da177e4SLinus Torvalds * platform_get_resource - get a resource for a device 321da177e4SLinus Torvalds * @dev: platform device 331da177e4SLinus Torvalds * @type: resource type 341da177e4SLinus Torvalds * @num: resource index 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds struct resource * 371da177e4SLinus Torvalds platform_get_resource(struct platform_device *dev, unsigned int type, 381da177e4SLinus Torvalds unsigned int num) 391da177e4SLinus Torvalds { 401da177e4SLinus Torvalds int i; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds for (i = 0; i < dev->num_resources; i++) { 431da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| 461da177e4SLinus Torvalds IORESOURCE_IRQ|IORESOURCE_DMA)) 471da177e4SLinus Torvalds == type) 481da177e4SLinus Torvalds if (num-- == 0) 491da177e4SLinus Torvalds return r; 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds return NULL; 521da177e4SLinus Torvalds } 53a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_get_resource); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /** 561da177e4SLinus Torvalds * platform_get_irq - get an IRQ for a device 571da177e4SLinus Torvalds * @dev: platform device 581da177e4SLinus Torvalds * @num: IRQ number index 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds int platform_get_irq(struct platform_device *dev, unsigned int num) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num); 631da177e4SLinus Torvalds 64305b3228SDavid Vrabel return r ? r->start : -ENXIO; 651da177e4SLinus Torvalds } 66a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_get_irq); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /** 691da177e4SLinus Torvalds * platform_get_resource_byname - get a resource for a device by name 701da177e4SLinus Torvalds * @dev: platform device 711da177e4SLinus Torvalds * @type: resource type 721da177e4SLinus Torvalds * @name: resource name 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds struct resource * 751da177e4SLinus Torvalds platform_get_resource_byname(struct platform_device *dev, unsigned int type, 761da177e4SLinus Torvalds char *name) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds int i; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds for (i = 0; i < dev->num_resources; i++) { 811da177e4SLinus Torvalds struct resource *r = &dev->resource[i]; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM| 841da177e4SLinus Torvalds IORESOURCE_IRQ|IORESOURCE_DMA)) == type) 851da177e4SLinus Torvalds if (!strcmp(r->name, name)) 861da177e4SLinus Torvalds return r; 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds return NULL; 891da177e4SLinus Torvalds } 90a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_get_resource_byname); 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /** 931da177e4SLinus Torvalds * platform_get_irq - get an IRQ for a device 941da177e4SLinus Torvalds * @dev: platform device 951da177e4SLinus Torvalds * @name: IRQ name 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds int platform_get_irq_byname(struct platform_device *dev, char *name) 981da177e4SLinus Torvalds { 991da177e4SLinus Torvalds struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name); 1001da177e4SLinus Torvalds 101305b3228SDavid Vrabel return r ? r->start : -ENXIO; 1021da177e4SLinus Torvalds } 103a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_get_irq_byname); 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /** 1061da177e4SLinus Torvalds * platform_add_devices - add a numbers of platform devices 1071da177e4SLinus Torvalds * @devs: array of platform devices to add 1081da177e4SLinus Torvalds * @num: number of platform devices in array 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds int platform_add_devices(struct platform_device **devs, int num) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds int i, ret = 0; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1151da177e4SLinus Torvalds ret = platform_device_register(devs[i]); 1161da177e4SLinus Torvalds if (ret) { 1171da177e4SLinus Torvalds while (--i >= 0) 1181da177e4SLinus Torvalds platform_device_unregister(devs[i]); 1191da177e4SLinus Torvalds break; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds return ret; 1241da177e4SLinus Torvalds } 125a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_add_devices); 1261da177e4SLinus Torvalds 12737c12e74SRussell King struct platform_object { 12837c12e74SRussell King struct platform_device pdev; 12937c12e74SRussell King char name[1]; 13037c12e74SRussell King }; 13137c12e74SRussell King 1321da177e4SLinus Torvalds /** 13337c12e74SRussell King * platform_device_put 13437c12e74SRussell King * @pdev: platform device to free 13537c12e74SRussell King * 13637c12e74SRussell King * Free all memory associated with a platform device. This function 13737c12e74SRussell King * must _only_ be externally called in error cases. All other usage 13837c12e74SRussell King * is a bug. 13937c12e74SRussell King */ 14037c12e74SRussell King void platform_device_put(struct platform_device *pdev) 14137c12e74SRussell King { 14237c12e74SRussell King if (pdev) 14337c12e74SRussell King put_device(&pdev->dev); 14437c12e74SRussell King } 14537c12e74SRussell King EXPORT_SYMBOL_GPL(platform_device_put); 14637c12e74SRussell King 14737c12e74SRussell King static void platform_device_release(struct device *dev) 14837c12e74SRussell King { 14937c12e74SRussell King struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev); 15037c12e74SRussell King 15137c12e74SRussell King kfree(pa->pdev.dev.platform_data); 15237c12e74SRussell King kfree(pa->pdev.resource); 15337c12e74SRussell King kfree(pa); 15437c12e74SRussell King } 15537c12e74SRussell King 15637c12e74SRussell King /** 15737c12e74SRussell King * platform_device_alloc 15837c12e74SRussell King * @name: base name of the device we're adding 15937c12e74SRussell King * @id: instance id 16037c12e74SRussell King * 16137c12e74SRussell King * Create a platform device object which can have other objects attached 16237c12e74SRussell King * to it, and which will have attached objects freed when it is released. 16337c12e74SRussell King */ 1641359555eSJean Delvare struct platform_device *platform_device_alloc(const char *name, int id) 16537c12e74SRussell King { 16637c12e74SRussell King struct platform_object *pa; 16737c12e74SRussell King 16837c12e74SRussell King pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL); 16937c12e74SRussell King if (pa) { 17037c12e74SRussell King strcpy(pa->name, name); 17137c12e74SRussell King pa->pdev.name = pa->name; 17237c12e74SRussell King pa->pdev.id = id; 17337c12e74SRussell King device_initialize(&pa->pdev.dev); 17437c12e74SRussell King pa->pdev.dev.release = platform_device_release; 17537c12e74SRussell King } 17637c12e74SRussell King 17737c12e74SRussell King return pa ? &pa->pdev : NULL; 17837c12e74SRussell King } 17937c12e74SRussell King EXPORT_SYMBOL_GPL(platform_device_alloc); 18037c12e74SRussell King 18137c12e74SRussell King /** 18237c12e74SRussell King * platform_device_add_resources 18337c12e74SRussell King * @pdev: platform device allocated by platform_device_alloc to add resources to 18437c12e74SRussell King * @res: set of resources that needs to be allocated for the device 18537c12e74SRussell King * @num: number of resources 18637c12e74SRussell King * 18737c12e74SRussell King * Add a copy of the resources to the platform device. The memory 18837c12e74SRussell King * associated with the resources will be freed when the platform 18937c12e74SRussell King * device is released. 19037c12e74SRussell King */ 19137c12e74SRussell King int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num) 19237c12e74SRussell King { 19337c12e74SRussell King struct resource *r; 19437c12e74SRussell King 19537c12e74SRussell King r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL); 19637c12e74SRussell King if (r) { 19737c12e74SRussell King memcpy(r, res, sizeof(struct resource) * num); 19837c12e74SRussell King pdev->resource = r; 19937c12e74SRussell King pdev->num_resources = num; 20037c12e74SRussell King } 20137c12e74SRussell King return r ? 0 : -ENOMEM; 20237c12e74SRussell King } 20337c12e74SRussell King EXPORT_SYMBOL_GPL(platform_device_add_resources); 20437c12e74SRussell King 20537c12e74SRussell King /** 20637c12e74SRussell King * platform_device_add_data 20737c12e74SRussell King * @pdev: platform device allocated by platform_device_alloc to add resources to 20837c12e74SRussell King * @data: platform specific data for this platform device 20937c12e74SRussell King * @size: size of platform specific data 21037c12e74SRussell King * 21137c12e74SRussell King * Add a copy of platform specific data to the platform device's platform_data 21237c12e74SRussell King * pointer. The memory associated with the platform data will be freed 21337c12e74SRussell King * when the platform device is released. 21437c12e74SRussell King */ 2156eefd34fSScott Wood int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size) 21637c12e74SRussell King { 21737c12e74SRussell King void *d; 21837c12e74SRussell King 21937c12e74SRussell King d = kmalloc(size, GFP_KERNEL); 22037c12e74SRussell King if (d) { 22137c12e74SRussell King memcpy(d, data, size); 22237c12e74SRussell King pdev->dev.platform_data = d; 22337c12e74SRussell King } 22437c12e74SRussell King return d ? 0 : -ENOMEM; 22537c12e74SRussell King } 22637c12e74SRussell King EXPORT_SYMBOL_GPL(platform_device_add_data); 22737c12e74SRussell King 22837c12e74SRussell King /** 22937c12e74SRussell King * platform_device_add - add a platform device to device hierarchy 23067be2dd1SMartin Waitz * @pdev: platform device we're adding 2311da177e4SLinus Torvalds * 23237c12e74SRussell King * This is part 2 of platform_device_register(), though may be called 23337c12e74SRussell King * separately _iff_ pdev was allocated by platform_device_alloc(). 2341da177e4SLinus Torvalds */ 23537c12e74SRussell King int platform_device_add(struct platform_device *pdev) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds int i, ret = 0; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds if (!pdev) 2401da177e4SLinus Torvalds return -EINVAL; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (!pdev->dev.parent) 2431da177e4SLinus Torvalds pdev->dev.parent = &platform_bus; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds pdev->dev.bus = &platform_bus_type; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds if (pdev->id != -1) 2481359555eSJean Delvare snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name, 2491359555eSJean Delvare pdev->id); 2501da177e4SLinus Torvalds else 2511da177e4SLinus Torvalds strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds for (i = 0; i < pdev->num_resources; i++) { 2541da177e4SLinus Torvalds struct resource *p, *r = &pdev->resource[i]; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (r->name == NULL) 2571da177e4SLinus Torvalds r->name = pdev->dev.bus_id; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds p = r->parent; 2601da177e4SLinus Torvalds if (!p) { 2611da177e4SLinus Torvalds if (r->flags & IORESOURCE_MEM) 2621da177e4SLinus Torvalds p = &iomem_resource; 2631da177e4SLinus Torvalds else if (r->flags & IORESOURCE_IO) 2641da177e4SLinus Torvalds p = &ioport_resource; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267d960bb4dSKumar Gala if (p && insert_resource(p, r)) { 2681da177e4SLinus Torvalds printk(KERN_ERR 2691da177e4SLinus Torvalds "%s: failed to claim resource %d\n", 2701da177e4SLinus Torvalds pdev->dev.bus_id, i); 2711da177e4SLinus Torvalds ret = -EBUSY; 2721da177e4SLinus Torvalds goto failed; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds pr_debug("Registering platform device '%s'. Parent at %s\n", 2771da177e4SLinus Torvalds pdev->dev.bus_id, pdev->dev.parent->bus_id); 2781da177e4SLinus Torvalds 279e3915532SRussell King ret = device_add(&pdev->dev); 2801da177e4SLinus Torvalds if (ret == 0) 2811da177e4SLinus Torvalds return ret; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds failed: 2841da177e4SLinus Torvalds while (--i >= 0) 2851da177e4SLinus Torvalds if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO)) 2861da177e4SLinus Torvalds release_resource(&pdev->resource[i]); 2871da177e4SLinus Torvalds return ret; 2881da177e4SLinus Torvalds } 28937c12e74SRussell King EXPORT_SYMBOL_GPL(platform_device_add); 29037c12e74SRussell King 29137c12e74SRussell King /** 29293ce3061SDmitry Torokhov * platform_device_del - remove a platform-level device 29393ce3061SDmitry Torokhov * @pdev: platform device we're removing 29493ce3061SDmitry Torokhov * 29593ce3061SDmitry Torokhov * Note that this function will also release all memory- and port-based 296dc4c15d4SJean Delvare * resources owned by the device (@dev->resource). This function 297dc4c15d4SJean Delvare * must _only_ be externally called in error cases. All other usage 298dc4c15d4SJean Delvare * is a bug. 29993ce3061SDmitry Torokhov */ 30093ce3061SDmitry Torokhov void platform_device_del(struct platform_device *pdev) 30193ce3061SDmitry Torokhov { 30293ce3061SDmitry Torokhov int i; 30393ce3061SDmitry Torokhov 30493ce3061SDmitry Torokhov if (pdev) { 305dc4c15d4SJean Delvare device_del(&pdev->dev); 306dc4c15d4SJean Delvare 30793ce3061SDmitry Torokhov for (i = 0; i < pdev->num_resources; i++) { 30893ce3061SDmitry Torokhov struct resource *r = &pdev->resource[i]; 30993ce3061SDmitry Torokhov if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO)) 31093ce3061SDmitry Torokhov release_resource(r); 31193ce3061SDmitry Torokhov } 31293ce3061SDmitry Torokhov } 31393ce3061SDmitry Torokhov } 31493ce3061SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_device_del); 31593ce3061SDmitry Torokhov 31693ce3061SDmitry Torokhov /** 31737c12e74SRussell King * platform_device_register - add a platform-level device 31837c12e74SRussell King * @pdev: platform device we're adding 31937c12e74SRussell King * 32037c12e74SRussell King */ 32137c12e74SRussell King int platform_device_register(struct platform_device * pdev) 32237c12e74SRussell King { 32337c12e74SRussell King device_initialize(&pdev->dev); 32437c12e74SRussell King return platform_device_add(pdev); 32537c12e74SRussell King } 326a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_device_register); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /** 32993ce3061SDmitry Torokhov * platform_device_unregister - unregister a platform-level device 33093ce3061SDmitry Torokhov * @pdev: platform device we're unregistering 3311da177e4SLinus Torvalds * 33280682fa9SUwe Zeisberger * Unregistration is done in 2 steps. First we release all resources 3332d7b5a70SJean Delvare * and remove it from the subsystem, then we drop reference count by 33493ce3061SDmitry Torokhov * calling platform_device_put(). 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds void platform_device_unregister(struct platform_device * pdev) 3371da177e4SLinus Torvalds { 33893ce3061SDmitry Torokhov platform_device_del(pdev); 33993ce3061SDmitry Torokhov platform_device_put(pdev); 3401da177e4SLinus Torvalds } 341a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_device_unregister); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /** 3441da177e4SLinus Torvalds * platform_device_register_simple 3451da177e4SLinus Torvalds * @name: base name of the device we're adding 3461da177e4SLinus Torvalds * @id: instance id 3471da177e4SLinus Torvalds * @res: set of resources that needs to be allocated for the device 3481da177e4SLinus Torvalds * @num: number of resources 3491da177e4SLinus Torvalds * 3501da177e4SLinus Torvalds * This function creates a simple platform device that requires minimal 3511da177e4SLinus Torvalds * resource and memory management. Canned release function freeing 3521da177e4SLinus Torvalds * memory allocated for the device allows drivers using such devices 35301afd806SMárton Németh * to be unloaded without waiting for the last reference to the device 3541da177e4SLinus Torvalds * to be dropped. 35549a4ec18SDavid Brownell * 35649a4ec18SDavid Brownell * This interface is primarily intended for use with legacy drivers 35749a4ec18SDavid Brownell * which probe hardware directly. Because such drivers create sysfs 35849a4ec18SDavid Brownell * device nodes themselves, rather than letting system infrastructure 35949a4ec18SDavid Brownell * handle such device enumeration tasks, they don't fully conform to 36049a4ec18SDavid Brownell * the Linux driver model. In particular, when such drivers are built 36149a4ec18SDavid Brownell * as modules, they can't be "hotplugged". 3621da177e4SLinus Torvalds */ 3631359555eSJean Delvare struct platform_device *platform_device_register_simple(char *name, int id, 3641da177e4SLinus Torvalds struct resource *res, unsigned int num) 3651da177e4SLinus Torvalds { 36637c12e74SRussell King struct platform_device *pdev; 3671da177e4SLinus Torvalds int retval; 3681da177e4SLinus Torvalds 36937c12e74SRussell King pdev = platform_device_alloc(name, id); 37037c12e74SRussell King if (!pdev) { 3711da177e4SLinus Torvalds retval = -ENOMEM; 3721da177e4SLinus Torvalds goto error; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (num) { 37637c12e74SRussell King retval = platform_device_add_resources(pdev, res, num); 37737c12e74SRussell King if (retval) 37837c12e74SRussell King goto error; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 38137c12e74SRussell King retval = platform_device_add(pdev); 3821da177e4SLinus Torvalds if (retval) 3831da177e4SLinus Torvalds goto error; 3841da177e4SLinus Torvalds 38537c12e74SRussell King return pdev; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds error: 38837c12e74SRussell King platform_device_put(pdev); 3891da177e4SLinus Torvalds return ERR_PTR(retval); 3901da177e4SLinus Torvalds } 391a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_device_register_simple); 3921da177e4SLinus Torvalds 39300d3dcddSRussell King static int platform_drv_probe(struct device *_dev) 39400d3dcddSRussell King { 39500d3dcddSRussell King struct platform_driver *drv = to_platform_driver(_dev->driver); 39600d3dcddSRussell King struct platform_device *dev = to_platform_device(_dev); 39700d3dcddSRussell King 39800d3dcddSRussell King return drv->probe(dev); 39900d3dcddSRussell King } 40000d3dcddSRussell King 401c67334fbSDavid Brownell static int platform_drv_probe_fail(struct device *_dev) 402c67334fbSDavid Brownell { 403c67334fbSDavid Brownell return -ENXIO; 404c67334fbSDavid Brownell } 405c67334fbSDavid Brownell 40600d3dcddSRussell King static int platform_drv_remove(struct device *_dev) 40700d3dcddSRussell King { 40800d3dcddSRussell King struct platform_driver *drv = to_platform_driver(_dev->driver); 40900d3dcddSRussell King struct platform_device *dev = to_platform_device(_dev); 41000d3dcddSRussell King 41100d3dcddSRussell King return drv->remove(dev); 41200d3dcddSRussell King } 41300d3dcddSRussell King 41400d3dcddSRussell King static void platform_drv_shutdown(struct device *_dev) 41500d3dcddSRussell King { 41600d3dcddSRussell King struct platform_driver *drv = to_platform_driver(_dev->driver); 41700d3dcddSRussell King struct platform_device *dev = to_platform_device(_dev); 41800d3dcddSRussell King 41900d3dcddSRussell King drv->shutdown(dev); 42000d3dcddSRussell King } 42100d3dcddSRussell King 42200d3dcddSRussell King static int platform_drv_suspend(struct device *_dev, pm_message_t state) 42300d3dcddSRussell King { 42400d3dcddSRussell King struct platform_driver *drv = to_platform_driver(_dev->driver); 42500d3dcddSRussell King struct platform_device *dev = to_platform_device(_dev); 42600d3dcddSRussell King 42700d3dcddSRussell King return drv->suspend(dev, state); 42800d3dcddSRussell King } 42900d3dcddSRussell King 43000d3dcddSRussell King static int platform_drv_resume(struct device *_dev) 43100d3dcddSRussell King { 43200d3dcddSRussell King struct platform_driver *drv = to_platform_driver(_dev->driver); 43300d3dcddSRussell King struct platform_device *dev = to_platform_device(_dev); 43400d3dcddSRussell King 43500d3dcddSRussell King return drv->resume(dev); 43600d3dcddSRussell King } 43700d3dcddSRussell King 43800d3dcddSRussell King /** 43900d3dcddSRussell King * platform_driver_register 44000d3dcddSRussell King * @drv: platform driver structure 44100d3dcddSRussell King */ 44200d3dcddSRussell King int platform_driver_register(struct platform_driver *drv) 44300d3dcddSRussell King { 44400d3dcddSRussell King drv->driver.bus = &platform_bus_type; 44500d3dcddSRussell King if (drv->probe) 44600d3dcddSRussell King drv->driver.probe = platform_drv_probe; 44700d3dcddSRussell King if (drv->remove) 44800d3dcddSRussell King drv->driver.remove = platform_drv_remove; 44900d3dcddSRussell King if (drv->shutdown) 45000d3dcddSRussell King drv->driver.shutdown = platform_drv_shutdown; 45100d3dcddSRussell King if (drv->suspend) 45200d3dcddSRussell King drv->driver.suspend = platform_drv_suspend; 45300d3dcddSRussell King if (drv->resume) 45400d3dcddSRussell King drv->driver.resume = platform_drv_resume; 45500d3dcddSRussell King return driver_register(&drv->driver); 45600d3dcddSRussell King } 45700d3dcddSRussell King EXPORT_SYMBOL_GPL(platform_driver_register); 45800d3dcddSRussell King 45900d3dcddSRussell King /** 46000d3dcddSRussell King * platform_driver_unregister 46100d3dcddSRussell King * @drv: platform driver structure 46200d3dcddSRussell King */ 46300d3dcddSRussell King void platform_driver_unregister(struct platform_driver *drv) 46400d3dcddSRussell King { 46500d3dcddSRussell King driver_unregister(&drv->driver); 46600d3dcddSRussell King } 46700d3dcddSRussell King EXPORT_SYMBOL_GPL(platform_driver_unregister); 46800d3dcddSRussell King 469c67334fbSDavid Brownell /** 470c67334fbSDavid Brownell * platform_driver_probe - register driver for non-hotpluggable device 471c67334fbSDavid Brownell * @drv: platform driver structure 472c67334fbSDavid Brownell * @probe: the driver probe routine, probably from an __init section 473c67334fbSDavid Brownell * 474c67334fbSDavid Brownell * Use this instead of platform_driver_register() when you know the device 475c67334fbSDavid Brownell * is not hotpluggable and has already been registered, and you want to 476c67334fbSDavid Brownell * remove its run-once probe() infrastructure from memory after the driver 477c67334fbSDavid Brownell * has bound to the device. 478c67334fbSDavid Brownell * 479c67334fbSDavid Brownell * One typical use for this would be with drivers for controllers integrated 480c67334fbSDavid Brownell * into system-on-chip processors, where the controller devices have been 481c67334fbSDavid Brownell * configured as part of board setup. 482c67334fbSDavid Brownell * 483c67334fbSDavid Brownell * Returns zero if the driver registered and bound to a device, else returns 484c67334fbSDavid Brownell * a negative error code and with the driver not registered. 485c67334fbSDavid Brownell */ 486c63e0783SAndrew Morton int __init_or_module platform_driver_probe(struct platform_driver *drv, 487c67334fbSDavid Brownell int (*probe)(struct platform_device *)) 488c67334fbSDavid Brownell { 489c67334fbSDavid Brownell int retval, code; 490c67334fbSDavid Brownell 491c67334fbSDavid Brownell /* temporary section violation during probe() */ 492c67334fbSDavid Brownell drv->probe = probe; 493c67334fbSDavid Brownell retval = code = platform_driver_register(drv); 494c67334fbSDavid Brownell 495c67334fbSDavid Brownell /* Fixup that section violation, being paranoid about code scanning 496c67334fbSDavid Brownell * the list of drivers in order to probe new devices. Check to see 497c67334fbSDavid Brownell * if the probe was successful, and make sure any forced probes of 498c67334fbSDavid Brownell * new devices fail. 499c67334fbSDavid Brownell */ 500c6f7e72aSGreg Kroah-Hartman spin_lock(&platform_bus_type.p->klist_drivers.k_lock); 501c67334fbSDavid Brownell drv->probe = NULL; 502e5dd1278SGreg Kroah-Hartman if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list)) 503c67334fbSDavid Brownell retval = -ENODEV; 504c67334fbSDavid Brownell drv->driver.probe = platform_drv_probe_fail; 505c6f7e72aSGreg Kroah-Hartman spin_unlock(&platform_bus_type.p->klist_drivers.k_lock); 506c67334fbSDavid Brownell 507c67334fbSDavid Brownell if (code != retval) 508c67334fbSDavid Brownell platform_driver_unregister(drv); 509c67334fbSDavid Brownell return retval; 510c67334fbSDavid Brownell } 511c67334fbSDavid Brownell EXPORT_SYMBOL_GPL(platform_driver_probe); 5121da177e4SLinus Torvalds 513a0245f7aSDavid Brownell /* modalias support enables more hands-off userspace setup: 514a0245f7aSDavid Brownell * (a) environment variable lets new-style hotplug events work once system is 515a0245f7aSDavid Brownell * fully running: "modprobe $MODALIAS" 516a0245f7aSDavid Brownell * (b) sysfs attribute lets new-style coldplug recover from hotplug events 517a0245f7aSDavid Brownell * mishandled before system is fully running: "modprobe $(cat modalias)" 518a0245f7aSDavid Brownell */ 519a0245f7aSDavid Brownell static ssize_t 520a0245f7aSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 521a0245f7aSDavid Brownell { 522a0245f7aSDavid Brownell struct platform_device *pdev = to_platform_device(dev); 52343cc71eeSKay Sievers int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name); 524a0245f7aSDavid Brownell 525a0245f7aSDavid Brownell return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; 526a0245f7aSDavid Brownell } 527a0245f7aSDavid Brownell 528a0245f7aSDavid Brownell static struct device_attribute platform_dev_attrs[] = { 529a0245f7aSDavid Brownell __ATTR_RO(modalias), 530a0245f7aSDavid Brownell __ATTR_NULL, 531a0245f7aSDavid Brownell }; 532a0245f7aSDavid Brownell 5337eff2e7aSKay Sievers static int platform_uevent(struct device *dev, struct kobj_uevent_env *env) 534a0245f7aSDavid Brownell { 535a0245f7aSDavid Brownell struct platform_device *pdev = to_platform_device(dev); 536a0245f7aSDavid Brownell 5377eff2e7aSKay Sievers add_uevent_var(env, "MODALIAS=platform:%s", pdev->name); 538a0245f7aSDavid Brownell return 0; 539a0245f7aSDavid Brownell } 540a0245f7aSDavid Brownell 541a0245f7aSDavid Brownell 5421da177e4SLinus Torvalds /** 5431da177e4SLinus Torvalds * platform_match - bind platform device to platform driver. 5441da177e4SLinus Torvalds * @dev: device. 5451da177e4SLinus Torvalds * @drv: driver. 5461da177e4SLinus Torvalds * 5471da177e4SLinus Torvalds * Platform device IDs are assumed to be encoded like this: 5481da177e4SLinus Torvalds * "<name><instance>", where <name> is a short description of the 5491da177e4SLinus Torvalds * type of device, like "pci" or "floppy", and <instance> is the 5501da177e4SLinus Torvalds * enumerated instance of the device, like '0' or '42'. 5511da177e4SLinus Torvalds * Driver IDs are simply "<name>". 5521da177e4SLinus Torvalds * So, extract the <name> from the platform_device structure, 5531da177e4SLinus Torvalds * and compare it against the name of the driver. Return whether 5541da177e4SLinus Torvalds * they match or not. 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds static int platform_match(struct device * dev, struct device_driver * drv) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds struct platform_device *pdev = container_of(dev, struct platform_device, dev); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 564386415d8SDavid Brownell static int platform_suspend(struct device *dev, pm_message_t mesg) 5651da177e4SLinus Torvalds { 5661da177e4SLinus Torvalds int ret = 0; 5671da177e4SLinus Torvalds 5689480e307SRussell King if (dev->driver && dev->driver->suspend) 569386415d8SDavid Brownell ret = dev->driver->suspend(dev, mesg); 570386415d8SDavid Brownell 571386415d8SDavid Brownell return ret; 572386415d8SDavid Brownell } 573386415d8SDavid Brownell 574386415d8SDavid Brownell static int platform_suspend_late(struct device *dev, pm_message_t mesg) 575386415d8SDavid Brownell { 576386415d8SDavid Brownell struct platform_driver *drv = to_platform_driver(dev->driver); 577386415d8SDavid Brownell struct platform_device *pdev = container_of(dev, struct platform_device, dev); 578386415d8SDavid Brownell int ret = 0; 579386415d8SDavid Brownell 580386415d8SDavid Brownell if (dev->driver && drv->suspend_late) 581386415d8SDavid Brownell ret = drv->suspend_late(pdev, mesg); 582386415d8SDavid Brownell 583386415d8SDavid Brownell return ret; 584386415d8SDavid Brownell } 585386415d8SDavid Brownell 586386415d8SDavid Brownell static int platform_resume_early(struct device *dev) 587386415d8SDavid Brownell { 588386415d8SDavid Brownell struct platform_driver *drv = to_platform_driver(dev->driver); 589386415d8SDavid Brownell struct platform_device *pdev = container_of(dev, struct platform_device, dev); 590386415d8SDavid Brownell int ret = 0; 591386415d8SDavid Brownell 592386415d8SDavid Brownell if (dev->driver && drv->resume_early) 593386415d8SDavid Brownell ret = drv->resume_early(pdev); 5949480e307SRussell King 5951da177e4SLinus Torvalds return ret; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds static int platform_resume(struct device * dev) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds int ret = 0; 6011da177e4SLinus Torvalds 6029480e307SRussell King if (dev->driver && dev->driver->resume) 6039480e307SRussell King ret = dev->driver->resume(dev); 6049480e307SRussell King 6051da177e4SLinus Torvalds return ret; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds struct bus_type platform_bus_type = { 6091da177e4SLinus Torvalds .name = "platform", 610a0245f7aSDavid Brownell .dev_attrs = platform_dev_attrs, 6111da177e4SLinus Torvalds .match = platform_match, 612a0245f7aSDavid Brownell .uevent = platform_uevent, 6131da177e4SLinus Torvalds .suspend = platform_suspend, 614386415d8SDavid Brownell .suspend_late = platform_suspend_late, 615386415d8SDavid Brownell .resume_early = platform_resume_early, 6161da177e4SLinus Torvalds .resume = platform_resume, 6171da177e4SLinus Torvalds }; 618a96b2042SDmitry Torokhov EXPORT_SYMBOL_GPL(platform_bus_type); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds int __init platform_bus_init(void) 6211da177e4SLinus Torvalds { 622fbfb1445SCornelia Huck int error; 623fbfb1445SCornelia Huck 624fbfb1445SCornelia Huck error = device_register(&platform_bus); 625fbfb1445SCornelia Huck if (error) 626fbfb1445SCornelia Huck return error; 627fbfb1445SCornelia Huck error = bus_register(&platform_bus_type); 628fbfb1445SCornelia Huck if (error) 629fbfb1445SCornelia Huck device_unregister(&platform_bus); 630fbfb1445SCornelia Huck return error; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK 6341da177e4SLinus Torvalds u64 dma_get_required_mask(struct device *dev) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT); 6371da177e4SLinus Torvalds u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT)); 6381da177e4SLinus Torvalds u64 mask; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds if (!high_totalram) { 6411da177e4SLinus Torvalds /* convert to mask just covering totalram */ 6421da177e4SLinus Torvalds low_totalram = (1 << (fls(low_totalram) - 1)); 6431da177e4SLinus Torvalds low_totalram += low_totalram - 1; 6441da177e4SLinus Torvalds mask = low_totalram; 6451da177e4SLinus Torvalds } else { 6461da177e4SLinus Torvalds high_totalram = (1 << (fls(high_totalram) - 1)); 6471da177e4SLinus Torvalds high_totalram += high_totalram - 1; 6481da177e4SLinus Torvalds mask = (((u64)high_totalram) << 32) + 0xffffffff; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds return mask & *dev->dma_mask; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dma_get_required_mask); 6531da177e4SLinus Torvalds #endif 654