blob: 466a8d02e29801abd74162cd3cff212381045838 [file] [log] [blame]
Russell Kingbbbf5082005-10-29 22:17:58 +01001/*
2 * platform_device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 *
6 * This file is released under the GPLv2
7 *
8 * See Documentation/driver-model/ for more information.
9 */
10
11#ifndef _PLATFORM_DEVICE_H_
12#define _PLATFORM_DEVICE_H_
13
14#include <linux/device.h>
15
Jean Delvare689ae232012-07-27 22:14:59 +020016#define PLATFORM_DEVID_NONE (-1)
17#define PLATFORM_DEVID_AUTO (-2)
18
Samuel Ortize710d7d2011-04-08 00:43:01 +020019struct mfd_cell;
Heikki Krogerusf4d05262016-03-29 14:52:23 +030020struct property_entry;
Randy Dunlapac316722018-06-19 22:47:28 -070021struct platform_device_id;
Samuel Ortize710d7d2011-04-08 00:43:01 +020022
Russell Kingbbbf5082005-10-29 22:17:58 +010023struct platform_device {
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010024 const char *name;
Jean Delvare1359555e2007-09-09 12:54:16 +020025 int id;
Jean Delvare689ae232012-07-27 22:14:59 +020026 bool id_auto;
Russell Kingbbbf5082005-10-29 22:17:58 +010027 struct device dev;
28 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
Kumar Galaa77ce812011-06-10 01:52:57 -050052extern void arch_setup_pdev_archdata(struct platform_device *);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010053extern struct resource *platform_get_resource(struct platform_device *,
54 unsigned int, unsigned int);
Russell Kingbbbf5082005-10-29 22:17:58 +010055extern int platform_get_irq(struct platform_device *, unsigned int);
Stephen Boyd4b835552016-01-06 17:12:47 -080056extern int platform_irq_count(struct platform_device *);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +010057extern struct resource *platform_get_resource_byname(struct platform_device *,
58 unsigned int,
59 const char *);
Linus Walleijc0afe7b2009-04-27 02:38:16 +020060extern int platform_get_irq_byname(struct platform_device *, const char *);
Russell Kingbbbf5082005-10-29 22:17:58 +010061extern int platform_add_devices(struct platform_device **, int);
62
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020063struct platform_device_info {
64 struct device *parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +010065 struct fwnode_handle *fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +000066 bool of_node_reused;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020067
68 const char *name;
69 int id;
70
71 const struct resource *res;
72 unsigned int num_res;
73
74 const void *data;
75 size_t size_data;
76 u64 dma_mask;
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020077
Heikki Krogerusf4d05262016-03-29 14:52:23 +030078 struct property_entry *properties;
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020079};
80extern struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +010081 const struct platform_device_info *pdevinfo);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +020082
83/**
84 * platform_device_register_resndata - add a platform-level device with
85 * resources and platform-specific data
86 *
87 * @parent: parent device for the device we're adding
88 * @name: base name of the device we're adding
89 * @id: instance id
90 * @res: set of resources that needs to be allocated for the device
91 * @num: number of resources
92 * @data: platform specific data for this platform device
93 * @size: size of platform specific data
94 *
95 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
96 */
97static inline struct platform_device *platform_device_register_resndata(
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +020098 struct device *parent, const char *name, int id,
99 const struct resource *res, unsigned int num,
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200100 const void *data, size_t size) {
101
102 struct platform_device_info pdevinfo = {
103 .parent = parent,
104 .name = name,
105 .id = id,
106 .res = res,
107 .num_res = num,
108 .data = data,
109 .size_data = size,
110 .dma_mask = 0,
111 };
112
113 return platform_device_register_full(&pdevinfo);
114}
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200115
116/**
117 * platform_device_register_simple - add a platform-level device and its resources
118 * @name: base name of the device we're adding
119 * @id: instance id
120 * @res: set of resources that needs to be allocated for the device
121 * @num: number of resources
122 *
123 * This function creates a simple platform device that requires minimal
124 * resource and memory management. Canned release function freeing memory
125 * allocated for the device allows drivers using such devices to be
126 * unloaded without waiting for the last reference to the device to be
127 * dropped.
128 *
129 * This interface is primarily intended for use with legacy drivers which
130 * probe hardware directly. Because such drivers create sysfs device nodes
131 * themselves, rather than letting system infrastructure handle such device
132 * enumeration tasks, they don't fully conform to the Linux driver model.
133 * In particular, when such drivers are built as modules, they can't be
134 * "hotplugged".
135 *
136 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
137 */
138static inline struct platform_device *platform_device_register_simple(
139 const char *name, int id,
140 const struct resource *res, unsigned int num)
141{
142 return platform_device_register_resndata(NULL, name, id,
143 res, num, NULL, 0);
144}
145
146/**
147 * platform_device_register_data - add a platform-level device with platform-specific data
148 * @parent: parent device for the device we're adding
149 * @name: base name of the device we're adding
150 * @id: instance id
151 * @data: platform specific data for this platform device
152 * @size: size of platform specific data
153 *
154 * This function creates a simple platform device that requires minimal
155 * resource and memory management. Canned release function freeing memory
156 * allocated for the device allows drivers using such devices to be
157 * unloaded without waiting for the last reference to the device to be
158 * dropped.
159 *
160 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
161 */
162static inline struct platform_device *platform_device_register_data(
163 struct device *parent, const char *name, int id,
164 const void *data, size_t size)
165{
166 return platform_device_register_resndata(parent, name, id,
167 NULL, 0, data, size);
168}
Russell Kingbbbf5082005-10-29 22:17:58 +0100169
Jean Delvare1359555e2007-09-09 12:54:16 +0200170extern struct platform_device *platform_device_alloc(const char *name, int id);
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100171extern int platform_device_add_resources(struct platform_device *pdev,
172 const struct resource *res,
173 unsigned int num);
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100174extern int platform_device_add_data(struct platform_device *pdev,
175 const void *data, size_t size);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200176extern int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200177 const struct property_entry *properties);
Russell King37c12e72005-11-05 21:19:33 +0000178extern int platform_device_add(struct platform_device *pdev);
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500179extern void platform_device_del(struct platform_device *pdev);
Russell King37c12e72005-11-05 21:19:33 +0000180extern void platform_device_put(struct platform_device *pdev);
181
Russell King00d3dcd2005-11-09 17:23:39 +0000182struct platform_driver {
183 int (*probe)(struct platform_device *);
184 int (*remove)(struct platform_device *);
185 void (*shutdown)(struct platform_device *);
186 int (*suspend)(struct platform_device *, pm_message_t state);
187 int (*resume)(struct platform_device *);
188 struct device_driver driver;
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100189 const struct platform_device_id *id_table;
Johan Hovold3f9120b2013-09-23 16:27:26 +0200190 bool prevent_deferred_probe;
Russell King00d3dcd2005-11-09 17:23:39 +0000191};
192
Rob Herring10dbc5e2013-04-21 16:38:31 -0500193#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
194 driver))
195
Libo Chen94470572013-05-25 12:40:50 +0800196/*
197 * use a macro to avoid include chaining to get THIS_MODULE
198 */
199#define platform_driver_register(drv) \
200 __platform_driver_register(drv, THIS_MODULE)
201extern int __platform_driver_register(struct platform_driver *,
202 struct module *);
Russell King00d3dcd2005-11-09 17:23:39 +0000203extern void platform_driver_unregister(struct platform_driver *);
204
David Brownellc67334f2006-11-16 23:28:47 -0800205/* non-hotpluggable platform devices may use this so that probe() and
206 * its support may live in __init sections, conserving runtime memory.
207 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100208#define platform_driver_probe(drv, probe) \
209 __platform_driver_probe(drv, probe, THIS_MODULE)
210extern int __platform_driver_probe(struct platform_driver *driver,
211 int (*probe)(struct platform_device *), struct module *module);
David Brownellc67334f2006-11-16 23:28:47 -0800212
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100213static inline void *platform_get_drvdata(const struct platform_device *pdev)
214{
215 return dev_get_drvdata(&pdev->dev);
216}
217
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100218static inline void platform_set_drvdata(struct platform_device *pdev,
219 void *data)
Marc Kleine-Budde71d64292011-02-16 23:23:27 +0100220{
221 dev_set_drvdata(&pdev->dev, data);
222}
Russell King00d3dcd2005-11-09 17:23:39 +0000223
Grant Likely940ab882011-10-05 11:29:49 -0600224/* module_platform_driver() - Helper macro for drivers that don't do
225 * anything special in module init/exit. This eliminates a lot of
226 * boilerplate. Each module may only use this macro once, and
227 * calling it replaces module_init() and module_exit()
228 */
229#define module_platform_driver(__platform_driver) \
Lars-Peter Clausen907d0ed2011-11-16 10:13:35 +0100230 module_driver(__platform_driver, platform_driver_register, \
231 platform_driver_unregister)
Grant Likely940ab882011-10-05 11:29:49 -0600232
Paul Gortmakerf309d442015-05-01 20:10:57 -0400233/* builtin_platform_driver() - Helper macro for builtin drivers that
234 * don't do anything special in driver init. This eliminates some
235 * boilerplate. Each driver may only use this macro once, and
236 * calling it replaces device_initcall(). Note this is meant to be
237 * a parallel of module_platform_driver() above, but w/o _exit stuff.
238 */
239#define builtin_platform_driver(__platform_driver) \
240 builtin_driver(__platform_driver, platform_driver_register)
241
Fabio Porceddabab734f2013-01-09 12:15:26 +0100242/* module_platform_driver_probe() - 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_probe(__platform_driver, __platform_probe) \
248static int __init __platform_driver##_init(void) \
249{ \
250 return platform_driver_probe(&(__platform_driver), \
251 __platform_probe); \
252} \
253module_init(__platform_driver##_init); \
254static void __exit __platform_driver##_exit(void) \
255{ \
256 platform_driver_unregister(&(__platform_driver)); \
257} \
258module_exit(__platform_driver##_exit);
259
Paul Gortmakerf309d442015-05-01 20:10:57 -0400260/* builtin_platform_driver_probe() - Helper macro for drivers that don't do
261 * anything special in device init. This eliminates some boilerplate. Each
262 * driver may only use this macro once, and using it replaces device_initcall.
263 * This is meant to be a parallel of module_platform_driver_probe above, but
264 * without the __exit parts.
265 */
266#define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
267static int __init __platform_driver##_init(void) \
268{ \
269 return platform_driver_probe(&(__platform_driver), \
270 __platform_probe); \
271} \
272device_initcall(__platform_driver##_init); \
273
Wolfram Sang291f6532014-10-28 17:40:42 +0100274#define platform_create_bundle(driver, probe, res, n_res, data, size) \
275 __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
276extern struct platform_device *__platform_create_bundle(
Fabio Porcedda6ae07f22013-03-26 10:35:17 +0100277 struct platform_driver *driver, int (*probe)(struct platform_device *),
278 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100279 const void *data, size_t size, struct module *module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800280
Thierry Redingdbe22562015-09-25 17:29:04 +0200281int __platform_register_drivers(struct platform_driver * const *drivers,
282 unsigned int count, struct module *owner);
283void platform_unregister_drivers(struct platform_driver * const *drivers,
284 unsigned int count);
285
286#define platform_register_drivers(drivers, count) \
287 __platform_register_drivers(drivers, count, THIS_MODULE)
288
Magnus Damm13977092009-03-30 14:37:25 -0700289/* early platform driver interface */
290struct early_platform_driver {
291 const char *class_str;
292 struct platform_driver *pdrv;
293 struct list_head list;
294 int requested_id;
Magnus Dammc60e0502009-11-27 17:38:51 +0900295 char *buffer;
296 int bufsize;
Magnus Damm13977092009-03-30 14:37:25 -0700297};
298
299#define EARLY_PLATFORM_ID_UNSET -2
300#define EARLY_PLATFORM_ID_ERROR -3
301
302extern int early_platform_driver_register(struct early_platform_driver *epdrv,
303 char *buf);
304extern void early_platform_add_devices(struct platform_device **devs, int num);
305
306static inline int is_early_platform_device(struct platform_device *pdev)
307{
308 return !pdev->dev.driver;
309}
310
311extern void early_platform_driver_register_all(char *class_str);
312extern int early_platform_driver_probe(char *class_str,
313 int nr_probe, int user_only);
314extern void early_platform_cleanup(void);
315
Magnus Dammc60e0502009-11-27 17:38:51 +0900316#define early_platform_init(class_string, platdrv) \
317 early_platform_init_buffer(class_string, platdrv, NULL, 0)
Magnus Damm13977092009-03-30 14:37:25 -0700318
319#ifndef MODULE
Magnus Dammc60e0502009-11-27 17:38:51 +0900320#define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
Magnus Damm13977092009-03-30 14:37:25 -0700321static __initdata struct early_platform_driver early_driver = { \
322 .class_str = class_string, \
Magnus Dammc60e0502009-11-27 17:38:51 +0900323 .buffer = buf, \
324 .bufsize = bufsiz, \
325 .pdrv = platdrv, \
Magnus Damm13977092009-03-30 14:37:25 -0700326 .requested_id = EARLY_PLATFORM_ID_UNSET, \
327}; \
Magnus Dammc60e0502009-11-27 17:38:51 +0900328static int __init early_platform_driver_setup_func(char *buffer) \
Magnus Damm13977092009-03-30 14:37:25 -0700329{ \
Magnus Dammc60e0502009-11-27 17:38:51 +0900330 return early_platform_driver_register(&early_driver, buffer); \
Magnus Damm13977092009-03-30 14:37:25 -0700331} \
332early_param(class_string, early_platform_driver_setup_func)
333#else /* MODULE */
Magnus Dammc60e0502009-11-27 17:38:51 +0900334#define early_platform_init_buffer(class_string, platdrv, buf, bufsiz) \
335static inline char *early_platform_driver_setup_func(void) \
336{ \
337 return bufsiz ? buf : NULL; \
338}
Magnus Damm13977092009-03-30 14:37:25 -0700339#endif /* MODULE */
340
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200341#ifdef CONFIG_SUSPEND
342extern int platform_pm_suspend(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200343extern int platform_pm_resume(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200344#else
345#define platform_pm_suspend NULL
346#define platform_pm_resume NULL
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200347#endif
348
349#ifdef CONFIG_HIBERNATE_CALLBACKS
350extern int platform_pm_freeze(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200351extern int platform_pm_thaw(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200352extern int platform_pm_poweroff(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200353extern int platform_pm_restore(struct device *dev);
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200354#else
355#define platform_pm_freeze NULL
356#define platform_pm_thaw NULL
357#define platform_pm_poweroff NULL
358#define platform_pm_restore NULL
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200359#endif
360
Nipun Gupta07397df2018-04-28 08:21:58 +0530361extern int platform_dma_configure(struct device *dev);
362
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200363#ifdef CONFIG_PM_SLEEP
364#define USE_PLATFORM_PM_SLEEP_OPS \
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200365 .suspend = platform_pm_suspend, \
366 .resume = platform_pm_resume, \
367 .freeze = platform_pm_freeze, \
368 .thaw = platform_pm_thaw, \
369 .poweroff = platform_pm_poweroff, \
Rafael J. Wysocki9b39e732011-12-18 00:34:24 +0100370 .restore = platform_pm_restore,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +0200371#else
372#define USE_PLATFORM_PM_SLEEP_OPS
373#endif
374
Russell Kingbbbf5082005-10-29 22:17:58 +0100375#endif /* _PLATFORM_DEVICE_H_ */