gpio-bcm-kona.c 18 KB

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