tps6507x-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * tps6507x-regulator.c
  3. *
  4. * Regulator driver for TPS65073 PMIC
  5. *
  6. * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regulator/tps6507x.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/mfd/tps6507x.h>
  28. /* DCDC's */
  29. #define TPS6507X_DCDC_1 0
  30. #define TPS6507X_DCDC_2 1
  31. #define TPS6507X_DCDC_3 2
  32. /* LDOs */
  33. #define TPS6507X_LDO_1 3
  34. #define TPS6507X_LDO_2 4
  35. #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
  36. /* Number of step-down converters available */
  37. #define TPS6507X_NUM_DCDC 3
  38. /* Number of LDO voltage regulators available */
  39. #define TPS6507X_NUM_LDO 2
  40. /* Number of total regulators available */
  41. #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
  42. /* Supported voltage values for regulators (in milliVolts) */
  43. static const u16 VDCDCx_VSEL_table[] = {
  44. 725, 750, 775, 800,
  45. 825, 850, 875, 900,
  46. 925, 950, 975, 1000,
  47. 1025, 1050, 1075, 1100,
  48. 1125, 1150, 1175, 1200,
  49. 1225, 1250, 1275, 1300,
  50. 1325, 1350, 1375, 1400,
  51. 1425, 1450, 1475, 1500,
  52. 1550, 1600, 1650, 1700,
  53. 1750, 1800, 1850, 1900,
  54. 1950, 2000, 2050, 2100,
  55. 2150, 2200, 2250, 2300,
  56. 2350, 2400, 2450, 2500,
  57. 2550, 2600, 2650, 2700,
  58. 2750, 2800, 2850, 2900,
  59. 3000, 3100, 3200, 3300,
  60. };
  61. static const u16 LDO1_VSEL_table[] = {
  62. 1000, 1100, 1200, 1250,
  63. 1300, 1350, 1400, 1500,
  64. 1600, 1800, 2500, 2750,
  65. 2800, 3000, 3100, 3300,
  66. };
  67. static const u16 LDO2_VSEL_table[] = {
  68. 725, 750, 775, 800,
  69. 825, 850, 875, 900,
  70. 925, 950, 975, 1000,
  71. 1025, 1050, 1075, 1100,
  72. 1125, 1150, 1175, 1200,
  73. 1225, 1250, 1275, 1300,
  74. 1325, 1350, 1375, 1400,
  75. 1425, 1450, 1475, 1500,
  76. 1550, 1600, 1650, 1700,
  77. 1750, 1800, 1850, 1900,
  78. 1950, 2000, 2050, 2100,
  79. 2150, 2200, 2250, 2300,
  80. 2350, 2400, 2450, 2500,
  81. 2550, 2600, 2650, 2700,
  82. 2750, 2800, 2850, 2900,
  83. 3000, 3100, 3200, 3300,
  84. };
  85. struct tps_info {
  86. const char *name;
  87. unsigned min_uV;
  88. unsigned max_uV;
  89. u8 table_len;
  90. const u16 *table;
  91. /* Does DCDC high or the low register defines output voltage? */
  92. bool defdcdc_default;
  93. };
  94. static struct tps_info tps6507x_pmic_regs[] = {
  95. {
  96. .name = "VDCDC1",
  97. .min_uV = 725000,
  98. .max_uV = 3300000,
  99. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  100. .table = VDCDCx_VSEL_table,
  101. },
  102. {
  103. .name = "VDCDC2",
  104. .min_uV = 725000,
  105. .max_uV = 3300000,
  106. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  107. .table = VDCDCx_VSEL_table,
  108. },
  109. {
  110. .name = "VDCDC3",
  111. .min_uV = 725000,
  112. .max_uV = 3300000,
  113. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  114. .table = VDCDCx_VSEL_table,
  115. },
  116. {
  117. .name = "LDO1",
  118. .min_uV = 1000000,
  119. .max_uV = 3300000,
  120. .table_len = ARRAY_SIZE(LDO1_VSEL_table),
  121. .table = LDO1_VSEL_table,
  122. },
  123. {
  124. .name = "LDO2",
  125. .min_uV = 725000,
  126. .max_uV = 3300000,
  127. .table_len = ARRAY_SIZE(LDO2_VSEL_table),
  128. .table = LDO2_VSEL_table,
  129. },
  130. };
  131. struct tps6507x_pmic {
  132. struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
  133. struct tps6507x_dev *mfd;
  134. struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
  135. struct tps_info *info[TPS6507X_NUM_REGULATOR];
  136. struct mutex io_lock;
  137. };
  138. static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
  139. {
  140. u8 val;
  141. int err;
  142. err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
  143. if (err)
  144. return err;
  145. return val;
  146. }
  147. static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  148. {
  149. return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
  150. }
  151. static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  152. {
  153. int err, data;
  154. mutex_lock(&tps->io_lock);
  155. data = tps6507x_pmic_read(tps, reg);
  156. if (data < 0) {
  157. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  158. err = data;
  159. goto out;
  160. }
  161. data |= mask;
  162. err = tps6507x_pmic_write(tps, reg, data);
  163. if (err)
  164. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  165. out:
  166. mutex_unlock(&tps->io_lock);
  167. return err;
  168. }
  169. static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  170. {
  171. int err, data;
  172. mutex_lock(&tps->io_lock);
  173. data = tps6507x_pmic_read(tps, reg);
  174. if (data < 0) {
  175. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  176. err = data;
  177. goto out;
  178. }
  179. data &= ~mask;
  180. err = tps6507x_pmic_write(tps, reg, data);
  181. if (err)
  182. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  183. out:
  184. mutex_unlock(&tps->io_lock);
  185. return err;
  186. }
  187. static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
  188. {
  189. int data;
  190. mutex_lock(&tps->io_lock);
  191. data = tps6507x_pmic_read(tps, reg);
  192. if (data < 0)
  193. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  194. mutex_unlock(&tps->io_lock);
  195. return data;
  196. }
  197. static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  198. {
  199. int err;
  200. mutex_lock(&tps->io_lock);
  201. err = tps6507x_pmic_write(tps, reg, val);
  202. if (err < 0)
  203. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  204. mutex_unlock(&tps->io_lock);
  205. return err;
  206. }
  207. static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
  208. {
  209. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  210. int data, rid = rdev_get_id(dev);
  211. u8 shift;
  212. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  213. return -EINVAL;
  214. shift = TPS6507X_MAX_REG_ID - rid;
  215. data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
  216. if (data < 0)
  217. return data;
  218. else
  219. return (data & 1<<shift) ? 1 : 0;
  220. }
  221. static int tps6507x_pmic_enable(struct regulator_dev *dev)
  222. {
  223. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  224. int rid = rdev_get_id(dev);
  225. u8 shift;
  226. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  227. return -EINVAL;
  228. shift = TPS6507X_MAX_REG_ID - rid;
  229. return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
  230. }
  231. static int tps6507x_pmic_disable(struct regulator_dev *dev)
  232. {
  233. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  234. int rid = rdev_get_id(dev);
  235. u8 shift;
  236. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  237. return -EINVAL;
  238. shift = TPS6507X_MAX_REG_ID - rid;
  239. return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
  240. 1 << shift);
  241. }
  242. static int tps6507x_pmic_get_voltage(struct regulator_dev *dev)
  243. {
  244. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  245. int data, rid = rdev_get_id(dev);
  246. u8 reg, mask;
  247. switch (rid) {
  248. case TPS6507X_DCDC_1:
  249. reg = TPS6507X_REG_DEFDCDC1;
  250. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  251. break;
  252. case TPS6507X_DCDC_2:
  253. if (tps->info[rid]->defdcdc_default)
  254. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  255. else
  256. reg = TPS6507X_REG_DEFDCDC2_LOW;
  257. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  258. break;
  259. case TPS6507X_DCDC_3:
  260. if (tps->info[rid]->defdcdc_default)
  261. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  262. else
  263. reg = TPS6507X_REG_DEFDCDC3_LOW;
  264. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  265. break;
  266. case TPS6507X_LDO_1:
  267. reg = TPS6507X_REG_LDO_CTRL1;
  268. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  269. break;
  270. case TPS6507X_LDO_2:
  271. reg = TPS6507X_REG_DEFLDO2;
  272. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. data = tps6507x_pmic_reg_read(tps, reg);
  278. if (data < 0)
  279. return data;
  280. data &= mask;
  281. return tps->info[rid]->table[data] * 1000;
  282. }
  283. static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
  284. unsigned selector)
  285. {
  286. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  287. int data, rid = rdev_get_id(dev);
  288. u8 reg, mask;
  289. switch (rid) {
  290. case TPS6507X_DCDC_1:
  291. reg = TPS6507X_REG_DEFDCDC1;
  292. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  293. break;
  294. case TPS6507X_DCDC_2:
  295. if (tps->info[rid]->defdcdc_default)
  296. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  297. else
  298. reg = TPS6507X_REG_DEFDCDC2_LOW;
  299. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  300. break;
  301. case TPS6507X_DCDC_3:
  302. if (tps->info[rid]->defdcdc_default)
  303. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  304. else
  305. reg = TPS6507X_REG_DEFDCDC3_LOW;
  306. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  307. break;
  308. case TPS6507X_LDO_1:
  309. reg = TPS6507X_REG_LDO_CTRL1;
  310. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  311. break;
  312. case TPS6507X_LDO_2:
  313. reg = TPS6507X_REG_DEFLDO2;
  314. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  315. break;
  316. default:
  317. return -EINVAL;
  318. }
  319. data = tps6507x_pmic_reg_read(tps, reg);
  320. if (data < 0)
  321. return data;
  322. data &= ~mask;
  323. data |= selector;
  324. return tps6507x_pmic_reg_write(tps, reg, data);
  325. }
  326. static int tps6507x_pmic_list_voltage(struct regulator_dev *dev,
  327. unsigned selector)
  328. {
  329. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  330. int rid = rdev_get_id(dev);
  331. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  332. return -EINVAL;
  333. if (selector >= tps->info[rid]->table_len)
  334. return -EINVAL;
  335. else
  336. return tps->info[rid]->table[selector] * 1000;
  337. }
  338. static struct regulator_ops tps6507x_pmic_ops = {
  339. .is_enabled = tps6507x_pmic_is_enabled,
  340. .enable = tps6507x_pmic_enable,
  341. .disable = tps6507x_pmic_disable,
  342. .get_voltage = tps6507x_pmic_get_voltage,
  343. .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
  344. .list_voltage = tps6507x_pmic_list_voltage,
  345. };
  346. static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
  347. {
  348. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  349. struct tps_info *info = &tps6507x_pmic_regs[0];
  350. struct regulator_init_data *init_data;
  351. struct regulator_dev *rdev;
  352. struct tps6507x_pmic *tps;
  353. struct tps6507x_board *tps_board;
  354. int i;
  355. int error;
  356. /**
  357. * tps_board points to pmic related constants
  358. * coming from the board-evm file.
  359. */
  360. tps_board = dev_get_platdata(tps6507x_dev->dev);
  361. if (!tps_board)
  362. return -EINVAL;
  363. /**
  364. * init_data points to array of regulator_init structures
  365. * coming from the board-evm file.
  366. */
  367. init_data = tps_board->tps6507x_pmic_init_data;
  368. if (!init_data)
  369. return -EINVAL;
  370. tps = kzalloc(sizeof(*tps), GFP_KERNEL);
  371. if (!tps)
  372. return -ENOMEM;
  373. mutex_init(&tps->io_lock);
  374. /* common for all regulators */
  375. tps->mfd = tps6507x_dev;
  376. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
  377. /* Register the regulators */
  378. tps->info[i] = info;
  379. if (init_data->driver_data) {
  380. struct tps6507x_reg_platform_data *data =
  381. init_data->driver_data;
  382. tps->info[i]->defdcdc_default = data->defdcdc_default;
  383. }
  384. tps->desc[i].name = info->name;
  385. tps->desc[i].id = i;
  386. tps->desc[i].n_voltages = info->table_len;
  387. tps->desc[i].ops = &tps6507x_pmic_ops;
  388. tps->desc[i].type = REGULATOR_VOLTAGE;
  389. tps->desc[i].owner = THIS_MODULE;
  390. rdev = regulator_register(&tps->desc[i],
  391. tps6507x_dev->dev, init_data, tps, NULL);
  392. if (IS_ERR(rdev)) {
  393. dev_err(tps6507x_dev->dev,
  394. "failed to register %s regulator\n",
  395. pdev->name);
  396. error = PTR_ERR(rdev);
  397. goto fail;
  398. }
  399. /* Save regulator for cleanup */
  400. tps->rdev[i] = rdev;
  401. }
  402. tps6507x_dev->pmic = tps;
  403. platform_set_drvdata(pdev, tps6507x_dev);
  404. return 0;
  405. fail:
  406. while (--i >= 0)
  407. regulator_unregister(tps->rdev[i]);
  408. kfree(tps);
  409. return error;
  410. }
  411. static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
  412. {
  413. struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
  414. struct tps6507x_pmic *tps = tps6507x_dev->pmic;
  415. int i;
  416. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
  417. regulator_unregister(tps->rdev[i]);
  418. kfree(tps);
  419. return 0;
  420. }
  421. static struct platform_driver tps6507x_pmic_driver = {
  422. .driver = {
  423. .name = "tps6507x-pmic",
  424. .owner = THIS_MODULE,
  425. },
  426. .probe = tps6507x_pmic_probe,
  427. .remove = __devexit_p(tps6507x_pmic_remove),
  428. };
  429. static int __init tps6507x_pmic_init(void)
  430. {
  431. return platform_driver_register(&tps6507x_pmic_driver);
  432. }
  433. subsys_initcall(tps6507x_pmic_init);
  434. static void __exit tps6507x_pmic_cleanup(void)
  435. {
  436. platform_driver_unregister(&tps6507x_pmic_driver);
  437. }
  438. module_exit(tps6507x_pmic_cleanup);
  439. MODULE_AUTHOR("Texas Instruments");
  440. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  441. MODULE_LICENSE("GPL v2");
  442. MODULE_ALIAS("platform:tps6507x-pmic");