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