diff options
author | Heiko Stübner <heiko@sntech.de> | 2011-12-29 06:52:07 -0500 |
---|---|---|
committer | Anton Vorontsov <cbouatmailru@gmail.com> | 2012-01-05 21:13:35 -0500 |
commit | 34aed73df3a9e75e313a7510b201f6755ae3e6bc (patch) | |
tree | 3ded8d8b8019eb5b7eff82ab1e4267ad5df789ab /drivers/power | |
parent | ded7fc7b055055427d0d04958482dbe3d23e087d (diff) |
s3c_adc_battery: Average over more than one adc sample
Some sources for adc battery information provide only inaccurate results
where the read value differs from the real value with positive and negative
offsets. For such sources it can be more accurate to collect two or more
value sample and use the average of all collected values.
This patch adds pdata options volt_samples, current_samples and
backup_volt_samples to specifiy the number of samples to collect,
reads the specified number of samples and calculates the average of those.
For unset sample-number-values a default of 1 is assumed.
Signed-off-by: Heiko Stuebner <heiko@sntech.de>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
Diffstat (limited to 'drivers/power')
-rw-r--r-- | drivers/power/s3c_adc_battery.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/drivers/power/s3c_adc_battery.c b/drivers/power/s3c_adc_battery.c index e687ee7f18f2..8b804a566756 100644 --- a/drivers/power/s3c_adc_battery.c +++ b/drivers/power/s3c_adc_battery.c | |||
@@ -47,6 +47,22 @@ static void s3c_adc_bat_ext_power_changed(struct power_supply *psy) | |||
47 | msecs_to_jiffies(JITTER_DELAY)); | 47 | msecs_to_jiffies(JITTER_DELAY)); |
48 | } | 48 | } |
49 | 49 | ||
50 | static int gather_samples(struct s3c_adc_client *client, int num, int channel) | ||
51 | { | ||
52 | int value, i; | ||
53 | |||
54 | /* default to 1 if nothing is set */ | ||
55 | if (num < 1) | ||
56 | num = 1; | ||
57 | |||
58 | value = 0; | ||
59 | for (i = 0; i < num; i++) | ||
60 | value += s3c_adc_read(client, channel); | ||
61 | value /= num; | ||
62 | |||
63 | return value; | ||
64 | } | ||
65 | |||
50 | static enum power_supply_property s3c_adc_backup_bat_props[] = { | 66 | static enum power_supply_property s3c_adc_backup_bat_props[] = { |
51 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | 67 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
52 | POWER_SUPPLY_PROP_VOLTAGE_MIN, | 68 | POWER_SUPPLY_PROP_VOLTAGE_MIN, |
@@ -67,7 +83,8 @@ static int s3c_adc_backup_bat_get_property(struct power_supply *psy, | |||
67 | if (bat->volt_value < 0 || | 83 | if (bat->volt_value < 0 || |
68 | jiffies_to_msecs(jiffies - bat->timestamp) > | 84 | jiffies_to_msecs(jiffies - bat->timestamp) > |
69 | BAT_POLL_INTERVAL) { | 85 | BAT_POLL_INTERVAL) { |
70 | bat->volt_value = s3c_adc_read(bat->client, | 86 | bat->volt_value = gather_samples(bat->client, |
87 | bat->pdata->backup_volt_samples, | ||
71 | bat->pdata->backup_volt_channel); | 88 | bat->pdata->backup_volt_channel); |
72 | bat->volt_value *= bat->pdata->backup_volt_mult; | 89 | bat->volt_value *= bat->pdata->backup_volt_mult; |
73 | bat->timestamp = jiffies; | 90 | bat->timestamp = jiffies; |
@@ -139,9 +156,11 @@ static int s3c_adc_bat_get_property(struct power_supply *psy, | |||
139 | if (bat->volt_value < 0 || bat->cur_value < 0 || | 156 | if (bat->volt_value < 0 || bat->cur_value < 0 || |
140 | jiffies_to_msecs(jiffies - bat->timestamp) > | 157 | jiffies_to_msecs(jiffies - bat->timestamp) > |
141 | BAT_POLL_INTERVAL) { | 158 | BAT_POLL_INTERVAL) { |
142 | bat->volt_value = s3c_adc_read(bat->client, | 159 | bat->volt_value = gather_samples(bat->client, |
160 | bat->pdata->volt_samples, | ||
143 | bat->pdata->volt_channel) * bat->pdata->volt_mult; | 161 | bat->pdata->volt_channel) * bat->pdata->volt_mult; |
144 | bat->cur_value = s3c_adc_read(bat->client, | 162 | bat->cur_value = gather_samples(bat->client, |
163 | bat->pdata->current_samples, | ||
145 | bat->pdata->current_channel) * bat->pdata->current_mult; | 164 | bat->pdata->current_channel) * bat->pdata->current_mult; |
146 | bat->timestamp = jiffies; | 165 | bat->timestamp = jiffies; |
147 | } | 166 | } |