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