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