gpio-dwapb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
  238. {
  239. u32 worked;
  240. struct dwapb_gpio *gpio = dev_id;
  241. worked = dwapb_do_irq(gpio);
  242. return worked ? IRQ_HANDLED : IRQ_NONE;
  243. }
  244. static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
  245. struct dwapb_gpio_port *port,
  246. struct dwapb_port_property *pp)
  247. {
  248. struct gpio_chip *gc = &port->gc;
  249. struct fwnode_handle *fwnode = pp->fwnode;
  250. struct irq_chip_generic *irq_gc = NULL;
  251. unsigned int hwirq, ngpio = gc->ngpio;
  252. struct irq_chip_type *ct;
  253. int err, i;
  254. gpio->domain = irq_domain_create_linear(fwnode, ngpio,
  255. &irq_generic_chip_ops, gpio);
  256. if (!gpio->domain)
  257. return;
  258. err = irq_alloc_domain_generic_chips(gpio->domain, ngpio, 2,
  259. "gpio-dwapb", handle_level_irq,
  260. IRQ_NOREQUEST, 0,
  261. IRQ_GC_INIT_NESTED_LOCK);
  262. if (err) {
  263. dev_info(gpio->dev, "irq_alloc_domain_generic_chips failed\n");
  264. irq_domain_remove(gpio->domain);
  265. gpio->domain = NULL;
  266. return;
  267. }
  268. irq_gc = irq_get_domain_generic_chip(gpio->domain, 0);
  269. if (!irq_gc) {
  270. irq_domain_remove(gpio->domain);
  271. gpio->domain = NULL;
  272. return;
  273. }
  274. irq_gc->reg_base = gpio->regs;
  275. irq_gc->private = gpio;
  276. for (i = 0; i < 2; i++) {
  277. ct = &irq_gc->chip_types[i];
  278. ct->chip.irq_ack = irq_gc_ack_set_bit;
  279. ct->chip.irq_mask = irq_gc_mask_set_bit;
  280. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  281. ct->chip.irq_set_type = dwapb_irq_set_type;
  282. ct->chip.irq_enable = dwapb_irq_enable;
  283. ct->chip.irq_disable = dwapb_irq_disable;
  284. ct->chip.irq_request_resources = dwapb_irq_reqres;
  285. ct->chip.irq_release_resources = dwapb_irq_relres;
  286. ct->regs.ack = GPIO_PORTA_EOI;
  287. ct->regs.mask = GPIO_INTMASK;
  288. ct->type = IRQ_TYPE_LEVEL_MASK;
  289. }
  290. irq_gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  291. irq_gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  292. irq_gc->chip_types[1].handler = handle_edge_irq;
  293. if (!pp->irq_shared) {
  294. irq_set_chained_handler_and_data(pp->irq, dwapb_irq_handler,
  295. gpio);
  296. } else {
  297. /*
  298. * Request a shared IRQ since where MFD would have devices
  299. * using the same irq pin
  300. */
  301. err = devm_request_irq(gpio->dev, pp->irq,
  302. dwapb_irq_handler_mfd,
  303. IRQF_SHARED, "gpio-dwapb-mfd", gpio);
  304. if (err) {
  305. dev_err(gpio->dev, "error requesting IRQ\n");
  306. irq_domain_remove(gpio->domain);
  307. gpio->domain = NULL;
  308. return;
  309. }
  310. }
  311. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  312. irq_create_mapping(gpio->domain, hwirq);
  313. port->gc.to_irq = dwapb_gpio_to_irq;
  314. }
  315. static void dwapb_irq_teardown(struct dwapb_gpio *gpio)
  316. {
  317. struct dwapb_gpio_port *port = &gpio->ports[0];
  318. struct gpio_chip *gc = &port->gc;
  319. unsigned int ngpio = gc->ngpio;
  320. irq_hw_number_t hwirq;
  321. if (!gpio->domain)
  322. return;
  323. for (hwirq = 0 ; hwirq < ngpio ; hwirq++)
  324. irq_dispose_mapping(irq_find_mapping(gpio->domain, hwirq));
  325. irq_domain_remove(gpio->domain);
  326. gpio->domain = NULL;
  327. }
  328. static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
  329. struct dwapb_port_property *pp,
  330. unsigned int offs)
  331. {
  332. struct dwapb_gpio_port *port;
  333. void __iomem *dat, *set, *dirout;
  334. int err;
  335. port = &gpio->ports[offs];
  336. port->gpio = gpio;
  337. port->idx = pp->idx;
  338. #ifdef CONFIG_PM_SLEEP
  339. port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
  340. if (!port->ctx)
  341. return -ENOMEM;
  342. #endif
  343. dat = gpio->regs + GPIO_EXT_PORTA + (pp->idx * GPIO_EXT_PORT_SIZE);
  344. set = gpio->regs + GPIO_SWPORTA_DR + (pp->idx * GPIO_SWPORT_DR_SIZE);
  345. dirout = gpio->regs + GPIO_SWPORTA_DDR +
  346. (pp->idx * GPIO_SWPORT_DDR_SIZE);
  347. err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
  348. NULL, false);
  349. if (err) {
  350. dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
  351. port->idx);
  352. return err;
  353. }
  354. #ifdef CONFIG_OF_GPIO
  355. port->gc.of_node = to_of_node(pp->fwnode);
  356. #endif
  357. port->gc.ngpio = pp->ngpio;
  358. port->gc.base = pp->gpio_base;
  359. /* Only port A support debounce */
  360. if (pp->idx == 0)
  361. port->gc.set_debounce = dwapb_gpio_set_debounce;
  362. if (pp->irq)
  363. dwapb_configure_irqs(gpio, port, pp);
  364. err = gpiochip_add_data(&port->gc, port);
  365. if (err)
  366. dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
  367. port->idx);
  368. else
  369. port->is_registered = true;
  370. /* Add GPIO-signaled ACPI event support */
  371. if (pp->irq)
  372. acpi_gpiochip_request_interrupts(&port->gc);
  373. return err;
  374. }
  375. static void dwapb_gpio_unregister(struct dwapb_gpio *gpio)
  376. {
  377. unsigned int m;
  378. for (m = 0; m < gpio->nr_ports; ++m)
  379. if (gpio->ports[m].is_registered)
  380. gpiochip_remove(&gpio->ports[m].gc);
  381. }
  382. static struct dwapb_platform_data *
  383. dwapb_gpio_get_pdata(struct device *dev)
  384. {
  385. struct fwnode_handle *fwnode;
  386. struct dwapb_platform_data *pdata;
  387. struct dwapb_port_property *pp;
  388. int nports;
  389. int i;
  390. nports = device_get_child_node_count(dev);
  391. if (nports == 0)
  392. return ERR_PTR(-ENODEV);
  393. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  394. if (!pdata)
  395. return ERR_PTR(-ENOMEM);
  396. pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
  397. if (!pdata->properties)
  398. return ERR_PTR(-ENOMEM);
  399. pdata->nports = nports;
  400. i = 0;
  401. device_for_each_child_node(dev, fwnode) {
  402. pp = &pdata->properties[i++];
  403. pp->fwnode = fwnode;
  404. if (fwnode_property_read_u32(fwnode, "reg", &pp->idx) ||
  405. pp->idx >= DWAPB_MAX_PORTS) {
  406. dev_err(dev,
  407. "missing/invalid port index for port%d\n", i);
  408. fwnode_handle_put(fwnode);
  409. return ERR_PTR(-EINVAL);
  410. }
  411. if (fwnode_property_read_u32(fwnode, "snps,nr-gpios",
  412. &pp->ngpio)) {
  413. dev_info(dev,
  414. "failed to get number of gpios for port%d\n",
  415. i);
  416. pp->ngpio = 32;
  417. }
  418. /*
  419. * Only port A can provide interrupts in all configurations of
  420. * the IP.
  421. */
  422. if (dev->of_node && pp->idx == 0 &&
  423. fwnode_property_read_bool(fwnode,
  424. "interrupt-controller")) {
  425. pp->irq = irq_of_parse_and_map(to_of_node(fwnode), 0);
  426. if (!pp->irq)
  427. dev_warn(dev, "no irq for port%d\n", pp->idx);
  428. }
  429. if (has_acpi_companion(dev) && pp->idx == 0)
  430. pp->irq = platform_get_irq(to_platform_device(dev), 0);
  431. pp->irq_shared = false;
  432. pp->gpio_base = -1;
  433. }
  434. return pdata;
  435. }
  436. static int dwapb_gpio_probe(struct platform_device *pdev)
  437. {
  438. unsigned int i;
  439. struct resource *res;
  440. struct dwapb_gpio *gpio;
  441. int err;
  442. struct device *dev = &pdev->dev;
  443. struct dwapb_platform_data *pdata = dev_get_platdata(dev);
  444. if (!pdata) {
  445. pdata = dwapb_gpio_get_pdata(dev);
  446. if (IS_ERR(pdata))
  447. return PTR_ERR(pdata);
  448. }
  449. if (!pdata->nports)
  450. return -ENODEV;
  451. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  452. if (!gpio)
  453. return -ENOMEM;
  454. gpio->dev = &pdev->dev;
  455. gpio->nr_ports = pdata->nports;
  456. gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
  457. sizeof(*gpio->ports), GFP_KERNEL);
  458. if (!gpio->ports)
  459. return -ENOMEM;
  460. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  461. gpio->regs = devm_ioremap_resource(&pdev->dev, res);
  462. if (IS_ERR(gpio->regs))
  463. return PTR_ERR(gpio->regs);
  464. for (i = 0; i < gpio->nr_ports; i++) {
  465. err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
  466. if (err)
  467. goto out_unregister;
  468. }
  469. platform_set_drvdata(pdev, gpio);
  470. return 0;
  471. out_unregister:
  472. dwapb_gpio_unregister(gpio);
  473. dwapb_irq_teardown(gpio);
  474. return err;
  475. }
  476. static int dwapb_gpio_remove(struct platform_device *pdev)
  477. {
  478. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  479. dwapb_gpio_unregister(gpio);
  480. dwapb_irq_teardown(gpio);
  481. return 0;
  482. }
  483. static const struct of_device_id dwapb_of_match[] = {
  484. { .compatible = "snps,dw-apb-gpio" },
  485. { /* Sentinel */ }
  486. };
  487. MODULE_DEVICE_TABLE(of, dwapb_of_match);
  488. static const struct acpi_device_id dwapb_acpi_match[] = {
  489. {"HISI0181", 0},
  490. {"APMC0D07", 0},
  491. { }
  492. };
  493. MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
  494. #ifdef CONFIG_PM_SLEEP
  495. static int dwapb_gpio_suspend(struct device *dev)
  496. {
  497. struct platform_device *pdev = to_platform_device(dev);
  498. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  499. struct gpio_chip *gc = &gpio->ports[0].gc;
  500. unsigned long flags;
  501. int i;
  502. spin_lock_irqsave(&gc->bgpio_lock, flags);
  503. for (i = 0; i < gpio->nr_ports; i++) {
  504. unsigned int offset;
  505. unsigned int idx = gpio->ports[i].idx;
  506. struct dwapb_context *ctx = gpio->ports[i].ctx;
  507. BUG_ON(!ctx);
  508. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
  509. ctx->dir = dwapb_read(gpio, offset);
  510. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
  511. ctx->data = dwapb_read(gpio, offset);
  512. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
  513. ctx->ext = dwapb_read(gpio, offset);
  514. /* Only port A can provide interrupts */
  515. if (idx == 0) {
  516. ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
  517. ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
  518. ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
  519. ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
  520. ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
  521. /* Mask out interrupts */
  522. dwapb_write(gpio, GPIO_INTMASK, 0xffffffff);
  523. }
  524. }
  525. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  526. return 0;
  527. }
  528. static int dwapb_gpio_resume(struct device *dev)
  529. {
  530. struct platform_device *pdev = to_platform_device(dev);
  531. struct dwapb_gpio *gpio = platform_get_drvdata(pdev);
  532. struct gpio_chip *gc = &gpio->ports[0].gc;
  533. unsigned long flags;
  534. int i;
  535. spin_lock_irqsave(&gc->bgpio_lock, flags);
  536. for (i = 0; i < gpio->nr_ports; i++) {
  537. unsigned int offset;
  538. unsigned int idx = gpio->ports[i].idx;
  539. struct dwapb_context *ctx = gpio->ports[i].ctx;
  540. BUG_ON(!ctx);
  541. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_SIZE;
  542. dwapb_write(gpio, offset, ctx->data);
  543. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_SIZE;
  544. dwapb_write(gpio, offset, ctx->dir);
  545. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_SIZE;
  546. dwapb_write(gpio, offset, ctx->ext);
  547. /* Only port A can provide interrupts */
  548. if (idx == 0) {
  549. dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
  550. dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
  551. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
  552. dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
  553. dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
  554. /* Clear out spurious interrupts */
  555. dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
  556. }
  557. }
  558. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  559. return 0;
  560. }
  561. #endif
  562. static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
  563. dwapb_gpio_resume);
  564. static struct platform_driver dwapb_gpio_driver = {
  565. .driver = {
  566. .name = "gpio-dwapb",
  567. .pm = &dwapb_gpio_pm_ops,
  568. .of_match_table = of_match_ptr(dwapb_of_match),
  569. .acpi_match_table = ACPI_PTR(dwapb_acpi_match),
  570. },
  571. .probe = dwapb_gpio_probe,
  572. .remove = dwapb_gpio_remove,
  573. };
  574. module_platform_driver(dwapb_gpio_driver);
  575. MODULE_LICENSE("GPL");
  576. MODULE_AUTHOR("Jamie Iles");
  577. MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");