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