blob: be6c1eb3cbe2042718dcf7a8649056f4d12f2e03 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010030#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020031#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010032
Jean Delvare689ae232012-07-27 22:14:59 +020033/* For automatically allocated device IDs */
34static DEFINE_IDA(platform_devid_ida);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010037 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050039EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/**
Kumar Galaa77ce812011-06-10 01:52:57 -050042 * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
Randy Dunlap7de636f2011-07-27 12:11:25 -070043 * @pdev: platform device
Kumar Galaa77ce812011-06-10 01:52:57 -050044 *
45 * This is called before platform_device_add() such that any pdev_archdata may
46 * be setup before the platform_notifier is called. So if a user needs to
47 * manipulate any relevant information in the pdev_archdata they can do:
48 *
Sebastian Andrzej Siewiorb1d6d822012-10-29 18:04:53 +010049 * platform_device_alloc()
Fabio Porcedda0258e182013-03-26 10:35:16 +010050 * ... manipulate ...
51 * platform_device_add()
Kumar Galaa77ce812011-06-10 01:52:57 -050052 *
53 * And if they don't care they can just call platform_device_register() and
54 * everything will just work out.
55 */
56void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
57{
58}
59
60/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080061 * platform_get_resource - get a resource for a device
62 * @dev: platform device
63 * @type: resource type
64 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080066struct resource *platform_get_resource(struct platform_device *dev,
67 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int i;
70
71 for (i = 0; i < dev->num_resources; i++) {
72 struct resource *r = &dev->resource[i];
73
Magnus Dammc9f66162008-10-15 22:05:15 -070074 if (type == resource_type(r) && num-- == 0)
75 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 return NULL;
78}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050079EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080082 * platform_get_irq - get an IRQ for a device
83 * @dev: platform device
84 * @num: IRQ number index
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86int platform_get_irq(struct platform_device *dev, unsigned int num)
87{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +000088#ifdef CONFIG_SPARC
89 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
90 if (!dev || num >= dev->archdata.num_irqs)
91 return -ENXIO;
92 return dev->archdata.irqs[num];
93#else
Rob Herring9ec36ca2014-04-23 17:57:41 -050094 struct resource *r;
Guenter Roeckaff008a2014-06-17 15:51:02 -070095 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
96 int ret;
97
98 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +030099 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700100 return ret;
101 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500102
103 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500104 if (has_acpi_companion(&dev->dev)) {
105 if (r && r->flags & IORESOURCE_DISABLED) {
106 int ret;
107
108 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
109 if (ret)
110 return ret;
111 }
112 }
113
Linus Walleij7085a742015-02-18 17:12:18 +0100114 /*
115 * The resources may pass trigger flags to the irqs that need
116 * to be set up. It so happens that the trigger flags for
117 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
118 * settings.
119 */
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700120 if (r && r->flags & IORESOURCE_BITS) {
121 struct irq_data *irqd;
122
123 irqd = irq_get_irq_data(r->start);
124 if (!irqd)
125 return -ENXIO;
126 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Vrabel305b3222006-01-19 17:52:27 +0000129 return r ? r->start : -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500132EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800135 * platform_irq_count - Count the number of IRQs a platform device uses
136 * @dev: platform device
137 *
138 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
139 */
140int platform_irq_count(struct platform_device *dev)
141{
142 int ret, nr = 0;
143
144 while ((ret = platform_get_irq(dev, nr)) >= 0)
145 nr++;
146
147 if (ret == -EPROBE_DEFER)
148 return ret;
149
150 return nr;
151}
152EXPORT_SYMBOL_GPL(platform_irq_count);
153
154/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800155 * platform_get_resource_byname - get a resource for a device by name
156 * @dev: platform device
157 * @type: resource type
158 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800160struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200161 unsigned int type,
162 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int i;
165
166 for (i = 0; i < dev->num_resources; i++) {
167 struct resource *r = &dev->resource[i];
168
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300169 if (unlikely(!r->name))
170 continue;
171
Magnus Dammc9f66162008-10-15 22:05:15 -0700172 if (type == resource_type(r) && !strcmp(r->name, name))
173 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 return NULL;
176}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500177EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/**
Wolfram Sangd6ff8552012-11-13 17:47:13 +0100180 * platform_get_irq_byname - get an IRQ for a device by name
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181 * @dev: platform device
182 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200184int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300186 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Guenter Roeckaff008a2014-06-17 15:51:02 -0700188 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
189 int ret;
190
191 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300192 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700193 return ret;
194 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300195
196 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
David Vrabel305b3222006-01-19 17:52:27 +0000197 return r ? r->start : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500199EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800202 * platform_add_devices - add a numbers of platform devices
203 * @devs: array of platform devices to add
204 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206int platform_add_devices(struct platform_device **devs, int num)
207{
208 int i, ret = 0;
209
210 for (i = 0; i < num; i++) {
211 ret = platform_device_register(devs[i]);
212 if (ret) {
213 while (--i >= 0)
214 platform_device_unregister(devs[i]);
215 break;
216 }
217 }
218
219 return ret;
220}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500221EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Russell King37c12e72005-11-05 21:19:33 +0000223struct platform_object {
224 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200225 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000229 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800230 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000231 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800232 * Free all memory associated with a platform device. This function must
233 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000234 */
235void platform_device_put(struct platform_device *pdev)
236{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200237 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000238 put_device(&pdev->dev);
239}
240EXPORT_SYMBOL_GPL(platform_device_put);
241
242static void platform_device_release(struct device *dev)
243{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244 struct platform_object *pa = container_of(dev, struct platform_object,
245 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000246
Grant Likely7096d042010-10-20 11:45:13 -0600247 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000248 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200249 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000250 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500251 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000252 kfree(pa);
253}
254
255/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000256 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800257 * @name: base name of the device we're adding
258 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000259 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800260 * Create a platform device object which can have other objects attached
261 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000262 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200263struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000264{
265 struct platform_object *pa;
266
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200267 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000268 if (pa) {
269 strcpy(pa->name, name);
270 pa->pdev.name = pa->name;
271 pa->pdev.id = id;
272 device_initialize(&pa->pdev.dev);
273 pa->pdev.dev.release = platform_device_release;
Kumar Galaa77ce812011-06-10 01:52:57 -0500274 arch_setup_pdev_archdata(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000275 }
276
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500277 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000278}
279EXPORT_SYMBOL_GPL(platform_device_alloc);
280
281/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000282 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800283 * @pdev: platform device allocated by platform_device_alloc to add resources to
284 * @res: set of resources that needs to be allocated for the device
285 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000286 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800287 * Add a copy of the resources to the platform device. The memory
288 * associated with the resources will be freed when the platform device is
289 * released.
Russell King37c12e72005-11-05 21:19:33 +0000290 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800291int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100292 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000293{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200294 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000295
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200296 if (res) {
297 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
298 if (!r)
299 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000300 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200301
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200302 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200303 pdev->resource = r;
304 pdev->num_resources = num;
305 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000306}
307EXPORT_SYMBOL_GPL(platform_device_add_resources);
308
309/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000310 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800311 * @pdev: platform device allocated by platform_device_alloc to add resources to
312 * @data: platform specific data for this platform device
313 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000314 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800315 * Add a copy of platform specific data to the platform device's
316 * platform_data pointer. The memory associated with the platform data
317 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000318 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800319int platform_device_add_data(struct platform_device *pdev, const void *data,
320 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000321{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200322 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000323
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200324 if (data) {
325 d = kmemdup(data, size, GFP_KERNEL);
326 if (!d)
327 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000328 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200329
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200330 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200331 pdev->dev.platform_data = d;
332 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000333}
334EXPORT_SYMBOL_GPL(platform_device_add_data);
335
336/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200337 * platform_device_add_properties - add built-in properties to a platform device
338 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300339 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200340 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300341 * The function will take deep copy of @properties and attach the copy to the
342 * platform device. The memory associated with properties will be freed when the
343 * platform device is released.
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200344 */
345int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200346 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200347{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300348 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200349}
350EXPORT_SYMBOL_GPL(platform_device_add_properties);
351
352/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800353 * platform_device_add - add a platform device to device hierarchy
354 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800356 * This is part 2 of platform_device_register(), though may be called
357 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Russell King37c12e72005-11-05 21:19:33 +0000359int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jean Delvare689ae232012-07-27 22:14:59 +0200361 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!pdev)
364 return -EINVAL;
365
366 if (!pdev->dev.parent)
367 pdev->dev.parent = &platform_bus;
368
369 pdev->dev.bus = &platform_bus_type;
370
Jean Delvare689ae232012-07-27 22:14:59 +0200371 switch (pdev->id) {
372 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100373 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200374 break;
375 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700376 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200377 break;
378 case PLATFORM_DEVID_AUTO:
379 /*
380 * Automatically allocated device ID. We mark it as such so
381 * that we remember it must be freed, and we append a suffix
382 * to avoid namespace collision with explicit IDs.
383 */
384 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
385 if (ret < 0)
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700386 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200387 pdev->id = ret;
388 pdev->id_auto = true;
389 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
390 break;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700394 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100397 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 p = r->parent;
400 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700401 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700403 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 p = &ioport_resource;
405 }
406
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700407 if (p && insert_resource(p, r)) {
Chen Yu8a18f422016-12-21 17:24:55 +0800408 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700409 ret = -EBUSY;
410 goto failed;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100415 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Russell Kinge3915532006-05-06 08:15:26 +0100417 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700418 if (ret == 0)
419 return ret;
420
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700421 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700422 if (pdev->id_auto) {
423 ida_simple_remove(&platform_devid_ida, pdev->id);
424 pdev->id = PLATFORM_DEVID_AUTO;
425 }
426
427 while (--i >= 0) {
428 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100429 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700430 release_resource(r);
431 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700432
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700433 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return ret;
435}
Russell King37c12e72005-11-05 21:19:33 +0000436EXPORT_SYMBOL_GPL(platform_device_add);
437
438/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800439 * platform_device_del - remove a platform-level device
440 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500441 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800442 * Note that this function will also release all memory- and port-based
443 * resources owned by the device (@dev->resource). This function must
444 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500445 */
446void platform_device_del(struct platform_device *pdev)
447{
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700448 int i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500449
Andy Shevchenko99fef582018-12-03 20:21:41 +0200450 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700451 device_del(&pdev->dev);
452
453 if (pdev->id_auto) {
454 ida_simple_remove(&platform_devid_ida, pdev->id);
455 pdev->id = PLATFORM_DEVID_AUTO;
456 }
457
458 for (i = 0; i < pdev->num_resources; i++) {
459 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100460 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700461 release_resource(r);
462 }
463 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500464}
465EXPORT_SYMBOL_GPL(platform_device_del);
466
467/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800468 * platform_device_register - add a platform-level device
469 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000470 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800471int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000472{
473 device_initialize(&pdev->dev);
Kumar Galaa77ce812011-06-10 01:52:57 -0500474 arch_setup_pdev_archdata(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000475 return platform_device_add(pdev);
476}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500477EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800480 * platform_device_unregister - unregister a platform-level device
481 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800483 * Unregistration is done in 2 steps. First we release all resources
484 * and remove it from the subsystem, then we drop reference count by
485 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800487void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500489 platform_device_del(pdev);
490 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500492EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200495 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200496 * resources and platform-specific data
497 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200498 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700499 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200500 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700501 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200502struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100503 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700504{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200505 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700506 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700507
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200508 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200509 if (!pdev)
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200510 goto err_alloc;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700511
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200512 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100513 pdev->dev.fwnode = pdevinfo->fwnode;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700514
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200515 if (pdevinfo->dma_mask) {
516 /*
517 * This memory isn't freed when the device is put,
518 * I don't have a nice idea for that though. Conceptually
519 * dma_mask in struct device should not be a pointer.
520 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
521 */
522 pdev->dev.dma_mask =
523 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
524 if (!pdev->dev.dma_mask)
525 goto err;
526
527 *pdev->dev.dma_mask = pdevinfo->dma_mask;
528 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
529 }
530
531 ret = platform_device_add_resources(pdev,
532 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400533 if (ret)
534 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700535
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200536 ret = platform_device_add_data(pdev,
537 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400538 if (ret)
539 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200540
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300541 if (pdevinfo->properties) {
542 ret = platform_device_add_properties(pdev,
543 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200544 if (ret)
545 goto err;
546 }
547
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200548 ret = platform_device_add(pdev);
549 if (ret) {
550err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100551 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200552 kfree(pdev->dev.dma_mask);
553
554err_alloc:
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200555 platform_device_put(pdev);
556 return ERR_PTR(ret);
557 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700558
559 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700560}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200561EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700562
Russell King00d3dcd2005-11-09 17:23:39 +0000563static int platform_drv_probe(struct device *_dev)
564{
565 struct platform_driver *drv = to_platform_driver(_dev->driver);
566 struct platform_device *dev = to_platform_device(_dev);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100567 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000568
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200569 ret = of_clk_set_defaults(_dev->of_node, false);
570 if (ret < 0)
571 return ret;
572
Ulf Hanssoncb518412014-09-19 20:27:38 +0200573 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200574 if (ret)
575 goto out;
576
577 if (drv->probe) {
578 ret = drv->probe(dev);
579 if (ret)
580 dev_pm_domain_detach(_dev, true);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200581 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100582
Ulf Hansson88a97692018-04-26 10:53:06 +0200583out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200584 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
585 dev_warn(_dev, "probe deferral not supported\n");
586 ret = -ENXIO;
587 }
588
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100589 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000590}
591
David Brownellc67334f2006-11-16 23:28:47 -0800592static int platform_drv_probe_fail(struct device *_dev)
593{
594 return -ENXIO;
595}
596
Russell King00d3dcd2005-11-09 17:23:39 +0000597static int platform_drv_remove(struct device *_dev)
598{
599 struct platform_driver *drv = to_platform_driver(_dev->driver);
600 struct platform_device *dev = to_platform_device(_dev);
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200601 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000602
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200603 if (drv->remove)
604 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200605 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100606
607 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000608}
609
610static void platform_drv_shutdown(struct device *_dev)
611{
612 struct platform_driver *drv = to_platform_driver(_dev->driver);
613 struct platform_device *dev = to_platform_device(_dev);
614
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200615 if (drv->shutdown)
616 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000617}
618
Russell King00d3dcd2005-11-09 17:23:39 +0000619/**
Libo Chen94470572013-05-25 12:40:50 +0800620 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800621 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700622 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000623 */
Libo Chen94470572013-05-25 12:40:50 +0800624int __platform_driver_register(struct platform_driver *drv,
625 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000626{
Libo Chen94470572013-05-25 12:40:50 +0800627 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000628 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200629 drv->driver.probe = platform_drv_probe;
630 drv->driver.remove = platform_drv_remove;
631 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200632
Russell King00d3dcd2005-11-09 17:23:39 +0000633 return driver_register(&drv->driver);
634}
Libo Chen94470572013-05-25 12:40:50 +0800635EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000636
637/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000638 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800639 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000640 */
641void platform_driver_unregister(struct platform_driver *drv)
642{
643 driver_unregister(&drv->driver);
644}
645EXPORT_SYMBOL_GPL(platform_driver_unregister);
646
David Brownellc67334f2006-11-16 23:28:47 -0800647/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100648 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800649 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200650 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100651 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800652 *
653 * Use this instead of platform_driver_register() when you know the device
654 * is not hotpluggable and has already been registered, and you want to
655 * remove its run-once probe() infrastructure from memory after the driver
656 * has bound to the device.
657 *
658 * One typical use for this would be with drivers for controllers integrated
659 * into system-on-chip processors, where the controller devices have been
660 * configured as part of board setup.
661 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200662 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100663 *
David Brownellc67334f2006-11-16 23:28:47 -0800664 * Returns zero if the driver registered and bound to a device, else returns
665 * a negative error code and with the driver not registered.
666 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100667int __init_or_module __platform_driver_probe(struct platform_driver *drv,
668 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800669{
670 int retval, code;
671
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700672 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
673 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
674 drv->driver.name, __func__);
675 return -EINVAL;
676 }
677
678 /*
679 * We have to run our probes synchronously because we check if
680 * we find any devices to bind to and exit with error if there
681 * are any.
682 */
683 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
684
Johan Hovold3f9120b2013-09-23 16:27:26 +0200685 /*
686 * Prevent driver from requesting probe deferral to avoid further
687 * futile probe attempts.
688 */
689 drv->prevent_deferred_probe = true;
690
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700691 /* make sure driver won't have bind/unbind attributes */
692 drv->driver.suppress_bind_attrs = true;
693
David Brownellc67334f2006-11-16 23:28:47 -0800694 /* temporary section violation during probe() */
695 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100696 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800697
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700698 /*
699 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800700 * the list of drivers in order to probe new devices. Check to see
701 * if the probe was successful, and make sure any forced probes of
702 * new devices fail.
703 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700704 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800705 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800706 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800707 retval = -ENODEV;
708 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700709 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800710
711 if (code != retval)
712 platform_driver_unregister(drv);
713 return retval;
714}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100715EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800717/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100718 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800719 * @driver: platform driver structure
720 * @probe: the driver probe routine, probably from an __init section
721 * @res: set of resources that needs to be allocated for the device
722 * @n_res: number of resources
723 * @data: platform specific data for this platform device
724 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100725 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800726 *
727 * Use this in legacy-style modules that probe hardware directly and
728 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200729 *
730 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800731 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100732struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800733 struct platform_driver *driver,
734 int (*probe)(struct platform_device *),
735 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100736 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800737{
738 struct platform_device *pdev;
739 int error;
740
741 pdev = platform_device_alloc(driver->driver.name, -1);
742 if (!pdev) {
743 error = -ENOMEM;
744 goto err_out;
745 }
746
Anton Vorontsov807508c2010-09-07 17:31:54 +0400747 error = platform_device_add_resources(pdev, res, n_res);
748 if (error)
749 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800750
Anton Vorontsov807508c2010-09-07 17:31:54 +0400751 error = platform_device_add_data(pdev, data, size);
752 if (error)
753 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800754
755 error = platform_device_add(pdev);
756 if (error)
757 goto err_pdev_put;
758
Wolfram Sang291f6532014-10-28 17:40:42 +0100759 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800760 if (error)
761 goto err_pdev_del;
762
763 return pdev;
764
765err_pdev_del:
766 platform_device_del(pdev);
767err_pdev_put:
768 platform_device_put(pdev);
769err_out:
770 return ERR_PTR(error);
771}
Wolfram Sang291f6532014-10-28 17:40:42 +0100772EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800773
Thierry Redingdbe22562015-09-25 17:29:04 +0200774/**
775 * __platform_register_drivers - register an array of platform drivers
776 * @drivers: an array of drivers to register
777 * @count: the number of drivers to register
778 * @owner: module owning the drivers
779 *
780 * Registers platform drivers specified by an array. On failure to register a
781 * driver, all previously registered drivers will be unregistered. Callers of
782 * this API should use platform_unregister_drivers() to unregister drivers in
783 * the reverse order.
784 *
785 * Returns: 0 on success or a negative error code on failure.
786 */
787int __platform_register_drivers(struct platform_driver * const *drivers,
788 unsigned int count, struct module *owner)
789{
790 unsigned int i;
791 int err;
792
793 for (i = 0; i < count; i++) {
794 pr_debug("registering platform driver %ps\n", drivers[i]);
795
796 err = __platform_driver_register(drivers[i], owner);
797 if (err < 0) {
798 pr_err("failed to register platform driver %ps: %d\n",
799 drivers[i], err);
800 goto error;
801 }
802 }
803
804 return 0;
805
806error:
807 while (i--) {
808 pr_debug("unregistering platform driver %ps\n", drivers[i]);
809 platform_driver_unregister(drivers[i]);
810 }
811
812 return err;
813}
814EXPORT_SYMBOL_GPL(__platform_register_drivers);
815
816/**
817 * platform_unregister_drivers - unregister an array of platform drivers
818 * @drivers: an array of drivers to unregister
819 * @count: the number of drivers to unregister
820 *
821 * Unegisters platform drivers specified by an array. This is typically used
822 * to complement an earlier call to platform_register_drivers(). Drivers are
823 * unregistered in the reverse order in which they were registered.
824 */
825void platform_unregister_drivers(struct platform_driver * const *drivers,
826 unsigned int count)
827{
828 while (count--) {
829 pr_debug("unregistering platform driver %ps\n", drivers[count]);
830 platform_driver_unregister(drivers[count]);
831 }
832}
833EXPORT_SYMBOL_GPL(platform_unregister_drivers);
834
David Brownella0245f72006-05-29 10:37:33 -0700835/* modalias support enables more hands-off userspace setup:
836 * (a) environment variable lets new-style hotplug events work once system is
837 * fully running: "modprobe $MODALIAS"
838 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
839 * mishandled before system is fully running: "modprobe $(cat modalias)"
840 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800841static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
842 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700843{
844 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800845 int len;
846
Rob Herring0634c292017-03-22 09:16:27 -0500847 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +0800848 if (len != -ENODEV)
849 return len;
850
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800851 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
852 if (len != -ENODEV)
853 return len;
854
855 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700856
857 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
858}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700859static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -0700860
Kim Phillips3d713e02014-06-02 19:42:58 -0500861static ssize_t driver_override_store(struct device *dev,
862 struct device_attribute *attr,
863 const char *buf, size_t count)
864{
865 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700866 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -0500867
Nicolai Stangebf563b02017-09-11 09:45:42 +0200868 /* We need to keep extra room for a newline */
869 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -0500870 return -EINVAL;
871
872 driver_override = kstrndup(buf, count, GFP_KERNEL);
873 if (!driver_override)
874 return -ENOMEM;
875
876 cp = strchr(driver_override, '\n');
877 if (cp)
878 *cp = '\0';
879
Adrian Salido626553972017-04-25 16:55:26 -0700880 device_lock(dev);
881 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -0500882 if (strlen(driver_override)) {
883 pdev->driver_override = driver_override;
884 } else {
885 kfree(driver_override);
886 pdev->driver_override = NULL;
887 }
Adrian Salido626553972017-04-25 16:55:26 -0700888 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -0500889
890 kfree(old);
891
892 return count;
893}
894
895static ssize_t driver_override_show(struct device *dev,
896 struct device_attribute *attr, char *buf)
897{
898 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700899 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500900
Adrian Salido626553972017-04-25 16:55:26 -0700901 device_lock(dev);
902 len = sprintf(buf, "%s\n", pdev->driver_override);
903 device_unlock(dev);
904 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500905}
906static DEVICE_ATTR_RW(driver_override);
907
908
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700909static struct attribute *platform_dev_attrs[] = {
910 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -0500911 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700912 NULL,
David Brownella0245f72006-05-29 10:37:33 -0700913};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700914ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -0700915
Kay Sievers7eff2e72007-08-14 15:15:12 +0200916static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700917{
918 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600919 int rc;
920
921 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +0100922 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -0600923 if (rc != -ENODEV)
924 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700925
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800926 rc = acpi_device_uevent_modalias(dev, env);
927 if (rc != -ENODEV)
928 return rc;
929
Eric Miao57fee4a2009-02-04 11:52:40 +0800930 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Sebastian Andrzej Siewior0a268132011-08-15 16:51:22 +0200931 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700932 return 0;
933}
934
Eric Miao57fee4a2009-02-04 11:52:40 +0800935static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100936 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800937 struct platform_device *pdev)
938{
939 while (id->name[0]) {
940 if (strcmp(pdev->name, id->name) == 0) {
941 pdev->id_entry = id;
942 return id;
943 }
944 id++;
945 }
946 return NULL;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800950 * platform_match - bind platform device to platform driver.
951 * @dev: device.
952 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800954 * Platform device IDs are assumed to be encoded like this:
955 * "<name><instance>", where <name> is a short description of the type of
956 * device, like "pci" or "floppy", and <instance> is the enumerated
957 * instance of the device, like '0' or '42'. Driver IDs are simply
958 * "<name>". So, extract the <name> from the platform_device structure,
959 * and compare it against the name of the driver. Return whether they match
960 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800962static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800964 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800965 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Kim Phillips3d713e02014-06-02 19:42:58 -0500967 /* When driver_override is set, only bind to the matching driver */
968 if (pdev->driver_override)
969 return !strcmp(pdev->driver_override, drv->name);
970
Grant Likely05212152010-06-08 07:48:20 -0600971 /* Attempt an OF style match first */
972 if (of_driver_match_device(dev, drv))
973 return 1;
974
Mika Westerberg91e56872012-10-31 22:45:02 +0100975 /* Then try ACPI style match */
976 if (acpi_driver_match_device(dev, drv))
977 return 1;
978
Grant Likely05212152010-06-08 07:48:20 -0600979 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800980 if (pdrv->id_table)
981 return platform_match_id(pdrv->id_table, pdev) != NULL;
982
983 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100984 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200987#ifdef CONFIG_PM_SLEEP
988
989static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200991 struct platform_driver *pdrv = to_platform_driver(dev->driver);
992 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int ret = 0;
994
Magnus Damm783ea7d2009-06-04 22:13:33 +0200995 if (dev->driver && pdrv->suspend)
996 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -0700997
998 return ret;
999}
1000
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001001static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001003 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1004 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int ret = 0;
1006
Magnus Damm783ea7d2009-06-04 22:13:33 +02001007 if (dev->driver && pdrv->resume)
1008 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return ret;
1011}
1012
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001013#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001014
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001015#ifdef CONFIG_SUSPEND
1016
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001017int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001018{
1019 struct device_driver *drv = dev->driver;
1020 int ret = 0;
1021
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001022 if (!drv)
1023 return 0;
1024
1025 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001026 if (drv->pm->suspend)
1027 ret = drv->pm->suspend(dev);
1028 } else {
1029 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1030 }
1031
1032 return ret;
1033}
1034
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001035int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001036{
1037 struct device_driver *drv = dev->driver;
1038 int ret = 0;
1039
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001040 if (!drv)
1041 return 0;
1042
1043 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001044 if (drv->pm->resume)
1045 ret = drv->pm->resume(dev);
1046 } else {
1047 ret = platform_legacy_resume(dev);
1048 }
1049
1050 return ret;
1051}
1052
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001053#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001054
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001055#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001056
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001057int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001058{
1059 struct device_driver *drv = dev->driver;
1060 int ret = 0;
1061
1062 if (!drv)
1063 return 0;
1064
1065 if (drv->pm) {
1066 if (drv->pm->freeze)
1067 ret = drv->pm->freeze(dev);
1068 } else {
1069 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1070 }
1071
1072 return ret;
1073}
1074
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001075int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001076{
1077 struct device_driver *drv = dev->driver;
1078 int ret = 0;
1079
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001080 if (!drv)
1081 return 0;
1082
1083 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001084 if (drv->pm->thaw)
1085 ret = drv->pm->thaw(dev);
1086 } else {
1087 ret = platform_legacy_resume(dev);
1088 }
1089
1090 return ret;
1091}
1092
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001093int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001094{
1095 struct device_driver *drv = dev->driver;
1096 int ret = 0;
1097
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001098 if (!drv)
1099 return 0;
1100
1101 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001102 if (drv->pm->poweroff)
1103 ret = drv->pm->poweroff(dev);
1104 } else {
1105 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1106 }
1107
1108 return ret;
1109}
1110
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001111int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001112{
1113 struct device_driver *drv = dev->driver;
1114 int ret = 0;
1115
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001116 if (!drv)
1117 return 0;
1118
1119 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001120 if (drv->pm->restore)
1121 ret = drv->pm->restore(dev);
1122 } else {
1123 ret = platform_legacy_resume(dev);
1124 }
1125
1126 return ret;
1127}
1128
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001129#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001130
Nipun Gupta07397df2018-04-28 08:21:58 +05301131int platform_dma_configure(struct device *dev)
1132{
1133 enum dev_dma_attr attr;
1134 int ret = 0;
1135
1136 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001137 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301138 } else if (has_acpi_companion(dev)) {
1139 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001140 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301141 }
1142
1143 return ret;
1144}
1145
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001146static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001147 .runtime_suspend = pm_generic_runtime_suspend,
1148 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001149 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001150};
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152struct bus_type platform_bus_type = {
1153 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001154 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001156 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301157 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001158 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001160EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162int __init platform_bus_init(void)
1163{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001164 int error;
1165
Magnus Damm13977092009-03-30 14:37:25 -07001166 early_platform_cleanup();
1167
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001168 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301169 if (error) {
1170 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001171 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301172 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001173 error = bus_register(&platform_bus_type);
1174 if (error)
1175 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001176 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001177 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Magnus Damm13977092009-03-30 14:37:25 -07001180static __initdata LIST_HEAD(early_platform_driver_list);
1181static __initdata LIST_HEAD(early_platform_device_list);
1182
1183/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001184 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001185 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001186 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001187 *
1188 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001189 */
1190int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1191 char *buf)
1192{
Magnus Dammc60e0502009-11-27 17:38:51 +09001193 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001194 int n;
1195
1196 /* Simply add the driver to the end of the global list.
1197 * Drivers will by default be put on the list in compiled-in order.
1198 */
1199 if (!epdrv->list.next) {
1200 INIT_LIST_HEAD(&epdrv->list);
1201 list_add_tail(&epdrv->list, &early_platform_driver_list);
1202 }
1203
1204 /* If the user has specified device then make sure the driver
1205 * gets prioritized. The driver of the last device specified on
1206 * command line will be put first on the list.
1207 */
1208 n = strlen(epdrv->pdrv->driver.name);
1209 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1210 list_move(&epdrv->list, &early_platform_driver_list);
1211
Magnus Dammc60e0502009-11-27 17:38:51 +09001212 /* Allow passing parameters after device name */
1213 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001214 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001215 else {
1216 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1217 &tmp, 10);
1218
1219 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1220 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1221 n = 0;
1222 } else
1223 n += strcspn(&buf[n + 1], ",") + 1;
1224 }
1225
1226 if (buf[n] == ',')
1227 n++;
1228
1229 if (epdrv->bufsize) {
1230 memcpy(epdrv->buffer, &buf[n],
1231 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1232 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1233 }
Magnus Damm13977092009-03-30 14:37:25 -07001234 }
1235
1236 return 0;
1237}
1238
1239/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001240 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001241 * @devs: array of early platform devices to add
1242 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001243 *
1244 * Used by early architecture code to register early platform devices and
1245 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001246 */
1247void __init early_platform_add_devices(struct platform_device **devs, int num)
1248{
1249 struct device *dev;
1250 int i;
1251
1252 /* simply add the devices to list */
1253 for (i = 0; i < num; i++) {
1254 dev = &devs[i]->dev;
1255
1256 if (!dev->devres_head.next) {
Rafael J. Wysockibed2b422012-08-06 01:45:11 +02001257 pm_runtime_early_init(dev);
Magnus Damm13977092009-03-30 14:37:25 -07001258 INIT_LIST_HEAD(&dev->devres_head);
1259 list_add_tail(&dev->devres_head,
1260 &early_platform_device_list);
1261 }
1262 }
1263}
1264
1265/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001266 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001267 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001268 *
1269 * Used by architecture code to register all early platform drivers
1270 * for a certain class. If omitted then only early platform drivers
1271 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001272 */
1273void __init early_platform_driver_register_all(char *class_str)
1274{
1275 /* The "class_str" parameter may or may not be present on the kernel
1276 * command line. If it is present then there may be more than one
1277 * matching parameter.
1278 *
1279 * Since we register our early platform drivers using early_param()
1280 * we need to make sure that they also get registered in the case
1281 * when the parameter is missing from the kernel command line.
1282 *
1283 * We use parse_early_options() to make sure the early_param() gets
1284 * called at least once. The early_param() may be called more than
1285 * once since the name of the preferred device may be specified on
1286 * the kernel command line. early_platform_driver_register() handles
1287 * this case for us.
1288 */
1289 parse_early_options(class_str);
1290}
1291
1292/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001293 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001294 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001295 * @id: id to match against
1296 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001297static struct platform_device * __init
Magnus Damm13977092009-03-30 14:37:25 -07001298early_platform_match(struct early_platform_driver *epdrv, int id)
1299{
1300 struct platform_device *pd;
1301
1302 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1303 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1304 if (pd->id == id)
1305 return pd;
1306
1307 return NULL;
1308}
1309
1310/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001311 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001312 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001313 * @id: return true if id or above exists
1314 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001315static int __init early_platform_left(struct early_platform_driver *epdrv,
Magnus Damm13977092009-03-30 14:37:25 -07001316 int id)
1317{
1318 struct platform_device *pd;
1319
1320 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1321 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1322 if (pd->id >= id)
1323 return 1;
1324
1325 return 0;
1326}
1327
1328/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001329 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001330 * @class_str: string to identify early platform driver class
1331 * @id: id to match against
1332 * @nr_probe: number of platform devices to successfully probe before exiting
1333 */
1334static int __init early_platform_driver_probe_id(char *class_str,
1335 int id,
1336 int nr_probe)
1337{
1338 struct early_platform_driver *epdrv;
1339 struct platform_device *match;
1340 int match_id;
1341 int n = 0;
1342 int left = 0;
1343
1344 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1345 /* only use drivers matching our class_str */
1346 if (strcmp(class_str, epdrv->class_str))
1347 continue;
1348
1349 if (id == -2) {
1350 match_id = epdrv->requested_id;
1351 left = 1;
1352
1353 } else {
1354 match_id = id;
1355 left += early_platform_left(epdrv, id);
1356
1357 /* skip requested id */
1358 switch (epdrv->requested_id) {
1359 case EARLY_PLATFORM_ID_ERROR:
1360 case EARLY_PLATFORM_ID_UNSET:
1361 break;
1362 default:
1363 if (epdrv->requested_id == id)
1364 match_id = EARLY_PLATFORM_ID_UNSET;
1365 }
1366 }
1367
1368 switch (match_id) {
1369 case EARLY_PLATFORM_ID_ERROR:
Fabio Porcedda0258e182013-03-26 10:35:16 +01001370 pr_warn("%s: unable to parse %s parameter\n",
1371 class_str, epdrv->pdrv->driver.name);
Magnus Damm13977092009-03-30 14:37:25 -07001372 /* fall-through */
1373 case EARLY_PLATFORM_ID_UNSET:
1374 match = NULL;
1375 break;
1376 default:
1377 match = early_platform_match(epdrv, match_id);
1378 }
1379
1380 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001381 /*
1382 * Set up a sensible init_name to enable
1383 * dev_name() and others to be used before the
1384 * rest of the driver core is initialized.
1385 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001386 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001387 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001388 match->dev.init_name =
1389 kasprintf(GFP_KERNEL, "%s.%d",
1390 match->name,
1391 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001392 else
Paul Mundtbd050862010-03-29 15:51:35 +09001393 match->dev.init_name =
1394 kasprintf(GFP_KERNEL, "%s",
1395 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001396
Paul Mundta636ee72010-03-09 06:57:53 +00001397 if (!match->dev.init_name)
1398 return -ENOMEM;
1399 }
Paul Mundtbd050862010-03-29 15:51:35 +09001400
Magnus Damm13977092009-03-30 14:37:25 -07001401 if (epdrv->pdrv->probe(match))
Fabio Porcedda0258e182013-03-26 10:35:16 +01001402 pr_warn("%s: unable to probe %s early.\n",
1403 class_str, match->name);
Magnus Damm13977092009-03-30 14:37:25 -07001404 else
1405 n++;
1406 }
1407
1408 if (n >= nr_probe)
1409 break;
1410 }
1411
1412 if (left)
1413 return n;
1414 else
1415 return -ENODEV;
1416}
1417
1418/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001419 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001420 * @class_str: string to identify early platform driver class
1421 * @nr_probe: number of platform devices to successfully probe before exiting
1422 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001423 *
1424 * Used by architecture code to probe registered early platform drivers
1425 * within a certain class. For probe to happen a registered early platform
1426 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001427 */
1428int __init early_platform_driver_probe(char *class_str,
1429 int nr_probe,
1430 int user_only)
1431{
1432 int k, n, i;
1433
1434 n = 0;
1435 for (i = -2; n < nr_probe; i++) {
1436 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1437
1438 if (k < 0)
1439 break;
1440
1441 n += k;
1442
1443 if (user_only)
1444 break;
1445 }
1446
1447 return n;
1448}
1449
1450/**
1451 * early_platform_cleanup - clean up early platform code
1452 */
1453void __init early_platform_cleanup(void)
1454{
1455 struct platform_device *pd, *pd2;
1456
1457 /* clean up the devres list used to chain devices */
1458 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1459 dev.devres_head) {
1460 list_del(&pd->dev.devres_head);
1461 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1462 }
1463}
1464