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-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);
55 devm_platform_ioremap_resource(struct platform_device
*pdev
,
57 extern int platform_get_irq(struct platform_device
*, unsigned int);
58 extern int platform_irq_count(struct platform_device
*);
59 extern struct resource
*platform_get_resource_byname(struct platform_device
*,
62 extern int platform_get_irq_byname(struct platform_device
*, const char *);
63 extern int platform_add_devices(struct platform_device
**, int);
65 struct platform_device_info
{
66 struct device
*parent
;
67 struct fwnode_handle
*fwnode
;
73 const struct resource
*res
;
80 struct property_entry
*properties
;
82 extern struct platform_device
*platform_device_register_full(
83 const struct platform_device_info
*pdevinfo
);
86 * platform_device_register_resndata - add a platform-level device with
87 * resources and platform-specific data
89 * @parent: parent device for the device we're adding
90 * @name: base name of the device we're adding
92 * @res: set of resources that needs to be allocated for the device
93 * @num: number of resources
94 * @data: platform specific data for this platform device
95 * @size: size of platform specific data
97 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
99 static inline struct platform_device
*platform_device_register_resndata(
100 struct device
*parent
, const char *name
, int id
,
101 const struct resource
*res
, unsigned int num
,
102 const void *data
, size_t size
) {
104 struct platform_device_info pdevinfo
= {
115 return platform_device_register_full(&pdevinfo
);
119 * platform_device_register_simple - add a platform-level device and its resources
120 * @name: base name of the device we're adding
122 * @res: set of resources that needs to be allocated for the device
123 * @num: number of resources
125 * This function creates a simple platform device that requires minimal
126 * resource and memory management. Canned release function freeing memory
127 * allocated for the device allows drivers using such devices to be
128 * unloaded without waiting for the last reference to the device to be
131 * This interface is primarily intended for use with legacy drivers which
132 * probe hardware directly. Because such drivers create sysfs device nodes
133 * themselves, rather than letting system infrastructure handle such device
134 * enumeration tasks, they don't fully conform to the Linux driver model.
135 * In particular, when such drivers are built as modules, they can't be
138 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
140 static inline struct platform_device
*platform_device_register_simple(
141 const char *name
, int id
,
142 const struct resource
*res
, unsigned int num
)
144 return platform_device_register_resndata(NULL
, name
, id
,
149 * platform_device_register_data - add a platform-level device with platform-specific data
150 * @parent: parent device for the device we're adding
151 * @name: base name of the device we're adding
153 * @data: platform specific data for this platform device
154 * @size: size of platform specific data
156 * This function creates a simple platform device that requires minimal
157 * resource and memory management. Canned release function freeing memory
158 * allocated for the device allows drivers using such devices to be
159 * unloaded without waiting for the last reference to the device to be
162 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
164 static inline struct platform_device
*platform_device_register_data(
165 struct device
*parent
, const char *name
, int id
,
166 const void *data
, size_t size
)
168 return platform_device_register_resndata(parent
, name
, id
,
169 NULL
, 0, data
, size
);
172 extern struct platform_device
*platform_device_alloc(const char *name
, int id
);
173 extern int platform_device_add_resources(struct platform_device
*pdev
,
174 const struct resource
*res
,
176 extern int platform_device_add_data(struct platform_device
*pdev
,
177 const void *data
, size_t size
);
178 extern int platform_device_add_properties(struct platform_device
*pdev
,
179 const struct property_entry
*properties
);
180 extern int platform_device_add(struct platform_device
*pdev
);
181 extern void platform_device_del(struct platform_device
*pdev
);
182 extern void platform_device_put(struct platform_device
*pdev
);
184 struct platform_driver
{
185 int (*probe
)(struct platform_device
*);
186 int (*remove
)(struct platform_device
*);
187 void (*shutdown
)(struct platform_device
*);
188 int (*suspend
)(struct platform_device
*, pm_message_t state
);
189 int (*resume
)(struct platform_device
*);
190 struct device_driver driver
;
191 const struct platform_device_id
*id_table
;
192 bool prevent_deferred_probe
;
195 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
199 * use a macro to avoid include chaining to get THIS_MODULE
201 #define platform_driver_register(drv) \
202 __platform_driver_register(drv, THIS_MODULE)
203 extern int __platform_driver_register(struct platform_driver
*,
205 extern void platform_driver_unregister(struct platform_driver
*);
207 /* non-hotpluggable platform devices may use this so that probe() and
208 * its support may live in __init sections, conserving runtime memory.
210 #define platform_driver_probe(drv, probe) \
211 __platform_driver_probe(drv, probe, THIS_MODULE)
212 extern int __platform_driver_probe(struct platform_driver
*driver
,
213 int (*probe
)(struct platform_device
*), struct module
*module
);
215 static inline void *platform_get_drvdata(const struct platform_device
*pdev
)
217 return dev_get_drvdata(&pdev
->dev
);
220 static inline void platform_set_drvdata(struct platform_device
*pdev
,
223 dev_set_drvdata(&pdev
->dev
, data
);
226 /* module_platform_driver() - Helper macro for drivers that don't do
227 * anything special in module init/exit. This eliminates a lot of
228 * boilerplate. Each module may only use this macro once, and
229 * calling it replaces module_init() and module_exit()
231 #define module_platform_driver(__platform_driver) \
232 module_driver(__platform_driver, platform_driver_register, \
233 platform_driver_unregister)
235 /* builtin_platform_driver() - Helper macro for builtin drivers that
236 * don't do anything special in driver init. This eliminates some
237 * boilerplate. Each driver may only use this macro once, and
238 * calling it replaces device_initcall(). Note this is meant to be
239 * a parallel of module_platform_driver() above, but w/o _exit stuff.
241 #define builtin_platform_driver(__platform_driver) \
242 builtin_driver(__platform_driver, platform_driver_register)
244 /* module_platform_driver_probe() - Helper macro for drivers that don't do
245 * anything special in module init/exit. This eliminates a lot of
246 * boilerplate. Each module may only use this macro once, and
247 * calling it replaces module_init() and module_exit()
249 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
250 static int __init __platform_driver##_init(void) \
252 return platform_driver_probe(&(__platform_driver), \
255 module_init(__platform_driver##_init); \
256 static void __exit __platform_driver##_exit(void) \
258 platform_driver_unregister(&(__platform_driver)); \
260 module_exit(__platform_driver##_exit);
262 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
263 * anything special in device init. This eliminates some boilerplate. Each
264 * driver may only use this macro once, and using it replaces device_initcall.
265 * This is meant to be a parallel of module_platform_driver_probe above, but
266 * without the __exit parts.
268 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
269 static int __init __platform_driver##_init(void) \
271 return platform_driver_probe(&(__platform_driver), \
274 device_initcall(__platform_driver##_init); \
276 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
277 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
278 extern struct platform_device
*__platform_create_bundle(
279 struct platform_driver
*driver
, int (*probe
)(struct platform_device
*),
280 struct resource
*res
, unsigned int n_res
,
281 const void *data
, size_t size
, struct module
*module
);
283 int __platform_register_drivers(struct platform_driver
* const *drivers
,
284 unsigned int count
, struct module
*owner
);
285 void platform_unregister_drivers(struct platform_driver
* const *drivers
,
288 #define platform_register_drivers(drivers, count) \
289 __platform_register_drivers(drivers, count, THIS_MODULE)
291 /* early platform driver interface */
292 struct early_platform_driver
{
293 const char *class_str
;
294 struct platform_driver
*pdrv
;
295 struct list_head list
;
301 #define EARLY_PLATFORM_ID_UNSET -2
302 #define EARLY_PLATFORM_ID_ERROR -3
304 extern int early_platform_driver_register(struct early_platform_driver
*epdrv
,
306 extern void early_platform_add_devices(struct platform_device
**devs
, int num
);
308 static inline int is_early_platform_device(struct platform_device
*pdev
)
310 return !pdev
->dev
.driver
;
313 extern void early_platform_driver_register_all(char *class_str
);
314 extern int early_platform_driver_probe(char *class_str
,
315 int nr_probe
, int user_only
);
316 extern void early_platform_cleanup(void);
318 #define early_platform_init(class_string, platdrv) \
319 early_platform_init_buffer(class_string, platdrv, NULL, 0)
322 #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
323 static __initdata struct early_platform_driver early_driver = { \
324 .class_str = class_string, \
328 .requested_id = EARLY_PLATFORM_ID_UNSET, \
330 static int __init early_platform_driver_setup_func(char *buffer) \
332 return early_platform_driver_register(&early_driver, buffer); \
334 early_param(class_string, early_platform_driver_setup_func)
336 #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
337 static inline char *early_platform_driver_setup_func(void) \
339 return bufsiz ? buf : NULL; \
343 #ifdef CONFIG_SUSPEND
344 extern int platform_pm_suspend(struct device
*dev
);
345 extern int platform_pm_resume(struct device
*dev
);
347 #define platform_pm_suspend NULL
348 #define platform_pm_resume NULL
351 #ifdef CONFIG_HIBERNATE_CALLBACKS
352 extern int platform_pm_freeze(struct device
*dev
);
353 extern int platform_pm_thaw(struct device
*dev
);
354 extern int platform_pm_poweroff(struct device
*dev
);
355 extern int platform_pm_restore(struct device
*dev
);
357 #define platform_pm_freeze NULL
358 #define platform_pm_thaw NULL
359 #define platform_pm_poweroff NULL
360 #define platform_pm_restore NULL
363 extern int platform_dma_configure(struct device
*dev
);
365 #ifdef CONFIG_PM_SLEEP
366 #define USE_PLATFORM_PM_SLEEP_OPS \
367 .suspend = platform_pm_suspend, \
368 .resume = platform_pm_resume, \
369 .freeze = platform_pm_freeze, \
370 .thaw = platform_pm_thaw, \
371 .poweroff = platform_pm_poweroff, \
372 .restore = platform_pm_restore,
374 #define USE_PLATFORM_PM_SLEEP_OPS
377 #endif /* _PLATFORM_DEVICE_H_ */