anatop-regulator.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3. */
  4. /*
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/regmap.h>
  27. #include <linux/regulator/driver.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regulator/machine.h>
  30. #define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */
  31. #define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */
  32. #define LDO_POWER_GATE 0x00
  33. #define LDO_FET_FULL_ON 0x1f
  34. struct anatop_regulator {
  35. const char *name;
  36. u32 control_reg;
  37. struct regmap *anatop;
  38. int vol_bit_shift;
  39. int vol_bit_width;
  40. u32 delay_reg;
  41. int delay_bit_shift;
  42. int delay_bit_width;
  43. int min_bit_val;
  44. int min_voltage;
  45. int max_voltage;
  46. struct regulator_desc rdesc;
  47. struct regulator_init_data *initdata;
  48. bool bypass;
  49. int sel;
  50. };
  51. static int anatop_regmap_set_voltage_time_sel(struct regulator_dev *reg,
  52. unsigned int old_sel,
  53. unsigned int new_sel)
  54. {
  55. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  56. u32 val;
  57. int ret = 0;
  58. /* check whether need to care about LDO ramp up speed */
  59. if (anatop_reg->delay_bit_width && new_sel > old_sel) {
  60. /*
  61. * the delay for LDO ramp up time is
  62. * based on the register setting, we need
  63. * to calculate how many steps LDO need to
  64. * ramp up, and how much delay needed. (us)
  65. */
  66. regmap_read(anatop_reg->anatop, anatop_reg->delay_reg, &val);
  67. val = (val >> anatop_reg->delay_bit_shift) &
  68. ((1 << anatop_reg->delay_bit_width) - 1);
  69. ret = (new_sel - old_sel) * (LDO_RAMP_UP_UNIT_IN_CYCLES <<
  70. val) / LDO_RAMP_UP_FREQ_IN_MHZ + 1;
  71. }
  72. return ret;
  73. }
  74. static int anatop_regmap_enable(struct regulator_dev *reg)
  75. {
  76. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  77. int sel;
  78. sel = anatop_reg->bypass ? LDO_FET_FULL_ON : anatop_reg->sel;
  79. return regulator_set_voltage_sel_regmap(reg, sel);
  80. }
  81. static int anatop_regmap_disable(struct regulator_dev *reg)
  82. {
  83. return regulator_set_voltage_sel_regmap(reg, LDO_POWER_GATE);
  84. }
  85. static int anatop_regmap_is_enabled(struct regulator_dev *reg)
  86. {
  87. return regulator_get_voltage_sel_regmap(reg) != LDO_POWER_GATE;
  88. }
  89. static int anatop_regmap_core_set_voltage_sel(struct regulator_dev *reg,
  90. unsigned selector)
  91. {
  92. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  93. int ret;
  94. if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg)) {
  95. anatop_reg->sel = selector;
  96. return 0;
  97. }
  98. ret = regulator_set_voltage_sel_regmap(reg, selector);
  99. if (!ret)
  100. anatop_reg->sel = selector;
  101. return ret;
  102. }
  103. static int anatop_regmap_core_get_voltage_sel(struct regulator_dev *reg)
  104. {
  105. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  106. if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg))
  107. return anatop_reg->sel;
  108. return regulator_get_voltage_sel_regmap(reg);
  109. }
  110. static int anatop_regmap_get_bypass(struct regulator_dev *reg, bool *enable)
  111. {
  112. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  113. int sel;
  114. sel = regulator_get_voltage_sel_regmap(reg);
  115. if (sel == LDO_FET_FULL_ON)
  116. WARN_ON(!anatop_reg->bypass);
  117. else if (sel != LDO_POWER_GATE)
  118. WARN_ON(anatop_reg->bypass);
  119. *enable = anatop_reg->bypass;
  120. return 0;
  121. }
  122. static int anatop_regmap_set_bypass(struct regulator_dev *reg, bool enable)
  123. {
  124. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  125. int sel;
  126. if (enable == anatop_reg->bypass)
  127. return 0;
  128. sel = enable ? LDO_FET_FULL_ON : anatop_reg->sel;
  129. anatop_reg->bypass = enable;
  130. return regulator_set_voltage_sel_regmap(reg, sel);
  131. }
  132. static struct regulator_ops anatop_rops = {
  133. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  134. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  135. .list_voltage = regulator_list_voltage_linear,
  136. .map_voltage = regulator_map_voltage_linear,
  137. };
  138. static struct regulator_ops anatop_core_rops = {
  139. .enable = anatop_regmap_enable,
  140. .disable = anatop_regmap_disable,
  141. .is_enabled = anatop_regmap_is_enabled,
  142. .set_voltage_sel = anatop_regmap_core_set_voltage_sel,
  143. .set_voltage_time_sel = anatop_regmap_set_voltage_time_sel,
  144. .get_voltage_sel = anatop_regmap_core_get_voltage_sel,
  145. .list_voltage = regulator_list_voltage_linear,
  146. .map_voltage = regulator_map_voltage_linear,
  147. .get_bypass = anatop_regmap_get_bypass,
  148. .set_bypass = anatop_regmap_set_bypass,
  149. };
  150. static int anatop_regulator_probe(struct platform_device *pdev)
  151. {
  152. struct device *dev = &pdev->dev;
  153. struct device_node *np = dev->of_node;
  154. struct device_node *anatop_np;
  155. struct regulator_desc *rdesc;
  156. struct regulator_dev *rdev;
  157. struct anatop_regulator *sreg;
  158. struct regulator_init_data *initdata;
  159. struct regulator_config config = { };
  160. int ret = 0;
  161. u32 val;
  162. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  163. if (!sreg)
  164. return -ENOMEM;
  165. sreg->name = of_get_property(np, "regulator-name", NULL);
  166. rdesc = &sreg->rdesc;
  167. rdesc->name = sreg->name;
  168. rdesc->type = REGULATOR_VOLTAGE;
  169. rdesc->owner = THIS_MODULE;
  170. initdata = of_get_regulator_init_data(dev, np, rdesc);
  171. initdata->supply_regulator = "vin";
  172. sreg->initdata = initdata;
  173. anatop_np = of_get_parent(np);
  174. if (!anatop_np)
  175. return -ENODEV;
  176. sreg->anatop = syscon_node_to_regmap(anatop_np);
  177. of_node_put(anatop_np);
  178. if (IS_ERR(sreg->anatop))
  179. return PTR_ERR(sreg->anatop);
  180. ret = of_property_read_u32(np, "anatop-reg-offset",
  181. &sreg->control_reg);
  182. if (ret) {
  183. dev_err(dev, "no anatop-reg-offset property set\n");
  184. return ret;
  185. }
  186. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  187. &sreg->vol_bit_width);
  188. if (ret) {
  189. dev_err(dev, "no anatop-vol-bit-width property set\n");
  190. return ret;
  191. }
  192. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  193. &sreg->vol_bit_shift);
  194. if (ret) {
  195. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  196. return ret;
  197. }
  198. ret = of_property_read_u32(np, "anatop-min-bit-val",
  199. &sreg->min_bit_val);
  200. if (ret) {
  201. dev_err(dev, "no anatop-min-bit-val property set\n");
  202. return ret;
  203. }
  204. ret = of_property_read_u32(np, "anatop-min-voltage",
  205. &sreg->min_voltage);
  206. if (ret) {
  207. dev_err(dev, "no anatop-min-voltage property set\n");
  208. return ret;
  209. }
  210. ret = of_property_read_u32(np, "anatop-max-voltage",
  211. &sreg->max_voltage);
  212. if (ret) {
  213. dev_err(dev, "no anatop-max-voltage property set\n");
  214. return ret;
  215. }
  216. /* read LDO ramp up setting, only for core reg */
  217. of_property_read_u32(np, "anatop-delay-reg-offset",
  218. &sreg->delay_reg);
  219. of_property_read_u32(np, "anatop-delay-bit-width",
  220. &sreg->delay_bit_width);
  221. of_property_read_u32(np, "anatop-delay-bit-shift",
  222. &sreg->delay_bit_shift);
  223. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1
  224. + sreg->min_bit_val;
  225. rdesc->min_uV = sreg->min_voltage;
  226. rdesc->uV_step = 25000;
  227. rdesc->linear_min_sel = sreg->min_bit_val;
  228. rdesc->vsel_reg = sreg->control_reg;
  229. rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) <<
  230. sreg->vol_bit_shift;
  231. rdesc->min_dropout_uV = 125000;
  232. config.dev = &pdev->dev;
  233. config.init_data = initdata;
  234. config.driver_data = sreg;
  235. config.of_node = pdev->dev.of_node;
  236. config.regmap = sreg->anatop;
  237. /* Only core regulators have the ramp up delay configuration. */
  238. if (sreg->control_reg && sreg->delay_bit_width) {
  239. rdesc->ops = &anatop_core_rops;
  240. ret = regmap_read(config.regmap, rdesc->vsel_reg, &val);
  241. if (ret) {
  242. dev_err(dev, "failed to read initial state\n");
  243. return ret;
  244. }
  245. sreg->sel = (val & rdesc->vsel_mask) >> sreg->vol_bit_shift;
  246. if (sreg->sel == LDO_FET_FULL_ON) {
  247. sreg->sel = 0;
  248. sreg->bypass = true;
  249. }
  250. /*
  251. * In case vddpu was disabled by the bootloader, we need to set
  252. * a sane default until imx6-cpufreq was probed and changes the
  253. * voltage to the correct value. In this case we set 1.25V.
  254. */
  255. if (!sreg->sel && !strcmp(sreg->name, "vddpu"))
  256. sreg->sel = 22;
  257. /* set the default voltage of the pcie phy to be 1.100v */
  258. if (!sreg->sel && rdesc->name &&
  259. !strcmp(rdesc->name, "vddpcie"))
  260. sreg->sel = 0x10;
  261. if (!sreg->bypass && !sreg->sel) {
  262. dev_err(&pdev->dev, "Failed to read a valid default voltage selector.\n");
  263. return -EINVAL;
  264. }
  265. } else {
  266. rdesc->ops = &anatop_rops;
  267. }
  268. /* register regulator */
  269. rdev = devm_regulator_register(dev, rdesc, &config);
  270. if (IS_ERR(rdev)) {
  271. dev_err(dev, "failed to register %s\n",
  272. rdesc->name);
  273. return PTR_ERR(rdev);
  274. }
  275. platform_set_drvdata(pdev, rdev);
  276. return 0;
  277. }
  278. static const struct of_device_id of_anatop_regulator_match_tbl[] = {
  279. { .compatible = "fsl,anatop-regulator", },
  280. { /* end */ }
  281. };
  282. MODULE_DEVICE_TABLE(of, of_anatop_regulator_match_tbl);
  283. static struct platform_driver anatop_regulator_driver = {
  284. .driver = {
  285. .name = "anatop_regulator",
  286. .of_match_table = of_anatop_regulator_match_tbl,
  287. },
  288. .probe = anatop_regulator_probe,
  289. };
  290. static int __init anatop_regulator_init(void)
  291. {
  292. return platform_driver_register(&anatop_regulator_driver);
  293. }
  294. postcore_initcall(anatop_regulator_init);
  295. static void __exit anatop_regulator_exit(void)
  296. {
  297. platform_driver_unregister(&anatop_regulator_driver);
  298. }
  299. module_exit(anatop_regulator_exit);
  300. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
  301. MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  302. MODULE_DESCRIPTION("ANATOP Regulator driver");
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_ALIAS("platform:anatop_regulator");