hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/power/supply/sbs-battery.c
....@@ -1,19 +1,11 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Gas Gauge driver for SBS Compliant Batteries
34 *
45 * Copyright (c) 2010, NVIDIA Corporation.
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful, but WITHOUT
12
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
- * more details.
156 */
167
8
+#include <linux/bits.h>
179 #include <linux/delay.h>
1810 #include <linux/err.h>
1911 #include <linux/gpio/consumer.h>
....@@ -22,7 +14,7 @@
2214 #include <linux/interrupt.h>
2315 #include <linux/kernel.h>
2416 #include <linux/module.h>
25
-#include <linux/of.h>
17
+#include <linux/property.h>
2618 #include <linux/of_device.h>
2719 #include <linux/power/sbs-battery.h>
2820 #include <linux/power_supply.h>
....@@ -31,9 +23,12 @@
3123
3224 enum {
3325 REG_MANUFACTURER_DATA,
26
+ REG_BATTERY_MODE,
3427 REG_TEMPERATURE,
3528 REG_VOLTAGE,
36
- REG_CURRENT,
29
+ REG_CURRENT_NOW,
30
+ REG_CURRENT_AVG,
31
+ REG_MAX_ERR,
3732 REG_CAPACITY,
3833 REG_TIME_TO_EMPTY,
3934 REG_TIME_TO_FULL,
....@@ -49,17 +44,31 @@
4944 REG_DESIGN_CAPACITY_CHARGE,
5045 REG_DESIGN_VOLTAGE_MIN,
5146 REG_DESIGN_VOLTAGE_MAX,
47
+ REG_CHEMISTRY,
5248 REG_MANUFACTURER,
5349 REG_MODEL_NAME,
50
+ REG_CHARGE_CURRENT,
51
+ REG_CHARGE_VOLTAGE,
5452 };
53
+
54
+#define REG_ADDR_SPEC_INFO 0x1A
55
+#define SPEC_INFO_VERSION_MASK GENMASK(7, 4)
56
+#define SPEC_INFO_VERSION_SHIFT 4
57
+
58
+#define SBS_VERSION_1_0 1
59
+#define SBS_VERSION_1_1 2
60
+#define SBS_VERSION_1_1_WITH_PEC 3
61
+
62
+#define REG_ADDR_MANUFACTURE_DATE 0x1B
5563
5664 /* Battery Mode defines */
5765 #define BATTERY_MODE_OFFSET 0x03
58
-#define BATTERY_MODE_MASK 0x8000
59
-enum sbs_battery_mode {
60
- BATTERY_MODE_AMPS = 0,
61
- BATTERY_MODE_WATTS = 0x8000
66
+#define BATTERY_MODE_CAPACITY_MASK BIT(15)
67
+enum sbs_capacity_mode {
68
+ CAPACITY_MODE_AMPS = 0,
69
+ CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
6270 };
71
+#define BATTERY_MODE_CHARGER_MASK (1<<14)
6372
6473 /* manufacturer access defines */
6574 #define MANUFACTURER_ACCESS_STATUS 0x0006
....@@ -87,12 +96,18 @@
8796 } sbs_data[] = {
8897 [REG_MANUFACTURER_DATA] =
8998 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
99
+ [REG_BATTERY_MODE] =
100
+ SBS_DATA(-1, 0x03, 0, 65535),
90101 [REG_TEMPERATURE] =
91102 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
92103 [REG_VOLTAGE] =
93104 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
94
- [REG_CURRENT] =
105
+ [REG_CURRENT_NOW] =
95106 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
107
+ [REG_CURRENT_AVG] =
108
+ SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
109
+ [REG_MAX_ERR] =
110
+ SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
96111 [REG_CAPACITY] =
97112 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
98113 [REG_REMAINING_CAPACITY] =
....@@ -107,6 +122,10 @@
107122 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
108123 [REG_TIME_TO_FULL] =
109124 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
125
+ [REG_CHARGE_CURRENT] =
126
+ SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
127
+ [REG_CHARGE_VOLTAGE] =
128
+ SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
110129 [REG_STATUS] =
111130 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
112131 [REG_CAPACITY_LEVEL] =
....@@ -127,10 +146,12 @@
127146 [REG_MANUFACTURER] =
128147 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
129148 [REG_MODEL_NAME] =
130
- SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
149
+ SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
150
+ [REG_CHEMISTRY] =
151
+ SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
131152 };
132153
133
-static enum power_supply_property sbs_properties[] = {
154
+static const enum power_supply_property sbs_properties[] = {
134155 POWER_SUPPLY_PROP_STATUS,
135156 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
136157 POWER_SUPPLY_PROP_HEALTH,
....@@ -139,7 +160,9 @@
139160 POWER_SUPPLY_PROP_CYCLE_COUNT,
140161 POWER_SUPPLY_PROP_VOLTAGE_NOW,
141162 POWER_SUPPLY_PROP_CURRENT_NOW,
163
+ POWER_SUPPLY_PROP_CURRENT_AVG,
142164 POWER_SUPPLY_PROP_CAPACITY,
165
+ POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
143166 POWER_SUPPLY_PROP_TEMP,
144167 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
145168 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
....@@ -152,20 +175,25 @@
152175 POWER_SUPPLY_PROP_CHARGE_NOW,
153176 POWER_SUPPLY_PROP_CHARGE_FULL,
154177 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
178
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
179
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
180
+ POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
181
+ POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
182
+ POWER_SUPPLY_PROP_MANUFACTURE_DAY,
155183 /* Properties of type `const char *' */
156184 POWER_SUPPLY_PROP_MANUFACTURER,
157185 POWER_SUPPLY_PROP_MODEL_NAME
158186 };
159187
160
-/* Supports special manufacturer commands from TI BQ20Z75 IC. */
161
-#define SBS_FLAGS_TI_BQ20Z75 BIT(0)
188
+/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
189
+#define SBS_FLAGS_TI_BQ20ZX5 BIT(0)
162190
163191 struct sbs_info {
164192 struct i2c_client *client;
165193 struct power_supply *power_supply;
166194 bool is_present;
167195 struct gpio_desc *gpio_detect;
168
- bool enable_detection;
196
+ bool charger_broadcasts;
169197 int last_state;
170198 int poll_time;
171199 u32 i2c_retry_count;
....@@ -177,7 +205,96 @@
177205
178206 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
179207 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
208
+static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
180209 static bool force_load;
210
+
211
+static int sbs_read_word_data(struct i2c_client *client, u8 address);
212
+static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);
213
+
214
+static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
215
+{
216
+ int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
217
+ if (val < 0)
218
+ goto exit;
219
+
220
+ val |= BATTERY_MODE_CHARGER_MASK;
221
+
222
+ val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
223
+
224
+exit:
225
+ if (val < 0)
226
+ dev_err(&chip->client->dev,
227
+ "Failed to disable charger broadcasting: %d\n", val);
228
+ else
229
+ dev_dbg(&chip->client->dev, "%s\n", __func__);
230
+}
231
+
232
+static int sbs_update_presence(struct sbs_info *chip, bool is_present)
233
+{
234
+ struct i2c_client *client = chip->client;
235
+ int retries = chip->i2c_retry_count;
236
+ s32 ret = 0;
237
+ u8 version;
238
+
239
+ if (chip->is_present == is_present)
240
+ return 0;
241
+
242
+ if (!is_present) {
243
+ chip->is_present = false;
244
+ /* Disable PEC when no device is present */
245
+ client->flags &= ~I2C_CLIENT_PEC;
246
+ return 0;
247
+ }
248
+
249
+ /* Check if device supports packet error checking and use it */
250
+ while (retries > 0) {
251
+ ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
252
+ if (ret >= 0)
253
+ break;
254
+
255
+ /*
256
+ * Some batteries trigger the detection pin before the
257
+ * I2C bus is properly connected. This works around the
258
+ * issue.
259
+ */
260
+ msleep(100);
261
+
262
+ retries--;
263
+ }
264
+
265
+ if (ret < 0) {
266
+ dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
267
+
268
+ /* fallback to old behaviour */
269
+ client->flags &= ~I2C_CLIENT_PEC;
270
+ chip->is_present = true;
271
+
272
+ return ret;
273
+ }
274
+
275
+ version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
276
+
277
+ if (version == SBS_VERSION_1_1_WITH_PEC)
278
+ client->flags |= I2C_CLIENT_PEC;
279
+ else
280
+ client->flags &= ~I2C_CLIENT_PEC;
281
+
282
+ if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
283
+ && client->flags & I2C_CLIENT_PEC) {
284
+ dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
285
+ client->flags &= ~I2C_CLIENT_PEC;
286
+ }
287
+
288
+ dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
289
+ "enabled" : "disabled");
290
+
291
+ if (!chip->is_present && is_present && !chip->charger_broadcasts)
292
+ sbs_disable_charger_broadcasts(chip);
293
+
294
+ chip->is_present = true;
295
+
296
+ return 0;
297
+}
181298
182299 static int sbs_read_word_data(struct i2c_client *client, u8 address)
183300 {
....@@ -202,8 +319,7 @@
202319 return ret;
203320 }
204321
205
-static int sbs_read_string_data(struct i2c_client *client, u8 address,
206
- char *values)
322
+static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
207323 {
208324 struct sbs_info *chip = i2c_get_clientdata(client);
209325 s32 ret = 0, block_length = 0;
....@@ -212,6 +328,9 @@
212328
213329 retries_length = chip->i2c_retry_count;
214330 retries_block = chip->i2c_retry_count;
331
+
332
+ dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
333
+ "Fallback method does not support PEC.\n");
215334
216335 /* Adapter needs to support these two functions */
217336 if (!i2c_check_functionality(client->adapter,
....@@ -268,6 +387,38 @@
268387 return ret;
269388 }
270389
390
+static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
391
+{
392
+ struct sbs_info *chip = i2c_get_clientdata(client);
393
+ int retries = chip->i2c_retry_count;
394
+ int ret = 0;
395
+
396
+ if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
397
+ bool pec = client->flags & I2C_CLIENT_PEC;
398
+ client->flags &= ~I2C_CLIENT_PEC;
399
+ ret = sbs_read_string_data_fallback(client, address, values);
400
+ if (pec)
401
+ client->flags |= I2C_CLIENT_PEC;
402
+ return ret;
403
+ }
404
+
405
+ while (retries > 0) {
406
+ ret = i2c_smbus_read_block_data(client, address, values);
407
+ if (ret >= 0)
408
+ break;
409
+ retries--;
410
+ }
411
+
412
+ if (ret < 0) {
413
+ dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
414
+ return ret;
415
+ }
416
+
417
+ /* add string termination */
418
+ values[ret] = '\0';
419
+ return ret;
420
+}
421
+
271422 static int sbs_write_word_data(struct i2c_client *client, u8 address,
272423 u16 value)
273424 {
....@@ -296,15 +447,15 @@
296447 {
297448 int ret;
298449
299
- ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
450
+ ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
300451 if (ret < 0)
301452 return ret;
302453
303454 ret = (s16)ret;
304455
305
- /* Not drawing current means full (cannot be not charging) */
306
- if (ret == 0)
307
- *intval = POWER_SUPPLY_STATUS_FULL;
456
+ /* Not drawing current -> not charging (i.e. idle) */
457
+ if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
458
+ *intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
308459
309460 if (*intval == POWER_SUPPLY_STATUS_FULL) {
310461 /* Drawing or providing current when full */
....@@ -317,30 +468,15 @@
317468 return 0;
318469 }
319470
320
-static int sbs_get_battery_presence_and_health(
321
- struct i2c_client *client, enum power_supply_property psp,
322
- union power_supply_propval *val)
471
+static bool sbs_bat_needs_calibration(struct i2c_client *client)
323472 {
324473 int ret;
325474
326
- /* Dummy command; if it succeeds, battery is present. */
327
- ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
475
+ ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
476
+ if (ret < 0)
477
+ return false;
328478
329
- if (ret < 0) { /* battery not present*/
330
- if (psp == POWER_SUPPLY_PROP_PRESENT) {
331
- val->intval = 0;
332
- return 0;
333
- }
334
- return ret;
335
- }
336
-
337
- if (psp == POWER_SUPPLY_PROP_PRESENT)
338
- val->intval = 1; /* battery present */
339
- else /* POWER_SUPPLY_PROP_HEALTH */
340
- /* SBS spec doesn't have a general health command. */
341
- val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
342
-
343
- return 0;
479
+ return !!(ret & BIT(7));
344480 }
345481
346482 static int sbs_get_ti_battery_presence_and_health(
....@@ -392,8 +528,45 @@
392528 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
393529 else if (ret == 0x0C)
394530 val->intval = POWER_SUPPLY_HEALTH_DEAD;
531
+ else if (sbs_bat_needs_calibration(client))
532
+ val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
395533 else
396534 val->intval = POWER_SUPPLY_HEALTH_GOOD;
535
+ }
536
+
537
+ return 0;
538
+}
539
+
540
+static int sbs_get_battery_presence_and_health(
541
+ struct i2c_client *client, enum power_supply_property psp,
542
+ union power_supply_propval *val)
543
+{
544
+ struct sbs_info *chip = i2c_get_clientdata(client);
545
+ int ret;
546
+
547
+ if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
548
+ return sbs_get_ti_battery_presence_and_health(client, psp, val);
549
+
550
+ /* Dummy command; if it succeeds, battery is present. */
551
+ ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
552
+
553
+ if (ret < 0) { /* battery not present*/
554
+ if (psp == POWER_SUPPLY_PROP_PRESENT) {
555
+ val->intval = 0;
556
+ return 0;
557
+ }
558
+ return ret;
559
+ }
560
+
561
+ if (psp == POWER_SUPPLY_PROP_PRESENT)
562
+ val->intval = 1; /* battery present */
563
+ else { /* POWER_SUPPLY_PROP_HEALTH */
564
+ if (sbs_bat_needs_calibration(client)) {
565
+ val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
566
+ } else {
567
+ /* SBS spec doesn't have a general health command. */
568
+ val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
569
+ }
397570 }
398571
399572 return 0;
....@@ -500,7 +673,10 @@
500673 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
501674 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
502675 case POWER_SUPPLY_PROP_CURRENT_NOW:
676
+ case POWER_SUPPLY_PROP_CURRENT_AVG:
503677 case POWER_SUPPLY_PROP_CHARGE_NOW:
678
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
679
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
504680 case POWER_SUPPLY_PROP_CHARGE_FULL:
505681 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
506682 val->intval *= BASE_UNIT_CONVERSION;
....@@ -527,8 +703,8 @@
527703 }
528704 }
529705
530
-static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
531
- enum sbs_battery_mode mode)
706
+static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
707
+ enum sbs_capacity_mode mode)
532708 {
533709 int ret, original_val;
534710
....@@ -536,13 +712,13 @@
536712 if (original_val < 0)
537713 return original_val;
538714
539
- if ((original_val & BATTERY_MODE_MASK) == mode)
715
+ if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
540716 return mode;
541717
542
- if (mode == BATTERY_MODE_AMPS)
543
- ret = original_val & ~BATTERY_MODE_MASK;
718
+ if (mode == CAPACITY_MODE_AMPS)
719
+ ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
544720 else
545
- ret = original_val | BATTERY_MODE_MASK;
721
+ ret = original_val | BATTERY_MODE_CAPACITY_MASK;
546722
547723 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
548724 if (ret < 0)
....@@ -550,7 +726,7 @@
550726
551727 usleep_range(1000, 2000);
552728
553
- return original_val & BATTERY_MODE_MASK;
729
+ return original_val & BATTERY_MODE_CAPACITY_MASK;
554730 }
555731
556732 static int sbs_get_battery_capacity(struct i2c_client *client,
....@@ -558,13 +734,13 @@
558734 union power_supply_propval *val)
559735 {
560736 s32 ret;
561
- enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
737
+ enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
562738
563739 if (power_supply_is_amp_property(psp))
564
- mode = BATTERY_MODE_AMPS;
740
+ mode = CAPACITY_MODE_AMPS;
565741
566
- mode = sbs_set_battery_mode(client, mode);
567
- if (mode < 0)
742
+ mode = sbs_set_capacity_mode(client, mode);
743
+ if ((int)mode < 0)
568744 return mode;
569745
570746 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
....@@ -573,7 +749,7 @@
573749
574750 val->intval = ret;
575751
576
- ret = sbs_set_battery_mode(client, mode);
752
+ ret = sbs_set_capacity_mode(client, mode);
577753 if (ret < 0)
578754 return ret;
579755
....@@ -610,6 +786,70 @@
610786 return -EINVAL;
611787 }
612788
789
+static int sbs_get_chemistry(struct i2c_client *client,
790
+ union power_supply_propval *val)
791
+{
792
+ enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
793
+ int ret;
794
+
795
+ ret = sbs_get_property_index(client, psp);
796
+ if (ret < 0)
797
+ return ret;
798
+
799
+ ret = sbs_get_battery_string_property(client, ret, psp,
800
+ chemistry);
801
+ if (ret < 0)
802
+ return ret;
803
+
804
+ if (!strncasecmp(chemistry, "LION", 4))
805
+ val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
806
+ else if (!strncasecmp(chemistry, "LiP", 3))
807
+ val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
808
+ else if (!strncasecmp(chemistry, "NiCd", 4))
809
+ val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
810
+ else if (!strncasecmp(chemistry, "NiMH", 4))
811
+ val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
812
+ else
813
+ val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
814
+
815
+ if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
816
+ dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);
817
+
818
+ return 0;
819
+}
820
+
821
+static int sbs_get_battery_manufacture_date(struct i2c_client *client,
822
+ enum power_supply_property psp,
823
+ union power_supply_propval *val)
824
+{
825
+ int ret;
826
+ u16 day, month, year;
827
+
828
+ ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
829
+ if (ret < 0)
830
+ return ret;
831
+
832
+ day = ret & GENMASK(4, 0);
833
+ month = (ret & GENMASK(8, 5)) >> 5;
834
+ year = ((ret & GENMASK(15, 9)) >> 9) + 1980;
835
+
836
+ switch (psp) {
837
+ case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
838
+ val->intval = year;
839
+ break;
840
+ case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
841
+ val->intval = month;
842
+ break;
843
+ case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
844
+ val->intval = day;
845
+ break;
846
+ default:
847
+ return -EINVAL;
848
+ }
849
+
850
+ return 0;
851
+}
852
+
613853 static int sbs_get_property(struct power_supply *psy,
614854 enum power_supply_property psp,
615855 union power_supply_propval *val)
....@@ -624,7 +864,7 @@
624864 return ret;
625865 if (psp == POWER_SUPPLY_PROP_PRESENT) {
626866 val->intval = ret;
627
- chip->is_present = val->intval;
867
+ sbs_update_presence(chip, ret);
628868 return 0;
629869 }
630870 if (ret == 0)
....@@ -634,12 +874,7 @@
634874 switch (psp) {
635875 case POWER_SUPPLY_PROP_PRESENT:
636876 case POWER_SUPPLY_PROP_HEALTH:
637
- if (chip->flags & SBS_FLAGS_TI_BQ20Z75)
638
- ret = sbs_get_ti_battery_presence_and_health(client,
639
- psp, val);
640
- else
641
- ret = sbs_get_battery_presence_and_health(client, psp,
642
- val);
877
+ ret = sbs_get_battery_presence_and_health(client, psp, val);
643878
644879 /* this can only be true if no gpio is used */
645880 if (psp == POWER_SUPPLY_PROP_PRESENT)
....@@ -647,7 +882,10 @@
647882 break;
648883
649884 case POWER_SUPPLY_PROP_TECHNOLOGY:
650
- val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
885
+ ret = sbs_get_chemistry(client, val);
886
+ if (ret < 0)
887
+ break;
888
+
651889 goto done; /* don't trigger power_supply_changed()! */
652890
653891 case POWER_SUPPLY_PROP_ENERGY_NOW:
....@@ -678,12 +916,16 @@
678916 case POWER_SUPPLY_PROP_CYCLE_COUNT:
679917 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
680918 case POWER_SUPPLY_PROP_CURRENT_NOW:
919
+ case POWER_SUPPLY_PROP_CURRENT_AVG:
681920 case POWER_SUPPLY_PROP_TEMP:
682921 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
683922 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
684923 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
685924 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
925
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
926
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
686927 case POWER_SUPPLY_PROP_CAPACITY:
928
+ case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
687929 ret = sbs_get_property_index(client, psp);
688930 if (ret < 0)
689931 break;
....@@ -711,38 +953,42 @@
711953 val->strval = manufacturer;
712954 break;
713955
956
+ case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
957
+ case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
958
+ case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
959
+ ret = sbs_get_battery_manufacture_date(client, psp, val);
960
+ break;
961
+
714962 default:
715963 dev_err(&client->dev,
716964 "%s: INVALID property\n", __func__);
717965 return -EINVAL;
718966 }
719967
720
- if (!chip->enable_detection)
721
- goto done;
968
+ if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
969
+ bool old_present = chip->is_present;
970
+ union power_supply_propval val;
971
+ int err = sbs_get_battery_presence_and_health(
972
+ client, POWER_SUPPLY_PROP_PRESENT, &val);
722973
723
- if (!chip->gpio_detect &&
724
- chip->is_present != (ret >= 0)) {
725
- chip->is_present = (ret >= 0);
726
- power_supply_changed(chip->power_supply);
974
+ sbs_update_presence(chip, !err && val.intval);
975
+
976
+ if (old_present != chip->is_present)
977
+ power_supply_changed(chip->power_supply);
727978 }
728979
729980 done:
730981 if (!ret) {
731982 /* Convert units to match requirements for power supply class */
732983 sbs_unit_adjustment(client, psp, val);
984
+ dev_dbg(&client->dev,
985
+ "%s: property = %d, value = %x\n", __func__,
986
+ psp, val->intval);
987
+ } else if (!chip->is_present) {
988
+ /* battery not present, so return NODATA for properties */
989
+ ret = -ENODATA;
733990 }
734
-
735
- dev_dbg(&client->dev,
736
- "%s: property = %d, value = %x\n", __func__, psp, val->intval);
737
-
738
- if (ret && chip->is_present)
739
- return ret;
740
-
741
- /* battery not present, so return NODATA for properties */
742
- if (ret)
743
- return -ENODATA;
744
-
745
- return 0;
991
+ return ret;
746992 }
747993
748994 static void sbs_supply_changed(struct sbs_info *chip)
....@@ -753,7 +999,7 @@
753999 ret = gpiod_get_value_cansleep(chip->gpio_detect);
7541000 if (ret < 0)
7551001 return;
756
- chip->is_present = ret;
1002
+ sbs_update_presence(chip, ret);
7571003 power_supply_changed(battery);
7581004 }
7591005
....@@ -823,8 +1069,7 @@
8231069 .external_power_changed = sbs_external_power_changed,
8241070 };
8251071
826
-static int sbs_probe(struct i2c_client *client,
827
- const struct i2c_device_id *id)
1072
+static int sbs_probe(struct i2c_client *client)
8281073 {
8291074 struct sbs_info *chip;
8301075 struct power_supply_desc *sbs_desc;
....@@ -847,9 +1092,8 @@
8471092 if (!chip)
8481093 return -ENOMEM;
8491094
850
- chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
1095
+ chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
8511096 chip->client = client;
852
- chip->enable_detection = false;
8531097 psy_cfg.of_node = client->dev.of_node;
8541098 psy_cfg.drv_data = chip;
8551099 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
....@@ -858,13 +1102,13 @@
8581102 /* use pdata if available, fall back to DT properties,
8591103 * or hardcoded defaults if not
8601104 */
861
- rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
862
- &chip->i2c_retry_count);
1105
+ rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1106
+ &chip->i2c_retry_count);
8631107 if (rc)
8641108 chip->i2c_retry_count = 0;
8651109
866
- rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
867
- &chip->poll_retry_count);
1110
+ rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1111
+ &chip->poll_retry_count);
8681112 if (rc)
8691113 chip->poll_retry_count = 0;
8701114
....@@ -873,6 +1117,9 @@
8731117 chip->i2c_retry_count = pdata->i2c_retry_count;
8741118 }
8751119 chip->i2c_retry_count = chip->i2c_retry_count + 1;
1120
+
1121
+ chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1122
+ "sbs,disable-charger-broadcasts");
8761123
8771124 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
8781125 "sbs,battery-detect", GPIOD_IN);
....@@ -907,14 +1154,18 @@
9071154 * to the battery.
9081155 */
9091156 if (!(force_load || chip->gpio_detect)) {
910
- rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
1157
+ union power_supply_propval val;
9111158
912
- if (rc < 0) {
913
- dev_err(&client->dev, "%s: Failed to get device status\n",
914
- __func__);
1159
+ rc = sbs_get_battery_presence_and_health(
1160
+ client, POWER_SUPPLY_PROP_PRESENT, &val);
1161
+ if (rc < 0 || !val.intval) {
1162
+ dev_err(&client->dev, "Failed to get present status\n");
1163
+ rc = -ENODEV;
9151164 goto exit_psupply;
9161165 }
9171166 }
1167
+
1168
+ INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
9181169
9191170 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
9201171 &psy_cfg);
....@@ -927,10 +1178,6 @@
9271178
9281179 dev_info(&client->dev,
9291180 "%s: battery gas gauge device registered\n", client->name);
930
-
931
- INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
932
-
933
- chip->enable_detection = true;
9341181
9351182 return 0;
9361183
....@@ -958,7 +1205,7 @@
9581205 if (chip->poll_time > 0)
9591206 cancel_delayed_work_sync(&chip->work);
9601207
961
- if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
1208
+ if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
9621209 /* Write to manufacturer access with sleep command. */
9631210 ret = sbs_write_word_data(client,
9641211 sbs_data[REG_MANUFACTURER_DATA].addr,
....@@ -978,6 +1225,7 @@
9781225 #endif
9791226
9801227 static const struct i2c_device_id sbs_id[] = {
1228
+ { "bq20z65", 0 },
9811229 { "bq20z75", 0 },
9821230 { "sbs-battery", 1 },
9831231 {}
....@@ -987,15 +1235,19 @@
9871235 static const struct of_device_id sbs_dt_ids[] = {
9881236 { .compatible = "sbs,sbs-battery" },
9891237 {
1238
+ .compatible = "ti,bq20z65",
1239
+ .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1240
+ },
1241
+ {
9901242 .compatible = "ti,bq20z75",
991
- .data = (void *)SBS_FLAGS_TI_BQ20Z75,
1243
+ .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
9921244 },
9931245 { }
9941246 };
9951247 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
9961248
9971249 static struct i2c_driver sbs_battery_driver = {
998
- .probe = sbs_probe,
1250
+ .probe_new = sbs_probe,
9991251 .remove = sbs_remove,
10001252 .alert = sbs_alert,
10011253 .id_table = sbs_id,
....@@ -1010,6 +1262,6 @@
10101262 MODULE_DESCRIPTION("SBS battery monitor driver");
10111263 MODULE_LICENSE("GPL");
10121264
1013
-module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
1265
+module_param(force_load, bool, 0444);
10141266 MODULE_PARM_DESC(force_load,
10151267 "Attempt to load the driver even if no battery is connected");