rockchip-io-domain.c 15 KB

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