2 * Copyright (C) ST-Ericsson SA 2012
4 * Battery temperature driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/workqueue.h>
23 #include <linux/jiffies.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/abx500.h>
27 #include <linux/mfd/abx500/ab8500.h>
28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/mfd/abx500/ab8500-gpadc.h>
33 #define BTEMP_THERMAL_LOW_LIMIT -10
34 #define BTEMP_THERMAL_MED_LIMIT 0
35 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
36 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
37 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
39 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
40 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
42 #define to_ab8500_btemp_device_info(x) container_of((x), \
43 struct ab8500_btemp, btemp_psy);
46 * struct ab8500_btemp_interrupts - ab8500 interrupts
47 * @name: name of the interrupt
48 * @isr function pointer to the isr
50 struct ab8500_btemp_interrupts {
52 irqreturn_t (*isr)(int irq, void *data);
55 struct ab8500_btemp_events {
65 struct ab8500_btemp_ranges {
72 * struct ab8500_btemp - ab8500 BTEMP device information
73 * @dev: Pointer to the structure device
74 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
75 * @curr_source: What current source we use, in uA
76 * @bat_temp: Battery temperature in degree Celcius
77 * @prev_bat_temp Last dispatched battery temperature
78 * @parent: Pointer to the struct ab8500
79 * @gpadc: Pointer to the struct gpadc
80 * @fg: Pointer to the struct fg
81 * @bm: Platform specific battery management information
82 * @btemp_psy: Structure for BTEMP specific battery properties
83 * @events: Structure for information about events triggered
84 * @btemp_ranges: Battery temperature range structure
85 * @btemp_wq: Work queue for measuring the temperature periodically
86 * @btemp_periodic_work: Work for measuring the temperature periodically
87 * @initialized: True if battery id read.
91 struct list_head node;
95 struct ab8500 *parent;
96 struct ab8500_gpadc *gpadc;
98 struct abx500_bm_data *bm;
99 struct power_supply btemp_psy;
100 struct ab8500_btemp_events events;
101 struct ab8500_btemp_ranges btemp_ranges;
102 struct workqueue_struct *btemp_wq;
103 struct delayed_work btemp_periodic_work;
107 /* BTEMP power supply properties */
108 static enum power_supply_property ab8500_btemp_props[] = {
109 POWER_SUPPLY_PROP_PRESENT,
110 POWER_SUPPLY_PROP_ONLINE,
111 POWER_SUPPLY_PROP_TECHNOLOGY,
112 POWER_SUPPLY_PROP_TEMP,
115 static LIST_HEAD(ab8500_btemp_list);
118 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
119 * (i.e. the first BTEMP in the instance list)
121 struct ab8500_btemp *ab8500_btemp_get(void)
123 struct ab8500_btemp *btemp;
124 btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
130 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
131 * @di: pointer to the ab8500_btemp structure
132 * @v_batctrl: measured batctrl voltage
133 * @inst_curr: measured instant current
135 * This function returns the battery resistance that is
136 * derived from the BATCTRL voltage.
137 * Returns value in Ohms.
139 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
140 int v_batctrl, int inst_curr)
144 if (is_ab8500_1p1_or_earlier(di->parent)) {
146 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
147 * connected to 1.8V through a 450k resistor
149 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
152 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
154 * If the battery has internal NTC, we use the current
155 * source to calculate the resistance, 7uA or 20uA
157 rbs = (v_batctrl * 1000
158 - di->bm->gnd_lift_resistance * inst_curr)
162 * BAT_CTRL is internally
163 * connected to 1.8V through a 80k resistor
165 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
172 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
173 * @di: pointer to the ab8500_btemp structure
175 * This function returns the voltage on BATCTRL. Returns value in mV.
177 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
182 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
185 "%s gpadc conversion failed, using previous value",
194 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
195 * @di: pointer to the ab8500_btemp structure
196 * @enable: enable or disable the current source
198 * Enable or disable the current sources for the BatCtrl AD channel
200 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
207 * BATCTRL current sources are included on AB8500 cut2.0
208 * and future versions
210 if (is_ab8500_1p1_or_earlier(di->parent))
213 /* Only do this for batteries with internal NTC */
214 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
215 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
216 curr = BAT_CTRL_7U_ENA;
218 curr = BAT_CTRL_20U_ENA;
220 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
222 ret = abx500_mask_and_set_register_interruptible(di->dev,
223 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
224 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
226 dev_err(di->dev, "%s failed setting cmp_force\n",
232 * We have to wait one 32kHz cycle before enabling
233 * the current source, since ForceBatCtrlCmpHigh needs
234 * to be written in a separate cycle
238 ret = abx500_set_register_interruptible(di->dev,
239 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
240 FORCE_BAT_CTRL_CMP_HIGH | curr);
242 dev_err(di->dev, "%s failed enabling current source\n",
244 goto disable_curr_source;
246 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
247 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
249 /* Write 0 to the curr bits */
250 ret = abx500_mask_and_set_register_interruptible(di->dev,
251 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
252 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
253 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
255 dev_err(di->dev, "%s failed disabling current source\n",
257 goto disable_curr_source;
260 /* Enable Pull-Up and comparator */
261 ret = abx500_mask_and_set_register_interruptible(di->dev,
262 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
263 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
264 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
266 dev_err(di->dev, "%s failed enabling PU and comp\n",
272 * We have to wait one 32kHz cycle before disabling
273 * ForceBatCtrlCmpHigh since this needs to be written
274 * in a separate cycle
278 /* Disable 'force comparator' */
279 ret = abx500_mask_and_set_register_interruptible(di->dev,
280 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
281 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
283 dev_err(di->dev, "%s failed disabling force comp\n",
285 goto disable_force_comp;
291 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
292 * if we got an error above
295 /* Write 0 to the curr bits */
296 ret = abx500_mask_and_set_register_interruptible(di->dev,
297 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
298 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
299 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
301 dev_err(di->dev, "%s failed disabling current source\n",
306 /* Enable Pull-Up and comparator */
307 ret = abx500_mask_and_set_register_interruptible(di->dev,
308 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
309 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
310 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
312 dev_err(di->dev, "%s failed enabling PU and comp\n",
319 * We have to wait one 32kHz cycle before disabling
320 * ForceBatCtrlCmpHigh since this needs to be written
321 * in a separate cycle
325 /* Disable 'force comparator' */
326 ret = abx500_mask_and_set_register_interruptible(di->dev,
327 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
328 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
330 dev_err(di->dev, "%s failed disabling force comp\n",
339 * ab8500_btemp_get_batctrl_res() - get battery resistance
340 * @di: pointer to the ab8500_btemp structure
342 * This function returns the battery pack identification resistance.
343 * Returns value in Ohms.
345 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
354 * BATCTRL current sources are included on AB8500 cut2.0
355 * and future versions
357 ret = ab8500_btemp_curr_source_enable(di, true);
359 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
364 di->fg = ab8500_fg_get();
366 dev_err(di->dev, "No fg found\n");
370 ret = ab8500_fg_inst_curr_start(di->fg);
373 dev_err(di->dev, "Failed to start current measurement\n");
379 } while (!ab8500_fg_inst_curr_started(di->fg));
384 batctrl += ab8500_btemp_read_batctrl_voltage(di);
387 } while (!ab8500_fg_inst_curr_done(di->fg));
390 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
392 dev_err(di->dev, "Failed to finalize current measurement\n");
396 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
398 ret = ab8500_btemp_curr_source_enable(di, false);
400 dev_err(di->dev, "%s curr source disable failed\n", __func__);
404 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
405 __func__, batctrl, res, inst_curr, i);
411 * ab8500_btemp_res_to_temp() - resistance to temperature
412 * @di: pointer to the ab8500_btemp structure
413 * @tbl: pointer to the resiatance to temperature table
414 * @tbl_size: size of the resistance to temperature table
415 * @res: resistance to calculate the temperature from
417 * This function returns the battery temperature in degrees Celcius
418 * based on the NTC resistance.
420 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
421 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
425 * Calculate the formula for the straight line
426 * Simple interpolation if we are within
427 * the resistance table limits, extrapolate
428 * if resistance is outside the limits.
430 if (res > tbl[0].resist)
432 else if (res <= tbl[tbl_size - 1].resist)
436 while (!(res <= tbl[i].resist &&
437 res > tbl[i + 1].resist))
441 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
442 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
447 * ab8500_btemp_measure_temp() - measure battery temperature
448 * @di: pointer to the ab8500_btemp structure
450 * Returns battery temperature (on success) else the previous temperature
452 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
456 int rbat, rntc, vntc;
459 id = di->bm->batt_id;
461 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
462 id != BATTERY_UNKNOWN) {
464 rbat = ab8500_btemp_get_batctrl_res(di);
466 dev_err(di->dev, "%s get batctrl res failed\n",
469 * Return out-of-range temperature so that
470 * charging is stopped
472 return BTEMP_THERMAL_LOW_LIMIT;
475 temp = ab8500_btemp_res_to_temp(di,
476 di->bm->bat_type[id].r_to_t_tbl,
477 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
479 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
482 "%s gpadc conversion failed,"
483 " using previous value\n", __func__);
487 * The PCB NTC is sourced from VTVOUT via a 230kOhm
490 rntc = 230000 * vntc / (VTVOUT_V - vntc);
492 temp = ab8500_btemp_res_to_temp(di,
493 di->bm->bat_type[id].r_to_t_tbl,
494 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
497 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
502 * ab8500_btemp_id() - Identify the connected battery
503 * @di: pointer to the ab8500_btemp structure
505 * This function will try to identify the battery by reading the ID
506 * resistor. Some brands use a combined ID resistor with a NTC resistor to
507 * both be able to identify and to read the temperature of it.
509 static int ab8500_btemp_id(struct ab8500_btemp *di)
514 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
515 di->bm->batt_id = BATTERY_UNKNOWN;
517 res = ab8500_btemp_get_batctrl_res(di);
519 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
523 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
524 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
525 if ((res <= di->bm->bat_type[i].resis_high) &&
526 (res >= di->bm->bat_type[i].resis_low)) {
527 dev_dbg(di->dev, "Battery detected on %s"
528 " low %d < res %d < high: %d"
530 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
531 "BATCTRL" : "BATTEMP",
532 di->bm->bat_type[i].resis_low, res,
533 di->bm->bat_type[i].resis_high, i);
540 if (di->bm->batt_id == BATTERY_UNKNOWN) {
541 dev_warn(di->dev, "Battery identified as unknown"
542 ", resistance %d Ohm\n", res);
547 * We only have to change current source if the
548 * detected type is Type 1, else we use the 7uA source
550 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
551 di->bm->batt_id == 1) {
552 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
553 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
556 return di->bm->batt_id;
560 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
561 * @work: pointer to the work_struct structure
563 * Work function for measuring the temperature periodically
565 static void ab8500_btemp_periodic_work(struct work_struct *work)
568 struct ab8500_btemp *di = container_of(work,
569 struct ab8500_btemp, btemp_periodic_work.work);
571 if (!di->initialized) {
572 di->initialized = true;
573 /* Identify the battery */
574 if (ab8500_btemp_id(di) < 0)
575 dev_warn(di->dev, "failed to identify the battery\n");
578 di->bat_temp = ab8500_btemp_measure_temp(di);
580 if (di->bat_temp != di->prev_bat_temp) {
581 di->prev_bat_temp = di->bat_temp;
582 power_supply_changed(&di->btemp_psy);
585 if (di->events.ac_conn || di->events.usb_conn)
586 interval = di->bm->temp_interval_chg;
588 interval = di->bm->temp_interval_nochg;
590 /* Schedule a new measurement */
591 queue_delayed_work(di->btemp_wq,
592 &di->btemp_periodic_work,
593 round_jiffies(interval * HZ));
597 * ab8500_btemp_batctrlindb_handler() - battery removal detected
598 * @irq: interrupt number
599 * @_di: void pointer that has to address of ab8500_btemp
601 * Returns IRQ status(IRQ_HANDLED)
603 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
605 struct ab8500_btemp *di = _di;
606 dev_err(di->dev, "Battery removal detected!\n");
608 di->events.batt_rem = true;
609 power_supply_changed(&di->btemp_psy);
615 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
616 * @irq: interrupt number
617 * @_di: void pointer that has to address of ab8500_btemp
619 * Returns IRQ status(IRQ_HANDLED)
621 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
623 struct ab8500_btemp *di = _di;
625 if (is_ab8500_3p3_or_earlier(di->parent)) {
626 dev_dbg(di->dev, "Ignore false btemp low irq"
627 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
629 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
631 di->events.btemp_low = true;
632 di->events.btemp_high = false;
633 di->events.btemp_medhigh = false;
634 di->events.btemp_lowmed = false;
635 power_supply_changed(&di->btemp_psy);
642 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
643 * @irq: interrupt number
644 * @_di: void pointer that has to address of ab8500_btemp
646 * Returns IRQ status(IRQ_HANDLED)
648 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
650 struct ab8500_btemp *di = _di;
652 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
654 di->events.btemp_high = true;
655 di->events.btemp_medhigh = false;
656 di->events.btemp_lowmed = false;
657 di->events.btemp_low = false;
658 power_supply_changed(&di->btemp_psy);
664 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
665 * @irq: interrupt number
666 * @_di: void pointer that has to address of ab8500_btemp
668 * Returns IRQ status(IRQ_HANDLED)
670 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
672 struct ab8500_btemp *di = _di;
674 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
676 di->events.btemp_lowmed = true;
677 di->events.btemp_medhigh = false;
678 di->events.btemp_high = false;
679 di->events.btemp_low = false;
680 power_supply_changed(&di->btemp_psy);
686 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
687 * @irq: interrupt number
688 * @_di: void pointer that has to address of ab8500_btemp
690 * Returns IRQ status(IRQ_HANDLED)
692 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
694 struct ab8500_btemp *di = _di;
696 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
698 di->events.btemp_medhigh = true;
699 di->events.btemp_lowmed = false;
700 di->events.btemp_high = false;
701 di->events.btemp_low = false;
702 power_supply_changed(&di->btemp_psy);
708 * ab8500_btemp_periodic() - Periodic temperature measurements
709 * @di: pointer to the ab8500_btemp structure
710 * @enable: enable or disable periodic temperature measurements
712 * Starts of stops periodic temperature measurements. Periodic measurements
713 * should only be done when a charger is connected.
715 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
718 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
721 * Make sure a new measurement is done directly by cancelling
724 cancel_delayed_work_sync(&di->btemp_periodic_work);
727 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
731 * ab8500_btemp_get_temp() - get battery temperature
732 * @di: pointer to the ab8500_btemp structure
734 * Returns battery temperature
736 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
741 * The BTEMP events are not reliabe on AB8500 cut3.3
744 if (is_ab8500_3p3_or_earlier(di->parent)) {
745 temp = di->bat_temp * 10;
747 if (di->events.btemp_low) {
748 if (temp > di->btemp_ranges.btemp_low_limit)
749 temp = di->btemp_ranges.btemp_low_limit * 10;
751 temp = di->bat_temp * 10;
752 } else if (di->events.btemp_high) {
753 if (temp < di->btemp_ranges.btemp_high_limit)
754 temp = di->btemp_ranges.btemp_high_limit * 10;
756 temp = di->bat_temp * 10;
757 } else if (di->events.btemp_lowmed) {
758 if (temp > di->btemp_ranges.btemp_med_limit)
759 temp = di->btemp_ranges.btemp_med_limit * 10;
761 temp = di->bat_temp * 10;
762 } else if (di->events.btemp_medhigh) {
763 if (temp < di->btemp_ranges.btemp_med_limit)
764 temp = di->btemp_ranges.btemp_med_limit * 10;
766 temp = di->bat_temp * 10;
768 temp = di->bat_temp * 10;
774 * ab8500_btemp_get_batctrl_temp() - get the temperature
775 * @btemp: pointer to the btemp structure
777 * Returns the batctrl temperature in millidegrees
779 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
781 return btemp->bat_temp * 1000;
785 * ab8500_btemp_get_property() - get the btemp properties
786 * @psy: pointer to the power_supply structure
787 * @psp: pointer to the power_supply_property structure
788 * @val: pointer to the power_supply_propval union
790 * This function gets called when an application tries to get the btemp
791 * properties by reading the sysfs files.
792 * online: presence of the battery
793 * present: presence of the battery
794 * technology: battery technology
795 * temp: battery temperature
796 * Returns error code in case of failure else 0(on success)
798 static int ab8500_btemp_get_property(struct power_supply *psy,
799 enum power_supply_property psp,
800 union power_supply_propval *val)
802 struct ab8500_btemp *di;
804 di = to_ab8500_btemp_device_info(psy);
807 case POWER_SUPPLY_PROP_PRESENT:
808 case POWER_SUPPLY_PROP_ONLINE:
809 if (di->events.batt_rem)
814 case POWER_SUPPLY_PROP_TECHNOLOGY:
815 val->intval = di->bm->bat_type[di->bm->batt_id].name;
817 case POWER_SUPPLY_PROP_TEMP:
818 val->intval = ab8500_btemp_get_temp(di);
826 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
828 struct power_supply *psy;
829 struct power_supply *ext;
830 struct ab8500_btemp *di;
831 union power_supply_propval ret;
833 bool psy_found = false;
835 psy = (struct power_supply *)data;
836 ext = dev_get_drvdata(dev);
837 di = to_ab8500_btemp_device_info(psy);
840 * For all psy where the name of your driver
841 * appears in any supplied_to
843 for (i = 0; i < ext->num_supplicants; i++) {
844 if (!strcmp(ext->supplied_to[i], psy->name))
851 /* Go through all properties for the psy */
852 for (j = 0; j < ext->num_properties; j++) {
853 enum power_supply_property prop;
854 prop = ext->properties[j];
856 if (ext->get_property(ext, prop, &ret))
860 case POWER_SUPPLY_PROP_PRESENT:
862 case POWER_SUPPLY_TYPE_MAINS:
863 /* AC disconnected */
864 if (!ret.intval && di->events.ac_conn) {
865 di->events.ac_conn = false;
868 else if (ret.intval && !di->events.ac_conn) {
869 di->events.ac_conn = true;
870 if (!di->events.usb_conn)
871 ab8500_btemp_periodic(di, true);
874 case POWER_SUPPLY_TYPE_USB:
875 /* USB disconnected */
876 if (!ret.intval && di->events.usb_conn) {
877 di->events.usb_conn = false;
880 else if (ret.intval && !di->events.usb_conn) {
881 di->events.usb_conn = true;
882 if (!di->events.ac_conn)
883 ab8500_btemp_periodic(di, true);
898 * ab8500_btemp_external_power_changed() - callback for power supply changes
899 * @psy: pointer to the structure power_supply
901 * This function is pointing to the function pointer external_power_changed
902 * of the structure power_supply.
903 * This function gets executed when there is a change in the external power
904 * supply to the btemp.
906 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
908 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
910 class_for_each_device(power_supply_class, NULL,
911 &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
914 /* ab8500 btemp driver interrupts and their respective isr */
915 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
916 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
917 {"BTEMP_LOW", ab8500_btemp_templow_handler},
918 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
919 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
920 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
923 #if defined(CONFIG_PM)
924 static int ab8500_btemp_resume(struct platform_device *pdev)
926 struct ab8500_btemp *di = platform_get_drvdata(pdev);
928 ab8500_btemp_periodic(di, true);
933 static int ab8500_btemp_suspend(struct platform_device *pdev,
936 struct ab8500_btemp *di = platform_get_drvdata(pdev);
938 ab8500_btemp_periodic(di, false);
943 #define ab8500_btemp_suspend NULL
944 #define ab8500_btemp_resume NULL
947 static int ab8500_btemp_remove(struct platform_device *pdev)
949 struct ab8500_btemp *di = platform_get_drvdata(pdev);
952 /* Disable interrupts */
953 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
954 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
958 /* Delete the work queue */
959 destroy_workqueue(di->btemp_wq);
961 flush_scheduled_work();
962 power_supply_unregister(&di->btemp_psy);
963 platform_set_drvdata(pdev, NULL);
968 static char *supply_interface[] = {
973 static int ab8500_btemp_probe(struct platform_device *pdev)
975 struct device_node *np = pdev->dev.of_node;
976 struct abx500_bm_data *plat = pdev->dev.platform_data;
977 struct ab8500_btemp *di;
981 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
983 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
988 dev_err(&pdev->dev, "no battery management data supplied\n");
994 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
996 dev_err(&pdev->dev, "failed to get battery information\n");
1001 /* get parent data */
1002 di->dev = &pdev->dev;
1003 di->parent = dev_get_drvdata(pdev->dev.parent);
1004 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1006 di->initialized = false;
1009 di->btemp_psy.name = "ab8500_btemp";
1010 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1011 di->btemp_psy.properties = ab8500_btemp_props;
1012 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
1013 di->btemp_psy.get_property = ab8500_btemp_get_property;
1014 di->btemp_psy.supplied_to = supply_interface;
1015 di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
1016 di->btemp_psy.external_power_changed =
1017 ab8500_btemp_external_power_changed;
1020 /* Create a work queue for the btemp */
1022 create_singlethread_workqueue("ab8500_btemp_wq");
1023 if (di->btemp_wq == NULL) {
1024 dev_err(di->dev, "failed to create work queue\n");
1028 /* Init work for measuring temperature periodically */
1029 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1030 ab8500_btemp_periodic_work);
1032 /* Set BTEMP thermal limits. Low and Med are fixed */
1033 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT * 10;
1034 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT * 10;
1036 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1037 AB8500_BTEMP_HIGH_TH, &val);
1039 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1043 case BTEMP_HIGH_TH_57_0:
1044 case BTEMP_HIGH_TH_57_1:
1045 di->btemp_ranges.btemp_high_limit =
1046 BTEMP_THERMAL_HIGH_LIMIT_57;
1048 case BTEMP_HIGH_TH_52:
1049 di->btemp_ranges.btemp_high_limit =
1050 BTEMP_THERMAL_HIGH_LIMIT_52;
1052 case BTEMP_HIGH_TH_62:
1053 di->btemp_ranges.btemp_high_limit =
1054 BTEMP_THERMAL_HIGH_LIMIT_62;
1058 /* Register BTEMP power supply class */
1059 ret = power_supply_register(di->dev, &di->btemp_psy);
1061 dev_err(di->dev, "failed to register BTEMP psy\n");
1065 /* Register interrupts */
1066 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1067 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1068 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1069 IRQF_SHARED | IRQF_NO_SUSPEND,
1070 ab8500_btemp_irq[i].name, di);
1073 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1074 , ab8500_btemp_irq[i].name, irq, ret);
1077 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1078 ab8500_btemp_irq[i].name, irq, ret);
1081 platform_set_drvdata(pdev, di);
1083 /* Kick off periodic temperature measurements */
1084 ab8500_btemp_periodic(di, true);
1085 list_add_tail(&di->node, &ab8500_btemp_list);
1090 power_supply_unregister(&di->btemp_psy);
1092 /* We also have to free all successfully registered irqs */
1093 for (i = i - 1; i >= 0; i--) {
1094 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1098 destroy_workqueue(di->btemp_wq);
1102 static const struct of_device_id ab8500_btemp_match[] = {
1103 { .compatible = "stericsson,ab8500-btemp", },
1107 static struct platform_driver ab8500_btemp_driver = {
1108 .probe = ab8500_btemp_probe,
1109 .remove = ab8500_btemp_remove,
1110 .suspend = ab8500_btemp_suspend,
1111 .resume = ab8500_btemp_resume,
1113 .name = "ab8500-btemp",
1114 .owner = THIS_MODULE,
1115 .of_match_table = ab8500_btemp_match,
1119 static int __init ab8500_btemp_init(void)
1121 return platform_driver_register(&ab8500_btemp_driver);
1124 static void __exit ab8500_btemp_exit(void)
1126 platform_driver_unregister(&ab8500_btemp_driver);
1129 subsys_initcall_sync(ab8500_btemp_init);
1130 module_exit(ab8500_btemp_exit);
1132 MODULE_LICENSE("GPL v2");
1133 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1134 MODULE_ALIAS("platform:ab8500-btemp");
1135 MODULE_DESCRIPTION("AB8500 battery temperature driver");