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