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 BTEMP_BATCTRL_CURR_SRC_16UA 16
43 #define BTEMP_BATCTRL_CURR_SRC_18UA 18
45 #define BTEMP_BATCTRL_CURR_SRC_60UA 60
46 #define BTEMP_BATCTRL_CURR_SRC_120UA 120
49 * struct ab8500_btemp_interrupts - ab8500 interrupts
50 * @name: name of the interrupt
51 * @isr function pointer to the isr
53 struct ab8500_btemp_interrupts {
55 irqreturn_t (*isr)(int irq, void *data);
58 struct ab8500_btemp_events {
68 struct ab8500_btemp_ranges {
75 * struct ab8500_btemp - ab8500 BTEMP device information
76 * @dev: Pointer to the structure device
77 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
78 * @curr_source: What current source we use, in uA
79 * @bat_temp: Dispatched battery temperature in degree Celsius
80 * @prev_bat_temp Last measured battery temperature in degree Celsius
81 * @parent: Pointer to the struct ab8500
82 * @gpadc: Pointer to the struct gpadc
83 * @fg: Pointer to the struct fg
84 * @bm: Platform specific battery management information
85 * @btemp_psy: Structure for BTEMP specific battery properties
86 * @events: Structure for information about events triggered
87 * @btemp_ranges: Battery temperature range structure
88 * @btemp_wq: Work queue for measuring the temperature periodically
89 * @btemp_periodic_work: Work for measuring the temperature periodically
90 * @initialized: True if battery id read.
94 struct list_head node;
98 struct ab8500 *parent;
99 struct ab8500_gpadc *gpadc;
100 struct ab8500_fg *fg;
101 struct abx500_bm_data *bm;
102 struct power_supply *btemp_psy;
103 struct ab8500_btemp_events events;
104 struct ab8500_btemp_ranges btemp_ranges;
105 struct workqueue_struct *btemp_wq;
106 struct delayed_work btemp_periodic_work;
110 /* BTEMP power supply properties */
111 static enum power_supply_property ab8500_btemp_props[] = {
112 POWER_SUPPLY_PROP_PRESENT,
113 POWER_SUPPLY_PROP_ONLINE,
114 POWER_SUPPLY_PROP_TECHNOLOGY,
115 POWER_SUPPLY_PROP_TEMP,
118 static LIST_HEAD(ab8500_btemp_list);
121 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
122 * (i.e. the first BTEMP in the instance list)
124 struct ab8500_btemp *ab8500_btemp_get(void)
126 return list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
128 EXPORT_SYMBOL(ab8500_btemp_get);
131 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
132 * @di: pointer to the ab8500_btemp structure
133 * @v_batctrl: measured batctrl voltage
134 * @inst_curr: measured instant current
136 * This function returns the battery resistance that is
137 * derived from the BATCTRL voltage.
138 * Returns value in Ohms.
140 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
141 int v_batctrl, int inst_curr)
145 if (is_ab8500_1p1_or_earlier(di->parent)) {
147 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
148 * connected to 1.8V through a 450k resistor
150 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
153 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
155 * If the battery has internal NTC, we use the current
156 * source to calculate the resistance.
158 rbs = (v_batctrl * 1000
159 - di->bm->gnd_lift_resistance * inst_curr)
163 * BAT_CTRL is internally
164 * connected to 1.8V through a 80k resistor
166 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
173 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
174 * @di: pointer to the ab8500_btemp structure
176 * This function returns the voltage on BATCTRL. Returns value in mV.
178 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
183 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
186 "%s gpadc conversion failed, using previous value",
195 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
196 * @di: pointer to the ab8500_btemp structure
197 * @enable: enable or disable the current source
199 * Enable or disable the current sources for the BatCtrl AD channel
201 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
208 * BATCTRL current sources are included on AB8500 cut2.0
209 * and future versions
211 if (is_ab8500_1p1_or_earlier(di->parent))
214 /* Only do this for batteries with internal NTC */
215 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
217 if (is_ab8540(di->parent)) {
218 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_60UA)
219 curr = BAT_CTRL_60U_ENA;
221 curr = BAT_CTRL_120U_ENA;
222 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
223 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_16UA)
224 curr = BAT_CTRL_16U_ENA;
226 curr = BAT_CTRL_18U_ENA;
228 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
229 curr = BAT_CTRL_7U_ENA;
231 curr = BAT_CTRL_20U_ENA;
234 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
236 ret = abx500_mask_and_set_register_interruptible(di->dev,
237 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
238 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
240 dev_err(di->dev, "%s failed setting cmp_force\n",
246 * We have to wait one 32kHz cycle before enabling
247 * the current source, since ForceBatCtrlCmpHigh needs
248 * to be written in a separate cycle
252 ret = abx500_set_register_interruptible(di->dev,
253 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
254 FORCE_BAT_CTRL_CMP_HIGH | curr);
256 dev_err(di->dev, "%s failed enabling current source\n",
258 goto disable_curr_source;
260 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
261 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
263 if (is_ab8540(di->parent)) {
264 /* Write 0 to the curr bits */
265 ret = abx500_mask_and_set_register_interruptible(
267 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
268 BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA,
269 ~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
270 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
271 /* Write 0 to the curr bits */
272 ret = abx500_mask_and_set_register_interruptible(
274 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
275 BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
276 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
278 /* Write 0 to the curr bits */
279 ret = abx500_mask_and_set_register_interruptible(
281 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
282 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
283 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
287 dev_err(di->dev, "%s failed disabling current source\n",
289 goto disable_curr_source;
292 /* Enable Pull-Up and comparator */
293 ret = abx500_mask_and_set_register_interruptible(di->dev,
294 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
295 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
296 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
298 dev_err(di->dev, "%s failed enabling PU and comp\n",
304 * We have to wait one 32kHz cycle before disabling
305 * ForceBatCtrlCmpHigh since this needs to be written
306 * in a separate cycle
310 /* Disable 'force comparator' */
311 ret = abx500_mask_and_set_register_interruptible(di->dev,
312 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
313 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
315 dev_err(di->dev, "%s failed disabling force comp\n",
317 goto disable_force_comp;
323 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
324 * if we got an error above
327 if (is_ab8540(di->parent)) {
328 /* Write 0 to the curr bits */
329 ret = abx500_mask_and_set_register_interruptible(di->dev,
330 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
331 BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA,
332 ~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
333 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
334 /* Write 0 to the curr bits */
335 ret = abx500_mask_and_set_register_interruptible(di->dev,
336 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
337 BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
338 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
340 /* Write 0 to the curr bits */
341 ret = abx500_mask_and_set_register_interruptible(di->dev,
342 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
343 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
344 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
348 dev_err(di->dev, "%s failed disabling current source\n",
353 /* Enable Pull-Up and comparator */
354 ret = abx500_mask_and_set_register_interruptible(di->dev,
355 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
356 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
357 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
359 dev_err(di->dev, "%s failed enabling PU and comp\n",
366 * We have to wait one 32kHz cycle before disabling
367 * ForceBatCtrlCmpHigh since this needs to be written
368 * in a separate cycle
372 /* Disable 'force comparator' */
373 ret = abx500_mask_and_set_register_interruptible(di->dev,
374 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
375 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
377 dev_err(di->dev, "%s failed disabling force comp\n",
386 * ab8500_btemp_get_batctrl_res() - get battery resistance
387 * @di: pointer to the ab8500_btemp structure
389 * This function returns the battery pack identification resistance.
390 * Returns value in Ohms.
392 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
401 * BATCTRL current sources are included on AB8500 cut2.0
402 * and future versions
404 ret = ab8500_btemp_curr_source_enable(di, true);
406 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
411 di->fg = ab8500_fg_get();
413 dev_err(di->dev, "No fg found\n");
417 ret = ab8500_fg_inst_curr_start(di->fg);
420 dev_err(di->dev, "Failed to start current measurement\n");
426 } while (!ab8500_fg_inst_curr_started(di->fg));
431 batctrl += ab8500_btemp_read_batctrl_voltage(di);
434 } while (!ab8500_fg_inst_curr_done(di->fg));
437 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
439 dev_err(di->dev, "Failed to finalize current measurement\n");
443 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
445 ret = ab8500_btemp_curr_source_enable(di, false);
447 dev_err(di->dev, "%s curr source disable failed\n", __func__);
451 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
452 __func__, batctrl, res, inst_curr, i);
458 * ab8500_btemp_res_to_temp() - resistance to temperature
459 * @di: pointer to the ab8500_btemp structure
460 * @tbl: pointer to the resiatance to temperature table
461 * @tbl_size: size of the resistance to temperature table
462 * @res: resistance to calculate the temperature from
464 * This function returns the battery temperature in degrees Celsius
465 * based on the NTC resistance.
467 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
468 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
472 * Calculate the formula for the straight line
473 * Simple interpolation if we are within
474 * the resistance table limits, extrapolate
475 * if resistance is outside the limits.
477 if (res > tbl[0].resist)
479 else if (res <= tbl[tbl_size - 1].resist)
483 while (!(res <= tbl[i].resist &&
484 res > tbl[i + 1].resist))
488 return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
489 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
493 * ab8500_btemp_measure_temp() - measure battery temperature
494 * @di: pointer to the ab8500_btemp structure
496 * Returns battery temperature (on success) else the previous temperature
498 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
502 int rbat, rntc, vntc;
505 id = di->bm->batt_id;
507 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
508 id != BATTERY_UNKNOWN) {
510 rbat = ab8500_btemp_get_batctrl_res(di);
512 dev_err(di->dev, "%s get batctrl res failed\n",
515 * Return out-of-range temperature so that
516 * charging is stopped
518 return BTEMP_THERMAL_LOW_LIMIT;
521 temp = ab8500_btemp_res_to_temp(di,
522 di->bm->bat_type[id].r_to_t_tbl,
523 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
525 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
528 "%s gpadc conversion failed,"
529 " using previous value\n", __func__);
533 * The PCB NTC is sourced from VTVOUT via a 230kOhm
536 rntc = 230000 * vntc / (VTVOUT_V - vntc);
538 temp = ab8500_btemp_res_to_temp(di,
539 di->bm->bat_type[id].r_to_t_tbl,
540 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
543 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
548 * ab8500_btemp_id() - Identify the connected battery
549 * @di: pointer to the ab8500_btemp structure
551 * This function will try to identify the battery by reading the ID
552 * resistor. Some brands use a combined ID resistor with a NTC resistor to
553 * both be able to identify and to read the temperature of it.
555 static int ab8500_btemp_id(struct ab8500_btemp *di)
559 if (is_ab8540(di->parent))
560 di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
561 else if (is_ab9540(di->parent) || is_ab8505(di->parent))
562 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
564 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
566 di->bm->batt_id = BATTERY_UNKNOWN;
568 res = ab8500_btemp_get_batctrl_res(di);
570 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
574 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
575 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
576 if ((res <= di->bm->bat_type[i].resis_high) &&
577 (res >= di->bm->bat_type[i].resis_low)) {
578 dev_dbg(di->dev, "Battery detected on %s"
579 " low %d < res %d < high: %d"
581 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
582 "BATCTRL" : "BATTEMP",
583 di->bm->bat_type[i].resis_low, res,
584 di->bm->bat_type[i].resis_high, i);
591 if (di->bm->batt_id == BATTERY_UNKNOWN) {
592 dev_warn(di->dev, "Battery identified as unknown"
593 ", resistance %d Ohm\n", res);
598 * We only have to change current source if the
599 * detected type is Type 1.
601 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
602 di->bm->batt_id == 1) {
603 if (is_ab8540(di->parent)) {
605 "Set BATCTRL current source to 60uA\n");
606 di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
607 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
609 "Set BATCTRL current source to 16uA\n");
610 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
612 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
613 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
617 return di->bm->batt_id;
621 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
622 * @work: pointer to the work_struct structure
624 * Work function for measuring the temperature periodically
626 static void ab8500_btemp_periodic_work(struct work_struct *work)
630 struct ab8500_btemp *di = container_of(work,
631 struct ab8500_btemp, btemp_periodic_work.work);
633 if (!di->initialized) {
634 /* Identify the battery */
635 if (ab8500_btemp_id(di) < 0)
636 dev_warn(di->dev, "failed to identify the battery\n");
639 bat_temp = ab8500_btemp_measure_temp(di);
641 * Filter battery temperature.
642 * Allow direct updates on temperature only if two samples result in
643 * same temperature. Else only allow 1 degree change from previous
644 * reported value in the direction of the new measurement.
646 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
647 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
648 di->initialized = true;
649 di->bat_temp = bat_temp;
650 power_supply_changed(di->btemp_psy);
652 } else if (bat_temp < di->prev_bat_temp) {
654 power_supply_changed(di->btemp_psy);
655 } else if (bat_temp > di->prev_bat_temp) {
657 power_supply_changed(di->btemp_psy);
659 di->prev_bat_temp = bat_temp;
661 if (di->events.ac_conn || di->events.usb_conn)
662 interval = di->bm->temp_interval_chg;
664 interval = di->bm->temp_interval_nochg;
666 /* Schedule a new measurement */
667 queue_delayed_work(di->btemp_wq,
668 &di->btemp_periodic_work,
669 round_jiffies(interval * HZ));
673 * ab8500_btemp_batctrlindb_handler() - battery removal detected
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_batctrlindb_handler(int irq, void *_di)
681 struct ab8500_btemp *di = _di;
682 dev_err(di->dev, "Battery removal detected!\n");
684 di->events.batt_rem = true;
685 power_supply_changed(di->btemp_psy);
691 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
692 * @irq: interrupt number
693 * @_di: void pointer that has to address of ab8500_btemp
695 * Returns IRQ status(IRQ_HANDLED)
697 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
699 struct ab8500_btemp *di = _di;
701 if (is_ab8500_3p3_or_earlier(di->parent)) {
702 dev_dbg(di->dev, "Ignore false btemp low irq"
703 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
705 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
707 di->events.btemp_low = true;
708 di->events.btemp_high = false;
709 di->events.btemp_medhigh = false;
710 di->events.btemp_lowmed = false;
711 power_supply_changed(di->btemp_psy);
718 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
719 * @irq: interrupt number
720 * @_di: void pointer that has to address of ab8500_btemp
722 * Returns IRQ status(IRQ_HANDLED)
724 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
726 struct ab8500_btemp *di = _di;
728 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
730 di->events.btemp_high = true;
731 di->events.btemp_medhigh = false;
732 di->events.btemp_lowmed = false;
733 di->events.btemp_low = false;
734 power_supply_changed(di->btemp_psy);
740 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
741 * @irq: interrupt number
742 * @_di: void pointer that has to address of ab8500_btemp
744 * Returns IRQ status(IRQ_HANDLED)
746 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
748 struct ab8500_btemp *di = _di;
750 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
752 di->events.btemp_lowmed = true;
753 di->events.btemp_medhigh = false;
754 di->events.btemp_high = false;
755 di->events.btemp_low = false;
756 power_supply_changed(di->btemp_psy);
762 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
763 * @irq: interrupt number
764 * @_di: void pointer that has to address of ab8500_btemp
766 * Returns IRQ status(IRQ_HANDLED)
768 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
770 struct ab8500_btemp *di = _di;
772 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
774 di->events.btemp_medhigh = true;
775 di->events.btemp_lowmed = false;
776 di->events.btemp_high = false;
777 di->events.btemp_low = false;
778 power_supply_changed(di->btemp_psy);
784 * ab8500_btemp_periodic() - Periodic temperature measurements
785 * @di: pointer to the ab8500_btemp structure
786 * @enable: enable or disable periodic temperature measurements
788 * Starts of stops periodic temperature measurements. Periodic measurements
789 * should only be done when a charger is connected.
791 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
794 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
797 * Make sure a new measurement is done directly by cancelling
800 cancel_delayed_work_sync(&di->btemp_periodic_work);
803 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
807 * ab8500_btemp_get_temp() - get battery temperature
808 * @di: pointer to the ab8500_btemp structure
810 * Returns battery temperature
812 int ab8500_btemp_get_temp(struct ab8500_btemp *di)
817 * The BTEMP events are not reliabe on AB8500 cut3.3
820 if (is_ab8500_3p3_or_earlier(di->parent)) {
821 temp = di->bat_temp * 10;
823 if (di->events.btemp_low) {
824 if (temp > di->btemp_ranges.btemp_low_limit)
825 temp = di->btemp_ranges.btemp_low_limit * 10;
827 temp = di->bat_temp * 10;
828 } else if (di->events.btemp_high) {
829 if (temp < di->btemp_ranges.btemp_high_limit)
830 temp = di->btemp_ranges.btemp_high_limit * 10;
832 temp = di->bat_temp * 10;
833 } else if (di->events.btemp_lowmed) {
834 if (temp > di->btemp_ranges.btemp_med_limit)
835 temp = di->btemp_ranges.btemp_med_limit * 10;
837 temp = di->bat_temp * 10;
838 } else if (di->events.btemp_medhigh) {
839 if (temp < di->btemp_ranges.btemp_med_limit)
840 temp = di->btemp_ranges.btemp_med_limit * 10;
842 temp = di->bat_temp * 10;
844 temp = di->bat_temp * 10;
848 EXPORT_SYMBOL(ab8500_btemp_get_temp);
851 * ab8500_btemp_get_batctrl_temp() - get the temperature
852 * @btemp: pointer to the btemp structure
854 * Returns the batctrl temperature in millidegrees
856 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
858 return btemp->bat_temp * 1000;
860 EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
863 * ab8500_btemp_get_property() - get the btemp properties
864 * @psy: pointer to the power_supply structure
865 * @psp: pointer to the power_supply_property structure
866 * @val: pointer to the power_supply_propval union
868 * This function gets called when an application tries to get the btemp
869 * properties by reading the sysfs files.
870 * online: presence of the battery
871 * present: presence of the battery
872 * technology: battery technology
873 * temp: battery temperature
874 * Returns error code in case of failure else 0(on success)
876 static int ab8500_btemp_get_property(struct power_supply *psy,
877 enum power_supply_property psp,
878 union power_supply_propval *val)
880 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
883 case POWER_SUPPLY_PROP_PRESENT:
884 case POWER_SUPPLY_PROP_ONLINE:
885 if (di->events.batt_rem)
890 case POWER_SUPPLY_PROP_TECHNOLOGY:
891 val->intval = di->bm->bat_type[di->bm->batt_id].name;
893 case POWER_SUPPLY_PROP_TEMP:
894 val->intval = ab8500_btemp_get_temp(di);
902 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
904 struct power_supply *psy;
905 struct power_supply *ext = dev_get_drvdata(dev);
906 const char **supplicants = (const char **)ext->supplied_to;
907 struct ab8500_btemp *di;
908 union power_supply_propval ret;
911 psy = (struct power_supply *)data;
912 di = power_supply_get_drvdata(psy);
915 * For all psy where the name of your driver
916 * appears in any supplied_to
918 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
922 /* Go through all properties for the psy */
923 for (j = 0; j < ext->desc->num_properties; j++) {
924 enum power_supply_property prop;
925 prop = ext->desc->properties[j];
927 if (power_supply_get_property(ext, prop, &ret))
931 case POWER_SUPPLY_PROP_PRESENT:
932 switch (ext->desc->type) {
933 case POWER_SUPPLY_TYPE_MAINS:
934 /* AC disconnected */
935 if (!ret.intval && di->events.ac_conn) {
936 di->events.ac_conn = false;
939 else if (ret.intval && !di->events.ac_conn) {
940 di->events.ac_conn = true;
941 if (!di->events.usb_conn)
942 ab8500_btemp_periodic(di, true);
945 case POWER_SUPPLY_TYPE_USB:
946 /* USB disconnected */
947 if (!ret.intval && di->events.usb_conn) {
948 di->events.usb_conn = false;
951 else if (ret.intval && !di->events.usb_conn) {
952 di->events.usb_conn = true;
953 if (!di->events.ac_conn)
954 ab8500_btemp_periodic(di, true);
969 * ab8500_btemp_external_power_changed() - callback for power supply changes
970 * @psy: pointer to the structure power_supply
972 * This function is pointing to the function pointer external_power_changed
973 * of the structure power_supply.
974 * This function gets executed when there is a change in the external power
975 * supply to the btemp.
977 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
979 class_for_each_device(power_supply_class, NULL, psy,
980 ab8500_btemp_get_ext_psy_data);
983 /* ab8500 btemp driver interrupts and their respective isr */
984 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
985 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
986 {"BTEMP_LOW", ab8500_btemp_templow_handler},
987 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
988 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
989 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
992 #if defined(CONFIG_PM)
993 static int ab8500_btemp_resume(struct platform_device *pdev)
995 struct ab8500_btemp *di = platform_get_drvdata(pdev);
997 ab8500_btemp_periodic(di, true);
1002 static int ab8500_btemp_suspend(struct platform_device *pdev,
1005 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1007 ab8500_btemp_periodic(di, false);
1012 #define ab8500_btemp_suspend NULL
1013 #define ab8500_btemp_resume NULL
1016 static int ab8500_btemp_remove(struct platform_device *pdev)
1018 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1021 /* Disable interrupts */
1022 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1023 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1027 /* Delete the work queue */
1028 destroy_workqueue(di->btemp_wq);
1030 flush_scheduled_work();
1031 power_supply_unregister(di->btemp_psy);
1036 static char *supply_interface[] = {
1041 static const struct power_supply_desc ab8500_btemp_desc = {
1042 .name = "ab8500_btemp",
1043 .type = POWER_SUPPLY_TYPE_BATTERY,
1044 .properties = ab8500_btemp_props,
1045 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
1046 .get_property = ab8500_btemp_get_property,
1047 .external_power_changed = ab8500_btemp_external_power_changed,
1050 static int ab8500_btemp_probe(struct platform_device *pdev)
1052 struct device_node *np = pdev->dev.of_node;
1053 struct abx500_bm_data *plat = pdev->dev.platform_data;
1054 struct power_supply_config psy_cfg = {};
1055 struct ab8500_btemp *di;
1056 int irq, i, ret = 0;
1059 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1061 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
1066 dev_err(&pdev->dev, "no battery management data supplied\n");
1072 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1074 dev_err(&pdev->dev, "failed to get battery information\n");
1079 /* get parent data */
1080 di->dev = &pdev->dev;
1081 di->parent = dev_get_drvdata(pdev->dev.parent);
1082 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1084 di->initialized = false;
1086 psy_cfg.supplied_to = supply_interface;
1087 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1088 psy_cfg.drv_data = di;
1090 /* Create a work queue for the btemp */
1092 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
1093 if (di->btemp_wq == NULL) {
1094 dev_err(di->dev, "failed to create work queue\n");
1098 /* Init work for measuring temperature periodically */
1099 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1100 ab8500_btemp_periodic_work);
1102 /* Set BTEMP thermal limits. Low and Med are fixed */
1103 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1104 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1106 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1107 AB8500_BTEMP_HIGH_TH, &val);
1109 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1113 case BTEMP_HIGH_TH_57_0:
1114 case BTEMP_HIGH_TH_57_1:
1115 di->btemp_ranges.btemp_high_limit =
1116 BTEMP_THERMAL_HIGH_LIMIT_57;
1118 case BTEMP_HIGH_TH_52:
1119 di->btemp_ranges.btemp_high_limit =
1120 BTEMP_THERMAL_HIGH_LIMIT_52;
1122 case BTEMP_HIGH_TH_62:
1123 di->btemp_ranges.btemp_high_limit =
1124 BTEMP_THERMAL_HIGH_LIMIT_62;
1128 /* Register BTEMP power supply class */
1129 di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
1131 if (IS_ERR(di->btemp_psy)) {
1132 dev_err(di->dev, "failed to register BTEMP psy\n");
1133 ret = PTR_ERR(di->btemp_psy);
1137 /* Register interrupts */
1138 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1139 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1140 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1141 IRQF_SHARED | IRQF_NO_SUSPEND,
1142 ab8500_btemp_irq[i].name, di);
1145 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1146 , ab8500_btemp_irq[i].name, irq, ret);
1149 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1150 ab8500_btemp_irq[i].name, irq, ret);
1153 platform_set_drvdata(pdev, di);
1155 /* Kick off periodic temperature measurements */
1156 ab8500_btemp_periodic(di, true);
1157 list_add_tail(&di->node, &ab8500_btemp_list);
1162 power_supply_unregister(di->btemp_psy);
1164 /* We also have to free all successfully registered irqs */
1165 for (i = i - 1; i >= 0; i--) {
1166 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1170 destroy_workqueue(di->btemp_wq);
1174 static const struct of_device_id ab8500_btemp_match[] = {
1175 { .compatible = "stericsson,ab8500-btemp", },
1178 MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
1180 static struct platform_driver ab8500_btemp_driver = {
1181 .probe = ab8500_btemp_probe,
1182 .remove = ab8500_btemp_remove,
1183 .suspend = ab8500_btemp_suspend,
1184 .resume = ab8500_btemp_resume,
1186 .name = "ab8500-btemp",
1187 .of_match_table = ab8500_btemp_match,
1191 static int __init ab8500_btemp_init(void)
1193 return platform_driver_register(&ab8500_btemp_driver);
1196 static void __exit ab8500_btemp_exit(void)
1198 platform_driver_unregister(&ab8500_btemp_driver);
1201 device_initcall(ab8500_btemp_init);
1202 module_exit(ab8500_btemp_exit);
1204 MODULE_LICENSE("GPL v2");
1205 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1206 MODULE_ALIAS("platform:ab8500-btemp");
1207 MODULE_DESCRIPTION("AB8500 battery temperature driver");