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 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 * Traditionally the remove callback returned an int which however is
247 * ignored by the driver core. This led to wrong expectations by driver
248 * authors who thought returning an error code was a valid error
249 * handling strategy. To convert to a callback returning void, new
250 * drivers should implement .remove_new() until the conversion it done
251 * that eventually makes .remove() return void.
252 */
253 int (*remove)(struct platform_device *);
254 void (*remove_new)(struct platform_device *);
255
256 void (*shutdown)(struct platform_device *);
257 int (*suspend)(struct platform_device *, pm_message_t state);
258 int (*resume)(struct platform_device *);
259 struct device_driver driver;
260 const struct platform_device_id *id_table;
261 bool prevent_deferred_probe;
262 /*
263 * For most device drivers, no need to care about this flag as long as
264 * all DMAs are handled through the kernel DMA API. For some special
265 * ones, for example VFIO drivers, they know how to manage the DMA
266 * themselves and set this flag so that the IOMMU layer will allow them
267 * to setup and manage their own I/O address space.
268 */
269 bool driver_managed_dma;
270
271 ANDROID_KABI_RESERVE(1);
272 };
273
274 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
275 driver))
276
277 /*
278 * use a macro to avoid include chaining to get THIS_MODULE
279 */
280 #define platform_driver_register(drv) \
281 __platform_driver_register(drv, THIS_MODULE)
282 extern int __platform_driver_register(struct platform_driver *,
283 struct module *);
284 extern void platform_driver_unregister(struct platform_driver *);
285
286 /* non-hotpluggable platform devices may use this so that probe() and
287 * its support may live in __init sections, conserving runtime memory.
288 */
289 #define platform_driver_probe(drv, probe) \
290 __platform_driver_probe(drv, probe, THIS_MODULE)
291 extern int __platform_driver_probe(struct platform_driver *driver,
292 int (*probe)(struct platform_device *), struct module *module);
293
platform_get_drvdata(const struct platform_device * pdev)294 static inline void *platform_get_drvdata(const struct platform_device *pdev)
295 {
296 return dev_get_drvdata(&pdev->dev);
297 }
298
platform_set_drvdata(struct platform_device * pdev,void * data)299 static inline void platform_set_drvdata(struct platform_device *pdev,
300 void *data)
301 {
302 dev_set_drvdata(&pdev->dev, data);
303 }
304
305 /* module_platform_driver() - Helper macro for drivers that don't do
306 * anything special in module init/exit. This eliminates a lot of
307 * boilerplate. Each module may only use this macro once, and
308 * calling it replaces module_init() and module_exit()
309 */
310 #define module_platform_driver(__platform_driver) \
311 module_driver(__platform_driver, platform_driver_register, \
312 platform_driver_unregister)
313
314 /* builtin_platform_driver() - Helper macro for builtin drivers that
315 * don't do anything special in driver init. This eliminates some
316 * boilerplate. Each driver may only use this macro once, and
317 * calling it replaces device_initcall(). Note this is meant to be
318 * a parallel of module_platform_driver() above, but w/o _exit stuff.
319 */
320 #define builtin_platform_driver(__platform_driver) \
321 builtin_driver(__platform_driver, platform_driver_register)
322
323 /* module_platform_driver_probe() - Helper macro for drivers that don't do
324 * anything special in module init/exit. This eliminates a lot of
325 * boilerplate. Each module may only use this macro once, and
326 * calling it replaces module_init() and module_exit()
327 */
328 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
329 static int __init __platform_driver##_init(void) \
330 { \
331 return platform_driver_probe(&(__platform_driver), \
332 __platform_probe); \
333 } \
334 module_init(__platform_driver##_init); \
335 static void __exit __platform_driver##_exit(void) \
336 { \
337 platform_driver_unregister(&(__platform_driver)); \
338 } \
339 module_exit(__platform_driver##_exit);
340
341 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
342 * anything special in device init. This eliminates some boilerplate. Each
343 * driver may only use this macro once, and using it replaces device_initcall.
344 * This is meant to be a parallel of module_platform_driver_probe above, but
345 * without the __exit parts.
346 */
347 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
348 static int __init __platform_driver##_init(void) \
349 { \
350 return platform_driver_probe(&(__platform_driver), \
351 __platform_probe); \
352 } \
353 device_initcall(__platform_driver##_init); \
354
355 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
356 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
357 extern struct platform_device *__platform_create_bundle(
358 struct platform_driver *driver, int (*probe)(struct platform_device *),
359 struct resource *res, unsigned int n_res,
360 const void *data, size_t size, struct module *module);
361
362 int __platform_register_drivers(struct platform_driver * const *drivers,
363 unsigned int count, struct module *owner);
364 void platform_unregister_drivers(struct platform_driver * const *drivers,
365 unsigned int count);
366
367 #define platform_register_drivers(drivers, count) \
368 __platform_register_drivers(drivers, count, THIS_MODULE)
369
370 #ifdef CONFIG_SUSPEND
371 extern int platform_pm_suspend(struct device *dev);
372 extern int platform_pm_resume(struct device *dev);
373 #else
374 #define platform_pm_suspend NULL
375 #define platform_pm_resume NULL
376 #endif
377
378 #ifdef CONFIG_HIBERNATE_CALLBACKS
379 extern int platform_pm_freeze(struct device *dev);
380 extern int platform_pm_thaw(struct device *dev);
381 extern int platform_pm_poweroff(struct device *dev);
382 extern int platform_pm_restore(struct device *dev);
383 #else
384 #define platform_pm_freeze NULL
385 #define platform_pm_thaw NULL
386 #define platform_pm_poweroff NULL
387 #define platform_pm_restore NULL
388 #endif
389
390 #ifdef CONFIG_PM_SLEEP
391 #define USE_PLATFORM_PM_SLEEP_OPS \
392 .suspend = platform_pm_suspend, \
393 .resume = platform_pm_resume, \
394 .freeze = platform_pm_freeze, \
395 .thaw = platform_pm_thaw, \
396 .poweroff = platform_pm_poweroff, \
397 .restore = platform_pm_restore,
398 #else
399 #define USE_PLATFORM_PM_SLEEP_OPS
400 #endif
401
402 #ifndef CONFIG_SUPERH
403 /*
404 * REVISIT: This stub is needed for all non-SuperH users of early platform
405 * drivers. It should go away once we introduce the new platform_device-based
406 * early driver framework.
407 */
is_sh_early_platform_device(struct platform_device * pdev)408 static inline int is_sh_early_platform_device(struct platform_device *pdev)
409 {
410 return 0;
411 }
412 #endif /* CONFIG_SUPERH */
413
414 /* For now only SuperH uses it */
415 void early_platform_cleanup(void);
416
417 #endif /* _PLATFORM_DEVICE_H_ */
418