123
/* controller response timeout in ms */
124
#define I2C_TIMEOUT_MS 2000
127
122
* struct i2c_nmk_client - client specific data
128
123
* @slave_adr: 7-bit slave address
129
* @count: no. bytes to be transfered
124
* @count: no. bytes to be transferred
130
125
* @buffer: client data buffer
131
* @xfer_bytes: bytes transfered till now
126
* @xfer_bytes: bytes transferred till now
132
127
* @operation: current I2C operation
134
129
struct i2c_nmk_client {
209
207
writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
211
209
for (i = 0; i < LOOP_ATTEMPTS; i++) {
212
timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT_MS);
210
timeout = jiffies + dev->adap.timeout;
214
212
while (!time_after(jiffies, timeout)) {
215
213
if ((readl(dev->virtbase + I2C_CR) &
330
324
* slsu defines the data setup time after SCL clock
331
325
* stretching in terms of i2c clk cycles. The
332
326
* needed setup time for the three modes are 250ns,
333
* 100ns, 10ns repectively thus leading to the values
327
* 100ns, 10ns respectively thus leading to the values
334
328
* of 14, 6, 2 for a 48 MHz i2c clk.
336
330
writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
365
359
* set the speed mode. Currently we support
366
360
* only standard and fast mode of operation
367
* TODO - support for fast mode plus (upto 1Mb/s)
361
* TODO - support for fast mode plus (up to 1Mb/s)
368
362
* and high speed (up to 3.4 Mb/s)
370
364
if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
436
430
if (timeout == 0) {
437
/* controller has timedout, re-init the h/w */
438
dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n");
431
/* Controller timed out */
432
dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
440
434
status = -ETIMEDOUT;
439
static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
443
for (count = (no_bytes - 2);
445
(dev->cli.count != 0);
447
/* write to the Tx FIFO */
448
writeb(*dev->cli.buffer,
449
dev->virtbase + I2C_TFR);
452
dev->cli.xfer_bytes++;
446
458
* write_i2c() - Write data to I2C client.
447
459
* @dev: private data of I2C Driver
469
481
init_completion(&dev->xfer_complete);
471
483
/* enable interrupts by settings the masks */
472
irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR |
473
I2C_IT_MAL | I2C_IT_BERR);
484
irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
486
/* Fill the TX FIFO with transmit data */
487
fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
489
if (dev->cli.count != 0)
490
irq_mask |= I2C_IT_TXFNE;
476
493
* check if we want to transfer a single or multiple bytes, if so
500
517
if (timeout == 0) {
501
/* controller has timedout, re-init the h/w */
502
dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n");
518
/* Controller timed out */
519
dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
528
* nmk_i2c_xfer_one() - transmit a single I2C message
529
* @dev: device with a message encoded into it
530
* @flags: message flags
532
static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
536
if (flags & I2C_M_RD) {
538
dev->cli.operation = I2C_READ;
539
status = read_i2c(dev);
541
/* write operation */
542
dev->cli.operation = I2C_WRITE;
543
status = write_i2c(dev);
546
if (status || (dev->result)) {
550
i2c_sr = readl(dev->virtbase + I2C_SR);
552
* Check if the controller I2C operation status
553
* is set to ABORT(11b).
555
if (((i2c_sr >> 2) & 0x3) == 0x3) {
556
/* get the abort cause */
557
cause = (i2c_sr >> 4) & 0x7;
558
dev_err(&dev->pdev->dev, "%s\n", cause
559
>= ARRAY_SIZE(abort_causes) ?
561
abort_causes[cause]);
503
564
(void) init_hw(dev);
566
status = status ? status : dev->result;
563
624
struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
630
regulator_enable(dev->regulator);
631
pm_runtime_get_sync(&dev->pdev->dev);
633
clk_enable(dev->clk);
565
635
status = init_hw(dev);
569
clk_enable(dev->clk);
571
/* setup the i2c controller */
572
setup_i2c_controller(dev);
574
for (i = 0; i < num_msgs; i++) {
575
if (unlikely(msgs[i].flags & I2C_M_TEN)) {
576
dev_err(&dev->pdev->dev, "10 bit addressing"
580
dev->cli.slave_adr = msgs[i].addr;
581
dev->cli.buffer = msgs[i].buf;
582
dev->cli.count = msgs[i].len;
583
dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
586
if (msgs[i].flags & I2C_M_RD) {
587
/* it is a read operation */
588
dev->cli.operation = I2C_READ;
589
status = read_i2c(dev);
591
/* write operation */
592
dev->cli.operation = I2C_WRITE;
593
status = write_i2c(dev);
595
if (status || (dev->result)) {
596
/* get the abort cause */
597
cause = (readl(dev->virtbase + I2C_SR) >> 4) & 0x7;
598
dev_err(&dev->pdev->dev, "error during I2C"
599
"message xfer: %d\n", cause);
600
dev_err(&dev->pdev->dev, "%s\n",
601
cause >= ARRAY_SIZE(abort_causes)
602
? "unknown reason" : abort_causes[cause]);
603
clk_disable(dev->clk);
639
/* Attempt three times to send the message queue */
640
for (j = 0; j < 3; j++) {
641
/* setup the i2c controller */
642
setup_i2c_controller(dev);
644
for (i = 0; i < num_msgs; i++) {
645
if (unlikely(msgs[i].flags & I2C_M_TEN)) {
646
dev_err(&dev->pdev->dev, "10 bit addressing"
652
dev->cli.slave_adr = msgs[i].addr;
653
dev->cli.buffer = msgs[i].buf;
654
dev->cli.count = msgs[i].len;
655
dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
658
status = nmk_i2c_xfer_one(dev, msgs[i].flags);
608
667
clk_disable(dev->clk);
668
pm_runtime_put_sync(&dev->pdev->dev);
670
regulator_disable(dev->regulator);
610
674
/* return the no. messages processed */
667
731
disable_interrupts(dev, I2C_IT_TXFNE);
669
for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2);
671
(dev->cli.count != 0);
673
/* write to the Tx FIFO */
674
writeb(*dev->cli.buffer,
675
dev->virtbase + I2C_TFR);
678
dev->cli.xfer_bytes++;
733
fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
681
735
* if done, close the transfer by disabling the
682
736
* corresponding TXFNE interrupt
732
i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
733
i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
735
disable_interrupts(dev,
736
(I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
737
| I2C_IT_TXFOVR | I2C_IT_RXFNF
738
| I2C_IT_RXFF | I2C_IT_RXFE));
786
disable_all_interrupts(dev);
787
clear_all_interrupts(dev);
740
789
if (dev->cli.count) {
742
791
dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
743
792
"xfered\n", dev->cli.count);
744
793
(void) init_hw(dev);
805
854
return IRQ_HANDLED;
859
static int nmk_i2c_suspend(struct device *dev)
861
struct platform_device *pdev = to_platform_device(dev);
862
struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
870
static int nmk_i2c_resume(struct device *dev)
875
#define nmk_i2c_suspend NULL
876
#define nmk_i2c_resume NULL
880
* We use noirq so that we suspend late and resume before the wakeup interrupt
881
* to ensure that we do the !pm_runtime_suspended() check in resume before
882
* there has been a regular pm runtime resume (via pm_runtime_get_sync()).
884
static const struct dev_pm_ops nmk_i2c_pm = {
885
.suspend_noirq = nmk_i2c_suspend,
886
.resume_noirq = nmk_i2c_resume,
808
889
static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
810
891
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
944
dev->regulator = regulator_get(&pdev->dev, "v-i2c");
945
if (IS_ERR(dev->regulator)) {
946
dev_warn(&pdev->dev, "could not get i2c regulator\n");
947
dev->regulator = NULL;
950
pm_suspend_ignore_children(&pdev->dev, true);
951
pm_runtime_enable(&pdev->dev);
863
953
dev->clk = clk_get(&pdev->dev, NULL);
864
954
if (IS_ERR(dev->clk)) {
865
955
dev_err(&pdev->dev, "could not get i2c clock\n");
872
962
adap->owner = THIS_MODULE;
873
963
adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
874
964
adap->algo = &nmk_i2c_algo;
965
adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
966
msecs_to_jiffies(20000);
875
967
snprintf(adap->name, sizeof(adap->name),
876
968
"Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
888
980
i2c_set_adapdata(adap, dev);
892
dev_err(&pdev->dev, "error in initializing i2c hardware\n");
896
982
dev_info(&pdev->dev, "initialize %s on virtual "
897
983
"base %p\n", adap->name, dev->virtbase);