gpc.c 13 KB

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