1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * platform_device.h - generic, centralized driver model
5 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
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)
19 struct property_entry;
20 struct platform_device_id;
22 struct platform_device {
29 struct resource *resource;
31 const struct platform_device_id *id_entry;
32 char *driver_override; /* Driver name to force a match */
34 /* MFD cell pointer */
35 struct mfd_cell *mfd_cell;
37 /* arch specific additions */
38 struct pdev_archdata archdata;
41 #define platform_get_device_id(pdev) ((pdev)->id_entry)
43 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
44 #define to_platform_device(x) container_of((x), struct platform_device, dev)
46 extern int platform_device_register(struct platform_device *);
47 extern void platform_device_unregister(struct platform_device *);
49 extern struct bus_type platform_bus_type;
50 extern struct device platform_bus;
52 extern struct resource *platform_get_resource(struct platform_device *,
53 unsigned int, unsigned int);
54 extern struct device *
55 platform_find_device_by_driver(struct device *start,
56 const struct device_driver *drv);
58 devm_platform_ioremap_resource(struct platform_device *pdev,
61 devm_platform_ioremap_resource_wc(struct platform_device *pdev,
64 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
66 extern int platform_get_irq(struct platform_device *, unsigned int);
67 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
68 extern int platform_irq_count(struct platform_device *);
69 extern struct resource *platform_get_resource_byname(struct platform_device *,
72 extern int platform_get_irq_byname(struct platform_device *, const char *);
73 extern int platform_get_irq_byname_optional(struct platform_device *dev,
75 extern int platform_add_devices(struct platform_device **, int);
77 struct platform_device_info {
78 struct device *parent;
79 struct fwnode_handle *fwnode;
85 const struct resource *res;
92 struct property_entry *properties;
94 extern struct platform_device *platform_device_register_full(
95 const struct platform_device_info *pdevinfo);
98 * platform_device_register_resndata - add a platform-level device with
99 * resources and platform-specific data
101 * @parent: parent device for the device we're adding
102 * @name: base name of the device we're adding
104 * @res: set of resources that needs to be allocated for the device
105 * @num: number of resources
106 * @data: platform specific data for this platform device
107 * @size: size of platform specific data
109 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
111 static inline struct platform_device *platform_device_register_resndata(
112 struct device *parent, const char *name, int id,
113 const struct resource *res, unsigned int num,
114 const void *data, size_t size) {
116 struct platform_device_info pdevinfo = {
127 return platform_device_register_full(&pdevinfo);
131 * platform_device_register_simple - add a platform-level device and its resources
132 * @name: base name of the device we're adding
134 * @res: set of resources that needs to be allocated for the device
135 * @num: number of resources
137 * This function creates a simple platform device that requires minimal
138 * resource and memory management. Canned release function freeing memory
139 * allocated for the device allows drivers using such devices to be
140 * unloaded without waiting for the last reference to the device to be
143 * This interface is primarily intended for use with legacy drivers which
144 * probe hardware directly. Because such drivers create sysfs device nodes
145 * themselves, rather than letting system infrastructure handle such device
146 * enumeration tasks, they don't fully conform to the Linux driver model.
147 * In particular, when such drivers are built as modules, they can't be
150 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
152 static inline struct platform_device *platform_device_register_simple(
153 const char *name, int id,
154 const struct resource *res, unsigned int num)
156 return platform_device_register_resndata(NULL, name, id,
161 * platform_device_register_data - add a platform-level device with platform-specific data
162 * @parent: parent device for the device we're adding
163 * @name: base name of the device we're adding
165 * @data: platform specific data for this platform device
166 * @size: size of platform specific data
168 * This function creates a simple platform device that requires minimal
169 * resource and memory management. Canned release function freeing memory
170 * allocated for the device allows drivers using such devices to be
171 * unloaded without waiting for the last reference to the device to be
174 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
176 static inline struct platform_device *platform_device_register_data(
177 struct device *parent, const char *name, int id,
178 const void *data, size_t size)
180 return platform_device_register_resndata(parent, name, id,
181 NULL, 0, data, size);
184 extern struct platform_device *platform_device_alloc(const char *name, int id);
185 extern int platform_device_add_resources(struct platform_device *pdev,
186 const struct resource *res,
188 extern int platform_device_add_data(struct platform_device *pdev,
189 const void *data, size_t size);
190 extern int platform_device_add_properties(struct platform_device *pdev,
191 const struct property_entry *properties);
192 extern int platform_device_add(struct platform_device *pdev);
193 extern void platform_device_del(struct platform_device *pdev);
194 extern void platform_device_put(struct platform_device *pdev);
196 struct platform_driver {
197 int (*probe)(struct platform_device *);
198 int (*remove)(struct platform_device *);
199 void (*shutdown)(struct platform_device *);
200 int (*suspend)(struct platform_device *, pm_message_t state);
201 int (*resume)(struct platform_device *);
202 struct device_driver driver;
203 const struct platform_device_id *id_table;
204 bool prevent_deferred_probe;
207 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
211 * use a macro to avoid include chaining to get THIS_MODULE
213 #define platform_driver_register(drv) \
214 __platform_driver_register(drv, THIS_MODULE)
215 extern int __platform_driver_register(struct platform_driver *,
217 extern void platform_driver_unregister(struct platform_driver *);
219 /* non-hotpluggable platform devices may use this so that probe() and
220 * its support may live in __init sections, conserving runtime memory.
222 #define platform_driver_probe(drv, probe) \
223 __platform_driver_probe(drv, probe, THIS_MODULE)
224 extern int __platform_driver_probe(struct platform_driver *driver,
225 int (*probe)(struct platform_device *), struct module *module);
227 static inline void *platform_get_drvdata(const struct platform_device *pdev)
229 return dev_get_drvdata(&pdev->dev);
232 static inline void platform_set_drvdata(struct platform_device *pdev,
235 dev_set_drvdata(&pdev->dev, data);
238 /* module_platform_driver() - Helper macro for drivers that don't do
239 * anything special in module init/exit. This eliminates a lot of
240 * boilerplate. Each module may only use this macro once, and
241 * calling it replaces module_init() and module_exit()
243 #define module_platform_driver(__platform_driver) \
244 module_driver(__platform_driver, platform_driver_register, \
245 platform_driver_unregister)
247 /* builtin_platform_driver() - Helper macro for builtin drivers that
248 * don't do anything special in driver init. This eliminates some
249 * boilerplate. Each driver may only use this macro once, and
250 * calling it replaces device_initcall(). Note this is meant to be
251 * a parallel of module_platform_driver() above, but w/o _exit stuff.
253 #define builtin_platform_driver(__platform_driver) \
254 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()
261 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
262 static int __init __platform_driver##_init(void) \
264 return platform_driver_probe(&(__platform_driver), \
267 module_init(__platform_driver##_init); \
268 static void __exit __platform_driver##_exit(void) \
270 platform_driver_unregister(&(__platform_driver)); \
272 module_exit(__platform_driver##_exit);
274 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
275 * anything special in device init. This eliminates some boilerplate. Each
276 * driver may only use this macro once, and using it replaces device_initcall.
277 * This is meant to be a parallel of module_platform_driver_probe above, but
278 * without the __exit parts.
280 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
281 static int __init __platform_driver##_init(void) \
283 return platform_driver_probe(&(__platform_driver), \
286 device_initcall(__platform_driver##_init); \
288 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
289 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
290 extern struct platform_device *__platform_create_bundle(
291 struct platform_driver *driver, int (*probe)(struct platform_device *),
292 struct resource *res, unsigned int n_res,
293 const void *data, size_t size, struct module *module);
295 int __platform_register_drivers(struct platform_driver * const *drivers,
296 unsigned int count, struct module *owner);
297 void platform_unregister_drivers(struct platform_driver * const *drivers,
300 #define platform_register_drivers(drivers, count) \
301 __platform_register_drivers(drivers, count, THIS_MODULE)
303 #ifdef CONFIG_SUSPEND
304 extern int platform_pm_suspend(struct device *dev);
305 extern int platform_pm_resume(struct device *dev);
307 #define platform_pm_suspend NULL
308 #define platform_pm_resume NULL
311 #ifdef CONFIG_HIBERNATE_CALLBACKS
312 extern int platform_pm_freeze(struct device *dev);
313 extern int platform_pm_thaw(struct device *dev);
314 extern int platform_pm_poweroff(struct device *dev);
315 extern int platform_pm_restore(struct device *dev);
317 #define platform_pm_freeze NULL
318 #define platform_pm_thaw NULL
319 #define platform_pm_poweroff NULL
320 #define platform_pm_restore NULL
323 extern int platform_dma_configure(struct device *dev);
325 #ifdef CONFIG_PM_SLEEP
326 #define USE_PLATFORM_PM_SLEEP_OPS \
327 .suspend = platform_pm_suspend, \
328 .resume = platform_pm_resume, \
329 .freeze = platform_pm_freeze, \
330 .thaw = platform_pm_thaw, \
331 .poweroff = platform_pm_poweroff, \
332 .restore = platform_pm_restore,
334 #define USE_PLATFORM_PM_SLEEP_OPS
337 #ifndef CONFIG_SUPERH
339 * REVISIT: This stub is needed for all non-SuperH users of early platform
340 * drivers. It should go away once we introduce the new platform_device-based
341 * early driver framework.
343 static inline int is_sh_early_platform_device(struct platform_device *pdev)
347 #endif /* CONFIG_SUPERH */
349 #endif /* _PLATFORM_DEVICE_H_ */