platform_device.h 12 KB

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