platform_device.h 11 KB

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