Angel Pons | bbc99cf | 2020-04-04 18:51:23 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Lin Huang | 589474f | 2017-08-02 16:59:06 +0800 | [diff] [blame] | 4 | #include <assert.h> |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 5 | #include <gpio.h> |
| 6 | #include <soc/gpio.h> |
| 7 | #include <soc/grf.h> |
| 8 | #include <soc/soc.h> |
Elyes HAOUAS | 27d02d8 | 2019-05-15 21:11:39 +0200 | [diff] [blame] | 9 | #include <types.h> |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 10 | |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 11 | static void gpio_set_dir(gpio_t gpio, enum gpio_dir dir) |
| 12 | { |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 13 | clrsetbits32(&gpio_port[gpio.port]->swporta_ddr, |
| 14 | 1 << gpio.num, dir << gpio.num); |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static void gpio_set_pull(gpio_t gpio, enum gpio_pull pull) |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 18 | { |
Shunqian Zheng | 74bb412 | 2016-05-17 14:00:04 +0800 | [diff] [blame] | 19 | u32 pull_val = gpio_get_pull_val(gpio, pull); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 20 | if (is_pmu_gpio(gpio) && CONFIG(SOC_ROCKCHIP_RK3288)) |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 21 | clrsetbits32(gpio_grf_reg(gpio), 3 << (gpio.idx * 2), |
| 22 | pull_val << (gpio.idx * 2)); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 23 | else |
| 24 | write32(gpio_grf_reg(gpio), RK_CLRSETBITS(3 << (gpio.idx * 2), |
Shunqian Zheng | 74bb412 | 2016-05-17 14:00:04 +0800 | [diff] [blame] | 25 | pull_val << (gpio.idx * 2))); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void gpio_input(gpio_t gpio) |
| 29 | { |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 30 | gpio_set_pull(gpio, GPIO_PULLNONE); |
| 31 | gpio_set_dir(gpio, GPIO_INPUT); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void gpio_input_pulldown(gpio_t gpio) |
| 35 | { |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 36 | gpio_set_pull(gpio, GPIO_PULLDOWN); |
| 37 | gpio_set_dir(gpio, GPIO_INPUT); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void gpio_input_pullup(gpio_t gpio) |
| 41 | { |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 42 | gpio_set_pull(gpio, GPIO_PULLUP); |
| 43 | gpio_set_dir(gpio, GPIO_INPUT); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 44 | } |
| 45 | |
Lin Huang | 589474f | 2017-08-02 16:59:06 +0800 | [diff] [blame] | 46 | void gpio_input_irq(gpio_t gpio, enum gpio_irq_type type, enum gpio_pull pull) |
Jeffy Chen | b0b5987 | 2017-03-03 18:24:02 +0800 | [diff] [blame] | 47 | { |
| 48 | uint32_t int_polarity, inttype_level; |
| 49 | uint32_t mask = BIT(gpio.num); |
| 50 | |
Lin Huang | 589474f | 2017-08-02 16:59:06 +0800 | [diff] [blame] | 51 | /* gpio pull only PULLNONE, PULLUP, PULLDOWN status */ |
| 52 | assert(pull <= GPIO_PULLDOWN); |
| 53 | |
| 54 | gpio_set_dir(gpio, GPIO_INPUT); |
| 55 | gpio_set_pull(gpio, pull); |
Jeffy Chen | b0b5987 | 2017-03-03 18:24:02 +0800 | [diff] [blame] | 56 | |
| 57 | int_polarity = inttype_level = 0; |
| 58 | switch (type) { |
| 59 | case IRQ_TYPE_EDGE_RISING: |
| 60 | int_polarity = mask; |
| 61 | inttype_level = mask; |
| 62 | break; |
| 63 | case IRQ_TYPE_EDGE_FALLING: |
| 64 | inttype_level = mask; |
| 65 | break; |
| 66 | case IRQ_TYPE_LEVEL_HIGH: |
| 67 | int_polarity = mask; |
| 68 | break; |
| 69 | case IRQ_TYPE_LEVEL_LOW: |
| 70 | break; |
| 71 | } |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 72 | clrsetbits32(&gpio_port[gpio.port]->int_polarity, |
| 73 | mask, int_polarity); |
| 74 | clrsetbits32(&gpio_port[gpio.port]->inttype_level, |
| 75 | mask, inttype_level); |
Jeffy Chen | b0b5987 | 2017-03-03 18:24:02 +0800 | [diff] [blame] | 76 | |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 77 | setbits32(&gpio_port[gpio.port]->inten, mask); |
| 78 | clrbits32(&gpio_port[gpio.port]->intmask, mask); |
Jeffy Chen | b0b5987 | 2017-03-03 18:24:02 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int gpio_irq_status(gpio_t gpio) |
| 82 | { |
| 83 | uint32_t mask = BIT(gpio.num); |
| 84 | uint32_t int_status = read32(&gpio_port[gpio.port]->int_status); |
| 85 | |
| 86 | if (!(int_status & mask)) |
| 87 | return 0; |
| 88 | |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 89 | setbits32(&gpio_port[gpio.port]->porta_eoi, mask); |
Jeffy Chen | b0b5987 | 2017-03-03 18:24:02 +0800 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 93 | int gpio_get(gpio_t gpio) |
| 94 | { |
| 95 | return (read32(&gpio_port[gpio.port]->ext_porta) >> gpio.num) & 0x1; |
| 96 | } |
| 97 | |
Julius Werner | ffeee42 | 2018-03-27 16:22:00 -0700 | [diff] [blame] | 98 | void gpio_set(gpio_t gpio, int value) |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 99 | { |
Julius Werner | 55009af | 2019-12-02 22:03:27 -0800 | [diff] [blame] | 100 | clrsetbits32(&gpio_port[gpio.port]->swporta_dr, 1 << gpio.num, |
| 101 | !!value << gpio.num); |
Julius Werner | ffeee42 | 2018-03-27 16:22:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void gpio_output(gpio_t gpio, int value) |
| 105 | { |
| 106 | gpio_set(gpio, value); |
Julius Werner | 2768a11 | 2016-09-01 22:55:58 -0700 | [diff] [blame] | 107 | gpio_set_dir(gpio, GPIO_OUTPUT); |
| 108 | gpio_set_pull(gpio, GPIO_PULLNONE); |
Shunqian Zheng | 015ae11 | 2016-04-20 20:35:09 +0800 | [diff] [blame] | 109 | } |