blob: 77a2aada106dcb016833d77cc3b0842c35a4db4b [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;
Ulf Hansson9495b7e2020-04-22 12:09:54 +020028 struct device_dma_parameters dma_parms;
Russell Kingbbbf5082005-10-29 22:17:58 +010029 u32 num_resources;
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010030 struct resource *resource;
Eric Miao57fee4a2009-02-04 11:52:40 +080031
Eric Miao3d03ba42010-01-01 15:43:28 +080032 const struct platform_device_id *id_entry;
Kim Phillips3d713e02014-06-02 19:42:58 -050033 char *driver_override; /* Driver name to force a match */
Magnus Dammd7aacad2009-07-08 13:21:31 +020034
Samuel Ortize710d7d2011-04-08 00:43:01 +020035 /* MFD cell pointer */
36 struct mfd_cell *mfd_cell;
37
Magnus Dammd7aacad2009-07-08 13:21:31 +020038 /* arch specific additions */
39 struct pdev_archdata archdata;
Russell Kingbbbf5082005-10-29 22:17:58 +010040};
41
Eric Miao57fee4a2009-02-04 11:52:40 +080042#define platform_get_device_id(pdev) ((pdev)->id_entry)
43
Andy Shevchenko719cf712018-08-20 16:45:41 +030044#define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
Russell Kingbbbf5082005-10-29 22:17:58 +010045#define to_platform_device(x) container_of((x), struct platform_device, dev)
46
47extern int platform_device_register(struct platform_device *);
48extern void platform_device_unregister(struct platform_device *);
49
50extern struct bus_type platform_bus_type;
51extern struct device platform_bus;
52
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010053extern struct resource *platform_get_resource(struct platform_device *,
54 unsigned int, unsigned int);
Suzuki K Poulose36f33132019-07-23 23:18:38 +010055extern struct device *
56platform_find_device_by_driver(struct device *start,
57 const struct device_driver *drv);
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000058extern void __iomem *
Dejin Zheng890cc392020-03-24 00:06:08 +080059devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
60 unsigned int index, struct resource **res);
61extern void __iomem *
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000062devm_platform_ioremap_resource(struct platform_device *pdev,
63 unsigned int index);
Bartosz Golaszewskibb6243b2019-10-22 10:43:14 +020064extern void __iomem *
65devm_platform_ioremap_resource_wc(struct platform_device *pdev,
66 unsigned int index);
Bartosz Golaszewskic9c86412019-10-22 10:43:16 +020067extern void __iomem *
68devm_platform_ioremap_resource_byname(struct platform_device *pdev,
69 const char *name);
Russell Kingbbbf5082005-10-29 22:17:58 +010070extern int platform_get_irq(struct platform_device *, unsigned int);
Thierry Reding8973ea42019-08-28 10:34:10 +020071extern int platform_get_irq_optional(struct platform_device *, unsigned int);
Stephen Boyd4b835552016-01-06 17:12:47 -080072extern int platform_irq_count(struct platform_device *);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010073extern struct resource *platform_get_resource_byname(struct platform_device *,
74 unsigned int,
75 const char *);
Linus Walleijc0afe7b2009-04-27 02:38:16 +020076extern int platform_get_irq_byname(struct platform_device *, const char *);
Hans de Goedef1da5672019-10-05 23:04:47 +020077extern int platform_get_irq_byname_optional(struct platform_device *dev,
78 const char *name);
Russell Kingbbbf5082005-10-29 22:17:58 +010079extern int platform_add_devices(struct platform_device **, int);
80
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020081struct platform_device_info {
82 struct device *parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +010083 struct fwnode_handle *fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +000084 bool of_node_reused;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020085
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;
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020095
Tomas Winkler469e1902020-02-08 20:44:07 +020096 const struct property_entry *properties;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020097};
98extern struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +010099 const struct platform_device_info *pdevinfo);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200100
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 */
115static inline struct platform_device *platform_device_register_resndata(
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200116 struct device *parent, const char *name, int id,
117 const struct resource *res, unsigned int num,
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200118 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}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200133
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 */
156static 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 */
180static 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}
Russell Kingbbbf5082005-10-29 22:17:58 +0100187
Jean Delvare1359555e2007-09-09 12:54:16 +0200188extern struct platform_device *platform_device_alloc(const char *name, int id);
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100189extern int platform_device_add_resources(struct platform_device *pdev,
190 const struct resource *res,
191 unsigned int num);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100192extern int platform_device_add_data(struct platform_device *pdev,
193 const void *data, size_t size);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200194extern int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200195 const struct property_entry *properties);
Russell King37c12e72005-11-05 21:19:33 +0000196extern int platform_device_add(struct platform_device *pdev);
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500197extern void platform_device_del(struct platform_device *pdev);
Russell King37c12e72005-11-05 21:19:33 +0000198extern void platform_device_put(struct platform_device *pdev);
199
Russell King00d3dcd2005-11-09 17:23:39 +0000200struct 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;
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100207 const struct platform_device_id *id_table;
Johan Hovold3f9120b2013-09-23 16:27:26 +0200208 bool prevent_deferred_probe;
Russell King00d3dcd2005-11-09 17:23:39 +0000209};
210
Rob Herring10dbc5e2013-04-21 16:38:31 -0500211#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
212 driver))
213
Libo Chen94470572013-05-25 12:40:50 +0800214/*
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)
219extern int __platform_driver_register(struct platform_driver *,
220 struct module *);
Russell King00d3dcd2005-11-09 17:23:39 +0000221extern void platform_driver_unregister(struct platform_driver *);
222
David Brownellc67334f2006-11-16 23:28:47 -0800223/* non-hotpluggable platform devices may use this so that probe() and
224 * its support may live in __init sections, conserving runtime memory.
225 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100226#define platform_driver_probe(drv, probe) \
227 __platform_driver_probe(drv, probe, THIS_MODULE)
228extern int __platform_driver_probe(struct platform_driver *driver,
229 int (*probe)(struct platform_device *), struct module *module);
David Brownellc67334f2006-11-16 23:28:47 -0800230
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100231static inline void *platform_get_drvdata(const struct platform_device *pdev)
232{
233 return dev_get_drvdata(&pdev->dev);
234}
235
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100236static inline void platform_set_drvdata(struct platform_device *pdev,
237 void *data)
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100238{
239 dev_set_drvdata(&pdev->dev, data);
240}
Russell King00d3dcd2005-11-09 17:23:39 +0000241
Grant Likely940ab882011-10-05 11:29:49 -0600242/* 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) \
Lars-Peter Clausen907d0ed2011-11-16 10:13:35 +0100248 module_driver(__platform_driver, platform_driver_register, \
249 platform_driver_unregister)
Grant Likely940ab882011-10-05 11:29:49 -0600250
Paul Gortmakerf309d442015-05-01 20:10:57 -0400251/* 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
Fabio Porceddabab734f2013-01-09 12:15:26 +0100260/* 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) \
266static int __init __platform_driver##_init(void) \
267{ \
268 return platform_driver_probe(&(__platform_driver), \
269 __platform_probe); \
270} \
271module_init(__platform_driver##_init); \
272static void __exit __platform_driver##_exit(void) \
273{ \
274 platform_driver_unregister(&(__platform_driver)); \
275} \
276module_exit(__platform_driver##_exit);
277
Paul Gortmakerf309d442015-05-01 20:10:57 -0400278/* 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) \
285static int __init __platform_driver##_init(void) \
286{ \
287 return platform_driver_probe(&(__platform_driver), \
288 __platform_probe); \
289} \
290device_initcall(__platform_driver##_init); \
291
Wolfram Sang291f6532014-10-28 17:40:42 +0100292#define platform_create_bundle(driver, probe, res, n_res, data, size) \
293 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
294extern struct platform_device *__platform_create_bundle(
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100295 struct platform_driver *driver, int (*probe)(struct platform_device *),
296 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100297 const void *data, size_t size, struct module *module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800298
Thierry Redingdbe22562015-09-25 17:29:04 +0200299int __platform_register_drivers(struct platform_driver * const *drivers,
300 unsigned int count, struct module *owner);
301void 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
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200307#ifdef CONFIG_SUSPEND
308extern int platform_pm_suspend(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200309extern int platform_pm_resume(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200310#else
311#define platform_pm_suspend NULL
312#define platform_pm_resume NULL
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200313#endif
314
315#ifdef CONFIG_HIBERNATE_CALLBACKS
316extern int platform_pm_freeze(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200317extern int platform_pm_thaw(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200318extern int platform_pm_poweroff(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200319extern int platform_pm_restore(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200320#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
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200325#endif
326
Nipun Gupta07397df2018-04-28 08:21:58 +0530327extern int platform_dma_configure(struct device *dev);
328
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200329#ifdef CONFIG_PM_SLEEP
330#define USE_PLATFORM_PM_SLEEP_OPS \
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200331 .suspend = platform_pm_suspend, \
332 .resume = platform_pm_resume, \
333 .freeze = platform_pm_freeze, \
334 .thaw = platform_pm_thaw, \
335 .poweroff = platform_pm_poweroff, \
Rafael J. Wysocki9b39e732011-12-18 00:34:24 +0100336 .restore = platform_pm_restore,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200337#else
338#define USE_PLATFORM_PM_SLEEP_OPS
339#endif
340
Bartosz Golaszewski507fd012019-10-03 11:29:12 +0200341#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 */
Bartosz Golaszewski201e9102019-10-03 11:29:13 +0200347static inline int is_sh_early_platform_device(struct platform_device *pdev)
Bartosz Golaszewski507fd012019-10-03 11:29:12 +0200348{
349 return 0;
350}
351#endif /* CONFIG_SUPERH */
352
Russell Kingbbbf5082005-10-29 22:17:58 +0100353#endif /* _PLATFORM_DEVICE_H_ */