Patrick Georgi | 7051707 | 2020-05-10 18:47:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 4 | #include <gpio.h> |
Julius Werner | 73d1ed6 | 2014-10-20 13:20:49 -0700 | [diff] [blame] | 5 | #include <soc/iomap.h> |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 6 | |
| 7 | /******************************************************* |
| 8 | Function description: check for invalid GPIO # |
| 9 | Arguments : |
| 10 | gpio_t gpio - Gpio number |
| 11 | |
| 12 | Return : GPIO Valid(0)/Invalid(1) |
| 13 | *******************************************************/ |
| 14 | |
| 15 | static inline int gpio_not_valid(gpio_t gpio) |
| 16 | { |
| 17 | return (gpio > GPIO_MAX_NUM); |
| 18 | } |
| 19 | |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 20 | /******************************************************* |
| 21 | Function description: configure GPIO functinality |
| 22 | Arguments : |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 23 | gpio_t gpio - Gpio number |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 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. |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 28 | |
| 29 | Return : None |
| 30 | *******************************************************/ |
| 31 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 32 | void gpio_tlmm_config_set(gpio_t gpio, unsigned int func, |
| 33 | unsigned int pull, unsigned int drvstr, |
| 34 | unsigned int enable) |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 35 | { |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 36 | unsigned int val = 0; |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 37 | |
| 38 | if (gpio_not_valid(gpio)) |
| 39 | return; |
| 40 | |
Elyes HAOUAS | 88607a4 | 2018-10-05 10:36:45 +0200 | [diff] [blame] | 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; |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 45 | |
Elyes HAOUAS | 88607a4 | 2018-10-05 10:36:45 +0200 | [diff] [blame] | 46 | write32(GPIO_CONFIG_ADDR(gpio), val); |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 49 | /******************************************************* |
| 50 | Function description: Get GPIO configuration |
| 51 | Arguments : |
| 52 | gpio_t gpio - Gpio number |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 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. |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 57 | |
| 58 | Return : None |
| 59 | *******************************************************/ |
| 60 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 61 | void gpio_tlmm_config_get(gpio_t gpio, unsigned int *func, |
| 62 | unsigned int *pull, unsigned int *drvstr, |
| 63 | unsigned int *enable) |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 64 | { |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 65 | unsigned int val; |
Elyes HAOUAS | 88607a4 | 2018-10-05 10:36:45 +0200 | [diff] [blame] | 66 | void *addr = GPIO_CONFIG_ADDR(gpio); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 67 | |
| 68 | if (gpio_not_valid(gpio)) |
| 69 | return; |
| 70 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 71 | val = read32(addr); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 72 | |
| 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; |
| 77 | } |
| 78 | |
| 79 | /******************************************************* |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 80 | Function description: get GPIO IO functinality details |
| 81 | Arguments : |
| 82 | gpio_t gpio - Gpio number |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 83 | unsigned *in - Value of GPIO input |
| 84 | unsigned *out - Value of GPIO output |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 85 | |
| 86 | Return : None |
| 87 | *******************************************************/ |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 88 | int gpio_get(gpio_t gpio) |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 89 | { |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 90 | if (gpio_not_valid(gpio)) |
| 91 | return -1; |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 92 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 93 | return (read32(GPIO_IN_OUT_ADDR(gpio)) >> GPIO_IO_IN_SHIFT) & |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 94 | GPIO_IO_IN_MASK; |
| 95 | } |
| 96 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 97 | void gpio_set(gpio_t gpio, int value) |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 98 | { |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 99 | if (gpio_not_valid(gpio)) |
| 100 | return; |
| 101 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 102 | write32(GPIO_IN_OUT_ADDR(gpio), (value & 1) << GPIO_IO_OUT_SHIFT); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 103 | } |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 104 | |
| 105 | void gpio_input_pulldown(gpio_t gpio) |
| 106 | { |
| 107 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 108 | GPIO_PULL_DOWN, GPIO_2MA, GPIO_DISABLE); |
| 109 | } |
| 110 | |
| 111 | void gpio_input_pullup(gpio_t gpio) |
| 112 | { |
| 113 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 114 | GPIO_PULL_UP, GPIO_2MA, GPIO_DISABLE); |
| 115 | } |
| 116 | |
| 117 | void gpio_input(gpio_t gpio) |
| 118 | { |
| 119 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 120 | GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE); |
| 121 | } |