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