gpio-xlp.c 12 KB

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