platform_device.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /*
  159. * use a macro to avoid include chaining to get THIS_MODULE
  160. */
  161. #define platform_driver_register(drv) \
  162. __platform_driver_register(drv, THIS_MODULE)
  163. extern int __platform_driver_register(struct platform_driver *,
  164. struct module *);
  165. extern void platform_driver_unregister(struct platform_driver *);
  166. /* non-hotpluggable platform devices may use this so that probe() and
  167. * its support may live in __init sections, conserving runtime memory.
  168. */
  169. extern int platform_driver_probe(struct platform_driver *driver,
  170. int (*probe)(struct platform_device *));
  171. static inline void *platform_get_drvdata(const struct platform_device *pdev)
  172. {
  173. return dev_get_drvdata(&pdev->dev);
  174. }
  175. static inline void platform_set_drvdata(struct platform_device *pdev,
  176. void *data)
  177. {
  178. dev_set_drvdata(&pdev->dev, data);
  179. }
  180. /* module_platform_driver() - Helper macro for drivers that don't do
  181. * anything special in module init/exit. This eliminates a lot of
  182. * boilerplate. Each module may only use this macro once, and
  183. * calling it replaces module_init() and module_exit()
  184. */
  185. #define module_platform_driver(__platform_driver) \
  186. module_driver(__platform_driver, platform_driver_register, \
  187. platform_driver_unregister)
  188. /* module_platform_driver_probe() - Helper macro for drivers that don't do
  189. * anything special in module init/exit. This eliminates a lot of
  190. * boilerplate. Each module may only use this macro once, and
  191. * calling it replaces module_init() and module_exit()
  192. */
  193. #define module_platform_driver_probe(__platform_driver, __platform_probe) \
  194. static int __init __platform_driver##_init(void) \
  195. { \
  196. return platform_driver_probe(&(__platform_driver), \
  197. __platform_probe); \
  198. } \
  199. module_init(__platform_driver##_init); \
  200. static void __exit __platform_driver##_exit(void) \
  201. { \
  202. platform_driver_unregister(&(__platform_driver)); \
  203. } \
  204. module_exit(__platform_driver##_exit);
  205. extern struct platform_device *platform_create_bundle(
  206. struct platform_driver *driver, int (*probe)(struct platform_device *),
  207. struct resource *res, unsigned int n_res,
  208. const void *data, size_t size);
  209. /* early platform driver interface */
  210. struct early_platform_driver {
  211. const char *class_str;
  212. struct platform_driver *pdrv;
  213. struct list_head list;
  214. int requested_id;
  215. char *buffer;
  216. int bufsize;
  217. };
  218. #define EARLY_PLATFORM_ID_UNSET -2
  219. #define EARLY_PLATFORM_ID_ERROR -3
  220. extern int early_platform_driver_register(struct early_platform_driver *epdrv,
  221. char *buf);
  222. extern void early_platform_add_devices(struct platform_device **devs, int num);
  223. static inline int is_early_platform_device(struct platform_device *pdev)
  224. {
  225. return !pdev->dev.driver;
  226. }
  227. extern void early_platform_driver_register_all(char *class_str);
  228. extern int early_platform_driver_probe(char *class_str,
  229. int nr_probe, int user_only);
  230. extern void early_platform_cleanup(void);
  231. #define early_platform_init(class_string, platdrv) \
  232. early_platform_init_buffer(class_string, platdrv, NULL, 0)
  233. #ifndef MODULE
  234. #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
  235. static __initdata struct early_platform_driver early_driver = { \
  236. .class_str = class_string, \
  237. .buffer = buf, \
  238. .bufsize = bufsiz, \
  239. .pdrv = platdrv, \
  240. .requested_id = EARLY_PLATFORM_ID_UNSET, \
  241. }; \
  242. static int __init early_platform_driver_setup_func(char *buffer) \
  243. { \
  244. return early_platform_driver_register(&early_driver, buffer); \
  245. } \
  246. early_param(class_string, early_platform_driver_setup_func)
  247. #else /* MODULE */
  248. #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
  249. static inline char *early_platform_driver_setup_func(void) \
  250. { \
  251. return bufsiz ? buf : NULL; \
  252. }
  253. #endif /* MODULE */
  254. #ifdef CONFIG_SUSPEND
  255. extern int platform_pm_suspend(struct device *dev);
  256. extern int platform_pm_resume(struct device *dev);
  257. #else
  258. #define platform_pm_suspend NULL
  259. #define platform_pm_resume NULL
  260. #endif
  261. #ifdef CONFIG_HIBERNATE_CALLBACKS
  262. extern int platform_pm_freeze(struct device *dev);
  263. extern int platform_pm_thaw(struct device *dev);
  264. extern int platform_pm_poweroff(struct device *dev);
  265. extern int platform_pm_restore(struct device *dev);
  266. #else
  267. #define platform_pm_freeze NULL
  268. #define platform_pm_thaw NULL
  269. #define platform_pm_poweroff NULL
  270. #define platform_pm_restore NULL
  271. #endif
  272. #ifdef CONFIG_PM_SLEEP
  273. #define USE_PLATFORM_PM_SLEEP_OPS \
  274. .suspend = platform_pm_suspend, \
  275. .resume = platform_pm_resume, \
  276. .freeze = platform_pm_freeze, \
  277. .thaw = platform_pm_thaw, \
  278. .poweroff = platform_pm_poweroff, \
  279. .restore = platform_pm_restore,
  280. #else
  281. #define USE_PLATFORM_PM_SLEEP_OPS
  282. #endif
  283. #endif /* _PLATFORM_DEVICE_H_ */