Skip to content

Commit cea76e5

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 <[email protected]>
1 parent 322c9b8 commit cea76e5

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
318318
{
319319
u32 acpi_speed = i2c_dw_acpi_round_bus_speed(dev->dev);
320320
struct i2c_timings *t = &dev->timings;
321+
u32 wanted_speed;
322+
u32 legal_speed = 0;
323+
int i;
321324

322325
/*
323326
* Find bus speed from the "clock-frequency" device property, ACPI
@@ -329,6 +332,30 @@ void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
329332
t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
330333
else
331334
t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
335+
336+
wanted_speed = t->bus_freq_hz;
337+
338+
/* For unsupported speeds, scale down the lowest speed which is faster. */
339+
for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) {
340+
/* supported speeds is in decreasing order */
341+
if (wanted_speed == supported_speeds[i]) {
342+
legal_speed = 0;
343+
break;
344+
}
345+
if (wanted_speed > supported_speeds[i])
346+
break;
347+
348+
legal_speed = supported_speeds[i];
349+
}
350+
351+
if (legal_speed) {
352+
/*
353+
* Pretend this was the requested speed, but preserve the preferred
354+
* speed so the clock counts can be scaled.
355+
*/
356+
t->bus_freq_hz = legal_speed;
357+
dev->wanted_bus_speed = wanted_speed;
358+
}
332359
}
333360
EXPORT_SYMBOL_GPL(i2c_dw_adjust_bus_speed);
334361

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ struct dw_i2c_dev {
291291
u16 fp_lcnt;
292292
u16 hs_hcnt;
293293
u16 hs_lcnt;
294+
u32 wanted_bus_speed;
294295
int (*acquire_lock)(void);
295296
void (*release_lock)(void);
296297
int semaphore_idx;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev)
4141
static u16 clock_calc(struct dw_i2c_dev *dev, bool want_high)
4242
{
4343
struct i2c_timings *t = &dev->timings;
44-
u32 wanted_speed = t->bus_freq_hz;
44+
u32 wanted_speed = dev->wanted_bus_speed ?: t->bus_freq_hz;
4545
u32 clk_khz = i2c_dw_clk_rate(dev);
4646
u32 extra_ns = want_high ? t->scl_fall_ns : t->scl_rise_ns;
4747
u32 extra_cycles = (u32)((u64)clk_khz * extra_ns / 1000000);

0 commit comments

Comments
 (0)