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