1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * platform_device.h - generic, centralized driver model
7 * See Documentation/driver-api/driver-model/ for more information.
10 #ifndef _PLATFORM_DEVICE_H_
11 #define _PLATFORM_DEVICE_H_
13 #include <linux/device.h>
15 #define PLATFORM_DEVID_NONE (-1)
16 #define PLATFORM_DEVID_AUTO (-2)
20 struct property_entry;
21 struct platform_device_id;
23 struct platform_device {
28 u64 platform_dma_mask;
29 struct device_dma_parameters dma_parms;
31 struct resource *resource;
33 const struct platform_device_id *id_entry;
35 * Driver name to force a match. Do not set directly, because core
36 * frees it. Use driver_set_override() to set or clear it.
38 const char *driver_override;
40 /* MFD cell pointer */
41 struct mfd_cell *mfd_cell;
43 /* arch specific additions */
44 struct pdev_archdata archdata;
47 #define platform_get_device_id(pdev) ((pdev)->id_entry)
49 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
50 #define to_platform_device(x) container_of((x), struct platform_device, dev)
52 extern int platform_device_register(struct platform_device *);
53 extern void platform_device_unregister(struct platform_device *);
55 extern struct bus_type platform_bus_type;
56 extern struct device platform_bus;
58 extern struct resource *platform_get_resource(struct platform_device *,
59 unsigned int, unsigned int);
60 extern struct resource *platform_get_mem_or_io(struct platform_device *,
63 extern struct device *
64 platform_find_device_by_driver(struct device *start,
65 const struct device_driver *drv);
67 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
68 unsigned int index, struct resource **res);
70 devm_platform_ioremap_resource(struct platform_device *pdev,
73 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
75 extern int platform_get_irq(struct platform_device *, unsigned int);
76 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
77 extern int platform_irq_count(struct platform_device *);
78 extern int devm_platform_get_irqs_affinity(struct platform_device *dev,
79 struct irq_affinity *affd,
83 extern struct resource *platform_get_resource_byname(struct platform_device *,
86 extern int platform_get_irq_byname(struct platform_device *, const char *);
87 extern int platform_get_irq_byname_optional(struct platform_device *dev,
89 extern int platform_add_devices(struct platform_device **, int);
91 struct platform_device_info {
92 struct device *parent;
93 struct fwnode_handle *fwnode;
99 const struct resource *res;
100 unsigned int num_res;
106 const struct property_entry *properties;
108 extern struct platform_device *platform_device_register_full(
109 const struct platform_device_info *pdevinfo);
112 * platform_device_register_resndata - add a platform-level device with
113 * resources and platform-specific data
115 * @parent: parent device for the device we're adding
116 * @name: base name of the device we're adding
118 * @res: set of resources that needs to be allocated for the device
119 * @num: number of resources
120 * @data: platform specific data for this platform device
121 * @size: size of platform specific data
123 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
125 static inline struct platform_device *platform_device_register_resndata(
126 struct device *parent, const char *name, int id,
127 const struct resource *res, unsigned int num,
128 const void *data, size_t size) {
130 struct platform_device_info pdevinfo = {
141 return platform_device_register_full(&pdevinfo);
145 * platform_device_register_simple - add a platform-level device and its resources
146 * @name: base name of the device we're adding
148 * @res: set of resources that needs to be allocated for the device
149 * @num: number of resources
151 * This function creates a simple platform device that requires minimal
152 * resource and memory management. Canned release function freeing memory
153 * allocated for the device allows drivers using such devices to be
154 * unloaded without waiting for the last reference to the device to be
157 * This interface is primarily intended for use with legacy drivers which
158 * probe hardware directly. Because such drivers create sysfs device nodes
159 * themselves, rather than letting system infrastructure handle such device
160 * enumeration tasks, they don't fully conform to the Linux driver model.
161 * In particular, when such drivers are built as modules, they can't be
164 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
166 static inline struct platform_device *platform_device_register_simple(
167 const char *name, int id,
168 const struct resource *res, unsigned int num)
170 return platform_device_register_resndata(NULL, name, id,
175 * platform_device_register_data - add a platform-level device with platform-specific data
176 * @parent: parent device for the device we're adding
177 * @name: base name of the device we're adding
179 * @data: platform specific data for this platform device
180 * @size: size of platform specific data
182 * This function creates a simple platform device that requires minimal
183 * resource and memory management. Canned release function freeing memory
184 * allocated for the device allows drivers using such devices to be
185 * unloaded without waiting for the last reference to the device to be
188 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
190 static inline struct platform_device *platform_device_register_data(
191 struct device *parent, const char *name, int id,
192 const void *data, size_t size)
194 return platform_device_register_resndata(parent, name, id,
195 NULL, 0, data, size);
198 extern struct platform_device *platform_device_alloc(const char *name, int id);
199 extern int platform_device_add_resources(struct platform_device *pdev,
200 const struct resource *res,
202 extern int platform_device_add_data(struct platform_device *pdev,
203 const void *data, size_t size);
204 extern int platform_device_add(struct platform_device *pdev);
205 extern void platform_device_del(struct platform_device *pdev);
206 extern void platform_device_put(struct platform_device *pdev);
208 struct platform_driver {
209 int (*probe)(struct platform_device *);
210 int (*remove)(struct platform_device *);
211 void (*shutdown)(struct platform_device *);
212 int (*suspend)(struct platform_device *, pm_message_t state);
213 int (*resume)(struct platform_device *);
214 struct device_driver driver;
215 const struct platform_device_id *id_table;
216 bool prevent_deferred_probe;
218 * For most device drivers, no need to care about this flag as long as
219 * all DMAs are handled through the kernel DMA API. For some special
220 * ones, for example VFIO drivers, they know how to manage the DMA
221 * themselves and set this flag so that the IOMMU layer will allow them
222 * to setup and manage their own I/O address space.
224 bool driver_managed_dma;
227 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
231 * use a macro to avoid include chaining to get THIS_MODULE
233 #define platform_driver_register(drv) \
234 __platform_driver_register(drv, THIS_MODULE)
235 extern int __platform_driver_register(struct platform_driver *,
237 extern void platform_driver_unregister(struct platform_driver *);
239 /* non-hotpluggable platform devices may use this so that probe() and
240 * its support may live in __init sections, conserving runtime memory.
242 #define platform_driver_probe(drv, probe) \
243 __platform_driver_probe(drv, probe, THIS_MODULE)
244 extern int __platform_driver_probe(struct platform_driver *driver,
245 int (*probe)(struct platform_device *), struct module *module);
247 static inline void *platform_get_drvdata(const struct platform_device *pdev)
249 return dev_get_drvdata(&pdev->dev);
252 static inline void platform_set_drvdata(struct platform_device *pdev,
255 dev_set_drvdata(&pdev->dev, data);
258 /* module_platform_driver() - Helper macro for drivers that don't do
259 * anything special in module init/exit. This eliminates a lot of
260 * boilerplate. Each module may only use this macro once, and
261 * calling it replaces module_init() and module_exit()
263 #define module_platform_driver(__platform_driver) \
264 module_driver(__platform_driver, platform_driver_register, \
265 platform_driver_unregister)
267 /* builtin_platform_driver() - Helper macro for builtin drivers that
268 * don't do anything special in driver init. This eliminates some
269 * boilerplate. Each driver may only use this macro once, and
270 * calling it replaces device_initcall(). Note this is meant to be
271 * a parallel of module_platform_driver() above, but w/o _exit stuff.
273 #define builtin_platform_driver(__platform_driver) \
274 builtin_driver(__platform_driver, platform_driver_register)
276 /* module_platform_driver_probe() - Helper macro for drivers that don't do
277 * anything special in module init/exit. This eliminates a lot of
278 * boilerplate. Each module may only use this macro once, and
279 * calling it replaces module_init() and module_exit()
281 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
282 static int __init __platform_driver##_init(void) \
284 return platform_driver_probe(&(__platform_driver), \
287 module_init(__platform_driver##_init); \
288 static void __exit __platform_driver##_exit(void) \
290 platform_driver_unregister(&(__platform_driver)); \
292 module_exit(__platform_driver##_exit);
294 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
295 * anything special in device init. This eliminates some boilerplate. Each
296 * driver may only use this macro once, and using it replaces device_initcall.
297 * This is meant to be a parallel of module_platform_driver_probe above, but
298 * without the __exit parts.
300 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
301 static int __init __platform_driver##_init(void) \
303 return platform_driver_probe(&(__platform_driver), \
306 device_initcall(__platform_driver##_init); \
308 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
309 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
310 extern struct platform_device *__platform_create_bundle(
311 struct platform_driver *driver, int (*probe)(struct platform_device *),
312 struct resource *res, unsigned int n_res,
313 const void *data, size_t size, struct module *module);
315 int __platform_register_drivers(struct platform_driver * const *drivers,
316 unsigned int count, struct module *owner);
317 void platform_unregister_drivers(struct platform_driver * const *drivers,
320 #define platform_register_drivers(drivers, count) \
321 __platform_register_drivers(drivers, count, THIS_MODULE)
323 #ifdef CONFIG_SUSPEND
324 extern int platform_pm_suspend(struct device *dev);
325 extern int platform_pm_resume(struct device *dev);
327 #define platform_pm_suspend NULL
328 #define platform_pm_resume NULL
331 #ifdef CONFIG_HIBERNATE_CALLBACKS
332 extern int platform_pm_freeze(struct device *dev);
333 extern int platform_pm_thaw(struct device *dev);
334 extern int platform_pm_poweroff(struct device *dev);
335 extern int platform_pm_restore(struct device *dev);
337 #define platform_pm_freeze NULL
338 #define platform_pm_thaw NULL
339 #define platform_pm_poweroff NULL
340 #define platform_pm_restore NULL
343 #ifdef CONFIG_PM_SLEEP
344 #define USE_PLATFORM_PM_SLEEP_OPS \
345 .suspend = platform_pm_suspend, \
346 .resume = platform_pm_resume, \
347 .freeze = platform_pm_freeze, \
348 .thaw = platform_pm_thaw, \
349 .poweroff = platform_pm_poweroff, \
350 .restore = platform_pm_restore,
352 #define USE_PLATFORM_PM_SLEEP_OPS
355 #ifndef CONFIG_SUPERH
357 * REVISIT: This stub is needed for all non-SuperH users of early platform
358 * drivers. It should go away once we introduce the new platform_device-based
359 * early driver framework.
361 static inline int is_sh_early_platform_device(struct platform_device *pdev)
365 #endif /* CONFIG_SUPERH */
367 /* For now only SuperH uses it */
368 void early_platform_cleanup(void);
370 #endif /* _PLATFORM_DEVICE_H_ */