16 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/i2c.h>
30 #define DRIVER_NAME "nmk-i2c"
33 #define I2C_CR (0x000)
34 #define I2C_SCR (0x004)
35 #define I2C_HSMCR (0x008)
36 #define I2C_MCR (0x00C)
37 #define I2C_TFR (0x010)
38 #define I2C_SR (0x014)
39 #define I2C_RFR (0x018)
40 #define I2C_TFTR (0x01C)
41 #define I2C_RFTR (0x020)
42 #define I2C_DMAR (0x024)
43 #define I2C_BRCR (0x028)
44 #define I2C_IMSCR (0x02C)
45 #define I2C_RISR (0x030)
46 #define I2C_MISR (0x034)
47 #define I2C_ICR (0x038)
50 #define I2C_CR_PE (0x1 << 0)
51 #define I2C_CR_OM (0x3 << 1)
52 #define I2C_CR_SAM (0x1 << 3)
53 #define I2C_CR_SM (0x3 << 4)
54 #define I2C_CR_SGCM (0x1 << 6)
55 #define I2C_CR_FTX (0x1 << 7)
56 #define I2C_CR_FRX (0x1 << 8)
57 #define I2C_CR_DMA_TX_EN (0x1 << 9)
58 #define I2C_CR_DMA_RX_EN (0x1 << 10)
59 #define I2C_CR_DMA_SLE (0x1 << 11)
60 #define I2C_CR_LM (0x1 << 12)
61 #define I2C_CR_FON (0x3 << 13)
62 #define I2C_CR_FS (0x3 << 15)
65 #define I2C_MCR_OP (0x1 << 0)
66 #define I2C_MCR_A7 (0x7f << 1)
67 #define I2C_MCR_EA10 (0x7 << 8)
68 #define I2C_MCR_SB (0x1 << 11)
69 #define I2C_MCR_AM (0x3 << 12)
70 #define I2C_MCR_STOP (0x1 << 14)
71 #define I2C_MCR_LENGTH (0x7ff << 15)
74 #define I2C_SR_OP (0x3 << 0)
75 #define I2C_SR_STATUS (0x3 << 2)
76 #define I2C_SR_CAUSE (0x7 << 4)
77 #define I2C_SR_TYPE (0x3 << 7)
78 #define I2C_SR_LENGTH (0x7ff << 9)
81 #define I2C_IT_TXFE (0x1 << 0)
82 #define I2C_IT_TXFNE (0x1 << 1)
83 #define I2C_IT_TXFF (0x1 << 2)
84 #define I2C_IT_TXFOVR (0x1 << 3)
85 #define I2C_IT_RXFE (0x1 << 4)
86 #define I2C_IT_RXFNF (0x1 << 5)
87 #define I2C_IT_RXFF (0x1 << 6)
88 #define I2C_IT_RFSR (0x1 << 16)
89 #define I2C_IT_RFSE (0x1 << 17)
90 #define I2C_IT_WTSR (0x1 << 18)
91 #define I2C_IT_MTD (0x1 << 19)
92 #define I2C_IT_STD (0x1 << 20)
93 #define I2C_IT_MAL (0x1 << 24)
94 #define I2C_IT_BERR (0x1 << 25)
95 #define I2C_IT_MTDWS (0x1 << 28)
97 #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))
100 #define I2C_CLEAR_ALL_INTS 0x131f007f
103 #define IRQ_MASK(mask) (mask & 0x1fffffff)
106 #define MAX_I2C_FIFO_THRESHOLD 15
167 static const char *abort_causes[] = {
168 "no ack received after address transmission",
169 "no ack received during data phase",
170 "ack received after xmission of master code",
171 "master lost arbitration",
174 "overflow, maxsize is 2047 bytes",
182 static inline void i2c_clr_bit(
void __iomem *reg,
u32 mask)
196 #define LOOP_ATTEMPTS 10
220 "flushing operation timed out giving up after %d attempts",
230 static void disable_all_interrupts(
struct nmk_i2c_dev *dev)
240 static void clear_all_interrupts(
struct nmk_i2c_dev *dev)
255 stat = flush_i2c_fifo(dev);
262 disable_all_interrupts(dev);
264 clear_all_interrupts(dev);
273 #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
283 unsigned short slave_adr_3msb_bits;
296 slave_adr_3msb_bits = (dev->
cli.slave_adr >> 7) & 0x7;
328 static void setup_i2c_controller(
struct nmk_i2c_dev *dev)
357 div = (dev->
cfg.clk_freq > 100000) ? 3 : 2;
367 brcr2 = (i2c_clk/(dev->
cfg.clk_freq *
div)) & 0xffff;
380 "do not support this mode defaulting to std. mode\n");
381 brcr2 = i2c_clk/(100000 * 2) & 0xffff;
409 mcr = load_i2c_mcr_reg(dev, flags);
440 "wait_for_completion_timeout "
441 "returned %d waiting for event\n", timeout);
447 dev_err(&dev->
adev->dev,
"read from slave 0x%x timed out\n",
454 static void fill_tx_fifo(
struct nmk_i2c_dev *dev,
int no_bytes)
458 for (count = (no_bytes - 2);
460 (dev->
cli.count != 0);
467 dev->
cli.xfer_bytes++;
486 mcr = load_i2c_mcr_reg(dev, flags);
505 if (dev->
cli.count != 0)
528 "wait_for_completion_timeout "
529 "returned %d waiting for event\n", timeout);
535 dev_err(&dev->
adev->dev,
"write to slave 0x%x timed out\n",
555 status = read_i2c(dev, flags);
559 status = write_i2c(dev, flags);
562 if (status || (dev->
result)) {
571 if (((i2c_sr >> 2) & 0x3) == 0x3) {
573 cause = (i2c_sr >> 4) & 0x7;
577 abort_causes[cause]);
582 status = status ? status : dev->
result;
635 static int nmk_i2c_xfer(
struct i2c_adapter *i2c_adap,
636 struct i2c_msg msgs[],
int num_msgs)
640 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
645 pm_runtime_get_sync(&dev->
adev->dev);
647 status = clk_prepare_enable(dev->
clk);
649 dev_err(&dev->
adev->dev,
"can't prepare_enable clock\n");
653 status = init_hw(dev);
658 for (j = 0; j < 3; j++) {
660 setup_i2c_controller(dev);
662 for (i = 0; i < num_msgs; i++) {
666 dev->
stop = (i < (num_msgs - 1)) ? 0 : 1;
669 status = nmk_i2c_xfer_one(dev, msgs[i].flags);
678 clk_disable_unprepare(dev->
clk);
680 pm_runtime_put_sync(&dev->
adev->dev);
731 switch ((1 << src)) {
748 if (dev->
cli.count == 0)
761 for (count = rft; count > 0; count--) {
766 dev->
cli.count -= rft;
767 dev->
cli.xfer_bytes += rft;
786 if (dev->
cli.count == 0)
792 dev->
cli.xfer_bytes++;
796 disable_all_interrupts(dev);
797 clear_all_interrupts(dev);
799 if (dev->
cli.count) {
802 "%lu bytes still remain to be xfered\n",
861 dev_err(&dev->
adev->dev,
"spurious Interrupt..\n");
886 #define nmk_i2c_suspend NULL
887 #define nmk_i2c_resume NULL
900 static unsigned int nmk_i2c_functionality(
struct i2c_adapter *adap)
906 .master_xfer = nmk_i2c_xfer,
907 .functionality = nmk_i2c_functionality
923 static void nmk_i2c_of_probe(
struct device_node *np,
926 of_property_read_u32(np,
"clock-frequency", &pdata->
clk_freq);
954 nmk_i2c_of_probe(np, pdata);
962 dev_err(&adev->
dev,
"cannot allocate memory\n");
984 pm_suspend_ignore_children(&adev->
dev,
true);
987 if (IS_ERR(dev->
clk)) {
988 dev_err(&adev->
dev,
"could not get i2c clock\n");
989 ret = PTR_ERR(dev->
clk);
994 adap->
dev.of_node = np;
995 adap->
dev.parent = &adev->
dev;
998 adap->
algo = &nmk_i2c_algo;
1002 "Nomadik I2C%d at %pR", adap->
nr, &adev->
res);
1008 dev->
cfg.tft = pdata->
tft;
1009 dev->
cfg.rft = pdata->
rft;
1010 dev->
cfg.sm = pdata->
sm;
1012 i2c_set_adapdata(adap, dev);
1015 "initialize %s on virtual base %p\n",
1020 dev_err(&adev->
dev,
"failed to add adapter\n");
1026 pm_runtime_put(&adev->
dev);
1044 static int nmk_i2c_remove(
struct amba_device *adev)
1050 flush_i2c_fifo(dev);
1051 disable_all_interrupts(dev);
1052 clear_all_interrupts(dev);
1060 pm_runtime_disable(&adev->
dev);
1067 static struct amba_id nmk_i2c_ids[] = {
1087 .id_table = nmk_i2c_ids,
1088 .probe = nmk_i2c_probe,
1089 .remove = nmk_i2c_remove,
1092 static int __init nmk_i2c_init(
void)
1097 static void __exit nmk_i2c_exit(
void)