gpio-xlp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2003-2015 Broadcom Corporation
  4. * All Rights Reserved
  5. */
  6. #include <linux/gpio/driver.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/module.h>
  9. #include <linux/irq.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irqchip/chained_irq.h>
  12. #include <linux/acpi.h>
  13. /*
  14. * XLP GPIO has multiple 32 bit registers for each feature where each register
  15. * controls 32 pins. So, pins up to 64 require 2 32-bit registers and up to 96
  16. * require 3 32-bit registers for each feature.
  17. * Here we only define offset of the first register for each feature. Offset of
  18. * the registers for pins greater than 32 can be calculated as following(Use
  19. * GPIO_INT_STAT as example):
  20. *
  21. * offset = (gpio / XLP_GPIO_REGSZ) * 4;
  22. * reg_addr = addr + offset;
  23. *
  24. * where addr is base address of the that feature register and gpio is the pin.
  25. */
  26. #define GPIO_9XX_BYTESWAP 0X00
  27. #define GPIO_9XX_CTRL 0X04
  28. #define GPIO_9XX_OUTPUT_EN 0x14
  29. #define GPIO_9XX_PADDRV 0x24
  30. /*
  31. * Only for 4 interrupt enable reg are defined for now,
  32. * total reg available are 12.
  33. */
  34. #define GPIO_9XX_INT_EN00 0x44
  35. #define GPIO_9XX_INT_EN10 0x54
  36. #define GPIO_9XX_INT_EN20 0x64
  37. #define GPIO_9XX_INT_EN30 0x74
  38. #define GPIO_9XX_INT_POL 0x104
  39. #define GPIO_9XX_INT_TYPE 0x114
  40. #define GPIO_9XX_INT_STAT 0x124
  41. /* Interrupt type register mask */
  42. #define XLP_GPIO_IRQ_TYPE_LVL 0x0
  43. #define XLP_GPIO_IRQ_TYPE_EDGE 0x1
  44. /* Interrupt polarity register mask */
  45. #define XLP_GPIO_IRQ_POL_HIGH 0x0
  46. #define XLP_GPIO_IRQ_POL_LOW 0x1
  47. #define XLP_GPIO_REGSZ 32
  48. #define XLP_GPIO_IRQ_BASE 768
  49. #define XLP_MAX_NR_GPIO 96
  50. struct xlp_gpio_priv {
  51. struct gpio_chip chip;
  52. DECLARE_BITMAP(gpio_enabled_mask, XLP_MAX_NR_GPIO);
  53. void __iomem *gpio_intr_en; /* pointer to first intr enable reg */
  54. void __iomem *gpio_intr_stat; /* pointer to first intr status reg */
  55. void __iomem *gpio_intr_type; /* pointer to first intr type reg */
  56. void __iomem *gpio_intr_pol; /* pointer to first intr polarity reg */
  57. void __iomem *gpio_out_en; /* pointer to first output enable reg */
  58. void __iomem *gpio_paddrv; /* pointer to first pad drive reg */
  59. spinlock_t lock;
  60. };
  61. static int xlp_gpio_get_reg(void __iomem *addr, unsigned gpio)
  62. {
  63. u32 pos, regset;
  64. pos = gpio % XLP_GPIO_REGSZ;
  65. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  66. return !!(readl(addr + regset) & BIT(pos));
  67. }
  68. static void xlp_gpio_set_reg(void __iomem *addr, unsigned gpio, int state)
  69. {
  70. u32 value, pos, regset;
  71. pos = gpio % XLP_GPIO_REGSZ;
  72. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  73. value = readl(addr + regset);
  74. if (state)
  75. value |= BIT(pos);
  76. else
  77. value &= ~BIT(pos);
  78. writel(value, addr + regset);
  79. }
  80. static void xlp_gpio_irq_disable(struct irq_data *d)
  81. {
  82. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  83. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  84. unsigned long flags;
  85. spin_lock_irqsave(&priv->lock, flags);
  86. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  87. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  88. spin_unlock_irqrestore(&priv->lock, flags);
  89. }
  90. static void xlp_gpio_irq_mask_ack(struct irq_data *d)
  91. {
  92. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  93. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  94. unsigned long flags;
  95. spin_lock_irqsave(&priv->lock, flags);
  96. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  97. xlp_gpio_set_reg(priv->gpio_intr_stat, d->hwirq, 0x1);
  98. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  99. spin_unlock_irqrestore(&priv->lock, flags);
  100. }
  101. static void xlp_gpio_irq_unmask(struct irq_data *d)
  102. {
  103. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  104. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  105. unsigned long flags;
  106. spin_lock_irqsave(&priv->lock, flags);
  107. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x1);
  108. __set_bit(d->hwirq, priv->gpio_enabled_mask);
  109. spin_unlock_irqrestore(&priv->lock, flags);
  110. }
  111. static int xlp_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  112. {
  113. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  114. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  115. int pol, irq_type;
  116. switch (type) {
  117. case IRQ_TYPE_EDGE_RISING:
  118. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  119. pol = XLP_GPIO_IRQ_POL_HIGH;
  120. break;
  121. case IRQ_TYPE_EDGE_FALLING:
  122. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  123. pol = XLP_GPIO_IRQ_POL_LOW;
  124. break;
  125. case IRQ_TYPE_LEVEL_HIGH:
  126. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  127. pol = XLP_GPIO_IRQ_POL_HIGH;
  128. break;
  129. case IRQ_TYPE_LEVEL_LOW:
  130. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  131. pol = XLP_GPIO_IRQ_POL_LOW;
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. xlp_gpio_set_reg(priv->gpio_intr_type, d->hwirq, irq_type);
  137. xlp_gpio_set_reg(priv->gpio_intr_pol, d->hwirq, pol);
  138. return 0;
  139. }
  140. static struct irq_chip xlp_gpio_irq_chip = {
  141. .name = "XLP-GPIO",
  142. .irq_mask_ack = xlp_gpio_irq_mask_ack,
  143. .irq_disable = xlp_gpio_irq_disable,
  144. .irq_set_type = xlp_gpio_set_irq_type,
  145. .irq_unmask = xlp_gpio_irq_unmask,
  146. .flags = IRQCHIP_ONESHOT_SAFE,
  147. };
  148. static void xlp_gpio_generic_handler(struct irq_desc *desc)
  149. {
  150. struct xlp_gpio_priv *priv = irq_desc_get_handler_data(desc);
  151. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  152. int gpio, regoff;
  153. u32 gpio_stat;
  154. regoff = -1;
  155. gpio_stat = 0;
  156. chained_irq_enter(irqchip, desc);
  157. for_each_set_bit(gpio, priv->gpio_enabled_mask, XLP_MAX_NR_GPIO) {
  158. if (regoff != gpio / XLP_GPIO_REGSZ) {
  159. regoff = gpio / XLP_GPIO_REGSZ;
  160. gpio_stat = readl(priv->gpio_intr_stat + regoff * 4);
  161. }
  162. if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
  163. generic_handle_domain_irq(priv->chip.irq.domain, gpio);
  164. }
  165. chained_irq_exit(irqchip, desc);
  166. }
  167. static int xlp_gpio_dir_output(struct gpio_chip *gc, unsigned gpio, int state)
  168. {
  169. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  170. BUG_ON(gpio >= gc->ngpio);
  171. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x1);
  172. return 0;
  173. }
  174. static int xlp_gpio_dir_input(struct gpio_chip *gc, unsigned gpio)
  175. {
  176. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  177. BUG_ON(gpio >= gc->ngpio);
  178. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x0);
  179. return 0;
  180. }
  181. static int xlp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  182. {
  183. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  184. BUG_ON(gpio >= gc->ngpio);
  185. return xlp_gpio_get_reg(priv->gpio_paddrv, gpio);
  186. }
  187. static void xlp_gpio_set(struct gpio_chip *gc, unsigned gpio, int state)
  188. {
  189. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  190. BUG_ON(gpio >= gc->ngpio);
  191. xlp_gpio_set_reg(priv->gpio_paddrv, gpio, state);
  192. }
  193. static int xlp_gpio_probe(struct platform_device *pdev)
  194. {
  195. struct gpio_chip *gc;
  196. struct gpio_irq_chip *girq;
  197. struct xlp_gpio_priv *priv;
  198. void __iomem *gpio_base;
  199. int irq, err;
  200. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  201. if (!priv)
  202. return -ENOMEM;
  203. gpio_base = devm_platform_ioremap_resource(pdev, 0);
  204. if (IS_ERR(gpio_base))
  205. return PTR_ERR(gpio_base);
  206. irq = platform_get_irq(pdev, 0);
  207. if (irq < 0)
  208. return irq;
  209. priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
  210. priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
  211. priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
  212. priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
  213. priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
  214. priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
  215. bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
  216. gc = &priv->chip;
  217. gc->owner = THIS_MODULE;
  218. gc->label = dev_name(&pdev->dev);
  219. gc->base = 0;
  220. gc->parent = &pdev->dev;
  221. gc->ngpio = 70;
  222. gc->direction_output = xlp_gpio_dir_output;
  223. gc->direction_input = xlp_gpio_dir_input;
  224. gc->set = xlp_gpio_set;
  225. gc->get = xlp_gpio_get;
  226. spin_lock_init(&priv->lock);
  227. girq = &gc->irq;
  228. girq->chip = &xlp_gpio_irq_chip;
  229. girq->parent_handler = xlp_gpio_generic_handler;
  230. girq->num_parents = 1;
  231. girq->parents = devm_kcalloc(&pdev->dev, 1,
  232. sizeof(*girq->parents),
  233. GFP_KERNEL);
  234. if (!girq->parents)
  235. return -ENOMEM;
  236. girq->parents[0] = irq;
  237. girq->first = 0;
  238. girq->default_type = IRQ_TYPE_NONE;
  239. girq->handler = handle_level_irq;
  240. err = gpiochip_add_data(gc, priv);
  241. if (err < 0)
  242. return err;
  243. dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
  244. return 0;
  245. }
  246. #ifdef CONFIG_ACPI
  247. static const struct acpi_device_id xlp_gpio_acpi_match[] = {
  248. { "BRCM9006" },
  249. { "CAV9006" },
  250. {},
  251. };
  252. MODULE_DEVICE_TABLE(acpi, xlp_gpio_acpi_match);
  253. #endif
  254. static struct platform_driver xlp_gpio_driver = {
  255. .driver = {
  256. .name = "xlp-gpio",
  257. .acpi_match_table = ACPI_PTR(xlp_gpio_acpi_match),
  258. },
  259. .probe = xlp_gpio_probe,
  260. };
  261. module_platform_driver(xlp_gpio_driver);
  262. MODULE_AUTHOR("Kamlakant Patel <[email protected]>");
  263. MODULE_AUTHOR("Ganesan Ramalingam <[email protected]>");
  264. MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
  265. MODULE_LICENSE("GPL v2");