blob: bdc35753ef7c1cf8488ef1b3079114a600fa8da1 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Russell Kingbbbf5082005-10-29 22:17:58 +01002/*
3 * platform_device.h - generic, centralized driver model
4 *
5 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6 *
Mauro Carvalho Chehabfe34c892019-06-18 12:34:59 -03007 * See Documentation/driver-api/driver-model/ for more information.
Russell Kingbbbf5082005-10-29 22:17:58 +01008 */
9
10#ifndef _PLATFORM_DEVICE_H_
11#define _PLATFORM_DEVICE_H_
12
13#include <linux/device.h>
14
Jean Delvare689ae232012-07-27 22:14:59 +020015#define PLATFORM_DEVID_NONE (-1)
16#define PLATFORM_DEVID_AUTO (-2)
17
Samuel Ortize710d7d2011-04-08 00:43:01 +020018struct mfd_cell;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030019struct property_entry;
Randy Dunlapac316722018-06-19 22:47:28 -070020struct platform_device_id;
Samuel Ortize710d7d2011-04-08 00:43:01 +020021
Russell Kingbbbf5082005-10-29 22:17:58 +010022struct platform_device {
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010023 const char *name;
Jean Delvare1359555e2007-09-09 12:54:16 +020024 int id;
Jean Delvare689ae232012-07-27 22:14:59 +020025 bool id_auto;
Russell Kingbbbf5082005-10-29 22:17:58 +010026 struct device dev;
Christoph Hellwige3a36eb2020-03-11 17:07:10 +010027 u64 platform_dma_mask;
Russell Kingbbbf5082005-10-29 22:17:58 +010028 u32 num_resources;
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010029 struct resource *resource;
Eric Miao57fee4a2009-02-04 11:52:40 +080030
Eric Miao3d03ba42010-01-01 15:43:28 +080031 const struct platform_device_id *id_entry;
Kim Phillips3d713e02014-06-02 19:42:58 -050032 char *driver_override; /* Driver name to force a match */
Magnus Dammd7aacad2009-07-08 13:21:31 +020033
Samuel Ortize710d7d2011-04-08 00:43:01 +020034 /* MFD cell pointer */
35 struct mfd_cell *mfd_cell;
36
Magnus Dammd7aacad2009-07-08 13:21:31 +020037 /* arch specific additions */
38 struct pdev_archdata archdata;
Russell Kingbbbf5082005-10-29 22:17:58 +010039};
40
Eric Miao57fee4a2009-02-04 11:52:40 +080041#define platform_get_device_id(pdev) ((pdev)->id_entry)
42
Andy Shevchenko719cf712018-08-20 16:45:41 +030043#define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
Russell Kingbbbf5082005-10-29 22:17:58 +010044#define to_platform_device(x) container_of((x), struct platform_device, dev)
45
46extern int platform_device_register(struct platform_device *);
47extern void platform_device_unregister(struct platform_device *);
48
49extern struct bus_type platform_bus_type;
50extern struct device platform_bus;
51
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010052extern struct resource *platform_get_resource(struct platform_device *,
53 unsigned int, unsigned int);
Suzuki K Poulose36f33132019-07-23 23:18:38 +010054extern struct device *
55platform_find_device_by_driver(struct device *start,
56 const struct device_driver *drv);
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000057extern void __iomem *
Dejin Zheng890cc392020-03-24 00:06:08 +080058devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
59 unsigned int index, struct resource **res);
60extern void __iomem *
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000061devm_platform_ioremap_resource(struct platform_device *pdev,
62 unsigned int index);
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020063extern void __iomem *
64devm_platform_ioremap_resource_wc(struct platform_device *pdev,
65 unsigned int index);
Bartosz Golaszewskic9c86412019-10-22 10:43:16 +020066extern void __iomem *
67devm_platform_ioremap_resource_byname(struct platform_device *pdev,
68 const char *name);
Russell Kingbbbf5082005-10-29 22:17:58 +010069extern int platform_get_irq(struct platform_device *, unsigned int);
Thierry Reding8973ea42019-08-28 10:34:10 +020070extern int platform_get_irq_optional(struct platform_device *, unsigned int);
Stephen Boyd4b835552016-01-06 17:12:47 -080071extern int platform_irq_count(struct platform_device *);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010072extern struct resource *platform_get_resource_byname(struct platform_device *,
73 unsigned int,
74 const char *);
Linus Walleijc0afe7b2009-04-27 02:38:16 +020075extern int platform_get_irq_byname(struct platform_device *, const char *);
Hans de Goedef1da5672019-10-05 23:04:47 +020076extern int platform_get_irq_byname_optional(struct platform_device *dev,
77 const char *name);
Russell Kingbbbf5082005-10-29 22:17:58 +010078extern int platform_add_devices(struct platform_device **, int);
79
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020080struct platform_device_info {
81 struct device *parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +010082 struct fwnode_handle *fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +000083 bool of_node_reused;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020084
85 const char *name;
86 int id;
87
88 const struct resource *res;
89 unsigned int num_res;
90
91 const void *data;
92 size_t size_data;
93 u64 dma_mask;
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020094
Tomas Winkler469e1902020-02-08 20:44:07 +020095 const struct property_entry *properties;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020096};
97extern struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +010098 const struct platform_device_info *pdevinfo);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020099
100/**
101 * platform_device_register_resndata - add a platform-level device with
102 * resources and platform-specific data
103 *
104 * @parent: parent device for the device we're adding
105 * @name: base name of the device we're adding
106 * @id: instance id
107 * @res: set of resources that needs to be allocated for the device
108 * @num: number of resources
109 * @data: platform specific data for this platform device
110 * @size: size of platform specific data
111 *
112 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
113 */
114static inline struct platform_device *platform_device_register_resndata(
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200115 struct device *parent, const char *name, int id,
116 const struct resource *res, unsigned int num,
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200117 const void *data, size_t size) {
118
119 struct platform_device_info pdevinfo = {
120 .parent = parent,
121 .name = name,
122 .id = id,
123 .res = res,
124 .num_res = num,
125 .data = data,
126 .size_data = size,
127 .dma_mask = 0,
128 };
129
130 return platform_device_register_full(&pdevinfo);
131}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200132
133/**
134 * platform_device_register_simple - add a platform-level device and its resources
135 * @name: base name of the device we're adding
136 * @id: instance id
137 * @res: set of resources that needs to be allocated for the device
138 * @num: number of resources
139 *
140 * This function creates a simple platform device that requires minimal
141 * resource and memory management. Canned release function freeing memory
142 * allocated for the device allows drivers using such devices to be
143 * unloaded without waiting for the last reference to the device to be
144 * dropped.
145 *
146 * This interface is primarily intended for use with legacy drivers which
147 * probe hardware directly. Because such drivers create sysfs device nodes
148 * themselves, rather than letting system infrastructure handle such device
149 * enumeration tasks, they don't fully conform to the Linux driver model.
150 * In particular, when such drivers are built as modules, they can't be
151 * "hotplugged".
152 *
153 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
154 */
155static inline struct platform_device *platform_device_register_simple(
156 const char *name, int id,
157 const struct resource *res, unsigned int num)
158{
159 return platform_device_register_resndata(NULL, name, id,
160 res, num, NULL, 0);
161}
162
163/**
164 * platform_device_register_data - add a platform-level device with platform-specific data
165 * @parent: parent device for the device we're adding
166 * @name: base name of the device we're adding
167 * @id: instance id
168 * @data: platform specific data for this platform device
169 * @size: size of platform specific data
170 *
171 * This function creates a simple platform device that requires minimal
172 * resource and memory management. Canned release function freeing memory
173 * allocated for the device allows drivers using such devices to be
174 * unloaded without waiting for the last reference to the device to be
175 * dropped.
176 *
177 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
178 */
179static inline struct platform_device *platform_device_register_data(
180 struct device *parent, const char *name, int id,
181 const void *data, size_t size)
182{
183 return platform_device_register_resndata(parent, name, id,
184 NULL, 0, data, size);
185}
Russell Kingbbbf5082005-10-29 22:17:58 +0100186
Jean Delvare1359555e2007-09-09 12:54:16 +0200187extern struct platform_device *platform_device_alloc(const char *name, int id);
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100188extern int platform_device_add_resources(struct platform_device *pdev,
189 const struct resource *res,
190 unsigned int num);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100191extern int platform_device_add_data(struct platform_device *pdev,
192 const void *data, size_t size);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200193extern int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200194 const struct property_entry *properties);
Russell King37c12e72005-11-05 21:19:33 +0000195extern int platform_device_add(struct platform_device *pdev);
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500196extern void platform_device_del(struct platform_device *pdev);
Russell King37c12e72005-11-05 21:19:33 +0000197extern void platform_device_put(struct platform_device *pdev);
198
Russell King00d3dcd2005-11-09 17:23:39 +0000199struct platform_driver {
200 int (*probe)(struct platform_device *);
201 int (*remove)(struct platform_device *);
202 void (*shutdown)(struct platform_device *);
203 int (*suspend)(struct platform_device *, pm_message_t state);
204 int (*resume)(struct platform_device *);
205 struct device_driver driver;
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100206 const struct platform_device_id *id_table;
Johan Hovold3f9120b2013-09-23 16:27:26 +0200207 bool prevent_deferred_probe;
Russell King00d3dcd2005-11-09 17:23:39 +0000208};
209
Rob Herring10dbc5e2013-04-21 16:38:31 -0500210#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
211 driver))
212
Libo Chen94470572013-05-25 12:40:50 +0800213/*
214 * use a macro to avoid include chaining to get THIS_MODULE
215 */
216#define platform_driver_register(drv) \
217 __platform_driver_register(drv, THIS_MODULE)
218extern int __platform_driver_register(struct platform_driver *,
219 struct module *);
Russell King00d3dcd2005-11-09 17:23:39 +0000220extern void platform_driver_unregister(struct platform_driver *);
221
David Brownellc67334f2006-11-16 23:28:47 -0800222/* non-hotpluggable platform devices may use this so that probe() and
223 * its support may live in __init sections, conserving runtime memory.
224 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100225#define platform_driver_probe(drv, probe) \
226 __platform_driver_probe(drv, probe, THIS_MODULE)
227extern int __platform_driver_probe(struct platform_driver *driver,
228 int (*probe)(struct platform_device *), struct module *module);
David Brownellc67334f2006-11-16 23:28:47 -0800229
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100230static inline void *platform_get_drvdata(const struct platform_device *pdev)
231{
232 return dev_get_drvdata(&pdev->dev);
233}
234
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100235static inline void platform_set_drvdata(struct platform_device *pdev,
236 void *data)
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100237{
238 dev_set_drvdata(&pdev->dev, data);
239}
Russell King00d3dcd2005-11-09 17:23:39 +0000240
Grant Likely940ab882011-10-05 11:29:49 -0600241/* module_platform_driver() - Helper macro for drivers that don't do
242 * anything special in module init/exit. This eliminates a lot of
243 * boilerplate. Each module may only use this macro once, and
244 * calling it replaces module_init() and module_exit()
245 */
246#define module_platform_driver(__platform_driver) \
Lars-Peter Clausen907d0ed2011-11-16 10:13:35 +0100247 module_driver(__platform_driver, platform_driver_register, \
248 platform_driver_unregister)
Grant Likely940ab882011-10-05 11:29:49 -0600249
Paul Gortmakerf309d442015-05-01 20:10:57 -0400250/* builtin_platform_driver() - Helper macro for builtin drivers that
251 * don't do anything special in driver init. This eliminates some
252 * boilerplate. Each driver may only use this macro once, and
253 * calling it replaces device_initcall(). Note this is meant to be
254 * a parallel of module_platform_driver() above, but w/o _exit stuff.
255 */
256#define builtin_platform_driver(__platform_driver) \
257 builtin_driver(__platform_driver, platform_driver_register)
258
Fabio Porceddabab734f2013-01-09 12:15:26 +0100259/* module_platform_driver_probe() - Helper macro for drivers that don't do
260 * anything special in module init/exit. This eliminates a lot of
261 * boilerplate. Each module may only use this macro once, and
262 * calling it replaces module_init() and module_exit()
263 */
264#define module_platform_driver_probe(__platform_driver, __platform_probe) \
265static int __init __platform_driver##_init(void) \
266{ \
267 return platform_driver_probe(&(__platform_driver), \
268 __platform_probe); \
269} \
270module_init(__platform_driver##_init); \
271static void __exit __platform_driver##_exit(void) \
272{ \
273 platform_driver_unregister(&(__platform_driver)); \
274} \
275module_exit(__platform_driver##_exit);
276
Paul Gortmakerf309d442015-05-01 20:10:57 -0400277/* builtin_platform_driver_probe() - Helper macro for drivers that don't do
278 * anything special in device init. This eliminates some boilerplate. Each
279 * driver may only use this macro once, and using it replaces device_initcall.
280 * This is meant to be a parallel of module_platform_driver_probe above, but
281 * without the __exit parts.
282 */
283#define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
284static int __init __platform_driver##_init(void) \
285{ \
286 return platform_driver_probe(&(__platform_driver), \
287 __platform_probe); \
288} \
289device_initcall(__platform_driver##_init); \
290
Wolfram Sang291f6532014-10-28 17:40:42 +0100291#define platform_create_bundle(driver, probe, res, n_res, data, size) \
292 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
293extern struct platform_device *__platform_create_bundle(
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100294 struct platform_driver *driver, int (*probe)(struct platform_device *),
295 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100296 const void *data, size_t size, struct module *module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800297
Thierry Redingdbe22562015-09-25 17:29:04 +0200298int __platform_register_drivers(struct platform_driver * const *drivers,
299 unsigned int count, struct module *owner);
300void platform_unregister_drivers(struct platform_driver * const *drivers,
301 unsigned int count);
302
303#define platform_register_drivers(drivers, count) \
304 __platform_register_drivers(drivers, count, THIS_MODULE)
305
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200306#ifdef CONFIG_SUSPEND
307extern int platform_pm_suspend(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200308extern int platform_pm_resume(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200309#else
310#define platform_pm_suspend NULL
311#define platform_pm_resume NULL
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200312#endif
313
314#ifdef CONFIG_HIBERNATE_CALLBACKS
315extern int platform_pm_freeze(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200316extern int platform_pm_thaw(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200317extern int platform_pm_poweroff(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200318extern int platform_pm_restore(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200319#else
320#define platform_pm_freeze NULL
321#define platform_pm_thaw NULL
322#define platform_pm_poweroff NULL
323#define platform_pm_restore NULL
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200324#endif
325
Nipun Gupta07397df2018-04-28 08:21:58 +0530326extern int platform_dma_configure(struct device *dev);
327
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200328#ifdef CONFIG_PM_SLEEP
329#define USE_PLATFORM_PM_SLEEP_OPS \
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200330 .suspend = platform_pm_suspend, \
331 .resume = platform_pm_resume, \
332 .freeze = platform_pm_freeze, \
333 .thaw = platform_pm_thaw, \
334 .poweroff = platform_pm_poweroff, \
Rafael J. Wysocki9b39e732011-12-18 00:34:24 +0100335 .restore = platform_pm_restore,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200336#else
337#define USE_PLATFORM_PM_SLEEP_OPS
338#endif
339
Bartosz Golaszewski507fd012019-10-03 11:29:12 +0200340#ifndef CONFIG_SUPERH
341/*
342 * REVISIT: This stub is needed for all non-SuperH users of early platform
343 * drivers. It should go away once we introduce the new platform_device-based
344 * early driver framework.
345 */
Bartosz Golaszewski201e9102019-10-03 11:29:13 +0200346static inline int is_sh_early_platform_device(struct platform_device *pdev)
Bartosz Golaszewski507fd012019-10-03 11:29:12 +0200347{
348 return 0;
349}
350#endif /* CONFIG_SUPERH */
351
Russell Kingbbbf5082005-10-29 22:17:58 +0100352#endif /* _PLATFORM_DEVICE_H_ */