Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * |
3 | * device driver for philips saa7134 based TV cards | |
4 | * i2c interface support | |
5 | * | |
6 | * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
9a12ccfc MCC |
23 | #include "saa7134.h" |
24 | #include "saa7134-reg.h" | |
25 | ||
1da177e4 LT |
26 | #include <linux/init.h> |
27 | #include <linux/list.h> | |
28 | #include <linux/module.h> | |
1da177e4 | 29 | #include <linux/kernel.h> |
1da177e4 LT |
30 | #include <linux/delay.h> |
31 | ||
5e453dc7 | 32 | #include <media/v4l2-common.h> |
1da177e4 LT |
33 | |
34 | /* ----------------------------------------------------------- */ | |
35 | ||
ff699e6b | 36 | static unsigned int i2c_debug; |
1da177e4 LT |
37 | module_param(i2c_debug, int, 0644); |
38 | MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]"); | |
39 | ||
ff699e6b | 40 | static unsigned int i2c_scan; |
1da177e4 LT |
41 | module_param(i2c_scan, int, 0444); |
42 | MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time"); | |
43 | ||
45f38cb3 MCC |
44 | #define i2c_dbg(level, fmt, arg...) do { \ |
45 | if (i2c_debug == level) \ | |
46 | printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \ | |
47 | } while (0) | |
48 | ||
49 | #define i2c_cont(level, fmt, arg...) do { \ | |
50 | if (i2c_debug == level) \ | |
51 | pr_cont(fmt, ## arg); \ | |
52 | } while (0) | |
1da177e4 LT |
53 | |
54 | #define I2C_WAIT_DELAY 32 | |
55 | #define I2C_WAIT_RETRY 16 | |
56 | ||
57 | /* ----------------------------------------------------------- */ | |
58 | ||
59 | static char *str_i2c_status[] = { | |
60 | "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE", | |
61 | "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE", | |
62 | "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR" | |
63 | }; | |
64 | ||
65 | enum i2c_status { | |
66 | IDLE = 0, // no I2C command pending | |
67 | DONE_STOP = 1, // I2C command done and STOP executed | |
68 | BUSY = 2, // executing I2C command | |
69 | TO_SCL = 3, // executing I2C command, time out on clock stretching | |
70 | TO_ARB = 4, // time out on arbitration trial, still trying | |
71 | DONE_WRITE = 5, // I2C command done and awaiting next write command | |
72 | DONE_READ = 6, // I2C command done and awaiting next read command | |
73 | DONE_WRITE_TO = 7, // see 5, and time out on status echo | |
74 | DONE_READ_TO = 8, // see 6, and time out on status echo | |
75 | NO_DEVICE = 9, // no acknowledge on device slave address | |
76 | NO_ACKN = 10, // no acknowledge after data byte transfer | |
77 | BUS_ERR = 11, // bus error | |
78 | ARB_LOST = 12, // arbitration lost during transfer | |
79 | SEQ_ERR = 13, // erroneous programming sequence | |
80 | ST_ERR = 14, // wrong status echoing | |
81 | SW_ERR = 15 // software error | |
82 | }; | |
83 | ||
84 | static char *str_i2c_attr[] = { | |
85 | "NOP", "STOP", "CONTINUE", "START" | |
86 | }; | |
87 | ||
88 | enum i2c_attr { | |
89 | NOP = 0, // no operation on I2C bus | |
90 | STOP = 1, // stop condition, no associated byte transfer | |
91 | CONTINUE = 2, // continue with byte transfer | |
92 | START = 3 // start condition with byte transfer | |
93 | }; | |
94 | ||
95 | static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev) | |
96 | { | |
97 | enum i2c_status status; | |
98 | ||
99 | status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f; | |
30693f34 | 100 | i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]); |
1da177e4 LT |
101 | return status; |
102 | } | |
103 | ||
104 | static inline void i2c_set_status(struct saa7134_dev *dev, | |
105 | enum i2c_status status) | |
106 | { | |
30693f34 | 107 | i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]); |
1da177e4 LT |
108 | saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status); |
109 | } | |
110 | ||
111 | static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr) | |
112 | { | |
30693f34 | 113 | i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]); |
1da177e4 LT |
114 | saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6); |
115 | } | |
116 | ||
117 | static inline int i2c_is_error(enum i2c_status status) | |
118 | { | |
119 | switch (status) { | |
120 | case NO_DEVICE: | |
121 | case NO_ACKN: | |
122 | case BUS_ERR: | |
123 | case ARB_LOST: | |
124 | case SEQ_ERR: | |
125 | case ST_ERR: | |
e8be02a3 | 126 | return true; |
1da177e4 | 127 | default: |
e8be02a3 | 128 | return false; |
1da177e4 LT |
129 | } |
130 | } | |
131 | ||
132 | static inline int i2c_is_idle(enum i2c_status status) | |
133 | { | |
134 | switch (status) { | |
135 | case IDLE: | |
136 | case DONE_STOP: | |
e8be02a3 | 137 | return true; |
1da177e4 | 138 | default: |
e8be02a3 | 139 | return false; |
1da177e4 LT |
140 | } |
141 | } | |
142 | ||
143 | static inline int i2c_is_busy(enum i2c_status status) | |
144 | { | |
145 | switch (status) { | |
146 | case BUSY: | |
8fb737b7 DB |
147 | case TO_SCL: |
148 | case TO_ARB: | |
e8be02a3 | 149 | return true; |
1da177e4 | 150 | default: |
e8be02a3 | 151 | return false; |
1da177e4 LT |
152 | } |
153 | } | |
154 | ||
155 | static int i2c_is_busy_wait(struct saa7134_dev *dev) | |
156 | { | |
157 | enum i2c_status status; | |
158 | int count; | |
159 | ||
160 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
161 | status = i2c_get_status(dev); | |
162 | if (!i2c_is_busy(status)) | |
163 | break; | |
164 | saa_wait(I2C_WAIT_DELAY); | |
165 | } | |
166 | if (I2C_WAIT_RETRY == count) | |
e8be02a3 RK |
167 | return false; |
168 | return true; | |
1da177e4 LT |
169 | } |
170 | ||
171 | static int i2c_reset(struct saa7134_dev *dev) | |
172 | { | |
173 | enum i2c_status status; | |
174 | int count; | |
175 | ||
30693f34 | 176 | i2c_dbg(2, "i2c reset\n"); |
1da177e4 LT |
177 | status = i2c_get_status(dev); |
178 | if (!i2c_is_error(status)) | |
e8be02a3 | 179 | return true; |
1da177e4 LT |
180 | i2c_set_status(dev,status); |
181 | ||
182 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | |
183 | status = i2c_get_status(dev); | |
184 | if (!i2c_is_error(status)) | |
185 | break; | |
186 | udelay(I2C_WAIT_DELAY); | |
187 | } | |
188 | if (I2C_WAIT_RETRY == count) | |
e8be02a3 | 189 | return false; |
1da177e4 LT |
190 | |
191 | if (!i2c_is_idle(status)) | |
e8be02a3 | 192 | return false; |
1da177e4 LT |
193 | |
194 | i2c_set_attr(dev,NOP); | |
e8be02a3 | 195 | return true; |
1da177e4 LT |
196 | } |
197 | ||
198 | static inline int i2c_send_byte(struct saa7134_dev *dev, | |
199 | enum i2c_attr attr, | |
200 | unsigned char data) | |
201 | { | |
202 | enum i2c_status status; | |
203 | __u32 dword; | |
204 | ||
1da177e4 LT |
205 | /* have to write both attr + data in one 32bit word */ |
206 | dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2); | |
207 | dword &= 0x0f; | |
208 | dword |= (attr << 6); | |
209 | dword |= ((__u32)data << 8); | |
210 | dword |= 0x00 << 16; /* 100 kHz */ | |
211 | // dword |= 0x40 << 16; /* 400 kHz */ | |
212 | dword |= 0xf0 << 24; | |
213 | saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword); | |
30693f34 | 214 | i2c_dbg(2, "i2c data => 0x%x\n", data); |
1da177e4 LT |
215 | |
216 | if (!i2c_is_busy_wait(dev)) | |
217 | return -EIO; | |
218 | status = i2c_get_status(dev); | |
219 | if (i2c_is_error(status)) | |
220 | return -EIO; | |
221 | return 0; | |
222 | } | |
223 | ||
224 | static inline int i2c_recv_byte(struct saa7134_dev *dev) | |
225 | { | |
226 | enum i2c_status status; | |
227 | unsigned char data; | |
228 | ||
229 | i2c_set_attr(dev,CONTINUE); | |
230 | if (!i2c_is_busy_wait(dev)) | |
231 | return -EIO; | |
232 | status = i2c_get_status(dev); | |
233 | if (i2c_is_error(status)) | |
234 | return -EIO; | |
235 | data = saa_readb(SAA7134_I2C_DATA); | |
30693f34 | 236 | i2c_dbg(2, "i2c data <= 0x%x\n", data); |
1da177e4 LT |
237 | return data; |
238 | } | |
239 | ||
240 | static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap, | |
241 | struct i2c_msg *msgs, int num) | |
242 | { | |
243 | struct saa7134_dev *dev = i2c_adap->algo_data; | |
244 | enum i2c_status status; | |
245 | unsigned char data; | |
246 | int addr,rc,i,byte; | |
247 | ||
4ac97914 | 248 | status = i2c_get_status(dev); |
1da177e4 LT |
249 | if (!i2c_is_idle(status)) |
250 | if (!i2c_reset(dev)) | |
251 | return -EIO; | |
252 | ||
30693f34 MCC |
253 | i2c_dbg(2, "start xfer\n"); |
254 | i2c_dbg(1, "i2c xfer:"); | |
1da177e4 LT |
255 | for (i = 0; i < num; i++) { |
256 | if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) { | |
257 | /* send address */ | |
30693f34 | 258 | i2c_dbg(2, "send address\n"); |
1da177e4 LT |
259 | addr = msgs[i].addr << 1; |
260 | if (msgs[i].flags & I2C_M_RD) | |
261 | addr |= 1; | |
25fa2071 KS |
262 | if (i > 0 && msgs[i].flags & |
263 | I2C_M_RD && msgs[i].addr != 0x40 && | |
34fe2784 | 264 | msgs[i].addr != 0x41 && |
25fa2071 | 265 | msgs[i].addr != 0x19) { |
1da177e4 LT |
266 | /* workaround for a saa7134 i2c bug |
267 | * needed to talk to the mt352 demux | |
268 | * thanks to pinnacle for the hint */ | |
b9dcdb6f | 269 | int quirk = 0xfe; |
6139ebc6 | 270 | i2c_cont(1, " [%02x quirk]", quirk); |
1da177e4 LT |
271 | i2c_send_byte(dev,START,quirk); |
272 | i2c_recv_byte(dev); | |
273 | } | |
30693f34 | 274 | i2c_cont(1, " < %02x", addr); |
1da177e4 LT |
275 | rc = i2c_send_byte(dev,START,addr); |
276 | if (rc < 0) | |
277 | goto err; | |
278 | } | |
279 | if (msgs[i].flags & I2C_M_RD) { | |
280 | /* read bytes */ | |
30693f34 | 281 | i2c_dbg(2, "read bytes\n"); |
1da177e4 | 282 | for (byte = 0; byte < msgs[i].len; byte++) { |
30693f34 | 283 | i2c_cont(1, " ="); |
1da177e4 LT |
284 | rc = i2c_recv_byte(dev); |
285 | if (rc < 0) | |
286 | goto err; | |
30693f34 | 287 | i2c_cont(1, "%02x", rc); |
1da177e4 LT |
288 | msgs[i].buf[byte] = rc; |
289 | } | |
25fa2071 KS |
290 | /* discard mysterious extra byte when reading |
291 | from Samsung S5H1411. i2c bus gets error | |
292 | if we do not. */ | |
293 | if (0x19 == msgs[i].addr) { | |
30693f34 | 294 | i2c_cont(1, " ?"); |
25fa2071 KS |
295 | rc = i2c_recv_byte(dev); |
296 | if (rc < 0) | |
297 | goto err; | |
30693f34 | 298 | i2c_cont(1, "%02x", rc); |
25fa2071 | 299 | } |
1da177e4 LT |
300 | } else { |
301 | /* write bytes */ | |
30693f34 | 302 | i2c_dbg(2, "write bytes\n"); |
1da177e4 LT |
303 | for (byte = 0; byte < msgs[i].len; byte++) { |
304 | data = msgs[i].buf[byte]; | |
30693f34 | 305 | i2c_cont(1, " %02x", data); |
1da177e4 LT |
306 | rc = i2c_send_byte(dev,CONTINUE,data); |
307 | if (rc < 0) | |
308 | goto err; | |
309 | } | |
310 | } | |
311 | } | |
30693f34 MCC |
312 | i2c_dbg(2, "xfer done\n"); |
313 | i2c_cont(1, " >"); | |
1da177e4 LT |
314 | i2c_set_attr(dev,STOP); |
315 | rc = -EIO; | |
316 | if (!i2c_is_busy_wait(dev)) | |
317 | goto err; | |
4ac97914 | 318 | status = i2c_get_status(dev); |
1da177e4 LT |
319 | if (i2c_is_error(status)) |
320 | goto err; | |
fd3113e8 MCC |
321 | /* ensure that the bus is idle for at least one bit slot */ |
322 | msleep(1); | |
1da177e4 | 323 | |
30693f34 | 324 | i2c_cont(1, "\n"); |
1da177e4 LT |
325 | return num; |
326 | err: | |
327 | if (1 == i2c_debug) { | |
328 | status = i2c_get_status(dev); | |
30693f34 | 329 | i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]); |
1da177e4 LT |
330 | } |
331 | return rc; | |
332 | } | |
333 | ||
334 | /* ----------------------------------------------------------- */ | |
335 | ||
1da177e4 LT |
336 | static u32 functionality(struct i2c_adapter *adap) |
337 | { | |
338 | return I2C_FUNC_SMBUS_EMUL; | |
339 | } | |
340 | ||
1da177e4 | 341 | static struct i2c_algorithm saa7134_algo = { |
1da177e4 | 342 | .master_xfer = saa7134_i2c_xfer, |
1da177e4 LT |
343 | .functionality = functionality, |
344 | }; | |
345 | ||
346 | static struct i2c_adapter saa7134_adap_template = { | |
347 | .owner = THIS_MODULE, | |
fae91e72 | 348 | .name = "saa7134", |
1da177e4 | 349 | .algo = &saa7134_algo, |
1da177e4 LT |
350 | }; |
351 | ||
352 | static struct i2c_client saa7134_client_template = { | |
fae91e72 | 353 | .name = "saa7134 internal", |
1da177e4 LT |
354 | }; |
355 | ||
356 | /* ----------------------------------------------------------- */ | |
357 | ||
358 | static int | |
359 | saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len) | |
360 | { | |
361 | unsigned char buf; | |
362 | int i,err; | |
363 | ||
364 | dev->i2c_client.addr = 0xa0 >> 1; | |
365 | buf = 0; | |
366 | if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) { | |
83582009 | 367 | pr_info("%s: Huh, no eeprom present (err=%d)?\n", |
1da177e4 LT |
368 | dev->name,err); |
369 | return -1; | |
370 | } | |
371 | if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) { | |
83582009 | 372 | pr_warn("%s: i2c eeprom read error (err=%d)\n", |
1da177e4 LT |
373 | dev->name,err); |
374 | return -1; | |
375 | } | |
3e0051c5 | 376 | |
6139ebc6 | 377 | for (i = 0; i < len; i += 16) { |
3e0051c5 | 378 | int size = (len - i) > 16 ? 16 : len - i; |
6139ebc6 | 379 | |
3e0051c5 | 380 | pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]); |
1da177e4 | 381 | } |
3e0051c5 | 382 | |
1da177e4 LT |
383 | return 0; |
384 | } | |
385 | ||
386 | static char *i2c_devs[128] = { | |
387 | [ 0x20 ] = "mpeg encoder (saa6752hs)", | |
388 | [ 0xa0 >> 1 ] = "eeprom", | |
389 | [ 0xc0 >> 1 ] = "tuner (analog)", | |
390 | [ 0x86 >> 1 ] = "tda9887", | |
e8018c9e | 391 | [ 0x5a >> 1 ] = "remote control", |
1da177e4 LT |
392 | }; |
393 | ||
2bb3e2ee | 394 | static void do_i2c_scan(struct i2c_client *c) |
1da177e4 LT |
395 | { |
396 | unsigned char buf; | |
397 | int i,rc; | |
398 | ||
53c4e955 | 399 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
1da177e4 LT |
400 | c->addr = i; |
401 | rc = i2c_master_recv(c,&buf,0); | |
402 | if (rc < 0) | |
403 | continue; | |
2bb3e2ee MCC |
404 | pr_info("i2c scan: found device @ 0x%x [%s]\n", |
405 | i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); | |
1da177e4 LT |
406 | } |
407 | } | |
408 | ||
1da177e4 LT |
409 | int saa7134_i2c_register(struct saa7134_dev *dev) |
410 | { | |
411 | dev->i2c_adap = saa7134_adap_template; | |
412 | dev->i2c_adap.dev.parent = &dev->pci->dev; | |
413 | strcpy(dev->i2c_adap.name,dev->name); | |
414 | dev->i2c_adap.algo_data = dev; | |
fac6986c | 415 | i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); |
1da177e4 LT |
416 | i2c_add_adapter(&dev->i2c_adap); |
417 | ||
418 | dev->i2c_client = saa7134_client_template; | |
419 | dev->i2c_client.adapter = &dev->i2c_adap; | |
420 | ||
421 | saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata)); | |
422 | if (i2c_scan) | |
2bb3e2ee | 423 | do_i2c_scan(&dev->i2c_client); |
c668f32d JD |
424 | |
425 | /* Instantiate the IR receiver device, if present */ | |
426 | saa7134_probe_i2c_ir(dev); | |
1da177e4 LT |
427 | return 0; |
428 | } | |
429 | ||
430 | int saa7134_i2c_unregister(struct saa7134_dev *dev) | |
431 | { | |
432 | i2c_del_adapter(&dev->i2c_adap); | |
433 | return 0; | |
434 | } |