aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authoraddy ke <addy.ke@rock-chips.com>2014-12-11 06:02:40 -0500
committerWolfram Sang <wsa@the-dreams.de>2015-01-13 10:21:04 -0500
commit1330e29105a3ad0a2a88d7a37ddd29d3f70675cf (patch)
tree1bd1859e23c0d2caf6cadf8a5052c644ff693770 /drivers/i2c
parent9fae82e1acda8d4a6881be63cc38521b84007ba9 (diff)
i2c: rk3x: fix bug that cause measured high_ns doesn't meet I2C specification
The number of clock cycles to be written into the CLKDIV register that determines the I2C clk high phase includes the rise time. So to meet the timing requirements defined in the I2C specification which defines the minimal time SCL has to be high, the rise time has to taken into account. The same applies to the low phase with falling time. In my test on RK3288-Pink2 board, which is not an upstream board yet, if external pull-up resistor is 4.7K, rise_ns is about 700ns. So the measured high_ns is about 3900ns, which is less than 4000ns (the minimum high_ns in I2C specification for Standard-mode). To fix this bug min_low_ns should include fall time and min_high_ns should include rise time. This patch merged the patch from chromium project which can get the rise and fall times for signals from the device tree. This allows us to more accurately calculate timings. see: https://chromium-review.googlesource.com/#/c/232774/ Signed-off-by: Addy Ke <addy.ke@rock-chips.com> Reviewed-by: Doug Anderson <dianders@chromium.org> Tested-by: Doug Anderson <dianders@chromium.org> [wsa: fixed a typo in the docs] Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/busses/i2c-rk3x.c72
1 files changed, 49 insertions, 23 deletions
diff --git a/drivers/i2c/busses/i2c-rk3x.c b/drivers/i2c/busses/i2c-rk3x.c
index 92462843db66..36a922445814 100644
--- a/drivers/i2c/busses/i2c-rk3x.c
+++ b/drivers/i2c/busses/i2c-rk3x.c
@@ -102,6 +102,8 @@ struct rk3x_i2c {
102 102
103 /* Settings */ 103 /* Settings */
104 unsigned int scl_frequency; 104 unsigned int scl_frequency;
105 unsigned int rise_ns;
106 unsigned int fall_ns;
105 107
106 /* Synchronization & notification */ 108 /* Synchronization & notification */
107 spinlock_t lock; 109 spinlock_t lock;
@@ -435,6 +437,8 @@ out:
435 * 437 *
436 * @clk_rate: I2C input clock rate 438 * @clk_rate: I2C input clock rate
437 * @scl_rate: Desired SCL rate 439 * @scl_rate: Desired SCL rate
440 * @rise_ns: How many ns it takes for signals to rise.
441 * @fall_ns: How many ns it takes for signals to fall.
438 * @div_low: Divider output for low 442 * @div_low: Divider output for low
439 * @div_high: Divider output for high 443 * @div_high: Divider output for high
440 * 444 *
@@ -443,11 +447,14 @@ out:
443 * too high, we silently use the highest possible rate. 447 * too high, we silently use the highest possible rate.
444 */ 448 */
445static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate, 449static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
450 unsigned long rise_ns, unsigned long fall_ns,
446 unsigned long *div_low, unsigned long *div_high) 451 unsigned long *div_low, unsigned long *div_high)
447{ 452{
448 unsigned long min_low_ns, min_high_ns; 453 unsigned long spec_min_low_ns, spec_min_high_ns;
449 unsigned long max_data_hold_ns; 454 unsigned long spec_max_data_hold_ns;
450 unsigned long data_hold_buffer_ns; 455 unsigned long data_hold_buffer_ns;
456
457 unsigned long min_low_ns, min_high_ns;
451 unsigned long max_low_ns, min_total_ns; 458 unsigned long max_low_ns, min_total_ns;
452 459
453 unsigned long clk_rate_khz, scl_rate_khz; 460 unsigned long clk_rate_khz, scl_rate_khz;
@@ -469,29 +476,33 @@ static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
469 scl_rate = 1000; 476 scl_rate = 1000;
470 477
471 /* 478 /*
472 * min_low_ns: The minimum number of ns we need to hold low 479 * min_low_ns: The minimum number of ns we need to hold low to
473 * to meet i2c spec 480 * meet I2C specification, should include fall time.
474 * min_high_ns: The minimum number of ns we need to hold high 481 * min_high_ns: The minimum number of ns we need to hold high to
475 * to meet i2c spec 482 * meet I2C specification, should include rise time.
476 * max_low_ns: The maximum number of ns we can hold low 483 * max_low_ns: The maximum number of ns we can hold low to meet
477 * to meet i2c spec 484 * I2C specification.
478 * 485 *
479 * Note: max_low_ns should be (max data hold time * 2 - buffer) 486 * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
480 * This is because the i2c host on Rockchip holds the data line 487 * This is because the i2c host on Rockchip holds the data line
481 * for half the low time. 488 * for half the low time.
482 */ 489 */
483 if (scl_rate <= 100000) { 490 if (scl_rate <= 100000) {
484 min_low_ns = 4700; 491 /* Standard-mode */
485 min_high_ns = 4000; 492 spec_min_low_ns = 4700;
486 max_data_hold_ns = 3450; 493 spec_min_high_ns = 4000;
494 spec_max_data_hold_ns = 3450;
487 data_hold_buffer_ns = 50; 495 data_hold_buffer_ns = 50;
488 } else { 496 } else {
489 min_low_ns = 1300; 497 /* Fast-mode */
490 min_high_ns = 600; 498 spec_min_low_ns = 1300;
491 max_data_hold_ns = 900; 499 spec_min_high_ns = 600;
500 spec_max_data_hold_ns = 900;
492 data_hold_buffer_ns = 50; 501 data_hold_buffer_ns = 50;
493 } 502 }
494 max_low_ns = max_data_hold_ns * 2 - data_hold_buffer_ns; 503 min_low_ns = spec_min_low_ns + fall_ns;
504 min_high_ns = spec_min_high_ns + rise_ns;
505 max_low_ns = spec_max_data_hold_ns * 2 - data_hold_buffer_ns;
495 min_total_ns = min_low_ns + min_high_ns; 506 min_total_ns = min_low_ns + min_high_ns;
496 507
497 /* Adjust to avoid overflow */ 508 /* Adjust to avoid overflow */
@@ -510,8 +521,8 @@ static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
510 min_div_for_hold = (min_low_div + min_high_div); 521 min_div_for_hold = (min_low_div + min_high_div);
511 522
512 /* 523 /*
513 * This is the maximum divider so we don't go over the max. 524 * This is the maximum divider so we don't go over the maximum.
514 * We don't round up here (we round down) since this is a max. 525 * We don't round up here (we round down) since this is a maximum.
515 */ 526 */
516 max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000); 527 max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
517 528
@@ -544,7 +555,7 @@ static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
544 ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 555 ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
545 scl_rate_khz * 8 * min_total_ns); 556 scl_rate_khz * 8 * min_total_ns);
546 557
547 /* Don't allow it to go over the max */ 558 /* Don't allow it to go over the maximum */
548 if (ideal_low_div > max_low_div) 559 if (ideal_low_div > max_low_div)
549 ideal_low_div = max_low_div; 560 ideal_low_div = max_low_div;
550 561
@@ -588,8 +599,8 @@ static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
588 u64 t_low_ns, t_high_ns; 599 u64 t_low_ns, t_high_ns;
589 int ret; 600 int ret;
590 601
591 ret = rk3x_i2c_calc_divs(clk_rate, i2c->scl_frequency, &div_low, 602 ret = rk3x_i2c_calc_divs(clk_rate, i2c->scl_frequency, i2c->rise_ns,
592 &div_high); 603 i2c->fall_ns, &div_low, &div_high);
593 604
594 WARN_ONCE(ret != 0, "Could not reach SCL freq %u", i2c->scl_frequency); 605 WARN_ONCE(ret != 0, "Could not reach SCL freq %u", i2c->scl_frequency);
595 606
@@ -633,9 +644,9 @@ static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
633 switch (event) { 644 switch (event) {
634 case PRE_RATE_CHANGE: 645 case PRE_RATE_CHANGE:
635 if (rk3x_i2c_calc_divs(ndata->new_rate, i2c->scl_frequency, 646 if (rk3x_i2c_calc_divs(ndata->new_rate, i2c->scl_frequency,
636 &div_low, &div_high) != 0) { 647 i2c->rise_ns, i2c->fall_ns, &div_low,
648 &div_high) != 0)
637 return NOTIFY_STOP; 649 return NOTIFY_STOP;
638 }
639 650
640 /* scale up */ 651 /* scale up */
641 if (ndata->new_rate > ndata->old_rate) 652 if (ndata->new_rate > ndata->old_rate)
@@ -859,6 +870,21 @@ static int rk3x_i2c_probe(struct platform_device *pdev)
859 i2c->scl_frequency = DEFAULT_SCL_RATE; 870 i2c->scl_frequency = DEFAULT_SCL_RATE;
860 } 871 }
861 872
873 /*
874 * Read rise and fall time from device tree. If not available use
875 * the default maximum timing from the specification.
876 */
877 if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-rising-time-ns",
878 &i2c->rise_ns)) {
879 if (i2c->scl_frequency <= 100000)
880 i2c->rise_ns = 1000;
881 else
882 i2c->rise_ns = 300;
883 }
884 if (of_property_read_u32(pdev->dev.of_node, "i2c-scl-falling-time-ns",
885 &i2c->fall_ns))
886 i2c->fall_ns = 300;
887
862 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name)); 888 strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
863 i2c->adap.owner = THIS_MODULE; 889 i2c->adap.owner = THIS_MODULE;
864 i2c->adap.algo = &rk3x_i2c_algorithm; 890 i2c->adap.algo = &rk3x_i2c_algorithm;