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