gpio-dwapb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2011 Jamie Iles
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * All enquiries to support@picochip.com
  9. */
  10. #include <linux/basic_mmio_gpio.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. #define GPIO_SWPORTA_DR 0x00
  25. #define GPIO_SWPORTA_DDR 0x04
  26. #define GPIO_SWPORTB_DR 0x0c
  27. #define GPIO_SWPORTB_DDR 0x10
  28. #define GPIO_SWPORTC_DR 0x18
  29. #define GPIO_SWPORTC_DDR 0x1c
  30. #define GPIO_SWPORTD_DR 0x24
  31. #define GPIO_SWPORTD_DDR 0x28
  32. #define GPIO_INTEN 0x30
  33. #define GPIO_INTMASK 0x34
  34. #define GPIO_INTTYPE_LEVEL 0x38
  35. #define GPIO_INT_POLARITY 0x3c
  36. #define GPIO_INTSTATUS 0x40
  37. #define GPIO_PORTA_EOI 0x4c
  38. #define GPIO_EXT_PORTA 0x50
  39. #define GPIO_EXT_PORTB 0x54
  40. #define GPIO_EXT_PORTC 0x58
  41. #define GPIO_EXT_PORTD 0x5c
  42. #define DWAPB_MAX_PORTS 4
  43. #define GPIO_EXT_PORT_SIZE (GPIO_EXT_PORTB - GPIO_EXT_PORTA)
  44. #define GPIO_SWPORT_DR_SIZE (GPIO_SWPORTB_DR - GPIO_SWPORTA_DR)
  45. #define GPIO_SWPORT_DDR_SIZE (GPIO_SWPORTB_DDR - GPIO_SWPORTA_DDR)
  46. struct dwapb_gpio;
  47. struct dwapb_gpio_port {
  48. struct bgpio_chip bgc;
  49. bool is_registered;
  50. struct dwapb_gpio *gpio;
  51. };
  52. struct dwapb_gpio {
  53. struct device *dev;
  54. void __iomem *regs;
  55. struct dwapb_gpio_port *ports;
  56. unsigned int nr_ports;
  57. struct irq_domain *domain;
  58. };
  59. static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  60. {
  61. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  62. struct dwapb_gpio_port *port = container_of(bgc, struct
  63. dwapb_gpio_port, bgc);
  64. struct dwapb_gpio *gpio = port->gpio;
  65. return irq_find_mapping(gpio->domain, offset);
  66. }
  67. static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
  68. {
  69. u32 v = readl(gpio->regs + GPIO_INT_POLARITY);
  70. if (gpio_get_value(gpio->ports[0].bgc.gc.base + offs))
  71. v &= ~BIT(offs);
  72. else
  73. v |= BIT(offs);
  74. writel(v, gpio->regs + GPIO_INT_POLARITY);
  75. }
  76. static void dwapb_irq_handler(u32 irq, struct irq_desc *desc)
  77. {
  78. struct dwapb_gpio *gpio = irq_get_handler_data(irq);
  79. struct irq_chip *chip = irq_desc_get_chip(desc);
  80. u32 irq_status = readl_relaxed(gpio->regs + GPIO_INTSTATUS);
  81. while (irq_status) {
  82. int hwirq = fls(irq_status) - 1;
  83. int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
  84. generic_handle_irq(gpio_irq);
  85. irq_status &= ~BIT(hwirq);
  86. if ((irq_get_trigger_type(gpio_irq) & IRQ_TYPE_SENSE_MASK)
  87. == IRQ_TYPE_EDGE_BOTH)
  88. dwapb_toggle_trigger(gpio, hwirq);
  89. }
  90. if (chip->irq_eoi)
  91. chip->irq_eoi(irq_desc_get_irq_data(desc));
  92. }
  93. static void dwapb_irq_enable(struct irq_data *d)
  94. {
  95. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  96. struct dwapb_gpio *gpio = igc->private;
  97. struct bgpio_chip *bgc = &gpio->ports[0].bgc;
  98. unsigned long flags;
  99. u32 val;
  100. spin_lock_irqsave(&bgc->lock, flags);
  101. val = readl(gpio->regs + GPIO_INTEN);
  102. val |= BIT(d->hwirq);
  103. writel(val, gpio->regs + GPIO_INTEN);
  104. spin_unlock_irqrestore(&bgc->lock, flags);
  105. }
  106. static void dwapb_irq_disable(struct irq_data *d)
  107. {
  108. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  109. struct dwapb_gpio *gpio = igc->private;
  110. struct bgpio_chip *bgc = &gpio->ports[0].bgc;
  111. unsigned long flags;
  112. u32 val;
  113. spin_lock_irqsave(&bgc->lock, flags);
  114. val = readl(gpio->regs + GPIO_INTEN);
  115. val &= ~BIT(d->hwirq);
  116. writel(val, gpio->regs + GPIO_INTEN);
  117. spin_unlock_irqrestore(&bgc->lock, flags);
  118. }
  119. static int dwapb_irq_reqres(struct irq_data *d)
  120. {
  121. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  122. struct dwapb_gpio *gpio = igc->private;
  123. struct bgpio_chip *bgc = &gpio->ports[0].bgc;
  124. if (gpio_lock_as_irq(&bgc->gc, irqd_to_hwirq(d))) {
  125. dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
  126. irqd_to_hwirq(d));
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static void dwapb_irq_relres(struct irq_data *d)
  132. {
  133. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  134. struct dwapb_gpio *gpio = igc->private;
  135. struct bgpio_chip *bgc = &gpio->ports[0].bgc;
  136. gpio_unlock_as_irq(&bgc->gc, irqd_to_hwirq(d));
  137. }
  138. static int dwapb_irq_set_type(struct irq_data *d, u32 type)
  139. {
  140. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  141. struct dwapb_gpio *gpio = igc->private;
  142. struct bgpio_chip *bgc = &gpio->ports[0].bgc;
  143. int bit = d->hwirq;
  144. unsigned long level, polarity, flags;
  145. if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
  146. IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  147. return -EINVAL;
  148. spin_lock_irqsave(&bgc->lock, flags);
  149. level = readl(gpio->regs + GPIO_INTTYPE_LEVEL);
  150. polarity = readl(gpio->regs + GPIO_INT_POLARITY);
  151. switch (type) {
  152. case IRQ_TYPE_EDGE_BOTH:
  153. level |= BIT(bit);
  154. dwapb_toggle_trigger(gpio, bit);
  155. break;
  156. case IRQ_TYPE_EDGE_RISING:
  157. level |= BIT(bit);
  158. polarity |= BIT(bit);
  159. break;
  160. case IRQ_TYPE_EDGE_FALLING:
  161. level |= BIT(bit);
  162. polarity &= ~BIT(bit);
  163. break;
  164. case IRQ_TYPE_LEVEL_HIGH:
  165. level &= ~BIT(bit);
  166. polarity |= BIT(bit);
  167. break;
  168. case IRQ_TYPE_LEVEL_LOW:
  169. level &= ~BIT(bit);
  170. polarity &= ~BIT(bit);
  171. break;
  172. }
  173. irq_setup_alt_chip(d, type);
  174. writel(level, gpio->regs + GPIO_INTTYPE_LEVEL);
  175. writel(polarity, gpio->regs + GPIO_INT_POLARITY);
  176. spin_unlock_irqrestore(&bgc->lock, flags);
  177. return 0;
  178. }
  179. static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
  180. struct dwapb_gpio_port *port)
  181. {
  182. struct gpio_chip *gc = &port->bgc.gc;
  183. struct device_node *node = gc->of_node;
  184. struct irq_chip_generic *irq_gc;
  185. unsigned int hwirq, ngpio = gc->ngpio;
  186. struct irq_chip_type *ct;
  187. int err, irq, i;
  188. irq = irq_of_parse_and_map(node, 0);
  189. if (!irq) {
  190. dev_warn(gpio->dev, "no irq for bank %s\n",
  191. port->bgc.gc.of_node->full_name);
  192. return;
  193. }
  194. gpio->domain = irq_domain_add_linear(node, ngpio,
  195. &irq_generic_chip_ops, gpio);
  196. if (!gpio->domain)
  197. return;
  198. err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
  199. "gpio-dwapb", handle_level_irq,
  200. IRQ_NOREQUEST, 0,
  201. IRQ_GC_INIT_NESTED_LOCK);
  202. if (err) {
  203. dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
  204. irq_domain_remove(gpio->domain);
  205. gpio->domain = NULL;
  206. return;
  207. }
  208. irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
  209. if (!irq_gc) {
  210. irq_domain_remove(gpio->domain);
  211. gpio->domain = NULL;
  212. return;
  213. }
  214. irq_gc->reg_base = gpio->regs;
  215. irq_gc->private = gpio;
  216. for (i = 0; i < 2; i++) {
  217. ct = &irq_gc->chip_types[i];
  218. ct->chip.irq_ack = irq_gc_ack_set_bit;
  219. ct->chip.irq_mask = irq_gc_mask_set_bit;
  220. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  221. ct->chip.irq_set_type = dwapb_irq_set_type;
  222. ct->chip.irq_enable = dwapb_irq_enable;
  223. ct->chip.irq_disable = dwapb_irq_disable;
  224. ct->chip.irq_request_resources = dwapb_irq_reqres;
  225. ct->chip.irq_release_resources = dwapb_irq_relres;
  226. ct->regs.ack = GPIO_PORTA_EOI;
  227. ct->regs.mask = GPIO_INTMASK;
  228. ct->type = IRQ_TYPE_LEVEL_MASK;
  229. }
  230. irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  231. irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  232. irq_gc->chip_types[1].handler = handle_edge_irq;
  233. irq_set_chained_handler(irq, dwapb_irq_handler);
  234. irq_set_handler_data(irq, gpio);
  235. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  236. irq_create_mapping(gpio->domain, hwirq);
  237. port->bgc.gc.to_irq = dwapb_gpio_to_irq;
  238. }
  239. static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
  240. {
  241. struct dwapb_gpio_port *port = &gpio->ports[0];
  242. struct gpio_chip *gc = &port->bgc.gc;
  243. unsigned int ngpio = gc->ngpio;
  244. irq_hw_number_t hwirq;
  245. if (!gpio->domain)
  246. return;
  247. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  248. irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
  249. irq_domain_remove(gpio->domain);
  250. gpio->domain = NULL;
  251. }
  252. static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
  253. struct device_node *port_np,
  254. unsigned int offs)
  255. {
  256. struct dwapb_gpio_port *port;
  257. u32 port_idx, ngpio;
  258. void __iomem *dat, *set, *dirout;
  259. int err;
  260. if (of_property_read_u32(port_np, "reg", &port_idx) ||
  261. port_idx >= DWAPB_MAX_PORTS) {
  262. dev_err(gpio->dev, "missing/invalid port index for %s\n",
  263. port_np->full_name);
  264. return -EINVAL;
  265. }
  266. port = &gpio->ports[offs];
  267. port->gpio = gpio;
  268. if (of_property_read_u32(port_np, "snps,nr-gpios", &ngpio)) {
  269. dev_info(gpio->dev, "failed to get number of gpios for %s\n",
  270. port_np->full_name);
  271. ngpio = 32;
  272. }
  273. dat = gpio->regs + GPIO_EXT_PORTA + (port_idx * GPIO_EXT_PORT_SIZE);
  274. set = gpio->regs + GPIO_SWPORTA_DR + (port_idx * GPIO_SWPORT_DR_SIZE);
  275. dirout = gpio->regs + GPIO_SWPORTA_DDR +
  276. (port_idx * GPIO_SWPORT_DDR_SIZE);
  277. err = bgpio_init(&port->bgc, gpio->dev, 4, dat, set, NULL, dirout,
  278. NULL, false);
  279. if (err) {
  280. dev_err(gpio->dev, "failed to init gpio chip for %s\n",
  281. port_np->full_name);
  282. return err;
  283. }
  284. port->bgc.gc.ngpio = ngpio;
  285. port->bgc.gc.of_node = port_np;
  286. /*
  287. * Only port A can provide interrupts in all configurations of the IP.
  288. */
  289. if (port_idx == 0 &&
  290. of_property_read_bool(port_np, "interrupt-controller"))
  291. dwapb_configure_irqs(gpio, port);
  292. err = gpiochip_add(&port->bgc.gc);
  293. if (err)
  294. dev_err(gpio->dev, "failed to register gpiochip for %s\n",
  295. port_np->full_name);
  296. else
  297. port->is_registered = true;
  298. return err;
  299. }
  300. static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
  301. {
  302. unsigned int m;
  303. for (m = 0; m < gpio->nr_ports; ++m)
  304. if (gpio->ports[m].is_registered)
  305. WARN_ON(gpiochip_remove(&gpio->ports[m].bgc.gc));
  306. }
  307. static int dwapb_gpio_probe(struct platform_device *pdev)
  308. {
  309. struct resource *res;
  310. struct dwapb_gpio *gpio;
  311. struct device_node *np;
  312. int err;
  313. unsigned int offs = 0;
  314. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  315. if (!gpio)
  316. return -ENOMEM;
  317. gpio->dev = &pdev->dev;
  318. gpio->nr_ports = of_get_child_count(pdev->dev.of_node);
  319. if (!gpio->nr_ports) {
  320. err = -EINVAL;
  321. goto out_err;
  322. }
  323. gpio->ports = devm_kzalloc(&pdev->dev, gpio->nr_ports *
  324. sizeof(*gpio->ports), GFP_KERNEL);
  325. if (!gpio->ports) {
  326. err = -ENOMEM;
  327. goto out_err;
  328. }
  329. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  330. gpio->regs = devm_ioremap_resource(&pdev->dev, res);
  331. if (IS_ERR(gpio->regs)) {
  332. err = PTR_ERR(gpio->regs);
  333. goto out_err;
  334. }
  335. for_each_child_of_node(pdev->dev.of_node, np) {
  336. err = dwapb_gpio_add_port(gpio, np, offs++);
  337. if (err)
  338. goto out_unregister;
  339. }
  340. platform_set_drvdata(pdev, gpio);
  341. return 0;
  342. out_unregister:
  343. dwapb_gpio_unregister(gpio);
  344. dwapb_irq_teardown(gpio);
  345. out_err:
  346. return err;
  347. }
  348. static int dwapb_gpio_remove(struct platform_device *pdev)
  349. {
  350. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  351. dwapb_gpio_unregister(gpio);
  352. dwapb_irq_teardown(gpio);
  353. return 0;
  354. }
  355. static const struct of_device_id dwapb_of_match[] = {
  356. { .compatible = "snps,dw-apb-gpio" },
  357. { /* Sentinel */ }
  358. };
  359. MODULE_DEVICE_TABLE(of, dwapb_of_match);
  360. static struct platform_driver dwapb_gpio_driver = {
  361. .driver = {
  362. .name = "gpio-dwapb",
  363. .owner = THIS_MODULE,
  364. .of_match_table = of_match_ptr(dwapb_of_match),
  365. },
  366. .probe = dwapb_gpio_probe,
  367. .remove = dwapb_gpio_remove,
  368. };
  369. module_platform_driver(dwapb_gpio_driver);
  370. MODULE_LICENSE("GPL");
  371. MODULE_AUTHOR("Jamie Iles");
  372. MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");