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