gpio-bcm-kona.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base,
  68. int bank_id, u32 lockcode)
  69. {
  70. writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
  71. writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
  72. }
  73. static void bcm_kona_gpio_lock_gpio(struct bcm_kona_gpio *kona_gpio,
  74. unsigned gpio)
  75. {
  76. u32 val;
  77. unsigned long flags;
  78. int bank_id = GPIO_BANK(gpio);
  79. spin_lock_irqsave(&kona_gpio->lock, flags);
  80. val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
  81. val |= BIT(gpio);
  82. bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
  83. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  84. }
  85. static void bcm_kona_gpio_unlock_gpio(struct bcm_kona_gpio *kona_gpio,
  86. unsigned gpio)
  87. {
  88. u32 val;
  89. unsigned long flags;
  90. int bank_id = GPIO_BANK(gpio);
  91. spin_lock_irqsave(&kona_gpio->lock, flags);
  92. val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
  93. val &= ~BIT(gpio);
  94. bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
  95. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  96. }
  97. static int bcm_kona_gpio_get_dir(struct gpio_chip *chip, unsigned gpio)
  98. {
  99. struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
  100. void __iomem *reg_base = kona_gpio->reg_base;
  101. u32 val;
  102. val = readl(reg_base + GPIO_CONTROL(gpio)) & GPIO_GPCTR0_IOTR_MASK;
  103. return val ? GPIOF_DIR_IN : GPIOF_DIR_OUT;
  104. }
  105. static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  106. {
  107. struct bcm_kona_gpio *kona_gpio;
  108. void __iomem *reg_base;
  109. int bank_id = GPIO_BANK(gpio);
  110. int bit = GPIO_BIT(gpio);
  111. u32 val, reg_offset;
  112. unsigned long flags;
  113. kona_gpio = gpiochip_get_data(chip);
  114. reg_base = kona_gpio->reg_base;
  115. spin_lock_irqsave(&kona_gpio->lock, flags);
  116. /* this function only applies to output pin */
  117. if (bcm_kona_gpio_get_dir(chip, gpio) == GPIOF_DIR_IN)
  118. goto out;
  119. reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
  120. val = readl(reg_base + reg_offset);
  121. val |= BIT(bit);
  122. writel(val, reg_base + reg_offset);
  123. out:
  124. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  125. }
  126. static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
  127. {
  128. struct bcm_kona_gpio *kona_gpio;
  129. void __iomem *reg_base;
  130. int bank_id = GPIO_BANK(gpio);
  131. int bit = GPIO_BIT(gpio);
  132. u32 val, reg_offset;
  133. unsigned long flags;
  134. kona_gpio = gpiochip_get_data(chip);
  135. reg_base = kona_gpio->reg_base;
  136. spin_lock_irqsave(&kona_gpio->lock, flags);
  137. if (bcm_kona_gpio_get_dir(chip, gpio) == GPIOF_DIR_IN)
  138. reg_offset = GPIO_IN_STATUS(bank_id);
  139. else
  140. reg_offset = GPIO_OUT_STATUS(bank_id);
  141. /* read the GPIO bank status */
  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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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 = gpiochip_get_data(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->parent, "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. .get_direction = bcm_kona_gpio_get_dir,
  251. .direction_input = bcm_kona_gpio_direction_input,
  252. .get = bcm_kona_gpio_get,
  253. .direction_output = bcm_kona_gpio_direction_output,
  254. .set = bcm_kona_gpio_set,
  255. .set_debounce = bcm_kona_gpio_set_debounce,
  256. .to_irq = bcm_kona_gpio_to_irq,
  257. .base = 0,
  258. };
  259. static void bcm_kona_gpio_irq_ack(struct irq_data *d)
  260. {
  261. struct bcm_kona_gpio *kona_gpio;
  262. void __iomem *reg_base;
  263. unsigned 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. val = readl(reg_base + GPIO_INT_STATUS(bank_id));
  272. val |= BIT(bit);
  273. writel(val, reg_base + GPIO_INT_STATUS(bank_id));
  274. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  275. }
  276. static void bcm_kona_gpio_irq_mask(struct irq_data *d)
  277. {
  278. struct bcm_kona_gpio *kona_gpio;
  279. void __iomem *reg_base;
  280. unsigned gpio = d->hwirq;
  281. int bank_id = GPIO_BANK(gpio);
  282. int bit = GPIO_BIT(gpio);
  283. u32 val;
  284. unsigned long flags;
  285. kona_gpio = irq_data_get_irq_chip_data(d);
  286. reg_base = kona_gpio->reg_base;
  287. spin_lock_irqsave(&kona_gpio->lock, flags);
  288. val = readl(reg_base + GPIO_INT_MASK(bank_id));
  289. val |= BIT(bit);
  290. writel(val, reg_base + GPIO_INT_MASK(bank_id));
  291. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  292. }
  293. static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
  294. {
  295. struct bcm_kona_gpio *kona_gpio;
  296. void __iomem *reg_base;
  297. unsigned gpio = d->hwirq;
  298. int bank_id = GPIO_BANK(gpio);
  299. int bit = GPIO_BIT(gpio);
  300. u32 val;
  301. unsigned long flags;
  302. kona_gpio = irq_data_get_irq_chip_data(d);
  303. reg_base = kona_gpio->reg_base;
  304. spin_lock_irqsave(&kona_gpio->lock, flags);
  305. val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
  306. val |= BIT(bit);
  307. writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
  308. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  309. }
  310. static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  311. {
  312. struct bcm_kona_gpio *kona_gpio;
  313. void __iomem *reg_base;
  314. unsigned gpio = d->hwirq;
  315. u32 lvl_type;
  316. u32 val;
  317. unsigned long flags;
  318. kona_gpio = irq_data_get_irq_chip_data(d);
  319. reg_base = kona_gpio->reg_base;
  320. switch (type & IRQ_TYPE_SENSE_MASK) {
  321. case IRQ_TYPE_EDGE_RISING:
  322. lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
  323. break;
  324. case IRQ_TYPE_EDGE_FALLING:
  325. lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
  326. break;
  327. case IRQ_TYPE_EDGE_BOTH:
  328. lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
  329. break;
  330. case IRQ_TYPE_LEVEL_HIGH:
  331. case IRQ_TYPE_LEVEL_LOW:
  332. /* BCM GPIO doesn't support level triggering */
  333. default:
  334. dev_err(kona_gpio->gpio_chip.parent,
  335. "Invalid BCM GPIO irq type 0x%x\n", type);
  336. return -EINVAL;
  337. }
  338. spin_lock_irqsave(&kona_gpio->lock, flags);
  339. val = readl(reg_base + GPIO_CONTROL(gpio));
  340. val &= ~GPIO_GPCTR0_ITR_MASK;
  341. val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
  342. writel(val, reg_base + GPIO_CONTROL(gpio));
  343. spin_unlock_irqrestore(&kona_gpio->lock, flags);
  344. return 0;
  345. }
  346. static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
  347. {
  348. void __iomem *reg_base;
  349. int bit, bank_id;
  350. unsigned long sta;
  351. struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
  352. struct irq_chip *chip = irq_desc_get_chip(desc);
  353. chained_irq_enter(chip, desc);
  354. /*
  355. * For bank interrupts, we can't use chip_data to store the kona_gpio
  356. * pointer, since GIC needs it for its own purposes. Therefore, we get
  357. * our pointer from the bank structure.
  358. */
  359. reg_base = bank->kona_gpio->reg_base;
  360. bank_id = bank->id;
  361. while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
  362. (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
  363. for_each_set_bit(bit, &sta, 32) {
  364. int hwirq = GPIO_PER_BANK * bank_id + bit;
  365. int child_irq =
  366. irq_find_mapping(bank->kona_gpio->irq_domain,
  367. hwirq);
  368. /*
  369. * Clear interrupt before handler is called so we don't
  370. * miss any interrupt occurred during executing them.
  371. */
  372. writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
  373. BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
  374. /* Invoke interrupt handler */
  375. generic_handle_irq(child_irq);
  376. }
  377. }
  378. chained_irq_exit(chip, desc);
  379. }
  380. static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
  381. {
  382. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  383. if (gpiochip_lock_as_irq(&kona_gpio->gpio_chip, d->hwirq)) {
  384. dev_err(kona_gpio->gpio_chip.parent,
  385. "unable to lock HW IRQ %lu for IRQ\n",
  386. d->hwirq);
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. static void bcm_kona_gpio_irq_relres(struct irq_data *d)
  392. {
  393. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  394. gpiochip_unlock_as_irq(&kona_gpio->gpio_chip, d->hwirq);
  395. }
  396. static struct irq_chip bcm_gpio_irq_chip = {
  397. .name = "bcm-kona-gpio",
  398. .irq_ack = bcm_kona_gpio_irq_ack,
  399. .irq_mask = bcm_kona_gpio_irq_mask,
  400. .irq_unmask = bcm_kona_gpio_irq_unmask,
  401. .irq_set_type = bcm_kona_gpio_irq_set_type,
  402. .irq_request_resources = bcm_kona_gpio_irq_reqres,
  403. .irq_release_resources = bcm_kona_gpio_irq_relres,
  404. };
  405. static struct of_device_id const bcm_kona_gpio_of_match[] = {
  406. { .compatible = "brcm,kona-gpio" },
  407. {}
  408. };
  409. MODULE_DEVICE_TABLE(of, bcm_kona_gpio_of_match);
  410. /*
  411. * This lock class tells lockdep that GPIO irqs are in a different
  412. * category than their parents, so it won't report false recursion.
  413. */
  414. static struct lock_class_key gpio_lock_class;
  415. static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  416. irq_hw_number_t hwirq)
  417. {
  418. int ret;
  419. ret = irq_set_chip_data(irq, d->host_data);
  420. if (ret < 0)
  421. return ret;
  422. irq_set_lockdep_class(irq, &gpio_lock_class);
  423. irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
  424. irq_set_noprobe(irq);
  425. return 0;
  426. }
  427. static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
  428. {
  429. irq_set_chip_and_handler(irq, NULL, NULL);
  430. irq_set_chip_data(irq, NULL);
  431. }
  432. static const struct irq_domain_ops bcm_kona_irq_ops = {
  433. .map = bcm_kona_gpio_irq_map,
  434. .unmap = bcm_kona_gpio_irq_unmap,
  435. .xlate = irq_domain_xlate_twocell,
  436. };
  437. static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
  438. {
  439. void __iomem *reg_base;
  440. int i;
  441. reg_base = kona_gpio->reg_base;
  442. /* disable interrupts and clear status */
  443. for (i = 0; i < kona_gpio->num_bank; i++) {
  444. /* Unlock the entire bank first */
  445. bcm_kona_gpio_write_lock_regs(kona_gpio, i, UNLOCK_CODE);
  446. writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
  447. writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
  448. /* Now re-lock the bank */
  449. bcm_kona_gpio_write_lock_regs(kona_gpio, i, LOCK_CODE);
  450. }
  451. }
  452. static int bcm_kona_gpio_probe(struct platform_device *pdev)
  453. {
  454. struct device *dev = &pdev->dev;
  455. const struct of_device_id *match;
  456. struct resource *res;
  457. struct bcm_kona_gpio_bank *bank;
  458. struct bcm_kona_gpio *kona_gpio;
  459. struct gpio_chip *chip;
  460. int ret;
  461. int i;
  462. match = of_match_device(bcm_kona_gpio_of_match, dev);
  463. if (!match) {
  464. dev_err(dev, "Failed to find gpio controller\n");
  465. return -ENODEV;
  466. }
  467. kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
  468. if (!kona_gpio)
  469. return -ENOMEM;
  470. kona_gpio->gpio_chip = template_chip;
  471. chip = &kona_gpio->gpio_chip;
  472. kona_gpio->num_bank = of_irq_count(dev->of_node);
  473. if (kona_gpio->num_bank == 0) {
  474. dev_err(dev, "Couldn't determine # GPIO banks\n");
  475. return -ENOENT;
  476. }
  477. if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
  478. dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
  479. GPIO_MAX_BANK_NUM);
  480. return -ENXIO;
  481. }
  482. kona_gpio->banks = devm_kzalloc(dev,
  483. kona_gpio->num_bank *
  484. sizeof(*kona_gpio->banks), GFP_KERNEL);
  485. if (!kona_gpio->banks)
  486. return -ENOMEM;
  487. kona_gpio->pdev = pdev;
  488. platform_set_drvdata(pdev, kona_gpio);
  489. chip->of_node = dev->of_node;
  490. chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
  491. kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
  492. chip->ngpio,
  493. &bcm_kona_irq_ops,
  494. kona_gpio);
  495. if (!kona_gpio->irq_domain) {
  496. dev_err(dev, "Couldn't allocate IRQ domain\n");
  497. return -ENXIO;
  498. }
  499. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  500. kona_gpio->reg_base = devm_ioremap_resource(dev, res);
  501. if (IS_ERR(kona_gpio->reg_base)) {
  502. ret = -ENXIO;
  503. goto err_irq_domain;
  504. }
  505. for (i = 0; i < kona_gpio->num_bank; i++) {
  506. bank = &kona_gpio->banks[i];
  507. bank->id = i;
  508. bank->irq = platform_get_irq(pdev, i);
  509. bank->kona_gpio = kona_gpio;
  510. if (bank->irq < 0) {
  511. dev_err(dev, "Couldn't get IRQ for bank %d", i);
  512. ret = -ENOENT;
  513. goto err_irq_domain;
  514. }
  515. }
  516. dev_info(&pdev->dev, "Setting up Kona GPIO\n");
  517. bcm_kona_gpio_reset(kona_gpio);
  518. ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
  519. if (ret < 0) {
  520. dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
  521. goto err_irq_domain;
  522. }
  523. for (i = 0; i < kona_gpio->num_bank; i++) {
  524. bank = &kona_gpio->banks[i];
  525. irq_set_chained_handler_and_data(bank->irq,
  526. bcm_kona_gpio_irq_handler,
  527. bank);
  528. }
  529. spin_lock_init(&kona_gpio->lock);
  530. return 0;
  531. err_irq_domain:
  532. irq_domain_remove(kona_gpio->irq_domain);
  533. return ret;
  534. }
  535. static struct platform_driver bcm_kona_gpio_driver = {
  536. .driver = {
  537. .name = "bcm-kona-gpio",
  538. .of_match_table = bcm_kona_gpio_of_match,
  539. },
  540. .probe = bcm_kona_gpio_probe,
  541. };
  542. module_platform_driver(bcm_kona_gpio_driver);
  543. MODULE_AUTHOR("Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>");
  544. MODULE_DESCRIPTION("Broadcom Kona GPIO Driver");
  545. MODULE_LICENSE("GPL v2");