tps65023-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /* Number of step-down converters available */
  66. #define TPS65023_NUM_DCDC 3
  67. /* Number of LDO voltage regulators available */
  68. #define TPS65023_NUM_LDO 2
  69. /* Number of total regulators available */
  70. #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
  71. /* DCDCs */
  72. #define TPS65023_DCDC_1 0
  73. #define TPS65023_DCDC_2 1
  74. #define TPS65023_DCDC_3 2
  75. /* LDOs */
  76. #define TPS65023_LDO_1 3
  77. #define TPS65023_LDO_2 4
  78. #define TPS65023_MAX_REG_ID TPS65023_LDO_2
  79. /* Supported voltage values for regulators */
  80. static const unsigned int VCORE_VSEL_table[] = {
  81. 800000, 825000, 850000, 875000,
  82. 900000, 925000, 950000, 975000,
  83. 1000000, 1025000, 1050000, 1075000,
  84. 1100000, 1125000, 1150000, 1175000,
  85. 1200000, 1225000, 1250000, 1275000,
  86. 1300000, 1325000, 1350000, 1375000,
  87. 1400000, 1425000, 1450000, 1475000,
  88. 1500000, 1525000, 1550000, 1600000,
  89. };
  90. static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
  91. 3300000,
  92. };
  93. static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
  94. 1800000,
  95. };
  96. /* Supported voltage values for LDO regulators for tps65020 */
  97. static const unsigned int TPS65020_LDO1_VSEL_table[] = {
  98. 1000000, 1050000, 1100000, 1300000,
  99. 1800000, 2500000, 3000000, 3300000,
  100. };
  101. static const unsigned int TPS65020_LDO2_VSEL_table[] = {
  102. 1000000, 1050000, 1100000, 1300000,
  103. 1800000, 2500000, 3000000, 3300000,
  104. };
  105. /* Supported voltage values for LDO regulators
  106. * for tps65021 and tps65023 */
  107. static const unsigned int TPS65023_LDO1_VSEL_table[] = {
  108. 1000000, 1100000, 1300000, 1800000,
  109. 2200000, 2600000, 2800000, 3150000,
  110. };
  111. static const unsigned int TPS65023_LDO2_VSEL_table[] = {
  112. 1050000, 1200000, 1300000, 1800000,
  113. 2500000, 2800000, 3000000, 3300000,
  114. };
  115. /* Regulator specific details */
  116. struct tps_info {
  117. const char *name;
  118. u8 table_len;
  119. const unsigned int *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_sel(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 data;
  147. } else
  148. return 0;
  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. /* Operations permitted on VDCDCx */
  170. static struct regulator_ops tps65023_dcdc_ops = {
  171. .is_enabled = regulator_is_enabled_regmap,
  172. .enable = regulator_enable_regmap,
  173. .disable = regulator_disable_regmap,
  174. .get_voltage_sel = tps65023_dcdc_get_voltage_sel,
  175. .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
  176. .list_voltage = regulator_list_voltage_table,
  177. };
  178. /* Operations permitted on LDOx */
  179. static struct regulator_ops tps65023_ldo_ops = {
  180. .is_enabled = regulator_is_enabled_regmap,
  181. .enable = regulator_enable_regmap,
  182. .disable = regulator_disable_regmap,
  183. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  184. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  185. .list_voltage = regulator_list_voltage_table,
  186. };
  187. static struct regmap_config tps65023_regmap_config = {
  188. .reg_bits = 8,
  189. .val_bits = 8,
  190. };
  191. static int tps_65023_probe(struct i2c_client *client,
  192. const struct i2c_device_id *id)
  193. {
  194. const struct tps_driver_data *drv_data = (void *)id->driver_data;
  195. const struct tps_info *info = drv_data->info;
  196. struct regulator_config config = { };
  197. struct regulator_init_data *init_data;
  198. struct regulator_dev *rdev;
  199. struct tps_pmic *tps;
  200. int i;
  201. int error;
  202. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  203. return -EIO;
  204. /**
  205. * init_data points to array of regulator_init structures
  206. * coming from the board-evm file.
  207. */
  208. init_data = client->dev.platform_data;
  209. if (!init_data)
  210. return -EIO;
  211. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  212. if (!tps)
  213. return -ENOMEM;
  214. tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
  215. if (IS_ERR(tps->regmap)) {
  216. error = PTR_ERR(tps->regmap);
  217. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  218. error);
  219. return error;
  220. }
  221. /* common for all regulators */
  222. tps->core_regulator = drv_data->core_regulator;
  223. for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
  224. /* Store regulator specific information */
  225. tps->info[i] = info;
  226. tps->desc[i].name = info->name;
  227. tps->desc[i].id = i;
  228. tps->desc[i].n_voltages = info->table_len;
  229. tps->desc[i].volt_table = info->table;
  230. tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
  231. &tps65023_ldo_ops : &tps65023_dcdc_ops);
  232. tps->desc[i].type = REGULATOR_VOLTAGE;
  233. tps->desc[i].owner = THIS_MODULE;
  234. tps->desc[i].enable_reg = TPS65023_REG_REG_CTRL;
  235. switch (i) {
  236. case TPS65023_LDO_1:
  237. tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL;
  238. tps->desc[i].vsel_mask = 0x07;
  239. tps->desc[i].enable_mask = 1 << 1;
  240. break;
  241. case TPS65023_LDO_2:
  242. tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL;
  243. tps->desc[i].vsel_mask = 0x70;
  244. tps->desc[i].enable_mask = 1 << 2;
  245. break;
  246. default: /* DCDCx */
  247. tps->desc[i].enable_mask =
  248. 1 << (TPS65023_NUM_REGULATOR - i);
  249. }
  250. config.dev = &client->dev;
  251. config.init_data = init_data;
  252. config.driver_data = tps;
  253. config.regmap = tps->regmap;
  254. /* Register the regulators */
  255. rdev = regulator_register(&tps->desc[i], &config);
  256. if (IS_ERR(rdev)) {
  257. dev_err(&client->dev, "failed to register %s\n",
  258. id->name);
  259. error = PTR_ERR(rdev);
  260. goto fail;
  261. }
  262. /* Save regulator for cleanup */
  263. tps->rdev[i] = rdev;
  264. }
  265. i2c_set_clientdata(client, tps);
  266. /* Enable setting output voltage by I2C */
  267. regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  268. TPS65023_REG_CTRL2_CORE_ADJ, TPS65023_REG_CTRL2_CORE_ADJ);
  269. return 0;
  270. fail:
  271. while (--i >= 0)
  272. regulator_unregister(tps->rdev[i]);
  273. return error;
  274. }
  275. static int tps_65023_remove(struct i2c_client *client)
  276. {
  277. struct tps_pmic *tps = i2c_get_clientdata(client);
  278. int i;
  279. for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
  280. regulator_unregister(tps->rdev[i]);
  281. return 0;
  282. }
  283. static const struct tps_info tps65020_regs[] = {
  284. {
  285. .name = "VDCDC1",
  286. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  287. .table = DCDC_FIXED_3300000_VSEL_table,
  288. },
  289. {
  290. .name = "VDCDC2",
  291. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  292. .table = DCDC_FIXED_1800000_VSEL_table,
  293. },
  294. {
  295. .name = "VDCDC3",
  296. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  297. .table = VCORE_VSEL_table,
  298. },
  299. {
  300. .name = "LDO1",
  301. .table_len = ARRAY_SIZE(TPS65020_LDO1_VSEL_table),
  302. .table = TPS65020_LDO1_VSEL_table,
  303. },
  304. {
  305. .name = "LDO2",
  306. .table_len = ARRAY_SIZE(TPS65020_LDO2_VSEL_table),
  307. .table = TPS65020_LDO2_VSEL_table,
  308. },
  309. };
  310. static const struct tps_info tps65021_regs[] = {
  311. {
  312. .name = "VDCDC1",
  313. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  314. .table = DCDC_FIXED_3300000_VSEL_table,
  315. },
  316. {
  317. .name = "VDCDC2",
  318. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  319. .table = DCDC_FIXED_1800000_VSEL_table,
  320. },
  321. {
  322. .name = "VDCDC3",
  323. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  324. .table = VCORE_VSEL_table,
  325. },
  326. {
  327. .name = "LDO1",
  328. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  329. .table = TPS65023_LDO1_VSEL_table,
  330. },
  331. {
  332. .name = "LDO2",
  333. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  334. .table = TPS65023_LDO2_VSEL_table,
  335. },
  336. };
  337. static const struct tps_info tps65023_regs[] = {
  338. {
  339. .name = "VDCDC1",
  340. .table_len = ARRAY_SIZE(VCORE_VSEL_table),
  341. .table = VCORE_VSEL_table,
  342. },
  343. {
  344. .name = "VDCDC2",
  345. .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table),
  346. .table = DCDC_FIXED_3300000_VSEL_table,
  347. },
  348. {
  349. .name = "VDCDC3",
  350. .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table),
  351. .table = DCDC_FIXED_1800000_VSEL_table,
  352. },
  353. {
  354. .name = "LDO1",
  355. .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table),
  356. .table = TPS65023_LDO1_VSEL_table,
  357. },
  358. {
  359. .name = "LDO2",
  360. .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table),
  361. .table = TPS65023_LDO2_VSEL_table,
  362. },
  363. };
  364. static struct tps_driver_data tps65020_drv_data = {
  365. .info = tps65020_regs,
  366. .core_regulator = TPS65023_DCDC_3,
  367. };
  368. static struct tps_driver_data tps65021_drv_data = {
  369. .info = tps65021_regs,
  370. .core_regulator = TPS65023_DCDC_3,
  371. };
  372. static struct tps_driver_data tps65023_drv_data = {
  373. .info = tps65023_regs,
  374. .core_regulator = TPS65023_DCDC_1,
  375. };
  376. static const struct i2c_device_id tps_65023_id[] = {
  377. {.name = "tps65023",
  378. .driver_data = (unsigned long) &tps65023_drv_data},
  379. {.name = "tps65021",
  380. .driver_data = (unsigned long) &tps65021_drv_data,},
  381. {.name = "tps65020",
  382. .driver_data = (unsigned long) &tps65020_drv_data},
  383. { },
  384. };
  385. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  386. static struct i2c_driver tps_65023_i2c_driver = {
  387. .driver = {
  388. .name = "tps65023",
  389. .owner = THIS_MODULE,
  390. },
  391. .probe = tps_65023_probe,
  392. .remove = tps_65023_remove,
  393. .id_table = tps_65023_id,
  394. };
  395. static int __init tps_65023_init(void)
  396. {
  397. return i2c_add_driver(&tps_65023_i2c_driver);
  398. }
  399. subsys_initcall(tps_65023_init);
  400. static void __exit tps_65023_cleanup(void)
  401. {
  402. i2c_del_driver(&tps_65023_i2c_driver);
  403. }
  404. module_exit(tps_65023_cleanup);
  405. MODULE_AUTHOR("Texas Instruments");
  406. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  407. MODULE_LICENSE("GPL v2");