// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2018 MediaTek Inc. * */ #include #include #include #include #include #include #include /* #include */ #include #include #include #include #include #ifdef CONFIG_MTK_AAL_SUPPORT #include /* #include */ #endif #include #ifdef CONFIG_MTK_PWM #include static unsigned int backlight_PWM_div_hal = CLK_DIV1; #else #define CLK_DIV1 1 #endif #ifdef CONFIG_MTK_PMIC_NEW_ARCH #include #endif #include "mtk_leds_sw.h" #include "mtk_leds_hal.h" #include "ddp_pwm.h" #include "mtkfb.h" //#define MET_USER_EVENT_SUPPORT #ifdef MET_USER_EVENT_SUPPORT #include #endif #undef pr_fmt #define pr_fmt(fmt) KBUILD_MODNAME " %s(%d) :" fmt, __func__, __LINE__ static DEFINE_MUTEX(leds_mutex); static DEFINE_MUTEX(leds_pmic_mutex); /**************************************************************************** * variables ***************************************************************************/ /* struct cust_mt65xx_led* bl_setting_hal = NULL; */ static unsigned int bl_brightness_hal = 102; static unsigned int bl_duty_hal = 21; static unsigned int bl_div_hal = CLK_DIV1; static unsigned int bl_frequency_hal = 32000; /* for button led don't do ISINK disable first time */ static int button_flag_isink1; struct wakeup_source leds_suspend_lock; char *leds_name[MT65XX_LED_TYPE_TOTAL] = { "red", "green", "blue", "jogball-backlight", "keyboard-backlight", "button-backlight", "lcd-backlight", }; struct cust_mt65xx_led *pled_dtsi; /*****************PWM *************************************************/ #define PWM_DIV_NUM 8 static unsigned int div_array_hal[PWM_DIV_NUM] = { 1, 2, 4, 8, 16, 32, 64, 128 }; /**************************************************************************** * func:return global variables ***************************************************************************/ static unsigned long long current_t, last_time; static int count; static char buffer[4096] = "[BL] Set Backlight directly "; static void backlight_debug_log(int level, int mappingLevel) { unsigned long cur_time_mod = 0; unsigned long long cur_time_display = 0; int ret = 0; current_t = sched_clock(); cur_time_display = current_t; do_div(cur_time_display, 1000000); cur_time_mod = do_div(cur_time_display, 1000); ret = snprintf(buffer + strlen(buffer), 4095 - strlen(buffer), "T:%lld.%ld,L:%d map:%d ", cur_time_display, cur_time_mod, level, mappingLevel); count++; if (ret < 0 || ret >= 4096) { pr_info("print log error!"); count = 5; } if (level == 0 || count >= 5 || (current_t - last_time) > 1000000000) { pr_info("%s", buffer); count = 0; buffer[strlen("[BL] Set Backlight directly ")] = '\0'; } last_time = sched_clock(); } void mt_leds_wake_lock_init(void) { // wakeup_source_init(&leds_suspend_lock, "leds wakelock"); } unsigned int mt_get_bl_brightness(void) { return bl_brightness_hal; } unsigned int mt_get_bl_duty(void) { return bl_duty_hal; } unsigned int mt_get_bl_div(void) { return bl_div_hal; } unsigned int mt_get_bl_frequency(void) { return bl_frequency_hal; } unsigned int *mt_get_div_array(void) { return &div_array_hal[0]; } void mt_set_bl_duty(unsigned int level) { bl_duty_hal = level; } void mt_set_bl_div(unsigned int div) { bl_div_hal = div; } void mt_set_bl_frequency(unsigned int freq) { bl_frequency_hal = freq; } struct cust_mt65xx_led *get_cust_led_dtsi(void) { struct device_node *led_node = NULL; bool isSupportDTS = false; int i, ret; int mode, data; int pwm_config[5] = { 0 }; if (pled_dtsi) goto out; /* this can allocat an new struct array */ pled_dtsi = kmalloc_array(MT65XX_LED_TYPE_TOTAL, sizeof(struct cust_mt65xx_led), GFP_KERNEL); if (pled_dtsi == NULL) goto out; for (i = 0; i < MT65XX_LED_TYPE_TOTAL; i++) { char node_name[32] = "mediatek,"; if (strlen(node_name) + strlen(leds_name[i]) + 1 > sizeof(node_name)) { pr_info("buffer for %s%s not enough\n", node_name, leds_name[i]); pled_dtsi[i].mode = 0; pled_dtsi[i].data = -1; continue; } pled_dtsi[i].name = leds_name[i]; led_node = of_find_compatible_node(NULL, NULL, strncat(node_name, leds_name[i], sizeof(node_name) - strlen(node_name) - 1)); if (!led_node) { pr_debug("Cannot find LED node from dts\n"); pled_dtsi[i].mode = 0; pled_dtsi[i].data = -1; } else { isSupportDTS = true; ret = of_property_read_u32(led_node, "led_mode", &mode); if (!ret) { pled_dtsi[i].mode = mode; pr_info ("The %s's led mode is : %d\n", pled_dtsi[i].name, pled_dtsi[i].mode); } else { pr_info ("led dts can not get led mode"); pled_dtsi[i].mode = 0; } ret = of_property_read_u32(led_node, "data", &data); if (!ret) { pled_dtsi[i].data = data; pr_info ("The %s's led data is : %ld\n", pled_dtsi[i].name, pled_dtsi[i].data); } else { pr_info ("led dts can not get led data"); pled_dtsi[i].data = -1; } ret = of_property_read_u32_array(led_node, "pwm_config", pwm_config, ARRAY_SIZE (pwm_config)); if (!ret) { pr_debug("%s's pwm cfg %d %d %d %d %d\n", pled_dtsi[i].name, pwm_config[0], pwm_config[1], pwm_config[2], pwm_config[3], pwm_config[4]); pled_dtsi[i].config_data.clock_source = pwm_config[0]; pled_dtsi[i].config_data.div = pwm_config[1]; pled_dtsi[i].config_data.low_duration = pwm_config[2]; pled_dtsi[i].config_data.High_duration = pwm_config[3]; pled_dtsi[i].config_data.pmic_pad = pwm_config[4]; } else pr_info ("led dts can't get pwm config\n"); switch (pled_dtsi[i].mode) { case MT65XX_LED_MODE_CUST_LCM: #if defined(CONFIG_BACKLIGHT_SUPPORT_LM3697) pled_dtsi[i].data = (long)chargepump_set_backlight_level; pr_info ("BL set by chargepump\n"); #else pled_dtsi[i].data = (long)mtkfb_set_backlight_level; #endif /* CONFIG_BACKLIGHT_SUPPORT_LM3697 */ pr_info ("kernel:the BL hw mode is LCM.\n"); break; case MT65XX_LED_MODE_CUST_BLS_PWM: pled_dtsi[i].data = (long)disp_bls_set_backlight; pr_info ("kernel:the BL hw mode is BLS.\n"); break; default: break; } } } if (!isSupportDTS) { kfree(pled_dtsi); pled_dtsi = NULL; } out: return pled_dtsi; } struct cust_mt65xx_led *mt_get_cust_led_list(void) { struct cust_mt65xx_led *cust_led_list = get_cust_led_dtsi(); return cust_led_list; } /**************************************************************************** * internal functions ***************************************************************************/ static int brightness_mapto64(int level) { if (level < 30) return (level >> 1) + 7; else if (level <= 120) return (level >> 2) + 14; else if (level <= 160) return level / 5 + 20; else return (level >> 3) + 33; } #ifdef CONFIG_MTK_PWM static int time_array_hal[PWM_DIV_NUM] = { 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 }; static int find_time_index(int time) { int index = 0; while (index < 8) { if (time < time_array_hal[index]) return index; index++; } return PWM_DIV_NUM - 1; } #endif int mt_led_set_pwm(int pwm_num, struct nled_setting *led) { #ifdef CONFIG_MTK_PWM struct pwm_spec_config pwm_setting; int time_index = 0; memset(&pwm_setting, 0, sizeof(struct pwm_spec_config)); pwm_setting.pwm_no = pwm_num; pwm_setting.mode = PWM_MODE_OLD; pr_debug("led_set_pwm: mode=%d,pwm_no=%d\n", led->nled_mode, pwm_num); /* We won't choose 32K to be the clock src of old mode because of * system performance. The setting here will be clock src = 26MHz, * CLKSEL = 26M/1625 (i.e. 16K) */ pwm_setting.clk_src = PWM_CLK_OLD_MODE_32K; pwm_setting.pmic_pad = 0; switch (led->nled_mode) { /* Actually, the setting still can not to turn off NLED. We should * disable PWM to turn off NLED. */ case NLED_OFF: pwm_setting.PWM_MODE_OLD_REGS.THRESH = 0; pwm_setting.clk_div = CLK_DIV1; pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 100 / 2; break; case NLED_ON: pwm_setting.PWM_MODE_OLD_REGS.THRESH = 30 / 2; pwm_setting.clk_div = CLK_DIV1; pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 100 / 2; break; case NLED_BLINK: pr_debug("LED blink on time = %d offtime = %d\n", led->blink_on_time, led->blink_off_time); time_index = find_time_index(led->blink_on_time + led->blink_off_time); pr_debug("LED div is %d\n", time_index); pwm_setting.clk_div = time_index; pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = (led->blink_on_time + led->blink_off_time) * MIN_FRE_OLD_PWM / div_array_hal[time_index]; pwm_setting.PWM_MODE_OLD_REGS.THRESH = (led->blink_on_time * 100) / (led->blink_on_time + led->blink_off_time); break; default: pr_debug("Invalid nled mode\n"); return -1; } pwm_setting.PWM_MODE_FIFO_REGS.IDLE_VALUE = 0; pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0; pwm_setting.PWM_MODE_FIFO_REGS.GDURATION = 0; pwm_setting.PWM_MODE_FIFO_REGS.WAVE_NUM = 0; pwm_set_spec_config(&pwm_setting); #endif return 0; } int mt_backlight_set_pwm(int pwm_num, u32 level, u32 div, struct PWM_config *config_data) { #ifdef CONFIG_MTK_PWM struct pwm_spec_config pwm_setting; unsigned int BacklightLevelSupport = Cust_GetBacklightLevelSupport_byPWM(); pwm_setting.pwm_no = pwm_num; if (BacklightLevelSupport == BACKLIGHT_LEVEL_PWM_256_SUPPORT) pwm_setting.mode = PWM_MODE_OLD; else pwm_setting.mode = PWM_MODE_FIFO; pwm_setting.pmic_pad = config_data->pmic_pad; if (config_data->div) { pwm_setting.clk_div = config_data->div; backlight_PWM_div_hal = config_data->div; } else pwm_setting.clk_div = div; if (BacklightLevelSupport == BACKLIGHT_LEVEL_PWM_256_SUPPORT) { /* PWM_CLK_OLD_MODE_32K is block/1625 = 26M/1625 = 16KHz @ MT6571 */ if (config_data->clock_source) pwm_setting.clk_src = PWM_CLK_OLD_MODE_BLOCK; else pwm_setting.clk_src = PWM_CLK_OLD_MODE_32K; pwm_setting.PWM_MODE_OLD_REGS.IDLE_VALUE = 0; pwm_setting.PWM_MODE_OLD_REGS.GUARD_VALUE = 0; pwm_setting.PWM_MODE_OLD_REGS.GDURATION = 0; pwm_setting.PWM_MODE_OLD_REGS.WAVE_NUM = 0; /* 256 level */ pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH = 255; pwm_setting.PWM_MODE_OLD_REGS.THRESH = level; pr_debug("[LEDS][%d]backlight_set_pwm:duty is %d/%d\n", BacklightLevelSupport, level, pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH); pr_debug("[LEDS][%d]backlight_set_pwm:clk_src/div is %d%d\n", BacklightLevelSupport, pwm_setting.clk_src, pwm_setting.clk_div); if (level > 0 && level < 256) { pwm_set_spec_config(&pwm_setting); pr_debug ("[LEDS][%d]old mode: thres/data_width is %d/%d\n", BacklightLevelSupport, pwm_setting.PWM_MODE_OLD_REGS.THRESH, pwm_setting.PWM_MODE_OLD_REGS.DATA_WIDTH); } else { pr_debug("[LEDS][%d]Error level in backlight\n", BacklightLevelSupport); mt_pwm_disable(pwm_setting.pwm_no, config_data->pmic_pad); } return 0; } else { if (config_data->clock_source) { pwm_setting.clk_src = PWM_CLK_NEW_MODE_BLOCK; } else { pwm_setting.clk_src = PWM_CLK_NEW_MODE_BLOCK_DIV_BY_1625; } if (config_data->High_duration && config_data->low_duration) { pwm_setting.PWM_MODE_FIFO_REGS.HDURATION = config_data->High_duration; pwm_setting.PWM_MODE_FIFO_REGS.LDURATION = pwm_setting.PWM_MODE_FIFO_REGS.HDURATION; } else { pwm_setting.PWM_MODE_FIFO_REGS.HDURATION = 4; pwm_setting.PWM_MODE_FIFO_REGS.LDURATION = 4; } pwm_setting.PWM_MODE_FIFO_REGS.IDLE_VALUE = 0; pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0; pwm_setting.PWM_MODE_FIFO_REGS.STOP_BITPOS_VALUE = 31; pwm_setting.PWM_MODE_FIFO_REGS.GDURATION = (pwm_setting.PWM_MODE_FIFO_REGS.HDURATION + 1) * 32 - 1; pwm_setting.PWM_MODE_FIFO_REGS.WAVE_NUM = 0; pr_debug("%s :duty is %d\n", __func__, level); pr_debug ("[LEDS]clk_src/div/high/low is %d%d%d%d\n", pwm_setting.clk_src, pwm_setting.clk_div, pwm_setting.PWM_MODE_FIFO_REGS.HDURATION, pwm_setting.PWM_MODE_FIFO_REGS.LDURATION); if (level > 0 && level <= 32) { pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 0; pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA0 = (1 << level) - 1; pwm_set_spec_config(&pwm_setting); } else if (level > 32 && level <= 64) { pwm_setting.PWM_MODE_FIFO_REGS.GUARD_VALUE = 1; level -= 32; pwm_setting.PWM_MODE_FIFO_REGS.SEND_DATA0 = (1 << level) - 1; pwm_set_spec_config(&pwm_setting); } else { pr_debug("[LEDS]Error level in backlight\n"); mt_pwm_disable(pwm_setting.pwm_no, config_data->pmic_pad); } return 0; } #endif return 0; } void mt_led_pwm_disable(int pwm_num) { #ifdef CONFIG_MTK_PWM struct cust_mt65xx_led *cust_led_list = get_cust_led_dtsi(); mt_pwm_disable(pwm_num, cust_led_list->config_data.pmic_pad); #endif } void mt_backlight_set_pwm_duty(int pwm_num, u32 level, u32 div, struct PWM_config *config_data) { mt_backlight_set_pwm(pwm_num, level, div, config_data); } void mt_backlight_set_pwm_div(int pwm_num, u32 level, u32 div, struct PWM_config *config_data) { mt_backlight_set_pwm(pwm_num, level, div, config_data); } void mt_backlight_get_pwm_fsel(unsigned int bl_div, unsigned int *bl_frequency) { } void mt_store_pwm_register(unsigned int addr, unsigned int value) { } unsigned int mt_show_pwm_register(unsigned int addr) { return 0; } int mt_brightness_set_pmic(enum mt65xx_led_pmic pmic_type, u32 level, u32 div) { pr_info("not support set brightness by pmic: PMIC#%d:%d\n", pmic_type, level); return -1; } int mt_led_blink_pmic(enum mt65xx_led_pmic pmic_type, struct nled_setting *led) { pr_info("not support set blink by pmic: pmic_type=%d\n", pmic_type); return -1; } int mt_brightness_set_pmic_duty_store(u32 level, u32 div) { return -1; } int mt_mt65xx_led_set_cust(struct cust_mt65xx_led *cust, int level) { struct nled_setting led_tmp_setting = { 0, 0, 0 }; int tmp_level = level; static bool button_flag; unsigned int BacklightLevelSupport = Cust_GetBacklightLevelSupport_byPWM(); switch (cust->mode) { case MT65XX_LED_MODE_PWM: if (strcmp(cust->name, "lcd-backlight") == 0) { bl_brightness_hal = level; if (level == 0) { #ifdef CONFIG_MTK_PWM mt_pwm_disable(cust->data, cust->config_data.pmic_pad); #endif } else { if (BacklightLevelSupport == BACKLIGHT_LEVEL_PWM_256_SUPPORT) level = brightness_mapping(tmp_level); else level = brightness_mapto64(tmp_level); mt_backlight_set_pwm(cust->data, level, bl_div_hal, &cust->config_data); } bl_duty_hal = level; } else { if (level == 0) { led_tmp_setting.nled_mode = NLED_OFF; mt_led_set_pwm(cust->data, &led_tmp_setting); #ifdef CONFIG_MTK_PWM mt_pwm_disable(cust->data, cust->config_data.pmic_pad); #endif } else { led_tmp_setting.nled_mode = NLED_ON; mt_led_set_pwm(cust->data, &led_tmp_setting); } } return 1; case MT65XX_LED_MODE_GPIO: pr_debug("brightness_set_cust:go GPIO mode!!!!!\n"); return ((cust_set_brightness) (cust->data)) (level); case MT65XX_LED_MODE_PMIC: /* for button baclight used SINK channel, when set button ISINK, * don't do disable other ISINK channel */ if ((strcmp(cust->name, "button-backlight") == 0)) { if (button_flag == false) { switch (cust->data) { case MT65XX_LED_PMIC_NLED_ISINK1: button_flag_isink1 = 1; break; default: break; } button_flag = true; } } return mt_brightness_set_pmic(cust->data, level, bl_div_hal); case MT65XX_LED_MODE_CUST_LCM: if (strcmp(cust->name, "lcd-backlight") == 0) bl_brightness_hal = level; pr_debug("brightness_set_cust:backlight control by LCM\n"); /* warning for this API revork */ return ((cust_brightness_set) (cust->data)) (level, bl_div_hal); case MT65XX_LED_MODE_CUST_BLS_PWM: if (strcmp(cust->name, "lcd-backlight") == 0) bl_brightness_hal = level; #ifdef MET_USER_EVENT_SUPPORT if (enable_met_backlight_tag()) output_met_backlight_tag(level); #endif return ((cust_set_brightness) (cust->data)) (level); case MT65XX_LED_MODE_NONE: default: break; } return -1; } void mt_mt65xx_led_work(struct work_struct *work) { struct mt65xx_led_data *led_data = container_of(work, struct mt65xx_led_data, work); pr_debug("%s:%d\n", led_data->cust.name, led_data->level); mutex_lock(&leds_mutex); mt_mt65xx_led_set_cust(&led_data->cust, led_data->level); mutex_unlock(&leds_mutex); } void mt_mt65xx_led_set(struct led_classdev *led_cdev, enum led_brightness level) { struct mt65xx_led_data *led_data = container_of(led_cdev, struct mt65xx_led_data, cdev); /* unsigned long flags; */ /* spin_lock_irqsave(&leds_lock, flags); */ #ifdef CONFIG_MTK_AAL_SUPPORT if (led_data->level != level) { led_data->level = level; if (strcmp(led_data->cust.name, "lcd-backlight") != 0) { pr_debug("Set NLED directly %d at time %lu\n", led_data->level, jiffies); schedule_work(&led_data->work); } else { if (level != 0 && level * CONFIG_LIGHTNESS_MAPPING_VALUE < 255) { level = 1; } else { level = (level * CONFIG_LIGHTNESS_MAPPING_VALUE) / 255; } backlight_debug_log(led_data->level, level); disp_pq_notify_backlight_changed((((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1) * level + 127) / 255); disp_aal_notify_backlight_changed((((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1) * level + 127) / 255); } } #else /* do something only when level is changed */ if (led_data->level != level) { led_data->level = level; if (strcmp(led_data->cust.name, "lcd-backlight") != 0) { pr_debug("Set NLED directly %d at time %lu\n", led_data->level, jiffies); schedule_work(&led_data->work); } else { if (level != 0 && level * CONFIG_LIGHTNESS_MAPPING_VALUE < 255) { level = 1; } else { level = (level * CONFIG_LIGHTNESS_MAPPING_VALUE) / 255; } backlight_debug_log(led_data->level, level); disp_pq_notify_backlight_changed((((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1) * level + 127) / 255); if (led_data->cust.mode == MT65XX_LED_MODE_CUST_BLS_PWM) mt_mt65xx_led_set_cust(&led_data->cust, ((((1 << MT_LED_INTERNAL_LEVEL_BIT_CNT) - 1) * level + 127) / 255)); else mt_mt65xx_led_set_cust(&led_data->cust, level); } } /* spin_unlock_irqrestore(&leds_lock, flags); */ #endif /* if(0!=aee_kernel_Powerkey_is_press()) */ /* aee_kernel_wdt_kick_Powkey_api("mt_mt65xx_led_set",WDT_SETBY_Backlight); */ } int mt_mt65xx_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on, unsigned long *delay_off) { struct mt65xx_led_data *led_data = container_of(led_cdev, struct mt65xx_led_data, cdev); static int got_wake_lock; struct nled_setting nled_tmp_setting = { 0, 0, 0 }; /* only allow software blink when delay_on or delay_off changed */ if (*delay_on != led_data->delay_on || *delay_off != led_data->delay_off) { led_data->delay_on = *delay_on; led_data->delay_off = *delay_off; if (led_data->delay_on && led_data->delay_off) { led_data->level = 255; /* AP PWM all support OLD mode */ if (led_data->cust.mode == MT65XX_LED_MODE_PWM) { nled_tmp_setting.nled_mode = NLED_BLINK; nled_tmp_setting.blink_off_time = led_data->delay_off; nled_tmp_setting.blink_on_time = led_data->delay_on; mt_led_set_pwm(led_data->cust.data, &nled_tmp_setting); return 0; } else if ((led_data->cust.mode == MT65XX_LED_MODE_PMIC) && (led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK1)) { nled_tmp_setting.nled_mode = NLED_BLINK; nled_tmp_setting.blink_off_time = led_data->delay_off; nled_tmp_setting.blink_on_time = led_data->delay_on; mt_led_blink_pmic(led_data->cust.data, &nled_tmp_setting); return 0; } else if (!got_wake_lock) { __pm_stay_awake(&leds_suspend_lock); got_wake_lock = 1; } } else if (!led_data->delay_on && !led_data->delay_off) { /* AP PWM all support OLD mode */ if (led_data->cust.mode == MT65XX_LED_MODE_PWM) { nled_tmp_setting.nled_mode = NLED_OFF; mt_led_set_pwm(led_data->cust.data, &nled_tmp_setting); return 0; } else if ((led_data->cust.mode == MT65XX_LED_MODE_PMIC) && (led_data->cust.data == MT65XX_LED_PMIC_NLED_ISINK1)) { mt_brightness_set_pmic(led_data->cust.data, 0, 0); return 0; } else if (got_wake_lock) { __pm_relax(&leds_suspend_lock); got_wake_lock = 0; } } return -1; } /* delay_on and delay_off are not changed */ return 0; }