gpio_txx9.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * A gpio chip driver for TXx9 SoCs
  4. *
  5. * Copyright (C) 2008 Atsushi Nemoto <[email protected]>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/errno.h>
  11. #include <linux/io.h>
  12. #include <asm/txx9pio.h>
  13. static DEFINE_SPINLOCK(txx9_gpio_lock);
  14. static struct txx9_pio_reg __iomem *txx9_pioptr;
  15. static int txx9_gpio_get(struct gpio_chip *chip, unsigned int offset)
  16. {
  17. return !!(__raw_readl(&txx9_pioptr->din) & (1 << offset));
  18. }
  19. static void txx9_gpio_set_raw(unsigned int offset, int value)
  20. {
  21. u32 val;
  22. val = __raw_readl(&txx9_pioptr->dout);
  23. if (value)
  24. val |= 1 << offset;
  25. else
  26. val &= ~(1 << offset);
  27. __raw_writel(val, &txx9_pioptr->dout);
  28. }
  29. static void txx9_gpio_set(struct gpio_chip *chip, unsigned int offset,
  30. int value)
  31. {
  32. unsigned long flags;
  33. spin_lock_irqsave(&txx9_gpio_lock, flags);
  34. txx9_gpio_set_raw(offset, value);
  35. mmiowb();
  36. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  37. }
  38. static int txx9_gpio_dir_in(struct gpio_chip *chip, unsigned int offset)
  39. {
  40. unsigned long flags;
  41. spin_lock_irqsave(&txx9_gpio_lock, flags);
  42. __raw_writel(__raw_readl(&txx9_pioptr->dir) & ~(1 << offset),
  43. &txx9_pioptr->dir);
  44. mmiowb();
  45. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  46. return 0;
  47. }
  48. static int txx9_gpio_dir_out(struct gpio_chip *chip, unsigned int offset,
  49. int value)
  50. {
  51. unsigned long flags;
  52. spin_lock_irqsave(&txx9_gpio_lock, flags);
  53. txx9_gpio_set_raw(offset, value);
  54. __raw_writel(__raw_readl(&txx9_pioptr->dir) | (1 << offset),
  55. &txx9_pioptr->dir);
  56. mmiowb();
  57. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  58. return 0;
  59. }
  60. static struct gpio_chip txx9_gpio_chip = {
  61. .get = txx9_gpio_get,
  62. .set = txx9_gpio_set,
  63. .direction_input = txx9_gpio_dir_in,
  64. .direction_output = txx9_gpio_dir_out,
  65. .label = "TXx9",
  66. };
  67. int __init txx9_gpio_init(unsigned long baseaddr,
  68. unsigned int base, unsigned int num)
  69. {
  70. txx9_pioptr = ioremap(baseaddr, sizeof(struct txx9_pio_reg));
  71. if (!txx9_pioptr)
  72. return -ENODEV;
  73. txx9_gpio_chip.base = base;
  74. txx9_gpio_chip.ngpio = num;
  75. return gpiochip_add_data(&txx9_gpio_chip, NULL);
  76. }