From 2aee554600c99d7da316e16c2bc36b5126bea757 Mon Sep 17 00:00:00 2001 From: yanyh2 Date: Fri, 12 Mar 2021 15:47:25 +0800 Subject: [PATCH] Get the profile id from device tree Get the battery profile id via looking up from a profile id and battery serial number mapping table in device tree, so each product can specify the supported battery profile id and serial number mapping table by its own in device tree. Change-Id: Ide3a5b73e5500c47e1afca2122d6ef082982875d Signed-off-by: yanyh2 Reviewed-on: https://gerrit.mot.com/1899502 SLTApproved: Slta Waiver SME-Granted: SME Approvals Granted Tested-by: Jira Key Reviewed-by: Wentao Xu Reviewed-by: Jianqi Yang Submit-Approved: Jira Key --- .../qti_glink_charger/qti_glink_charger.c | 72 ++++++++++++++++--- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/drivers/power/mmi_charger/qti_glink_charger/qti_glink_charger.c b/drivers/power/mmi_charger/qti_glink_charger/qti_glink_charger.c index c7de8cae6cb4..4aadb0fe27f9 100644 --- a/drivers/power/mmi_charger/qti_glink_charger/qti_glink_charger.c +++ b/drivers/power/mmi_charger/qti_glink_charger/qti_glink_charger.c @@ -39,9 +39,10 @@ #define OEM_WAIT_TIME_MS 5000 +#define BATT_DEFAULT_ID 107000 #define BATT_SN_UNKNOWN "unknown-sn" -static bool debug_enabled = true; +static bool debug_enabled; module_param(debug_enabled, bool, 0600); MODULE_PARM_DESC(debug_enabled, "Enable debug for qti glink charger driver"); @@ -145,20 +146,68 @@ struct qti_charger { bool *debug_enabled; }; -static int find_profile_id(const char *batt_sn) +static int find_profile_id(struct qti_charger *chg) { int i; + int rc; + int count; + int profile_id = -EINVAL; struct profile_sn_map { - int id; + const char *id; const char *sn; - } map_table[] = { - {0, BATT_SN_UNKNOWN}, - }; + } *map_table; - i = sizeof(map_table) / sizeof(struct profile_sn_map); - while (i > 0 && map_table[--i].sn && strcmp(map_table[i].sn, batt_sn)); + count = of_property_count_strings(chg->dev->of_node, "profile-ids-map"); + if (count <= 0 || (count % 2)) { + mmi_err(chg, "Invalid profile-ids-map in DT, rc=%d\n", count); + return -EINVAL; + } - return map_table[i].id; + map_table = devm_kmalloc_array(chg->dev, count / 2, + sizeof(struct profile_sn_map), + GFP_KERNEL); + if (!map_table) + return -ENOMEM; + + rc = of_property_read_string_array(chg->dev->of_node, "profile-ids-map", + (const char **)map_table, + count); + if (rc < 0) { + mmi_err(chg, "Failed to get profile-ids-map, rc=%d\n", rc); + profile_id = rc; + goto free_map; + } + + for (i = 0; i < count / 2 && map_table[i].sn; i++) { + mmi_info(chg, "profile_ids_map[%d]: id=%s, sn=%s\n", i, + map_table[i].id, map_table[i].sn); + if (!strcmp(map_table[i].sn, chg->batt_info.batt_sn)) + profile_id = i; + } + + if (profile_id >= 0 && profile_id < count / 2) { + i = profile_id; + profile_id = 0; + rc = kstrtou32(map_table[i].id, 0, &profile_id); + if (rc) { + mmi_err(chg, "Invalid id: %s, sn: %s\n", + map_table[i].id, + map_table[i].sn); + profile_id = rc; + } else { + mmi_info(chg, "profile id: %s(%d), sn: %s\n", + map_table[i].id, + profile_id, + map_table[i].sn); + } + } else { + mmi_warn(chg, "No matched profile id in profile-ids-map\n"); + } + +free_map: + devm_kfree(chg->dev, map_table); + + return profile_id; } static int handle_oem_read_ack(struct qti_charger *chg, void *data, size_t len) @@ -744,7 +793,10 @@ static int qti_charger_parse_dt(struct qti_charger *chg) } else { strcpy(chg->batt_info.batt_sn, dev_sn); } - chg->profile_info.profile_id = find_profile_id(chg->batt_info.batt_sn); + + chg->profile_info.profile_id = find_profile_id(chg); + if (chg->profile_info.profile_id < 0) + chg->profile_info.profile_id = BATT_DEFAULT_ID; rc = of_property_read_u32(node, "mmi,chrg-iterm-ma", &chg->profile_info.chrg_iterm);