battery-profile-loader.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "%s: " fmt, __func__
  7. #include <linux/err.h>
  8. #include <linux/of.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/power_supply.h>
  13. #include "battery-profile-loader.h"
  14. static int of_batterydata_read_batt_id_kohm(const struct device_node *np,
  15. const char *propname, struct batt_ids *batt_ids)
  16. {
  17. struct property *prop;
  18. const __be32 *data;
  19. int num, i, *id_kohm = batt_ids->kohm;
  20. prop = of_find_property(np, "qcom,batt-id-kohm", NULL);
  21. if (!prop) {
  22. pr_err("%s: No battery id resistor found\n", np->name);
  23. return -EINVAL;
  24. } else if (!prop->value) {
  25. pr_err("%s: No battery id resistor value found, np->name\n",
  26. np->name);
  27. return -ENODATA;
  28. } else if (prop->length > MAX_BATT_ID_NUM * sizeof(__be32)) {
  29. pr_err("%s: Too many battery id resistors\n", np->name);
  30. return -EINVAL;
  31. }
  32. num = prop->length/sizeof(__be32);
  33. batt_ids->num = num;
  34. data = prop->value;
  35. for (i = 0; i < num; i++)
  36. *id_kohm++ = be32_to_cpup(data++);
  37. return 0;
  38. }
  39. struct device_node *of_batterydata_get_best_profile(
  40. const struct device_node *batterydata_container_node,
  41. int batt_id_kohm, const char *batt_type)
  42. {
  43. struct batt_ids batt_ids;
  44. struct device_node *node, *best_node = NULL;
  45. const char *battery_type = NULL;
  46. int delta = 0, best_delta = 0, best_id_kohm = 0, id_range_pct,
  47. i = 0, rc = 0, limit = 0;
  48. bool in_range = false;
  49. /* read battery id range percentage for best profile */
  50. rc = of_property_read_u32(batterydata_container_node,
  51. "qcom,batt-id-range-pct", &id_range_pct);
  52. if (rc) {
  53. if (rc == -EINVAL) {
  54. id_range_pct = 0;
  55. } else {
  56. pr_err("failed to read battery id range\n");
  57. return ERR_PTR(-ENXIO);
  58. }
  59. }
  60. /*
  61. * Find the battery data with a battery id resistor closest to this one
  62. */
  63. for_each_child_of_node(batterydata_container_node, node) {
  64. if (batt_type != NULL) {
  65. rc = of_property_read_string(node, "qcom,battery-type",
  66. &battery_type);
  67. if (!rc && strcmp(battery_type, batt_type) == 0) {
  68. best_node = node;
  69. best_id_kohm = batt_id_kohm;
  70. break;
  71. }
  72. } else {
  73. rc = of_batterydata_read_batt_id_kohm(node,
  74. "qcom,batt-id-kohm",
  75. &batt_ids);
  76. if (rc)
  77. continue;
  78. for (i = 0; i < batt_ids.num; i++) {
  79. delta = abs(batt_ids.kohm[i] - batt_id_kohm);
  80. limit = (batt_ids.kohm[i] * id_range_pct) / 100;
  81. in_range = (delta <= limit);
  82. /*
  83. * Check if the delta is the lowest one
  84. * and also if the limits are in range
  85. * before selecting the best node.
  86. */
  87. if ((delta < best_delta || !best_node)
  88. && in_range) {
  89. best_node = node;
  90. best_delta = delta;
  91. best_id_kohm = batt_ids.kohm[i];
  92. }
  93. }
  94. }
  95. }
  96. if (best_node == NULL) {
  97. pr_err("No battery data found\n");
  98. return best_node;
  99. }
  100. /* check that profile id is in range of the measured batt_id */
  101. if (abs(best_id_kohm - batt_id_kohm) >
  102. ((best_id_kohm * id_range_pct) / 100)) {
  103. pr_err("out of range: profile id %d batt id %d pct %d\n",
  104. best_id_kohm, batt_id_kohm, id_range_pct);
  105. return NULL;
  106. }
  107. rc = of_property_read_string(best_node, "qcom,battery-type",
  108. &battery_type);
  109. if (!rc)
  110. pr_info("%s found\n", battery_type);
  111. else
  112. pr_info("%s found\n", best_node->name);
  113. return best_node;
  114. }
  115. struct device_node *of_batterydata_get_best_aged_profile(
  116. const struct device_node *batterydata_container_node,
  117. int batt_id_kohm, int batt_age_level, int *avail_age_level)
  118. {
  119. struct batt_ids batt_ids;
  120. struct device_node *node, *best_node = NULL;
  121. const char *battery_type = NULL;
  122. int delta = 0, best_id_kohm = 0, id_range_pct, i = 0, rc = 0, limit = 0;
  123. u32 val;
  124. bool in_range = false;
  125. /* read battery id range percentage for best profile */
  126. rc = of_property_read_u32(batterydata_container_node,
  127. "qcom,batt-id-range-pct", &id_range_pct);
  128. if (rc) {
  129. if (rc == -EINVAL) {
  130. id_range_pct = 0;
  131. } else {
  132. pr_err("failed to read battery id range\n");
  133. return ERR_PTR(-ENXIO);
  134. }
  135. }
  136. /*
  137. * Find the battery data with a battery id resistor closest to this one
  138. */
  139. for_each_available_child_of_node(batterydata_container_node, node) {
  140. val = 0;
  141. of_property_read_u32(node, "qcom,batt-age-level", &val);
  142. rc = of_batterydata_read_batt_id_kohm(node,
  143. "qcom,batt-id-kohm", &batt_ids);
  144. if (rc)
  145. continue;
  146. for (i = 0; i < batt_ids.num; i++) {
  147. delta = abs(batt_ids.kohm[i] - batt_id_kohm);
  148. limit = (batt_ids.kohm[i] * id_range_pct) / 100;
  149. in_range = (delta <= limit);
  150. /*
  151. * Check if the battery aging level matches and the
  152. * limits are in range before selecting the best node.
  153. */
  154. if ((batt_age_level == val || !best_node) && in_range) {
  155. best_node = node;
  156. best_id_kohm = batt_ids.kohm[i];
  157. *avail_age_level = val;
  158. break;
  159. }
  160. }
  161. }
  162. if (best_node == NULL) {
  163. pr_err("No battery data found\n");
  164. return best_node;
  165. }
  166. /* check that profile id is in range of the measured batt_id */
  167. if (abs(best_id_kohm - batt_id_kohm) >
  168. ((best_id_kohm * id_range_pct) / 100)) {
  169. pr_err("out of range: profile id %d batt id %d pct %d\n",
  170. best_id_kohm, batt_id_kohm, id_range_pct);
  171. return NULL;
  172. }
  173. rc = of_property_read_string(best_node, "qcom,battery-type",
  174. &battery_type);
  175. if (!rc)
  176. pr_info("%s age level %d found\n", battery_type,
  177. *avail_age_level);
  178. else
  179. pr_info("%s age level %d found\n", best_node->name,
  180. *avail_age_level);
  181. return best_node;
  182. }
  183. int of_batterydata_get_aged_profile_count(
  184. const struct device_node *batterydata_node,
  185. int batt_id_kohm, int *count)
  186. {
  187. struct device_node *node;
  188. int id_range_pct, i = 0, rc = 0, limit = 0, delta = 0;
  189. bool in_range = false;
  190. u32 batt_id;
  191. /* read battery id range percentage for best profile */
  192. rc = of_property_read_u32(batterydata_node,
  193. "qcom,batt-id-range-pct", &id_range_pct);
  194. if (rc) {
  195. if (rc == -EINVAL) {
  196. id_range_pct = 0;
  197. } else {
  198. pr_err("failed to read battery id range\n");
  199. return -ENXIO;
  200. }
  201. }
  202. for_each_available_child_of_node(batterydata_node, node) {
  203. if (!of_find_property(node, "qcom,batt-age-level", NULL))
  204. continue;
  205. if (!of_find_property(node, "qcom,soh-range", NULL))
  206. continue;
  207. rc = of_property_read_u32(node, "qcom,batt-id-kohm", &batt_id);
  208. if (rc)
  209. continue;
  210. delta = abs(batt_id_kohm - batt_id);
  211. limit = (batt_id_kohm * id_range_pct) / 100;
  212. in_range = (delta <= limit);
  213. if (!in_range) {
  214. pr_debug("not in range batt_id: %d\n", batt_id);
  215. continue;
  216. }
  217. i++;
  218. }
  219. if (i <= 1) {
  220. pr_err("Less number of profiles to support SOH\n");
  221. return -EINVAL;
  222. }
  223. *count = i;
  224. return 0;
  225. }
  226. int of_batterydata_read_soh_aged_profiles(
  227. const struct device_node *batterydata_node,
  228. int batt_id_kohm, struct soh_range *soh_data)
  229. {
  230. struct device_node *node;
  231. u32 val, temp[2], i = 0;
  232. int rc, batt_id, id_range_pct, limit = 0, delta = 0;
  233. bool in_range = false;
  234. if (!batterydata_node || !soh_data)
  235. return -ENODEV;
  236. /* read battery id range percentage for best profile */
  237. rc = of_property_read_u32(batterydata_node,
  238. "qcom,batt-id-range-pct", &id_range_pct);
  239. if (rc) {
  240. if (rc == -EINVAL) {
  241. id_range_pct = 0;
  242. } else {
  243. pr_err("failed to read battery id range\n");
  244. return -ENXIO;
  245. }
  246. }
  247. for_each_available_child_of_node(batterydata_node, node) {
  248. rc = of_property_read_u32(node, "qcom,batt-age-level", &val);
  249. if (rc)
  250. continue;
  251. rc = of_property_read_u32(node, "qcom,batt-id-kohm", &batt_id);
  252. if (rc)
  253. continue;
  254. delta = abs(batt_id_kohm - batt_id);
  255. limit = (batt_id_kohm * id_range_pct) / 100;
  256. in_range = (delta <= limit);
  257. if (!in_range) {
  258. pr_debug("not in range batt_id: %d\n", batt_id);
  259. continue;
  260. }
  261. if (!of_find_property(node, "qcom,soh-range", NULL))
  262. continue;
  263. rc = of_property_count_elems_of_size(node, "qcom,soh-range",
  264. sizeof(u32));
  265. if (rc != 2) {
  266. pr_err("Incorrect element size for qcom,soh-range, rc=%d\n",
  267. rc);
  268. return -EINVAL;
  269. }
  270. rc = of_property_read_u32_array(node, "qcom,soh-range", temp,
  271. 2);
  272. if (rc < 0) {
  273. pr_err("Error in reading qcom,soh-range, rc=%d\n", rc);
  274. return rc;
  275. }
  276. if (temp[0] > 100 || temp[1] > 100 || (temp[0] > temp[1])) {
  277. pr_err("Incorrect SOH range [%d %d]\n", temp[0],
  278. temp[1]);
  279. return -ERANGE;
  280. }
  281. pr_debug("batt_age_level: %d soh: [%d %d]\n", val, temp[0],
  282. temp[1]);
  283. soh_data[i].batt_age_level = val;
  284. soh_data[i].soh_min = temp[0];
  285. soh_data[i].soh_max = temp[1];
  286. i++;
  287. }
  288. return 0;
  289. }
  290. MODULE_LICENSE("GPL");