platform_device.h 10 KB

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