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.
27 #include <core/notify.h>
28 #include <core/option.h>
29 #include <subdev/bios.h>
30 #include <subdev/bios/dcb.h>
32 /******************************************************************************
33 * interface to linux i2c bit-banging algorithm
34 *****************************************************************************/
36 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
43 nvkm_i2c_pre_xfer(struct i2c_adapter *adap)
45 struct i2c_algo_bit_data *bit = adap->algo_data;
46 struct nvkm_i2c_port *port = bit->data;
47 return nvkm_i2c(port)->acquire(port, bit->timeout);
51 nvkm_i2c_post_xfer(struct i2c_adapter *adap)
53 struct i2c_algo_bit_data *bit = adap->algo_data;
54 struct nvkm_i2c_port *port = bit->data;
55 return nvkm_i2c(port)->release(port);
59 nvkm_i2c_setscl(void *data, int state)
61 struct nvkm_i2c_port *port = data;
62 port->func->drive_scl(port, state);
66 nvkm_i2c_setsda(void *data, int state)
68 struct nvkm_i2c_port *port = data;
69 port->func->drive_sda(port, state);
73 nvkm_i2c_getscl(void *data)
75 struct nvkm_i2c_port *port = data;
76 return port->func->sense_scl(port);
80 nvkm_i2c_getsda(void *data)
82 struct nvkm_i2c_port *port = data;
83 return port->func->sense_sda(port);
86 /******************************************************************************
87 * base i2c "port" class implementation
88 *****************************************************************************/
91 _nvkm_i2c_port_fini(struct nvkm_object *object, bool suspend)
93 struct nvkm_i2c_port *port = (void *)object;
94 struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
95 nv_ofuncs(pad)->fini(nv_object(pad), suspend);
96 return nvkm_object_fini(&port->base, suspend);
100 _nvkm_i2c_port_dtor(struct nvkm_object *object)
102 struct nvkm_i2c_port *port = (void *)object;
103 i2c_del_adapter(&port->adapter);
104 nvkm_object_destroy(&port->base);
108 nvkm_i2c_port_create_(struct nvkm_object *parent, struct nvkm_object *engine,
109 struct nvkm_oclass *oclass, u8 index,
110 const struct i2c_algorithm *algo,
111 const struct nvkm_i2c_func *func,
112 int size, void **pobject)
114 struct nvkm_device *device = nv_device(parent);
115 struct nvkm_i2c *i2c = nvkm_i2c(parent);
116 struct nvkm_i2c_port *port;
119 ret = nvkm_object_create_(parent, engine, oclass, 0, size, pobject);
124 snprintf(port->adapter.name, sizeof(port->adapter.name),
125 "nvkm-%s-%d", device->name, index);
126 port->adapter.owner = THIS_MODULE;
127 port->adapter.dev.parent = nv_device_base(device);
131 mutex_init(&port->mutex);
133 if ( algo == &nvkm_i2c_bit_algo &&
134 !nvkm_boolopt(device->cfgopt, "NvI2C", CSTMSEL)) {
135 struct i2c_algo_bit_data *bit;
137 bit = kzalloc(sizeof(*bit), GFP_KERNEL);
142 bit->timeout = usecs_to_jiffies(2200);
144 bit->pre_xfer = nvkm_i2c_pre_xfer;
145 bit->post_xfer = nvkm_i2c_post_xfer;
146 bit->setsda = nvkm_i2c_setsda;
147 bit->setscl = nvkm_i2c_setscl;
148 bit->getsda = nvkm_i2c_getsda;
149 bit->getscl = nvkm_i2c_getscl;
151 port->adapter.algo_data = bit;
152 ret = i2c_bit_add_bus(&port->adapter);
154 port->adapter.algo_data = port;
155 port->adapter.algo = algo;
156 ret = i2c_add_adapter(&port->adapter);
160 list_add_tail(&port->head, &i2c->ports);
164 /******************************************************************************
165 * base i2c subdev class implementation
166 *****************************************************************************/
168 static struct nvkm_i2c_port *
169 nvkm_i2c_find(struct nvkm_i2c *i2c, u8 index)
171 struct nvkm_bios *bios = nvkm_bios(i2c);
172 struct nvkm_i2c_port *port;
174 if (index == NV_I2C_DEFAULT(0) ||
175 index == NV_I2C_DEFAULT(1)) {
176 u8 ver, hdr, cnt, len;
177 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
178 if (i2c && ver >= 0x30) {
179 u8 auxidx = nv_ro08(bios, i2c + 4);
180 if (index == NV_I2C_DEFAULT(0))
181 index = (auxidx & 0x0f) >> 0;
183 index = (auxidx & 0xf0) >> 4;
189 list_for_each_entry(port, &i2c->ports, head) {
190 if (port->index == index)
197 static struct nvkm_i2c_port *
198 nvkm_i2c_find_type(struct nvkm_i2c *i2c, u16 type)
200 struct nvkm_i2c_port *port;
202 list_for_each_entry(port, &i2c->ports, head) {
203 if (nv_hclass(port) == type)
211 nvkm_i2c_release_pad(struct nvkm_i2c_port *port)
213 struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
214 struct nvkm_i2c *i2c = nvkm_i2c(port);
216 if (atomic_dec_and_test(&nv_object(pad)->usecount)) {
217 nv_ofuncs(pad)->fini(nv_object(pad), false);
218 wake_up_all(&i2c->wait);
223 nvkm_i2c_try_acquire_pad(struct nvkm_i2c_port *port)
225 struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
227 if (atomic_add_return(1, &nv_object(pad)->usecount) != 1) {
228 struct nvkm_object *owner = (void *)pad->port;
230 if (owner == (void *)port)
232 owner = owner->parent;
234 nvkm_i2c_release_pad(port);
239 nv_ofuncs(pad)->init(nv_object(pad));
244 nvkm_i2c_acquire_pad(struct nvkm_i2c_port *port, unsigned long timeout)
246 struct nvkm_i2c *i2c = nvkm_i2c(port);
249 if (wait_event_timeout(i2c->wait,
250 nvkm_i2c_try_acquire_pad(port) == 0,
254 wait_event(i2c->wait, nvkm_i2c_try_acquire_pad(port) == 0);
261 nvkm_i2c_release(struct nvkm_i2c_port *port)
262 __releases(pad->mutex)
264 nvkm_i2c(port)->release_pad(port);
265 mutex_unlock(&port->mutex);
269 nvkm_i2c_acquire(struct nvkm_i2c_port *port, unsigned long timeout)
270 __acquires(pad->mutex)
273 mutex_lock(&port->mutex);
274 if ((ret = nvkm_i2c(port)->acquire_pad(port, timeout)))
275 mutex_unlock(&port->mutex);
280 nvkm_i2c_identify(struct nvkm_i2c *i2c, int index, const char *what,
281 struct nvkm_i2c_board_info *info,
282 bool (*match)(struct nvkm_i2c_port *,
283 struct i2c_board_info *, void *), void *data)
285 struct nvkm_i2c_port *port = nvkm_i2c_find(i2c, index);
289 nv_debug(i2c, "no bus when probing %s on %d\n", what, index);
293 nv_debug(i2c, "probing %ss on bus: %d\n", what, port->index);
294 for (i = 0; info[i].dev.addr; i++) {
297 if ((port->adapter.algo == &i2c_bit_algo) &&
298 (info[i].udelay != 0)) {
299 struct i2c_algo_bit_data *algo = port->adapter.algo_data;
300 nv_debug(i2c, "using custom udelay %d instead of %d\n",
301 info[i].udelay, algo->udelay);
302 orig_udelay = algo->udelay;
303 algo->udelay = info[i].udelay;
306 if (nv_probe_i2c(port, info[i].dev.addr) &&
307 (!match || match(port, &info[i].dev, data))) {
308 nv_info(i2c, "detected %s: %s\n", what,
314 struct i2c_algo_bit_data *algo = port->adapter.algo_data;
315 algo->udelay = orig_udelay;
319 nv_debug(i2c, "no devices found.\n");
324 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int index)
326 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
327 struct nvkm_i2c_port *port = i2c->find(i2c, index);
328 const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass;
329 if (port && port->aux >= 0)
330 impl->aux_mask(i2c, type, 1 << port->aux, 0);
334 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int index)
336 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
337 struct nvkm_i2c_port *port = i2c->find(i2c, index);
338 const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass;
339 if (port && port->aux >= 0)
340 impl->aux_mask(i2c, type, 1 << port->aux, 1 << port->aux);
344 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
345 struct nvkm_notify *notify)
347 struct nvkm_i2c_ntfy_req *req = data;
348 if (!WARN_ON(size != sizeof(*req))) {
349 notify->size = sizeof(struct nvkm_i2c_ntfy_rep);
350 notify->types = req->mask;
351 notify->index = req->port;
358 nvkm_i2c_intr(struct nvkm_subdev *subdev)
360 struct nvkm_i2c_impl *impl = (void *)nv_oclass(subdev);
361 struct nvkm_i2c *i2c = nvkm_i2c(subdev);
362 struct nvkm_i2c_port *port;
363 u32 hi, lo, rq, tx, e;
365 if (impl->aux_stat) {
366 impl->aux_stat(i2c, &hi, &lo, &rq, &tx);
367 if (hi || lo || rq || tx) {
368 list_for_each_entry(port, &i2c->ports, head) {
369 if (e = 0, port->aux < 0)
372 if (hi & (1 << port->aux)) e |= NVKM_I2C_PLUG;
373 if (lo & (1 << port->aux)) e |= NVKM_I2C_UNPLUG;
374 if (rq & (1 << port->aux)) e |= NVKM_I2C_IRQ;
375 if (tx & (1 << port->aux)) e |= NVKM_I2C_DONE;
377 struct nvkm_i2c_ntfy_rep rep = {
380 nvkm_event_send(&i2c->event, rep.mask,
389 static const struct nvkm_event_func
390 nvkm_i2c_intr_func = {
391 .ctor = nvkm_i2c_intr_ctor,
392 .init = nvkm_i2c_intr_init,
393 .fini = nvkm_i2c_intr_fini,
397 _nvkm_i2c_fini(struct nvkm_object *object, bool suspend)
399 struct nvkm_i2c_impl *impl = (void *)nv_oclass(object);
400 struct nvkm_i2c *i2c = (void *)object;
401 struct nvkm_i2c_port *port;
405 list_for_each_entry(port, &i2c->ports, head) {
406 ret = nv_ofuncs(port)->fini(nv_object(port), suspend);
411 if ((mask = (1 << impl->aux) - 1), impl->aux_stat) {
412 impl->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
413 impl->aux_stat(i2c, &mask, &mask, &mask, &mask);
416 return nvkm_subdev_fini(&i2c->base, suspend);
418 list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
419 nv_ofuncs(port)->init(nv_object(port));
426 _nvkm_i2c_init(struct nvkm_object *object)
428 struct nvkm_i2c *i2c = (void *)object;
429 struct nvkm_i2c_port *port;
432 ret = nvkm_subdev_init(&i2c->base);
434 list_for_each_entry(port, &i2c->ports, head) {
435 ret = nv_ofuncs(port)->init(nv_object(port));
443 list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
444 nv_ofuncs(port)->fini(nv_object(port), false);
451 _nvkm_i2c_dtor(struct nvkm_object *object)
453 struct nvkm_i2c *i2c = (void *)object;
454 struct nvkm_i2c_port *port, *temp;
456 nvkm_event_fini(&i2c->event);
458 list_for_each_entry_safe(port, temp, &i2c->ports, head) {
459 nvkm_object_ref(NULL, (struct nvkm_object **)&port);
462 nvkm_subdev_destroy(&i2c->base);
465 static struct nvkm_oclass *
466 nvkm_i2c_extdev_sclass[] = {
471 nvkm_i2c_create_port(struct nvkm_i2c *i2c, int index, u8 type,
472 struct dcb_i2c_entry *info)
474 const struct nvkm_i2c_impl *impl = (void *)nv_oclass(i2c);
475 struct nvkm_oclass *oclass;
476 struct nvkm_object *parent;
477 struct nvkm_object *object;
480 if (info->share != DCB_I2C_UNUSED) {
482 oclass = impl->pad_s;
484 if (type != DCB_I2C_NVIO_AUX)
485 pad = 0x100 + info->drive;
487 pad = 0x100 + info->auxch;
488 oclass = impl->pad_x;
491 ret = nvkm_object_ctor(nv_object(i2c), NULL, oclass,
496 oclass = impl->sclass;
499 if (oclass->handle == type) {
500 ret = nvkm_object_ctor(parent, NULL, oclass,
501 info, index, &object);
503 } while (ret && (++oclass)->handle);
505 nvkm_object_ref(NULL, &parent);
509 nvkm_i2c_create_(struct nvkm_object *parent, struct nvkm_object *engine,
510 struct nvkm_oclass *oclass, int length, void **pobject)
512 struct nvkm_bios *bios = nvkm_bios(parent);
513 struct nvkm_i2c *i2c;
514 struct nvkm_object *object;
515 struct dcb_i2c_entry info;
516 int ret, i, j, index = -1;
517 struct dcb_output outp;
521 ret = nvkm_subdev_create(parent, engine, oclass, 0, "I2C", "i2c", &i2c);
522 *pobject = nv_object(i2c);
526 nv_subdev(i2c)->intr = nvkm_i2c_intr;
527 i2c->find = nvkm_i2c_find;
528 i2c->find_type = nvkm_i2c_find_type;
529 i2c->acquire_pad = nvkm_i2c_acquire_pad;
530 i2c->release_pad = nvkm_i2c_release_pad;
531 i2c->acquire = nvkm_i2c_acquire;
532 i2c->release = nvkm_i2c_release;
533 i2c->identify = nvkm_i2c_identify;
534 init_waitqueue_head(&i2c->wait);
535 INIT_LIST_HEAD(&i2c->ports);
537 while (!dcb_i2c_parse(bios, ++index, &info)) {
539 case DCB_I2C_NV04_BIT:
540 case DCB_I2C_NV4E_BIT:
541 case DCB_I2C_NVIO_BIT:
542 nvkm_i2c_create_port(i2c, NV_I2C_PORT(index),
545 case DCB_I2C_NVIO_AUX:
546 nvkm_i2c_create_port(i2c, NV_I2C_AUX(index),
550 if (info.drive != DCB_I2C_UNUSED) {
551 nvkm_i2c_create_port(i2c, NV_I2C_PORT(index),
552 DCB_I2C_NVIO_BIT, &info);
554 if (info.auxch != DCB_I2C_UNUSED) {
555 nvkm_i2c_create_port(i2c, NV_I2C_AUX(index),
556 DCB_I2C_NVIO_AUX, &info);
565 /* in addition to the busses specified in the i2c table, there
566 * may be ddc/aux channels hiding behind external tmds/dp/etc
569 index = NV_I2C_EXT(0);
571 while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &outp))) {
572 if (!outp.location || !outp.extdev)
576 case DCB_OUTPUT_TMDS:
577 info.type = NV_I2C_TYPE_EXTDDC(outp.extdev);
580 info.type = NV_I2C_TYPE_EXTAUX(outp.extdev);
588 while (ret && ++j < ARRAY_SIZE(nvkm_i2c_extdev_sclass)) {
589 parent = nv_object(i2c->find(i2c, outp.i2c_index));
590 oclass = nvkm_i2c_extdev_sclass[j];
592 if (oclass->handle != info.type)
594 ret = nvkm_object_ctor(parent, NULL, oclass,
595 NULL, index++, &object);
596 } while (ret && (++oclass)->handle);
600 ret = nvkm_event_init(&nvkm_i2c_intr_func, 4, index, &i2c->event);
608 _nvkm_i2c_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
609 struct nvkm_oclass *oclass, void *data, u32 size,
610 struct nvkm_object **pobject)
612 struct nvkm_i2c *i2c;
615 ret = nvkm_i2c_create(parent, engine, oclass, &i2c);
616 *pobject = nv_object(i2c);