gpio_txx9.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * A gpio chip driver for TXx9 SoCs
  3. *
  4. * Copyright (C) 2008 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/gpio.h>
  13. #include <linux/errno.h>
  14. #include <linux/io.h>
  15. #include <asm/txx9pio.h>
  16. static DEFINE_SPINLOCK(txx9_gpio_lock);
  17. static struct txx9_pio_reg __iomem *txx9_pioptr;
  18. static int txx9_gpio_get(struct gpio_chip *chip, unsigned int offset)
  19. {
  20. return __raw_readl(&txx9_pioptr->din) & (1 << offset);
  21. }
  22. static void txx9_gpio_set_raw(unsigned int offset, int value)
  23. {
  24. u32 val;
  25. val = __raw_readl(&txx9_pioptr->dout);
  26. if (value)
  27. val |= 1 << offset;
  28. else
  29. val &= ~(1 << offset);
  30. __raw_writel(val, &txx9_pioptr->dout);
  31. }
  32. static void txx9_gpio_set(struct gpio_chip *chip, unsigned int offset,
  33. int value)
  34. {
  35. unsigned long flags;
  36. spin_lock_irqsave(&txx9_gpio_lock, flags);
  37. txx9_gpio_set_raw(offset, value);
  38. mmiowb();
  39. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  40. }
  41. static int txx9_gpio_dir_in(struct gpio_chip *chip, unsigned int offset)
  42. {
  43. spin_lock_irq(&txx9_gpio_lock);
  44. __raw_writel(__raw_readl(&txx9_pioptr->dir) & ~(1 << offset),
  45. &txx9_pioptr->dir);
  46. mmiowb();
  47. spin_unlock_irq(&txx9_gpio_lock);
  48. return 0;
  49. }
  50. static int txx9_gpio_dir_out(struct gpio_chip *chip, unsigned int offset,
  51. int value)
  52. {
  53. spin_lock_irq(&txx9_gpio_lock);
  54. txx9_gpio_set_raw(offset, value);
  55. __raw_writel(__raw_readl(&txx9_pioptr->dir) | (1 << offset),
  56. &txx9_pioptr->dir);
  57. mmiowb();
  58. spin_unlock_irq(&txx9_gpio_lock);
  59. return 0;
  60. }
  61. static struct gpio_chip txx9_gpio_chip = {
  62. .get = txx9_gpio_get,
  63. .set = txx9_gpio_set,
  64. .direction_input = txx9_gpio_dir_in,
  65. .direction_output = txx9_gpio_dir_out,
  66. .label = "TXx9",
  67. };
  68. int __init txx9_gpio_init(unsigned long baseaddr,
  69. unsigned int base, unsigned int num)
  70. {
  71. txx9_pioptr = ioremap(baseaddr, sizeof(struct txx9_pio_reg));
  72. if (!txx9_pioptr)
  73. return -ENODEV;
  74. txx9_gpio_chip.base = base;
  75. txx9_gpio_chip.ngpio = num;
  76. return gpiochip_add(&txx9_gpio_chip);
  77. }