tps65090.c 9.3 KB

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