meson-ee-pwrc.c 17 KB

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