tps65023-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 unsigned int VCORE_VSEL_table[] = {
  84. 800000, 825000, 850000, 875000,
  85. 900000, 925000, 950000, 975000,
  86. 1000000, 1025000, 1050000, 1075000,
  87. 1100000, 1125000, 1150000, 1175000,
  88. 1200000, 1225000, 1250000, 1275000,
  89. 1300000, 1325000, 1350000, 1375000,
  90. 1400000, 1425000, 1450000, 1475000,
  91. 1500000, 1525000, 1550000, 1600000,
  92. };
  93. static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
  94. 3300000,
  95. };
  96. static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
  97. 1800000,
  98. };
  99. /* Supported voltage values for LDO regulators for tps65020 */
  100. static const unsigned int TPS65020_LDO1_VSEL_table[] = {
  101. 1000000, 1050000, 1100000, 1300000,
  102. 1800000, 2500000, 3000000, 3300000,
  103. };
  104. static const unsigned int TPS65020_LDO2_VSEL_table[] = {
  105. 1000000, 1050000, 1100000, 1300000,
  106. 1800000, 2500000, 3000000, 3300000,
  107. };
  108. /* Supported voltage values for LDO regulators
  109. * for tps65021 and tps65023 */
  110. static const unsigned int TPS65023_LDO1_VSEL_table[] = {
  111. 1000000, 1100000, 1300000, 1800000,
  112. 2200000, 2600000, 2800000, 3150000,
  113. };
  114. static const unsigned int TPS65023_LDO2_VSEL_table[] = {
  115. 1050000, 1200000, 1300000, 1800000,
  116. 2500000, 2800000, 3000000, 3300000,
  117. };
  118. /* Regulator specific details */
  119. struct tps_info {
  120. const char *name;
  121. u8 table_len;
  122. const unsigned int *table;
  123. };
  124. /* PMIC details */
  125. struct tps_pmic {
  126. struct regulator_desc desc[TPS65023_NUM_REGULATOR];
  127. struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
  128. const struct tps_info *info[TPS65023_NUM_REGULATOR];
  129. struct regmap *regmap;
  130. u8 core_regulator;
  131. };
  132. /* Struct passed as driver data */
  133. struct tps_driver_data {
  134. const struct tps_info *info;
  135. u8 core_regulator;
  136. };
  137. static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
  138. {
  139. struct tps_pmic *tps = rdev_get_drvdata(dev);
  140. int ret;
  141. int data, dcdc = rdev_get_id(dev);
  142. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  143. return -EINVAL;
  144. if (dcdc == tps->core_regulator) {
  145. ret = regmap_read(tps->regmap, TPS65023_REG_DEF_CORE, &data);
  146. if (ret != 0)
  147. return ret;
  148. data &= (tps->info[dcdc]->table_len - 1);
  149. return data;
  150. } else
  151. return 0;
  152. }
  153. static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
  154. unsigned selector)
  155. {
  156. struct tps_pmic *tps = rdev_get_drvdata(dev);
  157. int dcdc = rdev_get_id(dev);
  158. int ret;
  159. if (dcdc != tps->core_regulator)
  160. return -EINVAL;
  161. ret = regmap_write(tps->regmap, TPS65023_REG_DEF_CORE, selector);
  162. if (ret)
  163. goto out;
  164. /* Tell the chip that we have changed the value in DEFCORE
  165. * and its time to update the core voltage
  166. */
  167. ret = regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  168. TPS65023_REG_CTRL2_GO, TPS65023_REG_CTRL2_GO);
  169. out:
  170. return ret;
  171. }
  172. static int tps65023_ldo_get_voltage_sel(struct regulator_dev *dev)
  173. {
  174. struct tps_pmic *tps = rdev_get_drvdata(dev);
  175. int data, ldo = rdev_get_id(dev);
  176. int ret;
  177. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  178. return -EINVAL;
  179. ret = regmap_read(tps->regmap, TPS65023_REG_LDO_CTRL, &data);
  180. if (ret != 0)
  181. return ret;
  182. data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
  183. data &= (tps->info[ldo]->table_len - 1);
  184. return data;
  185. }
  186. static int tps65023_ldo_set_voltage_sel(struct regulator_dev *dev,
  187. unsigned selector)
  188. {
  189. struct tps_pmic *tps = rdev_get_drvdata(dev);
  190. int ldo_index = rdev_get_id(dev) - TPS65023_LDO_1;
  191. return regmap_update_bits(tps->regmap, TPS65023_REG_LDO_CTRL,
  192. TPS65023_LDO_CTRL_LDOx_MASK(ldo_index),
  193. selector << TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_index));
  194. }
  195. /* Operations permitted on VDCDCx */
  196. static struct regulator_ops tps65023_dcdc_ops = {
  197. .is_enabled = regulator_is_enabled_regmap,
  198. .enable = regulator_enable_regmap,
  199. .disable = regulator_disable_regmap,
  200. .get_voltage_sel = tps65023_dcdc_get_voltage_sel,
  201. .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
  202. .list_voltage = regulator_list_voltage_table,
  203. };
  204. /* Operations permitted on LDOx */
  205. static struct regulator_ops tps65023_ldo_ops = {
  206. .is_enabled = regulator_is_enabled_regmap,
  207. .enable = regulator_enable_regmap,
  208. .disable = regulator_disable_regmap,
  209. .get_voltage_sel = tps65023_ldo_get_voltage_sel,
  210. .set_voltage_sel = tps65023_ldo_set_voltage_sel,
  211. .list_voltage = regulator_list_voltage_table,
  212. };
  213. static struct regmap_config tps65023_regmap_config = {
  214. .reg_bits = 8,
  215. .val_bits = 8,
  216. };
  217. static int __devinit tps_65023_probe(struct i2c_client *client,
  218. const struct i2c_device_id *id)
  219. {
  220. const struct tps_driver_data *drv_data = (void *)id->driver_data;
  221. const struct tps_info *info = drv_data->info;
  222. struct regulator_config config = { };
  223. struct regulator_init_data *init_data;
  224. struct regulator_dev *rdev;
  225. struct tps_pmic *tps;
  226. int i;
  227. int error;
  228. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  229. return -EIO;
  230. /**
  231. * init_data points to array of regulator_init structures
  232. * coming from the board-evm file.
  233. */
  234. init_data = client->dev.platform_data;
  235. if (!init_data)
  236. return -EIO;
  237. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  238. if (!tps)
  239. return -ENOMEM;
  240. tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
  241. if (IS_ERR(tps->regmap)) {
  242. error = PTR_ERR(tps->regmap);
  243. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  244. error);
  245. return error;
  246. }
  247. /* common for all regulators */
  248. tps->core_regulator = drv_data->core_regulator;
  249. for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
  250. /* Store regulator specific information */
  251. tps->info[i] = info;
  252. tps->desc[i].name = info->name;
  253. tps->desc[i].id = i;
  254. tps->desc[i].n_voltages = info->table_len;
  255. tps->desc[i].volt_table = info->table;
  256. tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
  257. &tps65023_ldo_ops : &tps65023_dcdc_ops);
  258. tps->desc[i].type = REGULATOR_VOLTAGE;
  259. tps->desc[i].owner = THIS_MODULE;
  260. tps->desc[i].enable_reg = TPS65023_REG_REG_CTRL;
  261. if (i == TPS65023_LDO_1)
  262. tps->desc[i].enable_mask = 1 << 1;
  263. else if (i == TPS65023_LDO_2)
  264. tps->desc[i].enable_mask = 1 << 2;
  265. else /* DCDCx */
  266. tps->desc[i].enable_mask =
  267. 1 << (TPS65023_NUM_REGULATOR - i);
  268. config.dev = &client->dev;
  269. config.init_data = init_data;
  270. config.driver_data = tps;
  271. config.regmap = tps->regmap;
  272. /* Register the regulators */
  273. rdev = regulator_register(&tps->desc[i], &config);
  274. if (IS_ERR(rdev)) {
  275. dev_err(&client->dev, "failed to register %s\n",
  276. id->name);
  277. error = PTR_ERR(rdev);
  278. goto fail;
  279. }
  280. /* Save regulator for cleanup */
  281. tps->rdev[i] = rdev;
  282. }
  283. i2c_set_clientdata(client, tps);
  284. /* Enable setting output voltage by I2C */
  285. regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  286. TPS65023_REG_CTRL2_CORE_ADJ, TPS65023_REG_CTRL2_CORE_ADJ);
  287. return 0;
  288. fail:
  289. while (--i >= 0)
  290. regulator_unregister(tps->rdev[i]);
  291. return error;
  292. }
  293. static int __devexit tps_65023_remove(struct i2c_client *client)
  294. {
  295. struct tps_pmic *tps = i2c_get_clientdata(client);
  296. int i;
  297. for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
  298. regulator_unregister(tps->rdev[i]);
  299. return 0;
  300. }
  301. static const struct tps_info tps65020_regs[] = {
  302. {
  303. .name = "VDCDC1",
  304. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  305. .table = DCDC_FIXED_3300000_VSEL_table,
  306. },
  307. {
  308. .name = "VDCDC2",
  309. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  310. .table = DCDC_FIXED_1800000_VSEL_table,
  311. },
  312. {
  313. .name = "VDCDC3",
  314. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  315. .table = VCORE_VSEL_table,
  316. },
  317. {
  318. .name = "LDO1",
  319. .table_len = ARRAY_SIZE(TPS65020_LDO1_VSEL_table),
  320. .table = TPS65020_LDO1_VSEL_table,
  321. },
  322. {
  323. .name = "LDO2",
  324. .table_len = ARRAY_SIZE(TPS65020_LDO2_VSEL_table),
  325. .table = TPS65020_LDO2_VSEL_table,
  326. },
  327. };
  328. static const struct tps_info tps65021_regs[] = {
  329. {
  330. .name = "VDCDC1",
  331. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  332. .table = DCDC_FIXED_3300000_VSEL_table,
  333. },
  334. {
  335. .name = "VDCDC2",
  336. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  337. .table = DCDC_FIXED_1800000_VSEL_table,
  338. },
  339. {
  340. .name = "VDCDC3",
  341. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  342. .table = VCORE_VSEL_table,
  343. },
  344. {
  345. .name = "LDO1",
  346. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  347. .table = TPS65023_LDO1_VSEL_table,
  348. },
  349. {
  350. .name = "LDO2",
  351. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  352. .table = TPS65023_LDO2_VSEL_table,
  353. },
  354. };
  355. static const struct tps_info tps65023_regs[] = {
  356. {
  357. .name = "VDCDC1",
  358. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  359. .table = VCORE_VSEL_table,
  360. },
  361. {
  362. .name = "VDCDC2",
  363. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  364. .table = DCDC_FIXED_3300000_VSEL_table,
  365. },
  366. {
  367. .name = "VDCDC3",
  368. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  369. .table = DCDC_FIXED_1800000_VSEL_table,
  370. },
  371. {
  372. .name = "LDO1",
  373. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  374. .table = TPS65023_LDO1_VSEL_table,
  375. },
  376. {
  377. .name = "LDO2",
  378. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  379. .table = TPS65023_LDO2_VSEL_table,
  380. },
  381. };
  382. static struct tps_driver_data tps65020_drv_data = {
  383. .info = tps65020_regs,
  384. .core_regulator = TPS65023_DCDC_3,
  385. };
  386. static struct tps_driver_data tps65021_drv_data = {
  387. .info = tps65021_regs,
  388. .core_regulator = TPS65023_DCDC_3,
  389. };
  390. static struct tps_driver_data tps65023_drv_data = {
  391. .info = tps65023_regs,
  392. .core_regulator = TPS65023_DCDC_1,
  393. };
  394. static const struct i2c_device_id tps_65023_id[] = {
  395. {.name = "tps65023",
  396. .driver_data = (unsigned long) &tps65023_drv_data},
  397. {.name = "tps65021",
  398. .driver_data = (unsigned long) &tps65021_drv_data,},
  399. {.name = "tps65020",
  400. .driver_data = (unsigned long) &tps65020_drv_data},
  401. { },
  402. };
  403. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  404. static struct i2c_driver tps_65023_i2c_driver = {
  405. .driver = {
  406. .name = "tps65023",
  407. .owner = THIS_MODULE,
  408. },
  409. .probe = tps_65023_probe,
  410. .remove = __devexit_p(tps_65023_remove),
  411. .id_table = tps_65023_id,
  412. };
  413. static int __init tps_65023_init(void)
  414. {
  415. return i2c_add_driver(&tps_65023_i2c_driver);
  416. }
  417. subsys_initcall(tps_65023_init);
  418. static void __exit tps_65023_cleanup(void)
  419. {
  420. i2c_del_driver(&tps_65023_i2c_driver);
  421. }
  422. module_exit(tps_65023_cleanup);
  423. MODULE_AUTHOR("Texas Instruments");
  424. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  425. MODULE_LICENSE("GPL v2");