]> Git Repo - u-boot.git/blame - drivers/gpio/kw_gpio.c
Restore patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet"
[u-boot.git] / drivers / gpio / kw_gpio.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
ec164410
DK
2/*
3 * arch/arm/plat-orion/gpio.c
4 *
5 * Marvell Orion SoC GPIO handling.
ec164410
DK
6 */
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 [email protected]
13 */
14
ed3986ca 15#include <linux/bitops.h>
a7efd719 16#include <asm/io.h>
3dc23f78 17#include <asm/arch/soc.h>
ec164410
DK
18#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
2115d89d 37static void __set_level(unsigned pin, int high)
ec164410
DK
38{
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
2115d89d 49static void __set_blinking(unsigned pin, int blink)
ec164410
DK
50{
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{
e5afdf58 95 if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
ec164410
DK
96 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}
This page took 0.320231 seconds and 4 git commands to generate.