lm363x-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI LM363X Regulator Driver
  4. *
  5. * Copyright 2015 Texas Instruments
  6. *
  7. * Author: Milo Kim <milo.kim@ti.com>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mfd/ti-lmu.h>
  12. #include <linux/mfd/ti-lmu-register.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/driver.h>
  18. #include <linux/regulator/of_regulator.h>
  19. #include <linux/slab.h>
  20. /* LM3631 */
  21. #define LM3631_BOOST_VSEL_MAX 0x25
  22. #define LM3631_LDO_VSEL_MAX 0x28
  23. #define LM3631_CONT_VSEL_MAX 0x03
  24. #define LM3631_VBOOST_MIN 4500000
  25. #define LM3631_VCONT_MIN 1800000
  26. #define LM3631_VLDO_MIN 4000000
  27. #define ENABLE_TIME_USEC 1000
  28. /* LM3632 */
  29. #define LM3632_BOOST_VSEL_MAX 0x26
  30. #define LM3632_LDO_VSEL_MAX 0x28
  31. #define LM3632_VBOOST_MIN 4500000
  32. #define LM3632_VLDO_MIN 4000000
  33. /* LM36274 */
  34. #define LM36274_BOOST_VSEL_MAX 0x3f
  35. #define LM36274_LDO_VSEL_MAX 0x32
  36. #define LM36274_VOLTAGE_MIN 4000000
  37. /* Common */
  38. #define LM363X_STEP_50mV 50000
  39. #define LM363X_STEP_500mV 500000
  40. static const int ldo_cont_enable_time[] = {
  41. 0, 2000, 5000, 10000, 20000, 50000, 100000, 200000,
  42. };
  43. static int lm363x_regulator_enable_time(struct regulator_dev *rdev)
  44. {
  45. enum lm363x_regulator_id id = rdev_get_id(rdev);
  46. unsigned int val, addr, mask;
  47. switch (id) {
  48. case LM3631_LDO_CONT:
  49. addr = LM3631_REG_ENTIME_VCONT;
  50. mask = LM3631_ENTIME_CONT_MASK;
  51. break;
  52. case LM3631_LDO_OREF:
  53. addr = LM3631_REG_ENTIME_VOREF;
  54. mask = LM3631_ENTIME_MASK;
  55. break;
  56. case LM3631_LDO_POS:
  57. addr = LM3631_REG_ENTIME_VPOS;
  58. mask = LM3631_ENTIME_MASK;
  59. break;
  60. case LM3631_LDO_NEG:
  61. addr = LM3631_REG_ENTIME_VNEG;
  62. mask = LM3631_ENTIME_MASK;
  63. break;
  64. default:
  65. return 0;
  66. }
  67. if (regmap_read(rdev->regmap, addr, &val))
  68. return -EINVAL;
  69. val = (val & mask) >> LM3631_ENTIME_SHIFT;
  70. if (id == LM3631_LDO_CONT)
  71. return ldo_cont_enable_time[val];
  72. else
  73. return ENABLE_TIME_USEC * val;
  74. }
  75. static const struct regulator_ops lm363x_boost_voltage_table_ops = {
  76. .list_voltage = regulator_list_voltage_linear,
  77. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  78. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  79. };
  80. static const struct regulator_ops lm363x_regulator_voltage_table_ops = {
  81. .list_voltage = regulator_list_voltage_linear,
  82. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  83. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  84. .enable = regulator_enable_regmap,
  85. .disable = regulator_disable_regmap,
  86. .is_enabled = regulator_is_enabled_regmap,
  87. .enable_time = lm363x_regulator_enable_time,
  88. };
  89. static const struct regulator_desc lm363x_regulator_desc[] = {
  90. /* LM3631 */
  91. {
  92. .name = "vboost",
  93. .of_match = "vboost",
  94. .id = LM3631_BOOST,
  95. .ops = &lm363x_boost_voltage_table_ops,
  96. .n_voltages = LM3631_BOOST_VSEL_MAX + 1,
  97. .min_uV = LM3631_VBOOST_MIN,
  98. .uV_step = LM363X_STEP_50mV,
  99. .type = REGULATOR_VOLTAGE,
  100. .owner = THIS_MODULE,
  101. .vsel_reg = LM3631_REG_VOUT_BOOST,
  102. .vsel_mask = LM3631_VOUT_MASK,
  103. },
  104. {
  105. .name = "ldo_cont",
  106. .of_match = "vcont",
  107. .id = LM3631_LDO_CONT,
  108. .ops = &lm363x_regulator_voltage_table_ops,
  109. .n_voltages = LM3631_CONT_VSEL_MAX + 1,
  110. .min_uV = LM3631_VCONT_MIN,
  111. .uV_step = LM363X_STEP_500mV,
  112. .type = REGULATOR_VOLTAGE,
  113. .owner = THIS_MODULE,
  114. .vsel_reg = LM3631_REG_VOUT_CONT,
  115. .vsel_mask = LM3631_VOUT_CONT_MASK,
  116. .enable_reg = LM3631_REG_LDO_CTRL2,
  117. .enable_mask = LM3631_EN_CONT_MASK,
  118. },
  119. {
  120. .name = "ldo_oref",
  121. .of_match = "voref",
  122. .id = LM3631_LDO_OREF,
  123. .ops = &lm363x_regulator_voltage_table_ops,
  124. .n_voltages = LM3631_LDO_VSEL_MAX + 1,
  125. .min_uV = LM3631_VLDO_MIN,
  126. .uV_step = LM363X_STEP_50mV,
  127. .type = REGULATOR_VOLTAGE,
  128. .owner = THIS_MODULE,
  129. .vsel_reg = LM3631_REG_VOUT_OREF,
  130. .vsel_mask = LM3631_VOUT_MASK,
  131. .enable_reg = LM3631_REG_LDO_CTRL1,
  132. .enable_mask = LM3631_EN_OREF_MASK,
  133. },
  134. {
  135. .name = "ldo_vpos",
  136. .of_match = "vpos",
  137. .id = LM3631_LDO_POS,
  138. .ops = &lm363x_regulator_voltage_table_ops,
  139. .n_voltages = LM3631_LDO_VSEL_MAX + 1,
  140. .min_uV = LM3631_VLDO_MIN,
  141. .uV_step = LM363X_STEP_50mV,
  142. .type = REGULATOR_VOLTAGE,
  143. .owner = THIS_MODULE,
  144. .vsel_reg = LM3631_REG_VOUT_POS,
  145. .vsel_mask = LM3631_VOUT_MASK,
  146. .enable_reg = LM3631_REG_LDO_CTRL1,
  147. .enable_mask = LM3631_EN_VPOS_MASK,
  148. },
  149. {
  150. .name = "ldo_vneg",
  151. .of_match = "vneg",
  152. .id = LM3631_LDO_NEG,
  153. .ops = &lm363x_regulator_voltage_table_ops,
  154. .n_voltages = LM3631_LDO_VSEL_MAX + 1,
  155. .min_uV = LM3631_VLDO_MIN,
  156. .uV_step = LM363X_STEP_50mV,
  157. .type = REGULATOR_VOLTAGE,
  158. .owner = THIS_MODULE,
  159. .vsel_reg = LM3631_REG_VOUT_NEG,
  160. .vsel_mask = LM3631_VOUT_MASK,
  161. .enable_reg = LM3631_REG_LDO_CTRL1,
  162. .enable_mask = LM3631_EN_VNEG_MASK,
  163. },
  164. /* LM3632 */
  165. {
  166. .name = "vboost",
  167. .of_match = "vboost",
  168. .id = LM3632_BOOST,
  169. .ops = &lm363x_boost_voltage_table_ops,
  170. .n_voltages = LM3632_BOOST_VSEL_MAX + 1,
  171. .min_uV = LM3632_VBOOST_MIN,
  172. .uV_step = LM363X_STEP_50mV,
  173. .type = REGULATOR_VOLTAGE,
  174. .owner = THIS_MODULE,
  175. .vsel_reg = LM3632_REG_VOUT_BOOST,
  176. .vsel_mask = LM3632_VOUT_MASK,
  177. },
  178. {
  179. .name = "ldo_vpos",
  180. .of_match = "vpos",
  181. .id = LM3632_LDO_POS,
  182. .ops = &lm363x_regulator_voltage_table_ops,
  183. .n_voltages = LM3632_LDO_VSEL_MAX + 1,
  184. .min_uV = LM3632_VLDO_MIN,
  185. .uV_step = LM363X_STEP_50mV,
  186. .type = REGULATOR_VOLTAGE,
  187. .owner = THIS_MODULE,
  188. .vsel_reg = LM3632_REG_VOUT_POS,
  189. .vsel_mask = LM3632_VOUT_MASK,
  190. .enable_reg = LM3632_REG_BIAS_CONFIG,
  191. .enable_mask = LM3632_EN_VPOS_MASK,
  192. },
  193. {
  194. .name = "ldo_vneg",
  195. .of_match = "vneg",
  196. .id = LM3632_LDO_NEG,
  197. .ops = &lm363x_regulator_voltage_table_ops,
  198. .n_voltages = LM3632_LDO_VSEL_MAX + 1,
  199. .min_uV = LM3632_VLDO_MIN,
  200. .uV_step = LM363X_STEP_50mV,
  201. .type = REGULATOR_VOLTAGE,
  202. .owner = THIS_MODULE,
  203. .vsel_reg = LM3632_REG_VOUT_NEG,
  204. .vsel_mask = LM3632_VOUT_MASK,
  205. .enable_reg = LM3632_REG_BIAS_CONFIG,
  206. .enable_mask = LM3632_EN_VNEG_MASK,
  207. },
  208. /* LM36274 */
  209. {
  210. .name = "vboost",
  211. .of_match = "vboost",
  212. .id = LM36274_BOOST,
  213. .ops = &lm363x_boost_voltage_table_ops,
  214. .n_voltages = LM36274_BOOST_VSEL_MAX + 1,
  215. .min_uV = LM36274_VOLTAGE_MIN,
  216. .uV_step = LM363X_STEP_50mV,
  217. .type = REGULATOR_VOLTAGE,
  218. .owner = THIS_MODULE,
  219. .vsel_reg = LM36274_REG_VOUT_BOOST,
  220. .vsel_mask = LM36274_VOUT_MASK,
  221. },
  222. {
  223. .name = "ldo_vpos",
  224. .of_match = "vpos",
  225. .id = LM36274_LDO_POS,
  226. .ops = &lm363x_regulator_voltage_table_ops,
  227. .n_voltages = LM36274_LDO_VSEL_MAX + 1,
  228. .min_uV = LM36274_VOLTAGE_MIN,
  229. .uV_step = LM363X_STEP_50mV,
  230. .type = REGULATOR_VOLTAGE,
  231. .owner = THIS_MODULE,
  232. .vsel_reg = LM36274_REG_VOUT_POS,
  233. .vsel_mask = LM36274_VOUT_MASK,
  234. .enable_reg = LM36274_REG_BIAS_CONFIG_1,
  235. .enable_mask = LM36274_EN_VPOS_MASK,
  236. },
  237. {
  238. .name = "ldo_vneg",
  239. .of_match = "vneg",
  240. .id = LM36274_LDO_NEG,
  241. .ops = &lm363x_regulator_voltage_table_ops,
  242. .n_voltages = LM36274_LDO_VSEL_MAX + 1,
  243. .min_uV = LM36274_VOLTAGE_MIN,
  244. .uV_step = LM363X_STEP_50mV,
  245. .type = REGULATOR_VOLTAGE,
  246. .owner = THIS_MODULE,
  247. .vsel_reg = LM36274_REG_VOUT_NEG,
  248. .vsel_mask = LM36274_VOUT_MASK,
  249. .enable_reg = LM36274_REG_BIAS_CONFIG_1,
  250. .enable_mask = LM36274_EN_VNEG_MASK,
  251. },
  252. };
  253. static struct gpio_desc *lm363x_regulator_of_get_enable_gpio(struct device *dev, int id)
  254. {
  255. /*
  256. * Check LCM_EN1/2_GPIO is configured.
  257. * Those pins are used for enabling VPOS/VNEG LDOs.
  258. * Do not use devm* here: the regulator core takes over the
  259. * lifecycle management of the GPIO descriptor.
  260. */
  261. switch (id) {
  262. case LM3632_LDO_POS:
  263. case LM36274_LDO_POS:
  264. return gpiod_get_index_optional(dev, "enable", 0,
  265. GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  266. case LM3632_LDO_NEG:
  267. case LM36274_LDO_NEG:
  268. return gpiod_get_index_optional(dev, "enable", 1,
  269. GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  270. default:
  271. return NULL;
  272. }
  273. }
  274. static int lm363x_regulator_set_ext_en(struct regmap *regmap, int id)
  275. {
  276. int ext_en_mask = 0;
  277. switch (id) {
  278. case LM3632_LDO_POS:
  279. case LM3632_LDO_NEG:
  280. ext_en_mask = LM3632_EXT_EN_MASK;
  281. break;
  282. case LM36274_LDO_POS:
  283. case LM36274_LDO_NEG:
  284. ext_en_mask = LM36274_EXT_EN_MASK;
  285. break;
  286. default:
  287. return -ENODEV;
  288. }
  289. return regmap_update_bits(regmap, lm363x_regulator_desc[id].enable_reg,
  290. ext_en_mask, ext_en_mask);
  291. }
  292. static int lm363x_regulator_probe(struct platform_device *pdev)
  293. {
  294. struct ti_lmu *lmu = dev_get_drvdata(pdev->dev.parent);
  295. struct regmap *regmap = lmu->regmap;
  296. struct regulator_config cfg = { };
  297. struct regulator_dev *rdev;
  298. struct device *dev = &pdev->dev;
  299. int id = pdev->id;
  300. struct gpio_desc *gpiod;
  301. int ret;
  302. cfg.dev = dev;
  303. cfg.regmap = regmap;
  304. /*
  305. * LM3632 LDOs can be controlled by external pin.
  306. * Register update is required if the pin is used.
  307. */
  308. gpiod = lm363x_regulator_of_get_enable_gpio(dev, id);
  309. if (IS_ERR(gpiod))
  310. return PTR_ERR(gpiod);
  311. if (gpiod) {
  312. cfg.ena_gpiod = gpiod;
  313. ret = lm363x_regulator_set_ext_en(regmap, id);
  314. if (ret) {
  315. gpiod_put(gpiod);
  316. dev_err(dev, "External pin err: %d\n", ret);
  317. return ret;
  318. }
  319. }
  320. rdev = devm_regulator_register(dev, &lm363x_regulator_desc[id], &cfg);
  321. if (IS_ERR(rdev)) {
  322. ret = PTR_ERR(rdev);
  323. dev_err(dev, "[%d] regulator register err: %d\n", id, ret);
  324. return ret;
  325. }
  326. return 0;
  327. }
  328. static struct platform_driver lm363x_regulator_driver = {
  329. .probe = lm363x_regulator_probe,
  330. .driver = {
  331. .name = "lm363x-regulator",
  332. },
  333. };
  334. module_platform_driver(lm363x_regulator_driver);
  335. MODULE_DESCRIPTION("TI LM363X Regulator Driver");
  336. MODULE_AUTHOR("Milo Kim");
  337. MODULE_LICENSE("GPL v2");
  338. MODULE_ALIAS("platform:lm363x-regulator");