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)
20 struct property_entry;
21 struct platform_device_id;
23 struct platform_device {
28 u64 platform_dma_mask;
29 struct device_dma_parameters dma_parms;
31 struct resource *resource;
33 const struct platform_device_id *id_entry;
34 char *driver_override; /* Driver name to force a match */
36 /* MFD cell pointer */
37 struct mfd_cell *mfd_cell;
39 /* arch specific additions */
40 struct pdev_archdata archdata;
43 #define platform_get_device_id(pdev) ((pdev)->id_entry)
45 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
46 #define to_platform_device(x) container_of((x), struct platform_device, dev)
48 extern int platform_device_register(struct platform_device *);
49 extern void platform_device_unregister(struct platform_device *);
51 extern struct bus_type platform_bus_type;
52 extern struct device platform_bus;
54 extern struct resource *platform_get_resource(struct platform_device *,
55 unsigned int, unsigned int);
56 extern struct resource *platform_get_mem_or_io(struct platform_device *,
59 extern struct device *
60 platform_find_device_by_driver(struct device *start,
61 const struct device_driver *drv);
63 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
64 unsigned int index, struct resource **res);
66 devm_platform_ioremap_resource(struct platform_device *pdev,
69 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
71 extern int platform_get_irq(struct platform_device *, unsigned int);
72 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
73 extern int platform_irq_count(struct platform_device *);
74 extern int devm_platform_get_irqs_affinity(struct platform_device *dev,
75 struct irq_affinity *affd,
79 extern struct resource *platform_get_resource_byname(struct platform_device *,
82 extern int platform_get_irq_byname(struct platform_device *, const char *);
83 extern int platform_get_irq_byname_optional(struct platform_device *dev,
85 extern int platform_add_devices(struct platform_device **, int);
87 struct platform_device_info {
88 struct device *parent;
89 struct fwnode_handle *fwnode;
95 const struct resource *res;
102 const struct property_entry *properties;
104 extern struct platform_device *platform_device_register_full(
105 const struct platform_device_info *pdevinfo);
108 * platform_device_register_resndata - add a platform-level device with
109 * resources and platform-specific data
111 * @parent: parent device for the device we're adding
112 * @name: base name of the device we're adding
114 * @res: set of resources that needs to be allocated for the device
115 * @num: number of resources
116 * @data: platform specific data for this platform device
117 * @size: size of platform specific data
119 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
121 static inline struct platform_device *platform_device_register_resndata(
122 struct device *parent, const char *name, int id,
123 const struct resource *res, unsigned int num,
124 const void *data, size_t size) {
126 struct platform_device_info pdevinfo = {
137 return platform_device_register_full(&pdevinfo);
141 * platform_device_register_simple - add a platform-level device and its resources
142 * @name: base name of the device we're adding
144 * @res: set of resources that needs to be allocated for the device
145 * @num: number of resources
147 * This function creates a simple platform device that requires minimal
148 * resource and memory management. Canned release function freeing memory
149 * allocated for the device allows drivers using such devices to be
150 * unloaded without waiting for the last reference to the device to be
153 * This interface is primarily intended for use with legacy drivers which
154 * probe hardware directly. Because such drivers create sysfs device nodes
155 * themselves, rather than letting system infrastructure handle such device
156 * enumeration tasks, they don't fully conform to the Linux driver model.
157 * In particular, when such drivers are built as modules, they can't be
160 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
162 static inline struct platform_device *platform_device_register_simple(
163 const char *name, int id,
164 const struct resource *res, unsigned int num)
166 return platform_device_register_resndata(NULL, name, id,
171 * platform_device_register_data - add a platform-level device with platform-specific data
172 * @parent: parent device for the device we're adding
173 * @name: base name of the device we're adding
175 * @data: platform specific data for this platform device
176 * @size: size of platform specific data
178 * This function creates a simple platform device that requires minimal
179 * resource and memory management. Canned release function freeing memory
180 * allocated for the device allows drivers using such devices to be
181 * unloaded without waiting for the last reference to the device to be
184 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
186 static inline struct platform_device *platform_device_register_data(
187 struct device *parent, const char *name, int id,
188 const void *data, size_t size)
190 return platform_device_register_resndata(parent, name, id,
191 NULL, 0, data, size);
194 extern struct platform_device *platform_device_alloc(const char *name, int id);
195 extern int platform_device_add_resources(struct platform_device *pdev,
196 const struct resource *res,
198 extern int platform_device_add_data(struct platform_device *pdev,
199 const void *data, size_t size);
200 extern int platform_device_add(struct platform_device *pdev);
201 extern void platform_device_del(struct platform_device *pdev);
202 extern void platform_device_put(struct platform_device *pdev);
204 struct platform_driver {
205 int (*probe)(struct platform_device *);
208 * Traditionally the remove callback returned an int which however is
209 * ignored by the driver core. This led to wrong expectations by driver
210 * authors who thought returning an error code was a valid error
211 * handling strategy. To convert to a callback returning void, new
212 * drivers should implement .remove_new() until the conversion it done
213 * that eventually makes .remove() return void.
215 int (*remove)(struct platform_device *);
216 void (*remove_new)(struct platform_device *);
218 void (*shutdown)(struct platform_device *);
219 int (*suspend)(struct platform_device *, pm_message_t state);
220 int (*resume)(struct platform_device *);
221 struct device_driver driver;
222 const struct platform_device_id *id_table;
223 bool prevent_deferred_probe;
226 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
230 * use a macro to avoid include chaining to get THIS_MODULE
232 #define platform_driver_register(drv) \
233 __platform_driver_register(drv, THIS_MODULE)
234 extern int __platform_driver_register(struct platform_driver *,
236 extern void platform_driver_unregister(struct platform_driver *);
238 /* non-hotpluggable platform devices may use this so that probe() and
239 * its support may live in __init sections, conserving runtime memory.
241 #define platform_driver_probe(drv, probe) \
242 __platform_driver_probe(drv, probe, THIS_MODULE)
243 extern int __platform_driver_probe(struct platform_driver *driver,
244 int (*probe)(struct platform_device *), struct module *module);
246 static inline void *platform_get_drvdata(const struct platform_device *pdev)
248 return dev_get_drvdata(&pdev->dev);
251 static inline void platform_set_drvdata(struct platform_device *pdev,
254 dev_set_drvdata(&pdev->dev, data);
257 /* module_platform_driver() - Helper macro for drivers that don't do
258 * anything special in module init/exit. This eliminates a lot of
259 * boilerplate. Each module may only use this macro once, and
260 * calling it replaces module_init() and module_exit()
262 #define module_platform_driver(__platform_driver) \
263 module_driver(__platform_driver, platform_driver_register, \
264 platform_driver_unregister)
266 /* builtin_platform_driver() - Helper macro for builtin drivers that
267 * don't do anything special in driver init. This eliminates some
268 * boilerplate. Each driver may only use this macro once, and
269 * calling it replaces device_initcall(). Note this is meant to be
270 * a parallel of module_platform_driver() above, but w/o _exit stuff.
272 #define builtin_platform_driver(__platform_driver) \
273 builtin_driver(__platform_driver, platform_driver_register)
275 /* module_platform_driver_probe() - Helper macro for drivers that don't do
276 * anything special in module init/exit. This eliminates a lot of
277 * boilerplate. Each module may only use this macro once, and
278 * calling it replaces module_init() and module_exit()
280 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
281 static int __init __platform_driver##_init(void) \
283 return platform_driver_probe(&(__platform_driver), \
286 module_init(__platform_driver##_init); \
287 static void __exit __platform_driver##_exit(void) \
289 platform_driver_unregister(&(__platform_driver)); \
291 module_exit(__platform_driver##_exit);
293 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
294 * anything special in device init. This eliminates some boilerplate. Each
295 * driver may only use this macro once, and using it replaces device_initcall.
296 * This is meant to be a parallel of module_platform_driver_probe above, but
297 * without the __exit parts.
299 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
300 static int __init __platform_driver##_init(void) \
302 return platform_driver_probe(&(__platform_driver), \
305 device_initcall(__platform_driver##_init); \
307 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
308 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
309 extern struct platform_device *__platform_create_bundle(
310 struct platform_driver *driver, int (*probe)(struct platform_device *),
311 struct resource *res, unsigned int n_res,
312 const void *data, size_t size, struct module *module);
314 int __platform_register_drivers(struct platform_driver * const *drivers,
315 unsigned int count, struct module *owner);
316 void platform_unregister_drivers(struct platform_driver * const *drivers,
319 #define platform_register_drivers(drivers, count) \
320 __platform_register_drivers(drivers, count, THIS_MODULE)
322 #ifdef CONFIG_SUSPEND
323 extern int platform_pm_suspend(struct device *dev);
324 extern int platform_pm_resume(struct device *dev);
326 #define platform_pm_suspend NULL
327 #define platform_pm_resume NULL
330 #ifdef CONFIG_HIBERNATE_CALLBACKS
331 extern int platform_pm_freeze(struct device *dev);
332 extern int platform_pm_thaw(struct device *dev);
333 extern int platform_pm_poweroff(struct device *dev);
334 extern int platform_pm_restore(struct device *dev);
336 #define platform_pm_freeze NULL
337 #define platform_pm_thaw NULL
338 #define platform_pm_poweroff NULL
339 #define platform_pm_restore NULL
342 extern int platform_dma_configure(struct device *dev);
344 #ifdef CONFIG_PM_SLEEP
345 #define USE_PLATFORM_PM_SLEEP_OPS \
346 .suspend = platform_pm_suspend, \
347 .resume = platform_pm_resume, \
348 .freeze = platform_pm_freeze, \
349 .thaw = platform_pm_thaw, \
350 .poweroff = platform_pm_poweroff, \
351 .restore = platform_pm_restore,
353 #define USE_PLATFORM_PM_SLEEP_OPS
356 #ifndef CONFIG_SUPERH
358 * REVISIT: This stub is needed for all non-SuperH users of early platform
359 * drivers. It should go away once we introduce the new platform_device-based
360 * early driver framework.
362 static inline int is_sh_early_platform_device(struct platform_device *pdev)
366 #endif /* CONFIG_SUPERH */
368 /* For now only SuperH uses it */
369 void early_platform_cleanup(void);
371 #endif /* _PLATFORM_DEVICE_H_ */