platform_device.h 13 KB

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