tps65132-regulator.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_get_index_gpiod_from_child(tps->dev,
  118. "enable", 0, &np->fwnode, 0, "enable");
  119. if (IS_ERR_OR_NULL(rpdata->en_gpiod)) {
  120. ret = PTR_ERR(rpdata->en_gpiod);
  121. /* Ignore the error other than probe defer */
  122. if (ret == -EPROBE_DEFER)
  123. return ret;
  124. return 0;
  125. }
  126. rpdata->act_dis_gpiod = devm_fwnode_get_index_gpiod_from_child(
  127. tps->dev, "active-discharge", 0,
  128. &np->fwnode, 0, "active-discharge");
  129. if (IS_ERR_OR_NULL(rpdata->act_dis_gpiod)) {
  130. ret = PTR_ERR(rpdata->act_dis_gpiod);
  131. /* Ignore the error other than probe defer */
  132. if (ret == -EPROBE_DEFER)
  133. return ret;
  134. return 0;
  135. }
  136. ret = of_property_read_u32(np, "ti,active-discharge-time-us",
  137. &rpdata->act_dis_time_us);
  138. if (ret < 0) {
  139. dev_err(tps->dev, "Failed to read active discharge time:%d\n",
  140. ret);
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. #define TPS65132_REGULATOR_DESC(_id, _name) \
  146. [TPS65132_REGULATOR_ID_##_id] = { \
  147. .name = "tps65132-"#_name, \
  148. .supply_name = "vin", \
  149. .id = TPS65132_REGULATOR_ID_##_id, \
  150. .of_match = of_match_ptr(#_name), \
  151. .of_parse_cb = tps65132_of_parse_cb, \
  152. .ops = &tps65132_regulator_ops, \
  153. .n_voltages = TPS65132_VOUT_N_VOLTAGE, \
  154. .min_uV = TPS65132_VOUT_VMIN, \
  155. .uV_step = TPS65132_VOUT_STEP, \
  156. .enable_time = 500, \
  157. .vsel_mask = TPS65132_VOUT_MASK, \
  158. .vsel_reg = TPS65132_REG_##_id, \
  159. .active_discharge_off = 0, \
  160. .active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
  161. .active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
  162. .active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
  163. .type = REGULATOR_VOLTAGE, \
  164. .owner = THIS_MODULE, \
  165. }
  166. static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
  167. TPS65132_REGULATOR_DESC(VPOS, outp),
  168. TPS65132_REGULATOR_DESC(VNEG, outn),
  169. };
  170. static const struct regmap_range tps65132_no_reg_ranges[] = {
  171. regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
  172. TPS65132_REG_CONTROL - 1),
  173. };
  174. static const struct regmap_access_table tps65132_no_reg_table = {
  175. .no_ranges = tps65132_no_reg_ranges,
  176. .n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
  177. };
  178. static const struct regmap_config tps65132_regmap_config = {
  179. .reg_bits = 8,
  180. .val_bits = 8,
  181. .max_register = TPS65132_REG_CONTROL,
  182. .cache_type = REGCACHE_NONE,
  183. .rd_table = &tps65132_no_reg_table,
  184. .wr_table = &tps65132_no_reg_table,
  185. };
  186. static int tps65132_probe(struct i2c_client *client,
  187. const struct i2c_device_id *client_id)
  188. {
  189. struct device *dev = &client->dev;
  190. struct tps65132_regulator *tps;
  191. struct regulator_dev *rdev;
  192. struct regmap *rmap;
  193. struct regulator_config config = { };
  194. int id;
  195. int ret;
  196. tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
  197. if (!tps)
  198. return -ENOMEM;
  199. rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
  200. if (IS_ERR(rmap)) {
  201. ret = PTR_ERR(rmap);
  202. dev_err(dev, "regmap init failed: %d\n", ret);
  203. return ret;
  204. }
  205. i2c_set_clientdata(client, tps);
  206. tps->dev = dev;
  207. for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
  208. config.regmap = rmap;
  209. config.dev = dev;
  210. config.driver_data = tps;
  211. rdev = devm_regulator_register(dev, &tps_regs_desc[id],
  212. &config);
  213. if (IS_ERR(rdev)) {
  214. ret = PTR_ERR(rdev);
  215. dev_err(dev, "regulator %s register failed: %d\n",
  216. tps_regs_desc[id].name, ret);
  217. return ret;
  218. }
  219. }
  220. return 0;
  221. }
  222. static const struct i2c_device_id tps65132_id[] = {
  223. {.name = "tps65132",},
  224. {},
  225. };
  226. MODULE_DEVICE_TABLE(i2c, tps65132_id);
  227. static struct i2c_driver tps65132_i2c_driver = {
  228. .driver = {
  229. .name = "tps65132",
  230. },
  231. .probe = tps65132_probe,
  232. .id_table = tps65132_id,
  233. };
  234. module_i2c_driver(tps65132_i2c_driver);
  235. MODULE_DESCRIPTION("tps65132 regulator driver");
  236. MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
  237. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  238. MODULE_LICENSE("GPL v2");