tps65912-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * tps65912.c -- TI tps65912
  3. *
  4. * Copyright 2011 Texas Instruments Inc.
  5. *
  6. * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This driver is based on wm8350 implementation.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mfd/tps65912.h>
  26. /* DCDC's */
  27. #define TPS65912_REG_DCDC1 0
  28. #define TPS65912_REG_DCDC2 1
  29. #define TPS65912_REG_DCDC3 2
  30. #define TPS65912_REG_DCDC4 3
  31. /* LDOs */
  32. #define TPS65912_REG_LDO1 4
  33. #define TPS65912_REG_LDO2 5
  34. #define TPS65912_REG_LDO3 6
  35. #define TPS65912_REG_LDO4 7
  36. #define TPS65912_REG_LDO5 8
  37. #define TPS65912_REG_LDO6 9
  38. #define TPS65912_REG_LDO7 10
  39. #define TPS65912_REG_LDO8 11
  40. #define TPS65912_REG_LDO9 12
  41. #define TPS65912_REG_LDO10 13
  42. /* Number of step-down converters available */
  43. #define TPS65912_NUM_DCDC 4
  44. /* Number of LDO voltage regulators available */
  45. #define TPS65912_NUM_LDO 10
  46. /* Number of total regulators available */
  47. #define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
  48. #define TPS65912_REG_ENABLED 0x80
  49. #define OP_SELREG_MASK 0x40
  50. #define OP_SELREG_SHIFT 6
  51. struct tps_info {
  52. const char *name;
  53. };
  54. static struct tps_info tps65912_regs[] = {
  55. {
  56. .name = "DCDC1",
  57. },
  58. {
  59. .name = "DCDC2",
  60. },
  61. {
  62. .name = "DCDC3",
  63. },
  64. {
  65. .name = "DCDC4",
  66. },
  67. {
  68. .name = "LDO1",
  69. },
  70. {
  71. .name = "LDO2",
  72. },
  73. {
  74. .name = "LDO3",
  75. },
  76. {
  77. .name = "LDO4",
  78. },
  79. {
  80. .name = "LDO5",
  81. },
  82. {
  83. .name = "LDO6",
  84. },
  85. {
  86. .name = "LDO7",
  87. },
  88. {
  89. .name = "LDO8",
  90. },
  91. {
  92. .name = "LDO9",
  93. },
  94. {
  95. .name = "LDO10",
  96. },
  97. };
  98. struct tps65912_reg {
  99. struct regulator_desc desc[TPS65912_NUM_REGULATOR];
  100. struct tps65912 *mfd;
  101. struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
  102. struct tps_info *info[TPS65912_NUM_REGULATOR];
  103. /* for read/write access */
  104. struct mutex io_lock;
  105. int mode;
  106. int (*get_ctrl_reg)(int);
  107. int dcdc_range[TPS65912_NUM_DCDC];
  108. int pwm_mode_reg;
  109. int eco_reg;
  110. };
  111. static int tps65912_get_range(struct tps65912_reg *pmic, int id)
  112. {
  113. struct tps65912 *mfd = pmic->mfd;
  114. int range;
  115. switch (id) {
  116. case TPS65912_REG_DCDC1:
  117. range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
  118. break;
  119. case TPS65912_REG_DCDC2:
  120. range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
  121. break;
  122. case TPS65912_REG_DCDC3:
  123. range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
  124. break;
  125. case TPS65912_REG_DCDC4:
  126. range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
  127. break;
  128. default:
  129. return 0;
  130. }
  131. if (range >= 0)
  132. range = (range & DCDC_LIMIT_RANGE_MASK)
  133. >> DCDC_LIMIT_RANGE_SHIFT;
  134. pmic->dcdc_range[id] = range;
  135. return range;
  136. }
  137. static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
  138. {
  139. unsigned long uv;
  140. uv = ((vsel * 12500) + 500000);
  141. return uv;
  142. }
  143. static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
  144. {
  145. unsigned long uv;
  146. uv = ((vsel * 12500) + 700000);
  147. return uv;
  148. }
  149. static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
  150. {
  151. unsigned long uv;
  152. uv = ((vsel * 25000) + 500000);
  153. return uv;
  154. }
  155. static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
  156. {
  157. unsigned long uv;
  158. if (vsel == 0x3f)
  159. uv = 3800000;
  160. else
  161. uv = ((vsel * 50000) + 500000);
  162. return uv;
  163. }
  164. static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
  165. {
  166. unsigned long uv = 0;
  167. if (vsel <= 32)
  168. uv = ((vsel * 25000) + 800000);
  169. else if (vsel > 32 && vsel <= 60)
  170. uv = (((vsel - 32) * 50000) + 1600000);
  171. else if (vsel > 60)
  172. uv = (((vsel - 60) * 100000) + 3000000);
  173. return uv;
  174. }
  175. static int tps65912_get_ctrl_register(int id)
  176. {
  177. if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
  178. return id * 3 + TPS65912_DCDC1_AVS;
  179. else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
  180. return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
  181. else
  182. return -EINVAL;
  183. }
  184. static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
  185. {
  186. struct tps65912 *mfd = pmic->mfd;
  187. int opvsel;
  188. u8 reg = 0;
  189. if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
  190. opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
  191. if (opvsel & OP_SELREG_MASK)
  192. reg = id * 3 + TPS65912_DCDC1_AVS;
  193. else
  194. reg = id * 3 + TPS65912_DCDC1_OP;
  195. } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
  196. reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
  197. } else {
  198. return -EINVAL;
  199. }
  200. return reg;
  201. }
  202. static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
  203. {
  204. switch (id) {
  205. case TPS65912_REG_DCDC1:
  206. pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
  207. pmic->eco_reg = TPS65912_DCDC1_AVS;
  208. break;
  209. case TPS65912_REG_DCDC2:
  210. pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
  211. pmic->eco_reg = TPS65912_DCDC2_AVS;
  212. break;
  213. case TPS65912_REG_DCDC3:
  214. pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
  215. pmic->eco_reg = TPS65912_DCDC3_AVS;
  216. break;
  217. case TPS65912_REG_DCDC4:
  218. pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
  219. pmic->eco_reg = TPS65912_DCDC4_AVS;
  220. break;
  221. default:
  222. return -EINVAL;
  223. }
  224. return 0;
  225. }
  226. static int tps65912_reg_is_enabled(struct regulator_dev *dev)
  227. {
  228. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  229. struct tps65912 *mfd = pmic->mfd;
  230. int reg, value, id = rdev_get_id(dev);
  231. if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
  232. return -EINVAL;
  233. reg = pmic->get_ctrl_reg(id);
  234. if (reg < 0)
  235. return reg;
  236. value = tps65912_reg_read(mfd, reg);
  237. if (value < 0)
  238. return value;
  239. return value & TPS65912_REG_ENABLED;
  240. }
  241. static int tps65912_reg_enable(struct regulator_dev *dev)
  242. {
  243. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  244. struct tps65912 *mfd = pmic->mfd;
  245. int id = rdev_get_id(dev);
  246. int reg;
  247. if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
  248. return -EINVAL;
  249. reg = pmic->get_ctrl_reg(id);
  250. if (reg < 0)
  251. return reg;
  252. return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
  253. }
  254. static int tps65912_reg_disable(struct regulator_dev *dev)
  255. {
  256. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  257. struct tps65912 *mfd = pmic->mfd;
  258. int id = rdev_get_id(dev), reg;
  259. reg = pmic->get_ctrl_reg(id);
  260. if (reg < 0)
  261. return reg;
  262. return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
  263. }
  264. static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
  265. {
  266. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  267. struct tps65912 *mfd = pmic->mfd;
  268. int pwm_mode, eco, id = rdev_get_id(dev);
  269. tps65912_get_mode_regiters(pmic, id);
  270. pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
  271. eco = tps65912_reg_read(mfd, pmic->eco_reg);
  272. pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
  273. eco &= DCDC_AVS_ECO_MASK;
  274. switch (mode) {
  275. case REGULATOR_MODE_FAST:
  276. /* Verify if mode alredy set */
  277. if (pwm_mode && !eco)
  278. break;
  279. tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
  280. tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
  281. break;
  282. case REGULATOR_MODE_NORMAL:
  283. case REGULATOR_MODE_IDLE:
  284. if (!pwm_mode && !eco)
  285. break;
  286. tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
  287. tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
  288. break;
  289. case REGULATOR_MODE_STANDBY:
  290. if (!pwm_mode && eco)
  291. break;
  292. tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
  293. tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. return 0;
  299. }
  300. static unsigned int tps65912_get_mode(struct regulator_dev *dev)
  301. {
  302. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  303. struct tps65912 *mfd = pmic->mfd;
  304. int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
  305. tps65912_get_mode_regiters(pmic, id);
  306. pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
  307. eco = tps65912_reg_read(mfd, pmic->eco_reg);
  308. pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
  309. eco &= DCDC_AVS_ECO_MASK;
  310. if (pwm_mode && !eco)
  311. mode = REGULATOR_MODE_FAST;
  312. else if (!pwm_mode && !eco)
  313. mode = REGULATOR_MODE_NORMAL;
  314. else if (!pwm_mode && eco)
  315. mode = REGULATOR_MODE_STANDBY;
  316. return mode;
  317. }
  318. static int tps65912_list_voltage_dcdc(struct regulator_dev *dev,
  319. unsigned selector)
  320. {
  321. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  322. int range, voltage = 0, id = rdev_get_id(dev);
  323. if (id > TPS65912_REG_DCDC4)
  324. return -EINVAL;
  325. range = pmic->dcdc_range[id];
  326. switch (range) {
  327. case 0:
  328. /* 0.5 - 1.2875V in 12.5mV steps */
  329. voltage = tps65912_vsel_to_uv_range0(selector);
  330. break;
  331. case 1:
  332. /* 0.7 - 1.4875V in 12.5mV steps */
  333. voltage = tps65912_vsel_to_uv_range1(selector);
  334. break;
  335. case 2:
  336. /* 0.5 - 2.075V in 25mV steps */
  337. voltage = tps65912_vsel_to_uv_range2(selector);
  338. break;
  339. case 3:
  340. /* 0.5 - 3.8V in 50mV steps */
  341. voltage = tps65912_vsel_to_uv_range3(selector);
  342. break;
  343. }
  344. return voltage;
  345. }
  346. static int tps65912_get_voltage_dcdc(struct regulator_dev *dev)
  347. {
  348. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  349. struct tps65912 *mfd = pmic->mfd;
  350. int id = rdev_get_id(dev);
  351. int reg, vsel;
  352. reg = tps65912_get_sel_register(pmic, id);
  353. if (reg < 0)
  354. return reg;
  355. vsel = tps65912_reg_read(mfd, reg);
  356. vsel &= 0x3F;
  357. return tps65912_list_voltage_dcdc(dev, vsel);
  358. }
  359. static int tps65912_set_voltage_sel(struct regulator_dev *dev,
  360. unsigned selector)
  361. {
  362. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  363. struct tps65912 *mfd = pmic->mfd;
  364. int id = rdev_get_id(dev);
  365. int value;
  366. u8 reg;
  367. reg = tps65912_get_sel_register(pmic, id);
  368. value = tps65912_reg_read(mfd, reg);
  369. value &= 0xC0;
  370. return tps65912_reg_write(mfd, reg, selector | value);
  371. }
  372. static int tps65912_get_voltage_ldo(struct regulator_dev *dev)
  373. {
  374. struct tps65912_reg *pmic = rdev_get_drvdata(dev);
  375. struct tps65912 *mfd = pmic->mfd;
  376. int id = rdev_get_id(dev);
  377. int vsel = 0;
  378. u8 reg;
  379. reg = tps65912_get_sel_register(pmic, id);
  380. vsel = tps65912_reg_read(mfd, reg);
  381. vsel &= 0x3F;
  382. return tps65912_vsel_to_uv_ldo(vsel);
  383. }
  384. static int tps65912_list_voltage_ldo(struct regulator_dev *dev,
  385. unsigned selector)
  386. {
  387. int ldo = rdev_get_id(dev);
  388. if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10)
  389. return -EINVAL;
  390. return tps65912_vsel_to_uv_ldo(selector);
  391. }
  392. /* Operations permitted on DCDCx */
  393. static struct regulator_ops tps65912_ops_dcdc = {
  394. .is_enabled = tps65912_reg_is_enabled,
  395. .enable = tps65912_reg_enable,
  396. .disable = tps65912_reg_disable,
  397. .set_mode = tps65912_set_mode,
  398. .get_mode = tps65912_get_mode,
  399. .get_voltage = tps65912_get_voltage_dcdc,
  400. .set_voltage_sel = tps65912_set_voltage_sel,
  401. .list_voltage = tps65912_list_voltage_dcdc,
  402. };
  403. /* Operations permitted on LDOx */
  404. static struct regulator_ops tps65912_ops_ldo = {
  405. .is_enabled = tps65912_reg_is_enabled,
  406. .enable = tps65912_reg_enable,
  407. .disable = tps65912_reg_disable,
  408. .get_voltage = tps65912_get_voltage_ldo,
  409. .set_voltage_sel = tps65912_set_voltage_sel,
  410. .list_voltage = tps65912_list_voltage_ldo,
  411. };
  412. static __devinit int tps65912_probe(struct platform_device *pdev)
  413. {
  414. struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
  415. struct tps_info *info;
  416. struct regulator_init_data *reg_data;
  417. struct regulator_dev *rdev;
  418. struct tps65912_reg *pmic;
  419. struct tps65912_board *pmic_plat_data;
  420. int i, err;
  421. pmic_plat_data = dev_get_platdata(tps65912->dev);
  422. if (!pmic_plat_data)
  423. return -EINVAL;
  424. reg_data = pmic_plat_data->tps65912_pmic_init_data;
  425. pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
  426. if (!pmic)
  427. return -ENOMEM;
  428. mutex_init(&pmic->io_lock);
  429. pmic->mfd = tps65912;
  430. platform_set_drvdata(pdev, pmic);
  431. pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
  432. info = tps65912_regs;
  433. for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
  434. int range = 0;
  435. /* Register the regulators */
  436. pmic->info[i] = info;
  437. pmic->desc[i].name = info->name;
  438. pmic->desc[i].id = i;
  439. pmic->desc[i].n_voltages = 64;
  440. pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
  441. &tps65912_ops_ldo : &tps65912_ops_dcdc);
  442. pmic->desc[i].type = REGULATOR_VOLTAGE;
  443. pmic->desc[i].owner = THIS_MODULE;
  444. range = tps65912_get_range(pmic, i);
  445. rdev = regulator_register(&pmic->desc[i],
  446. tps65912->dev, reg_data, pmic, NULL);
  447. if (IS_ERR(rdev)) {
  448. dev_err(tps65912->dev,
  449. "failed to register %s regulator\n",
  450. pdev->name);
  451. err = PTR_ERR(rdev);
  452. goto err;
  453. }
  454. /* Save regulator for cleanup */
  455. pmic->rdev[i] = rdev;
  456. }
  457. return 0;
  458. err:
  459. while (--i >= 0)
  460. regulator_unregister(pmic->rdev[i]);
  461. kfree(pmic);
  462. return err;
  463. }
  464. static int __devexit tps65912_remove(struct platform_device *pdev)
  465. {
  466. struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
  467. int i;
  468. for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
  469. regulator_unregister(tps65912_reg->rdev[i]);
  470. kfree(tps65912_reg);
  471. return 0;
  472. }
  473. static struct platform_driver tps65912_driver = {
  474. .driver = {
  475. .name = "tps65912-pmic",
  476. .owner = THIS_MODULE,
  477. },
  478. .probe = tps65912_probe,
  479. .remove = __devexit_p(tps65912_remove),
  480. };
  481. static int __init tps65912_init(void)
  482. {
  483. return platform_driver_register(&tps65912_driver);
  484. }
  485. subsys_initcall(tps65912_init);
  486. static void __exit tps65912_cleanup(void)
  487. {
  488. platform_driver_unregister(&tps65912_driver);
  489. }
  490. module_exit(tps65912_cleanup);
  491. MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
  492. MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
  493. MODULE_LICENSE("GPL v2");
  494. MODULE_ALIAS("platform:tps65912-pmic");