tps65132-regulator.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 regmap *rmap;
  51. struct regulator_desc *rdesc[TPS65132_MAX_REGULATORS];
  52. struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
  53. struct regulator_dev *rdev[TPS65132_MAX_REGULATORS];
  54. };
  55. static int tps65132_regulator_enable(struct regulator_dev *rdev)
  56. {
  57. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  58. int id = rdev_get_id(rdev);
  59. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  60. int ret;
  61. if (!IS_ERR(rpdata->en_gpiod)) {
  62. gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
  63. rpdata->ena_gpio_state = 1;
  64. }
  65. /* Hardware automatically enable discharge bit in enable */
  66. if (rdev->constraints->active_discharge ==
  67. REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
  68. ret = regulator_set_active_discharge_regmap(rdev, false);
  69. if (ret < 0) {
  70. dev_err(tps->dev, "Failed to disable active discharge: %d\n",
  71. ret);
  72. return ret;
  73. }
  74. }
  75. return 0;
  76. }
  77. static int tps65132_regulator_disable(struct regulator_dev *rdev)
  78. {
  79. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  80. int id = rdev_get_id(rdev);
  81. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  82. if (!IS_ERR(rpdata->en_gpiod)) {
  83. gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
  84. rpdata->ena_gpio_state = 0;
  85. }
  86. if (!IS_ERR(rpdata->act_dis_gpiod)) {
  87. gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
  88. usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
  89. TPS65132_ACT_DIS_TIME_SLACK);
  90. gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
  91. }
  92. return 0;
  93. }
  94. static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
  95. {
  96. struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
  97. int id = rdev_get_id(rdev);
  98. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
  99. if (!IS_ERR(rpdata->en_gpiod))
  100. return rpdata->ena_gpio_state;
  101. return 1;
  102. }
  103. static struct regulator_ops tps65132_regulator_ops = {
  104. .enable = tps65132_regulator_enable,
  105. .disable = tps65132_regulator_disable,
  106. .is_enabled = tps65132_regulator_is_enabled,
  107. .list_voltage = regulator_list_voltage_linear,
  108. .map_voltage = regulator_map_voltage_linear,
  109. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  110. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  111. .set_active_discharge = regulator_set_active_discharge_regmap,
  112. };
  113. static int tps65132_of_parse_cb(struct device_node *np,
  114. const struct regulator_desc *desc,
  115. struct regulator_config *config)
  116. {
  117. struct tps65132_regulator *tps = config->driver_data;
  118. struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
  119. int ret;
  120. rpdata->en_gpiod = devm_fwnode_get_index_gpiod_from_child(tps->dev,
  121. "enable", 0, &np->fwnode, 0, "enable");
  122. if (IS_ERR(rpdata->en_gpiod)) {
  123. ret = PTR_ERR(rpdata->en_gpiod);
  124. /* Ignore the error other than probe defer */
  125. if (ret == -EPROBE_DEFER)
  126. return ret;
  127. return 0;
  128. }
  129. rpdata->act_dis_gpiod = devm_fwnode_get_index_gpiod_from_child(
  130. tps->dev, "active-discharge", 0,
  131. &np->fwnode, 0, "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 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. const struct i2c_device_id *client_id)
  191. {
  192. struct device *dev = &client->dev;
  193. struct tps65132_regulator *tps;
  194. struct regulator_config config = { };
  195. int id;
  196. int ret;
  197. tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
  198. if (!tps)
  199. return -ENOMEM;
  200. tps->rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
  201. if (IS_ERR(tps->rmap)) {
  202. ret = PTR_ERR(tps->rmap);
  203. dev_err(dev, "regmap init failed: %d\n", ret);
  204. return ret;
  205. }
  206. i2c_set_clientdata(client, tps);
  207. tps->dev = dev;
  208. for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
  209. tps->rdesc[id] = &tps_regs_desc[id];
  210. config.regmap = tps->rmap;
  211. config.dev = dev;
  212. config.driver_data = tps;
  213. tps->rdev[id] = devm_regulator_register(dev,
  214. tps->rdesc[id], &config);
  215. if (IS_ERR(tps->rdev[id])) {
  216. ret = PTR_ERR(tps->rdev[id]);
  217. dev_err(dev, "regulator %s register failed: %d\n",
  218. tps->rdesc[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 = 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");