MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / arch / mips / jz4740 / gpio.c
blob4397972949fa315e6796b828d65778394fd21451
1 /*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 platform GPIO support
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
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.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/syscore_ops.h>
22 #include <linux/io.h>
23 #include <linux/gpio.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/bitops.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
31 #include <asm/mach-jz4740/base.h>
33 #define JZ4740_GPIO_BASE_A (32*0)
34 #define JZ4740_GPIO_BASE_B (32*1)
35 #define JZ4740_GPIO_BASE_C (32*2)
36 #define JZ4740_GPIO_BASE_D (32*3)
38 #define JZ4740_GPIO_NUM_A 32
39 #define JZ4740_GPIO_NUM_B 32
40 #define JZ4740_GPIO_NUM_C 31
41 #define JZ4740_GPIO_NUM_D 32
43 #define JZ4740_IRQ_GPIO_BASE_A (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_A)
44 #define JZ4740_IRQ_GPIO_BASE_B (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_B)
45 #define JZ4740_IRQ_GPIO_BASE_C (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_C)
46 #define JZ4740_IRQ_GPIO_BASE_D (JZ4740_IRQ_GPIO(0) + JZ4740_GPIO_BASE_D)
48 #define JZ_REG_GPIO_PIN 0x00
49 #define JZ_REG_GPIO_DATA 0x10
50 #define JZ_REG_GPIO_DATA_SET 0x14
51 #define JZ_REG_GPIO_DATA_CLEAR 0x18
52 #define JZ_REG_GPIO_MASK 0x20
53 #define JZ_REG_GPIO_MASK_SET 0x24
54 #define JZ_REG_GPIO_MASK_CLEAR 0x28
55 #define JZ_REG_GPIO_PULL 0x30
56 #define JZ_REG_GPIO_PULL_SET 0x34
57 #define JZ_REG_GPIO_PULL_CLEAR 0x38
58 #define JZ_REG_GPIO_FUNC 0x40
59 #define JZ_REG_GPIO_FUNC_SET 0x44
60 #define JZ_REG_GPIO_FUNC_CLEAR 0x48
61 #define JZ_REG_GPIO_SELECT 0x50
62 #define JZ_REG_GPIO_SELECT_SET 0x54
63 #define JZ_REG_GPIO_SELECT_CLEAR 0x58
64 #define JZ_REG_GPIO_DIRECTION 0x60
65 #define JZ_REG_GPIO_DIRECTION_SET 0x64
66 #define JZ_REG_GPIO_DIRECTION_CLEAR 0x68
67 #define JZ_REG_GPIO_TRIGGER 0x70
68 #define JZ_REG_GPIO_TRIGGER_SET 0x74
69 #define JZ_REG_GPIO_TRIGGER_CLEAR 0x78
70 #define JZ_REG_GPIO_FLAG 0x80
71 #define JZ_REG_GPIO_FLAG_CLEAR 0x14
73 #define GPIO_TO_BIT(gpio) BIT(gpio & 0x1f)
74 #define GPIO_TO_REG(gpio, reg) (gpio_to_jz_gpio_chip(gpio)->base + (reg))
75 #define CHIP_TO_REG(chip, reg) (gpio_chip_to_jz_gpio_chip(chip)->base + (reg))
77 struct jz_gpio_chip {
78 unsigned int irq;
79 unsigned int irq_base;
80 uint32_t wakeup;
81 uint32_t suspend_mask;
82 uint32_t edge_trigger_both;
84 void __iomem *base;
86 spinlock_t lock;
88 struct gpio_chip gpio_chip;
91 static struct jz_gpio_chip jz4740_gpio_chips[];
93 static inline struct jz_gpio_chip *gpio_to_jz_gpio_chip(unsigned int gpio)
95 return &jz4740_gpio_chips[gpio >> 5];
98 static inline struct jz_gpio_chip *gpio_chip_to_jz_gpio_chip(struct gpio_chip *gpio_chip)
100 return container_of(gpio_chip, struct jz_gpio_chip, gpio_chip);
103 static inline struct jz_gpio_chip *irq_to_jz_gpio_chip(struct irq_data *data)
105 return irq_data_get_irq_chip_data(data);
108 static inline void jz_gpio_write_bit(unsigned int gpio, unsigned int reg)
110 writel(GPIO_TO_BIT(gpio), GPIO_TO_REG(gpio, reg));
113 int jz_gpio_set_function(int gpio, enum jz_gpio_function function)
115 if (function == JZ_GPIO_FUNC_NONE) {
116 jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_CLEAR);
117 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
118 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
119 } else {
120 jz_gpio_write_bit(gpio, JZ_REG_GPIO_FUNC_SET);
121 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_CLEAR);
122 switch (function) {
123 case JZ_GPIO_FUNC1:
124 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_CLEAR);
125 break;
126 case JZ_GPIO_FUNC3:
127 jz_gpio_write_bit(gpio, JZ_REG_GPIO_TRIGGER_SET);
128 case JZ_GPIO_FUNC2: /* Falltrough */
129 jz_gpio_write_bit(gpio, JZ_REG_GPIO_SELECT_SET);
130 break;
131 default:
132 BUG();
133 break;
137 return 0;
139 EXPORT_SYMBOL_GPL(jz_gpio_set_function);
141 int jz_gpio_bulk_request(const struct jz_gpio_bulk_request *request, size_t num)
143 size_t i;
144 int ret;
146 for (i = 0; i < num; ++i, ++request) {
147 ret = gpio_request(request->gpio, request->name);
148 if (ret)
149 goto err;
150 jz_gpio_set_function(request->gpio, request->function);
153 return 0;
155 err:
156 for (--request; i > 0; --i, --request) {
157 gpio_free(request->gpio);
158 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
161 return ret;
163 EXPORT_SYMBOL_GPL(jz_gpio_bulk_request);
165 void jz_gpio_bulk_free(const struct jz_gpio_bulk_request *request, size_t num)
167 size_t i;
169 for (i = 0; i < num; ++i, ++request) {
170 gpio_free(request->gpio);
171 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
175 EXPORT_SYMBOL_GPL(jz_gpio_bulk_free);
177 void jz_gpio_bulk_suspend(const struct jz_gpio_bulk_request *request, size_t num)
179 size_t i;
181 for (i = 0; i < num; ++i, ++request) {
182 jz_gpio_set_function(request->gpio, JZ_GPIO_FUNC_NONE);
183 jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_DIRECTION_CLEAR);
184 jz_gpio_write_bit(request->gpio, JZ_REG_GPIO_PULL_SET);
187 EXPORT_SYMBOL_GPL(jz_gpio_bulk_suspend);
189 void jz_gpio_bulk_resume(const struct jz_gpio_bulk_request *request, size_t num)
191 size_t i;
193 for (i = 0; i < num; ++i, ++request)
194 jz_gpio_set_function(request->gpio, request->function);
196 EXPORT_SYMBOL_GPL(jz_gpio_bulk_resume);
198 void jz_gpio_enable_pullup(unsigned gpio)
200 jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_CLEAR);
202 EXPORT_SYMBOL_GPL(jz_gpio_enable_pullup);
204 void jz_gpio_disable_pullup(unsigned gpio)
206 jz_gpio_write_bit(gpio, JZ_REG_GPIO_PULL_SET);
208 EXPORT_SYMBOL_GPL(jz_gpio_disable_pullup);
210 static int jz_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
212 return !!(readl(CHIP_TO_REG(chip, JZ_REG_GPIO_PIN)) & BIT(gpio));
215 static void jz_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
217 uint32_t __iomem *reg = CHIP_TO_REG(chip, JZ_REG_GPIO_DATA_SET);
218 reg += !value;
219 writel(BIT(gpio), reg);
222 static int jz_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
223 int value)
225 writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_SET));
226 jz_gpio_set_value(chip, gpio, value);
228 return 0;
231 static int jz_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
233 writel(BIT(gpio), CHIP_TO_REG(chip, JZ_REG_GPIO_DIRECTION_CLEAR));
235 return 0;
238 int jz_gpio_port_direction_input(int port, uint32_t mask)
240 writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_CLEAR));
242 return 0;
244 EXPORT_SYMBOL(jz_gpio_port_direction_input);
246 int jz_gpio_port_direction_output(int port, uint32_t mask)
248 writel(mask, GPIO_TO_REG(port, JZ_REG_GPIO_DIRECTION_SET));
250 return 0;
252 EXPORT_SYMBOL(jz_gpio_port_direction_output);
254 void jz_gpio_port_set_value(int port, uint32_t value, uint32_t mask)
256 writel(~value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_CLEAR));
257 writel(value & mask, GPIO_TO_REG(port, JZ_REG_GPIO_DATA_SET));
259 EXPORT_SYMBOL(jz_gpio_port_set_value);
261 uint32_t jz_gpio_port_get_value(int port, uint32_t mask)
263 uint32_t value = readl(GPIO_TO_REG(port, JZ_REG_GPIO_PIN));
265 return value & mask;
267 EXPORT_SYMBOL(jz_gpio_port_get_value);
269 int gpio_to_irq(unsigned gpio)
271 return JZ4740_IRQ_GPIO(0) + gpio;
273 EXPORT_SYMBOL_GPL(gpio_to_irq);
275 int irq_to_gpio(unsigned irq)
277 return irq - JZ4740_IRQ_GPIO(0);
279 EXPORT_SYMBOL_GPL(irq_to_gpio);
281 #define IRQ_TO_BIT(irq) BIT(irq_to_gpio(irq) & 0x1f)
283 static void jz_gpio_check_trigger_both(struct jz_gpio_chip *chip, unsigned int irq)
285 uint32_t value;
286 void __iomem *reg;
287 uint32_t mask = IRQ_TO_BIT(irq);
289 if (!(chip->edge_trigger_both & mask))
290 return;
292 reg = chip->base;
294 value = readl(chip->base + JZ_REG_GPIO_PIN);
295 if (value & mask)
296 reg += JZ_REG_GPIO_DIRECTION_CLEAR;
297 else
298 reg += JZ_REG_GPIO_DIRECTION_SET;
300 writel(mask, reg);
303 static void jz_gpio_irq_demux_handler(unsigned int irq, struct irq_desc *desc)
305 uint32_t flag;
306 unsigned int gpio_irq;
307 unsigned int gpio_bank;
308 struct jz_gpio_chip *chip = irq_desc_get_handler_data(desc);
310 gpio_bank = JZ4740_IRQ_GPIO0 - irq;
312 flag = readl(chip->base + JZ_REG_GPIO_FLAG);
314 if (!flag)
315 return;
317 gpio_irq = __fls(flag);
319 jz_gpio_check_trigger_both(chip, irq);
321 gpio_irq += (gpio_bank << 5) + JZ4740_IRQ_GPIO(0);
323 generic_handle_irq(gpio_irq);
326 static inline void jz_gpio_set_irq_bit(struct irq_data *data, unsigned int reg)
328 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
329 writel(IRQ_TO_BIT(data->irq), chip->base + reg);
332 static void jz_gpio_irq_mask(struct irq_data *data)
334 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_MASK_SET);
337 static void jz_gpio_irq_unmask(struct irq_data *data)
339 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
341 jz_gpio_check_trigger_both(chip, data->irq);
343 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_MASK_CLEAR);
346 /* TODO: Check if function is gpio */
347 static unsigned int jz_gpio_irq_startup(struct irq_data *data)
349 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_SET);
350 jz_gpio_irq_unmask(data);
351 return 0;
354 static void jz_gpio_irq_shutdown(struct irq_data *data)
356 jz_gpio_irq_mask(data);
358 /* Set direction to input */
359 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
360 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_SELECT_CLEAR);
363 static void jz_gpio_irq_ack(struct irq_data *data)
365 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_FLAG_CLEAR);
368 static int jz_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
370 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
371 unsigned int irq = data->irq;
373 if (flow_type == IRQ_TYPE_EDGE_BOTH) {
374 uint32_t value = readl(chip->base + JZ_REG_GPIO_PIN);
375 if (value & IRQ_TO_BIT(irq))
376 flow_type = IRQ_TYPE_EDGE_FALLING;
377 else
378 flow_type = IRQ_TYPE_EDGE_RISING;
379 chip->edge_trigger_both |= IRQ_TO_BIT(irq);
380 } else {
381 chip->edge_trigger_both &= ~IRQ_TO_BIT(irq);
384 switch (flow_type) {
385 case IRQ_TYPE_EDGE_RISING:
386 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
387 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
388 break;
389 case IRQ_TYPE_EDGE_FALLING:
390 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
391 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_SET);
392 break;
393 case IRQ_TYPE_LEVEL_HIGH:
394 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_SET);
395 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
396 break;
397 case IRQ_TYPE_LEVEL_LOW:
398 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_DIRECTION_CLEAR);
399 jz_gpio_set_irq_bit(data, JZ_REG_GPIO_TRIGGER_CLEAR);
400 break;
401 default:
402 return -EINVAL;
405 return 0;
408 static int jz_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
410 struct jz_gpio_chip *chip = irq_to_jz_gpio_chip(data);
411 spin_lock(&chip->lock);
412 if (on)
413 chip->wakeup |= IRQ_TO_BIT(data->irq);
414 else
415 chip->wakeup &= ~IRQ_TO_BIT(data->irq);
416 spin_unlock(&chip->lock);
418 irq_set_irq_wake(chip->irq, on);
419 return 0;
422 static struct irq_chip jz_gpio_irq_chip = {
423 .name = "GPIO",
424 .irq_mask = jz_gpio_irq_mask,
425 .irq_unmask = jz_gpio_irq_unmask,
426 .irq_ack = jz_gpio_irq_ack,
427 .irq_startup = jz_gpio_irq_startup,
428 .irq_shutdown = jz_gpio_irq_shutdown,
429 .irq_set_type = jz_gpio_irq_set_type,
430 .irq_set_wake = jz_gpio_irq_set_wake,
431 .flags = IRQCHIP_SET_TYPE_MASKED,
435 * This lock class tells lockdep that GPIO irqs are in a different
436 * category than their parents, so it won't report false recursion.
438 static struct lock_class_key gpio_lock_class;
440 #define JZ4740_GPIO_CHIP(_bank) { \
441 .irq_base = JZ4740_IRQ_GPIO_BASE_ ## _bank, \
442 .gpio_chip = { \
443 .label = "Bank " # _bank, \
444 .owner = THIS_MODULE, \
445 .set = jz_gpio_set_value, \
446 .get = jz_gpio_get_value, \
447 .direction_output = jz_gpio_direction_output, \
448 .direction_input = jz_gpio_direction_input, \
449 .base = JZ4740_GPIO_BASE_ ## _bank, \
450 .ngpio = JZ4740_GPIO_NUM_ ## _bank, \
451 }, \
454 static struct jz_gpio_chip jz4740_gpio_chips[] = {
455 JZ4740_GPIO_CHIP(A),
456 JZ4740_GPIO_CHIP(B),
457 JZ4740_GPIO_CHIP(C),
458 JZ4740_GPIO_CHIP(D),
461 static void jz4740_gpio_suspend_chip(struct jz_gpio_chip *chip)
463 chip->suspend_mask = readl(chip->base + JZ_REG_GPIO_MASK);
464 writel(~(chip->wakeup), chip->base + JZ_REG_GPIO_MASK_SET);
465 writel(chip->wakeup, chip->base + JZ_REG_GPIO_MASK_CLEAR);
468 static int jz4740_gpio_suspend(void)
470 int i;
472 for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); i++)
473 jz4740_gpio_suspend_chip(&jz4740_gpio_chips[i]);
475 return 0;
478 static void jz4740_gpio_resume_chip(struct jz_gpio_chip *chip)
480 uint32_t mask = chip->suspend_mask;
482 writel(~mask, chip->base + JZ_REG_GPIO_MASK_CLEAR);
483 writel(mask, chip->base + JZ_REG_GPIO_MASK_SET);
486 static void jz4740_gpio_resume(void)
488 int i;
490 for (i = ARRAY_SIZE(jz4740_gpio_chips) - 1; i >= 0 ; i--)
491 jz4740_gpio_resume_chip(&jz4740_gpio_chips[i]);
494 static struct syscore_ops jz4740_gpio_syscore_ops = {
495 .suspend = jz4740_gpio_suspend,
496 .resume = jz4740_gpio_resume,
499 static void jz4740_gpio_chip_init(struct jz_gpio_chip *chip, unsigned int id)
501 int irq;
503 spin_lock_init(&chip->lock);
505 chip->base = ioremap(JZ4740_GPIO_BASE_ADDR + (id * 0x100), 0x100);
507 gpiochip_add(&chip->gpio_chip);
509 chip->irq = JZ4740_IRQ_INTC_GPIO(id);
510 irq_set_handler_data(chip->irq, chip);
511 irq_set_chained_handler(chip->irq, jz_gpio_irq_demux_handler);
513 for (irq = chip->irq_base; irq < chip->irq_base + chip->gpio_chip.ngpio; ++irq) {
514 irq_set_lockdep_class(irq, &gpio_lock_class);
515 irq_set_chip_data(irq, chip);
516 irq_set_chip_and_handler(irq, &jz_gpio_irq_chip,
517 handle_level_irq);
521 static int __init jz4740_gpio_init(void)
523 unsigned int i;
525 for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i)
526 jz4740_gpio_chip_init(&jz4740_gpio_chips[i], i);
528 register_syscore_ops(&jz4740_gpio_syscore_ops);
530 printk(KERN_INFO "JZ4740 GPIO initialized\n");
532 return 0;
534 arch_initcall(jz4740_gpio_init);
536 #ifdef CONFIG_DEBUG_FS
538 static inline void gpio_seq_reg(struct seq_file *s, struct jz_gpio_chip *chip,
539 const char *name, unsigned int reg)
541 seq_printf(s, "\t%s: %08x\n", name, readl(chip->base + reg));
544 static int gpio_regs_show(struct seq_file *s, void *unused)
546 struct jz_gpio_chip *chip = jz4740_gpio_chips;
547 int i;
549 for (i = 0; i < ARRAY_SIZE(jz4740_gpio_chips); ++i, ++chip) {
550 seq_printf(s, "==GPIO %d==\n", i);
551 gpio_seq_reg(s, chip, "Pin", JZ_REG_GPIO_PIN);
552 gpio_seq_reg(s, chip, "Data", JZ_REG_GPIO_DATA);
553 gpio_seq_reg(s, chip, "Mask", JZ_REG_GPIO_MASK);
554 gpio_seq_reg(s, chip, "Pull", JZ_REG_GPIO_PULL);
555 gpio_seq_reg(s, chip, "Func", JZ_REG_GPIO_FUNC);
556 gpio_seq_reg(s, chip, "Select", JZ_REG_GPIO_SELECT);
557 gpio_seq_reg(s, chip, "Direction", JZ_REG_GPIO_DIRECTION);
558 gpio_seq_reg(s, chip, "Trigger", JZ_REG_GPIO_TRIGGER);
559 gpio_seq_reg(s, chip, "Flag", JZ_REG_GPIO_FLAG);
562 return 0;
565 static int gpio_regs_open(struct inode *inode, struct file *file)
567 return single_open(file, gpio_regs_show, NULL);
570 static const struct file_operations gpio_regs_operations = {
571 .open = gpio_regs_open,
572 .read = seq_read,
573 .llseek = seq_lseek,
574 .release = single_release,
577 static int __init gpio_debugfs_init(void)
579 (void) debugfs_create_file("jz_regs_gpio", S_IFREG | S_IRUGO,
580 NULL, NULL, &gpio_regs_operations);
581 return 0;
583 subsys_initcall(gpio_debugfs_init);
585 #endif