tps65023-regulator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tps65023-regulator.c
  4. *
  5. * Supports TPS65023 Regulator
  6. *
  7. * Copyright (C) 2009 Texas Instrument Incorporated - https://www.ti.com/
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/regmap.h>
  19. /* Register definitions */
  20. #define TPS65023_REG_VERSION 0
  21. #define TPS65023_REG_PGOODZ 1
  22. #define TPS65023_REG_MASK 2
  23. #define TPS65023_REG_REG_CTRL 3
  24. #define TPS65023_REG_CON_CTRL 4
  25. #define TPS65023_REG_CON_CTRL2 5
  26. #define TPS65023_REG_DEF_CORE 6
  27. #define TPS65023_REG_DEFSLEW 7
  28. #define TPS65023_REG_LDO_CTRL 8
  29. /* PGOODZ bitfields */
  30. #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
  31. #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
  32. #define TPS65023_PGOODZ_VDCDC1 BIT(5)
  33. #define TPS65023_PGOODZ_VDCDC2 BIT(4)
  34. #define TPS65023_PGOODZ_VDCDC3 BIT(3)
  35. #define TPS65023_PGOODZ_LDO2 BIT(2)
  36. #define TPS65023_PGOODZ_LDO1 BIT(1)
  37. /* MASK bitfields */
  38. #define TPS65023_MASK_PWRFAILZ BIT(7)
  39. #define TPS65023_MASK_LOWBATTZ BIT(6)
  40. #define TPS65023_MASK_VDCDC1 BIT(5)
  41. #define TPS65023_MASK_VDCDC2 BIT(4)
  42. #define TPS65023_MASK_VDCDC3 BIT(3)
  43. #define TPS65023_MASK_LDO2 BIT(2)
  44. #define TPS65023_MASK_LDO1 BIT(1)
  45. /* REG_CTRL bitfields */
  46. #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
  47. #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
  48. #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
  49. #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
  50. #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
  51. /* REG_CTRL2 bitfields */
  52. #define TPS65023_REG_CTRL2_GO BIT(7)
  53. #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
  54. #define TPS65023_REG_CTRL2_DCDC2 BIT(2)
  55. #define TPS65023_REG_CTRL2_DCDC1 BIT(1)
  56. #define TPS65023_REG_CTRL2_DCDC3 BIT(0)
  57. /* Number of step-down converters available */
  58. #define TPS65023_NUM_DCDC 3
  59. /* Number of LDO voltage regulators available */
  60. #define TPS65023_NUM_LDO 2
  61. /* Number of total regulators available */
  62. #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
  63. /* DCDCs */
  64. #define TPS65023_DCDC_1 0
  65. #define TPS65023_DCDC_2 1
  66. #define TPS65023_DCDC_3 2
  67. /* LDOs */
  68. #define TPS65023_LDO_1 3
  69. #define TPS65023_LDO_2 4
  70. #define TPS65023_MAX_REG_ID TPS65023_LDO_2
  71. #define TPS65023_REGULATOR_DCDC(_num, _t, _em) \
  72. { \
  73. .name = "VDCDC"#_num, \
  74. .of_match = of_match_ptr("VDCDC"#_num), \
  75. .regulators_node = of_match_ptr("regulators"), \
  76. .id = TPS65023_DCDC_##_num, \
  77. .n_voltages = ARRAY_SIZE(_t), \
  78. .ops = &tps65023_dcdc_ops, \
  79. .type = REGULATOR_VOLTAGE, \
  80. .owner = THIS_MODULE, \
  81. .volt_table = _t, \
  82. .vsel_reg = TPS65023_REG_DEF_CORE, \
  83. .vsel_mask = ARRAY_SIZE(_t) - 1, \
  84. .enable_mask = _em, \
  85. .enable_reg = TPS65023_REG_REG_CTRL, \
  86. .apply_reg = TPS65023_REG_CON_CTRL2, \
  87. .apply_bit = TPS65023_REG_CTRL2_GO, \
  88. } \
  89. #define TPS65023_REGULATOR_LDO(_num, _t, _vm) \
  90. { \
  91. .name = "LDO"#_num, \
  92. .of_match = of_match_ptr("LDO"#_num), \
  93. .regulators_node = of_match_ptr("regulators"), \
  94. .id = TPS65023_LDO_##_num, \
  95. .n_voltages = ARRAY_SIZE(_t), \
  96. .ops = &tps65023_ldo_ops, \
  97. .type = REGULATOR_VOLTAGE, \
  98. .owner = THIS_MODULE, \
  99. .volt_table = _t, \
  100. .vsel_reg = TPS65023_REG_LDO_CTRL, \
  101. .vsel_mask = _vm, \
  102. .enable_mask = 1 << (_num), \
  103. .enable_reg = TPS65023_REG_REG_CTRL, \
  104. } \
  105. /* Supported voltage values for regulators */
  106. static const unsigned int VCORE_VSEL_table[] = {
  107. 800000, 825000, 850000, 875000,
  108. 900000, 925000, 950000, 975000,
  109. 1000000, 1025000, 1050000, 1075000,
  110. 1100000, 1125000, 1150000, 1175000,
  111. 1200000, 1225000, 1250000, 1275000,
  112. 1300000, 1325000, 1350000, 1375000,
  113. 1400000, 1425000, 1450000, 1475000,
  114. 1500000, 1525000, 1550000, 1600000,
  115. };
  116. static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
  117. 3300000,
  118. };
  119. static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
  120. 1800000,
  121. };
  122. /* Supported voltage values for LDO regulators for tps65020 */
  123. static const unsigned int TPS65020_LDO_VSEL_table[] = {
  124. 1000000, 1050000, 1100000, 1300000,
  125. 1800000, 2500000, 3000000, 3300000,
  126. };
  127. /* Supported voltage values for LDO regulators
  128. * for tps65021 and tps65023 */
  129. static const unsigned int TPS65023_LDO1_VSEL_table[] = {
  130. 1000000, 1100000, 1300000, 1800000,
  131. 2200000, 2600000, 2800000, 3150000,
  132. };
  133. static const unsigned int TPS65023_LDO2_VSEL_table[] = {
  134. 1050000, 1200000, 1300000, 1800000,
  135. 2500000, 2800000, 3000000, 3300000,
  136. };
  137. /* PMIC details */
  138. struct tps_pmic {
  139. struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
  140. const struct tps_driver_data *driver_data;
  141. struct regmap *regmap;
  142. };
  143. /* Struct passed as driver data */
  144. struct tps_driver_data {
  145. const struct regulator_desc *desc;
  146. u8 core_regulator;
  147. };
  148. static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
  149. {
  150. struct tps_pmic *tps = rdev_get_drvdata(dev);
  151. int dcdc = rdev_get_id(dev);
  152. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  153. return -EINVAL;
  154. if (dcdc != tps->driver_data->core_regulator)
  155. return 0;
  156. return regulator_get_voltage_sel_regmap(dev);
  157. }
  158. static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
  159. unsigned selector)
  160. {
  161. struct tps_pmic *tps = rdev_get_drvdata(dev);
  162. int dcdc = rdev_get_id(dev);
  163. if (dcdc != tps->driver_data->core_regulator)
  164. return -EINVAL;
  165. return regulator_set_voltage_sel_regmap(dev, selector);
  166. }
  167. /* Operations permitted on VDCDCx */
  168. static const struct regulator_ops tps65023_dcdc_ops = {
  169. .is_enabled = regulator_is_enabled_regmap,
  170. .enable = regulator_enable_regmap,
  171. .disable = regulator_disable_regmap,
  172. .get_voltage_sel = tps65023_dcdc_get_voltage_sel,
  173. .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
  174. .list_voltage = regulator_list_voltage_table,
  175. .map_voltage = regulator_map_voltage_ascend,
  176. };
  177. /* Operations permitted on LDOx */
  178. static const struct regulator_ops tps65023_ldo_ops = {
  179. .is_enabled = regulator_is_enabled_regmap,
  180. .enable = regulator_enable_regmap,
  181. .disable = regulator_disable_regmap,
  182. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  183. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  184. .list_voltage = regulator_list_voltage_table,
  185. .map_voltage = regulator_map_voltage_ascend,
  186. };
  187. static const struct regmap_config tps65023_regmap_config = {
  188. .reg_bits = 8,
  189. .val_bits = 8,
  190. };
  191. static const struct regulator_desc tps65020_regulators[] = {
  192. TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
  193. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
  194. TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
  195. TPS65023_REGULATOR_LDO(1, TPS65020_LDO_VSEL_table, 0x07),
  196. TPS65023_REGULATOR_LDO(2, TPS65020_LDO_VSEL_table, 0x70),
  197. };
  198. static const struct regulator_desc tps65021_regulators[] = {
  199. TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
  200. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
  201. TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
  202. TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
  203. TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
  204. };
  205. static const struct regulator_desc tps65023_regulators[] = {
  206. TPS65023_REGULATOR_DCDC(1, VCORE_VSEL_table, 0x20),
  207. TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_3300000_VSEL_table, 0x10),
  208. TPS65023_REGULATOR_DCDC(3, DCDC_FIXED_1800000_VSEL_table, 0x08),
  209. TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
  210. TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
  211. };
  212. static struct tps_driver_data tps65020_drv_data = {
  213. .desc = tps65020_regulators,
  214. .core_regulator = TPS65023_DCDC_3,
  215. };
  216. static struct tps_driver_data tps65021_drv_data = {
  217. .desc = tps65021_regulators,
  218. .core_regulator = TPS65023_DCDC_3,
  219. };
  220. static struct tps_driver_data tps65023_drv_data = {
  221. .desc = tps65023_regulators,
  222. .core_regulator = TPS65023_DCDC_1,
  223. };
  224. static int tps_65023_probe(struct i2c_client *client,
  225. const struct i2c_device_id *id)
  226. {
  227. struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
  228. struct regulator_config config = { };
  229. struct tps_pmic *tps;
  230. int i;
  231. int error;
  232. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  233. if (!tps)
  234. return -ENOMEM;
  235. tps->driver_data = (struct tps_driver_data *)id->driver_data;
  236. tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
  237. if (IS_ERR(tps->regmap)) {
  238. error = PTR_ERR(tps->regmap);
  239. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  240. error);
  241. return error;
  242. }
  243. /* common for all regulators */
  244. config.dev = &client->dev;
  245. config.driver_data = tps;
  246. config.regmap = tps->regmap;
  247. for (i = 0; i < TPS65023_NUM_REGULATOR; i++) {
  248. if (init_data)
  249. config.init_data = &init_data[i];
  250. /* Register the regulators */
  251. tps->rdev[i] = devm_regulator_register(&client->dev,
  252. &tps->driver_data->desc[i], &config);
  253. if (IS_ERR(tps->rdev[i])) {
  254. dev_err(&client->dev, "failed to register %s\n",
  255. id->name);
  256. return PTR_ERR(tps->rdev[i]);
  257. }
  258. }
  259. i2c_set_clientdata(client, tps);
  260. /* Enable setting output voltage by I2C */
  261. regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
  262. TPS65023_REG_CTRL2_CORE_ADJ, 0);
  263. return 0;
  264. }
  265. static const struct of_device_id __maybe_unused tps65023_of_match[] = {
  266. { .compatible = "ti,tps65020", .data = &tps65020_drv_data},
  267. { .compatible = "ti,tps65021", .data = &tps65021_drv_data},
  268. { .compatible = "ti,tps65023", .data = &tps65023_drv_data},
  269. {},
  270. };
  271. MODULE_DEVICE_TABLE(of, tps65023_of_match);
  272. static const struct i2c_device_id tps_65023_id[] = {
  273. {
  274. .name = "tps65023",
  275. .driver_data = (kernel_ulong_t)&tps65023_drv_data
  276. }, {
  277. .name = "tps65021",
  278. .driver_data = (kernel_ulong_t)&tps65021_drv_data
  279. }, {
  280. .name = "tps65020",
  281. .driver_data = (kernel_ulong_t)&tps65020_drv_data
  282. },
  283. { },
  284. };
  285. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  286. static struct i2c_driver tps_65023_i2c_driver = {
  287. .driver = {
  288. .name = "tps65023",
  289. .of_match_table = of_match_ptr(tps65023_of_match),
  290. },
  291. .probe = tps_65023_probe,
  292. .id_table = tps_65023_id,
  293. };
  294. static int __init tps_65023_init(void)
  295. {
  296. return i2c_add_driver(&tps_65023_i2c_driver);
  297. }
  298. subsys_initcall(tps_65023_init);
  299. static void __exit tps_65023_cleanup(void)
  300. {
  301. i2c_del_driver(&tps_65023_i2c_driver);
  302. }
  303. module_exit(tps_65023_cleanup);
  304. MODULE_AUTHOR("Texas Instruments");
  305. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  306. MODULE_LICENSE("GPL v2");