tps65090.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/err.h>
  25. #define NUM_INT_REG 2
  26. #define TOTAL_NUM_REG 0x18
  27. /* interrupt status registers */
  28. #define TPS65090_INT_STS 0x0
  29. #define TPS65090_INT_STS2 0x1
  30. /* interrupt mask registers */
  31. #define TPS65090_INT_MSK 0x2
  32. #define TPS65090_INT_MSK2 0x3
  33. #define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
  34. #define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
  35. #define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
  36. #define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
  37. #define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
  38. #define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
  39. #define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
  40. #define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
  41. #define TPS65090_INT2_MASK_OVERLOAD_FET1 1
  42. #define TPS65090_INT2_MASK_OVERLOAD_FET2 2
  43. #define TPS65090_INT2_MASK_OVERLOAD_FET3 3
  44. #define TPS65090_INT2_MASK_OVERLOAD_FET4 4
  45. #define TPS65090_INT2_MASK_OVERLOAD_FET5 5
  46. #define TPS65090_INT2_MASK_OVERLOAD_FET6 6
  47. #define TPS65090_INT2_MASK_OVERLOAD_FET7 7
  48. static struct mfd_cell tps65090s[] = {
  49. {
  50. .name = "tps65090-pmic",
  51. },
  52. {
  53. .name = "tps65090-charger",
  54. },
  55. };
  56. static const struct regmap_irq tps65090_irqs[] = {
  57. /* INT1 IRQs*/
  58. [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
  59. .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
  60. },
  61. [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
  62. .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
  63. },
  64. [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
  65. .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
  66. },
  67. [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
  68. .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
  69. },
  70. [TPS65090_IRQ_CHARGING_COMPLETE] = {
  71. .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
  72. },
  73. [TPS65090_IRQ_OVERLOAD_DCDC1] = {
  74. .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
  75. },
  76. [TPS65090_IRQ_OVERLOAD_DCDC2] = {
  77. .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
  78. },
  79. /* INT2 IRQs*/
  80. [TPS65090_IRQ_OVERLOAD_DCDC3] = {
  81. .reg_offset = 1,
  82. .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
  83. },
  84. [TPS65090_IRQ_OVERLOAD_FET1] = {
  85. .reg_offset = 1,
  86. .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
  87. },
  88. [TPS65090_IRQ_OVERLOAD_FET2] = {
  89. .reg_offset = 1,
  90. .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
  91. },
  92. [TPS65090_IRQ_OVERLOAD_FET3] = {
  93. .reg_offset = 1,
  94. .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
  95. },
  96. [TPS65090_IRQ_OVERLOAD_FET4] = {
  97. .reg_offset = 1,
  98. .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
  99. },
  100. [TPS65090_IRQ_OVERLOAD_FET5] = {
  101. .reg_offset = 1,
  102. .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
  103. },
  104. [TPS65090_IRQ_OVERLOAD_FET6] = {
  105. .reg_offset = 1,
  106. .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
  107. },
  108. [TPS65090_IRQ_OVERLOAD_FET7] = {
  109. .reg_offset = 1,
  110. .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
  111. },
  112. };
  113. static struct regmap_irq_chip tps65090_irq_chip = {
  114. .name = "tps65090",
  115. .irqs = tps65090_irqs,
  116. .num_irqs = ARRAY_SIZE(tps65090_irqs),
  117. .num_regs = NUM_INT_REG,
  118. .status_base = TPS65090_INT_STS,
  119. .mask_base = TPS65090_INT_MSK,
  120. .mask_invert = true,
  121. };
  122. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  123. {
  124. if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2))
  125. return true;
  126. else
  127. return false;
  128. }
  129. static const struct regmap_config tps65090_regmap_config = {
  130. .reg_bits = 8,
  131. .val_bits = 8,
  132. .max_register = TOTAL_NUM_REG,
  133. .num_reg_defaults_raw = TOTAL_NUM_REG,
  134. .cache_type = REGCACHE_RBTREE,
  135. .volatile_reg = is_volatile_reg,
  136. };
  137. static int tps65090_i2c_probe(struct i2c_client *client,
  138. const struct i2c_device_id *id)
  139. {
  140. struct tps65090_platform_data *pdata = client->dev.platform_data;
  141. struct tps65090 *tps65090;
  142. int ret;
  143. if (!pdata) {
  144. dev_err(&client->dev, "tps65090 requires platform data\n");
  145. return -EINVAL;
  146. }
  147. tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
  148. if (!tps65090) {
  149. dev_err(&client->dev, "mem alloc for tps65090 failed\n");
  150. return -ENOMEM;
  151. }
  152. tps65090->dev = &client->dev;
  153. i2c_set_clientdata(client, tps65090);
  154. tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
  155. if (IS_ERR(tps65090->rmap)) {
  156. ret = PTR_ERR(tps65090->rmap);
  157. dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
  158. return ret;
  159. }
  160. if (client->irq) {
  161. ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
  162. IRQF_ONESHOT | IRQF_TRIGGER_LOW, pdata->irq_base,
  163. &tps65090_irq_chip, &tps65090->irq_data);
  164. if (ret) {
  165. dev_err(&client->dev,
  166. "IRQ init failed with err: %d\n", ret);
  167. return ret;
  168. }
  169. }
  170. ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
  171. ARRAY_SIZE(tps65090s), NULL,
  172. regmap_irq_chip_get_base(tps65090->irq_data), NULL);
  173. if (ret) {
  174. dev_err(&client->dev, "add mfd devices failed with err: %d\n",
  175. ret);
  176. goto err_irq_exit;
  177. }
  178. return 0;
  179. err_irq_exit:
  180. if (client->irq)
  181. regmap_del_irq_chip(client->irq, tps65090->irq_data);
  182. return ret;
  183. }
  184. static int tps65090_i2c_remove(struct i2c_client *client)
  185. {
  186. struct tps65090 *tps65090 = i2c_get_clientdata(client);
  187. mfd_remove_devices(tps65090->dev);
  188. if (client->irq)
  189. regmap_del_irq_chip(client->irq, tps65090->irq_data);
  190. return 0;
  191. }
  192. #ifdef CONFIG_PM_SLEEP
  193. static int tps65090_suspend(struct device *dev)
  194. {
  195. struct i2c_client *client = to_i2c_client(dev);
  196. if (client->irq)
  197. disable_irq(client->irq);
  198. return 0;
  199. }
  200. static int tps65090_resume(struct device *dev)
  201. {
  202. struct i2c_client *client = to_i2c_client(dev);
  203. if (client->irq)
  204. enable_irq(client->irq);
  205. return 0;
  206. }
  207. #endif
  208. static const struct dev_pm_ops tps65090_pm_ops = {
  209. SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
  210. };
  211. static const struct i2c_device_id tps65090_id_table[] = {
  212. { "tps65090", 0 },
  213. { },
  214. };
  215. MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
  216. static struct i2c_driver tps65090_driver = {
  217. .driver = {
  218. .name = "tps65090",
  219. .owner = THIS_MODULE,
  220. .pm = &tps65090_pm_ops,
  221. },
  222. .probe = tps65090_i2c_probe,
  223. .remove = tps65090_i2c_remove,
  224. .id_table = tps65090_id_table,
  225. };
  226. static int __init tps65090_init(void)
  227. {
  228. return i2c_add_driver(&tps65090_driver);
  229. }
  230. subsys_initcall(tps65090_init);
  231. static void __exit tps65090_exit(void)
  232. {
  233. i2c_del_driver(&tps65090_driver);
  234. }
  235. module_exit(tps65090_exit);
  236. MODULE_DESCRIPTION("TPS65090 core driver");
  237. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  238. MODULE_LICENSE("GPL v2");