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/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/iio/consumer.h>
30 #include "ab8500-bm.h"
34 #define BTEMP_THERMAL_LOW_LIMIT -10
35 #define BTEMP_THERMAL_MED_LIMIT 0
36 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
37 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
38 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
40 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
41 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
43 #define BTEMP_BATCTRL_CURR_SRC_16UA 16
44 #define BTEMP_BATCTRL_CURR_SRC_18UA 18
46 #define BTEMP_BATCTRL_CURR_SRC_60UA 60
47 #define BTEMP_BATCTRL_CURR_SRC_120UA 120
50 * struct ab8500_btemp_interrupts - ab8500 interrupts
51 * @name: name of the interrupt
52 * @isr function pointer to the isr
54 struct ab8500_btemp_interrupts {
56 irqreturn_t (*isr)(int irq, void *data);
59 struct ab8500_btemp_events {
69 struct ab8500_btemp_ranges {
76 * struct ab8500_btemp - ab8500 BTEMP device information
77 * @dev: Pointer to the structure device
78 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
79 * @curr_source: What current source we use, in uA
80 * @bat_temp: Dispatched battery temperature in degree Celsius
81 * @prev_bat_temp Last measured battery temperature in degree Celsius
82 * @parent: Pointer to the struct ab8500
83 * @adc_btemp_ball: ADC channel for the battery ball temperature
84 * @adc_bat_ctrl: ADC channel for the battery control
85 * @fg: Pointer to the struct fg
86 * @bm: Platform specific battery management information
87 * @btemp_psy: Structure for BTEMP specific battery properties
88 * @events: Structure for information about events triggered
89 * @btemp_ranges: Battery temperature range structure
90 * @btemp_wq: Work queue for measuring the temperature periodically
91 * @btemp_periodic_work: Work for measuring the temperature periodically
92 * @initialized: True if battery id read.
96 struct list_head node;
100 struct ab8500 *parent;
101 struct iio_channel *btemp_ball;
102 struct iio_channel *bat_ctrl;
103 struct ab8500_fg *fg;
104 struct abx500_bm_data *bm;
105 struct power_supply *btemp_psy;
106 struct ab8500_btemp_events events;
107 struct ab8500_btemp_ranges btemp_ranges;
108 struct workqueue_struct *btemp_wq;
109 struct delayed_work btemp_periodic_work;
113 /* BTEMP power supply properties */
114 static enum power_supply_property ab8500_btemp_props[] = {
115 POWER_SUPPLY_PROP_PRESENT,
116 POWER_SUPPLY_PROP_ONLINE,
117 POWER_SUPPLY_PROP_TECHNOLOGY,
118 POWER_SUPPLY_PROP_TEMP,
121 static LIST_HEAD(ab8500_btemp_list);
124 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
125 * @di: pointer to the ab8500_btemp structure
126 * @v_batctrl: measured batctrl voltage
127 * @inst_curr: measured instant current
129 * This function returns the battery resistance that is
130 * derived from the BATCTRL voltage.
131 * Returns value in Ohms.
133 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
134 int v_batctrl, int inst_curr)
138 if (is_ab8500_1p1_or_earlier(di->parent)) {
140 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
141 * connected to 1.8V through a 450k resistor
143 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
146 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
148 * If the battery has internal NTC, we use the current
149 * source to calculate the resistance.
151 rbs = (v_batctrl * 1000
152 - di->bm->gnd_lift_resistance * inst_curr)
156 * BAT_CTRL is internally
157 * connected to 1.8V through a 80k resistor
159 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
166 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
167 * @di: pointer to the ab8500_btemp structure
169 * This function returns the voltage on BATCTRL. Returns value in mV.
171 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
176 ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
179 "%s ADC conversion failed, using previous value",
188 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
189 * @di: pointer to the ab8500_btemp structure
190 * @enable: enable or disable the current source
192 * Enable or disable the current sources for the BatCtrl AD channel
194 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
201 * BATCTRL current sources are included on AB8500 cut2.0
202 * and future versions
204 if (is_ab8500_1p1_or_earlier(di->parent))
207 /* Only do this for batteries with internal NTC */
208 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
210 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
211 curr = BAT_CTRL_7U_ENA;
213 curr = BAT_CTRL_20U_ENA;
215 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
217 ret = abx500_mask_and_set_register_interruptible(di->dev,
218 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
219 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
221 dev_err(di->dev, "%s failed setting cmp_force\n",
227 * We have to wait one 32kHz cycle before enabling
228 * the current source, since ForceBatCtrlCmpHigh needs
229 * to be written in a separate cycle
233 ret = abx500_set_register_interruptible(di->dev,
234 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
235 FORCE_BAT_CTRL_CMP_HIGH | curr);
237 dev_err(di->dev, "%s failed enabling current source\n",
239 goto disable_curr_source;
241 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
242 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
244 /* Write 0 to the curr bits */
245 ret = abx500_mask_and_set_register_interruptible(
247 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
248 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
249 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
252 dev_err(di->dev, "%s failed disabling current source\n",
254 goto disable_curr_source;
257 /* Enable Pull-Up and comparator */
258 ret = abx500_mask_and_set_register_interruptible(di->dev,
259 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
260 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
261 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
263 dev_err(di->dev, "%s failed enabling PU and comp\n",
269 * We have to wait one 32kHz cycle before disabling
270 * ForceBatCtrlCmpHigh since this needs to be written
271 * in a separate cycle
275 /* Disable 'force comparator' */
276 ret = abx500_mask_and_set_register_interruptible(di->dev,
277 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
278 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
280 dev_err(di->dev, "%s failed disabling force comp\n",
282 goto disable_force_comp;
288 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
289 * if we got an error above
292 /* Write 0 to the curr bits */
293 ret = abx500_mask_and_set_register_interruptible(di->dev,
294 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
295 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
296 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
299 dev_err(di->dev, "%s failed disabling current source\n",
304 /* Enable Pull-Up and comparator */
305 ret = abx500_mask_and_set_register_interruptible(di->dev,
306 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
307 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
308 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
310 dev_err(di->dev, "%s failed enabling PU and comp\n",
317 * We have to wait one 32kHz cycle before disabling
318 * ForceBatCtrlCmpHigh since this needs to be written
319 * in a separate cycle
323 /* Disable 'force comparator' */
324 ret = abx500_mask_and_set_register_interruptible(di->dev,
325 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
326 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
328 dev_err(di->dev, "%s failed disabling force comp\n",
337 * ab8500_btemp_get_batctrl_res() - get battery resistance
338 * @di: pointer to the ab8500_btemp structure
340 * This function returns the battery pack identification resistance.
341 * Returns value in Ohms.
343 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
352 * BATCTRL current sources are included on AB8500 cut2.0
353 * and future versions
355 ret = ab8500_btemp_curr_source_enable(di, true);
357 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
362 di->fg = ab8500_fg_get();
364 dev_err(di->dev, "No fg found\n");
368 ret = ab8500_fg_inst_curr_start(di->fg);
371 dev_err(di->dev, "Failed to start current measurement\n");
377 } while (!ab8500_fg_inst_curr_started(di->fg));
382 batctrl += ab8500_btemp_read_batctrl_voltage(di);
385 } while (!ab8500_fg_inst_curr_done(di->fg));
388 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
390 dev_err(di->dev, "Failed to finalize current measurement\n");
394 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
396 ret = ab8500_btemp_curr_source_enable(di, false);
398 dev_err(di->dev, "%s curr source disable failed\n", __func__);
402 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
403 __func__, batctrl, res, inst_curr, i);
409 * ab8500_btemp_res_to_temp() - resistance to temperature
410 * @di: pointer to the ab8500_btemp structure
411 * @tbl: pointer to the resiatance to temperature table
412 * @tbl_size: size of the resistance to temperature table
413 * @res: resistance to calculate the temperature from
415 * This function returns the battery temperature in degrees Celsius
416 * based on the NTC resistance.
418 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
419 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
423 * Calculate the formula for the straight line
424 * Simple interpolation if we are within
425 * the resistance table limits, extrapolate
426 * if resistance is outside the limits.
428 if (res > tbl[0].resist)
430 else if (res <= tbl[tbl_size - 1].resist)
434 while (!(res <= tbl[i].resist &&
435 res > tbl[i + 1].resist))
439 return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
440 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
444 * ab8500_btemp_measure_temp() - measure battery temperature
445 * @di: pointer to the ab8500_btemp structure
447 * Returns battery temperature (on success) else the previous temperature
449 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
453 int rbat, rntc, vntc;
456 id = di->bm->batt_id;
458 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
459 id != BATTERY_UNKNOWN) {
461 rbat = ab8500_btemp_get_batctrl_res(di);
463 dev_err(di->dev, "%s get batctrl res failed\n",
466 * Return out-of-range temperature so that
467 * charging is stopped
469 return BTEMP_THERMAL_LOW_LIMIT;
472 temp = ab8500_btemp_res_to_temp(di,
473 di->bm->bat_type[id].r_to_t_tbl,
474 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
476 ret = iio_read_channel_processed(di->btemp_ball, &vntc);
479 "%s ADC conversion failed,"
480 " using previous value\n", __func__);
484 * The PCB NTC is sourced from VTVOUT via a 230kOhm
487 rntc = 230000 * vntc / (VTVOUT_V - vntc);
489 temp = ab8500_btemp_res_to_temp(di,
490 di->bm->bat_type[id].r_to_t_tbl,
491 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
494 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
499 * ab8500_btemp_id() - Identify the connected battery
500 * @di: pointer to the ab8500_btemp structure
502 * This function will try to identify the battery by reading the ID
503 * resistor. Some brands use a combined ID resistor with a NTC resistor to
504 * both be able to identify and to read the temperature of it.
506 static int ab8500_btemp_id(struct ab8500_btemp *di)
511 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
512 di->bm->batt_id = BATTERY_UNKNOWN;
514 res = ab8500_btemp_get_batctrl_res(di);
516 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
520 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
521 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
522 if ((res <= di->bm->bat_type[i].resis_high) &&
523 (res >= di->bm->bat_type[i].resis_low)) {
524 dev_dbg(di->dev, "Battery detected on %s"
525 " low %d < res %d < high: %d"
527 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
528 "BATCTRL" : "BATTEMP",
529 di->bm->bat_type[i].resis_low, res,
530 di->bm->bat_type[i].resis_high, i);
537 if (di->bm->batt_id == BATTERY_UNKNOWN) {
538 dev_warn(di->dev, "Battery identified as unknown"
539 ", resistance %d Ohm\n", res);
544 * We only have to change current source if the
545 * detected type is Type 1.
547 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
548 di->bm->batt_id == 1) {
549 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
550 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
553 return di->bm->batt_id;
557 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
558 * @work: pointer to the work_struct structure
560 * Work function for measuring the temperature periodically
562 static void ab8500_btemp_periodic_work(struct work_struct *work)
566 struct ab8500_btemp *di = container_of(work,
567 struct ab8500_btemp, btemp_periodic_work.work);
569 if (!di->initialized) {
570 /* Identify the battery */
571 if (ab8500_btemp_id(di) < 0)
572 dev_warn(di->dev, "failed to identify the battery\n");
575 bat_temp = ab8500_btemp_measure_temp(di);
577 * Filter battery temperature.
578 * Allow direct updates on temperature only if two samples result in
579 * same temperature. Else only allow 1 degree change from previous
580 * reported value in the direction of the new measurement.
582 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
583 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
584 di->initialized = true;
585 di->bat_temp = bat_temp;
586 power_supply_changed(di->btemp_psy);
588 } else if (bat_temp < di->prev_bat_temp) {
590 power_supply_changed(di->btemp_psy);
591 } else if (bat_temp > di->prev_bat_temp) {
593 power_supply_changed(di->btemp_psy);
595 di->prev_bat_temp = bat_temp;
597 if (di->events.ac_conn || di->events.usb_conn)
598 interval = di->bm->temp_interval_chg;
600 interval = di->bm->temp_interval_nochg;
602 /* Schedule a new measurement */
603 queue_delayed_work(di->btemp_wq,
604 &di->btemp_periodic_work,
605 round_jiffies(interval * HZ));
609 * ab8500_btemp_batctrlindb_handler() - battery removal detected
610 * @irq: interrupt number
611 * @_di: void pointer that has to address of ab8500_btemp
613 * Returns IRQ status(IRQ_HANDLED)
615 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
617 struct ab8500_btemp *di = _di;
618 dev_err(di->dev, "Battery removal detected!\n");
620 di->events.batt_rem = true;
621 power_supply_changed(di->btemp_psy);
627 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
628 * @irq: interrupt number
629 * @_di: void pointer that has to address of ab8500_btemp
631 * Returns IRQ status(IRQ_HANDLED)
633 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
635 struct ab8500_btemp *di = _di;
637 if (is_ab8500_3p3_or_earlier(di->parent)) {
638 dev_dbg(di->dev, "Ignore false btemp low irq"
639 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
641 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
643 di->events.btemp_low = true;
644 di->events.btemp_high = false;
645 di->events.btemp_medhigh = false;
646 di->events.btemp_lowmed = false;
647 power_supply_changed(di->btemp_psy);
654 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
655 * @irq: interrupt number
656 * @_di: void pointer that has to address of ab8500_btemp
658 * Returns IRQ status(IRQ_HANDLED)
660 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
662 struct ab8500_btemp *di = _di;
664 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
666 di->events.btemp_high = true;
667 di->events.btemp_medhigh = false;
668 di->events.btemp_lowmed = false;
669 di->events.btemp_low = false;
670 power_supply_changed(di->btemp_psy);
676 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
677 * @irq: interrupt number
678 * @_di: void pointer that has to address of ab8500_btemp
680 * Returns IRQ status(IRQ_HANDLED)
682 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
684 struct ab8500_btemp *di = _di;
686 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
688 di->events.btemp_lowmed = true;
689 di->events.btemp_medhigh = false;
690 di->events.btemp_high = false;
691 di->events.btemp_low = false;
692 power_supply_changed(di->btemp_psy);
698 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
699 * @irq: interrupt number
700 * @_di: void pointer that has to address of ab8500_btemp
702 * Returns IRQ status(IRQ_HANDLED)
704 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
706 struct ab8500_btemp *di = _di;
708 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
710 di->events.btemp_medhigh = true;
711 di->events.btemp_lowmed = false;
712 di->events.btemp_high = false;
713 di->events.btemp_low = false;
714 power_supply_changed(di->btemp_psy);
720 * ab8500_btemp_periodic() - Periodic temperature measurements
721 * @di: pointer to the ab8500_btemp structure
722 * @enable: enable or disable periodic temperature measurements
724 * Starts of stops periodic temperature measurements. Periodic measurements
725 * should only be done when a charger is connected.
727 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
730 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
733 * Make sure a new measurement is done directly by cancelling
736 cancel_delayed_work_sync(&di->btemp_periodic_work);
739 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
743 * ab8500_btemp_get_temp() - get battery temperature
744 * @di: pointer to the ab8500_btemp structure
746 * Returns battery temperature
748 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
753 * The BTEMP events are not reliabe on AB8500 cut3.3
756 if (is_ab8500_3p3_or_earlier(di->parent)) {
757 temp = di->bat_temp * 10;
759 if (di->events.btemp_low) {
760 if (temp > di->btemp_ranges.btemp_low_limit)
761 temp = di->btemp_ranges.btemp_low_limit * 10;
763 temp = di->bat_temp * 10;
764 } else if (di->events.btemp_high) {
765 if (temp < di->btemp_ranges.btemp_high_limit)
766 temp = di->btemp_ranges.btemp_high_limit * 10;
768 temp = di->bat_temp * 10;
769 } else if (di->events.btemp_lowmed) {
770 if (temp > di->btemp_ranges.btemp_med_limit)
771 temp = di->btemp_ranges.btemp_med_limit * 10;
773 temp = di->bat_temp * 10;
774 } else if (di->events.btemp_medhigh) {
775 if (temp < di->btemp_ranges.btemp_med_limit)
776 temp = di->btemp_ranges.btemp_med_limit * 10;
778 temp = di->bat_temp * 10;
780 temp = di->bat_temp * 10;
786 * ab8500_btemp_get_property() - get the btemp properties
787 * @psy: pointer to the power_supply structure
788 * @psp: pointer to the power_supply_property structure
789 * @val: pointer to the power_supply_propval union
791 * This function gets called when an application tries to get the btemp
792 * properties by reading the sysfs files.
793 * online: presence of the battery
794 * present: presence of the battery
795 * technology: battery technology
796 * temp: battery temperature
797 * Returns error code in case of failure else 0(on success)
799 static int ab8500_btemp_get_property(struct power_supply *psy,
800 enum power_supply_property psp,
801 union power_supply_propval *val)
803 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
806 case POWER_SUPPLY_PROP_PRESENT:
807 case POWER_SUPPLY_PROP_ONLINE:
808 if (di->events.batt_rem)
813 case POWER_SUPPLY_PROP_TECHNOLOGY:
814 val->intval = di->bm->bat_type[di->bm->batt_id].name;
816 case POWER_SUPPLY_PROP_TEMP:
817 val->intval = ab8500_btemp_get_temp(di);
825 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
827 struct power_supply *psy;
828 struct power_supply *ext = dev_get_drvdata(dev);
829 const char **supplicants = (const char **)ext->supplied_to;
830 struct ab8500_btemp *di;
831 union power_supply_propval ret;
834 psy = (struct power_supply *)data;
835 di = power_supply_get_drvdata(psy);
838 * For all psy where the name of your driver
839 * appears in any supplied_to
841 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
845 /* Go through all properties for the psy */
846 for (j = 0; j < ext->desc->num_properties; j++) {
847 enum power_supply_property prop;
848 prop = ext->desc->properties[j];
850 if (power_supply_get_property(ext, prop, &ret))
854 case POWER_SUPPLY_PROP_PRESENT:
855 switch (ext->desc->type) {
856 case POWER_SUPPLY_TYPE_MAINS:
857 /* AC disconnected */
858 if (!ret.intval && di->events.ac_conn) {
859 di->events.ac_conn = false;
862 else if (ret.intval && !di->events.ac_conn) {
863 di->events.ac_conn = true;
864 if (!di->events.usb_conn)
865 ab8500_btemp_periodic(di, true);
868 case POWER_SUPPLY_TYPE_USB:
869 /* USB disconnected */
870 if (!ret.intval && di->events.usb_conn) {
871 di->events.usb_conn = false;
874 else if (ret.intval && !di->events.usb_conn) {
875 di->events.usb_conn = true;
876 if (!di->events.ac_conn)
877 ab8500_btemp_periodic(di, true);
892 * ab8500_btemp_external_power_changed() - callback for power supply changes
893 * @psy: pointer to the structure power_supply
895 * This function is pointing to the function pointer external_power_changed
896 * of the structure power_supply.
897 * This function gets executed when there is a change in the external power
898 * supply to the btemp.
900 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
902 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
904 class_for_each_device(power_supply_class, NULL,
905 di->btemp_psy, ab8500_btemp_get_ext_psy_data);
908 /* ab8500 btemp driver interrupts and their respective isr */
909 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
910 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
911 {"BTEMP_LOW", ab8500_btemp_templow_handler},
912 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
913 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
914 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
917 static int __maybe_unused ab8500_btemp_resume(struct device *dev)
919 struct ab8500_btemp *di = dev_get_drvdata(dev);
921 ab8500_btemp_periodic(di, true);
926 static int __maybe_unused ab8500_btemp_suspend(struct device *dev)
928 struct ab8500_btemp *di = dev_get_drvdata(dev);
930 ab8500_btemp_periodic(di, false);
935 static int ab8500_btemp_remove(struct platform_device *pdev)
937 struct ab8500_btemp *di = platform_get_drvdata(pdev);
940 /* Disable interrupts */
941 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
942 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
946 /* Delete the work queue */
947 destroy_workqueue(di->btemp_wq);
949 flush_scheduled_work();
950 power_supply_unregister(di->btemp_psy);
955 static char *supply_interface[] = {
960 static const struct power_supply_desc ab8500_btemp_desc = {
961 .name = "ab8500_btemp",
962 .type = POWER_SUPPLY_TYPE_BATTERY,
963 .properties = ab8500_btemp_props,
964 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
965 .get_property = ab8500_btemp_get_property,
966 .external_power_changed = ab8500_btemp_external_power_changed,
969 static int ab8500_btemp_probe(struct platform_device *pdev)
971 struct device_node *np = pdev->dev.of_node;
972 struct power_supply_config psy_cfg = {};
973 struct device *dev = &pdev->dev;
974 struct ab8500_btemp *di;
978 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
982 di->bm = &ab8500_bm_data;
984 ret = ab8500_bm_of_probe(dev, np, di->bm);
986 dev_err(dev, "failed to get battery information\n");
990 /* get parent data */
992 di->parent = dev_get_drvdata(pdev->dev.parent);
994 /* Get ADC channels */
995 di->btemp_ball = devm_iio_channel_get(dev, "btemp_ball");
996 if (IS_ERR(di->btemp_ball)) {
997 ret = dev_err_probe(dev, PTR_ERR(di->btemp_ball),
998 "failed to get BTEMP BALL ADC channel\n");
1001 di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl");
1002 if (IS_ERR(di->bat_ctrl)) {
1003 ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl),
1004 "failed to get BAT CTRL ADC channel\n");
1008 di->initialized = false;
1010 psy_cfg.supplied_to = supply_interface;
1011 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1012 psy_cfg.drv_data = di;
1014 /* Create a work queue for the btemp */
1016 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
1017 if (di->btemp_wq == NULL) {
1018 dev_err(dev, "failed to create work queue\n");
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 = 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 ret = PTR_ERR(di->btemp_psy);
1061 /* Register interrupts */
1062 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1063 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1069 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1070 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
1071 ab8500_btemp_irq[i].name, di);
1074 dev_err(dev, "failed to request %s IRQ %d: %d\n"
1075 , ab8500_btemp_irq[i].name, irq, ret);
1078 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
1079 ab8500_btemp_irq[i].name, irq, ret);
1082 platform_set_drvdata(pdev, di);
1084 /* Kick off periodic temperature measurements */
1085 ab8500_btemp_periodic(di, true);
1086 list_add_tail(&di->node, &ab8500_btemp_list);
1091 /* We also have to free all successfully registered irqs */
1092 for (i = i - 1; i >= 0; i--) {
1093 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1097 power_supply_unregister(di->btemp_psy);
1099 destroy_workqueue(di->btemp_wq);
1103 static SIMPLE_DEV_PM_OPS(ab8500_btemp_pm_ops, ab8500_btemp_suspend, ab8500_btemp_resume);
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,
1114 .name = "ab8500-btemp",
1115 .of_match_table = ab8500_btemp_match,
1116 .pm = &ab8500_btemp_pm_ops,
1120 static int __init ab8500_btemp_init(void)
1122 return platform_driver_register(&ab8500_btemp_driver);
1125 static void __exit ab8500_btemp_exit(void)
1127 platform_driver_unregister(&ab8500_btemp_driver);
1130 device_initcall(ab8500_btemp_init);
1131 module_exit(ab8500_btemp_exit);
1133 MODULE_LICENSE("GPL v2");
1134 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1135 MODULE_ALIAS("platform:ab8500-btemp");
1136 MODULE_DESCRIPTION("AB8500 battery temperature driver");