PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / power / ab8500_btemp.c
blob7f9a4547dccd9bf757fa1197e5a26ae0a319de1f
1 /*
2 * Copyright (C) ST-Ericsson SA 2012
4 * Battery temperature driver for AB8500
6 * License Terms: GNU General Public License v2
7 * Author:
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>
24 #include <linux/of.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>
31 #define VTVOUT_V 1800
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
48 #define to_ab8500_btemp_device_info(x) container_of((x), \
49 struct ab8500_btemp, btemp_psy);
51 /**
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 {
57 char *name;
58 irqreturn_t (*isr)(int irq, void *data);
61 struct ab8500_btemp_events {
62 bool batt_rem;
63 bool btemp_high;
64 bool btemp_medhigh;
65 bool btemp_lowmed;
66 bool btemp_low;
67 bool ac_conn;
68 bool usb_conn;
71 struct ab8500_btemp_ranges {
72 int btemp_high_limit;
73 int btemp_med_limit;
74 int btemp_low_limit;
77 /**
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 Celcius
83 * @prev_bat_temp Last measured battery temperature in degree Celcius
84 * @parent: Pointer to the struct ab8500
85 * @gpadc: Pointer to the struct gpadc
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.
95 struct ab8500_btemp {
96 struct device *dev;
97 struct list_head node;
98 int curr_source;
99 int bat_temp;
100 int prev_bat_temp;
101 struct ab8500 *parent;
102 struct ab8500_gpadc *gpadc;
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;
110 bool initialized;
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_get() - returns a reference to the primary AB8500 BTEMP
125 * (i.e. the first BTEMP in the instance list)
127 struct ab8500_btemp *ab8500_btemp_get(void)
129 struct ab8500_btemp *btemp;
130 btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
132 return btemp;
134 EXPORT_SYMBOL(ab8500_btemp_get);
137 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
138 * @di: pointer to the ab8500_btemp structure
139 * @v_batctrl: measured batctrl voltage
140 * @inst_curr: measured instant current
142 * This function returns the battery resistance that is
143 * derived from the BATCTRL voltage.
144 * Returns value in Ohms.
146 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
147 int v_batctrl, int inst_curr)
149 int rbs;
151 if (is_ab8500_1p1_or_earlier(di->parent)) {
153 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
154 * connected to 1.8V through a 450k resistor
156 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
159 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
161 * If the battery has internal NTC, we use the current
162 * source to calculate the resistance.
164 rbs = (v_batctrl * 1000
165 - di->bm->gnd_lift_resistance * inst_curr)
166 / di->curr_source;
167 } else {
169 * BAT_CTRL is internally
170 * connected to 1.8V through a 80k resistor
172 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
175 return rbs;
179 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
180 * @di: pointer to the ab8500_btemp structure
182 * This function returns the voltage on BATCTRL. Returns value in mV.
184 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
186 int vbtemp;
187 static int prev;
189 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
190 if (vbtemp < 0) {
191 dev_err(di->dev,
192 "%s gpadc conversion failed, using previous value",
193 __func__);
194 return prev;
196 prev = vbtemp;
197 return vbtemp;
201 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
202 * @di: pointer to the ab8500_btemp structure
203 * @enable: enable or disable the current source
205 * Enable or disable the current sources for the BatCtrl AD channel
207 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
208 bool enable)
210 int curr;
211 int ret = 0;
214 * BATCTRL current sources are included on AB8500 cut2.0
215 * and future versions
217 if (is_ab8500_1p1_or_earlier(di->parent))
218 return 0;
220 /* Only do this for batteries with internal NTC */
221 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
223 if (is_ab8540(di->parent)) {
224 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_60UA)
225 curr = BAT_CTRL_60U_ENA;
226 else
227 curr = BAT_CTRL_120U_ENA;
228 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
229 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_16UA)
230 curr = BAT_CTRL_16U_ENA;
231 else
232 curr = BAT_CTRL_18U_ENA;
233 } else {
234 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
235 curr = BAT_CTRL_7U_ENA;
236 else
237 curr = BAT_CTRL_20U_ENA;
240 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
242 ret = abx500_mask_and_set_register_interruptible(di->dev,
243 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
244 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
245 if (ret) {
246 dev_err(di->dev, "%s failed setting cmp_force\n",
247 __func__);
248 return ret;
252 * We have to wait one 32kHz cycle before enabling
253 * the current source, since ForceBatCtrlCmpHigh needs
254 * to be written in a separate cycle
256 udelay(32);
258 ret = abx500_set_register_interruptible(di->dev,
259 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
260 FORCE_BAT_CTRL_CMP_HIGH | curr);
261 if (ret) {
262 dev_err(di->dev, "%s failed enabling current source\n",
263 __func__);
264 goto disable_curr_source;
266 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
267 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
269 if (is_ab8540(di->parent)) {
270 /* Write 0 to the curr bits */
271 ret = abx500_mask_and_set_register_interruptible(
272 di->dev,
273 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
274 BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA,
275 ~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
276 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
277 /* Write 0 to the curr bits */
278 ret = abx500_mask_and_set_register_interruptible(
279 di->dev,
280 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
281 BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
282 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
283 } else {
284 /* Write 0 to the curr bits */
285 ret = abx500_mask_and_set_register_interruptible(
286 di->dev,
287 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
288 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
289 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
292 if (ret) {
293 dev_err(di->dev, "%s failed disabling current source\n",
294 __func__);
295 goto disable_curr_source;
298 /* Enable Pull-Up and comparator */
299 ret = abx500_mask_and_set_register_interruptible(di->dev,
300 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
301 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
302 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
303 if (ret) {
304 dev_err(di->dev, "%s failed enabling PU and comp\n",
305 __func__);
306 goto enable_pu_comp;
310 * We have to wait one 32kHz cycle before disabling
311 * ForceBatCtrlCmpHigh since this needs to be written
312 * in a separate cycle
314 udelay(32);
316 /* Disable 'force comparator' */
317 ret = abx500_mask_and_set_register_interruptible(di->dev,
318 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
319 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
320 if (ret) {
321 dev_err(di->dev, "%s failed disabling force comp\n",
322 __func__);
323 goto disable_force_comp;
326 return ret;
329 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
330 * if we got an error above
332 disable_curr_source:
333 if (is_ab8540(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_60U_ENA | BAT_CTRL_120U_ENA,
338 ~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
339 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
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_16U_ENA | BAT_CTRL_18U_ENA,
344 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
345 } else {
346 /* Write 0 to the curr bits */
347 ret = abx500_mask_and_set_register_interruptible(di->dev,
348 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
349 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
350 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
353 if (ret) {
354 dev_err(di->dev, "%s failed disabling current source\n",
355 __func__);
356 return ret;
358 enable_pu_comp:
359 /* Enable Pull-Up and comparator */
360 ret = abx500_mask_and_set_register_interruptible(di->dev,
361 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
362 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
363 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
364 if (ret) {
365 dev_err(di->dev, "%s failed enabling PU and comp\n",
366 __func__);
367 return ret;
370 disable_force_comp:
372 * We have to wait one 32kHz cycle before disabling
373 * ForceBatCtrlCmpHigh since this needs to be written
374 * in a separate cycle
376 udelay(32);
378 /* Disable 'force comparator' */
379 ret = abx500_mask_and_set_register_interruptible(di->dev,
380 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
381 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
382 if (ret) {
383 dev_err(di->dev, "%s failed disabling force comp\n",
384 __func__);
385 return ret;
388 return ret;
392 * ab8500_btemp_get_batctrl_res() - get battery resistance
393 * @di: pointer to the ab8500_btemp structure
395 * This function returns the battery pack identification resistance.
396 * Returns value in Ohms.
398 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
400 int ret;
401 int batctrl = 0;
402 int res;
403 int inst_curr;
404 int i;
407 * BATCTRL current sources are included on AB8500 cut2.0
408 * and future versions
410 ret = ab8500_btemp_curr_source_enable(di, true);
411 if (ret) {
412 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
413 return ret;
416 if (!di->fg)
417 di->fg = ab8500_fg_get();
418 if (!di->fg) {
419 dev_err(di->dev, "No fg found\n");
420 return -EINVAL;
423 ret = ab8500_fg_inst_curr_start(di->fg);
425 if (ret) {
426 dev_err(di->dev, "Failed to start current measurement\n");
427 return ret;
430 do {
431 msleep(20);
432 } while (!ab8500_fg_inst_curr_started(di->fg));
434 i = 0;
436 do {
437 batctrl += ab8500_btemp_read_batctrl_voltage(di);
438 i++;
439 msleep(20);
440 } while (!ab8500_fg_inst_curr_done(di->fg));
441 batctrl /= i;
443 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
444 if (ret) {
445 dev_err(di->dev, "Failed to finalize current measurement\n");
446 return ret;
449 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
451 ret = ab8500_btemp_curr_source_enable(di, false);
452 if (ret) {
453 dev_err(di->dev, "%s curr source disable failed\n", __func__);
454 return ret;
457 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
458 __func__, batctrl, res, inst_curr, i);
460 return res;
464 * ab8500_btemp_res_to_temp() - resistance to temperature
465 * @di: pointer to the ab8500_btemp structure
466 * @tbl: pointer to the resiatance to temperature table
467 * @tbl_size: size of the resistance to temperature table
468 * @res: resistance to calculate the temperature from
470 * This function returns the battery temperature in degrees Celcius
471 * based on the NTC resistance.
473 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
474 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
476 int i, temp;
478 * Calculate the formula for the straight line
479 * Simple interpolation if we are within
480 * the resistance table limits, extrapolate
481 * if resistance is outside the limits.
483 if (res > tbl[0].resist)
484 i = 0;
485 else if (res <= tbl[tbl_size - 1].resist)
486 i = tbl_size - 2;
487 else {
488 i = 0;
489 while (!(res <= tbl[i].resist &&
490 res > tbl[i + 1].resist))
491 i++;
494 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
495 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
496 return temp;
500 * ab8500_btemp_measure_temp() - measure battery temperature
501 * @di: pointer to the ab8500_btemp structure
503 * Returns battery temperature (on success) else the previous temperature
505 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
507 int temp;
508 static int prev;
509 int rbat, rntc, vntc;
510 u8 id;
512 id = di->bm->batt_id;
514 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
515 id != BATTERY_UNKNOWN) {
517 rbat = ab8500_btemp_get_batctrl_res(di);
518 if (rbat < 0) {
519 dev_err(di->dev, "%s get batctrl res failed\n",
520 __func__);
522 * Return out-of-range temperature so that
523 * charging is stopped
525 return BTEMP_THERMAL_LOW_LIMIT;
528 temp = ab8500_btemp_res_to_temp(di,
529 di->bm->bat_type[id].r_to_t_tbl,
530 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
531 } else {
532 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
533 if (vntc < 0) {
534 dev_err(di->dev,
535 "%s gpadc conversion failed,"
536 " using previous value\n", __func__);
537 return prev;
540 * The PCB NTC is sourced from VTVOUT via a 230kOhm
541 * resistor.
543 rntc = 230000 * vntc / (VTVOUT_V - vntc);
545 temp = ab8500_btemp_res_to_temp(di,
546 di->bm->bat_type[id].r_to_t_tbl,
547 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
548 prev = temp;
550 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
551 return temp;
555 * ab8500_btemp_id() - Identify the connected battery
556 * @di: pointer to the ab8500_btemp structure
558 * This function will try to identify the battery by reading the ID
559 * resistor. Some brands use a combined ID resistor with a NTC resistor to
560 * both be able to identify and to read the temperature of it.
562 static int ab8500_btemp_id(struct ab8500_btemp *di)
564 int res;
565 u8 i;
566 if (is_ab8540(di->parent))
567 di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
568 else if (is_ab9540(di->parent) || is_ab8505(di->parent))
569 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
570 else
571 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
573 di->bm->batt_id = BATTERY_UNKNOWN;
575 res = ab8500_btemp_get_batctrl_res(di);
576 if (res < 0) {
577 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
578 return -ENXIO;
581 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
582 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
583 if ((res <= di->bm->bat_type[i].resis_high) &&
584 (res >= di->bm->bat_type[i].resis_low)) {
585 dev_dbg(di->dev, "Battery detected on %s"
586 " low %d < res %d < high: %d"
587 " index: %d\n",
588 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
589 "BATCTRL" : "BATTEMP",
590 di->bm->bat_type[i].resis_low, res,
591 di->bm->bat_type[i].resis_high, i);
593 di->bm->batt_id = i;
594 break;
598 if (di->bm->batt_id == BATTERY_UNKNOWN) {
599 dev_warn(di->dev, "Battery identified as unknown"
600 ", resistance %d Ohm\n", res);
601 return -ENXIO;
605 * We only have to change current source if the
606 * detected type is Type 1.
608 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
609 di->bm->batt_id == 1) {
610 if (is_ab8540(di->parent)) {
611 dev_dbg(di->dev,
612 "Set BATCTRL current source to 60uA\n");
613 di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
614 } else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
615 dev_dbg(di->dev,
616 "Set BATCTRL current source to 16uA\n");
617 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
618 } else {
619 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
620 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
624 return di->bm->batt_id;
628 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
629 * @work: pointer to the work_struct structure
631 * Work function for measuring the temperature periodically
633 static void ab8500_btemp_periodic_work(struct work_struct *work)
635 int interval;
636 int bat_temp;
637 struct ab8500_btemp *di = container_of(work,
638 struct ab8500_btemp, btemp_periodic_work.work);
640 if (!di->initialized) {
641 /* Identify the battery */
642 if (ab8500_btemp_id(di) < 0)
643 dev_warn(di->dev, "failed to identify the battery\n");
646 bat_temp = ab8500_btemp_measure_temp(di);
648 * Filter battery temperature.
649 * Allow direct updates on temperature only if two samples result in
650 * same temperature. Else only allow 1 degree change from previous
651 * reported value in the direction of the new measurement.
653 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
654 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
655 di->initialized = true;
656 di->bat_temp = bat_temp;
657 power_supply_changed(&di->btemp_psy);
659 } else if (bat_temp < di->prev_bat_temp) {
660 di->bat_temp--;
661 power_supply_changed(&di->btemp_psy);
662 } else if (bat_temp > di->prev_bat_temp) {
663 di->bat_temp++;
664 power_supply_changed(&di->btemp_psy);
666 di->prev_bat_temp = bat_temp;
668 if (di->events.ac_conn || di->events.usb_conn)
669 interval = di->bm->temp_interval_chg;
670 else
671 interval = di->bm->temp_interval_nochg;
673 /* Schedule a new measurement */
674 queue_delayed_work(di->btemp_wq,
675 &di->btemp_periodic_work,
676 round_jiffies(interval * HZ));
680 * ab8500_btemp_batctrlindb_handler() - battery removal detected
681 * @irq: interrupt number
682 * @_di: void pointer that has to address of ab8500_btemp
684 * Returns IRQ status(IRQ_HANDLED)
686 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
688 struct ab8500_btemp *di = _di;
689 dev_err(di->dev, "Battery removal detected!\n");
691 di->events.batt_rem = true;
692 power_supply_changed(&di->btemp_psy);
694 return IRQ_HANDLED;
698 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
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_templow_handler(int irq, void *_di)
706 struct ab8500_btemp *di = _di;
708 if (is_ab8500_3p3_or_earlier(di->parent)) {
709 dev_dbg(di->dev, "Ignore false btemp low irq"
710 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
711 } else {
712 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
714 di->events.btemp_low = true;
715 di->events.btemp_high = false;
716 di->events.btemp_medhigh = false;
717 di->events.btemp_lowmed = false;
718 power_supply_changed(&di->btemp_psy);
721 return IRQ_HANDLED;
725 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
726 * @irq: interrupt number
727 * @_di: void pointer that has to address of ab8500_btemp
729 * Returns IRQ status(IRQ_HANDLED)
731 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
733 struct ab8500_btemp *di = _di;
735 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
737 di->events.btemp_high = true;
738 di->events.btemp_medhigh = false;
739 di->events.btemp_lowmed = false;
740 di->events.btemp_low = false;
741 power_supply_changed(&di->btemp_psy);
743 return IRQ_HANDLED;
747 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
748 * @irq: interrupt number
749 * @_di: void pointer that has to address of ab8500_btemp
751 * Returns IRQ status(IRQ_HANDLED)
753 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
755 struct ab8500_btemp *di = _di;
757 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
759 di->events.btemp_lowmed = true;
760 di->events.btemp_medhigh = false;
761 di->events.btemp_high = false;
762 di->events.btemp_low = false;
763 power_supply_changed(&di->btemp_psy);
765 return IRQ_HANDLED;
769 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
770 * @irq: interrupt number
771 * @_di: void pointer that has to address of ab8500_btemp
773 * Returns IRQ status(IRQ_HANDLED)
775 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
777 struct ab8500_btemp *di = _di;
779 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
781 di->events.btemp_medhigh = true;
782 di->events.btemp_lowmed = false;
783 di->events.btemp_high = false;
784 di->events.btemp_low = false;
785 power_supply_changed(&di->btemp_psy);
787 return IRQ_HANDLED;
791 * ab8500_btemp_periodic() - Periodic temperature measurements
792 * @di: pointer to the ab8500_btemp structure
793 * @enable: enable or disable periodic temperature measurements
795 * Starts of stops periodic temperature measurements. Periodic measurements
796 * should only be done when a charger is connected.
798 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
799 bool enable)
801 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
802 enable);
804 * Make sure a new measurement is done directly by cancelling
805 * any pending work
807 cancel_delayed_work_sync(&di->btemp_periodic_work);
809 if (enable)
810 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
814 * ab8500_btemp_get_temp() - get battery temperature
815 * @di: pointer to the ab8500_btemp structure
817 * Returns battery temperature
819 int ab8500_btemp_get_temp(struct ab8500_btemp *di)
821 int temp = 0;
824 * The BTEMP events are not reliabe on AB8500 cut3.3
825 * and prior versions
827 if (is_ab8500_3p3_or_earlier(di->parent)) {
828 temp = di->bat_temp * 10;
829 } else {
830 if (di->events.btemp_low) {
831 if (temp > di->btemp_ranges.btemp_low_limit)
832 temp = di->btemp_ranges.btemp_low_limit * 10;
833 else
834 temp = di->bat_temp * 10;
835 } else if (di->events.btemp_high) {
836 if (temp < di->btemp_ranges.btemp_high_limit)
837 temp = di->btemp_ranges.btemp_high_limit * 10;
838 else
839 temp = di->bat_temp * 10;
840 } else if (di->events.btemp_lowmed) {
841 if (temp > di->btemp_ranges.btemp_med_limit)
842 temp = di->btemp_ranges.btemp_med_limit * 10;
843 else
844 temp = di->bat_temp * 10;
845 } else if (di->events.btemp_medhigh) {
846 if (temp < di->btemp_ranges.btemp_med_limit)
847 temp = di->btemp_ranges.btemp_med_limit * 10;
848 else
849 temp = di->bat_temp * 10;
850 } else
851 temp = di->bat_temp * 10;
853 return temp;
855 EXPORT_SYMBOL(ab8500_btemp_get_temp);
858 * ab8500_btemp_get_batctrl_temp() - get the temperature
859 * @btemp: pointer to the btemp structure
861 * Returns the batctrl temperature in millidegrees
863 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
865 return btemp->bat_temp * 1000;
867 EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
870 * ab8500_btemp_get_property() - get the btemp properties
871 * @psy: pointer to the power_supply structure
872 * @psp: pointer to the power_supply_property structure
873 * @val: pointer to the power_supply_propval union
875 * This function gets called when an application tries to get the btemp
876 * properties by reading the sysfs files.
877 * online: presence of the battery
878 * present: presence of the battery
879 * technology: battery technology
880 * temp: battery temperature
881 * Returns error code in case of failure else 0(on success)
883 static int ab8500_btemp_get_property(struct power_supply *psy,
884 enum power_supply_property psp,
885 union power_supply_propval *val)
887 struct ab8500_btemp *di;
889 di = to_ab8500_btemp_device_info(psy);
891 switch (psp) {
892 case POWER_SUPPLY_PROP_PRESENT:
893 case POWER_SUPPLY_PROP_ONLINE:
894 if (di->events.batt_rem)
895 val->intval = 0;
896 else
897 val->intval = 1;
898 break;
899 case POWER_SUPPLY_PROP_TECHNOLOGY:
900 val->intval = di->bm->bat_type[di->bm->batt_id].name;
901 break;
902 case POWER_SUPPLY_PROP_TEMP:
903 val->intval = ab8500_btemp_get_temp(di);
904 break;
905 default:
906 return -EINVAL;
908 return 0;
911 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
913 struct power_supply *psy;
914 struct power_supply *ext;
915 struct ab8500_btemp *di;
916 union power_supply_propval ret;
917 int i, j;
918 bool psy_found = false;
920 psy = (struct power_supply *)data;
921 ext = dev_get_drvdata(dev);
922 di = to_ab8500_btemp_device_info(psy);
925 * For all psy where the name of your driver
926 * appears in any supplied_to
928 for (i = 0; i < ext->num_supplicants; i++) {
929 if (!strcmp(ext->supplied_to[i], psy->name))
930 psy_found = true;
933 if (!psy_found)
934 return 0;
936 /* Go through all properties for the psy */
937 for (j = 0; j < ext->num_properties; j++) {
938 enum power_supply_property prop;
939 prop = ext->properties[j];
941 if (ext->get_property(ext, prop, &ret))
942 continue;
944 switch (prop) {
945 case POWER_SUPPLY_PROP_PRESENT:
946 switch (ext->type) {
947 case POWER_SUPPLY_TYPE_MAINS:
948 /* AC disconnected */
949 if (!ret.intval && di->events.ac_conn) {
950 di->events.ac_conn = false;
952 /* AC connected */
953 else if (ret.intval && !di->events.ac_conn) {
954 di->events.ac_conn = true;
955 if (!di->events.usb_conn)
956 ab8500_btemp_periodic(di, true);
958 break;
959 case POWER_SUPPLY_TYPE_USB:
960 /* USB disconnected */
961 if (!ret.intval && di->events.usb_conn) {
962 di->events.usb_conn = false;
964 /* USB connected */
965 else if (ret.intval && !di->events.usb_conn) {
966 di->events.usb_conn = true;
967 if (!di->events.ac_conn)
968 ab8500_btemp_periodic(di, true);
970 break;
971 default:
972 break;
974 break;
975 default:
976 break;
979 return 0;
983 * ab8500_btemp_external_power_changed() - callback for power supply changes
984 * @psy: pointer to the structure power_supply
986 * This function is pointing to the function pointer external_power_changed
987 * of the structure power_supply.
988 * This function gets executed when there is a change in the external power
989 * supply to the btemp.
991 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
993 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
995 class_for_each_device(power_supply_class, NULL,
996 &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
999 /* ab8500 btemp driver interrupts and their respective isr */
1000 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
1001 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
1002 {"BTEMP_LOW", ab8500_btemp_templow_handler},
1003 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
1004 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
1005 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
1008 #if defined(CONFIG_PM)
1009 static int ab8500_btemp_resume(struct platform_device *pdev)
1011 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1013 ab8500_btemp_periodic(di, true);
1015 return 0;
1018 static int ab8500_btemp_suspend(struct platform_device *pdev,
1019 pm_message_t state)
1021 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1023 ab8500_btemp_periodic(di, false);
1025 return 0;
1027 #else
1028 #define ab8500_btemp_suspend NULL
1029 #define ab8500_btemp_resume NULL
1030 #endif
1032 static int ab8500_btemp_remove(struct platform_device *pdev)
1034 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1035 int i, irq;
1037 /* Disable interrupts */
1038 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1039 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1040 free_irq(irq, di);
1043 /* Delete the work queue */
1044 destroy_workqueue(di->btemp_wq);
1046 flush_scheduled_work();
1047 power_supply_unregister(&di->btemp_psy);
1049 return 0;
1052 static char *supply_interface[] = {
1053 "ab8500_chargalg",
1054 "ab8500_fg",
1057 static int ab8500_btemp_probe(struct platform_device *pdev)
1059 struct device_node *np = pdev->dev.of_node;
1060 struct abx500_bm_data *plat = pdev->dev.platform_data;
1061 struct ab8500_btemp *di;
1062 int irq, i, ret = 0;
1063 u8 val;
1065 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1066 if (!di) {
1067 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
1068 return -ENOMEM;
1071 if (!plat) {
1072 dev_err(&pdev->dev, "no battery management data supplied\n");
1073 return -EINVAL;
1075 di->bm = plat;
1077 if (np) {
1078 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1079 if (ret) {
1080 dev_err(&pdev->dev, "failed to get battery information\n");
1081 return ret;
1085 /* get parent data */
1086 di->dev = &pdev->dev;
1087 di->parent = dev_get_drvdata(pdev->dev.parent);
1088 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1090 di->initialized = false;
1092 /* BTEMP supply */
1093 di->btemp_psy.name = "ab8500_btemp";
1094 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1095 di->btemp_psy.properties = ab8500_btemp_props;
1096 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
1097 di->btemp_psy.get_property = ab8500_btemp_get_property;
1098 di->btemp_psy.supplied_to = supply_interface;
1099 di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
1100 di->btemp_psy.external_power_changed =
1101 ab8500_btemp_external_power_changed;
1104 /* Create a work queue for the btemp */
1105 di->btemp_wq =
1106 create_singlethread_workqueue("ab8500_btemp_wq");
1107 if (di->btemp_wq == NULL) {
1108 dev_err(di->dev, "failed to create work queue\n");
1109 return -ENOMEM;
1112 /* Init work for measuring temperature periodically */
1113 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1114 ab8500_btemp_periodic_work);
1116 /* Set BTEMP thermal limits. Low and Med are fixed */
1117 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1118 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1120 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1121 AB8500_BTEMP_HIGH_TH, &val);
1122 if (ret < 0) {
1123 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1124 goto free_btemp_wq;
1126 switch (val) {
1127 case BTEMP_HIGH_TH_57_0:
1128 case BTEMP_HIGH_TH_57_1:
1129 di->btemp_ranges.btemp_high_limit =
1130 BTEMP_THERMAL_HIGH_LIMIT_57;
1131 break;
1132 case BTEMP_HIGH_TH_52:
1133 di->btemp_ranges.btemp_high_limit =
1134 BTEMP_THERMAL_HIGH_LIMIT_52;
1135 break;
1136 case BTEMP_HIGH_TH_62:
1137 di->btemp_ranges.btemp_high_limit =
1138 BTEMP_THERMAL_HIGH_LIMIT_62;
1139 break;
1142 /* Register BTEMP power supply class */
1143 ret = power_supply_register(di->dev, &di->btemp_psy);
1144 if (ret) {
1145 dev_err(di->dev, "failed to register BTEMP psy\n");
1146 goto free_btemp_wq;
1149 /* Register interrupts */
1150 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1151 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1152 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1153 IRQF_SHARED | IRQF_NO_SUSPEND,
1154 ab8500_btemp_irq[i].name, di);
1156 if (ret) {
1157 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1158 , ab8500_btemp_irq[i].name, irq, ret);
1159 goto free_irq;
1161 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1162 ab8500_btemp_irq[i].name, irq, ret);
1165 platform_set_drvdata(pdev, di);
1167 /* Kick off periodic temperature measurements */
1168 ab8500_btemp_periodic(di, true);
1169 list_add_tail(&di->node, &ab8500_btemp_list);
1171 return ret;
1173 free_irq:
1174 power_supply_unregister(&di->btemp_psy);
1176 /* We also have to free all successfully registered irqs */
1177 for (i = i - 1; i >= 0; i--) {
1178 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1179 free_irq(irq, di);
1181 free_btemp_wq:
1182 destroy_workqueue(di->btemp_wq);
1183 return ret;
1186 static const struct of_device_id ab8500_btemp_match[] = {
1187 { .compatible = "stericsson,ab8500-btemp", },
1188 { },
1191 static struct platform_driver ab8500_btemp_driver = {
1192 .probe = ab8500_btemp_probe,
1193 .remove = ab8500_btemp_remove,
1194 .suspend = ab8500_btemp_suspend,
1195 .resume = ab8500_btemp_resume,
1196 .driver = {
1197 .name = "ab8500-btemp",
1198 .owner = THIS_MODULE,
1199 .of_match_table = ab8500_btemp_match,
1203 static int __init ab8500_btemp_init(void)
1205 return platform_driver_register(&ab8500_btemp_driver);
1208 static void __exit ab8500_btemp_exit(void)
1210 platform_driver_unregister(&ab8500_btemp_driver);
1213 device_initcall(ab8500_btemp_init);
1214 module_exit(ab8500_btemp_exit);
1216 MODULE_LICENSE("GPL v2");
1217 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1218 MODULE_ALIAS("platform:ab8500-btemp");
1219 MODULE_DESCRIPTION("AB8500 battery temperature driver");