tps51632-regulator.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tps51632-regulator.c -- TI TPS51632
  4. *
  5. * Regulator driver for TPS51632 3-2-1 Phase D-Cap Step Down Driverless
  6. * Controller with serial VID control and DVFS.
  7. *
  8. * Copyright (c) 2012, NVIDIA Corporation.
  9. *
  10. * Author: Laxman Dewangan <[email protected]>
  11. */
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #include <linux/regulator/tps51632-regulator.h>
  25. #include <linux/slab.h>
  26. /* Register definitions */
  27. #define TPS51632_VOLTAGE_SELECT_REG 0x0
  28. #define TPS51632_VOLTAGE_BASE_REG 0x1
  29. #define TPS51632_OFFSET_REG 0x2
  30. #define TPS51632_IMON_REG 0x3
  31. #define TPS51632_VMAX_REG 0x4
  32. #define TPS51632_DVFS_CONTROL_REG 0x5
  33. #define TPS51632_POWER_STATE_REG 0x6
  34. #define TPS51632_SLEW_REGS 0x7
  35. #define TPS51632_FAULT_REG 0x14
  36. #define TPS51632_MAX_REG 0x15
  37. #define TPS51632_VOUT_MASK 0x7F
  38. #define TPS51632_VOUT_OFFSET_MASK 0x1F
  39. #define TPS51632_VMAX_MASK 0x7F
  40. #define TPS51632_VMAX_LOCK 0x80
  41. /* TPS51632_DVFS_CONTROL_REG */
  42. #define TPS51632_DVFS_PWMEN 0x1
  43. #define TPS51632_DVFS_STEP_20 0x2
  44. #define TPS51632_DVFS_VMAX_PG 0x4
  45. #define TPS51632_DVFS_PWMRST 0x8
  46. #define TPS51632_DVFS_OCA_EN 0x10
  47. #define TPS51632_DVFS_FCCM 0x20
  48. /* TPS51632_POWER_STATE_REG */
  49. #define TPS51632_POWER_STATE_MASK 0x03
  50. #define TPS51632_POWER_STATE_MULTI_PHASE_CCM 0x0
  51. #define TPS51632_POWER_STATE_SINGLE_PHASE_CCM 0x1
  52. #define TPS51632_POWER_STATE_SINGLE_PHASE_DCM 0x2
  53. #define TPS51632_MIN_VOLTAGE 500000
  54. #define TPS51632_MAX_VOLTAGE 1520000
  55. #define TPS51632_VOLTAGE_STEP_10mV 10000
  56. #define TPS51632_VOLTAGE_STEP_20mV 20000
  57. #define TPS51632_MAX_VSEL 0x7F
  58. #define TPS51632_MIN_VSEL 0x19
  59. #define TPS51632_DEFAULT_RAMP_DELAY 6000
  60. #define TPS51632_VOLT_VSEL(uV) \
  61. (DIV_ROUND_UP(uV - TPS51632_MIN_VOLTAGE, \
  62. TPS51632_VOLTAGE_STEP_10mV) + \
  63. TPS51632_MIN_VSEL)
  64. /* TPS51632 chip information */
  65. struct tps51632_chip {
  66. struct device *dev;
  67. struct regulator_desc desc;
  68. struct regulator_dev *rdev;
  69. struct regmap *regmap;
  70. };
  71. static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
  72. int ramp_delay)
  73. {
  74. struct tps51632_chip *tps = rdev_get_drvdata(rdev);
  75. int bit;
  76. int ret;
  77. if (ramp_delay == 0)
  78. bit = 0;
  79. else
  80. bit = DIV_ROUND_UP(ramp_delay, 6000) - 1;
  81. ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
  82. if (ret < 0)
  83. dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
  84. return ret;
  85. }
  86. static const struct regulator_ops tps51632_dcdc_ops = {
  87. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  88. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  89. .list_voltage = regulator_list_voltage_linear,
  90. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  91. .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
  92. };
  93. static int tps51632_init_dcdc(struct tps51632_chip *tps,
  94. struct tps51632_regulator_platform_data *pdata)
  95. {
  96. int ret;
  97. uint8_t control = 0;
  98. int vsel;
  99. if (!pdata->enable_pwm_dvfs)
  100. goto skip_pwm_config;
  101. control |= TPS51632_DVFS_PWMEN;
  102. vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
  103. ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
  104. if (ret < 0) {
  105. dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
  106. return ret;
  107. }
  108. if (pdata->dvfs_step_20mV)
  109. control |= TPS51632_DVFS_STEP_20;
  110. if (pdata->max_voltage_uV) {
  111. unsigned int vmax;
  112. /**
  113. * TPS51632 hw behavior: VMAX register can be write only
  114. * once as it get locked after first write. The lock get
  115. * reset only when device is power-reset.
  116. * Write register only when lock bit is not enabled.
  117. */
  118. ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
  119. if (ret < 0) {
  120. dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
  121. return ret;
  122. }
  123. if (!(vmax & TPS51632_VMAX_LOCK)) {
  124. vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
  125. ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
  126. vsel);
  127. if (ret < 0) {
  128. dev_err(tps->dev,
  129. "VMAX write failed, err %d\n", ret);
  130. return ret;
  131. }
  132. }
  133. }
  134. skip_pwm_config:
  135. ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
  136. if (ret < 0)
  137. dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
  138. return ret;
  139. }
  140. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  141. {
  142. switch (reg) {
  143. case TPS51632_OFFSET_REG:
  144. case TPS51632_FAULT_REG:
  145. case TPS51632_IMON_REG:
  146. return true;
  147. default:
  148. return false;
  149. }
  150. }
  151. static bool is_read_reg(struct device *dev, unsigned int reg)
  152. {
  153. switch (reg) {
  154. case 0x08 ... 0x0F:
  155. return false;
  156. default:
  157. return true;
  158. }
  159. }
  160. static bool is_write_reg(struct device *dev, unsigned int reg)
  161. {
  162. switch (reg) {
  163. case TPS51632_VOLTAGE_SELECT_REG:
  164. case TPS51632_VOLTAGE_BASE_REG:
  165. case TPS51632_VMAX_REG:
  166. case TPS51632_DVFS_CONTROL_REG:
  167. case TPS51632_POWER_STATE_REG:
  168. case TPS51632_SLEW_REGS:
  169. return true;
  170. default:
  171. return false;
  172. }
  173. }
  174. static const struct regmap_config tps51632_regmap_config = {
  175. .reg_bits = 8,
  176. .val_bits = 8,
  177. .writeable_reg = is_write_reg,
  178. .readable_reg = is_read_reg,
  179. .volatile_reg = is_volatile_reg,
  180. .max_register = TPS51632_MAX_REG - 1,
  181. .cache_type = REGCACHE_RBTREE,
  182. };
  183. #if defined(CONFIG_OF)
  184. static const struct of_device_id tps51632_of_match[] = {
  185. { .compatible = "ti,tps51632",},
  186. {},
  187. };
  188. MODULE_DEVICE_TABLE(of, tps51632_of_match);
  189. static struct tps51632_regulator_platform_data *
  190. of_get_tps51632_platform_data(struct device *dev,
  191. const struct regulator_desc *desc)
  192. {
  193. struct tps51632_regulator_platform_data *pdata;
  194. struct device_node *np = dev->of_node;
  195. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  196. if (!pdata)
  197. return NULL;
  198. pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
  199. desc);
  200. if (!pdata->reg_init_data) {
  201. dev_err(dev, "Not able to get OF regulator init data\n");
  202. return NULL;
  203. }
  204. pdata->enable_pwm_dvfs =
  205. of_property_read_bool(np, "ti,enable-pwm-dvfs");
  206. pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
  207. pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
  208. TPS51632_MIN_VOLTAGE;
  209. pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
  210. TPS51632_MAX_VOLTAGE;
  211. return pdata;
  212. }
  213. #else
  214. static struct tps51632_regulator_platform_data *
  215. of_get_tps51632_platform_data(struct device *dev,
  216. const struct regulator_desc *desc)
  217. {
  218. return NULL;
  219. }
  220. #endif
  221. static int tps51632_probe(struct i2c_client *client,
  222. const struct i2c_device_id *id)
  223. {
  224. struct tps51632_regulator_platform_data *pdata;
  225. struct regulator_dev *rdev;
  226. struct tps51632_chip *tps;
  227. int ret;
  228. struct regulator_config config = { };
  229. if (client->dev.of_node) {
  230. const struct of_device_id *match;
  231. match = of_match_device(of_match_ptr(tps51632_of_match),
  232. &client->dev);
  233. if (!match) {
  234. dev_err(&client->dev, "Error: No device match found\n");
  235. return -ENODEV;
  236. }
  237. }
  238. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  239. if (!tps)
  240. return -ENOMEM;
  241. tps->dev = &client->dev;
  242. tps->desc.name = client->name;
  243. tps->desc.id = 0;
  244. tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
  245. tps->desc.min_uV = TPS51632_MIN_VOLTAGE;
  246. tps->desc.uV_step = TPS51632_VOLTAGE_STEP_10mV;
  247. tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
  248. tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
  249. tps->desc.ops = &tps51632_dcdc_ops;
  250. tps->desc.type = REGULATOR_VOLTAGE;
  251. tps->desc.owner = THIS_MODULE;
  252. pdata = dev_get_platdata(&client->dev);
  253. if (!pdata && client->dev.of_node)
  254. pdata = of_get_tps51632_platform_data(&client->dev, &tps->desc);
  255. if (!pdata) {
  256. dev_err(&client->dev, "No Platform data\n");
  257. return -EINVAL;
  258. }
  259. if (pdata->enable_pwm_dvfs) {
  260. if ((pdata->base_voltage_uV < TPS51632_MIN_VOLTAGE) ||
  261. (pdata->base_voltage_uV > TPS51632_MAX_VOLTAGE)) {
  262. dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
  263. return -EINVAL;
  264. }
  265. if ((pdata->max_voltage_uV) &&
  266. ((pdata->max_voltage_uV < TPS51632_MIN_VOLTAGE) ||
  267. (pdata->max_voltage_uV > TPS51632_MAX_VOLTAGE))) {
  268. dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
  269. return -EINVAL;
  270. }
  271. }
  272. if (pdata->enable_pwm_dvfs)
  273. tps->desc.vsel_reg = TPS51632_VOLTAGE_BASE_REG;
  274. else
  275. tps->desc.vsel_reg = TPS51632_VOLTAGE_SELECT_REG;
  276. tps->desc.vsel_mask = TPS51632_VOUT_MASK;
  277. tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
  278. if (IS_ERR(tps->regmap)) {
  279. ret = PTR_ERR(tps->regmap);
  280. dev_err(&client->dev, "regmap init failed, err %d\n", ret);
  281. return ret;
  282. }
  283. i2c_set_clientdata(client, tps);
  284. ret = tps51632_init_dcdc(tps, pdata);
  285. if (ret < 0) {
  286. dev_err(tps->dev, "Init failed, err = %d\n", ret);
  287. return ret;
  288. }
  289. /* Register the regulators */
  290. config.dev = &client->dev;
  291. config.init_data = pdata->reg_init_data;
  292. config.driver_data = tps;
  293. config.regmap = tps->regmap;
  294. config.of_node = client->dev.of_node;
  295. rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
  296. if (IS_ERR(rdev)) {
  297. dev_err(tps->dev, "regulator register failed\n");
  298. return PTR_ERR(rdev);
  299. }
  300. tps->rdev = rdev;
  301. return 0;
  302. }
  303. static const struct i2c_device_id tps51632_id[] = {
  304. {.name = "tps51632",},
  305. {},
  306. };
  307. MODULE_DEVICE_TABLE(i2c, tps51632_id);
  308. static struct i2c_driver tps51632_i2c_driver = {
  309. .driver = {
  310. .name = "tps51632",
  311. .of_match_table = of_match_ptr(tps51632_of_match),
  312. },
  313. .probe = tps51632_probe,
  314. .id_table = tps51632_id,
  315. };
  316. static int __init tps51632_init(void)
  317. {
  318. return i2c_add_driver(&tps51632_i2c_driver);
  319. }
  320. subsys_initcall(tps51632_init);
  321. static void __exit tps51632_cleanup(void)
  322. {
  323. i2c_del_driver(&tps51632_i2c_driver);
  324. }
  325. module_exit(tps51632_cleanup);
  326. MODULE_AUTHOR("Laxman Dewangan <[email protected]>");
  327. MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
  328. MODULE_LICENSE("GPL v2");