gpio-dwapb.c 21 KB

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