platform_device.h 12 KB

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