Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / saa7134 / saa7134-i2c.c
blobcf1e526de56ac0fadd1c75b05b964481d8ec3e09
1 /*
3 * device driver for philips saa7134 based TV cards
4 * i2c interface support
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
19 #include "saa7134.h"
20 #include "saa7134-reg.h"
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
28 #include <media/v4l2-common.h>
30 /* ----------------------------------------------------------- */
32 static unsigned int i2c_debug;
33 module_param(i2c_debug, int, 0644);
34 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
36 static unsigned int i2c_scan;
37 module_param(i2c_scan, int, 0444);
38 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
40 #define i2c_dbg(level, fmt, arg...) do { \
41 if (i2c_debug == level) \
42 printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \
43 } while (0)
45 #define i2c_cont(level, fmt, arg...) do { \
46 if (i2c_debug == level) \
47 pr_cont(fmt, ## arg); \
48 } while (0)
50 #define I2C_WAIT_DELAY 32
51 #define I2C_WAIT_RETRY 16
53 /* ----------------------------------------------------------- */
55 static char *str_i2c_status[] = {
56 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
57 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
58 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
61 enum i2c_status {
62 IDLE = 0, // no I2C command pending
63 DONE_STOP = 1, // I2C command done and STOP executed
64 BUSY = 2, // executing I2C command
65 TO_SCL = 3, // executing I2C command, time out on clock stretching
66 TO_ARB = 4, // time out on arbitration trial, still trying
67 DONE_WRITE = 5, // I2C command done and awaiting next write command
68 DONE_READ = 6, // I2C command done and awaiting next read command
69 DONE_WRITE_TO = 7, // see 5, and time out on status echo
70 DONE_READ_TO = 8, // see 6, and time out on status echo
71 NO_DEVICE = 9, // no acknowledge on device slave address
72 NO_ACKN = 10, // no acknowledge after data byte transfer
73 BUS_ERR = 11, // bus error
74 ARB_LOST = 12, // arbitration lost during transfer
75 SEQ_ERR = 13, // erroneous programming sequence
76 ST_ERR = 14, // wrong status echoing
77 SW_ERR = 15 // software error
80 static char *str_i2c_attr[] = {
81 "NOP", "STOP", "CONTINUE", "START"
84 enum i2c_attr {
85 NOP = 0, // no operation on I2C bus
86 STOP = 1, // stop condition, no associated byte transfer
87 CONTINUE = 2, // continue with byte transfer
88 START = 3 // start condition with byte transfer
91 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
93 enum i2c_status status;
95 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
96 i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]);
97 return status;
100 static inline void i2c_set_status(struct saa7134_dev *dev,
101 enum i2c_status status)
103 i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]);
104 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
107 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
109 i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]);
110 saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
113 static inline int i2c_is_error(enum i2c_status status)
115 switch (status) {
116 case NO_DEVICE:
117 case NO_ACKN:
118 case BUS_ERR:
119 case ARB_LOST:
120 case SEQ_ERR:
121 case ST_ERR:
122 return true;
123 default:
124 return false;
128 static inline int i2c_is_idle(enum i2c_status status)
130 switch (status) {
131 case IDLE:
132 case DONE_STOP:
133 return true;
134 default:
135 return false;
139 static inline int i2c_is_busy(enum i2c_status status)
141 switch (status) {
142 case BUSY:
143 case TO_SCL:
144 case TO_ARB:
145 return true;
146 default:
147 return false;
151 static int i2c_is_busy_wait(struct saa7134_dev *dev)
153 enum i2c_status status;
154 int count;
156 for (count = 0; count < I2C_WAIT_RETRY; count++) {
157 status = i2c_get_status(dev);
158 if (!i2c_is_busy(status))
159 break;
160 saa_wait(I2C_WAIT_DELAY);
162 if (I2C_WAIT_RETRY == count)
163 return false;
164 return true;
167 static int i2c_reset(struct saa7134_dev *dev)
169 enum i2c_status status;
170 int count;
172 i2c_dbg(2, "i2c reset\n");
173 status = i2c_get_status(dev);
174 if (!i2c_is_error(status))
175 return true;
176 i2c_set_status(dev,status);
178 for (count = 0; count < I2C_WAIT_RETRY; count++) {
179 status = i2c_get_status(dev);
180 if (!i2c_is_error(status))
181 break;
182 udelay(I2C_WAIT_DELAY);
184 if (I2C_WAIT_RETRY == count)
185 return false;
187 if (!i2c_is_idle(status))
188 return false;
190 i2c_set_attr(dev,NOP);
191 return true;
194 static inline int i2c_send_byte(struct saa7134_dev *dev,
195 enum i2c_attr attr,
196 unsigned char data)
198 enum i2c_status status;
199 __u32 dword;
201 /* have to write both attr + data in one 32bit word */
202 dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
203 dword &= 0x0f;
204 dword |= (attr << 6);
205 dword |= ((__u32)data << 8);
206 dword |= 0x00 << 16; /* 100 kHz */
207 // dword |= 0x40 << 16; /* 400 kHz */
208 dword |= 0xf0 << 24;
209 saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
210 i2c_dbg(2, "i2c data => 0x%x\n", data);
212 if (!i2c_is_busy_wait(dev))
213 return -EIO;
214 status = i2c_get_status(dev);
215 if (i2c_is_error(status))
216 return -EIO;
217 return 0;
220 static inline int i2c_recv_byte(struct saa7134_dev *dev)
222 enum i2c_status status;
223 unsigned char data;
225 i2c_set_attr(dev,CONTINUE);
226 if (!i2c_is_busy_wait(dev))
227 return -EIO;
228 status = i2c_get_status(dev);
229 if (i2c_is_error(status))
230 return -EIO;
231 data = saa_readb(SAA7134_I2C_DATA);
232 i2c_dbg(2, "i2c data <= 0x%x\n", data);
233 return data;
236 static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
237 struct i2c_msg *msgs, int num)
239 struct saa7134_dev *dev = i2c_adap->algo_data;
240 enum i2c_status status;
241 unsigned char data;
242 int addr,rc,i,byte;
244 status = i2c_get_status(dev);
245 if (!i2c_is_idle(status))
246 if (!i2c_reset(dev))
247 return -EIO;
249 i2c_dbg(2, "start xfer\n");
250 i2c_dbg(1, "i2c xfer:");
251 for (i = 0; i < num; i++) {
252 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
253 /* send address */
254 i2c_dbg(2, "send address\n");
255 addr = msgs[i].addr << 1;
256 if (msgs[i].flags & I2C_M_RD)
257 addr |= 1;
258 if (i > 0 && msgs[i].flags &
259 I2C_M_RD && msgs[i].addr != 0x40 &&
260 msgs[i].addr != 0x41 &&
261 msgs[i].addr != 0x19) {
262 /* workaround for a saa7134 i2c bug
263 * needed to talk to the mt352 demux
264 * thanks to pinnacle for the hint */
265 int quirk = 0xfe;
266 i2c_cont(1, " [%02x quirk]", quirk);
267 i2c_send_byte(dev,START,quirk);
268 i2c_recv_byte(dev);
270 i2c_cont(1, " < %02x", addr);
271 rc = i2c_send_byte(dev,START,addr);
272 if (rc < 0)
273 goto err;
275 if (msgs[i].flags & I2C_M_RD) {
276 /* read bytes */
277 i2c_dbg(2, "read bytes\n");
278 for (byte = 0; byte < msgs[i].len; byte++) {
279 i2c_cont(1, " =");
280 rc = i2c_recv_byte(dev);
281 if (rc < 0)
282 goto err;
283 i2c_cont(1, "%02x", rc);
284 msgs[i].buf[byte] = rc;
286 /* discard mysterious extra byte when reading
287 from Samsung S5H1411. i2c bus gets error
288 if we do not. */
289 if (0x19 == msgs[i].addr) {
290 i2c_cont(1, " ?");
291 rc = i2c_recv_byte(dev);
292 if (rc < 0)
293 goto err;
294 i2c_cont(1, "%02x", rc);
296 } else {
297 /* write bytes */
298 i2c_dbg(2, "write bytes\n");
299 for (byte = 0; byte < msgs[i].len; byte++) {
300 data = msgs[i].buf[byte];
301 i2c_cont(1, " %02x", data);
302 rc = i2c_send_byte(dev,CONTINUE,data);
303 if (rc < 0)
304 goto err;
308 i2c_dbg(2, "xfer done\n");
309 i2c_cont(1, " >");
310 i2c_set_attr(dev,STOP);
311 rc = -EIO;
312 if (!i2c_is_busy_wait(dev))
313 goto err;
314 status = i2c_get_status(dev);
315 if (i2c_is_error(status))
316 goto err;
317 /* ensure that the bus is idle for at least one bit slot */
318 msleep(1);
320 i2c_cont(1, "\n");
321 return num;
322 err:
323 if (1 == i2c_debug) {
324 status = i2c_get_status(dev);
325 i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]);
327 return rc;
330 /* ----------------------------------------------------------- */
332 static u32 functionality(struct i2c_adapter *adap)
334 return I2C_FUNC_SMBUS_EMUL;
337 static const struct i2c_algorithm saa7134_algo = {
338 .master_xfer = saa7134_i2c_xfer,
339 .functionality = functionality,
342 static const struct i2c_adapter saa7134_adap_template = {
343 .owner = THIS_MODULE,
344 .name = "saa7134",
345 .algo = &saa7134_algo,
348 static const struct i2c_client saa7134_client_template = {
349 .name = "saa7134 internal",
352 /* ----------------------------------------------------------- */
354 /* On Medion 7134 reading EEPROM needs DVB-T demod i2c gate open */
355 static void saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev *dev)
357 u8 subaddr = 0x7, dmdregval;
358 u8 data[2];
359 int ret;
360 struct i2c_msg i2cgatemsg_r[] = { {.addr = 0x08, .flags = 0,
361 .buf = &subaddr, .len = 1},
362 {.addr = 0x08,
363 .flags = I2C_M_RD,
364 .buf = &dmdregval, .len = 1}
366 struct i2c_msg i2cgatemsg_w[] = { {.addr = 0x08, .flags = 0,
367 .buf = data, .len = 2} };
369 ret = i2c_transfer(&dev->i2c_adap, i2cgatemsg_r, 2);
370 if ((ret == 2) && (dmdregval & 0x2)) {
371 pr_debug("%s: DVB-T demod i2c gate was left closed\n",
372 dev->name);
374 data[0] = subaddr;
375 data[1] = (dmdregval & ~0x2);
376 if (i2c_transfer(&dev->i2c_adap, i2cgatemsg_w, 1) != 1)
377 pr_err("%s: EEPROM i2c gate open failure\n",
378 dev->name);
382 static int
383 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
385 unsigned char buf;
386 int i,err;
388 if (dev->board == SAA7134_BOARD_MD7134)
389 saa7134_i2c_eeprom_md7134_gate(dev);
391 dev->i2c_client.addr = 0xa0 >> 1;
392 buf = 0;
393 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
394 pr_info("%s: Huh, no eeprom present (err=%d)?\n",
395 dev->name,err);
396 return -1;
398 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
399 pr_warn("%s: i2c eeprom read error (err=%d)\n",
400 dev->name,err);
401 return -1;
404 for (i = 0; i < len; i += 16) {
405 int size = (len - i) > 16 ? 16 : len - i;
407 pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]);
410 return 0;
413 static char *i2c_devs[128] = {
414 [ 0x20 ] = "mpeg encoder (saa6752hs)",
415 [ 0xa0 >> 1 ] = "eeprom",
416 [ 0xc0 >> 1 ] = "tuner (analog)",
417 [ 0x86 >> 1 ] = "tda9887",
418 [ 0x5a >> 1 ] = "remote control",
421 static void do_i2c_scan(struct i2c_client *c)
423 unsigned char buf;
424 int i,rc;
426 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
427 c->addr = i;
428 rc = i2c_master_recv(c,&buf,0);
429 if (rc < 0)
430 continue;
431 pr_info("i2c scan: found device @ 0x%x [%s]\n",
432 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
436 int saa7134_i2c_register(struct saa7134_dev *dev)
438 dev->i2c_adap = saa7134_adap_template;
439 dev->i2c_adap.dev.parent = &dev->pci->dev;
440 strcpy(dev->i2c_adap.name,dev->name);
441 dev->i2c_adap.algo_data = dev;
442 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
443 i2c_add_adapter(&dev->i2c_adap);
445 dev->i2c_client = saa7134_client_template;
446 dev->i2c_client.adapter = &dev->i2c_adap;
448 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
449 if (i2c_scan)
450 do_i2c_scan(&dev->i2c_client);
452 /* Instantiate the IR receiver device, if present */
453 saa7134_probe_i2c_ir(dev);
454 return 0;
457 int saa7134_i2c_unregister(struct saa7134_dev *dev)
459 i2c_del_adapter(&dev->i2c_adap);
460 return 0;