meson-ee-pwrc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019 BayLibre, SAS
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. */
  6. #include <linux/platform_device.h>
  7. #include <linux/pm_domain.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/regmap.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/of.h>
  12. #include <linux/reset-controller.h>
  13. #include <linux/reset.h>
  14. #include <linux/clk.h>
  15. #include <linux/module.h>
  16. #include <dt-bindings/power/meson8-power.h>
  17. #include <dt-bindings/power/meson-axg-power.h>
  18. #include <dt-bindings/power/meson-g12a-power.h>
  19. #include <dt-bindings/power/meson-gxbb-power.h>
  20. #include <dt-bindings/power/meson-sm1-power.h>
  21. /* AO Offsets */
  22. #define GX_AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
  23. #define GX_AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
  24. /*
  25. * Meson8/Meson8b/Meson8m2 only expose the power management registers of the
  26. * AO-bus as syscon. 0x3a from GX translates to 0x02, 0x3b translates to 0x03
  27. * and so on.
  28. */
  29. #define MESON8_AO_RTI_GEN_PWR_SLEEP0 (0x02 << 2)
  30. #define MESON8_AO_RTI_GEN_PWR_ISO0 (0x03 << 2)
  31. /* HHI Offsets */
  32. #define HHI_MEM_PD_REG0 (0x40 << 2)
  33. #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
  34. #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
  35. #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
  36. #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
  37. #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
  38. #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
  39. #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
  40. #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
  41. #define G12A_HHI_NANOQ_MEM_PD_REG0 (0x43 << 2)
  42. #define G12A_HHI_NANOQ_MEM_PD_REG1 (0x44 << 2)
  43. #define G12A_HHI_ISP_MEM_PD_REG0 (0x45 << 2)
  44. #define G12A_HHI_ISP_MEM_PD_REG1 (0x46 << 2)
  45. struct meson_ee_pwrc;
  46. struct meson_ee_pwrc_domain;
  47. struct meson_ee_pwrc_mem_domain {
  48. unsigned int reg;
  49. unsigned int mask;
  50. };
  51. struct meson_ee_pwrc_top_domain {
  52. unsigned int sleep_reg;
  53. unsigned int sleep_mask;
  54. unsigned int iso_reg;
  55. unsigned int iso_mask;
  56. };
  57. struct meson_ee_pwrc_domain_desc {
  58. char *name;
  59. unsigned int reset_names_count;
  60. unsigned int clk_names_count;
  61. struct meson_ee_pwrc_top_domain *top_pd;
  62. unsigned int mem_pd_count;
  63. struct meson_ee_pwrc_mem_domain *mem_pd;
  64. bool (*is_powered_off)(struct meson_ee_pwrc_domain *pwrc_domain);
  65. };
  66. struct meson_ee_pwrc_domain_data {
  67. unsigned int count;
  68. struct meson_ee_pwrc_domain_desc *domains;
  69. };
  70. /* TOP Power Domains */
  71. static struct meson_ee_pwrc_top_domain gx_pwrc_vpu = {
  72. .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
  73. .sleep_mask = BIT(8),
  74. .iso_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
  75. .iso_mask = BIT(9),
  76. };
  77. static struct meson_ee_pwrc_top_domain meson8_pwrc_vpu = {
  78. .sleep_reg = MESON8_AO_RTI_GEN_PWR_SLEEP0,
  79. .sleep_mask = BIT(8),
  80. .iso_reg = MESON8_AO_RTI_GEN_PWR_SLEEP0,
  81. .iso_mask = BIT(9),
  82. };
  83. #define SM1_EE_PD(__bit) \
  84. { \
  85. .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0, \
  86. .sleep_mask = BIT(__bit), \
  87. .iso_reg = GX_AO_RTI_GEN_PWR_ISO0, \
  88. .iso_mask = BIT(__bit), \
  89. }
  90. static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
  91. static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
  92. static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
  93. static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
  94. static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
  95. static struct meson_ee_pwrc_top_domain g12a_pwrc_nna = {
  96. .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
  97. .sleep_mask = BIT(16) | BIT(17),
  98. .iso_reg = GX_AO_RTI_GEN_PWR_ISO0,
  99. .iso_mask = BIT(16) | BIT(17),
  100. };
  101. static struct meson_ee_pwrc_top_domain g12a_pwrc_isp = {
  102. .sleep_reg = GX_AO_RTI_GEN_PWR_SLEEP0,
  103. .sleep_mask = BIT(18) | BIT(19),
  104. .iso_reg = GX_AO_RTI_GEN_PWR_ISO0,
  105. .iso_mask = BIT(18) | BIT(19),
  106. };
  107. /* Memory PD Domains */
  108. #define VPU_MEMPD(__reg) \
  109. { __reg, GENMASK(1, 0) }, \
  110. { __reg, GENMASK(3, 2) }, \
  111. { __reg, GENMASK(5, 4) }, \
  112. { __reg, GENMASK(7, 6) }, \
  113. { __reg, GENMASK(9, 8) }, \
  114. { __reg, GENMASK(11, 10) }, \
  115. { __reg, GENMASK(13, 12) }, \
  116. { __reg, GENMASK(15, 14) }, \
  117. { __reg, GENMASK(17, 16) }, \
  118. { __reg, GENMASK(19, 18) }, \
  119. { __reg, GENMASK(21, 20) }, \
  120. { __reg, GENMASK(23, 22) }, \
  121. { __reg, GENMASK(25, 24) }, \
  122. { __reg, GENMASK(27, 26) }, \
  123. { __reg, GENMASK(29, 28) }, \
  124. { __reg, GENMASK(31, 30) }
  125. #define VPU_HHI_MEMPD(__reg) \
  126. { __reg, BIT(8) }, \
  127. { __reg, BIT(9) }, \
  128. { __reg, BIT(10) }, \
  129. { __reg, BIT(11) }, \
  130. { __reg, BIT(12) }, \
  131. { __reg, BIT(13) }, \
  132. { __reg, BIT(14) }, \
  133. { __reg, BIT(15) }
  134. static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_vpu[] = {
  135. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  136. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  137. };
  138. static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
  139. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  140. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  141. VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
  142. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  143. };
  144. static struct meson_ee_pwrc_mem_domain gxbb_pwrc_mem_vpu[] = {
  145. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  146. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  147. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  148. };
  149. static struct meson_ee_pwrc_mem_domain meson_pwrc_mem_eth[] = {
  150. { HHI_MEM_PD_REG0, GENMASK(3, 2) },
  151. };
  152. static struct meson_ee_pwrc_mem_domain meson8_pwrc_audio_dsp_mem[] = {
  153. { HHI_MEM_PD_REG0, GENMASK(1, 0) },
  154. };
  155. static struct meson_ee_pwrc_mem_domain meson8_pwrc_mem_vpu[] = {
  156. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  157. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  158. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  159. };
  160. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
  161. VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
  162. VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
  163. VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
  164. VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
  165. { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
  166. { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
  167. { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
  168. { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
  169. VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
  170. };
  171. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
  172. { HHI_NANOQ_MEM_PD_REG0, 0xff },
  173. { HHI_NANOQ_MEM_PD_REG1, 0xff },
  174. };
  175. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
  176. { HHI_MEM_PD_REG0, GENMASK(31, 30) },
  177. };
  178. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
  179. { HHI_MEM_PD_REG0, GENMASK(29, 26) },
  180. };
  181. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
  182. { HHI_MEM_PD_REG0, GENMASK(25, 18) },
  183. };
  184. static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_audio[] = {
  185. { HHI_MEM_PD_REG0, GENMASK(5, 4) },
  186. };
  187. static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
  188. { HHI_MEM_PD_REG0, GENMASK(5, 4) },
  189. { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
  190. { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
  191. { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
  192. { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
  193. { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
  194. { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
  195. { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
  196. { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
  197. { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
  198. { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
  199. { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
  200. { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
  201. };
  202. static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_nna[] = {
  203. { G12A_HHI_NANOQ_MEM_PD_REG0, GENMASK(31, 0) },
  204. { G12A_HHI_NANOQ_MEM_PD_REG1, GENMASK(31, 0) },
  205. };
  206. static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_isp[] = {
  207. { G12A_HHI_ISP_MEM_PD_REG0, GENMASK(31, 0) },
  208. { G12A_HHI_ISP_MEM_PD_REG1, GENMASK(31, 0) },
  209. };
  210. #define VPU_PD(__name, __top_pd, __mem, __is_pwr_off, __resets, __clks) \
  211. { \
  212. .name = __name, \
  213. .reset_names_count = __resets, \
  214. .clk_names_count = __clks, \
  215. .top_pd = __top_pd, \
  216. .mem_pd_count = ARRAY_SIZE(__mem), \
  217. .mem_pd = __mem, \
  218. .is_powered_off = __is_pwr_off, \
  219. }
  220. #define TOP_PD(__name, __top_pd, __mem, __is_pwr_off) \
  221. { \
  222. .name = __name, \
  223. .top_pd = __top_pd, \
  224. .mem_pd_count = ARRAY_SIZE(__mem), \
  225. .mem_pd = __mem, \
  226. .is_powered_off = __is_pwr_off, \
  227. }
  228. #define MEM_PD(__name, __mem) \
  229. TOP_PD(__name, NULL, __mem, NULL)
  230. static bool pwrc_ee_is_powered_off(struct meson_ee_pwrc_domain *pwrc_domain);
  231. static struct meson_ee_pwrc_domain_desc axg_pwrc_domains[] = {
  232. [PWRC_AXG_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, axg_pwrc_mem_vpu,
  233. pwrc_ee_is_powered_off, 5, 2),
  234. [PWRC_AXG_ETHERNET_MEM_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
  235. [PWRC_AXG_AUDIO_ID] = MEM_PD("AUDIO", axg_pwrc_mem_audio),
  236. };
  237. static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
  238. [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, g12a_pwrc_mem_vpu,
  239. pwrc_ee_is_powered_off, 11, 2),
  240. [PWRC_G12A_ETH_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
  241. [PWRC_G12A_NNA_ID] = TOP_PD("NNA", &g12a_pwrc_nna, g12a_pwrc_mem_nna,
  242. pwrc_ee_is_powered_off),
  243. [PWRC_G12A_ISP_ID] = TOP_PD("ISP", &g12a_pwrc_isp, g12a_pwrc_mem_isp,
  244. pwrc_ee_is_powered_off),
  245. };
  246. static struct meson_ee_pwrc_domain_desc gxbb_pwrc_domains[] = {
  247. [PWRC_GXBB_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, gxbb_pwrc_mem_vpu,
  248. pwrc_ee_is_powered_off, 12, 2),
  249. [PWRC_GXBB_ETHERNET_MEM_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
  250. };
  251. static struct meson_ee_pwrc_domain_desc meson8_pwrc_domains[] = {
  252. [PWRC_MESON8_VPU_ID] = VPU_PD("VPU", &meson8_pwrc_vpu,
  253. meson8_pwrc_mem_vpu,
  254. pwrc_ee_is_powered_off, 0, 1),
  255. [PWRC_MESON8_ETHERNET_MEM_ID] = MEM_PD("ETHERNET_MEM",
  256. meson_pwrc_mem_eth),
  257. [PWRC_MESON8_AUDIO_DSP_MEM_ID] = MEM_PD("AUDIO_DSP_MEM",
  258. meson8_pwrc_audio_dsp_mem),
  259. };
  260. static struct meson_ee_pwrc_domain_desc meson8b_pwrc_domains[] = {
  261. [PWRC_MESON8_VPU_ID] = VPU_PD("VPU", &meson8_pwrc_vpu,
  262. meson8_pwrc_mem_vpu,
  263. pwrc_ee_is_powered_off, 11, 1),
  264. [PWRC_MESON8_ETHERNET_MEM_ID] = MEM_PD("ETHERNET_MEM",
  265. meson_pwrc_mem_eth),
  266. [PWRC_MESON8_AUDIO_DSP_MEM_ID] = MEM_PD("AUDIO_DSP_MEM",
  267. meson8_pwrc_audio_dsp_mem),
  268. };
  269. static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
  270. [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
  271. pwrc_ee_is_powered_off, 11, 2),
  272. [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
  273. pwrc_ee_is_powered_off),
  274. [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
  275. pwrc_ee_is_powered_off),
  276. [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
  277. pwrc_ee_is_powered_off),
  278. [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
  279. pwrc_ee_is_powered_off),
  280. [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
  281. [PWRC_SM1_ETH_ID] = MEM_PD("ETH", meson_pwrc_mem_eth),
  282. };
  283. struct meson_ee_pwrc_domain {
  284. struct generic_pm_domain base;
  285. bool enabled;
  286. struct meson_ee_pwrc *pwrc;
  287. struct meson_ee_pwrc_domain_desc desc;
  288. struct clk_bulk_data *clks;
  289. int num_clks;
  290. struct reset_control *rstc;
  291. int num_rstc;
  292. };
  293. struct meson_ee_pwrc {
  294. struct regmap *regmap_ao;
  295. struct regmap *regmap_hhi;
  296. struct meson_ee_pwrc_domain *domains;
  297. struct genpd_onecell_data xlate;
  298. };
  299. static bool pwrc_ee_is_powered_off(struct meson_ee_pwrc_domain *pwrc_domain)
  300. {
  301. u32 reg;
  302. regmap_read(pwrc_domain->pwrc->regmap_ao,
  303. pwrc_domain->desc.top_pd->sleep_reg, &reg);
  304. return (reg & pwrc_domain->desc.top_pd->sleep_mask);
  305. }
  306. static int meson_ee_pwrc_off(struct generic_pm_domain *domain)
  307. {
  308. struct meson_ee_pwrc_domain *pwrc_domain =
  309. container_of(domain, struct meson_ee_pwrc_domain, base);
  310. int i;
  311. if (pwrc_domain->desc.top_pd)
  312. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  313. pwrc_domain->desc.top_pd->sleep_reg,
  314. pwrc_domain->desc.top_pd->sleep_mask,
  315. pwrc_domain->desc.top_pd->sleep_mask);
  316. udelay(20);
  317. for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
  318. regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
  319. pwrc_domain->desc.mem_pd[i].reg,
  320. pwrc_domain->desc.mem_pd[i].mask,
  321. pwrc_domain->desc.mem_pd[i].mask);
  322. udelay(20);
  323. if (pwrc_domain->desc.top_pd)
  324. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  325. pwrc_domain->desc.top_pd->iso_reg,
  326. pwrc_domain->desc.top_pd->iso_mask,
  327. pwrc_domain->desc.top_pd->iso_mask);
  328. if (pwrc_domain->num_clks) {
  329. msleep(20);
  330. clk_bulk_disable_unprepare(pwrc_domain->num_clks,
  331. pwrc_domain->clks);
  332. }
  333. return 0;
  334. }
  335. static int meson_ee_pwrc_on(struct generic_pm_domain *domain)
  336. {
  337. struct meson_ee_pwrc_domain *pwrc_domain =
  338. container_of(domain, struct meson_ee_pwrc_domain, base);
  339. int i, ret;
  340. if (pwrc_domain->desc.top_pd)
  341. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  342. pwrc_domain->desc.top_pd->sleep_reg,
  343. pwrc_domain->desc.top_pd->sleep_mask, 0);
  344. udelay(20);
  345. for (i = 0 ; i < pwrc_domain->desc.mem_pd_count ; ++i)
  346. regmap_update_bits(pwrc_domain->pwrc->regmap_hhi,
  347. pwrc_domain->desc.mem_pd[i].reg,
  348. pwrc_domain->desc.mem_pd[i].mask, 0);
  349. udelay(20);
  350. ret = reset_control_assert(pwrc_domain->rstc);
  351. if (ret)
  352. return ret;
  353. if (pwrc_domain->desc.top_pd)
  354. regmap_update_bits(pwrc_domain->pwrc->regmap_ao,
  355. pwrc_domain->desc.top_pd->iso_reg,
  356. pwrc_domain->desc.top_pd->iso_mask, 0);
  357. ret = reset_control_deassert(pwrc_domain->rstc);
  358. if (ret)
  359. return ret;
  360. return clk_bulk_prepare_enable(pwrc_domain->num_clks,
  361. pwrc_domain->clks);
  362. }
  363. static int meson_ee_pwrc_init_domain(struct platform_device *pdev,
  364. struct meson_ee_pwrc *pwrc,
  365. struct meson_ee_pwrc_domain *dom)
  366. {
  367. int ret;
  368. dom->pwrc = pwrc;
  369. dom->num_rstc = dom->desc.reset_names_count;
  370. dom->num_clks = dom->desc.clk_names_count;
  371. if (dom->num_rstc) {
  372. int count = reset_control_get_count(&pdev->dev);
  373. if (count != dom->num_rstc)
  374. dev_warn(&pdev->dev, "Invalid resets count %d for domain %s\n",
  375. count, dom->desc.name);
  376. dom->rstc = devm_reset_control_array_get_exclusive(&pdev->dev);
  377. if (IS_ERR(dom->rstc))
  378. return PTR_ERR(dom->rstc);
  379. }
  380. if (dom->num_clks) {
  381. int ret = devm_clk_bulk_get_all(&pdev->dev, &dom->clks);
  382. if (ret < 0)
  383. return ret;
  384. if (dom->num_clks != ret) {
  385. dev_warn(&pdev->dev, "Invalid clocks count %d for domain %s\n",
  386. ret, dom->desc.name);
  387. dom->num_clks = ret;
  388. }
  389. }
  390. dom->base.name = dom->desc.name;
  391. dom->base.power_on = meson_ee_pwrc_on;
  392. dom->base.power_off = meson_ee_pwrc_off;
  393. /*
  394. * TOFIX: This is a special case for the VPU power domain, which can
  395. * be enabled previously by the bootloader. In this case the VPU
  396. * pipeline may be functional but no driver maybe never attach
  397. * to this power domain, and if the domain is disabled it could
  398. * cause system errors. This is why the pm_domain_always_on_gov
  399. * is used here.
  400. * For the same reason, the clocks should be enabled in case
  401. * we need to power the domain off, otherwise the internal clocks
  402. * prepare/enable counters won't be in sync.
  403. */
  404. if (dom->num_clks && dom->desc.is_powered_off && !dom->desc.is_powered_off(dom)) {
  405. ret = clk_bulk_prepare_enable(dom->num_clks, dom->clks);
  406. if (ret)
  407. return ret;
  408. dom->base.flags = GENPD_FLAG_ALWAYS_ON;
  409. ret = pm_genpd_init(&dom->base, NULL, false);
  410. if (ret)
  411. return ret;
  412. } else {
  413. ret = pm_genpd_init(&dom->base, NULL,
  414. (dom->desc.is_powered_off ?
  415. dom->desc.is_powered_off(dom) : true));
  416. if (ret)
  417. return ret;
  418. }
  419. return 0;
  420. }
  421. static int meson_ee_pwrc_probe(struct platform_device *pdev)
  422. {
  423. const struct meson_ee_pwrc_domain_data *match;
  424. struct regmap *regmap_ao, *regmap_hhi;
  425. struct device_node *parent_np;
  426. struct meson_ee_pwrc *pwrc;
  427. int i, ret;
  428. match = of_device_get_match_data(&pdev->dev);
  429. if (!match) {
  430. dev_err(&pdev->dev, "failed to get match data\n");
  431. return -ENODEV;
  432. }
  433. pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
  434. if (!pwrc)
  435. return -ENOMEM;
  436. pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
  437. sizeof(*pwrc->xlate.domains),
  438. GFP_KERNEL);
  439. if (!pwrc->xlate.domains)
  440. return -ENOMEM;
  441. pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
  442. sizeof(*pwrc->domains), GFP_KERNEL);
  443. if (!pwrc->domains)
  444. return -ENOMEM;
  445. pwrc->xlate.num_domains = match->count;
  446. parent_np = of_get_parent(pdev->dev.of_node);
  447. regmap_hhi = syscon_node_to_regmap(parent_np);
  448. of_node_put(parent_np);
  449. if (IS_ERR(regmap_hhi)) {
  450. dev_err(&pdev->dev, "failed to get HHI regmap\n");
  451. return PTR_ERR(regmap_hhi);
  452. }
  453. regmap_ao = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  454. "amlogic,ao-sysctrl");
  455. if (IS_ERR(regmap_ao)) {
  456. dev_err(&pdev->dev, "failed to get AO regmap\n");
  457. return PTR_ERR(regmap_ao);
  458. }
  459. pwrc->regmap_ao = regmap_ao;
  460. pwrc->regmap_hhi = regmap_hhi;
  461. platform_set_drvdata(pdev, pwrc);
  462. for (i = 0 ; i < match->count ; ++i) {
  463. struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
  464. memcpy(&dom->desc, &match->domains[i], sizeof(dom->desc));
  465. ret = meson_ee_pwrc_init_domain(pdev, pwrc, dom);
  466. if (ret)
  467. return ret;
  468. pwrc->xlate.domains[i] = &dom->base;
  469. }
  470. return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
  471. }
  472. static void meson_ee_pwrc_shutdown(struct platform_device *pdev)
  473. {
  474. struct meson_ee_pwrc *pwrc = platform_get_drvdata(pdev);
  475. int i;
  476. for (i = 0 ; i < pwrc->xlate.num_domains ; ++i) {
  477. struct meson_ee_pwrc_domain *dom = &pwrc->domains[i];
  478. if (dom->desc.is_powered_off && !dom->desc.is_powered_off(dom))
  479. meson_ee_pwrc_off(&dom->base);
  480. }
  481. }
  482. static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
  483. .count = ARRAY_SIZE(g12a_pwrc_domains),
  484. .domains = g12a_pwrc_domains,
  485. };
  486. static struct meson_ee_pwrc_domain_data meson_ee_axg_pwrc_data = {
  487. .count = ARRAY_SIZE(axg_pwrc_domains),
  488. .domains = axg_pwrc_domains,
  489. };
  490. static struct meson_ee_pwrc_domain_data meson_ee_gxbb_pwrc_data = {
  491. .count = ARRAY_SIZE(gxbb_pwrc_domains),
  492. .domains = gxbb_pwrc_domains,
  493. };
  494. static struct meson_ee_pwrc_domain_data meson_ee_m8_pwrc_data = {
  495. .count = ARRAY_SIZE(meson8_pwrc_domains),
  496. .domains = meson8_pwrc_domains,
  497. };
  498. static struct meson_ee_pwrc_domain_data meson_ee_m8b_pwrc_data = {
  499. .count = ARRAY_SIZE(meson8b_pwrc_domains),
  500. .domains = meson8b_pwrc_domains,
  501. };
  502. static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
  503. .count = ARRAY_SIZE(sm1_pwrc_domains),
  504. .domains = sm1_pwrc_domains,
  505. };
  506. static const struct of_device_id meson_ee_pwrc_match_table[] = {
  507. {
  508. .compatible = "amlogic,meson8-pwrc",
  509. .data = &meson_ee_m8_pwrc_data,
  510. },
  511. {
  512. .compatible = "amlogic,meson8b-pwrc",
  513. .data = &meson_ee_m8b_pwrc_data,
  514. },
  515. {
  516. .compatible = "amlogic,meson8m2-pwrc",
  517. .data = &meson_ee_m8b_pwrc_data,
  518. },
  519. {
  520. .compatible = "amlogic,meson-axg-pwrc",
  521. .data = &meson_ee_axg_pwrc_data,
  522. },
  523. {
  524. .compatible = "amlogic,meson-gxbb-pwrc",
  525. .data = &meson_ee_gxbb_pwrc_data,
  526. },
  527. {
  528. .compatible = "amlogic,meson-g12a-pwrc",
  529. .data = &meson_ee_g12a_pwrc_data,
  530. },
  531. {
  532. .compatible = "amlogic,meson-sm1-pwrc",
  533. .data = &meson_ee_sm1_pwrc_data,
  534. },
  535. { /* sentinel */ }
  536. };
  537. MODULE_DEVICE_TABLE(of, meson_ee_pwrc_match_table);
  538. static struct platform_driver meson_ee_pwrc_driver = {
  539. .probe = meson_ee_pwrc_probe,
  540. .shutdown = meson_ee_pwrc_shutdown,
  541. .driver = {
  542. .name = "meson_ee_pwrc",
  543. .of_match_table = meson_ee_pwrc_match_table,
  544. },
  545. };
  546. module_platform_driver(meson_ee_pwrc_driver);
  547. MODULE_DESCRIPTION("Amlogic Meson Everything-Else Power Domains driver");
  548. MODULE_LICENSE("GPL v2");