14 #include <linux/module.h>
15 #include <linux/device.h>
18 #include <linux/slab.h>
31 #define BTEMP_THERMAL_LOW_LIMIT -10
32 #define BTEMP_THERMAL_MED_LIMIT 0
33 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
34 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
35 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
37 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
38 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
40 #define to_ab8500_btemp_device_info(x) container_of((x), \
41 struct ab8500_btemp, btemp_psy);
137 static int ab8500_btemp_batctrl_volt_to_res(
struct ab8500_btemp *di,
138 int v_batctrl,
int inst_curr)
142 if (is_ab8500_1p1_or_earlier(di->
parent)) {
147 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
155 rbs = (v_batctrl * 1000
156 - di->
bat->gnd_lift_resistance * inst_curr)
163 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
175 static int ab8500_btemp_read_batctrl_voltage(
struct ab8500_btemp *di)
183 "%s gpadc conversion failed, using previous value",
198 static int ab8500_btemp_curr_source_enable(
struct ab8500_btemp *di,
208 if (is_ab8500_1p1_or_earlier(di->
parent))
224 dev_err(di->
dev,
"%s failed setting cmp_force\n",
240 dev_err(di->
dev,
"%s failed enabling current source\n",
242 goto disable_curr_source;
245 dev_dbg(di->
dev,
"Disable BATCTRL curr source\n");
253 dev_err(di->
dev,
"%s failed disabling current source\n",
255 goto disable_curr_source;
264 dev_err(di->
dev,
"%s failed enabling PU and comp\n",
281 dev_err(di->
dev,
"%s failed disabling force comp\n",
283 goto disable_force_comp;
299 dev_err(di->
dev,
"%s failed disabling current source\n",
310 dev_err(di->
dev,
"%s failed enabling PU and comp\n",
328 dev_err(di->
dev,
"%s failed disabling force comp\n",
343 static int ab8500_btemp_get_batctrl_res(
struct ab8500_btemp *di)
355 ret = ab8500_btemp_curr_source_enable(di,
true);
357 dev_err(di->
dev,
"%s curr source enabled failed\n", __func__);
371 dev_err(di->
dev,
"Failed to start current measurement\n");
385 batctrl += ab8500_btemp_read_batctrl_voltage(di);
393 dev_err(di->
dev,
"Failed to finalize current measurement\n");
397 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
399 ret = ab8500_btemp_curr_source_enable(di,
false);
401 dev_err(di->
dev,
"%s curr source disable failed\n", __func__);
405 dev_dbg(di->
dev,
"%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
406 __func__, batctrl, res, inst_curr, i);
421 static int ab8500_btemp_res_to_temp(
struct ab8500_btemp *di,
431 if (res > tbl[0].resist)
433 else if (res <= tbl[tbl_size - 1].resist)
437 while (!(res <= tbl[i].resist &&
438 res > tbl[i + 1].resist))
443 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
453 static int ab8500_btemp_measure_temp(
struct ab8500_btemp *di)
457 int rbat, rntc, vntc;
460 id = di->
bat->batt_id;
465 rbat = ab8500_btemp_get_batctrl_res(di);
467 dev_err(di->
dev,
"%s get batctrl res failed\n",
476 temp = ab8500_btemp_res_to_temp(di,
477 di->
bat->bat_type[
id].r_to_t_tbl,
478 di->
bat->bat_type[
id].n_temp_tbl_elements, rbat);
483 "%s gpadc conversion failed,"
484 " using previous value\n", __func__);
491 rntc = 230000 * vntc / (
VTVOUT_V - vntc);
493 temp = ab8500_btemp_res_to_temp(di,
494 di->
bat->bat_type[
id].r_to_t_tbl,
495 di->
bat->bat_type[
id].n_temp_tbl_elements, rntc);
498 dev_dbg(di->
dev,
"Battery temperature is %d\n", temp);
518 res = ab8500_btemp_get_batctrl_res(di);
520 dev_err(di->
dev,
"%s get batctrl res failed\n", __func__);
526 if ((res <= di->
bat->bat_type[i].resis_high) &&
527 (res >= di->
bat->bat_type[i].resis_low)) {
529 " low %d < res %d < high: %d"
532 "BATCTRL" :
"BATTEMP",
533 di->
bat->bat_type[i].resis_low, res,
534 di->
bat->bat_type[i].resis_high, i);
536 di->
bat->batt_id =
i;
543 ", resistance %d Ohm\n", res);
552 di->
bat->batt_id == 1) {
553 dev_dbg(di->
dev,
"Set BATCTRL current source to 20uA\n");
557 return di->
bat->batt_id;
572 di->
bat_temp = ab8500_btemp_measure_temp(di);
580 interval = di->
bat->temp_interval_chg;
582 interval = di->
bat->temp_interval_nochg;
597 static irqreturn_t ab8500_btemp_batctrlindb_handler(
int irq,
void *_di)
600 dev_err(di->
dev,
"Battery removal detected!\n");
602 di->
events.batt_rem =
true;
615 static irqreturn_t ab8500_btemp_templow_handler(
int irq,
void *_di)
619 if (is_ab8500_2p0_or_earlier(di->
parent)) {
621 " for ABB cut 1.0, 1.1 and 2.0\n");
623 dev_crit(di->
dev,
"Battery temperature lower than -10deg c\n");
625 di->
events.btemp_low =
true;
626 di->
events.btemp_high =
false;
627 di->
events.btemp_medhigh =
false;
628 di->
events.btemp_lowmed =
false;
642 static irqreturn_t ab8500_btemp_temphigh_handler(
int irq,
void *_di)
646 dev_crit(di->
dev,
"Battery temperature is higher than MAX temp\n");
648 di->
events.btemp_high =
true;
649 di->
events.btemp_medhigh =
false;
650 di->
events.btemp_lowmed =
false;
651 di->
events.btemp_low =
false;
664 static irqreturn_t ab8500_btemp_lowmed_handler(
int irq,
void *_di)
668 dev_dbg(di->
dev,
"Battery temperature is between low and medium\n");
670 di->
events.btemp_lowmed =
true;
671 di->
events.btemp_medhigh =
false;
672 di->
events.btemp_high =
false;
673 di->
events.btemp_low =
false;
686 static irqreturn_t ab8500_btemp_medhigh_handler(
int irq,
void *_di)
690 dev_dbg(di->
dev,
"Battery temperature is between medium and high\n");
692 di->
events.btemp_medhigh =
true;
693 di->
events.btemp_lowmed =
false;
694 di->
events.btemp_high =
false;
695 di->
events.btemp_low =
false;
709 static void ab8500_btemp_periodic(
struct ab8500_btemp *di,
712 dev_dbg(di->
dev,
"Enable periodic temperature measurements: %d\n",
730 static int ab8500_btemp_get_temp(
struct ab8500_btemp *di)
738 if (is_ab8500_2p0_or_earlier(di->
parent)) {
741 if (di->
events.btemp_low) {
746 }
else if (di->
events.btemp_high) {
751 }
else if (di->
events.btemp_lowmed) {
756 }
else if (di->
events.btemp_medhigh) {
792 static int ab8500_btemp_get_property(
struct power_supply *psy,
812 val->
intval = ab8500_btemp_get_temp(di);
820 static int ab8500_btemp_get_ext_psy_data(
struct device *
dev,
void *
data)
827 bool psy_found =
false;
858 if (!ret.intval && di->
events.ac_conn) {
859 di->
events.ac_conn =
false;
862 else if (ret.intval && !di->
events.ac_conn) {
863 di->
events.ac_conn =
true;
865 ab8500_btemp_periodic(di,
true);
870 if (!ret.intval && di->
events.usb_conn) {
871 di->
events.usb_conn =
false;
874 else if (ret.intval && !di->
events.usb_conn) {
875 di->
events.usb_conn =
true;
877 ab8500_btemp_periodic(di,
true);
900 static void ab8500_btemp_external_power_changed(
struct power_supply *psy)
905 &di->
btemp_psy, ab8500_btemp_get_ext_psy_data);
910 {
"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
911 {
"BTEMP_LOW", ab8500_btemp_templow_handler},
912 {
"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
913 {
"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
914 {
"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
917 #if defined(CONFIG_PM)
922 ab8500_btemp_periodic(di,
true);
932 ab8500_btemp_periodic(di,
false);
937 #define ab8500_btemp_suspend NULL
938 #define ab8500_btemp_resume NULL
947 for (i = 0; i <
ARRAY_SIZE(ab8500_btemp_irq); i++) {
957 platform_set_drvdata(pdev,
NULL);
987 dev_err(di->
dev,
"no btemp platform data supplied\n");
989 goto free_device_info;
995 dev_err(di->
dev,
"no battery platform data supplied\n");
997 goto free_device_info;
1003 di->
btemp_psy.properties = ab8500_btemp_props;
1005 di->
btemp_psy.get_property = ab8500_btemp_get_property;
1009 ab8500_btemp_external_power_changed;
1016 dev_err(di->
dev,
"failed to create work queue\n");
1018 goto free_device_info;
1023 ab8500_btemp_periodic_work);
1026 if (ab8500_btemp_id(di) < 0)
1027 dev_warn(di->
dev,
"failed to identify the battery\n");
1036 dev_err(di->
dev,
"%s ab8500 read failed\n", __func__);
1058 dev_err(di->
dev,
"failed to register BTEMP psy\n");
1063 for (i = 0; i <
ARRAY_SIZE(ab8500_btemp_irq); i++) {
1067 ab8500_btemp_irq[i].
name, di);
1070 dev_err(di->
dev,
"failed to request %s IRQ %d: %d\n"
1071 , ab8500_btemp_irq[i].
name, irq, ret);
1074 dev_dbg(di->
dev,
"Requested %s IRQ %d: %d\n",
1075 ab8500_btemp_irq[i].
name, irq, ret);
1078 platform_set_drvdata(pdev, di);
1081 ab8500_btemp_periodic(di,
true);
1090 for (i = i - 1; i >= 0; i--) {
1103 .probe = ab8500_btemp_probe,
1108 .name =
"ab8500-btemp",
1113 static int __init ab8500_btemp_init(
void)
1118 static void __exit ab8500_btemp_exit(
void)
1127 MODULE_AUTHOR(
"Johan Palsson, Karl Komierowski, Arun R Murthy");