Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'for-v3.4-rc1' of git://git.infradead.org/battery-2.6

Pull battery updates from Anton Vorontsov:
"Various small bugfixes and enhancements, plus two new drivers:
- A quite complex ab8500 charger driver, submitted by Arun Murthy @
ST-Ericsson;
- Summit Microelectronics SMB347 Battery Charger, submitted by Bruce
E Robertson and Alan Cox @ Intel.

And that's all."

* tag 'for-v3.4-rc1' of git://git.infradead.org/battery-2.6: (36 commits)
max17042_battery: Clean up interrupt handling
Revert "max8998_charger: Include linux/module.h just once"
ab8500_fg: Fix some build warnings on x86_64
max17042_battery: Fix CHARGE_FULL representation.
max8998_charger: Include linux/module.h just once
power_supply: Convert i2c drivers to module_i2c_driver
lp8727_charger: Add MODULE_DEVICE_TABLE
charger-manager: Simplify charger_get_property(), get rid of a warning
charger-manager: Clean up for better readability
da9052-battery: Convert to use module_platform_driver
da9052-battery: Fix a memory leak when unload the module
da9052-battery: Add missing platform_set_drvdata
ab8500: Turn unneeded global symbols into local ones
ab8500_fg: Fix copy-paste error
ab8500_fg: Get rid of 'struct battery_type'
ab8500_fg: Get rid of 'struct v_to_cap'
ab8500_btemp: Get rid of 'enum adc_therm'
ab8500_charger: Convert to the new USB OTG calls
ab8500-btemp: AB8500 battery temperature driver
ab8500-fg: A8500 fuel gauge driver
...

+11388 -206
+18
Documentation/devicetree/bindings/power_supply/max17042_battery.txt
··· 1 + max17042_battery 2 + ~~~~~~~~~~~~~~~~ 3 + 4 + Required properties : 5 + - compatible : "maxim,max17042" 6 + 7 + Optional properties : 8 + - maxim,rsns-microohm : Resistance of rsns resistor in micro Ohms 9 + (datasheet-recommended value is 10000). 10 + Defining this property enables current-sense functionality. 11 + 12 + Example: 13 + 14 + battery-charger@36 { 15 + compatible = "maxim,max17042"; 16 + reg = <0x36>; 17 + maxim,rsns-microohm = <10000>; 18 + };
+20 -1
drivers/power/Kconfig
··· 249 249 Say Y here to enable support for TWL4030 Battery Charge Interface. 250 250 251 251 config CHARGER_LP8727 252 - tristate "National Semiconductor LP8727 charger driver" 252 + tristate "TI/National Semiconductor LP8727 charger driver" 253 253 depends on I2C 254 254 help 255 255 Say Y here to enable support for LP8727 Charger Driver. ··· 288 288 Say Y to enable support for the battery charger control sysfs and 289 289 platform data of MAX8998/LP3974 PMICs. 290 290 291 + config CHARGER_SMB347 292 + tristate "Summit Microelectronics SMB347 Battery Charger" 293 + depends on I2C 294 + help 295 + Say Y to include support for Summit Microelectronics SMB347 296 + Battery Charger. 297 + 298 + config AB8500_BM 299 + bool "AB8500 Battery Management Driver" 300 + depends on AB8500_CORE && AB8500_GPADC 301 + help 302 + Say Y to include support for AB5500 battery management. 303 + 304 + config AB8500_BATTERY_THERM_ON_BATCTRL 305 + bool "Thermistor connected on BATCTRL ADC" 306 + depends on AB8500_BM 307 + help 308 + Say Y to enable battery temperature measurements using 309 + thermistor connected on BATCTRL ADC. 291 310 endif # POWER_SUPPLY
+2
drivers/power/Makefile
··· 34 34 obj-$(CONFIG_CHARGER_PCF50633) += pcf50633-charger.o 35 35 obj-$(CONFIG_BATTERY_JZ4740) += jz4740-battery.o 36 36 obj-$(CONFIG_BATTERY_INTEL_MID) += intel_mid_battery.o 37 + obj-$(CONFIG_AB8500_BM) += ab8500_charger.o ab8500_btemp.o ab8500_fg.o abx500_chargalg.o 37 38 obj-$(CONFIG_CHARGER_ISP1704) += isp1704_charger.o 38 39 obj-$(CONFIG_CHARGER_MAX8903) += max8903_charger.o 39 40 obj-$(CONFIG_CHARGER_TWL4030) += twl4030_charger.o ··· 43 42 obj-$(CONFIG_CHARGER_MANAGER) += charger-manager.o 44 43 obj-$(CONFIG_CHARGER_MAX8997) += max8997_charger.o 45 44 obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o 45 + obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o
+1124
drivers/power/ab8500_btemp.c
··· 1 + /* 2 + * Copyright (C) ST-Ericsson SA 2012 3 + * 4 + * Battery temperature driver for AB8500 5 + * 6 + * License Terms: GNU General Public License v2 7 + * Author: 8 + * Johan Palsson <johan.palsson@stericsson.com> 9 + * Karl Komierowski <karl.komierowski@stericsson.com> 10 + * Arun R Murthy <arun.murthy@stericsson.com> 11 + */ 12 + 13 + #include <linux/init.h> 14 + #include <linux/module.h> 15 + #include <linux/device.h> 16 + #include <linux/interrupt.h> 17 + #include <linux/delay.h> 18 + #include <linux/slab.h> 19 + #include <linux/platform_device.h> 20 + #include <linux/power_supply.h> 21 + #include <linux/completion.h> 22 + #include <linux/workqueue.h> 23 + #include <linux/mfd/abx500/ab8500.h> 24 + #include <linux/mfd/abx500.h> 25 + #include <linux/mfd/abx500/ab8500-bm.h> 26 + #include <linux/mfd/abx500/ab8500-gpadc.h> 27 + #include <linux/jiffies.h> 28 + 29 + #define VTVOUT_V 1800 30 + 31 + #define BTEMP_THERMAL_LOW_LIMIT -10 32 + #define BTEMP_THERMAL_MED_LIMIT 0 33 + #define BTEMP_THERMAL_HIGH_LIMIT_52 52 34 + #define BTEMP_THERMAL_HIGH_LIMIT_57 57 35 + #define BTEMP_THERMAL_HIGH_LIMIT_62 62 36 + 37 + #define BTEMP_BATCTRL_CURR_SRC_7UA 7 38 + #define BTEMP_BATCTRL_CURR_SRC_20UA 20 39 + 40 + #define to_ab8500_btemp_device_info(x) container_of((x), \ 41 + struct ab8500_btemp, btemp_psy); 42 + 43 + /** 44 + * struct ab8500_btemp_interrupts - ab8500 interrupts 45 + * @name: name of the interrupt 46 + * @isr function pointer to the isr 47 + */ 48 + struct ab8500_btemp_interrupts { 49 + char *name; 50 + irqreturn_t (*isr)(int irq, void *data); 51 + }; 52 + 53 + struct ab8500_btemp_events { 54 + bool batt_rem; 55 + bool btemp_high; 56 + bool btemp_medhigh; 57 + bool btemp_lowmed; 58 + bool btemp_low; 59 + bool ac_conn; 60 + bool usb_conn; 61 + }; 62 + 63 + struct ab8500_btemp_ranges { 64 + int btemp_high_limit; 65 + int btemp_med_limit; 66 + int btemp_low_limit; 67 + }; 68 + 69 + /** 70 + * struct ab8500_btemp - ab8500 BTEMP device information 71 + * @dev: Pointer to the structure device 72 + * @node: List of AB8500 BTEMPs, hence prepared for reentrance 73 + * @curr_source: What current source we use, in uA 74 + * @bat_temp: Battery temperature in degree Celcius 75 + * @prev_bat_temp Last dispatched battery temperature 76 + * @parent: Pointer to the struct ab8500 77 + * @gpadc: Pointer to the struct gpadc 78 + * @fg: Pointer to the struct fg 79 + * @pdata: Pointer to the abx500_btemp platform data 80 + * @bat: Pointer to the abx500_bm platform data 81 + * @btemp_psy: Structure for BTEMP specific battery properties 82 + * @events: Structure for information about events triggered 83 + * @btemp_ranges: Battery temperature range structure 84 + * @btemp_wq: Work queue for measuring the temperature periodically 85 + * @btemp_periodic_work: Work for measuring the temperature periodically 86 + */ 87 + struct ab8500_btemp { 88 + struct device *dev; 89 + struct list_head node; 90 + int curr_source; 91 + int bat_temp; 92 + int prev_bat_temp; 93 + struct ab8500 *parent; 94 + struct ab8500_gpadc *gpadc; 95 + struct ab8500_fg *fg; 96 + struct abx500_btemp_platform_data *pdata; 97 + struct abx500_bm_data *bat; 98 + struct power_supply btemp_psy; 99 + struct ab8500_btemp_events events; 100 + struct ab8500_btemp_ranges btemp_ranges; 101 + struct workqueue_struct *btemp_wq; 102 + struct delayed_work btemp_periodic_work; 103 + }; 104 + 105 + /* BTEMP power supply properties */ 106 + static enum power_supply_property ab8500_btemp_props[] = { 107 + POWER_SUPPLY_PROP_PRESENT, 108 + POWER_SUPPLY_PROP_ONLINE, 109 + POWER_SUPPLY_PROP_TECHNOLOGY, 110 + POWER_SUPPLY_PROP_TEMP, 111 + }; 112 + 113 + static LIST_HEAD(ab8500_btemp_list); 114 + 115 + /** 116 + * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP 117 + * (i.e. the first BTEMP in the instance list) 118 + */ 119 + struct ab8500_btemp *ab8500_btemp_get(void) 120 + { 121 + struct ab8500_btemp *btemp; 122 + btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node); 123 + 124 + return btemp; 125 + } 126 + 127 + /** 128 + * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance 129 + * @di: pointer to the ab8500_btemp structure 130 + * @v_batctrl: measured batctrl voltage 131 + * @inst_curr: measured instant current 132 + * 133 + * This function returns the battery resistance that is 134 + * derived from the BATCTRL voltage. 135 + * Returns value in Ohms. 136 + */ 137 + static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di, 138 + int v_batctrl, int inst_curr) 139 + { 140 + int rbs; 141 + 142 + if (is_ab8500_1p1_or_earlier(di->parent)) { 143 + /* 144 + * For ABB cut1.0 and 1.1 BAT_CTRL is internally 145 + * connected to 1.8V through a 450k resistor 146 + */ 147 + return (450000 * (v_batctrl)) / (1800 - v_batctrl); 148 + } 149 + 150 + if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL) { 151 + /* 152 + * If the battery has internal NTC, we use the current 153 + * source to calculate the resistance, 7uA or 20uA 154 + */ 155 + rbs = (v_batctrl * 1000 156 + - di->bat->gnd_lift_resistance * inst_curr) 157 + / di->curr_source; 158 + } else { 159 + /* 160 + * BAT_CTRL is internally 161 + * connected to 1.8V through a 80k resistor 162 + */ 163 + rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl); 164 + } 165 + 166 + return rbs; 167 + } 168 + 169 + /** 170 + * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage 171 + * @di: pointer to the ab8500_btemp structure 172 + * 173 + * This function returns the voltage on BATCTRL. Returns value in mV. 174 + */ 175 + static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di) 176 + { 177 + int vbtemp; 178 + static int prev; 179 + 180 + vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL); 181 + if (vbtemp < 0) { 182 + dev_err(di->dev, 183 + "%s gpadc conversion failed, using previous value", 184 + __func__); 185 + return prev; 186 + } 187 + prev = vbtemp; 188 + return vbtemp; 189 + } 190 + 191 + /** 192 + * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source 193 + * @di: pointer to the ab8500_btemp structure 194 + * @enable: enable or disable the current source 195 + * 196 + * Enable or disable the current sources for the BatCtrl AD channel 197 + */ 198 + static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di, 199 + bool enable) 200 + { 201 + int curr; 202 + int ret = 0; 203 + 204 + /* 205 + * BATCTRL current sources are included on AB8500 cut2.0 206 + * and future versions 207 + */ 208 + if (is_ab8500_1p1_or_earlier(di->parent)) 209 + return 0; 210 + 211 + /* Only do this for batteries with internal NTC */ 212 + if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) { 213 + if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA) 214 + curr = BAT_CTRL_7U_ENA; 215 + else 216 + curr = BAT_CTRL_20U_ENA; 217 + 218 + dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source); 219 + 220 + ret = abx500_mask_and_set_register_interruptible(di->dev, 221 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 222 + FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH); 223 + if (ret) { 224 + dev_err(di->dev, "%s failed setting cmp_force\n", 225 + __func__); 226 + return ret; 227 + } 228 + 229 + /* 230 + * We have to wait one 32kHz cycle before enabling 231 + * the current source, since ForceBatCtrlCmpHigh needs 232 + * to be written in a separate cycle 233 + */ 234 + udelay(32); 235 + 236 + ret = abx500_set_register_interruptible(di->dev, 237 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 238 + FORCE_BAT_CTRL_CMP_HIGH | curr); 239 + if (ret) { 240 + dev_err(di->dev, "%s failed enabling current source\n", 241 + __func__); 242 + goto disable_curr_source; 243 + } 244 + } else if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) { 245 + dev_dbg(di->dev, "Disable BATCTRL curr source\n"); 246 + 247 + /* Write 0 to the curr bits */ 248 + ret = abx500_mask_and_set_register_interruptible(di->dev, 249 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 250 + BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA, 251 + ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA)); 252 + if (ret) { 253 + dev_err(di->dev, "%s failed disabling current source\n", 254 + __func__); 255 + goto disable_curr_source; 256 + } 257 + 258 + /* Enable Pull-Up and comparator */ 259 + ret = abx500_mask_and_set_register_interruptible(di->dev, 260 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 261 + BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA, 262 + BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA); 263 + if (ret) { 264 + dev_err(di->dev, "%s failed enabling PU and comp\n", 265 + __func__); 266 + goto enable_pu_comp; 267 + } 268 + 269 + /* 270 + * We have to wait one 32kHz cycle before disabling 271 + * ForceBatCtrlCmpHigh since this needs to be written 272 + * in a separate cycle 273 + */ 274 + udelay(32); 275 + 276 + /* Disable 'force comparator' */ 277 + ret = abx500_mask_and_set_register_interruptible(di->dev, 278 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 279 + FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH); 280 + if (ret) { 281 + dev_err(di->dev, "%s failed disabling force comp\n", 282 + __func__); 283 + goto disable_force_comp; 284 + } 285 + } 286 + return ret; 287 + 288 + /* 289 + * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time 290 + * if we got an error above 291 + */ 292 + disable_curr_source: 293 + /* Write 0 to the curr bits */ 294 + ret = abx500_mask_and_set_register_interruptible(di->dev, 295 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 296 + BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA, 297 + ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA)); 298 + if (ret) { 299 + dev_err(di->dev, "%s failed disabling current source\n", 300 + __func__); 301 + return ret; 302 + } 303 + enable_pu_comp: 304 + /* Enable Pull-Up and comparator */ 305 + ret = abx500_mask_and_set_register_interruptible(di->dev, 306 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 307 + BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA, 308 + BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA); 309 + if (ret) { 310 + dev_err(di->dev, "%s failed enabling PU and comp\n", 311 + __func__); 312 + return ret; 313 + } 314 + 315 + disable_force_comp: 316 + /* 317 + * We have to wait one 32kHz cycle before disabling 318 + * ForceBatCtrlCmpHigh since this needs to be written 319 + * in a separate cycle 320 + */ 321 + udelay(32); 322 + 323 + /* Disable 'force comparator' */ 324 + ret = abx500_mask_and_set_register_interruptible(di->dev, 325 + AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 326 + FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH); 327 + if (ret) { 328 + dev_err(di->dev, "%s failed disabling force comp\n", 329 + __func__); 330 + return ret; 331 + } 332 + 333 + return ret; 334 + } 335 + 336 + /** 337 + * ab8500_btemp_get_batctrl_res() - get battery resistance 338 + * @di: pointer to the ab8500_btemp structure 339 + * 340 + * This function returns the battery pack identification resistance. 341 + * Returns value in Ohms. 342 + */ 343 + static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di) 344 + { 345 + int ret; 346 + int batctrl = 0; 347 + int res; 348 + int inst_curr; 349 + int i; 350 + 351 + /* 352 + * BATCTRL current sources are included on AB8500 cut2.0 353 + * and future versions 354 + */ 355 + ret = ab8500_btemp_curr_source_enable(di, true); 356 + if (ret) { 357 + dev_err(di->dev, "%s curr source enabled failed\n", __func__); 358 + return ret; 359 + } 360 + 361 + if (!di->fg) 362 + di->fg = ab8500_fg_get(); 363 + if (!di->fg) { 364 + dev_err(di->dev, "No fg found\n"); 365 + return -EINVAL; 366 + } 367 + 368 + ret = ab8500_fg_inst_curr_start(di->fg); 369 + 370 + if (ret) { 371 + dev_err(di->dev, "Failed to start current measurement\n"); 372 + return ret; 373 + } 374 + 375 + /* 376 + * Since there is no interrupt when current measurement is done, 377 + * loop for over 250ms (250ms is one sample conversion time 378 + * with 32.768 Khz RTC clock). Note that a stop time must be set 379 + * since the ab8500_btemp_read_batctrl_voltage call can block and 380 + * take an unknown amount of time to complete. 381 + */ 382 + i = 0; 383 + 384 + do { 385 + batctrl += ab8500_btemp_read_batctrl_voltage(di); 386 + i++; 387 + msleep(20); 388 + } while (!ab8500_fg_inst_curr_done(di->fg)); 389 + batctrl /= i; 390 + 391 + ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr); 392 + if (ret) { 393 + dev_err(di->dev, "Failed to finalize current measurement\n"); 394 + return ret; 395 + } 396 + 397 + res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr); 398 + 399 + ret = ab8500_btemp_curr_source_enable(di, false); 400 + if (ret) { 401 + dev_err(di->dev, "%s curr source disable failed\n", __func__); 402 + return ret; 403 + } 404 + 405 + dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n", 406 + __func__, batctrl, res, inst_curr, i); 407 + 408 + return res; 409 + } 410 + 411 + /** 412 + * ab8500_btemp_res_to_temp() - resistance to temperature 413 + * @di: pointer to the ab8500_btemp structure 414 + * @tbl: pointer to the resiatance to temperature table 415 + * @tbl_size: size of the resistance to temperature table 416 + * @res: resistance to calculate the temperature from 417 + * 418 + * This function returns the battery temperature in degrees Celcius 419 + * based on the NTC resistance. 420 + */ 421 + static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di, 422 + const struct abx500_res_to_temp *tbl, int tbl_size, int res) 423 + { 424 + int i, temp; 425 + /* 426 + * Calculate the formula for the straight line 427 + * Simple interpolation if we are within 428 + * the resistance table limits, extrapolate 429 + * if resistance is outside the limits. 430 + */ 431 + if (res > tbl[0].resist) 432 + i = 0; 433 + else if (res <= tbl[tbl_size - 1].resist) 434 + i = tbl_size - 2; 435 + else { 436 + i = 0; 437 + while (!(res <= tbl[i].resist && 438 + res > tbl[i + 1].resist)) 439 + i++; 440 + } 441 + 442 + temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) * 443 + (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist); 444 + return temp; 445 + } 446 + 447 + /** 448 + * ab8500_btemp_measure_temp() - measure battery temperature 449 + * @di: pointer to the ab8500_btemp structure 450 + * 451 + * Returns battery temperature (on success) else the previous temperature 452 + */ 453 + static int ab8500_btemp_measure_temp(struct ab8500_btemp *di) 454 + { 455 + int temp; 456 + static int prev; 457 + int rbat, rntc, vntc; 458 + u8 id; 459 + 460 + id = di->bat->batt_id; 461 + 462 + if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && 463 + id != BATTERY_UNKNOWN) { 464 + 465 + rbat = ab8500_btemp_get_batctrl_res(di); 466 + if (rbat < 0) { 467 + dev_err(di->dev, "%s get batctrl res failed\n", 468 + __func__); 469 + /* 470 + * Return out-of-range temperature so that 471 + * charging is stopped 472 + */ 473 + return BTEMP_THERMAL_LOW_LIMIT; 474 + } 475 + 476 + temp = ab8500_btemp_res_to_temp(di, 477 + di->bat->bat_type[id].r_to_t_tbl, 478 + di->bat->bat_type[id].n_temp_tbl_elements, rbat); 479 + } else { 480 + vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL); 481 + if (vntc < 0) { 482 + dev_err(di->dev, 483 + "%s gpadc conversion failed," 484 + " using previous value\n", __func__); 485 + return prev; 486 + } 487 + /* 488 + * The PCB NTC is sourced from VTVOUT via a 230kOhm 489 + * resistor. 490 + */ 491 + rntc = 230000 * vntc / (VTVOUT_V - vntc); 492 + 493 + temp = ab8500_btemp_res_to_temp(di, 494 + di->bat->bat_type[id].r_to_t_tbl, 495 + di->bat->bat_type[id].n_temp_tbl_elements, rntc); 496 + prev = temp; 497 + } 498 + dev_dbg(di->dev, "Battery temperature is %d\n", temp); 499 + return temp; 500 + } 501 + 502 + /** 503 + * ab8500_btemp_id() - Identify the connected battery 504 + * @di: pointer to the ab8500_btemp structure 505 + * 506 + * This function will try to identify the battery by reading the ID 507 + * resistor. Some brands use a combined ID resistor with a NTC resistor to 508 + * both be able to identify and to read the temperature of it. 509 + */ 510 + static int ab8500_btemp_id(struct ab8500_btemp *di) 511 + { 512 + int res; 513 + u8 i; 514 + 515 + di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA; 516 + di->bat->batt_id = BATTERY_UNKNOWN; 517 + 518 + res = ab8500_btemp_get_batctrl_res(di); 519 + if (res < 0) { 520 + dev_err(di->dev, "%s get batctrl res failed\n", __func__); 521 + return -ENXIO; 522 + } 523 + 524 + /* BATTERY_UNKNOWN is defined on position 0, skip it! */ 525 + for (i = BATTERY_UNKNOWN + 1; i < di->bat->n_btypes; i++) { 526 + if ((res <= di->bat->bat_type[i].resis_high) && 527 + (res >= di->bat->bat_type[i].resis_low)) { 528 + dev_dbg(di->dev, "Battery detected on %s" 529 + " low %d < res %d < high: %d" 530 + " index: %d\n", 531 + di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL ? 532 + "BATCTRL" : "BATTEMP", 533 + di->bat->bat_type[i].resis_low, res, 534 + di->bat->bat_type[i].resis_high, i); 535 + 536 + di->bat->batt_id = i; 537 + break; 538 + } 539 + } 540 + 541 + if (di->bat->batt_id == BATTERY_UNKNOWN) { 542 + dev_warn(di->dev, "Battery identified as unknown" 543 + ", resistance %d Ohm\n", res); 544 + return -ENXIO; 545 + } 546 + 547 + /* 548 + * We only have to change current source if the 549 + * detected type is Type 1, else we use the 7uA source 550 + */ 551 + if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && 552 + di->bat->batt_id == 1) { 553 + dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n"); 554 + di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA; 555 + } 556 + 557 + return di->bat->batt_id; 558 + } 559 + 560 + /** 561 + * ab8500_btemp_periodic_work() - Measuring the temperature periodically 562 + * @work: pointer to the work_struct structure 563 + * 564 + * Work function for measuring the temperature periodically 565 + */ 566 + static void ab8500_btemp_periodic_work(struct work_struct *work) 567 + { 568 + int interval; 569 + struct ab8500_btemp *di = container_of(work, 570 + struct ab8500_btemp, btemp_periodic_work.work); 571 + 572 + di->bat_temp = ab8500_btemp_measure_temp(di); 573 + 574 + if (di->bat_temp != di->prev_bat_temp) { 575 + di->prev_bat_temp = di->bat_temp; 576 + power_supply_changed(&di->btemp_psy); 577 + } 578 + 579 + if (di->events.ac_conn || di->events.usb_conn) 580 + interval = di->bat->temp_interval_chg; 581 + else 582 + interval = di->bat->temp_interval_nochg; 583 + 584 + /* Schedule a new measurement */ 585 + queue_delayed_work(di->btemp_wq, 586 + &di->btemp_periodic_work, 587 + round_jiffies(interval * HZ)); 588 + } 589 + 590 + /** 591 + * ab8500_btemp_batctrlindb_handler() - battery removal detected 592 + * @irq: interrupt number 593 + * @_di: void pointer that has to address of ab8500_btemp 594 + * 595 + * Returns IRQ status(IRQ_HANDLED) 596 + */ 597 + static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di) 598 + { 599 + struct ab8500_btemp *di = _di; 600 + dev_err(di->dev, "Battery removal detected!\n"); 601 + 602 + di->events.batt_rem = true; 603 + power_supply_changed(&di->btemp_psy); 604 + 605 + return IRQ_HANDLED; 606 + } 607 + 608 + /** 609 + * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees 610 + * @irq: interrupt number 611 + * @_di: void pointer that has to address of ab8500_btemp 612 + * 613 + * Returns IRQ status(IRQ_HANDLED) 614 + */ 615 + static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di) 616 + { 617 + struct ab8500_btemp *di = _di; 618 + 619 + if (is_ab8500_2p0_or_earlier(di->parent)) { 620 + dev_dbg(di->dev, "Ignore false btemp low irq" 621 + " for ABB cut 1.0, 1.1 and 2.0\n"); 622 + } else { 623 + dev_crit(di->dev, "Battery temperature lower than -10deg c\n"); 624 + 625 + di->events.btemp_low = true; 626 + di->events.btemp_high = false; 627 + di->events.btemp_medhigh = false; 628 + di->events.btemp_lowmed = false; 629 + power_supply_changed(&di->btemp_psy); 630 + } 631 + 632 + return IRQ_HANDLED; 633 + } 634 + 635 + /** 636 + * ab8500_btemp_temphigh_handler() - battery temp higher than max temp 637 + * @irq: interrupt number 638 + * @_di: void pointer that has to address of ab8500_btemp 639 + * 640 + * Returns IRQ status(IRQ_HANDLED) 641 + */ 642 + static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di) 643 + { 644 + struct ab8500_btemp *di = _di; 645 + 646 + dev_crit(di->dev, "Battery temperature is higher than MAX temp\n"); 647 + 648 + di->events.btemp_high = true; 649 + di->events.btemp_medhigh = false; 650 + di->events.btemp_lowmed = false; 651 + di->events.btemp_low = false; 652 + power_supply_changed(&di->btemp_psy); 653 + 654 + return IRQ_HANDLED; 655 + } 656 + 657 + /** 658 + * ab8500_btemp_lowmed_handler() - battery temp between low and medium 659 + * @irq: interrupt number 660 + * @_di: void pointer that has to address of ab8500_btemp 661 + * 662 + * Returns IRQ status(IRQ_HANDLED) 663 + */ 664 + static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di) 665 + { 666 + struct ab8500_btemp *di = _di; 667 + 668 + dev_dbg(di->dev, "Battery temperature is between low and medium\n"); 669 + 670 + di->events.btemp_lowmed = true; 671 + di->events.btemp_medhigh = false; 672 + di->events.btemp_high = false; 673 + di->events.btemp_low = false; 674 + power_supply_changed(&di->btemp_psy); 675 + 676 + return IRQ_HANDLED; 677 + } 678 + 679 + /** 680 + * ab8500_btemp_medhigh_handler() - battery temp between medium and high 681 + * @irq: interrupt number 682 + * @_di: void pointer that has to address of ab8500_btemp 683 + * 684 + * Returns IRQ status(IRQ_HANDLED) 685 + */ 686 + static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di) 687 + { 688 + struct ab8500_btemp *di = _di; 689 + 690 + dev_dbg(di->dev, "Battery temperature is between medium and high\n"); 691 + 692 + di->events.btemp_medhigh = true; 693 + di->events.btemp_lowmed = false; 694 + di->events.btemp_high = false; 695 + di->events.btemp_low = false; 696 + power_supply_changed(&di->btemp_psy); 697 + 698 + return IRQ_HANDLED; 699 + } 700 + 701 + /** 702 + * ab8500_btemp_periodic() - Periodic temperature measurements 703 + * @di: pointer to the ab8500_btemp structure 704 + * @enable: enable or disable periodic temperature measurements 705 + * 706 + * Starts of stops periodic temperature measurements. Periodic measurements 707 + * should only be done when a charger is connected. 708 + */ 709 + static void ab8500_btemp_periodic(struct ab8500_btemp *di, 710 + bool enable) 711 + { 712 + dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n", 713 + enable); 714 + /* 715 + * Make sure a new measurement is done directly by cancelling 716 + * any pending work 717 + */ 718 + cancel_delayed_work_sync(&di->btemp_periodic_work); 719 + 720 + if (enable) 721 + queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0); 722 + } 723 + 724 + /** 725 + * ab8500_btemp_get_temp() - get battery temperature 726 + * @di: pointer to the ab8500_btemp structure 727 + * 728 + * Returns battery temperature 729 + */ 730 + static int ab8500_btemp_get_temp(struct ab8500_btemp *di) 731 + { 732 + int temp = 0; 733 + 734 + /* 735 + * The BTEMP events are not reliabe on AB8500 cut2.0 736 + * and prior versions 737 + */ 738 + if (is_ab8500_2p0_or_earlier(di->parent)) { 739 + temp = di->bat_temp * 10; 740 + } else { 741 + if (di->events.btemp_low) { 742 + if (temp > di->btemp_ranges.btemp_low_limit) 743 + temp = di->btemp_ranges.btemp_low_limit; 744 + else 745 + temp = di->bat_temp * 10; 746 + } else if (di->events.btemp_high) { 747 + if (temp < di->btemp_ranges.btemp_high_limit) 748 + temp = di->btemp_ranges.btemp_high_limit; 749 + else 750 + temp = di->bat_temp * 10; 751 + } else if (di->events.btemp_lowmed) { 752 + if (temp > di->btemp_ranges.btemp_med_limit) 753 + temp = di->btemp_ranges.btemp_med_limit; 754 + else 755 + temp = di->bat_temp * 10; 756 + } else if (di->events.btemp_medhigh) { 757 + if (temp < di->btemp_ranges.btemp_med_limit) 758 + temp = di->btemp_ranges.btemp_med_limit; 759 + else 760 + temp = di->bat_temp * 10; 761 + } else 762 + temp = di->bat_temp * 10; 763 + } 764 + return temp; 765 + } 766 + 767 + /** 768 + * ab8500_btemp_get_batctrl_temp() - get the temperature 769 + * @btemp: pointer to the btemp structure 770 + * 771 + * Returns the batctrl temperature in millidegrees 772 + */ 773 + int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp) 774 + { 775 + return btemp->bat_temp * 1000; 776 + } 777 + 778 + /** 779 + * ab8500_btemp_get_property() - get the btemp properties 780 + * @psy: pointer to the power_supply structure 781 + * @psp: pointer to the power_supply_property structure 782 + * @val: pointer to the power_supply_propval union 783 + * 784 + * This function gets called when an application tries to get the btemp 785 + * properties by reading the sysfs files. 786 + * online: presence of the battery 787 + * present: presence of the battery 788 + * technology: battery technology 789 + * temp: battery temperature 790 + * Returns error code in case of failure else 0(on success) 791 + */ 792 + static int ab8500_btemp_get_property(struct power_supply *psy, 793 + enum power_supply_property psp, 794 + union power_supply_propval *val) 795 + { 796 + struct ab8500_btemp *di; 797 + 798 + di = to_ab8500_btemp_device_info(psy); 799 + 800 + switch (psp) { 801 + case POWER_SUPPLY_PROP_PRESENT: 802 + case POWER_SUPPLY_PROP_ONLINE: 803 + if (di->events.batt_rem) 804 + val->intval = 0; 805 + else 806 + val->intval = 1; 807 + break; 808 + case POWER_SUPPLY_PROP_TECHNOLOGY: 809 + val->intval = di->bat->bat_type[di->bat->batt_id].name; 810 + break; 811 + case POWER_SUPPLY_PROP_TEMP: 812 + val->intval = ab8500_btemp_get_temp(di); 813 + break; 814 + default: 815 + return -EINVAL; 816 + } 817 + return 0; 818 + } 819 + 820 + static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data) 821 + { 822 + struct power_supply *psy; 823 + struct power_supply *ext; 824 + struct ab8500_btemp *di; 825 + union power_supply_propval ret; 826 + int i, j; 827 + bool psy_found = false; 828 + 829 + psy = (struct power_supply *)data; 830 + ext = dev_get_drvdata(dev); 831 + di = to_ab8500_btemp_device_info(psy); 832 + 833 + /* 834 + * For all psy where the name of your driver 835 + * appears in any supplied_to 836 + */ 837 + for (i = 0; i < ext->num_supplicants; i++) { 838 + if (!strcmp(ext->supplied_to[i], psy->name)) 839 + psy_found = true; 840 + } 841 + 842 + if (!psy_found) 843 + return 0; 844 + 845 + /* Go through all properties for the psy */ 846 + for (j = 0; j < ext->num_properties; j++) { 847 + enum power_supply_property prop; 848 + prop = ext->properties[j]; 849 + 850 + if (ext->get_property(ext, prop, &ret)) 851 + continue; 852 + 853 + switch (prop) { 854 + case POWER_SUPPLY_PROP_PRESENT: 855 + switch (ext->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 = to_ab8500_btemp_device_info(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 + #if defined(CONFIG_PM) 918 + static int ab8500_btemp_resume(struct platform_device *pdev) 919 + { 920 + struct ab8500_btemp *di = platform_get_drvdata(pdev); 921 + 922 + ab8500_btemp_periodic(di, true); 923 + 924 + return 0; 925 + } 926 + 927 + static int ab8500_btemp_suspend(struct platform_device *pdev, 928 + pm_message_t state) 929 + { 930 + struct ab8500_btemp *di = platform_get_drvdata(pdev); 931 + 932 + ab8500_btemp_periodic(di, false); 933 + 934 + return 0; 935 + } 936 + #else 937 + #define ab8500_btemp_suspend NULL 938 + #define ab8500_btemp_resume NULL 939 + #endif 940 + 941 + static int __devexit ab8500_btemp_remove(struct platform_device *pdev) 942 + { 943 + struct ab8500_btemp *di = platform_get_drvdata(pdev); 944 + int i, irq; 945 + 946 + /* Disable interrupts */ 947 + for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { 948 + irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 949 + free_irq(irq, di); 950 + } 951 + 952 + /* Delete the work queue */ 953 + destroy_workqueue(di->btemp_wq); 954 + 955 + flush_scheduled_work(); 956 + power_supply_unregister(&di->btemp_psy); 957 + platform_set_drvdata(pdev, NULL); 958 + kfree(di); 959 + 960 + return 0; 961 + } 962 + 963 + static int __devinit ab8500_btemp_probe(struct platform_device *pdev) 964 + { 965 + int irq, i, ret = 0; 966 + u8 val; 967 + struct abx500_bm_plat_data *plat_data; 968 + 969 + struct ab8500_btemp *di = 970 + kzalloc(sizeof(struct ab8500_btemp), GFP_KERNEL); 971 + if (!di) 972 + return -ENOMEM; 973 + 974 + /* get parent data */ 975 + di->dev = &pdev->dev; 976 + di->parent = dev_get_drvdata(pdev->dev.parent); 977 + di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 978 + 979 + /* get btemp specific platform data */ 980 + plat_data = pdev->dev.platform_data; 981 + di->pdata = plat_data->btemp; 982 + if (!di->pdata) { 983 + dev_err(di->dev, "no btemp platform data supplied\n"); 984 + ret = -EINVAL; 985 + goto free_device_info; 986 + } 987 + 988 + /* get battery specific platform data */ 989 + di->bat = plat_data->battery; 990 + if (!di->bat) { 991 + dev_err(di->dev, "no battery platform data supplied\n"); 992 + ret = -EINVAL; 993 + goto free_device_info; 994 + } 995 + 996 + /* BTEMP supply */ 997 + di->btemp_psy.name = "ab8500_btemp"; 998 + di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY; 999 + di->btemp_psy.properties = ab8500_btemp_props; 1000 + di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props); 1001 + di->btemp_psy.get_property = ab8500_btemp_get_property; 1002 + di->btemp_psy.supplied_to = di->pdata->supplied_to; 1003 + di->btemp_psy.num_supplicants = di->pdata->num_supplicants; 1004 + di->btemp_psy.external_power_changed = 1005 + ab8500_btemp_external_power_changed; 1006 + 1007 + 1008 + /* Create a work queue for the btemp */ 1009 + di->btemp_wq = 1010 + create_singlethread_workqueue("ab8500_btemp_wq"); 1011 + if (di->btemp_wq == NULL) { 1012 + dev_err(di->dev, "failed to create work queue\n"); 1013 + goto free_device_info; 1014 + } 1015 + 1016 + /* Init work for measuring temperature periodically */ 1017 + INIT_DELAYED_WORK_DEFERRABLE(&di->btemp_periodic_work, 1018 + ab8500_btemp_periodic_work); 1019 + 1020 + /* Identify the battery */ 1021 + if (ab8500_btemp_id(di) < 0) 1022 + dev_warn(di->dev, "failed to identify the battery\n"); 1023 + 1024 + /* Set BTEMP thermal limits. Low and Med are fixed */ 1025 + di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT; 1026 + di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT; 1027 + 1028 + ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 1029 + AB8500_BTEMP_HIGH_TH, &val); 1030 + if (ret < 0) { 1031 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1032 + goto free_btemp_wq; 1033 + } 1034 + switch (val) { 1035 + case BTEMP_HIGH_TH_57_0: 1036 + case BTEMP_HIGH_TH_57_1: 1037 + di->btemp_ranges.btemp_high_limit = 1038 + BTEMP_THERMAL_HIGH_LIMIT_57; 1039 + break; 1040 + case BTEMP_HIGH_TH_52: 1041 + di->btemp_ranges.btemp_high_limit = 1042 + BTEMP_THERMAL_HIGH_LIMIT_52; 1043 + break; 1044 + case BTEMP_HIGH_TH_62: 1045 + di->btemp_ranges.btemp_high_limit = 1046 + BTEMP_THERMAL_HIGH_LIMIT_62; 1047 + break; 1048 + } 1049 + 1050 + /* Register BTEMP power supply class */ 1051 + ret = power_supply_register(di->dev, &di->btemp_psy); 1052 + if (ret) { 1053 + dev_err(di->dev, "failed to register BTEMP psy\n"); 1054 + goto free_btemp_wq; 1055 + } 1056 + 1057 + /* Register interrupts */ 1058 + for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { 1059 + irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 1060 + ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr, 1061 + IRQF_SHARED | IRQF_NO_SUSPEND, 1062 + ab8500_btemp_irq[i].name, di); 1063 + 1064 + if (ret) { 1065 + dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 1066 + , ab8500_btemp_irq[i].name, irq, ret); 1067 + goto free_irq; 1068 + } 1069 + dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 1070 + ab8500_btemp_irq[i].name, irq, ret); 1071 + } 1072 + 1073 + platform_set_drvdata(pdev, di); 1074 + 1075 + /* Kick off periodic temperature measurements */ 1076 + ab8500_btemp_periodic(di, true); 1077 + list_add_tail(&di->node, &ab8500_btemp_list); 1078 + 1079 + return ret; 1080 + 1081 + free_irq: 1082 + power_supply_unregister(&di->btemp_psy); 1083 + 1084 + /* We also have to free all successfully registered irqs */ 1085 + for (i = i - 1; i >= 0; i--) { 1086 + irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 1087 + free_irq(irq, di); 1088 + } 1089 + free_btemp_wq: 1090 + destroy_workqueue(di->btemp_wq); 1091 + free_device_info: 1092 + kfree(di); 1093 + 1094 + return ret; 1095 + } 1096 + 1097 + static struct platform_driver ab8500_btemp_driver = { 1098 + .probe = ab8500_btemp_probe, 1099 + .remove = __devexit_p(ab8500_btemp_remove), 1100 + .suspend = ab8500_btemp_suspend, 1101 + .resume = ab8500_btemp_resume, 1102 + .driver = { 1103 + .name = "ab8500-btemp", 1104 + .owner = THIS_MODULE, 1105 + }, 1106 + }; 1107 + 1108 + static int __init ab8500_btemp_init(void) 1109 + { 1110 + return platform_driver_register(&ab8500_btemp_driver); 1111 + } 1112 + 1113 + static void __exit ab8500_btemp_exit(void) 1114 + { 1115 + platform_driver_unregister(&ab8500_btemp_driver); 1116 + } 1117 + 1118 + subsys_initcall_sync(ab8500_btemp_init); 1119 + module_exit(ab8500_btemp_exit); 1120 + 1121 + MODULE_LICENSE("GPL v2"); 1122 + MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); 1123 + MODULE_ALIAS("platform:ab8500-btemp"); 1124 + MODULE_DESCRIPTION("AB8500 battery temperature driver");
+2789
drivers/power/ab8500_charger.c
··· 1 + /* 2 + * Copyright (C) ST-Ericsson SA 2012 3 + * 4 + * Charger 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/regulator/consumer.h> 23 + #include <linux/err.h> 24 + #include <linux/workqueue.h> 25 + #include <linux/kobject.h> 26 + #include <linux/mfd/abx500/ab8500.h> 27 + #include <linux/mfd/abx500.h> 28 + #include <linux/mfd/abx500/ab8500-bm.h> 29 + #include <linux/mfd/abx500/ab8500-gpadc.h> 30 + #include <linux/mfd/abx500/ux500_chargalg.h> 31 + #include <linux/usb/otg.h> 32 + 33 + /* Charger constants */ 34 + #define NO_PW_CONN 0 35 + #define AC_PW_CONN 1 36 + #define USB_PW_CONN 2 37 + 38 + #define MAIN_WDOG_ENA 0x01 39 + #define MAIN_WDOG_KICK 0x02 40 + #define MAIN_WDOG_DIS 0x00 41 + #define CHARG_WD_KICK 0x01 42 + #define MAIN_CH_ENA 0x01 43 + #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02 44 + #define USB_CH_ENA 0x01 45 + #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02 46 + #define MAIN_CH_DET 0x01 47 + #define MAIN_CH_CV_ON 0x04 48 + #define USB_CH_CV_ON 0x08 49 + #define VBUS_DET_DBNC100 0x02 50 + #define VBUS_DET_DBNC1 0x01 51 + #define OTP_ENABLE_WD 0x01 52 + 53 + #define MAIN_CH_INPUT_CURR_SHIFT 4 54 + #define VBUS_IN_CURR_LIM_SHIFT 4 55 + 56 + #define LED_INDICATOR_PWM_ENA 0x01 57 + #define LED_INDICATOR_PWM_DIS 0x00 58 + #define LED_IND_CUR_5MA 0x04 59 + #define LED_INDICATOR_PWM_DUTY_252_256 0xBF 60 + 61 + /* HW failure constants */ 62 + #define MAIN_CH_TH_PROT 0x02 63 + #define VBUS_CH_NOK 0x08 64 + #define USB_CH_TH_PROT 0x02 65 + #define VBUS_OVV_TH 0x01 66 + #define MAIN_CH_NOK 0x01 67 + #define VBUS_DET 0x80 68 + 69 + /* UsbLineStatus register bit masks */ 70 + #define AB8500_USB_LINK_STATUS 0x78 71 + #define AB8500_STD_HOST_SUSP 0x18 72 + 73 + /* Watchdog timeout constant */ 74 + #define WD_TIMER 0x30 /* 4min */ 75 + #define WD_KICK_INTERVAL (60 * HZ) 76 + 77 + /* Lowest charger voltage is 3.39V -> 0x4E */ 78 + #define LOW_VOLT_REG 0x4E 79 + 80 + /* UsbLineStatus register - usb types */ 81 + enum ab8500_charger_link_status { 82 + USB_STAT_NOT_CONFIGURED, 83 + USB_STAT_STD_HOST_NC, 84 + USB_STAT_STD_HOST_C_NS, 85 + USB_STAT_STD_HOST_C_S, 86 + USB_STAT_HOST_CHG_NM, 87 + USB_STAT_HOST_CHG_HS, 88 + USB_STAT_HOST_CHG_HS_CHIRP, 89 + USB_STAT_DEDICATED_CHG, 90 + USB_STAT_ACA_RID_A, 91 + USB_STAT_ACA_RID_B, 92 + USB_STAT_ACA_RID_C_NM, 93 + USB_STAT_ACA_RID_C_HS, 94 + USB_STAT_ACA_RID_C_HS_CHIRP, 95 + USB_STAT_HM_IDGND, 96 + USB_STAT_RESERVED, 97 + USB_STAT_NOT_VALID_LINK, 98 + }; 99 + 100 + enum ab8500_usb_state { 101 + AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */ 102 + AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */ 103 + AB8500_BM_USB_STATE_CONFIGURED, 104 + AB8500_BM_USB_STATE_SUSPEND, 105 + AB8500_BM_USB_STATE_RESUME, 106 + AB8500_BM_USB_STATE_MAX, 107 + }; 108 + 109 + /* VBUS input current limits supported in AB8500 in mA */ 110 + #define USB_CH_IP_CUR_LVL_0P05 50 111 + #define USB_CH_IP_CUR_LVL_0P09 98 112 + #define USB_CH_IP_CUR_LVL_0P19 193 113 + #define USB_CH_IP_CUR_LVL_0P29 290 114 + #define USB_CH_IP_CUR_LVL_0P38 380 115 + #define USB_CH_IP_CUR_LVL_0P45 450 116 + #define USB_CH_IP_CUR_LVL_0P5 500 117 + #define USB_CH_IP_CUR_LVL_0P6 600 118 + #define USB_CH_IP_CUR_LVL_0P7 700 119 + #define USB_CH_IP_CUR_LVL_0P8 800 120 + #define USB_CH_IP_CUR_LVL_0P9 900 121 + #define USB_CH_IP_CUR_LVL_1P0 1000 122 + #define USB_CH_IP_CUR_LVL_1P1 1100 123 + #define USB_CH_IP_CUR_LVL_1P3 1300 124 + #define USB_CH_IP_CUR_LVL_1P4 1400 125 + #define USB_CH_IP_CUR_LVL_1P5 1500 126 + 127 + #define VBAT_TRESH_IP_CUR_RED 3800 128 + 129 + #define to_ab8500_charger_usb_device_info(x) container_of((x), \ 130 + struct ab8500_charger, usb_chg) 131 + #define to_ab8500_charger_ac_device_info(x) container_of((x), \ 132 + struct ab8500_charger, ac_chg) 133 + 134 + /** 135 + * struct ab8500_charger_interrupts - ab8500 interupts 136 + * @name: name of the interrupt 137 + * @isr function pointer to the isr 138 + */ 139 + struct ab8500_charger_interrupts { 140 + char *name; 141 + irqreturn_t (*isr)(int irq, void *data); 142 + }; 143 + 144 + struct ab8500_charger_info { 145 + int charger_connected; 146 + int charger_online; 147 + int charger_voltage; 148 + int cv_active; 149 + bool wd_expired; 150 + }; 151 + 152 + struct ab8500_charger_event_flags { 153 + bool mainextchnotok; 154 + bool main_thermal_prot; 155 + bool usb_thermal_prot; 156 + bool vbus_ovv; 157 + bool usbchargernotok; 158 + bool chgwdexp; 159 + bool vbus_collapse; 160 + }; 161 + 162 + struct ab8500_charger_usb_state { 163 + bool usb_changed; 164 + int usb_current; 165 + enum ab8500_usb_state state; 166 + spinlock_t usb_lock; 167 + }; 168 + 169 + /** 170 + * struct ab8500_charger - ab8500 Charger device information 171 + * @dev: Pointer to the structure device 172 + * @max_usb_in_curr: Max USB charger input current 173 + * @vbus_detected: VBUS detected 174 + * @vbus_detected_start: 175 + * VBUS detected during startup 176 + * @ac_conn: This will be true when the AC charger has been plugged 177 + * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC 178 + * charger is enabled 179 + * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB 180 + * charger is enabled 181 + * @vbat Battery voltage 182 + * @old_vbat Previously measured battery voltage 183 + * @autopower Indicate if we should have automatic pwron after pwrloss 184 + * @parent: Pointer to the struct ab8500 185 + * @gpadc: Pointer to the struct gpadc 186 + * @pdata: Pointer to the abx500_charger platform data 187 + * @bat: Pointer to the abx500_bm platform data 188 + * @flags: Structure for information about events triggered 189 + * @usb_state: Structure for usb stack information 190 + * @ac_chg: AC charger power supply 191 + * @usb_chg: USB charger power supply 192 + * @ac: Structure that holds the AC charger properties 193 + * @usb: Structure that holds the USB charger properties 194 + * @regu: Pointer to the struct regulator 195 + * @charger_wq: Work queue for the IRQs and checking HW state 196 + * @check_vbat_work Work for checking vbat threshold to adjust vbus current 197 + * @check_hw_failure_work: Work for checking HW state 198 + * @check_usbchgnotok_work: Work for checking USB charger not ok status 199 + * @kick_wd_work: Work for kicking the charger watchdog in case 200 + * of ABB rev 1.* due to the watchog logic bug 201 + * @ac_work: Work for checking AC charger connection 202 + * @detect_usb_type_work: Work for detecting the USB type connected 203 + * @usb_link_status_work: Work for checking the new USB link status 204 + * @usb_state_changed_work: Work for checking USB state 205 + * @check_main_thermal_prot_work: 206 + * Work for checking Main thermal status 207 + * @check_usb_thermal_prot_work: 208 + * Work for checking USB thermal status 209 + */ 210 + struct ab8500_charger { 211 + struct device *dev; 212 + int max_usb_in_curr; 213 + bool vbus_detected; 214 + bool vbus_detected_start; 215 + bool ac_conn; 216 + bool vddadc_en_ac; 217 + bool vddadc_en_usb; 218 + int vbat; 219 + int old_vbat; 220 + bool autopower; 221 + struct ab8500 *parent; 222 + struct ab8500_gpadc *gpadc; 223 + struct abx500_charger_platform_data *pdata; 224 + struct abx500_bm_data *bat; 225 + struct ab8500_charger_event_flags flags; 226 + struct ab8500_charger_usb_state usb_state; 227 + struct ux500_charger ac_chg; 228 + struct ux500_charger usb_chg; 229 + struct ab8500_charger_info ac; 230 + struct ab8500_charger_info usb; 231 + struct regulator *regu; 232 + struct workqueue_struct *charger_wq; 233 + struct delayed_work check_vbat_work; 234 + struct delayed_work check_hw_failure_work; 235 + struct delayed_work check_usbchgnotok_work; 236 + struct delayed_work kick_wd_work; 237 + struct work_struct ac_work; 238 + struct work_struct detect_usb_type_work; 239 + struct work_struct usb_link_status_work; 240 + struct work_struct usb_state_changed_work; 241 + struct work_struct check_main_thermal_prot_work; 242 + struct work_struct check_usb_thermal_prot_work; 243 + struct usb_phy *usb_phy; 244 + struct notifier_block nb; 245 + }; 246 + 247 + /* AC properties */ 248 + static enum power_supply_property ab8500_charger_ac_props[] = { 249 + POWER_SUPPLY_PROP_HEALTH, 250 + POWER_SUPPLY_PROP_PRESENT, 251 + POWER_SUPPLY_PROP_ONLINE, 252 + POWER_SUPPLY_PROP_VOLTAGE_NOW, 253 + POWER_SUPPLY_PROP_VOLTAGE_AVG, 254 + POWER_SUPPLY_PROP_CURRENT_NOW, 255 + }; 256 + 257 + /* USB properties */ 258 + static enum power_supply_property ab8500_charger_usb_props[] = { 259 + POWER_SUPPLY_PROP_HEALTH, 260 + POWER_SUPPLY_PROP_CURRENT_AVG, 261 + POWER_SUPPLY_PROP_PRESENT, 262 + POWER_SUPPLY_PROP_ONLINE, 263 + POWER_SUPPLY_PROP_VOLTAGE_NOW, 264 + POWER_SUPPLY_PROP_VOLTAGE_AVG, 265 + POWER_SUPPLY_PROP_CURRENT_NOW, 266 + }; 267 + 268 + /** 269 + * ab8500_power_loss_handling - set how we handle powerloss. 270 + * @di: pointer to the ab8500_charger structure 271 + * 272 + * Magic nummbers are from STE HW department. 273 + */ 274 + static void ab8500_power_loss_handling(struct ab8500_charger *di) 275 + { 276 + u8 reg; 277 + int ret; 278 + 279 + dev_dbg(di->dev, "Autopower : %d\n", di->autopower); 280 + 281 + /* read the autopower register */ 282 + ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg); 283 + if (ret) { 284 + dev_err(di->dev, "%d write failed\n", __LINE__); 285 + return; 286 + } 287 + 288 + /* enable the OPT emulation registers */ 289 + ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); 290 + if (ret) { 291 + dev_err(di->dev, "%d write failed\n", __LINE__); 292 + return; 293 + } 294 + 295 + if (di->autopower) 296 + reg |= 0x8; 297 + else 298 + reg &= ~0x8; 299 + 300 + /* write back the changed value to autopower reg */ 301 + ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); 302 + if (ret) { 303 + dev_err(di->dev, "%d write failed\n", __LINE__); 304 + return; 305 + } 306 + 307 + /* disable the set OTP registers again */ 308 + ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); 309 + if (ret) { 310 + dev_err(di->dev, "%d write failed\n", __LINE__); 311 + return; 312 + } 313 + } 314 + 315 + /** 316 + * ab8500_power_supply_changed - a wrapper with local extentions for 317 + * power_supply_changed 318 + * @di: pointer to the ab8500_charger structure 319 + * @psy: pointer to power_supply_that have changed. 320 + * 321 + */ 322 + static void ab8500_power_supply_changed(struct ab8500_charger *di, 323 + struct power_supply *psy) 324 + { 325 + if (di->pdata->autopower_cfg) { 326 + if (!di->usb.charger_connected && 327 + !di->ac.charger_connected && 328 + di->autopower) { 329 + di->autopower = false; 330 + ab8500_power_loss_handling(di); 331 + } else if (!di->autopower && 332 + (di->ac.charger_connected || 333 + di->usb.charger_connected)) { 334 + di->autopower = true; 335 + ab8500_power_loss_handling(di); 336 + } 337 + } 338 + power_supply_changed(psy); 339 + } 340 + 341 + static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, 342 + bool connected) 343 + { 344 + if (connected != di->usb.charger_connected) { 345 + dev_dbg(di->dev, "USB connected:%i\n", connected); 346 + di->usb.charger_connected = connected; 347 + sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); 348 + } 349 + } 350 + 351 + /** 352 + * ab8500_charger_get_ac_voltage() - get ac charger voltage 353 + * @di: pointer to the ab8500_charger structure 354 + * 355 + * Returns ac charger voltage (on success) 356 + */ 357 + static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) 358 + { 359 + int vch; 360 + 361 + /* Only measure voltage if the charger is connected */ 362 + if (di->ac.charger_connected) { 363 + vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); 364 + if (vch < 0) 365 + dev_err(di->dev, "%s gpadc conv failed,\n", __func__); 366 + } else { 367 + vch = 0; 368 + } 369 + return vch; 370 + } 371 + 372 + /** 373 + * ab8500_charger_ac_cv() - check if the main charger is in CV mode 374 + * @di: pointer to the ab8500_charger structure 375 + * 376 + * Returns ac charger CV mode (on success) else error code 377 + */ 378 + static int ab8500_charger_ac_cv(struct ab8500_charger *di) 379 + { 380 + u8 val; 381 + int ret = 0; 382 + 383 + /* Only check CV mode if the charger is online */ 384 + if (di->ac.charger_online) { 385 + ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 386 + AB8500_CH_STATUS1_REG, &val); 387 + if (ret < 0) { 388 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 389 + return 0; 390 + } 391 + 392 + if (val & MAIN_CH_CV_ON) 393 + ret = 1; 394 + else 395 + ret = 0; 396 + } 397 + 398 + return ret; 399 + } 400 + 401 + /** 402 + * ab8500_charger_get_vbus_voltage() - get vbus voltage 403 + * @di: pointer to the ab8500_charger structure 404 + * 405 + * This function returns the vbus voltage. 406 + * Returns vbus voltage (on success) 407 + */ 408 + static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) 409 + { 410 + int vch; 411 + 412 + /* Only measure voltage if the charger is connected */ 413 + if (di->usb.charger_connected) { 414 + vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); 415 + if (vch < 0) 416 + dev_err(di->dev, "%s gpadc conv failed\n", __func__); 417 + } else { 418 + vch = 0; 419 + } 420 + return vch; 421 + } 422 + 423 + /** 424 + * ab8500_charger_get_usb_current() - get usb charger current 425 + * @di: pointer to the ab8500_charger structure 426 + * 427 + * This function returns the usb charger current. 428 + * Returns usb current (on success) and error code on failure 429 + */ 430 + static int ab8500_charger_get_usb_current(struct ab8500_charger *di) 431 + { 432 + int ich; 433 + 434 + /* Only measure current if the charger is online */ 435 + if (di->usb.charger_online) { 436 + ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); 437 + if (ich < 0) 438 + dev_err(di->dev, "%s gpadc conv failed\n", __func__); 439 + } else { 440 + ich = 0; 441 + } 442 + return ich; 443 + } 444 + 445 + /** 446 + * ab8500_charger_get_ac_current() - get ac charger current 447 + * @di: pointer to the ab8500_charger structure 448 + * 449 + * This function returns the ac charger current. 450 + * Returns ac current (on success) and error code on failure. 451 + */ 452 + static int ab8500_charger_get_ac_current(struct ab8500_charger *di) 453 + { 454 + int ich; 455 + 456 + /* Only measure current if the charger is online */ 457 + if (di->ac.charger_online) { 458 + ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); 459 + if (ich < 0) 460 + dev_err(di->dev, "%s gpadc conv failed\n", __func__); 461 + } else { 462 + ich = 0; 463 + } 464 + return ich; 465 + } 466 + 467 + /** 468 + * ab8500_charger_usb_cv() - check if the usb charger is in CV mode 469 + * @di: pointer to the ab8500_charger structure 470 + * 471 + * Returns ac charger CV mode (on success) else error code 472 + */ 473 + static int ab8500_charger_usb_cv(struct ab8500_charger *di) 474 + { 475 + int ret; 476 + u8 val; 477 + 478 + /* Only check CV mode if the charger is online */ 479 + if (di->usb.charger_online) { 480 + ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 481 + AB8500_CH_USBCH_STAT1_REG, &val); 482 + if (ret < 0) { 483 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 484 + return 0; 485 + } 486 + 487 + if (val & USB_CH_CV_ON) 488 + ret = 1; 489 + else 490 + ret = 0; 491 + } else { 492 + ret = 0; 493 + } 494 + 495 + return ret; 496 + } 497 + 498 + /** 499 + * ab8500_charger_detect_chargers() - Detect the connected chargers 500 + * @di: pointer to the ab8500_charger structure 501 + * 502 + * Returns the type of charger connected. 503 + * For USB it will not mean we can actually charge from it 504 + * but that there is a USB cable connected that we have to 505 + * identify. This is used during startup when we don't get 506 + * interrupts of the charger detection 507 + * 508 + * Returns an integer value, that means, 509 + * NO_PW_CONN no power supply is connected 510 + * AC_PW_CONN if the AC power supply is connected 511 + * USB_PW_CONN if the USB power supply is connected 512 + * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected 513 + */ 514 + static int ab8500_charger_detect_chargers(struct ab8500_charger *di) 515 + { 516 + int result = NO_PW_CONN; 517 + int ret; 518 + u8 val; 519 + 520 + /* Check for AC charger */ 521 + ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 522 + AB8500_CH_STATUS1_REG, &val); 523 + if (ret < 0) { 524 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 525 + return ret; 526 + } 527 + 528 + if (val & MAIN_CH_DET) 529 + result = AC_PW_CONN; 530 + 531 + /* Check for USB charger */ 532 + ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 533 + AB8500_CH_USBCH_STAT1_REG, &val); 534 + if (ret < 0) { 535 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 536 + return ret; 537 + } 538 + 539 + if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) 540 + result |= USB_PW_CONN; 541 + 542 + return result; 543 + } 544 + 545 + /** 546 + * ab8500_charger_max_usb_curr() - get the max curr for the USB type 547 + * @di: pointer to the ab8500_charger structure 548 + * @link_status: the identified USB type 549 + * 550 + * Get the maximum current that is allowed to be drawn from the host 551 + * based on the USB type. 552 + * Returns error code in case of failure else 0 on success 553 + */ 554 + static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, 555 + enum ab8500_charger_link_status link_status) 556 + { 557 + int ret = 0; 558 + 559 + switch (link_status) { 560 + case USB_STAT_STD_HOST_NC: 561 + case USB_STAT_STD_HOST_C_NS: 562 + case USB_STAT_STD_HOST_C_S: 563 + dev_dbg(di->dev, "USB Type - Standard host is " 564 + "detected through USB driver\n"); 565 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 566 + break; 567 + case USB_STAT_HOST_CHG_HS_CHIRP: 568 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 569 + break; 570 + case USB_STAT_HOST_CHG_HS: 571 + case USB_STAT_ACA_RID_C_HS: 572 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; 573 + break; 574 + case USB_STAT_ACA_RID_A: 575 + /* 576 + * Dedicated charger level minus maximum current accessory 577 + * can consume (300mA). Closest level is 1100mA 578 + */ 579 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; 580 + break; 581 + case USB_STAT_ACA_RID_B: 582 + /* 583 + * Dedicated charger level minus 120mA (20mA for ACA and 584 + * 100mA for potential accessory). Closest level is 1300mA 585 + */ 586 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; 587 + break; 588 + case USB_STAT_DEDICATED_CHG: 589 + case USB_STAT_HOST_CHG_NM: 590 + case USB_STAT_ACA_RID_C_HS_CHIRP: 591 + case USB_STAT_ACA_RID_C_NM: 592 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; 593 + break; 594 + case USB_STAT_RESERVED: 595 + /* 596 + * This state is used to indicate that VBUS has dropped below 597 + * the detection level 4 times in a row. This is due to the 598 + * charger output current is set to high making the charger 599 + * voltage collapse. This have to be propagated through to 600 + * chargalg. This is done using the property 601 + * POWER_SUPPLY_PROP_CURRENT_AVG = 1 602 + */ 603 + di->flags.vbus_collapse = true; 604 + dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " 605 + "VBUS has collapsed\n"); 606 + ret = -1; 607 + break; 608 + case USB_STAT_HM_IDGND: 609 + case USB_STAT_NOT_CONFIGURED: 610 + case USB_STAT_NOT_VALID_LINK: 611 + dev_err(di->dev, "USB Type - Charging not allowed\n"); 612 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 613 + ret = -ENXIO; 614 + break; 615 + default: 616 + dev_err(di->dev, "USB Type - Unknown\n"); 617 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 618 + ret = -ENXIO; 619 + break; 620 + }; 621 + 622 + dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", 623 + link_status, di->max_usb_in_curr); 624 + 625 + return ret; 626 + } 627 + 628 + /** 629 + * ab8500_charger_read_usb_type() - read the type of usb connected 630 + * @di: pointer to the ab8500_charger structure 631 + * 632 + * Detect the type of the plugged USB 633 + * Returns error code in case of failure else 0 on success 634 + */ 635 + static int ab8500_charger_read_usb_type(struct ab8500_charger *di) 636 + { 637 + int ret; 638 + u8 val; 639 + 640 + ret = abx500_get_register_interruptible(di->dev, 641 + AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); 642 + if (ret < 0) { 643 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 644 + return ret; 645 + } 646 + ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 647 + AB8500_USB_LINE_STAT_REG, &val); 648 + if (ret < 0) { 649 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 650 + return ret; 651 + } 652 + 653 + /* get the USB type */ 654 + val = (val & AB8500_USB_LINK_STATUS) >> 3; 655 + ret = ab8500_charger_max_usb_curr(di, 656 + (enum ab8500_charger_link_status) val); 657 + 658 + return ret; 659 + } 660 + 661 + /** 662 + * ab8500_charger_detect_usb_type() - get the type of usb connected 663 + * @di: pointer to the ab8500_charger structure 664 + * 665 + * Detect the type of the plugged USB 666 + * Returns error code in case of failure else 0 on success 667 + */ 668 + static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) 669 + { 670 + int i, ret; 671 + u8 val; 672 + 673 + /* 674 + * On getting the VBUS rising edge detect interrupt there 675 + * is a 250ms delay after which the register UsbLineStatus 676 + * is filled with valid data. 677 + */ 678 + for (i = 0; i < 10; i++) { 679 + msleep(250); 680 + ret = abx500_get_register_interruptible(di->dev, 681 + AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, 682 + &val); 683 + if (ret < 0) { 684 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 685 + return ret; 686 + } 687 + ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 688 + AB8500_USB_LINE_STAT_REG, &val); 689 + if (ret < 0) { 690 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 691 + return ret; 692 + } 693 + /* 694 + * Until the IT source register is read the UsbLineStatus 695 + * register is not updated, hence doing the same 696 + * Revisit this: 697 + */ 698 + 699 + /* get the USB type */ 700 + val = (val & AB8500_USB_LINK_STATUS) >> 3; 701 + if (val) 702 + break; 703 + } 704 + ret = ab8500_charger_max_usb_curr(di, 705 + (enum ab8500_charger_link_status) val); 706 + 707 + return ret; 708 + } 709 + 710 + /* 711 + * This array maps the raw hex value to charger voltage used by the AB8500 712 + * Values taken from the UM0836 713 + */ 714 + static int ab8500_charger_voltage_map[] = { 715 + 3500 , 716 + 3525 , 717 + 3550 , 718 + 3575 , 719 + 3600 , 720 + 3625 , 721 + 3650 , 722 + 3675 , 723 + 3700 , 724 + 3725 , 725 + 3750 , 726 + 3775 , 727 + 3800 , 728 + 3825 , 729 + 3850 , 730 + 3875 , 731 + 3900 , 732 + 3925 , 733 + 3950 , 734 + 3975 , 735 + 4000 , 736 + 4025 , 737 + 4050 , 738 + 4060 , 739 + 4070 , 740 + 4080 , 741 + 4090 , 742 + 4100 , 743 + 4110 , 744 + 4120 , 745 + 4130 , 746 + 4140 , 747 + 4150 , 748 + 4160 , 749 + 4170 , 750 + 4180 , 751 + 4190 , 752 + 4200 , 753 + 4210 , 754 + 4220 , 755 + 4230 , 756 + 4240 , 757 + 4250 , 758 + 4260 , 759 + 4270 , 760 + 4280 , 761 + 4290 , 762 + 4300 , 763 + 4310 , 764 + 4320 , 765 + 4330 , 766 + 4340 , 767 + 4350 , 768 + 4360 , 769 + 4370 , 770 + 4380 , 771 + 4390 , 772 + 4400 , 773 + 4410 , 774 + 4420 , 775 + 4430 , 776 + 4440 , 777 + 4450 , 778 + 4460 , 779 + 4470 , 780 + 4480 , 781 + 4490 , 782 + 4500 , 783 + 4510 , 784 + 4520 , 785 + 4530 , 786 + 4540 , 787 + 4550 , 788 + 4560 , 789 + 4570 , 790 + 4580 , 791 + 4590 , 792 + 4600 , 793 + }; 794 + 795 + /* 796 + * This array maps the raw hex value to charger current used by the AB8500 797 + * Values taken from the UM0836 798 + */ 799 + static int ab8500_charger_current_map[] = { 800 + 100 , 801 + 200 , 802 + 300 , 803 + 400 , 804 + 500 , 805 + 600 , 806 + 700 , 807 + 800 , 808 + 900 , 809 + 1000 , 810 + 1100 , 811 + 1200 , 812 + 1300 , 813 + 1400 , 814 + 1500 , 815 + }; 816 + 817 + /* 818 + * This array maps the raw hex value to VBUS input current used by the AB8500 819 + * Values taken from the UM0836 820 + */ 821 + static int ab8500_charger_vbus_in_curr_map[] = { 822 + USB_CH_IP_CUR_LVL_0P05, 823 + USB_CH_IP_CUR_LVL_0P09, 824 + USB_CH_IP_CUR_LVL_0P19, 825 + USB_CH_IP_CUR_LVL_0P29, 826 + USB_CH_IP_CUR_LVL_0P38, 827 + USB_CH_IP_CUR_LVL_0P45, 828 + USB_CH_IP_CUR_LVL_0P5, 829 + USB_CH_IP_CUR_LVL_0P6, 830 + USB_CH_IP_CUR_LVL_0P7, 831 + USB_CH_IP_CUR_LVL_0P8, 832 + USB_CH_IP_CUR_LVL_0P9, 833 + USB_CH_IP_CUR_LVL_1P0, 834 + USB_CH_IP_CUR_LVL_1P1, 835 + USB_CH_IP_CUR_LVL_1P3, 836 + USB_CH_IP_CUR_LVL_1P4, 837 + USB_CH_IP_CUR_LVL_1P5, 838 + }; 839 + 840 + static int ab8500_voltage_to_regval(int voltage) 841 + { 842 + int i; 843 + 844 + /* Special case for voltage below 3.5V */ 845 + if (voltage < ab8500_charger_voltage_map[0]) 846 + return LOW_VOLT_REG; 847 + 848 + for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { 849 + if (voltage < ab8500_charger_voltage_map[i]) 850 + return i - 1; 851 + } 852 + 853 + /* If not last element, return error */ 854 + i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; 855 + if (voltage == ab8500_charger_voltage_map[i]) 856 + return i; 857 + else 858 + return -1; 859 + } 860 + 861 + static int ab8500_current_to_regval(int curr) 862 + { 863 + int i; 864 + 865 + if (curr < ab8500_charger_current_map[0]) 866 + return 0; 867 + 868 + for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) { 869 + if (curr < ab8500_charger_current_map[i]) 870 + return i - 1; 871 + } 872 + 873 + /* If not last element, return error */ 874 + i = ARRAY_SIZE(ab8500_charger_current_map) - 1; 875 + if (curr == ab8500_charger_current_map[i]) 876 + return i; 877 + else 878 + return -1; 879 + } 880 + 881 + static int ab8500_vbus_in_curr_to_regval(int curr) 882 + { 883 + int i; 884 + 885 + if (curr < ab8500_charger_vbus_in_curr_map[0]) 886 + return 0; 887 + 888 + for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) { 889 + if (curr < ab8500_charger_vbus_in_curr_map[i]) 890 + return i - 1; 891 + } 892 + 893 + /* If not last element, return error */ 894 + i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1; 895 + if (curr == ab8500_charger_vbus_in_curr_map[i]) 896 + return i; 897 + else 898 + return -1; 899 + } 900 + 901 + /** 902 + * ab8500_charger_get_usb_cur() - get usb current 903 + * @di: pointer to the ab8500_charger structre 904 + * 905 + * The usb stack provides the maximum current that can be drawn from 906 + * the standard usb host. This will be in mA. 907 + * This function converts current in mA to a value that can be written 908 + * to the register. Returns -1 if charging is not allowed 909 + */ 910 + static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) 911 + { 912 + switch (di->usb_state.usb_current) { 913 + case 100: 914 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 915 + break; 916 + case 200: 917 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19; 918 + break; 919 + case 300: 920 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29; 921 + break; 922 + case 400: 923 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38; 924 + break; 925 + case 500: 926 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 927 + break; 928 + default: 929 + di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 930 + return -1; 931 + break; 932 + }; 933 + return 0; 934 + } 935 + 936 + /** 937 + * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit 938 + * @di: pointer to the ab8500_charger structure 939 + * @ich_in: charger input current limit 940 + * 941 + * Sets the current that can be drawn from the USB host 942 + * Returns error code in case of failure else 0(on success) 943 + */ 944 + static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, 945 + int ich_in) 946 + { 947 + int ret; 948 + int input_curr_index; 949 + int min_value; 950 + 951 + /* We should always use to lowest current limit */ 952 + min_value = min(di->bat->chg_params->usb_curr_max, ich_in); 953 + 954 + switch (min_value) { 955 + case 100: 956 + if (di->vbat < VBAT_TRESH_IP_CUR_RED) 957 + min_value = USB_CH_IP_CUR_LVL_0P05; 958 + break; 959 + case 500: 960 + if (di->vbat < VBAT_TRESH_IP_CUR_RED) 961 + min_value = USB_CH_IP_CUR_LVL_0P45; 962 + break; 963 + default: 964 + break; 965 + } 966 + 967 + input_curr_index = ab8500_vbus_in_curr_to_regval(min_value); 968 + if (input_curr_index < 0) { 969 + dev_err(di->dev, "VBUS input current limit too high\n"); 970 + return -ENXIO; 971 + } 972 + 973 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 974 + AB8500_USBCH_IPT_CRNTLVL_REG, 975 + input_curr_index << VBUS_IN_CURR_LIM_SHIFT); 976 + if (ret) 977 + dev_err(di->dev, "%s write failed\n", __func__); 978 + 979 + return ret; 980 + } 981 + 982 + /** 983 + * ab8500_charger_led_en() - turn on/off chargign led 984 + * @di: pointer to the ab8500_charger structure 985 + * @on: flag to turn on/off the chargign led 986 + * 987 + * Power ON/OFF charging LED indication 988 + * Returns error code in case of failure else 0(on success) 989 + */ 990 + static int ab8500_charger_led_en(struct ab8500_charger *di, int on) 991 + { 992 + int ret; 993 + 994 + if (on) { 995 + /* Power ON charging LED indicator, set LED current to 5mA */ 996 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 997 + AB8500_LED_INDICATOR_PWM_CTRL, 998 + (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); 999 + if (ret) { 1000 + dev_err(di->dev, "Power ON LED failed\n"); 1001 + return ret; 1002 + } 1003 + /* LED indicator PWM duty cycle 252/256 */ 1004 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1005 + AB8500_LED_INDICATOR_PWM_DUTY, 1006 + LED_INDICATOR_PWM_DUTY_252_256); 1007 + if (ret) { 1008 + dev_err(di->dev, "Set LED PWM duty cycle failed\n"); 1009 + return ret; 1010 + } 1011 + } else { 1012 + /* Power off charging LED indicator */ 1013 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1014 + AB8500_LED_INDICATOR_PWM_CTRL, 1015 + LED_INDICATOR_PWM_DIS); 1016 + if (ret) { 1017 + dev_err(di->dev, "Power-off LED failed\n"); 1018 + return ret; 1019 + } 1020 + } 1021 + 1022 + return ret; 1023 + } 1024 + 1025 + /** 1026 + * ab8500_charger_ac_en() - enable or disable ac charging 1027 + * @di: pointer to the ab8500_charger structure 1028 + * @enable: enable/disable flag 1029 + * @vset: charging voltage 1030 + * @iset: charging current 1031 + * 1032 + * Enable/Disable AC/Mains charging and turns on/off the charging led 1033 + * respectively. 1034 + **/ 1035 + static int ab8500_charger_ac_en(struct ux500_charger *charger, 1036 + int enable, int vset, int iset) 1037 + { 1038 + int ret; 1039 + int volt_index; 1040 + int curr_index; 1041 + int input_curr_index; 1042 + u8 overshoot = 0; 1043 + 1044 + struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger); 1045 + 1046 + if (enable) { 1047 + /* Check if AC is connected */ 1048 + if (!di->ac.charger_connected) { 1049 + dev_err(di->dev, "AC charger not connected\n"); 1050 + return -ENXIO; 1051 + } 1052 + 1053 + /* Enable AC charging */ 1054 + dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset); 1055 + 1056 + /* 1057 + * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1058 + * will be triggered everytime we enable the VDD ADC supply. 1059 + * This will turn off charging for a short while. 1060 + * It can be avoided by having the supply on when 1061 + * there is a charger enabled. Normally the VDD ADC supply 1062 + * is enabled everytime a GPADC conversion is triggered. We will 1063 + * force it to be enabled from this driver to have 1064 + * the GPADC module independant of the AB8500 chargers 1065 + */ 1066 + if (!di->vddadc_en_ac) { 1067 + regulator_enable(di->regu); 1068 + di->vddadc_en_ac = true; 1069 + } 1070 + 1071 + /* Check if the requested voltage or current is valid */ 1072 + volt_index = ab8500_voltage_to_regval(vset); 1073 + curr_index = ab8500_current_to_regval(iset); 1074 + input_curr_index = ab8500_current_to_regval( 1075 + di->bat->chg_params->ac_curr_max); 1076 + if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { 1077 + dev_err(di->dev, 1078 + "Charger voltage or current too high, " 1079 + "charging not started\n"); 1080 + return -ENXIO; 1081 + } 1082 + 1083 + /* ChVoltLevel: maximum battery charging voltage */ 1084 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1085 + AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1086 + if (ret) { 1087 + dev_err(di->dev, "%s write failed\n", __func__); 1088 + return ret; 1089 + } 1090 + /* MainChInputCurr: current that can be drawn from the charger*/ 1091 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1092 + AB8500_MCH_IPT_CURLVL_REG, 1093 + input_curr_index << MAIN_CH_INPUT_CURR_SHIFT); 1094 + if (ret) { 1095 + dev_err(di->dev, "%s write failed\n", __func__); 1096 + return ret; 1097 + } 1098 + /* ChOutputCurentLevel: protected output current */ 1099 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1100 + AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1101 + if (ret) { 1102 + dev_err(di->dev, "%s write failed\n", __func__); 1103 + return ret; 1104 + } 1105 + 1106 + /* Check if VBAT overshoot control should be enabled */ 1107 + if (!di->bat->enable_overshoot) 1108 + overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; 1109 + 1110 + /* Enable Main Charger */ 1111 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1112 + AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); 1113 + if (ret) { 1114 + dev_err(di->dev, "%s write failed\n", __func__); 1115 + return ret; 1116 + } 1117 + 1118 + /* Power on charging LED indication */ 1119 + ret = ab8500_charger_led_en(di, true); 1120 + if (ret < 0) 1121 + dev_err(di->dev, "failed to enable LED\n"); 1122 + 1123 + di->ac.charger_online = 1; 1124 + } else { 1125 + /* Disable AC charging */ 1126 + if (is_ab8500_1p1_or_earlier(di->parent)) { 1127 + /* 1128 + * For ABB revision 1.0 and 1.1 there is a bug in the 1129 + * watchdog logic. That means we have to continously 1130 + * kick the charger watchdog even when no charger is 1131 + * connected. This is only valid once the AC charger 1132 + * has been enabled. This is a bug that is not handled 1133 + * by the algorithm and the watchdog have to be kicked 1134 + * by the charger driver when the AC charger 1135 + * is disabled 1136 + */ 1137 + if (di->ac_conn) { 1138 + queue_delayed_work(di->charger_wq, 1139 + &di->kick_wd_work, 1140 + round_jiffies(WD_KICK_INTERVAL)); 1141 + } 1142 + 1143 + /* 1144 + * We can't turn off charging completely 1145 + * due to a bug in AB8500 cut1. 1146 + * If we do, charging will not start again. 1147 + * That is why we set the lowest voltage 1148 + * and current possible 1149 + */ 1150 + ret = abx500_set_register_interruptible(di->dev, 1151 + AB8500_CHARGER, 1152 + AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); 1153 + if (ret) { 1154 + dev_err(di->dev, 1155 + "%s write failed\n", __func__); 1156 + return ret; 1157 + } 1158 + 1159 + ret = abx500_set_register_interruptible(di->dev, 1160 + AB8500_CHARGER, 1161 + AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1); 1162 + if (ret) { 1163 + dev_err(di->dev, 1164 + "%s write failed\n", __func__); 1165 + return ret; 1166 + } 1167 + } else { 1168 + ret = abx500_set_register_interruptible(di->dev, 1169 + AB8500_CHARGER, 1170 + AB8500_MCH_CTRL1, 0); 1171 + if (ret) { 1172 + dev_err(di->dev, 1173 + "%s write failed\n", __func__); 1174 + return ret; 1175 + } 1176 + } 1177 + 1178 + ret = ab8500_charger_led_en(di, false); 1179 + if (ret < 0) 1180 + dev_err(di->dev, "failed to disable LED\n"); 1181 + 1182 + di->ac.charger_online = 0; 1183 + di->ac.wd_expired = false; 1184 + 1185 + /* Disable regulator if enabled */ 1186 + if (di->vddadc_en_ac) { 1187 + regulator_disable(di->regu); 1188 + di->vddadc_en_ac = false; 1189 + } 1190 + 1191 + dev_dbg(di->dev, "%s Disabled AC charging\n", __func__); 1192 + } 1193 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 1194 + 1195 + return ret; 1196 + } 1197 + 1198 + /** 1199 + * ab8500_charger_usb_en() - enable usb charging 1200 + * @di: pointer to the ab8500_charger structure 1201 + * @enable: enable/disable flag 1202 + * @vset: charging voltage 1203 + * @ich_out: charger output current 1204 + * 1205 + * Enable/Disable USB charging and turns on/off the charging led respectively. 1206 + * Returns error code in case of failure else 0(on success) 1207 + */ 1208 + static int ab8500_charger_usb_en(struct ux500_charger *charger, 1209 + int enable, int vset, int ich_out) 1210 + { 1211 + int ret; 1212 + int volt_index; 1213 + int curr_index; 1214 + u8 overshoot = 0; 1215 + 1216 + struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger); 1217 + 1218 + if (enable) { 1219 + /* Check if USB is connected */ 1220 + if (!di->usb.charger_connected) { 1221 + dev_err(di->dev, "USB charger not connected\n"); 1222 + return -ENXIO; 1223 + } 1224 + 1225 + /* 1226 + * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1227 + * will be triggered everytime we enable the VDD ADC supply. 1228 + * This will turn off charging for a short while. 1229 + * It can be avoided by having the supply on when 1230 + * there is a charger enabled. Normally the VDD ADC supply 1231 + * is enabled everytime a GPADC conversion is triggered. We will 1232 + * force it to be enabled from this driver to have 1233 + * the GPADC module independant of the AB8500 chargers 1234 + */ 1235 + if (!di->vddadc_en_usb) { 1236 + regulator_enable(di->regu); 1237 + di->vddadc_en_usb = true; 1238 + } 1239 + 1240 + /* Enable USB charging */ 1241 + dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out); 1242 + 1243 + /* Check if the requested voltage or current is valid */ 1244 + volt_index = ab8500_voltage_to_regval(vset); 1245 + curr_index = ab8500_current_to_regval(ich_out); 1246 + if (volt_index < 0 || curr_index < 0) { 1247 + dev_err(di->dev, 1248 + "Charger voltage or current too high, " 1249 + "charging not started\n"); 1250 + return -ENXIO; 1251 + } 1252 + 1253 + /* ChVoltLevel: max voltage upto which battery can be charged */ 1254 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1255 + AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1256 + if (ret) { 1257 + dev_err(di->dev, "%s write failed\n", __func__); 1258 + return ret; 1259 + } 1260 + /* USBChInputCurr: current that can be drawn from the usb */ 1261 + ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1262 + if (ret) { 1263 + dev_err(di->dev, "setting USBChInputCurr failed\n"); 1264 + return ret; 1265 + } 1266 + /* ChOutputCurentLevel: protected output current */ 1267 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1268 + AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1269 + if (ret) { 1270 + dev_err(di->dev, "%s write failed\n", __func__); 1271 + return ret; 1272 + } 1273 + /* Check if VBAT overshoot control should be enabled */ 1274 + if (!di->bat->enable_overshoot) 1275 + overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; 1276 + 1277 + /* Enable USB Charger */ 1278 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1279 + AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); 1280 + if (ret) { 1281 + dev_err(di->dev, "%s write failed\n", __func__); 1282 + return ret; 1283 + } 1284 + 1285 + /* If success power on charging LED indication */ 1286 + ret = ab8500_charger_led_en(di, true); 1287 + if (ret < 0) 1288 + dev_err(di->dev, "failed to enable LED\n"); 1289 + 1290 + queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); 1291 + 1292 + di->usb.charger_online = 1; 1293 + } else { 1294 + /* Disable USB charging */ 1295 + ret = abx500_set_register_interruptible(di->dev, 1296 + AB8500_CHARGER, 1297 + AB8500_USBCH_CTRL1_REG, 0); 1298 + if (ret) { 1299 + dev_err(di->dev, 1300 + "%s write failed\n", __func__); 1301 + return ret; 1302 + } 1303 + 1304 + ret = ab8500_charger_led_en(di, false); 1305 + if (ret < 0) 1306 + dev_err(di->dev, "failed to disable LED\n"); 1307 + 1308 + di->usb.charger_online = 0; 1309 + di->usb.wd_expired = false; 1310 + 1311 + /* Disable regulator if enabled */ 1312 + if (di->vddadc_en_usb) { 1313 + regulator_disable(di->regu); 1314 + di->vddadc_en_usb = false; 1315 + } 1316 + 1317 + dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); 1318 + 1319 + /* Cancel any pending Vbat check work */ 1320 + if (delayed_work_pending(&di->check_vbat_work)) 1321 + cancel_delayed_work(&di->check_vbat_work); 1322 + 1323 + } 1324 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1325 + 1326 + return ret; 1327 + } 1328 + 1329 + /** 1330 + * ab8500_charger_watchdog_kick() - kick charger watchdog 1331 + * @di: pointer to the ab8500_charger structure 1332 + * 1333 + * Kick charger watchdog 1334 + * Returns error code in case of failure else 0(on success) 1335 + */ 1336 + static int ab8500_charger_watchdog_kick(struct ux500_charger *charger) 1337 + { 1338 + int ret; 1339 + struct ab8500_charger *di; 1340 + 1341 + if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1342 + di = to_ab8500_charger_ac_device_info(charger); 1343 + else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1344 + di = to_ab8500_charger_usb_device_info(charger); 1345 + else 1346 + return -ENXIO; 1347 + 1348 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1349 + AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1350 + if (ret) 1351 + dev_err(di->dev, "Failed to kick WD!\n"); 1352 + 1353 + return ret; 1354 + } 1355 + 1356 + /** 1357 + * ab8500_charger_update_charger_current() - update charger current 1358 + * @di: pointer to the ab8500_charger structure 1359 + * 1360 + * Update the charger output current for the specified charger 1361 + * Returns error code in case of failure else 0(on success) 1362 + */ 1363 + static int ab8500_charger_update_charger_current(struct ux500_charger *charger, 1364 + int ich_out) 1365 + { 1366 + int ret; 1367 + int curr_index; 1368 + struct ab8500_charger *di; 1369 + 1370 + if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1371 + di = to_ab8500_charger_ac_device_info(charger); 1372 + else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1373 + di = to_ab8500_charger_usb_device_info(charger); 1374 + else 1375 + return -ENXIO; 1376 + 1377 + curr_index = ab8500_current_to_regval(ich_out); 1378 + if (curr_index < 0) { 1379 + dev_err(di->dev, 1380 + "Charger current too high, " 1381 + "charging not started\n"); 1382 + return -ENXIO; 1383 + } 1384 + 1385 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1386 + AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1387 + if (ret) { 1388 + dev_err(di->dev, "%s write failed\n", __func__); 1389 + return ret; 1390 + } 1391 + 1392 + /* Reset the main and usb drop input current measurement counter */ 1393 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1394 + AB8500_CHARGER_CTRL, 1395 + 0x1); 1396 + if (ret) { 1397 + dev_err(di->dev, "%s write failed\n", __func__); 1398 + return ret; 1399 + } 1400 + 1401 + return ret; 1402 + } 1403 + 1404 + static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data) 1405 + { 1406 + struct power_supply *psy; 1407 + struct power_supply *ext; 1408 + struct ab8500_charger *di; 1409 + union power_supply_propval ret; 1410 + int i, j; 1411 + bool psy_found = false; 1412 + struct ux500_charger *usb_chg; 1413 + 1414 + usb_chg = (struct ux500_charger *)data; 1415 + psy = &usb_chg->psy; 1416 + 1417 + di = to_ab8500_charger_usb_device_info(usb_chg); 1418 + 1419 + ext = dev_get_drvdata(dev); 1420 + 1421 + /* For all psy where the driver name appears in any supplied_to */ 1422 + for (i = 0; i < ext->num_supplicants; i++) { 1423 + if (!strcmp(ext->supplied_to[i], psy->name)) 1424 + psy_found = true; 1425 + } 1426 + 1427 + if (!psy_found) 1428 + return 0; 1429 + 1430 + /* Go through all properties for the psy */ 1431 + for (j = 0; j < ext->num_properties; j++) { 1432 + enum power_supply_property prop; 1433 + prop = ext->properties[j]; 1434 + 1435 + if (ext->get_property(ext, prop, &ret)) 1436 + continue; 1437 + 1438 + switch (prop) { 1439 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1440 + switch (ext->type) { 1441 + case POWER_SUPPLY_TYPE_BATTERY: 1442 + di->vbat = ret.intval / 1000; 1443 + break; 1444 + default: 1445 + break; 1446 + } 1447 + break; 1448 + default: 1449 + break; 1450 + } 1451 + } 1452 + return 0; 1453 + } 1454 + 1455 + /** 1456 + * ab8500_charger_check_vbat_work() - keep vbus current within spec 1457 + * @work pointer to the work_struct structure 1458 + * 1459 + * Due to a asic bug it is necessary to lower the input current to the vbus 1460 + * charger when charging with at some specific levels. This issue is only valid 1461 + * for below a certain battery voltage. This function makes sure that the 1462 + * the allowed current limit isn't exceeded. 1463 + */ 1464 + static void ab8500_charger_check_vbat_work(struct work_struct *work) 1465 + { 1466 + int t = 10; 1467 + struct ab8500_charger *di = container_of(work, 1468 + struct ab8500_charger, check_vbat_work.work); 1469 + 1470 + class_for_each_device(power_supply_class, NULL, 1471 + &di->usb_chg.psy, ab8500_charger_get_ext_psy_data); 1472 + 1473 + /* First run old_vbat is 0. */ 1474 + if (di->old_vbat == 0) 1475 + di->old_vbat = di->vbat; 1476 + 1477 + if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED && 1478 + di->vbat <= VBAT_TRESH_IP_CUR_RED) || 1479 + (di->old_vbat > VBAT_TRESH_IP_CUR_RED && 1480 + di->vbat > VBAT_TRESH_IP_CUR_RED))) { 1481 + 1482 + dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d," 1483 + " old: %d\n", di->max_usb_in_curr, di->vbat, 1484 + di->old_vbat); 1485 + ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1486 + power_supply_changed(&di->usb_chg.psy); 1487 + } 1488 + 1489 + di->old_vbat = di->vbat; 1490 + 1491 + /* 1492 + * No need to check the battery voltage every second when not close to 1493 + * the threshold. 1494 + */ 1495 + if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) && 1496 + (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100))) 1497 + t = 1; 1498 + 1499 + queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ); 1500 + } 1501 + 1502 + /** 1503 + * ab8500_charger_check_hw_failure_work() - check main charger failure 1504 + * @work: pointer to the work_struct structure 1505 + * 1506 + * Work queue function for checking the main charger status 1507 + */ 1508 + static void ab8500_charger_check_hw_failure_work(struct work_struct *work) 1509 + { 1510 + int ret; 1511 + u8 reg_value; 1512 + 1513 + struct ab8500_charger *di = container_of(work, 1514 + struct ab8500_charger, check_hw_failure_work.work); 1515 + 1516 + /* Check if the status bits for HW failure is still active */ 1517 + if (di->flags.mainextchnotok) { 1518 + ret = abx500_get_register_interruptible(di->dev, 1519 + AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1520 + if (ret < 0) { 1521 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1522 + return; 1523 + } 1524 + if (!(reg_value & MAIN_CH_NOK)) { 1525 + di->flags.mainextchnotok = false; 1526 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 1527 + } 1528 + } 1529 + if (di->flags.vbus_ovv) { 1530 + ret = abx500_get_register_interruptible(di->dev, 1531 + AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, 1532 + &reg_value); 1533 + if (ret < 0) { 1534 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1535 + return; 1536 + } 1537 + if (!(reg_value & VBUS_OVV_TH)) { 1538 + di->flags.vbus_ovv = false; 1539 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1540 + } 1541 + } 1542 + /* If we still have a failure, schedule a new check */ 1543 + if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 1544 + queue_delayed_work(di->charger_wq, 1545 + &di->check_hw_failure_work, round_jiffies(HZ)); 1546 + } 1547 + } 1548 + 1549 + /** 1550 + * ab8500_charger_kick_watchdog_work() - kick the watchdog 1551 + * @work: pointer to the work_struct structure 1552 + * 1553 + * Work queue function for kicking the charger watchdog. 1554 + * 1555 + * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 1556 + * logic. That means we have to continously kick the charger 1557 + * watchdog even when no charger is connected. This is only 1558 + * valid once the AC charger has been enabled. This is 1559 + * a bug that is not handled by the algorithm and the 1560 + * watchdog have to be kicked by the charger driver 1561 + * when the AC charger is disabled 1562 + */ 1563 + static void ab8500_charger_kick_watchdog_work(struct work_struct *work) 1564 + { 1565 + int ret; 1566 + 1567 + struct ab8500_charger *di = container_of(work, 1568 + struct ab8500_charger, kick_wd_work.work); 1569 + 1570 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1571 + AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1572 + if (ret) 1573 + dev_err(di->dev, "Failed to kick WD!\n"); 1574 + 1575 + /* Schedule a new watchdog kick */ 1576 + queue_delayed_work(di->charger_wq, 1577 + &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL)); 1578 + } 1579 + 1580 + /** 1581 + * ab8500_charger_ac_work() - work to get and set main charger status 1582 + * @work: pointer to the work_struct structure 1583 + * 1584 + * Work queue function for checking the main charger status 1585 + */ 1586 + static void ab8500_charger_ac_work(struct work_struct *work) 1587 + { 1588 + int ret; 1589 + 1590 + struct ab8500_charger *di = container_of(work, 1591 + struct ab8500_charger, ac_work); 1592 + 1593 + /* 1594 + * Since we can't be sure that the events are received 1595 + * synchronously, we have the check if the main charger is 1596 + * connected by reading the status register 1597 + */ 1598 + ret = ab8500_charger_detect_chargers(di); 1599 + if (ret < 0) 1600 + return; 1601 + 1602 + if (ret & AC_PW_CONN) { 1603 + di->ac.charger_connected = 1; 1604 + di->ac_conn = true; 1605 + } else { 1606 + di->ac.charger_connected = 0; 1607 + } 1608 + 1609 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 1610 + sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 1611 + } 1612 + 1613 + /** 1614 + * ab8500_charger_detect_usb_type_work() - work to detect USB type 1615 + * @work: Pointer to the work_struct structure 1616 + * 1617 + * Detect the type of USB plugged 1618 + */ 1619 + static void ab8500_charger_detect_usb_type_work(struct work_struct *work) 1620 + { 1621 + int ret; 1622 + 1623 + struct ab8500_charger *di = container_of(work, 1624 + struct ab8500_charger, detect_usb_type_work); 1625 + 1626 + /* 1627 + * Since we can't be sure that the events are received 1628 + * synchronously, we have the check if is 1629 + * connected by reading the status register 1630 + */ 1631 + ret = ab8500_charger_detect_chargers(di); 1632 + if (ret < 0) 1633 + return; 1634 + 1635 + if (!(ret & USB_PW_CONN)) { 1636 + di->vbus_detected = 0; 1637 + ab8500_charger_set_usb_connected(di, false); 1638 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1639 + } else { 1640 + di->vbus_detected = 1; 1641 + 1642 + if (is_ab8500_1p1_or_earlier(di->parent)) { 1643 + ret = ab8500_charger_detect_usb_type(di); 1644 + if (!ret) { 1645 + ab8500_charger_set_usb_connected(di, true); 1646 + ab8500_power_supply_changed(di, 1647 + &di->usb_chg.psy); 1648 + } 1649 + } else { 1650 + /* For ABB cut2.0 and onwards we have an IRQ, 1651 + * USB_LINK_STATUS that will be triggered when the USB 1652 + * link status changes. The exception is USB connected 1653 + * during startup. Then we don't get a 1654 + * USB_LINK_STATUS IRQ 1655 + */ 1656 + if (di->vbus_detected_start) { 1657 + di->vbus_detected_start = false; 1658 + ret = ab8500_charger_detect_usb_type(di); 1659 + if (!ret) { 1660 + ab8500_charger_set_usb_connected(di, 1661 + true); 1662 + ab8500_power_supply_changed(di, 1663 + &di->usb_chg.psy); 1664 + } 1665 + } 1666 + } 1667 + } 1668 + } 1669 + 1670 + /** 1671 + * ab8500_charger_usb_link_status_work() - work to detect USB type 1672 + * @work: pointer to the work_struct structure 1673 + * 1674 + * Detect the type of USB plugged 1675 + */ 1676 + static void ab8500_charger_usb_link_status_work(struct work_struct *work) 1677 + { 1678 + int ret; 1679 + 1680 + struct ab8500_charger *di = container_of(work, 1681 + struct ab8500_charger, usb_link_status_work); 1682 + 1683 + /* 1684 + * Since we can't be sure that the events are received 1685 + * synchronously, we have the check if is 1686 + * connected by reading the status register 1687 + */ 1688 + ret = ab8500_charger_detect_chargers(di); 1689 + if (ret < 0) 1690 + return; 1691 + 1692 + if (!(ret & USB_PW_CONN)) { 1693 + di->vbus_detected = 0; 1694 + ab8500_charger_set_usb_connected(di, false); 1695 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1696 + } else { 1697 + di->vbus_detected = 1; 1698 + ret = ab8500_charger_read_usb_type(di); 1699 + if (!ret) { 1700 + /* Update maximum input current */ 1701 + ret = ab8500_charger_set_vbus_in_curr(di, 1702 + di->max_usb_in_curr); 1703 + if (ret) 1704 + return; 1705 + 1706 + ab8500_charger_set_usb_connected(di, true); 1707 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1708 + } else if (ret == -ENXIO) { 1709 + /* No valid charger type detected */ 1710 + ab8500_charger_set_usb_connected(di, false); 1711 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1712 + } 1713 + } 1714 + } 1715 + 1716 + static void ab8500_charger_usb_state_changed_work(struct work_struct *work) 1717 + { 1718 + int ret; 1719 + unsigned long flags; 1720 + 1721 + struct ab8500_charger *di = container_of(work, 1722 + struct ab8500_charger, usb_state_changed_work); 1723 + 1724 + if (!di->vbus_detected) 1725 + return; 1726 + 1727 + spin_lock_irqsave(&di->usb_state.usb_lock, flags); 1728 + di->usb_state.usb_changed = false; 1729 + spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); 1730 + 1731 + /* 1732 + * wait for some time until you get updates from the usb stack 1733 + * and negotiations are completed 1734 + */ 1735 + msleep(250); 1736 + 1737 + if (di->usb_state.usb_changed) 1738 + return; 1739 + 1740 + dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", 1741 + __func__, di->usb_state.state, di->usb_state.usb_current); 1742 + 1743 + switch (di->usb_state.state) { 1744 + case AB8500_BM_USB_STATE_RESET_HS: 1745 + case AB8500_BM_USB_STATE_RESET_FS: 1746 + case AB8500_BM_USB_STATE_SUSPEND: 1747 + case AB8500_BM_USB_STATE_MAX: 1748 + ab8500_charger_set_usb_connected(di, false); 1749 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1750 + break; 1751 + 1752 + case AB8500_BM_USB_STATE_RESUME: 1753 + /* 1754 + * when suspend->resume there should be delay 1755 + * of 1sec for enabling charging 1756 + */ 1757 + msleep(1000); 1758 + /* Intentional fall through */ 1759 + case AB8500_BM_USB_STATE_CONFIGURED: 1760 + /* 1761 + * USB is configured, enable charging with the charging 1762 + * input current obtained from USB driver 1763 + */ 1764 + if (!ab8500_charger_get_usb_cur(di)) { 1765 + /* Update maximum input current */ 1766 + ret = ab8500_charger_set_vbus_in_curr(di, 1767 + di->max_usb_in_curr); 1768 + if (ret) 1769 + return; 1770 + 1771 + ab8500_charger_set_usb_connected(di, true); 1772 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1773 + } 1774 + break; 1775 + 1776 + default: 1777 + break; 1778 + }; 1779 + } 1780 + 1781 + /** 1782 + * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status 1783 + * @work: pointer to the work_struct structure 1784 + * 1785 + * Work queue function for checking the USB charger Not OK status 1786 + */ 1787 + static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work) 1788 + { 1789 + int ret; 1790 + u8 reg_value; 1791 + bool prev_status; 1792 + 1793 + struct ab8500_charger *di = container_of(work, 1794 + struct ab8500_charger, check_usbchgnotok_work.work); 1795 + 1796 + /* Check if the status bit for usbchargernotok is still active */ 1797 + ret = abx500_get_register_interruptible(di->dev, 1798 + AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1799 + if (ret < 0) { 1800 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1801 + return; 1802 + } 1803 + prev_status = di->flags.usbchargernotok; 1804 + 1805 + if (reg_value & VBUS_CH_NOK) { 1806 + di->flags.usbchargernotok = true; 1807 + /* Check again in 1sec */ 1808 + queue_delayed_work(di->charger_wq, 1809 + &di->check_usbchgnotok_work, HZ); 1810 + } else { 1811 + di->flags.usbchargernotok = false; 1812 + di->flags.vbus_collapse = false; 1813 + } 1814 + 1815 + if (prev_status != di->flags.usbchargernotok) 1816 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1817 + } 1818 + 1819 + /** 1820 + * ab8500_charger_check_main_thermal_prot_work() - check main thermal status 1821 + * @work: pointer to the work_struct structure 1822 + * 1823 + * Work queue function for checking the Main thermal prot status 1824 + */ 1825 + static void ab8500_charger_check_main_thermal_prot_work( 1826 + struct work_struct *work) 1827 + { 1828 + int ret; 1829 + u8 reg_value; 1830 + 1831 + struct ab8500_charger *di = container_of(work, 1832 + struct ab8500_charger, check_main_thermal_prot_work); 1833 + 1834 + /* Check if the status bit for main_thermal_prot is still active */ 1835 + ret = abx500_get_register_interruptible(di->dev, 1836 + AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1837 + if (ret < 0) { 1838 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1839 + return; 1840 + } 1841 + if (reg_value & MAIN_CH_TH_PROT) 1842 + di->flags.main_thermal_prot = true; 1843 + else 1844 + di->flags.main_thermal_prot = false; 1845 + 1846 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 1847 + } 1848 + 1849 + /** 1850 + * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status 1851 + * @work: pointer to the work_struct structure 1852 + * 1853 + * Work queue function for checking the USB thermal prot status 1854 + */ 1855 + static void ab8500_charger_check_usb_thermal_prot_work( 1856 + struct work_struct *work) 1857 + { 1858 + int ret; 1859 + u8 reg_value; 1860 + 1861 + struct ab8500_charger *di = container_of(work, 1862 + struct ab8500_charger, check_usb_thermal_prot_work); 1863 + 1864 + /* Check if the status bit for usb_thermal_prot is still active */ 1865 + ret = abx500_get_register_interruptible(di->dev, 1866 + AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1867 + if (ret < 0) { 1868 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1869 + return; 1870 + } 1871 + if (reg_value & USB_CH_TH_PROT) 1872 + di->flags.usb_thermal_prot = true; 1873 + else 1874 + di->flags.usb_thermal_prot = false; 1875 + 1876 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 1877 + } 1878 + 1879 + /** 1880 + * ab8500_charger_mainchunplugdet_handler() - main charger unplugged 1881 + * @irq: interrupt number 1882 + * @_di: pointer to the ab8500_charger structure 1883 + * 1884 + * Returns IRQ status(IRQ_HANDLED) 1885 + */ 1886 + static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) 1887 + { 1888 + struct ab8500_charger *di = _di; 1889 + 1890 + dev_dbg(di->dev, "Main charger unplugged\n"); 1891 + queue_work(di->charger_wq, &di->ac_work); 1892 + 1893 + return IRQ_HANDLED; 1894 + } 1895 + 1896 + /** 1897 + * ab8500_charger_mainchplugdet_handler() - main charger plugged 1898 + * @irq: interrupt number 1899 + * @_di: pointer to the ab8500_charger structure 1900 + * 1901 + * Returns IRQ status(IRQ_HANDLED) 1902 + */ 1903 + static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) 1904 + { 1905 + struct ab8500_charger *di = _di; 1906 + 1907 + dev_dbg(di->dev, "Main charger plugged\n"); 1908 + queue_work(di->charger_wq, &di->ac_work); 1909 + 1910 + return IRQ_HANDLED; 1911 + } 1912 + 1913 + /** 1914 + * ab8500_charger_mainextchnotok_handler() - main charger not ok 1915 + * @irq: interrupt number 1916 + * @_di: pointer to the ab8500_charger structure 1917 + * 1918 + * Returns IRQ status(IRQ_HANDLED) 1919 + */ 1920 + static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di) 1921 + { 1922 + struct ab8500_charger *di = _di; 1923 + 1924 + dev_dbg(di->dev, "Main charger not ok\n"); 1925 + di->flags.mainextchnotok = true; 1926 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 1927 + 1928 + /* Schedule a new HW failure check */ 1929 + queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 1930 + 1931 + return IRQ_HANDLED; 1932 + } 1933 + 1934 + /** 1935 + * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger 1936 + * thermal protection threshold 1937 + * @irq: interrupt number 1938 + * @_di: pointer to the ab8500_charger structure 1939 + * 1940 + * Returns IRQ status(IRQ_HANDLED) 1941 + */ 1942 + static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di) 1943 + { 1944 + struct ab8500_charger *di = _di; 1945 + 1946 + dev_dbg(di->dev, 1947 + "Die temp above Main charger thermal protection threshold\n"); 1948 + queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1949 + 1950 + return IRQ_HANDLED; 1951 + } 1952 + 1953 + /** 1954 + * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger 1955 + * thermal protection threshold 1956 + * @irq: interrupt number 1957 + * @_di: pointer to the ab8500_charger structure 1958 + * 1959 + * Returns IRQ status(IRQ_HANDLED) 1960 + */ 1961 + static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) 1962 + { 1963 + struct ab8500_charger *di = _di; 1964 + 1965 + dev_dbg(di->dev, 1966 + "Die temp ok for Main charger thermal protection threshold\n"); 1967 + queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1968 + 1969 + return IRQ_HANDLED; 1970 + } 1971 + 1972 + /** 1973 + * ab8500_charger_vbusdetf_handler() - VBUS falling detected 1974 + * @irq: interrupt number 1975 + * @_di: pointer to the ab8500_charger structure 1976 + * 1977 + * Returns IRQ status(IRQ_HANDLED) 1978 + */ 1979 + static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) 1980 + { 1981 + struct ab8500_charger *di = _di; 1982 + 1983 + dev_dbg(di->dev, "VBUS falling detected\n"); 1984 + queue_work(di->charger_wq, &di->detect_usb_type_work); 1985 + 1986 + return IRQ_HANDLED; 1987 + } 1988 + 1989 + /** 1990 + * ab8500_charger_vbusdetr_handler() - VBUS rising detected 1991 + * @irq: interrupt number 1992 + * @_di: pointer to the ab8500_charger structure 1993 + * 1994 + * Returns IRQ status(IRQ_HANDLED) 1995 + */ 1996 + static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) 1997 + { 1998 + struct ab8500_charger *di = _di; 1999 + 2000 + di->vbus_detected = true; 2001 + dev_dbg(di->dev, "VBUS rising detected\n"); 2002 + queue_work(di->charger_wq, &di->detect_usb_type_work); 2003 + 2004 + return IRQ_HANDLED; 2005 + } 2006 + 2007 + /** 2008 + * ab8500_charger_usblinkstatus_handler() - USB link status has changed 2009 + * @irq: interrupt number 2010 + * @_di: pointer to the ab8500_charger structure 2011 + * 2012 + * Returns IRQ status(IRQ_HANDLED) 2013 + */ 2014 + static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di) 2015 + { 2016 + struct ab8500_charger *di = _di; 2017 + 2018 + dev_dbg(di->dev, "USB link status changed\n"); 2019 + 2020 + queue_work(di->charger_wq, &di->usb_link_status_work); 2021 + 2022 + return IRQ_HANDLED; 2023 + } 2024 + 2025 + /** 2026 + * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger 2027 + * thermal protection threshold 2028 + * @irq: interrupt number 2029 + * @_di: pointer to the ab8500_charger structure 2030 + * 2031 + * Returns IRQ status(IRQ_HANDLED) 2032 + */ 2033 + static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di) 2034 + { 2035 + struct ab8500_charger *di = _di; 2036 + 2037 + dev_dbg(di->dev, 2038 + "Die temp above USB charger thermal protection threshold\n"); 2039 + queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2040 + 2041 + return IRQ_HANDLED; 2042 + } 2043 + 2044 + /** 2045 + * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger 2046 + * thermal protection threshold 2047 + * @irq: interrupt number 2048 + * @_di: pointer to the ab8500_charger structure 2049 + * 2050 + * Returns IRQ status(IRQ_HANDLED) 2051 + */ 2052 + static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di) 2053 + { 2054 + struct ab8500_charger *di = _di; 2055 + 2056 + dev_dbg(di->dev, 2057 + "Die temp ok for USB charger thermal protection threshold\n"); 2058 + queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2059 + 2060 + return IRQ_HANDLED; 2061 + } 2062 + 2063 + /** 2064 + * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected 2065 + * @irq: interrupt number 2066 + * @_di: pointer to the ab8500_charger structure 2067 + * 2068 + * Returns IRQ status(IRQ_HANDLED) 2069 + */ 2070 + static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di) 2071 + { 2072 + struct ab8500_charger *di = _di; 2073 + 2074 + dev_dbg(di->dev, "Not allowed USB charger detected\n"); 2075 + queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0); 2076 + 2077 + return IRQ_HANDLED; 2078 + } 2079 + 2080 + /** 2081 + * ab8500_charger_chwdexp_handler() - Charger watchdog expired 2082 + * @irq: interrupt number 2083 + * @_di: pointer to the ab8500_charger structure 2084 + * 2085 + * Returns IRQ status(IRQ_HANDLED) 2086 + */ 2087 + static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) 2088 + { 2089 + struct ab8500_charger *di = _di; 2090 + 2091 + dev_dbg(di->dev, "Charger watchdog expired\n"); 2092 + 2093 + /* 2094 + * The charger that was online when the watchdog expired 2095 + * needs to be restarted for charging to start again 2096 + */ 2097 + if (di->ac.charger_online) { 2098 + di->ac.wd_expired = true; 2099 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 2100 + } 2101 + if (di->usb.charger_online) { 2102 + di->usb.wd_expired = true; 2103 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 2104 + } 2105 + 2106 + return IRQ_HANDLED; 2107 + } 2108 + 2109 + /** 2110 + * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected 2111 + * @irq: interrupt number 2112 + * @_di: pointer to the ab8500_charger structure 2113 + * 2114 + * Returns IRQ status(IRQ_HANDLED) 2115 + */ 2116 + static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di) 2117 + { 2118 + struct ab8500_charger *di = _di; 2119 + 2120 + dev_dbg(di->dev, "VBUS overvoltage detected\n"); 2121 + di->flags.vbus_ovv = true; 2122 + ab8500_power_supply_changed(di, &di->usb_chg.psy); 2123 + 2124 + /* Schedule a new HW failure check */ 2125 + queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 2126 + 2127 + return IRQ_HANDLED; 2128 + } 2129 + 2130 + /** 2131 + * ab8500_charger_ac_get_property() - get the ac/mains properties 2132 + * @psy: pointer to the power_supply structure 2133 + * @psp: pointer to the power_supply_property structure 2134 + * @val: pointer to the power_supply_propval union 2135 + * 2136 + * This function gets called when an application tries to get the ac/mains 2137 + * properties by reading the sysfs files. 2138 + * AC/Mains properties are online, present and voltage. 2139 + * online: ac/mains charging is in progress or not 2140 + * present: presence of the ac/mains 2141 + * voltage: AC/Mains voltage 2142 + * Returns error code in case of failure else 0(on success) 2143 + */ 2144 + static int ab8500_charger_ac_get_property(struct power_supply *psy, 2145 + enum power_supply_property psp, 2146 + union power_supply_propval *val) 2147 + { 2148 + struct ab8500_charger *di; 2149 + 2150 + di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); 2151 + 2152 + switch (psp) { 2153 + case POWER_SUPPLY_PROP_HEALTH: 2154 + if (di->flags.mainextchnotok) 2155 + val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2156 + else if (di->ac.wd_expired || di->usb.wd_expired) 2157 + val->intval = POWER_SUPPLY_HEALTH_DEAD; 2158 + else if (di->flags.main_thermal_prot) 2159 + val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2160 + else 2161 + val->intval = POWER_SUPPLY_HEALTH_GOOD; 2162 + break; 2163 + case POWER_SUPPLY_PROP_ONLINE: 2164 + val->intval = di->ac.charger_online; 2165 + break; 2166 + case POWER_SUPPLY_PROP_PRESENT: 2167 + val->intval = di->ac.charger_connected; 2168 + break; 2169 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2170 + di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di); 2171 + val->intval = di->ac.charger_voltage * 1000; 2172 + break; 2173 + case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2174 + /* 2175 + * This property is used to indicate when CV mode is entered 2176 + * for the AC charger 2177 + */ 2178 + di->ac.cv_active = ab8500_charger_ac_cv(di); 2179 + val->intval = di->ac.cv_active; 2180 + break; 2181 + case POWER_SUPPLY_PROP_CURRENT_NOW: 2182 + val->intval = ab8500_charger_get_ac_current(di) * 1000; 2183 + break; 2184 + default: 2185 + return -EINVAL; 2186 + } 2187 + return 0; 2188 + } 2189 + 2190 + /** 2191 + * ab8500_charger_usb_get_property() - get the usb properties 2192 + * @psy: pointer to the power_supply structure 2193 + * @psp: pointer to the power_supply_property structure 2194 + * @val: pointer to the power_supply_propval union 2195 + * 2196 + * This function gets called when an application tries to get the usb 2197 + * properties by reading the sysfs files. 2198 + * USB properties are online, present and voltage. 2199 + * online: usb charging is in progress or not 2200 + * present: presence of the usb 2201 + * voltage: vbus voltage 2202 + * Returns error code in case of failure else 0(on success) 2203 + */ 2204 + static int ab8500_charger_usb_get_property(struct power_supply *psy, 2205 + enum power_supply_property psp, 2206 + union power_supply_propval *val) 2207 + { 2208 + struct ab8500_charger *di; 2209 + 2210 + di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); 2211 + 2212 + switch (psp) { 2213 + case POWER_SUPPLY_PROP_HEALTH: 2214 + if (di->flags.usbchargernotok) 2215 + val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2216 + else if (di->ac.wd_expired || di->usb.wd_expired) 2217 + val->intval = POWER_SUPPLY_HEALTH_DEAD; 2218 + else if (di->flags.usb_thermal_prot) 2219 + val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2220 + else if (di->flags.vbus_ovv) 2221 + val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 2222 + else 2223 + val->intval = POWER_SUPPLY_HEALTH_GOOD; 2224 + break; 2225 + case POWER_SUPPLY_PROP_ONLINE: 2226 + val->intval = di->usb.charger_online; 2227 + break; 2228 + case POWER_SUPPLY_PROP_PRESENT: 2229 + val->intval = di->usb.charger_connected; 2230 + break; 2231 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2232 + di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di); 2233 + val->intval = di->usb.charger_voltage * 1000; 2234 + break; 2235 + case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2236 + /* 2237 + * This property is used to indicate when CV mode is entered 2238 + * for the USB charger 2239 + */ 2240 + di->usb.cv_active = ab8500_charger_usb_cv(di); 2241 + val->intval = di->usb.cv_active; 2242 + break; 2243 + case POWER_SUPPLY_PROP_CURRENT_NOW: 2244 + val->intval = ab8500_charger_get_usb_current(di) * 1000; 2245 + break; 2246 + case POWER_SUPPLY_PROP_CURRENT_AVG: 2247 + /* 2248 + * This property is used to indicate when VBUS has collapsed 2249 + * due to too high output current from the USB charger 2250 + */ 2251 + if (di->flags.vbus_collapse) 2252 + val->intval = 1; 2253 + else 2254 + val->intval = 0; 2255 + break; 2256 + default: 2257 + return -EINVAL; 2258 + } 2259 + return 0; 2260 + } 2261 + 2262 + /** 2263 + * ab8500_charger_init_hw_registers() - Set up charger related registers 2264 + * @di: pointer to the ab8500_charger structure 2265 + * 2266 + * Set up charger OVV, watchdog and maximum voltage registers as well as 2267 + * charging of the backup battery 2268 + */ 2269 + static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) 2270 + { 2271 + int ret = 0; 2272 + 2273 + /* Setup maximum charger current and voltage for ABB cut2.0 */ 2274 + if (!is_ab8500_1p1_or_earlier(di->parent)) { 2275 + ret = abx500_set_register_interruptible(di->dev, 2276 + AB8500_CHARGER, 2277 + AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6); 2278 + if (ret) { 2279 + dev_err(di->dev, 2280 + "failed to set CH_VOLT_LVL_MAX_REG\n"); 2281 + goto out; 2282 + } 2283 + 2284 + ret = abx500_set_register_interruptible(di->dev, 2285 + AB8500_CHARGER, 2286 + AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6); 2287 + if (ret) { 2288 + dev_err(di->dev, 2289 + "failed to set CH_OPT_CRNTLVL_MAX_REG\n"); 2290 + goto out; 2291 + } 2292 + } 2293 + 2294 + /* VBUS OVV set to 6.3V and enable automatic current limitiation */ 2295 + ret = abx500_set_register_interruptible(di->dev, 2296 + AB8500_CHARGER, 2297 + AB8500_USBCH_CTRL2_REG, 2298 + VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); 2299 + if (ret) { 2300 + dev_err(di->dev, "failed to set VBUS OVV\n"); 2301 + goto out; 2302 + } 2303 + 2304 + /* Enable main watchdog in OTP */ 2305 + ret = abx500_set_register_interruptible(di->dev, 2306 + AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD); 2307 + if (ret) { 2308 + dev_err(di->dev, "failed to enable main WD in OTP\n"); 2309 + goto out; 2310 + } 2311 + 2312 + /* Enable main watchdog */ 2313 + ret = abx500_set_register_interruptible(di->dev, 2314 + AB8500_SYS_CTRL2_BLOCK, 2315 + AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA); 2316 + if (ret) { 2317 + dev_err(di->dev, "faile to enable main watchdog\n"); 2318 + goto out; 2319 + } 2320 + 2321 + /* 2322 + * Due to internal synchronisation, Enable and Kick watchdog bits 2323 + * cannot be enabled in a single write. 2324 + * A minimum delay of 2*32 kHz period (62.5µs) must be inserted 2325 + * between writing Enable then Kick bits. 2326 + */ 2327 + udelay(63); 2328 + 2329 + /* Kick main watchdog */ 2330 + ret = abx500_set_register_interruptible(di->dev, 2331 + AB8500_SYS_CTRL2_BLOCK, 2332 + AB8500_MAIN_WDOG_CTRL_REG, 2333 + (MAIN_WDOG_ENA | MAIN_WDOG_KICK)); 2334 + if (ret) { 2335 + dev_err(di->dev, "failed to kick main watchdog\n"); 2336 + goto out; 2337 + } 2338 + 2339 + /* Disable main watchdog */ 2340 + ret = abx500_set_register_interruptible(di->dev, 2341 + AB8500_SYS_CTRL2_BLOCK, 2342 + AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS); 2343 + if (ret) { 2344 + dev_err(di->dev, "failed to disable main watchdog\n"); 2345 + goto out; 2346 + } 2347 + 2348 + /* Set watchdog timeout */ 2349 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2350 + AB8500_CH_WD_TIMER_REG, WD_TIMER); 2351 + if (ret) { 2352 + dev_err(di->dev, "failed to set charger watchdog timeout\n"); 2353 + goto out; 2354 + } 2355 + 2356 + /* Backup battery voltage and current */ 2357 + ret = abx500_set_register_interruptible(di->dev, 2358 + AB8500_RTC, 2359 + AB8500_RTC_BACKUP_CHG_REG, 2360 + di->bat->bkup_bat_v | 2361 + di->bat->bkup_bat_i); 2362 + if (ret) { 2363 + dev_err(di->dev, "failed to setup backup battery charging\n"); 2364 + goto out; 2365 + } 2366 + 2367 + /* Enable backup battery charging */ 2368 + abx500_mask_and_set_register_interruptible(di->dev, 2369 + AB8500_RTC, AB8500_RTC_CTRL_REG, 2370 + RTC_BUP_CH_ENA, RTC_BUP_CH_ENA); 2371 + if (ret < 0) 2372 + dev_err(di->dev, "%s mask and set failed\n", __func__); 2373 + 2374 + out: 2375 + return ret; 2376 + } 2377 + 2378 + /* 2379 + * ab8500 charger driver interrupts and their respective isr 2380 + */ 2381 + static struct ab8500_charger_interrupts ab8500_charger_irq[] = { 2382 + {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler}, 2383 + {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler}, 2384 + {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler}, 2385 + {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler}, 2386 + {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler}, 2387 + {"VBUS_DET_F", ab8500_charger_vbusdetf_handler}, 2388 + {"VBUS_DET_R", ab8500_charger_vbusdetr_handler}, 2389 + {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler}, 2390 + {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler}, 2391 + {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler}, 2392 + {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, 2393 + {"VBUS_OVV", ab8500_charger_vbusovv_handler}, 2394 + {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, 2395 + }; 2396 + 2397 + static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, 2398 + unsigned long event, void *power) 2399 + { 2400 + struct ab8500_charger *di = 2401 + container_of(nb, struct ab8500_charger, nb); 2402 + enum ab8500_usb_state bm_usb_state; 2403 + unsigned mA = *((unsigned *)power); 2404 + 2405 + if (event != USB_EVENT_VBUS) { 2406 + dev_dbg(di->dev, "not a standard host, returning\n"); 2407 + return NOTIFY_DONE; 2408 + } 2409 + 2410 + /* TODO: State is fabricate here. See if charger really needs USB 2411 + * state or if mA is enough 2412 + */ 2413 + if ((di->usb_state.usb_current == 2) && (mA > 2)) 2414 + bm_usb_state = AB8500_BM_USB_STATE_RESUME; 2415 + else if (mA == 0) 2416 + bm_usb_state = AB8500_BM_USB_STATE_RESET_HS; 2417 + else if (mA == 2) 2418 + bm_usb_state = AB8500_BM_USB_STATE_SUSPEND; 2419 + else if (mA >= 8) /* 8, 100, 500 */ 2420 + bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED; 2421 + else /* Should never occur */ 2422 + bm_usb_state = AB8500_BM_USB_STATE_RESET_FS; 2423 + 2424 + dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n", 2425 + __func__, bm_usb_state, mA); 2426 + 2427 + spin_lock(&di->usb_state.usb_lock); 2428 + di->usb_state.usb_changed = true; 2429 + spin_unlock(&di->usb_state.usb_lock); 2430 + 2431 + di->usb_state.state = bm_usb_state; 2432 + di->usb_state.usb_current = mA; 2433 + 2434 + queue_work(di->charger_wq, &di->usb_state_changed_work); 2435 + 2436 + return NOTIFY_OK; 2437 + } 2438 + 2439 + #if defined(CONFIG_PM) 2440 + static int ab8500_charger_resume(struct platform_device *pdev) 2441 + { 2442 + int ret; 2443 + struct ab8500_charger *di = platform_get_drvdata(pdev); 2444 + 2445 + /* 2446 + * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2447 + * logic. That means we have to continously kick the charger 2448 + * watchdog even when no charger is connected. This is only 2449 + * valid once the AC charger has been enabled. This is 2450 + * a bug that is not handled by the algorithm and the 2451 + * watchdog have to be kicked by the charger driver 2452 + * when the AC charger is disabled 2453 + */ 2454 + if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) { 2455 + ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2456 + AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 2457 + if (ret) 2458 + dev_err(di->dev, "Failed to kick WD!\n"); 2459 + 2460 + /* If not already pending start a new timer */ 2461 + if (!delayed_work_pending( 2462 + &di->kick_wd_work)) { 2463 + queue_delayed_work(di->charger_wq, &di->kick_wd_work, 2464 + round_jiffies(WD_KICK_INTERVAL)); 2465 + } 2466 + } 2467 + 2468 + /* If we still have a HW failure, schedule a new check */ 2469 + if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 2470 + queue_delayed_work(di->charger_wq, 2471 + &di->check_hw_failure_work, 0); 2472 + } 2473 + 2474 + return 0; 2475 + } 2476 + 2477 + static int ab8500_charger_suspend(struct platform_device *pdev, 2478 + pm_message_t state) 2479 + { 2480 + struct ab8500_charger *di = platform_get_drvdata(pdev); 2481 + 2482 + /* Cancel any pending HW failure check */ 2483 + if (delayed_work_pending(&di->check_hw_failure_work)) 2484 + cancel_delayed_work(&di->check_hw_failure_work); 2485 + 2486 + return 0; 2487 + } 2488 + #else 2489 + #define ab8500_charger_suspend NULL 2490 + #define ab8500_charger_resume NULL 2491 + #endif 2492 + 2493 + static int __devexit ab8500_charger_remove(struct platform_device *pdev) 2494 + { 2495 + struct ab8500_charger *di = platform_get_drvdata(pdev); 2496 + int i, irq, ret; 2497 + 2498 + /* Disable AC charging */ 2499 + ab8500_charger_ac_en(&di->ac_chg, false, 0, 0); 2500 + 2501 + /* Disable USB charging */ 2502 + ab8500_charger_usb_en(&di->usb_chg, false, 0, 0); 2503 + 2504 + /* Disable interrupts */ 2505 + for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2506 + irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2507 + free_irq(irq, di); 2508 + } 2509 + 2510 + /* disable the regulator */ 2511 + regulator_put(di->regu); 2512 + 2513 + /* Backup battery voltage and current disable */ 2514 + ret = abx500_mask_and_set_register_interruptible(di->dev, 2515 + AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); 2516 + if (ret < 0) 2517 + dev_err(di->dev, "%s mask and set failed\n", __func__); 2518 + 2519 + usb_unregister_notifier(di->usb_phy, &di->nb); 2520 + usb_put_transceiver(di->usb_phy); 2521 + 2522 + /* Delete the work queue */ 2523 + destroy_workqueue(di->charger_wq); 2524 + 2525 + flush_scheduled_work(); 2526 + power_supply_unregister(&di->usb_chg.psy); 2527 + power_supply_unregister(&di->ac_chg.psy); 2528 + platform_set_drvdata(pdev, NULL); 2529 + kfree(di); 2530 + 2531 + return 0; 2532 + } 2533 + 2534 + static int __devinit ab8500_charger_probe(struct platform_device *pdev) 2535 + { 2536 + int irq, i, charger_status, ret = 0; 2537 + struct abx500_bm_plat_data *plat_data; 2538 + 2539 + struct ab8500_charger *di = 2540 + kzalloc(sizeof(struct ab8500_charger), GFP_KERNEL); 2541 + if (!di) 2542 + return -ENOMEM; 2543 + 2544 + /* get parent data */ 2545 + di->dev = &pdev->dev; 2546 + di->parent = dev_get_drvdata(pdev->dev.parent); 2547 + di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2548 + 2549 + /* initialize lock */ 2550 + spin_lock_init(&di->usb_state.usb_lock); 2551 + 2552 + /* get charger specific platform data */ 2553 + plat_data = pdev->dev.platform_data; 2554 + di->pdata = plat_data->charger; 2555 + 2556 + if (!di->pdata) { 2557 + dev_err(di->dev, "no charger platform data supplied\n"); 2558 + ret = -EINVAL; 2559 + goto free_device_info; 2560 + } 2561 + 2562 + /* get battery specific platform data */ 2563 + di->bat = plat_data->battery; 2564 + if (!di->bat) { 2565 + dev_err(di->dev, "no battery platform data supplied\n"); 2566 + ret = -EINVAL; 2567 + goto free_device_info; 2568 + } 2569 + 2570 + di->autopower = false; 2571 + 2572 + /* AC supply */ 2573 + /* power_supply base class */ 2574 + di->ac_chg.psy.name = "ab8500_ac"; 2575 + di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; 2576 + di->ac_chg.psy.properties = ab8500_charger_ac_props; 2577 + di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props); 2578 + di->ac_chg.psy.get_property = ab8500_charger_ac_get_property; 2579 + di->ac_chg.psy.supplied_to = di->pdata->supplied_to; 2580 + di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants; 2581 + /* ux500_charger sub-class */ 2582 + di->ac_chg.ops.enable = &ab8500_charger_ac_en; 2583 + di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2584 + di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2585 + di->ac_chg.max_out_volt = ab8500_charger_voltage_map[ 2586 + ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2587 + di->ac_chg.max_out_curr = ab8500_charger_current_map[ 2588 + ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2589 + 2590 + /* USB supply */ 2591 + /* power_supply base class */ 2592 + di->usb_chg.psy.name = "ab8500_usb"; 2593 + di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB; 2594 + di->usb_chg.psy.properties = ab8500_charger_usb_props; 2595 + di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props); 2596 + di->usb_chg.psy.get_property = ab8500_charger_usb_get_property; 2597 + di->usb_chg.psy.supplied_to = di->pdata->supplied_to; 2598 + di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants; 2599 + /* ux500_charger sub-class */ 2600 + di->usb_chg.ops.enable = &ab8500_charger_usb_en; 2601 + di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2602 + di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2603 + di->usb_chg.max_out_volt = ab8500_charger_voltage_map[ 2604 + ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2605 + di->usb_chg.max_out_curr = ab8500_charger_current_map[ 2606 + ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2607 + 2608 + 2609 + /* Create a work queue for the charger */ 2610 + di->charger_wq = 2611 + create_singlethread_workqueue("ab8500_charger_wq"); 2612 + if (di->charger_wq == NULL) { 2613 + dev_err(di->dev, "failed to create work queue\n"); 2614 + goto free_device_info; 2615 + } 2616 + 2617 + /* Init work for HW failure check */ 2618 + INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work, 2619 + ab8500_charger_check_hw_failure_work); 2620 + INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work, 2621 + ab8500_charger_check_usbchargernotok_work); 2622 + 2623 + /* 2624 + * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2625 + * logic. That means we have to continously kick the charger 2626 + * watchdog even when no charger is connected. This is only 2627 + * valid once the AC charger has been enabled. This is 2628 + * a bug that is not handled by the algorithm and the 2629 + * watchdog have to be kicked by the charger driver 2630 + * when the AC charger is disabled 2631 + */ 2632 + INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work, 2633 + ab8500_charger_kick_watchdog_work); 2634 + 2635 + INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work, 2636 + ab8500_charger_check_vbat_work); 2637 + 2638 + /* Init work for charger detection */ 2639 + INIT_WORK(&di->usb_link_status_work, 2640 + ab8500_charger_usb_link_status_work); 2641 + INIT_WORK(&di->ac_work, ab8500_charger_ac_work); 2642 + INIT_WORK(&di->detect_usb_type_work, 2643 + ab8500_charger_detect_usb_type_work); 2644 + 2645 + INIT_WORK(&di->usb_state_changed_work, 2646 + ab8500_charger_usb_state_changed_work); 2647 + 2648 + /* Init work for checking HW status */ 2649 + INIT_WORK(&di->check_main_thermal_prot_work, 2650 + ab8500_charger_check_main_thermal_prot_work); 2651 + INIT_WORK(&di->check_usb_thermal_prot_work, 2652 + ab8500_charger_check_usb_thermal_prot_work); 2653 + 2654 + /* 2655 + * VDD ADC supply needs to be enabled from this driver when there 2656 + * is a charger connected to avoid erroneous BTEMP_HIGH/LOW 2657 + * interrupts during charging 2658 + */ 2659 + di->regu = regulator_get(di->dev, "vddadc"); 2660 + if (IS_ERR(di->regu)) { 2661 + ret = PTR_ERR(di->regu); 2662 + dev_err(di->dev, "failed to get vddadc regulator\n"); 2663 + goto free_charger_wq; 2664 + } 2665 + 2666 + 2667 + /* Initialize OVV, and other registers */ 2668 + ret = ab8500_charger_init_hw_registers(di); 2669 + if (ret) { 2670 + dev_err(di->dev, "failed to initialize ABB registers\n"); 2671 + goto free_regulator; 2672 + } 2673 + 2674 + /* Register AC charger class */ 2675 + ret = power_supply_register(di->dev, &di->ac_chg.psy); 2676 + if (ret) { 2677 + dev_err(di->dev, "failed to register AC charger\n"); 2678 + goto free_regulator; 2679 + } 2680 + 2681 + /* Register USB charger class */ 2682 + ret = power_supply_register(di->dev, &di->usb_chg.psy); 2683 + if (ret) { 2684 + dev_err(di->dev, "failed to register USB charger\n"); 2685 + goto free_ac; 2686 + } 2687 + 2688 + di->usb_phy = usb_get_transceiver(); 2689 + if (!di->usb_phy) { 2690 + dev_err(di->dev, "failed to get usb transceiver\n"); 2691 + ret = -EINVAL; 2692 + goto free_usb; 2693 + } 2694 + di->nb.notifier_call = ab8500_charger_usb_notifier_call; 2695 + ret = usb_register_notifier(di->usb_phy, &di->nb); 2696 + if (ret) { 2697 + dev_err(di->dev, "failed to register usb notifier\n"); 2698 + goto put_usb_phy; 2699 + } 2700 + 2701 + /* Identify the connected charger types during startup */ 2702 + charger_status = ab8500_charger_detect_chargers(di); 2703 + if (charger_status & AC_PW_CONN) { 2704 + di->ac.charger_connected = 1; 2705 + di->ac_conn = true; 2706 + ab8500_power_supply_changed(di, &di->ac_chg.psy); 2707 + sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 2708 + } 2709 + 2710 + if (charger_status & USB_PW_CONN) { 2711 + dev_dbg(di->dev, "VBUS Detect during startup\n"); 2712 + di->vbus_detected = true; 2713 + di->vbus_detected_start = true; 2714 + queue_work(di->charger_wq, 2715 + &di->detect_usb_type_work); 2716 + } 2717 + 2718 + /* Register interrupts */ 2719 + for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2720 + irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2721 + ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, 2722 + IRQF_SHARED | IRQF_NO_SUSPEND, 2723 + ab8500_charger_irq[i].name, di); 2724 + 2725 + if (ret != 0) { 2726 + dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 2727 + , ab8500_charger_irq[i].name, irq, ret); 2728 + goto free_irq; 2729 + } 2730 + dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 2731 + ab8500_charger_irq[i].name, irq, ret); 2732 + } 2733 + 2734 + platform_set_drvdata(pdev, di); 2735 + 2736 + return ret; 2737 + 2738 + free_irq: 2739 + usb_unregister_notifier(di->usb_phy, &di->nb); 2740 + 2741 + /* We also have to free all successfully registered irqs */ 2742 + for (i = i - 1; i >= 0; i--) { 2743 + irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2744 + free_irq(irq, di); 2745 + } 2746 + put_usb_phy: 2747 + usb_put_transceiver(di->usb_phy); 2748 + free_usb: 2749 + power_supply_unregister(&di->usb_chg.psy); 2750 + free_ac: 2751 + power_supply_unregister(&di->ac_chg.psy); 2752 + free_regulator: 2753 + regulator_put(di->regu); 2754 + free_charger_wq: 2755 + destroy_workqueue(di->charger_wq); 2756 + free_device_info: 2757 + kfree(di); 2758 + 2759 + return ret; 2760 + } 2761 + 2762 + static struct platform_driver ab8500_charger_driver = { 2763 + .probe = ab8500_charger_probe, 2764 + .remove = __devexit_p(ab8500_charger_remove), 2765 + .suspend = ab8500_charger_suspend, 2766 + .resume = ab8500_charger_resume, 2767 + .driver = { 2768 + .name = "ab8500-charger", 2769 + .owner = THIS_MODULE, 2770 + }, 2771 + }; 2772 + 2773 + static int __init ab8500_charger_init(void) 2774 + { 2775 + return platform_driver_register(&ab8500_charger_driver); 2776 + } 2777 + 2778 + static void __exit ab8500_charger_exit(void) 2779 + { 2780 + platform_driver_unregister(&ab8500_charger_driver); 2781 + } 2782 + 2783 + subsys_initcall_sync(ab8500_charger_init); 2784 + module_exit(ab8500_charger_exit); 2785 + 2786 + MODULE_LICENSE("GPL v2"); 2787 + MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); 2788 + MODULE_ALIAS("platform:ab8500-charger"); 2789 + MODULE_DESCRIPTION("AB8500 charger management driver");
+2637
drivers/power/ab8500_fg.c
··· 1 + /* 2 + * Copyright (C) ST-Ericsson AB 2012 3 + * 4 + * Main and Back-up battery management driver. 5 + * 6 + * Note: Backup battery management is required in case of Li-Ion battery and not 7 + * for capacitive battery. HREF boards have capacitive battery and hence backup 8 + * battery management is not used and the supported code is available in this 9 + * driver. 10 + * 11 + * License Terms: GNU General Public License v2 12 + * Author: 13 + * Johan Palsson <johan.palsson@stericsson.com> 14 + * Karl Komierowski <karl.komierowski@stericsson.com> 15 + * Arun R Murthy <arun.murthy@stericsson.com> 16 + */ 17 + 18 + #include <linux/init.h> 19 + #include <linux/module.h> 20 + #include <linux/device.h> 21 + #include <linux/interrupt.h> 22 + #include <linux/platform_device.h> 23 + #include <linux/power_supply.h> 24 + #include <linux/kobject.h> 25 + #include <linux/mfd/abx500/ab8500.h> 26 + #include <linux/mfd/abx500.h> 27 + #include <linux/slab.h> 28 + #include <linux/mfd/abx500/ab8500-bm.h> 29 + #include <linux/delay.h> 30 + #include <linux/mfd/abx500/ab8500-gpadc.h> 31 + #include <linux/mfd/abx500.h> 32 + #include <linux/time.h> 33 + #include <linux/completion.h> 34 + 35 + #define MILLI_TO_MICRO 1000 36 + #define FG_LSB_IN_MA 1627 37 + #define QLSB_NANO_AMP_HOURS_X10 1129 38 + #define INS_CURR_TIMEOUT (3 * HZ) 39 + 40 + #define SEC_TO_SAMPLE(S) (S * 4) 41 + 42 + #define NBR_AVG_SAMPLES 20 43 + 44 + #define LOW_BAT_CHECK_INTERVAL (2 * HZ) 45 + 46 + #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */ 47 + #define BATT_OK_MIN 2360 /* mV */ 48 + #define BATT_OK_INCREMENT 50 /* mV */ 49 + #define BATT_OK_MAX_NR_INCREMENTS 0xE 50 + 51 + /* FG constants */ 52 + #define BATT_OVV 0x01 53 + 54 + #define interpolate(x, x1, y1, x2, y2) \ 55 + ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1)))); 56 + 57 + #define to_ab8500_fg_device_info(x) container_of((x), \ 58 + struct ab8500_fg, fg_psy); 59 + 60 + /** 61 + * struct ab8500_fg_interrupts - ab8500 fg interupts 62 + * @name: name of the interrupt 63 + * @isr function pointer to the isr 64 + */ 65 + struct ab8500_fg_interrupts { 66 + char *name; 67 + irqreturn_t (*isr)(int irq, void *data); 68 + }; 69 + 70 + enum ab8500_fg_discharge_state { 71 + AB8500_FG_DISCHARGE_INIT, 72 + AB8500_FG_DISCHARGE_INITMEASURING, 73 + AB8500_FG_DISCHARGE_INIT_RECOVERY, 74 + AB8500_FG_DISCHARGE_RECOVERY, 75 + AB8500_FG_DISCHARGE_READOUT_INIT, 76 + AB8500_FG_DISCHARGE_READOUT, 77 + AB8500_FG_DISCHARGE_WAKEUP, 78 + }; 79 + 80 + static char *discharge_state[] = { 81 + "DISCHARGE_INIT", 82 + "DISCHARGE_INITMEASURING", 83 + "DISCHARGE_INIT_RECOVERY", 84 + "DISCHARGE_RECOVERY", 85 + "DISCHARGE_READOUT_INIT", 86 + "DISCHARGE_READOUT", 87 + "DISCHARGE_WAKEUP", 88 + }; 89 + 90 + enum ab8500_fg_charge_state { 91 + AB8500_FG_CHARGE_INIT, 92 + AB8500_FG_CHARGE_READOUT, 93 + }; 94 + 95 + static char *charge_state[] = { 96 + "CHARGE_INIT", 97 + "CHARGE_READOUT", 98 + }; 99 + 100 + enum ab8500_fg_calibration_state { 101 + AB8500_FG_CALIB_INIT, 102 + AB8500_FG_CALIB_WAIT, 103 + AB8500_FG_CALIB_END, 104 + }; 105 + 106 + struct ab8500_fg_avg_cap { 107 + int avg; 108 + int samples[NBR_AVG_SAMPLES]; 109 + __kernel_time_t time_stamps[NBR_AVG_SAMPLES]; 110 + int pos; 111 + int nbr_samples; 112 + int sum; 113 + }; 114 + 115 + struct ab8500_fg_battery_capacity { 116 + int max_mah_design; 117 + int max_mah; 118 + int mah; 119 + int permille; 120 + int level; 121 + int prev_mah; 122 + int prev_percent; 123 + int prev_level; 124 + int user_mah; 125 + }; 126 + 127 + struct ab8500_fg_flags { 128 + bool fg_enabled; 129 + bool conv_done; 130 + bool charging; 131 + bool fully_charged; 132 + bool force_full; 133 + bool low_bat_delay; 134 + bool low_bat; 135 + bool bat_ovv; 136 + bool batt_unknown; 137 + bool calibrate; 138 + bool user_cap; 139 + bool batt_id_received; 140 + }; 141 + 142 + struct inst_curr_result_list { 143 + struct list_head list; 144 + int *result; 145 + }; 146 + 147 + /** 148 + * struct ab8500_fg - ab8500 FG device information 149 + * @dev: Pointer to the structure device 150 + * @node: a list of AB8500 FGs, hence prepared for reentrance 151 + * @irq holds the CCEOC interrupt number 152 + * @vbat: Battery voltage in mV 153 + * @vbat_nom: Nominal battery voltage in mV 154 + * @inst_curr: Instantenous battery current in mA 155 + * @avg_curr: Average battery current in mA 156 + * @bat_temp battery temperature 157 + * @fg_samples: Number of samples used in the FG accumulation 158 + * @accu_charge: Accumulated charge from the last conversion 159 + * @recovery_cnt: Counter for recovery mode 160 + * @high_curr_cnt: Counter for high current mode 161 + * @init_cnt: Counter for init mode 162 + * @recovery_needed: Indicate if recovery is needed 163 + * @high_curr_mode: Indicate if we're in high current mode 164 + * @init_capacity: Indicate if initial capacity measuring should be done 165 + * @turn_off_fg: True if fg was off before current measurement 166 + * @calib_state State during offset calibration 167 + * @discharge_state: Current discharge state 168 + * @charge_state: Current charge state 169 + * @ab8500_fg_complete Completion struct used for the instant current reading 170 + * @flags: Structure for information about events triggered 171 + * @bat_cap: Structure for battery capacity specific parameters 172 + * @avg_cap: Average capacity filter 173 + * @parent: Pointer to the struct ab8500 174 + * @gpadc: Pointer to the struct gpadc 175 + * @pdata: Pointer to the abx500_fg platform data 176 + * @bat: Pointer to the abx500_bm platform data 177 + * @fg_psy: Structure that holds the FG specific battery properties 178 + * @fg_wq: Work queue for running the FG algorithm 179 + * @fg_periodic_work: Work to run the FG algorithm periodically 180 + * @fg_low_bat_work: Work to check low bat condition 181 + * @fg_reinit_work Work used to reset and reinitialise the FG algorithm 182 + * @fg_work: Work to run the FG algorithm instantly 183 + * @fg_acc_cur_work: Work to read the FG accumulator 184 + * @fg_check_hw_failure_work: Work for checking HW state 185 + * @cc_lock: Mutex for locking the CC 186 + * @fg_kobject: Structure of type kobject 187 + */ 188 + struct ab8500_fg { 189 + struct device *dev; 190 + struct list_head node; 191 + int irq; 192 + int vbat; 193 + int vbat_nom; 194 + int inst_curr; 195 + int avg_curr; 196 + int bat_temp; 197 + int fg_samples; 198 + int accu_charge; 199 + int recovery_cnt; 200 + int high_curr_cnt; 201 + int init_cnt; 202 + bool recovery_needed; 203 + bool high_curr_mode; 204 + bool init_capacity; 205 + bool turn_off_fg; 206 + enum ab8500_fg_calibration_state calib_state; 207 + enum ab8500_fg_discharge_state discharge_state; 208 + enum ab8500_fg_charge_state charge_state; 209 + struct completion ab8500_fg_complete; 210 + struct ab8500_fg_flags flags; 211 + struct ab8500_fg_battery_capacity bat_cap; 212 + struct ab8500_fg_avg_cap avg_cap; 213 + struct ab8500 *parent; 214 + struct ab8500_gpadc *gpadc; 215 + struct abx500_fg_platform_data *pdata; 216 + struct abx500_bm_data *bat; 217 + struct power_supply fg_psy; 218 + struct workqueue_struct *fg_wq; 219 + struct delayed_work fg_periodic_work; 220 + struct delayed_work fg_low_bat_work; 221 + struct delayed_work fg_reinit_work; 222 + struct work_struct fg_work; 223 + struct work_struct fg_acc_cur_work; 224 + struct delayed_work fg_check_hw_failure_work; 225 + struct mutex cc_lock; 226 + struct kobject fg_kobject; 227 + }; 228 + static LIST_HEAD(ab8500_fg_list); 229 + 230 + /** 231 + * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge 232 + * (i.e. the first fuel gauge in the instance list) 233 + */ 234 + struct ab8500_fg *ab8500_fg_get(void) 235 + { 236 + struct ab8500_fg *fg; 237 + 238 + if (list_empty(&ab8500_fg_list)) 239 + return NULL; 240 + 241 + fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node); 242 + return fg; 243 + } 244 + 245 + /* Main battery properties */ 246 + static enum power_supply_property ab8500_fg_props[] = { 247 + POWER_SUPPLY_PROP_VOLTAGE_NOW, 248 + POWER_SUPPLY_PROP_CURRENT_NOW, 249 + POWER_SUPPLY_PROP_CURRENT_AVG, 250 + POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 251 + POWER_SUPPLY_PROP_ENERGY_FULL, 252 + POWER_SUPPLY_PROP_ENERGY_NOW, 253 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 254 + POWER_SUPPLY_PROP_CHARGE_FULL, 255 + POWER_SUPPLY_PROP_CHARGE_NOW, 256 + POWER_SUPPLY_PROP_CAPACITY, 257 + POWER_SUPPLY_PROP_CAPACITY_LEVEL, 258 + }; 259 + 260 + /* 261 + * This array maps the raw hex value to lowbat voltage used by the AB8500 262 + * Values taken from the UM0836 263 + */ 264 + static int ab8500_fg_lowbat_voltage_map[] = { 265 + 2300 , 266 + 2325 , 267 + 2350 , 268 + 2375 , 269 + 2400 , 270 + 2425 , 271 + 2450 , 272 + 2475 , 273 + 2500 , 274 + 2525 , 275 + 2550 , 276 + 2575 , 277 + 2600 , 278 + 2625 , 279 + 2650 , 280 + 2675 , 281 + 2700 , 282 + 2725 , 283 + 2750 , 284 + 2775 , 285 + 2800 , 286 + 2825 , 287 + 2850 , 288 + 2875 , 289 + 2900 , 290 + 2925 , 291 + 2950 , 292 + 2975 , 293 + 3000 , 294 + 3025 , 295 + 3050 , 296 + 3075 , 297 + 3100 , 298 + 3125 , 299 + 3150 , 300 + 3175 , 301 + 3200 , 302 + 3225 , 303 + 3250 , 304 + 3275 , 305 + 3300 , 306 + 3325 , 307 + 3350 , 308 + 3375 , 309 + 3400 , 310 + 3425 , 311 + 3450 , 312 + 3475 , 313 + 3500 , 314 + 3525 , 315 + 3550 , 316 + 3575 , 317 + 3600 , 318 + 3625 , 319 + 3650 , 320 + 3675 , 321 + 3700 , 322 + 3725 , 323 + 3750 , 324 + 3775 , 325 + 3800 , 326 + 3825 , 327 + 3850 , 328 + 3850 , 329 + }; 330 + 331 + static u8 ab8500_volt_to_regval(int voltage) 332 + { 333 + int i; 334 + 335 + if (voltage < ab8500_fg_lowbat_voltage_map[0]) 336 + return 0; 337 + 338 + for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) { 339 + if (voltage < ab8500_fg_lowbat_voltage_map[i]) 340 + return (u8) i - 1; 341 + } 342 + 343 + /* If not captured above, return index of last element */ 344 + return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1; 345 + } 346 + 347 + /** 348 + * ab8500_fg_is_low_curr() - Low or high current mode 349 + * @di: pointer to the ab8500_fg structure 350 + * @curr: the current to base or our decision on 351 + * 352 + * Low current mode if the current consumption is below a certain threshold 353 + */ 354 + static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr) 355 + { 356 + /* 357 + * We want to know if we're in low current mode 358 + */ 359 + if (curr > -di->bat->fg_params->high_curr_threshold) 360 + return true; 361 + else 362 + return false; 363 + } 364 + 365 + /** 366 + * ab8500_fg_add_cap_sample() - Add capacity to average filter 367 + * @di: pointer to the ab8500_fg structure 368 + * @sample: the capacity in mAh to add to the filter 369 + * 370 + * A capacity is added to the filter and a new mean capacity is calculated and 371 + * returned 372 + */ 373 + static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample) 374 + { 375 + struct timespec ts; 376 + struct ab8500_fg_avg_cap *avg = &di->avg_cap; 377 + 378 + getnstimeofday(&ts); 379 + 380 + do { 381 + avg->sum += sample - avg->samples[avg->pos]; 382 + avg->samples[avg->pos] = sample; 383 + avg->time_stamps[avg->pos] = ts.tv_sec; 384 + avg->pos++; 385 + 386 + if (avg->pos == NBR_AVG_SAMPLES) 387 + avg->pos = 0; 388 + 389 + if (avg->nbr_samples < NBR_AVG_SAMPLES) 390 + avg->nbr_samples++; 391 + 392 + /* 393 + * Check the time stamp for each sample. If too old, 394 + * replace with latest sample 395 + */ 396 + } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]); 397 + 398 + avg->avg = avg->sum / avg->nbr_samples; 399 + 400 + return avg->avg; 401 + } 402 + 403 + /** 404 + * ab8500_fg_clear_cap_samples() - Clear average filter 405 + * @di: pointer to the ab8500_fg structure 406 + * 407 + * The capacity filter is is reset to zero. 408 + */ 409 + static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di) 410 + { 411 + int i; 412 + struct ab8500_fg_avg_cap *avg = &di->avg_cap; 413 + 414 + avg->pos = 0; 415 + avg->nbr_samples = 0; 416 + avg->sum = 0; 417 + avg->avg = 0; 418 + 419 + for (i = 0; i < NBR_AVG_SAMPLES; i++) { 420 + avg->samples[i] = 0; 421 + avg->time_stamps[i] = 0; 422 + } 423 + } 424 + 425 + /** 426 + * ab8500_fg_fill_cap_sample() - Fill average filter 427 + * @di: pointer to the ab8500_fg structure 428 + * @sample: the capacity in mAh to fill the filter with 429 + * 430 + * The capacity filter is filled with a capacity in mAh 431 + */ 432 + static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample) 433 + { 434 + int i; 435 + struct timespec ts; 436 + struct ab8500_fg_avg_cap *avg = &di->avg_cap; 437 + 438 + getnstimeofday(&ts); 439 + 440 + for (i = 0; i < NBR_AVG_SAMPLES; i++) { 441 + avg->samples[i] = sample; 442 + avg->time_stamps[i] = ts.tv_sec; 443 + } 444 + 445 + avg->pos = 0; 446 + avg->nbr_samples = NBR_AVG_SAMPLES; 447 + avg->sum = sample * NBR_AVG_SAMPLES; 448 + avg->avg = sample; 449 + } 450 + 451 + /** 452 + * ab8500_fg_coulomb_counter() - enable coulomb counter 453 + * @di: pointer to the ab8500_fg structure 454 + * @enable: enable/disable 455 + * 456 + * Enable/Disable coulomb counter. 457 + * On failure returns negative value. 458 + */ 459 + static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable) 460 + { 461 + int ret = 0; 462 + mutex_lock(&di->cc_lock); 463 + if (enable) { 464 + /* To be able to reprogram the number of samples, we have to 465 + * first stop the CC and then enable it again */ 466 + ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 467 + AB8500_RTC_CC_CONF_REG, 0x00); 468 + if (ret) 469 + goto cc_err; 470 + 471 + /* Program the samples */ 472 + ret = abx500_set_register_interruptible(di->dev, 473 + AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 474 + di->fg_samples); 475 + if (ret) 476 + goto cc_err; 477 + 478 + /* Start the CC */ 479 + ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 480 + AB8500_RTC_CC_CONF_REG, 481 + (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 482 + if (ret) 483 + goto cc_err; 484 + 485 + di->flags.fg_enabled = true; 486 + } else { 487 + /* Clear any pending read requests */ 488 + ret = abx500_set_register_interruptible(di->dev, 489 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0); 490 + if (ret) 491 + goto cc_err; 492 + 493 + ret = abx500_set_register_interruptible(di->dev, 494 + AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0); 495 + if (ret) 496 + goto cc_err; 497 + 498 + /* Stop the CC */ 499 + ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 500 + AB8500_RTC_CC_CONF_REG, 0); 501 + if (ret) 502 + goto cc_err; 503 + 504 + di->flags.fg_enabled = false; 505 + 506 + } 507 + dev_dbg(di->dev, " CC enabled: %d Samples: %d\n", 508 + enable, di->fg_samples); 509 + 510 + mutex_unlock(&di->cc_lock); 511 + 512 + return ret; 513 + cc_err: 514 + dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__); 515 + mutex_unlock(&di->cc_lock); 516 + return ret; 517 + } 518 + 519 + /** 520 + * ab8500_fg_inst_curr_start() - start battery instantaneous current 521 + * @di: pointer to the ab8500_fg structure 522 + * 523 + * Returns 0 or error code 524 + * Note: This is part "one" and has to be called before 525 + * ab8500_fg_inst_curr_finalize() 526 + */ 527 + int ab8500_fg_inst_curr_start(struct ab8500_fg *di) 528 + { 529 + u8 reg_val; 530 + int ret; 531 + 532 + mutex_lock(&di->cc_lock); 533 + 534 + ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 535 + AB8500_RTC_CC_CONF_REG, &reg_val); 536 + if (ret < 0) 537 + goto fail; 538 + 539 + if (!(reg_val & CC_PWR_UP_ENA)) { 540 + dev_dbg(di->dev, "%s Enable FG\n", __func__); 541 + di->turn_off_fg = true; 542 + 543 + /* Program the samples */ 544 + ret = abx500_set_register_interruptible(di->dev, 545 + AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 546 + SEC_TO_SAMPLE(10)); 547 + if (ret) 548 + goto fail; 549 + 550 + /* Start the CC */ 551 + ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 552 + AB8500_RTC_CC_CONF_REG, 553 + (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 554 + if (ret) 555 + goto fail; 556 + } else { 557 + di->turn_off_fg = false; 558 + } 559 + 560 + /* Return and WFI */ 561 + INIT_COMPLETION(di->ab8500_fg_complete); 562 + enable_irq(di->irq); 563 + 564 + /* Note: cc_lock is still locked */ 565 + return 0; 566 + fail: 567 + mutex_unlock(&di->cc_lock); 568 + return ret; 569 + } 570 + 571 + /** 572 + * ab8500_fg_inst_curr_done() - check if fg conversion is done 573 + * @di: pointer to the ab8500_fg structure 574 + * 575 + * Returns 1 if conversion done, 0 if still waiting 576 + */ 577 + int ab8500_fg_inst_curr_done(struct ab8500_fg *di) 578 + { 579 + return completion_done(&di->ab8500_fg_complete); 580 + } 581 + 582 + /** 583 + * ab8500_fg_inst_curr_finalize() - battery instantaneous current 584 + * @di: pointer to the ab8500_fg structure 585 + * @res: battery instantenous current(on success) 586 + * 587 + * Returns 0 or an error code 588 + * Note: This is part "two" and has to be called at earliest 250 ms 589 + * after ab8500_fg_inst_curr_start() 590 + */ 591 + int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res) 592 + { 593 + u8 low, high; 594 + int val; 595 + int ret; 596 + int timeout; 597 + 598 + if (!completion_done(&di->ab8500_fg_complete)) { 599 + timeout = wait_for_completion_timeout(&di->ab8500_fg_complete, 600 + INS_CURR_TIMEOUT); 601 + dev_dbg(di->dev, "Finalize time: %d ms\n", 602 + ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ); 603 + if (!timeout) { 604 + ret = -ETIME; 605 + disable_irq(di->irq); 606 + dev_err(di->dev, "completion timed out [%d]\n", 607 + __LINE__); 608 + goto fail; 609 + } 610 + } 611 + 612 + disable_irq(di->irq); 613 + 614 + ret = abx500_mask_and_set_register_interruptible(di->dev, 615 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 616 + READ_REQ, READ_REQ); 617 + 618 + /* 100uS between read request and read is needed */ 619 + usleep_range(100, 100); 620 + 621 + /* Read CC Sample conversion value Low and high */ 622 + ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 623 + AB8500_GASG_CC_SMPL_CNVL_REG, &low); 624 + if (ret < 0) 625 + goto fail; 626 + 627 + ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 628 + AB8500_GASG_CC_SMPL_CNVH_REG, &high); 629 + if (ret < 0) 630 + goto fail; 631 + 632 + /* 633 + * negative value for Discharging 634 + * convert 2's compliment into decimal 635 + */ 636 + if (high & 0x10) 637 + val = (low | (high << 8) | 0xFFFFE000); 638 + else 639 + val = (low | (high << 8)); 640 + 641 + /* 642 + * Convert to unit value in mA 643 + * Full scale input voltage is 644 + * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA 645 + * Given a 250ms conversion cycle time the LSB corresponds 646 + * to 112.9 nAh. Convert to current by dividing by the conversion 647 + * time in hours (250ms = 1 / (3600 * 4)h) 648 + * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 649 + */ 650 + val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) / 651 + (1000 * di->bat->fg_res); 652 + 653 + if (di->turn_off_fg) { 654 + dev_dbg(di->dev, "%s Disable FG\n", __func__); 655 + 656 + /* Clear any pending read requests */ 657 + ret = abx500_set_register_interruptible(di->dev, 658 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0); 659 + if (ret) 660 + goto fail; 661 + 662 + /* Stop the CC */ 663 + ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 664 + AB8500_RTC_CC_CONF_REG, 0); 665 + if (ret) 666 + goto fail; 667 + } 668 + mutex_unlock(&di->cc_lock); 669 + (*res) = val; 670 + 671 + return 0; 672 + fail: 673 + mutex_unlock(&di->cc_lock); 674 + return ret; 675 + } 676 + 677 + /** 678 + * ab8500_fg_inst_curr_blocking() - battery instantaneous current 679 + * @di: pointer to the ab8500_fg structure 680 + * @res: battery instantenous current(on success) 681 + * 682 + * Returns 0 else error code 683 + */ 684 + int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di) 685 + { 686 + int ret; 687 + int res = 0; 688 + 689 + ret = ab8500_fg_inst_curr_start(di); 690 + if (ret) { 691 + dev_err(di->dev, "Failed to initialize fg_inst\n"); 692 + return 0; 693 + } 694 + 695 + ret = ab8500_fg_inst_curr_finalize(di, &res); 696 + if (ret) { 697 + dev_err(di->dev, "Failed to finalize fg_inst\n"); 698 + return 0; 699 + } 700 + 701 + return res; 702 + } 703 + 704 + /** 705 + * ab8500_fg_acc_cur_work() - average battery current 706 + * @work: pointer to the work_struct structure 707 + * 708 + * Updated the average battery current obtained from the 709 + * coulomb counter. 710 + */ 711 + static void ab8500_fg_acc_cur_work(struct work_struct *work) 712 + { 713 + int val; 714 + int ret; 715 + u8 low, med, high; 716 + 717 + struct ab8500_fg *di = container_of(work, 718 + struct ab8500_fg, fg_acc_cur_work); 719 + 720 + mutex_lock(&di->cc_lock); 721 + ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE, 722 + AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ); 723 + if (ret) 724 + goto exit; 725 + 726 + ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 727 + AB8500_GASG_CC_NCOV_ACCU_LOW, &low); 728 + if (ret < 0) 729 + goto exit; 730 + 731 + ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 732 + AB8500_GASG_CC_NCOV_ACCU_MED, &med); 733 + if (ret < 0) 734 + goto exit; 735 + 736 + ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 737 + AB8500_GASG_CC_NCOV_ACCU_HIGH, &high); 738 + if (ret < 0) 739 + goto exit; 740 + 741 + /* Check for sign bit in case of negative value, 2's compliment */ 742 + if (high & 0x10) 743 + val = (low | (med << 8) | (high << 16) | 0xFFE00000); 744 + else 745 + val = (low | (med << 8) | (high << 16)); 746 + 747 + /* 748 + * Convert to uAh 749 + * Given a 250ms conversion cycle time the LSB corresponds 750 + * to 112.9 nAh. 751 + * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 752 + */ 753 + di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) / 754 + (100 * di->bat->fg_res); 755 + 756 + /* 757 + * Convert to unit value in mA 758 + * Full scale input voltage is 759 + * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA 760 + * Given a 250ms conversion cycle time the LSB corresponds 761 + * to 112.9 nAh. Convert to current by dividing by the conversion 762 + * time in hours (= samples / (3600 * 4)h) 763 + * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 764 + */ 765 + di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) / 766 + (1000 * di->bat->fg_res * (di->fg_samples / 4)); 767 + 768 + di->flags.conv_done = true; 769 + 770 + mutex_unlock(&di->cc_lock); 771 + 772 + queue_work(di->fg_wq, &di->fg_work); 773 + 774 + return; 775 + exit: 776 + dev_err(di->dev, 777 + "Failed to read or write gas gauge registers\n"); 778 + mutex_unlock(&di->cc_lock); 779 + queue_work(di->fg_wq, &di->fg_work); 780 + } 781 + 782 + /** 783 + * ab8500_fg_bat_voltage() - get battery voltage 784 + * @di: pointer to the ab8500_fg structure 785 + * 786 + * Returns battery voltage(on success) else error code 787 + */ 788 + static int ab8500_fg_bat_voltage(struct ab8500_fg *di) 789 + { 790 + int vbat; 791 + static int prev; 792 + 793 + vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V); 794 + if (vbat < 0) { 795 + dev_err(di->dev, 796 + "%s gpadc conversion failed, using previous value\n", 797 + __func__); 798 + return prev; 799 + } 800 + 801 + prev = vbat; 802 + return vbat; 803 + } 804 + 805 + /** 806 + * ab8500_fg_volt_to_capacity() - Voltage based capacity 807 + * @di: pointer to the ab8500_fg structure 808 + * @voltage: The voltage to convert to a capacity 809 + * 810 + * Returns battery capacity in per mille based on voltage 811 + */ 812 + static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage) 813 + { 814 + int i, tbl_size; 815 + struct abx500_v_to_cap *tbl; 816 + int cap = 0; 817 + 818 + tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl, 819 + tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements; 820 + 821 + for (i = 0; i < tbl_size; ++i) { 822 + if (voltage > tbl[i].voltage) 823 + break; 824 + } 825 + 826 + if ((i > 0) && (i < tbl_size)) { 827 + cap = interpolate(voltage, 828 + tbl[i].voltage, 829 + tbl[i].capacity * 10, 830 + tbl[i-1].voltage, 831 + tbl[i-1].capacity * 10); 832 + } else if (i == 0) { 833 + cap = 1000; 834 + } else { 835 + cap = 0; 836 + } 837 + 838 + dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille", 839 + __func__, voltage, cap); 840 + 841 + return cap; 842 + } 843 + 844 + /** 845 + * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity 846 + * @di: pointer to the ab8500_fg structure 847 + * 848 + * Returns battery capacity based on battery voltage that is not compensated 849 + * for the voltage drop due to the load 850 + */ 851 + static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di) 852 + { 853 + di->vbat = ab8500_fg_bat_voltage(di); 854 + return ab8500_fg_volt_to_capacity(di, di->vbat); 855 + } 856 + 857 + /** 858 + * ab8500_fg_battery_resistance() - Returns the battery inner resistance 859 + * @di: pointer to the ab8500_fg structure 860 + * 861 + * Returns battery inner resistance added with the fuel gauge resistor value 862 + * to get the total resistance in the whole link from gnd to bat+ node. 863 + */ 864 + static int ab8500_fg_battery_resistance(struct ab8500_fg *di) 865 + { 866 + int i, tbl_size; 867 + struct batres_vs_temp *tbl; 868 + int resist = 0; 869 + 870 + tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl; 871 + tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements; 872 + 873 + for (i = 0; i < tbl_size; ++i) { 874 + if (di->bat_temp / 10 > tbl[i].temp) 875 + break; 876 + } 877 + 878 + if ((i > 0) && (i < tbl_size)) { 879 + resist = interpolate(di->bat_temp / 10, 880 + tbl[i].temp, 881 + tbl[i].resist, 882 + tbl[i-1].temp, 883 + tbl[i-1].resist); 884 + } else if (i == 0) { 885 + resist = tbl[0].resist; 886 + } else { 887 + resist = tbl[tbl_size - 1].resist; 888 + } 889 + 890 + dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d" 891 + " fg resistance %d, total: %d (mOhm)\n", 892 + __func__, di->bat_temp, resist, di->bat->fg_res / 10, 893 + (di->bat->fg_res / 10) + resist); 894 + 895 + /* fg_res variable is in 0.1mOhm */ 896 + resist += di->bat->fg_res / 10; 897 + 898 + return resist; 899 + } 900 + 901 + /** 902 + * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity 903 + * @di: pointer to the ab8500_fg structure 904 + * 905 + * Returns battery capacity based on battery voltage that is load compensated 906 + * for the voltage drop 907 + */ 908 + static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di) 909 + { 910 + int vbat_comp, res; 911 + int i = 0; 912 + int vbat = 0; 913 + 914 + ab8500_fg_inst_curr_start(di); 915 + 916 + do { 917 + vbat += ab8500_fg_bat_voltage(di); 918 + i++; 919 + msleep(5); 920 + } while (!ab8500_fg_inst_curr_done(di)); 921 + 922 + ab8500_fg_inst_curr_finalize(di, &di->inst_curr); 923 + 924 + di->vbat = vbat / i; 925 + res = ab8500_fg_battery_resistance(di); 926 + 927 + /* Use Ohms law to get the load compensated voltage */ 928 + vbat_comp = di->vbat - (di->inst_curr * res) / 1000; 929 + 930 + dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, " 931 + "R: %dmOhm, Current: %dmA Vbat Samples: %d\n", 932 + __func__, di->vbat, vbat_comp, res, di->inst_curr, i); 933 + 934 + return ab8500_fg_volt_to_capacity(di, vbat_comp); 935 + } 936 + 937 + /** 938 + * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille 939 + * @di: pointer to the ab8500_fg structure 940 + * @cap_mah: capacity in mAh 941 + * 942 + * Converts capacity in mAh to capacity in permille 943 + */ 944 + static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah) 945 + { 946 + return (cap_mah * 1000) / di->bat_cap.max_mah_design; 947 + } 948 + 949 + /** 950 + * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh 951 + * @di: pointer to the ab8500_fg structure 952 + * @cap_pm: capacity in permille 953 + * 954 + * Converts capacity in permille to capacity in mAh 955 + */ 956 + static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm) 957 + { 958 + return cap_pm * di->bat_cap.max_mah_design / 1000; 959 + } 960 + 961 + /** 962 + * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh 963 + * @di: pointer to the ab8500_fg structure 964 + * @cap_mah: capacity in mAh 965 + * 966 + * Converts capacity in mAh to capacity in uWh 967 + */ 968 + static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah) 969 + { 970 + u64 div_res; 971 + u32 div_rem; 972 + 973 + div_res = ((u64) cap_mah) * ((u64) di->vbat_nom); 974 + div_rem = do_div(div_res, 1000); 975 + 976 + /* Make sure to round upwards if necessary */ 977 + if (div_rem >= 1000 / 2) 978 + div_res++; 979 + 980 + return (int) div_res; 981 + } 982 + 983 + /** 984 + * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging 985 + * @di: pointer to the ab8500_fg structure 986 + * 987 + * Return the capacity in mAh based on previous calculated capcity and the FG 988 + * accumulator register value. The filter is filled with this capacity 989 + */ 990 + static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di) 991 + { 992 + dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 993 + __func__, 994 + di->bat_cap.mah, 995 + di->accu_charge); 996 + 997 + /* Capacity should not be less than 0 */ 998 + if (di->bat_cap.mah + di->accu_charge > 0) 999 + di->bat_cap.mah += di->accu_charge; 1000 + else 1001 + di->bat_cap.mah = 0; 1002 + /* 1003 + * We force capacity to 100% once when the algorithm 1004 + * reports that it's full. 1005 + */ 1006 + if (di->bat_cap.mah >= di->bat_cap.max_mah_design || 1007 + di->flags.force_full) { 1008 + di->bat_cap.mah = di->bat_cap.max_mah_design; 1009 + } 1010 + 1011 + ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1012 + di->bat_cap.permille = 1013 + ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1014 + 1015 + /* We need to update battery voltage and inst current when charging */ 1016 + di->vbat = ab8500_fg_bat_voltage(di); 1017 + di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1018 + 1019 + return di->bat_cap.mah; 1020 + } 1021 + 1022 + /** 1023 + * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage 1024 + * @di: pointer to the ab8500_fg structure 1025 + * @comp: if voltage should be load compensated before capacity calc 1026 + * 1027 + * Return the capacity in mAh based on the battery voltage. The voltage can 1028 + * either be load compensated or not. This value is added to the filter and a 1029 + * new mean value is calculated and returned. 1030 + */ 1031 + static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp) 1032 + { 1033 + int permille, mah; 1034 + 1035 + if (comp) 1036 + permille = ab8500_fg_load_comp_volt_to_capacity(di); 1037 + else 1038 + permille = ab8500_fg_uncomp_volt_to_capacity(di); 1039 + 1040 + mah = ab8500_fg_convert_permille_to_mah(di, permille); 1041 + 1042 + di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah); 1043 + di->bat_cap.permille = 1044 + ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1045 + 1046 + return di->bat_cap.mah; 1047 + } 1048 + 1049 + /** 1050 + * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG 1051 + * @di: pointer to the ab8500_fg structure 1052 + * 1053 + * Return the capacity in mAh based on previous calculated capcity and the FG 1054 + * accumulator register value. This value is added to the filter and a 1055 + * new mean value is calculated and returned. 1056 + */ 1057 + static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di) 1058 + { 1059 + int permille_volt, permille; 1060 + 1061 + dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 1062 + __func__, 1063 + di->bat_cap.mah, 1064 + di->accu_charge); 1065 + 1066 + /* Capacity should not be less than 0 */ 1067 + if (di->bat_cap.mah + di->accu_charge > 0) 1068 + di->bat_cap.mah += di->accu_charge; 1069 + else 1070 + di->bat_cap.mah = 0; 1071 + 1072 + if (di->bat_cap.mah >= di->bat_cap.max_mah_design) 1073 + di->bat_cap.mah = di->bat_cap.max_mah_design; 1074 + 1075 + /* 1076 + * Check against voltage based capacity. It can not be lower 1077 + * than what the uncompensated voltage says 1078 + */ 1079 + permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1080 + permille_volt = ab8500_fg_uncomp_volt_to_capacity(di); 1081 + 1082 + if (permille < permille_volt) { 1083 + di->bat_cap.permille = permille_volt; 1084 + di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di, 1085 + di->bat_cap.permille); 1086 + 1087 + dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n", 1088 + __func__, 1089 + permille, 1090 + permille_volt); 1091 + 1092 + ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1093 + } else { 1094 + ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1095 + di->bat_cap.permille = 1096 + ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1097 + } 1098 + 1099 + return di->bat_cap.mah; 1100 + } 1101 + 1102 + /** 1103 + * ab8500_fg_capacity_level() - Get the battery capacity level 1104 + * @di: pointer to the ab8500_fg structure 1105 + * 1106 + * Get the battery capacity level based on the capacity in percent 1107 + */ 1108 + static int ab8500_fg_capacity_level(struct ab8500_fg *di) 1109 + { 1110 + int ret, percent; 1111 + 1112 + percent = di->bat_cap.permille / 10; 1113 + 1114 + if (percent <= di->bat->cap_levels->critical || 1115 + di->flags.low_bat) 1116 + ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1117 + else if (percent <= di->bat->cap_levels->low) 1118 + ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1119 + else if (percent <= di->bat->cap_levels->normal) 1120 + ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1121 + else if (percent <= di->bat->cap_levels->high) 1122 + ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; 1123 + else 1124 + ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1125 + 1126 + return ret; 1127 + } 1128 + 1129 + /** 1130 + * ab8500_fg_check_capacity_limits() - Check if capacity has changed 1131 + * @di: pointer to the ab8500_fg structure 1132 + * @init: capacity is allowed to go up in init mode 1133 + * 1134 + * Check if capacity or capacity limit has changed and notify the system 1135 + * about it using the power_supply framework 1136 + */ 1137 + static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init) 1138 + { 1139 + bool changed = false; 1140 + 1141 + di->bat_cap.level = ab8500_fg_capacity_level(di); 1142 + 1143 + if (di->bat_cap.level != di->bat_cap.prev_level) { 1144 + /* 1145 + * We do not allow reported capacity level to go up 1146 + * unless we're charging or if we're in init 1147 + */ 1148 + if (!(!di->flags.charging && di->bat_cap.level > 1149 + di->bat_cap.prev_level) || init) { 1150 + dev_dbg(di->dev, "level changed from %d to %d\n", 1151 + di->bat_cap.prev_level, 1152 + di->bat_cap.level); 1153 + di->bat_cap.prev_level = di->bat_cap.level; 1154 + changed = true; 1155 + } else { 1156 + dev_dbg(di->dev, "level not allowed to go up " 1157 + "since no charger is connected: %d to %d\n", 1158 + di->bat_cap.prev_level, 1159 + di->bat_cap.level); 1160 + } 1161 + } 1162 + 1163 + /* 1164 + * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate 1165 + * shutdown 1166 + */ 1167 + if (di->flags.low_bat) { 1168 + dev_dbg(di->dev, "Battery low, set capacity to 0\n"); 1169 + di->bat_cap.prev_percent = 0; 1170 + di->bat_cap.permille = 0; 1171 + di->bat_cap.prev_mah = 0; 1172 + di->bat_cap.mah = 0; 1173 + changed = true; 1174 + } else if (di->flags.fully_charged) { 1175 + /* 1176 + * We report 100% if algorithm reported fully charged 1177 + * unless capacity drops too much 1178 + */ 1179 + if (di->flags.force_full) { 1180 + di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1181 + di->bat_cap.prev_mah = di->bat_cap.mah; 1182 + } else if (!di->flags.force_full && 1183 + di->bat_cap.prev_percent != 1184 + (di->bat_cap.permille) / 10 && 1185 + (di->bat_cap.permille / 10) < 1186 + di->bat->fg_params->maint_thres) { 1187 + dev_dbg(di->dev, 1188 + "battery reported full " 1189 + "but capacity dropping: %d\n", 1190 + di->bat_cap.permille / 10); 1191 + di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1192 + di->bat_cap.prev_mah = di->bat_cap.mah; 1193 + 1194 + changed = true; 1195 + } 1196 + } else if (di->bat_cap.prev_percent != di->bat_cap.permille / 10) { 1197 + if (di->bat_cap.permille / 10 == 0) { 1198 + /* 1199 + * We will not report 0% unless we've got 1200 + * the LOW_BAT IRQ, no matter what the FG 1201 + * algorithm says. 1202 + */ 1203 + di->bat_cap.prev_percent = 1; 1204 + di->bat_cap.permille = 1; 1205 + di->bat_cap.prev_mah = 1; 1206 + di->bat_cap.mah = 1; 1207 + 1208 + changed = true; 1209 + } else if (!(!di->flags.charging && 1210 + (di->bat_cap.permille / 10) > 1211 + di->bat_cap.prev_percent) || init) { 1212 + /* 1213 + * We do not allow reported capacity to go up 1214 + * unless we're charging or if we're in init 1215 + */ 1216 + dev_dbg(di->dev, 1217 + "capacity changed from %d to %d (%d)\n", 1218 + di->bat_cap.prev_percent, 1219 + di->bat_cap.permille / 10, 1220 + di->bat_cap.permille); 1221 + di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1222 + di->bat_cap.prev_mah = di->bat_cap.mah; 1223 + 1224 + changed = true; 1225 + } else { 1226 + dev_dbg(di->dev, "capacity not allowed to go up since " 1227 + "no charger is connected: %d to %d (%d)\n", 1228 + di->bat_cap.prev_percent, 1229 + di->bat_cap.permille / 10, 1230 + di->bat_cap.permille); 1231 + } 1232 + } 1233 + 1234 + if (changed) { 1235 + power_supply_changed(&di->fg_psy); 1236 + if (di->flags.fully_charged && di->flags.force_full) { 1237 + dev_dbg(di->dev, "Battery full, notifying.\n"); 1238 + di->flags.force_full = false; 1239 + sysfs_notify(&di->fg_kobject, NULL, "charge_full"); 1240 + } 1241 + sysfs_notify(&di->fg_kobject, NULL, "charge_now"); 1242 + } 1243 + } 1244 + 1245 + static void ab8500_fg_charge_state_to(struct ab8500_fg *di, 1246 + enum ab8500_fg_charge_state new_state) 1247 + { 1248 + dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n", 1249 + di->charge_state, 1250 + charge_state[di->charge_state], 1251 + new_state, 1252 + charge_state[new_state]); 1253 + 1254 + di->charge_state = new_state; 1255 + } 1256 + 1257 + static void ab8500_fg_discharge_state_to(struct ab8500_fg *di, 1258 + enum ab8500_fg_discharge_state new_state) 1259 + { 1260 + dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n", 1261 + di->discharge_state, 1262 + discharge_state[di->discharge_state], 1263 + new_state, 1264 + discharge_state[new_state]); 1265 + 1266 + di->discharge_state = new_state; 1267 + } 1268 + 1269 + /** 1270 + * ab8500_fg_algorithm_charging() - FG algorithm for when charging 1271 + * @di: pointer to the ab8500_fg structure 1272 + * 1273 + * Battery capacity calculation state machine for when we're charging 1274 + */ 1275 + static void ab8500_fg_algorithm_charging(struct ab8500_fg *di) 1276 + { 1277 + /* 1278 + * If we change to discharge mode 1279 + * we should start with recovery 1280 + */ 1281 + if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY) 1282 + ab8500_fg_discharge_state_to(di, 1283 + AB8500_FG_DISCHARGE_INIT_RECOVERY); 1284 + 1285 + switch (di->charge_state) { 1286 + case AB8500_FG_CHARGE_INIT: 1287 + di->fg_samples = SEC_TO_SAMPLE( 1288 + di->bat->fg_params->accu_charging); 1289 + 1290 + ab8500_fg_coulomb_counter(di, true); 1291 + ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT); 1292 + 1293 + break; 1294 + 1295 + case AB8500_FG_CHARGE_READOUT: 1296 + /* 1297 + * Read the FG and calculate the new capacity 1298 + */ 1299 + mutex_lock(&di->cc_lock); 1300 + if (!di->flags.conv_done) { 1301 + /* Wasn't the CC IRQ that got us here */ 1302 + mutex_unlock(&di->cc_lock); 1303 + dev_dbg(di->dev, "%s CC conv not done\n", 1304 + __func__); 1305 + 1306 + break; 1307 + } 1308 + di->flags.conv_done = false; 1309 + mutex_unlock(&di->cc_lock); 1310 + 1311 + ab8500_fg_calc_cap_charging(di); 1312 + 1313 + break; 1314 + 1315 + default: 1316 + break; 1317 + } 1318 + 1319 + /* Check capacity limits */ 1320 + ab8500_fg_check_capacity_limits(di, false); 1321 + } 1322 + 1323 + static void force_capacity(struct ab8500_fg *di) 1324 + { 1325 + int cap; 1326 + 1327 + ab8500_fg_clear_cap_samples(di); 1328 + cap = di->bat_cap.user_mah; 1329 + if (cap > di->bat_cap.max_mah_design) { 1330 + dev_dbg(di->dev, "Remaining cap %d can't be bigger than total" 1331 + " %d\n", cap, di->bat_cap.max_mah_design); 1332 + cap = di->bat_cap.max_mah_design; 1333 + } 1334 + ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah); 1335 + di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap); 1336 + di->bat_cap.mah = cap; 1337 + ab8500_fg_check_capacity_limits(di, true); 1338 + } 1339 + 1340 + static bool check_sysfs_capacity(struct ab8500_fg *di) 1341 + { 1342 + int cap, lower, upper; 1343 + int cap_permille; 1344 + 1345 + cap = di->bat_cap.user_mah; 1346 + 1347 + cap_permille = ab8500_fg_convert_mah_to_permille(di, 1348 + di->bat_cap.user_mah); 1349 + 1350 + lower = di->bat_cap.permille - di->bat->fg_params->user_cap_limit * 10; 1351 + upper = di->bat_cap.permille + di->bat->fg_params->user_cap_limit * 10; 1352 + 1353 + if (lower < 0) 1354 + lower = 0; 1355 + /* 1000 is permille, -> 100 percent */ 1356 + if (upper > 1000) 1357 + upper = 1000; 1358 + 1359 + dev_dbg(di->dev, "Capacity limits:" 1360 + " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n", 1361 + lower, cap_permille, upper, cap, di->bat_cap.mah); 1362 + 1363 + /* If within limits, use the saved capacity and exit estimation...*/ 1364 + if (cap_permille > lower && cap_permille < upper) { 1365 + dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap); 1366 + force_capacity(di); 1367 + return true; 1368 + } 1369 + dev_dbg(di->dev, "Capacity from user out of limits, ignoring"); 1370 + return false; 1371 + } 1372 + 1373 + /** 1374 + * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging 1375 + * @di: pointer to the ab8500_fg structure 1376 + * 1377 + * Battery capacity calculation state machine for when we're discharging 1378 + */ 1379 + static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di) 1380 + { 1381 + int sleep_time; 1382 + 1383 + /* If we change to charge mode we should start with init */ 1384 + if (di->charge_state != AB8500_FG_CHARGE_INIT) 1385 + ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 1386 + 1387 + switch (di->discharge_state) { 1388 + case AB8500_FG_DISCHARGE_INIT: 1389 + /* We use the FG IRQ to work on */ 1390 + di->init_cnt = 0; 1391 + di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer); 1392 + ab8500_fg_coulomb_counter(di, true); 1393 + ab8500_fg_discharge_state_to(di, 1394 + AB8500_FG_DISCHARGE_INITMEASURING); 1395 + 1396 + /* Intentional fallthrough */ 1397 + case AB8500_FG_DISCHARGE_INITMEASURING: 1398 + /* 1399 + * Discard a number of samples during startup. 1400 + * After that, use compensated voltage for a few 1401 + * samples to get an initial capacity. 1402 + * Then go to READOUT 1403 + */ 1404 + sleep_time = di->bat->fg_params->init_timer; 1405 + 1406 + /* Discard the first [x] seconds */ 1407 + if (di->init_cnt > 1408 + di->bat->fg_params->init_discard_time) { 1409 + ab8500_fg_calc_cap_discharge_voltage(di, true); 1410 + 1411 + ab8500_fg_check_capacity_limits(di, true); 1412 + } 1413 + 1414 + di->init_cnt += sleep_time; 1415 + if (di->init_cnt > di->bat->fg_params->init_total_time) 1416 + ab8500_fg_discharge_state_to(di, 1417 + AB8500_FG_DISCHARGE_READOUT_INIT); 1418 + 1419 + break; 1420 + 1421 + case AB8500_FG_DISCHARGE_INIT_RECOVERY: 1422 + di->recovery_cnt = 0; 1423 + di->recovery_needed = true; 1424 + ab8500_fg_discharge_state_to(di, 1425 + AB8500_FG_DISCHARGE_RECOVERY); 1426 + 1427 + /* Intentional fallthrough */ 1428 + 1429 + case AB8500_FG_DISCHARGE_RECOVERY: 1430 + sleep_time = di->bat->fg_params->recovery_sleep_timer; 1431 + 1432 + /* 1433 + * We should check the power consumption 1434 + * If low, go to READOUT (after x min) or 1435 + * RECOVERY_SLEEP if time left. 1436 + * If high, go to READOUT 1437 + */ 1438 + di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1439 + 1440 + if (ab8500_fg_is_low_curr(di, di->inst_curr)) { 1441 + if (di->recovery_cnt > 1442 + di->bat->fg_params->recovery_total_time) { 1443 + di->fg_samples = SEC_TO_SAMPLE( 1444 + di->bat->fg_params->accu_high_curr); 1445 + ab8500_fg_coulomb_counter(di, true); 1446 + ab8500_fg_discharge_state_to(di, 1447 + AB8500_FG_DISCHARGE_READOUT); 1448 + di->recovery_needed = false; 1449 + } else { 1450 + queue_delayed_work(di->fg_wq, 1451 + &di->fg_periodic_work, 1452 + sleep_time * HZ); 1453 + } 1454 + di->recovery_cnt += sleep_time; 1455 + } else { 1456 + di->fg_samples = SEC_TO_SAMPLE( 1457 + di->bat->fg_params->accu_high_curr); 1458 + ab8500_fg_coulomb_counter(di, true); 1459 + ab8500_fg_discharge_state_to(di, 1460 + AB8500_FG_DISCHARGE_READOUT); 1461 + } 1462 + break; 1463 + 1464 + case AB8500_FG_DISCHARGE_READOUT_INIT: 1465 + di->fg_samples = SEC_TO_SAMPLE( 1466 + di->bat->fg_params->accu_high_curr); 1467 + ab8500_fg_coulomb_counter(di, true); 1468 + ab8500_fg_discharge_state_to(di, 1469 + AB8500_FG_DISCHARGE_READOUT); 1470 + break; 1471 + 1472 + case AB8500_FG_DISCHARGE_READOUT: 1473 + di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1474 + 1475 + if (ab8500_fg_is_low_curr(di, di->inst_curr)) { 1476 + /* Detect mode change */ 1477 + if (di->high_curr_mode) { 1478 + di->high_curr_mode = false; 1479 + di->high_curr_cnt = 0; 1480 + } 1481 + 1482 + if (di->recovery_needed) { 1483 + ab8500_fg_discharge_state_to(di, 1484 + AB8500_FG_DISCHARGE_RECOVERY); 1485 + 1486 + queue_delayed_work(di->fg_wq, 1487 + &di->fg_periodic_work, 0); 1488 + 1489 + break; 1490 + } 1491 + 1492 + ab8500_fg_calc_cap_discharge_voltage(di, true); 1493 + } else { 1494 + mutex_lock(&di->cc_lock); 1495 + if (!di->flags.conv_done) { 1496 + /* Wasn't the CC IRQ that got us here */ 1497 + mutex_unlock(&di->cc_lock); 1498 + dev_dbg(di->dev, "%s CC conv not done\n", 1499 + __func__); 1500 + 1501 + break; 1502 + } 1503 + di->flags.conv_done = false; 1504 + mutex_unlock(&di->cc_lock); 1505 + 1506 + /* Detect mode change */ 1507 + if (!di->high_curr_mode) { 1508 + di->high_curr_mode = true; 1509 + di->high_curr_cnt = 0; 1510 + } 1511 + 1512 + di->high_curr_cnt += 1513 + di->bat->fg_params->accu_high_curr; 1514 + if (di->high_curr_cnt > 1515 + di->bat->fg_params->high_curr_time) 1516 + di->recovery_needed = true; 1517 + 1518 + ab8500_fg_calc_cap_discharge_fg(di); 1519 + } 1520 + 1521 + ab8500_fg_check_capacity_limits(di, false); 1522 + 1523 + break; 1524 + 1525 + case AB8500_FG_DISCHARGE_WAKEUP: 1526 + ab8500_fg_coulomb_counter(di, true); 1527 + di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1528 + 1529 + ab8500_fg_calc_cap_discharge_voltage(di, true); 1530 + 1531 + di->fg_samples = SEC_TO_SAMPLE( 1532 + di->bat->fg_params->accu_high_curr); 1533 + ab8500_fg_coulomb_counter(di, true); 1534 + ab8500_fg_discharge_state_to(di, 1535 + AB8500_FG_DISCHARGE_READOUT); 1536 + 1537 + ab8500_fg_check_capacity_limits(di, false); 1538 + 1539 + break; 1540 + 1541 + default: 1542 + break; 1543 + } 1544 + } 1545 + 1546 + /** 1547 + * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration 1548 + * @di: pointer to the ab8500_fg structure 1549 + * 1550 + */ 1551 + static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di) 1552 + { 1553 + int ret; 1554 + 1555 + switch (di->calib_state) { 1556 + case AB8500_FG_CALIB_INIT: 1557 + dev_dbg(di->dev, "Calibration ongoing...\n"); 1558 + 1559 + ret = abx500_mask_and_set_register_interruptible(di->dev, 1560 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1561 + CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8); 1562 + if (ret < 0) 1563 + goto err; 1564 + 1565 + ret = abx500_mask_and_set_register_interruptible(di->dev, 1566 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1567 + CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA); 1568 + if (ret < 0) 1569 + goto err; 1570 + di->calib_state = AB8500_FG_CALIB_WAIT; 1571 + break; 1572 + case AB8500_FG_CALIB_END: 1573 + ret = abx500_mask_and_set_register_interruptible(di->dev, 1574 + AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1575 + CC_MUXOFFSET, CC_MUXOFFSET); 1576 + if (ret < 0) 1577 + goto err; 1578 + di->flags.calibrate = false; 1579 + dev_dbg(di->dev, "Calibration done...\n"); 1580 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1581 + break; 1582 + case AB8500_FG_CALIB_WAIT: 1583 + dev_dbg(di->dev, "Calibration WFI\n"); 1584 + default: 1585 + break; 1586 + } 1587 + return; 1588 + err: 1589 + /* Something went wrong, don't calibrate then */ 1590 + dev_err(di->dev, "failed to calibrate the CC\n"); 1591 + di->flags.calibrate = false; 1592 + di->calib_state = AB8500_FG_CALIB_INIT; 1593 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1594 + } 1595 + 1596 + /** 1597 + * ab8500_fg_algorithm() - Entry point for the FG algorithm 1598 + * @di: pointer to the ab8500_fg structure 1599 + * 1600 + * Entry point for the battery capacity calculation state machine 1601 + */ 1602 + static void ab8500_fg_algorithm(struct ab8500_fg *di) 1603 + { 1604 + if (di->flags.calibrate) 1605 + ab8500_fg_algorithm_calibrate(di); 1606 + else { 1607 + if (di->flags.charging) 1608 + ab8500_fg_algorithm_charging(di); 1609 + else 1610 + ab8500_fg_algorithm_discharging(di); 1611 + } 1612 + 1613 + dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d " 1614 + "%d %d %d %d %d %d %d\n", 1615 + di->bat_cap.max_mah_design, 1616 + di->bat_cap.mah, 1617 + di->bat_cap.permille, 1618 + di->bat_cap.level, 1619 + di->bat_cap.prev_mah, 1620 + di->bat_cap.prev_percent, 1621 + di->bat_cap.prev_level, 1622 + di->vbat, 1623 + di->inst_curr, 1624 + di->avg_curr, 1625 + di->accu_charge, 1626 + di->flags.charging, 1627 + di->charge_state, 1628 + di->discharge_state, 1629 + di->high_curr_mode, 1630 + di->recovery_needed); 1631 + } 1632 + 1633 + /** 1634 + * ab8500_fg_periodic_work() - Run the FG state machine periodically 1635 + * @work: pointer to the work_struct structure 1636 + * 1637 + * Work queue function for periodic work 1638 + */ 1639 + static void ab8500_fg_periodic_work(struct work_struct *work) 1640 + { 1641 + struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1642 + fg_periodic_work.work); 1643 + 1644 + if (di->init_capacity) { 1645 + /* A dummy read that will return 0 */ 1646 + di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1647 + /* Get an initial capacity calculation */ 1648 + ab8500_fg_calc_cap_discharge_voltage(di, true); 1649 + ab8500_fg_check_capacity_limits(di, true); 1650 + di->init_capacity = false; 1651 + 1652 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1653 + } else if (di->flags.user_cap) { 1654 + if (check_sysfs_capacity(di)) { 1655 + ab8500_fg_check_capacity_limits(di, true); 1656 + if (di->flags.charging) 1657 + ab8500_fg_charge_state_to(di, 1658 + AB8500_FG_CHARGE_INIT); 1659 + else 1660 + ab8500_fg_discharge_state_to(di, 1661 + AB8500_FG_DISCHARGE_READOUT_INIT); 1662 + } 1663 + di->flags.user_cap = false; 1664 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1665 + } else 1666 + ab8500_fg_algorithm(di); 1667 + 1668 + } 1669 + 1670 + /** 1671 + * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition 1672 + * @work: pointer to the work_struct structure 1673 + * 1674 + * Work queue function for checking the OVV_BAT condition 1675 + */ 1676 + static void ab8500_fg_check_hw_failure_work(struct work_struct *work) 1677 + { 1678 + int ret; 1679 + u8 reg_value; 1680 + 1681 + struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1682 + fg_check_hw_failure_work.work); 1683 + 1684 + /* 1685 + * If we have had a battery over-voltage situation, 1686 + * check ovv-bit to see if it should be reset. 1687 + */ 1688 + if (di->flags.bat_ovv) { 1689 + ret = abx500_get_register_interruptible(di->dev, 1690 + AB8500_CHARGER, AB8500_CH_STAT_REG, 1691 + &reg_value); 1692 + if (ret < 0) { 1693 + dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1694 + return; 1695 + } 1696 + if ((reg_value & BATT_OVV) != BATT_OVV) { 1697 + dev_dbg(di->dev, "Battery recovered from OVV\n"); 1698 + di->flags.bat_ovv = false; 1699 + power_supply_changed(&di->fg_psy); 1700 + return; 1701 + } 1702 + 1703 + /* Not yet recovered from ovv, reschedule this test */ 1704 + queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 1705 + round_jiffies(HZ)); 1706 + } 1707 + } 1708 + 1709 + /** 1710 + * ab8500_fg_low_bat_work() - Check LOW_BAT condition 1711 + * @work: pointer to the work_struct structure 1712 + * 1713 + * Work queue function for checking the LOW_BAT condition 1714 + */ 1715 + static void ab8500_fg_low_bat_work(struct work_struct *work) 1716 + { 1717 + int vbat; 1718 + 1719 + struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1720 + fg_low_bat_work.work); 1721 + 1722 + vbat = ab8500_fg_bat_voltage(di); 1723 + 1724 + /* Check if LOW_BAT still fulfilled */ 1725 + if (vbat < di->bat->fg_params->lowbat_threshold) { 1726 + di->flags.low_bat = true; 1727 + dev_warn(di->dev, "Battery voltage still LOW\n"); 1728 + 1729 + /* 1730 + * We need to re-schedule this check to be able to detect 1731 + * if the voltage increases again during charging 1732 + */ 1733 + queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 1734 + round_jiffies(LOW_BAT_CHECK_INTERVAL)); 1735 + } else { 1736 + di->flags.low_bat = false; 1737 + dev_warn(di->dev, "Battery voltage OK again\n"); 1738 + } 1739 + 1740 + /* This is needed to dispatch LOW_BAT */ 1741 + ab8500_fg_check_capacity_limits(di, false); 1742 + 1743 + /* Set this flag to check if LOW_BAT IRQ still occurs */ 1744 + di->flags.low_bat_delay = false; 1745 + } 1746 + 1747 + /** 1748 + * ab8500_fg_battok_calc - calculate the bit pattern corresponding 1749 + * to the target voltage. 1750 + * @di: pointer to the ab8500_fg structure 1751 + * @target target voltage 1752 + * 1753 + * Returns bit pattern closest to the target voltage 1754 + * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS) 1755 + */ 1756 + 1757 + static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target) 1758 + { 1759 + if (target > BATT_OK_MIN + 1760 + (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS)) 1761 + return BATT_OK_MAX_NR_INCREMENTS; 1762 + if (target < BATT_OK_MIN) 1763 + return 0; 1764 + return (target - BATT_OK_MIN) / BATT_OK_INCREMENT; 1765 + } 1766 + 1767 + /** 1768 + * ab8500_fg_battok_init_hw_register - init battok levels 1769 + * @di: pointer to the ab8500_fg structure 1770 + * 1771 + */ 1772 + 1773 + static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di) 1774 + { 1775 + int selected; 1776 + int sel0; 1777 + int sel1; 1778 + int cbp_sel0; 1779 + int cbp_sel1; 1780 + int ret; 1781 + int new_val; 1782 + 1783 + sel0 = di->bat->fg_params->battok_falling_th_sel0; 1784 + sel1 = di->bat->fg_params->battok_raising_th_sel1; 1785 + 1786 + cbp_sel0 = ab8500_fg_battok_calc(di, sel0); 1787 + cbp_sel1 = ab8500_fg_battok_calc(di, sel1); 1788 + 1789 + selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT; 1790 + 1791 + if (selected != sel0) 1792 + dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1793 + sel0, selected, cbp_sel0); 1794 + 1795 + selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT; 1796 + 1797 + if (selected != sel1) 1798 + dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1799 + sel1, selected, cbp_sel1); 1800 + 1801 + new_val = cbp_sel0 | (cbp_sel1 << 4); 1802 + 1803 + dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1); 1804 + ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK, 1805 + AB8500_BATT_OK_REG, new_val); 1806 + return ret; 1807 + } 1808 + 1809 + /** 1810 + * ab8500_fg_instant_work() - Run the FG state machine instantly 1811 + * @work: pointer to the work_struct structure 1812 + * 1813 + * Work queue function for instant work 1814 + */ 1815 + static void ab8500_fg_instant_work(struct work_struct *work) 1816 + { 1817 + struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work); 1818 + 1819 + ab8500_fg_algorithm(di); 1820 + } 1821 + 1822 + /** 1823 + * ab8500_fg_cc_data_end_handler() - isr to get battery avg current. 1824 + * @irq: interrupt number 1825 + * @_di: pointer to the ab8500_fg structure 1826 + * 1827 + * Returns IRQ status(IRQ_HANDLED) 1828 + */ 1829 + static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di) 1830 + { 1831 + struct ab8500_fg *di = _di; 1832 + complete(&di->ab8500_fg_complete); 1833 + return IRQ_HANDLED; 1834 + } 1835 + 1836 + /** 1837 + * ab8500_fg_cc_convend_handler() - isr to get battery avg current. 1838 + * @irq: interrupt number 1839 + * @_di: pointer to the ab8500_fg structure 1840 + * 1841 + * Returns IRQ status(IRQ_HANDLED) 1842 + */ 1843 + static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di) 1844 + { 1845 + struct ab8500_fg *di = _di; 1846 + di->calib_state = AB8500_FG_CALIB_END; 1847 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1848 + return IRQ_HANDLED; 1849 + } 1850 + 1851 + /** 1852 + * ab8500_fg_cc_convend_handler() - isr to get battery avg current. 1853 + * @irq: interrupt number 1854 + * @_di: pointer to the ab8500_fg structure 1855 + * 1856 + * Returns IRQ status(IRQ_HANDLED) 1857 + */ 1858 + static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di) 1859 + { 1860 + struct ab8500_fg *di = _di; 1861 + 1862 + queue_work(di->fg_wq, &di->fg_acc_cur_work); 1863 + 1864 + return IRQ_HANDLED; 1865 + } 1866 + 1867 + /** 1868 + * ab8500_fg_batt_ovv_handler() - Battery OVV occured 1869 + * @irq: interrupt number 1870 + * @_di: pointer to the ab8500_fg structure 1871 + * 1872 + * Returns IRQ status(IRQ_HANDLED) 1873 + */ 1874 + static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di) 1875 + { 1876 + struct ab8500_fg *di = _di; 1877 + 1878 + dev_dbg(di->dev, "Battery OVV\n"); 1879 + di->flags.bat_ovv = true; 1880 + power_supply_changed(&di->fg_psy); 1881 + 1882 + /* Schedule a new HW failure check */ 1883 + queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0); 1884 + 1885 + return IRQ_HANDLED; 1886 + } 1887 + 1888 + /** 1889 + * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold 1890 + * @irq: interrupt number 1891 + * @_di: pointer to the ab8500_fg structure 1892 + * 1893 + * Returns IRQ status(IRQ_HANDLED) 1894 + */ 1895 + static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di) 1896 + { 1897 + struct ab8500_fg *di = _di; 1898 + 1899 + if (!di->flags.low_bat_delay) { 1900 + dev_warn(di->dev, "Battery voltage is below LOW threshold\n"); 1901 + di->flags.low_bat_delay = true; 1902 + /* 1903 + * Start a timer to check LOW_BAT again after some time 1904 + * This is done to avoid shutdown on single voltage dips 1905 + */ 1906 + queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 1907 + round_jiffies(LOW_BAT_CHECK_INTERVAL)); 1908 + } 1909 + return IRQ_HANDLED; 1910 + } 1911 + 1912 + /** 1913 + * ab8500_fg_get_property() - get the fg properties 1914 + * @psy: pointer to the power_supply structure 1915 + * @psp: pointer to the power_supply_property structure 1916 + * @val: pointer to the power_supply_propval union 1917 + * 1918 + * This function gets called when an application tries to get the 1919 + * fg properties by reading the sysfs files. 1920 + * voltage_now: battery voltage 1921 + * current_now: battery instant current 1922 + * current_avg: battery average current 1923 + * charge_full_design: capacity where battery is considered full 1924 + * charge_now: battery capacity in nAh 1925 + * capacity: capacity in percent 1926 + * capacity_level: capacity level 1927 + * 1928 + * Returns error code in case of failure else 0 on success 1929 + */ 1930 + static int ab8500_fg_get_property(struct power_supply *psy, 1931 + enum power_supply_property psp, 1932 + union power_supply_propval *val) 1933 + { 1934 + struct ab8500_fg *di; 1935 + 1936 + di = to_ab8500_fg_device_info(psy); 1937 + 1938 + /* 1939 + * If battery is identified as unknown and charging of unknown 1940 + * batteries is disabled, we always report 100% capacity and 1941 + * capacity level UNKNOWN, since we can't calculate 1942 + * remaining capacity 1943 + */ 1944 + 1945 + switch (psp) { 1946 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1947 + if (di->flags.bat_ovv) 1948 + val->intval = BATT_OVV_VALUE * 1000; 1949 + else 1950 + val->intval = di->vbat * 1000; 1951 + break; 1952 + case POWER_SUPPLY_PROP_CURRENT_NOW: 1953 + val->intval = di->inst_curr * 1000; 1954 + break; 1955 + case POWER_SUPPLY_PROP_CURRENT_AVG: 1956 + val->intval = di->avg_curr * 1000; 1957 + break; 1958 + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 1959 + val->intval = ab8500_fg_convert_mah_to_uwh(di, 1960 + di->bat_cap.max_mah_design); 1961 + break; 1962 + case POWER_SUPPLY_PROP_ENERGY_FULL: 1963 + val->intval = ab8500_fg_convert_mah_to_uwh(di, 1964 + di->bat_cap.max_mah); 1965 + break; 1966 + case POWER_SUPPLY_PROP_ENERGY_NOW: 1967 + if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1968 + di->flags.batt_id_received) 1969 + val->intval = ab8500_fg_convert_mah_to_uwh(di, 1970 + di->bat_cap.max_mah); 1971 + else 1972 + val->intval = ab8500_fg_convert_mah_to_uwh(di, 1973 + di->bat_cap.prev_mah); 1974 + break; 1975 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 1976 + val->intval = di->bat_cap.max_mah_design; 1977 + break; 1978 + case POWER_SUPPLY_PROP_CHARGE_FULL: 1979 + val->intval = di->bat_cap.max_mah; 1980 + break; 1981 + case POWER_SUPPLY_PROP_CHARGE_NOW: 1982 + if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1983 + di->flags.batt_id_received) 1984 + val->intval = di->bat_cap.max_mah; 1985 + else 1986 + val->intval = di->bat_cap.prev_mah; 1987 + break; 1988 + case POWER_SUPPLY_PROP_CAPACITY: 1989 + if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1990 + di->flags.batt_id_received) 1991 + val->intval = 100; 1992 + else 1993 + val->intval = di->bat_cap.prev_percent; 1994 + break; 1995 + case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 1996 + if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1997 + di->flags.batt_id_received) 1998 + val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 1999 + else 2000 + val->intval = di->bat_cap.prev_level; 2001 + break; 2002 + default: 2003 + return -EINVAL; 2004 + } 2005 + return 0; 2006 + } 2007 + 2008 + static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data) 2009 + { 2010 + struct power_supply *psy; 2011 + struct power_supply *ext; 2012 + struct ab8500_fg *di; 2013 + union power_supply_propval ret; 2014 + int i, j; 2015 + bool psy_found = false; 2016 + 2017 + psy = (struct power_supply *)data; 2018 + ext = dev_get_drvdata(dev); 2019 + di = to_ab8500_fg_device_info(psy); 2020 + 2021 + /* 2022 + * For all psy where the name of your driver 2023 + * appears in any supplied_to 2024 + */ 2025 + for (i = 0; i < ext->num_supplicants; i++) { 2026 + if (!strcmp(ext->supplied_to[i], psy->name)) 2027 + psy_found = true; 2028 + } 2029 + 2030 + if (!psy_found) 2031 + return 0; 2032 + 2033 + /* Go through all properties for the psy */ 2034 + for (j = 0; j < ext->num_properties; j++) { 2035 + enum power_supply_property prop; 2036 + prop = ext->properties[j]; 2037 + 2038 + if (ext->get_property(ext, prop, &ret)) 2039 + continue; 2040 + 2041 + switch (prop) { 2042 + case POWER_SUPPLY_PROP_STATUS: 2043 + switch (ext->type) { 2044 + case POWER_SUPPLY_TYPE_BATTERY: 2045 + switch (ret.intval) { 2046 + case POWER_SUPPLY_STATUS_UNKNOWN: 2047 + case POWER_SUPPLY_STATUS_DISCHARGING: 2048 + case POWER_SUPPLY_STATUS_NOT_CHARGING: 2049 + if (!di->flags.charging) 2050 + break; 2051 + di->flags.charging = false; 2052 + di->flags.fully_charged = false; 2053 + queue_work(di->fg_wq, &di->fg_work); 2054 + break; 2055 + case POWER_SUPPLY_STATUS_FULL: 2056 + if (di->flags.fully_charged) 2057 + break; 2058 + di->flags.fully_charged = true; 2059 + di->flags.force_full = true; 2060 + /* Save current capacity as maximum */ 2061 + di->bat_cap.max_mah = di->bat_cap.mah; 2062 + queue_work(di->fg_wq, &di->fg_work); 2063 + break; 2064 + case POWER_SUPPLY_STATUS_CHARGING: 2065 + if (di->flags.charging) 2066 + break; 2067 + di->flags.charging = true; 2068 + di->flags.fully_charged = false; 2069 + queue_work(di->fg_wq, &di->fg_work); 2070 + break; 2071 + }; 2072 + default: 2073 + break; 2074 + }; 2075 + break; 2076 + case POWER_SUPPLY_PROP_TECHNOLOGY: 2077 + switch (ext->type) { 2078 + case POWER_SUPPLY_TYPE_BATTERY: 2079 + if (!di->flags.batt_id_received) { 2080 + const struct abx500_battery_type *b; 2081 + 2082 + b = &(di->bat->bat_type[di->bat->batt_id]); 2083 + 2084 + di->flags.batt_id_received = true; 2085 + 2086 + di->bat_cap.max_mah_design = 2087 + MILLI_TO_MICRO * 2088 + b->charge_full_design; 2089 + 2090 + di->bat_cap.max_mah = 2091 + di->bat_cap.max_mah_design; 2092 + 2093 + di->vbat_nom = b->nominal_voltage; 2094 + } 2095 + 2096 + if (ret.intval) 2097 + di->flags.batt_unknown = false; 2098 + else 2099 + di->flags.batt_unknown = true; 2100 + break; 2101 + default: 2102 + break; 2103 + } 2104 + break; 2105 + case POWER_SUPPLY_PROP_TEMP: 2106 + switch (ext->type) { 2107 + case POWER_SUPPLY_TYPE_BATTERY: 2108 + if (di->flags.batt_id_received) 2109 + di->bat_temp = ret.intval; 2110 + break; 2111 + default: 2112 + break; 2113 + } 2114 + break; 2115 + default: 2116 + break; 2117 + } 2118 + } 2119 + return 0; 2120 + } 2121 + 2122 + /** 2123 + * ab8500_fg_init_hw_registers() - Set up FG related registers 2124 + * @di: pointer to the ab8500_fg structure 2125 + * 2126 + * Set up battery OVV, low battery voltage registers 2127 + */ 2128 + static int ab8500_fg_init_hw_registers(struct ab8500_fg *di) 2129 + { 2130 + int ret; 2131 + 2132 + /* Set VBAT OVV threshold */ 2133 + ret = abx500_mask_and_set_register_interruptible(di->dev, 2134 + AB8500_CHARGER, 2135 + AB8500_BATT_OVV, 2136 + BATT_OVV_TH_4P75, 2137 + BATT_OVV_TH_4P75); 2138 + if (ret) { 2139 + dev_err(di->dev, "failed to set BATT_OVV\n"); 2140 + goto out; 2141 + } 2142 + 2143 + /* Enable VBAT OVV detection */ 2144 + ret = abx500_mask_and_set_register_interruptible(di->dev, 2145 + AB8500_CHARGER, 2146 + AB8500_BATT_OVV, 2147 + BATT_OVV_ENA, 2148 + BATT_OVV_ENA); 2149 + if (ret) { 2150 + dev_err(di->dev, "failed to enable BATT_OVV\n"); 2151 + goto out; 2152 + } 2153 + 2154 + /* Low Battery Voltage */ 2155 + ret = abx500_set_register_interruptible(di->dev, 2156 + AB8500_SYS_CTRL2_BLOCK, 2157 + AB8500_LOW_BAT_REG, 2158 + ab8500_volt_to_regval( 2159 + di->bat->fg_params->lowbat_threshold) << 1 | 2160 + LOW_BAT_ENABLE); 2161 + if (ret) { 2162 + dev_err(di->dev, "%s write failed\n", __func__); 2163 + goto out; 2164 + } 2165 + 2166 + /* Battery OK threshold */ 2167 + ret = ab8500_fg_battok_init_hw_register(di); 2168 + if (ret) { 2169 + dev_err(di->dev, "BattOk init write failed.\n"); 2170 + goto out; 2171 + } 2172 + out: 2173 + return ret; 2174 + } 2175 + 2176 + /** 2177 + * ab8500_fg_external_power_changed() - callback for power supply changes 2178 + * @psy: pointer to the structure power_supply 2179 + * 2180 + * This function is the entry point of the pointer external_power_changed 2181 + * of the structure power_supply. 2182 + * This function gets executed when there is a change in any external power 2183 + * supply that this driver needs to be notified of. 2184 + */ 2185 + static void ab8500_fg_external_power_changed(struct power_supply *psy) 2186 + { 2187 + struct ab8500_fg *di = to_ab8500_fg_device_info(psy); 2188 + 2189 + class_for_each_device(power_supply_class, NULL, 2190 + &di->fg_psy, ab8500_fg_get_ext_psy_data); 2191 + } 2192 + 2193 + /** 2194 + * abab8500_fg_reinit_work() - work to reset the FG algorithm 2195 + * @work: pointer to the work_struct structure 2196 + * 2197 + * Used to reset the current battery capacity to be able to 2198 + * retrigger a new voltage base capacity calculation. For 2199 + * test and verification purpose. 2200 + */ 2201 + static void ab8500_fg_reinit_work(struct work_struct *work) 2202 + { 2203 + struct ab8500_fg *di = container_of(work, struct ab8500_fg, 2204 + fg_reinit_work.work); 2205 + 2206 + if (di->flags.calibrate == false) { 2207 + dev_dbg(di->dev, "Resetting FG state machine to init.\n"); 2208 + ab8500_fg_clear_cap_samples(di); 2209 + ab8500_fg_calc_cap_discharge_voltage(di, true); 2210 + ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 2211 + ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 2212 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2213 + 2214 + } else { 2215 + dev_err(di->dev, "Residual offset calibration ongoing " 2216 + "retrying..\n"); 2217 + /* Wait one second until next try*/ 2218 + queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 2219 + round_jiffies(1)); 2220 + } 2221 + } 2222 + 2223 + /** 2224 + * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values 2225 + * 2226 + * This function can be used to force the FG algorithm to recalculate a new 2227 + * voltage based battery capacity. 2228 + */ 2229 + void ab8500_fg_reinit(void) 2230 + { 2231 + struct ab8500_fg *di = ab8500_fg_get(); 2232 + /* User won't be notified if a null pointer returned. */ 2233 + if (di != NULL) 2234 + queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0); 2235 + } 2236 + 2237 + /* Exposure to the sysfs interface */ 2238 + 2239 + struct ab8500_fg_sysfs_entry { 2240 + struct attribute attr; 2241 + ssize_t (*show)(struct ab8500_fg *, char *); 2242 + ssize_t (*store)(struct ab8500_fg *, const char *, size_t); 2243 + }; 2244 + 2245 + static ssize_t charge_full_show(struct ab8500_fg *di, char *buf) 2246 + { 2247 + return sprintf(buf, "%d\n", di->bat_cap.max_mah); 2248 + } 2249 + 2250 + static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf, 2251 + size_t count) 2252 + { 2253 + unsigned long charge_full; 2254 + ssize_t ret = -EINVAL; 2255 + 2256 + ret = strict_strtoul(buf, 10, &charge_full); 2257 + 2258 + dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full); 2259 + 2260 + if (!ret) { 2261 + di->bat_cap.max_mah = (int) charge_full; 2262 + ret = count; 2263 + } 2264 + return ret; 2265 + } 2266 + 2267 + static ssize_t charge_now_show(struct ab8500_fg *di, char *buf) 2268 + { 2269 + return sprintf(buf, "%d\n", di->bat_cap.prev_mah); 2270 + } 2271 + 2272 + static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf, 2273 + size_t count) 2274 + { 2275 + unsigned long charge_now; 2276 + ssize_t ret; 2277 + 2278 + ret = strict_strtoul(buf, 10, &charge_now); 2279 + 2280 + dev_dbg(di->dev, "Ret %zd charge_now %lu was %d", 2281 + ret, charge_now, di->bat_cap.prev_mah); 2282 + 2283 + if (!ret) { 2284 + di->bat_cap.user_mah = (int) charge_now; 2285 + di->flags.user_cap = true; 2286 + ret = count; 2287 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2288 + } 2289 + return ret; 2290 + } 2291 + 2292 + static struct ab8500_fg_sysfs_entry charge_full_attr = 2293 + __ATTR(charge_full, 0644, charge_full_show, charge_full_store); 2294 + 2295 + static struct ab8500_fg_sysfs_entry charge_now_attr = 2296 + __ATTR(charge_now, 0644, charge_now_show, charge_now_store); 2297 + 2298 + static ssize_t 2299 + ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf) 2300 + { 2301 + struct ab8500_fg_sysfs_entry *entry; 2302 + struct ab8500_fg *di; 2303 + 2304 + entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2305 + di = container_of(kobj, struct ab8500_fg, fg_kobject); 2306 + 2307 + if (!entry->show) 2308 + return -EIO; 2309 + 2310 + return entry->show(di, buf); 2311 + } 2312 + static ssize_t 2313 + ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf, 2314 + size_t count) 2315 + { 2316 + struct ab8500_fg_sysfs_entry *entry; 2317 + struct ab8500_fg *di; 2318 + 2319 + entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2320 + di = container_of(kobj, struct ab8500_fg, fg_kobject); 2321 + 2322 + if (!entry->store) 2323 + return -EIO; 2324 + 2325 + return entry->store(di, buf, count); 2326 + } 2327 + 2328 + static const struct sysfs_ops ab8500_fg_sysfs_ops = { 2329 + .show = ab8500_fg_show, 2330 + .store = ab8500_fg_store, 2331 + }; 2332 + 2333 + static struct attribute *ab8500_fg_attrs[] = { 2334 + &charge_full_attr.attr, 2335 + &charge_now_attr.attr, 2336 + NULL, 2337 + }; 2338 + 2339 + static struct kobj_type ab8500_fg_ktype = { 2340 + .sysfs_ops = &ab8500_fg_sysfs_ops, 2341 + .default_attrs = ab8500_fg_attrs, 2342 + }; 2343 + 2344 + /** 2345 + * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry 2346 + * @di: pointer to the struct ab8500_chargalg 2347 + * 2348 + * This function removes the entry in sysfs. 2349 + */ 2350 + static void ab8500_fg_sysfs_exit(struct ab8500_fg *di) 2351 + { 2352 + kobject_del(&di->fg_kobject); 2353 + } 2354 + 2355 + /** 2356 + * ab8500_chargalg_sysfs_init() - init of sysfs entry 2357 + * @di: pointer to the struct ab8500_chargalg 2358 + * 2359 + * This function adds an entry in sysfs. 2360 + * Returns error code in case of failure else 0(on success) 2361 + */ 2362 + static int ab8500_fg_sysfs_init(struct ab8500_fg *di) 2363 + { 2364 + int ret = 0; 2365 + 2366 + ret = kobject_init_and_add(&di->fg_kobject, 2367 + &ab8500_fg_ktype, 2368 + NULL, "battery"); 2369 + if (ret < 0) 2370 + dev_err(di->dev, "failed to create sysfs entry\n"); 2371 + 2372 + return ret; 2373 + } 2374 + /* Exposure to the sysfs interface <<END>> */ 2375 + 2376 + #if defined(CONFIG_PM) 2377 + static int ab8500_fg_resume(struct platform_device *pdev) 2378 + { 2379 + struct ab8500_fg *di = platform_get_drvdata(pdev); 2380 + 2381 + /* 2382 + * Change state if we're not charging. If we're charging we will wake 2383 + * up on the FG IRQ 2384 + */ 2385 + if (!di->flags.charging) { 2386 + ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP); 2387 + queue_work(di->fg_wq, &di->fg_work); 2388 + } 2389 + 2390 + return 0; 2391 + } 2392 + 2393 + static int ab8500_fg_suspend(struct platform_device *pdev, 2394 + pm_message_t state) 2395 + { 2396 + struct ab8500_fg *di = platform_get_drvdata(pdev); 2397 + 2398 + flush_delayed_work(&di->fg_periodic_work); 2399 + 2400 + /* 2401 + * If the FG is enabled we will disable it before going to suspend 2402 + * only if we're not charging 2403 + */ 2404 + if (di->flags.fg_enabled && !di->flags.charging) 2405 + ab8500_fg_coulomb_counter(di, false); 2406 + 2407 + return 0; 2408 + } 2409 + #else 2410 + #define ab8500_fg_suspend NULL 2411 + #define ab8500_fg_resume NULL 2412 + #endif 2413 + 2414 + static int __devexit ab8500_fg_remove(struct platform_device *pdev) 2415 + { 2416 + int ret = 0; 2417 + struct ab8500_fg *di = platform_get_drvdata(pdev); 2418 + 2419 + list_del(&di->node); 2420 + 2421 + /* Disable coulomb counter */ 2422 + ret = ab8500_fg_coulomb_counter(di, false); 2423 + if (ret) 2424 + dev_err(di->dev, "failed to disable coulomb counter\n"); 2425 + 2426 + destroy_workqueue(di->fg_wq); 2427 + ab8500_fg_sysfs_exit(di); 2428 + 2429 + flush_scheduled_work(); 2430 + power_supply_unregister(&di->fg_psy); 2431 + platform_set_drvdata(pdev, NULL); 2432 + kfree(di); 2433 + return ret; 2434 + } 2435 + 2436 + /* ab8500 fg driver interrupts and their respective isr */ 2437 + static struct ab8500_fg_interrupts ab8500_fg_irq[] = { 2438 + {"NCONV_ACCU", ab8500_fg_cc_convend_handler}, 2439 + {"BATT_OVV", ab8500_fg_batt_ovv_handler}, 2440 + {"LOW_BAT_F", ab8500_fg_lowbatf_handler}, 2441 + {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler}, 2442 + {"CCEOC", ab8500_fg_cc_data_end_handler}, 2443 + }; 2444 + 2445 + static int __devinit ab8500_fg_probe(struct platform_device *pdev) 2446 + { 2447 + int i, irq; 2448 + int ret = 0; 2449 + struct abx500_bm_plat_data *plat_data; 2450 + 2451 + struct ab8500_fg *di = 2452 + kzalloc(sizeof(struct ab8500_fg), GFP_KERNEL); 2453 + if (!di) 2454 + return -ENOMEM; 2455 + 2456 + mutex_init(&di->cc_lock); 2457 + 2458 + /* get parent data */ 2459 + di->dev = &pdev->dev; 2460 + di->parent = dev_get_drvdata(pdev->dev.parent); 2461 + di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2462 + 2463 + /* get fg specific platform data */ 2464 + plat_data = pdev->dev.platform_data; 2465 + di->pdata = plat_data->fg; 2466 + if (!di->pdata) { 2467 + dev_err(di->dev, "no fg platform data supplied\n"); 2468 + ret = -EINVAL; 2469 + goto free_device_info; 2470 + } 2471 + 2472 + /* get battery specific platform data */ 2473 + di->bat = plat_data->battery; 2474 + if (!di->bat) { 2475 + dev_err(di->dev, "no battery platform data supplied\n"); 2476 + ret = -EINVAL; 2477 + goto free_device_info; 2478 + } 2479 + 2480 + di->fg_psy.name = "ab8500_fg"; 2481 + di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY; 2482 + di->fg_psy.properties = ab8500_fg_props; 2483 + di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props); 2484 + di->fg_psy.get_property = ab8500_fg_get_property; 2485 + di->fg_psy.supplied_to = di->pdata->supplied_to; 2486 + di->fg_psy.num_supplicants = di->pdata->num_supplicants; 2487 + di->fg_psy.external_power_changed = ab8500_fg_external_power_changed; 2488 + 2489 + di->bat_cap.max_mah_design = MILLI_TO_MICRO * 2490 + di->bat->bat_type[di->bat->batt_id].charge_full_design; 2491 + 2492 + di->bat_cap.max_mah = di->bat_cap.max_mah_design; 2493 + 2494 + di->vbat_nom = di->bat->bat_type[di->bat->batt_id].nominal_voltage; 2495 + 2496 + di->init_capacity = true; 2497 + 2498 + ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 2499 + ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 2500 + 2501 + /* Create a work queue for running the FG algorithm */ 2502 + di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq"); 2503 + if (di->fg_wq == NULL) { 2504 + dev_err(di->dev, "failed to create work queue\n"); 2505 + goto free_device_info; 2506 + } 2507 + 2508 + /* Init work for running the fg algorithm instantly */ 2509 + INIT_WORK(&di->fg_work, ab8500_fg_instant_work); 2510 + 2511 + /* Init work for getting the battery accumulated current */ 2512 + INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work); 2513 + 2514 + /* Init work for reinitialising the fg algorithm */ 2515 + INIT_DELAYED_WORK_DEFERRABLE(&di->fg_reinit_work, 2516 + ab8500_fg_reinit_work); 2517 + 2518 + /* Work delayed Queue to run the state machine */ 2519 + INIT_DELAYED_WORK_DEFERRABLE(&di->fg_periodic_work, 2520 + ab8500_fg_periodic_work); 2521 + 2522 + /* Work to check low battery condition */ 2523 + INIT_DELAYED_WORK_DEFERRABLE(&di->fg_low_bat_work, 2524 + ab8500_fg_low_bat_work); 2525 + 2526 + /* Init work for HW failure check */ 2527 + INIT_DELAYED_WORK_DEFERRABLE(&di->fg_check_hw_failure_work, 2528 + ab8500_fg_check_hw_failure_work); 2529 + 2530 + /* Initialize OVV, and other registers */ 2531 + ret = ab8500_fg_init_hw_registers(di); 2532 + if (ret) { 2533 + dev_err(di->dev, "failed to initialize registers\n"); 2534 + goto free_inst_curr_wq; 2535 + } 2536 + 2537 + /* Consider battery unknown until we're informed otherwise */ 2538 + di->flags.batt_unknown = true; 2539 + di->flags.batt_id_received = false; 2540 + 2541 + /* Register FG power supply class */ 2542 + ret = power_supply_register(di->dev, &di->fg_psy); 2543 + if (ret) { 2544 + dev_err(di->dev, "failed to register FG psy\n"); 2545 + goto free_inst_curr_wq; 2546 + } 2547 + 2548 + di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer); 2549 + ab8500_fg_coulomb_counter(di, true); 2550 + 2551 + /* Initialize completion used to notify completion of inst current */ 2552 + init_completion(&di->ab8500_fg_complete); 2553 + 2554 + /* Register interrupts */ 2555 + for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) { 2556 + irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name); 2557 + ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr, 2558 + IRQF_SHARED | IRQF_NO_SUSPEND, 2559 + ab8500_fg_irq[i].name, di); 2560 + 2561 + if (ret != 0) { 2562 + dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 2563 + , ab8500_fg_irq[i].name, irq, ret); 2564 + goto free_irq; 2565 + } 2566 + dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 2567 + ab8500_fg_irq[i].name, irq, ret); 2568 + } 2569 + di->irq = platform_get_irq_byname(pdev, "CCEOC"); 2570 + disable_irq(di->irq); 2571 + 2572 + platform_set_drvdata(pdev, di); 2573 + 2574 + ret = ab8500_fg_sysfs_init(di); 2575 + if (ret) { 2576 + dev_err(di->dev, "failed to create sysfs entry\n"); 2577 + goto free_irq; 2578 + } 2579 + 2580 + /* Calibrate the fg first time */ 2581 + di->flags.calibrate = true; 2582 + di->calib_state = AB8500_FG_CALIB_INIT; 2583 + 2584 + /* Use room temp as default value until we get an update from driver. */ 2585 + di->bat_temp = 210; 2586 + 2587 + /* Run the FG algorithm */ 2588 + queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2589 + 2590 + list_add_tail(&di->node, &ab8500_fg_list); 2591 + 2592 + return ret; 2593 + 2594 + free_irq: 2595 + power_supply_unregister(&di->fg_psy); 2596 + 2597 + /* We also have to free all successfully registered irqs */ 2598 + for (i = i - 1; i >= 0; i--) { 2599 + irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name); 2600 + free_irq(irq, di); 2601 + } 2602 + free_inst_curr_wq: 2603 + destroy_workqueue(di->fg_wq); 2604 + free_device_info: 2605 + kfree(di); 2606 + 2607 + return ret; 2608 + } 2609 + 2610 + static struct platform_driver ab8500_fg_driver = { 2611 + .probe = ab8500_fg_probe, 2612 + .remove = __devexit_p(ab8500_fg_remove), 2613 + .suspend = ab8500_fg_suspend, 2614 + .resume = ab8500_fg_resume, 2615 + .driver = { 2616 + .name = "ab8500-fg", 2617 + .owner = THIS_MODULE, 2618 + }, 2619 + }; 2620 + 2621 + static int __init ab8500_fg_init(void) 2622 + { 2623 + return platform_driver_register(&ab8500_fg_driver); 2624 + } 2625 + 2626 + static void __exit ab8500_fg_exit(void) 2627 + { 2628 + platform_driver_unregister(&ab8500_fg_driver); 2629 + } 2630 + 2631 + subsys_initcall_sync(ab8500_fg_init); 2632 + module_exit(ab8500_fg_exit); 2633 + 2634 + MODULE_LICENSE("GPL v2"); 2635 + MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); 2636 + MODULE_ALIAS("platform:ab8500-fg"); 2637 + MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");
+1921
drivers/power/abx500_chargalg.c
··· 1 + /* 2 + * Copyright (C) ST-Ericsson SA 2012 3 + * 4 + * Charging algorithm driver for abx500 variants 5 + * 6 + * License Terms: GNU General Public License v2 7 + * Authors: 8 + * Johan Palsson <johan.palsson@stericsson.com> 9 + * Karl Komierowski <karl.komierowski@stericsson.com> 10 + * Arun R Murthy <arun.murthy@stericsson.com> 11 + */ 12 + 13 + #include <linux/init.h> 14 + #include <linux/module.h> 15 + #include <linux/device.h> 16 + #include <linux/interrupt.h> 17 + #include <linux/delay.h> 18 + #include <linux/slab.h> 19 + #include <linux/platform_device.h> 20 + #include <linux/power_supply.h> 21 + #include <linux/completion.h> 22 + #include <linux/workqueue.h> 23 + #include <linux/kobject.h> 24 + #include <linux/mfd/abx500.h> 25 + #include <linux/mfd/abx500/ux500_chargalg.h> 26 + #include <linux/mfd/abx500/ab8500-bm.h> 27 + 28 + /* Watchdog kick interval */ 29 + #define CHG_WD_INTERVAL (6 * HZ) 30 + 31 + /* End-of-charge criteria counter */ 32 + #define EOC_COND_CNT 10 33 + 34 + /* Recharge criteria counter */ 35 + #define RCH_COND_CNT 3 36 + 37 + #define to_abx500_chargalg_device_info(x) container_of((x), \ 38 + struct abx500_chargalg, chargalg_psy); 39 + 40 + enum abx500_chargers { 41 + NO_CHG, 42 + AC_CHG, 43 + USB_CHG, 44 + }; 45 + 46 + struct abx500_chargalg_charger_info { 47 + enum abx500_chargers conn_chg; 48 + enum abx500_chargers prev_conn_chg; 49 + enum abx500_chargers online_chg; 50 + enum abx500_chargers prev_online_chg; 51 + enum abx500_chargers charger_type; 52 + bool usb_chg_ok; 53 + bool ac_chg_ok; 54 + int usb_volt; 55 + int usb_curr; 56 + int ac_volt; 57 + int ac_curr; 58 + int usb_vset; 59 + int usb_iset; 60 + int ac_vset; 61 + int ac_iset; 62 + }; 63 + 64 + struct abx500_chargalg_suspension_status { 65 + bool suspended_change; 66 + bool ac_suspended; 67 + bool usb_suspended; 68 + }; 69 + 70 + struct abx500_chargalg_battery_data { 71 + int temp; 72 + int volt; 73 + int avg_curr; 74 + int inst_curr; 75 + int percent; 76 + }; 77 + 78 + enum abx500_chargalg_states { 79 + STATE_HANDHELD_INIT, 80 + STATE_HANDHELD, 81 + STATE_CHG_NOT_OK_INIT, 82 + STATE_CHG_NOT_OK, 83 + STATE_HW_TEMP_PROTECT_INIT, 84 + STATE_HW_TEMP_PROTECT, 85 + STATE_NORMAL_INIT, 86 + STATE_NORMAL, 87 + STATE_WAIT_FOR_RECHARGE_INIT, 88 + STATE_WAIT_FOR_RECHARGE, 89 + STATE_MAINTENANCE_A_INIT, 90 + STATE_MAINTENANCE_A, 91 + STATE_MAINTENANCE_B_INIT, 92 + STATE_MAINTENANCE_B, 93 + STATE_TEMP_UNDEROVER_INIT, 94 + STATE_TEMP_UNDEROVER, 95 + STATE_TEMP_LOWHIGH_INIT, 96 + STATE_TEMP_LOWHIGH, 97 + STATE_SUSPENDED_INIT, 98 + STATE_SUSPENDED, 99 + STATE_OVV_PROTECT_INIT, 100 + STATE_OVV_PROTECT, 101 + STATE_SAFETY_TIMER_EXPIRED_INIT, 102 + STATE_SAFETY_TIMER_EXPIRED, 103 + STATE_BATT_REMOVED_INIT, 104 + STATE_BATT_REMOVED, 105 + STATE_WD_EXPIRED_INIT, 106 + STATE_WD_EXPIRED, 107 + }; 108 + 109 + static const char *states[] = { 110 + "HANDHELD_INIT", 111 + "HANDHELD", 112 + "CHG_NOT_OK_INIT", 113 + "CHG_NOT_OK", 114 + "HW_TEMP_PROTECT_INIT", 115 + "HW_TEMP_PROTECT", 116 + "NORMAL_INIT", 117 + "NORMAL", 118 + "WAIT_FOR_RECHARGE_INIT", 119 + "WAIT_FOR_RECHARGE", 120 + "MAINTENANCE_A_INIT", 121 + "MAINTENANCE_A", 122 + "MAINTENANCE_B_INIT", 123 + "MAINTENANCE_B", 124 + "TEMP_UNDEROVER_INIT", 125 + "TEMP_UNDEROVER", 126 + "TEMP_LOWHIGH_INIT", 127 + "TEMP_LOWHIGH", 128 + "SUSPENDED_INIT", 129 + "SUSPENDED", 130 + "OVV_PROTECT_INIT", 131 + "OVV_PROTECT", 132 + "SAFETY_TIMER_EXPIRED_INIT", 133 + "SAFETY_TIMER_EXPIRED", 134 + "BATT_REMOVED_INIT", 135 + "BATT_REMOVED", 136 + "WD_EXPIRED_INIT", 137 + "WD_EXPIRED", 138 + }; 139 + 140 + struct abx500_chargalg_events { 141 + bool batt_unknown; 142 + bool mainextchnotok; 143 + bool batt_ovv; 144 + bool batt_rem; 145 + bool btemp_underover; 146 + bool btemp_lowhigh; 147 + bool main_thermal_prot; 148 + bool usb_thermal_prot; 149 + bool main_ovv; 150 + bool vbus_ovv; 151 + bool usbchargernotok; 152 + bool safety_timer_expired; 153 + bool maintenance_timer_expired; 154 + bool ac_wd_expired; 155 + bool usb_wd_expired; 156 + bool ac_cv_active; 157 + bool usb_cv_active; 158 + bool vbus_collapsed; 159 + }; 160 + 161 + /** 162 + * struct abx500_charge_curr_maximization - Charger maximization parameters 163 + * @original_iset: the non optimized/maximised charger current 164 + * @current_iset: the charging current used at this moment 165 + * @test_delta_i: the delta between the current we want to charge and the 166 + current that is really going into the battery 167 + * @condition_cnt: number of iterations needed before a new charger current 168 + is set 169 + * @max_current: maximum charger current 170 + * @wait_cnt: to avoid too fast current step down in case of charger 171 + * voltage collapse, we insert this delay between step 172 + * down 173 + * @level: tells in how many steps the charging current has been 174 + increased 175 + */ 176 + struct abx500_charge_curr_maximization { 177 + int original_iset; 178 + int current_iset; 179 + int test_delta_i; 180 + int condition_cnt; 181 + int max_current; 182 + int wait_cnt; 183 + u8 level; 184 + }; 185 + 186 + enum maxim_ret { 187 + MAXIM_RET_NOACTION, 188 + MAXIM_RET_CHANGE, 189 + MAXIM_RET_IBAT_TOO_HIGH, 190 + }; 191 + 192 + /** 193 + * struct abx500_chargalg - abx500 Charging algorithm device information 194 + * @dev: pointer to the structure device 195 + * @charge_status: battery operating status 196 + * @eoc_cnt: counter used to determine end-of_charge 197 + * @rch_cnt: counter used to determine start of recharge 198 + * @maintenance_chg: indicate if maintenance charge is active 199 + * @t_hyst_norm temperature hysteresis when the temperature has been 200 + * over or under normal limits 201 + * @t_hyst_lowhigh temperature hysteresis when the temperature has been 202 + * over or under the high or low limits 203 + * @charge_state: current state of the charging algorithm 204 + * @ccm charging current maximization parameters 205 + * @chg_info: information about connected charger types 206 + * @batt_data: data of the battery 207 + * @susp_status: current charger suspension status 208 + * @pdata: pointer to the abx500_chargalg platform data 209 + * @bat: pointer to the abx500_bm platform data 210 + * @chargalg_psy: structure that holds the battery properties exposed by 211 + * the charging algorithm 212 + * @events: structure for information about events triggered 213 + * @chargalg_wq: work queue for running the charging algorithm 214 + * @chargalg_periodic_work: work to run the charging algorithm periodically 215 + * @chargalg_wd_work: work to kick the charger watchdog periodically 216 + * @chargalg_work: work to run the charging algorithm instantly 217 + * @safety_timer: charging safety timer 218 + * @maintenance_timer: maintenance charging timer 219 + * @chargalg_kobject: structure of type kobject 220 + */ 221 + struct abx500_chargalg { 222 + struct device *dev; 223 + int charge_status; 224 + int eoc_cnt; 225 + int rch_cnt; 226 + bool maintenance_chg; 227 + int t_hyst_norm; 228 + int t_hyst_lowhigh; 229 + enum abx500_chargalg_states charge_state; 230 + struct abx500_charge_curr_maximization ccm; 231 + struct abx500_chargalg_charger_info chg_info; 232 + struct abx500_chargalg_battery_data batt_data; 233 + struct abx500_chargalg_suspension_status susp_status; 234 + struct abx500_chargalg_platform_data *pdata; 235 + struct abx500_bm_data *bat; 236 + struct power_supply chargalg_psy; 237 + struct ux500_charger *ac_chg; 238 + struct ux500_charger *usb_chg; 239 + struct abx500_chargalg_events events; 240 + struct workqueue_struct *chargalg_wq; 241 + struct delayed_work chargalg_periodic_work; 242 + struct delayed_work chargalg_wd_work; 243 + struct work_struct chargalg_work; 244 + struct timer_list safety_timer; 245 + struct timer_list maintenance_timer; 246 + struct kobject chargalg_kobject; 247 + }; 248 + 249 + /* Main battery properties */ 250 + static enum power_supply_property abx500_chargalg_props[] = { 251 + POWER_SUPPLY_PROP_STATUS, 252 + POWER_SUPPLY_PROP_HEALTH, 253 + }; 254 + 255 + /** 256 + * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer 257 + * @data: pointer to the abx500_chargalg structure 258 + * 259 + * This function gets called when the safety timer for the charger 260 + * expires 261 + */ 262 + static void abx500_chargalg_safety_timer_expired(unsigned long data) 263 + { 264 + struct abx500_chargalg *di = (struct abx500_chargalg *) data; 265 + dev_err(di->dev, "Safety timer expired\n"); 266 + di->events.safety_timer_expired = true; 267 + 268 + /* Trigger execution of the algorithm instantly */ 269 + queue_work(di->chargalg_wq, &di->chargalg_work); 270 + } 271 + 272 + /** 273 + * abx500_chargalg_maintenance_timer_expired() - Expiration of 274 + * the maintenance timer 275 + * @i: pointer to the abx500_chargalg structure 276 + * 277 + * This function gets called when the maintenence timer 278 + * expires 279 + */ 280 + static void abx500_chargalg_maintenance_timer_expired(unsigned long data) 281 + { 282 + 283 + struct abx500_chargalg *di = (struct abx500_chargalg *) data; 284 + dev_dbg(di->dev, "Maintenance timer expired\n"); 285 + di->events.maintenance_timer_expired = true; 286 + 287 + /* Trigger execution of the algorithm instantly */ 288 + queue_work(di->chargalg_wq, &di->chargalg_work); 289 + } 290 + 291 + /** 292 + * abx500_chargalg_state_to() - Change charge state 293 + * @di: pointer to the abx500_chargalg structure 294 + * 295 + * This function gets called when a charge state change should occur 296 + */ 297 + static void abx500_chargalg_state_to(struct abx500_chargalg *di, 298 + enum abx500_chargalg_states state) 299 + { 300 + dev_dbg(di->dev, 301 + "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n", 302 + di->charge_state == state ? "NO" : "YES", 303 + di->charge_state, 304 + states[di->charge_state], 305 + state, 306 + states[state]); 307 + 308 + di->charge_state = state; 309 + } 310 + 311 + /** 312 + * abx500_chargalg_check_charger_connection() - Check charger connection change 313 + * @di: pointer to the abx500_chargalg structure 314 + * 315 + * This function will check if there is a change in the charger connection 316 + * and change charge state accordingly. AC has precedence over USB. 317 + */ 318 + static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di) 319 + { 320 + if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg || 321 + di->susp_status.suspended_change) { 322 + /* 323 + * Charger state changed or suspension 324 + * has changed since last update 325 + */ 326 + if ((di->chg_info.conn_chg & AC_CHG) && 327 + !di->susp_status.ac_suspended) { 328 + dev_dbg(di->dev, "Charging source is AC\n"); 329 + if (di->chg_info.charger_type != AC_CHG) { 330 + di->chg_info.charger_type = AC_CHG; 331 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 332 + } 333 + } else if ((di->chg_info.conn_chg & USB_CHG) && 334 + !di->susp_status.usb_suspended) { 335 + dev_dbg(di->dev, "Charging source is USB\n"); 336 + di->chg_info.charger_type = USB_CHG; 337 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 338 + } else if (di->chg_info.conn_chg && 339 + (di->susp_status.ac_suspended || 340 + di->susp_status.usb_suspended)) { 341 + dev_dbg(di->dev, "Charging is suspended\n"); 342 + di->chg_info.charger_type = NO_CHG; 343 + abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT); 344 + } else { 345 + dev_dbg(di->dev, "Charging source is OFF\n"); 346 + di->chg_info.charger_type = NO_CHG; 347 + abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); 348 + } 349 + di->chg_info.prev_conn_chg = di->chg_info.conn_chg; 350 + di->susp_status.suspended_change = false; 351 + } 352 + return di->chg_info.conn_chg; 353 + } 354 + 355 + /** 356 + * abx500_chargalg_start_safety_timer() - Start charging safety timer 357 + * @di: pointer to the abx500_chargalg structure 358 + * 359 + * The safety timer is used to avoid overcharging of old or bad batteries. 360 + * There are different timers for AC and USB 361 + */ 362 + static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di) 363 + { 364 + unsigned long timer_expiration = 0; 365 + 366 + switch (di->chg_info.charger_type) { 367 + case AC_CHG: 368 + timer_expiration = 369 + round_jiffies(jiffies + 370 + (di->bat->main_safety_tmr_h * 3600 * HZ)); 371 + break; 372 + 373 + case USB_CHG: 374 + timer_expiration = 375 + round_jiffies(jiffies + 376 + (di->bat->usb_safety_tmr_h * 3600 * HZ)); 377 + break; 378 + 379 + default: 380 + dev_err(di->dev, "Unknown charger to charge from\n"); 381 + break; 382 + } 383 + 384 + di->events.safety_timer_expired = false; 385 + di->safety_timer.expires = timer_expiration; 386 + if (!timer_pending(&di->safety_timer)) 387 + add_timer(&di->safety_timer); 388 + else 389 + mod_timer(&di->safety_timer, timer_expiration); 390 + } 391 + 392 + /** 393 + * abx500_chargalg_stop_safety_timer() - Stop charging safety timer 394 + * @di: pointer to the abx500_chargalg structure 395 + * 396 + * The safety timer is stopped whenever the NORMAL state is exited 397 + */ 398 + static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di) 399 + { 400 + di->events.safety_timer_expired = false; 401 + del_timer(&di->safety_timer); 402 + } 403 + 404 + /** 405 + * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer 406 + * @di: pointer to the abx500_chargalg structure 407 + * @duration: duration of ther maintenance timer in hours 408 + * 409 + * The maintenance timer is used to maintain the charge in the battery once 410 + * the battery is considered full. These timers are chosen to match the 411 + * discharge curve of the battery 412 + */ 413 + static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di, 414 + int duration) 415 + { 416 + unsigned long timer_expiration; 417 + 418 + /* Convert from hours to jiffies */ 419 + timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ)); 420 + 421 + di->events.maintenance_timer_expired = false; 422 + di->maintenance_timer.expires = timer_expiration; 423 + if (!timer_pending(&di->maintenance_timer)) 424 + add_timer(&di->maintenance_timer); 425 + else 426 + mod_timer(&di->maintenance_timer, timer_expiration); 427 + } 428 + 429 + /** 430 + * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer 431 + * @di: pointer to the abx500_chargalg structure 432 + * 433 + * The maintenance timer is stopped whenever maintenance ends or when another 434 + * state is entered 435 + */ 436 + static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di) 437 + { 438 + di->events.maintenance_timer_expired = false; 439 + del_timer(&di->maintenance_timer); 440 + } 441 + 442 + /** 443 + * abx500_chargalg_kick_watchdog() - Kick charger watchdog 444 + * @di: pointer to the abx500_chargalg structure 445 + * 446 + * The charger watchdog have to be kicked periodically whenever the charger is 447 + * on, else the ABB will reset the system 448 + */ 449 + static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di) 450 + { 451 + /* Check if charger exists and kick watchdog if charging */ 452 + if (di->ac_chg && di->ac_chg->ops.kick_wd && 453 + di->chg_info.online_chg & AC_CHG) 454 + return di->ac_chg->ops.kick_wd(di->ac_chg); 455 + else if (di->usb_chg && di->usb_chg->ops.kick_wd && 456 + di->chg_info.online_chg & USB_CHG) 457 + return di->usb_chg->ops.kick_wd(di->usb_chg); 458 + 459 + return -ENXIO; 460 + } 461 + 462 + /** 463 + * abx500_chargalg_ac_en() - Turn on/off the AC charger 464 + * @di: pointer to the abx500_chargalg structure 465 + * @enable: charger on/off 466 + * @vset: requested charger output voltage 467 + * @iset: requested charger output current 468 + * 469 + * The AC charger will be turned on/off with the requested charge voltage and 470 + * current 471 + */ 472 + static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable, 473 + int vset, int iset) 474 + { 475 + if (!di->ac_chg || !di->ac_chg->ops.enable) 476 + return -ENXIO; 477 + 478 + /* Select maximum of what both the charger and the battery supports */ 479 + if (di->ac_chg->max_out_volt) 480 + vset = min(vset, di->ac_chg->max_out_volt); 481 + if (di->ac_chg->max_out_curr) 482 + iset = min(iset, di->ac_chg->max_out_curr); 483 + 484 + di->chg_info.ac_iset = iset; 485 + di->chg_info.ac_vset = vset; 486 + 487 + return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset); 488 + } 489 + 490 + /** 491 + * abx500_chargalg_usb_en() - Turn on/off the USB charger 492 + * @di: pointer to the abx500_chargalg structure 493 + * @enable: charger on/off 494 + * @vset: requested charger output voltage 495 + * @iset: requested charger output current 496 + * 497 + * The USB charger will be turned on/off with the requested charge voltage and 498 + * current 499 + */ 500 + static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable, 501 + int vset, int iset) 502 + { 503 + if (!di->usb_chg || !di->usb_chg->ops.enable) 504 + return -ENXIO; 505 + 506 + /* Select maximum of what both the charger and the battery supports */ 507 + if (di->usb_chg->max_out_volt) 508 + vset = min(vset, di->usb_chg->max_out_volt); 509 + if (di->usb_chg->max_out_curr) 510 + iset = min(iset, di->usb_chg->max_out_curr); 511 + 512 + di->chg_info.usb_iset = iset; 513 + di->chg_info.usb_vset = vset; 514 + 515 + return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset); 516 + } 517 + 518 + /** 519 + * abx500_chargalg_update_chg_curr() - Update charger current 520 + * @di: pointer to the abx500_chargalg structure 521 + * @iset: requested charger output current 522 + * 523 + * The charger output current will be updated for the charger 524 + * that is currently in use 525 + */ 526 + static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di, 527 + int iset) 528 + { 529 + /* Check if charger exists and update current if charging */ 530 + if (di->ac_chg && di->ac_chg->ops.update_curr && 531 + di->chg_info.charger_type & AC_CHG) { 532 + /* 533 + * Select maximum of what both the charger 534 + * and the battery supports 535 + */ 536 + if (di->ac_chg->max_out_curr) 537 + iset = min(iset, di->ac_chg->max_out_curr); 538 + 539 + di->chg_info.ac_iset = iset; 540 + 541 + return di->ac_chg->ops.update_curr(di->ac_chg, iset); 542 + } else if (di->usb_chg && di->usb_chg->ops.update_curr && 543 + di->chg_info.charger_type & USB_CHG) { 544 + /* 545 + * Select maximum of what both the charger 546 + * and the battery supports 547 + */ 548 + if (di->usb_chg->max_out_curr) 549 + iset = min(iset, di->usb_chg->max_out_curr); 550 + 551 + di->chg_info.usb_iset = iset; 552 + 553 + return di->usb_chg->ops.update_curr(di->usb_chg, iset); 554 + } 555 + 556 + return -ENXIO; 557 + } 558 + 559 + /** 560 + * abx500_chargalg_stop_charging() - Stop charging 561 + * @di: pointer to the abx500_chargalg structure 562 + * 563 + * This function is called from any state where charging should be stopped. 564 + * All charging is disabled and all status parameters and timers are changed 565 + * accordingly 566 + */ 567 + static void abx500_chargalg_stop_charging(struct abx500_chargalg *di) 568 + { 569 + abx500_chargalg_ac_en(di, false, 0, 0); 570 + abx500_chargalg_usb_en(di, false, 0, 0); 571 + abx500_chargalg_stop_safety_timer(di); 572 + abx500_chargalg_stop_maintenance_timer(di); 573 + di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 574 + di->maintenance_chg = false; 575 + cancel_delayed_work(&di->chargalg_wd_work); 576 + power_supply_changed(&di->chargalg_psy); 577 + } 578 + 579 + /** 580 + * abx500_chargalg_hold_charging() - Pauses charging 581 + * @di: pointer to the abx500_chargalg structure 582 + * 583 + * This function is called in the case where maintenance charging has been 584 + * disabled and instead a battery voltage mode is entered to check when the 585 + * battery voltage has reached a certain recharge voltage 586 + */ 587 + static void abx500_chargalg_hold_charging(struct abx500_chargalg *di) 588 + { 589 + abx500_chargalg_ac_en(di, false, 0, 0); 590 + abx500_chargalg_usb_en(di, false, 0, 0); 591 + abx500_chargalg_stop_safety_timer(di); 592 + abx500_chargalg_stop_maintenance_timer(di); 593 + di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 594 + di->maintenance_chg = false; 595 + cancel_delayed_work(&di->chargalg_wd_work); 596 + power_supply_changed(&di->chargalg_psy); 597 + } 598 + 599 + /** 600 + * abx500_chargalg_start_charging() - Start the charger 601 + * @di: pointer to the abx500_chargalg structure 602 + * @vset: requested charger output voltage 603 + * @iset: requested charger output current 604 + * 605 + * A charger will be enabled depending on the requested charger type that was 606 + * detected previously. 607 + */ 608 + static void abx500_chargalg_start_charging(struct abx500_chargalg *di, 609 + int vset, int iset) 610 + { 611 + switch (di->chg_info.charger_type) { 612 + case AC_CHG: 613 + dev_dbg(di->dev, 614 + "AC parameters: Vset %d, Ich %d\n", vset, iset); 615 + abx500_chargalg_usb_en(di, false, 0, 0); 616 + abx500_chargalg_ac_en(di, true, vset, iset); 617 + break; 618 + 619 + case USB_CHG: 620 + dev_dbg(di->dev, 621 + "USB parameters: Vset %d, Ich %d\n", vset, iset); 622 + abx500_chargalg_ac_en(di, false, 0, 0); 623 + abx500_chargalg_usb_en(di, true, vset, iset); 624 + break; 625 + 626 + default: 627 + dev_err(di->dev, "Unknown charger to charge from\n"); 628 + break; 629 + } 630 + } 631 + 632 + /** 633 + * abx500_chargalg_check_temp() - Check battery temperature ranges 634 + * @di: pointer to the abx500_chargalg structure 635 + * 636 + * The battery temperature is checked against the predefined limits and the 637 + * charge state is changed accordingly 638 + */ 639 + static void abx500_chargalg_check_temp(struct abx500_chargalg *di) 640 + { 641 + if (di->batt_data.temp > (di->bat->temp_low + di->t_hyst_norm) && 642 + di->batt_data.temp < (di->bat->temp_high - di->t_hyst_norm)) { 643 + /* Temp OK! */ 644 + di->events.btemp_underover = false; 645 + di->events.btemp_lowhigh = false; 646 + di->t_hyst_norm = 0; 647 + di->t_hyst_lowhigh = 0; 648 + } else { 649 + if (((di->batt_data.temp >= di->bat->temp_high) && 650 + (di->batt_data.temp < 651 + (di->bat->temp_over - di->t_hyst_lowhigh))) || 652 + ((di->batt_data.temp > 653 + (di->bat->temp_under + di->t_hyst_lowhigh)) && 654 + (di->batt_data.temp <= di->bat->temp_low))) { 655 + /* TEMP minor!!!!! */ 656 + di->events.btemp_underover = false; 657 + di->events.btemp_lowhigh = true; 658 + di->t_hyst_norm = di->bat->temp_hysteresis; 659 + di->t_hyst_lowhigh = 0; 660 + } else if (di->batt_data.temp <= di->bat->temp_under || 661 + di->batt_data.temp >= di->bat->temp_over) { 662 + /* TEMP major!!!!! */ 663 + di->events.btemp_underover = true; 664 + di->events.btemp_lowhigh = false; 665 + di->t_hyst_norm = 0; 666 + di->t_hyst_lowhigh = di->bat->temp_hysteresis; 667 + } else { 668 + /* Within hysteresis */ 669 + dev_dbg(di->dev, "Within hysteresis limit temp: %d " 670 + "hyst_lowhigh %d, hyst normal %d\n", 671 + di->batt_data.temp, di->t_hyst_lowhigh, 672 + di->t_hyst_norm); 673 + } 674 + } 675 + } 676 + 677 + /** 678 + * abx500_chargalg_check_charger_voltage() - Check charger voltage 679 + * @di: pointer to the abx500_chargalg structure 680 + * 681 + * Charger voltage is checked against maximum limit 682 + */ 683 + static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di) 684 + { 685 + if (di->chg_info.usb_volt > di->bat->chg_params->usb_volt_max) 686 + di->chg_info.usb_chg_ok = false; 687 + else 688 + di->chg_info.usb_chg_ok = true; 689 + 690 + if (di->chg_info.ac_volt > di->bat->chg_params->ac_volt_max) 691 + di->chg_info.ac_chg_ok = false; 692 + else 693 + di->chg_info.ac_chg_ok = true; 694 + 695 + } 696 + 697 + /** 698 + * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled 699 + * @di: pointer to the abx500_chargalg structure 700 + * 701 + * End-of-charge criteria is fulfilled when the battery voltage is above a 702 + * certain limit and the battery current is below a certain limit for a 703 + * predefined number of consecutive seconds. If true, the battery is full 704 + */ 705 + static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di) 706 + { 707 + if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING && 708 + di->charge_state == STATE_NORMAL && 709 + !di->maintenance_chg && (di->batt_data.volt >= 710 + di->bat->bat_type[di->bat->batt_id].termination_vol || 711 + di->events.usb_cv_active || di->events.ac_cv_active) && 712 + di->batt_data.avg_curr < 713 + di->bat->bat_type[di->bat->batt_id].termination_curr && 714 + di->batt_data.avg_curr > 0) { 715 + if (++di->eoc_cnt >= EOC_COND_CNT) { 716 + di->eoc_cnt = 0; 717 + di->charge_status = POWER_SUPPLY_STATUS_FULL; 718 + di->maintenance_chg = true; 719 + dev_dbg(di->dev, "EOC reached!\n"); 720 + power_supply_changed(&di->chargalg_psy); 721 + } else { 722 + dev_dbg(di->dev, 723 + " EOC limit reached for the %d" 724 + " time, out of %d before EOC\n", 725 + di->eoc_cnt, 726 + EOC_COND_CNT); 727 + } 728 + } else { 729 + di->eoc_cnt = 0; 730 + } 731 + } 732 + 733 + static void init_maxim_chg_curr(struct abx500_chargalg *di) 734 + { 735 + di->ccm.original_iset = 736 + di->bat->bat_type[di->bat->batt_id].normal_cur_lvl; 737 + di->ccm.current_iset = 738 + di->bat->bat_type[di->bat->batt_id].normal_cur_lvl; 739 + di->ccm.test_delta_i = di->bat->maxi->charger_curr_step; 740 + di->ccm.max_current = di->bat->maxi->chg_curr; 741 + di->ccm.condition_cnt = di->bat->maxi->wait_cycles; 742 + di->ccm.level = 0; 743 + } 744 + 745 + /** 746 + * abx500_chargalg_chg_curr_maxim - increases the charger current to 747 + * compensate for the system load 748 + * @di pointer to the abx500_chargalg structure 749 + * 750 + * This maximization function is used to raise the charger current to get the 751 + * battery current as close to the optimal value as possible. The battery 752 + * current during charging is affected by the system load 753 + */ 754 + static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di) 755 + { 756 + int delta_i; 757 + 758 + if (!di->bat->maxi->ena_maxi) 759 + return MAXIM_RET_NOACTION; 760 + 761 + delta_i = di->ccm.original_iset - di->batt_data.inst_curr; 762 + 763 + if (di->events.vbus_collapsed) { 764 + dev_dbg(di->dev, "Charger voltage has collapsed %d\n", 765 + di->ccm.wait_cnt); 766 + if (di->ccm.wait_cnt == 0) { 767 + dev_dbg(di->dev, "lowering current\n"); 768 + di->ccm.wait_cnt++; 769 + di->ccm.condition_cnt = di->bat->maxi->wait_cycles; 770 + di->ccm.max_current = 771 + di->ccm.current_iset - di->ccm.test_delta_i; 772 + di->ccm.current_iset = di->ccm.max_current; 773 + di->ccm.level--; 774 + return MAXIM_RET_CHANGE; 775 + } else { 776 + dev_dbg(di->dev, "waiting\n"); 777 + /* Let's go in here twice before lowering curr again */ 778 + di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3; 779 + return MAXIM_RET_NOACTION; 780 + } 781 + } 782 + 783 + di->ccm.wait_cnt = 0; 784 + 785 + if ((di->batt_data.inst_curr > di->ccm.original_iset)) { 786 + dev_dbg(di->dev, " Maximization Ibat (%dmA) too high" 787 + " (limit %dmA) (current iset: %dmA)!\n", 788 + di->batt_data.inst_curr, di->ccm.original_iset, 789 + di->ccm.current_iset); 790 + 791 + if (di->ccm.current_iset == di->ccm.original_iset) 792 + return MAXIM_RET_NOACTION; 793 + 794 + di->ccm.condition_cnt = di->bat->maxi->wait_cycles; 795 + di->ccm.current_iset = di->ccm.original_iset; 796 + di->ccm.level = 0; 797 + 798 + return MAXIM_RET_IBAT_TOO_HIGH; 799 + } 800 + 801 + if (delta_i > di->ccm.test_delta_i && 802 + (di->ccm.current_iset + di->ccm.test_delta_i) < 803 + di->ccm.max_current) { 804 + if (di->ccm.condition_cnt-- == 0) { 805 + /* Increse the iset with cco.test_delta_i */ 806 + di->ccm.condition_cnt = di->bat->maxi->wait_cycles; 807 + di->ccm.current_iset += di->ccm.test_delta_i; 808 + di->ccm.level++; 809 + dev_dbg(di->dev, " Maximization needed, increase" 810 + " with %d mA to %dmA (Optimal ibat: %d)" 811 + " Level %d\n", 812 + di->ccm.test_delta_i, 813 + di->ccm.current_iset, 814 + di->ccm.original_iset, 815 + di->ccm.level); 816 + return MAXIM_RET_CHANGE; 817 + } else { 818 + return MAXIM_RET_NOACTION; 819 + } 820 + } else { 821 + di->ccm.condition_cnt = di->bat->maxi->wait_cycles; 822 + return MAXIM_RET_NOACTION; 823 + } 824 + } 825 + 826 + static void handle_maxim_chg_curr(struct abx500_chargalg *di) 827 + { 828 + enum maxim_ret ret; 829 + int result; 830 + 831 + ret = abx500_chargalg_chg_curr_maxim(di); 832 + switch (ret) { 833 + case MAXIM_RET_CHANGE: 834 + result = abx500_chargalg_update_chg_curr(di, 835 + di->ccm.current_iset); 836 + if (result) 837 + dev_err(di->dev, "failed to set chg curr\n"); 838 + break; 839 + case MAXIM_RET_IBAT_TOO_HIGH: 840 + result = abx500_chargalg_update_chg_curr(di, 841 + di->bat->bat_type[di->bat->batt_id].normal_cur_lvl); 842 + if (result) 843 + dev_err(di->dev, "failed to set chg curr\n"); 844 + break; 845 + 846 + case MAXIM_RET_NOACTION: 847 + default: 848 + /* Do nothing..*/ 849 + break; 850 + } 851 + } 852 + 853 + static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data) 854 + { 855 + struct power_supply *psy; 856 + struct power_supply *ext; 857 + struct abx500_chargalg *di; 858 + union power_supply_propval ret; 859 + int i, j; 860 + bool psy_found = false; 861 + 862 + psy = (struct power_supply *)data; 863 + ext = dev_get_drvdata(dev); 864 + di = to_abx500_chargalg_device_info(psy); 865 + /* For all psy where the driver name appears in any supplied_to */ 866 + for (i = 0; i < ext->num_supplicants; i++) { 867 + if (!strcmp(ext->supplied_to[i], psy->name)) 868 + psy_found = true; 869 + } 870 + if (!psy_found) 871 + return 0; 872 + 873 + /* Go through all properties for the psy */ 874 + for (j = 0; j < ext->num_properties; j++) { 875 + enum power_supply_property prop; 876 + prop = ext->properties[j]; 877 + 878 + /* Initialize chargers if not already done */ 879 + if (!di->ac_chg && 880 + ext->type == POWER_SUPPLY_TYPE_MAINS) 881 + di->ac_chg = psy_to_ux500_charger(ext); 882 + else if (!di->usb_chg && 883 + ext->type == POWER_SUPPLY_TYPE_USB) 884 + di->usb_chg = psy_to_ux500_charger(ext); 885 + 886 + if (ext->get_property(ext, prop, &ret)) 887 + continue; 888 + switch (prop) { 889 + case POWER_SUPPLY_PROP_PRESENT: 890 + switch (ext->type) { 891 + case POWER_SUPPLY_TYPE_BATTERY: 892 + /* Battery present */ 893 + if (ret.intval) 894 + di->events.batt_rem = false; 895 + /* Battery removed */ 896 + else 897 + di->events.batt_rem = true; 898 + break; 899 + case POWER_SUPPLY_TYPE_MAINS: 900 + /* AC disconnected */ 901 + if (!ret.intval && 902 + (di->chg_info.conn_chg & AC_CHG)) { 903 + di->chg_info.prev_conn_chg = 904 + di->chg_info.conn_chg; 905 + di->chg_info.conn_chg &= ~AC_CHG; 906 + } 907 + /* AC connected */ 908 + else if (ret.intval && 909 + !(di->chg_info.conn_chg & AC_CHG)) { 910 + di->chg_info.prev_conn_chg = 911 + di->chg_info.conn_chg; 912 + di->chg_info.conn_chg |= AC_CHG; 913 + } 914 + break; 915 + case POWER_SUPPLY_TYPE_USB: 916 + /* USB disconnected */ 917 + if (!ret.intval && 918 + (di->chg_info.conn_chg & USB_CHG)) { 919 + di->chg_info.prev_conn_chg = 920 + di->chg_info.conn_chg; 921 + di->chg_info.conn_chg &= ~USB_CHG; 922 + } 923 + /* USB connected */ 924 + else if (ret.intval && 925 + !(di->chg_info.conn_chg & USB_CHG)) { 926 + di->chg_info.prev_conn_chg = 927 + di->chg_info.conn_chg; 928 + di->chg_info.conn_chg |= USB_CHG; 929 + } 930 + break; 931 + default: 932 + break; 933 + } 934 + break; 935 + 936 + case POWER_SUPPLY_PROP_ONLINE: 937 + switch (ext->type) { 938 + case POWER_SUPPLY_TYPE_BATTERY: 939 + break; 940 + case POWER_SUPPLY_TYPE_MAINS: 941 + /* AC offline */ 942 + if (!ret.intval && 943 + (di->chg_info.online_chg & AC_CHG)) { 944 + di->chg_info.prev_online_chg = 945 + di->chg_info.online_chg; 946 + di->chg_info.online_chg &= ~AC_CHG; 947 + } 948 + /* AC online */ 949 + else if (ret.intval && 950 + !(di->chg_info.online_chg & AC_CHG)) { 951 + di->chg_info.prev_online_chg = 952 + di->chg_info.online_chg; 953 + di->chg_info.online_chg |= AC_CHG; 954 + queue_delayed_work(di->chargalg_wq, 955 + &di->chargalg_wd_work, 0); 956 + } 957 + break; 958 + case POWER_SUPPLY_TYPE_USB: 959 + /* USB offline */ 960 + if (!ret.intval && 961 + (di->chg_info.online_chg & USB_CHG)) { 962 + di->chg_info.prev_online_chg = 963 + di->chg_info.online_chg; 964 + di->chg_info.online_chg &= ~USB_CHG; 965 + } 966 + /* USB online */ 967 + else if (ret.intval && 968 + !(di->chg_info.online_chg & USB_CHG)) { 969 + di->chg_info.prev_online_chg = 970 + di->chg_info.online_chg; 971 + di->chg_info.online_chg |= USB_CHG; 972 + queue_delayed_work(di->chargalg_wq, 973 + &di->chargalg_wd_work, 0); 974 + } 975 + break; 976 + default: 977 + break; 978 + } 979 + break; 980 + 981 + case POWER_SUPPLY_PROP_HEALTH: 982 + switch (ext->type) { 983 + case POWER_SUPPLY_TYPE_BATTERY: 984 + break; 985 + case POWER_SUPPLY_TYPE_MAINS: 986 + switch (ret.intval) { 987 + case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: 988 + di->events.mainextchnotok = true; 989 + di->events.main_thermal_prot = false; 990 + di->events.main_ovv = false; 991 + di->events.ac_wd_expired = false; 992 + break; 993 + case POWER_SUPPLY_HEALTH_DEAD: 994 + di->events.ac_wd_expired = true; 995 + di->events.mainextchnotok = false; 996 + di->events.main_ovv = false; 997 + di->events.main_thermal_prot = false; 998 + break; 999 + case POWER_SUPPLY_HEALTH_COLD: 1000 + case POWER_SUPPLY_HEALTH_OVERHEAT: 1001 + di->events.main_thermal_prot = true; 1002 + di->events.mainextchnotok = false; 1003 + di->events.main_ovv = false; 1004 + di->events.ac_wd_expired = false; 1005 + break; 1006 + case POWER_SUPPLY_HEALTH_OVERVOLTAGE: 1007 + di->events.main_ovv = true; 1008 + di->events.mainextchnotok = false; 1009 + di->events.main_thermal_prot = false; 1010 + di->events.ac_wd_expired = false; 1011 + break; 1012 + case POWER_SUPPLY_HEALTH_GOOD: 1013 + di->events.main_thermal_prot = false; 1014 + di->events.mainextchnotok = false; 1015 + di->events.main_ovv = false; 1016 + di->events.ac_wd_expired = false; 1017 + break; 1018 + default: 1019 + break; 1020 + } 1021 + break; 1022 + 1023 + case POWER_SUPPLY_TYPE_USB: 1024 + switch (ret.intval) { 1025 + case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: 1026 + di->events.usbchargernotok = true; 1027 + di->events.usb_thermal_prot = false; 1028 + di->events.vbus_ovv = false; 1029 + di->events.usb_wd_expired = false; 1030 + break; 1031 + case POWER_SUPPLY_HEALTH_DEAD: 1032 + di->events.usb_wd_expired = true; 1033 + di->events.usbchargernotok = false; 1034 + di->events.usb_thermal_prot = false; 1035 + di->events.vbus_ovv = false; 1036 + break; 1037 + case POWER_SUPPLY_HEALTH_COLD: 1038 + case POWER_SUPPLY_HEALTH_OVERHEAT: 1039 + di->events.usb_thermal_prot = true; 1040 + di->events.usbchargernotok = false; 1041 + di->events.vbus_ovv = false; 1042 + di->events.usb_wd_expired = false; 1043 + break; 1044 + case POWER_SUPPLY_HEALTH_OVERVOLTAGE: 1045 + di->events.vbus_ovv = true; 1046 + di->events.usbchargernotok = false; 1047 + di->events.usb_thermal_prot = false; 1048 + di->events.usb_wd_expired = false; 1049 + break; 1050 + case POWER_SUPPLY_HEALTH_GOOD: 1051 + di->events.usbchargernotok = false; 1052 + di->events.usb_thermal_prot = false; 1053 + di->events.vbus_ovv = false; 1054 + di->events.usb_wd_expired = false; 1055 + break; 1056 + default: 1057 + break; 1058 + } 1059 + default: 1060 + break; 1061 + } 1062 + break; 1063 + 1064 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1065 + switch (ext->type) { 1066 + case POWER_SUPPLY_TYPE_BATTERY: 1067 + di->batt_data.volt = ret.intval / 1000; 1068 + break; 1069 + case POWER_SUPPLY_TYPE_MAINS: 1070 + di->chg_info.ac_volt = ret.intval / 1000; 1071 + break; 1072 + case POWER_SUPPLY_TYPE_USB: 1073 + di->chg_info.usb_volt = ret.intval / 1000; 1074 + break; 1075 + default: 1076 + break; 1077 + } 1078 + break; 1079 + 1080 + case POWER_SUPPLY_PROP_VOLTAGE_AVG: 1081 + switch (ext->type) { 1082 + case POWER_SUPPLY_TYPE_MAINS: 1083 + /* AVG is used to indicate when we are 1084 + * in CV mode */ 1085 + if (ret.intval) 1086 + di->events.ac_cv_active = true; 1087 + else 1088 + di->events.ac_cv_active = false; 1089 + 1090 + break; 1091 + case POWER_SUPPLY_TYPE_USB: 1092 + /* AVG is used to indicate when we are 1093 + * in CV mode */ 1094 + if (ret.intval) 1095 + di->events.usb_cv_active = true; 1096 + else 1097 + di->events.usb_cv_active = false; 1098 + 1099 + break; 1100 + default: 1101 + break; 1102 + } 1103 + break; 1104 + 1105 + case POWER_SUPPLY_PROP_TECHNOLOGY: 1106 + switch (ext->type) { 1107 + case POWER_SUPPLY_TYPE_BATTERY: 1108 + if (ret.intval) 1109 + di->events.batt_unknown = false; 1110 + else 1111 + di->events.batt_unknown = true; 1112 + 1113 + break; 1114 + default: 1115 + break; 1116 + } 1117 + break; 1118 + 1119 + case POWER_SUPPLY_PROP_TEMP: 1120 + di->batt_data.temp = ret.intval / 10; 1121 + break; 1122 + 1123 + case POWER_SUPPLY_PROP_CURRENT_NOW: 1124 + switch (ext->type) { 1125 + case POWER_SUPPLY_TYPE_MAINS: 1126 + di->chg_info.ac_curr = 1127 + ret.intval / 1000; 1128 + break; 1129 + case POWER_SUPPLY_TYPE_USB: 1130 + di->chg_info.usb_curr = 1131 + ret.intval / 1000; 1132 + break; 1133 + case POWER_SUPPLY_TYPE_BATTERY: 1134 + di->batt_data.inst_curr = ret.intval / 1000; 1135 + break; 1136 + default: 1137 + break; 1138 + } 1139 + break; 1140 + 1141 + case POWER_SUPPLY_PROP_CURRENT_AVG: 1142 + switch (ext->type) { 1143 + case POWER_SUPPLY_TYPE_BATTERY: 1144 + di->batt_data.avg_curr = ret.intval / 1000; 1145 + break; 1146 + case POWER_SUPPLY_TYPE_USB: 1147 + if (ret.intval) 1148 + di->events.vbus_collapsed = true; 1149 + else 1150 + di->events.vbus_collapsed = false; 1151 + break; 1152 + default: 1153 + break; 1154 + } 1155 + break; 1156 + case POWER_SUPPLY_PROP_CAPACITY: 1157 + di->batt_data.percent = ret.intval; 1158 + break; 1159 + default: 1160 + break; 1161 + } 1162 + } 1163 + return 0; 1164 + } 1165 + 1166 + /** 1167 + * abx500_chargalg_external_power_changed() - callback for power supply changes 1168 + * @psy: pointer to the structure power_supply 1169 + * 1170 + * This function is the entry point of the pointer external_power_changed 1171 + * of the structure power_supply. 1172 + * This function gets executed when there is a change in any external power 1173 + * supply that this driver needs to be notified of. 1174 + */ 1175 + static void abx500_chargalg_external_power_changed(struct power_supply *psy) 1176 + { 1177 + struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy); 1178 + 1179 + /* 1180 + * Trigger execution of the algorithm instantly and read 1181 + * all power_supply properties there instead 1182 + */ 1183 + queue_work(di->chargalg_wq, &di->chargalg_work); 1184 + } 1185 + 1186 + /** 1187 + * abx500_chargalg_algorithm() - Main function for the algorithm 1188 + * @di: pointer to the abx500_chargalg structure 1189 + * 1190 + * This is the main control function for the charging algorithm. 1191 + * It is called periodically or when something happens that will 1192 + * trigger a state change 1193 + */ 1194 + static void abx500_chargalg_algorithm(struct abx500_chargalg *di) 1195 + { 1196 + int charger_status; 1197 + 1198 + /* Collect data from all power_supply class devices */ 1199 + class_for_each_device(power_supply_class, NULL, 1200 + &di->chargalg_psy, abx500_chargalg_get_ext_psy_data); 1201 + 1202 + abx500_chargalg_end_of_charge(di); 1203 + abx500_chargalg_check_temp(di); 1204 + abx500_chargalg_check_charger_voltage(di); 1205 + 1206 + charger_status = abx500_chargalg_check_charger_connection(di); 1207 + /* 1208 + * First check if we have a charger connected. 1209 + * Also we don't allow charging of unknown batteries if configured 1210 + * this way 1211 + */ 1212 + if (!charger_status || 1213 + (di->events.batt_unknown && !di->bat->chg_unknown_bat)) { 1214 + if (di->charge_state != STATE_HANDHELD) { 1215 + di->events.safety_timer_expired = false; 1216 + abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); 1217 + } 1218 + } 1219 + 1220 + /* If suspended, we should not continue checking the flags */ 1221 + else if (di->charge_state == STATE_SUSPENDED_INIT || 1222 + di->charge_state == STATE_SUSPENDED) { 1223 + /* We don't do anything here, just don,t continue */ 1224 + } 1225 + 1226 + /* Safety timer expiration */ 1227 + else if (di->events.safety_timer_expired) { 1228 + if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED) 1229 + abx500_chargalg_state_to(di, 1230 + STATE_SAFETY_TIMER_EXPIRED_INIT); 1231 + } 1232 + /* 1233 + * Check if any interrupts has occured 1234 + * that will prevent us from charging 1235 + */ 1236 + 1237 + /* Battery removed */ 1238 + else if (di->events.batt_rem) { 1239 + if (di->charge_state != STATE_BATT_REMOVED) 1240 + abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT); 1241 + } 1242 + /* Main or USB charger not ok. */ 1243 + else if (di->events.mainextchnotok || di->events.usbchargernotok) { 1244 + /* 1245 + * If vbus_collapsed is set, we have to lower the charger 1246 + * current, which is done in the normal state below 1247 + */ 1248 + if (di->charge_state != STATE_CHG_NOT_OK && 1249 + !di->events.vbus_collapsed) 1250 + abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT); 1251 + } 1252 + /* VBUS, Main or VBAT OVV. */ 1253 + else if (di->events.vbus_ovv || 1254 + di->events.main_ovv || 1255 + di->events.batt_ovv || 1256 + !di->chg_info.usb_chg_ok || 1257 + !di->chg_info.ac_chg_ok) { 1258 + if (di->charge_state != STATE_OVV_PROTECT) 1259 + abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT); 1260 + } 1261 + /* USB Thermal, stop charging */ 1262 + else if (di->events.main_thermal_prot || 1263 + di->events.usb_thermal_prot) { 1264 + if (di->charge_state != STATE_HW_TEMP_PROTECT) 1265 + abx500_chargalg_state_to(di, 1266 + STATE_HW_TEMP_PROTECT_INIT); 1267 + } 1268 + /* Battery temp over/under */ 1269 + else if (di->events.btemp_underover) { 1270 + if (di->charge_state != STATE_TEMP_UNDEROVER) 1271 + abx500_chargalg_state_to(di, 1272 + STATE_TEMP_UNDEROVER_INIT); 1273 + } 1274 + /* Watchdog expired */ 1275 + else if (di->events.ac_wd_expired || 1276 + di->events.usb_wd_expired) { 1277 + if (di->charge_state != STATE_WD_EXPIRED) 1278 + abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT); 1279 + } 1280 + /* Battery temp high/low */ 1281 + else if (di->events.btemp_lowhigh) { 1282 + if (di->charge_state != STATE_TEMP_LOWHIGH) 1283 + abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT); 1284 + } 1285 + 1286 + dev_dbg(di->dev, 1287 + "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d " 1288 + "State %s Active_chg %d Chg_status %d AC %d USB %d " 1289 + "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d " 1290 + "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n", 1291 + di->batt_data.volt, 1292 + di->batt_data.avg_curr, 1293 + di->batt_data.inst_curr, 1294 + di->batt_data.temp, 1295 + di->batt_data.percent, 1296 + di->maintenance_chg, 1297 + states[di->charge_state], 1298 + di->chg_info.charger_type, 1299 + di->charge_status, 1300 + di->chg_info.conn_chg & AC_CHG, 1301 + di->chg_info.conn_chg & USB_CHG, 1302 + di->chg_info.online_chg & AC_CHG, 1303 + di->chg_info.online_chg & USB_CHG, 1304 + di->events.ac_cv_active, 1305 + di->events.usb_cv_active, 1306 + di->chg_info.ac_curr, 1307 + di->chg_info.usb_curr, 1308 + di->chg_info.ac_vset, 1309 + di->chg_info.ac_iset, 1310 + di->chg_info.usb_vset, 1311 + di->chg_info.usb_iset); 1312 + 1313 + switch (di->charge_state) { 1314 + case STATE_HANDHELD_INIT: 1315 + abx500_chargalg_stop_charging(di); 1316 + di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; 1317 + abx500_chargalg_state_to(di, STATE_HANDHELD); 1318 + /* Intentional fallthrough */ 1319 + 1320 + case STATE_HANDHELD: 1321 + break; 1322 + 1323 + case STATE_SUSPENDED_INIT: 1324 + if (di->susp_status.ac_suspended) 1325 + abx500_chargalg_ac_en(di, false, 0, 0); 1326 + if (di->susp_status.usb_suspended) 1327 + abx500_chargalg_usb_en(di, false, 0, 0); 1328 + abx500_chargalg_stop_safety_timer(di); 1329 + abx500_chargalg_stop_maintenance_timer(di); 1330 + di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1331 + di->maintenance_chg = false; 1332 + abx500_chargalg_state_to(di, STATE_SUSPENDED); 1333 + power_supply_changed(&di->chargalg_psy); 1334 + /* Intentional fallthrough */ 1335 + 1336 + case STATE_SUSPENDED: 1337 + /* CHARGING is suspended */ 1338 + break; 1339 + 1340 + case STATE_BATT_REMOVED_INIT: 1341 + abx500_chargalg_stop_charging(di); 1342 + abx500_chargalg_state_to(di, STATE_BATT_REMOVED); 1343 + /* Intentional fallthrough */ 1344 + 1345 + case STATE_BATT_REMOVED: 1346 + if (!di->events.batt_rem) 1347 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1348 + break; 1349 + 1350 + case STATE_HW_TEMP_PROTECT_INIT: 1351 + abx500_chargalg_stop_charging(di); 1352 + abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT); 1353 + /* Intentional fallthrough */ 1354 + 1355 + case STATE_HW_TEMP_PROTECT: 1356 + if (!di->events.main_thermal_prot && 1357 + !di->events.usb_thermal_prot) 1358 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1359 + break; 1360 + 1361 + case STATE_OVV_PROTECT_INIT: 1362 + abx500_chargalg_stop_charging(di); 1363 + abx500_chargalg_state_to(di, STATE_OVV_PROTECT); 1364 + /* Intentional fallthrough */ 1365 + 1366 + case STATE_OVV_PROTECT: 1367 + if (!di->events.vbus_ovv && 1368 + !di->events.main_ovv && 1369 + !di->events.batt_ovv && 1370 + di->chg_info.usb_chg_ok && 1371 + di->chg_info.ac_chg_ok) 1372 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1373 + break; 1374 + 1375 + case STATE_CHG_NOT_OK_INIT: 1376 + abx500_chargalg_stop_charging(di); 1377 + abx500_chargalg_state_to(di, STATE_CHG_NOT_OK); 1378 + /* Intentional fallthrough */ 1379 + 1380 + case STATE_CHG_NOT_OK: 1381 + if (!di->events.mainextchnotok && 1382 + !di->events.usbchargernotok) 1383 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1384 + break; 1385 + 1386 + case STATE_SAFETY_TIMER_EXPIRED_INIT: 1387 + abx500_chargalg_stop_charging(di); 1388 + abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED); 1389 + /* Intentional fallthrough */ 1390 + 1391 + case STATE_SAFETY_TIMER_EXPIRED: 1392 + /* We exit this state when charger is removed */ 1393 + break; 1394 + 1395 + case STATE_NORMAL_INIT: 1396 + abx500_chargalg_start_charging(di, 1397 + di->bat->bat_type[di->bat->batt_id].normal_vol_lvl, 1398 + di->bat->bat_type[di->bat->batt_id].normal_cur_lvl); 1399 + abx500_chargalg_state_to(di, STATE_NORMAL); 1400 + abx500_chargalg_start_safety_timer(di); 1401 + abx500_chargalg_stop_maintenance_timer(di); 1402 + init_maxim_chg_curr(di); 1403 + di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 1404 + di->eoc_cnt = 0; 1405 + di->maintenance_chg = false; 1406 + power_supply_changed(&di->chargalg_psy); 1407 + 1408 + break; 1409 + 1410 + case STATE_NORMAL: 1411 + handle_maxim_chg_curr(di); 1412 + if (di->charge_status == POWER_SUPPLY_STATUS_FULL && 1413 + di->maintenance_chg) { 1414 + if (di->bat->no_maintenance) 1415 + abx500_chargalg_state_to(di, 1416 + STATE_WAIT_FOR_RECHARGE_INIT); 1417 + else 1418 + abx500_chargalg_state_to(di, 1419 + STATE_MAINTENANCE_A_INIT); 1420 + } 1421 + break; 1422 + 1423 + /* This state will be used when the maintenance state is disabled */ 1424 + case STATE_WAIT_FOR_RECHARGE_INIT: 1425 + abx500_chargalg_hold_charging(di); 1426 + abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE); 1427 + di->rch_cnt = RCH_COND_CNT; 1428 + /* Intentional fallthrough */ 1429 + 1430 + case STATE_WAIT_FOR_RECHARGE: 1431 + if (di->batt_data.volt <= 1432 + di->bat->bat_type[di->bat->batt_id].recharge_vol) { 1433 + if (di->rch_cnt-- == 0) 1434 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1435 + } else 1436 + di->rch_cnt = RCH_COND_CNT; 1437 + break; 1438 + 1439 + case STATE_MAINTENANCE_A_INIT: 1440 + abx500_chargalg_stop_safety_timer(di); 1441 + abx500_chargalg_start_maintenance_timer(di, 1442 + di->bat->bat_type[ 1443 + di->bat->batt_id].maint_a_chg_timer_h); 1444 + abx500_chargalg_start_charging(di, 1445 + di->bat->bat_type[ 1446 + di->bat->batt_id].maint_a_vol_lvl, 1447 + di->bat->bat_type[ 1448 + di->bat->batt_id].maint_a_cur_lvl); 1449 + abx500_chargalg_state_to(di, STATE_MAINTENANCE_A); 1450 + power_supply_changed(&di->chargalg_psy); 1451 + /* Intentional fallthrough*/ 1452 + 1453 + case STATE_MAINTENANCE_A: 1454 + if (di->events.maintenance_timer_expired) { 1455 + abx500_chargalg_stop_maintenance_timer(di); 1456 + abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT); 1457 + } 1458 + break; 1459 + 1460 + case STATE_MAINTENANCE_B_INIT: 1461 + abx500_chargalg_start_maintenance_timer(di, 1462 + di->bat->bat_type[ 1463 + di->bat->batt_id].maint_b_chg_timer_h); 1464 + abx500_chargalg_start_charging(di, 1465 + di->bat->bat_type[ 1466 + di->bat->batt_id].maint_b_vol_lvl, 1467 + di->bat->bat_type[ 1468 + di->bat->batt_id].maint_b_cur_lvl); 1469 + abx500_chargalg_state_to(di, STATE_MAINTENANCE_B); 1470 + power_supply_changed(&di->chargalg_psy); 1471 + /* Intentional fallthrough*/ 1472 + 1473 + case STATE_MAINTENANCE_B: 1474 + if (di->events.maintenance_timer_expired) { 1475 + abx500_chargalg_stop_maintenance_timer(di); 1476 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1477 + } 1478 + break; 1479 + 1480 + case STATE_TEMP_LOWHIGH_INIT: 1481 + abx500_chargalg_start_charging(di, 1482 + di->bat->bat_type[ 1483 + di->bat->batt_id].low_high_vol_lvl, 1484 + di->bat->bat_type[ 1485 + di->bat->batt_id].low_high_cur_lvl); 1486 + abx500_chargalg_stop_maintenance_timer(di); 1487 + di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 1488 + abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH); 1489 + power_supply_changed(&di->chargalg_psy); 1490 + /* Intentional fallthrough */ 1491 + 1492 + case STATE_TEMP_LOWHIGH: 1493 + if (!di->events.btemp_lowhigh) 1494 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1495 + break; 1496 + 1497 + case STATE_WD_EXPIRED_INIT: 1498 + abx500_chargalg_stop_charging(di); 1499 + abx500_chargalg_state_to(di, STATE_WD_EXPIRED); 1500 + /* Intentional fallthrough */ 1501 + 1502 + case STATE_WD_EXPIRED: 1503 + if (!di->events.ac_wd_expired && 1504 + !di->events.usb_wd_expired) 1505 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1506 + break; 1507 + 1508 + case STATE_TEMP_UNDEROVER_INIT: 1509 + abx500_chargalg_stop_charging(di); 1510 + abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER); 1511 + /* Intentional fallthrough */ 1512 + 1513 + case STATE_TEMP_UNDEROVER: 1514 + if (!di->events.btemp_underover) 1515 + abx500_chargalg_state_to(di, STATE_NORMAL_INIT); 1516 + break; 1517 + } 1518 + 1519 + /* Start charging directly if the new state is a charge state */ 1520 + if (di->charge_state == STATE_NORMAL_INIT || 1521 + di->charge_state == STATE_MAINTENANCE_A_INIT || 1522 + di->charge_state == STATE_MAINTENANCE_B_INIT) 1523 + queue_work(di->chargalg_wq, &di->chargalg_work); 1524 + } 1525 + 1526 + /** 1527 + * abx500_chargalg_periodic_work() - Periodic work for the algorithm 1528 + * @work: pointer to the work_struct structure 1529 + * 1530 + * Work queue function for the charging algorithm 1531 + */ 1532 + static void abx500_chargalg_periodic_work(struct work_struct *work) 1533 + { 1534 + struct abx500_chargalg *di = container_of(work, 1535 + struct abx500_chargalg, chargalg_periodic_work.work); 1536 + 1537 + abx500_chargalg_algorithm(di); 1538 + 1539 + /* 1540 + * If a charger is connected then the battery has to be monitored 1541 + * frequently, else the work can be delayed. 1542 + */ 1543 + if (di->chg_info.conn_chg) 1544 + queue_delayed_work(di->chargalg_wq, 1545 + &di->chargalg_periodic_work, 1546 + di->bat->interval_charging * HZ); 1547 + else 1548 + queue_delayed_work(di->chargalg_wq, 1549 + &di->chargalg_periodic_work, 1550 + di->bat->interval_not_charging * HZ); 1551 + } 1552 + 1553 + /** 1554 + * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog 1555 + * @work: pointer to the work_struct structure 1556 + * 1557 + * Work queue function for kicking the charger watchdog 1558 + */ 1559 + static void abx500_chargalg_wd_work(struct work_struct *work) 1560 + { 1561 + int ret; 1562 + struct abx500_chargalg *di = container_of(work, 1563 + struct abx500_chargalg, chargalg_wd_work.work); 1564 + 1565 + dev_dbg(di->dev, "abx500_chargalg_wd_work\n"); 1566 + 1567 + ret = abx500_chargalg_kick_watchdog(di); 1568 + if (ret < 0) 1569 + dev_err(di->dev, "failed to kick watchdog\n"); 1570 + 1571 + queue_delayed_work(di->chargalg_wq, 1572 + &di->chargalg_wd_work, CHG_WD_INTERVAL); 1573 + } 1574 + 1575 + /** 1576 + * abx500_chargalg_work() - Work to run the charging algorithm instantly 1577 + * @work: pointer to the work_struct structure 1578 + * 1579 + * Work queue function for calling the charging algorithm 1580 + */ 1581 + static void abx500_chargalg_work(struct work_struct *work) 1582 + { 1583 + struct abx500_chargalg *di = container_of(work, 1584 + struct abx500_chargalg, chargalg_work); 1585 + 1586 + abx500_chargalg_algorithm(di); 1587 + } 1588 + 1589 + /** 1590 + * abx500_chargalg_get_property() - get the chargalg properties 1591 + * @psy: pointer to the power_supply structure 1592 + * @psp: pointer to the power_supply_property structure 1593 + * @val: pointer to the power_supply_propval union 1594 + * 1595 + * This function gets called when an application tries to get the 1596 + * chargalg properties by reading the sysfs files. 1597 + * status: charging/discharging/full/unknown 1598 + * health: health of the battery 1599 + * Returns error code in case of failure else 0 on success 1600 + */ 1601 + static int abx500_chargalg_get_property(struct power_supply *psy, 1602 + enum power_supply_property psp, 1603 + union power_supply_propval *val) 1604 + { 1605 + struct abx500_chargalg *di; 1606 + 1607 + di = to_abx500_chargalg_device_info(psy); 1608 + 1609 + switch (psp) { 1610 + case POWER_SUPPLY_PROP_STATUS: 1611 + val->intval = di->charge_status; 1612 + break; 1613 + case POWER_SUPPLY_PROP_HEALTH: 1614 + if (di->events.batt_ovv) { 1615 + val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 1616 + } else if (di->events.btemp_underover) { 1617 + if (di->batt_data.temp <= di->bat->temp_under) 1618 + val->intval = POWER_SUPPLY_HEALTH_COLD; 1619 + else 1620 + val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 1621 + } else { 1622 + val->intval = POWER_SUPPLY_HEALTH_GOOD; 1623 + } 1624 + break; 1625 + default: 1626 + return -EINVAL; 1627 + } 1628 + return 0; 1629 + } 1630 + 1631 + /* Exposure to the sysfs interface */ 1632 + 1633 + /** 1634 + * abx500_chargalg_sysfs_charger() - sysfs store operations 1635 + * @kobj: pointer to the struct kobject 1636 + * @attr: pointer to the struct attribute 1637 + * @buf: buffer that holds the parameter passed from userspace 1638 + * @length: length of the parameter passed 1639 + * 1640 + * Returns length of the buffer(input taken from user space) on success 1641 + * else error code on failure 1642 + * The operation to be performed on passing the parameters from the user space. 1643 + */ 1644 + static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj, 1645 + struct attribute *attr, const char *buf, size_t length) 1646 + { 1647 + struct abx500_chargalg *di = container_of(kobj, 1648 + struct abx500_chargalg, chargalg_kobject); 1649 + long int param; 1650 + int ac_usb; 1651 + int ret; 1652 + char entry = *attr->name; 1653 + 1654 + switch (entry) { 1655 + case 'c': 1656 + ret = strict_strtol(buf, 10, &param); 1657 + if (ret < 0) 1658 + return ret; 1659 + 1660 + ac_usb = param; 1661 + switch (ac_usb) { 1662 + case 0: 1663 + /* Disable charging */ 1664 + di->susp_status.ac_suspended = true; 1665 + di->susp_status.usb_suspended = true; 1666 + di->susp_status.suspended_change = true; 1667 + /* Trigger a state change */ 1668 + queue_work(di->chargalg_wq, 1669 + &di->chargalg_work); 1670 + break; 1671 + case 1: 1672 + /* Enable AC Charging */ 1673 + di->susp_status.ac_suspended = false; 1674 + di->susp_status.suspended_change = true; 1675 + /* Trigger a state change */ 1676 + queue_work(di->chargalg_wq, 1677 + &di->chargalg_work); 1678 + break; 1679 + case 2: 1680 + /* Enable USB charging */ 1681 + di->susp_status.usb_suspended = false; 1682 + di->susp_status.suspended_change = true; 1683 + /* Trigger a state change */ 1684 + queue_work(di->chargalg_wq, 1685 + &di->chargalg_work); 1686 + break; 1687 + default: 1688 + dev_info(di->dev, "Wrong input\n" 1689 + "Enter 0. Disable AC/USB Charging\n" 1690 + "1. Enable AC charging\n" 1691 + "2. Enable USB Charging\n"); 1692 + }; 1693 + break; 1694 + }; 1695 + return strlen(buf); 1696 + } 1697 + 1698 + static struct attribute abx500_chargalg_en_charger = \ 1699 + { 1700 + .name = "chargalg", 1701 + .mode = S_IWUGO, 1702 + }; 1703 + 1704 + static struct attribute *abx500_chargalg_chg[] = { 1705 + &abx500_chargalg_en_charger, 1706 + NULL 1707 + }; 1708 + 1709 + static const struct sysfs_ops abx500_chargalg_sysfs_ops = { 1710 + .store = abx500_chargalg_sysfs_charger, 1711 + }; 1712 + 1713 + static struct kobj_type abx500_chargalg_ktype = { 1714 + .sysfs_ops = &abx500_chargalg_sysfs_ops, 1715 + .default_attrs = abx500_chargalg_chg, 1716 + }; 1717 + 1718 + /** 1719 + * abx500_chargalg_sysfs_exit() - de-init of sysfs entry 1720 + * @di: pointer to the struct abx500_chargalg 1721 + * 1722 + * This function removes the entry in sysfs. 1723 + */ 1724 + static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di) 1725 + { 1726 + kobject_del(&di->chargalg_kobject); 1727 + } 1728 + 1729 + /** 1730 + * abx500_chargalg_sysfs_init() - init of sysfs entry 1731 + * @di: pointer to the struct abx500_chargalg 1732 + * 1733 + * This function adds an entry in sysfs. 1734 + * Returns error code in case of failure else 0(on success) 1735 + */ 1736 + static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di) 1737 + { 1738 + int ret = 0; 1739 + 1740 + ret = kobject_init_and_add(&di->chargalg_kobject, 1741 + &abx500_chargalg_ktype, 1742 + NULL, "abx500_chargalg"); 1743 + if (ret < 0) 1744 + dev_err(di->dev, "failed to create sysfs entry\n"); 1745 + 1746 + return ret; 1747 + } 1748 + /* Exposure to the sysfs interface <<END>> */ 1749 + 1750 + #if defined(CONFIG_PM) 1751 + static int abx500_chargalg_resume(struct platform_device *pdev) 1752 + { 1753 + struct abx500_chargalg *di = platform_get_drvdata(pdev); 1754 + 1755 + /* Kick charger watchdog if charging (any charger online) */ 1756 + if (di->chg_info.online_chg) 1757 + queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0); 1758 + 1759 + /* 1760 + * Run the charging algorithm directly to be sure we don't 1761 + * do it too seldom 1762 + */ 1763 + queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); 1764 + 1765 + return 0; 1766 + } 1767 + 1768 + static int abx500_chargalg_suspend(struct platform_device *pdev, 1769 + pm_message_t state) 1770 + { 1771 + struct abx500_chargalg *di = platform_get_drvdata(pdev); 1772 + 1773 + if (di->chg_info.online_chg) 1774 + cancel_delayed_work_sync(&di->chargalg_wd_work); 1775 + 1776 + cancel_delayed_work_sync(&di->chargalg_periodic_work); 1777 + 1778 + return 0; 1779 + } 1780 + #else 1781 + #define abx500_chargalg_suspend NULL 1782 + #define abx500_chargalg_resume NULL 1783 + #endif 1784 + 1785 + static int __devexit abx500_chargalg_remove(struct platform_device *pdev) 1786 + { 1787 + struct abx500_chargalg *di = platform_get_drvdata(pdev); 1788 + 1789 + /* sysfs interface to enable/disbale charging from user space */ 1790 + abx500_chargalg_sysfs_exit(di); 1791 + 1792 + /* Delete the work queue */ 1793 + destroy_workqueue(di->chargalg_wq); 1794 + 1795 + flush_scheduled_work(); 1796 + power_supply_unregister(&di->chargalg_psy); 1797 + platform_set_drvdata(pdev, NULL); 1798 + kfree(di); 1799 + 1800 + return 0; 1801 + } 1802 + 1803 + static int __devinit abx500_chargalg_probe(struct platform_device *pdev) 1804 + { 1805 + struct abx500_bm_plat_data *plat_data; 1806 + int ret = 0; 1807 + 1808 + struct abx500_chargalg *di = 1809 + kzalloc(sizeof(struct abx500_chargalg), GFP_KERNEL); 1810 + if (!di) 1811 + return -ENOMEM; 1812 + 1813 + /* get device struct */ 1814 + di->dev = &pdev->dev; 1815 + 1816 + plat_data = pdev->dev.platform_data; 1817 + di->pdata = plat_data->chargalg; 1818 + di->bat = plat_data->battery; 1819 + 1820 + /* chargalg supply */ 1821 + di->chargalg_psy.name = "abx500_chargalg"; 1822 + di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY; 1823 + di->chargalg_psy.properties = abx500_chargalg_props; 1824 + di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props); 1825 + di->chargalg_psy.get_property = abx500_chargalg_get_property; 1826 + di->chargalg_psy.supplied_to = di->pdata->supplied_to; 1827 + di->chargalg_psy.num_supplicants = di->pdata->num_supplicants; 1828 + di->chargalg_psy.external_power_changed = 1829 + abx500_chargalg_external_power_changed; 1830 + 1831 + /* Initilialize safety timer */ 1832 + init_timer(&di->safety_timer); 1833 + di->safety_timer.function = abx500_chargalg_safety_timer_expired; 1834 + di->safety_timer.data = (unsigned long) di; 1835 + 1836 + /* Initilialize maintenance timer */ 1837 + init_timer(&di->maintenance_timer); 1838 + di->maintenance_timer.function = 1839 + abx500_chargalg_maintenance_timer_expired; 1840 + di->maintenance_timer.data = (unsigned long) di; 1841 + 1842 + /* Create a work queue for the chargalg */ 1843 + di->chargalg_wq = 1844 + create_singlethread_workqueue("abx500_chargalg_wq"); 1845 + if (di->chargalg_wq == NULL) { 1846 + dev_err(di->dev, "failed to create work queue\n"); 1847 + goto free_device_info; 1848 + } 1849 + 1850 + /* Init work for chargalg */ 1851 + INIT_DELAYED_WORK_DEFERRABLE(&di->chargalg_periodic_work, 1852 + abx500_chargalg_periodic_work); 1853 + INIT_DELAYED_WORK_DEFERRABLE(&di->chargalg_wd_work, 1854 + abx500_chargalg_wd_work); 1855 + 1856 + /* Init work for chargalg */ 1857 + INIT_WORK(&di->chargalg_work, abx500_chargalg_work); 1858 + 1859 + /* To detect charger at startup */ 1860 + di->chg_info.prev_conn_chg = -1; 1861 + 1862 + /* Register chargalg power supply class */ 1863 + ret = power_supply_register(di->dev, &di->chargalg_psy); 1864 + if (ret) { 1865 + dev_err(di->dev, "failed to register chargalg psy\n"); 1866 + goto free_chargalg_wq; 1867 + } 1868 + 1869 + platform_set_drvdata(pdev, di); 1870 + 1871 + /* sysfs interface to enable/disable charging from user space */ 1872 + ret = abx500_chargalg_sysfs_init(di); 1873 + if (ret) { 1874 + dev_err(di->dev, "failed to create sysfs entry\n"); 1875 + goto free_psy; 1876 + } 1877 + 1878 + /* Run the charging algorithm */ 1879 + queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); 1880 + 1881 + dev_info(di->dev, "probe success\n"); 1882 + return ret; 1883 + 1884 + free_psy: 1885 + power_supply_unregister(&di->chargalg_psy); 1886 + free_chargalg_wq: 1887 + destroy_workqueue(di->chargalg_wq); 1888 + free_device_info: 1889 + kfree(di); 1890 + 1891 + return ret; 1892 + } 1893 + 1894 + static struct platform_driver abx500_chargalg_driver = { 1895 + .probe = abx500_chargalg_probe, 1896 + .remove = __devexit_p(abx500_chargalg_remove), 1897 + .suspend = abx500_chargalg_suspend, 1898 + .resume = abx500_chargalg_resume, 1899 + .driver = { 1900 + .name = "abx500-chargalg", 1901 + .owner = THIS_MODULE, 1902 + }, 1903 + }; 1904 + 1905 + static int __init abx500_chargalg_init(void) 1906 + { 1907 + return platform_driver_register(&abx500_chargalg_driver); 1908 + } 1909 + 1910 + static void __exit abx500_chargalg_exit(void) 1911 + { 1912 + platform_driver_unregister(&abx500_chargalg_driver); 1913 + } 1914 + 1915 + module_init(abx500_chargalg_init); 1916 + module_exit(abx500_chargalg_exit); 1917 + 1918 + MODULE_LICENSE("GPL v2"); 1919 + MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); 1920 + MODULE_ALIAS("platform:abx500-chargalg"); 1921 + MODULE_DESCRIPTION("abx500 battery charging algorithm");
+29 -38
drivers/power/charger-manager.c
··· 134 134 union power_supply_propval val; 135 135 int ret; 136 136 137 - if (cm->fuel_gauge) 138 - ret = cm->fuel_gauge->get_property(cm->fuel_gauge, 139 - POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); 140 - else 137 + if (!cm->fuel_gauge) 141 138 return -ENODEV; 142 139 140 + ret = cm->fuel_gauge->get_property(cm->fuel_gauge, 141 + POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); 143 142 if (ret) 144 143 return ret; 145 144 ··· 244 245 struct charger_desc *desc = cm->desc; 245 246 246 247 /* Ignore if it's redundent command */ 247 - if (enable && cm->charger_enabled) 248 - return 0; 249 - if (!enable && !cm->charger_enabled) 248 + if (enable == cm->charger_enabled) 250 249 return 0; 251 250 252 251 if (enable) { ··· 306 309 307 310 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE)) 308 311 return; /* Duplicated. */ 309 - else 310 - strncpy(env_str_save, event, UEVENT_BUF_SIZE); 311 - 312 + strncpy(env_str_save, event, UEVENT_BUF_SIZE); 312 313 return; 313 314 } 314 315 ··· 382 387 383 388 mutex_lock(&cm_list_mtx); 384 389 385 - list_for_each_entry(cm, &cm_list, entry) 386 - stop = stop || _cm_monitor(cm); 390 + list_for_each_entry(cm, &cm_list, entry) { 391 + if (_cm_monitor(cm)) 392 + stop = true; 393 + } 387 394 388 395 mutex_unlock(&cm_list_mtx); 389 396 ··· 399 402 struct charger_manager *cm = container_of(psy, 400 403 struct charger_manager, charger_psy); 401 404 struct charger_desc *desc = cm->desc; 402 - int i, ret = 0, uV; 405 + int ret = 0; 406 + int uV; 403 407 404 408 switch (psp) { 405 409 case POWER_SUPPLY_PROP_STATUS: ··· 426 428 val->intval = 0; 427 429 break; 428 430 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 429 - ret = get_batt_uV(cm, &i); 430 - val->intval = i; 431 + ret = get_batt_uV(cm, &val->intval); 431 432 break; 432 433 case POWER_SUPPLY_PROP_CURRENT_NOW: 433 434 ret = cm->fuel_gauge->get_property(cm->fuel_gauge, ··· 694 697 mutex_lock(&cm_list_mtx); 695 698 list_for_each_entry(cm, &cm_list, entry) { 696 699 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) || 697 - cm->status_save_batt != is_batt_present(cm)) 700 + cm->status_save_batt != is_batt_present(cm)) { 698 701 ret = false; 702 + break; 703 + } 699 704 } 700 705 mutex_unlock(&cm_list_mtx); 701 706 ··· 854 855 855 856 platform_set_drvdata(pdev, cm); 856 857 857 - memcpy(&cm->charger_psy, &psy_default, 858 - sizeof(psy_default)); 858 + memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default)); 859 + 859 860 if (!desc->psy_name) { 860 - strncpy(cm->psy_name_buf, psy_default.name, 861 - PSY_NAME_MAX); 861 + strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); 862 862 } else { 863 863 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); 864 864 } ··· 892 894 POWER_SUPPLY_PROP_CURRENT_NOW; 893 895 cm->charger_psy.num_properties++; 894 896 } 895 - if (!desc->measure_battery_temp) { 896 - cm->charger_psy.properties[cm->charger_psy.num_properties] = 897 - POWER_SUPPLY_PROP_TEMP_AMBIENT; 898 - cm->charger_psy.num_properties++; 899 - } 897 + 900 898 if (desc->measure_battery_temp) { 901 899 cm->charger_psy.properties[cm->charger_psy.num_properties] = 902 900 POWER_SUPPLY_PROP_TEMP; 901 + cm->charger_psy.num_properties++; 902 + } else { 903 + cm->charger_psy.properties[cm->charger_psy.num_properties] = 904 + POWER_SUPPLY_PROP_TEMP_AMBIENT; 903 905 cm->charger_psy.num_properties++; 904 906 } 905 907 ··· 931 933 return 0; 932 934 933 935 err_chg_enable: 934 - if (desc->charger_regulators) 935 - regulator_bulk_free(desc->num_charger_regulators, 936 - desc->charger_regulators); 936 + regulator_bulk_free(desc->num_charger_regulators, 937 + desc->charger_regulators); 937 938 err_bulk_get: 938 939 power_supply_unregister(&cm->charger_psy); 939 940 err_register: ··· 958 961 list_del(&cm->entry); 959 962 mutex_unlock(&cm_list_mtx); 960 963 961 - if (desc->charger_regulators) 962 - regulator_bulk_free(desc->num_charger_regulators, 963 - desc->charger_regulators); 964 - 964 + regulator_bulk_free(desc->num_charger_regulators, 965 + desc->charger_regulators); 965 966 power_supply_unregister(&cm->charger_psy); 966 967 kfree(cm->charger_psy.properties); 967 968 kfree(cm->charger_stat); ··· 977 982 978 983 static int cm_suspend_prepare(struct device *dev) 979 984 { 980 - struct platform_device *pdev = container_of(dev, struct platform_device, 981 - dev); 982 - struct charger_manager *cm = platform_get_drvdata(pdev); 985 + struct charger_manager *cm = dev_get_drvdata(dev); 983 986 984 987 if (!cm_suspended) { 985 988 if (rtc_dev) { ··· 1013 1020 1014 1021 static void cm_suspend_complete(struct device *dev) 1015 1022 { 1016 - struct platform_device *pdev = container_of(dev, struct platform_device, 1017 - dev); 1018 - struct charger_manager *cm = platform_get_drvdata(pdev); 1023 + struct charger_manager *cm = dev_get_drvdata(dev); 1019 1024 1020 1025 if (cm_suspended) { 1021 1026 if (rtc_dev) {
+3 -12
drivers/power/da9052-battery.c
··· 612 612 if (ret) 613 613 goto err; 614 614 615 + platform_set_drvdata(pdev, bat); 615 616 return 0; 616 617 617 618 err: ··· 634 633 free_irq(bat->da9052->irq_base + irq, bat); 635 634 } 636 635 power_supply_unregister(&bat->psy); 636 + kfree(bat); 637 637 638 638 return 0; 639 639 } ··· 647 645 .owner = THIS_MODULE, 648 646 }, 649 647 }; 650 - 651 - static int __init da9052_bat_init(void) 652 - { 653 - return platform_driver_register(&da9052_bat_driver); 654 - } 655 - module_init(da9052_bat_init); 656 - 657 - static void __exit da9052_bat_exit(void) 658 - { 659 - platform_driver_unregister(&da9052_bat_driver); 660 - } 661 - module_exit(da9052_bat_exit); 648 + module_platform_driver(da9052_bat_driver); 662 649 663 650 MODULE_DESCRIPTION("DA9052 BAT Device Driver"); 664 651 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
+1 -12
drivers/power/ds2782_battery.c
··· 403 403 .remove = ds278x_battery_remove, 404 404 .id_table = ds278x_id, 405 405 }; 406 - 407 - static int __init ds278x_init(void) 408 - { 409 - return i2c_add_driver(&ds278x_battery_driver); 410 - } 411 - module_init(ds278x_init); 412 - 413 - static void __exit ds278x_exit(void) 414 - { 415 - i2c_del_driver(&ds278x_battery_driver); 416 - } 417 - module_exit(ds278x_exit); 406 + module_i2c_driver(ds278x_battery_driver); 418 407 419 408 MODULE_AUTHOR("Ryan Mallon"); 420 409 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
+1
drivers/power/isp1704_charger.c
··· 480 480 481 481 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret); 482 482 483 + isp1704_charger_set_power(isp, 0); 483 484 return ret; 484 485 } 485 486
+67 -64
drivers/power/lp8727_charger.c
··· 1 1 /* 2 - * Driver for LP8727 Micro/Mini USB IC with intergrated charger 2 + * Driver for LP8727 Micro/Mini USB IC with integrated charger 3 3 * 4 + * Copyright (C) 2011 Texas Instruments 4 5 * Copyright (C) 2011 National Semiconductor 5 6 * 6 7 * This program is free software; you can redistribute it and/or modify ··· 26 25 #define INT1 0x4 27 26 #define INT2 0x5 28 27 #define STATUS1 0x6 29 - #define STATUS2 0x7 28 + #define STATUS2 0x7 30 29 #define CHGCTRL2 0x9 31 30 32 31 /* CTRL1 register */ ··· 92 91 enum lp8727_dev_id devid; 93 92 }; 94 93 95 - static int lp8727_i2c_read(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) 94 + static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) 96 95 { 97 96 s32 ret; 98 97 ··· 103 102 return (ret != len) ? -EIO : 0; 104 103 } 105 104 106 - static int lp8727_i2c_write(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len) 105 + static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data) 107 106 { 108 - s32 ret; 107 + return lp8727_read_bytes(pchg, reg, data, 1); 108 + } 109 + 110 + static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data) 111 + { 112 + int ret; 109 113 110 114 mutex_lock(&pchg->xfer_lock); 111 - ret = i2c_smbus_write_i2c_block_data(pchg->client, reg, len, data); 115 + ret = i2c_smbus_write_byte_data(pchg->client, reg, data); 112 116 mutex_unlock(&pchg->xfer_lock); 113 117 114 118 return ret; 115 - } 116 - 117 - static inline int lp8727_i2c_read_byte(struct lp8727_chg *pchg, u8 reg, 118 - u8 *data) 119 - { 120 - return lp8727_i2c_read(pchg, reg, data, 1); 121 - } 122 - 123 - static inline int lp8727_i2c_write_byte(struct lp8727_chg *pchg, u8 reg, 124 - u8 *data) 125 - { 126 - return lp8727_i2c_write(pchg, reg, data, 1); 127 119 } 128 120 129 121 static int lp8727_is_charger_attached(const char *name, int id) ··· 131 137 return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0; 132 138 } 133 139 134 - static void lp8727_init_device(struct lp8727_chg *pchg) 140 + static int lp8727_init_device(struct lp8727_chg *pchg) 135 141 { 136 142 u8 val; 143 + int ret; 137 144 138 145 val = ID200_EN | ADC_EN | CP_EN; 139 - if (lp8727_i2c_write_byte(pchg, CTRL1, &val)) 140 - dev_err(pchg->dev, "i2c write err : addr=0x%.2x\n", CTRL1); 146 + ret = lp8727_write_byte(pchg, CTRL1, val); 147 + if (ret) 148 + return ret; 141 149 142 150 val = INT_EN | CHGDET_EN; 143 - if (lp8727_i2c_write_byte(pchg, CTRL2, &val)) 144 - dev_err(pchg->dev, "i2c write err : addr=0x%.2x\n", CTRL2); 151 + ret = lp8727_write_byte(pchg, CTRL2, val); 152 + if (ret) 153 + return ret; 154 + 155 + return 0; 145 156 } 146 157 147 158 static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg) 148 159 { 149 160 u8 val; 150 - lp8727_i2c_read_byte(pchg, STATUS1, &val); 151 - return (val & DCPORT); 161 + lp8727_read_byte(pchg, STATUS1, &val); 162 + return val & DCPORT; 152 163 } 153 164 154 165 static int lp8727_is_usb_charger(struct lp8727_chg *pchg) 155 166 { 156 167 u8 val; 157 - lp8727_i2c_read_byte(pchg, STATUS1, &val); 158 - return (val & CHPORT); 168 + lp8727_read_byte(pchg, STATUS1, &val); 169 + return val & CHPORT; 159 170 } 160 171 161 172 static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw) 162 173 { 163 - u8 val = sw; 164 - lp8727_i2c_write_byte(pchg, SWCTRL, &val); 174 + lp8727_write_byte(pchg, SWCTRL, sw); 165 175 } 166 176 167 177 static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin) ··· 205 207 { 206 208 u8 val; 207 209 208 - lp8727_i2c_read_byte(pchg, CTRL2, &val); 210 + lp8727_read_byte(pchg, CTRL2, &val); 209 211 val |= CHGDET_EN; 210 - lp8727_i2c_write_byte(pchg, CTRL2, &val); 212 + lp8727_write_byte(pchg, CTRL2, val); 211 213 } 212 214 213 215 static void lp8727_delayed_func(struct work_struct *_work) ··· 216 218 struct lp8727_chg *pchg = 217 219 container_of(_work, struct lp8727_chg, work.work); 218 220 219 - if (lp8727_i2c_read(pchg, INT1, intstat, 2)) { 221 + if (lp8727_read_bytes(pchg, INT1, intstat, 2)) { 220 222 dev_err(pchg->dev, "can not read INT registers\n"); 221 223 return; 222 224 } ··· 242 244 return IRQ_HANDLED; 243 245 } 244 246 245 - static void lp8727_intr_config(struct lp8727_chg *pchg) 247 + static int lp8727_intr_config(struct lp8727_chg *pchg) 246 248 { 247 249 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func); 248 250 249 251 pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd"); 250 - if (!pchg->irqthread) 252 + if (!pchg->irqthread) { 251 253 dev_err(pchg->dev, "can not create thread for lp8727\n"); 252 - 253 - if (request_threaded_irq(pchg->client->irq, 254 - NULL, 255 - lp8727_isr_func, 256 - IRQF_TRIGGER_FALLING, "lp8727_irq", pchg)) { 257 - dev_err(pchg->dev, "lp8727 irq can not be registered\n"); 254 + return -ENOMEM; 258 255 } 256 + 257 + return request_threaded_irq(pchg->client->irq, 258 + NULL, 259 + lp8727_isr_func, 260 + IRQF_TRIGGER_FALLING, 261 + "lp8727_irq", 262 + pchg); 259 263 } 260 264 261 265 static enum power_supply_property lp8727_charger_prop[] = { ··· 300 300 switch (psp) { 301 301 case POWER_SUPPLY_PROP_STATUS: 302 302 if (lp8727_is_charger_attached(psy->name, pchg->devid)) { 303 - lp8727_i2c_read_byte(pchg, STATUS1, &read); 303 + lp8727_read_byte(pchg, STATUS1, &read); 304 304 if (((read & CHGSTAT) >> 4) == EOC) 305 305 val->intval = POWER_SUPPLY_STATUS_FULL; 306 306 else ··· 310 310 } 311 311 break; 312 312 case POWER_SUPPLY_PROP_HEALTH: 313 - lp8727_i2c_read_byte(pchg, STATUS2, &read); 313 + lp8727_read_byte(pchg, STATUS2, &read); 314 314 read = (read & TEMP_STAT) >> 5; 315 315 if (read >= 0x1 && read <= 0x3) 316 316 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; ··· 351 351 eoc_level = pchg->chg_parm->eoc_level; 352 352 ichg = pchg->chg_parm->ichg; 353 353 val = (ichg << 4) | eoc_level; 354 - lp8727_i2c_write_byte(pchg, CHGCTRL2, &val); 354 + lp8727_write_byte(pchg, CHGCTRL2, val); 355 355 } 356 356 } 357 357 } ··· 439 439 440 440 mutex_init(&pchg->xfer_lock); 441 441 442 - lp8727_init_device(pchg); 443 - lp8727_intr_config(pchg); 442 + ret = lp8727_init_device(pchg); 443 + if (ret) { 444 + dev_err(pchg->dev, "i2c communication err: %d", ret); 445 + goto error; 446 + } 447 + 448 + ret = lp8727_intr_config(pchg); 449 + if (ret) { 450 + dev_err(pchg->dev, "irq handler err: %d", ret); 451 + goto error; 452 + } 444 453 445 454 ret = lp8727_register_psy(pchg); 446 - if (ret) 447 - dev_err(pchg->dev, 448 - "can not register power supplies. err=%d", ret); 455 + if (ret) { 456 + dev_err(pchg->dev, "power supplies register err: %d", ret); 457 + goto error; 458 + } 449 459 450 460 return 0; 461 + 462 + error: 463 + kfree(pchg); 464 + return ret; 451 465 } 452 466 453 467 static int __devexit lp8727_remove(struct i2c_client *cl) ··· 480 466 {"lp8727", 0}, 481 467 { } 482 468 }; 469 + MODULE_DEVICE_TABLE(i2c, lp8727_ids); 483 470 484 471 static struct i2c_driver lp8727_driver = { 485 472 .driver = { ··· 490 475 .remove = __devexit_p(lp8727_remove), 491 476 .id_table = lp8727_ids, 492 477 }; 478 + module_i2c_driver(lp8727_driver); 493 479 494 - static int __init lp8727_init(void) 495 - { 496 - return i2c_add_driver(&lp8727_driver); 497 - } 498 - 499 - static void __exit lp8727_exit(void) 500 - { 501 - i2c_del_driver(&lp8727_driver); 502 - } 503 - 504 - module_init(lp8727_init); 505 - module_exit(lp8727_exit); 506 - 507 - MODULE_DESCRIPTION("National Semiconductor LP8727 charger driver"); 508 - MODULE_AUTHOR 509 - ("Woogyom Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>"); 480 + MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver"); 481 + MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, " 482 + "Daniel Jeong <daniel.jeong@ti.com>"); 510 483 MODULE_LICENSE("GPL");
+1 -12
drivers/power/max17040_battery.c
··· 290 290 .resume = max17040_resume, 291 291 .id_table = max17040_id, 292 292 }; 293 - 294 - static int __init max17040_init(void) 295 - { 296 - return i2c_add_driver(&max17040_i2c_driver); 297 - } 298 - module_init(max17040_init); 299 - 300 - static void __exit max17040_exit(void) 301 - { 302 - i2c_del_driver(&max17040_i2c_driver); 303 - } 304 - module_exit(max17040_exit); 293 + module_i2c_driver(max17040_i2c_driver); 305 294 306 295 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>"); 307 296 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
+477 -31
drivers/power/max17042_battery.c
··· 26 26 #include <linux/module.h> 27 27 #include <linux/slab.h> 28 28 #include <linux/i2c.h> 29 + #include <linux/delay.h> 30 + #include <linux/interrupt.h> 29 31 #include <linux/mod_devicetable.h> 30 32 #include <linux/power_supply.h> 31 33 #include <linux/power/max17042_battery.h> 34 + #include <linux/of.h> 35 + 36 + /* Status register bits */ 37 + #define STATUS_POR_BIT (1 << 1) 38 + #define STATUS_BST_BIT (1 << 3) 39 + #define STATUS_VMN_BIT (1 << 8) 40 + #define STATUS_TMN_BIT (1 << 9) 41 + #define STATUS_SMN_BIT (1 << 10) 42 + #define STATUS_BI_BIT (1 << 11) 43 + #define STATUS_VMX_BIT (1 << 12) 44 + #define STATUS_TMX_BIT (1 << 13) 45 + #define STATUS_SMX_BIT (1 << 14) 46 + #define STATUS_BR_BIT (1 << 15) 47 + 48 + /* Interrupt mask bits */ 49 + #define CONFIG_ALRT_BIT_ENBL (1 << 2) 50 + #define STATUS_INTR_SOCMIN_BIT (1 << 10) 51 + #define STATUS_INTR_SOCMAX_BIT (1 << 14) 52 + 53 + #define VFSOC0_LOCK 0x0000 54 + #define VFSOC0_UNLOCK 0x0080 55 + #define MODEL_UNLOCK1 0X0059 56 + #define MODEL_UNLOCK2 0X00C4 57 + #define MODEL_LOCK1 0X0000 58 + #define MODEL_LOCK2 0X0000 59 + 60 + #define dQ_ACC_DIV 0x4 61 + #define dP_ACC_100 0x1900 62 + #define dP_ACC_200 0x3200 32 63 33 64 struct max17042_chip { 34 65 struct i2c_client *client; 35 66 struct power_supply battery; 36 67 struct max17042_platform_data *pdata; 68 + struct work_struct work; 69 + int init_complete; 37 70 }; 38 71 39 72 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value) ··· 120 87 struct max17042_chip, battery); 121 88 int ret; 122 89 90 + if (!chip->init_complete) 91 + return -EAGAIN; 92 + 123 93 switch (psp) { 124 94 case POWER_SUPPLY_PROP_PRESENT: 125 95 ret = max17042_read_reg(chip->client, MAX17042_STATUS); ··· 172 136 val->intval = ret * 625 / 8; 173 137 break; 174 138 case POWER_SUPPLY_PROP_CAPACITY: 175 - ret = max17042_read_reg(chip->client, MAX17042_SOC); 139 + ret = max17042_read_reg(chip->client, MAX17042_RepSOC); 176 140 if (ret < 0) 177 141 return ret; 178 142 179 143 val->intval = ret >> 8; 180 144 break; 181 145 case POWER_SUPPLY_PROP_CHARGE_FULL: 182 - ret = max17042_read_reg(chip->client, MAX17042_RepSOC); 146 + ret = max17042_read_reg(chip->client, MAX17042_FullCAP); 183 147 if (ret < 0) 184 148 return ret; 185 149 186 - if ((ret >> 8) >= MAX17042_BATTERY_FULL) 187 - val->intval = 1; 188 - else if (ret >= 0) 189 - val->intval = 0; 150 + val->intval = ret * 1000 / 2; 190 151 break; 191 152 case POWER_SUPPLY_PROP_TEMP: 192 153 ret = max17042_read_reg(chip->client, MAX17042_TEMP); ··· 243 210 return 0; 244 211 } 245 212 213 + static int max17042_write_verify_reg(struct i2c_client *client, 214 + u8 reg, u16 value) 215 + { 216 + int retries = 8; 217 + int ret; 218 + u16 read_value; 219 + 220 + do { 221 + ret = i2c_smbus_write_word_data(client, reg, value); 222 + read_value = max17042_read_reg(client, reg); 223 + if (read_value != value) { 224 + ret = -EIO; 225 + retries--; 226 + } 227 + } while (retries && read_value != value); 228 + 229 + if (ret < 0) 230 + dev_err(&client->dev, "%s: err %d\n", __func__, ret); 231 + 232 + return ret; 233 + } 234 + 235 + static inline void max17042_override_por( 236 + struct i2c_client *client, u8 reg, u16 value) 237 + { 238 + if (value) 239 + max17042_write_reg(client, reg, value); 240 + } 241 + 242 + static inline void max10742_unlock_model(struct max17042_chip *chip) 243 + { 244 + struct i2c_client *client = chip->client; 245 + max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1); 246 + max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2); 247 + } 248 + 249 + static inline void max10742_lock_model(struct max17042_chip *chip) 250 + { 251 + struct i2c_client *client = chip->client; 252 + max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1); 253 + max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2); 254 + } 255 + 256 + static inline void max17042_write_model_data(struct max17042_chip *chip, 257 + u8 addr, int size) 258 + { 259 + struct i2c_client *client = chip->client; 260 + int i; 261 + for (i = 0; i < size; i++) 262 + max17042_write_reg(client, addr + i, 263 + chip->pdata->config_data->cell_char_tbl[i]); 264 + } 265 + 266 + static inline void max17042_read_model_data(struct max17042_chip *chip, 267 + u8 addr, u16 *data, int size) 268 + { 269 + struct i2c_client *client = chip->client; 270 + int i; 271 + 272 + for (i = 0; i < size; i++) 273 + data[i] = max17042_read_reg(client, addr + i); 274 + } 275 + 276 + static inline int max17042_model_data_compare(struct max17042_chip *chip, 277 + u16 *data1, u16 *data2, int size) 278 + { 279 + int i; 280 + 281 + if (memcmp(data1, data2, size)) { 282 + dev_err(&chip->client->dev, "%s compare failed\n", __func__); 283 + for (i = 0; i < size; i++) 284 + dev_info(&chip->client->dev, "0x%x, 0x%x", 285 + data1[i], data2[i]); 286 + dev_info(&chip->client->dev, "\n"); 287 + return -EINVAL; 288 + } 289 + return 0; 290 + } 291 + 292 + static int max17042_init_model(struct max17042_chip *chip) 293 + { 294 + int ret; 295 + int table_size = 296 + sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16); 297 + u16 *temp_data; 298 + 299 + temp_data = kzalloc(table_size, GFP_KERNEL); 300 + if (!temp_data) 301 + return -ENOMEM; 302 + 303 + max10742_unlock_model(chip); 304 + max17042_write_model_data(chip, MAX17042_MODELChrTbl, 305 + table_size); 306 + max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, 307 + table_size); 308 + 309 + ret = max17042_model_data_compare( 310 + chip, 311 + chip->pdata->config_data->cell_char_tbl, 312 + temp_data, 313 + table_size); 314 + 315 + max10742_lock_model(chip); 316 + kfree(temp_data); 317 + 318 + return ret; 319 + } 320 + 321 + static int max17042_verify_model_lock(struct max17042_chip *chip) 322 + { 323 + int i; 324 + int table_size = 325 + sizeof(chip->pdata->config_data->cell_char_tbl); 326 + u16 *temp_data; 327 + int ret = 0; 328 + 329 + temp_data = kzalloc(table_size, GFP_KERNEL); 330 + if (!temp_data) 331 + return -ENOMEM; 332 + 333 + max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, 334 + table_size); 335 + for (i = 0; i < table_size; i++) 336 + if (temp_data[i]) 337 + ret = -EINVAL; 338 + 339 + kfree(temp_data); 340 + return ret; 341 + } 342 + 343 + static void max17042_write_config_regs(struct max17042_chip *chip) 344 + { 345 + struct max17042_config_data *config = chip->pdata->config_data; 346 + 347 + max17042_write_reg(chip->client, MAX17042_CONFIG, config->config); 348 + max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg); 349 + max17042_write_reg(chip->client, MAX17042_FilterCFG, 350 + config->filter_cfg); 351 + max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg); 352 + } 353 + 354 + static void max17042_write_custom_regs(struct max17042_chip *chip) 355 + { 356 + struct max17042_config_data *config = chip->pdata->config_data; 357 + 358 + max17042_write_verify_reg(chip->client, MAX17042_RCOMP0, 359 + config->rcomp0); 360 + max17042_write_verify_reg(chip->client, MAX17042_TempCo, 361 + config->tcompc0); 362 + max17042_write_reg(chip->client, MAX17042_EmptyTempCo, 363 + config->empty_tempco); 364 + max17042_write_verify_reg(chip->client, MAX17042_K_empty0, 365 + config->kempty0); 366 + max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm, 367 + config->ichgt_term); 368 + } 369 + 370 + static void max17042_update_capacity_regs(struct max17042_chip *chip) 371 + { 372 + struct max17042_config_data *config = chip->pdata->config_data; 373 + 374 + max17042_write_verify_reg(chip->client, MAX17042_FullCAP, 375 + config->fullcap); 376 + max17042_write_reg(chip->client, MAX17042_DesignCap, 377 + config->design_cap); 378 + max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, 379 + config->fullcapnom); 380 + } 381 + 382 + static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) 383 + { 384 + u16 vfSoc; 385 + 386 + vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC); 387 + max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK); 388 + max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc); 389 + max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK); 390 + } 391 + 392 + static void max17042_load_new_capacity_params(struct max17042_chip *chip) 393 + { 394 + u16 full_cap0, rep_cap, dq_acc, vfSoc; 395 + u32 rem_cap; 396 + 397 + struct max17042_config_data *config = chip->pdata->config_data; 398 + 399 + full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0); 400 + vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC); 401 + 402 + /* fg_vfSoc needs to shifted by 8 bits to get the 403 + * perc in 1% accuracy, to get the right rem_cap multiply 404 + * full_cap0, fg_vfSoc and devide by 100 405 + */ 406 + rem_cap = ((vfSoc >> 8) * full_cap0) / 100; 407 + max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap); 408 + 409 + rep_cap = (u16)rem_cap; 410 + max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap); 411 + 412 + /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ 413 + dq_acc = config->fullcap / dQ_ACC_DIV; 414 + max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc); 415 + max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200); 416 + 417 + max17042_write_verify_reg(chip->client, MAX17042_FullCAP, 418 + config->fullcap); 419 + max17042_write_reg(chip->client, MAX17042_DesignCap, 420 + config->design_cap); 421 + max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, 422 + config->fullcapnom); 423 + } 424 + 425 + /* 426 + * Block write all the override values coming from platform data. 427 + * This function MUST be called before the POR initialization proceedure 428 + * specified by maxim. 429 + */ 430 + static inline void max17042_override_por_values(struct max17042_chip *chip) 431 + { 432 + struct i2c_client *client = chip->client; 433 + struct max17042_config_data *config = chip->pdata->config_data; 434 + 435 + max17042_override_por(client, MAX17042_TGAIN, config->tgain); 436 + max17042_override_por(client, MAx17042_TOFF, config->toff); 437 + max17042_override_por(client, MAX17042_CGAIN, config->cgain); 438 + max17042_override_por(client, MAX17042_COFF, config->coff); 439 + 440 + max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh); 441 + max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh); 442 + max17042_override_por(client, MAX17042_SALRT_Th, 443 + config->soc_alrt_thresh); 444 + max17042_override_por(client, MAX17042_CONFIG, config->config); 445 + max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer); 446 + 447 + max17042_override_por(client, MAX17042_DesignCap, config->design_cap); 448 + max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term); 449 + 450 + max17042_override_por(client, MAX17042_AtRate, config->at_rate); 451 + max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg); 452 + max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg); 453 + max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg); 454 + max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg); 455 + max17042_override_por(client, MAX17042_MaskSOC, config->masksoc); 456 + 457 + max17042_override_por(client, MAX17042_FullCAP, config->fullcap); 458 + max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom); 459 + max17042_override_por(client, MAX17042_SOC_empty, config->socempty); 460 + max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty); 461 + max17042_override_por(client, MAX17042_dQacc, config->dqacc); 462 + max17042_override_por(client, MAX17042_dPacc, config->dpacc); 463 + 464 + max17042_override_por(client, MAX17042_V_empty, config->vempty); 465 + max17042_override_por(client, MAX17042_TempNom, config->temp_nom); 466 + max17042_override_por(client, MAX17042_TempLim, config->temp_lim); 467 + max17042_override_por(client, MAX17042_FCTC, config->fctc); 468 + max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0); 469 + max17042_override_por(client, MAX17042_TempCo, config->tcompc0); 470 + max17042_override_por(client, MAX17042_EmptyTempCo, 471 + config->empty_tempco); 472 + max17042_override_por(client, MAX17042_K_empty0, config->kempty0); 473 + } 474 + 475 + static int max17042_init_chip(struct max17042_chip *chip) 476 + { 477 + int ret; 478 + int val; 479 + 480 + max17042_override_por_values(chip); 481 + /* After Power up, the MAX17042 requires 500mS in order 482 + * to perform signal debouncing and initial SOC reporting 483 + */ 484 + msleep(500); 485 + 486 + /* Initialize configaration */ 487 + max17042_write_config_regs(chip); 488 + 489 + /* write cell characterization data */ 490 + ret = max17042_init_model(chip); 491 + if (ret) { 492 + dev_err(&chip->client->dev, "%s init failed\n", 493 + __func__); 494 + return -EIO; 495 + } 496 + max17042_verify_model_lock(chip); 497 + if (ret) { 498 + dev_err(&chip->client->dev, "%s lock verify failed\n", 499 + __func__); 500 + return -EIO; 501 + } 502 + /* write custom parameters */ 503 + max17042_write_custom_regs(chip); 504 + 505 + /* update capacity params */ 506 + max17042_update_capacity_regs(chip); 507 + 508 + /* delay must be atleast 350mS to allow VFSOC 509 + * to be calculated from the new configuration 510 + */ 511 + msleep(350); 512 + 513 + /* reset vfsoc0 reg */ 514 + max17042_reset_vfsoc0_reg(chip); 515 + 516 + /* load new capacity params */ 517 + max17042_load_new_capacity_params(chip); 518 + 519 + /* Init complete, Clear the POR bit */ 520 + val = max17042_read_reg(chip->client, MAX17042_STATUS); 521 + max17042_write_reg(chip->client, MAX17042_STATUS, 522 + val & (~STATUS_POR_BIT)); 523 + return 0; 524 + } 525 + 526 + static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off) 527 + { 528 + u16 soc, soc_tr; 529 + 530 + /* program interrupt thesholds such that we should 531 + * get interrupt for every 'off' perc change in the soc 532 + */ 533 + soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8; 534 + soc_tr = (soc + off) << 8; 535 + soc_tr |= (soc - off); 536 + max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr); 537 + } 538 + 539 + static irqreturn_t max17042_thread_handler(int id, void *dev) 540 + { 541 + struct max17042_chip *chip = dev; 542 + u16 val; 543 + 544 + val = max17042_read_reg(chip->client, MAX17042_STATUS); 545 + if ((val & STATUS_INTR_SOCMIN_BIT) || 546 + (val & STATUS_INTR_SOCMAX_BIT)) { 547 + dev_info(&chip->client->dev, "SOC threshold INTR\n"); 548 + max17042_set_soc_threshold(chip, 1); 549 + } 550 + 551 + power_supply_changed(&chip->battery); 552 + return IRQ_HANDLED; 553 + } 554 + 555 + static void max17042_init_worker(struct work_struct *work) 556 + { 557 + struct max17042_chip *chip = container_of(work, 558 + struct max17042_chip, work); 559 + int ret; 560 + 561 + /* Initialize registers according to values from the platform data */ 562 + if (chip->pdata->enable_por_init && chip->pdata->config_data) { 563 + ret = max17042_init_chip(chip); 564 + if (ret) 565 + return; 566 + } 567 + 568 + chip->init_complete = 1; 569 + } 570 + 571 + #ifdef CONFIG_OF 572 + static struct max17042_platform_data * 573 + max17042_get_pdata(struct device *dev) 574 + { 575 + struct device_node *np = dev->of_node; 576 + u32 prop; 577 + struct max17042_platform_data *pdata; 578 + 579 + if (!np) 580 + return dev->platform_data; 581 + 582 + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 583 + if (!pdata) 584 + return NULL; 585 + 586 + /* 587 + * Require current sense resistor value to be specified for 588 + * current-sense functionality to be enabled at all. 589 + */ 590 + if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { 591 + pdata->r_sns = prop; 592 + pdata->enable_current_sense = true; 593 + } 594 + 595 + return pdata; 596 + } 597 + #else 598 + static struct max17042_platform_data * 599 + max17042_get_pdata(struct device *dev) 600 + { 601 + return dev->platform_data; 602 + } 603 + #endif 604 + 246 605 static int __devinit max17042_probe(struct i2c_client *client, 247 606 const struct i2c_device_id *id) 248 607 { 249 608 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 250 609 struct max17042_chip *chip; 251 610 int ret; 611 + int reg; 252 612 253 613 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) 254 614 return -EIO; 255 615 256 - chip = kzalloc(sizeof(*chip), GFP_KERNEL); 616 + chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 257 617 if (!chip) 258 618 return -ENOMEM; 259 619 260 620 chip->client = client; 261 - chip->pdata = client->dev.platform_data; 621 + chip->pdata = max17042_get_pdata(&client->dev); 622 + if (!chip->pdata) { 623 + dev_err(&client->dev, "no platform data provided\n"); 624 + return -EINVAL; 625 + } 262 626 263 627 i2c_set_clientdata(client, chip); 264 628 ··· 673 243 if (chip->pdata->r_sns == 0) 674 244 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; 675 245 676 - ret = power_supply_register(&client->dev, &chip->battery); 677 - if (ret) { 678 - dev_err(&client->dev, "failed: power supply register\n"); 679 - kfree(chip); 680 - return ret; 681 - } 682 - 683 - /* Initialize registers according to values from the platform data */ 684 246 if (chip->pdata->init_data) 685 247 max17042_set_reg(client, chip->pdata->init_data, 686 - chip->pdata->num_init_data); 248 + chip->pdata->num_init_data); 687 249 688 250 if (!chip->pdata->enable_current_sense) { 689 251 max17042_write_reg(client, MAX17042_CGAIN, 0x0000); ··· 683 261 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007); 684 262 } 685 263 686 - return 0; 264 + if (client->irq) { 265 + ret = request_threaded_irq(client->irq, NULL, 266 + max17042_thread_handler, 267 + IRQF_TRIGGER_FALLING, 268 + chip->battery.name, chip); 269 + if (!ret) { 270 + reg = max17042_read_reg(client, MAX17042_CONFIG); 271 + reg |= CONFIG_ALRT_BIT_ENBL; 272 + max17042_write_reg(client, MAX17042_CONFIG, reg); 273 + max17042_set_soc_threshold(chip, 1); 274 + } else 275 + dev_err(&client->dev, "%s(): cannot get IRQ\n", 276 + __func__); 277 + } 278 + 279 + reg = max17042_read_reg(chip->client, MAX17042_STATUS); 280 + 281 + if (reg & STATUS_POR_BIT) { 282 + INIT_WORK(&chip->work, max17042_init_worker); 283 + schedule_work(&chip->work); 284 + } else { 285 + chip->init_complete = 1; 286 + } 287 + 288 + ret = power_supply_register(&client->dev, &chip->battery); 289 + if (ret) 290 + dev_err(&client->dev, "failed: power supply register\n"); 291 + return ret; 687 292 } 688 293 689 294 static int __devexit max17042_remove(struct i2c_client *client) ··· 718 269 struct max17042_chip *chip = i2c_get_clientdata(client); 719 270 720 271 power_supply_unregister(&chip->battery); 721 - kfree(chip); 722 272 return 0; 723 273 } 274 + 275 + #ifdef CONFIG_OF 276 + static const struct of_device_id max17042_dt_match[] = { 277 + { .compatible = "maxim,max17042" }, 278 + { }, 279 + }; 280 + MODULE_DEVICE_TABLE(of, max17042_dt_match); 281 + #endif 724 282 725 283 static const struct i2c_device_id max17042_id[] = { 726 284 { "max17042", 0 }, ··· 738 282 static struct i2c_driver max17042_i2c_driver = { 739 283 .driver = { 740 284 .name = "max17042", 285 + .of_match_table = of_match_ptr(max17042_dt_match), 741 286 }, 742 287 .probe = max17042_probe, 743 288 .remove = __devexit_p(max17042_remove), 744 289 .id_table = max17042_id, 745 290 }; 746 - 747 - static int __init max17042_init(void) 748 - { 749 - return i2c_add_driver(&max17042_i2c_driver); 750 - } 751 - module_init(max17042_init); 752 - 753 - static void __exit max17042_exit(void) 754 - { 755 - i2c_del_driver(&max17042_i2c_driver); 756 - } 757 - module_exit(max17042_exit); 291 + module_i2c_driver(max17042_i2c_driver); 758 292 759 293 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 760 294 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
+1 -12
drivers/power/sbs-battery.c
··· 852 852 .of_match_table = sbs_dt_ids, 853 853 }, 854 854 }; 855 - 856 - static int __init sbs_battery_init(void) 857 - { 858 - return i2c_add_driver(&sbs_battery_driver); 859 - } 860 - module_init(sbs_battery_init); 861 - 862 - static void __exit sbs_battery_exit(void) 863 - { 864 - i2c_del_driver(&sbs_battery_driver); 865 - } 866 - module_exit(sbs_battery_exit); 855 + module_i2c_driver(sbs_battery_driver); 867 856 868 857 MODULE_DESCRIPTION("SBS battery monitor driver"); 869 858 MODULE_LICENSE("GPL");
+1294
drivers/power/smb347-charger.c
··· 1 + /* 2 + * Summit Microelectronics SMB347 Battery Charger Driver 3 + * 4 + * Copyright (C) 2011, Intel Corporation 5 + * 6 + * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> 7 + * Mika Westerberg <mika.westerberg@linux.intel.com> 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License version 2 as 11 + * published by the Free Software Foundation. 12 + */ 13 + 14 + #include <linux/debugfs.h> 15 + #include <linux/gpio.h> 16 + #include <linux/kernel.h> 17 + #include <linux/module.h> 18 + #include <linux/init.h> 19 + #include <linux/interrupt.h> 20 + #include <linux/i2c.h> 21 + #include <linux/mutex.h> 22 + #include <linux/power_supply.h> 23 + #include <linux/power/smb347-charger.h> 24 + #include <linux/seq_file.h> 25 + 26 + /* 27 + * Configuration registers. These are mirrored to volatile RAM and can be 28 + * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be 29 + * reloaded from non-volatile registers after POR. 30 + */ 31 + #define CFG_CHARGE_CURRENT 0x00 32 + #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0 33 + #define CFG_CHARGE_CURRENT_FCC_SHIFT 5 34 + #define CFG_CHARGE_CURRENT_PCC_MASK 0x18 35 + #define CFG_CHARGE_CURRENT_PCC_SHIFT 3 36 + #define CFG_CHARGE_CURRENT_TC_MASK 0x07 37 + #define CFG_CURRENT_LIMIT 0x01 38 + #define CFG_CURRENT_LIMIT_DC_MASK 0xf0 39 + #define CFG_CURRENT_LIMIT_DC_SHIFT 4 40 + #define CFG_CURRENT_LIMIT_USB_MASK 0x0f 41 + #define CFG_FLOAT_VOLTAGE 0x03 42 + #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 43 + #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 44 + #define CFG_STAT 0x05 45 + #define CFG_STAT_DISABLED BIT(5) 46 + #define CFG_STAT_ACTIVE_HIGH BIT(7) 47 + #define CFG_PIN 0x06 48 + #define CFG_PIN_EN_CTRL_MASK 0x60 49 + #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40 50 + #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60 51 + #define CFG_PIN_EN_APSD_IRQ BIT(1) 52 + #define CFG_PIN_EN_CHARGER_ERROR BIT(2) 53 + #define CFG_THERM 0x07 54 + #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03 55 + #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0 56 + #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c 57 + #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2 58 + #define CFG_THERM_MONITOR_DISABLED BIT(4) 59 + #define CFG_SYSOK 0x08 60 + #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2) 61 + #define CFG_OTHER 0x09 62 + #define CFG_OTHER_RID_MASK 0xc0 63 + #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0 64 + #define CFG_OTG 0x0a 65 + #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30 66 + #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4 67 + #define CFG_OTG_CC_COMPENSATION_MASK 0xc0 68 + #define CFG_OTG_CC_COMPENSATION_SHIFT 6 69 + #define CFG_TEMP_LIMIT 0x0b 70 + #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03 71 + #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0 72 + #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c 73 + #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2 74 + #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30 75 + #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4 76 + #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0 77 + #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6 78 + #define CFG_FAULT_IRQ 0x0c 79 + #define CFG_FAULT_IRQ_DCIN_UV BIT(2) 80 + #define CFG_STATUS_IRQ 0x0d 81 + #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4) 82 + #define CFG_ADDRESS 0x0e 83 + 84 + /* Command registers */ 85 + #define CMD_A 0x30 86 + #define CMD_A_CHG_ENABLED BIT(1) 87 + #define CMD_A_SUSPEND_ENABLED BIT(2) 88 + #define CMD_A_ALLOW_WRITE BIT(7) 89 + #define CMD_B 0x31 90 + #define CMD_C 0x33 91 + 92 + /* Interrupt Status registers */ 93 + #define IRQSTAT_A 0x35 94 + #define IRQSTAT_C 0x37 95 + #define IRQSTAT_C_TERMINATION_STAT BIT(0) 96 + #define IRQSTAT_C_TERMINATION_IRQ BIT(1) 97 + #define IRQSTAT_C_TAPER_IRQ BIT(3) 98 + #define IRQSTAT_E 0x39 99 + #define IRQSTAT_E_USBIN_UV_STAT BIT(0) 100 + #define IRQSTAT_E_USBIN_UV_IRQ BIT(1) 101 + #define IRQSTAT_E_DCIN_UV_STAT BIT(4) 102 + #define IRQSTAT_E_DCIN_UV_IRQ BIT(5) 103 + #define IRQSTAT_F 0x3a 104 + 105 + /* Status registers */ 106 + #define STAT_A 0x3b 107 + #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f 108 + #define STAT_B 0x3c 109 + #define STAT_C 0x3d 110 + #define STAT_C_CHG_ENABLED BIT(0) 111 + #define STAT_C_CHG_MASK 0x06 112 + #define STAT_C_CHG_SHIFT 1 113 + #define STAT_C_CHARGER_ERROR BIT(6) 114 + #define STAT_E 0x3f 115 + 116 + /** 117 + * struct smb347_charger - smb347 charger instance 118 + * @lock: protects concurrent access to online variables 119 + * @client: pointer to i2c client 120 + * @mains: power_supply instance for AC/DC power 121 + * @usb: power_supply instance for USB power 122 + * @battery: power_supply instance for battery 123 + * @mains_online: is AC/DC input connected 124 + * @usb_online: is USB input connected 125 + * @charging_enabled: is charging enabled 126 + * @dentry: for debugfs 127 + * @pdata: pointer to platform data 128 + */ 129 + struct smb347_charger { 130 + struct mutex lock; 131 + struct i2c_client *client; 132 + struct power_supply mains; 133 + struct power_supply usb; 134 + struct power_supply battery; 135 + bool mains_online; 136 + bool usb_online; 137 + bool charging_enabled; 138 + struct dentry *dentry; 139 + const struct smb347_charger_platform_data *pdata; 140 + }; 141 + 142 + /* Fast charge current in uA */ 143 + static const unsigned int fcc_tbl[] = { 144 + 700000, 145 + 900000, 146 + 1200000, 147 + 1500000, 148 + 1800000, 149 + 2000000, 150 + 2200000, 151 + 2500000, 152 + }; 153 + 154 + /* Pre-charge current in uA */ 155 + static const unsigned int pcc_tbl[] = { 156 + 100000, 157 + 150000, 158 + 200000, 159 + 250000, 160 + }; 161 + 162 + /* Termination current in uA */ 163 + static const unsigned int tc_tbl[] = { 164 + 37500, 165 + 50000, 166 + 100000, 167 + 150000, 168 + 200000, 169 + 250000, 170 + 500000, 171 + 600000, 172 + }; 173 + 174 + /* Input current limit in uA */ 175 + static const unsigned int icl_tbl[] = { 176 + 300000, 177 + 500000, 178 + 700000, 179 + 900000, 180 + 1200000, 181 + 1500000, 182 + 1800000, 183 + 2000000, 184 + 2200000, 185 + 2500000, 186 + }; 187 + 188 + /* Charge current compensation in uA */ 189 + static const unsigned int ccc_tbl[] = { 190 + 250000, 191 + 700000, 192 + 900000, 193 + 1200000, 194 + }; 195 + 196 + /* Convert register value to current using lookup table */ 197 + static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val) 198 + { 199 + if (val >= size) 200 + return -EINVAL; 201 + return tbl[val]; 202 + } 203 + 204 + /* Convert current to register value using lookup table */ 205 + static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) 206 + { 207 + size_t i; 208 + 209 + for (i = 0; i < size; i++) 210 + if (val < tbl[i]) 211 + break; 212 + return i > 0 ? i - 1 : -EINVAL; 213 + } 214 + 215 + static int smb347_read(struct smb347_charger *smb, u8 reg) 216 + { 217 + int ret; 218 + 219 + ret = i2c_smbus_read_byte_data(smb->client, reg); 220 + if (ret < 0) 221 + dev_warn(&smb->client->dev, "failed to read reg 0x%x: %d\n", 222 + reg, ret); 223 + return ret; 224 + } 225 + 226 + static int smb347_write(struct smb347_charger *smb, u8 reg, u8 val) 227 + { 228 + int ret; 229 + 230 + ret = i2c_smbus_write_byte_data(smb->client, reg, val); 231 + if (ret < 0) 232 + dev_warn(&smb->client->dev, "failed to write reg 0x%x: %d\n", 233 + reg, ret); 234 + return ret; 235 + } 236 + 237 + /** 238 + * smb347_update_status - updates the charging status 239 + * @smb: pointer to smb347 charger instance 240 + * 241 + * Function checks status of the charging and updates internal state 242 + * accordingly. Returns %0 if there is no change in status, %1 if the 243 + * status has changed and negative errno in case of failure. 244 + */ 245 + static int smb347_update_status(struct smb347_charger *smb) 246 + { 247 + bool usb = false; 248 + bool dc = false; 249 + int ret; 250 + 251 + ret = smb347_read(smb, IRQSTAT_E); 252 + if (ret < 0) 253 + return ret; 254 + 255 + /* 256 + * Dc and usb are set depending on whether they are enabled in 257 + * platform data _and_ whether corresponding undervoltage is set. 258 + */ 259 + if (smb->pdata->use_mains) 260 + dc = !(ret & IRQSTAT_E_DCIN_UV_STAT); 261 + if (smb->pdata->use_usb) 262 + usb = !(ret & IRQSTAT_E_USBIN_UV_STAT); 263 + 264 + mutex_lock(&smb->lock); 265 + ret = smb->mains_online != dc || smb->usb_online != usb; 266 + smb->mains_online = dc; 267 + smb->usb_online = usb; 268 + mutex_unlock(&smb->lock); 269 + 270 + return ret; 271 + } 272 + 273 + /* 274 + * smb347_is_online - returns whether input power source is connected 275 + * @smb: pointer to smb347 charger instance 276 + * 277 + * Returns %true if input power source is connected. Note that this is 278 + * dependent on what platform has configured for usable power sources. For 279 + * example if USB is disabled, this will return %false even if the USB 280 + * cable is connected. 281 + */ 282 + static bool smb347_is_online(struct smb347_charger *smb) 283 + { 284 + bool ret; 285 + 286 + mutex_lock(&smb->lock); 287 + ret = smb->usb_online || smb->mains_online; 288 + mutex_unlock(&smb->lock); 289 + 290 + return ret; 291 + } 292 + 293 + /** 294 + * smb347_charging_status - returns status of charging 295 + * @smb: pointer to smb347 charger instance 296 + * 297 + * Function returns charging status. %0 means no charging is in progress, 298 + * %1 means pre-charging, %2 fast-charging and %3 taper-charging. 299 + */ 300 + static int smb347_charging_status(struct smb347_charger *smb) 301 + { 302 + int ret; 303 + 304 + if (!smb347_is_online(smb)) 305 + return 0; 306 + 307 + ret = smb347_read(smb, STAT_C); 308 + if (ret < 0) 309 + return 0; 310 + 311 + return (ret & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; 312 + } 313 + 314 + static int smb347_charging_set(struct smb347_charger *smb, bool enable) 315 + { 316 + int ret = 0; 317 + 318 + if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { 319 + dev_dbg(&smb->client->dev, 320 + "charging enable/disable in SW disabled\n"); 321 + return 0; 322 + } 323 + 324 + mutex_lock(&smb->lock); 325 + if (smb->charging_enabled != enable) { 326 + ret = smb347_read(smb, CMD_A); 327 + if (ret < 0) 328 + goto out; 329 + 330 + smb->charging_enabled = enable; 331 + 332 + if (enable) 333 + ret |= CMD_A_CHG_ENABLED; 334 + else 335 + ret &= ~CMD_A_CHG_ENABLED; 336 + 337 + ret = smb347_write(smb, CMD_A, ret); 338 + } 339 + out: 340 + mutex_unlock(&smb->lock); 341 + return ret; 342 + } 343 + 344 + static inline int smb347_charging_enable(struct smb347_charger *smb) 345 + { 346 + return smb347_charging_set(smb, true); 347 + } 348 + 349 + static inline int smb347_charging_disable(struct smb347_charger *smb) 350 + { 351 + return smb347_charging_set(smb, false); 352 + } 353 + 354 + static int smb347_update_online(struct smb347_charger *smb) 355 + { 356 + int ret; 357 + 358 + /* 359 + * Depending on whether valid power source is connected or not, we 360 + * disable or enable the charging. We do it manually because it 361 + * depends on how the platform has configured the valid inputs. 362 + */ 363 + if (smb347_is_online(smb)) { 364 + ret = smb347_charging_enable(smb); 365 + if (ret < 0) 366 + dev_err(&smb->client->dev, 367 + "failed to enable charging\n"); 368 + } else { 369 + ret = smb347_charging_disable(smb); 370 + if (ret < 0) 371 + dev_err(&smb->client->dev, 372 + "failed to disable charging\n"); 373 + } 374 + 375 + return ret; 376 + } 377 + 378 + static int smb347_set_charge_current(struct smb347_charger *smb) 379 + { 380 + int ret, val; 381 + 382 + ret = smb347_read(smb, CFG_CHARGE_CURRENT); 383 + if (ret < 0) 384 + return ret; 385 + 386 + if (smb->pdata->max_charge_current) { 387 + val = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), 388 + smb->pdata->max_charge_current); 389 + if (val < 0) 390 + return val; 391 + 392 + ret &= ~CFG_CHARGE_CURRENT_FCC_MASK; 393 + ret |= val << CFG_CHARGE_CURRENT_FCC_SHIFT; 394 + } 395 + 396 + if (smb->pdata->pre_charge_current) { 397 + val = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), 398 + smb->pdata->pre_charge_current); 399 + if (val < 0) 400 + return val; 401 + 402 + ret &= ~CFG_CHARGE_CURRENT_PCC_MASK; 403 + ret |= val << CFG_CHARGE_CURRENT_PCC_SHIFT; 404 + } 405 + 406 + if (smb->pdata->termination_current) { 407 + val = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), 408 + smb->pdata->termination_current); 409 + if (val < 0) 410 + return val; 411 + 412 + ret &= ~CFG_CHARGE_CURRENT_TC_MASK; 413 + ret |= val; 414 + } 415 + 416 + return smb347_write(smb, CFG_CHARGE_CURRENT, ret); 417 + } 418 + 419 + static int smb347_set_current_limits(struct smb347_charger *smb) 420 + { 421 + int ret, val; 422 + 423 + ret = smb347_read(smb, CFG_CURRENT_LIMIT); 424 + if (ret < 0) 425 + return ret; 426 + 427 + if (smb->pdata->mains_current_limit) { 428 + val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 429 + smb->pdata->mains_current_limit); 430 + if (val < 0) 431 + return val; 432 + 433 + ret &= ~CFG_CURRENT_LIMIT_DC_MASK; 434 + ret |= val << CFG_CURRENT_LIMIT_DC_SHIFT; 435 + } 436 + 437 + if (smb->pdata->usb_hc_current_limit) { 438 + val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 439 + smb->pdata->usb_hc_current_limit); 440 + if (val < 0) 441 + return val; 442 + 443 + ret &= ~CFG_CURRENT_LIMIT_USB_MASK; 444 + ret |= val; 445 + } 446 + 447 + return smb347_write(smb, CFG_CURRENT_LIMIT, ret); 448 + } 449 + 450 + static int smb347_set_voltage_limits(struct smb347_charger *smb) 451 + { 452 + int ret, val; 453 + 454 + ret = smb347_read(smb, CFG_FLOAT_VOLTAGE); 455 + if (ret < 0) 456 + return ret; 457 + 458 + if (smb->pdata->pre_to_fast_voltage) { 459 + val = smb->pdata->pre_to_fast_voltage; 460 + 461 + /* uV */ 462 + val = clamp_val(val, 2400000, 3000000) - 2400000; 463 + val /= 200000; 464 + 465 + ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK; 466 + ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT; 467 + } 468 + 469 + if (smb->pdata->max_charge_voltage) { 470 + val = smb->pdata->max_charge_voltage; 471 + 472 + /* uV */ 473 + val = clamp_val(val, 3500000, 4500000) - 3500000; 474 + val /= 20000; 475 + 476 + ret |= val; 477 + } 478 + 479 + return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret); 480 + } 481 + 482 + static int smb347_set_temp_limits(struct smb347_charger *smb) 483 + { 484 + bool enable_therm_monitor = false; 485 + int ret, val; 486 + 487 + if (smb->pdata->chip_temp_threshold) { 488 + val = smb->pdata->chip_temp_threshold; 489 + 490 + /* degree C */ 491 + val = clamp_val(val, 100, 130) - 100; 492 + val /= 10; 493 + 494 + ret = smb347_read(smb, CFG_OTG); 495 + if (ret < 0) 496 + return ret; 497 + 498 + ret &= ~CFG_OTG_TEMP_THRESHOLD_MASK; 499 + ret |= val << CFG_OTG_TEMP_THRESHOLD_SHIFT; 500 + 501 + ret = smb347_write(smb, CFG_OTG, ret); 502 + if (ret < 0) 503 + return ret; 504 + } 505 + 506 + ret = smb347_read(smb, CFG_TEMP_LIMIT); 507 + if (ret < 0) 508 + return ret; 509 + 510 + if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 511 + val = smb->pdata->soft_cold_temp_limit; 512 + 513 + val = clamp_val(val, 0, 15); 514 + val /= 5; 515 + /* this goes from higher to lower so invert the value */ 516 + val = ~val & 0x3; 517 + 518 + ret &= ~CFG_TEMP_LIMIT_SOFT_COLD_MASK; 519 + ret |= val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT; 520 + 521 + enable_therm_monitor = true; 522 + } 523 + 524 + if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { 525 + val = smb->pdata->soft_hot_temp_limit; 526 + 527 + val = clamp_val(val, 40, 55) - 40; 528 + val /= 5; 529 + 530 + ret &= ~CFG_TEMP_LIMIT_SOFT_HOT_MASK; 531 + ret |= val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT; 532 + 533 + enable_therm_monitor = true; 534 + } 535 + 536 + if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 537 + val = smb->pdata->hard_cold_temp_limit; 538 + 539 + val = clamp_val(val, -5, 10) + 5; 540 + val /= 5; 541 + /* this goes from higher to lower so invert the value */ 542 + val = ~val & 0x3; 543 + 544 + ret &= ~CFG_TEMP_LIMIT_HARD_COLD_MASK; 545 + ret |= val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT; 546 + 547 + enable_therm_monitor = true; 548 + } 549 + 550 + if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { 551 + val = smb->pdata->hard_hot_temp_limit; 552 + 553 + val = clamp_val(val, 50, 65) - 50; 554 + val /= 5; 555 + 556 + ret &= ~CFG_TEMP_LIMIT_HARD_HOT_MASK; 557 + ret |= val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT; 558 + 559 + enable_therm_monitor = true; 560 + } 561 + 562 + ret = smb347_write(smb, CFG_TEMP_LIMIT, ret); 563 + if (ret < 0) 564 + return ret; 565 + 566 + /* 567 + * If any of the temperature limits are set, we also enable the 568 + * thermistor monitoring. 569 + * 570 + * When soft limits are hit, the device will start to compensate 571 + * current and/or voltage depending on the configuration. 572 + * 573 + * When hard limit is hit, the device will suspend charging 574 + * depending on the configuration. 575 + */ 576 + if (enable_therm_monitor) { 577 + ret = smb347_read(smb, CFG_THERM); 578 + if (ret < 0) 579 + return ret; 580 + 581 + ret &= ~CFG_THERM_MONITOR_DISABLED; 582 + 583 + ret = smb347_write(smb, CFG_THERM, ret); 584 + if (ret < 0) 585 + return ret; 586 + } 587 + 588 + if (smb->pdata->suspend_on_hard_temp_limit) { 589 + ret = smb347_read(smb, CFG_SYSOK); 590 + if (ret < 0) 591 + return ret; 592 + 593 + ret &= ~CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED; 594 + 595 + ret = smb347_write(smb, CFG_SYSOK, ret); 596 + if (ret < 0) 597 + return ret; 598 + } 599 + 600 + if (smb->pdata->soft_temp_limit_compensation != 601 + SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { 602 + val = smb->pdata->soft_temp_limit_compensation & 0x3; 603 + 604 + ret = smb347_read(smb, CFG_THERM); 605 + if (ret < 0) 606 + return ret; 607 + 608 + ret &= ~CFG_THERM_SOFT_HOT_COMPENSATION_MASK; 609 + ret |= val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT; 610 + 611 + ret &= ~CFG_THERM_SOFT_COLD_COMPENSATION_MASK; 612 + ret |= val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT; 613 + 614 + ret = smb347_write(smb, CFG_THERM, ret); 615 + if (ret < 0) 616 + return ret; 617 + } 618 + 619 + if (smb->pdata->charge_current_compensation) { 620 + val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl), 621 + smb->pdata->charge_current_compensation); 622 + if (val < 0) 623 + return val; 624 + 625 + ret = smb347_read(smb, CFG_OTG); 626 + if (ret < 0) 627 + return ret; 628 + 629 + ret &= ~CFG_OTG_CC_COMPENSATION_MASK; 630 + ret |= (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT; 631 + 632 + ret = smb347_write(smb, CFG_OTG, ret); 633 + if (ret < 0) 634 + return ret; 635 + } 636 + 637 + return ret; 638 + } 639 + 640 + /* 641 + * smb347_set_writable - enables/disables writing to non-volatile registers 642 + * @smb: pointer to smb347 charger instance 643 + * 644 + * You can enable/disable writing to the non-volatile configuration 645 + * registers by calling this function. 646 + * 647 + * Returns %0 on success and negative errno in case of failure. 648 + */ 649 + static int smb347_set_writable(struct smb347_charger *smb, bool writable) 650 + { 651 + int ret; 652 + 653 + ret = smb347_read(smb, CMD_A); 654 + if (ret < 0) 655 + return ret; 656 + 657 + if (writable) 658 + ret |= CMD_A_ALLOW_WRITE; 659 + else 660 + ret &= ~CMD_A_ALLOW_WRITE; 661 + 662 + return smb347_write(smb, CMD_A, ret); 663 + } 664 + 665 + static int smb347_hw_init(struct smb347_charger *smb) 666 + { 667 + int ret; 668 + 669 + ret = smb347_set_writable(smb, true); 670 + if (ret < 0) 671 + return ret; 672 + 673 + /* 674 + * Program the platform specific configuration values to the device 675 + * first. 676 + */ 677 + ret = smb347_set_charge_current(smb); 678 + if (ret < 0) 679 + goto fail; 680 + 681 + ret = smb347_set_current_limits(smb); 682 + if (ret < 0) 683 + goto fail; 684 + 685 + ret = smb347_set_voltage_limits(smb); 686 + if (ret < 0) 687 + goto fail; 688 + 689 + ret = smb347_set_temp_limits(smb); 690 + if (ret < 0) 691 + goto fail; 692 + 693 + /* If USB charging is disabled we put the USB in suspend mode */ 694 + if (!smb->pdata->use_usb) { 695 + ret = smb347_read(smb, CMD_A); 696 + if (ret < 0) 697 + goto fail; 698 + 699 + ret |= CMD_A_SUSPEND_ENABLED; 700 + 701 + ret = smb347_write(smb, CMD_A, ret); 702 + if (ret < 0) 703 + goto fail; 704 + } 705 + 706 + ret = smb347_read(smb, CFG_OTHER); 707 + if (ret < 0) 708 + goto fail; 709 + 710 + /* 711 + * If configured by platform data, we enable hardware Auto-OTG 712 + * support for driving VBUS. Otherwise we disable it. 713 + */ 714 + ret &= ~CFG_OTHER_RID_MASK; 715 + if (smb->pdata->use_usb_otg) 716 + ret |= CFG_OTHER_RID_ENABLED_AUTO_OTG; 717 + 718 + ret = smb347_write(smb, CFG_OTHER, ret); 719 + if (ret < 0) 720 + goto fail; 721 + 722 + ret = smb347_read(smb, CFG_PIN); 723 + if (ret < 0) 724 + goto fail; 725 + 726 + /* 727 + * Make the charging functionality controllable by a write to the 728 + * command register unless pin control is specified in the platform 729 + * data. 730 + */ 731 + ret &= ~CFG_PIN_EN_CTRL_MASK; 732 + 733 + switch (smb->pdata->enable_control) { 734 + case SMB347_CHG_ENABLE_SW: 735 + /* Do nothing, 0 means i2c control */ 736 + break; 737 + case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: 738 + ret |= CFG_PIN_EN_CTRL_ACTIVE_LOW; 739 + break; 740 + case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: 741 + ret |= CFG_PIN_EN_CTRL_ACTIVE_HIGH; 742 + break; 743 + } 744 + 745 + /* Disable Automatic Power Source Detection (APSD) interrupt. */ 746 + ret &= ~CFG_PIN_EN_APSD_IRQ; 747 + 748 + ret = smb347_write(smb, CFG_PIN, ret); 749 + if (ret < 0) 750 + goto fail; 751 + 752 + ret = smb347_update_status(smb); 753 + if (ret < 0) 754 + goto fail; 755 + 756 + ret = smb347_update_online(smb); 757 + 758 + fail: 759 + smb347_set_writable(smb, false); 760 + return ret; 761 + } 762 + 763 + static irqreturn_t smb347_interrupt(int irq, void *data) 764 + { 765 + struct smb347_charger *smb = data; 766 + int stat_c, irqstat_e, irqstat_c; 767 + irqreturn_t ret = IRQ_NONE; 768 + 769 + stat_c = smb347_read(smb, STAT_C); 770 + if (stat_c < 0) { 771 + dev_warn(&smb->client->dev, "reading STAT_C failed\n"); 772 + return IRQ_NONE; 773 + } 774 + 775 + irqstat_c = smb347_read(smb, IRQSTAT_C); 776 + if (irqstat_c < 0) { 777 + dev_warn(&smb->client->dev, "reading IRQSTAT_C failed\n"); 778 + return IRQ_NONE; 779 + } 780 + 781 + irqstat_e = smb347_read(smb, IRQSTAT_E); 782 + if (irqstat_e < 0) { 783 + dev_warn(&smb->client->dev, "reading IRQSTAT_E failed\n"); 784 + return IRQ_NONE; 785 + } 786 + 787 + /* 788 + * If we get charger error we report the error back to user and 789 + * disable charging. 790 + */ 791 + if (stat_c & STAT_C_CHARGER_ERROR) { 792 + dev_err(&smb->client->dev, 793 + "error in charger, disabling charging\n"); 794 + 795 + smb347_charging_disable(smb); 796 + power_supply_changed(&smb->battery); 797 + 798 + ret = IRQ_HANDLED; 799 + } 800 + 801 + /* 802 + * If we reached the termination current the battery is charged and 803 + * we can update the status now. Charging is automatically 804 + * disabled by the hardware. 805 + */ 806 + if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { 807 + if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) 808 + power_supply_changed(&smb->battery); 809 + ret = IRQ_HANDLED; 810 + } 811 + 812 + /* 813 + * If we got an under voltage interrupt it means that AC/USB input 814 + * was connected or disconnected. 815 + */ 816 + if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { 817 + if (smb347_update_status(smb) > 0) { 818 + smb347_update_online(smb); 819 + power_supply_changed(&smb->mains); 820 + power_supply_changed(&smb->usb); 821 + } 822 + ret = IRQ_HANDLED; 823 + } 824 + 825 + return ret; 826 + } 827 + 828 + static int smb347_irq_set(struct smb347_charger *smb, bool enable) 829 + { 830 + int ret; 831 + 832 + ret = smb347_set_writable(smb, true); 833 + if (ret < 0) 834 + return ret; 835 + 836 + /* 837 + * Enable/disable interrupts for: 838 + * - under voltage 839 + * - termination current reached 840 + * - charger error 841 + */ 842 + if (enable) { 843 + ret = smb347_write(smb, CFG_FAULT_IRQ, CFG_FAULT_IRQ_DCIN_UV); 844 + if (ret < 0) 845 + goto fail; 846 + 847 + ret = smb347_write(smb, CFG_STATUS_IRQ, 848 + CFG_STATUS_IRQ_TERMINATION_OR_TAPER); 849 + if (ret < 0) 850 + goto fail; 851 + 852 + ret = smb347_read(smb, CFG_PIN); 853 + if (ret < 0) 854 + goto fail; 855 + 856 + ret |= CFG_PIN_EN_CHARGER_ERROR; 857 + 858 + ret = smb347_write(smb, CFG_PIN, ret); 859 + } else { 860 + ret = smb347_write(smb, CFG_FAULT_IRQ, 0); 861 + if (ret < 0) 862 + goto fail; 863 + 864 + ret = smb347_write(smb, CFG_STATUS_IRQ, 0); 865 + if (ret < 0) 866 + goto fail; 867 + 868 + ret = smb347_read(smb, CFG_PIN); 869 + if (ret < 0) 870 + goto fail; 871 + 872 + ret &= ~CFG_PIN_EN_CHARGER_ERROR; 873 + 874 + ret = smb347_write(smb, CFG_PIN, ret); 875 + } 876 + 877 + fail: 878 + smb347_set_writable(smb, false); 879 + return ret; 880 + } 881 + 882 + static inline int smb347_irq_enable(struct smb347_charger *smb) 883 + { 884 + return smb347_irq_set(smb, true); 885 + } 886 + 887 + static inline int smb347_irq_disable(struct smb347_charger *smb) 888 + { 889 + return smb347_irq_set(smb, false); 890 + } 891 + 892 + static int smb347_irq_init(struct smb347_charger *smb) 893 + { 894 + const struct smb347_charger_platform_data *pdata = smb->pdata; 895 + int ret, irq = gpio_to_irq(pdata->irq_gpio); 896 + 897 + ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, smb->client->name); 898 + if (ret < 0) 899 + goto fail; 900 + 901 + ret = request_threaded_irq(irq, NULL, smb347_interrupt, 902 + IRQF_TRIGGER_FALLING, smb->client->name, 903 + smb); 904 + if (ret < 0) 905 + goto fail_gpio; 906 + 907 + ret = smb347_set_writable(smb, true); 908 + if (ret < 0) 909 + goto fail_irq; 910 + 911 + /* 912 + * Configure the STAT output to be suitable for interrupts: disable 913 + * all other output (except interrupts) and make it active low. 914 + */ 915 + ret = smb347_read(smb, CFG_STAT); 916 + if (ret < 0) 917 + goto fail_readonly; 918 + 919 + ret &= ~CFG_STAT_ACTIVE_HIGH; 920 + ret |= CFG_STAT_DISABLED; 921 + 922 + ret = smb347_write(smb, CFG_STAT, ret); 923 + if (ret < 0) 924 + goto fail_readonly; 925 + 926 + ret = smb347_irq_enable(smb); 927 + if (ret < 0) 928 + goto fail_readonly; 929 + 930 + smb347_set_writable(smb, false); 931 + smb->client->irq = irq; 932 + return 0; 933 + 934 + fail_readonly: 935 + smb347_set_writable(smb, false); 936 + fail_irq: 937 + free_irq(irq, smb); 938 + fail_gpio: 939 + gpio_free(pdata->irq_gpio); 940 + fail: 941 + smb->client->irq = 0; 942 + return ret; 943 + } 944 + 945 + static int smb347_mains_get_property(struct power_supply *psy, 946 + enum power_supply_property prop, 947 + union power_supply_propval *val) 948 + { 949 + struct smb347_charger *smb = 950 + container_of(psy, struct smb347_charger, mains); 951 + 952 + if (prop == POWER_SUPPLY_PROP_ONLINE) { 953 + val->intval = smb->mains_online; 954 + return 0; 955 + } 956 + return -EINVAL; 957 + } 958 + 959 + static enum power_supply_property smb347_mains_properties[] = { 960 + POWER_SUPPLY_PROP_ONLINE, 961 + }; 962 + 963 + static int smb347_usb_get_property(struct power_supply *psy, 964 + enum power_supply_property prop, 965 + union power_supply_propval *val) 966 + { 967 + struct smb347_charger *smb = 968 + container_of(psy, struct smb347_charger, usb); 969 + 970 + if (prop == POWER_SUPPLY_PROP_ONLINE) { 971 + val->intval = smb->usb_online; 972 + return 0; 973 + } 974 + return -EINVAL; 975 + } 976 + 977 + static enum power_supply_property smb347_usb_properties[] = { 978 + POWER_SUPPLY_PROP_ONLINE, 979 + }; 980 + 981 + static int smb347_battery_get_property(struct power_supply *psy, 982 + enum power_supply_property prop, 983 + union power_supply_propval *val) 984 + { 985 + struct smb347_charger *smb = 986 + container_of(psy, struct smb347_charger, battery); 987 + const struct smb347_charger_platform_data *pdata = smb->pdata; 988 + int ret; 989 + 990 + ret = smb347_update_status(smb); 991 + if (ret < 0) 992 + return ret; 993 + 994 + switch (prop) { 995 + case POWER_SUPPLY_PROP_STATUS: 996 + if (!smb347_is_online(smb)) { 997 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 998 + break; 999 + } 1000 + if (smb347_charging_status(smb)) 1001 + val->intval = POWER_SUPPLY_STATUS_CHARGING; 1002 + else 1003 + val->intval = POWER_SUPPLY_STATUS_FULL; 1004 + break; 1005 + 1006 + case POWER_SUPPLY_PROP_CHARGE_TYPE: 1007 + if (!smb347_is_online(smb)) 1008 + return -ENODATA; 1009 + 1010 + /* 1011 + * We handle trickle and pre-charging the same, and taper 1012 + * and none the same. 1013 + */ 1014 + switch (smb347_charging_status(smb)) { 1015 + case 1: 1016 + val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 1017 + break; 1018 + case 2: 1019 + val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; 1020 + break; 1021 + default: 1022 + val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; 1023 + break; 1024 + } 1025 + break; 1026 + 1027 + case POWER_SUPPLY_PROP_TECHNOLOGY: 1028 + val->intval = pdata->battery_info.technology; 1029 + break; 1030 + 1031 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 1032 + val->intval = pdata->battery_info.voltage_min_design; 1033 + break; 1034 + 1035 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 1036 + val->intval = pdata->battery_info.voltage_max_design; 1037 + break; 1038 + 1039 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1040 + if (!smb347_is_online(smb)) 1041 + return -ENODATA; 1042 + ret = smb347_read(smb, STAT_A); 1043 + if (ret < 0) 1044 + return ret; 1045 + 1046 + ret &= STAT_A_FLOAT_VOLTAGE_MASK; 1047 + if (ret > 0x3d) 1048 + ret = 0x3d; 1049 + 1050 + val->intval = 3500000 + ret * 20000; 1051 + break; 1052 + 1053 + case POWER_SUPPLY_PROP_CURRENT_NOW: 1054 + if (!smb347_is_online(smb)) 1055 + return -ENODATA; 1056 + 1057 + ret = smb347_read(smb, STAT_B); 1058 + if (ret < 0) 1059 + return ret; 1060 + 1061 + /* 1062 + * The current value is composition of FCC and PCC values 1063 + * and we can detect which table to use from bit 5. 1064 + */ 1065 + if (ret & 0x20) { 1066 + val->intval = hw_to_current(fcc_tbl, 1067 + ARRAY_SIZE(fcc_tbl), 1068 + ret & 7); 1069 + } else { 1070 + ret >>= 3; 1071 + val->intval = hw_to_current(pcc_tbl, 1072 + ARRAY_SIZE(pcc_tbl), 1073 + ret & 7); 1074 + } 1075 + break; 1076 + 1077 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 1078 + val->intval = pdata->battery_info.charge_full_design; 1079 + break; 1080 + 1081 + case POWER_SUPPLY_PROP_MODEL_NAME: 1082 + val->strval = pdata->battery_info.name; 1083 + break; 1084 + 1085 + default: 1086 + return -EINVAL; 1087 + } 1088 + 1089 + return 0; 1090 + } 1091 + 1092 + static enum power_supply_property smb347_battery_properties[] = { 1093 + POWER_SUPPLY_PROP_STATUS, 1094 + POWER_SUPPLY_PROP_CHARGE_TYPE, 1095 + POWER_SUPPLY_PROP_TECHNOLOGY, 1096 + POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 1097 + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 1098 + POWER_SUPPLY_PROP_VOLTAGE_NOW, 1099 + POWER_SUPPLY_PROP_CURRENT_NOW, 1100 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 1101 + POWER_SUPPLY_PROP_MODEL_NAME, 1102 + }; 1103 + 1104 + static int smb347_debugfs_show(struct seq_file *s, void *data) 1105 + { 1106 + struct smb347_charger *smb = s->private; 1107 + int ret; 1108 + u8 reg; 1109 + 1110 + seq_printf(s, "Control registers:\n"); 1111 + seq_printf(s, "==================\n"); 1112 + for (reg = CFG_CHARGE_CURRENT; reg <= CFG_ADDRESS; reg++) { 1113 + ret = smb347_read(smb, reg); 1114 + seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 1115 + } 1116 + seq_printf(s, "\n"); 1117 + 1118 + seq_printf(s, "Command registers:\n"); 1119 + seq_printf(s, "==================\n"); 1120 + ret = smb347_read(smb, CMD_A); 1121 + seq_printf(s, "0x%02x:\t0x%02x\n", CMD_A, ret); 1122 + ret = smb347_read(smb, CMD_B); 1123 + seq_printf(s, "0x%02x:\t0x%02x\n", CMD_B, ret); 1124 + ret = smb347_read(smb, CMD_C); 1125 + seq_printf(s, "0x%02x:\t0x%02x\n", CMD_C, ret); 1126 + seq_printf(s, "\n"); 1127 + 1128 + seq_printf(s, "Interrupt status registers:\n"); 1129 + seq_printf(s, "===========================\n"); 1130 + for (reg = IRQSTAT_A; reg <= IRQSTAT_F; reg++) { 1131 + ret = smb347_read(smb, reg); 1132 + seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 1133 + } 1134 + seq_printf(s, "\n"); 1135 + 1136 + seq_printf(s, "Status registers:\n"); 1137 + seq_printf(s, "=================\n"); 1138 + for (reg = STAT_A; reg <= STAT_E; reg++) { 1139 + ret = smb347_read(smb, reg); 1140 + seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 1141 + } 1142 + 1143 + return 0; 1144 + } 1145 + 1146 + static int smb347_debugfs_open(struct inode *inode, struct file *file) 1147 + { 1148 + return single_open(file, smb347_debugfs_show, inode->i_private); 1149 + } 1150 + 1151 + static const struct file_operations smb347_debugfs_fops = { 1152 + .open = smb347_debugfs_open, 1153 + .read = seq_read, 1154 + .llseek = seq_lseek, 1155 + .release = single_release, 1156 + }; 1157 + 1158 + static int smb347_probe(struct i2c_client *client, 1159 + const struct i2c_device_id *id) 1160 + { 1161 + static char *battery[] = { "smb347-battery" }; 1162 + const struct smb347_charger_platform_data *pdata; 1163 + struct device *dev = &client->dev; 1164 + struct smb347_charger *smb; 1165 + int ret; 1166 + 1167 + pdata = dev->platform_data; 1168 + if (!pdata) 1169 + return -EINVAL; 1170 + 1171 + if (!pdata->use_mains && !pdata->use_usb) 1172 + return -EINVAL; 1173 + 1174 + smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL); 1175 + if (!smb) 1176 + return -ENOMEM; 1177 + 1178 + i2c_set_clientdata(client, smb); 1179 + 1180 + mutex_init(&smb->lock); 1181 + smb->client = client; 1182 + smb->pdata = pdata; 1183 + 1184 + ret = smb347_hw_init(smb); 1185 + if (ret < 0) 1186 + return ret; 1187 + 1188 + smb->mains.name = "smb347-mains"; 1189 + smb->mains.type = POWER_SUPPLY_TYPE_MAINS; 1190 + smb->mains.get_property = smb347_mains_get_property; 1191 + smb->mains.properties = smb347_mains_properties; 1192 + smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties); 1193 + smb->mains.supplied_to = battery; 1194 + smb->mains.num_supplicants = ARRAY_SIZE(battery); 1195 + 1196 + smb->usb.name = "smb347-usb"; 1197 + smb->usb.type = POWER_SUPPLY_TYPE_USB; 1198 + smb->usb.get_property = smb347_usb_get_property; 1199 + smb->usb.properties = smb347_usb_properties; 1200 + smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties); 1201 + smb->usb.supplied_to = battery; 1202 + smb->usb.num_supplicants = ARRAY_SIZE(battery); 1203 + 1204 + smb->battery.name = "smb347-battery"; 1205 + smb->battery.type = POWER_SUPPLY_TYPE_BATTERY; 1206 + smb->battery.get_property = smb347_battery_get_property; 1207 + smb->battery.properties = smb347_battery_properties; 1208 + smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties); 1209 + 1210 + ret = power_supply_register(dev, &smb->mains); 1211 + if (ret < 0) 1212 + return ret; 1213 + 1214 + ret = power_supply_register(dev, &smb->usb); 1215 + if (ret < 0) { 1216 + power_supply_unregister(&smb->mains); 1217 + return ret; 1218 + } 1219 + 1220 + ret = power_supply_register(dev, &smb->battery); 1221 + if (ret < 0) { 1222 + power_supply_unregister(&smb->usb); 1223 + power_supply_unregister(&smb->mains); 1224 + return ret; 1225 + } 1226 + 1227 + /* 1228 + * Interrupt pin is optional. If it is connected, we setup the 1229 + * interrupt support here. 1230 + */ 1231 + if (pdata->irq_gpio >= 0) { 1232 + ret = smb347_irq_init(smb); 1233 + if (ret < 0) { 1234 + dev_warn(dev, "failed to initialize IRQ: %d\n", ret); 1235 + dev_warn(dev, "disabling IRQ support\n"); 1236 + } 1237 + } 1238 + 1239 + smb->dentry = debugfs_create_file("smb347-regs", S_IRUSR, NULL, smb, 1240 + &smb347_debugfs_fops); 1241 + return 0; 1242 + } 1243 + 1244 + static int smb347_remove(struct i2c_client *client) 1245 + { 1246 + struct smb347_charger *smb = i2c_get_clientdata(client); 1247 + 1248 + if (!IS_ERR_OR_NULL(smb->dentry)) 1249 + debugfs_remove(smb->dentry); 1250 + 1251 + if (client->irq) { 1252 + smb347_irq_disable(smb); 1253 + free_irq(client->irq, smb); 1254 + gpio_free(smb->pdata->irq_gpio); 1255 + } 1256 + 1257 + power_supply_unregister(&smb->battery); 1258 + power_supply_unregister(&smb->usb); 1259 + power_supply_unregister(&smb->mains); 1260 + return 0; 1261 + } 1262 + 1263 + static const struct i2c_device_id smb347_id[] = { 1264 + { "smb347", 0 }, 1265 + { } 1266 + }; 1267 + MODULE_DEVICE_TABLE(i2c, smb347_id); 1268 + 1269 + static struct i2c_driver smb347_driver = { 1270 + .driver = { 1271 + .name = "smb347", 1272 + }, 1273 + .probe = smb347_probe, 1274 + .remove = __devexit_p(smb347_remove), 1275 + .id_table = smb347_id, 1276 + }; 1277 + 1278 + static int __init smb347_init(void) 1279 + { 1280 + return i2c_add_driver(&smb347_driver); 1281 + } 1282 + module_init(smb347_init); 1283 + 1284 + static void __exit smb347_exit(void) 1285 + { 1286 + i2c_del_driver(&smb347_driver); 1287 + } 1288 + module_exit(smb347_exit); 1289 + 1290 + MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); 1291 + MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 1292 + MODULE_DESCRIPTION("SMB347 battery charger driver"); 1293 + MODULE_LICENSE("GPL"); 1294 + MODULE_ALIAS("i2c:smb347");
+1 -13
drivers/power/z2_battery.c
··· 316 316 .remove = __devexit_p(z2_batt_remove), 317 317 .id_table = z2_batt_id, 318 318 }; 319 - 320 - static int __init z2_batt_init(void) 321 - { 322 - return i2c_add_driver(&z2_batt_driver); 323 - } 324 - 325 - static void __exit z2_batt_exit(void) 326 - { 327 - i2c_del_driver(&z2_batt_driver); 328 - } 329 - 330 - module_init(z2_batt_init); 331 - module_exit(z2_batt_exit); 319 + module_i2c_driver(z2_batt_driver); 332 320 333 321 MODULE_LICENSE("GPL"); 334 322 MODULE_AUTHOR("Peter Edwards <sweetlilmre@gmail.com>");
+16 -2
include/linux/lp8727.h
··· 1 1 /* 2 + * LP8727 Micro/Mini USB IC with integrated charger 3 + * 4 + * Copyright (C) 2011 Texas Instruments 2 5 * Copyright (C) 2011 National Semiconductor 3 6 * 4 7 * This program is free software; you can redistribute it and/or modify ··· 35 32 ICHG_1000mA, 36 33 }; 37 34 35 + /** 36 + * struct lp8727_chg_param 37 + * @eoc_level : end of charge level setting 38 + * @ichg : charging current 39 + */ 38 40 struct lp8727_chg_param { 39 - /* end of charge level setting */ 40 41 enum lp8727_eoc_level eoc_level; 41 - /* charging current */ 42 42 enum lp8727_ichg ichg; 43 43 }; 44 44 45 + /** 46 + * struct lp8727_platform_data 47 + * @get_batt_present : check battery status - exists or not 48 + * @get_batt_level : get battery voltage (mV) 49 + * @get_batt_capacity : get battery capacity (%) 50 + * @get_batt_temp : get battery temperature 51 + * @ac, @usb : charging parameters each charger type 52 + */ 45 53 struct lp8727_platform_data { 46 54 u8 (*get_batt_present)(void); 47 55 u16 (*get_batt_level)(void);
+273
include/linux/mfd/abx500.h
··· 146 146 u8 setting; 147 147 }; 148 148 149 + /* Battery driver related data */ 150 + /* 151 + * ADC for the battery thermistor. 152 + * When using the ABx500_ADC_THERM_BATCTRL the battery ID resistor is combined 153 + * with a NTC resistor to both identify the battery and to measure its 154 + * temperature. Different phone manufactures uses different techniques to both 155 + * identify the battery and to read its temperature. 156 + */ 157 + enum abx500_adc_therm { 158 + ABx500_ADC_THERM_BATCTRL, 159 + ABx500_ADC_THERM_BATTEMP, 160 + }; 161 + 162 + /** 163 + * struct abx500_res_to_temp - defines one point in a temp to res curve. To 164 + * be used in battery packs that combines the identification resistor with a 165 + * NTC resistor. 166 + * @temp: battery pack temperature in Celcius 167 + * @resist: NTC resistor net total resistance 168 + */ 169 + struct abx500_res_to_temp { 170 + int temp; 171 + int resist; 172 + }; 173 + 174 + /** 175 + * struct abx500_v_to_cap - Table for translating voltage to capacity 176 + * @voltage: Voltage in mV 177 + * @capacity: Capacity in percent 178 + */ 179 + struct abx500_v_to_cap { 180 + int voltage; 181 + int capacity; 182 + }; 183 + 184 + /* Forward declaration */ 185 + struct abx500_fg; 186 + 187 + /** 188 + * struct abx500_fg_parameters - Fuel gauge algorithm parameters, in seconds 189 + * if not specified 190 + * @recovery_sleep_timer: Time between measurements while recovering 191 + * @recovery_total_time: Total recovery time 192 + * @init_timer: Measurement interval during startup 193 + * @init_discard_time: Time we discard voltage measurement at startup 194 + * @init_total_time: Total init time during startup 195 + * @high_curr_time: Time current has to be high to go to recovery 196 + * @accu_charging: FG accumulation time while charging 197 + * @accu_high_curr: FG accumulation time in high current mode 198 + * @high_curr_threshold: High current threshold, in mA 199 + * @lowbat_threshold: Low battery threshold, in mV 200 + * @overbat_threshold: Over battery threshold, in mV 201 + * @battok_falling_th_sel0 Threshold in mV for battOk signal sel0 202 + * Resolution in 50 mV step. 203 + * @battok_raising_th_sel1 Threshold in mV for battOk signal sel1 204 + * Resolution in 50 mV step. 205 + * @user_cap_limit Capacity reported from user must be within this 206 + * limit to be considered as sane, in percentage 207 + * points. 208 + * @maint_thres This is the threshold where we stop reporting 209 + * battery full while in maintenance, in per cent 210 + */ 211 + struct abx500_fg_parameters { 212 + int recovery_sleep_timer; 213 + int recovery_total_time; 214 + int init_timer; 215 + int init_discard_time; 216 + int init_total_time; 217 + int high_curr_time; 218 + int accu_charging; 219 + int accu_high_curr; 220 + int high_curr_threshold; 221 + int lowbat_threshold; 222 + int overbat_threshold; 223 + int battok_falling_th_sel0; 224 + int battok_raising_th_sel1; 225 + int user_cap_limit; 226 + int maint_thres; 227 + }; 228 + 229 + /** 230 + * struct abx500_charger_maximization - struct used by the board config. 231 + * @use_maxi: Enable maximization for this battery type 232 + * @maxi_chg_curr: Maximum charger current allowed 233 + * @maxi_wait_cycles: cycles to wait before setting charger current 234 + * @charger_curr_step delta between two charger current settings (mA) 235 + */ 236 + struct abx500_maxim_parameters { 237 + bool ena_maxi; 238 + int chg_curr; 239 + int wait_cycles; 240 + int charger_curr_step; 241 + }; 242 + 243 + /** 244 + * struct abx500_battery_type - different batteries supported 245 + * @name: battery technology 246 + * @resis_high: battery upper resistance limit 247 + * @resis_low: battery lower resistance limit 248 + * @charge_full_design: Maximum battery capacity in mAh 249 + * @nominal_voltage: Nominal voltage of the battery in mV 250 + * @termination_vol: max voltage upto which battery can be charged 251 + * @termination_curr battery charging termination current in mA 252 + * @recharge_vol battery voltage limit that will trigger a new 253 + * full charging cycle in the case where maintenan- 254 + * -ce charging has been disabled 255 + * @normal_cur_lvl: charger current in normal state in mA 256 + * @normal_vol_lvl: charger voltage in normal state in mV 257 + * @maint_a_cur_lvl: charger current in maintenance A state in mA 258 + * @maint_a_vol_lvl: charger voltage in maintenance A state in mV 259 + * @maint_a_chg_timer_h: charge time in maintenance A state 260 + * @maint_b_cur_lvl: charger current in maintenance B state in mA 261 + * @maint_b_vol_lvl: charger voltage in maintenance B state in mV 262 + * @maint_b_chg_timer_h: charge time in maintenance B state 263 + * @low_high_cur_lvl: charger current in temp low/high state in mA 264 + * @low_high_vol_lvl: charger voltage in temp low/high state in mV' 265 + * @battery_resistance: battery inner resistance in mOhm. 266 + * @n_r_t_tbl_elements: number of elements in r_to_t_tbl 267 + * @r_to_t_tbl: table containing resistance to temp points 268 + * @n_v_cap_tbl_elements: number of elements in v_to_cap_tbl 269 + * @v_to_cap_tbl: Voltage to capacity (in %) table 270 + * @n_batres_tbl_elements number of elements in the batres_tbl 271 + * @batres_tbl battery internal resistance vs temperature table 272 + */ 273 + struct abx500_battery_type { 274 + int name; 275 + int resis_high; 276 + int resis_low; 277 + int charge_full_design; 278 + int nominal_voltage; 279 + int termination_vol; 280 + int termination_curr; 281 + int recharge_vol; 282 + int normal_cur_lvl; 283 + int normal_vol_lvl; 284 + int maint_a_cur_lvl; 285 + int maint_a_vol_lvl; 286 + int maint_a_chg_timer_h; 287 + int maint_b_cur_lvl; 288 + int maint_b_vol_lvl; 289 + int maint_b_chg_timer_h; 290 + int low_high_cur_lvl; 291 + int low_high_vol_lvl; 292 + int battery_resistance; 293 + int n_temp_tbl_elements; 294 + struct abx500_res_to_temp *r_to_t_tbl; 295 + int n_v_cap_tbl_elements; 296 + struct abx500_v_to_cap *v_to_cap_tbl; 297 + int n_batres_tbl_elements; 298 + struct batres_vs_temp *batres_tbl; 299 + }; 300 + 301 + /** 302 + * struct abx500_bm_capacity_levels - abx500 capacity level data 303 + * @critical: critical capacity level in percent 304 + * @low: low capacity level in percent 305 + * @normal: normal capacity level in percent 306 + * @high: high capacity level in percent 307 + * @full: full capacity level in percent 308 + */ 309 + struct abx500_bm_capacity_levels { 310 + int critical; 311 + int low; 312 + int normal; 313 + int high; 314 + int full; 315 + }; 316 + 317 + /** 318 + * struct abx500_bm_charger_parameters - Charger specific parameters 319 + * @usb_volt_max: maximum allowed USB charger voltage in mV 320 + * @usb_curr_max: maximum allowed USB charger current in mA 321 + * @ac_volt_max: maximum allowed AC charger voltage in mV 322 + * @ac_curr_max: maximum allowed AC charger current in mA 323 + */ 324 + struct abx500_bm_charger_parameters { 325 + int usb_volt_max; 326 + int usb_curr_max; 327 + int ac_volt_max; 328 + int ac_curr_max; 329 + }; 330 + 331 + /** 332 + * struct abx500_bm_data - abx500 battery management data 333 + * @temp_under under this temp, charging is stopped 334 + * @temp_low between this temp and temp_under charging is reduced 335 + * @temp_high between this temp and temp_over charging is reduced 336 + * @temp_over over this temp, charging is stopped 337 + * @temp_now present battery temperature 338 + * @temp_interval_chg temperature measurement interval in s when charging 339 + * @temp_interval_nochg temperature measurement interval in s when not charging 340 + * @main_safety_tmr_h safety timer for main charger 341 + * @usb_safety_tmr_h safety timer for usb charger 342 + * @bkup_bat_v voltage which we charge the backup battery with 343 + * @bkup_bat_i current which we charge the backup battery with 344 + * @no_maintenance indicates that maintenance charging is disabled 345 + * @abx500_adc_therm placement of thermistor, batctrl or battemp adc 346 + * @chg_unknown_bat flag to enable charging of unknown batteries 347 + * @enable_overshoot flag to enable VBAT overshoot control 348 + * @auto_trig flag to enable auto adc trigger 349 + * @fg_res resistance of FG resistor in 0.1mOhm 350 + * @n_btypes number of elements in array bat_type 351 + * @batt_id index of the identified battery in array bat_type 352 + * @interval_charging charge alg cycle period time when charging (sec) 353 + * @interval_not_charging charge alg cycle period time when not charging (sec) 354 + * @temp_hysteresis temperature hysteresis 355 + * @gnd_lift_resistance Battery ground to phone ground resistance (mOhm) 356 + * @maxi: maximization parameters 357 + * @cap_levels capacity in percent for the different capacity levels 358 + * @bat_type table of supported battery types 359 + * @chg_params charger parameters 360 + * @fg_params fuel gauge parameters 361 + */ 362 + struct abx500_bm_data { 363 + int temp_under; 364 + int temp_low; 365 + int temp_high; 366 + int temp_over; 367 + int temp_now; 368 + int temp_interval_chg; 369 + int temp_interval_nochg; 370 + int main_safety_tmr_h; 371 + int usb_safety_tmr_h; 372 + int bkup_bat_v; 373 + int bkup_bat_i; 374 + bool no_maintenance; 375 + bool chg_unknown_bat; 376 + bool enable_overshoot; 377 + bool auto_trig; 378 + enum abx500_adc_therm adc_therm; 379 + int fg_res; 380 + int n_btypes; 381 + int batt_id; 382 + int interval_charging; 383 + int interval_not_charging; 384 + int temp_hysteresis; 385 + int gnd_lift_resistance; 386 + const struct abx500_maxim_parameters *maxi; 387 + const struct abx500_bm_capacity_levels *cap_levels; 388 + const struct abx500_battery_type *bat_type; 389 + const struct abx500_bm_charger_parameters *chg_params; 390 + const struct abx500_fg_parameters *fg_params; 391 + }; 392 + 393 + struct abx500_chargalg_platform_data { 394 + char **supplied_to; 395 + size_t num_supplicants; 396 + }; 397 + 398 + struct abx500_charger_platform_data { 399 + char **supplied_to; 400 + size_t num_supplicants; 401 + bool autopower_cfg; 402 + }; 403 + 404 + struct abx500_btemp_platform_data { 405 + char **supplied_to; 406 + size_t num_supplicants; 407 + }; 408 + 409 + struct abx500_fg_platform_data { 410 + char **supplied_to; 411 + size_t num_supplicants; 412 + }; 413 + 414 + struct abx500_bm_plat_data { 415 + struct abx500_bm_data *battery; 416 + struct abx500_charger_platform_data *charger; 417 + struct abx500_btemp_platform_data *btemp; 418 + struct abx500_fg_platform_data *fg; 419 + struct abx500_chargalg_platform_data *chargalg; 420 + }; 421 + 149 422 int abx500_set_register_interruptible(struct device *dev, u8 bank, u8 reg, 150 423 u8 value); 151 424 int abx500_get_register_interruptible(struct device *dev, u8 bank, u8 reg,
+474
include/linux/mfd/abx500/ab8500-bm.h
··· 1 + /* 2 + * Copyright ST-Ericsson 2012. 3 + * 4 + * Author: Arun Murthy <arun.murthy@stericsson.com> 5 + * Licensed under GPLv2. 6 + */ 7 + 8 + #ifndef _AB8500_BM_H 9 + #define _AB8500_BM_H 10 + 11 + #include <linux/kernel.h> 12 + #include <linux/mfd/abx500.h> 13 + 14 + /* 15 + * System control 2 register offsets. 16 + * bank = 0x02 17 + */ 18 + #define AB8500_MAIN_WDOG_CTRL_REG 0x01 19 + #define AB8500_LOW_BAT_REG 0x03 20 + #define AB8500_BATT_OK_REG 0x04 21 + /* 22 + * USB/ULPI register offsets 23 + * Bank : 0x5 24 + */ 25 + #define AB8500_USB_LINE_STAT_REG 0x80 26 + 27 + /* 28 + * Charger / status register offfsets 29 + * Bank : 0x0B 30 + */ 31 + #define AB8500_CH_STATUS1_REG 0x00 32 + #define AB8500_CH_STATUS2_REG 0x01 33 + #define AB8500_CH_USBCH_STAT1_REG 0x02 34 + #define AB8500_CH_USBCH_STAT2_REG 0x03 35 + #define AB8500_CH_FSM_STAT_REG 0x04 36 + #define AB8500_CH_STAT_REG 0x05 37 + 38 + /* 39 + * Charger / control register offfsets 40 + * Bank : 0x0B 41 + */ 42 + #define AB8500_CH_VOLT_LVL_REG 0x40 43 + #define AB8500_CH_VOLT_LVL_MAX_REG 0x41 /*Only in Cut2.0*/ 44 + #define AB8500_CH_OPT_CRNTLVL_REG 0x42 45 + #define AB8500_CH_OPT_CRNTLVL_MAX_REG 0x43 /*Only in Cut2.0*/ 46 + #define AB8500_CH_WD_TIMER_REG 0x50 47 + #define AB8500_CHARG_WD_CTRL 0x51 48 + #define AB8500_BTEMP_HIGH_TH 0x52 49 + #define AB8500_LED_INDICATOR_PWM_CTRL 0x53 50 + #define AB8500_LED_INDICATOR_PWM_DUTY 0x54 51 + #define AB8500_BATT_OVV 0x55 52 + #define AB8500_CHARGER_CTRL 0x56 53 + #define AB8500_BAT_CTRL_CURRENT_SOURCE 0x60 /*Only in Cut2.0*/ 54 + 55 + /* 56 + * Charger / main control register offsets 57 + * Bank : 0x0B 58 + */ 59 + #define AB8500_MCH_CTRL1 0x80 60 + #define AB8500_MCH_CTRL2 0x81 61 + #define AB8500_MCH_IPT_CURLVL_REG 0x82 62 + #define AB8500_CH_WD_REG 0x83 63 + 64 + /* 65 + * Charger / USB control register offsets 66 + * Bank : 0x0B 67 + */ 68 + #define AB8500_USBCH_CTRL1_REG 0xC0 69 + #define AB8500_USBCH_CTRL2_REG 0xC1 70 + #define AB8500_USBCH_IPT_CRNTLVL_REG 0xC2 71 + 72 + /* 73 + * Gas Gauge register offsets 74 + * Bank : 0x0C 75 + */ 76 + #define AB8500_GASG_CC_CTRL_REG 0x00 77 + #define AB8500_GASG_CC_ACCU1_REG 0x01 78 + #define AB8500_GASG_CC_ACCU2_REG 0x02 79 + #define AB8500_GASG_CC_ACCU3_REG 0x03 80 + #define AB8500_GASG_CC_ACCU4_REG 0x04 81 + #define AB8500_GASG_CC_SMPL_CNTRL_REG 0x05 82 + #define AB8500_GASG_CC_SMPL_CNTRH_REG 0x06 83 + #define AB8500_GASG_CC_SMPL_CNVL_REG 0x07 84 + #define AB8500_GASG_CC_SMPL_CNVH_REG 0x08 85 + #define AB8500_GASG_CC_CNTR_AVGOFF_REG 0x09 86 + #define AB8500_GASG_CC_OFFSET_REG 0x0A 87 + #define AB8500_GASG_CC_NCOV_ACCU 0x10 88 + #define AB8500_GASG_CC_NCOV_ACCU_CTRL 0x11 89 + #define AB8500_GASG_CC_NCOV_ACCU_LOW 0x12 90 + #define AB8500_GASG_CC_NCOV_ACCU_MED 0x13 91 + #define AB8500_GASG_CC_NCOV_ACCU_HIGH 0x14 92 + 93 + /* 94 + * Interrupt register offsets 95 + * Bank : 0x0E 96 + */ 97 + #define AB8500_IT_SOURCE2_REG 0x01 98 + #define AB8500_IT_SOURCE21_REG 0x14 99 + 100 + /* 101 + * RTC register offsets 102 + * Bank: 0x0F 103 + */ 104 + #define AB8500_RTC_BACKUP_CHG_REG 0x0C 105 + #define AB8500_RTC_CC_CONF_REG 0x01 106 + #define AB8500_RTC_CTRL_REG 0x0B 107 + 108 + /* 109 + * OTP register offsets 110 + * Bank : 0x15 111 + */ 112 + #define AB8500_OTP_CONF_15 0x0E 113 + 114 + /* GPADC constants from AB8500 spec, UM0836 */ 115 + #define ADC_RESOLUTION 1024 116 + #define ADC_CH_MAIN_MIN 0 117 + #define ADC_CH_MAIN_MAX 20030 118 + #define ADC_CH_VBUS_MIN 0 119 + #define ADC_CH_VBUS_MAX 20030 120 + #define ADC_CH_VBAT_MIN 2300 121 + #define ADC_CH_VBAT_MAX 4800 122 + #define ADC_CH_BKBAT_MIN 0 123 + #define ADC_CH_BKBAT_MAX 3200 124 + 125 + /* Main charge i/p current */ 126 + #define MAIN_CH_IP_CUR_0P9A 0x80 127 + #define MAIN_CH_IP_CUR_1P0A 0x90 128 + #define MAIN_CH_IP_CUR_1P1A 0xA0 129 + #define MAIN_CH_IP_CUR_1P2A 0xB0 130 + #define MAIN_CH_IP_CUR_1P3A 0xC0 131 + #define MAIN_CH_IP_CUR_1P4A 0xD0 132 + #define MAIN_CH_IP_CUR_1P5A 0xE0 133 + 134 + /* ChVoltLevel */ 135 + #define CH_VOL_LVL_3P5 0x00 136 + #define CH_VOL_LVL_4P0 0x14 137 + #define CH_VOL_LVL_4P05 0x16 138 + #define CH_VOL_LVL_4P1 0x1B 139 + #define CH_VOL_LVL_4P15 0x20 140 + #define CH_VOL_LVL_4P2 0x25 141 + #define CH_VOL_LVL_4P6 0x4D 142 + 143 + /* ChOutputCurrentLevel */ 144 + #define CH_OP_CUR_LVL_0P1 0x00 145 + #define CH_OP_CUR_LVL_0P2 0x01 146 + #define CH_OP_CUR_LVL_0P3 0x02 147 + #define CH_OP_CUR_LVL_0P4 0x03 148 + #define CH_OP_CUR_LVL_0P5 0x04 149 + #define CH_OP_CUR_LVL_0P6 0x05 150 + #define CH_OP_CUR_LVL_0P7 0x06 151 + #define CH_OP_CUR_LVL_0P8 0x07 152 + #define CH_OP_CUR_LVL_0P9 0x08 153 + #define CH_OP_CUR_LVL_1P4 0x0D 154 + #define CH_OP_CUR_LVL_1P5 0x0E 155 + #define CH_OP_CUR_LVL_1P6 0x0F 156 + 157 + /* BTEMP High thermal limits */ 158 + #define BTEMP_HIGH_TH_57_0 0x00 159 + #define BTEMP_HIGH_TH_52 0x01 160 + #define BTEMP_HIGH_TH_57_1 0x02 161 + #define BTEMP_HIGH_TH_62 0x03 162 + 163 + /* current is mA */ 164 + #define USB_0P1A 100 165 + #define USB_0P2A 200 166 + #define USB_0P3A 300 167 + #define USB_0P4A 400 168 + #define USB_0P5A 500 169 + 170 + #define LOW_BAT_3P1V 0x20 171 + #define LOW_BAT_2P3V 0x00 172 + #define LOW_BAT_RESET 0x01 173 + #define LOW_BAT_ENABLE 0x01 174 + 175 + /* Backup battery constants */ 176 + #define BUP_ICH_SEL_50UA 0x00 177 + #define BUP_ICH_SEL_150UA 0x04 178 + #define BUP_ICH_SEL_300UA 0x08 179 + #define BUP_ICH_SEL_700UA 0x0C 180 + 181 + #define BUP_VCH_SEL_2P5V 0x00 182 + #define BUP_VCH_SEL_2P6V 0x01 183 + #define BUP_VCH_SEL_2P8V 0x02 184 + #define BUP_VCH_SEL_3P1V 0x03 185 + 186 + /* Battery OVV constants */ 187 + #define BATT_OVV_ENA 0x02 188 + #define BATT_OVV_TH_3P7 0x00 189 + #define BATT_OVV_TH_4P75 0x01 190 + 191 + /* A value to indicate over voltage */ 192 + #define BATT_OVV_VALUE 4750 193 + 194 + /* VBUS OVV constants */ 195 + #define VBUS_OVV_SELECT_MASK 0x78 196 + #define VBUS_OVV_SELECT_5P6V 0x00 197 + #define VBUS_OVV_SELECT_5P7V 0x08 198 + #define VBUS_OVV_SELECT_5P8V 0x10 199 + #define VBUS_OVV_SELECT_5P9V 0x18 200 + #define VBUS_OVV_SELECT_6P0V 0x20 201 + #define VBUS_OVV_SELECT_6P1V 0x28 202 + #define VBUS_OVV_SELECT_6P2V 0x30 203 + #define VBUS_OVV_SELECT_6P3V 0x38 204 + 205 + #define VBUS_AUTO_IN_CURR_LIM_ENA 0x04 206 + 207 + /* Fuel Gauge constants */ 208 + #define RESET_ACCU 0x02 209 + #define READ_REQ 0x01 210 + #define CC_DEEP_SLEEP_ENA 0x02 211 + #define CC_PWR_UP_ENA 0x01 212 + #define CC_SAMPLES_40 0x28 213 + #define RD_NCONV_ACCU_REQ 0x01 214 + #define CC_CALIB 0x08 215 + #define CC_INTAVGOFFSET_ENA 0x10 216 + #define CC_MUXOFFSET 0x80 217 + #define CC_INT_CAL_N_AVG_MASK 0x60 218 + #define CC_INT_CAL_SAMPLES_16 0x40 219 + #define CC_INT_CAL_SAMPLES_8 0x20 220 + #define CC_INT_CAL_SAMPLES_4 0x00 221 + 222 + /* RTC constants */ 223 + #define RTC_BUP_CH_ENA 0x10 224 + 225 + /* BatCtrl Current Source Constants */ 226 + #define BAT_CTRL_7U_ENA 0x01 227 + #define BAT_CTRL_20U_ENA 0x02 228 + #define BAT_CTRL_CMP_ENA 0x04 229 + #define FORCE_BAT_CTRL_CMP_HIGH 0x08 230 + #define BAT_CTRL_PULL_UP_ENA 0x10 231 + 232 + /* Battery type */ 233 + #define BATTERY_UNKNOWN 00 234 + 235 + /** 236 + * struct res_to_temp - defines one point in a temp to res curve. To 237 + * be used in battery packs that combines the identification resistor with a 238 + * NTC resistor. 239 + * @temp: battery pack temperature in Celcius 240 + * @resist: NTC resistor net total resistance 241 + */ 242 + struct res_to_temp { 243 + int temp; 244 + int resist; 245 + }; 246 + 247 + /** 248 + * struct batres_vs_temp - defines one point in a temp vs battery internal 249 + * resistance curve. 250 + * @temp: battery pack temperature in Celcius 251 + * @resist: battery internal reistance in mOhm 252 + */ 253 + struct batres_vs_temp { 254 + int temp; 255 + int resist; 256 + }; 257 + 258 + /* Forward declaration */ 259 + struct ab8500_fg; 260 + 261 + /** 262 + * struct ab8500_fg_parameters - Fuel gauge algorithm parameters, in seconds 263 + * if not specified 264 + * @recovery_sleep_timer: Time between measurements while recovering 265 + * @recovery_total_time: Total recovery time 266 + * @init_timer: Measurement interval during startup 267 + * @init_discard_time: Time we discard voltage measurement at startup 268 + * @init_total_time: Total init time during startup 269 + * @high_curr_time: Time current has to be high to go to recovery 270 + * @accu_charging: FG accumulation time while charging 271 + * @accu_high_curr: FG accumulation time in high current mode 272 + * @high_curr_threshold: High current threshold, in mA 273 + * @lowbat_threshold: Low battery threshold, in mV 274 + * @battok_falling_th_sel0 Threshold in mV for battOk signal sel0 275 + * Resolution in 50 mV step. 276 + * @battok_raising_th_sel1 Threshold in mV for battOk signal sel1 277 + * Resolution in 50 mV step. 278 + * @user_cap_limit Capacity reported from user must be within this 279 + * limit to be considered as sane, in percentage 280 + * points. 281 + * @maint_thres This is the threshold where we stop reporting 282 + * battery full while in maintenance, in per cent 283 + */ 284 + struct ab8500_fg_parameters { 285 + int recovery_sleep_timer; 286 + int recovery_total_time; 287 + int init_timer; 288 + int init_discard_time; 289 + int init_total_time; 290 + int high_curr_time; 291 + int accu_charging; 292 + int accu_high_curr; 293 + int high_curr_threshold; 294 + int lowbat_threshold; 295 + int battok_falling_th_sel0; 296 + int battok_raising_th_sel1; 297 + int user_cap_limit; 298 + int maint_thres; 299 + }; 300 + 301 + /** 302 + * struct ab8500_charger_maximization - struct used by the board config. 303 + * @use_maxi: Enable maximization for this battery type 304 + * @maxi_chg_curr: Maximum charger current allowed 305 + * @maxi_wait_cycles: cycles to wait before setting charger current 306 + * @charger_curr_step delta between two charger current settings (mA) 307 + */ 308 + struct ab8500_maxim_parameters { 309 + bool ena_maxi; 310 + int chg_curr; 311 + int wait_cycles; 312 + int charger_curr_step; 313 + }; 314 + 315 + /** 316 + * struct ab8500_bm_capacity_levels - ab8500 capacity level data 317 + * @critical: critical capacity level in percent 318 + * @low: low capacity level in percent 319 + * @normal: normal capacity level in percent 320 + * @high: high capacity level in percent 321 + * @full: full capacity level in percent 322 + */ 323 + struct ab8500_bm_capacity_levels { 324 + int critical; 325 + int low; 326 + int normal; 327 + int high; 328 + int full; 329 + }; 330 + 331 + /** 332 + * struct ab8500_bm_charger_parameters - Charger specific parameters 333 + * @usb_volt_max: maximum allowed USB charger voltage in mV 334 + * @usb_curr_max: maximum allowed USB charger current in mA 335 + * @ac_volt_max: maximum allowed AC charger voltage in mV 336 + * @ac_curr_max: maximum allowed AC charger current in mA 337 + */ 338 + struct ab8500_bm_charger_parameters { 339 + int usb_volt_max; 340 + int usb_curr_max; 341 + int ac_volt_max; 342 + int ac_curr_max; 343 + }; 344 + 345 + /** 346 + * struct ab8500_bm_data - ab8500 battery management data 347 + * @temp_under under this temp, charging is stopped 348 + * @temp_low between this temp and temp_under charging is reduced 349 + * @temp_high between this temp and temp_over charging is reduced 350 + * @temp_over over this temp, charging is stopped 351 + * @temp_interval_chg temperature measurement interval in s when charging 352 + * @temp_interval_nochg temperature measurement interval in s when not charging 353 + * @main_safety_tmr_h safety timer for main charger 354 + * @usb_safety_tmr_h safety timer for usb charger 355 + * @bkup_bat_v voltage which we charge the backup battery with 356 + * @bkup_bat_i current which we charge the backup battery with 357 + * @no_maintenance indicates that maintenance charging is disabled 358 + * @adc_therm placement of thermistor, batctrl or battemp adc 359 + * @chg_unknown_bat flag to enable charging of unknown batteries 360 + * @enable_overshoot flag to enable VBAT overshoot control 361 + * @fg_res resistance of FG resistor in 0.1mOhm 362 + * @n_btypes number of elements in array bat_type 363 + * @batt_id index of the identified battery in array bat_type 364 + * @interval_charging charge alg cycle period time when charging (sec) 365 + * @interval_not_charging charge alg cycle period time when not charging (sec) 366 + * @temp_hysteresis temperature hysteresis 367 + * @gnd_lift_resistance Battery ground to phone ground resistance (mOhm) 368 + * @maxi: maximization parameters 369 + * @cap_levels capacity in percent for the different capacity levels 370 + * @bat_type table of supported battery types 371 + * @chg_params charger parameters 372 + * @fg_params fuel gauge parameters 373 + */ 374 + struct ab8500_bm_data { 375 + int temp_under; 376 + int temp_low; 377 + int temp_high; 378 + int temp_over; 379 + int temp_interval_chg; 380 + int temp_interval_nochg; 381 + int main_safety_tmr_h; 382 + int usb_safety_tmr_h; 383 + int bkup_bat_v; 384 + int bkup_bat_i; 385 + bool no_maintenance; 386 + bool chg_unknown_bat; 387 + bool enable_overshoot; 388 + enum abx500_adc_therm adc_therm; 389 + int fg_res; 390 + int n_btypes; 391 + int batt_id; 392 + int interval_charging; 393 + int interval_not_charging; 394 + int temp_hysteresis; 395 + int gnd_lift_resistance; 396 + const struct ab8500_maxim_parameters *maxi; 397 + const struct ab8500_bm_capacity_levels *cap_levels; 398 + const struct ab8500_bm_charger_parameters *chg_params; 399 + const struct ab8500_fg_parameters *fg_params; 400 + }; 401 + 402 + struct ab8500_charger_platform_data { 403 + char **supplied_to; 404 + size_t num_supplicants; 405 + bool autopower_cfg; 406 + }; 407 + 408 + struct ab8500_btemp_platform_data { 409 + char **supplied_to; 410 + size_t num_supplicants; 411 + }; 412 + 413 + struct ab8500_fg_platform_data { 414 + char **supplied_to; 415 + size_t num_supplicants; 416 + }; 417 + 418 + struct ab8500_chargalg_platform_data { 419 + char **supplied_to; 420 + size_t num_supplicants; 421 + }; 422 + struct ab8500_btemp; 423 + struct ab8500_gpadc; 424 + struct ab8500_fg; 425 + #ifdef CONFIG_AB8500_BM 426 + void ab8500_fg_reinit(void); 427 + void ab8500_charger_usb_state_changed(u8 bm_usb_state, u16 mA); 428 + struct ab8500_btemp *ab8500_btemp_get(void); 429 + int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp); 430 + struct ab8500_fg *ab8500_fg_get(void); 431 + int ab8500_fg_inst_curr_blocking(struct ab8500_fg *dev); 432 + int ab8500_fg_inst_curr_start(struct ab8500_fg *di); 433 + int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res); 434 + int ab8500_fg_inst_curr_done(struct ab8500_fg *di); 435 + 436 + #else 437 + int ab8500_fg_inst_curr_done(struct ab8500_fg *di) 438 + { 439 + } 440 + static void ab8500_fg_reinit(void) 441 + { 442 + } 443 + static void ab8500_charger_usb_state_changed(u8 bm_usb_state, u16 mA) 444 + { 445 + } 446 + static struct ab8500_btemp *ab8500_btemp_get(void) 447 + { 448 + return NULL; 449 + } 450 + static int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp) 451 + { 452 + return 0; 453 + } 454 + struct ab8500_fg *ab8500_fg_get(void) 455 + { 456 + return NULL; 457 + } 458 + static int ab8500_fg_inst_curr_blocking(struct ab8500_fg *dev) 459 + { 460 + return -ENODEV; 461 + } 462 + 463 + static inline int ab8500_fg_inst_curr_start(struct ab8500_fg *di) 464 + { 465 + return -ENODEV; 466 + } 467 + 468 + static inline int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res) 469 + { 470 + return -ENODEV; 471 + } 472 + 473 + #endif 474 + #endif /* _AB8500_BM_H */
+38
include/linux/mfd/abx500/ux500_chargalg.h
··· 1 + /* 2 + * Copyright (C) ST-Ericsson SA 2012 3 + * Author: Johan Gardsmark <johan.gardsmark@stericsson.com> for ST-Ericsson. 4 + * License terms: GNU General Public License (GPL), version 2 5 + */ 6 + 7 + #ifndef _UX500_CHARGALG_H 8 + #define _UX500_CHARGALG_H 9 + 10 + #include <linux/power_supply.h> 11 + 12 + #define psy_to_ux500_charger(x) container_of((x), \ 13 + struct ux500_charger, psy) 14 + 15 + /* Forward declaration */ 16 + struct ux500_charger; 17 + 18 + struct ux500_charger_ops { 19 + int (*enable) (struct ux500_charger *, int, int, int); 20 + int (*kick_wd) (struct ux500_charger *); 21 + int (*update_curr) (struct ux500_charger *, int); 22 + }; 23 + 24 + /** 25 + * struct ux500_charger - power supply ux500 charger sub class 26 + * @psy power supply base class 27 + * @ops ux500 charger operations 28 + * @max_out_volt maximum output charger voltage in mV 29 + * @max_out_curr maximum output charger current in mA 30 + */ 31 + struct ux500_charger { 32 + struct power_supply psy; 33 + struct ux500_charger_ops ops; 34 + int max_out_volt; 35 + int max_out_curr; 36 + }; 37 + 38 + #endif
+84 -9
include/linux/power/max17042_battery.h
··· 27 27 #define MAX17042_BATTERY_FULL (100) 28 28 #define MAX17042_DEFAULT_SNS_RESISTOR (10000) 29 29 30 + #define MAX17042_CHARACTERIZATION_DATA_SIZE 48 31 + 30 32 enum max17042_register { 31 33 MAX17042_STATUS = 0x00, 32 34 MAX17042_VALRT_Th = 0x01, ··· 42 40 MAX17042_VCELL = 0x09, 43 41 MAX17042_Current = 0x0A, 44 42 MAX17042_AvgCurrent = 0x0B, 45 - MAX17042_Qresidual = 0x0C, 43 + 46 44 MAX17042_SOC = 0x0D, 47 45 MAX17042_AvSOC = 0x0E, 48 46 MAX17042_RemCap = 0x0F, 49 - MAX17402_FullCAP = 0x10, 47 + MAX17042_FullCAP = 0x10, 50 48 MAX17042_TTE = 0x11, 51 49 MAX17042_V_empty = 0x12, 52 50 ··· 64 62 MAX17042_AvCap = 0x1F, 65 63 MAX17042_ManName = 0x20, 66 64 MAX17042_DevName = 0x21, 67 - MAX17042_DevChem = 0x22, 68 65 66 + MAX17042_FullCAPNom = 0x23, 69 67 MAX17042_TempNom = 0x24, 70 - MAX17042_TempCold = 0x25, 68 + MAX17042_TempLim = 0x25, 71 69 MAX17042_TempHot = 0x26, 72 70 MAX17042_AIN = 0x27, 73 71 MAX17042_LearnCFG = 0x28, 74 - MAX17042_SHFTCFG = 0x29, 72 + MAX17042_FilterCFG = 0x29, 75 73 MAX17042_RelaxCFG = 0x2A, 76 74 MAX17042_MiscCFG = 0x2B, 77 75 MAX17042_TGAIN = 0x2C, ··· 79 77 MAX17042_CGAIN = 0x2E, 80 78 MAX17042_COFF = 0x2F, 81 79 82 - MAX17042_Q_empty = 0x33, 80 + MAX17042_MaskSOC = 0x32, 81 + MAX17042_SOC_empty = 0x33, 83 82 MAX17042_T_empty = 0x34, 84 83 84 + MAX17042_FullCAP0 = 0x35, 85 + MAX17042_LAvg_empty = 0x36, 86 + MAX17042_FCTC = 0x37, 85 87 MAX17042_RCOMP0 = 0x38, 86 88 MAX17042_TempCo = 0x39, 87 - MAX17042_Rx = 0x3A, 88 - MAX17042_T_empty0 = 0x3B, 89 + MAX17042_EmptyTempCo = 0x3A, 90 + MAX17042_K_empty0 = 0x3B, 89 91 MAX17042_TaskPeriod = 0x3C, 90 92 MAX17042_FSTAT = 0x3D, 91 93 92 94 MAX17042_SHDNTIMER = 0x3F, 93 95 94 - MAX17042_VFRemCap = 0x4A, 96 + MAX17042_dQacc = 0x45, 97 + MAX17042_dPacc = 0x46, 98 + 99 + MAX17042_VFSOC0 = 0x48, 95 100 96 101 MAX17042_QH = 0x4D, 97 102 MAX17042_QL = 0x4E, 103 + 104 + MAX17042_VFSOC0Enable = 0x60, 105 + MAX17042_MLOCKReg1 = 0x62, 106 + MAX17042_MLOCKReg2 = 0x63, 107 + 108 + MAX17042_MODELChrTbl = 0x80, 109 + 110 + MAX17042_OCV = 0xEE, 111 + 112 + MAX17042_OCVInternal = 0xFB, 113 + 114 + MAX17042_VFSOC = 0xFF, 98 115 }; 99 116 100 117 /* ··· 126 105 u16 data; 127 106 }; 128 107 108 + struct max17042_config_data { 109 + /* External current sense resistor value in milli-ohms */ 110 + u32 cur_sense_val; 111 + 112 + /* A/D measurement */ 113 + u16 tgain; /* 0x2C */ 114 + u16 toff; /* 0x2D */ 115 + u16 cgain; /* 0x2E */ 116 + u16 coff; /* 0x2F */ 117 + 118 + /* Alert / Status */ 119 + u16 valrt_thresh; /* 0x01 */ 120 + u16 talrt_thresh; /* 0x02 */ 121 + u16 soc_alrt_thresh; /* 0x03 */ 122 + u16 config; /* 0x01D */ 123 + u16 shdntimer; /* 0x03F */ 124 + 125 + /* App data */ 126 + u16 design_cap; /* 0x18 */ 127 + u16 ichgt_term; /* 0x1E */ 128 + 129 + /* MG3 config */ 130 + u16 at_rate; /* 0x04 */ 131 + u16 learn_cfg; /* 0x28 */ 132 + u16 filter_cfg; /* 0x29 */ 133 + u16 relax_cfg; /* 0x2A */ 134 + u16 misc_cfg; /* 0x2B */ 135 + u16 masksoc; /* 0x32 */ 136 + 137 + /* MG3 save and restore */ 138 + u16 fullcap; /* 0x10 */ 139 + u16 fullcapnom; /* 0x23 */ 140 + u16 socempty; /* 0x33 */ 141 + u16 lavg_empty; /* 0x36 */ 142 + u16 dqacc; /* 0x45 */ 143 + u16 dpacc; /* 0x46 */ 144 + 145 + /* Cell technology from power_supply.h */ 146 + u16 cell_technology; 147 + 148 + /* Cell Data */ 149 + u16 vempty; /* 0x12 */ 150 + u16 temp_nom; /* 0x24 */ 151 + u16 temp_lim; /* 0x25 */ 152 + u16 fctc; /* 0x37 */ 153 + u16 rcomp0; /* 0x38 */ 154 + u16 tcompc0; /* 0x39 */ 155 + u16 empty_tempco; /* 0x3A */ 156 + u16 kempty0; /* 0x3B */ 157 + u16 cell_char_tbl[MAX17042_CHARACTERIZATION_DATA_SIZE]; 158 + } __packed; 159 + 129 160 struct max17042_platform_data { 130 161 struct max17042_reg_data *init_data; 162 + struct max17042_config_data *config_data; 131 163 int num_init_data; /* Number of enties in init_data array */ 132 164 bool enable_current_sense; 165 + bool enable_por_init; /* Use POR init from Maxim appnote */ 133 166 134 167 /* 135 168 * R_sns in micro-ohms.
+117
include/linux/power/smb347-charger.h
··· 1 + /* 2 + * Summit Microelectronics SMB347 Battery Charger Driver 3 + * 4 + * Copyright (C) 2011, Intel Corporation 5 + * 6 + * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> 7 + * Mika Westerberg <mika.westerberg@linux.intel.com> 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License version 2 as 11 + * published by the Free Software Foundation. 12 + */ 13 + 14 + #ifndef SMB347_CHARGER_H 15 + #define SMB347_CHARGER_H 16 + 17 + #include <linux/types.h> 18 + #include <linux/power_supply.h> 19 + 20 + enum { 21 + /* use the default compensation method */ 22 + SMB347_SOFT_TEMP_COMPENSATE_DEFAULT = -1, 23 + 24 + SMB347_SOFT_TEMP_COMPENSATE_NONE, 25 + SMB347_SOFT_TEMP_COMPENSATE_CURRENT, 26 + SMB347_SOFT_TEMP_COMPENSATE_VOLTAGE, 27 + }; 28 + 29 + /* Use default factory programmed value for hard/soft temperature limit */ 30 + #define SMB347_TEMP_USE_DEFAULT -273 31 + 32 + /* 33 + * Charging enable can be controlled by software (via i2c) by 34 + * smb347-charger driver or by EN pin (active low/high). 35 + */ 36 + enum smb347_chg_enable { 37 + SMB347_CHG_ENABLE_SW, 38 + SMB347_CHG_ENABLE_PIN_ACTIVE_LOW, 39 + SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH, 40 + }; 41 + 42 + /** 43 + * struct smb347_charger_platform_data - platform data for SMB347 charger 44 + * @battery_info: Information about the battery 45 + * @max_charge_current: maximum current (in uA) the battery can be charged 46 + * @max_charge_voltage: maximum voltage (in uV) the battery can be charged 47 + * @pre_charge_current: current (in uA) to use in pre-charging phase 48 + * @termination_current: current (in uA) used to determine when the 49 + * charging cycle terminates 50 + * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to 51 + * pre-charge to fast charge mode 52 + * @mains_current_limit: maximum input current drawn from AC/DC input (in uA) 53 + * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB 54 + * input 55 + * @chip_temp_threshold: die temperature where device starts limiting charge 56 + * current [%100 - %130] (in degree C) 57 + * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C), 58 + * granularity is 5 deg C. 59 + * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C), 60 + * granularity is 5 deg C. 61 + * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C), 62 + * granularity is 5 deg C. 63 + * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C), 64 + * granularity is 5 deg C. 65 + * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit 66 + * @soft_temp_limit_compensation: compensation method when soft temperature 67 + * limit is hit 68 + * @charge_current_compensation: current (in uA) for charging compensation 69 + * current when temperature hits soft limits 70 + * @use_mains: AC/DC input can be used 71 + * @use_usb: USB input can be used 72 + * @use_usb_otg: USB OTG output can be used (not implemented yet) 73 + * @irq_gpio: GPIO number used for interrupts (%-1 if not used) 74 + * @enable_control: how charging enable/disable is controlled 75 + * (driver/pin controls) 76 + * 77 + * @use_main, @use_usb, and @use_usb_otg are means to enable/disable 78 + * hardware support for these. This is useful when we want to have for 79 + * example OTG charging controlled via OTG transceiver driver and not by 80 + * the SMB347 hardware. 81 + * 82 + * Hard and soft temperature limit values are given as described in the 83 + * device data sheet and assuming NTC beta value is %3750. Even if this is 84 + * not the case, these values should be used. They can be mapped to the 85 + * corresponding NTC beta values with the help of table %2 in the data 86 + * sheet. So for example if NTC beta is %3375 and we want to program hard 87 + * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50. 88 + * 89 + * If zero value is given in any of the current and voltage values, the 90 + * factory programmed default will be used. For soft/hard temperature 91 + * values, pass in %SMB347_TEMP_USE_DEFAULT instead. 92 + */ 93 + struct smb347_charger_platform_data { 94 + struct power_supply_info battery_info; 95 + unsigned int max_charge_current; 96 + unsigned int max_charge_voltage; 97 + unsigned int pre_charge_current; 98 + unsigned int termination_current; 99 + unsigned int pre_to_fast_voltage; 100 + unsigned int mains_current_limit; 101 + unsigned int usb_hc_current_limit; 102 + unsigned int chip_temp_threshold; 103 + int soft_cold_temp_limit; 104 + int soft_hot_temp_limit; 105 + int hard_cold_temp_limit; 106 + int hard_hot_temp_limit; 107 + bool suspend_on_hard_temp_limit; 108 + unsigned int soft_temp_limit_compensation; 109 + unsigned int charge_current_compensation; 110 + bool use_mains; 111 + bool use_usb; 112 + bool use_usb_otg; 113 + int irq_gpio; 114 + enum smb347_chg_enable enable_control; 115 + }; 116 + 117 + #endif /* SMB347_CHARGER_H */