请问Linux下I2C设备驱动该如何写?

sunling83417 sunling83417 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想写一个Linux下的I2C设备驱动,基于系统中现有的I2C子系统,采用probe的方式来实现,请问此类代码是采用什么样的一种固定框架?我看了内核中一些设备的代码,觉得很复杂,而我只是想实现很简单的功能;网上也查了一些资料,但并没有找到详细叙述。
目前我的代码如下:

static const struct i2c_device_id dm2016_ids[] =
{
{"dm2016", 0},
{/* END OF LIST */}
};
MODULE_DEVICE_TABLE(i2c, dm2016_ids);


struct dm2016_data {
struct i2c_client *client;
};

static int __devinit dm2016_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct dm2016_data *pstDM2016Data = NULL;
struct device *pstDev = &client->dev;
int s32Ret = 0;

pstDM2016Data = kzalloc(sizeof(*pstDev), GFP_KERNEL);
if (NULL == pstDM2016Data)
{
dev_err(pstDev, "alloc dm2016 data memory fail!\n");
return -ENOMEM;
}

pstDM2016Data->client = client;
i2c_set_clientdata(client, pstDM2016Data);


dev_info(pstDev, "create dm2016 client OK!\n");
return 0;
}

static int __devexit dm2016_remove(struct i2c_client *client)
{
struct dm2016_data *pstDM2016Data = i2c_get_clientdata(client);

kfree(pstDM2016Data);

return 0;
}


static struct i2c_driver dm2016_driver = {
.driver = {
.name = "dm2016",
.owner = THIS_MODULE,
},
.probe = dm2016_probe,
.remove = __devexit_p(dm2016_remove),
.id_table = dm2016_ids,
};

static int __init dm2016_init(void)
{
return i2c_add_driver(&dm2016_driver);
}

static void __exit dm2016_exit(void)
{
i2c_del_driver(&dm2016_driver);
}

我想为设备定义自己的读写函数以及类似ioctl这样的操作,请问我在dm2016_probe函数中要做哪些操作?还有结构体dm2016_data需要哪些成员?
谢谢!
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部