blob: f7a35ebfbab29cf329312bd4b7315bdd0b2b7ec6 [file] [log] [blame]
Arun Murthy1f855822012-02-29 21:54:28 +05301/*
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>
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -080023#include <linux/jiffies.h>
24#include <linux/of.h>
25#include <linux/mfd/core.h>
Arun Murthy1f855822012-02-29 21:54:28 +053026#include <linux/mfd/abx500.h>
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -080027#include <linux/mfd/abx500/ab8500.h>
Arun Murthy1f855822012-02-29 21:54:28 +053028#include <linux/mfd/abx500/ab8500-bm.h>
29#include <linux/mfd/abx500/ab8500-gpadc.h>
Arun Murthy1f855822012-02-29 21:54:28 +053030
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
Michel JAOUENe6aac612012-05-22 15:46:46 +020042#define BTEMP_BATCTRL_CURR_SRC_16UA 16
43#define BTEMP_BATCTRL_CURR_SRC_18UA 18
44
Lee Jones861a30d2012-08-29 20:36:51 +080045#define BTEMP_BATCTRL_CURR_SRC_60UA 60
46#define BTEMP_BATCTRL_CURR_SRC_120UA 120
47
Arun Murthy1f855822012-02-29 21:54:28 +053048/**
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
Colin Ian Kingc56ca242016-12-28 21:59:26 +000079 * @bat_temp: Dispatched battery temperature in degree Celsius
80 * @prev_bat_temp Last measured battery temperature in degree Celsius
Arun Murthy1f855822012-02-29 21:54:28 +053081 * @parent: Pointer to the struct ab8500
82 * @gpadc: Pointer to the struct gpadc
83 * @fg: Pointer to the struct fg
Lee Jonesb0284de2012-11-30 10:09:42 +000084 * @bm: Platform specific battery management information
Arun Murthy1f855822012-02-29 21:54:28 +053085 * @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
Jonas Aabergf6271b42012-02-27 11:02:44 +010090 * @initialized: True if battery id read.
Arun Murthy1f855822012-02-29 21:54:28 +053091 */
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;
Lee Jonesb0284de2012-11-30 10:09:42 +0000101 struct abx500_bm_data *bm;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100102 struct power_supply *btemp_psy;
Arun Murthy1f855822012-02-29 21:54:28 +0530103 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;
Jonas Aabergf6271b42012-02-27 11:02:44 +0100107 bool initialized;
Arun Murthy1f855822012-02-29 21:54:28 +0530108};
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{
Gustavo A. R. Silva33237fb2017-01-11 20:29:35 -0600126 return list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
Arun Murthy1f855822012-02-29 21:54:28 +0530127}
Hongbo Zhang6f780962013-04-03 20:18:08 +0800128EXPORT_SYMBOL(ab8500_btemp_get);
Arun Murthy1f855822012-02-29 21:54:28 +0530129
130/**
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
135 *
136 * This function returns the battery resistance that is
137 * derived from the BATCTRL voltage.
138 * Returns value in Ohms.
139 */
140static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
141 int v_batctrl, int inst_curr)
142{
143 int rbs;
144
145 if (is_ab8500_1p1_or_earlier(di->parent)) {
146 /*
147 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
148 * connected to 1.8V through a 450k resistor
149 */
150 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
151 }
152
Lee Jonesb0284de2012-11-30 10:09:42 +0000153 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
Arun Murthy1f855822012-02-29 21:54:28 +0530154 /*
155 * If the battery has internal NTC, we use the current
Marcus Cooper7a2cf9b2012-11-28 14:42:59 +0100156 * source to calculate the resistance.
Arun Murthy1f855822012-02-29 21:54:28 +0530157 */
158 rbs = (v_batctrl * 1000
Lee Jonesb0284de2012-11-30 10:09:42 +0000159 - di->bm->gnd_lift_resistance * inst_curr)
Arun Murthy1f855822012-02-29 21:54:28 +0530160 / di->curr_source;
161 } else {
162 /*
163 * BAT_CTRL is internally
164 * connected to 1.8V through a 80k resistor
165 */
166 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
167 }
168
169 return rbs;
170}
171
172/**
173 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
174 * @di: pointer to the ab8500_btemp structure
175 *
176 * This function returns the voltage on BATCTRL. Returns value in mV.
177 */
178static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
179{
180 int vbtemp;
181 static int prev;
182
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;
189 }
190 prev = vbtemp;
191 return vbtemp;
192}
193
194/**
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
198 *
199 * Enable or disable the current sources for the BatCtrl AD channel
200 */
201static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
202 bool enable)
203{
204 int curr;
205 int ret = 0;
206
207 /*
208 * BATCTRL current sources are included on AB8500 cut2.0
209 * and future versions
210 */
211 if (is_ab8500_1p1_or_earlier(di->parent))
212 return 0;
213
214 /* Only do this for batteries with internal NTC */
Lee Jonesb0284de2012-11-30 10:09:42 +0000215 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
Michel JAOUENe6aac612012-05-22 15:46:46 +0200216
Lee Jones861a30d2012-08-29 20:36:51 +0800217 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)) {
Michel JAOUENe6aac612012-05-22 15:46:46 +0200223 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;
232 }
Arun Murthy1f855822012-02-29 21:54:28 +0530233
234 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
235
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;
243 }
244
245 /*
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
249 */
250 udelay(32);
251
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;
259 }
Lee Jonesb0284de2012-11-30 10:09:42 +0000260 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
Arun Murthy1f855822012-02-29 21:54:28 +0530261 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
262
Lee Jones861a30d2012-08-29 20:36:51 +0800263 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)) {
Michel JAOUENe6aac612012-05-22 15:46:46 +0200271 /* 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));
284 }
285
Arun Murthy1f855822012-02-29 21:54:28 +0530286 if (ret) {
287 dev_err(di->dev, "%s failed disabling current source\n",
288 __func__);
289 goto disable_curr_source;
290 }
291
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;
301 }
302
303 /*
304 * We have to wait one 32kHz cycle before disabling
305 * ForceBatCtrlCmpHigh since this needs to be written
306 * in a separate cycle
307 */
308 udelay(32);
309
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;
318 }
319 }
320 return ret;
321
322 /*
323 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
324 * if we got an error above
325 */
326disable_curr_source:
Lee Jones861a30d2012-08-29 20:36:51 +0800327 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)) {
Michel JAOUENe6aac612012-05-22 15:46:46 +0200334 /* 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,
Arun Murthy1f855822012-02-29 21:54:28 +0530342 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));
Michel JAOUENe6aac612012-05-22 15:46:46 +0200345 }
346
Arun Murthy1f855822012-02-29 21:54:28 +0530347 if (ret) {
348 dev_err(di->dev, "%s failed disabling current source\n",
349 __func__);
350 return ret;
351 }
352enable_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;
362 }
363
364disable_force_comp:
365 /*
366 * We have to wait one 32kHz cycle before disabling
367 * ForceBatCtrlCmpHigh since this needs to be written
368 * in a separate cycle
369 */
370 udelay(32);
371
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;
380 }
381
382 return ret;
383}
384
385/**
386 * ab8500_btemp_get_batctrl_res() - get battery resistance
387 * @di: pointer to the ab8500_btemp structure
388 *
389 * This function returns the battery pack identification resistance.
390 * Returns value in Ohms.
391 */
392static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
393{
394 int ret;
395 int batctrl = 0;
396 int res;
397 int inst_curr;
398 int i;
399
400 /*
401 * BATCTRL current sources are included on AB8500 cut2.0
402 * and future versions
403 */
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;
408 }
409
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;
415 }
416
417 ret = ab8500_fg_inst_curr_start(di->fg);
418
419 if (ret) {
420 dev_err(di->dev, "Failed to start current measurement\n");
421 return ret;
422 }
423
Johan Bjornstedt3988a4d2013-01-11 13:12:50 +0000424 do {
425 msleep(20);
426 } while (!ab8500_fg_inst_curr_started(di->fg));
427
Arun Murthy1f855822012-02-29 21:54:28 +0530428 i = 0;
429
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;
436
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;
441 }
442
443 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
444
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;
449 }
450
451 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
452 __func__, batctrl, res, inst_curr, i);
453
454 return res;
455}
456
457/**
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
463 *
Colin Ian Kingc56ca242016-12-28 21:59:26 +0000464 * This function returns the battery temperature in degrees Celsius
Arun Murthy1f855822012-02-29 21:54:28 +0530465 * based on the NTC resistance.
466 */
467static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
468 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
469{
Gustavo A. R. Silva33237fb2017-01-11 20:29:35 -0600470 int i;
Arun Murthy1f855822012-02-29 21:54:28 +0530471 /*
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.
476 */
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++;
486 }
487
Gustavo A. R. Silva33237fb2017-01-11 20:29:35 -0600488 return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
Arun Murthy1f855822012-02-29 21:54:28 +0530489 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
Arun Murthy1f855822012-02-29 21:54:28 +0530490}
491
492/**
493 * ab8500_btemp_measure_temp() - measure battery temperature
494 * @di: pointer to the ab8500_btemp structure
495 *
496 * Returns battery temperature (on success) else the previous temperature
497 */
498static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
499{
500 int temp;
501 static int prev;
502 int rbat, rntc, vntc;
503 u8 id;
504
Lee Jonesb0284de2012-11-30 10:09:42 +0000505 id = di->bm->batt_id;
Arun Murthy1f855822012-02-29 21:54:28 +0530506
Lee Jonesb0284de2012-11-30 10:09:42 +0000507 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
Arun Murthy1f855822012-02-29 21:54:28 +0530508 id != BATTERY_UNKNOWN) {
509
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__);
514 /*
515 * Return out-of-range temperature so that
516 * charging is stopped
517 */
518 return BTEMP_THERMAL_LOW_LIMIT;
519 }
520
521 temp = ab8500_btemp_res_to_temp(di,
Lee Jonesb0284de2012-11-30 10:09:42 +0000522 di->bm->bat_type[id].r_to_t_tbl,
523 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
Arun Murthy1f855822012-02-29 21:54:28 +0530524 } 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;
531 }
532 /*
533 * The PCB NTC is sourced from VTVOUT via a 230kOhm
534 * resistor.
535 */
536 rntc = 230000 * vntc / (VTVOUT_V - vntc);
537
538 temp = ab8500_btemp_res_to_temp(di,
Lee Jonesb0284de2012-11-30 10:09:42 +0000539 di->bm->bat_type[id].r_to_t_tbl,
540 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
Arun Murthy1f855822012-02-29 21:54:28 +0530541 prev = temp;
542 }
543 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
544 return temp;
545}
546
547/**
548 * ab8500_btemp_id() - Identify the connected battery
549 * @di: pointer to the ab8500_btemp structure
550 *
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.
554 */
555static int ab8500_btemp_id(struct ab8500_btemp *di)
556{
557 int res;
558 u8 i;
Lee Jones861a30d2012-08-29 20:36:51 +0800559 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))
Michel JAOUENe6aac612012-05-22 15:46:46 +0200562 di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
563 else
564 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
Arun Murthy1f855822012-02-29 21:54:28 +0530565
Lee Jonesb0284de2012-11-30 10:09:42 +0000566 di->bm->batt_id = BATTERY_UNKNOWN;
Arun Murthy1f855822012-02-29 21:54:28 +0530567
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;
572 }
573
574 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
Lee Jonesb0284de2012-11-30 10:09:42 +0000575 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)) {
Arun Murthy1f855822012-02-29 21:54:28 +0530578 dev_dbg(di->dev, "Battery detected on %s"
579 " low %d < res %d < high: %d"
580 " index: %d\n",
Lee Jonesb0284de2012-11-30 10:09:42 +0000581 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
Arun Murthy1f855822012-02-29 21:54:28 +0530582 "BATCTRL" : "BATTEMP",
Lee Jonesb0284de2012-11-30 10:09:42 +0000583 di->bm->bat_type[i].resis_low, res,
584 di->bm->bat_type[i].resis_high, i);
Arun Murthy1f855822012-02-29 21:54:28 +0530585
Lee Jonesb0284de2012-11-30 10:09:42 +0000586 di->bm->batt_id = i;
Arun Murthy1f855822012-02-29 21:54:28 +0530587 break;
588 }
589 }
590
Lee Jonesb0284de2012-11-30 10:09:42 +0000591 if (di->bm->batt_id == BATTERY_UNKNOWN) {
Arun Murthy1f855822012-02-29 21:54:28 +0530592 dev_warn(di->dev, "Battery identified as unknown"
593 ", resistance %d Ohm\n", res);
594 return -ENXIO;
595 }
596
597 /*
598 * We only have to change current source if the
Marcus Cooper7a2cf9b2012-11-28 14:42:59 +0100599 * detected type is Type 1.
Arun Murthy1f855822012-02-29 21:54:28 +0530600 */
Lee Jonesb0284de2012-11-30 10:09:42 +0000601 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
Lee Jones861a30d2012-08-29 20:36:51 +0800602 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");
Michel JAOUENe6aac612012-05-22 15:46:46 +0200610 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;
614 }
Arun Murthy1f855822012-02-29 21:54:28 +0530615 }
616
Lee Jonesb0284de2012-11-30 10:09:42 +0000617 return di->bm->batt_id;
Arun Murthy1f855822012-02-29 21:54:28 +0530618}
619
620/**
621 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
622 * @work: pointer to the work_struct structure
623 *
624 * Work function for measuring the temperature periodically
625 */
626static void ab8500_btemp_periodic_work(struct work_struct *work)
627{
628 int interval;
Hakan Berg908fe8d2012-07-17 14:17:16 +0200629 int bat_temp;
Arun Murthy1f855822012-02-29 21:54:28 +0530630 struct ab8500_btemp *di = container_of(work,
631 struct ab8500_btemp, btemp_periodic_work.work);
632
Jonas Aabergf6271b42012-02-27 11:02:44 +0100633 if (!di->initialized) {
Jonas Aabergf6271b42012-02-27 11:02:44 +0100634 /* Identify the battery */
635 if (ab8500_btemp_id(di) < 0)
636 dev_warn(di->dev, "failed to identify the battery\n");
637 }
638
Hakan Berg908fe8d2012-07-17 14:17:16 +0200639 bat_temp = ab8500_btemp_measure_temp(di);
640 /*
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.
645 */
Rupesh Kumar2c4c40a2012-08-22 15:14:22 +0530646 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
647 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
648 di->initialized = true;
Hakan Berg908fe8d2012-07-17 14:17:16 +0200649 di->bat_temp = bat_temp;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100650 power_supply_changed(di->btemp_psy);
Hakan Berg908fe8d2012-07-17 14:17:16 +0200651 }
652 } else if (bat_temp < di->prev_bat_temp) {
653 di->bat_temp--;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100654 power_supply_changed(di->btemp_psy);
Hakan Berg908fe8d2012-07-17 14:17:16 +0200655 } else if (bat_temp > di->prev_bat_temp) {
656 di->bat_temp++;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100657 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530658 }
Hakan Berg908fe8d2012-07-17 14:17:16 +0200659 di->prev_bat_temp = bat_temp;
Arun Murthy1f855822012-02-29 21:54:28 +0530660
661 if (di->events.ac_conn || di->events.usb_conn)
Lee Jonesb0284de2012-11-30 10:09:42 +0000662 interval = di->bm->temp_interval_chg;
Arun Murthy1f855822012-02-29 21:54:28 +0530663 else
Lee Jonesb0284de2012-11-30 10:09:42 +0000664 interval = di->bm->temp_interval_nochg;
Arun Murthy1f855822012-02-29 21:54:28 +0530665
666 /* Schedule a new measurement */
667 queue_delayed_work(di->btemp_wq,
668 &di->btemp_periodic_work,
669 round_jiffies(interval * HZ));
670}
671
672/**
673 * ab8500_btemp_batctrlindb_handler() - battery removal detected
674 * @irq: interrupt number
675 * @_di: void pointer that has to address of ab8500_btemp
676 *
677 * Returns IRQ status(IRQ_HANDLED)
678 */
679static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
680{
681 struct ab8500_btemp *di = _di;
682 dev_err(di->dev, "Battery removal detected!\n");
683
684 di->events.batt_rem = true;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100685 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530686
687 return IRQ_HANDLED;
688}
689
690/**
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
694 *
695 * Returns IRQ status(IRQ_HANDLED)
696 */
697static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
698{
699 struct ab8500_btemp *di = _di;
700
Hakan Bergd36e3e62013-01-11 13:12:57 +0000701 if (is_ab8500_3p3_or_earlier(di->parent)) {
Arun Murthy1f855822012-02-29 21:54:28 +0530702 dev_dbg(di->dev, "Ignore false btemp low irq"
Hakan Bergd36e3e62013-01-11 13:12:57 +0000703 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
Arun Murthy1f855822012-02-29 21:54:28 +0530704 } else {
705 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
706
707 di->events.btemp_low = true;
708 di->events.btemp_high = false;
709 di->events.btemp_medhigh = false;
710 di->events.btemp_lowmed = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100711 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530712 }
713
714 return IRQ_HANDLED;
715}
716
717/**
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
721 *
722 * Returns IRQ status(IRQ_HANDLED)
723 */
724static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
725{
726 struct ab8500_btemp *di = _di;
727
728 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
729
730 di->events.btemp_high = true;
731 di->events.btemp_medhigh = false;
732 di->events.btemp_lowmed = false;
733 di->events.btemp_low = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100734 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530735
736 return IRQ_HANDLED;
737}
738
739/**
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
743 *
744 * Returns IRQ status(IRQ_HANDLED)
745 */
746static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
747{
748 struct ab8500_btemp *di = _di;
749
750 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
751
752 di->events.btemp_lowmed = true;
753 di->events.btemp_medhigh = false;
754 di->events.btemp_high = false;
755 di->events.btemp_low = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100756 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530757
758 return IRQ_HANDLED;
759}
760
761/**
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
765 *
766 * Returns IRQ status(IRQ_HANDLED)
767 */
768static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
769{
770 struct ab8500_btemp *di = _di;
771
772 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
773
774 di->events.btemp_medhigh = true;
775 di->events.btemp_lowmed = false;
776 di->events.btemp_high = false;
777 di->events.btemp_low = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100778 power_supply_changed(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530779
780 return IRQ_HANDLED;
781}
782
783/**
784 * ab8500_btemp_periodic() - Periodic temperature measurements
785 * @di: pointer to the ab8500_btemp structure
786 * @enable: enable or disable periodic temperature measurements
787 *
788 * Starts of stops periodic temperature measurements. Periodic measurements
789 * should only be done when a charger is connected.
790 */
791static void ab8500_btemp_periodic(struct ab8500_btemp *di,
792 bool enable)
793{
794 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
795 enable);
796 /*
797 * Make sure a new measurement is done directly by cancelling
798 * any pending work
799 */
800 cancel_delayed_work_sync(&di->btemp_periodic_work);
801
802 if (enable)
803 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
804}
805
806/**
807 * ab8500_btemp_get_temp() - get battery temperature
808 * @di: pointer to the ab8500_btemp structure
809 *
810 * Returns battery temperature
811 */
Hongbo Zhang6f780962013-04-03 20:18:08 +0800812int ab8500_btemp_get_temp(struct ab8500_btemp *di)
Arun Murthy1f855822012-02-29 21:54:28 +0530813{
814 int temp = 0;
815
816 /*
Hakan Bergd36e3e62013-01-11 13:12:57 +0000817 * The BTEMP events are not reliabe on AB8500 cut3.3
Arun Murthy1f855822012-02-29 21:54:28 +0530818 * and prior versions
819 */
Hakan Bergd36e3e62013-01-11 13:12:57 +0000820 if (is_ab8500_3p3_or_earlier(di->parent)) {
Arun Murthy1f855822012-02-29 21:54:28 +0530821 temp = di->bat_temp * 10;
822 } else {
823 if (di->events.btemp_low) {
824 if (temp > di->btemp_ranges.btemp_low_limit)
Lee Jones5b41aa92013-01-11 13:12:56 +0000825 temp = di->btemp_ranges.btemp_low_limit * 10;
Arun Murthy1f855822012-02-29 21:54:28 +0530826 else
827 temp = di->bat_temp * 10;
828 } else if (di->events.btemp_high) {
829 if (temp < di->btemp_ranges.btemp_high_limit)
Lee Jones5b41aa92013-01-11 13:12:56 +0000830 temp = di->btemp_ranges.btemp_high_limit * 10;
Arun Murthy1f855822012-02-29 21:54:28 +0530831 else
832 temp = di->bat_temp * 10;
833 } else if (di->events.btemp_lowmed) {
834 if (temp > di->btemp_ranges.btemp_med_limit)
Lee Jones5b41aa92013-01-11 13:12:56 +0000835 temp = di->btemp_ranges.btemp_med_limit * 10;
Arun Murthy1f855822012-02-29 21:54:28 +0530836 else
837 temp = di->bat_temp * 10;
838 } else if (di->events.btemp_medhigh) {
839 if (temp < di->btemp_ranges.btemp_med_limit)
Lee Jones5b41aa92013-01-11 13:12:56 +0000840 temp = di->btemp_ranges.btemp_med_limit * 10;
Arun Murthy1f855822012-02-29 21:54:28 +0530841 else
842 temp = di->bat_temp * 10;
843 } else
844 temp = di->bat_temp * 10;
845 }
846 return temp;
847}
Hongbo Zhang6f780962013-04-03 20:18:08 +0800848EXPORT_SYMBOL(ab8500_btemp_get_temp);
Arun Murthy1f855822012-02-29 21:54:28 +0530849
850/**
851 * ab8500_btemp_get_batctrl_temp() - get the temperature
852 * @btemp: pointer to the btemp structure
853 *
854 * Returns the batctrl temperature in millidegrees
855 */
856int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
857{
858 return btemp->bat_temp * 1000;
859}
Hongbo Zhang6f780962013-04-03 20:18:08 +0800860EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
Arun Murthy1f855822012-02-29 21:54:28 +0530861
862/**
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
867 *
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)
875 */
876static int ab8500_btemp_get_property(struct power_supply *psy,
877 enum power_supply_property psp,
878 union power_supply_propval *val)
879{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100880 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530881
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:
Lee Jonesb0284de2012-11-30 10:09:42 +0000891 val->intval = di->bm->bat_type[di->bm->batt_id].name;
Arun Murthy1f855822012-02-29 21:54:28 +0530892 break;
893 case POWER_SUPPLY_PROP_TEMP:
894 val->intval = ab8500_btemp_get_temp(di);
895 break;
896 default:
897 return -EINVAL;
898 }
899 return 0;
900}
901
902static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
903{
904 struct power_supply *psy;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700905 struct power_supply *ext = dev_get_drvdata(dev);
906 const char **supplicants = (const char **)ext->supplied_to;
Arun Murthy1f855822012-02-29 21:54:28 +0530907 struct ab8500_btemp *di;
908 union power_supply_propval ret;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700909 int j;
Arun Murthy1f855822012-02-29 21:54:28 +0530910
911 psy = (struct power_supply *)data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100912 di = power_supply_get_drvdata(psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530913
914 /*
915 * For all psy where the name of your driver
916 * appears in any supplied_to
917 */
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700918 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
919 if (j < 0)
Arun Murthy1f855822012-02-29 21:54:28 +0530920 return 0;
921
922 /* Go through all properties for the psy */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100923 for (j = 0; j < ext->desc->num_properties; j++) {
Arun Murthy1f855822012-02-29 21:54:28 +0530924 enum power_supply_property prop;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100925 prop = ext->desc->properties[j];
Arun Murthy1f855822012-02-29 21:54:28 +0530926
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +0100927 if (power_supply_get_property(ext, prop, &ret))
Arun Murthy1f855822012-02-29 21:54:28 +0530928 continue;
929
930 switch (prop) {
931 case POWER_SUPPLY_PROP_PRESENT:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100932 switch (ext->desc->type) {
Arun Murthy1f855822012-02-29 21:54:28 +0530933 case POWER_SUPPLY_TYPE_MAINS:
934 /* AC disconnected */
935 if (!ret.intval && di->events.ac_conn) {
936 di->events.ac_conn = false;
937 }
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);
943 }
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;
949 }
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);
955 }
956 break;
957 default:
958 break;
959 }
960 break;
961 default:
962 break;
963 }
964 }
965 return 0;
966}
967
968/**
969 * ab8500_btemp_external_power_changed() - callback for power supply changes
970 * @psy: pointer to the structure power_supply
971 *
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.
976 */
977static void ab8500_btemp_external_power_changed(struct power_supply *psy)
978{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100979 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
Arun Murthy1f855822012-02-29 21:54:28 +0530980
981 class_for_each_device(power_supply_class, NULL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100982 di->btemp_psy, ab8500_btemp_get_ext_psy_data);
Arun Murthy1f855822012-02-29 21:54:28 +0530983}
984
985/* ab8500 btemp driver interrupts and their respective isr */
986static 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},
992};
993
994#if defined(CONFIG_PM)
995static int ab8500_btemp_resume(struct platform_device *pdev)
996{
997 struct ab8500_btemp *di = platform_get_drvdata(pdev);
998
999 ab8500_btemp_periodic(di, true);
1000
1001 return 0;
1002}
1003
1004static int ab8500_btemp_suspend(struct platform_device *pdev,
1005 pm_message_t state)
1006{
1007 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1008
1009 ab8500_btemp_periodic(di, false);
1010
1011 return 0;
1012}
1013#else
1014#define ab8500_btemp_suspend NULL
1015#define ab8500_btemp_resume NULL
1016#endif
1017
Bill Pemberton415ec692012-11-19 13:26:07 -05001018static int ab8500_btemp_remove(struct platform_device *pdev)
Arun Murthy1f855822012-02-29 21:54:28 +05301019{
1020 struct ab8500_btemp *di = platform_get_drvdata(pdev);
1021 int i, irq;
1022
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);
1027 }
1028
1029 /* Delete the work queue */
1030 destroy_workqueue(di->btemp_wq);
1031
1032 flush_scheduled_work();
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001033 power_supply_unregister(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +05301034
1035 return 0;
1036}
1037
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001038static char *supply_interface[] = {
1039 "ab8500_chargalg",
1040 "ab8500_fg",
1041};
1042
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001043static 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,
1050};
1051
Bill Pembertonc8afa642012-11-19 13:22:23 -05001052static int ab8500_btemp_probe(struct platform_device *pdev)
Arun Murthy1f855822012-02-29 21:54:28 +05301053{
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001054 struct device_node *np = pdev->dev.of_node;
Lee Jones95820242012-11-30 10:48:30 +00001055 struct abx500_bm_data *plat = pdev->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001056 struct power_supply_config psy_cfg = {};
Rajanikanth H.Ve0f1abe2012-11-18 18:45:41 -08001057 struct ab8500_btemp *di;
Arun Murthy1f855822012-02-29 21:54:28 +05301058 int irq, i, ret = 0;
1059 u8 val;
Arun Murthy1f855822012-02-29 21:54:28 +05301060
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001061 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__);
Arun Murthy1f855822012-02-29 21:54:28 +05301064 return -ENOMEM;
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001065 }
Lee Jones95820242012-11-30 10:48:30 +00001066
1067 if (!plat) {
1068 dev_err(&pdev->dev, "no battery management data supplied\n");
1069 return -EINVAL;
1070 }
1071 di->bm = plat;
1072
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;
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001078 }
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001079 }
Arun Murthy1f855822012-02-29 21:54:28 +05301080
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");
1085
Jonas Aabergf6271b42012-02-27 11:02:44 +01001086 di->initialized = false;
1087
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001088 psy_cfg.supplied_to = supply_interface;
1089 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001090 psy_cfg.drv_data = di;
Arun Murthy1f855822012-02-29 21:54:28 +05301091
1092 /* Create a work queue for the btemp */
1093 di->btemp_wq =
Bhaktipriya Shridhara8dd5b62016-08-13 21:47:35 +05301094 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
Arun Murthy1f855822012-02-29 21:54:28 +05301095 if (di->btemp_wq == NULL) {
1096 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001097 return -ENOMEM;
Arun Murthy1f855822012-02-29 21:54:28 +05301098 }
1099
1100 /* Init work for measuring temperature periodically */
Tejun Heo203b42f2012-08-21 13:18:23 -07001101 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
Arun Murthy1f855822012-02-29 21:54:28 +05301102 ab8500_btemp_periodic_work);
1103
Arun Murthy1f855822012-02-29 21:54:28 +05301104 /* Set BTEMP thermal limits. Low and Med are fixed */
Hakan Berg0cdaf9a2013-01-11 13:13:00 +00001105 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1106 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
Arun Murthy1f855822012-02-29 21:54:28 +05301107
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;
1113 }
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;
1128 }
1129
1130 /* Register BTEMP power supply class */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001131 di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
1132 &psy_cfg);
1133 if (IS_ERR(di->btemp_psy)) {
Arun Murthy1f855822012-02-29 21:54:28 +05301134 dev_err(di->dev, "failed to register BTEMP psy\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001135 ret = PTR_ERR(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +05301136 goto free_btemp_wq;
1137 }
1138
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);
1145
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;
1150 }
1151 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1152 ab8500_btemp_irq[i].name, irq, ret);
1153 }
1154
1155 platform_set_drvdata(pdev, di);
1156
1157 /* Kick off periodic temperature measurements */
1158 ab8500_btemp_periodic(di, true);
1159 list_add_tail(&di->node, &ab8500_btemp_list);
1160
1161 return ret;
1162
1163free_irq:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001164 power_supply_unregister(di->btemp_psy);
Arun Murthy1f855822012-02-29 21:54:28 +05301165
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);
1170 }
1171free_btemp_wq:
1172 destroy_workqueue(di->btemp_wq);
Arun Murthy1f855822012-02-29 21:54:28 +05301173 return ret;
1174}
1175
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001176static const struct of_device_id ab8500_btemp_match[] = {
1177 { .compatible = "stericsson,ab8500-btemp", },
1178 { },
1179};
1180
Arun Murthy1f855822012-02-29 21:54:28 +05301181static struct platform_driver ab8500_btemp_driver = {
1182 .probe = ab8500_btemp_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001183 .remove = ab8500_btemp_remove,
Arun Murthy1f855822012-02-29 21:54:28 +05301184 .suspend = ab8500_btemp_suspend,
1185 .resume = ab8500_btemp_resume,
1186 .driver = {
1187 .name = "ab8500-btemp",
Rajanikanth H.Vbd9e8ab2012-11-18 19:16:58 -08001188 .of_match_table = ab8500_btemp_match,
Arun Murthy1f855822012-02-29 21:54:28 +05301189 },
1190};
1191
1192static int __init ab8500_btemp_init(void)
1193{
1194 return platform_driver_register(&ab8500_btemp_driver);
1195}
1196
1197static void __exit ab8500_btemp_exit(void)
1198{
1199 platform_driver_unregister(&ab8500_btemp_driver);
1200}
1201
Rajanikanth H.Veeb07512013-01-23 09:56:45 +05301202device_initcall(ab8500_btemp_init);
Arun Murthy1f855822012-02-29 21:54:28 +05301203module_exit(ab8500_btemp_exit);
1204
1205MODULE_LICENSE("GPL v2");
1206MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1207MODULE_ALIAS("platform:ab8500-btemp");
1208MODULE_DESCRIPTION("AB8500 battery temperature driver");