blob: 41352419d96ed4304fbc7059a832d995af5d41a7 [file] [log] [blame]
Varadarajan Narayanana6935c22016-03-02 16:57:10 +05301/*
2 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Kyösti Mälkki13f66502019-03-03 08:01:05 +020030#include <device/mmio.h>
Varadarajan Narayanana6935c22016-03-02 16:57:10 +053031#include <gpio.h>
32#include <soc/iomap.h>
33
34/*******************************************************
35Function description: check for invalid GPIO #
36Arguments :
37gpio_t gpio - Gpio number
38
39Return : GPIO Valid(0)/Invalid(1)
40*******************************************************/
41
42static inline int gpio_not_valid(gpio_t gpio)
43{
44 return (gpio > GPIO_MAX_NUM);
45}
46
47
48/*******************************************************
49Function description: configure GPIO functinality
50Arguments :
51gpio_t gpio - Gpio number
52unsigned func - Functionality number
53unsigned pull - pull up/down, no pull range(0-3)
54unsigned drvstr - range (0 - 7)-> (2- 16)MA steps of 2
55unsigned enable - 0 Disable, 1 - Enable.
56
57Return : None
58*******************************************************/
59
60
Martin Roth57e89092019-10-23 21:45:23 -060061void gpio_tlmm_config_set(gpio_t gpio, unsigned int func,
62 unsigned int pull, unsigned int drvstr,
63 unsigned int enable)
Varadarajan Narayanana6935c22016-03-02 16:57:10 +053064{
Martin Roth57e89092019-10-23 21:45:23 -060065 unsigned int val = 0;
Varadarajan Narayanana6935c22016-03-02 16:57:10 +053066
67 if (gpio_not_valid(gpio))
68 return;
69
70 val |= (pull & GPIO_CFG_PULL_MASK) << GPIO_CFG_PULL_SHIFT;
71 val |= (func & GPIO_CFG_FUNC_MASK) << GPIO_CFG_FUNC_SHIFT;
72 val |= (drvstr & GPIO_CFG_DRV_MASK) << GPIO_CFG_DRV_SHIFT;
73 val |= (enable & GPIO_CFG_OE_MASK) << GPIO_CFG_OE_SHIFT;
74
75 write32(GPIO_CONFIG_ADDR(gpio), val);
76}
77
78/*******************************************************
79Function description: Get GPIO configuration
80Arguments :
81gpio_t gpio - Gpio number
82unsigned *func - Functionality number
83unsigned *pull - pull up/down, no pull range(0-3)
84unsigned *drvstr - range (0 - 7)-> (2- 16)MA steps of 2
85unsigned *enable - 0 - Disable, 1- Enable.
86
87Return : None
88*******************************************************/
89
90
Martin Roth57e89092019-10-23 21:45:23 -060091void gpio_tlmm_config_get(gpio_t gpio, unsigned int *func,
92 unsigned int *pull, unsigned int *drvstr,
93 unsigned int *enable)
Varadarajan Narayanana6935c22016-03-02 16:57:10 +053094{
Martin Roth57e89092019-10-23 21:45:23 -060095 unsigned int val;
Varadarajan Narayanana6935c22016-03-02 16:57:10 +053096 void *addr = GPIO_CONFIG_ADDR(gpio);
97
98 if (gpio_not_valid(gpio))
99 return;
100
101 val = read32(addr);
102
103 *pull = (val >> GPIO_CFG_PULL_SHIFT) & GPIO_CFG_PULL_MASK;
104 *func = (val >> GPIO_CFG_FUNC_SHIFT) & GPIO_CFG_FUNC_MASK;
105 *drvstr = (val >> GPIO_CFG_DRV_SHIFT) & GPIO_CFG_DRV_MASK;
106 *enable = (val >> GPIO_CFG_OE_SHIFT) & GPIO_CFG_OE_MASK;
107}
108
109/*******************************************************
110Function description: get GPIO IO functinality details
111Arguments :
112gpio_t gpio - Gpio number
113unsigned *in - Value of GPIO input
114unsigned *out - Value of GPIO output
115
116Return : None
117*******************************************************/
118int gpio_get(gpio_t gpio)
119{
120 if (gpio_not_valid(gpio))
121 return -1;
122
123
124 return (read32(GPIO_IN_OUT_ADDR(gpio)) >> GPIO_IO_IN_SHIFT) &
125 GPIO_IO_IN_MASK;
126}
127
128void gpio_set(gpio_t gpio, int value)
129{
130 if (gpio_not_valid(gpio))
131 return;
132
133 write32(GPIO_IN_OUT_ADDR(gpio), (value & 1) << GPIO_IO_OUT_SHIFT);
134}
135
136void gpio_input_pulldown(gpio_t gpio)
137{
138 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
139 GPIO_PULL_DOWN, GPIO_2MA, GPIO_DISABLE);
140}
141
142void gpio_input_pullup(gpio_t gpio)
143{
144 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
145 GPIO_PULL_UP, GPIO_2MA, GPIO_DISABLE);
146}
147
148void gpio_input(gpio_t gpio)
149{
150 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
151 GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE);
152}