xref: /linux/include/linux/platform_device.h (revision 14cf406e083c0541e40cd467ae8336ecceede09e)
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 *
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 *
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 *
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 /**
117  * struct platform_device_info - set of parameters for creating a platform device
118  * @parent: parent device for the new platform device.
119  * @fwnode: firmware node associated with the device.
120  * @of_node_reused: indicates that device tree node associated with the device
121  *	is shared with another device, typically its ancestor. Setting this to
122  *	%true prevents the device from being matched via the OF match table,
123  *	and stops the device core from automatically binding pinctrl
124  *	configuration to avoid disrupting the other device.
125  * @name: name of the device.
126  * @id: instance ID of the device. Use %PLATFORM_DEVID_NONE if there is only
127  *	one instance of the device, or %PLATFORM_DEVID_AUTO to let the
128  *	kernel automatically assign a unique instance ID.
129  * @res: set of resources to attach to the device.
130  * @num_res: number of entries in @res.
131  * @data: device-specific data for this platform device.
132  * @size_data: size of device-specific data.
133  * @dma_mask: DMA mask for the device.
134  * @swnode: a secondary software node to be attached to the device. The node
135  *	will be automatically registered and its lifetime tied to the platform
136  *	device if it is not registered yet.
137  * @properties: a set of software properties for the device. If provided,
138  *	a managed software node will be automatically created and
139  *	assigned to the device. The properties array must be terminated
140  *	with a sentinel entry. Specifying both @properties and @swnode is not
141  *	allowed.
142  *
143  * This structure is used to hold information needed to create and register
144  * a platform device using platform_device_register_full().
145  *
146  * platform_device_register_full() makes deep copies of @name, @res, @data and
147  * @properties, so the caller does not need to keep them after registration.
148  * If the registration is performed during initialization, these can be marked
149  * as __initconst.
150  */
151 struct platform_device_info {
152 	struct device *parent;
153 	struct fwnode_handle *fwnode;
154 	bool of_node_reused;
155 
156 	const char *name;
157 	int id;
158 
159 	const struct resource *res;
160 	unsigned int num_res;
161 
162 	const void *data;
163 	size_t size_data;
164 	u64 dma_mask;
165 
166 	const struct software_node *swnode;
167 	const struct property_entry *properties;
168 };
169 extern struct platform_device *platform_device_register_full(
170 		const struct platform_device_info *pdevinfo);
171 
172 /**
173  * platform_device_register_resndata - add a platform-level device with
174  * resources and platform-specific data
175  *
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  * @res: set of resources that needs to be allocated for the device
180  * @num: number of resources
181  * @data: platform specific data for this platform device
182  * @size: size of platform specific data
183  *
184  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
185  */
186 static inline struct platform_device *platform_device_register_resndata(
187 		struct device *parent, const char *name, int id,
188 		const struct resource *res, unsigned int num,
189 		const void *data, size_t size) {
190 
191 	struct platform_device_info pdevinfo = {
192 		.parent = parent,
193 		.name = name,
194 		.id = id,
195 		.res = res,
196 		.num_res = num,
197 		.data = data,
198 		.size_data = size,
199 		.dma_mask = 0,
200 	};
201 
202 	return platform_device_register_full(&pdevinfo);
203 }
204 
205 /**
206  * platform_device_register_simple - add a platform-level device and its resources
207  * @name: base name of the device we're adding
208  * @id: instance id
209  * @res: set of resources that needs to be allocated for the device
210  * @num: number of resources
211  *
212  * This function creates a simple platform device that requires minimal
213  * resource and memory management. Canned release function freeing memory
214  * allocated for the device allows drivers using such devices to be
215  * unloaded without waiting for the last reference to the device to be
216  * dropped.
217  *
218  * This interface is primarily intended for use with legacy drivers which
219  * probe hardware directly.  Because such drivers create sysfs device nodes
220  * themselves, rather than letting system infrastructure handle such device
221  * enumeration tasks, they don't fully conform to the Linux driver model.
222  * In particular, when such drivers are built as modules, they can't be
223  * "hotplugged".
224  *
225  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
226  */
227 static inline struct platform_device *platform_device_register_simple(
228 		const char *name, int id,
229 		const struct resource *res, unsigned int num)
230 {
231 	return platform_device_register_resndata(NULL, name, id,
232 			res, num, NULL, 0);
233 }
234 
235 /**
236  * platform_device_register_data - add a platform-level device with platform-specific data
237  * @parent: parent device for the device we're adding
238  * @name: base name of the device we're adding
239  * @id: instance id
240  * @data: platform specific data for this platform device
241  * @size: size of platform specific data
242  *
243  * This function creates a simple platform device that requires minimal
244  * resource and memory management. Canned release function freeing memory
245  * allocated for the device allows drivers using such devices to be
246  * unloaded without waiting for the last reference to the device to be
247  * dropped.
248  *
249  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
250  */
251 static inline struct platform_device *platform_device_register_data(
252 		struct device *parent, const char *name, int id,
253 		const void *data, size_t size)
254 {
255 	return platform_device_register_resndata(parent, name, id,
256 			NULL, 0, data, size);
257 }
258 
259 extern struct platform_device *platform_device_alloc(const char *name, int id);
260 extern int platform_device_add_resources(struct platform_device *pdev,
261 					 const struct resource *res,
262 					 unsigned int num);
263 extern int platform_device_add_data(struct platform_device *pdev,
264 				    const void *data, size_t size);
265 extern int platform_device_add(struct platform_device *pdev);
266 extern void platform_device_del(struct platform_device *pdev);
267 extern void platform_device_put(struct platform_device *pdev);
268 DEFINE_FREE(platform_device_put, struct platform_device *, if (_T) platform_device_put(_T))
269 
270 struct platform_driver {
271 	int (*probe)(struct platform_device *);
272 	void (*remove)(struct platform_device *);
273 	void (*shutdown)(struct platform_device *);
274 	int (*suspend)(struct platform_device *, pm_message_t state);
275 	int (*resume)(struct platform_device *);
276 	struct device_driver driver;
277 	const struct platform_device_id *id_table;
278 	bool prevent_deferred_probe;
279 	/*
280 	 * For most device drivers, no need to care about this flag as long as
281 	 * all DMAs are handled through the kernel DMA API. For some special
282 	 * ones, for example VFIO drivers, they know how to manage the DMA
283 	 * themselves and set this flag so that the IOMMU layer will allow them
284 	 * to setup and manage their own I/O address space.
285 	 */
286 	bool driver_managed_dma;
287 };
288 
289 #define to_platform_driver(drv)	(container_of((drv), struct platform_driver, \
290 				 driver))
291 
292 /*
293  * use a macro to avoid include chaining to get THIS_MODULE
294  */
295 #define platform_driver_register(drv) \
296 	__platform_driver_register(drv, THIS_MODULE)
297 extern int __platform_driver_register(struct platform_driver *,
298 					struct module *);
299 extern void platform_driver_unregister(struct platform_driver *);
300 
301 /* non-hotpluggable platform devices may use this so that probe() and
302  * its support may live in __init sections, conserving runtime memory.
303  */
304 #define platform_driver_probe(drv, probe) \
305 	__platform_driver_probe(drv, probe, THIS_MODULE)
306 extern int __platform_driver_probe(struct platform_driver *driver,
307 		int (*probe)(struct platform_device *), struct module *module);
308 
309 static inline void *platform_get_drvdata(const struct platform_device *pdev)
310 {
311 	return dev_get_drvdata(&pdev->dev);
312 }
313 
314 static inline void platform_set_drvdata(struct platform_device *pdev,
315 					void *data)
316 {
317 	dev_set_drvdata(&pdev->dev, data);
318 }
319 
320 /* module_platform_driver() - Helper macro for drivers that don't do
321  * anything special in module init/exit.  This eliminates a lot of
322  * boilerplate.  Each module may only use this macro once, and
323  * calling it replaces module_init() and module_exit()
324  */
325 #define module_platform_driver(__platform_driver) \
326 	module_driver(__platform_driver, platform_driver_register, \
327 			platform_driver_unregister)
328 
329 /* builtin_platform_driver() - Helper macro for builtin drivers that
330  * don't do anything special in driver init.  This eliminates some
331  * boilerplate.  Each driver may only use this macro once, and
332  * calling it replaces device_initcall().  Note this is meant to be
333  * a parallel of module_platform_driver() above, but w/o _exit stuff.
334  */
335 #define builtin_platform_driver(__platform_driver) \
336 	builtin_driver(__platform_driver, platform_driver_register)
337 
338 /* module_platform_driver_probe() - Helper macro for drivers that don't do
339  * anything special in module init/exit.  This eliminates a lot of
340  * boilerplate.  Each module may only use this macro once, and
341  * calling it replaces module_init() and module_exit()
342  */
343 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
344 static int __init __platform_driver##_init(void) \
345 { \
346 	return platform_driver_probe(&(__platform_driver), \
347 				     __platform_probe);    \
348 } \
349 module_init(__platform_driver##_init); \
350 static void __exit __platform_driver##_exit(void) \
351 { \
352 	platform_driver_unregister(&(__platform_driver)); \
353 } \
354 module_exit(__platform_driver##_exit);
355 
356 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
357  * anything special in device init.  This eliminates some boilerplate.  Each
358  * driver may only use this macro once, and using it replaces device_initcall.
359  * This is meant to be a parallel of module_platform_driver_probe above, but
360  * without the __exit parts.
361  */
362 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
363 static int __init __platform_driver##_init(void) \
364 { \
365 	return platform_driver_probe(&(__platform_driver), \
366 				     __platform_probe);    \
367 } \
368 device_initcall(__platform_driver##_init); \
369 
370 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
371 	__platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
372 extern struct platform_device *__platform_create_bundle(
373 	struct platform_driver *driver, int (*probe)(struct platform_device *),
374 	struct resource *res, unsigned int n_res,
375 	const void *data, size_t size, struct module *module);
376 
377 int __platform_register_drivers(struct platform_driver * const *drivers,
378 				unsigned int count, struct module *owner);
379 void platform_unregister_drivers(struct platform_driver * const *drivers,
380 				 unsigned int count);
381 
382 #define platform_register_drivers(drivers, count) \
383 	__platform_register_drivers(drivers, count, THIS_MODULE)
384 
385 #ifdef CONFIG_SUSPEND
386 extern int platform_pm_suspend(struct device *dev);
387 extern int platform_pm_resume(struct device *dev);
388 #else
389 #define platform_pm_suspend		NULL
390 #define platform_pm_resume		NULL
391 #endif
392 
393 #ifdef CONFIG_HIBERNATE_CALLBACKS
394 extern int platform_pm_freeze(struct device *dev);
395 extern int platform_pm_thaw(struct device *dev);
396 extern int platform_pm_poweroff(struct device *dev);
397 extern int platform_pm_restore(struct device *dev);
398 #else
399 #define platform_pm_freeze		NULL
400 #define platform_pm_thaw		NULL
401 #define platform_pm_poweroff		NULL
402 #define platform_pm_restore		NULL
403 #endif
404 
405 #ifdef CONFIG_PM_SLEEP
406 #define USE_PLATFORM_PM_SLEEP_OPS \
407 	.suspend = platform_pm_suspend, \
408 	.resume = platform_pm_resume, \
409 	.freeze = platform_pm_freeze, \
410 	.thaw = platform_pm_thaw, \
411 	.poweroff = platform_pm_poweroff, \
412 	.restore = platform_pm_restore,
413 #else
414 #define USE_PLATFORM_PM_SLEEP_OPS
415 #endif
416 
417 #ifndef CONFIG_SUPERH
418 /*
419  * REVISIT: This stub is needed for all non-SuperH users of early platform
420  * drivers. It should go away once we introduce the new platform_device-based
421  * early driver framework.
422  */
423 static inline int is_sh_early_platform_device(struct platform_device *pdev)
424 {
425 	return 0;
426 }
427 #endif /* CONFIG_SUPERH */
428 
429 /* For now only SuperH uses it */
430 void early_platform_cleanup(void);
431 
432 #endif /* _PLATFORM_DEVICE_H_ */
433