方法一:
static ssize_t bq24152_attr_store(struct device_driver *driver,const char *buf, size_t count) { int iRet = 0; unsigned char path_image[255]; if(NULL == buf || count >255 || count == 0 || strnchr(buf, count, 0x20)) return -1; memcpy (path_image, buf, count); printk("count is %d\n", count); printk("buf is %s\n", buf); printk("bus is %d\n", buf[0]); #if 0 ret = pm8058_set_charge_batt(0); if (ret < 0) { pr_err("can not close the bat_fet mos\n"); return ret; } di->enable_charger = 0; bq2415x_config_control_reg(di); #endif return iRet; } static ssize_t bq24152_attr_show(struct device_driver *driver, char *buf) { ssize_t ret =0; u8 read_reg[5] = {0}; u8 temp; if(NULL == buf) { return -1; } bq2415x_read_block(this_di, &read_reg[0], 0, 5); temp = pm8058_get_charge_batt(); ret += sprintf(buf, "status register is 0x%x \n", read_reg[0]); ret += sprintf(buf + ret, "control register is 0x%x \n", read_reg[1]); ret += sprintf(buf + ret, "battery voltage register is 0x%x \n", read_reg[2]); ret += sprintf(buf + ret, "charge current register is 0x%x \n", read_reg[4]); ret += sprintf(buf + ret, "bat mos state is %d\n", temp); return ret; } static DRIVER_ATTR(state, S_IRUGO|S_IWUGO, bq24152_attr_show, bq24152_attr_store); // S_IRUGO|S_IWUGO 这个可以换成 0644
然后在probe 函数中加入:
ret = driver_create_file(&(bq2415x_charger_driver.driver), &driver_attr_state); if (0 != ret) { pr_err("failed to create sysfs entry(state): %d\n", ret); goto free_irq; }
方法二: