2 * Copyright 2013 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.
29 #include <core/option.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/dcb.h>
32 #include <subdev/bios/i2c.h>
34 static struct nvkm_i2c_pad *
35 nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
37 struct nvkm_i2c_pad *pad;
39 list_for_each_entry(pad, &i2c->pad, head) {
48 nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
50 struct nvkm_bios *bios = i2c->subdev.device->bios;
51 struct nvkm_i2c_bus *bus;
53 if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
54 u8 ver, hdr, cnt, len;
55 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
56 if (i2c && ver >= 0x30) {
57 u8 auxidx = nvbios_rd08(bios, i2c + 4);
58 if (id == NVKM_I2C_BUS_PRI)
59 id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
61 id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
63 id = NVKM_I2C_BUS_CCB(2);
67 list_for_each_entry(bus, &i2c->bus, head) {
76 nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
78 struct nvkm_i2c_aux *aux;
80 list_for_each_entry(aux, &i2c->aux, head) {
89 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
91 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
92 struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
94 i2c->func->aux_mask(i2c, type, aux->intr, 0);
98 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
100 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
101 struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
103 i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
106 static const struct nvkm_event_func
107 nvkm_i2c_intr_func = {
108 .init = nvkm_i2c_intr_init,
109 .fini = nvkm_i2c_intr_fini,
113 nvkm_i2c_intr(struct nvkm_subdev *subdev)
115 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
116 struct nvkm_i2c_aux *aux;
119 if (!i2c->func->aux_stat)
122 i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
123 if (!hi && !lo && !rq && !tx)
126 list_for_each_entry(aux, &i2c->aux, head) {
128 if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
129 if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
130 if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
131 if (tx & aux->intr) mask |= NVKM_I2C_DONE;
133 nvkm_event_ntfy(&i2c->event, aux->id, mask);
138 nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
140 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
141 struct nvkm_i2c_pad *pad;
142 struct nvkm_i2c_bus *bus;
143 struct nvkm_i2c_aux *aux;
146 list_for_each_entry(aux, &i2c->aux, head) {
147 nvkm_i2c_aux_fini(aux);
150 list_for_each_entry(bus, &i2c->bus, head) {
151 nvkm_i2c_bus_fini(bus);
154 if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
155 i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
156 i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
159 list_for_each_entry(pad, &i2c->pad, head) {
160 nvkm_i2c_pad_fini(pad);
167 nvkm_i2c_preinit(struct nvkm_subdev *subdev)
169 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
170 struct nvkm_i2c_bus *bus;
171 struct nvkm_i2c_pad *pad;
174 * We init our i2c busses as early as possible, since they may be
175 * needed by the vbios init scripts on some cards
177 list_for_each_entry(pad, &i2c->pad, head)
178 nvkm_i2c_pad_init(pad);
179 list_for_each_entry(bus, &i2c->bus, head)
180 nvkm_i2c_bus_init(bus);
186 nvkm_i2c_init(struct nvkm_subdev *subdev)
188 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
189 struct nvkm_i2c_bus *bus;
190 struct nvkm_i2c_pad *pad;
191 struct nvkm_i2c_aux *aux;
193 list_for_each_entry(pad, &i2c->pad, head) {
194 nvkm_i2c_pad_init(pad);
197 list_for_each_entry(bus, &i2c->bus, head) {
198 nvkm_i2c_bus_init(bus);
201 list_for_each_entry(aux, &i2c->aux, head) {
202 nvkm_i2c_aux_init(aux);
209 nvkm_i2c_dtor(struct nvkm_subdev *subdev)
211 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
213 nvkm_event_fini(&i2c->event);
215 while (!list_empty(&i2c->aux)) {
216 struct nvkm_i2c_aux *aux =
217 list_first_entry(&i2c->aux, typeof(*aux), head);
218 nvkm_i2c_aux_del(&aux);
221 while (!list_empty(&i2c->bus)) {
222 struct nvkm_i2c_bus *bus =
223 list_first_entry(&i2c->bus, typeof(*bus), head);
224 nvkm_i2c_bus_del(&bus);
227 while (!list_empty(&i2c->pad)) {
228 struct nvkm_i2c_pad *pad =
229 list_first_entry(&i2c->pad, typeof(*pad), head);
230 nvkm_i2c_pad_del(&pad);
236 static const struct nvkm_subdev_func
238 .dtor = nvkm_i2c_dtor,
239 .preinit = nvkm_i2c_preinit,
240 .init = nvkm_i2c_init,
241 .fini = nvkm_i2c_fini,
242 .intr = nvkm_i2c_intr,
245 static const struct nvkm_i2c_drv {
248 int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
249 struct nvkm_i2c_pad **);
252 { 0x0d, 0x39, anx9805_pad_new },
253 { 0x0e, 0x3b, anx9805_pad_new },
258 nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
259 enum nvkm_subdev_type type, int inst, struct nvkm_i2c **pi2c)
261 struct nvkm_bios *bios = device->bios;
262 struct nvkm_i2c *i2c;
263 struct dcb_i2c_entry ccbE;
264 struct dcb_output dcbE;
268 if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
271 nvkm_subdev_ctor(&nvkm_i2c, device, type, inst, &i2c->subdev);
273 INIT_LIST_HEAD(&i2c->pad);
274 INIT_LIST_HEAD(&i2c->bus);
275 INIT_LIST_HEAD(&i2c->aux);
278 while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
279 struct nvkm_i2c_pad *pad = NULL;
280 struct nvkm_i2c_bus *bus = NULL;
281 struct nvkm_i2c_aux *aux = NULL;
283 nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
284 "sense %02x share %02x auxch %02x\n", i, ccbE.type,
285 ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
287 if (ccbE.share != DCB_I2C_UNUSED) {
288 const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
289 if (!(pad = nvkm_i2c_pad_find(i2c, id)))
290 ret = func->pad_s_new(i2c, id, &pad);
294 ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
298 nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
299 nvkm_i2c_pad_del(&pad);
303 if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
304 ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
308 if (pad->func->bus_new_4 &&
309 ( ccbE.type == DCB_I2C_NV4E_BIT ||
310 ccbE.type == DCB_I2C_NVIO_BIT ||
311 (ccbE.type == DCB_I2C_PMGR &&
312 ccbE.drive != DCB_I2C_UNUSED))) {
313 ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
318 nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
319 nvkm_i2c_bus_del(&bus);
322 if (pad->func->aux_new_6 &&
323 ( ccbE.type == DCB_I2C_NVIO_AUX ||
324 (ccbE.type == DCB_I2C_PMGR &&
325 ccbE.auxch != DCB_I2C_UNUSED))) {
326 ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
333 nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
334 nvkm_i2c_aux_del(&aux);
337 if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
338 nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
344 while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
345 const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
346 struct nvkm_i2c_bus *bus;
347 struct nvkm_i2c_pad *pad;
349 /* internal outputs handled by native i2c busses (above) */
353 /* we need an i2c bus to talk to the external encoder */
354 bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
356 nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
360 /* ... and a driver for it */
361 while (drv->pad_new) {
362 if (drv->bios == dcbE.extdev)
368 nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
373 /* find/create an instance of the driver */
374 pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
376 const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
377 ret = drv->pad_new(bus, id, drv->addr, &pad);
379 nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
381 nvkm_i2c_pad_del(&pad);
386 /* create any i2c bus / aux channel required by the output */
387 if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
388 const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
389 struct nvkm_i2c_aux *aux = NULL;
390 ret = pad->func->aux_new_6(pad, id, 0, &aux);
392 nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
394 nvkm_i2c_aux_del(&aux);
397 if (pad->func->bus_new_4) {
398 const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
399 struct nvkm_i2c_bus *bus = NULL;
400 ret = pad->func->bus_new_4(pad, id, 0, &bus);
402 nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
404 nvkm_i2c_bus_del(&bus);
409 return nvkm_event_init(&nvkm_i2c_intr_func, &i2c->subdev, 4, i, &i2c->event);