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/device.h>
28 #include <core/notify.h>
29 #include <core/option.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/dcb.h>
33 /******************************************************************************
34 * interface to linux i2c bit-banging algorithm
35 *****************************************************************************/
37 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
44 nvkm_i2c_pre_xfer(struct i2c_adapter
*adap
)
46 struct i2c_algo_bit_data
*bit
= adap
->algo_data
;
47 struct nvkm_i2c_port
*port
= bit
->data
;
48 return nvkm_i2c(port
)->acquire(port
, bit
->timeout
);
52 nvkm_i2c_post_xfer(struct i2c_adapter
*adap
)
54 struct i2c_algo_bit_data
*bit
= adap
->algo_data
;
55 struct nvkm_i2c_port
*port
= bit
->data
;
56 return nvkm_i2c(port
)->release(port
);
60 nvkm_i2c_setscl(void *data
, int state
)
62 struct nvkm_i2c_port
*port
= data
;
63 port
->func
->drive_scl(port
, state
);
67 nvkm_i2c_setsda(void *data
, int state
)
69 struct nvkm_i2c_port
*port
= data
;
70 port
->func
->drive_sda(port
, state
);
74 nvkm_i2c_getscl(void *data
)
76 struct nvkm_i2c_port
*port
= data
;
77 return port
->func
->sense_scl(port
);
81 nvkm_i2c_getsda(void *data
)
83 struct nvkm_i2c_port
*port
= data
;
84 return port
->func
->sense_sda(port
);
87 /******************************************************************************
88 * base i2c "port" class implementation
89 *****************************************************************************/
92 _nvkm_i2c_port_fini(struct nvkm_object
*object
, bool suspend
)
94 struct nvkm_i2c_port
*port
= (void *)object
;
95 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
96 nv_ofuncs(pad
)->fini(nv_object(pad
), suspend
);
97 return nvkm_object_fini(&port
->base
, suspend
);
101 _nvkm_i2c_port_dtor(struct nvkm_object
*object
)
103 struct nvkm_i2c_port
*port
= (void *)object
;
104 i2c_del_adapter(&port
->adapter
);
105 nvkm_object_destroy(&port
->base
);
109 nvkm_i2c_port_create_(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
110 struct nvkm_oclass
*oclass
, u8 index
,
111 const struct i2c_algorithm
*algo
,
112 const struct nvkm_i2c_func
*func
,
113 int size
, void **pobject
)
115 struct nvkm_device
*device
= nv_device(parent
);
116 struct nvkm_i2c
*i2c
= nvkm_i2c(parent
);
117 struct nvkm_i2c_port
*port
;
120 ret
= nvkm_object_create_(parent
, engine
, oclass
, 0, size
, pobject
);
125 snprintf(port
->adapter
.name
, sizeof(port
->adapter
.name
),
126 "nvkm-%s-%d", device
->name
, index
);
127 port
->adapter
.owner
= THIS_MODULE
;
128 port
->adapter
.dev
.parent
= nv_device_base(device
);
132 mutex_init(&port
->mutex
);
134 if ( algo
== &nvkm_i2c_bit_algo
&&
135 !nvkm_boolopt(device
->cfgopt
, "NvI2C", CSTMSEL
)) {
136 struct i2c_algo_bit_data
*bit
;
138 bit
= kzalloc(sizeof(*bit
), GFP_KERNEL
);
143 bit
->timeout
= usecs_to_jiffies(2200);
145 bit
->pre_xfer
= nvkm_i2c_pre_xfer
;
146 bit
->post_xfer
= nvkm_i2c_post_xfer
;
147 bit
->setsda
= nvkm_i2c_setsda
;
148 bit
->setscl
= nvkm_i2c_setscl
;
149 bit
->getsda
= nvkm_i2c_getsda
;
150 bit
->getscl
= nvkm_i2c_getscl
;
152 port
->adapter
.algo_data
= bit
;
153 ret
= i2c_bit_add_bus(&port
->adapter
);
155 port
->adapter
.algo_data
= port
;
156 port
->adapter
.algo
= algo
;
157 ret
= i2c_add_adapter(&port
->adapter
);
161 list_add_tail(&port
->head
, &i2c
->ports
);
165 /******************************************************************************
166 * base i2c subdev class implementation
167 *****************************************************************************/
169 static struct nvkm_i2c_port
*
170 nvkm_i2c_find(struct nvkm_i2c
*i2c
, u8 index
)
172 struct nvkm_bios
*bios
= nvkm_bios(i2c
);
173 struct nvkm_i2c_port
*port
;
175 if (index
== NV_I2C_DEFAULT(0) ||
176 index
== NV_I2C_DEFAULT(1)) {
177 u8 ver
, hdr
, cnt
, len
;
178 u16 i2c
= dcb_i2c_table(bios
, &ver
, &hdr
, &cnt
, &len
);
179 if (i2c
&& ver
>= 0x30) {
180 u8 auxidx
= nv_ro08(bios
, i2c
+ 4);
181 if (index
== NV_I2C_DEFAULT(0))
182 index
= (auxidx
& 0x0f) >> 0;
184 index
= (auxidx
& 0xf0) >> 4;
190 list_for_each_entry(port
, &i2c
->ports
, head
) {
191 if (port
->index
== index
)
198 static struct nvkm_i2c_port
*
199 nvkm_i2c_find_type(struct nvkm_i2c
*i2c
, u16 type
)
201 struct nvkm_i2c_port
*port
;
203 list_for_each_entry(port
, &i2c
->ports
, head
) {
204 if (nv_hclass(port
) == type
)
212 nvkm_i2c_release_pad(struct nvkm_i2c_port
*port
)
214 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
215 struct nvkm_i2c
*i2c
= nvkm_i2c(port
);
217 if (atomic_dec_and_test(&nv_object(pad
)->usecount
)) {
218 nv_ofuncs(pad
)->fini(nv_object(pad
), false);
219 wake_up_all(&i2c
->wait
);
224 nvkm_i2c_try_acquire_pad(struct nvkm_i2c_port
*port
)
226 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
228 if (atomic_add_return(1, &nv_object(pad
)->usecount
) != 1) {
229 struct nvkm_object
*owner
= (void *)pad
->port
;
231 if (owner
== (void *)port
)
233 owner
= owner
->parent
;
235 nvkm_i2c_release_pad(port
);
240 nv_ofuncs(pad
)->init(nv_object(pad
));
245 nvkm_i2c_acquire_pad(struct nvkm_i2c_port
*port
, unsigned long timeout
)
247 struct nvkm_i2c
*i2c
= nvkm_i2c(port
);
250 if (wait_event_timeout(i2c
->wait
,
251 nvkm_i2c_try_acquire_pad(port
) == 0,
255 wait_event(i2c
->wait
, nvkm_i2c_try_acquire_pad(port
) == 0);
262 nvkm_i2c_release(struct nvkm_i2c_port
*port
)
263 __releases(pad
->mutex
)
265 nvkm_i2c(port
)->release_pad(port
);
266 mutex_unlock(&port
->mutex
);
270 nvkm_i2c_acquire(struct nvkm_i2c_port
*port
, unsigned long timeout
)
271 __acquires(pad
->mutex
)
274 mutex_lock(&port
->mutex
);
275 if ((ret
= nvkm_i2c(port
)->acquire_pad(port
, timeout
)))
276 mutex_unlock(&port
->mutex
);
281 nvkm_i2c_identify(struct nvkm_i2c
*i2c
, int index
, const char *what
,
282 struct nvkm_i2c_board_info
*info
,
283 bool (*match
)(struct nvkm_i2c_port
*,
284 struct i2c_board_info
*, void *), void *data
)
286 struct nvkm_i2c_port
*port
= nvkm_i2c_find(i2c
, index
);
290 nv_debug(i2c
, "no bus when probing %s on %d\n", what
, index
);
294 nv_debug(i2c
, "probing %ss on bus: %d\n", what
, port
->index
);
295 for (i
= 0; info
[i
].dev
.addr
; i
++) {
298 if ((port
->adapter
.algo
== &i2c_bit_algo
) &&
299 (info
[i
].udelay
!= 0)) {
300 struct i2c_algo_bit_data
*algo
= port
->adapter
.algo_data
;
301 nv_debug(i2c
, "using custom udelay %d instead of %d\n",
302 info
[i
].udelay
, algo
->udelay
);
303 orig_udelay
= algo
->udelay
;
304 algo
->udelay
= info
[i
].udelay
;
307 if (nv_probe_i2c(port
, info
[i
].dev
.addr
) &&
308 (!match
|| match(port
, &info
[i
].dev
, data
))) {
309 nv_info(i2c
, "detected %s: %s\n", what
,
315 struct i2c_algo_bit_data
*algo
= port
->adapter
.algo_data
;
316 algo
->udelay
= orig_udelay
;
320 nv_debug(i2c
, "no devices found.\n");
325 nvkm_i2c_intr_fini(struct nvkm_event
*event
, int type
, int index
)
327 struct nvkm_i2c
*i2c
= container_of(event
, typeof(*i2c
), event
);
328 struct nvkm_i2c_port
*port
= i2c
->find(i2c
, index
);
329 const struct nvkm_i2c_impl
*impl
= (void *)nv_object(i2c
)->oclass
;
330 if (port
&& port
->aux
>= 0)
331 impl
->aux_mask(i2c
, type
, 1 << port
->aux
, 0);
335 nvkm_i2c_intr_init(struct nvkm_event
*event
, int type
, int index
)
337 struct nvkm_i2c
*i2c
= container_of(event
, typeof(*i2c
), event
);
338 struct nvkm_i2c_port
*port
= i2c
->find(i2c
, index
);
339 const struct nvkm_i2c_impl
*impl
= (void *)nv_object(i2c
)->oclass
;
340 if (port
&& port
->aux
>= 0)
341 impl
->aux_mask(i2c
, type
, 1 << port
->aux
, 1 << port
->aux
);
345 nvkm_i2c_intr_ctor(struct nvkm_object
*object
, void *data
, u32 size
,
346 struct nvkm_notify
*notify
)
348 struct nvkm_i2c_ntfy_req
*req
= data
;
349 if (!WARN_ON(size
!= sizeof(*req
))) {
350 notify
->size
= sizeof(struct nvkm_i2c_ntfy_rep
);
351 notify
->types
= req
->mask
;
352 notify
->index
= req
->port
;
359 nvkm_i2c_intr(struct nvkm_subdev
*subdev
)
361 struct nvkm_i2c_impl
*impl
= (void *)nv_oclass(subdev
);
362 struct nvkm_i2c
*i2c
= nvkm_i2c(subdev
);
363 struct nvkm_i2c_port
*port
;
364 u32 hi
, lo
, rq
, tx
, e
;
366 if (impl
->aux_stat
) {
367 impl
->aux_stat(i2c
, &hi
, &lo
, &rq
, &tx
);
368 if (hi
|| lo
|| rq
|| tx
) {
369 list_for_each_entry(port
, &i2c
->ports
, head
) {
370 if (e
= 0, port
->aux
< 0)
373 if (hi
& (1 << port
->aux
)) e
|= NVKM_I2C_PLUG
;
374 if (lo
& (1 << port
->aux
)) e
|= NVKM_I2C_UNPLUG
;
375 if (rq
& (1 << port
->aux
)) e
|= NVKM_I2C_IRQ
;
376 if (tx
& (1 << port
->aux
)) e
|= NVKM_I2C_DONE
;
378 struct nvkm_i2c_ntfy_rep rep
= {
381 nvkm_event_send(&i2c
->event
, rep
.mask
,
390 static const struct nvkm_event_func
391 nvkm_i2c_intr_func
= {
392 .ctor
= nvkm_i2c_intr_ctor
,
393 .init
= nvkm_i2c_intr_init
,
394 .fini
= nvkm_i2c_intr_fini
,
398 _nvkm_i2c_fini(struct nvkm_object
*object
, bool suspend
)
400 struct nvkm_i2c_impl
*impl
= (void *)nv_oclass(object
);
401 struct nvkm_i2c
*i2c
= (void *)object
;
402 struct nvkm_i2c_port
*port
;
406 list_for_each_entry(port
, &i2c
->ports
, head
) {
407 ret
= nv_ofuncs(port
)->fini(nv_object(port
), suspend
);
412 if ((mask
= (1 << impl
->aux
) - 1), impl
->aux_stat
) {
413 impl
->aux_mask(i2c
, NVKM_I2C_ANY
, mask
, 0);
414 impl
->aux_stat(i2c
, &mask
, &mask
, &mask
, &mask
);
417 return nvkm_subdev_fini(&i2c
->base
, suspend
);
419 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
420 nv_ofuncs(port
)->init(nv_object(port
));
427 _nvkm_i2c_init(struct nvkm_object
*object
)
429 struct nvkm_i2c
*i2c
= (void *)object
;
430 struct nvkm_i2c_port
*port
;
433 ret
= nvkm_subdev_init(&i2c
->base
);
435 list_for_each_entry(port
, &i2c
->ports
, head
) {
436 ret
= nv_ofuncs(port
)->init(nv_object(port
));
444 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
445 nv_ofuncs(port
)->fini(nv_object(port
), false);
452 _nvkm_i2c_dtor(struct nvkm_object
*object
)
454 struct nvkm_i2c
*i2c
= (void *)object
;
455 struct nvkm_i2c_port
*port
, *temp
;
457 nvkm_event_fini(&i2c
->event
);
459 list_for_each_entry_safe(port
, temp
, &i2c
->ports
, head
) {
460 nvkm_object_ref(NULL
, (struct nvkm_object
**)&port
);
463 nvkm_subdev_destroy(&i2c
->base
);
466 static struct nvkm_oclass
*
467 nvkm_i2c_extdev_sclass
[] = {
472 nvkm_i2c_create_port(struct nvkm_i2c
*i2c
, int index
, u8 type
,
473 struct dcb_i2c_entry
*info
)
475 const struct nvkm_i2c_impl
*impl
= (void *)nv_oclass(i2c
);
476 struct nvkm_oclass
*oclass
;
477 struct nvkm_object
*parent
;
478 struct nvkm_object
*object
;
481 if (info
->share
!= DCB_I2C_UNUSED
) {
483 oclass
= impl
->pad_s
;
485 if (type
!= DCB_I2C_NVIO_AUX
)
486 pad
= 0x100 + info
->drive
;
488 pad
= 0x100 + info
->auxch
;
489 oclass
= impl
->pad_x
;
492 ret
= nvkm_object_ctor(nv_object(i2c
), NULL
, oclass
,
497 oclass
= impl
->sclass
;
500 if (oclass
->handle
== type
) {
501 ret
= nvkm_object_ctor(parent
, NULL
, oclass
,
502 info
, index
, &object
);
504 } while (ret
&& (++oclass
)->handle
);
506 nvkm_object_ref(NULL
, &parent
);
510 nvkm_i2c_create_(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
511 struct nvkm_oclass
*oclass
, int length
, void **pobject
)
513 struct nvkm_bios
*bios
= nvkm_bios(parent
);
514 struct nvkm_i2c
*i2c
;
515 struct nvkm_object
*object
;
516 struct dcb_i2c_entry info
;
517 int ret
, i
, j
, index
= -1;
518 struct dcb_output outp
;
522 ret
= nvkm_subdev_create(parent
, engine
, oclass
, 0, "I2C", "i2c", &i2c
);
523 *pobject
= nv_object(i2c
);
527 nv_subdev(i2c
)->intr
= nvkm_i2c_intr
;
528 i2c
->find
= nvkm_i2c_find
;
529 i2c
->find_type
= nvkm_i2c_find_type
;
530 i2c
->acquire_pad
= nvkm_i2c_acquire_pad
;
531 i2c
->release_pad
= nvkm_i2c_release_pad
;
532 i2c
->acquire
= nvkm_i2c_acquire
;
533 i2c
->release
= nvkm_i2c_release
;
534 i2c
->identify
= nvkm_i2c_identify
;
535 init_waitqueue_head(&i2c
->wait
);
536 INIT_LIST_HEAD(&i2c
->ports
);
538 while (!dcb_i2c_parse(bios
, ++index
, &info
)) {
540 case DCB_I2C_NV04_BIT
:
541 case DCB_I2C_NV4E_BIT
:
542 case DCB_I2C_NVIO_BIT
:
543 nvkm_i2c_create_port(i2c
, NV_I2C_PORT(index
),
546 case DCB_I2C_NVIO_AUX
:
547 nvkm_i2c_create_port(i2c
, NV_I2C_AUX(index
),
551 if (info
.drive
!= DCB_I2C_UNUSED
) {
552 nvkm_i2c_create_port(i2c
, NV_I2C_PORT(index
),
553 DCB_I2C_NVIO_BIT
, &info
);
555 if (info
.auxch
!= DCB_I2C_UNUSED
) {
556 nvkm_i2c_create_port(i2c
, NV_I2C_AUX(index
),
557 DCB_I2C_NVIO_AUX
, &info
);
566 /* in addition to the busses specified in the i2c table, there
567 * may be ddc/aux channels hiding behind external tmds/dp/etc
570 index
= NV_I2C_EXT(0);
572 while ((data
= dcb_outp_parse(bios
, ++i
, &ver
, &hdr
, &outp
))) {
573 if (!outp
.location
|| !outp
.extdev
)
577 case DCB_OUTPUT_TMDS
:
578 info
.type
= NV_I2C_TYPE_EXTDDC(outp
.extdev
);
581 info
.type
= NV_I2C_TYPE_EXTAUX(outp
.extdev
);
589 while (ret
&& ++j
< ARRAY_SIZE(nvkm_i2c_extdev_sclass
)) {
590 parent
= nv_object(i2c
->find(i2c
, outp
.i2c_index
));
591 oclass
= nvkm_i2c_extdev_sclass
[j
];
593 if (oclass
->handle
!= info
.type
)
595 ret
= nvkm_object_ctor(parent
, NULL
, oclass
,
596 NULL
, index
++, &object
);
597 } while (ret
&& (++oclass
)->handle
);
601 ret
= nvkm_event_init(&nvkm_i2c_intr_func
, 4, index
, &i2c
->event
);
609 _nvkm_i2c_ctor(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
610 struct nvkm_oclass
*oclass
, void *data
, u32 size
,
611 struct nvkm_object
**pobject
)
613 struct nvkm_i2c
*i2c
;
616 ret
= nvkm_i2c_create(parent
, engine
, oclass
, &i2c
);
617 *pobject
= nv_object(i2c
);