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