2 * Copyright (C) ST-Ericsson SA 2012
4 * Battery temperature driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/workqueue.h>
23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/mfd/abx500.h>
25 #include <linux/mfd/abx500/ab8500-bm.h>
26 #include <linux/mfd/abx500/ab8500-gpadc.h>
27 #include <linux/jiffies.h>
31 #define BTEMP_THERMAL_LOW_LIMIT -10
32 #define BTEMP_THERMAL_MED_LIMIT 0
33 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
34 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
35 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
37 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
38 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
40 #define to_ab8500_btemp_device_info(x) container_of((x), \
41 struct ab8500_btemp, btemp_psy);
44 * struct ab8500_btemp_interrupts - ab8500 interrupts
45 * @name: name of the interrupt
46 * @isr function pointer to the isr
48 struct ab8500_btemp_interrupts
{
50 irqreturn_t (*isr
)(int irq
, void *data
);
53 struct ab8500_btemp_events
{
63 struct ab8500_btemp_ranges
{
70 * struct ab8500_btemp - ab8500 BTEMP device information
71 * @dev: Pointer to the structure device
72 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
73 * @curr_source: What current source we use, in uA
74 * @bat_temp: Battery temperature in degree Celcius
75 * @prev_bat_temp Last dispatched battery temperature
76 * @parent: Pointer to the struct ab8500
77 * @gpadc: Pointer to the struct gpadc
78 * @fg: Pointer to the struct fg
79 * @pdata: Pointer to the abx500_btemp platform data
80 * @bat: Pointer to the abx500_bm platform data
81 * @btemp_psy: Structure for BTEMP specific battery properties
82 * @events: Structure for information about events triggered
83 * @btemp_ranges: Battery temperature range structure
84 * @btemp_wq: Work queue for measuring the temperature periodically
85 * @btemp_periodic_work: Work for measuring the temperature periodically
89 struct list_head node
;
93 struct ab8500
*parent
;
94 struct ab8500_gpadc
*gpadc
;
96 struct abx500_btemp_platform_data
*pdata
;
97 struct abx500_bm_data
*bat
;
98 struct power_supply btemp_psy
;
99 struct ab8500_btemp_events events
;
100 struct ab8500_btemp_ranges btemp_ranges
;
101 struct workqueue_struct
*btemp_wq
;
102 struct delayed_work btemp_periodic_work
;
105 /* BTEMP power supply properties */
106 static enum power_supply_property ab8500_btemp_props
[] = {
107 POWER_SUPPLY_PROP_PRESENT
,
108 POWER_SUPPLY_PROP_ONLINE
,
109 POWER_SUPPLY_PROP_TECHNOLOGY
,
110 POWER_SUPPLY_PROP_TEMP
,
113 static LIST_HEAD(ab8500_btemp_list
);
116 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
117 * (i.e. the first BTEMP in the instance list)
119 struct ab8500_btemp
*ab8500_btemp_get(void)
121 struct ab8500_btemp
*btemp
;
122 btemp
= list_first_entry(&ab8500_btemp_list
, struct ab8500_btemp
, node
);
128 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
129 * @di: pointer to the ab8500_btemp structure
130 * @v_batctrl: measured batctrl voltage
131 * @inst_curr: measured instant current
133 * This function returns the battery resistance that is
134 * derived from the BATCTRL voltage.
135 * Returns value in Ohms.
137 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp
*di
,
138 int v_batctrl
, int inst_curr
)
142 if (is_ab8500_1p1_or_earlier(di
->parent
)) {
144 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
145 * connected to 1.8V through a 450k resistor
147 return (450000 * (v_batctrl
)) / (1800 - v_batctrl
);
150 if (di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
) {
152 * If the battery has internal NTC, we use the current
153 * source to calculate the resistance, 7uA or 20uA
155 rbs
= (v_batctrl
* 1000
156 - di
->bat
->gnd_lift_resistance
* inst_curr
)
160 * BAT_CTRL is internally
161 * connected to 1.8V through a 80k resistor
163 rbs
= (80000 * (v_batctrl
)) / (1800 - v_batctrl
);
170 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
171 * @di: pointer to the ab8500_btemp structure
173 * This function returns the voltage on BATCTRL. Returns value in mV.
175 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp
*di
)
180 vbtemp
= ab8500_gpadc_convert(di
->gpadc
, BAT_CTRL
);
183 "%s gpadc conversion failed, using previous value",
192 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
193 * @di: pointer to the ab8500_btemp structure
194 * @enable: enable or disable the current source
196 * Enable or disable the current sources for the BatCtrl AD channel
198 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp
*di
,
205 * BATCTRL current sources are included on AB8500 cut2.0
206 * and future versions
208 if (is_ab8500_1p1_or_earlier(di
->parent
))
211 /* Only do this for batteries with internal NTC */
212 if (di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
&& enable
) {
213 if (di
->curr_source
== BTEMP_BATCTRL_CURR_SRC_7UA
)
214 curr
= BAT_CTRL_7U_ENA
;
216 curr
= BAT_CTRL_20U_ENA
;
218 dev_dbg(di
->dev
, "Set BATCTRL %duA\n", di
->curr_source
);
220 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
221 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
222 FORCE_BAT_CTRL_CMP_HIGH
, FORCE_BAT_CTRL_CMP_HIGH
);
224 dev_err(di
->dev
, "%s failed setting cmp_force\n",
230 * We have to wait one 32kHz cycle before enabling
231 * the current source, since ForceBatCtrlCmpHigh needs
232 * to be written in a separate cycle
236 ret
= abx500_set_register_interruptible(di
->dev
,
237 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
238 FORCE_BAT_CTRL_CMP_HIGH
| curr
);
240 dev_err(di
->dev
, "%s failed enabling current source\n",
242 goto disable_curr_source
;
244 } else if (di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
&& !enable
) {
245 dev_dbg(di
->dev
, "Disable BATCTRL curr source\n");
247 /* Write 0 to the curr bits */
248 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
249 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
250 BAT_CTRL_7U_ENA
| BAT_CTRL_20U_ENA
,
251 ~(BAT_CTRL_7U_ENA
| BAT_CTRL_20U_ENA
));
253 dev_err(di
->dev
, "%s failed disabling current source\n",
255 goto disable_curr_source
;
258 /* Enable Pull-Up and comparator */
259 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
260 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
261 BAT_CTRL_PULL_UP_ENA
| BAT_CTRL_CMP_ENA
,
262 BAT_CTRL_PULL_UP_ENA
| BAT_CTRL_CMP_ENA
);
264 dev_err(di
->dev
, "%s failed enabling PU and comp\n",
270 * We have to wait one 32kHz cycle before disabling
271 * ForceBatCtrlCmpHigh since this needs to be written
272 * in a separate cycle
276 /* Disable 'force comparator' */
277 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
278 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
279 FORCE_BAT_CTRL_CMP_HIGH
, ~FORCE_BAT_CTRL_CMP_HIGH
);
281 dev_err(di
->dev
, "%s failed disabling force comp\n",
283 goto disable_force_comp
;
289 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
290 * if we got an error above
293 /* Write 0 to the curr bits */
294 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
295 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
296 BAT_CTRL_7U_ENA
| BAT_CTRL_20U_ENA
,
297 ~(BAT_CTRL_7U_ENA
| BAT_CTRL_20U_ENA
));
299 dev_err(di
->dev
, "%s failed disabling current source\n",
304 /* Enable Pull-Up and comparator */
305 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
306 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
307 BAT_CTRL_PULL_UP_ENA
| BAT_CTRL_CMP_ENA
,
308 BAT_CTRL_PULL_UP_ENA
| BAT_CTRL_CMP_ENA
);
310 dev_err(di
->dev
, "%s failed enabling PU and comp\n",
317 * We have to wait one 32kHz cycle before disabling
318 * ForceBatCtrlCmpHigh since this needs to be written
319 * in a separate cycle
323 /* Disable 'force comparator' */
324 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
325 AB8500_CHARGER
, AB8500_BAT_CTRL_CURRENT_SOURCE
,
326 FORCE_BAT_CTRL_CMP_HIGH
, ~FORCE_BAT_CTRL_CMP_HIGH
);
328 dev_err(di
->dev
, "%s failed disabling force comp\n",
337 * ab8500_btemp_get_batctrl_res() - get battery resistance
338 * @di: pointer to the ab8500_btemp structure
340 * This function returns the battery pack identification resistance.
341 * Returns value in Ohms.
343 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp
*di
)
352 * BATCTRL current sources are included on AB8500 cut2.0
353 * and future versions
355 ret
= ab8500_btemp_curr_source_enable(di
, true);
357 dev_err(di
->dev
, "%s curr source enabled failed\n", __func__
);
362 di
->fg
= ab8500_fg_get();
364 dev_err(di
->dev
, "No fg found\n");
368 ret
= ab8500_fg_inst_curr_start(di
->fg
);
371 dev_err(di
->dev
, "Failed to start current measurement\n");
376 * Since there is no interrupt when current measurement is done,
377 * loop for over 250ms (250ms is one sample conversion time
378 * with 32.768 Khz RTC clock). Note that a stop time must be set
379 * since the ab8500_btemp_read_batctrl_voltage call can block and
380 * take an unknown amount of time to complete.
385 batctrl
+= ab8500_btemp_read_batctrl_voltage(di
);
388 } while (!ab8500_fg_inst_curr_done(di
->fg
));
391 ret
= ab8500_fg_inst_curr_finalize(di
->fg
, &inst_curr
);
393 dev_err(di
->dev
, "Failed to finalize current measurement\n");
397 res
= ab8500_btemp_batctrl_volt_to_res(di
, batctrl
, inst_curr
);
399 ret
= ab8500_btemp_curr_source_enable(di
, false);
401 dev_err(di
->dev
, "%s curr source disable failed\n", __func__
);
405 dev_dbg(di
->dev
, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
406 __func__
, batctrl
, res
, inst_curr
, i
);
412 * ab8500_btemp_res_to_temp() - resistance to temperature
413 * @di: pointer to the ab8500_btemp structure
414 * @tbl: pointer to the resiatance to temperature table
415 * @tbl_size: size of the resistance to temperature table
416 * @res: resistance to calculate the temperature from
418 * This function returns the battery temperature in degrees Celcius
419 * based on the NTC resistance.
421 static int ab8500_btemp_res_to_temp(struct ab8500_btemp
*di
,
422 const struct abx500_res_to_temp
*tbl
, int tbl_size
, int res
)
426 * Calculate the formula for the straight line
427 * Simple interpolation if we are within
428 * the resistance table limits, extrapolate
429 * if resistance is outside the limits.
431 if (res
> tbl
[0].resist
)
433 else if (res
<= tbl
[tbl_size
- 1].resist
)
437 while (!(res
<= tbl
[i
].resist
&&
438 res
> tbl
[i
+ 1].resist
))
442 temp
= tbl
[i
].temp
+ ((tbl
[i
+ 1].temp
- tbl
[i
].temp
) *
443 (res
- tbl
[i
].resist
)) / (tbl
[i
+ 1].resist
- tbl
[i
].resist
);
448 * ab8500_btemp_measure_temp() - measure battery temperature
449 * @di: pointer to the ab8500_btemp structure
451 * Returns battery temperature (on success) else the previous temperature
453 static int ab8500_btemp_measure_temp(struct ab8500_btemp
*di
)
457 int rbat
, rntc
, vntc
;
460 id
= di
->bat
->batt_id
;
462 if (di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
&&
463 id
!= BATTERY_UNKNOWN
) {
465 rbat
= ab8500_btemp_get_batctrl_res(di
);
467 dev_err(di
->dev
, "%s get batctrl res failed\n",
470 * Return out-of-range temperature so that
471 * charging is stopped
473 return BTEMP_THERMAL_LOW_LIMIT
;
476 temp
= ab8500_btemp_res_to_temp(di
,
477 di
->bat
->bat_type
[id
].r_to_t_tbl
,
478 di
->bat
->bat_type
[id
].n_temp_tbl_elements
, rbat
);
480 vntc
= ab8500_gpadc_convert(di
->gpadc
, BTEMP_BALL
);
483 "%s gpadc conversion failed,"
484 " using previous value\n", __func__
);
488 * The PCB NTC is sourced from VTVOUT via a 230kOhm
491 rntc
= 230000 * vntc
/ (VTVOUT_V
- vntc
);
493 temp
= ab8500_btemp_res_to_temp(di
,
494 di
->bat
->bat_type
[id
].r_to_t_tbl
,
495 di
->bat
->bat_type
[id
].n_temp_tbl_elements
, rntc
);
498 dev_dbg(di
->dev
, "Battery temperature is %d\n", temp
);
503 * ab8500_btemp_id() - Identify the connected battery
504 * @di: pointer to the ab8500_btemp structure
506 * This function will try to identify the battery by reading the ID
507 * resistor. Some brands use a combined ID resistor with a NTC resistor to
508 * both be able to identify and to read the temperature of it.
510 static int ab8500_btemp_id(struct ab8500_btemp
*di
)
515 di
->curr_source
= BTEMP_BATCTRL_CURR_SRC_7UA
;
516 di
->bat
->batt_id
= BATTERY_UNKNOWN
;
518 res
= ab8500_btemp_get_batctrl_res(di
);
520 dev_err(di
->dev
, "%s get batctrl res failed\n", __func__
);
524 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
525 for (i
= BATTERY_UNKNOWN
+ 1; i
< di
->bat
->n_btypes
; i
++) {
526 if ((res
<= di
->bat
->bat_type
[i
].resis_high
) &&
527 (res
>= di
->bat
->bat_type
[i
].resis_low
)) {
528 dev_dbg(di
->dev
, "Battery detected on %s"
529 " low %d < res %d < high: %d"
531 di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
?
532 "BATCTRL" : "BATTEMP",
533 di
->bat
->bat_type
[i
].resis_low
, res
,
534 di
->bat
->bat_type
[i
].resis_high
, i
);
536 di
->bat
->batt_id
= i
;
541 if (di
->bat
->batt_id
== BATTERY_UNKNOWN
) {
542 dev_warn(di
->dev
, "Battery identified as unknown"
543 ", resistance %d Ohm\n", res
);
548 * We only have to change current source if the
549 * detected type is Type 1, else we use the 7uA source
551 if (di
->bat
->adc_therm
== ABx500_ADC_THERM_BATCTRL
&&
552 di
->bat
->batt_id
== 1) {
553 dev_dbg(di
->dev
, "Set BATCTRL current source to 20uA\n");
554 di
->curr_source
= BTEMP_BATCTRL_CURR_SRC_20UA
;
557 return di
->bat
->batt_id
;
561 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
562 * @work: pointer to the work_struct structure
564 * Work function for measuring the temperature periodically
566 static void ab8500_btemp_periodic_work(struct work_struct
*work
)
569 struct ab8500_btemp
*di
= container_of(work
,
570 struct ab8500_btemp
, btemp_periodic_work
.work
);
572 di
->bat_temp
= ab8500_btemp_measure_temp(di
);
574 if (di
->bat_temp
!= di
->prev_bat_temp
) {
575 di
->prev_bat_temp
= di
->bat_temp
;
576 power_supply_changed(&di
->btemp_psy
);
579 if (di
->events
.ac_conn
|| di
->events
.usb_conn
)
580 interval
= di
->bat
->temp_interval_chg
;
582 interval
= di
->bat
->temp_interval_nochg
;
584 /* Schedule a new measurement */
585 queue_delayed_work(di
->btemp_wq
,
586 &di
->btemp_periodic_work
,
587 round_jiffies(interval
* HZ
));
591 * ab8500_btemp_batctrlindb_handler() - battery removal detected
592 * @irq: interrupt number
593 * @_di: void pointer that has to address of ab8500_btemp
595 * Returns IRQ status(IRQ_HANDLED)
597 static irqreturn_t
ab8500_btemp_batctrlindb_handler(int irq
, void *_di
)
599 struct ab8500_btemp
*di
= _di
;
600 dev_err(di
->dev
, "Battery removal detected!\n");
602 di
->events
.batt_rem
= true;
603 power_supply_changed(&di
->btemp_psy
);
609 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
610 * @irq: interrupt number
611 * @_di: void pointer that has to address of ab8500_btemp
613 * Returns IRQ status(IRQ_HANDLED)
615 static irqreturn_t
ab8500_btemp_templow_handler(int irq
, void *_di
)
617 struct ab8500_btemp
*di
= _di
;
619 if (is_ab8500_2p0_or_earlier(di
->parent
)) {
620 dev_dbg(di
->dev
, "Ignore false btemp low irq"
621 " for ABB cut 1.0, 1.1 and 2.0\n");
623 dev_crit(di
->dev
, "Battery temperature lower than -10deg c\n");
625 di
->events
.btemp_low
= true;
626 di
->events
.btemp_high
= false;
627 di
->events
.btemp_medhigh
= false;
628 di
->events
.btemp_lowmed
= false;
629 power_supply_changed(&di
->btemp_psy
);
636 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
637 * @irq: interrupt number
638 * @_di: void pointer that has to address of ab8500_btemp
640 * Returns IRQ status(IRQ_HANDLED)
642 static irqreturn_t
ab8500_btemp_temphigh_handler(int irq
, void *_di
)
644 struct ab8500_btemp
*di
= _di
;
646 dev_crit(di
->dev
, "Battery temperature is higher than MAX temp\n");
648 di
->events
.btemp_high
= true;
649 di
->events
.btemp_medhigh
= false;
650 di
->events
.btemp_lowmed
= false;
651 di
->events
.btemp_low
= false;
652 power_supply_changed(&di
->btemp_psy
);
658 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
659 * @irq: interrupt number
660 * @_di: void pointer that has to address of ab8500_btemp
662 * Returns IRQ status(IRQ_HANDLED)
664 static irqreturn_t
ab8500_btemp_lowmed_handler(int irq
, void *_di
)
666 struct ab8500_btemp
*di
= _di
;
668 dev_dbg(di
->dev
, "Battery temperature is between low and medium\n");
670 di
->events
.btemp_lowmed
= true;
671 di
->events
.btemp_medhigh
= false;
672 di
->events
.btemp_high
= false;
673 di
->events
.btemp_low
= false;
674 power_supply_changed(&di
->btemp_psy
);
680 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
681 * @irq: interrupt number
682 * @_di: void pointer that has to address of ab8500_btemp
684 * Returns IRQ status(IRQ_HANDLED)
686 static irqreturn_t
ab8500_btemp_medhigh_handler(int irq
, void *_di
)
688 struct ab8500_btemp
*di
= _di
;
690 dev_dbg(di
->dev
, "Battery temperature is between medium and high\n");
692 di
->events
.btemp_medhigh
= true;
693 di
->events
.btemp_lowmed
= false;
694 di
->events
.btemp_high
= false;
695 di
->events
.btemp_low
= false;
696 power_supply_changed(&di
->btemp_psy
);
702 * ab8500_btemp_periodic() - Periodic temperature measurements
703 * @di: pointer to the ab8500_btemp structure
704 * @enable: enable or disable periodic temperature measurements
706 * Starts of stops periodic temperature measurements. Periodic measurements
707 * should only be done when a charger is connected.
709 static void ab8500_btemp_periodic(struct ab8500_btemp
*di
,
712 dev_dbg(di
->dev
, "Enable periodic temperature measurements: %d\n",
715 * Make sure a new measurement is done directly by cancelling
718 cancel_delayed_work_sync(&di
->btemp_periodic_work
);
721 queue_delayed_work(di
->btemp_wq
, &di
->btemp_periodic_work
, 0);
725 * ab8500_btemp_get_temp() - get battery temperature
726 * @di: pointer to the ab8500_btemp structure
728 * Returns battery temperature
730 static int ab8500_btemp_get_temp(struct ab8500_btemp
*di
)
735 * The BTEMP events are not reliabe on AB8500 cut2.0
738 if (is_ab8500_2p0_or_earlier(di
->parent
)) {
739 temp
= di
->bat_temp
* 10;
741 if (di
->events
.btemp_low
) {
742 if (temp
> di
->btemp_ranges
.btemp_low_limit
)
743 temp
= di
->btemp_ranges
.btemp_low_limit
;
745 temp
= di
->bat_temp
* 10;
746 } else if (di
->events
.btemp_high
) {
747 if (temp
< di
->btemp_ranges
.btemp_high_limit
)
748 temp
= di
->btemp_ranges
.btemp_high_limit
;
750 temp
= di
->bat_temp
* 10;
751 } else if (di
->events
.btemp_lowmed
) {
752 if (temp
> di
->btemp_ranges
.btemp_med_limit
)
753 temp
= di
->btemp_ranges
.btemp_med_limit
;
755 temp
= di
->bat_temp
* 10;
756 } else if (di
->events
.btemp_medhigh
) {
757 if (temp
< di
->btemp_ranges
.btemp_med_limit
)
758 temp
= di
->btemp_ranges
.btemp_med_limit
;
760 temp
= di
->bat_temp
* 10;
762 temp
= di
->bat_temp
* 10;
768 * ab8500_btemp_get_batctrl_temp() - get the temperature
769 * @btemp: pointer to the btemp structure
771 * Returns the batctrl temperature in millidegrees
773 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp
*btemp
)
775 return btemp
->bat_temp
* 1000;
779 * ab8500_btemp_get_property() - get the btemp properties
780 * @psy: pointer to the power_supply structure
781 * @psp: pointer to the power_supply_property structure
782 * @val: pointer to the power_supply_propval union
784 * This function gets called when an application tries to get the btemp
785 * properties by reading the sysfs files.
786 * online: presence of the battery
787 * present: presence of the battery
788 * technology: battery technology
789 * temp: battery temperature
790 * Returns error code in case of failure else 0(on success)
792 static int ab8500_btemp_get_property(struct power_supply
*psy
,
793 enum power_supply_property psp
,
794 union power_supply_propval
*val
)
796 struct ab8500_btemp
*di
;
798 di
= to_ab8500_btemp_device_info(psy
);
801 case POWER_SUPPLY_PROP_PRESENT
:
802 case POWER_SUPPLY_PROP_ONLINE
:
803 if (di
->events
.batt_rem
)
808 case POWER_SUPPLY_PROP_TECHNOLOGY
:
809 val
->intval
= di
->bat
->bat_type
[di
->bat
->batt_id
].name
;
811 case POWER_SUPPLY_PROP_TEMP
:
812 val
->intval
= ab8500_btemp_get_temp(di
);
820 static int ab8500_btemp_get_ext_psy_data(struct device
*dev
, void *data
)
822 struct power_supply
*psy
;
823 struct power_supply
*ext
;
824 struct ab8500_btemp
*di
;
825 union power_supply_propval ret
;
827 bool psy_found
= false;
829 psy
= (struct power_supply
*)data
;
830 ext
= dev_get_drvdata(dev
);
831 di
= to_ab8500_btemp_device_info(psy
);
834 * For all psy where the name of your driver
835 * appears in any supplied_to
837 for (i
= 0; i
< ext
->num_supplicants
; i
++) {
838 if (!strcmp(ext
->supplied_to
[i
], psy
->name
))
845 /* Go through all properties for the psy */
846 for (j
= 0; j
< ext
->num_properties
; j
++) {
847 enum power_supply_property prop
;
848 prop
= ext
->properties
[j
];
850 if (ext
->get_property(ext
, prop
, &ret
))
854 case POWER_SUPPLY_PROP_PRESENT
:
856 case POWER_SUPPLY_TYPE_MAINS
:
857 /* AC disconnected */
858 if (!ret
.intval
&& di
->events
.ac_conn
) {
859 di
->events
.ac_conn
= false;
862 else if (ret
.intval
&& !di
->events
.ac_conn
) {
863 di
->events
.ac_conn
= true;
864 if (!di
->events
.usb_conn
)
865 ab8500_btemp_periodic(di
, true);
868 case POWER_SUPPLY_TYPE_USB
:
869 /* USB disconnected */
870 if (!ret
.intval
&& di
->events
.usb_conn
) {
871 di
->events
.usb_conn
= false;
874 else if (ret
.intval
&& !di
->events
.usb_conn
) {
875 di
->events
.usb_conn
= true;
876 if (!di
->events
.ac_conn
)
877 ab8500_btemp_periodic(di
, true);
892 * ab8500_btemp_external_power_changed() - callback for power supply changes
893 * @psy: pointer to the structure power_supply
895 * This function is pointing to the function pointer external_power_changed
896 * of the structure power_supply.
897 * This function gets executed when there is a change in the external power
898 * supply to the btemp.
900 static void ab8500_btemp_external_power_changed(struct power_supply
*psy
)
902 struct ab8500_btemp
*di
= to_ab8500_btemp_device_info(psy
);
904 class_for_each_device(power_supply_class
, NULL
,
905 &di
->btemp_psy
, ab8500_btemp_get_ext_psy_data
);
908 /* ab8500 btemp driver interrupts and their respective isr */
909 static struct ab8500_btemp_interrupts ab8500_btemp_irq
[] = {
910 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler
},
911 {"BTEMP_LOW", ab8500_btemp_templow_handler
},
912 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler
},
913 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler
},
914 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler
},
917 #if defined(CONFIG_PM)
918 static int ab8500_btemp_resume(struct platform_device
*pdev
)
920 struct ab8500_btemp
*di
= platform_get_drvdata(pdev
);
922 ab8500_btemp_periodic(di
, true);
927 static int ab8500_btemp_suspend(struct platform_device
*pdev
,
930 struct ab8500_btemp
*di
= platform_get_drvdata(pdev
);
932 ab8500_btemp_periodic(di
, false);
937 #define ab8500_btemp_suspend NULL
938 #define ab8500_btemp_resume NULL
941 static int __devexit
ab8500_btemp_remove(struct platform_device
*pdev
)
943 struct ab8500_btemp
*di
= platform_get_drvdata(pdev
);
946 /* Disable interrupts */
947 for (i
= 0; i
< ARRAY_SIZE(ab8500_btemp_irq
); i
++) {
948 irq
= platform_get_irq_byname(pdev
, ab8500_btemp_irq
[i
].name
);
952 /* Delete the work queue */
953 destroy_workqueue(di
->btemp_wq
);
955 flush_scheduled_work();
956 power_supply_unregister(&di
->btemp_psy
);
957 platform_set_drvdata(pdev
, NULL
);
963 static int __devinit
ab8500_btemp_probe(struct platform_device
*pdev
)
967 struct abx500_bm_plat_data
*plat_data
= pdev
->dev
.platform_data
;
968 struct ab8500_btemp
*di
;
971 dev_err(&pdev
->dev
, "No platform data\n");
975 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
979 /* get parent data */
980 di
->dev
= &pdev
->dev
;
981 di
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
982 di
->gpadc
= ab8500_gpadc_get("ab8500-gpadc.0");
984 /* get btemp specific platform data */
985 di
->pdata
= plat_data
->btemp
;
987 dev_err(di
->dev
, "no btemp platform data supplied\n");
989 goto free_device_info
;
992 /* get battery specific platform data */
993 di
->bat
= plat_data
->battery
;
995 dev_err(di
->dev
, "no battery platform data supplied\n");
997 goto free_device_info
;
1001 di
->btemp_psy
.name
= "ab8500_btemp";
1002 di
->btemp_psy
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1003 di
->btemp_psy
.properties
= ab8500_btemp_props
;
1004 di
->btemp_psy
.num_properties
= ARRAY_SIZE(ab8500_btemp_props
);
1005 di
->btemp_psy
.get_property
= ab8500_btemp_get_property
;
1006 di
->btemp_psy
.supplied_to
= di
->pdata
->supplied_to
;
1007 di
->btemp_psy
.num_supplicants
= di
->pdata
->num_supplicants
;
1008 di
->btemp_psy
.external_power_changed
=
1009 ab8500_btemp_external_power_changed
;
1012 /* Create a work queue for the btemp */
1014 create_singlethread_workqueue("ab8500_btemp_wq");
1015 if (di
->btemp_wq
== NULL
) {
1016 dev_err(di
->dev
, "failed to create work queue\n");
1018 goto free_device_info
;
1021 /* Init work for measuring temperature periodically */
1022 INIT_DEFERRABLE_WORK(&di
->btemp_periodic_work
,
1023 ab8500_btemp_periodic_work
);
1025 /* Identify the battery */
1026 if (ab8500_btemp_id(di
) < 0)
1027 dev_warn(di
->dev
, "failed to identify the battery\n");
1029 /* Set BTEMP thermal limits. Low and Med are fixed */
1030 di
->btemp_ranges
.btemp_low_limit
= BTEMP_THERMAL_LOW_LIMIT
;
1031 di
->btemp_ranges
.btemp_med_limit
= BTEMP_THERMAL_MED_LIMIT
;
1033 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1034 AB8500_BTEMP_HIGH_TH
, &val
);
1036 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
1040 case BTEMP_HIGH_TH_57_0
:
1041 case BTEMP_HIGH_TH_57_1
:
1042 di
->btemp_ranges
.btemp_high_limit
=
1043 BTEMP_THERMAL_HIGH_LIMIT_57
;
1045 case BTEMP_HIGH_TH_52
:
1046 di
->btemp_ranges
.btemp_high_limit
=
1047 BTEMP_THERMAL_HIGH_LIMIT_52
;
1049 case BTEMP_HIGH_TH_62
:
1050 di
->btemp_ranges
.btemp_high_limit
=
1051 BTEMP_THERMAL_HIGH_LIMIT_62
;
1055 /* Register BTEMP power supply class */
1056 ret
= power_supply_register(di
->dev
, &di
->btemp_psy
);
1058 dev_err(di
->dev
, "failed to register BTEMP psy\n");
1062 /* Register interrupts */
1063 for (i
= 0; i
< ARRAY_SIZE(ab8500_btemp_irq
); i
++) {
1064 irq
= platform_get_irq_byname(pdev
, ab8500_btemp_irq
[i
].name
);
1065 ret
= request_threaded_irq(irq
, NULL
, ab8500_btemp_irq
[i
].isr
,
1066 IRQF_SHARED
| IRQF_NO_SUSPEND
,
1067 ab8500_btemp_irq
[i
].name
, di
);
1070 dev_err(di
->dev
, "failed to request %s IRQ %d: %d\n"
1071 , ab8500_btemp_irq
[i
].name
, irq
, ret
);
1074 dev_dbg(di
->dev
, "Requested %s IRQ %d: %d\n",
1075 ab8500_btemp_irq
[i
].name
, irq
, ret
);
1078 platform_set_drvdata(pdev
, di
);
1080 /* Kick off periodic temperature measurements */
1081 ab8500_btemp_periodic(di
, true);
1082 list_add_tail(&di
->node
, &ab8500_btemp_list
);
1087 power_supply_unregister(&di
->btemp_psy
);
1089 /* We also have to free all successfully registered irqs */
1090 for (i
= i
- 1; i
>= 0; i
--) {
1091 irq
= platform_get_irq_byname(pdev
, ab8500_btemp_irq
[i
].name
);
1095 destroy_workqueue(di
->btemp_wq
);
1102 static struct platform_driver ab8500_btemp_driver
= {
1103 .probe
= ab8500_btemp_probe
,
1104 .remove
= __devexit_p(ab8500_btemp_remove
),
1105 .suspend
= ab8500_btemp_suspend
,
1106 .resume
= ab8500_btemp_resume
,
1108 .name
= "ab8500-btemp",
1109 .owner
= THIS_MODULE
,
1113 static int __init
ab8500_btemp_init(void)
1115 return platform_driver_register(&ab8500_btemp_driver
);
1118 static void __exit
ab8500_btemp_exit(void)
1120 platform_driver_unregister(&ab8500_btemp_driver
);
1123 subsys_initcall_sync(ab8500_btemp_init
);
1124 module_exit(ab8500_btemp_exit
);
1126 MODULE_LICENSE("GPL v2");
1127 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1128 MODULE_ALIAS("platform:ab8500-btemp");
1129 MODULE_DESCRIPTION("AB8500 battery temperature driver");