imx8mp-blk-ctrl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2022 Pengutronix, Lucas Stach <[email protected]>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/device.h>
  7. #include <linux/interconnect.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_domain.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/regmap.h>
  14. #include <dt-bindings/power/imx8mp-power.h>
  15. #define GPR_REG0 0x0
  16. #define PCIE_CLOCK_MODULE_EN BIT(0)
  17. #define USB_CLOCK_MODULE_EN BIT(1)
  18. #define PCIE_PHY_APB_RST BIT(4)
  19. #define PCIE_PHY_INIT_RST BIT(5)
  20. struct imx8mp_blk_ctrl_domain;
  21. struct imx8mp_blk_ctrl {
  22. struct device *dev;
  23. struct notifier_block power_nb;
  24. struct device *bus_power_dev;
  25. struct regmap *regmap;
  26. struct imx8mp_blk_ctrl_domain *domains;
  27. struct genpd_onecell_data onecell_data;
  28. void (*power_off) (struct imx8mp_blk_ctrl *bc, struct imx8mp_blk_ctrl_domain *domain);
  29. void (*power_on) (struct imx8mp_blk_ctrl *bc, struct imx8mp_blk_ctrl_domain *domain);
  30. };
  31. struct imx8mp_blk_ctrl_domain_data {
  32. const char *name;
  33. const char * const *clk_names;
  34. int num_clks;
  35. const char * const *path_names;
  36. int num_paths;
  37. const char *gpc_name;
  38. };
  39. #define DOMAIN_MAX_CLKS 2
  40. #define DOMAIN_MAX_PATHS 3
  41. struct imx8mp_blk_ctrl_domain {
  42. struct generic_pm_domain genpd;
  43. const struct imx8mp_blk_ctrl_domain_data *data;
  44. struct clk_bulk_data clks[DOMAIN_MAX_CLKS];
  45. struct icc_bulk_data paths[DOMAIN_MAX_PATHS];
  46. struct device *power_dev;
  47. struct imx8mp_blk_ctrl *bc;
  48. int num_paths;
  49. int id;
  50. };
  51. struct imx8mp_blk_ctrl_data {
  52. int max_reg;
  53. notifier_fn_t power_notifier_fn;
  54. void (*power_off) (struct imx8mp_blk_ctrl *bc, struct imx8mp_blk_ctrl_domain *domain);
  55. void (*power_on) (struct imx8mp_blk_ctrl *bc, struct imx8mp_blk_ctrl_domain *domain);
  56. const struct imx8mp_blk_ctrl_domain_data *domains;
  57. int num_domains;
  58. };
  59. static inline struct imx8mp_blk_ctrl_domain *
  60. to_imx8mp_blk_ctrl_domain(struct generic_pm_domain *genpd)
  61. {
  62. return container_of(genpd, struct imx8mp_blk_ctrl_domain, genpd);
  63. }
  64. static void imx8mp_hsio_blk_ctrl_power_on(struct imx8mp_blk_ctrl *bc,
  65. struct imx8mp_blk_ctrl_domain *domain)
  66. {
  67. switch (domain->id) {
  68. case IMX8MP_HSIOBLK_PD_USB:
  69. regmap_set_bits(bc->regmap, GPR_REG0, USB_CLOCK_MODULE_EN);
  70. break;
  71. case IMX8MP_HSIOBLK_PD_PCIE:
  72. regmap_set_bits(bc->regmap, GPR_REG0, PCIE_CLOCK_MODULE_EN);
  73. break;
  74. case IMX8MP_HSIOBLK_PD_PCIE_PHY:
  75. regmap_set_bits(bc->regmap, GPR_REG0,
  76. PCIE_PHY_APB_RST | PCIE_PHY_INIT_RST);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. static void imx8mp_hsio_blk_ctrl_power_off(struct imx8mp_blk_ctrl *bc,
  83. struct imx8mp_blk_ctrl_domain *domain)
  84. {
  85. switch (domain->id) {
  86. case IMX8MP_HSIOBLK_PD_USB:
  87. regmap_clear_bits(bc->regmap, GPR_REG0, USB_CLOCK_MODULE_EN);
  88. break;
  89. case IMX8MP_HSIOBLK_PD_PCIE:
  90. regmap_clear_bits(bc->regmap, GPR_REG0, PCIE_CLOCK_MODULE_EN);
  91. break;
  92. case IMX8MP_HSIOBLK_PD_PCIE_PHY:
  93. regmap_clear_bits(bc->regmap, GPR_REG0,
  94. PCIE_PHY_APB_RST | PCIE_PHY_INIT_RST);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. static int imx8mp_hsio_power_notifier(struct notifier_block *nb,
  101. unsigned long action, void *data)
  102. {
  103. struct imx8mp_blk_ctrl *bc = container_of(nb, struct imx8mp_blk_ctrl,
  104. power_nb);
  105. struct clk_bulk_data *usb_clk = bc->domains[IMX8MP_HSIOBLK_PD_USB].clks;
  106. int num_clks = bc->domains[IMX8MP_HSIOBLK_PD_USB].data->num_clks;
  107. int ret;
  108. switch (action) {
  109. case GENPD_NOTIFY_ON:
  110. /*
  111. * enable USB clock for a moment for the power-on ADB handshake
  112. * to proceed
  113. */
  114. ret = clk_bulk_prepare_enable(num_clks, usb_clk);
  115. if (ret)
  116. return NOTIFY_BAD;
  117. regmap_set_bits(bc->regmap, GPR_REG0, USB_CLOCK_MODULE_EN);
  118. udelay(5);
  119. regmap_clear_bits(bc->regmap, GPR_REG0, USB_CLOCK_MODULE_EN);
  120. clk_bulk_disable_unprepare(num_clks, usb_clk);
  121. break;
  122. case GENPD_NOTIFY_PRE_OFF:
  123. /* enable USB clock for the power-down ADB handshake to work */
  124. ret = clk_bulk_prepare_enable(num_clks, usb_clk);
  125. if (ret)
  126. return NOTIFY_BAD;
  127. regmap_set_bits(bc->regmap, GPR_REG0, USB_CLOCK_MODULE_EN);
  128. break;
  129. case GENPD_NOTIFY_OFF:
  130. clk_bulk_disable_unprepare(num_clks, usb_clk);
  131. break;
  132. default:
  133. break;
  134. }
  135. return NOTIFY_OK;
  136. }
  137. static const struct imx8mp_blk_ctrl_domain_data imx8mp_hsio_domain_data[] = {
  138. [IMX8MP_HSIOBLK_PD_USB] = {
  139. .name = "hsioblk-usb",
  140. .clk_names = (const char *[]){ "usb" },
  141. .num_clks = 1,
  142. .gpc_name = "usb",
  143. .path_names = (const char *[]){"usb1", "usb2"},
  144. .num_paths = 2,
  145. },
  146. [IMX8MP_HSIOBLK_PD_USB_PHY1] = {
  147. .name = "hsioblk-usb-phy1",
  148. .gpc_name = "usb-phy1",
  149. },
  150. [IMX8MP_HSIOBLK_PD_USB_PHY2] = {
  151. .name = "hsioblk-usb-phy2",
  152. .gpc_name = "usb-phy2",
  153. },
  154. [IMX8MP_HSIOBLK_PD_PCIE] = {
  155. .name = "hsioblk-pcie",
  156. .clk_names = (const char *[]){ "pcie" },
  157. .num_clks = 1,
  158. .gpc_name = "pcie",
  159. .path_names = (const char *[]){"noc-pcie", "pcie"},
  160. .num_paths = 2,
  161. },
  162. [IMX8MP_HSIOBLK_PD_PCIE_PHY] = {
  163. .name = "hsioblk-pcie-phy",
  164. .gpc_name = "pcie-phy",
  165. },
  166. };
  167. static const struct imx8mp_blk_ctrl_data imx8mp_hsio_blk_ctl_dev_data = {
  168. .max_reg = 0x24,
  169. .power_on = imx8mp_hsio_blk_ctrl_power_on,
  170. .power_off = imx8mp_hsio_blk_ctrl_power_off,
  171. .power_notifier_fn = imx8mp_hsio_power_notifier,
  172. .domains = imx8mp_hsio_domain_data,
  173. .num_domains = ARRAY_SIZE(imx8mp_hsio_domain_data),
  174. };
  175. #define HDMI_RTX_RESET_CTL0 0x20
  176. #define HDMI_RTX_CLK_CTL0 0x40
  177. #define HDMI_RTX_CLK_CTL1 0x50
  178. #define HDMI_RTX_CLK_CTL2 0x60
  179. #define HDMI_RTX_CLK_CTL3 0x70
  180. #define HDMI_RTX_CLK_CTL4 0x80
  181. #define HDMI_TX_CONTROL0 0x200
  182. static void imx8mp_hdmi_blk_ctrl_power_on(struct imx8mp_blk_ctrl *bc,
  183. struct imx8mp_blk_ctrl_domain *domain)
  184. {
  185. switch (domain->id) {
  186. case IMX8MP_HDMIBLK_PD_IRQSTEER:
  187. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(9));
  188. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(16));
  189. break;
  190. case IMX8MP_HDMIBLK_PD_LCDIF:
  191. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0,
  192. BIT(16) | BIT(17) | BIT(18) |
  193. BIT(19) | BIT(20));
  194. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(11));
  195. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0,
  196. BIT(4) | BIT(5) | BIT(6));
  197. break;
  198. case IMX8MP_HDMIBLK_PD_PAI:
  199. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(17));
  200. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(18));
  201. break;
  202. case IMX8MP_HDMIBLK_PD_PVI:
  203. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(28));
  204. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(22));
  205. break;
  206. case IMX8MP_HDMIBLK_PD_TRNG:
  207. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(27) | BIT(30));
  208. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(20));
  209. break;
  210. case IMX8MP_HDMIBLK_PD_HDMI_TX:
  211. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0,
  212. BIT(2) | BIT(4) | BIT(5));
  213. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1,
  214. BIT(12) | BIT(13) | BIT(14) | BIT(15) | BIT(16) |
  215. BIT(18) | BIT(19) | BIT(20) | BIT(21));
  216. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0,
  217. BIT(7) | BIT(10) | BIT(11));
  218. regmap_set_bits(bc->regmap, HDMI_TX_CONTROL0, BIT(1));
  219. break;
  220. case IMX8MP_HDMIBLK_PD_HDMI_TX_PHY:
  221. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(7));
  222. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(22) | BIT(24));
  223. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(12));
  224. regmap_clear_bits(bc->regmap, HDMI_TX_CONTROL0, BIT(3));
  225. break;
  226. case IMX8MP_HDMIBLK_PD_HDCP:
  227. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(11));
  228. break;
  229. case IMX8MP_HDMIBLK_PD_HRV:
  230. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(3) | BIT(4) | BIT(5));
  231. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(15));
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. static void imx8mp_hdmi_blk_ctrl_power_off(struct imx8mp_blk_ctrl *bc,
  238. struct imx8mp_blk_ctrl_domain *domain)
  239. {
  240. switch (domain->id) {
  241. case IMX8MP_HDMIBLK_PD_IRQSTEER:
  242. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(9));
  243. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(16));
  244. break;
  245. case IMX8MP_HDMIBLK_PD_LCDIF:
  246. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0,
  247. BIT(4) | BIT(5) | BIT(6));
  248. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(11));
  249. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL0,
  250. BIT(16) | BIT(17) | BIT(18) |
  251. BIT(19) | BIT(20));
  252. break;
  253. case IMX8MP_HDMIBLK_PD_PAI:
  254. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(18));
  255. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(17));
  256. break;
  257. case IMX8MP_HDMIBLK_PD_PVI:
  258. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(22));
  259. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(28));
  260. break;
  261. case IMX8MP_HDMIBLK_PD_TRNG:
  262. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(20));
  263. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(27) | BIT(30));
  264. break;
  265. case IMX8MP_HDMIBLK_PD_HDMI_TX:
  266. regmap_clear_bits(bc->regmap, HDMI_TX_CONTROL0, BIT(1));
  267. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0,
  268. BIT(7) | BIT(10) | BIT(11));
  269. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1,
  270. BIT(12) | BIT(13) | BIT(14) | BIT(15) | BIT(16) |
  271. BIT(18) | BIT(19) | BIT(20) | BIT(21));
  272. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL0,
  273. BIT(2) | BIT(4) | BIT(5));
  274. break;
  275. case IMX8MP_HDMIBLK_PD_HDMI_TX_PHY:
  276. regmap_set_bits(bc->regmap, HDMI_TX_CONTROL0, BIT(3));
  277. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(12));
  278. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(7));
  279. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(22) | BIT(24));
  280. break;
  281. case IMX8MP_HDMIBLK_PD_HDCP:
  282. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL0, BIT(11));
  283. break;
  284. case IMX8MP_HDMIBLK_PD_HRV:
  285. regmap_clear_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(15));
  286. regmap_clear_bits(bc->regmap, HDMI_RTX_CLK_CTL1, BIT(3) | BIT(4) | BIT(5));
  287. break;
  288. default:
  289. break;
  290. }
  291. }
  292. static int imx8mp_hdmi_power_notifier(struct notifier_block *nb,
  293. unsigned long action, void *data)
  294. {
  295. struct imx8mp_blk_ctrl *bc = container_of(nb, struct imx8mp_blk_ctrl,
  296. power_nb);
  297. if (action != GENPD_NOTIFY_ON)
  298. return NOTIFY_OK;
  299. /*
  300. * Contrary to other blk-ctrls the reset and clock don't clear when the
  301. * power domain is powered down. To ensure the proper reset pulsing,
  302. * first clear them all to asserted state, then enable the bus clocks
  303. * and then release the ADB reset.
  304. */
  305. regmap_write(bc->regmap, HDMI_RTX_RESET_CTL0, 0x0);
  306. regmap_write(bc->regmap, HDMI_RTX_CLK_CTL0, 0x0);
  307. regmap_write(bc->regmap, HDMI_RTX_CLK_CTL1, 0x0);
  308. regmap_set_bits(bc->regmap, HDMI_RTX_CLK_CTL0,
  309. BIT(0) | BIT(1) | BIT(10));
  310. regmap_set_bits(bc->regmap, HDMI_RTX_RESET_CTL0, BIT(0));
  311. /*
  312. * On power up we have no software backchannel to the GPC to
  313. * wait for the ADB handshake to happen, so we just delay for a
  314. * bit. On power down the GPC driver waits for the handshake.
  315. */
  316. udelay(5);
  317. return NOTIFY_OK;
  318. }
  319. static const struct imx8mp_blk_ctrl_domain_data imx8mp_hdmi_domain_data[] = {
  320. [IMX8MP_HDMIBLK_PD_IRQSTEER] = {
  321. .name = "hdmiblk-irqsteer",
  322. .clk_names = (const char *[]){ "apb" },
  323. .num_clks = 1,
  324. .gpc_name = "irqsteer",
  325. },
  326. [IMX8MP_HDMIBLK_PD_LCDIF] = {
  327. .name = "hdmiblk-lcdif",
  328. .clk_names = (const char *[]){ "axi", "apb" },
  329. .num_clks = 2,
  330. .gpc_name = "lcdif",
  331. .path_names = (const char *[]){"lcdif-hdmi"},
  332. .num_paths = 1,
  333. },
  334. [IMX8MP_HDMIBLK_PD_PAI] = {
  335. .name = "hdmiblk-pai",
  336. .clk_names = (const char *[]){ "apb" },
  337. .num_clks = 1,
  338. .gpc_name = "pai",
  339. },
  340. [IMX8MP_HDMIBLK_PD_PVI] = {
  341. .name = "hdmiblk-pvi",
  342. .clk_names = (const char *[]){ "apb" },
  343. .num_clks = 1,
  344. .gpc_name = "pvi",
  345. },
  346. [IMX8MP_HDMIBLK_PD_TRNG] = {
  347. .name = "hdmiblk-trng",
  348. .clk_names = (const char *[]){ "apb" },
  349. .num_clks = 1,
  350. .gpc_name = "trng",
  351. },
  352. [IMX8MP_HDMIBLK_PD_HDMI_TX] = {
  353. .name = "hdmiblk-hdmi-tx",
  354. .clk_names = (const char *[]){ "apb", "ref_266m" },
  355. .num_clks = 2,
  356. .gpc_name = "hdmi-tx",
  357. },
  358. [IMX8MP_HDMIBLK_PD_HDMI_TX_PHY] = {
  359. .name = "hdmiblk-hdmi-tx-phy",
  360. .clk_names = (const char *[]){ "apb", "ref_24m" },
  361. .num_clks = 2,
  362. .gpc_name = "hdmi-tx-phy",
  363. },
  364. [IMX8MP_HDMIBLK_PD_HRV] = {
  365. .name = "hdmiblk-hrv",
  366. .clk_names = (const char *[]){ "axi", "apb" },
  367. .num_clks = 2,
  368. .gpc_name = "hrv",
  369. .path_names = (const char *[]){"hrv"},
  370. .num_paths = 1,
  371. },
  372. [IMX8MP_HDMIBLK_PD_HDCP] = {
  373. .name = "hdmiblk-hdcp",
  374. .clk_names = (const char *[]){ "axi", "apb" },
  375. .num_clks = 2,
  376. .gpc_name = "hdcp",
  377. .path_names = (const char *[]){"hdcp"},
  378. .num_paths = 1,
  379. },
  380. };
  381. static const struct imx8mp_blk_ctrl_data imx8mp_hdmi_blk_ctl_dev_data = {
  382. .max_reg = 0x23c,
  383. .power_on = imx8mp_hdmi_blk_ctrl_power_on,
  384. .power_off = imx8mp_hdmi_blk_ctrl_power_off,
  385. .power_notifier_fn = imx8mp_hdmi_power_notifier,
  386. .domains = imx8mp_hdmi_domain_data,
  387. .num_domains = ARRAY_SIZE(imx8mp_hdmi_domain_data),
  388. };
  389. static int imx8mp_blk_ctrl_power_on(struct generic_pm_domain *genpd)
  390. {
  391. struct imx8mp_blk_ctrl_domain *domain = to_imx8mp_blk_ctrl_domain(genpd);
  392. const struct imx8mp_blk_ctrl_domain_data *data = domain->data;
  393. struct imx8mp_blk_ctrl *bc = domain->bc;
  394. int ret;
  395. /* make sure bus domain is awake */
  396. ret = pm_runtime_resume_and_get(bc->bus_power_dev);
  397. if (ret < 0) {
  398. dev_err(bc->dev, "failed to power up bus domain\n");
  399. return ret;
  400. }
  401. /* enable upstream clocks */
  402. ret = clk_bulk_prepare_enable(data->num_clks, domain->clks);
  403. if (ret) {
  404. dev_err(bc->dev, "failed to enable clocks\n");
  405. goto bus_put;
  406. }
  407. /* domain specific blk-ctrl manipulation */
  408. bc->power_on(bc, domain);
  409. /* power up upstream GPC domain */
  410. ret = pm_runtime_resume_and_get(domain->power_dev);
  411. if (ret < 0) {
  412. dev_err(bc->dev, "failed to power up peripheral domain\n");
  413. goto clk_disable;
  414. }
  415. ret = icc_bulk_set_bw(domain->num_paths, domain->paths);
  416. if (ret)
  417. dev_err(bc->dev, "failed to set icc bw\n");
  418. clk_bulk_disable_unprepare(data->num_clks, domain->clks);
  419. return 0;
  420. clk_disable:
  421. clk_bulk_disable_unprepare(data->num_clks, domain->clks);
  422. bus_put:
  423. pm_runtime_put(bc->bus_power_dev);
  424. return ret;
  425. }
  426. static int imx8mp_blk_ctrl_power_off(struct generic_pm_domain *genpd)
  427. {
  428. struct imx8mp_blk_ctrl_domain *domain = to_imx8mp_blk_ctrl_domain(genpd);
  429. const struct imx8mp_blk_ctrl_domain_data *data = domain->data;
  430. struct imx8mp_blk_ctrl *bc = domain->bc;
  431. int ret;
  432. ret = clk_bulk_prepare_enable(data->num_clks, domain->clks);
  433. if (ret) {
  434. dev_err(bc->dev, "failed to enable clocks\n");
  435. return ret;
  436. }
  437. /* domain specific blk-ctrl manipulation */
  438. bc->power_off(bc, domain);
  439. clk_bulk_disable_unprepare(data->num_clks, domain->clks);
  440. /* power down upstream GPC domain */
  441. pm_runtime_put(domain->power_dev);
  442. /* allow bus domain to suspend */
  443. pm_runtime_put(bc->bus_power_dev);
  444. return 0;
  445. }
  446. static struct lock_class_key blk_ctrl_genpd_lock_class;
  447. static int imx8mp_blk_ctrl_probe(struct platform_device *pdev)
  448. {
  449. const struct imx8mp_blk_ctrl_data *bc_data;
  450. struct device *dev = &pdev->dev;
  451. struct imx8mp_blk_ctrl *bc;
  452. void __iomem *base;
  453. int num_domains, i, ret;
  454. struct regmap_config regmap_config = {
  455. .reg_bits = 32,
  456. .val_bits = 32,
  457. .reg_stride = 4,
  458. };
  459. bc = devm_kzalloc(dev, sizeof(*bc), GFP_KERNEL);
  460. if (!bc)
  461. return -ENOMEM;
  462. bc->dev = dev;
  463. bc_data = of_device_get_match_data(dev);
  464. num_domains = bc_data->num_domains;
  465. base = devm_platform_ioremap_resource(pdev, 0);
  466. if (IS_ERR(base))
  467. return PTR_ERR(base);
  468. regmap_config.max_register = bc_data->max_reg;
  469. bc->regmap = devm_regmap_init_mmio(dev, base, &regmap_config);
  470. if (IS_ERR(bc->regmap))
  471. return dev_err_probe(dev, PTR_ERR(bc->regmap),
  472. "failed to init regmap\n");
  473. bc->domains = devm_kcalloc(dev, num_domains,
  474. sizeof(struct imx8mp_blk_ctrl_domain),
  475. GFP_KERNEL);
  476. if (!bc->domains)
  477. return -ENOMEM;
  478. bc->onecell_data.num_domains = num_domains;
  479. bc->onecell_data.domains =
  480. devm_kcalloc(dev, num_domains,
  481. sizeof(struct generic_pm_domain *), GFP_KERNEL);
  482. if (!bc->onecell_data.domains)
  483. return -ENOMEM;
  484. bc->bus_power_dev = genpd_dev_pm_attach_by_name(dev, "bus");
  485. if (IS_ERR(bc->bus_power_dev))
  486. return dev_err_probe(dev, PTR_ERR(bc->bus_power_dev),
  487. "failed to attach bus power domain\n");
  488. bc->power_off = bc_data->power_off;
  489. bc->power_on = bc_data->power_on;
  490. for (i = 0; i < num_domains; i++) {
  491. const struct imx8mp_blk_ctrl_domain_data *data = &bc_data->domains[i];
  492. struct imx8mp_blk_ctrl_domain *domain = &bc->domains[i];
  493. int j;
  494. domain->data = data;
  495. domain->num_paths = data->num_paths;
  496. for (j = 0; j < data->num_clks; j++)
  497. domain->clks[j].id = data->clk_names[j];
  498. for (j = 0; j < data->num_paths; j++) {
  499. domain->paths[j].name = data->path_names[j];
  500. /* Fake value for now, just let ICC could configure NoC mode/priority */
  501. domain->paths[j].avg_bw = 1;
  502. domain->paths[j].peak_bw = 1;
  503. }
  504. ret = devm_of_icc_bulk_get(dev, data->num_paths, domain->paths);
  505. if (ret) {
  506. if (ret != -EPROBE_DEFER) {
  507. dev_warn_once(dev, "Could not get interconnect paths, NoC will stay unconfigured!\n");
  508. domain->num_paths = 0;
  509. } else {
  510. dev_err_probe(dev, ret, "failed to get noc entries\n");
  511. goto cleanup_pds;
  512. }
  513. }
  514. ret = devm_clk_bulk_get(dev, data->num_clks, domain->clks);
  515. if (ret) {
  516. dev_err_probe(dev, ret, "failed to get clock\n");
  517. goto cleanup_pds;
  518. }
  519. domain->power_dev =
  520. dev_pm_domain_attach_by_name(dev, data->gpc_name);
  521. if (IS_ERR(domain->power_dev)) {
  522. dev_err_probe(dev, PTR_ERR(domain->power_dev),
  523. "failed to attach power domain %s\n",
  524. data->gpc_name);
  525. ret = PTR_ERR(domain->power_dev);
  526. goto cleanup_pds;
  527. }
  528. domain->genpd.name = data->name;
  529. domain->genpd.power_on = imx8mp_blk_ctrl_power_on;
  530. domain->genpd.power_off = imx8mp_blk_ctrl_power_off;
  531. domain->bc = bc;
  532. domain->id = i;
  533. ret = pm_genpd_init(&domain->genpd, NULL, true);
  534. if (ret) {
  535. dev_err_probe(dev, ret, "failed to init power domain\n");
  536. dev_pm_domain_detach(domain->power_dev, true);
  537. goto cleanup_pds;
  538. }
  539. /*
  540. * We use runtime PM to trigger power on/off of the upstream GPC
  541. * domain, as a strict hierarchical parent/child power domain
  542. * setup doesn't allow us to meet the sequencing requirements.
  543. * This means we have nested locking of genpd locks, without the
  544. * nesting being visible at the genpd level, so we need a
  545. * separate lock class to make lockdep aware of the fact that
  546. * this are separate domain locks that can be nested without a
  547. * self-deadlock.
  548. */
  549. lockdep_set_class(&domain->genpd.mlock,
  550. &blk_ctrl_genpd_lock_class);
  551. bc->onecell_data.domains[i] = &domain->genpd;
  552. }
  553. ret = of_genpd_add_provider_onecell(dev->of_node, &bc->onecell_data);
  554. if (ret) {
  555. dev_err_probe(dev, ret, "failed to add power domain provider\n");
  556. goto cleanup_pds;
  557. }
  558. bc->power_nb.notifier_call = bc_data->power_notifier_fn;
  559. ret = dev_pm_genpd_add_notifier(bc->bus_power_dev, &bc->power_nb);
  560. if (ret) {
  561. dev_err_probe(dev, ret, "failed to add power notifier\n");
  562. goto cleanup_provider;
  563. }
  564. dev_set_drvdata(dev, bc);
  565. return 0;
  566. cleanup_provider:
  567. of_genpd_del_provider(dev->of_node);
  568. cleanup_pds:
  569. for (i--; i >= 0; i--) {
  570. pm_genpd_remove(&bc->domains[i].genpd);
  571. dev_pm_domain_detach(bc->domains[i].power_dev, true);
  572. }
  573. dev_pm_domain_detach(bc->bus_power_dev, true);
  574. return ret;
  575. }
  576. static int imx8mp_blk_ctrl_remove(struct platform_device *pdev)
  577. {
  578. struct imx8mp_blk_ctrl *bc = dev_get_drvdata(&pdev->dev);
  579. int i;
  580. of_genpd_del_provider(pdev->dev.of_node);
  581. for (i = 0; bc->onecell_data.num_domains; i++) {
  582. struct imx8mp_blk_ctrl_domain *domain = &bc->domains[i];
  583. pm_genpd_remove(&domain->genpd);
  584. dev_pm_domain_detach(domain->power_dev, true);
  585. }
  586. dev_pm_genpd_remove_notifier(bc->bus_power_dev);
  587. dev_pm_domain_detach(bc->bus_power_dev, true);
  588. return 0;
  589. }
  590. #ifdef CONFIG_PM_SLEEP
  591. static int imx8mp_blk_ctrl_suspend(struct device *dev)
  592. {
  593. struct imx8mp_blk_ctrl *bc = dev_get_drvdata(dev);
  594. int ret, i;
  595. /*
  596. * This may look strange, but is done so the generic PM_SLEEP code
  597. * can power down our domains and more importantly power them up again
  598. * after resume, without tripping over our usage of runtime PM to
  599. * control the upstream GPC domains. Things happen in the right order
  600. * in the system suspend/resume paths due to the device parent/child
  601. * hierarchy.
  602. */
  603. ret = pm_runtime_get_sync(bc->bus_power_dev);
  604. if (ret < 0) {
  605. pm_runtime_put_noidle(bc->bus_power_dev);
  606. return ret;
  607. }
  608. for (i = 0; i < bc->onecell_data.num_domains; i++) {
  609. struct imx8mp_blk_ctrl_domain *domain = &bc->domains[i];
  610. ret = pm_runtime_get_sync(domain->power_dev);
  611. if (ret < 0) {
  612. pm_runtime_put_noidle(domain->power_dev);
  613. goto out_fail;
  614. }
  615. }
  616. return 0;
  617. out_fail:
  618. for (i--; i >= 0; i--)
  619. pm_runtime_put(bc->domains[i].power_dev);
  620. pm_runtime_put(bc->bus_power_dev);
  621. return ret;
  622. }
  623. static int imx8mp_blk_ctrl_resume(struct device *dev)
  624. {
  625. struct imx8mp_blk_ctrl *bc = dev_get_drvdata(dev);
  626. int i;
  627. for (i = 0; i < bc->onecell_data.num_domains; i++)
  628. pm_runtime_put(bc->domains[i].power_dev);
  629. pm_runtime_put(bc->bus_power_dev);
  630. return 0;
  631. }
  632. #endif
  633. static const struct dev_pm_ops imx8mp_blk_ctrl_pm_ops = {
  634. SET_SYSTEM_SLEEP_PM_OPS(imx8mp_blk_ctrl_suspend,
  635. imx8mp_blk_ctrl_resume)
  636. };
  637. static const struct of_device_id imx8mp_blk_ctrl_of_match[] = {
  638. {
  639. .compatible = "fsl,imx8mp-hsio-blk-ctrl",
  640. .data = &imx8mp_hsio_blk_ctl_dev_data,
  641. }, {
  642. .compatible = "fsl,imx8mp-hdmi-blk-ctrl",
  643. .data = &imx8mp_hdmi_blk_ctl_dev_data,
  644. }, {
  645. /* Sentinel */
  646. }
  647. };
  648. MODULE_DEVICE_TABLE(of, imx8m_blk_ctrl_of_match);
  649. static struct platform_driver imx8mp_blk_ctrl_driver = {
  650. .probe = imx8mp_blk_ctrl_probe,
  651. .remove = imx8mp_blk_ctrl_remove,
  652. .driver = {
  653. .name = "imx8mp-blk-ctrl",
  654. .pm = &imx8mp_blk_ctrl_pm_ops,
  655. .of_match_table = imx8mp_blk_ctrl_of_match,
  656. },
  657. };
  658. module_platform_driver(imx8mp_blk_ctrl_driver);