anatop-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. 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. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  162. if (!sreg)
  163. return -ENOMEM;
  164. rdesc = &sreg->rdesc;
  165. rdesc->type = REGULATOR_VOLTAGE;
  166. rdesc->owner = THIS_MODULE;
  167. of_property_read_string(np, "regulator-name", &rdesc->name);
  168. if (!rdesc->name) {
  169. dev_err(dev, "failed to get a regulator-name\n");
  170. return -EINVAL;
  171. }
  172. initdata = of_get_regulator_init_data(dev, np, rdesc);
  173. if (!initdata)
  174. return -ENOMEM;
  175. initdata->supply_regulator = "vin";
  176. sreg->initdata = initdata;
  177. anatop_np = of_get_parent(np);
  178. if (!anatop_np)
  179. return -ENODEV;
  180. sreg->anatop = syscon_node_to_regmap(anatop_np);
  181. of_node_put(anatop_np);
  182. if (IS_ERR(sreg->anatop))
  183. return PTR_ERR(sreg->anatop);
  184. ret = of_property_read_u32(np, "anatop-reg-offset",
  185. &sreg->control_reg);
  186. if (ret) {
  187. dev_err(dev, "no anatop-reg-offset property set\n");
  188. return ret;
  189. }
  190. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  191. &sreg->vol_bit_width);
  192. if (ret) {
  193. dev_err(dev, "no anatop-vol-bit-width property set\n");
  194. return ret;
  195. }
  196. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  197. &sreg->vol_bit_shift);
  198. if (ret) {
  199. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  200. return ret;
  201. }
  202. ret = of_property_read_u32(np, "anatop-min-bit-val",
  203. &sreg->min_bit_val);
  204. if (ret) {
  205. dev_err(dev, "no anatop-min-bit-val property set\n");
  206. return ret;
  207. }
  208. ret = of_property_read_u32(np, "anatop-min-voltage",
  209. &sreg->min_voltage);
  210. if (ret) {
  211. dev_err(dev, "no anatop-min-voltage property set\n");
  212. return ret;
  213. }
  214. ret = of_property_read_u32(np, "anatop-max-voltage",
  215. &sreg->max_voltage);
  216. if (ret) {
  217. dev_err(dev, "no anatop-max-voltage property set\n");
  218. return ret;
  219. }
  220. /* read LDO ramp up setting, only for core reg */
  221. of_property_read_u32(np, "anatop-delay-reg-offset",
  222. &sreg->delay_reg);
  223. of_property_read_u32(np, "anatop-delay-bit-width",
  224. &sreg->delay_bit_width);
  225. of_property_read_u32(np, "anatop-delay-bit-shift",
  226. &sreg->delay_bit_shift);
  227. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1
  228. + sreg->min_bit_val;
  229. rdesc->min_uV = sreg->min_voltage;
  230. rdesc->uV_step = 25000;
  231. rdesc->linear_min_sel = sreg->min_bit_val;
  232. rdesc->vsel_reg = sreg->control_reg;
  233. rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) <<
  234. sreg->vol_bit_shift;
  235. rdesc->min_dropout_uV = 125000;
  236. config.dev = &pdev->dev;
  237. config.init_data = initdata;
  238. config.driver_data = sreg;
  239. config.of_node = pdev->dev.of_node;
  240. config.regmap = sreg->anatop;
  241. /* Only core regulators have the ramp up delay configuration. */
  242. if (sreg->control_reg && sreg->delay_bit_width) {
  243. rdesc->ops = &anatop_core_rops;
  244. ret = regmap_read(config.regmap, rdesc->vsel_reg, &val);
  245. if (ret) {
  246. dev_err(dev, "failed to read initial state\n");
  247. return ret;
  248. }
  249. sreg->sel = (val & rdesc->vsel_mask) >> sreg->vol_bit_shift;
  250. if (sreg->sel == LDO_FET_FULL_ON) {
  251. sreg->sel = 0;
  252. sreg->bypass = true;
  253. }
  254. /*
  255. * In case vddpu was disabled by the bootloader, we need to set
  256. * a sane default until imx6-cpufreq was probed and changes the
  257. * voltage to the correct value. In this case we set 1.25V.
  258. */
  259. if (!sreg->sel && !strcmp(rdesc->name, "vddpu"))
  260. sreg->sel = 22;
  261. /* set the default voltage of the pcie phy to be 1.100v */
  262. if (!sreg->sel && !strcmp(rdesc->name, "vddpcie"))
  263. sreg->sel = 0x10;
  264. if (!sreg->bypass && !sreg->sel) {
  265. dev_err(&pdev->dev, "Failed to read a valid default voltage selector.\n");
  266. return -EINVAL;
  267. }
  268. } else {
  269. u32 enable_bit;
  270. rdesc->ops = &anatop_rops;
  271. if (!of_property_read_u32(np, "anatop-enable-bit",
  272. &enable_bit)) {
  273. anatop_rops.enable = regulator_enable_regmap;
  274. anatop_rops.disable = regulator_disable_regmap;
  275. anatop_rops.is_enabled = regulator_is_enabled_regmap;
  276. rdesc->enable_reg = sreg->control_reg;
  277. rdesc->enable_mask = BIT(enable_bit);
  278. }
  279. }
  280. /* register regulator */
  281. rdev = devm_regulator_register(dev, rdesc, &config);
  282. if (IS_ERR(rdev)) {
  283. dev_err(dev, "failed to register %s\n",
  284. rdesc->name);
  285. return PTR_ERR(rdev);
  286. }
  287. platform_set_drvdata(pdev, rdev);
  288. return 0;
  289. }
  290. static const struct of_device_id of_anatop_regulator_match_tbl[] = {
  291. { .compatible = "fsl,anatop-regulator", },
  292. { /* end */ }
  293. };
  294. MODULE_DEVICE_TABLE(of, of_anatop_regulator_match_tbl);
  295. static struct platform_driver anatop_regulator_driver = {
  296. .driver = {
  297. .name = "anatop_regulator",
  298. .of_match_table = of_anatop_regulator_match_tbl,
  299. },
  300. .probe = anatop_regulator_probe,
  301. };
  302. static int __init anatop_regulator_init(void)
  303. {
  304. return platform_driver_register(&anatop_regulator_driver);
  305. }
  306. postcore_initcall(anatop_regulator_init);
  307. static void __exit anatop_regulator_exit(void)
  308. {
  309. platform_driver_unregister(&anatop_regulator_driver);
  310. }
  311. module_exit(anatop_regulator_exit);
  312. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
  313. MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  314. MODULE_DESCRIPTION("ANATOP Regulator driver");
  315. MODULE_LICENSE("GPL v2");
  316. MODULE_ALIAS("platform:anatop_regulator");