13 #include <linux/module.h>
17 #include <linux/i2c.h>
25 #include <linux/slab.h>
28 #define I2C_PNX_TIMEOUT_DEFAULT 10
29 #define I2C_PNX_SPEED_KHZ_DEFAULT 100
30 #define I2C_PNX_REGION_SIZE 0x100
65 #define I2C_REG_RX(a) ((a)->ioaddr)
66 #define I2C_REG_TX(a) ((a)->ioaddr)
67 #define I2C_REG_STS(a) ((a)->ioaddr + 0x04)
68 #define I2C_REG_CTL(a) ((a)->ioaddr + 0x08)
69 #define I2C_REG_CKL(a) ((a)->ioaddr + 0x0c)
70 #define I2C_REG_CKH(a) ((a)->ioaddr + 0x10)
71 #define I2C_REG_ADR(a) ((a)->ioaddr + 0x14)
72 #define I2C_REG_RFL(a) ((a)->ioaddr + 0x18)
73 #define I2C_REG_TFL(a) ((a)->ioaddr + 0x1c)
74 #define I2C_REG_RXB(a) ((a)->ioaddr + 0x20)
75 #define I2C_REG_TXB(a) ((a)->ioaddr + 0x24)
76 #define I2C_REG_TXS(a) ((a)->ioaddr + 0x28)
77 #define I2C_REG_STFL(a) ((a)->ioaddr + 0x2c)
87 return (timeout <= 0);
98 return (timeout <= 0);
111 dev_dbg(&alg_data->
adapter.dev,
"Timer armed at %lu plus %lu jiffies.\n",
115 timer->
data = (
unsigned long)alg_data;
127 static int i2c_pnx_start(
unsigned char slave_addr,
130 dev_dbg(&alg_data->
adapter.dev,
"%s(): addr 0x%x mode %d\n", __func__,
131 slave_addr, alg_data->
mif.mode);
134 if (slave_addr & ~0x7f) {
136 "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
137 alg_data->
adapter.name, slave_addr);
145 "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
146 alg_data->
adapter.name, slave_addr,
153 "%s: Arbitration failure. Slave addr = %02x\n",
154 alg_data->
adapter.name, slave_addr);
195 while (timeout > 0 &&
219 if (alg_data->
mif.len > 0) {
221 val = *alg_data->
mif.buf++;
223 if (alg_data->
mif.len == 1)
230 __func__, val, alg_data->
mif.len + 1);
232 if (alg_data->
mif.len == 0) {
233 if (alg_data->
last) {
237 "The bus is still active after timeout\n");
247 "%s(): Waking up xfer routine.\n",
252 }
else if (alg_data->
mif.len == 0) {
254 i2c_pnx_stop(alg_data);
264 "%s(): Waking up xfer routine after zero-xfer.\n",
284 unsigned int val = 0;
298 if (alg_data->
mif.order) {
300 "%s(): Write dummy data to fill Rx-fifo...\n",
303 if (alg_data->
mif.order == 1) {
323 alg_data->
mif.order--;
329 if (alg_data->
mif.len > 0) {
331 *alg_data->
mif.buf++ = (
u8) (val & 0xff);
333 __func__, val, alg_data->
mif.len);
336 if (alg_data->
mif.len == 0) {
341 "The bus is still active after timeout\n");
367 "%s(): mstat = %x mctrl = %x, mode = %d\n",
375 if (stat & mstatus_afi) {
391 "%s(): Slave did not acknowledge, generating a STOP.\n",
393 i2c_pnx_stop(alg_data);
419 i2c_pnx_master_xmit(alg_data);
421 i2c_pnx_master_rcv(alg_data);
431 "%s(): exiting, stat = %x ctrl = %x.\n",
438 static void i2c_pnx_timeout(
unsigned long data)
444 "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
455 wait_reset(alg_data);
466 "%s: Bus is still active after xfer. Reset it...\n",
470 wait_reset(alg_data);
471 }
else if (!(stat & mstatus_rfe) || !(stat &
mstatus_tfe)) {
477 wait_reset(alg_data);
478 }
else if (stat & mstatus_nai) {
481 wait_reset(alg_data);
502 "%s(): entering: %d messages, stat = %04x.\n",
505 bus_reset_if_active(alg_data);
508 for (
i = 0; rc >= 0 &&
i < num;
i++) {
516 "%s: 10 bits addr not supported!\n",
522 alg_data->
mif.buf = pmsg->
buf;
523 alg_data->
mif.len = pmsg->
len;
524 alg_data->
mif.order = pmsg->
len;
527 alg_data->
mif.ret = 0;
528 alg_data->
last = (
i == num - 1);
531 __func__, alg_data->
mif.mode, alg_data->
mif.len);
533 i2c_pnx_arm_timer(alg_data);
536 init_completion(&alg_data->
mif.complete);
544 rc = i2c_pnx_start(addr, alg_data);
551 if (!(rc = alg_data->
mif.ret))
554 "%s(): Complete, return code = %d.\n",
560 "%s: TDI still set... clearing now.\n",
566 "%s: AFI still set... clearing now.\n",
572 bus_reset_if_active(alg_data);
576 alg_data->
mif.len = 0;
577 alg_data->
mif.order = 0;
594 .master_xfer = i2c_pnx_xfer,
595 .functionality = i2c_pnx_func,
599 static int i2c_pnx_controller_suspend(
struct device *
dev)
608 static int i2c_pnx_controller_resume(
struct device *
dev)
616 i2c_pnx_controller_suspend, i2c_pnx_controller_resume);
617 #define PNX_I2C_PM (&i2c_pnx_pm)
619 #define PNX_I2C_PM NULL
631 alg_data = kzalloc(
sizeof(*alg_data),
GFP_KERNEL);
637 platform_set_drvdata(pdev, alg_data);
640 alg_data->
adapter.algo = &pnx_algorithm;
641 alg_data->
adapter.algo_data = alg_data;
646 alg_data->
adapter.dev.of_node = of_node_get(pdev->
dev.of_node);
647 if (pdev->
dev.of_node) {
648 of_property_read_u32(pdev->
dev.of_node,
"clock-frequency",
661 if (IS_ERR(alg_data->
clk)) {
662 ret = PTR_ERR(alg_data->
clk);
667 alg_data->
mif.timer.function = i2c_pnx_timeout;
668 alg_data->
mif.timer.data = (
unsigned long)alg_data;
676 dev_err(&pdev->
dev,
"Unable to get mem resource.\n");
683 "I/O region 0x%08x for I2C already in use.\n",
692 dev_err(&pdev->
dev,
"Couldn't ioremap I2C I/O region\n");
714 tmp = (freq / speed) / 2 - 2;
721 if (wait_reset(alg_data)) {
725 init_completion(&alg_data->
mif.complete);
728 if (alg_data->
irq < 0) {
729 dev_err(&pdev->
dev,
"Failed to get IRQ from platform resource\n");
733 0, pdev->
name, alg_data);
740 dev_err(&pdev->
dev,
"I2C: Failed to add bus\n");
746 dev_dbg(&pdev->
dev,
"%s: Master at %#8x, irq %d.\n",
764 platform_set_drvdata(pdev,
NULL);
779 platform_set_drvdata(pdev,
NULL);
799 .probe = i2c_pnx_probe,
803 static int __init i2c_adap_pnx_init(
void)
808 static void __exit i2c_adap_pnx_exit(
void)