aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power/max17042_battery.c
diff options
context:
space:
mode:
authorRamakrishna Pallala <ramakrishna.pallala@intel.com>2011-11-25 19:11:15 -0500
committerAnton Vorontsov <cbouatmailru@gmail.com>2011-11-25 19:11:15 -0500
commit60a1f6e4462bb71b39543ddbca314886a55adb9d (patch)
treeb7e8216b6c35c8fa4423d33cb928666a2f11e371 /drivers/power/max17042_battery.c
parent86e6c6bd9b70260fc5bc9fdfa2e5b4dfd8fe545c (diff)
max17042_battery: Fix error handling
In max17042_get_property(...), the values returned by max17042_read_reg are directly assigned to the variables, even if the read results in an error. This patch checks for the return code from max17042_read_reg and exits the function if there is any error. Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com> Acked-by: MyungJoo Ham <myungjoo.ham@samsung.com> Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
Diffstat (limited to 'drivers/power/max17042_battery.c')
-rw-r--r--drivers/power/max17042_battery.c85
1 files changed, 58 insertions, 27 deletions
diff --git a/drivers/power/max17042_battery.c b/drivers/power/max17042_battery.c
index a6dc9c7a95e2..10ffa8c22d36 100644
--- a/drivers/power/max17042_battery.c
+++ b/drivers/power/max17042_battery.c
@@ -84,55 +84,79 @@ static int max17042_get_property(struct power_supply *psy,
84{ 84{
85 struct max17042_chip *chip = container_of(psy, 85 struct max17042_chip *chip = container_of(psy,
86 struct max17042_chip, battery); 86 struct max17042_chip, battery);
87 int ret;
87 88
88 switch (psp) { 89 switch (psp) {
89 case POWER_SUPPLY_PROP_PRESENT: 90 case POWER_SUPPLY_PROP_PRESENT:
90 val->intval = max17042_read_reg(chip->client, 91 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
91 MAX17042_STATUS); 92 if (ret < 0)
92 if (val->intval & MAX17042_STATUS_BattAbsent) 93 return ret;
94
95 if (ret & MAX17042_STATUS_BattAbsent)
93 val->intval = 0; 96 val->intval = 0;
94 else 97 else
95 val->intval = 1; 98 val->intval = 1;
96 break; 99 break;
97 case POWER_SUPPLY_PROP_CYCLE_COUNT: 100 case POWER_SUPPLY_PROP_CYCLE_COUNT:
98 val->intval = max17042_read_reg(chip->client, 101 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
99 MAX17042_Cycles); 102 if (ret < 0)
103 return ret;
104
105 val->intval = ret;
100 break; 106 break;
101 case POWER_SUPPLY_PROP_VOLTAGE_MAX: 107 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
102 val->intval = max17042_read_reg(chip->client, 108 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
103 MAX17042_MinMaxVolt); 109 if (ret < 0)
104 val->intval >>= 8; 110 return ret;
111
112 val->intval = ret >> 8;
105 val->intval *= 20000; /* Units of LSB = 20mV */ 113 val->intval *= 20000; /* Units of LSB = 20mV */
106 break; 114 break;
107 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 115 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
108 val->intval = max17042_read_reg(chip->client, 116 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
109 MAX17042_V_empty); 117 if (ret < 0)
110 val->intval >>= 7; 118 return ret;
119
120 val->intval = ret >> 7;
111 val->intval *= 10000; /* Units of LSB = 10mV */ 121 val->intval *= 10000; /* Units of LSB = 10mV */
112 break; 122 break;
113 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 123 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
114 val->intval = max17042_read_reg(chip->client, MAX17042_VCELL) 124 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
115 * 625 / 8; 125 if (ret < 0)
126 return ret;
127
128 val->intval = ret * 625 / 8;
116 break; 129 break;
117 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 130 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
118 val->intval = max17042_read_reg(chip->client, MAX17042_AvgVCELL) 131 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
119 * 625 / 8; 132 if (ret < 0)
133 return ret;
134
135 val->intval = ret * 625 / 8;
120 break; 136 break;
121 case POWER_SUPPLY_PROP_CAPACITY: 137 case POWER_SUPPLY_PROP_CAPACITY:
122 val->intval = max17042_read_reg(chip->client, 138 ret = max17042_read_reg(chip->client, MAX17042_SOC);
123 MAX17042_SOC) / 256; 139 if (ret < 0)
140 return ret;
141
142 val->intval = ret >> 8;
124 break; 143 break;
125 case POWER_SUPPLY_PROP_CHARGE_FULL: 144 case POWER_SUPPLY_PROP_CHARGE_FULL:
126 val->intval = max17042_read_reg(chip->client, 145 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
127 MAX17042_RepSOC); 146 if (ret < 0)
128 if ((val->intval / 256) >= MAX17042_BATTERY_FULL) 147 return ret;
148
149 if ((ret >> 8) >= MAX17042_BATTERY_FULL)
129 val->intval = 1; 150 val->intval = 1;
130 else if (val->intval >= 0) 151 else if (ret >= 0)
131 val->intval = 0; 152 val->intval = 0;
132 break; 153 break;
133 case POWER_SUPPLY_PROP_TEMP: 154 case POWER_SUPPLY_PROP_TEMP:
134 val->intval = max17042_read_reg(chip->client, 155 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
135 MAX17042_TEMP); 156 if (ret < 0)
157 return ret;
158
159 val->intval = ret;
136 /* The value is signed. */ 160 /* The value is signed. */
137 if (val->intval & 0x8000) { 161 if (val->intval & 0x8000) {
138 val->intval = (0x7fff & ~val->intval) + 1; 162 val->intval = (0x7fff & ~val->intval) + 1;
@@ -144,8 +168,11 @@ static int max17042_get_property(struct power_supply *psy,
144 break; 168 break;
145 case POWER_SUPPLY_PROP_CURRENT_NOW: 169 case POWER_SUPPLY_PROP_CURRENT_NOW:
146 if (chip->pdata->enable_current_sense) { 170 if (chip->pdata->enable_current_sense) {
147 val->intval = max17042_read_reg(chip->client, 171 ret = max17042_read_reg(chip->client, MAX17042_Current);
148 MAX17042_Current); 172 if (ret < 0)
173 return ret;
174
175 val->intval = ret;
149 if (val->intval & 0x8000) { 176 if (val->intval & 0x8000) {
150 /* Negative */ 177 /* Negative */
151 val->intval = ~val->intval & 0x7fff; 178 val->intval = ~val->intval & 0x7fff;
@@ -159,8 +186,12 @@ static int max17042_get_property(struct power_supply *psy,
159 break; 186 break;
160 case POWER_SUPPLY_PROP_CURRENT_AVG: 187 case POWER_SUPPLY_PROP_CURRENT_AVG:
161 if (chip->pdata->enable_current_sense) { 188 if (chip->pdata->enable_current_sense) {
162 val->intval = max17042_read_reg(chip->client, 189 ret = max17042_read_reg(chip->client,
163 MAX17042_AvgCurrent); 190 MAX17042_AvgCurrent);
191 if (ret < 0)
192 return ret;
193
194 val->intval = ret;
164 if (val->intval & 0x8000) { 195 if (val->intval & 0x8000) {
165 /* Negative */ 196 /* Negative */
166 val->intval = ~val->intval & 0x7fff; 197 val->intval = ~val->intval & 0x7fff;