blob: b6c6c7d97d5b2aa388baa153550ed7a4db3af022 [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 *
Mauro Carvalho Chehabfe34c892019-06-18 12:34:59 -03008 * Please see Documentation/driver-api/driver-model/platform.rst for more
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * information.
10 */
11
Andrew Mortondaa41222009-08-06 16:00:44 -070012#include <linux/string.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010013#include <linux/platform_device.h>
Grant Likely05212152010-06-08 07:48:20 -060014#include <linux/of_device.h>
Rob Herring9ec36ca2014-04-23 17:57:41 -050015#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/dma-mapping.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070019#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/slab.h>
Magnus Damm9d730222009-08-20 20:25:32 +020022#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020023#include <linux/pm_domain.h>
Jean Delvare689ae232012-07-27 22:14:59 +020024#include <linux/idr.h>
Mika Westerberg91e56872012-10-31 22:45:02 +010025#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020026#include <linux/clk/clk-conf.h>
Kim Phillips3d713e02014-06-02 19:42:58 -050027#include <linux/limits.h>
Mika Westerberg00bbc1d2015-11-30 17:11:38 +020028#include <linux/property.h>
Qian Cai967d3012019-01-03 15:29:05 -080029#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010031#include "base.h"
Rafael J. Wysockibed2b422012-08-06 01:45:11 +020032#include "power/power.h"
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010033
Jean Delvare689ae232012-07-27 22:14:59 +020034/* For automatically allocated device IDs */
35static DEFINE_IDA(platform_devid_ida);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct device platform_bus = {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010038 .init_name = "platform",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
Dmitry Torokhova96b2042005-12-10 01:36:28 -050040EXPORT_SYMBOL_GPL(platform_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080043 * platform_get_resource - get a resource for a device
44 * @dev: platform device
45 * @type: resource type
46 * @num: resource index
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080048struct resource *platform_get_resource(struct platform_device *dev,
49 unsigned int type, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int i;
52
53 for (i = 0; i < dev->num_resources; i++) {
54 struct resource *r = &dev->resource[i];
55
Magnus Dammc9f66162008-10-15 22:05:15 -070056 if (type == resource_type(r) && num-- == 0)
57 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59 return NULL;
60}
Dmitry Torokhova96b2042005-12-10 01:36:28 -050061EXPORT_SYMBOL_GPL(platform_get_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/**
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000064 * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform
65 * device
66 *
67 * @pdev: platform device to use both for memory resource lookup as well as
Bartosz Golaszewski7067c962019-04-01 10:16:35 +020068 * resource management
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000069 * @index: resource index
70 */
Bartosz Golaszewski837ccda2019-02-21 17:26:27 +010071#ifdef CONFIG_HAS_IOMEM
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000072void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev,
73 unsigned int index)
74{
75 struct resource *res;
76
77 res = platform_get_resource(pdev, IORESOURCE_MEM, index);
78 return devm_ioremap_resource(&pdev->dev, res);
79}
80EXPORT_SYMBOL_GPL(devm_platform_ioremap_resource);
Bartosz Golaszewski837ccda2019-02-21 17:26:27 +010081#endif /* CONFIG_HAS_IOMEM */
Bartosz Golaszewski7945f922019-02-20 11:12:39 +000082
Stephen Boyd7723f4c2019-07-29 22:38:43 -070083static int __platform_get_irq(struct platform_device *dev, unsigned int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +000085#ifdef CONFIG_SPARC
86 /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
87 if (!dev || num >= dev->archdata.num_irqs)
88 return -ENXIO;
89 return dev->archdata.irqs[num];
90#else
Rob Herring9ec36ca2014-04-23 17:57:41 -050091 struct resource *r;
Guenter Roeckaff008a2014-06-17 15:51:02 -070092 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
93 int ret;
94
95 ret = of_irq_get(dev->dev.of_node, num);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +030096 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -070097 return ret;
98 }
Rob Herring9ec36ca2014-04-23 17:57:41 -050099
100 r = platform_get_resource(dev, IORESOURCE_IRQ, num);
Agustin Vega-Friasd44fa3d2017-02-02 18:23:58 -0500101 if (has_acpi_companion(&dev->dev)) {
102 if (r && r->flags & IORESOURCE_DISABLED) {
103 int ret;
104
105 ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
106 if (ret)
107 return ret;
108 }
109 }
110
Linus Walleij7085a742015-02-18 17:12:18 +0100111 /*
112 * The resources may pass trigger flags to the irqs that need
113 * to be set up. It so happens that the trigger flags for
114 * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
115 * settings.
116 */
Guenter Roeck60ca5e02016-09-13 20:32:44 -0700117 if (r && r->flags & IORESOURCE_BITS) {
118 struct irq_data *irqd;
119
120 irqd = irq_get_irq_data(r->start);
121 if (!irqd)
122 return -ENXIO;
123 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Enrico Granatadaaef252019-02-11 11:01:12 -0800126 if (r)
127 return r->start;
128
129 /*
130 * For the index 0 interrupt, allow falling back to GpioInt
131 * resources. While a device could have both Interrupt and GpioInt
132 * resources, making this fallback ambiguous, in many common cases
133 * the device will only expose one IRQ, and this fallback
134 * allows a common code path across either kind of resource.
135 */
Brian Norris46c42d82019-07-29 13:49:54 -0700136 if (num == 0 && has_acpi_companion(&dev->dev)) {
137 int ret = acpi_dev_gpio_irq_get(ACPI_COMPANION(&dev->dev), num);
138
139 /* Our callers expect -ENXIO for missing IRQs. */
140 if (ret >= 0 || ret == -EPROBE_DEFER)
141 return ret;
142 }
Enrico Granatadaaef252019-02-11 11:01:12 -0800143
144 return -ENXIO;
Andreas Larsson5cf8f7d2012-10-29 23:26:56 +0000145#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700147
148/**
149 * platform_get_irq - get an IRQ for a device
150 * @dev: platform device
151 * @num: IRQ number index
152 *
153 * Gets an IRQ for a platform device and prints an error message if finding the
154 * IRQ fails. Device drivers should check the return value for errors so as to
155 * not pass a negative integer value to the request_irq() APIs.
156 *
157 * Example:
158 * int irq = platform_get_irq(pdev, 0);
159 * if (irq < 0)
160 * return irq;
161 *
162 * Return: IRQ number on success, negative error number on failure.
163 */
164int platform_get_irq(struct platform_device *dev, unsigned int num)
165{
166 int ret;
167
168 ret = __platform_get_irq(dev, num);
169 if (ret < 0 && ret != -EPROBE_DEFER)
170 dev_err(&dev->dev, "IRQ index %u not found\n", num);
171
172 return ret;
173}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500174EXPORT_SYMBOL_GPL(platform_get_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/**
Thierry Reding8973ea42019-08-28 10:34:10 +0200177 * platform_get_irq_optional - get an optional IRQ for a device
178 * @dev: platform device
179 * @num: IRQ number index
180 *
181 * Gets an IRQ for a platform device. Device drivers should check the return
182 * value for errors so as to not pass a negative integer value to the
183 * request_irq() APIs. This is the same as platform_get_irq(), except that it
184 * does not print an error message if an IRQ can not be obtained.
185 *
186 * Example:
187 * int irq = platform_get_irq_optional(pdev, 0);
188 * if (irq < 0)
189 * return irq;
190 *
191 * Return: IRQ number on success, negative error number on failure.
192 */
193int platform_get_irq_optional(struct platform_device *dev, unsigned int num)
194{
195 return __platform_get_irq(dev, num);
196}
Thierry Redingd9430f92019-08-29 09:29:32 +0200197EXPORT_SYMBOL_GPL(platform_get_irq_optional);
Thierry Reding8973ea42019-08-28 10:34:10 +0200198
199/**
Stephen Boyd4b835552016-01-06 17:12:47 -0800200 * platform_irq_count - Count the number of IRQs a platform device uses
201 * @dev: platform device
202 *
203 * Return: Number of IRQs a platform device uses or EPROBE_DEFER
204 */
205int platform_irq_count(struct platform_device *dev)
206{
207 int ret, nr = 0;
208
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700209 while ((ret = __platform_get_irq(dev, nr)) >= 0)
Stephen Boyd4b835552016-01-06 17:12:47 -0800210 nr++;
211
212 if (ret == -EPROBE_DEFER)
213 return ret;
214
215 return nr;
216}
217EXPORT_SYMBOL_GPL(platform_irq_count);
218
219/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800220 * platform_get_resource_byname - get a resource for a device by name
221 * @dev: platform device
222 * @type: resource type
223 * @name: resource name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800225struct resource *platform_get_resource_byname(struct platform_device *dev,
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200226 unsigned int type,
227 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int i;
230
231 for (i = 0; i < dev->num_resources; i++) {
232 struct resource *r = &dev->resource[i];
233
Peter Ujfalusi1b8cb922012-08-23 17:10:00 +0300234 if (unlikely(!r->name))
235 continue;
236
Magnus Dammc9f66162008-10-15 22:05:15 -0700237 if (type == resource_type(r) && !strcmp(r->name, name))
238 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 return NULL;
241}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500242EXPORT_SYMBOL_GPL(platform_get_resource_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/**
Wolfram Sangd6ff8552012-11-13 17:47:13 +0100245 * platform_get_irq_byname - get an IRQ for a device by name
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800246 * @dev: platform device
247 * @name: IRQ name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Linus Walleijc0afe7b2009-04-27 02:38:16 +0200249int platform_get_irq_byname(struct platform_device *dev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Grygorii Strashkoad696742014-05-20 13:42:02 +0300251 struct resource *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Guenter Roeckaff008a2014-06-17 15:51:02 -0700253 if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
254 int ret;
255
256 ret = of_irq_get_byname(dev->dev.of_node, name);
Sergei Shtylyove330b9a2016-07-04 01:04:24 +0300257 if (ret > 0 || ret == -EPROBE_DEFER)
Guenter Roeckaff008a2014-06-17 15:51:02 -0700258 return ret;
259 }
Grygorii Strashkoad696742014-05-20 13:42:02 +0300260
261 r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
Stephen Boyd7723f4c2019-07-29 22:38:43 -0700262 if (r)
263 return r->start;
264
265 dev_err(&dev->dev, "IRQ %s not found\n", name);
266 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500268EXPORT_SYMBOL_GPL(platform_get_irq_byname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800271 * platform_add_devices - add a numbers of platform devices
272 * @devs: array of platform devices to add
273 * @num: number of platform devices in array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275int platform_add_devices(struct platform_device **devs, int num)
276{
277 int i, ret = 0;
278
279 for (i = 0; i < num; i++) {
280 ret = platform_device_register(devs[i]);
281 if (ret) {
282 while (--i >= 0)
283 platform_device_unregister(devs[i]);
284 break;
285 }
286 }
287
288 return ret;
289}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500290EXPORT_SYMBOL_GPL(platform_add_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Russell King37c12e72005-11-05 21:19:33 +0000292struct platform_object {
293 struct platform_device pdev;
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200294 char name[];
Russell King37c12e72005-11-05 21:19:33 +0000295};
296
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200297/*
298 * Set up default DMA mask for platform devices if the they weren't
299 * previously set by the architecture / DT.
300 */
301static void setup_pdev_dma_masks(struct platform_device *pdev)
302{
303 if (!pdev->dev.coherent_dma_mask)
304 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
305 if (!pdev->dma_mask)
306 pdev->dma_mask = DMA_BIT_MASK(32);
307 if (!pdev->dev.dma_mask)
308 pdev->dev.dma_mask = &pdev->dma_mask;
309};
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000312 * platform_device_put - destroy a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800313 * @pdev: platform device to free
Russell King37c12e72005-11-05 21:19:33 +0000314 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800315 * Free all memory associated with a platform device. This function must
316 * _only_ be externally called in error cases. All other usage is a bug.
Russell King37c12e72005-11-05 21:19:33 +0000317 */
318void platform_device_put(struct platform_device *pdev)
319{
Andy Shevchenko99fef582018-12-03 20:21:41 +0200320 if (!IS_ERR_OR_NULL(pdev))
Russell King37c12e72005-11-05 21:19:33 +0000321 put_device(&pdev->dev);
322}
323EXPORT_SYMBOL_GPL(platform_device_put);
324
325static void platform_device_release(struct device *dev)
326{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800327 struct platform_object *pa = container_of(dev, struct platform_object,
328 pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000329
Grant Likely7096d042010-10-20 11:45:13 -0600330 of_device_node_put(&pa->pdev.dev);
Russell King37c12e72005-11-05 21:19:33 +0000331 kfree(pa->pdev.dev.platform_data);
Samuel Ortize710d7d2011-04-08 00:43:01 +0200332 kfree(pa->pdev.mfd_cell);
Russell King37c12e72005-11-05 21:19:33 +0000333 kfree(pa->pdev.resource);
Kim Phillips3d713e02014-06-02 19:42:58 -0500334 kfree(pa->pdev.driver_override);
Russell King37c12e72005-11-05 21:19:33 +0000335 kfree(pa);
336}
337
338/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000339 * platform_device_alloc - create a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800340 * @name: base name of the device we're adding
341 * @id: instance id
Russell King37c12e72005-11-05 21:19:33 +0000342 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800343 * Create a platform device object which can have other objects attached
344 * to it, and which will have attached objects freed when it is released.
Russell King37c12e72005-11-05 21:19:33 +0000345 */
Jean Delvare1359555e2007-09-09 12:54:16 +0200346struct platform_device *platform_device_alloc(const char *name, int id)
Russell King37c12e72005-11-05 21:19:33 +0000347{
348 struct platform_object *pa;
349
Yann Droneaud1cec24c2014-05-30 22:02:47 +0200350 pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
Russell King37c12e72005-11-05 21:19:33 +0000351 if (pa) {
352 strcpy(pa->name, name);
353 pa->pdev.name = pa->name;
354 pa->pdev.id = id;
355 device_initialize(&pa->pdev.dev);
356 pa->pdev.dev.release = platform_device_release;
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200357 setup_pdev_dma_masks(&pa->pdev);
Russell King37c12e72005-11-05 21:19:33 +0000358 }
359
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500360 return pa ? &pa->pdev : NULL;
Russell King37c12e72005-11-05 21:19:33 +0000361}
362EXPORT_SYMBOL_GPL(platform_device_alloc);
363
364/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000365 * platform_device_add_resources - add resources to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800366 * @pdev: platform device allocated by platform_device_alloc to add resources to
367 * @res: set of resources that needs to be allocated for the device
368 * @num: number of resources
Russell King37c12e72005-11-05 21:19:33 +0000369 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800370 * Add a copy of the resources to the platform device. The memory
371 * associated with the resources will be freed when the platform device is
372 * released.
Russell King37c12e72005-11-05 21:19:33 +0000373 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800374int platform_device_add_resources(struct platform_device *pdev,
Geert Uytterhoeven0b7f1a72009-01-28 21:01:02 +0100375 const struct resource *res, unsigned int num)
Russell King37c12e72005-11-05 21:19:33 +0000376{
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200377 struct resource *r = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000378
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200379 if (res) {
380 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
381 if (!r)
382 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000383 }
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200384
Uwe Kleine-König4a03d6f2011-04-20 09:44:45 +0200385 kfree(pdev->resource);
Uwe Kleine-Königcea89622011-04-20 09:44:44 +0200386 pdev->resource = r;
387 pdev->num_resources = num;
388 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000389}
390EXPORT_SYMBOL_GPL(platform_device_add_resources);
391
392/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000393 * platform_device_add_data - add platform-specific data to a platform device
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800394 * @pdev: platform device allocated by platform_device_alloc to add resources to
395 * @data: platform specific data for this platform device
396 * @size: size of platform specific data
Russell King37c12e72005-11-05 21:19:33 +0000397 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800398 * Add a copy of platform specific data to the platform device's
399 * platform_data pointer. The memory associated with the platform data
400 * will be freed when the platform device is released.
Russell King37c12e72005-11-05 21:19:33 +0000401 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800402int platform_device_add_data(struct platform_device *pdev, const void *data,
403 size_t size)
Russell King37c12e72005-11-05 21:19:33 +0000404{
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200405 void *d = NULL;
Russell King37c12e72005-11-05 21:19:33 +0000406
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200407 if (data) {
408 d = kmemdup(data, size, GFP_KERNEL);
409 if (!d)
410 return -ENOMEM;
Russell King37c12e72005-11-05 21:19:33 +0000411 }
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200412
Uwe Kleine-König251e0312011-04-20 09:44:43 +0200413 kfree(pdev->dev.platform_data);
Uwe Kleine-König27a33f92011-04-20 09:44:42 +0200414 pdev->dev.platform_data = d;
415 return 0;
Russell King37c12e72005-11-05 21:19:33 +0000416}
417EXPORT_SYMBOL_GPL(platform_device_add_data);
418
419/**
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200420 * platform_device_add_properties - add built-in properties to a platform device
421 * @pdev: platform device to add properties to
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300422 * @properties: null terminated array of properties to add
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200423 *
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300424 * The function will take deep copy of @properties and attach the copy to the
425 * platform device. The memory associated with properties will be freed when the
426 * platform device is released.
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200427 */
428int platform_device_add_properties(struct platform_device *pdev,
Jan Kiszka277036f2017-06-02 07:43:27 +0200429 const struct property_entry *properties)
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200430{
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300431 return device_add_properties(&pdev->dev, properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200432}
433EXPORT_SYMBOL_GPL(platform_device_add_properties);
434
435/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800436 * platform_device_add - add a platform device to device hierarchy
437 * @pdev: platform device we're adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800439 * This is part 2 of platform_device_register(), though may be called
440 * separately _iff_ pdev was allocated by platform_device_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Russell King37c12e72005-11-05 21:19:33 +0000442int platform_device_add(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jean Delvare689ae232012-07-27 22:14:59 +0200444 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!pdev)
447 return -EINVAL;
448
449 if (!pdev->dev.parent)
450 pdev->dev.parent = &platform_bus;
451
452 pdev->dev.bus = &platform_bus_type;
453
Jean Delvare689ae232012-07-27 22:14:59 +0200454 switch (pdev->id) {
455 default:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100456 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
Jean Delvare689ae232012-07-27 22:14:59 +0200457 break;
458 case PLATFORM_DEVID_NONE:
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -0700459 dev_set_name(&pdev->dev, "%s", pdev->name);
Jean Delvare689ae232012-07-27 22:14:59 +0200460 break;
461 case PLATFORM_DEVID_AUTO:
462 /*
463 * Automatically allocated device ID. We mark it as such so
464 * that we remember it must be freed, and we append a suffix
465 * to avoid namespace collision with explicit IDs.
466 */
467 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
468 if (ret < 0)
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700469 goto err_out;
Jean Delvare689ae232012-07-27 22:14:59 +0200470 pdev->id = ret;
471 pdev->id_auto = true;
472 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
473 break;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 for (i = 0; i < pdev->num_resources; i++) {
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700477 struct resource *p, *r = &pdev->resource[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (r->name == NULL)
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100480 r->name = dev_name(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 p = r->parent;
483 if (!p) {
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700484 if (resource_type(r) == IORESOURCE_MEM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 p = &iomem_resource;
Greg Kroah-Hartman0e6c8612015-06-10 08:38:29 -0700486 else if (resource_type(r) == IORESOURCE_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 p = &ioport_resource;
488 }
489
Andy Shevchenko25ebcb72019-04-04 11:11:58 +0300490 if (p) {
491 ret = insert_resource(p, r);
492 if (ret) {
493 dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
494 goto failed;
495 }
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499 pr_debug("Registering platform device '%s'. Parent at %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100500 dev_name(&pdev->dev), dev_name(pdev->dev.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Russell Kinge3915532006-05-06 08:15:26 +0100502 ret = device_add(&pdev->dev);
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700503 if (ret == 0)
504 return ret;
505
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700506 failed:
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700507 if (pdev->id_auto) {
508 ida_simple_remove(&platform_devid_ida, pdev->id);
509 pdev->id = PLATFORM_DEVID_AUTO;
510 }
511
512 while (--i >= 0) {
513 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100514 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700515 release_resource(r);
516 }
Magnus Dammc9f66162008-10-15 22:05:15 -0700517
Greg Kroah-Hartman5da7f702015-06-10 08:38:02 -0700518 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return ret;
520}
Russell King37c12e72005-11-05 21:19:33 +0000521EXPORT_SYMBOL_GPL(platform_device_add);
522
523/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800524 * platform_device_del - remove a platform-level device
525 * @pdev: platform device we're removing
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500526 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800527 * Note that this function will also release all memory- and port-based
528 * resources owned by the device (@dev->resource). This function must
529 * _only_ be externally called in error cases. All other usage is a bug.
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500530 */
531void platform_device_del(struct platform_device *pdev)
532{
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700533 int i;
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500534
Andy Shevchenko99fef582018-12-03 20:21:41 +0200535 if (!IS_ERR_OR_NULL(pdev)) {
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700536 device_del(&pdev->dev);
537
538 if (pdev->id_auto) {
539 ida_simple_remove(&platform_devid_ida, pdev->id);
540 pdev->id = PLATFORM_DEVID_AUTO;
541 }
542
543 for (i = 0; i < pdev->num_resources; i++) {
544 struct resource *r = &pdev->resource[i];
Grant Likely7f5dcaf2015-06-07 15:20:11 +0100545 if (r->parent)
Greg Kroah-Hartman8b2dceb2015-06-10 08:36:50 -0700546 release_resource(r);
547 }
548 }
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500549}
550EXPORT_SYMBOL_GPL(platform_device_del);
551
552/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800553 * platform_device_register - add a platform-level device
554 * @pdev: platform device we're adding
Russell King37c12e72005-11-05 21:19:33 +0000555 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800556int platform_device_register(struct platform_device *pdev)
Russell King37c12e72005-11-05 21:19:33 +0000557{
558 device_initialize(&pdev->dev);
Christoph Hellwigcdfee562019-08-16 08:24:35 +0200559 setup_pdev_dma_masks(pdev);
Russell King37c12e72005-11-05 21:19:33 +0000560 return platform_device_add(pdev);
561}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500562EXPORT_SYMBOL_GPL(platform_device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800565 * platform_device_unregister - unregister a platform-level device
566 * @pdev: platform device we're unregistering
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800568 * Unregistration is done in 2 steps. First we release all resources
569 * and remove it from the subsystem, then we drop reference count by
570 * calling platform_device_put().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800572void platform_device_unregister(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Dmitry Torokhov93ce3062005-12-10 01:36:27 -0500574 platform_device_del(pdev);
575 platform_device_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Dmitry Torokhova96b2042005-12-10 01:36:28 -0500577EXPORT_SYMBOL_GPL(platform_device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/**
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200580 * platform_device_register_full - add a platform-level device with
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200581 * resources and platform-specific data
582 *
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200583 * @pdevinfo: data used to create device
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700584 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200585 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700586 */
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200587struct platform_device *platform_device_register_full(
Uwe Kleine-König5a3072b2011-12-08 22:53:29 +0100588 const struct platform_device_info *pdevinfo)
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700589{
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200590 int ret = -ENOMEM;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700591 struct platform_device *pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700592
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200593 pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200594 if (!pdev)
Johannes Berg36cf3b12019-03-01 13:24:47 +0100595 return ERR_PTR(-ENOMEM);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700596
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200597 pdev->dev.parent = pdevinfo->parent;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100598 pdev->dev.fwnode = pdevinfo->fwnode;
Mans Rullgard2c1ea6a2019-02-21 11:29:35 +0000599 pdev->dev.of_node = of_node_get(to_of_node(pdev->dev.fwnode));
600 pdev->dev.of_node_reused = pdevinfo->of_node_reused;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700601
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200602 if (pdevinfo->dma_mask) {
603 /*
604 * This memory isn't freed when the device is put,
605 * I don't have a nice idea for that though. Conceptually
606 * dma_mask in struct device should not be a pointer.
607 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
608 */
609 pdev->dev.dma_mask =
610 kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
611 if (!pdev->dev.dma_mask)
612 goto err;
613
Qian Cai967d3012019-01-03 15:29:05 -0800614 kmemleak_ignore(pdev->dev.dma_mask);
615
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200616 *pdev->dev.dma_mask = pdevinfo->dma_mask;
617 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
618 }
619
620 ret = platform_device_add_resources(pdev,
621 pdevinfo->res, pdevinfo->num_res);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400622 if (ret)
623 goto err;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700624
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200625 ret = platform_device_add_data(pdev,
626 pdevinfo->data, pdevinfo->size_data);
Anton Vorontsov807508c2010-09-07 17:31:54 +0400627 if (ret)
628 goto err;
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200629
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300630 if (pdevinfo->properties) {
631 ret = platform_device_add_properties(pdev,
632 pdevinfo->properties);
Mika Westerberg00bbc1d2015-11-30 17:11:38 +0200633 if (ret)
634 goto err;
635 }
636
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200637 ret = platform_device_add(pdev);
638 if (ret) {
639err:
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100640 ACPI_COMPANION_SET(&pdev->dev, NULL);
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200641 kfree(pdev->dev.dma_mask);
Uwe Kleine-König44f28bd2010-06-21 16:11:44 +0200642 platform_device_put(pdev);
643 return ERR_PTR(ret);
644 }
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700645
646 return pdev;
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700647}
Uwe Kleine-König01dcc602011-08-25 11:16:00 +0200648EXPORT_SYMBOL_GPL(platform_device_register_full);
Dmitry Baryshkovd8bf2542008-09-22 14:41:40 -0700649
Russell King00d3dcd2005-11-09 17:23:39 +0000650static int platform_drv_probe(struct device *_dev)
651{
652 struct platform_driver *drv = to_platform_driver(_dev->driver);
653 struct platform_device *dev = to_platform_device(_dev);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100654 int ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000655
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200656 ret = of_clk_set_defaults(_dev->of_node, false);
657 if (ret < 0)
658 return ret;
659
Ulf Hanssoncb518412014-09-19 20:27:38 +0200660 ret = dev_pm_domain_attach(_dev, true);
Ulf Hansson88a97692018-04-26 10:53:06 +0200661 if (ret)
662 goto out;
663
664 if (drv->probe) {
665 ret = drv->probe(dev);
666 if (ret)
667 dev_pm_domain_detach(_dev, true);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200668 }
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100669
Ulf Hansson88a97692018-04-26 10:53:06 +0200670out:
Johan Hovold3f9120b2013-09-23 16:27:26 +0200671 if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
672 dev_warn(_dev, "probe deferral not supported\n");
673 ret = -ENXIO;
674 }
675
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100676 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000677}
678
David Brownellc67334f2006-11-16 23:28:47 -0800679static int platform_drv_probe_fail(struct device *_dev)
680{
681 return -ENXIO;
682}
683
Russell King00d3dcd2005-11-09 17:23:39 +0000684static int platform_drv_remove(struct device *_dev)
685{
686 struct platform_driver *drv = to_platform_driver(_dev->driver);
687 struct platform_device *dev = to_platform_device(_dev);
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200688 int ret = 0;
Russell King00d3dcd2005-11-09 17:23:39 +0000689
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200690 if (drv->remove)
691 ret = drv->remove(dev);
Ulf Hanssoncb518412014-09-19 20:27:38 +0200692 dev_pm_domain_detach(_dev, true);
Rafael J. Wysocki94d76d52012-11-26 10:04:53 +0100693
694 return ret;
Russell King00d3dcd2005-11-09 17:23:39 +0000695}
696
697static void platform_drv_shutdown(struct device *_dev)
698{
699 struct platform_driver *drv = to_platform_driver(_dev->driver);
700 struct platform_device *dev = to_platform_device(_dev);
701
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200702 if (drv->shutdown)
703 drv->shutdown(dev);
Russell King00d3dcd2005-11-09 17:23:39 +0000704}
705
Russell King00d3dcd2005-11-09 17:23:39 +0000706/**
Libo Chen94470572013-05-25 12:40:50 +0800707 * __platform_driver_register - register a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800708 * @drv: platform driver structure
Randy Dunlap08801f92013-07-14 17:43:06 -0700709 * @owner: owning module/driver
Russell King00d3dcd2005-11-09 17:23:39 +0000710 */
Libo Chen94470572013-05-25 12:40:50 +0800711int __platform_driver_register(struct platform_driver *drv,
712 struct module *owner)
Russell King00d3dcd2005-11-09 17:23:39 +0000713{
Libo Chen94470572013-05-25 12:40:50 +0800714 drv->driver.owner = owner;
Russell King00d3dcd2005-11-09 17:23:39 +0000715 drv->driver.bus = &platform_bus_type;
Uwe Kleine-Königb8b2c7d2015-08-07 07:19:22 +0200716 drv->driver.probe = platform_drv_probe;
717 drv->driver.remove = platform_drv_remove;
718 drv->driver.shutdown = platform_drv_shutdown;
Magnus Damm783ea7d2009-06-04 22:13:33 +0200719
Russell King00d3dcd2005-11-09 17:23:39 +0000720 return driver_register(&drv->driver);
721}
Libo Chen94470572013-05-25 12:40:50 +0800722EXPORT_SYMBOL_GPL(__platform_driver_register);
Russell King00d3dcd2005-11-09 17:23:39 +0000723
724/**
Ben Hutchings3c31f072010-02-14 14:18:53 +0000725 * platform_driver_unregister - unregister a driver for platform-level devices
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800726 * @drv: platform driver structure
Russell King00d3dcd2005-11-09 17:23:39 +0000727 */
728void platform_driver_unregister(struct platform_driver *drv)
729{
730 driver_unregister(&drv->driver);
731}
732EXPORT_SYMBOL_GPL(platform_driver_unregister);
733
David Brownellc67334f2006-11-16 23:28:47 -0800734/**
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100735 * __platform_driver_probe - register driver for non-hotpluggable device
David Brownellc67334f2006-11-16 23:28:47 -0800736 * @drv: platform driver structure
Johan Hovold3f9120b2013-09-23 16:27:26 +0200737 * @probe: the driver probe routine, probably from an __init section
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100738 * @module: module which will be the owner of the driver
David Brownellc67334f2006-11-16 23:28:47 -0800739 *
740 * Use this instead of platform_driver_register() when you know the device
741 * is not hotpluggable and has already been registered, and you want to
742 * remove its run-once probe() infrastructure from memory after the driver
743 * has bound to the device.
744 *
745 * One typical use for this would be with drivers for controllers integrated
746 * into system-on-chip processors, where the controller devices have been
747 * configured as part of board setup.
748 *
Johan Hovold3f9120b2013-09-23 16:27:26 +0200749 * Note that this is incompatible with deferred probing.
Fabio Porcedda647c86d2013-03-26 10:35:15 +0100750 *
David Brownellc67334f2006-11-16 23:28:47 -0800751 * Returns zero if the driver registered and bound to a device, else returns
752 * a negative error code and with the driver not registered.
753 */
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100754int __init_or_module __platform_driver_probe(struct platform_driver *drv,
755 int (*probe)(struct platform_device *), struct module *module)
David Brownellc67334f2006-11-16 23:28:47 -0800756{
757 int retval, code;
758
Dmitry Torokhov5c36eb22015-03-30 16:20:07 -0700759 if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
760 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
761 drv->driver.name, __func__);
762 return -EINVAL;
763 }
764
765 /*
766 * We have to run our probes synchronously because we check if
767 * we find any devices to bind to and exit with error if there
768 * are any.
769 */
770 drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
771
Johan Hovold3f9120b2013-09-23 16:27:26 +0200772 /*
773 * Prevent driver from requesting probe deferral to avoid further
774 * futile probe attempts.
775 */
776 drv->prevent_deferred_probe = true;
777
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700778 /* make sure driver won't have bind/unbind attributes */
779 drv->driver.suppress_bind_attrs = true;
780
David Brownellc67334f2006-11-16 23:28:47 -0800781 /* temporary section violation during probe() */
782 drv->probe = probe;
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100783 retval = code = __platform_driver_register(drv, module);
David Brownellc67334f2006-11-16 23:28:47 -0800784
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700785 /*
786 * Fixup that section violation, being paranoid about code scanning
David Brownellc67334f2006-11-16 23:28:47 -0800787 * the list of drivers in order to probe new devices. Check to see
788 * if the probe was successful, and make sure any forced probes of
789 * new devices fail.
790 */
Patrick Pannutod79d3242010-08-06 17:12:41 -0700791 spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800792 drv->probe = NULL;
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800793 if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
David Brownellc67334f2006-11-16 23:28:47 -0800794 retval = -ENODEV;
795 drv->driver.probe = platform_drv_probe_fail;
Patrick Pannutod79d3242010-08-06 17:12:41 -0700796 spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
David Brownellc67334f2006-11-16 23:28:47 -0800797
798 if (code != retval)
799 platform_driver_unregister(drv);
800 return retval;
801}
Wolfram Sangc3b50dc2014-10-28 17:40:41 +0100802EXPORT_SYMBOL_GPL(__platform_driver_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800804/**
Wolfram Sang291f6532014-10-28 17:40:42 +0100805 * __platform_create_bundle - register driver and create corresponding device
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800806 * @driver: platform driver structure
807 * @probe: the driver probe routine, probably from an __init section
808 * @res: set of resources that needs to be allocated for the device
809 * @n_res: number of resources
810 * @data: platform specific data for this platform device
811 * @size: size of platform specific data
Wolfram Sang291f6532014-10-28 17:40:42 +0100812 * @module: module which will be the owner of the driver
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800813 *
814 * Use this in legacy-style modules that probe hardware directly and
815 * register a single platform device and corresponding platform driver.
Jani Nikulaf0eae0e2010-03-11 18:11:45 +0200816 *
817 * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800818 */
Wolfram Sang291f6532014-10-28 17:40:42 +0100819struct platform_device * __init_or_module __platform_create_bundle(
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800820 struct platform_driver *driver,
821 int (*probe)(struct platform_device *),
822 struct resource *res, unsigned int n_res,
Wolfram Sang291f6532014-10-28 17:40:42 +0100823 const void *data, size_t size, struct module *module)
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800824{
825 struct platform_device *pdev;
826 int error;
827
828 pdev = platform_device_alloc(driver->driver.name, -1);
829 if (!pdev) {
830 error = -ENOMEM;
831 goto err_out;
832 }
833
Anton Vorontsov807508c2010-09-07 17:31:54 +0400834 error = platform_device_add_resources(pdev, res, n_res);
835 if (error)
836 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800837
Anton Vorontsov807508c2010-09-07 17:31:54 +0400838 error = platform_device_add_data(pdev, data, size);
839 if (error)
840 goto err_pdev_put;
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800841
842 error = platform_device_add(pdev);
843 if (error)
844 goto err_pdev_put;
845
Wolfram Sang291f6532014-10-28 17:40:42 +0100846 error = __platform_driver_probe(driver, probe, module);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800847 if (error)
848 goto err_pdev_del;
849
850 return pdev;
851
852err_pdev_del:
853 platform_device_del(pdev);
854err_pdev_put:
855 platform_device_put(pdev);
856err_out:
857 return ERR_PTR(error);
858}
Wolfram Sang291f6532014-10-28 17:40:42 +0100859EXPORT_SYMBOL_GPL(__platform_create_bundle);
Dmitry Torokhovecdf6ce2009-12-29 20:11:20 -0800860
Thierry Redingdbe22562015-09-25 17:29:04 +0200861/**
862 * __platform_register_drivers - register an array of platform drivers
863 * @drivers: an array of drivers to register
864 * @count: the number of drivers to register
865 * @owner: module owning the drivers
866 *
867 * Registers platform drivers specified by an array. On failure to register a
868 * driver, all previously registered drivers will be unregistered. Callers of
869 * this API should use platform_unregister_drivers() to unregister drivers in
870 * the reverse order.
871 *
872 * Returns: 0 on success or a negative error code on failure.
873 */
874int __platform_register_drivers(struct platform_driver * const *drivers,
875 unsigned int count, struct module *owner)
876{
877 unsigned int i;
878 int err;
879
880 for (i = 0; i < count; i++) {
881 pr_debug("registering platform driver %ps\n", drivers[i]);
882
883 err = __platform_driver_register(drivers[i], owner);
884 if (err < 0) {
885 pr_err("failed to register platform driver %ps: %d\n",
886 drivers[i], err);
887 goto error;
888 }
889 }
890
891 return 0;
892
893error:
894 while (i--) {
895 pr_debug("unregistering platform driver %ps\n", drivers[i]);
896 platform_driver_unregister(drivers[i]);
897 }
898
899 return err;
900}
901EXPORT_SYMBOL_GPL(__platform_register_drivers);
902
903/**
904 * platform_unregister_drivers - unregister an array of platform drivers
905 * @drivers: an array of drivers to unregister
906 * @count: the number of drivers to unregister
907 *
908 * Unegisters platform drivers specified by an array. This is typically used
909 * to complement an earlier call to platform_register_drivers(). Drivers are
910 * unregistered in the reverse order in which they were registered.
911 */
912void platform_unregister_drivers(struct platform_driver * const *drivers,
913 unsigned int count)
914{
915 while (count--) {
916 pr_debug("unregistering platform driver %ps\n", drivers[count]);
917 platform_driver_unregister(drivers[count]);
918 }
919}
920EXPORT_SYMBOL_GPL(platform_unregister_drivers);
921
David Brownella0245f72006-05-29 10:37:33 -0700922/* modalias support enables more hands-off userspace setup:
923 * (a) environment variable lets new-style hotplug events work once system is
924 * fully running: "modprobe $MODALIAS"
925 * (b) sysfs attribute lets new-style coldplug recover from hotplug events
926 * mishandled before system is fully running: "modprobe $(cat modalias)"
927 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800928static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
929 char *buf)
David Brownella0245f72006-05-29 10:37:33 -0700930{
931 struct platform_device *pdev = to_platform_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800932 int len;
933
Rob Herring0634c292017-03-22 09:16:27 -0500934 len = of_device_modalias(dev, buf, PAGE_SIZE);
Zhang Ruib9f73062014-01-14 16:46:38 +0800935 if (len != -ENODEV)
936 return len;
937
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800938 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
939 if (len != -ENODEV)
940 return len;
941
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +0200942 len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
David Brownella0245f72006-05-29 10:37:33 -0700943
944 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
945}
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700946static DEVICE_ATTR_RO(modalias);
David Brownella0245f72006-05-29 10:37:33 -0700947
Kim Phillips3d713e02014-06-02 19:42:58 -0500948static ssize_t driver_override_store(struct device *dev,
949 struct device_attribute *attr,
950 const char *buf, size_t count)
951{
952 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700953 char *driver_override, *old, *cp;
Kim Phillips3d713e02014-06-02 19:42:58 -0500954
Nicolai Stangebf563b02017-09-11 09:45:42 +0200955 /* We need to keep extra room for a newline */
956 if (count >= (PAGE_SIZE - 1))
Kim Phillips3d713e02014-06-02 19:42:58 -0500957 return -EINVAL;
958
959 driver_override = kstrndup(buf, count, GFP_KERNEL);
960 if (!driver_override)
961 return -ENOMEM;
962
963 cp = strchr(driver_override, '\n');
964 if (cp)
965 *cp = '\0';
966
Adrian Salido626553972017-04-25 16:55:26 -0700967 device_lock(dev);
968 old = pdev->driver_override;
Kim Phillips3d713e02014-06-02 19:42:58 -0500969 if (strlen(driver_override)) {
970 pdev->driver_override = driver_override;
971 } else {
972 kfree(driver_override);
973 pdev->driver_override = NULL;
974 }
Adrian Salido626553972017-04-25 16:55:26 -0700975 device_unlock(dev);
Kim Phillips3d713e02014-06-02 19:42:58 -0500976
977 kfree(old);
978
979 return count;
980}
981
982static ssize_t driver_override_show(struct device *dev,
983 struct device_attribute *attr, char *buf)
984{
985 struct platform_device *pdev = to_platform_device(dev);
Adrian Salido626553972017-04-25 16:55:26 -0700986 ssize_t len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500987
Adrian Salido626553972017-04-25 16:55:26 -0700988 device_lock(dev);
989 len = sprintf(buf, "%s\n", pdev->driver_override);
990 device_unlock(dev);
991 return len;
Kim Phillips3d713e02014-06-02 19:42:58 -0500992}
993static DEVICE_ATTR_RW(driver_override);
994
995
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700996static struct attribute *platform_dev_attrs[] = {
997 &dev_attr_modalias.attr,
Kim Phillips3d713e02014-06-02 19:42:58 -0500998 &dev_attr_driver_override.attr,
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -0700999 NULL,
David Brownella0245f72006-05-29 10:37:33 -07001000};
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001001ATTRIBUTE_GROUPS(platform_dev);
David Brownella0245f72006-05-29 10:37:33 -07001002
Kay Sievers7eff2e72007-08-14 15:15:12 +02001003static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownella0245f72006-05-29 10:37:33 -07001004{
1005 struct platform_device *pdev = to_platform_device(dev);
Grant Likelyeca39302010-06-08 07:48:21 -06001006 int rc;
1007
1008 /* Some devices have extra OF data and an OF-style MODALIAS */
Fabio Porcedda0258e182013-03-26 10:35:16 +01001009 rc = of_device_uevent_modalias(dev, env);
Grant Likelyeca39302010-06-08 07:48:21 -06001010 if (rc != -ENODEV)
1011 return rc;
David Brownella0245f72006-05-29 10:37:33 -07001012
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001013 rc = acpi_device_uevent_modalias(dev, env);
1014 if (rc != -ENODEV)
1015 return rc;
1016
Eric Miao57fee4a2009-02-04 11:52:40 +08001017 add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001018 pdev->name);
David Brownella0245f72006-05-29 10:37:33 -07001019 return 0;
1020}
1021
Eric Miao57fee4a2009-02-04 11:52:40 +08001022static const struct platform_device_id *platform_match_id(
Uwe Kleine-König831fad22010-01-26 09:35:00 +01001023 const struct platform_device_id *id,
Eric Miao57fee4a2009-02-04 11:52:40 +08001024 struct platform_device *pdev)
1025{
1026 while (id->name[0]) {
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001027 if (strcmp(pdev->name, id->name) == 0) {
Eric Miao57fee4a2009-02-04 11:52:40 +08001028 pdev->id_entry = id;
1029 return id;
1030 }
1031 id++;
1032 }
1033 return NULL;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001037 * platform_match - bind platform device to platform driver.
1038 * @dev: device.
1039 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001041 * Platform device IDs are assumed to be encoded like this:
1042 * "<name><instance>", where <name> is a short description of the type of
1043 * device, like "pci" or "floppy", and <instance> is the enumerated
1044 * instance of the device, like '0' or '42'. Driver IDs are simply
1045 * "<name>". So, extract the <name> from the platform_device structure,
1046 * and compare it against the name of the driver. Return whether they match
1047 * or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001049static int platform_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Eric Miao71b3e0c2009-01-31 22:47:44 +08001051 struct platform_device *pdev = to_platform_device(dev);
Eric Miao57fee4a2009-02-04 11:52:40 +08001052 struct platform_driver *pdrv = to_platform_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Kim Phillips3d713e02014-06-02 19:42:58 -05001054 /* When driver_override is set, only bind to the matching driver */
1055 if (pdev->driver_override)
1056 return !strcmp(pdev->driver_override, drv->name);
1057
Grant Likely05212152010-06-08 07:48:20 -06001058 /* Attempt an OF style match first */
1059 if (of_driver_match_device(dev, drv))
1060 return 1;
1061
Mika Westerberg91e56872012-10-31 22:45:02 +01001062 /* Then try ACPI style match */
1063 if (acpi_driver_match_device(dev, drv))
1064 return 1;
1065
Grant Likely05212152010-06-08 07:48:20 -06001066 /* Then try to match against the id table */
Eric Miao57fee4a2009-02-04 11:52:40 +08001067 if (pdrv->id_table)
1068 return platform_match_id(pdrv->id_table, pdev) != NULL;
1069
1070 /* fall-back to driver name match */
Greg Kroah-Hartman391c0322019-04-29 19:49:21 +02001071 return (strcmp(pdev->name, drv->name) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001074#ifdef CONFIG_PM_SLEEP
1075
1076static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001078 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1079 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int ret = 0;
1081
Magnus Damm783ea7d2009-06-04 22:13:33 +02001082 if (dev->driver && pdrv->suspend)
1083 ret = pdrv->suspend(pdev, mesg);
David Brownell386415d82006-09-03 13:16:45 -07001084
1085 return ret;
1086}
1087
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001088static int platform_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Magnus Damm783ea7d2009-06-04 22:13:33 +02001090 struct platform_driver *pdrv = to_platform_driver(dev->driver);
1091 struct platform_device *pdev = to_platform_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int ret = 0;
1093
Magnus Damm783ea7d2009-06-04 22:13:33 +02001094 if (dev->driver && pdrv->resume)
1095 ret = pdrv->resume(pdev);
Russell King9480e302005-10-28 09:52:56 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return ret;
1098}
1099
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001100#endif /* CONFIG_PM_SLEEP */
Magnus Damm9d730222009-08-20 20:25:32 +02001101
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001102#ifdef CONFIG_SUSPEND
1103
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001104int platform_pm_suspend(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001105{
1106 struct device_driver *drv = dev->driver;
1107 int ret = 0;
1108
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001109 if (!drv)
1110 return 0;
1111
1112 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001113 if (drv->pm->suspend)
1114 ret = drv->pm->suspend(dev);
1115 } else {
1116 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1117 }
1118
1119 return ret;
1120}
1121
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001122int platform_pm_resume(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001123{
1124 struct device_driver *drv = dev->driver;
1125 int ret = 0;
1126
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001127 if (!drv)
1128 return 0;
1129
1130 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001131 if (drv->pm->resume)
1132 ret = drv->pm->resume(dev);
1133 } else {
1134 ret = platform_legacy_resume(dev);
1135 }
1136
1137 return ret;
1138}
1139
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001140#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001141
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001142#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001143
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001144int platform_pm_freeze(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001145{
1146 struct device_driver *drv = dev->driver;
1147 int ret = 0;
1148
1149 if (!drv)
1150 return 0;
1151
1152 if (drv->pm) {
1153 if (drv->pm->freeze)
1154 ret = drv->pm->freeze(dev);
1155 } else {
1156 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1157 }
1158
1159 return ret;
1160}
1161
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001162int platform_pm_thaw(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001163{
1164 struct device_driver *drv = dev->driver;
1165 int ret = 0;
1166
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001167 if (!drv)
1168 return 0;
1169
1170 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001171 if (drv->pm->thaw)
1172 ret = drv->pm->thaw(dev);
1173 } else {
1174 ret = platform_legacy_resume(dev);
1175 }
1176
1177 return ret;
1178}
1179
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001180int platform_pm_poweroff(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001181{
1182 struct device_driver *drv = dev->driver;
1183 int ret = 0;
1184
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001185 if (!drv)
1186 return 0;
1187
1188 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001189 if (drv->pm->poweroff)
1190 ret = drv->pm->poweroff(dev);
1191 } else {
1192 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1193 }
1194
1195 return ret;
1196}
1197
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001198int platform_pm_restore(struct device *dev)
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001199{
1200 struct device_driver *drv = dev->driver;
1201 int ret = 0;
1202
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001203 if (!drv)
1204 return 0;
1205
1206 if (drv->pm) {
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001207 if (drv->pm->restore)
1208 ret = drv->pm->restore(dev);
1209 } else {
1210 ret = platform_legacy_resume(dev);
1211 }
1212
1213 return ret;
1214}
1215
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001216#endif /* CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001217
Nipun Gupta07397df2018-04-28 08:21:58 +05301218int platform_dma_configure(struct device *dev)
1219{
1220 enum dev_dma_attr attr;
1221 int ret = 0;
1222
1223 if (dev->of_node) {
Christoph Hellwig3d6ce862018-05-03 16:25:08 +02001224 ret = of_dma_configure(dev, dev->of_node, true);
Nipun Gupta07397df2018-04-28 08:21:58 +05301225 } else if (has_acpi_companion(dev)) {
1226 attr = acpi_get_dma_attr(to_acpi_device_node(dev->fwnode));
Robin Murphye5361ca2018-12-06 13:20:49 -08001227 ret = acpi_dma_configure(dev, attr);
Nipun Gupta07397df2018-04-28 08:21:58 +05301228 }
1229
1230 return ret;
1231}
1232
Dmitry Torokhovd9ab7712009-07-22 00:37:25 +02001233static const struct dev_pm_ops platform_dev_pm_ops = {
Rafael J. Wysocki8b313a32011-04-29 00:36:32 +02001234 .runtime_suspend = pm_generic_runtime_suspend,
1235 .runtime_resume = pm_generic_runtime_resume,
Rafael J. Wysocki69c9dd12011-04-29 00:36:05 +02001236 USE_PLATFORM_PM_SLEEP_OPS
Rafael J. Wysocki25e18492008-05-21 01:40:43 +02001237};
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239struct bus_type platform_bus_type = {
1240 .name = "platform",
Greg Kroah-Hartmand06262e2013-08-23 14:24:37 -07001241 .dev_groups = platform_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .match = platform_match,
David Brownella0245f72006-05-29 10:37:33 -07001243 .uevent = platform_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +05301244 .dma_configure = platform_dma_configure,
Magnus Damm9d730222009-08-20 20:25:32 +02001245 .pm = &platform_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246};
Dmitry Torokhova96b2042005-12-10 01:36:28 -05001247EXPORT_SYMBOL_GPL(platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Suzuki K Poulose36f33132019-07-23 23:18:38 +01001249/**
1250 * platform_find_device_by_driver - Find a platform device with a given
1251 * driver.
1252 * @start: The device to start the search from.
1253 * @drv: The device driver to look for.
1254 */
1255struct device *platform_find_device_by_driver(struct device *start,
1256 const struct device_driver *drv)
1257{
1258 return bus_find_device(&platform_bus_type, start, drv,
1259 (void *)platform_match);
1260}
1261EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263int __init platform_bus_init(void)
1264{
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001265 int error;
1266
Magnus Damm13977092009-03-30 14:37:25 -07001267 early_platform_cleanup();
1268
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001269 error = device_register(&platform_bus);
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301270 if (error) {
1271 put_device(&platform_bus);
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001272 return error;
Arvind Yadavc8ae1672018-03-11 11:25:49 +05301273 }
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001274 error = bus_register(&platform_bus_type);
1275 if (error)
1276 device_unregister(&platform_bus);
Pantelis Antoniou801d7282014-10-28 22:36:01 +02001277 of_platform_register_reconfig_notifier();
Cornelia Huckfbfb1442006-11-27 10:35:08 +01001278 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Magnus Damm13977092009-03-30 14:37:25 -07001281static __initdata LIST_HEAD(early_platform_driver_list);
1282static __initdata LIST_HEAD(early_platform_device_list);
1283
1284/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001285 * early_platform_driver_register - register early platform driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001286 * @epdrv: early_platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001287 * @buf: string passed from early_param()
Magnus Damm4d26e1392010-03-10 20:50:38 +09001288 *
1289 * Helper function for early_platform_init() / early_platform_init_buffer()
Magnus Damm13977092009-03-30 14:37:25 -07001290 */
1291int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1292 char *buf)
1293{
Magnus Dammc60e0502009-11-27 17:38:51 +09001294 char *tmp;
Magnus Damm13977092009-03-30 14:37:25 -07001295 int n;
1296
1297 /* Simply add the driver to the end of the global list.
1298 * Drivers will by default be put on the list in compiled-in order.
1299 */
1300 if (!epdrv->list.next) {
1301 INIT_LIST_HEAD(&epdrv->list);
1302 list_add_tail(&epdrv->list, &early_platform_driver_list);
1303 }
1304
1305 /* If the user has specified device then make sure the driver
1306 * gets prioritized. The driver of the last device specified on
1307 * command line will be put first on the list.
1308 */
1309 n = strlen(epdrv->pdrv->driver.name);
1310 if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1311 list_move(&epdrv->list, &early_platform_driver_list);
1312
Magnus Dammc60e0502009-11-27 17:38:51 +09001313 /* Allow passing parameters after device name */
1314 if (buf[n] == '\0' || buf[n] == ',')
Magnus Damm13977092009-03-30 14:37:25 -07001315 epdrv->requested_id = -1;
Magnus Dammc60e0502009-11-27 17:38:51 +09001316 else {
1317 epdrv->requested_id = simple_strtoul(&buf[n + 1],
1318 &tmp, 10);
1319
1320 if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1321 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1322 n = 0;
1323 } else
1324 n += strcspn(&buf[n + 1], ",") + 1;
1325 }
1326
1327 if (buf[n] == ',')
1328 n++;
1329
1330 if (epdrv->bufsize) {
1331 memcpy(epdrv->buffer, &buf[n],
1332 min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1333 epdrv->buffer[epdrv->bufsize - 1] = '\0';
1334 }
Magnus Damm13977092009-03-30 14:37:25 -07001335 }
1336
1337 return 0;
1338}
1339
1340/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001341 * early_platform_add_devices - adds a number of early platform devices
Magnus Damm13977092009-03-30 14:37:25 -07001342 * @devs: array of early platform devices to add
1343 * @num: number of early platform devices in array
Magnus Damm4d26e1392010-03-10 20:50:38 +09001344 *
1345 * Used by early architecture code to register early platform devices and
1346 * their platform data.
Magnus Damm13977092009-03-30 14:37:25 -07001347 */
1348void __init early_platform_add_devices(struct platform_device **devs, int num)
1349{
1350 struct device *dev;
1351 int i;
1352
1353 /* simply add the devices to list */
1354 for (i = 0; i < num; i++) {
1355 dev = &devs[i]->dev;
1356
1357 if (!dev->devres_head.next) {
Rafael J. Wysockibed2b422012-08-06 01:45:11 +02001358 pm_runtime_early_init(dev);
Magnus Damm13977092009-03-30 14:37:25 -07001359 INIT_LIST_HEAD(&dev->devres_head);
1360 list_add_tail(&dev->devres_head,
1361 &early_platform_device_list);
1362 }
1363 }
1364}
1365
1366/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001367 * early_platform_driver_register_all - register early platform drivers
Magnus Damm13977092009-03-30 14:37:25 -07001368 * @class_str: string to identify early platform driver class
Magnus Damm4d26e1392010-03-10 20:50:38 +09001369 *
1370 * Used by architecture code to register all early platform drivers
1371 * for a certain class. If omitted then only early platform drivers
1372 * with matching kernel command line class parameters will be registered.
Magnus Damm13977092009-03-30 14:37:25 -07001373 */
1374void __init early_platform_driver_register_all(char *class_str)
1375{
1376 /* The "class_str" parameter may or may not be present on the kernel
1377 * command line. If it is present then there may be more than one
1378 * matching parameter.
1379 *
1380 * Since we register our early platform drivers using early_param()
1381 * we need to make sure that they also get registered in the case
1382 * when the parameter is missing from the kernel command line.
1383 *
1384 * We use parse_early_options() to make sure the early_param() gets
1385 * called at least once. The early_param() may be called more than
1386 * once since the name of the preferred device may be specified on
1387 * the kernel command line. early_platform_driver_register() handles
1388 * this case for us.
1389 */
1390 parse_early_options(class_str);
1391}
1392
1393/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001394 * early_platform_match - find early platform device matching driver
Randy Dunlapd86c1302009-04-21 07:22:53 -07001395 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001396 * @id: id to match against
1397 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001398static struct platform_device * __init
Magnus Damm13977092009-03-30 14:37:25 -07001399early_platform_match(struct early_platform_driver *epdrv, int id)
1400{
1401 struct platform_device *pd;
1402
1403 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1404 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1405 if (pd->id == id)
1406 return pd;
1407
1408 return NULL;
1409}
1410
1411/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001412 * early_platform_left - check if early platform driver has matching devices
Randy Dunlapd86c1302009-04-21 07:22:53 -07001413 * @epdrv: early platform driver structure
Magnus Damm13977092009-03-30 14:37:25 -07001414 * @id: return true if id or above exists
1415 */
Hanjun Guoa8257912013-08-17 20:42:25 +08001416static int __init early_platform_left(struct early_platform_driver *epdrv,
Magnus Damm13977092009-03-30 14:37:25 -07001417 int id)
1418{
1419 struct platform_device *pd;
1420
1421 list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1422 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1423 if (pd->id >= id)
1424 return 1;
1425
1426 return 0;
1427}
1428
1429/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001430 * early_platform_driver_probe_id - probe drivers matching class_str and id
Magnus Damm13977092009-03-30 14:37:25 -07001431 * @class_str: string to identify early platform driver class
1432 * @id: id to match against
1433 * @nr_probe: number of platform devices to successfully probe before exiting
1434 */
1435static int __init early_platform_driver_probe_id(char *class_str,
1436 int id,
1437 int nr_probe)
1438{
1439 struct early_platform_driver *epdrv;
1440 struct platform_device *match;
1441 int match_id;
1442 int n = 0;
1443 int left = 0;
1444
1445 list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1446 /* only use drivers matching our class_str */
1447 if (strcmp(class_str, epdrv->class_str))
1448 continue;
1449
1450 if (id == -2) {
1451 match_id = epdrv->requested_id;
1452 left = 1;
1453
1454 } else {
1455 match_id = id;
1456 left += early_platform_left(epdrv, id);
1457
1458 /* skip requested id */
1459 switch (epdrv->requested_id) {
1460 case EARLY_PLATFORM_ID_ERROR:
1461 case EARLY_PLATFORM_ID_UNSET:
1462 break;
1463 default:
1464 if (epdrv->requested_id == id)
1465 match_id = EARLY_PLATFORM_ID_UNSET;
1466 }
1467 }
1468
1469 switch (match_id) {
1470 case EARLY_PLATFORM_ID_ERROR:
Fabio Porcedda0258e182013-03-26 10:35:16 +01001471 pr_warn("%s: unable to parse %s parameter\n",
1472 class_str, epdrv->pdrv->driver.name);
Magnus Damm13977092009-03-30 14:37:25 -07001473 /* fall-through */
1474 case EARLY_PLATFORM_ID_UNSET:
1475 match = NULL;
1476 break;
1477 default:
1478 match = early_platform_match(epdrv, match_id);
1479 }
1480
1481 if (match) {
Paul Mundta636ee72010-03-09 06:57:53 +00001482 /*
1483 * Set up a sensible init_name to enable
1484 * dev_name() and others to be used before the
1485 * rest of the driver core is initialized.
1486 */
Paul Mundt06fe53b2010-05-13 17:56:56 +09001487 if (!match->dev.init_name && slab_is_available()) {
Paul Mundta636ee72010-03-09 06:57:53 +00001488 if (match->id != -1)
Paul Mundtbd050862010-03-29 15:51:35 +09001489 match->dev.init_name =
1490 kasprintf(GFP_KERNEL, "%s.%d",
1491 match->name,
1492 match->id);
Paul Mundta636ee72010-03-09 06:57:53 +00001493 else
Paul Mundtbd050862010-03-29 15:51:35 +09001494 match->dev.init_name =
1495 kasprintf(GFP_KERNEL, "%s",
1496 match->name);
Paul Mundta636ee72010-03-09 06:57:53 +00001497
Paul Mundta636ee72010-03-09 06:57:53 +00001498 if (!match->dev.init_name)
1499 return -ENOMEM;
1500 }
Paul Mundtbd050862010-03-29 15:51:35 +09001501
Magnus Damm13977092009-03-30 14:37:25 -07001502 if (epdrv->pdrv->probe(match))
Fabio Porcedda0258e182013-03-26 10:35:16 +01001503 pr_warn("%s: unable to probe %s early.\n",
1504 class_str, match->name);
Magnus Damm13977092009-03-30 14:37:25 -07001505 else
1506 n++;
1507 }
1508
1509 if (n >= nr_probe)
1510 break;
1511 }
1512
1513 if (left)
1514 return n;
1515 else
1516 return -ENODEV;
1517}
1518
1519/**
Magnus Damm4d26e1392010-03-10 20:50:38 +09001520 * early_platform_driver_probe - probe a class of registered drivers
Magnus Damm13977092009-03-30 14:37:25 -07001521 * @class_str: string to identify early platform driver class
1522 * @nr_probe: number of platform devices to successfully probe before exiting
1523 * @user_only: only probe user specified early platform devices
Magnus Damm4d26e1392010-03-10 20:50:38 +09001524 *
1525 * Used by architecture code to probe registered early platform drivers
1526 * within a certain class. For probe to happen a registered early platform
1527 * device matching a registered early platform driver is needed.
Magnus Damm13977092009-03-30 14:37:25 -07001528 */
1529int __init early_platform_driver_probe(char *class_str,
1530 int nr_probe,
1531 int user_only)
1532{
1533 int k, n, i;
1534
1535 n = 0;
1536 for (i = -2; n < nr_probe; i++) {
1537 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1538
1539 if (k < 0)
1540 break;
1541
1542 n += k;
1543
1544 if (user_only)
1545 break;
1546 }
1547
1548 return n;
1549}
1550
1551/**
1552 * early_platform_cleanup - clean up early platform code
1553 */
1554void __init early_platform_cleanup(void)
1555{
1556 struct platform_device *pd, *pd2;
1557
1558 /* clean up the devres list used to chain devices */
1559 list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1560 dev.devres_head) {
1561 list_del(&pd->dev.devres_head);
1562 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1563 }
1564}
1565