四、在內核裏寫i2c設備驅動的兩種方式


前文介紹了利用/dev/i2c-0在應用層完成對i2c設備的操作,但很多時候我們還是習慣为i2c設備在內核層編寫驅動程序。目前內核支持兩種編寫i2c驅動程序的方式。下面分別介紹這兩種方式的實現。這裏分別稱這兩種方式为“Adapter方式(LEGACY)”和“Probe方式(new style)”。


(1) Adapter方式(LEGACY)


(下面的實例代碼是在2.6.27內核的pca953x.c基礎上修改的,原始代碼采用的是本文將要討論的第2種方式,即Probe方式)


●    構建i2c_driver


static struct i2c_driver pca953x_driver = {
                .driver = {
                                    .name= "pca953x", //名稱
                                },
                .id= ID_PCA9555,//id號
                .attach_adapter= pca953x_attach_adapter, //調用适配器連接設備
                .detach_client= pca953x_detach_client,//讓設備脫離适配器
        };


●    注冊i2c_driver


static int __init pca953x_init(void)
        {
                return i2c_add_driver(&pca953x_driver);
        }
        module_init(pca953x_init);


●    attach_adapter動作


執行i2c_add_driver(&pca953x_driver)後會,如果內核中已經注冊了i2c适配器,則順序調用這些适配器來連接我們的i2c設備。此過程是通過調用i2c_driver中的attach_adapter方法完成的。具體實現形式如下:


static int pca953x_attach_adapter(struct i2c_adapter *adapter)
        {
                return i2c_probe(adapter, &addr_data, pca953x_detect);
                /*
                adapter:适配器
                addr_data:地址信息
                pca953x_detect:探測到設備後調用的函數
                */
        }


地址信息addr_data是由下面代碼指定的。
        /* Addresses to scan */
        static unsigned short normal_i2c[] = {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,I2C_CLIENT_END};
        I2C_CLIENT_INSMOD;


注意:normal_i2c裏的地址必須是你i2c芯片的地址。否則將無法正確探測到設備。而I2C_ CLIENT_INSMOD是一個宏,它會利用normal_i2c構建addr_data。


●    構建i2c_client,並注冊字符設備驅動


i2c_probe在探測到目標設備後,後調用pca953x_detect,並把當時的探測地址address作为参數傳入。


static int pca953x_detect(struct i2c_adapter *adapter, int address, int kind)
        {
                struct i2c_client *new_client;
                struct pca953x_chip *chip; //設備結構體
                int err = 0,result;
                dev_t pca953x_dev=MKDEV(pca953x_major,0);//構建設備號,根據具體情況設定,這裏我只考慮了normal_i2c中只有一個地址匹配的情況。
                if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA| I2C_FUNC_SMBUS_WORD_DATA))//判定适配器能力
                goto exit;
                if (!(chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL))) {
                        err = -ENOMEM;
                        goto exit;
                }
                /****構建i2c-client****/
                chip->client=kzalloc(sizeof(struct i2c_client),GFP_KERNEL);
                new_client = chip->client;
                i2c_set_clientdata(new_client, chip);
                new_client->addr = address;
                new_client->adapter = adapter;
                new_client->driver = &pca953x_driver;
                new_client->flags = 0;
                strlcpy(new_client->name, "pca953x", I2C_NAME_SIZE);
                if ((err = i2c_attach_client(new_client)))//注冊i2c_client
                goto exit_kfree;
                if (err)
                goto exit_detach;
                if(pca953x_major)
                {
                        result=register_chrdev_region(pca953x_dev,1,"pca953x");
                }
                else{
                        result=alloc_chrdev_region(&pca953x_dev,0,1,"pca953x");
                        pca953x_major=MAJOR(pca953x_dev);
                }
                if (result < 0) {
                        printk(KERN_NOTICE "Unable to get pca953x region, error %d\n", result);
                        return result;
                }
                pca953x_setup_cdev(chip,0); //注冊字符設備,此處不詳解
                return 0;
                exit_detach:
                i2c_detach_client(new_client);
        exit_kfree:
                kfree(chip);
        exit:
                return err;
        }


i2c_check_functionality用來判定設配器的能力,這一點非常重要。你也可以直接查看對應設配器的能力,如


static const struct i2c_algorithm smbus_algorithm = {
                .smbus_xfer= i801_access,
                .functionality= i801_func,
        };
        static u32 i801_func(struct i2c_adapter *adapter)
        {
                        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
                    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
                I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
                                | (isich4 ? I2C_FUNC_SMBUS_HWPEC_CALC : 0);
        }


●    字符驅動的具體實現


struct file_operations pca953x_fops = {
                .owner = THIS_MODULE,
                .ioctl= pca953x_ioctl,
                .open= pca953x_open,
                .release =pca953x_release,
        };


字符設備驅動本身沒有什麼好說的,這裏主要想說一下,如何在驅動中調用i2c設配器幫我們完成數據傳輸。


目前設配器主要支持兩種傳輸方法:smbus_xfer和master_xfer。一般來說,如果設配器支持了master_xfer那麼它也可以模擬支持smbus的傳輸。但如果只實現smbus_xfer,則不支持一些i2c的傳輸。


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


master_xfer中的参數設置,和前面的用戶空間編程一致。現在只是要在驅動中構建相關的参數然後調用i2c_transfer來完成傳輸既可。


int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)


smbus_xfer中的参數設置及調用方法如下:


static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
        {
                int ret;
                ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
                if (ret < 0) {
                                dev_err(&chip->client->dev, "failed writing register\n");
                                        return -EIO;
                                }
                return 0;
        }


上面函數完成向芯片的地址为reg的寄存器寫一個16bit的數據。i2c_smbus_write_word_data的實現如下:


s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
        {
                union i2c_smbus_data data;
                data.word = value;
                return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
                                                                        I2C_SMBUS_WRITE,command,
                                                                        I2C_SMBUS_WORD_DATA,&data);
        }


從中可以看出smbus傳輸一個16位數據的方法。其它操作如:字符寫、字符讀、字讀、塊操作等,可以参考內核的i2c-core.c中提供的方法。


●    注銷i2c_driver


static void __exit pca953x_exit(void)
        {
                i2c_del_driver(&pca953x_driver);
        }
        module_exit(pca953x_exit);


●    detach_client動作


順序調用內核中注冊的适配器來斷開我們注冊過的i2c設備。此過程通過調用i2c_driver中的attach_adapter方法完成的。具體實現形式如下:


static int pca953x_detach_client(struct i2c_client *client)
        {
                int err;
                struct pca953x_chip *data;
                if ((err = i2c_detach_client(client)))//斷開i2c_client
                return err;
                data=i2c_get_clientdata(client);
                cdev_del(&(data->cdev));
                unregister_chrdev_region(MKDEV(pca953x_major, 0), 1);
                kfree(data->client);
                kfree(data);
                return 0;
        }


(2) Probe方式(new style)


●    構建i2c_driver


和LEGACY方式一样,也需要構建i2c_driver,但是內容有所不同。


static struct i2c_driver pca953x_driver = {
                .driver = {
                        .name= "pca953x",
                        },
                        .probe= pca953x_probe, //當有i2c_client和i2c_driver匹配時調用
                        .remove= pca953x_remove,//注銷時調用
                        .id_table= pca953x_id,//匹配規則
        };


●    注冊i2c_driver


static int __init pca953x_init(void)
        {
                return i2c_add_driver(&pca953x_driver);
        }
        module_init(pca953x_init);


在注冊i2c_driver的過程中,是將driver注冊到了i2c_bus_type的總線上。此總線的匹配規則是:


static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
                                                                                                const struct i2c_client *client)
        {
                while (id->name[0]) {
                        if (strcmp(client->name, id->name) == 0)
                                return id;
                        id++;
                }
                return NULL;
        }


可以看出是利用i2c_client的名稱和id_table中的名稱做匹配的。本驅動中的id_table为


static const struct i2c_device_id pca953x_id[] = {
                { "pca9534", 8, },
                { "pca9535", 16, },
                { "pca9536", 4, },
                { "pca9537", 4, },
                { "pca9538", 8, },
                { "pca9539", 16, },
                { "pca9554", 8, },
                { "pca9555", 16, },
                { "pca9557", 8, },
                { "max7310", 8, },
                { }
        };


看到現在我們應該會有這样的疑問,在Adapter模式中,i2c_client是我們自己構造出來的,而現在的i2c_client是從哪來的呢?看看下面的解釋


●    注冊i2c_board_info


對於Probe模式,通常在平台代碼中要完成i2c_board_info的注冊。方法如下:


static struct i2c_board_info __initdata test_i2c_devices[] = {
                {
                        I2C_BOARD_INFO("pca9555", 0x27),//pca9555为芯片名稱,0x27为芯片地址
                        .platform_data = &pca9555_data,
                }, {
                        I2C_BOARD_INFO("mt9v022", 0x48),
                        .platform_data = &iclink[0], /* With extender */
                }, {
                        I2C_BOARD_INFO("mt9m001", 0x5d),
                        .platform_data = &iclink[0], /* With extender */
                },
        };
        i2c_register_board_info(0, test_i2c_devices,ARRAY_SIZE(test_i2c_devices)); //注冊


i2c_client就是在注冊過程中構建的。但有一點需要注意的是i2c_register_board_info並沒有EXPORT_SYMBOL给模塊使用。


●    字符驅動注冊


在Probe方式下,添加字符驅動的位置在pca953x_probe中。


static int __devinit pca953x_probe(struct i2c_client *client,const struct i2c_device_id *id)
        {
                        ……
                        /****字符設備驅動注冊位置****/
                        ……
                        return 0;
        }


●    注銷i2c_driver


static void __exit pca953x_exit(void)
        {
                i2c_del_driver(&pca953x_driver);
        }
        module_exit(pca953x_exit);


●    注銷字符設備驅動


在Probe方式下,注銷字符驅動的位置在pca953x_remove中。


static int __devinit pca953x_remove (struct i2c_client *client)
        {
                ……
                /****字符設備驅動注銷的位置****/
                ……
                return 0;
        }


●    I2C設備的數據交互方法(即:調用适配器操作設備的方法)和Adapter方式下相同。



本文摘自:http://blog.csdn.net/hongtao_liu/archive/2010/01/27/5260739.aspx
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 立你斯 的頭像
    立你斯

    立你斯學習記錄

    立你斯 發表在 痞客邦 留言(0) 人氣()