tps65090.c 7.3 KB

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