8 #include <linux/module.h>
11 #include <linux/slab.h>
23 #define CACHE_NR_REGS 3
24 #define CACHE_NR_BANKS (STMPE_NR_GPIOS / 8)
40 static inline struct stmpe_gpio *to_stmpe_gpio(
struct gpio_chip *
chip)
45 static int stmpe_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
50 u8 mask = 1 << (offset % 8);
57 return !!(ret &
mask);
60 static void stmpe_gpio_set(
struct gpio_chip *chip,
unsigned offset,
int val)
62 struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
63 struct stmpe *stmpe = stmpe_gpio->
stmpe;
65 u8 reg = stmpe->
regs[which] - (offset / 8);
66 u8 mask = 1 << (offset % 8);
78 static int stmpe_gpio_direction_output(
struct gpio_chip *chip,
79 unsigned offset,
int val)
81 struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
82 struct stmpe *stmpe = stmpe_gpio->
stmpe;
84 u8 mask = 1 << (offset % 8);
86 stmpe_gpio_set(chip, offset, val);
91 static int stmpe_gpio_direction_input(
struct gpio_chip *chip,
94 struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
95 struct stmpe *stmpe = stmpe_gpio->
stmpe;
97 u8 mask = 1 << (offset % 8);
102 static int stmpe_gpio_to_irq(
struct gpio_chip *chip,
unsigned offset)
104 struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
109 static int stmpe_gpio_request(
struct gpio_chip *chip,
unsigned offset)
111 struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
112 struct stmpe *stmpe = stmpe_gpio->
stmpe;
120 static struct gpio_chip template_chip = {
123 .direction_input = stmpe_gpio_direction_input,
124 .get = stmpe_gpio_get,
125 .direction_output = stmpe_gpio_direction_output,
126 .set = stmpe_gpio_set,
127 .to_irq = stmpe_gpio_to_irq,
128 .request = stmpe_gpio_request,
132 static int stmpe_gpio_irq_set_type(
struct irq_data *
d,
unsigned int type)
134 struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
136 int regoffset = offset / 8;
137 int mask = 1 << (offset % 8);
159 static void stmpe_gpio_irq_lock(
struct irq_data *d)
161 struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
166 static void stmpe_gpio_irq_sync_unlock(
struct irq_data *d)
168 struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
169 struct stmpe *stmpe = stmpe_gpio->
stmpe;
184 for (j = 0; j < num_banks; j++) {
199 static void stmpe_gpio_irq_mask(
struct irq_data *d)
201 struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
203 int regoffset = offset / 8;
204 int mask = 1 << (offset % 8);
209 static void stmpe_gpio_irq_unmask(
struct irq_data *d)
211 struct stmpe_gpio *stmpe_gpio = irq_data_get_irq_chip_data(d);
213 int regoffset = offset / 8;
214 int mask = 1 << (offset % 8);
219 static struct irq_chip stmpe_gpio_irq_chip = {
220 .name =
"stmpe-gpio",
221 .irq_bus_lock = stmpe_gpio_irq_lock,
222 .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
223 .irq_mask = stmpe_gpio_irq_mask,
224 .irq_unmask = stmpe_gpio_irq_unmask,
225 .irq_set_type = stmpe_gpio_irq_set_type,
230 struct stmpe_gpio *stmpe_gpio =
dev;
231 struct stmpe *stmpe = stmpe_gpio->
stmpe;
242 for (i = 0; i < num_banks; i++) {
243 int bank = num_banks - i - 1;
245 unsigned int stat = status[
i];
270 static int __devinit stmpe_gpio_irq_init(
struct stmpe_gpio *stmpe_gpio)
275 for (irq = base; irq < base + stmpe_gpio->
chip.ngpio; irq++) {
277 irq_set_chip_and_handler(irq, &stmpe_gpio_irq_chip,
279 irq_set_nested_thread(irq, 1);
283 irq_set_noprobe(irq);
290 static void stmpe_gpio_irq_remove(
struct stmpe_gpio *stmpe_gpio)
295 for (irq = base; irq < base + stmpe_gpio->
chip.ngpio; irq++) {
299 irq_set_chip_and_handler(irq,
NULL,
NULL);
308 struct stmpe_gpio *stmpe_gpio;
312 pdata = stmpe->
pdata->gpio;
316 stmpe_gpio = kzalloc(
sizeof(
struct stmpe_gpio),
GFP_KERNEL);
322 stmpe_gpio->
dev = &pdev->
dev;
326 stmpe_gpio->
chip = template_chip;
328 stmpe_gpio->
chip.dev = &pdev->
dev;
335 "device configured in no-irq mode; "
336 "irqs are not available\n");
343 ret = stmpe_gpio_irq_init(stmpe_gpio);
350 dev_err(&pdev->
dev,
"unable to get irq: %d\n", ret);
357 dev_err(&pdev->
dev,
"unable to add gpiochip: %d\n", ret);
361 if (pdata && pdata->
setup)
362 pdata->
setup(stmpe, stmpe_gpio->
chip.base);
364 platform_set_drvdata(pdev, stmpe_gpio);
373 stmpe_gpio_irq_remove(stmpe_gpio);
383 struct stmpe_gpio *stmpe_gpio = platform_get_drvdata(pdev);
384 struct stmpe *stmpe = stmpe_gpio->
stmpe;
389 if (pdata && pdata->
remove)
395 "unable to remove gpiochip: %d\n", ret);
403 stmpe_gpio_irq_remove(stmpe_gpio);
405 platform_set_drvdata(pdev,
NULL);
412 .driver.name =
"stmpe-gpio",
414 .probe = stmpe_gpio_probe,
418 static int __init stmpe_gpio_init(
void)
424 static void __exit stmpe_gpio_exit(
void)