blob: cc208a6ddc9d9f0b2245f0e4edddca1b64500274 [file] [log] [blame]
Angel Pons7c1d70e2020-04-04 18:51:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Nitheesh Sekarea4c7d02018-09-16 19:01:01 +05302
3#include <device/mmio.h>
4#include <types.h>
Nitheesh Sekarea4c7d02018-09-16 19:01:01 +05305#include <gpio.h>
6
7void gpio_configure(gpio_t gpio, uint32_t func, uint32_t pull,
8 uint32_t drive_str, uint32_t enable)
9{
10 uint32_t reg_val;
11 struct tlmm_gpio *regs = (void *)(uintptr_t)gpio.addr;
12
13 reg_val = ((enable & GPIO_CFG_OE_BMSK) << GPIO_CFG_OE_SHFT) |
14 ((drive_str & GPIO_CFG_DRV_BMSK) << GPIO_CFG_DRV_SHFT) |
15 ((func & GPIO_CFG_FUNC_BMSK) << GPIO_CFG_FUNC_SHFT) |
16 ((pull & GPIO_CFG_PULL_BMSK) << GPIO_CFG_PULL_SHFT);
17
18 write32(&regs->cfg, reg_val);
19}
20
21void gpio_set(gpio_t gpio, int value)
22{
23 struct tlmm_gpio *regs = (void *)(uintptr_t)gpio.addr;
24 write32(&regs->in_out, (!!value) << GPIO_IO_OUT_SHFT);
25}
26
27int gpio_get(gpio_t gpio)
28{
29 struct tlmm_gpio *regs = (void *)(uintptr_t)gpio.addr;
30
31 return ((read32(&regs->in_out) >> GPIO_IO_IN_SHFT) &
32 GPIO_IO_IN_BMSK);
33}
34
35void gpio_input_pulldown(gpio_t gpio)
36{
37 gpio_configure(gpio, GPIO_FUNC_GPIO,
38 GPIO_PULL_DOWN, GPIO_2MA, GPIO_DISABLE);
39}
40
41void gpio_input_pullup(gpio_t gpio)
42{
43 gpio_configure(gpio, GPIO_FUNC_GPIO,
44 GPIO_PULL_UP, GPIO_2MA, GPIO_DISABLE);
45}
46
47void gpio_input(gpio_t gpio)
48{
49 gpio_configure(gpio, GPIO_FUNC_GPIO,
50 GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE);
51}
52
53void gpio_output(gpio_t gpio, int value)
54{
55 gpio_set(gpio, value);
56 gpio_configure(gpio, GPIO_FUNC_GPIO,
57 GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
58}
Taniya Das4b766392019-04-04 15:28:36 +053059
60void gpio_input_irq(gpio_t gpio, enum gpio_irq_type type, uint32_t pull)
61{
62 struct tlmm_gpio *regs = (void *)(uintptr_t)gpio.addr;
63
64 gpio_configure(gpio, GPIO_FUNC_GPIO,
65 pull, GPIO_2MA, GPIO_DISABLE);
66
Julius Werner55009af2019-12-02 22:03:27 -080067 clrsetbits32(&regs->intr_cfg, GPIO_INTR_DECT_CTL_MASK <<
Taniya Das4b766392019-04-04 15:28:36 +053068 GPIO_INTR_DECT_CTL_SHIFT, type << GPIO_INTR_DECT_CTL_SHIFT);
Julius Werner55009af2019-12-02 22:03:27 -080069 clrsetbits32(&regs->intr_cfg, GPIO_INTR_RAW_STATUS_ENABLE
Taniya Das4b766392019-04-04 15:28:36 +053070 << GPIO_INTR_RAW_STATUS_EN_SHIFT, GPIO_INTR_RAW_STATUS_ENABLE
71 << GPIO_INTR_RAW_STATUS_EN_SHIFT);
72}
73
74int gpio_irq_status(gpio_t gpio)
75{
76 struct tlmm_gpio *regs = (void *)(uintptr_t)gpio.addr;
77
78 if (!(read32(&regs->intr_status) & GPIO_INTR_STATUS_MASK))
79 return 0;
80
81 write32(&regs->intr_status, GPIO_INTR_STATUS_DISABLE);
82 return 1;
83}