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