Skip to content

Commit c713dd5

Browse files
pelwellpopcornmix
authored andcommitted
i2c: designware: Support non-standard bus speeds
Add support for non-standard bus speeds by treating them as detuned versions of the slowest standard speed not less than the requested speed. Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent fa1a86b commit c713dd5

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

drivers/i2c/busses/i2c-designware-common.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ static void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
362362
{
363363
u32 acpi_speed = i2c_dw_acpi_round_bus_speed(dev->dev);
364364
struct i2c_timings *t = &dev->timings;
365+
u32 wanted_speed;
366+
u32 legal_speed = 0;
367+
int i;
365368

366369
/*
367370
* Find bus speed from the "clock-frequency" device property, ACPI
@@ -373,6 +376,30 @@ static void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
373376
t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
374377
else
375378
t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
379+
380+
wanted_speed = t->bus_freq_hz;
381+
382+
/* For unsupported speeds, scale down the lowest speed which is faster. */
383+
for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
384+
/* supported speeds is in decreasing order */
385+
if (wanted_speed == supported_speeds[i]) {
386+
legal_speed = 0;
387+
break;
388+
}
389+
if (wanted_speed > supported_speeds[i])
390+
break;
391+
392+
legal_speed = supported_speeds[i];
393+
}
394+
395+
if (legal_speed) {
396+
/*
397+
* Pretend this was the requested speed, but preserve the preferred
398+
* speed so the clock counts can be scaled.
399+
*/
400+
t->bus_freq_hz = legal_speed;
401+
dev->wanted_bus_speed = wanted_speed;
402+
}
376403
}
377404

378405
int i2c_dw_fw_parse_and_configure(struct dw_i2c_dev *dev)

drivers/i2c/busses/i2c-designware-core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ struct dw_i2c_dev {
297297
u16 fp_lcnt;
298298
u16 hs_hcnt;
299299
u16 hs_lcnt;
300+
u32 wanted_bus_speed;
300301
int (*acquire_lock)(void);
301302
void (*release_lock)(void);
302303
int semaphore_idx;

drivers/i2c/busses/i2c-designware-master.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,32 @@ static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev)
4141
regmap_write(dev->map, DW_IC_CON, dev->master_cfg);
4242
}
4343

44-
static u16 clock_calc(struct dw_i2c_dev *dev, bool want_high)
44+
static u32 linear_interpolate(u32 x, u32 x1, u32 x2, u32 y1, u32 y2)
4545
{
46-
struct i2c_timings *t = &dev->timings;
47-
u32 wanted_speed = t->bus_freq_hz;
48-
u32 clk_khz = i2c_dw_clk_rate(dev);
49-
u32 extra_ns = want_high ? t->scl_fall_ns : t->scl_rise_ns;
50-
u32 extra_cycles = (u32)((u64)clk_khz * extra_ns / 1000000);
51-
u32 period = ((u64)clk_khz * 1000 + wanted_speed - 1) / wanted_speed;
52-
u32 cycles = (period + want_high)/2 - extra_cycles;
46+
return ((x - x1) * y2 + (x2 - x) * y1) / (x2 - x1);
47+
}
5348

54-
if (cycles > 0xffff)
55-
cycles = 0xffff;
49+
static u16 u16_clamp(u32 v)
50+
{
51+
return (u16)min(v, 0xffff);
52+
}
5653

57-
return (u16)cycles;
54+
static void clock_calc(struct dw_i2c_dev *dev, u32 *hcnt, u32 *lcnt)
55+
{
56+
struct i2c_timings *t = &dev->timings;
57+
u32 wanted_khz = (dev->wanted_bus_speed ?: t->bus_freq_hz)/1000;
58+
u32 clk_khz = i2c_dw_clk_rate(dev);
59+
u32 min_high_ns = (wanted_khz <= 100) ? 4000 :
60+
(wanted_khz <= 400) ?
61+
linear_interpolate(wanted_khz, 100, 400, 4000, 600) :
62+
linear_interpolate(wanted_khz, 400, 1000, 600, 260);
63+
u32 high_cycles = (u32)(((u64)clk_khz * min_high_ns + 999999) / 1000000) + 1;
64+
u32 extra_high_cycles = (u32)((u64)clk_khz * t->scl_fall_ns / 1000000);
65+
u32 extra_low_cycles = (u32)((u64)clk_khz * t->scl_rise_ns / 1000000);
66+
u32 period = ((u64)clk_khz + wanted_khz - 1) / wanted_khz;
67+
68+
*hcnt = u16_clamp(high_cycles - extra_high_cycles);
69+
*lcnt = u16_clamp(period - high_cycles - extra_low_cycles);
5870
}
5971

6072
static int i2c_dw_set_timings_master(struct dw_i2c_dev *dev)
@@ -80,8 +92,7 @@ static int i2c_dw_set_timings_master(struct dw_i2c_dev *dev)
8092
sda_falling_time = t->sda_fall_ns ?: 300; /* ns */
8193
scl_falling_time = t->scl_fall_ns ?: 300; /* ns */
8294

83-
hcnt = clock_calc(dev, true);
84-
lcnt = clock_calc(dev, false);
95+
clock_calc(dev, &hcnt, &lcnt);
8596

8697
/* Calculate SCL timing parameters for standard mode if not set */
8798
if (!dev->ss_hcnt || !dev->ss_lcnt) {

0 commit comments

Comments
 (0)