Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 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. |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <device/i2c.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 17 | #include <gpio.h> |
Julius Werner | f0d21ff3 | 2014-10-20 13:24:14 -0700 | [diff] [blame] | 18 | #include <soc/pinmux.h> |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 19 | |
| 20 | #include "i2c.h" |
| 21 | |
| 22 | static struct { |
| 23 | int pinmux_func; |
| 24 | gpio_t sda; |
| 25 | gpio_t scl; |
| 26 | } pins[] = { |
| 27 | [0]{.pinmux_func = PINMUX_GEN1_I2C_SCL_FUNC_I2C1, |
| 28 | .sda = GPIO(C5), .scl = GPIO(C4)}, |
| 29 | [1]{.pinmux_func = PINMUX_GEN2_I2C_SCL_FUNC_I2C2, |
| 30 | .sda = GPIO(T6), .scl = GPIO(T5)}, |
| 31 | [2]{.pinmux_func = PINMUX_CAM_I2C_SCL_FUNC_I2C3, |
| 32 | .sda = GPIO(BB2), .scl = GPIO(BB1)}, |
| 33 | [3]{.pinmux_func = PINMUX_DDC_SCL_FUNC_I2C4, |
| 34 | .sda = GPIO(V5), .scl = GPIO(V4)}, |
| 35 | [4]{.pinmux_func = PINMUX_PWR_I2C_SCL_FUNC_I2CPMU, |
| 36 | .sda = GPIO(Z7), .scl = GPIO(Z6)}, |
| 37 | }; |
| 38 | |
| 39 | static void tegra_set_sda(unsigned bus, int high) |
| 40 | { |
| 41 | if (high) |
| 42 | gpio_input_pullup(pins[bus].sda); |
| 43 | else |
| 44 | gpio_output(pins[bus].sda, 0); |
| 45 | } |
| 46 | |
| 47 | static void tegra_set_scl(unsigned bus, int high) |
| 48 | { |
| 49 | if (high) |
| 50 | gpio_input_pullup(pins[bus].scl); |
| 51 | else |
| 52 | gpio_output(pins[bus].scl, 0); |
| 53 | } |
| 54 | |
| 55 | static int tegra_get_sda(unsigned bus) |
| 56 | { |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 57 | return gpio_get(pins[bus].sda); |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int tegra_get_scl(unsigned bus) |
| 61 | { |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 62 | return gpio_get(pins[bus].scl); |
Julius Werner | 37d7ac8 | 2014-05-05 18:03:46 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static struct software_i2c_ops tegra_ops = { |
| 66 | .set_sda = tegra_set_sda, |
| 67 | .set_scl = tegra_set_scl, |
| 68 | .get_sda = tegra_get_sda, |
| 69 | .get_scl = tegra_get_scl, |
| 70 | }; |
| 71 | |
| 72 | void tegra_software_i2c_init(unsigned bus) |
| 73 | { |
| 74 | software_i2c[bus] = &tegra_ops; |
| 75 | |
| 76 | /* Initialize bus to idle state. */ |
| 77 | tegra_set_sda(bus, 1); |
| 78 | tegra_set_scl(bus, 1); |
| 79 | } |
| 80 | |
| 81 | void tegra_software_i2c_disable(unsigned bus) |
| 82 | { |
| 83 | software_i2c[bus] = NULL; |
| 84 | |
| 85 | /* Return pins to I2C controller. */ |
| 86 | pinmux_set_config(pins[bus].sda >> GPIO_PINMUX_SHIFT, |
| 87 | pins[bus].pinmux_func | PINMUX_INPUT_ENABLE); |
| 88 | pinmux_set_config(pins[bus].scl >> GPIO_PINMUX_SHIFT, |
| 89 | pins[bus].pinmux_func | PINMUX_INPUT_ENABLE); |
| 90 | gpio_set_mode(pins[bus].sda, GPIO_MODE_SPIO); |
| 91 | gpio_set_mode(pins[bus].scl, GPIO_MODE_SPIO); |
| 92 | } |