gpio-htc-egpio.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. static inline int egpio_pos(struct egpio_info *ei, int bit)
  105. {
  106. return bit >> ei->reg_shift;
  107. }
  108. static inline int egpio_bit(struct egpio_info *ei, int bit)
  109. {
  110. return 1 << (bit & ((1 << ei->reg_shift)-1));
  111. }
  112. /*
  113. * Input pins
  114. */
  115. static int egpio_get(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct egpio_chip *egpio;
  118. struct egpio_info *ei;
  119. unsigned bit;
  120. int reg;
  121. int value;
  122. pr_debug("egpio_get_value(%d)\n", chip->base + offset);
  123. egpio = gpiochip_get_data(chip);
  124. ei = dev_get_drvdata(egpio->dev);
  125. bit = egpio_bit(ei, offset);
  126. reg = egpio->reg_start + egpio_pos(ei, offset);
  127. if (test_bit(offset, &egpio->is_out)) {
  128. return !!(egpio->cached_values & (1 << offset));
  129. } else {
  130. value = egpio_readw(ei, reg);
  131. pr_debug("readw(%p + %x) = %x\n",
  132. ei->base_addr, reg << ei->bus_shift, value);
  133. return !!(value & bit);
  134. }
  135. }
  136. static int egpio_direction_input(struct gpio_chip *chip, unsigned offset)
  137. {
  138. struct egpio_chip *egpio;
  139. egpio = gpiochip_get_data(chip);
  140. return test_bit(offset, &egpio->is_out) ? -EINVAL : 0;
  141. }
  142. /*
  143. * Output pins
  144. */
  145. static void egpio_set(struct gpio_chip *chip, unsigned offset, int value)
  146. {
  147. unsigned long flag;
  148. struct egpio_chip *egpio;
  149. struct egpio_info *ei;
  150. int pos;
  151. int reg;
  152. int shift;
  153. pr_debug("egpio_set(%s, %d(%d), %d)\n",
  154. chip->label, offset, offset+chip->base, value);
  155. egpio = gpiochip_get_data(chip);
  156. ei = dev_get_drvdata(egpio->dev);
  157. pos = egpio_pos(ei, offset);
  158. reg = egpio->reg_start + pos;
  159. shift = pos << ei->reg_shift;
  160. pr_debug("egpio %s: reg %d = 0x%04x\n", value ? "set" : "clear",
  161. reg, (egpio->cached_values >> shift) & ei->reg_mask);
  162. spin_lock_irqsave(&ei->lock, flag);
  163. if (value)
  164. egpio->cached_values |= (1 << offset);
  165. else
  166. egpio->cached_values &= ~(1 << offset);
  167. egpio_writew((egpio->cached_values >> shift) & ei->reg_mask, ei, reg);
  168. spin_unlock_irqrestore(&ei->lock, flag);
  169. }
  170. static int egpio_direction_output(struct gpio_chip *chip,
  171. unsigned offset, int value)
  172. {
  173. struct egpio_chip *egpio;
  174. egpio = gpiochip_get_data(chip);
  175. if (test_bit(offset, &egpio->is_out)) {
  176. egpio_set(chip, offset, value);
  177. return 0;
  178. } else {
  179. return -EINVAL;
  180. }
  181. }
  182. static int egpio_get_direction(struct gpio_chip *chip, unsigned offset)
  183. {
  184. struct egpio_chip *egpio;
  185. egpio = gpiochip_get_data(chip);
  186. if (test_bit(offset, &egpio->is_out))
  187. return GPIO_LINE_DIRECTION_OUT;
  188. return GPIO_LINE_DIRECTION_IN;
  189. }
  190. static void egpio_write_cache(struct egpio_info *ei)
  191. {
  192. int i;
  193. struct egpio_chip *egpio;
  194. int shift;
  195. for (i = 0; i < ei->nchips; i++) {
  196. egpio = &(ei->chip[i]);
  197. if (!egpio->is_out)
  198. continue;
  199. for (shift = 0; shift < egpio->chip.ngpio;
  200. shift += (1<<ei->reg_shift)) {
  201. int reg = egpio->reg_start + egpio_pos(ei, shift);
  202. if (!((egpio->is_out >> shift) & ei->reg_mask))
  203. continue;
  204. pr_debug("EGPIO: setting %x to %x, was %x\n", reg,
  205. (egpio->cached_values >> shift) & ei->reg_mask,
  206. egpio_readw(ei, reg));
  207. egpio_writew((egpio->cached_values >> shift)
  208. & ei->reg_mask, ei, reg);
  209. }
  210. }
  211. }
  212. /*
  213. * Setup
  214. */
  215. static int __init egpio_probe(struct platform_device *pdev)
  216. {
  217. struct htc_egpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  218. struct resource *res;
  219. struct egpio_info *ei;
  220. struct gpio_chip *chip;
  221. unsigned int irq, irq_end;
  222. int i;
  223. /* Initialize ei data structure. */
  224. ei = devm_kzalloc(&pdev->dev, sizeof(*ei), GFP_KERNEL);
  225. if (!ei)
  226. return -ENOMEM;
  227. spin_lock_init(&ei->lock);
  228. /* Find chained irq */
  229. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  230. if (res)
  231. ei->chained_irq = res->start;
  232. /* Map egpio chip into virtual address space. */
  233. ei->base_addr = devm_platform_ioremap_resource(pdev, 0);
  234. if (IS_ERR(ei->base_addr))
  235. return PTR_ERR(ei->base_addr);
  236. if ((pdata->bus_width != 16) && (pdata->bus_width != 32))
  237. return -EINVAL;
  238. ei->bus_shift = fls(pdata->bus_width - 1) - 3;
  239. pr_debug("bus_shift = %d\n", ei->bus_shift);
  240. if ((pdata->reg_width != 8) && (pdata->reg_width != 16))
  241. return -EINVAL;
  242. ei->reg_shift = fls(pdata->reg_width - 1);
  243. pr_debug("reg_shift = %d\n", ei->reg_shift);
  244. ei->reg_mask = (1 << pdata->reg_width) - 1;
  245. platform_set_drvdata(pdev, ei);
  246. ei->nchips = pdata->num_chips;
  247. ei->chip = devm_kcalloc(&pdev->dev,
  248. ei->nchips, sizeof(struct egpio_chip),
  249. GFP_KERNEL);
  250. if (!ei->chip)
  251. return -ENOMEM;
  252. for (i = 0; i < ei->nchips; i++) {
  253. ei->chip[i].reg_start = pdata->chip[i].reg_start;
  254. ei->chip[i].cached_values = pdata->chip[i].initial_values;
  255. ei->chip[i].is_out = pdata->chip[i].direction;
  256. ei->chip[i].dev = &(pdev->dev);
  257. chip = &(ei->chip[i].chip);
  258. chip->label = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  259. "htc-egpio-%d",
  260. i);
  261. if (!chip->label)
  262. return -ENOMEM;
  263. chip->parent = &pdev->dev;
  264. chip->owner = THIS_MODULE;
  265. chip->get = egpio_get;
  266. chip->set = egpio_set;
  267. chip->direction_input = egpio_direction_input;
  268. chip->direction_output = egpio_direction_output;
  269. chip->get_direction = egpio_get_direction;
  270. chip->base = pdata->chip[i].gpio_base;
  271. chip->ngpio = pdata->chip[i].num_gpios;
  272. gpiochip_add_data(chip, &ei->chip[i]);
  273. }
  274. /* Set initial pin values */
  275. egpio_write_cache(ei);
  276. ei->irq_start = pdata->irq_base;
  277. ei->nirqs = pdata->num_irqs;
  278. ei->ack_register = pdata->ack_register;
  279. if (ei->chained_irq) {
  280. /* Setup irq handlers */
  281. ei->ack_write = 0xFFFF;
  282. if (pdata->invert_acks)
  283. ei->ack_write = 0;
  284. irq_end = ei->irq_start + ei->nirqs;
  285. for (irq = ei->irq_start; irq < irq_end; irq++) {
  286. irq_set_chip_and_handler(irq, &egpio_muxed_chip,
  287. handle_simple_irq);
  288. irq_set_chip_data(irq, ei);
  289. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  290. }
  291. irq_set_irq_type(ei->chained_irq, IRQ_TYPE_EDGE_RISING);
  292. irq_set_chained_handler_and_data(ei->chained_irq,
  293. egpio_handler, ei);
  294. ack_irqs(ei);
  295. device_init_wakeup(&pdev->dev, 1);
  296. }
  297. return 0;
  298. }
  299. #ifdef CONFIG_PM
  300. static int egpio_suspend(struct platform_device *pdev, pm_message_t state)
  301. {
  302. struct egpio_info *ei = platform_get_drvdata(pdev);
  303. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  304. enable_irq_wake(ei->chained_irq);
  305. return 0;
  306. }
  307. static int egpio_resume(struct platform_device *pdev)
  308. {
  309. struct egpio_info *ei = platform_get_drvdata(pdev);
  310. if (ei->chained_irq && device_may_wakeup(&pdev->dev))
  311. disable_irq_wake(ei->chained_irq);
  312. /* Update registers from the cache, in case
  313. the CPLD was powered off during suspend */
  314. egpio_write_cache(ei);
  315. return 0;
  316. }
  317. #else
  318. #define egpio_suspend NULL
  319. #define egpio_resume NULL
  320. #endif
  321. static struct platform_driver egpio_driver = {
  322. .driver = {
  323. .name = "htc-egpio",
  324. .suppress_bind_attrs = true,
  325. },
  326. .suspend = egpio_suspend,
  327. .resume = egpio_resume,
  328. };
  329. static int __init egpio_init(void)
  330. {
  331. return platform_driver_probe(&egpio_driver, egpio_probe);
  332. }
  333. /* start early for dependencies */
  334. subsys_initcall(egpio_init);