gpio-dwapb.c 17 KB

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