1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/power_supply.h>
8 /* 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
19 static const 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},
47 * Note that the batres_vs_temp table must be strictly sorted by falling
48 * temperature values to work. Factory resistance is 300 mOhm and the
49 * resistance values to the right are percentages of 300 mOhm.
51 static const struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor
[] = {
52 { .temp
= 40, .resistance
= 40 /* 120 mOhm */ },
53 { .temp
= 30, .resistance
= 45 /* 135 mOhm */ },
54 { .temp
= 20, .resistance
= 55 /* 165 mOhm */ },
55 { .temp
= 10, .resistance
= 77 /* 230 mOhm */ },
56 { .temp
= 00, .resistance
= 108 /* 325 mOhm */ },
57 { .temp
= -10, .resistance
= 158 /* 445 mOhm */ },
58 { .temp
= -20, .resistance
= 198 /* 595 mOhm */ },
61 static struct power_supply_maintenance_charge_table ab8500_maint_charg_table
[] = {
63 /* Maintenance charging phase A, 60 hours */
64 .charge_current_max_ua
= 400000,
65 .charge_voltage_max_uv
= 4050000,
66 .charge_safety_timer_minutes
= 60*60,
69 /* Maintenance charging phase B, 200 hours */
70 .charge_current_max_ua
= 400000,
71 .charge_voltage_max_uv
= 4000000,
72 .charge_safety_timer_minutes
= 200*60,
76 static const struct ab8500_bm_capacity_levels cap_levels
= {
84 static const struct ab8500_fg_parameters fg
= {
85 .recovery_sleep_timer
= 10,
86 .recovery_total_time
= 100,
88 .init_discard_time
= 5,
89 .init_total_time
= 40,
93 .high_curr_threshold_ua
= 50000,
94 .lowbat_threshold_uv
= 3100000,
95 .battok_falling_th_sel0
= 2860,
96 .battok_raising_th_sel1
= 2860,
100 .pcut_max_time
= 127,
101 .pcut_flag_time
= 112,
102 .pcut_max_restart
= 15,
103 .pcut_debounce_time
= 2,
106 static const struct ab8500_maxim_parameters ab8500_maxi_params
= {
108 .chg_curr_ua
= 910000,
110 .charger_curr_step_ua
= 100000,
113 static const struct ab8500_bm_charger_parameters chg
= {
114 .usb_volt_max_uv
= 5500000,
115 .usb_curr_max_ua
= 1500000,
116 .ac_volt_max_uv
= 7500000,
117 .ac_curr_max_ua
= 1500000,
120 /* This is referenced directly in the charger code */
121 struct ab8500_bm_data ab8500_bm_data
= {
122 .main_safety_tmr_h
= 4,
123 .temp_interval_chg
= 20,
124 .temp_interval_nochg
= 120,
125 .usb_safety_tmr_h
= 4,
126 .bkup_bat_v
= BUP_VCH_SEL_2P6V
,
127 .bkup_bat_i
= BUP_ICH_SEL_150UA
,
128 .capacity_scaling
= false,
129 .chg_unknown_bat
= false,
130 .enable_overshoot
= false,
132 .cap_levels
= &cap_levels
,
133 .interval_charging
= 5,
134 .interval_not_charging
= 120,
135 .maxi
= &ab8500_maxi_params
,
140 int ab8500_bm_of_probe(struct power_supply
*psy
,
141 struct ab8500_bm_data
*bm
)
143 struct power_supply_battery_info
*bi
;
144 struct device
*dev
= &psy
->dev
;
147 ret
= power_supply_get_battery_info(psy
, &bm
->bi
);
149 dev_err(dev
, "cannot retrieve battery info\n");
154 /* Fill in defaults for any data missing from the device tree */
155 if (bi
->charge_full_design_uah
< 0)
156 /* The default capacity is 612 mAh for unknown batteries */
157 bi
->charge_full_design_uah
= 612000;
160 * All of these voltages need to be specified or we will simply
161 * fall back to safe defaults.
163 if ((bi
->voltage_min_design_uv
< 0) ||
164 (bi
->voltage_max_design_uv
< 0)) {
165 /* Nominal voltage is 3.7V for unknown batteries */
166 bi
->voltage_min_design_uv
= 3700000;
167 /* Termination voltage 4.05V */
168 bi
->voltage_max_design_uv
= 4050000;
171 if (bi
->constant_charge_current_max_ua
< 0)
172 bi
->constant_charge_current_max_ua
= 400000;
174 if (bi
->constant_charge_voltage_max_uv
< 0)
175 bi
->constant_charge_voltage_max_uv
= 4100000;
177 if (bi
->charge_term_current_ua
)
178 /* Charging stops when we drop below this current */
179 bi
->charge_term_current_ua
= 200000;
181 if (!bi
->maintenance_charge
|| !bi
->maintenance_charge_size
) {
182 bi
->maintenance_charge
= ab8500_maint_charg_table
;
183 bi
->maintenance_charge_size
= ARRAY_SIZE(ab8500_maint_charg_table
);
186 if (bi
->alert_low_temp_charge_current_ua
< 0 ||
187 bi
->alert_low_temp_charge_voltage_uv
< 0)
189 bi
->alert_low_temp_charge_current_ua
= 300000;
190 bi
->alert_low_temp_charge_voltage_uv
= 4000000;
192 if (bi
->alert_high_temp_charge_current_ua
< 0 ||
193 bi
->alert_high_temp_charge_voltage_uv
< 0)
195 bi
->alert_high_temp_charge_current_ua
= 300000;
196 bi
->alert_high_temp_charge_voltage_uv
= 4000000;
200 * Internal resistance and factory resistance are tightly coupled
201 * so both MUST be defined or we fall back to defaults.
203 if ((bi
->factory_internal_resistance_uohm
< 0) ||
205 bi
->factory_internal_resistance_uohm
= 300000;
206 bi
->resist_table
= temp_to_batres_tbl_thermistor
;
207 bi
->resist_table_size
= ARRAY_SIZE(temp_to_batres_tbl_thermistor
);
210 /* The default battery is emulated by a resistor at 7K */
211 if (bi
->bti_resistance_ohm
< 0 ||
212 bi
->bti_resistance_tolerance
< 0) {
213 bi
->bti_resistance_ohm
= 7000;
214 bi
->bti_resistance_tolerance
= 20;
217 if (!bi
->ocv_table
[0]) {
218 /* Default capacity table at say 25 degrees Celsius */
219 bi
->ocv_temp
[0] = 25;
220 bi
->ocv_table
[0] = ocv_cap_tbl
;
221 bi
->ocv_table_size
[0] = ARRAY_SIZE(ocv_cap_tbl
);
224 if (bi
->temp_min
== INT_MIN
)
225 bi
->temp_min
= AB8500_TEMP_UNDER
;
226 if (bi
->temp_max
== INT_MAX
)
227 bi
->temp_max
= AB8500_TEMP_OVER
;
228 if (bi
->temp_alert_min
== INT_MIN
)
229 bi
->temp_alert_min
= AB8500_TEMP_LOW
;
230 if (bi
->temp_alert_max
== INT_MAX
)
231 bi
->temp_alert_max
= AB8500_TEMP_HIGH
;
232 bm
->temp_hysteresis
= AB8500_TEMP_HYSTERESIS
;
237 void ab8500_bm_of_remove(struct power_supply
*psy
,
238 struct ab8500_bm_data
*bm
)
240 power_supply_put_battery_info(psy
, bm
->bi
);