tps6507x-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 milliVolts) */
  42. static const u16 VDCDCx_VSEL_table[] = {
  43. 725, 750, 775, 800,
  44. 825, 850, 875, 900,
  45. 925, 950, 975, 1000,
  46. 1025, 1050, 1075, 1100,
  47. 1125, 1150, 1175, 1200,
  48. 1225, 1250, 1275, 1300,
  49. 1325, 1350, 1375, 1400,
  50. 1425, 1450, 1475, 1500,
  51. 1550, 1600, 1650, 1700,
  52. 1750, 1800, 1850, 1900,
  53. 1950, 2000, 2050, 2100,
  54. 2150, 2200, 2250, 2300,
  55. 2350, 2400, 2450, 2500,
  56. 2550, 2600, 2650, 2700,
  57. 2750, 2800, 2850, 2900,
  58. 3000, 3100, 3200, 3300,
  59. };
  60. static const u16 LDO1_VSEL_table[] = {
  61. 1000, 1100, 1200, 1250,
  62. 1300, 1350, 1400, 1500,
  63. 1600, 1800, 2500, 2750,
  64. 2800, 3000, 3100, 3300,
  65. };
  66. static const u16 LDO2_VSEL_table[] = {
  67. 725, 750, 775, 800,
  68. 825, 850, 875, 900,
  69. 925, 950, 975, 1000,
  70. 1025, 1050, 1075, 1100,
  71. 1125, 1150, 1175, 1200,
  72. 1225, 1250, 1275, 1300,
  73. 1325, 1350, 1375, 1400,
  74. 1425, 1450, 1475, 1500,
  75. 1550, 1600, 1650, 1700,
  76. 1750, 1800, 1850, 1900,
  77. 1950, 2000, 2050, 2100,
  78. 2150, 2200, 2250, 2300,
  79. 2350, 2400, 2450, 2500,
  80. 2550, 2600, 2650, 2700,
  81. 2750, 2800, 2850, 2900,
  82. 3000, 3100, 3200, 3300,
  83. };
  84. struct tps_info {
  85. const char *name;
  86. unsigned min_uV;
  87. unsigned max_uV;
  88. u8 table_len;
  89. const u16 *table;
  90. /* Does DCDC high or the low register defines output voltage? */
  91. bool defdcdc_default;
  92. };
  93. static struct tps_info tps6507x_pmic_regs[] = {
  94. {
  95. .name = "VDCDC1",
  96. .min_uV = 725000,
  97. .max_uV = 3300000,
  98. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  99. .table = VDCDCx_VSEL_table,
  100. },
  101. {
  102. .name = "VDCDC2",
  103. .min_uV = 725000,
  104. .max_uV = 3300000,
  105. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  106. .table = VDCDCx_VSEL_table,
  107. },
  108. {
  109. .name = "VDCDC3",
  110. .min_uV = 725000,
  111. .max_uV = 3300000,
  112. .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
  113. .table = VDCDCx_VSEL_table,
  114. },
  115. {
  116. .name = "LDO1",
  117. .min_uV = 1000000,
  118. .max_uV = 3300000,
  119. .table_len = ARRAY_SIZE(LDO1_VSEL_table),
  120. .table = LDO1_VSEL_table,
  121. },
  122. {
  123. .name = "LDO2",
  124. .min_uV = 725000,
  125. .max_uV = 3300000,
  126. .table_len = ARRAY_SIZE(LDO2_VSEL_table),
  127. .table = LDO2_VSEL_table,
  128. },
  129. };
  130. struct tps6507x_pmic {
  131. struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
  132. struct tps6507x_dev *mfd;
  133. struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
  134. struct tps_info *info[TPS6507X_NUM_REGULATOR];
  135. struct mutex io_lock;
  136. };
  137. static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
  138. {
  139. u8 val;
  140. int err;
  141. err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
  142. if (err)
  143. return err;
  144. return val;
  145. }
  146. static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  147. {
  148. return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
  149. }
  150. static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  151. {
  152. int err, data;
  153. mutex_lock(&tps->io_lock);
  154. data = tps6507x_pmic_read(tps, reg);
  155. if (data < 0) {
  156. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  157. err = data;
  158. goto out;
  159. }
  160. data |= mask;
  161. err = tps6507x_pmic_write(tps, reg, data);
  162. if (err)
  163. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  164. out:
  165. mutex_unlock(&tps->io_lock);
  166. return err;
  167. }
  168. static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
  169. {
  170. int err, data;
  171. mutex_lock(&tps->io_lock);
  172. data = tps6507x_pmic_read(tps, reg);
  173. if (data < 0) {
  174. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  175. err = data;
  176. goto out;
  177. }
  178. data &= ~mask;
  179. err = tps6507x_pmic_write(tps, reg, data);
  180. if (err)
  181. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  182. out:
  183. mutex_unlock(&tps->io_lock);
  184. return err;
  185. }
  186. static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
  187. {
  188. int data;
  189. mutex_lock(&tps->io_lock);
  190. data = tps6507x_pmic_read(tps, reg);
  191. if (data < 0)
  192. dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
  193. mutex_unlock(&tps->io_lock);
  194. return data;
  195. }
  196. static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
  197. {
  198. int err;
  199. mutex_lock(&tps->io_lock);
  200. err = tps6507x_pmic_write(tps, reg, val);
  201. if (err < 0)
  202. dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
  203. mutex_unlock(&tps->io_lock);
  204. return err;
  205. }
  206. static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
  207. {
  208. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  209. int data, rid = rdev_get_id(dev);
  210. u8 shift;
  211. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  212. return -EINVAL;
  213. shift = TPS6507X_MAX_REG_ID - rid;
  214. data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
  215. if (data < 0)
  216. return data;
  217. else
  218. return (data & 1<<shift) ? 1 : 0;
  219. }
  220. static int tps6507x_pmic_enable(struct regulator_dev *dev)
  221. {
  222. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  223. int rid = rdev_get_id(dev);
  224. u8 shift;
  225. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  226. return -EINVAL;
  227. shift = TPS6507X_MAX_REG_ID - rid;
  228. return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
  229. }
  230. static int tps6507x_pmic_disable(struct regulator_dev *dev)
  231. {
  232. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  233. int rid = rdev_get_id(dev);
  234. u8 shift;
  235. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  236. return -EINVAL;
  237. shift = TPS6507X_MAX_REG_ID - rid;
  238. return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
  239. 1 << shift);
  240. }
  241. static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
  242. {
  243. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  244. int data, rid = rdev_get_id(dev);
  245. u8 reg, mask;
  246. switch (rid) {
  247. case TPS6507X_DCDC_1:
  248. reg = TPS6507X_REG_DEFDCDC1;
  249. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  250. break;
  251. case TPS6507X_DCDC_2:
  252. if (tps->info[rid]->defdcdc_default)
  253. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  254. else
  255. reg = TPS6507X_REG_DEFDCDC2_LOW;
  256. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  257. break;
  258. case TPS6507X_DCDC_3:
  259. if (tps->info[rid]->defdcdc_default)
  260. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  261. else
  262. reg = TPS6507X_REG_DEFDCDC3_LOW;
  263. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  264. break;
  265. case TPS6507X_LDO_1:
  266. reg = TPS6507X_REG_LDO_CTRL1;
  267. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  268. break;
  269. case TPS6507X_LDO_2:
  270. reg = TPS6507X_REG_DEFLDO2;
  271. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. data = tps6507x_pmic_reg_read(tps, reg);
  277. if (data < 0)
  278. return data;
  279. data &= mask;
  280. return data;
  281. }
  282. static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
  283. unsigned selector)
  284. {
  285. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  286. int data, rid = rdev_get_id(dev);
  287. u8 reg, mask;
  288. switch (rid) {
  289. case TPS6507X_DCDC_1:
  290. reg = TPS6507X_REG_DEFDCDC1;
  291. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  292. break;
  293. case TPS6507X_DCDC_2:
  294. if (tps->info[rid]->defdcdc_default)
  295. reg = TPS6507X_REG_DEFDCDC2_HIGH;
  296. else
  297. reg = TPS6507X_REG_DEFDCDC2_LOW;
  298. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  299. break;
  300. case TPS6507X_DCDC_3:
  301. if (tps->info[rid]->defdcdc_default)
  302. reg = TPS6507X_REG_DEFDCDC3_HIGH;
  303. else
  304. reg = TPS6507X_REG_DEFDCDC3_LOW;
  305. mask = TPS6507X_DEFDCDCX_DCDC_MASK;
  306. break;
  307. case TPS6507X_LDO_1:
  308. reg = TPS6507X_REG_LDO_CTRL1;
  309. mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
  310. break;
  311. case TPS6507X_LDO_2:
  312. reg = TPS6507X_REG_DEFLDO2;
  313. mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. data = tps6507x_pmic_reg_read(tps, reg);
  319. if (data < 0)
  320. return data;
  321. data &= ~mask;
  322. data |= selector;
  323. return tps6507x_pmic_reg_write(tps, reg, data);
  324. }
  325. static int tps6507x_pmic_list_voltage(struct regulator_dev *dev,
  326. unsigned selector)
  327. {
  328. struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
  329. int rid = rdev_get_id(dev);
  330. if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
  331. return -EINVAL;
  332. if (selector >= tps->info[rid]->table_len)
  333. return -EINVAL;
  334. else
  335. return tps->info[rid]->table[selector] * 1000;
  336. }
  337. static struct regulator_ops tps6507x_pmic_ops = {
  338. .is_enabled = tps6507x_pmic_is_enabled,
  339. .enable = tps6507x_pmic_enable,
  340. .disable = tps6507x_pmic_disable,
  341. .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
  342. .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
  343. .list_voltage = tps6507x_pmic_list_voltage,
  344. };
  345. static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
  346. {
  347. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  348. struct tps_info *info = &tps6507x_pmic_regs[0];
  349. struct regulator_config config = { };
  350. struct regulator_init_data *init_data;
  351. struct regulator_dev *rdev;
  352. struct tps6507x_pmic *tps;
  353. struct tps6507x_board *tps_board;
  354. int i;
  355. int error;
  356. /**
  357. * tps_board points to pmic related constants
  358. * coming from the board-evm file.
  359. */
  360. tps_board = dev_get_platdata(tps6507x_dev->dev);
  361. if (!tps_board)
  362. return -EINVAL;
  363. /**
  364. * init_data points to array of regulator_init structures
  365. * coming from the board-evm file.
  366. */
  367. init_data = tps_board->tps6507x_pmic_init_data;
  368. if (!init_data)
  369. return -EINVAL;
  370. tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
  371. if (!tps)
  372. return -ENOMEM;
  373. mutex_init(&tps->io_lock);
  374. /* common for all regulators */
  375. tps->mfd = tps6507x_dev;
  376. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
  377. /* Register the regulators */
  378. tps->info[i] = info;
  379. if (init_data->driver_data) {
  380. struct tps6507x_reg_platform_data *data =
  381. init_data->driver_data;
  382. tps->info[i]->defdcdc_default = data->defdcdc_default;
  383. }
  384. tps->desc[i].name = info->name;
  385. tps->desc[i].id = i;
  386. tps->desc[i].n_voltages = info->table_len;
  387. tps->desc[i].ops = &tps6507x_pmic_ops;
  388. tps->desc[i].type = REGULATOR_VOLTAGE;
  389. tps->desc[i].owner = THIS_MODULE;
  390. config.dev = tps6507x_dev->dev;
  391. config.init_data = init_data;
  392. config.driver_data = tps;
  393. rdev = regulator_register(&tps->desc[i], &config);
  394. if (IS_ERR(rdev)) {
  395. dev_err(tps6507x_dev->dev,
  396. "failed to register %s regulator\n",
  397. pdev->name);
  398. error = PTR_ERR(rdev);
  399. goto fail;
  400. }
  401. /* Save regulator for cleanup */
  402. tps->rdev[i] = rdev;
  403. }
  404. tps6507x_dev->pmic = tps;
  405. platform_set_drvdata(pdev, tps6507x_dev);
  406. return 0;
  407. fail:
  408. while (--i >= 0)
  409. regulator_unregister(tps->rdev[i]);
  410. return error;
  411. }
  412. static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
  413. {
  414. struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
  415. struct tps6507x_pmic *tps = tps6507x_dev->pmic;
  416. int i;
  417. for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
  418. regulator_unregister(tps->rdev[i]);
  419. return 0;
  420. }
  421. static struct platform_driver tps6507x_pmic_driver = {
  422. .driver = {
  423. .name = "tps6507x-pmic",
  424. .owner = THIS_MODULE,
  425. },
  426. .probe = tps6507x_pmic_probe,
  427. .remove = __devexit_p(tps6507x_pmic_remove),
  428. };
  429. static int __init tps6507x_pmic_init(void)
  430. {
  431. return platform_driver_register(&tps6507x_pmic_driver);
  432. }
  433. subsys_initcall(tps6507x_pmic_init);
  434. static void __exit tps6507x_pmic_cleanup(void)
  435. {
  436. platform_driver_unregister(&tps6507x_pmic_driver);
  437. }
  438. module_exit(tps6507x_pmic_cleanup);
  439. MODULE_AUTHOR("Texas Instruments");
  440. MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
  441. MODULE_LICENSE("GPL v2");
  442. MODULE_ALIAS("platform:tps6507x-pmic");