Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/plat-iop/gpio.c |
| 3 | * GPIO handling for Intel IOP3xx processors. |
| 4 | * |
| 5 | * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/device.h> |
Arnaud Patard | 63f385c | 2008-07-08 23:07:48 +0100 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/gpio.h> |
Paul Gortmaker | dc28094 | 2011-07-31 16:17:29 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
Linus Walleij | 7b85b86 | 2013-09-09 16:39:51 +0200 | [diff] [blame^] | 19 | #include <linux/platform_device.h> |
Linus Walleij | f6ffa5e | 2013-09-09 15:00:40 +0200 | [diff] [blame] | 20 | |
| 21 | #define IOP3XX_N_GPIOS 8 |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 22 | |
Linus Walleij | 7b85b86 | 2013-09-09 16:39:51 +0200 | [diff] [blame^] | 23 | #define GPIO_IN 0 |
| 24 | #define GPIO_OUT 1 |
| 25 | #define GPIO_LOW 0 |
| 26 | #define GPIO_HIGH 1 |
| 27 | |
| 28 | /* Memory base offset */ |
| 29 | static void __iomem *base; |
| 30 | |
| 31 | #define IOP3XX_GPIO_REG(reg) (base + (reg)) |
| 32 | #define IOP3XX_GPOE (volatile u32 *)IOP3XX_GPIO_REG(0x0000) |
| 33 | #define IOP3XX_GPID (volatile u32 *)IOP3XX_GPIO_REG(0x0004) |
| 34 | #define IOP3XX_GPOD (volatile u32 *)IOP3XX_GPIO_REG(0x0008) |
| 35 | |
Linus Walleij | 51a97d8 | 2013-09-09 16:07:42 +0200 | [diff] [blame] | 36 | static void gpio_line_config(int line, int direction) |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 37 | { |
| 38 | unsigned long flags; |
| 39 | |
| 40 | local_irq_save(flags); |
| 41 | if (direction == GPIO_IN) { |
| 42 | *IOP3XX_GPOE |= 1 << line; |
| 43 | } else if (direction == GPIO_OUT) { |
| 44 | *IOP3XX_GPOE &= ~(1 << line); |
| 45 | } |
| 46 | local_irq_restore(flags); |
| 47 | } |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 48 | |
Linus Walleij | 51a97d8 | 2013-09-09 16:07:42 +0200 | [diff] [blame] | 49 | static int gpio_line_get(int line) |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 50 | { |
| 51 | return !!(*IOP3XX_GPID & (1 << line)); |
| 52 | } |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 53 | |
Linus Walleij | 51a97d8 | 2013-09-09 16:07:42 +0200 | [diff] [blame] | 54 | static void gpio_line_set(int line, int value) |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 55 | { |
| 56 | unsigned long flags; |
| 57 | |
| 58 | local_irq_save(flags); |
| 59 | if (value == GPIO_LOW) { |
| 60 | *IOP3XX_GPOD &= ~(1 << line); |
| 61 | } else if (value == GPIO_HIGH) { |
| 62 | *IOP3XX_GPOD |= 1 << line; |
| 63 | } |
| 64 | local_irq_restore(flags); |
| 65 | } |
Arnaud Patard | 63f385c | 2008-07-08 23:07:48 +0100 | [diff] [blame] | 66 | |
| 67 | static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 68 | { |
| 69 | gpio_line_config(gpio, GPIO_IN); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int iop3xx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int level) |
| 74 | { |
| 75 | gpio_line_set(gpio, level); |
| 76 | gpio_line_config(gpio, GPIO_OUT); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int iop3xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
| 81 | { |
| 82 | return gpio_line_get(gpio); |
| 83 | } |
| 84 | |
| 85 | static void iop3xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value) |
| 86 | { |
| 87 | gpio_line_set(gpio, value); |
| 88 | } |
| 89 | |
| 90 | static struct gpio_chip iop3xx_chip = { |
| 91 | .label = "iop3xx", |
| 92 | .direction_input = iop3xx_gpio_direction_input, |
| 93 | .get = iop3xx_gpio_get_value, |
| 94 | .direction_output = iop3xx_gpio_direction_output, |
| 95 | .set = iop3xx_gpio_set_value, |
| 96 | .base = 0, |
| 97 | .ngpio = IOP3XX_N_GPIOS, |
| 98 | }; |
| 99 | |
Linus Walleij | 7b85b86 | 2013-09-09 16:39:51 +0200 | [diff] [blame^] | 100 | static int iop3xx_gpio_probe(struct platform_device *pdev) |
Arnaud Patard | 63f385c | 2008-07-08 23:07:48 +0100 | [diff] [blame] | 101 | { |
Linus Walleij | 7b85b86 | 2013-09-09 16:39:51 +0200 | [diff] [blame^] | 102 | struct resource *res; |
| 103 | |
| 104 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 105 | base = (void *) res->start; |
| 106 | |
Arnaud Patard | 63f385c | 2008-07-08 23:07:48 +0100 | [diff] [blame] | 107 | return gpiochip_add(&iop3xx_chip); |
| 108 | } |
Linus Walleij | 7b85b86 | 2013-09-09 16:39:51 +0200 | [diff] [blame^] | 109 | |
| 110 | static struct platform_driver iop3xx_gpio_driver = { |
| 111 | .driver = { |
| 112 | .name = "gpio-iop", |
| 113 | .owner = THIS_MODULE, |
| 114 | }, |
| 115 | .probe = iop3xx_gpio_probe, |
| 116 | }; |
| 117 | |
| 118 | static int __init iop3xx_gpio_init(void) |
| 119 | { |
| 120 | return platform_driver_register(&iop3xx_gpio_driver); |
| 121 | } |
| 122 | arch_initcall(iop3xx_gpio_init); |