15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/i2c.h>
20 #include <linux/time.h>
23 #include <linux/errno.h>
28 #include <linux/slab.h>
47 #define I2CSTART 0x018
49 #define ARBIT_LOST 0x200
50 #define SLAVE_ACK 0x800
54 #define I2C_CMD_START 0x10
55 #define I2C_CMD_STOP 0x08
56 #define I2C_CMD_READ 0x04
57 #define I2C_CMD_WRITE 0x02
58 #define I2C_CMD_NACK 0x01
97 static inline void nuc900_i2c_master_complete(
struct nuc900_i2c *i2c,
int ret)
99 dev_dbg(i2c->
dev,
"master_complete %d\n", ret);
113 static inline void nuc900_i2c_disable_irq(
struct nuc900_i2c *i2c)
121 static inline void nuc900_i2c_enable_irq(
struct nuc900_i2c *i2c)
135 static void nuc900_i2c_message_start(
struct nuc900_i2c *i2c,
138 unsigned int addr = (msg->
addr & 0x7f) << 1;
146 static inline void nuc900_i2c_stop(
struct nuc900_i2c *i2c,
int ret)
155 nuc900_i2c_master_complete(i2c, ret);
156 nuc900_i2c_disable_irq(i2c);
168 static inline int is_lastmsg(
struct nuc900_i2c *i2c)
178 static inline int is_msglast(
struct nuc900_i2c *i2c)
188 static inline int is_msgend(
struct nuc900_i2c *i2c)
198 static void i2c_nuc900_irq_nextbyte(
struct nuc900_i2c *i2c,
199 unsigned long iicstat)
203 switch (i2c->
state) {
206 dev_err(i2c->
dev,
"%s: called in STATE_IDLE\n", __func__);
210 dev_err(i2c->
dev,
"%s: called in STATE_STOP\n", __func__);
211 nuc900_i2c_disable_irq(i2c);
224 nuc900_i2c_stop(i2c, -
ENXIO);
237 if (is_lastmsg(i2c) && i2c->
msg->len == 0) {
238 nuc900_i2c_stop(i2c, 0);
265 if (!is_msgend(i2c)) {
270 }
else if (!is_lastmsg(i2c)) {
288 nuc900_i2c_stop(i2c, -
EINVAL);
294 nuc900_i2c_message_start(i2c, i2c->
msg);
301 nuc900_i2c_stop(i2c, 0);
315 if (is_msglast(i2c)) {
322 }
else if (is_msgend(i2c)) {
327 if (is_lastmsg(i2c)) {
331 nuc900_i2c_stop(i2c, 0);
366 dev_err(i2c->
dev,
"deal with arbitration loss\n");
371 dev_dbg(i2c->
dev,
"IRQ: error i2c->state == IDLE\n");
379 i2c_nuc900_irq_nextbyte(i2c, status);
391 static int nuc900_i2c_set_master(
struct nuc900_i2c *i2c)
395 while (timeout-- > 0) {
412 static int nuc900_i2c_doxfer(
struct nuc900_i2c *i2c,
415 unsigned long iicstat, timeout;
419 ret = nuc900_i2c_set_master(i2c);
421 dev_err(i2c->
dev,
"cannot get bus (error %d)\n", ret);
426 spin_lock_irq(&i2c->
lock);
434 nuc900_i2c_message_start(i2c, msgs);
435 spin_unlock_irq(&i2c->
lock);
448 dev_dbg(i2c->
dev,
"incomplete xfer (%d)\n", ret);
457 }
while ((iicstat &
I2CBUSY) && --spins);
465 if (iicstat & I2CBUSY)
466 dev_warn(i2c->
dev,
"timeout waiting for bus idle\n");
485 nuc900_i2c_enable_irq(i2c);
489 ret = nuc900_i2c_doxfer(i2c, msgs, num);
512 .master_xfer = nuc900_i2c_xfer,
513 .functionality = nuc900_i2c_func,
528 pdata = pdev->
dev.platform_data;
542 i2c->
adap.algo = &nuc900_i2c_algorithm;
543 i2c->
adap.retries = 2;
553 if (IS_ERR(i2c->
clk)) {
567 dev_err(&pdev->
dev,
"cannot find IO resource\n");
589 dev_dbg(&pdev->
dev,
"registers %p (%p, %p)\n",
594 i2c->
adap.algo_data = i2c;
595 i2c->
adap.dev.parent = &pdev->
dev;
601 ret = (i2c->
clk.apbfreq)/(pdata->
bus_freq * 5) - 1;
615 dev_name(&pdev->
dev), i2c);
632 dev_err(&pdev->
dev,
"failed to add bus to i2c core\n");
636 platform_set_drvdata(pdev, i2c);
639 dev_name(&i2c->
adap.dev));
668 struct nuc900_i2c *i2c = platform_get_drvdata(pdev);
686 .probe = nuc900_i2c_probe,
690 .name =
"nuc900-i2c0",
694 static int __init i2c_adap_nuc900_init(
void)
699 static void __exit i2c_adap_nuc900_exit(
void)