Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / power / supply / ab8500_btemp.c
blobf7a35ebfbab29cf329312bd4b7315bdd0b2b7ec6
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 /**
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 {
54 char *name;
55 irqreturn_t (*isr)(int irq, void *data);
58 struct ab8500_btemp_events {
59 bool batt_rem;
60 bool btemp_high;
61 bool btemp_medhigh;
62 bool btemp_lowmed;
63 bool btemp_low;
64 bool ac_conn;
65 bool usb_conn;
68 struct ab8500_btemp_ranges {
69 int btemp_high_limit;
70 int btemp_med_limit;
71 int btemp_low_limit;
74 /**
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.
92 struct ab8500_btemp {
93 struct device *dev;
94 struct list_head node;
95 int curr_source;
96 int bat_temp;
97 int prev_bat_temp;
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;
107 bool initialized;
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)
143 int rbs;
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)
160 / di->curr_source;
161 } else {
163 * BAT_CTRL is internally
164 * connected to 1.8V through a 80k resistor
166 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
169 return rbs;
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)
180 int vbtemp;
181 static int prev;
183 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
184 if (vbtemp < 0) {
185 dev_err(di->dev,
186 "%s gpadc conversion failed, using previous value",
187 __func__);
188 return prev;
190 prev = vbtemp;
191 return vbtemp;
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,
202 bool enable)
204 int curr;
205 int ret = 0;
208 * BATCTRL current sources are included on AB8500 cut2.0
209 * and future versions
211 if (is_ab8500_1p1_or_earlier(di->parent))
212 return 0;
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;
220 else
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;
225 else
226 curr = BAT_CTRL_18U_ENA;
227 } else {
228 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
229 curr = BAT_CTRL_7U_ENA;
230 else
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);
239 if (ret) {
240 dev_err(di->dev, "%s failed setting cmp_force\n",
241 __func__);
242 return ret;
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
250 udelay(32);
252 ret = abx500_set_register_interruptible(di->dev,
253 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
254 FORCE_BAT_CTRL_CMP_HIGH | curr);
255 if (ret) {
256 dev_err(di->dev, "%s failed enabling current source\n",
257 __func__);
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(
266 di->dev,
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(
273 di->dev,
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));
277 } else {
278 /* Write 0 to the curr bits */
279 ret = abx500_mask_and_set_register_interruptible(
280 di->dev,
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));
286 if (ret) {
287 dev_err(di->dev, "%s failed disabling current source\n",
288 __func__);
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);
297 if (ret) {
298 dev_err(di->dev, "%s failed enabling PU and comp\n",
299 __func__);
300 goto enable_pu_comp;
304 * We have to wait one 32kHz cycle before disabling
305 * ForceBatCtrlCmpHigh since this needs to be written
306 * in a separate cycle
308 udelay(32);
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);
314 if (ret) {
315 dev_err(di->dev, "%s failed disabling force comp\n",
316 __func__);
317 goto disable_force_comp;
320 return ret;
323 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
324 * if we got an error above
326 disable_curr_source:
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));
339 } else {
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));
347 if (ret) {
348 dev_err(di->dev, "%s failed disabling current source\n",
349 __func__);
350 return ret;
352 enable_pu_comp:
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);
358 if (ret) {
359 dev_err(di->dev, "%s failed enabling PU and comp\n",
360 __func__);
361 return ret;
364 disable_force_comp:
366 * We have to wait one 32kHz cycle before disabling
367 * ForceBatCtrlCmpHigh since this needs to be written
368 * in a separate cycle
370 udelay(32);
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);
376 if (ret) {
377 dev_err(di->dev, "%s failed disabling force comp\n",
378 __func__);
379 return ret;
382 return ret;
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)
394 int ret;
395 int batctrl = 0;
396 int res;
397 int inst_curr;
398 int i;
401 * BATCTRL current sources are included on AB8500 cut2.0
402 * and future versions
404 ret = ab8500_btemp_curr_source_enable(di, true);
405 if (ret) {
406 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
407 return ret;
410 if (!di->fg)
411 di->fg = ab8500_fg_get();
412 if (!di->fg) {
413 dev_err(di->dev, "No fg found\n");
414 return -EINVAL;
417 ret = ab8500_fg_inst_curr_start(di->fg);
419 if (ret) {
420 dev_err(di->dev, "Failed to start current measurement\n");
421 return ret;
424 do {
425 msleep(20);
426 } while (!ab8500_fg_inst_curr_started(di->fg));
428 i = 0;
430 do {
431 batctrl += ab8500_btemp_read_batctrl_voltage(di);
432 i++;
433 msleep(20);
434 } while (!ab8500_fg_inst_curr_done(di->fg));
435 batctrl /= i;
437 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
438 if (ret) {
439 dev_err(di->dev, "Failed to finalize current measurement\n");
440 return ret;
443 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
445 ret = ab8500_btemp_curr_source_enable(di, false);
446 if (ret) {
447 dev_err(di->dev, "%s curr source disable failed\n", __func__);
448 return ret;
451 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
452 __func__, batctrl, res, inst_curr, i);
454 return res;
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)
470 int i;
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)
478 i = 0;
479 else if (res <= tbl[tbl_size - 1].resist)
480 i = tbl_size - 2;
481 else {
482 i = 0;
483 while (!(res <= tbl[i].resist &&
484 res > tbl[i + 1].resist))
485 i++;
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)
500 int temp;
501 static int prev;
502 int rbat, rntc, vntc;
503 u8 id;
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);
511 if (rbat < 0) {
512 dev_err(di->dev, "%s get batctrl res failed\n",
513 __func__);
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);
524 } else {
525 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
526 if (vntc < 0) {
527 dev_err(di->dev,
528 "%s gpadc conversion failed,"
529 " using previous value\n", __func__);
530 return prev;
533 * The PCB NTC is sourced from VTVOUT via a 230kOhm
534 * resistor.
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);
541 prev = temp;
543 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
544 return 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)
557 int res;
558 u8 i;
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;
563 else
564 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
566 di->bm->batt_id = BATTERY_UNKNOWN;
568 res = ab8500_btemp_get_batctrl_res(di);
569 if (res < 0) {
570 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
571 return -ENXIO;
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"
580 " index: %d\n",
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);
586 di->bm->batt_id = i;
587 break;
591 if (di->bm->batt_id == BATTERY_UNKNOWN) {
592 dev_warn(di->dev, "Battery identified as unknown"
593 ", resistance %d Ohm\n", res);
594 return -ENXIO;
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)) {
604 dev_dbg(di->dev,
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)) {
608 dev_dbg(di->dev,
609 "Set BATCTRL current source to 16uA\n");
610 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
611 } else {
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)
628 int interval;
629 int bat_temp;
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) {
653 di->bat_temp--;
654 power_supply_changed(di->btemp_psy);
655 } else if (bat_temp > di->prev_bat_temp) {
656 di->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;
663 else
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);
687 return IRQ_HANDLED;
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");
704 } else {
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);
714 return IRQ_HANDLED;
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);
736 return IRQ_HANDLED;
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);
758 return IRQ_HANDLED;
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);
780 return IRQ_HANDLED;
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,
792 bool enable)
794 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
795 enable);
797 * Make sure a new measurement is done directly by cancelling
798 * any pending work
800 cancel_delayed_work_sync(&di->btemp_periodic_work);
802 if (enable)
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)
814 int temp = 0;
817 * The BTEMP events are not reliabe on AB8500 cut3.3
818 * and prior versions
820 if (is_ab8500_3p3_or_earlier(di->parent)) {
821 temp = di->bat_temp * 10;
822 } else {
823 if (di->events.btemp_low) {
824 if (temp > di->btemp_ranges.btemp_low_limit)
825 temp = di->btemp_ranges.btemp_low_limit * 10;
826 else
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;
831 else
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;
836 else
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;
841 else
842 temp = di->bat_temp * 10;
843 } else
844 temp = di->bat_temp * 10;
846 return temp;
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);
882 switch (psp) {
883 case POWER_SUPPLY_PROP_PRESENT:
884 case POWER_SUPPLY_PROP_ONLINE:
885 if (di->events.batt_rem)
886 val->intval = 0;
887 else
888 val->intval = 1;
889 break;
890 case POWER_SUPPLY_PROP_TECHNOLOGY:
891 val->intval = di->bm->bat_type[di->bm->batt_id].name;
892 break;
893 case POWER_SUPPLY_PROP_TEMP:
894 val->intval = ab8500_btemp_get_temp(di);
895 break;
896 default:
897 return -EINVAL;
899 return 0;
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;
909 int j;
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);
919 if (j < 0)
920 return 0;
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))
928 continue;
930 switch (prop) {
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;
938 /* AC connected */
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);
944 break;
945 case POWER_SUPPLY_TYPE_USB:
946 /* USB disconnected */
947 if (!ret.intval && di->events.usb_conn) {
948 di->events.usb_conn = false;
950 /* USB connected */
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);
956 break;
957 default:
958 break;
960 break;
961 default:
962 break;
965 return 0;
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 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
981 class_for_each_device(power_supply_class, NULL,
982 di->btemp_psy, ab8500_btemp_get_ext_psy_data);
985 /* ab8500 btemp driver interrupts and their respective isr */
986 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
987 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
988 {"BTEMP_LOW", ab8500_btemp_templow_handler},
989 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
990 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
991 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
994 #if defined(CONFIG_PM)
995 static int ab8500_btemp_resume(struct platform_device *pdev)
997 struct ab8500_btemp *di = platform_get_drvdata(pdev);
999 ab8500_btemp_periodic(di, true);
1001 return 0;
1004 static int ab8500_btemp_suspend(struct platform_device *pdev,
1005 pm_message_t state)
1007 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1009 ab8500_btemp_periodic(di, false);
1011 return 0;
1013 #else
1014 #define ab8500_btemp_suspend NULL
1015 #define ab8500_btemp_resume NULL
1016 #endif
1018 static int ab8500_btemp_remove(struct platform_device *pdev)
1020 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1021 int i, irq;
1023 /* Disable interrupts */
1024 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1025 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1026 free_irq(irq, di);
1029 /* Delete the work queue */
1030 destroy_workqueue(di->btemp_wq);
1032 flush_scheduled_work();
1033 power_supply_unregister(di->btemp_psy);
1035 return 0;
1038 static char *supply_interface[] = {
1039 "ab8500_chargalg",
1040 "ab8500_fg",
1043 static const struct power_supply_desc ab8500_btemp_desc = {
1044 .name = "ab8500_btemp",
1045 .type = POWER_SUPPLY_TYPE_BATTERY,
1046 .properties = ab8500_btemp_props,
1047 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
1048 .get_property = ab8500_btemp_get_property,
1049 .external_power_changed = ab8500_btemp_external_power_changed,
1052 static int ab8500_btemp_probe(struct platform_device *pdev)
1054 struct device_node *np = pdev->dev.of_node;
1055 struct abx500_bm_data *plat = pdev->dev.platform_data;
1056 struct power_supply_config psy_cfg = {};
1057 struct ab8500_btemp *di;
1058 int irq, i, ret = 0;
1059 u8 val;
1061 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1062 if (!di) {
1063 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
1064 return -ENOMEM;
1067 if (!plat) {
1068 dev_err(&pdev->dev, "no battery management data supplied\n");
1069 return -EINVAL;
1071 di->bm = plat;
1073 if (np) {
1074 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1075 if (ret) {
1076 dev_err(&pdev->dev, "failed to get battery information\n");
1077 return ret;
1081 /* get parent data */
1082 di->dev = &pdev->dev;
1083 di->parent = dev_get_drvdata(pdev->dev.parent);
1084 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1086 di->initialized = false;
1088 psy_cfg.supplied_to = supply_interface;
1089 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1090 psy_cfg.drv_data = di;
1092 /* Create a work queue for the btemp */
1093 di->btemp_wq =
1094 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
1095 if (di->btemp_wq == NULL) {
1096 dev_err(di->dev, "failed to create work queue\n");
1097 return -ENOMEM;
1100 /* Init work for measuring temperature periodically */
1101 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1102 ab8500_btemp_periodic_work);
1104 /* Set BTEMP thermal limits. Low and Med are fixed */
1105 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1106 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1108 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1109 AB8500_BTEMP_HIGH_TH, &val);
1110 if (ret < 0) {
1111 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1112 goto free_btemp_wq;
1114 switch (val) {
1115 case BTEMP_HIGH_TH_57_0:
1116 case BTEMP_HIGH_TH_57_1:
1117 di->btemp_ranges.btemp_high_limit =
1118 BTEMP_THERMAL_HIGH_LIMIT_57;
1119 break;
1120 case BTEMP_HIGH_TH_52:
1121 di->btemp_ranges.btemp_high_limit =
1122 BTEMP_THERMAL_HIGH_LIMIT_52;
1123 break;
1124 case BTEMP_HIGH_TH_62:
1125 di->btemp_ranges.btemp_high_limit =
1126 BTEMP_THERMAL_HIGH_LIMIT_62;
1127 break;
1130 /* Register BTEMP power supply class */
1131 di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
1132 &psy_cfg);
1133 if (IS_ERR(di->btemp_psy)) {
1134 dev_err(di->dev, "failed to register BTEMP psy\n");
1135 ret = PTR_ERR(di->btemp_psy);
1136 goto free_btemp_wq;
1139 /* Register interrupts */
1140 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1141 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1142 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1143 IRQF_SHARED | IRQF_NO_SUSPEND,
1144 ab8500_btemp_irq[i].name, di);
1146 if (ret) {
1147 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1148 , ab8500_btemp_irq[i].name, irq, ret);
1149 goto free_irq;
1151 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1152 ab8500_btemp_irq[i].name, irq, ret);
1155 platform_set_drvdata(pdev, di);
1157 /* Kick off periodic temperature measurements */
1158 ab8500_btemp_periodic(di, true);
1159 list_add_tail(&di->node, &ab8500_btemp_list);
1161 return ret;
1163 free_irq:
1164 power_supply_unregister(di->btemp_psy);
1166 /* We also have to free all successfully registered irqs */
1167 for (i = i - 1; i >= 0; i--) {
1168 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1169 free_irq(irq, di);
1171 free_btemp_wq:
1172 destroy_workqueue(di->btemp_wq);
1173 return ret;
1176 static const struct of_device_id ab8500_btemp_match[] = {
1177 { .compatible = "stericsson,ab8500-btemp", },
1178 { },
1181 static struct platform_driver ab8500_btemp_driver = {
1182 .probe = ab8500_btemp_probe,
1183 .remove = ab8500_btemp_remove,
1184 .suspend = ab8500_btemp_suspend,
1185 .resume = ab8500_btemp_resume,
1186 .driver = {
1187 .name = "ab8500-btemp",
1188 .of_match_table = ab8500_btemp_match,
1192 static int __init ab8500_btemp_init(void)
1194 return platform_driver_register(&ab8500_btemp_driver);
1197 static void __exit ab8500_btemp_exit(void)
1199 platform_driver_unregister(&ab8500_btemp_driver);
1202 device_initcall(ab8500_btemp_init);
1203 module_exit(ab8500_btemp_exit);
1205 MODULE_LICENSE("GPL v2");
1206 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1207 MODULE_ALIAS("platform:ab8500-btemp");
1208 MODULE_DESCRIPTION("AB8500 battery temperature driver");