mb/system76/cml-u/dt: Make use of chipset devicetree
[coreboot.git] / src / soc / qualcomm / ipq806x / gpio.c
blob8e248c50cdf1323daca3331bd89cf20d1228bb74
1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <device/mmio.h>
4 #include <gpio.h>
5 #include <soc/iomap.h>
7 /*******************************************************
8 Function description: check for invalid GPIO #
9 Arguments :
10 gpio_t gpio - Gpio number
12 Return : GPIO Valid(0)/Invalid(1)
13 *******************************************************/
15 static inline int gpio_not_valid(gpio_t gpio)
17 return (gpio > GPIO_MAX_NUM);
20 /*******************************************************
21 Function description: configure GPIO functionality
22 Arguments :
23 gpio_t gpio - Gpio number
24 unsigned func - Functionality number
25 unsigned pull - pull up/down, no pull range(0-3)
26 unsigned drvstr - range (0 - 7)-> (2- 16)MA steps of 2
27 unsigned enable - 0 Disable, 1 - Enable.
29 Return : None
30 *******************************************************/
32 void gpio_tlmm_config_set(gpio_t gpio, unsigned int func,
33 unsigned int pull, unsigned int drvstr,
34 unsigned int enable)
36 unsigned int val = 0;
38 if (gpio_not_valid(gpio))
39 return;
41 val |= (pull & GPIO_CFG_PULL_MASK) << GPIO_CFG_PULL_SHIFT;
42 val |= (func & GPIO_CFG_FUNC_MASK) << GPIO_CFG_FUNC_SHIFT;
43 val |= (drvstr & GPIO_CFG_DRV_MASK) << GPIO_CFG_DRV_SHIFT;
44 val |= (enable & GPIO_CFG_OE_MASK) << GPIO_CFG_OE_SHIFT;
46 write32(GPIO_CONFIG_ADDR(gpio), val);
49 /*******************************************************
50 Function description: Get GPIO configuration
51 Arguments :
52 gpio_t gpio - Gpio number
53 unsigned *func - Functionality number
54 unsigned *pull - pull up/down, no pull range(0-3)
55 unsigned *drvstr - range (0 - 7)-> (2- 16)MA steps of 2
56 unsigned *enable - 0 - Disable, 1- Enable.
58 Return : None
59 *******************************************************/
61 void gpio_tlmm_config_get(gpio_t gpio, unsigned int *func,
62 unsigned int *pull, unsigned int *drvstr,
63 unsigned int *enable)
65 unsigned int val;
66 void *addr = GPIO_CONFIG_ADDR(gpio);
68 if (gpio_not_valid(gpio))
69 return;
71 val = read32(addr);
73 *pull = (val >> GPIO_CFG_PULL_SHIFT) & GPIO_CFG_PULL_MASK;
74 *func = (val >> GPIO_CFG_FUNC_SHIFT) & GPIO_CFG_FUNC_MASK;
75 *drvstr = (val >> GPIO_CFG_DRV_SHIFT) & GPIO_CFG_DRV_MASK;
76 *enable = (val >> GPIO_CFG_OE_SHIFT) & GPIO_CFG_OE_MASK;
79 /*******************************************************
80 Function description: get GPIO IO functionality details
81 Arguments :
82 gpio_t gpio - Gpio number
83 unsigned *in - Value of GPIO input
84 unsigned *out - Value of GPIO output
86 Return : None
87 *******************************************************/
88 int gpio_get(gpio_t gpio)
90 if (gpio_not_valid(gpio))
91 return -1;
93 return (read32(GPIO_IN_OUT_ADDR(gpio)) >> GPIO_IO_IN_SHIFT) &
94 GPIO_IO_IN_MASK;
97 void gpio_set(gpio_t gpio, int value)
99 if (gpio_not_valid(gpio))
100 return;
102 write32(GPIO_IN_OUT_ADDR(gpio), (value & 1) << GPIO_IO_OUT_SHIFT);
105 void gpio_input_pulldown(gpio_t gpio)
107 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
108 GPIO_PULL_DOWN, GPIO_2MA, GPIO_DISABLE);
111 void gpio_input_pullup(gpio_t gpio)
113 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
114 GPIO_PULL_UP, GPIO_2MA, GPIO_DISABLE);
117 void gpio_input(gpio_t gpio)
119 gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE,
120 GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE);