platform_device.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * platform_device.h - generic, centralized driver model
  3. *
  4. * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * See Documentation/driver-model/ for more information.
  9. */
  10. #ifndef _PLATFORM_DEVICE_H_
  11. #define _PLATFORM_DEVICE_H_
  12. #include <linux/device.h>
  13. #include <linux/mod_devicetable.h>
  14. #define PLATFORM_DEVID_NONE (-1)
  15. #define PLATFORM_DEVID_AUTO (-2)
  16. struct mfd_cell;
  17. struct platform_device {
  18. const char * name;
  19. int id;
  20. bool id_auto;
  21. struct device dev;
  22. u32 num_resources;
  23. struct resource * resource;
  24. const struct platform_device_id *id_entry;
  25. /* MFD cell pointer */
  26. struct mfd_cell *mfd_cell;
  27. /* arch specific additions */
  28. struct pdev_archdata archdata;
  29. };
  30. #define platform_get_device_id(pdev) ((pdev)->id_entry)
  31. #define to_platform_device(x) container_of((x), struct platform_device, dev)
  32. extern int platform_device_register(struct platform_device *);
  33. extern void platform_device_unregister(struct platform_device *);
  34. extern struct bus_type platform_bus_type;
  35. extern struct device platform_bus;
  36. extern void arch_setup_pdev_archdata(struct platform_device *);
  37. extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
  38. extern int platform_get_irq(struct platform_device *, unsigned int);
  39. extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, const char *);
  40. extern int platform_get_irq_byname(struct platform_device *, const char *);
  41. extern int platform_add_devices(struct platform_device **, int);
  42. struct platform_device_info {
  43. struct device *parent;
  44. const char *name;
  45. int id;
  46. const struct resource *res;
  47. unsigned int num_res;
  48. const void *data;
  49. size_t size_data;
  50. u64 dma_mask;
  51. };
  52. extern struct platform_device *platform_device_register_full(
  53. const struct platform_device_info *pdevinfo);
  54. /**
  55. * platform_device_register_resndata - add a platform-level device with
  56. * resources and platform-specific data
  57. *
  58. * @parent: parent device for the device we're adding
  59. * @name: base name of the device we're adding
  60. * @id: instance id
  61. * @res: set of resources that needs to be allocated for the device
  62. * @num: number of resources
  63. * @data: platform specific data for this platform device
  64. * @size: size of platform specific data
  65. *
  66. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  67. */
  68. static inline struct platform_device *platform_device_register_resndata(
  69. struct device *parent, const char *name, int id,
  70. const struct resource *res, unsigned int num,
  71. const void *data, size_t size) {
  72. struct platform_device_info pdevinfo = {
  73. .parent = parent,
  74. .name = name,
  75. .id = id,
  76. .res = res,
  77. .num_res = num,
  78. .data = data,
  79. .size_data = size,
  80. .dma_mask = 0,
  81. };
  82. return platform_device_register_full(&pdevinfo);
  83. }
  84. /**
  85. * platform_device_register_simple - add a platform-level device and its resources
  86. * @name: base name of the device we're adding
  87. * @id: instance id
  88. * @res: set of resources that needs to be allocated for the device
  89. * @num: number of resources
  90. *
  91. * This function creates a simple platform device that requires minimal
  92. * resource and memory management. Canned release function freeing memory
  93. * allocated for the device allows drivers using such devices to be
  94. * unloaded without waiting for the last reference to the device to be
  95. * dropped.
  96. *
  97. * This interface is primarily intended for use with legacy drivers which
  98. * probe hardware directly. Because such drivers create sysfs device nodes
  99. * themselves, rather than letting system infrastructure handle such device
  100. * enumeration tasks, they don't fully conform to the Linux driver model.
  101. * In particular, when such drivers are built as modules, they can't be
  102. * "hotplugged".
  103. *
  104. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  105. */
  106. static inline struct platform_device *platform_device_register_simple(
  107. const char *name, int id,
  108. const struct resource *res, unsigned int num)
  109. {
  110. return platform_device_register_resndata(NULL, name, id,
  111. res, num, NULL, 0);
  112. }
  113. /**
  114. * platform_device_register_data - add a platform-level device with platform-specific data
  115. * @parent: parent device for the device we're adding
  116. * @name: base name of the device we're adding
  117. * @id: instance id
  118. * @data: platform specific data for this platform device
  119. * @size: size of platform specific data
  120. *
  121. * This function creates a simple platform device that requires minimal
  122. * resource and memory management. Canned release function freeing memory
  123. * allocated for the device allows drivers using such devices to be
  124. * unloaded without waiting for the last reference to the device to be
  125. * dropped.
  126. *
  127. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  128. */
  129. static inline struct platform_device *platform_device_register_data(
  130. struct device *parent, const char *name, int id,
  131. const void *data, size_t size)
  132. {
  133. return platform_device_register_resndata(parent, name, id,
  134. NULL, 0, data, size);
  135. }
  136. extern struct platform_device *platform_device_alloc(const char *name, int id);
  137. extern int platform_device_add_resources(struct platform_device *pdev,
  138. const struct resource *res,
  139. unsigned int num);
  140. extern int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size);
  141. extern int platform_device_add(struct platform_device *pdev);
  142. extern void platform_device_del(struct platform_device *pdev);
  143. extern void platform_device_put(struct platform_device *pdev);
  144. struct platform_driver {
  145. int (*probe)(struct platform_device *);
  146. int (*remove)(struct platform_device *);
  147. void (*shutdown)(struct platform_device *);
  148. int (*suspend)(struct platform_device *, pm_message_t state);
  149. int (*resume)(struct platform_device *);
  150. struct device_driver driver;
  151. const struct platform_device_id *id_table;
  152. };
  153. extern int platform_driver_register(struct platform_driver *);
  154. extern void platform_driver_unregister(struct platform_driver *);
  155. /* non-hotpluggable platform devices may use this so that probe() and
  156. * its support may live in __init sections, conserving runtime memory.
  157. */
  158. extern int platform_driver_probe(struct platform_driver *driver,
  159. int (*probe)(struct platform_device *));
  160. static inline void *platform_get_drvdata(const struct platform_device *pdev)
  161. {
  162. return dev_get_drvdata(&pdev->dev);
  163. }
  164. static inline void platform_set_drvdata(struct platform_device *pdev, void *data)
  165. {
  166. dev_set_drvdata(&pdev->dev, data);
  167. }
  168. /* module_platform_driver() - Helper macro for drivers that don't do
  169. * anything special in module init/exit. This eliminates a lot of
  170. * boilerplate. Each module may only use this macro once, and
  171. * calling it replaces module_init() and module_exit()
  172. */
  173. #define module_platform_driver(__platform_driver) \
  174. module_driver(__platform_driver, platform_driver_register, \
  175. platform_driver_unregister)
  176. extern struct platform_device *platform_create_bundle(struct platform_driver *driver,
  177. int (*probe)(struct platform_device *),
  178. struct resource *res, unsigned int n_res,
  179. const void *data, size_t size);
  180. /* early platform driver interface */
  181. struct early_platform_driver {
  182. const char *class_str;
  183. struct platform_driver *pdrv;
  184. struct list_head list;
  185. int requested_id;
  186. char *buffer;
  187. int bufsize;
  188. };
  189. #define EARLY_PLATFORM_ID_UNSET -2
  190. #define EARLY_PLATFORM_ID_ERROR -3
  191. extern int early_platform_driver_register(struct early_platform_driver *epdrv,
  192. char *buf);
  193. extern void early_platform_add_devices(struct platform_device **devs, int num);
  194. static inline int is_early_platform_device(struct platform_device *pdev)
  195. {
  196. return !pdev->dev.driver;
  197. }
  198. extern void early_platform_driver_register_all(char *class_str);
  199. extern int early_platform_driver_probe(char *class_str,
  200. int nr_probe, int user_only);
  201. extern void early_platform_cleanup(void);
  202. #define early_platform_init(class_string, platdrv) \
  203. early_platform_init_buffer(class_string, platdrv, NULL, 0)
  204. #ifndef MODULE
  205. #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
  206. static __initdata struct early_platform_driver early_driver = { \
  207. .class_str = class_string, \
  208. .buffer = buf, \
  209. .bufsize = bufsiz, \
  210. .pdrv = platdrv, \
  211. .requested_id = EARLY_PLATFORM_ID_UNSET, \
  212. }; \
  213. static int __init early_platform_driver_setup_func(char *buffer) \
  214. { \
  215. return early_platform_driver_register(&early_driver, buffer); \
  216. } \
  217. early_param(class_string, early_platform_driver_setup_func)
  218. #else /* MODULE */
  219. #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
  220. static inline char *early_platform_driver_setup_func(void) \
  221. { \
  222. return bufsiz ? buf : NULL; \
  223. }
  224. #endif /* MODULE */
  225. #ifdef CONFIG_SUSPEND
  226. extern int platform_pm_suspend(struct device *dev);
  227. extern int platform_pm_resume(struct device *dev);
  228. #else
  229. #define platform_pm_suspend NULL
  230. #define platform_pm_resume NULL
  231. #endif
  232. #ifdef CONFIG_HIBERNATE_CALLBACKS
  233. extern int platform_pm_freeze(struct device *dev);
  234. extern int platform_pm_thaw(struct device *dev);
  235. extern int platform_pm_poweroff(struct device *dev);
  236. extern int platform_pm_restore(struct device *dev);
  237. #else
  238. #define platform_pm_freeze NULL
  239. #define platform_pm_thaw NULL
  240. #define platform_pm_poweroff NULL
  241. #define platform_pm_restore NULL
  242. #endif
  243. #ifdef CONFIG_PM_SLEEP
  244. #define USE_PLATFORM_PM_SLEEP_OPS \
  245. .suspend = platform_pm_suspend, \
  246. .resume = platform_pm_resume, \
  247. .freeze = platform_pm_freeze, \
  248. .thaw = platform_pm_thaw, \
  249. .poweroff = platform_pm_poweroff, \
  250. .restore = platform_pm_restore,
  251. #else
  252. #define USE_PLATFORM_PM_SLEEP_OPS
  253. #endif
  254. #endif /* _PLATFORM_DEVICE_H_ */