i2c-eg20t: add helper function for xfer check
PATCH 1/2 i2c-eg20t: add helper function for xfer check
Currently, there are the same code for xfer complete check processing. So, I add helper function pch_i2c_wait_for_check_xfer.
--- drivers/i2c/busses/i2c-eg20t.c | 226 ++++++++++++---------------------------- 1 files changed, 66 insertions(+), 160 deletions(-)
drivers/i2c/busses/i2c-eg20t.c +66 -160
@@ -366,6 +366,34 @@ static void pch_i2c_stop(struct i2c_algo_pch_data *adap)pch_clrbit (adap->pch_base_address,PCH_I2CCTL ,PCH_START ); } +static int pch_i2c_wait_for_check_xfer (struct i2c_algo_pch_data *adap) +{ +int rtn; + + rtn =pch_i2c_wait_for_xfer_complete (adap); +if (rtn ==0 ) { +if (pch_i2c_getack (adap)) { +pch_dbg (adap,"Receive NACK for slave address" +"setting\n" ); +return -EIO ; + } + }else if (rtn == -EIO ) {/* Arbitration Lost */ +pch_err (adap,"Lost Arbitration\n" ); +pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMAL_BIT ); +pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMIF_BIT ); +pch_i2c_init (adap); +return -EAGAIN ; + }else {/* wait-event timeout */ +pch_err (adap,"%s(L.%d):wait-event timeout\n" , + __func__, __LINE__); +pch_i2c_stop (adap); +pch_i2c_init (adap); +return -ETIME ; + } + +return 0 ; +} +/** * pch_i2c_repstart() - generate repeated start condition in normal mode * @adap: Pointer to struct i2c_algo_pch_data.
@@ -420,30 +448,12 @@ static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,if (first)pch_i2c_start (adap); - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - addr_8_lsb = (addr &I2C_ADDR_MSK ); -iowrite32 (addr_8_lsb, p +PCH_I2CDR ); - }else if (rtn == -EIO ) {/* Arbitration Lost */ -pch_err (adap,"Lost Arbitration\n" ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMAL_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMIF_BIT ); -pch_i2c_init (adap); -return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn; + + addr_8_lsb = (addr &I2C_ADDR_MSK ); +iowrite32 (addr_8_lsb, p +PCH_I2CDR ); }else {/* set 7 bit slave address and R/W bit as 0 */ iowrite32 (addr <<1 , p +PCH_I2CDR );
@@ -451,50 +461,21 @@ static s32 pch_i2c_writebytes(struct i2c_adapter *i2c_adap,pch_i2c_start (adap); } - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - }else if (rtn == -EIO ) {/* Arbitration Lost */ -pch_err (adap,"Lost Arbitration\n" ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMAL_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMIF_BIT ); -pch_i2c_init (adap); -return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn;for (wrcount =0 ; wrcount < length; ++wrcount) {/* write buffer value to I2C data register */ iowrite32 (buf[wrcount], p +PCH_I2CDR );pch_dbg (adap,"writing %x to Data register\n" , buf[wrcount]); - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMCF_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMIF_BIT ); - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn; + +pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMCF_BIT ); +pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMIF_BIT ); }/* check if this is the last message */
@@ -582,56 +563,21 @@ static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,if (first)pch_i2c_start (adap); - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - addr_8_lsb = (addr &I2C_ADDR_MSK ); -iowrite32 (addr_8_lsb, p +PCH_I2CDR ); - }else if (rtn == -EIO ) {/* Arbitration Lost */ -pch_err (adap,"Lost Arbitration\n" ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMAL_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMIF_BIT ); -pch_i2c_init (adap); -return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn; + + addr_8_lsb = (addr &I2C_ADDR_MSK ); +iowrite32 (addr_8_lsb, p +PCH_I2CDR ); +pch_i2c_restart (adap); - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - addr_2_msb |=I2C_RD ; -iowrite32 (addr_2_msb |TEN_BIT_ADDR_MASK , - p +PCH_I2CDR ); - }else if (rtn == -EIO ) {/* Arbitration Lost */ -pch_err (adap,"Lost Arbitration\n" ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMAL_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR , -I2CMIF_BIT ); -pch_i2c_init (adap); -return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn; + + addr_2_msb |=I2C_RD ; +iowrite32 (addr_2_msb |TEN_BIT_ADDR_MASK , p +PCH_I2CDR ); }else {/* 7 address bits + R/W bit */ addr = (((addr) <<1 ) | (I2C_RD ));
@@ -642,26 +588,9 @@ static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,if (first)pch_i2c_start (adap); - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - }else if (rtn == -EIO ) {/* Arbitration Lost */ -pch_err (adap,"Lost Arbitration\n" ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMAL_BIT ); -pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMIF_BIT ); -pch_i2c_init (adap); -return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn;if (length ==0 ) {pch_i2c_stop (adap);
@@ -680,21 +609,9 @@ static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,if (loop !=1 ) read_index++; - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave" -"address setting\n" ); -return -EIO ; - } - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } - + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn; }/* end for */ pch_i2c_sendnack (adap);
@@ -704,20 +621,9 @@ static s32 pch_i2c_readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,if (length !=1 ) read_index++; - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave" -"address setting\n" ); -return -EIO ; - } - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; - } + rtn =pch_i2c_wait_for_check_xfer (adap); +if (rtn) +return rtn;if (last)pch_i2c_stop (adap);
Signature · 1 line
1.7.4.4
PATCH 2/2 i2c-eg20t: Merge 2 functions
By previous patch(1/2), pch_i2c_wait_for_xfer_complete can be merged to pch_i2c_wait_for_check_xfer.
--- drivers/i2c/busses/i2c-eg20t.c | 65 +++++++++++++-------------------------- 1 files changed, 22 insertions(+), 43 deletions(-)
drivers/i2c/busses/i2c-eg20t.c +22 -43
@@ -310,33 +310,6 @@ static void pch_i2c_start(struct i2c_algo_pch_data *adap) }/** -* pch_i2c_wait_for_xfer_complete() - initiates a wait for the tx complete event -* @adap: Pointer to struct i2c_algo_pch_data. -*/ -static s32 pch_i2c_wait_for_xfer_complete (struct i2c_algo_pch_data *adap) -{ -long ret; - ret =wait_event_timeout (pch_event, - (adap->pch_event_flag !=0 ),msecs_to_jiffies (1000 )); - -if (ret ==0 ) { -pch_err (adap,"timeout: %x\n" , adap->pch_event_flag); - adap->pch_event_flag =0 ; -return -ETIMEDOUT ; - } - -if (adap->pch_event_flag &I2C_ERROR_MASK ) { -pch_err (adap,"error bits set: %x\n" , adap->pch_event_flag); - adap->pch_event_flag =0 ; -return -EIO ; - } - - adap->pch_event_flag =0 ; - -return 0 ; -} - -/** * pch_i2c_getack() - to confirm ACK/NACK * @adap: Pointer to struct i2c_algo_pch_data. */
@@ -368,27 +341,33 @@ static void pch_i2c_stop(struct i2c_algo_pch_data *adap)static int pch_i2c_wait_for_check_xfer (struct i2c_algo_pch_data *adap) { -int rtn; - - rtn =pch_i2c_wait_for_xfer_complete (adap); -if (rtn ==0 ) { -if (pch_i2c_getack (adap)) { -pch_dbg (adap,"Receive NACK for slave address" -"setting\n" ); -return -EIO ; - } - }else if (rtn == -EIO ) {/* Arbitration Lost */ +long ret; + + ret =wait_event_timeout (pch_event, + (adap->pch_event_flag !=0 ),msecs_to_jiffies (1000 )); +if (!ret) { +pch_err (adap,"%s:wait-event timeout\n" , __func__); + adap->pch_event_flag =0 ; +pch_i2c_stop (adap); +pch_i2c_init (adap); +return -ETIMEDOUT ; + } + +if (adap->pch_event_flag &I2C_ERROR_MASK ) {pch_err (adap,"Lost Arbitration\n" ); + adap->pch_event_flag =0 ;pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMAL_BIT );pch_clrbit (adap->pch_base_address,PCH_I2CSR ,I2CMIF_BIT );pch_i2c_init (adap);return -EAGAIN ; - }else {/* wait-event timeout */ -pch_err (adap,"%s(L.%d):wait-event timeout\n" , - __func__, __LINE__); -pch_i2c_stop (adap); -pch_i2c_init (adap); -return -ETIME ; + } + + adap->pch_event_flag =0 ; + +if (pch_i2c_getack (adap)) { +pch_dbg (adap,"Receive NACK for slave address" +"setting\n" ); +return -EIO ; }return 0 ;
Signature · 1 line
1.7.4.4
Quoted 5 lines · Tomoya MORINAGA
Currently, there are the same code for xfer complete check processing. So, I add helper function
pch_i2c_wait_for_check_xfer.Reported-by Wolfram Sang <[email protected]>Signed-off-by Tomoya MORINAGA <[email protected]>
Both applied to next, thanks for doing this! ~120 lines saved, that's about 10%. And also way better maintainable, yay.
My checkers found some other issues, for which I will send patches in a minute. Please have a look and test.
Signature · 2 lines
Pengutronix e.K. | Wolfram Sang | Industrial Linux Solutions | http://www.pengutronix.de/ |
Quoted 9 lines · Wolfram Sang
Quoted 5 lines · Tomoya MORINAGA
Currently, there are the same code for xfer complete check processing. So, I add helper function
pch_i2c_wait_for_check_xfer.Reported-by Wolfram Sang <[email protected]>Signed-off-by Tomoya MORINAGA <[email protected]>Both applied to next, thanks for doing this! ~120 lines saved, that's about 10%. And also way better maintainable, yay.
Thank you for your applied.
Quoted 3 lines
My checkers found some other issues, for which I will send patches in a minute. Please have a look and test.
OK.
I'll do until this week end and report to you.
thanks,
Signature · 2 lines
ROHM Co., Ltd. tomoya
Quoted 7 lines · Tomoya MORINAGA
Quoted 3 lines
My checkers found some other issues, for which I will send patches in a minute. Please have a look and test.
OK.
I'll do until this week end and report to you.
These 4 patches posted by you don't affect this driver's logical behavior. So, I only confirmed the driver is compiled without any error/warning.
thanks.
Signature · 2 lines
ROHM Co., Ltd. tomoya