platform_device.h 10 KB

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