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