Lines Matching defs:i2c_imx
131 static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
136 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
139 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
145 dev_dbg(&i2c_imx->adapter.dev,
150 dev_dbg(&i2c_imx->adapter.dev,
160 static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
162 wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
164 if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
165 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
168 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
169 i2c_imx->i2csr = 0;
173 static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
175 if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) {
176 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
180 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
184 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
189 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
191 clk_enable(i2c_imx->clk);
192 writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);
194 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
195 writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
201 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
203 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
204 result = i2c_imx_bus_busy(i2c_imx, 1);
207 i2c_imx->stopped = 0;
210 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
214 static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
218 if (!i2c_imx->stopped) {
220 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
221 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
223 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
230 udelay(i2c_imx->disable_delay);
233 if (!i2c_imx->stopped) {
234 i2c_imx_bus_busy(i2c_imx, 0);
235 i2c_imx->stopped = 1;
239 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
240 clk_disable(i2c_imx->clk);
243 static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
251 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
261 i2c_imx->ifdr = i2c_clk_div[i][1];
269 i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
283 struct imx_i2c_struct *i2c_imx = dev_id;
286 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
289 i2c_imx->i2csr = temp;
291 writeb(temp, i2c_imx->base + IMX_I2C_I2SR);
292 wake_up(&i2c_imx->queue);
299 static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
303 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
307 writeb(msgs->addr << 1, i2c_imx->base + IMX_I2C_I2DR);
308 result = i2c_imx_trx_complete(i2c_imx);
311 result = i2c_imx_acked(i2c_imx);
314 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
318 dev_dbg(&i2c_imx->adapter.dev,
321 writeb(msgs->buf[i], i2c_imx->base + IMX_I2C_I2DR);
322 result = i2c_imx_trx_complete(i2c_imx);
325 result = i2c_imx_acked(i2c_imx);
332 static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
337 dev_dbg(&i2c_imx->adapter.dev,
342 writeb((msgs->addr << 1) | 0x01, i2c_imx->base + IMX_I2C_I2DR);
343 result = i2c_imx_trx_complete(i2c_imx);
346 result = i2c_imx_acked(i2c_imx);
350 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
353 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
357 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
358 readb(i2c_imx->base + IMX_I2C_I2DR); /* dummy read */
360 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
364 result = i2c_imx_trx_complete(i2c_imx);
370 dev_dbg(&i2c_imx->adapter.dev,
372 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
374 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
375 i2c_imx_bus_busy(i2c_imx, 0);
376 i2c_imx->stopped = 1;
378 dev_dbg(&i2c_imx->adapter.dev,
380 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
382 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
384 msgs->buf[i] = readb(i2c_imx->base + IMX_I2C_I2DR);
385 dev_dbg(&i2c_imx->adapter.dev,
397 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
399 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
402 result = i2c_imx_start(i2c_imx);
409 dev_dbg(&i2c_imx->adapter.dev,
411 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
413 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
414 result = i2c_imx_bus_busy(i2c_imx, 1);
418 dev_dbg(&i2c_imx->adapter.dev,
422 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
423 dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
428 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
429 dev_dbg(&i2c_imx->adapter.dev,
438 result = i2c_imx_read(i2c_imx, &msgs[i]);
440 result = i2c_imx_write(i2c_imx, &msgs[i]);
447 i2c_imx_stop(i2c_imx);
449 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
467 struct imx_i2c_struct *i2c_imx;
510 i2c_imx = kzalloc(sizeof(struct imx_i2c_struct), GFP_KERNEL);
511 if (!i2c_imx) {
517 /* Setup i2c_imx driver structure */
518 strcpy(i2c_imx->adapter.name, pdev->name);
519 i2c_imx->adapter.owner = THIS_MODULE;
520 i2c_imx->adapter.algo = &i2c_imx_algo;
521 i2c_imx->adapter.dev.parent = &pdev->dev;
522 i2c_imx->adapter.nr = pdev->id;
523 i2c_imx->irq = irq;
524 i2c_imx->base = base;
525 i2c_imx->res = res;
528 i2c_imx->clk = clk_get(&pdev->dev, "i2c_clk");
529 if (IS_ERR(i2c_imx->clk)) {
530 ret = PTR_ERR(i2c_imx->clk);
536 ret = request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);
538 dev_err(&pdev->dev, "can't claim irq %d\n", i2c_imx->irq);
543 init_waitqueue_head(&i2c_imx->queue);
546 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
550 i2c_imx_set_clk(i2c_imx, pdata->bitrate);
552 i2c_imx_set_clk(i2c_imx, IMX_I2C_BIT_RATE);
555 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
556 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
559 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
566 platform_set_drvdata(pdev, i2c_imx);
568 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", i2c_imx->irq);
569 dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
570 i2c_imx->res->start, i2c_imx->res->end);
571 dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x \n",
572 res_size, i2c_imx->res->start);
573 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
574 i2c_imx->adapter.name);
575 dev_dbg(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
580 free_irq(i2c_imx->irq, i2c_imx);
582 clk_put(i2c_imx->clk);
584 kfree(i2c_imx);
597 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
601 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
602 i2c_del_adapter(&i2c_imx->adapter);
606 free_irq(i2c_imx->irq, i2c_imx);
609 writeb(0, i2c_imx->base + IMX_I2C_IADR);
610 writeb(0, i2c_imx->base + IMX_I2C_IFDR);
611 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
612 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
618 clk_put(i2c_imx->clk);
620 iounmap(i2c_imx->base);
621 release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res));
622 kfree(i2c_imx);