-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsccb.c
168 lines (133 loc) · 2.19 KB
/
sccb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* soft-sccb.c
* Implement the SCCB master side protocol.
*/
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <asm/io.h>
#include <linux/semaphore.h>
#include <mach/regs-gpio.h>
#include "sccb.h"
static DEFINE_SEMAPHORE(bus_lock);
static void __inline__ sccb_start(void)
{
CFG_WRITE(SIO_D);
Low(SIO_D);
WAIT_STABLE();
}
static void __inline__ sccb_write_byte(u8 data)
{
int i;
CFG_WRITE(SIO_D);
WAIT_STABLE();
/* write 8-bits octet. */
for (i=0;i<8;i++)
{
Low(SIO_C);
WAIT_STABLE();
if (data & 0x80)
{
High(SIO_D);
}
else
{
Low(SIO_D);
}
data = data<<1;
WAIT_CYCLE();
High(SIO_C);
WAIT_CYCLE();
}
/* write byte done, wait the Don't care bit now. */
{
Low(SIO_C);
High(SIO_D);
CFG_READ(SIO_D);
WAIT_CYCLE();
High(SIO_C);
WAIT_CYCLE();
}
}
static u8 __inline__ sccb_read_byte(void)
{
int i;
u8 data;
CFG_READ(SIO_D);
WAIT_STABLE();
Low(SIO_C);
WAIT_CYCLE();
data = 0;
for (i=7;i>=0;i--)
{
//High(SIO_C);
//WAIT_STABLE();
data |= State(SIO_D)<<i;
WAIT_CYCLE();
//Low(SIO_C);
//WAIT_CYCLE();
}
/* read byte down, write the NA bit now.*/
{
CFG_WRITE(SIO_D);
High(SIO_D);
WAIT_CYCLE();
High(SIO_C);
WAIT_CYCLE();
}
printk("(%d)",data & 0xFF);
return (data);
}
static void __inline__ sccb_stop(void)
{
Low(SIO_C);
WAIT_STABLE();
CFG_WRITE(SIO_D);
Low(SIO_D);
WAIT_CYCLE();
High(SIO_C);
WAIT_STABLE();
High(SIO_D);
WAIT_CYCLE();
CFG_READ(SIO_D);
}
void sccb_write(u8 IdAddr, u8 SubAddr, u8 data)
{
down(&bus_lock);
sccb_start();
sccb_write_byte(IdAddr);
sccb_write_byte(SubAddr);
sccb_write_byte(data);
sccb_stop();
up (&bus_lock);
}
u8 sccb_read(u8 IdAddr, u8 SubAddr)
{
u8 data;
down(&bus_lock);
sccb_start();
sccb_write_byte(IdAddr);
sccb_write_byte(SubAddr);
sccb_stop();
sccb_start();
sccb_write_byte(IdAddr|0x01);
data = sccb_read_byte();
sccb_stop();
up(&bus_lock);
return data;
}
int sccb_init(void)
{
CFG_WRITE(SIO_C);
CFG_WRITE(SIO_D);
High(SIO_C);
High(SIO_D);
WAIT_STABLE();
return 0;
}
void sccb_cleanup(void)
{
CFG_READ(SIO_C);
CFG_READ(SIO_D);
}