gpio-dwapb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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/acpi.h>
  11. #include <linux/gpio/driver.h>
  12. /* FIXME: for gpio_get_value(), replace this with direct register read */
  13. #include <linux/gpio.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/irq.h>
  20. #include <linux/irqdomain.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/property.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/platform_data/gpio-dwapb.h>
  29. #include <linux/slab.h>
  30. #include "gpiolib.h"
  31. #define GPIO_SWPORTA_DR 0x00
  32. #define GPIO_SWPORTA_DDR 0x04
  33. #define GPIO_SWPORTB_DR 0x0c
  34. #define GPIO_SWPORTB_DDR 0x10
  35. #define GPIO_SWPORTC_DR 0x18
  36. #define GPIO_SWPORTC_DDR 0x1c
  37. #define GPIO_SWPORTD_DR 0x24
  38. #define GPIO_SWPORTD_DDR 0x28
  39. #define GPIO_INTEN 0x30
  40. #define GPIO_INTMASK 0x34
  41. #define GPIO_INTTYPE_LEVEL 0x38
  42. #define GPIO_INT_POLARITY 0x3c
  43. #define GPIO_INTSTATUS 0x40
  44. #define GPIO_PORTA_DEBOUNCE 0x48
  45. #define GPIO_PORTA_EOI 0x4c
  46. #define GPIO_EXT_PORTA 0x50
  47. #define GPIO_EXT_PORTB 0x54
  48. #define GPIO_EXT_PORTC 0x58
  49. #define GPIO_EXT_PORTD 0x5c
  50. #define DWAPB_MAX_PORTS 4
  51. #define GPIO_EXT_PORT_SIZE (GPIO_EXT_PORTB - GPIO_EXT_PORTA)
  52. #define GPIO_SWPORT_DR_SIZE (GPIO_SWPORTB_DR - GPIO_SWPORTA_DR)
  53. #define GPIO_SWPORT_DDR_SIZE (GPIO_SWPORTB_DDR - GPIO_SWPORTA_DDR)
  54. struct dwapb_gpio;
  55. #ifdef CONFIG_PM_SLEEP
  56. /* Store GPIO context across system-wide suspend/resume transitions */
  57. struct dwapb_context {
  58. u32 data;
  59. u32 dir;
  60. u32 ext;
  61. u32 int_en;
  62. u32 int_mask;
  63. u32 int_type;
  64. u32 int_pol;
  65. u32 int_deb;
  66. };
  67. #endif
  68. struct dwapb_gpio_port {
  69. struct gpio_chip gc;
  70. bool is_registered;
  71. struct dwapb_gpio *gpio;
  72. #ifdef CONFIG_PM_SLEEP
  73. struct dwapb_context *ctx;
  74. #endif
  75. unsigned int idx;
  76. };
  77. struct dwapb_gpio {
  78. struct device *dev;
  79. void __iomem *regs;
  80. struct dwapb_gpio_port *ports;
  81. unsigned int nr_ports;
  82. struct irq_domain *domain;
  83. };
  84. static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
  85. {
  86. struct gpio_chip *gc = &gpio->ports[0].gc;
  87. void __iomem *reg_base = gpio->regs;
  88. return gc->read_reg(reg_base + offset);
  89. }
  90. static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
  91. u32 val)
  92. {
  93. struct gpio_chip *gc = &gpio->ports[0].gc;
  94. void __iomem *reg_base = gpio->regs;
  95. gc->write_reg(reg_base + offset, val);
  96. }
  97. static int dwapb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  98. {
  99. struct dwapb_gpio_port *port = gpiochip_get_data(gc);
  100. struct dwapb_gpio *gpio = port->gpio;
  101. return irq_find_mapping(gpio->domain, offset);
  102. }
  103. static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
  104. {
  105. u32 v = dwapb_read(gpio, GPIO_INT_POLARITY);
  106. if (gpio_get_value(gpio->ports[0].gc.base + offs))
  107. v &= ~BIT(offs);
  108. else
  109. v |= BIT(offs);
  110. dwapb_write(gpio, GPIO_INT_POLARITY, v);
  111. }
  112. static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
  113. {
  114. u32 irq_status = readl_relaxed(gpio->regs + GPIO_INTSTATUS);
  115. u32 ret = irq_status;
  116. while (irq_status) {
  117. int hwirq = fls(irq_status) - 1;
  118. int gpio_irq = irq_find_mapping(gpio->domain, hwirq);
  119. generic_handle_irq(gpio_irq);
  120. irq_status &= ~BIT(hwirq);
  121. if ((irq_get_trigger_type(gpio_irq) & IRQ_TYPE_SENSE_MASK)
  122. == IRQ_TYPE_EDGE_BOTH)
  123. dwapb_toggle_trigger(gpio, hwirq);
  124. }
  125. return ret;
  126. }
  127. static void dwapb_irq_handler(struct irq_desc *desc)
  128. {
  129. struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
  130. struct irq_chip *chip = irq_desc_get_chip(desc);
  131. dwapb_do_irq(gpio);
  132. if (chip->irq_eoi)
  133. chip->irq_eoi(irq_desc_get_irq_data(desc));
  134. }
  135. static void dwapb_irq_enable(struct irq_data *d)
  136. {
  137. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  138. struct dwapb_gpio *gpio = igc->private;
  139. struct gpio_chip *gc = &gpio->ports[0].gc;
  140. unsigned long flags;
  141. u32 val;
  142. spin_lock_irqsave(&gc->bgpio_lock, flags);
  143. val = dwapb_read(gpio, GPIO_INTEN);
  144. val |= BIT(d->hwirq);
  145. dwapb_write(gpio, GPIO_INTEN, val);
  146. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  147. }
  148. static void dwapb_irq_disable(struct irq_data *d)
  149. {
  150. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  151. struct dwapb_gpio *gpio = igc->private;
  152. struct gpio_chip *gc = &gpio->ports[0].gc;
  153. unsigned long flags;
  154. u32 val;
  155. spin_lock_irqsave(&gc->bgpio_lock, flags);
  156. val = dwapb_read(gpio, GPIO_INTEN);
  157. val &= ~BIT(d->hwirq);
  158. dwapb_write(gpio, GPIO_INTEN, val);
  159. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  160. }
  161. static int dwapb_irq_reqres(struct irq_data *d)
  162. {
  163. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  164. struct dwapb_gpio *gpio = igc->private;
  165. struct gpio_chip *gc = &gpio->ports[0].gc;
  166. if (gpiochip_lock_as_irq(gc, irqd_to_hwirq(d))) {
  167. dev_err(gpio->dev, "unable to lock HW IRQ %lu for IRQ\n",
  168. irqd_to_hwirq(d));
  169. return -EINVAL;
  170. }
  171. return 0;
  172. }
  173. static void dwapb_irq_relres(struct irq_data *d)
  174. {
  175. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  176. struct dwapb_gpio *gpio = igc->private;
  177. struct gpio_chip *gc = &gpio->ports[0].gc;
  178. gpiochip_unlock_as_irq(gc, irqd_to_hwirq(d));
  179. }
  180. static int dwapb_irq_set_type(struct irq_data *d, u32 type)
  181. {
  182. struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
  183. struct dwapb_gpio *gpio = igc->private;
  184. struct gpio_chip *gc = &gpio->ports[0].gc;
  185. int bit = d->hwirq;
  186. unsigned long level, polarity, flags;
  187. if (type & ~(IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
  188. IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  189. return -EINVAL;
  190. spin_lock_irqsave(&gc->bgpio_lock, flags);
  191. level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
  192. polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
  193. switch (type) {
  194. case IRQ_TYPE_EDGE_BOTH:
  195. level |= BIT(bit);
  196. dwapb_toggle_trigger(gpio, bit);
  197. break;
  198. case IRQ_TYPE_EDGE_RISING:
  199. level |= BIT(bit);
  200. polarity |= BIT(bit);
  201. break;
  202. case IRQ_TYPE_EDGE_FALLING:
  203. level |= BIT(bit);
  204. polarity &= ~BIT(bit);
  205. break;
  206. case IRQ_TYPE_LEVEL_HIGH:
  207. level &= ~BIT(bit);
  208. polarity |= BIT(bit);
  209. break;
  210. case IRQ_TYPE_LEVEL_LOW:
  211. level &= ~BIT(bit);
  212. polarity &= ~BIT(bit);
  213. break;
  214. }
  215. irq_setup_alt_chip(d, type);
  216. dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
  217. dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
  218. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  219. return 0;
  220. }
  221. static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
  222. unsigned offset, unsigned debounce)
  223. {
  224. struct dwapb_gpio_port *port = gpiochip_get_data(gc);
  225. struct dwapb_gpio *gpio = port->gpio;
  226. unsigned long flags, val_deb;
  227. unsigned long mask = gc->pin2mask(gc, offset);
  228. spin_lock_irqsave(&gc->bgpio_lock, flags);
  229. val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
  230. if (debounce)
  231. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb | mask);
  232. else
  233. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb & ~mask);
  234. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  235. return 0;
  236. }
  237. static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  238. unsigned long config)
  239. {
  240. u32 debounce;
  241. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  242. return -ENOTSUPP;
  243. debounce = pinconf_to_config_argument(config);
  244. return dwapb_gpio_set_debounce(gc, offset, debounce);
  245. }
  246. static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
  247. {
  248. u32 worked;
  249. struct dwapb_gpio *gpio = dev_id;
  250. worked = dwapb_do_irq(gpio);
  251. return worked ? IRQ_HANDLED : IRQ_NONE;
  252. }
  253. static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
  254. struct dwapb_gpio_port *port,
  255. struct dwapb_port_property *pp)
  256. {
  257. struct gpio_chip *gc = &port->gc;
  258. struct fwnode_handle *fwnode = pp->fwnode;
  259. struct irq_chip_generic *irq_gc = NULL;
  260. unsigned int hwirq, ngpio = gc->ngpio;
  261. struct irq_chip_type *ct;
  262. int err, i;
  263. gpio->domain = irq_domain_create_linear(fwnode, ngpio,
  264. &irq_generic_chip_ops, gpio);
  265. if (!gpio->domain)
  266. return;
  267. err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
  268. "gpio-dwapb", handle_level_irq,
  269. IRQ_NOREQUEST, 0,
  270. IRQ_GC_INIT_NESTED_LOCK);
  271. if (err) {
  272. dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
  273. irq_domain_remove(gpio->domain);
  274. gpio->domain = NULL;
  275. return;
  276. }
  277. irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
  278. if (!irq_gc) {
  279. irq_domain_remove(gpio->domain);
  280. gpio->domain = NULL;
  281. return;
  282. }
  283. irq_gc->reg_base = gpio->regs;
  284. irq_gc->private = gpio;
  285. for (i = 0; i < 2; i++) {
  286. ct = &irq_gc->chip_types[i];
  287. ct->chip.irq_ack = irq_gc_ack_set_bit;
  288. ct->chip.irq_mask = irq_gc_mask_set_bit;
  289. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  290. ct->chip.irq_set_type = dwapb_irq_set_type;
  291. ct->chip.irq_enable = dwapb_irq_enable;
  292. ct->chip.irq_disable = dwapb_irq_disable;
  293. ct->chip.irq_request_resources = dwapb_irq_reqres;
  294. ct->chip.irq_release_resources = dwapb_irq_relres;
  295. ct->regs.ack = GPIO_PORTA_EOI;
  296. ct->regs.mask = GPIO_INTMASK;
  297. ct->type = IRQ_TYPE_LEVEL_MASK;
  298. }
  299. irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  300. irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  301. irq_gc->chip_types[1].handler = handle_edge_irq;
  302. if (!pp->irq_shared) {
  303. irq_set_chained_handler_and_data(pp->irq, dwapb_irq_handler,
  304. gpio);
  305. } else {
  306. /*
  307. * Request a shared IRQ since where MFD would have devices
  308. * using the same irq pin
  309. */
  310. err = devm_request_irq(gpio->dev, pp->irq,
  311. dwapb_irq_handler_mfd,
  312. IRQF_SHARED, "gpio-dwapb-mfd", gpio);
  313. if (err) {
  314. dev_err(gpio->dev, "error requesting IRQ\n");
  315. irq_domain_remove(gpio->domain);
  316. gpio->domain = NULL;
  317. return;
  318. }
  319. }
  320. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  321. irq_create_mapping(gpio->domain, hwirq);
  322. port->gc.to_irq = dwapb_gpio_to_irq;
  323. }
  324. static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
  325. {
  326. struct dwapb_gpio_port *port = &gpio->ports[0];
  327. struct gpio_chip *gc = &port->gc;
  328. unsigned int ngpio = gc->ngpio;
  329. irq_hw_number_t hwirq;
  330. if (!gpio->domain)
  331. return;
  332. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  333. irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
  334. irq_domain_remove(gpio->domain);
  335. gpio->domain = NULL;
  336. }
  337. static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
  338. struct dwapb_port_property *pp,
  339. unsigned int offs)
  340. {
  341. struct dwapb_gpio_port *port;
  342. void __iomem *dat, *set, *dirout;
  343. int err;
  344. port = &gpio->ports[offs];
  345. port->gpio = gpio;
  346. port->idx = pp->idx;
  347. #ifdef CONFIG_PM_SLEEP
  348. port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
  349. if (!port->ctx)
  350. return -ENOMEM;
  351. #endif
  352. dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_SIZE);
  353. set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_SIZE);
  354. dirout = gpio->regs + GPIO_SWPORTA_DDR +
  355. (pp->idx * GPIO_SWPORT_DDR_SIZE);
  356. err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
  357. NULL, false);
  358. if (err) {
  359. dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
  360. port->idx);
  361. return err;
  362. }
  363. #ifdef CONFIG_OF_GPIO
  364. port->gc.of_node = to_of_node(pp->fwnode);
  365. #endif
  366. port->gc.ngpio = pp->ngpio;
  367. port->gc.base = pp->gpio_base;
  368. /* Only port A support debounce */
  369. if (pp->idx == 0)
  370. port->gc.set_config = dwapb_gpio_set_config;
  371. if (pp->irq)
  372. dwapb_configure_irqs(gpio, port, pp);
  373. err = gpiochip_add_data(&port->gc, port);
  374. if (err)
  375. dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
  376. port->idx);
  377. else
  378. port->is_registered = true;
  379. /* Add GPIO-signaled ACPI event support */
  380. if (pp->irq)
  381. acpi_gpiochip_request_interrupts(&port->gc);
  382. return err;
  383. }
  384. static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
  385. {
  386. unsigned int m;
  387. for (m = 0; m < gpio->nr_ports; ++m)
  388. if (gpio->ports[m].is_registered)
  389. gpiochip_remove(&gpio->ports[m].gc);
  390. }
  391. static struct dwapb_platform_data *
  392. dwapb_gpio_get_pdata(struct device *dev)
  393. {
  394. struct fwnode_handle *fwnode;
  395. struct dwapb_platform_data *pdata;
  396. struct dwapb_port_property *pp;
  397. int nports;
  398. int i;
  399. nports = device_get_child_node_count(dev);
  400. if (nports == 0)
  401. return ERR_PTR(-ENODEV);
  402. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  403. if (!pdata)
  404. return ERR_PTR(-ENOMEM);
  405. pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
  406. if (!pdata->properties)
  407. return ERR_PTR(-ENOMEM);
  408. pdata->nports = nports;
  409. i = 0;
  410. device_for_each_child_node(dev, fwnode) {
  411. pp = &pdata->properties[i++];
  412. pp->fwnode = fwnode;
  413. if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
  414. pp->idx >= DWAPB_MAX_PORTS) {
  415. dev_err(dev,
  416. "missing/invalid port index for port%d\n", i);
  417. fwnode_handle_put(fwnode);
  418. return ERR_PTR(-EINVAL);
  419. }
  420. if (fwnode_property_read_u32(fwnode, "snps,nr-gpios",
  421. &pp->ngpio)) {
  422. dev_info(dev,
  423. "failed to get number of gpios for port%d\n",
  424. i);
  425. pp->ngpio = 32;
  426. }
  427. /*
  428. * Only port A can provide interrupts in all configurations of
  429. * the IP.
  430. */
  431. if (dev->of_node && pp->idx == 0 &&
  432. fwnode_property_read_bool(fwnode,
  433. "interrupt-controller")) {
  434. pp->irq = irq_of_parse_and_map(to_of_node(fwnode), 0);
  435. if (!pp->irq)
  436. dev_warn(dev, "no irq for port%d\n", pp->idx);
  437. }
  438. if (has_acpi_companion(dev) && pp->idx == 0)
  439. pp->irq = platform_get_irq(to_platform_device(dev), 0);
  440. pp->irq_shared = false;
  441. pp->gpio_base = -1;
  442. }
  443. return pdata;
  444. }
  445. static int dwapb_gpio_probe(struct platform_device *pdev)
  446. {
  447. unsigned int i;
  448. struct resource *res;
  449. struct dwapb_gpio *gpio;
  450. int err;
  451. struct device *dev = &pdev->dev;
  452. struct dwapb_platform_data *pdata = dev_get_platdata(dev);
  453. if (!pdata) {
  454. pdata = dwapb_gpio_get_pdata(dev);
  455. if (IS_ERR(pdata))
  456. return PTR_ERR(pdata);
  457. }
  458. if (!pdata->nports)
  459. return -ENODEV;
  460. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  461. if (!gpio)
  462. return -ENOMEM;
  463. gpio->dev = &pdev->dev;
  464. gpio->nr_ports = pdata->nports;
  465. gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
  466. sizeof(*gpio->ports), GFP_KERNEL);
  467. if (!gpio->ports)
  468. return -ENOMEM;
  469. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  470. gpio->regs = devm_ioremap_resource(&pdev->dev, res);
  471. if (IS_ERR(gpio->regs))
  472. return PTR_ERR(gpio->regs);
  473. for (i = 0; i < gpio->nr_ports; i++) {
  474. err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
  475. if (err)
  476. goto out_unregister;
  477. }
  478. platform_set_drvdata(pdev, gpio);
  479. return 0;
  480. out_unregister:
  481. dwapb_gpio_unregister(gpio);
  482. dwapb_irq_teardown(gpio);
  483. return err;
  484. }
  485. static int dwapb_gpio_remove(struct platform_device *pdev)
  486. {
  487. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  488. dwapb_gpio_unregister(gpio);
  489. dwapb_irq_teardown(gpio);
  490. return 0;
  491. }
  492. static const struct of_device_id dwapb_of_match[] = {
  493. { .compatible = "snps,dw-apb-gpio" },
  494. { /* Sentinel */ }
  495. };
  496. MODULE_DEVICE_TABLE(of, dwapb_of_match);
  497. static const struct acpi_device_id dwapb_acpi_match[] = {
  498. {"HISI0181", 0},
  499. {"APMC0D07", 0},
  500. { }
  501. };
  502. MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
  503. #ifdef CONFIG_PM_SLEEP
  504. static int dwapb_gpio_suspend(struct device *dev)
  505. {
  506. struct platform_device *pdev = to_platform_device(dev);
  507. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  508. struct gpio_chip *gc = &gpio->ports[0].gc;
  509. unsigned long flags;
  510. int i;
  511. spin_lock_irqsave(&gc->bgpio_lock, flags);
  512. for (i = 0; i < gpio->nr_ports; i++) {
  513. unsigned int offset;
  514. unsigned int idx = gpio->ports[i].idx;
  515. struct dwapb_context *ctx = gpio->ports[i].ctx;
  516. BUG_ON(!ctx);
  517. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
  518. ctx->dir = dwapb_read(gpio, offset);
  519. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
  520. ctx->data = dwapb_read(gpio, offset);
  521. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
  522. ctx->ext = dwapb_read(gpio, offset);
  523. /* Only port A can provide interrupts */
  524. if (idx == 0) {
  525. ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
  526. ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
  527. ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
  528. ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
  529. ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
  530. /* Mask out interrupts */
  531. dwapb_write(gpio, GPIO_INTMASK, 0xffffffff);
  532. }
  533. }
  534. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  535. return 0;
  536. }
  537. static int dwapb_gpio_resume(struct device *dev)
  538. {
  539. struct platform_device *pdev = to_platform_device(dev);
  540. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  541. struct gpio_chip *gc = &gpio->ports[0].gc;
  542. unsigned long flags;
  543. int i;
  544. spin_lock_irqsave(&gc->bgpio_lock, flags);
  545. for (i = 0; i < gpio->nr_ports; i++) {
  546. unsigned int offset;
  547. unsigned int idx = gpio->ports[i].idx;
  548. struct dwapb_context *ctx = gpio->ports[i].ctx;
  549. BUG_ON(!ctx);
  550. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
  551. dwapb_write(gpio, offset, ctx->data);
  552. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
  553. dwapb_write(gpio, offset, ctx->dir);
  554. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
  555. dwapb_write(gpio, offset, ctx->ext);
  556. /* Only port A can provide interrupts */
  557. if (idx == 0) {
  558. dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
  559. dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
  560. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
  561. dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
  562. dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
  563. /* Clear out spurious interrupts */
  564. dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
  565. }
  566. }
  567. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  568. return 0;
  569. }
  570. #endif
  571. static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
  572. dwapb_gpio_resume);
  573. static struct platform_driver dwapb_gpio_driver = {
  574. .driver = {
  575. .name = "gpio-dwapb",
  576. .pm = &dwapb_gpio_pm_ops,
  577. .of_match_table = of_match_ptr(dwapb_of_match),
  578. .acpi_match_table = ACPI_PTR(dwapb_acpi_match),
  579. },
  580. .probe = dwapb_gpio_probe,
  581. .remove = dwapb_gpio_remove,
  582. };
  583. module_platform_driver(dwapb_gpio_driver);
  584. MODULE_LICENSE("GPL");
  585. MODULE_AUTHOR("Jamie Iles");
  586. MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");