gpio-zevio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * GPIO controller in LSI ZEVIO SoCs.
  3. *
  4. * Author: Fabian Vogt <fabian@ritter-vogt.de>
  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/spinlock.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/bitops.h>
  14. #include <linux/io.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/slab.h>
  18. #include <linux/gpio.h>
  19. /*
  20. * Memory layout:
  21. * This chip has four gpio sections, each controls 8 GPIOs.
  22. * Bit 0 in section 0 is GPIO 0, bit 2 in section 1 is GPIO 10.
  23. * Disclaimer: Reverse engineered!
  24. * For more information refer to:
  25. * http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#90000000_-_General_Purpose_I.2FO_.28GPIO.29
  26. *
  27. * 0x00-0x3F: Section 0
  28. * +0x00: Masked interrupt status (read-only)
  29. * +0x04: R: Interrupt status W: Reset interrupt status
  30. * +0x08: R: Interrupt mask W: Mask interrupt
  31. * +0x0C: W: Unmask interrupt (write-only)
  32. * +0x10: Direction: I/O=1/0
  33. * +0x14: Output
  34. * +0x18: Input (read-only)
  35. * +0x20: R: Level interrupt W: Set as level interrupt
  36. * 0x40-0x7F: Section 1
  37. * 0x80-0xBF: Section 2
  38. * 0xC0-0xFF: Section 3
  39. */
  40. #define ZEVIO_GPIO_SECTION_SIZE 0x40
  41. /* Offsets to various registers */
  42. #define ZEVIO_GPIO_INT_MASKED_STATUS 0x00
  43. #define ZEVIO_GPIO_INT_STATUS 0x04
  44. #define ZEVIO_GPIO_INT_UNMASK 0x08
  45. #define ZEVIO_GPIO_INT_MASK 0x0C
  46. #define ZEVIO_GPIO_DIRECTION 0x10
  47. #define ZEVIO_GPIO_OUTPUT 0x14
  48. #define ZEVIO_GPIO_INPUT 0x18
  49. #define ZEVIO_GPIO_INT_STICKY 0x20
  50. #define to_zevio_gpio(chip) container_of(to_of_mm_gpio_chip(chip), \
  51. struct zevio_gpio, chip)
  52. /* Bit number of GPIO in its section */
  53. #define ZEVIO_GPIO_BIT(gpio) (gpio&7)
  54. struct zevio_gpio {
  55. spinlock_t lock;
  56. struct of_mm_gpio_chip chip;
  57. };
  58. static inline u32 zevio_gpio_port_get(struct zevio_gpio *c, unsigned pin,
  59. unsigned port_offset)
  60. {
  61. unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE;
  62. return readl(IOMEM(c->chip.regs + section_offset + port_offset));
  63. }
  64. static inline void zevio_gpio_port_set(struct zevio_gpio *c, unsigned pin,
  65. unsigned port_offset, u32 val)
  66. {
  67. unsigned section_offset = ((pin >> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE;
  68. writel(val, IOMEM(c->chip.regs + section_offset + port_offset));
  69. }
  70. /* Functions for struct gpio_chip */
  71. static int zevio_gpio_get(struct gpio_chip *chip, unsigned pin)
  72. {
  73. struct zevio_gpio *controller = to_zevio_gpio(chip);
  74. u32 val, dir;
  75. spin_lock(&controller->lock);
  76. dir = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
  77. if (dir & BIT(ZEVIO_GPIO_BIT(pin)))
  78. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_INPUT);
  79. else
  80. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
  81. spin_unlock(&controller->lock);
  82. return (val >> ZEVIO_GPIO_BIT(pin)) & 0x1;
  83. }
  84. static void zevio_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  85. {
  86. struct zevio_gpio *controller = to_zevio_gpio(chip);
  87. u32 val;
  88. spin_lock(&controller->lock);
  89. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
  90. if (value)
  91. val |= BIT(ZEVIO_GPIO_BIT(pin));
  92. else
  93. val &= ~BIT(ZEVIO_GPIO_BIT(pin));
  94. zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val);
  95. spin_unlock(&controller->lock);
  96. }
  97. static int zevio_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  98. {
  99. struct zevio_gpio *controller = to_zevio_gpio(chip);
  100. u32 val;
  101. spin_lock(&controller->lock);
  102. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
  103. val |= BIT(ZEVIO_GPIO_BIT(pin));
  104. zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val);
  105. spin_unlock(&controller->lock);
  106. return 0;
  107. }
  108. static int zevio_gpio_direction_output(struct gpio_chip *chip,
  109. unsigned pin, int value)
  110. {
  111. struct zevio_gpio *controller = to_zevio_gpio(chip);
  112. u32 val;
  113. spin_lock(&controller->lock);
  114. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_OUTPUT);
  115. if (value)
  116. val |= BIT(ZEVIO_GPIO_BIT(pin));
  117. else
  118. val &= ~BIT(ZEVIO_GPIO_BIT(pin));
  119. zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_OUTPUT, val);
  120. val = zevio_gpio_port_get(controller, pin, ZEVIO_GPIO_DIRECTION);
  121. val &= ~BIT(ZEVIO_GPIO_BIT(pin));
  122. zevio_gpio_port_set(controller, pin, ZEVIO_GPIO_DIRECTION, val);
  123. spin_unlock(&controller->lock);
  124. return 0;
  125. }
  126. static int zevio_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  127. {
  128. /*
  129. * TODO: Implement IRQs.
  130. * Not implemented yet due to weird lockups
  131. */
  132. return -ENXIO;
  133. }
  134. static struct gpio_chip zevio_gpio_chip = {
  135. .direction_input = zevio_gpio_direction_input,
  136. .direction_output = zevio_gpio_direction_output,
  137. .set = zevio_gpio_set,
  138. .get = zevio_gpio_get,
  139. .to_irq = zevio_gpio_to_irq,
  140. .base = 0,
  141. .owner = THIS_MODULE,
  142. .ngpio = 32,
  143. .of_gpio_n_cells = 2,
  144. };
  145. /* Initialization */
  146. static int zevio_gpio_probe(struct platform_device *pdev)
  147. {
  148. struct zevio_gpio *controller;
  149. int status, i;
  150. controller = devm_kzalloc(&pdev->dev, sizeof(*controller), GFP_KERNEL);
  151. if (!controller)
  152. return -ENOMEM;
  153. platform_set_drvdata(pdev, controller);
  154. /* Copy our reference */
  155. controller->chip.gc = zevio_gpio_chip;
  156. controller->chip.gc.dev = &pdev->dev;
  157. status = of_mm_gpiochip_add(pdev->dev.of_node, &(controller->chip));
  158. if (status) {
  159. dev_err(&pdev->dev, "failed to add gpiochip: %d\n", status);
  160. return status;
  161. }
  162. spin_lock_init(&controller->lock);
  163. /* Disable interrupts, they only cause errors */
  164. for (i = 0; i < controller->chip.gc.ngpio; i += 8)
  165. zevio_gpio_port_set(controller, i, ZEVIO_GPIO_INT_MASK, 0xFF);
  166. dev_dbg(controller->chip.gc.dev, "ZEVIO GPIO controller set up!\n");
  167. return 0;
  168. }
  169. static int zevio_gpio_remove(struct platform_device *pdev)
  170. {
  171. struct zevio_gpio *controller = platform_get_drvdata(pdev);
  172. of_mm_gpiochip_remove(&controller->chip);
  173. return 0;
  174. }
  175. static const struct of_device_id zevio_gpio_of_match[] = {
  176. { .compatible = "lsi,zevio-gpio", },
  177. { },
  178. };
  179. MODULE_DEVICE_TABLE(of, zevio_gpio_of_match);
  180. static struct platform_driver zevio_gpio_driver = {
  181. .driver = {
  182. .name = "gpio-zevio",
  183. .of_match_table = zevio_gpio_of_match,
  184. },
  185. .probe = zevio_gpio_probe,
  186. .remove = zevio_gpio_remove,
  187. };
  188. module_platform_driver(zevio_gpio_driver);
  189. MODULE_LICENSE("GPL");
  190. MODULE_AUTHOR("Fabian Vogt <fabian@ritter-vogt.de>");
  191. MODULE_DESCRIPTION("LSI ZEVIO SoC GPIO driver");