GNU Linux-libre 5.10.217-gnu1
[releases.git] / include / linux / platform_device.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * platform_device.h - generic, centralized driver model
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6  *
7  * See Documentation/driver-api/driver-model/ for more information.
8  */
9
10 #ifndef _PLATFORM_DEVICE_H_
11 #define _PLATFORM_DEVICE_H_
12
13 #include <linux/device.h>
14
15 #define PLATFORM_DEVID_NONE     (-1)
16 #define PLATFORM_DEVID_AUTO     (-2)
17
18 struct mfd_cell;
19 struct property_entry;
20 struct platform_device_id;
21
22 struct platform_device {
23         const char      *name;
24         int             id;
25         bool            id_auto;
26         struct device   dev;
27         u64             platform_dma_mask;
28         struct device_dma_parameters dma_parms;
29         u32             num_resources;
30         struct resource *resource;
31
32         const struct platform_device_id *id_entry;
33         /*
34          * Driver name to force a match.  Do not set directly, because core
35          * frees it.  Use driver_set_override() to set or clear it.
36          */
37         const char *driver_override;
38
39         /* MFD cell pointer */
40         struct mfd_cell *mfd_cell;
41
42         /* arch specific additions */
43         struct pdev_archdata    archdata;
44 };
45
46 #define platform_get_device_id(pdev)    ((pdev)->id_entry)
47
48 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
49 #define to_platform_device(x) container_of((x), struct platform_device, dev)
50
51 extern int platform_device_register(struct platform_device *);
52 extern void platform_device_unregister(struct platform_device *);
53
54 extern struct bus_type platform_bus_type;
55 extern struct device platform_bus;
56
57 extern struct resource *platform_get_resource(struct platform_device *,
58                                               unsigned int, unsigned int);
59 extern struct device *
60 platform_find_device_by_driver(struct device *start,
61                                const struct device_driver *drv);
62 extern void __iomem *
63 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
64                                 unsigned int index, struct resource **res);
65 extern void __iomem *
66 devm_platform_ioremap_resource(struct platform_device *pdev,
67                                unsigned int index);
68 extern void __iomem *
69 devm_platform_ioremap_resource_wc(struct platform_device *pdev,
70                                   unsigned int index);
71 extern void __iomem *
72 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
73                                       const char *name);
74 extern int platform_get_irq(struct platform_device *, unsigned int);
75 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
76 extern int platform_irq_count(struct platform_device *);
77 extern struct resource *platform_get_resource_byname(struct platform_device *,
78                                                      unsigned int,
79                                                      const char *);
80 extern int platform_get_irq_byname(struct platform_device *, const char *);
81 extern int platform_get_irq_byname_optional(struct platform_device *dev,
82                                             const char *name);
83 extern int platform_add_devices(struct platform_device **, int);
84
85 struct platform_device_info {
86                 struct device *parent;
87                 struct fwnode_handle *fwnode;
88                 bool of_node_reused;
89
90                 const char *name;
91                 int id;
92
93                 const struct resource *res;
94                 unsigned int num_res;
95
96                 const void *data;
97                 size_t size_data;
98                 u64 dma_mask;
99
100                 const struct property_entry *properties;
101 };
102 extern struct platform_device *platform_device_register_full(
103                 const struct platform_device_info *pdevinfo);
104
105 /**
106  * platform_device_register_resndata - add a platform-level device with
107  * resources and platform-specific data
108  *
109  * @parent: parent device for the device we're adding
110  * @name: base name of the device we're adding
111  * @id: instance id
112  * @res: set of resources that needs to be allocated for the device
113  * @num: number of resources
114  * @data: platform specific data for this platform device
115  * @size: size of platform specific data
116  *
117  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
118  */
119 static inline struct platform_device *platform_device_register_resndata(
120                 struct device *parent, const char *name, int id,
121                 const struct resource *res, unsigned int num,
122                 const void *data, size_t size) {
123
124         struct platform_device_info pdevinfo = {
125                 .parent = parent,
126                 .name = name,
127                 .id = id,
128                 .res = res,
129                 .num_res = num,
130                 .data = data,
131                 .size_data = size,
132                 .dma_mask = 0,
133         };
134
135         return platform_device_register_full(&pdevinfo);
136 }
137
138 /**
139  * platform_device_register_simple - add a platform-level device and its resources
140  * @name: base name of the device we're adding
141  * @id: instance id
142  * @res: set of resources that needs to be allocated for the device
143  * @num: number of resources
144  *
145  * This function creates a simple platform device that requires minimal
146  * resource and memory management. Canned release function freeing memory
147  * allocated for the device allows drivers using such devices to be
148  * unloaded without waiting for the last reference to the device to be
149  * dropped.
150  *
151  * This interface is primarily intended for use with legacy drivers which
152  * probe hardware directly.  Because such drivers create sysfs device nodes
153  * themselves, rather than letting system infrastructure handle such device
154  * enumeration tasks, they don't fully conform to the Linux driver model.
155  * In particular, when such drivers are built as modules, they can't be
156  * "hotplugged".
157  *
158  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
159  */
160 static inline struct platform_device *platform_device_register_simple(
161                 const char *name, int id,
162                 const struct resource *res, unsigned int num)
163 {
164         return platform_device_register_resndata(NULL, name, id,
165                         res, num, NULL, 0);
166 }
167
168 /**
169  * platform_device_register_data - add a platform-level device with platform-specific data
170  * @parent: parent device for the device we're adding
171  * @name: base name of the device we're adding
172  * @id: instance id
173  * @data: platform specific data for this platform device
174  * @size: size of platform specific data
175  *
176  * This function creates a simple platform device that requires minimal
177  * resource and memory management. Canned release function freeing memory
178  * allocated for the device allows drivers using such devices to be
179  * unloaded without waiting for the last reference to the device to be
180  * dropped.
181  *
182  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
183  */
184 static inline struct platform_device *platform_device_register_data(
185                 struct device *parent, const char *name, int id,
186                 const void *data, size_t size)
187 {
188         return platform_device_register_resndata(parent, name, id,
189                         NULL, 0, data, size);
190 }
191
192 extern struct platform_device *platform_device_alloc(const char *name, int id);
193 extern int platform_device_add_resources(struct platform_device *pdev,
194                                          const struct resource *res,
195                                          unsigned int num);
196 extern int platform_device_add_data(struct platform_device *pdev,
197                                     const void *data, size_t size);
198 extern int platform_device_add_properties(struct platform_device *pdev,
199                                 const struct property_entry *properties);
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);
203
204 struct platform_driver {
205         int (*probe)(struct platform_device *);
206         int (*remove)(struct platform_device *);
207         void (*shutdown)(struct platform_device *);
208         int (*suspend)(struct platform_device *, pm_message_t state);
209         int (*resume)(struct platform_device *);
210         struct device_driver driver;
211         const struct platform_device_id *id_table;
212         bool prevent_deferred_probe;
213 };
214
215 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
216                                  driver))
217
218 /*
219  * use a macro to avoid include chaining to get THIS_MODULE
220  */
221 #define platform_driver_register(drv) \
222         __platform_driver_register(drv, THIS_MODULE)
223 extern int __platform_driver_register(struct platform_driver *,
224                                         struct module *);
225 extern void platform_driver_unregister(struct platform_driver *);
226
227 /* non-hotpluggable platform devices may use this so that probe() and
228  * its support may live in __init sections, conserving runtime memory.
229  */
230 #define platform_driver_probe(drv, probe) \
231         __platform_driver_probe(drv, probe, THIS_MODULE)
232 extern int __platform_driver_probe(struct platform_driver *driver,
233                 int (*probe)(struct platform_device *), struct module *module);
234
235 static inline void *platform_get_drvdata(const struct platform_device *pdev)
236 {
237         return dev_get_drvdata(&pdev->dev);
238 }
239
240 static inline void platform_set_drvdata(struct platform_device *pdev,
241                                         void *data)
242 {
243         dev_set_drvdata(&pdev->dev, data);
244 }
245
246 /* module_platform_driver() - Helper macro for drivers that don't do
247  * anything special in module init/exit.  This eliminates a lot of
248  * boilerplate.  Each module may only use this macro once, and
249  * calling it replaces module_init() and module_exit()
250  */
251 #define module_platform_driver(__platform_driver) \
252         module_driver(__platform_driver, platform_driver_register, \
253                         platform_driver_unregister)
254
255 /* builtin_platform_driver() - Helper macro for builtin drivers that
256  * don't do anything special in driver init.  This eliminates some
257  * boilerplate.  Each driver may only use this macro once, and
258  * calling it replaces device_initcall().  Note this is meant to be
259  * a parallel of module_platform_driver() above, but w/o _exit stuff.
260  */
261 #define builtin_platform_driver(__platform_driver) \
262         builtin_driver(__platform_driver, platform_driver_register)
263
264 /* module_platform_driver_probe() - Helper macro for drivers that don't do
265  * anything special in module init/exit.  This eliminates a lot of
266  * boilerplate.  Each module may only use this macro once, and
267  * calling it replaces module_init() and module_exit()
268  */
269 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
270 static int __init __platform_driver##_init(void) \
271 { \
272         return platform_driver_probe(&(__platform_driver), \
273                                      __platform_probe);    \
274 } \
275 module_init(__platform_driver##_init); \
276 static void __exit __platform_driver##_exit(void) \
277 { \
278         platform_driver_unregister(&(__platform_driver)); \
279 } \
280 module_exit(__platform_driver##_exit);
281
282 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
283  * anything special in device init.  This eliminates some boilerplate.  Each
284  * driver may only use this macro once, and using it replaces device_initcall.
285  * This is meant to be a parallel of module_platform_driver_probe above, but
286  * without the __exit parts.
287  */
288 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
289 static int __init __platform_driver##_init(void) \
290 { \
291         return platform_driver_probe(&(__platform_driver), \
292                                      __platform_probe);    \
293 } \
294 device_initcall(__platform_driver##_init); \
295
296 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
297         __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
298 extern struct platform_device *__platform_create_bundle(
299         struct platform_driver *driver, int (*probe)(struct platform_device *),
300         struct resource *res, unsigned int n_res,
301         const void *data, size_t size, struct module *module);
302
303 int __platform_register_drivers(struct platform_driver * const *drivers,
304                                 unsigned int count, struct module *owner);
305 void platform_unregister_drivers(struct platform_driver * const *drivers,
306                                  unsigned int count);
307
308 #define platform_register_drivers(drivers, count) \
309         __platform_register_drivers(drivers, count, THIS_MODULE)
310
311 #ifdef CONFIG_SUSPEND
312 extern int platform_pm_suspend(struct device *dev);
313 extern int platform_pm_resume(struct device *dev);
314 #else
315 #define platform_pm_suspend             NULL
316 #define platform_pm_resume              NULL
317 #endif
318
319 #ifdef CONFIG_HIBERNATE_CALLBACKS
320 extern int platform_pm_freeze(struct device *dev);
321 extern int platform_pm_thaw(struct device *dev);
322 extern int platform_pm_poweroff(struct device *dev);
323 extern int platform_pm_restore(struct device *dev);
324 #else
325 #define platform_pm_freeze              NULL
326 #define platform_pm_thaw                NULL
327 #define platform_pm_poweroff            NULL
328 #define platform_pm_restore             NULL
329 #endif
330
331 extern int platform_dma_configure(struct device *dev);
332
333 #ifdef CONFIG_PM_SLEEP
334 #define USE_PLATFORM_PM_SLEEP_OPS \
335         .suspend = platform_pm_suspend, \
336         .resume = platform_pm_resume, \
337         .freeze = platform_pm_freeze, \
338         .thaw = platform_pm_thaw, \
339         .poweroff = platform_pm_poweroff, \
340         .restore = platform_pm_restore,
341 #else
342 #define USE_PLATFORM_PM_SLEEP_OPS
343 #endif
344
345 #ifndef CONFIG_SUPERH
346 /*
347  * REVISIT: This stub is needed for all non-SuperH users of early platform
348  * drivers. It should go away once we introduce the new platform_device-based
349  * early driver framework.
350  */
351 static inline int is_sh_early_platform_device(struct platform_device *pdev)
352 {
353         return 0;
354 }
355 #endif /* CONFIG_SUPERH */
356
357 /* For now only SuperH uses it */
358 void early_platform_cleanup(void);
359
360 #endif /* _PLATFORM_DEVICE_H_ */