diff options
author | NeilBrown <neil@brown.name> | 2015-07-29 20:11:24 -0400 |
---|---|---|
committer | Sebastian Reichel <sre@kernel.org> | 2015-08-04 23:14:43 -0400 |
commit | 3b542f089dcbdcf1c21a01927fbc6d5116af01f6 (patch) | |
tree | ac8469ca7f95f1f8c477bd53cdbf79873f6ad939 | |
parent | 6e37ec8c77e3e6580cbb844dfeea0fb3970c0a35 (diff) |
twl4030_charger: split uA calculation into a function.
We will need this calculation in other places, so
create functions to map between register value and uA value.
Acked-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: NeilBrown <neil@brown.name>
Signed-off-by: Sebastian Reichel <sre@kernel.org>
-rw-r--r-- | drivers/power/twl4030_charger.c | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/drivers/power/twl4030_charger.c b/drivers/power/twl4030_charger.c index a075216d65ed..29984b263a35 100644 --- a/drivers/power/twl4030_charger.c +++ b/drivers/power/twl4030_charger.c | |||
@@ -178,6 +178,40 @@ static int twl4030_is_battery_present(struct twl4030_bci *bci) | |||
178 | } | 178 | } |
179 | 179 | ||
180 | /* | 180 | /* |
181 | * TI provided formulas: | ||
182 | * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85 | ||
183 | * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7 | ||
184 | * Here we use integer approximation of: | ||
185 | * CGAIN == 0: val * 1.6618 - 0.85 * 1000 | ||
186 | * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2 | ||
187 | */ | ||
188 | /* | ||
189 | * convert twl register value for currents into uA | ||
190 | */ | ||
191 | static int regval2ua(int regval, bool cgain) | ||
192 | { | ||
193 | if (cgain) | ||
194 | return (regval * 16618 - 8500 * 1000) / 5; | ||
195 | else | ||
196 | return (regval * 16618 - 8500 * 1000) / 10; | ||
197 | } | ||
198 | |||
199 | /* | ||
200 | * convert uA currents into twl register value | ||
201 | */ | ||
202 | static int ua2regval(int ua, bool cgain) | ||
203 | { | ||
204 | int ret; | ||
205 | if (cgain) | ||
206 | ua /= 2; | ||
207 | ret = (ua * 10 + 8500 * 1000) / 16618; | ||
208 | /* rounding problems */ | ||
209 | if (ret < 512) | ||
210 | ret = 512; | ||
211 | return ret; | ||
212 | } | ||
213 | |||
214 | /* | ||
181 | * Enable/Disable USB Charge functionality. | 215 | * Enable/Disable USB Charge functionality. |
182 | */ | 216 | */ |
183 | static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable) | 217 | static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable) |
@@ -366,14 +400,6 @@ static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val, | |||
366 | return NOTIFY_OK; | 400 | return NOTIFY_OK; |
367 | } | 401 | } |
368 | 402 | ||
369 | /* | ||
370 | * TI provided formulas: | ||
371 | * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85 | ||
372 | * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7 | ||
373 | * Here we use integer approximation of: | ||
374 | * CGAIN == 0: val * 1.6618 - 0.85 | ||
375 | * CGAIN == 1: (val * 1.6618 - 0.85) * 2 | ||
376 | */ | ||
377 | static int twl4030_charger_get_current(void) | 403 | static int twl4030_charger_get_current(void) |
378 | { | 404 | { |
379 | int curr; | 405 | int curr; |
@@ -388,11 +414,7 @@ static int twl4030_charger_get_current(void) | |||
388 | if (ret) | 414 | if (ret) |
389 | return ret; | 415 | return ret; |
390 | 416 | ||
391 | ret = (curr * 16618 - 850 * 10000) / 10; | 417 | return regval2ua(curr, bcictl1 & TWL4030_CGAIN); |
392 | if (bcictl1 & TWL4030_CGAIN) | ||
393 | ret *= 2; | ||
394 | |||
395 | return ret; | ||
396 | } | 418 | } |
397 | 419 | ||
398 | /* | 420 | /* |