blob: e183f5594b51fde1ac256181583394ca05fbb283 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dieter Kiermaiere740eb82009-06-29 14:45:08 +02002/*
3 * arch/arm/plat-orion/gpio.c
4 *
5 * Marvell Orion SoC GPIO handling.
Dieter Kiermaiere740eb82009-06-29 14:45:08 +02006 */
7
8/*
9 * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver.
10 * Removed orion_gpiochip struct and kernel level irq handling.
11 *
12 * Dieter Kiermaier dk-arm-linux@gmx.de
13 */
14
Masahiro Yamada14fecf92017-11-22 02:38:11 +090015#include <linux/bitops.h>
Lei Wen298ae912011-10-18 20:11:42 +053016#include <asm/io.h>
Stefan Roesec2437842014-10-22 12:13:06 +020017#include <asm/arch/soc.h>
Dieter Kiermaiere740eb82009-06-29 14:45:08 +020018#include <asm/arch/gpio.h>
19
20static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
21static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
22
23void __set_direction(unsigned pin, int input)
24{
25 u32 u;
26
27 u = readl(GPIO_IO_CONF(pin));
28 if (input)
29 u |= 1 << (pin & 31);
30 else
31 u &= ~(1 << (pin & 31));
32 writel(u, GPIO_IO_CONF(pin));
33
34 u = readl(GPIO_IO_CONF(pin));
35}
36
Jeroen Hofstee15da9742014-10-08 22:57:37 +020037static void __set_level(unsigned pin, int high)
Dieter Kiermaiere740eb82009-06-29 14:45:08 +020038{
39 u32 u;
40
41 u = readl(GPIO_OUT(pin));
42 if (high)
43 u |= 1 << (pin & 31);
44 else
45 u &= ~(1 << (pin & 31));
46 writel(u, GPIO_OUT(pin));
47}
48
Jeroen Hofstee15da9742014-10-08 22:57:37 +020049static void __set_blinking(unsigned pin, int blink)
Dieter Kiermaiere740eb82009-06-29 14:45:08 +020050{
51 u32 u;
52
53 u = readl(GPIO_BLINK_EN(pin));
54 if (blink)
55 u |= 1 << (pin & 31);
56 else
57 u &= ~(1 << (pin & 31));
58 writel(u, GPIO_BLINK_EN(pin));
59}
60
61int kw_gpio_is_valid(unsigned pin, int mode)
62{
63 if (pin < GPIO_MAX) {
64 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
65 goto err_out;
66
67 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
68 goto err_out;
69 return 0;
70 }
71
72err_out:
73 printf("%s: invalid GPIO %d\n", __func__, pin);
74 return 1;
75}
76
77void kw_gpio_set_valid(unsigned pin, int mode)
78{
79 if (mode == 1)
80 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
81 if (mode & GPIO_INPUT_OK)
82 __set_bit(pin, gpio_valid_input);
83 else
84 __clear_bit(pin, gpio_valid_input);
85 if (mode & GPIO_OUTPUT_OK)
86 __set_bit(pin, gpio_valid_output);
87 else
88 __clear_bit(pin, gpio_valid_output);
89}
90/*
91 * GENERIC_GPIO primitives.
92 */
93int kw_gpio_direction_input(unsigned pin)
94{
Holger Brunck8dd156e2011-08-21 23:21:06 +000095 if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
Dieter Kiermaiere740eb82009-06-29 14:45:08 +020096 return 1;
97
98 /* Configure GPIO direction. */
99 __set_direction(pin, 1);
100
101 return 0;
102}
103
104int kw_gpio_direction_output(unsigned pin, int value)
105{
106 if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
107 {
108 printf("%s: invalid GPIO %d\n", __func__, pin);
109 return 1;
110 }
111
112 __set_blinking(pin, 0);
113
114 /* Configure GPIO output value. */
115 __set_level(pin, value);
116
117 /* Configure GPIO direction. */
118 __set_direction(pin, 0);
119
120 return 0;
121}
122
123int kw_gpio_get_value(unsigned pin)
124{
125 int val;
126
127 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
128 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
129 else
130 val = readl(GPIO_OUT(pin));
131
132 return (val >> (pin & 31)) & 1;
133}
134
135void kw_gpio_set_value(unsigned pin, int value)
136{
137 /* Configure GPIO output value. */
138 __set_level(pin, value);
139}
140
141void kw_gpio_set_blink(unsigned pin, int blink)
142{
143 /* Set output value to zero. */
144 __set_level(pin, 0);
145
146 /* Set blinking. */
147 __set_blinking(pin, blink);
148}