gpio-bcm-kona.c 17 KB

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