OFN鼠標驅動(六) -- drivers\i2c\busses\i2c-s3c2410.c的分析

 

之前我們分析的都是I2C的一些結構問題,現在我們來看看I2C的具體物理實現。

drivers\i2c\busses裏面有很多文件,每一個文件都對應着一個平臺的I2C物理實現函數,因爲我們的平臺是S3C2440,所以我們只需要看i2c_s3c2410.c文件就可以了,其他的文件的實現方法應該都是類似的。

 

分析完這個文件的代碼之後,我們對I2C驅動的設備模型理解又更深了一層。之前我們有個錯誤的認識,認爲一條I2C總線上可以有多個適配器,其實一條I2C總線上只能有一個適配器,而之前一直苦尋不到的適配器的添加操作,則是在這個文件中實現的,也就是在I2C總線探測的時候添加該總線的適配器,同時關聯上的algo算法,這樣,就又解決了前面留下的兩個問題

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C的狀態

enum      s3c24xx_i2c_state {

       STATE_IDLE,                     //空閒

       STATE_START,            //開始位

       STATE_READ,             //讀

       STATE_WRITE,           //寫

       STATE_STOP               //停止位

};

 

//定義I2C的一個結構體

struct s3c24xx_i2c {

       spinlock_t                     lock;                     //自旋鎖

       wait_queue_head_t        wait;                     //等待隊列

 

       struct i2c_msg               *msg;             //消息包(I2C內核代碼中經常看到這個結構)

       unsigned int                  msg_num;      //消息包數量

       unsigned int                  msg_idx;        //當前操作的是第幾個包

       unsigned int                  msg_ptr;         //當前操作的msg包中的字節地址

 

       unsigned int                  tx_setup;        //數據發送前的建立時間

 

       enum s3c24xx_i2c_state        state;       //I2C狀態

 

       void __iomem               *regs;             //ARM 寄存器指針(首地址)

       struct clk                      *clk;

       struct device                 *dev;

       struct resource               *irq;

       struct resource               *ioarea;

       struct i2c_adapter          adap;              //適配器

};

 

//設置一個默認的I2C平臺數據結構

static struct    s3c2410_platform_i2c          s3c24xx_i2c_default_platform = {

       .flags                    = 0,

       .slave_addr            = 0x10,                 //控制器從機地址

       .bus_freq               = 100*1000,          //標準的總線頻率

       .max_freq              = 400*1000,          //最大的總線頻率(400K)

       .sda_delay             = S3C2410_IICLC_SDA_DELAY5 | S3C2410_IICLC_FILTER_ON,

       //SDA線上的過濾操作,去幹擾用

};

 

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//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");

}

 

//獲取平臺的I2C結構體,如果dev有掛載就返回dev掛載的數據,否則就返回默認的數據

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;

 

       return     &s3c24xx_i2c_default_platform;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//發送STOP位後調用的函數

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;               //msg包

       i2c->msg_idx ++;                 //當前操作的包的索引

       i2c->msg_num = 0;                     //msg包的數量

       if (ret)

              i2c->msg_idx = ret;             

 

       wake_up(&i2c->wait);          //喚醒等待隊列

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//不使用ACK

//實現方法是將IICCON寄存器的bit7寫0

static inline void    s3c24xx_i2c_disable_ack(struct s3c24xx_i2c      *i2c)

{

       unsigned long        tmp;

      

       tmp = readl(i2c->regs + S3C2410_IICCON);             //讀寄存器IICCON

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

}

 

//使用ACK

//實現方法是將IICCON寄存器的bit7寫1

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);

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//IICCON.5負責控制RX/TX是否產生中斷

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);

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//發送MSG包的第一個字節(設備地址)

static void      s3c24xx_i2c_message_start(

       struct s3c24xx_i2c        *i2c,                                 

       struct i2c_msg              *msg)

{

       unsigned int addr = (msg->addr & 0x7f) << 1;    //取地址

       unsigned long        stat;        //這個是準備設置CPU的IICSTAT寄存器的

       unsigned long        iiccon;

 

       stat = 0;

       stat |=  S3C2410_IICSTAT_TXRXEN;              //I2C的RXTX有效

 

       if (msg->flags & I2C_M_RD) {                         //主機讀操作

              stat |= S3C2410_IICSTAT_MASTER_RX;  //主機接收

              addr |= 1;              //地址最低位爲0

       } else                                                              //主機寫操作

              stat |= S3C2410_IICSTAT_MASTER_TX;   //主機發送

 

       //這個標記沒有看到定義

       if (msg->flags & I2C_M_REV_DIR_ADDR)

              addr ^= 1;

 

       //打開ACK硬件功能

       s3c24xx_i2c_enable_ack(i2c);

 

       //讀IICCON寄存器

       iiccon = readl(i2c->regs + S3C2410_IICCON);

       //寫IICSTAT寄存器

       writel(stat, i2c->regs + S3C2410_IICSTAT);             

      

       dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);

       //將地址作爲數據寫入(I2C第一字節爲發送設備地址)

       writeb(addr, i2c->regs + S3C2410_IICDS);

      

       //短延遲,保證剛纔的數據寫入到寄存器中

       //也就是設置時序的建立時間

       ndelay(i2c->tx_setup);

 

       dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);

       writel(iiccon, i2c->regs + S3C2410_IICCON);

      

       //這位寫1,表示開始,發送IICDS中的數據

       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");

 

       //該位寫0,停止信號(讀是BUSY)

       iicstat     &= ~ S3C2410_IICSTAT_START;

       writel(iicstat, i2c->regs + S3C2410_IICSTAT);

      

       i2c->state = STATE_STOP;                        //狀態機爲停

      

       s3c24xx_i2c_master_complete(i2c, ret);       //主機I2C動作完成

       s3c24xx_i2c_disable_irq(i2c);                     //禁止I2C中斷

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//返回1 – 當前操作的包是最後一個包

static inline int      is_lastmsg(struct s3c24xx_i2c *i2c)

{

       return     i2c->msg_idx >= (i2c->msg_num - 1);

}

 

//返回1 – 當前操作的字節是MSG包的最後一個字節

static inline int      is_msglast(struct s3c24xx_i2c *i2c)

{

       return     i2c->msg_ptr == i2c->msg->len-1;

}

 

//返回1 – 當前msg包的數據指針已經到包尾

static inline int      is_msgend(struct s3c24xx_i2c *i2c)

{

       return     i2c->msg_ptr >= i2c->msg->len;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C的中斷函數,這個函數在執行s3c24xx_i2c_irq函數時用request_irq註冊的

//irqno – 請求號

//dev_id – 設備ID

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);          //讀狀態

 

       //總線仲裁位(0-成功, 1-失敗)

       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;

       }

      

       //繼續下一字節的中斷

       i2s_s3c_irq_nextbyte(i2c, status);

 

 out:

       return     IRQ_HANDLED;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C中斷,根據狀態機判斷下一個操作

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);              //禁止I2C中斷    

              goto       out_ack;

 

       case STATE_START:             //I2C開始狀態

              if (iicstat  & S3C2410_IICSTAT_LASTBIT      //I2C最後收到的位是1(NO ACK)

                     &&  !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {//消息包可以忽略ACK

                     dev_dbg(i2c->dev, "ack was not received\n");

                     s3c24xx_i2c_stop(i2c, -EREMOTEIO);              //發停止位

                     goto       out_ack;

              }

 

              //運行到這裏,說明收到了ACK

              //判斷開始位之後的操作是讀還是寫?

              if (i2c->msg->flags & I2C_M_RD)

                     i2c->state = STATE_READ;                       //讀狀態

              else

                     i2c->state = STATE_WRITE;                      //寫狀態

      

              //當前是最後一個包 且 包的長度爲0

              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:                   //I2C寫狀態

       retry_write:

              if (!is_msgend(i2c)) {           //不是msg包的最後一個字節

                     byte = i2c->msg->buf[i2c->msg_ptr++];     //讀出要寫的字節

                     writeb(byte, i2c->regs + S3C2410_IICDS); //寫入IICDS

                     ndelay(i2c->tx_setup);   //等待建立時間

              } else if (!is_lastmsg(i2c)) {  //不是最後一個msg包

                     dev_dbg(i2c->dev, "WRITE: Next Message\n");

 

                     i2c->msg_ptr = 0;         //當前操作的字節爲首地址

                     i2c->msg_idx ++;          //當前操作的msg包爲下一個

                     i2c->msg++;                 //到下一個msg包

                    

                     if (i2c->msg->flags & I2C_M_NOSTART) {       //本msg包不需要發開始位

                            if (i2c->msg->flags & I2C_M_RD) {          //本包是讀操作

                                   s3c24xx_i2c_stop(i2c, -EINVAL);        //發停止位

                            }

                            goto       retry_write;                                //寫操作,連續寫數據

                     } else {                                                           //本msg包需要發開始位

                            s3c24xx_i2c_message_start(i2c, i2c->msg);  //發開始位

                            i2c->state = STATE_START;                       //修改狀態爲開始

                     }

              } else {                                      //最後一個msg包的最後一個字節

                     s3c24xx_i2c_stop(i2c, 0);      //發停止位

              }

              break;

 

       case STATE_READ:                    //I2C讀操作

       //I2C讀操作的最後一個字節是可以收NO ACK的,不明白的話就先看一下IIC協議

              if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)      //不能忽略ACK

                     && !(is_msglast(i2c) && is_lastmsg(i2c))) { //不是最後一個包的最後一個字節

                     if (iicstat & S3C2410_IICSTAT_LASTBIT) {      //I2C接收到的最後一位爲1

                            dev_dbg(i2c->dev, "READ: No Ack\n");

 

                            s3c24xx_i2c_stop(i2c, -ECONNREFUSED);      //沒有收到ACK,發停止位

                            goto       out_ack;

                     }

              }

 

              byte = readb(i2c->regs + S3C2410_IICDS);        //讀取接到的字節

              i2c->msg->buf[i2c->msg_ptr++] = byte;           

 

       //準備讀下一字節

       prepare_read:

              if (is_msglast(i2c)) {                                 //準備操作msg包的最後一個字節

                     if (is_lastmsg(i2c))                             //當前操作的是最後一個msg包

                            s3c24xx_i2c_disable_ack(i2c);      //禁止ACK(發NO ACK)

              } else if (is_msgend(i2c)) {                        //當前msg包的最後一個字節操作結束

                     if (is_lastmsg(i2c)) {                          //當前操作的是最後一個msg包

                            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;

       }

 

//當前狀態爲停止位發出

//沒有收到ACK

//當前發的是空包

 out_ack:

       //清除掛起條件 並 恢復操作

       tmp = readl(i2c->regs + S3C2410_IICCON);     

       tmp &= ~S3C2410_IICCON_IRQPEND;

       writel(tmp, i2c->regs + S3C2410_IICCON);

 

//空閒狀態下進中斷,直接到這裏

 out:

       return     ret;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//等待400ms看IIC總線是否空閒

//空閒返回0

static int        s3c24xx_i2c_set_master(struct s3c24xx_i2c        *i2c)

{

       unsigned long        iicstat;

       int                        timeout = 400;              //等待400ms的ACK

 

       while (timeout-- > 0) {

              iicstat = readl(i2c->regs + S3C2410_IICSTAT);   //讀狀態

              if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))

                     return 0;         //I2C空閒,讀結束

 

              msleep(1);             //睡眠1ms(讓出CPU給其他進程)

       }

 

       //超時出錯

       dev_dbg(i2c->dev, "timeout: GPEDAT is %08x\n",

              __raw_readl(S3C2410_GPEDAT));

 

       return     -ETIMEDOUT;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//將消息包發送出去,用wait_event_timeout等待全部的包發送完,或則超時

//注意:doxfer是do  xfer的意思,xfer是數據通訊

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);              //判斷400ms內IIC總線是否空閒

       if (ret != 0) {         //連續400ms IIC總線不空閒

              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;    //當前I2C狀態機

 

       s3c24xx_i2c_enable_irq(i2c);              //開I2C中斷

       s3c24xx_i2c_message_start(i2c, msgs); //發開始位

       spin_unlock_irq(&i2c->lock);       //恢復中斷

      

       //進程進入睡眠,等待隊列喚醒,返回的是剩餘時間。

       //喚醒條件:

       //     1、i2c->msg_num == 0

       //     2、HZ * 5,等待時間達到

       //     3、wake_up函數(發STOP位後調用的s3c24xx_i2c_master_complete函數中將調用wake_up)

       timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);

 

       ret = i2c->msg_idx;              //當前操作的msg包號

 

       if (timeout == 0)           //超時

              dev_dbg(i2c->dev, "timeout\n");

       else if (ret != num)        //當前要操作的包沒有操作完

              dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);

 

       //睡眠1ms(保證兩次操作至少間隔1ms)

       msleep(1);

 

 out:

       return     ret;                //返回操作了幾個包

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//嘗試調用s3c24xx_i2c_doxfer進行數據傳輸

static int        s3c24xx_i2c_xfer(

       struct i2c_adapter *adap,            //適配器

       struct i2c_msg       *msgs,

       int                        num)

{

       //從這裏可見,I2C數據是從適配器的algo_data中來的

       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);   //進行I2C通訊

              if (ret != -EAGAIN)      //正常結束,返回

                     return ret;

 

              //超時結束,等待100us再嘗試

              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;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C總線的註冊信息

//適配器關聯的algo(算法)操作

static const struct i2c_algorithm   s3c24xx_i2c_algorithm = {

       .master_xfer          = s3c24xx_i2c_xfer,

       .functionality         = s3c24xx_i2c_func,

};

 

//I2C總線的數據結構

static struct s3c24xx_i2c       s3c24xx_i2c = {

       .lock              = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),                        //中斷鎖

       .wait              = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),        //等待隊列

       .tx_setup = 50,                     //建立時間

       .adap              = {                //適配器

              .name                    = "s3c2410-i2c",                  //適配器名

              .owner                  = THIS_MODULE,

              .algo                            = &s3c24xx_i2c_algorithm,  //關聯的算法操作

              .retries                  = 2,                                    //嘗試次數

              .class                     = I2C_CLASS_HWMON,     //所屬類

       },

};

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

下面代碼是設置IIC總線頻率的,在我們看下面代碼之前,我們先看看S3C2440規格書中對計算I2C總線頻率的描述,否則下面的代碼會看得一頭霧水:

IIC總線頻率是由IICCON寄存器設置的:

Tx clock = IICCLK / (IICCON[3:0] + 1)

其中IICCLK是IICCON的bit6, 該位爲0則IICCLK = Fpclk/16,爲1爲Fpclk/512

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//計算要設置的分頻參數,返回的是clk/分頻(即計算出來的分頻後的頻率)

static int        s3c24xx_i2c_calcdivisor(

       unsigned long        clkin,            //輸入頻率

       unsigned int          wanted,          //希望得到的頻率

       unsigned int          *div1,

       unsigned int          *divs)

{

       unsigned int   calc_divs = clkin / wanted;    //計算需要的分頻

       unsigned int   calc_div1;

 

       if (calc_divs > (16*16))              

              calc_div1 = 512;                  //需要的分頻>256

       else

              calc_div1 = 16;                    //需要的分頻<=256

 

       calc_divs += calc_div1-1;

       calc_divs /= calc_div1;

 

       //由這裏可見,calc_divs是設置進IICCON[3:0]中的

       //加1是因爲計算公式中有個加1

       if (calc_divs == 0)

              calc_divs = 1;

       if (calc_divs > 17)

              calc_divs = 17;

 

       *divs = calc_divs;

       *div1 = calc_div1;

 

       return     clkin / (calc_divs * calc_div1);

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//比較實際頻率和目標頻率的誤差,+-2khz內可以接受

static inline int      freq_acceptable(unsigned int freq,       unsigned int wanted)

{

       int   diff = freq - wanted;

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

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//設置I2C總線的頻率,先設置bus_freq中心頻率,如果該頻率下沒有合適的參數,則掃描總線的最大最小頻率範圍內有無合適的參數

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);      //獲取I2C的時鐘頻率(HZ)

      

       unsigned int   divs, div1;

       int                 freq;

       int                 start, end;

 

       clkin /= 1000;        //將單位轉爲KHz

    

       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) {   

              //如果I2C總線頻率不爲0,計算頻率並比較誤差

              freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000, &div1, &divs);

              if (freq_acceptable(freq, pdata->bus_freq/1000))

                     goto found;            //找到頻率的設置參數

       }

 

       //獲取總線的最大和最小頻率

       start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;

       end = pdata->min_freq;

 

       start /= 1000;

       end /= 1000;                 //轉換單位爲KHz

 

       //掃描頻率範圍

       for (; start > end; start--) {

              freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);

              if (freq_acceptable(freq, start))

                     goto found;

       }

 

       //沒有找到合適的參數

       return -EINVAL;

 

//找到I2C頻率的參數

 found:

       *got = freq;                  //獲得的頻率

       *iicon |= (divs-1);         //divs-1爲設置進寄存器的值(參看公式)

       *iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0;

       return 0;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//初始化I2C(主要是設置IICON)

static int        s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)

{

       //IICON設置爲中斷有效,並使能ACK

       unsigned long        iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;

       struct s3c2410_platform_i2c        *pdata;           //平臺數據

       unsigned int                        freq;

 

       //獲取平臺數據(I2C總線->適配器->設備->父設備)

       //如父設備沒有平臺數據,則關聯上本文件一開始設置的默認的平臺數據

       pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);

 

       //初始化GPIO口的配置,功能爲IIC功能

       s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA);

       s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL);

 

       //寫從機地址(懷疑是CPU作爲IIC從機時的設備地址)

       writeb(pdata->slave_addr,     i2c->regs + S3C2410_IICADD);

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

 

       //根據總線頻率設置IICON寄存器

       if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) {

              dev_err(i2c->dev, "cannot meet bus frequency required\n");

              return     -EINVAL;

       }

 

       //打印信息

       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);     //實際設置到ARM的IICON寄存器中

 

       //檢查CPU類型是否是S3C2440

       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;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C總線的探測函數,這個函數裏面關聯了適配器,基本上I2C的初始化都在這個函數中

//*pdev爲平臺設備

static int        s3c24xx_i2c_probe(struct platform_device *pdev)

{

       //獲取設備數據,該數據在本文件的一開始定義

       struct s3c24xx_i2c        *i2c = &s3c24xx_i2c;

       struct resource              *res;

       int                               ret;

 

       //I2C設備關聯到平臺設備

       i2c->dev = &pdev->dev;

       i2c->clk = clk_get(&pdev->dev, "i2c");              //獲取設備的時鐘

       if (IS_ERR(i2c->clk)) {

              dev_err(&pdev->dev, "cannot get clock\n");

              ret = -ENOENT;

              goto err_noclk;

       }

 

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

       clk_enable(i2c->clk);                                 //時鐘有效

 

       //獲取平臺的IO內存資源

       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

       if (res == NULL) {        //資源爲空,出錯

              dev_err(&pdev->dev, "cannot find IO resource\n");

              ret = -ENOENT;

              goto err_clk;

       }

 

       //申請IO內存

       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 err_clk;

       }

 

       //映射IO內存

       i2c->regs = ioremap(res->start, (res->end-res->start)+1);

 

       if (i2c->regs == NULL) {            //映射失敗

              dev_err(&pdev->dev, "cannot map IO\n");

              ret = -ENXIO;

              goto err_ioarea;

       }

 

       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;    //適配器的父設備爲平臺設備

 

       //初始化I2C(IICON寄存器)

       ret = s3c24xx_i2c_init(i2c);

       if (ret != 0)

              goto       err_iomap;

 

       //獲取平臺的IRQ資源

       res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

       if (res == NULL) {

              dev_err(&pdev->dev, "cannot find IRQ\n");

              ret = -ENOENT;

              goto err_iomap;

       }

 

       //申請IRQ中斷

       ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,

                       pdev->name, i2c);

 

       if (ret != 0) {

              dev_err(&pdev->dev, "cannot claim IRQ\n");

              goto err_iomap;

       }

 

       //關聯上設備的IRQ數據

       i2c->irq = res;

       dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res,

              (unsigned long)res->start);

 

       //添加適配器

       //難怪之前分析的代碼裏都沒有看到適配器的添加,原理是在這裏添加的,由此可見,一根I2C總線上應該只有一個適配器

       ret = i2c_add_adapter(&i2c->adap);

       if (ret < 0) {

              dev_err(&pdev->dev, "failed to add bus to i2c core\n");

              goto err_irq;

       }

 

       //pdev->dev->driver_data = i2c

       //即平臺設備關聯的驅動數據爲I2C數據

       platform_set_drvdata(pdev, i2c);

 

       dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);

       return 0;

 

 err_irq:

       free_irq(i2c->irq->start, i2c);              //釋放中斷請求

 

//初始化I2C失敗

 err_iomap:

       iounmap(i2c->regs);                    //取消內存映射

 

 err_ioarea:

       release_resource(i2c->ioarea);       //釋放資源

       kfree(i2c->ioarea);

 

 err_clk:

       clk_disable(i2c->clk);                  //禁止時鐘

       clk_put(i2c->clk);                       //本時鐘的使用者減1

 

 err_noclk:

       return     ret;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//移除設備

static int        s3c24xx_i2c_remove(struct platform_device *pdev)

{

       //從平臺設備中獲取driver_data(上個函數探測的時候才關聯上I2C)

       struct s3c24xx_i2c        *i2c = platform_get_drvdata(pdev);

 

       //刪除適配器,釋放IRQ

       i2c_del_adapter(&i2c->adap);

       free_irq(i2c->irq->start, i2c);

 

       //禁止時鐘,時鐘的使用者減1

       clk_disable(i2c->clk);

       clk_put(i2c->clk);

 

       //釋放內存映射

       iounmap(i2c->regs);

 

       //釋放IO資源的關聯

       //釋放IO資源佔用的內存

       release_resource(i2c->ioarea);

       kfree(i2c->ioarea);

 

       return 0;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//I2C設備的恢復

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;

}

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//平臺設備的驅動,關聯上的一些操作函數,看數據的結構,其實也是遵循了字符型驅動設備的結構了。

static struct platform_driver   s3c2410_i2c_driver = {

       .probe            = s3c24xx_i2c_probe,

       .remove          = s3c24xx_i2c_remove,

       .resume          = s3c24xx_i2c_resume,

       .driver            = {

              .owner    = THIS_MODULE,

              .name      = "s3c2410-i2c",

       },

};

 

//同上,只是.name換了一下

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",

       },

};

 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

//代碼在init段,也就是啓動時會自動執行代碼

static int __init      i2c_adap_s3c_init(void)

{

       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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章