嵌入式系统与单片机|技术阅读
登录|注册

您现在的位置是:嵌入式系统与单片机 > 技术阅读 > Linux Platform Driver 之 I2C Driver

Linux Platform Driver 之 I2C Driver

上篇中说到,Linux kernel中大部分设备可以归结为平台设备,因此大部分的驱动是平台驱动。

I2C控制器挂载在platform Bus上,因此我们在linux kernel中常说的I2C driver,都是指I2C controller driver,都是以platform driver的形式存在,当然,对应的控制器是platform device。

小鱼学编程,公众号:嵌入式ABC

与此同时,kernel抽象出I2C bus(/sys/bus/i2c),用于挂载和I2C controller通过I2C总线连接的各个I2C slave device。

本篇学习i2c驱动架构。并理解和回答如下问题

      • I2C Controller Driver是如何基于platform driver展开的

      • 为什么需要I2C Adapter, I2C Adapter提供了什么?

      • I2C Slave Driver是如何实现的

I2C 硬件总线拓扑

设想一个智能家居中控屏带有LCD大屏和电容触摸屏。其电容触摸屏,加速度传感器(横竖屏)等有可能即是i2C接口。
上文中,我们将SOC外设抽象为platform bus上的platform 设备。处理器i2c controller作为master,i2c总线上的触摸屏和传感器为从设备(i2C slaves)。则进一步抽象为下图

Note:linux不支持I2C外设作为i2c slave设备。但是绝大数的跑linux系统的处理器应该都是作为主设备的。

linux kernel I2C framework

上图的硬件拓扑在i2c framework中又是如何抽象的呢?

其中

/sys/bus/i2c/devices/<0>-<1>/where <0> is the bus the chip is connected to (e. g. i2c-0)and <1> the chip address例如:pi@raspberrypi:/sys/bus/platform/devices/3f804000.i2c $ ls
driver driver_override i2c-1 modalias of_node power subsystem uevent

i2c Framework 框架图

代码位于路径drivers\i2c\

  • I2C core使用I2C adapter和I2C algorithm两个子模块抽象I2C controller的功能,使用I2C client和I2C driver抽象I2C slave device的功能(对应设备模型中的device和device driver)。另外,基于I2C协议,通过smbus模块实现SMBus(System Management Bus,系统管理总线)的功能。

  • I2C busses是各个I2C controller drivers的集合,位于drivers/i2c/busses/目录下,驱动工程师常说的“I2C driver”就是指它们。比如树莓派的i2C驱动:busses\i2c-bcm2835.c

I2C Controller Driver

上节中提到了,I2C busses是各个I2C controller drivers的集合,位于drivers/i2c/busses/目录下,驱动工程师常说的“I2C driver”就是指它们。比如树莓派的i2C驱动:busses\i2c-bcm2835.c。
注意这里的driver 和i2C core 框图里面的driver的区别。
还以busses\i2c-bcm2835.c展开,如下图,这是一个platform_driver.

参照上文中的platform驱动开发框架,

  • 模块的入口和出口

  • platform driver三要素

    • struct platform_driver变量

    • probe/remove函数

    • 用于和device tree匹配的match table

    其中“模块的入口和出口”, 由宏 module_platform_driver(bcm2835_i2c_driver);定义。

    三要素中的“match table"函数

    //driver中match tablestatic const struct of_device_id bcm2835_i2c_of_match[] = { { .compatible = "brcm,bcm2835-i2c" }, {},};
    //dts 中 i2c0 controller资源 i2c0: i2c@20205000 { compatible = "brcm,bcm2835-i2c"; reg = <0x7e205000 0x1000>; interrupts = <2 21>; clocks = <&clk_i2c>; #address-cells = <1>; #size-cells = <0>; status = "disabled";    };

    三要素中的“probe"函数

    static int bcm2835_i2c_probe(struct platform_device *pdev){ struct bcm2835_i2c_dev *i2c_dev; struct resource *mem, *irq; u32 bus_clk_rate, divider; int ret; struct i2c_adapter *adap;
    i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); if (!i2c_dev) return -ENOMEM; platform_set_drvdata(pdev, i2c_dev); i2c_dev->dev = &pdev->dev; init_completion(&i2c_dev->completion);
    mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem); if (IS_ERR(i2c_dev->regs)) return PTR_ERR(i2c_dev->regs);
    i2c_dev->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(i2c_dev->clk)) { dev_err(&pdev->dev, "Could not get clock\n"); return PTR_ERR(i2c_dev->clk); }
    ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &bus_clk_rate); if (ret < 0) { dev_warn(&pdev->dev, "Could not read clock-frequency property\n"); bus_clk_rate = 100000; }
    divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate); /* * Per the datasheet, the register is always interpreted as an even * number, by rounding down. In other words, the LSB is ignored. So, * if the LSB is set, increment the divider to avoid any issue. */ if (divider & 1) divider++; bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
    irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); if (!irq) { dev_err(&pdev->dev, "No IRQ resource\n"); return -ENODEV; } i2c_dev->irq = irq->start;
    ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED, dev_name(&pdev->dev), i2c_dev); if (ret) { dev_err(&pdev->dev, "Could not request IRQ\n"); return -ENODEV; }
    adap = &i2c_dev->adapter; i2c_set_adapdata(adap, i2c_dev); adap->owner = THIS_MODULE; adap->class = I2C_CLASS_DEPRECATED; strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name)); adap->algo = &bcm2835_i2c_algo; adap->dev.parent = &pdev->dev; adap->dev.of_node = pdev->dev.of_node;
    bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
    ret = i2c_add_adapter(adap); if (ret) free_irq(i2c_dev->irq, i2c_dev);
    return ret;}

    我们发现其实和上文中的serial driver干了类似的事情

    回忆一下上文中的serial driver:

    • 定义并注册uart driver

    • 注册uart port

    • 定义并实现uart ops

    这里的i2C driver(注意是图中的i2c buses)应该也是完成的类似的实现,只是不同于serial driver,这里又封装了个"adapter". 在上面的probe函数中,最终是添加了一个"adapter"。

    adap = &i2c_dev->adapter; i2c_set_adapdata(adap, i2c_dev); adap->owner = THIS_MODULE; adap->class = I2C_CLASS_DEPRECATED; strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name)); adap->algo = &bcm2835_i2c_algo; adap->dev.parent = &pdev->dev;  adap->dev.of_node = pdev->dev.of_node;  bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0); ret = i2c_add_adapter(adap);

    这个adapter最终实现了底层的驱动

    static const struct i2c_algorithm bcm2835_i2c_algo = { .master_xfer = bcm2835_i2c_xfer, .functionality = bcm2835_i2c_func,};
    //probe函数中://注册收发中断 ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED, dev_name(&pdev->dev), i2c_dev);//给adapter的algo赋值 adap->algo = &bcm2835_i2c_algo;

    i2c core

    上节中提到,I2C core使用I2C adapter和I2C algorithm两个子模块抽象I2C controller的功能,使用I2C client和I2C driver抽象I2C slave device的功能(对应设备模型中的device和device driver)。这里的driver是i2c设备driver。
    /* * i2c_adapter is the structure used to identify a physical i2c bus along * with the access algorithms necessary to access it. */struct i2c_adapter { struct module *owner; unsigned int class; /* classes to allow probing for */ const struct i2c_algorithm *algo; /* the algorithm to access the bus */ void *algo_data;
    /* data fields that are valid for all devices */ struct rt_mutex bus_lock;
    int timeout; /* in jiffies */ int retries; struct device dev; /* the adapter device */
    int nr; char name[48]; struct completion dev_released;
    struct mutex userspace_clients_lock; struct list_head userspace_clients;
    struct i2c_bus_recovery_info *bus_recovery_info; const struct i2c_adapter_quirks *quirks;};
    下文再接着看如何基于kernel提供的i2c framework,在内核空间或者用户空间开发i2c 设备驱动(device driver)