gpio-xlp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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.irq.domain, 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. soc_type = (uintptr_t)of_device_get_match_data(&pdev->dev);
  279. } else {
  280. const struct acpi_device_id *acpi_id;
  281. acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  282. &pdev->dev);
  283. if (!acpi_id || !acpi_id->driver_data) {
  284. dev_err(&pdev->dev, "Unable to match ACPI ID\n");
  285. return -ENODEV;
  286. }
  287. soc_type = (uintptr_t) acpi_id->driver_data;
  288. }
  289. switch (soc_type) {
  290. case XLP_GPIO_VARIANT_XLP832:
  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_INT_STAT;
  294. priv->gpio_intr_type = gpio_base + GPIO_INT_TYPE;
  295. priv->gpio_intr_pol = gpio_base + GPIO_INT_POL;
  296. priv->gpio_intr_en = gpio_base + GPIO_INT_EN00;
  297. ngpio = 41;
  298. break;
  299. case XLP_GPIO_VARIANT_XLP208:
  300. case XLP_GPIO_VARIANT_XLP316:
  301. priv->gpio_out_en = gpio_base + GPIO_OUTPUT_EN;
  302. priv->gpio_paddrv = gpio_base + GPIO_PADDRV;
  303. priv->gpio_intr_stat = gpio_base + GPIO_3XX_INT_STAT;
  304. priv->gpio_intr_type = gpio_base + GPIO_3XX_INT_TYPE;
  305. priv->gpio_intr_pol = gpio_base + GPIO_3XX_INT_POL;
  306. priv->gpio_intr_en = gpio_base + GPIO_3XX_INT_EN00;
  307. ngpio = (soc_type == XLP_GPIO_VARIANT_XLP208) ? 42 : 57;
  308. break;
  309. case XLP_GPIO_VARIANT_XLP980:
  310. case XLP_GPIO_VARIANT_XLP532:
  311. case GPIO_VARIANT_VULCAN:
  312. priv->gpio_out_en = gpio_base + GPIO_9XX_OUTPUT_EN;
  313. priv->gpio_paddrv = gpio_base + GPIO_9XX_PADDRV;
  314. priv->gpio_intr_stat = gpio_base + GPIO_9XX_INT_STAT;
  315. priv->gpio_intr_type = gpio_base + GPIO_9XX_INT_TYPE;
  316. priv->gpio_intr_pol = gpio_base + GPIO_9XX_INT_POL;
  317. priv->gpio_intr_en = gpio_base + GPIO_9XX_INT_EN00;
  318. if (soc_type == XLP_GPIO_VARIANT_XLP980)
  319. ngpio = 66;
  320. else if (soc_type == XLP_GPIO_VARIANT_XLP532)
  321. ngpio = 67;
  322. else
  323. ngpio = 70;
  324. break;
  325. default:
  326. dev_err(&pdev->dev, "Unknown Processor type!\n");
  327. return -ENODEV;
  328. }
  329. bitmap_zero(priv->gpio_enabled_mask, XLP_MAX_NR_GPIO);
  330. gc = &priv->chip;
  331. gc->owner = THIS_MODULE;
  332. gc->label = dev_name(&pdev->dev);
  333. gc->base = 0;
  334. gc->parent = &pdev->dev;
  335. gc->ngpio = ngpio;
  336. gc->of_node = pdev->dev.of_node;
  337. gc->direction_output = xlp_gpio_dir_output;
  338. gc->direction_input = xlp_gpio_dir_input;
  339. gc->set = xlp_gpio_set;
  340. gc->get = xlp_gpio_get;
  341. spin_lock_init(&priv->lock);
  342. /* XLP(MIPS) has fixed range for GPIO IRQs, Vulcan(ARM64) does not */
  343. if (soc_type != GPIO_VARIANT_VULCAN) {
  344. irq_base = devm_irq_alloc_descs(&pdev->dev, -1,
  345. XLP_GPIO_IRQ_BASE,
  346. gc->ngpio, 0);
  347. if (irq_base < 0) {
  348. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  349. return irq_base;
  350. }
  351. } else {
  352. irq_base = 0;
  353. }
  354. err = gpiochip_add_data(gc, priv);
  355. if (err < 0)
  356. return err;
  357. err = gpiochip_irqchip_add(gc, &xlp_gpio_irq_chip, irq_base,
  358. handle_level_irq, IRQ_TYPE_NONE);
  359. if (err) {
  360. dev_err(&pdev->dev, "Could not connect irqchip to gpiochip!\n");
  361. goto out_gpio_remove;
  362. }
  363. gpiochip_set_chained_irqchip(gc, &xlp_gpio_irq_chip, irq,
  364. xlp_gpio_generic_handler);
  365. dev_info(&pdev->dev, "registered %d GPIOs\n", gc->ngpio);
  366. return 0;
  367. out_gpio_remove:
  368. gpiochip_remove(gc);
  369. return err;
  370. }
  371. #ifdef CONFIG_ACPI
  372. static const struct acpi_device_id xlp_gpio_acpi_match[] = {
  373. { "BRCM9006", GPIO_VARIANT_VULCAN },
  374. { "CAV9006", GPIO_VARIANT_VULCAN },
  375. {},
  376. };
  377. MODULE_DEVICE_TABLE(acpi, xlp_gpio_acpi_match);
  378. #endif
  379. static struct platform_driver xlp_gpio_driver = {
  380. .driver = {
  381. .name = "xlp-gpio",
  382. .of_match_table = xlp_gpio_of_ids,
  383. .acpi_match_table = ACPI_PTR(xlp_gpio_acpi_match),
  384. },
  385. .probe = xlp_gpio_probe,
  386. };
  387. module_platform_driver(xlp_gpio_driver);
  388. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  389. MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@broadcom.com>");
  390. MODULE_DESCRIPTION("Netlogic XLP GPIO Driver");
  391. MODULE_LICENSE("GPL v2");