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

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>

authored by

Heiko Stübner and committed by
Anton Vorontsov
34aed73d ded7fc7b

+26 -3
+22 -3
drivers/power/s3c_adc_battery.c
··· 47 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 66 static enum power_supply_property s3c_adc_backup_bat_props[] = { 51 67 POWER_SUPPLY_PROP_VOLTAGE_NOW, 52 68 POWER_SUPPLY_PROP_VOLTAGE_MIN, ··· 83 67 if (bat->volt_value < 0 || 84 68 jiffies_to_msecs(jiffies - bat->timestamp) > 85 69 BAT_POLL_INTERVAL) { 86 - bat->volt_value = s3c_adc_read(bat->client, 70 + bat->volt_value = gather_samples(bat->client, 71 + bat->pdata->backup_volt_samples, 87 72 bat->pdata->backup_volt_channel); 88 73 bat->volt_value *= bat->pdata->backup_volt_mult; 89 74 bat->timestamp = jiffies; ··· 156 139 if (bat->volt_value < 0 || bat->cur_value < 0 || 157 140 jiffies_to_msecs(jiffies - bat->timestamp) > 158 141 BAT_POLL_INTERVAL) { 159 - bat->volt_value = s3c_adc_read(bat->client, 142 + bat->volt_value = gather_samples(bat->client, 143 + bat->pdata->volt_samples, 160 144 bat->pdata->volt_channel) * bat->pdata->volt_mult; 161 - bat->cur_value = s3c_adc_read(bat->client, 145 + bat->cur_value = gather_samples(bat->client, 146 + bat->pdata->current_samples, 162 147 bat->pdata->current_channel) * bat->pdata->current_mult; 163 148 bat->timestamp = jiffies; 164 149 }
+4
include/linux/s3c_adc_battery.h
··· 25 25 const unsigned int current_channel; 26 26 const unsigned int backup_volt_channel; 27 27 28 + const unsigned int volt_samples; 29 + const unsigned int current_samples; 30 + const unsigned int backup_volt_samples; 31 + 28 32 const unsigned int volt_mult; 29 33 const unsigned int current_mult; 30 34 const unsigned int backup_volt_mult;