2 * Copyright 2015 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 portions 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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
27 #include <core/option.h>
29 /*******************************************************************************
31 ******************************************************************************/
33 nvkm_i2c_bus_pre_xfer(struct i2c_adapter *adap)
35 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
36 return nvkm_i2c_bus_acquire(bus);
40 nvkm_i2c_bus_post_xfer(struct i2c_adapter *adap)
42 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
43 return nvkm_i2c_bus_release(bus);
47 nvkm_i2c_bus_setscl(void *data, int state)
49 struct nvkm_i2c_bus *bus = data;
50 bus->func->drive_scl(bus, state);
54 nvkm_i2c_bus_setsda(void *data, int state)
56 struct nvkm_i2c_bus *bus = data;
57 bus->func->drive_sda(bus, state);
61 nvkm_i2c_bus_getscl(void *data)
63 struct nvkm_i2c_bus *bus = data;
64 return bus->func->sense_scl(bus);
68 nvkm_i2c_bus_getsda(void *data)
70 struct nvkm_i2c_bus *bus = data;
71 return bus->func->sense_sda(bus);
74 /*******************************************************************************
75 * !i2c-algo-bit (off-chip i2c bus / hw i2c / internal bit-banging algo)
76 ******************************************************************************/
78 nvkm_i2c_bus_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
80 struct nvkm_i2c_bus *bus = container_of(adap, typeof(*bus), i2c);
83 ret = nvkm_i2c_bus_acquire(bus);
87 ret = bus->func->xfer(bus, msgs, num);
88 nvkm_i2c_bus_release(bus);
93 nvkm_i2c_bus_func(struct i2c_adapter *adap)
95 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
98 static const struct i2c_algorithm
100 .master_xfer = nvkm_i2c_bus_xfer,
101 .functionality = nvkm_i2c_bus_func,
104 /*******************************************************************************
106 ******************************************************************************/
108 nvkm_i2c_bus_init(struct nvkm_i2c_bus *bus)
110 BUS_TRACE(bus, "init");
112 bus->func->init(bus);
116 nvkm_i2c_bus_release(struct nvkm_i2c_bus *bus)
118 struct nvkm_i2c_pad *pad = bus->pad;
119 BUS_TRACE(bus, "release");
120 nvkm_i2c_pad_release(pad);
121 mutex_unlock(&bus->mutex);
125 nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *bus)
127 struct nvkm_i2c_pad *pad = bus->pad;
129 BUS_TRACE(bus, "acquire");
130 mutex_lock(&bus->mutex);
131 ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_I2C);
133 mutex_unlock(&bus->mutex);
138 nvkm_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)
145 BUS_DBG(bus, "probing %ss", what);
146 for (i = 0; info[i].dev.addr; i++) {
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;
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);
165 struct i2c_algo_bit_data *algo = bus->i2c.algo_data;
166 algo->udelay = orig_udelay;
170 BUS_DBG(bus, "no devices found.");
175 nvkm_i2c_bus_del(struct nvkm_i2c_bus **pbus)
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);
189 nvkm_i2c_bus_ctor(const struct nvkm_i2c_bus_func *func,
190 struct nvkm_i2c_pad *pad, int id,
191 struct nvkm_i2c_bus *bus)
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;
198 const bool internal = true;
205 mutex_init(&bus->mutex);
206 list_add_tail(&bus->head, &pad->i2c->bus);
207 BUS_TRACE(bus, "ctor");
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;
214 if ( bus->func->drive_scl &&
215 !nvkm_boolopt(device->cfgopt, "NvI2C", internal)) {
216 if (!(bit = kzalloc(sizeof(*bit), GFP_KERNEL)))
219 bit->timeout = usecs_to_jiffies(2200);
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);
230 bus->i2c.algo = &nvkm_i2c_bus_algo;
231 ret = i2c_add_adapter(&bus->i2c);
238 nvkm_i2c_bus_new_(const struct nvkm_i2c_bus_func *func,
239 struct nvkm_i2c_pad *pad, int id,
240 struct nvkm_i2c_bus **pbus)
242 if (!(*pbus = kzalloc(sizeof(**pbus), GFP_KERNEL)))
244 return nvkm_i2c_bus_ctor(func, pad, id, *pbus);