gpio-xlp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 struct xlp_gpio_priv *gpio_chip_to_xlp_priv(struct gpio_chip *gc)
  94. {
  95. return container_of(gc, struct xlp_gpio_priv, chip);
  96. }
  97. static int xlp_gpio_get_reg(void __iomem *addr, unsigned gpio)
  98. {
  99. u32 pos, regset;
  100. pos = gpio % XLP_GPIO_REGSZ;
  101. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  102. return !!(readl(addr + regset) & BIT(pos));
  103. }
  104. static void xlp_gpio_set_reg(void __iomem *addr, unsigned gpio, int state)
  105. {
  106. u32 value, pos, regset;
  107. pos = gpio % XLP_GPIO_REGSZ;
  108. regset = (gpio / XLP_GPIO_REGSZ) * 4;
  109. value = readl(addr + regset);
  110. if (state)
  111. value |= BIT(pos);
  112. else
  113. value &= ~BIT(pos);
  114. writel(value, addr + regset);
  115. }
  116. static void xlp_gpio_irq_disable(struct irq_data *d)
  117. {
  118. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  119. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  120. unsigned long flags;
  121. spin_lock_irqsave(&priv->lock, flags);
  122. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  123. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  124. spin_unlock_irqrestore(&priv->lock, flags);
  125. }
  126. static void xlp_gpio_irq_mask_ack(struct irq_data *d)
  127. {
  128. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  129. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  130. unsigned long flags;
  131. spin_lock_irqsave(&priv->lock, flags);
  132. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x0);
  133. xlp_gpio_set_reg(priv->gpio_intr_stat, d->hwirq, 0x1);
  134. __clear_bit(d->hwirq, priv->gpio_enabled_mask);
  135. spin_unlock_irqrestore(&priv->lock, flags);
  136. }
  137. static void xlp_gpio_irq_unmask(struct irq_data *d)
  138. {
  139. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  140. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  141. unsigned long flags;
  142. spin_lock_irqsave(&priv->lock, flags);
  143. xlp_gpio_set_reg(priv->gpio_intr_en, d->hwirq, 0x1);
  144. __set_bit(d->hwirq, priv->gpio_enabled_mask);
  145. spin_unlock_irqrestore(&priv->lock, flags);
  146. }
  147. static int xlp_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  148. {
  149. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  150. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  151. int pol, irq_type;
  152. switch (type) {
  153. case IRQ_TYPE_EDGE_RISING:
  154. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  155. pol = XLP_GPIO_IRQ_POL_HIGH;
  156. break;
  157. case IRQ_TYPE_EDGE_FALLING:
  158. irq_type = XLP_GPIO_IRQ_TYPE_EDGE;
  159. pol = XLP_GPIO_IRQ_POL_LOW;
  160. break;
  161. case IRQ_TYPE_LEVEL_HIGH:
  162. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  163. pol = XLP_GPIO_IRQ_POL_HIGH;
  164. break;
  165. case IRQ_TYPE_LEVEL_LOW:
  166. irq_type = XLP_GPIO_IRQ_TYPE_LVL;
  167. pol = XLP_GPIO_IRQ_POL_LOW;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. xlp_gpio_set_reg(priv->gpio_intr_type, d->hwirq, irq_type);
  173. xlp_gpio_set_reg(priv->gpio_intr_pol, d->hwirq, pol);
  174. return 0;
  175. }
  176. static struct irq_chip xlp_gpio_irq_chip = {
  177. .name = "XLP-GPIO",
  178. .irq_mask_ack = xlp_gpio_irq_mask_ack,
  179. .irq_disable = xlp_gpio_irq_disable,
  180. .irq_set_type = xlp_gpio_set_irq_type,
  181. .irq_unmask = xlp_gpio_irq_unmask,
  182. .flags = IRQCHIP_ONESHOT_SAFE,
  183. };
  184. static void xlp_gpio_generic_handler(struct irq_desc *desc)
  185. {
  186. struct xlp_gpio_priv *priv = irq_desc_get_handler_data(desc);
  187. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  188. int gpio, regoff;
  189. u32 gpio_stat;
  190. regoff = -1;
  191. gpio_stat = 0;
  192. chained_irq_enter(irqchip, desc);
  193. for_each_set_bit(gpio, priv->gpio_enabled_mask, XLP_MAX_NR_GPIO) {
  194. if (regoff != gpio / XLP_GPIO_REGSZ) {
  195. regoff = gpio / XLP_GPIO_REGSZ;
  196. gpio_stat = readl(priv->gpio_intr_stat + regoff * 4);
  197. }
  198. if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
  199. generic_handle_irq(irq_find_mapping(
  200. priv->chip.irqdomain, gpio));
  201. }
  202. chained_irq_exit(irqchip, desc);
  203. }
  204. static int xlp_gpio_dir_output(struct gpio_chip *gc, unsigned gpio, int state)
  205. {
  206. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  207. BUG_ON(gpio >= gc->ngpio);
  208. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x1);
  209. return 0;
  210. }
  211. static int xlp_gpio_dir_input(struct gpio_chip *gc, unsigned gpio)
  212. {
  213. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  214. BUG_ON(gpio >= gc->ngpio);
  215. xlp_gpio_set_reg(priv->gpio_out_en, gpio, 0x0);
  216. return 0;
  217. }
  218. static int xlp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  219. {
  220. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  221. BUG_ON(gpio >= gc->ngpio);
  222. return xlp_gpio_get_reg(priv->gpio_paddrv, gpio);
  223. }
  224. static void xlp_gpio_set(struct gpio_chip *gc, unsigned gpio, int state)
  225. {
  226. struct xlp_gpio_priv *priv = gpio_chip_to_xlp_priv(gc);
  227. BUG_ON(gpio >= gc->ngpio);
  228. xlp_gpio_set_reg(priv->gpio_paddrv, gpio, state);
  229. }
  230. static const struct of_device_id xlp_gpio_of_ids[] = {
  231. {
  232. .compatible = "netlogic,xlp832-gpio",
  233. .data = (void *)XLP_GPIO_VARIANT_XLP832,
  234. },
  235. {
  236. .compatible = "netlogic,xlp316-gpio",
  237. .data = (void *)XLP_GPIO_VARIANT_XLP316,
  238. },
  239. {
  240. .compatible = "netlogic,xlp208-gpio",
  241. .data = (void *)XLP_GPIO_VARIANT_XLP208,
  242. },
  243. {
  244. .compatible = "netlogic,xlp980-gpio",
  245. .data = (void *)XLP_GPIO_VARIANT_XLP980,
  246. },
  247. {
  248. .compatible = "netlogic,xlp532-gpio",
  249. .data = (void *)XLP_GPIO_VARIANT_XLP532,
  250. },
  251. { /* sentinel */ },
  252. };
  253. MODULE_DEVICE_TABLE(of, xlp_gpio_of_ids);
  254. static int xlp_gpio_probe(struct platform_device *pdev)
  255. {
  256. struct gpio_chip *gc;
  257. struct resource *iores;
  258. struct xlp_gpio_priv *priv;
  259. const struct of_device_id *of_id;
  260. void __iomem *gpio_base;
  261. int irq_base, irq, err;
  262. int ngpio;
  263. u32 soc_type;
  264. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. if (!iores)
  266. return -ENODEV;
  267. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  268. if (!priv)
  269. return -ENOMEM;
  270. gpio_base = devm_ioremap_resource(&pdev->dev, iores);
  271. if (IS_ERR(gpio_base))
  272. return PTR_ERR(gpio_base);
  273. irq = platform_get_irq(pdev, 0);
  274. if (irq < 0)
  275. return irq;
  276. of_id = of_match_device(xlp_gpio_of_ids, &pdev->dev);
  277. if (!of_id) {
  278. dev_err(&pdev->dev, "Failed to get soc type!\n");
  279. return -ENODEV;
  280. }
  281. soc_type = (uintptr_t) of_id->data;
  282. switch (soc_type) {
  283. case XLP_GPIO_VARIANT_XLP832:
  284. priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
  285. priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
  286. priv->gpio_intr_stat = gpio_base + GPIO_INT_STAT;
  287. priv->gpio_intr_type = gpio_base + GPIO_INT_TYPE;
  288. priv->gpio_intr_pol = gpio_base + GPIO_INT_POL;
  289. priv->gpio_intr_en = gpio_base + GPIO_INT_EN00;
  290. ngpio = 41;
  291. break;
  292. case XLP_GPIO_VARIANT_XLP208:
  293. case XLP_GPIO_VARIANT_XLP316:
  294. priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
  295. priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
  296. priv->gpio_intr_stat = gpio_base + GPIO_3XX_INT_STAT;
  297. priv->gpio_intr_type = gpio_base + GPIO_3XX_INT_TYPE;
  298. priv->gpio_intr_pol = gpio_base + GPIO_3XX_INT_POL;
  299. priv->gpio_intr_en = gpio_base + GPIO_3XX_INT_EN00;
  300. ngpio = (soc_type == XLP_GPIO_VARIANT_XLP208) ? 42 : 57;
  301. break;
  302. case XLP_GPIO_VARIANT_XLP980:
  303. case XLP_GPIO_VARIANT_XLP532:
  304. priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
  305. priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
  306. priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
  307. priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
  308. priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
  309. priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
  310. ngpio = (soc_type == XLP_GPIO_VARIANT_XLP980) ? 66 : 67;
  311. break;
  312. default:
  313. dev_err(&pdev->dev, "Unknown Processor type!\n");
  314. return -ENODEV;
  315. }
  316. bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
  317. gc = &priv->chip;
  318. gc->owner = THIS_MODULE;
  319. gc->label = dev_name(&pdev->dev);
  320. gc->base = 0;
  321. gc->dev = &pdev->dev;
  322. gc->ngpio = ngpio;
  323. gc->of_node = pdev->dev.of_node;
  324. gc->direction_output = xlp_gpio_dir_output;
  325. gc->direction_input = xlp_gpio_dir_input;
  326. gc->set = xlp_gpio_set;
  327. gc->get = xlp_gpio_get;
  328. spin_lock_init(&priv->lock);
  329. irq_base = irq_alloc_descs(-1, XLP_GPIO_IRQ_BASE, gc->ngpio, 0);
  330. if (irq_base < 0) {
  331. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  332. return -ENODEV;
  333. }
  334. err = gpiochip_add(gc);
  335. if (err < 0)
  336. goto out_free_desc;
  337. err = gpiochip_irqchip_add(gc, &xlp_gpio_irq_chip, irq_base,
  338. handle_level_irq, IRQ_TYPE_NONE);
  339. if (err) {
  340. dev_err(&pdev->dev, "Could not connect irqchip to gpiochip!\n");
  341. goto out_gpio_remove;
  342. }
  343. gpiochip_set_chained_irqchip(gc, &xlp_gpio_irq_chip, irq,
  344. xlp_gpio_generic_handler);
  345. dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
  346. return 0;
  347. out_gpio_remove:
  348. gpiochip_remove(gc);
  349. out_free_desc:
  350. irq_free_descs(irq_base, gc->ngpio);
  351. return err;
  352. }
  353. static struct platform_driver xlp_gpio_driver = {
  354. .driver = {
  355. .name = "xlp-gpio",
  356. .of_match_table = xlp_gpio_of_ids,
  357. },
  358. .probe = xlp_gpio_probe,
  359. };
  360. module_platform_driver(xlp_gpio_driver);
  361. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  362. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@broadcom.com>");
  363. MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
  364. MODULE_LICENSE("GPL v2");