gpio-bcm-kona.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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/driver.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. raw_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. raw_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. raw_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. raw_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. raw_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;
  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. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  119. /* this function only applies to output pin */
  120. if (bcm_kona_gpio_get_dir(chip, gpio) == 1)
  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. raw_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. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  140. if (bcm_kona_gpio_get_dir(chip, gpio) == 1)
  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. raw_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. raw_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. raw_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. raw_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. raw_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. raw_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. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  246. return 0;
  247. }
  248. static int bcm_kona_gpio_set_config(struct gpio_chip *chip, unsigned gpio,
  249. unsigned long config)
  250. {
  251. u32 debounce;
  252. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  253. return -ENOTSUPP;
  254. debounce = pinconf_to_config_argument(config);
  255. return bcm_kona_gpio_set_debounce(chip, gpio, debounce);
  256. }
  257. static const struct gpio_chip template_chip = {
  258. .label = "bcm-kona-gpio",
  259. .owner = THIS_MODULE,
  260. .request = bcm_kona_gpio_request,
  261. .free = bcm_kona_gpio_free,
  262. .get_direction = bcm_kona_gpio_get_dir,
  263. .direction_input = bcm_kona_gpio_direction_input,
  264. .get = bcm_kona_gpio_get,
  265. .direction_output = bcm_kona_gpio_direction_output,
  266. .set = bcm_kona_gpio_set,
  267. .set_config = bcm_kona_gpio_set_config,
  268. .to_irq = bcm_kona_gpio_to_irq,
  269. .base = 0,
  270. };
  271. static void bcm_kona_gpio_irq_ack(struct irq_data *d)
  272. {
  273. struct bcm_kona_gpio *kona_gpio;
  274. void __iomem *reg_base;
  275. unsigned gpio = d->hwirq;
  276. int bank_id = GPIO_BANK(gpio);
  277. int bit = GPIO_BIT(gpio);
  278. u32 val;
  279. unsigned long flags;
  280. kona_gpio = irq_data_get_irq_chip_data(d);
  281. reg_base = kona_gpio->reg_base;
  282. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  283. val = readl(reg_base + GPIO_INT_STATUS(bank_id));
  284. val |= BIT(bit);
  285. writel(val, reg_base + GPIO_INT_STATUS(bank_id));
  286. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  287. }
  288. static void bcm_kona_gpio_irq_mask(struct irq_data *d)
  289. {
  290. struct bcm_kona_gpio *kona_gpio;
  291. void __iomem *reg_base;
  292. unsigned gpio = d->hwirq;
  293. int bank_id = GPIO_BANK(gpio);
  294. int bit = GPIO_BIT(gpio);
  295. u32 val;
  296. unsigned long flags;
  297. kona_gpio = irq_data_get_irq_chip_data(d);
  298. reg_base = kona_gpio->reg_base;
  299. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  300. val = readl(reg_base + GPIO_INT_MASK(bank_id));
  301. val |= BIT(bit);
  302. writel(val, reg_base + GPIO_INT_MASK(bank_id));
  303. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  304. }
  305. static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
  306. {
  307. struct bcm_kona_gpio *kona_gpio;
  308. void __iomem *reg_base;
  309. unsigned gpio = d->hwirq;
  310. int bank_id = GPIO_BANK(gpio);
  311. int bit = GPIO_BIT(gpio);
  312. u32 val;
  313. unsigned long flags;
  314. kona_gpio = irq_data_get_irq_chip_data(d);
  315. reg_base = kona_gpio->reg_base;
  316. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  317. val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
  318. val |= BIT(bit);
  319. writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
  320. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  321. }
  322. static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  323. {
  324. struct bcm_kona_gpio *kona_gpio;
  325. void __iomem *reg_base;
  326. unsigned gpio = d->hwirq;
  327. u32 lvl_type;
  328. u32 val;
  329. unsigned long flags;
  330. kona_gpio = irq_data_get_irq_chip_data(d);
  331. reg_base = kona_gpio->reg_base;
  332. switch (type & IRQ_TYPE_SENSE_MASK) {
  333. case IRQ_TYPE_EDGE_RISING:
  334. lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
  335. break;
  336. case IRQ_TYPE_EDGE_FALLING:
  337. lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
  338. break;
  339. case IRQ_TYPE_EDGE_BOTH:
  340. lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
  341. break;
  342. case IRQ_TYPE_LEVEL_HIGH:
  343. case IRQ_TYPE_LEVEL_LOW:
  344. /* BCM GPIO doesn't support level triggering */
  345. default:
  346. dev_err(kona_gpio->gpio_chip.parent,
  347. "Invalid BCM GPIO irq type 0x%x\n", type);
  348. return -EINVAL;
  349. }
  350. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  351. val = readl(reg_base + GPIO_CONTROL(gpio));
  352. val &= ~GPIO_GPCTR0_ITR_MASK;
  353. val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
  354. writel(val, reg_base + GPIO_CONTROL(gpio));
  355. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  356. return 0;
  357. }
  358. static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
  359. {
  360. void __iomem *reg_base;
  361. int bit, bank_id;
  362. unsigned long sta;
  363. struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
  364. struct irq_chip *chip = irq_desc_get_chip(desc);
  365. chained_irq_enter(chip, desc);
  366. /*
  367. * For bank interrupts, we can't use chip_data to store the kona_gpio
  368. * pointer, since GIC needs it for its own purposes. Therefore, we get
  369. * our pointer from the bank structure.
  370. */
  371. reg_base = bank->kona_gpio->reg_base;
  372. bank_id = bank->id;
  373. while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
  374. (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
  375. for_each_set_bit(bit, &sta, 32) {
  376. int hwirq = GPIO_PER_BANK * bank_id + bit;
  377. int child_irq =
  378. irq_find_mapping(bank->kona_gpio->irq_domain,
  379. hwirq);
  380. /*
  381. * Clear interrupt before handler is called so we don't
  382. * miss any interrupt occurred during executing them.
  383. */
  384. writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
  385. BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
  386. /* Invoke interrupt handler */
  387. generic_handle_irq(child_irq);
  388. }
  389. }
  390. chained_irq_exit(chip, desc);
  391. }
  392. static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
  393. {
  394. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  395. int ret;
  396. ret = gpiochip_lock_as_irq(&kona_gpio->gpio_chip, d->hwirq);
  397. if (ret) {
  398. dev_err(kona_gpio->gpio_chip.parent,
  399. "unable to lock HW IRQ %lu for IRQ\n",
  400. d->hwirq);
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. static void bcm_kona_gpio_irq_relres(struct irq_data *d)
  406. {
  407. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  408. gpiochip_unlock_as_irq(&kona_gpio->gpio_chip, d->hwirq);
  409. }
  410. static struct irq_chip bcm_gpio_irq_chip = {
  411. .name = "bcm-kona-gpio",
  412. .irq_ack = bcm_kona_gpio_irq_ack,
  413. .irq_mask = bcm_kona_gpio_irq_mask,
  414. .irq_unmask = bcm_kona_gpio_irq_unmask,
  415. .irq_set_type = bcm_kona_gpio_irq_set_type,
  416. .irq_request_resources = bcm_kona_gpio_irq_reqres,
  417. .irq_release_resources = bcm_kona_gpio_irq_relres,
  418. };
  419. static struct of_device_id const bcm_kona_gpio_of_match[] = {
  420. { .compatible = "brcm,kona-gpio" },
  421. {}
  422. };
  423. /*
  424. * This lock class tells lockdep that GPIO irqs are in a different
  425. * category than their parents, so it won't report false recursion.
  426. */
  427. static struct lock_class_key gpio_lock_class;
  428. static struct lock_class_key gpio_request_class;
  429. static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  430. irq_hw_number_t hwirq)
  431. {
  432. int ret;
  433. ret = irq_set_chip_data(irq, d->host_data);
  434. if (ret < 0)
  435. return ret;
  436. irq_set_lockdep_class(irq, &gpio_lock_class, &gpio_request_class);
  437. irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
  438. irq_set_noprobe(irq);
  439. return 0;
  440. }
  441. static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
  442. {
  443. irq_set_chip_and_handler(irq, NULL, NULL);
  444. irq_set_chip_data(irq, NULL);
  445. }
  446. static const struct irq_domain_ops bcm_kona_irq_ops = {
  447. .map = bcm_kona_gpio_irq_map,
  448. .unmap = bcm_kona_gpio_irq_unmap,
  449. .xlate = irq_domain_xlate_twocell,
  450. };
  451. static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
  452. {
  453. void __iomem *reg_base;
  454. int i;
  455. reg_base = kona_gpio->reg_base;
  456. /* disable interrupts and clear status */
  457. for (i = 0; i < kona_gpio->num_bank; i++) {
  458. /* Unlock the entire bank first */
  459. bcm_kona_gpio_write_lock_regs(reg_base, i, UNLOCK_CODE);
  460. writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
  461. writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
  462. /* Now re-lock the bank */
  463. bcm_kona_gpio_write_lock_regs(reg_base, i, LOCK_CODE);
  464. }
  465. }
  466. static int bcm_kona_gpio_probe(struct platform_device *pdev)
  467. {
  468. struct device *dev = &pdev->dev;
  469. const struct of_device_id *match;
  470. struct resource *res;
  471. struct bcm_kona_gpio_bank *bank;
  472. struct bcm_kona_gpio *kona_gpio;
  473. struct gpio_chip *chip;
  474. int ret;
  475. int i;
  476. match = of_match_device(bcm_kona_gpio_of_match, dev);
  477. if (!match) {
  478. dev_err(dev, "Failed to find gpio controller\n");
  479. return -ENODEV;
  480. }
  481. kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
  482. if (!kona_gpio)
  483. return -ENOMEM;
  484. kona_gpio->gpio_chip = template_chip;
  485. chip = &kona_gpio->gpio_chip;
  486. kona_gpio->num_bank = of_irq_count(dev->of_node);
  487. if (kona_gpio->num_bank == 0) {
  488. dev_err(dev, "Couldn't determine # GPIO banks\n");
  489. return -ENOENT;
  490. }
  491. if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
  492. dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
  493. GPIO_MAX_BANK_NUM);
  494. return -ENXIO;
  495. }
  496. kona_gpio->banks = devm_kcalloc(dev,
  497. kona_gpio->num_bank,
  498. sizeof(*kona_gpio->banks),
  499. GFP_KERNEL);
  500. if (!kona_gpio->banks)
  501. return -ENOMEM;
  502. kona_gpio->pdev = pdev;
  503. platform_set_drvdata(pdev, kona_gpio);
  504. chip->of_node = dev->of_node;
  505. chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
  506. kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
  507. chip->ngpio,
  508. &bcm_kona_irq_ops,
  509. kona_gpio);
  510. if (!kona_gpio->irq_domain) {
  511. dev_err(dev, "Couldn't allocate IRQ domain\n");
  512. return -ENXIO;
  513. }
  514. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  515. kona_gpio->reg_base = devm_ioremap_resource(dev, res);
  516. if (IS_ERR(kona_gpio->reg_base)) {
  517. ret = -ENXIO;
  518. goto err_irq_domain;
  519. }
  520. for (i = 0; i < kona_gpio->num_bank; i++) {
  521. bank = &kona_gpio->banks[i];
  522. bank->id = i;
  523. bank->irq = platform_get_irq(pdev, i);
  524. bank->kona_gpio = kona_gpio;
  525. if (bank->irq < 0) {
  526. dev_err(dev, "Couldn't get IRQ for bank %d", i);
  527. ret = -ENOENT;
  528. goto err_irq_domain;
  529. }
  530. }
  531. dev_info(&pdev->dev, "Setting up Kona GPIO\n");
  532. bcm_kona_gpio_reset(kona_gpio);
  533. ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
  534. if (ret < 0) {
  535. dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
  536. goto err_irq_domain;
  537. }
  538. for (i = 0; i < kona_gpio->num_bank; i++) {
  539. bank = &kona_gpio->banks[i];
  540. irq_set_chained_handler_and_data(bank->irq,
  541. bcm_kona_gpio_irq_handler,
  542. bank);
  543. }
  544. raw_spin_lock_init(&kona_gpio->lock);
  545. return 0;
  546. err_irq_domain:
  547. irq_domain_remove(kona_gpio->irq_domain);
  548. return ret;
  549. }
  550. static struct platform_driver bcm_kona_gpio_driver = {
  551. .driver = {
  552. .name = "bcm-kona-gpio",
  553. .of_match_table = bcm_kona_gpio_of_match,
  554. },
  555. .probe = bcm_kona_gpio_probe,
  556. };
  557. builtin_platform_driver(bcm_kona_gpio_driver);