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");
378 * Since there is no interrupt when current measurement is done,
379 * loop for over 250ms (250ms is one sample conversion time
380 * with 32.768 Khz RTC clock). Note that a stop time must be set
381 * since the ab8500_btemp_read_batctrl_voltage call can block and
382 * take an unknown amount of time to complete.
387 batctrl += ab8500_btemp_read_batctrl_voltage(di);
390 } while (!ab8500_fg_inst_curr_done(di->fg));
393 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
395 dev_err(di->dev, "Failed to finalize current measurement\n");
399 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
401 ret = ab8500_btemp_curr_source_enable(di, false);
403 dev_err(di->dev, "%s curr source disable failed\n", __func__);
407 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
408 __func__, batctrl, res, inst_curr, i);
414 * ab8500_btemp_res_to_temp() - resistance to temperature
415 * @di: pointer to the ab8500_btemp structure
416 * @tbl: pointer to the resiatance to temperature table
417 * @tbl_size: size of the resistance to temperature table
418 * @res: resistance to calculate the temperature from
420 * This function returns the battery temperature in degrees Celcius
421 * based on the NTC resistance.
423 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
424 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
428 * Calculate the formula for the straight line
429 * Simple interpolation if we are within
430 * the resistance table limits, extrapolate
431 * if resistance is outside the limits.
433 if (res > tbl[0].resist)
435 else if (res <= tbl[tbl_size - 1].resist)
439 while (!(res <= tbl[i].resist &&
440 res > tbl[i + 1].resist))
444 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
445 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
450 * ab8500_btemp_measure_temp() - measure battery temperature
451 * @di: pointer to the ab8500_btemp structure
453 * Returns battery temperature (on success) else the previous temperature
455 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
459 int rbat, rntc, vntc;
462 id = di->bm->batt_id;
464 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
465 id != BATTERY_UNKNOWN) {
467 rbat = ab8500_btemp_get_batctrl_res(di);
469 dev_err(di->dev, "%s get batctrl res failed\n",
472 * Return out-of-range temperature so that
473 * charging is stopped
475 return BTEMP_THERMAL_LOW_LIMIT;
478 temp = ab8500_btemp_res_to_temp(di,
479 di->bm->bat_type[id].r_to_t_tbl,
480 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
482 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
485 "%s gpadc conversion failed,"
486 " using previous value\n", __func__);
490 * The PCB NTC is sourced from VTVOUT via a 230kOhm
493 rntc = 230000 * vntc / (VTVOUT_V - vntc);
495 temp = ab8500_btemp_res_to_temp(di,
496 di->bm->bat_type[id].r_to_t_tbl,
497 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
500 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
505 * ab8500_btemp_id() - Identify the connected battery
506 * @di: pointer to the ab8500_btemp structure
508 * This function will try to identify the battery by reading the ID
509 * resistor. Some brands use a combined ID resistor with a NTC resistor to
510 * both be able to identify and to read the temperature of it.
512 static int ab8500_btemp_id(struct ab8500_btemp *di)
517 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
518 di->bm->batt_id = BATTERY_UNKNOWN;
520 res = ab8500_btemp_get_batctrl_res(di);
522 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
526 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
527 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
528 if ((res <= di->bm->bat_type[i].resis_high) &&
529 (res >= di->bm->bat_type[i].resis_low)) {
530 dev_dbg(di->dev, "Battery detected on %s"
531 " low %d < res %d < high: %d"
533 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
534 "BATCTRL" : "BATTEMP",
535 di->bm->bat_type[i].resis_low, res,
536 di->bm->bat_type[i].resis_high, i);
543 if (di->bm->batt_id == BATTERY_UNKNOWN) {
544 dev_warn(di->dev, "Battery identified as unknown"
545 ", resistance %d Ohm\n", res);
550 * We only have to change current source if the
551 * detected type is Type 1, else we use the 7uA source
553 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
554 di->bm->batt_id == 1) {
555 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
556 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
559 return di->bm->batt_id;
563 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
564 * @work: pointer to the work_struct structure
566 * Work function for measuring the temperature periodically
568 static void ab8500_btemp_periodic_work(struct work_struct *work)
571 struct ab8500_btemp *di = container_of(work,
572 struct ab8500_btemp, btemp_periodic_work.work);
574 if (!di->initialized) {
575 di->initialized = true;
576 /* Identify the battery */
577 if (ab8500_btemp_id(di) < 0)
578 dev_warn(di->dev, "failed to identify the battery\n");
581 di->bat_temp = ab8500_btemp_measure_temp(di);
583 if (di->bat_temp != di->prev_bat_temp) {
584 di->prev_bat_temp = di->bat_temp;
585 power_supply_changed(&di->btemp_psy);
588 if (di->events.ac_conn || di->events.usb_conn)
589 interval = di->bm->temp_interval_chg;
591 interval = di->bm->temp_interval_nochg;
593 /* Schedule a new measurement */
594 queue_delayed_work(di->btemp_wq,
595 &di->btemp_periodic_work,
596 round_jiffies(interval * HZ));
600 * ab8500_btemp_batctrlindb_handler() - battery removal detected
601 * @irq: interrupt number
602 * @_di: void pointer that has to address of ab8500_btemp
604 * Returns IRQ status(IRQ_HANDLED)
606 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
608 struct ab8500_btemp *di = _di;
609 dev_err(di->dev, "Battery removal detected!\n");
611 di->events.batt_rem = true;
612 power_supply_changed(&di->btemp_psy);
618 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
619 * @irq: interrupt number
620 * @_di: void pointer that has to address of ab8500_btemp
622 * Returns IRQ status(IRQ_HANDLED)
624 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
626 struct ab8500_btemp *di = _di;
628 if (is_ab8500_2p0_or_earlier(di->parent)) {
629 dev_dbg(di->dev, "Ignore false btemp low irq"
630 " for ABB cut 1.0, 1.1 and 2.0\n");
632 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
634 di->events.btemp_low = true;
635 di->events.btemp_high = false;
636 di->events.btemp_medhigh = false;
637 di->events.btemp_lowmed = false;
638 power_supply_changed(&di->btemp_psy);
645 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
646 * @irq: interrupt number
647 * @_di: void pointer that has to address of ab8500_btemp
649 * Returns IRQ status(IRQ_HANDLED)
651 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
653 struct ab8500_btemp *di = _di;
655 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
657 di->events.btemp_high = true;
658 di->events.btemp_medhigh = false;
659 di->events.btemp_lowmed = false;
660 di->events.btemp_low = false;
661 power_supply_changed(&di->btemp_psy);
667 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
668 * @irq: interrupt number
669 * @_di: void pointer that has to address of ab8500_btemp
671 * Returns IRQ status(IRQ_HANDLED)
673 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
675 struct ab8500_btemp *di = _di;
677 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
679 di->events.btemp_lowmed = true;
680 di->events.btemp_medhigh = false;
681 di->events.btemp_high = false;
682 di->events.btemp_low = false;
683 power_supply_changed(&di->btemp_psy);
689 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
690 * @irq: interrupt number
691 * @_di: void pointer that has to address of ab8500_btemp
693 * Returns IRQ status(IRQ_HANDLED)
695 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
697 struct ab8500_btemp *di = _di;
699 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
701 di->events.btemp_medhigh = true;
702 di->events.btemp_lowmed = false;
703 di->events.btemp_high = false;
704 di->events.btemp_low = false;
705 power_supply_changed(&di->btemp_psy);
711 * ab8500_btemp_periodic() - Periodic temperature measurements
712 * @di: pointer to the ab8500_btemp structure
713 * @enable: enable or disable periodic temperature measurements
715 * Starts of stops periodic temperature measurements. Periodic measurements
716 * should only be done when a charger is connected.
718 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
721 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
724 * Make sure a new measurement is done directly by cancelling
727 cancel_delayed_work_sync(&di->btemp_periodic_work);
730 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
734 * ab8500_btemp_get_temp() - get battery temperature
735 * @di: pointer to the ab8500_btemp structure
737 * Returns battery temperature
739 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
744 * The BTEMP events are not reliabe on AB8500 cut2.0
747 if (is_ab8500_2p0_or_earlier(di->parent)) {
748 temp = di->bat_temp * 10;
750 if (di->events.btemp_low) {
751 if (temp > di->btemp_ranges.btemp_low_limit)
752 temp = di->btemp_ranges.btemp_low_limit;
754 temp = di->bat_temp * 10;
755 } else if (di->events.btemp_high) {
756 if (temp < di->btemp_ranges.btemp_high_limit)
757 temp = di->btemp_ranges.btemp_high_limit;
759 temp = di->bat_temp * 10;
760 } else if (di->events.btemp_lowmed) {
761 if (temp > di->btemp_ranges.btemp_med_limit)
762 temp = di->btemp_ranges.btemp_med_limit;
764 temp = di->bat_temp * 10;
765 } else if (di->events.btemp_medhigh) {
766 if (temp < di->btemp_ranges.btemp_med_limit)
767 temp = di->btemp_ranges.btemp_med_limit;
769 temp = di->bat_temp * 10;
771 temp = di->bat_temp * 10;
777 * ab8500_btemp_get_batctrl_temp() - get the temperature
778 * @btemp: pointer to the btemp structure
780 * Returns the batctrl temperature in millidegrees
782 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
784 return btemp->bat_temp * 1000;
788 * ab8500_btemp_get_property() - get the btemp properties
789 * @psy: pointer to the power_supply structure
790 * @psp: pointer to the power_supply_property structure
791 * @val: pointer to the power_supply_propval union
793 * This function gets called when an application tries to get the btemp
794 * properties by reading the sysfs files.
795 * online: presence of the battery
796 * present: presence of the battery
797 * technology: battery technology
798 * temp: battery temperature
799 * Returns error code in case of failure else 0(on success)
801 static int ab8500_btemp_get_property(struct power_supply *psy,
802 enum power_supply_property psp,
803 union power_supply_propval *val)
805 struct ab8500_btemp *di;
807 di = to_ab8500_btemp_device_info(psy);
810 case POWER_SUPPLY_PROP_PRESENT:
811 case POWER_SUPPLY_PROP_ONLINE:
812 if (di->events.batt_rem)
817 case POWER_SUPPLY_PROP_TECHNOLOGY:
818 val->intval = di->bm->bat_type[di->bm->batt_id].name;
820 case POWER_SUPPLY_PROP_TEMP:
821 val->intval = ab8500_btemp_get_temp(di);
829 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
831 struct power_supply *psy;
832 struct power_supply *ext;
833 struct ab8500_btemp *di;
834 union power_supply_propval ret;
836 bool psy_found = false;
838 psy = (struct power_supply *)data;
839 ext = dev_get_drvdata(dev);
840 di = to_ab8500_btemp_device_info(psy);
843 * For all psy where the name of your driver
844 * appears in any supplied_to
846 for (i = 0; i < ext->num_supplicants; i++) {
847 if (!strcmp(ext->supplied_to[i], psy->name))
854 /* Go through all properties for the psy */
855 for (j = 0; j < ext->num_properties; j++) {
856 enum power_supply_property prop;
857 prop = ext->properties[j];
859 if (ext->get_property(ext, prop, &ret))
863 case POWER_SUPPLY_PROP_PRESENT:
865 case POWER_SUPPLY_TYPE_MAINS:
866 /* AC disconnected */
867 if (!ret.intval && di->events.ac_conn) {
868 di->events.ac_conn = false;
871 else if (ret.intval && !di->events.ac_conn) {
872 di->events.ac_conn = true;
873 if (!di->events.usb_conn)
874 ab8500_btemp_periodic(di, true);
877 case POWER_SUPPLY_TYPE_USB:
878 /* USB disconnected */
879 if (!ret.intval && di->events.usb_conn) {
880 di->events.usb_conn = false;
883 else if (ret.intval && !di->events.usb_conn) {
884 di->events.usb_conn = true;
885 if (!di->events.ac_conn)
886 ab8500_btemp_periodic(di, true);
901 * ab8500_btemp_external_power_changed() - callback for power supply changes
902 * @psy: pointer to the structure power_supply
904 * This function is pointing to the function pointer external_power_changed
905 * of the structure power_supply.
906 * This function gets executed when there is a change in the external power
907 * supply to the btemp.
909 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
911 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
913 class_for_each_device(power_supply_class, NULL,
914 &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
917 /* ab8500 btemp driver interrupts and their respective isr */
918 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
919 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
920 {"BTEMP_LOW", ab8500_btemp_templow_handler},
921 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
922 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
923 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
926 #if defined(CONFIG_PM)
927 static int ab8500_btemp_resume(struct platform_device *pdev)
929 struct ab8500_btemp *di = platform_get_drvdata(pdev);
931 ab8500_btemp_periodic(di, true);
936 static int ab8500_btemp_suspend(struct platform_device *pdev,
939 struct ab8500_btemp *di = platform_get_drvdata(pdev);
941 ab8500_btemp_periodic(di, false);
946 #define ab8500_btemp_suspend NULL
947 #define ab8500_btemp_resume NULL
950 static int ab8500_btemp_remove(struct platform_device *pdev)
952 struct ab8500_btemp *di = platform_get_drvdata(pdev);
955 /* Disable interrupts */
956 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
957 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
961 /* Delete the work queue */
962 destroy_workqueue(di->btemp_wq);
964 flush_scheduled_work();
965 power_supply_unregister(&di->btemp_psy);
966 platform_set_drvdata(pdev, NULL);
971 static char *supply_interface[] = {
976 static int ab8500_btemp_probe(struct platform_device *pdev)
978 struct device_node *np = pdev->dev.of_node;
979 struct abx500_bm_data *plat = pdev->dev.platform_data;
980 struct ab8500_btemp *di;
984 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
986 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
991 dev_err(&pdev->dev, "no battery management data supplied\n");
997 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
999 dev_err(&pdev->dev, "failed to get battery information\n");
1004 /* get parent data */
1005 di->dev = &pdev->dev;
1006 di->parent = dev_get_drvdata(pdev->dev.parent);
1007 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1009 di->initialized = false;
1012 di->btemp_psy.name = "ab8500_btemp";
1013 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1014 di->btemp_psy.properties = ab8500_btemp_props;
1015 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
1016 di->btemp_psy.get_property = ab8500_btemp_get_property;
1017 di->btemp_psy.supplied_to = supply_interface;
1018 di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
1019 di->btemp_psy.external_power_changed =
1020 ab8500_btemp_external_power_changed;
1023 /* Create a work queue for the btemp */
1025 create_singlethread_workqueue("ab8500_btemp_wq");
1026 if (di->btemp_wq == NULL) {
1027 dev_err(di->dev, "failed to create work queue\n");
1031 /* Init work for measuring temperature periodically */
1032 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1033 ab8500_btemp_periodic_work);
1035 /* Set BTEMP thermal limits. Low and Med are fixed */
1036 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1037 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1039 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1040 AB8500_BTEMP_HIGH_TH, &val);
1042 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1046 case BTEMP_HIGH_TH_57_0:
1047 case BTEMP_HIGH_TH_57_1:
1048 di->btemp_ranges.btemp_high_limit =
1049 BTEMP_THERMAL_HIGH_LIMIT_57;
1051 case BTEMP_HIGH_TH_52:
1052 di->btemp_ranges.btemp_high_limit =
1053 BTEMP_THERMAL_HIGH_LIMIT_52;
1055 case BTEMP_HIGH_TH_62:
1056 di->btemp_ranges.btemp_high_limit =
1057 BTEMP_THERMAL_HIGH_LIMIT_62;
1061 /* Register BTEMP power supply class */
1062 ret = power_supply_register(di->dev, &di->btemp_psy);
1064 dev_err(di->dev, "failed to register BTEMP psy\n");
1068 /* Register interrupts */
1069 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1070 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1071 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1072 IRQF_SHARED | IRQF_NO_SUSPEND,
1073 ab8500_btemp_irq[i].name, di);
1076 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1077 , ab8500_btemp_irq[i].name, irq, ret);
1080 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1081 ab8500_btemp_irq[i].name, irq, ret);
1084 platform_set_drvdata(pdev, di);
1086 /* Kick off periodic temperature measurements */
1087 ab8500_btemp_periodic(di, true);
1088 list_add_tail(&di->node, &ab8500_btemp_list);
1093 power_supply_unregister(&di->btemp_psy);
1095 /* We also have to free all successfully registered irqs */
1096 for (i = i - 1; i >= 0; i--) {
1097 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1101 destroy_workqueue(di->btemp_wq);
1105 static const struct of_device_id ab8500_btemp_match[] = {
1106 { .compatible = "stericsson,ab8500-btemp", },
1110 static struct platform_driver ab8500_btemp_driver = {
1111 .probe = ab8500_btemp_probe,
1112 .remove = ab8500_btemp_remove,
1113 .suspend = ab8500_btemp_suspend,
1114 .resume = ab8500_btemp_resume,
1116 .name = "ab8500-btemp",
1117 .owner = THIS_MODULE,
1118 .of_match_table = ab8500_btemp_match,
1122 static int __init ab8500_btemp_init(void)
1124 return platform_driver_register(&ab8500_btemp_driver);
1127 static void __exit ab8500_btemp_exit(void)
1129 platform_driver_unregister(&ab8500_btemp_driver);
1132 subsys_initcall_sync(ab8500_btemp_init);
1133 module_exit(ab8500_btemp_exit);
1135 MODULE_LICENSE("GPL v2");
1136 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1137 MODULE_ALIAS("platform:ab8500-btemp");
1138 MODULE_DESCRIPTION("AB8500 battery temperature driver");