gpio-xlp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2003-2015 Broadcom Corporation
  3. * All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/gpio.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of_device.h>
  17. #include <linux/module.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. /*
  22. * XLP GPIO has multiple 32 bit registers for each feature where each register
  23. * controls 32 pins. So, pins up to 64 require 2 32-bit registers and up to 96
  24. * require 3 32-bit registers for each feature.
  25. * Here we only define offset of the first register for each feature. Offset of
  26. * the registers for pins greater than 32 can be calculated as following(Use
  27. * GPIO_INT_STAT as example):
  28. *
  29. * offset = (gpio / XLP_GPIO_REGSZ) * 4;
  30. * reg_addr = addr + offset;
  31. *
  32. * where addr is base address of the that feature register and gpio is the pin.
  33. */
  34. #define GPIO_OUTPUT_EN 0x00
  35. #define GPIO_PADDRV 0x08
  36. #define GPIO_INT_EN00 0x18
  37. #define GPIO_INT_EN10 0x20
  38. #define GPIO_INT_EN20 0x28
  39. #define GPIO_INT_EN30 0x30
  40. #define GPIO_INT_POL 0x38
  41. #define GPIO_INT_TYPE 0x40
  42. #define GPIO_INT_STAT 0x48
  43. #define GPIO_9XX_BYTESWAP 0X00
  44. #define GPIO_9XX_CTRL 0X04
  45. #define GPIO_9XX_OUTPUT_EN 0x14
  46. #define GPIO_9XX_PADDRV 0x24
  47. /*
  48. * Only for 4 interrupt enable reg are defined for now,
  49. * total reg available are 12.
  50. */
  51. #define GPIO_9XX_INT_EN00 0x44
  52. #define GPIO_9XX_INT_EN10 0x54
  53. #define GPIO_9XX_INT_EN20 0x64
  54. #define GPIO_9XX_INT_EN30 0x74
  55. #define GPIO_9XX_INT_POL 0x104
  56. #define GPIO_9XX_INT_TYPE 0x114
  57. #define GPIO_9XX_INT_STAT 0x124
  58. #define GPIO_3XX_INT_EN00 0x18
  59. #define GPIO_3XX_INT_EN10 0x20
  60. #define GPIO_3XX_INT_EN20 0x28
  61. #define GPIO_3XX_INT_EN30 0x30
  62. #define GPIO_3XX_INT_POL 0x78
  63. #define GPIO_3XX_INT_TYPE 0x80
  64. #define GPIO_3XX_INT_STAT 0x88
  65. /* Interrupt type register mask */
  66. #define XLP_GPIO_IRQ_TYPE_LVL 0x0
  67. #define XLP_GPIO_IRQ_TYPE_EDGE 0x1
  68. /* Interrupt polarity register mask */
  69. #define XLP_GPIO_IRQ_POL_HIGH 0x0
  70. #define XLP_GPIO_IRQ_POL_LOW 0x1
  71. #define XLP_GPIO_REGSZ 32
  72. #define XLP_GPIO_IRQ_BASE 768
  73. #define XLP_MAX_NR_GPIO 96
  74. /* XLP variants supported by this driver */
  75. enum {
  76. XLP_GPIO_VARIANT_XLP832 = 1,
  77. XLP_GPIO_VARIANT_XLP316,
  78. XLP_GPIO_VARIANT_XLP208,
  79. XLP_GPIO_VARIANT_XLP980,
  80. XLP_GPIO_VARIANT_XLP532
  81. };
  82. struct xlp_gpio_priv {
  83. struct gpio_chip chip;
  84. DECLARE_BITMAP(gpio_enabled_mask, XLP_MAX_NR_GPIO);
  85. void __iomem *gpio_intr_en; /* pointer to first intr enable reg */
  86. void __iomem *gpio_intr_stat; /* pointer to first intr status reg */
  87. void __iomem *gpio_intr_type; /* pointer to first intr type reg */
  88. void __iomem *gpio_intr_pol; /* pointer to first intr polarity reg */
  89. void __iomem *gpio_out_en; /* pointer to first output enable reg */
  90. void __iomem *gpio_paddrv; /* pointer to first pad drive reg */
  91. spinlock_t lock;
  92. };
  93. static int xlp_gpio_get_reg(void __iomem *addr, unsigned gpio)
  94. {
  95. u32 pos, regset;
  96. pos = gpio % XLP_GPIO_REGSZ;
  97. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  98. return !!(readl(addr + regset) & BIT(pos));
  99. }
  100. static void xlp_gpio_set_reg(void __iomem *addr, unsigned gpio, int state)
  101. {
  102. u32 value, pos, regset;
  103. pos = gpio % XLP_GPIO_REGSZ;
  104. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  105. value = readl(addr + regset);
  106. if (state)
  107. value |= BIT(pos);
  108. else
  109. value &= ~BIT(pos);
  110. writel(value, addr + regset);
  111. }
  112. static void xlp_gpio_irq_disable(struct irq_data *d)
  113. {
  114. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  115. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  116. unsigned long flags;
  117. spin_lock_irqsave(&priv->lock, flags);
  118. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  119. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  120. spin_unlock_irqrestore(&priv->lock, flags);
  121. }
  122. static void xlp_gpio_irq_mask_ack(struct irq_data *d)
  123. {
  124. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  125. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  126. unsigned long flags;
  127. spin_lock_irqsave(&priv->lock, flags);
  128. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  129. xlp_gpio_set_reg(priv->gpio_intr_stat, d->hwirq, 0x1);
  130. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  131. spin_unlock_irqrestore(&priv->lock, flags);
  132. }
  133. static void xlp_gpio_irq_unmask(struct irq_data *d)
  134. {
  135. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  136. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  137. unsigned long flags;
  138. spin_lock_irqsave(&priv->lock, flags);
  139. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x1);
  140. __set_bit(d->hwirq, priv->gpio_enabled_mask);
  141. spin_unlock_irqrestore(&priv->lock, flags);
  142. }
  143. static int xlp_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  144. {
  145. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  146. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  147. int pol, irq_type;
  148. switch (type) {
  149. case IRQ_TYPE_EDGE_RISING:
  150. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  151. pol = XLP_GPIO_IRQ_POL_HIGH;
  152. break;
  153. case IRQ_TYPE_EDGE_FALLING:
  154. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  155. pol = XLP_GPIO_IRQ_POL_LOW;
  156. break;
  157. case IRQ_TYPE_LEVEL_HIGH:
  158. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  159. pol = XLP_GPIO_IRQ_POL_HIGH;
  160. break;
  161. case IRQ_TYPE_LEVEL_LOW:
  162. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  163. pol = XLP_GPIO_IRQ_POL_LOW;
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. xlp_gpio_set_reg(priv->gpio_intr_type, d->hwirq, irq_type);
  169. xlp_gpio_set_reg(priv->gpio_intr_pol, d->hwirq, pol);
  170. return 0;
  171. }
  172. static struct irq_chip xlp_gpio_irq_chip = {
  173. .name = "XLP-GPIO",
  174. .irq_mask_ack = xlp_gpio_irq_mask_ack,
  175. .irq_disable = xlp_gpio_irq_disable,
  176. .irq_set_type = xlp_gpio_set_irq_type,
  177. .irq_unmask = xlp_gpio_irq_unmask,
  178. .flags = IRQCHIP_ONESHOT_SAFE,
  179. };
  180. static void xlp_gpio_generic_handler(struct irq_desc *desc)
  181. {
  182. struct xlp_gpio_priv *priv = irq_desc_get_handler_data(desc);
  183. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  184. int gpio, regoff;
  185. u32 gpio_stat;
  186. regoff = -1;
  187. gpio_stat = 0;
  188. chained_irq_enter(irqchip, desc);
  189. for_each_set_bit(gpio, priv->gpio_enabled_mask, XLP_MAX_NR_GPIO) {
  190. if (regoff != gpio / XLP_GPIO_REGSZ) {
  191. regoff = gpio / XLP_GPIO_REGSZ;
  192. gpio_stat = readl(priv->gpio_intr_stat + regoff * 4);
  193. }
  194. if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
  195. generic_handle_irq(irq_find_mapping(
  196. priv->chip.irqdomain, gpio));
  197. }
  198. chained_irq_exit(irqchip, desc);
  199. }
  200. static int xlp_gpio_dir_output(struct gpio_chip *gc, unsigned gpio, int state)
  201. {
  202. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  203. BUG_ON(gpio >= gc->ngpio);
  204. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x1);
  205. return 0;
  206. }
  207. static int xlp_gpio_dir_input(struct gpio_chip *gc, unsigned gpio)
  208. {
  209. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  210. BUG_ON(gpio >= gc->ngpio);
  211. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x0);
  212. return 0;
  213. }
  214. static int xlp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  215. {
  216. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  217. BUG_ON(gpio >= gc->ngpio);
  218. return xlp_gpio_get_reg(priv->gpio_paddrv, gpio);
  219. }
  220. static void xlp_gpio_set(struct gpio_chip *gc, unsigned gpio, int state)
  221. {
  222. struct xlp_gpio_priv *priv = gpiochip_get_data(gc);
  223. BUG_ON(gpio >= gc->ngpio);
  224. xlp_gpio_set_reg(priv->gpio_paddrv, gpio, state);
  225. }
  226. static const struct of_device_id xlp_gpio_of_ids[] = {
  227. {
  228. .compatible = "netlogic,xlp832-gpio",
  229. .data = (void *)XLP_GPIO_VARIANT_XLP832,
  230. },
  231. {
  232. .compatible = "netlogic,xlp316-gpio",
  233. .data = (void *)XLP_GPIO_VARIANT_XLP316,
  234. },
  235. {
  236. .compatible = "netlogic,xlp208-gpio",
  237. .data = (void *)XLP_GPIO_VARIANT_XLP208,
  238. },
  239. {
  240. .compatible = "netlogic,xlp980-gpio",
  241. .data = (void *)XLP_GPIO_VARIANT_XLP980,
  242. },
  243. {
  244. .compatible = "netlogic,xlp532-gpio",
  245. .data = (void *)XLP_GPIO_VARIANT_XLP532,
  246. },
  247. { /* sentinel */ },
  248. };
  249. MODULE_DEVICE_TABLE(of, xlp_gpio_of_ids);
  250. static int xlp_gpio_probe(struct platform_device *pdev)
  251. {
  252. struct gpio_chip *gc;
  253. struct resource *iores;
  254. struct xlp_gpio_priv *priv;
  255. const struct of_device_id *of_id;
  256. void __iomem *gpio_base;
  257. int irq_base, irq, err;
  258. int ngpio;
  259. u32 soc_type;
  260. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  261. if (!iores)
  262. return -ENODEV;
  263. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  264. if (!priv)
  265. return -ENOMEM;
  266. gpio_base = devm_ioremap_resource(&pdev->dev, iores);
  267. if (IS_ERR(gpio_base))
  268. return PTR_ERR(gpio_base);
  269. irq = platform_get_irq(pdev, 0);
  270. if (irq < 0)
  271. return irq;
  272. of_id = of_match_device(xlp_gpio_of_ids, &pdev->dev);
  273. if (!of_id) {
  274. dev_err(&pdev->dev, "Failed to get soc type!\n");
  275. return -ENODEV;
  276. }
  277. soc_type = (uintptr_t) of_id->data;
  278. switch (soc_type) {
  279. case XLP_GPIO_VARIANT_XLP832:
  280. priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
  281. priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
  282. priv->gpio_intr_stat = gpio_base + GPIO_INT_STAT;
  283. priv->gpio_intr_type = gpio_base + GPIO_INT_TYPE;
  284. priv->gpio_intr_pol = gpio_base + GPIO_INT_POL;
  285. priv->gpio_intr_en = gpio_base + GPIO_INT_EN00;
  286. ngpio = 41;
  287. break;
  288. case XLP_GPIO_VARIANT_XLP208:
  289. case XLP_GPIO_VARIANT_XLP316:
  290. priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
  291. priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
  292. priv->gpio_intr_stat = gpio_base + GPIO_3XX_INT_STAT;
  293. priv->gpio_intr_type = gpio_base + GPIO_3XX_INT_TYPE;
  294. priv->gpio_intr_pol = gpio_base + GPIO_3XX_INT_POL;
  295. priv->gpio_intr_en = gpio_base + GPIO_3XX_INT_EN00;
  296. ngpio = (soc_type == XLP_GPIO_VARIANT_XLP208) ? 42 : 57;
  297. break;
  298. case XLP_GPIO_VARIANT_XLP980:
  299. case XLP_GPIO_VARIANT_XLP532:
  300. priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
  301. priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
  302. priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
  303. priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
  304. priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
  305. priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
  306. ngpio = (soc_type == XLP_GPIO_VARIANT_XLP980) ? 66 : 67;
  307. break;
  308. default:
  309. dev_err(&pdev->dev, "Unknown Processor type!\n");
  310. return -ENODEV;
  311. }
  312. bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
  313. gc = &priv->chip;
  314. gc->owner = THIS_MODULE;
  315. gc->label = dev_name(&pdev->dev);
  316. gc->base = 0;
  317. gc->parent = &pdev->dev;
  318. gc->ngpio = ngpio;
  319. gc->of_node = pdev->dev.of_node;
  320. gc->direction_output = xlp_gpio_dir_output;
  321. gc->direction_input = xlp_gpio_dir_input;
  322. gc->set = xlp_gpio_set;
  323. gc->get = xlp_gpio_get;
  324. spin_lock_init(&priv->lock);
  325. irq_base = irq_alloc_descs(-1, XLP_GPIO_IRQ_BASE, gc->ngpio, 0);
  326. if (irq_base < 0) {
  327. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  328. return -ENODEV;
  329. }
  330. err = gpiochip_add_data(gc, priv);
  331. if (err < 0)
  332. goto out_free_desc;
  333. err = gpiochip_irqchip_add(gc, &xlp_gpio_irq_chip, irq_base,
  334. handle_level_irq, IRQ_TYPE_NONE);
  335. if (err) {
  336. dev_err(&pdev->dev, "Could not connect irqchip to gpiochip!\n");
  337. goto out_gpio_remove;
  338. }
  339. gpiochip_set_chained_irqchip(gc, &xlp_gpio_irq_chip, irq,
  340. xlp_gpio_generic_handler);
  341. dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
  342. return 0;
  343. out_gpio_remove:
  344. gpiochip_remove(gc);
  345. out_free_desc:
  346. irq_free_descs(irq_base, gc->ngpio);
  347. return err;
  348. }
  349. static struct platform_driver xlp_gpio_driver = {
  350. .driver = {
  351. .name = "xlp-gpio",
  352. .of_match_table = xlp_gpio_of_ids,
  353. },
  354. .probe = xlp_gpio_probe,
  355. };
  356. module_platform_driver(xlp_gpio_driver);
  357. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  358. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@broadcom.com>");
  359. MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
  360. MODULE_LICENSE("GPL v2");