]> Git Repo - J-linux.git/blob - include/linux/platform_device.h
platform: Add platform_find_device_by_driver() helper
[J-linux.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 <[email protected]>
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         u32             num_resources;
28         struct resource *resource;
29
30         const struct platform_device_id *id_entry;
31         char *driver_override; /* Driver name to force a match */
32
33         /* MFD cell pointer */
34         struct mfd_cell *mfd_cell;
35
36         /* arch specific additions */
37         struct pdev_archdata    archdata;
38 };
39
40 #define platform_get_device_id(pdev)    ((pdev)->id_entry)
41
42 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
43 #define to_platform_device(x) container_of((x), struct platform_device, dev)
44
45 extern int platform_device_register(struct platform_device *);
46 extern void platform_device_unregister(struct platform_device *);
47
48 extern struct bus_type platform_bus_type;
49 extern struct device platform_bus;
50
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);
57 extern void __iomem *
58 devm_platform_ioremap_resource(struct platform_device *pdev,
59                                unsigned int index);
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 *,
63                                                      unsigned int,
64                                                      const char *);
65 extern int platform_get_irq_byname(struct platform_device *, const char *);
66 extern int platform_add_devices(struct platform_device **, int);
67
68 struct platform_device_info {
69                 struct device *parent;
70                 struct fwnode_handle *fwnode;
71                 bool of_node_reused;
72
73                 const char *name;
74                 int id;
75
76                 const struct resource *res;
77                 unsigned int num_res;
78
79                 const void *data;
80                 size_t size_data;
81                 u64 dma_mask;
82
83                 struct property_entry *properties;
84 };
85 extern struct platform_device *platform_device_register_full(
86                 const struct platform_device_info *pdevinfo);
87
88 /**
89  * platform_device_register_resndata - add a platform-level device with
90  * resources and platform-specific data
91  *
92  * @parent: parent device for the device we're adding
93  * @name: base name of the device we're adding
94  * @id: instance id
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
99  *
100  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
101  */
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) {
106
107         struct platform_device_info pdevinfo = {
108                 .parent = parent,
109                 .name = name,
110                 .id = id,
111                 .res = res,
112                 .num_res = num,
113                 .data = data,
114                 .size_data = size,
115                 .dma_mask = 0,
116         };
117
118         return platform_device_register_full(&pdevinfo);
119 }
120
121 /**
122  * platform_device_register_simple - add a platform-level device and its resources
123  * @name: base name of the device we're adding
124  * @id: instance id
125  * @res: set of resources that needs to be allocated for the device
126  * @num: number of resources
127  *
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
132  * dropped.
133  *
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
139  * "hotplugged".
140  *
141  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
142  */
143 static inline struct platform_device *platform_device_register_simple(
144                 const char *name, int id,
145                 const struct resource *res, unsigned int num)
146 {
147         return platform_device_register_resndata(NULL, name, id,
148                         res, num, NULL, 0);
149 }
150
151 /**
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
155  * @id: instance id
156  * @data: platform specific data for this platform device
157  * @size: size of platform specific data
158  *
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
163  * dropped.
164  *
165  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
166  */
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)
170 {
171         return platform_device_register_resndata(parent, name, id,
172                         NULL, 0, data, size);
173 }
174
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,
178                                          unsigned int num);
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);
186
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;
196 };
197
198 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
199                                  driver))
200
201 /*
202  * use a macro to avoid include chaining to get THIS_MODULE
203  */
204 #define platform_driver_register(drv) \
205         __platform_driver_register(drv, THIS_MODULE)
206 extern int __platform_driver_register(struct platform_driver *,
207                                         struct module *);
208 extern void platform_driver_unregister(struct platform_driver *);
209
210 /* non-hotpluggable platform devices may use this so that probe() and
211  * its support may live in __init sections, conserving runtime memory.
212  */
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);
217
218 static inline void *platform_get_drvdata(const struct platform_device *pdev)
219 {
220         return dev_get_drvdata(&pdev->dev);
221 }
222
223 static inline void platform_set_drvdata(struct platform_device *pdev,
224                                         void *data)
225 {
226         dev_set_drvdata(&pdev->dev, data);
227 }
228
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()
233  */
234 #define module_platform_driver(__platform_driver) \
235         module_driver(__platform_driver, platform_driver_register, \
236                         platform_driver_unregister)
237
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.
243  */
244 #define builtin_platform_driver(__platform_driver) \
245         builtin_driver(__platform_driver, platform_driver_register)
246
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()
251  */
252 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
253 static int __init __platform_driver##_init(void) \
254 { \
255         return platform_driver_probe(&(__platform_driver), \
256                                      __platform_probe);    \
257 } \
258 module_init(__platform_driver##_init); \
259 static void __exit __platform_driver##_exit(void) \
260 { \
261         platform_driver_unregister(&(__platform_driver)); \
262 } \
263 module_exit(__platform_driver##_exit);
264
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.
270  */
271 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
272 static int __init __platform_driver##_init(void) \
273 { \
274         return platform_driver_probe(&(__platform_driver), \
275                                      __platform_probe);    \
276 } \
277 device_initcall(__platform_driver##_init); \
278
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);
285
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,
289                                  unsigned int count);
290
291 #define platform_register_drivers(drivers, count) \
292         __platform_register_drivers(drivers, count, THIS_MODULE)
293
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;
299         int requested_id;
300         char *buffer;
301         int bufsize;
302 };
303
304 #define EARLY_PLATFORM_ID_UNSET -2
305 #define EARLY_PLATFORM_ID_ERROR -3
306
307 extern int early_platform_driver_register(struct early_platform_driver *epdrv,
308                                           char *buf);
309 extern void early_platform_add_devices(struct platform_device **devs, int num);
310
311 static inline int is_early_platform_device(struct platform_device *pdev)
312 {
313         return !pdev->dev.driver;
314 }
315
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);
320
321 #define early_platform_init(class_string, platdrv)              \
322         early_platform_init_buffer(class_string, platdrv, NULL, 0)
323
324 #ifndef MODULE
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,                                      \
328         .buffer = buf,                                                  \
329         .bufsize = bufsiz,                                              \
330         .pdrv = platdrv,                                                \
331         .requested_id = EARLY_PLATFORM_ID_UNSET,                        \
332 };                                                                      \
333 static int __init early_platform_driver_setup_func(char *buffer)        \
334 {                                                                       \
335         return early_platform_driver_register(&early_driver, buffer);   \
336 }                                                                       \
337 early_param(class_string, early_platform_driver_setup_func)
338 #else /* MODULE */
339 #define early_platform_init_buffer(class_string, platdrv, buf, bufsiz)  \
340 static inline char *early_platform_driver_setup_func(void)              \
341 {                                                                       \
342         return bufsiz ? buf : NULL;                                     \
343 }
344 #endif /* MODULE */
345
346 #ifdef CONFIG_SUSPEND
347 extern int platform_pm_suspend(struct device *dev);
348 extern int platform_pm_resume(struct device *dev);
349 #else
350 #define platform_pm_suspend             NULL
351 #define platform_pm_resume              NULL
352 #endif
353
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);
359 #else
360 #define platform_pm_freeze              NULL
361 #define platform_pm_thaw                NULL
362 #define platform_pm_poweroff            NULL
363 #define platform_pm_restore             NULL
364 #endif
365
366 extern int platform_dma_configure(struct device *dev);
367
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,
376 #else
377 #define USE_PLATFORM_PM_SLEEP_OPS
378 #endif
379
380 #endif /* _PLATFORM_DEVICE_H_ */
This page took 0.054274 seconds and 4 git commands to generate.