gpcv2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright 2017 Impinj, Inc
  3. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  4. *
  5. * Based on the code of analogus driver:
  6. *
  7. * Copyright 2015-2017 Pengutronix, Lucas Stach <kernel@pengutronix.de>
  8. *
  9. * The code contained herein is licensed under the GNU General Public
  10. * License. You may obtain a copy of the GNU General Public License
  11. * Version 2 or later at the following locations:
  12. *
  13. * http://www.opensource.org/licenses/gpl-license.html
  14. * http://www.gnu.org/copyleft/gpl.html
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_domain.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <dt-bindings/power/imx7-power.h>
  21. #define GPC_LPCR_A7_BSC 0x000
  22. #define GPC_PGC_CPU_MAPPING 0x0ec
  23. #define USB_HSIC_PHY_A7_DOMAIN BIT(6)
  24. #define USB_OTG2_PHY_A7_DOMAIN BIT(5)
  25. #define USB_OTG1_PHY_A7_DOMAIN BIT(4)
  26. #define PCIE_PHY_A7_DOMAIN BIT(3)
  27. #define MIPI_PHY_A7_DOMAIN BIT(2)
  28. #define GPC_PU_PGC_SW_PUP_REQ 0x0f8
  29. #define GPC_PU_PGC_SW_PDN_REQ 0x104
  30. #define USB_HSIC_PHY_SW_Pxx_REQ BIT(4)
  31. #define USB_OTG2_PHY_SW_Pxx_REQ BIT(3)
  32. #define USB_OTG1_PHY_SW_Pxx_REQ BIT(2)
  33. #define PCIE_PHY_SW_Pxx_REQ BIT(1)
  34. #define MIPI_PHY_SW_Pxx_REQ BIT(0)
  35. #define GPC_M4_PU_PDN_FLG 0x1bc
  36. /*
  37. * The PGC offset values in Reference Manual
  38. * (Rev. 1, 01/2018 and the older ones) GPC chapter's
  39. * GPC_PGC memory map are incorrect, below offset
  40. * values are from design RTL.
  41. */
  42. #define PGC_MIPI 16
  43. #define PGC_PCIE 17
  44. #define PGC_USB_HSIC 20
  45. #define GPC_PGC_CTRL(n) (0x800 + (n) * 0x40)
  46. #define GPC_PGC_SR(n) (GPC_PGC_CTRL(n) + 0xc)
  47. #define GPC_PGC_CTRL_PCR BIT(0)
  48. struct imx7_pgc_domain {
  49. struct generic_pm_domain genpd;
  50. struct regmap *regmap;
  51. struct regulator *regulator;
  52. unsigned int pgc;
  53. const struct {
  54. u32 pxx;
  55. u32 map;
  56. } bits;
  57. const int voltage;
  58. struct device *dev;
  59. };
  60. static int imx7_gpc_pu_pgc_sw_pxx_req(struct generic_pm_domain *genpd,
  61. bool on)
  62. {
  63. struct imx7_pgc_domain *domain = container_of(genpd,
  64. struct imx7_pgc_domain,
  65. genpd);
  66. unsigned int offset = on ?
  67. GPC_PU_PGC_SW_PUP_REQ : GPC_PU_PGC_SW_PDN_REQ;
  68. const bool enable_power_control = !on;
  69. const bool has_regulator = !IS_ERR(domain->regulator);
  70. unsigned long deadline;
  71. int ret = 0;
  72. regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
  73. domain->bits.map, domain->bits.map);
  74. if (has_regulator && on) {
  75. ret = regulator_enable(domain->regulator);
  76. if (ret) {
  77. dev_err(domain->dev, "failed to enable regulator\n");
  78. goto unmap;
  79. }
  80. }
  81. if (enable_power_control)
  82. regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
  83. GPC_PGC_CTRL_PCR, GPC_PGC_CTRL_PCR);
  84. regmap_update_bits(domain->regmap, offset,
  85. domain->bits.pxx, domain->bits.pxx);
  86. /*
  87. * As per "5.5.9.4 Example Code 4" in IMX7DRM.pdf wait
  88. * for PUP_REQ/PDN_REQ bit to be cleared
  89. */
  90. deadline = jiffies + msecs_to_jiffies(1);
  91. while (true) {
  92. u32 pxx_req;
  93. regmap_read(domain->regmap, offset, &pxx_req);
  94. if (!(pxx_req & domain->bits.pxx))
  95. break;
  96. if (time_after(jiffies, deadline)) {
  97. dev_err(domain->dev, "falied to command PGC\n");
  98. ret = -ETIMEDOUT;
  99. /*
  100. * If we were in a process of enabling a
  101. * domain and failed we might as well disable
  102. * the regulator we just enabled. And if it
  103. * was the opposite situation and we failed to
  104. * power down -- keep the regulator on
  105. */
  106. on = !on;
  107. break;
  108. }
  109. cpu_relax();
  110. }
  111. if (enable_power_control)
  112. regmap_update_bits(domain->regmap, GPC_PGC_CTRL(domain->pgc),
  113. GPC_PGC_CTRL_PCR, 0);
  114. if (has_regulator && !on) {
  115. int err;
  116. err = regulator_disable(domain->regulator);
  117. if (err)
  118. dev_err(domain->dev,
  119. "failed to disable regulator: %d\n", ret);
  120. /* Preserve earlier error code */
  121. ret = ret ?: err;
  122. }
  123. unmap:
  124. regmap_update_bits(domain->regmap, GPC_PGC_CPU_MAPPING,
  125. domain->bits.map, 0);
  126. return ret;
  127. }
  128. static int imx7_gpc_pu_pgc_sw_pup_req(struct generic_pm_domain *genpd)
  129. {
  130. return imx7_gpc_pu_pgc_sw_pxx_req(genpd, true);
  131. }
  132. static int imx7_gpc_pu_pgc_sw_pdn_req(struct generic_pm_domain *genpd)
  133. {
  134. return imx7_gpc_pu_pgc_sw_pxx_req(genpd, false);
  135. }
  136. static const struct imx7_pgc_domain imx7_pgc_domains[] = {
  137. [IMX7_POWER_DOMAIN_MIPI_PHY] = {
  138. .genpd = {
  139. .name = "mipi-phy",
  140. },
  141. .bits = {
  142. .pxx = MIPI_PHY_SW_Pxx_REQ,
  143. .map = MIPI_PHY_A7_DOMAIN,
  144. },
  145. .voltage = 1000000,
  146. .pgc = PGC_MIPI,
  147. },
  148. [IMX7_POWER_DOMAIN_PCIE_PHY] = {
  149. .genpd = {
  150. .name = "pcie-phy",
  151. },
  152. .bits = {
  153. .pxx = PCIE_PHY_SW_Pxx_REQ,
  154. .map = PCIE_PHY_A7_DOMAIN,
  155. },
  156. .voltage = 1000000,
  157. .pgc = PGC_PCIE,
  158. },
  159. [IMX7_POWER_DOMAIN_USB_HSIC_PHY] = {
  160. .genpd = {
  161. .name = "usb-hsic-phy",
  162. },
  163. .bits = {
  164. .pxx = USB_HSIC_PHY_SW_Pxx_REQ,
  165. .map = USB_HSIC_PHY_A7_DOMAIN,
  166. },
  167. .voltage = 1200000,
  168. .pgc = PGC_USB_HSIC,
  169. },
  170. };
  171. static int imx7_pgc_domain_probe(struct platform_device *pdev)
  172. {
  173. struct imx7_pgc_domain *domain = pdev->dev.platform_data;
  174. int ret;
  175. domain->dev = &pdev->dev;
  176. domain->regulator = devm_regulator_get_optional(domain->dev, "power");
  177. if (IS_ERR(domain->regulator)) {
  178. if (PTR_ERR(domain->regulator) != -ENODEV) {
  179. if (PTR_ERR(domain->regulator) != -EPROBE_DEFER)
  180. dev_err(domain->dev, "Failed to get domain's regulator\n");
  181. return PTR_ERR(domain->regulator);
  182. }
  183. } else {
  184. regulator_set_voltage(domain->regulator,
  185. domain->voltage, domain->voltage);
  186. }
  187. ret = pm_genpd_init(&domain->genpd, NULL, true);
  188. if (ret) {
  189. dev_err(domain->dev, "Failed to init power domain\n");
  190. return ret;
  191. }
  192. ret = of_genpd_add_provider_simple(domain->dev->of_node,
  193. &domain->genpd);
  194. if (ret) {
  195. dev_err(domain->dev, "Failed to add genpd provider\n");
  196. pm_genpd_remove(&domain->genpd);
  197. }
  198. return ret;
  199. }
  200. static int imx7_pgc_domain_remove(struct platform_device *pdev)
  201. {
  202. struct imx7_pgc_domain *domain = pdev->dev.platform_data;
  203. of_genpd_del_provider(domain->dev->of_node);
  204. pm_genpd_remove(&domain->genpd);
  205. return 0;
  206. }
  207. static const struct platform_device_id imx7_pgc_domain_id[] = {
  208. { "imx7-pgc-domain", },
  209. { },
  210. };
  211. static struct platform_driver imx7_pgc_domain_driver = {
  212. .driver = {
  213. .name = "imx7-pgc",
  214. },
  215. .probe = imx7_pgc_domain_probe,
  216. .remove = imx7_pgc_domain_remove,
  217. .id_table = imx7_pgc_domain_id,
  218. };
  219. builtin_platform_driver(imx7_pgc_domain_driver)
  220. static int imx_gpcv2_probe(struct platform_device *pdev)
  221. {
  222. static const struct regmap_range yes_ranges[] = {
  223. regmap_reg_range(GPC_LPCR_A7_BSC,
  224. GPC_M4_PU_PDN_FLG),
  225. regmap_reg_range(GPC_PGC_CTRL(PGC_MIPI),
  226. GPC_PGC_SR(PGC_MIPI)),
  227. regmap_reg_range(GPC_PGC_CTRL(PGC_PCIE),
  228. GPC_PGC_SR(PGC_PCIE)),
  229. regmap_reg_range(GPC_PGC_CTRL(PGC_USB_HSIC),
  230. GPC_PGC_SR(PGC_USB_HSIC)),
  231. };
  232. static const struct regmap_access_table access_table = {
  233. .yes_ranges = yes_ranges,
  234. .n_yes_ranges = ARRAY_SIZE(yes_ranges),
  235. };
  236. static const struct regmap_config regmap_config = {
  237. .reg_bits = 32,
  238. .val_bits = 32,
  239. .reg_stride = 4,
  240. .rd_table = &access_table,
  241. .wr_table = &access_table,
  242. .max_register = SZ_4K,
  243. };
  244. struct device *dev = &pdev->dev;
  245. struct device_node *pgc_np, *np;
  246. struct regmap *regmap;
  247. struct resource *res;
  248. void __iomem *base;
  249. int ret;
  250. pgc_np = of_get_child_by_name(dev->of_node, "pgc");
  251. if (!pgc_np) {
  252. dev_err(dev, "No power domains specified in DT\n");
  253. return -EINVAL;
  254. }
  255. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  256. base = devm_ioremap_resource(dev, res);
  257. if (IS_ERR(base))
  258. return PTR_ERR(base);
  259. regmap = devm_regmap_init_mmio(dev, base, &regmap_config);
  260. if (IS_ERR(regmap)) {
  261. ret = PTR_ERR(regmap);
  262. dev_err(dev, "failed to init regmap (%d)\n", ret);
  263. return ret;
  264. }
  265. for_each_child_of_node(pgc_np, np) {
  266. struct platform_device *pd_pdev;
  267. struct imx7_pgc_domain *domain;
  268. u32 domain_index;
  269. ret = of_property_read_u32(np, "reg", &domain_index);
  270. if (ret) {
  271. dev_err(dev, "Failed to read 'reg' property\n");
  272. of_node_put(np);
  273. return ret;
  274. }
  275. if (domain_index >= ARRAY_SIZE(imx7_pgc_domains)) {
  276. dev_warn(dev,
  277. "Domain index %d is out of bounds\n",
  278. domain_index);
  279. continue;
  280. }
  281. pd_pdev = platform_device_alloc("imx7-pgc-domain",
  282. domain_index);
  283. if (!pd_pdev) {
  284. dev_err(dev, "Failed to allocate platform device\n");
  285. of_node_put(np);
  286. return -ENOMEM;
  287. }
  288. ret = platform_device_add_data(pd_pdev,
  289. &imx7_pgc_domains[domain_index],
  290. sizeof(imx7_pgc_domains[domain_index]));
  291. if (ret) {
  292. platform_device_put(pd_pdev);
  293. of_node_put(np);
  294. return ret;
  295. }
  296. domain = pd_pdev->dev.platform_data;
  297. domain->regmap = regmap;
  298. domain->genpd.power_on = imx7_gpc_pu_pgc_sw_pup_req;
  299. domain->genpd.power_off = imx7_gpc_pu_pgc_sw_pdn_req;
  300. pd_pdev->dev.parent = dev;
  301. pd_pdev->dev.of_node = np;
  302. ret = platform_device_add(pd_pdev);
  303. if (ret) {
  304. platform_device_put(pd_pdev);
  305. of_node_put(np);
  306. return ret;
  307. }
  308. }
  309. return 0;
  310. }
  311. static const struct of_device_id imx_gpcv2_dt_ids[] = {
  312. { .compatible = "fsl,imx7d-gpc" },
  313. { }
  314. };
  315. static struct platform_driver imx_gpc_driver = {
  316. .driver = {
  317. .name = "imx-gpcv2",
  318. .of_match_table = imx_gpcv2_dt_ids,
  319. },
  320. .probe = imx_gpcv2_probe,
  321. };
  322. builtin_platform_driver(imx_gpc_driver)