radeon: remove unused legacy state
[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 uint32_t nv50_i2c_port(int index)
32 {
33         uint32_t port = 0;
34
35         switch (index) {
36                 case 0:
37                         port = NV50_PCONNECTOR_I2C_PORT_0;
38                         break;
39                 case 1:
40                         port = NV50_PCONNECTOR_I2C_PORT_1;
41                         break;
42                 case 2:
43                         port = NV50_PCONNECTOR_I2C_PORT_2;
44                         break;
45                 case 3:
46                         port = NV50_PCONNECTOR_I2C_PORT_3;
47                         break;
48                 case 4:
49                         port = NV50_PCONNECTOR_I2C_PORT_4;
50                         break;
51                 case 5:
52                         port = NV50_PCONNECTOR_I2C_PORT_5;
53                         break;
54                 default:
55                         break;
56         }
57
58         if (!port) {
59                 DRM_ERROR("Invalid i2c port, returning 0.\n");
60                 BUG();
61         }
62
63         return port;
64 }
65
66 static void nv50_i2c_set_bits(struct nv50_i2c_channel *chan, int clock_high, int data_high)
67 {
68         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
69         uint32_t port = nv50_i2c_port(chan->index);
70
71         if (!port)
72                 return;
73
74         NV_WRITE(port, 4 | (data_high << 1) | clock_high);
75 }
76
77 static void nv50_i2c_get_bits(struct nv50_i2c_channel *chan, int *clock_high, int *data_high)
78 {
79         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
80         uint32_t port = nv50_i2c_port(chan->index);
81         uint32_t val;
82
83         if (!port)
84                 return;
85
86         val = NV_READ(port);
87
88         if (val & 1)
89                 *clock_high = 1;
90         else
91                 *clock_high = 0;
92
93         if (val & 2)
94                 *data_high = 1;
95         else
96                 *data_high = 0;
97 }
98
99 static bool nv50_i2c_raise_clock(struct nv50_i2c_channel *chan, int data)
100 {
101         int i, clock;
102
103         nv50_i2c_set_bits(chan, 1, data);
104         udelay(2);
105
106         for (i = 2200; i > 0; i -= 2) {
107                 nv50_i2c_get_bits(chan, &clock, &data);
108                 if (clock)
109                         return true;
110                 udelay(2);
111         }
112
113         printk("a timeout occured in nv50_i2c_raise_clock\n");
114
115         return false;
116 }
117
118 static bool nv50_i2c_start(struct nv50_i2c_channel *chan)
119 {
120         if (!nv50_i2c_raise_clock(chan, 1))
121                 return false;
122
123         nv50_i2c_set_bits(chan, 1, 0);
124         udelay(5);
125
126         nv50_i2c_set_bits(chan, 0, 0);
127         udelay(5);
128
129         return true;
130 }
131
132 static void nv50_i2c_stop(struct nv50_i2c_channel *chan)
133 {
134         nv50_i2c_set_bits(chan, 0, 0);
135         udelay(2);
136
137         nv50_i2c_set_bits(chan, 1, 0);
138         udelay(5);
139
140         nv50_i2c_set_bits(chan, 1, 1);
141         udelay(5);
142 }
143
144 static bool nv50_i2c_write_bit(struct nv50_i2c_channel *chan, int data)
145 {
146         bool rval;
147
148         nv50_i2c_set_bits(chan, 0, data);
149         udelay(2);
150
151         rval = nv50_i2c_raise_clock(chan, data);
152         udelay(5);
153
154         nv50_i2c_set_bits(chan, 0, data);
155         udelay(5);
156
157         return rval;
158 }
159
160 static bool nv50_i2c_read_bit(struct nv50_i2c_channel *chan, int *data)
161 {
162         bool rval;
163         int clock;
164
165         rval = nv50_i2c_raise_clock(chan, 1);
166         udelay(5);
167
168         nv50_i2c_get_bits(chan, &clock, data);
169         udelay(5);
170
171         nv50_i2c_set_bits(chan, 0, 1);
172         udelay(5);
173
174         return rval;
175 }
176
177 static bool nv50_i2c_write_byte(struct nv50_i2c_channel *chan, uint8_t byte)
178 {
179         bool rval;
180         int i, clock, data;
181
182         for (i = 7; i >= 0; i--)
183                 if (!nv50_i2c_write_bit(chan, (byte >> i) & 1))
184                         return false;
185
186         nv50_i2c_set_bits(chan, 0, 1);
187         udelay(5);
188
189         rval = nv50_i2c_raise_clock(chan, 1);
190
191         if (rval) {
192                 for (i = 40; i > 0; i -= 2) {
193                         udelay(2);
194                         nv50_i2c_get_bits(chan, &clock, &data);
195                         if (data == 0) 
196                                 break;
197                 }
198
199                 if (i <= 0) {
200                         printk("a timeout occured in nv50_i2c_write_byte\n");
201                         rval = false;
202                 }
203         }
204
205         nv50_i2c_set_bits(chan, 0, 1);
206         udelay(5);
207
208         return rval;
209 }
210
211 static bool nv50_i2c_read_byte(struct nv50_i2c_channel *chan, uint8_t *byte, bool last)
212 {
213         int i, bit;
214
215         nv50_i2c_set_bits(chan, 0, 1);
216         udelay(5);
217
218         *byte = 0;
219
220         for (i = 7; i >= 0; i--) {
221                 if (nv50_i2c_read_bit(chan, &bit)) {
222                         if (bit)
223                                 *byte |= (1 << i);
224                 } else {
225                         return false;
226                 }
227         }
228
229         if (!nv50_i2c_write_bit(chan, last ? 1 : 0))
230                 return false;
231
232         return true;
233 }
234
235 /* only 7 bits addresses. */
236 static bool nv50_i2c_address(struct nv50_i2c_channel *chan, uint8_t address, bool write)
237 {
238         if (nv50_i2c_start(chan)) {
239                 uint8_t real_addr = (address << 1);
240                 if (!write)
241                         real_addr |= 1;
242
243                 if (nv50_i2c_write_byte(chan, real_addr))
244                         return true;
245
246                 /* failure, so issue stop */
247                 nv50_i2c_stop(chan);
248         }
249
250         return false;
251 }
252
253 static bool nv50_i2c_read(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
254 {
255         int i, j;
256         bool rval, last;
257
258         /* retries */
259         for (i = 0; i < 4; i++) {
260                 rval = nv50_i2c_address(chan, address, false);
261                 if (!rval)
262                         return false;
263
264                 for (j = 0; j < length; j++) {
265                         last = false;
266                         if (j == (length - 1))
267                                 last = true;
268                         rval = nv50_i2c_read_byte(chan, &buffer[j], last);
269                         if (!rval) {
270                                 nv50_i2c_stop(chan);
271                                 break;
272                         }
273                 }
274
275                 nv50_i2c_stop(chan);
276
277                 /* done */
278                 if (rval)
279                         break;
280         }
281
282         if (!rval)
283                 printk("nv50_i2c_read failed\n");
284
285         return rval;
286 }
287
288 static bool nv50_i2c_write(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
289 {
290         int i, j;
291         bool rval;
292
293         /* retries */
294         for (i = 0; i < 4; i++) {
295                 rval = nv50_i2c_address(chan, address, true);
296                 if (!rval)
297                         return false;
298
299                 for (j = 0; j < length; j++) {
300                         rval = nv50_i2c_write_byte(chan, buffer[j]);
301                         if (!rval) {
302                                 break;
303                         }
304                 }
305
306                 nv50_i2c_stop(chan);
307
308                 /* done */
309                 if (rval)
310                         break;
311         }
312
313         if (!rval)
314                 printk("nv50_i2c_write failed\n");
315
316         return rval;
317 }
318
319 static int nv50_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
320 {
321         struct nv50_i2c_channel *chan = i2c_get_adapdata(i2c_adap);
322         bool rval;
323         int i;
324
325         for (i = 0; i < num; i++) {
326                 if (msgs[i].flags & I2C_M_RD) { /* read */
327                         rval = nv50_i2c_read(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
328                 } else { /* write */
329                         rval = nv50_i2c_write(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
330                 }
331
332                 if (!rval)
333                         break;
334         }
335
336         if (rval)
337                 return i;
338         else
339                 return -EINVAL;
340 }
341
342 static u32 nv50_i2c_functionality(struct i2c_adapter *adap)
343 {
344         return I2C_FUNC_I2C;
345 }
346
347 static const struct i2c_algorithm nv50_i2c_algo = {
348         .master_xfer    = nv50_i2c_xfer,
349         .functionality  = nv50_i2c_functionality,
350 };
351
352 static int nv50_i2c_register_bus(struct i2c_adapter *adap)
353 {
354         adap->algo = &nv50_i2c_algo;
355
356         adap->timeout = 40;
357         adap->retries = 4;
358
359         return i2c_add_adapter(adap);
360 }
361
362 #define I2C_HW_B_NOUVEAU 0x010030
363 struct nv50_i2c_channel *nv50_i2c_channel_create(struct drm_device *dev, uint32_t index)
364 {
365         struct nv50_i2c_channel *chan;
366
367         chan = kzalloc(sizeof(struct nv50_i2c_channel), GFP_KERNEL);
368
369         if (!chan)
370                 goto out;
371
372         DRM_INFO("Creating i2c bus with index %d\n", index);
373
374         chan->dev = dev;
375         chan->index = index;
376         snprintf(chan->adapter.name, I2C_NAME_SIZE, "nv50 i2c %d", index);
377         chan->adapter.owner = THIS_MODULE;
378         chan->adapter.id = I2C_HW_B_NOUVEAU;
379         chan->adapter.dev.parent = &dev->pdev->dev;
380
381         i2c_set_adapdata(&chan->adapter, chan);
382
383         if (nv50_i2c_register_bus(&chan->adapter))
384                 goto out;
385
386         return chan;
387
388 out:
389         kfree(chan);
390         return NULL;
391 }
392
393 void nv50_i2c_channel_destroy(struct nv50_i2c_channel *chan)
394 {
395         if (!chan)
396                 return;
397
398         i2c_del_adapter(&chan->adapter);
399         kfree(chan);
400 }