blob: b765773ab8aaf8921a51265e850d98f5fb263a93 [file] [log] [blame]
Lars-Peter Clausena55f4502010-07-17 11:11:19 +00001/*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 platform GPIO support
4 *
5 * This program is free software; you can redistribute it and/or modify it
Ralf Baechle70342282013-01-22 12:59:30 +01006 * under the terms of the GNU General Public License as published by the
Lars-Peter Clausena55f4502010-07-17 11:11:19 +00007 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000020#include <linux/io.h>
Linus Walleijf5abf9e2015-12-08 14:28:45 +010021#include <linux/gpio/driver.h>
22/* FIXME: needed for gpio_request(), try to remove consumer API from driver */
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000023#include <linux/gpio.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
Paul Burton44e08e72015-05-24 16:11:31 +010026#include <linux/irqchip/ingenic.h>
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000027#include <linux/bitops.h>
28
29#include <linux/debugfs.h>
30#include <linux/seq_file.h>
31
32#include <asm/mach-jz4740/base.h>
Alban Bedel5b235dc2015-09-04 14:29:16 +020033#include <asm/mach-jz4740/gpio.h>
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000034
35#define JZ4740_GPIO_BASE_A (32*0)
36#define JZ4740_GPIO_BASE_B (32*1)
37#define JZ4740_GPIO_BASE_C (32*2)
38#define JZ4740_GPIO_BASE_D (32*3)
39
40#define JZ4740_GPIO_NUM_A 32
41#define JZ4740_GPIO_NUM_B 32
42#define JZ4740_GPIO_NUM_C 31
43#define JZ4740_GPIO_NUM_D 32
44
45#define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A)
46#define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B)
47#define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C)
48#define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D)
49
50#define JZ_REG_GPIO_PIN 0x00
51#define JZ_REG_GPIO_DATA 0x10
52#define JZ_REG_GPIO_DATA_SET 0x14
53#define JZ_REG_GPIO_DATA_CLEAR 0x18
54#define JZ_REG_GPIO_MASK 0x20
55#define JZ_REG_GPIO_MASK_SET 0x24
56#define JZ_REG_GPIO_MASK_CLEAR 0x28
57#define JZ_REG_GPIO_PULL 0x30
58#define JZ_REG_GPIO_PULL_SET 0x34
59#define JZ_REG_GPIO_PULL_CLEAR 0x38
60#define JZ_REG_GPIO_FUNC 0x40
61#define JZ_REG_GPIO_FUNC_SET 0x44
62#define JZ_REG_GPIO_FUNC_CLEAR 0x48
63#define JZ_REG_GPIO_SELECT 0x50
64#define JZ_REG_GPIO_SELECT_SET 0x54
65#define JZ_REG_GPIO_SELECT_CLEAR 0x58
66#define JZ_REG_GPIO_DIRECTION 0x60
67#define JZ_REG_GPIO_DIRECTION_SET 0x64
68#define JZ_REG_GPIO_DIRECTION_CLEAR 0x68
69#define JZ_REG_GPIO_TRIGGER 0x70
70#define JZ_REG_GPIO_TRIGGER_SET 0x74
71#define JZ_REG_GPIO_TRIGGER_CLEAR 0x78
72#define JZ_REG_GPIO_FLAG 0x80
73#define JZ_REG_GPIO_FLAG_CLEAR 0x14
74
75#define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
76#define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg))
77#define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg))
78
79struct jz_gpio_chip {
80 unsigned int irq;
81 unsigned int irq_base;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000082 uint32_t edge_trigger_both;
83
84 void __iomem *base;
85
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000086 struct gpio_chip gpio_chip;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000087};
88
89static struct jz_gpio_chip jz4740_gpio_chips[];
90
91static inline struct jz_gpio_chip *gpio_to_jz_gpio_chip(unsigned int gpio)
92{
93 return &jz4740_gpio_chips[gpio >> 5];
94}
95
Linus Walleijf5abf9e2015-12-08 14:28:45 +010096static inline struct jz_gpio_chip *gpio_chip_to_jz_gpio_chip(struct gpio_chip *gc)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000097{
Linus Walleijf5abf9e2015-12-08 14:28:45 +010098 return gpiochip_get_data(gc);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +000099}
100
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000101static inline struct jz_gpio_chip *irq_to_jz_gpio_chip(struct irq_data *data)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000102{
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200103 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
104 return gc->private;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000105}
106
107static inline void jz_gpio_write_bit(unsigned int gpio, unsigned int reg)
108{
109 writel(GPIO_TO_BIT(gpio), GPIO_TO_REG(gpio, reg));
110}
111
112int jz_gpio_set_function(int gpio, enum jz_gpio_function function)
113{
114 if (function == JZ_GPIO_FUNC_NONE) {
115 jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_CLEAR);
116 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
117 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
118 } else {
119 jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_SET);
120 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
121 switch (function) {
122 case JZ_GPIO_FUNC1:
123 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
124 break;
125 case JZ_GPIO_FUNC3:
126 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_SET);
127 case JZ_GPIO_FUNC2: /* Falltrough */
128 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_SET);
129 break;
130 default:
131 BUG();
132 break;
133 }
134 }
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(jz_gpio_set_function);
139
140int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num)
141{
142 size_t i;
143 int ret;
144
145 for (i = 0; i < num; ++i, ++request) {
146 ret = gpio_request(request->gpio, request->name);
147 if (ret)
148 goto err;
149 jz_gpio_set_function(request->gpio, request->function);
150 }
151
152 return 0;
153
154err:
155 for (--request; i > 0; --i, --request) {
156 gpio_free(request->gpio);
157 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
158 }
159
160 return ret;
161}
162EXPORT_SYMBOL_GPL(jz_gpio_bulk_request);
163
164void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num)
165{
166 size_t i;
167
168 for (i = 0; i < num; ++i, ++request) {
169 gpio_free(request->gpio);
170 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
171 }
172
173}
174EXPORT_SYMBOL_GPL(jz_gpio_bulk_free);
175
176void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num)
177{
178 size_t i;
179
180 for (i = 0; i < num; ++i, ++request) {
181 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
182 jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_DIRECTION_CLEAR);
183 jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_PULL_SET);
184 }
185}
186EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend);
187
188void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num)
189{
190 size_t i;
191
192 for (i = 0; i < num; ++i, ++request)
193 jz_gpio_set_function(request->gpio, request->function);
194}
195EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume);
196
197void jz_gpio_enable_pullup(unsigned gpio)
198{
199 jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_CLEAR);
200}
201EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup);
202
203void jz_gpio_disable_pullup(unsigned gpio)
204{
205 jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_SET);
206}
207EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup);
208
209static int jz_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
210{
211 return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio));
212}
213
214static void jz_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
215{
216 uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET);
217 reg += !value;
218 writel(BIT(gpio), reg);
219}
220
221static int jz_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
222 int value)
223{
224 writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET));
225 jz_gpio_set_value(chip, gpio, value);
226
227 return 0;
228}
229
230static int jz_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
231{
232 writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR));
233
234 return 0;
235}
236
Alban Bedel832f5da2015-08-02 18:30:11 +0200237static int jz_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
238{
Linus Walleijf5abf9e2015-12-08 14:28:45 +0100239 struct jz_gpio_chip *jz_gpio = gpiochip_get_data(chip);
Alban Bedel832f5da2015-08-02 18:30:11 +0200240
241 return jz_gpio->irq_base + gpio;
242}
243
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000244int jz_gpio_port_direction_input(int port, uint32_t mask)
245{
246 writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_CLEAR));
247
248 return 0;
249}
250EXPORT_SYMBOL(jz_gpio_port_direction_input);
251
252int jz_gpio_port_direction_output(int port, uint32_t mask)
253{
254 writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_SET));
255
256 return 0;
257}
258EXPORT_SYMBOL(jz_gpio_port_direction_output);
259
260void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask)
261{
262 writel(~value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_CLEAR));
263 writel(value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_SET));
264}
265EXPORT_SYMBOL(jz_gpio_port_set_value);
266
267uint32_t jz_gpio_port_get_value(int port, uint32_t mask)
268{
269 uint32_t value = readl(GPIO_TO_REG(port, JZ_REG_GPIO_PIN));
270
271 return value & mask;
272}
273EXPORT_SYMBOL(jz_gpio_port_get_value);
274
Ralf Baechleb61a4592016-02-16 12:29:46 +0100275#define IRQ_TO_BIT(irq) BIT((irq - JZ4740_IRQ_GPIO(0)) & 0x1f)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000276
277static void jz_gpio_check_trigger_both(struct jz_gpio_chip *chip, unsigned int irq)
278{
279 uint32_t value;
280 void __iomem *reg;
281 uint32_t mask = IRQ_TO_BIT(irq);
282
283 if (!(chip->edge_trigger_both & mask))
284 return;
285
286 reg = chip->base;
287
288 value = readl(chip->base + JZ_REG_GPIO_PIN);
289 if (value & mask)
290 reg += JZ_REG_GPIO_DIRECTION_CLEAR;
291 else
292 reg += JZ_REG_GPIO_DIRECTION_SET;
293
294 writel(mask, reg);
295}
296
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200297static void jz_gpio_irq_demux_handler(struct irq_desc *desc)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000298{
299 uint32_t flag;
300 unsigned int gpio_irq;
Thomas Gleixnere4ec7982011-03-27 15:19:28 +0200301 struct jz_gpio_chip *chip = irq_desc_get_handler_data(desc);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000302
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000303 flag = readl(chip->base + JZ_REG_GPIO_FLAG);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000304 if (!flag)
305 return;
306
Lars-Peter Clausenfe5a8b72011-09-24 02:29:54 +0200307 gpio_irq = chip->irq_base + __fls(flag);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000308
Lars-Peter Clausen93303632011-09-24 02:29:54 +0200309 jz_gpio_check_trigger_both(chip, gpio_irq);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000310
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000311 generic_handle_irq(gpio_irq);
312};
313
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000314static inline void jz_gpio_set_irq_bit(struct irq_data *data, unsigned int reg)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000315{
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000316 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
317 writel(IRQ_TO_BIT(data->irq), chip->base + reg);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000318}
319
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000320static void jz_gpio_irq_unmask(struct irq_data *data)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000321{
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000322 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000323
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000324 jz_gpio_check_trigger_both(chip, data->irq);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200325 irq_gc_unmask_enable_reg(data);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000326};
327
328/* TODO: Check if function is gpio */
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000329static unsigned int jz_gpio_irq_startup(struct irq_data *data)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000330{
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000331 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_SET);
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000332 jz_gpio_irq_unmask(data);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000333 return 0;
334}
335
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000336static void jz_gpio_irq_shutdown(struct irq_data *data)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000337{
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200338 irq_gc_mask_disable_reg(data);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000339
340 /* Set direction to input */
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000341 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
342 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_CLEAR);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000343}
344
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000345static int jz_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000346{
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000347 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000348 unsigned int irq = data->irq;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000349
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000350 if (flow_type == IRQ_TYPE_EDGE_BOTH) {
351 uint32_t value = readl(chip->base + JZ_REG_GPIO_PIN);
352 if (value & IRQ_TO_BIT(irq))
353 flow_type = IRQ_TYPE_EDGE_FALLING;
354 else
355 flow_type = IRQ_TYPE_EDGE_RISING;
356 chip->edge_trigger_both |= IRQ_TO_BIT(irq);
357 } else {
358 chip->edge_trigger_both &= ~IRQ_TO_BIT(irq);
359 }
360
361 switch (flow_type) {
362 case IRQ_TYPE_EDGE_RISING:
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000363 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
364 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000365 break;
366 case IRQ_TYPE_EDGE_FALLING:
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000367 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
368 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000369 break;
370 case IRQ_TYPE_LEVEL_HIGH:
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000371 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
372 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000373 break;
374 case IRQ_TYPE_LEVEL_LOW:
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000375 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
376 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000377 break;
378 default:
379 return -EINVAL;
380 }
381
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000382 return 0;
383}
384
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000385static int jz_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000386{
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000387 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000388
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200389 irq_gc_set_wake(data, on);
Thomas Gleixnere4ec7982011-03-27 15:19:28 +0200390 irq_set_irq_wake(chip->irq, on);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200391
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000392 return 0;
393}
394
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000395#define JZ4740_GPIO_CHIP(_bank) { \
396 .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \
397 .gpio_chip = { \
398 .label = "Bank " # _bank, \
399 .owner = THIS_MODULE, \
400 .set = jz_gpio_set_value, \
401 .get = jz_gpio_get_value, \
402 .direction_output = jz_gpio_direction_output, \
403 .direction_input = jz_gpio_direction_input, \
Alban Bedel832f5da2015-08-02 18:30:11 +0200404 .to_irq = jz_gpio_to_irq, \
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000405 .base = JZ4740_GPIO_BASE_ ## _bank, \
406 .ngpio = JZ4740_GPIO_NUM_ ## _bank, \
407 }, \
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000408}
409
410static struct jz_gpio_chip jz4740_gpio_chips[] = {
411 JZ4740_GPIO_CHIP(A),
412 JZ4740_GPIO_CHIP(B),
413 JZ4740_GPIO_CHIP(C),
414 JZ4740_GPIO_CHIP(D),
415};
416
Rafael J. Wysockibd710002011-06-02 21:06:48 +0200417static void jz4740_gpio_chip_init(struct jz_gpio_chip *chip, unsigned int id)
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000418{
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200419 struct irq_chip_generic *gc;
420 struct irq_chip_type *ct;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000421
422 chip->base = ioremap(JZ4740_GPIO_BASE_ADDR + (id * 0x100), 0x100);
423
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000424 chip->irq = JZ4740_IRQ_INTC_GPIO(id);
Thomas Gleixnerf058dc82015-07-13 20:45:53 +0000425 irq_set_chained_handler_and_data(chip->irq,
426 jz_gpio_irq_demux_handler, chip);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000427
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200428 gc = irq_alloc_generic_chip(chip->gpio_chip.label, 1, chip->irq_base,
429 chip->base, handle_level_irq);
430
431 gc->wake_enabled = IRQ_MSK(chip->gpio_chip.ngpio);
432 gc->private = chip;
433
434 ct = gc->chip_types;
435 ct->regs.enable = JZ_REG_GPIO_MASK_CLEAR;
436 ct->regs.disable = JZ_REG_GPIO_MASK_SET;
437 ct->regs.ack = JZ_REG_GPIO_FLAG_CLEAR;
438
439 ct->chip.name = "GPIO";
440 ct->chip.irq_mask = irq_gc_mask_disable_reg;
441 ct->chip.irq_unmask = jz_gpio_irq_unmask;
442 ct->chip.irq_ack = irq_gc_ack_set_bit;
Paul Burton2da01882015-05-24 16:11:29 +0100443 ct->chip.irq_suspend = ingenic_intc_irq_suspend;
444 ct->chip.irq_resume = ingenic_intc_irq_resume;
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200445 ct->chip.irq_startup = jz_gpio_irq_startup;
446 ct->chip.irq_shutdown = jz_gpio_irq_shutdown;
447 ct->chip.irq_set_type = jz_gpio_irq_set_type;
448 ct->chip.irq_set_wake = jz_gpio_irq_set_wake;
449 ct->chip.flags = IRQCHIP_SET_TYPE_MASKED;
450
451 irq_setup_generic_chip(gc, IRQ_MSK(chip->gpio_chip.ngpio),
452 IRQ_GC_INIT_NESTED_LOCK, 0, IRQ_NOPROBE | IRQ_LEVEL);
453
Linus Walleijf5abf9e2015-12-08 14:28:45 +0100454 gpiochip_add_data(&chip->gpio_chip, chip);
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000455}
456
457static int __init jz4740_gpio_init(void)
458{
459 unsigned int i;
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000460
461 for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i)
462 jz4740_gpio_chip_init(&jz4740_gpio_chips[i], i);
463
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400464 printk(KERN_INFO "JZ4740 GPIO initialized\n");
Lars-Peter Clausena55f4502010-07-17 11:11:19 +0000465
466 return 0;
467}
468arch_initcall(jz4740_gpio_init);
469
470#ifdef CONFIG_DEBUG_FS
471
472static inline void gpio_seq_reg(struct seq_file *s, struct jz_gpio_chip *chip,
473 const char *name, unsigned int reg)
474{
475 seq_printf(s, "\t%s: %08x\n", name, readl(chip->base + reg));
476}
477
478static int gpio_regs_show(struct seq_file *s, void *unused)
479{
480 struct jz_gpio_chip *chip = jz4740_gpio_chips;
481 int i;
482
483 for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i, ++chip) {
484 seq_printf(s, "==GPIO %d==\n", i);
485 gpio_seq_reg(s, chip, "Pin", JZ_REG_GPIO_PIN);
486 gpio_seq_reg(s, chip, "Data", JZ_REG_GPIO_DATA);
487 gpio_seq_reg(s, chip, "Mask", JZ_REG_GPIO_MASK);
488 gpio_seq_reg(s, chip, "Pull", JZ_REG_GPIO_PULL);
489 gpio_seq_reg(s, chip, "Func", JZ_REG_GPIO_FUNC);
490 gpio_seq_reg(s, chip, "Select", JZ_REG_GPIO_SELECT);
491 gpio_seq_reg(s, chip, "Direction", JZ_REG_GPIO_DIRECTION);
492 gpio_seq_reg(s, chip, "Trigger", JZ_REG_GPIO_TRIGGER);
493 gpio_seq_reg(s, chip, "Flag", JZ_REG_GPIO_FLAG);
494 }
495
496 return 0;
497}
498
499static int gpio_regs_open(struct inode *inode, struct file *file)
500{
501 return single_open(file, gpio_regs_show, NULL);
502}
503
504static const struct file_operations gpio_regs_operations = {
505 .open = gpio_regs_open,
506 .read = seq_read,
507 .llseek = seq_lseek,
508 .release = single_release,
509};
510
511static int __init gpio_debugfs_init(void)
512{
513 (void) debugfs_create_file("jz_regs_gpio", S_IFREG | S_IRUGO,
514 NULL, NULL, &gpio_regs_operations);
515 return 0;
516}
517subsys_initcall(gpio_debugfs_init);
518
519#endif