NV50: A minor change.
[platform/upstream/libdrm.git] / linux-core / nv50_i2c.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 /* This is largely a clone from xorg i2c functions, as i had serious trouble getting an i2c_bit_algo adaptor running. */
28
29 #include "nv50_i2c.h"
30
31 static void nv50_i2c_set_bits(struct nv50_i2c_channel *chan, int clock_high, int data_high)
32 {
33         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
34
35         NV_WRITE(NV50_PCONNECTOR_I2C_PORT(chan->index), 4 | (data_high << 1) | clock_high);
36 }
37
38 static void nv50_i2c_get_bits(struct nv50_i2c_channel *chan, int *clock_high, int *data_high)
39 {
40         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
41
42         uint32_t val = NV_READ(NV50_PCONNECTOR_I2C_PORT(chan->index));
43
44         if (val & 1)
45                 *clock_high = 1;
46         else
47                 *clock_high = 0;
48
49         if (val & 2)
50                 *data_high = 1;
51         else
52                 *data_high = 0;
53 }
54
55 static bool nv50_i2c_raise_clock(struct nv50_i2c_channel *chan, int data)
56 {
57         int i, clock;
58
59         nv50_i2c_set_bits(chan, 1, data);
60         udelay(2);
61
62         for (i = 2200; i > 0; i -= 2) {
63                 nv50_i2c_get_bits(chan, &clock, &data);
64                 if (clock)
65                         return TRUE;
66                 udelay(2);
67         }
68
69         printk("a timeout occured in nv50_i2c_raise_clock\n");
70
71         return FALSE;
72 }
73
74 static bool nv50_i2c_start(struct nv50_i2c_channel *chan)
75 {
76         if (!nv50_i2c_raise_clock(chan, 1))
77                 return FALSE;
78
79         nv50_i2c_set_bits(chan, 1, 0);
80         udelay(5);
81
82         nv50_i2c_set_bits(chan, 0, 0);
83         udelay(5);
84
85         return TRUE;
86 }
87
88 static void nv50_i2c_stop(struct nv50_i2c_channel *chan)
89 {
90         nv50_i2c_set_bits(chan, 0, 0);
91         udelay(2);
92
93         nv50_i2c_set_bits(chan, 1, 0);
94         udelay(5);
95
96         nv50_i2c_set_bits(chan, 1, 1);
97         udelay(5);
98 }
99
100 static bool nv50_i2c_write_bit(struct nv50_i2c_channel *chan, int data)
101 {
102         bool rval;
103
104         nv50_i2c_set_bits(chan, 0, data);
105         udelay(2);
106
107         rval = nv50_i2c_raise_clock(chan, data);
108         udelay(5);
109
110         nv50_i2c_set_bits(chan, 0, data);
111         udelay(5);
112
113         return rval;
114 }
115
116 static bool nv50_i2c_read_bit(struct nv50_i2c_channel *chan, int *data)
117 {
118         bool rval;
119         int clock;
120
121         rval = nv50_i2c_raise_clock(chan, 1);
122         udelay(5);
123
124         nv50_i2c_get_bits(chan, &clock, data);
125         udelay(5);
126
127         nv50_i2c_set_bits(chan, 0, 1);
128         udelay(5);
129
130         return rval;
131 }
132
133 static bool nv50_i2c_write_byte(struct nv50_i2c_channel *chan, uint8_t byte)
134 {
135         bool rval;
136         int i, clock, data;
137
138         for (i = 7; i >= 0; i--)
139                 if (!nv50_i2c_write_bit(chan, (byte >> i) & 1))
140                         return FALSE;
141
142         nv50_i2c_set_bits(chan, 0, 1);
143         udelay(5);
144
145         rval = nv50_i2c_raise_clock(chan, 1);
146
147         if (rval) {
148                 for (i = 40; i > 0; i -= 2) {
149                         udelay(2);
150                         nv50_i2c_get_bits(chan, &clock, &data);
151                         if (data == 0) 
152                                 break;
153                 }
154
155                 if (i <= 0) {
156                         printk("a timeout occured in nv50_i2c_write_byte\n");
157                         rval = FALSE;
158                 }
159         }
160
161         nv50_i2c_set_bits(chan, 0, 1);
162         udelay(5);
163
164         return rval;
165 }
166
167 static bool nv50_i2c_read_byte(struct nv50_i2c_channel *chan, uint8_t *byte, bool last)
168 {
169         int i, bit;
170
171         nv50_i2c_set_bits(chan, 0, 1);
172         udelay(5);
173
174         *byte = 0;
175
176         for (i = 7; i >= 0; i--) {
177                 if (nv50_i2c_read_bit(chan, &bit)) {
178                         if (bit)
179                                 *byte |= (1 << i);
180                 } else {
181                         return FALSE;
182                 }
183         }
184
185         if (!nv50_i2c_write_bit(chan, last ? 1 : 0))
186                 return FALSE;
187
188         return TRUE;
189 }
190
191 /* only 7 bits addresses. */
192 static bool nv50_i2c_address(struct nv50_i2c_channel *chan, uint8_t address, bool write)
193 {
194         if (nv50_i2c_start(chan)) {
195                 uint8_t real_addr = (address << 1);
196                 if (!write)
197                         real_addr |= 1;
198
199                 if (nv50_i2c_write_byte(chan, real_addr))
200                         return TRUE;
201
202                 /* failure, so issue stop */
203                 nv50_i2c_stop(chan);
204         }
205
206         return FALSE;
207 }
208
209 static bool nv50_i2c_read(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
210 {
211         int i, j;
212         bool rval, last;
213
214         /* retries */
215         for (i = 0; i < 4; i++) {
216                 rval = nv50_i2c_address(chan, address, FALSE);
217                 if (!rval)
218                         return FALSE;
219
220                 for (j = 0; j < length; j++) {
221                         last = false;
222                         if (j == (length - 1))
223                                 last = true;
224                         rval = nv50_i2c_read_byte(chan, &buffer[j], last);
225                         if (!rval) {
226                                 nv50_i2c_stop(chan);
227                                 break;
228                         }
229                 }
230
231                 nv50_i2c_stop(chan);
232
233                 /* done */
234                 if (rval)
235                         break;
236         }
237
238         if (!rval)
239                 printk("nv50_i2c_read failed\n");
240
241         return rval;
242 }
243
244 static bool nv50_i2c_write(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
245 {
246         int i, j;
247         bool rval;
248
249         /* retries */
250         for (i = 0; i < 4; i++) {
251                 rval = nv50_i2c_address(chan, address, TRUE);
252                 if (!rval)
253                         return FALSE;
254
255                 for (j = 0; j < length; j++) {
256                         rval = nv50_i2c_write_byte(chan, buffer[j]);
257                         if (!rval) {
258                                 break;
259                         }
260                 }
261
262                 nv50_i2c_stop(chan);
263
264                 /* done */
265                 if (rval)
266                         break;
267         }
268
269         if (!rval)
270                 printk("nv50_i2c_write failed\n");
271
272         return rval;
273 }
274
275 static int nv50_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
276 {
277         struct nv50_i2c_channel *chan = i2c_get_adapdata(i2c_adap);
278         bool rval;
279         int i;
280
281         for (i = 0; i < num; i++) {
282                 if (msgs[i].flags & I2C_M_RD) { /* read */
283                         rval = nv50_i2c_read(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
284                 } else { /* write */
285                         rval = nv50_i2c_write(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
286                 }
287
288                 if (!rval)
289                         break;
290         }
291
292         if (rval)
293                 return i;
294         else
295                 return -EINVAL;
296 }
297
298 static u32 nv50_i2c_functionality(struct i2c_adapter *adap)
299 {
300         return I2C_FUNC_I2C;
301 }
302
303 static const struct i2c_algorithm nv50_i2c_algo = {
304         .master_xfer    = nv50_i2c_xfer,
305         .functionality  = nv50_i2c_functionality,
306 };
307
308 static int nv50_i2c_register_bus(struct i2c_adapter *adap)
309 {
310         adap->algo = &nv50_i2c_algo;
311
312         adap->timeout = 40;
313         adap->retries = 4;
314
315         return i2c_add_adapter(adap);
316 }
317
318 #define I2C_HW_B_NOUVEAU 0x010030
319 struct nv50_i2c_channel *nv50_i2c_channel_create(struct drm_device *dev, uint32_t index)
320 {
321         struct nv50_i2c_channel *chan;
322
323         chan = kzalloc(sizeof(struct nv50_i2c_channel), GFP_KERNEL);
324
325         if (!chan)
326                 goto out;
327
328         DRM_INFO("Creating i2c bus with index %d\n", index);
329
330         chan->dev = dev;
331         chan->index = index;
332         snprintf(chan->adapter.name, I2C_NAME_SIZE, "nv50 i2c %d", index);
333         chan->adapter.owner = THIS_MODULE;
334         chan->adapter.id = I2C_HW_B_NOUVEAU;
335         chan->adapter.dev.parent = &dev->pdev->dev;
336
337         i2c_set_adapdata(&chan->adapter, chan);
338
339         if (nv50_i2c_register_bus(&chan->adapter))
340                 goto out;
341
342         return chan;
343
344 out:
345         kfree(chan);
346         return NULL;
347 }
348
349 void nv50_i2c_channel_destroy(struct nv50_i2c_channel *chan)
350 {
351         if (!chan)
352                 return;
353
354         i2c_del_adapter(&chan->adapter);
355         kfree(chan);
356 }