gpio-bcm-kona.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. gpiochip_disable_irq(&kona_gpio->gpio_chip, gpio);
  304. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  305. }
  306. static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
  307. {
  308. struct bcm_kona_gpio *kona_gpio;
  309. void __iomem *reg_base;
  310. unsigned gpio = d->hwirq;
  311. int bank_id = GPIO_BANK(gpio);
  312. int bit = GPIO_BIT(gpio);
  313. u32 val;
  314. unsigned long flags;
  315. kona_gpio = irq_data_get_irq_chip_data(d);
  316. reg_base = kona_gpio->reg_base;
  317. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  318. val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
  319. val |= BIT(bit);
  320. writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
  321. gpiochip_enable_irq(&kona_gpio->gpio_chip, gpio);
  322. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  323. }
  324. static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  325. {
  326. struct bcm_kona_gpio *kona_gpio;
  327. void __iomem *reg_base;
  328. unsigned gpio = d->hwirq;
  329. u32 lvl_type;
  330. u32 val;
  331. unsigned long flags;
  332. kona_gpio = irq_data_get_irq_chip_data(d);
  333. reg_base = kona_gpio->reg_base;
  334. switch (type & IRQ_TYPE_SENSE_MASK) {
  335. case IRQ_TYPE_EDGE_RISING:
  336. lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
  337. break;
  338. case IRQ_TYPE_EDGE_FALLING:
  339. lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
  340. break;
  341. case IRQ_TYPE_EDGE_BOTH:
  342. lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
  343. break;
  344. case IRQ_TYPE_LEVEL_HIGH:
  345. case IRQ_TYPE_LEVEL_LOW:
  346. /* BCM GPIO doesn't support level triggering */
  347. default:
  348. dev_err(kona_gpio->gpio_chip.parent,
  349. "Invalid BCM GPIO irq type 0x%x\n", type);
  350. return -EINVAL;
  351. }
  352. raw_spin_lock_irqsave(&kona_gpio->lock, flags);
  353. val = readl(reg_base + GPIO_CONTROL(gpio));
  354. val &= ~GPIO_GPCTR0_ITR_MASK;
  355. val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
  356. writel(val, reg_base + GPIO_CONTROL(gpio));
  357. raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
  358. return 0;
  359. }
  360. static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
  361. {
  362. void __iomem *reg_base;
  363. int bit, bank_id;
  364. unsigned long sta;
  365. struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
  366. struct irq_chip *chip = irq_desc_get_chip(desc);
  367. chained_irq_enter(chip, desc);
  368. /*
  369. * For bank interrupts, we can't use chip_data to store the kona_gpio
  370. * pointer, since GIC needs it for its own purposes. Therefore, we get
  371. * our pointer from the bank structure.
  372. */
  373. reg_base = bank->kona_gpio->reg_base;
  374. bank_id = bank->id;
  375. while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
  376. (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
  377. for_each_set_bit(bit, &sta, 32) {
  378. int hwirq = GPIO_PER_BANK * bank_id + bit;
  379. int child_irq =
  380. irq_find_mapping(bank->kona_gpio->irq_domain,
  381. hwirq);
  382. /*
  383. * Clear interrupt before handler is called so we don't
  384. * miss any interrupt occurred during executing them.
  385. */
  386. writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
  387. BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
  388. /* Invoke interrupt handler */
  389. generic_handle_irq(child_irq);
  390. }
  391. }
  392. chained_irq_exit(chip, desc);
  393. }
  394. static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
  395. {
  396. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  397. return gpiochip_reqres_irq(&kona_gpio->gpio_chip, d->hwirq);
  398. }
  399. static void bcm_kona_gpio_irq_relres(struct irq_data *d)
  400. {
  401. struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
  402. gpiochip_relres_irq(&kona_gpio->gpio_chip, d->hwirq);
  403. }
  404. static struct irq_chip bcm_gpio_irq_chip = {
  405. .name = "bcm-kona-gpio",
  406. .irq_ack = bcm_kona_gpio_irq_ack,
  407. .irq_mask = bcm_kona_gpio_irq_mask,
  408. .irq_unmask = bcm_kona_gpio_irq_unmask,
  409. .irq_set_type = bcm_kona_gpio_irq_set_type,
  410. .irq_request_resources = bcm_kona_gpio_irq_reqres,
  411. .irq_release_resources = bcm_kona_gpio_irq_relres,
  412. };
  413. static struct of_device_id const bcm_kona_gpio_of_match[] = {
  414. { .compatible = "brcm,kona-gpio" },
  415. {}
  416. };
  417. /*
  418. * This lock class tells lockdep that GPIO irqs are in a different
  419. * category than their parents, so it won't report false recursion.
  420. */
  421. static struct lock_class_key gpio_lock_class;
  422. static struct lock_class_key gpio_request_class;
  423. static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  424. irq_hw_number_t hwirq)
  425. {
  426. int ret;
  427. ret = irq_set_chip_data(irq, d->host_data);
  428. if (ret < 0)
  429. return ret;
  430. irq_set_lockdep_class(irq, &gpio_lock_class, &gpio_request_class);
  431. irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
  432. irq_set_noprobe(irq);
  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(reg_base, 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(reg_base, 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_kcalloc(dev,
  491. kona_gpio->num_bank,
  492. sizeof(*kona_gpio->banks),
  493. GFP_KERNEL);
  494. if (!kona_gpio->banks)
  495. return -ENOMEM;
  496. kona_gpio->pdev = pdev;
  497. platform_set_drvdata(pdev, kona_gpio);
  498. chip->of_node = dev->of_node;
  499. chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
  500. kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
  501. chip->ngpio,
  502. &bcm_kona_irq_ops,
  503. kona_gpio);
  504. if (!kona_gpio->irq_domain) {
  505. dev_err(dev, "Couldn't allocate IRQ domain\n");
  506. return -ENXIO;
  507. }
  508. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  509. kona_gpio->reg_base = devm_ioremap_resource(dev, res);
  510. if (IS_ERR(kona_gpio->reg_base)) {
  511. ret = -ENXIO;
  512. goto err_irq_domain;
  513. }
  514. for (i = 0; i < kona_gpio->num_bank; i++) {
  515. bank = &kona_gpio->banks[i];
  516. bank->id = i;
  517. bank->irq = platform_get_irq(pdev, i);
  518. bank->kona_gpio = kona_gpio;
  519. if (bank->irq < 0) {
  520. dev_err(dev, "Couldn't get IRQ for bank %d", i);
  521. ret = -ENOENT;
  522. goto err_irq_domain;
  523. }
  524. }
  525. dev_info(&pdev->dev, "Setting up Kona GPIO\n");
  526. bcm_kona_gpio_reset(kona_gpio);
  527. ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
  528. if (ret < 0) {
  529. dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
  530. goto err_irq_domain;
  531. }
  532. for (i = 0; i < kona_gpio->num_bank; i++) {
  533. bank = &kona_gpio->banks[i];
  534. irq_set_chained_handler_and_data(bank->irq,
  535. bcm_kona_gpio_irq_handler,
  536. bank);
  537. }
  538. raw_spin_lock_init(&kona_gpio->lock);
  539. return 0;
  540. err_irq_domain:
  541. irq_domain_remove(kona_gpio->irq_domain);
  542. return ret;
  543. }
  544. static struct platform_driver bcm_kona_gpio_driver = {
  545. .driver = {
  546. .name = "bcm-kona-gpio",
  547. .of_match_table = bcm_kona_gpio_of_match,
  548. },
  549. .probe = bcm_kona_gpio_probe,
  550. };
  551. builtin_platform_driver(bcm_kona_gpio_driver);