内核中编写i2c设备驱动

二、内核中编写i2c设备驱动

      内核编写i2c设备驱动支持两种方式:Adapter方式(LEGACY)和Probe方式(new style)。

1.legacy方式

       此方法驱动需要自己创建i2c_client,并且要知道芯片的地址,在内核目录documentation/i2c/upgrading-clients中有一个例程。

i2c_driver构建

static struct i2c_driver at24c02_driver = {

       .driver = {

              .name      = "at24c02",

       },

       .id           = I2C_DRIVERID_EEPROM,

       .attach_adapter       = at24c02_attach_adapter,

       .detach_client  = at24c02_detach_client,

};

I2C设备驱动的加载与卸载函数模板

static int __init at24c02_init(void)

{

       return i2c_add_driver(&at24c02_driver);

}

      i2c_add_driver的执行回引发i2c_driver结构体at24c02_attach_adapter的执行,若内核中注册了i2c适配器,就顺序调用这些适配器来连接i2c设备,at24c02_attach_adapter调用i2c核心i2c_probe探测设备

static int at24c02_attach_adapter(struct i2c_adapter *adapter)

{

       return i2c_probe(adapter, &addr_data, at24c02_detect);

}

static unsigned short normal_i2c[] = { 0x50, 0x51, 0x52, 0x53, 0x54,

                                   0x55, 0x56, 0x57, I2C_CLIENT_END };

/* Insmod parameters */

I2C_CLIENT_INSMOD_1(at24c02);

       其中第二个参数add_data是i2c_client_address_data类型的变量,由normal_i2c[]通过宏I2C_CLIENT_INSMOD_1构建而成,具体方法参考i2c.h文件。normal_i2c是i2c芯片的地址,如果地址与芯片对应不上,无法探测到设备,当探测到目标设备后,调用at24c02_detect,把探测到得地址address作为参数传入。

#define AT24C02_MAJOR 250

static int at24c02_major = AT24C02_MAJOR;

struct at24c02_data {

       struct cdev cdev;

       struct i2c_client client;

       struct mutex update_lock;

       u8 data[EEPROM_SIZE];             /* Register values */

};

static int at24c02_detect(struct i2c_adapter *adapter, int address, int kind)

{

       struct i2c_client *new_client;

       struct at24c02_data *data;

       int err = 0,result;

       dev_t at24c02_dev=MKDEV(at24c02_major,0);


       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA

                                       | I2C_FUNC_SMBUS_BYTE)) //判断适配器功能

              goto exit;


       if (!(data = kzalloc(sizeof(struct at24c02_data), GFP_KERNEL))) {

              err = -ENOMEM;

              goto exit;

       }

       new_client = &data->client;

       memset(data->data, 0xff, EEPROM_SIZE);

       i2c_set_clientdata(new_client, data);

       new_client->addr = address;

       new_client->adapter = adapter;

       new_client->driver = &at24c02_driver;

       new_client->flags = 0;


       /* Fill in the remaining client fields */

       strlcpy(new_client->name, "at24c02", I2C_NAME_SIZE);

       mutex_init(&data->update_lock);


       /* Tell the I2C layer a new client has arrived */

       if ((err = i2c_attach_client(new_client)))

              goto exit_kfree;


       if(at24c02_major)

       {

              result=register_chrdev_region(at24c02_dev,1,”at24c02”);

       }

       else

      {

              result=alloc_chrdev_region(&at24c02_dev,0,1,”at24c02”);

              at24c02_major=MAJOR(at24c02_dev);

       }

       if(result<0)

       {

              printk(KERN_NOTICE “Unable to get region %d/n”,result);

              err=result;

              goto exit_detach;

       }

       at24c02_setup_cdev(data,0);    //注册字符设备at24c02_fops

       return 0;


exit_detach:

       i2c_detach_client(new_client);

exit_kfree:

       kfree(data);

exit:

       return err;

}

static void at24c02_setup_cdev(struct at24c02_data *dev,int index)

{

       int err,at24c02_dev=MKDEV(at24c02_major,index);

       cdev_init(&dev->cdev,&at24c02_fops);

       dev_cdev.owner=THIS_MODULE;

       err=cdev_add(&dev->cdev,at24c02_dev,1);

       if(err)

              printk(KERN_NOTICE "error %d adding at24c02 %d/n",err,index);

}

        i2c设备驱动卸载函数进行i2c_del_driver调用后,顺序调用内核中注册的适配器断开注册过的i2c设备,此过程是调用at24c02_detach_client实现的。

static void __exit at24c02_exit(void)

{

       i2c_del_driver(&at24c02_driver);

}

static int at24c02_detach_client(struct i2c_client *client)

{

       int err;

       struct at24c02_data *data;


       data=i2c_get_client(client);

       cdev_del(&(data->cdev));

       unregister_chrdev_region(MKDEV(at24c02_major, 0), 1);

       err = i2c_detach_client(client);

       if (err)

              return err;

       kfree(data);


       return 0;

}

字符驱动的实现

struct file_operations at24c02_fops = {

                .owner = THIS_MODULE,

                .read= at24c02 _read,

                .write= at24c02 _write,

                .open= at24c02 _open,

                .release = at24c02_release,

        };

      字符设备驱动不在详述,主要说明如何调用适配器完成数据传输。首先构造消息,通过i2c_transfer来传递,i2c_transfer找到对应适配器algorithm通信方法master_xfer最终完成i2c消息处理。下面是一个关于设备驱动的读函数,其他类似。

static int at24c02_open(struct inode *inode, struct file *file)

{

       struct at24c02_data *data;

       data=container_of(inode->i_cdev,struct at24c02_data,cdev);

       file->private_data = data;

       return 0;

}

static int at24c02_read(struct file *filp, char __user *buff,size_t count, loff_t *offp)

{

       int ret;

       struct i2c_msg msg[2];

       char addr = 0;

       struct at24c02_data *data=file->private_data;

  

       if (count > 1024)

       {

              return -EINVAL;

       }


       msg[0].addr  = data->client->addr;

       msg[0].flags = 0;       /* write */

       msg[0].len   = 1;       /* 1个地址 */

       msg[0].buf   = &addr;

       msg[1].addr  = data->client->addr;

       msg[1].flags = I2C_M_RD;       /* read */

       msg[1].len   = count;          /* 要读的数据个数 */

       msg[1].buf   = data->data; 


       ret = i2c_transfer(at24c02_client->adapter, msg, 2);

       if (ret == 2)

       {

              copy_to_user(buff, data->data, count);

              return count;

       }

       else

              return -EIO;

}

      目前适配器主要支持的传输方法有两种:master_xfer和smbus_xfer,从i2c_algorithm结构体可看出。一般来说,若适配器支持master_xfer那么它可以模拟支持smbus,但只实现smbus_xfer,则不支持master_xfer传输。当然,上面的驱动也可以采用smbus方式完成传输。采用LEGACY方式在2.6.32.2内核下编译不过去,主要是i2c核心中有些函数不存在,所以,设备驱动发展方向是new style方式。

2.new style方式

构建i2c_driver

static struct i2c_driver at24c02_driver = {

       .driver = {

              .name = "at24c02",

              .owner = THIS_MODULE,

       },

       .probe = at24c02_probe,

       .remove = __devexit_p(at24c02_remove),

       .id_table = at24c02_id,

};

加载与注销

static int __init at24c02_init(void)

{

       return i2c_add_driver(&at24c02_driver);

}

module_init(at24c02_init);

        i2c_add_driver会将驱动注册到总线上,探测到i2c设备就会调用at24c02_probe,探测主要是用i2c_match_id函数比较client的名字和id_table中名字,如果相等,则探测到i2c设备,本驱动中id_table如下:

static const struct i2c_device_id at24c02_id[] = {

       { "at24c02", 0 },

       { }

};

MODULE_DEVICE_TABLE(i2c, at24c02_id);

       MODULE_DEVICE_TABLE宏是用来生成i2c_device_id。在legacy方式中i2c_client是自己创建的,而此处的i2c_client如何得到?实际上是在i2c_register_board_info函数注册i2c_board_info过程中构建的,所以arch/arm/mach-s3c2440/mach-smdk2440.c中添加注册信息。

static struct i2c_board_info i2c_devices[] __initdata = {

       { I2C_BOARD_INFO("at24c02", 0x50), },

};

static void __init smdk2440_machine_init(void)

{

       i2c_register_board_info(0,i2c_devices,ARRAY_SIZE(i2c_devices));

       s3c24xx_fb_set_platdata(&smdk2440_fb_info);

       s3c_i2c0_set_platdata(NULL);


       platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices));

       smdk_machine_init();

}

       如果没有注册i2c信息,就探测不到i2c设备。探测到at24c02设备后就会调用at24c02_probe函数。

static int __devinit at24c08b_probe(struct i2c_client *client,const struct i2c_device_id *id)

{

       struct at24c02_data *data;

       int err = 0,result;

       dev_t at24c02_dev=MKDEV(at24c02_major,0);


       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA

                                       | I2C_FUNC_SMBUS_BYTE)) //判断适配器功能

              goto exit;


       if (!(data = kzalloc(sizeof(struct at24c02_data), GFP_KERNEL))) {

              err = -ENOMEM;

              goto exit;

       }


       memset(data, 0, sizeof(struct at24c02_data));

       data->client=client;

       i2c_set_clientdata(client, data);


       if(at24c02_major)

       {

              result=register_chrdev_region(at24c02_dev,1,”at24c02”);

       }

       else

       {

              result=alloc_chrdev_region(&at24c02_dev,0,1,”at24c02”);

              at24c02_major=MAJOR(at24c02_dev);

       }

       if(result<0)

       {

              printk(KERN_NOTICE “Unable to get region %d/n”,result);

              err=result;

              goto exit_kfree;

       }

       at24c02_setup_cdev(data,0);    //注册字符设备at24c02_fops

       return 0;


exit_kfree:

       kfree(data);

exit:

       return err;

}

       probe函数主要注册了字符设备,通过data->client=client获得的相关信息。关于at24c02_fops结构体的完善,由于与前面一种方法类似,这里就不详述。最后就是驱动的注销。

static void __exit at24c02_exit(void)

{

       i2c_del_driver(&at24c02_driver);

}

module_exit(at24c02_exit);

static int __devexit at24c02_remove(struct i2c_client *client)

{

       int err;

       struct at24c02_data *data;

       data=i2c_get_client(client);

       cdev_del(&(data->cdev));

       unregister_chrdev_region(MKDEV(at24c02_major, 0), 1);

       kfree(data);

       return 0;

}

你可能感兴趣的:(驱动)