tps65090.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/regmap.h>
  25. #include <linux/err.h>
  26. #define NUM_INT_REG 2
  27. #define TOTAL_NUM_REG 0x18
  28. /* interrupt status registers */
  29. #define TPS65090_INT_STS 0x0
  30. #define TPS65090_INT_STS2 0x1
  31. /* interrupt mask registers */
  32. #define TPS65090_INT_MSK 0x2
  33. #define TPS65090_INT_MSK2 0x3
  34. struct tps65090_irq_data {
  35. u8 mask_reg;
  36. u8 mask_pos;
  37. };
  38. #define TPS65090_IRQ(_reg, _mask_pos) \
  39. { \
  40. .mask_reg = (_reg), \
  41. .mask_pos = (_mask_pos), \
  42. }
  43. static const struct tps65090_irq_data tps65090_irqs[] = {
  44. [0] = TPS65090_IRQ(0, 0),
  45. [1] = TPS65090_IRQ(0, 1),
  46. [2] = TPS65090_IRQ(0, 2),
  47. [3] = TPS65090_IRQ(0, 3),
  48. [4] = TPS65090_IRQ(0, 4),
  49. [5] = TPS65090_IRQ(0, 5),
  50. [6] = TPS65090_IRQ(0, 6),
  51. [7] = TPS65090_IRQ(0, 7),
  52. [8] = TPS65090_IRQ(1, 0),
  53. [9] = TPS65090_IRQ(1, 1),
  54. [10] = TPS65090_IRQ(1, 2),
  55. [11] = TPS65090_IRQ(1, 3),
  56. [12] = TPS65090_IRQ(1, 4),
  57. [13] = TPS65090_IRQ(1, 5),
  58. [14] = TPS65090_IRQ(1, 6),
  59. [15] = TPS65090_IRQ(1, 7),
  60. };
  61. static struct mfd_cell tps65090s[] = {
  62. {
  63. .name = "tps65090-pmic",
  64. },
  65. {
  66. .name = "tps65090-regulator",
  67. },
  68. };
  69. int tps65090_write(struct device *dev, int reg, uint8_t val)
  70. {
  71. struct tps65090 *tps = dev_get_drvdata(dev);
  72. return regmap_write(tps->rmap, reg, val);
  73. }
  74. EXPORT_SYMBOL_GPL(tps65090_write);
  75. int tps65090_read(struct device *dev, int reg, uint8_t *val)
  76. {
  77. struct tps65090 *tps = dev_get_drvdata(dev);
  78. unsigned int temp_val;
  79. int ret;
  80. ret = regmap_read(tps->rmap, reg, &temp_val);
  81. if (!ret)
  82. *val = temp_val;
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(tps65090_read);
  86. int tps65090_set_bits(struct device *dev, int reg, uint8_t bit_num)
  87. {
  88. struct tps65090 *tps = dev_get_drvdata(dev);
  89. return regmap_update_bits(tps->rmap, reg, BIT(bit_num), ~0u);
  90. }
  91. EXPORT_SYMBOL_GPL(tps65090_set_bits);
  92. int tps65090_clr_bits(struct device *dev, int reg, uint8_t bit_num)
  93. {
  94. struct tps65090 *tps = dev_get_drvdata(dev);
  95. return regmap_update_bits(tps->rmap, reg, BIT(bit_num), 0u);
  96. }
  97. EXPORT_SYMBOL_GPL(tps65090_clr_bits);
  98. static void tps65090_irq_lock(struct irq_data *data)
  99. {
  100. struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
  101. mutex_lock(&tps65090->irq_lock);
  102. }
  103. static void tps65090_irq_mask(struct irq_data *irq_data)
  104. {
  105. struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
  106. unsigned int __irq = irq_data->hwirq;
  107. const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
  108. tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
  109. data->mask_pos);
  110. }
  111. static void tps65090_irq_unmask(struct irq_data *irq_data)
  112. {
  113. struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
  114. unsigned int __irq = irq_data->irq - tps65090->irq_base;
  115. const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
  116. tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
  117. data->mask_pos);
  118. }
  119. static void tps65090_irq_sync_unlock(struct irq_data *data)
  120. {
  121. struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
  122. mutex_unlock(&tps65090->irq_lock);
  123. }
  124. static irqreturn_t tps65090_irq(int irq, void *data)
  125. {
  126. struct tps65090 *tps65090 = data;
  127. int ret = 0;
  128. u8 status, mask;
  129. unsigned long int acks = 0;
  130. int i;
  131. for (i = 0; i < NUM_INT_REG; i++) {
  132. ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
  133. if (ret < 0) {
  134. dev_err(tps65090->dev,
  135. "failed to read mask reg [addr:%d]\n",
  136. TPS65090_INT_MSK + i);
  137. return IRQ_NONE;
  138. }
  139. ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
  140. &status);
  141. if (ret < 0) {
  142. dev_err(tps65090->dev,
  143. "failed to read status reg [addr:%d]\n",
  144. TPS65090_INT_STS + i);
  145. return IRQ_NONE;
  146. }
  147. if (status) {
  148. /* Ack only those interrupts which are not masked */
  149. status &= (~mask);
  150. ret = tps65090_write(tps65090->dev,
  151. TPS65090_INT_STS + i, status);
  152. if (ret < 0) {
  153. dev_err(tps65090->dev,
  154. "failed to write interrupt status\n");
  155. return IRQ_NONE;
  156. }
  157. acks |= (status << (i * 8));
  158. }
  159. }
  160. for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
  161. handle_nested_irq(tps65090->irq_base + i);
  162. return acks ? IRQ_HANDLED : IRQ_NONE;
  163. }
  164. static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq,
  165. int irq_base)
  166. {
  167. int i, ret;
  168. if (!irq_base) {
  169. dev_err(tps65090->dev, "IRQ base not set\n");
  170. return -EINVAL;
  171. }
  172. mutex_init(&tps65090->irq_lock);
  173. for (i = 0; i < NUM_INT_REG; i++)
  174. tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
  175. for (i = 0; i < NUM_INT_REG; i++)
  176. tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
  177. tps65090->irq_base = irq_base;
  178. tps65090->irq_chip.name = "tps65090";
  179. tps65090->irq_chip.irq_mask = tps65090_irq_mask;
  180. tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
  181. tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
  182. tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
  183. for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
  184. int __irq = i + tps65090->irq_base;
  185. irq_set_chip_data(__irq, tps65090);
  186. irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
  187. handle_simple_irq);
  188. irq_set_nested_thread(__irq, 1);
  189. #ifdef CONFIG_ARM
  190. set_irq_flags(__irq, IRQF_VALID);
  191. #endif
  192. }
  193. ret = request_threaded_irq(irq, NULL, tps65090_irq, IRQF_ONESHOT,
  194. "tps65090", tps65090);
  195. if (!ret) {
  196. device_init_wakeup(tps65090->dev, 1);
  197. enable_irq_wake(irq);
  198. }
  199. return ret;
  200. }
  201. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  202. {
  203. if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS))
  204. return true;
  205. else
  206. return false;
  207. }
  208. static const struct regmap_config tps65090_regmap_config = {
  209. .reg_bits = 8,
  210. .val_bits = 8,
  211. .max_register = TOTAL_NUM_REG,
  212. .num_reg_defaults_raw = TOTAL_NUM_REG,
  213. .cache_type = REGCACHE_RBTREE,
  214. .volatile_reg = is_volatile_reg,
  215. };
  216. static int __devinit tps65090_i2c_probe(struct i2c_client *client,
  217. const struct i2c_device_id *id)
  218. {
  219. struct tps65090_platform_data *pdata = client->dev.platform_data;
  220. struct tps65090 *tps65090;
  221. int ret;
  222. if (!pdata) {
  223. dev_err(&client->dev, "tps65090 requires platform data\n");
  224. return -EINVAL;
  225. }
  226. tps65090 = devm_kzalloc(&client->dev, sizeof(struct tps65090),
  227. GFP_KERNEL);
  228. if (tps65090 == NULL)
  229. return -ENOMEM;
  230. tps65090->client = client;
  231. tps65090->dev = &client->dev;
  232. i2c_set_clientdata(client, tps65090);
  233. mutex_init(&tps65090->lock);
  234. if (client->irq) {
  235. ret = tps65090_irq_init(tps65090, client->irq, pdata->irq_base);
  236. if (ret) {
  237. dev_err(&client->dev, "IRQ init failed with err: %d\n",
  238. ret);
  239. goto err_exit;
  240. }
  241. }
  242. tps65090->rmap = devm_regmap_init_i2c(tps65090->client,
  243. &tps65090_regmap_config);
  244. if (IS_ERR(tps65090->rmap)) {
  245. ret = PTR_ERR(tps65090->rmap);
  246. dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
  247. goto err_irq_exit;
  248. }
  249. ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
  250. ARRAY_SIZE(tps65090s), NULL, 0);
  251. if (ret) {
  252. dev_err(&client->dev, "add mfd devices failed with err: %d\n",
  253. ret);
  254. goto err_irq_exit;
  255. }
  256. return 0;
  257. err_irq_exit:
  258. if (client->irq)
  259. free_irq(client->irq, tps65090);
  260. err_exit:
  261. return ret;
  262. }
  263. static int __devexit tps65090_i2c_remove(struct i2c_client *client)
  264. {
  265. struct tps65090 *tps65090 = i2c_get_clientdata(client);
  266. mfd_remove_devices(tps65090->dev);
  267. if (client->irq)
  268. free_irq(client->irq, tps65090);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM_SLEEP
  272. static int tps65090_suspend(struct device *dev)
  273. {
  274. struct i2c_client *client = to_i2c_client(dev);
  275. if (client->irq)
  276. disable_irq(client->irq);
  277. return 0;
  278. }
  279. static int tps65090_resume(struct device *dev)
  280. {
  281. struct i2c_client *client = to_i2c_client(dev);
  282. if (client->irq)
  283. enable_irq(client->irq);
  284. return 0;
  285. }
  286. #endif
  287. static const struct dev_pm_ops tps65090_pm_ops = {
  288. SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
  289. };
  290. static const struct i2c_device_id tps65090_id_table[] = {
  291. { "tps65090", 0 },
  292. { },
  293. };
  294. MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
  295. static struct i2c_driver tps65090_driver = {
  296. .driver = {
  297. .name = "tps65090",
  298. .owner = THIS_MODULE,
  299. .pm = &tps65090_pm_ops,
  300. },
  301. .probe = tps65090_i2c_probe,
  302. .remove = __devexit_p(tps65090_i2c_remove),
  303. .id_table = tps65090_id_table,
  304. };
  305. static int __init tps65090_init(void)
  306. {
  307. return i2c_add_driver(&tps65090_driver);
  308. }
  309. subsys_initcall(tps65090_init);
  310. static void __exit tps65090_exit(void)
  311. {
  312. i2c_del_driver(&tps65090_driver);
  313. }
  314. module_exit(tps65090_exit);
  315. MODULE_DESCRIPTION("TPS65090 core driver");
  316. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  317. MODULE_LICENSE("GPL v2");