/* * Copyright (C) 2022 Awinic Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "flashlight.h" #include "flashlight-dt.h" #include "flashlight-core.h" /* device tree should be defined in flashlight-dt.h */ #ifndef AW36515_DTNAME #define AW36515_DTNAME "mediatek,flashlights_aw36515" #endif #ifndef AW36515_DTNAME_I2C #define AW36515_DTNAME_I2C "mediatek,flashlights_aw36515" #endif #define AW36515_NAME "flashlights_aw36515" #define AW36515_DRIVER_VERSION "V1.1.0" /* define registers */ #define AW36515_REG_ENABLE (0x01) #define AW36515_MASK_ENABLE_LED1 (0x01) #define AW36515_MASK_ENABLE_LED2 (0x02) #define AW36515_DISABLE (0x00) #define AW36515_ENABLE_LED1 (0x01) #define AW36515_ENABLE_LED1_TORCH (0x09) #define AW36515_ENABLE_LED1_FLASH (0x0D) #define AW36515_ENABLE_LED2 (0x02) #define AW36515_ENABLE_LED2_TORCH (0x0A) #define AW36515_ENABLE_LED2_FLASH (0x0E) #define AW36515_REG_TORCH_LEVEL_LED1 (0x05) #define AW36515_REG_FLASH_LEVEL_LED1 (0x03) #define AW36515_REG_TORCH_LEVEL_LED2 (0x06) #define AW36515_REG_FLASH_LEVEL_LED2 (0x04) #define AW36515_REG_TIMING_CONF (0x08) #define AW36515_TORCH_RAMP_TIME (0x10) #define AW36515_FLASH_TIMEOUT (0x0F) /* define channel, level */ #define AW36515_CHANNEL_NUM 2 #define AW36515_CHANNEL_CH1 0 #define AW36515_CHANNEL_CH2 1 #define AW36515_LEVEL_NUM 26 #define AW36515_LEVEL_TORCH 6 //always in torch mode // prize modify by zhuzhengjiang #define CONFIG_PRIZE_SOFT_LED_ENABLE 1 #define AW36515_DEBUG(format, args...) printk(KERN_WARNING format, ##args) /* define mutex and work queue */ static DEFINE_MUTEX(aw36515_mutex); static struct work_struct aw36515_work_ch1; static struct work_struct aw36515_work_ch2; struct i2c_client *aw36515_flashlight_client; /* define usage count */ static int use_count; /* define i2c */ static struct i2c_client *aw36515_i2c_client; /* platform data * torch_pin_enable: TX1/TORCH pin isa hardware TORCH enable * pam_sync_pin_enable: TX2 Mode The ENVM/TX2 is a PAM Sync. on input * thermal_comp_mode_enable: LEDI/NTC pin in Thermal Comparator Mode * strobe_pin_disable: STROBE Input disabled * vout_mode_enable: Voltage Out Mode enable */ struct aw36515_platform_data { u8 torch_pin_enable; u8 pam_sync_pin_enable; u8 thermal_comp_mode_enable; u8 strobe_pin_disable; u8 vout_mode_enable; int channel_num; struct flashlight_device_id *dev_id; }; /* aw36515 chip data */ struct aw36515_chip_data { struct i2c_client *client; struct aw36515_platform_data *pdata; struct mutex lock; u8 last_flag; u8 no_pdata; }; static struct platform_device *g_aw36515_pdev; static int led_num = -1; /****************************************************************************** * aw36515 operations *****************************************************************************/ static const unsigned char aw36515_torch_level[AW36515_LEVEL_NUM] = { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // flash:duty * 7.83 + 3.91 MAX:2A static const unsigned char aw36515_flash_level[AW36515_LEVEL_NUM] = { 0x04, 0x08, 0x0c, 0x10, 0x04, 0x18, 0x1c, 0x26, 0x31, 0x3c, 0x47, 0x52, 0x5d, 0x68, 0x73, 0x7e, 0x89, 0x94, 0x9f, 0xaa, 0xb5, 0xC0, 0xcb, 0xd6, 0xe1, 0xec}; static volatile unsigned char aw36515_reg_enable; static volatile int aw36515_level_ch1 = -1; static volatile int aw36515_level_ch2 = -1; static int aw36515_is_torch(int level) { if (level >= AW36515_LEVEL_TORCH) return -1; return 0; } static int aw36515_verify_level(int level) { if (level < 0) level = 0; else if (level >= AW36515_LEVEL_NUM) level = AW36515_LEVEL_NUM - 1; return level; } /* i2c wrapper function */ static int aw36515_write_reg(struct i2c_client *client, u8 reg, u8 val) { int ret; struct aw36515_chip_data *chip = i2c_get_clientdata(client); mutex_lock(&chip->lock); ret = i2c_smbus_write_byte_data(client, reg, val); mutex_unlock(&chip->lock); if (ret < 0) pr_err("failed writing at 0x%02x\n", reg); return ret; } static int aw36515_read_reg(struct i2c_client *client, u8 reg) { int val; struct aw36515_chip_data *chip = i2c_get_clientdata(client); mutex_lock(&chip->lock); val = i2c_smbus_read_byte_data(client, reg); mutex_unlock(&chip->lock); if (val < 0) pr_err("failed read at 0x%02x\n", reg); return val; } /* flashlight enable function */ static int aw36515_enable_ch1(void) { unsigned char reg, val; reg = AW36515_REG_ENABLE; if (!aw36515_is_torch(aw36515_level_ch1)) { /* torch mode */ aw36515_reg_enable |= AW36515_ENABLE_LED1_TORCH; } else { /* flash mode */ aw36515_reg_enable |= AW36515_ENABLE_LED1_FLASH; } val = aw36515_reg_enable; return aw36515_write_reg(aw36515_i2c_client, reg, val); } static int aw36515_enable_ch2(void) { unsigned char reg, val; reg = AW36515_REG_ENABLE; if (!aw36515_is_torch(aw36515_level_ch2)) { /* torch mode */ aw36515_reg_enable |= AW36515_ENABLE_LED2_TORCH; } else { /* flash mode */ aw36515_reg_enable |= AW36515_ENABLE_LED2_FLASH; } val = aw36515_reg_enable; return aw36515_write_reg(aw36515_i2c_client, reg, val); } /* flashlight disable function */ static int aw36515_disable_ch1(void) { unsigned char reg, val; reg = AW36515_REG_ENABLE; if (aw36515_reg_enable & AW36515_MASK_ENABLE_LED2) { /* if LED 2 is enable, disable LED 1 */ aw36515_reg_enable &= (~AW36515_ENABLE_LED1); } else { /* if LED 2 is enable, disable LED 1 and clear mode */ aw36515_reg_enable &= (~AW36515_ENABLE_LED1_FLASH); } val = aw36515_reg_enable; return aw36515_write_reg(aw36515_i2c_client, reg, val); } static int aw36515_disable_ch2(void) { unsigned char reg, val; reg = AW36515_REG_ENABLE; if (aw36515_reg_enable & AW36515_MASK_ENABLE_LED1) { /* if LED 1 is enable, disable LED 2 */ aw36515_reg_enable &= (~AW36515_ENABLE_LED2); } else { /* if LED 1 is enable, disable LED 2 and clear mode */ aw36515_reg_enable &= (~AW36515_ENABLE_LED2_FLASH); } val = aw36515_reg_enable; return aw36515_write_reg(aw36515_i2c_client, reg, val); } static int aw36515_enable(int channel) { if (channel == AW36515_CHANNEL_CH1) aw36515_enable_ch1(); else if (channel == AW36515_CHANNEL_CH2) aw36515_enable_ch2(); else { pr_err("%s, Error channel\n", __func__); return -1; } return 0; } static int aw36515_disable(int channel) { if (channel == AW36515_CHANNEL_CH1) aw36515_disable_ch1(); else if (channel == AW36515_CHANNEL_CH2) aw36515_disable_ch2(); else { pr_err("Error channel\n"); return -1; } return 0; } /* set flashlight level */ static int aw36515_set_level_ch1(int level) { int ret; unsigned char reg, val; level = aw36515_verify_level(level); /* set torch brightness level */ reg = AW36515_REG_TORCH_LEVEL_LED1; val = aw36515_torch_level[level]; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); aw36515_level_ch1 = level; /* set flash brightness level */ reg = AW36515_REG_FLASH_LEVEL_LED1; val = aw36515_flash_level[level]; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); return ret; } int aw36515_set_level_ch2(int level) { int ret; unsigned char reg, val; level = aw36515_verify_level(level); /* set torch brightness level */ reg = AW36515_REG_TORCH_LEVEL_LED2; val = aw36515_torch_level[level]; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); aw36515_level_ch2 = level; /* set flash brightness level */ reg = AW36515_REG_FLASH_LEVEL_LED2; val = aw36515_flash_level[level]; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); return ret; } static int aw36515_set_level(int channel, int level) { if (channel == AW36515_CHANNEL_CH1) aw36515_set_level_ch1(level); else if (channel == AW36515_CHANNEL_CH2) aw36515_set_level_ch2(level); else { pr_err("%s, Error channel\n", __func__); return -1; } return 0; } /* flashlight init */ int aw36515_init(void) { int ret; unsigned char reg, val; usleep_range(2000, 2500); /* clear enable register */ reg = AW36515_REG_ENABLE; val = AW36515_DISABLE; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); aw36515_reg_enable = val; /* set torch current ramp time and flash timeout */ reg = AW36515_REG_TIMING_CONF; val = AW36515_TORCH_RAMP_TIME | AW36515_FLASH_TIMEOUT; ret = aw36515_write_reg(aw36515_i2c_client, reg, val); return ret; } /* flashlight uninit */ int aw36515_uninit(void) { aw36515_disable(AW36515_CHANNEL_CH1); aw36515_disable(AW36515_CHANNEL_CH2); return 0; } /****************************************************************************** * Timer and work queue *****************************************************************************/ static struct hrtimer aw36515_timer_ch1; static struct hrtimer aw36515_timer_ch2; static unsigned int aw36515_timeout_ms[AW36515_CHANNEL_NUM]; static void aw36515_work_disable_ch1(struct work_struct *data) { AW36515_DEBUG("ht work queue callback\n"); aw36515_disable_ch1(); } static void aw36515_work_disable_ch2(struct work_struct *data) { printk("lt work queue callback\n"); aw36515_disable_ch2(); } static enum hrtimer_restart aw36515_timer_func_ch1(struct hrtimer *timer) { schedule_work(&aw36515_work_ch1); return HRTIMER_NORESTART; } static enum hrtimer_restart aw36515_timer_func_ch2(struct hrtimer *timer) { schedule_work(&aw36515_work_ch2); return HRTIMER_NORESTART; } int aw36515_timer_start(int channel, ktime_t ktime) { if (channel == AW36515_CHANNEL_CH1) hrtimer_start(&aw36515_timer_ch1, ktime, HRTIMER_MODE_REL); else if (channel == AW36515_CHANNEL_CH2) hrtimer_start(&aw36515_timer_ch2, ktime, HRTIMER_MODE_REL); else { pr_err("%s, Error channel\n", __func__); return -1; } return 0; } int aw36515_timer_cancel(int channel) { if (channel == AW36515_CHANNEL_CH1) hrtimer_cancel(&aw36515_timer_ch1); else if (channel == AW36515_CHANNEL_CH2) hrtimer_cancel(&aw36515_timer_ch2); else { pr_err("Error channel\n"); return -1; } return 0; } /****************************************************************************** * Flashlight operations *****************************************************************************/ static int aw36515_ioctl(unsigned int cmd, unsigned long arg) { struct flashlight_dev_arg *fl_arg; int channel; ktime_t ktime; fl_arg = (struct flashlight_dev_arg *)arg; channel = fl_arg->channel; /* verify channel */ if (channel < 0 || channel >= AW36515_CHANNEL_NUM) { pr_err("%s, Failed with error channel\n", __func__); return -EINVAL; } switch (cmd) { case FLASH_IOC_SET_TIME_OUT_TIME_MS: pr_info("%s, FLASH_IOC_SET_TIME_OUT_TIME_MS(%d): %d\n", __func__, channel, (int)fl_arg->arg); aw36515_timeout_ms[channel] = fl_arg->arg; break; case FLASH_IOC_SET_DUTY: pr_info("%s. FLASH_IOC_SET_DUTY(%d): %d\n", __func__, channel, (int)fl_arg->arg); aw36515_set_level(channel, fl_arg->arg); break; case FLASH_IOC_SET_ONOFF: pr_info("%s. FLASH_IOC_SET_ONOFF(%d): %d\n", __func__, channel, (int)fl_arg->arg); if (fl_arg->arg == 1) { if (aw36515_timeout_ms[channel]) { ktime = ktime_set(aw36515_timeout_ms[channel] / 1000, (aw36515_timeout_ms[channel] % 1000) * 1000000); aw36515_timer_start(channel, ktime); } aw36515_enable(channel); } else { aw36515_disable(channel); aw36515_timer_cancel(channel); } break; default: pr_info("No such command and arg(%d): (%d, %d)\n", channel, _IOC_NR(cmd), (int)fl_arg->arg); return -ENOTTY; } return 0; } static int aw36515_open(void) { /* Actual behavior move to set driver function */ /*since power saving issue */ return 0; } static int aw36515_release(void) { /* uninit chip and clear usage count */ mutex_lock(&aw36515_mutex); use_count--; if (!use_count) aw36515_uninit(); if (use_count < 0) use_count = 0; mutex_unlock(&aw36515_mutex); pr_info("Release: %d\n", use_count); return 0; } static int aw36515_set_driver(int set) { /* init chip and set usage count */ mutex_lock(&aw36515_mutex); if (!use_count) aw36515_init(); use_count++; mutex_unlock(&aw36515_mutex); pr_info("Set driver: %d\n", use_count); return 0; } static ssize_t aw36515_strobe_store(struct flashlight_arg arg) { aw36515_set_driver(1); aw36515_set_level(arg.ct, arg.level); aw36515_enable(arg.ct); msleep(arg.dur); aw36515_disable(arg.ct); aw36515_set_driver(0); return 0; } static struct flashlight_operations aw36515_ops = { aw36515_open, aw36515_release, aw36515_ioctl, aw36515_strobe_store, aw36515_set_driver }; /****************************************************************************** * I2C device and driver *****************************************************************************/ static int aw36515_chip_init(struct aw36515_chip_data *chip) { /* NOTE: Chip initialication move to *"set driver" operation for power saving issue. */ return 0; } /***************************************************************************/ /*AW36515 Debug file */ /***************************************************************************/ static ssize_t aw36515_get_reg(struct device *cd, struct device_attribute *attr, char *buf) { unsigned char reg_val; unsigned char i; ssize_t len = 0; for (i = 0; i < 0x0E; i++) { reg_val = aw36515_read_reg(aw36515_i2c_client, i); len += snprintf(buf+len, PAGE_SIZE-len, "reg0x%2X = 0x%2X\n", i, reg_val); } len += snprintf(buf+len, PAGE_SIZE-len, "\r\n"); return len; } static ssize_t aw36515_set_reg(struct device *cd, struct device_attribute *attr, const char *buf, size_t len) { unsigned int databuf[2]; if (sscanf(buf, "%x %x", &databuf[0], &databuf[1]) == 2) aw36515_write_reg(aw36515_i2c_client, databuf[0], databuf[1]); return len; } static ssize_t aw36515_store(struct device *cd, struct device_attribute *attr, const char *buf, size_t len) { if(buf[0] == '0') { aw36515_enable(AW36515_CHANNEL_CH1); } else if(buf[0] == '1') { aw36515_disable(AW36515_CHANNEL_CH1); } else if(buf[0] == '2') { aw36515_enable(AW36515_CHANNEL_CH2); } else if(buf[0] == '3') { aw36515_disable(AW36515_CHANNEL_CH2); } return len; } static DEVICE_ATTR(reg, 0660, aw36515_get_reg, aw36515_set_reg); static DEVICE_ATTR(set, 0660, NULL, aw36515_store); static int aw36515_create_sysfs(struct i2c_client *client) { int err; struct device *dev = &(client->dev); err = device_create_file(dev, &dev_attr_reg); err = device_create_file(dev, &dev_attr_set); return err; } static int aw36515_parse_dt(struct device *dev, struct aw36515_platform_data *pdata) { struct device_node *np, *cnp; u32 decouple = 0; int i = 0; if (!dev || !dev->of_node || !pdata) return -ENODEV; np = dev->of_node; pdata->channel_num = of_get_child_count(np); if (!pdata->channel_num) { AW36515_DEBUG("Parse no dt, node.\n"); return 0; } AW36515_DEBUG("Channel number(%d).\n", pdata->channel_num); if (of_property_read_u32(np, "decouple", &decouple)) AW36515_DEBUG("Parse no dt, decouple.\n"); if (of_property_read_u32(np, "led_num", &led_num)) AW36515_DEBUG("Parse no dt, led_num.\n"); pdata->dev_id = devm_kzalloc(dev, pdata->channel_num * sizeof(struct flashlight_device_id), GFP_KERNEL); if (!pdata->dev_id) return -ENOMEM; for_each_child_of_node(np, cnp) { if (of_property_read_u32(cnp, "type", &pdata->dev_id[i].type)) goto err_node_put; if (of_property_read_u32(cnp, "ct", &pdata->dev_id[i].ct)) goto err_node_put; if (of_property_read_u32(cnp, "part", &pdata->dev_id[i].part)) goto err_node_put; snprintf(pdata->dev_id[i].name, FLASHLIGHT_NAME_SIZE, AW36515_NAME); pdata->dev_id[i].channel = i; pdata->dev_id[i].decouple = decouple; AW36515_DEBUG("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n", pdata->dev_id[i].type, pdata->dev_id[i].ct, pdata->dev_id[i].part, pdata->dev_id[i].name, pdata->dev_id[i].channel, pdata->dev_id[i].decouple); i++; } return 0; err_node_put: of_node_put(cnp); return -EINVAL; } static int aw36515_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) { struct aw36515_chip_data *chip; struct aw36515_platform_data *pdata = client->dev.platform_data; int err; int i; AW36515_DEBUG("%s Probe start.\n", __func__); /* check i2c */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { pr_err("Failed to check i2c functionality.\n"); err = -ENODEV; goto err_out; } /* init chip private data */ chip = kzalloc(sizeof(struct aw36515_chip_data), GFP_KERNEL); if (!chip) { err = -ENOMEM; goto err_out; } chip->client = client; /* init platform data */ if (!pdata) { pr_err("Platform data does not exist\n"); pdata = kzalloc(sizeof(struct aw36515_platform_data), GFP_KERNEL); if (!pdata) { err = -ENOMEM; goto err_init_pdata; } chip->no_pdata = 1; } chip->pdata = pdata; err = aw36515_parse_dt(&g_aw36515_pdev->dev, chip->pdata); if (err) goto err_init_pdata; i2c_set_clientdata(client, chip); aw36515_i2c_client = client; /* init mutex and spinlock */ mutex_init(&chip->lock); /* init work queue */ INIT_WORK(&aw36515_work_ch1, aw36515_work_disable_ch1); INIT_WORK(&aw36515_work_ch2, aw36515_work_disable_ch2); /* init timer */ hrtimer_init(&aw36515_timer_ch1, CLOCK_MONOTONIC, HRTIMER_MODE_REL); aw36515_timer_ch1.function = aw36515_timer_func_ch1; hrtimer_init(&aw36515_timer_ch2, CLOCK_MONOTONIC, HRTIMER_MODE_REL); aw36515_timer_ch2.function = aw36515_timer_func_ch2; aw36515_timeout_ms[AW36515_CHANNEL_CH1] = 100; aw36515_timeout_ms[AW36515_CHANNEL_CH2] = 100; /* init chip hw */ aw36515_chip_init(chip); /* register flashlight device */ if (pdata->channel_num) { for (i = 0; i < pdata->channel_num; i++) { if (flashlight_dev_register_by_device_id(&pdata->dev_id[i], &aw36515_ops)) { err = -EFAULT; goto err_free; } } } else { if (flashlight_dev_register(AW36515_NAME, &aw36515_ops)) { err = -EFAULT; goto err_free; } } /* clear usage count */ use_count = 0; aw36515_create_sysfs(client); AW36515_DEBUG("%s Probe done.\n", __func__); return 0; err_free: kfree(chip->pdata); err_init_pdata: i2c_set_clientdata(client, NULL); kfree(chip); err_out: return err; } static int aw36515_i2c_remove(struct i2c_client *client) { struct aw36515_chip_data *chip = i2c_get_clientdata(client); AW36515_DEBUG("Remove start.\n"); /* flush work queue */ flush_work(&aw36515_work_ch1); flush_work(&aw36515_work_ch2); /* unregister flashlight operations */ flashlight_dev_unregister(AW36515_NAME); /* free resource */ if (chip->no_pdata) kfree(chip->pdata); kfree(chip); AW36515_DEBUG("Remove done.\n"); return 0; } static const struct i2c_device_id aw36515_i2c_id[] = { {AW36515_NAME, 0}, {} }; #ifdef CONFIG_OF static const struct of_device_id aw36515_i2c_of_match[] = { {.compatible = AW36515_DTNAME_I2C}, {}, }; #endif static struct i2c_driver aw36515_i2c_driver = { .driver = { .name = AW36515_NAME, #ifdef CONFIG_OF .of_match_table = aw36515_i2c_of_match, #endif }, .probe = aw36515_i2c_probe, .remove = aw36515_i2c_remove, .id_table = aw36515_i2c_id, }; #if CONFIG_PRIZE_SOFT_LED_ENABLE //prize add by lipengpeng 20220610 start static volatile int cur_soft_torch_status = 0; static ssize_t device_soft_torch_value_show(struct device *dev, struct device_attribute *attr, char *buf) { printk("aw36515 cur_soft_torch_status=%d\n", cur_soft_torch_status); return sprintf(buf, "%u\n", cur_soft_torch_status); } static ssize_t device_soft_torch_value_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { //ktime_t ktime; printk("aw36515 %s ON/OFF value = %d:\n ", cur_soft_torch_status); if(sscanf(buf, "%u", &cur_soft_torch_status) != 1) { printk("aw36515Invalid values\n"); return -EINVAL; } if(1==cur_soft_torch_status) { //aw36515_timeout_ms[AW36515_CHANNEL_CH1]=100; //aw36515_set_level(AW36515_CHANNEL_CH1, 1); //printk("aw36515_set_level[AW36515_CHANNEL_CH1]=%d\n",aw36515_timeout_ms[AW36515_CHANNEL_CH1]); //if (aw36515_timeout_ms[AW36515_CHANNEL_CH1]) { // printk("aw36515_timeout_ms[AW36515_CHANNEL_CH1]=%d\n",aw36515_timeout_ms[AW36515_CHANNEL_CH1]); // ktime =ktime_set(aw36515_timeout_ms[AW36515_CHANNEL_CH1] / 1000, // (aw36515_timeout_ms[AW36515_CHANNEL_CH1] % 1000) * 1000000); // aw36515_timer_start(AW36515_CHANNEL_CH1, ktime); // } aw36515_enable(AW36515_CHANNEL_CH1); aw36515_set_level_ch1(6); }else{ aw36515_disable(AW36515_CHANNEL_CH1); aw36515_timer_cancel(AW36515_CHANNEL_CH1); aw36515_set_level_ch1(0); } return size; } static DEVICE_ATTR(soft_torch_value, 0664, device_soft_torch_value_show, device_soft_torch_value_store); static volatile int cur_soft_torch2_status = 0; static ssize_t device_soft_torch2_value_show(struct device *dev, struct device_attribute *attr, char *buf) { printk("aw36515 cur_soft_torch2_status=%d\n", cur_soft_torch2_status); return sprintf(buf, "%u\n", cur_soft_torch2_status); } static ssize_t device_soft_torch2_value_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { //ktime_t ktime; printk("aw36515 %s ON/OFF value = %d:\n ", cur_soft_torch2_status); if(sscanf(buf, "%u", &cur_soft_torch2_status) != 1) { printk("aw36515Invalid values\n"); return -EINVAL; } if(1==cur_soft_torch2_status) { //aw36515_timeout_ms[AW36515_CHANNEL_CH1]=100; //aw36515_set_level(AW36515_CHANNEL_CH1, 1); //printk("aw36515_set_level[AW36515_CHANNEL_CH1]=%d\n",aw36515_timeout_ms[AW36515_CHANNEL_CH1]); //if (aw36515_timeout_ms[AW36515_CHANNEL_CH1]) { // printk("aw36515_timeout_ms[AW36515_CHANNEL_CH1]=%d\n",aw36515_timeout_ms[AW36515_CHANNEL_CH1]); // ktime =ktime_set(aw36515_timeout_ms[AW36515_CHANNEL_CH1] / 1000, // (aw36515_timeout_ms[AW36515_CHANNEL_CH1] % 1000) * 1000000); // aw36515_timer_start(AW36515_CHANNEL_CH1, ktime); // } aw36515_enable(AW36515_CHANNEL_CH2); aw36515_set_level_ch2(6); }else{ aw36515_disable(AW36515_CHANNEL_CH2); aw36515_timer_cancel(AW36515_CHANNEL_CH2); aw36515_set_level_ch2(0); } return size; } static DEVICE_ATTR(soft_torch2_value, 0664, device_soft_torch2_value_show, device_soft_torch2_value_store); #endif //prize add by lipengpeng 20220610 end /****************************************************************************** * Platform device and driver *****************************************************************************/ #if CONFIG_PRIZE_SOFT_LED_ENABLE struct class *flash_soft_torch_class; //prize add by lipengpeng 20220610 start struct class *flash_soft_torch2_class; //prize add by lipengpeng 20220610 start #endif //struct class *flash_torch_class; //prize add by lipengpeng 20220610 start static int aw36515_probe(struct platform_device *dev) { #if CONFIG_PRIZE_SOFT_LED_ENABLE struct device *flash_soft_torch_dev; struct device *flash_soft_torch2_dev; //struct device *flash_torch_dev; #endif g_aw36515_pdev = dev; printk(" aw36515 Probe start.\n"); //prize add by lipengpeng 20220610 start #if CONFIG_PRIZE_SOFT_LED_ENABLE flash_soft_torch_class = class_create(THIS_MODULE, "flash_soft_torch"); if (IS_ERR(flash_soft_torch_class)) { printk("Failed to create class(flash_soft_torch_class)!"); return 0; } flash_soft_torch_dev = device_create(flash_soft_torch_class, NULL, 0, NULL, "flash_soft_torch_data"); if (IS_ERR(flash_soft_torch_dev)) printk("Failed to create flash_soft_torch_dev device"); if (device_create_file(flash_soft_torch_dev, &dev_attr_soft_torch_value) < 0) printk("Failed to create device file(%s)!",dev_attr_soft_torch_value.attr.name); flash_soft_torch2_class = class_create(THIS_MODULE, "flash_soft_torch2"); if (IS_ERR(flash_soft_torch2_class)) { printk("Failed to create class(flash_soft_torch2_class)!"); return 0; } flash_soft_torch2_dev = device_create(flash_soft_torch2_class, NULL, 0, NULL, "flash_soft_torch2_data"); if (IS_ERR(flash_soft_torch2_dev)) printk("Failed to create flash_soft_torch2_dev device"); if (device_create_file(flash_soft_torch2_dev, &dev_attr_soft_torch2_value) < 0) printk("Failed to create device file(%s)!",dev_attr_soft_torch2_value.attr.name); #endif //prize add by lipengpeng 20220610 end if (i2c_add_driver(&aw36515_i2c_driver)) { pr_err("Failed to add i2c driver.\n"); return -1; } pr_info("%s Probe done.\n", __func__); return 0; } static int aw36515_remove(struct platform_device *dev) { pr_info("Remove start.\n"); i2c_del_driver(&aw36515_i2c_driver); pr_info("Remove done.\n"); return 0; } #ifdef CONFIG_OF static const struct of_device_id aw36515_of_match[] = { {.compatible = AW36515_DTNAME}, {}, }; MODULE_DEVICE_TABLE(of, aw36515_of_match); #else static struct platform_device aw36515_platform_device[] = { { .name = AW36515_NAME, .id = 0, .dev = {} }, {} }; MODULE_DEVICE_TABLE(platform, aw36515_platform_device); #endif static struct platform_driver aw36515_platform_driver = { .probe = aw36515_probe, .remove = aw36515_remove, .driver = { .name = AW36515_NAME, .owner = THIS_MODULE, #ifdef CONFIG_OF .of_match_table = aw36515_of_match, #endif }, }; static int __init flashlight_aw36515_init(void) { int ret; pr_info("%s driver version %s.\n", __func__, AW36515_DRIVER_VERSION); #ifndef CONFIG_OF ret = platform_device_register(&aw36515_platform_device); if (ret) { pr_err("Failed to register platform device\n"); return ret; } #endif ret = platform_driver_register(&aw36515_platform_driver); if (ret) { pr_err("Failed to register platform driver\n"); return ret; } pr_info("flashlight_aw36515 Init done.\n"); return 0; } static void __exit flashlight_aw36515_exit(void) { pr_info("flashlight_aw36515-Exit start.\n"); platform_driver_unregister(&aw36515_platform_driver); pr_info("flashlight_aw36515 Exit done.\n"); } module_init(flashlight_aw36515_init); module_exit(flashlight_aw36515_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Awinic"); MODULE_DESCRIPTION("AW36515 Flashlight Driver");