imx8mp-blk-ctrl.c 24 KB

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