Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89584
  • 博文数量: 19
  • 博客积分: 760
  • 博客等级: 军士长
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-30 16:30
文章存档

2011年(1)

2009年(18)

我的朋友

分类: 嵌入式

2009-07-30 17:10:20

3.2.1  具体开发实例

 

#include

#include

#include

#include

#include

#include

#include 2c.h>

#include

#include

#include

#include

#include

 

#define DEV_NAME "zlg7290"

/* Addresses to scan */

static unsigned short normal_i2c[] = {0x38, I2C_CLIENT_END };

 

// Insmod parameters

I2C_CLIENT_INSMOD_1(zlg7290);

//I2C_CLIENT_INSMOD;

 

/* Each client has this additional data */

struct zlg7290_device {

       struct i2c_client    client;

       struct cdev            dev;

};

 

static int zlg7290_attach_adapter(struct i2c_adapter *adapter);

static int zlg7290_detect(struct i2c_adapter *adapter, int address, int kind);

static int zlg7290_detach_client(struct i2c_client *client);

static ssize_t zlg7290_key_read(struct file *filp, char *buff, size_t count, loff_t *offp);

static ssize_t zlg7290_led_write(struct file *filp, const char *buff, size_t count, loff_t *offp);

static int zlg7290_open(struct inode *inode, struct file *filp);

 

/* This is the driver that will be inserted */

static struct i2c_driver zlg7290_driver = {

       .owner           = THIS_MODULE,

       .name            = DEV_NAME,

//     .id          = I2C_DRIVERID_ZLG7290,

       .flags             = I2C_DF_NOTIFY,

       .attach_adapter     = zlg7290_attach_adapter,

       .detach_client       = zlg7290_detach_client,

};

 

static struct file_operations zlg7290_fops = {

       .owner    =    THIS_MODULE,

       .open    =    zlg7290_open,

//     .release =     led_release,

       .write      =    

       .read      =    zlg7290_key_read,

};

 

static int zlg7290_attach_adapter(struct i2c_adapter *adapter)

{

//     printk("In function %s\n", __FUNCTION__);

       return i2c_probe(adapter, &addr_data, zlg7290_detect);

}

 

/* This function is called by i2c_probe */

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

{

       struct i2c_client *new_client;

       struct zlg7290_device *pdev;

       dev_t dev_no;

       int err = 0;

       /* There are three ways we can read the EEPROM data:

          (1) I2C block reads (faster, but unsupported by most adapters)

          (2) Consecutive byte reads (100% overhead)

          (3) Regular byte data reads (200% overhead)

          The third method is not implemented by this driver because all

          known adapters support at least the second. */

//     printk("In function %s\n", __FUNCTION__);

 

       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA

                                       | I2C_FUNC_SMBUS_BYTE)){

              printk("i2c check functionality error.\n");

              goto exit;

       }

 

       if (!(pdev = kmalloc(sizeof(struct zlg7290_device), GFP_KERNEL))) {

              printk("Kmalloc error.\n");

              err = -ENOMEM;

              goto exit;

       }

      

       memset(pdev, 0, sizeof(struct zlg7290_device));

      

 

      

       new_client = &pdev->client;

       i2c_set_clientdata(new_client, pdev);             

      

       new_client->addr = address;

       new_client->adapter = adapter;

       new_client->driver = &zlg7290_driver;

       new_client->flags = 0;

 

       /* Fill in the remaining client fields */

       strlcpy(new_client->name, DEV_NAME, I2C_NAME_SIZE);

 

 

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

       if ((err = i

阅读(2582) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

qlx19832010-03-08 15:58:12

写的非常棒啊,,学习了!!!