aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonghwa Lee <jonghwa3.lee@samsung.com>2013-12-18 01:42:34 -0500
committerAnton Vorontsov <anton@enomsg.org>2013-12-23 20:09:09 -0500
commit5c49a6256bed52f639ed70d252b1c91d1ab899d6 (patch)
tree8c4d8d6e4fcadc81de02dea26f1a643a2db59399
parentd36240d26025bec95f3499e2401a56db98d9f01c (diff)
charger-manager: Modify the way of checking battery's temperature
Charger-manager driver used to check battery temperature through the callback function passed by platform data. Unfortunatley, without that pre-defined callback function, charger-manager can't get battery's temperature at all. Also passing callback function through platform data ruins DT support for charger-manager. This patch mondifies charger-manager driver to get temperature of battery without pre-defined callback function. Now, charger-manager can use either of battery thermometer in fuel-gauge and ouside of battery. It uses thermal framework interface for outer thermometer if thermal fw is enabled. Otherwise, it tries to use fuel-gauge's through the power supply interface. Signed-off-by: Jonghwa Lee <jonghwa3.lee@samsung.com> Signed-off-by: Myungjoo Ham <myungjoo.ham@samsung.com> Signed-off-by: Anton Vorontsov <anton@enomsg.org>
-rw-r--r--drivers/power/charger-manager.c161
-rw-r--r--include/linux/power/charger-manager.h24
2 files changed, 136 insertions, 49 deletions
diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c
index 7287c0efd6bf..6b68d158e3c1 100644
--- a/drivers/power/charger-manager.c
+++ b/drivers/power/charger-manager.c
@@ -25,12 +25,22 @@
25#include <linux/power/charger-manager.h> 25#include <linux/power/charger-manager.h>
26#include <linux/regulator/consumer.h> 26#include <linux/regulator/consumer.h>
27#include <linux/sysfs.h> 27#include <linux/sysfs.h>
28#include <linux/thermal.h>
29
30/*
31 * Default termperature threshold for charging.
32 * Every temperature units are in tenth of centigrade.
33 */
34#define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
35#define CM_DEFAULT_CHARGE_TEMP_MAX 500
28 36
29static const char * const default_event_names[] = { 37static const char * const default_event_names[] = {
30 [CM_EVENT_UNKNOWN] = "Unknown", 38 [CM_EVENT_UNKNOWN] = "Unknown",
31 [CM_EVENT_BATT_FULL] = "Battery Full", 39 [CM_EVENT_BATT_FULL] = "Battery Full",
32 [CM_EVENT_BATT_IN] = "Battery Inserted", 40 [CM_EVENT_BATT_IN] = "Battery Inserted",
33 [CM_EVENT_BATT_OUT] = "Battery Pulled Out", 41 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
42 [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat",
43 [CM_EVENT_BATT_COLD] = "Battery Cold",
34 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach", 44 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
35 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop", 45 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
36 [CM_EVENT_OTHERS] = "Other battery events" 46 [CM_EVENT_OTHERS] = "Other battery events"
@@ -540,6 +550,60 @@ static int check_charging_duration(struct charger_manager *cm)
540 return ret; 550 return ret;
541} 551}
542 552
553static int cm_get_battery_temperature(struct charger_manager *cm,
554 int *temp)
555{
556 int ret;
557
558 if (!cm->desc->measure_battery_temp)
559 return -ENODEV;
560
561#ifdef CONFIG_THERMAL
562 ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp);
563 if (!ret)
564 /* Calibrate temperature unit */
565 *temp /= 100;
566#else
567 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
568 POWER_SUPPLY_PROP_TEMP,
569 (union power_supply_propval *)temp);
570#endif
571 return ret;
572}
573
574static int cm_check_thermal_status(struct charger_manager *cm)
575{
576 struct charger_desc *desc = cm->desc;
577 int temp, upper_limit, lower_limit;
578 int ret = 0;
579
580 ret = cm_get_battery_temperature(cm, &temp);
581 if (ret) {
582 /* FIXME:
583 * No information of battery temperature might
584 * occur hazadous result. We have to handle it
585 * depending on battery type.
586 */
587 dev_err(cm->dev, "Failed to get battery temperature\n");
588 return 0;
589 }
590
591 upper_limit = desc->temp_max;
592 lower_limit = desc->temp_min;
593
594 if (cm->emergency_stop) {
595 upper_limit -= desc->temp_diff;
596 lower_limit += desc->temp_diff;
597 }
598
599 if (temp > upper_limit)
600 ret = CM_EVENT_BATT_OVERHEAT;
601 else if (temp < lower_limit)
602 ret = CM_EVENT_BATT_COLD;
603
604 return ret;
605}
606
543/** 607/**
544 * _cm_monitor - Monitor the temperature and return true for exceptions. 608 * _cm_monitor - Monitor the temperature and return true for exceptions.
545 * @cm: the Charger Manager representing the battery. 609 * @cm: the Charger Manager representing the battery.
@@ -549,28 +613,22 @@ static int check_charging_duration(struct charger_manager *cm)
549 */ 613 */
550static bool _cm_monitor(struct charger_manager *cm) 614static bool _cm_monitor(struct charger_manager *cm)
551{ 615{
552 struct charger_desc *desc = cm->desc; 616 int temp_alrt;
553 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
554 617
555 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n", 618 temp_alrt = cm_check_thermal_status(cm);
556 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
557 619
558 /* It has been stopped already */ 620 /* It has been stopped already */
559 if (temp && cm->emergency_stop) 621 if (temp_alrt && cm->emergency_stop)
560 return false; 622 return false;
561 623
562 /* 624 /*
563 * Check temperature whether overheat or cold. 625 * Check temperature whether overheat or cold.
564 * If temperature is out of range normal state, stop charging. 626 * If temperature is out of range normal state, stop charging.
565 */ 627 */
566 if (temp) { 628 if (temp_alrt) {
567 cm->emergency_stop = temp; 629 cm->emergency_stop = temp_alrt;
568 if (!try_charger_enable(cm, false)) { 630 if (!try_charger_enable(cm, false))
569 if (temp > 0) 631 uevent_notify(cm, default_event_names[temp_alrt]);
570 uevent_notify(cm, "OVERHEAT");
571 else
572 uevent_notify(cm, "COLD");
573 }
574 632
575 /* 633 /*
576 * Check whole charging duration and discharing duration 634 * Check whole charging duration and discharing duration
@@ -802,21 +860,8 @@ static int charger_get_property(struct power_supply *psy,
802 POWER_SUPPLY_PROP_CURRENT_NOW, val); 860 POWER_SUPPLY_PROP_CURRENT_NOW, val);
803 break; 861 break;
804 case POWER_SUPPLY_PROP_TEMP: 862 case POWER_SUPPLY_PROP_TEMP:
805 /* in thenth of centigrade */
806 if (cm->last_temp_mC == INT_MIN)
807 desc->temperature_out_of_range(&cm->last_temp_mC);
808 val->intval = cm->last_temp_mC / 100;
809 if (!desc->measure_battery_temp)
810 ret = -ENODEV;
811 break;
812 case POWER_SUPPLY_PROP_TEMP_AMBIENT: 863 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
813 /* in thenth of centigrade */ 864 return cm_get_battery_temperature(cm, &val->intval);
814 if (cm->last_temp_mC == INT_MIN)
815 desc->temperature_out_of_range(&cm->last_temp_mC);
816 val->intval = cm->last_temp_mC / 100;
817 if (desc->measure_battery_temp)
818 ret = -ENODEV;
819 break;
820 case POWER_SUPPLY_PROP_CAPACITY: 865 case POWER_SUPPLY_PROP_CAPACITY:
821 if (!cm->fuel_gauge) { 866 if (!cm->fuel_gauge) {
822 ret = -ENODEV; 867 ret = -ENODEV;
@@ -1439,6 +1484,50 @@ err:
1439 return ret; 1484 return ret;
1440} 1485}
1441 1486
1487static int cm_init_thermal_data(struct charger_manager *cm)
1488{
1489 struct charger_desc *desc = cm->desc;
1490 union power_supply_propval val;
1491 int ret;
1492
1493 /* Verify whether fuel gauge provides battery temperature */
1494 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
1495 POWER_SUPPLY_PROP_TEMP, &val);
1496
1497 if (!ret) {
1498 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1499 POWER_SUPPLY_PROP_TEMP;
1500 cm->charger_psy.num_properties++;
1501 cm->desc->measure_battery_temp = true;
1502 }
1503#ifdef CONFIG_THERMAL
1504 cm->tzd_batt = cm->fuel_gauge->tzd;
1505
1506 if (ret && desc->thermal_zone) {
1507 cm->tzd_batt =
1508 thermal_zone_get_zone_by_name(desc->thermal_zone);
1509 if (IS_ERR(cm->tzd_batt))
1510 return PTR_ERR(cm->tzd_batt);
1511
1512 /* Use external thermometer */
1513 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1514 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1515 cm->charger_psy.num_properties++;
1516 cm->desc->measure_battery_temp = true;
1517 ret = 0;
1518 }
1519#endif
1520 if (cm->desc->measure_battery_temp) {
1521 /* NOTICE : Default allowable minimum charge temperature is 0 */
1522 if (!desc->temp_max)
1523 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1524 if (!desc->temp_diff)
1525 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1526 }
1527
1528 return ret;
1529}
1530
1442static int charger_manager_probe(struct platform_device *pdev) 1531static int charger_manager_probe(struct platform_device *pdev)
1443{ 1532{
1444 struct charger_desc *desc = dev_get_platdata(&pdev->dev); 1533 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
@@ -1470,7 +1559,6 @@ static int charger_manager_probe(struct platform_device *pdev)
1470 /* Basic Values. Unspecified are Null or 0 */ 1559 /* Basic Values. Unspecified are Null or 0 */
1471 cm->dev = &pdev->dev; 1560 cm->dev = &pdev->dev;
1472 cm->desc = desc; 1561 cm->desc = desc;
1473 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1474 1562
1475 /* 1563 /*
1476 * The following two do not need to be errors. 1564 * The following two do not need to be errors.
@@ -1533,11 +1621,6 @@ static int charger_manager_probe(struct platform_device *pdev)
1533 return -EINVAL; 1621 return -EINVAL;
1534 } 1622 }
1535 1623
1536 if (!desc->temperature_out_of_range) {
1537 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1538 return -EINVAL;
1539 }
1540
1541 if (!desc->charging_max_duration_ms || 1624 if (!desc->charging_max_duration_ms ||
1542 !desc->discharging_max_duration_ms) { 1625 !desc->discharging_max_duration_ms) {
1543 dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n"); 1626 dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
@@ -1583,14 +1666,10 @@ static int charger_manager_probe(struct platform_device *pdev)
1583 cm->charger_psy.num_properties++; 1666 cm->charger_psy.num_properties++;
1584 } 1667 }
1585 1668
1586 if (desc->measure_battery_temp) { 1669 ret = cm_init_thermal_data(cm);
1587 cm->charger_psy.properties[cm->charger_psy.num_properties] = 1670 if (ret) {
1588 POWER_SUPPLY_PROP_TEMP; 1671 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1589 cm->charger_psy.num_properties++; 1672 cm->desc->measure_battery_temp = false;
1590 } else {
1591 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1592 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1593 cm->charger_psy.num_properties++;
1594 } 1673 }
1595 1674
1596 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk); 1675 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
diff --git a/include/linux/power/charger-manager.h b/include/linux/power/charger-manager.h
index 0e86840eb603..9aec0293f204 100644
--- a/include/linux/power/charger-manager.h
+++ b/include/linux/power/charger-manager.h
@@ -37,6 +37,8 @@ enum cm_event_types {
37 CM_EVENT_BATT_FULL, 37 CM_EVENT_BATT_FULL,
38 CM_EVENT_BATT_IN, 38 CM_EVENT_BATT_IN,
39 CM_EVENT_BATT_OUT, 39 CM_EVENT_BATT_OUT,
40 CM_EVENT_BATT_OVERHEAT,
41 CM_EVENT_BATT_COLD,
40 CM_EVENT_EXT_PWR_IN_OUT, 42 CM_EVENT_EXT_PWR_IN_OUT,
41 CM_EVENT_CHG_START_STOP, 43 CM_EVENT_CHG_START_STOP,
42 CM_EVENT_OTHERS, 44 CM_EVENT_OTHERS,
@@ -173,11 +175,10 @@ struct charger_regulator {
173 * @num_charger_regulator: the number of entries in charger_regulators 175 * @num_charger_regulator: the number of entries in charger_regulators
174 * @charger_regulators: array of charger regulators 176 * @charger_regulators: array of charger regulators
175 * @psy_fuel_gauge: the name of power-supply for fuel gauge 177 * @psy_fuel_gauge: the name of power-supply for fuel gauge
176 * @temperature_out_of_range: 178 * @thermal_zone : the name of thermal zone for battery
177 * Determine whether the status is overheat or cold or normal. 179 * @temp_min : Minimum battery temperature for charging.
178 * return_value > 0: overheat 180 * @temp_max : Maximum battery temperature for charging.
179 * return_value == 0: normal 181 * @temp_diff : Temperature diffential to restart charging.
180 * return_value < 0: cold
181 * @measure_battery_temp: 182 * @measure_battery_temp:
182 * true: measure battery temperature 183 * true: measure battery temperature
183 * false: measure ambient temperature 184 * false: measure ambient temperature
@@ -210,7 +211,12 @@ struct charger_desc {
210 211
211 char *psy_fuel_gauge; 212 char *psy_fuel_gauge;
212 213
213 int (*temperature_out_of_range)(int *mC); 214 char *thermal_zone;
215
216 int temp_min;
217 int temp_max;
218 int temp_diff;
219
214 bool measure_battery_temp; 220 bool measure_battery_temp;
215 221
216 u64 charging_max_duration_ms; 222 u64 charging_max_duration_ms;
@@ -226,13 +232,13 @@ struct charger_desc {
226 * @desc: instance of charger_desc 232 * @desc: instance of charger_desc
227 * @fuel_gauge: power_supply for fuel gauge 233 * @fuel_gauge: power_supply for fuel gauge
228 * @charger_stat: array of power_supply for chargers 234 * @charger_stat: array of power_supply for chargers
235 * @tzd_batt : thermal zone device for battery
229 * @charger_enabled: the state of charger 236 * @charger_enabled: the state of charger
230 * @fullbatt_vchk_jiffies_at: 237 * @fullbatt_vchk_jiffies_at:
231 * jiffies at the time full battery check will occur. 238 * jiffies at the time full battery check will occur.
232 * @fullbatt_vchk_work: work queue for full battery check 239 * @fullbatt_vchk_work: work queue for full battery check
233 * @emergency_stop: 240 * @emergency_stop:
234 * When setting true, stop charging 241 * When setting true, stop charging
235 * @last_temp_mC: the measured temperature in milli-Celsius
236 * @psy_name_buf: the name of power-supply-class for charger manager 242 * @psy_name_buf: the name of power-supply-class for charger manager
237 * @charger_psy: power_supply for charger manager 243 * @charger_psy: power_supply for charger manager
238 * @status_save_ext_pwr_inserted: 244 * @status_save_ext_pwr_inserted:
@@ -250,13 +256,15 @@ struct charger_manager {
250 struct power_supply *fuel_gauge; 256 struct power_supply *fuel_gauge;
251 struct power_supply **charger_stat; 257 struct power_supply **charger_stat;
252 258
259#ifdef CONFIG_THERMAL
260 struct thermal_zone_device *tzd_batt;
261#endif
253 bool charger_enabled; 262 bool charger_enabled;
254 263
255 unsigned long fullbatt_vchk_jiffies_at; 264 unsigned long fullbatt_vchk_jiffies_at;
256 struct delayed_work fullbatt_vchk_work; 265 struct delayed_work fullbatt_vchk_work;
257 266
258 int emergency_stop; 267 int emergency_stop;
259 int last_temp_mC;
260 268
261 char psy_name_buf[PSY_NAME_MAX + 1]; 269 char psy_name_buf[PSY_NAME_MAX + 1];
262 struct power_supply charger_psy; 270 struct power_supply charger_psy;