mini2440 linuxi2c驱动

#include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/delay.h> #include <linux/mutex.h> #include <linux/sysfs.h> #include <linux/mod_devicetable.h> #include <linux/log2.h> #include <linux/bitops.h> #include <linux/jiffies.h> #include <linux/i2c.h> #include <linux/i2c/at24.h> #include <linux/fs.h> #include <asm/uaccess.h>

static struct i2c_client  *at24_client;

static int major=0;

static struct class  *cls;

static const struct i2c_device_id at24_ids[] = {  

 {"at24",8},    {}, };

MODULE_DEVICE_TABLE(i2c,at24_ids);

static ssize_t at24_read(struct file *file, char __user *buf, size_t len, loff_t *offset)

{  unsigned char address;  

unsigned char data;

 struct i2c_msg msg[2];

 int ret;

 if(len!=1)   return -EINVAL;  copy_from_user(&address,buf,1);  msg[0].addr=at24_client->addr;  msg[0].buf=&address;  msg[0].len=1;  msg[0].flags=0;  msg[1].addr=at24_client->addr;  msg[1].buf=&data;  msg[1].len=1;  msg[1].flags =I2C_M_RD;  ret=i2c_transfer(at24_client->adapter,msg,2);  if(ret==2)   {    copy_to_user(buf,&data,1);    return 1;   }  else   return -EIO;  printk("read !\n");  return 0; } static ssize_t at24_write(struct file *file, const char __user *buf, size_t len, loff_t *offset) {  unsigned char val[2];  struct i2c_msg msg[1];  int ret;  if(len!=2)   return -EINVAL;  copy_from_user(val,buf,2);  msg[0].addr=at24_client->addr;  msg[0].buf=val;  msg[0].len=2;  msg[0].flags=0;  ret=i2c_transfer(at24_client->adapter,msg,1);  if(ret==1)  {   return 2;  }  else   return -EIO;  printk("write !\n");  return 0; }

static struct file_operations fop={  .owner =THIS_MODULE,  .write  =at24_write,  .read   =at24_read, }; static int at24_probe(struct i2c_client *client, const struct i2c_device_id *ids) {  printk("probe ok!\n");  at24_client=client;  major=register_chrdev(0,"at24",&fop);  cls=class_create(THIS_MODULE,"at24");  device_create(cls,NULL,MKDEV(major,0),NULL,"at24");  return 0; } static int at24_remove(struct i2c_client *client) {  kfree(at24_client);  unregister_chrdev(major,"at24");  device_destroy(cls,MKDEV(major,0));  class_destroy(cls);  printk("remove ok!\n");  return 0; }

static struct i2c_driver at24cx_driver = {  .driver = {   .name = "at24",   .owner = THIS_MODULE,  },  .probe = at24_probe,  .remove = at24_remove,  .id_table =at24_ids,   };

static int at24cx_init(void) {  return i2c_add_driver(&at24cx_driver); }

static void at24cx_exit(void) {  i2c_del_driver(&at24cx_driver); } module_init(at24cx_init); module_exit(at24cx_exit); MODULE_LICENSE("GPL");

static struct at24_platform_data at24_i2c_info = {  

.byte_len = 8,  .page_size = 8, };

static struct i2c_board_info at24_i2c_devices[] =

{   {         I2C_BOARD_INFO("at24", 0x50),     

    .platform_data = &at24_i2c_info,          },

};

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。