blob: 41b91af95afbd31c2220981b7255b4460a03a3ce [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{
237 if (pdev)
238 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
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700450 if (pdev) {
Jerome Marchandc90aab92016-07-25 16:13:32 +0200451 device_remove_properties(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700452 device_del(&pdev->dev);
453
454 if (pdev->id_auto) {
455 ida_simple_remove(&platform_devid_ida, pdev->id);
456 pdev->id = PLATFORM_DEVID_AUTO;
457 }
458
459 for (i = 0; i < pdev->num_resources; i++) {
460 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100461 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700462 release_resource(r);
463 }
464 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500465}
466EXPORT_SYMBOL_GPL(platform_device_del);
467
468/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800469 * platform_device_register - add a platform-level device
470 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000471 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800472int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000473{
474 device_initialize(&pdev->dev);
Kumar Galaa77ce812011-06-10 01:52:57 -0500475 arch_setup_pdev_archdata(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000476 return platform_device_add(pdev);
477}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500478EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800481 * platform_device_unregister - unregister a platform-level device
482 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800484 * Unregistration is done in 2 steps. First we release all resources
485 * and remove it from the subsystem, then we drop reference count by
486 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800488void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500490 platform_device_del(pdev);
491 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500493EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200496 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200497 * resources and platform-specific data
498 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200499 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700500 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200501 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700502 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200503struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100504 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700505{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200506 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700507 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700508
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200509 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200510 if (!pdev)
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200511 goto err_alloc;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700512
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200513 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100514 pdev->dev.fwnode = pdevinfo->fwnode;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700515
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200516 if (pdevinfo->dma_mask) {
517 /*
518 * This memory isn't freed when the device is put,
519 * I don't have a nice idea for that though. Conceptually
520 * dma_mask in struct device should not be a pointer.
521 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
522 */
523 pdev->dev.dma_mask =
524 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
525 if (!pdev->dev.dma_mask)
526 goto err;
527
528 *pdev->dev.dma_mask = pdevinfo->dma_mask;
529 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
530 }
531
532 ret = platform_device_add_resources(pdev,
533 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400534 if (ret)
535 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700536
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200537 ret = platform_device_add_data(pdev,
538 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400539 if (ret)
540 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200541
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300542 if (pdevinfo->properties) {
543 ret = platform_device_add_properties(pdev,
544 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200545 if (ret)
546 goto err;
547 }
548
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200549 ret = platform_device_add(pdev);
550 if (ret) {
551err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100552 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200553 kfree(pdev->dev.dma_mask);
554
555err_alloc:
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200556 platform_device_put(pdev);
557 return ERR_PTR(ret);
558 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700559
560 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700561}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200562EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700563
Russell King00d3dcd2005-11-09 17:23:39 +0000564static int platform_drv_probe(struct device *_dev)
565{
566 struct platform_driver *drv = to_platform_driver(_dev->driver);
567 struct platform_device *dev = to_platform_device(_dev);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100568 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000569
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200570 ret = of_clk_set_defaults(_dev->of_node, false);
571 if (ret < 0)
572 return ret;
573
Ulf Hanssoncb518412014-09-19 20:27:38 +0200574 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200575 if (ret)
576 goto out;
577
578 if (drv->probe) {
579 ret = drv->probe(dev);
580 if (ret)
581 dev_pm_domain_detach(_dev, true);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200582 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100583
Ulf Hansson88a97692018-04-26 10:53:06 +0200584out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200585 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
586 dev_warn(_dev, "probe deferral not supported\n");
587 ret = -ENXIO;
588 }
589
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100590 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000591}
592
David Brownellc67334f2006-11-16 23:28:47 -0800593static int platform_drv_probe_fail(struct device *_dev)
594{
595 return -ENXIO;
596}
597
Russell King00d3dcd2005-11-09 17:23:39 +0000598static int platform_drv_remove(struct device *_dev)
599{
600 struct platform_driver *drv = to_platform_driver(_dev->driver);
601 struct platform_device *dev = to_platform_device(_dev);
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200602 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000603
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200604 if (drv->remove)
605 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200606 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100607
608 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000609}
610
611static void platform_drv_shutdown(struct device *_dev)
612{
613 struct platform_driver *drv = to_platform_driver(_dev->driver);
614 struct platform_device *dev = to_platform_device(_dev);
615
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200616 if (drv->shutdown)
617 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000618}
619
Russell King00d3dcd2005-11-09 17:23:39 +0000620/**
Libo Chen94470572013-05-25 12:40:50 +0800621 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800622 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700623 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000624 */
Libo Chen94470572013-05-25 12:40:50 +0800625int __platform_driver_register(struct platform_driver *drv,
626 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000627{
Libo Chen94470572013-05-25 12:40:50 +0800628 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000629 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200630 drv->driver.probe = platform_drv_probe;
631 drv->driver.remove = platform_drv_remove;
632 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200633
Russell King00d3dcd2005-11-09 17:23:39 +0000634 return driver_register(&drv->driver);
635}
Libo Chen94470572013-05-25 12:40:50 +0800636EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000637
638/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000639 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800640 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000641 */
642void platform_driver_unregister(struct platform_driver *drv)
643{
644 driver_unregister(&drv->driver);
645}
646EXPORT_SYMBOL_GPL(platform_driver_unregister);
647
David Brownellc67334f2006-11-16 23:28:47 -0800648/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100649 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800650 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200651 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100652 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800653 *
654 * Use this instead of platform_driver_register() when you know the device
655 * is not hotpluggable and has already been registered, and you want to
656 * remove its run-once probe() infrastructure from memory after the driver
657 * has bound to the device.
658 *
659 * One typical use for this would be with drivers for controllers integrated
660 * into system-on-chip processors, where the controller devices have been
661 * configured as part of board setup.
662 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200663 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100664 *
David Brownellc67334f2006-11-16 23:28:47 -0800665 * Returns zero if the driver registered and bound to a device, else returns
666 * a negative error code and with the driver not registered.
667 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100668int __init_or_module __platform_driver_probe(struct platform_driver *drv,
669 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800670{
671 int retval, code;
672
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700673 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
674 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
675 drv->driver.name, __func__);
676 return -EINVAL;
677 }
678
679 /*
680 * We have to run our probes synchronously because we check if
681 * we find any devices to bind to and exit with error if there
682 * are any.
683 */
684 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
685
Johan Hovold3f9120b2013-09-23 16:27:26 +0200686 /*
687 * Prevent driver from requesting probe deferral to avoid further
688 * futile probe attempts.
689 */
690 drv->prevent_deferred_probe = true;
691
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700692 /* make sure driver won't have bind/unbind attributes */
693 drv->driver.suppress_bind_attrs = true;
694
David Brownellc67334f2006-11-16 23:28:47 -0800695 /* temporary section violation during probe() */
696 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100697 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800698
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700699 /*
700 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800701 * the list of drivers in order to probe new devices. Check to see
702 * if the probe was successful, and make sure any forced probes of
703 * new devices fail.
704 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700705 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800706 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800707 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800708 retval = -ENODEV;
709 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700710 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800711
712 if (code != retval)
713 platform_driver_unregister(drv);
714 return retval;
715}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100716EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800718/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100719 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800720 * @driver: platform driver structure
721 * @probe: the driver probe routine, probably from an __init section
722 * @res: set of resources that needs to be allocated for the device
723 * @n_res: number of resources
724 * @data: platform specific data for this platform device
725 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100726 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800727 *
728 * Use this in legacy-style modules that probe hardware directly and
729 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200730 *
731 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800732 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100733struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800734 struct platform_driver *driver,
735 int (*probe)(struct platform_device *),
736 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100737 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800738{
739 struct platform_device *pdev;
740 int error;
741
742 pdev = platform_device_alloc(driver->driver.name, -1);
743 if (!pdev) {
744 error = -ENOMEM;
745 goto err_out;
746 }
747
Anton Vorontsov807508c2010-09-07 17:31:54 +0400748 error = platform_device_add_resources(pdev, res, n_res);
749 if (error)
750 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800751
Anton Vorontsov807508c2010-09-07 17:31:54 +0400752 error = platform_device_add_data(pdev, data, size);
753 if (error)
754 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800755
756 error = platform_device_add(pdev);
757 if (error)
758 goto err_pdev_put;
759
Wolfram Sang291f6532014-10-28 17:40:42 +0100760 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800761 if (error)
762 goto err_pdev_del;
763
764 return pdev;
765
766err_pdev_del:
767 platform_device_del(pdev);
768err_pdev_put:
769 platform_device_put(pdev);
770err_out:
771 return ERR_PTR(error);
772}
Wolfram Sang291f6532014-10-28 17:40:42 +0100773EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800774
Thierry Redingdbe22562015-09-25 17:29:04 +0200775/**
776 * __platform_register_drivers - register an array of platform drivers
777 * @drivers: an array of drivers to register
778 * @count: the number of drivers to register
779 * @owner: module owning the drivers
780 *
781 * Registers platform drivers specified by an array. On failure to register a
782 * driver, all previously registered drivers will be unregistered. Callers of
783 * this API should use platform_unregister_drivers() to unregister drivers in
784 * the reverse order.
785 *
786 * Returns: 0 on success or a negative error code on failure.
787 */
788int __platform_register_drivers(struct platform_driver * const *drivers,
789 unsigned int count, struct module *owner)
790{
791 unsigned int i;
792 int err;
793
794 for (i = 0; i < count; i++) {
795 pr_debug("registering platform driver %ps\n", drivers[i]);
796
797 err = __platform_driver_register(drivers[i], owner);
798 if (err < 0) {
799 pr_err("failed to register platform driver %ps: %d\n",
800 drivers[i], err);
801 goto error;
802 }
803 }
804
805 return 0;
806
807error:
808 while (i--) {
809 pr_debug("unregistering platform driver %ps\n", drivers[i]);
810 platform_driver_unregister(drivers[i]);
811 }
812
813 return err;
814}
815EXPORT_SYMBOL_GPL(__platform_register_drivers);
816
817/**
818 * platform_unregister_drivers - unregister an array of platform drivers
819 * @drivers: an array of drivers to unregister
820 * @count: the number of drivers to unregister
821 *
822 * Unegisters platform drivers specified by an array. This is typically used
823 * to complement an earlier call to platform_register_drivers(). Drivers are
824 * unregistered in the reverse order in which they were registered.
825 */
826void platform_unregister_drivers(struct platform_driver * const *drivers,
827 unsigned int count)
828{
829 while (count--) {
830 pr_debug("unregistering platform driver %ps\n", drivers[count]);
831 platform_driver_unregister(drivers[count]);
832 }
833}
834EXPORT_SYMBOL_GPL(platform_unregister_drivers);
835
David Brownella0245f72006-05-29 10:37:33 -0700836/* modalias support enables more hands-off userspace setup:
837 * (a) environment variable lets new-style hotplug events work once system is
838 * fully running: "modprobe $MODALIAS"
839 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
840 * mishandled before system is fully running: "modprobe $(cat modalias)"
841 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800842static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
843 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700844{
845 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800846 int len;
847
Rob Herring0634c292017-03-22 09:16:27 -0500848 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +0800849 if (len != -ENODEV)
850 return len;
851
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800852 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
853 if (len != -ENODEV)
854 return len;
855
856 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700857
858 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
859}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700860static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -0700861
Kim Phillips3d713e02014-06-02 19:42:58 -0500862static ssize_t driver_override_store(struct device *dev,
863 struct device_attribute *attr,
864 const char *buf, size_t count)
865{
866 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700867 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -0500868
Nicolai Stangebf563b02017-09-11 09:45:42 +0200869 /* We need to keep extra room for a newline */
870 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -0500871 return -EINVAL;
872
873 driver_override = kstrndup(buf, count, GFP_KERNEL);
874 if (!driver_override)
875 return -ENOMEM;
876
877 cp = strchr(driver_override, '\n');
878 if (cp)
879 *cp = '\0';
880
Adrian Salido626553972017-04-25 16:55:26 -0700881 device_lock(dev);
882 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -0500883 if (strlen(driver_override)) {
884 pdev->driver_override = driver_override;
885 } else {
886 kfree(driver_override);
887 pdev->driver_override = NULL;
888 }
Adrian Salido626553972017-04-25 16:55:26 -0700889 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -0500890
891 kfree(old);
892
893 return count;
894}
895
896static ssize_t driver_override_show(struct device *dev,
897 struct device_attribute *attr, char *buf)
898{
899 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700900 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500901
Adrian Salido626553972017-04-25 16:55:26 -0700902 device_lock(dev);
903 len = sprintf(buf, "%s\n", pdev->driver_override);
904 device_unlock(dev);
905 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500906}
907static DEVICE_ATTR_RW(driver_override);
908
909
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700910static struct attribute *platform_dev_attrs[] = {
911 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -0500912 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700913 NULL,
David Brownella0245f72006-05-29 10:37:33 -0700914};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700915ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -0700916
Kay Sievers7eff2e72007-08-14 15:15:12 +0200917static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -0700918{
919 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600920 int rc;
921
922 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +0100923 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -0600924 if (rc != -ENODEV)
925 return rc;
David Brownella0245f72006-05-29 10:37:33 -0700926
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800927 rc = acpi_device_uevent_modalias(dev, env);
928 if (rc != -ENODEV)
929 return rc;
930
Eric Miao57fee4a2009-02-04 11:52:40 +0800931 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Sebastian Andrzej Siewior0a268132011-08-15 16:51:22 +0200932 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700933 return 0;
934}
935
Eric Miao57fee4a2009-02-04 11:52:40 +0800936static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +0100937 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +0800938 struct platform_device *pdev)
939{
940 while (id->name[0]) {
941 if (strcmp(pdev->name, id->name) == 0) {
942 pdev->id_entry = id;
943 return id;
944 }
945 id++;
946 }
947 return NULL;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800951 * platform_match - bind platform device to platform driver.
952 * @dev: device.
953 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800955 * Platform device IDs are assumed to be encoded like this:
956 * "<name><instance>", where <name> is a short description of the type of
957 * device, like "pci" or "floppy", and <instance> is the enumerated
958 * instance of the device, like '0' or '42'. Driver IDs are simply
959 * "<name>". So, extract the <name> from the platform_device structure,
960 * and compare it against the name of the driver. Return whether they match
961 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800963static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800965 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +0800966 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Kim Phillips3d713e02014-06-02 19:42:58 -0500968 /* When driver_override is set, only bind to the matching driver */
969 if (pdev->driver_override)
970 return !strcmp(pdev->driver_override, drv->name);
971
Grant Likely05212152010-06-08 07:48:20 -0600972 /* Attempt an OF style match first */
973 if (of_driver_match_device(dev, drv))
974 return 1;
975
Mika Westerberg91e56872012-10-31 22:45:02 +0100976 /* Then try ACPI style match */
977 if (acpi_driver_match_device(dev, drv))
978 return 1;
979
Grant Likely05212152010-06-08 07:48:20 -0600980 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +0800981 if (pdrv->id_table)
982 return platform_match_id(pdrv->id_table, pdev) != NULL;
983
984 /* fall-back to driver name match */
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100985 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Rafael J. Wysocki25e18492008-05-21 01:40:43 +0200988#ifdef CONFIG_PM_SLEEP
989
990static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Magnus Damm783ea7d2009-06-04 22:13:33 +0200992 struct platform_driver *pdrv = to_platform_driver(dev->driver);
993 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int ret = 0;
995
Magnus Damm783ea7d2009-06-04 22:13:33 +0200996 if (dev->driver && pdrv->suspend)
997 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -0700998
999 return ret;
1000}
1001
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001002static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001004 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1005 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int ret = 0;
1007
Magnus Damm783ea7d2009-06-04 22:13:33 +02001008 if (dev->driver && pdrv->resume)
1009 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return ret;
1012}
1013
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001014#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001015
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001016#ifdef CONFIG_SUSPEND
1017
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001018int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001019{
1020 struct device_driver *drv = dev->driver;
1021 int ret = 0;
1022
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001023 if (!drv)
1024 return 0;
1025
1026 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001027 if (drv->pm->suspend)
1028 ret = drv->pm->suspend(dev);
1029 } else {
1030 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1031 }
1032
1033 return ret;
1034}
1035
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001036int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001037{
1038 struct device_driver *drv = dev->driver;
1039 int ret = 0;
1040
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001041 if (!drv)
1042 return 0;
1043
1044 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001045 if (drv->pm->resume)
1046 ret = drv->pm->resume(dev);
1047 } else {
1048 ret = platform_legacy_resume(dev);
1049 }
1050
1051 return ret;
1052}
1053
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001054#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001055
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001056#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001057
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001058int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001059{
1060 struct device_driver *drv = dev->driver;
1061 int ret = 0;
1062
1063 if (!drv)
1064 return 0;
1065
1066 if (drv->pm) {
1067 if (drv->pm->freeze)
1068 ret = drv->pm->freeze(dev);
1069 } else {
1070 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1071 }
1072
1073 return ret;
1074}
1075
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001076int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001077{
1078 struct device_driver *drv = dev->driver;
1079 int ret = 0;
1080
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001081 if (!drv)
1082 return 0;
1083
1084 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001085 if (drv->pm->thaw)
1086 ret = drv->pm->thaw(dev);
1087 } else {
1088 ret = platform_legacy_resume(dev);
1089 }
1090
1091 return ret;
1092}
1093
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001094int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001095{
1096 struct device_driver *drv = dev->driver;
1097 int ret = 0;
1098
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001099 if (!drv)
1100 return 0;
1101
1102 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001103 if (drv->pm->poweroff)
1104 ret = drv->pm->poweroff(dev);
1105 } else {
1106 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1107 }
1108
1109 return ret;
1110}
1111
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001112int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001113{
1114 struct device_driver *drv = dev->driver;
1115 int ret = 0;
1116
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001117 if (!drv)
1118 return 0;
1119
1120 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001121 if (drv->pm->restore)
1122 ret = drv->pm->restore(dev);
1123 } else {
1124 ret = platform_legacy_resume(dev);
1125 }
1126
1127 return ret;
1128}
1129
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001130#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001131
Nipun Gupta07397df2018-04-28 08:21:58 +05301132int platform_dma_configure(struct device *dev)
1133{
1134 enum dev_dma_attr attr;
1135 int ret = 0;
1136
1137 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001138 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301139 } else if (has_acpi_companion(dev)) {
1140 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
1141 if (attr != DEV_DMA_NOT_SUPPORTED)
1142 ret = acpi_dma_configure(dev, attr);
1143 }
1144
1145 return ret;
1146}
1147
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001148static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001149 .runtime_suspend = pm_generic_runtime_suspend,
1150 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001151 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001152};
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154struct bus_type platform_bus_type = {
1155 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001156 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001158 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301159 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001160 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001162EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164int __init platform_bus_init(void)
1165{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001166 int error;
1167
Magnus Damm13977092009-03-30 14:37:25 -07001168 early_platform_cleanup();
1169
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001170 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301171 if (error) {
1172 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001173 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301174 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001175 error = bus_register(&platform_bus_type);
1176 if (error)
1177 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001178 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001179 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Christoph Hellwig6db37ad2018-10-02 07:21:31 -07001182#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
Christoph Hellwigc6d43812018-09-06 19:27:24 -04001183static u64 dma_default_get_required_mask(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1186 u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1187 u64 mask;
1188
1189 if (!high_totalram) {
1190 /* convert to mask just covering totalram */
1191 low_totalram = (1 << (fls(low_totalram) - 1));
1192 low_totalram += low_totalram - 1;
1193 mask = low_totalram;
1194 } else {
1195 high_totalram = (1 << (fls(high_totalram) - 1));
1196 high_totalram += high_totalram - 1;
1197 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1198 }
James Bottomleye88a0c22008-03-09 11:57:56 -05001199 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
Christoph Hellwigc6d43812018-09-06 19:27:24 -04001201
Christoph Hellwigc6d43812018-09-06 19:27:24 -04001202u64 dma_get_required_mask(struct device *dev)
1203{
1204 const struct dma_map_ops *ops = get_dma_ops(dev);
1205
1206 if (ops->get_required_mask)
1207 return ops->get_required_mask(dev);
1208 return dma_default_get_required_mask(dev);
1209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210EXPORT_SYMBOL_GPL(dma_get_required_mask);
1211#endif
Magnus Damm13977092009-03-30 14:37:25 -07001212
1213static __initdata LIST_HEAD(early_platform_driver_list);
1214static __initdata LIST_HEAD(early_platform_device_list);
1215
1216/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001217 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001218 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001219 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001220 *
1221 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001222 */
1223int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1224 char *buf)
1225{
Magnus Dammc60e0502009-11-27 17:38:51 +09001226 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001227 int n;
1228
1229 /* Simply add the driver to the end of the global list.
1230 * Drivers will by default be put on the list in compiled-in order.
1231 */
1232 if (!epdrv->list.next) {
1233 INIT_LIST_HEAD(&epdrv->list);
1234 list_add_tail(&epdrv->list, &early_platform_driver_list);
1235 }
1236
1237 /* If the user has specified device then make sure the driver
1238 * gets prioritized. The driver of the last device specified on
1239 * command line will be put first on the list.
1240 */
1241 n = strlen(epdrv->pdrv->driver.name);
1242 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1243 list_move(&epdrv->list, &early_platform_driver_list);
1244
Magnus Dammc60e0502009-11-27 17:38:51 +09001245 /* Allow passing parameters after device name */
1246 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001247 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001248 else {
1249 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1250 &tmp, 10);
1251
1252 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1253 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1254 n = 0;
1255 } else
1256 n += strcspn(&buf[n + 1], ",") + 1;
1257 }
1258
1259 if (buf[n] == ',')
1260 n++;
1261
1262 if (epdrv->bufsize) {
1263 memcpy(epdrv->buffer, &buf[n],
1264 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1265 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1266 }
Magnus Damm13977092009-03-30 14:37:25 -07001267 }
1268
1269 return 0;
1270}
1271
1272/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001273 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001274 * @devs: array of early platform devices to add
1275 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001276 *
1277 * Used by early architecture code to register early platform devices and
1278 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001279 */
1280void __init early_platform_add_devices(struct platform_device **devs, int num)
1281{
1282 struct device *dev;
1283 int i;
1284
1285 /* simply add the devices to list */
1286 for (i = 0; i < num; i++) {
1287 dev = &devs[i]->dev;
1288
1289 if (!dev->devres_head.next) {
Rafael J. Wysockibed2b422012-08-06 01:45:11 +02001290 pm_runtime_early_init(dev);
Magnus Damm13977092009-03-30 14:37:25 -07001291 INIT_LIST_HEAD(&dev->devres_head);
1292 list_add_tail(&dev->devres_head,
1293 &early_platform_device_list);
1294 }
1295 }
1296}
1297
1298/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001299 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001300 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001301 *
1302 * Used by architecture code to register all early platform drivers
1303 * for a certain class. If omitted then only early platform drivers
1304 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001305 */
1306void __init early_platform_driver_register_all(char *class_str)
1307{
1308 /* The "class_str" parameter may or may not be present on the kernel
1309 * command line. If it is present then there may be more than one
1310 * matching parameter.
1311 *
1312 * Since we register our early platform drivers using early_param()
1313 * we need to make sure that they also get registered in the case
1314 * when the parameter is missing from the kernel command line.
1315 *
1316 * We use parse_early_options() to make sure the early_param() gets
1317 * called at least once. The early_param() may be called more than
1318 * once since the name of the preferred device may be specified on
1319 * the kernel command line. early_platform_driver_register() handles
1320 * this case for us.
1321 */
1322 parse_early_options(class_str);
1323}
1324
1325/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001326 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001327 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001328 * @id: id to match against
1329 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001330static struct platform_device * __init
Magnus Damm13977092009-03-30 14:37:25 -07001331early_platform_match(struct early_platform_driver *epdrv, int id)
1332{
1333 struct platform_device *pd;
1334
1335 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1336 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1337 if (pd->id == id)
1338 return pd;
1339
1340 return NULL;
1341}
1342
1343/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001344 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001345 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001346 * @id: return true if id or above exists
1347 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001348static int __init early_platform_left(struct early_platform_driver *epdrv,
Magnus Damm13977092009-03-30 14:37:25 -07001349 int id)
1350{
1351 struct platform_device *pd;
1352
1353 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1354 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1355 if (pd->id >= id)
1356 return 1;
1357
1358 return 0;
1359}
1360
1361/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001362 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001363 * @class_str: string to identify early platform driver class
1364 * @id: id to match against
1365 * @nr_probe: number of platform devices to successfully probe before exiting
1366 */
1367static int __init early_platform_driver_probe_id(char *class_str,
1368 int id,
1369 int nr_probe)
1370{
1371 struct early_platform_driver *epdrv;
1372 struct platform_device *match;
1373 int match_id;
1374 int n = 0;
1375 int left = 0;
1376
1377 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1378 /* only use drivers matching our class_str */
1379 if (strcmp(class_str, epdrv->class_str))
1380 continue;
1381
1382 if (id == -2) {
1383 match_id = epdrv->requested_id;
1384 left = 1;
1385
1386 } else {
1387 match_id = id;
1388 left += early_platform_left(epdrv, id);
1389
1390 /* skip requested id */
1391 switch (epdrv->requested_id) {
1392 case EARLY_PLATFORM_ID_ERROR:
1393 case EARLY_PLATFORM_ID_UNSET:
1394 break;
1395 default:
1396 if (epdrv->requested_id == id)
1397 match_id = EARLY_PLATFORM_ID_UNSET;
1398 }
1399 }
1400
1401 switch (match_id) {
1402 case EARLY_PLATFORM_ID_ERROR:
Fabio Porcedda0258e182013-03-26 10:35:16 +01001403 pr_warn("%s: unable to parse %s parameter\n",
1404 class_str, epdrv->pdrv->driver.name);
Magnus Damm13977092009-03-30 14:37:25 -07001405 /* fall-through */
1406 case EARLY_PLATFORM_ID_UNSET:
1407 match = NULL;
1408 break;
1409 default:
1410 match = early_platform_match(epdrv, match_id);
1411 }
1412
1413 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001414 /*
1415 * Set up a sensible init_name to enable
1416 * dev_name() and others to be used before the
1417 * rest of the driver core is initialized.
1418 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001419 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001420 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001421 match->dev.init_name =
1422 kasprintf(GFP_KERNEL, "%s.%d",
1423 match->name,
1424 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001425 else
Paul Mundtbd050862010-03-29 15:51:35 +09001426 match->dev.init_name =
1427 kasprintf(GFP_KERNEL, "%s",
1428 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001429
Paul Mundta636ee72010-03-09 06:57:53 +00001430 if (!match->dev.init_name)
1431 return -ENOMEM;
1432 }
Paul Mundtbd050862010-03-29 15:51:35 +09001433
Magnus Damm13977092009-03-30 14:37:25 -07001434 if (epdrv->pdrv->probe(match))
Fabio Porcedda0258e182013-03-26 10:35:16 +01001435 pr_warn("%s: unable to probe %s early.\n",
1436 class_str, match->name);
Magnus Damm13977092009-03-30 14:37:25 -07001437 else
1438 n++;
1439 }
1440
1441 if (n >= nr_probe)
1442 break;
1443 }
1444
1445 if (left)
1446 return n;
1447 else
1448 return -ENODEV;
1449}
1450
1451/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001452 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001453 * @class_str: string to identify early platform driver class
1454 * @nr_probe: number of platform devices to successfully probe before exiting
1455 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001456 *
1457 * Used by architecture code to probe registered early platform drivers
1458 * within a certain class. For probe to happen a registered early platform
1459 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001460 */
1461int __init early_platform_driver_probe(char *class_str,
1462 int nr_probe,
1463 int user_only)
1464{
1465 int k, n, i;
1466
1467 n = 0;
1468 for (i = -2; n < nr_probe; i++) {
1469 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1470
1471 if (k < 0)
1472 break;
1473
1474 n += k;
1475
1476 if (user_only)
1477 break;
1478 }
1479
1480 return n;
1481}
1482
1483/**
1484 * early_platform_cleanup - clean up early platform code
1485 */
1486void __init early_platform_cleanup(void)
1487{
1488 struct platform_device *pd, *pd2;
1489
1490 /* clean up the devres list used to chain devices */
1491 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1492 dev.devres_head) {
1493 list_del(&pd->dev.devres_head);
1494 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1495 }
1496}
1497