rockchip-io-domain.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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_rk3288 = {
  219. .grf_offset = 0x380,
  220. .supply_names = {
  221. "lcdc", /* LCDC_VDD */
  222. "dvp", /* DVPIO_VDD */
  223. "flash0", /* FLASH0_VDD (emmc) */
  224. "flash1", /* FLASH1_VDD (sdio1) */
  225. "wifi", /* APIO3_VDD (sdio0) */
  226. "bb", /* APIO5_VDD */
  227. "audio", /* APIO4_VDD */
  228. "sdcard", /* SDMMC0_VDD (sdmmc) */
  229. "gpio30", /* APIO1_VDD */
  230. "gpio1830", /* APIO2_VDD */
  231. },
  232. .init = rk3288_iodomain_init,
  233. };
  234. static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
  235. .grf_offset = 0x410,
  236. .supply_names = {
  237. "vccio1",
  238. "vccio2",
  239. "vccio3",
  240. "vccio4",
  241. "vccio5",
  242. "vccio6",
  243. "pmuio",
  244. },
  245. .init = rk3328_iodomain_init,
  246. };
  247. static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
  248. .grf_offset = 0x900,
  249. .supply_names = {
  250. NULL, /* reserved */
  251. "dvp", /* DVPIO_VDD */
  252. "flash0", /* FLASH0_VDD (emmc) */
  253. "wifi", /* APIO2_VDD (sdio0) */
  254. NULL,
  255. "audio", /* APIO3_VDD */
  256. "sdcard", /* SDMMC0_VDD (sdmmc) */
  257. "gpio30", /* APIO1_VDD */
  258. "gpio1830", /* APIO4_VDD (gpujtag) */
  259. },
  260. .init = rk3368_iodomain_init,
  261. };
  262. static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
  263. .grf_offset = 0x100,
  264. .supply_names = {
  265. NULL,
  266. NULL,
  267. NULL,
  268. NULL,
  269. "pmu", /*PMU IO domain*/
  270. "vop", /*LCDC IO domain*/
  271. },
  272. };
  273. static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
  274. .grf_offset = 0xe640,
  275. .supply_names = {
  276. "bt656", /* APIO2_VDD */
  277. "audio", /* APIO5_VDD */
  278. "sdmmc", /* SDMMC0_VDD */
  279. "gpio1830", /* APIO4_VDD */
  280. },
  281. };
  282. static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
  283. .grf_offset = 0x180,
  284. .supply_names = {
  285. NULL,
  286. NULL,
  287. NULL,
  288. NULL,
  289. NULL,
  290. NULL,
  291. NULL,
  292. NULL,
  293. NULL,
  294. "pmu1830", /* PMUIO2_VDD */
  295. },
  296. .init = rk3399_pmu_iodomain_init,
  297. };
  298. static const struct of_device_id rockchip_iodomain_match[] = {
  299. {
  300. .compatible = "rockchip,rk3188-io-voltage-domain",
  301. .data = (void *)&soc_data_rk3188
  302. },
  303. {
  304. .compatible = "rockchip,rk3288-io-voltage-domain",
  305. .data = (void *)&soc_data_rk3288
  306. },
  307. {
  308. .compatible = "rockchip,rk3328-io-voltage-domain",
  309. .data = (void *)&soc_data_rk3328
  310. },
  311. {
  312. .compatible = "rockchip,rk3368-io-voltage-domain",
  313. .data = (void *)&soc_data_rk3368
  314. },
  315. {
  316. .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
  317. .data = (void *)&soc_data_rk3368_pmu
  318. },
  319. {
  320. .compatible = "rockchip,rk3399-io-voltage-domain",
  321. .data = (void *)&soc_data_rk3399
  322. },
  323. {
  324. .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
  325. .data = (void *)&soc_data_rk3399_pmu
  326. },
  327. { /* sentinel */ },
  328. };
  329. MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
  330. static int rockchip_iodomain_probe(struct platform_device *pdev)
  331. {
  332. struct device_node *np = pdev->dev.of_node;
  333. const struct of_device_id *match;
  334. struct rockchip_iodomain *iod;
  335. struct device *parent;
  336. int i, ret = 0;
  337. if (!np)
  338. return -ENODEV;
  339. iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
  340. if (!iod)
  341. return -ENOMEM;
  342. iod->dev = &pdev->dev;
  343. platform_set_drvdata(pdev, iod);
  344. match = of_match_node(rockchip_iodomain_match, np);
  345. iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
  346. parent = pdev->dev.parent;
  347. if (parent && parent->of_node) {
  348. iod->grf = syscon_node_to_regmap(parent->of_node);
  349. } else {
  350. dev_dbg(&pdev->dev, "falling back to old binding\n");
  351. iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  352. }
  353. if (IS_ERR(iod->grf)) {
  354. dev_err(&pdev->dev, "couldn't find grf regmap\n");
  355. return PTR_ERR(iod->grf);
  356. }
  357. for (i = 0; i < MAX_SUPPLIES; i++) {
  358. const char *supply_name = iod->soc_data->supply_names[i];
  359. struct rockchip_iodomain_supply *supply = &iod->supplies[i];
  360. struct regulator *reg;
  361. int uV;
  362. if (!supply_name)
  363. continue;
  364. reg = devm_regulator_get_optional(iod->dev, supply_name);
  365. if (IS_ERR(reg)) {
  366. ret = PTR_ERR(reg);
  367. /* If a supply wasn't specified, that's OK */
  368. if (ret == -ENODEV)
  369. continue;
  370. else if (ret != -EPROBE_DEFER)
  371. dev_err(iod->dev, "couldn't get regulator %s\n",
  372. supply_name);
  373. goto unreg_notify;
  374. }
  375. /* set initial correct value */
  376. uV = regulator_get_voltage(reg);
  377. /* must be a regulator we can get the voltage of */
  378. if (uV < 0) {
  379. dev_err(iod->dev, "Can't determine voltage: %s\n",
  380. supply_name);
  381. goto unreg_notify;
  382. }
  383. if (uV > MAX_VOLTAGE_3_3) {
  384. dev_crit(iod->dev,
  385. "%d uV is too high. May damage SoC!\n",
  386. uV);
  387. ret = -EINVAL;
  388. goto unreg_notify;
  389. }
  390. /* setup our supply */
  391. supply->idx = i;
  392. supply->iod = iod;
  393. supply->reg = reg;
  394. supply->nb.notifier_call = rockchip_iodomain_notify;
  395. ret = rockchip_iodomain_write(supply, uV);
  396. if (ret) {
  397. supply->reg = NULL;
  398. goto unreg_notify;
  399. }
  400. /* register regulator notifier */
  401. ret = regulator_register_notifier(reg, &supply->nb);
  402. if (ret) {
  403. dev_err(&pdev->dev,
  404. "regulator notifier request failed\n");
  405. supply->reg = NULL;
  406. goto unreg_notify;
  407. }
  408. }
  409. if (iod->soc_data->init)
  410. iod->soc_data->init(iod);
  411. return 0;
  412. unreg_notify:
  413. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  414. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  415. if (io_supply->reg)
  416. regulator_unregister_notifier(io_supply->reg,
  417. &io_supply->nb);
  418. }
  419. return ret;
  420. }
  421. static int rockchip_iodomain_remove(struct platform_device *pdev)
  422. {
  423. struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
  424. int i;
  425. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  426. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  427. if (io_supply->reg)
  428. regulator_unregister_notifier(io_supply->reg,
  429. &io_supply->nb);
  430. }
  431. return 0;
  432. }
  433. static struct platform_driver rockchip_iodomain_driver = {
  434. .probe = rockchip_iodomain_probe,
  435. .remove = rockchip_iodomain_remove,
  436. .driver = {
  437. .name = "rockchip-iodomain",
  438. .of_match_table = rockchip_iodomain_match,
  439. },
  440. };
  441. module_platform_driver(rockchip_iodomain_driver);
  442. MODULE_DESCRIPTION("Rockchip IO-domain driver");
  443. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  444. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  445. MODULE_LICENSE("GPL v2");