@@ -168,10 +168,11 @@ struct i2c_nmk_client {
168
168
* @clk_freq: clock frequency for the operation mode
169
169
* @tft: Tx FIFO Threshold in bytes
170
170
* @rft: Rx FIFO Threshold in bytes
171
- * @timeout : Slave response timeout (ms)
171
+ * @timeout_usecs : Slave response timeout
172
172
* @sm: speed mode
173
173
* @stop: stop condition.
174
- * @xfer_complete: acknowledge completion for a I2C message.
174
+ * @xfer_wq: xfer done wait queue.
175
+ * @xfer_done: xfer done boolean.
175
176
* @result: controller propogated result.
176
177
*/
177
178
struct nmk_i2c_dev {
@@ -185,10 +186,11 @@ struct nmk_i2c_dev {
185
186
u32 clk_freq ;
186
187
unsigned char tft ;
187
188
unsigned char rft ;
188
- int timeout ;
189
+ u32 timeout_usecs ;
189
190
enum i2c_freq_mode sm ;
190
191
int stop ;
191
- struct completion xfer_complete ;
192
+ struct wait_queue_head xfer_wq ;
193
+ bool xfer_done ;
192
194
int result ;
193
195
};
194
196
@@ -443,6 +445,22 @@ static void setup_i2c_controller(struct nmk_i2c_dev *priv)
443
445
writel (priv -> rft , priv -> virtbase + I2C_RFTR );
444
446
}
445
447
448
+ static bool nmk_i2c_wait_xfer_done (struct nmk_i2c_dev * priv )
449
+ {
450
+ if (priv -> timeout_usecs < jiffies_to_usecs (1 )) {
451
+ unsigned long timeout_usecs = priv -> timeout_usecs ;
452
+ ktime_t timeout = ktime_set (0 , timeout_usecs * NSEC_PER_USEC );
453
+
454
+ wait_event_hrtimeout (priv -> xfer_wq , priv -> xfer_done , timeout );
455
+ } else {
456
+ unsigned long timeout = usecs_to_jiffies (priv -> timeout_usecs );
457
+
458
+ wait_event_timeout (priv -> xfer_wq , priv -> xfer_done , timeout );
459
+ }
460
+
461
+ return priv -> xfer_done ;
462
+ }
463
+
446
464
/**
447
465
* read_i2c() - Read from I2C client device
448
466
* @priv: private data of I2C Driver
@@ -454,9 +472,9 @@ static void setup_i2c_controller(struct nmk_i2c_dev *priv)
454
472
*/
455
473
static int read_i2c (struct nmk_i2c_dev * priv , u16 flags )
456
474
{
457
- int status = 0 ;
458
475
u32 mcr , irq_mask ;
459
- unsigned long timeout ;
476
+ int status = 0 ;
477
+ bool xfer_done ;
460
478
461
479
mcr = load_i2c_mcr_reg (priv , flags );
462
480
writel (mcr , priv -> virtbase + I2C_MCR );
@@ -468,7 +486,8 @@ static int read_i2c(struct nmk_i2c_dev *priv, u16 flags)
468
486
/* enable the controller */
469
487
i2c_set_bit (priv -> virtbase + I2C_CR , I2C_CR_PE );
470
488
471
- init_completion (& priv -> xfer_complete );
489
+ init_waitqueue_head (& priv -> xfer_wq );
490
+ priv -> xfer_done = false;
472
491
473
492
/* enable interrupts by setting the mask */
474
493
irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
@@ -484,10 +503,9 @@ static int read_i2c(struct nmk_i2c_dev *priv, u16 flags)
484
503
writel (readl (priv -> virtbase + I2C_IMSCR ) | irq_mask ,
485
504
priv -> virtbase + I2C_IMSCR );
486
505
487
- timeout = wait_for_completion_timeout (
488
- & priv -> xfer_complete , priv -> adap .timeout );
506
+ xfer_done = nmk_i2c_wait_xfer_done (priv );
489
507
490
- if (timeout == 0 ) {
508
+ if (! xfer_done ) {
491
509
/* Controller timed out */
492
510
dev_err (& priv -> adev -> dev , "read from slave 0x%x timed out\n" ,
493
511
priv -> cli .slave_adr );
@@ -522,9 +540,9 @@ static void fill_tx_fifo(struct nmk_i2c_dev *priv, int no_bytes)
522
540
*/
523
541
static int write_i2c (struct nmk_i2c_dev * priv , u16 flags )
524
542
{
525
- u32 status = 0 ;
526
543
u32 mcr , irq_mask ;
527
- unsigned long timeout ;
544
+ u32 status = 0 ;
545
+ bool xfer_done ;
528
546
529
547
mcr = load_i2c_mcr_reg (priv , flags );
530
548
@@ -537,7 +555,8 @@ static int write_i2c(struct nmk_i2c_dev *priv, u16 flags)
537
555
/* enable the controller */
538
556
i2c_set_bit (priv -> virtbase + I2C_CR , I2C_CR_PE );
539
557
540
- init_completion (& priv -> xfer_complete );
558
+ init_waitqueue_head (& priv -> xfer_wq );
559
+ priv -> xfer_done = false;
541
560
542
561
/* enable interrupts by settings the masks */
543
562
irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR );
@@ -563,10 +582,9 @@ static int write_i2c(struct nmk_i2c_dev *priv, u16 flags)
563
582
writel (readl (priv -> virtbase + I2C_IMSCR ) | irq_mask ,
564
583
priv -> virtbase + I2C_IMSCR );
565
584
566
- timeout = wait_for_completion_timeout (
567
- & priv -> xfer_complete , priv -> adap .timeout );
585
+ xfer_done = nmk_i2c_wait_xfer_done (priv );
568
586
569
- if (timeout == 0 ) {
587
+ if (! xfer_done ) {
570
588
/* Controller timed out */
571
589
dev_err (& priv -> adev -> dev , "write to slave 0x%x timed out\n" ,
572
590
priv -> cli .slave_adr );
@@ -816,7 +834,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
816
834
priv -> cli .count );
817
835
init_hw (priv );
818
836
}
819
- complete (& priv -> xfer_complete );
837
+ priv -> xfer_done = true;
838
+ wake_up (& priv -> xfer_wq );
839
+
820
840
821
841
break ;
822
842
@@ -826,7 +846,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
826
846
init_hw (priv );
827
847
828
848
i2c_set_bit (priv -> virtbase + I2C_ICR , I2C_IT_MAL );
829
- complete (& priv -> xfer_complete );
849
+ priv -> xfer_done = true;
850
+ wake_up (& priv -> xfer_wq );
851
+
830
852
831
853
break ;
832
854
@@ -845,7 +867,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
845
867
init_hw (priv );
846
868
847
869
i2c_set_bit (priv -> virtbase + I2C_ICR , I2C_IT_BERR );
848
- complete (& priv -> xfer_complete );
870
+ priv -> xfer_done = true;
871
+ wake_up (& priv -> xfer_wq );
872
+
849
873
}
850
874
break ;
851
875
@@ -859,7 +883,9 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
859
883
init_hw (priv );
860
884
861
885
dev_err (dev , "Tx Fifo Over run\n" );
862
- complete (& priv -> xfer_complete );
886
+ priv -> xfer_done = true;
887
+ wake_up (& priv -> xfer_wq );
888
+
863
889
864
890
break ;
865
891
@@ -960,7 +986,7 @@ static void nmk_i2c_of_probe(struct device_node *np,
960
986
priv -> sm = I2C_FREQ_MODE_FAST ;
961
987
priv -> tft = 1 ; /* Tx FIFO threshold */
962
988
priv -> rft = 8 ; /* Rx FIFO threshold */
963
- priv -> timeout = 200 ; /* Slave response timeout(ms) */
989
+ priv -> timeout_usecs = 200 * USEC_PER_MSEC ; /* Slave response timeout */
964
990
}
965
991
966
992
static int nmk_i2c_probe (struct amba_device * adev , const struct amba_id * id )
@@ -1020,7 +1046,7 @@ static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
1020
1046
adap -> owner = THIS_MODULE ;
1021
1047
adap -> class = I2C_CLASS_DEPRECATED ;
1022
1048
adap -> algo = & nmk_i2c_algo ;
1023
- adap -> timeout = msecs_to_jiffies (priv -> timeout );
1049
+ adap -> timeout = usecs_to_jiffies (priv -> timeout_usecs );
1024
1050
snprintf (adap -> name , sizeof (adap -> name ),
1025
1051
"Nomadik I2C at %pR" , & adev -> res );
1026
1052
0 commit comments