This source file includes following definitions.
- go7007_i2c_xfer
- go7007_smbus_xfer
- go7007_i2c_master_xfer
- go7007_functionality
- go7007_i2c_init
1
2
3
4
5
6 #include <linux/module.h>
7 #include <linux/delay.h>
8 #include <linux/sched.h>
9 #include <linux/list.h>
10 #include <linux/unistd.h>
11 #include <linux/time.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/mutex.h>
15 #include <linux/uaccess.h>
16
17 #include "go7007-priv.h"
18
19
20
21
22
23 #define SPI_I2C_ADDR_BASE 0x1400
24 #define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
25 #define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
26 #define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
27 #define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
28 #define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
29 #define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
30
31 #define I2C_STATE_MASK 0x0007
32 #define I2C_READ_READY_MASK 0x0008
33
34
35
36 static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
37
38 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
39 u16 command, int flags, u8 *data)
40 {
41 int i, ret = -EIO;
42 u16 val;
43
44 if (go->status == STATUS_SHUTDOWN)
45 return -ENODEV;
46
47 #ifdef GO7007_I2C_DEBUG
48 if (read)
49 dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
50 command, addr);
51 else
52 dev_dbg(go->dev,
53 "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
54 *data, command, addr);
55 #endif
56
57 mutex_lock(&go->hw_lock);
58
59 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
60
61 mutex_lock(&adlink_mpg24_i2c_lock);
62 go7007_write_addr(go, 0x3c82, 0x0020);
63 }
64
65
66 for (i = 0; i < 10; ++i) {
67 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
68 goto i2c_done;
69 if (!(val & I2C_STATE_MASK))
70 break;
71 msleep(100);
72 }
73 if (i == 10) {
74 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
75 goto i2c_done;
76 }
77
78
79 go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
80 go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
81
82
83 if (!read) {
84 go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
85 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
86 (addr << 9) | (command >> 8));
87 ret = 0;
88 goto i2c_done;
89 }
90
91
92 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
93 goto i2c_done;
94
95
96 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
97 (addr << 9) | 0x0100 | (command >> 8));
98
99
100 for (i = 0; i < 10; ++i) {
101 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
102 goto i2c_done;
103 if (val & I2C_READ_READY_MASK)
104 break;
105 msleep(100);
106 }
107 if (i == 10) {
108 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
109 goto i2c_done;
110 }
111
112
113 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
114 goto i2c_done;
115 *data = val;
116 ret = 0;
117
118 i2c_done:
119 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
120
121 go7007_write_addr(go, 0x3c82, 0x0000);
122 mutex_unlock(&adlink_mpg24_i2c_lock);
123 }
124 mutex_unlock(&go->hw_lock);
125 return ret;
126 }
127
128 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
129 unsigned short flags, char read_write,
130 u8 command, int size, union i2c_smbus_data *data)
131 {
132 struct go7007 *go = i2c_get_adapdata(adapter);
133
134 if (size != I2C_SMBUS_BYTE_DATA)
135 return -EIO;
136 return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
137 flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
138 }
139
140
141
142
143
144
145 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
146 struct i2c_msg msgs[], int num)
147 {
148 struct go7007 *go = i2c_get_adapdata(adapter);
149 int i;
150
151 for (i = 0; i < num; ++i) {
152
153
154 if (msgs[i].len == 2) {
155 if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
156 (msgs[i].flags & I2C_M_RD) ||
157 !(msgs[i + 1].flags & I2C_M_RD) ||
158 msgs[i + 1].len != 1)
159 return -EIO;
160 if (go7007_i2c_xfer(go, msgs[i].addr, 1,
161 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
162 0x01, &msgs[i + 1].buf[0]) < 0)
163 return -EIO;
164 ++i;
165 } else if (msgs[i].len == 3) {
166 if (msgs[i].flags & I2C_M_RD)
167 return -EIO;
168 if (msgs[i].len != 3)
169 return -EIO;
170 if (go7007_i2c_xfer(go, msgs[i].addr, 0,
171 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
172 0x01, &msgs[i].buf[2]) < 0)
173 return -EIO;
174 } else
175 return -EIO;
176 }
177
178 return num;
179 }
180
181 static u32 go7007_functionality(struct i2c_adapter *adapter)
182 {
183 return I2C_FUNC_SMBUS_BYTE_DATA;
184 }
185
186 static const struct i2c_algorithm go7007_algo = {
187 .smbus_xfer = go7007_smbus_xfer,
188 .master_xfer = go7007_i2c_master_xfer,
189 .functionality = go7007_functionality,
190 };
191
192 static struct i2c_adapter go7007_adap_templ = {
193 .owner = THIS_MODULE,
194 .name = "WIS GO7007SB",
195 .algo = &go7007_algo,
196 };
197
198 int go7007_i2c_init(struct go7007 *go)
199 {
200 memcpy(&go->i2c_adapter, &go7007_adap_templ,
201 sizeof(go7007_adap_templ));
202 go->i2c_adapter.dev.parent = go->dev;
203 i2c_set_adapdata(&go->i2c_adapter, go);
204 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
205 dev_err(go->dev,
206 "go7007-i2c: error: i2c_add_adapter failed\n");
207 return -1;
208 }
209 return 0;
210 }