tps65090.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core driver for TI TPS65090 PMIC family
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/mfd/tps65090.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/err.h>
  21. #define NUM_INT_REG 2
  22. #define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
  23. #define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
  24. #define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
  25. #define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
  26. #define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
  27. #define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
  28. #define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
  29. #define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
  30. #define TPS65090_INT2_MASK_OVERLOAD_FET1 1
  31. #define TPS65090_INT2_MASK_OVERLOAD_FET2 2
  32. #define TPS65090_INT2_MASK_OVERLOAD_FET3 3
  33. #define TPS65090_INT2_MASK_OVERLOAD_FET4 4
  34. #define TPS65090_INT2_MASK_OVERLOAD_FET5 5
  35. #define TPS65090_INT2_MASK_OVERLOAD_FET6 6
  36. #define TPS65090_INT2_MASK_OVERLOAD_FET7 7
  37. static struct resource charger_resources[] = {
  38. {
  39. .start = TPS65090_IRQ_VAC_STATUS_CHANGE,
  40. .end = TPS65090_IRQ_VAC_STATUS_CHANGE,
  41. .flags = IORESOURCE_IRQ,
  42. }
  43. };
  44. enum tps65090_cells {
  45. PMIC = 0,
  46. CHARGER = 1,
  47. };
  48. static struct mfd_cell tps65090s[] = {
  49. [PMIC] = {
  50. .name = "tps65090-pmic",
  51. },
  52. [CHARGER] = {
  53. .name = "tps65090-charger",
  54. .num_resources = ARRAY_SIZE(charger_resources),
  55. .resources = &charger_resources[0],
  56. .of_compatible = "ti,tps65090-charger",
  57. },
  58. };
  59. static const struct regmap_irq tps65090_irqs[] = {
  60. /* INT1 IRQs*/
  61. [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
  62. .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
  63. },
  64. [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
  65. .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
  66. },
  67. [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
  68. .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
  69. },
  70. [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
  71. .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
  72. },
  73. [TPS65090_IRQ_CHARGING_COMPLETE] = {
  74. .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
  75. },
  76. [TPS65090_IRQ_OVERLOAD_DCDC1] = {
  77. .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
  78. },
  79. [TPS65090_IRQ_OVERLOAD_DCDC2] = {
  80. .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
  81. },
  82. /* INT2 IRQs*/
  83. [TPS65090_IRQ_OVERLOAD_DCDC3] = {
  84. .reg_offset = 1,
  85. .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
  86. },
  87. [TPS65090_IRQ_OVERLOAD_FET1] = {
  88. .reg_offset = 1,
  89. .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
  90. },
  91. [TPS65090_IRQ_OVERLOAD_FET2] = {
  92. .reg_offset = 1,
  93. .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
  94. },
  95. [TPS65090_IRQ_OVERLOAD_FET3] = {
  96. .reg_offset = 1,
  97. .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
  98. },
  99. [TPS65090_IRQ_OVERLOAD_FET4] = {
  100. .reg_offset = 1,
  101. .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
  102. },
  103. [TPS65090_IRQ_OVERLOAD_FET5] = {
  104. .reg_offset = 1,
  105. .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
  106. },
  107. [TPS65090_IRQ_OVERLOAD_FET6] = {
  108. .reg_offset = 1,
  109. .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
  110. },
  111. [TPS65090_IRQ_OVERLOAD_FET7] = {
  112. .reg_offset = 1,
  113. .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
  114. },
  115. };
  116. static struct regmap_irq_chip tps65090_irq_chip = {
  117. .name = "tps65090",
  118. .irqs = tps65090_irqs,
  119. .num_irqs = ARRAY_SIZE(tps65090_irqs),
  120. .num_regs = NUM_INT_REG,
  121. .status_base = TPS65090_REG_INTR_STS,
  122. .mask_base = TPS65090_REG_INTR_MASK,
  123. .mask_invert = true,
  124. };
  125. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  126. {
  127. /* Nearly all registers have status bits mixed in, except a few */
  128. switch (reg) {
  129. case TPS65090_REG_INTR_MASK:
  130. case TPS65090_REG_INTR_MASK2:
  131. case TPS65090_REG_CG_CTRL0:
  132. case TPS65090_REG_CG_CTRL1:
  133. case TPS65090_REG_CG_CTRL2:
  134. case TPS65090_REG_CG_CTRL3:
  135. case TPS65090_REG_CG_CTRL4:
  136. case TPS65090_REG_CG_CTRL5:
  137. return false;
  138. }
  139. return true;
  140. }
  141. static const struct regmap_config tps65090_regmap_config = {
  142. .reg_bits = 8,
  143. .val_bits = 8,
  144. .max_register = TPS65090_MAX_REG,
  145. .num_reg_defaults_raw = TPS65090_NUM_REGS,
  146. .cache_type = REGCACHE_RBTREE,
  147. .volatile_reg = is_volatile_reg,
  148. };
  149. #ifdef CONFIG_OF
  150. static const struct of_device_id tps65090_of_match[] = {
  151. { .compatible = "ti,tps65090",},
  152. {},
  153. };
  154. #endif
  155. static int tps65090_i2c_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev);
  159. int irq_base = 0;
  160. struct tps65090 *tps65090;
  161. int ret;
  162. if (!pdata && !client->dev.of_node) {
  163. dev_err(&client->dev,
  164. "tps65090 requires platform data or of_node\n");
  165. return -EINVAL;
  166. }
  167. if (pdata)
  168. irq_base = pdata->irq_base;
  169. tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
  170. if (!tps65090)
  171. return -ENOMEM;
  172. tps65090->dev = &client->dev;
  173. i2c_set_clientdata(client, tps65090);
  174. tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
  175. if (IS_ERR(tps65090->rmap)) {
  176. ret = PTR_ERR(tps65090->rmap);
  177. dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
  178. return ret;
  179. }
  180. if (client->irq) {
  181. ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
  182. IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
  183. &tps65090_irq_chip, &tps65090->irq_data);
  184. if (ret) {
  185. dev_err(&client->dev,
  186. "IRQ init failed with err: %d\n", ret);
  187. return ret;
  188. }
  189. } else {
  190. /* Don't tell children they have an IRQ that'll never fire */
  191. tps65090s[CHARGER].num_resources = 0;
  192. }
  193. ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
  194. ARRAY_SIZE(tps65090s), NULL,
  195. 0, regmap_irq_get_domain(tps65090->irq_data));
  196. if (ret) {
  197. dev_err(&client->dev, "add mfd devices failed with err: %d\n",
  198. ret);
  199. goto err_irq_exit;
  200. }
  201. return 0;
  202. err_irq_exit:
  203. if (client->irq)
  204. regmap_del_irq_chip(client->irq, tps65090->irq_data);
  205. return ret;
  206. }
  207. static const struct i2c_device_id tps65090_id_table[] = {
  208. { "tps65090", 0 },
  209. { },
  210. };
  211. static struct i2c_driver tps65090_driver = {
  212. .driver = {
  213. .name = "tps65090",
  214. .suppress_bind_attrs = true,
  215. .of_match_table = of_match_ptr(tps65090_of_match),
  216. },
  217. .probe = tps65090_i2c_probe,
  218. .id_table = tps65090_id_table,
  219. };
  220. static int __init tps65090_init(void)
  221. {
  222. return i2c_add_driver(&tps65090_driver);
  223. }
  224. subsys_initcall(tps65090_init);