blob: 860c535922fd184d929e1fda6a9fff2c9452622c [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 Walleije3f94b32013-09-09 16:54:32 +020020#include <linux/bitops.h>
21#include <linux/io.h>
Linus Walleijf6ffa5e2013-09-09 15:00:40 +020022
23#define IOP3XX_N_GPIOS 8
Lennert Buytenhek72edd842006-09-18 23:23:07 +010024
Linus Walleij7b85b862013-09-09 16:39:51 +020025#define GPIO_IN 0
26#define GPIO_OUT 1
27#define GPIO_LOW 0
28#define GPIO_HIGH 1
29
30/* Memory base offset */
31static void __iomem *base;
32
33#define IOP3XX_GPIO_REG(reg) (base + (reg))
Linus Walleije3f94b32013-09-09 16:54:32 +020034#define IOP3XX_GPOE IOP3XX_GPIO_REG(0x0000)
35#define IOP3XX_GPID IOP3XX_GPIO_REG(0x0004)
36#define IOP3XX_GPOD IOP3XX_GPIO_REG(0x0008)
Linus Walleij7b85b862013-09-09 16:39:51 +020037
Linus Walleij51a97d82013-09-09 16:07:42 +020038static void gpio_line_config(int line, int direction)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010039{
40 unsigned long flags;
Linus Walleije3f94b32013-09-09 16:54:32 +020041 u32 val;
Lennert Buytenhek72edd842006-09-18 23:23:07 +010042
43 local_irq_save(flags);
Linus Walleije3f94b32013-09-09 16:54:32 +020044 val = readl(IOP3XX_GPOE);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010045 if (direction == GPIO_IN) {
Linus Walleije3f94b32013-09-09 16:54:32 +020046 val |= BIT(line);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010047 } else if (direction == GPIO_OUT) {
Linus Walleije3f94b32013-09-09 16:54:32 +020048 val &= ~BIT(line);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010049 }
Linus Walleije3f94b32013-09-09 16:54:32 +020050 writel(val, IOP3XX_GPOE);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010051 local_irq_restore(flags);
52}
Lennert Buytenhek72edd842006-09-18 23:23:07 +010053
Linus Walleij51a97d82013-09-09 16:07:42 +020054static int gpio_line_get(int line)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010055{
Linus Walleije3f94b32013-09-09 16:54:32 +020056 return !!(readl(IOP3XX_GPID) & BIT(line));
Lennert Buytenhek72edd842006-09-18 23:23:07 +010057}
Lennert Buytenhek72edd842006-09-18 23:23:07 +010058
Linus Walleij51a97d82013-09-09 16:07:42 +020059static void gpio_line_set(int line, int value)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010060{
61 unsigned long flags;
Linus Walleije3f94b32013-09-09 16:54:32 +020062 u32 val;
Lennert Buytenhek72edd842006-09-18 23:23:07 +010063
64 local_irq_save(flags);
Linus Walleije3f94b32013-09-09 16:54:32 +020065 val = readl(IOP3XX_GPOD);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010066 if (value == GPIO_LOW) {
Linus Walleije3f94b32013-09-09 16:54:32 +020067 val &= ~BIT(line);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010068 } else if (value == GPIO_HIGH) {
Linus Walleije3f94b32013-09-09 16:54:32 +020069 val |= BIT(line);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010070 }
Linus Walleije3f94b32013-09-09 16:54:32 +020071 writel(val, IOP3XX_GPOD);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010072 local_irq_restore(flags);
73}
Arnaud Patard63f385c2008-07-08 23:07:48 +010074
75static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
76{
77 gpio_line_config(gpio, GPIO_IN);
78 return 0;
79}
80
81static int iop3xx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
82{
83 gpio_line_set(gpio, level);
84 gpio_line_config(gpio, GPIO_OUT);
85 return 0;
86}
87
88static int iop3xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
89{
90 return gpio_line_get(gpio);
91}
92
93static void iop3xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
94{
95 gpio_line_set(gpio, value);
96}
97
98static struct gpio_chip iop3xx_chip = {
99 .label = "iop3xx",
100 .direction_input = iop3xx_gpio_direction_input,
101 .get = iop3xx_gpio_get_value,
102 .direction_output = iop3xx_gpio_direction_output,
103 .set = iop3xx_gpio_set_value,
104 .base = 0,
105 .ngpio = IOP3XX_N_GPIOS,
106};
107
Linus Walleij7b85b862013-09-09 16:39:51 +0200108static int iop3xx_gpio_probe(struct platform_device *pdev)
Arnaud Patard63f385c2008-07-08 23:07:48 +0100109{
Linus Walleij7b85b862013-09-09 16:39:51 +0200110 struct resource *res;
111
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linus Walleije34ca9d2013-09-09 16:59:54 +0200113 base = devm_ioremap_resource(&pdev->dev, res);
Bartlomiej Zolnierkiewicz138d8762014-03-18 10:58:33 +0100114 if (IS_ERR(base))
115 return PTR_ERR(base);
Linus Walleij7b85b862013-09-09 16:39:51 +0200116
Laxman Dewangan0c638752016-02-22 17:43:28 +0530117 return devm_gpiochip_add_data(&pdev->dev, &iop3xx_chip, NULL);
Arnaud Patard63f385c2008-07-08 23:07:48 +0100118}
Linus Walleij7b85b862013-09-09 16:39:51 +0200119
120static struct platform_driver iop3xx_gpio_driver = {
121 .driver = {
122 .name = "gpio-iop",
Linus Walleij7b85b862013-09-09 16:39:51 +0200123 },
124 .probe = iop3xx_gpio_probe,
125};
126
127static int __init iop3xx_gpio_init(void)
128{
129 return platform_driver_register(&iop3xx_gpio_driver);
130}
131arch_initcall(iop3xx_gpio_init);