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"
33 i2c_drive_scl(void *data, int state)
35 struct nouveau_i2c_chan *port = data;
36 if (port->type == 0) {
37 u8 val = NVReadVgaCrtc(port->dev, 0, port->drive);
38 if (state) val |= 0x20;
40 NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
42 if (port->type == 4) {
43 nv_mask(port->dev, port->drive, 0x2f, state ? 0x21 : 0x01);
45 if (port->type == 5) {
46 if (state) port->state |= 0x01;
47 else port->state &= 0xfe;
48 nv_wr32(port->dev, port->drive, 4 | port->state);
53 i2c_drive_sda(void *data, int state)
55 struct nouveau_i2c_chan *port = data;
56 if (port->type == 0) {
57 u8 val = NVReadVgaCrtc(port->dev, 0, port->drive);
58 if (state) val |= 0x10;
60 NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
62 if (port->type == 4) {
63 nv_mask(port->dev, port->drive, 0x1f, state ? 0x11 : 0x01);
65 if (port->type == 5) {
66 if (state) port->state |= 0x02;
67 else port->state &= 0xfd;
68 nv_wr32(port->dev, port->drive, 4 | port->state);
73 i2c_sense_scl(void *data)
75 struct nouveau_i2c_chan *port = data;
76 struct drm_nouveau_private *dev_priv = port->dev->dev_private;
77 if (port->type == 0) {
78 return !!(NVReadVgaCrtc(port->dev, 0, port->sense) & 0x04);
80 if (port->type == 4) {
81 return !!(nv_rd32(port->dev, port->sense) & 0x00040000);
83 if (port->type == 5) {
84 if (dev_priv->card_type < NV_D0)
85 return !!(nv_rd32(port->dev, port->sense) & 0x01);
87 return !!(nv_rd32(port->dev, port->sense) & 0x10);
93 i2c_sense_sda(void *data)
95 struct nouveau_i2c_chan *port = data;
96 struct drm_nouveau_private *dev_priv = port->dev->dev_private;
97 if (port->type == 0) {
98 return !!(NVReadVgaCrtc(port->dev, 0, port->sense) & 0x08);
100 if (port->type == 4) {
101 return !!(nv_rd32(port->dev, port->sense) & 0x00080000);
103 if (port->type == 5) {
104 if (dev_priv->card_type < NV_D0)
105 return !!(nv_rd32(port->dev, port->sense) & 0x02);
107 return !!(nv_rd32(port->dev, port->sense) & 0x20);
112 static const uint32_t nv50_i2c_port[] = {
113 0x00e138, 0x00e150, 0x00e168, 0x00e180,
114 0x00e254, 0x00e274, 0x00e764, 0x00e780,
119 i2c_table(struct drm_device *dev, u8 *version)
121 u8 *dcb = dcb_table(dev), *i2c = NULL;
124 i2c = ROMPTR(dev, dcb[2]);
126 i2c = ROMPTR(dev, dcb[4]);
129 /* early revisions had no version number, use dcb version */
132 if (*version >= 0x30)
140 nouveau_i2c_init(struct drm_device *dev)
142 struct drm_nouveau_private *dev_priv = dev->dev_private;
143 struct nvbios *bios = &dev_priv->vbios;
144 struct nouveau_i2c_chan *port;
145 u8 version = 0x00, entries, recordlen;
146 u8 *i2c, *entry, legacy[2][4] = {};
149 INIT_LIST_HEAD(&dev_priv->i2c_ports);
151 i2c = i2c_table(dev, &version);
153 u8 *bmp = &bios->data[bios->offset];
154 if (bios->type != NVBIOS_BMP)
157 legacy[0][0] = NV_CIO_CRE_DDC_WR__INDEX;
158 legacy[0][1] = NV_CIO_CRE_DDC_STATUS__INDEX;
159 legacy[1][0] = NV_CIO_CRE_DDC0_WR__INDEX;
160 legacy[1][1] = NV_CIO_CRE_DDC0_STATUS__INDEX;
162 /* BMP (from v4.0) has i2c info in the structure, it's in a
163 * fixed location on earlier VBIOS
166 i2c = &bios->data[0x48];
170 if (i2c[4]) legacy[0][0] = i2c[4];
171 if (i2c[5]) legacy[0][1] = i2c[5];
172 if (i2c[6]) legacy[1][0] = i2c[6];
173 if (i2c[7]) legacy[1][1] = i2c[7];
176 if (version >= 0x30) {
177 entry = i2c[1] + i2c;
191 for (i = 0; i < entries; i++, entry += recordlen) {
192 port = kzalloc(sizeof(*port), GFP_KERNEL);
194 nouveau_i2c_fini(dev);
198 port->type = entry[3];
199 if (version < 0x30) {
201 if (port->type == 0x07)
205 if (port->type == 0xff) {
210 switch (port->type) {
211 case 0: /* NV04:NV50 */
212 port->drive = entry[0];
213 port->sense = entry[1];
216 port->drive = 0x600800 + entry[1];
217 port->sense = port->drive;
220 port->drive = entry[0] & 0x0f;
221 if (dev_priv->card_type < NV_D0) {
222 if (port->drive >= ARRAY_SIZE(nv50_i2c_port))
224 port->drive = nv50_i2c_port[port->drive];
225 port->sense = port->drive;
227 port->drive = 0x00d014 + (port->drive * 0x20);
228 port->sense = port->drive;
231 case 6: /* NV50- DP AUX */
232 port->drive = entry[0];
233 port->sense = port->drive;
234 port->adapter.algo = &nouveau_dp_i2c_algo;
240 if (!port->adapter.algo && !port->drive) {
241 NV_ERROR(dev, "I2C%d: type %d index %x/%x unknown\n",
242 i, port->type, port->drive, port->sense);
247 snprintf(port->adapter.name, sizeof(port->adapter.name),
248 "nouveau-%s-%d", pci_name(dev->pdev), i);
249 port->adapter.owner = THIS_MODULE;
250 port->adapter.dev.parent = &dev->pdev->dev;
253 port->dcb = ROM32(entry[0]);
254 i2c_set_adapdata(&port->adapter, i2c);
256 if (port->adapter.algo != &nouveau_dp_i2c_algo) {
257 port->adapter.algo_data = &port->bit;
258 port->bit.udelay = 10;
259 port->bit.timeout = usecs_to_jiffies(2200);
260 port->bit.data = port;
261 port->bit.setsda = i2c_drive_sda;
262 port->bit.setscl = i2c_drive_scl;
263 port->bit.getsda = i2c_sense_sda;
264 port->bit.getscl = i2c_sense_scl;
266 i2c_drive_scl(port, 0);
267 i2c_drive_sda(port, 1);
268 i2c_drive_scl(port, 1);
270 ret = i2c_bit_add_bus(&port->adapter);
272 port->adapter.algo = &nouveau_dp_i2c_algo;
273 ret = i2c_add_adapter(&port->adapter);
277 NV_ERROR(dev, "I2C%d: failed register: %d\n", i, ret);
282 list_add_tail(&port->head, &dev_priv->i2c_ports);
289 nouveau_i2c_fini(struct drm_device *dev)
291 struct drm_nouveau_private *dev_priv = dev->dev_private;
292 struct nouveau_i2c_chan *port, *tmp;
294 list_for_each_entry_safe(port, tmp, &dev_priv->i2c_ports, head) {
295 i2c_del_adapter(&port->adapter);
300 struct nouveau_i2c_chan *
301 nouveau_i2c_find(struct drm_device *dev, u8 index)
303 struct drm_nouveau_private *dev_priv = dev->dev_private;
304 struct nouveau_i2c_chan *port;
306 if (index == NV_I2C_DEFAULT(0) ||
307 index == NV_I2C_DEFAULT(1)) {
308 u8 version, *i2c = i2c_table(dev, &version);
309 if (i2c && version >= 0x30) {
310 if (index == NV_I2C_DEFAULT(0))
311 index = (i2c[4] & 0x0f);
313 index = (i2c[4] & 0xf0) >> 4;
319 list_for_each_entry(port, &dev_priv->i2c_ports, head) {
320 if (port->index == index)
324 if (&port->head == &dev_priv->i2c_ports)
327 if (dev_priv->card_type >= NV_50 && (port->dcb & 0x00000100)) {
328 u32 reg = 0x00e500, val;
329 if (port->type == 6) {
330 reg += port->drive * 0x50;
333 reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
337 /* nfi, but neither auxch or i2c work if it's 1 */
338 nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
339 /* nfi, but switches auxch vs normal i2c */
340 nv_mask(dev, reg + 0x00, 0x0000f003, val);
347 nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
349 uint8_t buf[] = { 0 };
350 struct i2c_msg msgs[] = {
365 return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
369 nouveau_i2c_identify(struct drm_device *dev, const char *what,
370 struct i2c_board_info *info,
371 bool (*match)(struct nouveau_i2c_chan *,
372 struct i2c_board_info *),
375 struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
379 NV_DEBUG(dev, "No bus when probing %s on %d\n", what, index);
383 NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, i2c->index);
384 for (i = 0; info[i].addr; i++) {
385 if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
386 (!match || match(i2c, &info[i]))) {
387 NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
392 NV_DEBUG(dev, "No devices found.\n");