0265-ARM-bcm2835-add-rpi-power-domain-driver.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. From a25a9d99959c6508d26d5d48794334b11326bb4c Mon Sep 17 00:00:00 2001
  2. From: Alexander Aring <alex.aring@gmail.com>
  3. Date: Wed, 16 Dec 2015 16:26:47 -0800
  4. Subject: [PATCH] ARM: bcm2835: add rpi power domain driver
  5. This patch adds support for several power domains on Raspberry Pi,
  6. including USB (so it can be enabled even if the bootloader didn't do
  7. it), and graphics.
  8. This patch is the combined work of Eric Anholt (who wrote USB support
  9. inside of the Raspberry Pi firmware driver, and wrote the non-USB
  10. domain support) and Alexander Aring (who separated the original USB
  11. work out from the firmware driver).
  12. Signed-off-by: Alexander Aring <alex.aring@gmail.com>
  13. Signed-off-by: Eric Anholt <eric@anholt.net>
  14. Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>
  15. Reviewed-by: Kevin Hilman <khilman@linaro.org>
  16. (cherry picked from commit a09cd356586d33f64cbe64ee4f5c1a7c4a6abee5)
  17. ---
  18. drivers/soc/Kconfig | 1 +
  19. drivers/soc/Makefile | 1 +
  20. drivers/soc/bcm/Kconfig | 9 +
  21. drivers/soc/bcm/Makefile | 1 +
  22. drivers/soc/bcm/raspberrypi-power.c | 247 ++++++++++++++++++++++++++
  23. include/dt-bindings/power/raspberrypi-power.h | 41 +++++
  24. 6 files changed, 300 insertions(+)
  25. create mode 100644 drivers/soc/bcm/Kconfig
  26. create mode 100644 drivers/soc/bcm/Makefile
  27. create mode 100644 drivers/soc/bcm/raspberrypi-power.c
  28. create mode 100644 include/dt-bindings/power/raspberrypi-power.h
  29. --- a/drivers/soc/Kconfig
  30. +++ b/drivers/soc/Kconfig
  31. @@ -1,5 +1,6 @@
  32. menu "SOC (System On Chip) specific Drivers"
  33. +source "drivers/soc/bcm/Kconfig"
  34. source "drivers/soc/brcmstb/Kconfig"
  35. source "drivers/soc/mediatek/Kconfig"
  36. source "drivers/soc/qcom/Kconfig"
  37. --- a/drivers/soc/Makefile
  38. +++ b/drivers/soc/Makefile
  39. @@ -2,6 +2,7 @@
  40. # Makefile for the Linux Kernel SOC specific device drivers.
  41. #
  42. +obj-y += bcm/
  43. obj-$(CONFIG_SOC_BRCMSTB) += brcmstb/
  44. obj-$(CONFIG_MACH_DOVE) += dove/
  45. obj-$(CONFIG_ARCH_MEDIATEK) += mediatek/
  46. --- /dev/null
  47. +++ b/drivers/soc/bcm/Kconfig
  48. @@ -0,0 +1,9 @@
  49. +config RASPBERRYPI_POWER
  50. + bool "Raspberry Pi power domain driver"
  51. + depends on ARCH_BCM2835 || COMPILE_TEST
  52. + depends on RASPBERRYPI_FIRMWARE
  53. + select PM_GENERIC_DOMAINS if PM
  54. + select PM_GENERIC_DOMAINS_OF if PM
  55. + help
  56. + This enables support for the RPi power domains which can be enabled
  57. + or disabled via the RPi firmware.
  58. --- /dev/null
  59. +++ b/drivers/soc/bcm/Makefile
  60. @@ -0,0 +1 @@
  61. +obj-$(CONFIG_RASPBERRYPI_POWER) += raspberrypi-power.o
  62. --- /dev/null
  63. +++ b/drivers/soc/bcm/raspberrypi-power.c
  64. @@ -0,0 +1,247 @@
  65. +/* (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
  66. + *
  67. + * This program is free software; you can redistribute it and/or modify
  68. + * it under the terms of the GNU General Public License version 2 as
  69. + * published by the Free Software Foundation.
  70. + *
  71. + * Authors:
  72. + * Alexander Aring <aar@pengutronix.de>
  73. + * Eric Anholt <eric@anholt.net>
  74. + */
  75. +
  76. +#include <linux/module.h>
  77. +#include <linux/of_platform.h>
  78. +#include <linux/platform_device.h>
  79. +#include <linux/pm_domain.h>
  80. +#include <dt-bindings/power/raspberrypi-power.h>
  81. +#include <soc/bcm2835/raspberrypi-firmware.h>
  82. +
  83. +/*
  84. + * Firmware indices for the old power domains interface. Only a few
  85. + * of them were actually implemented.
  86. + */
  87. +#define RPI_OLD_POWER_DOMAIN_USB 3
  88. +#define RPI_OLD_POWER_DOMAIN_V3D 10
  89. +
  90. +struct rpi_power_domain {
  91. + u32 domain;
  92. + bool enabled;
  93. + bool old_interface;
  94. + struct generic_pm_domain base;
  95. + struct rpi_firmware *fw;
  96. +};
  97. +
  98. +struct rpi_power_domains {
  99. + bool has_new_interface;
  100. + struct genpd_onecell_data xlate;
  101. + struct rpi_firmware *fw;
  102. + struct rpi_power_domain domains[RPI_POWER_DOMAIN_COUNT];
  103. +};
  104. +
  105. +/*
  106. + * Packet definition used by RPI_FIRMWARE_SET_POWER_STATE and
  107. + * RPI_FIRMWARE_SET_DOMAIN_STATE
  108. + */
  109. +struct rpi_power_domain_packet {
  110. + u32 domain;
  111. + u32 on;
  112. +} __packet;
  113. +
  114. +/*
  115. + * Asks the firmware to enable or disable power on a specific power
  116. + * domain.
  117. + */
  118. +static int rpi_firmware_set_power(struct rpi_power_domain *rpi_domain, bool on)
  119. +{
  120. + struct rpi_power_domain_packet packet;
  121. +
  122. + packet.domain = rpi_domain->domain;
  123. + packet.on = on;
  124. + return rpi_firmware_property(rpi_domain->fw,
  125. + rpi_domain->old_interface ?
  126. + RPI_FIRMWARE_SET_POWER_STATE :
  127. + RPI_FIRMWARE_SET_DOMAIN_STATE,
  128. + &packet, sizeof(packet));
  129. +}
  130. +
  131. +static int rpi_domain_off(struct generic_pm_domain *domain)
  132. +{
  133. + struct rpi_power_domain *rpi_domain =
  134. + container_of(domain, struct rpi_power_domain, base);
  135. +
  136. + return rpi_firmware_set_power(rpi_domain, false);
  137. +}
  138. +
  139. +static int rpi_domain_on(struct generic_pm_domain *domain)
  140. +{
  141. + struct rpi_power_domain *rpi_domain =
  142. + container_of(domain, struct rpi_power_domain, base);
  143. +
  144. + return rpi_firmware_set_power(rpi_domain, true);
  145. +}
  146. +
  147. +static void rpi_common_init_power_domain(struct rpi_power_domains *rpi_domains,
  148. + int xlate_index, const char *name)
  149. +{
  150. + struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  151. +
  152. + dom->fw = rpi_domains->fw;
  153. +
  154. + dom->base.name = name;
  155. + dom->base.power_on = rpi_domain_on;
  156. + dom->base.power_off = rpi_domain_off;
  157. +
  158. + /*
  159. + * Treat all power domains as off at boot.
  160. + *
  161. + * The firmware itself may be keeping some domains on, but
  162. + * from Linux's perspective all we control is the refcounts
  163. + * that we give to the firmware, and we can't ask the firmware
  164. + * to turn off something that we haven't ourselves turned on.
  165. + */
  166. + pm_genpd_init(&dom->base, NULL, true);
  167. +
  168. + rpi_domains->xlate.domains[xlate_index] = &dom->base;
  169. +}
  170. +
  171. +static void rpi_init_power_domain(struct rpi_power_domains *rpi_domains,
  172. + int xlate_index, const char *name)
  173. +{
  174. + struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  175. +
  176. + if (!rpi_domains->has_new_interface)
  177. + return;
  178. +
  179. + /* The DT binding index is the firmware's domain index minus one. */
  180. + dom->domain = xlate_index + 1;
  181. +
  182. + rpi_common_init_power_domain(rpi_domains, xlate_index, name);
  183. +}
  184. +
  185. +static void rpi_init_old_power_domain(struct rpi_power_domains *rpi_domains,
  186. + int xlate_index, int domain,
  187. + const char *name)
  188. +{
  189. + struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  190. +
  191. + dom->old_interface = true;
  192. + dom->domain = domain;
  193. +
  194. + rpi_common_init_power_domain(rpi_domains, xlate_index, name);
  195. +}
  196. +
  197. +/*
  198. + * Detects whether the firmware supports the new power domains interface.
  199. + *
  200. + * The firmware doesn't actually return an error on an unknown tag,
  201. + * and just skips over it, so we do the detection by putting an
  202. + * unexpected value in the return field and checking if it was
  203. + * unchanged.
  204. + */
  205. +static bool
  206. +rpi_has_new_domain_support(struct rpi_power_domains *rpi_domains)
  207. +{
  208. + struct rpi_power_domain_packet packet;
  209. + int ret;
  210. +
  211. + packet.domain = RPI_POWER_DOMAIN_ARM;
  212. + packet.on = ~0;
  213. +
  214. + ret = rpi_firmware_property(rpi_domains->fw,
  215. + RPI_FIRMWARE_GET_DOMAIN_STATE,
  216. + &packet, sizeof(packet));
  217. +
  218. + return ret == 0 && packet.on != ~0;
  219. +}
  220. +
  221. +static int rpi_power_probe(struct platform_device *pdev)
  222. +{
  223. + struct device_node *fw_np;
  224. + struct device *dev = &pdev->dev;
  225. + struct rpi_power_domains *rpi_domains;
  226. +
  227. + rpi_domains = devm_kzalloc(dev, sizeof(*rpi_domains), GFP_KERNEL);
  228. + if (!rpi_domains)
  229. + return -ENOMEM;
  230. +
  231. + rpi_domains->xlate.domains =
  232. + devm_kzalloc(dev, sizeof(*rpi_domains->xlate.domains) *
  233. + RPI_POWER_DOMAIN_COUNT, GFP_KERNEL);
  234. + if (!rpi_domains->xlate.domains)
  235. + return -ENOMEM;
  236. +
  237. + rpi_domains->xlate.num_domains = RPI_POWER_DOMAIN_COUNT;
  238. +
  239. + fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
  240. + if (!fw_np) {
  241. + dev_err(&pdev->dev, "no firmware node\n");
  242. + return -ENODEV;
  243. + }
  244. +
  245. + rpi_domains->fw = rpi_firmware_get(fw_np);
  246. + of_node_put(fw_np);
  247. + if (!rpi_domains->fw)
  248. + return -EPROBE_DEFER;
  249. +
  250. + rpi_domains->has_new_interface =
  251. + rpi_has_new_domain_support(rpi_domains);
  252. +
  253. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C0, "I2C0");
  254. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C1, "I2C1");
  255. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C2, "I2C2");
  256. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VIDEO_SCALER,
  257. + "VIDEO_SCALER");
  258. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VPU1, "VPU1");
  259. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_HDMI, "HDMI");
  260. +
  261. + /*
  262. + * Use the old firmware interface for USB power, so that we
  263. + * can turn it on even if the firmware hasn't been updated.
  264. + */
  265. + rpi_init_old_power_domain(rpi_domains, RPI_POWER_DOMAIN_USB,
  266. + RPI_OLD_POWER_DOMAIN_USB, "USB");
  267. +
  268. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VEC, "VEC");
  269. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_JPEG, "JPEG");
  270. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_H264, "H264");
  271. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_V3D, "V3D");
  272. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ISP, "ISP");
  273. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM0, "UNICAM0");
  274. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM1, "UNICAM1");
  275. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2RX, "CCP2RX");
  276. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CSI2, "CSI2");
  277. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CPI, "CPI");
  278. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI0, "DSI0");
  279. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI1, "DSI1");
  280. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_TRANSPOSER,
  281. + "TRANSPOSER");
  282. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2TX, "CCP2TX");
  283. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CDP, "CDP");
  284. + rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ARM, "ARM");
  285. +
  286. + of_genpd_add_provider_onecell(dev->of_node, &rpi_domains->xlate);
  287. +
  288. + platform_set_drvdata(pdev, rpi_domains);
  289. +
  290. + return 0;
  291. +}
  292. +
  293. +static const struct of_device_id rpi_power_of_match[] = {
  294. + { .compatible = "raspberrypi,bcm2835-power", },
  295. + {},
  296. +};
  297. +MODULE_DEVICE_TABLE(of, rpi_power_of_match);
  298. +
  299. +static struct platform_driver rpi_power_driver = {
  300. + .driver = {
  301. + .name = "raspberrypi-power",
  302. + .of_match_table = rpi_power_of_match,
  303. + },
  304. + .probe = rpi_power_probe,
  305. +};
  306. +builtin_platform_driver(rpi_power_driver);
  307. +
  308. +MODULE_AUTHOR("Alexander Aring <aar@pengutronix.de>");
  309. +MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  310. +MODULE_DESCRIPTION("Raspberry Pi power domain driver");
  311. +MODULE_LICENSE("GPL v2");
  312. --- /dev/null
  313. +++ b/include/dt-bindings/power/raspberrypi-power.h
  314. @@ -0,0 +1,41 @@
  315. +/*
  316. + * Copyright © 2015 Broadcom
  317. + *
  318. + * This program is free software; you can redistribute it and/or modify
  319. + * it under the terms of the GNU General Public License version 2 as
  320. + * published by the Free Software Foundation.
  321. + */
  322. +
  323. +#ifndef _DT_BINDINGS_ARM_BCM2835_RPI_POWER_H
  324. +#define _DT_BINDINGS_ARM_BCM2835_RPI_POWER_H
  325. +
  326. +/* These power domain indices are the firmware interface's indices
  327. + * minus one.
  328. + */
  329. +#define RPI_POWER_DOMAIN_I2C0 0
  330. +#define RPI_POWER_DOMAIN_I2C1 1
  331. +#define RPI_POWER_DOMAIN_I2C2 2
  332. +#define RPI_POWER_DOMAIN_VIDEO_SCALER 3
  333. +#define RPI_POWER_DOMAIN_VPU1 4
  334. +#define RPI_POWER_DOMAIN_HDMI 5
  335. +#define RPI_POWER_DOMAIN_USB 6
  336. +#define RPI_POWER_DOMAIN_VEC 7
  337. +#define RPI_POWER_DOMAIN_JPEG 8
  338. +#define RPI_POWER_DOMAIN_H264 9
  339. +#define RPI_POWER_DOMAIN_V3D 10
  340. +#define RPI_POWER_DOMAIN_ISP 11
  341. +#define RPI_POWER_DOMAIN_UNICAM0 12
  342. +#define RPI_POWER_DOMAIN_UNICAM1 13
  343. +#define RPI_POWER_DOMAIN_CCP2RX 14
  344. +#define RPI_POWER_DOMAIN_CSI2 15
  345. +#define RPI_POWER_DOMAIN_CPI 16
  346. +#define RPI_POWER_DOMAIN_DSI0 17
  347. +#define RPI_POWER_DOMAIN_DSI1 18
  348. +#define RPI_POWER_DOMAIN_TRANSPOSER 19
  349. +#define RPI_POWER_DOMAIN_CCP2TX 20
  350. +#define RPI_POWER_DOMAIN_CDP 21
  351. +#define RPI_POWER_DOMAIN_ARM 22
  352. +
  353. +#define RPI_POWER_DOMAIN_COUNT 23
  354. +
  355. +#endif /* _DT_BINDINGS_ARM_BCM2835_RPI_POWER_H */