1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
5 * Battery temperature driver for AB8500
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/component.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/workqueue.h>
24 #include <linux/jiffies.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/abx500.h>
28 #include <linux/mfd/abx500/ab8500.h>
29 #include <linux/iio/consumer.h>
31 #include "ab8500-bm.h"
35 #define BTEMP_THERMAL_LOW_LIMIT -10
36 #define BTEMP_THERMAL_MED_LIMIT 0
37 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
38 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
39 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
41 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
42 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
44 #define BTEMP_BATCTRL_CURR_SRC_16UA 16
45 #define BTEMP_BATCTRL_CURR_SRC_18UA 18
47 #define BTEMP_BATCTRL_CURR_SRC_60UA 60
48 #define BTEMP_BATCTRL_CURR_SRC_120UA 120
51 * struct ab8500_btemp_interrupts - ab8500 interrupts
52 * @name: name of the interrupt
53 * @isr function pointer to the isr
55 struct ab8500_btemp_interrupts {
57 irqreturn_t (*isr)(int irq, void *data);
60 struct ab8500_btemp_events {
70 struct ab8500_btemp_ranges {
77 * struct ab8500_btemp - ab8500 BTEMP device information
78 * @dev: Pointer to the structure device
79 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
80 * @curr_source: What current source we use, in uA
81 * @bat_temp: Dispatched battery temperature in degree Celsius
82 * @prev_bat_temp Last measured battery temperature in degree Celsius
83 * @parent: Pointer to the struct ab8500
84 * @adc_btemp_ball: ADC channel for the battery ball temperature
85 * @adc_bat_ctrl: ADC channel for the battery control
86 * @fg: Pointer to the struct fg
87 * @bm: Platform specific battery management information
88 * @btemp_psy: Structure for BTEMP specific battery properties
89 * @events: Structure for information about events triggered
90 * @btemp_ranges: Battery temperature range structure
91 * @btemp_wq: Work queue for measuring the temperature periodically
92 * @btemp_periodic_work: Work for measuring the temperature periodically
93 * @initialized: True if battery id read.
97 struct list_head node;
101 struct ab8500 *parent;
102 struct iio_channel *btemp_ball;
103 struct iio_channel *bat_ctrl;
104 struct ab8500_fg *fg;
105 struct abx500_bm_data *bm;
106 struct power_supply *btemp_psy;
107 struct ab8500_btemp_events events;
108 struct ab8500_btemp_ranges btemp_ranges;
109 struct workqueue_struct *btemp_wq;
110 struct delayed_work btemp_periodic_work;
114 /* BTEMP power supply properties */
115 static enum power_supply_property ab8500_btemp_props[] = {
116 POWER_SUPPLY_PROP_PRESENT,
117 POWER_SUPPLY_PROP_ONLINE,
118 POWER_SUPPLY_PROP_TECHNOLOGY,
119 POWER_SUPPLY_PROP_TEMP,
122 static LIST_HEAD(ab8500_btemp_list);
125 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
126 * @di: pointer to the ab8500_btemp structure
127 * @v_batctrl: measured batctrl voltage
128 * @inst_curr: measured instant current
130 * This function returns the battery resistance that is
131 * derived from the BATCTRL voltage.
132 * Returns value in Ohms.
134 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
135 int v_batctrl, int inst_curr)
139 if (is_ab8500_1p1_or_earlier(di->parent)) {
141 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
142 * connected to 1.8V through a 450k resistor
144 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
147 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
149 * If the battery has internal NTC, we use the current
150 * source to calculate the resistance.
152 rbs = (v_batctrl * 1000
153 - di->bm->gnd_lift_resistance * inst_curr)
157 * BAT_CTRL is internally
158 * connected to 1.8V through a 80k resistor
160 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
167 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
168 * @di: pointer to the ab8500_btemp structure
170 * This function returns the voltage on BATCTRL. Returns value in mV.
172 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
177 ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
180 "%s ADC conversion failed, using previous value",
189 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
190 * @di: pointer to the ab8500_btemp structure
191 * @enable: enable or disable the current source
193 * Enable or disable the current sources for the BatCtrl AD channel
195 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
202 * BATCTRL current sources are included on AB8500 cut2.0
203 * and future versions
205 if (is_ab8500_1p1_or_earlier(di->parent))
208 /* Only do this for batteries with internal NTC */
209 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
211 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
212 curr = BAT_CTRL_7U_ENA;
214 curr = BAT_CTRL_20U_ENA;
216 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
218 ret = abx500_mask_and_set_register_interruptible(di->dev,
219 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
220 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
222 dev_err(di->dev, "%s failed setting cmp_force\n",
228 * We have to wait one 32kHz cycle before enabling
229 * the current source, since ForceBatCtrlCmpHigh needs
230 * to be written in a separate cycle
234 ret = abx500_set_register_interruptible(di->dev,
235 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
236 FORCE_BAT_CTRL_CMP_HIGH | curr);
238 dev_err(di->dev, "%s failed enabling current source\n",
240 goto disable_curr_source;
242 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
243 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
245 /* Write 0 to the curr bits */
246 ret = abx500_mask_and_set_register_interruptible(
248 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
249 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
250 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
253 dev_err(di->dev, "%s failed disabling current source\n",
255 goto disable_curr_source;
258 /* Enable Pull-Up and comparator */
259 ret = abx500_mask_and_set_register_interruptible(di->dev,
260 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
261 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
262 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
264 dev_err(di->dev, "%s failed enabling PU and comp\n",
270 * We have to wait one 32kHz cycle before disabling
271 * ForceBatCtrlCmpHigh since this needs to be written
272 * in a separate cycle
276 /* Disable 'force comparator' */
277 ret = abx500_mask_and_set_register_interruptible(di->dev,
278 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
279 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
281 dev_err(di->dev, "%s failed disabling force comp\n",
283 goto disable_force_comp;
289 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
290 * if we got an error above
293 /* Write 0 to the curr bits */
294 ret = abx500_mask_and_set_register_interruptible(di->dev,
295 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
296 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
297 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
300 dev_err(di->dev, "%s failed disabling current source\n",
305 /* Enable Pull-Up and comparator */
306 ret = abx500_mask_and_set_register_interruptible(di->dev,
307 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
308 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
309 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
311 dev_err(di->dev, "%s failed enabling PU and comp\n",
318 * We have to wait one 32kHz cycle before disabling
319 * ForceBatCtrlCmpHigh since this needs to be written
320 * in a separate cycle
324 /* Disable 'force comparator' */
325 ret = abx500_mask_and_set_register_interruptible(di->dev,
326 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
327 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
329 dev_err(di->dev, "%s failed disabling force comp\n",
338 * ab8500_btemp_get_batctrl_res() - get battery resistance
339 * @di: pointer to the ab8500_btemp structure
341 * This function returns the battery pack identification resistance.
342 * Returns value in Ohms.
344 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
353 * BATCTRL current sources are included on AB8500 cut2.0
354 * and future versions
356 ret = ab8500_btemp_curr_source_enable(di, true);
358 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
363 di->fg = ab8500_fg_get();
365 dev_err(di->dev, "No fg found\n");
369 ret = ab8500_fg_inst_curr_start(di->fg);
372 dev_err(di->dev, "Failed to start current measurement\n");
378 } while (!ab8500_fg_inst_curr_started(di->fg));
383 batctrl += ab8500_btemp_read_batctrl_voltage(di);
386 } while (!ab8500_fg_inst_curr_done(di->fg));
389 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
391 dev_err(di->dev, "Failed to finalize current measurement\n");
395 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
397 ret = ab8500_btemp_curr_source_enable(di, false);
399 dev_err(di->dev, "%s curr source disable failed\n", __func__);
403 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
404 __func__, batctrl, res, inst_curr, i);
410 * ab8500_btemp_res_to_temp() - resistance to temperature
411 * @di: pointer to the ab8500_btemp structure
412 * @tbl: pointer to the resiatance to temperature table
413 * @tbl_size: size of the resistance to temperature table
414 * @res: resistance to calculate the temperature from
416 * This function returns the battery temperature in degrees Celsius
417 * based on the NTC resistance.
419 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
420 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
424 * Calculate the formula for the straight line
425 * Simple interpolation if we are within
426 * the resistance table limits, extrapolate
427 * if resistance is outside the limits.
429 if (res > tbl[0].resist)
431 else if (res <= tbl[tbl_size - 1].resist)
435 while (!(res <= tbl[i].resist &&
436 res > tbl[i + 1].resist))
440 return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
441 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
445 * ab8500_btemp_measure_temp() - measure battery temperature
446 * @di: pointer to the ab8500_btemp structure
448 * Returns battery temperature (on success) else the previous temperature
450 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
454 int rbat, rntc, vntc;
457 id = di->bm->batt_id;
459 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
460 id != BATTERY_UNKNOWN) {
462 rbat = ab8500_btemp_get_batctrl_res(di);
464 dev_err(di->dev, "%s get batctrl res failed\n",
467 * Return out-of-range temperature so that
468 * charging is stopped
470 return BTEMP_THERMAL_LOW_LIMIT;
473 temp = ab8500_btemp_res_to_temp(di,
474 di->bm->bat_type[id].r_to_t_tbl,
475 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
477 ret = iio_read_channel_processed(di->btemp_ball, &vntc);
480 "%s ADC conversion failed,"
481 " using previous value\n", __func__);
485 * The PCB NTC is sourced from VTVOUT via a 230kOhm
488 rntc = 230000 * vntc / (VTVOUT_V - vntc);
490 temp = ab8500_btemp_res_to_temp(di,
491 di->bm->bat_type[id].r_to_t_tbl,
492 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
495 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
500 * ab8500_btemp_id() - Identify the connected battery
501 * @di: pointer to the ab8500_btemp structure
503 * This function will try to identify the battery by reading the ID
504 * resistor. Some brands use a combined ID resistor with a NTC resistor to
505 * both be able to identify and to read the temperature of it.
507 static int ab8500_btemp_id(struct ab8500_btemp *di)
512 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
513 di->bm->batt_id = BATTERY_UNKNOWN;
515 res = ab8500_btemp_get_batctrl_res(di);
517 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
521 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
522 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
523 if ((res <= di->bm->bat_type[i].resis_high) &&
524 (res >= di->bm->bat_type[i].resis_low)) {
525 dev_dbg(di->dev, "Battery detected on %s"
526 " low %d < res %d < high: %d"
528 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
529 "BATCTRL" : "BATTEMP",
530 di->bm->bat_type[i].resis_low, res,
531 di->bm->bat_type[i].resis_high, i);
538 if (di->bm->batt_id == BATTERY_UNKNOWN) {
539 dev_warn(di->dev, "Battery identified as unknown"
540 ", resistance %d Ohm\n", res);
545 * We only have to change current source if the
546 * detected type is Type 1.
548 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
549 di->bm->batt_id == 1) {
550 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
551 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
554 return di->bm->batt_id;
558 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
559 * @work: pointer to the work_struct structure
561 * Work function for measuring the temperature periodically
563 static void ab8500_btemp_periodic_work(struct work_struct *work)
567 struct ab8500_btemp *di = container_of(work,
568 struct ab8500_btemp, btemp_periodic_work.work);
570 if (!di->initialized) {
571 /* Identify the battery */
572 if (ab8500_btemp_id(di) < 0)
573 dev_warn(di->dev, "failed to identify the battery\n");
576 bat_temp = ab8500_btemp_measure_temp(di);
578 * Filter battery temperature.
579 * Allow direct updates on temperature only if two samples result in
580 * same temperature. Else only allow 1 degree change from previous
581 * reported value in the direction of the new measurement.
583 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
584 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
585 di->initialized = true;
586 di->bat_temp = bat_temp;
587 power_supply_changed(di->btemp_psy);
589 } else if (bat_temp < di->prev_bat_temp) {
591 power_supply_changed(di->btemp_psy);
592 } else if (bat_temp > di->prev_bat_temp) {
594 power_supply_changed(di->btemp_psy);
596 di->prev_bat_temp = bat_temp;
598 if (di->events.ac_conn || di->events.usb_conn)
599 interval = di->bm->temp_interval_chg;
601 interval = di->bm->temp_interval_nochg;
603 /* Schedule a new measurement */
604 queue_delayed_work(di->btemp_wq,
605 &di->btemp_periodic_work,
606 round_jiffies(interval * HZ));
610 * ab8500_btemp_batctrlindb_handler() - battery removal detected
611 * @irq: interrupt number
612 * @_di: void pointer that has to address of ab8500_btemp
614 * Returns IRQ status(IRQ_HANDLED)
616 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
618 struct ab8500_btemp *di = _di;
619 dev_err(di->dev, "Battery removal detected!\n");
621 di->events.batt_rem = true;
622 power_supply_changed(di->btemp_psy);
628 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
629 * @irq: interrupt number
630 * @_di: void pointer that has to address of ab8500_btemp
632 * Returns IRQ status(IRQ_HANDLED)
634 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
636 struct ab8500_btemp *di = _di;
638 if (is_ab8500_3p3_or_earlier(di->parent)) {
639 dev_dbg(di->dev, "Ignore false btemp low irq"
640 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
642 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
644 di->events.btemp_low = true;
645 di->events.btemp_high = false;
646 di->events.btemp_medhigh = false;
647 di->events.btemp_lowmed = false;
648 power_supply_changed(di->btemp_psy);
655 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
656 * @irq: interrupt number
657 * @_di: void pointer that has to address of ab8500_btemp
659 * Returns IRQ status(IRQ_HANDLED)
661 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
663 struct ab8500_btemp *di = _di;
665 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
667 di->events.btemp_high = true;
668 di->events.btemp_medhigh = false;
669 di->events.btemp_lowmed = false;
670 di->events.btemp_low = false;
671 power_supply_changed(di->btemp_psy);
677 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
678 * @irq: interrupt number
679 * @_di: void pointer that has to address of ab8500_btemp
681 * Returns IRQ status(IRQ_HANDLED)
683 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
685 struct ab8500_btemp *di = _di;
687 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
689 di->events.btemp_lowmed = true;
690 di->events.btemp_medhigh = false;
691 di->events.btemp_high = false;
692 di->events.btemp_low = false;
693 power_supply_changed(di->btemp_psy);
699 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
700 * @irq: interrupt number
701 * @_di: void pointer that has to address of ab8500_btemp
703 * Returns IRQ status(IRQ_HANDLED)
705 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
707 struct ab8500_btemp *di = _di;
709 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
711 di->events.btemp_medhigh = true;
712 di->events.btemp_lowmed = false;
713 di->events.btemp_high = false;
714 di->events.btemp_low = false;
715 power_supply_changed(di->btemp_psy);
721 * ab8500_btemp_periodic() - Periodic temperature measurements
722 * @di: pointer to the ab8500_btemp structure
723 * @enable: enable or disable periodic temperature measurements
725 * Starts of stops periodic temperature measurements. Periodic measurements
726 * should only be done when a charger is connected.
728 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
731 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
734 * Make sure a new measurement is done directly by cancelling
737 cancel_delayed_work_sync(&di->btemp_periodic_work);
740 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
744 * ab8500_btemp_get_temp() - get battery temperature
745 * @di: pointer to the ab8500_btemp structure
747 * Returns battery temperature
749 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
754 * The BTEMP events are not reliabe on AB8500 cut3.3
757 if (is_ab8500_3p3_or_earlier(di->parent)) {
758 temp = di->bat_temp * 10;
760 if (di->events.btemp_low) {
761 if (temp > di->btemp_ranges.btemp_low_limit)
762 temp = di->btemp_ranges.btemp_low_limit * 10;
764 temp = di->bat_temp * 10;
765 } else if (di->events.btemp_high) {
766 if (temp < di->btemp_ranges.btemp_high_limit)
767 temp = di->btemp_ranges.btemp_high_limit * 10;
769 temp = di->bat_temp * 10;
770 } else if (di->events.btemp_lowmed) {
771 if (temp > di->btemp_ranges.btemp_med_limit)
772 temp = di->btemp_ranges.btemp_med_limit * 10;
774 temp = di->bat_temp * 10;
775 } else if (di->events.btemp_medhigh) {
776 if (temp < di->btemp_ranges.btemp_med_limit)
777 temp = di->btemp_ranges.btemp_med_limit * 10;
779 temp = di->bat_temp * 10;
781 temp = di->bat_temp * 10;
787 * ab8500_btemp_get_property() - get the btemp properties
788 * @psy: pointer to the power_supply structure
789 * @psp: pointer to the power_supply_property structure
790 * @val: pointer to the power_supply_propval union
792 * This function gets called when an application tries to get the btemp
793 * properties by reading the sysfs files.
794 * online: presence of the battery
795 * present: presence of the battery
796 * technology: battery technology
797 * temp: battery temperature
798 * Returns error code in case of failure else 0(on success)
800 static int ab8500_btemp_get_property(struct power_supply *psy,
801 enum power_supply_property psp,
802 union power_supply_propval *val)
804 struct ab8500_btemp *di = power_supply_get_drvdata(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 = dev_get_drvdata(dev);
830 const char **supplicants = (const char **)ext->supplied_to;
831 struct ab8500_btemp *di;
832 union power_supply_propval ret;
835 psy = (struct power_supply *)data;
836 di = power_supply_get_drvdata(psy);
839 * For all psy where the name of your driver
840 * appears in any supplied_to
842 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
846 /* Go through all properties for the psy */
847 for (j = 0; j < ext->desc->num_properties; j++) {
848 enum power_supply_property prop;
849 prop = ext->desc->properties[j];
851 if (power_supply_get_property(ext, prop, &ret))
855 case POWER_SUPPLY_PROP_PRESENT:
856 switch (ext->desc->type) {
857 case POWER_SUPPLY_TYPE_MAINS:
858 /* AC disconnected */
859 if (!ret.intval && di->events.ac_conn) {
860 di->events.ac_conn = false;
863 else if (ret.intval && !di->events.ac_conn) {
864 di->events.ac_conn = true;
865 if (!di->events.usb_conn)
866 ab8500_btemp_periodic(di, true);
869 case POWER_SUPPLY_TYPE_USB:
870 /* USB disconnected */
871 if (!ret.intval && di->events.usb_conn) {
872 di->events.usb_conn = false;
875 else if (ret.intval && !di->events.usb_conn) {
876 di->events.usb_conn = true;
877 if (!di->events.ac_conn)
878 ab8500_btemp_periodic(di, true);
893 * ab8500_btemp_external_power_changed() - callback for power supply changes
894 * @psy: pointer to the structure power_supply
896 * This function is pointing to the function pointer external_power_changed
897 * of the structure power_supply.
898 * This function gets executed when there is a change in the external power
899 * supply to the btemp.
901 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
903 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
905 class_for_each_device(power_supply_class, NULL,
906 di->btemp_psy, ab8500_btemp_get_ext_psy_data);
909 /* ab8500 btemp driver interrupts and their respective isr */
910 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
911 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
912 {"BTEMP_LOW", ab8500_btemp_templow_handler},
913 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
914 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
915 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
918 static int __maybe_unused ab8500_btemp_resume(struct device *dev)
920 struct ab8500_btemp *di = dev_get_drvdata(dev);
922 ab8500_btemp_periodic(di, true);
927 static int __maybe_unused ab8500_btemp_suspend(struct device *dev)
929 struct ab8500_btemp *di = dev_get_drvdata(dev);
931 ab8500_btemp_periodic(di, false);
936 static char *supply_interface[] = {
941 static const struct power_supply_desc ab8500_btemp_desc = {
942 .name = "ab8500_btemp",
943 .type = POWER_SUPPLY_TYPE_BATTERY,
944 .properties = ab8500_btemp_props,
945 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
946 .get_property = ab8500_btemp_get_property,
947 .external_power_changed = ab8500_btemp_external_power_changed,
950 static int ab8500_btemp_bind(struct device *dev, struct device *master,
953 struct ab8500_btemp *di = dev_get_drvdata(dev);
955 /* Create a work queue for the btemp */
957 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
958 if (di->btemp_wq == NULL) {
959 dev_err(dev, "failed to create work queue\n");
963 /* Kick off periodic temperature measurements */
964 ab8500_btemp_periodic(di, true);
969 static void ab8500_btemp_unbind(struct device *dev, struct device *master,
972 struct ab8500_btemp *di = dev_get_drvdata(dev);
974 /* Delete the work queue */
975 destroy_workqueue(di->btemp_wq);
976 flush_scheduled_work();
979 static const struct component_ops ab8500_btemp_component_ops = {
980 .bind = ab8500_btemp_bind,
981 .unbind = ab8500_btemp_unbind,
984 static int ab8500_btemp_probe(struct platform_device *pdev)
986 struct power_supply_config psy_cfg = {};
987 struct device *dev = &pdev->dev;
988 struct ab8500_btemp *di;
992 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
996 di->bm = &ab8500_bm_data;
998 /* get parent data */
1000 di->parent = dev_get_drvdata(pdev->dev.parent);
1002 /* Get ADC channels */
1003 di->btemp_ball = devm_iio_channel_get(dev, "btemp_ball");
1004 if (IS_ERR(di->btemp_ball)) {
1005 ret = dev_err_probe(dev, PTR_ERR(di->btemp_ball),
1006 "failed to get BTEMP BALL ADC channel\n");
1009 di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl");
1010 if (IS_ERR(di->bat_ctrl)) {
1011 ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl),
1012 "failed to get BAT CTRL ADC channel\n");
1016 di->initialized = false;
1018 psy_cfg.supplied_to = supply_interface;
1019 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1020 psy_cfg.drv_data = di;
1022 /* Init work for measuring temperature periodically */
1023 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1024 ab8500_btemp_periodic_work);
1026 /* Set BTEMP thermal limits. Low and Med are fixed */
1027 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1028 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1030 ret = abx500_get_register_interruptible(dev, AB8500_CHARGER,
1031 AB8500_BTEMP_HIGH_TH, &val);
1033 dev_err(dev, "%s ab8500 read failed\n", __func__);
1037 case BTEMP_HIGH_TH_57_0:
1038 case BTEMP_HIGH_TH_57_1:
1039 di->btemp_ranges.btemp_high_limit =
1040 BTEMP_THERMAL_HIGH_LIMIT_57;
1042 case BTEMP_HIGH_TH_52:
1043 di->btemp_ranges.btemp_high_limit =
1044 BTEMP_THERMAL_HIGH_LIMIT_52;
1046 case BTEMP_HIGH_TH_62:
1047 di->btemp_ranges.btemp_high_limit =
1048 BTEMP_THERMAL_HIGH_LIMIT_62;
1052 /* Register BTEMP power supply class */
1053 di->btemp_psy = devm_power_supply_register(dev, &ab8500_btemp_desc,
1055 if (IS_ERR(di->btemp_psy)) {
1056 dev_err(dev, "failed to register BTEMP psy\n");
1057 return PTR_ERR(di->btemp_psy);
1060 /* Register interrupts */
1061 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1062 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1066 ret = devm_request_threaded_irq(dev, irq, NULL,
1067 ab8500_btemp_irq[i].isr,
1068 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
1069 ab8500_btemp_irq[i].name, di);
1072 dev_err(dev, "failed to request %s IRQ %d: %d\n"
1073 , ab8500_btemp_irq[i].name, irq, ret);
1076 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
1077 ab8500_btemp_irq[i].name, irq, ret);
1080 platform_set_drvdata(pdev, di);
1082 list_add_tail(&di->node, &ab8500_btemp_list);
1084 return component_add(dev, &ab8500_btemp_component_ops);
1087 static int ab8500_btemp_remove(struct platform_device *pdev)
1089 component_del(&pdev->dev, &ab8500_btemp_component_ops);
1094 static SIMPLE_DEV_PM_OPS(ab8500_btemp_pm_ops, ab8500_btemp_suspend, ab8500_btemp_resume);
1096 static const struct of_device_id ab8500_btemp_match[] = {
1097 { .compatible = "stericsson,ab8500-btemp", },
1100 MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
1102 struct platform_driver ab8500_btemp_driver = {
1103 .probe = ab8500_btemp_probe,
1104 .remove = ab8500_btemp_remove,
1106 .name = "ab8500-btemp",
1107 .of_match_table = ab8500_btemp_match,
1108 .pm = &ab8500_btemp_pm_ops,
1111 MODULE_LICENSE("GPL v2");
1112 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1113 MODULE_ALIAS("platform:ab8500-btemp");
1114 MODULE_DESCRIPTION("AB8500 battery temperature driver");