tps65023-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * tps65023-regulator.c
  3. *
  4. * Supports TPS65023 Regulator
  5. *
  6. * Copyright (C) 2009 Texas Instrument Incorporated - https://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. #define TPS65023_REGULATOR_DCDC(_num, _t, _em) \
  80. { \
  81. .name = "VDCDC"#_num, \
  82. .of_match = of_match_ptr("VDCDC"#_num), \
  83. .regulators_node = of_match_ptr("regulators"), \
  84. .id = TPS65023_DCDC_##_num, \
  85. .n_voltages = ARRAY_SIZE(_t), \
  86. .ops = &tps65023_dcdc_ops, \
  87. .type = REGULATOR_VOLTAGE, \
  88. .owner = THIS_MODULE, \
  89. .volt_table = _t, \
  90. .vsel_reg = TPS65023_REG_DEF_CORE, \
  91. .vsel_mask = ARRAY_SIZE(_t) - 1, \
  92. .enable_mask = _em, \
  93. .enable_reg = TPS65023_REG_REG_CTRL, \
  94. .apply_reg = TPS65023_REG_CON_CTRL2, \
  95. .apply_bit = TPS65023_REG_CTRL2_GO, \
  96. } \
  97. #define TPS65023_REGULATOR_LDO(_num, _t, _vm) \
  98. { \
  99. .name = "LDO"#_num, \
  100. .of_match = of_match_ptr("LDO"#_num), \
  101. .regulators_node = of_match_ptr("regulators"), \
  102. .id = TPS65023_LDO_##_num, \
  103. .n_voltages = ARRAY_SIZE(_t), \
  104. .ops = &tps65023_ldo_ops, \
  105. .type = REGULATOR_VOLTAGE, \
  106. .owner = THIS_MODULE, \
  107. .volt_table = _t, \
  108. .vsel_reg = TPS65023_REG_LDO_CTRL, \
  109. .vsel_mask = _vm, \
  110. .enable_mask = 1 << (_num), \
  111. .enable_reg = TPS65023_REG_REG_CTRL, \
  112. } \
  113. /* Supported voltage values for regulators */
  114. static const unsigned int VCORE_VSEL_table[] = {
  115. 800000, 825000, 850000, 875000,
  116. 900000, 925000, 950000, 975000,
  117. 1000000, 1025000, 1050000, 1075000,
  118. 1100000, 1125000, 1150000, 1175000,
  119. 1200000, 1225000, 1250000, 1275000,
  120. 1300000, 1325000, 1350000, 1375000,
  121. 1400000, 1425000, 1450000, 1475000,
  122. 1500000, 1525000, 1550000, 1600000,
  123. };
  124. static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
  125. 3300000,
  126. };
  127. static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
  128. 1800000,
  129. };
  130. /* Supported voltage values for LDO regulators for tps65020 */
  131. static const unsigned int TPS65020_LDO_VSEL_table[] = {
  132. 1000000, 1050000, 1100000, 1300000,
  133. 1800000, 2500000, 3000000, 3300000,
  134. };
  135. /* Supported voltage values for LDO regulators
  136. * for tps65021 and tps65023 */
  137. static const unsigned int TPS65023_LDO1_VSEL_table[] = {
  138. 1000000, 1100000, 1300000, 1800000,
  139. 2200000, 2600000, 2800000, 3150000,
  140. };
  141. static const unsigned int TPS65023_LDO2_VSEL_table[] = {
  142. 1050000, 1200000, 1300000, 1800000,
  143. 2500000, 2800000, 3000000, 3300000,
  144. };
  145. /* PMIC details */
  146. struct tps_pmic {
  147. struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
  148. const struct tps_driver_data *driver_data;
  149. struct regmap *regmap;
  150. };
  151. /* Struct passed as driver data */
  152. struct tps_driver_data {
  153. const struct regulator_desc *desc;
  154. u8 core_regulator;
  155. };
  156. static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
  157. {
  158. struct tps_pmic *tps = rdev_get_drvdata(dev);
  159. int dcdc = rdev_get_id(dev);
  160. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  161. return -EINVAL;
  162. if (dcdc != tps->driver_data->core_regulator)
  163. return 0;
  164. return regulator_get_voltage_sel_regmap(dev);
  165. }
  166. static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
  167. unsigned selector)
  168. {
  169. struct tps_pmic *tps = rdev_get_drvdata(dev);
  170. int dcdc = rdev_get_id(dev);
  171. if (dcdc != tps->driver_data->core_regulator)
  172. return -EINVAL;
  173. return regulator_set_voltage_sel_regmap(dev, selector);
  174. }
  175. /* Operations permitted on VDCDCx */
  176. static const struct regulator_ops tps65023_dcdc_ops = {
  177. .is_enabled = regulator_is_enabled_regmap,
  178. .enable = regulator_enable_regmap,
  179. .disable = regulator_disable_regmap,
  180. .get_voltage_sel = tps65023_dcdc_get_voltage_sel,
  181. .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
  182. .list_voltage = regulator_list_voltage_table,
  183. .map_voltage = regulator_map_voltage_ascend,
  184. };
  185. /* Operations permitted on LDOx */
  186. static const struct regulator_ops tps65023_ldo_ops = {
  187. .is_enabled = regulator_is_enabled_regmap,
  188. .enable = regulator_enable_regmap,
  189. .disable = regulator_disable_regmap,
  190. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  191. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  192. .list_voltage = regulator_list_voltage_table,
  193. .map_voltage = regulator_map_voltage_ascend,
  194. };
  195. static const struct regmap_config tps65023_regmap_config = {
  196. .reg_bits = 8,
  197. .val_bits = 8,
  198. };
  199. static const struct regulator_desc tps65020_regulators[] = {
  200. TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
  201. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
  202. TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
  203. TPS65023_REGULATOR_LDO(1, TPS65020_LDO_VSEL_table, 0x07),
  204. TPS65023_REGULATOR_LDO(2, TPS65020_LDO_VSEL_table, 0x70),
  205. };
  206. static const struct regulator_desc tps65021_regulators[] = {
  207. TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
  208. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
  209. TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
  210. TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
  211. TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
  212. };
  213. static const struct regulator_desc tps65023_regulators[] = {
  214. TPS65023_REGULATOR_DCDC(1, VCORE_VSEL_table, 0x20),
  215. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_3300000_VSEL_table, 0x10),
  216. TPS65023_REGULATOR_DCDC(3, DCDC_FIXED_1800000_VSEL_table, 0x08),
  217. TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
  218. TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
  219. };
  220. static struct tps_driver_data tps65020_drv_data = {
  221. .desc = tps65020_regulators,
  222. .core_regulator = TPS65023_DCDC_3,
  223. };
  224. static struct tps_driver_data tps65021_drv_data = {
  225. .desc = tps65021_regulators,
  226. .core_regulator = TPS65023_DCDC_3,
  227. };
  228. static struct tps_driver_data tps65023_drv_data = {
  229. .desc = tps65023_regulators,
  230. .core_regulator = TPS65023_DCDC_1,
  231. };
  232. static int tps_65023_probe(struct i2c_client *client,
  233. const struct i2c_device_id *id)
  234. {
  235. struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
  236. struct regulator_config config = { };
  237. struct tps_pmic *tps;
  238. int i;
  239. int error;
  240. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  241. if (!tps)
  242. return -ENOMEM;
  243. tps->driver_data = (struct tps_driver_data *)id->driver_data;
  244. tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
  245. if (IS_ERR(tps->regmap)) {
  246. error = PTR_ERR(tps->regmap);
  247. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  248. error);
  249. return error;
  250. }
  251. /* common for all regulators */
  252. config.dev = &client->dev;
  253. config.driver_data = tps;
  254. config.regmap = tps->regmap;
  255. for (i = 0; i < TPS65023_NUM_REGULATOR; i++) {
  256. if (init_data)
  257. config.init_data = &init_data[i];
  258. /* Register the regulators */
  259. tps->rdev[i] = devm_regulator_register(&client->dev,
  260. &tps->driver_data->desc[i], &config);
  261. if (IS_ERR(tps->rdev[i])) {
  262. dev_err(&client->dev, "failed to register %s\n",
  263. id->name);
  264. return PTR_ERR(tps->rdev[i]);
  265. }
  266. }
  267. i2c_set_clientdata(client, tps);
  268. /* Enable setting output voltage by I2C */
  269. regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  270. TPS65023_REG_CTRL2_CORE_ADJ, 0);
  271. return 0;
  272. }
  273. static const struct of_device_id __maybe_unused tps65023_of_match[] = {
  274. { .compatible = "ti,tps65020", .data = &tps65020_drv_data},
  275. { .compatible = "ti,tps65021", .data = &tps65021_drv_data},
  276. { .compatible = "ti,tps65023", .data = &tps65023_drv_data},
  277. {},
  278. };
  279. MODULE_DEVICE_TABLE(of, tps65023_of_match);
  280. static const struct i2c_device_id tps_65023_id[] = {
  281. {
  282. .name = "tps65023",
  283. .driver_data = (kernel_ulong_t)&tps65023_drv_data
  284. }, {
  285. .name = "tps65021",
  286. .driver_data = (kernel_ulong_t)&tps65021_drv_data
  287. }, {
  288. .name = "tps65020",
  289. .driver_data = (kernel_ulong_t)&tps65020_drv_data
  290. },
  291. { },
  292. };
  293. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  294. static struct i2c_driver tps_65023_i2c_driver = {
  295. .driver = {
  296. .name = "tps65023",
  297. .of_match_table = of_match_ptr(tps65023_of_match),
  298. },
  299. .probe = tps_65023_probe,
  300. .id_table = tps_65023_id,
  301. };
  302. static int __init tps_65023_init(void)
  303. {
  304. return i2c_add_driver(&tps_65023_i2c_driver);
  305. }
  306. subsys_initcall(tps_65023_init);
  307. static void __exit tps_65023_cleanup(void)
  308. {
  309. i2c_del_driver(&tps_65023_i2c_driver);
  310. }
  311. module_exit(tps_65023_cleanup);
  312. MODULE_AUTHOR("Texas Instruments");
  313. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  314. MODULE_LICENSE("GPL v2");