gpio-zx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 2015 Linaro Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/pinctrl/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #define ZX_GPIO_DIR 0x00
  21. #define ZX_GPIO_IVE 0x04
  22. #define ZX_GPIO_IV 0x08
  23. #define ZX_GPIO_IEP 0x0C
  24. #define ZX_GPIO_IEN 0x10
  25. #define ZX_GPIO_DI 0x14
  26. #define ZX_GPIO_DO1 0x18
  27. #define ZX_GPIO_DO0 0x1C
  28. #define ZX_GPIO_DO 0x20
  29. #define ZX_GPIO_IM 0x28
  30. #define ZX_GPIO_IE 0x2C
  31. #define ZX_GPIO_MIS 0x30
  32. #define ZX_GPIO_IC 0x34
  33. #define ZX_GPIO_NR 16
  34. struct zx_gpio {
  35. spinlock_t lock;
  36. void __iomem *base;
  37. struct gpio_chip gc;
  38. };
  39. static int zx_direction_input(struct gpio_chip *gc, unsigned offset)
  40. {
  41. struct zx_gpio *chip = gpiochip_get_data(gc);
  42. unsigned long flags;
  43. u16 gpiodir;
  44. if (offset >= gc->ngpio)
  45. return -EINVAL;
  46. spin_lock_irqsave(&chip->lock, flags);
  47. gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
  48. gpiodir &= ~BIT(offset);
  49. writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
  50. spin_unlock_irqrestore(&chip->lock, flags);
  51. return 0;
  52. }
  53. static int zx_direction_output(struct gpio_chip *gc, unsigned offset,
  54. int value)
  55. {
  56. struct zx_gpio *chip = gpiochip_get_data(gc);
  57. unsigned long flags;
  58. u16 gpiodir;
  59. if (offset >= gc->ngpio)
  60. return -EINVAL;
  61. spin_lock_irqsave(&chip->lock, flags);
  62. gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
  63. gpiodir |= BIT(offset);
  64. writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
  65. if (value)
  66. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
  67. else
  68. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
  69. spin_unlock_irqrestore(&chip->lock, flags);
  70. return 0;
  71. }
  72. static int zx_get_value(struct gpio_chip *gc, unsigned offset)
  73. {
  74. struct zx_gpio *chip = gpiochip_get_data(gc);
  75. return !!(readw_relaxed(chip->base + ZX_GPIO_DI) & BIT(offset));
  76. }
  77. static void zx_set_value(struct gpio_chip *gc, unsigned offset, int value)
  78. {
  79. struct zx_gpio *chip = gpiochip_get_data(gc);
  80. if (value)
  81. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
  82. else
  83. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
  84. }
  85. static int zx_irq_type(struct irq_data *d, unsigned trigger)
  86. {
  87. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  88. struct zx_gpio *chip = gpiochip_get_data(gc);
  89. int offset = irqd_to_hwirq(d);
  90. unsigned long flags;
  91. u16 gpiois, gpioi_epos, gpioi_eneg, gpioiev;
  92. u16 bit = BIT(offset);
  93. if (offset < 0 || offset >= ZX_GPIO_NR)
  94. return -EINVAL;
  95. spin_lock_irqsave(&chip->lock, flags);
  96. gpioiev = readw_relaxed(chip->base + ZX_GPIO_IV);
  97. gpiois = readw_relaxed(chip->base + ZX_GPIO_IVE);
  98. gpioi_epos = readw_relaxed(chip->base + ZX_GPIO_IEP);
  99. gpioi_eneg = readw_relaxed(chip->base + ZX_GPIO_IEN);
  100. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  101. gpiois |= bit;
  102. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  103. gpioiev |= bit;
  104. else
  105. gpioiev &= ~bit;
  106. } else
  107. gpiois &= ~bit;
  108. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  109. gpioi_epos |= bit;
  110. gpioi_eneg |= bit;
  111. } else {
  112. if (trigger & IRQ_TYPE_EDGE_RISING) {
  113. gpioi_epos |= bit;
  114. gpioi_eneg &= ~bit;
  115. } else if (trigger & IRQ_TYPE_EDGE_FALLING) {
  116. gpioi_eneg |= bit;
  117. gpioi_epos &= ~bit;
  118. }
  119. }
  120. writew_relaxed(gpiois, chip->base + ZX_GPIO_IVE);
  121. writew_relaxed(gpioi_epos, chip->base + ZX_GPIO_IEP);
  122. writew_relaxed(gpioi_eneg, chip->base + ZX_GPIO_IEN);
  123. writew_relaxed(gpioiev, chip->base + ZX_GPIO_IV);
  124. spin_unlock_irqrestore(&chip->lock, flags);
  125. return 0;
  126. }
  127. static void zx_irq_handler(struct irq_desc *desc)
  128. {
  129. unsigned long pending;
  130. int offset;
  131. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  132. struct zx_gpio *chip = gpiochip_get_data(gc);
  133. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  134. chained_irq_enter(irqchip, desc);
  135. pending = readw_relaxed(chip->base + ZX_GPIO_MIS);
  136. writew_relaxed(pending, chip->base + ZX_GPIO_IC);
  137. if (pending) {
  138. for_each_set_bit(offset, &pending, ZX_GPIO_NR)
  139. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  140. offset));
  141. }
  142. chained_irq_exit(irqchip, desc);
  143. }
  144. static void zx_irq_mask(struct irq_data *d)
  145. {
  146. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  147. struct zx_gpio *chip = gpiochip_get_data(gc);
  148. u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
  149. u16 gpioie;
  150. spin_lock(&chip->lock);
  151. gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) | mask;
  152. writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
  153. gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) & ~mask;
  154. writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
  155. spin_unlock(&chip->lock);
  156. }
  157. static void zx_irq_unmask(struct irq_data *d)
  158. {
  159. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  160. struct zx_gpio *chip = gpiochip_get_data(gc);
  161. u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
  162. u16 gpioie;
  163. spin_lock(&chip->lock);
  164. gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) & ~mask;
  165. writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
  166. gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) | mask;
  167. writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
  168. spin_unlock(&chip->lock);
  169. }
  170. static struct irq_chip zx_irqchip = {
  171. .name = "zx-gpio",
  172. .irq_mask = zx_irq_mask,
  173. .irq_unmask = zx_irq_unmask,
  174. .irq_set_type = zx_irq_type,
  175. };
  176. static int zx_gpio_probe(struct platform_device *pdev)
  177. {
  178. struct device *dev = &pdev->dev;
  179. struct zx_gpio *chip;
  180. struct resource *res;
  181. int irq, id, ret;
  182. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  183. if (!chip)
  184. return -ENOMEM;
  185. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  186. chip->base = devm_ioremap_resource(dev, res);
  187. if (IS_ERR(chip->base))
  188. return PTR_ERR(chip->base);
  189. spin_lock_init(&chip->lock);
  190. if (of_property_read_bool(dev->of_node, "gpio-ranges")) {
  191. chip->gc.request = gpiochip_generic_request;
  192. chip->gc.free = gpiochip_generic_free;
  193. }
  194. id = of_alias_get_id(dev->of_node, "gpio");
  195. chip->gc.direction_input = zx_direction_input;
  196. chip->gc.direction_output = zx_direction_output;
  197. chip->gc.get = zx_get_value;
  198. chip->gc.set = zx_set_value;
  199. chip->gc.base = ZX_GPIO_NR * id;
  200. chip->gc.ngpio = ZX_GPIO_NR;
  201. chip->gc.label = dev_name(dev);
  202. chip->gc.parent = dev;
  203. chip->gc.owner = THIS_MODULE;
  204. ret = gpiochip_add_data(&chip->gc, chip);
  205. if (ret)
  206. return ret;
  207. /*
  208. * irq_chip support
  209. */
  210. writew_relaxed(0xffff, chip->base + ZX_GPIO_IM);
  211. writew_relaxed(0, chip->base + ZX_GPIO_IE);
  212. irq = platform_get_irq(pdev, 0);
  213. if (irq < 0) {
  214. dev_err(dev, "invalid IRQ\n");
  215. gpiochip_remove(&chip->gc);
  216. return -ENODEV;
  217. }
  218. ret = gpiochip_irqchip_add(&chip->gc, &zx_irqchip,
  219. 0, handle_simple_irq,
  220. IRQ_TYPE_NONE);
  221. if (ret) {
  222. dev_err(dev, "could not add irqchip\n");
  223. gpiochip_remove(&chip->gc);
  224. return ret;
  225. }
  226. gpiochip_set_chained_irqchip(&chip->gc, &zx_irqchip,
  227. irq, zx_irq_handler);
  228. platform_set_drvdata(pdev, chip);
  229. dev_info(dev, "ZX GPIO chip registered\n");
  230. return 0;
  231. }
  232. static const struct of_device_id zx_gpio_match[] = {
  233. {
  234. .compatible = "zte,zx296702-gpio",
  235. },
  236. { },
  237. };
  238. MODULE_DEVICE_TABLE(of, zx_gpio_match);
  239. static struct platform_driver zx_gpio_driver = {
  240. .probe = zx_gpio_probe,
  241. .driver = {
  242. .name = "zx_gpio",
  243. .of_match_table = of_match_ptr(zx_gpio_match),
  244. },
  245. };
  246. module_platform_driver(zx_gpio_driver)
  247. MODULE_AUTHOR("Jun Nie <jun.nie@linaro.org>");
  248. MODULE_DESCRIPTION("ZTE ZX296702 GPIO driver");
  249. MODULE_LICENSE("GPL");