2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial busions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL
27 #define T_TIMEOUT 2200000
28 #define T_RISEFALL 1000
32 nvkm_i2c_drive_scl(struct nvkm_i2c_bus *bus, int state)
34 bus->func->drive_scl(bus, state);
38 nvkm_i2c_drive_sda(struct nvkm_i2c_bus *bus, int state)
40 bus->func->drive_sda(bus, state);
44 nvkm_i2c_sense_scl(struct nvkm_i2c_bus *bus)
46 return bus->func->sense_scl(bus);
50 nvkm_i2c_sense_sda(struct nvkm_i2c_bus *bus)
52 return bus->func->sense_sda(bus);
56 nvkm_i2c_delay(struct nvkm_i2c_bus *bus, u32 nsec)
58 udelay((nsec + 500) / 1000);
62 nvkm_i2c_raise_scl(struct nvkm_i2c_bus *bus)
64 u32 timeout = T_TIMEOUT / T_RISEFALL;
66 nvkm_i2c_drive_scl(bus, 1);
68 nvkm_i2c_delay(bus, T_RISEFALL);
69 } while (!nvkm_i2c_sense_scl(bus) && --timeout);
75 i2c_start(struct nvkm_i2c_bus *bus)
79 if (!nvkm_i2c_sense_scl(bus) ||
80 !nvkm_i2c_sense_sda(bus)) {
81 nvkm_i2c_drive_scl(bus, 0);
82 nvkm_i2c_drive_sda(bus, 1);
83 if (!nvkm_i2c_raise_scl(bus))
87 nvkm_i2c_drive_sda(bus, 0);
88 nvkm_i2c_delay(bus, T_HOLD);
89 nvkm_i2c_drive_scl(bus, 0);
90 nvkm_i2c_delay(bus, T_HOLD);
95 i2c_stop(struct nvkm_i2c_bus *bus)
97 nvkm_i2c_drive_scl(bus, 0);
98 nvkm_i2c_drive_sda(bus, 0);
99 nvkm_i2c_delay(bus, T_RISEFALL);
101 nvkm_i2c_drive_scl(bus, 1);
102 nvkm_i2c_delay(bus, T_HOLD);
103 nvkm_i2c_drive_sda(bus, 1);
104 nvkm_i2c_delay(bus, T_HOLD);
108 i2c_bitw(struct nvkm_i2c_bus *bus, int sda)
110 nvkm_i2c_drive_sda(bus, sda);
111 nvkm_i2c_delay(bus, T_RISEFALL);
113 if (!nvkm_i2c_raise_scl(bus))
115 nvkm_i2c_delay(bus, T_HOLD);
117 nvkm_i2c_drive_scl(bus, 0);
118 nvkm_i2c_delay(bus, T_HOLD);
123 i2c_bitr(struct nvkm_i2c_bus *bus)
127 nvkm_i2c_drive_sda(bus, 1);
128 nvkm_i2c_delay(bus, T_RISEFALL);
130 if (!nvkm_i2c_raise_scl(bus))
132 nvkm_i2c_delay(bus, T_HOLD);
134 sda = nvkm_i2c_sense_sda(bus);
136 nvkm_i2c_drive_scl(bus, 0);
137 nvkm_i2c_delay(bus, T_HOLD);
142 nvkm_i2c_get_byte(struct nvkm_i2c_bus *bus, u8 *byte, bool last)
147 for (i = 7; i >= 0; i--) {
154 return i2c_bitw(bus, last ? 1 : 0);
158 nvkm_i2c_put_byte(struct nvkm_i2c_bus *bus, u8 byte)
161 for (i = 7; i >= 0; i--) {
162 ret = i2c_bitw(bus, !!(byte & (1 << i)));
168 if (ret == 1) /* nack */
174 i2c_addr(struct nvkm_i2c_bus *bus, struct i2c_msg *msg)
176 u32 addr = msg->addr << 1;
177 if (msg->flags & I2C_M_RD)
179 return nvkm_i2c_put_byte(bus, addr);
183 nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)
185 struct i2c_msg *msg = msgs;
186 int ret = 0, mcnt = num;
188 while (!ret && mcnt--) {
189 u8 remaining = msg->len;
192 ret = i2c_start(bus);
194 ret = i2c_addr(bus, msg);
196 if (msg->flags & I2C_M_RD) {
197 while (!ret && remaining--)
198 ret = nvkm_i2c_get_byte(bus, ptr++, !remaining);
200 while (!ret && remaining--)
201 ret = nvkm_i2c_put_byte(bus, *ptr++);
208 return (ret < 0) ? ret : num;
212 nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)