gpio-bcm-kona.c 18 KB

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