gpio-htc-egpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_data/gpio-htc-egpio.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/gpio/driver.h>
  21. struct egpio_chip {
  22. int reg_start;
  23. int cached_values;
  24. unsigned long is_out;
  25. struct device *dev;
  26. struct gpio_chip chip;
  27. };
  28. struct egpio_info {
  29. spinlock_t lock;
  30. /* iomem info */
  31. void __iomem *base_addr;
  32. int bus_shift; /* byte shift */
  33. int reg_shift; /* bit shift */
  34. int reg_mask;
  35. /* irq info */
  36. int ack_register;
  37. int ack_write;
  38. u16 irqs_enabled;
  39. uint irq_start;
  40. int nirqs;
  41. uint chained_irq;
  42. /* egpio info */
  43. struct egpio_chip *chip;
  44. int nchips;
  45. };
  46. static inline void egpio_writew(u16 value, struct egpio_info *ei, int reg)
  47. {
  48. writew(value, ei->base_addr + (reg << ei->bus_shift));
  49. }
  50. static inline u16 egpio_readw(struct egpio_info *ei, int reg)
  51. {
  52. return readw(ei->base_addr + (reg << ei->bus_shift));
  53. }
  54. /*
  55. * IRQs
  56. */
  57. static inline void ack_irqs(struct egpio_info *ei)
  58. {
  59. egpio_writew(ei->ack_write, ei, ei->ack_register);
  60. pr_debug("EGPIO ack - write %x to base+%x\n",
  61. ei->ack_write, ei->ack_register << ei->bus_shift);
  62. }
  63. static void egpio_ack(struct irq_data *data)
  64. {
  65. }
  66. /* There does not appear to be a way to proactively mask interrupts
  67. * on the egpio chip itself. So, we simply ignore interrupts that
  68. * aren't desired. */
  69. static void egpio_mask(struct irq_data *data)
  70. {
  71. struct egpio_info *ei = irq_data_get_irq_chip_data(data);
  72. ei->irqs_enabled &= ~(1 << (data->irq - ei->irq_start));
  73. pr_debug("EGPIO mask %d %04x\n", data->irq, ei->irqs_enabled);
  74. }
  75. static void egpio_unmask(struct irq_data *data)
  76. {
  77. struct egpio_info *ei = irq_data_get_irq_chip_data(data);
  78. ei->irqs_enabled |= 1 << (data->irq - ei->irq_start);
  79. pr_debug("EGPIO unmask %d %04x\n", data->irq, ei->irqs_enabled);
  80. }
  81. static struct irq_chip egpio_muxed_chip = {
  82. .name = "htc-egpio",
  83. .irq_ack = egpio_ack,
  84. .irq_mask = egpio_mask,
  85. .irq_unmask = egpio_unmask,
  86. };
  87. static void egpio_handler(struct irq_desc *desc)
  88. {
  89. struct egpio_info *ei = irq_desc_get_handler_data(desc);
  90. int irqpin;
  91. /* Read current pins. */
  92. unsigned long readval = egpio_readw(ei, ei->ack_register);
  93. pr_debug("IRQ reg: %x\n", (unsigned int)readval);
  94. /* Ack/unmask interrupts. */
  95. ack_irqs(ei);
  96. /* Process all set pins. */
  97. readval &= ei->irqs_enabled;
  98. for_each_set_bit(irqpin, &readval, ei->nirqs) {
  99. /* Run irq handler */
  100. pr_debug("got IRQ %d\n", irqpin);
  101. generic_handle_irq(ei->irq_start + irqpin);
  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 = gpiochip_get_data(chip);
  136. ei = dev_get_drvdata(egpio->dev);
  137. bit = egpio_bit(ei, offset);
  138. reg = egpio->reg_start + egpio_pos(ei, offset);
  139. if (test_bit(offset, &egpio->is_out)) {
  140. return !!(egpio->cached_values & (1 << offset));
  141. } else {
  142. value = egpio_readw(ei, reg);
  143. pr_debug("readw(%p + %x) = %x\n",
  144. ei->base_addr, reg << ei->bus_shift, value);
  145. return !!(value & bit);
  146. }
  147. }
  148. static int egpio_direction_input(struct gpio_chip *chip, unsigned offset)
  149. {
  150. struct egpio_chip *egpio;
  151. egpio = gpiochip_get_data(chip);
  152. return test_bit(offset, &egpio->is_out) ? -EINVAL : 0;
  153. }
  154. /*
  155. * Output pins
  156. */
  157. static void egpio_set(struct gpio_chip *chip, unsigned offset, int value)
  158. {
  159. unsigned long flag;
  160. struct egpio_chip *egpio;
  161. struct egpio_info *ei;
  162. int pos;
  163. int reg;
  164. int shift;
  165. pr_debug("egpio_set(%s, %d(%d), %d)\n",
  166. chip->label, offset, offset+chip->base, value);
  167. egpio = gpiochip_get_data(chip);
  168. ei = dev_get_drvdata(egpio->dev);
  169. pos = egpio_pos(ei, offset);
  170. reg = egpio->reg_start + pos;
  171. shift = pos << ei->reg_shift;
  172. pr_debug("egpio %s: reg %d = 0x%04x\n", value ? "set" : "clear",
  173. reg, (egpio->cached_values >> shift) & ei->reg_mask);
  174. spin_lock_irqsave(&ei->lock, flag);
  175. if (value)
  176. egpio->cached_values |= (1 << offset);
  177. else
  178. egpio->cached_values &= ~(1 << offset);
  179. egpio_writew((egpio->cached_values >> shift) & ei->reg_mask, ei, reg);
  180. spin_unlock_irqrestore(&ei->lock, flag);
  181. }
  182. static int egpio_direction_output(struct gpio_chip *chip,
  183. unsigned offset, int value)
  184. {
  185. struct egpio_chip *egpio;
  186. egpio = gpiochip_get_data(chip);
  187. if (test_bit(offset, &egpio->is_out)) {
  188. egpio_set(chip, offset, value);
  189. return 0;
  190. } else {
  191. return -EINVAL;
  192. }
  193. }
  194. static int egpio_get_direction(struct gpio_chip *chip, unsigned offset)
  195. {
  196. struct egpio_chip *egpio;
  197. egpio = gpiochip_get_data(chip);
  198. return !test_bit(offset, &egpio->is_out);
  199. }
  200. static void egpio_write_cache(struct egpio_info *ei)
  201. {
  202. int i;
  203. struct egpio_chip *egpio;
  204. int shift;
  205. for (i = 0; i < ei->nchips; i++) {
  206. egpio = &(ei->chip[i]);
  207. if (!egpio->is_out)
  208. continue;
  209. for (shift = 0; shift < egpio->chip.ngpio;
  210. shift += (1<<ei->reg_shift)) {
  211. int reg = egpio->reg_start + egpio_pos(ei, shift);
  212. if (!((egpio->is_out >> shift) & ei->reg_mask))
  213. continue;
  214. pr_debug("EGPIO: setting %x to %x, was %x\n", reg,
  215. (egpio->cached_values >> shift) & ei->reg_mask,
  216. egpio_readw(ei, reg));
  217. egpio_writew((egpio->cached_values >> shift)
  218. & ei->reg_mask, ei, reg);
  219. }
  220. }
  221. }
  222. /*
  223. * Setup
  224. */
  225. static int __init egpio_probe(struct platform_device *pdev)
  226. {
  227. struct htc_egpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  228. struct resource *res;
  229. struct egpio_info *ei;
  230. struct gpio_chip *chip;
  231. unsigned int irq, irq_end;
  232. int i;
  233. int ret;
  234. /* Initialize ei data structure. */
  235. ei = devm_kzalloc(&pdev->dev, sizeof(*ei), GFP_KERNEL);
  236. if (!ei)
  237. return -ENOMEM;
  238. spin_lock_init(&ei->lock);
  239. /* Find chained irq */
  240. ret = -EINVAL;
  241. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  242. if (res)
  243. ei->chained_irq = res->start;
  244. /* Map egpio chip into virtual address space. */
  245. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  246. if (!res)
  247. goto fail;
  248. ei->base_addr = devm_ioremap_nocache(&pdev->dev, res->start,
  249. resource_size(res));
  250. if (!ei->base_addr)
  251. goto fail;
  252. pr_debug("EGPIO phys=%08x virt=%p\n", (u32)res->start, ei->base_addr);
  253. if ((pdata->bus_width != 16) && (pdata->bus_width != 32))
  254. goto fail;
  255. ei->bus_shift = fls(pdata->bus_width - 1) - 3;
  256. pr_debug("bus_shift = %d\n", ei->bus_shift);
  257. if ((pdata->reg_width != 8) && (pdata->reg_width != 16))
  258. goto fail;
  259. ei->reg_shift = fls(pdata->reg_width - 1);
  260. pr_debug("reg_shift = %d\n", ei->reg_shift);
  261. ei->reg_mask = (1 << pdata->reg_width) - 1;
  262. platform_set_drvdata(pdev, ei);
  263. ei->nchips = pdata->num_chips;
  264. ei->chip = devm_kcalloc(&pdev->dev,
  265. ei->nchips, sizeof(struct egpio_chip),
  266. GFP_KERNEL);
  267. if (!ei->chip) {
  268. ret = -ENOMEM;
  269. goto fail;
  270. }
  271. for (i = 0; i < ei->nchips; i++) {
  272. ei->chip[i].reg_start = pdata->chip[i].reg_start;
  273. ei->chip[i].cached_values = pdata->chip[i].initial_values;
  274. ei->chip[i].is_out = pdata->chip[i].direction;
  275. ei->chip[i].dev = &(pdev->dev);
  276. chip = &(ei->chip[i].chip);
  277. chip->label = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  278. "htc-egpio-%d",
  279. i);
  280. if (!chip->label) {
  281. ret = -ENOMEM;
  282. goto fail;
  283. }
  284. chip->parent = &pdev->dev;
  285. chip->owner = THIS_MODULE;
  286. chip->get = egpio_get;
  287. chip->set = egpio_set;
  288. chip->direction_input = egpio_direction_input;
  289. chip->direction_output = egpio_direction_output;
  290. chip->get_direction = egpio_get_direction;
  291. chip->base = pdata->chip[i].gpio_base;
  292. chip->ngpio = pdata->chip[i].num_gpios;
  293. gpiochip_add_data(chip, &ei->chip[i]);
  294. }
  295. /* Set initial pin values */
  296. egpio_write_cache(ei);
  297. ei->irq_start = pdata->irq_base;
  298. ei->nirqs = pdata->num_irqs;
  299. ei->ack_register = pdata->ack_register;
  300. if (ei->chained_irq) {
  301. /* Setup irq handlers */
  302. ei->ack_write = 0xFFFF;
  303. if (pdata->invert_acks)
  304. ei->ack_write = 0;
  305. irq_end = ei->irq_start + ei->nirqs;
  306. for (irq = ei->irq_start; irq < irq_end; irq++) {
  307. irq_set_chip_and_handler(irq, &egpio_muxed_chip,
  308. handle_simple_irq);
  309. irq_set_chip_data(irq, ei);
  310. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  311. }
  312. irq_set_irq_type(ei->chained_irq, IRQ_TYPE_EDGE_RISING);
  313. irq_set_chained_handler_and_data(ei->chained_irq,
  314. egpio_handler, ei);
  315. ack_irqs(ei);
  316. device_init_wakeup(&pdev->dev, 1);
  317. }
  318. return 0;
  319. fail:
  320. printk(KERN_ERR "EGPIO failed to setup\n");
  321. return ret;
  322. }
  323. #ifdef CONFIG_PM
  324. static int egpio_suspend(struct platform_device *pdev, pm_message_t state)
  325. {
  326. struct egpio_info *ei = platform_get_drvdata(pdev);
  327. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  328. enable_irq_wake(ei->chained_irq);
  329. return 0;
  330. }
  331. static int egpio_resume(struct platform_device *pdev)
  332. {
  333. struct egpio_info *ei = platform_get_drvdata(pdev);
  334. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  335. disable_irq_wake(ei->chained_irq);
  336. /* Update registers from the cache, in case
  337. the CPLD was powered off during suspend */
  338. egpio_write_cache(ei);
  339. return 0;
  340. }
  341. #else
  342. #define egpio_suspend NULL
  343. #define egpio_resume NULL
  344. #endif
  345. static struct platform_driver egpio_driver = {
  346. .driver = {
  347. .name = "htc-egpio",
  348. .suppress_bind_attrs = true,
  349. },
  350. .suspend = egpio_suspend,
  351. .resume = egpio_resume,
  352. };
  353. static int __init egpio_init(void)
  354. {
  355. return platform_driver_probe(&egpio_driver, egpio_probe);
  356. }
  357. /* start early for dependencies */
  358. subsys_initcall(egpio_init);