meson-gx-pwrc-vpu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (c) 2017 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <linux/of_address.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm_domain.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/regmap.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/of_device.h>
  14. #include <linux/reset.h>
  15. #include <linux/clk.h>
  16. /* AO Offsets */
  17. #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
  18. #define GEN_PWR_VPU_HDMI BIT(8)
  19. #define GEN_PWR_VPU_HDMI_ISO BIT(9)
  20. /* HHI Offsets */
  21. #define HHI_MEM_PD_REG0 (0x40 << 2)
  22. #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
  23. #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
  24. #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
  25. struct meson_gx_pwrc_vpu {
  26. struct generic_pm_domain genpd;
  27. struct regmap *regmap_ao;
  28. struct regmap *regmap_hhi;
  29. struct reset_control *rstc;
  30. struct clk *vpu_clk;
  31. struct clk *vapb_clk;
  32. };
  33. static inline
  34. struct meson_gx_pwrc_vpu *genpd_to_pd(struct generic_pm_domain *d)
  35. {
  36. return container_of(d, struct meson_gx_pwrc_vpu, genpd);
  37. }
  38. static int meson_gx_pwrc_vpu_power_off(struct generic_pm_domain *genpd)
  39. {
  40. struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
  41. int i;
  42. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  43. GEN_PWR_VPU_HDMI_ISO, GEN_PWR_VPU_HDMI_ISO);
  44. udelay(20);
  45. /* Power Down Memories */
  46. for (i = 0; i < 32; i += 2) {
  47. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
  48. 0x3 << i, 0x3 << i);
  49. udelay(5);
  50. }
  51. for (i = 0; i < 32; i += 2) {
  52. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
  53. 0x3 << i, 0x3 << i);
  54. udelay(5);
  55. }
  56. for (i = 8; i < 16; i++) {
  57. regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
  58. BIT(i), BIT(i));
  59. udelay(5);
  60. }
  61. udelay(20);
  62. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  63. GEN_PWR_VPU_HDMI, GEN_PWR_VPU_HDMI);
  64. msleep(20);
  65. clk_disable_unprepare(pd->vpu_clk);
  66. clk_disable_unprepare(pd->vapb_clk);
  67. return 0;
  68. }
  69. static int meson_g12a_pwrc_vpu_power_off(struct generic_pm_domain *genpd)
  70. {
  71. struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
  72. int i;
  73. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  74. GEN_PWR_VPU_HDMI_ISO, GEN_PWR_VPU_HDMI_ISO);
  75. udelay(20);
  76. /* Power Down Memories */
  77. for (i = 0; i < 32; i += 2) {
  78. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
  79. 0x3 << i, 0x3 << i);
  80. udelay(5);
  81. }
  82. for (i = 0; i < 32; i += 2) {
  83. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
  84. 0x3 << i, 0x3 << i);
  85. udelay(5);
  86. }
  87. for (i = 0; i < 32; i += 2) {
  88. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG2,
  89. 0x3 << i, 0x3 << i);
  90. udelay(5);
  91. }
  92. for (i = 8; i < 16; i++) {
  93. regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
  94. BIT(i), BIT(i));
  95. udelay(5);
  96. }
  97. udelay(20);
  98. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  99. GEN_PWR_VPU_HDMI, GEN_PWR_VPU_HDMI);
  100. msleep(20);
  101. clk_disable_unprepare(pd->vpu_clk);
  102. clk_disable_unprepare(pd->vapb_clk);
  103. return 0;
  104. }
  105. static int meson_gx_pwrc_vpu_setup_clk(struct meson_gx_pwrc_vpu *pd)
  106. {
  107. int ret;
  108. ret = clk_prepare_enable(pd->vpu_clk);
  109. if (ret)
  110. return ret;
  111. ret = clk_prepare_enable(pd->vapb_clk);
  112. if (ret)
  113. clk_disable_unprepare(pd->vpu_clk);
  114. return ret;
  115. }
  116. static int meson_gx_pwrc_vpu_power_on(struct generic_pm_domain *genpd)
  117. {
  118. struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
  119. int ret;
  120. int i;
  121. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  122. GEN_PWR_VPU_HDMI, 0);
  123. udelay(20);
  124. /* Power Up Memories */
  125. for (i = 0; i < 32; i += 2) {
  126. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
  127. 0x3 << i, 0);
  128. udelay(5);
  129. }
  130. for (i = 0; i < 32; i += 2) {
  131. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
  132. 0x3 << i, 0);
  133. udelay(5);
  134. }
  135. for (i = 8; i < 16; i++) {
  136. regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
  137. BIT(i), 0);
  138. udelay(5);
  139. }
  140. udelay(20);
  141. ret = reset_control_assert(pd->rstc);
  142. if (ret)
  143. return ret;
  144. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  145. GEN_PWR_VPU_HDMI_ISO, 0);
  146. ret = reset_control_deassert(pd->rstc);
  147. if (ret)
  148. return ret;
  149. ret = meson_gx_pwrc_vpu_setup_clk(pd);
  150. if (ret)
  151. return ret;
  152. return 0;
  153. }
  154. static int meson_g12a_pwrc_vpu_power_on(struct generic_pm_domain *genpd)
  155. {
  156. struct meson_gx_pwrc_vpu *pd = genpd_to_pd(genpd);
  157. int ret;
  158. int i;
  159. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  160. GEN_PWR_VPU_HDMI, 0);
  161. udelay(20);
  162. /* Power Up Memories */
  163. for (i = 0; i < 32; i += 2) {
  164. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG0,
  165. 0x3 << i, 0);
  166. udelay(5);
  167. }
  168. for (i = 0; i < 32; i += 2) {
  169. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG1,
  170. 0x3 << i, 0);
  171. udelay(5);
  172. }
  173. for (i = 0; i < 32; i += 2) {
  174. regmap_update_bits(pd->regmap_hhi, HHI_VPU_MEM_PD_REG2,
  175. 0x3 << i, 0);
  176. udelay(5);
  177. }
  178. for (i = 8; i < 16; i++) {
  179. regmap_update_bits(pd->regmap_hhi, HHI_MEM_PD_REG0,
  180. BIT(i), 0);
  181. udelay(5);
  182. }
  183. udelay(20);
  184. ret = reset_control_assert(pd->rstc);
  185. if (ret)
  186. return ret;
  187. regmap_update_bits(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0,
  188. GEN_PWR_VPU_HDMI_ISO, 0);
  189. ret = reset_control_deassert(pd->rstc);
  190. if (ret)
  191. return ret;
  192. ret = meson_gx_pwrc_vpu_setup_clk(pd);
  193. if (ret)
  194. return ret;
  195. return 0;
  196. }
  197. static bool meson_gx_pwrc_vpu_get_power(struct meson_gx_pwrc_vpu *pd)
  198. {
  199. u32 reg;
  200. regmap_read(pd->regmap_ao, AO_RTI_GEN_PWR_SLEEP0, &reg);
  201. return (reg & GEN_PWR_VPU_HDMI);
  202. }
  203. static struct meson_gx_pwrc_vpu vpu_hdmi_pd = {
  204. .genpd = {
  205. .name = "vpu_hdmi",
  206. .power_off = meson_gx_pwrc_vpu_power_off,
  207. .power_on = meson_gx_pwrc_vpu_power_on,
  208. },
  209. };
  210. static struct meson_gx_pwrc_vpu vpu_hdmi_pd_g12a = {
  211. .genpd = {
  212. .name = "vpu_hdmi",
  213. .power_off = meson_g12a_pwrc_vpu_power_off,
  214. .power_on = meson_g12a_pwrc_vpu_power_on,
  215. },
  216. };
  217. static int meson_gx_pwrc_vpu_probe(struct platform_device *pdev)
  218. {
  219. const struct meson_gx_pwrc_vpu *vpu_pd_match;
  220. struct regmap *regmap_ao, *regmap_hhi;
  221. struct meson_gx_pwrc_vpu *vpu_pd;
  222. struct reset_control *rstc;
  223. struct clk *vpu_clk;
  224. struct clk *vapb_clk;
  225. bool powered_off;
  226. int ret;
  227. vpu_pd_match = of_device_get_match_data(&pdev->dev);
  228. if (!vpu_pd_match) {
  229. dev_err(&pdev->dev, "failed to get match data\n");
  230. return -ENODEV;
  231. }
  232. vpu_pd = devm_kzalloc(&pdev->dev, sizeof(*vpu_pd), GFP_KERNEL);
  233. if (!vpu_pd)
  234. return -ENOMEM;
  235. memcpy(vpu_pd, vpu_pd_match, sizeof(*vpu_pd));
  236. regmap_ao = syscon_node_to_regmap(of_get_parent(pdev->dev.of_node));
  237. if (IS_ERR(regmap_ao)) {
  238. dev_err(&pdev->dev, "failed to get regmap\n");
  239. return PTR_ERR(regmap_ao);
  240. }
  241. regmap_hhi = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  242. "amlogic,hhi-sysctrl");
  243. if (IS_ERR(regmap_hhi)) {
  244. dev_err(&pdev->dev, "failed to get HHI regmap\n");
  245. return PTR_ERR(regmap_hhi);
  246. }
  247. rstc = devm_reset_control_array_get(&pdev->dev, false, false);
  248. if (IS_ERR(rstc)) {
  249. if (PTR_ERR(rstc) != -EPROBE_DEFER)
  250. dev_err(&pdev->dev, "failed to get reset lines\n");
  251. return PTR_ERR(rstc);
  252. }
  253. vpu_clk = devm_clk_get(&pdev->dev, "vpu");
  254. if (IS_ERR(vpu_clk)) {
  255. dev_err(&pdev->dev, "vpu clock request failed\n");
  256. return PTR_ERR(vpu_clk);
  257. }
  258. vapb_clk = devm_clk_get(&pdev->dev, "vapb");
  259. if (IS_ERR(vapb_clk)) {
  260. dev_err(&pdev->dev, "vapb clock request failed\n");
  261. return PTR_ERR(vapb_clk);
  262. }
  263. vpu_pd->regmap_ao = regmap_ao;
  264. vpu_pd->regmap_hhi = regmap_hhi;
  265. vpu_pd->rstc = rstc;
  266. vpu_pd->vpu_clk = vpu_clk;
  267. vpu_pd->vapb_clk = vapb_clk;
  268. platform_set_drvdata(pdev, vpu_pd);
  269. powered_off = meson_gx_pwrc_vpu_get_power(vpu_pd);
  270. /* If already powered, sync the clock states */
  271. if (!powered_off) {
  272. ret = meson_gx_pwrc_vpu_setup_clk(vpu_pd);
  273. if (ret)
  274. return ret;
  275. }
  276. pm_genpd_init(&vpu_pd->genpd, &pm_domain_always_on_gov,
  277. powered_off);
  278. return of_genpd_add_provider_simple(pdev->dev.of_node,
  279. &vpu_pd->genpd);
  280. }
  281. static void meson_gx_pwrc_vpu_shutdown(struct platform_device *pdev)
  282. {
  283. struct meson_gx_pwrc_vpu *vpu_pd = platform_get_drvdata(pdev);
  284. bool powered_off;
  285. powered_off = meson_gx_pwrc_vpu_get_power(vpu_pd);
  286. if (!powered_off)
  287. vpu_pd->genpd.power_off(&vpu_pd->genpd);
  288. }
  289. static const struct of_device_id meson_gx_pwrc_vpu_match_table[] = {
  290. { .compatible = "amlogic,meson-gx-pwrc-vpu", .data = &vpu_hdmi_pd },
  291. {
  292. .compatible = "amlogic,meson-g12a-pwrc-vpu",
  293. .data = &vpu_hdmi_pd_g12a
  294. },
  295. { /* sentinel */ }
  296. };
  297. static struct platform_driver meson_gx_pwrc_vpu_driver = {
  298. .probe = meson_gx_pwrc_vpu_probe,
  299. .shutdown = meson_gx_pwrc_vpu_shutdown,
  300. .driver = {
  301. .name = "meson_gx_pwrc_vpu",
  302. .of_match_table = meson_gx_pwrc_vpu_match_table,
  303. },
  304. };
  305. builtin_platform_driver(meson_gx_pwrc_vpu_driver);