• <code id="zjelh"></code>

          <source id="zjelh"><form id="zjelh"></form></source><acronym id="zjelh"><form id="zjelh"><blockquote id="zjelh"></blockquote></form></acronym>

          1. <acronym id="zjelh"><form id="zjelh"></form></acronym>

            <input id="zjelh"><rt id="zjelh"></rt></input>
            <var id="zjelh"><rt id="zjelh"></rt></var>
            <source id="zjelh"><form id="zjelh"><del id="zjelh"></del></form></source>
            當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 >
            實例解析linux內核I2C體系結構(2)
            時間:2018-08-16作者:華清遠見

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

            《實例解析linux內核I2C體系結構(1)》一文介紹了利用/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方式下相同。


            發表評論

            全國咨詢電話:400-611-6270,雙休日及節假日請致電值班手機:15010390966

            在線咨詢: 曹老師QQ(3337544669), 徐老師QQ(1462495461), 劉老師 QQ(3108687497)

            企業培訓洽談專線:010-82600901,院校合作洽談專線:010-82600350,在線咨詢:QQ(248856300)

            Copyright 2004-2018 華清遠見教育集團 版權所有 ,京ICP備16055225號,京公海網安備11010802025203號

            yy4480影院