2 * Copyright 2009 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 <linux/module.h>
28 #include "nouveau_drv.h"
29 #include "nouveau_i2c.h"
30 #include "nouveau_hw.h"
32 #define T_TIMEOUT 2200000
33 #define T_RISEFALL 1000
37 i2c_drive_scl(void *data
, int state
)
39 struct nouveau_i2c_chan
*port
= data
;
40 if (port
->type
== 0) {
41 u8 val
= NVReadVgaCrtc(port
->dev
, 0, port
->drive
);
42 if (state
) val
|= 0x20;
44 NVWriteVgaCrtc(port
->dev
, 0, port
->drive
, val
| 0x01);
46 if (port
->type
== 4) {
47 nv_mask(port
->dev
, port
->drive
, 0x2f, state
? 0x21 : 0x01);
49 if (port
->type
== 5) {
50 if (state
) port
->state
|= 0x01;
51 else port
->state
&= 0xfe;
52 nv_wr32(port
->dev
, port
->drive
, 4 | port
->state
);
57 i2c_drive_sda(void *data
, int state
)
59 struct nouveau_i2c_chan
*port
= data
;
60 if (port
->type
== 0) {
61 u8 val
= NVReadVgaCrtc(port
->dev
, 0, port
->drive
);
62 if (state
) val
|= 0x10;
64 NVWriteVgaCrtc(port
->dev
, 0, port
->drive
, val
| 0x01);
66 if (port
->type
== 4) {
67 nv_mask(port
->dev
, port
->drive
, 0x1f, state
? 0x11 : 0x01);
69 if (port
->type
== 5) {
70 if (state
) port
->state
|= 0x02;
71 else port
->state
&= 0xfd;
72 nv_wr32(port
->dev
, port
->drive
, 4 | port
->state
);
77 i2c_sense_scl(void *data
)
79 struct nouveau_i2c_chan
*port
= data
;
80 struct drm_nouveau_private
*dev_priv
= port
->dev
->dev_private
;
81 if (port
->type
== 0) {
82 return !!(NVReadVgaCrtc(port
->dev
, 0, port
->sense
) & 0x04);
84 if (port
->type
== 4) {
85 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x00040000);
87 if (port
->type
== 5) {
88 if (dev_priv
->card_type
< NV_D0
)
89 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x01);
91 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x10);
97 i2c_sense_sda(void *data
)
99 struct nouveau_i2c_chan
*port
= data
;
100 struct drm_nouveau_private
*dev_priv
= port
->dev
->dev_private
;
101 if (port
->type
== 0) {
102 return !!(NVReadVgaCrtc(port
->dev
, 0, port
->sense
) & 0x08);
104 if (port
->type
== 4) {
105 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x00080000);
107 if (port
->type
== 5) {
108 if (dev_priv
->card_type
< NV_D0
)
109 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x02);
111 return !!(nv_rd32(port
->dev
, port
->sense
) & 0x20);
117 i2c_delay(struct nouveau_i2c_chan
*port
, u32 nsec
)
119 udelay((nsec
+ 500) / 1000);
123 i2c_raise_scl(struct nouveau_i2c_chan
*port
)
125 u32 timeout
= T_TIMEOUT
/ T_RISEFALL
;
127 i2c_drive_scl(port
, 1);
129 i2c_delay(port
, T_RISEFALL
);
130 } while (!i2c_sense_scl(port
) && --timeout
);
136 i2c_start(struct nouveau_i2c_chan
*port
)
140 port
->state
= i2c_sense_scl(port
);
141 port
->state
|= i2c_sense_sda(port
) << 1;
142 if (port
->state
!= 3) {
143 i2c_drive_scl(port
, 0);
144 i2c_drive_sda(port
, 1);
145 if (!i2c_raise_scl(port
))
149 i2c_drive_sda(port
, 0);
150 i2c_delay(port
, T_HOLD
);
151 i2c_drive_scl(port
, 0);
152 i2c_delay(port
, T_HOLD
);
157 i2c_stop(struct nouveau_i2c_chan
*port
)
159 i2c_drive_scl(port
, 0);
160 i2c_drive_sda(port
, 0);
161 i2c_delay(port
, T_RISEFALL
);
163 i2c_drive_scl(port
, 1);
164 i2c_delay(port
, T_HOLD
);
165 i2c_drive_sda(port
, 1);
166 i2c_delay(port
, T_HOLD
);
170 i2c_bitw(struct nouveau_i2c_chan
*port
, int sda
)
172 i2c_drive_sda(port
, sda
);
173 i2c_delay(port
, T_RISEFALL
);
175 if (!i2c_raise_scl(port
))
177 i2c_delay(port
, T_HOLD
);
179 i2c_drive_scl(port
, 0);
180 i2c_delay(port
, T_HOLD
);
185 i2c_bitr(struct nouveau_i2c_chan
*port
)
189 i2c_drive_sda(port
, 1);
190 i2c_delay(port
, T_RISEFALL
);
192 if (!i2c_raise_scl(port
))
194 i2c_delay(port
, T_HOLD
);
196 sda
= i2c_sense_sda(port
);
198 i2c_drive_scl(port
, 0);
199 i2c_delay(port
, T_HOLD
);
204 i2c_get_byte(struct nouveau_i2c_chan
*port
, u8
*byte
, bool last
)
209 for (i
= 7; i
>= 0; i
--) {
210 bit
= i2c_bitr(port
);
216 return i2c_bitw(port
, last
? 1 : 0);
220 i2c_put_byte(struct nouveau_i2c_chan
*port
, u8 byte
)
223 for (i
= 7; i
>= 0; i
--) {
224 ret
= i2c_bitw(port
, !!(byte
& (1 << i
)));
229 ret
= i2c_bitr(port
);
230 if (ret
== 1) /* nack */
236 i2c_addr(struct nouveau_i2c_chan
*port
, struct i2c_msg
*msg
)
238 u32 addr
= msg
->addr
<< 1;
239 if (msg
->flags
& I2C_M_RD
)
241 return i2c_put_byte(port
, addr
);
245 i2c_bit_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
247 struct nouveau_i2c_chan
*port
= (struct nouveau_i2c_chan
*)adap
;
248 struct i2c_msg
*msg
= msgs
;
249 int ret
= 0, mcnt
= num
;
251 while (!ret
&& mcnt
--) {
252 u8 remaining
= msg
->len
;
255 ret
= i2c_start(port
);
257 ret
= i2c_addr(port
, msg
);
259 if (msg
->flags
& I2C_M_RD
) {
260 while (!ret
&& remaining
--)
261 ret
= i2c_get_byte(port
, ptr
++, !remaining
);
263 while (!ret
&& remaining
--)
264 ret
= i2c_put_byte(port
, *ptr
++);
271 return (ret
< 0) ? ret
: num
;
275 i2c_bit_func(struct i2c_adapter
*adap
)
277 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
280 const struct i2c_algorithm i2c_bit_algo
= {
281 .master_xfer
= i2c_bit_xfer
,
282 .functionality
= i2c_bit_func
285 static const uint32_t nv50_i2c_port
[] = {
286 0x00e138, 0x00e150, 0x00e168, 0x00e180,
287 0x00e254, 0x00e274, 0x00e764, 0x00e780,
292 i2c_table(struct drm_device
*dev
, u8
*version
)
294 u8
*dcb
= dcb_table(dev
), *i2c
= NULL
;
297 i2c
= ROMPTR(dev
, dcb
[2]);
299 i2c
= ROMPTR(dev
, dcb
[4]);
302 /* early revisions had no version number, use dcb version */
305 if (*version
>= 0x30)
313 nouveau_i2c_init(struct drm_device
*dev
)
315 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
316 struct nvbios
*bios
= &dev_priv
->vbios
;
317 struct nouveau_i2c_chan
*port
;
318 u8
*i2c
, *entry
, legacy
[2][4] = {};
319 u8 version
, entries
, recordlen
;
322 INIT_LIST_HEAD(&dev_priv
->i2c_ports
);
324 i2c
= i2c_table(dev
, &version
);
326 u8
*bmp
= &bios
->data
[bios
->offset
];
327 if (bios
->type
!= NVBIOS_BMP
)
330 legacy
[0][0] = NV_CIO_CRE_DDC_WR__INDEX
;
331 legacy
[0][1] = NV_CIO_CRE_DDC_STATUS__INDEX
;
332 legacy
[1][0] = NV_CIO_CRE_DDC0_WR__INDEX
;
333 legacy
[1][1] = NV_CIO_CRE_DDC0_STATUS__INDEX
;
335 /* BMP (from v4.0) has i2c info in the structure, it's in a
336 * fixed location on earlier VBIOS
339 i2c
= &bios
->data
[0x48];
343 if (i2c
[4]) legacy
[0][0] = i2c
[4];
344 if (i2c
[5]) legacy
[0][1] = i2c
[5];
345 if (i2c
[6]) legacy
[1][0] = i2c
[6];
346 if (i2c
[7]) legacy
[1][1] = i2c
[7];
349 if (i2c
&& version
>= 0x30) {
350 entry
= i2c
[1] + i2c
;
364 for (i
= 0; i
< entries
; i
++, entry
+= recordlen
) {
365 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
367 nouveau_i2c_fini(dev
);
371 port
->type
= entry
[3];
372 if (version
< 0x30) {
374 if (port
->type
== 0x07)
378 if (port
->type
== 0xff) {
383 switch (port
->type
) {
384 case 0: /* NV04:NV50 */
385 port
->drive
= entry
[0];
386 port
->sense
= entry
[1];
387 port
->adapter
.algo
= &i2c_bit_algo
;
390 port
->drive
= 0x600800 + entry
[1];
391 port
->sense
= port
->drive
;
392 port
->adapter
.algo
= &i2c_bit_algo
;
395 port
->drive
= entry
[0] & 0x0f;
396 if (dev_priv
->card_type
< NV_D0
) {
397 if (port
->drive
>= ARRAY_SIZE(nv50_i2c_port
))
399 port
->drive
= nv50_i2c_port
[port
->drive
];
400 port
->sense
= port
->drive
;
402 port
->drive
= 0x00d014 + (port
->drive
* 0x20);
403 port
->sense
= port
->drive
;
405 port
->adapter
.algo
= &i2c_bit_algo
;
407 case 6: /* NV50- DP AUX */
408 port
->drive
= entry
[0];
409 port
->sense
= port
->drive
;
410 port
->adapter
.algo
= &nouveau_dp_i2c_algo
;
416 if (!port
->adapter
.algo
) {
417 NV_ERROR(dev
, "I2C%d: type %d index %x/%x unknown\n",
418 i
, port
->type
, port
->drive
, port
->sense
);
423 snprintf(port
->adapter
.name
, sizeof(port
->adapter
.name
),
424 "nouveau-%s-%d", pci_name(dev
->pdev
), i
);
425 port
->adapter
.owner
= THIS_MODULE
;
426 port
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
429 port
->dcb
= ROM32(entry
[0]);
430 i2c_set_adapdata(&port
->adapter
, i2c
);
432 ret
= i2c_add_adapter(&port
->adapter
);
434 NV_ERROR(dev
, "I2C%d: failed register: %d\n", i
, ret
);
439 list_add_tail(&port
->head
, &dev_priv
->i2c_ports
);
446 nouveau_i2c_fini(struct drm_device
*dev
)
448 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
449 struct nouveau_i2c_chan
*port
, *tmp
;
451 list_for_each_entry_safe(port
, tmp
, &dev_priv
->i2c_ports
, head
) {
452 i2c_del_adapter(&port
->adapter
);
457 struct nouveau_i2c_chan
*
458 nouveau_i2c_find(struct drm_device
*dev
, u8 index
)
460 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
461 struct nouveau_i2c_chan
*port
;
463 if (index
== NV_I2C_DEFAULT(0) ||
464 index
== NV_I2C_DEFAULT(1)) {
465 u8 version
, *i2c
= i2c_table(dev
, &version
);
466 if (i2c
&& version
>= 0x30) {
467 if (index
== NV_I2C_DEFAULT(0))
468 index
= (i2c
[4] & 0x0f);
470 index
= (i2c
[4] & 0xf0) >> 4;
476 list_for_each_entry(port
, &dev_priv
->i2c_ports
, head
) {
477 if (port
->index
== index
)
481 if (&port
->head
== &dev_priv
->i2c_ports
)
484 if (dev_priv
->card_type
>= NV_50
&& (port
->dcb
& 0x00000100)) {
485 u32 reg
= 0x00e500, val
;
486 if (port
->type
== 6) {
487 reg
+= port
->drive
* 0x50;
490 reg
+= ((port
->dcb
& 0x1e00) >> 9) * 0x50;
494 /* nfi, but neither auxch or i2c work if it's 1 */
495 nv_mask(dev
, reg
+ 0x0c, 0x00000001, 0x00000000);
496 /* nfi, but switches auxch vs normal i2c */
497 nv_mask(dev
, reg
+ 0x00, 0x0000f003, val
);
504 nouveau_probe_i2c_addr(struct nouveau_i2c_chan
*i2c
, int addr
)
506 uint8_t buf
[] = { 0 };
507 struct i2c_msg msgs
[] = {
522 return i2c_transfer(&i2c
->adapter
, msgs
, 2) == 2;
526 nouveau_i2c_identify(struct drm_device
*dev
, const char *what
,
527 struct i2c_board_info
*info
,
528 bool (*match
)(struct nouveau_i2c_chan
*,
529 struct i2c_board_info
*),
532 struct nouveau_i2c_chan
*i2c
= nouveau_i2c_find(dev
, index
);
536 NV_DEBUG(dev
, "No bus when probing %s on %d\n", what
, index
);
540 NV_DEBUG(dev
, "Probing %ss on I2C bus: %d\n", what
, i2c
->index
);
541 for (i
= 0; info
[i
].addr
; i
++) {
542 if (nouveau_probe_i2c_addr(i2c
, info
[i
].addr
) &&
543 (!match
|| match(i2c
, &info
[i
]))) {
544 NV_INFO(dev
, "Detected %s: %s\n", what
, info
[i
].type
);
549 NV_DEBUG(dev
, "No devices found.\n");