参考资料
- Linux内核真正的I2C驱动控制器驱动程序。
drivers\i2c\busses\i2c-imx.c - 芯片手册:IMX6ULL:
Chapter 31: I2C Controller (I2C)
一、I2C控制器内部结构
I2C单独控制的话,占用CPU的时间非常高,这样不利于系统高效运转,所以一般的芯片里面都会有I2C控制器。
1.1 通用的简化结构
I2C控制器里面一般会有很多的寄存器。比如:
- controller_register可以设置I2C控制器的频率
- tx_register、rx_register控制数据的传输
- status_register控制I2C的状态
- int_register控制I2C的中断
当CPU需要发送数据时,只需要写给tx_register。后面的I2C控制器会自动帮忙做好,在SDA上发送数据。 当程序写入tx_register后,就不用管了。直到数据发送结束,会产生一个发送完成的中断。
1.2 IMX6ULL的I2C控制器内部结构
imx6ull有
- I2C_IFDR(频率寄存器)
- I2C_I2CR(控制寄存器
- I2C_I2SR(状态寄存器)
- I2C_I2DR(数据I/O寄存器)
- I2C_IADR(地址寄存器)
1.3 STM32MP157的I2C控制器内部结构体
- i2c_ker_ck时钟
- Data control(数据控制),shift register(移位寄存器)
当CPU把数据写入移位寄存器后,通过寄存器、通过SDA发送出去。接收数据则是相反的路径。 - Clock control(时钟控制)
二、I2C控制器操作方法
- 使能使用、设置时钟
- 发送数据:
- 把数据写入
tx_register ,等待中断发生 - 中断发生后,判断状态:是否发生错误、是否得到回应信号(ACK)
- 把下一个数据写入
tx_register ,等待中断:如此循环 - 接收数据:
- 设置
controller_register ,进入接收模式,启动接收,等待中断发生 - 中断发生后,判断状态,读取
tx_register 得到数据 - 如此循环
三、分析代码
3.1 设备树
- imx6ull:
arch/arm/boot/dts/imx6ull.dtsi
i2c1: i2c@02221a0000 {
#address-cells = <1>;
#size-cells = <0>;
compatible = <"fsl,imx6ul-i2c", "fsl,imx21-i2c";
reg = <0x021a0000 0x40000>;
interrupts = <GIC_SPI 36 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clks IMX6UL_CLK_I2C1>;
status = "disabled";
};
- STM32MP157:
arch/arm/boot/dts/stm32mp151.dtsi
i2c1: i2c@40012000 {
compatible = "st,stm32mp15-i2c";
reg = <0x40012000 0x400>;
interrupt-names = "event", "error";
interrupts-extended = <&exti 21 IRQ_TYPE_LEVEL_HIGH>,
<&intc GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&rcc I2C1_K>;
resets = <&rcc I2C1_R>;
#address-cells = <1>;
#size-cells = <0>;
dmas = <&dmamux1 33 0x400 0x80000001>,
<&dmamux1 34 0x400 0x80000001>;
dma-names = "rx", "tx";
power-domains = <&pd_core>;
st,syscfg-fmp = <&syscfg 0x4 0x1>;
wakeup-source;
status = "disabled";
};
3.2 驱动程序分析
读I2C数据时,要先发出设备地址,这是写操作,然后再发起读操作,涉及写、读操作。 所以可以以读I2C数据为例程学习核心代码。
- IMX6ULL
在文件drivers/i2c/busses/i2c-imx.c 中,分析i2c的具体程序:
static struct platform_driver i2c_imx_driver = {
.probe = i2c_imx_probe,
.remove = i2c_imx_remove,
.driver = {
.name = DRIVER_NAME,
.pm = I2C_IMX_PM_OPS,
.of_match_table = i2c_imx_dt_ids,
},
.id_table = imx_i2c_devtype,
};
static int i2c_imx_probe(struct platform_device *pdev)
{
const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
&pdev->dev);
struct imx_i2c_struct *i2c_imx;
struct resource *res;
struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
void __iomem *base;
int irq, ret;
dma_addr_t phy_addr;
irq = platform_get_irq(pdev, 0);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
base = devm_ioremap_resource(&pdev->dev, res);
phy_addr = (dma_addr_t)res->start;
i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
i2c_imx->adapter.owner = THIS_MODULE;
i2c_imx->adapter.algo = &i2c_imx_algo;
i2c_imx->adapter.dev.parent = &pdev->dev;
i2c_imx->adapter.nr = pdev->id;
i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
i2c_imx->base = base;
i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
ret = clk_prepare_enable(i2c_imx->clk);
ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr,
IRQF_NO_SUSPEND, pdev->name, i2c_imx);
init_waitqueue_head(&i2c_imx->queue);
i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
platform_set_drvdata(pdev, i2c_imx);
return 0;
}
static struct i2c_algorithm i2c_imx_algo = {
.master_xfer = i2c_imx_xfer,
.functionality = i2c_imx_func,
};
static int i2c_imx_xfer(struct i2c_adapter *adapter,
struct i2c_msg *msgs, int num)
{
result = i2c_imx_start(i2c_imx);
for (i = 0; i < num; i++) {
if (i == num - 1)
is_lastmsg = true;
if (i) {
dev_dbg(&i2c_imx->adapter.dev,
"<%s> repeated start\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
temp |= I2CR_RSTA;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
result = i2c_imx_bus_busy(i2c_imx, 1);
if (result)
goto fail0;
}
dev_dbg(&i2c_imx->adapter.dev,
"<%s> transfer message: %d\n", __func__, i);
if (msgs[i].flags & I2C_M_RD)
result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
else {
if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
else
result = i2c_imx_write(i2c_imx, &msgs[i]);
}
if (result)
goto fail0;
}
fail0:
i2c_imx_stop(i2c_imx);
pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent);
pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent);
out:
}
- STM32MP157
- rk3288
在文件drivers/i2c/busses/i2c-rk3x.c 中,分析i2c的具体程序:
static struct platform_driver rk3x_i2c_driver = {
.probe = rk3x_i2c_probe,
.remove = rk3x_i2c_remove,
.driver = {
.name = "rk3x-i2c",
.of_match_table = rk3x_i2c_match,
.pm = &rk3x_i2c_pm_ops,
},
};
module_platform_driver(rk3x_i2c_driver);
|