tps6507x-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /* The voltage mapping table for LDO2 is the same as VDCDCx */
  67. #define LDO2_VSEL_table VDCDCx_VSEL_table
  68. struct tps_info {
  69. const char *name;
  70. u8 table_len;
  71. const unsigned int *table;
  72. /* Does DCDC high or the low register defines output voltage? */
  73. bool defdcdc_default;
  74. };
  75. static struct tps_info tps6507x_pmic_regs[] = {
  76. {
  77. .name = "VDCDC1",
  78. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  79. .table = VDCDCx_VSEL_table,
  80. },
  81. {
  82. .name = "VDCDC2",
  83. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  84. .table = VDCDCx_VSEL_table,
  85. },
  86. {
  87. .name = "VDCDC3",
  88. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  89. .table = VDCDCx_VSEL_table,
  90. },
  91. {
  92. .name = "LDO1",
  93. .table_len = ARRAY_SIZE(LDO1_VSEL_table),
  94. .table = LDO1_VSEL_table,
  95. },
  96. {
  97. .name = "LDO2",
  98. .table_len = ARRAY_SIZE(LDO2_VSEL_table),
  99. .table = LDO2_VSEL_table,
  100. },
  101. };
  102. struct tps6507x_pmic {
  103. struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
  104. struct tps6507x_dev *mfd;
  105. struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
  106. struct tps_info *info[TPS6507X_NUM_REGULATOR];
  107. struct mutex io_lock;
  108. };
  109. static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
  110. {
  111. u8 val;
  112. int err;
  113. err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
  114. if (err)
  115. return err;
  116. return val;
  117. }
  118. static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  119. {
  120. return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
  121. }
  122. static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  123. {
  124. int err, data;
  125. mutex_lock(&tps->io_lock);
  126. data = tps6507x_pmic_read(tps, reg);
  127. if (data < 0) {
  128. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  129. err = data;
  130. goto out;
  131. }
  132. data |= mask;
  133. err = tps6507x_pmic_write(tps, reg, data);
  134. if (err)
  135. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  136. out:
  137. mutex_unlock(&tps->io_lock);
  138. return err;
  139. }
  140. static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  141. {
  142. int err, data;
  143. mutex_lock(&tps->io_lock);
  144. data = tps6507x_pmic_read(tps, reg);
  145. if (data < 0) {
  146. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  147. err = data;
  148. goto out;
  149. }
  150. data &= ~mask;
  151. err = tps6507x_pmic_write(tps, reg, data);
  152. if (err)
  153. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  154. out:
  155. mutex_unlock(&tps->io_lock);
  156. return err;
  157. }
  158. static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
  159. {
  160. int data;
  161. mutex_lock(&tps->io_lock);
  162. data = tps6507x_pmic_read(tps, reg);
  163. if (data < 0)
  164. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  165. mutex_unlock(&tps->io_lock);
  166. return data;
  167. }
  168. static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  169. {
  170. int err;
  171. mutex_lock(&tps->io_lock);
  172. err = tps6507x_pmic_write(tps, reg, val);
  173. if (err < 0)
  174. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  175. mutex_unlock(&tps->io_lock);
  176. return err;
  177. }
  178. static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
  179. {
  180. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  181. int data, rid = rdev_get_id(dev);
  182. u8 shift;
  183. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  184. return -EINVAL;
  185. shift = TPS6507X_MAX_REG_ID - rid;
  186. data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
  187. if (data < 0)
  188. return data;
  189. else
  190. return (data & 1<<shift) ? 1 : 0;
  191. }
  192. static int tps6507x_pmic_enable(struct regulator_dev *dev)
  193. {
  194. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  195. int rid = rdev_get_id(dev);
  196. u8 shift;
  197. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  198. return -EINVAL;
  199. shift = TPS6507X_MAX_REG_ID - rid;
  200. return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
  201. }
  202. static int tps6507x_pmic_disable(struct regulator_dev *dev)
  203. {
  204. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  205. int rid = rdev_get_id(dev);
  206. u8 shift;
  207. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  208. return -EINVAL;
  209. shift = TPS6507X_MAX_REG_ID - rid;
  210. return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
  211. 1 << shift);
  212. }
  213. static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
  214. {
  215. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  216. int data, rid = rdev_get_id(dev);
  217. u8 reg, mask;
  218. switch (rid) {
  219. case TPS6507X_DCDC_1:
  220. reg = TPS6507X_REG_DEFDCDC1;
  221. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  222. break;
  223. case TPS6507X_DCDC_2:
  224. if (tps->info[rid]->defdcdc_default)
  225. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  226. else
  227. reg = TPS6507X_REG_DEFDCDC2_LOW;
  228. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  229. break;
  230. case TPS6507X_DCDC_3:
  231. if (tps->info[rid]->defdcdc_default)
  232. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  233. else
  234. reg = TPS6507X_REG_DEFDCDC3_LOW;
  235. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  236. break;
  237. case TPS6507X_LDO_1:
  238. reg = TPS6507X_REG_LDO_CTRL1;
  239. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  240. break;
  241. case TPS6507X_LDO_2:
  242. reg = TPS6507X_REG_DEFLDO2;
  243. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  244. break;
  245. default:
  246. return -EINVAL;
  247. }
  248. data = tps6507x_pmic_reg_read(tps, reg);
  249. if (data < 0)
  250. return data;
  251. data &= mask;
  252. return data;
  253. }
  254. static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
  255. unsigned selector)
  256. {
  257. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  258. int data, rid = rdev_get_id(dev);
  259. u8 reg, mask;
  260. switch (rid) {
  261. case TPS6507X_DCDC_1:
  262. reg = TPS6507X_REG_DEFDCDC1;
  263. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  264. break;
  265. case TPS6507X_DCDC_2:
  266. if (tps->info[rid]->defdcdc_default)
  267. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  268. else
  269. reg = TPS6507X_REG_DEFDCDC2_LOW;
  270. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  271. break;
  272. case TPS6507X_DCDC_3:
  273. if (tps->info[rid]->defdcdc_default)
  274. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  275. else
  276. reg = TPS6507X_REG_DEFDCDC3_LOW;
  277. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  278. break;
  279. case TPS6507X_LDO_1:
  280. reg = TPS6507X_REG_LDO_CTRL1;
  281. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  282. break;
  283. case TPS6507X_LDO_2:
  284. reg = TPS6507X_REG_DEFLDO2;
  285. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. data = tps6507x_pmic_reg_read(tps, reg);
  291. if (data < 0)
  292. return data;
  293. data &= ~mask;
  294. data |= selector;
  295. return tps6507x_pmic_reg_write(tps, reg, data);
  296. }
  297. static struct regulator_ops tps6507x_pmic_ops = {
  298. .is_enabled = tps6507x_pmic_is_enabled,
  299. .enable = tps6507x_pmic_enable,
  300. .disable = tps6507x_pmic_disable,
  301. .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
  302. .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
  303. .list_voltage = regulator_list_voltage_table,
  304. };
  305. static int tps6507x_pmic_probe(struct platform_device *pdev)
  306. {
  307. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  308. struct tps_info *info = &tps6507x_pmic_regs[0];
  309. struct regulator_config config = { };
  310. struct regulator_init_data *init_data;
  311. struct regulator_dev *rdev;
  312. struct tps6507x_pmic *tps;
  313. struct tps6507x_board *tps_board;
  314. int i;
  315. int error;
  316. /**
  317. * tps_board points to pmic related constants
  318. * coming from the board-evm file.
  319. */
  320. tps_board = dev_get_platdata(tps6507x_dev->dev);
  321. if (!tps_board)
  322. return -EINVAL;
  323. /**
  324. * init_data points to array of regulator_init structures
  325. * coming from the board-evm file.
  326. */
  327. init_data = tps_board->tps6507x_pmic_init_data;
  328. if (!init_data)
  329. return -EINVAL;
  330. tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
  331. if (!tps)
  332. return -ENOMEM;
  333. mutex_init(&tps->io_lock);
  334. /* common for all regulators */
  335. tps->mfd = tps6507x_dev;
  336. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
  337. /* Register the regulators */
  338. tps->info[i] = info;
  339. if (init_data->driver_data) {
  340. struct tps6507x_reg_platform_data *data =
  341. init_data->driver_data;
  342. tps->info[i]->defdcdc_default = data->defdcdc_default;
  343. }
  344. tps->desc[i].name = info->name;
  345. tps->desc[i].id = i;
  346. tps->desc[i].n_voltages = info->table_len;
  347. tps->desc[i].volt_table = info->table;
  348. tps->desc[i].ops = &tps6507x_pmic_ops;
  349. tps->desc[i].type = REGULATOR_VOLTAGE;
  350. tps->desc[i].owner = THIS_MODULE;
  351. config.dev = tps6507x_dev->dev;
  352. config.init_data = init_data;
  353. config.driver_data = tps;
  354. rdev = regulator_register(&tps->desc[i], &config);
  355. if (IS_ERR(rdev)) {
  356. dev_err(tps6507x_dev->dev,
  357. "failed to register %s regulator\n",
  358. pdev->name);
  359. error = PTR_ERR(rdev);
  360. goto fail;
  361. }
  362. /* Save regulator for cleanup */
  363. tps->rdev[i] = rdev;
  364. }
  365. tps6507x_dev->pmic = tps;
  366. platform_set_drvdata(pdev, tps6507x_dev);
  367. return 0;
  368. fail:
  369. while (--i >= 0)
  370. regulator_unregister(tps->rdev[i]);
  371. return error;
  372. }
  373. static int tps6507x_pmic_remove(struct platform_device *pdev)
  374. {
  375. struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
  376. struct tps6507x_pmic *tps = tps6507x_dev->pmic;
  377. int i;
  378. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
  379. regulator_unregister(tps->rdev[i]);
  380. return 0;
  381. }
  382. static struct platform_driver tps6507x_pmic_driver = {
  383. .driver = {
  384. .name = "tps6507x-pmic",
  385. .owner = THIS_MODULE,
  386. },
  387. .probe = tps6507x_pmic_probe,
  388. .remove = tps6507x_pmic_remove,
  389. };
  390. static int __init tps6507x_pmic_init(void)
  391. {
  392. return platform_driver_register(&tps6507x_pmic_driver);
  393. }
  394. subsys_initcall(tps6507x_pmic_init);
  395. static void __exit tps6507x_pmic_cleanup(void)
  396. {
  397. platform_driver_unregister(&tps6507x_pmic_driver);
  398. }
  399. module_exit(tps6507x_pmic_cleanup);
  400. MODULE_AUTHOR("Texas Instruments");
  401. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  402. MODULE_LICENSE("GPL v2");
  403. MODULE_ALIAS("platform:tps6507x-pmic");