`
isiqi
  • 浏览: 15990179 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

I2C驱动原理(1)

 
阅读更多

I2C驱动有两部分组成:I2C总线驱动和I2C设备构成。

I2C总线驱动是对适配器端的实现,其含有适配器数据结构struct i2c_adapter,适配器算法数据结构struct i2c_algorithm。I2C设备驱动是对设备端的实现和控制,其含有设备驱动结构i2c_driver和设备客户端结构struct i2c_client。

struct i2c_adapter {
struct module *owner;
unsigned int id;
unsigned int class;
struct i2c_algorithm *algo;//总线通讯数据结构体
void *algo_data; //用于保存包含适配器的私有数据结构


int (*client_register)(struct i2c_client *);//客户端注册函数
int (*client_unregister)(struct i2c_client *);//客户端注销函数


struct semaphore bus_lock;
struct semaphore clist_lock;

int timeout;
int retries; //重试次数
struct device dev; //适配器设备
struct class_device class_dev; //类设备

int nr;
struct list_head clients; //客户端链表
struct list_head list; //适配器链表
char name[I2C_NAME_SIZE]; //适配器名称
struct completion dev_released;
struct completion class_dev_released;

};

struct i2c_algorithm {


int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,
int num); //i2c总线传输函数
int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
unsigned short flags, char read_write,
u8 command, int size, union i2c_smbus_data * data); //smbus总线传输函数

int (*slave_send)(struct i2c_adapter *,char*,int); //适配器为主模式时发送函数
int (*slave_recv)(struct i2c_adapter *,char*,int); //适配器为主模式时接收函数
int (*algo_control)(struct i2c_adapter *, unsigned int, unsigned long);

u32 (*functionality) (struct i2c_adapter *); //适配器支持功能


};以上两个数据结构为总线驱动需要设置并初始化。

struct i2c_driver {


struct module *owner;
char name[32]; //驱动名称
int id;
unsigned int class;
unsigned int flags; //I2C_DF_NOTIFY用于当设备依附或脱离时通知总线

int (*attach_adapter)(struct i2c_adapter *);//依附适配器函数
int (*detach_adapter)(struct i2c_adapter *);//脱离适配器函数

int (*detach_client)(struct i2c_client *); //脱离客户端函数

int (*command)(struct i2c_client *client,unsigned int cmd, void *arg);

struct device_driver driver; //设备驱动结构体
struct list_head list; //链表头


};

struct i2c_client {


unsigned int flags;
unsigned short addr; // 低7为设备地址
struct i2c_adapter *adapter; //依附的适配器
struct i2c_driver *driver; //依附的驱动结构
int usage_count;
struct device dev;
struct list_head list; //客户端链表
char name[I2C_NAME_SIZE];//客户端名称
struct completion released;


};以上两个数据结构为设备驱动需要设置并初始化。

Intel制定了SMBus标准用于低速通讯。SMBus二线接口与I2C接口非常相似。SMBus也使用一条数据线(SMBDATA)和一条时钟线 (SMBCLK)实现通讯。I2C接口和SMBus接口的主要区别是最大和最小时钟速度。SMBCLK必须在10kHz和100kHz之间。SMBCLK 和SMBDATA线也需要上拉电阻。3V供电时上拉电阻大于8.5k ,5V供电时上拉电阻大于14k 。SMBus工作电压范围在3V和5V之间,大于2.1V为高电平,低于0.8V为低电平。struct i2c_adapter对应于物理上的一个适配器,而struct i2c_algorithm对应于一套通讯方法。i2c_algorithm提供一些控制适配器发送或接收函数,对于i2c总线需要初始化 master_xfer函数,对于smbus总线需要初始化smbus_xfer函数。master_xfer函数是以i2c_msg为单位进行控制的, 其结构如下:

struct i2c_msg {


__u16 addr; //从设备地址
__u16 flags; //动作标志:读或写
#define I2C_M_TEN 0x10 //器件地址是10Bit
#define I2C_M_RD 0x01
#define I2C_M_NOSTART 0x4000 //意味当前i2c_msg不发送start信号
#define I2C_M_REV_DIR_ADDR 0x2000 //把读写标志位反转
#define I2C_M_IGNORE_NAK 0x1000//当前i2c_msg忽略I2C器件的ack和nack 信号。
#define I2C_M_NO_RD_ACK 0x0800 //表示在正行读操作时不去ACK
__u16 len; //信息长度
__u8 *buf; //信息缓冲区首地址

};

i2c_driver和i2c_client用于控制设备驱动方面的结构。当i2c_driver->attach_adapter探测到物理设备 后,因为i2c_client对应一个真实的物理设备则把探测到的i2c_client->adapter指向其依附的适配器的struct i2c_adapter 结构,把i2c_client->driver指向其依附的 i2c_driver结构.其注册和注销的函数分别为 i2c_attach_client和i2c_detach_client.

总线驱动需要定义一个包含 struct i2c_adapter的私有数据结构,用 i2c_adapter->algo_data指向它即可。

以下为2410适配器驱动程序:

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/i2c.h>
#include <linux/i2c-id.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/clk.h>

#include <asm/hardware.h>
#include <asm/irq.h>
#include <asm/io.h>

#include <asm/arch/regs-gpio.h>
#include <asm/arch/regs-iic.h>
#include <asm/arch/iic.h>

/* i2c controller state */
enum s3c24xx_i2c_state {
STATE_IDLE,
STATE_START,
STATE_READ,
STATE_WRITE,
STATE_STOP
};

struct s3c24xx_i2c {//适配器结构私有结构
spinlock_t lock;
wait_queue_head_t wait;

struct i2c_msg *msg;//信息
unsigned int msg_num;//信息数量
unsigned int msg_idx;//信息索引
unsigned int msg_ptr;//信息操作字节指针
////////////////////////以上用于操作信息结构

enum s3c24xx_i2c_state state;

void __iomem *regs;//io内存首地址
struct clk *clk;
struct device *dev; //控制器对应设备
struct resource *irq;//中断资源
struct resource *ioarea;//申请的IO内存
struct i2c_adapter adap;//适配器结构
};

//1010表明设备为EEPROM
static struct s3c2410_platform_i2c s3c24xx_i2c_default_platform = {//平台驱动数据
.flags = 0,
.slave_addr = 0x10,
.bus_freq = 100*1000,//I2C总线频率 100K
.max_freq = 400*1000,//I2C总线最大频率
.sda_delay = S3C2410_IICLC_SDA_DELAY5 | S3C2410_IICLC_FILTER_ON,//
};

/* s3c24xx_i2c_is2440()
*
* return true is this is an s3c2440
*/
static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
{
struct platform_device *pdev = to_platform_device(i2c->dev);

return !strcmp(pdev->name, "s3c2440-i2c");
}

//获取设备的平台设备数据
static inline struct s3c2410_platform_i2c *s3c24xx_i2c_get_platformdata(struct device *dev)
{
if (dev->platform_data != NULL)
return (struct s3c2410_platform_i2c *)dev->platform_data;
//有一种将平台数据赋给dev->platform_data,一般在平台设备结构中初始化 如nand如此
return &s3c24xx_i2c_default_platform;
}

//完成信息传递且唤醒等待队列对应进程
static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
{
dev_dbg(i2c->dev, "master_complete %d/n", ret);

i2c->msg_ptr = 0;
i2c->msg = NULL;
i2c->msg_idx ++;//指向下一个信息
i2c->msg_num = 0;
if (ret)
i2c->msg_idx = ret;

wake_up(&i2c->wait); //完成所有信息时唤醒进程
}

static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)//禁止响应
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
}

static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)//使能响应
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
}

static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)//关中断
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
}

static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)//使能中断
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
}

//起始字节发送
static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
struct i2c_msg *msg)
{
unsigned int addr = (msg->addr & 0x7f) << 1;//7位从设备地址先发送
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; //1为读
} else //为写?
stat |= S3C2410_IICSTAT_MASTER_TX;//设置控制为主且写动作

if (msg->flags & I2C_M_REV_DIR_ADDR)//切换操作动作
addr ^= 1;

s3c24xx_i2c_enable_ack(i2c);//使能响应

iiccon = readl(i2c->regs + S3C2410_IICCON);
writel(stat, i2c->regs + S3C2410_IICSTAT);//写入IICSTAT

dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS/n", stat, addr);
writeb(addr, i2c->regs + S3C2410_IICDS);//写入起始数据
udelay(1);
dev_dbg(i2c->dev, "iiccon, %08lx/n", iiccon);
writel(iiccon, i2c->regs + S3C2410_IICCON);//刷新iiccon

stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);//启动开始波形
}

static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)//停止传送动作
{
unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);

dev_dbg(i2c->dev, "STOP/n");
/* stop the transfer */
iicstat &= ~ S3C2410_IICSTAT_START;
writel(iicstat, i2c->regs + S3C2410_IICSTAT);//为写操作时发送停止波形

i2c->state = STATE_STOP; //i2c总线为停止状态

s3c24xx_i2c_master_complete(i2c, ret); //唤醒进程
s3c24xx_i2c_disable_irq(i2c);
}

//判断当前信息是否为信息队列中最后一个 1=y
static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
{
return i2c->msg_idx >= (i2c->msg_num - 1);
}

//判断该操作字节是否为信息中的最后一个 1=y
static inline int is_msglast(struct s3c24xx_i2c *i2c)
{
return i2c->msg_ptr == i2c->msg->len-1;
}

//判断该信息字节是否已处理完 1=y
static inline int is_msgend(struct s3c24xx_i2c *i2c)
{
return i2c->msg_ptr >= i2c->msg->len;
}

//传送动作处理
static int i2s_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: //总线空闲
dev_err(i2c->dev, "%s: called in STATE_IDLE/n", __FUNCTION__);
goto out;
break;

case STATE_STOP: //总线停止
dev_err(i2c->dev, "%s: called in STATE_STOP/n", __FUNCTION__);
s3c24xx_i2c_disable_irq(i2c);
goto out_ack;

case STATE_START:
//开始处理信息或下一个信息
if (iicstat & S3C2410_IICSTAT_LASTBIT &&
!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { //响应未到达 出错!!!!

dev_dbg(i2c->dev, "ack was not received/n");
s3c24xx_i2c_stop(i2c, -EREMOTEIO);//停止操作
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;

case STATE_WRITE:

retry_write:
if (!is_msgend(i2c)) { //信息未处理完?
byte = i2c->msg->buf[i2c->msg_ptr++];//处理下一字节
writeb(byte, i2c->regs + S3C2410_IICDS);//写入数据移位寄存器

} else if (!is_lastmsg(i2c)) { //非最后一个信息?
dev_dbg(i2c->dev, "WRITE: Next Message/n");

i2c->msg_ptr = 0; //处理下一信息
i2c->msg_idx ++;
i2c->msg++;

if (i2c->msg->flags & I2C_M_NOSTART) {//不需起始波形?
if (i2c->msg->flags & I2C_M_RD) { //因从写切换到读需起始波形,所以非法
s3c24xx_i2c_stop(i2c, -EINVAL);
}
goto retry_write;
} else { //需要起始波形
s3c24xx_i2c_message_start(i2c, i2c->msg);//起始波形
i2c->state = STATE_START;
}

} else { //一个信息处理完成后停止,以便处理下一个信息
s3c24xx_i2c_stop(i2c, 0);
}
break;

case STATE_READ:

if (!(i2c->msg->flags & I2C_M_IGNORE_NAK) &&
!(is_msglast(i2c) && is_lastmsg(i2c))) { //需要nack 信号&&非最后字节&&最后信息

if (iicstat & S3C2410_IICSTAT_LASTBIT) {//序列读 非最后字节完成后需响应信号
dev_dbg(i2c->dev, "READ: No A ck/n");

s3c24xx_i2c_stop(i2c, -ECONNREFUSED);//出错 停止
goto out_ack;
}
}

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)) {//最后信息?
dev_dbg(i2c->dev, "READ: Send Stop/n");
s3c24xx_i2c_stop(i2c, 0);//停止且所有信息成功处理完
} else {
dev_dbg(i2c->dev, "READ: Next Transfer/n");

i2c->msg_ptr = 0;//处理下一个信息
i2c->msg_idx++;
i2c->msg++;
}
}
break;
}

/* acknowlegde the IRQ and get back on with the work */
out_ack:
tmp = readl(i2c->regs + S3C2410_IICCON);
tmp &= ~S3C2410_IICCON_IRQPEND;
writel(tmp, i2c->regs + S3C2410_IICCON);
out:
return ret;
}

//中断处理函数
static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id,
struct pt_regs *regs)
{
struct s3c24xx_i2c *i2c = dev_id;
unsigned long status;
unsigned long tmp;

status = readl(i2c->regs + S3C2410_IICSTAT);//读状态寄存器以判断何种原因引起中断

if (status & S3C2410_IICSTAT_ARBITR) {//总线仲裁失败
dev_err(i2c->dev, "deal with arbitration loss/n");
}

if (i2c->state == STATE_IDLE) { //i2c总线空闲时
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;
}

//等待总线空闲
static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
{
unsigned long iicstat;
int timeout = 400;

while (timeout-- > 0) {
iicstat = readl(i2c->regs + S3C2410_IICSTAT);

if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))//总线不忙
return 0;
msleep(1);
}

dev_dbg(i2c->dev, "timeout: GPEDAT is %08x/n",
__raw_readl(S3C2410_GPEDAT));

return -ETIMEDOUT;
}

//实质操作函数
static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
{
unsigned long timeout;
int ret;

ret = s3c24xx_i2c_set_master(i2c);//等待总线空闲以占用总线
if (ret != 0) {
dev_err(i2c->dev, "cannot get bus (error %d)/n", ret);
ret = -EAGAIN;
goto out;
}

spin_lock_irq(&i2c->lock);

i2c->msg = msgs;
i2c->msg_num = num;
i2c->msg_ptr = 0;//指向第一字节
i2c->msg_idx = 0;//指向第一信息
i2c->state = STATE_START;

s3c24xx_i2c_enable_irq(i2c);
s3c24xx_i2c_message_start(i2c, msgs);//起始字节
spin_unlock_irq(&i2c->lock);

//将进程阻塞,若在5MS内信息传完或超时则退出等待唤醒
timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);

ret = i2c->msg_idx;

if (timeout == 0) //超时退出
dev_dbg(i2c->dev, "timeout/n");
else if (ret != num) //非超时退出两者一定要相等复制出错
dev_dbg(i2c->dev, "incomplete xfer (%d)/n", ret);

msleep(1);

out:
return ret;
}

//传输函数
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;
int retry;
int ret;

for (retry = 0; retry < adap->retries; retry++) {

ret = s3c24xx_i2c_doxfer(i2c, msgs, num);

if (ret != -EAGAIN)
return ret;

dev_dbg(i2c->dev, "Retrying transmission (%d)/n", retry);

udelay(100);
}

return -EREMOTEIO;
}

//返回适配器所支持的功能
static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
}

static struct i2c_algorithm s3c24xx_i2c_algorithm = {//i2c通信方式
.master_xfer = s3c24xx_i2c_xfer,//底层传输函数
.functionality = s3c24xx_i2c_func,//适配器支持的功能函数
};

static struct s3c24xx_i2c s3c24xx_i2c = {//私有结构
.lock = SPIN_LOCK_UNLOCKED,
.wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
.adap = {
.name = "s3c2410-i2c",
.owner = THIS_MODULE,
.algo = &s3c24xx_i2c_algorithm,//该适配器的通信方式
.retries = 2,
.class = I2C_CLASS_HWMON,
},
};

//返回 tx分频设定值
static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
unsigned int *div1, unsigned int *divs)
{
unsigned int calc_divs = clkin / wanted;// 0/100=0
unsigned int calc_div1;

if (calc_divs > (16*16))
calc_div1 = 512;
else
calc_div1 = 16;

calc_divs += calc_div1-1;//15
calc_divs /= calc_div1;//0

if (calc_divs == 0)
calc_divs = 1;
if (calc_divs > 17)
calc_divs = 17;

*divs = calc_divs;//1
*div1 = calc_div1;//16

return clkin / (calc_divs * calc_div1);//0
}

//测试设定的频率值是否和要求
static inline int freq_acceptable(unsigned int freq, unsigned int wanted)
{
int diff = freq - wanted;//-100不合要求?需测试

return (diff >= -2 && diff <= 2);
}

//为用户制定一个除数为请求频率设置,要么被请求的频率或扫描可以接受频率范围内,直到有发现
static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c,
struct s3c2410_platform_i2c *pdata,
unsigned long *iicon,
unsigned int *got)
{
unsigned long clkin = clk_get_rate(i2c->clk);

unsigned int divs, div1;
int freq;
int start, end;

clkin /= 1000; //时钟由转换为KHZ单位,为0

dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu/n",
pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);

if (pdata->bus_freq != 0) {//时钟频率存在?
freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
&div1, &divs);//div1=16 divs=1 freq=0
if (freq_acceptable(freq, pdata->bus_freq/1000))//测试设定的频率值是否和要求
goto found;
}

//不合要求需查找合适值
start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;//start =400K
end = pdata->min_freq;//0

start /= 1000;//400
end /= 1000;//0

/* search loop... */
for (; start > end; start--) {
freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
if (freq_acceptable(freq, start))//当start=2时满足要求
goto found;
}
/* cannot find frequency spec */
return -EINVAL;

found:
*got = freq;//0
*iicon |= (divs-1);//0
*iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0;//tx选择fplk/16时分频值无效
return 0;
}

//硬件初始化(使能中断 使能响应 设置tx选择fplk/16)
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 = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);

s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA);
s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL);//将IO引脚配置为I2C控制引脚

writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);//将从设备地址写到IICADD

dev_info(i2c->dev, "slave address 0x%02x/n", pdata->slave_addr);

if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) {//计算I2C时钟及分频值
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);

writel(iicon, i2c->regs + S3C2410_IICCON);//以设置tx选择fplk/16

/* check for s3c2440 i2c controller */
if (s3c24xx_i2c_is2440(i2c)) {
dev_dbg(i2c->dev, "S3C2440_IICLC=%08x/n", pdata->sda_delay);
writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
}

return 0;
}

static void s3c24xx_i2c_free(struct s3c24xx_i2c *i2c)
{
if (i2c->clk != NULL && !IS_ERR(i2c->clk)) {
clk_disable(i2c->clk);
clk_put(i2c->clk);
i2c->clk = NULL;
}

if (i2c->regs != NULL) {
iounmap(i2c->regs);
i2c->regs = NULL;
}

if (i2c->ioarea != NULL) {
release_resource(i2c->ioarea);
kfree(i2c->ioarea);
i2c->ioarea = NULL;
}
}

static int s3c24xx_i2c_probe(struct platform_device *pdev)//探测函数
{
struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
struct resource *res;
int ret;

i2c->dev = &pdev->dev;
i2c->clk = clk_get(&pdev->dev, "i2c");//从clk链表中查找i2c时钟结构
if (IS_ERR(i2c->clk)) {
dev_err(&pdev->dev, "cannot get clock/n");
ret = -ENOENT;
goto out;
}

dev_dbg(&pdev->dev, "clock source %p/n", i2c->clk);
clk_enable(i2c->clk);

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);//获取寄存器资源
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IO resource/n");
ret = -ENOENT;
goto out;
}

i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
pdev->name);
if (i2c->ioarea == NULL) {
dev_err(&pdev->dev, "cannot request IO/n");
ret = -ENXIO;
goto out;
}

i2c->regs = ioremap(res->start, (res->end-res->start)+1);//映射IO内存
if (i2c->regs == NULL) {
dev_err(&pdev->dev, "cannot map IO/n");
ret = -ENXIO;
goto out;
}
dev_dbg(&pdev->dev, "registers %p (%p, %p)/n", i2c->regs, i2c->ioarea, res);

i2c->adap.algo_data = i2c;//保存封装适配器私有结构的指针
i2c->adap.dev.parent = &pdev->dev;//平台设备为其父设备

ret = s3c24xx_i2c_init(i2c);//实际即设置iiccon寄存器
if (ret != 0)
goto out;

/* find the IRQ for this unit (note, this relies on the init call to
* ensure no current IRQs pending
*/
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (res == NULL) {
dev_err(&pdev->dev, "cannot find IRQ/n");
ret = -ENOENT;
goto out;
}

ret = request_irq(res->start, s3c24xx_i2c_irq, SA_INTERRUPT,
pdev->name, i2c);//申请中断函数
if (ret != 0) {
dev_err(&pdev->dev, "cannot claim IRQ/n");
goto out;
}

i2c->irq = res;
dev_dbg(&pdev->dev, "irq resource %p (%ld)/n", res, res->start);

ret = i2c_add_adapter(&i2c->adap);//注册适配器
if (ret < 0) {
dev_err(&pdev->dev, "failed to add bus to i2c core/n");
goto out;
}

platform_set_drvdata(pdev, i2c);//pdev.dev.platform_data = i2c

dev_info(&pdev->dev, "%s: S3C I2C adapter/n", i2c->adap.dev.bus_id);
out:
if (ret < 0)
s3c24xx_i2c_free(i2c);

return ret;
}

static int s3c24xx_i2c_remove(struct platform_device *pdev)
{
struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);

if (i2c != NULL) {
s3c24xx_i2c_free(i2c);
platform_set_drvdata(pdev, NULL);
}

return 0;
}

#ifdef CONFIG_PM
static int s3c24xx_i2c_resume(struct platform_device *dev)
{
struct s3c24xx_i2c *i2c = platform_get_drvdata(dev);

if (i2c != NULL)
s3c24xx_i2c_init(i2c);

return 0;
}

#else
#define s3c24xx_i2c_resume NULL
#endif

static struct platform_driver s3c2410_i2c_driver = {//2410平台驱动结构
.probe = s3c24xx_i2c_probe,
.remove = s3c24xx_i2c_remove,
.resume = s3c24xx_i2c_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2410-i2c",
},
};

static struct platform_driver s3c2440_i2c_driver = {
.probe = s3c24xx_i2c_probe,
.remove = s3c24xx_i2c_remove,
.resume = s3c24xx_i2c_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2440-i2c",
},
};

static int __init i2c_adap_s3c_init(void)//IIC适配器作为平台设备
{
int ret;

ret = platform_driver_register(&s3c2410_i2c_driver);
if (ret == 0) {
ret = platform_driver_register(&s3c2440_i2c_driver);
if (ret)
platform_driver_unregister(&s3c2410_i2c_driver);
}

return ret;
}

static void __exit i2c_adap_s3c_exit(void)//注销平台设备
{
platform_driver_unregister(&s3c2410_i2c_driver);
platform_driver_unregister(&s3c2440_i2c_driver);
}

module_init(i2c_adap_s3c_init);
module_exit(i2c_adap_s3c_exit);

MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
MODULE_LICENSE("GPL");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics