![]() ![]() | ![]() |
1.1 cvs 1: /*
2: i2c.h
3:
4: Copyright (C) Marcus Metzler for convergence integrated media.
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; either version 2 of the License, or
9: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19: */
20:
21: #define __NO_VERSION__
22:
23: #include "i2c.h"
24:
25: void out(struct cvdv_cards *card)
26: {
27: write_indexed_register(card, IIO_GPIO_PINS,
28: (card->scl ? SCL : 0) |
29: (card->sda ? SDA : 0) | 1);
30: udelay(10);
31: }
32:
33: void clkon(struct cvdv_cards *card)
34: {
35: card->scl = 1;
36: }
37:
38: void clkoff(struct cvdv_cards *card)
39: {
40: card->scl = 0;
41: }
42:
43: void dat(struct cvdv_cards *card, u_char data)
44: {
45: card->sda = data;
46: }
47:
48: int rdat(struct cvdv_cards *card)
49: {
50: return ((read_indexed_register(card, IIO_GPIO_PINS) & SDA) ? 1 :
51: 0);
52: }
53:
54:
55: void I2CStart(struct cvdv_cards *card)
56: {
57: dat(card, 1);
58: out(card);
59: clkon(card);
60: out(card);
61: dat(card, 0);
62: out(card);
63: clkoff(card);
64: out(card);
65: }
66:
67: void I2CStop(struct cvdv_cards *card)
68: {
69: dat(card, 0);
70: out(card);
71: clkon(card);
72: out(card);
73: dat(card, 1);
74: out(card);
75: clkoff(card);
76: out(card);
77: }
78:
79: int I2CAck(struct cvdv_cards *card, int ack)
80: {
81: dat(card, ack);
82: out(card);
83: write_indexed_register(card, IIO_GPIO_CONTROL, (~SDA) & 0x07);
84: clkon(card);
85: out(card);
86: ack = rdat(card);
87: clkoff(card);
88: out(card);
89: write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
90: out(card);
91: return ack;
92: }
93:
94: u_char I2CReadByte(struct cvdv_cards * card, int ack)
95: {
96: int i;
97: u_char data = 0;
98:
99: clkoff(card);
100: dat(card, 1);
101: out(card);
102: write_indexed_register(card, IIO_GPIO_CONTROL, (~SDA) & 0x07);
103: for (i = 7; i >= 0; i--) {
104: clkon(card);
105: out(card);
106: data |= (rdat(card) << i);
107: clkoff(card);
108: out(card);
109: }
110: write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
111: I2CAck(card, ack);
112: return data;
113: }
114:
115:
116: int I2CSendByte(struct cvdv_cards *card, u_char data)
117: {
118: int i;
119:
120: for (i = 7; i >= 0; i--) {
121: dat(card, data & (1 << i));
122: out(card);
123: clkon(card);
124: out(card);
125: clkoff(card);
126: out(card);
127: }
128: i = I2CAck(card, 1);
129: return i;
130: }
131:
132: void I2CWrite(struct cvdv_cards *card, int adr, int reg, int val)
133: {
134: I2CStart(card);
135: I2CSendByte(card, adr);
136: I2CSendByte(card, reg);
137: I2CSendByte(card, val);
138: I2CStop(card);
139: }
140:
141:
142: u_char I2CRead(struct cvdv_cards *card, int adr, int reg)
143: {
144: u_char c;
145:
146: I2CStart(card);
147: I2CSendByte(card, adr);
148: I2CSendByte(card, reg);
149: I2CStart(card);
150: I2CSendByte(card, adr | 1);
151: c = I2CReadByte(card, 1);
152: I2CStop(card);
153: return c;
154: }
155:
156:
157: int I2CScan(struct cvdv_cards *card, int adr)
158: {
159: int result;
160: I2CStart(card);
161: result = I2CSendByte(card, adr);
162: I2CStop(card);
163: return result;
164: }
165:
166: void I2CScanBus(struct cvdv_cards *card)
167: {
168: int i;
169:
170: for (i = 0; i < 0xff; i += 2) {
171: if (!I2CScan(card, i))
1.2 ! mocm 172: MDEBUG(0,"Found i2c device at %d\n", i);
1.1 cvs 173: }
174: }
175:
176: void I2CSend(struct cvdv_cards *card, int adr, u_char * vals)
177: {
178: int reg, val;
179: while (*vals != 0xff) {
180: reg = *vals;
181: vals++;
182: val = *vals;
183: vals++;
184: I2CWrite(card, adr, reg, val);
185: }
186: }