blob: 4e4cd125457dcb930a346d9f59d2b3c10173f33e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesead38e5d2012-02-14 14:01:36 +01002/*
3 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
Stefan Roesead38e5d2012-02-14 14:01:36 +01004 */
5
6/*
7 * Driver for SPEAr600 GPIO controller
8 */
9
10#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Stefan Roesead38e5d2012-02-14 14:01:36 +010012#include <asm/arch/hardware.h>
13#include <asm/gpio.h>
14#include <asm/io.h>
15#include <errno.h>
16
17static int gpio_direction(unsigned gpio,
18 enum gpio_direction direction)
19{
20 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
21 u32 val;
22
23 val = readl(&regs->gpiodir);
24
25 if (direction == GPIO_DIRECTION_OUT)
26 val |= 1 << gpio;
27 else
28 val &= ~(1 << gpio);
29
30 writel(val, &regs->gpiodir);
31
32 return 0;
33}
34
35int gpio_set_value(unsigned gpio, int value)
36{
37 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
38
Axel Lin6e903e12014-05-25 10:31:18 +080039 if (value)
40 writel(1 << gpio, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
41 else
42 writel(0, &regs->gpiodata[DATA_REG_ADDR(gpio)]);
Stefan Roesead38e5d2012-02-14 14:01:36 +010043
44 return 0;
45}
46
47int gpio_get_value(unsigned gpio)
48{
49 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE;
50 u32 val;
51
52 val = readl(&regs->gpiodata[DATA_REG_ADDR(gpio)]);
53
54 return !!val;
55}
56
57int gpio_request(unsigned gpio, const char *label)
58{
59 if (gpio >= SPEAR_GPIO_COUNT)
60 return -EINVAL;
61
62 return 0;
63}
64
65int gpio_free(unsigned gpio)
66{
67 return 0;
68}
69
70void gpio_toggle_value(unsigned gpio)
71{
72 gpio_set_value(gpio, !gpio_get_value(gpio));
73}
74
75int gpio_direction_input(unsigned gpio)
76{
77 return gpio_direction(gpio, GPIO_DIRECTION_IN);
78}
79
80int gpio_direction_output(unsigned gpio, int value)
81{
82 int ret = gpio_direction(gpio, GPIO_DIRECTION_OUT);
83
84 if (ret < 0)
85 return ret;
86
87 gpio_set_value(gpio, value);
88 return 0;
89}