tps80031-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // tps80031-regulator.c -- TI TPS80031 regulator driver.
  4. //
  5. // Regulator driver for TI TPS80031/TPS80032 Fully Integrated Power
  6. // Management with Power Path and Battery Charger.
  7. //
  8. // Copyright (c) 2012, NVIDIA Corporation.
  9. //
  10. // Author: Laxman Dewangan <ldewangan@nvidia.com>
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mfd/tps80031.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/slab.h>
  21. /* Flags for DCDC Voltage reading */
  22. #define DCDC_OFFSET_EN BIT(0)
  23. #define DCDC_EXTENDED_EN BIT(1)
  24. #define TRACK_MODE_ENABLE BIT(2)
  25. #define SMPS_MULTOFFSET_VIO BIT(1)
  26. #define SMPS_MULTOFFSET_SMPS1 BIT(3)
  27. #define SMPS_MULTOFFSET_SMPS2 BIT(4)
  28. #define SMPS_MULTOFFSET_SMPS3 BIT(6)
  29. #define SMPS_MULTOFFSET_SMPS4 BIT(0)
  30. #define SMPS_CMD_MASK 0xC0
  31. #define SMPS_VSEL_MASK 0x3F
  32. #define LDO_VSEL_MASK 0x1F
  33. #define LDO_TRACK_VSEL_MASK 0x3F
  34. #define MISC2_LDOUSB_IN_VSYS BIT(4)
  35. #define MISC2_LDOUSB_IN_PMID BIT(3)
  36. #define MISC2_LDOUSB_IN_MASK 0x18
  37. #define MISC2_LDO3_SEL_VIB_VAL BIT(0)
  38. #define MISC2_LDO3_SEL_VIB_MASK 0x1
  39. #define BOOST_HW_PWR_EN BIT(5)
  40. #define BOOST_HW_PWR_EN_MASK BIT(5)
  41. #define OPA_MODE_EN BIT(6)
  42. #define OPA_MODE_EN_MASK BIT(6)
  43. #define USB_VBUS_CTRL_SET 0x04
  44. #define USB_VBUS_CTRL_CLR 0x05
  45. #define VBUS_DISCHRG 0x20
  46. struct tps80031_regulator_info {
  47. /* Regulator register address.*/
  48. u8 trans_reg;
  49. u8 state_reg;
  50. u8 force_reg;
  51. u8 volt_reg;
  52. u8 volt_id;
  53. /*Power request bits */
  54. int preq_bit;
  55. /* used by regulator core */
  56. struct regulator_desc desc;
  57. };
  58. struct tps80031_regulator {
  59. struct device *dev;
  60. struct tps80031_regulator_info *rinfo;
  61. u8 device_flags;
  62. unsigned int config_flags;
  63. unsigned int ext_ctrl_flag;
  64. };
  65. static inline struct device *to_tps80031_dev(struct regulator_dev *rdev)
  66. {
  67. return rdev_get_dev(rdev)->parent->parent;
  68. }
  69. static int tps80031_reg_is_enabled(struct regulator_dev *rdev)
  70. {
  71. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  72. struct device *parent = to_tps80031_dev(rdev);
  73. u8 reg_val;
  74. int ret;
  75. if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
  76. return true;
  77. ret = tps80031_read(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
  78. &reg_val);
  79. if (ret < 0) {
  80. dev_err(&rdev->dev, "Reg 0x%02x read failed, err = %d\n",
  81. ri->rinfo->state_reg, ret);
  82. return ret;
  83. }
  84. return (reg_val & TPS80031_STATE_MASK) == TPS80031_STATE_ON;
  85. }
  86. static int tps80031_reg_enable(struct regulator_dev *rdev)
  87. {
  88. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  89. struct device *parent = to_tps80031_dev(rdev);
  90. int ret;
  91. if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
  92. return 0;
  93. ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
  94. TPS80031_STATE_ON, TPS80031_STATE_MASK);
  95. if (ret < 0) {
  96. dev_err(&rdev->dev, "Reg 0x%02x update failed, err = %d\n",
  97. ri->rinfo->state_reg, ret);
  98. return ret;
  99. }
  100. return ret;
  101. }
  102. static int tps80031_reg_disable(struct regulator_dev *rdev)
  103. {
  104. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  105. struct device *parent = to_tps80031_dev(rdev);
  106. int ret;
  107. if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
  108. return 0;
  109. ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
  110. TPS80031_STATE_OFF, TPS80031_STATE_MASK);
  111. if (ret < 0)
  112. dev_err(&rdev->dev, "Reg 0x%02x update failed, err = %d\n",
  113. ri->rinfo->state_reg, ret);
  114. return ret;
  115. }
  116. /* DCDC voltages for the selector of 58 to 63 */
  117. static const int tps80031_dcdc_voltages[4][5] = {
  118. { 1350, 1500, 1800, 1900, 2100},
  119. { 1350, 1500, 1800, 1900, 2100},
  120. { 2084, 2315, 2778, 2932, 3241},
  121. { 4167, 2315, 2778, 2932, 3241},
  122. };
  123. static int tps80031_dcdc_list_voltage(struct regulator_dev *rdev, unsigned sel)
  124. {
  125. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  126. int volt_index = ri->device_flags & 0x3;
  127. if (sel == 0)
  128. return 0;
  129. else if (sel < 58)
  130. return regulator_list_voltage_linear(rdev, sel - 1);
  131. else
  132. return tps80031_dcdc_voltages[volt_index][sel - 58] * 1000;
  133. }
  134. static int tps80031_dcdc_set_voltage_sel(struct regulator_dev *rdev,
  135. unsigned vsel)
  136. {
  137. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  138. struct device *parent = to_tps80031_dev(rdev);
  139. int ret;
  140. u8 reg_val;
  141. if (ri->rinfo->force_reg) {
  142. ret = tps80031_read(parent, ri->rinfo->volt_id,
  143. ri->rinfo->force_reg, &reg_val);
  144. if (ret < 0) {
  145. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  146. ri->rinfo->force_reg, ret);
  147. return ret;
  148. }
  149. if (!(reg_val & SMPS_CMD_MASK)) {
  150. ret = tps80031_update(parent, ri->rinfo->volt_id,
  151. ri->rinfo->force_reg, vsel, SMPS_VSEL_MASK);
  152. if (ret < 0)
  153. dev_err(ri->dev,
  154. "reg 0x%02x update failed, e = %d\n",
  155. ri->rinfo->force_reg, ret);
  156. return ret;
  157. }
  158. }
  159. ret = tps80031_update(parent, ri->rinfo->volt_id,
  160. ri->rinfo->volt_reg, vsel, SMPS_VSEL_MASK);
  161. if (ret < 0)
  162. dev_err(ri->dev, "reg 0x%02x update failed, e = %d\n",
  163. ri->rinfo->volt_reg, ret);
  164. return ret;
  165. }
  166. static int tps80031_dcdc_get_voltage_sel(struct regulator_dev *rdev)
  167. {
  168. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  169. struct device *parent = to_tps80031_dev(rdev);
  170. uint8_t vsel = 0;
  171. int ret;
  172. if (ri->rinfo->force_reg) {
  173. ret = tps80031_read(parent, ri->rinfo->volt_id,
  174. ri->rinfo->force_reg, &vsel);
  175. if (ret < 0) {
  176. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  177. ri->rinfo->force_reg, ret);
  178. return ret;
  179. }
  180. if (!(vsel & SMPS_CMD_MASK))
  181. return vsel & SMPS_VSEL_MASK;
  182. }
  183. ret = tps80031_read(parent, ri->rinfo->volt_id,
  184. ri->rinfo->volt_reg, &vsel);
  185. if (ret < 0) {
  186. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  187. ri->rinfo->volt_reg, ret);
  188. return ret;
  189. }
  190. return vsel & SMPS_VSEL_MASK;
  191. }
  192. static int tps80031_ldo_list_voltage(struct regulator_dev *rdev,
  193. unsigned int sel)
  194. {
  195. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  196. struct device *parent = to_tps80031_dev(rdev);
  197. /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
  198. if ((ri->rinfo->desc.id == TPS80031_REGULATOR_LDO2) &&
  199. (ri->device_flags & TRACK_MODE_ENABLE)) {
  200. unsigned nvsel = (sel) & 0x1F;
  201. if (((tps80031_get_chip_info(parent) == TPS80031) ||
  202. ((tps80031_get_chip_info(parent) == TPS80032) &&
  203. (tps80031_get_pmu_version(parent) == 0x0))) &&
  204. ((nvsel == 0x0) || (nvsel >= 0x19 && nvsel <= 0x1F))) {
  205. dev_err(ri->dev,
  206. "Invalid sel %d in track mode LDO2\n",
  207. nvsel);
  208. return -EINVAL;
  209. }
  210. }
  211. return regulator_list_voltage_linear(rdev, sel);
  212. }
  213. static int tps80031_ldo_map_voltage(struct regulator_dev *rdev,
  214. int min_uV, int max_uV)
  215. {
  216. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  217. struct device *parent = to_tps80031_dev(rdev);
  218. /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
  219. if ((ri->rinfo->desc.id == TPS80031_REGULATOR_LDO2) &&
  220. (ri->device_flags & TRACK_MODE_ENABLE)) {
  221. if (((tps80031_get_chip_info(parent) == TPS80031) ||
  222. ((tps80031_get_chip_info(parent) == TPS80032) &&
  223. (tps80031_get_pmu_version(parent) == 0x0)))) {
  224. return regulator_map_voltage_iterate(rdev, min_uV,
  225. max_uV);
  226. }
  227. }
  228. return regulator_map_voltage_linear(rdev, min_uV, max_uV);
  229. }
  230. static int tps80031_vbus_is_enabled(struct regulator_dev *rdev)
  231. {
  232. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  233. struct device *parent = to_tps80031_dev(rdev);
  234. int ret = -EIO;
  235. uint8_t ctrl1 = 0;
  236. uint8_t ctrl3 = 0;
  237. ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
  238. TPS80031_CHARGERUSB_CTRL1, &ctrl1);
  239. if (ret < 0) {
  240. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  241. TPS80031_CHARGERUSB_CTRL1, ret);
  242. return ret;
  243. }
  244. ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
  245. TPS80031_CHARGERUSB_CTRL3, &ctrl3);
  246. if (ret < 0) {
  247. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  248. TPS80031_CHARGERUSB_CTRL3, ret);
  249. return ret;
  250. }
  251. if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
  252. return 1;
  253. return ret;
  254. }
  255. static int tps80031_vbus_enable(struct regulator_dev *rdev)
  256. {
  257. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  258. struct device *parent = to_tps80031_dev(rdev);
  259. int ret;
  260. ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
  261. TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN);
  262. if (ret < 0) {
  263. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  264. TPS80031_CHARGERUSB_CTRL1, ret);
  265. return ret;
  266. }
  267. ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
  268. TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
  269. if (ret < 0) {
  270. dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
  271. TPS80031_CHARGERUSB_CTRL3, ret);
  272. return ret;
  273. }
  274. return ret;
  275. }
  276. static int tps80031_vbus_disable(struct regulator_dev *rdev)
  277. {
  278. struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
  279. struct device *parent = to_tps80031_dev(rdev);
  280. int ret = 0;
  281. if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
  282. ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
  283. USB_VBUS_CTRL_SET, VBUS_DISCHRG);
  284. if (ret < 0) {
  285. dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
  286. USB_VBUS_CTRL_SET, ret);
  287. return ret;
  288. }
  289. }
  290. ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
  291. TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN);
  292. if (ret < 0) {
  293. dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
  294. TPS80031_CHARGERUSB_CTRL1, ret);
  295. return ret;
  296. }
  297. ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
  298. TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
  299. if (ret < 0) {
  300. dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
  301. TPS80031_CHARGERUSB_CTRL3, ret);
  302. return ret;
  303. }
  304. mdelay(DIV_ROUND_UP(ri->rinfo->desc.enable_time, 1000));
  305. if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
  306. ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
  307. USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
  308. if (ret < 0) {
  309. dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
  310. USB_VBUS_CTRL_CLR, ret);
  311. return ret;
  312. }
  313. }
  314. return ret;
  315. }
  316. static const struct regulator_ops tps80031_dcdc_ops = {
  317. .list_voltage = tps80031_dcdc_list_voltage,
  318. .set_voltage_sel = tps80031_dcdc_set_voltage_sel,
  319. .get_voltage_sel = tps80031_dcdc_get_voltage_sel,
  320. .enable = tps80031_reg_enable,
  321. .disable = tps80031_reg_disable,
  322. .is_enabled = tps80031_reg_is_enabled,
  323. };
  324. static const struct regulator_ops tps80031_ldo_ops = {
  325. .list_voltage = tps80031_ldo_list_voltage,
  326. .map_voltage = tps80031_ldo_map_voltage,
  327. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  328. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  329. .enable = tps80031_reg_enable,
  330. .disable = tps80031_reg_disable,
  331. .is_enabled = tps80031_reg_is_enabled,
  332. };
  333. static const struct regulator_ops tps80031_vbus_sw_ops = {
  334. .list_voltage = regulator_list_voltage_linear,
  335. .enable = tps80031_vbus_enable,
  336. .disable = tps80031_vbus_disable,
  337. .is_enabled = tps80031_vbus_is_enabled,
  338. };
  339. static const struct regulator_ops tps80031_vbus_hw_ops = {
  340. .list_voltage = regulator_list_voltage_linear,
  341. };
  342. static const struct regulator_ops tps80031_ext_reg_ops = {
  343. .list_voltage = regulator_list_voltage_linear,
  344. .enable = tps80031_reg_enable,
  345. .disable = tps80031_reg_disable,
  346. .is_enabled = tps80031_reg_is_enabled,
  347. };
  348. /* Non-exiting default definition for some register */
  349. #define TPS80031_SMPS3_CFG_FORCE 0
  350. #define TPS80031_SMPS4_CFG_FORCE 0
  351. #define TPS80031_VBUS_CFG_TRANS 0
  352. #define TPS80031_VBUS_CFG_STATE 0
  353. #define TPS80031_REG_SMPS(_id, _volt_id, _pbit) \
  354. { \
  355. .trans_reg = TPS80031_##_id##_CFG_TRANS, \
  356. .state_reg = TPS80031_##_id##_CFG_STATE, \
  357. .force_reg = TPS80031_##_id##_CFG_FORCE, \
  358. .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \
  359. .volt_id = TPS80031_SLAVE_##_volt_id, \
  360. .preq_bit = _pbit, \
  361. .desc = { \
  362. .name = "tps80031_"#_id, \
  363. .id = TPS80031_REGULATOR_##_id, \
  364. .n_voltages = 63, \
  365. .ops = &tps80031_dcdc_ops, \
  366. .type = REGULATOR_VOLTAGE, \
  367. .owner = THIS_MODULE, \
  368. .enable_time = 500, \
  369. }, \
  370. }
  371. #define TPS80031_REG_LDO(_id, _preq_bit) \
  372. { \
  373. .trans_reg = TPS80031_##_id##_CFG_TRANS, \
  374. .state_reg = TPS80031_##_id##_CFG_STATE, \
  375. .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \
  376. .volt_id = TPS80031_SLAVE_ID1, \
  377. .preq_bit = _preq_bit, \
  378. .desc = { \
  379. .owner = THIS_MODULE, \
  380. .name = "tps80031_"#_id, \
  381. .id = TPS80031_REGULATOR_##_id, \
  382. .ops = &tps80031_ldo_ops, \
  383. .type = REGULATOR_VOLTAGE, \
  384. .min_uV = 1000000, \
  385. .uV_step = 100000, \
  386. .linear_min_sel = 1, \
  387. .n_voltages = 25, \
  388. .vsel_reg = TPS80031_##_id##_CFG_VOLTAGE, \
  389. .vsel_mask = LDO_VSEL_MASK, \
  390. .enable_time = 500, \
  391. }, \
  392. }
  393. #define TPS80031_REG_FIXED(_id, max_mV, _ops, _delay, _pbit) \
  394. { \
  395. .trans_reg = TPS80031_##_id##_CFG_TRANS, \
  396. .state_reg = TPS80031_##_id##_CFG_STATE, \
  397. .volt_id = TPS80031_SLAVE_ID1, \
  398. .preq_bit = _pbit, \
  399. .desc = { \
  400. .name = "tps80031_"#_id, \
  401. .id = TPS80031_REGULATOR_##_id, \
  402. .min_uV = max_mV * 1000, \
  403. .n_voltages = 1, \
  404. .ops = &_ops, \
  405. .type = REGULATOR_VOLTAGE, \
  406. .owner = THIS_MODULE, \
  407. .enable_time = _delay, \
  408. }, \
  409. }
  410. static struct tps80031_regulator_info tps80031_rinfo[TPS80031_REGULATOR_MAX] = {
  411. TPS80031_REG_SMPS(VIO, ID0, 4),
  412. TPS80031_REG_SMPS(SMPS1, ID0, 0),
  413. TPS80031_REG_SMPS(SMPS2, ID0, 1),
  414. TPS80031_REG_SMPS(SMPS3, ID1, 2),
  415. TPS80031_REG_SMPS(SMPS4, ID1, 3),
  416. TPS80031_REG_LDO(VANA, -1),
  417. TPS80031_REG_LDO(LDO1, 8),
  418. TPS80031_REG_LDO(LDO2, 9),
  419. TPS80031_REG_LDO(LDO3, 10),
  420. TPS80031_REG_LDO(LDO4, 11),
  421. TPS80031_REG_LDO(LDO5, 12),
  422. TPS80031_REG_LDO(LDO6, 13),
  423. TPS80031_REG_LDO(LDO7, 14),
  424. TPS80031_REG_LDO(LDOLN, 15),
  425. TPS80031_REG_LDO(LDOUSB, 5),
  426. TPS80031_REG_FIXED(VBUS, 5000, tps80031_vbus_hw_ops, 100000, -1),
  427. TPS80031_REG_FIXED(REGEN1, 3300, tps80031_ext_reg_ops, 0, 16),
  428. TPS80031_REG_FIXED(REGEN2, 3300, tps80031_ext_reg_ops, 0, 17),
  429. TPS80031_REG_FIXED(SYSEN, 3300, tps80031_ext_reg_ops, 0, 18),
  430. };
  431. static int tps80031_power_req_config(struct device *parent,
  432. struct tps80031_regulator *ri,
  433. struct tps80031_regulator_platform_data *tps80031_pdata)
  434. {
  435. int ret = 0;
  436. if (ri->rinfo->preq_bit < 0)
  437. goto skip_pwr_req_config;
  438. ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag,
  439. ri->rinfo->preq_bit, ri->rinfo->state_reg,
  440. ri->rinfo->trans_reg);
  441. if (ret < 0) {
  442. dev_err(ri->dev, "ext powerreq config failed, err = %d\n", ret);
  443. return ret;
  444. }
  445. skip_pwr_req_config:
  446. if (tps80031_pdata->ext_ctrl_flag & TPS80031_PWR_ON_ON_SLEEP) {
  447. ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
  448. ri->rinfo->trans_reg, TPS80031_TRANS_SLEEP_ON,
  449. TPS80031_TRANS_SLEEP_MASK);
  450. if (ret < 0) {
  451. dev_err(ri->dev, "Reg 0x%02x update failed, e %d\n",
  452. ri->rinfo->trans_reg, ret);
  453. return ret;
  454. }
  455. }
  456. return ret;
  457. }
  458. static int tps80031_regulator_config(struct device *parent,
  459. struct tps80031_regulator *ri,
  460. struct tps80031_regulator_platform_data *tps80031_pdata)
  461. {
  462. int ret = 0;
  463. switch (ri->rinfo->desc.id) {
  464. case TPS80031_REGULATOR_LDOUSB:
  465. if (ri->config_flags & (TPS80031_USBLDO_INPUT_VSYS |
  466. TPS80031_USBLDO_INPUT_PMID)) {
  467. unsigned val = 0;
  468. if (ri->config_flags & TPS80031_USBLDO_INPUT_VSYS)
  469. val = MISC2_LDOUSB_IN_VSYS;
  470. else
  471. val = MISC2_LDOUSB_IN_PMID;
  472. ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
  473. TPS80031_MISC2, val,
  474. MISC2_LDOUSB_IN_MASK);
  475. if (ret < 0) {
  476. dev_err(ri->dev,
  477. "LDOUSB config failed, e= %d\n", ret);
  478. return ret;
  479. }
  480. }
  481. break;
  482. case TPS80031_REGULATOR_LDO3:
  483. if (ri->config_flags & TPS80031_LDO3_OUTPUT_VIB) {
  484. ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
  485. TPS80031_MISC2, MISC2_LDO3_SEL_VIB_VAL,
  486. MISC2_LDO3_SEL_VIB_MASK);
  487. if (ret < 0) {
  488. dev_err(ri->dev,
  489. "LDO3 config failed, e = %d\n", ret);
  490. return ret;
  491. }
  492. }
  493. break;
  494. case TPS80031_REGULATOR_VBUS:
  495. /* Provide SW control Ops if VBUS is SW control */
  496. if (!(ri->config_flags & TPS80031_VBUS_SW_ONLY))
  497. ri->rinfo->desc.ops = &tps80031_vbus_sw_ops;
  498. break;
  499. default:
  500. break;
  501. }
  502. /* Configure Active state to ON, SLEEP to OFF and OFF_state to OFF */
  503. ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->trans_reg,
  504. TPS80031_TRANS_ACTIVE_ON | TPS80031_TRANS_SLEEP_OFF |
  505. TPS80031_TRANS_OFF_OFF, TPS80031_TRANS_ACTIVE_MASK |
  506. TPS80031_TRANS_SLEEP_MASK | TPS80031_TRANS_OFF_MASK);
  507. if (ret < 0) {
  508. dev_err(ri->dev, "trans reg update failed, e %d\n", ret);
  509. return ret;
  510. }
  511. return ret;
  512. }
  513. static int check_smps_mode_mult(struct device *parent,
  514. struct tps80031_regulator *ri)
  515. {
  516. int mult_offset;
  517. int ret;
  518. u8 smps_offset;
  519. u8 smps_mult;
  520. ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
  521. TPS80031_SMPS_OFFSET, &smps_offset);
  522. if (ret < 0) {
  523. dev_err(parent, "Error in reading smps offset register\n");
  524. return ret;
  525. }
  526. ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
  527. TPS80031_SMPS_MULT, &smps_mult);
  528. if (ret < 0) {
  529. dev_err(parent, "Error in reading smps mult register\n");
  530. return ret;
  531. }
  532. switch (ri->rinfo->desc.id) {
  533. case TPS80031_REGULATOR_VIO:
  534. mult_offset = SMPS_MULTOFFSET_VIO;
  535. break;
  536. case TPS80031_REGULATOR_SMPS1:
  537. mult_offset = SMPS_MULTOFFSET_SMPS1;
  538. break;
  539. case TPS80031_REGULATOR_SMPS2:
  540. mult_offset = SMPS_MULTOFFSET_SMPS2;
  541. break;
  542. case TPS80031_REGULATOR_SMPS3:
  543. mult_offset = SMPS_MULTOFFSET_SMPS3;
  544. break;
  545. case TPS80031_REGULATOR_SMPS4:
  546. mult_offset = SMPS_MULTOFFSET_SMPS4;
  547. break;
  548. case TPS80031_REGULATOR_LDO2:
  549. ri->device_flags = smps_mult & BIT(5) ? TRACK_MODE_ENABLE : 0;
  550. /* TRACK mode the ldo2 varies from 600mV to 1300mV */
  551. if (ri->device_flags & TRACK_MODE_ENABLE) {
  552. ri->rinfo->desc.min_uV = 600000;
  553. ri->rinfo->desc.uV_step = 12500;
  554. ri->rinfo->desc.n_voltages = 57;
  555. ri->rinfo->desc.vsel_mask = LDO_TRACK_VSEL_MASK;
  556. }
  557. return 0;
  558. default:
  559. return 0;
  560. }
  561. ri->device_flags = (smps_offset & mult_offset) ? DCDC_OFFSET_EN : 0;
  562. ri->device_flags |= (smps_mult & mult_offset) ? DCDC_EXTENDED_EN : 0;
  563. switch (ri->device_flags) {
  564. case 0:
  565. ri->rinfo->desc.min_uV = 607700;
  566. ri->rinfo->desc.uV_step = 12660;
  567. break;
  568. case DCDC_OFFSET_EN:
  569. ri->rinfo->desc.min_uV = 700000;
  570. ri->rinfo->desc.uV_step = 12500;
  571. break;
  572. case DCDC_EXTENDED_EN:
  573. ri->rinfo->desc.min_uV = 1852000;
  574. ri->rinfo->desc.uV_step = 38600;
  575. break;
  576. case DCDC_OFFSET_EN | DCDC_EXTENDED_EN:
  577. ri->rinfo->desc.min_uV = 2161000;
  578. ri->rinfo->desc.uV_step = 38600;
  579. break;
  580. }
  581. return 0;
  582. }
  583. static int tps80031_regulator_probe(struct platform_device *pdev)
  584. {
  585. struct tps80031_platform_data *pdata;
  586. struct tps80031_regulator_platform_data *tps_pdata;
  587. struct tps80031_regulator *ri;
  588. struct tps80031_regulator *pmic;
  589. struct regulator_dev *rdev;
  590. struct regulator_config config = { };
  591. struct tps80031 *tps80031_mfd = dev_get_drvdata(pdev->dev.parent);
  592. int ret;
  593. int num;
  594. pdata = dev_get_platdata(pdev->dev.parent);
  595. if (!pdata) {
  596. dev_err(&pdev->dev, "No platform data\n");
  597. return -EINVAL;
  598. }
  599. pmic = devm_kcalloc(&pdev->dev,
  600. TPS80031_REGULATOR_MAX, sizeof(*pmic), GFP_KERNEL);
  601. if (!pmic)
  602. return -ENOMEM;
  603. for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) {
  604. tps_pdata = pdata->regulator_pdata[num];
  605. ri = &pmic[num];
  606. ri->rinfo = &tps80031_rinfo[num];
  607. ri->dev = &pdev->dev;
  608. check_smps_mode_mult(pdev->dev.parent, ri);
  609. config.dev = &pdev->dev;
  610. config.init_data = NULL;
  611. config.driver_data = ri;
  612. config.regmap = tps80031_mfd->regmap[ri->rinfo->volt_id];
  613. if (tps_pdata) {
  614. config.init_data = tps_pdata->reg_init_data;
  615. ri->config_flags = tps_pdata->config_flags;
  616. ri->ext_ctrl_flag = tps_pdata->ext_ctrl_flag;
  617. ret = tps80031_regulator_config(pdev->dev.parent,
  618. ri, tps_pdata);
  619. if (ret < 0) {
  620. dev_err(&pdev->dev,
  621. "regulator config failed, e %d\n", ret);
  622. return ret;
  623. }
  624. ret = tps80031_power_req_config(pdev->dev.parent,
  625. ri, tps_pdata);
  626. if (ret < 0) {
  627. dev_err(&pdev->dev,
  628. "pwr_req config failed, err %d\n", ret);
  629. return ret;
  630. }
  631. }
  632. rdev = devm_regulator_register(&pdev->dev, &ri->rinfo->desc,
  633. &config);
  634. if (IS_ERR(rdev)) {
  635. dev_err(&pdev->dev,
  636. "register regulator failed %s\n",
  637. ri->rinfo->desc.name);
  638. return PTR_ERR(rdev);
  639. }
  640. }
  641. platform_set_drvdata(pdev, pmic);
  642. return 0;
  643. }
  644. static struct platform_driver tps80031_regulator_driver = {
  645. .driver = {
  646. .name = "tps80031-pmic",
  647. },
  648. .probe = tps80031_regulator_probe,
  649. };
  650. static int __init tps80031_regulator_init(void)
  651. {
  652. return platform_driver_register(&tps80031_regulator_driver);
  653. }
  654. subsys_initcall(tps80031_regulator_init);
  655. static void __exit tps80031_regulator_exit(void)
  656. {
  657. platform_driver_unregister(&tps80031_regulator_driver);
  658. }
  659. module_exit(tps80031_regulator_exit);
  660. MODULE_ALIAS("platform:tps80031-regulator");
  661. MODULE_DESCRIPTION("Regulator Driver for TI TPS80031/TPS80032 PMIC");
  662. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  663. MODULE_LICENSE("GPL v2");