1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <device/mmio.h>
7 /*******************************************************
8 Function description: check for invalid GPIO #
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
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.
30 *******************************************************/
32 void gpio_tlmm_config_set(gpio_t gpio
, unsigned int func
,
33 unsigned int pull
, unsigned int drvstr
,
38 if (gpio_not_valid(gpio
))
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
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.
59 *******************************************************/
61 void gpio_tlmm_config_get(gpio_t gpio
, unsigned int *func
,
62 unsigned int *pull
, unsigned int *drvstr
,
66 void *addr
= GPIO_CONFIG_ADDR(gpio
);
68 if (gpio_not_valid(gpio
))
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
82 gpio_t gpio - Gpio number
83 unsigned *in - Value of GPIO input
84 unsigned *out - Value of GPIO output
87 *******************************************************/
88 int gpio_get(gpio_t gpio
)
90 if (gpio_not_valid(gpio
))
93 return (read32(GPIO_IN_OUT_ADDR(gpio
)) >> GPIO_IO_IN_SHIFT
) &
97 void gpio_set(gpio_t gpio
, int value
)
99 if (gpio_not_valid(gpio
))
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
);