linux核心I2C子系統學習(三)

來源:互聯網
上載者:User
寫裝置驅動:四部曲:
  1. 構建i2c_driver
  2. 註冊i2c_driver
  3. 構建i2c_client ( 第一種方法:註冊字元裝置驅動、第二種方法:通過板檔案的i2c_board_info填充,然後註冊)
  4.  登出i2c_driver

具體如下:

●    構建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);執行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_client 資訊通常在BSP的板檔案中通過i2c_board_info 填充,如: 定義一個I2C裝置ID為“ad7142_joystick”、地址為0x2C、中斷號為IRQ_PF5的i2c_client   static struct i2c_board_info __initdata xxx_i2c_board_info[] = {     {         I2C_BOARD_INFO(“ad7142_joystick”,0x2C),         .irq = IRQ_PF5,
    },
......... }; 然後註冊 i2c_register_board_info(1, i2c_devs1, ARRAY_SIZE(i2c_devs1)); 通過這個就完成了i2c_client 的註冊 

●    登出i2c_driver

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

  

順序調用核心中註冊的適配器來斷開我們註冊過的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;        }

  

其實主晶片的i2c的驅動基本上都支援啦,哈哈,所以剩下的工作量不是很大,只需完成從晶片的i2c的驅動操作就ok啦,那個只是分析如何編寫的便於深入理解。

另外:

 

幾個重要的結構體:i2c_msg(設定裝置地址的)、i2c_client(從機裝置的地址,一般採用平台裝置的形式,用probe函數探測)、i2c_driver自己構建

 

幾個重要的方法:i2c_add_driver添加裝置、i2c_transfer用於進行I2C適配器和I2C裝置之間的一組訊息的互動 I2C與SCCB協議區別:從機地址因為I2C是7位地址,最後一位是讀寫位,而SCCB是8位地址,比如ov9650,他是SCCB協議,他的地址是0x60,那麼如果掛接到I2C匯流排上,他的地址就變成0x30了,這樣算的:SCCB地址:::  0x60:   0 1 1 0_0 0 0  0     這個0還是地址位I2C地址::::                 0 1 1 0_0 0 0  0最後紅色的0是讀寫位,那麼地址變成了7 位 +讀寫位 即 0 1 1_ 0 0 0 0 + 0( 讀寫位 )  所以從機地址變成了0x30     linux核心I2C驅動基本上就這些了!

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.