0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/init.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/gpio/driver.h>
0011 #include <linux/errno.h>
0012 #include <linux/io.h>
0013 #include <asm/txx9pio.h>
0014
0015 static DEFINE_SPINLOCK(txx9_gpio_lock);
0016
0017 static struct txx9_pio_reg __iomem *txx9_pioptr;
0018
0019 static int txx9_gpio_get(struct gpio_chip *chip, unsigned int offset)
0020 {
0021 return !!(__raw_readl(&txx9_pioptr->din) & (1 << offset));
0022 }
0023
0024 static void txx9_gpio_set_raw(unsigned int offset, int value)
0025 {
0026 u32 val;
0027 val = __raw_readl(&txx9_pioptr->dout);
0028 if (value)
0029 val |= 1 << offset;
0030 else
0031 val &= ~(1 << offset);
0032 __raw_writel(val, &txx9_pioptr->dout);
0033 }
0034
0035 static void txx9_gpio_set(struct gpio_chip *chip, unsigned int offset,
0036 int value)
0037 {
0038 unsigned long flags;
0039 spin_lock_irqsave(&txx9_gpio_lock, flags);
0040 txx9_gpio_set_raw(offset, value);
0041 mmiowb();
0042 spin_unlock_irqrestore(&txx9_gpio_lock, flags);
0043 }
0044
0045 static int txx9_gpio_dir_in(struct gpio_chip *chip, unsigned int offset)
0046 {
0047 unsigned long flags;
0048 spin_lock_irqsave(&txx9_gpio_lock, flags);
0049 __raw_writel(__raw_readl(&txx9_pioptr->dir) & ~(1 << offset),
0050 &txx9_pioptr->dir);
0051 mmiowb();
0052 spin_unlock_irqrestore(&txx9_gpio_lock, flags);
0053 return 0;
0054 }
0055
0056 static int txx9_gpio_dir_out(struct gpio_chip *chip, unsigned int offset,
0057 int value)
0058 {
0059 unsigned long flags;
0060 spin_lock_irqsave(&txx9_gpio_lock, flags);
0061 txx9_gpio_set_raw(offset, value);
0062 __raw_writel(__raw_readl(&txx9_pioptr->dir) | (1 << offset),
0063 &txx9_pioptr->dir);
0064 mmiowb();
0065 spin_unlock_irqrestore(&txx9_gpio_lock, flags);
0066 return 0;
0067 }
0068
0069 static struct gpio_chip txx9_gpio_chip = {
0070 .get = txx9_gpio_get,
0071 .set = txx9_gpio_set,
0072 .direction_input = txx9_gpio_dir_in,
0073 .direction_output = txx9_gpio_dir_out,
0074 .label = "TXx9",
0075 };
0076
0077 int __init txx9_gpio_init(unsigned long baseaddr,
0078 unsigned int base, unsigned int num)
0079 {
0080 txx9_pioptr = ioremap(baseaddr, sizeof(struct txx9_pio_reg));
0081 if (!txx9_pioptr)
0082 return -ENODEV;
0083 txx9_gpio_chip.base = base;
0084 txx9_gpio_chip.ngpio = num;
0085 return gpiochip_add_data(&txx9_gpio_chip, NULL);
0086 }