blob: 24a86b05dc8cb4f38f69e05d2686b2e18ae257ef [file] [log] [blame]
Lennert Buytenhek72edd842006-09-18 23:23:07 +01001/*
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 Patard63f385c2008-07-08 23:07:48 +010014#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/errno.h>
17#include <linux/gpio.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040018#include <linux/export.h>
Linus Walleij7b85b862013-09-09 16:39:51 +020019#include <linux/platform_device.h>
Linus Walleijf6ffa5e2013-09-09 15:00:40 +020020
21#define IOP3XX_N_GPIOS 8
Lennert Buytenhek72edd842006-09-18 23:23:07 +010022
Linus Walleij7b85b862013-09-09 16:39:51 +020023#define GPIO_IN 0
24#define GPIO_OUT 1
25#define GPIO_LOW 0
26#define GPIO_HIGH 1
27
28/* Memory base offset */
29static 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 Walleij51a97d82013-09-09 16:07:42 +020036static void gpio_line_config(int line, int direction)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010037{
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 Buytenhek72edd842006-09-18 23:23:07 +010048
Linus Walleij51a97d82013-09-09 16:07:42 +020049static int gpio_line_get(int line)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010050{
51 return !!(*IOP3XX_GPID & (1 << line));
52}
Lennert Buytenhek72edd842006-09-18 23:23:07 +010053
Linus Walleij51a97d82013-09-09 16:07:42 +020054static void gpio_line_set(int line, int value)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010055{
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 Patard63f385c2008-07-08 23:07:48 +010066
67static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
68{
69 gpio_line_config(gpio, GPIO_IN);
70 return 0;
71}
72
73static 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
80static int iop3xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
81{
82 return gpio_line_get(gpio);
83}
84
85static void iop3xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
86{
87 gpio_line_set(gpio, value);
88}
89
90static 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 Walleij7b85b862013-09-09 16:39:51 +0200100static int iop3xx_gpio_probe(struct platform_device *pdev)
Arnaud Patard63f385c2008-07-08 23:07:48 +0100101{
Linus Walleij7b85b862013-09-09 16:39:51 +0200102 struct resource *res;
103
104 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 base = (void *) res->start;
106
Arnaud Patard63f385c2008-07-08 23:07:48 +0100107 return gpiochip_add(&iop3xx_chip);
108}
Linus Walleij7b85b862013-09-09 16:39:51 +0200109
110static struct platform_driver iop3xx_gpio_driver = {
111 .driver = {
112 .name = "gpio-iop",
113 .owner = THIS_MODULE,
114 },
115 .probe = iop3xx_gpio_probe,
116};
117
118static int __init iop3xx_gpio_init(void)
119{
120 return platform_driver_register(&iop3xx_gpio_driver);
121}
122arch_initcall(iop3xx_gpio_init);