Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in |
| 11 | * the documentation and/or other materials provided with the |
| 12 | * distribution. |
| 13 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this |
| 15 | * software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 31 | #include <arch/io.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 32 | #include <gpio.h> |
Julius Werner | 73d1ed6 | 2014-10-20 13:20:49 -0700 | [diff] [blame] | 33 | #include <soc/iomap.h> |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 34 | |
| 35 | /******************************************************* |
| 36 | Function description: check for invalid GPIO # |
| 37 | Arguments : |
| 38 | gpio_t gpio - Gpio number |
| 39 | |
| 40 | Return : GPIO Valid(0)/Invalid(1) |
| 41 | *******************************************************/ |
| 42 | |
| 43 | static inline int gpio_not_valid(gpio_t gpio) |
| 44 | { |
| 45 | return (gpio > GPIO_MAX_NUM); |
| 46 | } |
| 47 | |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 48 | |
| 49 | /******************************************************* |
| 50 | Function description: configure GPIO functinality |
| 51 | Arguments : |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 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. |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 57 | |
| 58 | Return : None |
| 59 | *******************************************************/ |
| 60 | |
| 61 | |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 62 | void gpio_tlmm_config_set(gpio_t gpio, unsigned func, |
| 63 | unsigned pull, unsigned drvstr, |
| 64 | unsigned enable) |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 65 | { |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 66 | unsigned val = 0; |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 67 | |
| 68 | if (gpio_not_valid(gpio)) |
| 69 | return; |
| 70 | |
| 71 | val |= (pull & GPIO_CFG_PULL_MASK) << GPIO_CFG_PULL_SHIFT; |
| 72 | val |= (func & GPIO_CFG_FUNC_MASK) << GPIO_CFG_FUNC_SHIFT; |
| 73 | val |= (drvstr & GPIO_CFG_DRV_MASK) << GPIO_CFG_DRV_SHIFT; |
| 74 | val |= (enable & GPIO_CFG_OE_MASK) << GPIO_CFG_OE_SHIFT; |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 75 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 76 | write32(GPIO_CONFIG_ADDR(gpio), val); |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 79 | /******************************************************* |
| 80 | Function description: Get GPIO configuration |
| 81 | Arguments : |
| 82 | gpio_t gpio - Gpio number |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 83 | unsigned *func - Functionality number |
| 84 | unsigned *pull - pull up/down, no pull range(0-3) |
| 85 | unsigned *drvstr - range (0 - 7)-> (2- 16)MA steps of 2 |
| 86 | unsigned *enable - 0 - Disable, 1- Enable. |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 87 | |
| 88 | Return : None |
| 89 | *******************************************************/ |
| 90 | |
| 91 | |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 92 | void gpio_tlmm_config_get(gpio_t gpio, unsigned *func, |
| 93 | unsigned *pull, unsigned *drvstr, |
| 94 | unsigned *enable) |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 95 | { |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 96 | unsigned val; |
| 97 | void *addr = GPIO_CONFIG_ADDR(gpio); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 98 | |
| 99 | if (gpio_not_valid(gpio)) |
| 100 | return; |
| 101 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 102 | val = read32(addr); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 103 | |
| 104 | *pull = (val >> GPIO_CFG_PULL_SHIFT) & GPIO_CFG_PULL_MASK; |
| 105 | *func = (val >> GPIO_CFG_FUNC_SHIFT) & GPIO_CFG_FUNC_MASK; |
| 106 | *drvstr = (val >> GPIO_CFG_DRV_SHIFT) & GPIO_CFG_DRV_MASK; |
| 107 | *enable = (val >> GPIO_CFG_OE_SHIFT) & GPIO_CFG_OE_MASK; |
| 108 | } |
| 109 | |
| 110 | /******************************************************* |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 111 | Function description: get GPIO IO functinality details |
| 112 | Arguments : |
| 113 | gpio_t gpio - Gpio number |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 114 | unsigned *in - Value of GPIO input |
| 115 | unsigned *out - Value of GPIO output |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 116 | |
| 117 | Return : None |
| 118 | *******************************************************/ |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 119 | int gpio_get(gpio_t gpio) |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 120 | { |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 121 | if (gpio_not_valid(gpio)) |
| 122 | return -1; |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 123 | |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 124 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 125 | return (read32(GPIO_IN_OUT_ADDR(gpio)) >> GPIO_IO_IN_SHIFT) & |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 126 | GPIO_IO_IN_MASK; |
| 127 | } |
| 128 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 129 | void gpio_set(gpio_t gpio, int value) |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 130 | { |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 131 | if (gpio_not_valid(gpio)) |
| 132 | return; |
| 133 | |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 134 | write32(GPIO_IN_OUT_ADDR(gpio), (value & 1) << GPIO_IO_OUT_SHIFT); |
Furquan Shaikh | 9d91aba | 2014-04-10 20:35:05 -0700 | [diff] [blame] | 135 | } |
Vadim Bendebury | d36ef6a | 2014-07-25 17:34:42 -0700 | [diff] [blame] | 136 | |
| 137 | void gpio_input_pulldown(gpio_t gpio) |
| 138 | { |
| 139 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 140 | GPIO_PULL_DOWN, GPIO_2MA, GPIO_DISABLE); |
| 141 | } |
| 142 | |
| 143 | void gpio_input_pullup(gpio_t gpio) |
| 144 | { |
| 145 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 146 | GPIO_PULL_UP, GPIO_2MA, GPIO_DISABLE); |
| 147 | } |
| 148 | |
| 149 | void gpio_input(gpio_t gpio) |
| 150 | { |
| 151 | gpio_tlmm_config_set(gpio, GPIO_FUNC_DISABLE, |
| 152 | GPIO_NO_PULL, GPIO_2MA, GPIO_DISABLE); |
| 153 | } |