12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/device.h>
18 #include <linux/module.h>
26 #define DA9030_FAULT_LOG 0x0a
27 #define DA9030_FAULT_LOG_OVER_TEMP (1 << 7)
28 #define DA9030_FAULT_LOG_VBAT_OVER (1 << 4)
30 #define DA9030_CHARGE_CONTROL 0x28
31 #define DA9030_CHRG_CHARGER_ENABLE (1 << 7)
33 #define DA9030_ADC_MAN_CONTROL 0x30
34 #define DA9030_ADC_TBATREF_ENABLE (1 << 5)
35 #define DA9030_ADC_LDO_INT_ENABLE (1 << 4)
37 #define DA9030_ADC_AUTO_CONTROL 0x31
38 #define DA9030_ADC_TBAT_ENABLE (1 << 5)
39 #define DA9030_ADC_VBAT_IN_TXON (1 << 4)
40 #define DA9030_ADC_VCH_ENABLE (1 << 3)
41 #define DA9030_ADC_ICH_ENABLE (1 << 2)
42 #define DA9030_ADC_VBAT_ENABLE (1 << 1)
43 #define DA9030_ADC_AUTO_SLEEP_ENABLE (1 << 0)
45 #define DA9030_VBATMON 0x32
46 #define DA9030_VBATMONTXON 0x33
47 #define DA9030_TBATHIGHP 0x34
48 #define DA9030_TBATHIGHN 0x35
49 #define DA9030_TBATLOW 0x36
51 #define DA9030_VBAT_RES 0x41
52 #define DA9030_VBATMIN_RES 0x42
53 #define DA9030_VBATMINTXON_RES 0x43
54 #define DA9030_ICHMAX_RES 0x44
55 #define DA9030_ICHMIN_RES 0x45
56 #define DA9030_ICHAVERAGE_RES 0x46
57 #define DA9030_VCHMAX_RES 0x47
58 #define DA9030_VCHMIN_RES 0x48
59 #define DA9030_TBAT_RES 0x49
122 static inline int da9030_reg_to_mV(
int reg)
124 return ((reg * 2650) >> 8) + 2650;
127 static inline int da9030_millivolt_to_reg(
int mV)
129 return ((mV - 2650) << 8) / 2650;
132 static inline int da9030_reg_to_mA(
int reg)
134 return ((reg * 24000) >> 8) / 15;
137 #ifdef CONFIG_DEBUG_FS
143 if (charger->
chdet) {
145 charger->
mA, charger->
mV);
149 charger->
adc.vbat_res,
150 da9030_reg_to_mV(charger->
adc.vbat_res));
152 charger->
adc.vbatmin_res,
153 da9030_reg_to_mV(charger->
adc.vbatmin_res));
155 charger->
adc.vbatmintxon,
156 da9030_reg_to_mV(charger->
adc.vbatmintxon));
158 charger->
adc.ichmax_res,
159 da9030_reg_to_mV(charger->
adc.ichmax_res));
161 charger->
adc.ichmin_res,
162 da9030_reg_to_mA(charger->
adc.ichmin_res));
164 charger->
adc.ichaverage_res,
165 da9030_reg_to_mA(charger->
adc.ichaverage_res));
167 charger->
adc.vchmax_res,
168 da9030_reg_to_mA(charger->
adc.vchmax_res));
170 charger->
adc.vchmin_res,
171 da9030_reg_to_mV(charger->
adc.vchmin_res));
191 charger, &bat_debug_fops);
195 static void da9030_bat_remove_debugfs(
struct da9030_charger *charger)
204 static inline void da9030_bat_remove_debugfs(
struct da9030_charger *charger)
209 static inline void da9030_read_adc(
struct da9030_charger *charger,
216 static void da9030_charger_update_state(
struct da9030_charger *charger)
222 charger->
mA = ((val >> 3) & 0xf) * 100;
223 charger->
mV = (val & 0x7) * 50 + 4000;
225 da9030_read_adc(charger, &charger->
adc);
231 static void da9030_set_charge(
struct da9030_charger *charger,
int on)
250 static void da9030_charger_check_state(
struct da9030_charger *charger)
252 da9030_charger_update_state(charger);
255 if (!charger->
is_on) {
256 if ((charger->
chdet) &&
257 (charger->
adc.vbat_res <
259 da9030_set_charge(charger, 1);
263 if (!charger->
chdet) {
264 da9030_set_charge(charger, 0);
268 if (charger->
adc.vbat_res >=
270 da9030_set_charge(charger, 0);
273 }
else if (charger->
adc.vbat_res >
281 if (charger->
adc.vchmax_res > charger->
thresholds.vcharge_max ||
287 da9030_set_charge(charger, 0);
298 da9030_charger_check_state(charger);
315 static void da9030_battery_check_status(
struct da9030_charger *charger,
318 if (charger->
chdet) {
328 static void da9030_battery_check_health(
struct da9030_charger *charger,
348 da9030_battery_check_status(charger, val);
351 da9030_battery_check_health(charger, val);
363 val->
intval = da9030_reg_to_mV(charger->
adc.vbat_res) * 1000;
367 da9030_reg_to_mA(charger->
adc.ichaverage_res) * 1000;
379 static void da9030_battery_vbat_event(
struct da9030_charger *charger)
381 da9030_read_adc(charger, &charger->
adc);
392 }
else if (charger->
adc.vbat_res <
412 da9030_battery_vbat_event(charger);
416 da9030_set_charge(charger, 0);
423 static void da9030_battery_convert_thresholds(
struct da9030_charger *charger,
431 da9030_millivolt_to_reg(pdata->
vbat_low);
433 da9030_millivolt_to_reg(pdata->
vbat_crit);
447 static void da9030_battery_setup_psy(
struct da9030_charger *charger)
461 static int da9030_battery_charger_init(
struct da9030_charger *charger)
507 charger = kzalloc(
sizeof(*charger),
GFP_KERNEL);
524 da9030_battery_convert_thresholds(charger, pdata);
526 ret = da9030_battery_charger_init(charger);
528 goto err_charger_init;
533 charger->
nb.notifier_call = da9030_battery_event;
542 da9030_battery_setup_psy(charger);
545 goto err_ps_register;
547 charger->
debug_file = da9030_bat_create_debugfs(charger);
548 platform_set_drvdata(pdev, charger);
568 da9030_bat_remove_debugfs(charger);
574 da9030_set_charge(charger, 0);
584 .name =
"da903x-battery",
587 .probe = da9030_battery_probe,
588 .remove = da9030_battery_remove,