tps65132-regulator.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * TI TPS65132 Regulator driver
  3. *
  4. * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
  7. * Laxman Dewangan <ldewangan@nvidia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  15. * whether express or implied; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/i2c.h>
  23. #include <linux/module.h>
  24. #include <linux/regmap.h>
  25. #include <linux/regulator/driver.h>
  26. #include <linux/regulator/machine.h>
  27. #define TPS65132_REG_VPOS 0x00
  28. #define TPS65132_REG_VNEG 0x01
  29. #define TPS65132_REG_APPS_DISP_DISN 0x03
  30. #define TPS65132_REG_CONTROL 0x0FF
  31. #define TPS65132_VOUT_MASK 0x1F
  32. #define TPS65132_VOUT_N_VOLTAGE 0x15
  33. #define TPS65132_VOUT_VMIN 4000000
  34. #define TPS65132_VOUT_VMAX 6000000
  35. #define TPS65132_VOUT_STEP 100000
  36. #define TPS65132_REG_APPS_DIS_VPOS BIT(0)
  37. #define TPS65132_REG_APPS_DIS_VNEG BIT(1)
  38. #define TPS65132_REGULATOR_ID_VPOS 0
  39. #define TPS65132_REGULATOR_ID_VNEG 1
  40. #define TPS65132_MAX_REGULATORS 2
  41. #define TPS65132_ACT_DIS_TIME_SLACK 1000
  42. struct tps65132_reg_pdata {
  43. struct gpio_desc *en_gpiod;
  44. struct gpio_desc *act_dis_gpiod;
  45. unsigned int act_dis_time_us;
  46. int ena_gpio_state;
  47. };
  48. struct tps65132_regulator {
  49. struct device *dev;
  50. struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
  51. };
  52. static int tps65132_regulator_enable(struct regulator_dev *rdev)
  53. {
  54. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  55. int id = rdev_get_id(rdev);
  56. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  57. int ret;
  58. if (!IS_ERR(rpdata->en_gpiod)) {
  59. gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
  60. rpdata->ena_gpio_state = 1;
  61. }
  62. /* Hardware automatically enable discharge bit in enable */
  63. if (rdev->constraints->active_discharge ==
  64. REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
  65. ret = regulator_set_active_discharge_regmap(rdev, false);
  66. if (ret < 0) {
  67. dev_err(tps->dev, "Failed to disable active discharge: %d\n",
  68. ret);
  69. return ret;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int tps65132_regulator_disable(struct regulator_dev *rdev)
  75. {
  76. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  77. int id = rdev_get_id(rdev);
  78. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  79. if (!IS_ERR(rpdata->en_gpiod)) {
  80. gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
  81. rpdata->ena_gpio_state = 0;
  82. }
  83. if (!IS_ERR(rpdata->act_dis_gpiod)) {
  84. gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
  85. usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
  86. TPS65132_ACT_DIS_TIME_SLACK);
  87. gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
  88. }
  89. return 0;
  90. }
  91. static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
  92. {
  93. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  94. int id = rdev_get_id(rdev);
  95. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  96. if (!IS_ERR(rpdata->en_gpiod))
  97. return rpdata->ena_gpio_state;
  98. return 1;
  99. }
  100. static const struct regulator_ops tps65132_regulator_ops = {
  101. .enable = tps65132_regulator_enable,
  102. .disable = tps65132_regulator_disable,
  103. .is_enabled = tps65132_regulator_is_enabled,
  104. .list_voltage = regulator_list_voltage_linear,
  105. .map_voltage = regulator_map_voltage_linear,
  106. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  107. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  108. .set_active_discharge = regulator_set_active_discharge_regmap,
  109. };
  110. static int tps65132_of_parse_cb(struct device_node *np,
  111. const struct regulator_desc *desc,
  112. struct regulator_config *config)
  113. {
  114. struct tps65132_regulator *tps = config->driver_data;
  115. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
  116. int ret;
  117. rpdata->en_gpiod = devm_fwnode_gpiod_get(tps->dev, of_fwnode_handle(np),
  118. "enable", GPIOD_ASIS,
  119. "enable");
  120. if (IS_ERR(rpdata->en_gpiod)) {
  121. ret = PTR_ERR(rpdata->en_gpiod);
  122. /* Ignore the error other than probe defer */
  123. if (ret == -EPROBE_DEFER)
  124. return ret;
  125. return 0;
  126. }
  127. rpdata->act_dis_gpiod = devm_fwnode_gpiod_get(tps->dev,
  128. of_fwnode_handle(np),
  129. "active-discharge",
  130. GPIOD_ASIS,
  131. "active-discharge");
  132. if (IS_ERR(rpdata->act_dis_gpiod)) {
  133. ret = PTR_ERR(rpdata->act_dis_gpiod);
  134. /* Ignore the error other than probe defer */
  135. if (ret == -EPROBE_DEFER)
  136. return ret;
  137. return 0;
  138. }
  139. ret = of_property_read_u32(np, "ti,active-discharge-time-us",
  140. &rpdata->act_dis_time_us);
  141. if (ret < 0) {
  142. dev_err(tps->dev, "Failed to read active discharge time:%d\n",
  143. ret);
  144. return ret;
  145. }
  146. return 0;
  147. }
  148. #define TPS65132_REGULATOR_DESC(_id, _name) \
  149. [TPS65132_REGULATOR_ID_##_id] = { \
  150. .name = "tps65132-"#_name, \
  151. .supply_name = "vin", \
  152. .id = TPS65132_REGULATOR_ID_##_id, \
  153. .of_match = of_match_ptr(#_name), \
  154. .of_parse_cb = tps65132_of_parse_cb, \
  155. .ops = &tps65132_regulator_ops, \
  156. .n_voltages = TPS65132_VOUT_N_VOLTAGE, \
  157. .min_uV = TPS65132_VOUT_VMIN, \
  158. .uV_step = TPS65132_VOUT_STEP, \
  159. .enable_time = 500, \
  160. .vsel_mask = TPS65132_VOUT_MASK, \
  161. .vsel_reg = TPS65132_REG_##_id, \
  162. .active_discharge_off = 0, \
  163. .active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
  164. .active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
  165. .active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
  166. .type = REGULATOR_VOLTAGE, \
  167. .owner = THIS_MODULE, \
  168. }
  169. static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
  170. TPS65132_REGULATOR_DESC(VPOS, outp),
  171. TPS65132_REGULATOR_DESC(VNEG, outn),
  172. };
  173. static const struct regmap_range tps65132_no_reg_ranges[] = {
  174. regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
  175. TPS65132_REG_CONTROL - 1),
  176. };
  177. static const struct regmap_access_table tps65132_no_reg_table = {
  178. .no_ranges = tps65132_no_reg_ranges,
  179. .n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
  180. };
  181. static const struct regmap_config tps65132_regmap_config = {
  182. .reg_bits = 8,
  183. .val_bits = 8,
  184. .max_register = TPS65132_REG_CONTROL,
  185. .cache_type = REGCACHE_NONE,
  186. .rd_table = &tps65132_no_reg_table,
  187. .wr_table = &tps65132_no_reg_table,
  188. };
  189. static int tps65132_probe(struct i2c_client *client)
  190. {
  191. struct device *dev = &client->dev;
  192. struct tps65132_regulator *tps;
  193. struct regulator_dev *rdev;
  194. struct regmap *rmap;
  195. struct regulator_config config = { };
  196. int id;
  197. int ret;
  198. tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
  199. if (!tps)
  200. return -ENOMEM;
  201. rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
  202. if (IS_ERR(rmap)) {
  203. ret = PTR_ERR(rmap);
  204. dev_err(dev, "regmap init failed: %d\n", ret);
  205. return ret;
  206. }
  207. i2c_set_clientdata(client, tps);
  208. tps->dev = dev;
  209. for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
  210. config.regmap = rmap;
  211. config.dev = dev;
  212. config.driver_data = tps;
  213. rdev = devm_regulator_register(dev, &tps_regs_desc[id],
  214. &config);
  215. if (IS_ERR(rdev)) {
  216. ret = PTR_ERR(rdev);
  217. dev_err(dev, "regulator %s register failed: %d\n",
  218. tps_regs_desc[id].name, ret);
  219. return ret;
  220. }
  221. }
  222. return 0;
  223. }
  224. static const struct i2c_device_id tps65132_id[] = {
  225. {.name = "tps65132",},
  226. {},
  227. };
  228. MODULE_DEVICE_TABLE(i2c, tps65132_id);
  229. static struct i2c_driver tps65132_i2c_driver = {
  230. .driver = {
  231. .name = "tps65132",
  232. },
  233. .probe_new = tps65132_probe,
  234. .id_table = tps65132_id,
  235. };
  236. module_i2c_driver(tps65132_i2c_driver);
  237. MODULE_DESCRIPTION("tps65132 regulator driver");
  238. MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
  239. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  240. MODULE_LICENSE("GPL v2");