tps6507x-regulator.c 11 KB

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