Merge branch 'stable/vmalloc-3.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.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 "drmP.h"
26 #include "nouveau_drv.h"
27 #include "nouveau_i2c.h"
28 #include "nouveau_hw.h"
29
30 static void
31 nv04_i2c_setscl(void *data, int state)
32 {
33         struct nouveau_i2c_chan *i2c = data;
34         struct drm_device *dev = i2c->dev;
35         uint8_t val;
36
37         val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
38         NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
39 }
40
41 static void
42 nv04_i2c_setsda(void *data, int state)
43 {
44         struct nouveau_i2c_chan *i2c = data;
45         struct drm_device *dev = i2c->dev;
46         uint8_t val;
47
48         val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
49         NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
50 }
51
52 static int
53 nv04_i2c_getscl(void *data)
54 {
55         struct nouveau_i2c_chan *i2c = data;
56         struct drm_device *dev = i2c->dev;
57
58         return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 4);
59 }
60
61 static int
62 nv04_i2c_getsda(void *data)
63 {
64         struct nouveau_i2c_chan *i2c = data;
65         struct drm_device *dev = i2c->dev;
66
67         return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 8);
68 }
69
70 static void
71 nv4e_i2c_setscl(void *data, int state)
72 {
73         struct nouveau_i2c_chan *i2c = data;
74         struct drm_device *dev = i2c->dev;
75         uint8_t val;
76
77         val = (nv_rd32(dev, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
78         nv_wr32(dev, i2c->wr, val | 0x01);
79 }
80
81 static void
82 nv4e_i2c_setsda(void *data, int state)
83 {
84         struct nouveau_i2c_chan *i2c = data;
85         struct drm_device *dev = i2c->dev;
86         uint8_t val;
87
88         val = (nv_rd32(dev, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
89         nv_wr32(dev, i2c->wr, val | 0x01);
90 }
91
92 static int
93 nv4e_i2c_getscl(void *data)
94 {
95         struct nouveau_i2c_chan *i2c = data;
96         struct drm_device *dev = i2c->dev;
97
98         return !!((nv_rd32(dev, i2c->rd) >> 16) & 4);
99 }
100
101 static int
102 nv4e_i2c_getsda(void *data)
103 {
104         struct nouveau_i2c_chan *i2c = data;
105         struct drm_device *dev = i2c->dev;
106
107         return !!((nv_rd32(dev, i2c->rd) >> 16) & 8);
108 }
109
110 static const uint32_t nv50_i2c_port[] = {
111         0x00e138, 0x00e150, 0x00e168, 0x00e180,
112         0x00e254, 0x00e274, 0x00e764, 0x00e780,
113         0x00e79c, 0x00e7b8
114 };
115 #define NV50_I2C_PORTS ARRAY_SIZE(nv50_i2c_port)
116
117 static int
118 nv50_i2c_getscl(void *data)
119 {
120         struct nouveau_i2c_chan *i2c = data;
121         struct drm_device *dev = i2c->dev;
122
123         return !!(nv_rd32(dev, i2c->rd) & 1);
124 }
125
126
127 static int
128 nv50_i2c_getsda(void *data)
129 {
130         struct nouveau_i2c_chan *i2c = data;
131         struct drm_device *dev = i2c->dev;
132
133         return !!(nv_rd32(dev, i2c->rd) & 2);
134 }
135
136 static void
137 nv50_i2c_setscl(void *data, int state)
138 {
139         struct nouveau_i2c_chan *i2c = data;
140
141         nv_wr32(i2c->dev, i2c->wr, 4 | (i2c->data ? 2 : 0) | (state ? 1 : 0));
142 }
143
144 static void
145 nv50_i2c_setsda(void *data, int state)
146 {
147         struct nouveau_i2c_chan *i2c = data;
148
149         nv_mask(i2c->dev, i2c->wr, 0x00000006, 4 | (state ? 2 : 0));
150         i2c->data = state;
151 }
152
153 static int
154 nvd0_i2c_getscl(void *data)
155 {
156         struct nouveau_i2c_chan *i2c = data;
157         return !!(nv_rd32(i2c->dev, i2c->rd) & 0x10);
158 }
159
160 static int
161 nvd0_i2c_getsda(void *data)
162 {
163         struct nouveau_i2c_chan *i2c = data;
164         return !!(nv_rd32(i2c->dev, i2c->rd) & 0x20);
165 }
166
167 int
168 nouveau_i2c_init(struct drm_device *dev, struct dcb_i2c_entry *entry, int index)
169 {
170         struct drm_nouveau_private *dev_priv = dev->dev_private;
171         struct nouveau_i2c_chan *i2c;
172         int ret;
173
174         if (entry->chan)
175                 return -EEXIST;
176
177         if (dev_priv->card_type >= NV_50 &&
178             dev_priv->card_type <= NV_C0 && entry->read >= NV50_I2C_PORTS) {
179                 NV_ERROR(dev, "unknown i2c port %d\n", entry->read);
180                 return -EINVAL;
181         }
182
183         i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
184         if (i2c == NULL)
185                 return -ENOMEM;
186
187         switch (entry->port_type) {
188         case 0:
189                 i2c->bit.setsda = nv04_i2c_setsda;
190                 i2c->bit.setscl = nv04_i2c_setscl;
191                 i2c->bit.getsda = nv04_i2c_getsda;
192                 i2c->bit.getscl = nv04_i2c_getscl;
193                 i2c->rd = entry->read;
194                 i2c->wr = entry->write;
195                 break;
196         case 4:
197                 i2c->bit.setsda = nv4e_i2c_setsda;
198                 i2c->bit.setscl = nv4e_i2c_setscl;
199                 i2c->bit.getsda = nv4e_i2c_getsda;
200                 i2c->bit.getscl = nv4e_i2c_getscl;
201                 i2c->rd = 0x600800 + entry->read;
202                 i2c->wr = 0x600800 + entry->write;
203                 break;
204         case 5:
205                 i2c->bit.setsda = nv50_i2c_setsda;
206                 i2c->bit.setscl = nv50_i2c_setscl;
207                 if (dev_priv->card_type < NV_D0) {
208                         i2c->bit.getsda = nv50_i2c_getsda;
209                         i2c->bit.getscl = nv50_i2c_getscl;
210                         i2c->rd = nv50_i2c_port[entry->read];
211                         i2c->wr = i2c->rd;
212                 } else {
213                         i2c->bit.getsda = nvd0_i2c_getsda;
214                         i2c->bit.getscl = nvd0_i2c_getscl;
215                         i2c->rd = 0x00d014 + (entry->read * 0x20);
216                         i2c->wr = i2c->rd;
217                 }
218                 break;
219         case 6:
220                 i2c->rd = entry->read;
221                 i2c->wr = entry->write;
222                 break;
223         default:
224                 NV_ERROR(dev, "DCB I2C port type %d unknown\n",
225                          entry->port_type);
226                 kfree(i2c);
227                 return -EINVAL;
228         }
229
230         snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
231                  "nouveau-%s-%d", pci_name(dev->pdev), index);
232         i2c->adapter.owner = THIS_MODULE;
233         i2c->adapter.dev.parent = &dev->pdev->dev;
234         i2c->dev = dev;
235         i2c_set_adapdata(&i2c->adapter, i2c);
236
237         if (entry->port_type < 6) {
238                 i2c->adapter.algo_data = &i2c->bit;
239                 i2c->bit.udelay = 40;
240                 i2c->bit.timeout = usecs_to_jiffies(5000);
241                 i2c->bit.data = i2c;
242                 ret = i2c_bit_add_bus(&i2c->adapter);
243         } else {
244                 i2c->adapter.algo = &nouveau_dp_i2c_algo;
245                 ret = i2c_add_adapter(&i2c->adapter);
246         }
247
248         if (ret) {
249                 NV_ERROR(dev, "Failed to register i2c %d\n", index);
250                 kfree(i2c);
251                 return ret;
252         }
253
254         entry->chan = i2c;
255         return 0;
256 }
257
258 void
259 nouveau_i2c_fini(struct drm_device *dev, struct dcb_i2c_entry *entry)
260 {
261         if (!entry->chan)
262                 return;
263
264         i2c_del_adapter(&entry->chan->adapter);
265         kfree(entry->chan);
266         entry->chan = NULL;
267 }
268
269 struct nouveau_i2c_chan *
270 nouveau_i2c_find(struct drm_device *dev, int index)
271 {
272         struct drm_nouveau_private *dev_priv = dev->dev_private;
273         struct dcb_i2c_entry *i2c = &dev_priv->vbios.dcb.i2c[index];
274
275         if (index >= DCB_MAX_NUM_I2C_ENTRIES)
276                 return NULL;
277
278         if (dev_priv->card_type >= NV_50 && (i2c->entry & 0x00000100)) {
279                 uint32_t reg = 0xe500, val;
280
281                 if (i2c->port_type == 6) {
282                         reg += i2c->read * 0x50;
283                         val  = 0x2002;
284                 } else {
285                         reg += ((i2c->entry & 0x1e00) >> 9) * 0x50;
286                         val  = 0xe001;
287                 }
288
289                 /* nfi, but neither auxch or i2c work if it's 1 */
290                 nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
291                 /* nfi, but switches auxch vs normal i2c */
292                 nv_mask(dev, reg + 0x00, 0x0000f003, val);
293         }
294
295         if (!i2c->chan && nouveau_i2c_init(dev, i2c, index))
296                 return NULL;
297         return i2c->chan;
298 }
299
300 bool
301 nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
302 {
303         uint8_t buf[] = { 0 };
304         struct i2c_msg msgs[] = {
305                 {
306                         .addr = addr,
307                         .flags = 0,
308                         .len = 1,
309                         .buf = buf,
310                 },
311                 {
312                         .addr = addr,
313                         .flags = I2C_M_RD,
314                         .len = 1,
315                         .buf = buf,
316                 }
317         };
318
319         return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
320 }
321
322 int
323 nouveau_i2c_identify(struct drm_device *dev, const char *what,
324                      struct i2c_board_info *info,
325                      bool (*match)(struct nouveau_i2c_chan *,
326                                    struct i2c_board_info *),
327                      int index)
328 {
329         struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
330         int i;
331
332         NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, index);
333
334         for (i = 0; info[i].addr; i++) {
335                 if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
336                     (!match || match(i2c, &info[i]))) {
337                         NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
338                         return i;
339                 }
340         }
341
342         NV_DEBUG(dev, "No devices found.\n");
343
344         return -ENODEV;
345 }