gpio-xlp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_enable(struct irq_data *d)
  81. {
  82. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  83. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  84. }
  85. static void xlp_gpio_irq_disable(struct irq_data *d)
  86. {
  87. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  88. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  89. unsigned long flags;
  90. spin_lock_irqsave(&priv->lock, flags);
  91. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  92. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  93. spin_unlock_irqrestore(&priv->lock, flags);
  94. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  95. }
  96. static void xlp_gpio_irq_mask_ack(struct irq_data *d)
  97. {
  98. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  99. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  100. unsigned long flags;
  101. spin_lock_irqsave(&priv->lock, flags);
  102. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  103. xlp_gpio_set_reg(priv->gpio_intr_stat, d->hwirq, 0x1);
  104. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  105. spin_unlock_irqrestore(&priv->lock, flags);
  106. }
  107. static void xlp_gpio_irq_unmask(struct irq_data *d)
  108. {
  109. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  110. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  111. unsigned long flags;
  112. spin_lock_irqsave(&priv->lock, flags);
  113. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x1);
  114. __set_bit(d->hwirq, priv->gpio_enabled_mask);
  115. spin_unlock_irqrestore(&priv->lock, flags);
  116. }
  117. static int xlp_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  118. {
  119. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  120. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  121. int pol, irq_type;
  122. switch (type) {
  123. case IRQ_TYPE_EDGE_RISING:
  124. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  125. pol = XLP_GPIO_IRQ_POL_HIGH;
  126. break;
  127. case IRQ_TYPE_EDGE_FALLING:
  128. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  129. pol = XLP_GPIO_IRQ_POL_LOW;
  130. break;
  131. case IRQ_TYPE_LEVEL_HIGH:
  132. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  133. pol = XLP_GPIO_IRQ_POL_HIGH;
  134. break;
  135. case IRQ_TYPE_LEVEL_LOW:
  136. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  137. pol = XLP_GPIO_IRQ_POL_LOW;
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. xlp_gpio_set_reg(priv->gpio_intr_type, d->hwirq, irq_type);
  143. xlp_gpio_set_reg(priv->gpio_intr_pol, d->hwirq, pol);
  144. return 0;
  145. }
  146. static struct irq_chip xlp_gpio_irq_chip = {
  147. .name = "XLP-GPIO",
  148. .irq_mask_ack = xlp_gpio_irq_mask_ack,
  149. .irq_enable = xlp_gpio_irq_enable,
  150. .irq_disable = xlp_gpio_irq_disable,
  151. .irq_set_type = xlp_gpio_set_irq_type,
  152. .irq_unmask = xlp_gpio_irq_unmask,
  153. .flags = IRQCHIP_ONESHOT_SAFE | IRQCHIP_IMMUTABLE,
  154. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  155. };
  156. static void xlp_gpio_generic_handler(struct irq_desc *desc)
  157. {
  158. struct xlp_gpio_priv *priv = irq_desc_get_handler_data(desc);
  159. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  160. int gpio, regoff;
  161. u32 gpio_stat;
  162. regoff = -1;
  163. gpio_stat = 0;
  164. chained_irq_enter(irqchip, desc);
  165. for_each_set_bit(gpio, priv->gpio_enabled_mask, XLP_MAX_NR_GPIO) {
  166. if (regoff != gpio / XLP_GPIO_REGSZ) {
  167. regoff = gpio / XLP_GPIO_REGSZ;
  168. gpio_stat = readl(priv->gpio_intr_stat + regoff * 4);
  169. }
  170. if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
  171. generic_handle_domain_irq(priv->chip.irq.domain, gpio);
  172. }
  173. chained_irq_exit(irqchip, desc);
  174. }
  175. static int xlp_gpio_dir_output(struct gpio_chip *gc, unsigned gpio, int state)
  176. {
  177. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  178. BUG_ON(gpio >= gc->ngpio);
  179. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x1);
  180. return 0;
  181. }
  182. static int xlp_gpio_dir_input(struct gpio_chip *gc, unsigned gpio)
  183. {
  184. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  185. BUG_ON(gpio >= gc->ngpio);
  186. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x0);
  187. return 0;
  188. }
  189. static int xlp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  190. {
  191. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  192. BUG_ON(gpio >= gc->ngpio);
  193. return xlp_gpio_get_reg(priv->gpio_paddrv, gpio);
  194. }
  195. static void xlp_gpio_set(struct gpio_chip *gc, unsigned gpio, int state)
  196. {
  197. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  198. BUG_ON(gpio >= gc->ngpio);
  199. xlp_gpio_set_reg(priv->gpio_paddrv, gpio, state);
  200. }
  201. static int xlp_gpio_probe(struct platform_device *pdev)
  202. {
  203. struct gpio_chip *gc;
  204. struct gpio_irq_chip *girq;
  205. struct xlp_gpio_priv *priv;
  206. void __iomem *gpio_base;
  207. int irq, err;
  208. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  209. if (!priv)
  210. return -ENOMEM;
  211. gpio_base = devm_platform_ioremap_resource(pdev, 0);
  212. if (IS_ERR(gpio_base))
  213. return PTR_ERR(gpio_base);
  214. irq = platform_get_irq(pdev, 0);
  215. if (irq < 0)
  216. return irq;
  217. priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
  218. priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
  219. priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
  220. priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
  221. priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
  222. priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
  223. bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
  224. gc = &priv->chip;
  225. gc->owner = THIS_MODULE;
  226. gc->label = dev_name(&pdev->dev);
  227. gc->base = 0;
  228. gc->parent = &pdev->dev;
  229. gc->ngpio = 70;
  230. gc->direction_output = xlp_gpio_dir_output;
  231. gc->direction_input = xlp_gpio_dir_input;
  232. gc->set = xlp_gpio_set;
  233. gc->get = xlp_gpio_get;
  234. spin_lock_init(&priv->lock);
  235. girq = &gc->irq;
  236. gpio_irq_chip_set_chip(girq, &xlp_gpio_irq_chip);
  237. girq->parent_handler = xlp_gpio_generic_handler;
  238. girq->num_parents = 1;
  239. girq->parents = devm_kcalloc(&pdev->dev, 1,
  240. sizeof(*girq->parents),
  241. GFP_KERNEL);
  242. if (!girq->parents)
  243. return -ENOMEM;
  244. girq->parents[0] = irq;
  245. girq->first = 0;
  246. girq->default_type = IRQ_TYPE_NONE;
  247. girq->handler = handle_level_irq;
  248. err = gpiochip_add_data(gc, priv);
  249. if (err < 0)
  250. return err;
  251. dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
  252. return 0;
  253. }
  254. #ifdef CONFIG_ACPI
  255. static const struct acpi_device_id xlp_gpio_acpi_match[] = {
  256. { "BRCM9006" },
  257. { "CAV9006" },
  258. {},
  259. };
  260. MODULE_DEVICE_TABLE(acpi, xlp_gpio_acpi_match);
  261. #endif
  262. static struct platform_driver xlp_gpio_driver = {
  263. .driver = {
  264. .name = "xlp-gpio",
  265. .acpi_match_table = ACPI_PTR(xlp_gpio_acpi_match),
  266. },
  267. .probe = xlp_gpio_probe,
  268. };
  269. module_platform_driver(xlp_gpio_driver);
  270. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  271. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@broadcom.com>");
  272. MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
  273. MODULE_LICENSE("GPL v2");