tps80031-regulator.c 22 KB

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