Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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> |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/mfd/core.h> |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 26 | #include <linux/mfd/abx500.h> |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 27 | #include <linux/mfd/abx500/ab8500.h> |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 28 | #include <linux/mfd/abx500/ab8500-bm.h> |
| 29 | #include <linux/mfd/abx500/ab8500-gpadc.h> |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 81 | * @bm: Platform specific battery management information |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 |
Jonas Aaberg | f6271b4 | 2012-02-27 11:02:44 +0100 | [diff] [blame] | 87 | * @initialized: True if battery id read. |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 98 | struct abx500_bm_data *bm; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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; |
Jonas Aaberg | f6271b4 | 2012-02-27 11:02:44 +0100 | [diff] [blame] | 104 | bool initialized; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 152 | if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 158 | - di->bm->gnd_lift_resistance * inst_curr) |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 214 | if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 | } |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 246 | } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 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 | /* |
| 378 | * Since there is no interrupt when current measurement is done, |
| 379 | * loop for over 250ms (250ms is one sample conversion time |
| 380 | * with 32.768 Khz RTC clock). Note that a stop time must be set |
| 381 | * since the ab8500_btemp_read_batctrl_voltage call can block and |
| 382 | * take an unknown amount of time to complete. |
| 383 | */ |
| 384 | i = 0; |
| 385 | |
| 386 | do { |
| 387 | batctrl += ab8500_btemp_read_batctrl_voltage(di); |
| 388 | i++; |
| 389 | msleep(20); |
| 390 | } while (!ab8500_fg_inst_curr_done(di->fg)); |
| 391 | batctrl /= i; |
| 392 | |
| 393 | ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr); |
| 394 | if (ret) { |
| 395 | dev_err(di->dev, "Failed to finalize current measurement\n"); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr); |
| 400 | |
| 401 | ret = ab8500_btemp_curr_source_enable(di, false); |
| 402 | if (ret) { |
| 403 | dev_err(di->dev, "%s curr source disable failed\n", __func__); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n", |
| 408 | __func__, batctrl, res, inst_curr, i); |
| 409 | |
| 410 | return res; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * ab8500_btemp_res_to_temp() - resistance to temperature |
| 415 | * @di: pointer to the ab8500_btemp structure |
| 416 | * @tbl: pointer to the resiatance to temperature table |
| 417 | * @tbl_size: size of the resistance to temperature table |
| 418 | * @res: resistance to calculate the temperature from |
| 419 | * |
| 420 | * This function returns the battery temperature in degrees Celcius |
| 421 | * based on the NTC resistance. |
| 422 | */ |
| 423 | static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di, |
| 424 | const struct abx500_res_to_temp *tbl, int tbl_size, int res) |
| 425 | { |
| 426 | int i, temp; |
| 427 | /* |
| 428 | * Calculate the formula for the straight line |
| 429 | * Simple interpolation if we are within |
| 430 | * the resistance table limits, extrapolate |
| 431 | * if resistance is outside the limits. |
| 432 | */ |
| 433 | if (res > tbl[0].resist) |
| 434 | i = 0; |
| 435 | else if (res <= tbl[tbl_size - 1].resist) |
| 436 | i = tbl_size - 2; |
| 437 | else { |
| 438 | i = 0; |
| 439 | while (!(res <= tbl[i].resist && |
| 440 | res > tbl[i + 1].resist)) |
| 441 | i++; |
| 442 | } |
| 443 | |
| 444 | temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) * |
| 445 | (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist); |
| 446 | return temp; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * ab8500_btemp_measure_temp() - measure battery temperature |
| 451 | * @di: pointer to the ab8500_btemp structure |
| 452 | * |
| 453 | * Returns battery temperature (on success) else the previous temperature |
| 454 | */ |
| 455 | static int ab8500_btemp_measure_temp(struct ab8500_btemp *di) |
| 456 | { |
| 457 | int temp; |
| 458 | static int prev; |
| 459 | int rbat, rntc, vntc; |
| 460 | u8 id; |
| 461 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 462 | id = di->bm->batt_id; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 463 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 464 | if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 465 | id != BATTERY_UNKNOWN) { |
| 466 | |
| 467 | rbat = ab8500_btemp_get_batctrl_res(di); |
| 468 | if (rbat < 0) { |
| 469 | dev_err(di->dev, "%s get batctrl res failed\n", |
| 470 | __func__); |
| 471 | /* |
| 472 | * Return out-of-range temperature so that |
| 473 | * charging is stopped |
| 474 | */ |
| 475 | return BTEMP_THERMAL_LOW_LIMIT; |
| 476 | } |
| 477 | |
| 478 | temp = ab8500_btemp_res_to_temp(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 479 | di->bm->bat_type[id].r_to_t_tbl, |
| 480 | di->bm->bat_type[id].n_temp_tbl_elements, rbat); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 481 | } else { |
| 482 | vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL); |
| 483 | if (vntc < 0) { |
| 484 | dev_err(di->dev, |
| 485 | "%s gpadc conversion failed," |
| 486 | " using previous value\n", __func__); |
| 487 | return prev; |
| 488 | } |
| 489 | /* |
| 490 | * The PCB NTC is sourced from VTVOUT via a 230kOhm |
| 491 | * resistor. |
| 492 | */ |
| 493 | rntc = 230000 * vntc / (VTVOUT_V - vntc); |
| 494 | |
| 495 | temp = ab8500_btemp_res_to_temp(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 496 | di->bm->bat_type[id].r_to_t_tbl, |
| 497 | di->bm->bat_type[id].n_temp_tbl_elements, rntc); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 498 | prev = temp; |
| 499 | } |
| 500 | dev_dbg(di->dev, "Battery temperature is %d\n", temp); |
| 501 | return temp; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * ab8500_btemp_id() - Identify the connected battery |
| 506 | * @di: pointer to the ab8500_btemp structure |
| 507 | * |
| 508 | * This function will try to identify the battery by reading the ID |
| 509 | * resistor. Some brands use a combined ID resistor with a NTC resistor to |
| 510 | * both be able to identify and to read the temperature of it. |
| 511 | */ |
| 512 | static int ab8500_btemp_id(struct ab8500_btemp *di) |
| 513 | { |
| 514 | int res; |
| 515 | u8 i; |
| 516 | |
| 517 | di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 518 | di->bm->batt_id = BATTERY_UNKNOWN; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 519 | |
| 520 | res = ab8500_btemp_get_batctrl_res(di); |
| 521 | if (res < 0) { |
| 522 | dev_err(di->dev, "%s get batctrl res failed\n", __func__); |
| 523 | return -ENXIO; |
| 524 | } |
| 525 | |
| 526 | /* BATTERY_UNKNOWN is defined on position 0, skip it! */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 527 | for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) { |
| 528 | if ((res <= di->bm->bat_type[i].resis_high) && |
| 529 | (res >= di->bm->bat_type[i].resis_low)) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 530 | dev_dbg(di->dev, "Battery detected on %s" |
| 531 | " low %d < res %d < high: %d" |
| 532 | " index: %d\n", |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 533 | di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ? |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 534 | "BATCTRL" : "BATTEMP", |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 535 | di->bm->bat_type[i].resis_low, res, |
| 536 | di->bm->bat_type[i].resis_high, i); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 537 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 538 | di->bm->batt_id = i; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 543 | if (di->bm->batt_id == BATTERY_UNKNOWN) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 544 | dev_warn(di->dev, "Battery identified as unknown" |
| 545 | ", resistance %d Ohm\n", res); |
| 546 | return -ENXIO; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * We only have to change current source if the |
| 551 | * detected type is Type 1, else we use the 7uA source |
| 552 | */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 553 | if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && |
| 554 | di->bm->batt_id == 1) { |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 555 | dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n"); |
| 556 | di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA; |
| 557 | } |
| 558 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 559 | return di->bm->batt_id; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
| 563 | * ab8500_btemp_periodic_work() - Measuring the temperature periodically |
| 564 | * @work: pointer to the work_struct structure |
| 565 | * |
| 566 | * Work function for measuring the temperature periodically |
| 567 | */ |
| 568 | static void ab8500_btemp_periodic_work(struct work_struct *work) |
| 569 | { |
| 570 | int interval; |
| 571 | struct ab8500_btemp *di = container_of(work, |
| 572 | struct ab8500_btemp, btemp_periodic_work.work); |
| 573 | |
Jonas Aaberg | f6271b4 | 2012-02-27 11:02:44 +0100 | [diff] [blame] | 574 | if (!di->initialized) { |
| 575 | di->initialized = true; |
| 576 | /* Identify the battery */ |
| 577 | if (ab8500_btemp_id(di) < 0) |
| 578 | dev_warn(di->dev, "failed to identify the battery\n"); |
| 579 | } |
| 580 | |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 581 | di->bat_temp = ab8500_btemp_measure_temp(di); |
| 582 | |
| 583 | if (di->bat_temp != di->prev_bat_temp) { |
| 584 | di->prev_bat_temp = di->bat_temp; |
| 585 | power_supply_changed(&di->btemp_psy); |
| 586 | } |
| 587 | |
| 588 | if (di->events.ac_conn || di->events.usb_conn) |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 589 | interval = di->bm->temp_interval_chg; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 590 | else |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 591 | interval = di->bm->temp_interval_nochg; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 592 | |
| 593 | /* Schedule a new measurement */ |
| 594 | queue_delayed_work(di->btemp_wq, |
| 595 | &di->btemp_periodic_work, |
| 596 | round_jiffies(interval * HZ)); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * ab8500_btemp_batctrlindb_handler() - battery removal detected |
| 601 | * @irq: interrupt number |
| 602 | * @_di: void pointer that has to address of ab8500_btemp |
| 603 | * |
| 604 | * Returns IRQ status(IRQ_HANDLED) |
| 605 | */ |
| 606 | static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di) |
| 607 | { |
| 608 | struct ab8500_btemp *di = _di; |
| 609 | dev_err(di->dev, "Battery removal detected!\n"); |
| 610 | |
| 611 | di->events.batt_rem = true; |
| 612 | power_supply_changed(&di->btemp_psy); |
| 613 | |
| 614 | return IRQ_HANDLED; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees |
| 619 | * @irq: interrupt number |
| 620 | * @_di: void pointer that has to address of ab8500_btemp |
| 621 | * |
| 622 | * Returns IRQ status(IRQ_HANDLED) |
| 623 | */ |
| 624 | static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di) |
| 625 | { |
| 626 | struct ab8500_btemp *di = _di; |
| 627 | |
| 628 | if (is_ab8500_2p0_or_earlier(di->parent)) { |
| 629 | dev_dbg(di->dev, "Ignore false btemp low irq" |
| 630 | " for ABB cut 1.0, 1.1 and 2.0\n"); |
| 631 | } else { |
| 632 | dev_crit(di->dev, "Battery temperature lower than -10deg c\n"); |
| 633 | |
| 634 | di->events.btemp_low = true; |
| 635 | di->events.btemp_high = false; |
| 636 | di->events.btemp_medhigh = false; |
| 637 | di->events.btemp_lowmed = false; |
| 638 | power_supply_changed(&di->btemp_psy); |
| 639 | } |
| 640 | |
| 641 | return IRQ_HANDLED; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * ab8500_btemp_temphigh_handler() - battery temp higher than max temp |
| 646 | * @irq: interrupt number |
| 647 | * @_di: void pointer that has to address of ab8500_btemp |
| 648 | * |
| 649 | * Returns IRQ status(IRQ_HANDLED) |
| 650 | */ |
| 651 | static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di) |
| 652 | { |
| 653 | struct ab8500_btemp *di = _di; |
| 654 | |
| 655 | dev_crit(di->dev, "Battery temperature is higher than MAX temp\n"); |
| 656 | |
| 657 | di->events.btemp_high = true; |
| 658 | di->events.btemp_medhigh = false; |
| 659 | di->events.btemp_lowmed = false; |
| 660 | di->events.btemp_low = false; |
| 661 | power_supply_changed(&di->btemp_psy); |
| 662 | |
| 663 | return IRQ_HANDLED; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * ab8500_btemp_lowmed_handler() - battery temp between low and medium |
| 668 | * @irq: interrupt number |
| 669 | * @_di: void pointer that has to address of ab8500_btemp |
| 670 | * |
| 671 | * Returns IRQ status(IRQ_HANDLED) |
| 672 | */ |
| 673 | static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di) |
| 674 | { |
| 675 | struct ab8500_btemp *di = _di; |
| 676 | |
| 677 | dev_dbg(di->dev, "Battery temperature is between low and medium\n"); |
| 678 | |
| 679 | di->events.btemp_lowmed = true; |
| 680 | di->events.btemp_medhigh = false; |
| 681 | di->events.btemp_high = false; |
| 682 | di->events.btemp_low = false; |
| 683 | power_supply_changed(&di->btemp_psy); |
| 684 | |
| 685 | return IRQ_HANDLED; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * ab8500_btemp_medhigh_handler() - battery temp between medium and high |
| 690 | * @irq: interrupt number |
| 691 | * @_di: void pointer that has to address of ab8500_btemp |
| 692 | * |
| 693 | * Returns IRQ status(IRQ_HANDLED) |
| 694 | */ |
| 695 | static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di) |
| 696 | { |
| 697 | struct ab8500_btemp *di = _di; |
| 698 | |
| 699 | dev_dbg(di->dev, "Battery temperature is between medium and high\n"); |
| 700 | |
| 701 | di->events.btemp_medhigh = true; |
| 702 | di->events.btemp_lowmed = false; |
| 703 | di->events.btemp_high = false; |
| 704 | di->events.btemp_low = false; |
| 705 | power_supply_changed(&di->btemp_psy); |
| 706 | |
| 707 | return IRQ_HANDLED; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * ab8500_btemp_periodic() - Periodic temperature measurements |
| 712 | * @di: pointer to the ab8500_btemp structure |
| 713 | * @enable: enable or disable periodic temperature measurements |
| 714 | * |
| 715 | * Starts of stops periodic temperature measurements. Periodic measurements |
| 716 | * should only be done when a charger is connected. |
| 717 | */ |
| 718 | static void ab8500_btemp_periodic(struct ab8500_btemp *di, |
| 719 | bool enable) |
| 720 | { |
| 721 | dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n", |
| 722 | enable); |
| 723 | /* |
| 724 | * Make sure a new measurement is done directly by cancelling |
| 725 | * any pending work |
| 726 | */ |
| 727 | cancel_delayed_work_sync(&di->btemp_periodic_work); |
| 728 | |
| 729 | if (enable) |
| 730 | queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * ab8500_btemp_get_temp() - get battery temperature |
| 735 | * @di: pointer to the ab8500_btemp structure |
| 736 | * |
| 737 | * Returns battery temperature |
| 738 | */ |
| 739 | static int ab8500_btemp_get_temp(struct ab8500_btemp *di) |
| 740 | { |
| 741 | int temp = 0; |
| 742 | |
| 743 | /* |
| 744 | * The BTEMP events are not reliabe on AB8500 cut2.0 |
| 745 | * and prior versions |
| 746 | */ |
| 747 | if (is_ab8500_2p0_or_earlier(di->parent)) { |
| 748 | temp = di->bat_temp * 10; |
| 749 | } else { |
| 750 | if (di->events.btemp_low) { |
| 751 | if (temp > di->btemp_ranges.btemp_low_limit) |
| 752 | temp = di->btemp_ranges.btemp_low_limit; |
| 753 | else |
| 754 | temp = di->bat_temp * 10; |
| 755 | } else if (di->events.btemp_high) { |
| 756 | if (temp < di->btemp_ranges.btemp_high_limit) |
| 757 | temp = di->btemp_ranges.btemp_high_limit; |
| 758 | else |
| 759 | temp = di->bat_temp * 10; |
| 760 | } else if (di->events.btemp_lowmed) { |
| 761 | if (temp > di->btemp_ranges.btemp_med_limit) |
| 762 | temp = di->btemp_ranges.btemp_med_limit; |
| 763 | else |
| 764 | temp = di->bat_temp * 10; |
| 765 | } else if (di->events.btemp_medhigh) { |
| 766 | if (temp < di->btemp_ranges.btemp_med_limit) |
| 767 | temp = di->btemp_ranges.btemp_med_limit; |
| 768 | else |
| 769 | temp = di->bat_temp * 10; |
| 770 | } else |
| 771 | temp = di->bat_temp * 10; |
| 772 | } |
| 773 | return temp; |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * ab8500_btemp_get_batctrl_temp() - get the temperature |
| 778 | * @btemp: pointer to the btemp structure |
| 779 | * |
| 780 | * Returns the batctrl temperature in millidegrees |
| 781 | */ |
| 782 | int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp) |
| 783 | { |
| 784 | return btemp->bat_temp * 1000; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * ab8500_btemp_get_property() - get the btemp properties |
| 789 | * @psy: pointer to the power_supply structure |
| 790 | * @psp: pointer to the power_supply_property structure |
| 791 | * @val: pointer to the power_supply_propval union |
| 792 | * |
| 793 | * This function gets called when an application tries to get the btemp |
| 794 | * properties by reading the sysfs files. |
| 795 | * online: presence of the battery |
| 796 | * present: presence of the battery |
| 797 | * technology: battery technology |
| 798 | * temp: battery temperature |
| 799 | * Returns error code in case of failure else 0(on success) |
| 800 | */ |
| 801 | static int ab8500_btemp_get_property(struct power_supply *psy, |
| 802 | enum power_supply_property psp, |
| 803 | union power_supply_propval *val) |
| 804 | { |
| 805 | struct ab8500_btemp *di; |
| 806 | |
| 807 | di = to_ab8500_btemp_device_info(psy); |
| 808 | |
| 809 | switch (psp) { |
| 810 | case POWER_SUPPLY_PROP_PRESENT: |
| 811 | case POWER_SUPPLY_PROP_ONLINE: |
| 812 | if (di->events.batt_rem) |
| 813 | val->intval = 0; |
| 814 | else |
| 815 | val->intval = 1; |
| 816 | break; |
| 817 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 818 | val->intval = di->bm->bat_type[di->bm->batt_id].name; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 819 | break; |
| 820 | case POWER_SUPPLY_PROP_TEMP: |
| 821 | val->intval = ab8500_btemp_get_temp(di); |
| 822 | break; |
| 823 | default: |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data) |
| 830 | { |
| 831 | struct power_supply *psy; |
| 832 | struct power_supply *ext; |
| 833 | struct ab8500_btemp *di; |
| 834 | union power_supply_propval ret; |
| 835 | int i, j; |
| 836 | bool psy_found = false; |
| 837 | |
| 838 | psy = (struct power_supply *)data; |
| 839 | ext = dev_get_drvdata(dev); |
| 840 | di = to_ab8500_btemp_device_info(psy); |
| 841 | |
| 842 | /* |
| 843 | * For all psy where the name of your driver |
| 844 | * appears in any supplied_to |
| 845 | */ |
| 846 | for (i = 0; i < ext->num_supplicants; i++) { |
| 847 | if (!strcmp(ext->supplied_to[i], psy->name)) |
| 848 | psy_found = true; |
| 849 | } |
| 850 | |
| 851 | if (!psy_found) |
| 852 | return 0; |
| 853 | |
| 854 | /* Go through all properties for the psy */ |
| 855 | for (j = 0; j < ext->num_properties; j++) { |
| 856 | enum power_supply_property prop; |
| 857 | prop = ext->properties[j]; |
| 858 | |
| 859 | if (ext->get_property(ext, prop, &ret)) |
| 860 | continue; |
| 861 | |
| 862 | switch (prop) { |
| 863 | case POWER_SUPPLY_PROP_PRESENT: |
| 864 | switch (ext->type) { |
| 865 | case POWER_SUPPLY_TYPE_MAINS: |
| 866 | /* AC disconnected */ |
| 867 | if (!ret.intval && di->events.ac_conn) { |
| 868 | di->events.ac_conn = false; |
| 869 | } |
| 870 | /* AC connected */ |
| 871 | else if (ret.intval && !di->events.ac_conn) { |
| 872 | di->events.ac_conn = true; |
| 873 | if (!di->events.usb_conn) |
| 874 | ab8500_btemp_periodic(di, true); |
| 875 | } |
| 876 | break; |
| 877 | case POWER_SUPPLY_TYPE_USB: |
| 878 | /* USB disconnected */ |
| 879 | if (!ret.intval && di->events.usb_conn) { |
| 880 | di->events.usb_conn = false; |
| 881 | } |
| 882 | /* USB connected */ |
| 883 | else if (ret.intval && !di->events.usb_conn) { |
| 884 | di->events.usb_conn = true; |
| 885 | if (!di->events.ac_conn) |
| 886 | ab8500_btemp_periodic(di, true); |
| 887 | } |
| 888 | break; |
| 889 | default: |
| 890 | break; |
| 891 | } |
| 892 | break; |
| 893 | default: |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * ab8500_btemp_external_power_changed() - callback for power supply changes |
| 902 | * @psy: pointer to the structure power_supply |
| 903 | * |
| 904 | * This function is pointing to the function pointer external_power_changed |
| 905 | * of the structure power_supply. |
| 906 | * This function gets executed when there is a change in the external power |
| 907 | * supply to the btemp. |
| 908 | */ |
| 909 | static void ab8500_btemp_external_power_changed(struct power_supply *psy) |
| 910 | { |
| 911 | struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy); |
| 912 | |
| 913 | class_for_each_device(power_supply_class, NULL, |
| 914 | &di->btemp_psy, ab8500_btemp_get_ext_psy_data); |
| 915 | } |
| 916 | |
| 917 | /* ab8500 btemp driver interrupts and their respective isr */ |
| 918 | static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = { |
| 919 | {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler}, |
| 920 | {"BTEMP_LOW", ab8500_btemp_templow_handler}, |
| 921 | {"BTEMP_HIGH", ab8500_btemp_temphigh_handler}, |
| 922 | {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler}, |
| 923 | {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler}, |
| 924 | }; |
| 925 | |
| 926 | #if defined(CONFIG_PM) |
| 927 | static int ab8500_btemp_resume(struct platform_device *pdev) |
| 928 | { |
| 929 | struct ab8500_btemp *di = platform_get_drvdata(pdev); |
| 930 | |
| 931 | ab8500_btemp_periodic(di, true); |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static int ab8500_btemp_suspend(struct platform_device *pdev, |
| 937 | pm_message_t state) |
| 938 | { |
| 939 | struct ab8500_btemp *di = platform_get_drvdata(pdev); |
| 940 | |
| 941 | ab8500_btemp_periodic(di, false); |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | #else |
| 946 | #define ab8500_btemp_suspend NULL |
| 947 | #define ab8500_btemp_resume NULL |
| 948 | #endif |
| 949 | |
| 950 | static int __devexit ab8500_btemp_remove(struct platform_device *pdev) |
| 951 | { |
| 952 | struct ab8500_btemp *di = platform_get_drvdata(pdev); |
| 953 | int i, irq; |
| 954 | |
| 955 | /* Disable interrupts */ |
| 956 | for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { |
| 957 | irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); |
| 958 | free_irq(irq, di); |
| 959 | } |
| 960 | |
| 961 | /* Delete the work queue */ |
| 962 | destroy_workqueue(di->btemp_wq); |
| 963 | |
| 964 | flush_scheduled_work(); |
| 965 | power_supply_unregister(&di->btemp_psy); |
| 966 | platform_set_drvdata(pdev, NULL); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 971 | static char *supply_interface[] = { |
| 972 | "ab8500_chargalg", |
| 973 | "ab8500_fg", |
| 974 | }; |
| 975 | |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 976 | static int __devinit ab8500_btemp_probe(struct platform_device *pdev) |
| 977 | { |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 978 | struct device_node *np = pdev->dev.of_node; |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 979 | struct ab8500_btemp *di; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 980 | int irq, i, ret = 0; |
| 981 | u8 val; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 982 | |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 983 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
| 984 | if (!di) { |
| 985 | dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 986 | return -ENOMEM; |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 987 | } |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 988 | di->bm = pdev->mfd_cell->platform_data; |
| 989 | if (!di->bm) { |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 990 | if (np) { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame^] | 991 | ret = ab8500_bm_of_probe(&pdev->dev, np, &di->bm); |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 992 | if (ret) { |
| 993 | dev_err(&pdev->dev, |
| 994 | "failed to get battery information\n"); |
| 995 | return ret; |
| 996 | } |
| 997 | } else { |
| 998 | dev_err(&pdev->dev, "missing dt node for ab8500_btemp\n"); |
| 999 | return -EINVAL; |
| 1000 | } |
| 1001 | } else { |
| 1002 | dev_info(&pdev->dev, "falling back to legacy platform data\n"); |
| 1003 | } |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1004 | |
| 1005 | /* get parent data */ |
| 1006 | di->dev = &pdev->dev; |
| 1007 | di->parent = dev_get_drvdata(pdev->dev.parent); |
| 1008 | di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); |
| 1009 | |
Jonas Aaberg | f6271b4 | 2012-02-27 11:02:44 +0100 | [diff] [blame] | 1010 | di->initialized = false; |
| 1011 | |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1012 | /* BTEMP supply */ |
| 1013 | di->btemp_psy.name = "ab8500_btemp"; |
| 1014 | di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY; |
| 1015 | di->btemp_psy.properties = ab8500_btemp_props; |
| 1016 | di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props); |
| 1017 | di->btemp_psy.get_property = ab8500_btemp_get_property; |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 1018 | di->btemp_psy.supplied_to = supply_interface; |
| 1019 | di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1020 | di->btemp_psy.external_power_changed = |
| 1021 | ab8500_btemp_external_power_changed; |
| 1022 | |
| 1023 | |
| 1024 | /* Create a work queue for the btemp */ |
| 1025 | di->btemp_wq = |
| 1026 | create_singlethread_workqueue("ab8500_btemp_wq"); |
| 1027 | if (di->btemp_wq == NULL) { |
| 1028 | dev_err(di->dev, "failed to create work queue\n"); |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 1029 | return -ENOMEM; |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* Init work for measuring temperature periodically */ |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 1033 | INIT_DEFERRABLE_WORK(&di->btemp_periodic_work, |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1034 | ab8500_btemp_periodic_work); |
| 1035 | |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1036 | /* Set BTEMP thermal limits. Low and Med are fixed */ |
| 1037 | di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT; |
| 1038 | di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT; |
| 1039 | |
| 1040 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 1041 | AB8500_BTEMP_HIGH_TH, &val); |
| 1042 | if (ret < 0) { |
| 1043 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 1044 | goto free_btemp_wq; |
| 1045 | } |
| 1046 | switch (val) { |
| 1047 | case BTEMP_HIGH_TH_57_0: |
| 1048 | case BTEMP_HIGH_TH_57_1: |
| 1049 | di->btemp_ranges.btemp_high_limit = |
| 1050 | BTEMP_THERMAL_HIGH_LIMIT_57; |
| 1051 | break; |
| 1052 | case BTEMP_HIGH_TH_52: |
| 1053 | di->btemp_ranges.btemp_high_limit = |
| 1054 | BTEMP_THERMAL_HIGH_LIMIT_52; |
| 1055 | break; |
| 1056 | case BTEMP_HIGH_TH_62: |
| 1057 | di->btemp_ranges.btemp_high_limit = |
| 1058 | BTEMP_THERMAL_HIGH_LIMIT_62; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | /* Register BTEMP power supply class */ |
| 1063 | ret = power_supply_register(di->dev, &di->btemp_psy); |
| 1064 | if (ret) { |
| 1065 | dev_err(di->dev, "failed to register BTEMP psy\n"); |
| 1066 | goto free_btemp_wq; |
| 1067 | } |
| 1068 | |
| 1069 | /* Register interrupts */ |
| 1070 | for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { |
| 1071 | irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); |
| 1072 | ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr, |
| 1073 | IRQF_SHARED | IRQF_NO_SUSPEND, |
| 1074 | ab8500_btemp_irq[i].name, di); |
| 1075 | |
| 1076 | if (ret) { |
| 1077 | dev_err(di->dev, "failed to request %s IRQ %d: %d\n" |
| 1078 | , ab8500_btemp_irq[i].name, irq, ret); |
| 1079 | goto free_irq; |
| 1080 | } |
| 1081 | dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", |
| 1082 | ab8500_btemp_irq[i].name, irq, ret); |
| 1083 | } |
| 1084 | |
| 1085 | platform_set_drvdata(pdev, di); |
| 1086 | |
| 1087 | /* Kick off periodic temperature measurements */ |
| 1088 | ab8500_btemp_periodic(di, true); |
| 1089 | list_add_tail(&di->node, &ab8500_btemp_list); |
| 1090 | |
| 1091 | return ret; |
| 1092 | |
| 1093 | free_irq: |
| 1094 | power_supply_unregister(&di->btemp_psy); |
| 1095 | |
| 1096 | /* We also have to free all successfully registered irqs */ |
| 1097 | for (i = i - 1; i >= 0; i--) { |
| 1098 | irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); |
| 1099 | free_irq(irq, di); |
| 1100 | } |
| 1101 | free_btemp_wq: |
| 1102 | destroy_workqueue(di->btemp_wq); |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1103 | return ret; |
| 1104 | } |
| 1105 | |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 1106 | static const struct of_device_id ab8500_btemp_match[] = { |
| 1107 | { .compatible = "stericsson,ab8500-btemp", }, |
| 1108 | { }, |
| 1109 | }; |
| 1110 | |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1111 | static struct platform_driver ab8500_btemp_driver = { |
| 1112 | .probe = ab8500_btemp_probe, |
| 1113 | .remove = __devexit_p(ab8500_btemp_remove), |
| 1114 | .suspend = ab8500_btemp_suspend, |
| 1115 | .resume = ab8500_btemp_resume, |
| 1116 | .driver = { |
| 1117 | .name = "ab8500-btemp", |
| 1118 | .owner = THIS_MODULE, |
Rajanikanth H.V | bd9e8ab | 2012-11-18 19:16:58 -0800 | [diff] [blame] | 1119 | .of_match_table = ab8500_btemp_match, |
Arun Murthy | 1f85582 | 2012-02-29 21:54:28 +0530 | [diff] [blame] | 1120 | }, |
| 1121 | }; |
| 1122 | |
| 1123 | static int __init ab8500_btemp_init(void) |
| 1124 | { |
| 1125 | return platform_driver_register(&ab8500_btemp_driver); |
| 1126 | } |
| 1127 | |
| 1128 | static void __exit ab8500_btemp_exit(void) |
| 1129 | { |
| 1130 | platform_driver_unregister(&ab8500_btemp_driver); |
| 1131 | } |
| 1132 | |
| 1133 | subsys_initcall_sync(ab8500_btemp_init); |
| 1134 | module_exit(ab8500_btemp_exit); |
| 1135 | |
| 1136 | MODULE_LICENSE("GPL v2"); |
| 1137 | MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); |
| 1138 | MODULE_ALIAS("platform:ab8500-btemp"); |
| 1139 | MODULE_DESCRIPTION("AB8500 battery temperature driver"); |