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