tps65023-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * tps65023-regulator.c
  3. *
  4. * Supports TPS65023 Regulator
  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/i2c.h>
  25. #include <linux/slab.h>
  26. #include <linux/regmap.h>
  27. /* Register definitions */
  28. #define TPS65023_REG_VERSION 0
  29. #define TPS65023_REG_PGOODZ 1
  30. #define TPS65023_REG_MASK 2
  31. #define TPS65023_REG_REG_CTRL 3
  32. #define TPS65023_REG_CON_CTRL 4
  33. #define TPS65023_REG_CON_CTRL2 5
  34. #define TPS65023_REG_DEF_CORE 6
  35. #define TPS65023_REG_DEFSLEW 7
  36. #define TPS65023_REG_LDO_CTRL 8
  37. /* PGOODZ bitfields */
  38. #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
  39. #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
  40. #define TPS65023_PGOODZ_VDCDC1 BIT(5)
  41. #define TPS65023_PGOODZ_VDCDC2 BIT(4)
  42. #define TPS65023_PGOODZ_VDCDC3 BIT(3)
  43. #define TPS65023_PGOODZ_LDO2 BIT(2)
  44. #define TPS65023_PGOODZ_LDO1 BIT(1)
  45. /* MASK bitfields */
  46. #define TPS65023_MASK_PWRFAILZ BIT(7)
  47. #define TPS65023_MASK_LOWBATTZ BIT(6)
  48. #define TPS65023_MASK_VDCDC1 BIT(5)
  49. #define TPS65023_MASK_VDCDC2 BIT(4)
  50. #define TPS65023_MASK_VDCDC3 BIT(3)
  51. #define TPS65023_MASK_LDO2 BIT(2)
  52. #define TPS65023_MASK_LDO1 BIT(1)
  53. /* REG_CTRL bitfields */
  54. #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
  55. #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
  56. #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
  57. #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
  58. #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
  59. /* REG_CTRL2 bitfields */
  60. #define TPS65023_REG_CTRL2_GO BIT(7)
  61. #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
  62. #define TPS65023_REG_CTRL2_DCDC2 BIT(2)
  63. #define TPS65023_REG_CTRL2_DCDC1 BIT(1)
  64. #define TPS65023_REG_CTRL2_DCDC3 BIT(0)
  65. /* LDO_CTRL bitfields */
  66. #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
  67. #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0x0F << ((ldo_id)*4))
  68. /* Number of step-down converters available */
  69. #define TPS65023_NUM_DCDC 3
  70. /* Number of LDO voltage regulators available */
  71. #define TPS65023_NUM_LDO 2
  72. /* Number of total regulators available */
  73. #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
  74. /* DCDCs */
  75. #define TPS65023_DCDC_1 0
  76. #define TPS65023_DCDC_2 1
  77. #define TPS65023_DCDC_3 2
  78. /* LDOs */
  79. #define TPS65023_LDO_1 3
  80. #define TPS65023_LDO_2 4
  81. #define TPS65023_MAX_REG_ID TPS65023_LDO_2
  82. /* Supported voltage values for regulators */
  83. static const u16 VCORE_VSEL_table[] = {
  84. 800, 825, 850, 875,
  85. 900, 925, 950, 975,
  86. 1000, 1025, 1050, 1075,
  87. 1100, 1125, 1150, 1175,
  88. 1200, 1225, 1250, 1275,
  89. 1300, 1325, 1350, 1375,
  90. 1400, 1425, 1450, 1475,
  91. 1500, 1525, 1550, 1600,
  92. };
  93. /* Supported voltage values for LDO regulators for tps65020 */
  94. static const u16 TPS65020_LDO1_VSEL_table[] = {
  95. 1000, 1050, 1100, 1300,
  96. 1800, 2500, 3000, 3300,
  97. };
  98. static const u16 TPS65020_LDO2_VSEL_table[] = {
  99. 1000, 1050, 1100, 1300,
  100. 1800, 2500, 3000, 3300,
  101. };
  102. /* Supported voltage values for LDO regulators
  103. * for tps65021 and tps65023 */
  104. static const u16 TPS65023_LDO1_VSEL_table[] = {
  105. 1000, 1100, 1300, 1800,
  106. 2200, 2600, 2800, 3150,
  107. };
  108. static const u16 TPS65023_LDO2_VSEL_table[] = {
  109. 1050, 1200, 1300, 1800,
  110. 2500, 2800, 3000, 3300,
  111. };
  112. /* Regulator specific details */
  113. struct tps_info {
  114. const char *name;
  115. unsigned min_uV;
  116. unsigned max_uV;
  117. bool fixed;
  118. u8 table_len;
  119. const u16 *table;
  120. };
  121. /* PMIC details */
  122. struct tps_pmic {
  123. struct regulator_desc desc[TPS65023_NUM_REGULATOR];
  124. struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
  125. const struct tps_info *info[TPS65023_NUM_REGULATOR];
  126. struct regmap *regmap;
  127. u8 core_regulator;
  128. };
  129. /* Struct passed as driver data */
  130. struct tps_driver_data {
  131. const struct tps_info *info;
  132. u8 core_regulator;
  133. };
  134. static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
  135. {
  136. struct tps_pmic *tps = rdev_get_drvdata(dev);
  137. int ret;
  138. int data, dcdc = rdev_get_id(dev);
  139. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  140. return -EINVAL;
  141. if (dcdc == tps->core_regulator) {
  142. ret = regmap_read(tps->regmap, TPS65023_REG_DEF_CORE, &data);
  143. if (ret != 0)
  144. return ret;
  145. data &= (tps->info[dcdc]->table_len - 1);
  146. return tps->info[dcdc]->table[data] * 1000;
  147. } else
  148. return tps->info[dcdc]->min_uV;
  149. }
  150. static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
  151. unsigned selector)
  152. {
  153. struct tps_pmic *tps = rdev_get_drvdata(dev);
  154. int dcdc = rdev_get_id(dev);
  155. int ret;
  156. if (dcdc != tps->core_regulator)
  157. return -EINVAL;
  158. ret = regmap_write(tps->regmap, TPS65023_REG_DEF_CORE, selector);
  159. if (ret)
  160. goto out;
  161. /* Tell the chip that we have changed the value in DEFCORE
  162. * and its time to update the core voltage
  163. */
  164. ret = regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  165. TPS65023_REG_CTRL2_GO, TPS65023_REG_CTRL2_GO);
  166. out:
  167. return ret;
  168. }
  169. static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
  170. {
  171. struct tps_pmic *tps = rdev_get_drvdata(dev);
  172. int data, ldo = rdev_get_id(dev);
  173. int ret;
  174. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  175. return -EINVAL;
  176. ret = regmap_read(tps->regmap, TPS65023_REG_LDO_CTRL, &data);
  177. if (ret != 0)
  178. return ret;
  179. data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
  180. data &= (tps->info[ldo]->table_len - 1);
  181. return tps->info[ldo]->table[data] * 1000;
  182. }
  183. static int tps65023_ldo_set_voltage_sel(struct regulator_dev *dev,
  184. unsigned selector)
  185. {
  186. struct tps_pmic *tps = rdev_get_drvdata(dev);
  187. int ldo_index = rdev_get_id(dev) - TPS65023_LDO_1;
  188. return regmap_update_bits(tps->regmap, TPS65023_REG_LDO_CTRL,
  189. TPS65023_LDO_CTRL_LDOx_MASK(ldo_index),
  190. selector << TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_index));
  191. }
  192. static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
  193. unsigned selector)
  194. {
  195. struct tps_pmic *tps = rdev_get_drvdata(dev);
  196. int dcdc = rdev_get_id(dev);
  197. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  198. return -EINVAL;
  199. if (dcdc == tps->core_regulator) {
  200. if (selector >= tps->info[dcdc]->table_len)
  201. return -EINVAL;
  202. else
  203. return tps->info[dcdc]->table[selector] * 1000;
  204. } else
  205. return tps->info[dcdc]->min_uV;
  206. }
  207. static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
  208. unsigned selector)
  209. {
  210. struct tps_pmic *tps = rdev_get_drvdata(dev);
  211. int ldo = rdev_get_id(dev);
  212. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  213. return -EINVAL;
  214. if (selector >= tps->info[ldo]->table_len)
  215. return -EINVAL;
  216. else
  217. return tps->info[ldo]->table[selector] * 1000;
  218. }
  219. /* Operations permitted on VDCDCx */
  220. static struct regulator_ops tps65023_dcdc_ops = {
  221. .is_enabled = regulator_is_enabled_regmap,
  222. .enable = regulator_enable_regmap,
  223. .disable = regulator_disable_regmap,
  224. .get_voltage = tps65023_dcdc_get_voltage,
  225. .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
  226. .list_voltage = tps65023_dcdc_list_voltage,
  227. };
  228. /* Operations permitted on LDOx */
  229. static struct regulator_ops tps65023_ldo_ops = {
  230. .is_enabled = regulator_is_enabled_regmap,
  231. .enable = regulator_enable_regmap,
  232. .disable = regulator_disable_regmap,
  233. .get_voltage = tps65023_ldo_get_voltage,
  234. .set_voltage_sel = tps65023_ldo_set_voltage_sel,
  235. .list_voltage = tps65023_ldo_list_voltage,
  236. };
  237. static struct regmap_config tps65023_regmap_config = {
  238. .reg_bits = 8,
  239. .val_bits = 8,
  240. };
  241. static int __devinit tps_65023_probe(struct i2c_client *client,
  242. const struct i2c_device_id *id)
  243. {
  244. const struct tps_driver_data *drv_data = (void *)id->driver_data;
  245. const struct tps_info *info = drv_data->info;
  246. struct regulator_config config = { };
  247. struct regulator_init_data *init_data;
  248. struct regulator_dev *rdev;
  249. struct tps_pmic *tps;
  250. int i;
  251. int error;
  252. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  253. return -EIO;
  254. /**
  255. * init_data points to array of regulator_init structures
  256. * coming from the board-evm file.
  257. */
  258. init_data = client->dev.platform_data;
  259. if (!init_data)
  260. return -EIO;
  261. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  262. if (!tps)
  263. return -ENOMEM;
  264. tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
  265. if (IS_ERR(tps->regmap)) {
  266. error = PTR_ERR(tps->regmap);
  267. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  268. error);
  269. return error;
  270. }
  271. /* common for all regulators */
  272. tps->core_regulator = drv_data->core_regulator;
  273. for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
  274. /* Store regulator specific information */
  275. tps->info[i] = info;
  276. tps->desc[i].name = info->name;
  277. tps->desc[i].id = i;
  278. tps->desc[i].n_voltages = info->table_len;
  279. tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
  280. &tps65023_ldo_ops : &tps65023_dcdc_ops);
  281. tps->desc[i].type = REGULATOR_VOLTAGE;
  282. tps->desc[i].owner = THIS_MODULE;
  283. tps->desc[i].enable_reg = TPS65023_REG_REG_CTRL;
  284. if (i == TPS65023_LDO_1)
  285. tps->desc[i].enable_mask = 1 << 1;
  286. else if (i == TPS65023_LDO_2)
  287. tps->desc[i].enable_mask = 1 << 2;
  288. else /* DCDCx */
  289. tps->desc[i].enable_mask =
  290. 1 << (TPS65023_NUM_REGULATOR - i);
  291. config.dev = &client->dev;
  292. config.init_data = init_data;
  293. config.driver_data = tps;
  294. config.regmap = tps->regmap;
  295. /* Register the regulators */
  296. rdev = regulator_register(&tps->desc[i], &config);
  297. if (IS_ERR(rdev)) {
  298. dev_err(&client->dev, "failed to register %s\n",
  299. id->name);
  300. error = PTR_ERR(rdev);
  301. goto fail;
  302. }
  303. /* Save regulator for cleanup */
  304. tps->rdev[i] = rdev;
  305. }
  306. i2c_set_clientdata(client, tps);
  307. /* Enable setting output voltage by I2C */
  308. regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  309. TPS65023_REG_CTRL2_CORE_ADJ, TPS65023_REG_CTRL2_CORE_ADJ);
  310. return 0;
  311. fail:
  312. while (--i >= 0)
  313. regulator_unregister(tps->rdev[i]);
  314. return error;
  315. }
  316. static int __devexit tps_65023_remove(struct i2c_client *client)
  317. {
  318. struct tps_pmic *tps = i2c_get_clientdata(client);
  319. int i;
  320. for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
  321. regulator_unregister(tps->rdev[i]);
  322. return 0;
  323. }
  324. static const struct tps_info tps65020_regs[] = {
  325. {
  326. .name = "VDCDC1",
  327. .min_uV = 3300000,
  328. .max_uV = 3300000,
  329. .fixed = 1,
  330. },
  331. {
  332. .name = "VDCDC2",
  333. .min_uV = 1800000,
  334. .max_uV = 1800000,
  335. .fixed = 1,
  336. },
  337. {
  338. .name = "VDCDC3",
  339. .min_uV = 800000,
  340. .max_uV = 1600000,
  341. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  342. .table = VCORE_VSEL_table,
  343. },
  344. {
  345. .name = "LDO1",
  346. .min_uV = 1000000,
  347. .max_uV = 3150000,
  348. .table_len = ARRAY_SIZE(TPS65020_LDO1_VSEL_table),
  349. .table = TPS65020_LDO1_VSEL_table,
  350. },
  351. {
  352. .name = "LDO2",
  353. .min_uV = 1050000,
  354. .max_uV = 3300000,
  355. .table_len = ARRAY_SIZE(TPS65020_LDO2_VSEL_table),
  356. .table = TPS65020_LDO2_VSEL_table,
  357. },
  358. };
  359. static const struct tps_info tps65021_regs[] = {
  360. {
  361. .name = "VDCDC1",
  362. .min_uV = 3300000,
  363. .max_uV = 3300000,
  364. .fixed = 1,
  365. },
  366. {
  367. .name = "VDCDC2",
  368. .min_uV = 1800000,
  369. .max_uV = 1800000,
  370. .fixed = 1,
  371. },
  372. {
  373. .name = "VDCDC3",
  374. .min_uV = 800000,
  375. .max_uV = 1600000,
  376. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  377. .table = VCORE_VSEL_table,
  378. },
  379. {
  380. .name = "LDO1",
  381. .min_uV = 1000000,
  382. .max_uV = 3150000,
  383. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  384. .table = TPS65023_LDO1_VSEL_table,
  385. },
  386. {
  387. .name = "LDO2",
  388. .min_uV = 1050000,
  389. .max_uV = 3300000,
  390. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  391. .table = TPS65023_LDO2_VSEL_table,
  392. },
  393. };
  394. static const struct tps_info tps65023_regs[] = {
  395. {
  396. .name = "VDCDC1",
  397. .min_uV = 800000,
  398. .max_uV = 1600000,
  399. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  400. .table = VCORE_VSEL_table,
  401. },
  402. {
  403. .name = "VDCDC2",
  404. .min_uV = 3300000,
  405. .max_uV = 3300000,
  406. .fixed = 1,
  407. },
  408. {
  409. .name = "VDCDC3",
  410. .min_uV = 1800000,
  411. .max_uV = 1800000,
  412. .fixed = 1,
  413. },
  414. {
  415. .name = "LDO1",
  416. .min_uV = 1000000,
  417. .max_uV = 3150000,
  418. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  419. .table = TPS65023_LDO1_VSEL_table,
  420. },
  421. {
  422. .name = "LDO2",
  423. .min_uV = 1050000,
  424. .max_uV = 3300000,
  425. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  426. .table = TPS65023_LDO2_VSEL_table,
  427. },
  428. };
  429. static struct tps_driver_data tps65020_drv_data = {
  430. .info = tps65020_regs,
  431. .core_regulator = TPS65023_DCDC_3,
  432. };
  433. static struct tps_driver_data tps65021_drv_data = {
  434. .info = tps65021_regs,
  435. .core_regulator = TPS65023_DCDC_3,
  436. };
  437. static struct tps_driver_data tps65023_drv_data = {
  438. .info = tps65023_regs,
  439. .core_regulator = TPS65023_DCDC_1,
  440. };
  441. static const struct i2c_device_id tps_65023_id[] = {
  442. {.name = "tps65023",
  443. .driver_data = (unsigned long) &tps65023_drv_data},
  444. {.name = "tps65021",
  445. .driver_data = (unsigned long) &tps65021_drv_data,},
  446. {.name = "tps65020",
  447. .driver_data = (unsigned long) &tps65020_drv_data},
  448. { },
  449. };
  450. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  451. static struct i2c_driver tps_65023_i2c_driver = {
  452. .driver = {
  453. .name = "tps65023",
  454. .owner = THIS_MODULE,
  455. },
  456. .probe = tps_65023_probe,
  457. .remove = __devexit_p(tps_65023_remove),
  458. .id_table = tps_65023_id,
  459. };
  460. static int __init tps_65023_init(void)
  461. {
  462. return i2c_add_driver(&tps_65023_i2c_driver);
  463. }
  464. subsys_initcall(tps_65023_init);
  465. static void __exit tps_65023_cleanup(void)
  466. {
  467. i2c_del_driver(&tps_65023_i2c_driver);
  468. }
  469. module_exit(tps_65023_cleanup);
  470. MODULE_AUTHOR("Texas Instruments");
  471. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  472. MODULE_LICENSE("GPL v2");