2 * Texas Instruments TNETV107X GPIO Controller
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/gpio.h>
18 #include <linux/platform_data/gpio-davinci.h>
20 #include <mach/common.h>
21 #include <mach/tnetv107x.h>
23 struct tnetv107x_gpio_regs {
31 #define gpio_reg_index(gpio) ((gpio) >> 5)
32 #define gpio_reg_bit(gpio) BIT((gpio) & 0x1f)
34 #define gpio_reg_rmw(reg, mask, val) \
35 __raw_writel((__raw_readl(reg) & ~(mask)) | (val), (reg))
37 #define gpio_reg_set_bit(reg, gpio) \
38 gpio_reg_rmw((reg) + gpio_reg_index(gpio), 0, gpio_reg_bit(gpio))
40 #define gpio_reg_clear_bit(reg, gpio) \
41 gpio_reg_rmw((reg) + gpio_reg_index(gpio), gpio_reg_bit(gpio), 0)
43 #define gpio_reg_get_bit(reg, gpio) \
44 (__raw_readl((reg) + gpio_reg_index(gpio)) & gpio_reg_bit(gpio))
46 #define chip2controller(chip) \
47 container_of(chip, struct davinci_gpio_controller, chip)
49 #define TNETV107X_GPIO_CTLRS DIV_ROUND_UP(TNETV107X_N_GPIO, 32)
51 static struct davinci_gpio_controller chips[TNETV107X_GPIO_CTLRS];
53 static int tnetv107x_gpio_request(struct gpio_chip *chip, unsigned offset)
55 struct davinci_gpio_controller *ctlr = chip2controller(chip);
56 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
57 unsigned gpio = chip->base + offset;
60 spin_lock_irqsave(&ctlr->lock, flags);
62 gpio_reg_set_bit(regs->enable, gpio);
64 spin_unlock_irqrestore(&ctlr->lock, flags);
69 static void tnetv107x_gpio_free(struct gpio_chip *chip, unsigned offset)
71 struct davinci_gpio_controller *ctlr = chip2controller(chip);
72 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
73 unsigned gpio = chip->base + offset;
76 spin_lock_irqsave(&ctlr->lock, flags);
78 gpio_reg_clear_bit(regs->enable, gpio);
80 spin_unlock_irqrestore(&ctlr->lock, flags);
83 static int tnetv107x_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
85 struct davinci_gpio_controller *ctlr = chip2controller(chip);
86 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
87 unsigned gpio = chip->base + offset;
90 spin_lock_irqsave(&ctlr->lock, flags);
92 gpio_reg_set_bit(regs->direction, gpio);
94 spin_unlock_irqrestore(&ctlr->lock, flags);
99 static int tnetv107x_gpio_dir_out(struct gpio_chip *chip,
100 unsigned offset, int value)
102 struct davinci_gpio_controller *ctlr = chip2controller(chip);
103 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
104 unsigned gpio = chip->base + offset;
107 spin_lock_irqsave(&ctlr->lock, flags);
110 gpio_reg_set_bit(regs->data_out, gpio);
112 gpio_reg_clear_bit(regs->data_out, gpio);
114 gpio_reg_clear_bit(regs->direction, gpio);
116 spin_unlock_irqrestore(&ctlr->lock, flags);
121 static int tnetv107x_gpio_get(struct gpio_chip *chip, unsigned offset)
123 struct davinci_gpio_controller *ctlr = chip2controller(chip);
124 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
125 unsigned gpio = chip->base + offset;
128 ret = gpio_reg_get_bit(regs->data_in, gpio);
133 static void tnetv107x_gpio_set(struct gpio_chip *chip,
134 unsigned offset, int value)
136 struct davinci_gpio_controller *ctlr = chip2controller(chip);
137 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
138 unsigned gpio = chip->base + offset;
141 spin_lock_irqsave(&ctlr->lock, flags);
144 gpio_reg_set_bit(regs->data_out, gpio);
146 gpio_reg_clear_bit(regs->data_out, gpio);
148 spin_unlock_irqrestore(&ctlr->lock, flags);
151 static int __init tnetv107x_gpio_setup(void)
155 struct davinci_soc_info *soc_info = &davinci_soc_info;
156 struct tnetv107x_gpio_regs *regs;
157 struct davinci_gpio_controller *ctlr;
159 if (soc_info->gpio_type != GPIO_TYPE_TNETV107X)
162 ngpio = soc_info->gpio_num;
164 pr_err("GPIO setup: how many GPIOs?\n");
168 if (WARN_ON(TNETV107X_N_GPIO < ngpio))
169 ngpio = TNETV107X_N_GPIO;
171 regs = ioremap(soc_info->gpio_base, SZ_4K);
175 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
178 ctlr->chip.label = "tnetv107x";
179 ctlr->chip.can_sleep = false;
180 ctlr->chip.base = base;
181 ctlr->chip.ngpio = ngpio - base;
182 if (ctlr->chip.ngpio > 32)
183 ctlr->chip.ngpio = 32;
185 ctlr->chip.request = tnetv107x_gpio_request;
186 ctlr->chip.free = tnetv107x_gpio_free;
187 ctlr->chip.direction_input = tnetv107x_gpio_dir_in;
188 ctlr->chip.get = tnetv107x_gpio_get;
189 ctlr->chip.direction_output = tnetv107x_gpio_dir_out;
190 ctlr->chip.set = tnetv107x_gpio_set;
192 spin_lock_init(&ctlr->lock);
195 ctlr->set_data = ®s->data_out[i];
196 ctlr->clr_data = ®s->data_out[i];
197 ctlr->in_data = ®s->data_in[i];
199 gpiochip_add(&ctlr->chip);
202 soc_info->gpio_ctlrs = chips;
203 soc_info->gpio_ctlrs_num = DIV_ROUND_UP(ngpio, 32);
206 pure_initcall(tnetv107x_gpio_setup);