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