ab8500_bmdata.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/power_supply.h>
  4. #include <linux/of.h>
  5. #include "ab8500-bm.h"
  6. /* Default: under this temperature, charging is stopped */
  7. #define AB8500_TEMP_UNDER 3
  8. /* Default: between this temp and AB8500_TEMP_UNDER charging is reduced */
  9. #define AB8500_TEMP_LOW 8
  10. /* Default: between this temp and AB8500_TEMP_OVER charging is reduced */
  11. #define AB8500_TEMP_HIGH 43
  12. /* Default: over this temp, charging is stopped */
  13. #define AB8500_TEMP_OVER 48
  14. /* Default: temperature hysteresis */
  15. #define AB8500_TEMP_HYSTERESIS 3
  16. static struct power_supply_battery_ocv_table ocv_cap_tbl[] = {
  17. { .ocv = 4186000, .capacity = 100},
  18. { .ocv = 4163000, .capacity = 99},
  19. { .ocv = 4114000, .capacity = 95},
  20. { .ocv = 4068000, .capacity = 90},
  21. { .ocv = 3990000, .capacity = 80},
  22. { .ocv = 3926000, .capacity = 70},
  23. { .ocv = 3898000, .capacity = 65},
  24. { .ocv = 3866000, .capacity = 60},
  25. { .ocv = 3833000, .capacity = 55},
  26. { .ocv = 3812000, .capacity = 50},
  27. { .ocv = 3787000, .capacity = 40},
  28. { .ocv = 3768000, .capacity = 30},
  29. { .ocv = 3747000, .capacity = 25},
  30. { .ocv = 3730000, .capacity = 20},
  31. { .ocv = 3705000, .capacity = 15},
  32. { .ocv = 3699000, .capacity = 14},
  33. { .ocv = 3684000, .capacity = 12},
  34. { .ocv = 3672000, .capacity = 9},
  35. { .ocv = 3657000, .capacity = 7},
  36. { .ocv = 3638000, .capacity = 6},
  37. { .ocv = 3556000, .capacity = 4},
  38. { .ocv = 3424000, .capacity = 2},
  39. { .ocv = 3317000, .capacity = 1},
  40. { .ocv = 3094000, .capacity = 0},
  41. };
  42. /*
  43. * Note that the batres_vs_temp table must be strictly sorted by falling
  44. * temperature values to work. Factory resistance is 300 mOhm and the
  45. * resistance values to the right are percentages of 300 mOhm.
  46. */
  47. static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = {
  48. { .temp = 40, .resistance = 40 /* 120 mOhm */ },
  49. { .temp = 30, .resistance = 45 /* 135 mOhm */ },
  50. { .temp = 20, .resistance = 55 /* 165 mOhm */ },
  51. { .temp = 10, .resistance = 77 /* 230 mOhm */ },
  52. { .temp = 00, .resistance = 108 /* 325 mOhm */ },
  53. { .temp = -10, .resistance = 158 /* 445 mOhm */ },
  54. { .temp = -20, .resistance = 198 /* 595 mOhm */ },
  55. };
  56. static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = {
  57. {
  58. /* Maintenance charging phase A, 60 hours */
  59. .charge_current_max_ua = 400000,
  60. .charge_voltage_max_uv = 4050000,
  61. .charge_safety_timer_minutes = 60*60,
  62. },
  63. {
  64. /* Maintenance charging phase B, 200 hours */
  65. .charge_current_max_ua = 400000,
  66. .charge_voltage_max_uv = 4000000,
  67. .charge_safety_timer_minutes = 200*60,
  68. }
  69. };
  70. static const struct ab8500_bm_capacity_levels cap_levels = {
  71. .critical = 2,
  72. .low = 10,
  73. .normal = 70,
  74. .high = 95,
  75. .full = 100,
  76. };
  77. static const struct ab8500_fg_parameters fg = {
  78. .recovery_sleep_timer = 10,
  79. .recovery_total_time = 100,
  80. .init_timer = 1,
  81. .init_discard_time = 5,
  82. .init_total_time = 40,
  83. .high_curr_time = 60,
  84. .accu_charging = 30,
  85. .accu_high_curr = 30,
  86. .high_curr_threshold_ua = 50000,
  87. .lowbat_threshold_uv = 3100000,
  88. .battok_falling_th_sel0 = 2860,
  89. .battok_raising_th_sel1 = 2860,
  90. .maint_thres = 95,
  91. .user_cap_limit = 15,
  92. .pcut_enable = 1,
  93. .pcut_max_time = 127,
  94. .pcut_flag_time = 112,
  95. .pcut_max_restart = 15,
  96. .pcut_debounce_time = 2,
  97. };
  98. static const struct ab8500_maxim_parameters ab8500_maxi_params = {
  99. .ena_maxi = true,
  100. .chg_curr_ua = 910000,
  101. .wait_cycles = 10,
  102. .charger_curr_step_ua = 100000,
  103. };
  104. static const struct ab8500_bm_charger_parameters chg = {
  105. .usb_volt_max_uv = 5500000,
  106. .usb_curr_max_ua = 1500000,
  107. .ac_volt_max_uv = 7500000,
  108. .ac_curr_max_ua = 1500000,
  109. };
  110. /* This is referenced directly in the charger code */
  111. struct ab8500_bm_data ab8500_bm_data = {
  112. .main_safety_tmr_h = 4,
  113. .temp_interval_chg = 20,
  114. .temp_interval_nochg = 120,
  115. .usb_safety_tmr_h = 4,
  116. .bkup_bat_v = BUP_VCH_SEL_2P6V,
  117. .bkup_bat_i = BUP_ICH_SEL_150UA,
  118. .capacity_scaling = false,
  119. .chg_unknown_bat = false,
  120. .enable_overshoot = false,
  121. .fg_res = 100,
  122. .cap_levels = &cap_levels,
  123. .interval_charging = 5,
  124. .interval_not_charging = 120,
  125. .maxi = &ab8500_maxi_params,
  126. .chg_params = &chg,
  127. .fg_params = &fg,
  128. };
  129. int ab8500_bm_of_probe(struct power_supply *psy,
  130. struct ab8500_bm_data *bm)
  131. {
  132. struct power_supply_battery_info *bi;
  133. struct device *dev = &psy->dev;
  134. int ret;
  135. ret = power_supply_get_battery_info(psy, &bm->bi);
  136. if (ret) {
  137. dev_err(dev, "cannot retrieve battery info\n");
  138. return ret;
  139. }
  140. bi = bm->bi;
  141. /* Fill in defaults for any data missing from the device tree */
  142. if (bi->charge_full_design_uah < 0)
  143. /* The default capacity is 612 mAh for unknown batteries */
  144. bi->charge_full_design_uah = 612000;
  145. /*
  146. * All of these voltages need to be specified or we will simply
  147. * fall back to safe defaults.
  148. */
  149. if ((bi->voltage_min_design_uv < 0) ||
  150. (bi->voltage_max_design_uv < 0)) {
  151. /* Nominal voltage is 3.7V for unknown batteries */
  152. bi->voltage_min_design_uv = 3700000;
  153. /* Termination voltage 4.05V */
  154. bi->voltage_max_design_uv = 4050000;
  155. }
  156. if (bi->constant_charge_current_max_ua < 0)
  157. bi->constant_charge_current_max_ua = 400000;
  158. if (bi->constant_charge_voltage_max_uv < 0)
  159. bi->constant_charge_voltage_max_uv = 4100000;
  160. if (bi->charge_term_current_ua)
  161. /* Charging stops when we drop below this current */
  162. bi->charge_term_current_ua = 200000;
  163. if (!bi->maintenance_charge || !bi->maintenance_charge_size) {
  164. bi->maintenance_charge = ab8500_maint_charg_table;
  165. bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table);
  166. }
  167. if (bi->alert_low_temp_charge_current_ua < 0 ||
  168. bi->alert_low_temp_charge_voltage_uv < 0)
  169. {
  170. bi->alert_low_temp_charge_current_ua = 300000;
  171. bi->alert_low_temp_charge_voltage_uv = 4000000;
  172. }
  173. if (bi->alert_high_temp_charge_current_ua < 0 ||
  174. bi->alert_high_temp_charge_voltage_uv < 0)
  175. {
  176. bi->alert_high_temp_charge_current_ua = 300000;
  177. bi->alert_high_temp_charge_voltage_uv = 4000000;
  178. }
  179. /*
  180. * Internal resistance and factory resistance are tightly coupled
  181. * so both MUST be defined or we fall back to defaults.
  182. */
  183. if ((bi->factory_internal_resistance_uohm < 0) ||
  184. !bi->resist_table) {
  185. bi->factory_internal_resistance_uohm = 300000;
  186. bi->resist_table = temp_to_batres_tbl_thermistor;
  187. bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor);
  188. }
  189. /* The default battery is emulated by a resistor at 7K */
  190. if (bi->bti_resistance_ohm < 0 ||
  191. bi->bti_resistance_tolerance < 0) {
  192. bi->bti_resistance_ohm = 7000;
  193. bi->bti_resistance_tolerance = 20;
  194. }
  195. if (!bi->ocv_table[0]) {
  196. /* Default capacity table at say 25 degrees Celsius */
  197. bi->ocv_temp[0] = 25;
  198. bi->ocv_table[0] = ocv_cap_tbl;
  199. bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl);
  200. }
  201. if (bi->temp_min == INT_MIN)
  202. bi->temp_min = AB8500_TEMP_UNDER;
  203. if (bi->temp_max == INT_MAX)
  204. bi->temp_max = AB8500_TEMP_OVER;
  205. if (bi->temp_alert_min == INT_MIN)
  206. bi->temp_alert_min = AB8500_TEMP_LOW;
  207. if (bi->temp_alert_max == INT_MAX)
  208. bi->temp_alert_max = AB8500_TEMP_HIGH;
  209. bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS;
  210. return 0;
  211. }
  212. void ab8500_bm_of_remove(struct power_supply *psy,
  213. struct ab8500_bm_data *bm)
  214. {
  215. power_supply_put_battery_info(psy, bm->bi);
  216. }