gpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. .flags = GENPD_FLAG_ALWAYS_ON,
  207. },
  208. }, {
  209. .base = {
  210. .name = "PU",
  211. .power_off = imx6_pm_domain_power_off,
  212. .power_on = imx6_pm_domain_power_on,
  213. .states = &imx6_pm_domain_pu_state,
  214. .state_count = 1,
  215. },
  216. .reg_offs = 0x260,
  217. .cntr_pdn_bit = 0,
  218. }, {
  219. .base = {
  220. .name = "DISPLAY",
  221. .power_off = imx6_pm_domain_power_off,
  222. .power_on = imx6_pm_domain_power_on,
  223. },
  224. .reg_offs = 0x240,
  225. .cntr_pdn_bit = 4,
  226. }, {
  227. .base = {
  228. .name = "PCI",
  229. .power_off = imx6_pm_domain_power_off,
  230. .power_on = imx6_pm_domain_power_on,
  231. },
  232. .reg_offs = 0x200,
  233. .cntr_pdn_bit = 6,
  234. },
  235. };
  236. struct imx_gpc_dt_data {
  237. int num_domains;
  238. bool err009619_present;
  239. };
  240. static const struct imx_gpc_dt_data imx6q_dt_data = {
  241. .num_domains = 2,
  242. .err009619_present = false,
  243. };
  244. static const struct imx_gpc_dt_data imx6qp_dt_data = {
  245. .num_domains = 2,
  246. .err009619_present = true,
  247. };
  248. static const struct imx_gpc_dt_data imx6sl_dt_data = {
  249. .num_domains = 3,
  250. .err009619_present = false,
  251. };
  252. static const struct imx_gpc_dt_data imx6sx_dt_data = {
  253. .num_domains = 4,
  254. .err009619_present = false,
  255. };
  256. static const struct of_device_id imx_gpc_dt_ids[] = {
  257. { .compatible = "fsl,imx6q-gpc", .data = &imx6q_dt_data },
  258. { .compatible = "fsl,imx6qp-gpc", .data = &imx6qp_dt_data },
  259. { .compatible = "fsl,imx6sl-gpc", .data = &imx6sl_dt_data },
  260. { .compatible = "fsl,imx6sx-gpc", .data = &imx6sx_dt_data },
  261. { }
  262. };
  263. static const struct regmap_config imx_gpc_regmap_config = {
  264. .reg_bits = 32,
  265. .val_bits = 32,
  266. .reg_stride = 4,
  267. .max_register = 0x2ac,
  268. };
  269. static struct generic_pm_domain *imx_gpc_onecell_domains[] = {
  270. &imx_gpc_domains[0].base,
  271. &imx_gpc_domains[1].base,
  272. };
  273. static struct genpd_onecell_data imx_gpc_onecell_data = {
  274. .domains = imx_gpc_onecell_domains,
  275. .num_domains = 2,
  276. };
  277. static int imx_gpc_old_dt_init(struct device *dev, struct regmap *regmap,
  278. unsigned int num_domains)
  279. {
  280. struct imx_pm_domain *domain;
  281. int i, ret;
  282. for (i = 0; i < num_domains; i++) {
  283. domain = &imx_gpc_domains[i];
  284. domain->regmap = regmap;
  285. domain->ipg_rate_mhz = 66;
  286. if (i == 1) {
  287. domain->supply = devm_regulator_get(dev, "pu");
  288. if (IS_ERR(domain->supply))
  289. return PTR_ERR(domain->supply);
  290. ret = imx_pgc_get_clocks(dev, domain);
  291. if (ret)
  292. goto clk_err;
  293. domain->base.power_on(&domain->base);
  294. }
  295. }
  296. for (i = 0; i < num_domains; i++)
  297. pm_genpd_init(&imx_gpc_domains[i].base, NULL, false);
  298. if (IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
  299. ret = of_genpd_add_provider_onecell(dev->of_node,
  300. &imx_gpc_onecell_data);
  301. if (ret)
  302. goto genpd_err;
  303. }
  304. return 0;
  305. genpd_err:
  306. for (i = 0; i < num_domains; i++)
  307. pm_genpd_remove(&imx_gpc_domains[i].base);
  308. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  309. clk_err:
  310. return ret;
  311. }
  312. static int imx_gpc_probe(struct platform_device *pdev)
  313. {
  314. const struct of_device_id *of_id =
  315. of_match_device(imx_gpc_dt_ids, &pdev->dev);
  316. const struct imx_gpc_dt_data *of_id_data = of_id->data;
  317. struct device_node *pgc_node;
  318. struct regmap *regmap;
  319. struct resource *res;
  320. void __iomem *base;
  321. int ret;
  322. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  323. /* bail out if DT too old and doesn't provide the necessary info */
  324. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  325. !pgc_node)
  326. return 0;
  327. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  328. base = devm_ioremap_resource(&pdev->dev, res);
  329. if (IS_ERR(base))
  330. return PTR_ERR(base);
  331. regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  332. &imx_gpc_regmap_config);
  333. if (IS_ERR(regmap)) {
  334. ret = PTR_ERR(regmap);
  335. dev_err(&pdev->dev, "failed to init regmap: %d\n",
  336. ret);
  337. return ret;
  338. }
  339. /* Disable PU power down in normal operation if ERR009619 is present */
  340. if (of_id_data->err009619_present)
  341. imx_gpc_domains[GPC_PGC_DOMAIN_PU].flags |=
  342. PGC_DOMAIN_FLAG_NO_PD;
  343. if (!pgc_node) {
  344. ret = imx_gpc_old_dt_init(&pdev->dev, regmap,
  345. of_id_data->num_domains);
  346. if (ret)
  347. return ret;
  348. } else {
  349. struct imx_pm_domain *domain;
  350. struct platform_device *pd_pdev;
  351. struct device_node *np;
  352. struct clk *ipg_clk;
  353. unsigned int ipg_rate_mhz;
  354. int domain_index;
  355. ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  356. if (IS_ERR(ipg_clk))
  357. return PTR_ERR(ipg_clk);
  358. ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
  359. for_each_child_of_node(pgc_node, np) {
  360. ret = of_property_read_u32(np, "reg", &domain_index);
  361. if (ret) {
  362. of_node_put(np);
  363. return ret;
  364. }
  365. if (domain_index >= of_id_data->num_domains)
  366. continue;
  367. domain = &imx_gpc_domains[domain_index];
  368. domain->regmap = regmap;
  369. domain->ipg_rate_mhz = ipg_rate_mhz;
  370. pd_pdev = platform_device_alloc("imx-pgc-power-domain",
  371. domain_index);
  372. if (!pd_pdev) {
  373. of_node_put(np);
  374. return -ENOMEM;
  375. }
  376. pd_pdev->dev.platform_data = domain;
  377. pd_pdev->dev.parent = &pdev->dev;
  378. pd_pdev->dev.of_node = np;
  379. ret = platform_device_add(pd_pdev);
  380. if (ret) {
  381. platform_device_put(pd_pdev);
  382. of_node_put(np);
  383. return ret;
  384. }
  385. }
  386. }
  387. return 0;
  388. }
  389. static int imx_gpc_remove(struct platform_device *pdev)
  390. {
  391. struct device_node *pgc_node;
  392. int ret;
  393. pgc_node = of_get_child_by_name(pdev->dev.of_node, "pgc");
  394. /* bail out if DT too old and doesn't provide the necessary info */
  395. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells") &&
  396. !pgc_node)
  397. return 0;
  398. /*
  399. * If the old DT binding is used the toplevel driver needs to
  400. * de-register the power domains
  401. */
  402. if (!pgc_node) {
  403. of_genpd_del_provider(pdev->dev.of_node);
  404. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_PU].base);
  405. if (ret)
  406. return ret;
  407. imx_pgc_put_clocks(&imx_gpc_domains[GPC_PGC_DOMAIN_PU]);
  408. ret = pm_genpd_remove(&imx_gpc_domains[GPC_PGC_DOMAIN_ARM].base);
  409. if (ret)
  410. return ret;
  411. }
  412. return 0;
  413. }
  414. static struct platform_driver imx_gpc_driver = {
  415. .driver = {
  416. .name = "imx-gpc",
  417. .of_match_table = imx_gpc_dt_ids,
  418. },
  419. .probe = imx_gpc_probe,
  420. .remove = imx_gpc_remove,
  421. };
  422. builtin_platform_driver(imx_gpc_driver)