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