ab8500-btemp: Filter btemp readings
[deliverable/linux.git] / drivers / power / ab8500_btemp.c
CommitLineData
1f855824
AM
1/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Battery temperature driver for AB8500
5 *
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>
11 */
12
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>
bd9e8ab2
R
23#include <linux/jiffies.h>
24#include <linux/of.h>
25#include <linux/mfd/core.h>
1f855824 26#include <linux/mfd/abx500.h>
bd9e8ab2 27#include <linux/mfd/abx500/ab8500.h>
1f855824
AM
28#include <linux/mfd/abx500/ab8500-bm.h>
29#include <linux/mfd/abx500/ab8500-gpadc.h>
1f855824
AM
30
31#define VTVOUT_V 1800
32
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
38
39#define BTEMP_BATCTRL_CURR_SRC_7UA 7
40#define BTEMP_BATCTRL_CURR_SRC_20UA 20
41
e6aac611
MJ
42#define BTEMP_BATCTRL_CURR_SRC_16UA 16
43#define BTEMP_BATCTRL_CURR_SRC_18UA 18
44
1f855824
AM
45#define to_ab8500_btemp_device_info(x) container_of((x), \
46 struct ab8500_btemp, btemp_psy);
47
48/**
49 * struct ab8500_btemp_interrupts - ab8500 interrupts
50 * @name: name of the interrupt
51 * @isr function pointer to the isr
52 */
53struct ab8500_btemp_interrupts {
54 char *name;
55 irqreturn_t (*isr)(int irq, void *data);
56};
57
58struct 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;
66};
67
68struct ab8500_btemp_ranges {
69 int btemp_high_limit;
70 int btemp_med_limit;
71 int btemp_low_limit;
72};
73
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
908fe8d6
HB
79 * @bat_temp: Dispatched battery temperature in degree Celcius
80 * @prev_bat_temp Last measured battery temperature in degree Celcius
1f855824
AM
81 * @parent: Pointer to the struct ab8500
82 * @gpadc: Pointer to the struct gpadc
83 * @fg: Pointer to the struct fg
b0284de0 84 * @bm: Platform specific battery management information
1f855824
AM
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
f6271b4f 90 * @initialized: True if battery id read.
1f855824
AM
91 */
92struct 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;
b0284de0 101 struct abx500_bm_data *bm;
1f855824
AM
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;
f6271b4f 107 bool initialized;
1f855824
AM
108};
109
110/* BTEMP power supply properties */
111static 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,
116};
117
118static LIST_HEAD(ab8500_btemp_list);
119
120/**
121 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
122 * (i.e. the first BTEMP in the instance list)
123 */
124struct ab8500_btemp *ab8500_btemp_get(void)
125{
126 struct ab8500_btemp *btemp;
127 btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
128
129 return btemp;
130}
131
132/**
133 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
134 * @di: pointer to the ab8500_btemp structure
135 * @v_batctrl: measured batctrl voltage
136 * @inst_curr: measured instant current
137 *
138 * This function returns the battery resistance that is
139 * derived from the BATCTRL voltage.
140 * Returns value in Ohms.
141 */
142static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
143 int v_batctrl, int inst_curr)
144{
145 int rbs;
146
147 if (is_ab8500_1p1_or_earlier(di->parent)) {
148 /*
149 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
150 * connected to 1.8V through a 450k resistor
151 */
152 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
153 }
154
b0284de0 155 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
1f855824
AM
156 /*
157 * If the battery has internal NTC, we use the current
158 * source to calculate the resistance, 7uA or 20uA
159 */
160 rbs = (v_batctrl * 1000
b0284de0 161 - di->bm->gnd_lift_resistance * inst_curr)
1f855824
AM
162 / di->curr_source;
163 } else {
164 /*
165 * BAT_CTRL is internally
166 * connected to 1.8V through a 80k resistor
167 */
168 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
169 }
170
171 return rbs;
172}
173
174/**
175 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
176 * @di: pointer to the ab8500_btemp structure
177 *
178 * This function returns the voltage on BATCTRL. Returns value in mV.
179 */
180static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
181{
182 int vbtemp;
183 static int prev;
184
185 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
186 if (vbtemp < 0) {
187 dev_err(di->dev,
188 "%s gpadc conversion failed, using previous value",
189 __func__);
190 return prev;
191 }
192 prev = vbtemp;
193 return vbtemp;
194}
195
196/**
197 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
198 * @di: pointer to the ab8500_btemp structure
199 * @enable: enable or disable the current source
200 *
201 * Enable or disable the current sources for the BatCtrl AD channel
202 */
203static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
204 bool enable)
205{
206 int curr;
207 int ret = 0;
208
209 /*
210 * BATCTRL current sources are included on AB8500 cut2.0
211 * and future versions
212 */
213 if (is_ab8500_1p1_or_earlier(di->parent))
214 return 0;
215
216 /* Only do this for batteries with internal NTC */
b0284de0 217 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
e6aac611
MJ
218
219 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
220 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_16UA)
221 curr = BAT_CTRL_16U_ENA;
222 else
223 curr = BAT_CTRL_18U_ENA;
224 } else {
225 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
226 curr = BAT_CTRL_7U_ENA;
227 else
228 curr = BAT_CTRL_20U_ENA;
229 }
1f855824
AM
230
231 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
232
233 ret = abx500_mask_and_set_register_interruptible(di->dev,
234 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
235 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
236 if (ret) {
237 dev_err(di->dev, "%s failed setting cmp_force\n",
238 __func__);
239 return ret;
240 }
241
242 /*
243 * We have to wait one 32kHz cycle before enabling
244 * the current source, since ForceBatCtrlCmpHigh needs
245 * to be written in a separate cycle
246 */
247 udelay(32);
248
249 ret = abx500_set_register_interruptible(di->dev,
250 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
251 FORCE_BAT_CTRL_CMP_HIGH | curr);
252 if (ret) {
253 dev_err(di->dev, "%s failed enabling current source\n",
254 __func__);
255 goto disable_curr_source;
256 }
b0284de0 257 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
1f855824
AM
258 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
259
e6aac611
MJ
260 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
261 /* Write 0 to the curr bits */
262 ret = abx500_mask_and_set_register_interruptible(
263 di->dev,
264 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
265 BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
266 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
267 } else {
268 /* Write 0 to the curr bits */
269 ret = abx500_mask_and_set_register_interruptible(
270 di->dev,
271 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
272 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
273 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
274 }
275
1f855824
AM
276 if (ret) {
277 dev_err(di->dev, "%s failed disabling current source\n",
278 __func__);
279 goto disable_curr_source;
280 }
281
282 /* Enable Pull-Up and comparator */
283 ret = abx500_mask_and_set_register_interruptible(di->dev,
284 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
285 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
286 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
287 if (ret) {
288 dev_err(di->dev, "%s failed enabling PU and comp\n",
289 __func__);
290 goto enable_pu_comp;
291 }
292
293 /*
294 * We have to wait one 32kHz cycle before disabling
295 * ForceBatCtrlCmpHigh since this needs to be written
296 * in a separate cycle
297 */
298 udelay(32);
299
300 /* Disable 'force comparator' */
301 ret = abx500_mask_and_set_register_interruptible(di->dev,
302 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
303 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
304 if (ret) {
305 dev_err(di->dev, "%s failed disabling force comp\n",
306 __func__);
307 goto disable_force_comp;
308 }
309 }
310 return ret;
311
312 /*
313 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
314 * if we got an error above
315 */
316disable_curr_source:
e6aac611
MJ
317 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
318 /* Write 0 to the curr bits */
319 ret = abx500_mask_and_set_register_interruptible(di->dev,
320 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
321 BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
322 ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
323 } else {
324 /* Write 0 to the curr bits */
325 ret = abx500_mask_and_set_register_interruptible(di->dev,
1f855824
AM
326 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
327 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
328 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
e6aac611
MJ
329 }
330
1f855824
AM
331 if (ret) {
332 dev_err(di->dev, "%s failed disabling current source\n",
333 __func__);
334 return ret;
335 }
336enable_pu_comp:
337 /* Enable Pull-Up and comparator */
338 ret = abx500_mask_and_set_register_interruptible(di->dev,
339 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
340 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
341 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
342 if (ret) {
343 dev_err(di->dev, "%s failed enabling PU and comp\n",
344 __func__);
345 return ret;
346 }
347
348disable_force_comp:
349 /*
350 * We have to wait one 32kHz cycle before disabling
351 * ForceBatCtrlCmpHigh since this needs to be written
352 * in a separate cycle
353 */
354 udelay(32);
355
356 /* Disable 'force comparator' */
357 ret = abx500_mask_and_set_register_interruptible(di->dev,
358 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
359 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
360 if (ret) {
361 dev_err(di->dev, "%s failed disabling force comp\n",
362 __func__);
363 return ret;
364 }
365
366 return ret;
367}
368
369/**
370 * ab8500_btemp_get_batctrl_res() - get battery resistance
371 * @di: pointer to the ab8500_btemp structure
372 *
373 * This function returns the battery pack identification resistance.
374 * Returns value in Ohms.
375 */
376static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
377{
378 int ret;
379 int batctrl = 0;
380 int res;
381 int inst_curr;
382 int i;
383
384 /*
385 * BATCTRL current sources are included on AB8500 cut2.0
386 * and future versions
387 */
388 ret = ab8500_btemp_curr_source_enable(di, true);
389 if (ret) {
390 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
391 return ret;
392 }
393
394 if (!di->fg)
395 di->fg = ab8500_fg_get();
396 if (!di->fg) {
397 dev_err(di->dev, "No fg found\n");
398 return -EINVAL;
399 }
400
401 ret = ab8500_fg_inst_curr_start(di->fg);
402
403 if (ret) {
404 dev_err(di->dev, "Failed to start current measurement\n");
405 return ret;
406 }
407
3988a4df
JB
408 do {
409 msleep(20);
410 } while (!ab8500_fg_inst_curr_started(di->fg));
411
1f855824
AM
412 i = 0;
413
414 do {
415 batctrl += ab8500_btemp_read_batctrl_voltage(di);
416 i++;
417 msleep(20);
418 } while (!ab8500_fg_inst_curr_done(di->fg));
419 batctrl /= i;
420
421 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
422 if (ret) {
423 dev_err(di->dev, "Failed to finalize current measurement\n");
424 return ret;
425 }
426
427 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
428
429 ret = ab8500_btemp_curr_source_enable(di, false);
430 if (ret) {
431 dev_err(di->dev, "%s curr source disable failed\n", __func__);
432 return ret;
433 }
434
435 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
436 __func__, batctrl, res, inst_curr, i);
437
438 return res;
439}
440
441/**
442 * ab8500_btemp_res_to_temp() - resistance to temperature
443 * @di: pointer to the ab8500_btemp structure
444 * @tbl: pointer to the resiatance to temperature table
445 * @tbl_size: size of the resistance to temperature table
446 * @res: resistance to calculate the temperature from
447 *
448 * This function returns the battery temperature in degrees Celcius
449 * based on the NTC resistance.
450 */
451static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
452 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
453{
454 int i, temp;
455 /*
456 * Calculate the formula for the straight line
457 * Simple interpolation if we are within
458 * the resistance table limits, extrapolate
459 * if resistance is outside the limits.
460 */
461 if (res > tbl[0].resist)
462 i = 0;
463 else if (res <= tbl[tbl_size - 1].resist)
464 i = tbl_size - 2;
465 else {
466 i = 0;
467 while (!(res <= tbl[i].resist &&
468 res > tbl[i + 1].resist))
469 i++;
470 }
471
472 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
473 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
474 return temp;
475}
476
477/**
478 * ab8500_btemp_measure_temp() - measure battery temperature
479 * @di: pointer to the ab8500_btemp structure
480 *
481 * Returns battery temperature (on success) else the previous temperature
482 */
483static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
484{
485 int temp;
486 static int prev;
487 int rbat, rntc, vntc;
488 u8 id;
489
b0284de0 490 id = di->bm->batt_id;
1f855824 491
b0284de0 492 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
1f855824
AM
493 id != BATTERY_UNKNOWN) {
494
495 rbat = ab8500_btemp_get_batctrl_res(di);
496 if (rbat < 0) {
497 dev_err(di->dev, "%s get batctrl res failed\n",
498 __func__);
499 /*
500 * Return out-of-range temperature so that
501 * charging is stopped
502 */
503 return BTEMP_THERMAL_LOW_LIMIT;
504 }
505
506 temp = ab8500_btemp_res_to_temp(di,
b0284de0
LJ
507 di->bm->bat_type[id].r_to_t_tbl,
508 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
1f855824
AM
509 } else {
510 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
511 if (vntc < 0) {
512 dev_err(di->dev,
513 "%s gpadc conversion failed,"
514 " using previous value\n", __func__);
515 return prev;
516 }
517 /*
518 * The PCB NTC is sourced from VTVOUT via a 230kOhm
519 * resistor.
520 */
521 rntc = 230000 * vntc / (VTVOUT_V - vntc);
522
523 temp = ab8500_btemp_res_to_temp(di,
b0284de0
LJ
524 di->bm->bat_type[id].r_to_t_tbl,
525 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
1f855824
AM
526 prev = temp;
527 }
528 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
529 return temp;
530}
531
532/**
533 * ab8500_btemp_id() - Identify the connected battery
534 * @di: pointer to the ab8500_btemp structure
535 *
536 * This function will try to identify the battery by reading the ID
537 * resistor. Some brands use a combined ID resistor with a NTC resistor to
538 * both be able to identify and to read the temperature of it.
539 */
540static int ab8500_btemp_id(struct ab8500_btemp *di)
541{
542 int res;
543 u8 i;
e6aac611
MJ
544 if (is_ab9540(di->parent) || is_ab8505(di->parent))
545 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
546 else
547 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
1f855824 548
b0284de0 549 di->bm->batt_id = BATTERY_UNKNOWN;
1f855824
AM
550
551 res = ab8500_btemp_get_batctrl_res(di);
552 if (res < 0) {
553 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
554 return -ENXIO;
555 }
556
557 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
b0284de0
LJ
558 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
559 if ((res <= di->bm->bat_type[i].resis_high) &&
560 (res >= di->bm->bat_type[i].resis_low)) {
1f855824
AM
561 dev_dbg(di->dev, "Battery detected on %s"
562 " low %d < res %d < high: %d"
563 " index: %d\n",
b0284de0 564 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
1f855824 565 "BATCTRL" : "BATTEMP",
b0284de0
LJ
566 di->bm->bat_type[i].resis_low, res,
567 di->bm->bat_type[i].resis_high, i);
1f855824 568
b0284de0 569 di->bm->batt_id = i;
1f855824
AM
570 break;
571 }
572 }
573
b0284de0 574 if (di->bm->batt_id == BATTERY_UNKNOWN) {
1f855824
AM
575 dev_warn(di->dev, "Battery identified as unknown"
576 ", resistance %d Ohm\n", res);
577 return -ENXIO;
578 }
579
580 /*
581 * We only have to change current source if the
582 * detected type is Type 1, else we use the 7uA source
583 */
b0284de0
LJ
584 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
585 di->bm->batt_id == 1) {
e6aac611
MJ
586 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
587 dev_dbg(di->dev, "Set BATCTRL current source to 16uA\n");
588 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
589 } else {
590 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
591 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
592 }
1f855824
AM
593 }
594
b0284de0 595 return di->bm->batt_id;
1f855824
AM
596}
597
598/**
599 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
600 * @work: pointer to the work_struct structure
601 *
602 * Work function for measuring the temperature periodically
603 */
604static void ab8500_btemp_periodic_work(struct work_struct *work)
605{
606 int interval;
908fe8d6 607 int bat_temp;
1f855824
AM
608 struct ab8500_btemp *di = container_of(work,
609 struct ab8500_btemp, btemp_periodic_work.work);
610
f6271b4f
JA
611 if (!di->initialized) {
612 di->initialized = true;
613 /* Identify the battery */
614 if (ab8500_btemp_id(di) < 0)
615 dev_warn(di->dev, "failed to identify the battery\n");
616 }
617
908fe8d6
HB
618 bat_temp = ab8500_btemp_measure_temp(di);
619 /*
620 * Filter battery temperature.
621 * Allow direct updates on temperature only if two samples result in
622 * same temperature. Else only allow 1 degree change from previous
623 * reported value in the direction of the new measurement.
624 */
625 if (bat_temp == di->prev_bat_temp || !di->initialized) {
626 if (di->bat_temp != di->prev_bat_temp || !di->initialized) {
627 di->bat_temp = bat_temp;
628 power_supply_changed(&di->btemp_psy);
629 }
630 } else if (bat_temp < di->prev_bat_temp) {
631 di->bat_temp--;
632 power_supply_changed(&di->btemp_psy);
633 } else if (bat_temp > di->prev_bat_temp) {
634 di->bat_temp++;
1f855824
AM
635 power_supply_changed(&di->btemp_psy);
636 }
908fe8d6 637 di->prev_bat_temp = bat_temp;
1f855824
AM
638
639 if (di->events.ac_conn || di->events.usb_conn)
b0284de0 640 interval = di->bm->temp_interval_chg;
1f855824 641 else
b0284de0 642 interval = di->bm->temp_interval_nochg;
1f855824
AM
643
644 /* Schedule a new measurement */
645 queue_delayed_work(di->btemp_wq,
646 &di->btemp_periodic_work,
647 round_jiffies(interval * HZ));
648}
649
650/**
651 * ab8500_btemp_batctrlindb_handler() - battery removal detected
652 * @irq: interrupt number
653 * @_di: void pointer that has to address of ab8500_btemp
654 *
655 * Returns IRQ status(IRQ_HANDLED)
656 */
657static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
658{
659 struct ab8500_btemp *di = _di;
660 dev_err(di->dev, "Battery removal detected!\n");
661
662 di->events.batt_rem = true;
663 power_supply_changed(&di->btemp_psy);
664
665 return IRQ_HANDLED;
666}
667
668/**
669 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
670 * @irq: interrupt number
671 * @_di: void pointer that has to address of ab8500_btemp
672 *
673 * Returns IRQ status(IRQ_HANDLED)
674 */
675static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
676{
677 struct ab8500_btemp *di = _di;
678
d36e3e6d 679 if (is_ab8500_3p3_or_earlier(di->parent)) {
1f855824 680 dev_dbg(di->dev, "Ignore false btemp low irq"
d36e3e6d 681 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
1f855824
AM
682 } else {
683 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
684
685 di->events.btemp_low = true;
686 di->events.btemp_high = false;
687 di->events.btemp_medhigh = false;
688 di->events.btemp_lowmed = false;
689 power_supply_changed(&di->btemp_psy);
690 }
691
692 return IRQ_HANDLED;
693}
694
695/**
696 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
697 * @irq: interrupt number
698 * @_di: void pointer that has to address of ab8500_btemp
699 *
700 * Returns IRQ status(IRQ_HANDLED)
701 */
702static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
703{
704 struct ab8500_btemp *di = _di;
705
706 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
707
708 di->events.btemp_high = true;
709 di->events.btemp_medhigh = false;
710 di->events.btemp_lowmed = false;
711 di->events.btemp_low = false;
712 power_supply_changed(&di->btemp_psy);
713
714 return IRQ_HANDLED;
715}
716
717/**
718 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
719 * @irq: interrupt number
720 * @_di: void pointer that has to address of ab8500_btemp
721 *
722 * Returns IRQ status(IRQ_HANDLED)
723 */
724static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
725{
726 struct ab8500_btemp *di = _di;
727
728 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
729
730 di->events.btemp_lowmed = true;
731 di->events.btemp_medhigh = false;
732 di->events.btemp_high = false;
733 di->events.btemp_low = false;
734 power_supply_changed(&di->btemp_psy);
735
736 return IRQ_HANDLED;
737}
738
739/**
740 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
741 * @irq: interrupt number
742 * @_di: void pointer that has to address of ab8500_btemp
743 *
744 * Returns IRQ status(IRQ_HANDLED)
745 */
746static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
747{
748 struct ab8500_btemp *di = _di;
749
750 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
751
752 di->events.btemp_medhigh = true;
753 di->events.btemp_lowmed = false;
754 di->events.btemp_high = false;
755 di->events.btemp_low = false;
756 power_supply_changed(&di->btemp_psy);
757
758 return IRQ_HANDLED;
759}
760
761/**
762 * ab8500_btemp_periodic() - Periodic temperature measurements
763 * @di: pointer to the ab8500_btemp structure
764 * @enable: enable or disable periodic temperature measurements
765 *
766 * Starts of stops periodic temperature measurements. Periodic measurements
767 * should only be done when a charger is connected.
768 */
769static void ab8500_btemp_periodic(struct ab8500_btemp *di,
770 bool enable)
771{
772 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
773 enable);
774 /*
775 * Make sure a new measurement is done directly by cancelling
776 * any pending work
777 */
778 cancel_delayed_work_sync(&di->btemp_periodic_work);
779
780 if (enable)
781 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
782}
783
784/**
785 * ab8500_btemp_get_temp() - get battery temperature
786 * @di: pointer to the ab8500_btemp structure
787 *
788 * Returns battery temperature
789 */
790static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
791{
792 int temp = 0;
793
794 /*
d36e3e6d 795 * The BTEMP events are not reliabe on AB8500 cut3.3
1f855824
AM
796 * and prior versions
797 */
d36e3e6d 798 if (is_ab8500_3p3_or_earlier(di->parent)) {
1f855824
AM
799 temp = di->bat_temp * 10;
800 } else {
801 if (di->events.btemp_low) {
802 if (temp > di->btemp_ranges.btemp_low_limit)
5b41aa9f 803 temp = di->btemp_ranges.btemp_low_limit * 10;
1f855824
AM
804 else
805 temp = di->bat_temp * 10;
806 } else if (di->events.btemp_high) {
807 if (temp < di->btemp_ranges.btemp_high_limit)
5b41aa9f 808 temp = di->btemp_ranges.btemp_high_limit * 10;
1f855824
AM
809 else
810 temp = di->bat_temp * 10;
811 } else if (di->events.btemp_lowmed) {
812 if (temp > di->btemp_ranges.btemp_med_limit)
5b41aa9f 813 temp = di->btemp_ranges.btemp_med_limit * 10;
1f855824
AM
814 else
815 temp = di->bat_temp * 10;
816 } else if (di->events.btemp_medhigh) {
817 if (temp < di->btemp_ranges.btemp_med_limit)
5b41aa9f 818 temp = di->btemp_ranges.btemp_med_limit * 10;
1f855824
AM
819 else
820 temp = di->bat_temp * 10;
821 } else
822 temp = di->bat_temp * 10;
823 }
824 return temp;
825}
826
827/**
828 * ab8500_btemp_get_batctrl_temp() - get the temperature
829 * @btemp: pointer to the btemp structure
830 *
831 * Returns the batctrl temperature in millidegrees
832 */
833int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
834{
835 return btemp->bat_temp * 1000;
836}
837
838/**
839 * ab8500_btemp_get_property() - get the btemp properties
840 * @psy: pointer to the power_supply structure
841 * @psp: pointer to the power_supply_property structure
842 * @val: pointer to the power_supply_propval union
843 *
844 * This function gets called when an application tries to get the btemp
845 * properties by reading the sysfs files.
846 * online: presence of the battery
847 * present: presence of the battery
848 * technology: battery technology
849 * temp: battery temperature
850 * Returns error code in case of failure else 0(on success)
851 */
852static int ab8500_btemp_get_property(struct power_supply *psy,
853 enum power_supply_property psp,
854 union power_supply_propval *val)
855{
856 struct ab8500_btemp *di;
857
858 di = to_ab8500_btemp_device_info(psy);
859
860 switch (psp) {
861 case POWER_SUPPLY_PROP_PRESENT:
862 case POWER_SUPPLY_PROP_ONLINE:
863 if (di->events.batt_rem)
864 val->intval = 0;
865 else
866 val->intval = 1;
867 break;
868 case POWER_SUPPLY_PROP_TECHNOLOGY:
b0284de0 869 val->intval = di->bm->bat_type[di->bm->batt_id].name;
1f855824
AM
870 break;
871 case POWER_SUPPLY_PROP_TEMP:
872 val->intval = ab8500_btemp_get_temp(di);
873 break;
874 default:
875 return -EINVAL;
876 }
877 return 0;
878}
879
880static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
881{
882 struct power_supply *psy;
883 struct power_supply *ext;
884 struct ab8500_btemp *di;
885 union power_supply_propval ret;
886 int i, j;
887 bool psy_found = false;
888
889 psy = (struct power_supply *)data;
890 ext = dev_get_drvdata(dev);
891 di = to_ab8500_btemp_device_info(psy);
892
893 /*
894 * For all psy where the name of your driver
895 * appears in any supplied_to
896 */
897 for (i = 0; i < ext->num_supplicants; i++) {
898 if (!strcmp(ext->supplied_to[i], psy->name))
899 psy_found = true;
900 }
901
902 if (!psy_found)
903 return 0;
904
905 /* Go through all properties for the psy */
906 for (j = 0; j < ext->num_properties; j++) {
907 enum power_supply_property prop;
908 prop = ext->properties[j];
909
910 if (ext->get_property(ext, prop, &ret))
911 continue;
912
913 switch (prop) {
914 case POWER_SUPPLY_PROP_PRESENT:
915 switch (ext->type) {
916 case POWER_SUPPLY_TYPE_MAINS:
917 /* AC disconnected */
918 if (!ret.intval && di->events.ac_conn) {
919 di->events.ac_conn = false;
920 }
921 /* AC connected */
922 else if (ret.intval && !di->events.ac_conn) {
923 di->events.ac_conn = true;
924 if (!di->events.usb_conn)
925 ab8500_btemp_periodic(di, true);
926 }
927 break;
928 case POWER_SUPPLY_TYPE_USB:
929 /* USB disconnected */
930 if (!ret.intval && di->events.usb_conn) {
931 di->events.usb_conn = false;
932 }
933 /* USB connected */
934 else if (ret.intval && !di->events.usb_conn) {
935 di->events.usb_conn = true;
936 if (!di->events.ac_conn)
937 ab8500_btemp_periodic(di, true);
938 }
939 break;
940 default:
941 break;
942 }
943 break;
944 default:
945 break;
946 }
947 }
948 return 0;
949}
950
951/**
952 * ab8500_btemp_external_power_changed() - callback for power supply changes
953 * @psy: pointer to the structure power_supply
954 *
955 * This function is pointing to the function pointer external_power_changed
956 * of the structure power_supply.
957 * This function gets executed when there is a change in the external power
958 * supply to the btemp.
959 */
960static void ab8500_btemp_external_power_changed(struct power_supply *psy)
961{
962 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
963
964 class_for_each_device(power_supply_class, NULL,
965 &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
966}
967
968/* ab8500 btemp driver interrupts and their respective isr */
969static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
970 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
971 {"BTEMP_LOW", ab8500_btemp_templow_handler},
972 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
973 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
974 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
975};
976
977#if defined(CONFIG_PM)
978static int ab8500_btemp_resume(struct platform_device *pdev)
979{
980 struct ab8500_btemp *di = platform_get_drvdata(pdev);
981
982 ab8500_btemp_periodic(di, true);
983
984 return 0;
985}
986
987static int ab8500_btemp_suspend(struct platform_device *pdev,
988 pm_message_t state)
989{
990 struct ab8500_btemp *di = platform_get_drvdata(pdev);
991
992 ab8500_btemp_periodic(di, false);
993
994 return 0;
995}
996#else
997#define ab8500_btemp_suspend NULL
998#define ab8500_btemp_resume NULL
999#endif
1000
415ec69f 1001static int ab8500_btemp_remove(struct platform_device *pdev)
1f855824
AM
1002{
1003 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1004 int i, irq;
1005
1006 /* Disable interrupts */
1007 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1008 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1009 free_irq(irq, di);
1010 }
1011
1012 /* Delete the work queue */
1013 destroy_workqueue(di->btemp_wq);
1014
1015 flush_scheduled_work();
1016 power_supply_unregister(&di->btemp_psy);
1017 platform_set_drvdata(pdev, NULL);
1f855824
AM
1018
1019 return 0;
1020}
1021
bd9e8ab2
R
1022static char *supply_interface[] = {
1023 "ab8500_chargalg",
1024 "ab8500_fg",
1025};
1026
c8afa640 1027static int ab8500_btemp_probe(struct platform_device *pdev)
1f855824 1028{
bd9e8ab2 1029 struct device_node *np = pdev->dev.of_node;
95820245 1030 struct abx500_bm_data *plat = pdev->dev.platform_data;
e0f1abeb 1031 struct ab8500_btemp *di;
1f855824
AM
1032 int irq, i, ret = 0;
1033 u8 val;
2aac3de1 1034
bd9e8ab2
R
1035 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1036 if (!di) {
1037 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
1f855824 1038 return -ENOMEM;
bd9e8ab2 1039 }
95820245
LJ
1040
1041 if (!plat) {
1042 dev_err(&pdev->dev, "no battery management data supplied\n");
1043 return -EINVAL;
1044 }
1045 di->bm = plat;
1046
1047 if (np) {
1048 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1049 if (ret) {
1050 dev_err(&pdev->dev, "failed to get battery information\n");
1051 return ret;
bd9e8ab2 1052 }
bd9e8ab2 1053 }
1f855824
AM
1054
1055 /* get parent data */
1056 di->dev = &pdev->dev;
1057 di->parent = dev_get_drvdata(pdev->dev.parent);
1058 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1059
f6271b4f
JA
1060 di->initialized = false;
1061
1f855824
AM
1062 /* BTEMP supply */
1063 di->btemp_psy.name = "ab8500_btemp";
1064 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1065 di->btemp_psy.properties = ab8500_btemp_props;
1066 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
1067 di->btemp_psy.get_property = ab8500_btemp_get_property;
bd9e8ab2
R
1068 di->btemp_psy.supplied_to = supply_interface;
1069 di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
1f855824
AM
1070 di->btemp_psy.external_power_changed =
1071 ab8500_btemp_external_power_changed;
1072
1073
1074 /* Create a work queue for the btemp */
1075 di->btemp_wq =
1076 create_singlethread_workqueue("ab8500_btemp_wq");
1077 if (di->btemp_wq == NULL) {
1078 dev_err(di->dev, "failed to create work queue\n");
bd9e8ab2 1079 return -ENOMEM;
1f855824
AM
1080 }
1081
1082 /* Init work for measuring temperature periodically */
203b42f7 1083 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1f855824
AM
1084 ab8500_btemp_periodic_work);
1085
1f855824 1086 /* Set BTEMP thermal limits. Low and Med are fixed */
0cdaf9a9
HB
1087 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1088 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1f855824
AM
1089
1090 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1091 AB8500_BTEMP_HIGH_TH, &val);
1092 if (ret < 0) {
1093 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1094 goto free_btemp_wq;
1095 }
1096 switch (val) {
1097 case BTEMP_HIGH_TH_57_0:
1098 case BTEMP_HIGH_TH_57_1:
1099 di->btemp_ranges.btemp_high_limit =
1100 BTEMP_THERMAL_HIGH_LIMIT_57;
1101 break;
1102 case BTEMP_HIGH_TH_52:
1103 di->btemp_ranges.btemp_high_limit =
1104 BTEMP_THERMAL_HIGH_LIMIT_52;
1105 break;
1106 case BTEMP_HIGH_TH_62:
1107 di->btemp_ranges.btemp_high_limit =
1108 BTEMP_THERMAL_HIGH_LIMIT_62;
1109 break;
1110 }
1111
1112 /* Register BTEMP power supply class */
1113 ret = power_supply_register(di->dev, &di->btemp_psy);
1114 if (ret) {
1115 dev_err(di->dev, "failed to register BTEMP psy\n");
1116 goto free_btemp_wq;
1117 }
1118
1119 /* Register interrupts */
1120 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1121 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1122 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1123 IRQF_SHARED | IRQF_NO_SUSPEND,
1124 ab8500_btemp_irq[i].name, di);
1125
1126 if (ret) {
1127 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1128 , ab8500_btemp_irq[i].name, irq, ret);
1129 goto free_irq;
1130 }
1131 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1132 ab8500_btemp_irq[i].name, irq, ret);
1133 }
1134
1135 platform_set_drvdata(pdev, di);
1136
1137 /* Kick off periodic temperature measurements */
1138 ab8500_btemp_periodic(di, true);
1139 list_add_tail(&di->node, &ab8500_btemp_list);
1140
1141 return ret;
1142
1143free_irq:
1144 power_supply_unregister(&di->btemp_psy);
1145
1146 /* We also have to free all successfully registered irqs */
1147 for (i = i - 1; i >= 0; i--) {
1148 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1149 free_irq(irq, di);
1150 }
1151free_btemp_wq:
1152 destroy_workqueue(di->btemp_wq);
1f855824
AM
1153 return ret;
1154}
1155
bd9e8ab2
R
1156static const struct of_device_id ab8500_btemp_match[] = {
1157 { .compatible = "stericsson,ab8500-btemp", },
1158 { },
1159};
1160
1f855824
AM
1161static struct platform_driver ab8500_btemp_driver = {
1162 .probe = ab8500_btemp_probe,
28ea73f4 1163 .remove = ab8500_btemp_remove,
1f855824
AM
1164 .suspend = ab8500_btemp_suspend,
1165 .resume = ab8500_btemp_resume,
1166 .driver = {
1167 .name = "ab8500-btemp",
1168 .owner = THIS_MODULE,
bd9e8ab2 1169 .of_match_table = ab8500_btemp_match,
1f855824
AM
1170 },
1171};
1172
1173static int __init ab8500_btemp_init(void)
1174{
1175 return platform_driver_register(&ab8500_btemp_driver);
1176}
1177
1178static void __exit ab8500_btemp_exit(void)
1179{
1180 platform_driver_unregister(&ab8500_btemp_driver);
1181}
1182
eeb0751c 1183device_initcall(ab8500_btemp_init);
1f855824
AM
1184module_exit(ab8500_btemp_exit);
1185
1186MODULE_LICENSE("GPL v2");
1187MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1188MODULE_ALIAS("platform:ab8500-btemp");
1189MODULE_DESCRIPTION("AB8500 battery temperature driver");
This page took 0.126407 seconds and 5 git commands to generate.