gpio-dwapb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011 Jamie Iles
  4. *
  5. * All enquiries to [email protected]
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/irq.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include <linux/reset.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include "gpiolib.h"
  24. #include "gpiolib-acpi.h"
  25. #define GPIO_SWPORTA_DR 0x00
  26. #define GPIO_SWPORTA_DDR 0x04
  27. #define GPIO_SWPORTB_DR 0x0c
  28. #define GPIO_SWPORTB_DDR 0x10
  29. #define GPIO_SWPORTC_DR 0x18
  30. #define GPIO_SWPORTC_DDR 0x1c
  31. #define GPIO_SWPORTD_DR 0x24
  32. #define GPIO_SWPORTD_DDR 0x28
  33. #define GPIO_INTEN 0x30
  34. #define GPIO_INTMASK 0x34
  35. #define GPIO_INTTYPE_LEVEL 0x38
  36. #define GPIO_INT_POLARITY 0x3c
  37. #define GPIO_INTSTATUS 0x40
  38. #define GPIO_PORTA_DEBOUNCE 0x48
  39. #define GPIO_PORTA_EOI 0x4c
  40. #define GPIO_EXT_PORTA 0x50
  41. #define GPIO_EXT_PORTB 0x54
  42. #define GPIO_EXT_PORTC 0x58
  43. #define GPIO_EXT_PORTD 0x5c
  44. #define DWAPB_DRIVER_NAME "gpio-dwapb"
  45. #define DWAPB_MAX_PORTS 4
  46. #define DWAPB_MAX_GPIOS 32
  47. #define GPIO_EXT_PORT_STRIDE 0x04 /* register stride 32 bits */
  48. #define GPIO_SWPORT_DR_STRIDE 0x0c /* register stride 3*32 bits */
  49. #define GPIO_SWPORT_DDR_STRIDE 0x0c /* register stride 3*32 bits */
  50. #define GPIO_REG_OFFSET_V1 0
  51. #define GPIO_REG_OFFSET_V2 1
  52. #define GPIO_REG_OFFSET_MASK BIT(0)
  53. #define GPIO_INTMASK_V2 0x44
  54. #define GPIO_INTTYPE_LEVEL_V2 0x34
  55. #define GPIO_INT_POLARITY_V2 0x38
  56. #define GPIO_INTSTATUS_V2 0x3c
  57. #define GPIO_PORTA_EOI_V2 0x40
  58. #define DWAPB_NR_CLOCKS 2
  59. struct dwapb_gpio;
  60. struct dwapb_port_property {
  61. struct fwnode_handle *fwnode;
  62. unsigned int idx;
  63. unsigned int ngpio;
  64. unsigned int gpio_base;
  65. int irq[DWAPB_MAX_GPIOS];
  66. };
  67. struct dwapb_platform_data {
  68. struct dwapb_port_property *properties;
  69. unsigned int nports;
  70. };
  71. #ifdef CONFIG_PM_SLEEP
  72. /* Store GPIO context across system-wide suspend/resume transitions */
  73. struct dwapb_context {
  74. u32 data;
  75. u32 dir;
  76. u32 ext;
  77. u32 int_en;
  78. u32 int_mask;
  79. u32 int_type;
  80. u32 int_pol;
  81. u32 int_deb;
  82. u32 wake_en;
  83. };
  84. #endif
  85. struct dwapb_gpio_port_irqchip {
  86. unsigned int nr_irqs;
  87. unsigned int irq[DWAPB_MAX_GPIOS];
  88. };
  89. struct dwapb_gpio_port {
  90. struct gpio_chip gc;
  91. struct dwapb_gpio_port_irqchip *pirq;
  92. struct dwapb_gpio *gpio;
  93. #ifdef CONFIG_PM_SLEEP
  94. struct dwapb_context *ctx;
  95. #endif
  96. unsigned int idx;
  97. };
  98. #define to_dwapb_gpio(_gc) \
  99. (container_of(_gc, struct dwapb_gpio_port, gc)->gpio)
  100. struct dwapb_gpio {
  101. struct device *dev;
  102. void __iomem *regs;
  103. struct dwapb_gpio_port *ports;
  104. unsigned int nr_ports;
  105. unsigned int flags;
  106. struct reset_control *rst;
  107. struct clk_bulk_data clks[DWAPB_NR_CLOCKS];
  108. };
  109. static inline u32 gpio_reg_v2_convert(unsigned int offset)
  110. {
  111. switch (offset) {
  112. case GPIO_INTMASK:
  113. return GPIO_INTMASK_V2;
  114. case GPIO_INTTYPE_LEVEL:
  115. return GPIO_INTTYPE_LEVEL_V2;
  116. case GPIO_INT_POLARITY:
  117. return GPIO_INT_POLARITY_V2;
  118. case GPIO_INTSTATUS:
  119. return GPIO_INTSTATUS_V2;
  120. case GPIO_PORTA_EOI:
  121. return GPIO_PORTA_EOI_V2;
  122. }
  123. return offset;
  124. }
  125. static inline u32 gpio_reg_convert(struct dwapb_gpio *gpio, unsigned int offset)
  126. {
  127. if ((gpio->flags & GPIO_REG_OFFSET_MASK) == GPIO_REG_OFFSET_V2)
  128. return gpio_reg_v2_convert(offset);
  129. return offset;
  130. }
  131. static inline u32 dwapb_read(struct dwapb_gpio *gpio, unsigned int offset)
  132. {
  133. struct gpio_chip *gc = &gpio->ports[0].gc;
  134. void __iomem *reg_base = gpio->regs;
  135. return gc->read_reg(reg_base + gpio_reg_convert(gpio, offset));
  136. }
  137. static inline void dwapb_write(struct dwapb_gpio *gpio, unsigned int offset,
  138. u32 val)
  139. {
  140. struct gpio_chip *gc = &gpio->ports[0].gc;
  141. void __iomem *reg_base = gpio->regs;
  142. gc->write_reg(reg_base + gpio_reg_convert(gpio, offset), val);
  143. }
  144. static struct dwapb_gpio_port *dwapb_offs_to_port(struct dwapb_gpio *gpio, unsigned int offs)
  145. {
  146. struct dwapb_gpio_port *port;
  147. int i;
  148. for (i = 0; i < gpio->nr_ports; i++) {
  149. port = &gpio->ports[i];
  150. if (port->idx == offs / DWAPB_MAX_GPIOS)
  151. return port;
  152. }
  153. return NULL;
  154. }
  155. static void dwapb_toggle_trigger(struct dwapb_gpio *gpio, unsigned int offs)
  156. {
  157. struct dwapb_gpio_port *port = dwapb_offs_to_port(gpio, offs);
  158. struct gpio_chip *gc;
  159. u32 pol;
  160. int val;
  161. if (!port)
  162. return;
  163. gc = &port->gc;
  164. pol = dwapb_read(gpio, GPIO_INT_POLARITY);
  165. /* Just read the current value right out of the data register */
  166. val = gc->get(gc, offs % DWAPB_MAX_GPIOS);
  167. if (val)
  168. pol &= ~BIT(offs);
  169. else
  170. pol |= BIT(offs);
  171. dwapb_write(gpio, GPIO_INT_POLARITY, pol);
  172. }
  173. static u32 dwapb_do_irq(struct dwapb_gpio *gpio)
  174. {
  175. struct gpio_chip *gc = &gpio->ports[0].gc;
  176. unsigned long irq_status;
  177. irq_hw_number_t hwirq;
  178. irq_status = dwapb_read(gpio, GPIO_INTSTATUS);
  179. for_each_set_bit(hwirq, &irq_status, DWAPB_MAX_GPIOS) {
  180. int gpio_irq = irq_find_mapping(gc->irq.domain, hwirq);
  181. u32 irq_type = irq_get_trigger_type(gpio_irq);
  182. generic_handle_irq(gpio_irq);
  183. if ((irq_type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  184. dwapb_toggle_trigger(gpio, hwirq);
  185. }
  186. return irq_status;
  187. }
  188. static void dwapb_irq_handler(struct irq_desc *desc)
  189. {
  190. struct dwapb_gpio *gpio = irq_desc_get_handler_data(desc);
  191. struct irq_chip *chip = irq_desc_get_chip(desc);
  192. chained_irq_enter(chip, desc);
  193. dwapb_do_irq(gpio);
  194. chained_irq_exit(chip, desc);
  195. }
  196. static irqreturn_t dwapb_irq_handler_mfd(int irq, void *dev_id)
  197. {
  198. return IRQ_RETVAL(dwapb_do_irq(dev_id));
  199. }
  200. static void dwapb_irq_ack(struct irq_data *d)
  201. {
  202. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  203. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  204. u32 val = BIT(irqd_to_hwirq(d));
  205. unsigned long flags;
  206. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  207. dwapb_write(gpio, GPIO_PORTA_EOI, val);
  208. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  209. }
  210. static void dwapb_irq_mask(struct irq_data *d)
  211. {
  212. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  213. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  214. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  215. unsigned long flags;
  216. u32 val;
  217. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  218. val = dwapb_read(gpio, GPIO_INTMASK) | BIT(hwirq);
  219. dwapb_write(gpio, GPIO_INTMASK, val);
  220. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  221. gpiochip_disable_irq(gc, hwirq);
  222. }
  223. static void dwapb_irq_unmask(struct irq_data *d)
  224. {
  225. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  226. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  227. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  228. unsigned long flags;
  229. u32 val;
  230. gpiochip_enable_irq(gc, hwirq);
  231. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  232. val = dwapb_read(gpio, GPIO_INTMASK) & ~BIT(hwirq);
  233. dwapb_write(gpio, GPIO_INTMASK, val);
  234. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  235. }
  236. static void dwapb_irq_enable(struct irq_data *d)
  237. {
  238. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  239. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  240. unsigned long flags;
  241. u32 val;
  242. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  243. val = dwapb_read(gpio, GPIO_INTEN);
  244. val |= BIT(irqd_to_hwirq(d));
  245. dwapb_write(gpio, GPIO_INTEN, val);
  246. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  247. }
  248. static void dwapb_irq_disable(struct irq_data *d)
  249. {
  250. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  251. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  252. unsigned long flags;
  253. u32 val;
  254. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  255. val = dwapb_read(gpio, GPIO_INTEN);
  256. val &= ~BIT(irqd_to_hwirq(d));
  257. dwapb_write(gpio, GPIO_INTEN, val);
  258. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  259. }
  260. static int dwapb_irq_set_type(struct irq_data *d, u32 type)
  261. {
  262. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  263. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  264. irq_hw_number_t bit = irqd_to_hwirq(d);
  265. unsigned long level, polarity, flags;
  266. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  267. level = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
  268. polarity = dwapb_read(gpio, GPIO_INT_POLARITY);
  269. switch (type) {
  270. case IRQ_TYPE_EDGE_BOTH:
  271. level |= BIT(bit);
  272. dwapb_toggle_trigger(gpio, bit);
  273. break;
  274. case IRQ_TYPE_EDGE_RISING:
  275. level |= BIT(bit);
  276. polarity |= BIT(bit);
  277. break;
  278. case IRQ_TYPE_EDGE_FALLING:
  279. level |= BIT(bit);
  280. polarity &= ~BIT(bit);
  281. break;
  282. case IRQ_TYPE_LEVEL_HIGH:
  283. level &= ~BIT(bit);
  284. polarity |= BIT(bit);
  285. break;
  286. case IRQ_TYPE_LEVEL_LOW:
  287. level &= ~BIT(bit);
  288. polarity &= ~BIT(bit);
  289. break;
  290. }
  291. if (type & IRQ_TYPE_LEVEL_MASK)
  292. irq_set_handler_locked(d, handle_level_irq);
  293. else if (type & IRQ_TYPE_EDGE_BOTH)
  294. irq_set_handler_locked(d, handle_edge_irq);
  295. dwapb_write(gpio, GPIO_INTTYPE_LEVEL, level);
  296. if (type != IRQ_TYPE_EDGE_BOTH)
  297. dwapb_write(gpio, GPIO_INT_POLARITY, polarity);
  298. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  299. return 0;
  300. }
  301. #ifdef CONFIG_PM_SLEEP
  302. static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
  303. {
  304. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  305. struct dwapb_gpio *gpio = to_dwapb_gpio(gc);
  306. struct dwapb_context *ctx = gpio->ports[0].ctx;
  307. irq_hw_number_t bit = irqd_to_hwirq(d);
  308. if (enable)
  309. ctx->wake_en |= BIT(bit);
  310. else
  311. ctx->wake_en &= ~BIT(bit);
  312. return 0;
  313. }
  314. #else
  315. #define dwapb_irq_set_wake NULL
  316. #endif
  317. static const struct irq_chip dwapb_irq_chip = {
  318. .name = DWAPB_DRIVER_NAME,
  319. .irq_ack = dwapb_irq_ack,
  320. .irq_mask = dwapb_irq_mask,
  321. .irq_unmask = dwapb_irq_unmask,
  322. .irq_set_type = dwapb_irq_set_type,
  323. .irq_enable = dwapb_irq_enable,
  324. .irq_disable = dwapb_irq_disable,
  325. .irq_set_wake = dwapb_irq_set_wake,
  326. .flags = IRQCHIP_IMMUTABLE,
  327. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  328. };
  329. static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
  330. unsigned offset, unsigned debounce)
  331. {
  332. struct dwapb_gpio_port *port = gpiochip_get_data(gc);
  333. struct dwapb_gpio *gpio = port->gpio;
  334. unsigned long flags, val_deb;
  335. unsigned long mask = BIT(offset);
  336. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  337. val_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
  338. if (debounce)
  339. val_deb |= mask;
  340. else
  341. val_deb &= ~mask;
  342. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, val_deb);
  343. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  344. return 0;
  345. }
  346. static int dwapb_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  347. unsigned long config)
  348. {
  349. u32 debounce;
  350. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  351. return -ENOTSUPP;
  352. debounce = pinconf_to_config_argument(config);
  353. return dwapb_gpio_set_debounce(gc, offset, debounce);
  354. }
  355. static int dwapb_convert_irqs(struct dwapb_gpio_port_irqchip *pirq,
  356. struct dwapb_port_property *pp)
  357. {
  358. int i;
  359. /* Group all available IRQs into an array of parental IRQs. */
  360. for (i = 0; i < pp->ngpio; ++i) {
  361. if (!pp->irq[i])
  362. continue;
  363. pirq->irq[pirq->nr_irqs++] = pp->irq[i];
  364. }
  365. return pirq->nr_irqs ? 0 : -ENOENT;
  366. }
  367. static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
  368. struct dwapb_gpio_port *port,
  369. struct dwapb_port_property *pp)
  370. {
  371. struct dwapb_gpio_port_irqchip *pirq;
  372. struct gpio_chip *gc = &port->gc;
  373. struct gpio_irq_chip *girq;
  374. int err;
  375. pirq = devm_kzalloc(gpio->dev, sizeof(*pirq), GFP_KERNEL);
  376. if (!pirq)
  377. return;
  378. if (dwapb_convert_irqs(pirq, pp)) {
  379. dev_warn(gpio->dev, "no IRQ for port%d\n", pp->idx);
  380. goto err_kfree_pirq;
  381. }
  382. girq = &gc->irq;
  383. girq->handler = handle_bad_irq;
  384. girq->default_type = IRQ_TYPE_NONE;
  385. port->pirq = pirq;
  386. /*
  387. * Intel ACPI-based platforms mostly have the DesignWare APB GPIO
  388. * IRQ lane shared between several devices. In that case the parental
  389. * IRQ has to be handled in the shared way so to be properly delivered
  390. * to all the connected devices.
  391. */
  392. if (has_acpi_companion(gpio->dev)) {
  393. girq->num_parents = 0;
  394. girq->parents = NULL;
  395. girq->parent_handler = NULL;
  396. err = devm_request_irq(gpio->dev, pp->irq[0],
  397. dwapb_irq_handler_mfd,
  398. IRQF_SHARED, DWAPB_DRIVER_NAME, gpio);
  399. if (err) {
  400. dev_err(gpio->dev, "error requesting IRQ\n");
  401. goto err_kfree_pirq;
  402. }
  403. } else {
  404. girq->num_parents = pirq->nr_irqs;
  405. girq->parents = pirq->irq;
  406. girq->parent_handler_data = gpio;
  407. girq->parent_handler = dwapb_irq_handler;
  408. }
  409. gpio_irq_chip_set_chip(girq, &dwapb_irq_chip);
  410. return;
  411. err_kfree_pirq:
  412. devm_kfree(gpio->dev, pirq);
  413. }
  414. static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
  415. struct dwapb_port_property *pp,
  416. unsigned int offs)
  417. {
  418. struct dwapb_gpio_port *port;
  419. void __iomem *dat, *set, *dirout;
  420. int err;
  421. port = &gpio->ports[offs];
  422. port->gpio = gpio;
  423. port->idx = pp->idx;
  424. #ifdef CONFIG_PM_SLEEP
  425. port->ctx = devm_kzalloc(gpio->dev, sizeof(*port->ctx), GFP_KERNEL);
  426. if (!port->ctx)
  427. return -ENOMEM;
  428. #endif
  429. dat = gpio->regs + GPIO_EXT_PORTA + pp->idx * GPIO_EXT_PORT_STRIDE;
  430. set = gpio->regs + GPIO_SWPORTA_DR + pp->idx * GPIO_SWPORT_DR_STRIDE;
  431. dirout = gpio->regs + GPIO_SWPORTA_DDR + 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. port->gc.fwnode = pp->fwnode;
  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. /* Only port A can provide interrupts in all configurations of the IP */
  447. if (pp->idx == 0)
  448. dwapb_configure_irqs(gpio, port, pp);
  449. err = devm_gpiochip_add_data(gpio->dev, &port->gc, port);
  450. if (err) {
  451. dev_err(gpio->dev, "failed to register gpiochip for port%d\n",
  452. port->idx);
  453. return err;
  454. }
  455. return 0;
  456. }
  457. static void dwapb_get_irq(struct device *dev, struct fwnode_handle *fwnode,
  458. struct dwapb_port_property *pp)
  459. {
  460. int irq, j;
  461. for (j = 0; j < pp->ngpio; j++) {
  462. if (has_acpi_companion(dev))
  463. irq = platform_get_irq_optional(to_platform_device(dev), j);
  464. else
  465. irq = fwnode_irq_get(fwnode, j);
  466. if (irq > 0)
  467. pp->irq[j] = irq;
  468. }
  469. }
  470. static struct dwapb_platform_data *dwapb_gpio_get_pdata(struct device *dev)
  471. {
  472. struct fwnode_handle *fwnode;
  473. struct dwapb_platform_data *pdata;
  474. struct dwapb_port_property *pp;
  475. int nports;
  476. int i;
  477. nports = device_get_child_node_count(dev);
  478. if (nports == 0)
  479. return ERR_PTR(-ENODEV);
  480. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  481. if (!pdata)
  482. return ERR_PTR(-ENOMEM);
  483. pdata->properties = devm_kcalloc(dev, nports, sizeof(*pp), GFP_KERNEL);
  484. if (!pdata->properties)
  485. return ERR_PTR(-ENOMEM);
  486. pdata->nports = nports;
  487. i = 0;
  488. device_for_each_child_node(dev, fwnode) {
  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, "ngpios", &pp->ngpio) &&
  499. fwnode_property_read_u32(fwnode, "snps,nr-gpios", &pp->ngpio)) {
  500. dev_info(dev,
  501. "failed to get number of gpios for port%d\n",
  502. i);
  503. pp->ngpio = DWAPB_MAX_GPIOS;
  504. }
  505. pp->gpio_base = -1;
  506. /* For internal use only, new platforms mustn't exercise this */
  507. if (is_software_node(fwnode))
  508. fwnode_property_read_u32(fwnode, "gpio-base", &pp->gpio_base);
  509. /*
  510. * Only port A can provide interrupts in all configurations of
  511. * the IP.
  512. */
  513. if (pp->idx == 0)
  514. dwapb_get_irq(dev, fwnode, pp);
  515. }
  516. return pdata;
  517. }
  518. static void dwapb_assert_reset(void *data)
  519. {
  520. struct dwapb_gpio *gpio = data;
  521. reset_control_assert(gpio->rst);
  522. }
  523. static int dwapb_get_reset(struct dwapb_gpio *gpio)
  524. {
  525. int err;
  526. gpio->rst = devm_reset_control_get_optional_shared(gpio->dev, NULL);
  527. if (IS_ERR(gpio->rst))
  528. return dev_err_probe(gpio->dev, PTR_ERR(gpio->rst),
  529. "Cannot get reset descriptor\n");
  530. err = reset_control_deassert(gpio->rst);
  531. if (err) {
  532. dev_err(gpio->dev, "Cannot deassert reset lane\n");
  533. return err;
  534. }
  535. return devm_add_action_or_reset(gpio->dev, dwapb_assert_reset, gpio);
  536. }
  537. static void dwapb_disable_clks(void *data)
  538. {
  539. struct dwapb_gpio *gpio = data;
  540. clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
  541. }
  542. static int dwapb_get_clks(struct dwapb_gpio *gpio)
  543. {
  544. int err;
  545. /* Optional bus and debounce clocks */
  546. gpio->clks[0].id = "bus";
  547. gpio->clks[1].id = "db";
  548. err = devm_clk_bulk_get_optional(gpio->dev, DWAPB_NR_CLOCKS,
  549. gpio->clks);
  550. if (err)
  551. return dev_err_probe(gpio->dev, err,
  552. "Cannot get APB/Debounce clocks\n");
  553. err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
  554. if (err) {
  555. dev_err(gpio->dev, "Cannot enable APB/Debounce clocks\n");
  556. return err;
  557. }
  558. return devm_add_action_or_reset(gpio->dev, dwapb_disable_clks, gpio);
  559. }
  560. static const struct of_device_id dwapb_of_match[] = {
  561. { .compatible = "snps,dw-apb-gpio", .data = (void *)GPIO_REG_OFFSET_V1},
  562. { .compatible = "apm,xgene-gpio-v2", .data = (void *)GPIO_REG_OFFSET_V2},
  563. { /* Sentinel */ }
  564. };
  565. MODULE_DEVICE_TABLE(of, dwapb_of_match);
  566. static const struct acpi_device_id dwapb_acpi_match[] = {
  567. {"HISI0181", GPIO_REG_OFFSET_V1},
  568. {"APMC0D07", GPIO_REG_OFFSET_V1},
  569. {"APMC0D81", GPIO_REG_OFFSET_V2},
  570. { }
  571. };
  572. MODULE_DEVICE_TABLE(acpi, dwapb_acpi_match);
  573. static int dwapb_gpio_probe(struct platform_device *pdev)
  574. {
  575. unsigned int i;
  576. struct dwapb_gpio *gpio;
  577. int err;
  578. struct dwapb_platform_data *pdata;
  579. struct device *dev = &pdev->dev;
  580. pdata = dwapb_gpio_get_pdata(dev);
  581. if (IS_ERR(pdata))
  582. return PTR_ERR(pdata);
  583. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  584. if (!gpio)
  585. return -ENOMEM;
  586. gpio->dev = &pdev->dev;
  587. gpio->nr_ports = pdata->nports;
  588. err = dwapb_get_reset(gpio);
  589. if (err)
  590. return err;
  591. gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
  592. sizeof(*gpio->ports), GFP_KERNEL);
  593. if (!gpio->ports)
  594. return -ENOMEM;
  595. gpio->regs = devm_platform_ioremap_resource(pdev, 0);
  596. if (IS_ERR(gpio->regs))
  597. return PTR_ERR(gpio->regs);
  598. err = dwapb_get_clks(gpio);
  599. if (err)
  600. return err;
  601. gpio->flags = (uintptr_t)device_get_match_data(dev);
  602. for (i = 0; i < gpio->nr_ports; i++) {
  603. err = dwapb_gpio_add_port(gpio, &pdata->properties[i], i);
  604. if (err)
  605. return err;
  606. }
  607. platform_set_drvdata(pdev, gpio);
  608. return 0;
  609. }
  610. #ifdef CONFIG_PM_SLEEP
  611. static int dwapb_gpio_suspend(struct device *dev)
  612. {
  613. struct dwapb_gpio *gpio = dev_get_drvdata(dev);
  614. struct gpio_chip *gc = &gpio->ports[0].gc;
  615. unsigned long flags;
  616. int i;
  617. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  618. for (i = 0; i < gpio->nr_ports; i++) {
  619. unsigned int offset;
  620. unsigned int idx = gpio->ports[i].idx;
  621. struct dwapb_context *ctx = gpio->ports[i].ctx;
  622. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
  623. ctx->dir = dwapb_read(gpio, offset);
  624. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
  625. ctx->data = dwapb_read(gpio, offset);
  626. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
  627. ctx->ext = dwapb_read(gpio, offset);
  628. /* Only port A can provide interrupts */
  629. if (idx == 0) {
  630. ctx->int_mask = dwapb_read(gpio, GPIO_INTMASK);
  631. ctx->int_en = dwapb_read(gpio, GPIO_INTEN);
  632. ctx->int_pol = dwapb_read(gpio, GPIO_INT_POLARITY);
  633. ctx->int_type = dwapb_read(gpio, GPIO_INTTYPE_LEVEL);
  634. ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
  635. /* Mask out interrupts */
  636. dwapb_write(gpio, GPIO_INTMASK, ~ctx->wake_en);
  637. }
  638. }
  639. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  640. clk_bulk_disable_unprepare(DWAPB_NR_CLOCKS, gpio->clks);
  641. return 0;
  642. }
  643. static int dwapb_gpio_resume(struct device *dev)
  644. {
  645. struct dwapb_gpio *gpio = dev_get_drvdata(dev);
  646. struct gpio_chip *gc = &gpio->ports[0].gc;
  647. unsigned long flags;
  648. int i, err;
  649. err = clk_bulk_prepare_enable(DWAPB_NR_CLOCKS, gpio->clks);
  650. if (err) {
  651. dev_err(gpio->dev, "Cannot reenable APB/Debounce clocks\n");
  652. return err;
  653. }
  654. raw_spin_lock_irqsave(&gc->bgpio_lock, flags);
  655. for (i = 0; i < gpio->nr_ports; i++) {
  656. unsigned int offset;
  657. unsigned int idx = gpio->ports[i].idx;
  658. struct dwapb_context *ctx = gpio->ports[i].ctx;
  659. offset = GPIO_SWPORTA_DR + idx * GPIO_SWPORT_DR_STRIDE;
  660. dwapb_write(gpio, offset, ctx->data);
  661. offset = GPIO_SWPORTA_DDR + idx * GPIO_SWPORT_DDR_STRIDE;
  662. dwapb_write(gpio, offset, ctx->dir);
  663. offset = GPIO_EXT_PORTA + idx * GPIO_EXT_PORT_STRIDE;
  664. dwapb_write(gpio, offset, ctx->ext);
  665. /* Only port A can provide interrupts */
  666. if (idx == 0) {
  667. dwapb_write(gpio, GPIO_INTTYPE_LEVEL, ctx->int_type);
  668. dwapb_write(gpio, GPIO_INT_POLARITY, ctx->int_pol);
  669. dwapb_write(gpio, GPIO_PORTA_DEBOUNCE, ctx->int_deb);
  670. dwapb_write(gpio, GPIO_INTEN, ctx->int_en);
  671. dwapb_write(gpio, GPIO_INTMASK, ctx->int_mask);
  672. /* Clear out spurious interrupts */
  673. dwapb_write(gpio, GPIO_PORTA_EOI, 0xffffffff);
  674. }
  675. }
  676. raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  677. return 0;
  678. }
  679. #endif
  680. static SIMPLE_DEV_PM_OPS(dwapb_gpio_pm_ops, dwapb_gpio_suspend,
  681. dwapb_gpio_resume);
  682. static struct platform_driver dwapb_gpio_driver = {
  683. .driver = {
  684. .name = DWAPB_DRIVER_NAME,
  685. .pm = &dwapb_gpio_pm_ops,
  686. .of_match_table = dwapb_of_match,
  687. .acpi_match_table = dwapb_acpi_match,
  688. },
  689. .probe = dwapb_gpio_probe,
  690. };
  691. module_platform_driver(dwapb_gpio_driver);
  692. MODULE_LICENSE("GPL");
  693. MODULE_AUTHOR("Jamie Iles");
  694. MODULE_DESCRIPTION("Synopsys DesignWare APB GPIO driver");
  695. MODULE_ALIAS("platform:" DWAPB_DRIVER_NAME);