Linux DM6441下I2C設備驅動的開發
和常用的Linux I2C的驅動開發一樣,主要包括總線Bus驅動,以及相應的設備驅動開發。
總線驅動和CPU的關系較為密切,涉及到相關硬件的操作。
本文以Linux2.6.1和Dm6441簡單解析一下整個驅動的編寫架構。
1. 總線驅動
涉及到總線驅動的相關代碼在drivers/i2c/bussses下面,該目錄下是各CPU的I2C模塊涉及到的內容,這裡我們選擇i2c-davinci.c文件
總線驅動中Init函數i2c_davinci_init如下:
- static int __init i2c_davinci_init(void)
- {
- int status;
- struct device *dev = NULL;
-
- DEB0("%s %s", __TIME__, __DATE__);
-
- DEB1("i2c_davinci_init()");
-
- #if 0
- if (i2c_davinci_busFreq > 200)
- i2c_davinci_busFreq = 400; /*Fast mode */
- else
- i2c_davinci_busFreq = 100; /*Standard mode */
- #endif
-
- i2c_clock = clk_get (dev, "I2CCLK"); //獲取I2C輸入時鐘批頻率27MHz
-
- if (IS_ERR(i2c_clock))
- return -1;
-
- clk_use (i2c_clock);
- clk_enable (i2c_clock);
- i2c_davinci_inputClock = clk_get_rate (i2c_clock); //獲取I2C輸入時鐘批頻率27MHz
-
- DEB1 ("IP CLOCK = %ld", i2c_davinci_inputClock);
-
- memset(&i2c_davinci_dev, 0, sizeof(i2c_davinci_dev));
- init_waitqueue_head(&i2c_davinci_dev.cmd_wait);
-
- i2c_davinci_dev.regs = (davinci_i2cregsovly)I2C_BASE; //I2C控制器相關寄存器所在地址
-
- status = (int)request_region(I2C_BASE, I2C_IOSIZE, MODULE_NAME); //申請一塊IO輸入輸出區域#define MODULE_NAME "DaVinci I2C"
- if (!status) {
- i2c_err("I2C is already in use\n");
- return -ENODEV;
- }
-
- status = request_irq(IRQ_I2C, i2c_davinci_isr, 0, "i2c",
- &i2c_davinci_dev); //申請I2C中斷號IRQ_I2C=39
- if (status) {
- i2c_err("failed to request I2C IRQ");
- goto do_release_region;
- }
-
- i2c_set_adapdata(&i2c_davinci_adap, &i2c_davinci_dev); //adap設備的驅動程序私有數據i2c_davinci_adap->dev->driver_data =i2c_davinci_dev
- status = i2c_add_adapter(&i2c_davinci_adap); //I2C上添加adapter
- if (status) {
- i2c_err("failed to add adapter");
- goto do_free_irq;
- }
-
- i2c_davinci_reset(&i2c_davinci_dev); //完成I2C相關寄存器的初始化
-
- if (driver_register(&davinci_i2c_driver) != 0) // I2C驅動注冊
- printk(KERN_ERR "Driver register failed for davinci_i2c\n");
- if (platform_device_register(&davinci_i2c_device) != 0) {
- printk(KERN_ERR "Device register failed for i2c\n");
- driver_unregister(&davinci_i2c_driver);
- }
-
- return 0;
-
- do_free_irq:
- free_irq(IRQ_I2C, &i2c_davinci_dev);
- do_release_region:
- release_region(I2C_BASE, I2C_IOSIZE);
-
- return status;
- }
操作主要圍繞以下4個結構體展開:
- static struct i2c_algorithm i2c_davinci_algo = { //主要涉及相關的I2C通信協議以及數據的傳輸和接受
- .name = "DAVINCI I2C algorithm",
- .id = I2C_ALGO_EXP,
- .master_xfer = i2c_davinci_xfer,
- .smbus_xfer = NULL,
- .slave_send = NULL,
- .slave_recv = NULL,
- .algo_control = NULL,
- .functionality = i2c_davinci_func,
- };
-
- static struct i2c_adapter i2c_davinci_adap = { //對I2C通信協議的封裝,定義的適配器實例
- .owner = THIS_MODULE,
- .name = "DAVINCI I2C adapter",
- .id = I2C_ALGO_EXP,
- .algo = &i2c_davinci_algo,
- .algo_data = NULL,
- .client_register = NULL,
- .client_unregister = NULL,
- };
-
- static struct device_driver davinci_i2c_driver = { //達芬奇的設備驅動
- .name = "davinci_i2c",
- .bus = &platform_bus_type,
- .remove = davinci_i2c_remove,
- };
-
- static struct platform_device davinci_i2c_device = { //定義的達芬奇平台設備
- .name = "i2c",
- .id = -1,
- .dev = {
- .driver = &davinci_i2c_driver,
- .release = davinci_i2c_device_release,
- },
- };
結合以上兩段代碼,可以知道以下內容
1 .在模塊初始化函數中完成適配器與設備。的連接了設備驅動davinci_i2c_driver和平台設備davinci_i2c_device的注冊
2.獲取I2C相關端口的申請,I2C相應寄存器的物理首地址0x01C21000,完成了I2C模塊初始化操作以及IRQ_I2C=39的中斷申請。
在這裡對I2C模塊的初始化進行解析,內容如下:
- static int i2c_davinci_reset(struct i2c_davinci_device *dev)
- {
- u16 psc;
- u32 clk;
-
- DEB1("i2c: reset called");
-
- /* put I2C into reset */
- dev->regs->icmdr &= ~DAVINCI_I2C_ICMDR_IRS_MASK; //首先需復位I2C模塊
-
- /* NOTE: I2C Clock divider programming info
- * As per I2C specs the following formulas provide prescalar
- * and low/high divider values
- *
- * input clk --> PSC Div -----------> ICCL/H Div --> output clock
- * module clk
- *
- * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
- *
- * Thus,
- * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
- *
- * where if PSC == 0, d = 7,
- * if PSC == 1, d = 6
- * if PSC > 1 , d = 5
- */
-
- psc = 26; /* To get 1MHz clock */
-
- clk = ((i2c_davinci_inputClock/(psc + 1)) / (i2c_davinci_busFreq * 1000)) - 10; //i2c_davinci_busFreq=20
-
- dev->regs->icpsc = psc; //PSC寄存器寫26,即預分頻數27
- dev->regs->icclkh = (50 * clk) / 100; /* duty cycle should be 50% */
- dev->regs->icclkl = (clk - dev->regs->icclkh); //高低電平配置內容
-
- DEB1("CLK = %d", clk);
- DEB1("PSC = %d", dev->regs->icpsc);
- DEB1("CLKL = %d", dev->regs->icclkl);
- DEB1("CLKH = %d", dev->regs->icclkh);
-
- /* Set Own Address: */
- dev->regs->icoar = i2c_davinci_own_addr; //oxa
-
- /* Enable interrupts */
- dev->regs->icimr = I2C_DAVINCI_INTR_ALL; //所有中斷都使能
-
- /* Take the I2C module out of reset: */
- dev->regs->icmdr |= DAVINCI_I2C_ICMDR_IRS_MASK; //使能I2C模塊
-
- return 0;
- }
主要完成Dm6441上I2C的總線時鐘的配置。以及做一些中斷使能的初始化。在這裡都是對dev->regs這個結構體指針的內容
該結構體的實際內容如下:
- typedef struct {
- u16 icoar;
- u8 rsvd0[2];
- u16 icimr;
- u8 rsvd1[2];
- u16 icstr;
- u8 rsvd2[2];
- u16 icclkl;
- u8 rsvd3[2];
- u16 icclkh;
- u8 rsvd4[2];
- u16 iccnt;
- u8 rsvd5[2];
- u16 icdrr;
- u8 rsvd6[2];
- u16 icsar;
- u8 rsvd7[2];
- u16 icdxr;
- u8 rsvd8[2];
- u16 icmdr;
- u8 rsvd9[2];
- u16 icivr;
- u8 rsvd10[2];
- u16 icemdr;
- u8 rsvd11[2];
- u16 icpsc;
- u8 rsvd12[2];
- u16 icpid1;
- u8 rsvd13[2];
- u16 icpid2;
- u8 rsvd14[14];
- u32 ipcfunc;
- u32 icpdir;
- u32 icpdin;
- u32 icpdout;
- u32 icpdset;
- u32 icpdclr;
- } davinci_i2cregs;
-
- /**************************************************************************\
- * Overlay structure typedef definition
- \**************************************************************************/
- typedef volatile davinci_i2cregs *davinci_i2cregsovly;
涉及的內容是DM6441 I2C模塊的相關寄存器。在獲取寄存器的物理首地址之後,就可以直接對這個結構體實例的內容寫數值,該結構體被包含在以下結構體中
- struct i2c_davinci_device {
- int cmd_complete, cmd_err;
- wait_queue_head_t cmd_wait;
- u8 *buf;
- size_t buf_len;
- davinci_i2cregsovly regs;
- };
這裡的另一塊內容是添加適配器到I2C驅動中使用i2c_add_adapter函數完成i2c_davinci_adap(該結構體實例內容見上述代碼)
- int i2c_add_adapter(struct i2c_adapter *adap)
- {
- int id, res = 0;
- struct list_head *item;
- struct i2c_driver *driver;
-
- down(&core_lists);
-
- if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
- res = -ENOMEM;
- goto out_unlock;
- }
-
- res = idr_get_new(&i2c_adapter_idr, NULL, &id);
- if (res < 0) {
- if (res == -EAGAIN)
- res = -ENOMEM;
- goto out_unlock;
- }
-
- adap->nr = id & MAX_ID_MASK;
- init_MUTEX(&adap->bus_lock);
- init_MUTEX(&adap->clist_lock);
- list_add_tail(&adap->list,&adapters);
- INIT_LIST_HEAD(&adap->clients);
-
- /* Add the adapter to the driver core.
- * If the parent pointer is not set up,
- * we add this adapter to the host bus.
- */
- if (adap->dev.parent == NULL)
- adap->dev.parent = &platform_bus;
- sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
- adap->dev.driver = &i2c_adapter_driver; //適配器的設備驅動指向i2c_adapter_driver
- adap->dev.release = &i2c_adapter_dev_release;
- device_register(&adap->dev); //適配器設備注冊
- device_create_file(&adap->dev, &dev_attr_name);
-
- /* Add this adapter to the i2c_adapter class */
- memset(&adap->class_dev, 0x00, sizeof(struct class_device));
- adap->class_dev.dev = &adap->dev;
- adap->class_dev.class = &i2c_adapter_class;
- strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
- class_device_register(&adap->class_dev);
-
- /* inform drivers of new adapters */
- list_for_each(item,&drivers) {
- driver = list_entry(item, struct i2c_driver, list);
- if (driver->flags & I2C_DF_NOTIFY)
- /* We ignore the return code; if it fails, too bad */
- driver->attach_adapter(adap);
- }
-
- dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
-
- out_unlock:
- up(&core_lists);
- return res;
- }
在該函數內主要完成適配器結構體中成員變量設備dev的初始化,緊接著完成適配器這個抽象設備的注冊,再完成適配器設備類的注冊以及相關設備節點的生成。