rockchip-io-domain.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Rockchip IO Voltage Domain driver
  3. *
  4. * Copyright 2014 MundoReader S.L.
  5. * Copyright 2014 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/err.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/regulator/consumer.h>
  24. #define MAX_SUPPLIES 16
  25. /*
  26. * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
  27. * "Recommended Operating Conditions" for "Digital GPIO". When the typical
  28. * is 3.3V the max is 3.6V. When the typical is 1.8V the max is 1.98V.
  29. *
  30. * They are used like this:
  31. * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
  32. * SoC we're at 3.3.
  33. * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
  34. * that to be an error.
  35. */
  36. #define MAX_VOLTAGE_1_8 1980000
  37. #define MAX_VOLTAGE_3_3 3600000
  38. #define RK3288_SOC_CON2 0x24c
  39. #define RK3288_SOC_CON2_FLASH0 BIT(7)
  40. #define RK3288_SOC_FLASH_SUPPLY_NUM 2
  41. #define RK3368_SOC_CON15 0x43c
  42. #define RK3368_SOC_CON15_FLASH0 BIT(14)
  43. #define RK3368_SOC_FLASH_SUPPLY_NUM 2
  44. #define RK3399_PMUGRF_CON0 0x180
  45. #define RK3399_PMUGRF_CON0_VSEL BIT(8)
  46. #define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
  47. struct rockchip_iodomain;
  48. /**
  49. * @supplies: voltage settings matching the register bits.
  50. */
  51. struct rockchip_iodomain_soc_data {
  52. int grf_offset;
  53. const char *supply_names[MAX_SUPPLIES];
  54. void (*init)(struct rockchip_iodomain *iod);
  55. };
  56. struct rockchip_iodomain_supply {
  57. struct rockchip_iodomain *iod;
  58. struct regulator *reg;
  59. struct notifier_block nb;
  60. int idx;
  61. };
  62. struct rockchip_iodomain {
  63. struct device *dev;
  64. struct regmap *grf;
  65. struct rockchip_iodomain_soc_data *soc_data;
  66. struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
  67. };
  68. static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
  69. int uV)
  70. {
  71. struct rockchip_iodomain *iod = supply->iod;
  72. u32 val;
  73. int ret;
  74. /* set value bit */
  75. val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
  76. val <<= supply->idx;
  77. /* apply hiword-mask */
  78. val |= (BIT(supply->idx) << 16);
  79. ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
  80. if (ret)
  81. dev_err(iod->dev, "Couldn't write to GRF\n");
  82. return ret;
  83. }
  84. static int rockchip_iodomain_notify(struct notifier_block *nb,
  85. unsigned long event,
  86. void *data)
  87. {
  88. struct rockchip_iodomain_supply *supply =
  89. container_of(nb, struct rockchip_iodomain_supply, nb);
  90. int uV;
  91. int ret;
  92. /*
  93. * According to Rockchip it's important to keep the SoC IO domain
  94. * higher than (or equal to) the external voltage. That means we need
  95. * to change it before external voltage changes happen in the case
  96. * of an increase.
  97. *
  98. * Note that in the "pre" change we pick the max possible voltage that
  99. * the regulator might end up at (the client requests a range and we
  100. * don't know for certain the exact voltage). Right now we rely on the
  101. * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
  102. * request something like a max of 3.6V when they really want 3.3V.
  103. * We could attempt to come up with better rules if this fails.
  104. */
  105. if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
  106. struct pre_voltage_change_data *pvc_data = data;
  107. uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
  108. } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
  109. REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
  110. uV = (unsigned long)data;
  111. } else {
  112. return NOTIFY_OK;
  113. }
  114. dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
  115. if (uV > MAX_VOLTAGE_3_3) {
  116. dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
  117. if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  118. return NOTIFY_BAD;
  119. }
  120. ret = rockchip_iodomain_write(supply, uV);
  121. if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  122. return NOTIFY_BAD;
  123. dev_info(supply->iod->dev, "Setting to %d done\n", uV);
  124. return NOTIFY_OK;
  125. }
  126. static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
  127. {
  128. int ret;
  129. u32 val;
  130. /* if no flash supply we should leave things alone */
  131. if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
  132. return;
  133. /*
  134. * set flash0 iodomain to also use this framework
  135. * instead of a special gpio.
  136. */
  137. val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
  138. ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
  139. if (ret < 0)
  140. dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
  141. }
  142. static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
  143. {
  144. int ret;
  145. u32 val;
  146. /* if no flash supply we should leave things alone */
  147. if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
  148. return;
  149. /*
  150. * set flash0 iodomain to also use this framework
  151. * instead of a special gpio.
  152. */
  153. val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
  154. ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
  155. if (ret < 0)
  156. dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
  157. }
  158. static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
  159. {
  160. int ret;
  161. u32 val;
  162. /* if no pmu io supply we should leave things alone */
  163. if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
  164. return;
  165. /*
  166. * set pmu io iodomain to also use this framework
  167. * instead of a special gpio.
  168. */
  169. val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
  170. ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
  171. if (ret < 0)
  172. dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
  173. }
  174. /*
  175. * On the rk3188 the io-domains are handled by a shared register with the
  176. * lower 8 bits being still being continuing drive-strength settings.
  177. */
  178. static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
  179. .grf_offset = 0x104,
  180. .supply_names = {
  181. NULL,
  182. NULL,
  183. NULL,
  184. NULL,
  185. NULL,
  186. NULL,
  187. NULL,
  188. NULL,
  189. "ap0",
  190. "ap1",
  191. "cif",
  192. "flash",
  193. "vccio0",
  194. "vccio1",
  195. "lcdc0",
  196. "lcdc1",
  197. },
  198. };
  199. static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
  200. .grf_offset = 0x380,
  201. .supply_names = {
  202. "lcdc", /* LCDC_VDD */
  203. "dvp", /* DVPIO_VDD */
  204. "flash0", /* FLASH0_VDD (emmc) */
  205. "flash1", /* FLASH1_VDD (sdio1) */
  206. "wifi", /* APIO3_VDD (sdio0) */
  207. "bb", /* APIO5_VDD */
  208. "audio", /* APIO4_VDD */
  209. "sdcard", /* SDMMC0_VDD (sdmmc) */
  210. "gpio30", /* APIO1_VDD */
  211. "gpio1830", /* APIO2_VDD */
  212. },
  213. .init = rk3288_iodomain_init,
  214. };
  215. static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
  216. .grf_offset = 0x900,
  217. .supply_names = {
  218. NULL, /* reserved */
  219. "dvp", /* DVPIO_VDD */
  220. "flash0", /* FLASH0_VDD (emmc) */
  221. "wifi", /* APIO2_VDD (sdio0) */
  222. NULL,
  223. "audio", /* APIO3_VDD */
  224. "sdcard", /* SDMMC0_VDD (sdmmc) */
  225. "gpio30", /* APIO1_VDD */
  226. "gpio1830", /* APIO4_VDD (gpujtag) */
  227. },
  228. .init = rk3368_iodomain_init,
  229. };
  230. static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
  231. .grf_offset = 0x100,
  232. .supply_names = {
  233. NULL,
  234. NULL,
  235. NULL,
  236. NULL,
  237. "pmu", /*PMU IO domain*/
  238. "vop", /*LCDC IO domain*/
  239. },
  240. };
  241. static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
  242. .grf_offset = 0xe640,
  243. .supply_names = {
  244. "bt656", /* APIO2_VDD */
  245. "audio", /* APIO5_VDD */
  246. "sdmmc", /* SDMMC0_VDD */
  247. "gpio1830", /* APIO4_VDD */
  248. },
  249. };
  250. static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
  251. .grf_offset = 0x180,
  252. .supply_names = {
  253. NULL,
  254. NULL,
  255. NULL,
  256. NULL,
  257. NULL,
  258. NULL,
  259. NULL,
  260. NULL,
  261. NULL,
  262. "pmu1830", /* PMUIO2_VDD */
  263. },
  264. .init = rk3399_pmu_iodomain_init,
  265. };
  266. static const struct of_device_id rockchip_iodomain_match[] = {
  267. {
  268. .compatible = "rockchip,rk3188-io-voltage-domain",
  269. .data = (void *)&soc_data_rk3188
  270. },
  271. {
  272. .compatible = "rockchip,rk3288-io-voltage-domain",
  273. .data = (void *)&soc_data_rk3288
  274. },
  275. {
  276. .compatible = "rockchip,rk3368-io-voltage-domain",
  277. .data = (void *)&soc_data_rk3368
  278. },
  279. {
  280. .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
  281. .data = (void *)&soc_data_rk3368_pmu
  282. },
  283. {
  284. .compatible = "rockchip,rk3399-io-voltage-domain",
  285. .data = (void *)&soc_data_rk3399
  286. },
  287. {
  288. .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
  289. .data = (void *)&soc_data_rk3399_pmu
  290. },
  291. { /* sentinel */ },
  292. };
  293. MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
  294. static int rockchip_iodomain_probe(struct platform_device *pdev)
  295. {
  296. struct device_node *np = pdev->dev.of_node;
  297. const struct of_device_id *match;
  298. struct rockchip_iodomain *iod;
  299. struct device *parent;
  300. int i, ret = 0;
  301. if (!np)
  302. return -ENODEV;
  303. iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
  304. if (!iod)
  305. return -ENOMEM;
  306. iod->dev = &pdev->dev;
  307. platform_set_drvdata(pdev, iod);
  308. match = of_match_node(rockchip_iodomain_match, np);
  309. iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
  310. parent = pdev->dev.parent;
  311. if (parent && parent->of_node) {
  312. iod->grf = syscon_node_to_regmap(parent->of_node);
  313. } else {
  314. dev_dbg(&pdev->dev, "falling back to old binding\n");
  315. iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  316. }
  317. if (IS_ERR(iod->grf)) {
  318. dev_err(&pdev->dev, "couldn't find grf regmap\n");
  319. return PTR_ERR(iod->grf);
  320. }
  321. for (i = 0; i < MAX_SUPPLIES; i++) {
  322. const char *supply_name = iod->soc_data->supply_names[i];
  323. struct rockchip_iodomain_supply *supply = &iod->supplies[i];
  324. struct regulator *reg;
  325. int uV;
  326. if (!supply_name)
  327. continue;
  328. reg = devm_regulator_get_optional(iod->dev, supply_name);
  329. if (IS_ERR(reg)) {
  330. ret = PTR_ERR(reg);
  331. /* If a supply wasn't specified, that's OK */
  332. if (ret == -ENODEV)
  333. continue;
  334. else if (ret != -EPROBE_DEFER)
  335. dev_err(iod->dev, "couldn't get regulator %s\n",
  336. supply_name);
  337. goto unreg_notify;
  338. }
  339. /* set initial correct value */
  340. uV = regulator_get_voltage(reg);
  341. /* must be a regulator we can get the voltage of */
  342. if (uV < 0) {
  343. dev_err(iod->dev, "Can't determine voltage: %s\n",
  344. supply_name);
  345. goto unreg_notify;
  346. }
  347. if (uV > MAX_VOLTAGE_3_3) {
  348. dev_crit(iod->dev,
  349. "%d uV is too high. May damage SoC!\n",
  350. uV);
  351. ret = -EINVAL;
  352. goto unreg_notify;
  353. }
  354. /* setup our supply */
  355. supply->idx = i;
  356. supply->iod = iod;
  357. supply->reg = reg;
  358. supply->nb.notifier_call = rockchip_iodomain_notify;
  359. ret = rockchip_iodomain_write(supply, uV);
  360. if (ret) {
  361. supply->reg = NULL;
  362. goto unreg_notify;
  363. }
  364. /* register regulator notifier */
  365. ret = regulator_register_notifier(reg, &supply->nb);
  366. if (ret) {
  367. dev_err(&pdev->dev,
  368. "regulator notifier request failed\n");
  369. supply->reg = NULL;
  370. goto unreg_notify;
  371. }
  372. }
  373. if (iod->soc_data->init)
  374. iod->soc_data->init(iod);
  375. return 0;
  376. unreg_notify:
  377. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  378. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  379. if (io_supply->reg)
  380. regulator_unregister_notifier(io_supply->reg,
  381. &io_supply->nb);
  382. }
  383. return ret;
  384. }
  385. static int rockchip_iodomain_remove(struct platform_device *pdev)
  386. {
  387. struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
  388. int i;
  389. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  390. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  391. if (io_supply->reg)
  392. regulator_unregister_notifier(io_supply->reg,
  393. &io_supply->nb);
  394. }
  395. return 0;
  396. }
  397. static struct platform_driver rockchip_iodomain_driver = {
  398. .probe = rockchip_iodomain_probe,
  399. .remove = rockchip_iodomain_remove,
  400. .driver = {
  401. .name = "rockchip-iodomain",
  402. .of_match_table = rockchip_iodomain_match,
  403. },
  404. };
  405. module_platform_driver(rockchip_iodomain_driver);
  406. MODULE_DESCRIPTION("Rockchip IO-domain driver");
  407. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  408. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  409. MODULE_LICENSE("GPL v2");