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
{
27 u64 platform_dma_mask
;
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_get_and_ioremap_resource(struct platform_device
*pdev
,
59 unsigned int index
, struct resource
**res
);
61 devm_platform_ioremap_resource(struct platform_device
*pdev
,
64 devm_platform_ioremap_resource_wc(struct platform_device
*pdev
,
67 devm_platform_ioremap_resource_byname(struct platform_device
*pdev
,
69 extern int platform_get_irq(struct platform_device
*, unsigned int);
70 extern int platform_get_irq_optional(struct platform_device
*, unsigned int);
71 extern int platform_irq_count(struct platform_device
*);
72 extern struct resource
*platform_get_resource_byname(struct platform_device
*,
75 extern int platform_get_irq_byname(struct platform_device
*, const char *);
76 extern int platform_get_irq_byname_optional(struct platform_device
*dev
,
78 extern int platform_add_devices(struct platform_device
**, int);
80 struct platform_device_info
{
81 struct device
*parent
;
82 struct fwnode_handle
*fwnode
;
88 const struct resource
*res
;
95 const struct property_entry
*properties
;
97 extern struct platform_device
*platform_device_register_full(
98 const struct platform_device_info
*pdevinfo
);
101 * platform_device_register_resndata - add a platform-level device with
102 * resources and platform-specific data
104 * @parent: parent device for the device we're adding
105 * @name: base name of the device we're adding
107 * @res: set of resources that needs to be allocated for the device
108 * @num: number of resources
109 * @data: platform specific data for this platform device
110 * @size: size of platform specific data
112 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
114 static inline struct platform_device
*platform_device_register_resndata(
115 struct device
*parent
, const char *name
, int id
,
116 const struct resource
*res
, unsigned int num
,
117 const void *data
, size_t size
) {
119 struct platform_device_info pdevinfo
= {
130 return platform_device_register_full(&pdevinfo
);
134 * platform_device_register_simple - add a platform-level device and its resources
135 * @name: base name of the device we're adding
137 * @res: set of resources that needs to be allocated for the device
138 * @num: number of resources
140 * This function creates a simple platform device that requires minimal
141 * resource and memory management. Canned release function freeing memory
142 * allocated for the device allows drivers using such devices to be
143 * unloaded without waiting for the last reference to the device to be
146 * This interface is primarily intended for use with legacy drivers which
147 * probe hardware directly. Because such drivers create sysfs device nodes
148 * themselves, rather than letting system infrastructure handle such device
149 * enumeration tasks, they don't fully conform to the Linux driver model.
150 * In particular, when such drivers are built as modules, they can't be
153 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
155 static inline struct platform_device
*platform_device_register_simple(
156 const char *name
, int id
,
157 const struct resource
*res
, unsigned int num
)
159 return platform_device_register_resndata(NULL
, name
, id
,
164 * platform_device_register_data - add a platform-level device with platform-specific data
165 * @parent: parent device for the device we're adding
166 * @name: base name of the device we're adding
168 * @data: platform specific data for this platform device
169 * @size: size of platform specific data
171 * This function creates a simple platform device that requires minimal
172 * resource and memory management. Canned release function freeing memory
173 * allocated for the device allows drivers using such devices to be
174 * unloaded without waiting for the last reference to the device to be
177 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
179 static inline struct platform_device
*platform_device_register_data(
180 struct device
*parent
, const char *name
, int id
,
181 const void *data
, size_t size
)
183 return platform_device_register_resndata(parent
, name
, id
,
184 NULL
, 0, data
, size
);
187 extern struct platform_device
*platform_device_alloc(const char *name
, int id
);
188 extern int platform_device_add_resources(struct platform_device
*pdev
,
189 const struct resource
*res
,
191 extern int platform_device_add_data(struct platform_device
*pdev
,
192 const void *data
, size_t size
);
193 extern int platform_device_add_properties(struct platform_device
*pdev
,
194 const struct property_entry
*properties
);
195 extern int platform_device_add(struct platform_device
*pdev
);
196 extern void platform_device_del(struct platform_device
*pdev
);
197 extern void platform_device_put(struct platform_device
*pdev
);
199 struct platform_driver
{
200 int (*probe
)(struct platform_device
*);
201 int (*remove
)(struct platform_device
*);
202 void (*shutdown
)(struct platform_device
*);
203 int (*suspend
)(struct platform_device
*, pm_message_t state
);
204 int (*resume
)(struct platform_device
*);
205 struct device_driver driver
;
206 const struct platform_device_id
*id_table
;
207 bool prevent_deferred_probe
;
210 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
214 * use a macro to avoid include chaining to get THIS_MODULE
216 #define platform_driver_register(drv) \
217 __platform_driver_register(drv, THIS_MODULE)
218 extern int __platform_driver_register(struct platform_driver
*,
220 extern void platform_driver_unregister(struct platform_driver
*);
222 /* non-hotpluggable platform devices may use this so that probe() and
223 * its support may live in __init sections, conserving runtime memory.
225 #define platform_driver_probe(drv, probe) \
226 __platform_driver_probe(drv, probe, THIS_MODULE)
227 extern int __platform_driver_probe(struct platform_driver
*driver
,
228 int (*probe
)(struct platform_device
*), struct module
*module
);
230 static inline void *platform_get_drvdata(const struct platform_device
*pdev
)
232 return dev_get_drvdata(&pdev
->dev
);
235 static inline void platform_set_drvdata(struct platform_device
*pdev
,
238 dev_set_drvdata(&pdev
->dev
, data
);
241 /* module_platform_driver() - Helper macro for drivers that don't do
242 * anything special in module init/exit. This eliminates a lot of
243 * boilerplate. Each module may only use this macro once, and
244 * calling it replaces module_init() and module_exit()
246 #define module_platform_driver(__platform_driver) \
247 module_driver(__platform_driver, platform_driver_register, \
248 platform_driver_unregister)
250 /* builtin_platform_driver() - Helper macro for builtin drivers that
251 * don't do anything special in driver init. This eliminates some
252 * boilerplate. Each driver may only use this macro once, and
253 * calling it replaces device_initcall(). Note this is meant to be
254 * a parallel of module_platform_driver() above, but w/o _exit stuff.
256 #define builtin_platform_driver(__platform_driver) \
257 builtin_driver(__platform_driver, platform_driver_register)
259 /* module_platform_driver_probe() - Helper macro for drivers that don't do
260 * anything special in module init/exit. This eliminates a lot of
261 * boilerplate. Each module may only use this macro once, and
262 * calling it replaces module_init() and module_exit()
264 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
265 static int __init __platform_driver##_init(void) \
267 return platform_driver_probe(&(__platform_driver), \
270 module_init(__platform_driver##_init); \
271 static void __exit __platform_driver##_exit(void) \
273 platform_driver_unregister(&(__platform_driver)); \
275 module_exit(__platform_driver##_exit);
277 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
278 * anything special in device init. This eliminates some boilerplate. Each
279 * driver may only use this macro once, and using it replaces device_initcall.
280 * This is meant to be a parallel of module_platform_driver_probe above, but
281 * without the __exit parts.
283 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
284 static int __init __platform_driver##_init(void) \
286 return platform_driver_probe(&(__platform_driver), \
289 device_initcall(__platform_driver##_init); \
291 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
292 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
293 extern struct platform_device
*__platform_create_bundle(
294 struct platform_driver
*driver
, int (*probe
)(struct platform_device
*),
295 struct resource
*res
, unsigned int n_res
,
296 const void *data
, size_t size
, struct module
*module
);
298 int __platform_register_drivers(struct platform_driver
* const *drivers
,
299 unsigned int count
, struct module
*owner
);
300 void platform_unregister_drivers(struct platform_driver
* const *drivers
,
303 #define platform_register_drivers(drivers, count) \
304 __platform_register_drivers(drivers, count, THIS_MODULE)
306 #ifdef CONFIG_SUSPEND
307 extern int platform_pm_suspend(struct device
*dev
);
308 extern int platform_pm_resume(struct device
*dev
);
310 #define platform_pm_suspend NULL
311 #define platform_pm_resume NULL
314 #ifdef CONFIG_HIBERNATE_CALLBACKS
315 extern int platform_pm_freeze(struct device
*dev
);
316 extern int platform_pm_thaw(struct device
*dev
);
317 extern int platform_pm_poweroff(struct device
*dev
);
318 extern int platform_pm_restore(struct device
*dev
);
320 #define platform_pm_freeze NULL
321 #define platform_pm_thaw NULL
322 #define platform_pm_poweroff NULL
323 #define platform_pm_restore NULL
326 extern int platform_dma_configure(struct device
*dev
);
328 #ifdef CONFIG_PM_SLEEP
329 #define USE_PLATFORM_PM_SLEEP_OPS \
330 .suspend = platform_pm_suspend, \
331 .resume = platform_pm_resume, \
332 .freeze = platform_pm_freeze, \
333 .thaw = platform_pm_thaw, \
334 .poweroff = platform_pm_poweroff, \
335 .restore = platform_pm_restore,
337 #define USE_PLATFORM_PM_SLEEP_OPS
340 #ifndef CONFIG_SUPERH
342 * REVISIT: This stub is needed for all non-SuperH users of early platform
343 * drivers. It should go away once we introduce the new platform_device-based
344 * early driver framework.
346 static inline int is_sh_early_platform_device(struct platform_device
*pdev
)
350 #endif /* CONFIG_SUPERH */
352 #endif /* _PLATFORM_DEVICE_H_ */