summaryrefslogtreecommitdiff
path: root/src/soc/rockchip/rk3288/software_i2c.c
blob: dbe058eea61cd9888431e4d5d4f3dec2d3b398ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2015 Google, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <arch/io.h>
#include <console/console.h>
#include <device/i2c_simple.h>
#include <gpio.h>
#include <soc/grf.h>
#include <soc/i2c.h>
#include <soc/pmu.h>

static struct {
	gpio_t scl;
	gpio_t sda;
} pins[] = {
	[0]{.scl = GPIO(0, C, 0), .sda = GPIO(0, B, 7)},
	[1]{.scl = GPIO(8, A, 5), .sda = GPIO(8, A, 4)},
	[2]{.scl = GPIO(6, B, 2), .sda = GPIO(6, B, 1)},
	[3]{.scl = GPIO(2, C, 0), .sda = GPIO(2, C, 1)},
	[4]{.scl = GPIO(7, C, 2), .sda = GPIO(7, C, 1)},
	[5]{.scl = GPIO(7, C, 4), .sda = GPIO(7, C, 3)},
};

static int get_scl(unsigned bus)
{
	return gpio_get(pins[bus].scl);
}

static int get_sda(unsigned bus)
{
	return gpio_get(pins[bus].sda);
}

static void set_scl(unsigned bus, int high)
{
	if (high)
		gpio_input_pullup(pins[bus].scl);
	else
		gpio_output(pins[bus].scl, 0);
}

static void set_sda(unsigned bus, int high)
{
	if (high)
		gpio_input_pullup(pins[bus].sda);
	else
		gpio_output(pins[bus].sda, 0);
}

static struct software_i2c_ops rk_ops = {
	.get_scl = get_scl,
	.get_sda = get_sda,
	.set_scl = set_scl,
	.set_sda = set_sda,
};

void software_i2c_attach(unsigned bus)
{
	software_i2c[bus] = &rk_ops;

	/* Mux pins to GPIO function for software I2C emulation. */
	switch (bus) {
	case 0:
		clrbits_le32(&rk3288_pmu->iomux_i2c0scl, IOMUX_I2C0SCL);
		clrbits_le32(&rk3288_pmu->iomux_i2c0sda, IOMUX_I2C0SDA);
		break;
	case 1:
		write32(&rk3288_grf->iomux_i2c1, IOMUX_GPIO(IOMUX_I2C1));
		break;
	case 2:
		write32(&rk3288_grf->iomux_i2c2, IOMUX_GPIO(IOMUX_I2C2));
		break;
	case 3:
		write32(&rk3288_grf->iomux_i2c3, IOMUX_GPIO(IOMUX_I2C3));
		break;
	case 4:
		write32(&rk3288_grf->iomux_i2c4, IOMUX_GPIO(IOMUX_I2C4));
		break;
	case 5:
		write32(&rk3288_grf->iomux_i2c5scl, IOMUX_GPIO(IOMUX_I2C5SCL));
		write32(&rk3288_grf->iomux_i2c5sda, IOMUX_GPIO(IOMUX_I2C5SDA));
		break;
	default:
		die("Unknown I2C bus number!");
	}

	/* Initialize bus to idle state. */
	set_scl(bus, 1);
	set_sda(bus, 1);
}

void software_i2c_detach(unsigned bus)
{
	software_i2c[bus] = NULL;

	/* Mux pins back to hardware I2C controller. */
	switch (bus) {
	case 0:
		setbits_le32(&rk3288_pmu->iomux_i2c0scl, IOMUX_I2C0SCL);
		setbits_le32(&rk3288_pmu->iomux_i2c0sda, IOMUX_I2C0SDA);
		break;
	case 1:
		write32(&rk3288_grf->iomux_i2c1, IOMUX_I2C1);
		break;
	case 2:
		write32(&rk3288_grf->iomux_i2c2, IOMUX_I2C2);
		break;
	case 3:
		write32(&rk3288_grf->iomux_i2c3, IOMUX_I2C3);
		break;
	case 4:
		write32(&rk3288_grf->iomux_i2c4, IOMUX_I2C4);
		break;
	case 5:
		write32(&rk3288_grf->iomux_i2c5scl, IOMUX_I2C5SCL);
		write32(&rk3288_grf->iomux_i2c5sda, IOMUX_I2C5SDA);
		break;
	default:
		die("Unknown I2C bus number!");
	}
}