htc-egpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Support for the GPIO/IRQ expander chips present on several HTC phones.
  3. * These are implemented in CPLD chips present on the board.
  4. *
  5. * Copyright (c) 2007 Kevin O'Connor <kevin@koconnor.net>
  6. * Copyright (c) 2007 Philipp Zabel <philipp.zabel@gmail.com>
  7. *
  8. * This file may be distributed under the terms of the GNU GPL license.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/io.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/module.h>
  18. #include <linux/mfd/htc-egpio.h>
  19. struct egpio_chip {
  20. int reg_start;
  21. int cached_values;
  22. unsigned long is_out;
  23. struct device *dev;
  24. struct gpio_chip chip;
  25. };
  26. struct egpio_info {
  27. spinlock_t lock;
  28. /* iomem info */
  29. void __iomem *base_addr;
  30. int bus_shift; /* byte shift */
  31. int reg_shift; /* bit shift */
  32. int reg_mask;
  33. /* irq info */
  34. int ack_register;
  35. int ack_write;
  36. u16 irqs_enabled;
  37. uint irq_start;
  38. int nirqs;
  39. uint chained_irq;
  40. /* egpio info */
  41. struct egpio_chip *chip;
  42. int nchips;
  43. };
  44. static inline void egpio_writew(u16 value, struct egpio_info *ei, int reg)
  45. {
  46. writew(value, ei->base_addr + (reg << ei->bus_shift));
  47. }
  48. static inline u16 egpio_readw(struct egpio_info *ei, int reg)
  49. {
  50. return readw(ei->base_addr + (reg << ei->bus_shift));
  51. }
  52. /*
  53. * IRQs
  54. */
  55. static inline void ack_irqs(struct egpio_info *ei)
  56. {
  57. egpio_writew(ei->ack_write, ei, ei->ack_register);
  58. pr_debug("EGPIO ack - write %x to base+%x\n",
  59. ei->ack_write, ei->ack_register << ei->bus_shift);
  60. }
  61. static void egpio_ack(unsigned int irq)
  62. {
  63. }
  64. /* There does not appear to be a way to proactively mask interrupts
  65. * on the egpio chip itself. So, we simply ignore interrupts that
  66. * aren't desired. */
  67. static void egpio_mask(unsigned int irq)
  68. {
  69. struct egpio_info *ei = get_irq_chip_data(irq);
  70. ei->irqs_enabled &= ~(1 << (irq - ei->irq_start));
  71. pr_debug("EGPIO mask %d %04x\n", irq, ei->irqs_enabled);
  72. }
  73. static void egpio_unmask(unsigned int irq)
  74. {
  75. struct egpio_info *ei = get_irq_chip_data(irq);
  76. ei->irqs_enabled |= 1 << (irq - ei->irq_start);
  77. pr_debug("EGPIO unmask %d %04x\n", irq, ei->irqs_enabled);
  78. }
  79. static struct irq_chip egpio_muxed_chip = {
  80. .name = "htc-egpio",
  81. .ack = egpio_ack,
  82. .mask = egpio_mask,
  83. .unmask = egpio_unmask,
  84. };
  85. static void egpio_handler(unsigned int irq, struct irq_desc *desc)
  86. {
  87. struct egpio_info *ei = get_irq_data(irq);
  88. int irqpin;
  89. /* Read current pins. */
  90. unsigned long readval = egpio_readw(ei, ei->ack_register);
  91. pr_debug("IRQ reg: %x\n", (unsigned int)readval);
  92. /* Ack/unmask interrupts. */
  93. ack_irqs(ei);
  94. /* Process all set pins. */
  95. readval &= ei->irqs_enabled;
  96. for_each_bit(irqpin, &readval, ei->nirqs) {
  97. /* Run irq handler */
  98. pr_debug("got IRQ %d\n", irqpin);
  99. irq = ei->irq_start + irqpin;
  100. desc = &irq_desc[irq];
  101. desc->handle_irq(irq, desc);
  102. }
  103. }
  104. int htc_egpio_get_wakeup_irq(struct device *dev)
  105. {
  106. struct egpio_info *ei = dev_get_drvdata(dev);
  107. /* Read current pins. */
  108. u16 readval = egpio_readw(ei, ei->ack_register);
  109. /* Ack/unmask interrupts. */
  110. ack_irqs(ei);
  111. /* Return first set pin. */
  112. readval &= ei->irqs_enabled;
  113. return ei->irq_start + ffs(readval) - 1;
  114. }
  115. EXPORT_SYMBOL(htc_egpio_get_wakeup_irq);
  116. static inline int egpio_pos(struct egpio_info *ei, int bit)
  117. {
  118. return bit >> ei->reg_shift;
  119. }
  120. static inline int egpio_bit(struct egpio_info *ei, int bit)
  121. {
  122. return 1 << (bit & ((1 << ei->reg_shift)-1));
  123. }
  124. /*
  125. * Input pins
  126. */
  127. static int egpio_get(struct gpio_chip *chip, unsigned offset)
  128. {
  129. struct egpio_chip *egpio;
  130. struct egpio_info *ei;
  131. unsigned bit;
  132. int reg;
  133. int value;
  134. pr_debug("egpio_get_value(%d)\n", chip->base + offset);
  135. egpio = container_of(chip, struct egpio_chip, chip);
  136. ei = dev_get_drvdata(egpio->dev);
  137. bit = egpio_bit(ei, offset);
  138. reg = egpio->reg_start + egpio_pos(ei, offset);
  139. value = egpio_readw(ei, reg);
  140. pr_debug("readw(%p + %x) = %x\n",
  141. ei->base_addr, reg << ei->bus_shift, value);
  142. return value & bit;
  143. }
  144. static int egpio_direction_input(struct gpio_chip *chip, unsigned offset)
  145. {
  146. struct egpio_chip *egpio;
  147. egpio = container_of(chip, struct egpio_chip, chip);
  148. return test_bit(offset, &egpio->is_out) ? -EINVAL : 0;
  149. }
  150. /*
  151. * Output pins
  152. */
  153. static void egpio_set(struct gpio_chip *chip, unsigned offset, int value)
  154. {
  155. unsigned long flag;
  156. struct egpio_chip *egpio;
  157. struct egpio_info *ei;
  158. unsigned bit;
  159. int pos;
  160. int reg;
  161. int shift;
  162. pr_debug("egpio_set(%s, %d(%d), %d)\n",
  163. chip->label, offset, offset+chip->base, value);
  164. egpio = container_of(chip, struct egpio_chip, chip);
  165. ei = dev_get_drvdata(egpio->dev);
  166. bit = egpio_bit(ei, offset);
  167. pos = egpio_pos(ei, offset);
  168. reg = egpio->reg_start + pos;
  169. shift = pos << ei->reg_shift;
  170. pr_debug("egpio %s: reg %d = 0x%04x\n", value ? "set" : "clear",
  171. reg, (egpio->cached_values >> shift) & ei->reg_mask);
  172. spin_lock_irqsave(&ei->lock, flag);
  173. if (value)
  174. egpio->cached_values |= (1 << offset);
  175. else
  176. egpio->cached_values &= ~(1 << offset);
  177. egpio_writew((egpio->cached_values >> shift) & ei->reg_mask, ei, reg);
  178. spin_unlock_irqrestore(&ei->lock, flag);
  179. }
  180. static int egpio_direction_output(struct gpio_chip *chip,
  181. unsigned offset, int value)
  182. {
  183. struct egpio_chip *egpio;
  184. egpio = container_of(chip, struct egpio_chip, chip);
  185. if (test_bit(offset, &egpio->is_out)) {
  186. egpio_set(chip, offset, value);
  187. return 0;
  188. } else {
  189. return -EINVAL;
  190. }
  191. }
  192. static void egpio_write_cache(struct egpio_info *ei)
  193. {
  194. int i;
  195. struct egpio_chip *egpio;
  196. int shift;
  197. for (i = 0; i < ei->nchips; i++) {
  198. egpio = &(ei->chip[i]);
  199. if (!egpio->is_out)
  200. continue;
  201. for (shift = 0; shift < egpio->chip.ngpio;
  202. shift += (1<<ei->reg_shift)) {
  203. int reg = egpio->reg_start + egpio_pos(ei, shift);
  204. if (!((egpio->is_out >> shift) & ei->reg_mask))
  205. continue;
  206. pr_debug("EGPIO: setting %x to %x, was %x\n", reg,
  207. (egpio->cached_values >> shift) & ei->reg_mask,
  208. egpio_readw(ei, reg));
  209. egpio_writew((egpio->cached_values >> shift)
  210. & ei->reg_mask, ei, reg);
  211. }
  212. }
  213. }
  214. /*
  215. * Setup
  216. */
  217. static int __init egpio_probe(struct platform_device *pdev)
  218. {
  219. struct htc_egpio_platform_data *pdata = pdev->dev.platform_data;
  220. struct resource *res;
  221. struct egpio_info *ei;
  222. struct gpio_chip *chip;
  223. unsigned int irq, irq_end;
  224. int i;
  225. int ret;
  226. /* Initialize ei data structure. */
  227. ei = kzalloc(sizeof(*ei), GFP_KERNEL);
  228. if (!ei)
  229. return -ENOMEM;
  230. spin_lock_init(&ei->lock);
  231. /* Find chained irq */
  232. ret = -EINVAL;
  233. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  234. if (res)
  235. ei->chained_irq = res->start;
  236. /* Map egpio chip into virtual address space. */
  237. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  238. if (!res)
  239. goto fail;
  240. ei->base_addr = ioremap_nocache(res->start, res->end - res->start);
  241. if (!ei->base_addr)
  242. goto fail;
  243. pr_debug("EGPIO phys=%08x virt=%p\n", res->start, ei->base_addr);
  244. if ((pdata->bus_width != 16) && (pdata->bus_width != 32))
  245. goto fail;
  246. ei->bus_shift = fls(pdata->bus_width - 1) - 3;
  247. pr_debug("bus_shift = %d\n", ei->bus_shift);
  248. if ((pdata->reg_width != 8) && (pdata->reg_width != 16))
  249. goto fail;
  250. ei->reg_shift = fls(pdata->reg_width - 1);
  251. pr_debug("reg_shift = %d\n", ei->reg_shift);
  252. ei->reg_mask = (1 << pdata->reg_width) - 1;
  253. platform_set_drvdata(pdev, ei);
  254. ei->nchips = pdata->num_chips;
  255. ei->chip = kzalloc(sizeof(struct egpio_chip) * ei->nchips, GFP_KERNEL);
  256. if (!ei) {
  257. ret = -ENOMEM;
  258. goto fail;
  259. }
  260. for (i = 0; i < ei->nchips; i++) {
  261. ei->chip[i].reg_start = pdata->chip[i].reg_start;
  262. ei->chip[i].cached_values = pdata->chip[i].initial_values;
  263. ei->chip[i].is_out = pdata->chip[i].direction;
  264. ei->chip[i].dev = &(pdev->dev);
  265. chip = &(ei->chip[i].chip);
  266. chip->label = "htc-egpio";
  267. chip->dev = &pdev->dev;
  268. chip->owner = THIS_MODULE;
  269. chip->get = egpio_get;
  270. chip->set = egpio_set;
  271. chip->direction_input = egpio_direction_input;
  272. chip->direction_output = egpio_direction_output;
  273. chip->base = pdata->chip[i].gpio_base;
  274. chip->ngpio = pdata->chip[i].num_gpios;
  275. gpiochip_add(chip);
  276. }
  277. /* Set initial pin values */
  278. egpio_write_cache(ei);
  279. ei->irq_start = pdata->irq_base;
  280. ei->nirqs = pdata->num_irqs;
  281. ei->ack_register = pdata->ack_register;
  282. if (ei->chained_irq) {
  283. /* Setup irq handlers */
  284. ei->ack_write = 0xFFFF;
  285. if (pdata->invert_acks)
  286. ei->ack_write = 0;
  287. irq_end = ei->irq_start + ei->nirqs;
  288. for (irq = ei->irq_start; irq < irq_end; irq++) {
  289. set_irq_chip(irq, &egpio_muxed_chip);
  290. set_irq_chip_data(irq, ei);
  291. set_irq_handler(irq, handle_simple_irq);
  292. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  293. }
  294. set_irq_type(ei->chained_irq, IRQ_TYPE_EDGE_RISING);
  295. set_irq_data(ei->chained_irq, ei);
  296. set_irq_chained_handler(ei->chained_irq, egpio_handler);
  297. ack_irqs(ei);
  298. device_init_wakeup(&pdev->dev, 1);
  299. }
  300. return 0;
  301. fail:
  302. printk(KERN_ERR "EGPIO failed to setup\n");
  303. kfree(ei);
  304. return ret;
  305. }
  306. static int __exit egpio_remove(struct platform_device *pdev)
  307. {
  308. struct egpio_info *ei = platform_get_drvdata(pdev);
  309. unsigned int irq, irq_end;
  310. if (ei->chained_irq) {
  311. irq_end = ei->irq_start + ei->nirqs;
  312. for (irq = ei->irq_start; irq < irq_end; irq++) {
  313. set_irq_chip(irq, NULL);
  314. set_irq_handler(irq, NULL);
  315. set_irq_flags(irq, 0);
  316. }
  317. set_irq_chained_handler(ei->chained_irq, NULL);
  318. device_init_wakeup(&pdev->dev, 0);
  319. }
  320. iounmap(ei->base_addr);
  321. kfree(ei->chip);
  322. kfree(ei);
  323. return 0;
  324. }
  325. #ifdef CONFIG_PM
  326. static int egpio_suspend(struct platform_device *pdev, pm_message_t state)
  327. {
  328. struct egpio_info *ei = platform_get_drvdata(pdev);
  329. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  330. enable_irq_wake(ei->chained_irq);
  331. return 0;
  332. }
  333. static int egpio_resume(struct platform_device *pdev)
  334. {
  335. struct egpio_info *ei = platform_get_drvdata(pdev);
  336. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  337. disable_irq_wake(ei->chained_irq);
  338. /* Update registers from the cache, in case
  339. the CPLD was powered off during suspend */
  340. egpio_write_cache(ei);
  341. return 0;
  342. }
  343. #else
  344. #define egpio_suspend NULL
  345. #define egpio_resume NULL
  346. #endif
  347. static struct platform_driver egpio_driver = {
  348. .driver = {
  349. .name = "htc-egpio",
  350. },
  351. .remove = __exit_p(egpio_remove),
  352. .suspend = egpio_suspend,
  353. .resume = egpio_resume,
  354. };
  355. static int __init egpio_init(void)
  356. {
  357. return platform_driver_probe(&egpio_driver, egpio_probe);
  358. }
  359. static void __exit egpio_exit(void)
  360. {
  361. platform_driver_unregister(&egpio_driver);
  362. }
  363. /* start early for dependencies */
  364. subsys_initcall(egpio_init);
  365. module_exit(egpio_exit)
  366. MODULE_LICENSE("GPL");
  367. MODULE_AUTHOR("Kevin O'Connor <kevin@koconnor.net>");