xref: /linux/drivers/media/pci/saa7134/saa7134-i2c.c (revision 976e3645923bdd2fe7893aae33fd7a21098bfb28)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  * device driver for philips saa7134 based TV cards
5b285192aSMauro Carvalho Chehab  * i2c interface support
6b285192aSMauro Carvalho Chehab  *
7b285192aSMauro Carvalho Chehab  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8b285192aSMauro Carvalho Chehab  */
9b285192aSMauro Carvalho Chehab 
109a12ccfcSMauro Carvalho Chehab #include "saa7134.h"
119a12ccfcSMauro Carvalho Chehab #include "saa7134-reg.h"
129a12ccfcSMauro Carvalho Chehab 
13b285192aSMauro Carvalho Chehab #include <linux/init.h>
14b285192aSMauro Carvalho Chehab #include <linux/list.h>
15b285192aSMauro Carvalho Chehab #include <linux/module.h>
16b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
17b285192aSMauro Carvalho Chehab #include <linux/delay.h>
18b285192aSMauro Carvalho Chehab 
19b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
20b285192aSMauro Carvalho Chehab 
21b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
22b285192aSMauro Carvalho Chehab 
23b285192aSMauro Carvalho Chehab static unsigned int i2c_debug;
24b285192aSMauro Carvalho Chehab module_param(i2c_debug, int, 0644);
25b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
26b285192aSMauro Carvalho Chehab 
27b285192aSMauro Carvalho Chehab static unsigned int i2c_scan;
28b285192aSMauro Carvalho Chehab module_param(i2c_scan, int, 0444);
29b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
30b285192aSMauro Carvalho Chehab 
3145f38cb3SMauro Carvalho Chehab #define i2c_dbg(level, fmt, arg...) do { \
3245f38cb3SMauro Carvalho Chehab 	if (i2c_debug == level) \
3345f38cb3SMauro Carvalho Chehab 		printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \
3445f38cb3SMauro Carvalho Chehab 	} while (0)
3530693f34SMauro Carvalho Chehab 
3645f38cb3SMauro Carvalho Chehab #define i2c_cont(level, fmt, arg...) do { \
3745f38cb3SMauro Carvalho Chehab 	if (i2c_debug == level) \
3845f38cb3SMauro Carvalho Chehab 		pr_cont(fmt, ## arg); \
3945f38cb3SMauro Carvalho Chehab 	} while (0)
40b285192aSMauro Carvalho Chehab 
41b285192aSMauro Carvalho Chehab #define I2C_WAIT_DELAY  32
42b285192aSMauro Carvalho Chehab #define I2C_WAIT_RETRY  16
43b285192aSMauro Carvalho Chehab 
44b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
45b285192aSMauro Carvalho Chehab 
46b285192aSMauro Carvalho Chehab static char *str_i2c_status[] = {
47b285192aSMauro Carvalho Chehab 	"IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
48b285192aSMauro Carvalho Chehab 	"DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
49b285192aSMauro Carvalho Chehab 	"NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
50b285192aSMauro Carvalho Chehab };
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab enum i2c_status {
53b285192aSMauro Carvalho Chehab 	IDLE          = 0,  // no I2C command pending
54b285192aSMauro Carvalho Chehab 	DONE_STOP     = 1,  // I2C command done and STOP executed
55b285192aSMauro Carvalho Chehab 	BUSY          = 2,  // executing I2C command
56b285192aSMauro Carvalho Chehab 	TO_SCL        = 3,  // executing I2C command, time out on clock stretching
57b285192aSMauro Carvalho Chehab 	TO_ARB        = 4,  // time out on arbitration trial, still trying
58b285192aSMauro Carvalho Chehab 	DONE_WRITE    = 5,  // I2C command done and awaiting next write command
59b285192aSMauro Carvalho Chehab 	DONE_READ     = 6,  // I2C command done and awaiting next read command
60b285192aSMauro Carvalho Chehab 	DONE_WRITE_TO = 7,  // see 5, and time out on status echo
61b285192aSMauro Carvalho Chehab 	DONE_READ_TO  = 8,  // see 6, and time out on status echo
62b285192aSMauro Carvalho Chehab 	NO_DEVICE     = 9,  // no acknowledge on device slave address
63b285192aSMauro Carvalho Chehab 	NO_ACKN       = 10, // no acknowledge after data byte transfer
64b285192aSMauro Carvalho Chehab 	BUS_ERR       = 11, // bus error
65b285192aSMauro Carvalho Chehab 	ARB_LOST      = 12, // arbitration lost during transfer
66b285192aSMauro Carvalho Chehab 	SEQ_ERR       = 13, // erroneous programming sequence
67b285192aSMauro Carvalho Chehab 	ST_ERR        = 14, // wrong status echoing
68b285192aSMauro Carvalho Chehab 	SW_ERR        = 15  // software error
69b285192aSMauro Carvalho Chehab };
70b285192aSMauro Carvalho Chehab 
71b285192aSMauro Carvalho Chehab static char *str_i2c_attr[] = {
72b285192aSMauro Carvalho Chehab 	"NOP", "STOP", "CONTINUE", "START"
73b285192aSMauro Carvalho Chehab };
74b285192aSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab enum i2c_attr {
76b285192aSMauro Carvalho Chehab 	NOP           = 0,  // no operation on I2C bus
77b285192aSMauro Carvalho Chehab 	STOP          = 1,  // stop condition, no associated byte transfer
78b285192aSMauro Carvalho Chehab 	CONTINUE      = 2,  // continue with byte transfer
79b285192aSMauro Carvalho Chehab 	START         = 3   // start condition with byte transfer
80b285192aSMauro Carvalho Chehab };
81b285192aSMauro Carvalho Chehab 
i2c_get_status(struct saa7134_dev * dev)82b285192aSMauro Carvalho Chehab static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
83b285192aSMauro Carvalho Chehab {
84b285192aSMauro Carvalho Chehab 	enum i2c_status status;
85b285192aSMauro Carvalho Chehab 
86b285192aSMauro Carvalho Chehab 	status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
8730693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]);
88b285192aSMauro Carvalho Chehab 	return status;
89b285192aSMauro Carvalho Chehab }
90b285192aSMauro Carvalho Chehab 
i2c_set_status(struct saa7134_dev * dev,enum i2c_status status)91b285192aSMauro Carvalho Chehab static inline void i2c_set_status(struct saa7134_dev *dev,
92b285192aSMauro Carvalho Chehab 				  enum i2c_status status)
93b285192aSMauro Carvalho Chehab {
9430693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]);
95b285192aSMauro Carvalho Chehab 	saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
96b285192aSMauro Carvalho Chehab }
97b285192aSMauro Carvalho Chehab 
i2c_set_attr(struct saa7134_dev * dev,enum i2c_attr attr)98b285192aSMauro Carvalho Chehab static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
99b285192aSMauro Carvalho Chehab {
10030693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]);
101b285192aSMauro Carvalho Chehab 	saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
102b285192aSMauro Carvalho Chehab }
103b285192aSMauro Carvalho Chehab 
i2c_is_error(enum i2c_status status)104b285192aSMauro Carvalho Chehab static inline int i2c_is_error(enum i2c_status status)
105b285192aSMauro Carvalho Chehab {
106b285192aSMauro Carvalho Chehab 	switch (status) {
107b285192aSMauro Carvalho Chehab 	case NO_DEVICE:
108b285192aSMauro Carvalho Chehab 	case NO_ACKN:
109b285192aSMauro Carvalho Chehab 	case BUS_ERR:
110b285192aSMauro Carvalho Chehab 	case ARB_LOST:
111b285192aSMauro Carvalho Chehab 	case SEQ_ERR:
112b285192aSMauro Carvalho Chehab 	case ST_ERR:
113b285192aSMauro Carvalho Chehab 		return true;
114b285192aSMauro Carvalho Chehab 	default:
115b285192aSMauro Carvalho Chehab 		return false;
116b285192aSMauro Carvalho Chehab 	}
117b285192aSMauro Carvalho Chehab }
118b285192aSMauro Carvalho Chehab 
i2c_is_idle(enum i2c_status status)119b285192aSMauro Carvalho Chehab static inline int i2c_is_idle(enum i2c_status status)
120b285192aSMauro Carvalho Chehab {
121b285192aSMauro Carvalho Chehab 	switch (status) {
122b285192aSMauro Carvalho Chehab 	case IDLE:
123b285192aSMauro Carvalho Chehab 	case DONE_STOP:
124b285192aSMauro Carvalho Chehab 		return true;
125b285192aSMauro Carvalho Chehab 	default:
126b285192aSMauro Carvalho Chehab 		return false;
127b285192aSMauro Carvalho Chehab 	}
128b285192aSMauro Carvalho Chehab }
129b285192aSMauro Carvalho Chehab 
i2c_is_busy(enum i2c_status status)130b285192aSMauro Carvalho Chehab static inline int i2c_is_busy(enum i2c_status status)
131b285192aSMauro Carvalho Chehab {
132b285192aSMauro Carvalho Chehab 	switch (status) {
133b285192aSMauro Carvalho Chehab 	case BUSY:
134b285192aSMauro Carvalho Chehab 	case TO_SCL:
135b285192aSMauro Carvalho Chehab 	case TO_ARB:
136b285192aSMauro Carvalho Chehab 		return true;
137b285192aSMauro Carvalho Chehab 	default:
138b285192aSMauro Carvalho Chehab 		return false;
139b285192aSMauro Carvalho Chehab 	}
140b285192aSMauro Carvalho Chehab }
141b285192aSMauro Carvalho Chehab 
i2c_is_busy_wait(struct saa7134_dev * dev)142b285192aSMauro Carvalho Chehab static int i2c_is_busy_wait(struct saa7134_dev *dev)
143b285192aSMauro Carvalho Chehab {
144b285192aSMauro Carvalho Chehab 	enum i2c_status status;
145b285192aSMauro Carvalho Chehab 	int count;
146b285192aSMauro Carvalho Chehab 
147b285192aSMauro Carvalho Chehab 	for (count = 0; count < I2C_WAIT_RETRY; count++) {
148b285192aSMauro Carvalho Chehab 		status = i2c_get_status(dev);
149b285192aSMauro Carvalho Chehab 		if (!i2c_is_busy(status))
150b285192aSMauro Carvalho Chehab 			break;
151b285192aSMauro Carvalho Chehab 		saa_wait(I2C_WAIT_DELAY);
152b285192aSMauro Carvalho Chehab 	}
153b285192aSMauro Carvalho Chehab 	if (I2C_WAIT_RETRY == count)
154b285192aSMauro Carvalho Chehab 		return false;
155b285192aSMauro Carvalho Chehab 	return true;
156b285192aSMauro Carvalho Chehab }
157b285192aSMauro Carvalho Chehab 
i2c_reset(struct saa7134_dev * dev)158b285192aSMauro Carvalho Chehab static int i2c_reset(struct saa7134_dev *dev)
159b285192aSMauro Carvalho Chehab {
160b285192aSMauro Carvalho Chehab 	enum i2c_status status;
161b285192aSMauro Carvalho Chehab 	int count;
162b285192aSMauro Carvalho Chehab 
16330693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c reset\n");
164b285192aSMauro Carvalho Chehab 	status = i2c_get_status(dev);
165b285192aSMauro Carvalho Chehab 	if (!i2c_is_error(status))
166b285192aSMauro Carvalho Chehab 		return true;
167b285192aSMauro Carvalho Chehab 	i2c_set_status(dev,status);
168b285192aSMauro Carvalho Chehab 
169b285192aSMauro Carvalho Chehab 	for (count = 0; count < I2C_WAIT_RETRY; count++) {
170b285192aSMauro Carvalho Chehab 		status = i2c_get_status(dev);
171b285192aSMauro Carvalho Chehab 		if (!i2c_is_error(status))
172b285192aSMauro Carvalho Chehab 			break;
173b285192aSMauro Carvalho Chehab 		udelay(I2C_WAIT_DELAY);
174b285192aSMauro Carvalho Chehab 	}
175b285192aSMauro Carvalho Chehab 	if (I2C_WAIT_RETRY == count)
176b285192aSMauro Carvalho Chehab 		return false;
177b285192aSMauro Carvalho Chehab 
178b285192aSMauro Carvalho Chehab 	if (!i2c_is_idle(status))
179b285192aSMauro Carvalho Chehab 		return false;
180b285192aSMauro Carvalho Chehab 
181b285192aSMauro Carvalho Chehab 	i2c_set_attr(dev,NOP);
182b285192aSMauro Carvalho Chehab 	return true;
183b285192aSMauro Carvalho Chehab }
184b285192aSMauro Carvalho Chehab 
i2c_send_byte(struct saa7134_dev * dev,enum i2c_attr attr,unsigned char data)185b285192aSMauro Carvalho Chehab static inline int i2c_send_byte(struct saa7134_dev *dev,
186b285192aSMauro Carvalho Chehab 				enum i2c_attr attr,
187b285192aSMauro Carvalho Chehab 				unsigned char data)
188b285192aSMauro Carvalho Chehab {
189b285192aSMauro Carvalho Chehab 	enum i2c_status status;
190b285192aSMauro Carvalho Chehab 	__u32 dword;
191b285192aSMauro Carvalho Chehab 
192b285192aSMauro Carvalho Chehab 	/* have to write both attr + data in one 32bit word */
193b285192aSMauro Carvalho Chehab 	dword  = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
194b285192aSMauro Carvalho Chehab 	dword &= 0x0f;
195b285192aSMauro Carvalho Chehab 	dword |= (attr << 6);
196b285192aSMauro Carvalho Chehab 	dword |= ((__u32)data << 8);
197b285192aSMauro Carvalho Chehab 	dword |= 0x00 << 16;  /* 100 kHz */
198b285192aSMauro Carvalho Chehab //	dword |= 0x40 << 16;  /* 400 kHz */
199b285192aSMauro Carvalho Chehab 	dword |= 0xf0 << 24;
200b285192aSMauro Carvalho Chehab 	saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
20130693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c data => 0x%x\n", data);
202b285192aSMauro Carvalho Chehab 
203b285192aSMauro Carvalho Chehab 	if (!i2c_is_busy_wait(dev))
204b285192aSMauro Carvalho Chehab 		return -EIO;
205b285192aSMauro Carvalho Chehab 	status = i2c_get_status(dev);
206b285192aSMauro Carvalho Chehab 	if (i2c_is_error(status))
207b285192aSMauro Carvalho Chehab 		return -EIO;
208b285192aSMauro Carvalho Chehab 	return 0;
209b285192aSMauro Carvalho Chehab }
210b285192aSMauro Carvalho Chehab 
i2c_recv_byte(struct saa7134_dev * dev)211b285192aSMauro Carvalho Chehab static inline int i2c_recv_byte(struct saa7134_dev *dev)
212b285192aSMauro Carvalho Chehab {
213b285192aSMauro Carvalho Chehab 	enum i2c_status status;
214b285192aSMauro Carvalho Chehab 	unsigned char data;
215b285192aSMauro Carvalho Chehab 
216b285192aSMauro Carvalho Chehab 	i2c_set_attr(dev,CONTINUE);
217b285192aSMauro Carvalho Chehab 	if (!i2c_is_busy_wait(dev))
218b285192aSMauro Carvalho Chehab 		return -EIO;
219b285192aSMauro Carvalho Chehab 	status = i2c_get_status(dev);
220b285192aSMauro Carvalho Chehab 	if (i2c_is_error(status))
221b285192aSMauro Carvalho Chehab 		return -EIO;
222b285192aSMauro Carvalho Chehab 	data = saa_readb(SAA7134_I2C_DATA);
22330693f34SMauro Carvalho Chehab 	i2c_dbg(2, "i2c data <= 0x%x\n", data);
224b285192aSMauro Carvalho Chehab 	return data;
225b285192aSMauro Carvalho Chehab }
226b285192aSMauro Carvalho Chehab 
saa7134_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)227b285192aSMauro Carvalho Chehab static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
228b285192aSMauro Carvalho Chehab 			    struct i2c_msg *msgs, int num)
229b285192aSMauro Carvalho Chehab {
230b285192aSMauro Carvalho Chehab 	struct saa7134_dev *dev = i2c_adap->algo_data;
231b285192aSMauro Carvalho Chehab 	enum i2c_status status;
232b285192aSMauro Carvalho Chehab 	unsigned char data;
233b285192aSMauro Carvalho Chehab 	int addr,rc,i,byte;
234b285192aSMauro Carvalho Chehab 
235b285192aSMauro Carvalho Chehab 	status = i2c_get_status(dev);
236b285192aSMauro Carvalho Chehab 	if (!i2c_is_idle(status))
237b285192aSMauro Carvalho Chehab 		if (!i2c_reset(dev))
238b285192aSMauro Carvalho Chehab 			return -EIO;
239b285192aSMauro Carvalho Chehab 
24030693f34SMauro Carvalho Chehab 	i2c_dbg(2, "start xfer\n");
24130693f34SMauro Carvalho Chehab 	i2c_dbg(1, "i2c xfer:");
242b285192aSMauro Carvalho Chehab 	for (i = 0; i < num; i++) {
243b285192aSMauro Carvalho Chehab 		if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
244b285192aSMauro Carvalho Chehab 			/* send address */
24530693f34SMauro Carvalho Chehab 			i2c_dbg(2, "send address\n");
246b285192aSMauro Carvalho Chehab 			addr  = msgs[i].addr << 1;
247b285192aSMauro Carvalho Chehab 			if (msgs[i].flags & I2C_M_RD)
248b285192aSMauro Carvalho Chehab 				addr |= 1;
249b285192aSMauro Carvalho Chehab 			if (i > 0 && msgs[i].flags &
250b285192aSMauro Carvalho Chehab 			    I2C_M_RD && msgs[i].addr != 0x40 &&
25134fe2784SOndrej Zary 			    msgs[i].addr != 0x41 &&
252b285192aSMauro Carvalho Chehab 			    msgs[i].addr != 0x19) {
253b285192aSMauro Carvalho Chehab 				/* workaround for a saa7134 i2c bug
254b285192aSMauro Carvalho Chehab 				 * needed to talk to the mt352 demux
255b285192aSMauro Carvalho Chehab 				 * thanks to pinnacle for the hint */
256b285192aSMauro Carvalho Chehab 				int quirk = 0xfe;
25730693f34SMauro Carvalho Chehab 				i2c_cont(1, " [%02x quirk]", quirk);
258b285192aSMauro Carvalho Chehab 				i2c_send_byte(dev,START,quirk);
259b285192aSMauro Carvalho Chehab 				i2c_recv_byte(dev);
260b285192aSMauro Carvalho Chehab 			}
26130693f34SMauro Carvalho Chehab 			i2c_cont(1, " < %02x", addr);
262b285192aSMauro Carvalho Chehab 			rc = i2c_send_byte(dev,START,addr);
263b285192aSMauro Carvalho Chehab 			if (rc < 0)
264b285192aSMauro Carvalho Chehab 				 goto err;
265b285192aSMauro Carvalho Chehab 		}
266b285192aSMauro Carvalho Chehab 		if (msgs[i].flags & I2C_M_RD) {
267b285192aSMauro Carvalho Chehab 			/* read bytes */
26830693f34SMauro Carvalho Chehab 			i2c_dbg(2, "read bytes\n");
269b285192aSMauro Carvalho Chehab 			for (byte = 0; byte < msgs[i].len; byte++) {
27030693f34SMauro Carvalho Chehab 				i2c_cont(1, " =");
271b285192aSMauro Carvalho Chehab 				rc = i2c_recv_byte(dev);
272b285192aSMauro Carvalho Chehab 				if (rc < 0)
273b285192aSMauro Carvalho Chehab 					goto err;
27430693f34SMauro Carvalho Chehab 				i2c_cont(1, "%02x", rc);
275b285192aSMauro Carvalho Chehab 				msgs[i].buf[byte] = rc;
276b285192aSMauro Carvalho Chehab 			}
277b285192aSMauro Carvalho Chehab 			/* discard mysterious extra byte when reading
278b285192aSMauro Carvalho Chehab 			   from Samsung S5H1411.  i2c bus gets error
279b285192aSMauro Carvalho Chehab 			   if we do not. */
280b285192aSMauro Carvalho Chehab 			if (0x19 == msgs[i].addr) {
28130693f34SMauro Carvalho Chehab 				i2c_cont(1, " ?");
282b285192aSMauro Carvalho Chehab 				rc = i2c_recv_byte(dev);
283b285192aSMauro Carvalho Chehab 				if (rc < 0)
284b285192aSMauro Carvalho Chehab 					goto err;
28530693f34SMauro Carvalho Chehab 				i2c_cont(1, "%02x", rc);
286b285192aSMauro Carvalho Chehab 			}
287b285192aSMauro Carvalho Chehab 		} else {
288b285192aSMauro Carvalho Chehab 			/* write bytes */
28930693f34SMauro Carvalho Chehab 			i2c_dbg(2, "write bytes\n");
290b285192aSMauro Carvalho Chehab 			for (byte = 0; byte < msgs[i].len; byte++) {
291b285192aSMauro Carvalho Chehab 				data = msgs[i].buf[byte];
29230693f34SMauro Carvalho Chehab 				i2c_cont(1, " %02x", data);
293b285192aSMauro Carvalho Chehab 				rc = i2c_send_byte(dev,CONTINUE,data);
294b285192aSMauro Carvalho Chehab 				if (rc < 0)
295b285192aSMauro Carvalho Chehab 					goto err;
296b285192aSMauro Carvalho Chehab 			}
297b285192aSMauro Carvalho Chehab 		}
298b285192aSMauro Carvalho Chehab 	}
29930693f34SMauro Carvalho Chehab 	i2c_dbg(2, "xfer done\n");
30030693f34SMauro Carvalho Chehab 	i2c_cont(1, " >");
301b285192aSMauro Carvalho Chehab 	i2c_set_attr(dev,STOP);
302b285192aSMauro Carvalho Chehab 	rc = -EIO;
303b285192aSMauro Carvalho Chehab 	if (!i2c_is_busy_wait(dev))
304b285192aSMauro Carvalho Chehab 		goto err;
305b285192aSMauro Carvalho Chehab 	status = i2c_get_status(dev);
306b285192aSMauro Carvalho Chehab 	if (i2c_is_error(status))
307b285192aSMauro Carvalho Chehab 		goto err;
308b285192aSMauro Carvalho Chehab 	/* ensure that the bus is idle for at least one bit slot */
309b285192aSMauro Carvalho Chehab 	msleep(1);
310b285192aSMauro Carvalho Chehab 
31130693f34SMauro Carvalho Chehab 	i2c_cont(1, "\n");
312b285192aSMauro Carvalho Chehab 	return num;
313b285192aSMauro Carvalho Chehab  err:
314b285192aSMauro Carvalho Chehab 	if (1 == i2c_debug) {
315b285192aSMauro Carvalho Chehab 		status = i2c_get_status(dev);
31630693f34SMauro Carvalho Chehab 		i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]);
317b285192aSMauro Carvalho Chehab 	}
318b285192aSMauro Carvalho Chehab 	return rc;
319b285192aSMauro Carvalho Chehab }
320b285192aSMauro Carvalho Chehab 
321b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
322b285192aSMauro Carvalho Chehab 
functionality(struct i2c_adapter * adap)323b285192aSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *adap)
324b285192aSMauro Carvalho Chehab {
325b285192aSMauro Carvalho Chehab 	return I2C_FUNC_SMBUS_EMUL;
326b285192aSMauro Carvalho Chehab }
327b285192aSMauro Carvalho Chehab 
32878f2c50bSJulia Lawall static const struct i2c_algorithm saa7134_algo = {
329b285192aSMauro Carvalho Chehab 	.master_xfer   = saa7134_i2c_xfer,
330b285192aSMauro Carvalho Chehab 	.functionality = functionality,
331b285192aSMauro Carvalho Chehab };
332b285192aSMauro Carvalho Chehab 
333e5fffebeSBhumika Goyal static const struct i2c_adapter saa7134_adap_template = {
334b285192aSMauro Carvalho Chehab 	.owner         = THIS_MODULE,
335b285192aSMauro Carvalho Chehab 	.name          = "saa7134",
336b285192aSMauro Carvalho Chehab 	.algo          = &saa7134_algo,
337b285192aSMauro Carvalho Chehab };
338b285192aSMauro Carvalho Chehab 
339b8e9b36dSBhumika Goyal static const struct i2c_client saa7134_client_template = {
340b285192aSMauro Carvalho Chehab 	.name	= "saa7134 internal",
341b285192aSMauro Carvalho Chehab };
342b285192aSMauro Carvalho Chehab 
343b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
344b285192aSMauro Carvalho Chehab 
345*9d802222SMaciej S. Szmigiero /*
346*9d802222SMaciej S. Szmigiero  * On Medion 7134 reading the SAA7134 chip config EEPROM needs DVB-T
347*9d802222SMaciej S. Szmigiero  * demod i2c gate closed due to an address clash between this EEPROM
348*9d802222SMaciej S. Szmigiero  * and the demod one.
349*9d802222SMaciej S. Szmigiero  */
saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev * dev)3505a91206fSMaciej S. Szmigiero static void saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev *dev)
3515a91206fSMaciej S. Szmigiero {
3525a91206fSMaciej S. Szmigiero 	u8 subaddr = 0x7, dmdregval;
3535a91206fSMaciej S. Szmigiero 	u8 data[2];
3545a91206fSMaciej S. Szmigiero 	int ret;
3555a91206fSMaciej S. Szmigiero 	struct i2c_msg i2cgatemsg_r[] = { {.addr = 0x08, .flags = 0,
3565a91206fSMaciej S. Szmigiero 					   .buf = &subaddr, .len = 1},
3575a91206fSMaciej S. Szmigiero 					  {.addr = 0x08,
3585a91206fSMaciej S. Szmigiero 					   .flags = I2C_M_RD,
3595a91206fSMaciej S. Szmigiero 					   .buf = &dmdregval, .len = 1}
3605a91206fSMaciej S. Szmigiero 					};
3615a91206fSMaciej S. Szmigiero 	struct i2c_msg i2cgatemsg_w[] = { {.addr = 0x08, .flags = 0,
3625a91206fSMaciej S. Szmigiero 					   .buf = data, .len = 2} };
3635a91206fSMaciej S. Szmigiero 
3645a91206fSMaciej S. Szmigiero 	ret = i2c_transfer(&dev->i2c_adap, i2cgatemsg_r, 2);
3655a91206fSMaciej S. Szmigiero 	if ((ret == 2) && (dmdregval & 0x2)) {
366*9d802222SMaciej S. Szmigiero 		pr_debug("%s: DVB-T demod i2c gate was left open\n",
3675a91206fSMaciej S. Szmigiero 			 dev->name);
3685a91206fSMaciej S. Szmigiero 
3695a91206fSMaciej S. Szmigiero 		data[0] = subaddr;
3705a91206fSMaciej S. Szmigiero 		data[1] = (dmdregval & ~0x2);
3715a91206fSMaciej S. Szmigiero 		if (i2c_transfer(&dev->i2c_adap, i2cgatemsg_w, 1) != 1)
372*9d802222SMaciej S. Szmigiero 			pr_err("%s: EEPROM i2c gate close failure\n",
3735a91206fSMaciej S. Szmigiero 			       dev->name);
3745a91206fSMaciej S. Szmigiero 	}
3755a91206fSMaciej S. Szmigiero }
3765a91206fSMaciej S. Szmigiero 
377b285192aSMauro Carvalho Chehab static int
saa7134_i2c_eeprom(struct saa7134_dev * dev,unsigned char * eedata,int len)378b285192aSMauro Carvalho Chehab saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
379b285192aSMauro Carvalho Chehab {
380b285192aSMauro Carvalho Chehab 	unsigned char buf;
381b285192aSMauro Carvalho Chehab 	int i,err;
382b285192aSMauro Carvalho Chehab 
3835a91206fSMaciej S. Szmigiero 	if (dev->board == SAA7134_BOARD_MD7134)
3845a91206fSMaciej S. Szmigiero 		saa7134_i2c_eeprom_md7134_gate(dev);
3855a91206fSMaciej S. Szmigiero 
386b285192aSMauro Carvalho Chehab 	dev->i2c_client.addr = 0xa0 >> 1;
387b285192aSMauro Carvalho Chehab 	buf = 0;
388b285192aSMauro Carvalho Chehab 	if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
38983582009SMauro Carvalho Chehab 		pr_info("%s: Huh, no eeprom present (err=%d)?\n",
390b285192aSMauro Carvalho Chehab 		       dev->name,err);
391b285192aSMauro Carvalho Chehab 		return -1;
392b285192aSMauro Carvalho Chehab 	}
393b285192aSMauro Carvalho Chehab 	if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
39483582009SMauro Carvalho Chehab 		pr_warn("%s: i2c eeprom read error (err=%d)\n",
395b285192aSMauro Carvalho Chehab 		       dev->name,err);
396b285192aSMauro Carvalho Chehab 		return -1;
397b285192aSMauro Carvalho Chehab 	}
3983e0051c5SMauro Carvalho Chehab 
3993e0051c5SMauro Carvalho Chehab 	for (i = 0; i < len; i += 16) {
4003e0051c5SMauro Carvalho Chehab 		int size = (len - i) > 16 ? 16 : len - i;
4016139ebc6SMauro Carvalho Chehab 
4023e0051c5SMauro Carvalho Chehab 		pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]);
403b285192aSMauro Carvalho Chehab 	}
4043e0051c5SMauro Carvalho Chehab 
405b285192aSMauro Carvalho Chehab 	return 0;
406b285192aSMauro Carvalho Chehab }
407b285192aSMauro Carvalho Chehab 
408b285192aSMauro Carvalho Chehab static char *i2c_devs[128] = {
409b285192aSMauro Carvalho Chehab 	[ 0x20      ] = "mpeg encoder (saa6752hs)",
410b285192aSMauro Carvalho Chehab 	[ 0xa0 >> 1 ] = "eeprom",
411b285192aSMauro Carvalho Chehab 	[ 0xc0 >> 1 ] = "tuner (analog)",
412b285192aSMauro Carvalho Chehab 	[ 0x86 >> 1 ] = "tda9887",
413b285192aSMauro Carvalho Chehab 	[ 0x5a >> 1 ] = "remote control",
414b285192aSMauro Carvalho Chehab };
415b285192aSMauro Carvalho Chehab 
do_i2c_scan(struct i2c_client * c)4162bb3e2eeSMauro Carvalho Chehab static void do_i2c_scan(struct i2c_client *c)
417b285192aSMauro Carvalho Chehab {
418b285192aSMauro Carvalho Chehab 	unsigned char buf;
419b285192aSMauro Carvalho Chehab 	int i,rc;
420b285192aSMauro Carvalho Chehab 
421b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
422b285192aSMauro Carvalho Chehab 		c->addr = i;
423b285192aSMauro Carvalho Chehab 		rc = i2c_master_recv(c,&buf,0);
424b285192aSMauro Carvalho Chehab 		if (rc < 0)
425b285192aSMauro Carvalho Chehab 			continue;
4262bb3e2eeSMauro Carvalho Chehab 		pr_info("i2c scan: found device @ 0x%x  [%s]\n",
4272bb3e2eeSMauro Carvalho Chehab 			 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
428b285192aSMauro Carvalho Chehab 	}
429b285192aSMauro Carvalho Chehab }
430b285192aSMauro Carvalho Chehab 
saa7134_i2c_register(struct saa7134_dev * dev)431b285192aSMauro Carvalho Chehab int saa7134_i2c_register(struct saa7134_dev *dev)
432b285192aSMauro Carvalho Chehab {
433b285192aSMauro Carvalho Chehab 	dev->i2c_adap = saa7134_adap_template;
434b285192aSMauro Carvalho Chehab 	dev->i2c_adap.dev.parent = &dev->pci->dev;
435cc1e6315SMauro Carvalho Chehab 	strscpy(dev->i2c_adap.name, dev->name, sizeof(dev->i2c_adap.name));
436b285192aSMauro Carvalho Chehab 	dev->i2c_adap.algo_data = dev;
437b285192aSMauro Carvalho Chehab 	i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
438b285192aSMauro Carvalho Chehab 	i2c_add_adapter(&dev->i2c_adap);
439b285192aSMauro Carvalho Chehab 
440b285192aSMauro Carvalho Chehab 	dev->i2c_client = saa7134_client_template;
441b285192aSMauro Carvalho Chehab 	dev->i2c_client.adapter = &dev->i2c_adap;
442b285192aSMauro Carvalho Chehab 
443b285192aSMauro Carvalho Chehab 	saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
444b285192aSMauro Carvalho Chehab 	if (i2c_scan)
4452bb3e2eeSMauro Carvalho Chehab 		do_i2c_scan(&dev->i2c_client);
446b285192aSMauro Carvalho Chehab 
447b285192aSMauro Carvalho Chehab 	/* Instantiate the IR receiver device, if present */
448b285192aSMauro Carvalho Chehab 	saa7134_probe_i2c_ir(dev);
449b285192aSMauro Carvalho Chehab 	return 0;
450b285192aSMauro Carvalho Chehab }
451b285192aSMauro Carvalho Chehab 
saa7134_i2c_unregister(struct saa7134_dev * dev)452b285192aSMauro Carvalho Chehab int saa7134_i2c_unregister(struct saa7134_dev *dev)
453b285192aSMauro Carvalho Chehab {
454b285192aSMauro Carvalho Chehab 	i2c_del_adapter(&dev->i2c_adap);
455b285192aSMauro Carvalho Chehab 	return 0;
456b285192aSMauro Carvalho Chehab }
457