Lines Matching refs:i2c

7  * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
21 #include <linux/i2c.h>
28 #define DRV_NAME "i2c-octeon"
78 * @i2c: The struct octeon_i2c.
84 static void octeon_i2c_write_sw(struct octeon_i2c *i2c,
90 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
92 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
98 * @i2c: The struct octeon_i2c.
105 static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
109 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
111 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
119 * @i2c: The struct octeon_i2c.
122 static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
126 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
127 tmp = __raw_readq(i2c->twsi_base + TWSI_INT);
132 * @i2c: The struct octeon_i2c.
137 static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
139 octeon_i2c_write_int(i2c, 0x40);
144 * @i2c: The struct octeon_i2c.
146 static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
148 octeon_i2c_write_int(i2c, 0);
153 * @i2c: The struct octeon_i2c.
159 static void octeon_i2c_unblock(struct octeon_i2c *i2c)
163 dev_dbg(i2c->dev, "%s\n", __func__);
165 octeon_i2c_write_int(i2c, 0x0);
167 octeon_i2c_write_int(i2c, 0x200);
170 octeon_i2c_write_int(i2c, 0x300);
172 octeon_i2c_write_int(i2c, 0x100);
174 octeon_i2c_write_int(i2c, 0x0);
184 struct octeon_i2c *i2c = dev_id;
186 octeon_i2c_int_disable(i2c);
187 wake_up_interruptible(&i2c->queue);
193 static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
195 return (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_CTL) & TWSI_CTL_IFLG) != 0;
200 * @i2c: The struct octeon_i2c.
204 static int octeon_i2c_wait(struct octeon_i2c *i2c)
208 octeon_i2c_int_enable(i2c);
210 result = wait_event_interruptible_timeout(i2c->queue,
211 octeon_i2c_test_iflg(i2c),
212 i2c->adap.timeout);
214 octeon_i2c_int_disable(i2c);
217 dev_dbg(i2c->dev, "%s: wait interrupted\n", __func__);
220 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
229 * @i2c: The struct octeon_i2c.
233 static int octeon_i2c_start(struct octeon_i2c *i2c)
238 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
241 result = octeon_i2c_wait(i2c);
243 if (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT) == STAT_IDLE) {
249 octeon_i2c_unblock(i2c);
250 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
253 result = octeon_i2c_wait(i2c);
259 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
261 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
270 * @i2c: The struct octeon_i2c.
274 static int octeon_i2c_stop(struct octeon_i2c *i2c)
278 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
281 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
284 dev_err(i2c->dev, "%s: bad status(0x%x)\n", __func__, data);
292 * @i2c: The struct octeon_i2c.
301 static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
307 result = octeon_i2c_start(i2c);
311 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, target << 1);
312 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
314 result = octeon_i2c_wait(i2c);
319 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
321 dev_err(i2c->dev,
327 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, data[i]);
328 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
330 result = octeon_i2c_wait(i2c);
340 * @i2c: The struct octeon_i2c.
349 static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
358 result = octeon_i2c_start(i2c);
362 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target<<1) | 1);
363 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
365 result = octeon_i2c_wait(i2c);
370 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
372 dev_err(i2c->dev,
379 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
382 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
385 result = octeon_i2c_wait(i2c);
389 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
410 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
414 dev_dbg(i2c->dev,
419 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
422 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
425 octeon_i2c_stop(i2c);
450 static int __devinit octeon_i2c_setclock(struct octeon_i2c *i2c)
466 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
468 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
474 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
477 diff = abs(foscl - i2c->twsi_freq);
487 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
488 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
493 static int __devinit octeon_i2c_initlowlevel(struct octeon_i2c *i2c)
499 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
502 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_RST, 0);
506 status = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
510 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
517 struct octeon_i2c *i2c;
526 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
527 if (!i2c) {
532 i2c->dev = &pdev->dev;
538 dev_err(i2c->dev, "found no memory resource\n");
544 dev_err(i2c->dev, "no I2C frequency data\n");
549 i2c->twsi_phys = res_mem->start;
550 i2c->regsize = resource_size(res_mem);
551 i2c->twsi_freq = i2c_data->i2c_freq;
552 i2c->sys_freq = i2c_data->sys_freq;
554 if (!request_mem_region(i2c->twsi_phys, i2c->regsize, res_mem->name)) {
555 dev_err(i2c->dev, "request_mem_region failed\n");
558 i2c->twsi_base = ioremap(i2c->twsi_phys, i2c->regsize);
560 init_waitqueue_head(&i2c->queue);
562 i2c->irq = irq;
564 result = request_irq(i2c->irq, octeon_i2c_isr, 0, DRV_NAME, i2c);
566 dev_err(i2c->dev, "failed to attach interrupt\n");
570 result = octeon_i2c_initlowlevel(i2c);
572 dev_err(i2c->dev, "init low level failed\n");
576 result = octeon_i2c_setclock(i2c);
578 dev_err(i2c->dev, "clock init failed\n");
582 i2c->adap = octeon_i2c_ops;
583 i2c->adap.dev.parent = &pdev->dev;
584 i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0;
585 i2c_set_adapdata(&i2c->adap, i2c);
586 platform_set_drvdata(pdev, i2c);
588 result = i2c_add_numbered_adapter(&i2c->adap);
590 dev_err(i2c->dev, "failed to add adapter\n");
594 dev_info(i2c->dev, "version %s\n", DRV_VERSION);
600 free_irq(i2c->irq, i2c);
602 iounmap(i2c->twsi_base);
603 release_mem_region(i2c->twsi_phys, i2c->regsize);
605 kfree(i2c);
612 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
614 i2c_del_adapter(&i2c->adap);
616 free_irq(i2c->irq, i2c);
617 iounmap(i2c->twsi_base);
618 release_mem_region(i2c->twsi_phys, i2c->regsize);
619 kfree(i2c);