// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019 MediaTek Inc. #include #include #include #include #include #include #include #include #include #include #include #include #if IS_ENABLED(CONFIG_MTK_FLASHLIGHT) #include "flashlight-core.h" #include #endif //prize add by chenwenhui 20240510 for sts KE start #define Enable_Cooling 0 //prize add by chenwenhui 20240510 for sts KE end #define AW36518_NAME "aw36518" #define AW36518_I2C_ADDR (0x63) /* registers definitions */ #define REG_ENABLE 0x01 #define REG_LED0_FLASH_BR 0x03 #define REG_LED1_FLASH_BR 0x04 #define REG_LED0_TORCH_BR 0x05 #define REG_LED1_TORCH_BR 0x06 #define REG_FLASH_TOUT 0x08 #define REG_FLAG1 0x0A #define REG_FLAG2 0x0B /* fault mask */ #define FAULT_TIMEOUT (1<<0) #define FAULT_THERMAL_SHUTDOWN (1<<2) #define FAULT_LED0_SHORT_CIRCUIT (1<<5) #define FAULT_LED1_SHORT_CIRCUIT (1<<4) /* FLASH Brightness * min 2940uA, step 5870uA, max 1500000uA */ #define AW36518_FLASH_BRT_MIN 2940 #define AW36518_FLASH_BRT_STEP 5870 #define AW36518_FLASH_BRT_MAX 1500000 #define AW36518_FLASH_BRT_uA_TO_REG(a) \ ((a) < AW36518_FLASH_BRT_MIN ? 0 : \ (((a) - AW36518_FLASH_BRT_MIN) / AW36518_FLASH_BRT_STEP)) /* FLASH TIMEOUT DURATION * min 32ms, step 32ms, max 1024ms */ #define AW36518_FLASH_TOUT_MIN 100 #define AW36518_FLASH_TOUT_STEP 50 #define AW36518_FLASH_TOUT_MAX 400 /* TORCH BRT * min 750uA, step 1510uA, max 386000uA */ #define AW36518_TORCH_BRT_MIN 750 #define AW36518_TORCH_BRT_STEP 1510 #define AW36518_TORCH_BRT_MAX 386000 #define AW36518_TORCH_BRT_uA_TO_REG(a) \ ((a) < AW36518_TORCH_BRT_MIN ? 0 : \ (((a) - AW36518_TORCH_BRT_MIN) / AW36518_TORCH_BRT_STEP)) #define AW36518_COOLER_MAX_STATE 4 static const int flash_state_to_current_limit[AW36518_COOLER_MAX_STATE] = { 150000, 100000, 50000, 25000 }; enum aw36518_led_id { AW36518_LED0 = 0, AW36518_LED1, AW36518_LED_MAX }; /* struct aw36518_platform_data * * @max_flash_timeout: flash timeout * @max_flash_brt: flash mode led brightness * @max_torch_brt: torch mode led brightness */ struct aw36518_platform_data { u32 max_flash_timeout; u32 max_flash_brt[AW36518_LED_MAX]; u32 max_torch_brt[AW36518_LED_MAX]; }; enum led_enable { MODE_SHDN = 0x0, MODE_TORCH = 0x08, MODE_FLASH = 0x0C, }; /** * struct aw36518_flash * * @dev: pointer to &struct device * @pdata: platform data * @regmap: reg. map for i2c * @lock: muxtex for serial access. * @led_mode: V4L2 LED mode * @ctrls_led: V4L2 controls * @subdev_led: V4L2 subdev */ struct aw36518_flash { struct device *dev; struct aw36518_platform_data *pdata; struct regmap *regmap; struct mutex lock; enum v4l2_flash_led_mode led_mode; struct v4l2_ctrl_handler ctrls_led[AW36518_LED_MAX]; struct v4l2_subdev subdev_led[AW36518_LED_MAX]; struct device_node *dnode[AW36518_LED_MAX]; struct pinctrl *aw36518_hwen_pinctrl; struct pinctrl_state *aw36518_hwen_high; struct pinctrl_state *aw36518_hwen_low; #if IS_ENABLED(CONFIG_MTK_FLASHLIGHT) struct flashlight_device_id flash_dev_id[AW36518_LED_MAX]; #endif struct thermal_cooling_device *cdev; int need_cooler; unsigned long max_state; unsigned long target_state; unsigned long target_current[AW36518_LED_MAX]; unsigned long ori_current[AW36518_LED_MAX]; }; /* define usage count */ static int use_count; static struct aw36518_flash *aw36518_flash_data; #define to_aw36518_flash(_ctrl, _no) \ container_of(_ctrl->handler, struct aw36518_flash, ctrls_led[_no]) static int aw36518_set_driver(int set); /* define pinctrl */ #define AW36518_PINCTRL_PIN_HWEN 0 #define AW36518_PINCTRL_PINSTATE_LOW 0 #define AW36518_PINCTRL_PINSTATE_HIGH 1 #define AW36518_PINCTRL_STATE_HWEN_HIGH "hwen_high" #define AW36518_PINCTRL_STATE_HWEN_LOW "hwen_low" /****************************************************************************** * Pinctrl configuration *****************************************************************************/ static int aw36518_pinctrl_init(struct aw36518_flash *flash) { int ret = 0; /* get pinctrl */ flash->aw36518_hwen_pinctrl = devm_pinctrl_get(flash->dev); if (IS_ERR(flash->aw36518_hwen_pinctrl)) { pr_info("Failed to get flashlight pinctrl.\n"); ret = PTR_ERR(flash->aw36518_hwen_pinctrl); return ret; } /* Flashlight HWEN pin initialization */ flash->aw36518_hwen_high = pinctrl_lookup_state( flash->aw36518_hwen_pinctrl, AW36518_PINCTRL_STATE_HWEN_HIGH); if (IS_ERR(flash->aw36518_hwen_high)) { pr_info("Failed to init (%s)\n", AW36518_PINCTRL_STATE_HWEN_HIGH); ret = PTR_ERR(flash->aw36518_hwen_high); } flash->aw36518_hwen_low = pinctrl_lookup_state( flash->aw36518_hwen_pinctrl, AW36518_PINCTRL_STATE_HWEN_LOW); if (IS_ERR(flash->aw36518_hwen_low)) { pr_info("Failed to init (%s)\n", AW36518_PINCTRL_STATE_HWEN_LOW); ret = PTR_ERR(flash->aw36518_hwen_low); } return ret; } static int aw36518_pinctrl_set(struct aw36518_flash *flash, int pin, int state) { int ret = 0; if (IS_ERR(flash->aw36518_hwen_pinctrl)) { pr_info("pinctrl is not available\n"); return -1; } switch (pin) { case AW36518_PINCTRL_PIN_HWEN: if (state == AW36518_PINCTRL_PINSTATE_LOW && !IS_ERR(flash->aw36518_hwen_low)) pinctrl_select_state(flash->aw36518_hwen_pinctrl, flash->aw36518_hwen_low); else if (state == AW36518_PINCTRL_PINSTATE_HIGH && !IS_ERR(flash->aw36518_hwen_high)) pinctrl_select_state(flash->aw36518_hwen_pinctrl, flash->aw36518_hwen_high); else pr_info("set err, pin(%d) state(%d)\n", pin, state); break; default: pr_info("set err, pin(%d) state(%d)\n", pin, state); break; } return ret; } /* enable mode control */ static int aw36518_mode_ctrl(struct aw36518_flash *flash) { int rval = -EINVAL; pr_info_ratelimited("%s mode:%d", __func__, flash->led_mode); switch (flash->led_mode) { case V4L2_FLASH_LED_MODE_NONE: pr_info("%s V4L2_FLASH_LED_MODE_NONE\n"); rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x0C, MODE_SHDN); break; case V4L2_FLASH_LED_MODE_TORCH: pr_info("%s V4L2_FLASH_LED_MODE_TORCH\n"); rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x0C, MODE_TORCH); break; case V4L2_FLASH_LED_MODE_FLASH: pr_info("%s V4L2_FLASH_LED_MODE_FLASH\n"); rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x0C, MODE_FLASH); break; } return rval; } /* led1/2 enable/disable */ static int aw36518_enable_ctrl(struct aw36518_flash *flash, enum aw36518_led_id led_no, bool on) { int rval; if (led_no < 0 || led_no >= AW36518_LED_MAX) { pr_info("led_no error\n"); return -1; } pr_info_ratelimited("%s led:%d enable:%d", __func__, led_no, on); flashlight_kicker_pbm(on); #ifdef CONFIG_MTK_FLASHLIGHT_PT if (flashlight_pt_is_low()) { pr_info_ratelimited("pt is low\n"); return 0; } #endif if (led_no == AW36518_LED0) { if (on) rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x03, 0x03); else rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x03, 0x00); } else { if (on) rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x02, 0x02); else rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x02, 0x00); } return rval; } /* torch1/2 brightness control */ static int aw36518_torch_brt_ctrl(struct aw36518_flash *flash, enum aw36518_led_id led_no, unsigned int brt) { int rval; u8 br_bits; if (led_no < 0 || led_no >= AW36518_LED_MAX) { pr_info("led_no error\n"); return -1; } pr_info_ratelimited("%s %d brt:%u\n", __func__, led_no, brt); if (brt < AW36518_TORCH_BRT_MIN) return aw36518_enable_ctrl(flash, led_no, false); if (flash->need_cooler == 0) { flash->ori_current[led_no] = brt; } else { if (brt > flash->target_current[led_no]) { brt = flash->target_current[led_no]; pr_info("thermal limit current:%d\n", brt); } } br_bits = AW36518_TORCH_BRT_uA_TO_REG(brt); if (led_no == AW36518_LED0) rval = regmap_update_bits(flash->regmap, REG_LED0_TORCH_BR, 0xFF, br_bits); else rval = regmap_update_bits(flash->regmap, REG_LED1_TORCH_BR, 0xFF, br_bits); return rval; } /* flash1/2 brightness control */ static int aw36518_flash_brt_ctrl(struct aw36518_flash *flash, enum aw36518_led_id led_no, unsigned int brt) { int rval; u8 br_bits; if (led_no < 0 || led_no >= AW36518_LED_MAX) { pr_info("led_no error\n"); return -1; } pr_info("%s %d brt:%u", __func__, led_no, brt); if (brt < AW36518_FLASH_BRT_MIN) return aw36518_enable_ctrl(flash, led_no, false); if (flash->need_cooler == 1 && brt > flash->target_current[led_no]) { brt = flash->target_current[led_no]; pr_info("thermal limit current:%d\n", brt); } br_bits = AW36518_FLASH_BRT_uA_TO_REG(brt); if (led_no == AW36518_LED0) rval = regmap_update_bits(flash->regmap, REG_LED0_FLASH_BR, 0xFF, br_bits); else rval = regmap_update_bits(flash->regmap, REG_LED1_FLASH_BR, 0xFF, br_bits); return rval; } /* flash1/2 timeout control */ static int aw36518_flash_tout_ctrl(struct aw36518_flash *flash, unsigned int tout) { int rval; u8 tout_bits; pr_info("%s tout:%u", __func__, tout); if (tout == 50) tout_bits = 0x04; else tout_bits = 0x07 + (tout / AW36518_FLASH_TOUT_STEP); rval = regmap_update_bits(flash->regmap, REG_FLASH_TOUT, 0x0F, tout_bits); return rval; } /* v4l2 controls */ static int aw36518_get_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id led_no) { struct aw36518_flash *flash = to_aw36518_flash(ctrl, led_no); int rval = -EINVAL; mutex_lock(&flash->lock); if (ctrl->id == V4L2_CID_FLASH_FAULT) { s32 fault = 0; unsigned int reg_val = 0; rval = regmap_read(flash->regmap, REG_FLAG1, ®_val); if (rval < 0) goto out; if (reg_val & FAULT_LED0_SHORT_CIRCUIT) fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT; if (reg_val & FAULT_LED1_SHORT_CIRCUIT) fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT; if (reg_val & FAULT_THERMAL_SHUTDOWN) fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE; if (reg_val & FAULT_TIMEOUT) fault |= V4L2_FLASH_FAULT_TIMEOUT; ctrl->cur.val = fault; } out: mutex_unlock(&flash->lock); return rval; } static int aw36518_set_ctrl(struct v4l2_ctrl *ctrl, enum aw36518_led_id led_no) { struct aw36518_flash *flash = to_aw36518_flash(ctrl, led_no); int rval = -EINVAL; pr_info("%s led:%d ID:%d", __func__, led_no, ctrl->id); mutex_lock(&flash->lock); switch (ctrl->id) { case V4L2_CID_FLASH_LED_MODE: flash->led_mode = ctrl->val; if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) rval = aw36518_mode_ctrl(flash); else rval = 0; if (flash->led_mode == V4L2_FLASH_LED_MODE_NONE) aw36518_enable_ctrl(flash, led_no, false); else if (flash->led_mode == V4L2_FLASH_LED_MODE_TORCH) rval = aw36518_enable_ctrl(flash, led_no, true); break; case V4L2_CID_FLASH_STROBE_SOURCE: if (ctrl->val == V4L2_FLASH_STROBE_SOURCE_SOFTWARE) { pr_info("sw ctrl\n"); rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x2C, 0x00); } else if (ctrl->val == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) { pr_info("hw trigger\n"); rval = regmap_update_bits(flash->regmap, REG_ENABLE, 0x2C, 0x24); rval = aw36518_enable_ctrl(flash, led_no, true); } if (rval < 0) goto err_out; break; case V4L2_CID_FLASH_STROBE: if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) { rval = -EBUSY; goto err_out; } flash->led_mode = V4L2_FLASH_LED_MODE_FLASH; rval = aw36518_mode_ctrl(flash); rval = aw36518_enable_ctrl(flash, led_no, true); break; case V4L2_CID_FLASH_STROBE_STOP: if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) { rval = -EBUSY; goto err_out; } aw36518_enable_ctrl(flash, led_no, false); flash->led_mode = V4L2_FLASH_LED_MODE_NONE; rval = aw36518_mode_ctrl(flash); break; case V4L2_CID_FLASH_TIMEOUT: rval = aw36518_flash_tout_ctrl(flash, ctrl->val); break; case V4L2_CID_FLASH_INTENSITY: rval = aw36518_flash_brt_ctrl(flash, led_no, ctrl->val); break; case V4L2_CID_FLASH_TORCH_INTENSITY: rval = aw36518_torch_brt_ctrl(flash, led_no, ctrl->val); break; } err_out: mutex_unlock(&flash->lock); return rval; } static int aw36518_led1_get_ctrl(struct v4l2_ctrl *ctrl) { return aw36518_get_ctrl(ctrl, AW36518_LED1); } static int aw36518_led1_set_ctrl(struct v4l2_ctrl *ctrl) { return aw36518_set_ctrl(ctrl, AW36518_LED1); } static int aw36518_led0_get_ctrl(struct v4l2_ctrl *ctrl) { return aw36518_get_ctrl(ctrl, AW36518_LED0); } static int aw36518_led0_set_ctrl(struct v4l2_ctrl *ctrl) { return aw36518_set_ctrl(ctrl, AW36518_LED0); } static const struct v4l2_ctrl_ops aw36518_led_ctrl_ops[AW36518_LED_MAX] = { [AW36518_LED0] = { .g_volatile_ctrl = aw36518_led0_get_ctrl, .s_ctrl = aw36518_led0_set_ctrl, }, [AW36518_LED1] = { .g_volatile_ctrl = aw36518_led1_get_ctrl, .s_ctrl = aw36518_led1_set_ctrl, } }; static int aw36518_init_controls(struct aw36518_flash *flash, enum aw36518_led_id led_no) { struct v4l2_ctrl *fault; u32 max_flash_brt = flash->pdata->max_flash_brt[led_no]; u32 max_torch_brt = flash->pdata->max_torch_brt[led_no]; struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no]; const struct v4l2_ctrl_ops *ops = &aw36518_led_ctrl_ops[led_no]; v4l2_ctrl_handler_init(hdl, 8); /* flash mode */ v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE, V4L2_FLASH_LED_MODE_TORCH, ~0x7, V4L2_FLASH_LED_MODE_NONE); flash->led_mode = V4L2_FLASH_LED_MODE_NONE; /* flash source */ v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE, 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE); /* flash strobe */ v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0); /* flash strobe stop */ v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0); /* flash strobe timeout */ v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT, AW36518_FLASH_TOUT_MIN, flash->pdata->max_flash_timeout, AW36518_FLASH_TOUT_STEP, flash->pdata->max_flash_timeout); /* flash brt */ v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY, AW36518_FLASH_BRT_MIN, max_flash_brt, AW36518_FLASH_BRT_STEP, max_flash_brt); /* torch brt */ v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY, AW36518_TORCH_BRT_MIN, max_torch_brt, AW36518_TORCH_BRT_STEP, max_torch_brt); /* fault */ fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0, V4L2_FLASH_FAULT_OVER_VOLTAGE | V4L2_FLASH_FAULT_OVER_TEMPERATURE | V4L2_FLASH_FAULT_SHORT_CIRCUIT | V4L2_FLASH_FAULT_TIMEOUT, 0, 0); if (fault != NULL) fault->flags |= V4L2_CTRL_FLAG_VOLATILE; if (hdl->error) return hdl->error; if (led_no < 0 || led_no >= AW36518_LED_MAX) { pr_info("led_no error\n"); return -1; } flash->subdev_led[led_no].ctrl_handler = hdl; return 0; } /* initialize device */ static const struct v4l2_subdev_ops aw36518_ops = { .core = NULL, }; static const struct regmap_config aw36518_regmap = { .reg_bits = 8, .val_bits = 8, .max_register = 0xFF, }; static void aw36518_v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, const struct v4l2_subdev_ops *ops) { int ret = 0; v4l2_subdev_init(sd, ops); sd->flags |= V4L2_SUBDEV_FL_IS_I2C; /* the owner is the same as the i2c_client's driver owner */ sd->owner = client->dev.driver->owner; sd->dev = &client->dev; /* i2c_client and v4l2_subdev point to one another */ v4l2_set_subdevdata(sd, client); i2c_set_clientdata(client, sd); /* initialize name */ ret = snprintf(sd->name, sizeof(sd->name), "%s %d-%04x", client->dev.driver->name, i2c_adapter_id(client->adapter), client->addr); if (ret < 0) pr_info("snprintf failed\n"); } static int aw36518_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) { pr_info("%s\n", __func__); aw36518_set_driver(1); return 0; } static int aw36518_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) { pr_info("%s\n", __func__); aw36518_set_driver(0); return 0; } static const struct v4l2_subdev_internal_ops aw36518_int_ops = { .open = aw36518_open, .close = aw36518_close, }; static int aw36518_subdev_init(struct aw36518_flash *flash, enum aw36518_led_id led_no, char *led_name) { struct i2c_client *client = to_i2c_client(flash->dev); struct device_node *np = flash->dev->of_node, *child; const char *fled_name = "flash"; int rval; // pr_info("%s %d", __func__, led_no); if (led_no < 0 || led_no >= AW36518_LED_MAX) { pr_info("led_no error\n"); return -1; } aw36518_v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &aw36518_ops); flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; flash->subdev_led[led_no].internal_ops = &aw36518_int_ops; strscpy(flash->subdev_led[led_no].name, led_name, sizeof(flash->subdev_led[led_no].name)); for (child = of_get_child_by_name(np, fled_name); child; child = of_find_node_by_name(child, fled_name)) { int rv; u32 reg = 0; rv = of_property_read_u32(child, "reg", ®); if (rv) continue; if (reg == led_no) { flash->dnode[led_no] = child; flash->subdev_led[led_no].fwnode = of_fwnode_handle(flash->dnode[led_no]); } } rval = aw36518_init_controls(flash, led_no); if (rval) goto err_out; rval = media_entity_pads_init(&flash->subdev_led[led_no].entity, 0, NULL); if (rval < 0) goto err_out; flash->subdev_led[led_no].entity.function = MEDIA_ENT_F_FLASH; rval = v4l2_async_register_subdev(&flash->subdev_led[led_no]); if (rval < 0) goto err_out; return rval; err_out: v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]); return rval; } /* flashlight init */ static int aw36518_init(struct aw36518_flash *flash) { int rval = 0; unsigned int reg_val; // aw36518_pinctrl_set(flash, AW36518_PINCTRL_PIN_HWEN, AW36518_PINCTRL_PINSTATE_HIGH); rval = regmap_read(flash->regmap, REG_FLAG2, ®_val); if((reg_val&0x02) != 0){ pr_info("aw36518 reset OVP flag\n"); rval = regmap_update_bits(flash->regmap, REG_FLAG2, 0x02, 0x00); } /* set timeout */ rval = aw36518_flash_tout_ctrl(flash, 400); if (rval < 0) return rval; /* output disable */ flash->led_mode = V4L2_FLASH_LED_MODE_NONE; rval = aw36518_mode_ctrl(flash); aw36518_torch_brt_ctrl(flash, AW36518_LED0, flash->ori_current[AW36518_LED0]); aw36518_torch_brt_ctrl(flash, AW36518_LED1, flash->ori_current[AW36518_LED1]); aw36518_flash_brt_ctrl(flash, AW36518_LED0, flash->ori_current[AW36518_LED0]); aw36518_flash_brt_ctrl(flash, AW36518_LED1, flash->ori_current[AW36518_LED1]); /* reset faults */ rval = regmap_read(flash->regmap, REG_FLAG1, ®_val); return rval; } /* flashlight uninit */ static int aw36518_uninit(struct aw36518_flash *flash) { aw36518_pinctrl_set(flash, AW36518_PINCTRL_PIN_HWEN, AW36518_PINCTRL_PINSTATE_LOW); return 0; } static int aw36518_flash_open(void) { return 0; } static int aw36518_flash_release(void) { return 0; } static int aw36518_ioctl(unsigned int cmd, unsigned long arg) { struct flashlight_dev_arg *fl_arg; int channel; fl_arg = (struct flashlight_dev_arg *)arg; channel = fl_arg->channel; switch (cmd) { case FLASH_IOC_SET_ONOFF: pr_info_ratelimited("FLASH_IOC_SET_ONOFF(%d): %d\n", channel, (int)fl_arg->arg); if ((int)fl_arg->arg) { aw36518_torch_brt_ctrl(aw36518_flash_data, channel, 25000); aw36518_flash_data->led_mode = V4L2_FLASH_LED_MODE_TORCH; aw36518_mode_ctrl(aw36518_flash_data); aw36518_enable_ctrl(aw36518_flash_data, channel, true); } else { if (aw36518_flash_data->led_mode != V4L2_FLASH_LED_MODE_NONE) { aw36518_flash_data->led_mode = V4L2_FLASH_LED_MODE_NONE; aw36518_mode_ctrl(aw36518_flash_data); aw36518_enable_ctrl(aw36518_flash_data, channel, false); } } 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 aw36518_set_driver(int set) { int ret = 0; /* set chip and usage count */ //mutex_lock(&aw36518_mutex); if (set) { if (!use_count) ret = aw36518_init(aw36518_flash_data); use_count++; pr_debug("Set driver: %d\n", use_count); } else { use_count--; if (!use_count) ret = aw36518_uninit(aw36518_flash_data); if (use_count < 0) use_count = 0; pr_debug("Unset driver: %d\n", use_count); } //mutex_unlock(&aw36518_mutex); return 0; } static ssize_t aw36518_strobe_store(struct flashlight_arg arg) { aw36518_set_driver(1); //aw36518_set_level(arg.channel, arg.level); //aw36518_timeout_ms[arg.channel] = 0; //aw36518_enable(arg.channel); aw36518_torch_brt_ctrl(aw36518_flash_data, arg.channel, arg.level * 25000); aw36518_enable_ctrl(aw36518_flash_data, arg.channel, true); aw36518_flash_data->led_mode = V4L2_FLASH_LED_MODE_TORCH; aw36518_mode_ctrl(aw36518_flash_data); msleep(arg.dur); //aw36518_disable(arg.channel); aw36518_flash_data->led_mode = V4L2_FLASH_LED_MODE_NONE; aw36518_mode_ctrl(aw36518_flash_data); aw36518_enable_ctrl(aw36518_flash_data, arg.channel, false); aw36518_set_driver(0); return 0; } #if Enable_Cooling static int aw36518_cooling_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state) { struct aw36518_flash *flash = cdev->devdata; *state = flash->max_state; return 0; } static int aw36518_cooling_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state) { struct aw36518_flash *flash = cdev->devdata; *state = flash->target_state; return 0; } static int aw36518_cooling_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) { struct aw36518_flash *flash = cdev->devdata; int ret = 0; /* Request state should be less than max_state */ if (state > flash->max_state) state = flash->max_state; if (flash->target_state == state) return 0; flash->target_state = state; pr_info("set thermal current:%lu\n", flash->target_state); if (flash->target_state == 0) { flash->need_cooler = 0; flash->target_current[AW36518_LED0] = AW36518_FLASH_BRT_MAX; flash->target_current[AW36518_LED1] = AW36518_FLASH_BRT_MAX; ret = aw36518_torch_brt_ctrl(flash, AW36518_LED0, AW36518_TORCH_BRT_MAX); ret = aw36518_torch_brt_ctrl(flash, AW36518_LED1, AW36518_TORCH_BRT_MAX); } else { flash->need_cooler = 1; flash->target_current[AW36518_LED0] = flash_state_to_current_limit[flash->target_state - 1]; flash->target_current[AW36518_LED1] = flash_state_to_current_limit[flash->target_state - 1]; ret = aw36518_torch_brt_ctrl(flash, AW36518_LED0, flash->target_current[AW36518_LED0]); ret = aw36518_torch_brt_ctrl(flash, AW36518_LED1, flash->target_current[AW36518_LED1]); } return ret; } static struct thermal_cooling_device_ops aw36518_cooling_ops = { .get_max_state = aw36518_cooling_get_max_state, .get_cur_state = aw36518_cooling_get_cur_state, .set_cur_state = aw36518_cooling_set_cur_state, }; #endif static struct flashlight_operations aw36518_flash_ops = { aw36518_flash_open, aw36518_flash_release, aw36518_ioctl, aw36518_strobe_store, aw36518_set_driver }; static int aw36518_parse_dt(struct aw36518_flash *flash) { struct device_node *np, *cnp; struct device *dev = flash->dev; u32 decouple = 0; int i = 0, ret = 0; if (!dev || !dev->of_node) return -ENODEV; np = dev->of_node; for_each_child_of_node(np, cnp) { if (of_property_read_u32(cnp, "type", &flash->flash_dev_id[i].type)) goto err_node_put; if (of_property_read_u32(cnp, "ct", &flash->flash_dev_id[i].ct)) goto err_node_put; if (of_property_read_u32(cnp, "part", &flash->flash_dev_id[i].part)) goto err_node_put; ret = snprintf(flash->flash_dev_id[i].name, FLASHLIGHT_NAME_SIZE, flash->subdev_led[i].name); if (ret < 0) pr_info("snprintf failed\n"); flash->flash_dev_id[i].channel = i; flash->flash_dev_id[i].decouple = decouple; pr_info("Parse dt (type,ct,part,name,channel,decouple)=(%d,%d,%d,%s,%d,%d).\n", flash->flash_dev_id[i].type, flash->flash_dev_id[i].ct, flash->flash_dev_id[i].part, flash->flash_dev_id[i].name, flash->flash_dev_id[i].channel, flash->flash_dev_id[i].decouple); if (flashlight_dev_register_by_device_id(&flash->flash_dev_id[i], &aw36518_flash_ops)) return -EFAULT; i++; } return 0; err_node_put: of_node_put(cnp); return -EINVAL; } static int aw36518_probe(struct i2c_client *client, const struct i2c_device_id *devid) { struct aw36518_flash *flash; struct aw36518_platform_data *pdata = dev_get_platdata(&client->dev); int rval; pr_info("%s:%d", __func__, __LINE__); flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL); if (flash == NULL) return -ENOMEM; flash->regmap = devm_regmap_init_i2c(client, &aw36518_regmap); if (IS_ERR(flash->regmap)) { rval = PTR_ERR(flash->regmap); return rval; } /* if there is no platform data, use chip default value */ if (pdata == NULL) { pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); if (pdata == NULL) return -ENODEV; pdata->max_flash_timeout = AW36518_FLASH_TOUT_MAX; /* led 1 */ pdata->max_flash_brt[AW36518_LED0] = AW36518_FLASH_BRT_MAX; pdata->max_torch_brt[AW36518_LED0] = AW36518_TORCH_BRT_MAX; /* led 2 */ pdata->max_flash_brt[AW36518_LED1] = AW36518_FLASH_BRT_MAX; pdata->max_torch_brt[AW36518_LED1] = AW36518_TORCH_BRT_MAX; } flash->pdata = pdata; flash->dev = &client->dev; mutex_init(&flash->lock); aw36518_flash_data = flash; rval = aw36518_pinctrl_init(flash); if (rval < 0) return rval; rval = aw36518_subdev_init(flash, AW36518_LED0, "aw36518-led0"); if (rval < 0) return rval; rval = aw36518_subdev_init(flash, AW36518_LED1, "aw36518-led1"); if (rval < 0) return rval; rval = aw36518_parse_dt(flash); i2c_set_clientdata(client, flash); flash->max_state = AW36518_COOLER_MAX_STATE; flash->target_state = 0; flash->need_cooler = 0; flash->target_current[AW36518_LED0] = AW36518_FLASH_BRT_MAX; flash->target_current[AW36518_LED1] = AW36518_FLASH_BRT_MAX; flash->ori_current[AW36518_LED0] = AW36518_TORCH_BRT_MIN; flash->ori_current[AW36518_LED1] = AW36518_TORCH_BRT_MIN; #if Enable_Cooling flash->cdev = thermal_of_cooling_device_register(client->dev.of_node, "flashlight_cooler", flash, &aw36518_cooling_ops); if (IS_ERR(flash->cdev)) pr_info("register thermal failed\n"); #endif pr_info("%s:%d", __func__, __LINE__); return 0; } static int aw36518_remove(struct i2c_client *client) { struct aw36518_flash *flash = i2c_get_clientdata(client); unsigned int i; thermal_cooling_device_unregister(flash->cdev); for (i = AW36518_LED0; i < AW36518_LED_MAX; i++) { v4l2_device_unregister_subdev(&flash->subdev_led[i]); v4l2_ctrl_handler_free(&flash->ctrls_led[i]); media_entity_cleanup(&flash->subdev_led[i].entity); } return 0; } static const struct i2c_device_id aw36518_id_table[] = { {AW36518_NAME, 0}, {} }; MODULE_DEVICE_TABLE(i2c, aw36518_id_table); static const struct of_device_id aw36518_of_table[] = { { .compatible = "mediatek,aw36518" }, { }, }; MODULE_DEVICE_TABLE(of, aw36518_of_table); static struct i2c_driver aw36518_i2c_driver = { .driver = { .name = AW36518_NAME, .of_match_table = aw36518_of_table, }, .probe = aw36518_probe, .remove = aw36518_remove, .id_table = aw36518_id_table, }; module_i2c_driver(aw36518_i2c_driver); MODULE_AUTHOR("Roger-HY Wang "); MODULE_DESCRIPTION("Texas Instruments AW36518 LED flash driver"); MODULE_LICENSE("GPL");