Commit | Line | Data |
---|---|---|
faa4fd2a BC |
1 | /* |
2 | * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com | |
3 | * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | /* XXX: The SOLO6010 i2c does not have separate interrupts for each i2c | |
21 | * channel. The bus can only handle one i2c event at a time. The below handles | |
22 | * this all wrong. We should be using the status registers to see if the bus | |
23 | * is in use, and have a global lock to check the status register. Also, | |
24 | * the bulk of the work should be handled out-of-interrupt. The ugly loops | |
25 | * that occur during interrupt scare me. The ISR should merely signal | |
26 | * thread context, ACK the interrupt, and move on. -- BenC */ | |
27 | ||
28 | #include <linux/kernel.h> | |
29 | ||
30 | #include "solo6010.h" | |
31 | ||
32 | u8 solo_i2c_readbyte(struct solo6010_dev *solo_dev, int id, u8 addr, u8 off) | |
33 | { | |
34 | struct i2c_msg msgs[2]; | |
35 | u8 data; | |
36 | ||
37 | msgs[0].flags = 0; | |
38 | msgs[0].addr = addr; | |
39 | msgs[0].len = 1; | |
40 | msgs[0].buf = &off; | |
41 | ||
42 | msgs[1].flags = I2C_M_RD; | |
43 | msgs[1].addr = addr; | |
44 | msgs[1].len = 1; | |
45 | msgs[1].buf = &data; | |
46 | ||
47 | i2c_transfer(&solo_dev->i2c_adap[id], msgs, 2); | |
48 | ||
f62de9be | 49 | return data; |
faa4fd2a BC |
50 | } |
51 | ||
52 | void solo_i2c_writebyte(struct solo6010_dev *solo_dev, int id, u8 addr, | |
53 | u8 off, u8 data) | |
54 | { | |
55 | struct i2c_msg msgs; | |
56 | u8 buf[2]; | |
57 | ||
58 | buf[0] = off; | |
59 | buf[1] = data; | |
60 | msgs.flags = 0; | |
61 | msgs.addr = addr; | |
62 | msgs.len = 2; | |
63 | msgs.buf = buf; | |
64 | ||
65 | i2c_transfer(&solo_dev->i2c_adap[id], &msgs, 1); | |
66 | } | |
67 | ||
68 | static void solo_i2c_flush(struct solo6010_dev *solo_dev, int wr) | |
69 | { | |
70 | u32 ctrl; | |
71 | ||
72 | ctrl = SOLO_IIC_CH_SET(solo_dev->i2c_id); | |
73 | ||
74 | if (solo_dev->i2c_state == IIC_STATE_START) | |
75 | ctrl |= SOLO_IIC_START; | |
76 | ||
77 | if (wr) { | |
78 | ctrl |= SOLO_IIC_WRITE; | |
79 | } else { | |
80 | ctrl |= SOLO_IIC_READ; | |
81 | if (!(solo_dev->i2c_msg->flags & I2C_M_NO_RD_ACK)) | |
82 | ctrl |= SOLO_IIC_ACK_EN; | |
83 | } | |
84 | ||
85 | if (solo_dev->i2c_msg_ptr == solo_dev->i2c_msg->len) | |
86 | ctrl |= SOLO_IIC_STOP; | |
87 | ||
88 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, ctrl); | |
89 | } | |
90 | ||
91 | static void solo_i2c_start(struct solo6010_dev *solo_dev) | |
92 | { | |
93 | u32 addr = solo_dev->i2c_msg->addr << 1; | |
94 | ||
95 | if (solo_dev->i2c_msg->flags & I2C_M_RD) | |
96 | addr |= 1; | |
97 | ||
98 | solo_dev->i2c_state = IIC_STATE_START; | |
99 | solo_reg_write(solo_dev, SOLO_IIC_TXD, addr); | |
100 | solo_i2c_flush(solo_dev, 1); | |
101 | } | |
102 | ||
103 | static void solo_i2c_stop(struct solo6010_dev *solo_dev) | |
104 | { | |
105 | solo6010_irq_off(solo_dev, SOLO_IRQ_IIC); | |
106 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0); | |
107 | solo_dev->i2c_state = IIC_STATE_STOP; | |
108 | wake_up(&solo_dev->i2c_wait); | |
109 | } | |
110 | ||
111 | static int solo_i2c_handle_read(struct solo6010_dev *solo_dev) | |
112 | { | |
113 | prepare_read: | |
114 | if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) { | |
115 | solo_i2c_flush(solo_dev, 0); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | solo_dev->i2c_msg_ptr = 0; | |
120 | solo_dev->i2c_msg++; | |
121 | solo_dev->i2c_msg_num--; | |
122 | ||
123 | if (solo_dev->i2c_msg_num == 0) { | |
124 | solo_i2c_stop(solo_dev); | |
125 | return 0; | |
126 | } | |
127 | ||
128 | if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) { | |
129 | solo_i2c_start(solo_dev); | |
130 | } else { | |
131 | if (solo_dev->i2c_msg->flags & I2C_M_RD) | |
132 | goto prepare_read; | |
133 | else | |
134 | solo_i2c_stop(solo_dev); | |
135 | } | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int solo_i2c_handle_write(struct solo6010_dev *solo_dev) | |
141 | { | |
142 | retry_write: | |
143 | if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) { | |
144 | solo_reg_write(solo_dev, SOLO_IIC_TXD, | |
145 | solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr]); | |
146 | solo_dev->i2c_msg_ptr++; | |
147 | solo_i2c_flush(solo_dev, 1); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | solo_dev->i2c_msg_ptr = 0; | |
152 | solo_dev->i2c_msg++; | |
153 | solo_dev->i2c_msg_num--; | |
154 | ||
155 | if (solo_dev->i2c_msg_num == 0) { | |
156 | solo_i2c_stop(solo_dev); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) { | |
161 | solo_i2c_start(solo_dev); | |
162 | } else { | |
163 | if (solo_dev->i2c_msg->flags & I2C_M_RD) | |
164 | solo_i2c_stop(solo_dev); | |
165 | else | |
166 | goto retry_write; | |
167 | } | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | int solo_i2c_isr(struct solo6010_dev *solo_dev) | |
173 | { | |
174 | u32 status = solo_reg_read(solo_dev, SOLO_IIC_CTRL); | |
175 | int ret = -EINVAL; | |
176 | ||
177 | solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_IIC); | |
178 | ||
179 | if (status & (SOLO_IIC_STATE_TRNS & SOLO_IIC_STATE_SIG_ERR) || | |
180 | solo_dev->i2c_id < 0) { | |
181 | solo_i2c_stop(solo_dev); | |
182 | return -ENXIO; | |
183 | } | |
184 | ||
185 | switch (solo_dev->i2c_state) { | |
186 | case IIC_STATE_START: | |
187 | if (solo_dev->i2c_msg->flags & I2C_M_RD) { | |
188 | solo_dev->i2c_state = IIC_STATE_READ; | |
189 | ret = solo_i2c_handle_read(solo_dev); | |
190 | break; | |
191 | } | |
192 | ||
193 | solo_dev->i2c_state = IIC_STATE_WRITE; | |
194 | case IIC_STATE_WRITE: | |
195 | ret = solo_i2c_handle_write(solo_dev); | |
196 | break; | |
197 | ||
198 | case IIC_STATE_READ: | |
199 | solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr] = | |
200 | solo_reg_read(solo_dev, SOLO_IIC_RXD); | |
201 | solo_dev->i2c_msg_ptr++; | |
202 | ||
203 | ret = solo_i2c_handle_read(solo_dev); | |
204 | break; | |
205 | ||
206 | default: | |
207 | solo_i2c_stop(solo_dev); | |
208 | } | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
213 | static int solo_i2c_master_xfer(struct i2c_adapter *adap, | |
214 | struct i2c_msg msgs[], int num) | |
215 | { | |
216 | struct solo6010_dev *solo_dev = adap->algo_data; | |
217 | unsigned long timeout; | |
218 | int ret; | |
219 | int i; | |
220 | DEFINE_WAIT(wait); | |
221 | ||
222 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { | |
223 | if (&solo_dev->i2c_adap[i] == adap) | |
224 | break; | |
225 | } | |
226 | ||
227 | if (i == SOLO_I2C_ADAPTERS) | |
afabbe6d | 228 | return num; /* XXX Right return value for failure? */ |
faa4fd2a | 229 | |
f62de9be | 230 | mutex_lock(&solo_dev->i2c_mutex); |
faa4fd2a BC |
231 | solo_dev->i2c_id = i; |
232 | solo_dev->i2c_msg = msgs; | |
233 | solo_dev->i2c_msg_num = num; | |
234 | solo_dev->i2c_msg_ptr = 0; | |
235 | ||
236 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0); | |
237 | solo6010_irq_on(solo_dev, SOLO_IRQ_IIC); | |
238 | solo_i2c_start(solo_dev); | |
239 | ||
240 | timeout = HZ / 2; | |
241 | ||
242 | for (;;) { | |
243 | prepare_to_wait(&solo_dev->i2c_wait, &wait, TASK_INTERRUPTIBLE); | |
244 | ||
245 | if (solo_dev->i2c_state == IIC_STATE_STOP) | |
246 | break; | |
247 | ||
248 | timeout = schedule_timeout(timeout); | |
249 | if (!timeout) | |
250 | break; | |
251 | ||
252 | if (signal_pending(current)) | |
253 | break; | |
254 | } | |
255 | ||
256 | finish_wait(&solo_dev->i2c_wait, &wait); | |
257 | ret = num - solo_dev->i2c_msg_num; | |
258 | solo_dev->i2c_state = IIC_STATE_IDLE; | |
259 | solo_dev->i2c_id = -1; | |
260 | ||
f62de9be | 261 | mutex_unlock(&solo_dev->i2c_mutex); |
faa4fd2a BC |
262 | |
263 | return ret; | |
264 | } | |
265 | ||
266 | static u32 solo_i2c_functionality(struct i2c_adapter *adap) | |
267 | { | |
268 | return I2C_FUNC_I2C; | |
269 | } | |
270 | ||
271 | static struct i2c_algorithm solo_i2c_algo = { | |
272 | .master_xfer = solo_i2c_master_xfer, | |
273 | .functionality = solo_i2c_functionality, | |
274 | }; | |
275 | ||
276 | int solo_i2c_init(struct solo6010_dev *solo_dev) | |
277 | { | |
278 | int i; | |
279 | int ret; | |
280 | ||
281 | solo_reg_write(solo_dev, SOLO_IIC_CFG, | |
282 | SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE); | |
283 | ||
284 | solo_dev->i2c_id = -1; | |
285 | solo_dev->i2c_state = IIC_STATE_IDLE; | |
286 | init_waitqueue_head(&solo_dev->i2c_wait); | |
f62de9be | 287 | mutex_init(&solo_dev->i2c_mutex); |
faa4fd2a BC |
288 | |
289 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { | |
290 | struct i2c_adapter *adap = &solo_dev->i2c_adap[i]; | |
291 | ||
292 | snprintf(adap->name, I2C_NAME_SIZE, "%s I2C %d", | |
293 | SOLO6010_NAME, i); | |
294 | adap->algo = &solo_i2c_algo; | |
295 | adap->algo_data = solo_dev; | |
296 | adap->retries = 1; | |
297 | adap->dev.parent = &solo_dev->pdev->dev; | |
298 | ||
afabbe6d | 299 | ret = i2c_add_adapter(adap); |
300 | if (ret) { | |
faa4fd2a BC |
301 | adap->algo_data = NULL; |
302 | break; | |
303 | } | |
304 | } | |
305 | ||
306 | if (ret) { | |
307 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { | |
308 | if (!solo_dev->i2c_adap[i].algo_data) | |
309 | break; | |
310 | i2c_del_adapter(&solo_dev->i2c_adap[i]); | |
311 | solo_dev->i2c_adap[i].algo_data = NULL; | |
312 | } | |
313 | return ret; | |
314 | } | |
315 | ||
316 | dev_info(&solo_dev->pdev->dev, "Enabled %d i2c adapters\n", | |
317 | SOLO_I2C_ADAPTERS); | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | void solo_i2c_exit(struct solo6010_dev *solo_dev) | |
323 | { | |
324 | int i; | |
325 | ||
326 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { | |
327 | if (!solo_dev->i2c_adap[i].algo_data) | |
328 | continue; | |
329 | i2c_del_adapter(&solo_dev->i2c_adap[i]); | |
330 | solo_dev->i2c_adap[i].algo_data = NULL; | |
331 | } | |
332 | } |