diff options
Diffstat (limited to 'drivers/i2c/busses/i2c-davinci.c')
| -rw-r--r-- | drivers/i2c/busses/i2c-davinci.c | 80 |
1 files changed, 51 insertions, 29 deletions
diff --git a/drivers/i2c/busses/i2c-davinci.c b/drivers/i2c/busses/i2c-davinci.c index 4788a32afb86..3fbb9a035532 100644 --- a/drivers/i2c/busses/i2c-davinci.c +++ b/drivers/i2c/busses/i2c-davinci.c | |||
| @@ -41,8 +41,8 @@ | |||
| 41 | 41 | ||
| 42 | #define DAVINCI_I2C_TIMEOUT (1*HZ) | 42 | #define DAVINCI_I2C_TIMEOUT (1*HZ) |
| 43 | #define DAVINCI_I2C_MAX_TRIES 2 | 43 | #define DAVINCI_I2C_MAX_TRIES 2 |
| 44 | #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \ | 44 | #define DAVINCI_I2C_OWN_ADDRESS 0x08 |
| 45 | DAVINCI_I2C_IMR_SCD | \ | 45 | #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_SCD | \ |
| 46 | DAVINCI_I2C_IMR_ARDY | \ | 46 | DAVINCI_I2C_IMR_ARDY | \ |
| 47 | DAVINCI_I2C_IMR_NACK | \ | 47 | DAVINCI_I2C_IMR_NACK | \ |
| 48 | DAVINCI_I2C_IMR_AL) | 48 | DAVINCI_I2C_IMR_AL) |
| @@ -204,9 +204,30 @@ static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev) | |||
| 204 | psc++; /* better to run under spec than over */ | 204 | psc++; /* better to run under spec than over */ |
| 205 | d = (psc >= 2) ? 5 : 7 - psc; | 205 | d = (psc >= 2) ? 5 : 7 - psc; |
| 206 | 206 | ||
| 207 | clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1); | 207 | clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)); |
| 208 | clkh = clk >> 1; | 208 | /* Avoid driving the bus too fast because of rounding errors above */ |
| 209 | clkl = clk - clkh; | 209 | if (input_clock / (psc + 1) / clk > pdata->bus_freq * 1000) |
| 210 | clk++; | ||
| 211 | /* | ||
| 212 | * According to I2C-BUS Spec 2.1, in FAST-MODE LOW period should be at | ||
| 213 | * least 1.3uS, which is not the case with 50% duty cycle. Driving HIGH | ||
| 214 | * to LOW ratio as 1 to 2 is more safe. | ||
| 215 | */ | ||
| 216 | if (pdata->bus_freq > 100) | ||
| 217 | clkl = (clk << 1) / 3; | ||
| 218 | else | ||
| 219 | clkl = (clk >> 1); | ||
| 220 | /* | ||
| 221 | * It's not always possible to have 1 to 2 ratio when d=7, so fall back | ||
| 222 | * to minimal possible clkh in this case. | ||
| 223 | */ | ||
| 224 | if (clk >= clkl + d) { | ||
| 225 | clkh = clk - clkl - d; | ||
| 226 | clkl -= d; | ||
| 227 | } else { | ||
| 228 | clkh = 0; | ||
| 229 | clkl = clk - (d << 1); | ||
| 230 | } | ||
| 210 | 231 | ||
| 211 | davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); | 232 | davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc); |
| 212 | davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); | 233 | davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh); |
| @@ -233,7 +254,7 @@ static int i2c_davinci_init(struct davinci_i2c_dev *dev) | |||
| 233 | /* Respond at reserved "SMBus Host" slave address" (and zero); | 254 | /* Respond at reserved "SMBus Host" slave address" (and zero); |
| 234 | * we seem to have no option to not respond... | 255 | * we seem to have no option to not respond... |
| 235 | */ | 256 | */ |
| 236 | davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08); | 257 | davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, DAVINCI_I2C_OWN_ADDRESS); |
| 237 | 258 | ||
| 238 | dev_dbg(dev->dev, "PSC = %d\n", | 259 | dev_dbg(dev->dev, "PSC = %d\n", |
| 239 | davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); | 260 | davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG)); |
| @@ -350,29 +371,25 @@ static struct i2c_bus_recovery_info davinci_i2c_scl_recovery_info = { | |||
| 350 | /* | 371 | /* |
| 351 | * Waiting for bus not busy | 372 | * Waiting for bus not busy |
| 352 | */ | 373 | */ |
| 353 | static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev, | 374 | static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev) |
| 354 | char allow_sleep) | ||
| 355 | { | 375 | { |
| 356 | unsigned long timeout; | 376 | unsigned long timeout = jiffies + dev->adapter.timeout; |
| 357 | static u16 to_cnt; | 377 | |
| 358 | 378 | do { | |
| 359 | timeout = jiffies + dev->adapter.timeout; | 379 | if (!(davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB)) |
| 360 | while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) | 380 | return 0; |
| 361 | & DAVINCI_I2C_STR_BB) { | 381 | schedule_timeout_uninterruptible(1); |
| 362 | if (to_cnt <= DAVINCI_I2C_MAX_TRIES) { | 382 | } while (time_before_eq(jiffies, timeout)); |
| 363 | if (time_after(jiffies, timeout)) { | 383 | |
| 364 | dev_warn(dev->dev, | 384 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 365 | "timeout waiting for bus ready\n"); | 385 | i2c_recover_bus(&dev->adapter); |
| 366 | to_cnt++; | 386 | |
| 367 | return -ETIMEDOUT; | 387 | /* |
| 368 | } else { | 388 | * if bus is still "busy" here, it's most probably a HW problem like |
| 369 | to_cnt = 0; | 389 | * short-circuit |
| 370 | i2c_recover_bus(&dev->adapter); | 390 | */ |
| 371 | } | 391 | if (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & DAVINCI_I2C_STR_BB) |
| 372 | } | 392 | return -EIO; |
| 373 | if (allow_sleep) | ||
| 374 | schedule_timeout(1); | ||
| 375 | } | ||
| 376 | 393 | ||
| 377 | return 0; | 394 | return 0; |
| 378 | } | 395 | } |
| @@ -390,6 +407,11 @@ i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop) | |||
| 390 | u16 w; | 407 | u16 w; |
| 391 | unsigned long time_left; | 408 | unsigned long time_left; |
| 392 | 409 | ||
| 410 | if (msg->addr == DAVINCI_I2C_OWN_ADDRESS) { | ||
| 411 | dev_warn(dev->dev, "transfer to own address aborted\n"); | ||
| 412 | return -EADDRNOTAVAIL; | ||
| 413 | } | ||
| 414 | |||
| 393 | /* Introduce a delay, required for some boards (e.g Davinci EVM) */ | 415 | /* Introduce a delay, required for some boards (e.g Davinci EVM) */ |
| 394 | if (pdata->bus_delay) | 416 | if (pdata->bus_delay) |
| 395 | udelay(pdata->bus_delay); | 417 | udelay(pdata->bus_delay); |
| @@ -505,7 +527,7 @@ i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) | |||
| 505 | 527 | ||
| 506 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); | 528 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
| 507 | 529 | ||
| 508 | ret = i2c_davinci_wait_bus_not_busy(dev, 1); | 530 | ret = i2c_davinci_wait_bus_not_busy(dev); |
| 509 | if (ret < 0) { | 531 | if (ret < 0) { |
| 510 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); | 532 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 511 | return ret; | 533 | return ret; |
