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 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 char *driver_override; /* Driver name to force a match */
35
36 /* MFD cell pointer */
37 struct mfd_cell *mfd_cell;
38
39 /* arch specific additions */
40 struct pdev_archdata archdata;
41
42 ANDROID_KABI_RESERVE(1);
43 ANDROID_KABI_RESERVE(2);
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 ANDROID_KABI_RESERVE(1);
103 };
104 extern struct platform_device *platform_device_register_full(
105 const struct platform_device_info *pdevinfo);
106
107 /**
108 * platform_device_register_resndata - add a platform-level device with
109 * resources and platform-specific data
110 *
111 * @parent: parent device for the device we're adding
112 * @name: base name of the device we're adding
113 * @id: instance id
114 * @res: set of resources that needs to be allocated for the device
115 * @num: number of resources
116 * @data: platform specific data for this platform device
117 * @size: size of platform specific data
118 *
119 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
120 */
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)121 static inline struct platform_device *platform_device_register_resndata(
122 struct device *parent, const char *name, int id,
123 const struct resource *res, unsigned int num,
124 const void *data, size_t size) {
125
126 struct platform_device_info pdevinfo = {
127 .parent = parent,
128 .name = name,
129 .id = id,
130 .res = res,
131 .num_res = num,
132 .data = data,
133 .size_data = size,
134 .dma_mask = 0,
135 };
136
137 return platform_device_register_full(&pdevinfo);
138 }
139
140 /**
141 * platform_device_register_simple - add a platform-level device and its resources
142 * @name: base name of the device we're adding
143 * @id: instance id
144 * @res: set of resources that needs to be allocated for the device
145 * @num: number of resources
146 *
147 * This function creates a simple platform device that requires minimal
148 * resource and memory management. Canned release function freeing memory
149 * allocated for the device allows drivers using such devices to be
150 * unloaded without waiting for the last reference to the device to be
151 * dropped.
152 *
153 * This interface is primarily intended for use with legacy drivers which
154 * probe hardware directly. Because such drivers create sysfs device nodes
155 * themselves, rather than letting system infrastructure handle such device
156 * enumeration tasks, they don't fully conform to the Linux driver model.
157 * In particular, when such drivers are built as modules, they can't be
158 * "hotplugged".
159 *
160 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
161 */
platform_device_register_simple(const char * name,int id,const struct resource * res,unsigned int num)162 static inline struct platform_device *platform_device_register_simple(
163 const char *name, int id,
164 const struct resource *res, unsigned int num)
165 {
166 return platform_device_register_resndata(NULL, name, id,
167 res, num, NULL, 0);
168 }
169
170 /**
171 * platform_device_register_data - add a platform-level device with platform-specific data
172 * @parent: parent device for the device we're adding
173 * @name: base name of the device we're adding
174 * @id: instance id
175 * @data: platform specific data for this platform device
176 * @size: size of platform specific data
177 *
178 * This function creates a simple platform device that requires minimal
179 * resource and memory management. Canned release function freeing memory
180 * allocated for the device allows drivers using such devices to be
181 * unloaded without waiting for the last reference to the device to be
182 * dropped.
183 *
184 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
185 */
platform_device_register_data(struct device * parent,const char * name,int id,const void * data,size_t size)186 static inline struct platform_device *platform_device_register_data(
187 struct device *parent, const char *name, int id,
188 const void *data, size_t size)
189 {
190 return platform_device_register_resndata(parent, name, id,
191 NULL, 0, data, size);
192 }
193
194 extern struct platform_device *platform_device_alloc(const char *name, int id);
195 extern int platform_device_add_resources(struct platform_device *pdev,
196 const struct resource *res,
197 unsigned int num);
198 extern int platform_device_add_data(struct platform_device *pdev,
199 const void *data, size_t size);
200 extern int platform_device_add_properties(struct platform_device *pdev,
201 const struct property_entry *properties);
202 extern int platform_device_add(struct platform_device *pdev);
203 extern void platform_device_del(struct platform_device *pdev);
204 extern void platform_device_put(struct platform_device *pdev);
205
206 struct platform_driver {
207 int (*probe)(struct platform_device *);
208 int (*remove)(struct platform_device *);
209 void (*shutdown)(struct platform_device *);
210 int (*suspend)(struct platform_device *, pm_message_t state);
211 int (*resume)(struct platform_device *);
212 struct device_driver driver;
213 const struct platform_device_id *id_table;
214 bool prevent_deferred_probe;
215
216 ANDROID_KABI_RESERVE(1);
217 };
218
219 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
220 driver))
221
222 /*
223 * use a macro to avoid include chaining to get THIS_MODULE
224 */
225 #define platform_driver_register(drv) \
226 __platform_driver_register(drv, THIS_MODULE)
227 extern int __platform_driver_register(struct platform_driver *,
228 struct module *);
229 extern void platform_driver_unregister(struct platform_driver *);
230
231 /* non-hotpluggable platform devices may use this so that probe() and
232 * its support may live in __init sections, conserving runtime memory.
233 */
234 #define platform_driver_probe(drv, probe) \
235 __platform_driver_probe(drv, probe, THIS_MODULE)
236 extern int __platform_driver_probe(struct platform_driver *driver,
237 int (*probe)(struct platform_device *), struct module *module);
238
platform_get_drvdata(const struct platform_device * pdev)239 static inline void *platform_get_drvdata(const struct platform_device *pdev)
240 {
241 return dev_get_drvdata(&pdev->dev);
242 }
243
platform_set_drvdata(struct platform_device * pdev,void * data)244 static inline void platform_set_drvdata(struct platform_device *pdev,
245 void *data)
246 {
247 dev_set_drvdata(&pdev->dev, data);
248 }
249
250 /* module_platform_driver() - Helper macro for drivers that don't do
251 * anything special in module init/exit. This eliminates a lot of
252 * boilerplate. Each module may only use this macro once, and
253 * calling it replaces module_init() and module_exit()
254 */
255 #define module_platform_driver(__platform_driver) \
256 module_driver(__platform_driver, platform_driver_register, \
257 platform_driver_unregister)
258
259 /* builtin_platform_driver() - Helper macro for builtin drivers that
260 * don't do anything special in driver init. This eliminates some
261 * boilerplate. Each driver may only use this macro once, and
262 * calling it replaces device_initcall(). Note this is meant to be
263 * a parallel of module_platform_driver() above, but w/o _exit stuff.
264 */
265 #define builtin_platform_driver(__platform_driver) \
266 builtin_driver(__platform_driver, platform_driver_register)
267
268 /* module_platform_driver_probe() - Helper macro for drivers that don't do
269 * anything special in module init/exit. This eliminates a lot of
270 * boilerplate. Each module may only use this macro once, and
271 * calling it replaces module_init() and module_exit()
272 */
273 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
274 static int __init __platform_driver##_init(void) \
275 { \
276 return platform_driver_probe(&(__platform_driver), \
277 __platform_probe); \
278 } \
279 module_init(__platform_driver##_init); \
280 static void __exit __platform_driver##_exit(void) \
281 { \
282 platform_driver_unregister(&(__platform_driver)); \
283 } \
284 module_exit(__platform_driver##_exit);
285
286 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
287 * anything special in device init. This eliminates some boilerplate. Each
288 * driver may only use this macro once, and using it replaces device_initcall.
289 * This is meant to be a parallel of module_platform_driver_probe above, but
290 * without the __exit parts.
291 */
292 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
293 static int __init __platform_driver##_init(void) \
294 { \
295 return platform_driver_probe(&(__platform_driver), \
296 __platform_probe); \
297 } \
298 device_initcall(__platform_driver##_init); \
299
300 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
301 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
302 extern struct platform_device *__platform_create_bundle(
303 struct platform_driver *driver, int (*probe)(struct platform_device *),
304 struct resource *res, unsigned int n_res,
305 const void *data, size_t size, struct module *module);
306
307 int __platform_register_drivers(struct platform_driver * const *drivers,
308 unsigned int count, struct module *owner);
309 void platform_unregister_drivers(struct platform_driver * const *drivers,
310 unsigned int count);
311
312 #define platform_register_drivers(drivers, count) \
313 __platform_register_drivers(drivers, count, THIS_MODULE)
314
315 #ifdef CONFIG_SUSPEND
316 extern int platform_pm_suspend(struct device *dev);
317 extern int platform_pm_resume(struct device *dev);
318 #else
319 #define platform_pm_suspend NULL
320 #define platform_pm_resume NULL
321 #endif
322
323 #ifdef CONFIG_HIBERNATE_CALLBACKS
324 extern int platform_pm_freeze(struct device *dev);
325 extern int platform_pm_thaw(struct device *dev);
326 extern int platform_pm_poweroff(struct device *dev);
327 extern int platform_pm_restore(struct device *dev);
328 #else
329 #define platform_pm_freeze NULL
330 #define platform_pm_thaw NULL
331 #define platform_pm_poweroff NULL
332 #define platform_pm_restore NULL
333 #endif
334
335 extern int platform_dma_configure(struct device *dev);
336
337 #ifdef CONFIG_PM_SLEEP
338 #define USE_PLATFORM_PM_SLEEP_OPS \
339 .suspend = platform_pm_suspend, \
340 .resume = platform_pm_resume, \
341 .freeze = platform_pm_freeze, \
342 .thaw = platform_pm_thaw, \
343 .poweroff = platform_pm_poweroff, \
344 .restore = platform_pm_restore,
345 #else
346 #define USE_PLATFORM_PM_SLEEP_OPS
347 #endif
348
349 #ifndef CONFIG_SUPERH
350 /*
351 * REVISIT: This stub is needed for all non-SuperH users of early platform
352 * drivers. It should go away once we introduce the new platform_device-based
353 * early driver framework.
354 */
is_sh_early_platform_device(struct platform_device * pdev)355 static inline int is_sh_early_platform_device(struct platform_device *pdev)
356 {
357 return 0;
358 }
359 #endif /* CONFIG_SUPERH */
360
361 /* For now only SuperH uses it */
362 void early_platform_cleanup(void);
363
364 #endif /* _PLATFORM_DEVICE_H_ */
365