MD: Add del_timer_sync to mddev_suspend (fix nasty panic)
[linux-flexiantxendom0-3.2.10.git] / drivers / gpu / drm / nouveau / nouveau_i2c.c
1 /*
2  * Copyright 2009 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
25 #include <linux/module.h>
26
27 #include "drmP.h"
28 #include "nouveau_drv.h"
29 #include "nouveau_i2c.h"
30 #include "nouveau_hw.h"
31
32 #define T_TIMEOUT  2200000
33 #define T_RISEFALL 1000
34 #define T_HOLD     5000
35
36 static void
37 i2c_drive_scl(void *data, int state)
38 {
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;
43                 else       val &= 0xdf;
44                 NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
45         } else
46         if (port->type == 4) {
47                 nv_mask(port->dev, port->drive, 0x2f, state ? 0x21 : 0x01);
48         } else
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);
53         }
54 }
55
56 static void
57 i2c_drive_sda(void *data, int state)
58 {
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;
63                 else       val &= 0xef;
64                 NVWriteVgaCrtc(port->dev, 0, port->drive, val | 0x01);
65         } else
66         if (port->type == 4) {
67                 nv_mask(port->dev, port->drive, 0x1f, state ? 0x11 : 0x01);
68         } else
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);
73         }
74 }
75
76 static int
77 i2c_sense_scl(void *data)
78 {
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);
83         } else
84         if (port->type == 4) {
85                 return !!(nv_rd32(port->dev, port->sense) & 0x00040000);
86         } else
87         if (port->type == 5) {
88                 if (dev_priv->card_type < NV_D0)
89                         return !!(nv_rd32(port->dev, port->sense) & 0x01);
90                 else
91                         return !!(nv_rd32(port->dev, port->sense) & 0x10);
92         }
93         return 0;
94 }
95
96 static int
97 i2c_sense_sda(void *data)
98 {
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);
103         } else
104         if (port->type == 4) {
105                 return !!(nv_rd32(port->dev, port->sense) & 0x00080000);
106         } else
107         if (port->type == 5) {
108                 if (dev_priv->card_type < NV_D0)
109                         return !!(nv_rd32(port->dev, port->sense) & 0x02);
110                 else
111                         return !!(nv_rd32(port->dev, port->sense) & 0x20);
112         }
113         return 0;
114 }
115
116 static void
117 i2c_delay(struct nouveau_i2c_chan *port, u32 nsec)
118 {
119         udelay((nsec + 500) / 1000);
120 }
121
122 static bool
123 i2c_raise_scl(struct nouveau_i2c_chan *port)
124 {
125         u32 timeout = T_TIMEOUT / T_RISEFALL;
126
127         i2c_drive_scl(port, 1);
128         do {
129                 i2c_delay(port, T_RISEFALL);
130         } while (!i2c_sense_scl(port) && --timeout);
131
132         return timeout != 0;
133 }
134
135 static int
136 i2c_start(struct nouveau_i2c_chan *port)
137 {
138         int ret = 0;
139
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))
146                         ret = -EBUSY;
147         }
148
149         i2c_drive_sda(port, 0);
150         i2c_delay(port, T_HOLD);
151         i2c_drive_scl(port, 0);
152         i2c_delay(port, T_HOLD);
153         return ret;
154 }
155
156 static void
157 i2c_stop(struct nouveau_i2c_chan *port)
158 {
159         i2c_drive_scl(port, 0);
160         i2c_drive_sda(port, 0);
161         i2c_delay(port, T_RISEFALL);
162
163         i2c_drive_scl(port, 1);
164         i2c_delay(port, T_HOLD);
165         i2c_drive_sda(port, 1);
166         i2c_delay(port, T_HOLD);
167 }
168
169 static int
170 i2c_bitw(struct nouveau_i2c_chan *port, int sda)
171 {
172         i2c_drive_sda(port, sda);
173         i2c_delay(port, T_RISEFALL);
174
175         if (!i2c_raise_scl(port))
176                 return -ETIMEDOUT;
177         i2c_delay(port, T_HOLD);
178
179         i2c_drive_scl(port, 0);
180         i2c_delay(port, T_HOLD);
181         return 0;
182 }
183
184 static int
185 i2c_bitr(struct nouveau_i2c_chan *port)
186 {
187         int sda;
188
189         i2c_drive_sda(port, 1);
190         i2c_delay(port, T_RISEFALL);
191
192         if (!i2c_raise_scl(port))
193                 return -ETIMEDOUT;
194         i2c_delay(port, T_HOLD);
195
196         sda = i2c_sense_sda(port);
197
198         i2c_drive_scl(port, 0);
199         i2c_delay(port, T_HOLD);
200         return sda;
201 }
202
203 static int
204 i2c_get_byte(struct nouveau_i2c_chan *port, u8 *byte, bool last)
205 {
206         int i, bit;
207
208         *byte = 0;
209         for (i = 7; i >= 0; i--) {
210                 bit = i2c_bitr(port);
211                 if (bit < 0)
212                         return bit;
213                 *byte |= bit << i;
214         }
215
216         return i2c_bitw(port, last ? 1 : 0);
217 }
218
219 static int
220 i2c_put_byte(struct nouveau_i2c_chan *port, u8 byte)
221 {
222         int i, ret;
223         for (i = 7; i >= 0; i--) {
224                 ret = i2c_bitw(port, !!(byte & (1 << i)));
225                 if (ret < 0)
226                         return ret;
227         }
228
229         ret = i2c_bitr(port);
230         if (ret == 1) /* nack */
231                 ret = -EIO;
232         return ret;
233 }
234
235 static int
236 i2c_addr(struct nouveau_i2c_chan *port, struct i2c_msg *msg)
237 {
238         u32 addr = msg->addr << 1;
239         if (msg->flags & I2C_M_RD)
240                 addr |= 1;
241         return i2c_put_byte(port, addr);
242 }
243
244 static int
245 i2c_bit_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
246 {
247         struct nouveau_i2c_chan *port = (struct nouveau_i2c_chan *)adap;
248         struct i2c_msg *msg = msgs;
249         int ret = 0, mcnt = num;
250
251         while (!ret && mcnt--) {
252                 u8 remaining = msg->len;
253                 u8 *ptr = msg->buf;
254
255                 ret = i2c_start(port);
256                 if (ret == 0)
257                         ret = i2c_addr(port, msg);
258
259                 if (msg->flags & I2C_M_RD) {
260                         while (!ret && remaining--)
261                                 ret = i2c_get_byte(port, ptr++, !remaining);
262                 } else {
263                         while (!ret && remaining--)
264                                 ret = i2c_put_byte(port, *ptr++);
265                 }
266
267                 msg++;
268         }
269
270         i2c_stop(port);
271         return (ret < 0) ? ret : num;
272 }
273
274 static u32
275 i2c_bit_func(struct i2c_adapter *adap)
276 {
277         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
278 }
279
280 const struct i2c_algorithm nouveau_i2c_bit_algo = {
281         .master_xfer = i2c_bit_xfer,
282         .functionality = i2c_bit_func
283 };
284
285 static const uint32_t nv50_i2c_port[] = {
286         0x00e138, 0x00e150, 0x00e168, 0x00e180,
287         0x00e254, 0x00e274, 0x00e764, 0x00e780,
288         0x00e79c, 0x00e7b8
289 };
290
291 static u8 *
292 i2c_table(struct drm_device *dev, u8 *version)
293 {
294         u8 *dcb = dcb_table(dev), *i2c = NULL;
295         if (dcb) {
296                 if (dcb[0] >= 0x15)
297                         i2c = ROMPTR(dev, dcb[2]);
298                 if (dcb[0] >= 0x30)
299                         i2c = ROMPTR(dev, dcb[4]);
300         }
301
302         /* early revisions had no version number, use dcb version */
303         if (i2c) {
304                 *version = dcb[0];
305                 if (*version >= 0x30)
306                         *version = i2c[0];
307         }
308
309         return i2c;
310 }
311
312 int
313 nouveau_i2c_init(struct drm_device *dev)
314 {
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 version = 0x00, entries, recordlen;
319         u8 *i2c, *entry, legacy[2][4] = {};
320         int ret, i;
321
322         INIT_LIST_HEAD(&dev_priv->i2c_ports);
323
324         i2c = i2c_table(dev, &version);
325         if (!i2c) {
326                 u8 *bmp = &bios->data[bios->offset];
327                 if (bios->type != NVBIOS_BMP)
328                         return -ENODEV;
329
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;
334
335                 /* BMP (from v4.0) has i2c info in the structure, it's in a
336                  * fixed location on earlier VBIOS
337                  */
338                 if (bmp[5] < 4)
339                         i2c = &bios->data[0x48];
340                 else
341                         i2c = &bmp[0x36];
342
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];
347         }
348
349         if (version >= 0x30) {
350                 entry     = i2c[1] + i2c;
351                 entries   = i2c[2];
352                 recordlen = i2c[3];
353         } else
354         if (version) {
355                 entry     = i2c;
356                 entries   = 16;
357                 recordlen = 4;
358         } else {
359                 entry     = legacy[0];
360                 entries   = 2;
361                 recordlen = 4;
362         }
363
364         for (i = 0; i < entries; i++, entry += recordlen) {
365                 port = kzalloc(sizeof(*port), GFP_KERNEL);
366                 if (port == NULL) {
367                         nouveau_i2c_fini(dev);
368                         return -ENOMEM;
369                 }
370
371                 port->type = entry[3];
372                 if (version < 0x30) {
373                         port->type &= 0x07;
374                         if (port->type == 0x07)
375                                 port->type = 0xff;
376                 }
377
378                 if (port->type == 0xff) {
379                         kfree(port);
380                         continue;
381                 }
382
383                 switch (port->type) {
384                 case 0: /* NV04:NV50 */
385                         port->drive = entry[0];
386                         port->sense = entry[1];
387                         port->adapter.algo = &nouveau_i2c_bit_algo;
388                         break;
389                 case 4: /* NV4E */
390                         port->drive = 0x600800 + entry[1];
391                         port->sense = port->drive;
392                         port->adapter.algo = &nouveau_i2c_bit_algo;
393                         break;
394                 case 5: /* NV50- */
395                         port->drive = entry[0] & 0x0f;
396                         if (dev_priv->card_type < NV_D0) {
397                                 if (port->drive >= ARRAY_SIZE(nv50_i2c_port))
398                                         break;
399                                 port->drive = nv50_i2c_port[port->drive];
400                                 port->sense = port->drive;
401                         } else {
402                                 port->drive = 0x00d014 + (port->drive * 0x20);
403                                 port->sense = port->drive;
404                         }
405                         port->adapter.algo = &nouveau_i2c_bit_algo;
406                         break;
407                 case 6: /* NV50- DP AUX */
408                         port->drive = entry[0];
409                         port->sense = port->drive;
410                         port->adapter.algo = &nouveau_dp_i2c_algo;
411                         break;
412                 default:
413                         break;
414                 }
415
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);
419                         kfree(port);
420                         continue;
421                 }
422
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;
427                 port->dev = dev;
428                 port->index = i;
429                 port->dcb = ROM32(entry[0]);
430                 i2c_set_adapdata(&port->adapter, i2c);
431
432                 ret = i2c_add_adapter(&port->adapter);
433                 if (ret) {
434                         NV_ERROR(dev, "I2C%d: failed register: %d\n", i, ret);
435                         kfree(port);
436                         continue;
437                 }
438
439                 list_add_tail(&port->head, &dev_priv->i2c_ports);
440         }
441
442         return 0;
443 }
444
445 void
446 nouveau_i2c_fini(struct drm_device *dev)
447 {
448         struct drm_nouveau_private *dev_priv = dev->dev_private;
449         struct nouveau_i2c_chan *port, *tmp;
450
451         list_for_each_entry_safe(port, tmp, &dev_priv->i2c_ports, head) {
452                 i2c_del_adapter(&port->adapter);
453                 kfree(port);
454         }
455 }
456
457 struct nouveau_i2c_chan *
458 nouveau_i2c_find(struct drm_device *dev, u8 index)
459 {
460         struct drm_nouveau_private *dev_priv = dev->dev_private;
461         struct nouveau_i2c_chan *port;
462
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);
469                         else
470                                 index = (i2c[4] & 0xf0) >> 4;
471                 } else {
472                         index = 2;
473                 }
474         }
475
476         list_for_each_entry(port, &dev_priv->i2c_ports, head) {
477                 if (port->index == index)
478                         break;
479         }
480
481         if (&port->head == &dev_priv->i2c_ports)
482                 return NULL;
483
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;
488                         val  = 0x2002;
489                 } else {
490                         reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
491                         val  = 0xe001;
492                 }
493
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);
498         }
499
500         return port;
501 }
502
503 bool
504 nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
505 {
506         uint8_t buf[] = { 0 };
507         struct i2c_msg msgs[] = {
508                 {
509                         .addr = addr,
510                         .flags = 0,
511                         .len = 1,
512                         .buf = buf,
513                 },
514                 {
515                         .addr = addr,
516                         .flags = I2C_M_RD,
517                         .len = 1,
518                         .buf = buf,
519                 }
520         };
521
522         return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
523 }
524
525 int
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 *),
530                      int index)
531 {
532         struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
533         int i;
534
535         if (!i2c) {
536                 NV_DEBUG(dev, "No bus when probing %s on %d\n", what, index);
537                 return -ENODEV;
538         }
539
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);
545                         return i;
546                 }
547         }
548
549         NV_DEBUG(dev, "No devices found.\n");
550         return -ENODEV;
551 }