tps6507x-regulator.c 11 KB

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