gpio-zx.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. bool uses_pinctrl;
  39. };
  40. static inline struct zx_gpio *to_zx(struct gpio_chip *gc)
  41. {
  42. return container_of(gc, struct zx_gpio, gc);
  43. }
  44. static int zx_gpio_request(struct gpio_chip *gc, unsigned offset)
  45. {
  46. struct zx_gpio *chip = to_zx(gc);
  47. int gpio = gc->base + offset;
  48. if (chip->uses_pinctrl)
  49. return pinctrl_request_gpio(gpio);
  50. return 0;
  51. }
  52. static void zx_gpio_free(struct gpio_chip *gc, unsigned offset)
  53. {
  54. struct zx_gpio *chip = to_zx(gc);
  55. int gpio = gc->base + offset;
  56. if (chip->uses_pinctrl)
  57. pinctrl_free_gpio(gpio);
  58. }
  59. static int zx_direction_input(struct gpio_chip *gc, unsigned offset)
  60. {
  61. struct zx_gpio *chip = to_zx(gc);
  62. unsigned long flags;
  63. u16 gpiodir;
  64. if (offset >= gc->ngpio)
  65. return -EINVAL;
  66. spin_lock_irqsave(&chip->lock, flags);
  67. gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
  68. gpiodir &= ~BIT(offset);
  69. writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
  70. spin_unlock_irqrestore(&chip->lock, flags);
  71. return 0;
  72. }
  73. static int zx_direction_output(struct gpio_chip *gc, unsigned offset,
  74. int value)
  75. {
  76. struct zx_gpio *chip = to_zx(gc);
  77. unsigned long flags;
  78. u16 gpiodir;
  79. if (offset >= gc->ngpio)
  80. return -EINVAL;
  81. spin_lock_irqsave(&chip->lock, flags);
  82. gpiodir = readw_relaxed(chip->base + ZX_GPIO_DIR);
  83. gpiodir |= BIT(offset);
  84. writew_relaxed(gpiodir, chip->base + ZX_GPIO_DIR);
  85. if (value)
  86. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
  87. else
  88. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
  89. spin_unlock_irqrestore(&chip->lock, flags);
  90. return 0;
  91. }
  92. static int zx_get_value(struct gpio_chip *gc, unsigned offset)
  93. {
  94. struct zx_gpio *chip = to_zx(gc);
  95. return !!(readw_relaxed(chip->base + ZX_GPIO_DI) & BIT(offset));
  96. }
  97. static void zx_set_value(struct gpio_chip *gc, unsigned offset, int value)
  98. {
  99. struct zx_gpio *chip = to_zx(gc);
  100. if (value)
  101. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO1);
  102. else
  103. writew_relaxed(BIT(offset), chip->base + ZX_GPIO_DO0);
  104. }
  105. static int zx_irq_type(struct irq_data *d, unsigned trigger)
  106. {
  107. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  108. struct zx_gpio *chip = to_zx(gc);
  109. int offset = irqd_to_hwirq(d);
  110. unsigned long flags;
  111. u16 gpiois, gpioi_epos, gpioi_eneg, gpioiev;
  112. u16 bit = BIT(offset);
  113. if (offset < 0 || offset >= ZX_GPIO_NR)
  114. return -EINVAL;
  115. spin_lock_irqsave(&chip->lock, flags);
  116. gpioiev = readw_relaxed(chip->base + ZX_GPIO_IV);
  117. gpiois = readw_relaxed(chip->base + ZX_GPIO_IVE);
  118. gpioi_epos = readw_relaxed(chip->base + ZX_GPIO_IEP);
  119. gpioi_eneg = readw_relaxed(chip->base + ZX_GPIO_IEN);
  120. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  121. gpiois |= bit;
  122. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  123. gpioiev |= bit;
  124. else
  125. gpioiev &= ~bit;
  126. } else
  127. gpiois &= ~bit;
  128. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  129. gpioi_epos |= bit;
  130. gpioi_eneg |= bit;
  131. } else {
  132. if (trigger & IRQ_TYPE_EDGE_RISING) {
  133. gpioi_epos |= bit;
  134. gpioi_eneg &= ~bit;
  135. } else if (trigger & IRQ_TYPE_EDGE_FALLING) {
  136. gpioi_eneg |= bit;
  137. gpioi_epos &= ~bit;
  138. }
  139. }
  140. writew_relaxed(gpiois, chip->base + ZX_GPIO_IVE);
  141. writew_relaxed(gpioi_epos, chip->base + ZX_GPIO_IEP);
  142. writew_relaxed(gpioi_eneg, chip->base + ZX_GPIO_IEN);
  143. writew_relaxed(gpioiev, chip->base + ZX_GPIO_IV);
  144. spin_unlock_irqrestore(&chip->lock, flags);
  145. return 0;
  146. }
  147. static void zx_irq_handler(struct irq_desc *desc)
  148. {
  149. unsigned long pending;
  150. int offset;
  151. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  152. struct zx_gpio *chip = to_zx(gc);
  153. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  154. chained_irq_enter(irqchip, desc);
  155. pending = readw_relaxed(chip->base + ZX_GPIO_MIS);
  156. writew_relaxed(pending, chip->base + ZX_GPIO_IC);
  157. if (pending) {
  158. for_each_set_bit(offset, &pending, ZX_GPIO_NR)
  159. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  160. offset));
  161. }
  162. chained_irq_exit(irqchip, desc);
  163. }
  164. static void zx_irq_mask(struct irq_data *d)
  165. {
  166. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  167. struct zx_gpio *chip = to_zx(gc);
  168. u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
  169. u16 gpioie;
  170. spin_lock(&chip->lock);
  171. gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) | mask;
  172. writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
  173. gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) & ~mask;
  174. writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
  175. spin_unlock(&chip->lock);
  176. }
  177. static void zx_irq_unmask(struct irq_data *d)
  178. {
  179. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  180. struct zx_gpio *chip = to_zx(gc);
  181. u16 mask = BIT(irqd_to_hwirq(d) % ZX_GPIO_NR);
  182. u16 gpioie;
  183. spin_lock(&chip->lock);
  184. gpioie = readw_relaxed(chip->base + ZX_GPIO_IM) & ~mask;
  185. writew_relaxed(gpioie, chip->base + ZX_GPIO_IM);
  186. gpioie = readw_relaxed(chip->base + ZX_GPIO_IE) | mask;
  187. writew_relaxed(gpioie, chip->base + ZX_GPIO_IE);
  188. spin_unlock(&chip->lock);
  189. }
  190. static struct irq_chip zx_irqchip = {
  191. .name = "zx-gpio",
  192. .irq_mask = zx_irq_mask,
  193. .irq_unmask = zx_irq_unmask,
  194. .irq_set_type = zx_irq_type,
  195. };
  196. static int zx_gpio_probe(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. struct zx_gpio *chip;
  200. struct resource *res;
  201. int irq, id, ret;
  202. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  203. if (!chip)
  204. return -ENOMEM;
  205. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  206. chip->base = devm_ioremap_resource(dev, res);
  207. if (IS_ERR(chip->base))
  208. return PTR_ERR(chip->base);
  209. spin_lock_init(&chip->lock);
  210. if (of_property_read_bool(dev->of_node, "gpio-ranges"))
  211. chip->uses_pinctrl = true;
  212. id = of_alias_get_id(dev->of_node, "gpio");
  213. chip->gc.request = zx_gpio_request;
  214. chip->gc.free = zx_gpio_free;
  215. chip->gc.direction_input = zx_direction_input;
  216. chip->gc.direction_output = zx_direction_output;
  217. chip->gc.get = zx_get_value;
  218. chip->gc.set = zx_set_value;
  219. chip->gc.base = ZX_GPIO_NR * id;
  220. chip->gc.ngpio = ZX_GPIO_NR;
  221. chip->gc.label = dev_name(dev);
  222. chip->gc.dev = dev;
  223. chip->gc.owner = THIS_MODULE;
  224. ret = gpiochip_add(&chip->gc);
  225. if (ret)
  226. return ret;
  227. /*
  228. * irq_chip support
  229. */
  230. writew_relaxed(0xffff, chip->base + ZX_GPIO_IM);
  231. writew_relaxed(0, chip->base + ZX_GPIO_IE);
  232. irq = platform_get_irq(pdev, 0);
  233. if (irq < 0) {
  234. dev_err(dev, "invalid IRQ\n");
  235. gpiochip_remove(&chip->gc);
  236. return -ENODEV;
  237. }
  238. ret = gpiochip_irqchip_add(&chip->gc, &zx_irqchip,
  239. 0, handle_simple_irq,
  240. IRQ_TYPE_NONE);
  241. if (ret) {
  242. dev_err(dev, "could not add irqchip\n");
  243. gpiochip_remove(&chip->gc);
  244. return ret;
  245. }
  246. gpiochip_set_chained_irqchip(&chip->gc, &zx_irqchip,
  247. irq, zx_irq_handler);
  248. platform_set_drvdata(pdev, chip);
  249. dev_info(dev, "ZX GPIO chip registered\n");
  250. return 0;
  251. }
  252. static const struct of_device_id zx_gpio_match[] = {
  253. {
  254. .compatible = "zte,zx296702-gpio",
  255. },
  256. { },
  257. };
  258. MODULE_DEVICE_TABLE(of, zx_gpio_match);
  259. static struct platform_driver zx_gpio_driver = {
  260. .probe = zx_gpio_probe,
  261. .driver = {
  262. .name = "zx_gpio",
  263. .of_match_table = of_match_ptr(zx_gpio_match),
  264. },
  265. };
  266. module_platform_driver(zx_gpio_driver)
  267. MODULE_AUTHOR("Jun Nie <jun.nie@linaro.org>");
  268. MODULE_DESCRIPTION("ZTE ZX296702 GPIO driver");
  269. MODULE_LICENSE("GPL");