xref: /linux/drivers/pmdomain/arm/scpi_pm_domain.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SCPI Generic power domain support.
4  *
5  * Copyright (C) 2016 ARM Ltd.
6  */
7 
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_domain.h>
14 #include <linux/scpi_protocol.h>
15 
16 struct scpi_pm_domain {
17 	struct generic_pm_domain genpd;
18 	struct scpi_ops *ops;
19 	u32 domain;
20 };
21 
22 /*
23  * These device power state values are not well-defined in the specification.
24  * In case, different implementations use different values, we can make these
25  * specific to compatibles rather than getting these values from device tree.
26  */
27 enum scpi_power_domain_state {
28 	SCPI_PD_STATE_ON = 0,
29 	SCPI_PD_STATE_OFF = 3,
30 };
31 
32 #define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
33 
34 static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
35 {
36 	int ret;
37 	enum scpi_power_domain_state state;
38 
39 	if (power_on)
40 		state = SCPI_PD_STATE_ON;
41 	else
42 		state = SCPI_PD_STATE_OFF;
43 
44 	ret = pd->ops->device_set_power_state(pd->domain, state);
45 	if (ret)
46 		return ret;
47 
48 	return !(state == pd->ops->device_get_power_state(pd->domain));
49 }
50 
51 static int scpi_pd_power_on(struct generic_pm_domain *domain)
52 {
53 	struct scpi_pm_domain *pd = to_scpi_pd(domain);
54 
55 	return scpi_pd_power(pd, true);
56 }
57 
58 static int scpi_pd_power_off(struct generic_pm_domain *domain)
59 {
60 	struct scpi_pm_domain *pd = to_scpi_pd(domain);
61 
62 	return scpi_pd_power(pd, false);
63 }
64 
65 static int scpi_pm_domain_probe(struct platform_device *pdev)
66 {
67 	struct device *dev = &pdev->dev;
68 	struct device_node *np = dev->of_node;
69 	struct scpi_pm_domain *scpi_pd;
70 	struct genpd_onecell_data *scpi_pd_data;
71 	struct generic_pm_domain **domains;
72 	struct scpi_ops *scpi_ops;
73 	int ret, num_domains, i;
74 
75 	scpi_ops = get_scpi_ops();
76 	if (!scpi_ops)
77 		return -EPROBE_DEFER;
78 
79 	if (!np) {
80 		dev_err(dev, "device tree node not found\n");
81 		return -ENODEV;
82 	}
83 
84 	if (!scpi_ops->device_set_power_state ||
85 	    !scpi_ops->device_get_power_state) {
86 		dev_err(dev, "power domains not supported in the firmware\n");
87 		return -ENODEV;
88 	}
89 
90 	ret = of_property_read_u32(np, "num-domains", &num_domains);
91 	if (ret) {
92 		dev_err(dev, "number of domains not found\n");
93 		return -EINVAL;
94 	}
95 
96 	scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
97 	if (!scpi_pd)
98 		return -ENOMEM;
99 
100 	scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
101 	if (!scpi_pd_data)
102 		return -ENOMEM;
103 
104 	domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
105 	if (!domains)
106 		return -ENOMEM;
107 
108 	for (i = 0; i < num_domains; i++, scpi_pd++) {
109 		domains[i] = &scpi_pd->genpd;
110 
111 		scpi_pd->domain = i;
112 		scpi_pd->ops = scpi_ops;
113 		scpi_pd->genpd.name = devm_kasprintf(dev, GFP_KERNEL,
114 						     "%pOFn.%d", np, i);
115 		if (!scpi_pd->genpd.name) {
116 			dev_err(dev, "Failed to allocate genpd name:%pOFn.%d\n",
117 				np, i);
118 			continue;
119 		}
120 		scpi_pd->genpd.power_off = scpi_pd_power_off;
121 		scpi_pd->genpd.power_on = scpi_pd_power_on;
122 
123 		/*
124 		 * Treat all power domains as off at boot.
125 		 *
126 		 * The SCP firmware itself may have switched on some domains,
127 		 * but for reference counting purpose, keep it this way.
128 		 */
129 		pm_genpd_init(&scpi_pd->genpd, NULL, true);
130 	}
131 
132 	scpi_pd_data->domains = domains;
133 	scpi_pd_data->num_domains = num_domains;
134 
135 	of_genpd_add_provider_onecell(np, scpi_pd_data);
136 
137 	return 0;
138 }
139 
140 static const struct of_device_id scpi_power_domain_ids[] = {
141 	{ .compatible = "arm,scpi-power-domains", },
142 	{ /* sentinel */ }
143 };
144 MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
145 
146 static struct platform_driver scpi_power_domain_driver = {
147 	.driver	= {
148 		.name = "scpi_power_domain",
149 		.of_match_table = scpi_power_domain_ids,
150 	},
151 	.probe = scpi_pm_domain_probe,
152 };
153 module_platform_driver(scpi_power_domain_driver);
154 
155 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
156 MODULE_DESCRIPTION("ARM SCPI power domain driver");
157 MODULE_LICENSE("GPL v2");
158