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>
30 #include <linux/fixp-arith.h>
32 #include "ab8500-bm.h"
36 #define BTEMP_THERMAL_LOW_LIMIT -10
37 #define BTEMP_THERMAL_MED_LIMIT 0
38 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
39 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
40 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
42 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
43 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
45 #define BTEMP_BATCTRL_CURR_SRC_16UA 16
46 #define BTEMP_BATCTRL_CURR_SRC_18UA 18
48 #define BTEMP_BATCTRL_CURR_SRC_60UA 60
49 #define BTEMP_BATCTRL_CURR_SRC_120UA 120
52 * struct ab8500_btemp_interrupts - ab8500 interrupts
53 * @name: name of the interrupt
54 * @isr function pointer to the isr
56 struct ab8500_btemp_interrupts {
58 irqreturn_t (*isr)(int irq, void *data);
61 struct ab8500_btemp_events {
71 struct ab8500_btemp_ranges {
78 * struct ab8500_btemp - ab8500 BTEMP device information
79 * @dev: Pointer to the structure device
80 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
81 * @curr_source: What current source we use, in uA
82 * @bat_temp: Dispatched battery temperature in degree Celsius
83 * @prev_bat_temp Last measured battery temperature in degree Celsius
84 * @parent: Pointer to the struct ab8500
85 * @adc_btemp_ball: ADC channel for the battery ball temperature
86 * @adc_bat_ctrl: ADC channel for the battery control
87 * @fg: Pointer to the struct fg
88 * @bm: Platform specific battery management information
89 * @btemp_psy: Structure for BTEMP specific battery properties
90 * @events: Structure for information about events triggered
91 * @btemp_ranges: Battery temperature range structure
92 * @btemp_wq: Work queue for measuring the temperature periodically
93 * @btemp_periodic_work: Work for measuring the temperature periodically
94 * @initialized: True if battery id read.
98 struct list_head node;
102 struct ab8500 *parent;
103 struct iio_channel *btemp_ball;
104 struct iio_channel *bat_ctrl;
105 struct ab8500_fg *fg;
106 struct ab8500_bm_data *bm;
107 struct power_supply *btemp_psy;
108 struct ab8500_btemp_events events;
109 struct ab8500_btemp_ranges btemp_ranges;
110 struct workqueue_struct *btemp_wq;
111 struct delayed_work btemp_periodic_work;
115 /* BTEMP power supply properties */
116 static enum power_supply_property ab8500_btemp_props[] = {
117 POWER_SUPPLY_PROP_PRESENT,
118 POWER_SUPPLY_PROP_ONLINE,
119 POWER_SUPPLY_PROP_TECHNOLOGY,
120 POWER_SUPPLY_PROP_TEMP,
123 static LIST_HEAD(ab8500_btemp_list);
126 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
127 * @di: pointer to the ab8500_btemp structure
128 * @v_batctrl: measured batctrl voltage
129 * @inst_curr: measured instant current
131 * This function returns the battery resistance that is
132 * derived from the BATCTRL voltage.
133 * Returns value in Ohms.
135 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
136 int v_batctrl, int inst_curr)
140 if (is_ab8500_1p1_or_earlier(di->parent)) {
142 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
143 * connected to 1.8V through a 450k resistor
145 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
148 if (di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL) {
150 * If the battery has internal NTC, we use the current
151 * source to calculate the resistance.
153 rbs = (v_batctrl * 1000
154 - di->bm->gnd_lift_resistance * inst_curr)
158 * BAT_CTRL is internally
159 * connected to 1.8V through a 80k resistor
161 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
168 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
169 * @di: pointer to the ab8500_btemp structure
171 * This function returns the voltage on BATCTRL. Returns value in mV.
173 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
178 ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
181 "%s ADC conversion failed, using previous value",
190 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
191 * @di: pointer to the ab8500_btemp structure
192 * @enable: enable or disable the current source
194 * Enable or disable the current sources for the BatCtrl AD channel
196 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
203 * BATCTRL current sources are included on AB8500 cut2.0
204 * and future versions
206 if (is_ab8500_1p1_or_earlier(di->parent))
209 /* Only do this for batteries with internal NTC */
210 if (di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL && enable) {
212 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
213 curr = BAT_CTRL_7U_ENA;
215 curr = BAT_CTRL_20U_ENA;
217 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
219 ret = abx500_mask_and_set_register_interruptible(di->dev,
220 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
221 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
223 dev_err(di->dev, "%s failed setting cmp_force\n",
229 * We have to wait one 32kHz cycle before enabling
230 * the current source, since ForceBatCtrlCmpHigh needs
231 * to be written in a separate cycle
235 ret = abx500_set_register_interruptible(di->dev,
236 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
237 FORCE_BAT_CTRL_CMP_HIGH | curr);
239 dev_err(di->dev, "%s failed enabling current source\n",
241 goto disable_curr_source;
243 } else if (di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL && !enable) {
244 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
246 /* Write 0 to the curr bits */
247 ret = abx500_mask_and_set_register_interruptible(
249 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
250 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
251 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
254 dev_err(di->dev, "%s failed disabling current source\n",
256 goto disable_curr_source;
259 /* Enable Pull-Up and comparator */
260 ret = abx500_mask_and_set_register_interruptible(di->dev,
261 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
262 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
263 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
265 dev_err(di->dev, "%s failed enabling PU and comp\n",
271 * We have to wait one 32kHz cycle before disabling
272 * ForceBatCtrlCmpHigh since this needs to be written
273 * in a separate cycle
277 /* Disable 'force comparator' */
278 ret = abx500_mask_and_set_register_interruptible(di->dev,
279 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
280 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
282 dev_err(di->dev, "%s failed disabling force comp\n",
284 goto disable_force_comp;
290 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
291 * if we got an error above
294 /* Write 0 to the curr bits */
295 ret = abx500_mask_and_set_register_interruptible(di->dev,
296 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
297 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
298 ~(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 Celsius
418 * based on the NTC resistance.
420 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
421 const struct ab8500_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 return fixp_linear_interpolate(tbl[i].resist, tbl[i].temp,
442 tbl[i + 1].resist, tbl[i + 1].temp,
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)
454 struct power_supply_battery_info *bi = di->bm->bi;
457 int rbat, rntc, vntc;
459 if ((di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL) &&
460 (bi && (bi->technology == POWER_SUPPLY_TECHNOLOGY_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->r_to_t_tbl,
475 di->bm->bat_type->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->r_to_t_tbl,
492 di->bm->bat_type->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;
514 res = ab8500_btemp_get_batctrl_res(di);
516 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
520 if ((res <= di->bm->bat_type->resis_high) &&
521 (res >= di->bm->bat_type->resis_low)) {
522 dev_info(di->dev, "Battery detected on %s"
523 " low %d < res %d < high: %d"
525 di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL ?
526 "BATCTRL" : "BATTEMP",
527 di->bm->bat_type->resis_low, res,
528 di->bm->bat_type->resis_high, i);
530 dev_warn(di->dev, "Battery identified as unknown"
531 ", resistance %d Ohm\n", res);
536 * We only have to change current source if the
537 * detected type is Type 1 (LIPO) resis_high = 53407, resis_low = 12500
538 * if someone hacks this in.
540 * FIXME: make sure this is done automatically for the batteries
543 if ((di->bm->adc_therm == AB8500_ADC_THERM_BATCTRL) &&
544 (di->bm->bi && (di->bm->bi->technology == POWER_SUPPLY_TECHNOLOGY_LIPO)) &&
545 (res <= 53407) && (res >= 12500)) {
546 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
547 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
554 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
555 * @work: pointer to the work_struct structure
557 * Work function for measuring the temperature periodically
559 static void ab8500_btemp_periodic_work(struct work_struct *work)
563 struct ab8500_btemp *di = container_of(work,
564 struct ab8500_btemp, btemp_periodic_work.work);
566 if (!di->initialized) {
567 /* Identify the battery */
568 if (ab8500_btemp_id(di) < 0)
569 dev_warn(di->dev, "failed to identify the battery\n");
572 bat_temp = ab8500_btemp_measure_temp(di);
574 * Filter battery temperature.
575 * Allow direct updates on temperature only if two samples result in
576 * same temperature. Else only allow 1 degree change from previous
577 * reported value in the direction of the new measurement.
579 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
580 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
581 di->initialized = true;
582 di->bat_temp = bat_temp;
583 power_supply_changed(di->btemp_psy);
585 } else if (bat_temp < di->prev_bat_temp) {
587 power_supply_changed(di->btemp_psy);
588 } else if (bat_temp > di->prev_bat_temp) {
590 power_supply_changed(di->btemp_psy);
592 di->prev_bat_temp = bat_temp;
594 if (di->events.ac_conn || di->events.usb_conn)
595 interval = di->bm->temp_interval_chg;
597 interval = di->bm->temp_interval_nochg;
599 /* Schedule a new measurement */
600 queue_delayed_work(di->btemp_wq,
601 &di->btemp_periodic_work,
602 round_jiffies(interval * HZ));
606 * ab8500_btemp_batctrlindb_handler() - battery removal detected
607 * @irq: interrupt number
608 * @_di: void pointer that has to address of ab8500_btemp
610 * Returns IRQ status(IRQ_HANDLED)
612 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
614 struct ab8500_btemp *di = _di;
615 dev_err(di->dev, "Battery removal detected!\n");
617 di->events.batt_rem = true;
618 power_supply_changed(di->btemp_psy);
624 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
625 * @irq: interrupt number
626 * @_di: void pointer that has to address of ab8500_btemp
628 * Returns IRQ status(IRQ_HANDLED)
630 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
632 struct ab8500_btemp *di = _di;
634 if (is_ab8500_3p3_or_earlier(di->parent)) {
635 dev_dbg(di->dev, "Ignore false btemp low irq"
636 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
638 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
640 di->events.btemp_low = true;
641 di->events.btemp_high = false;
642 di->events.btemp_medhigh = false;
643 di->events.btemp_lowmed = false;
644 power_supply_changed(di->btemp_psy);
651 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
652 * @irq: interrupt number
653 * @_di: void pointer that has to address of ab8500_btemp
655 * Returns IRQ status(IRQ_HANDLED)
657 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
659 struct ab8500_btemp *di = _di;
661 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
663 di->events.btemp_high = true;
664 di->events.btemp_medhigh = false;
665 di->events.btemp_lowmed = false;
666 di->events.btemp_low = false;
667 power_supply_changed(di->btemp_psy);
673 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
674 * @irq: interrupt number
675 * @_di: void pointer that has to address of ab8500_btemp
677 * Returns IRQ status(IRQ_HANDLED)
679 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
681 struct ab8500_btemp *di = _di;
683 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
685 di->events.btemp_lowmed = true;
686 di->events.btemp_medhigh = false;
687 di->events.btemp_high = false;
688 di->events.btemp_low = false;
689 power_supply_changed(di->btemp_psy);
695 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
696 * @irq: interrupt number
697 * @_di: void pointer that has to address of ab8500_btemp
699 * Returns IRQ status(IRQ_HANDLED)
701 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
703 struct ab8500_btemp *di = _di;
705 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
707 di->events.btemp_medhigh = true;
708 di->events.btemp_lowmed = false;
709 di->events.btemp_high = false;
710 di->events.btemp_low = false;
711 power_supply_changed(di->btemp_psy);
717 * ab8500_btemp_periodic() - Periodic temperature measurements
718 * @di: pointer to the ab8500_btemp structure
719 * @enable: enable or disable periodic temperature measurements
721 * Starts of stops periodic temperature measurements. Periodic measurements
722 * should only be done when a charger is connected.
724 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
727 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
730 * Make sure a new measurement is done directly by cancelling
733 cancel_delayed_work_sync(&di->btemp_periodic_work);
736 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
740 * ab8500_btemp_get_temp() - get battery temperature
741 * @di: pointer to the ab8500_btemp structure
743 * Returns battery temperature
745 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
750 * The BTEMP events are not reliabe on AB8500 cut3.3
753 if (is_ab8500_3p3_or_earlier(di->parent)) {
754 temp = di->bat_temp * 10;
756 if (di->events.btemp_low) {
757 if (temp > di->btemp_ranges.btemp_low_limit)
758 temp = di->btemp_ranges.btemp_low_limit * 10;
760 temp = di->bat_temp * 10;
761 } else if (di->events.btemp_high) {
762 if (temp < di->btemp_ranges.btemp_high_limit)
763 temp = di->btemp_ranges.btemp_high_limit * 10;
765 temp = di->bat_temp * 10;
766 } else if (di->events.btemp_lowmed) {
767 if (temp > di->btemp_ranges.btemp_med_limit)
768 temp = di->btemp_ranges.btemp_med_limit * 10;
770 temp = di->bat_temp * 10;
771 } else if (di->events.btemp_medhigh) {
772 if (temp < di->btemp_ranges.btemp_med_limit)
773 temp = di->btemp_ranges.btemp_med_limit * 10;
775 temp = di->bat_temp * 10;
777 temp = di->bat_temp * 10;
783 * ab8500_btemp_get_property() - get the btemp properties
784 * @psy: pointer to the power_supply structure
785 * @psp: pointer to the power_supply_property structure
786 * @val: pointer to the power_supply_propval union
788 * This function gets called when an application tries to get the btemp
789 * properties by reading the sysfs files.
790 * online: presence of the battery
791 * present: presence of the battery
792 * technology: battery technology
793 * temp: battery temperature
794 * Returns error code in case of failure else 0(on success)
796 static int ab8500_btemp_get_property(struct power_supply *psy,
797 enum power_supply_property psp,
798 union power_supply_propval *val)
800 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
803 case POWER_SUPPLY_PROP_PRESENT:
804 case POWER_SUPPLY_PROP_ONLINE:
805 if (di->events.batt_rem)
810 case POWER_SUPPLY_PROP_TECHNOLOGY:
812 val->intval = di->bm->bi->technology;
814 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
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 char *supply_interface[] = {
940 static const struct power_supply_desc ab8500_btemp_desc = {
941 .name = "ab8500_btemp",
942 .type = POWER_SUPPLY_TYPE_BATTERY,
943 .properties = ab8500_btemp_props,
944 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
945 .get_property = ab8500_btemp_get_property,
946 .external_power_changed = ab8500_btemp_external_power_changed,
949 static int ab8500_btemp_bind(struct device *dev, struct device *master,
952 struct ab8500_btemp *di = dev_get_drvdata(dev);
954 /* Create a work queue for the btemp */
956 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
957 if (di->btemp_wq == NULL) {
958 dev_err(dev, "failed to create work queue\n");
962 /* Kick off periodic temperature measurements */
963 ab8500_btemp_periodic(di, true);
968 static void ab8500_btemp_unbind(struct device *dev, struct device *master,
971 struct ab8500_btemp *di = dev_get_drvdata(dev);
973 /* Delete the work queue */
974 destroy_workqueue(di->btemp_wq);
975 flush_scheduled_work();
978 static const struct component_ops ab8500_btemp_component_ops = {
979 .bind = ab8500_btemp_bind,
980 .unbind = ab8500_btemp_unbind,
983 static int ab8500_btemp_probe(struct platform_device *pdev)
985 struct power_supply_config psy_cfg = {};
986 struct device *dev = &pdev->dev;
987 struct ab8500_btemp *di;
991 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
995 di->bm = &ab8500_bm_data;
997 /* get parent data */
999 di->parent = dev_get_drvdata(pdev->dev.parent);
1001 /* Get ADC channels */
1002 di->btemp_ball = devm_iio_channel_get(dev, "btemp_ball");
1003 if (IS_ERR(di->btemp_ball)) {
1004 ret = dev_err_probe(dev, PTR_ERR(di->btemp_ball),
1005 "failed to get BTEMP BALL ADC channel\n");
1008 di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl");
1009 if (IS_ERR(di->bat_ctrl)) {
1010 ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl),
1011 "failed to get BAT CTRL ADC channel\n");
1015 di->initialized = false;
1017 psy_cfg.supplied_to = supply_interface;
1018 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1019 psy_cfg.drv_data = di;
1021 /* Init work for measuring temperature periodically */
1022 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1023 ab8500_btemp_periodic_work);
1025 /* Set BTEMP thermal limits. Low and Med are fixed */
1026 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1027 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1029 ret = abx500_get_register_interruptible(dev, AB8500_CHARGER,
1030 AB8500_BTEMP_HIGH_TH, &val);
1032 dev_err(dev, "%s ab8500 read failed\n", __func__);
1036 case BTEMP_HIGH_TH_57_0:
1037 case BTEMP_HIGH_TH_57_1:
1038 di->btemp_ranges.btemp_high_limit =
1039 BTEMP_THERMAL_HIGH_LIMIT_57;
1041 case BTEMP_HIGH_TH_52:
1042 di->btemp_ranges.btemp_high_limit =
1043 BTEMP_THERMAL_HIGH_LIMIT_52;
1045 case BTEMP_HIGH_TH_62:
1046 di->btemp_ranges.btemp_high_limit =
1047 BTEMP_THERMAL_HIGH_LIMIT_62;
1051 /* Register BTEMP power supply class */
1052 di->btemp_psy = devm_power_supply_register(dev, &ab8500_btemp_desc,
1054 if (IS_ERR(di->btemp_psy)) {
1055 dev_err(dev, "failed to register BTEMP psy\n");
1056 return PTR_ERR(di->btemp_psy);
1059 /* Register interrupts */
1060 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1061 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1065 ret = devm_request_threaded_irq(dev, irq, NULL,
1066 ab8500_btemp_irq[i].isr,
1067 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
1068 ab8500_btemp_irq[i].name, di);
1071 dev_err(dev, "failed to request %s IRQ %d: %d\n"
1072 , ab8500_btemp_irq[i].name, irq, ret);
1075 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
1076 ab8500_btemp_irq[i].name, irq, ret);
1079 platform_set_drvdata(pdev, di);
1081 list_add_tail(&di->node, &ab8500_btemp_list);
1083 return component_add(dev, &ab8500_btemp_component_ops);
1086 static int ab8500_btemp_remove(struct platform_device *pdev)
1088 component_del(&pdev->dev, &ab8500_btemp_component_ops);
1093 static SIMPLE_DEV_PM_OPS(ab8500_btemp_pm_ops, ab8500_btemp_suspend, ab8500_btemp_resume);
1095 static const struct of_device_id ab8500_btemp_match[] = {
1096 { .compatible = "stericsson,ab8500-btemp", },
1099 MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
1101 struct platform_driver ab8500_btemp_driver = {
1102 .probe = ab8500_btemp_probe,
1103 .remove = ab8500_btemp_remove,
1105 .name = "ab8500-btemp",
1106 .of_match_table = ab8500_btemp_match,
1107 .pm = &ab8500_btemp_pm_ops,
1110 MODULE_LICENSE("GPL v2");
1111 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1112 MODULE_ALIAS("platform:ab8500-btemp");
1113 MODULE_DESCRIPTION("AB8500 battery temperature driver");