tps6507x-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/slab.h>
  26. #include <linux/mfd/tps6507x.h>
  27. /* DCDC's */
  28. #define TPS6507X_DCDC_1 0
  29. #define TPS6507X_DCDC_2 1
  30. #define TPS6507X_DCDC_3 2
  31. /* LDOs */
  32. #define TPS6507X_LDO_1 3
  33. #define TPS6507X_LDO_2 4
  34. #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
  35. /* Number of step-down converters available */
  36. #define TPS6507X_NUM_DCDC 3
  37. /* Number of LDO voltage regulators available */
  38. #define TPS6507X_NUM_LDO 2
  39. /* Number of total regulators available */
  40. #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
  41. /* Supported voltage values for regulators (in microVolts) */
  42. static const unsigned int VDCDCx_VSEL_table[] = {
  43. 725000, 750000, 775000, 800000,
  44. 825000, 850000, 875000, 900000,
  45. 925000, 950000, 975000, 1000000,
  46. 1025000, 1050000, 1075000, 1100000,
  47. 1125000, 1150000, 1175000, 1200000,
  48. 1225000, 1250000, 1275000, 1300000,
  49. 1325000, 1350000, 1375000, 1400000,
  50. 1425000, 1450000, 1475000, 1500000,
  51. 1550000, 1600000, 1650000, 1700000,
  52. 1750000, 1800000, 1850000, 1900000,
  53. 1950000, 2000000, 2050000, 2100000,
  54. 2150000, 2200000, 2250000, 2300000,
  55. 2350000, 2400000, 2450000, 2500000,
  56. 2550000, 2600000, 2650000, 2700000,
  57. 2750000, 2800000, 2850000, 2900000,
  58. 3000000, 3100000, 3200000, 3300000,
  59. };
  60. static const unsigned int LDO1_VSEL_table[] = {
  61. 1000000, 1100000, 1200000, 1250000,
  62. 1300000, 1350000, 1400000, 1500000,
  63. 1600000, 1800000, 2500000, 2750000,
  64. 2800000, 3000000, 3100000, 3300000,
  65. };
  66. static const unsigned int LDO2_VSEL_table[] = {
  67. 725000, 750000, 775000, 800000,
  68. 825000, 850000, 875000, 900000,
  69. 925000, 950000, 975000, 1000000,
  70. 1025000, 1050000, 1075000, 1100000,
  71. 1125000, 1150000, 1175000, 1200000,
  72. 1225000, 1250000, 1275000, 1300000,
  73. 1325000, 1350000, 1375000, 1400000,
  74. 1425000, 1450000, 1475000, 1500000,
  75. 1550000, 1600000, 1650000, 1700000,
  76. 1750000, 1800000, 1850000, 1900000,
  77. 1950000, 2000000, 2050000, 2100000,
  78. 2150000, 2200000, 2250000, 2300000,
  79. 2350000, 2400000, 2450000, 2500000,
  80. 2550000, 2600000, 2650000, 2700000,
  81. 2750000, 2800000, 2850000, 2900000,
  82. 3000000, 3100000, 3200000, 3300000,
  83. };
  84. struct tps_info {
  85. const char *name;
  86. u8 table_len;
  87. const unsigned int *table;
  88. /* Does DCDC high or the low register defines output voltage? */
  89. bool defdcdc_default;
  90. };
  91. static struct tps_info tps6507x_pmic_regs[] = {
  92. {
  93. .name = "VDCDC1",
  94. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  95. .table = VDCDCx_VSEL_table,
  96. },
  97. {
  98. .name = "VDCDC2",
  99. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  100. .table = VDCDCx_VSEL_table,
  101. },
  102. {
  103. .name = "VDCDC3",
  104. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  105. .table = VDCDCx_VSEL_table,
  106. },
  107. {
  108. .name = "LDO1",
  109. .table_len = ARRAY_SIZE(LDO1_VSEL_table),
  110. .table = LDO1_VSEL_table,
  111. },
  112. {
  113. .name = "LDO2",
  114. .table_len = ARRAY_SIZE(LDO2_VSEL_table),
  115. .table = LDO2_VSEL_table,
  116. },
  117. };
  118. struct tps6507x_pmic {
  119. struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
  120. struct tps6507x_dev *mfd;
  121. struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
  122. struct tps_info *info[TPS6507X_NUM_REGULATOR];
  123. struct mutex io_lock;
  124. };
  125. static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
  126. {
  127. u8 val;
  128. int err;
  129. err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
  130. if (err)
  131. return err;
  132. return val;
  133. }
  134. static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  135. {
  136. return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
  137. }
  138. static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  139. {
  140. int err, data;
  141. mutex_lock(&tps->io_lock);
  142. data = tps6507x_pmic_read(tps, reg);
  143. if (data < 0) {
  144. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  145. err = data;
  146. goto out;
  147. }
  148. data |= mask;
  149. err = tps6507x_pmic_write(tps, reg, data);
  150. if (err)
  151. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  152. out:
  153. mutex_unlock(&tps->io_lock);
  154. return err;
  155. }
  156. static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  157. {
  158. int err, data;
  159. mutex_lock(&tps->io_lock);
  160. data = tps6507x_pmic_read(tps, reg);
  161. if (data < 0) {
  162. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  163. err = data;
  164. goto out;
  165. }
  166. data &= ~mask;
  167. err = tps6507x_pmic_write(tps, reg, data);
  168. if (err)
  169. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  170. out:
  171. mutex_unlock(&tps->io_lock);
  172. return err;
  173. }
  174. static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
  175. {
  176. int data;
  177. mutex_lock(&tps->io_lock);
  178. data = tps6507x_pmic_read(tps, reg);
  179. if (data < 0)
  180. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  181. mutex_unlock(&tps->io_lock);
  182. return data;
  183. }
  184. static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  185. {
  186. int err;
  187. mutex_lock(&tps->io_lock);
  188. err = tps6507x_pmic_write(tps, reg, val);
  189. if (err < 0)
  190. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  191. mutex_unlock(&tps->io_lock);
  192. return err;
  193. }
  194. static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
  195. {
  196. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  197. int data, rid = rdev_get_id(dev);
  198. u8 shift;
  199. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  200. return -EINVAL;
  201. shift = TPS6507X_MAX_REG_ID - rid;
  202. data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
  203. if (data < 0)
  204. return data;
  205. else
  206. return (data & 1<<shift) ? 1 : 0;
  207. }
  208. static int tps6507x_pmic_enable(struct regulator_dev *dev)
  209. {
  210. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  211. int rid = rdev_get_id(dev);
  212. u8 shift;
  213. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  214. return -EINVAL;
  215. shift = TPS6507X_MAX_REG_ID - rid;
  216. return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
  217. }
  218. static int tps6507x_pmic_disable(struct regulator_dev *dev)
  219. {
  220. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  221. int rid = rdev_get_id(dev);
  222. u8 shift;
  223. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  224. return -EINVAL;
  225. shift = TPS6507X_MAX_REG_ID - rid;
  226. return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
  227. 1 << shift);
  228. }
  229. static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
  230. {
  231. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  232. int data, rid = rdev_get_id(dev);
  233. u8 reg, mask;
  234. switch (rid) {
  235. case TPS6507X_DCDC_1:
  236. reg = TPS6507X_REG_DEFDCDC1;
  237. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  238. break;
  239. case TPS6507X_DCDC_2:
  240. if (tps->info[rid]->defdcdc_default)
  241. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  242. else
  243. reg = TPS6507X_REG_DEFDCDC2_LOW;
  244. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  245. break;
  246. case TPS6507X_DCDC_3:
  247. if (tps->info[rid]->defdcdc_default)
  248. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  249. else
  250. reg = TPS6507X_REG_DEFDCDC3_LOW;
  251. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  252. break;
  253. case TPS6507X_LDO_1:
  254. reg = TPS6507X_REG_LDO_CTRL1;
  255. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  256. break;
  257. case TPS6507X_LDO_2:
  258. reg = TPS6507X_REG_DEFLDO2;
  259. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  260. break;
  261. default:
  262. return -EINVAL;
  263. }
  264. data = tps6507x_pmic_reg_read(tps, reg);
  265. if (data < 0)
  266. return data;
  267. data &= mask;
  268. return data;
  269. }
  270. static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
  271. unsigned selector)
  272. {
  273. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  274. int data, rid = rdev_get_id(dev);
  275. u8 reg, mask;
  276. switch (rid) {
  277. case TPS6507X_DCDC_1:
  278. reg = TPS6507X_REG_DEFDCDC1;
  279. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  280. break;
  281. case TPS6507X_DCDC_2:
  282. if (tps->info[rid]->defdcdc_default)
  283. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  284. else
  285. reg = TPS6507X_REG_DEFDCDC2_LOW;
  286. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  287. break;
  288. case TPS6507X_DCDC_3:
  289. if (tps->info[rid]->defdcdc_default)
  290. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  291. else
  292. reg = TPS6507X_REG_DEFDCDC3_LOW;
  293. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  294. break;
  295. case TPS6507X_LDO_1:
  296. reg = TPS6507X_REG_LDO_CTRL1;
  297. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  298. break;
  299. case TPS6507X_LDO_2:
  300. reg = TPS6507X_REG_DEFLDO2;
  301. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. data = tps6507x_pmic_reg_read(tps, reg);
  307. if (data < 0)
  308. return data;
  309. data &= ~mask;
  310. data |= selector;
  311. return tps6507x_pmic_reg_write(tps, reg, data);
  312. }
  313. static struct regulator_ops tps6507x_pmic_ops = {
  314. .is_enabled = tps6507x_pmic_is_enabled,
  315. .enable = tps6507x_pmic_enable,
  316. .disable = tps6507x_pmic_disable,
  317. .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
  318. .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
  319. .list_voltage = regulator_list_voltage_table,
  320. };
  321. static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
  322. {
  323. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  324. struct tps_info *info = &tps6507x_pmic_regs[0];
  325. struct regulator_config config = { };
  326. struct regulator_init_data *init_data;
  327. struct regulator_dev *rdev;
  328. struct tps6507x_pmic *tps;
  329. struct tps6507x_board *tps_board;
  330. int i;
  331. int error;
  332. /**
  333. * tps_board points to pmic related constants
  334. * coming from the board-evm file.
  335. */
  336. tps_board = dev_get_platdata(tps6507x_dev->dev);
  337. if (!tps_board)
  338. return -EINVAL;
  339. /**
  340. * init_data points to array of regulator_init structures
  341. * coming from the board-evm file.
  342. */
  343. init_data = tps_board->tps6507x_pmic_init_data;
  344. if (!init_data)
  345. return -EINVAL;
  346. tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
  347. if (!tps)
  348. return -ENOMEM;
  349. mutex_init(&tps->io_lock);
  350. /* common for all regulators */
  351. tps->mfd = tps6507x_dev;
  352. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
  353. /* Register the regulators */
  354. tps->info[i] = info;
  355. if (init_data->driver_data) {
  356. struct tps6507x_reg_platform_data *data =
  357. init_data->driver_data;
  358. tps->info[i]->defdcdc_default = data->defdcdc_default;
  359. }
  360. tps->desc[i].name = info->name;
  361. tps->desc[i].id = i;
  362. tps->desc[i].n_voltages = info->table_len;
  363. tps->desc[i].volt_table = info->table;
  364. tps->desc[i].ops = &tps6507x_pmic_ops;
  365. tps->desc[i].type = REGULATOR_VOLTAGE;
  366. tps->desc[i].owner = THIS_MODULE;
  367. config.dev = tps6507x_dev->dev;
  368. config.init_data = init_data;
  369. config.driver_data = tps;
  370. rdev = regulator_register(&tps->desc[i], &config);
  371. if (IS_ERR(rdev)) {
  372. dev_err(tps6507x_dev->dev,
  373. "failed to register %s regulator\n",
  374. pdev->name);
  375. error = PTR_ERR(rdev);
  376. goto fail;
  377. }
  378. /* Save regulator for cleanup */
  379. tps->rdev[i] = rdev;
  380. }
  381. tps6507x_dev->pmic = tps;
  382. platform_set_drvdata(pdev, tps6507x_dev);
  383. return 0;
  384. fail:
  385. while (--i >= 0)
  386. regulator_unregister(tps->rdev[i]);
  387. return error;
  388. }
  389. static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
  390. {
  391. struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
  392. struct tps6507x_pmic *tps = tps6507x_dev->pmic;
  393. int i;
  394. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
  395. regulator_unregister(tps->rdev[i]);
  396. return 0;
  397. }
  398. static struct platform_driver tps6507x_pmic_driver = {
  399. .driver = {
  400. .name = "tps6507x-pmic",
  401. .owner = THIS_MODULE,
  402. },
  403. .probe = tps6507x_pmic_probe,
  404. .remove = __devexit_p(tps6507x_pmic_remove),
  405. };
  406. static int __init tps6507x_pmic_init(void)
  407. {
  408. return platform_driver_register(&tps6507x_pmic_driver);
  409. }
  410. subsys_initcall(tps6507x_pmic_init);
  411. static void __exit tps6507x_pmic_cleanup(void)
  412. {
  413. platform_driver_unregister(&tps6507x_pmic_driver);
  414. }
  415. module_exit(tps6507x_pmic_cleanup);
  416. MODULE_AUTHOR("Texas Instruments");
  417. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  418. MODULE_LICENSE("GPL v2");
  419. MODULE_ALIAS("platform:tps6507x-pmic");