gpio-dwapb.c 21 KB

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