Lines Matching full:i2c
5 * Internal I2C bus driver for NetUP Universal Dual DVB-CI
65 irqreturn_t netup_i2c_interrupt(struct netup_i2c *i2c) in netup_i2c_interrupt() argument
71 spin_lock_irqsave(&i2c->lock, flags); in netup_i2c_interrupt()
72 reg = readw(&i2c->regs->twi_ctrl0_stat); in netup_i2c_interrupt()
73 writew(reg & ~TWI_IRQEN, &i2c->regs->twi_ctrl0_stat); in netup_i2c_interrupt()
74 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
77 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
79 i2c->state = STATE_DONE; in netup_i2c_interrupt()
83 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
85 i2c->state = STATE_ERROR; in netup_i2c_interrupt()
89 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
91 i2c->state = STATE_ERROR; in netup_i2c_interrupt()
95 tmp = readw(&i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_interrupt()
96 writew(tmp & ~FIFO_IRQEN, &i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_interrupt()
97 i2c->state = STATE_WANT_READ; in netup_i2c_interrupt()
98 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
103 tmp = readw(&i2c->regs->tx_fifo.stat_ctrl); in netup_i2c_interrupt()
104 writew(tmp & ~FIFO_IRQEN, &i2c->regs->tx_fifo.stat_ctrl); in netup_i2c_interrupt()
105 i2c->state = STATE_WANT_WRITE; in netup_i2c_interrupt()
106 dev_dbg(i2c->adap.dev.parent, in netup_i2c_interrupt()
110 dev_warn(&i2c->adap.dev, "%s(): not mine interrupt\n", __func__); in netup_i2c_interrupt()
113 spin_unlock_irqrestore(&i2c->lock, flags); in netup_i2c_interrupt()
115 wake_up(&i2c->wq); in netup_i2c_interrupt()
119 static void netup_i2c_reset(struct netup_i2c *i2c) in netup_i2c_reset() argument
121 dev_dbg(i2c->adap.dev.parent, "%s()\n", __func__); in netup_i2c_reset()
122 i2c->state = STATE_DONE; in netup_i2c_reset()
123 writew(TWI_SOFT_RESET, &i2c->regs->twi_addr_ctrl1); in netup_i2c_reset()
124 writew(TWI_CLKDIV, &i2c->regs->clkdiv); in netup_i2c_reset()
125 writew(FIFO_RESET, &i2c->regs->tx_fifo.stat_ctrl); in netup_i2c_reset()
126 writew(FIFO_RESET, &i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_reset()
127 writew(0x800, &i2c->regs->tx_fifo.stat_ctrl); in netup_i2c_reset()
128 writew(0x800, &i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_reset()
131 static void netup_i2c_fifo_tx(struct netup_i2c *i2c) in netup_i2c_fifo_tx() argument
135 (readw(&i2c->regs->tx_fifo.stat_ctrl) & 0x3f); in netup_i2c_fifo_tx()
136 u32 msg_length = i2c->msg->len - i2c->xmit_size; in netup_i2c_fifo_tx()
140 data = i2c->msg->buf[i2c->xmit_size++]; in netup_i2c_fifo_tx()
141 writeb(data, &i2c->regs->tx_fifo.data8); in netup_i2c_fifo_tx()
142 dev_dbg(i2c->adap.dev.parent, in netup_i2c_fifo_tx()
145 if (i2c->xmit_size < i2c->msg->len) { in netup_i2c_fifo_tx()
146 dev_dbg(i2c->adap.dev.parent, in netup_i2c_fifo_tx()
148 writew(readw(&i2c->regs->tx_fifo.stat_ctrl) | FIFO_IRQEN, in netup_i2c_fifo_tx()
149 &i2c->regs->tx_fifo.stat_ctrl); in netup_i2c_fifo_tx()
153 static void netup_i2c_fifo_rx(struct netup_i2c *i2c) in netup_i2c_fifo_rx() argument
156 u32 fifo_size = readw(&i2c->regs->rx_fifo.stat_ctrl) & 0x3f; in netup_i2c_fifo_rx()
158 dev_dbg(i2c->adap.dev.parent, in netup_i2c_fifo_rx()
161 data = readb(&i2c->regs->rx_fifo.data8); in netup_i2c_fifo_rx()
162 if ((i2c->msg->flags & I2C_M_RD) != 0 && in netup_i2c_fifo_rx()
163 i2c->xmit_size < i2c->msg->len) { in netup_i2c_fifo_rx()
164 i2c->msg->buf[i2c->xmit_size++] = data; in netup_i2c_fifo_rx()
165 dev_dbg(i2c->adap.dev.parent, in netup_i2c_fifo_rx()
169 if (i2c->xmit_size < i2c->msg->len) { in netup_i2c_fifo_rx()
170 dev_dbg(i2c->adap.dev.parent, in netup_i2c_fifo_rx()
172 writew(readw(&i2c->regs->rx_fifo.stat_ctrl) | FIFO_IRQEN, in netup_i2c_fifo_rx()
173 &i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_fifo_rx()
177 static void netup_i2c_start_xfer(struct netup_i2c *i2c) in netup_i2c_start_xfer() argument
179 u16 rdflag = ((i2c->msg->flags & I2C_M_RD) ? 1 : 0); in netup_i2c_start_xfer()
180 u16 reg = readw(&i2c->regs->twi_ctrl0_stat); in netup_i2c_start_xfer()
182 writew(TWI_IRQEN | reg, &i2c->regs->twi_ctrl0_stat); in netup_i2c_start_xfer()
183 writew(i2c->msg->len, &i2c->regs->length); in netup_i2c_start_xfer()
184 writew(TWI_TRANSFER | (i2c->msg->addr << 1) | rdflag, in netup_i2c_start_xfer()
185 &i2c->regs->twi_addr_ctrl1); in netup_i2c_start_xfer()
186 dev_dbg(i2c->adap.dev.parent, in netup_i2c_start_xfer()
188 __func__, readw(&i2c->regs->length), in netup_i2c_start_xfer()
189 readw(&i2c->regs->twi_addr_ctrl1), in netup_i2c_start_xfer()
190 readw(&i2c->regs->twi_ctrl0_stat)); in netup_i2c_start_xfer()
191 i2c->state = STATE_WAIT; in netup_i2c_start_xfer()
192 i2c->xmit_size = 0; in netup_i2c_start_xfer()
194 netup_i2c_fifo_tx(i2c); in netup_i2c_start_xfer()
196 writew(FIFO_IRQEN | readw(&i2c->regs->rx_fifo.stat_ctrl), in netup_i2c_start_xfer()
197 &i2c->regs->rx_fifo.stat_ctrl); in netup_i2c_start_xfer()
205 struct netup_i2c *i2c = i2c_get_adapdata(adap); in netup_i2c_xfer() local
208 spin_lock_irqsave(&i2c->lock, flags); in netup_i2c_xfer()
209 if (i2c->state != STATE_DONE) { in netup_i2c_xfer()
210 dev_dbg(i2c->adap.dev.parent, in netup_i2c_xfer()
211 "%s(): i2c->state == %d, resetting I2C\n", in netup_i2c_xfer()
212 __func__, i2c->state); in netup_i2c_xfer()
213 netup_i2c_reset(i2c); in netup_i2c_xfer()
215 dev_dbg(i2c->adap.dev.parent, "%s() num %d\n", __func__, num); in netup_i2c_xfer()
217 i2c->msg = &msgs[i]; in netup_i2c_xfer()
218 netup_i2c_start_xfer(i2c); in netup_i2c_xfer()
221 spin_unlock_irqrestore(&i2c->lock, flags); in netup_i2c_xfer()
222 if (wait_event_timeout(i2c->wq, in netup_i2c_xfer()
223 i2c->state != STATE_WAIT, in netup_i2c_xfer()
225 spin_lock_irqsave(&i2c->lock, flags); in netup_i2c_xfer()
226 switch (i2c->state) { in netup_i2c_xfer()
228 netup_i2c_fifo_rx(i2c); in netup_i2c_xfer()
231 netup_i2c_fifo_tx(i2c); in netup_i2c_xfer()
234 if ((i2c->msg->flags & I2C_M_RD) != 0 && in netup_i2c_xfer()
235 i2c->xmit_size != i2c->msg->len) in netup_i2c_xfer()
236 netup_i2c_fifo_rx(i2c); in netup_i2c_xfer()
237 dev_dbg(i2c->adap.dev.parent, in netup_i2c_xfer()
244 dev_dbg(i2c->adap.dev.parent, in netup_i2c_xfer()
249 dev_dbg(i2c->adap.dev.parent, in netup_i2c_xfer()
251 __func__, i2c->state); in netup_i2c_xfer()
256 i2c->state = STATE_WAIT; in netup_i2c_xfer()
258 &i2c->regs->twi_ctrl0_stat); in netup_i2c_xfer()
260 &i2c->regs->twi_ctrl0_stat); in netup_i2c_xfer()
262 spin_unlock_irqrestore(&i2c->lock, flags); in netup_i2c_xfer()
264 spin_lock_irqsave(&i2c->lock, flags); in netup_i2c_xfer()
265 dev_dbg(i2c->adap.dev.parent, in netup_i2c_xfer()
270 spin_lock_irqsave(&i2c->lock, flags); in netup_i2c_xfer()
274 spin_unlock_irqrestore(&i2c->lock, flags); in netup_i2c_xfer()
275 dev_dbg(i2c->adap.dev.parent, "%s(): result %d\n", __func__, res); in netup_i2c_xfer()
299 struct netup_i2c *i2c; in netup_i2c_init() local
306 i2c = &ndev->i2c[bus_num]; in netup_i2c_init()
307 spin_lock_init(&i2c->lock); in netup_i2c_init()
308 init_waitqueue_head(&i2c->wq); in netup_i2c_init()
309 i2c->regs = (struct netup_i2c_regs __iomem *)(ndev->bmmio0 + in netup_i2c_init()
311 netup_i2c_reset(i2c); in netup_i2c_init()
312 i2c->adap = netup_i2c_adapter; in netup_i2c_init()
313 i2c->adap.dev.parent = &ndev->pci_dev->dev; in netup_i2c_init()
314 i2c_set_adapdata(&i2c->adap, i2c); in netup_i2c_init()
315 ret = i2c_add_adapter(&i2c->adap); in netup_i2c_init()
319 "%s(): registered I2C bus %d at 0x%x\n", in netup_i2c_init()
329 struct netup_i2c *i2c; in netup_i2c_remove() local
336 i2c = &ndev->i2c[bus_num]; in netup_i2c_remove()
337 netup_i2c_reset(i2c); in netup_i2c_remove()
339 i2c_del_adapter(&i2c->adap); in netup_i2c_remove()
341 "netup_i2c_remove: unregistered I2C bus %d\n", bus_num); in netup_i2c_remove()