2 * Copyright 2009 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.
28 nvkm_i2c_aux_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
30 struct nvkm_i2c_aux *aux = container_of(adap, typeof(*aux), i2c);
31 struct i2c_msg *msg = msgs;
34 ret = nvkm_i2c_aux_acquire(aux);
39 u8 remaining = msg->len;
43 u8 cnt = (remaining > 16) ? 16 : remaining;
46 if (msg->flags & I2C_M_RD)
51 if (mcnt || remaining > 16)
54 ret = aux->func->xfer(aux, true, cmd, msg->addr, ptr, cnt);
56 nvkm_i2c_aux_release(aux);
67 nvkm_i2c_aux_release(aux);
72 nvkm_i2c_aux_i2c_func(struct i2c_adapter *adap)
74 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
77 const struct i2c_algorithm
78 nvkm_i2c_aux_i2c_algo = {
79 .master_xfer = nvkm_i2c_aux_i2c_xfer,
80 .functionality = nvkm_i2c_aux_i2c_func
84 nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *aux, bool monitor)
86 struct nvkm_i2c_pad *pad = aux->pad;
87 AUX_TRACE(aux, "monitor: %s", monitor ? "yes" : "no");
89 nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_AUX);
91 nvkm_i2c_pad_mode(pad, NVKM_I2C_PAD_OFF);
95 nvkm_i2c_aux_release(struct nvkm_i2c_aux *aux)
97 struct nvkm_i2c_pad *pad = aux->pad;
98 AUX_TRACE(aux, "release");
99 nvkm_i2c_pad_release(pad);
100 mutex_unlock(&aux->mutex);
104 nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *aux)
106 struct nvkm_i2c_pad *pad = aux->pad;
109 AUX_TRACE(aux, "acquire");
110 mutex_lock(&aux->mutex);
113 ret = nvkm_i2c_pad_acquire(pad, NVKM_I2C_PAD_AUX);
118 mutex_unlock(&aux->mutex);
123 nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *aux, bool retry, u8 type,
124 u32 addr, u8 *data, u8 size)
126 return aux->func->xfer(aux, retry, type, addr, data, size);
130 nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *aux, int nr, int bw, bool ef)
132 if (aux->func->lnk_ctl)
133 return aux->func->lnk_ctl(aux, nr, bw, ef);
138 nvkm_i2c_aux_del(struct nvkm_i2c_aux **paux)
140 struct nvkm_i2c_aux *aux = *paux;
141 if (aux && !WARN_ON(!aux->func)) {
142 AUX_TRACE(aux, "dtor");
143 list_del(&aux->head);
144 i2c_del_adapter(&aux->i2c);
151 nvkm_i2c_aux_init(struct nvkm_i2c_aux *aux)
153 AUX_TRACE(aux, "init");
154 mutex_lock(&aux->mutex);
156 mutex_unlock(&aux->mutex);
160 nvkm_i2c_aux_fini(struct nvkm_i2c_aux *aux)
162 AUX_TRACE(aux, "fini");
163 mutex_lock(&aux->mutex);
164 aux->enabled = false;
165 mutex_unlock(&aux->mutex);
169 nvkm_i2c_aux_ctor(const struct nvkm_i2c_aux_func *func,
170 struct nvkm_i2c_pad *pad, int id,
171 struct nvkm_i2c_aux *aux)
173 struct nvkm_device *device = pad->i2c->subdev.device;
178 mutex_init(&aux->mutex);
179 list_add_tail(&aux->head, &pad->i2c->aux);
180 AUX_TRACE(aux, "ctor");
182 snprintf(aux->i2c.name, sizeof(aux->i2c.name), "nvkm-%s-aux-%04x",
183 dev_name(device->dev), id);
184 aux->i2c.owner = THIS_MODULE;
185 aux->i2c.dev.parent = device->dev;
186 aux->i2c.algo = &nvkm_i2c_aux_i2c_algo;
187 return i2c_add_adapter(&aux->i2c);
191 nvkm_i2c_aux_new_(const struct nvkm_i2c_aux_func *func,
192 struct nvkm_i2c_pad *pad, int id,
193 struct nvkm_i2c_aux **paux)
195 if (!(*paux = kzalloc(sizeof(**paux), GFP_KERNEL)))
197 return nvkm_i2c_aux_ctor(func, pad, id, *paux);