Lines Matching full:i2c
7 * This file contains the shared part of the driver for the i2c adapter in
16 #include <linux/i2c.h>
21 #include "i2c-octeon-core.h"
26 struct octeon_i2c *i2c = dev_id; in octeon_i2c_isr() local
28 i2c->int_disable(i2c); in octeon_i2c_isr()
29 wake_up(&i2c->queue); in octeon_i2c_isr()
34 static bool octeon_i2c_test_iflg(struct octeon_i2c *i2c) in octeon_i2c_test_iflg() argument
36 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG); in octeon_i2c_test_iflg()
41 * @i2c: The struct octeon_i2c
45 static int octeon_i2c_wait(struct octeon_i2c *i2c) in octeon_i2c_wait() argument
53 if (i2c->broken_irq_mode) { in octeon_i2c_wait()
54 u64 end = get_jiffies_64() + i2c->adap.timeout; in octeon_i2c_wait()
56 while (!octeon_i2c_test_iflg(i2c) && in octeon_i2c_wait()
60 return octeon_i2c_test_iflg(i2c) ? 0 : -ETIMEDOUT; in octeon_i2c_wait()
63 i2c->int_enable(i2c); in octeon_i2c_wait()
64 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c), in octeon_i2c_wait()
65 i2c->adap.timeout); in octeon_i2c_wait()
66 i2c->int_disable(i2c); in octeon_i2c_wait()
68 if (i2c->broken_irq_check && !time_left && in octeon_i2c_wait()
69 octeon_i2c_test_iflg(i2c)) { in octeon_i2c_wait()
70 dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n"); in octeon_i2c_wait()
71 i2c->broken_irq_mode = true; in octeon_i2c_wait()
81 static bool octeon_i2c_hlc_test_valid(struct octeon_i2c *i2c) in octeon_i2c_hlc_test_valid() argument
83 return (__raw_readq(i2c->twsi_base + SW_TWSI(i2c)) & SW_TWSI_V) == 0; in octeon_i2c_hlc_test_valid()
86 static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c) in octeon_i2c_hlc_int_clear() argument
89 octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT); in octeon_i2c_hlc_int_clear()
95 static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c) in octeon_i2c_hlc_enable() argument
100 if (i2c->hlc_enabled) in octeon_i2c_hlc_enable()
102 i2c->hlc_enabled = true; in octeon_i2c_hlc_enable()
105 val = octeon_i2c_ctl_read(i2c); in octeon_i2c_hlc_enable()
111 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_hlc_enable()
121 octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB); in octeon_i2c_hlc_enable()
124 static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c) in octeon_i2c_hlc_disable() argument
126 if (!i2c->hlc_enabled) in octeon_i2c_hlc_disable()
129 i2c->hlc_enabled = false; in octeon_i2c_hlc_disable()
130 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_hlc_disable()
135 * @i2c: The struct octeon_i2c
139 static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c) in octeon_i2c_hlc_wait() argument
147 if (i2c->broken_irq_mode) { in octeon_i2c_hlc_wait()
148 u64 end = get_jiffies_64() + i2c->adap.timeout; in octeon_i2c_hlc_wait()
150 while (!octeon_i2c_hlc_test_valid(i2c) && in octeon_i2c_hlc_wait()
154 return octeon_i2c_hlc_test_valid(i2c) ? 0 : -ETIMEDOUT; in octeon_i2c_hlc_wait()
157 i2c->hlc_int_enable(i2c); in octeon_i2c_hlc_wait()
158 time_left = wait_event_timeout(i2c->queue, in octeon_i2c_hlc_wait()
159 octeon_i2c_hlc_test_valid(i2c), in octeon_i2c_hlc_wait()
160 i2c->adap.timeout); in octeon_i2c_hlc_wait()
161 i2c->hlc_int_disable(i2c); in octeon_i2c_hlc_wait()
163 octeon_i2c_hlc_int_clear(i2c); in octeon_i2c_hlc_wait()
165 if (i2c->broken_irq_check && !time_left && in octeon_i2c_hlc_wait()
166 octeon_i2c_hlc_test_valid(i2c)) { in octeon_i2c_hlc_wait()
167 dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n"); in octeon_i2c_hlc_wait()
168 i2c->broken_irq_mode = true; in octeon_i2c_hlc_wait()
177 static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read) in octeon_i2c_check_status() argument
185 if (i2c->hlc_enabled) in octeon_i2c_check_status()
186 stat = __raw_readq(i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_check_status()
188 stat = octeon_i2c_stat_read(i2c); in octeon_i2c_check_status()
243 dev_err(i2c->dev, "unhandled state: %d\n", stat); in octeon_i2c_check_status()
248 static int octeon_i2c_recovery(struct octeon_i2c *i2c) in octeon_i2c_recovery() argument
252 ret = i2c_recover_bus(&i2c->adap); in octeon_i2c_recovery()
255 ret = octeon_i2c_init_lowlevel(i2c); in octeon_i2c_recovery()
261 * @i2c: The struct octeon_i2c
265 static int octeon_i2c_start(struct octeon_i2c *i2c) in octeon_i2c_start() argument
270 octeon_i2c_hlc_disable(i2c); in octeon_i2c_start()
272 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA); in octeon_i2c_start()
273 ret = octeon_i2c_wait(i2c); in octeon_i2c_start()
277 stat = octeon_i2c_stat_read(i2c); in octeon_i2c_start()
284 ret = octeon_i2c_recovery(i2c); in octeon_i2c_start()
289 static void octeon_i2c_stop(struct octeon_i2c *i2c) in octeon_i2c_stop() argument
291 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP); in octeon_i2c_stop()
296 * @i2c: The struct octeon_i2c
306 static int octeon_i2c_read(struct octeon_i2c *i2c, int target, in octeon_i2c_read() argument
312 octeon_i2c_data_write(i2c, (target << 1) | 1); in octeon_i2c_read()
313 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_read()
315 result = octeon_i2c_wait(i2c); in octeon_i2c_read()
320 result = octeon_i2c_check_status(i2c, false); in octeon_i2c_read()
338 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_read()
340 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK); in octeon_i2c_read()
342 result = octeon_i2c_wait(i2c); in octeon_i2c_read()
346 data[i] = octeon_i2c_data_read(i2c, &result); in octeon_i2c_read()
355 result = octeon_i2c_check_status(i2c, final_read); in octeon_i2c_read()
365 * @i2c: The struct octeon_i2c
374 static int octeon_i2c_write(struct octeon_i2c *i2c, int target, in octeon_i2c_write() argument
379 octeon_i2c_data_write(i2c, target << 1); in octeon_i2c_write()
380 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_write()
382 result = octeon_i2c_wait(i2c); in octeon_i2c_write()
387 result = octeon_i2c_check_status(i2c, false); in octeon_i2c_write()
391 octeon_i2c_data_write(i2c, data[i]); in octeon_i2c_write()
392 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_write()
394 result = octeon_i2c_wait(i2c); in octeon_i2c_write()
403 static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs) in octeon_i2c_hlc_read() argument
408 octeon_i2c_hlc_enable(i2c); in octeon_i2c_hlc_read()
409 octeon_i2c_hlc_int_clear(i2c); in octeon_i2c_hlc_read()
422 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_read()
423 ret = octeon_i2c_hlc_wait(i2c); in octeon_i2c_hlc_read()
427 cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_read()
429 return octeon_i2c_check_status(i2c, false); in octeon_i2c_hlc_read()
435 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT(i2c)); in octeon_i2c_hlc_read()
445 static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs) in octeon_i2c_hlc_write() argument
450 octeon_i2c_hlc_enable(i2c); in octeon_i2c_hlc_write()
451 octeon_i2c_hlc_int_clear(i2c); in octeon_i2c_hlc_write()
472 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c)); in octeon_i2c_hlc_write()
475 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_write()
476 ret = octeon_i2c_hlc_wait(i2c); in octeon_i2c_hlc_write()
480 cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_write()
482 return octeon_i2c_check_status(i2c, false); in octeon_i2c_hlc_write()
489 static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs) in octeon_i2c_hlc_comp_read() argument
494 octeon_i2c_hlc_enable(i2c); in octeon_i2c_hlc_comp_read()
513 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c)); in octeon_i2c_hlc_comp_read()
518 octeon_i2c_hlc_int_clear(i2c); in octeon_i2c_hlc_comp_read()
519 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_comp_read()
521 ret = octeon_i2c_hlc_wait(i2c); in octeon_i2c_hlc_comp_read()
525 cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_comp_read()
527 return octeon_i2c_check_status(i2c, false); in octeon_i2c_hlc_comp_read()
533 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT(i2c)); in octeon_i2c_hlc_comp_read()
543 static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs) in octeon_i2c_hlc_comp_write() argument
549 octeon_i2c_hlc_enable(i2c); in octeon_i2c_hlc_comp_write()
580 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT(i2c)); in octeon_i2c_hlc_comp_write()
582 octeon_i2c_hlc_int_clear(i2c); in octeon_i2c_hlc_comp_write()
583 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_comp_write()
585 ret = octeon_i2c_hlc_wait(i2c); in octeon_i2c_hlc_comp_write()
589 cmd = __raw_readq(i2c->twsi_base + SW_TWSI(i2c)); in octeon_i2c_hlc_comp_write()
591 return octeon_i2c_check_status(i2c, false); in octeon_i2c_hlc_comp_write()
607 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_xfer() local
613 ret = octeon_i2c_hlc_read(i2c, msgs); in octeon_i2c_xfer()
615 ret = octeon_i2c_hlc_write(i2c, msgs); in octeon_i2c_xfer()
625 ret = octeon_i2c_hlc_comp_read(i2c, msgs); in octeon_i2c_xfer()
627 ret = octeon_i2c_hlc_comp_write(i2c, msgs); in octeon_i2c_xfer()
641 ret = octeon_i2c_start(i2c); in octeon_i2c_xfer()
646 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf, in octeon_i2c_xfer()
649 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf, in octeon_i2c_xfer()
652 octeon_i2c_stop(i2c); in octeon_i2c_xfer()
658 void octeon_i2c_set_clock(struct octeon_i2c *i2c) in octeon_i2c_set_clock() argument
673 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10; in octeon_i2c_set_clock()
675 thp_base = (i2c->sys_freq / (tclk * 2)) - 1; in octeon_i2c_set_clock()
682 foscl = i2c->sys_freq / (2 * (thp_idx + 1)); in octeon_i2c_set_clock()
685 diff = abs(foscl - i2c->twsi_freq); in octeon_i2c_set_clock()
695 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp); in octeon_i2c_set_clock()
696 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv); in octeon_i2c_set_clock()
699 int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c) in octeon_i2c_init_lowlevel() argument
705 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0); in octeon_i2c_init_lowlevel()
709 status = octeon_i2c_stat_read(i2c); in octeon_i2c_init_lowlevel()
715 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", in octeon_i2c_init_lowlevel()
721 octeon_i2c_hlc_enable(i2c); in octeon_i2c_init_lowlevel()
722 octeon_i2c_hlc_disable(i2c); in octeon_i2c_init_lowlevel()
728 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_get_scl() local
731 state = octeon_i2c_read_int(i2c); in octeon_i2c_get_scl()
737 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_set_scl() local
739 octeon_i2c_write_int(i2c, val ? 0 : TWSI_INT_SCL_OVR); in octeon_i2c_set_scl()
744 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_get_sda() local
747 state = octeon_i2c_read_int(i2c); in octeon_i2c_get_sda()
753 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_prepare_recovery() local
755 octeon_i2c_hlc_disable(i2c); in octeon_i2c_prepare_recovery()
756 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0); in octeon_i2c_prepare_recovery()
764 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); in octeon_i2c_prepare_recovery()
766 octeon_i2c_write_int(i2c, 0); in octeon_i2c_prepare_recovery()
771 struct octeon_i2c *i2c = i2c_get_adapdata(adap); in octeon_i2c_unprepare_recovery() local
778 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR); in octeon_i2c_unprepare_recovery()
780 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR); in octeon_i2c_unprepare_recovery()
782 octeon_i2c_write_int(i2c, 0); in octeon_i2c_unprepare_recovery()