gpio-dwapb.c 19 KB

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