gpio-brcmstb.c 15 KB

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