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