anatop-regulator.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */
  30. #define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */
  31. #define LDO_POWER_GATE 0x00
  32. #define LDO_FET_FULL_ON 0x1f
  33. struct anatop_regulator {
  34. const char *name;
  35. u32 control_reg;
  36. struct regmap *anatop;
  37. int vol_bit_shift;
  38. int vol_bit_width;
  39. u32 delay_reg;
  40. int delay_bit_shift;
  41. int delay_bit_width;
  42. int min_bit_val;
  43. int min_voltage;
  44. int max_voltage;
  45. struct regulator_desc rdesc;
  46. struct regulator_init_data *initdata;
  47. bool bypass;
  48. int sel;
  49. };
  50. static int anatop_regmap_set_voltage_time_sel(struct regulator_dev *reg,
  51. unsigned int old_sel,
  52. unsigned int new_sel)
  53. {
  54. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  55. u32 val;
  56. int ret = 0;
  57. /* check whether need to care about LDO ramp up speed */
  58. if (anatop_reg->delay_bit_width && new_sel > old_sel) {
  59. /*
  60. * the delay for LDO ramp up time is
  61. * based on the register setting, we need
  62. * to calculate how many steps LDO need to
  63. * ramp up, and how much delay needed. (us)
  64. */
  65. regmap_read(anatop_reg->anatop, anatop_reg->delay_reg, &val);
  66. val = (val >> anatop_reg->delay_bit_shift) &
  67. ((1 << anatop_reg->delay_bit_width) - 1);
  68. ret = (new_sel - old_sel) * (LDO_RAMP_UP_UNIT_IN_CYCLES <<
  69. val) / LDO_RAMP_UP_FREQ_IN_MHZ + 1;
  70. }
  71. return ret;
  72. }
  73. static int anatop_regmap_enable(struct regulator_dev *reg)
  74. {
  75. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  76. int sel;
  77. sel = anatop_reg->bypass ? LDO_FET_FULL_ON : anatop_reg->sel;
  78. return regulator_set_voltage_sel_regmap(reg, sel);
  79. }
  80. static int anatop_regmap_disable(struct regulator_dev *reg)
  81. {
  82. return regulator_set_voltage_sel_regmap(reg, LDO_POWER_GATE);
  83. }
  84. static int anatop_regmap_is_enabled(struct regulator_dev *reg)
  85. {
  86. return regulator_get_voltage_sel_regmap(reg) != LDO_POWER_GATE;
  87. }
  88. static int anatop_regmap_core_set_voltage_sel(struct regulator_dev *reg,
  89. unsigned selector)
  90. {
  91. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  92. int ret;
  93. if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg)) {
  94. anatop_reg->sel = selector;
  95. return 0;
  96. }
  97. ret = regulator_set_voltage_sel_regmap(reg, selector);
  98. if (!ret)
  99. anatop_reg->sel = selector;
  100. return ret;
  101. }
  102. static int anatop_regmap_core_get_voltage_sel(struct regulator_dev *reg)
  103. {
  104. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  105. if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg))
  106. return anatop_reg->sel;
  107. return regulator_get_voltage_sel_regmap(reg);
  108. }
  109. static int anatop_regmap_get_bypass(struct regulator_dev *reg, bool *enable)
  110. {
  111. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  112. int sel;
  113. sel = regulator_get_voltage_sel_regmap(reg);
  114. if (sel == LDO_FET_FULL_ON)
  115. WARN_ON(!anatop_reg->bypass);
  116. else if (sel != LDO_POWER_GATE)
  117. WARN_ON(anatop_reg->bypass);
  118. *enable = anatop_reg->bypass;
  119. return 0;
  120. }
  121. static int anatop_regmap_set_bypass(struct regulator_dev *reg, bool enable)
  122. {
  123. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  124. int sel;
  125. if (enable == anatop_reg->bypass)
  126. return 0;
  127. sel = enable ? LDO_FET_FULL_ON : anatop_reg->sel;
  128. anatop_reg->bypass = enable;
  129. return regulator_set_voltage_sel_regmap(reg, sel);
  130. }
  131. static struct regulator_ops anatop_rops = {
  132. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  133. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  134. .list_voltage = regulator_list_voltage_linear,
  135. .map_voltage = regulator_map_voltage_linear,
  136. };
  137. static struct regulator_ops anatop_core_rops = {
  138. .enable = anatop_regmap_enable,
  139. .disable = anatop_regmap_disable,
  140. .is_enabled = anatop_regmap_is_enabled,
  141. .set_voltage_sel = anatop_regmap_core_set_voltage_sel,
  142. .set_voltage_time_sel = anatop_regmap_set_voltage_time_sel,
  143. .get_voltage_sel = anatop_regmap_core_get_voltage_sel,
  144. .list_voltage = regulator_list_voltage_linear,
  145. .map_voltage = regulator_map_voltage_linear,
  146. .get_bypass = anatop_regmap_get_bypass,
  147. .set_bypass = anatop_regmap_set_bypass,
  148. };
  149. static int anatop_regulator_probe(struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. struct device_node *np = dev->of_node;
  153. struct device_node *anatop_np;
  154. struct regulator_desc *rdesc;
  155. struct regulator_dev *rdev;
  156. struct anatop_regulator *sreg;
  157. struct regulator_init_data *initdata;
  158. struct regulator_config config = { };
  159. int ret = 0;
  160. u32 val;
  161. initdata = of_get_regulator_init_data(dev, np);
  162. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  163. if (!sreg)
  164. return -ENOMEM;
  165. sreg->initdata = initdata;
  166. sreg->name = of_get_property(np, "regulator-name", NULL);
  167. rdesc = &sreg->rdesc;
  168. rdesc->name = sreg->name;
  169. rdesc->type = REGULATOR_VOLTAGE;
  170. rdesc->owner = THIS_MODULE;
  171. anatop_np = of_get_parent(np);
  172. if (!anatop_np)
  173. return -ENODEV;
  174. sreg->anatop = syscon_node_to_regmap(anatop_np);
  175. of_node_put(anatop_np);
  176. if (IS_ERR(sreg->anatop))
  177. return PTR_ERR(sreg->anatop);
  178. ret = of_property_read_u32(np, "anatop-reg-offset",
  179. &sreg->control_reg);
  180. if (ret) {
  181. dev_err(dev, "no anatop-reg-offset property set\n");
  182. return ret;
  183. }
  184. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  185. &sreg->vol_bit_width);
  186. if (ret) {
  187. dev_err(dev, "no anatop-vol-bit-width property set\n");
  188. return ret;
  189. }
  190. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  191. &sreg->vol_bit_shift);
  192. if (ret) {
  193. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  194. return ret;
  195. }
  196. ret = of_property_read_u32(np, "anatop-min-bit-val",
  197. &sreg->min_bit_val);
  198. if (ret) {
  199. dev_err(dev, "no anatop-min-bit-val property set\n");
  200. return ret;
  201. }
  202. ret = of_property_read_u32(np, "anatop-min-voltage",
  203. &sreg->min_voltage);
  204. if (ret) {
  205. dev_err(dev, "no anatop-min-voltage property set\n");
  206. return ret;
  207. }
  208. ret = of_property_read_u32(np, "anatop-max-voltage",
  209. &sreg->max_voltage);
  210. if (ret) {
  211. dev_err(dev, "no anatop-max-voltage property set\n");
  212. return ret;
  213. }
  214. /* read LDO ramp up setting, only for core reg */
  215. of_property_read_u32(np, "anatop-delay-reg-offset",
  216. &sreg->delay_reg);
  217. of_property_read_u32(np, "anatop-delay-bit-width",
  218. &sreg->delay_bit_width);
  219. of_property_read_u32(np, "anatop-delay-bit-shift",
  220. &sreg->delay_bit_shift);
  221. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1
  222. + sreg->min_bit_val;
  223. rdesc->min_uV = sreg->min_voltage;
  224. rdesc->uV_step = 25000;
  225. rdesc->linear_min_sel = sreg->min_bit_val;
  226. rdesc->vsel_reg = sreg->control_reg;
  227. rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) <<
  228. sreg->vol_bit_shift;
  229. config.dev = &pdev->dev;
  230. config.init_data = initdata;
  231. config.driver_data = sreg;
  232. config.of_node = pdev->dev.of_node;
  233. config.regmap = sreg->anatop;
  234. /* Only core regulators have the ramp up delay configuration. */
  235. if (sreg->control_reg && sreg->delay_bit_width) {
  236. rdesc->ops = &anatop_core_rops;
  237. ret = regmap_read(config.regmap, rdesc->vsel_reg, &val);
  238. if (ret) {
  239. dev_err(dev, "failed to read initial state\n");
  240. return ret;
  241. }
  242. sreg->sel = (val & rdesc->vsel_mask) >> sreg->vol_bit_shift;
  243. if (sreg->sel == LDO_FET_FULL_ON) {
  244. sreg->sel = 0;
  245. sreg->bypass = true;
  246. }
  247. } else {
  248. rdesc->ops = &anatop_rops;
  249. }
  250. /* register regulator */
  251. rdev = devm_regulator_register(dev, rdesc, &config);
  252. if (IS_ERR(rdev)) {
  253. dev_err(dev, "failed to register %s\n",
  254. rdesc->name);
  255. return PTR_ERR(rdev);
  256. }
  257. platform_set_drvdata(pdev, rdev);
  258. return 0;
  259. }
  260. static const struct of_device_id of_anatop_regulator_match_tbl[] = {
  261. { .compatible = "fsl,anatop-regulator", },
  262. { /* end */ }
  263. };
  264. static struct platform_driver anatop_regulator_driver = {
  265. .driver = {
  266. .name = "anatop_regulator",
  267. .owner = THIS_MODULE,
  268. .of_match_table = of_anatop_regulator_match_tbl,
  269. },
  270. .probe = anatop_regulator_probe,
  271. };
  272. static int __init anatop_regulator_init(void)
  273. {
  274. return platform_driver_register(&anatop_regulator_driver);
  275. }
  276. postcore_initcall(anatop_regulator_init);
  277. static void __exit anatop_regulator_exit(void)
  278. {
  279. platform_driver_unregister(&anatop_regulator_driver);
  280. }
  281. module_exit(anatop_regulator_exit);
  282. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
  283. MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  284. MODULE_DESCRIPTION("ANATOP Regulator driver");
  285. MODULE_LICENSE("GPL v2");
  286. MODULE_ALIAS("platform:anatop_regulator");