gpio-dwapb.c 19 KB

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