anatop-regulator.c 9.6 KB

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