rockchip-io-domain.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. struct rockchip_iodomain;
  42. /**
  43. * @supplies: voltage settings matching the register bits.
  44. */
  45. struct rockchip_iodomain_soc_data {
  46. int grf_offset;
  47. const char *supply_names[MAX_SUPPLIES];
  48. void (*init)(struct rockchip_iodomain *iod);
  49. };
  50. struct rockchip_iodomain_supply {
  51. struct rockchip_iodomain *iod;
  52. struct regulator *reg;
  53. struct notifier_block nb;
  54. int idx;
  55. };
  56. struct rockchip_iodomain {
  57. struct device *dev;
  58. struct regmap *grf;
  59. struct rockchip_iodomain_soc_data *soc_data;
  60. struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
  61. };
  62. static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
  63. int uV)
  64. {
  65. struct rockchip_iodomain *iod = supply->iod;
  66. u32 val;
  67. int ret;
  68. /* set value bit */
  69. val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
  70. val <<= supply->idx;
  71. /* apply hiword-mask */
  72. val |= (BIT(supply->idx) << 16);
  73. ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
  74. if (ret)
  75. dev_err(iod->dev, "Couldn't write to GRF\n");
  76. return ret;
  77. }
  78. static int rockchip_iodomain_notify(struct notifier_block *nb,
  79. unsigned long event,
  80. void *data)
  81. {
  82. struct rockchip_iodomain_supply *supply =
  83. container_of(nb, struct rockchip_iodomain_supply, nb);
  84. int uV;
  85. int ret;
  86. /*
  87. * According to Rockchip it's important to keep the SoC IO domain
  88. * higher than (or equal to) the external voltage. That means we need
  89. * to change it before external voltage changes happen in the case
  90. * of an increase.
  91. *
  92. * Note that in the "pre" change we pick the max possible voltage that
  93. * the regulator might end up at (the client requests a range and we
  94. * don't know for certain the exact voltage). Right now we rely on the
  95. * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
  96. * request something like a max of 3.6V when they really want 3.3V.
  97. * We could attempt to come up with better rules if this fails.
  98. */
  99. if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
  100. struct pre_voltage_change_data *pvc_data = data;
  101. uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
  102. } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
  103. REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
  104. uV = (unsigned long)data;
  105. } else {
  106. return NOTIFY_OK;
  107. }
  108. dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
  109. if (uV > MAX_VOLTAGE_3_3) {
  110. dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
  111. if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  112. return NOTIFY_BAD;
  113. }
  114. ret = rockchip_iodomain_write(supply, uV);
  115. if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
  116. return NOTIFY_BAD;
  117. dev_info(supply->iod->dev, "Setting to %d done\n", uV);
  118. return NOTIFY_OK;
  119. }
  120. static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
  121. {
  122. int ret;
  123. u32 val;
  124. /* if no flash supply we should leave things alone */
  125. if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
  126. return;
  127. /*
  128. * set flash0 iodomain to also use this framework
  129. * instead of a special gpio.
  130. */
  131. val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
  132. ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
  133. if (ret < 0)
  134. dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
  135. }
  136. /*
  137. * On the rk3188 the io-domains are handled by a shared register with the
  138. * lower 8 bits being still being continuing drive-strength settings.
  139. */
  140. static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
  141. .grf_offset = 0x104,
  142. .supply_names = {
  143. NULL,
  144. NULL,
  145. NULL,
  146. NULL,
  147. NULL,
  148. NULL,
  149. NULL,
  150. NULL,
  151. "ap0",
  152. "ap1",
  153. "cif",
  154. "flash",
  155. "vccio0",
  156. "vccio1",
  157. "lcdc0",
  158. "lcdc1",
  159. },
  160. };
  161. static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
  162. .grf_offset = 0x380,
  163. .supply_names = {
  164. "lcdc", /* LCDC_VDD */
  165. "dvp", /* DVPIO_VDD */
  166. "flash0", /* FLASH0_VDD (emmc) */
  167. "flash1", /* FLASH1_VDD (sdio1) */
  168. "wifi", /* APIO3_VDD (sdio0) */
  169. "bb", /* APIO5_VDD */
  170. "audio", /* APIO4_VDD */
  171. "sdcard", /* SDMMC0_VDD (sdmmc) */
  172. "gpio30", /* APIO1_VDD */
  173. "gpio1830", /* APIO2_VDD */
  174. },
  175. .init = rk3288_iodomain_init,
  176. };
  177. static const struct of_device_id rockchip_iodomain_match[] = {
  178. {
  179. .compatible = "rockchip,rk3188-io-voltage-domain",
  180. .data = (void *)&soc_data_rk3188
  181. },
  182. {
  183. .compatible = "rockchip,rk3288-io-voltage-domain",
  184. .data = (void *)&soc_data_rk3288
  185. },
  186. { /* sentinel */ },
  187. };
  188. static int rockchip_iodomain_probe(struct platform_device *pdev)
  189. {
  190. struct device_node *np = pdev->dev.of_node;
  191. const struct of_device_id *match;
  192. struct rockchip_iodomain *iod;
  193. int i, ret = 0;
  194. if (!np)
  195. return -ENODEV;
  196. iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
  197. if (!iod)
  198. return -ENOMEM;
  199. iod->dev = &pdev->dev;
  200. platform_set_drvdata(pdev, iod);
  201. match = of_match_node(rockchip_iodomain_match, np);
  202. iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
  203. iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  204. if (IS_ERR(iod->grf)) {
  205. dev_err(&pdev->dev, "couldn't find grf regmap\n");
  206. return PTR_ERR(iod->grf);
  207. }
  208. for (i = 0; i < MAX_SUPPLIES; i++) {
  209. const char *supply_name = iod->soc_data->supply_names[i];
  210. struct rockchip_iodomain_supply *supply = &iod->supplies[i];
  211. struct regulator *reg;
  212. int uV;
  213. if (!supply_name)
  214. continue;
  215. reg = devm_regulator_get_optional(iod->dev, supply_name);
  216. if (IS_ERR(reg)) {
  217. ret = PTR_ERR(reg);
  218. /* If a supply wasn't specified, that's OK */
  219. if (ret == -ENODEV)
  220. continue;
  221. else if (ret != -EPROBE_DEFER)
  222. dev_err(iod->dev, "couldn't get regulator %s\n",
  223. supply_name);
  224. goto unreg_notify;
  225. }
  226. /* set initial correct value */
  227. uV = regulator_get_voltage(reg);
  228. /* must be a regulator we can get the voltage of */
  229. if (uV < 0) {
  230. dev_err(iod->dev, "Can't determine voltage: %s\n",
  231. supply_name);
  232. goto unreg_notify;
  233. }
  234. if (uV > MAX_VOLTAGE_3_3) {
  235. dev_crit(iod->dev,
  236. "%d uV is too high. May damage SoC!\n",
  237. uV);
  238. ret = -EINVAL;
  239. goto unreg_notify;
  240. }
  241. /* setup our supply */
  242. supply->idx = i;
  243. supply->iod = iod;
  244. supply->reg = reg;
  245. supply->nb.notifier_call = rockchip_iodomain_notify;
  246. ret = rockchip_iodomain_write(supply, uV);
  247. if (ret) {
  248. supply->reg = NULL;
  249. goto unreg_notify;
  250. }
  251. /* register regulator notifier */
  252. ret = regulator_register_notifier(reg, &supply->nb);
  253. if (ret) {
  254. dev_err(&pdev->dev,
  255. "regulator notifier request failed\n");
  256. supply->reg = NULL;
  257. goto unreg_notify;
  258. }
  259. }
  260. if (iod->soc_data->init)
  261. iod->soc_data->init(iod);
  262. return 0;
  263. unreg_notify:
  264. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  265. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  266. if (io_supply->reg)
  267. regulator_unregister_notifier(io_supply->reg,
  268. &io_supply->nb);
  269. }
  270. return ret;
  271. }
  272. static int rockchip_iodomain_remove(struct platform_device *pdev)
  273. {
  274. struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
  275. int i;
  276. for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
  277. struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
  278. if (io_supply->reg)
  279. regulator_unregister_notifier(io_supply->reg,
  280. &io_supply->nb);
  281. }
  282. return 0;
  283. }
  284. static struct platform_driver rockchip_iodomain_driver = {
  285. .probe = rockchip_iodomain_probe,
  286. .remove = rockchip_iodomain_remove,
  287. .driver = {
  288. .name = "rockchip-iodomain",
  289. .of_match_table = rockchip_iodomain_match,
  290. },
  291. };
  292. module_platform_driver(rockchip_iodomain_driver);
  293. MODULE_DESCRIPTION("Rockchip IO-domain driver");
  294. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  295. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  296. MODULE_LICENSE("GPL v2");