Linux-IIC驱动(2)-Linux下IIC子系统的介绍

2024-08-31 06:38

本文主要是介绍Linux-IIC驱动(2)-Linux下IIC子系统的介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

IIC子系统架构

 

Linux下IIC的架构模型大概可以分为3层:

第一层是IIC的从设备驱动,他包含图中的device driver和i2c-dev。device driver需要用户编写,i2c-dev由内核实现,包含了IIC设备的通用方法,但是用户不能直接使用这个驱动,需要编写一个用户层驱动,它们2个合起来才可以实现一个驱动程序。

 

第二层总线驱动,他又叫做控制器驱动,比如说芯片内部的IIC控制器的使用需要实现一个驱动程序,比如所需要往IIC总线上传输数据它需要什么方法,需要实现那些函数等等。它包含图中的i2c-adapter和adapter-agio。

 

第三层是i2c-core。它作为一个媒介关联了总线驱动和i2c驱动。第二个作用就是它还提供了一个IIC驱动的注册和注销。

IIC总线驱动

 

图中我们可以知道编写i2c驱动有2种实现方法。

第一种是用户自己完全写一个i2c的驱动程序。

另一种方法是使用i2c的通用驱动i2c-dev,然后自己设计一个i2c用户模式驱动。

 

 

对于第一种情况,应用程序读写i2c设备的流程是比较明了的。

 

对于第二种情况的读写流程稍微有点复杂,我们先来介绍另一个比较重要的地方,就是i2c控制器驱动,i2c adapter/algorithm。

它是直接操作i2c设备的。里面包含2部分的东西,一个是adapter,适配器的意思,另一个是algorithm,算法的意思。

 

 

我们先来分析adapter,打开Linux源代码,搜索i2c_adapter

/** 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 id;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	*/u8 level; 			/* nesting level for lockdep */struct mutex bus_lock;int timeout;			/* in jiffies */int retries;struct device dev;		/* the adapter device */int nr;char name[48];struct completion dev_released;
};


在Linux中每一个i2c适配器或者说控制器都会有一个i2c_adapter来描述一个i2c控制器并确定i2c设备的访问方法。

里面有一个成员

const struct i2c_algorithm *algo; /* the algorithm to access the bus */

 

看看它的定义:

/** The following structs are for those who like to implement new bus drivers:* i2c_algorithm is the interface to a class of hardware solutions which can* be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584* to name two of the most common.*/
struct i2c_algorithm {/* If an adapter algorithm can't do I2C-level access, set master_xferto NULL. If an adapter algorithm can do SMBus access, setsmbus_xfer. If set to NULL, the SMBus protocol is simulatedusing common I2C messages *//* master_xfer should return the number of messages successfullyprocessed, or a negative value on error */int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,int num);int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,unsigned short flags, char read_write,u8 command, int size, union i2c_smbus_data *data);/* To determine what the adapter supports */u32 (*functionality) (struct i2c_adapter *);
};


这个结构里面主要是一些函数指针,最重要的要数master_xfer了。这个函数主要来实现需要在i2c总线上传输数据的方法。比如说CPU需要通过i2c总线往一个i2c设备发送一些数据,这时候就可以使用i2c控制器里面实现的传输方法,比如说xxx_transfer来实现对i2c设备的读写。这样就把i2c数据的传输给封装起来了。

 

 

我们接下来分析一下2440上i2c控制器驱动的实现,它的实现在i2c-s3c2410.c文件中实现。

 

先来分析一下模块初始化和退出函数:

MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);static struct platform_driver s3c24xx_i2c_driver = {.probe		= s3c24xx_i2c_probe,.remove		= s3c24xx_i2c_remove,.id_table	= s3c24xx_driver_ids,.driver		= {.owner	= THIS_MODULE,.name	= "s3c-i2c",.pm	= S3C24XX_DEV_PM_OPS,},
};static int __init i2c_adap_s3c_init(void)
{return platform_driver_register(&s3c24xx_i2c_driver);
}
subsys_initcall(i2c_adap_s3c_init);static void __exit i2c_adap_s3c_exit(void)
{platform_driver_unregister(&s3c24xx_i2c_driver);
}
module_exit(i2c_adap_s3c_exit);	.probe		= s3c24xx_i2c_probe,.remove		= s3c24xx_i2c_remove,.id_table	= s3c24xx_driver_ids,.driver		= {.owner	= THIS_MODULE,.name	= "s3c-i2c",.pm	= S3C24XX_DEV_PM_OPS,},
};static int __init i2c_adap_s3c_init(void)
{return platform_driver_register(&s3c24xx_i2c_driver);
}
subsys_initcall(i2c_adap_s3c_init);static void __exit i2c_adap_s3c_exit(void)
{platform_driver_unregister(&s3c24xx_i2c_driver);
}
module_exit(i2c_adap_s3c_exit);

 

这里主要通过平台驱动注册了一个设备。设备名叫做s3c24xx_i2c_driver。上面是s3c24xx_i2c_driver的定义,先来分析一些s3c24xx_i2c_probe函数。里面的一些变量赋值和其他一些简单的函数就不分析了,我们分析一个重要的函数s3c24xx_i2c_init。

 

/* s3c24xx_i2c_init** initialise the controller, set the IO lines and frequency
*/static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
{unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;struct s3c2410_platform_i2c *pdata;unsigned int freq;/* get the plafrom data */pdata = i2c->dev->platform_data;/* inititalise the gpio */if (pdata->cfg_gpio)pdata->cfg_gpio(to_platform_device(i2c->dev));/* write slave address */writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);writel(iicon, i2c->regs + S3C2410_IICCON);/* we need to work out the divisors for the clock... */if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {writel(0, i2c->regs + S3C2410_IICCON);dev_err(i2c->dev, "cannot meet bus frequency required\n");return -EINVAL;}/* todo - check that the i2c lines aren't being dragged anywhere */dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);return 0;
}


我们可以看到这个函数主要完成了一下的功能:

1、unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;使能中断和应答

2、pdata->cfg_gpio(to_platform_device(i2c->dev));初始化GPIO

3、writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);写从地址

4、if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {初始化时钟。

 

可以发现,这里的初始化代码和裸机的代码也是非常类似的。

初始化之后还调用了i2c_add_numbered_adapter函数来注册一个i2c适配器。

 

 

prob函数中有一个重要的赋值语句i2c->adap.algo    = &s3c24xx_i2c_algorithm;之前说过algorithm是用来实现i2c设备读写方法的。这里面包含2个函数,一个是s3c24xx_i2c_xfer,这个函数用来实现i2c设备的读写,他们依次调用s3c24xx_i2c_doxfer->s3c24xx_i2c_message_start,最终实现设备的读写,这个函数如下:

 

/* s3c24xx_i2c_message_start** put the start of a message onto the bus
*/static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,struct i2c_msg *msg)
{unsigned int addr = (msg->addr & 0x7f) << 1;unsigned long stat;unsigned long iiccon;stat = 0;stat |=  S3C2410_IICSTAT_TXRXEN;if (msg->flags & I2C_M_RD) {stat |= S3C2410_IICSTAT_MASTER_RX;addr |= 1;} elsestat |= S3C2410_IICSTAT_MASTER_TX;if (msg->flags & I2C_M_REV_DIR_ADDR)addr ^= 1;/* todo - check for wether ack wanted or not */s3c24xx_i2c_enable_ack(i2c);iiccon = readl(i2c->regs + S3C2410_IICCON);writel(stat, i2c->regs + S3C2410_IICSTAT);dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);writeb(addr, i2c->regs + S3C2410_IICDS);/* delay here to ensure the data byte has gotten onto the bus* before the transaction is started */ndelay(i2c->tx_setup);dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);writel(iiccon, i2c->regs + S3C2410_IICCON);stat |= S3C2410_IICSTAT_START;writel(stat, i2c->regs + S3C2410_IICSTAT);
}


我们对比2440数据手册中的IIC读写时序:

可以发现读写的流程和数据手册是一致的(肯定是一致的,不然就读写失败了)。

 

不过后面从ACK period开始就属于中断的内容了,我们需要找到i2c的中断处理函数,肯定是在prob函数里面找的,发现了这么一段话:

ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,dev_name(&pdev->dev), i2c);


这里面注册了一个中断,s3c24xx_i2c_irq。中断处理程序如下:

/* s3c24xx_i2c_irq** top level IRQ servicing routine
*/static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
{struct s3c24xx_i2c *i2c = dev_id;unsigned long status;unsigned long tmp;status = readl(i2c->regs + S3C2410_IICSTAT);if (status & S3C2410_IICSTAT_ARBITR) {/* deal with arbitration loss */dev_err(i2c->dev, "deal with arbitration loss\n");}if (i2c->state == STATE_IDLE) {dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");tmp = readl(i2c->regs + S3C2410_IICCON);tmp &= ~S3C2410_IICCON_IRQPEND;writel(tmp, i2c->regs +  S3C2410_IICCON);goto out;}/* pretty much this leaves us with the fact that we've* transmitted or received whatever byte we last sent */i2s_s3c_irq_nextbyte(i2c, status);out:return IRQ_HANDLED;
}


前面都是一些错误处理,主要分析i2s_s3c_irq_nextbyte函数。里面的步骤完成就是剩下的i2c读写的流程。

更多Linux资料及视频教程点击这里

 

 

这篇关于Linux-IIC驱动(2)-Linux下IIC子系统的介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123175

相关文章

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

Linux samba共享慢的原因及解决方案

《Linuxsamba共享慢的原因及解决方案》:本文主要介绍Linuxsamba共享慢的原因及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux samba共享慢原因及解决问题表现原因解决办法总结Linandroidux samba共享慢原因及解决

新特性抢先看! Ubuntu 25.04 Beta 发布:Linux 6.14 内核

《新特性抢先看!Ubuntu25.04Beta发布:Linux6.14内核》Canonical公司近日发布了Ubuntu25.04Beta版,这一版本被赋予了一个活泼的代号——“Plu... Canonical 昨日(3 月 27 日)放出了 Beta 版 Ubuntu 25.04 系统镜像,代号“Pluc

Linux安装MySQL的教程

《Linux安装MySQL的教程》:本文主要介绍Linux安装MySQL的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux安装mysql1.Mysql官网2.我的存放路径3.解压mysql文件到当前目录4.重命名一下5.创建mysql用户组和用户并修

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用