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