tps6507x-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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/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 regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
  108. struct tps_info *info[TPS6507X_NUM_REGULATOR];
  109. struct mutex io_lock;
  110. };
  111. static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
  112. {
  113. u8 val;
  114. int err;
  115. err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
  116. if (err)
  117. return err;
  118. return val;
  119. }
  120. static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  121. {
  122. return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
  123. }
  124. static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  125. {
  126. int err, data;
  127. mutex_lock(&tps->io_lock);
  128. data = tps6507x_pmic_read(tps, reg);
  129. if (data < 0) {
  130. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  131. err = data;
  132. goto out;
  133. }
  134. data |= mask;
  135. err = tps6507x_pmic_write(tps, reg, data);
  136. if (err)
  137. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  138. out:
  139. mutex_unlock(&tps->io_lock);
  140. return err;
  141. }
  142. static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  143. {
  144. int err, data;
  145. mutex_lock(&tps->io_lock);
  146. data = tps6507x_pmic_read(tps, reg);
  147. if (data < 0) {
  148. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  149. err = data;
  150. goto out;
  151. }
  152. data &= ~mask;
  153. err = tps6507x_pmic_write(tps, reg, data);
  154. if (err)
  155. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  156. out:
  157. mutex_unlock(&tps->io_lock);
  158. return err;
  159. }
  160. static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
  161. {
  162. int data;
  163. mutex_lock(&tps->io_lock);
  164. data = tps6507x_pmic_read(tps, reg);
  165. if (data < 0)
  166. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  167. mutex_unlock(&tps->io_lock);
  168. return data;
  169. }
  170. static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  171. {
  172. int err;
  173. mutex_lock(&tps->io_lock);
  174. err = tps6507x_pmic_write(tps, reg, val);
  175. if (err < 0)
  176. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  177. mutex_unlock(&tps->io_lock);
  178. return err;
  179. }
  180. static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
  181. {
  182. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  183. int data, rid = rdev_get_id(dev);
  184. u8 shift;
  185. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  186. return -EINVAL;
  187. shift = TPS6507X_MAX_REG_ID - rid;
  188. data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
  189. if (data < 0)
  190. return data;
  191. else
  192. return (data & 1<<shift) ? 1 : 0;
  193. }
  194. static int tps6507x_pmic_enable(struct regulator_dev *dev)
  195. {
  196. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  197. int 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. return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
  203. }
  204. static int tps6507x_pmic_disable(struct regulator_dev *dev)
  205. {
  206. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  207. int rid = rdev_get_id(dev);
  208. u8 shift;
  209. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  210. return -EINVAL;
  211. shift = TPS6507X_MAX_REG_ID - rid;
  212. return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
  213. 1 << shift);
  214. }
  215. static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
  216. {
  217. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  218. int data, rid = rdev_get_id(dev);
  219. u8 reg, mask;
  220. switch (rid) {
  221. case TPS6507X_DCDC_1:
  222. reg = TPS6507X_REG_DEFDCDC1;
  223. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  224. break;
  225. case TPS6507X_DCDC_2:
  226. if (tps->info[rid]->defdcdc_default)
  227. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  228. else
  229. reg = TPS6507X_REG_DEFDCDC2_LOW;
  230. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  231. break;
  232. case TPS6507X_DCDC_3:
  233. if (tps->info[rid]->defdcdc_default)
  234. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  235. else
  236. reg = TPS6507X_REG_DEFDCDC3_LOW;
  237. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  238. break;
  239. case TPS6507X_LDO_1:
  240. reg = TPS6507X_REG_LDO_CTRL1;
  241. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  242. break;
  243. case TPS6507X_LDO_2:
  244. reg = TPS6507X_REG_DEFLDO2;
  245. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  246. break;
  247. default:
  248. return -EINVAL;
  249. }
  250. data = tps6507x_pmic_reg_read(tps, reg);
  251. if (data < 0)
  252. return data;
  253. data &= mask;
  254. return data;
  255. }
  256. static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
  257. unsigned selector)
  258. {
  259. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  260. int data, rid = rdev_get_id(dev);
  261. u8 reg, mask;
  262. switch (rid) {
  263. case TPS6507X_DCDC_1:
  264. reg = TPS6507X_REG_DEFDCDC1;
  265. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  266. break;
  267. case TPS6507X_DCDC_2:
  268. if (tps->info[rid]->defdcdc_default)
  269. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  270. else
  271. reg = TPS6507X_REG_DEFDCDC2_LOW;
  272. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  273. break;
  274. case TPS6507X_DCDC_3:
  275. if (tps->info[rid]->defdcdc_default)
  276. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  277. else
  278. reg = TPS6507X_REG_DEFDCDC3_LOW;
  279. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  280. break;
  281. case TPS6507X_LDO_1:
  282. reg = TPS6507X_REG_LDO_CTRL1;
  283. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  284. break;
  285. case TPS6507X_LDO_2:
  286. reg = TPS6507X_REG_DEFLDO2;
  287. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  288. break;
  289. default:
  290. return -EINVAL;
  291. }
  292. data = tps6507x_pmic_reg_read(tps, reg);
  293. if (data < 0)
  294. return data;
  295. data &= ~mask;
  296. data |= selector;
  297. return tps6507x_pmic_reg_write(tps, reg, data);
  298. }
  299. static struct regulator_ops tps6507x_pmic_ops = {
  300. .is_enabled = tps6507x_pmic_is_enabled,
  301. .enable = tps6507x_pmic_enable,
  302. .disable = tps6507x_pmic_disable,
  303. .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
  304. .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
  305. .list_voltage = regulator_list_voltage_table,
  306. .map_voltage = regulator_map_voltage_ascend,
  307. };
  308. static struct of_regulator_match tps6507x_matches[] = {
  309. { .name = "VDCDC1"},
  310. { .name = "VDCDC2"},
  311. { .name = "VDCDC3"},
  312. { .name = "LDO1"},
  313. { .name = "LDO2"},
  314. };
  315. static struct tps6507x_board *tps6507x_parse_dt_reg_data(
  316. struct platform_device *pdev,
  317. struct of_regulator_match **tps6507x_reg_matches)
  318. {
  319. struct tps6507x_board *tps_board;
  320. struct device_node *np = pdev->dev.parent->of_node;
  321. struct device_node *regulators;
  322. struct of_regulator_match *matches;
  323. static struct regulator_init_data *reg_data;
  324. int idx = 0, count, ret;
  325. tps_board = devm_kzalloc(&pdev->dev, sizeof(*tps_board),
  326. GFP_KERNEL);
  327. if (!tps_board)
  328. return NULL;
  329. regulators = of_get_child_by_name(np, "regulators");
  330. if (!regulators) {
  331. dev_err(&pdev->dev, "regulator node not found\n");
  332. return NULL;
  333. }
  334. count = ARRAY_SIZE(tps6507x_matches);
  335. matches = tps6507x_matches;
  336. ret = of_regulator_match(&pdev->dev, regulators, matches, count);
  337. of_node_put(regulators);
  338. if (ret < 0) {
  339. dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
  340. ret);
  341. return NULL;
  342. }
  343. *tps6507x_reg_matches = matches;
  344. reg_data = devm_kzalloc(&pdev->dev, (sizeof(struct regulator_init_data)
  345. * TPS6507X_NUM_REGULATOR), GFP_KERNEL);
  346. if (!reg_data)
  347. return NULL;
  348. tps_board->tps6507x_pmic_init_data = reg_data;
  349. for (idx = 0; idx < count; idx++) {
  350. if (!matches[idx].init_data || !matches[idx].of_node)
  351. continue;
  352. memcpy(&reg_data[idx], matches[idx].init_data,
  353. sizeof(struct regulator_init_data));
  354. }
  355. return tps_board;
  356. }
  357. static int tps6507x_pmic_probe(struct platform_device *pdev)
  358. {
  359. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  360. struct tps_info *info = &tps6507x_pmic_regs[0];
  361. struct regulator_config config = { };
  362. struct regulator_init_data *init_data;
  363. struct regulator_dev *rdev;
  364. struct tps6507x_pmic *tps;
  365. struct tps6507x_board *tps_board;
  366. struct of_regulator_match *tps6507x_reg_matches = NULL;
  367. int i;
  368. int error;
  369. unsigned int prop;
  370. /**
  371. * tps_board points to pmic related constants
  372. * coming from the board-evm file.
  373. */
  374. tps_board = dev_get_platdata(tps6507x_dev->dev);
  375. if (IS_ENABLED(CONFIG_OF) && !tps_board &&
  376. tps6507x_dev->dev->of_node)
  377. tps_board = tps6507x_parse_dt_reg_data(pdev,
  378. &tps6507x_reg_matches);
  379. if (!tps_board)
  380. return -EINVAL;
  381. /**
  382. * init_data points to array of regulator_init structures
  383. * coming from the board-evm file.
  384. */
  385. init_data = tps_board->tps6507x_pmic_init_data;
  386. if (!init_data)
  387. return -EINVAL;
  388. tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
  389. if (!tps)
  390. return -ENOMEM;
  391. mutex_init(&tps->io_lock);
  392. /* common for all regulators */
  393. tps->mfd = tps6507x_dev;
  394. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
  395. /* Register the regulators */
  396. tps->info[i] = info;
  397. if (init_data->driver_data) {
  398. struct tps6507x_reg_platform_data *data =
  399. init_data->driver_data;
  400. tps->info[i]->defdcdc_default = data->defdcdc_default;
  401. }
  402. tps->desc[i].name = info->name;
  403. tps->desc[i].id = i;
  404. tps->desc[i].n_voltages = info->table_len;
  405. tps->desc[i].volt_table = info->table;
  406. tps->desc[i].ops = &tps6507x_pmic_ops;
  407. tps->desc[i].type = REGULATOR_VOLTAGE;
  408. tps->desc[i].owner = THIS_MODULE;
  409. config.dev = tps6507x_dev->dev;
  410. config.init_data = init_data;
  411. config.driver_data = tps;
  412. if (tps6507x_reg_matches) {
  413. error = of_property_read_u32(
  414. tps6507x_reg_matches[i].of_node,
  415. "ti,defdcdc_default", &prop);
  416. if (!error)
  417. tps->info[i]->defdcdc_default = prop;
  418. config.of_node = tps6507x_reg_matches[i].of_node;
  419. }
  420. rdev = devm_regulator_register(&pdev->dev, &tps->desc[i],
  421. &config);
  422. if (IS_ERR(rdev)) {
  423. dev_err(tps6507x_dev->dev,
  424. "failed to register %s regulator\n",
  425. pdev->name);
  426. return PTR_ERR(rdev);
  427. }
  428. /* Save regulator for cleanup */
  429. tps->rdev[i] = rdev;
  430. }
  431. tps6507x_dev->pmic = tps;
  432. platform_set_drvdata(pdev, tps6507x_dev);
  433. return 0;
  434. }
  435. static struct platform_driver tps6507x_pmic_driver = {
  436. .driver = {
  437. .name = "tps6507x-pmic",
  438. },
  439. .probe = tps6507x_pmic_probe,
  440. };
  441. static int __init tps6507x_pmic_init(void)
  442. {
  443. return platform_driver_register(&tps6507x_pmic_driver);
  444. }
  445. subsys_initcall(tps6507x_pmic_init);
  446. static void __exit tps6507x_pmic_cleanup(void)
  447. {
  448. platform_driver_unregister(&tps6507x_pmic_driver);
  449. }
  450. module_exit(tps6507x_pmic_cleanup);
  451. MODULE_AUTHOR("Texas Instruments");
  452. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  453. MODULE_LICENSE("GPL v2");
  454. MODULE_ALIAS("platform:tps6507x-pmic");