s5m-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * s5m-irq.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd
  5. * http://www.samsung.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/mfd/s5m87xx/s5m-core.h>
  17. struct s5m_irq_data {
  18. int reg;
  19. int mask;
  20. };
  21. static struct s5m_irq_data s5m8767_irqs[] = {
  22. [S5M8767_IRQ_PWRR] = {
  23. .reg = 1,
  24. .mask = S5M8767_IRQ_PWRR_MASK,
  25. },
  26. [S5M8767_IRQ_PWRF] = {
  27. .reg = 1,
  28. .mask = S5M8767_IRQ_PWRF_MASK,
  29. },
  30. [S5M8767_IRQ_PWR1S] = {
  31. .reg = 1,
  32. .mask = S5M8767_IRQ_PWR1S_MASK,
  33. },
  34. [S5M8767_IRQ_JIGR] = {
  35. .reg = 1,
  36. .mask = S5M8767_IRQ_JIGR_MASK,
  37. },
  38. [S5M8767_IRQ_JIGF] = {
  39. .reg = 1,
  40. .mask = S5M8767_IRQ_JIGF_MASK,
  41. },
  42. [S5M8767_IRQ_LOWBAT2] = {
  43. .reg = 1,
  44. .mask = S5M8767_IRQ_LOWBAT2_MASK,
  45. },
  46. [S5M8767_IRQ_LOWBAT1] = {
  47. .reg = 1,
  48. .mask = S5M8767_IRQ_LOWBAT1_MASK,
  49. },
  50. [S5M8767_IRQ_MRB] = {
  51. .reg = 2,
  52. .mask = S5M8767_IRQ_MRB_MASK,
  53. },
  54. [S5M8767_IRQ_DVSOK2] = {
  55. .reg = 2,
  56. .mask = S5M8767_IRQ_DVSOK2_MASK,
  57. },
  58. [S5M8767_IRQ_DVSOK3] = {
  59. .reg = 2,
  60. .mask = S5M8767_IRQ_DVSOK3_MASK,
  61. },
  62. [S5M8767_IRQ_DVSOK4] = {
  63. .reg = 2,
  64. .mask = S5M8767_IRQ_DVSOK4_MASK,
  65. },
  66. [S5M8767_IRQ_RTC60S] = {
  67. .reg = 3,
  68. .mask = S5M8767_IRQ_RTC60S_MASK,
  69. },
  70. [S5M8767_IRQ_RTCA1] = {
  71. .reg = 3,
  72. .mask = S5M8767_IRQ_RTCA1_MASK,
  73. },
  74. [S5M8767_IRQ_RTCA2] = {
  75. .reg = 3,
  76. .mask = S5M8767_IRQ_RTCA2_MASK,
  77. },
  78. [S5M8767_IRQ_SMPL] = {
  79. .reg = 3,
  80. .mask = S5M8767_IRQ_SMPL_MASK,
  81. },
  82. [S5M8767_IRQ_RTC1S] = {
  83. .reg = 3,
  84. .mask = S5M8767_IRQ_RTC1S_MASK,
  85. },
  86. [S5M8767_IRQ_WTSR] = {
  87. .reg = 3,
  88. .mask = S5M8767_IRQ_WTSR_MASK,
  89. },
  90. };
  91. static struct s5m_irq_data s5m8763_irqs[] = {
  92. [S5M8763_IRQ_DCINF] = {
  93. .reg = 1,
  94. .mask = S5M8763_IRQ_DCINF_MASK,
  95. },
  96. [S5M8763_IRQ_DCINR] = {
  97. .reg = 1,
  98. .mask = S5M8763_IRQ_DCINR_MASK,
  99. },
  100. [S5M8763_IRQ_JIGF] = {
  101. .reg = 1,
  102. .mask = S5M8763_IRQ_JIGF_MASK,
  103. },
  104. [S5M8763_IRQ_JIGR] = {
  105. .reg = 1,
  106. .mask = S5M8763_IRQ_JIGR_MASK,
  107. },
  108. [S5M8763_IRQ_PWRONF] = {
  109. .reg = 1,
  110. .mask = S5M8763_IRQ_PWRONF_MASK,
  111. },
  112. [S5M8763_IRQ_PWRONR] = {
  113. .reg = 1,
  114. .mask = S5M8763_IRQ_PWRONR_MASK,
  115. },
  116. [S5M8763_IRQ_WTSREVNT] = {
  117. .reg = 2,
  118. .mask = S5M8763_IRQ_WTSREVNT_MASK,
  119. },
  120. [S5M8763_IRQ_SMPLEVNT] = {
  121. .reg = 2,
  122. .mask = S5M8763_IRQ_SMPLEVNT_MASK,
  123. },
  124. [S5M8763_IRQ_ALARM1] = {
  125. .reg = 2,
  126. .mask = S5M8763_IRQ_ALARM1_MASK,
  127. },
  128. [S5M8763_IRQ_ALARM0] = {
  129. .reg = 2,
  130. .mask = S5M8763_IRQ_ALARM0_MASK,
  131. },
  132. [S5M8763_IRQ_ONKEY1S] = {
  133. .reg = 3,
  134. .mask = S5M8763_IRQ_ONKEY1S_MASK,
  135. },
  136. [S5M8763_IRQ_TOPOFFR] = {
  137. .reg = 3,
  138. .mask = S5M8763_IRQ_TOPOFFR_MASK,
  139. },
  140. [S5M8763_IRQ_DCINOVPR] = {
  141. .reg = 3,
  142. .mask = S5M8763_IRQ_DCINOVPR_MASK,
  143. },
  144. [S5M8763_IRQ_CHGRSTF] = {
  145. .reg = 3,
  146. .mask = S5M8763_IRQ_CHGRSTF_MASK,
  147. },
  148. [S5M8763_IRQ_DONER] = {
  149. .reg = 3,
  150. .mask = S5M8763_IRQ_DONER_MASK,
  151. },
  152. [S5M8763_IRQ_CHGFAULT] = {
  153. .reg = 3,
  154. .mask = S5M8763_IRQ_CHGFAULT_MASK,
  155. },
  156. [S5M8763_IRQ_LOBAT1] = {
  157. .reg = 4,
  158. .mask = S5M8763_IRQ_LOBAT1_MASK,
  159. },
  160. [S5M8763_IRQ_LOBAT2] = {
  161. .reg = 4,
  162. .mask = S5M8763_IRQ_LOBAT2_MASK,
  163. },
  164. };
  165. static inline struct s5m_irq_data *
  166. irq_to_s5m8767_irq(struct s5m87xx_dev *s5m87xx, int irq)
  167. {
  168. return &s5m8767_irqs[irq - s5m87xx->irq_base];
  169. }
  170. static void s5m8767_irq_lock(struct irq_data *data)
  171. {
  172. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  173. mutex_lock(&s5m87xx->irqlock);
  174. }
  175. static void s5m8767_irq_sync_unlock(struct irq_data *data)
  176. {
  177. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  178. int i;
  179. for (i = 0; i < ARRAY_SIZE(s5m87xx->irq_masks_cur); i++) {
  180. if (s5m87xx->irq_masks_cur[i] != s5m87xx->irq_masks_cache[i]) {
  181. s5m87xx->irq_masks_cache[i] = s5m87xx->irq_masks_cur[i];
  182. s5m_reg_write(s5m87xx, S5M8767_REG_INT1M + i,
  183. s5m87xx->irq_masks_cur[i]);
  184. }
  185. }
  186. mutex_unlock(&s5m87xx->irqlock);
  187. }
  188. static void s5m8767_irq_unmask(struct irq_data *data)
  189. {
  190. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  191. struct s5m_irq_data *irq_data = irq_to_s5m8767_irq(s5m87xx,
  192. data->irq);
  193. s5m87xx->irq_masks_cur[irq_data->reg - 1] &= ~irq_data->mask;
  194. }
  195. static void s5m8767_irq_mask(struct irq_data *data)
  196. {
  197. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  198. struct s5m_irq_data *irq_data = irq_to_s5m8767_irq(s5m87xx,
  199. data->irq);
  200. s5m87xx->irq_masks_cur[irq_data->reg - 1] |= irq_data->mask;
  201. }
  202. static struct irq_chip s5m8767_irq_chip = {
  203. .name = "s5m8767",
  204. .irq_bus_lock = s5m8767_irq_lock,
  205. .irq_bus_sync_unlock = s5m8767_irq_sync_unlock,
  206. .irq_mask = s5m8767_irq_mask,
  207. .irq_unmask = s5m8767_irq_unmask,
  208. };
  209. static inline struct s5m_irq_data *
  210. irq_to_s5m8763_irq(struct s5m87xx_dev *s5m87xx, int irq)
  211. {
  212. return &s5m8763_irqs[irq - s5m87xx->irq_base];
  213. }
  214. static void s5m8763_irq_lock(struct irq_data *data)
  215. {
  216. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  217. mutex_lock(&s5m87xx->irqlock);
  218. }
  219. static void s5m8763_irq_sync_unlock(struct irq_data *data)
  220. {
  221. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  222. int i;
  223. for (i = 0; i < ARRAY_SIZE(s5m87xx->irq_masks_cur); i++) {
  224. if (s5m87xx->irq_masks_cur[i] != s5m87xx->irq_masks_cache[i]) {
  225. s5m87xx->irq_masks_cache[i] = s5m87xx->irq_masks_cur[i];
  226. s5m_reg_write(s5m87xx, S5M8763_REG_IRQM1 + i,
  227. s5m87xx->irq_masks_cur[i]);
  228. }
  229. }
  230. mutex_unlock(&s5m87xx->irqlock);
  231. }
  232. static void s5m8763_irq_unmask(struct irq_data *data)
  233. {
  234. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  235. struct s5m_irq_data *irq_data = irq_to_s5m8763_irq(s5m87xx,
  236. data->irq);
  237. s5m87xx->irq_masks_cur[irq_data->reg - 1] &= ~irq_data->mask;
  238. }
  239. static void s5m8763_irq_mask(struct irq_data *data)
  240. {
  241. struct s5m87xx_dev *s5m87xx = irq_data_get_irq_chip_data(data);
  242. struct s5m_irq_data *irq_data = irq_to_s5m8763_irq(s5m87xx,
  243. data->irq);
  244. s5m87xx->irq_masks_cur[irq_data->reg - 1] |= irq_data->mask;
  245. }
  246. static struct irq_chip s5m8763_irq_chip = {
  247. .name = "s5m8763",
  248. .irq_bus_lock = s5m8763_irq_lock,
  249. .irq_bus_sync_unlock = s5m8763_irq_sync_unlock,
  250. .irq_mask = s5m8763_irq_mask,
  251. .irq_unmask = s5m8763_irq_unmask,
  252. };
  253. static irqreturn_t s5m8767_irq_thread(int irq, void *data)
  254. {
  255. struct s5m87xx_dev *s5m87xx = data;
  256. u8 irq_reg[NUM_IRQ_REGS-1];
  257. int ret;
  258. int i;
  259. ret = s5m_bulk_read(s5m87xx, S5M8767_REG_INT1,
  260. NUM_IRQ_REGS - 1, irq_reg);
  261. if (ret < 0) {
  262. dev_err(s5m87xx->dev, "Failed to read interrupt register: %d\n",
  263. ret);
  264. return IRQ_NONE;
  265. }
  266. for (i = 0; i < NUM_IRQ_REGS - 1; i++)
  267. irq_reg[i] &= ~s5m87xx->irq_masks_cur[i];
  268. for (i = 0; i < S5M8767_IRQ_NR; i++) {
  269. if (irq_reg[s5m8767_irqs[i].reg - 1] & s5m8767_irqs[i].mask)
  270. handle_nested_irq(s5m87xx->irq_base + i);
  271. }
  272. return IRQ_HANDLED;
  273. }
  274. static irqreturn_t s5m8763_irq_thread(int irq, void *data)
  275. {
  276. struct s5m87xx_dev *s5m87xx = data;
  277. u8 irq_reg[NUM_IRQ_REGS];
  278. int ret;
  279. int i;
  280. ret = s5m_bulk_read(s5m87xx, S5M8763_REG_IRQ1,
  281. NUM_IRQ_REGS, irq_reg);
  282. if (ret < 0) {
  283. dev_err(s5m87xx->dev, "Failed to read interrupt register: %d\n",
  284. ret);
  285. return IRQ_NONE;
  286. }
  287. for (i = 0; i < NUM_IRQ_REGS; i++)
  288. irq_reg[i] &= ~s5m87xx->irq_masks_cur[i];
  289. for (i = 0; i < S5M8763_IRQ_NR; i++) {
  290. if (irq_reg[s5m8763_irqs[i].reg - 1] & s5m8763_irqs[i].mask)
  291. handle_nested_irq(s5m87xx->irq_base + i);
  292. }
  293. return IRQ_HANDLED;
  294. }
  295. int s5m_irq_resume(struct s5m87xx_dev *s5m87xx)
  296. {
  297. if (s5m87xx->irq && s5m87xx->irq_base){
  298. switch (s5m87xx->device_type) {
  299. case S5M8763X:
  300. s5m8763_irq_thread(s5m87xx->irq_base, s5m87xx);
  301. break;
  302. case S5M8767X:
  303. s5m8767_irq_thread(s5m87xx->irq_base, s5m87xx);
  304. break;
  305. default:
  306. dev_err(s5m87xx->dev,
  307. "Unknown device type %d\n",
  308. s5m87xx->device_type);
  309. return -EINVAL;
  310. }
  311. }
  312. return 0;
  313. }
  314. int s5m_irq_init(struct s5m87xx_dev *s5m87xx)
  315. {
  316. int i;
  317. int cur_irq;
  318. int ret = 0;
  319. int type = s5m87xx->device_type;
  320. if (!s5m87xx->irq) {
  321. dev_warn(s5m87xx->dev,
  322. "No interrupt specified, no interrupts\n");
  323. s5m87xx->irq_base = 0;
  324. return 0;
  325. }
  326. if (!s5m87xx->irq_base) {
  327. dev_err(s5m87xx->dev,
  328. "No interrupt base specified, no interrupts\n");
  329. return 0;
  330. }
  331. mutex_init(&s5m87xx->irqlock);
  332. switch (type) {
  333. case S5M8763X:
  334. for (i = 0; i < NUM_IRQ_REGS; i++) {
  335. s5m87xx->irq_masks_cur[i] = 0xff;
  336. s5m87xx->irq_masks_cache[i] = 0xff;
  337. s5m_reg_write(s5m87xx, S5M8763_REG_IRQM1 + i,
  338. 0xff);
  339. }
  340. s5m_reg_write(s5m87xx, S5M8763_REG_STATUSM1, 0xff);
  341. s5m_reg_write(s5m87xx, S5M8763_REG_STATUSM2, 0xff);
  342. for (i = 0; i < S5M8763_IRQ_NR; i++) {
  343. cur_irq = i + s5m87xx->irq_base;
  344. irq_set_chip_data(cur_irq, s5m87xx);
  345. irq_set_chip_and_handler(cur_irq, &s5m8763_irq_chip,
  346. handle_edge_irq);
  347. irq_set_nested_thread(cur_irq, 1);
  348. #ifdef CONFIG_ARM
  349. set_irq_flags(cur_irq, IRQF_VALID);
  350. #else
  351. irq_set_noprobe(cur_irq);
  352. #endif
  353. }
  354. ret = request_threaded_irq(s5m87xx->irq, NULL,
  355. s5m8763_irq_thread,
  356. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  357. "s5m87xx-irq", s5m87xx);
  358. if (ret) {
  359. dev_err(s5m87xx->dev, "Failed to request IRQ %d: %d\n",
  360. s5m87xx->irq, ret);
  361. return ret;
  362. }
  363. break;
  364. case S5M8767X:
  365. for (i = 0; i < NUM_IRQ_REGS - 1; i++) {
  366. s5m87xx->irq_masks_cur[i] = 0xff;
  367. s5m87xx->irq_masks_cache[i] = 0xff;
  368. s5m_reg_write(s5m87xx, S5M8767_REG_INT1M + i,
  369. 0xff);
  370. }
  371. for (i = 0; i < S5M8767_IRQ_NR; i++) {
  372. cur_irq = i + s5m87xx->irq_base;
  373. irq_set_chip_data(cur_irq, s5m87xx);
  374. if (ret) {
  375. dev_err(s5m87xx->dev,
  376. "Failed to irq_set_chip_data %d: %d\n",
  377. s5m87xx->irq, ret);
  378. return ret;
  379. }
  380. irq_set_chip_and_handler(cur_irq, &s5m8767_irq_chip,
  381. handle_edge_irq);
  382. irq_set_nested_thread(cur_irq, 1);
  383. #ifdef CONFIG_ARM
  384. set_irq_flags(cur_irq, IRQF_VALID);
  385. #else
  386. irq_set_noprobe(cur_irq);
  387. #endif
  388. }
  389. ret = request_threaded_irq(s5m87xx->irq, NULL,
  390. s5m8767_irq_thread,
  391. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  392. "s5m87xx-irq", s5m87xx);
  393. if (ret) {
  394. dev_err(s5m87xx->dev, "Failed to request IRQ %d: %d\n",
  395. s5m87xx->irq, ret);
  396. return ret;
  397. }
  398. break;
  399. default:
  400. dev_err(s5m87xx->dev,
  401. "Unknown device type %d\n", s5m87xx->device_type);
  402. return -EINVAL;
  403. }
  404. if (!s5m87xx->ono)
  405. return 0;
  406. switch (type) {
  407. case S5M8763X:
  408. ret = request_threaded_irq(s5m87xx->ono, NULL,
  409. s5m8763_irq_thread,
  410. IRQF_TRIGGER_FALLING |
  411. IRQF_TRIGGER_RISING |
  412. IRQF_ONESHOT, "s5m87xx-ono",
  413. s5m87xx);
  414. break;
  415. case S5M8767X:
  416. ret = request_threaded_irq(s5m87xx->ono, NULL,
  417. s5m8767_irq_thread,
  418. IRQF_TRIGGER_FALLING |
  419. IRQF_TRIGGER_RISING |
  420. IRQF_ONESHOT, "s5m87xx-ono", s5m87xx);
  421. break;
  422. default:
  423. ret = -EINVAL;
  424. break;
  425. }
  426. if (ret) {
  427. dev_err(s5m87xx->dev, "Failed to request IRQ %d: %d\n",
  428. s5m87xx->ono, ret);
  429. return ret;
  430. }
  431. return 0;
  432. }
  433. void s5m_irq_exit(struct s5m87xx_dev *s5m87xx)
  434. {
  435. if (s5m87xx->ono)
  436. free_irq(s5m87xx->ono, s5m87xx);
  437. if (s5m87xx->irq)
  438. free_irq(s5m87xx->irq, s5m87xx);
  439. }