rockchip-io-domain.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 RK3328_SOC_CON4 0x410
  42. #define RK3328_SOC_CON4_VCCIO2 BIT(7)
  43. #define RK3328_SOC_VCCIO2_SUPPLY_NUM 1
  44. #define RK3368_SOC_CON15 0x43c
  45. #define RK3368_SOC_CON15_FLASH0 BIT(14)
  46. #define RK3368_SOC_FLASH_SUPPLY_NUM 2
  47. #define RK3399_PMUGRF_CON0 0x180
  48. #define RK3399_PMUGRF_CON0_VSEL BIT(8)
  49. #define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
  50. struct rockchip_iodomain;
  51. /**
  52. * @supplies: voltage settings matching the register bits.
  53. */
  54. struct rockchip_iodomain_soc_data {
  55. int grf_offset;
  56. const char *supply_names[MAX_SUPPLIES];
  57. void (*init)(struct rockchip_iodomain *iod);
  58. };
  59. struct rockchip_iodomain_supply {
  60. struct rockchip_iodomain *iod;
  61. struct regulator *reg;
  62. struct notifier_block nb;
  63. int idx;
  64. };
  65. struct rockchip_iodomain {
  66. struct device *dev;
  67. struct regmap *grf;
  68. struct rockchip_iodomain_soc_data *soc_data;
  69. struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
  70. };
  71. static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
  72. int uV)
  73. {
  74. struct rockchip_iodomain *iod = supply->iod;
  75. u32 val;
  76. int ret;
  77. /* set value bit */
  78. val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
  79. val <<= supply->idx;
  80. /* apply hiword-mask */
  81. val |= (BIT(supply->idx) << 16);
  82. ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
  83. if (ret)
  84. dev_err(iod->dev, "Couldn't write to GRF\n");
  85. return ret;
  86. }
  87. static int rockchip_iodomain_notify(struct notifier_block *nb,
  88. unsigned long event,
  89. void *data)
  90. {
  91. struct rockchip_iodomain_supply *supply =
  92. container_of(nb, struct rockchip_iodomain_supply, nb);
  93. int uV;
  94. int ret;
  95. /*
  96. * According to Rockchip it's important to keep the SoC IO domain
  97. * higher than (or equal to) the external voltage. That means we need
  98. * to change it before external voltage changes happen in the case
  99. * of an increase.
  100. *
  101. * Note that in the "pre" change we pick the max possible voltage that
  102. * the regulator might end up at (the client requests a range and we
  103. * don't know for certain the exact voltage). Right now we rely on the
  104. * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
  105. * request something like a max of 3.6V when they really want 3.3V.
  106. * We could attempt to come up with better rules if this fails.
  107. */
  108. if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
  109. struct pre_voltage_change_data *pvc_data = data;
  110. uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
  111. } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
  112. REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
  113. uV = (unsigned long)data;
  114. } else {
  115. return NOTIFY_OK;
  116. }
  117. dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
  118. if (uV > MAX_VOLTAGE_3_3) {
  119. dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
  120. if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  121. return NOTIFY_BAD;
  122. }
  123. ret = rockchip_iodomain_write(supply, uV);
  124. if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  125. return NOTIFY_BAD;
  126. dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
  127. return NOTIFY_OK;
  128. }
  129. static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
  130. {
  131. int ret;
  132. u32 val;
  133. /* if no flash supply we should leave things alone */
  134. if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
  135. return;
  136. /*
  137. * set flash0 iodomain to also use this framework
  138. * instead of a special gpio.
  139. */
  140. val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
  141. ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
  142. if (ret < 0)
  143. dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
  144. }
  145. static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
  146. {
  147. int ret;
  148. u32 val;
  149. /* if no vccio2 supply we should leave things alone */
  150. if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
  151. return;
  152. /*
  153. * set vccio2 iodomain to also use this framework
  154. * instead of a special gpio.
  155. */
  156. val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
  157. ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
  158. if (ret < 0)
  159. dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
  160. }
  161. static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
  162. {
  163. int ret;
  164. u32 val;
  165. /* if no flash supply we should leave things alone */
  166. if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
  167. return;
  168. /*
  169. * set flash0 iodomain to also use this framework
  170. * instead of a special gpio.
  171. */
  172. val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
  173. ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
  174. if (ret < 0)
  175. dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
  176. }
  177. static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
  178. {
  179. int ret;
  180. u32 val;
  181. /* if no pmu io supply we should leave things alone */
  182. if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
  183. return;
  184. /*
  185. * set pmu io iodomain to also use this framework
  186. * instead of a special gpio.
  187. */
  188. val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
  189. ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
  190. if (ret < 0)
  191. dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
  192. }
  193. /*
  194. * On the rk3188 the io-domains are handled by a shared register with the
  195. * lower 8 bits being still being continuing drive-strength settings.
  196. */
  197. static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
  198. .grf_offset = 0x104,
  199. .supply_names = {
  200. NULL,
  201. NULL,
  202. NULL,
  203. NULL,
  204. NULL,
  205. NULL,
  206. NULL,
  207. NULL,
  208. "ap0",
  209. "ap1",
  210. "cif",
  211. "flash",
  212. "vccio0",
  213. "vccio1",
  214. "lcdc0",
  215. "lcdc1",
  216. },
  217. };
  218. static const struct rockchip_iodomain_soc_data soc_data_rk3228 = {
  219. .grf_offset = 0x418,
  220. .supply_names = {
  221. "vccio1",
  222. "vccio2",
  223. "vccio3",
  224. "vccio4",
  225. },
  226. };
  227. static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
  228. .grf_offset = 0x380,
  229. .supply_names = {
  230. "lcdc", /* LCDC_VDD */
  231. "dvp", /* DVPIO_VDD */
  232. "flash0", /* FLASH0_VDD (emmc) */
  233. "flash1", /* FLASH1_VDD (sdio1) */
  234. "wifi", /* APIO3_VDD (sdio0) */
  235. "bb", /* APIO5_VDD */
  236. "audio", /* APIO4_VDD */
  237. "sdcard", /* SDMMC0_VDD (sdmmc) */
  238. "gpio30", /* APIO1_VDD */
  239. "gpio1830", /* APIO2_VDD */
  240. },
  241. .init = rk3288_iodomain_init,
  242. };
  243. static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
  244. .grf_offset = 0x410,
  245. .supply_names = {
  246. "vccio1",
  247. "vccio2",
  248. "vccio3",
  249. "vccio4",
  250. "vccio5",
  251. "vccio6",
  252. "pmuio",
  253. },
  254. .init = rk3328_iodomain_init,
  255. };
  256. static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
  257. .grf_offset = 0x900,
  258. .supply_names = {
  259. NULL, /* reserved */
  260. "dvp", /* DVPIO_VDD */
  261. "flash0", /* FLASH0_VDD (emmc) */
  262. "wifi", /* APIO2_VDD (sdio0) */
  263. NULL,
  264. "audio", /* APIO3_VDD */
  265. "sdcard", /* SDMMC0_VDD (sdmmc) */
  266. "gpio30", /* APIO1_VDD */
  267. "gpio1830", /* APIO4_VDD (gpujtag) */
  268. },
  269. .init = rk3368_iodomain_init,
  270. };
  271. static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
  272. .grf_offset = 0x100,
  273. .supply_names = {
  274. NULL,
  275. NULL,
  276. NULL,
  277. NULL,
  278. "pmu", /*PMU IO domain*/
  279. "vop", /*LCDC IO domain*/
  280. },
  281. };
  282. static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
  283. .grf_offset = 0xe640,
  284. .supply_names = {
  285. "bt656", /* APIO2_VDD */
  286. "audio", /* APIO5_VDD */
  287. "sdmmc", /* SDMMC0_VDD */
  288. "gpio1830", /* APIO4_VDD */
  289. },
  290. };
  291. static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
  292. .grf_offset = 0x180,
  293. .supply_names = {
  294. NULL,
  295. NULL,
  296. NULL,
  297. NULL,
  298. NULL,
  299. NULL,
  300. NULL,
  301. NULL,
  302. NULL,
  303. "pmu1830", /* PMUIO2_VDD */
  304. },
  305. .init = rk3399_pmu_iodomain_init,
  306. };
  307. static const struct rockchip_iodomain_soc_data soc_data_rv1108 = {
  308. .grf_offset = 0x404,
  309. .supply_names = {
  310. NULL,
  311. NULL,
  312. NULL,
  313. NULL,
  314. NULL,
  315. NULL,
  316. NULL,
  317. NULL,
  318. NULL,
  319. NULL,
  320. NULL,
  321. "vccio1",
  322. "vccio2",
  323. "vccio3",
  324. "vccio5",
  325. "vccio6",
  326. },
  327. };
  328. static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu = {
  329. .grf_offset = 0x104,
  330. .supply_names = {
  331. "pmu",
  332. },
  333. };
  334. static const struct of_device_id rockchip_iodomain_match[] = {
  335. {
  336. .compatible = "rockchip,rk3188-io-voltage-domain",
  337. .data = (void *)&soc_data_rk3188
  338. },
  339. {
  340. .compatible = "rockchip,rk3228-io-voltage-domain",
  341. .data = (void *)&soc_data_rk3228
  342. },
  343. {
  344. .compatible = "rockchip,rk3288-io-voltage-domain",
  345. .data = (void *)&soc_data_rk3288
  346. },
  347. {
  348. .compatible = "rockchip,rk3328-io-voltage-domain",
  349. .data = (void *)&soc_data_rk3328
  350. },
  351. {
  352. .compatible = "rockchip,rk3368-io-voltage-domain",
  353. .data = (void *)&soc_data_rk3368
  354. },
  355. {
  356. .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
  357. .data = (void *)&soc_data_rk3368_pmu
  358. },
  359. {
  360. .compatible = "rockchip,rk3399-io-voltage-domain",
  361. .data = (void *)&soc_data_rk3399
  362. },
  363. {
  364. .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
  365. .data = (void *)&soc_data_rk3399_pmu
  366. },
  367. {
  368. .compatible = "rockchip,rv1108-io-voltage-domain",
  369. .data = (void *)&soc_data_rv1108
  370. },
  371. {
  372. .compatible = "rockchip,rv1108-pmu-io-voltage-domain",
  373. .data = (void *)&soc_data_rv1108_pmu
  374. },
  375. { /* sentinel */ },
  376. };
  377. MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
  378. static int rockchip_iodomain_probe(struct platform_device *pdev)
  379. {
  380. struct device_node *np = pdev->dev.of_node;
  381. const struct of_device_id *match;
  382. struct rockchip_iodomain *iod;
  383. struct device *parent;
  384. int i, ret = 0;
  385. if (!np)
  386. return -ENODEV;
  387. iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
  388. if (!iod)
  389. return -ENOMEM;
  390. iod->dev = &pdev->dev;
  391. platform_set_drvdata(pdev, iod);
  392. match = of_match_node(rockchip_iodomain_match, np);
  393. iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
  394. parent = pdev->dev.parent;
  395. if (parent && parent->of_node) {
  396. iod->grf = syscon_node_to_regmap(parent->of_node);
  397. } else {
  398. dev_dbg(&pdev->dev, "falling back to old binding\n");
  399. iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  400. }
  401. if (IS_ERR(iod->grf)) {
  402. dev_err(&pdev->dev, "couldn't find grf regmap\n");
  403. return PTR_ERR(iod->grf);
  404. }
  405. for (i = 0; i < MAX_SUPPLIES; i++) {
  406. const char *supply_name = iod->soc_data->supply_names[i];
  407. struct rockchip_iodomain_supply *supply = &iod->supplies[i];
  408. struct regulator *reg;
  409. int uV;
  410. if (!supply_name)
  411. continue;
  412. reg = devm_regulator_get_optional(iod->dev, supply_name);
  413. if (IS_ERR(reg)) {
  414. ret = PTR_ERR(reg);
  415. /* If a supply wasn't specified, that's OK */
  416. if (ret == -ENODEV)
  417. continue;
  418. else if (ret != -EPROBE_DEFER)
  419. dev_err(iod->dev, "couldn't get regulator %s\n",
  420. supply_name);
  421. goto unreg_notify;
  422. }
  423. /* set initial correct value */
  424. uV = regulator_get_voltage(reg);
  425. /* must be a regulator we can get the voltage of */
  426. if (uV < 0) {
  427. dev_err(iod->dev, "Can't determine voltage: %s\n",
  428. supply_name);
  429. goto unreg_notify;
  430. }
  431. if (uV > MAX_VOLTAGE_3_3) {
  432. dev_crit(iod->dev,
  433. "%d uV is too high. May damage SoC!\n",
  434. uV);
  435. ret = -EINVAL;
  436. goto unreg_notify;
  437. }
  438. /* setup our supply */
  439. supply->idx = i;
  440. supply->iod = iod;
  441. supply->reg = reg;
  442. supply->nb.notifier_call = rockchip_iodomain_notify;
  443. ret = rockchip_iodomain_write(supply, uV);
  444. if (ret) {
  445. supply->reg = NULL;
  446. goto unreg_notify;
  447. }
  448. /* register regulator notifier */
  449. ret = regulator_register_notifier(reg, &supply->nb);
  450. if (ret) {
  451. dev_err(&pdev->dev,
  452. "regulator notifier request failed\n");
  453. supply->reg = NULL;
  454. goto unreg_notify;
  455. }
  456. }
  457. if (iod->soc_data->init)
  458. iod->soc_data->init(iod);
  459. return 0;
  460. unreg_notify:
  461. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  462. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  463. if (io_supply->reg)
  464. regulator_unregister_notifier(io_supply->reg,
  465. &io_supply->nb);
  466. }
  467. return ret;
  468. }
  469. static int rockchip_iodomain_remove(struct platform_device *pdev)
  470. {
  471. struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
  472. int i;
  473. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  474. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  475. if (io_supply->reg)
  476. regulator_unregister_notifier(io_supply->reg,
  477. &io_supply->nb);
  478. }
  479. return 0;
  480. }
  481. static struct platform_driver rockchip_iodomain_driver = {
  482. .probe = rockchip_iodomain_probe,
  483. .remove = rockchip_iodomain_remove,
  484. .driver = {
  485. .name = "rockchip-iodomain",
  486. .of_match_table = rockchip_iodomain_match,
  487. },
  488. };
  489. module_platform_driver(rockchip_iodomain_driver);
  490. MODULE_DESCRIPTION("Rockchip IO-domain driver");
  491. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  492. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  493. MODULE_LICENSE("GPL v2");