blob: 5ee5580ae83fe41a9304d3530ef723f874dda8ef [file] [log] [blame]
Vadim Bendebury9c9c3362014-07-23 09:40:02 -07001/*
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 Wernereaa9c452014-09-24 15:40:49 -070021#include <gpio.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070022
Julius Wernereaa9c452014-09-24 15:40:49 -070023int gpio_get_tristates(gpio_t gpio[], int num_gpio, int tertiary)
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070024{
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 Wernereaa9c452014-09-24 15:40:49 -070048 value[index] = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070049
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 Nojiri573e2112014-08-08 15:38:52 -070065 for (index = num_gpio - 1; index >= 0; --index) {
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070066 if (tertiary)
67 id *= 3;
68 else
69 id <<= 2;
Julius Wernereaa9c452014-09-24 15:40:49 -070070 temp = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070071 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}