Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 Samsung Electronics |
| 3 | * Minkyu Kang <mk7.kang@samsung.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | #include <asm/io.h> |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 23 | #include <asm/gpio.h> |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 24 | |
| 25 | #define CON_MASK(x) (0xf << ((x) << 2)) |
| 26 | #define CON_SFR(x, v) ((v) << ((x) << 2)) |
| 27 | |
| 28 | #define DAT_MASK(x) (0x1 << (x)) |
| 29 | #define DAT_SET(x) (0x1 << (x)) |
| 30 | |
| 31 | #define PULL_MASK(x) (0x3 << ((x) << 1)) |
| 32 | #define PULL_MODE(x, v) ((v) << ((x) << 1)) |
| 33 | |
| 34 | #define DRV_MASK(x) (0x3 << ((x) << 1)) |
| 35 | #define DRV_SET(x, m) ((m) << ((x) << 1)) |
| 36 | #define RATE_MASK(x) (0x1 << (x + 16)) |
| 37 | #define RATE_SET(x) (0x1 << (x + 16)) |
| 38 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 39 | void s5p_gpio_cfg_pin(struct s5p_gpio_bank *bank, int gpio, int cfg) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 40 | { |
| 41 | unsigned int value; |
| 42 | |
| 43 | value = readl(&bank->con); |
| 44 | value &= ~CON_MASK(gpio); |
| 45 | value |= CON_SFR(gpio, cfg); |
| 46 | writel(value, &bank->con); |
| 47 | } |
| 48 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 49 | void s5p_gpio_direction_output(struct s5p_gpio_bank *bank, int gpio, int en) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 50 | { |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 51 | s5p_gpio_cfg_pin(bank, gpio, GPIO_OUTPUT); |
Axel Lin | 7b885cf | 2013-06-15 22:19:45 +0800 | [diff] [blame^] | 52 | s5p_gpio_set_value(bank, gpio, en); |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 55 | void s5p_gpio_direction_input(struct s5p_gpio_bank *bank, int gpio) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 56 | { |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 57 | s5p_gpio_cfg_pin(bank, gpio, GPIO_INPUT); |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 60 | void s5p_gpio_set_value(struct s5p_gpio_bank *bank, int gpio, int en) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 61 | { |
| 62 | unsigned int value; |
| 63 | |
| 64 | value = readl(&bank->dat); |
| 65 | value &= ~DAT_MASK(gpio); |
| 66 | if (en) |
| 67 | value |= DAT_SET(gpio); |
| 68 | writel(value, &bank->dat); |
| 69 | } |
| 70 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 71 | unsigned int s5p_gpio_get_value(struct s5p_gpio_bank *bank, int gpio) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 72 | { |
| 73 | unsigned int value; |
| 74 | |
| 75 | value = readl(&bank->dat); |
| 76 | return !!(value & DAT_MASK(gpio)); |
| 77 | } |
| 78 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 79 | void s5p_gpio_set_pull(struct s5p_gpio_bank *bank, int gpio, int mode) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 80 | { |
| 81 | unsigned int value; |
| 82 | |
| 83 | value = readl(&bank->pull); |
| 84 | value &= ~PULL_MASK(gpio); |
| 85 | |
| 86 | switch (mode) { |
| 87 | case GPIO_PULL_DOWN: |
| 88 | case GPIO_PULL_UP: |
| 89 | value |= PULL_MODE(gpio, mode); |
| 90 | break; |
| 91 | default: |
Minkyu Kang | ced528f | 2010-05-28 12:34:29 +0900 | [diff] [blame] | 92 | break; |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | writel(value, &bank->pull); |
| 96 | } |
| 97 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 98 | void s5p_gpio_set_drv(struct s5p_gpio_bank *bank, int gpio, int mode) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 99 | { |
| 100 | unsigned int value; |
| 101 | |
| 102 | value = readl(&bank->drv); |
| 103 | value &= ~DRV_MASK(gpio); |
| 104 | |
| 105 | switch (mode) { |
| 106 | case GPIO_DRV_1X: |
| 107 | case GPIO_DRV_2X: |
| 108 | case GPIO_DRV_3X: |
| 109 | case GPIO_DRV_4X: |
| 110 | value |= DRV_SET(gpio, mode); |
| 111 | break; |
| 112 | default: |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | writel(value, &bank->drv); |
| 117 | } |
| 118 | |
Łukasz Majewski | 4d954cc | 2011-07-15 00:16:22 +0000 | [diff] [blame] | 119 | void s5p_gpio_set_rate(struct s5p_gpio_bank *bank, int gpio, int mode) |
Minkyu Kang | 1edf0f2 | 2010-02-12 18:17:52 +0900 | [diff] [blame] | 120 | { |
| 121 | unsigned int value; |
| 122 | |
| 123 | value = readl(&bank->drv); |
| 124 | value &= ~RATE_MASK(gpio); |
| 125 | |
| 126 | switch (mode) { |
| 127 | case GPIO_DRV_FAST: |
| 128 | case GPIO_DRV_SLOW: |
| 129 | value |= RATE_SET(gpio); |
| 130 | break; |
| 131 | default: |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | writel(value, &bank->drv); |
| 136 | } |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 137 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 138 | struct s5p_gpio_bank *s5p_gpio_get_bank(unsigned gpio) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 139 | { |
Łukasz Majewski | d6614b7 | 2012-09-04 21:47:46 +0000 | [diff] [blame] | 140 | int bank; |
| 141 | unsigned g = gpio - s5p_gpio_part_max(gpio); |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 142 | |
Łukasz Majewski | d6614b7 | 2012-09-04 21:47:46 +0000 | [diff] [blame] | 143 | bank = g / GPIO_PER_BANK; |
| 144 | bank *= sizeof(struct s5p_gpio_bank); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 145 | return (struct s5p_gpio_bank *) (s5p_gpio_base(gpio) + bank); |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 148 | int s5p_gpio_get_pin(unsigned gpio) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 149 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 150 | return gpio % GPIO_PER_BANK; |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 153 | /* Common GPIO API */ |
| 154 | |
| 155 | int gpio_request(unsigned gpio, const char *label) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 160 | int gpio_free(unsigned gpio) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 161 | { |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 165 | int gpio_direction_input(unsigned gpio) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 166 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 167 | s5p_gpio_direction_input(s5p_gpio_get_bank(gpio), |
| 168 | s5p_gpio_get_pin(gpio)); |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 172 | int gpio_direction_output(unsigned gpio, int value) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 173 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 174 | s5p_gpio_direction_output(s5p_gpio_get_bank(gpio), |
| 175 | s5p_gpio_get_pin(gpio), value); |
| 176 | return 0; |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 179 | int gpio_get_value(unsigned gpio) |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 180 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 181 | return (int) s5p_gpio_get_value(s5p_gpio_get_bank(gpio), |
| 182 | s5p_gpio_get_pin(gpio)); |
| 183 | } |
| 184 | |
| 185 | int gpio_set_value(unsigned gpio, int value) |
| 186 | { |
| 187 | s5p_gpio_set_value(s5p_gpio_get_bank(gpio), |
| 188 | s5p_gpio_get_pin(gpio), value); |
| 189 | |
| 190 | return 0; |
Łukasz Majewski | 1e04cae | 2011-08-22 22:34:58 +0000 | [diff] [blame] | 191 | } |