22 #include <linux/kernel.h>
23 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/i2c.h>
32 #define DEB1(fmt, args...) do { if (i2c_debug >= 1) \
33 printk(KERN_DEBUG fmt, ## args); } while (0)
34 #define DEB2(fmt, args...) do { if (i2c_debug >= 2) \
35 printk(KERN_DEBUG fmt, ## args); } while (0)
36 #define DEB3(fmt, args...) do { if (i2c_debug >= 3) \
37 printk(KERN_DEBUG fmt, ## args); } while (0)
41 #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
42 #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
44 #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
45 #define pca_clock(adap) adap->i2c_clock
46 #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
47 #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
48 #define pca_wait(adap) adap->wait_for_completion(adap->data)
87 DEB2(
"=== REPEATED START\n");
123 addr = ((0x7f & msg->
addr) << 1);
126 DEB2(
"=== SLAVE ADDRESS %#04x+%c=%#04x\n",
146 DEB2(
"=== WRITE %#04x\n", b);
164 DEB2(
"=== READ %#04x %s\n", *b, ack ?
"ACK" :
"NACK");
203 dev_dbg(&i2c_adap->
dev,
"bus is not idle. status is "
209 DEB1(
"{{{ XFER %d messages\n", num);
212 for (curmsg = 0; curmsg < num; curmsg++) {
216 addr = (0x7f & msg->
addr) ;
220 curmsg, msg->
len, addr, (addr << 1) | 1);
223 curmsg, msg->
len, addr, addr << 1,
224 msg->
len == 0 ?
"" :
", ");
225 for (i = 0; i < msg->
len; i++)
226 printk(
"%#04x%s", msg->
buf[i], i == msg->
len - 1 ?
"" :
", ");
234 while (curmsg < num) {
237 DEB3(
"STATE is 0x%02x\n", state);
242 completed = pca_start(adap);
247 completed = pca_address(adap, msg);
252 if (numbytes < msg->
len) {
253 completed = pca_tx_byte(adap,
258 curmsg++; numbytes = 0;
262 completed = pca_repeated_start(adap);
266 DEB2(
"NOT ACK received after SLA+W\n");
272 completed = pca_rx_ack(adap, msg->
len > 1);
276 if (numbytes < msg->len) {
277 pca_rx_byte(adap, &msg->
buf[numbytes], 1);
279 completed = pca_rx_ack(adap,
280 numbytes < msg->len - 1);
283 curmsg++; numbytes = 0;
287 completed = pca_repeated_start(adap);
291 DEB2(
"NOT ACK received after SLA+R\n");
297 DEB2(
"NOT ACK received after data byte\n");
302 DEB2(
"Arbitration lost\n");
316 if (numbytes == msg->
len - 1) {
317 pca_rx_byte(adap, &msg->
buf[numbytes], 0);
318 curmsg++; numbytes = 0;
322 completed = pca_repeated_start(adap);
324 DEB2(
"NOT ACK sent after data byte received. "
325 "Not final byte. numbytes %d. len %d\n",
332 DEB2(
"BUS ERROR - SDA Stuck low\n");
336 DEB2(
"BUS ERROR - SCL Stuck low\n");
340 DEB2(
"BUS ERROR - Illegal START or STOP\n");
344 dev_err(&i2c_adap->
dev,
"unhandled SIO state 0x%02x\n", state);
354 DEB1(
"}}} transferred %d/%d messages. "
355 "status is %#04x. control is %#04x\n",
367 .master_xfer = pca_xfer,
368 .functionality = pca_func,
371 static unsigned int pca_probe_chip(
struct i2c_adapter *adap)
391 return pca_data->
chip;
398 adap->
algo = &pca_algo;
401 static int freqs[] = {330, 288, 217, 146, 88, 59, 44, 36};
432 "%s: Invalid I2C clock speed selected."
433 " Using default 59kHz.\n", adap->
name);
438 "Choosing the clock frequency based on "
439 "index is deprecated."
440 " Use the nominal frequency.\n", adap->
name);
447 adap->
name, freqs[clock]);
455 int min_tlow, min_thi;
467 " Using 1265.8kHz.\n", adap->
name);
473 " Using 60.3kHz.\n", adap->
name);
484 raise_fall_time = 22;
485 }
else if (pca_data->
i2c_clock > 400000) {
489 raise_fall_time = 22;
490 }
else if (pca_data->
i2c_clock > 100000) {
494 raise_fall_time = 58;
499 raise_fall_time = 127;
508 thi = 1000000 - clock * raise_fall_time;
511 tlow = (1000000 - clock * raise_fall_time) * min_tlow;
513 thi = tlow * min_thi / min_tlow;
519 "%s: Clock frequency is %dHz\n", adap->
name, clock * 100);
542 rval = pca_init(adap);
554 rval = pca_init(adap);