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)
19 struct property_entry;
20 struct platform_device_id;
22 struct platform_device {
27 u64 platform_dma_mask;
28 struct device_dma_parameters dma_parms;
30 struct resource *resource;
32 const struct platform_device_id *id_entry;
33 char *driver_override; /* Driver name to force a match */
35 /* MFD cell pointer */
36 struct mfd_cell *mfd_cell;
38 /* arch specific additions */
39 struct pdev_archdata archdata;
42 #define platform_get_device_id(pdev) ((pdev)->id_entry)
44 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
45 #define to_platform_device(x) container_of((x), struct platform_device, dev)
47 extern int platform_device_register(struct platform_device *);
48 extern void platform_device_unregister(struct platform_device *);
50 extern struct bus_type platform_bus_type;
51 extern struct device platform_bus;
53 extern struct resource *platform_get_resource(struct platform_device *,
54 unsigned int, unsigned int);
55 extern struct device *
56 platform_find_device_by_driver(struct device *start,
57 const struct device_driver *drv);
59 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
60 unsigned int index, struct resource **res);
62 devm_platform_ioremap_resource(struct platform_device *pdev,
65 devm_platform_ioremap_resource_wc(struct platform_device *pdev,
68 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
70 extern int platform_get_irq(struct platform_device *, unsigned int);
71 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
72 extern int platform_irq_count(struct platform_device *);
73 extern struct resource *platform_get_resource_byname(struct platform_device *,
76 extern int platform_get_irq_byname(struct platform_device *, const char *);
77 extern int platform_get_irq_byname_optional(struct platform_device *dev,
79 extern int platform_add_devices(struct platform_device **, int);
81 struct platform_device_info {
82 struct device *parent;
83 struct fwnode_handle *fwnode;
89 const struct resource *res;
96 const struct property_entry *properties;
98 extern struct platform_device *platform_device_register_full(
99 const struct platform_device_info *pdevinfo);
102 * platform_device_register_resndata - add a platform-level device with
103 * resources and platform-specific data
105 * @parent: parent device for the device we're adding
106 * @name: base name of the device we're adding
108 * @res: set of resources that needs to be allocated for the device
109 * @num: number of resources
110 * @data: platform specific data for this platform device
111 * @size: size of platform specific data
113 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
115 static inline struct platform_device *platform_device_register_resndata(
116 struct device *parent, const char *name, int id,
117 const struct resource *res, unsigned int num,
118 const void *data, size_t size) {
120 struct platform_device_info pdevinfo = {
131 return platform_device_register_full(&pdevinfo);
135 * platform_device_register_simple - add a platform-level device and its resources
136 * @name: base name of the device we're adding
138 * @res: set of resources that needs to be allocated for the device
139 * @num: number of resources
141 * This function creates a simple platform device that requires minimal
142 * resource and memory management. Canned release function freeing memory
143 * allocated for the device allows drivers using such devices to be
144 * unloaded without waiting for the last reference to the device to be
147 * This interface is primarily intended for use with legacy drivers which
148 * probe hardware directly. Because such drivers create sysfs device nodes
149 * themselves, rather than letting system infrastructure handle such device
150 * enumeration tasks, they don't fully conform to the Linux driver model.
151 * In particular, when such drivers are built as modules, they can't be
154 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
156 static inline struct platform_device *platform_device_register_simple(
157 const char *name, int id,
158 const struct resource *res, unsigned int num)
160 return platform_device_register_resndata(NULL, name, id,
165 * platform_device_register_data - add a platform-level device with platform-specific data
166 * @parent: parent device for the device we're adding
167 * @name: base name of the device we're adding
169 * @data: platform specific data for this platform device
170 * @size: size of platform specific data
172 * This function creates a simple platform device that requires minimal
173 * resource and memory management. Canned release function freeing memory
174 * allocated for the device allows drivers using such devices to be
175 * unloaded without waiting for the last reference to the device to be
178 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
180 static inline struct platform_device *platform_device_register_data(
181 struct device *parent, const char *name, int id,
182 const void *data, size_t size)
184 return platform_device_register_resndata(parent, name, id,
185 NULL, 0, data, size);
188 extern struct platform_device *platform_device_alloc(const char *name, int id);
189 extern int platform_device_add_resources(struct platform_device *pdev,
190 const struct resource *res,
192 extern int platform_device_add_data(struct platform_device *pdev,
193 const void *data, size_t size);
194 extern int platform_device_add_properties(struct platform_device *pdev,
195 const struct property_entry *properties);
196 extern int platform_device_add(struct platform_device *pdev);
197 extern void platform_device_del(struct platform_device *pdev);
198 extern void platform_device_put(struct platform_device *pdev);
200 struct platform_driver {
201 int (*probe)(struct platform_device *);
202 int (*remove)(struct platform_device *);
203 void (*shutdown)(struct platform_device *);
204 int (*suspend)(struct platform_device *, pm_message_t state);
205 int (*resume)(struct platform_device *);
206 struct device_driver driver;
207 const struct platform_device_id *id_table;
208 bool prevent_deferred_probe;
211 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
215 * use a macro to avoid include chaining to get THIS_MODULE
217 #define platform_driver_register(drv) \
218 __platform_driver_register(drv, THIS_MODULE)
219 extern int __platform_driver_register(struct platform_driver *,
221 extern void platform_driver_unregister(struct platform_driver *);
223 /* non-hotpluggable platform devices may use this so that probe() and
224 * its support may live in __init sections, conserving runtime memory.
226 #define platform_driver_probe(drv, probe) \
227 __platform_driver_probe(drv, probe, THIS_MODULE)
228 extern int __platform_driver_probe(struct platform_driver *driver,
229 int (*probe)(struct platform_device *), struct module *module);
231 static inline void *platform_get_drvdata(const struct platform_device *pdev)
233 return dev_get_drvdata(&pdev->dev);
236 static inline void platform_set_drvdata(struct platform_device *pdev,
239 dev_set_drvdata(&pdev->dev, data);
242 /* module_platform_driver() - Helper macro for drivers that don't do
243 * anything special in module init/exit. This eliminates a lot of
244 * boilerplate. Each module may only use this macro once, and
245 * calling it replaces module_init() and module_exit()
247 #define module_platform_driver(__platform_driver) \
248 module_driver(__platform_driver, platform_driver_register, \
249 platform_driver_unregister)
251 /* builtin_platform_driver() - Helper macro for builtin drivers that
252 * don't do anything special in driver init. This eliminates some
253 * boilerplate. Each driver may only use this macro once, and
254 * calling it replaces device_initcall(). Note this is meant to be
255 * a parallel of module_platform_driver() above, but w/o _exit stuff.
257 #define builtin_platform_driver(__platform_driver) \
258 builtin_driver(__platform_driver, platform_driver_register)
260 /* module_platform_driver_probe() - Helper macro for drivers that don't do
261 * anything special in module init/exit. This eliminates a lot of
262 * boilerplate. Each module may only use this macro once, and
263 * calling it replaces module_init() and module_exit()
265 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
266 static int __init __platform_driver##_init(void) \
268 return platform_driver_probe(&(__platform_driver), \
271 module_init(__platform_driver##_init); \
272 static void __exit __platform_driver##_exit(void) \
274 platform_driver_unregister(&(__platform_driver)); \
276 module_exit(__platform_driver##_exit);
278 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
279 * anything special in device init. This eliminates some boilerplate. Each
280 * driver may only use this macro once, and using it replaces device_initcall.
281 * This is meant to be a parallel of module_platform_driver_probe above, but
282 * without the __exit parts.
284 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
285 static int __init __platform_driver##_init(void) \
287 return platform_driver_probe(&(__platform_driver), \
290 device_initcall(__platform_driver##_init); \
292 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
293 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
294 extern struct platform_device *__platform_create_bundle(
295 struct platform_driver *driver, int (*probe)(struct platform_device *),
296 struct resource *res, unsigned int n_res,
297 const void *data, size_t size, struct module *module);
299 int __platform_register_drivers(struct platform_driver * const *drivers,
300 unsigned int count, struct module *owner);
301 void platform_unregister_drivers(struct platform_driver * const *drivers,
304 #define platform_register_drivers(drivers, count) \
305 __platform_register_drivers(drivers, count, THIS_MODULE)
307 #ifdef CONFIG_SUSPEND
308 extern int platform_pm_suspend(struct device *dev);
309 extern int platform_pm_resume(struct device *dev);
311 #define platform_pm_suspend NULL
312 #define platform_pm_resume NULL
315 #ifdef CONFIG_HIBERNATE_CALLBACKS
316 extern int platform_pm_freeze(struct device *dev);
317 extern int platform_pm_thaw(struct device *dev);
318 extern int platform_pm_poweroff(struct device *dev);
319 extern int platform_pm_restore(struct device *dev);
321 #define platform_pm_freeze NULL
322 #define platform_pm_thaw NULL
323 #define platform_pm_poweroff NULL
324 #define platform_pm_restore NULL
327 extern int platform_dma_configure(struct device *dev);
329 #ifdef CONFIG_PM_SLEEP
330 #define USE_PLATFORM_PM_SLEEP_OPS \
331 .suspend = platform_pm_suspend, \
332 .resume = platform_pm_resume, \
333 .freeze = platform_pm_freeze, \
334 .thaw = platform_pm_thaw, \
335 .poweroff = platform_pm_poweroff, \
336 .restore = platform_pm_restore,
338 #define USE_PLATFORM_PM_SLEEP_OPS
341 #ifndef CONFIG_SUPERH
343 * REVISIT: This stub is needed for all non-SuperH users of early platform
344 * drivers. It should go away once we introduce the new platform_device-based
345 * early driver framework.
347 static inline int is_sh_early_platform_device(struct platform_device *pdev)
351 #endif /* CONFIG_SUPERH */
353 #endif /* _PLATFORM_DEVICE_H_ */