gpio_txx9.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/driver.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. unsigned long flags;
  44. spin_lock_irqsave(&txx9_gpio_lock, flags);
  45. __raw_writel(__raw_readl(&txx9_pioptr->dir) & ~(1 << offset),
  46. &txx9_pioptr->dir);
  47. mmiowb();
  48. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  49. return 0;
  50. }
  51. static int txx9_gpio_dir_out(struct gpio_chip *chip, unsigned int offset,
  52. int value)
  53. {
  54. unsigned long flags;
  55. spin_lock_irqsave(&txx9_gpio_lock, flags);
  56. txx9_gpio_set_raw(offset, value);
  57. __raw_writel(__raw_readl(&txx9_pioptr->dir) | (1 << offset),
  58. &txx9_pioptr->dir);
  59. mmiowb();
  60. spin_unlock_irqrestore(&txx9_gpio_lock, flags);
  61. return 0;
  62. }
  63. static struct gpio_chip txx9_gpio_chip = {
  64. .get = txx9_gpio_get,
  65. .set = txx9_gpio_set,
  66. .direction_input = txx9_gpio_dir_in,
  67. .direction_output = txx9_gpio_dir_out,
  68. .label = "TXx9",
  69. };
  70. int __init txx9_gpio_init(unsigned long baseaddr,
  71. unsigned int base, unsigned int num)
  72. {
  73. txx9_pioptr = ioremap(baseaddr, sizeof(struct txx9_pio_reg));
  74. if (!txx9_pioptr)
  75. return -ENODEV;
  76. txx9_gpio_chip.base = base;
  77. txx9_gpio_chip.ngpio = num;
  78. return gpiochip_add_data(&txx9_gpio_chip, NULL);
  79. }