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. /* Register accesses into the PDC MMIO area */
  48. static inline void pdc_write(struct tz1090_pdc_gpio *priv, unsigned int reg_offs,
  49. unsigned int data)
  50. {
  51. writel(data, priv->reg + reg_offs);
  52. }
  53. static inline unsigned int pdc_read(struct tz1090_pdc_gpio *priv,
  54. unsigned int reg_offs)
  55. {
  56. return readl(priv->reg + reg_offs);
  57. }
  58. /* Generic GPIO interface */
  59. static int tz1090_pdc_gpio_direction_input(struct gpio_chip *chip,
  60. unsigned int offset)
  61. {
  62. struct tz1090_pdc_gpio *priv = gpiochip_get_data(chip);
  63. u32 value;
  64. int lstat;
  65. __global_lock2(lstat);
  66. value = pdc_read(priv, REG_SOC_GPIO_CONTROL1);
  67. value |= BIT(offset);
  68. pdc_write(priv, REG_SOC_GPIO_CONTROL1, value);
  69. __global_unlock2(lstat);
  70. return 0;
  71. }
  72. static int tz1090_pdc_gpio_direction_output(struct gpio_chip *chip,
  73. unsigned int offset,
  74. int output_value)
  75. {
  76. struct tz1090_pdc_gpio *priv = gpiochip_get_data(chip);
  77. u32 value;
  78. int lstat;
  79. __global_lock2(lstat);
  80. /* EXT_POWER doesn't seem to have an output value bit */
  81. if (offset < 6) {
  82. value = pdc_read(priv, REG_SOC_GPIO_CONTROL0);
  83. if (output_value)
  84. value |= BIT(offset);
  85. else
  86. value &= ~BIT(offset);
  87. pdc_write(priv, REG_SOC_GPIO_CONTROL0, value);
  88. }
  89. value = pdc_read(priv, REG_SOC_GPIO_CONTROL1);
  90. value &= ~BIT(offset);
  91. pdc_write(priv, REG_SOC_GPIO_CONTROL1, value);
  92. __global_unlock2(lstat);
  93. return 0;
  94. }
  95. static int tz1090_pdc_gpio_get(struct gpio_chip *chip, unsigned int offset)
  96. {
  97. struct tz1090_pdc_gpio *priv = gpiochip_get_data(chip);
  98. return !!(pdc_read(priv, REG_SOC_GPIO_STATUS) & BIT(offset));
  99. }
  100. static void tz1090_pdc_gpio_set(struct gpio_chip *chip, unsigned int offset,
  101. int output_value)
  102. {
  103. struct tz1090_pdc_gpio *priv = gpiochip_get_data(chip);
  104. u32 value;
  105. int lstat;
  106. /* EXT_POWER doesn't seem to have an output value bit */
  107. if (offset >= 6)
  108. return;
  109. __global_lock2(lstat);
  110. value = pdc_read(priv, REG_SOC_GPIO_CONTROL0);
  111. if (output_value)
  112. value |= BIT(offset);
  113. else
  114. value &= ~BIT(offset);
  115. pdc_write(priv, REG_SOC_GPIO_CONTROL0, value);
  116. __global_unlock2(lstat);
  117. }
  118. static int tz1090_pdc_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
  119. {
  120. struct tz1090_pdc_gpio *priv = gpiochip_get_data(chip);
  121. unsigned int syswake = offset - GPIO_PDC_IRQ_FIRST;
  122. int irq;
  123. /* only syswakes have irqs */
  124. if (syswake >= GPIO_PDC_NIRQ)
  125. return -EINVAL;
  126. irq = priv->irq[syswake];
  127. if (!irq)
  128. return -EINVAL;
  129. return irq;
  130. }
  131. static int tz1090_pdc_gpio_probe(struct platform_device *pdev)
  132. {
  133. struct device_node *np = pdev->dev.of_node;
  134. struct resource *res_regs;
  135. struct tz1090_pdc_gpio *priv;
  136. unsigned int i;
  137. if (!np) {
  138. dev_err(&pdev->dev, "must be instantiated via devicetree\n");
  139. return -ENOENT;
  140. }
  141. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  142. if (!res_regs) {
  143. dev_err(&pdev->dev, "cannot find registers resource\n");
  144. return -ENOENT;
  145. }
  146. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  147. if (!priv) {
  148. dev_err(&pdev->dev, "unable to allocate driver data\n");
  149. return -ENOMEM;
  150. }
  151. /* Ioremap the registers */
  152. priv->reg = devm_ioremap(&pdev->dev, res_regs->start,
  153. resource_size(res_regs));
  154. if (!priv->reg) {
  155. dev_err(&pdev->dev, "unable to ioremap registers\n");
  156. return -ENOMEM;
  157. }
  158. /* Set up GPIO chip */
  159. priv->chip.label = "tz1090-pdc-gpio";
  160. priv->chip.parent = &pdev->dev;
  161. priv->chip.direction_input = tz1090_pdc_gpio_direction_input;
  162. priv->chip.direction_output = tz1090_pdc_gpio_direction_output;
  163. priv->chip.get = tz1090_pdc_gpio_get;
  164. priv->chip.set = tz1090_pdc_gpio_set;
  165. priv->chip.free = gpiochip_generic_free;
  166. priv->chip.request = gpiochip_generic_request;
  167. priv->chip.to_irq = tz1090_pdc_gpio_to_irq;
  168. priv->chip.of_node = np;
  169. /* GPIO numbering */
  170. priv->chip.base = GPIO_PDC_BASE;
  171. priv->chip.ngpio = GPIO_PDC_NGPIO;
  172. /* Map the syswake irqs */
  173. for (i = 0; i < GPIO_PDC_NIRQ; ++i)
  174. priv->irq[i] = irq_of_parse_and_map(np, i);
  175. /* Add the GPIO bank */
  176. gpiochip_add_data(&priv->chip, priv);
  177. return 0;
  178. }
  179. static struct of_device_id tz1090_pdc_gpio_of_match[] = {
  180. { .compatible = "img,tz1090-pdc-gpio" },
  181. { },
  182. };
  183. static struct platform_driver tz1090_pdc_gpio_driver = {
  184. .driver = {
  185. .name = "tz1090-pdc-gpio",
  186. .of_match_table = tz1090_pdc_gpio_of_match,
  187. },
  188. .probe = tz1090_pdc_gpio_probe,
  189. };
  190. static int __init tz1090_pdc_gpio_init(void)
  191. {
  192. return platform_driver_register(&tz1090_pdc_gpio_driver);
  193. }
  194. subsys_initcall(tz1090_pdc_gpio_init);