gpio-dwapb.c 21 KB

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