xref: /linux/drivers/media/pci/saa7134/saa7134-i2c.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
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 
19 #include "saa7134.h"
20 #include "saa7134-reg.h"
21 
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>
27 
28 #include <media/v4l2-common.h>
29 
30 /* ----------------------------------------------------------- */
31 
32 static unsigned int i2c_debug;
33 module_param(i2c_debug, int, 0644);
34 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
35 
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");
39 
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)
44 
45 #define i2c_cont(level, fmt, arg...) do { \
46 	if (i2c_debug == level) \
47 		pr_cont(fmt, ## arg); \
48 	} while (0)
49 
50 #define I2C_WAIT_DELAY  32
51 #define I2C_WAIT_RETRY  16
52 
53 /* ----------------------------------------------------------- */
54 
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"
59 };
60 
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
78 };
79 
80 static char *str_i2c_attr[] = {
81 	"NOP", "STOP", "CONTINUE", "START"
82 };
83 
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
89 };
90 
91 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
92 {
93 	enum i2c_status status;
94 
95 	status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
96 	i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]);
97 	return status;
98 }
99 
100 static inline void i2c_set_status(struct saa7134_dev *dev,
101 				  enum i2c_status status)
102 {
103 	i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]);
104 	saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
105 }
106 
107 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
108 {
109 	i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]);
110 	saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
111 }
112 
113 static inline int i2c_is_error(enum i2c_status status)
114 {
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;
125 	}
126 }
127 
128 static inline int i2c_is_idle(enum i2c_status status)
129 {
130 	switch (status) {
131 	case IDLE:
132 	case DONE_STOP:
133 		return true;
134 	default:
135 		return false;
136 	}
137 }
138 
139 static inline int i2c_is_busy(enum i2c_status status)
140 {
141 	switch (status) {
142 	case BUSY:
143 	case TO_SCL:
144 	case TO_ARB:
145 		return true;
146 	default:
147 		return false;
148 	}
149 }
150 
151 static int i2c_is_busy_wait(struct saa7134_dev *dev)
152 {
153 	enum i2c_status status;
154 	int count;
155 
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);
161 	}
162 	if (I2C_WAIT_RETRY == count)
163 		return false;
164 	return true;
165 }
166 
167 static int i2c_reset(struct saa7134_dev *dev)
168 {
169 	enum i2c_status status;
170 	int count;
171 
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);
177 
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);
183 	}
184 	if (I2C_WAIT_RETRY == count)
185 		return false;
186 
187 	if (!i2c_is_idle(status))
188 		return false;
189 
190 	i2c_set_attr(dev,NOP);
191 	return true;
192 }
193 
194 static inline int i2c_send_byte(struct saa7134_dev *dev,
195 				enum i2c_attr attr,
196 				unsigned char data)
197 {
198 	enum i2c_status status;
199 	__u32 dword;
200 
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);
211 
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;
218 }
219 
220 static inline int i2c_recv_byte(struct saa7134_dev *dev)
221 {
222 	enum i2c_status status;
223 	unsigned char data;
224 
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;
234 }
235 
236 static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
237 			    struct i2c_msg *msgs, int num)
238 {
239 	struct saa7134_dev *dev = i2c_adap->algo_data;
240 	enum i2c_status status;
241 	unsigned char data;
242 	int addr,rc,i,byte;
243 
244 	status = i2c_get_status(dev);
245 	if (!i2c_is_idle(status))
246 		if (!i2c_reset(dev))
247 			return -EIO;
248 
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);
269 			}
270 			i2c_cont(1, " < %02x", addr);
271 			rc = i2c_send_byte(dev,START,addr);
272 			if (rc < 0)
273 				 goto err;
274 		}
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;
285 			}
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);
295 			}
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;
305 			}
306 		}
307 	}
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);
319 
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]);
326 	}
327 	return rc;
328 }
329 
330 /* ----------------------------------------------------------- */
331 
332 static u32 functionality(struct i2c_adapter *adap)
333 {
334 	return I2C_FUNC_SMBUS_EMUL;
335 }
336 
337 static const struct i2c_algorithm saa7134_algo = {
338 	.master_xfer   = saa7134_i2c_xfer,
339 	.functionality = functionality,
340 };
341 
342 static struct i2c_adapter saa7134_adap_template = {
343 	.owner         = THIS_MODULE,
344 	.name          = "saa7134",
345 	.algo          = &saa7134_algo,
346 };
347 
348 static struct i2c_client saa7134_client_template = {
349 	.name	= "saa7134 internal",
350 };
351 
352 /* ----------------------------------------------------------- */
353 
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)
356 {
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}
365 					};
366 	struct i2c_msg i2cgatemsg_w[] = { {.addr = 0x08, .flags = 0,
367 					   .buf = data, .len = 2} };
368 
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);
373 
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);
379 	}
380 }
381 
382 static int
383 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
384 {
385 	unsigned char buf;
386 	int i,err;
387 
388 	if (dev->board == SAA7134_BOARD_MD7134)
389 		saa7134_i2c_eeprom_md7134_gate(dev);
390 
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;
397 	}
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;
402 	}
403 
404 	for (i = 0; i < len; i += 16) {
405 		int size = (len - i) > 16 ? 16 : len - i;
406 
407 		pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]);
408 	}
409 
410 	return 0;
411 }
412 
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",
419 };
420 
421 static void do_i2c_scan(struct i2c_client *c)
422 {
423 	unsigned char buf;
424 	int i,rc;
425 
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] : "???");
433 	}
434 }
435 
436 int saa7134_i2c_register(struct saa7134_dev *dev)
437 {
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);
444 
445 	dev->i2c_client = saa7134_client_template;
446 	dev->i2c_client.adapter = &dev->i2c_adap;
447 
448 	saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
449 	if (i2c_scan)
450 		do_i2c_scan(&dev->i2c_client);
451 
452 	/* Instantiate the IR receiver device, if present */
453 	saa7134_probe_i2c_ir(dev);
454 	return 0;
455 }
456 
457 int saa7134_i2c_unregister(struct saa7134_dev *dev)
458 {
459 	i2c_del_adapter(&dev->i2c_adap);
460 	return 0;
461 }
462