1 /* 2 * Copyright 2013 Red Hat Inc. 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 * 22 * Authors: Ben Skeggs 23 */ 24 #include "priv.h" 25 #include "pad.h" 26 27 #include <core/notify.h> 28 #include <core/option.h> 29 #include <subdev/bios.h> 30 #include <subdev/bios/dcb.h> 31 32 /****************************************************************************** 33 * interface to linux i2c bit-banging algorithm 34 *****************************************************************************/ 35 36 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT 37 #define CSTMSEL true 38 #else 39 #define CSTMSEL false 40 #endif 41 42 static int 43 nvkm_i2c_pre_xfer(struct i2c_adapter *adap) 44 { 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); 48 } 49 50 static void 51 nvkm_i2c_post_xfer(struct i2c_adapter *adap) 52 { 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); 56 } 57 58 static void 59 nvkm_i2c_setscl(void *data, int state) 60 { 61 struct nvkm_i2c_port *port = data; 62 port->func->drive_scl(port, state); 63 } 64 65 static void 66 nvkm_i2c_setsda(void *data, int state) 67 { 68 struct nvkm_i2c_port *port = data; 69 port->func->drive_sda(port, state); 70 } 71 72 static int 73 nvkm_i2c_getscl(void *data) 74 { 75 struct nvkm_i2c_port *port = data; 76 return port->func->sense_scl(port); 77 } 78 79 static int 80 nvkm_i2c_getsda(void *data) 81 { 82 struct nvkm_i2c_port *port = data; 83 return port->func->sense_sda(port); 84 } 85 86 /****************************************************************************** 87 * base i2c "port" class implementation 88 *****************************************************************************/ 89 90 int 91 _nvkm_i2c_port_fini(struct nvkm_object *object, bool suspend) 92 { 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); 97 } 98 99 void 100 _nvkm_i2c_port_dtor(struct nvkm_object *object) 101 { 102 struct nvkm_i2c_port *port = (void *)object; 103 i2c_del_adapter(&port->adapter); 104 nvkm_object_destroy(&port->base); 105 } 106 107 int 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) 113 { 114 struct nvkm_device *device = nv_device(parent); 115 struct nvkm_i2c *i2c = nvkm_i2c(parent); 116 struct nvkm_i2c_port *port; 117 int ret; 118 119 ret = nvkm_object_create_(parent, engine, oclass, 0, size, pobject); 120 port = *pobject; 121 if (ret) 122 return ret; 123 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); 128 port->index = index; 129 port->aux = -1; 130 port->func = func; 131 mutex_init(&port->mutex); 132 133 if ( algo == &nvkm_i2c_bit_algo && 134 !nvkm_boolopt(device->cfgopt, "NvI2C", CSTMSEL)) { 135 struct i2c_algo_bit_data *bit; 136 137 bit = kzalloc(sizeof(*bit), GFP_KERNEL); 138 if (!bit) 139 return -ENOMEM; 140 141 bit->udelay = 10; 142 bit->timeout = usecs_to_jiffies(2200); 143 bit->data = port; 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; 150 151 port->adapter.algo_data = bit; 152 ret = i2c_bit_add_bus(&port->adapter); 153 } else { 154 port->adapter.algo_data = port; 155 port->adapter.algo = algo; 156 ret = i2c_add_adapter(&port->adapter); 157 } 158 159 if (ret == 0) 160 list_add_tail(&port->head, &i2c->ports); 161 return ret; 162 } 163 164 /****************************************************************************** 165 * base i2c subdev class implementation 166 *****************************************************************************/ 167 168 static struct nvkm_i2c_port * 169 nvkm_i2c_find(struct nvkm_i2c *i2c, u8 index) 170 { 171 struct nvkm_bios *bios = nvkm_bios(i2c); 172 struct nvkm_i2c_port *port; 173 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 = nvbios_rd08(bios, i2c + 4); 180 if (index == NV_I2C_DEFAULT(0)) 181 index = (auxidx & 0x0f) >> 0; 182 else 183 index = (auxidx & 0xf0) >> 4; 184 } else { 185 index = 2; 186 } 187 } 188 189 list_for_each_entry(port, &i2c->ports, head) { 190 if (port->index == index) 191 return port; 192 } 193 194 return NULL; 195 } 196 197 static struct nvkm_i2c_port * 198 nvkm_i2c_find_type(struct nvkm_i2c *i2c, u16 type) 199 { 200 struct nvkm_i2c_port *port; 201 202 list_for_each_entry(port, &i2c->ports, head) { 203 if (nv_hclass(port) == type) 204 return port; 205 } 206 207 return NULL; 208 } 209 210 static void 211 nvkm_i2c_release_pad(struct nvkm_i2c_port *port) 212 { 213 struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port); 214 struct nvkm_i2c *i2c = nvkm_i2c(port); 215 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); 219 } 220 } 221 222 static int 223 nvkm_i2c_try_acquire_pad(struct nvkm_i2c_port *port) 224 { 225 struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port); 226 227 if (atomic_add_return(1, &nv_object(pad)->usecount) != 1) { 228 struct nvkm_object *owner = (void *)pad->port; 229 do { 230 if (owner == (void *)port) 231 return 0; 232 owner = owner->parent; 233 } while(owner); 234 nvkm_i2c_release_pad(port); 235 return -EBUSY; 236 } 237 238 pad->next = port; 239 nv_ofuncs(pad)->init(nv_object(pad)); 240 return 0; 241 } 242 243 static int 244 nvkm_i2c_acquire_pad(struct nvkm_i2c_port *port, unsigned long timeout) 245 { 246 struct nvkm_i2c *i2c = nvkm_i2c(port); 247 248 if (timeout) { 249 if (wait_event_timeout(i2c->wait, 250 nvkm_i2c_try_acquire_pad(port) == 0, 251 timeout) == 0) 252 return -EBUSY; 253 } else { 254 wait_event(i2c->wait, nvkm_i2c_try_acquire_pad(port) == 0); 255 } 256 257 return 0; 258 } 259 260 static void 261 nvkm_i2c_release(struct nvkm_i2c_port *port) 262 __releases(pad->mutex) 263 { 264 nvkm_i2c(port)->release_pad(port); 265 mutex_unlock(&port->mutex); 266 } 267 268 static int 269 nvkm_i2c_acquire(struct nvkm_i2c_port *port, unsigned long timeout) 270 __acquires(pad->mutex) 271 { 272 int ret; 273 mutex_lock(&port->mutex); 274 if ((ret = nvkm_i2c(port)->acquire_pad(port, timeout))) 275 mutex_unlock(&port->mutex); 276 return ret; 277 } 278 279 static int 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) 284 { 285 struct nvkm_subdev *subdev = &i2c->subdev; 286 struct nvkm_i2c_port *port = nvkm_i2c_find(i2c, index); 287 int i; 288 289 if (!port) { 290 nvkm_debug(subdev, "no bus when probing %s on %d\n", 291 what, index); 292 return -ENODEV; 293 } 294 295 nvkm_debug(subdev, "probing %ss on bus: %d\n", what, port->index); 296 for (i = 0; info[i].dev.addr; i++) { 297 u8 orig_udelay = 0; 298 299 if ((port->adapter.algo == &i2c_bit_algo) && 300 (info[i].udelay != 0)) { 301 struct i2c_algo_bit_data *algo = port->adapter.algo_data; 302 nvkm_debug(subdev, 303 "using custom udelay %d instead of %d\n", 304 info[i].udelay, algo->udelay); 305 orig_udelay = algo->udelay; 306 algo->udelay = info[i].udelay; 307 } 308 309 if (nv_probe_i2c(port, info[i].dev.addr) && 310 (!match || match(port, &info[i].dev, data))) { 311 nvkm_info(subdev, "detected %s: %s\n", what, 312 info[i].dev.type); 313 return i; 314 } 315 316 if (orig_udelay) { 317 struct i2c_algo_bit_data *algo = port->adapter.algo_data; 318 algo->udelay = orig_udelay; 319 } 320 } 321 322 nvkm_debug(subdev, "no devices found.\n"); 323 return -ENODEV; 324 } 325 326 static void 327 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int index) 328 { 329 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event); 330 struct nvkm_i2c_port *port = i2c->find(i2c, index); 331 const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass; 332 if (port && port->aux >= 0) 333 impl->aux_mask(i2c, type, 1 << port->aux, 0); 334 } 335 336 static void 337 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int index) 338 { 339 struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event); 340 struct nvkm_i2c_port *port = i2c->find(i2c, index); 341 const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass; 342 if (port && port->aux >= 0) 343 impl->aux_mask(i2c, type, 1 << port->aux, 1 << port->aux); 344 } 345 346 static int 347 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size, 348 struct nvkm_notify *notify) 349 { 350 struct nvkm_i2c_ntfy_req *req = data; 351 if (!WARN_ON(size != sizeof(*req))) { 352 notify->size = sizeof(struct nvkm_i2c_ntfy_rep); 353 notify->types = req->mask; 354 notify->index = req->port; 355 return 0; 356 } 357 return -EINVAL; 358 } 359 360 static void 361 nvkm_i2c_intr(struct nvkm_subdev *subdev) 362 { 363 struct nvkm_i2c_impl *impl = (void *)nv_oclass(subdev); 364 struct nvkm_i2c *i2c = nvkm_i2c(subdev); 365 struct nvkm_i2c_port *port; 366 u32 hi, lo, rq, tx, e; 367 368 if (impl->aux_stat) { 369 impl->aux_stat(i2c, &hi, &lo, &rq, &tx); 370 if (hi || lo || rq || tx) { 371 list_for_each_entry(port, &i2c->ports, head) { 372 if (e = 0, port->aux < 0) 373 continue; 374 375 if (hi & (1 << port->aux)) e |= NVKM_I2C_PLUG; 376 if (lo & (1 << port->aux)) e |= NVKM_I2C_UNPLUG; 377 if (rq & (1 << port->aux)) e |= NVKM_I2C_IRQ; 378 if (tx & (1 << port->aux)) e |= NVKM_I2C_DONE; 379 if (e) { 380 struct nvkm_i2c_ntfy_rep rep = { 381 .mask = e, 382 }; 383 nvkm_event_send(&i2c->event, rep.mask, 384 port->index, &rep, 385 sizeof(rep)); 386 } 387 } 388 } 389 } 390 } 391 392 static const struct nvkm_event_func 393 nvkm_i2c_intr_func = { 394 .ctor = nvkm_i2c_intr_ctor, 395 .init = nvkm_i2c_intr_init, 396 .fini = nvkm_i2c_intr_fini, 397 }; 398 399 int 400 _nvkm_i2c_fini(struct nvkm_object *object, bool suspend) 401 { 402 struct nvkm_i2c_impl *impl = (void *)nv_oclass(object); 403 struct nvkm_i2c *i2c = (void *)object; 404 struct nvkm_i2c_port *port; 405 u32 mask; 406 int ret; 407 408 list_for_each_entry(port, &i2c->ports, head) { 409 ret = nv_ofuncs(port)->fini(nv_object(port), suspend); 410 if (ret && suspend) 411 goto fail; 412 } 413 414 if ((mask = (1 << impl->aux) - 1), impl->aux_stat) { 415 impl->aux_mask(i2c, NVKM_I2C_ANY, mask, 0); 416 impl->aux_stat(i2c, &mask, &mask, &mask, &mask); 417 } 418 419 return nvkm_subdev_fini(&i2c->subdev, suspend); 420 fail: 421 list_for_each_entry_continue_reverse(port, &i2c->ports, head) { 422 nv_ofuncs(port)->init(nv_object(port)); 423 } 424 425 return ret; 426 } 427 428 int 429 _nvkm_i2c_init(struct nvkm_object *object) 430 { 431 struct nvkm_i2c *i2c = (void *)object; 432 struct nvkm_i2c_port *port; 433 int ret; 434 435 ret = nvkm_subdev_init(&i2c->subdev); 436 if (ret == 0) { 437 list_for_each_entry(port, &i2c->ports, head) { 438 ret = nv_ofuncs(port)->init(nv_object(port)); 439 if (ret) 440 goto fail; 441 } 442 } 443 444 return ret; 445 fail: 446 list_for_each_entry_continue_reverse(port, &i2c->ports, head) { 447 nv_ofuncs(port)->fini(nv_object(port), false); 448 } 449 450 return ret; 451 } 452 453 void 454 _nvkm_i2c_dtor(struct nvkm_object *object) 455 { 456 struct nvkm_i2c *i2c = (void *)object; 457 struct nvkm_i2c_port *port, *temp; 458 459 nvkm_event_fini(&i2c->event); 460 461 list_for_each_entry_safe(port, temp, &i2c->ports, head) { 462 nvkm_object_ref(NULL, (struct nvkm_object **)&port); 463 } 464 465 nvkm_subdev_destroy(&i2c->subdev); 466 } 467 468 static struct nvkm_oclass * 469 nvkm_i2c_extdev_sclass[] = { 470 nvkm_anx9805_sclass, 471 }; 472 473 static void 474 nvkm_i2c_create_port(struct nvkm_i2c *i2c, int index, u8 type, 475 struct dcb_i2c_entry *info) 476 { 477 const struct nvkm_i2c_impl *impl = (void *)nv_oclass(i2c); 478 struct nvkm_oclass *oclass; 479 struct nvkm_object *parent; 480 struct nvkm_object *object; 481 int ret, pad; 482 483 if (info->share != DCB_I2C_UNUSED) { 484 pad = info->share; 485 oclass = impl->pad_s; 486 } else { 487 if (type != DCB_I2C_NVIO_AUX) 488 pad = 0x100 + info->drive; 489 else 490 pad = 0x100 + info->auxch; 491 oclass = impl->pad_x; 492 } 493 494 ret = nvkm_object_ctor(nv_object(i2c), NULL, oclass, 495 NULL, pad, &parent); 496 if (ret < 0) 497 return; 498 499 oclass = impl->sclass; 500 do { 501 ret = -EINVAL; 502 if (oclass->handle == type) { 503 ret = nvkm_object_ctor(parent, NULL, oclass, 504 info, index, &object); 505 } 506 } while (ret && (++oclass)->handle); 507 508 nvkm_object_ref(NULL, &parent); 509 } 510 511 int 512 nvkm_i2c_create_(struct nvkm_object *parent, struct nvkm_object *engine, 513 struct nvkm_oclass *oclass, int length, void **pobject) 514 { 515 struct nvkm_bios *bios = nvkm_bios(parent); 516 struct nvkm_i2c *i2c; 517 struct nvkm_object *object; 518 struct dcb_i2c_entry info; 519 int ret, i, j, index = -1; 520 struct dcb_output outp; 521 u8 ver, hdr; 522 u32 data; 523 524 ret = nvkm_subdev_create(parent, engine, oclass, 0, "I2C", "i2c", &i2c); 525 *pobject = nv_object(i2c); 526 if (ret) 527 return ret; 528 529 nv_subdev(i2c)->intr = nvkm_i2c_intr; 530 i2c->find = nvkm_i2c_find; 531 i2c->find_type = nvkm_i2c_find_type; 532 i2c->acquire_pad = nvkm_i2c_acquire_pad; 533 i2c->release_pad = nvkm_i2c_release_pad; 534 i2c->acquire = nvkm_i2c_acquire; 535 i2c->release = nvkm_i2c_release; 536 i2c->identify = nvkm_i2c_identify; 537 init_waitqueue_head(&i2c->wait); 538 INIT_LIST_HEAD(&i2c->ports); 539 540 while (!dcb_i2c_parse(bios, ++index, &info)) { 541 switch (info.type) { 542 case DCB_I2C_NV04_BIT: 543 case DCB_I2C_NV4E_BIT: 544 case DCB_I2C_NVIO_BIT: 545 nvkm_i2c_create_port(i2c, NV_I2C_PORT(index), 546 info.type, &info); 547 break; 548 case DCB_I2C_NVIO_AUX: 549 nvkm_i2c_create_port(i2c, NV_I2C_AUX(index), 550 info.type, &info); 551 break; 552 case DCB_I2C_PMGR: 553 if (info.drive != DCB_I2C_UNUSED) { 554 nvkm_i2c_create_port(i2c, NV_I2C_PORT(index), 555 DCB_I2C_NVIO_BIT, &info); 556 } 557 if (info.auxch != DCB_I2C_UNUSED) { 558 nvkm_i2c_create_port(i2c, NV_I2C_AUX(index), 559 DCB_I2C_NVIO_AUX, &info); 560 } 561 break; 562 case DCB_I2C_UNUSED: 563 default: 564 continue; 565 } 566 } 567 568 /* in addition to the busses specified in the i2c table, there 569 * may be ddc/aux channels hiding behind external tmds/dp/etc 570 * transmitters. 571 */ 572 index = NV_I2C_EXT(0); 573 i = -1; 574 while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &outp))) { 575 if (!outp.location || !outp.extdev) 576 continue; 577 578 switch (outp.type) { 579 case DCB_OUTPUT_TMDS: 580 info.type = NV_I2C_TYPE_EXTDDC(outp.extdev); 581 break; 582 case DCB_OUTPUT_DP: 583 info.type = NV_I2C_TYPE_EXTAUX(outp.extdev); 584 break; 585 default: 586 continue; 587 } 588 589 ret = -ENODEV; 590 j = -1; 591 while (ret && ++j < ARRAY_SIZE(nvkm_i2c_extdev_sclass)) { 592 parent = nv_object(i2c->find(i2c, outp.i2c_index)); 593 oclass = nvkm_i2c_extdev_sclass[j]; 594 do { 595 if (oclass->handle != info.type) 596 continue; 597 ret = nvkm_object_ctor(parent, NULL, oclass, 598 NULL, index++, &object); 599 } while (ret && (++oclass)->handle); 600 } 601 } 602 603 ret = nvkm_event_init(&nvkm_i2c_intr_func, 4, index, &i2c->event); 604 if (ret) 605 return ret; 606 607 return 0; 608 } 609 610 int 611 _nvkm_i2c_ctor(struct nvkm_object *parent, struct nvkm_object *engine, 612 struct nvkm_oclass *oclass, void *data, u32 size, 613 struct nvkm_object **pobject) 614 { 615 struct nvkm_i2c *i2c; 616 int ret; 617 618 ret = nvkm_i2c_create(parent, engine, oclass, &i2c); 619 *pobject = nv_object(i2c); 620 if (ret) 621 return ret; 622 623 return 0; 624 } 625