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