]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
a7640bfa | 2 | /* |
3ddca062 | 3 | * Gas Gauge driver for SBS Compliant Batteries |
a7640bfa RK |
4 | * |
5 | * Copyright (c) 2010, NVIDIA Corporation. | |
a7640bfa RK |
6 | */ |
7 | ||
e2ec6aef | 8 | #include <linux/bits.h> |
adcf04c9 | 9 | #include <linux/delay.h> |
6d0c5de2 | 10 | #include <linux/devm-helpers.h> |
a7640bfa | 11 | #include <linux/err.h> |
b70f0a28 | 12 | #include <linux/gpio/consumer.h> |
a7640bfa | 13 | #include <linux/i2c.h> |
b70f0a28 | 14 | #include <linux/init.h> |
bb879101 | 15 | #include <linux/interrupt.h> |
b70f0a28 PR |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | |
03b758ba | 18 | #include <linux/property.h> |
76b16f4c | 19 | #include <linux/of_device.h> |
3ddca062 | 20 | #include <linux/power/sbs-battery.h> |
b70f0a28 PR |
21 | #include <linux/power_supply.h> |
22 | #include <linux/slab.h> | |
23 | #include <linux/stat.h> | |
a7640bfa RK |
24 | |
25 | enum { | |
26 | REG_MANUFACTURER_DATA, | |
6f72a07a | 27 | REG_BATTERY_MODE, |
a7640bfa RK |
28 | REG_TEMPERATURE, |
29 | REG_VOLTAGE, | |
8ce6ee43 SR |
30 | REG_CURRENT_NOW, |
31 | REG_CURRENT_AVG, | |
d6f56321 | 32 | REG_MAX_ERR, |
a7640bfa RK |
33 | REG_CAPACITY, |
34 | REG_TIME_TO_EMPTY, | |
35 | REG_TIME_TO_FULL, | |
36 | REG_STATUS, | |
957cb720 | 37 | REG_CAPACITY_LEVEL, |
a7640bfa | 38 | REG_CYCLE_COUNT, |
d3ab61ec RK |
39 | REG_SERIAL_NUMBER, |
40 | REG_REMAINING_CAPACITY, | |
51d07566 | 41 | REG_REMAINING_CAPACITY_CHARGE, |
d3ab61ec | 42 | REG_FULL_CHARGE_CAPACITY, |
51d07566 | 43 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
d3ab61ec | 44 | REG_DESIGN_CAPACITY, |
51d07566 | 45 | REG_DESIGN_CAPACITY_CHARGE, |
4495b0ad SQ |
46 | REG_DESIGN_VOLTAGE_MIN, |
47 | REG_DESIGN_VOLTAGE_MAX, | |
3e9544f7 | 48 | REG_CHEMISTRY, |
9ea89402 CYC |
49 | REG_MANUFACTURER, |
50 | REG_MODEL_NAME, | |
787fdbcf SR |
51 | REG_CHARGE_CURRENT, |
52 | REG_CHARGE_VOLTAGE, | |
a7640bfa RK |
53 | }; |
54 | ||
7222bd60 SR |
55 | #define REG_ADDR_SPEC_INFO 0x1A |
56 | #define SPEC_INFO_VERSION_MASK GENMASK(7, 4) | |
57 | #define SPEC_INFO_VERSION_SHIFT 4 | |
58 | ||
59 | #define SBS_VERSION_1_0 1 | |
60 | #define SBS_VERSION_1_1 2 | |
61 | #define SBS_VERSION_1_1_WITH_PEC 3 | |
62 | ||
7721c2fd SR |
63 | #define REG_ADDR_MANUFACTURE_DATE 0x1B |
64 | ||
51d07566 RK |
65 | /* Battery Mode defines */ |
66 | #define BATTERY_MODE_OFFSET 0x03 | |
e2ec6aef JFD |
67 | #define BATTERY_MODE_CAPACITY_MASK BIT(15) |
68 | enum sbs_capacity_mode { | |
69 | CAPACITY_MODE_AMPS = 0, | |
70 | CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK | |
51d07566 | 71 | }; |
182fc882 | 72 | #define BATTERY_MODE_CHARGER_MASK (1<<14) |
51d07566 | 73 | |
a7640bfa RK |
74 | /* manufacturer access defines */ |
75 | #define MANUFACTURER_ACCESS_STATUS 0x0006 | |
76 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 | |
77 | ||
78 | /* battery status value bits */ | |
957cb720 | 79 | #define BATTERY_INITIALIZED 0x80 |
d3ab61ec | 80 | #define BATTERY_DISCHARGING 0x40 |
a7640bfa RK |
81 | #define BATTERY_FULL_CHARGED 0x20 |
82 | #define BATTERY_FULL_DISCHARGED 0x10 | |
83 | ||
9ea89402 | 84 | /* min_value and max_value are only valid for numerical data */ |
3ddca062 | 85 | #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ |
a7640bfa RK |
86 | .psp = _psp, \ |
87 | .addr = _addr, \ | |
88 | .min_value = _min_value, \ | |
89 | .max_value = _max_value, \ | |
90 | } | |
91 | ||
3ddca062 | 92 | static const struct chip_data { |
a7640bfa RK |
93 | enum power_supply_property psp; |
94 | u8 addr; | |
95 | int min_value; | |
96 | int max_value; | |
3ddca062 | 97 | } sbs_data[] = { |
a7640bfa | 98 | [REG_MANUFACTURER_DATA] = |
3ddca062 | 99 | SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
6f72a07a SR |
100 | [REG_BATTERY_MODE] = |
101 | SBS_DATA(-1, 0x03, 0, 65535), | |
a7640bfa | 102 | [REG_TEMPERATURE] = |
3ddca062 | 103 | SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
a7640bfa | 104 | [REG_VOLTAGE] = |
e11544d0 | 105 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 65535), |
8ce6ee43 | 106 | [REG_CURRENT_NOW] = |
3ddca062 | 107 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), |
8ce6ee43 SR |
108 | [REG_CURRENT_AVG] = |
109 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767), | |
d6f56321 SR |
110 | [REG_MAX_ERR] = |
111 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100), | |
a7640bfa | 112 | [REG_CAPACITY] = |
b1f092f6 | 113 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), |
d3ab61ec | 114 | [REG_REMAINING_CAPACITY] = |
3ddca062 | 115 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
51d07566 | 116 | [REG_REMAINING_CAPACITY_CHARGE] = |
3ddca062 | 117 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
d3ab61ec | 118 | [REG_FULL_CHARGE_CAPACITY] = |
3ddca062 | 119 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
51d07566 | 120 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
3ddca062 | 121 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
a7640bfa | 122 | [REG_TIME_TO_EMPTY] = |
3ddca062 | 123 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), |
a7640bfa | 124 | [REG_TIME_TO_FULL] = |
3ddca062 | 125 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), |
787fdbcf SR |
126 | [REG_CHARGE_CURRENT] = |
127 | SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535), | |
128 | [REG_CHARGE_VOLTAGE] = | |
129 | SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535), | |
a7640bfa | 130 | [REG_STATUS] = |
3ddca062 | 131 | SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
957cb720 JC |
132 | [REG_CAPACITY_LEVEL] = |
133 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535), | |
a7640bfa | 134 | [REG_CYCLE_COUNT] = |
3ddca062 | 135 | SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
d3ab61ec | 136 | [REG_DESIGN_CAPACITY] = |
3ddca062 | 137 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), |
51d07566 | 138 | [REG_DESIGN_CAPACITY_CHARGE] = |
3ddca062 | 139 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), |
4495b0ad SQ |
140 | [REG_DESIGN_VOLTAGE_MIN] = |
141 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), | |
142 | [REG_DESIGN_VOLTAGE_MAX] = | |
3ddca062 | 143 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), |
a7640bfa | 144 | [REG_SERIAL_NUMBER] = |
3ddca062 | 145 | SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
9ea89402 CYC |
146 | /* Properties of type `const char *' */ |
147 | [REG_MANUFACTURER] = | |
148 | SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), | |
149 | [REG_MODEL_NAME] = | |
3e9544f7 SR |
150 | SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535), |
151 | [REG_CHEMISTRY] = | |
152 | SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535) | |
a7640bfa RK |
153 | }; |
154 | ||
68956dbe | 155 | static const enum power_supply_property sbs_properties[] = { |
a7640bfa | 156 | POWER_SUPPLY_PROP_STATUS, |
957cb720 | 157 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
a7640bfa RK |
158 | POWER_SUPPLY_PROP_HEALTH, |
159 | POWER_SUPPLY_PROP_PRESENT, | |
160 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
161 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
162 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
163 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
8ce6ee43 | 164 | POWER_SUPPLY_PROP_CURRENT_AVG, |
a7640bfa | 165 | POWER_SUPPLY_PROP_CAPACITY, |
d6f56321 | 166 | POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, |
a7640bfa RK |
167 | POWER_SUPPLY_PROP_TEMP, |
168 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, | |
169 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, | |
170 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
4495b0ad | 171 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
d3ab61ec RK |
172 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
173 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
174 | POWER_SUPPLY_PROP_ENERGY_FULL, | |
175 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, | |
51d07566 RK |
176 | POWER_SUPPLY_PROP_CHARGE_NOW, |
177 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
178 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
787fdbcf SR |
179 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
180 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, | |
7721c2fd SR |
181 | POWER_SUPPLY_PROP_MANUFACTURE_YEAR, |
182 | POWER_SUPPLY_PROP_MANUFACTURE_MONTH, | |
183 | POWER_SUPPLY_PROP_MANUFACTURE_DAY, | |
9ea89402 CYC |
184 | /* Properties of type `const char *' */ |
185 | POWER_SUPPLY_PROP_MANUFACTURER, | |
186 | POWER_SUPPLY_PROP_MODEL_NAME | |
a7640bfa RK |
187 | }; |
188 | ||
0ff96915 SR |
189 | /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */ |
190 | #define SBS_FLAGS_TI_BQ20ZX5 BIT(0) | |
76b16f4c | 191 | |
b49a81d0 IJ |
192 | static const enum power_supply_property string_properties[] = { |
193 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
194 | POWER_SUPPLY_PROP_MANUFACTURER, | |
195 | POWER_SUPPLY_PROP_MODEL_NAME, | |
196 | }; | |
197 | ||
198 | #define NR_STRING_BUFFERS ARRAY_SIZE(string_properties) | |
199 | ||
3ddca062 | 200 | struct sbs_info { |
bb879101 | 201 | struct i2c_client *client; |
297d716f | 202 | struct power_supply *power_supply; |
bb879101 | 203 | bool is_present; |
3b5dd3a4 | 204 | struct gpio_desc *gpio_detect; |
182fc882 | 205 | bool charger_broadcasts; |
58ddafae RK |
206 | int last_state; |
207 | int poll_time; | |
389958bb PR |
208 | u32 i2c_retry_count; |
209 | u32 poll_retry_count; | |
58ddafae | 210 | struct delayed_work work; |
fe8a6534 | 211 | struct mutex mode_lock; |
76b16f4c | 212 | u32 flags; |
b49a81d0 IJ |
213 | int technology; |
214 | char strings[NR_STRING_BUFFERS][I2C_SMBUS_BLOCK_MAX + 1]; | |
a7640bfa RK |
215 | }; |
216 | ||
b49a81d0 IJ |
217 | static char *sbs_get_string_buf(struct sbs_info *chip, |
218 | enum power_supply_property psp) | |
219 | { | |
220 | int i = 0; | |
221 | ||
222 | for (i = 0; i < NR_STRING_BUFFERS; i++) | |
223 | if (string_properties[i] == psp) | |
224 | return chip->strings[i]; | |
225 | ||
226 | return ERR_PTR(-EINVAL); | |
227 | } | |
228 | ||
229 | static void sbs_invalidate_cached_props(struct sbs_info *chip) | |
230 | { | |
231 | int i = 0; | |
232 | ||
233 | chip->technology = -1; | |
234 | ||
235 | for (i = 0; i < NR_STRING_BUFFERS; i++) | |
236 | chip->strings[i][0] = 0; | |
237 | } | |
238 | ||
f4ed950a | 239 | static bool force_load; |
9ea89402 | 240 | |
182fc882 JFD |
241 | static int sbs_read_word_data(struct i2c_client *client, u8 address); |
242 | static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value); | |
243 | ||
244 | static void sbs_disable_charger_broadcasts(struct sbs_info *chip) | |
245 | { | |
246 | int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET); | |
247 | if (val < 0) | |
248 | goto exit; | |
249 | ||
250 | val |= BATTERY_MODE_CHARGER_MASK; | |
251 | ||
252 | val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val); | |
253 | ||
254 | exit: | |
255 | if (val < 0) | |
256 | dev_err(&chip->client->dev, | |
257 | "Failed to disable charger broadcasting: %d\n", val); | |
258 | else | |
259 | dev_dbg(&chip->client->dev, "%s\n", __func__); | |
260 | } | |
261 | ||
79bcd5a4 SR |
262 | static int sbs_update_presence(struct sbs_info *chip, bool is_present) |
263 | { | |
7222bd60 SR |
264 | struct i2c_client *client = chip->client; |
265 | int retries = chip->i2c_retry_count; | |
266 | s32 ret = 0; | |
267 | u8 version; | |
268 | ||
79bcd5a4 SR |
269 | if (chip->is_present == is_present) |
270 | return 0; | |
271 | ||
272 | if (!is_present) { | |
273 | chip->is_present = false; | |
7222bd60 SR |
274 | /* Disable PEC when no device is present */ |
275 | client->flags &= ~I2C_CLIENT_PEC; | |
b49a81d0 | 276 | sbs_invalidate_cached_props(chip); |
79bcd5a4 SR |
277 | return 0; |
278 | } | |
279 | ||
7222bd60 SR |
280 | /* Check if device supports packet error checking and use it */ |
281 | while (retries > 0) { | |
282 | ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO); | |
283 | if (ret >= 0) | |
284 | break; | |
285 | ||
286 | /* | |
287 | * Some batteries trigger the detection pin before the | |
288 | * I2C bus is properly connected. This works around the | |
289 | * issue. | |
290 | */ | |
291 | msleep(100); | |
292 | ||
293 | retries--; | |
294 | } | |
295 | ||
296 | if (ret < 0) { | |
297 | dev_dbg(&client->dev, "failed to read spec info: %d\n", ret); | |
298 | ||
299 | /* fallback to old behaviour */ | |
300 | client->flags &= ~I2C_CLIENT_PEC; | |
301 | chip->is_present = true; | |
302 | ||
303 | return ret; | |
304 | } | |
305 | ||
306 | version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT; | |
307 | ||
308 | if (version == SBS_VERSION_1_1_WITH_PEC) | |
309 | client->flags |= I2C_CLIENT_PEC; | |
310 | else | |
311 | client->flags &= ~I2C_CLIENT_PEC; | |
312 | ||
e3f2396b SR |
313 | if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel") |
314 | && client->flags & I2C_CLIENT_PEC) { | |
315 | dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n"); | |
316 | client->flags &= ~I2C_CLIENT_PEC; | |
317 | } | |
318 | ||
7222bd60 SR |
319 | dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ? |
320 | "enabled" : "disabled"); | |
321 | ||
182fc882 JFD |
322 | if (!chip->is_present && is_present && !chip->charger_broadcasts) |
323 | sbs_disable_charger_broadcasts(chip); | |
324 | ||
79bcd5a4 SR |
325 | chip->is_present = true; |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
3ddca062 | 330 | static int sbs_read_word_data(struct i2c_client *client, u8 address) |
d3ab61ec | 331 | { |
3ddca062 | 332 | struct sbs_info *chip = i2c_get_clientdata(client); |
9410b7d7 | 333 | int retries = chip->i2c_retry_count; |
ff28fcef | 334 | s32 ret = 0; |
ff28fcef RK |
335 | |
336 | while (retries > 0) { | |
337 | ret = i2c_smbus_read_word_data(client, address); | |
338 | if (ret >= 0) | |
339 | break; | |
340 | retries--; | |
341 | } | |
d3ab61ec | 342 | |
d3ab61ec | 343 | if (ret < 0) { |
a7d9ace4 | 344 | dev_dbg(&client->dev, |
d3ab61ec RK |
345 | "%s: i2c read at address 0x%x failed\n", |
346 | __func__, address); | |
347 | return ret; | |
348 | } | |
ff28fcef | 349 | |
a1bbec72 | 350 | return ret; |
d3ab61ec RK |
351 | } |
352 | ||
05e04309 | 353 | static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values) |
9ea89402 CYC |
354 | { |
355 | struct sbs_info *chip = i2c_get_clientdata(client); | |
972eabb9 SR |
356 | s32 ret = 0, block_length = 0; |
357 | int retries_length, retries_block; | |
358 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; | |
9ea89402 | 359 | |
972eabb9 SR |
360 | retries_length = chip->i2c_retry_count; |
361 | retries_block = chip->i2c_retry_count; | |
362 | ||
7222bd60 SR |
363 | dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n" |
364 | "Fallback method does not support PEC.\n"); | |
05e04309 | 365 | |
972eabb9 | 366 | /* Adapter needs to support these two functions */ |
9ea89402 | 367 | if (!i2c_check_functionality(client->adapter, |
972eabb9 SR |
368 | I2C_FUNC_SMBUS_BYTE_DATA | |
369 | I2C_FUNC_SMBUS_I2C_BLOCK)){ | |
9ea89402 CYC |
370 | return -ENODEV; |
371 | } | |
372 | ||
972eabb9 SR |
373 | /* Get the length of block data */ |
374 | while (retries_length > 0) { | |
375 | ret = i2c_smbus_read_byte_data(client, address); | |
376 | if (ret >= 0) | |
377 | break; | |
378 | retries_length--; | |
379 | } | |
380 | ||
381 | if (ret < 0) { | |
382 | dev_dbg(&client->dev, | |
383 | "%s: i2c read at address 0x%x failed\n", | |
384 | __func__, address); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | /* block_length does not include NULL terminator */ | |
389 | block_length = ret; | |
390 | if (block_length > I2C_SMBUS_BLOCK_MAX) { | |
391 | dev_err(&client->dev, | |
392 | "%s: Returned block_length is longer than 0x%x\n", | |
393 | __func__, I2C_SMBUS_BLOCK_MAX); | |
394 | return -EINVAL; | |
395 | } | |
396 | ||
9ea89402 | 397 | /* Get the block data */ |
972eabb9 SR |
398 | while (retries_block > 0) { |
399 | ret = i2c_smbus_read_i2c_block_data( | |
400 | client, address, | |
401 | block_length + 1, block_buffer); | |
9ea89402 CYC |
402 | if (ret >= 0) |
403 | break; | |
972eabb9 | 404 | retries_block--; |
9ea89402 CYC |
405 | } |
406 | ||
407 | if (ret < 0) { | |
972eabb9 SR |
408 | dev_dbg(&client->dev, |
409 | "%s: i2c read at address 0x%x failed\n", | |
410 | __func__, address); | |
9ea89402 CYC |
411 | return ret; |
412 | } | |
413 | ||
972eabb9 SR |
414 | /* block_buffer[0] == block_length */ |
415 | memcpy(values, block_buffer + 1, block_length); | |
416 | values[block_length] = '\0'; | |
9ea89402 | 417 | |
972eabb9 | 418 | return ret; |
9ea89402 CYC |
419 | } |
420 | ||
05e04309 SR |
421 | static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values) |
422 | { | |
423 | struct sbs_info *chip = i2c_get_clientdata(client); | |
424 | int retries = chip->i2c_retry_count; | |
425 | int ret = 0; | |
426 | ||
7222bd60 SR |
427 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { |
428 | bool pec = client->flags & I2C_CLIENT_PEC; | |
429 | client->flags &= ~I2C_CLIENT_PEC; | |
430 | ret = sbs_read_string_data_fallback(client, address, values); | |
431 | if (pec) | |
432 | client->flags |= I2C_CLIENT_PEC; | |
433 | return ret; | |
434 | } | |
05e04309 SR |
435 | |
436 | while (retries > 0) { | |
437 | ret = i2c_smbus_read_block_data(client, address, values); | |
438 | if (ret >= 0) | |
439 | break; | |
440 | retries--; | |
441 | } | |
442 | ||
443 | if (ret < 0) { | |
444 | dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret); | |
445 | return ret; | |
446 | } | |
447 | ||
448 | /* add string termination */ | |
449 | values[ret] = '\0'; | |
450 | return ret; | |
451 | } | |
452 | ||
3ddca062 | 453 | static int sbs_write_word_data(struct i2c_client *client, u8 address, |
d3ab61ec RK |
454 | u16 value) |
455 | { | |
3ddca062 | 456 | struct sbs_info *chip = i2c_get_clientdata(client); |
9410b7d7 | 457 | int retries = chip->i2c_retry_count; |
ff28fcef | 458 | s32 ret = 0; |
ff28fcef RK |
459 | |
460 | while (retries > 0) { | |
a1bbec72 | 461 | ret = i2c_smbus_write_word_data(client, address, value); |
ff28fcef RK |
462 | if (ret >= 0) |
463 | break; | |
464 | retries--; | |
465 | } | |
d3ab61ec | 466 | |
d3ab61ec | 467 | if (ret < 0) { |
a7d9ace4 | 468 | dev_dbg(&client->dev, |
d3ab61ec RK |
469 | "%s: i2c write to address 0x%x failed\n", |
470 | __func__, address); | |
471 | return ret; | |
472 | } | |
ff28fcef | 473 | |
d3ab61ec RK |
474 | return 0; |
475 | } | |
476 | ||
7f93e1fa PK |
477 | static int sbs_status_correct(struct i2c_client *client, int *intval) |
478 | { | |
479 | int ret; | |
480 | ||
8ce6ee43 | 481 | ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr); |
7f93e1fa PK |
482 | if (ret < 0) |
483 | return ret; | |
484 | ||
485 | ret = (s16)ret; | |
486 | ||
f0318bc9 SR |
487 | /* Not drawing current -> not charging (i.e. idle) */ |
488 | if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0) | |
489 | *intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
7f93e1fa PK |
490 | |
491 | if (*intval == POWER_SUPPLY_STATUS_FULL) { | |
492 | /* Drawing or providing current when full */ | |
493 | if (ret > 0) | |
494 | *intval = POWER_SUPPLY_STATUS_CHARGING; | |
495 | else if (ret < 0) | |
496 | *intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
497 | } | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
6f72a07a SR |
502 | static bool sbs_bat_needs_calibration(struct i2c_client *client) |
503 | { | |
504 | int ret; | |
505 | ||
506 | ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr); | |
507 | if (ret < 0) | |
508 | return false; | |
509 | ||
510 | return !!(ret & BIT(7)); | |
511 | } | |
512 | ||
76b16f4c BN |
513 | static int sbs_get_ti_battery_presence_and_health( |
514 | struct i2c_client *client, enum power_supply_property psp, | |
515 | union power_supply_propval *val) | |
a7640bfa RK |
516 | { |
517 | s32 ret; | |
518 | ||
17c6d397 GR |
519 | /* |
520 | * Write to ManufacturerAccess with ManufacturerAccess command | |
76b16f4c | 521 | * and then read the status. |
17c6d397 | 522 | */ |
76b16f4c BN |
523 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
524 | MANUFACTURER_ACCESS_STATUS); | |
525 | if (ret < 0) { | |
526 | if (psp == POWER_SUPPLY_PROP_PRESENT) | |
527 | val->intval = 0; /* battery removed */ | |
528 | return ret; | |
529 | } | |
17c6d397 GR |
530 | |
531 | ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); | |
bb879101 RK |
532 | if (ret < 0) { |
533 | if (psp == POWER_SUPPLY_PROP_PRESENT) | |
534 | val->intval = 0; /* battery removed */ | |
d3ab61ec | 535 | return ret; |
bb879101 | 536 | } |
a7640bfa | 537 | |
3ddca062 RK |
538 | if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || |
539 | ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { | |
a7640bfa RK |
540 | val->intval = 0; |
541 | return 0; | |
542 | } | |
543 | ||
544 | /* Mask the upper nibble of 2nd byte and | |
545 | * lower byte of response then | |
546 | * shift the result by 8 to get status*/ | |
547 | ret &= 0x0F00; | |
548 | ret >>= 8; | |
549 | if (psp == POWER_SUPPLY_PROP_PRESENT) { | |
550 | if (ret == 0x0F) | |
551 | /* battery removed */ | |
552 | val->intval = 0; | |
553 | else | |
554 | val->intval = 1; | |
555 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { | |
556 | if (ret == 0x09) | |
557 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | |
558 | else if (ret == 0x0B) | |
559 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
560 | else if (ret == 0x0C) | |
561 | val->intval = POWER_SUPPLY_HEALTH_DEAD; | |
6f72a07a SR |
562 | else if (sbs_bat_needs_calibration(client)) |
563 | val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; | |
a7640bfa RK |
564 | else |
565 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | |
566 | } | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
2c4bf698 IJ |
571 | static int sbs_get_battery_presence_and_health( |
572 | struct i2c_client *client, enum power_supply_property psp, | |
573 | union power_supply_propval *val) | |
574 | { | |
575 | struct sbs_info *chip = i2c_get_clientdata(client); | |
576 | int ret; | |
577 | ||
578 | if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) | |
579 | return sbs_get_ti_battery_presence_and_health(client, psp, val); | |
580 | ||
581 | /* Dummy command; if it succeeds, battery is present. */ | |
582 | ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); | |
583 | ||
584 | if (ret < 0) { /* battery not present*/ | |
585 | if (psp == POWER_SUPPLY_PROP_PRESENT) { | |
586 | val->intval = 0; | |
587 | return 0; | |
588 | } | |
589 | return ret; | |
590 | } | |
591 | ||
592 | if (psp == POWER_SUPPLY_PROP_PRESENT) | |
593 | val->intval = 1; /* battery present */ | |
594 | else { /* POWER_SUPPLY_PROP_HEALTH */ | |
595 | if (sbs_bat_needs_calibration(client)) { | |
596 | val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED; | |
597 | } else { | |
598 | /* SBS spec doesn't have a general health command. */ | |
599 | val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; | |
600 | } | |
601 | } | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
3ddca062 | 606 | static int sbs_get_battery_property(struct i2c_client *client, |
a7640bfa RK |
607 | int reg_offset, enum power_supply_property psp, |
608 | union power_supply_propval *val) | |
609 | { | |
3ddca062 | 610 | struct sbs_info *chip = i2c_get_clientdata(client); |
a7640bfa RK |
611 | s32 ret; |
612 | ||
3ddca062 | 613 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
d3ab61ec RK |
614 | if (ret < 0) |
615 | return ret; | |
616 | ||
617 | /* returned values are 16 bit */ | |
3ddca062 | 618 | if (sbs_data[reg_offset].min_value < 0) |
d3ab61ec | 619 | ret = (s16)ret; |
a7640bfa | 620 | |
3ddca062 RK |
621 | if (ret >= sbs_data[reg_offset].min_value && |
622 | ret <= sbs_data[reg_offset].max_value) { | |
a7640bfa | 623 | val->intval = ret; |
957cb720 JC |
624 | if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { |
625 | if (!(ret & BATTERY_INITIALIZED)) | |
626 | val->intval = | |
627 | POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; | |
628 | else if (ret & BATTERY_FULL_CHARGED) | |
629 | val->intval = | |
630 | POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
631 | else if (ret & BATTERY_FULL_DISCHARGED) | |
632 | val->intval = | |
633 | POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
634 | else | |
635 | val->intval = | |
636 | POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
58ddafae | 637 | return 0; |
957cb720 JC |
638 | } else if (psp != POWER_SUPPLY_PROP_STATUS) { |
639 | return 0; | |
640 | } | |
58ddafae RK |
641 | |
642 | if (ret & BATTERY_FULL_CHARGED) | |
643 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
58ddafae RK |
644 | else if (ret & BATTERY_DISCHARGING) |
645 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
646 | else | |
647 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
648 | ||
7f93e1fa PK |
649 | sbs_status_correct(client, &val->intval); |
650 | ||
3ddca062 RK |
651 | if (chip->poll_time == 0) |
652 | chip->last_state = val->intval; | |
653 | else if (chip->last_state != val->intval) { | |
654 | cancel_delayed_work_sync(&chip->work); | |
297d716f | 655 | power_supply_changed(chip->power_supply); |
3ddca062 | 656 | chip->poll_time = 0; |
a7640bfa | 657 | } |
a7640bfa RK |
658 | } else { |
659 | if (psp == POWER_SUPPLY_PROP_STATUS) | |
660 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; | |
bfa953d3 SN |
661 | else if (psp == POWER_SUPPLY_PROP_CAPACITY) |
662 | /* sbs spec says that this can be >100 % | |
663 | * even if max value is 100 % | |
664 | */ | |
665 | val->intval = min(ret, 100); | |
a7640bfa RK |
666 | else |
667 | val->intval = 0; | |
668 | } | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
b49a81d0 IJ |
673 | static int sbs_get_property_index(struct i2c_client *client, |
674 | enum power_supply_property psp) | |
9ea89402 | 675 | { |
b49a81d0 | 676 | int count; |
972eabb9 | 677 | |
b49a81d0 IJ |
678 | for (count = 0; count < ARRAY_SIZE(sbs_data); count++) |
679 | if (psp == sbs_data[count].psp) | |
680 | return count; | |
972eabb9 | 681 | |
b49a81d0 IJ |
682 | dev_warn(&client->dev, |
683 | "%s: Invalid Property - %d\n", __func__, psp); | |
972eabb9 | 684 | |
b49a81d0 IJ |
685 | return -EINVAL; |
686 | } | |
687 | ||
688 | static const char *sbs_get_constant_string(struct sbs_info *chip, | |
689 | enum power_supply_property psp) | |
690 | { | |
691 | int ret; | |
692 | char *buf; | |
693 | u8 addr; | |
694 | ||
695 | buf = sbs_get_string_buf(chip, psp); | |
696 | if (IS_ERR(buf)) | |
697 | return buf; | |
698 | ||
699 | if (!buf[0]) { | |
700 | ret = sbs_get_property_index(chip->client, psp); | |
701 | if (ret < 0) | |
702 | return ERR_PTR(ret); | |
703 | ||
704 | addr = sbs_data[ret].addr; | |
705 | ||
706 | ret = sbs_read_string_data(chip->client, addr, buf); | |
707 | if (ret < 0) | |
708 | return ERR_PTR(ret); | |
709 | } | |
710 | ||
711 | return buf; | |
9ea89402 CYC |
712 | } |
713 | ||
3ddca062 | 714 | static void sbs_unit_adjustment(struct i2c_client *client, |
d3ab61ec RK |
715 | enum power_supply_property psp, union power_supply_propval *val) |
716 | { | |
717 | #define BASE_UNIT_CONVERSION 1000 | |
718 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) | |
909a78b3 BL |
719 | #define TIME_UNIT_CONVERSION 60 |
720 | #define TEMP_KELVIN_TO_CELSIUS 2731 | |
d3ab61ec RK |
721 | switch (psp) { |
722 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
723 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
724 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
3ddca062 | 725 | /* sbs provides energy in units of 10mWh. |
909a78b3 BL |
726 | * Convert to µWh |
727 | */ | |
d3ab61ec RK |
728 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
729 | break; | |
730 | ||
731 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
4495b0ad | 732 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
d3ab61ec RK |
733 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
734 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
8ce6ee43 | 735 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
51d07566 | 736 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
787fdbcf SR |
737 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
738 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: | |
51d07566 RK |
739 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
740 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
d3ab61ec RK |
741 | val->intval *= BASE_UNIT_CONVERSION; |
742 | break; | |
743 | ||
744 | case POWER_SUPPLY_PROP_TEMP: | |
3ddca062 | 745 | /* sbs provides battery temperature in 0.1K |
909a78b3 BL |
746 | * so convert it to 0.1°C |
747 | */ | |
748 | val->intval -= TEMP_KELVIN_TO_CELSIUS; | |
d3ab61ec RK |
749 | break; |
750 | ||
751 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | |
752 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: | |
3ddca062 | 753 | /* sbs provides time to empty and time to full in minutes. |
909a78b3 BL |
754 | * Convert to seconds |
755 | */ | |
d3ab61ec RK |
756 | val->intval *= TIME_UNIT_CONVERSION; |
757 | break; | |
758 | ||
759 | default: | |
760 | dev_dbg(&client->dev, | |
761 | "%s: no need for unit conversion %d\n", __func__, psp); | |
762 | } | |
763 | } | |
764 | ||
e2ec6aef JFD |
765 | static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client, |
766 | enum sbs_capacity_mode mode) | |
51d07566 RK |
767 | { |
768 | int ret, original_val; | |
769 | ||
3ddca062 | 770 | original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); |
51d07566 RK |
771 | if (original_val < 0) |
772 | return original_val; | |
773 | ||
e2ec6aef | 774 | if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode) |
51d07566 RK |
775 | return mode; |
776 | ||
e2ec6aef JFD |
777 | if (mode == CAPACITY_MODE_AMPS) |
778 | ret = original_val & ~BATTERY_MODE_CAPACITY_MASK; | |
51d07566 | 779 | else |
e2ec6aef | 780 | ret = original_val | BATTERY_MODE_CAPACITY_MASK; |
51d07566 | 781 | |
3ddca062 | 782 | ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
51d07566 RK |
783 | if (ret < 0) |
784 | return ret; | |
785 | ||
adcf04c9 PR |
786 | usleep_range(1000, 2000); |
787 | ||
e2ec6aef | 788 | return original_val & BATTERY_MODE_CAPACITY_MASK; |
51d07566 RK |
789 | } |
790 | ||
3ddca062 | 791 | static int sbs_get_battery_capacity(struct i2c_client *client, |
d3ab61ec | 792 | int reg_offset, enum power_supply_property psp, |
a7640bfa RK |
793 | union power_supply_propval *val) |
794 | { | |
795 | s32 ret; | |
e2ec6aef | 796 | enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS; |
51d07566 RK |
797 | |
798 | if (power_supply_is_amp_property(psp)) | |
e2ec6aef | 799 | mode = CAPACITY_MODE_AMPS; |
51d07566 | 800 | |
e2ec6aef | 801 | mode = sbs_set_capacity_mode(client, mode); |
eb368de6 | 802 | if ((int)mode < 0) |
51d07566 | 803 | return mode; |
a7640bfa | 804 | |
3ddca062 | 805 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
d3ab61ec RK |
806 | if (ret < 0) |
807 | return ret; | |
a7640bfa | 808 | |
bfa953d3 | 809 | val->intval = ret; |
d3ab61ec | 810 | |
e2ec6aef | 811 | ret = sbs_set_capacity_mode(client, mode); |
51d07566 RK |
812 | if (ret < 0) |
813 | return ret; | |
814 | ||
d3ab61ec RK |
815 | return 0; |
816 | } | |
817 | ||
3ddca062 RK |
818 | static char sbs_serial[5]; |
819 | static int sbs_get_battery_serial_number(struct i2c_client *client, | |
d3ab61ec RK |
820 | union power_supply_propval *val) |
821 | { | |
822 | int ret; | |
823 | ||
3ddca062 | 824 | ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); |
d3ab61ec RK |
825 | if (ret < 0) |
826 | return ret; | |
827 | ||
5e9bee52 | 828 | sprintf(sbs_serial, "%04x", ret); |
3ddca062 | 829 | val->strval = sbs_serial; |
a7640bfa RK |
830 | |
831 | return 0; | |
832 | } | |
833 | ||
b49a81d0 | 834 | static int sbs_get_chemistry(struct sbs_info *chip, |
3e9544f7 SR |
835 | union power_supply_propval *val) |
836 | { | |
b49a81d0 | 837 | const char *chemistry; |
3e9544f7 | 838 | |
b49a81d0 IJ |
839 | if (chip->technology != -1) { |
840 | val->intval = chip->technology; | |
841 | return 0; | |
842 | } | |
3e9544f7 | 843 | |
b49a81d0 IJ |
844 | chemistry = sbs_get_constant_string(chip, POWER_SUPPLY_PROP_TECHNOLOGY); |
845 | ||
846 | if (IS_ERR(chemistry)) | |
847 | return PTR_ERR(chemistry); | |
3e9544f7 SR |
848 | |
849 | if (!strncasecmp(chemistry, "LION", 4)) | |
b49a81d0 | 850 | chip->technology = POWER_SUPPLY_TECHNOLOGY_LION; |
3e9544f7 | 851 | else if (!strncasecmp(chemistry, "LiP", 3)) |
b49a81d0 | 852 | chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO; |
3e9544f7 | 853 | else if (!strncasecmp(chemistry, "NiCd", 4)) |
b49a81d0 | 854 | chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd; |
3e9544f7 | 855 | else if (!strncasecmp(chemistry, "NiMH", 4)) |
b49a81d0 | 856 | chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH; |
3e9544f7 | 857 | else |
b49a81d0 IJ |
858 | chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
859 | ||
860 | if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN) | |
861 | dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry); | |
3e9544f7 | 862 | |
b49a81d0 | 863 | val->intval = chip->technology; |
3e9544f7 SR |
864 | |
865 | return 0; | |
866 | } | |
867 | ||
7721c2fd SR |
868 | static int sbs_get_battery_manufacture_date(struct i2c_client *client, |
869 | enum power_supply_property psp, | |
870 | union power_supply_propval *val) | |
871 | { | |
872 | int ret; | |
873 | u16 day, month, year; | |
874 | ||
875 | ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE); | |
876 | if (ret < 0) | |
877 | return ret; | |
878 | ||
879 | day = ret & GENMASK(4, 0); | |
880 | month = (ret & GENMASK(8, 5)) >> 5; | |
881 | year = ((ret & GENMASK(15, 9)) >> 9) + 1980; | |
882 | ||
883 | switch (psp) { | |
884 | case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: | |
885 | val->intval = year; | |
886 | break; | |
887 | case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: | |
888 | val->intval = month; | |
889 | break; | |
890 | case POWER_SUPPLY_PROP_MANUFACTURE_DAY: | |
891 | val->intval = day; | |
892 | break; | |
893 | default: | |
894 | return -EINVAL; | |
895 | } | |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
3ddca062 | 900 | static int sbs_get_property(struct power_supply *psy, |
a7640bfa RK |
901 | enum power_supply_property psp, |
902 | union power_supply_propval *val) | |
903 | { | |
bb879101 | 904 | int ret = 0; |
297d716f | 905 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
3ddca062 | 906 | struct i2c_client *client = chip->client; |
b49a81d0 | 907 | const char *str; |
a7640bfa | 908 | |
1cf85553 PR |
909 | if (chip->gpio_detect) { |
910 | ret = gpiod_get_value_cansleep(chip->gpio_detect); | |
911 | if (ret < 0) | |
912 | return ret; | |
913 | if (psp == POWER_SUPPLY_PROP_PRESENT) { | |
914 | val->intval = ret; | |
79bcd5a4 | 915 | sbs_update_presence(chip, ret); |
1cf85553 PR |
916 | return 0; |
917 | } | |
918 | if (ret == 0) | |
919 | return -ENODATA; | |
920 | } | |
921 | ||
a7640bfa RK |
922 | switch (psp) { |
923 | case POWER_SUPPLY_PROP_PRESENT: | |
924 | case POWER_SUPPLY_PROP_HEALTH: | |
2c4bf698 | 925 | ret = sbs_get_battery_presence_and_health(client, psp, val); |
fe55e770 MN |
926 | |
927 | /* this can only be true if no gpio is used */ | |
a7d9ace4 RK |
928 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
929 | return 0; | |
a7640bfa RK |
930 | break; |
931 | ||
932 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
b49a81d0 | 933 | ret = sbs_get_chemistry(chip, val); |
3e9544f7 SR |
934 | if (ret < 0) |
935 | break; | |
936 | ||
5da50988 | 937 | goto done; /* don't trigger power_supply_changed()! */ |
a7640bfa | 938 | |
d3ab61ec RK |
939 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
940 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
941 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
51d07566 RK |
942 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
943 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
944 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
3ddca062 | 945 | ret = sbs_get_property_index(client, psp); |
bb879101 RK |
946 | if (ret < 0) |
947 | break; | |
d3ab61ec | 948 | |
fe8a6534 SN |
949 | /* sbs_get_battery_capacity() will change the battery mode |
950 | * temporarily to read the requested attribute. Ensure we stay | |
951 | * in the desired mode for the duration of the attribute read. | |
952 | */ | |
953 | mutex_lock(&chip->mode_lock); | |
3ddca062 | 954 | ret = sbs_get_battery_capacity(client, ret, psp, val); |
fe8a6534 | 955 | mutex_unlock(&chip->mode_lock); |
d3ab61ec RK |
956 | break; |
957 | ||
958 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: | |
3ddca062 | 959 | ret = sbs_get_battery_serial_number(client, val); |
a7640bfa RK |
960 | break; |
961 | ||
962 | case POWER_SUPPLY_PROP_STATUS: | |
957cb720 | 963 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
a7640bfa RK |
964 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
965 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
966 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
8ce6ee43 | 967 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
a7640bfa RK |
968 | case POWER_SUPPLY_PROP_TEMP: |
969 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | |
970 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: | |
4495b0ad | 971 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
d3ab61ec | 972 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
787fdbcf SR |
973 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
974 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: | |
bfa953d3 | 975 | case POWER_SUPPLY_PROP_CAPACITY: |
d6f56321 | 976 | case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN: |
3ddca062 | 977 | ret = sbs_get_property_index(client, psp); |
bb879101 RK |
978 | if (ret < 0) |
979 | break; | |
d3ab61ec | 980 | |
3ddca062 | 981 | ret = sbs_get_battery_property(client, ret, psp, val); |
a7640bfa RK |
982 | break; |
983 | ||
9ea89402 | 984 | case POWER_SUPPLY_PROP_MODEL_NAME: |
9ea89402 | 985 | case POWER_SUPPLY_PROP_MANUFACTURER: |
b49a81d0 IJ |
986 | str = sbs_get_constant_string(chip, psp); |
987 | if (IS_ERR(str)) | |
988 | ret = PTR_ERR(str); | |
989 | else | |
990 | val->strval = str; | |
9ea89402 CYC |
991 | break; |
992 | ||
7721c2fd SR |
993 | case POWER_SUPPLY_PROP_MANUFACTURE_YEAR: |
994 | case POWER_SUPPLY_PROP_MANUFACTURE_MONTH: | |
995 | case POWER_SUPPLY_PROP_MANUFACTURE_DAY: | |
996 | ret = sbs_get_battery_manufacture_date(client, psp, val); | |
997 | break; | |
998 | ||
a7640bfa RK |
999 | default: |
1000 | dev_err(&client->dev, | |
1001 | "%s: INVALID property\n", __func__); | |
1002 | return -EINVAL; | |
1003 | } | |
1004 | ||
395a7251 IJ |
1005 | if (!chip->gpio_detect && chip->is_present != (ret >= 0)) { |
1006 | bool old_present = chip->is_present; | |
1007 | union power_supply_propval val; | |
8ae237ec | 1008 | int err = sbs_get_battery_presence_and_health( |
395a7251 | 1009 | client, POWER_SUPPLY_PROP_PRESENT, &val); |
bb879101 | 1010 | |
8ae237ec | 1011 | sbs_update_presence(chip, !err && val.intval); |
395a7251 IJ |
1012 | |
1013 | if (old_present != chip->is_present) | |
1014 | power_supply_changed(chip->power_supply); | |
bb879101 RK |
1015 | } |
1016 | ||
1017 | done: | |
1018 | if (!ret) { | |
1019 | /* Convert units to match requirements for power supply class */ | |
3ddca062 | 1020 | sbs_unit_adjustment(client, psp, val); |
8ae237ec IJ |
1021 | dev_dbg(&client->dev, |
1022 | "%s: property = %d, value = %x\n", __func__, | |
1023 | psp, val->intval); | |
1024 | } else if (!chip->is_present) { | |
1025 | /* battery not present, so return NODATA for properties */ | |
1026 | ret = -ENODATA; | |
bb879101 | 1027 | } |
8ae237ec | 1028 | return ret; |
a7640bfa RK |
1029 | } |
1030 | ||
cda3b013 | 1031 | static void sbs_supply_changed(struct sbs_info *chip) |
bb879101 | 1032 | { |
d2cec82c | 1033 | struct power_supply *battery = chip->power_supply; |
3b5dd3a4 | 1034 | int ret; |
bb879101 | 1035 | |
3b5dd3a4 PR |
1036 | ret = gpiod_get_value_cansleep(chip->gpio_detect); |
1037 | if (ret < 0) | |
cda3b013 | 1038 | return; |
79bcd5a4 | 1039 | sbs_update_presence(chip, ret); |
bb879101 | 1040 | power_supply_changed(battery); |
cda3b013 | 1041 | } |
bb879101 | 1042 | |
cda3b013 PR |
1043 | static irqreturn_t sbs_irq(int irq, void *devid) |
1044 | { | |
1045 | sbs_supply_changed(devid); | |
bb879101 RK |
1046 | return IRQ_HANDLED; |
1047 | } | |
1048 | ||
cda3b013 PR |
1049 | static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, |
1050 | unsigned int data) | |
1051 | { | |
1052 | sbs_supply_changed(i2c_get_clientdata(client)); | |
1053 | } | |
1054 | ||
3ddca062 | 1055 | static void sbs_external_power_changed(struct power_supply *psy) |
58ddafae | 1056 | { |
297d716f | 1057 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
58ddafae | 1058 | |
58ddafae | 1059 | /* cancel outstanding work */ |
3ddca062 | 1060 | cancel_delayed_work_sync(&chip->work); |
58ddafae | 1061 | |
3ddca062 | 1062 | schedule_delayed_work(&chip->work, HZ); |
389958bb | 1063 | chip->poll_time = chip->poll_retry_count; |
58ddafae RK |
1064 | } |
1065 | ||
3ddca062 | 1066 | static void sbs_delayed_work(struct work_struct *work) |
58ddafae | 1067 | { |
3ddca062 | 1068 | struct sbs_info *chip; |
58ddafae RK |
1069 | s32 ret; |
1070 | ||
3ddca062 | 1071 | chip = container_of(work, struct sbs_info, work.work); |
58ddafae | 1072 | |
3ddca062 | 1073 | ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); |
58ddafae RK |
1074 | /* if the read failed, give up on this work */ |
1075 | if (ret < 0) { | |
3ddca062 | 1076 | chip->poll_time = 0; |
58ddafae RK |
1077 | return; |
1078 | } | |
1079 | ||
1080 | if (ret & BATTERY_FULL_CHARGED) | |
1081 | ret = POWER_SUPPLY_STATUS_FULL; | |
58ddafae RK |
1082 | else if (ret & BATTERY_DISCHARGING) |
1083 | ret = POWER_SUPPLY_STATUS_DISCHARGING; | |
1084 | else | |
1085 | ret = POWER_SUPPLY_STATUS_CHARGING; | |
1086 | ||
7f93e1fa PK |
1087 | sbs_status_correct(chip->client, &ret); |
1088 | ||
3ddca062 RK |
1089 | if (chip->last_state != ret) { |
1090 | chip->poll_time = 0; | |
297d716f | 1091 | power_supply_changed(chip->power_supply); |
58ddafae RK |
1092 | return; |
1093 | } | |
3ddca062 RK |
1094 | if (chip->poll_time > 0) { |
1095 | schedule_delayed_work(&chip->work, HZ); | |
1096 | chip->poll_time--; | |
58ddafae RK |
1097 | return; |
1098 | } | |
1099 | } | |
1100 | ||
297d716f KK |
1101 | static const struct power_supply_desc sbs_default_desc = { |
1102 | .type = POWER_SUPPLY_TYPE_BATTERY, | |
1103 | .properties = sbs_properties, | |
1104 | .num_properties = ARRAY_SIZE(sbs_properties), | |
1105 | .get_property = sbs_get_property, | |
1106 | .external_power_changed = sbs_external_power_changed, | |
1107 | }; | |
1108 | ||
f9ca07a1 | 1109 | static int sbs_probe(struct i2c_client *client) |
a7640bfa | 1110 | { |
3ddca062 | 1111 | struct sbs_info *chip; |
297d716f | 1112 | struct power_supply_desc *sbs_desc; |
3ddca062 | 1113 | struct sbs_platform_data *pdata = client->dev.platform_data; |
2dc9215d | 1114 | struct power_supply_config psy_cfg = {}; |
a7640bfa | 1115 | int rc; |
bb879101 | 1116 | int irq; |
a7640bfa | 1117 | |
297d716f KK |
1118 | sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, |
1119 | sizeof(*sbs_desc), GFP_KERNEL); | |
1120 | if (!sbs_desc) | |
1121 | return -ENOMEM; | |
1122 | ||
1123 | sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", | |
1124 | dev_name(&client->dev)); | |
1125 | if (!sbs_desc->name) | |
a7640bfa | 1126 | return -ENOMEM; |
52f56c69 | 1127 | |
9239a86f | 1128 | chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); |
297d716f KK |
1129 | if (!chip) |
1130 | return -ENOMEM; | |
a7640bfa | 1131 | |
03b758ba | 1132 | chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev); |
3ddca062 | 1133 | chip->client = client; |
2dc9215d | 1134 | psy_cfg.of_node = client->dev.of_node; |
297d716f | 1135 | psy_cfg.drv_data = chip; |
3ddca062 | 1136 | chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; |
b49a81d0 | 1137 | sbs_invalidate_cached_props(chip); |
fe8a6534 | 1138 | mutex_init(&chip->mode_lock); |
a7640bfa | 1139 | |
9edeaada AB |
1140 | /* use pdata if available, fall back to DT properties, |
1141 | * or hardcoded defaults if not | |
1142 | */ | |
03b758ba SR |
1143 | rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count", |
1144 | &chip->i2c_retry_count); | |
9edeaada | 1145 | if (rc) |
389958bb | 1146 | chip->i2c_retry_count = 0; |
9edeaada | 1147 | |
03b758ba SR |
1148 | rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count", |
1149 | &chip->poll_retry_count); | |
9edeaada AB |
1150 | if (rc) |
1151 | chip->poll_retry_count = 0; | |
1152 | ||
1153 | if (pdata) { | |
1154 | chip->poll_retry_count = pdata->poll_retry_count; | |
1155 | chip->i2c_retry_count = pdata->i2c_retry_count; | |
1156 | } | |
389958bb | 1157 | chip->i2c_retry_count = chip->i2c_retry_count + 1; |
6c75ea1e | 1158 | |
03b758ba | 1159 | chip->charger_broadcasts = !device_property_read_bool(&client->dev, |
182fc882 JFD |
1160 | "sbs,disable-charger-broadcasts"); |
1161 | ||
3b5dd3a4 PR |
1162 | chip->gpio_detect = devm_gpiod_get_optional(&client->dev, |
1163 | "sbs,battery-detect", GPIOD_IN); | |
166767ab SR |
1164 | if (IS_ERR(chip->gpio_detect)) |
1165 | return dev_err_probe(&client->dev, PTR_ERR(chip->gpio_detect), | |
1166 | "Failed to get gpio\n"); | |
bb879101 | 1167 | |
3ddca062 | 1168 | i2c_set_clientdata(client, chip); |
a7640bfa | 1169 | |
3ddca062 | 1170 | if (!chip->gpio_detect) |
bb879101 RK |
1171 | goto skip_gpio; |
1172 | ||
3b5dd3a4 | 1173 | irq = gpiod_to_irq(chip->gpio_detect); |
bb879101 RK |
1174 | if (irq <= 0) { |
1175 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); | |
bb879101 RK |
1176 | goto skip_gpio; |
1177 | } | |
1178 | ||
d2cec82c | 1179 | rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, |
bb8fe8ea | 1180 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
d2cec82c | 1181 | dev_name(&client->dev), chip); |
bb879101 RK |
1182 | if (rc) { |
1183 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); | |
bb879101 RK |
1184 | goto skip_gpio; |
1185 | } | |
1186 | ||
bb879101 | 1187 | skip_gpio: |
a22b41a3 | 1188 | /* |
f4ed950a | 1189 | * Before we register, we might need to make sure we can actually talk |
a22b41a3 OJ |
1190 | * to the battery. |
1191 | */ | |
3b5dd3a4 | 1192 | if (!(force_load || chip->gpio_detect)) { |
395a7251 | 1193 | union power_supply_propval val; |
f4ed950a | 1194 | |
395a7251 IJ |
1195 | rc = sbs_get_battery_presence_and_health( |
1196 | client, POWER_SUPPLY_PROP_PRESENT, &val); | |
166767ab SR |
1197 | if (rc < 0 || !val.intval) |
1198 | return dev_err_probe(&client->dev, -ENODEV, | |
1199 | "Failed to get present status\n"); | |
a22b41a3 | 1200 | } |
bb879101 | 1201 | |
6d0c5de2 MV |
1202 | rc = devm_delayed_work_autocancel(&client->dev, &chip->work, |
1203 | sbs_delayed_work); | |
1204 | if (rc) | |
1205 | return rc; | |
52bef41f | 1206 | |
492ff9d8 | 1207 | chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, |
297d716f | 1208 | &psy_cfg); |
166767ab SR |
1209 | if (IS_ERR(chip->power_supply)) |
1210 | return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply), | |
1211 | "Failed to register power supply\n"); | |
a7640bfa RK |
1212 | |
1213 | dev_info(&client->dev, | |
1214 | "%s: battery gas gauge device registered\n", client->name); | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
9c1d1af0 LPC |
1219 | #if defined CONFIG_PM_SLEEP |
1220 | ||
1221 | static int sbs_suspend(struct device *dev) | |
a7640bfa | 1222 | { |
9c1d1af0 | 1223 | struct i2c_client *client = to_i2c_client(dev); |
3ddca062 | 1224 | struct sbs_info *chip = i2c_get_clientdata(client); |
76b16f4c | 1225 | int ret; |
a7640bfa | 1226 | |
3ddca062 RK |
1227 | if (chip->poll_time > 0) |
1228 | cancel_delayed_work_sync(&chip->work); | |
58ddafae | 1229 | |
0ff96915 | 1230 | if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) { |
76b16f4c BN |
1231 | /* Write to manufacturer access with sleep command. */ |
1232 | ret = sbs_write_word_data(client, | |
1233 | sbs_data[REG_MANUFACTURER_DATA].addr, | |
1234 | MANUFACTURER_ACCESS_SLEEP); | |
1235 | if (chip->is_present && ret < 0) | |
1236 | return ret; | |
1237 | } | |
a7640bfa RK |
1238 | |
1239 | return 0; | |
1240 | } | |
9c1d1af0 LPC |
1241 | |
1242 | static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); | |
1243 | #define SBS_PM_OPS (&sbs_pm_ops) | |
1244 | ||
a7640bfa | 1245 | #else |
9c1d1af0 | 1246 | #define SBS_PM_OPS NULL |
a7640bfa | 1247 | #endif |
a7640bfa | 1248 | |
3ddca062 | 1249 | static const struct i2c_device_id sbs_id[] = { |
0ff96915 | 1250 | { "bq20z65", 0 }, |
a7640bfa | 1251 | { "bq20z75", 0 }, |
3ddca062 | 1252 | { "sbs-battery", 1 }, |
a7640bfa RK |
1253 | {} |
1254 | }; | |
3ddca062 RK |
1255 | MODULE_DEVICE_TABLE(i2c, sbs_id); |
1256 | ||
9edeaada AB |
1257 | static const struct of_device_id sbs_dt_ids[] = { |
1258 | { .compatible = "sbs,sbs-battery" }, | |
0ff96915 SR |
1259 | { |
1260 | .compatible = "ti,bq20z65", | |
1261 | .data = (void *)SBS_FLAGS_TI_BQ20ZX5, | |
1262 | }, | |
76b16f4c BN |
1263 | { |
1264 | .compatible = "ti,bq20z75", | |
0ff96915 | 1265 | .data = (void *)SBS_FLAGS_TI_BQ20ZX5, |
76b16f4c | 1266 | }, |
9edeaada AB |
1267 | { } |
1268 | }; | |
1269 | MODULE_DEVICE_TABLE(of, sbs_dt_ids); | |
1270 | ||
3ddca062 | 1271 | static struct i2c_driver sbs_battery_driver = { |
f9ca07a1 | 1272 | .probe_new = sbs_probe, |
cda3b013 | 1273 | .alert = sbs_alert, |
3ddca062 | 1274 | .id_table = sbs_id, |
a7640bfa | 1275 | .driver = { |
3ddca062 | 1276 | .name = "sbs-battery", |
9edeaada | 1277 | .of_match_table = sbs_dt_ids, |
9c1d1af0 | 1278 | .pm = SBS_PM_OPS, |
a7640bfa RK |
1279 | }, |
1280 | }; | |
5ff92e7a | 1281 | module_i2c_driver(sbs_battery_driver); |
a7640bfa | 1282 | |
3ddca062 | 1283 | MODULE_DESCRIPTION("SBS battery monitor driver"); |
a7640bfa | 1284 | MODULE_LICENSE("GPL"); |
f4ed950a | 1285 | |
75d8a842 | 1286 | module_param(force_load, bool, 0444); |
f4ed950a FK |
1287 | MODULE_PARM_DESC(force_load, |
1288 | "Attempt to load the driver even if no battery is connected"); |