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.
25 #include <core/option.h>
26 #include <core/event.h>
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/bios/i2c.h>
31 #include <subdev/vga.h>
36 /******************************************************************************
37 * interface to linux i2c bit-banging algorithm
38 *****************************************************************************/
40 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
47 nouveau_i2c_pre_xfer(struct i2c_adapter
*adap
)
49 struct i2c_algo_bit_data
*bit
= adap
->algo_data
;
50 struct nouveau_i2c_port
*port
= bit
->data
;
51 return nouveau_i2c(port
)->acquire(port
, bit
->timeout
);
55 nouveau_i2c_post_xfer(struct i2c_adapter
*adap
)
57 struct i2c_algo_bit_data
*bit
= adap
->algo_data
;
58 struct nouveau_i2c_port
*port
= bit
->data
;
59 return nouveau_i2c(port
)->release(port
);
63 nouveau_i2c_setscl(void *data
, int state
)
65 struct nouveau_i2c_port
*port
= data
;
66 port
->func
->drive_scl(port
, state
);
70 nouveau_i2c_setsda(void *data
, int state
)
72 struct nouveau_i2c_port
*port
= data
;
73 port
->func
->drive_sda(port
, state
);
77 nouveau_i2c_getscl(void *data
)
79 struct nouveau_i2c_port
*port
= data
;
80 return port
->func
->sense_scl(port
);
84 nouveau_i2c_getsda(void *data
)
86 struct nouveau_i2c_port
*port
= data
;
87 return port
->func
->sense_sda(port
);
90 /******************************************************************************
91 * base i2c "port" class implementation
92 *****************************************************************************/
95 _nouveau_i2c_port_fini(struct nouveau_object
*object
, bool suspend
)
97 struct nouveau_i2c_port
*port
= (void *)object
;
98 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
99 nv_ofuncs(pad
)->fini(nv_object(pad
), suspend
);
100 return nouveau_object_fini(&port
->base
, suspend
);
104 _nouveau_i2c_port_dtor(struct nouveau_object
*object
)
106 struct nouveau_i2c_port
*port
= (void *)object
;
107 i2c_del_adapter(&port
->adapter
);
108 nouveau_object_destroy(&port
->base
);
112 nouveau_i2c_port_create_(struct nouveau_object
*parent
,
113 struct nouveau_object
*engine
,
114 struct nouveau_oclass
*oclass
, u8 index
,
115 const struct i2c_algorithm
*algo
,
116 const struct nouveau_i2c_func
*func
,
117 int size
, void **pobject
)
119 struct nouveau_device
*device
= nv_device(engine
);
120 struct nouveau_i2c
*i2c
= (void *)engine
;
121 struct nouveau_i2c_port
*port
;
124 ret
= nouveau_object_create_(parent
, engine
, oclass
, 0, size
, pobject
);
129 snprintf(port
->adapter
.name
, sizeof(port
->adapter
.name
),
130 "nouveau-%s-%d", device
->name
, index
);
131 port
->adapter
.owner
= THIS_MODULE
;
132 port
->adapter
.dev
.parent
= nv_device_base(device
);
136 mutex_init(&port
->mutex
);
138 if ( algo
== &nouveau_i2c_bit_algo
&&
139 !nouveau_boolopt(device
->cfgopt
, "NvI2C", CSTMSEL
)) {
140 struct i2c_algo_bit_data
*bit
;
142 bit
= kzalloc(sizeof(*bit
), GFP_KERNEL
);
147 bit
->timeout
= usecs_to_jiffies(2200);
149 bit
->pre_xfer
= nouveau_i2c_pre_xfer
;
150 bit
->post_xfer
= nouveau_i2c_post_xfer
;
151 bit
->setsda
= nouveau_i2c_setsda
;
152 bit
->setscl
= nouveau_i2c_setscl
;
153 bit
->getsda
= nouveau_i2c_getsda
;
154 bit
->getscl
= nouveau_i2c_getscl
;
156 port
->adapter
.algo_data
= bit
;
157 ret
= i2c_bit_add_bus(&port
->adapter
);
159 port
->adapter
.algo_data
= port
;
160 port
->adapter
.algo
= algo
;
161 ret
= i2c_add_adapter(&port
->adapter
);
165 list_add_tail(&port
->head
, &i2c
->ports
);
169 /******************************************************************************
170 * base i2c subdev class implementation
171 *****************************************************************************/
173 static struct nouveau_i2c_port
*
174 nouveau_i2c_find(struct nouveau_i2c
*i2c
, u8 index
)
176 struct nouveau_bios
*bios
= nouveau_bios(i2c
);
177 struct nouveau_i2c_port
*port
;
179 if (index
== NV_I2C_DEFAULT(0) ||
180 index
== NV_I2C_DEFAULT(1)) {
181 u8 ver
, hdr
, cnt
, len
;
182 u16 i2c
= dcb_i2c_table(bios
, &ver
, &hdr
, &cnt
, &len
);
183 if (i2c
&& ver
>= 0x30) {
184 u8 auxidx
= nv_ro08(bios
, i2c
+ 4);
185 if (index
== NV_I2C_DEFAULT(0))
186 index
= (auxidx
& 0x0f) >> 0;
188 index
= (auxidx
& 0xf0) >> 4;
194 list_for_each_entry(port
, &i2c
->ports
, head
) {
195 if (port
->index
== index
)
202 static struct nouveau_i2c_port
*
203 nouveau_i2c_find_type(struct nouveau_i2c
*i2c
, u16 type
)
205 struct nouveau_i2c_port
*port
;
207 list_for_each_entry(port
, &i2c
->ports
, head
) {
208 if (nv_hclass(port
) == type
)
216 nouveau_i2c_release_pad(struct nouveau_i2c_port
*port
)
218 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
219 struct nouveau_i2c
*i2c
= nouveau_i2c(port
);
221 if (atomic_dec_and_test(&nv_object(pad
)->usecount
)) {
222 nv_ofuncs(pad
)->fini(nv_object(pad
), false);
223 wake_up_all(&i2c
->wait
);
228 nouveau_i2c_try_acquire_pad(struct nouveau_i2c_port
*port
)
230 struct nvkm_i2c_pad
*pad
= nvkm_i2c_pad(port
);
232 if (atomic_add_return(1, &nv_object(pad
)->usecount
) != 1) {
233 struct nouveau_object
*owner
= (void *)pad
->port
;
235 if (owner
== (void *)port
)
237 owner
= owner
->parent
;
239 nouveau_i2c_release_pad(port
);
244 nv_ofuncs(pad
)->init(nv_object(pad
));
249 nouveau_i2c_acquire_pad(struct nouveau_i2c_port
*port
, unsigned long timeout
)
251 struct nouveau_i2c
*i2c
= nouveau_i2c(port
);
254 if (wait_event_timeout(i2c
->wait
,
255 nouveau_i2c_try_acquire_pad(port
) == 0,
259 wait_event(i2c
->wait
, nouveau_i2c_try_acquire_pad(port
) == 0);
266 nouveau_i2c_release(struct nouveau_i2c_port
*port
)
267 __releases(pad
->mutex
)
269 nouveau_i2c(port
)->release_pad(port
);
270 mutex_unlock(&port
->mutex
);
274 nouveau_i2c_acquire(struct nouveau_i2c_port
*port
, unsigned long timeout
)
275 __acquires(pad
->mutex
)
278 mutex_lock(&port
->mutex
);
279 if ((ret
= nouveau_i2c(port
)->acquire_pad(port
, timeout
)))
280 mutex_unlock(&port
->mutex
);
285 nouveau_i2c_identify(struct nouveau_i2c
*i2c
, int index
, const char *what
,
286 struct nouveau_i2c_board_info
*info
,
287 bool (*match
)(struct nouveau_i2c_port
*,
288 struct i2c_board_info
*, void *), void *data
)
290 struct nouveau_i2c_port
*port
= nouveau_i2c_find(i2c
, index
);
294 nv_debug(i2c
, "no bus when probing %s on %d\n", what
, index
);
298 nv_debug(i2c
, "probing %ss on bus: %d\n", what
, port
->index
);
299 for (i
= 0; info
[i
].dev
.addr
; i
++) {
302 if ((port
->adapter
.algo
== &i2c_bit_algo
) &&
303 (info
[i
].udelay
!= 0)) {
304 struct i2c_algo_bit_data
*algo
= port
->adapter
.algo_data
;
305 nv_debug(i2c
, "using custom udelay %d instead of %d\n",
306 info
[i
].udelay
, algo
->udelay
);
307 orig_udelay
= algo
->udelay
;
308 algo
->udelay
= info
[i
].udelay
;
311 if (nv_probe_i2c(port
, info
[i
].dev
.addr
) &&
312 (!match
|| match(port
, &info
[i
].dev
, data
))) {
313 nv_info(i2c
, "detected %s: %s\n", what
,
319 struct i2c_algo_bit_data
*algo
= port
->adapter
.algo_data
;
320 algo
->udelay
= orig_udelay
;
324 nv_debug(i2c
, "no devices found.\n");
329 nouveau_i2c_intr_disable(struct nouveau_event
*event
, int type
, int index
)
331 struct nouveau_i2c
*i2c
= nouveau_i2c(event
->priv
);
332 struct nouveau_i2c_port
*port
= i2c
->find(i2c
, index
);
333 const struct nouveau_i2c_impl
*impl
= (void *)nv_object(i2c
)->oclass
;
334 if (port
&& port
->aux
>= 0)
335 impl
->aux_mask(i2c
, type
, 1 << port
->aux
, 0);
339 nouveau_i2c_intr_enable(struct nouveau_event
*event
, int type
, int index
)
341 struct nouveau_i2c
*i2c
= nouveau_i2c(event
->priv
);
342 struct nouveau_i2c_port
*port
= i2c
->find(i2c
, index
);
343 const struct nouveau_i2c_impl
*impl
= (void *)nv_object(i2c
)->oclass
;
344 if (port
&& port
->aux
>= 0)
345 impl
->aux_mask(i2c
, type
, 1 << port
->aux
, 1 << port
->aux
);
349 nouveau_i2c_intr(struct nouveau_subdev
*subdev
)
351 struct nouveau_i2c_impl
*impl
= (void *)nv_oclass(subdev
);
352 struct nouveau_i2c
*i2c
= nouveau_i2c(subdev
);
353 struct nouveau_i2c_port
*port
;
354 u32 hi
, lo
, rq
, tx
, e
;
356 if (impl
->aux_stat
) {
357 impl
->aux_stat(i2c
, &hi
, &lo
, &rq
, &tx
);
358 if (hi
|| lo
|| rq
|| tx
) {
359 list_for_each_entry(port
, &i2c
->ports
, head
) {
360 if (e
= 0, port
->aux
< 0)
363 if (hi
& (1 << port
->aux
)) e
|= NVKM_I2C_PLUG
;
364 if (lo
& (1 << port
->aux
)) e
|= NVKM_I2C_UNPLUG
;
365 if (rq
& (1 << port
->aux
)) e
|= NVKM_I2C_IRQ
;
366 if (tx
& (1 << port
->aux
)) e
|= NVKM_I2C_DONE
;
368 nouveau_event_trigger(i2c
->ntfy
, e
, port
->index
);
375 _nouveau_i2c_fini(struct nouveau_object
*object
, bool suspend
)
377 struct nouveau_i2c_impl
*impl
= (void *)nv_oclass(object
);
378 struct nouveau_i2c
*i2c
= (void *)object
;
379 struct nouveau_i2c_port
*port
;
383 list_for_each_entry(port
, &i2c
->ports
, head
) {
384 ret
= nv_ofuncs(port
)->fini(nv_object(port
), suspend
);
389 if ((mask
= (1 << impl
->aux
) - 1), impl
->aux_stat
) {
390 impl
->aux_mask(i2c
, NVKM_I2C_ANY
, mask
, 0);
391 impl
->aux_stat(i2c
, &mask
, &mask
, &mask
, &mask
);
394 return nouveau_subdev_fini(&i2c
->base
, suspend
);
396 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
397 nv_ofuncs(port
)->init(nv_object(port
));
404 _nouveau_i2c_init(struct nouveau_object
*object
)
406 struct nouveau_i2c
*i2c
= (void *)object
;
407 struct nouveau_i2c_port
*port
;
410 ret
= nouveau_subdev_init(&i2c
->base
);
412 list_for_each_entry(port
, &i2c
->ports
, head
) {
413 ret
= nv_ofuncs(port
)->init(nv_object(port
));
421 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
422 nv_ofuncs(port
)->fini(nv_object(port
), false);
429 _nouveau_i2c_dtor(struct nouveau_object
*object
)
431 struct nouveau_i2c
*i2c
= (void *)object
;
432 struct nouveau_i2c_port
*port
, *temp
;
434 nouveau_event_destroy(&i2c
->ntfy
);
436 list_for_each_entry_safe(port
, temp
, &i2c
->ports
, head
) {
437 nouveau_object_ref(NULL
, (struct nouveau_object
**)&port
);
440 nouveau_subdev_destroy(&i2c
->base
);
443 static struct nouveau_oclass
*
444 nouveau_i2c_extdev_sclass
[] = {
445 nouveau_anx9805_sclass
,
449 nouveau_i2c_create_(struct nouveau_object
*parent
,
450 struct nouveau_object
*engine
,
451 struct nouveau_oclass
*oclass
,
452 int length
, void **pobject
)
454 const struct nouveau_i2c_impl
*impl
= (void *)oclass
;
455 struct nouveau_bios
*bios
= nouveau_bios(parent
);
456 struct nouveau_i2c
*i2c
;
457 struct nouveau_object
*object
;
458 struct dcb_i2c_entry info
;
459 int ret
, i
, j
, index
= -1, pad
;
460 struct dcb_output outp
;
464 ret
= nouveau_subdev_create(parent
, engine
, oclass
, 0,
466 *pobject
= nv_object(i2c
);
470 nv_subdev(i2c
)->intr
= nouveau_i2c_intr
;
471 i2c
->find
= nouveau_i2c_find
;
472 i2c
->find_type
= nouveau_i2c_find_type
;
473 i2c
->acquire_pad
= nouveau_i2c_acquire_pad
;
474 i2c
->release_pad
= nouveau_i2c_release_pad
;
475 i2c
->acquire
= nouveau_i2c_acquire
;
476 i2c
->release
= nouveau_i2c_release
;
477 i2c
->identify
= nouveau_i2c_identify
;
478 init_waitqueue_head(&i2c
->wait
);
479 INIT_LIST_HEAD(&i2c
->ports
);
481 while (!dcb_i2c_parse(bios
, ++index
, &info
)) {
482 if (info
.type
== DCB_I2C_UNUSED
)
485 if (info
.share
!= DCB_I2C_UNUSED
) {
486 if (info
.type
== DCB_I2C_NVIO_AUX
)
490 oclass
= impl
->pad_s
;
492 pad
= 0x100 + info
.drive
;
493 oclass
= impl
->pad_x
;
496 ret
= nouveau_object_ctor(NULL
, *pobject
, oclass
,
501 oclass
= impl
->sclass
;
504 if (oclass
->handle
== info
.type
) {
505 ret
= nouveau_object_ctor(parent
, *pobject
,
509 } while (ret
&& (++oclass
)->handle
);
511 nouveau_object_ref(NULL
, &parent
);
514 /* in addition to the busses specified in the i2c table, there
515 * may be ddc/aux channels hiding behind external tmds/dp/etc
518 index
= ((index
+ 0x0f) / 0x10) * 0x10;
520 while ((data
= dcb_outp_parse(bios
, ++i
, &ver
, &hdr
, &outp
))) {
521 if (!outp
.location
|| !outp
.extdev
)
525 case DCB_OUTPUT_TMDS
:
526 info
.type
= NV_I2C_TYPE_EXTDDC(outp
.extdev
);
529 info
.type
= NV_I2C_TYPE_EXTAUX(outp
.extdev
);
537 while (ret
&& ++j
< ARRAY_SIZE(nouveau_i2c_extdev_sclass
)) {
538 parent
= nv_object(i2c
->find(i2c
, outp
.i2c_index
));
539 oclass
= nouveau_i2c_extdev_sclass
[j
];
541 if (oclass
->handle
!= info
.type
)
543 ret
= nouveau_object_ctor(parent
, *pobject
,
546 } while (ret
&& (++oclass
)->handle
);
550 ret
= nouveau_event_create(4, index
, &i2c
->ntfy
);
554 i2c
->ntfy
->priv
= i2c
;
555 i2c
->ntfy
->enable
= nouveau_i2c_intr_enable
;
556 i2c
->ntfy
->disable
= nouveau_i2c_intr_disable
;
561 _nouveau_i2c_ctor(struct nouveau_object
*parent
, struct nouveau_object
*engine
,
562 struct nouveau_oclass
*oclass
, void *data
, u32 size
,
563 struct nouveau_object
**pobject
)
565 struct nouveau_i2c
*i2c
;
568 ret
= nouveau_i2c_create(parent
, engine
, oclass
, &i2c
);
569 *pobject
= nv_object(i2c
);