apple-pmgr-pwrstate.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Apple SoC PMGR device power state driver
  4. *
  5. * Copyright The Asahi Linux Contributors
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/err.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/regmap.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/reset-controller.h>
  17. #include <linux/module.h>
  18. #define APPLE_PMGR_RESET BIT(31)
  19. #define APPLE_PMGR_AUTO_ENABLE BIT(28)
  20. #define APPLE_PMGR_PS_AUTO GENMASK(27, 24)
  21. #define APPLE_PMGR_PS_MIN GENMASK(19, 16)
  22. #define APPLE_PMGR_PARENT_OFF BIT(11)
  23. #define APPLE_PMGR_DEV_DISABLE BIT(10)
  24. #define APPLE_PMGR_WAS_CLKGATED BIT(9)
  25. #define APPLE_PMGR_WAS_PWRGATED BIT(8)
  26. #define APPLE_PMGR_PS_ACTUAL GENMASK(7, 4)
  27. #define APPLE_PMGR_PS_TARGET GENMASK(3, 0)
  28. #define APPLE_PMGR_FLAGS (APPLE_PMGR_WAS_CLKGATED | APPLE_PMGR_WAS_PWRGATED)
  29. #define APPLE_PMGR_PS_ACTIVE 0xf
  30. #define APPLE_PMGR_PS_CLKGATE 0x4
  31. #define APPLE_PMGR_PS_PWRGATE 0x0
  32. #define APPLE_PMGR_PS_SET_TIMEOUT 100
  33. #define APPLE_PMGR_RESET_TIME 1
  34. struct apple_pmgr_ps {
  35. struct device *dev;
  36. struct generic_pm_domain genpd;
  37. struct reset_controller_dev rcdev;
  38. struct regmap *regmap;
  39. u32 offset;
  40. u32 min_state;
  41. };
  42. #define genpd_to_apple_pmgr_ps(_genpd) container_of(_genpd, struct apple_pmgr_ps, genpd)
  43. #define rcdev_to_apple_pmgr_ps(_rcdev) container_of(_rcdev, struct apple_pmgr_ps, rcdev)
  44. static int apple_pmgr_ps_set(struct generic_pm_domain *genpd, u32 pstate, bool auto_enable)
  45. {
  46. int ret;
  47. struct apple_pmgr_ps *ps = genpd_to_apple_pmgr_ps(genpd);
  48. u32 reg;
  49. ret = regmap_read(ps->regmap, ps->offset, &reg);
  50. if (ret < 0)
  51. return ret;
  52. /* Resets are synchronous, and only work if the device is powered and clocked. */
  53. if (reg & APPLE_PMGR_RESET && pstate != APPLE_PMGR_PS_ACTIVE)
  54. dev_err(ps->dev, "PS %s: powering off with RESET active\n",
  55. genpd->name);
  56. reg &= ~(APPLE_PMGR_AUTO_ENABLE | APPLE_PMGR_FLAGS | APPLE_PMGR_PS_TARGET);
  57. reg |= FIELD_PREP(APPLE_PMGR_PS_TARGET, pstate);
  58. dev_dbg(ps->dev, "PS %s: pwrstate = 0x%x: 0x%x\n", genpd->name, pstate, reg);
  59. regmap_write(ps->regmap, ps->offset, reg);
  60. ret = regmap_read_poll_timeout_atomic(
  61. ps->regmap, ps->offset, reg,
  62. (FIELD_GET(APPLE_PMGR_PS_ACTUAL, reg) == pstate), 1,
  63. APPLE_PMGR_PS_SET_TIMEOUT);
  64. if (ret < 0)
  65. dev_err(ps->dev, "PS %s: Failed to reach power state 0x%x (now: 0x%x)\n",
  66. genpd->name, pstate, reg);
  67. if (auto_enable) {
  68. /* Not all devices implement this; this is a no-op where not implemented. */
  69. reg &= ~APPLE_PMGR_FLAGS;
  70. reg |= APPLE_PMGR_AUTO_ENABLE;
  71. regmap_write(ps->regmap, ps->offset, reg);
  72. }
  73. return ret;
  74. }
  75. static bool apple_pmgr_ps_is_active(struct apple_pmgr_ps *ps)
  76. {
  77. u32 reg = 0;
  78. regmap_read(ps->regmap, ps->offset, &reg);
  79. /*
  80. * We consider domains as active if they are actually on, or if they have auto-PM
  81. * enabled and the intended target is on.
  82. */
  83. return (FIELD_GET(APPLE_PMGR_PS_ACTUAL, reg) == APPLE_PMGR_PS_ACTIVE ||
  84. (FIELD_GET(APPLE_PMGR_PS_TARGET, reg) == APPLE_PMGR_PS_ACTIVE &&
  85. reg & APPLE_PMGR_AUTO_ENABLE));
  86. }
  87. static int apple_pmgr_ps_power_on(struct generic_pm_domain *genpd)
  88. {
  89. return apple_pmgr_ps_set(genpd, APPLE_PMGR_PS_ACTIVE, true);
  90. }
  91. static int apple_pmgr_ps_power_off(struct generic_pm_domain *genpd)
  92. {
  93. return apple_pmgr_ps_set(genpd, APPLE_PMGR_PS_PWRGATE, false);
  94. }
  95. static int apple_pmgr_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
  96. {
  97. struct apple_pmgr_ps *ps = rcdev_to_apple_pmgr_ps(rcdev);
  98. mutex_lock(&ps->genpd.mlock);
  99. if (ps->genpd.status == GENPD_STATE_OFF)
  100. dev_err(ps->dev, "PS 0x%x: asserting RESET while powered down\n", ps->offset);
  101. dev_dbg(ps->dev, "PS 0x%x: assert reset\n", ps->offset);
  102. /* Quiesce device before asserting reset */
  103. regmap_update_bits(ps->regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_DEV_DISABLE,
  104. APPLE_PMGR_DEV_DISABLE);
  105. regmap_update_bits(ps->regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_RESET,
  106. APPLE_PMGR_RESET);
  107. mutex_unlock(&ps->genpd.mlock);
  108. return 0;
  109. }
  110. static int apple_pmgr_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  111. {
  112. struct apple_pmgr_ps *ps = rcdev_to_apple_pmgr_ps(rcdev);
  113. mutex_lock(&ps->genpd.mlock);
  114. dev_dbg(ps->dev, "PS 0x%x: deassert reset\n", ps->offset);
  115. regmap_update_bits(ps->regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_RESET, 0);
  116. regmap_update_bits(ps->regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_DEV_DISABLE, 0);
  117. if (ps->genpd.status == GENPD_STATE_OFF)
  118. dev_err(ps->dev, "PS 0x%x: RESET was deasserted while powered down\n", ps->offset);
  119. mutex_unlock(&ps->genpd.mlock);
  120. return 0;
  121. }
  122. static int apple_pmgr_reset_reset(struct reset_controller_dev *rcdev, unsigned long id)
  123. {
  124. int ret;
  125. ret = apple_pmgr_reset_assert(rcdev, id);
  126. if (ret)
  127. return ret;
  128. usleep_range(APPLE_PMGR_RESET_TIME, 2 * APPLE_PMGR_RESET_TIME);
  129. return apple_pmgr_reset_deassert(rcdev, id);
  130. }
  131. static int apple_pmgr_reset_status(struct reset_controller_dev *rcdev, unsigned long id)
  132. {
  133. struct apple_pmgr_ps *ps = rcdev_to_apple_pmgr_ps(rcdev);
  134. u32 reg = 0;
  135. regmap_read(ps->regmap, ps->offset, &reg);
  136. return !!(reg & APPLE_PMGR_RESET);
  137. }
  138. const struct reset_control_ops apple_pmgr_reset_ops = {
  139. .assert = apple_pmgr_reset_assert,
  140. .deassert = apple_pmgr_reset_deassert,
  141. .reset = apple_pmgr_reset_reset,
  142. .status = apple_pmgr_reset_status,
  143. };
  144. static int apple_pmgr_reset_xlate(struct reset_controller_dev *rcdev,
  145. const struct of_phandle_args *reset_spec)
  146. {
  147. return 0;
  148. }
  149. static int apple_pmgr_ps_probe(struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. struct device_node *node = dev->of_node;
  153. struct apple_pmgr_ps *ps;
  154. struct regmap *regmap;
  155. struct of_phandle_iterator it;
  156. int ret;
  157. const char *name;
  158. bool active;
  159. regmap = syscon_node_to_regmap(node->parent);
  160. if (IS_ERR(regmap))
  161. return PTR_ERR(regmap);
  162. ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
  163. if (!ps)
  164. return -ENOMEM;
  165. ps->dev = dev;
  166. ps->regmap = regmap;
  167. ret = of_property_read_string(node, "label", &name);
  168. if (ret < 0) {
  169. dev_err(dev, "missing label property\n");
  170. return ret;
  171. }
  172. ret = of_property_read_u32(node, "reg", &ps->offset);
  173. if (ret < 0) {
  174. dev_err(dev, "missing reg property\n");
  175. return ret;
  176. }
  177. ps->genpd.name = name;
  178. ps->genpd.power_on = apple_pmgr_ps_power_on;
  179. ps->genpd.power_off = apple_pmgr_ps_power_off;
  180. ret = of_property_read_u32(node, "apple,min-state", &ps->min_state);
  181. if (ret == 0 && ps->min_state <= APPLE_PMGR_PS_ACTIVE)
  182. regmap_update_bits(regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_PS_MIN,
  183. FIELD_PREP(APPLE_PMGR_PS_MIN, ps->min_state));
  184. active = apple_pmgr_ps_is_active(ps);
  185. if (of_property_read_bool(node, "apple,always-on")) {
  186. ps->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
  187. if (!active) {
  188. dev_warn(dev, "always-on domain %s is not on at boot\n", name);
  189. /* Turn it on so pm_genpd_init does not fail */
  190. active = apple_pmgr_ps_power_on(&ps->genpd) == 0;
  191. }
  192. }
  193. /* Turn on auto-PM if the domain is already on */
  194. if (active)
  195. regmap_update_bits(regmap, ps->offset, APPLE_PMGR_FLAGS | APPLE_PMGR_AUTO_ENABLE,
  196. APPLE_PMGR_AUTO_ENABLE);
  197. ret = pm_genpd_init(&ps->genpd, NULL, !active);
  198. if (ret < 0) {
  199. dev_err(dev, "pm_genpd_init failed\n");
  200. return ret;
  201. }
  202. ret = of_genpd_add_provider_simple(node, &ps->genpd);
  203. if (ret < 0) {
  204. dev_err(dev, "of_genpd_add_provider_simple failed\n");
  205. return ret;
  206. }
  207. of_for_each_phandle(&it, ret, node, "power-domains", "#power-domain-cells", -1) {
  208. struct of_phandle_args parent, child;
  209. parent.np = it.node;
  210. parent.args_count = of_phandle_iterator_args(&it, parent.args, MAX_PHANDLE_ARGS);
  211. child.np = node;
  212. child.args_count = 0;
  213. ret = of_genpd_add_subdomain(&parent, &child);
  214. if (ret == -EPROBE_DEFER) {
  215. of_node_put(parent.np);
  216. goto err_remove;
  217. } else if (ret < 0) {
  218. dev_err(dev, "failed to add to parent domain: %d (%s -> %s)\n",
  219. ret, it.node->name, node->name);
  220. of_node_put(parent.np);
  221. goto err_remove;
  222. }
  223. }
  224. /*
  225. * Do not participate in regular PM; parent power domains are handled via the
  226. * genpd hierarchy.
  227. */
  228. pm_genpd_remove_device(dev);
  229. ps->rcdev.owner = THIS_MODULE;
  230. ps->rcdev.nr_resets = 1;
  231. ps->rcdev.ops = &apple_pmgr_reset_ops;
  232. ps->rcdev.of_node = dev->of_node;
  233. ps->rcdev.of_reset_n_cells = 0;
  234. ps->rcdev.of_xlate = apple_pmgr_reset_xlate;
  235. ret = devm_reset_controller_register(dev, &ps->rcdev);
  236. if (ret < 0)
  237. goto err_remove;
  238. return 0;
  239. err_remove:
  240. of_genpd_del_provider(node);
  241. pm_genpd_remove(&ps->genpd);
  242. return ret;
  243. }
  244. static const struct of_device_id apple_pmgr_ps_of_match[] = {
  245. { .compatible = "apple,pmgr-pwrstate" },
  246. {}
  247. };
  248. MODULE_DEVICE_TABLE(of, apple_pmgr_ps_of_match);
  249. static struct platform_driver apple_pmgr_ps_driver = {
  250. .probe = apple_pmgr_ps_probe,
  251. .driver = {
  252. .name = "apple-pmgr-pwrstate",
  253. .of_match_table = apple_pmgr_ps_of_match,
  254. },
  255. };
  256. MODULE_AUTHOR("Hector Martin <[email protected]>");
  257. MODULE_DESCRIPTION("PMGR power state driver for Apple SoCs");
  258. MODULE_LICENSE("GPL v2");
  259. module_platform_driver(apple_pmgr_ps_driver);