Merge tag 'devicetree-fixes-for-6.2-3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / include / linux / platform_device.h
CommitLineData
55716d26 1/* SPDX-License-Identifier: GPL-2.0-only */
bbbf508d
RK
2/*
3 * platform_device.h - generic, centralized driver model
4 *
5 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6 *
fe34c89d 7 * See Documentation/driver-api/driver-model/ for more information.
bbbf508d
RK
8 */
9
10#ifndef _PLATFORM_DEVICE_H_
11#define _PLATFORM_DEVICE_H_
12
13#include <linux/device.h>
14
689ae231
JD
15#define PLATFORM_DEVID_NONE (-1)
16#define PLATFORM_DEVID_AUTO (-2)
17
e15f2fa9 18struct irq_affinity;
e710d7d5 19struct mfd_cell;
f4d05266 20struct property_entry;
ac316725 21struct platform_device_id;
e710d7d5 22
bbbf508d 23struct platform_device {
6ae07f27 24 const char *name;
1359555e 25 int id;
689ae231 26 bool id_auto;
bbbf508d 27 struct device dev;
e3a36eb6 28 u64 platform_dma_mask;
9495b7e9 29 struct device_dma_parameters dma_parms;
bbbf508d 30 u32 num_resources;
6ae07f27 31 struct resource *resource;
57fee4a5 32
3d03ba4d 33 const struct platform_device_id *id_entry;
6c2f4211
KK
34 /*
35 * Driver name to force a match. Do not set directly, because core
36 * frees it. Use driver_set_override() to set or clear it.
37 */
38 const char *driver_override;
d7aacadd 39
e710d7d5
SO
40 /* MFD cell pointer */
41 struct mfd_cell *mfd_cell;
42
d7aacadd
MD
43 /* arch specific additions */
44 struct pdev_archdata archdata;
bbbf508d
RK
45};
46
57fee4a5
EM
47#define platform_get_device_id(pdev) ((pdev)->id_entry)
48
719cf71c 49#define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
bbbf508d
RK
50#define to_platform_device(x) container_of((x), struct platform_device, dev)
51
52extern int platform_device_register(struct platform_device *);
53extern void platform_device_unregister(struct platform_device *);
54
55extern struct bus_type platform_bus_type;
56extern struct device platform_bus;
57
6ae07f27
FP
58extern struct resource *platform_get_resource(struct platform_device *,
59 unsigned int, unsigned int);
0aec2da4
AS
60extern struct resource *platform_get_mem_or_io(struct platform_device *,
61 unsigned int);
62
36f3313d
SP
63extern struct device *
64platform_find_device_by_driver(struct device *start,
65 const struct device_driver *drv);
7945f929 66extern void __iomem *
890cc39a
DZ
67devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
68 unsigned int index, struct resource **res);
69extern void __iomem *
7945f929
BG
70devm_platform_ioremap_resource(struct platform_device *pdev,
71 unsigned int index);
bb6243b4 72extern void __iomem *
c9c8641d
BG
73devm_platform_ioremap_resource_byname(struct platform_device *pdev,
74 const char *name);
bbbf508d 75extern int platform_get_irq(struct platform_device *, unsigned int);
8973ea47 76extern int platform_get_irq_optional(struct platform_device *, unsigned int);
4b83555d 77extern int platform_irq_count(struct platform_device *);
e15f2fa9
JG
78extern int devm_platform_get_irqs_affinity(struct platform_device *dev,
79 struct irq_affinity *affd,
80 unsigned int minvec,
81 unsigned int maxvec,
82 int **irqs);
6ae07f27
FP
83extern struct resource *platform_get_resource_byname(struct platform_device *,
84 unsigned int,
85 const char *);
c0afe7ba 86extern int platform_get_irq_byname(struct platform_device *, const char *);
f1da567f
HG
87extern int platform_get_irq_byname_optional(struct platform_device *dev,
88 const char *name);
bbbf508d
RK
89extern int platform_add_devices(struct platform_device **, int);
90
01dcc60a
UKK
91struct platform_device_info {
92 struct device *parent;
ce793486 93 struct fwnode_handle *fwnode;
2c1ea6ab 94 bool of_node_reused;
01dcc60a
UKK
95
96 const char *name;
97 int id;
98
99 const struct resource *res;
100 unsigned int num_res;
101
102 const void *data;
103 size_t size_data;
104 u64 dma_mask;
00bbc1d8 105
469e1906 106 const struct property_entry *properties;
01dcc60a
UKK
107};
108extern struct platform_device *platform_device_register_full(
5a3072be 109 const struct platform_device_info *pdevinfo);
01dcc60a
UKK
110
111/**
112 * platform_device_register_resndata - add a platform-level device with
113 * resources and platform-specific data
114 *
115 * @parent: parent device for the device we're adding
116 * @name: base name of the device we're adding
117 * @id: instance id
118 * @res: set of resources that needs to be allocated for the device
119 * @num: number of resources
120 * @data: platform specific data for this platform device
121 * @size: size of platform specific data
122 *
123 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
124 */
125static inline struct platform_device *platform_device_register_resndata(
44f28bde
UKK
126 struct device *parent, const char *name, int id,
127 const struct resource *res, unsigned int num,
01dcc60a
UKK
128 const void *data, size_t size) {
129
130 struct platform_device_info pdevinfo = {
131 .parent = parent,
132 .name = name,
133 .id = id,
134 .res = res,
135 .num_res = num,
136 .data = data,
137 .size_data = size,
138 .dma_mask = 0,
139 };
140
141 return platform_device_register_full(&pdevinfo);
142}
44f28bde
UKK
143
144/**
145 * platform_device_register_simple - add a platform-level device and its resources
146 * @name: base name of the device we're adding
147 * @id: instance id
148 * @res: set of resources that needs to be allocated for the device
149 * @num: number of resources
150 *
151 * This function creates a simple platform device that requires minimal
152 * resource and memory management. Canned release function freeing memory
153 * allocated for the device allows drivers using such devices to be
154 * unloaded without waiting for the last reference to the device to be
155 * dropped.
156 *
157 * This interface is primarily intended for use with legacy drivers which
158 * probe hardware directly. Because such drivers create sysfs device nodes
159 * themselves, rather than letting system infrastructure handle such device
160 * enumeration tasks, they don't fully conform to the Linux driver model.
161 * In particular, when such drivers are built as modules, they can't be
162 * "hotplugged".
163 *
164 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
165 */
166static inline struct platform_device *platform_device_register_simple(
167 const char *name, int id,
168 const struct resource *res, unsigned int num)
169{
170 return platform_device_register_resndata(NULL, name, id,
171 res, num, NULL, 0);
172}
173
174/**
175 * platform_device_register_data - add a platform-level device with platform-specific data
176 * @parent: parent device for the device we're adding
177 * @name: base name of the device we're adding
178 * @id: instance id
179 * @data: platform specific data for this platform device
180 * @size: size of platform specific data
181 *
182 * This function creates a simple platform device that requires minimal
183 * resource and memory management. Canned release function freeing memory
184 * allocated for the device allows drivers using such devices to be
185 * unloaded without waiting for the last reference to the device to be
186 * dropped.
187 *
188 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
189 */
190static inline struct platform_device *platform_device_register_data(
191 struct device *parent, const char *name, int id,
192 const void *data, size_t size)
193{
194 return platform_device_register_resndata(parent, name, id,
195 NULL, 0, data, size);
196}
bbbf508d 197
1359555e 198extern struct platform_device *platform_device_alloc(const char *name, int id);
0b7f1a7e
GU
199extern int platform_device_add_resources(struct platform_device *pdev,
200 const struct resource *res,
201 unsigned int num);
6ae07f27
FP
202extern int platform_device_add_data(struct platform_device *pdev,
203 const void *data, size_t size);
37c12e74 204extern int platform_device_add(struct platform_device *pdev);
93ce3061 205extern void platform_device_del(struct platform_device *pdev);
37c12e74
RK
206extern void platform_device_put(struct platform_device *pdev);
207
00d3dcdd
RK
208struct platform_driver {
209 int (*probe)(struct platform_device *);
5c5a7680
UKK
210
211 /*
212 * Traditionally the remove callback returned an int which however is
213 * ignored by the driver core. This led to wrong expectations by driver
214 * authors who thought returning an error code was a valid error
215 * handling strategy. To convert to a callback returning void, new
216 * drivers should implement .remove_new() until the conversion it done
217 * that eventually makes .remove() return void.
218 */
00d3dcdd 219 int (*remove)(struct platform_device *);
5c5a7680
UKK
220 void (*remove_new)(struct platform_device *);
221
00d3dcdd
RK
222 void (*shutdown)(struct platform_device *);
223 int (*suspend)(struct platform_device *, pm_message_t state);
224 int (*resume)(struct platform_device *);
225 struct device_driver driver;
831fad2f 226 const struct platform_device_id *id_table;
3f9120b0 227 bool prevent_deferred_probe;
512881ea
LB
228 /*
229 * For most device drivers, no need to care about this flag as long as
230 * all DMAs are handled through the kernel DMA API. For some special
231 * ones, for example VFIO drivers, they know how to manage the DMA
232 * themselves and set this flag so that the IOMMU layer will allow them
233 * to setup and manage their own I/O address space.
234 */
235 bool driver_managed_dma;
00d3dcdd
RK
236};
237
10dbc5e3
RH
238#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
239 driver))
240
9447057e
LC
241/*
242 * use a macro to avoid include chaining to get THIS_MODULE
243 */
244#define platform_driver_register(drv) \
245 __platform_driver_register(drv, THIS_MODULE)
246extern int __platform_driver_register(struct platform_driver *,
247 struct module *);
00d3dcdd
RK
248extern void platform_driver_unregister(struct platform_driver *);
249
c67334fb
DB
250/* non-hotpluggable platform devices may use this so that probe() and
251 * its support may live in __init sections, conserving runtime memory.
252 */
c3b50dc2
WS
253#define platform_driver_probe(drv, probe) \
254 __platform_driver_probe(drv, probe, THIS_MODULE)
255extern int __platform_driver_probe(struct platform_driver *driver,
256 int (*probe)(struct platform_device *), struct module *module);
c67334fb 257
71d64290
MKB
258static inline void *platform_get_drvdata(const struct platform_device *pdev)
259{
260 return dev_get_drvdata(&pdev->dev);
261}
262
6ae07f27
FP
263static inline void platform_set_drvdata(struct platform_device *pdev,
264 void *data)
71d64290
MKB
265{
266 dev_set_drvdata(&pdev->dev, data);
267}
00d3dcdd 268
940ab889
GL
269/* module_platform_driver() - Helper macro for drivers that don't do
270 * anything special in module init/exit. This eliminates a lot of
271 * boilerplate. Each module may only use this macro once, and
272 * calling it replaces module_init() and module_exit()
273 */
274#define module_platform_driver(__platform_driver) \
907d0ed1
LPC
275 module_driver(__platform_driver, platform_driver_register, \
276 platform_driver_unregister)
940ab889 277
f309d444
PG
278/* builtin_platform_driver() - Helper macro for builtin drivers that
279 * don't do anything special in driver init. This eliminates some
280 * boilerplate. Each driver may only use this macro once, and
281 * calling it replaces device_initcall(). Note this is meant to be
282 * a parallel of module_platform_driver() above, but w/o _exit stuff.
283 */
284#define builtin_platform_driver(__platform_driver) \
285 builtin_driver(__platform_driver, platform_driver_register)
286
bab734fc
FP
287/* module_platform_driver_probe() - Helper macro for drivers that don't do
288 * anything special in module init/exit. This eliminates a lot of
289 * boilerplate. Each module may only use this macro once, and
290 * calling it replaces module_init() and module_exit()
291 */
292#define module_platform_driver_probe(__platform_driver, __platform_probe) \
293static int __init __platform_driver##_init(void) \
294{ \
295 return platform_driver_probe(&(__platform_driver), \
296 __platform_probe); \
297} \
298module_init(__platform_driver##_init); \
299static void __exit __platform_driver##_exit(void) \
300{ \
301 platform_driver_unregister(&(__platform_driver)); \
302} \
303module_exit(__platform_driver##_exit);
304
f309d444
PG
305/* builtin_platform_driver_probe() - Helper macro for drivers that don't do
306 * anything special in device init. This eliminates some boilerplate. Each
307 * driver may only use this macro once, and using it replaces device_initcall.
308 * This is meant to be a parallel of module_platform_driver_probe above, but
309 * without the __exit parts.
310 */
311#define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
312static int __init __platform_driver##_init(void) \
313{ \
314 return platform_driver_probe(&(__platform_driver), \
315 __platform_probe); \
316} \
317device_initcall(__platform_driver##_init); \
318
291f653a
WS
319#define platform_create_bundle(driver, probe, res, n_res, data, size) \
320 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
321extern struct platform_device *__platform_create_bundle(
6ae07f27
FP
322 struct platform_driver *driver, int (*probe)(struct platform_device *),
323 struct resource *res, unsigned int n_res,
291f653a 324 const void *data, size_t size, struct module *module);
ecdf6ceb 325
dbe2256d
TR
326int __platform_register_drivers(struct platform_driver * const *drivers,
327 unsigned int count, struct module *owner);
328void platform_unregister_drivers(struct platform_driver * const *drivers,
329 unsigned int count);
330
331#define platform_register_drivers(drivers, count) \
332 __platform_register_drivers(drivers, count, THIS_MODULE)
333
69c9dd1e
RW
334#ifdef CONFIG_SUSPEND
335extern int platform_pm_suspend(struct device *dev);
69c9dd1e 336extern int platform_pm_resume(struct device *dev);
69c9dd1e
RW
337#else
338#define platform_pm_suspend NULL
339#define platform_pm_resume NULL
69c9dd1e
RW
340#endif
341
342#ifdef CONFIG_HIBERNATE_CALLBACKS
343extern int platform_pm_freeze(struct device *dev);
69c9dd1e 344extern int platform_pm_thaw(struct device *dev);
69c9dd1e 345extern int platform_pm_poweroff(struct device *dev);
69c9dd1e 346extern int platform_pm_restore(struct device *dev);
69c9dd1e
RW
347#else
348#define platform_pm_freeze NULL
349#define platform_pm_thaw NULL
350#define platform_pm_poweroff NULL
351#define platform_pm_restore NULL
69c9dd1e
RW
352#endif
353
354#ifdef CONFIG_PM_SLEEP
355#define USE_PLATFORM_PM_SLEEP_OPS \
69c9dd1e
RW
356 .suspend = platform_pm_suspend, \
357 .resume = platform_pm_resume, \
358 .freeze = platform_pm_freeze, \
359 .thaw = platform_pm_thaw, \
360 .poweroff = platform_pm_poweroff, \
9b39e73d 361 .restore = platform_pm_restore,
69c9dd1e
RW
362#else
363#define USE_PLATFORM_PM_SLEEP_OPS
364#endif
365
507fd01d
BG
366#ifndef CONFIG_SUPERH
367/*
368 * REVISIT: This stub is needed for all non-SuperH users of early platform
369 * drivers. It should go away once we introduce the new platform_device-based
370 * early driver framework.
371 */
201e9109 372static inline int is_sh_early_platform_device(struct platform_device *pdev)
507fd01d
BG
373{
374 return 0;
375}
376#endif /* CONFIG_SUPERH */
377
1768289b
AS
378/* For now only SuperH uses it */
379void early_platform_cleanup(void);
380
bbbf508d 381#endif /* _PLATFORM_DEVICE_H_ */