ab8500-core.c 9.9 KB

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