1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * device driver for philips saa7134 based TV cards
5 * i2c interface support
7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
11 #include "saa7134-reg.h"
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
19 #include <media/v4l2-common.h>
21 /* ----------------------------------------------------------- */
23 static unsigned int i2c_debug
;
24 module_param(i2c_debug
, int, 0644);
25 MODULE_PARM_DESC(i2c_debug
,"enable debug messages [i2c]");
27 static unsigned int i2c_scan
;
28 module_param(i2c_scan
, int, 0444);
29 MODULE_PARM_DESC(i2c_scan
,"scan i2c bus at insmod time");
31 #define i2c_dbg(level, fmt, arg...) do { \
32 if (i2c_debug == level) \
33 printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \
36 #define i2c_cont(level, fmt, arg...) do { \
37 if (i2c_debug == level) \
38 pr_cont(fmt, ## arg); \
41 #define I2C_WAIT_DELAY 32
42 #define I2C_WAIT_RETRY 16
44 /* ----------------------------------------------------------- */
46 static char *str_i2c_status
[] = {
47 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
48 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
49 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
53 IDLE
= 0, // no I2C command pending
54 DONE_STOP
= 1, // I2C command done and STOP executed
55 BUSY
= 2, // executing I2C command
56 TO_SCL
= 3, // executing I2C command, time out on clock stretching
57 TO_ARB
= 4, // time out on arbitration trial, still trying
58 DONE_WRITE
= 5, // I2C command done and awaiting next write command
59 DONE_READ
= 6, // I2C command done and awaiting next read command
60 DONE_WRITE_TO
= 7, // see 5, and time out on status echo
61 DONE_READ_TO
= 8, // see 6, and time out on status echo
62 NO_DEVICE
= 9, // no acknowledge on device slave address
63 NO_ACKN
= 10, // no acknowledge after data byte transfer
64 BUS_ERR
= 11, // bus error
65 ARB_LOST
= 12, // arbitration lost during transfer
66 SEQ_ERR
= 13, // erroneous programming sequence
67 ST_ERR
= 14, // wrong status echoing
68 SW_ERR
= 15 // software error
71 static char *str_i2c_attr
[] = {
72 "NOP", "STOP", "CONTINUE", "START"
76 NOP
= 0, // no operation on I2C bus
77 STOP
= 1, // stop condition, no associated byte transfer
78 CONTINUE
= 2, // continue with byte transfer
79 START
= 3 // start condition with byte transfer
82 static inline enum i2c_status
i2c_get_status(struct saa7134_dev
*dev
)
84 enum i2c_status status
;
86 status
= saa_readb(SAA7134_I2C_ATTR_STATUS
) & 0x0f;
87 i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status
[status
]);
91 static inline void i2c_set_status(struct saa7134_dev
*dev
,
92 enum i2c_status status
)
94 i2c_dbg(2, "i2c stat => %s\n", str_i2c_status
[status
]);
95 saa_andorb(SAA7134_I2C_ATTR_STATUS
,0x0f,status
);
98 static inline void i2c_set_attr(struct saa7134_dev
*dev
, enum i2c_attr attr
)
100 i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr
[attr
]);
101 saa_andorb(SAA7134_I2C_ATTR_STATUS
,0xc0,attr
<< 6);
104 static inline int i2c_is_error(enum i2c_status status
)
119 static inline int i2c_is_idle(enum i2c_status status
)
130 static inline int i2c_is_busy(enum i2c_status status
)
142 static int i2c_is_busy_wait(struct saa7134_dev
*dev
)
144 enum i2c_status status
;
147 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
148 status
= i2c_get_status(dev
);
149 if (!i2c_is_busy(status
))
151 saa_wait(I2C_WAIT_DELAY
);
153 if (I2C_WAIT_RETRY
== count
)
158 static int i2c_reset(struct saa7134_dev
*dev
)
160 enum i2c_status status
;
163 i2c_dbg(2, "i2c reset\n");
164 status
= i2c_get_status(dev
);
165 if (!i2c_is_error(status
))
167 i2c_set_status(dev
,status
);
169 for (count
= 0; count
< I2C_WAIT_RETRY
; count
++) {
170 status
= i2c_get_status(dev
);
171 if (!i2c_is_error(status
))
173 udelay(I2C_WAIT_DELAY
);
175 if (I2C_WAIT_RETRY
== count
)
178 if (!i2c_is_idle(status
))
181 i2c_set_attr(dev
,NOP
);
185 static inline int i2c_send_byte(struct saa7134_dev
*dev
,
189 enum i2c_status status
;
192 /* have to write both attr + data in one 32bit word */
193 dword
= saa_readl(SAA7134_I2C_ATTR_STATUS
>> 2);
195 dword
|= (attr
<< 6);
196 dword
|= ((__u32
)data
<< 8);
197 dword
|= 0x00 << 16; /* 100 kHz */
198 // dword |= 0x40 << 16; /* 400 kHz */
200 saa_writel(SAA7134_I2C_ATTR_STATUS
>> 2, dword
);
201 i2c_dbg(2, "i2c data => 0x%x\n", data
);
203 if (!i2c_is_busy_wait(dev
))
205 status
= i2c_get_status(dev
);
206 if (i2c_is_error(status
))
211 static inline int i2c_recv_byte(struct saa7134_dev
*dev
)
213 enum i2c_status status
;
216 i2c_set_attr(dev
,CONTINUE
);
217 if (!i2c_is_busy_wait(dev
))
219 status
= i2c_get_status(dev
);
220 if (i2c_is_error(status
))
222 data
= saa_readb(SAA7134_I2C_DATA
);
223 i2c_dbg(2, "i2c data <= 0x%x\n", data
);
227 static int saa7134_i2c_xfer(struct i2c_adapter
*i2c_adap
,
228 struct i2c_msg
*msgs
, int num
)
230 struct saa7134_dev
*dev
= i2c_adap
->algo_data
;
231 enum i2c_status status
;
235 status
= i2c_get_status(dev
);
236 if (!i2c_is_idle(status
))
240 i2c_dbg(2, "start xfer\n");
241 i2c_dbg(1, "i2c xfer:");
242 for (i
= 0; i
< num
; i
++) {
243 if (!(msgs
[i
].flags
& I2C_M_NOSTART
) || 0 == i
) {
245 i2c_dbg(2, "send address\n");
246 addr
= msgs
[i
].addr
<< 1;
247 if (msgs
[i
].flags
& I2C_M_RD
)
249 if (i
> 0 && msgs
[i
].flags
&
250 I2C_M_RD
&& msgs
[i
].addr
!= 0x40 &&
251 msgs
[i
].addr
!= 0x41 &&
252 msgs
[i
].addr
!= 0x19) {
253 /* workaround for a saa7134 i2c bug
254 * needed to talk to the mt352 demux
255 * thanks to pinnacle for the hint */
257 i2c_cont(1, " [%02x quirk]", quirk
);
258 i2c_send_byte(dev
,START
,quirk
);
261 i2c_cont(1, " < %02x", addr
);
262 rc
= i2c_send_byte(dev
,START
,addr
);
266 if (msgs
[i
].flags
& I2C_M_RD
) {
268 i2c_dbg(2, "read bytes\n");
269 for (byte
= 0; byte
< msgs
[i
].len
; byte
++) {
271 rc
= i2c_recv_byte(dev
);
274 i2c_cont(1, "%02x", rc
);
275 msgs
[i
].buf
[byte
] = rc
;
277 /* discard mysterious extra byte when reading
278 from Samsung S5H1411. i2c bus gets error
280 if (0x19 == msgs
[i
].addr
) {
282 rc
= i2c_recv_byte(dev
);
285 i2c_cont(1, "%02x", rc
);
289 i2c_dbg(2, "write bytes\n");
290 for (byte
= 0; byte
< msgs
[i
].len
; byte
++) {
291 data
= msgs
[i
].buf
[byte
];
292 i2c_cont(1, " %02x", data
);
293 rc
= i2c_send_byte(dev
,CONTINUE
,data
);
299 i2c_dbg(2, "xfer done\n");
301 i2c_set_attr(dev
,STOP
);
303 if (!i2c_is_busy_wait(dev
))
305 status
= i2c_get_status(dev
);
306 if (i2c_is_error(status
))
308 /* ensure that the bus is idle for at least one bit slot */
314 if (1 == i2c_debug
) {
315 status
= i2c_get_status(dev
);
316 i2c_cont(1, " ERROR: %s\n", str_i2c_status
[status
]);
321 /* ----------------------------------------------------------- */
323 static u32
functionality(struct i2c_adapter
*adap
)
325 return I2C_FUNC_SMBUS_EMUL
;
328 static const struct i2c_algorithm saa7134_algo
= {
329 .master_xfer
= saa7134_i2c_xfer
,
330 .functionality
= functionality
,
333 static const struct i2c_adapter saa7134_adap_template
= {
334 .owner
= THIS_MODULE
,
336 .algo
= &saa7134_algo
,
339 static const struct i2c_client saa7134_client_template
= {
340 .name
= "saa7134 internal",
343 /* ----------------------------------------------------------- */
346 * On Medion 7134 reading the SAA7134 chip config EEPROM needs DVB-T
347 * demod i2c gate closed due to an address clash between this EEPROM
350 static void saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev
*dev
)
352 u8 subaddr
= 0x7, dmdregval
;
355 struct i2c_msg i2cgatemsg_r
[] = { {.addr
= 0x08, .flags
= 0,
356 .buf
= &subaddr
, .len
= 1},
359 .buf
= &dmdregval
, .len
= 1}
361 struct i2c_msg i2cgatemsg_w
[] = { {.addr
= 0x08, .flags
= 0,
362 .buf
= data
, .len
= 2} };
364 ret
= i2c_transfer(&dev
->i2c_adap
, i2cgatemsg_r
, 2);
365 if ((ret
== 2) && (dmdregval
& 0x2)) {
366 pr_debug("%s: DVB-T demod i2c gate was left open\n",
370 data
[1] = (dmdregval
& ~0x2);
371 if (i2c_transfer(&dev
->i2c_adap
, i2cgatemsg_w
, 1) != 1)
372 pr_err("%s: EEPROM i2c gate close failure\n",
378 saa7134_i2c_eeprom(struct saa7134_dev
*dev
, unsigned char *eedata
, int len
)
383 if (dev
->board
== SAA7134_BOARD_MD7134
)
384 saa7134_i2c_eeprom_md7134_gate(dev
);
386 dev
->i2c_client
.addr
= 0xa0 >> 1;
388 if (1 != (err
= i2c_master_send(&dev
->i2c_client
,&buf
,1))) {
389 pr_info("%s: Huh, no eeprom present (err=%d)?\n",
393 if (len
!= (err
= i2c_master_recv(&dev
->i2c_client
,eedata
,len
))) {
394 pr_warn("%s: i2c eeprom read error (err=%d)\n",
399 for (i
= 0; i
< len
; i
+= 16) {
400 int size
= (len
- i
) > 16 ? 16 : len
- i
;
402 pr_info("i2c eeprom %02x: %*ph\n", i
, size
, &eedata
[i
]);
408 static char *i2c_devs
[128] = {
409 [ 0x20 ] = "mpeg encoder (saa6752hs)",
410 [ 0xa0 >> 1 ] = "eeprom",
411 [ 0xc0 >> 1 ] = "tuner (analog)",
412 [ 0x86 >> 1 ] = "tda9887",
413 [ 0x5a >> 1 ] = "remote control",
416 static void do_i2c_scan(struct i2c_client
*c
)
421 for (i
= 0; i
< ARRAY_SIZE(i2c_devs
); i
++) {
423 rc
= i2c_master_recv(c
,&buf
,0);
426 pr_info("i2c scan: found device @ 0x%x [%s]\n",
427 i
<< 1, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
431 int saa7134_i2c_register(struct saa7134_dev
*dev
)
433 dev
->i2c_adap
= saa7134_adap_template
;
434 dev
->i2c_adap
.dev
.parent
= &dev
->pci
->dev
;
435 strscpy(dev
->i2c_adap
.name
, dev
->name
, sizeof(dev
->i2c_adap
.name
));
436 dev
->i2c_adap
.algo_data
= dev
;
437 i2c_set_adapdata(&dev
->i2c_adap
, &dev
->v4l2_dev
);
438 i2c_add_adapter(&dev
->i2c_adap
);
440 dev
->i2c_client
= saa7134_client_template
;
441 dev
->i2c_client
.adapter
= &dev
->i2c_adap
;
443 saa7134_i2c_eeprom(dev
,dev
->eedata
,sizeof(dev
->eedata
));
445 do_i2c_scan(&dev
->i2c_client
);
447 /* Instantiate the IR receiver device, if present */
448 saa7134_probe_i2c_ir(dev
);
452 int saa7134_i2c_unregister(struct saa7134_dev
*dev
)
454 i2c_del_adapter(&dev
->i2c_adap
);