微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 3.4.2内核下的I2C驱动框架解析

3.4.2内核下的I2C驱动框架解析

时间:11-21 来源:互联网 点击:
第一种方法:

at24cxx_drv.c源码:

#include "linux/kernel.h"

#include "linux/module.h"

#include "linux/platform_device.h"

#include "linux/i2c.h"

#include "linux/err.h"

#include "linux/regmap.h"

#include "linux/slab.h"

static int __devinit at24cxx_probe(struct i2c_client *client,

const struct i2c_device_id *id)

{

printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

return 0;

}

static int __devexit at24cxx_remove(struct i2c_client *client)

{

printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

return 0;

}

static const struct i2c_device_id at24cxx_id_table[] = {

{ "at24c08", 0 },

{}

};

// 1. 分配/设置i2c_driver //

static struct i2c_driver at24cxx_driver = {

.driver = {

.name = "100ask",

.owner = THIS_MODULE,

},

.probe = at24cxx_probe,

.remove = __devexit_p(at24cxx_remove),

.id_table = at24cxx_id_table,

};

static int at24cxx_drv_init(void)

{

// 2. 注册i2c_driver //

i2c_add_driver(&at24cxx_driver);

return 0;

}

static void at24cxx_drv_exit(void)

{

i2c_del_driver(&at24cxx_driver);

}

module_init(at24cxx_drv_init);

module_exit(at24cxx_drv_exit);

MODULE_LICENSE("GPL");

at24cxx_dev.c源码:

#include "linux/kernel.h"

#include "linux/module.h"

#include "linux/platform_device.h"

#include "linux/i2c.h"

#include "linux/err.h"

#include "linux/regmap.h"

#include "linux/slab.h"

static struct i2c_board_info at24cxx_info = {

I2C_BOARD_INFO("at24c08", 0x50),

};

static struct i2c_client *at24cxx_client;

static int at24cxx_dev_init(void)

{

struct i2c_adapter *i2c_adap;

i2c_adap = i2c_get_adapter(0);

at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);

i2c_put_adapter(i2c_adap);

return 0;

}

static void at24cxx_dev_exit(void)

{

i2c_unregister_device(at24cxx_client);

}

module_init(at24cxx_dev_init);

module_exit(at24cxx_dev_exit);

MODULE_LICENSE("GPL");

===============================================================

第二种方法:

at24cxx_drv.c源码:

#include "linux/kernel.h"

#include "linux/module.h"

#include "linux/platform_device.h"

#include "linux/i2c.h"

#include "linux/err.h"

#include "linux/regmap.h"

#include "linux/slab.h"

static int __devinit at24cxx_probe(struct i2c_client *client,

const struct i2c_device_id *id)

{

printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

return 0;

}

static int __devexit at24cxx_remove(struct i2c_client *client)

{

printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

return 0;

}

static const struct i2c_device_id at24cxx_id_table[] = {

{ "at24c08", 0 },

{}

};

// 1. 分配/设置i2c_driver //

static struct i2c_driver at24cxx_driver = {

.driver = {

.name = "100ask",

.owner = THIS_MODULE,

},

.probe = at24cxx_probe,

.remove = __devexit_p(at24cxx_remove),

.id_table = at24cxx_id_table,

};

static int at24cxx_drv_init(void)

{

// 2. 注册i2c_driver //

i2c_add_driver(&at24cxx_driver);

return 0;

}

static void at24cxx_drv_exit(void)

{

i2c_del_driver(&at24cxx_driver);

}

module_init(at24cxx_drv_init);

module_exit(at24cxx_drv_exit);

MODULE_LICENSE("GPL");

at24cxx_dev.c源码:

#include "linux/kernel.h"

#include "linux/module.h"

#include "linux/platform_device.h"

#include "linux/i2c.h"

#include "linux/err.h"

#include "linux/regmap.h"

#include "linux/slab.h"

static struct i2c_client *at24cxx_client;

static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };

static int at24cxx_dev_init(void)

{

struct i2c_adapter *i2c_adap;

struct i2c_board_info at24cxx_info;

memset(&at24cxx_info, 0, sizeof(struct i2c_board_info));

strlcpy(at24cxx_info.type, "at24c08", I2C_NAME_SIZE);

i2c_adap = i2c_get_adapter(0);

at24cxx_client = i2c_new_probed_device(i2c_adap, &at24cxx_info, addr_list, NULL);

i2c_put_adapter(i2c_adap);

if (at24cxx_client)

return 0;

else

return -ENODEV;

}

static void at24cxx_dev_exit

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top