blob: 7ae95f53758012a5b997815d1221499a1ef2de37 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08002#include <linux/export.h>
3#include <linux/power_supply.h>
4#include <linux/of.h>
Linus Walleij417c0fc2021-03-12 09:36:02 +01005
6#include "ab8500-bm.h"
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08007
Linus Walleij6252c702021-11-20 16:53:13 +01008/* Default: under this temperature, charging is stopped */
9#define AB8500_TEMP_UNDER 3
10/* Default: between this temp and AB8500_TEMP_UNDER charging is reduced */
11#define AB8500_TEMP_LOW 8
12/* Default: between this temp and AB8500_TEMP_OVER charging is reduced */
13#define AB8500_TEMP_HIGH 43
14/* Default: over this temp, charging is stopped */
15#define AB8500_TEMP_OVER 48
16/* Default: temperature hysteresis */
17#define AB8500_TEMP_HYSTERESIS 3
18
Linus Walleij0525f342021-11-20 16:53:26 +010019static struct power_supply_battery_ocv_table ocv_cap_tbl[] = {
20 { .ocv = 4186000, .capacity = 100},
21 { .ocv = 4163000, .capacity = 99},
22 { .ocv = 4114000, .capacity = 95},
23 { .ocv = 4068000, .capacity = 90},
24 { .ocv = 3990000, .capacity = 80},
25 { .ocv = 3926000, .capacity = 70},
26 { .ocv = 3898000, .capacity = 65},
27 { .ocv = 3866000, .capacity = 60},
28 { .ocv = 3833000, .capacity = 55},
29 { .ocv = 3812000, .capacity = 50},
30 { .ocv = 3787000, .capacity = 40},
31 { .ocv = 3768000, .capacity = 30},
32 { .ocv = 3747000, .capacity = 25},
33 { .ocv = 3730000, .capacity = 20},
34 { .ocv = 3705000, .capacity = 15},
35 { .ocv = 3699000, .capacity = 14},
36 { .ocv = 3684000, .capacity = 12},
37 { .ocv = 3672000, .capacity = 9},
38 { .ocv = 3657000, .capacity = 7},
39 { .ocv = 3638000, .capacity = 6},
40 { .ocv = 3556000, .capacity = 4},
41 { .ocv = 3424000, .capacity = 2},
42 { .ocv = 3317000, .capacity = 1},
43 { .ocv = 3094000, .capacity = 0},
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080044};
45
46/*
47 * Note that the res_to_temp table must be strictly sorted by falling
48 * resistance values to work.
49 */
Linus Walleij484a9cc2021-07-13 17:27:07 +020050static const struct ab8500_res_to_temp temp_tbl[] = {
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080051 {-5, 214834},
52 { 0, 162943},
53 { 5, 124820},
54 {10, 96520},
55 {15, 75306},
56 {20, 59254},
57 {25, 47000},
58 {30, 37566},
59 {35, 30245},
60 {40, 24520},
61 {45, 20010},
62 {50, 16432},
63 {55, 13576},
64 {60, 11280},
65 {65, 9425},
66};
67
68/*
69 * Note that the batres_vs_temp table must be strictly sorted by falling
Linus Walleij67acb292021-11-20 16:53:25 +010070 * temperature values to work. Factory resistance is 300 mOhm and the
71 * resistance values to the right are percentages of 300 mOhm.
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080072 */
Linus Walleij67acb292021-11-20 16:53:25 +010073static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = {
74 { .temp = 40, .resistance = 40 /* 120 mOhm */ },
75 { .temp = 30, .resistance = 45 /* 135 mOhm */ },
76 { .temp = 20, .resistance = 55 /* 165 mOhm */ },
77 { .temp = 10, .resistance = 77 /* 230 mOhm */ },
78 { .temp = 00, .resistance = 108 /* 325 mOhm */ },
79 { .temp = -10, .resistance = 158 /* 445 mOhm */ },
80 { .temp = -20, .resistance = 198 /* 595 mOhm */ },
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080081};
82
Linus Walleije5dff302021-11-20 16:53:15 +010083/* Default battery type for reference designs is the unknown type */
84static struct ab8500_battery_type bat_type_thermistor_unknown = {
Linus Walleije5dff302021-11-20 16:53:15 +010085 .resis_high = 0,
86 .resis_low = 0,
Linus Walleije5dff302021-11-20 16:53:15 +010087 .maint_a_cur_lvl = 400,
88 .maint_a_vol_lvl = 4050,
89 .maint_a_chg_timer_h = 60,
90 .maint_b_cur_lvl = 400,
91 .maint_b_vol_lvl = 4000,
92 .maint_b_chg_timer_h = 200,
93 .low_high_cur_lvl = 300,
94 .low_high_vol_lvl = 4000,
95 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
96 .r_to_t_tbl = temp_tbl,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -080097};
98
Linus Walleij484a9cc2021-07-13 17:27:07 +020099static const struct ab8500_bm_capacity_levels cap_levels = {
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800100 .critical = 2,
101 .low = 10,
102 .normal = 70,
103 .high = 95,
104 .full = 100,
105};
106
Linus Walleij484a9cc2021-07-13 17:27:07 +0200107static const struct ab8500_fg_parameters fg = {
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800108 .recovery_sleep_timer = 10,
109 .recovery_total_time = 100,
110 .init_timer = 1,
111 .init_discard_time = 5,
112 .init_total_time = 40,
113 .high_curr_time = 60,
114 .accu_charging = 30,
115 .accu_high_curr = 30,
Linus Walleij0525f342021-11-20 16:53:26 +0100116 .high_curr_threshold_ua = 50000,
117 .lowbat_threshold_uv = 3100000,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800118 .battok_falling_th_sel0 = 2860,
119 .battok_raising_th_sel1 = 2860,
Marcus Cooperea402402013-01-11 13:12:54 +0000120 .maint_thres = 95,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800121 .user_cap_limit = 15,
Lee Jones93ff7222012-05-31 16:16:36 +0200122 .pcut_enable = 1,
123 .pcut_max_time = 127,
124 .pcut_flag_time = 112,
125 .pcut_max_restart = 15,
126 .pcut_debounce_time = 2,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800127};
128
Linus Walleij484a9cc2021-07-13 17:27:07 +0200129static const struct ab8500_maxim_parameters ab8500_maxi_params = {
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800130 .ena_maxi = true,
Linus Walleij83e5aa72021-11-20 16:53:23 +0100131 .chg_curr_ua = 910000,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800132 .wait_cycles = 10,
Linus Walleij83e5aa72021-11-20 16:53:23 +0100133 .charger_curr_step_ua = 100000,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800134};
135
Linus Walleij484a9cc2021-07-13 17:27:07 +0200136static const struct ab8500_bm_charger_parameters chg = {
Linus Walleijbc6e0282021-11-20 16:53:24 +0100137 .usb_volt_max_uv = 5500000,
Linus Walleij83e5aa72021-11-20 16:53:23 +0100138 .usb_curr_max_ua = 1500000,
Linus Walleijbc6e0282021-11-20 16:53:24 +0100139 .ac_volt_max_uv = 7500000,
Linus Walleij83e5aa72021-11-20 16:53:23 +0100140 .ac_curr_max_ua = 1500000,
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800141};
142
Linus Walleijd8d26ac2021-11-20 16:53:14 +0100143/* This is referenced directly in the charger code */
Linus Walleij484a9cc2021-07-13 17:27:07 +0200144struct ab8500_bm_data ab8500_bm_data = {
Lee Jones43dc4472012-11-29 15:26:22 +0000145 .main_safety_tmr_h = 4,
146 .temp_interval_chg = 20,
147 .temp_interval_nochg = 120,
148 .usb_safety_tmr_h = 4,
149 .bkup_bat_v = BUP_VCH_SEL_2P6V,
150 .bkup_bat_i = BUP_ICH_SEL_150UA,
151 .no_maintenance = false,
Marcus Cooperea402402013-01-11 13:12:54 +0000152 .capacity_scaling = false,
Linus Walleij484a9cc2021-07-13 17:27:07 +0200153 .adc_therm = AB8500_ADC_THERM_BATCTRL,
Lee Jones43dc4472012-11-29 15:26:22 +0000154 .chg_unknown_bat = false,
155 .enable_overshoot = false,
156 .fg_res = 100,
157 .cap_levels = &cap_levels,
Linus Walleije5dff302021-11-20 16:53:15 +0100158 .bat_type = &bat_type_thermistor_unknown,
Lee Jones43dc4472012-11-29 15:26:22 +0000159 .interval_charging = 5,
160 .interval_not_charging = 120,
Lee Jones43dc4472012-11-29 15:26:22 +0000161 .gnd_lift_resistance = 34,
Lee Jones861a30d2012-08-29 20:36:51 +0800162 .maxi = &ab8500_maxi_params,
Lee Jones43dc4472012-11-29 15:26:22 +0000163 .chg_params = &chg,
164 .fg_params = &fg,
Lee Jones861a30d2012-08-29 20:36:51 +0800165};
166
Linus Walleij59f1b852021-11-20 16:53:11 +0100167int ab8500_bm_of_probe(struct power_supply *psy,
Linus Walleij484a9cc2021-07-13 17:27:07 +0200168 struct ab8500_bm_data *bm)
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800169{
Linus Walleij25fd3302021-12-15 02:01:18 +0100170 struct power_supply_battery_info *bi;
Linus Walleij59f1b852021-11-20 16:53:11 +0100171 struct device *dev = &psy->dev;
172 int ret;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800173
Linus Walleij25fd3302021-12-15 02:01:18 +0100174 ret = power_supply_get_battery_info(psy, &bm->bi);
Linus Walleij59f1b852021-11-20 16:53:11 +0100175 if (ret) {
176 dev_err(dev, "cannot retrieve battery info\n");
177 return ret;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800178 }
Linus Walleij25fd3302021-12-15 02:01:18 +0100179 bi = bm->bi;
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800180
Linus Walleij22be8d72021-11-20 16:53:16 +0100181 /* Fill in defaults for any data missing from the device tree */
182 if (bi->charge_full_design_uah < 0)
183 /* The default capacity is 612 mAh for unknown batteries */
184 bi->charge_full_design_uah = 612000;
Linus Walleij2a5f4182021-11-20 16:53:18 +0100185
186 /*
187 * All of these voltages need to be specified or we will simply
188 * fall back to safe defaults.
189 */
190 if ((bi->voltage_min_design_uv < 0) ||
191 (bi->voltage_max_design_uv < 0) ||
192 (bi->overvoltage_limit_uv < 0)) {
193 /* Nominal voltage is 3.7V for unknown batteries */
194 bi->voltage_min_design_uv = 3700000;
195 bi->voltage_max_design_uv = 3700000;
196 /* Termination voltage (overcharge limit) 4.05V */
197 bi->overvoltage_limit_uv = 4050000;
198 }
199
Linus Walleij83e5aa72021-11-20 16:53:23 +0100200 if (bi->constant_charge_current_max_ua < 0)
201 bi->constant_charge_current_max_ua = 400000;
202
Linus Walleijbc6e0282021-11-20 16:53:24 +0100203 if (bi->constant_charge_voltage_max_uv < 0)
204 bi->constant_charge_voltage_max_uv = 4100000;
205
Linus Walleij9c208992021-11-20 16:53:21 +0100206 if (bi->charge_term_current_ua)
207 /* Charging stops when we drop below this current */
208 bi->charge_term_current_ua = 200000;
209
Linus Walleij67acb292021-11-20 16:53:25 +0100210 /*
211 * Internal resistance and factory resistance are tightly coupled
212 * so both MUST be defined or we fall back to defaults.
213 */
214 if ((bi->factory_internal_resistance_uohm < 0) ||
215 !bi->resist_table) {
Linus Walleij50425cc2021-11-20 16:53:20 +0100216 bi->factory_internal_resistance_uohm = 300000;
Linus Walleij67acb292021-11-20 16:53:25 +0100217 bi->resist_table = temp_to_batres_tbl_thermistor;
218 bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor);
219 }
Linus Walleij50425cc2021-11-20 16:53:20 +0100220
Linus Walleij0525f342021-11-20 16:53:26 +0100221 if (!bi->ocv_table[0]) {
222 /* Default capacity table at say 25 degrees Celsius */
223 bi->ocv_temp[0] = 25;
224 bi->ocv_table[0] = ocv_cap_tbl;
225 bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl);
226 }
227
Linus Walleij6252c702021-11-20 16:53:13 +0100228 if (bi->temp_min == INT_MIN)
229 bi->temp_min = AB8500_TEMP_UNDER;
230 if (bi->temp_max == INT_MAX)
231 bi->temp_max = AB8500_TEMP_OVER;
232 if (bi->temp_alert_min == INT_MIN)
233 bi->temp_alert_min = AB8500_TEMP_LOW;
234 if (bi->temp_alert_max == INT_MAX)
235 bi->temp_alert_max = AB8500_TEMP_HIGH;
236 bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS;
237
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -0800238 return 0;
239}
Linus Walleij6252c702021-11-20 16:53:13 +0100240
241void ab8500_bm_of_remove(struct power_supply *psy,
242 struct ab8500_bm_data *bm)
243{
Linus Walleij25fd3302021-12-15 02:01:18 +0100244 power_supply_put_battery_info(psy, bm->bi);
Linus Walleij6252c702021-11-20 16:53:13 +0100245}