blob: c33c9a80e14934250637ecf31914a12b55d71c66 [file] [log] [blame]
Ryan Mallonb6850042008-04-16 02:56:35 +01001/*
Ryan Mallonb6850042008-04-16 02:56:35 +01002 * Generic EP93xx GPIO handling
3 *
4 * Copyright (c) 2008 Ryan Mallon <ryan@bluewatersys.com>
5 *
6 * Based on code originally from:
7 * linux/arch/arm/mach-ep93xx/core.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Hartley Sweetend056ab72010-02-23 21:41:17 +010014#define pr_fmt(fmt) "ep93xx " KBUILD_MODNAME ": " fmt
15
Ryan Mallonb6850042008-04-16 02:56:35 +010016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/seq_file.h>
Russell Kingfced80c2008-09-06 12:10:45 +010019#include <linux/io.h>
Hartley Sweetenddf4f3d2009-06-26 21:39:27 +010020#include <linux/gpio.h>
Ryan Mallon595c0502009-07-15 21:31:46 +010021#include <linux/irq.h>
Ryan Mallonb6850042008-04-16 02:56:35 +010022
Hartley Sweetenddf4f3d2009-06-26 21:39:27 +010023#include <mach/hardware.h>
Ryan Mallonb6850042008-04-16 02:56:35 +010024
Hartley Sweetend056ab72010-02-23 21:41:17 +010025/*************************************************************************
Hartley Sweeten47427232010-04-06 22:46:16 +010026 * Interrupt handling for EP93xx on-chip GPIOs
Hartley Sweetend056ab72010-02-23 21:41:17 +010027 *************************************************************************/
28static unsigned char gpio_int_unmasked[3];
29static unsigned char gpio_int_enabled[3];
30static unsigned char gpio_int_type1[3];
31static unsigned char gpio_int_type2[3];
32static unsigned char gpio_int_debounce[3];
33
34/* Port ordering is: A B F */
35static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
36static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
37static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
38static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
39static const u8 int_debounce_register_offset[3] = { 0xa8, 0xc4, 0x64 };
40
Hartley Sweeten47427232010-04-06 22:46:16 +010041static void ep93xx_gpio_update_int_params(unsigned port)
Hartley Sweetend056ab72010-02-23 21:41:17 +010042{
43 BUG_ON(port > 2);
44
45 __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
46
47 __raw_writeb(gpio_int_type2[port],
48 EP93XX_GPIO_REG(int_type2_register_offset[port]));
49
50 __raw_writeb(gpio_int_type1[port],
51 EP93XX_GPIO_REG(int_type1_register_offset[port]));
52
53 __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
54 EP93XX_GPIO_REG(int_en_register_offset[port]));
55}
56
Hartley Sweeten47427232010-04-06 22:46:16 +010057static inline void ep93xx_gpio_int_mask(unsigned line)
Hartley Sweetend056ab72010-02-23 21:41:17 +010058{
59 gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
60}
61
Hartley Sweeten5d046af2011-01-27 17:29:29 +010062static void ep93xx_gpio_int_debounce(unsigned int irq, bool enable)
Hartley Sweetend056ab72010-02-23 21:41:17 +010063{
64 int line = irq_to_gpio(irq);
65 int port = line >> 3;
66 int port_mask = 1 << (line & 7);
67
68 if (enable)
69 gpio_int_debounce[port] |= port_mask;
70 else
71 gpio_int_debounce[port] &= ~port_mask;
72
73 __raw_writeb(gpio_int_debounce[port],
74 EP93XX_GPIO_REG(int_debounce_register_offset[port]));
75}
Hartley Sweetend056ab72010-02-23 21:41:17 +010076
77static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
78{
79 unsigned char status;
80 int i;
81
82 status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
83 for (i = 0; i < 8; i++) {
84 if (status & (1 << i)) {
85 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
86 generic_handle_irq(gpio_irq);
87 }
88 }
89
90 status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
91 for (i = 0; i < 8; i++) {
92 if (status & (1 << i)) {
93 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
94 generic_handle_irq(gpio_irq);
95 }
96 }
97}
98
99static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
100{
101 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300102 * map discontiguous hw irq range to continuous sw irq range:
Hartley Sweetend056ab72010-02-23 21:41:17 +0100103 *
104 * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
105 */
106 int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
107 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
108
109 generic_handle_irq(gpio_irq);
110}
111
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100112static void ep93xx_gpio_irq_ack(struct irq_data *d)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100113{
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100114 int line = irq_to_gpio(d->irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100115 int port = line >> 3;
116 int port_mask = 1 << (line & 7);
117
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100118 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
Hartley Sweetend056ab72010-02-23 21:41:17 +0100119 gpio_int_type2[port] ^= port_mask; /* switch edge direction */
120 ep93xx_gpio_update_int_params(port);
121 }
122
123 __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
124}
125
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100126static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100127{
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100128 int line = irq_to_gpio(d->irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100129 int port = line >> 3;
130 int port_mask = 1 << (line & 7);
131
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100132 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100133 gpio_int_type2[port] ^= port_mask; /* switch edge direction */
134
135 gpio_int_unmasked[port] &= ~port_mask;
136 ep93xx_gpio_update_int_params(port);
137
138 __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
139}
140
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100141static void ep93xx_gpio_irq_mask(struct irq_data *d)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100142{
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100143 int line = irq_to_gpio(d->irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100144 int port = line >> 3;
145
146 gpio_int_unmasked[port] &= ~(1 << (line & 7));
147 ep93xx_gpio_update_int_params(port);
148}
149
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100150static void ep93xx_gpio_irq_unmask(struct irq_data *d)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100151{
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100152 int line = irq_to_gpio(d->irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100153 int port = line >> 3;
154
155 gpio_int_unmasked[port] |= 1 << (line & 7);
156 ep93xx_gpio_update_int_params(port);
157}
158
159/*
160 * gpio_int_type1 controls whether the interrupt is level (0) or
161 * edge (1) triggered, while gpio_int_type2 controls whether it
162 * triggers on low/falling (0) or high/rising (1).
163 */
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100164static int ep93xx_gpio_irq_type(struct irq_data *d, unsigned int type)
Hartley Sweetend056ab72010-02-23 21:41:17 +0100165{
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100166 const int gpio = irq_to_gpio(d->irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100167 const int port = gpio >> 3;
168 const int port_mask = 1 << (gpio & 7);
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100169 irq_flow_handler_t handler;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100170
171 gpio_direction_input(gpio);
172
173 switch (type) {
174 case IRQ_TYPE_EDGE_RISING:
175 gpio_int_type1[port] |= port_mask;
176 gpio_int_type2[port] |= port_mask;
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100177 handler = handle_edge_irq;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100178 break;
179 case IRQ_TYPE_EDGE_FALLING:
180 gpio_int_type1[port] |= port_mask;
181 gpio_int_type2[port] &= ~port_mask;
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100182 handler = handle_edge_irq;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100183 break;
184 case IRQ_TYPE_LEVEL_HIGH:
185 gpio_int_type1[port] &= ~port_mask;
186 gpio_int_type2[port] |= port_mask;
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100187 handler = handle_level_irq;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100188 break;
189 case IRQ_TYPE_LEVEL_LOW:
190 gpio_int_type1[port] &= ~port_mask;
191 gpio_int_type2[port] &= ~port_mask;
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100192 handler = handle_level_irq;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100193 break;
194 case IRQ_TYPE_EDGE_BOTH:
195 gpio_int_type1[port] |= port_mask;
196 /* set initial polarity based on current input level */
197 if (gpio_get_value(gpio))
198 gpio_int_type2[port] &= ~port_mask; /* falling */
199 else
200 gpio_int_type2[port] |= port_mask; /* rising */
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100201 handler = handle_edge_irq;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100202 break;
203 default:
204 pr_err("failed to set irq type %d for gpio %d\n", type, gpio);
205 return -EINVAL;
206 }
207
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100208 __irq_set_handler_locked(d->irq, handler);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100209
Thomas Gleixnerd1735a22011-03-24 12:45:56 +0100210 gpio_int_enabled[port] |= port_mask;
Hartley Sweetend056ab72010-02-23 21:41:17 +0100211
212 ep93xx_gpio_update_int_params(port);
213
214 return 0;
215}
216
217static struct irq_chip ep93xx_gpio_irq_chip = {
218 .name = "GPIO",
Lennert Buytenhekc0afc912010-11-29 10:29:50 +0100219 .irq_ack = ep93xx_gpio_irq_ack,
220 .irq_mask_ack = ep93xx_gpio_irq_mask_ack,
221 .irq_mask = ep93xx_gpio_irq_mask,
222 .irq_unmask = ep93xx_gpio_irq_unmask,
223 .irq_set_type = ep93xx_gpio_irq_type,
Hartley Sweetend056ab72010-02-23 21:41:17 +0100224};
225
226void __init ep93xx_gpio_init_irq(void)
227{
228 int gpio_irq;
229
230 for (gpio_irq = gpio_to_irq(0);
231 gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100232 irq_set_chip_and_handler(gpio_irq, &ep93xx_gpio_irq_chip,
233 handle_level_irq);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100234 set_irq_flags(gpio_irq, IRQF_VALID);
235 }
236
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100237 irq_set_chained_handler(IRQ_EP93XX_GPIO_AB,
238 ep93xx_gpio_ab_irq_handler);
239 irq_set_chained_handler(IRQ_EP93XX_GPIO0MUX,
240 ep93xx_gpio_f_irq_handler);
241 irq_set_chained_handler(IRQ_EP93XX_GPIO1MUX,
242 ep93xx_gpio_f_irq_handler);
243 irq_set_chained_handler(IRQ_EP93XX_GPIO2MUX,
244 ep93xx_gpio_f_irq_handler);
245 irq_set_chained_handler(IRQ_EP93XX_GPIO3MUX,
246 ep93xx_gpio_f_irq_handler);
247 irq_set_chained_handler(IRQ_EP93XX_GPIO4MUX,
248 ep93xx_gpio_f_irq_handler);
249 irq_set_chained_handler(IRQ_EP93XX_GPIO5MUX,
250 ep93xx_gpio_f_irq_handler);
251 irq_set_chained_handler(IRQ_EP93XX_GPIO6MUX,
252 ep93xx_gpio_f_irq_handler);
253 irq_set_chained_handler(IRQ_EP93XX_GPIO7MUX,
254 ep93xx_gpio_f_irq_handler);
Hartley Sweetend056ab72010-02-23 21:41:17 +0100255}
256
257
258/*************************************************************************
259 * gpiolib interface for EP93xx on-chip GPIOs
260 *************************************************************************/
Ryan Mallonb6850042008-04-16 02:56:35 +0100261struct ep93xx_gpio_chip {
262 struct gpio_chip chip;
263
Hartley Sweetenddf4f3d2009-06-26 21:39:27 +0100264 void __iomem *data_reg;
265 void __iomem *data_dir_reg;
Ryan Mallonb6850042008-04-16 02:56:35 +0100266};
267
268#define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
269
Ryan Mallonb6850042008-04-16 02:56:35 +0100270static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
271{
272 struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
273 unsigned long flags;
274 u8 v;
275
276 local_irq_save(flags);
277 v = __raw_readb(ep93xx_chip->data_dir_reg);
278 v &= ~(1 << offset);
279 __raw_writeb(v, ep93xx_chip->data_dir_reg);
280 local_irq_restore(flags);
281
282 return 0;
283}
284
285static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
286 unsigned offset, int val)
287{
288 struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
289 unsigned long flags;
290 int line;
291 u8 v;
292
293 local_irq_save(flags);
294
295 /* Set the value */
296 v = __raw_readb(ep93xx_chip->data_reg);
297 if (val)
298 v |= (1 << offset);
299 else
300 v &= ~(1 << offset);
301 __raw_writeb(v, ep93xx_chip->data_reg);
302
303 /* Drive as an output */
304 line = chip->base + offset;
305 if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
306 /* Ports A/B/F */
307 ep93xx_gpio_int_mask(line);
308 ep93xx_gpio_update_int_params(line >> 3);
309 }
310
311 v = __raw_readb(ep93xx_chip->data_dir_reg);
312 v |= (1 << offset);
313 __raw_writeb(v, ep93xx_chip->data_dir_reg);
314
315 local_irq_restore(flags);
316
317 return 0;
318}
319
320static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
321{
322 struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
323
324 return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
325}
326
327static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
328{
329 struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
330 unsigned long flags;
331 u8 v;
332
333 local_irq_save(flags);
334 v = __raw_readb(ep93xx_chip->data_reg);
335 if (val)
336 v |= (1 << offset);
337 else
338 v &= ~(1 << offset);
339 __raw_writeb(v, ep93xx_chip->data_reg);
340 local_irq_restore(flags);
341}
342
Hartley Sweeten5d046af2011-01-27 17:29:29 +0100343static int ep93xx_gpio_set_debounce(struct gpio_chip *chip,
344 unsigned offset, unsigned debounce)
345{
346 int gpio = chip->base + offset;
347 int irq = gpio_to_irq(gpio);
348
349 if (irq < 0)
350 return -EINVAL;
351
352 ep93xx_gpio_int_debounce(irq, debounce ? true : false);
353
354 return 0;
355}
356
Ryan Mallonb6850042008-04-16 02:56:35 +0100357#define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio) \
358 { \
359 .chip = { \
360 .label = name, \
361 .direction_input = ep93xx_gpio_direction_input, \
362 .direction_output = ep93xx_gpio_direction_output, \
363 .get = ep93xx_gpio_get, \
364 .set = ep93xx_gpio_set, \
Ryan Mallonb6850042008-04-16 02:56:35 +0100365 .base = base_gpio, \
366 .ngpio = 8, \
367 }, \
368 .data_reg = EP93XX_GPIO_REG(dr), \
369 .data_dir_reg = EP93XX_GPIO_REG(ddr), \
370 }
371
372static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
373 EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
374 EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
Hartley Sweeten7a1f3702008-09-05 17:24:40 +0100375 EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
Ryan Mallonb6850042008-04-16 02:56:35 +0100376 EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
377 EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
Hartley Sweeten7a1f3702008-09-05 17:24:40 +0100378 EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
Ryan Mallonb6850042008-04-16 02:56:35 +0100379 EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
380 EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
381};
382
383void __init ep93xx_gpio_init(void)
384{
385 int i;
386
Hartley Sweetenfd015482011-01-25 01:05:35 +0100387 /* Set Ports C, D, E, G, and H for GPIO use */
388 ep93xx_devcfg_set_bits(EP93XX_SYSCON_DEVCFG_KEYS |
389 EP93XX_SYSCON_DEVCFG_GONK |
390 EP93XX_SYSCON_DEVCFG_EONIDE |
391 EP93XX_SYSCON_DEVCFG_GONIDE |
392 EP93XX_SYSCON_DEVCFG_HONIDE);
393
Hartley Sweeten5d046af2011-01-27 17:29:29 +0100394 for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
395 struct gpio_chip *chip = &ep93xx_gpio_banks[i].chip;
396
397 /*
398 * Ports A, B, and F support input debouncing when
399 * used as interrupts.
400 */
401 if (!strcmp(chip->label, "A") ||
402 !strcmp(chip->label, "B") ||
403 !strcmp(chip->label, "F"))
404 chip->set_debounce = ep93xx_gpio_set_debounce;
405
406 gpiochip_add(chip);
407 }
Ryan Mallonb6850042008-04-16 02:56:35 +0100408}