gpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright 2015-2017 Pengutronix, Lucas Stach <kernel@pengutronix.de>
  3. * Copyright 2011-2013 Freescale Semiconductor, Inc.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_domain.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #define GPC_CNTR 0x000
  21. #define GPC_PGC_CTRL_OFFS 0x0
  22. #define GPC_PGC_PUPSCR_OFFS 0x4
  23. #define GPC_PGC_PDNSCR_OFFS 0x8
  24. #define GPC_PGC_SW2ISO_SHIFT 0x8
  25. #define GPC_PGC_SW_SHIFT 0x0
  26. #define GPC_PGC_GPU_PDN 0x260
  27. #define GPC_PGC_GPU_PUPSCR 0x264
  28. #define GPC_PGC_GPU_PDNSCR 0x268
  29. #define GPU_VPU_PUP_REQ BIT(1)
  30. #define GPU_VPU_PDN_REQ BIT(0)
  31. #define GPC_CLK_MAX 6
  32. #define PGC_DOMAIN_FLAG_NO_PD BIT(0)
  33. struct imx_pm_domain {
  34. struct generic_pm_domain base;
  35. struct regmap *regmap;
  36. struct regulator *supply;
  37. struct clk *clk[GPC_CLK_MAX];
  38. int num_clks;
  39. unsigned int reg_offs;
  40. signed char cntr_pdn_bit;
  41. unsigned int ipg_rate_mhz;
  42. unsigned int flags;
  43. };
  44. static inline struct imx_pm_domain *
  45. to_imx_pm_domain(struct generic_pm_domain *genpd)
  46. {
  47. return container_of(genpd, struct imx_pm_domain, base);
  48. }
  49. static int imx6_pm_domain_power_off(struct generic_pm_domain *genpd)
  50. {
  51. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  52. int iso, iso2sw;
  53. u32 val;
  54. if (pd->flags & PGC_DOMAIN_FLAG_NO_PD)
  55. return -EBUSY;
  56. /* Read ISO and ISO2SW power down delays */
  57. regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PUPSCR_OFFS, &val);
  58. iso = val & 0x3f;
  59. iso2sw = (val >> 8) & 0x3f;
  60. /* Gate off domain when powered down */
  61. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  62. 0x1, 0x1);
  63. /* Request GPC to power down domain */
  64. val = BIT(pd->cntr_pdn_bit);
  65. regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
  66. /* Wait ISO + ISO2SW IPG clock cycles */
  67. udelay(DIV_ROUND_UP(iso + iso2sw, pd->ipg_rate_mhz));
  68. if (pd->supply)
  69. regulator_disable(pd->supply);
  70. return 0;
  71. }
  72. static int imx6_pm_domain_power_on(struct generic_pm_domain *genpd)
  73. {
  74. struct imx_pm_domain *pd = to_imx_pm_domain(genpd);
  75. int i, ret, sw, sw2iso;
  76. u32 val;
  77. if (pd->supply) {
  78. ret = regulator_enable(pd->supply);
  79. if (ret) {
  80. pr_err("%s: failed to enable regulator: %d\n",
  81. __func__, ret);
  82. return ret;
  83. }
  84. }
  85. /* Enable reset clocks for all devices in the domain */
  86. for (i = 0; i < pd->num_clks; i++)
  87. clk_prepare_enable(pd->clk[i]);
  88. /* Gate off domain when powered down */
  89. regmap_update_bits(pd->regmap, pd->reg_offs + GPC_PGC_CTRL_OFFS,
  90. 0x1, 0x1);
  91. /* Read ISO and ISO2SW power up delays */
  92. regmap_read(pd->regmap, pd->reg_offs + GPC_PGC_PUPSCR_OFFS, &val);
  93. sw = val & 0x3f;
  94. sw2iso = (val >> 8) & 0x3f;
  95. /* Request GPC to power up domain */
  96. val = BIT(pd->cntr_pdn_bit + 1);
  97. regmap_update_bits(pd->regmap, GPC_CNTR, val, val);
  98. /* Wait ISO + ISO2SW IPG clock cycles */
  99. udelay(DIV_ROUND_UP(sw + sw2iso, pd->ipg_rate_mhz));
  100. /* Disable reset clocks for all devices in the domain */
  101. for (i = 0; i < pd->num_clks; i++)
  102. clk_disable_unprepare(pd->clk[i]);
  103. return 0;
  104. }
  105. static int imx_pgc_get_clocks(struct device *dev, struct imx_pm_domain *domain)
  106. {
  107. int i, ret;
  108. for (i = 0; ; i++) {
  109. struct clk *clk = of_clk_get(dev->of_node, i);
  110. if (IS_ERR(clk))
  111. break;
  112. if (i >= GPC_CLK_MAX) {
  113. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  114. ret = -EINVAL;
  115. goto clk_err;
  116. }
  117. domain->clk[i] = clk;
  118. }
  119. domain->num_clks = i;
  120. return 0;
  121. clk_err:
  122. while (i--)
  123. clk_put(domain->clk[i]);
  124. return ret;
  125. }
  126. static void imx_pgc_put_clocks(struct imx_pm_domain *domain)
  127. {
  128. int i;
  129. for (i = domain->num_clks - 1; i >= 0; i--)
  130. clk_put(domain->clk[i]);
  131. }
  132. static int imx_pgc_parse_dt(struct device *dev, struct imx_pm_domain *domain)
  133. {
  134. /* try to get the domain supply regulator */
  135. domain->supply = devm_regulator_get_optional(dev, "power");
  136. if (IS_ERR(domain->supply)) {
  137. if (PTR_ERR(domain->supply) == -ENODEV)
  138. domain->supply = NULL;
  139. else
  140. return PTR_ERR(domain->supply);
  141. }
  142. /* try to get all clocks needed for reset propagation */
  143. return imx_pgc_get_clocks(dev, domain);
  144. }
  145. static int imx_pgc_power_domain_probe(struct platform_device *pdev)
  146. {
  147. struct imx_pm_domain *domain = pdev->dev.platform_data;
  148. struct device *dev = &pdev->dev;
  149. int ret;
  150. /* if this PD is associated with a DT node try to parse it */
  151. if (dev->of_node) {
  152. ret = imx_pgc_parse_dt(dev, domain);
  153. if (ret)
  154. return ret;
  155. }
  156. /* initially power on the domain */
  157. if (domain->base.power_on)
  158. domain->base.power_on(&domain->base);
  159. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  160. pm_genpd_init(&domain->base, NULL, false);
  161. ret = of_genpd_add_provider_simple(dev->of_node, &domain->base);
  162. if (ret)
  163. goto genpd_err;
  164. }
  165. device_link_add(dev, dev->parent, DL_FLAG_AUTOREMOVE);
  166. return 0;
  167. genpd_err:
  168. pm_genpd_remove(&domain->base);
  169. imx_pgc_put_clocks(domain);
  170. return ret;
  171. }
  172. static int imx_pgc_power_domain_remove(struct platform_device *pdev)
  173. {
  174. struct imx_pm_domain *domain = pdev->dev.platform_data;
  175. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  176. of_genpd_del_provider(pdev->dev.of_node);
  177. pm_genpd_remove(&domain->base);
  178. imx_pgc_put_clocks(domain);
  179. }
  180. return 0;
  181. }
  182. static const struct platform_device_id imx_pgc_power_domain_id[] = {
  183. { "imx-pgc-power-domain"},
  184. { },
  185. };
  186. static struct platform_driver imx_pgc_power_domain_driver = {
  187. .driver = {
  188. .name = "imx-pgc-pd",
  189. },
  190. .probe = imx_pgc_power_domain_probe,
  191. .remove = imx_pgc_power_domain_remove,
  192. .id_table = imx_pgc_power_domain_id,
  193. };
  194. builtin_platform_driver(imx_pgc_power_domain_driver)
  195. #define GPC_PGC_DOMAIN_ARM 0
  196. #define GPC_PGC_DOMAIN_PU 1
  197. #define GPC_PGC_DOMAIN_DISPLAY 2
  198. static struct genpd_power_state imx6_pm_domain_pu_state = {
  199. .power_off_latency_ns = 25000,
  200. .power_on_latency_ns = 2000000,
  201. };
  202. static struct imx_pm_domain imx_gpc_domains[] = {
  203. {
  204. .base = {
  205. .name = "ARM",
  206. },
  207. }, {
  208. .base = {
  209. .name = "PU",
  210. .power_off = imx6_pm_domain_power_off,
  211. .power_on = imx6_pm_domain_power_on,
  212. .states = &imx6_pm_domain_pu_state,
  213. .state_count = 1,
  214. },
  215. .reg_offs = 0x260,
  216. .cntr_pdn_bit = 0,
  217. }, {
  218. .base = {
  219. .name = "DISPLAY",
  220. .power_off = imx6_pm_domain_power_off,
  221. .power_on = imx6_pm_domain_power_on,
  222. },
  223. .reg_offs = 0x240,
  224. .cntr_pdn_bit = 4,
  225. }, {
  226. .base = {
  227. .name = "PCI",
  228. .power_off = imx6_pm_domain_power_off,
  229. .power_on = imx6_pm_domain_power_on,
  230. },
  231. .reg_offs = 0x200,
  232. .cntr_pdn_bit = 6,
  233. },
  234. };
  235. struct imx_gpc_dt_data {
  236. int num_domains;
  237. bool err009619_present;
  238. };
  239. static const struct imx_gpc_dt_data imx6q_dt_data = {
  240. .num_domains = 2,
  241. .err009619_present = false,
  242. };
  243. static const struct imx_gpc_dt_data imx6qp_dt_data = {
  244. .num_domains = 2,
  245. .err009619_present = true,
  246. };
  247. static const struct imx_gpc_dt_data imx6sl_dt_data = {
  248. .num_domains = 3,
  249. .err009619_present = false,
  250. };
  251. static const struct imx_gpc_dt_data imx6sx_dt_data = {
  252. .num_domains = 4,
  253. .err009619_present = false,
  254. };
  255. static const struct of_device_id imx_gpc_dt_ids[] = {
  256. { .compatible = "fsl,imx6q-gpc", .data = &imx6q_dt_data },
  257. { .compatible = "fsl,imx6qp-gpc", .data = &imx6qp_dt_data },
  258. { .compatible = "fsl,imx6sl-gpc", .data = &imx6sl_dt_data },
  259. { .compatible = "fsl,imx6sx-gpc", .data = &imx6sx_dt_data },
  260. { }
  261. };
  262. static const struct regmap_config imx_gpc_regmap_config = {
  263. .reg_bits = 32,
  264. .val_bits = 32,
  265. .reg_stride = 4,
  266. .max_register = 0x2ac,
  267. };
  268. static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
  269. &imx_gpc_domains[0].base,
  270. &imx_gpc_domains[1].base,
  271. };
  272. static struct genpd_onecell_data imx_gpc_onecell_data = {
  273. .domains = imx_gpc_onecell_domains,
  274. .num_domains = 2,
  275. };
  276. static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
  277. unsigned int num_domains)
  278. {
  279. struct imx_pm_domain *domain;
  280. int i, ret;
  281. for (i = 0; i < num_domains; i++) {
  282. domain = &imx_gpc_domains[i];
  283. domain->regmap = regmap;
  284. domain->ipg_rate_mhz = 66;
  285. if (i == 1) {
  286. domain->supply = devm_regulator_get(dev, "pu");
  287. if (IS_ERR(domain->supply))
  288. return PTR_ERR(domain->supply);
  289. ret = imx_pgc_get_clocks(dev, domain);
  290. if (ret)
  291. goto clk_err;
  292. domain->base.power_on(&domain->base);
  293. }
  294. }
  295. for (i = 0; i < num_domains; i++)
  296. pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
  297. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  298. ret = of_genpd_add_provider_onecell(dev->of_node,
  299. &imx_gpc_onecell_data);
  300. if (ret)
  301. goto genpd_err;
  302. }
  303. return 0;
  304. genpd_err:
  305. for (i = 0; i < num_domains; i++)
  306. pm_genpd_remove(&imx_gpc_domains[i].base);
  307. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  308. clk_err:
  309. return ret;
  310. }
  311. static int imx_gpc_probe(struct platform_device *pdev)
  312. {
  313. const struct of_device_id *of_id =
  314. of_match_device(imx_gpc_dt_ids, &pdev->dev);
  315. const struct imx_gpc_dt_data *of_id_data = of_id->data;
  316. struct device_node *pgc_node;
  317. struct regmap *regmap;
  318. struct resource *res;
  319. void __iomem *base;
  320. int ret;
  321. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  322. /* bail out if DT too old and doesn't provide the necessary info */
  323. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  324. !pgc_node)
  325. return 0;
  326. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. base = devm_ioremap_resource(&pdev->dev, res);
  328. if (IS_ERR(base))
  329. return PTR_ERR(base);
  330. regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  331. &imx_gpc_regmap_config);
  332. if (IS_ERR(regmap)) {
  333. ret = PTR_ERR(regmap);
  334. dev_err(&pdev->dev, "failed to init regmap: %d\n",
  335. ret);
  336. return ret;
  337. }
  338. /* Disable PU power down in normal operation if ERR009619 is present */
  339. if (of_id_data->err009619_present)
  340. imx_gpc_domains[GPC_PGC_DOMAIN_PU].flags |=
  341. PGC_DOMAIN_FLAG_NO_PD;
  342. if (!pgc_node) {
  343. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  344. of_id_data->num_domains);
  345. if (ret)
  346. return ret;
  347. } else {
  348. struct imx_pm_domain *domain;
  349. struct platform_device *pd_pdev;
  350. struct device_node *np;
  351. struct clk *ipg_clk;
  352. unsigned int ipg_rate_mhz;
  353. int domain_index;
  354. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  355. if (IS_ERR(ipg_clk))
  356. return PTR_ERR(ipg_clk);
  357. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  358. for_each_child_of_node(pgc_node, np) {
  359. ret = of_property_read_u32(np, "reg", &domain_index);
  360. if (ret) {
  361. of_node_put(np);
  362. return ret;
  363. }
  364. if (domain_index >= of_id_data->num_domains)
  365. continue;
  366. domain = &imx_gpc_domains[domain_index];
  367. domain->regmap = regmap;
  368. domain->ipg_rate_mhz = ipg_rate_mhz;
  369. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  370. domain_index);
  371. if (!pd_pdev) {
  372. of_node_put(np);
  373. return -ENOMEM;
  374. }
  375. pd_pdev->dev.platform_data = domain;
  376. pd_pdev->dev.parent = &pdev->dev;
  377. pd_pdev->dev.of_node = np;
  378. ret = platform_device_add(pd_pdev);
  379. if (ret) {
  380. platform_device_put(pd_pdev);
  381. of_node_put(np);
  382. return ret;
  383. }
  384. }
  385. }
  386. return 0;
  387. }
  388. static int imx_gpc_remove(struct platform_device *pdev)
  389. {
  390. int ret;
  391. /*
  392. * If the old DT binding is used the toplevel driver needs to
  393. * de-register the power domains
  394. */
  395. if (!of_get_child_by_name(pdev->dev.of_node, "pgc")) {
  396. of_genpd_del_provider(pdev->dev.of_node);
  397. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
  398. if (ret)
  399. return ret;
  400. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  401. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  402. if (ret)
  403. return ret;
  404. }
  405. return 0;
  406. }
  407. static struct platform_driver imx_gpc_driver = {
  408. .driver = {
  409. .name = "imx-gpc",
  410. .of_match_table = imx_gpc_dt_ids,
  411. },
  412. .probe = imx_gpc_probe,
  413. .remove = imx_gpc_remove,
  414. };
  415. builtin_platform_driver(imx_gpc_driver)