代码流程:
1.构建struct aw9106设备结构体
2.创建struct i2c_driver aw9106_i2c_driver结构体
3.i2c_add_driver(&aw9106_i2c_driver)注册i2c设备
4.主要的probe函数
probe:
1.创建aw9106结构体指针
2.i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
3.申请aw9106结构体空间,返回地址给结构体指针
4.解析dtsi,获得reset(shutdown)gpio
5.申请reset gpio, 初始化为高电平,使能引脚
6.从reg 0x10读取芯片id, id为0x23
7.再解析dsti,获得预设值,imax, brightness, max_brightness,rise_time, ontime, fall_time等
8.初始化工作线程
INIT_WORK(&aw9106->brightness_work, aw9106_brightness_work);
通过写寄存器设置输出模式
9.创建sys文件节点
ret = sysfs_create_group(&aw9106->cdev.dev->kobj,&aw9106_attribute_group);
static void aw9106_brightness_work(struct work_struct *work)
{
struct aw9106 *aw9106 = container_of(work, struct aw9106,
brightness_work);
unsigned char i;
if(aw9106->cdev.brightness > aw9106->cdev.max_brightness) {
aw9106->cdev.brightness = aw9106->cdev.max_brightness;
}
aw9106_i2c_write(aw9106, REG_WORK_MODE_P0, 0x00); // reg0x12,out4~5 led mode
aw9106_i2c_write(aw9106, REG_WORK_MODE_P1, 0x00); // reg0x13, out0~3 led mode
aw9106_i2c_write(aw9106, REG_EN_BREATH, 0x00); // disable breath
aw9106_i2c_write(aw9106, REG_CTRL, 0x03); // reg0x11, imax (0~10ma), 256 level in 10mA
for(i=0; i<6; i++) {
aw9106_i2c_write(aw9106, REG_DIM00+i,
aw9106->cdev.brightness); // set the brightness of six IO ports
}
}
i2c write:
aw9106_i2c_write(aw9106, REG_WORK_MODE_P0, 0x00);
|
|
i2c_smbus_write_byte_data(aw9106->i2c, reg_addr, reg_data);
------------------------------------------------------------------------
i2c read:
aw9106_i2c_read(aw9106, REG_ID, ®_val);
|
|
*data = i2c_smbus_read_byte_data(aw9106->i2c, reg_addr);