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 {
28 struct resource *resource;
30 const struct platform_device_id *id_entry;
31 char *driver_override; /* Driver name to force a match */
33 /* MFD cell pointer */
34 struct mfd_cell *mfd_cell;
36 /* arch specific additions */
37 struct pdev_archdata archdata;
40 #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)
45 extern int platform_device_register(struct platform_device *);
46 extern void platform_device_unregister(struct platform_device *);
48 extern struct bus_type platform_bus_type;
49 extern struct device platform_bus;
51 extern void arch_setup_pdev_archdata(struct platform_device *);
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,
60 extern int platform_get_irq(struct platform_device *, unsigned int);
61 extern int platform_irq_count(struct platform_device *);
62 extern struct resource *platform_get_resource_byname(struct platform_device *,
65 extern int platform_get_irq_byname(struct platform_device *, const char *);
66 extern int platform_add_devices(struct platform_device **, int);
68 struct platform_device_info {
69 struct device *parent;
70 struct fwnode_handle *fwnode;
76 const struct resource *res;
83 struct property_entry *properties;
85 extern struct platform_device *platform_device_register_full(
86 const struct platform_device_info *pdevinfo);
89 * platform_device_register_resndata - add a platform-level device with
90 * resources and platform-specific data
92 * @parent: parent device for the device we're adding
93 * @name: base name of the device we're adding
95 * @res: set of resources that needs to be allocated for the device
96 * @num: number of resources
97 * @data: platform specific data for this platform device
98 * @size: size of platform specific data
100 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
102 static inline struct platform_device *platform_device_register_resndata(
103 struct device *parent, const char *name, int id,
104 const struct resource *res, unsigned int num,
105 const void *data, size_t size) {
107 struct platform_device_info pdevinfo = {
118 return platform_device_register_full(&pdevinfo);
122 * platform_device_register_simple - add a platform-level device and its resources
123 * @name: base name of the device we're adding
125 * @res: set of resources that needs to be allocated for the device
126 * @num: number of resources
128 * This function creates a simple platform device that requires minimal
129 * resource and memory management. Canned release function freeing memory
130 * allocated for the device allows drivers using such devices to be
131 * unloaded without waiting for the last reference to the device to be
134 * This interface is primarily intended for use with legacy drivers which
135 * probe hardware directly. Because such drivers create sysfs device nodes
136 * themselves, rather than letting system infrastructure handle such device
137 * enumeration tasks, they don't fully conform to the Linux driver model.
138 * In particular, when such drivers are built as modules, they can't be
141 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
143 static inline struct platform_device *platform_device_register_simple(
144 const char *name, int id,
145 const struct resource *res, unsigned int num)
147 return platform_device_register_resndata(NULL, name, id,
152 * platform_device_register_data - add a platform-level device with platform-specific data
153 * @parent: parent device for the device we're adding
154 * @name: base name of the device we're adding
156 * @data: platform specific data for this platform device
157 * @size: size of platform specific data
159 * This function creates a simple platform device that requires minimal
160 * resource and memory management. Canned release function freeing memory
161 * allocated for the device allows drivers using such devices to be
162 * unloaded without waiting for the last reference to the device to be
165 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
167 static inline struct platform_device *platform_device_register_data(
168 struct device *parent, const char *name, int id,
169 const void *data, size_t size)
171 return platform_device_register_resndata(parent, name, id,
172 NULL, 0, data, size);
175 extern struct platform_device *platform_device_alloc(const char *name, int id);
176 extern int platform_device_add_resources(struct platform_device *pdev,
177 const struct resource *res,
179 extern int platform_device_add_data(struct platform_device *pdev,
180 const void *data, size_t size);
181 extern int platform_device_add_properties(struct platform_device *pdev,
182 const struct property_entry *properties);
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);
187 struct platform_driver {
188 int (*probe)(struct platform_device *);
189 int (*remove)(struct platform_device *);
190 void (*shutdown)(struct platform_device *);
191 int (*suspend)(struct platform_device *, pm_message_t state);
192 int (*resume)(struct platform_device *);
193 struct device_driver driver;
194 const struct platform_device_id *id_table;
195 bool prevent_deferred_probe;
198 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
202 * use a macro to avoid include chaining to get THIS_MODULE
204 #define platform_driver_register(drv) \
205 __platform_driver_register(drv, THIS_MODULE)
206 extern int __platform_driver_register(struct platform_driver *,
208 extern void platform_driver_unregister(struct platform_driver *);
210 /* non-hotpluggable platform devices may use this so that probe() and
211 * its support may live in __init sections, conserving runtime memory.
213 #define platform_driver_probe(drv, probe) \
214 __platform_driver_probe(drv, probe, THIS_MODULE)
215 extern int __platform_driver_probe(struct platform_driver *driver,
216 int (*probe)(struct platform_device *), struct module *module);
218 static inline void *platform_get_drvdata(const struct platform_device *pdev)
220 return dev_get_drvdata(&pdev->dev);
223 static inline void platform_set_drvdata(struct platform_device *pdev,
226 dev_set_drvdata(&pdev->dev, data);
229 /* module_platform_driver() - Helper macro for drivers that don't do
230 * anything special in module init/exit. This eliminates a lot of
231 * boilerplate. Each module may only use this macro once, and
232 * calling it replaces module_init() and module_exit()
234 #define module_platform_driver(__platform_driver) \
235 module_driver(__platform_driver, platform_driver_register, \
236 platform_driver_unregister)
238 /* builtin_platform_driver() - Helper macro for builtin drivers that
239 * don't do anything special in driver init. This eliminates some
240 * boilerplate. Each driver may only use this macro once, and
241 * calling it replaces device_initcall(). Note this is meant to be
242 * a parallel of module_platform_driver() above, but w/o _exit stuff.
244 #define builtin_platform_driver(__platform_driver) \
245 builtin_driver(__platform_driver, platform_driver_register)
247 /* module_platform_driver_probe() - Helper macro for drivers that don't do
248 * anything special in module init/exit. This eliminates a lot of
249 * boilerplate. Each module may only use this macro once, and
250 * calling it replaces module_init() and module_exit()
252 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
253 static int __init __platform_driver##_init(void) \
255 return platform_driver_probe(&(__platform_driver), \
258 module_init(__platform_driver##_init); \
259 static void __exit __platform_driver##_exit(void) \
261 platform_driver_unregister(&(__platform_driver)); \
263 module_exit(__platform_driver##_exit);
265 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
266 * anything special in device init. This eliminates some boilerplate. Each
267 * driver may only use this macro once, and using it replaces device_initcall.
268 * This is meant to be a parallel of module_platform_driver_probe above, but
269 * without the __exit parts.
271 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
272 static int __init __platform_driver##_init(void) \
274 return platform_driver_probe(&(__platform_driver), \
277 device_initcall(__platform_driver##_init); \
279 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
280 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
281 extern struct platform_device *__platform_create_bundle(
282 struct platform_driver *driver, int (*probe)(struct platform_device *),
283 struct resource *res, unsigned int n_res,
284 const void *data, size_t size, struct module *module);
286 int __platform_register_drivers(struct platform_driver * const *drivers,
287 unsigned int count, struct module *owner);
288 void platform_unregister_drivers(struct platform_driver * const *drivers,
291 #define platform_register_drivers(drivers, count) \
292 __platform_register_drivers(drivers, count, THIS_MODULE)
294 /* early platform driver interface */
295 struct early_platform_driver {
296 const char *class_str;
297 struct platform_driver *pdrv;
298 struct list_head list;
304 #define EARLY_PLATFORM_ID_UNSET -2
305 #define EARLY_PLATFORM_ID_ERROR -3
307 extern int early_platform_driver_register(struct early_platform_driver *epdrv,
309 extern void early_platform_add_devices(struct platform_device **devs, int num);
311 static inline int is_early_platform_device(struct platform_device *pdev)
313 return !pdev->dev.driver;
316 extern void early_platform_driver_register_all(char *class_str);
317 extern int early_platform_driver_probe(char *class_str,
318 int nr_probe, int user_only);
319 extern void early_platform_cleanup(void);
321 #define early_platform_init(class_string, platdrv) \
322 early_platform_init_buffer(class_string, platdrv, NULL, 0)
325 #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
326 static __initdata struct early_platform_driver early_driver = { \
327 .class_str = class_string, \
331 .requested_id = EARLY_PLATFORM_ID_UNSET, \
333 static int __init early_platform_driver_setup_func(char *buffer) \
335 return early_platform_driver_register(&early_driver, buffer); \
337 early_param(class_string, early_platform_driver_setup_func)
339 #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
340 static inline char *early_platform_driver_setup_func(void) \
342 return bufsiz ? buf : NULL; \
346 #ifdef CONFIG_SUSPEND
347 extern int platform_pm_suspend(struct device *dev);
348 extern int platform_pm_resume(struct device *dev);
350 #define platform_pm_suspend NULL
351 #define platform_pm_resume NULL
354 #ifdef CONFIG_HIBERNATE_CALLBACKS
355 extern int platform_pm_freeze(struct device *dev);
356 extern int platform_pm_thaw(struct device *dev);
357 extern int platform_pm_poweroff(struct device *dev);
358 extern int platform_pm_restore(struct device *dev);
360 #define platform_pm_freeze NULL
361 #define platform_pm_thaw NULL
362 #define platform_pm_poweroff NULL
363 #define platform_pm_restore NULL
366 extern int platform_dma_configure(struct device *dev);
368 #ifdef CONFIG_PM_SLEEP
369 #define USE_PLATFORM_PM_SLEEP_OPS \
370 .suspend = platform_pm_suspend, \
371 .resume = platform_pm_resume, \
372 .freeze = platform_pm_freeze, \
373 .thaw = platform_pm_thaw, \
374 .poweroff = platform_pm_poweroff, \
375 .restore = platform_pm_restore,
377 #define USE_PLATFORM_PM_SLEEP_OPS
380 #endif /* _PLATFORM_DEVICE_H_ */