Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <delay.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 21 | #include <gpio.h> |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 22 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 23 | int gpio_get_tristates(gpio_t gpio[], int num_gpio, int tertiary) |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 24 | { |
| 25 | /* |
| 26 | * GPIOs which are tied to stronger external pull up or pull down |
| 27 | * will stay there regardless of the internal pull up or pull |
| 28 | * down setting. |
| 29 | * |
| 30 | * GPIOs which are floating will go to whatever level they're |
| 31 | * internally pulled to. |
| 32 | */ |
| 33 | |
| 34 | int temp; |
| 35 | int index; |
| 36 | int id = 0; |
| 37 | char value[num_gpio]; |
| 38 | |
| 39 | /* Enable internal pull up */ |
| 40 | for (index = 0; index < num_gpio; ++index) |
| 41 | gpio_input_pullup(gpio[index]); |
| 42 | |
| 43 | /* Wait until signals become stable */ |
| 44 | udelay(10); |
| 45 | |
| 46 | /* Get gpio values at internal pull up */ |
| 47 | for (index = 0; index < num_gpio; ++index) |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 48 | value[index] = gpio_get(gpio[index]); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 49 | |
| 50 | /* Enable internal pull down */ |
| 51 | for (index = 0; index < num_gpio; ++index) |
| 52 | gpio_input_pulldown(gpio[index]); |
| 53 | |
| 54 | /* Wait until signals become stable */ |
| 55 | udelay(10); |
| 56 | |
| 57 | /* |
| 58 | * Get gpio values at internal pull down. |
| 59 | * Compare with gpio pull up value and then |
| 60 | * determine a gpio final value/state: |
| 61 | * 0: pull down |
| 62 | * 1: pull up |
| 63 | * 2: floating |
| 64 | */ |
Daisuke Nojiri | 573e211 | 2014-08-08 15:38:52 -0700 | [diff] [blame] | 65 | for (index = num_gpio - 1; index >= 0; --index) { |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 66 | if (tertiary) |
| 67 | id *= 3; |
| 68 | else |
| 69 | id <<= 2; |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 70 | temp = gpio_get(gpio[index]); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 71 | id += ((value[index] ^ temp) << 1) | temp; |
| 72 | } |
| 73 | |
| 74 | /* Disable pull up / pull down to conserve power */ |
| 75 | for (index = 0; index < num_gpio; ++index) |
| 76 | gpio_input(gpio[index]); |
| 77 | |
| 78 | return id; |
| 79 | } |