linux iic驱动开发,linux下IIC驱动开发分析

#include

#include

#include

#include

#include

#include

#include

#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_clientclient;

struct cdevdev;

};

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 structi2c_driverzlg7290_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=zlg7290_led_write,

.read=zlg7290_key_read,

};

static int zlg7290_attach_adapter(struct i2c_adapter *adapter)

{

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

returni2c_probe(adapter, &addr_data,zlg7290_detect);

}

/* This function is called by i2c_probe */

static intzlg7290_detect(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) I2Cblock 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("i2ccheck 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 I2Clayer a new client has arrived */

if ((err = i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值