blob: fe2f61edbfbc44c19b251982361b069d72dff5a3 [file] [log] [blame]
Julius Werner37d7ac82014-05-05 18:03:46 -07001/*
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 Werner37d7ac82014-05-05 18:03:46 -070014 */
15
16#include <device/i2c.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070017#include <gpio.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070018#include <soc/pinmux.h>
Julius Werner37d7ac82014-05-05 18:03:46 -070019
20#include "i2c.h"
21
22static 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
39static 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
47static 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
55static int tegra_get_sda(unsigned bus)
56{
Julius Wernereaa9c452014-09-24 15:40:49 -070057 return gpio_get(pins[bus].sda);
Julius Werner37d7ac82014-05-05 18:03:46 -070058}
59
60static int tegra_get_scl(unsigned bus)
61{
Julius Wernereaa9c452014-09-24 15:40:49 -070062 return gpio_get(pins[bus].scl);
Julius Werner37d7ac82014-05-05 18:03:46 -070063}
64
65static 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
72void 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
81void 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}