This source file includes following definitions.
- sw_i2c_wait
- sw_i2c_scl
- sw_i2c_sda
- sw_i2c_read_sda
- sw_i2c_ack
- sw_i2c_start
- sw_i2c_stop
- sw_i2c_write_byte
- sw_i2c_read_byte
- sm750le_i2c_init
- sm750_sw_i2c_init
- sm750_sw_i2c_read_reg
- sm750_sw_i2c_write_reg
1
2
3
4
5
6
7
8
9
10 #include "ddk750_chip.h"
11 #include "ddk750_reg.h"
12 #include "ddk750_swi2c.h"
13 #include "ddk750_power.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 static unsigned char sw_i2c_clk_gpio = DEFAULT_I2C_SCL;
55 static unsigned char sw_i2c_data_gpio = DEFAULT_I2C_SDA;
56
57
58
59
60
61
62
63
64
65
66
67
68
69 static unsigned long sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
70 static unsigned long sw_i2c_clk_gpio_data_reg = GPIO_DATA;
71 static unsigned long sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
72
73
74 static unsigned long sw_i2c_data_gpio_mux_reg = GPIO_MUX;
75 static unsigned long sw_i2c_data_gpio_data_reg = GPIO_DATA;
76 static unsigned long sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
77
78
79
80
81 static void sw_i2c_wait(void)
82 {
83
84
85
86
87
88
89
90
91
92
93
94
95 int i, tmp;
96
97 for (i = 0; i < 600; i++) {
98 tmp = i;
99 tmp += i;
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 static void sw_i2c_scl(unsigned char value)
116 {
117 unsigned long gpio_data;
118 unsigned long gpio_dir;
119
120 gpio_dir = peek32(sw_i2c_clk_gpio_data_dir_reg);
121 if (value) {
122
123
124
125
126 gpio_dir &= ~(1 << sw_i2c_clk_gpio);
127 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
128 } else {
129
130 gpio_data = peek32(sw_i2c_clk_gpio_data_reg);
131 gpio_data &= ~(1 << sw_i2c_clk_gpio);
132 poke32(sw_i2c_clk_gpio_data_reg, gpio_data);
133
134
135 gpio_dir |= (1 << sw_i2c_clk_gpio);
136 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
137 }
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152 static void sw_i2c_sda(unsigned char value)
153 {
154 unsigned long gpio_data;
155 unsigned long gpio_dir;
156
157 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
158 if (value) {
159
160
161
162
163 gpio_dir &= ~(1 << sw_i2c_data_gpio);
164 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
165 } else {
166
167 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
168 gpio_data &= ~(1 << sw_i2c_data_gpio);
169 poke32(sw_i2c_data_gpio_data_reg, gpio_data);
170
171
172 gpio_dir |= (1 << sw_i2c_data_gpio);
173 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
174 }
175 }
176
177
178
179
180
181
182
183 static unsigned char sw_i2c_read_sda(void)
184 {
185 unsigned long gpio_dir;
186 unsigned long gpio_data;
187 unsigned long dir_mask = 1 << sw_i2c_data_gpio;
188
189
190 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
191 if ((gpio_dir & dir_mask) != ~dir_mask) {
192 gpio_dir &= ~(1 << sw_i2c_data_gpio);
193 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
194 }
195
196
197 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
198 if (gpio_data & (1 << sw_i2c_data_gpio))
199 return 1;
200 else
201 return 0;
202 }
203
204
205
206
207 static void sw_i2c_ack(void)
208 {
209 return;
210 }
211
212
213
214
215 static void sw_i2c_start(void)
216 {
217
218 sw_i2c_sda(1);
219 sw_i2c_scl(1);
220 sw_i2c_sda(0);
221 }
222
223
224
225
226 static void sw_i2c_stop(void)
227 {
228
229 sw_i2c_scl(1);
230 sw_i2c_sda(0);
231 sw_i2c_sda(1);
232 }
233
234
235
236
237
238
239
240
241
242
243
244 static long sw_i2c_write_byte(unsigned char data)
245 {
246 unsigned char value = data;
247 int i;
248
249
250 for (i = 0; i < 8; i++) {
251
252 sw_i2c_scl(0);
253
254
255 if ((value & 0x80) != 0)
256 sw_i2c_sda(1);
257 else
258 sw_i2c_sda(0);
259
260 sw_i2c_wait();
261
262
263 sw_i2c_scl(1);
264 sw_i2c_wait();
265
266
267 value = value << 1;
268 }
269
270
271 sw_i2c_scl(0);
272 sw_i2c_sda(1);
273
274
275 sw_i2c_wait();
276 sw_i2c_scl(1);
277 sw_i2c_wait();
278
279
280 for (i = 0; i < 0xff; i++) {
281 if (!sw_i2c_read_sda())
282 break;
283
284 sw_i2c_scl(0);
285 sw_i2c_wait();
286 sw_i2c_scl(1);
287 sw_i2c_wait();
288 }
289
290
291 sw_i2c_scl(0);
292 sw_i2c_sda(1);
293
294 if (i < 0xff)
295 return 0;
296 else
297 return -1;
298 }
299
300
301
302
303
304
305
306
307
308
309
310 static unsigned char sw_i2c_read_byte(unsigned char ack)
311 {
312 int i;
313 unsigned char data = 0;
314
315 for (i = 7; i >= 0; i--) {
316
317 sw_i2c_scl(0);
318 sw_i2c_sda(1);
319 sw_i2c_wait();
320
321
322 sw_i2c_scl(1);
323 sw_i2c_wait();
324
325
326 data |= (sw_i2c_read_sda() << i);
327 }
328
329 if (ack)
330 sw_i2c_ack();
331
332
333 sw_i2c_scl(0);
334 sw_i2c_sda(1);
335
336 return data;
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350 static long sm750le_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
351 {
352 int i;
353
354
355 sw_i2c_clk_gpio_data_reg = GPIO_DATA_SM750LE;
356 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
357
358
359 sw_i2c_clk_gpio = clk_gpio;
360
361
362 sw_i2c_data_gpio_data_reg = GPIO_DATA_SM750LE;
363 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
364
365
366 sw_i2c_data_gpio = data_gpio;
367
368
369
370
371 for (i = 0; i < 9; i++)
372 sw_i2c_stop();
373
374 return 0;
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388 long sm750_sw_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
389 {
390 int i;
391
392
393
394
395
396 if ((clk_gpio > 31) || (data_gpio > 31))
397 return -1;
398
399 if (sm750_get_chip_type() == SM750LE)
400 return sm750le_i2c_init(clk_gpio, data_gpio);
401
402
403 sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
404 sw_i2c_clk_gpio_data_reg = GPIO_DATA;
405 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
406
407
408 sw_i2c_clk_gpio = clk_gpio;
409
410
411 sw_i2c_data_gpio_mux_reg = GPIO_MUX;
412 sw_i2c_data_gpio_data_reg = GPIO_DATA;
413 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
414
415
416 sw_i2c_data_gpio = data_gpio;
417
418
419 poke32(sw_i2c_clk_gpio_mux_reg,
420 peek32(sw_i2c_clk_gpio_mux_reg) & ~(1 << sw_i2c_clk_gpio));
421 poke32(sw_i2c_data_gpio_mux_reg,
422 peek32(sw_i2c_data_gpio_mux_reg) & ~(1 << sw_i2c_data_gpio));
423
424
425 sm750_enable_gpio(1);
426
427
428 for (i = 0; i < 9; i++)
429 sw_i2c_stop();
430
431 return 0;
432 }
433
434
435
436
437
438
439
440
441
442
443
444
445 unsigned char sm750_sw_i2c_read_reg(unsigned char addr, unsigned char reg)
446 {
447 unsigned char data;
448
449
450 sw_i2c_start();
451
452
453 sw_i2c_write_byte(addr);
454
455
456 sw_i2c_write_byte(reg);
457
458
459 sw_i2c_start();
460 sw_i2c_write_byte(addr + 1);
461 data = sw_i2c_read_byte(1);
462
463
464 sw_i2c_stop();
465
466 return data;
467 }
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 long sm750_sw_i2c_write_reg(unsigned char addr,
483 unsigned char reg,
484 unsigned char data)
485 {
486 long ret = 0;
487
488
489 sw_i2c_start();
490
491
492
493
494 if ((sw_i2c_write_byte(addr) != 0) ||
495 (sw_i2c_write_byte(reg) != 0) ||
496 (sw_i2c_write_byte(data) != 0)) {
497 ret = -1;
498 }
499
500
501 sw_i2c_stop();
502
503 return ret;
504 }