]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/power/lc709203f_battery.c
power: lc709203f: add interface to get battery soc
[sojka/nv-tegra/linux-3.10.git] / drivers / power / lc709203f_battery.c
1 /*
2  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <asm/unaligned.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/power_supply.h>
26 #include <linux/slab.h>
27 #include <linux/power/battery-charger-gauge-comm.h>
28 #include <linux/pm.h>
29 #include <linux/jiffies.h>
30 #include <linux/interrupt.h>
31
32 #define LC709203F_THERMISTOR_B          0x06
33 #define LC709203F_INITIAL_RSOC          0x07
34 #define LC709203F_TEMPERATURE           0x08
35 #define LC709203F_VOLTAGE               0x09
36
37 #define LC709203F_ADJUSTMENT_PACK_APPLI 0x0B
38 #define LC709203F_ADJUSTMENT_PACK_THERM 0x0C
39 #define LC709203F_RSOC                  0x0D
40 #define LC709203F_INDICATOR_TO_EMPTY    0x0F
41
42 #define LC709203F_IC_VERSION            0x11
43 #define LC709203F_CHANGE_OF_THE_PARAM   0x12
44 #define LC709203F_ALARM_LOW_CELL_RSOC   0x13
45 #define LC709203F_ALARM_LOW_CELL_VOLT   0x14
46 #define LC709203F_IC_POWER_MODE         0x15
47 #define LC709203F_STATUS_BIT            0x16
48 #define LC709203F_NUM_OF_THE_PARAM      0x1A
49
50 #define LC709203F_DELAY                 (30*HZ)
51 #define LC709203F_MAX_REGS              0x1A
52
53 #define LC709203F_BATTERY_LOW           15
54 #define LC709203F_BATTERY_FULL          100
55
56 struct lc709203f_platform_data {
57         const char *tz_name;
58         u32 initial_rsoc;
59         u32 appli_adjustment;
60         u32 thermistor_beta;
61         u32 therm_adjustment;
62         u32 threshold_soc;
63         u32 maximum_soc;
64         u32 alert_low_rsoc;
65         u32 alert_low_voltage;
66         bool support_battery_current;
67 };
68
69 struct lc709203f_chip {
70         struct i2c_client               *client;
71         struct delayed_work             work;
72         struct power_supply             battery;
73         struct lc709203f_platform_data  *pdata;
74         struct battery_gauge_dev        *bg_dev;
75
76         /* battery voltage */
77         int vcell;
78         /* battery capacity */
79         int soc;
80         /* State Of Charge */
81         int status;
82         /* battery health */
83         int health;
84         /* battery capacity */
85         int capacity_level;
86
87         int temperature;
88
89         int lasttime_soc;
90         int lasttime_status;
91         int shutdown_complete;
92         int charge_complete;
93         struct mutex mutex;
94         int read_failed;
95 };
96
97 static int lc709203f_read_word(struct i2c_client *client, u8 reg)
98 {
99         int ret;
100
101         ret = i2c_smbus_read_word_data(client, reg);
102         if (ret < 0)
103                 dev_err(&client->dev, "err reading reg: 0x%x, %d\n", reg, ret);
104         return ret;
105 }
106
107 static int lc709203f_write_word(struct i2c_client *client, u8 reg, u16 value)
108 {
109         int ret;
110
111         ret = i2c_smbus_write_word_data(client, reg, value);
112         if (ret < 0)
113                 dev_err(&client->dev, "err writing 0x%0x, %d\n" , reg, ret);
114
115         return ret;
116 }
117
118 static int lc709203f_get_battery_soc(struct battery_gauge_dev *bg_dev)
119 {
120         struct lc709203f_chip *chip = battery_gauge_get_drvdata(bg_dev);
121         int val;
122
123         val = lc709203f_read_word(chip->client, LC709203F_RSOC);
124         if (val < 0)
125                 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
126         else
127                 val =  battery_gauge_get_adjusted_soc(chip->bg_dev,
128                                 chip->pdata->threshold_soc,
129                                 chip->pdata->maximum_soc, val * 100);
130
131         return val;
132 }
133
134 static int lc709203f_update_soc_voltage(struct lc709203f_chip *chip)
135 {
136         int val;
137
138         val = lc709203f_read_word(chip->client, LC709203F_VOLTAGE);
139         if (val < 0)
140                 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
141         else
142                 chip->vcell = val;
143
144         val = lc709203f_read_word(chip->client, LC709203F_RSOC);
145         if (val < 0)
146                 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
147         else
148                 chip->soc = battery_gauge_get_adjusted_soc(chip->bg_dev,
149                                 chip->pdata->threshold_soc,
150                                 chip->pdata->maximum_soc, val * 100);
151
152         if (chip->soc == LC709203F_BATTERY_FULL && chip->charge_complete) {
153                 chip->status = POWER_SUPPLY_STATUS_FULL;
154                 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
155                 chip->health = POWER_SUPPLY_HEALTH_GOOD;
156         } else if (chip->soc < LC709203F_BATTERY_LOW) {
157                 chip->status = chip->lasttime_status;
158                 chip->health = POWER_SUPPLY_HEALTH_DEAD;
159                 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
160         } else {
161                 chip->charge_complete = 0;
162                 chip->status = chip->lasttime_status;
163                 chip->health = POWER_SUPPLY_HEALTH_GOOD;
164                 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
165         }
166
167         return 0;
168 }
169
170 static void lc709203f_work(struct work_struct *work)
171 {
172         struct lc709203f_chip *chip;
173         int val;
174         int temperature;
175
176         chip = container_of(work, struct lc709203f_chip, work.work);
177
178         mutex_lock(&chip->mutex);
179         if (chip->shutdown_complete) {
180                 mutex_unlock(&chip->mutex);
181                 return;
182         }
183
184         lc709203f_update_soc_voltage(chip);
185
186         if (chip->soc != chip->lasttime_soc ||
187                 chip->status != chip->lasttime_status) {
188                 chip->lasttime_soc = chip->soc;
189                 power_supply_changed(&chip->battery);
190         }
191
192         if (chip->pdata->tz_name) {
193                 val = battery_gauge_get_battery_temperature(chip->bg_dev,
194                                                         &temperature);
195                 if (val < 0) {
196                         dev_err(&chip->client->dev, "temp invalid\n");
197                 } else {
198                         lc709203f_write_word(chip->client, LC709203F_TEMPERATURE
199                                                 , temperature * 10 + 2732);
200                         chip->temperature = temperature;
201                 }
202         }
203
204         mutex_unlock(&chip->mutex);
205         battery_gauge_report_battery_soc(chip->bg_dev, chip->soc);
206         schedule_delayed_work(&chip->work, LC709203F_DELAY);
207 }
208
209 static int lc709203f_get_temperature(struct lc709203f_chip *chip)
210 {
211         int val;
212         int retries = 2;
213         int i;
214
215         if (chip->shutdown_complete)
216                 return chip->temperature;
217
218         for (i = 0; i < retries; i++) {
219                 val = lc709203f_read_word(chip->client, LC709203F_TEMPERATURE);
220                 if (val < 0)
221                         continue;
222         }
223
224         if (val < 0) {
225                 chip->read_failed++;
226                 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
227                 if (chip->read_failed > 50)
228                         return val;
229                 return chip->temperature;
230         }
231         chip->read_failed = 0;;
232         chip->temperature = val;
233         return val;
234 }
235
236 static enum power_supply_property lc709203f_battery_props[] = {
237         POWER_SUPPLY_PROP_TECHNOLOGY,
238         POWER_SUPPLY_PROP_STATUS,
239         POWER_SUPPLY_PROP_VOLTAGE_NOW,
240         POWER_SUPPLY_PROP_CAPACITY,
241         POWER_SUPPLY_PROP_HEALTH,
242         POWER_SUPPLY_PROP_PRESENT,
243         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
244         POWER_SUPPLY_PROP_TEMP,
245         POWER_SUPPLY_PROP_CURRENT_NOW,
246 };
247
248 static int lc709203f_get_property(struct power_supply *psy,
249                             enum power_supply_property psp,
250                             union power_supply_propval *val)
251 {
252         struct lc709203f_chip *chip = container_of(psy,
253                                 struct lc709203f_chip, battery);
254         int temperature;
255         int curr_ma;
256         int ret = 0;
257
258         mutex_lock(&chip->mutex);
259
260         switch (psp) {
261         case POWER_SUPPLY_PROP_TECHNOLOGY:
262                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
263                 break;
264         case POWER_SUPPLY_PROP_STATUS:
265                 val->intval = chip->status;
266                 break;
267         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
268                 val->intval = 1000 * chip->vcell;
269                 break;
270         case POWER_SUPPLY_PROP_CAPACITY:
271                 val->intval = chip->soc;
272                 if (chip->soc == 15)
273                         dev_warn(&chip->client->dev,
274                         "\nSystem Running low on battery - 15 percent\n");
275                 if (chip->soc == 10)
276                         dev_warn(&chip->client->dev,
277                         "\nSystem Running low on battery - 10 percent\n");
278                 if (chip->soc == 5)
279                         dev_warn(&chip->client->dev,
280                         "\nSystem Running low on battery - 5 percent\n");
281                 break;
282         case POWER_SUPPLY_PROP_HEALTH:
283                 val->intval = chip->health;
284                 break;
285         case POWER_SUPPLY_PROP_PRESENT:
286                 val->intval = 1;
287                 break;
288         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
289                 val->intval = chip->capacity_level;
290                 break;
291         case POWER_SUPPLY_PROP_TEMP:
292                 temperature = lc709203f_get_temperature(chip);
293                 /*
294                    Temp ready by device is deci-kelvin
295                    C = K -273.2
296                    Report temp in dec-celcius.
297                 */
298                 val->intval = temperature - 2732;
299                 break;
300         case POWER_SUPPLY_PROP_CURRENT_NOW:
301                 val->intval = 0;
302                 ret = battery_gauge_get_battery_current(chip->bg_dev, &curr_ma);
303                 if (!ret)
304                         val->intval = 1000 * curr_ma;
305                 break;
306         default:
307                 ret = -EINVAL;
308                 break;
309         }
310
311         mutex_unlock(&chip->mutex);
312         return ret;
313 }
314
315 static int lc709203f_update_battery_status(struct battery_gauge_dev *bg_dev,
316                 enum battery_charger_status status)
317 {
318         struct lc709203f_chip *chip = battery_gauge_get_drvdata(bg_dev);
319         int val;
320
321         mutex_lock(&chip->mutex);
322         if (chip->shutdown_complete) {
323                 mutex_unlock(&chip->mutex);
324                 return 0;
325         }
326
327         val = lc709203f_read_word(chip->client, LC709203F_RSOC);
328         if (val < 0)
329                 dev_err(&chip->client->dev, "%s: err %d\n",
330                         __func__, val);
331         else
332                 chip->soc = battery_gauge_get_adjusted_soc(chip->bg_dev,
333                                 chip->pdata->threshold_soc,
334                                 chip->pdata->maximum_soc, val * 100);
335
336         if (status == BATTERY_CHARGING) {
337                 chip->charge_complete = 0;
338                 chip->status = POWER_SUPPLY_STATUS_CHARGING;
339         } else if (status == BATTERY_CHARGING_DONE) {
340                 if (chip->soc == LC709203F_BATTERY_FULL) {
341                         chip->charge_complete = 1;
342                         chip->status = POWER_SUPPLY_STATUS_FULL;
343                         chip->capacity_level =
344                                 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
345                 }
346                 goto done;
347         } else {
348                 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
349                 chip->charge_complete = 0;
350         }
351         chip->lasttime_status = chip->status;
352
353 done:
354         mutex_unlock(&chip->mutex);
355         power_supply_changed(&chip->battery);
356         dev_info(&chip->client->dev,
357                 "%s() Battery status: %d and SoC: %d%% UI status: %d\n",
358                 __func__, status, chip->soc, chip->status);
359
360         return 0;
361 }
362
363 static struct battery_gauge_ops lc709203f_bg_ops = {
364         .update_battery_status = lc709203f_update_battery_status,
365         .get_battery_soc = lc709203f_get_battery_soc,
366 };
367
368 static struct battery_gauge_info lc709203f_bgi = {
369         .cell_id = 0,
370         .bg_ops = &lc709203f_bg_ops,
371         .current_channel_name = "battery-current",
372 };
373
374 static irqreturn_t lc709203f_irq(int id, void *dev)
375 {
376         struct lc709203f_chip *chip = dev;
377         struct i2c_client *client = chip->client;
378
379         dev_info(&client->dev, "%s(): STATUS_VL\n", __func__);
380         /* Forced set SOC 0 to power off */
381         chip->soc = 0;
382         chip->lasttime_soc = chip->soc;
383         chip->status = chip->lasttime_status;
384         chip->health = POWER_SUPPLY_HEALTH_DEAD;
385         chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
386         power_supply_changed(&chip->battery);
387
388         return IRQ_HANDLED;
389 }
390
391 static void of_lc709203f_parse_platform_data(struct i2c_client *client,
392                                 struct lc709203f_platform_data *pdata)
393 {
394         char const *pstr;
395         struct device_node *np = client->dev.of_node;
396         u32 pval;
397         int ret;
398
399         ret = of_property_read_u32(np, "onsemi,initial-rsoc", &pval);
400         if (!ret)
401                 pdata->initial_rsoc = pval;
402
403         ret = of_property_read_u32(np, "onsemi,appli-adjustment", &pval);
404         if (!ret)
405                 pdata->appli_adjustment = pval;
406
407         pdata->tz_name = NULL;
408         ret = of_property_read_string(np, "onsemi,tz-name", &pstr);
409         if (!ret)
410                 pdata->tz_name = pstr;
411
412         ret = of_property_read_u32(np, "onsemi,thermistor-beta", &pval);
413         if (!ret) {
414                 pdata->thermistor_beta = pval;
415         } else {
416                 if (!pdata->tz_name)
417                         dev_warn(&client->dev,
418                                 "Thermistor beta not provided\n");
419         }
420
421         ret = of_property_read_u32(np, "onsemi,thermistor-adjustment", &pval);
422         if (!ret)
423                 pdata->therm_adjustment = pval;
424
425         ret = of_property_read_u32(np, "onsemi,kernel-threshold-soc", &pval);
426         if (!ret)
427                 pdata->threshold_soc = pval;
428
429         ret = of_property_read_u32(np, "onsemi,kernel-maximum-soc", &pval);
430         if (!ret)
431                 pdata->maximum_soc = pval;
432         else
433                 pdata->maximum_soc = 100;
434
435         ret = of_property_read_u32(np, "onsemi,alert-low-rsoc", &pval);
436         if (!ret)
437                 pdata->alert_low_rsoc = pval;
438
439         ret = of_property_read_u32(np, "onsemi,alert-low-voltage", &pval);
440         if (!ret)
441                 pdata->alert_low_voltage = pval;
442
443         pdata->support_battery_current = of_property_read_bool(np,
444                                                 "io-channel-names");
445 }
446
447 #ifdef CONFIG_DEBUG_FS
448
449 #include <linux/debugfs.h>
450 #include <linux/seq_file.h>
451
452 static struct dentry *debugfs_root;
453 static u8 valid_command[] = {0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xD, 0xF,
454                         0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x1A};
455 static int dbg_lc709203f_show(struct seq_file *s, void *data)
456 {
457         struct i2c_client *client = s->private;
458         int ret;
459         int i;
460
461         seq_puts(s, "Register-->Value(16bit)\n");
462         for (i = 0; i < ARRAY_SIZE(valid_command); ++i) {
463                 ret = lc709203f_read_word(client, valid_command[i]);
464                 if (ret < 0)
465                         seq_printf(s, "0x%02x: ERROR\n", valid_command[i]);
466                 else
467                         seq_printf(s, "0x%02x: 0x%04x\n",
468                                                 valid_command[i], ret);
469         }
470         return 0;
471 }
472
473 static int dbg_lc709203f_open(struct inode *inode, struct file *file)
474 {
475         return single_open(file, dbg_lc709203f_show, inode->i_private);
476 }
477
478 static const struct file_operations lc709203f_debug_fops = {
479         .open           = dbg_lc709203f_open,
480         .read           = seq_read,
481         .llseek         = seq_lseek,
482         .release        = single_release,
483 };
484
485 static int lc709203f_debugfs_init(struct i2c_client *client)
486 {
487         debugfs_root = debugfs_create_dir("lc709203f", NULL);
488         if (!debugfs_root)
489                 pr_warn("lc709203f: Failed to create debugfs directory\n");
490
491         (void) debugfs_create_file("registers", S_IRUGO,
492                         debugfs_root, (void *)client, &lc709203f_debug_fops);
493         return 0;
494 }
495 #else
496 static int lc709203f_debugfs_init(struct i2c_client *client)
497 {
498         return 0;
499 }
500 #endif
501
502 static int lc709203f_probe(struct i2c_client *client,
503                           const struct i2c_device_id *id)
504 {
505         struct lc709203f_chip *chip;
506         int ret;
507
508         /* Required PEC functionality */
509         client->flags = client->flags | I2C_CLIENT_PEC;
510
511         /* Check if device exist or not */
512         ret = i2c_smbus_read_word_data(client, LC709203F_NUM_OF_THE_PARAM);
513         if (ret < 0) {
514                 dev_err(&client->dev, "device is not responding, %d\n", ret);
515                 return ret;
516         }
517
518         dev_info(&client->dev, "Device Params 0x%04x\n", ret);
519
520         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
521         if (!chip)
522                 return -ENOMEM;
523
524         chip->client = client;
525         if (client->dev.of_node) {
526                 chip->pdata = devm_kzalloc(&client->dev,
527                                         sizeof(*chip->pdata), GFP_KERNEL);
528                 if (!chip->pdata)
529                         return -ENOMEM;
530                 of_lc709203f_parse_platform_data(client, chip->pdata);
531         } else {
532                 chip->pdata = client->dev.platform_data;
533         }
534
535         if (!chip->pdata)
536                 return -ENODATA;
537
538         mutex_init(&chip->mutex);
539         chip->shutdown_complete = 0;
540         i2c_set_clientdata(client, chip);
541
542         if (chip->pdata->initial_rsoc) {
543                 ret = lc709203f_write_word(chip->client,
544                         LC709203F_INITIAL_RSOC, chip->pdata->initial_rsoc);
545                 if (ret < 0) {
546                         dev_err(&client->dev,
547                                 "INITIAL_RSOC write failed: %d\n", ret);
548                         return ret;
549                 }
550                 dev_info(&client->dev, "initial-rsoc: 0x%04x\n",
551                         chip->pdata->initial_rsoc);
552         }
553
554         ret = lc709203f_write_word(chip->client,
555                 LC709203F_ALARM_LOW_CELL_RSOC, chip->pdata->alert_low_rsoc);
556         if (ret < 0) {
557                 dev_err(&client->dev, "LOW_RSOC write failed: %d\n", ret);
558                 return ret;
559         }
560
561         ret = lc709203f_write_word(chip->client,
562                 LC709203F_ALARM_LOW_CELL_VOLT, chip->pdata->alert_low_voltage);
563         if (ret < 0) {
564                 dev_err(&client->dev, "LOW_VOLT write failed: %d\n", ret);
565                 return ret;
566         }
567
568         if (chip->pdata->appli_adjustment) {
569                 ret = lc709203f_write_word(chip->client,
570                         LC709203F_ADJUSTMENT_PACK_APPLI,
571                         chip->pdata->appli_adjustment);
572                 if (ret < 0) {
573                         dev_err(&client->dev,
574                                 "ADJUSTMENT_APPLI write failed: %d\n", ret);
575                         return ret;
576                 }
577         }
578
579         if (chip->pdata->tz_name || !chip->pdata->thermistor_beta)
580                 goto skip_thermistor_config;
581
582         if (chip->pdata->therm_adjustment) {
583                 ret = lc709203f_write_word(chip->client,
584                         LC709203F_ADJUSTMENT_PACK_THERM,
585                         chip->pdata->therm_adjustment);
586                 if (ret < 0) {
587                         dev_err(&client->dev,
588                                 "ADJUSTMENT_THERM write failed: %d\n", ret);
589                         return ret;
590                 }
591         }
592
593         ret = lc709203f_write_word(chip->client,
594                 LC709203F_THERMISTOR_B, chip->pdata->thermistor_beta);
595         if (ret < 0) {
596                 dev_err(&client->dev, "THERMISTOR_B write failed: %d\n", ret);
597                 return ret;
598         }
599
600         ret = lc709203f_write_word(chip->client, LC709203F_STATUS_BIT, 0x1);
601         if (ret < 0) {
602                 dev_err(&client->dev, "STATUS_BIT write failed: %d\n", ret);
603                 return ret;
604         }
605
606 skip_thermistor_config:
607         lc709203f_update_soc_voltage(chip);
608
609         chip->battery.name              = "battery";
610         chip->battery.type              = POWER_SUPPLY_TYPE_BATTERY;
611         chip->battery.get_property      = lc709203f_get_property;
612         chip->battery.properties        = lc709203f_battery_props;
613         chip->battery.num_properties    = ARRAY_SIZE(lc709203f_battery_props);
614         chip->status                    = POWER_SUPPLY_STATUS_DISCHARGING;
615         chip->lasttime_status           = POWER_SUPPLY_STATUS_DISCHARGING;
616         chip->charge_complete           = 0;
617
618         /* Remove current property if it is not supported */
619         if (!chip->pdata->support_battery_current)
620                 chip->battery.num_properties--;
621
622         ret = power_supply_register(&client->dev, &chip->battery);
623         if (ret) {
624                 dev_err(&client->dev, "failed: power supply register\n");
625                 goto error;
626         }
627
628         lc709203f_bgi.tz_name = chip->pdata->tz_name;
629         chip->bg_dev = battery_gauge_register(&client->dev, &lc709203f_bgi,
630                                 chip);
631         if (IS_ERR(chip->bg_dev)) {
632                 ret = PTR_ERR(chip->bg_dev);
633                 dev_err(&client->dev, "battery gauge register failed: %d\n",
634                         ret);
635                 goto bg_err;
636         }
637
638         INIT_DEFERRABLE_WORK(&chip->work, lc709203f_work);
639         schedule_delayed_work(&chip->work, 0);
640
641         lc709203f_debugfs_init(client);
642
643         if (client->irq) {
644                 ret = devm_request_threaded_irq(&client->dev, client->irq,
645                         NULL, lc709203f_irq,
646                         IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
647                         dev_name(&client->dev), chip);
648                 if (ret < 0) {
649                         dev_err(&client->dev,
650                                 "%s: request IRQ %d fail, err = %d\n",
651                                 __func__, client->irq, ret);
652                         client->irq = 0;
653                         goto irq_reg_error;
654                 }
655         }
656         device_set_wakeup_capable(&client->dev, 1);
657
658         dev_info(&client->dev, "Battery Voltage %dmV and SoC %d%%\n",
659                         chip->vcell, chip->soc);
660
661         return 0;
662 irq_reg_error:
663         cancel_delayed_work_sync(&chip->work);
664 bg_err:
665         power_supply_unregister(&chip->battery);
666 error:
667         mutex_destroy(&chip->mutex);
668
669         return ret;
670 }
671
672 static int lc709203f_remove(struct i2c_client *client)
673 {
674         struct lc709203f_chip *chip = i2c_get_clientdata(client);
675
676         battery_gauge_unregister(chip->bg_dev);
677         power_supply_unregister(&chip->battery);
678         cancel_delayed_work_sync(&chip->work);
679         mutex_destroy(&chip->mutex);
680
681         return 0;
682 }
683
684 static void lc709203f_shutdown(struct i2c_client *client)
685 {
686         struct lc709203f_chip *chip = i2c_get_clientdata(client);
687
688         mutex_lock(&chip->mutex);
689         chip->shutdown_complete = 1;
690         mutex_unlock(&chip->mutex);
691
692         cancel_delayed_work_sync(&chip->work);
693         dev_info(&chip->client->dev, "At shutdown Voltage %dmV and SoC %d%%\n",
694                         chip->vcell, chip->soc);
695 }
696
697 #ifdef CONFIG_PM_SLEEP
698 static int lc709203f_suspend(struct device *dev)
699 {
700         struct lc709203f_chip *chip = dev_get_drvdata(dev);
701         cancel_delayed_work_sync(&chip->work);
702
703         if (device_may_wakeup(&chip->client->dev))
704                 enable_irq_wake(chip->client->irq);
705
706         dev_info(&chip->client->dev, "At suspend Voltage %dmV and SoC %d%%\n",
707                         chip->vcell, chip->soc);
708
709         return 0;
710 }
711
712 static int lc709203f_resume(struct device *dev)
713 {
714         struct lc709203f_chip *chip = dev_get_drvdata(dev);
715
716         if (device_may_wakeup(&chip->client->dev))
717                 disable_irq_wake(chip->client->irq);
718
719         mutex_lock(&chip->mutex);
720         lc709203f_update_soc_voltage(chip);
721         mutex_unlock(&chip->mutex);
722         battery_gauge_report_battery_soc(chip->bg_dev, chip->soc);
723
724         dev_info(&chip->client->dev, "At resume Voltage %dmV and SoC %d%%\n",
725                         chip->vcell, chip->soc);
726
727         schedule_delayed_work(&chip->work, LC709203F_DELAY);
728         return 0;
729 }
730 #endif /* CONFIG_PM_SLEEP */
731
732 static SIMPLE_DEV_PM_OPS(lc709203f_pm_ops, lc709203f_suspend, lc709203f_resume);
733
734 static const struct i2c_device_id lc709203f_id[] = {
735         { "lc709203f", 0 },
736         { }
737 };
738 MODULE_DEVICE_TABLE(i2c, lc709203f_id);
739
740 static struct i2c_driver lc709203f_i2c_driver = {
741         .driver = {
742                 .name   = "lc709203f",
743                 .pm = &lc709203f_pm_ops,
744         },
745         .probe          = lc709203f_probe,
746         .remove         = lc709203f_remove,
747         .id_table       = lc709203f_id,
748         .shutdown       = lc709203f_shutdown,
749 };
750
751 static int __init lc709203f_init(void)
752 {
753         return i2c_add_driver(&lc709203f_i2c_driver);
754 }
755 fs_initcall_sync(lc709203f_init);
756
757 static void __exit lc709203f_exit(void)
758 {
759         i2c_del_driver(&lc709203f_i2c_driver);
760 }
761 module_exit(lc709203f_exit);
762
763 MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
764 MODULE_DESCRIPTION("OnSemi LC709203F Fuel Gauge");
765 MODULE_LICENSE("GPL v2");