blob: 0d3611cd1b3bc40a5113417138456341230d1aa9 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * platform.c - platform 'pseudo' bus for legacy devices
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Please see Documentation/driver-model/platform.txt for more
9 * information.
10 */
11
Andrew Mortondaa41222009-08-06 16:00:44 -070012#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Grant Likely05212152010-06-08 07:48:20 -060014#include <linux/of_device.h>
Rob Herring9ec36ca2014-04-23 17:57:41 -050015#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070019#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020022#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020023#include <linux/pm_domain.h>
Jean Delvare689ae232012-07-27 22:14:59 +020024#include <linux/idr.h>
Mika Westerberg91e56872012-10-31 22:45:02 +010025#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020026#include <linux/clk/clk-conf.h>
Kim Phillips3d713e02014-06-02 19:42:58 -050027#include <linux/limits.h>
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020028#include <linux/property.h>
Qian Cai967d3012019-01-03 15:29:05 -080029#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010031#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020032#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010033
Jean Delvare689ae232012-07-27 22:14:59 +020034/* For automatically allocated device IDs */
35static DEFINE_IDA(platform_devid_ida);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010038 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050040EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/**
Kumar Galaa77ce812011-06-10 01:52:57 -050043 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
Randy Dunlap7de636f2011-07-27 12:11:25 -070044 * @pdev: platform device
Kumar Galaa77ce812011-06-10 01:52:57 -050045 *
46 * This is called before platform_device_add() such that any pdev_archdata may
47 * be setup before the platform_notifier is called. So if a user needs to
48 * manipulate any relevant information in the pdev_archdata they can do:
49 *
Sebastian Andrzej Siewiorb1d6d822012-10-29 18:04:53 +010050 * platform_device_alloc()
Fabio Porcedda0258e182013-03-26 10:35:16 +010051 * ... manipulate ...
52 * platform_device_add()
Kumar Galaa77ce812011-06-10 01:52:57 -050053 *
54 * And if they don't care they can just call platform_device_register() and
55 * everything will just work out.
56 */
57void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
58{
59}
60
61/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080062 * platform_get_resource - get a resource for a device
63 * @dev: platform device
64 * @type: resource type
65 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080067struct resource *platform_get_resource(struct platform_device *dev,
68 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int i;
71
72 for (i = 0; i < dev->num_resources; i++) {
73 struct resource *r = &dev->resource[i];
74
Magnus Dammc9f66162008-10-15 22:05:15 -070075 if (type == resource_type(r) && num-- == 0)
76 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 return NULL;
79}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050080EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080083 * platform_get_irq - get an IRQ for a device
84 * @dev: platform device
85 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87int platform_get_irq(struct platform_device *dev, unsigned int num)
88{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +000089#ifdef CONFIG_SPARC
90 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
91 if (!dev || num >= dev->archdata.num_irqs)
92 return -ENXIO;
93 return dev->archdata.irqs[num];
94#else
Rob Herring9ec36ca2014-04-23 17:57:41 -050095 struct resource *r;
Guenter Roeckaff008a2014-06-17 15:51:02 -070096 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
97 int ret;
98
99 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300100 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700101 return ret;
102 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500103
104 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500105 if (has_acpi_companion(&dev->dev)) {
106 if (r && r->flags & IORESOURCE_DISABLED) {
107 int ret;
108
109 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
110 if (ret)
111 return ret;
112 }
113 }
114
Linus Walleij7085a742015-02-18 17:12:18 +0100115 /*
116 * The resources may pass trigger flags to the irqs that need
117 * to be set up. It so happens that the trigger flags for
118 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
119 * settings.
120 */
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700121 if (r && r->flags & IORESOURCE_BITS) {
122 struct irq_data *irqd;
123
124 irqd = irq_get_irq_data(r->start);
125 if (!irqd)
126 return -ENXIO;
127 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Enrico Granatadaaef252019-02-11 11:01:12 -0800130 if (r)
131 return r->start;
132
133 /*
134 * For the index 0 interrupt, allow falling back to GpioInt
135 * resources. While a device could have both Interrupt and GpioInt
136 * resources, making this fallback ambiguous, in many common cases
137 * the device will only expose one IRQ, and this fallback
138 * allows a common code path across either kind of resource.
139 */
140 if (num == 0 && has_acpi_companion(&dev->dev))
141 return acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
142
143 return -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500146EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800149 * platform_irq_count - Count the number of IRQs a platform device uses
150 * @dev: platform device
151 *
152 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
153 */
154int platform_irq_count(struct platform_device *dev)
155{
156 int ret, nr = 0;
157
158 while ((ret = platform_get_irq(dev, nr)) >= 0)
159 nr++;
160
161 if (ret == -EPROBE_DEFER)
162 return ret;
163
164 return nr;
165}
166EXPORT_SYMBOL_GPL(platform_irq_count);
167
168/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800169 * platform_get_resource_byname - get a resource for a device by name
170 * @dev: platform device
171 * @type: resource type
172 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800174struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200175 unsigned int type,
176 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int i;
179
180 for (i = 0; i < dev->num_resources; i++) {
181 struct resource *r = &dev->resource[i];
182
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300183 if (unlikely(!r->name))
184 continue;
185
Magnus Dammc9f66162008-10-15 22:05:15 -0700186 if (type == resource_type(r) && !strcmp(r->name, name))
187 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 return NULL;
190}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500191EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/**
Wolfram Sangd6ff8552012-11-13 17:47:13 +0100194 * platform_get_irq_byname - get an IRQ for a device by name
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800195 * @dev: platform device
196 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200198int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300200 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Guenter Roeckaff008a2014-06-17 15:51:02 -0700202 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
203 int ret;
204
205 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300206 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700207 return ret;
208 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300209
210 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
David Vrabel305b3222006-01-19 17:52:27 +0000211 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500213EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216 * platform_add_devices - add a numbers of platform devices
217 * @devs: array of platform devices to add
218 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
220int platform_add_devices(struct platform_device **devs, int num)
221{
222 int i, ret = 0;
223
224 for (i = 0; i < num; i++) {
225 ret = platform_device_register(devs[i]);
226 if (ret) {
227 while (--i >= 0)
228 platform_device_unregister(devs[i]);
229 break;
230 }
231 }
232
233 return ret;
234}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500235EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Russell King37c12e72005-11-05 21:19:33 +0000237struct platform_object {
238 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200239 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000240};
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000243 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000245 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800246 * Free all memory associated with a platform device. This function must
247 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000248 */
249void platform_device_put(struct platform_device *pdev)
250{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200251 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000252 put_device(&pdev->dev);
253}
254EXPORT_SYMBOL_GPL(platform_device_put);
255
256static void platform_device_release(struct device *dev)
257{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800258 struct platform_object *pa = container_of(dev, struct platform_object,
259 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000260
Grant Likely7096d042010-10-20 11:45:13 -0600261 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000262 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200263 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000264 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500265 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000266 kfree(pa);
267}
268
269/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000270 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800271 * @name: base name of the device we're adding
272 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000273 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800274 * Create a platform device object which can have other objects attached
275 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000276 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200277struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000278{
279 struct platform_object *pa;
280
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200281 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000282 if (pa) {
283 strcpy(pa->name, name);
284 pa->pdev.name = pa->name;
285 pa->pdev.id = id;
286 device_initialize(&pa->pdev.dev);
287 pa->pdev.dev.release = platform_device_release;
Kumar Galaa77ce812011-06-10 01:52:57 -0500288 arch_setup_pdev_archdata(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000289 }
290
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500291 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000292}
293EXPORT_SYMBOL_GPL(platform_device_alloc);
294
295/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000296 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800297 * @pdev: platform device allocated by platform_device_alloc to add resources to
298 * @res: set of resources that needs to be allocated for the device
299 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000300 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800301 * Add a copy of the resources to the platform device. The memory
302 * associated with the resources will be freed when the platform device is
303 * released.
Russell King37c12e72005-11-05 21:19:33 +0000304 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800305int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100306 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000307{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200308 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000309
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200310 if (res) {
311 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
312 if (!r)
313 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000314 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200315
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200316 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200317 pdev->resource = r;
318 pdev->num_resources = num;
319 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000320}
321EXPORT_SYMBOL_GPL(platform_device_add_resources);
322
323/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000324 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800325 * @pdev: platform device allocated by platform_device_alloc to add resources to
326 * @data: platform specific data for this platform device
327 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000328 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800329 * Add a copy of platform specific data to the platform device's
330 * platform_data pointer. The memory associated with the platform data
331 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000332 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800333int platform_device_add_data(struct platform_device *pdev, const void *data,
334 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000335{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200336 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000337
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200338 if (data) {
339 d = kmemdup(data, size, GFP_KERNEL);
340 if (!d)
341 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000342 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200343
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200344 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200345 pdev->dev.platform_data = d;
346 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000347}
348EXPORT_SYMBOL_GPL(platform_device_add_data);
349
350/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200351 * platform_device_add_properties - add built-in properties to a platform device
352 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300353 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200354 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300355 * The function will take deep copy of @properties and attach the copy to the
356 * platform device. The memory associated with properties will be freed when the
357 * platform device is released.
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200358 */
359int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200360 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200361{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300362 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200363}
364EXPORT_SYMBOL_GPL(platform_device_add_properties);
365
366/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800367 * platform_device_add - add a platform device to device hierarchy
368 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800370 * This is part 2 of platform_device_register(), though may be called
371 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
Russell King37c12e72005-11-05 21:19:33 +0000373int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jean Delvare689ae232012-07-27 22:14:59 +0200375 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!pdev)
378 return -EINVAL;
379
380 if (!pdev->dev.parent)
381 pdev->dev.parent = &platform_bus;
382
383 pdev->dev.bus = &platform_bus_type;
384
Jean Delvare689ae232012-07-27 22:14:59 +0200385 switch (pdev->id) {
386 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100387 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200388 break;
389 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700390 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200391 break;
392 case PLATFORM_DEVID_AUTO:
393 /*
394 * Automatically allocated device ID. We mark it as such so
395 * that we remember it must be freed, and we append a suffix
396 * to avoid namespace collision with explicit IDs.
397 */
398 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
399 if (ret < 0)
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700400 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200401 pdev->id = ret;
402 pdev->id_auto = true;
403 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
404 break;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700408 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100411 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 p = r->parent;
414 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700415 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700417 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 p = &ioport_resource;
419 }
420
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700421 if (p && insert_resource(p, r)) {
Chen Yu8a18f422016-12-21 17:24:55 +0800422 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700423 ret = -EBUSY;
424 goto failed;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
428 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100429 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Russell Kinge3915532006-05-06 08:15:26 +0100431 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700432 if (ret == 0)
433 return ret;
434
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700435 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700436 if (pdev->id_auto) {
437 ida_simple_remove(&platform_devid_ida, pdev->id);
438 pdev->id = PLATFORM_DEVID_AUTO;
439 }
440
441 while (--i >= 0) {
442 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100443 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700444 release_resource(r);
445 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700446
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700447 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return ret;
449}
Russell King37c12e72005-11-05 21:19:33 +0000450EXPORT_SYMBOL_GPL(platform_device_add);
451
452/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800453 * platform_device_del - remove a platform-level device
454 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500455 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800456 * Note that this function will also release all memory- and port-based
457 * resources owned by the device (@dev->resource). This function must
458 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500459 */
460void platform_device_del(struct platform_device *pdev)
461{
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700462 int i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500463
Andy Shevchenko99fef582018-12-03 20:21:41 +0200464 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700465 device_del(&pdev->dev);
466
467 if (pdev->id_auto) {
468 ida_simple_remove(&platform_devid_ida, pdev->id);
469 pdev->id = PLATFORM_DEVID_AUTO;
470 }
471
472 for (i = 0; i < pdev->num_resources; i++) {
473 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100474 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700475 release_resource(r);
476 }
477 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500478}
479EXPORT_SYMBOL_GPL(platform_device_del);
480
481/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800482 * platform_device_register - add a platform-level device
483 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000484 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800485int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000486{
487 device_initialize(&pdev->dev);
Kumar Galaa77ce812011-06-10 01:52:57 -0500488 arch_setup_pdev_archdata(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000489 return platform_device_add(pdev);
490}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500491EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800494 * platform_device_unregister - unregister a platform-level device
495 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800497 * Unregistration is done in 2 steps. First we release all resources
498 * and remove it from the subsystem, then we drop reference count by
499 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800501void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500503 platform_device_del(pdev);
504 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500506EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200509 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200510 * resources and platform-specific data
511 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200512 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700513 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200514 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700515 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200516struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100517 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700518{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200519 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700520 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700521
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200522 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200523 if (!pdev)
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200524 goto err_alloc;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700525
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200526 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100527 pdev->dev.fwnode = pdevinfo->fwnode;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700528
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200529 if (pdevinfo->dma_mask) {
530 /*
531 * This memory isn't freed when the device is put,
532 * I don't have a nice idea for that though. Conceptually
533 * dma_mask in struct device should not be a pointer.
534 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
535 */
536 pdev->dev.dma_mask =
537 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
538 if (!pdev->dev.dma_mask)
539 goto err;
540
Qian Cai967d3012019-01-03 15:29:05 -0800541 kmemleak_ignore(pdev->dev.dma_mask);
542
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200543 *pdev->dev.dma_mask = pdevinfo->dma_mask;
544 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
545 }
546
547 ret = platform_device_add_resources(pdev,
548 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400549 if (ret)
550 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700551
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200552 ret = platform_device_add_data(pdev,
553 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400554 if (ret)
555 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200556
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300557 if (pdevinfo->properties) {
558 ret = platform_device_add_properties(pdev,
559 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200560 if (ret)
561 goto err;
562 }
563
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200564 ret = platform_device_add(pdev);
565 if (ret) {
566err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100567 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200568 kfree(pdev->dev.dma_mask);
569
570err_alloc:
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200571 platform_device_put(pdev);
572 return ERR_PTR(ret);
573 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700574
575 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700576}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200577EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700578
Russell King00d3dcd2005-11-09 17:23:39 +0000579static int platform_drv_probe(struct device *_dev)
580{
581 struct platform_driver *drv = to_platform_driver(_dev->driver);
582 struct platform_device *dev = to_platform_device(_dev);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100583 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000584
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200585 ret = of_clk_set_defaults(_dev->of_node, false);
586 if (ret < 0)
587 return ret;
588
Ulf Hanssoncb518412014-09-19 20:27:38 +0200589 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200590 if (ret)
591 goto out;
592
593 if (drv->probe) {
594 ret = drv->probe(dev);
595 if (ret)
596 dev_pm_domain_detach(_dev, true);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200597 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100598
Ulf Hansson88a97692018-04-26 10:53:06 +0200599out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200600 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
601 dev_warn(_dev, "probe deferral not supported\n");
602 ret = -ENXIO;
603 }
604
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100605 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000606}
607
David Brownellc67334f2006-11-16 23:28:47 -0800608static int platform_drv_probe_fail(struct device *_dev)
609{
610 return -ENXIO;
611}
612
Russell King00d3dcd2005-11-09 17:23:39 +0000613static int platform_drv_remove(struct device *_dev)
614{
615 struct platform_driver *drv = to_platform_driver(_dev->driver);
616 struct platform_device *dev = to_platform_device(_dev);
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200617 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000618
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200619 if (drv->remove)
620 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200621 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100622
623 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000624}
625
626static void platform_drv_shutdown(struct device *_dev)
627{
628 struct platform_driver *drv = to_platform_driver(_dev->driver);
629 struct platform_device *dev = to_platform_device(_dev);
630
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200631 if (drv->shutdown)
632 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000633}
634
Russell King00d3dcd2005-11-09 17:23:39 +0000635/**
Libo Chen94470572013-05-25 12:40:50 +0800636 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800637 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700638 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000639 */
Libo Chen94470572013-05-25 12:40:50 +0800640int __platform_driver_register(struct platform_driver *drv,
641 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000642{
Libo Chen94470572013-05-25 12:40:50 +0800643 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000644 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200645 drv->driver.probe = platform_drv_probe;
646 drv->driver.remove = platform_drv_remove;
647 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200648
Russell King00d3dcd2005-11-09 17:23:39 +0000649 return driver_register(&drv->driver);
650}
Libo Chen94470572013-05-25 12:40:50 +0800651EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000652
653/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000654 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800655 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000656 */
657void platform_driver_unregister(struct platform_driver *drv)
658{
659 driver_unregister(&drv->driver);
660}
661EXPORT_SYMBOL_GPL(platform_driver_unregister);
662
David Brownellc67334f2006-11-16 23:28:47 -0800663/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100664 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800665 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200666 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100667 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800668 *
669 * Use this instead of platform_driver_register() when you know the device
670 * is not hotpluggable and has already been registered, and you want to
671 * remove its run-once probe() infrastructure from memory after the driver
672 * has bound to the device.
673 *
674 * One typical use for this would be with drivers for controllers integrated
675 * into system-on-chip processors, where the controller devices have been
676 * configured as part of board setup.
677 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200678 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100679 *
David Brownellc67334f2006-11-16 23:28:47 -0800680 * Returns zero if the driver registered and bound to a device, else returns
681 * a negative error code and with the driver not registered.
682 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100683int __init_or_module __platform_driver_probe(struct platform_driver *drv,
684 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800685{
686 int retval, code;
687
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700688 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
689 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
690 drv->driver.name, __func__);
691 return -EINVAL;
692 }
693
694 /*
695 * We have to run our probes synchronously because we check if
696 * we find any devices to bind to and exit with error if there
697 * are any.
698 */
699 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
700
Johan Hovold3f9120b2013-09-23 16:27:26 +0200701 /*
702 * Prevent driver from requesting probe deferral to avoid further
703 * futile probe attempts.
704 */
705 drv->prevent_deferred_probe = true;
706
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700707 /* make sure driver won't have bind/unbind attributes */
708 drv->driver.suppress_bind_attrs = true;
709
David Brownellc67334f2006-11-16 23:28:47 -0800710 /* temporary section violation during probe() */
711 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100712 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800713
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700714 /*
715 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800716 * the list of drivers in order to probe new devices. Check to see
717 * if the probe was successful, and make sure any forced probes of
718 * new devices fail.
719 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700720 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800721 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800722 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800723 retval = -ENODEV;
724 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700725 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800726
727 if (code != retval)
728 platform_driver_unregister(drv);
729 return retval;
730}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100731EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800733/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100734 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800735 * @driver: platform driver structure
736 * @probe: the driver probe routine, probably from an __init section
737 * @res: set of resources that needs to be allocated for the device
738 * @n_res: number of resources
739 * @data: platform specific data for this platform device
740 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100741 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800742 *
743 * Use this in legacy-style modules that probe hardware directly and
744 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200745 *
746 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800747 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100748struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800749 struct platform_driver *driver,
750 int (*probe)(struct platform_device *),
751 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100752 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800753{
754 struct platform_device *pdev;
755 int error;
756
757 pdev = platform_device_alloc(driver->driver.name, -1);
758 if (!pdev) {
759 error = -ENOMEM;
760 goto err_out;
761 }
762
Anton Vorontsov807508c2010-09-07 17:31:54 +0400763 error = platform_device_add_resources(pdev, res, n_res);
764 if (error)
765 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800766
Anton Vorontsov807508c2010-09-07 17:31:54 +0400767 error = platform_device_add_data(pdev, data, size);
768 if (error)
769 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800770
771 error = platform_device_add(pdev);
772 if (error)
773 goto err_pdev_put;
774
Wolfram Sang291f6532014-10-28 17:40:42 +0100775 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800776 if (error)
777 goto err_pdev_del;
778
779 return pdev;
780
781err_pdev_del:
782 platform_device_del(pdev);
783err_pdev_put:
784 platform_device_put(pdev);
785err_out:
786 return ERR_PTR(error);
787}
Wolfram Sang291f6532014-10-28 17:40:42 +0100788EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800789
Thierry Redingdbe22562015-09-25 17:29:04 +0200790/**
791 * __platform_register_drivers - register an array of platform drivers
792 * @drivers: an array of drivers to register
793 * @count: the number of drivers to register
794 * @owner: module owning the drivers
795 *
796 * Registers platform drivers specified by an array. On failure to register a
797 * driver, all previously registered drivers will be unregistered. Callers of
798 * this API should use platform_unregister_drivers() to unregister drivers in
799 * the reverse order.
800 *
801 * Returns: 0 on success or a negative error code on failure.
802 */
803int __platform_register_drivers(struct platform_driver * const *drivers,
804 unsigned int count, struct module *owner)
805{
806 unsigned int i;
807 int err;
808
809 for (i = 0; i < count; i++) {
810 pr_debug("registering platform driver %ps\n", drivers[i]);
811
812 err = __platform_driver_register(drivers[i], owner);
813 if (err < 0) {
814 pr_err("failed to register platform driver %ps: %d\n",
815 drivers[i], err);
816 goto error;
817 }
818 }
819
820 return 0;
821
822error:
823 while (i--) {
824 pr_debug("unregistering platform driver %ps\n", drivers[i]);
825 platform_driver_unregister(drivers[i]);
826 }
827
828 return err;
829}
830EXPORT_SYMBOL_GPL(__platform_register_drivers);
831
832/**
833 * platform_unregister_drivers - unregister an array of platform drivers
834 * @drivers: an array of drivers to unregister
835 * @count: the number of drivers to unregister
836 *
837 * Unegisters platform drivers specified by an array. This is typically used
838 * to complement an earlier call to platform_register_drivers(). Drivers are
839 * unregistered in the reverse order in which they were registered.
840 */
841void platform_unregister_drivers(struct platform_driver * const *drivers,
842 unsigned int count)
843{
844 while (count--) {
845 pr_debug("unregistering platform driver %ps\n", drivers[count]);
846 platform_driver_unregister(drivers[count]);
847 }
848}
849EXPORT_SYMBOL_GPL(platform_unregister_drivers);
850
David Brownella0245f72006-05-29 10:37:33 -0700851/* modalias support enables more hands-off userspace setup:
852 * (a) environment variable lets new-style hotplug events work once system is
853 * fully running: "modprobe $MODALIAS"
854 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
855 * mishandled before system is fully running: "modprobe $(cat modalias)"
856 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800857static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
858 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700859{
860 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800861 int len;
862
Rob Herring0634c292017-03-22 09:16:27 -0500863 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +0800864 if (len != -ENODEV)
865 return len;
866
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800867 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
868 if (len != -ENODEV)
869 return len;
870
871 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700872
873 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
874}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700875static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -0700876
Kim Phillips3d713e02014-06-02 19:42:58 -0500877static ssize_t driver_override_store(struct device *dev,
878 struct device_attribute *attr,
879 const char *buf, size_t count)
880{
881 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700882 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -0500883
Nicolai Stangebf563b02017-09-11 09:45:42 +0200884 /* We need to keep extra room for a newline */
885 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -0500886 return -EINVAL;
887
888 driver_override = kstrndup(buf, count, GFP_KERNEL);
889 if (!driver_override)
890 return -ENOMEM;
891
892 cp = strchr(driver_override, '\n');
893 if (cp)
894 *cp = '\0';
895
Adrian Salido626553972017-04-25 16:55:26 -0700896 device_lock(dev);
897 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -0500898 if (strlen(driver_override)) {
899 pdev->driver_override = driver_override;
900 } else {
901 kfree(driver_override);
902 pdev->driver_override = NULL;
903 }
Adrian Salido626553972017-04-25 16:55:26 -0700904 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -0500905
906 kfree(old);
907
908 return count;
909}
910
911static ssize_t driver_override_show(struct device *dev,
912 struct device_attribute *attr, char *buf)
913{
914 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700915 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500916
Adrian Salido626553972017-04-25 16:55:26 -0700917 device_lock(dev);
918 len = sprintf(buf, "%s\n", pdev->driver_override);
919 device_unlock(dev);
920 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500921}
922static DEVICE_ATTR_RW(driver_override);
923
924
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700925static struct attribute *platform_dev_attrs[] = {
926 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -0500927 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700928 NULL,
David Brownella0245f72006-05-29 10:37:33 -0700929};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700930ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -0700931
Kay Sievers7eff2e72007-08-14 15:15:12 +0200932static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700933{
934 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600935 int rc;
936
937 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +0100938 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -0600939 if (rc != -ENODEV)
940 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700941
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800942 rc = acpi_device_uevent_modalias(dev, env);
943 if (rc != -ENODEV)
944 return rc;
945
Eric Miao57fee4a2009-02-04 11:52:40 +0800946 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Sebastian Andrzej Siewior0a268132011-08-15 16:51:22 +0200947 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700948 return 0;
949}
950
Eric Miao57fee4a2009-02-04 11:52:40 +0800951static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100952 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800953 struct platform_device *pdev)
954{
955 while (id->name[0]) {
956 if (strcmp(pdev->name, id->name) == 0) {
957 pdev->id_entry = id;
958 return id;
959 }
960 id++;
961 }
962 return NULL;
963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800966 * platform_match - bind platform device to platform driver.
967 * @dev: device.
968 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800970 * Platform device IDs are assumed to be encoded like this:
971 * "<name><instance>", where <name> is a short description of the type of
972 * device, like "pci" or "floppy", and <instance> is the enumerated
973 * instance of the device, like '0' or '42'. Driver IDs are simply
974 * "<name>". So, extract the <name> from the platform_device structure,
975 * and compare it against the name of the driver. Return whether they match
976 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800978static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800980 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800981 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Kim Phillips3d713e02014-06-02 19:42:58 -0500983 /* When driver_override is set, only bind to the matching driver */
984 if (pdev->driver_override)
985 return !strcmp(pdev->driver_override, drv->name);
986
Grant Likely05212152010-06-08 07:48:20 -0600987 /* Attempt an OF style match first */
988 if (of_driver_match_device(dev, drv))
989 return 1;
990
Mika Westerberg91e56872012-10-31 22:45:02 +0100991 /* Then try ACPI style match */
992 if (acpi_driver_match_device(dev, drv))
993 return 1;
994
Grant Likely05212152010-06-08 07:48:20 -0600995 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800996 if (pdrv->id_table)
997 return platform_match_id(pdrv->id_table, pdev) != NULL;
998
999 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001000 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001003#ifdef CONFIG_PM_SLEEP
1004
1005static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001007 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1008 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int ret = 0;
1010
Magnus Damm783ea7d2009-06-04 22:13:33 +02001011 if (dev->driver && pdrv->suspend)
1012 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001013
1014 return ret;
1015}
1016
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001017static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001019 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1020 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int ret = 0;
1022
Magnus Damm783ea7d2009-06-04 22:13:33 +02001023 if (dev->driver && pdrv->resume)
1024 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return ret;
1027}
1028
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001029#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001030
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001031#ifdef CONFIG_SUSPEND
1032
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001033int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001034{
1035 struct device_driver *drv = dev->driver;
1036 int ret = 0;
1037
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001038 if (!drv)
1039 return 0;
1040
1041 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001042 if (drv->pm->suspend)
1043 ret = drv->pm->suspend(dev);
1044 } else {
1045 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1046 }
1047
1048 return ret;
1049}
1050
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001051int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001052{
1053 struct device_driver *drv = dev->driver;
1054 int ret = 0;
1055
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001056 if (!drv)
1057 return 0;
1058
1059 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001060 if (drv->pm->resume)
1061 ret = drv->pm->resume(dev);
1062 } else {
1063 ret = platform_legacy_resume(dev);
1064 }
1065
1066 return ret;
1067}
1068
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001069#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001070
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001071#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001072
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001073int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001074{
1075 struct device_driver *drv = dev->driver;
1076 int ret = 0;
1077
1078 if (!drv)
1079 return 0;
1080
1081 if (drv->pm) {
1082 if (drv->pm->freeze)
1083 ret = drv->pm->freeze(dev);
1084 } else {
1085 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1086 }
1087
1088 return ret;
1089}
1090
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001091int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001092{
1093 struct device_driver *drv = dev->driver;
1094 int ret = 0;
1095
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001096 if (!drv)
1097 return 0;
1098
1099 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001100 if (drv->pm->thaw)
1101 ret = drv->pm->thaw(dev);
1102 } else {
1103 ret = platform_legacy_resume(dev);
1104 }
1105
1106 return ret;
1107}
1108
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001109int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001110{
1111 struct device_driver *drv = dev->driver;
1112 int ret = 0;
1113
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001114 if (!drv)
1115 return 0;
1116
1117 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001118 if (drv->pm->poweroff)
1119 ret = drv->pm->poweroff(dev);
1120 } else {
1121 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1122 }
1123
1124 return ret;
1125}
1126
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001127int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001128{
1129 struct device_driver *drv = dev->driver;
1130 int ret = 0;
1131
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001132 if (!drv)
1133 return 0;
1134
1135 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001136 if (drv->pm->restore)
1137 ret = drv->pm->restore(dev);
1138 } else {
1139 ret = platform_legacy_resume(dev);
1140 }
1141
1142 return ret;
1143}
1144
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001145#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001146
Nipun Gupta07397df2018-04-28 08:21:58 +05301147int platform_dma_configure(struct device *dev)
1148{
1149 enum dev_dma_attr attr;
1150 int ret = 0;
1151
1152 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001153 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301154 } else if (has_acpi_companion(dev)) {
1155 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001156 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301157 }
1158
1159 return ret;
1160}
1161
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001162static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001163 .runtime_suspend = pm_generic_runtime_suspend,
1164 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001165 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001166};
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168struct bus_type platform_bus_type = {
1169 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001170 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001172 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301173 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001174 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001176EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178int __init platform_bus_init(void)
1179{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001180 int error;
1181
Magnus Damm13977092009-03-30 14:37:25 -07001182 early_platform_cleanup();
1183
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001184 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301185 if (error) {
1186 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001187 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301188 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001189 error = bus_register(&platform_bus_type);
1190 if (error)
1191 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001192 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Magnus Damm13977092009-03-30 14:37:25 -07001196static __initdata LIST_HEAD(early_platform_driver_list);
1197static __initdata LIST_HEAD(early_platform_device_list);
1198
1199/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001200 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001201 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001202 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001203 *
1204 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001205 */
1206int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1207 char *buf)
1208{
Magnus Dammc60e0502009-11-27 17:38:51 +09001209 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001210 int n;
1211
1212 /* Simply add the driver to the end of the global list.
1213 * Drivers will by default be put on the list in compiled-in order.
1214 */
1215 if (!epdrv->list.next) {
1216 INIT_LIST_HEAD(&epdrv->list);
1217 list_add_tail(&epdrv->list, &early_platform_driver_list);
1218 }
1219
1220 /* If the user has specified device then make sure the driver
1221 * gets prioritized. The driver of the last device specified on
1222 * command line will be put first on the list.
1223 */
1224 n = strlen(epdrv->pdrv->driver.name);
1225 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1226 list_move(&epdrv->list, &early_platform_driver_list);
1227
Magnus Dammc60e0502009-11-27 17:38:51 +09001228 /* Allow passing parameters after device name */
1229 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001230 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001231 else {
1232 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1233 &tmp, 10);
1234
1235 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1236 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1237 n = 0;
1238 } else
1239 n += strcspn(&buf[n + 1], ",") + 1;
1240 }
1241
1242 if (buf[n] == ',')
1243 n++;
1244
1245 if (epdrv->bufsize) {
1246 memcpy(epdrv->buffer, &buf[n],
1247 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1248 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1249 }
Magnus Damm13977092009-03-30 14:37:25 -07001250 }
1251
1252 return 0;
1253}
1254
1255/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001256 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001257 * @devs: array of early platform devices to add
1258 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001259 *
1260 * Used by early architecture code to register early platform devices and
1261 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001262 */
1263void __init early_platform_add_devices(struct platform_device **devs, int num)
1264{
1265 struct device *dev;
1266 int i;
1267
1268 /* simply add the devices to list */
1269 for (i = 0; i < num; i++) {
1270 dev = &devs[i]->dev;
1271
1272 if (!dev->devres_head.next) {
Rafael J. Wysockibed2b422012-08-06 01:45:11 +02001273 pm_runtime_early_init(dev);
Magnus Damm13977092009-03-30 14:37:25 -07001274 INIT_LIST_HEAD(&dev->devres_head);
1275 list_add_tail(&dev->devres_head,
1276 &early_platform_device_list);
1277 }
1278 }
1279}
1280
1281/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001282 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001283 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001284 *
1285 * Used by architecture code to register all early platform drivers
1286 * for a certain class. If omitted then only early platform drivers
1287 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001288 */
1289void __init early_platform_driver_register_all(char *class_str)
1290{
1291 /* The "class_str" parameter may or may not be present on the kernel
1292 * command line. If it is present then there may be more than one
1293 * matching parameter.
1294 *
1295 * Since we register our early platform drivers using early_param()
1296 * we need to make sure that they also get registered in the case
1297 * when the parameter is missing from the kernel command line.
1298 *
1299 * We use parse_early_options() to make sure the early_param() gets
1300 * called at least once. The early_param() may be called more than
1301 * once since the name of the preferred device may be specified on
1302 * the kernel command line. early_platform_driver_register() handles
1303 * this case for us.
1304 */
1305 parse_early_options(class_str);
1306}
1307
1308/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001309 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001310 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001311 * @id: id to match against
1312 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001313static struct platform_device * __init
Magnus Damm13977092009-03-30 14:37:25 -07001314early_platform_match(struct early_platform_driver *epdrv, int id)
1315{
1316 struct platform_device *pd;
1317
1318 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1319 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1320 if (pd->id == id)
1321 return pd;
1322
1323 return NULL;
1324}
1325
1326/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001327 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001328 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001329 * @id: return true if id or above exists
1330 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001331static int __init early_platform_left(struct early_platform_driver *epdrv,
Magnus Damm13977092009-03-30 14:37:25 -07001332 int id)
1333{
1334 struct platform_device *pd;
1335
1336 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1337 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1338 if (pd->id >= id)
1339 return 1;
1340
1341 return 0;
1342}
1343
1344/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001345 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001346 * @class_str: string to identify early platform driver class
1347 * @id: id to match against
1348 * @nr_probe: number of platform devices to successfully probe before exiting
1349 */
1350static int __init early_platform_driver_probe_id(char *class_str,
1351 int id,
1352 int nr_probe)
1353{
1354 struct early_platform_driver *epdrv;
1355 struct platform_device *match;
1356 int match_id;
1357 int n = 0;
1358 int left = 0;
1359
1360 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1361 /* only use drivers matching our class_str */
1362 if (strcmp(class_str, epdrv->class_str))
1363 continue;
1364
1365 if (id == -2) {
1366 match_id = epdrv->requested_id;
1367 left = 1;
1368
1369 } else {
1370 match_id = id;
1371 left += early_platform_left(epdrv, id);
1372
1373 /* skip requested id */
1374 switch (epdrv->requested_id) {
1375 case EARLY_PLATFORM_ID_ERROR:
1376 case EARLY_PLATFORM_ID_UNSET:
1377 break;
1378 default:
1379 if (epdrv->requested_id == id)
1380 match_id = EARLY_PLATFORM_ID_UNSET;
1381 }
1382 }
1383
1384 switch (match_id) {
1385 case EARLY_PLATFORM_ID_ERROR:
Fabio Porcedda0258e182013-03-26 10:35:16 +01001386 pr_warn("%s: unable to parse %s parameter\n",
1387 class_str, epdrv->pdrv->driver.name);
Magnus Damm13977092009-03-30 14:37:25 -07001388 /* fall-through */
1389 case EARLY_PLATFORM_ID_UNSET:
1390 match = NULL;
1391 break;
1392 default:
1393 match = early_platform_match(epdrv, match_id);
1394 }
1395
1396 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001397 /*
1398 * Set up a sensible init_name to enable
1399 * dev_name() and others to be used before the
1400 * rest of the driver core is initialized.
1401 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001402 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001403 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001404 match->dev.init_name =
1405 kasprintf(GFP_KERNEL, "%s.%d",
1406 match->name,
1407 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001408 else
Paul Mundtbd050862010-03-29 15:51:35 +09001409 match->dev.init_name =
1410 kasprintf(GFP_KERNEL, "%s",
1411 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001412
Paul Mundta636ee72010-03-09 06:57:53 +00001413 if (!match->dev.init_name)
1414 return -ENOMEM;
1415 }
Paul Mundtbd050862010-03-29 15:51:35 +09001416
Magnus Damm13977092009-03-30 14:37:25 -07001417 if (epdrv->pdrv->probe(match))
Fabio Porcedda0258e182013-03-26 10:35:16 +01001418 pr_warn("%s: unable to probe %s early.\n",
1419 class_str, match->name);
Magnus Damm13977092009-03-30 14:37:25 -07001420 else
1421 n++;
1422 }
1423
1424 if (n >= nr_probe)
1425 break;
1426 }
1427
1428 if (left)
1429 return n;
1430 else
1431 return -ENODEV;
1432}
1433
1434/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001435 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001436 * @class_str: string to identify early platform driver class
1437 * @nr_probe: number of platform devices to successfully probe before exiting
1438 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001439 *
1440 * Used by architecture code to probe registered early platform drivers
1441 * within a certain class. For probe to happen a registered early platform
1442 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001443 */
1444int __init early_platform_driver_probe(char *class_str,
1445 int nr_probe,
1446 int user_only)
1447{
1448 int k, n, i;
1449
1450 n = 0;
1451 for (i = -2; n < nr_probe; i++) {
1452 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1453
1454 if (k < 0)
1455 break;
1456
1457 n += k;
1458
1459 if (user_only)
1460 break;
1461 }
1462
1463 return n;
1464}
1465
1466/**
1467 * early_platform_cleanup - clean up early platform code
1468 */
1469void __init early_platform_cleanup(void)
1470{
1471 struct platform_device *pd, *pd2;
1472
1473 /* clean up the devres list used to chain devices */
1474 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1475 dev.devres_head) {
1476 list_del(&pd->dev.devres_head);
1477 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1478 }
1479}
1480