Skip to content

Commit 626f0a2

Browse files
Marek Vasutwsakernel
authored andcommitted
i2c: mxs: Implement arbitrary clock speed derivation algorithm
This patch drops the i2c timing tables from this driver and instead derives the timing based from the requested clock sleep. The timing tables were completely wrong anyway when observed on a scope. This new algorithm is also only derived by using a scope, but it seems to produce much more accurate result. Signed-off-by: Marek Vasut <[email protected]> Tested-by: Shawn Guo <[email protected]> [wsa: changed messages from dev_err to dev_warn] Signed-off-by: Wolfram Sang <[email protected]>
1 parent c2db409 commit 626f0a2

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

drivers/i2c/busses/i2c-mxs.c

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,6 @@
9393
#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
9494
MXS_I2C_CTRL0_MASTER_MODE)
9595

96-
struct mxs_i2c_speed_config {
97-
uint32_t timing0;
98-
uint32_t timing1;
99-
uint32_t timing2;
100-
};
101-
102-
/*
103-
* Timing values for the default 24MHz clock supplied into the i2c block.
104-
*
105-
* The bus can operate at 95kHz or at 400kHz with the following timing
106-
* register configurations. The 100kHz mode isn't present because it's
107-
* values are not stated in the i.MX233/i.MX28 datasheet. The 95kHz mode
108-
* shall be close enough replacement. Therefore when the bus is configured
109-
* for 100kHz operation, 95kHz timing settings are actually loaded.
110-
*
111-
* For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
112-
*/
113-
static const struct mxs_i2c_speed_config mxs_i2c_95kHz_config = {
114-
.timing0 = 0x00780030,
115-
.timing1 = 0x00800030,
116-
.timing2 = 0x00300030,
117-
};
118-
119-
static const struct mxs_i2c_speed_config mxs_i2c_400kHz_config = {
120-
.timing0 = 0x000f0007,
121-
.timing1 = 0x001f000f,
122-
.timing2 = 0x00300030,
123-
};
124-
12596
/**
12697
* struct mxs_i2c_dev - per device, private MXS-I2C data
12798
*
@@ -137,7 +108,9 @@ struct mxs_i2c_dev {
137108
struct completion cmd_complete;
138109
int cmd_err;
139110
struct i2c_adapter adapter;
140-
const struct mxs_i2c_speed_config *speed;
111+
112+
uint32_t timing0;
113+
uint32_t timing1;
141114

142115
/* DMA support components */
143116
int dma_channel;
@@ -153,9 +126,16 @@ static void mxs_i2c_reset(struct mxs_i2c_dev *i2c)
153126
{
154127
stmp_reset_block(i2c->regs);
155128

156-
writel(i2c->speed->timing0, i2c->regs + MXS_I2C_TIMING0);
157-
writel(i2c->speed->timing1, i2c->regs + MXS_I2C_TIMING1);
158-
writel(i2c->speed->timing2, i2c->regs + MXS_I2C_TIMING2);
129+
/*
130+
* Configure timing for the I2C block. The I2C TIMING2 register has to
131+
* be programmed with this particular magic number. The rest is derived
132+
* from the XTAL speed and requested I2C speed.
133+
*
134+
* For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
135+
*/
136+
writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
137+
writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
138+
writel(0x00300030, i2c->regs + MXS_I2C_TIMING2);
159139

160140
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
161141
}
@@ -553,6 +533,43 @@ static bool mxs_i2c_dma_filter(struct dma_chan *chan, void *param)
553533
return true;
554534
}
555535

536+
static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, int speed)
537+
{
538+
/* The I2C block clock run at 24MHz */
539+
const uint32_t clk = 24000000;
540+
uint32_t base;
541+
uint16_t high_count, low_count, rcv_count, xmit_count;
542+
struct device *dev = i2c->dev;
543+
544+
if (speed > 540000) {
545+
dev_warn(dev, "Speed too high (%d Hz), using 540 kHz\n", speed);
546+
speed = 540000;
547+
} else if (speed < 12000) {
548+
dev_warn(dev, "Speed too low (%d Hz), using 12 kHz\n", speed);
549+
speed = 12000;
550+
}
551+
552+
/*
553+
* The timing derivation algorithm. There is no documentation for this
554+
* algorithm available, it was derived by using the scope and fiddling
555+
* with constants until the result observed on the scope was good enough
556+
* for 20kHz, 50kHz, 100kHz, 200kHz, 300kHz and 400kHz. It should be
557+
* possible to assume the algorithm works for other frequencies as well.
558+
*
559+
* Note it was necessary to cap the frequency on both ends as it's not
560+
* possible to configure completely arbitrary frequency for the I2C bus
561+
* clock.
562+
*/
563+
base = ((clk / speed) - 38) / 2;
564+
high_count = base + 3;
565+
low_count = base - 3;
566+
rcv_count = (high_count * 3) / 4;
567+
xmit_count = low_count / 4;
568+
569+
i2c->timing0 = (high_count << 16) | rcv_count;
570+
i2c->timing1 = (low_count << 16) | xmit_count;
571+
}
572+
556573
static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
557574
{
558575
uint32_t speed;
@@ -572,12 +589,12 @@ static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
572589
}
573590

574591
ret = of_property_read_u32(node, "clock-frequency", &speed);
575-
if (ret)
592+
if (ret) {
576593
dev_warn(dev, "No I2C speed selected, using 100kHz\n");
577-
else if (speed == 400000)
578-
i2c->speed = &mxs_i2c_400kHz_config;
579-
else if (speed != 100000)
580-
dev_warn(dev, "Unsupported I2C speed selected, using 100kHz\n");
594+
speed = 100000;
595+
}
596+
597+
mxs_i2c_derive_timing(i2c, speed);
581598

582599
return 0;
583600
}
@@ -621,7 +638,6 @@ static int mxs_i2c_probe(struct platform_device *pdev)
621638
return err;
622639

623640
i2c->dev = dev;
624-
i2c->speed = &mxs_i2c_95kHz_config;
625641

626642
init_completion(&i2c->cmd_complete);
627643

0 commit comments

Comments
 (0)