xref: /linux/drivers/pmdomain/bcm/raspberrypi-power.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1e2ad626fSUlf Hansson // SPDX-License-Identifier: GPL-2.0
2e2ad626fSUlf Hansson /* (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
3e2ad626fSUlf Hansson  *
4e2ad626fSUlf Hansson  * Authors:
5e2ad626fSUlf Hansson  * Alexander Aring <aar@pengutronix.de>
6e2ad626fSUlf Hansson  * Eric Anholt <eric@anholt.net>
7e2ad626fSUlf Hansson  */
8e2ad626fSUlf Hansson 
9e2ad626fSUlf Hansson #include <linux/module.h>
10e2ad626fSUlf Hansson #include <linux/of.h>
11e2ad626fSUlf Hansson #include <linux/platform_device.h>
12e2ad626fSUlf Hansson #include <linux/pm_domain.h>
13e2ad626fSUlf Hansson #include <dt-bindings/power/raspberrypi-power.h>
14e2ad626fSUlf Hansson #include <soc/bcm2835/raspberrypi-firmware.h>
15e2ad626fSUlf Hansson 
16e2ad626fSUlf Hansson /*
17e2ad626fSUlf Hansson  * Firmware indices for the old power domains interface.  Only a few
18e2ad626fSUlf Hansson  * of them were actually implemented.
19e2ad626fSUlf Hansson  */
20e2ad626fSUlf Hansson #define RPI_OLD_POWER_DOMAIN_USB		3
21e2ad626fSUlf Hansson #define RPI_OLD_POWER_DOMAIN_V3D		10
22e2ad626fSUlf Hansson 
23e2ad626fSUlf Hansson struct rpi_power_domain {
24e2ad626fSUlf Hansson 	u32 domain;
25e2ad626fSUlf Hansson 	bool enabled;
26e2ad626fSUlf Hansson 	bool old_interface;
27e2ad626fSUlf Hansson 	struct generic_pm_domain base;
28e2ad626fSUlf Hansson 	struct rpi_firmware *fw;
29e2ad626fSUlf Hansson };
30e2ad626fSUlf Hansson 
31e2ad626fSUlf Hansson struct rpi_power_domains {
32e2ad626fSUlf Hansson 	bool has_new_interface;
33e2ad626fSUlf Hansson 	struct genpd_onecell_data xlate;
34e2ad626fSUlf Hansson 	struct rpi_firmware *fw;
35e2ad626fSUlf Hansson 	struct rpi_power_domain domains[RPI_POWER_DOMAIN_COUNT];
36e2ad626fSUlf Hansson };
37e2ad626fSUlf Hansson 
38e2ad626fSUlf Hansson /*
39e2ad626fSUlf Hansson  * Packet definition used by RPI_FIRMWARE_SET_POWER_STATE and
40e2ad626fSUlf Hansson  * RPI_FIRMWARE_SET_DOMAIN_STATE
41e2ad626fSUlf Hansson  */
42e2ad626fSUlf Hansson struct rpi_power_domain_packet {
43e2ad626fSUlf Hansson 	u32 domain;
4449d2a1ecSStefan Wahren 	u32 state;
45e2ad626fSUlf Hansson };
46e2ad626fSUlf Hansson 
47e2ad626fSUlf Hansson /*
48e2ad626fSUlf Hansson  * Asks the firmware to enable or disable power on a specific power
49e2ad626fSUlf Hansson  * domain.
50e2ad626fSUlf Hansson  */
rpi_firmware_set_power(struct generic_pm_domain * domain,bool on)51eb3896eaSStefan Wahren static int rpi_firmware_set_power(struct generic_pm_domain *domain, bool on)
52e2ad626fSUlf Hansson {
53eb3896eaSStefan Wahren 	struct rpi_power_domain *rpi_domain =
54eb3896eaSStefan Wahren 		container_of(domain, struct rpi_power_domain, base);
55eb3896eaSStefan Wahren 	bool old_interface = rpi_domain->old_interface;
56e2ad626fSUlf Hansson 	struct rpi_power_domain_packet packet;
57eb3896eaSStefan Wahren 	int ret;
58e2ad626fSUlf Hansson 
59e2ad626fSUlf Hansson 	packet.domain = rpi_domain->domain;
6049d2a1ecSStefan Wahren 	packet.state = on;
61eb3896eaSStefan Wahren 
62eb3896eaSStefan Wahren 	ret = rpi_firmware_property(rpi_domain->fw, old_interface ?
63e2ad626fSUlf Hansson 				    RPI_FIRMWARE_SET_POWER_STATE :
64e2ad626fSUlf Hansson 				    RPI_FIRMWARE_SET_DOMAIN_STATE,
65e2ad626fSUlf Hansson 				    &packet, sizeof(packet));
66eb3896eaSStefan Wahren 	if (ret)
67eb3896eaSStefan Wahren 		dev_err(&domain->dev, "Failed to set %s to %u (%d)\n",
68eb3896eaSStefan Wahren 			old_interface ? "power" : "domain", on, ret);
69eb3896eaSStefan Wahren 	else
70eb3896eaSStefan Wahren 		dev_dbg(&domain->dev, "Set %s to %u\n",
71eb3896eaSStefan Wahren 			old_interface ? "power" : "domain", on);
72eb3896eaSStefan Wahren 
73eb3896eaSStefan Wahren 	return ret;
74e2ad626fSUlf Hansson }
75e2ad626fSUlf Hansson 
rpi_domain_off(struct generic_pm_domain * domain)76e2ad626fSUlf Hansson static int rpi_domain_off(struct generic_pm_domain *domain)
77e2ad626fSUlf Hansson {
78eb3896eaSStefan Wahren 	return rpi_firmware_set_power(domain, false);
79e2ad626fSUlf Hansson }
80e2ad626fSUlf Hansson 
rpi_domain_on(struct generic_pm_domain * domain)81e2ad626fSUlf Hansson static int rpi_domain_on(struct generic_pm_domain *domain)
82e2ad626fSUlf Hansson {
83eb3896eaSStefan Wahren 	return rpi_firmware_set_power(domain, true);
84e2ad626fSUlf Hansson }
85e2ad626fSUlf Hansson 
rpi_common_init_power_domain(struct rpi_power_domains * rpi_domains,int xlate_index,const char * name)86e2ad626fSUlf Hansson static void rpi_common_init_power_domain(struct rpi_power_domains *rpi_domains,
87e2ad626fSUlf Hansson 					 int xlate_index, const char *name)
88e2ad626fSUlf Hansson {
89e2ad626fSUlf Hansson 	struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
90e2ad626fSUlf Hansson 
91e2ad626fSUlf Hansson 	dom->fw = rpi_domains->fw;
92e2ad626fSUlf Hansson 
93e2ad626fSUlf Hansson 	dom->base.name = name;
94*562cdeadSStefan Wahren 	dom->base.flags = GENPD_FLAG_ACTIVE_WAKEUP;
95e2ad626fSUlf Hansson 	dom->base.power_on = rpi_domain_on;
96e2ad626fSUlf Hansson 	dom->base.power_off = rpi_domain_off;
97e2ad626fSUlf Hansson 
98e2ad626fSUlf Hansson 	/*
99e2ad626fSUlf Hansson 	 * Treat all power domains as off at boot.
100e2ad626fSUlf Hansson 	 *
101e2ad626fSUlf Hansson 	 * The firmware itself may be keeping some domains on, but
102e2ad626fSUlf Hansson 	 * from Linux's perspective all we control is the refcounts
103e2ad626fSUlf Hansson 	 * that we give to the firmware, and we can't ask the firmware
104e2ad626fSUlf Hansson 	 * to turn off something that we haven't ourselves turned on.
105e2ad626fSUlf Hansson 	 */
106e2ad626fSUlf Hansson 	pm_genpd_init(&dom->base, NULL, true);
107e2ad626fSUlf Hansson 
108e2ad626fSUlf Hansson 	rpi_domains->xlate.domains[xlate_index] = &dom->base;
109e2ad626fSUlf Hansson }
110e2ad626fSUlf Hansson 
rpi_init_power_domain(struct rpi_power_domains * rpi_domains,int xlate_index,const char * name)111e2ad626fSUlf Hansson static void rpi_init_power_domain(struct rpi_power_domains *rpi_domains,
112e2ad626fSUlf Hansson 				  int xlate_index, const char *name)
113e2ad626fSUlf Hansson {
114e2ad626fSUlf Hansson 	struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
115e2ad626fSUlf Hansson 
116e2ad626fSUlf Hansson 	if (!rpi_domains->has_new_interface)
117e2ad626fSUlf Hansson 		return;
118e2ad626fSUlf Hansson 
119e2ad626fSUlf Hansson 	/* The DT binding index is the firmware's domain index minus one. */
120e2ad626fSUlf Hansson 	dom->domain = xlate_index + 1;
121e2ad626fSUlf Hansson 
122e2ad626fSUlf Hansson 	rpi_common_init_power_domain(rpi_domains, xlate_index, name);
123e2ad626fSUlf Hansson }
124e2ad626fSUlf Hansson 
rpi_init_old_power_domain(struct rpi_power_domains * rpi_domains,int xlate_index,int domain,const char * name)125e2ad626fSUlf Hansson static void rpi_init_old_power_domain(struct rpi_power_domains *rpi_domains,
126e2ad626fSUlf Hansson 				      int xlate_index, int domain,
127e2ad626fSUlf Hansson 				      const char *name)
128e2ad626fSUlf Hansson {
129e2ad626fSUlf Hansson 	struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
130e2ad626fSUlf Hansson 
131e2ad626fSUlf Hansson 	dom->old_interface = true;
132e2ad626fSUlf Hansson 	dom->domain = domain;
133e2ad626fSUlf Hansson 
134e2ad626fSUlf Hansson 	rpi_common_init_power_domain(rpi_domains, xlate_index, name);
135e2ad626fSUlf Hansson }
136e2ad626fSUlf Hansson 
137e2ad626fSUlf Hansson /*
138e2ad626fSUlf Hansson  * Detects whether the firmware supports the new power domains interface.
139e2ad626fSUlf Hansson  *
140e2ad626fSUlf Hansson  * The firmware doesn't actually return an error on an unknown tag,
141e2ad626fSUlf Hansson  * and just skips over it, so we do the detection by putting an
142e2ad626fSUlf Hansson  * unexpected value in the return field and checking if it was
143e2ad626fSUlf Hansson  * unchanged.
144e2ad626fSUlf Hansson  */
145e2ad626fSUlf Hansson static bool
rpi_has_new_domain_support(struct rpi_power_domains * rpi_domains)146e2ad626fSUlf Hansson rpi_has_new_domain_support(struct rpi_power_domains *rpi_domains)
147e2ad626fSUlf Hansson {
148e2ad626fSUlf Hansson 	struct rpi_power_domain_packet packet;
149e2ad626fSUlf Hansson 	int ret;
150e2ad626fSUlf Hansson 
151e2ad626fSUlf Hansson 	packet.domain = RPI_POWER_DOMAIN_ARM;
15249d2a1ecSStefan Wahren 	packet.state = ~0;
153e2ad626fSUlf Hansson 
154e2ad626fSUlf Hansson 	ret = rpi_firmware_property(rpi_domains->fw,
155e2ad626fSUlf Hansson 				    RPI_FIRMWARE_GET_DOMAIN_STATE,
156e2ad626fSUlf Hansson 				    &packet, sizeof(packet));
157e2ad626fSUlf Hansson 
15849d2a1ecSStefan Wahren 	return ret == 0 && packet.state != ~0;
159e2ad626fSUlf Hansson }
160e2ad626fSUlf Hansson 
rpi_power_probe(struct platform_device * pdev)161e2ad626fSUlf Hansson static int rpi_power_probe(struct platform_device *pdev)
162e2ad626fSUlf Hansson {
163e2ad626fSUlf Hansson 	struct device_node *fw_np;
164e2ad626fSUlf Hansson 	struct device *dev = &pdev->dev;
165e2ad626fSUlf Hansson 	struct rpi_power_domains *rpi_domains;
166e2ad626fSUlf Hansson 
167e2ad626fSUlf Hansson 	rpi_domains = devm_kzalloc(dev, sizeof(*rpi_domains), GFP_KERNEL);
168e2ad626fSUlf Hansson 	if (!rpi_domains)
169e2ad626fSUlf Hansson 		return -ENOMEM;
170e2ad626fSUlf Hansson 
171e2ad626fSUlf Hansson 	rpi_domains->xlate.domains =
172e2ad626fSUlf Hansson 		devm_kcalloc(dev,
173e2ad626fSUlf Hansson 			     RPI_POWER_DOMAIN_COUNT,
174e2ad626fSUlf Hansson 			     sizeof(*rpi_domains->xlate.domains),
175e2ad626fSUlf Hansson 			     GFP_KERNEL);
176e2ad626fSUlf Hansson 	if (!rpi_domains->xlate.domains)
177e2ad626fSUlf Hansson 		return -ENOMEM;
178e2ad626fSUlf Hansson 
179e2ad626fSUlf Hansson 	rpi_domains->xlate.num_domains = RPI_POWER_DOMAIN_COUNT;
180e2ad626fSUlf Hansson 
181e2ad626fSUlf Hansson 	fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
182e2ad626fSUlf Hansson 	if (!fw_np) {
183e2ad626fSUlf Hansson 		dev_err(&pdev->dev, "no firmware node\n");
184e2ad626fSUlf Hansson 		return -ENODEV;
185e2ad626fSUlf Hansson 	}
186e2ad626fSUlf Hansson 
187e2ad626fSUlf Hansson 	rpi_domains->fw = devm_rpi_firmware_get(&pdev->dev, fw_np);
188e2ad626fSUlf Hansson 	of_node_put(fw_np);
189e2ad626fSUlf Hansson 	if (!rpi_domains->fw)
190e2ad626fSUlf Hansson 		return -EPROBE_DEFER;
191e2ad626fSUlf Hansson 
192e2ad626fSUlf Hansson 	rpi_domains->has_new_interface =
193e2ad626fSUlf Hansson 		rpi_has_new_domain_support(rpi_domains);
194e2ad626fSUlf Hansson 
195e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C0, "I2C0");
196e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C1, "I2C1");
197e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C2, "I2C2");
198e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VIDEO_SCALER,
199e2ad626fSUlf Hansson 			      "VIDEO_SCALER");
200e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VPU1, "VPU1");
201e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_HDMI, "HDMI");
202e2ad626fSUlf Hansson 
203e2ad626fSUlf Hansson 	/*
204e2ad626fSUlf Hansson 	 * Use the old firmware interface for USB power, so that we
205e2ad626fSUlf Hansson 	 * can turn it on even if the firmware hasn't been updated.
206e2ad626fSUlf Hansson 	 */
207e2ad626fSUlf Hansson 	rpi_init_old_power_domain(rpi_domains, RPI_POWER_DOMAIN_USB,
208e2ad626fSUlf Hansson 				  RPI_OLD_POWER_DOMAIN_USB, "USB");
209e2ad626fSUlf Hansson 
210e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VEC, "VEC");
211e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_JPEG, "JPEG");
212e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_H264, "H264");
213e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_V3D, "V3D");
214e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ISP, "ISP");
215e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM0, "UNICAM0");
216e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM1, "UNICAM1");
217e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2RX, "CCP2RX");
218e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CSI2, "CSI2");
219e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CPI, "CPI");
220e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI0, "DSI0");
221e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI1, "DSI1");
222e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_TRANSPOSER,
223e2ad626fSUlf Hansson 			      "TRANSPOSER");
224e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2TX, "CCP2TX");
225e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CDP, "CDP");
226e2ad626fSUlf Hansson 	rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ARM, "ARM");
227e2ad626fSUlf Hansson 
228e2ad626fSUlf Hansson 	of_genpd_add_provider_onecell(dev->of_node, &rpi_domains->xlate);
229e2ad626fSUlf Hansson 
230e2ad626fSUlf Hansson 	platform_set_drvdata(pdev, rpi_domains);
231e2ad626fSUlf Hansson 
232e2ad626fSUlf Hansson 	return 0;
233e2ad626fSUlf Hansson }
234e2ad626fSUlf Hansson 
235e2ad626fSUlf Hansson static const struct of_device_id rpi_power_of_match[] = {
236e2ad626fSUlf Hansson 	{ .compatible = "raspberrypi,bcm2835-power", },
237e2ad626fSUlf Hansson 	{},
238e2ad626fSUlf Hansson };
239e2ad626fSUlf Hansson MODULE_DEVICE_TABLE(of, rpi_power_of_match);
240e2ad626fSUlf Hansson 
241e2ad626fSUlf Hansson static struct platform_driver rpi_power_driver = {
242e2ad626fSUlf Hansson 	.driver = {
243e2ad626fSUlf Hansson 		.name = "raspberrypi-power",
244e2ad626fSUlf Hansson 		.of_match_table = rpi_power_of_match,
245e2ad626fSUlf Hansson 	},
246e2ad626fSUlf Hansson 	.probe		= rpi_power_probe,
247e2ad626fSUlf Hansson };
248e2ad626fSUlf Hansson builtin_platform_driver(rpi_power_driver);
249e2ad626fSUlf Hansson 
250e2ad626fSUlf Hansson MODULE_AUTHOR("Alexander Aring <aar@pengutronix.de>");
251e2ad626fSUlf Hansson MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
252e2ad626fSUlf Hansson MODULE_DESCRIPTION("Raspberry Pi power domain driver");
253