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