gpio-tz1090-pdc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Toumaz Xenif TZ1090 PDC GPIO handling.
  3. *
  4. * Copyright (C) 2012-2013 Imagination Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/gpio.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/syscore_ops.h>
  19. #include <asm/global_lock.h>
  20. /* Register offsets from SOC_GPIO_CONTROL0 */
  21. #define REG_SOC_GPIO_CONTROL0 0x00
  22. #define REG_SOC_GPIO_CONTROL1 0x04
  23. #define REG_SOC_GPIO_CONTROL2 0x08
  24. #define REG_SOC_GPIO_CONTROL3 0x0c
  25. #define REG_SOC_GPIO_STATUS 0x80
  26. /* PDC GPIOs go after normal GPIOs */
  27. #define GPIO_PDC_BASE 90
  28. #define GPIO_PDC_NGPIO 7
  29. /* Out of PDC gpios, only syswakes have irqs */
  30. #define GPIO_PDC_IRQ_FIRST 2
  31. #define GPIO_PDC_NIRQ 3
  32. /**
  33. * struct tz1090_pdc_gpio - GPIO bank private data
  34. * @chip: Generic GPIO chip for GPIO bank
  35. * @reg: Base of registers, offset for this GPIO bank
  36. * @irq: IRQ numbers for Syswake GPIOs
  37. *
  38. * This is the main private data for the PDC GPIO driver. It encapsulates a
  39. * gpio_chip, and the callbacks for the gpio_chip can access the private data
  40. * with the to_pdc() macro below.
  41. */
  42. struct tz1090_pdc_gpio {
  43. struct gpio_chip chip;
  44. void __iomem *reg;
  45. int irq[GPIO_PDC_NIRQ];
  46. };
  47. #define to_pdc(c) container_of(c, struct tz1090_pdc_gpio, chip)
  48. /* Register accesses into the PDC MMIO area */
  49. static inline void pdc_write(struct tz1090_pdc_gpio *priv, unsigned int reg_offs,
  50. unsigned int data)
  51. {
  52. writel(data, priv->reg + reg_offs);
  53. }
  54. static inline unsigned int pdc_read(struct tz1090_pdc_gpio *priv,
  55. unsigned int reg_offs)
  56. {
  57. return readl(priv->reg + reg_offs);
  58. }
  59. /* Generic GPIO interface */
  60. static int tz1090_pdc_gpio_direction_input(struct gpio_chip *chip,
  61. unsigned int offset)
  62. {
  63. struct tz1090_pdc_gpio *priv = to_pdc(chip);
  64. u32 value;
  65. int lstat;
  66. __global_lock2(lstat);
  67. value = pdc_read(priv, REG_SOC_GPIO_CONTROL1);
  68. value |= BIT(offset);
  69. pdc_write(priv, REG_SOC_GPIO_CONTROL1, value);
  70. __global_unlock2(lstat);
  71. return 0;
  72. }
  73. static int tz1090_pdc_gpio_direction_output(struct gpio_chip *chip,
  74. unsigned int offset,
  75. int output_value)
  76. {
  77. struct tz1090_pdc_gpio *priv = to_pdc(chip);
  78. u32 value;
  79. int lstat;
  80. __global_lock2(lstat);
  81. /* EXT_POWER doesn't seem to have an output value bit */
  82. if (offset < 6) {
  83. value = pdc_read(priv, REG_SOC_GPIO_CONTROL0);
  84. if (output_value)
  85. value |= BIT(offset);
  86. else
  87. value &= ~BIT(offset);
  88. pdc_write(priv, REG_SOC_GPIO_CONTROL0, value);
  89. }
  90. value = pdc_read(priv, REG_SOC_GPIO_CONTROL1);
  91. value &= ~BIT(offset);
  92. pdc_write(priv, REG_SOC_GPIO_CONTROL1, value);
  93. __global_unlock2(lstat);
  94. return 0;
  95. }
  96. static int tz1090_pdc_gpio_get(struct gpio_chip *chip, unsigned int offset)
  97. {
  98. struct tz1090_pdc_gpio *priv = to_pdc(chip);
  99. return pdc_read(priv, REG_SOC_GPIO_STATUS) & BIT(offset);
  100. }
  101. static void tz1090_pdc_gpio_set(struct gpio_chip *chip, unsigned int offset,
  102. int output_value)
  103. {
  104. struct tz1090_pdc_gpio *priv = to_pdc(chip);
  105. u32 value;
  106. int lstat;
  107. /* EXT_POWER doesn't seem to have an output value bit */
  108. if (offset >= 6)
  109. return;
  110. __global_lock2(lstat);
  111. value = pdc_read(priv, REG_SOC_GPIO_CONTROL0);
  112. if (output_value)
  113. value |= BIT(offset);
  114. else
  115. value &= ~BIT(offset);
  116. pdc_write(priv, REG_SOC_GPIO_CONTROL0, value);
  117. __global_unlock2(lstat);
  118. }
  119. static int tz1090_pdc_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
  120. {
  121. struct tz1090_pdc_gpio *priv = to_pdc(chip);
  122. unsigned int syswake = offset - GPIO_PDC_IRQ_FIRST;
  123. int irq;
  124. /* only syswakes have irqs */
  125. if (syswake >= GPIO_PDC_NIRQ)
  126. return -EINVAL;
  127. irq = priv->irq[syswake];
  128. if (!irq)
  129. return -EINVAL;
  130. return irq;
  131. }
  132. static int tz1090_pdc_gpio_probe(struct platform_device *pdev)
  133. {
  134. struct device_node *np = pdev->dev.of_node;
  135. struct resource *res_regs;
  136. struct tz1090_pdc_gpio *priv;
  137. unsigned int i;
  138. if (!np) {
  139. dev_err(&pdev->dev, "must be instantiated via devicetree\n");
  140. return -ENOENT;
  141. }
  142. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  143. if (!res_regs) {
  144. dev_err(&pdev->dev, "cannot find registers resource\n");
  145. return -ENOENT;
  146. }
  147. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  148. if (!priv) {
  149. dev_err(&pdev->dev, "unable to allocate driver data\n");
  150. return -ENOMEM;
  151. }
  152. /* Ioremap the registers */
  153. priv->reg = devm_ioremap(&pdev->dev, res_regs->start,
  154. resource_size(res_regs));
  155. if (!priv->reg) {
  156. dev_err(&pdev->dev, "unable to ioremap registers\n");
  157. return -ENOMEM;
  158. }
  159. /* Set up GPIO chip */
  160. priv->chip.label = "tz1090-pdc-gpio";
  161. priv->chip.dev = &pdev->dev;
  162. priv->chip.direction_input = tz1090_pdc_gpio_direction_input;
  163. priv->chip.direction_output = tz1090_pdc_gpio_direction_output;
  164. priv->chip.get = tz1090_pdc_gpio_get;
  165. priv->chip.set = tz1090_pdc_gpio_set;
  166. priv->chip.free = gpiochip_generic_free;
  167. priv->chip.request = gpiochip_generic_request;
  168. priv->chip.to_irq = tz1090_pdc_gpio_to_irq;
  169. priv->chip.of_node = np;
  170. /* GPIO numbering */
  171. priv->chip.base = GPIO_PDC_BASE;
  172. priv->chip.ngpio = GPIO_PDC_NGPIO;
  173. /* Map the syswake irqs */
  174. for (i = 0; i < GPIO_PDC_NIRQ; ++i)
  175. priv->irq[i] = irq_of_parse_and_map(np, i);
  176. /* Add the GPIO bank */
  177. gpiochip_add(&priv->chip);
  178. return 0;
  179. }
  180. static struct of_device_id tz1090_pdc_gpio_of_match[] = {
  181. { .compatible = "img,tz1090-pdc-gpio" },
  182. { },
  183. };
  184. static struct platform_driver tz1090_pdc_gpio_driver = {
  185. .driver = {
  186. .name = "tz1090-pdc-gpio",
  187. .of_match_table = tz1090_pdc_gpio_of_match,
  188. },
  189. .probe = tz1090_pdc_gpio_probe,
  190. };
  191. static int __init tz1090_pdc_gpio_init(void)
  192. {
  193. return platform_driver_register(&tz1090_pdc_gpio_driver);
  194. }
  195. subsys_initcall(tz1090_pdc_gpio_init);