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