1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "bus.h"
25#include "pad.h"
26
27#include <core/option.h>
28
29
30
31
32static int
33nvkm_i2c_bus_pre_xfer(struct i2c_adapter *adap)
34{
35 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
36 return nvkm_i2c_bus_acquire(bus);
37}
38
39static void
40nvkm_i2c_bus_post_xfer(struct i2c_adapter *adap)
41{
42 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
43 return nvkm_i2c_bus_release(bus);
44}
45
46static void
47nvkm_i2c_bus_setscl(void *data, int state)
48{
49 struct nvkm_i2c_bus *bus = data;
50 bus->func->drive_scl(bus, state);
51}
52
53static void
54nvkm_i2c_bus_setsda(void *data, int state)
55{
56 struct nvkm_i2c_bus *bus = data;
57 bus->func->drive_sda(bus, state);
58}
59
60static int
61nvkm_i2c_bus_getscl(void *data)
62{
63 struct nvkm_i2c_bus *bus = data;
64 return bus->func->sense_scl(bus);
65}
66
67static int
68nvkm_i2c_bus_getsda(void *data)
69{
70 struct nvkm_i2c_bus *bus = data;
71 return bus->func->sense_sda(bus);
72}
73
74
75
76
77static int
78nvkm_i2c_bus_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
79{
80 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
81 int ret;
82
83 ret = nvkm_i2c_bus_acquire(bus);
84 if (ret)
85 return ret;
86
87 ret = bus->func->xfer(bus, msgs, num);
88 nvkm_i2c_bus_release(bus);
89 return ret;
90}
91
92static u32
93nvkm_i2c_bus_func(struct i2c_adapter *adap)
94{
95 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
96}
97
98static const struct i2c_algorithm
99nvkm_i2c_bus_algo = {
100 .master_xfer = nvkm_i2c_bus_xfer,
101 .functionality = nvkm_i2c_bus_func,
102};
103
104
105
106
107void
108nvkm_i2c_bus_init(struct nvkm_i2c_bus *bus)
109{
110 BUS_TRACE(bus, "init");
111 if (bus->func->init)
112 bus->func->init(bus);
113}
114
115void
116nvkm_i2c_bus_release(struct nvkm_i2c_bus *bus)
117{
118 struct nvkm_i2c_pad *pad = bus->pad;
119 BUS_TRACE(bus, "release");
120 nvkm_i2c_pad_release(pad);
121 mutex_unlock(&bus->mutex);
122}
123
124int
125nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *bus)
126{
127 struct nvkm_i2c_pad *pad = bus->pad;
128 int ret;
129 BUS_TRACE(bus, "acquire");
130 mutex_lock(&bus->mutex);
131 ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_I2C);
132 if (ret)
133 mutex_unlock(&bus->mutex);
134 return ret;
135}
136
137int
138nvkm_i2c_bus_probe(struct nvkm_i2c_bus *bus, const char *what,
139 struct nvkm_i2c_bus_probe *info,
140 bool (*match)(struct nvkm_i2c_bus *,
141 struct i2c_board_info *, void *), void *data)
142{
143 int i;
144
145 BUS_DBG(bus, "probing %ss", what);
146 for (i = 0; info[i].dev.addr; i++) {
147 u8 orig_udelay = 0;
148
149 if ((bus->i2c.algo == &i2c_bit_algo) && (info[i].udelay != 0)) {
150 struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
151 BUS_DBG(bus, "%dms delay instead of %dms",
152 info[i].udelay, algo->udelay);
153 orig_udelay = algo->udelay;
154 algo->udelay = info[i].udelay;
155 }
156
157 if (nvkm_probe_i2c(&bus->i2c, info[i].dev.addr) &&
158 (!match || match(bus, &info[i].dev, data))) {
159 BUS_DBG(bus, "detected %s: %s",
160 what, info[i].dev.type);
161 return i;
162 }
163
164 if (orig_udelay) {
165 struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
166 algo->udelay = orig_udelay;
167 }
168 }
169
170 BUS_DBG(bus, "no devices found.");
171 return -ENODEV;
172}
173
174void
175nvkm_i2c_bus_del(struct nvkm_i2c_bus **pbus)
176{
177 struct nvkm_i2c_bus *bus = *pbus;
178 if (bus && !WARN_ON(!bus->func)) {
179 BUS_TRACE(bus, "dtor");
180 list_del(&bus->head);
181 i2c_del_adapter(&bus->i2c);
182 kfree(bus->i2c.algo_data);
183 kfree(*pbus);
184 *pbus = NULL;
185 }
186}
187
188int
189nvkm_i2c_bus_ctor(const struct nvkm_i2c_bus_func *func,
190 struct nvkm_i2c_pad *pad, int id,
191 struct nvkm_i2c_bus *bus)
192{
193 struct nvkm_device *device = pad->i2c->subdev.device;
194 struct i2c_algo_bit_data *bit;
195#ifndef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
196 const bool internal = false;
197#else
198 const bool internal = true;
199#endif
200 int ret;
201
202 bus->func = func;
203 bus->pad = pad;
204 bus->id = id;
205 mutex_init(&bus->mutex);
206 list_add_tail(&bus->head, &pad->i2c->bus);
207 BUS_TRACE(bus, "ctor");
208
209 snprintf(bus->i2c.name, sizeof(bus->i2c.name), "nvkm-%s-bus-%04x",
210 dev_name(device->dev), id);
211 bus->i2c.owner = THIS_MODULE;
212 bus->i2c.dev.parent = device->dev;
213
214 if ( bus->func->drive_scl &&
215 !nvkm_boolopt(device->cfgopt, "NvI2C", internal)) {
216 if (!(bit = kzalloc(sizeof(*bit), GFP_KERNEL)))
217 return -ENOMEM;
218 bit->udelay = 10;
219 bit->timeout = usecs_to_jiffies(2200);
220 bit->data = bus;
221 bit->pre_xfer = nvkm_i2c_bus_pre_xfer;
222 bit->post_xfer = nvkm_i2c_bus_post_xfer;
223 bit->setscl = nvkm_i2c_bus_setscl;
224 bit->setsda = nvkm_i2c_bus_setsda;
225 bit->getscl = nvkm_i2c_bus_getscl;
226 bit->getsda = nvkm_i2c_bus_getsda;
227 bus->i2c.algo_data = bit;
228 ret = i2c_bit_add_bus(&bus->i2c);
229 } else {
230 bus->i2c.algo = &nvkm_i2c_bus_algo;
231 ret = i2c_add_adapter(&bus->i2c);
232 }
233
234 return ret;
235}
236
237int
238nvkm_i2c_bus_new_(const struct nvkm_i2c_bus_func *func,
239 struct nvkm_i2c_pad *pad, int id,
240 struct nvkm_i2c_bus **pbus)
241{
242 if (!(*pbus = kzalloc(sizeof(**pbus), GFP_KERNEL)))
243 return -ENOMEM;
244 return nvkm_i2c_bus_ctor(func, pad, id, *pbus);
245}
246