Linux 4.16.11
[linux/fpc-iii.git] / drivers / soc / bcm / raspberrypi-power.c
blobfe96a8b956fbd54424bea059af5b0be1240c91b3
1 /* (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
7 * Authors:
8 * Alexander Aring <aar@pengutronix.de>
9 * Eric Anholt <eric@anholt.net>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_domain.h>
16 #include <dt-bindings/power/raspberrypi-power.h>
17 #include <soc/bcm2835/raspberrypi-firmware.h>
20 * Firmware indices for the old power domains interface. Only a few
21 * of them were actually implemented.
23 #define RPI_OLD_POWER_DOMAIN_USB 3
24 #define RPI_OLD_POWER_DOMAIN_V3D 10
26 struct rpi_power_domain {
27 u32 domain;
28 bool enabled;
29 bool old_interface;
30 struct generic_pm_domain base;
31 struct rpi_firmware *fw;
34 struct rpi_power_domains {
35 bool has_new_interface;
36 struct genpd_onecell_data xlate;
37 struct rpi_firmware *fw;
38 struct rpi_power_domain domains[RPI_POWER_DOMAIN_COUNT];
42 * Packet definition used by RPI_FIRMWARE_SET_POWER_STATE and
43 * RPI_FIRMWARE_SET_DOMAIN_STATE
45 struct rpi_power_domain_packet {
46 u32 domain;
47 u32 on;
48 } __packet;
51 * Asks the firmware to enable or disable power on a specific power
52 * domain.
54 static int rpi_firmware_set_power(struct rpi_power_domain *rpi_domain, bool on)
56 struct rpi_power_domain_packet packet;
58 packet.domain = rpi_domain->domain;
59 packet.on = on;
60 return rpi_firmware_property(rpi_domain->fw,
61 rpi_domain->old_interface ?
62 RPI_FIRMWARE_SET_POWER_STATE :
63 RPI_FIRMWARE_SET_DOMAIN_STATE,
64 &packet, sizeof(packet));
67 static int rpi_domain_off(struct generic_pm_domain *domain)
69 struct rpi_power_domain *rpi_domain =
70 container_of(domain, struct rpi_power_domain, base);
72 return rpi_firmware_set_power(rpi_domain, false);
75 static int rpi_domain_on(struct generic_pm_domain *domain)
77 struct rpi_power_domain *rpi_domain =
78 container_of(domain, struct rpi_power_domain, base);
80 return rpi_firmware_set_power(rpi_domain, true);
83 static void rpi_common_init_power_domain(struct rpi_power_domains *rpi_domains,
84 int xlate_index, const char *name)
86 struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
88 dom->fw = rpi_domains->fw;
90 dom->base.name = name;
91 dom->base.power_on = rpi_domain_on;
92 dom->base.power_off = rpi_domain_off;
95 * Treat all power domains as off at boot.
97 * The firmware itself may be keeping some domains on, but
98 * from Linux's perspective all we control is the refcounts
99 * that we give to the firmware, and we can't ask the firmware
100 * to turn off something that we haven't ourselves turned on.
102 pm_genpd_init(&dom->base, NULL, true);
104 rpi_domains->xlate.domains[xlate_index] = &dom->base;
107 static void rpi_init_power_domain(struct rpi_power_domains *rpi_domains,
108 int xlate_index, const char *name)
110 struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
112 if (!rpi_domains->has_new_interface)
113 return;
115 /* The DT binding index is the firmware's domain index minus one. */
116 dom->domain = xlate_index + 1;
118 rpi_common_init_power_domain(rpi_domains, xlate_index, name);
121 static void rpi_init_old_power_domain(struct rpi_power_domains *rpi_domains,
122 int xlate_index, int domain,
123 const char *name)
125 struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
127 dom->old_interface = true;
128 dom->domain = domain;
130 rpi_common_init_power_domain(rpi_domains, xlate_index, name);
134 * Detects whether the firmware supports the new power domains interface.
136 * The firmware doesn't actually return an error on an unknown tag,
137 * and just skips over it, so we do the detection by putting an
138 * unexpected value in the return field and checking if it was
139 * unchanged.
141 static bool
142 rpi_has_new_domain_support(struct rpi_power_domains *rpi_domains)
144 struct rpi_power_domain_packet packet;
145 int ret;
147 packet.domain = RPI_POWER_DOMAIN_ARM;
148 packet.on = ~0;
150 ret = rpi_firmware_property(rpi_domains->fw,
151 RPI_FIRMWARE_GET_DOMAIN_STATE,
152 &packet, sizeof(packet));
154 return ret == 0 && packet.on != ~0;
157 static int rpi_power_probe(struct platform_device *pdev)
159 struct device_node *fw_np;
160 struct device *dev = &pdev->dev;
161 struct rpi_power_domains *rpi_domains;
163 rpi_domains = devm_kzalloc(dev, sizeof(*rpi_domains), GFP_KERNEL);
164 if (!rpi_domains)
165 return -ENOMEM;
167 rpi_domains->xlate.domains =
168 devm_kzalloc(dev, sizeof(*rpi_domains->xlate.domains) *
169 RPI_POWER_DOMAIN_COUNT, GFP_KERNEL);
170 if (!rpi_domains->xlate.domains)
171 return -ENOMEM;
173 rpi_domains->xlate.num_domains = RPI_POWER_DOMAIN_COUNT;
175 fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
176 if (!fw_np) {
177 dev_err(&pdev->dev, "no firmware node\n");
178 return -ENODEV;
181 rpi_domains->fw = rpi_firmware_get(fw_np);
182 of_node_put(fw_np);
183 if (!rpi_domains->fw)
184 return -EPROBE_DEFER;
186 rpi_domains->has_new_interface =
187 rpi_has_new_domain_support(rpi_domains);
189 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C0, "I2C0");
190 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C1, "I2C1");
191 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C2, "I2C2");
192 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VIDEO_SCALER,
193 "VIDEO_SCALER");
194 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VPU1, "VPU1");
195 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_HDMI, "HDMI");
198 * Use the old firmware interface for USB power, so that we
199 * can turn it on even if the firmware hasn't been updated.
201 rpi_init_old_power_domain(rpi_domains, RPI_POWER_DOMAIN_USB,
202 RPI_OLD_POWER_DOMAIN_USB, "USB");
204 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VEC, "VEC");
205 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_JPEG, "JPEG");
206 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_H264, "H264");
207 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_V3D, "V3D");
208 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ISP, "ISP");
209 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM0, "UNICAM0");
210 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM1, "UNICAM1");
211 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2RX, "CCP2RX");
212 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CSI2, "CSI2");
213 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CPI, "CPI");
214 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI0, "DSI0");
215 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI1, "DSI1");
216 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_TRANSPOSER,
217 "TRANSPOSER");
218 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2TX, "CCP2TX");
219 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CDP, "CDP");
220 rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ARM, "ARM");
222 of_genpd_add_provider_onecell(dev->of_node, &rpi_domains->xlate);
224 platform_set_drvdata(pdev, rpi_domains);
226 return 0;
229 static const struct of_device_id rpi_power_of_match[] = {
230 { .compatible = "raspberrypi,bcm2835-power", },
233 MODULE_DEVICE_TABLE(of, rpi_power_of_match);
235 static struct platform_driver rpi_power_driver = {
236 .driver = {
237 .name = "raspberrypi-power",
238 .of_match_table = rpi_power_of_match,
240 .probe = rpi_power_probe,
242 builtin_platform_driver(rpi_power_driver);
244 MODULE_AUTHOR("Alexander Aring <aar@pengutronix.de>");
245 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
246 MODULE_DESCRIPTION("Raspberry Pi power domain driver");
247 MODULE_LICENSE("GPL v2");