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