gpio-bcm-kona.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Broadcom Kona GPIO Driver
  3. *
  4. * Author: Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>
  5. * Copyright (C) 2012-2014 Broadcom Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/init.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/irqchip/chained_irq.h>
  25. #define BCM_GPIO_PASSWD 0x00a5a501
  26. #define GPIO_PER_BANK 32
  27. #define GPIO_MAX_BANK_NUM 8
  28. #define GPIO_BANK(gpio) ((gpio) >> 5)
  29. #define GPIO_BIT(gpio) ((gpio) & (GPIO_PER_BANK - 1))
  30. /* There is a GPIO control register for each GPIO */
  31. #define GPIO_CONTROL(gpio) (0x00000100 + ((gpio) << 2))
  32. /* The remaining registers are per GPIO bank */
  33. #define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
  34. #define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
  35. #define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
  36. #define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
  37. #define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
  38. #define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
  39. #define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
  40. #define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
  41. #define GPIO_GPPWR_OFFSET 0x00000520
  42. #define GPIO_GPCTR0_DBR_SHIFT 5
  43. #define GPIO_GPCTR0_DBR_MASK 0x000001e0
  44. #define GPIO_GPCTR0_ITR_SHIFT 3
  45. #define GPIO_GPCTR0_ITR_MASK 0x00000018
  46. #define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
  47. #define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
  48. #define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
  49. #define GPIO_GPCTR0_IOTR_MASK 0x00000001
  50. #define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
  51. #define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
  52. #define GPIO_GPCTR0_DB_ENABLE_MASK 0x00000100
  53. #define LOCK_CODE 0xffffffff
  54. #define UNLOCK_CODE 0x00000000
  55. struct bcm_kona_gpio {
  56. void __iomem *reg_base;
  57. int num_bank;
  58. spinlock_t lock;
  59. struct gpio_chip gpio_chip;
  60. struct irq_domain *irq_domain;
  61. struct bcm_kona_gpio_bank *banks;
  62. struct platform_device *pdev;
  63. };
  64. struct bcm_kona_gpio_bank {
  65. int id;
  66. int irq;
  67. /* Used in the interrupt handler */
  68. struct bcm_kona_gpio *kona_gpio;
  69. };
  70. static inline void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base,
  71. int bank_id, u32 lockcode)
  72. {
  73. writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
  74. writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
  75. }
  76. static void bcm_kona_gpio_lock_gpio(struct bcm_kona_gpio *kona_gpio,
  77. unsigned gpio)
  78. {
  79. u32 val;
  80. unsigned long flags;
  81. int bank_id = GPIO_BANK(gpio);
  82. spin_lock_irqsave(&kona_gpio->lock, flags);
  83. val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
  84. val |= BIT(gpio);
  85. bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
  86. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  87. }
  88. static void bcm_kona_gpio_unlock_gpio(struct bcm_kona_gpio *kona_gpio,
  89. unsigned gpio)
  90. {
  91. u32 val;
  92. unsigned long flags;
  93. int bank_id = GPIO_BANK(gpio);
  94. spin_lock_irqsave(&kona_gpio->lock, flags);
  95. val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
  96. val &= ~BIT(gpio);
  97. bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
  98. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  99. }
  100. static int bcm_kona_gpio_get_dir(struct gpio_chip *chip, unsigned gpio)
  101. {
  102. struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
  103. void __iomem *reg_base = kona_gpio->reg_base;
  104. u32 val;
  105. val = readl(reg_base + GPIO_CONTROL(gpio)) & GPIO_GPCTR0_IOTR_MASK;
  106. return val ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
  107. }
  108. static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  109. {
  110. struct bcm_kona_gpio *kona_gpio;
  111. void __iomem *reg_base;
  112. int bank_id = GPIO_BANK(gpio);
  113. int bit = GPIO_BIT(gpio);
  114. u32 val, reg_offset;
  115. unsigned long flags;
  116. kona_gpio = gpiochip_get_data(chip);
  117. reg_base = kona_gpio->reg_base;
  118. spin_lock_irqsave(&kona_gpio->lock, flags);
  119. /* this function only applies to output pin */
  120. if (bcm_kona_gpio_get_dir(chip, gpio) == GPIOF_DIR_IN)
  121. goto out;
  122. reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
  123. val = readl(reg_base + reg_offset);
  124. val |= BIT(bit);
  125. writel(val, reg_base + reg_offset);
  126. out:
  127. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  128. }
  129. static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
  130. {
  131. struct bcm_kona_gpio *kona_gpio;
  132. void __iomem *reg_base;
  133. int bank_id = GPIO_BANK(gpio);
  134. int bit = GPIO_BIT(gpio);
  135. u32 val, reg_offset;
  136. unsigned long flags;
  137. kona_gpio = gpiochip_get_data(chip);
  138. reg_base = kona_gpio->reg_base;
  139. spin_lock_irqsave(&kona_gpio->lock, flags);
  140. if (bcm_kona_gpio_get_dir(chip, gpio) == GPIOF_DIR_IN)
  141. reg_offset = GPIO_IN_STATUS(bank_id);
  142. else
  143. reg_offset = GPIO_OUT_STATUS(bank_id);
  144. /* read the GPIO bank status */
  145. val = readl(reg_base + reg_offset);
  146. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  147. /* return the specified bit status */
  148. return !!(val & BIT(bit));
  149. }
  150. static int bcm_kona_gpio_request(struct gpio_chip *chip, unsigned gpio)
  151. {
  152. struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
  153. bcm_kona_gpio_unlock_gpio(kona_gpio, gpio);
  154. return 0;
  155. }
  156. static void bcm_kona_gpio_free(struct gpio_chip *chip, unsigned gpio)
  157. {
  158. struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
  159. bcm_kona_gpio_lock_gpio(kona_gpio, gpio);
  160. }
  161. static int bcm_kona_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  162. {
  163. struct bcm_kona_gpio *kona_gpio;
  164. void __iomem *reg_base;
  165. u32 val;
  166. unsigned long flags;
  167. kona_gpio = gpiochip_get_data(chip);
  168. reg_base = kona_gpio->reg_base;
  169. spin_lock_irqsave(&kona_gpio->lock, flags);
  170. val = readl(reg_base + GPIO_CONTROL(gpio));
  171. val &= ~GPIO_GPCTR0_IOTR_MASK;
  172. val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
  173. writel(val, reg_base + GPIO_CONTROL(gpio));
  174. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  175. return 0;
  176. }
  177. static int bcm_kona_gpio_direction_output(struct gpio_chip *chip,
  178. unsigned gpio, int value)
  179. {
  180. struct bcm_kona_gpio *kona_gpio;
  181. void __iomem *reg_base;
  182. int bank_id = GPIO_BANK(gpio);
  183. int bit = GPIO_BIT(gpio);
  184. u32 val, reg_offset;
  185. unsigned long flags;
  186. kona_gpio = gpiochip_get_data(chip);
  187. reg_base = kona_gpio->reg_base;
  188. spin_lock_irqsave(&kona_gpio->lock, flags);
  189. val = readl(reg_base + GPIO_CONTROL(gpio));
  190. val &= ~GPIO_GPCTR0_IOTR_MASK;
  191. val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
  192. writel(val, reg_base + GPIO_CONTROL(gpio));
  193. reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
  194. val = readl(reg_base + reg_offset);
  195. val |= BIT(bit);
  196. writel(val, reg_base + reg_offset);
  197. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  198. return 0;
  199. }
  200. static int bcm_kona_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  201. {
  202. struct bcm_kona_gpio *kona_gpio;
  203. kona_gpio = gpiochip_get_data(chip);
  204. if (gpio >= kona_gpio->gpio_chip.ngpio)
  205. return -ENXIO;
  206. return irq_create_mapping(kona_gpio->irq_domain, gpio);
  207. }
  208. static int bcm_kona_gpio_set_debounce(struct gpio_chip *chip, unsigned gpio,
  209. unsigned debounce)
  210. {
  211. struct bcm_kona_gpio *kona_gpio;
  212. void __iomem *reg_base;
  213. u32 val, res;
  214. unsigned long flags;
  215. kona_gpio = gpiochip_get_data(chip);
  216. reg_base = kona_gpio->reg_base;
  217. /* debounce must be 1-128ms (or 0) */
  218. if ((debounce > 0 && debounce < 1000) || debounce > 128000) {
  219. dev_err(chip->parent, "Debounce value %u not in range\n",
  220. debounce);
  221. return -EINVAL;
  222. }
  223. /* calculate debounce bit value */
  224. if (debounce != 0) {
  225. /* Convert to ms */
  226. debounce /= 1000;
  227. /* find the MSB */
  228. res = fls(debounce) - 1;
  229. /* Check if MSB-1 is set (round up or down) */
  230. if (res > 0 && (debounce & BIT(res - 1)))
  231. res++;
  232. }
  233. /* spin lock for read-modify-write of the GPIO register */
  234. spin_lock_irqsave(&kona_gpio->lock, flags);
  235. val = readl(reg_base + GPIO_CONTROL(gpio));
  236. val &= ~GPIO_GPCTR0_DBR_MASK;
  237. if (debounce == 0) {
  238. /* disable debounce */
  239. val &= ~GPIO_GPCTR0_DB_ENABLE_MASK;
  240. } else {
  241. val |= GPIO_GPCTR0_DB_ENABLE_MASK |
  242. (res << GPIO_GPCTR0_DBR_SHIFT);
  243. }
  244. writel(val, reg_base + GPIO_CONTROL(gpio));
  245. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  246. return 0;
  247. }
  248. static const struct gpio_chip template_chip = {
  249. .label = "bcm-kona-gpio",
  250. .owner = THIS_MODULE,
  251. .request = bcm_kona_gpio_request,
  252. .free = bcm_kona_gpio_free,
  253. .get_direction = bcm_kona_gpio_get_dir,
  254. .direction_input = bcm_kona_gpio_direction_input,
  255. .get = bcm_kona_gpio_get,
  256. .direction_output = bcm_kona_gpio_direction_output,
  257. .set = bcm_kona_gpio_set,
  258. .set_debounce = bcm_kona_gpio_set_debounce,
  259. .to_irq = bcm_kona_gpio_to_irq,
  260. .base = 0,
  261. };
  262. static void bcm_kona_gpio_irq_ack(struct irq_data *d)
  263. {
  264. struct bcm_kona_gpio *kona_gpio;
  265. void __iomem *reg_base;
  266. unsigned gpio = d->hwirq;
  267. int bank_id = GPIO_BANK(gpio);
  268. int bit = GPIO_BIT(gpio);
  269. u32 val;
  270. unsigned long flags;
  271. kona_gpio = irq_data_get_irq_chip_data(d);
  272. reg_base = kona_gpio->reg_base;
  273. spin_lock_irqsave(&kona_gpio->lock, flags);
  274. val = readl(reg_base + GPIO_INT_STATUS(bank_id));
  275. val |= BIT(bit);
  276. writel(val, reg_base + GPIO_INT_STATUS(bank_id));
  277. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  278. }
  279. static void bcm_kona_gpio_irq_mask(struct irq_data *d)
  280. {
  281. struct bcm_kona_gpio *kona_gpio;
  282. void __iomem *reg_base;
  283. unsigned gpio = d->hwirq;
  284. int bank_id = GPIO_BANK(gpio);
  285. int bit = GPIO_BIT(gpio);
  286. u32 val;
  287. unsigned long flags;
  288. kona_gpio = irq_data_get_irq_chip_data(d);
  289. reg_base = kona_gpio->reg_base;
  290. spin_lock_irqsave(&kona_gpio->lock, flags);
  291. val = readl(reg_base + GPIO_INT_MASK(bank_id));
  292. val |= BIT(bit);
  293. writel(val, reg_base + GPIO_INT_MASK(bank_id));
  294. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  295. }
  296. static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
  297. {
  298. struct bcm_kona_gpio *kona_gpio;
  299. void __iomem *reg_base;
  300. unsigned gpio = d->hwirq;
  301. int bank_id = GPIO_BANK(gpio);
  302. int bit = GPIO_BIT(gpio);
  303. u32 val;
  304. unsigned long flags;
  305. kona_gpio = irq_data_get_irq_chip_data(d);
  306. reg_base = kona_gpio->reg_base;
  307. spin_lock_irqsave(&kona_gpio->lock, flags);
  308. val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
  309. val |= BIT(bit);
  310. writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
  311. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  312. }
  313. static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  314. {
  315. struct bcm_kona_gpio *kona_gpio;
  316. void __iomem *reg_base;
  317. unsigned gpio = d->hwirq;
  318. u32 lvl_type;
  319. u32 val;
  320. unsigned long flags;
  321. kona_gpio = irq_data_get_irq_chip_data(d);
  322. reg_base = kona_gpio->reg_base;
  323. switch (type & IRQ_TYPE_SENSE_MASK) {
  324. case IRQ_TYPE_EDGE_RISING:
  325. lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
  326. break;
  327. case IRQ_TYPE_EDGE_FALLING:
  328. lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
  329. break;
  330. case IRQ_TYPE_EDGE_BOTH:
  331. lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
  332. break;
  333. case IRQ_TYPE_LEVEL_HIGH:
  334. case IRQ_TYPE_LEVEL_LOW:
  335. /* BCM GPIO doesn't support level triggering */
  336. default:
  337. dev_err(kona_gpio->gpio_chip.parent,
  338. "Invalid BCM GPIO irq type 0x%x\n", type);
  339. return -EINVAL;
  340. }
  341. spin_lock_irqsave(&kona_gpio->lock, flags);
  342. val = readl(reg_base + GPIO_CONTROL(gpio));
  343. val &= ~GPIO_GPCTR0_ITR_MASK;
  344. val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
  345. writel(val, reg_base + GPIO_CONTROL(gpio));
  346. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  347. return 0;
  348. }
  349. static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
  350. {
  351. void __iomem *reg_base;
  352. int bit, bank_id;
  353. unsigned long sta;
  354. struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
  355. struct irq_chip *chip = irq_desc_get_chip(desc);
  356. chained_irq_enter(chip, desc);
  357. /*
  358. * For bank interrupts, we can't use chip_data to store the kona_gpio
  359. * pointer, since GIC needs it for its own purposes. Therefore, we get
  360. * our pointer from the bank structure.
  361. */
  362. reg_base = bank->kona_gpio->reg_base;
  363. bank_id = bank->id;
  364. while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
  365. (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
  366. for_each_set_bit(bit, &sta, 32) {
  367. int hwirq = GPIO_PER_BANK * bank_id + bit;
  368. int child_irq =
  369. irq_find_mapping(bank->kona_gpio->irq_domain,
  370. hwirq);
  371. /*
  372. * Clear interrupt before handler is called so we don't
  373. * miss any interrupt occurred during executing them.
  374. */
  375. writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
  376. BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
  377. /* Invoke interrupt handler */
  378. generic_handle_irq(child_irq);
  379. }
  380. }
  381. chained_irq_exit(chip, desc);
  382. }
  383. static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
  384. {
  385. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  386. if (gpiochip_lock_as_irq(&kona_gpio->gpio_chip, d->hwirq)) {
  387. dev_err(kona_gpio->gpio_chip.parent,
  388. "unable to lock HW IRQ %lu for IRQ\n",
  389. d->hwirq);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static void bcm_kona_gpio_irq_relres(struct irq_data *d)
  395. {
  396. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  397. gpiochip_unlock_as_irq(&kona_gpio->gpio_chip, d->hwirq);
  398. }
  399. static struct irq_chip bcm_gpio_irq_chip = {
  400. .name = "bcm-kona-gpio",
  401. .irq_ack = bcm_kona_gpio_irq_ack,
  402. .irq_mask = bcm_kona_gpio_irq_mask,
  403. .irq_unmask = bcm_kona_gpio_irq_unmask,
  404. .irq_set_type = bcm_kona_gpio_irq_set_type,
  405. .irq_request_resources = bcm_kona_gpio_irq_reqres,
  406. .irq_release_resources = bcm_kona_gpio_irq_relres,
  407. };
  408. static struct of_device_id const bcm_kona_gpio_of_match[] = {
  409. { .compatible = "brcm,kona-gpio" },
  410. {}
  411. };
  412. /*
  413. * This lock class tells lockdep that GPIO irqs are in a different
  414. * category than their parents, so it won't report false recursion.
  415. */
  416. static struct lock_class_key gpio_lock_class;
  417. static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  418. irq_hw_number_t hwirq)
  419. {
  420. int ret;
  421. ret = irq_set_chip_data(irq, d->host_data);
  422. if (ret < 0)
  423. return ret;
  424. irq_set_lockdep_class(irq, &gpio_lock_class);
  425. irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
  426. irq_set_noprobe(irq);
  427. return 0;
  428. }
  429. static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
  430. {
  431. irq_set_chip_and_handler(irq, NULL, NULL);
  432. irq_set_chip_data(irq, NULL);
  433. }
  434. static const struct irq_domain_ops bcm_kona_irq_ops = {
  435. .map = bcm_kona_gpio_irq_map,
  436. .unmap = bcm_kona_gpio_irq_unmap,
  437. .xlate = irq_domain_xlate_twocell,
  438. };
  439. static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
  440. {
  441. void __iomem *reg_base;
  442. int i;
  443. reg_base = kona_gpio->reg_base;
  444. /* disable interrupts and clear status */
  445. for (i = 0; i < kona_gpio->num_bank; i++) {
  446. /* Unlock the entire bank first */
  447. bcm_kona_gpio_write_lock_regs(reg_base, i, UNLOCK_CODE);
  448. writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
  449. writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
  450. /* Now re-lock the bank */
  451. bcm_kona_gpio_write_lock_regs(reg_base, i, LOCK_CODE);
  452. }
  453. }
  454. static int bcm_kona_gpio_probe(struct platform_device *pdev)
  455. {
  456. struct device *dev = &pdev->dev;
  457. const struct of_device_id *match;
  458. struct resource *res;
  459. struct bcm_kona_gpio_bank *bank;
  460. struct bcm_kona_gpio *kona_gpio;
  461. struct gpio_chip *chip;
  462. int ret;
  463. int i;
  464. match = of_match_device(bcm_kona_gpio_of_match, dev);
  465. if (!match) {
  466. dev_err(dev, "Failed to find gpio controller\n");
  467. return -ENODEV;
  468. }
  469. kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
  470. if (!kona_gpio)
  471. return -ENOMEM;
  472. kona_gpio->gpio_chip = template_chip;
  473. chip = &kona_gpio->gpio_chip;
  474. kona_gpio->num_bank = of_irq_count(dev->of_node);
  475. if (kona_gpio->num_bank == 0) {
  476. dev_err(dev, "Couldn't determine # GPIO banks\n");
  477. return -ENOENT;
  478. }
  479. if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
  480. dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
  481. GPIO_MAX_BANK_NUM);
  482. return -ENXIO;
  483. }
  484. kona_gpio->banks = devm_kzalloc(dev,
  485. kona_gpio->num_bank *
  486. sizeof(*kona_gpio->banks), GFP_KERNEL);
  487. if (!kona_gpio->banks)
  488. return -ENOMEM;
  489. kona_gpio->pdev = pdev;
  490. platform_set_drvdata(pdev, kona_gpio);
  491. chip->of_node = dev->of_node;
  492. chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
  493. kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
  494. chip->ngpio,
  495. &bcm_kona_irq_ops,
  496. kona_gpio);
  497. if (!kona_gpio->irq_domain) {
  498. dev_err(dev, "Couldn't allocate IRQ domain\n");
  499. return -ENXIO;
  500. }
  501. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  502. kona_gpio->reg_base = devm_ioremap_resource(dev, res);
  503. if (IS_ERR(kona_gpio->reg_base)) {
  504. ret = -ENXIO;
  505. goto err_irq_domain;
  506. }
  507. for (i = 0; i < kona_gpio->num_bank; i++) {
  508. bank = &kona_gpio->banks[i];
  509. bank->id = i;
  510. bank->irq = platform_get_irq(pdev, i);
  511. bank->kona_gpio = kona_gpio;
  512. if (bank->irq < 0) {
  513. dev_err(dev, "Couldn't get IRQ for bank %d", i);
  514. ret = -ENOENT;
  515. goto err_irq_domain;
  516. }
  517. }
  518. dev_info(&pdev->dev, "Setting up Kona GPIO\n");
  519. bcm_kona_gpio_reset(kona_gpio);
  520. ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
  521. if (ret < 0) {
  522. dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
  523. goto err_irq_domain;
  524. }
  525. for (i = 0; i < kona_gpio->num_bank; i++) {
  526. bank = &kona_gpio->banks[i];
  527. irq_set_chained_handler_and_data(bank->irq,
  528. bcm_kona_gpio_irq_handler,
  529. bank);
  530. }
  531. spin_lock_init(&kona_gpio->lock);
  532. return 0;
  533. err_irq_domain:
  534. irq_domain_remove(kona_gpio->irq_domain);
  535. return ret;
  536. }
  537. static struct platform_driver bcm_kona_gpio_driver = {
  538. .driver = {
  539. .name = "bcm-kona-gpio",
  540. .of_match_table = bcm_kona_gpio_of_match,
  541. },
  542. .probe = bcm_kona_gpio_probe,
  543. };
  544. builtin_platform_driver(bcm_kona_gpio_driver);