gpio-brcmstb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (C) 2015 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/gpio/driver.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/module.h>
  18. #include <linux/basic_mmio_gpio.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/reboot.h>
  23. #define GIO_BANK_SIZE 0x20
  24. #define GIO_ODEN(bank) (((bank) * GIO_BANK_SIZE) + 0x00)
  25. #define GIO_DATA(bank) (((bank) * GIO_BANK_SIZE) + 0x04)
  26. #define GIO_IODIR(bank) (((bank) * GIO_BANK_SIZE) + 0x08)
  27. #define GIO_EC(bank) (((bank) * GIO_BANK_SIZE) + 0x0c)
  28. #define GIO_EI(bank) (((bank) * GIO_BANK_SIZE) + 0x10)
  29. #define GIO_MASK(bank) (((bank) * GIO_BANK_SIZE) + 0x14)
  30. #define GIO_LEVEL(bank) (((bank) * GIO_BANK_SIZE) + 0x18)
  31. #define GIO_STAT(bank) (((bank) * GIO_BANK_SIZE) + 0x1c)
  32. struct brcmstb_gpio_bank {
  33. struct list_head node;
  34. int id;
  35. struct bgpio_chip bgc;
  36. struct brcmstb_gpio_priv *parent_priv;
  37. u32 width;
  38. struct irq_chip irq_chip;
  39. };
  40. struct brcmstb_gpio_priv {
  41. struct list_head bank_list;
  42. void __iomem *reg_base;
  43. struct platform_device *pdev;
  44. int parent_irq;
  45. int gpio_base;
  46. bool can_wake;
  47. int parent_wake_irq;
  48. struct notifier_block reboot_notifier;
  49. };
  50. #define MAX_GPIO_PER_BANK 32
  51. #define GPIO_BANK(gpio) ((gpio) >> 5)
  52. /* assumes MAX_GPIO_PER_BANK is a multiple of 2 */
  53. #define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1))
  54. static inline struct brcmstb_gpio_bank *
  55. brcmstb_gpio_gc_to_bank(struct gpio_chip *gc)
  56. {
  57. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  58. return container_of(bgc, struct brcmstb_gpio_bank, bgc);
  59. }
  60. static inline struct brcmstb_gpio_priv *
  61. brcmstb_gpio_gc_to_priv(struct gpio_chip *gc)
  62. {
  63. struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc);
  64. return bank->parent_priv;
  65. }
  66. static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
  67. unsigned int offset, bool enable)
  68. {
  69. struct bgpio_chip *bgc = &bank->bgc;
  70. struct brcmstb_gpio_priv *priv = bank->parent_priv;
  71. u32 mask = bgc->pin2mask(bgc, offset);
  72. u32 imask;
  73. unsigned long flags;
  74. spin_lock_irqsave(&bgc->lock, flags);
  75. imask = bgc->read_reg(priv->reg_base + GIO_MASK(bank->id));
  76. if (enable)
  77. imask |= mask;
  78. else
  79. imask &= ~mask;
  80. bgc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask);
  81. spin_unlock_irqrestore(&bgc->lock, flags);
  82. }
  83. /* -------------------- IRQ chip functions -------------------- */
  84. static void brcmstb_gpio_irq_mask(struct irq_data *d)
  85. {
  86. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  87. struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc);
  88. brcmstb_gpio_set_imask(bank, d->hwirq, false);
  89. }
  90. static void brcmstb_gpio_irq_unmask(struct irq_data *d)
  91. {
  92. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  93. struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc);
  94. brcmstb_gpio_set_imask(bank, d->hwirq, true);
  95. }
  96. static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  97. {
  98. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  99. struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc);
  100. struct brcmstb_gpio_priv *priv = bank->parent_priv;
  101. u32 mask = BIT(d->hwirq);
  102. u32 edge_insensitive, iedge_insensitive;
  103. u32 edge_config, iedge_config;
  104. u32 level, ilevel;
  105. unsigned long flags;
  106. switch (type) {
  107. case IRQ_TYPE_LEVEL_LOW:
  108. level = 0;
  109. edge_config = 0;
  110. edge_insensitive = 0;
  111. break;
  112. case IRQ_TYPE_LEVEL_HIGH:
  113. level = mask;
  114. edge_config = 0;
  115. edge_insensitive = 0;
  116. break;
  117. case IRQ_TYPE_EDGE_FALLING:
  118. level = 0;
  119. edge_config = 0;
  120. edge_insensitive = 0;
  121. break;
  122. case IRQ_TYPE_EDGE_RISING:
  123. level = 0;
  124. edge_config = mask;
  125. edge_insensitive = 0;
  126. break;
  127. case IRQ_TYPE_EDGE_BOTH:
  128. level = 0;
  129. edge_config = 0; /* don't care, but want known value */
  130. edge_insensitive = mask;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. spin_lock_irqsave(&bank->bgc.lock, flags);
  136. iedge_config = bank->bgc.read_reg(priv->reg_base +
  137. GIO_EC(bank->id)) & ~mask;
  138. iedge_insensitive = bank->bgc.read_reg(priv->reg_base +
  139. GIO_EI(bank->id)) & ~mask;
  140. ilevel = bank->bgc.read_reg(priv->reg_base +
  141. GIO_LEVEL(bank->id)) & ~mask;
  142. bank->bgc.write_reg(priv->reg_base + GIO_EC(bank->id),
  143. iedge_config | edge_config);
  144. bank->bgc.write_reg(priv->reg_base + GIO_EI(bank->id),
  145. iedge_insensitive | edge_insensitive);
  146. bank->bgc.write_reg(priv->reg_base + GIO_LEVEL(bank->id),
  147. ilevel | level);
  148. spin_unlock_irqrestore(&bank->bgc.lock, flags);
  149. return 0;
  150. }
  151. static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
  152. unsigned int enable)
  153. {
  154. int ret = 0;
  155. /*
  156. * Only enable wake IRQ once for however many hwirqs can wake
  157. * since they all use the same wake IRQ. Mask will be set
  158. * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
  159. */
  160. if (enable)
  161. ret = enable_irq_wake(priv->parent_wake_irq);
  162. else
  163. ret = disable_irq_wake(priv->parent_wake_irq);
  164. if (ret)
  165. dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n",
  166. enable ? "enable" : "disable");
  167. return ret;
  168. }
  169. static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
  170. {
  171. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  172. struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
  173. return brcmstb_gpio_priv_set_wake(priv, enable);
  174. }
  175. static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
  176. {
  177. struct brcmstb_gpio_priv *priv = data;
  178. if (!priv || irq != priv->parent_wake_irq)
  179. return IRQ_NONE;
  180. pm_wakeup_event(&priv->pdev->dev, 0);
  181. return IRQ_HANDLED;
  182. }
  183. static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
  184. {
  185. struct brcmstb_gpio_priv *priv = bank->parent_priv;
  186. struct irq_domain *irq_domain = bank->bgc.gc.irqdomain;
  187. void __iomem *reg_base = priv->reg_base;
  188. unsigned long status;
  189. unsigned long flags;
  190. spin_lock_irqsave(&bank->bgc.lock, flags);
  191. while ((status = bank->bgc.read_reg(reg_base + GIO_STAT(bank->id)) &
  192. bank->bgc.read_reg(reg_base + GIO_MASK(bank->id)))) {
  193. int bit;
  194. for_each_set_bit(bit, &status, 32) {
  195. u32 stat = bank->bgc.read_reg(reg_base +
  196. GIO_STAT(bank->id));
  197. if (bit >= bank->width)
  198. dev_warn(&priv->pdev->dev,
  199. "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
  200. bank->id, bit);
  201. bank->bgc.write_reg(reg_base + GIO_STAT(bank->id),
  202. stat | BIT(bit));
  203. generic_handle_irq(irq_find_mapping(irq_domain, bit));
  204. }
  205. }
  206. spin_unlock_irqrestore(&bank->bgc.lock, flags);
  207. }
  208. /* Each UPG GIO block has one IRQ for all banks */
  209. static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
  210. {
  211. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  212. struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
  213. struct irq_chip *chip = irq_desc_get_chip(desc);
  214. struct list_head *pos;
  215. /* Interrupts weren't properly cleared during probe */
  216. BUG_ON(!priv || !chip);
  217. chained_irq_enter(chip, desc);
  218. list_for_each(pos, &priv->bank_list) {
  219. struct brcmstb_gpio_bank *bank =
  220. list_entry(pos, struct brcmstb_gpio_bank, node);
  221. brcmstb_gpio_irq_bank_handler(bank);
  222. }
  223. chained_irq_exit(chip, desc);
  224. }
  225. static int brcmstb_gpio_reboot(struct notifier_block *nb,
  226. unsigned long action, void *data)
  227. {
  228. struct brcmstb_gpio_priv *priv =
  229. container_of(nb, struct brcmstb_gpio_priv, reboot_notifier);
  230. /* Enable GPIO for S5 cold boot */
  231. if (action == SYS_POWER_OFF)
  232. brcmstb_gpio_priv_set_wake(priv, 1);
  233. return NOTIFY_DONE;
  234. }
  235. /* Make sure that the number of banks matches up between properties */
  236. static int brcmstb_gpio_sanity_check_banks(struct device *dev,
  237. struct device_node *np, struct resource *res)
  238. {
  239. int res_num_banks = resource_size(res) / GIO_BANK_SIZE;
  240. int num_banks =
  241. of_property_count_u32_elems(np, "brcm,gpio-bank-widths");
  242. if (res_num_banks != num_banks) {
  243. dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n",
  244. res_num_banks, num_banks);
  245. return -EINVAL;
  246. } else {
  247. return 0;
  248. }
  249. }
  250. static int brcmstb_gpio_remove(struct platform_device *pdev)
  251. {
  252. struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
  253. struct list_head *pos;
  254. struct brcmstb_gpio_bank *bank;
  255. int ret = 0;
  256. if (!priv) {
  257. dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
  258. return -EFAULT;
  259. }
  260. /*
  261. * You can lose return values below, but we report all errors, and it's
  262. * more important to actually perform all of the steps.
  263. */
  264. list_for_each(pos, &priv->bank_list) {
  265. bank = list_entry(pos, struct brcmstb_gpio_bank, node);
  266. ret = bgpio_remove(&bank->bgc);
  267. if (ret)
  268. dev_err(&pdev->dev, "gpiochip_remove fail in cleanup\n");
  269. }
  270. if (priv->reboot_notifier.notifier_call) {
  271. ret = unregister_reboot_notifier(&priv->reboot_notifier);
  272. if (ret)
  273. dev_err(&pdev->dev,
  274. "failed to unregister reboot notifier\n");
  275. }
  276. return ret;
  277. }
  278. static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
  279. const struct of_phandle_args *gpiospec, u32 *flags)
  280. {
  281. struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
  282. struct brcmstb_gpio_bank *bank = brcmstb_gpio_gc_to_bank(gc);
  283. int offset;
  284. if (gc->of_gpio_n_cells != 2) {
  285. WARN_ON(1);
  286. return -EINVAL;
  287. }
  288. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  289. return -EINVAL;
  290. offset = gpiospec->args[0] - (gc->base - priv->gpio_base);
  291. if (offset >= gc->ngpio || offset < 0)
  292. return -EINVAL;
  293. if (unlikely(offset >= bank->width)) {
  294. dev_warn_ratelimited(&priv->pdev->dev,
  295. "Received request for invalid GPIO offset %d\n",
  296. gpiospec->args[0]);
  297. }
  298. if (flags)
  299. *flags = gpiospec->args[1];
  300. return offset;
  301. }
  302. /* Before calling, must have bank->parent_irq set and gpiochip registered */
  303. static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
  304. struct brcmstb_gpio_bank *bank)
  305. {
  306. struct brcmstb_gpio_priv *priv = bank->parent_priv;
  307. struct device *dev = &pdev->dev;
  308. struct device_node *np = dev->of_node;
  309. bank->irq_chip.name = dev_name(dev);
  310. bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
  311. bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
  312. bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
  313. /* Ensures that all non-wakeup IRQs are disabled at suspend */
  314. bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  315. if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->can_wake &&
  316. of_property_read_bool(np, "wakeup-source")) {
  317. priv->parent_wake_irq = platform_get_irq(pdev, 1);
  318. if (priv->parent_wake_irq < 0) {
  319. dev_warn(dev,
  320. "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
  321. } else {
  322. int err;
  323. /*
  324. * Set wakeup capability before requesting wakeup
  325. * interrupt, so we can process boot-time "wakeups"
  326. * (e.g., from S5 cold boot)
  327. */
  328. device_set_wakeup_capable(dev, true);
  329. device_wakeup_enable(dev);
  330. err = devm_request_irq(dev, priv->parent_wake_irq,
  331. brcmstb_gpio_wake_irq_handler, 0,
  332. "brcmstb-gpio-wake", priv);
  333. if (err < 0) {
  334. dev_err(dev, "Couldn't request wake IRQ");
  335. return err;
  336. }
  337. priv->reboot_notifier.notifier_call =
  338. brcmstb_gpio_reboot;
  339. register_reboot_notifier(&priv->reboot_notifier);
  340. priv->can_wake = true;
  341. }
  342. }
  343. if (priv->can_wake)
  344. bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
  345. gpiochip_irqchip_add(&bank->bgc.gc, &bank->irq_chip, 0,
  346. handle_simple_irq, IRQ_TYPE_NONE);
  347. gpiochip_set_chained_irqchip(&bank->bgc.gc, &bank->irq_chip,
  348. priv->parent_irq, brcmstb_gpio_irq_handler);
  349. return 0;
  350. }
  351. static int brcmstb_gpio_probe(struct platform_device *pdev)
  352. {
  353. struct device *dev = &pdev->dev;
  354. struct device_node *np = dev->of_node;
  355. void __iomem *reg_base;
  356. struct brcmstb_gpio_priv *priv;
  357. struct resource *res;
  358. struct property *prop;
  359. const __be32 *p;
  360. u32 bank_width;
  361. int num_banks = 0;
  362. int err;
  363. static int gpio_base;
  364. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  365. if (!priv)
  366. return -ENOMEM;
  367. platform_set_drvdata(pdev, priv);
  368. INIT_LIST_HEAD(&priv->bank_list);
  369. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  370. reg_base = devm_ioremap_resource(dev, res);
  371. if (IS_ERR(reg_base))
  372. return PTR_ERR(reg_base);
  373. priv->gpio_base = gpio_base;
  374. priv->reg_base = reg_base;
  375. priv->pdev = pdev;
  376. if (of_property_read_bool(np, "interrupt-controller")) {
  377. priv->parent_irq = platform_get_irq(pdev, 0);
  378. if (priv->parent_irq <= 0) {
  379. dev_err(dev, "Couldn't get IRQ");
  380. return -ENOENT;
  381. }
  382. } else {
  383. priv->parent_irq = -ENOENT;
  384. }
  385. if (brcmstb_gpio_sanity_check_banks(dev, np, res))
  386. return -EINVAL;
  387. of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p,
  388. bank_width) {
  389. struct brcmstb_gpio_bank *bank;
  390. struct bgpio_chip *bgc;
  391. struct gpio_chip *gc;
  392. bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
  393. if (!bank) {
  394. err = -ENOMEM;
  395. goto fail;
  396. }
  397. bank->parent_priv = priv;
  398. bank->id = num_banks;
  399. if (bank_width <= 0 || bank_width > MAX_GPIO_PER_BANK) {
  400. dev_err(dev, "Invalid bank width %d\n", bank_width);
  401. goto fail;
  402. } else {
  403. bank->width = bank_width;
  404. }
  405. /*
  406. * Regs are 4 bytes wide, have data reg, no set/clear regs,
  407. * and direction bits have 0 = output and 1 = input
  408. */
  409. bgc = &bank->bgc;
  410. err = bgpio_init(bgc, dev, 4,
  411. reg_base + GIO_DATA(bank->id),
  412. NULL, NULL, NULL,
  413. reg_base + GIO_IODIR(bank->id), 0);
  414. if (err) {
  415. dev_err(dev, "bgpio_init() failed\n");
  416. goto fail;
  417. }
  418. gc = &bgc->gc;
  419. gc->of_node = np;
  420. gc->owner = THIS_MODULE;
  421. gc->label = np->full_name;
  422. gc->base = gpio_base;
  423. gc->of_gpio_n_cells = 2;
  424. gc->of_xlate = brcmstb_gpio_of_xlate;
  425. /* not all ngpio lines are valid, will use bank width later */
  426. gc->ngpio = MAX_GPIO_PER_BANK;
  427. /*
  428. * Mask all interrupts by default, since wakeup interrupts may
  429. * be retained from S5 cold boot
  430. */
  431. bank->bgc.write_reg(reg_base + GIO_MASK(bank->id), 0);
  432. err = gpiochip_add(gc);
  433. if (err) {
  434. dev_err(dev, "Could not add gpiochip for bank %d\n",
  435. bank->id);
  436. goto fail;
  437. }
  438. gpio_base += gc->ngpio;
  439. if (priv->parent_irq > 0) {
  440. err = brcmstb_gpio_irq_setup(pdev, bank);
  441. if (err)
  442. goto fail;
  443. }
  444. dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
  445. gc->base, gc->ngpio, bank->width);
  446. /* Everything looks good, so add bank to list */
  447. list_add(&bank->node, &priv->bank_list);
  448. num_banks++;
  449. }
  450. dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
  451. num_banks, priv->gpio_base, gpio_base - 1);
  452. return 0;
  453. fail:
  454. (void) brcmstb_gpio_remove(pdev);
  455. return err;
  456. }
  457. static const struct of_device_id brcmstb_gpio_of_match[] = {
  458. { .compatible = "brcm,brcmstb-gpio" },
  459. {},
  460. };
  461. MODULE_DEVICE_TABLE(of, brcmstb_gpio_of_match);
  462. static struct platform_driver brcmstb_gpio_driver = {
  463. .driver = {
  464. .name = "brcmstb-gpio",
  465. .of_match_table = brcmstb_gpio_of_match,
  466. },
  467. .probe = brcmstb_gpio_probe,
  468. .remove = brcmstb_gpio_remove,
  469. };
  470. module_platform_driver(brcmstb_gpio_driver);
  471. MODULE_AUTHOR("Gregory Fong");
  472. MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");
  473. MODULE_LICENSE("GPL v2");