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>
19 #include <mach/common.h>
20 #include <mach/tnetv107x.h>
22 struct tnetv107x_gpio_regs {
30 #define gpio_reg_index(gpio) ((gpio) >> 5)
31 #define gpio_reg_bit(gpio) BIT((gpio) & 0x1f)
33 #define gpio_reg_rmw(reg, mask, val) \
34 __raw_writel((__raw_readl(reg) & ~(mask)) | (val), (reg))
36 #define gpio_reg_set_bit(reg, gpio) \
37 gpio_reg_rmw((reg) + gpio_reg_index(gpio), 0, gpio_reg_bit(gpio))
39 #define gpio_reg_clear_bit(reg, gpio) \
40 gpio_reg_rmw((reg) + gpio_reg_index(gpio), gpio_reg_bit(gpio), 0)
42 #define gpio_reg_get_bit(reg, gpio) \
43 (__raw_readl((reg) + gpio_reg_index(gpio)) & gpio_reg_bit(gpio))
45 #define chip2controller(chip) \
46 container_of(chip, struct davinci_gpio_controller, chip)
48 #define TNETV107X_GPIO_CTLRS DIV_ROUND_UP(TNETV107X_N_GPIO, 32)
50 static struct davinci_gpio_controller chips[TNETV107X_GPIO_CTLRS];
52 static int tnetv107x_gpio_request(struct gpio_chip *chip, unsigned offset)
54 struct davinci_gpio_controller *ctlr = chip2controller(chip);
55 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
56 unsigned gpio = chip->base + offset;
59 spin_lock_irqsave(&ctlr->lock, flags);
61 gpio_reg_set_bit(®s->enable, gpio);
63 spin_unlock_irqrestore(&ctlr->lock, flags);
68 static void tnetv107x_gpio_free(struct gpio_chip *chip, unsigned offset)
70 struct davinci_gpio_controller *ctlr = chip2controller(chip);
71 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
72 unsigned gpio = chip->base + offset;
75 spin_lock_irqsave(&ctlr->lock, flags);
77 gpio_reg_clear_bit(®s->enable, gpio);
79 spin_unlock_irqrestore(&ctlr->lock, flags);
82 static int tnetv107x_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
84 struct davinci_gpio_controller *ctlr = chip2controller(chip);
85 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
86 unsigned gpio = chip->base + offset;
89 spin_lock_irqsave(&ctlr->lock, flags);
91 gpio_reg_set_bit(®s->direction, gpio);
93 spin_unlock_irqrestore(&ctlr->lock, flags);
98 static int tnetv107x_gpio_dir_out(struct gpio_chip *chip,
99 unsigned offset, int value)
101 struct davinci_gpio_controller *ctlr = chip2controller(chip);
102 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
103 unsigned gpio = chip->base + offset;
106 spin_lock_irqsave(&ctlr->lock, flags);
109 gpio_reg_set_bit(®s->data_out, gpio);
111 gpio_reg_clear_bit(®s->data_out, gpio);
113 gpio_reg_clear_bit(®s->direction, gpio);
115 spin_unlock_irqrestore(&ctlr->lock, flags);
120 static int tnetv107x_gpio_get(struct gpio_chip *chip, unsigned offset)
122 struct davinci_gpio_controller *ctlr = chip2controller(chip);
123 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
124 unsigned gpio = chip->base + offset;
127 ret = gpio_reg_get_bit(®s->data_in, gpio);
132 static void tnetv107x_gpio_set(struct gpio_chip *chip,
133 unsigned offset, int value)
135 struct davinci_gpio_controller *ctlr = chip2controller(chip);
136 struct tnetv107x_gpio_regs __iomem *regs = ctlr->regs;
137 unsigned gpio = chip->base + offset;
140 spin_lock_irqsave(&ctlr->lock, flags);
143 gpio_reg_set_bit(®s->data_out, gpio);
145 gpio_reg_clear_bit(®s->data_out, gpio);
147 spin_unlock_irqrestore(&ctlr->lock, flags);
150 static int __init tnetv107x_gpio_setup(void)
154 struct davinci_soc_info *soc_info = &davinci_soc_info;
155 struct tnetv107x_gpio_regs *regs;
156 struct davinci_gpio_controller *ctlr;
158 if (soc_info->gpio_type != GPIO_TYPE_TNETV107X)
161 ngpio = soc_info->gpio_num;
163 pr_err("GPIO setup: how many GPIOs?\n");
167 if (WARN_ON(TNETV107X_N_GPIO < ngpio))
168 ngpio = TNETV107X_N_GPIO;
170 regs = ioremap(soc_info->gpio_base, SZ_4K);
174 for (i = 0, base = 0; base < ngpio; i++, base += 32) {
177 ctlr->chip.label = "tnetv107x";
178 ctlr->chip.can_sleep = 0;
179 ctlr->chip.base = base;
180 ctlr->chip.ngpio = ngpio - base;
181 if (ctlr->chip.ngpio > 32)
182 ctlr->chip.ngpio = 32;
184 ctlr->chip.request = tnetv107x_gpio_request;
185 ctlr->chip.free = tnetv107x_gpio_free;
186 ctlr->chip.direction_input = tnetv107x_gpio_dir_in;
187 ctlr->chip.get = tnetv107x_gpio_get;
188 ctlr->chip.direction_output = tnetv107x_gpio_dir_out;
189 ctlr->chip.set = tnetv107x_gpio_set;
191 spin_lock_init(&ctlr->lock);
194 ctlr->set_data = ®s->data_out[i];
195 ctlr->clr_data = ®s->data_out[i];
196 ctlr->in_data = ®s->data_in[i];
198 gpiochip_add(&ctlr->chip);
201 soc_info->gpio_ctlrs = chips;
202 soc_info->gpio_ctlrs_num = DIV_ROUND_UP(ngpio, 32);
205 pure_initcall(tnetv107x_gpio_setup);