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