Lines Matching +full:i2c +full:- +full:retry +full:- +full:count
2 * I2C bus driver for the SH7760 I2C Interfaces.
4 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
13 #include <linux/i2c.h>
22 #include <asm/i2c-sh7760.h>
38 #define I2CRFDR 0x34 /* rx fifo count */
39 #define I2CTFDR 0x38 /* tx fifo count */
43 #define MCR_MDBS 0x80 /* non-fifo mode switch */
104 __raw_writel(val, (unsigned long)cam->iobase + reg); in OUT32()
109 return __raw_readl((unsigned long)cam->iobase + reg); in IN32()
115 struct i2c_msg *msg = id->msg; in sh7760_i2c_irq()
116 char *data = msg->buf; in sh7760_i2c_irq()
127 id->status |= IDS_DONE | IDS_ARBLOST; in sh7760_i2c_irq()
135 * do a normal i2c stop. Then wait at least 1 ms before in sh7760_i2c_irq()
145 id->status |= IDS_NACK; in sh7760_i2c_irq()
151 /* i2c-stop was sent */ in sh7760_i2c_irq()
153 id->status |= IDS_DONE; in sh7760_i2c_irq()
157 /* i2c slave addr was sent; set to "normal" operation */ in sh7760_i2c_irq()
165 if (msg->len <= len) { in sh7760_i2c_irq()
166 if (id->flags & IDF_STOP) { in sh7760_i2c_irq()
173 id->status |= IDS_DONE; in sh7760_i2c_irq()
181 while (msg->len && len) { in sh7760_i2c_irq()
183 msg->len--; in sh7760_i2c_irq()
184 len--; in sh7760_i2c_irq()
187 if (msg->len) { in sh7760_i2c_irq()
188 len = (msg->len >= FIFO_SIZE) ? FIFO_SIZE - 1 in sh7760_i2c_irq()
189 : msg->len - 1; in sh7760_i2c_irq()
194 } else if (id->flags & IDF_SEND) { in sh7760_i2c_irq()
195 if ((fsr & FSR_TEND) && (msg->len < 1)) { in sh7760_i2c_irq()
196 if (id->flags & IDF_STOP) { in sh7760_i2c_irq()
199 id->status |= IDS_DONE; in sh7760_i2c_irq()
208 while (msg->len && (IN32(id, I2CTFDR) < FIFO_SIZE)) { in sh7760_i2c_irq()
210 msg->len--; in sh7760_i2c_irq()
213 if (msg->len < 1) { in sh7760_i2c_irq()
217 len = (msg->len >= FIFO_SIZE) ? 2 : 0; in sh7760_i2c_irq()
224 if (id->status & IDS_DONE) { in sh7760_i2c_irq()
227 id->msg = NULL; in sh7760_i2c_irq()
228 complete(&id->xfer_done); in sh7760_i2c_irq()
244 id->flags |= IDF_RECV; in sh7760_i2c_mrecv()
248 OUT32(id, I2CMAR, (id->msg->addr << 1) | 1); in sh7760_i2c_mrecv()
251 if (id->msg->len >= FIFO_SIZE) in sh7760_i2c_mrecv()
252 len = FIFO_SIZE - 1; /* trigger at fifo full */ in sh7760_i2c_mrecv()
254 len = id->msg->len - 1; /* trigger before all received */ in sh7760_i2c_mrecv()
270 id->flags |= IDF_SEND; in sh7760_i2c_msend()
274 OUT32(id, I2CMAR, (id->msg->addr << 1) | 0); in sh7760_i2c_msend()
277 if (id->msg->len >= FIFO_SIZE) in sh7760_i2c_msend()
285 while (id->msg->len && IN32(id, I2CTFDR) < FIFO_SIZE) { in sh7760_i2c_msend()
286 OUT32(id, I2CRXTX, *(id->msg->buf)); in sh7760_i2c_msend()
287 (id->msg->len)--; in sh7760_i2c_msend()
288 (id->msg->buf)++; in sh7760_i2c_msend()
295 OUT32(id, I2CFIER, FIER_TEIE | (id->msg->len ? FIER_TXIE : 0)); in sh7760_i2c_msend()
307 struct cami2c *id = adap->algo_data; in sh7760_i2c_master_xfer()
311 dev_err(&adap->dev, "sh7760-i2c%d: bus busy!\n", adap->nr); in sh7760_i2c_master_xfer()
312 return -EBUSY; in sh7760_i2c_master_xfer()
317 retr = adap->retries; in sh7760_i2c_master_xfer()
318 retry: in sh7760_i2c_master_xfer()
319 id->flags = ((i == (num-1)) ? IDF_STOP : 0); in sh7760_i2c_master_xfer()
320 id->status = 0; in sh7760_i2c_master_xfer()
321 id->msg = msgs; in sh7760_i2c_master_xfer()
322 init_completion(&id->xfer_done); in sh7760_i2c_master_xfer()
324 if (msgs->flags & I2C_M_RD) in sh7760_i2c_master_xfer()
329 wait_for_completion(&id->xfer_done); in sh7760_i2c_master_xfer()
331 if (id->status == 0) { in sh7760_i2c_master_xfer()
332 num = -EIO; in sh7760_i2c_master_xfer()
336 if (id->status & IDS_NACK) { in sh7760_i2c_master_xfer()
337 /* wait a bit or i2c module stops working */ in sh7760_i2c_master_xfer()
339 num = -EREMOTEIO; in sh7760_i2c_master_xfer()
343 if (id->status & IDS_ARBLOST) { in sh7760_i2c_master_xfer()
344 if (retr--) { in sh7760_i2c_master_xfer()
346 goto retry; in sh7760_i2c_master_xfer()
348 num = -EREMOTEIO; in sh7760_i2c_master_xfer()
356 id->msg = NULL; in sh7760_i2c_master_xfer()
357 id->flags = 0; in sh7760_i2c_master_xfer()
358 id->status = 0; in sh7760_i2c_master_xfer()
387 * derived from I2C module clock (iclk) which in turn is derived from
403 mck = mclk->rate; in calc_CCR()
409 for (cdf = 3; cdf >= 0; cdf--) { in calc_CCR()
413 scgds = ((iclk / scl_hz) - 20) >> 3; in calc_CCR()
416 dff = abs(scl_hz - m1); in calc_CCR()
426 return -EINVAL; in calc_CCR()
439 pd = dev_get_platdata(&pdev->dev); in sh7760_i2c_probe()
441 dev_err(&pdev->dev, "no platform_data!\n"); in sh7760_i2c_probe()
442 ret = -ENODEV; in sh7760_i2c_probe()
448 dev_err(&pdev->dev, "no mem for private data\n"); in sh7760_i2c_probe()
449 ret = -ENOMEM; in sh7760_i2c_probe()
455 dev_err(&pdev->dev, "no mmio resources\n"); in sh7760_i2c_probe()
456 ret = -ENODEV; in sh7760_i2c_probe()
460 id->ioarea = request_mem_region(res->start, REGSIZE, pdev->name); in sh7760_i2c_probe()
461 if (!id->ioarea) { in sh7760_i2c_probe()
462 dev_err(&pdev->dev, "mmio already reserved\n"); in sh7760_i2c_probe()
463 ret = -EBUSY; in sh7760_i2c_probe()
467 id->iobase = ioremap(res->start, REGSIZE); in sh7760_i2c_probe()
468 if (!id->iobase) { in sh7760_i2c_probe()
469 dev_err(&pdev->dev, "cannot ioremap\n"); in sh7760_i2c_probe()
470 ret = -ENODEV; in sh7760_i2c_probe()
474 id->irq = platform_get_irq(pdev, 0); in sh7760_i2c_probe()
476 id->adap.nr = pdev->id; in sh7760_i2c_probe()
477 id->adap.algo = &sh7760_i2c_algo; in sh7760_i2c_probe()
478 id->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; in sh7760_i2c_probe()
479 id->adap.retries = 3; in sh7760_i2c_probe()
480 id->adap.algo_data = id; in sh7760_i2c_probe()
481 id->adap.dev.parent = &pdev->dev; in sh7760_i2c_probe()
482 snprintf(id->adap.name, sizeof(id->adap.name), in sh7760_i2c_probe()
483 "SH7760 I2C at %08lx", (unsigned long)res->start); in sh7760_i2c_probe()
497 ret = calc_CCR(pd->speed_khz * 1000); in sh7760_i2c_probe()
499 dev_err(&pdev->dev, "invalid SCL clock: %dkHz\n", in sh7760_i2c_probe()
500 pd->speed_khz); in sh7760_i2c_probe()
505 if (request_irq(id->irq, sh7760_i2c_irq, 0, in sh7760_i2c_probe()
507 dev_err(&pdev->dev, "cannot get irq %d\n", id->irq); in sh7760_i2c_probe()
508 ret = -EBUSY; in sh7760_i2c_probe()
512 ret = i2c_add_numbered_adapter(&id->adap); in sh7760_i2c_probe()
518 dev_info(&pdev->dev, "%d kHz mmio %08x irq %d\n", in sh7760_i2c_probe()
519 pd->speed_khz, res->start, id->irq); in sh7760_i2c_probe()
524 free_irq(id->irq, id); in sh7760_i2c_probe()
526 iounmap(id->iobase); in sh7760_i2c_probe()
528 release_resource(id->ioarea); in sh7760_i2c_probe()
529 kfree(id->ioarea); in sh7760_i2c_probe()
540 i2c_del_adapter(&id->adap); in sh7760_i2c_remove()
541 free_irq(id->irq, id); in sh7760_i2c_remove()
542 iounmap(id->iobase); in sh7760_i2c_remove()
543 release_resource(id->ioarea); in sh7760_i2c_remove()
544 kfree(id->ioarea); in sh7760_i2c_remove()
561 MODULE_DESCRIPTION("SH7760 I2C bus driver");