一. I2C控制器设备的定义
1.0 I2C控制器设备的定义及注册
a. 在arch/arm/plat-samsung/dev-i2c0.c中,定义了I2C控制器设备的资源,
-
static struct resource s3c_i2c_resource[] = {
-
[0] = {
-
.start = S3C_PA_IIC,
-
.end = S3C_PA_IIC + SZ_4K - 1,
-
.flags = IORESOURCE_MEM,
-
},
-
[1] = {
-
.start = IRQ_IIC,
-
.end = IRQ_IIC,
-
.flags = IORESOURCE_IRQ,
-
},
-
};
-
-
struct platform_device s3c_device_i2c0 = {
-
.name = "s3c2410-i2c",
-
#ifdef CONFIG_S3C_DEV_I2C1
-
.id = 0,
-
#else
-
.id = -1,
-
#endif
-
.num_resources = ARRAY_SIZE(s3c_i2c_resource),
-
.resource = s3c_i2c_resource,
-
};
-
I2C控制器设备的私有成员定义
-
static struct s3c2410_platform_i2c default_i2c_data0 __initdata = {
-
.flags = 0,
-
.slave_addr = 0x10,
-
.frequency = 100*1000,
-
.sda_delay = 100,
-
};
b. I2C控制器设备的注册
注册过程还是老样子, 在machine_init中完成的
arch/arm/mach-s3c64xx/mach-smdk6410.c
-
static struct platform_device *smdk6410_devices[] __initdata = {
-
&s3c_device_i2c0,
-
}
-
static void __init smdk6410_machine_init(void)
-
{
-
s3c_i2c0_set_platdata(NULL);
-
platform_add_devices(smdk6410_devices, ARRAY_SIZE(smdk6410_devices));
-
}
二. I2C控制器设备驱动
2.1 I2C控制器的初始化
a. I2C控制器的驱动在文件, drivers/i2c/busses/i2c-s3c2410.c中
-
static struct platform_device_id s3c24xx_driver_ids[] = {
-
{
-
.name = "s3c2410-i2c",
-
.driver_data = TYPE_S3C2410,
-
}, {
-
.name = "s3c2440-i2c",
-
.driver_data = TYPE_S3C2440,
-
}, { },
-
};
-
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);
-
}
注意: 在I2C控制器的设备定义中名字是 s2c2410-i2c而这儿是s3c-i2c,两个名字不相同,如何匹配呢?
答案在s3c24xx_driver_ids中,如果没有s3c24xx_driver_ids,那么I2C控制器设备与I2C控制器设备驱动是一一对应的,一个设备对应一个设备驱动;
但是有了s3c24xx_driver_ids之后,一个设备驱动可以对应多个设备
b. 进入probe函数
-
static int s3c24xx_i2c_probe(struct platform_device *pdev)
-
{
-
struct s3c2410_platform_i2c* pdata = pdev->dev.platform_data;
-
//申请一个s3c24xx_i2c结构体并初始化
-
struct s3c24xx_i2c * i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
-
strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
-
i2c->adap.owner = THIS_MODULE;
-
i2c->adap.algo = &s3c24xx_i2c_algorithm;
-
i2c->adap.retries = 2;
-
i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
-
i2c->tx_setup = 50;
-
spin_lock_init(&i2c->lock);
-
init_waitqueue_head(&i2c->wait);
-
i2c->dev = &pdev->dev;
-
i2c->clk = clk_get(&pdev->dev, "i2c"); //时钟使能
-
clk_enable(i2c->clk);
-
//映射i2c的控制寄存器
-
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-
i2c->ioarea = request_mem_region(res->start, resource_size(res), pdev->name);
-
i2c->regs = ioremap(res->start, resource_size(res));
-
i2c->adap.algo_data = i2c;
-
i2c->adap.dev.parent = &pdev->dev;
-
ret = s3c24xx_i2c_init(i2c); //配置寄存器,初始化i2c
-
//获取中断
-
i2c->irq = ret = platform_get_irq(pdev, 0);
-
ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, dev_name(&pdev->dev), i2c);
-
ret = s3c24xx_i2c_register_cpufreq(i2c);
-
i2c->adap.nr = pdata->bus_num;
-
ret = i2c_add_numbered_adapter(&i2c->adap); //最关键的一步,把这个i2c控制器添加到adapter中
-
platform_set_drvdata(pdev, i2c);
-
clk_disable(i2c->clk);
-
return 0;
-
}
c. i2c控制器的初始化函数
s3c24xx_i2c_probe
--> s3c24xx_i2c_init
-
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;
-
pdata = i2c->dev->platform_data;
-
if (pdata->cfg_gpio)
-
pdata->cfg_gpio(to_platform_device(i2c->dev));
-
writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
-
writel(iicon, i2c->regs + S3C2410_IICCON);
-
s3c24xx_i2c_clockrate(i2c, &freq);
-
return 0;
-
}
d. i2c控制器及其上的设备注册过程
s3c24xx_i2c_probe
-->
i2c_add_numbered_adapter
-
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
-
{
-
retry:
-
if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
-
return -ENOMEM;
-
-
mutex_lock(&core_lock);
-
-
status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
-
-
mutex_unlock(&core_lock);
-
if (status == -EAGAIN)
-
goto retry;
-
-
if (status == 0)
-
status = i2c_register_adapter(adap); //注册i2c总线及其上的设备
-
return status;
-
}
s3c24xx_i2c_probe
-->
i2c_add_numbered_adapter
--> i2c_register_adapter
-
static int i2c_register_adapter(struct i2c_adapter *adap)
-
{
-
int res = 0;
-
-
rt_mutex_init(&adap->bus_lock);
-
mutex_init(&adap->userspace_clients_lock);
-
INIT_LIST_HEAD(&adap->userspace_clients);
-
-
if (adap->timeout == 0)
-
adap->timeout = HZ;
-
-
dev_set_name(&adap->dev, "i2c-%d", adap->nr);
-
adap->dev.bus = &i2c_bus_type;
-
adap->dev.type = &i2c_adapter_type;
-
res = device_register(&adap->dev); //注册i2c控制器
-
if (adap->nr < __i2c_first_dynamic_bus_num)
-
i2c_scan_static_board_info(adap); //扫描i2c总线上的所有设备,并注册每一个设备
-
-
mutex_lock(&core_lock);
-
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
-
mutex_unlock(&core_lock);
-
-
return 0;
-
}
s3c24xx_i2c_probe
-->
i2c_add_numbered_adapter
--> i2c_register_adapter
-->
i2c_scan_static_board_info
-
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
-
{
-
//__i2c_board_list是在smdk6410.c中初始化的,是系统中全部i2c设备的列表
-
//这儿要扫描全部的i2c设备,并为每一个i2c设备注册
-
down_read(&__i2c_board_lock);
-
list_for_each_entry(devinfo, &__i2c_board_list, list) {
-
if (devinfo->busnum == adapter->nr && !i2c_new_device(adapter, &devinfo->board_info))
-
dev_err(&adapter->dev,"Can't create device at 0x%02x\n", devinfo->board_info.addr);
-
}
-
up_read(&__i2c_board_lock);
-
}
s3c24xx_i2c_probe
-->
i2c_add_numbered_adapter
--> i2c_register_adapter
-->
i2c_scan_static_board_info
--> i2c_new_device
-
struct i2c_client * i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
-
{
-
struct i2c_client * client = kzalloc(sizeof *client, GFP_KERNEL); //申请i2c_client结构体的内存
-
-
//把i2c_client结构体初始化一下
-
client->adapter = adap;
-
client->dev.platform_data = info->platform_data;
-
if (info->archdata)
-
client->dev.archdata = *info->archdata;
-
client->flags = info->flags;
-
client->addr = info->addr;
-
client->irq = info->irq;
-
strlcpy(client->name, info->type, sizeof(client->name));
-
status = i2c_check_client_addr_validity(client);
-
status = i2c_check_addr_busy(adap, client->addr);
-
-
//把client->dev结构体初始化一下
-
client->dev.parent = &client->adapter->dev;
-
client->dev.bus = &i2c_bus_type;
-
client->dev.type = &i2c_client_type;
-
client->dev.of_node = info->of_node;
-
dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), client->addr);
-
status = device_register(&client->dev); //注册client->dev
-
return client; //这里虽然返回client结构体,但是没有使用
-
}
注意: !i2c_new_device(adapter, &devinfo->board_info);
i2c_new_device虽然有个client结构体但是没有人使用,只是把client->dev 注册了一下,client中剩余的部分如变量的addr flags没有人管.
那问题出来了,在使用时如果需要client->addr怎么办? (例如: i2c在数据传输时需要器件地址 addr=0x30)
答: 用小结构体得到大结构体 --> container_of -->struct i2c_client *client = i2c_verify_client(dev);
2.2 I2C控制器的algorithm
s3c24xx_i2c_probe中注册了i2c控制器的algorithm是s3c24xx_i2c_algorithm
-
static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
-
.master_xfer = s3c24xx_i2c_xfer,
-
.functionality = s3c24xx_i2c_func,
-
};
a.下面就看一下,s3c24xx_i2c_xfer
-
static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
-
{
-
struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
-
clk_enable(i2c->clk); //使能i2c时钟
-
//调用s3c24xx_i2c_doxfer行进传输
-
//重复adap->retries次,如果不成功则sleep 100us
-
for (retry = 0; retry < adap->retries; retry++) {
-
ret = s3c24xx_i2c_doxfer(i2c, msgs, num); //传输msg,但是真正的数据传输的过程是中断
-
if (ret != -EAGAIN) {
-
clk_disable(i2c->clk);
-
return ret;
-
}
-
udelay(100);
-
}
-
clk_disable(i2c->clk); //关闭i2c时钟
-
return -EREMOTEIO;
-
}
b. 进入开始传输状态
s3c24xx_i2c_xfer
--> s3c24xx_i2c_doxfer
-
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
-
{
-
unsigned long iicstat, timeout;
-
int spins = 20;
-
int ret;
-
-
if (i2c->suspended)
-
return -EIO;
-
-
ret = s3c24xx_i2c_set_master(i2c); //等侍I2C总线为空闲状态
-
spin_lock_irq(&i2c->lock);
-
i2c->msg = msgs;
-
i2c->msg_num = num;
-
i2c->msg_ptr = 0;
-
i2c->msg_idx = 0;
-
i2c->state = STATE_START; //设定此时状态为START
-
s3c24xx_i2c_enable_irq(i2c); //开中断
-
s3c24xx_i2c_message_start(i2c, msgs); //进入start传输状态,剩下的事就交给中断了
-
spin_unlock_irq(&i2c->lock);
-
-
timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); //进入等侍状态,直到传输结束,产生stop信号将其唤醒
-
ret = i2c->msg_idx;
-
do {
-
iicstat = readl(i2c->regs + S3C2410_IICSTAT);
-
} while ((iicstat & S3C2410_IICSTAT_START) && --spins);
-
-
if (!spins) {
-
msleep(1);
-
iicstat = readl(i2c->regs + S3C2410_IICSTAT);
-
}
-
out:
-
return ret;
-
}
注意 I2C的状态:一个是通过i2c->state可以叫做软件层的状态标志,另一个就是读取i2c的IICSTAT寄存器标志着当前IIC控制器总线的状态是空闲还是忙
c. 等侍IIC总结空闲
s3c24xx_i2c_xfer
--> s3c24xx_i2c_doxfer
-->
s3c24xx_i2c_set_master
-
static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
-
{
-
//读取IICSTAT寄存器的状态,直到IIC总线是空闲状态
-
//重复读取400次,每次不成功sleep 1ms
-
unsigned long iicstat;
-
int timeout = 400;
-
while (timeout-- > 0) {
-
iicstat = readl(i2c->regs + S3C2410_IICSTAT);
-
if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
-
return 0;
-
msleep(1);
-
}
-
return -ETIMEDOUT;
-
}
IICSTAT[bit5] --> read 0 --> not busy
IICSTAT[bit5] --> read 1 --> busy
IICSTAT[bit5] --> write 0 --> 产生一个stop signal
IICSTAT[bit5] --> write 1 --> 产生一个start signal
d. 进行传输开始状态
-
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;
-
} else
-
stat |= 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);
-
-
writeb(addr, i2c->regs + S3C2410_IICDS);
-
-
ndelay(i2c->tx_setup);
-
-
writel(iiccon, i2c->regs + S3C2410_IICCON);
-
-
//IICSTAT[bit5] --> write 1 --> 产生一个start signal,下面就进入中断了
-
stat |= S3C2410_IICSTAT_START;
-
writel(stat, i2c->regs + S3C2410_IICSTAT);
-
}
e. 在中断中进行实际的传输
IIC产生中断的条件:
第1种情况是: 产生了start信号
第2种情况是: IIC仲裁失败
第3种情况是: 产生了stop信号
-
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);
-
-
//如果是第2种情况--> IIC仲裁失败,则打印出错信息
-
if (status & S3C2410_IICSTAT_ARBITR)
-
dev_err(i2c->dev, "deal with arbitration loss\n");
-
//如果当前的状态是IDLE,说明状态出错了
-
if (i2c->state == STATE_IDLE) {
-
tmp = readl(i2c->regs + S3C2410_IICCON);
-
tmp &= ~S3C2410_IICCON_IRQPEND;
-
writel(tmp, i2c->regs + S3C2410_IICCON);
-
goto out;
-
}
-
//进入中断调用过程
-
i2c_s3c_irq_nextbyte(i2c, status);
-
out:
-
return IRQ_HANDLED;
-
}
f. 中断调用过程
s3c24xx_i2c_irq
-->
i2c_s3c_irq_nextbyte
-
static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
-
{
-
unsigned long tmp;
-
unsigned char byte;
-
int ret = 0;
-
-
switch (i2c->state) {
-
case STATE_IDLE:
-
goto out;
-
-
case STATE_STOP:
-
s3c24xx_i2c_disable_irq(i2c);
-
goto out_ack;
-
-
case STATE_START:
-
if (iicstat & S3C2410_IICSTAT_LASTBIT && !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
-
s3c24xx_i2c_stop(i2c, -ENXIO);
-
goto out_ack;
-
}
-
-
if (i2c->msg->flags & I2C_M_RD)
-
i2c->state = STATE_READ;
-
else
-
i2c->state = STATE_WRITE;
-
-
if (is_lastmsg(i2c) && i2c->msg->len == 0) {
-
s3c24xx_i2c_stop(i2c, 0);
-
goto out_ack;
-
}
-
-
if (i2c->state == STATE_READ)
-
goto prepare_read;
-
//注意:这儿没有break,所以直接到STATE_WRITE或者prepare_read
-
case STATE_WRITE:
-
if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
-
if (iicstat & S3C2410_IICSTAT_LASTBIT) {
-
s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
-
goto out_ack;
-
}
-
}
-
retry_write:
-
if (!is_msgend(i2c)) { //如果这个msg中的数据还没有传完
-
byte = i2c->msg->buf[i2c->msg_ptr++]; //获取下一个要传输的字节
-
writeb(byte, i2c->regs + S3C2410_IICDS); //向IIC中写入数据
-
ndelay(i2c->tx_setup);
-
} else if (!is_lastmsg(i2c)) { //如果还有其它的msg
-
i2c->msg_ptr = 0;
-
i2c->msg_idx++;
-
i2c->msg++; //移动到下一个msg
-
if (i2c->msg->flags & I2C_M_NOSTART) {
-
if (i2c->msg->flags & I2C_M_RD)
-
s3c24xx_i2c_stop(i2c, -EINVAL);
-
goto retry_write;
-
} else { //产生一个start信号
-
s3c24xx_i2c_message_start(i2c, i2c->msg);
-
i2c->state = STATE_START;
-
}
-
} else {
-
s3c24xx_i2c_stop(i2c, 0); //终于传完了,就发送结束singal,并关中断
-
}
-
break;
-
case STATE_READ:
-
byte = readb(i2c->regs + S3C2410_IICDS);
-
i2c->msg->buf[i2c->msg_ptr++] = byte;
-
-
prepare_read:
-
if (is_msglast(i2c)) {
-
if (is_lastmsg(i2c))
-
s3c24xx_i2c_disable_ack(i2c);
-
} else if (is_msgend(i2c)) {
-
if (is_lastmsg(i2c))
-
s3c24xx_i2c_stop(i2c, 0);
-
else {
-
i2c->msg_ptr = 0;
-
i2c->msg_idx++;
-
i2c->msg++;
-
}
-
}
-
break;
-
}
-
out_ack:
-
tmp = readl(i2c->regs + S3C2410_IICCON);
-
tmp &= ~S3C2410_IICCON_IRQPEND;
-
writel(tmp, i2c->regs + S3C2410_IICCON);
-
out:
-
return ret;
-
}
s3c24xx_i2c_irq
-->
i2c_s3c_irq_nextbyte
--> s3c24xx_i2c_stop
-
static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
-
{
-
unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
-
-
iicstat &= ~S3C2410_IICSTAT_START;
-
writel(iicstat, i2c->regs + S3C2410_IICSTAT);
-
-
i2c->state = STATE_STOP; //状态改为STOP
-
-
s3c24xx_i2c_master_complete(i2c, ret); //wake_up等侍队列,这时候函数s3c24xx_i2c_doxfer就可以返回了
-
s3c24xx_i2c_disable_irq(i2c); //关中断,不能再让中断产生了
-
}
三. 总结
阅读(1551) | 评论(0) | 转发(0) |