platform_device.h 12 KB

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