Merge tag 'iwlwifi-for-kalle-2016-01-26_2' of https://git.kernel.org/pub/scm/linux...
[cascardo/linux.git] / drivers / base / platform.c
1 /*
2  * platform.c - platform 'pseudo' bus for legacy devices
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  * Please see Documentation/driver-model/platform.txt for more
10  * information.
11  */
12
13 #include <linux/string.h>
14 #include <linux/platform_device.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/bootmem.h>
21 #include <linux/err.h>
22 #include <linux/slab.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pm_domain.h>
25 #include <linux/idr.h>
26 #include <linux/acpi.h>
27 #include <linux/clk/clk-conf.h>
28 #include <linux/limits.h>
29 #include <linux/property.h>
30
31 #include "base.h"
32 #include "power/power.h"
33
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_devid_ida);
36
37 struct device platform_bus = {
38         .init_name      = "platform",
39 };
40 EXPORT_SYMBOL_GPL(platform_bus);
41
42 /**
43  * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
44  * @pdev: platform device
45  *
46  * This is called before platform_device_add() such that any pdev_archdata may
47  * be setup before the platform_notifier is called.  So if a user needs to
48  * manipulate any relevant information in the pdev_archdata they can do:
49  *
50  *      platform_device_alloc()
51  *      ... manipulate ...
52  *      platform_device_add()
53  *
54  * And if they don't care they can just call platform_device_register() and
55  * everything will just work out.
56  */
57 void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
58 {
59 }
60
61 /**
62  * platform_get_resource - get a resource for a device
63  * @dev: platform device
64  * @type: resource type
65  * @num: resource index
66  */
67 struct resource *platform_get_resource(struct platform_device *dev,
68                                        unsigned int type, unsigned int num)
69 {
70         int i;
71
72         for (i = 0; i < dev->num_resources; i++) {
73                 struct resource *r = &dev->resource[i];
74
75                 if (type == resource_type(r) && num-- == 0)
76                         return r;
77         }
78         return NULL;
79 }
80 EXPORT_SYMBOL_GPL(platform_get_resource);
81
82 /**
83  * platform_get_irq - get an IRQ for a device
84  * @dev: platform device
85  * @num: IRQ number index
86  */
87 int platform_get_irq(struct platform_device *dev, unsigned int num)
88 {
89 #ifdef CONFIG_SPARC
90         /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
91         if (!dev || num >= dev->archdata.num_irqs)
92                 return -ENXIO;
93         return dev->archdata.irqs[num];
94 #else
95         struct resource *r;
96         if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
97                 int ret;
98
99                 ret = of_irq_get(dev->dev.of_node, num);
100                 if (ret >= 0 || ret == -EPROBE_DEFER)
101                         return ret;
102         }
103
104         r = platform_get_resource(dev, IORESOURCE_IRQ, num);
105         /*
106          * The resources may pass trigger flags to the irqs that need
107          * to be set up. It so happens that the trigger flags for
108          * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
109          * settings.
110          */
111         if (r && r->flags & IORESOURCE_BITS)
112                 irqd_set_trigger_type(irq_get_irq_data(r->start),
113                                       r->flags & IORESOURCE_BITS);
114
115         return r ? r->start : -ENXIO;
116 #endif
117 }
118 EXPORT_SYMBOL_GPL(platform_get_irq);
119
120 /**
121  * platform_irq_count - Count the number of IRQs a platform device uses
122  * @dev: platform device
123  *
124  * Return: Number of IRQs a platform device uses or EPROBE_DEFER
125  */
126 int platform_irq_count(struct platform_device *dev)
127 {
128         int ret, nr = 0;
129
130         while ((ret = platform_get_irq(dev, nr)) >= 0)
131                 nr++;
132
133         if (ret == -EPROBE_DEFER)
134                 return ret;
135
136         return nr;
137 }
138 EXPORT_SYMBOL_GPL(platform_irq_count);
139
140 /**
141  * platform_get_resource_byname - get a resource for a device by name
142  * @dev: platform device
143  * @type: resource type
144  * @name: resource name
145  */
146 struct resource *platform_get_resource_byname(struct platform_device *dev,
147                                               unsigned int type,
148                                               const char *name)
149 {
150         int i;
151
152         for (i = 0; i < dev->num_resources; i++) {
153                 struct resource *r = &dev->resource[i];
154
155                 if (unlikely(!r->name))
156                         continue;
157
158                 if (type == resource_type(r) && !strcmp(r->name, name))
159                         return r;
160         }
161         return NULL;
162 }
163 EXPORT_SYMBOL_GPL(platform_get_resource_byname);
164
165 /**
166  * platform_get_irq_byname - get an IRQ for a device by name
167  * @dev: platform device
168  * @name: IRQ name
169  */
170 int platform_get_irq_byname(struct platform_device *dev, const char *name)
171 {
172         struct resource *r;
173
174         if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
175                 int ret;
176
177                 ret = of_irq_get_byname(dev->dev.of_node, name);
178                 if (ret >= 0 || ret == -EPROBE_DEFER)
179                         return ret;
180         }
181
182         r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
183         return r ? r->start : -ENXIO;
184 }
185 EXPORT_SYMBOL_GPL(platform_get_irq_byname);
186
187 /**
188  * platform_add_devices - add a numbers of platform devices
189  * @devs: array of platform devices to add
190  * @num: number of platform devices in array
191  */
192 int platform_add_devices(struct platform_device **devs, int num)
193 {
194         int i, ret = 0;
195
196         for (i = 0; i < num; i++) {
197                 ret = platform_device_register(devs[i]);
198                 if (ret) {
199                         while (--i >= 0)
200                                 platform_device_unregister(devs[i]);
201                         break;
202                 }
203         }
204
205         return ret;
206 }
207 EXPORT_SYMBOL_GPL(platform_add_devices);
208
209 struct platform_object {
210         struct platform_device pdev;
211         char name[];
212 };
213
214 /**
215  * platform_device_put - destroy a platform device
216  * @pdev: platform device to free
217  *
218  * Free all memory associated with a platform device.  This function must
219  * _only_ be externally called in error cases.  All other usage is a bug.
220  */
221 void platform_device_put(struct platform_device *pdev)
222 {
223         if (pdev)
224                 put_device(&pdev->dev);
225 }
226 EXPORT_SYMBOL_GPL(platform_device_put);
227
228 static void platform_device_release(struct device *dev)
229 {
230         struct platform_object *pa = container_of(dev, struct platform_object,
231                                                   pdev.dev);
232
233         of_device_node_put(&pa->pdev.dev);
234         kfree(pa->pdev.dev.platform_data);
235         kfree(pa->pdev.mfd_cell);
236         kfree(pa->pdev.resource);
237         kfree(pa->pdev.driver_override);
238         kfree(pa);
239 }
240
241 /**
242  * platform_device_alloc - create a platform device
243  * @name: base name of the device we're adding
244  * @id: instance id
245  *
246  * Create a platform device object which can have other objects attached
247  * to it, and which will have attached objects freed when it is released.
248  */
249 struct platform_device *platform_device_alloc(const char *name, int id)
250 {
251         struct platform_object *pa;
252
253         pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
254         if (pa) {
255                 strcpy(pa->name, name);
256                 pa->pdev.name = pa->name;
257                 pa->pdev.id = id;
258                 device_initialize(&pa->pdev.dev);
259                 pa->pdev.dev.release = platform_device_release;
260                 arch_setup_pdev_archdata(&pa->pdev);
261         }
262
263         return pa ? &pa->pdev : NULL;
264 }
265 EXPORT_SYMBOL_GPL(platform_device_alloc);
266
267 /**
268  * platform_device_add_resources - add resources to a platform device
269  * @pdev: platform device allocated by platform_device_alloc to add resources to
270  * @res: set of resources that needs to be allocated for the device
271  * @num: number of resources
272  *
273  * Add a copy of the resources to the platform device.  The memory
274  * associated with the resources will be freed when the platform device is
275  * released.
276  */
277 int platform_device_add_resources(struct platform_device *pdev,
278                                   const struct resource *res, unsigned int num)
279 {
280         struct resource *r = NULL;
281
282         if (res) {
283                 r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
284                 if (!r)
285                         return -ENOMEM;
286         }
287
288         kfree(pdev->resource);
289         pdev->resource = r;
290         pdev->num_resources = num;
291         return 0;
292 }
293 EXPORT_SYMBOL_GPL(platform_device_add_resources);
294
295 /**
296  * platform_device_add_data - add platform-specific data to a platform device
297  * @pdev: platform device allocated by platform_device_alloc to add resources to
298  * @data: platform specific data for this platform device
299  * @size: size of platform specific data
300  *
301  * Add a copy of platform specific data to the platform device's
302  * platform_data pointer.  The memory associated with the platform data
303  * will be freed when the platform device is released.
304  */
305 int platform_device_add_data(struct platform_device *pdev, const void *data,
306                              size_t size)
307 {
308         void *d = NULL;
309
310         if (data) {
311                 d = kmemdup(data, size, GFP_KERNEL);
312                 if (!d)
313                         return -ENOMEM;
314         }
315
316         kfree(pdev->dev.platform_data);
317         pdev->dev.platform_data = d;
318         return 0;
319 }
320 EXPORT_SYMBOL_GPL(platform_device_add_data);
321
322 /**
323  * platform_device_add_properties - add built-in properties to a platform device
324  * @pdev: platform device to add properties to
325  * @pset: properties to add
326  *
327  * The function will take deep copy of the properties in @pset and attach
328  * the copy to the platform device. The memory associated with properties
329  * will be freed when the platform device is released.
330  */
331 int platform_device_add_properties(struct platform_device *pdev,
332                                    const struct property_set *pset)
333 {
334         return device_add_property_set(&pdev->dev, pset);
335 }
336 EXPORT_SYMBOL_GPL(platform_device_add_properties);
337
338 /**
339  * platform_device_add - add a platform device to device hierarchy
340  * @pdev: platform device we're adding
341  *
342  * This is part 2 of platform_device_register(), though may be called
343  * separately _iff_ pdev was allocated by platform_device_alloc().
344  */
345 int platform_device_add(struct platform_device *pdev)
346 {
347         int i, ret;
348
349         if (!pdev)
350                 return -EINVAL;
351
352         if (!pdev->dev.parent)
353                 pdev->dev.parent = &platform_bus;
354
355         pdev->dev.bus = &platform_bus_type;
356
357         switch (pdev->id) {
358         default:
359                 dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
360                 break;
361         case PLATFORM_DEVID_NONE:
362                 dev_set_name(&pdev->dev, "%s", pdev->name);
363                 break;
364         case PLATFORM_DEVID_AUTO:
365                 /*
366                  * Automatically allocated device ID. We mark it as such so
367                  * that we remember it must be freed, and we append a suffix
368                  * to avoid namespace collision with explicit IDs.
369                  */
370                 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
371                 if (ret < 0)
372                         goto err_out;
373                 pdev->id = ret;
374                 pdev->id_auto = true;
375                 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
376                 break;
377         }
378
379         for (i = 0; i < pdev->num_resources; i++) {
380                 struct resource *p, *r = &pdev->resource[i];
381
382                 if (r->name == NULL)
383                         r->name = dev_name(&pdev->dev);
384
385                 p = r->parent;
386                 if (!p) {
387                         if (resource_type(r) == IORESOURCE_MEM)
388                                 p = &iomem_resource;
389                         else if (resource_type(r) == IORESOURCE_IO)
390                                 p = &ioport_resource;
391                 }
392
393                 if (p && insert_resource(p, r)) {
394                         dev_err(&pdev->dev, "failed to claim resource %d\n", i);
395                         ret = -EBUSY;
396                         goto failed;
397                 }
398         }
399
400         pr_debug("Registering platform device '%s'. Parent at %s\n",
401                  dev_name(&pdev->dev), dev_name(pdev->dev.parent));
402
403         ret = device_add(&pdev->dev);
404         if (ret == 0)
405                 return ret;
406
407  failed:
408         if (pdev->id_auto) {
409                 ida_simple_remove(&platform_devid_ida, pdev->id);
410                 pdev->id = PLATFORM_DEVID_AUTO;
411         }
412
413         while (--i >= 0) {
414                 struct resource *r = &pdev->resource[i];
415                 if (r->parent)
416                         release_resource(r);
417         }
418
419  err_out:
420         return ret;
421 }
422 EXPORT_SYMBOL_GPL(platform_device_add);
423
424 /**
425  * platform_device_del - remove a platform-level device
426  * @pdev: platform device we're removing
427  *
428  * Note that this function will also release all memory- and port-based
429  * resources owned by the device (@dev->resource).  This function must
430  * _only_ be externally called in error cases.  All other usage is a bug.
431  */
432 void platform_device_del(struct platform_device *pdev)
433 {
434         int i;
435
436         if (pdev) {
437                 device_del(&pdev->dev);
438
439                 if (pdev->id_auto) {
440                         ida_simple_remove(&platform_devid_ida, pdev->id);
441                         pdev->id = PLATFORM_DEVID_AUTO;
442                 }
443
444                 for (i = 0; i < pdev->num_resources; i++) {
445                         struct resource *r = &pdev->resource[i];
446                         if (r->parent)
447                                 release_resource(r);
448                 }
449
450                 device_remove_property_set(&pdev->dev);
451         }
452 }
453 EXPORT_SYMBOL_GPL(platform_device_del);
454
455 /**
456  * platform_device_register - add a platform-level device
457  * @pdev: platform device we're adding
458  */
459 int platform_device_register(struct platform_device *pdev)
460 {
461         device_initialize(&pdev->dev);
462         arch_setup_pdev_archdata(pdev);
463         return platform_device_add(pdev);
464 }
465 EXPORT_SYMBOL_GPL(platform_device_register);
466
467 /**
468  * platform_device_unregister - unregister a platform-level device
469  * @pdev: platform device we're unregistering
470  *
471  * Unregistration is done in 2 steps. First we release all resources
472  * and remove it from the subsystem, then we drop reference count by
473  * calling platform_device_put().
474  */
475 void platform_device_unregister(struct platform_device *pdev)
476 {
477         platform_device_del(pdev);
478         platform_device_put(pdev);
479 }
480 EXPORT_SYMBOL_GPL(platform_device_unregister);
481
482 /**
483  * platform_device_register_full - add a platform-level device with
484  * resources and platform-specific data
485  *
486  * @pdevinfo: data used to create device
487  *
488  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
489  */
490 struct platform_device *platform_device_register_full(
491                 const struct platform_device_info *pdevinfo)
492 {
493         int ret = -ENOMEM;
494         struct platform_device *pdev;
495
496         pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
497         if (!pdev)
498                 goto err_alloc;
499
500         pdev->dev.parent = pdevinfo->parent;
501         pdev->dev.fwnode = pdevinfo->fwnode;
502
503         if (pdevinfo->dma_mask) {
504                 /*
505                  * This memory isn't freed when the device is put,
506                  * I don't have a nice idea for that though.  Conceptually
507                  * dma_mask in struct device should not be a pointer.
508                  * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
509                  */
510                 pdev->dev.dma_mask =
511                         kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
512                 if (!pdev->dev.dma_mask)
513                         goto err;
514
515                 *pdev->dev.dma_mask = pdevinfo->dma_mask;
516                 pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
517         }
518
519         ret = platform_device_add_resources(pdev,
520                         pdevinfo->res, pdevinfo->num_res);
521         if (ret)
522                 goto err;
523
524         ret = platform_device_add_data(pdev,
525                         pdevinfo->data, pdevinfo->size_data);
526         if (ret)
527                 goto err;
528
529         if (pdevinfo->pset) {
530                 ret = platform_device_add_properties(pdev, pdevinfo->pset);
531                 if (ret)
532                         goto err;
533         }
534
535         ret = platform_device_add(pdev);
536         if (ret) {
537 err:
538                 ACPI_COMPANION_SET(&pdev->dev, NULL);
539                 kfree(pdev->dev.dma_mask);
540
541 err_alloc:
542                 platform_device_put(pdev);
543                 return ERR_PTR(ret);
544         }
545
546         return pdev;
547 }
548 EXPORT_SYMBOL_GPL(platform_device_register_full);
549
550 static int platform_drv_probe(struct device *_dev)
551 {
552         struct platform_driver *drv = to_platform_driver(_dev->driver);
553         struct platform_device *dev = to_platform_device(_dev);
554         int ret;
555
556         ret = of_clk_set_defaults(_dev->of_node, false);
557         if (ret < 0)
558                 return ret;
559
560         ret = dev_pm_domain_attach(_dev, true);
561         if (ret != -EPROBE_DEFER && drv->probe) {
562                 ret = drv->probe(dev);
563                 if (ret)
564                         dev_pm_domain_detach(_dev, true);
565         }
566
567         if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
568                 dev_warn(_dev, "probe deferral not supported\n");
569                 ret = -ENXIO;
570         }
571
572         return ret;
573 }
574
575 static int platform_drv_probe_fail(struct device *_dev)
576 {
577         return -ENXIO;
578 }
579
580 static int platform_drv_remove(struct device *_dev)
581 {
582         struct platform_driver *drv = to_platform_driver(_dev->driver);
583         struct platform_device *dev = to_platform_device(_dev);
584         int ret = 0;
585
586         if (drv->remove)
587                 ret = drv->remove(dev);
588         dev_pm_domain_detach(_dev, true);
589
590         return ret;
591 }
592
593 static void platform_drv_shutdown(struct device *_dev)
594 {
595         struct platform_driver *drv = to_platform_driver(_dev->driver);
596         struct platform_device *dev = to_platform_device(_dev);
597
598         if (drv->shutdown)
599                 drv->shutdown(dev);
600         dev_pm_domain_detach(_dev, true);
601 }
602
603 /**
604  * __platform_driver_register - register a driver for platform-level devices
605  * @drv: platform driver structure
606  * @owner: owning module/driver
607  */
608 int __platform_driver_register(struct platform_driver *drv,
609                                 struct module *owner)
610 {
611         drv->driver.owner = owner;
612         drv->driver.bus = &platform_bus_type;
613         drv->driver.probe = platform_drv_probe;
614         drv->driver.remove = platform_drv_remove;
615         drv->driver.shutdown = platform_drv_shutdown;
616
617         return driver_register(&drv->driver);
618 }
619 EXPORT_SYMBOL_GPL(__platform_driver_register);
620
621 /**
622  * platform_driver_unregister - unregister a driver for platform-level devices
623  * @drv: platform driver structure
624  */
625 void platform_driver_unregister(struct platform_driver *drv)
626 {
627         driver_unregister(&drv->driver);
628 }
629 EXPORT_SYMBOL_GPL(platform_driver_unregister);
630
631 /**
632  * __platform_driver_probe - register driver for non-hotpluggable device
633  * @drv: platform driver structure
634  * @probe: the driver probe routine, probably from an __init section
635  * @module: module which will be the owner of the driver
636  *
637  * Use this instead of platform_driver_register() when you know the device
638  * is not hotpluggable and has already been registered, and you want to
639  * remove its run-once probe() infrastructure from memory after the driver
640  * has bound to the device.
641  *
642  * One typical use for this would be with drivers for controllers integrated
643  * into system-on-chip processors, where the controller devices have been
644  * configured as part of board setup.
645  *
646  * Note that this is incompatible with deferred probing.
647  *
648  * Returns zero if the driver registered and bound to a device, else returns
649  * a negative error code and with the driver not registered.
650  */
651 int __init_or_module __platform_driver_probe(struct platform_driver *drv,
652                 int (*probe)(struct platform_device *), struct module *module)
653 {
654         int retval, code;
655
656         if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
657                 pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
658                          drv->driver.name, __func__);
659                 return -EINVAL;
660         }
661
662         /*
663          * We have to run our probes synchronously because we check if
664          * we find any devices to bind to and exit with error if there
665          * are any.
666          */
667         drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
668
669         /*
670          * Prevent driver from requesting probe deferral to avoid further
671          * futile probe attempts.
672          */
673         drv->prevent_deferred_probe = true;
674
675         /* make sure driver won't have bind/unbind attributes */
676         drv->driver.suppress_bind_attrs = true;
677
678         /* temporary section violation during probe() */
679         drv->probe = probe;
680         retval = code = __platform_driver_register(drv, module);
681
682         /*
683          * Fixup that section violation, being paranoid about code scanning
684          * the list of drivers in order to probe new devices.  Check to see
685          * if the probe was successful, and make sure any forced probes of
686          * new devices fail.
687          */
688         spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
689         drv->probe = NULL;
690         if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
691                 retval = -ENODEV;
692         drv->driver.probe = platform_drv_probe_fail;
693         spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
694
695         if (code != retval)
696                 platform_driver_unregister(drv);
697         return retval;
698 }
699 EXPORT_SYMBOL_GPL(__platform_driver_probe);
700
701 /**
702  * __platform_create_bundle - register driver and create corresponding device
703  * @driver: platform driver structure
704  * @probe: the driver probe routine, probably from an __init section
705  * @res: set of resources that needs to be allocated for the device
706  * @n_res: number of resources
707  * @data: platform specific data for this platform device
708  * @size: size of platform specific data
709  * @module: module which will be the owner of the driver
710  *
711  * Use this in legacy-style modules that probe hardware directly and
712  * register a single platform device and corresponding platform driver.
713  *
714  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
715  */
716 struct platform_device * __init_or_module __platform_create_bundle(
717                         struct platform_driver *driver,
718                         int (*probe)(struct platform_device *),
719                         struct resource *res, unsigned int n_res,
720                         const void *data, size_t size, struct module *module)
721 {
722         struct platform_device *pdev;
723         int error;
724
725         pdev = platform_device_alloc(driver->driver.name, -1);
726         if (!pdev) {
727                 error = -ENOMEM;
728                 goto err_out;
729         }
730
731         error = platform_device_add_resources(pdev, res, n_res);
732         if (error)
733                 goto err_pdev_put;
734
735         error = platform_device_add_data(pdev, data, size);
736         if (error)
737                 goto err_pdev_put;
738
739         error = platform_device_add(pdev);
740         if (error)
741                 goto err_pdev_put;
742
743         error = __platform_driver_probe(driver, probe, module);
744         if (error)
745                 goto err_pdev_del;
746
747         return pdev;
748
749 err_pdev_del:
750         platform_device_del(pdev);
751 err_pdev_put:
752         platform_device_put(pdev);
753 err_out:
754         return ERR_PTR(error);
755 }
756 EXPORT_SYMBOL_GPL(__platform_create_bundle);
757
758 /**
759  * __platform_register_drivers - register an array of platform drivers
760  * @drivers: an array of drivers to register
761  * @count: the number of drivers to register
762  * @owner: module owning the drivers
763  *
764  * Registers platform drivers specified by an array. On failure to register a
765  * driver, all previously registered drivers will be unregistered. Callers of
766  * this API should use platform_unregister_drivers() to unregister drivers in
767  * the reverse order.
768  *
769  * Returns: 0 on success or a negative error code on failure.
770  */
771 int __platform_register_drivers(struct platform_driver * const *drivers,
772                                 unsigned int count, struct module *owner)
773 {
774         unsigned int i;
775         int err;
776
777         for (i = 0; i < count; i++) {
778                 pr_debug("registering platform driver %ps\n", drivers[i]);
779
780                 err = __platform_driver_register(drivers[i], owner);
781                 if (err < 0) {
782                         pr_err("failed to register platform driver %ps: %d\n",
783                                drivers[i], err);
784                         goto error;
785                 }
786         }
787
788         return 0;
789
790 error:
791         while (i--) {
792                 pr_debug("unregistering platform driver %ps\n", drivers[i]);
793                 platform_driver_unregister(drivers[i]);
794         }
795
796         return err;
797 }
798 EXPORT_SYMBOL_GPL(__platform_register_drivers);
799
800 /**
801  * platform_unregister_drivers - unregister an array of platform drivers
802  * @drivers: an array of drivers to unregister
803  * @count: the number of drivers to unregister
804  *
805  * Unegisters platform drivers specified by an array. This is typically used
806  * to complement an earlier call to platform_register_drivers(). Drivers are
807  * unregistered in the reverse order in which they were registered.
808  */
809 void platform_unregister_drivers(struct platform_driver * const *drivers,
810                                  unsigned int count)
811 {
812         while (count--) {
813                 pr_debug("unregistering platform driver %ps\n", drivers[count]);
814                 platform_driver_unregister(drivers[count]);
815         }
816 }
817 EXPORT_SYMBOL_GPL(platform_unregister_drivers);
818
819 /* modalias support enables more hands-off userspace setup:
820  * (a) environment variable lets new-style hotplug events work once system is
821  *     fully running:  "modprobe $MODALIAS"
822  * (b) sysfs attribute lets new-style coldplug recover from hotplug events
823  *     mishandled before system is fully running:  "modprobe $(cat modalias)"
824  */
825 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
826                              char *buf)
827 {
828         struct platform_device  *pdev = to_platform_device(dev);
829         int len;
830
831         len = of_device_get_modalias(dev, buf, PAGE_SIZE -1);
832         if (len != -ENODEV)
833                 return len;
834
835         len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
836         if (len != -ENODEV)
837                 return len;
838
839         len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
840
841         return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
842 }
843 static DEVICE_ATTR_RO(modalias);
844
845 static ssize_t driver_override_store(struct device *dev,
846                                      struct device_attribute *attr,
847                                      const char *buf, size_t count)
848 {
849         struct platform_device *pdev = to_platform_device(dev);
850         char *driver_override, *old = pdev->driver_override, *cp;
851
852         if (count > PATH_MAX)
853                 return -EINVAL;
854
855         driver_override = kstrndup(buf, count, GFP_KERNEL);
856         if (!driver_override)
857                 return -ENOMEM;
858
859         cp = strchr(driver_override, '\n');
860         if (cp)
861                 *cp = '\0';
862
863         if (strlen(driver_override)) {
864                 pdev->driver_override = driver_override;
865         } else {
866                 kfree(driver_override);
867                 pdev->driver_override = NULL;
868         }
869
870         kfree(old);
871
872         return count;
873 }
874
875 static ssize_t driver_override_show(struct device *dev,
876                                     struct device_attribute *attr, char *buf)
877 {
878         struct platform_device *pdev = to_platform_device(dev);
879
880         return sprintf(buf, "%s\n", pdev->driver_override);
881 }
882 static DEVICE_ATTR_RW(driver_override);
883
884
885 static struct attribute *platform_dev_attrs[] = {
886         &dev_attr_modalias.attr,
887         &dev_attr_driver_override.attr,
888         NULL,
889 };
890 ATTRIBUTE_GROUPS(platform_dev);
891
892 static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
893 {
894         struct platform_device  *pdev = to_platform_device(dev);
895         int rc;
896
897         /* Some devices have extra OF data and an OF-style MODALIAS */
898         rc = of_device_uevent_modalias(dev, env);
899         if (rc != -ENODEV)
900                 return rc;
901
902         rc = acpi_device_uevent_modalias(dev, env);
903         if (rc != -ENODEV)
904                 return rc;
905
906         add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
907                         pdev->name);
908         return 0;
909 }
910
911 static const struct platform_device_id *platform_match_id(
912                         const struct platform_device_id *id,
913                         struct platform_device *pdev)
914 {
915         while (id->name[0]) {
916                 if (strcmp(pdev->name, id->name) == 0) {
917                         pdev->id_entry = id;
918                         return id;
919                 }
920                 id++;
921         }
922         return NULL;
923 }
924
925 /**
926  * platform_match - bind platform device to platform driver.
927  * @dev: device.
928  * @drv: driver.
929  *
930  * Platform device IDs are assumed to be encoded like this:
931  * "<name><instance>", where <name> is a short description of the type of
932  * device, like "pci" or "floppy", and <instance> is the enumerated
933  * instance of the device, like '0' or '42'.  Driver IDs are simply
934  * "<name>".  So, extract the <name> from the platform_device structure,
935  * and compare it against the name of the driver. Return whether they match
936  * or not.
937  */
938 static int platform_match(struct device *dev, struct device_driver *drv)
939 {
940         struct platform_device *pdev = to_platform_device(dev);
941         struct platform_driver *pdrv = to_platform_driver(drv);
942
943         /* When driver_override is set, only bind to the matching driver */
944         if (pdev->driver_override)
945                 return !strcmp(pdev->driver_override, drv->name);
946
947         /* Attempt an OF style match first */
948         if (of_driver_match_device(dev, drv))
949                 return 1;
950
951         /* Then try ACPI style match */
952         if (acpi_driver_match_device(dev, drv))
953                 return 1;
954
955         /* Then try to match against the id table */
956         if (pdrv->id_table)
957                 return platform_match_id(pdrv->id_table, pdev) != NULL;
958
959         /* fall-back to driver name match */
960         return (strcmp(pdev->name, drv->name) == 0);
961 }
962
963 #ifdef CONFIG_PM_SLEEP
964
965 static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
966 {
967         struct platform_driver *pdrv = to_platform_driver(dev->driver);
968         struct platform_device *pdev = to_platform_device(dev);
969         int ret = 0;
970
971         if (dev->driver && pdrv->suspend)
972                 ret = pdrv->suspend(pdev, mesg);
973
974         return ret;
975 }
976
977 static int platform_legacy_resume(struct device *dev)
978 {
979         struct platform_driver *pdrv = to_platform_driver(dev->driver);
980         struct platform_device *pdev = to_platform_device(dev);
981         int ret = 0;
982
983         if (dev->driver && pdrv->resume)
984                 ret = pdrv->resume(pdev);
985
986         return ret;
987 }
988
989 #endif /* CONFIG_PM_SLEEP */
990
991 #ifdef CONFIG_SUSPEND
992
993 int platform_pm_suspend(struct device *dev)
994 {
995         struct device_driver *drv = dev->driver;
996         int ret = 0;
997
998         if (!drv)
999                 return 0;
1000
1001         if (drv->pm) {
1002                 if (drv->pm->suspend)
1003                         ret = drv->pm->suspend(dev);
1004         } else {
1005                 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
1006         }
1007
1008         return ret;
1009 }
1010
1011 int platform_pm_resume(struct device *dev)
1012 {
1013         struct device_driver *drv = dev->driver;
1014         int ret = 0;
1015
1016         if (!drv)
1017                 return 0;
1018
1019         if (drv->pm) {
1020                 if (drv->pm->resume)
1021                         ret = drv->pm->resume(dev);
1022         } else {
1023                 ret = platform_legacy_resume(dev);
1024         }
1025
1026         return ret;
1027 }
1028
1029 #endif /* CONFIG_SUSPEND */
1030
1031 #ifdef CONFIG_HIBERNATE_CALLBACKS
1032
1033 int platform_pm_freeze(struct device *dev)
1034 {
1035         struct device_driver *drv = dev->driver;
1036         int ret = 0;
1037
1038         if (!drv)
1039                 return 0;
1040
1041         if (drv->pm) {
1042                 if (drv->pm->freeze)
1043                         ret = drv->pm->freeze(dev);
1044         } else {
1045                 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
1046         }
1047
1048         return ret;
1049 }
1050
1051 int platform_pm_thaw(struct device *dev)
1052 {
1053         struct device_driver *drv = dev->driver;
1054         int ret = 0;
1055
1056         if (!drv)
1057                 return 0;
1058
1059         if (drv->pm) {
1060                 if (drv->pm->thaw)
1061                         ret = drv->pm->thaw(dev);
1062         } else {
1063                 ret = platform_legacy_resume(dev);
1064         }
1065
1066         return ret;
1067 }
1068
1069 int platform_pm_poweroff(struct device *dev)
1070 {
1071         struct device_driver *drv = dev->driver;
1072         int ret = 0;
1073
1074         if (!drv)
1075                 return 0;
1076
1077         if (drv->pm) {
1078                 if (drv->pm->poweroff)
1079                         ret = drv->pm->poweroff(dev);
1080         } else {
1081                 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
1082         }
1083
1084         return ret;
1085 }
1086
1087 int platform_pm_restore(struct device *dev)
1088 {
1089         struct device_driver *drv = dev->driver;
1090         int ret = 0;
1091
1092         if (!drv)
1093                 return 0;
1094
1095         if (drv->pm) {
1096                 if (drv->pm->restore)
1097                         ret = drv->pm->restore(dev);
1098         } else {
1099                 ret = platform_legacy_resume(dev);
1100         }
1101
1102         return ret;
1103 }
1104
1105 #endif /* CONFIG_HIBERNATE_CALLBACKS */
1106
1107 static const struct dev_pm_ops platform_dev_pm_ops = {
1108         .runtime_suspend = pm_generic_runtime_suspend,
1109         .runtime_resume = pm_generic_runtime_resume,
1110         USE_PLATFORM_PM_SLEEP_OPS
1111 };
1112
1113 struct bus_type platform_bus_type = {
1114         .name           = "platform",
1115         .dev_groups     = platform_dev_groups,
1116         .match          = platform_match,
1117         .uevent         = platform_uevent,
1118         .pm             = &platform_dev_pm_ops,
1119 };
1120 EXPORT_SYMBOL_GPL(platform_bus_type);
1121
1122 int __init platform_bus_init(void)
1123 {
1124         int error;
1125
1126         early_platform_cleanup();
1127
1128         error = device_register(&platform_bus);
1129         if (error)
1130                 return error;
1131         error =  bus_register(&platform_bus_type);
1132         if (error)
1133                 device_unregister(&platform_bus);
1134         of_platform_register_reconfig_notifier();
1135         return error;
1136 }
1137
1138 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
1139 u64 dma_get_required_mask(struct device *dev)
1140 {
1141         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
1142         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
1143         u64 mask;
1144
1145         if (!high_totalram) {
1146                 /* convert to mask just covering totalram */
1147                 low_totalram = (1 << (fls(low_totalram) - 1));
1148                 low_totalram += low_totalram - 1;
1149                 mask = low_totalram;
1150         } else {
1151                 high_totalram = (1 << (fls(high_totalram) - 1));
1152                 high_totalram += high_totalram - 1;
1153                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
1154         }
1155         return mask;
1156 }
1157 EXPORT_SYMBOL_GPL(dma_get_required_mask);
1158 #endif
1159
1160 static __initdata LIST_HEAD(early_platform_driver_list);
1161 static __initdata LIST_HEAD(early_platform_device_list);
1162
1163 /**
1164  * early_platform_driver_register - register early platform driver
1165  * @epdrv: early_platform driver structure
1166  * @buf: string passed from early_param()
1167  *
1168  * Helper function for early_platform_init() / early_platform_init_buffer()
1169  */
1170 int __init early_platform_driver_register(struct early_platform_driver *epdrv,
1171                                           char *buf)
1172 {
1173         char *tmp;
1174         int n;
1175
1176         /* Simply add the driver to the end of the global list.
1177          * Drivers will by default be put on the list in compiled-in order.
1178          */
1179         if (!epdrv->list.next) {
1180                 INIT_LIST_HEAD(&epdrv->list);
1181                 list_add_tail(&epdrv->list, &early_platform_driver_list);
1182         }
1183
1184         /* If the user has specified device then make sure the driver
1185          * gets prioritized. The driver of the last device specified on
1186          * command line will be put first on the list.
1187          */
1188         n = strlen(epdrv->pdrv->driver.name);
1189         if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
1190                 list_move(&epdrv->list, &early_platform_driver_list);
1191
1192                 /* Allow passing parameters after device name */
1193                 if (buf[n] == '\0' || buf[n] == ',')
1194                         epdrv->requested_id = -1;
1195                 else {
1196                         epdrv->requested_id = simple_strtoul(&buf[n + 1],
1197                                                              &tmp, 10);
1198
1199                         if (buf[n] != '.' || (tmp == &buf[n + 1])) {
1200                                 epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
1201                                 n = 0;
1202                         } else
1203                                 n += strcspn(&buf[n + 1], ",") + 1;
1204                 }
1205
1206                 if (buf[n] == ',')
1207                         n++;
1208
1209                 if (epdrv->bufsize) {
1210                         memcpy(epdrv->buffer, &buf[n],
1211                                min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
1212                         epdrv->buffer[epdrv->bufsize - 1] = '\0';
1213                 }
1214         }
1215
1216         return 0;
1217 }
1218
1219 /**
1220  * early_platform_add_devices - adds a number of early platform devices
1221  * @devs: array of early platform devices to add
1222  * @num: number of early platform devices in array
1223  *
1224  * Used by early architecture code to register early platform devices and
1225  * their platform data.
1226  */
1227 void __init early_platform_add_devices(struct platform_device **devs, int num)
1228 {
1229         struct device *dev;
1230         int i;
1231
1232         /* simply add the devices to list */
1233         for (i = 0; i < num; i++) {
1234                 dev = &devs[i]->dev;
1235
1236                 if (!dev->devres_head.next) {
1237                         pm_runtime_early_init(dev);
1238                         INIT_LIST_HEAD(&dev->devres_head);
1239                         list_add_tail(&dev->devres_head,
1240                                       &early_platform_device_list);
1241                 }
1242         }
1243 }
1244
1245 /**
1246  * early_platform_driver_register_all - register early platform drivers
1247  * @class_str: string to identify early platform driver class
1248  *
1249  * Used by architecture code to register all early platform drivers
1250  * for a certain class. If omitted then only early platform drivers
1251  * with matching kernel command line class parameters will be registered.
1252  */
1253 void __init early_platform_driver_register_all(char *class_str)
1254 {
1255         /* The "class_str" parameter may or may not be present on the kernel
1256          * command line. If it is present then there may be more than one
1257          * matching parameter.
1258          *
1259          * Since we register our early platform drivers using early_param()
1260          * we need to make sure that they also get registered in the case
1261          * when the parameter is missing from the kernel command line.
1262          *
1263          * We use parse_early_options() to make sure the early_param() gets
1264          * called at least once. The early_param() may be called more than
1265          * once since the name of the preferred device may be specified on
1266          * the kernel command line. early_platform_driver_register() handles
1267          * this case for us.
1268          */
1269         parse_early_options(class_str);
1270 }
1271
1272 /**
1273  * early_platform_match - find early platform device matching driver
1274  * @epdrv: early platform driver structure
1275  * @id: id to match against
1276  */
1277 static struct platform_device * __init
1278 early_platform_match(struct early_platform_driver *epdrv, int id)
1279 {
1280         struct platform_device *pd;
1281
1282         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1283                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1284                         if (pd->id == id)
1285                                 return pd;
1286
1287         return NULL;
1288 }
1289
1290 /**
1291  * early_platform_left - check if early platform driver has matching devices
1292  * @epdrv: early platform driver structure
1293  * @id: return true if id or above exists
1294  */
1295 static int __init early_platform_left(struct early_platform_driver *epdrv,
1296                                        int id)
1297 {
1298         struct platform_device *pd;
1299
1300         list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
1301                 if (platform_match(&pd->dev, &epdrv->pdrv->driver))
1302                         if (pd->id >= id)
1303                                 return 1;
1304
1305         return 0;
1306 }
1307
1308 /**
1309  * early_platform_driver_probe_id - probe drivers matching class_str and id
1310  * @class_str: string to identify early platform driver class
1311  * @id: id to match against
1312  * @nr_probe: number of platform devices to successfully probe before exiting
1313  */
1314 static int __init early_platform_driver_probe_id(char *class_str,
1315                                                  int id,
1316                                                  int nr_probe)
1317 {
1318         struct early_platform_driver *epdrv;
1319         struct platform_device *match;
1320         int match_id;
1321         int n = 0;
1322         int left = 0;
1323
1324         list_for_each_entry(epdrv, &early_platform_driver_list, list) {
1325                 /* only use drivers matching our class_str */
1326                 if (strcmp(class_str, epdrv->class_str))
1327                         continue;
1328
1329                 if (id == -2) {
1330                         match_id = epdrv->requested_id;
1331                         left = 1;
1332
1333                 } else {
1334                         match_id = id;
1335                         left += early_platform_left(epdrv, id);
1336
1337                         /* skip requested id */
1338                         switch (epdrv->requested_id) {
1339                         case EARLY_PLATFORM_ID_ERROR:
1340                         case EARLY_PLATFORM_ID_UNSET:
1341                                 break;
1342                         default:
1343                                 if (epdrv->requested_id == id)
1344                                         match_id = EARLY_PLATFORM_ID_UNSET;
1345                         }
1346                 }
1347
1348                 switch (match_id) {
1349                 case EARLY_PLATFORM_ID_ERROR:
1350                         pr_warn("%s: unable to parse %s parameter\n",
1351                                 class_str, epdrv->pdrv->driver.name);
1352                         /* fall-through */
1353                 case EARLY_PLATFORM_ID_UNSET:
1354                         match = NULL;
1355                         break;
1356                 default:
1357                         match = early_platform_match(epdrv, match_id);
1358                 }
1359
1360                 if (match) {
1361                         /*
1362                          * Set up a sensible init_name to enable
1363                          * dev_name() and others to be used before the
1364                          * rest of the driver core is initialized.
1365                          */
1366                         if (!match->dev.init_name && slab_is_available()) {
1367                                 if (match->id != -1)
1368                                         match->dev.init_name =
1369                                                 kasprintf(GFP_KERNEL, "%s.%d",
1370                                                           match->name,
1371                                                           match->id);
1372                                 else
1373                                         match->dev.init_name =
1374                                                 kasprintf(GFP_KERNEL, "%s",
1375                                                           match->name);
1376
1377                                 if (!match->dev.init_name)
1378                                         return -ENOMEM;
1379                         }
1380
1381                         if (epdrv->pdrv->probe(match))
1382                                 pr_warn("%s: unable to probe %s early.\n",
1383                                         class_str, match->name);
1384                         else
1385                                 n++;
1386                 }
1387
1388                 if (n >= nr_probe)
1389                         break;
1390         }
1391
1392         if (left)
1393                 return n;
1394         else
1395                 return -ENODEV;
1396 }
1397
1398 /**
1399  * early_platform_driver_probe - probe a class of registered drivers
1400  * @class_str: string to identify early platform driver class
1401  * @nr_probe: number of platform devices to successfully probe before exiting
1402  * @user_only: only probe user specified early platform devices
1403  *
1404  * Used by architecture code to probe registered early platform drivers
1405  * within a certain class. For probe to happen a registered early platform
1406  * device matching a registered early platform driver is needed.
1407  */
1408 int __init early_platform_driver_probe(char *class_str,
1409                                        int nr_probe,
1410                                        int user_only)
1411 {
1412         int k, n, i;
1413
1414         n = 0;
1415         for (i = -2; n < nr_probe; i++) {
1416                 k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
1417
1418                 if (k < 0)
1419                         break;
1420
1421                 n += k;
1422
1423                 if (user_only)
1424                         break;
1425         }
1426
1427         return n;
1428 }
1429
1430 /**
1431  * early_platform_cleanup - clean up early platform code
1432  */
1433 void __init early_platform_cleanup(void)
1434 {
1435         struct platform_device *pd, *pd2;
1436
1437         /* clean up the devres list used to chain devices */
1438         list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
1439                                  dev.devres_head) {
1440                 list_del(&pd->dev.devres_head);
1441                 memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
1442         }
1443 }
1444