ab8500-core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/irq.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/mfd/ab8500.h>
  18. /*
  19. * Interrupt register offsets
  20. * Bank : 0x0E
  21. */
  22. #define AB8500_IT_SOURCE1_REG 0x0E00
  23. #define AB8500_IT_SOURCE2_REG 0x0E01
  24. #define AB8500_IT_SOURCE3_REG 0x0E02
  25. #define AB8500_IT_SOURCE4_REG 0x0E03
  26. #define AB8500_IT_SOURCE5_REG 0x0E04
  27. #define AB8500_IT_SOURCE6_REG 0x0E05
  28. #define AB8500_IT_SOURCE7_REG 0x0E06
  29. #define AB8500_IT_SOURCE8_REG 0x0E07
  30. #define AB8500_IT_SOURCE19_REG 0x0E12
  31. #define AB8500_IT_SOURCE20_REG 0x0E13
  32. #define AB8500_IT_SOURCE21_REG 0x0E14
  33. #define AB8500_IT_SOURCE22_REG 0x0E15
  34. #define AB8500_IT_SOURCE23_REG 0x0E16
  35. #define AB8500_IT_SOURCE24_REG 0x0E17
  36. /*
  37. * latch registers
  38. */
  39. #define AB8500_IT_LATCH1_REG 0x0E20
  40. #define AB8500_IT_LATCH2_REG 0x0E21
  41. #define AB8500_IT_LATCH3_REG 0x0E22
  42. #define AB8500_IT_LATCH4_REG 0x0E23
  43. #define AB8500_IT_LATCH5_REG 0x0E24
  44. #define AB8500_IT_LATCH6_REG 0x0E25
  45. #define AB8500_IT_LATCH7_REG 0x0E26
  46. #define AB8500_IT_LATCH8_REG 0x0E27
  47. #define AB8500_IT_LATCH9_REG 0x0E28
  48. #define AB8500_IT_LATCH10_REG 0x0E29
  49. #define AB8500_IT_LATCH19_REG 0x0E32
  50. #define AB8500_IT_LATCH20_REG 0x0E33
  51. #define AB8500_IT_LATCH21_REG 0x0E34
  52. #define AB8500_IT_LATCH22_REG 0x0E35
  53. #define AB8500_IT_LATCH23_REG 0x0E36
  54. #define AB8500_IT_LATCH24_REG 0x0E37
  55. /*
  56. * mask registers
  57. */
  58. #define AB8500_IT_MASK1_REG 0x0E40
  59. #define AB8500_IT_MASK2_REG 0x0E41
  60. #define AB8500_IT_MASK3_REG 0x0E42
  61. #define AB8500_IT_MASK4_REG 0x0E43
  62. #define AB8500_IT_MASK5_REG 0x0E44
  63. #define AB8500_IT_MASK6_REG 0x0E45
  64. #define AB8500_IT_MASK7_REG 0x0E46
  65. #define AB8500_IT_MASK8_REG 0x0E47
  66. #define AB8500_IT_MASK9_REG 0x0E48
  67. #define AB8500_IT_MASK10_REG 0x0E49
  68. #define AB8500_IT_MASK11_REG 0x0E4A
  69. #define AB8500_IT_MASK12_REG 0x0E4B
  70. #define AB8500_IT_MASK13_REG 0x0E4C
  71. #define AB8500_IT_MASK14_REG 0x0E4D
  72. #define AB8500_IT_MASK15_REG 0x0E4E
  73. #define AB8500_IT_MASK16_REG 0x0E4F
  74. #define AB8500_IT_MASK17_REG 0x0E50
  75. #define AB8500_IT_MASK18_REG 0x0E51
  76. #define AB8500_IT_MASK19_REG 0x0E52
  77. #define AB8500_IT_MASK20_REG 0x0E53
  78. #define AB8500_IT_MASK21_REG 0x0E54
  79. #define AB8500_IT_MASK22_REG 0x0E55
  80. #define AB8500_IT_MASK23_REG 0x0E56
  81. #define AB8500_IT_MASK24_REG 0x0E57
  82. #define AB8500_REV_REG 0x1080
  83. /*
  84. * Map interrupt numbers to the LATCH and MASK register offsets, Interrupt
  85. * numbers are indexed into this array with (num / 8).
  86. *
  87. * This is one off from the register names, i.e. AB8500_IT_MASK1_REG is at
  88. * offset 0.
  89. */
  90. static const int ab8500_irq_regoffset[AB8500_NUM_IRQ_REGS] = {
  91. 0, 1, 2, 3, 4, 6, 7, 8, 9, 18, 19, 20, 21,
  92. };
  93. static int __ab8500_write(struct ab8500 *ab8500, u16 addr, u8 data)
  94. {
  95. int ret;
  96. dev_vdbg(ab8500->dev, "wr: addr %#x <= %#x\n", addr, data);
  97. ret = ab8500->write(ab8500, addr, data);
  98. if (ret < 0)
  99. dev_err(ab8500->dev, "failed to write reg %#x: %d\n",
  100. addr, ret);
  101. return ret;
  102. }
  103. /**
  104. * ab8500_write() - write an AB8500 register
  105. * @ab8500: device to write to
  106. * @addr: address of the register
  107. * @data: value to write
  108. */
  109. int ab8500_write(struct ab8500 *ab8500, u16 addr, u8 data)
  110. {
  111. int ret;
  112. mutex_lock(&ab8500->lock);
  113. ret = __ab8500_write(ab8500, addr, data);
  114. mutex_unlock(&ab8500->lock);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(ab8500_write);
  118. static int __ab8500_read(struct ab8500 *ab8500, u16 addr)
  119. {
  120. int ret;
  121. ret = ab8500->read(ab8500, addr);
  122. if (ret < 0)
  123. dev_err(ab8500->dev, "failed to read reg %#x: %d\n",
  124. addr, ret);
  125. dev_vdbg(ab8500->dev, "rd: addr %#x => data %#x\n", addr, ret);
  126. return ret;
  127. }
  128. /**
  129. * ab8500_read() - read an AB8500 register
  130. * @ab8500: device to read from
  131. * @addr: address of the register
  132. */
  133. int ab8500_read(struct ab8500 *ab8500, u16 addr)
  134. {
  135. int ret;
  136. mutex_lock(&ab8500->lock);
  137. ret = __ab8500_read(ab8500, addr);
  138. mutex_unlock(&ab8500->lock);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(ab8500_read);
  142. /**
  143. * ab8500_set_bits() - set a bitfield in an AB8500 register
  144. * @ab8500: device to read from
  145. * @addr: address of the register
  146. * @mask: mask of the bitfield to modify
  147. * @data: value to set to the bitfield
  148. */
  149. int ab8500_set_bits(struct ab8500 *ab8500, u16 addr, u8 mask, u8 data)
  150. {
  151. int ret;
  152. mutex_lock(&ab8500->lock);
  153. ret = __ab8500_read(ab8500, addr);
  154. if (ret < 0)
  155. goto out;
  156. ret &= ~mask;
  157. ret |= data;
  158. ret = __ab8500_write(ab8500, addr, ret);
  159. out:
  160. mutex_unlock(&ab8500->lock);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(ab8500_set_bits);
  164. static void ab8500_irq_lock(unsigned int irq)
  165. {
  166. struct ab8500 *ab8500 = get_irq_chip_data(irq);
  167. mutex_lock(&ab8500->irq_lock);
  168. }
  169. static void ab8500_irq_sync_unlock(unsigned int irq)
  170. {
  171. struct ab8500 *ab8500 = get_irq_chip_data(irq);
  172. int i;
  173. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
  174. u8 old = ab8500->oldmask[i];
  175. u8 new = ab8500->mask[i];
  176. int reg;
  177. if (new == old)
  178. continue;
  179. ab8500->oldmask[i] = new;
  180. reg = AB8500_IT_MASK1_REG + ab8500_irq_regoffset[i];
  181. ab8500_write(ab8500, reg, new);
  182. }
  183. mutex_unlock(&ab8500->irq_lock);
  184. }
  185. static void ab8500_irq_mask(unsigned int irq)
  186. {
  187. struct ab8500 *ab8500 = get_irq_chip_data(irq);
  188. int offset = irq - ab8500->irq_base;
  189. int index = offset / 8;
  190. int mask = 1 << (offset % 8);
  191. ab8500->mask[index] |= mask;
  192. }
  193. static void ab8500_irq_unmask(unsigned int irq)
  194. {
  195. struct ab8500 *ab8500 = get_irq_chip_data(irq);
  196. int offset = irq - ab8500->irq_base;
  197. int index = offset / 8;
  198. int mask = 1 << (offset % 8);
  199. ab8500->mask[index] &= ~mask;
  200. }
  201. static struct irq_chip ab8500_irq_chip = {
  202. .name = "ab8500",
  203. .bus_lock = ab8500_irq_lock,
  204. .bus_sync_unlock = ab8500_irq_sync_unlock,
  205. .mask = ab8500_irq_mask,
  206. .unmask = ab8500_irq_unmask,
  207. };
  208. static irqreturn_t ab8500_irq(int irq, void *dev)
  209. {
  210. struct ab8500 *ab8500 = dev;
  211. int i;
  212. dev_vdbg(ab8500->dev, "interrupt\n");
  213. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++) {
  214. int regoffset = ab8500_irq_regoffset[i];
  215. int status;
  216. status = ab8500_read(ab8500, AB8500_IT_LATCH1_REG + regoffset);
  217. if (status <= 0)
  218. continue;
  219. do {
  220. int bit = __ffs(status);
  221. int line = i * 8 + bit;
  222. handle_nested_irq(ab8500->irq_base + line);
  223. status &= ~(1 << bit);
  224. } while (status);
  225. }
  226. return IRQ_HANDLED;
  227. }
  228. static int ab8500_irq_init(struct ab8500 *ab8500)
  229. {
  230. int base = ab8500->irq_base;
  231. int irq;
  232. for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
  233. set_irq_chip_data(irq, ab8500);
  234. set_irq_chip_and_handler(irq, &ab8500_irq_chip,
  235. handle_simple_irq);
  236. set_irq_nested_thread(irq, 1);
  237. #ifdef CONFIG_ARM
  238. set_irq_flags(irq, IRQF_VALID);
  239. #else
  240. set_irq_noprobe(irq);
  241. #endif
  242. }
  243. return 0;
  244. }
  245. static void ab8500_irq_remove(struct ab8500 *ab8500)
  246. {
  247. int base = ab8500->irq_base;
  248. int irq;
  249. for (irq = base; irq < base + AB8500_NR_IRQS; irq++) {
  250. #ifdef CONFIG_ARM
  251. set_irq_flags(irq, 0);
  252. #endif
  253. set_irq_chip_and_handler(irq, NULL, NULL);
  254. set_irq_chip_data(irq, NULL);
  255. }
  256. }
  257. static struct resource ab8500_gpadc_resources[] = {
  258. {
  259. .name = "HW_CONV_END",
  260. .start = AB8500_INT_GP_HW_ADC_CONV_END,
  261. .end = AB8500_INT_GP_HW_ADC_CONV_END,
  262. .flags = IORESOURCE_IRQ,
  263. },
  264. {
  265. .name = "SW_CONV_END",
  266. .start = AB8500_INT_GP_SW_ADC_CONV_END,
  267. .end = AB8500_INT_GP_SW_ADC_CONV_END,
  268. .flags = IORESOURCE_IRQ,
  269. },
  270. };
  271. static struct resource ab8500_rtc_resources[] = {
  272. {
  273. .name = "60S",
  274. .start = AB8500_INT_RTC_60S,
  275. .end = AB8500_INT_RTC_60S,
  276. .flags = IORESOURCE_IRQ,
  277. },
  278. {
  279. .name = "ALARM",
  280. .start = AB8500_INT_RTC_ALARM,
  281. .end = AB8500_INT_RTC_ALARM,
  282. .flags = IORESOURCE_IRQ,
  283. },
  284. };
  285. static struct mfd_cell ab8500_devs[] = {
  286. {
  287. .name = "ab8500-gpadc",
  288. .num_resources = ARRAY_SIZE(ab8500_gpadc_resources),
  289. .resources = ab8500_gpadc_resources,
  290. },
  291. {
  292. .name = "ab8500-rtc",
  293. .num_resources = ARRAY_SIZE(ab8500_rtc_resources),
  294. .resources = ab8500_rtc_resources,
  295. },
  296. { .name = "ab8500-charger", },
  297. { .name = "ab8500-audio", },
  298. { .name = "ab8500-usb", },
  299. { .name = "ab8500-pwm", },
  300. };
  301. int __devinit ab8500_init(struct ab8500 *ab8500)
  302. {
  303. struct ab8500_platform_data *plat = dev_get_platdata(ab8500->dev);
  304. int ret;
  305. int i;
  306. if (plat)
  307. ab8500->irq_base = plat->irq_base;
  308. mutex_init(&ab8500->lock);
  309. mutex_init(&ab8500->irq_lock);
  310. ret = ab8500_read(ab8500, AB8500_REV_REG);
  311. if (ret < 0)
  312. return ret;
  313. /*
  314. * 0x0 - Early Drop
  315. * 0x10 - Cut 1.0
  316. * 0x11 - Cut 1.1
  317. */
  318. if (ret == 0x0 || ret == 0x10 || ret == 0x11) {
  319. ab8500->revision = ret;
  320. dev_info(ab8500->dev, "detected chip, revision: %#x\n", ret);
  321. } else {
  322. dev_err(ab8500->dev, "unknown chip, revision: %#x\n", ret);
  323. return -EINVAL;
  324. }
  325. if (plat && plat->init)
  326. plat->init(ab8500);
  327. /* Clear and mask all interrupts */
  328. for (i = 0; i < 10; i++) {
  329. ab8500_read(ab8500, AB8500_IT_LATCH1_REG + i);
  330. ab8500_write(ab8500, AB8500_IT_MASK1_REG + i, 0xff);
  331. }
  332. for (i = 18; i < 24; i++) {
  333. ab8500_read(ab8500, AB8500_IT_LATCH1_REG + i);
  334. ab8500_write(ab8500, AB8500_IT_MASK1_REG + i, 0xff);
  335. }
  336. for (i = 0; i < AB8500_NUM_IRQ_REGS; i++)
  337. ab8500->mask[i] = ab8500->oldmask[i] = 0xff;
  338. if (ab8500->irq_base) {
  339. ret = ab8500_irq_init(ab8500);
  340. if (ret)
  341. return ret;
  342. ret = request_threaded_irq(ab8500->irq, NULL, ab8500_irq,
  343. IRQF_ONESHOT, "ab8500", ab8500);
  344. if (ret)
  345. goto out_removeirq;
  346. }
  347. ret = mfd_add_devices(ab8500->dev, -1, ab8500_devs,
  348. ARRAY_SIZE(ab8500_devs), NULL,
  349. ab8500->irq_base);
  350. if (ret)
  351. goto out_freeirq;
  352. return ret;
  353. out_freeirq:
  354. if (ab8500->irq_base) {
  355. free_irq(ab8500->irq, ab8500);
  356. out_removeirq:
  357. ab8500_irq_remove(ab8500);
  358. }
  359. return ret;
  360. }
  361. int __devexit ab8500_exit(struct ab8500 *ab8500)
  362. {
  363. mfd_remove_devices(ab8500->dev);
  364. if (ab8500->irq_base) {
  365. free_irq(ab8500->irq, ab8500);
  366. ab8500_irq_remove(ab8500);
  367. }
  368. return 0;
  369. }
  370. MODULE_AUTHOR("Srinidhi Kasagar, Rabin Vincent");
  371. MODULE_DESCRIPTION("AB8500 MFD core");
  372. MODULE_LICENSE("GPL v2");