xref: /openbmc/linux/drivers/bus/simple-pm-bus.c (revision 726ccdba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Simple Power-Managed Bus Driver
4  *
5  * Copyright (C) 2014-2015 Glider bvba
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 
18 struct simple_pm_bus {
19 	struct clk_bulk_data *clks;
20 	int num_clks;
21 };
22 
23 static int simple_pm_bus_probe(struct platform_device *pdev)
24 {
25 	const struct device *dev = &pdev->dev;
26 	const struct of_dev_auxdata *lookup = dev_get_platdata(dev);
27 	struct device_node *np = dev->of_node;
28 	const struct of_device_id *match;
29 	struct simple_pm_bus *bus;
30 
31 	/*
32 	 * Allow user to use driver_override to bind this driver to a
33 	 * transparent bus device which has a different compatible string
34 	 * that's not listed in simple_pm_bus_of_match. We don't want to do any
35 	 * of the simple-pm-bus tasks for these devices, so return early.
36 	 */
37 	if (pdev->driver_override)
38 		return 0;
39 
40 	match = of_match_device(dev->driver->of_match_table, dev);
41 	/*
42 	 * These are transparent bus devices (not simple-pm-bus matches) that
43 	 * have their child nodes populated automatically.  So, don't need to
44 	 * do anything more. We only match with the device if this driver is
45 	 * the most specific match because we don't want to incorrectly bind to
46 	 * a device that has a more specific driver.
47 	 */
48 	if (match && match->data) {
49 		if (of_property_match_string(np, "compatible", match->compatible) == 0)
50 			return 0;
51 		else
52 			return -ENODEV;
53 	}
54 
55 	bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
56 	if (!bus)
57 		return -ENOMEM;
58 
59 	bus->num_clks = devm_clk_bulk_get_all(&pdev->dev, &bus->clks);
60 	if (bus->num_clks < 0)
61 		return dev_err_probe(&pdev->dev, bus->num_clks, "failed to get clocks\n");
62 
63 	dev_set_drvdata(&pdev->dev, bus);
64 
65 	dev_dbg(&pdev->dev, "%s\n", __func__);
66 
67 	pm_runtime_enable(&pdev->dev);
68 
69 	if (np)
70 		of_platform_populate(np, NULL, lookup, &pdev->dev);
71 
72 	return 0;
73 }
74 
75 static int simple_pm_bus_remove(struct platform_device *pdev)
76 {
77 	const void *data = of_device_get_match_data(&pdev->dev);
78 
79 	if (pdev->driver_override || data)
80 		return 0;
81 
82 	dev_dbg(&pdev->dev, "%s\n", __func__);
83 
84 	pm_runtime_disable(&pdev->dev);
85 	return 0;
86 }
87 
88 static int simple_pm_bus_runtime_suspend(struct device *dev)
89 {
90 	struct simple_pm_bus *bus = dev_get_drvdata(dev);
91 
92 	clk_bulk_disable_unprepare(bus->num_clks, bus->clks);
93 
94 	return 0;
95 }
96 
97 static int simple_pm_bus_runtime_resume(struct device *dev)
98 {
99 	struct simple_pm_bus *bus = dev_get_drvdata(dev);
100 	int ret;
101 
102 	ret = clk_bulk_prepare_enable(bus->num_clks, bus->clks);
103 	if (ret) {
104 		dev_err(dev, "failed to enable clocks: %d\n", ret);
105 		return ret;
106 	}
107 
108 	return 0;
109 }
110 
111 static const struct dev_pm_ops simple_pm_bus_pm_ops = {
112 	RUNTIME_PM_OPS(simple_pm_bus_runtime_suspend, simple_pm_bus_runtime_resume, NULL)
113 	NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
114 };
115 
116 #define ONLY_BUS	((void *) 1) /* Match if the device is only a bus. */
117 
118 static const struct of_device_id simple_pm_bus_of_match[] = {
119 	{ .compatible = "simple-pm-bus", },
120 	{ .compatible = "simple-bus",	.data = ONLY_BUS },
121 	{ .compatible = "simple-mfd",	.data = ONLY_BUS },
122 	{ .compatible = "isa",		.data = ONLY_BUS },
123 	{ .compatible = "arm,amba-bus",	.data = ONLY_BUS },
124 	{ /* sentinel */ }
125 };
126 MODULE_DEVICE_TABLE(of, simple_pm_bus_of_match);
127 
128 static struct platform_driver simple_pm_bus_driver = {
129 	.probe = simple_pm_bus_probe,
130 	.remove = simple_pm_bus_remove,
131 	.driver = {
132 		.name = "simple-pm-bus",
133 		.of_match_table = simple_pm_bus_of_match,
134 		.pm = pm_ptr(&simple_pm_bus_pm_ops),
135 	},
136 };
137 
138 module_platform_driver(simple_pm_bus_driver);
139 
140 MODULE_DESCRIPTION("Simple Power-Managed Bus Driver");
141 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
142