460fe3b729ce519a674f3399adb466ea63068e37
[linux-2.6.git] / drivers / power / bq2419x-charger.c
1 /*
2  * bq2419x-charger.c -- BQ24190/BQ24192/BQ24192i/BQ24193 Charger driver
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * Author: Laxman Dewangan <ldewangan@nvidia.com>
7  * Author: Syed Rafiuddin <srafiuddin@nvidia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation version 2.
12  *
13  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
14  * whether express or implied; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307, USA
22  */
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/kthread.h>
29 #include <linux/sched.h>
30 #include <linux/time.h>
31 #include <linux/timer.h>
32 #include <linux/gpio.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/power/bq2419x-charger.h>
36 #include <linux/regmap.h>
37 #include <linux/regmap.h>
38 #include <linux/regulator/driver.h>
39 #include <linux/regulator/machine.h>
40 #include <linux/slab.h>
41 #include <linux/rtc.h>
42 #include <linux/alarmtimer.h>
43 #include <generated/mach-types.h>
44
45 enum bq2419x_version {
46         BQ24190_IC = 0,
47         BQ24192_IC,
48         BQ24193_IC,
49         BQ24192i_IC,
50 };
51
52 /* input current limit */
53 static const unsigned int iinlim[] = {
54         100, 150, 500, 900, 1200, 1500, 2000, 3000,
55 };
56
57 /* Kthread scheduling parameters */
58 struct sched_param bq2419x_param = {
59         .sched_priority = MAX_RT_PRIO - 1,
60 };
61
62 static const struct regmap_config bq2419x_regmap_config = {
63         .reg_bits               = 8,
64         .val_bits               = 8,
65         .max_register           = BQ2419X_MAX_REGS,
66 };
67
68 struct bq2419x_chip {
69         struct device                   *dev;
70         struct regmap                   *regmap;
71         int                             irq;
72         int                             gpio_otg_iusb;
73         int                             wdt_refresh_timeout;
74         int                             wdt_time_sec;
75
76         struct mutex                    mutex;
77         int                             in_current_limit;
78         int                             status;
79         int                             rtc_alarm_time;
80         void                            (*update_status)(int, int);
81         int                             (*soc_check)(void);
82         int                             (*vcell_check)(void);
83         s32                             (*current_check)(void);
84         struct regulator_dev            *chg_rdev;
85         struct regulator_desc           chg_reg_desc;
86         struct regulator_init_data      chg_reg_init_data;
87
88         struct regulator_dev            *vbus_rdev;
89         struct regulator_desc           vbus_reg_desc;
90         struct regulator_init_data      vbus_reg_init_data;
91
92         struct kthread_worker           bq_kworker;
93         struct task_struct              *bq_kworker_task;
94         struct kthread_work             bq_wdt_work;
95         struct rtc_device               *rtc;
96         int                             stop_thread;
97         int                             suspended;
98         int                             chg_restart_timeout;
99         int                             chg_restart_time;
100         int                             chg_complete_soc;
101         int                             chg_enable;
102         int                             chip_version;
103 };
104
105 #define FULL_CHARGE_SOC                 100
106 #define MAX_CHARING_VOLTAGE             4202
107 #define RECHARING_VOLTAGE               4140
108 #define CHARING_COMPLETE_CURRENT        -256
109
110 static int chg_complete_check;
111
112 static int current_to_reg(const unsigned int *tbl,
113                         size_t size, unsigned int val)
114 {
115         size_t i;
116
117         for (i = 0; i < size; i++)
118                 if (val < tbl[i])
119                         break;
120         return i > 0 ? i - 1 : -EINVAL;
121 }
122
123 static int bq2419x_charger_disable(struct bq2419x_chip *bq2419x)
124 {
125         int ret;
126
127         ret = regmap_update_bits(bq2419x->regmap, BQ2419X_PWR_ON_REG,
128                          BQ2419X_ENABLE_CHARGE_MASK,
129                          BQ2419X_DISABLE_CHARGE);
130         if (ret < 0)
131                 dev_err(bq2419x->dev, "register update failed, err %d\n", ret);
132         return ret;
133 }
134
135 static int bq2419x_charger_enable(struct bq2419x_chip *bq2419x)
136 {
137         int ret;
138
139         if (bq2419x->chg_enable) {
140                 /* charging enabled reset flag and timeout*/
141                 chg_complete_check = 0;
142                 bq2419x->chg_restart_timeout = 0;
143
144                 ret = regmap_update_bits(bq2419x->regmap, BQ2419X_PWR_ON_REG,
145                          BQ2419X_ENABLE_CHARGE_MASK, BQ2419X_ENABLE_CHARGE);
146         } else {
147                 ret = regmap_update_bits(bq2419x->regmap, BQ2419X_PWR_ON_REG,
148                                  BQ2419X_ENABLE_CHARGE_MASK,
149                                  BQ2419X_DISABLE_CHARGE);
150         }
151         if (ret < 0)
152                 dev_err(bq2419x->dev, "register update failed, err %d\n", ret);
153         return ret;
154 }
155
156 static int bq2419x_vbus_regulator_enable_time(struct regulator_dev *rdev)
157 {
158         return 8000;
159 }
160
161 static int bq2419x_vbus_enable(struct regulator_dev *rdev)
162 {
163         struct bq2419x_chip *bq2419x = rdev_get_drvdata(rdev);
164         int ret;
165
166         dev_info(bq2419x->dev, "VBUS enabled, charging disabled\n");
167
168         ret = regmap_update_bits(bq2419x->regmap, BQ2419X_PWR_ON_REG,
169                         BQ2419X_ENABLE_CHARGE_MASK, BQ2419X_ENABLE_VBUS);
170         if (ret < 0)
171                 dev_err(bq2419x->dev, "PWR_ON_REG update failed %d", ret);
172
173         return ret;
174 }
175
176 static int bq2419x_vbus_disable(struct regulator_dev *rdev)
177 {
178         struct bq2419x_chip *bq2419x = rdev_get_drvdata(rdev);
179         int ret;
180
181         dev_info(bq2419x->dev, "VBUS disabled, charging enabled\n");
182         ret = bq2419x_charger_enable(bq2419x);
183         if (ret < 0) {
184                 dev_err(bq2419x->dev, "Charger enable failed %d", ret);
185                 return ret;
186         }
187
188         return ret;
189 }
190
191 static int bq2419x_vbus_is_enabled(struct regulator_dev *rdev)
192 {
193         struct bq2419x_chip *bq2419x = rdev_get_drvdata(rdev);
194         int ret;
195         unsigned int data;
196
197         ret = regmap_read(bq2419x->regmap, BQ2419X_PWR_ON_REG, &data);
198         if (ret < 0) {
199                 dev_err(bq2419x->dev, "PWR_ON_REG read failed %d", ret);
200                 return ret;
201         }
202         return (data & BQ2419X_ENABLE_CHARGE_MASK) == BQ2419X_ENABLE_VBUS;
203 }
204
205 static struct regulator_ops bq2419x_vbus_ops = {
206         .enable         = bq2419x_vbus_enable,
207         .disable        = bq2419x_vbus_disable,
208         .is_enabled     = bq2419x_vbus_is_enabled,
209         .enable_time    = bq2419x_vbus_regulator_enable_time,
210 };
211
212 static int bq2419x_init(struct bq2419x_chip *bq2419x)
213 {
214         int val = 0;
215         int ret = 0;
216         int floor = 0;
217
218         /* Configure input voltage to 4.52 in case of NV charger */
219         if (machine_is_roth() && (bq2419x->in_current_limit == 2000))
220                 val |= BQ2419x_NVCHARGER_INPUT_VOL_SEL;
221         else
222                 val |= BQ2419x_DEFAULT_INPUT_VOL_SEL;
223
224         /* Clear EN_HIZ */
225         ret = regmap_update_bits(bq2419x->regmap, BQ2419X_INPUT_SRC_REG,
226                         BQ2419X_EN_HIZ | BQ2419x_INPUT_VOLTAGE_MASK, val);
227         if (ret < 0) {
228                 dev_err(bq2419x->dev, "INPUT_SRC_REG update failed %d\n", ret);
229                 return ret;
230         }
231
232         /* Set input current limit to 2A in case of DCP */
233         if (machine_is_tegratab() && (bq2419x->in_current_limit >= 1800))
234                 bq2419x->in_current_limit = 2000;
235
236         dev_info(bq2419x->dev,
237                 "bq2419x_init: bq2419x->in_current_limit %d\n",
238                 bq2419x->in_current_limit);
239
240         /* Configure input current limit */
241         val = current_to_reg(iinlim, ARRAY_SIZE(iinlim),
242                                 bq2419x->in_current_limit);
243
244         /* Start from 500mA and then step to val */
245         floor = current_to_reg(iinlim, ARRAY_SIZE(iinlim), 500);
246         if (val < 0 || floor < 0)
247                 return 0;
248
249         for (; floor <= val; floor++) {
250                 udelay(BQ2419x_CHARGING_CURRENT_STEP_DELAY_US);
251                 ret = regmap_update_bits(bq2419x->regmap, BQ2419X_INPUT_SRC_REG,
252                                 BQ2419x_CONFIG_MASK, floor);
253                 if (ret < 0)
254                         dev_err(bq2419x->dev,
255                                 "INPUT_SRC_REG update failed: %d\n", ret);
256         }
257         return ret;
258 }
259
260 static int bq2419x_charger_init(struct bq2419x_chip *bq2419x)
261 {
262         int ret;
263
264         if (machine_is_tegratab()) {
265                 /* InCharge Limit=3.136A, Battery Spec is 3.2A */
266                 ret = regmap_write(bq2419x->regmap,
267                                 BQ2419X_CHRG_CTRL_REG, 0xA4);
268                 if (ret < 0) {
269                         dev_err(bq2419x->dev,
270                         "CHRG_CTRL_REG write failed %d\n", ret);
271                         return ret;
272                 }
273         } else if (machine_is_roth()) {
274                 /* Configure Output Current Control to 2.25A*/
275                 ret = regmap_write(bq2419x->regmap,
276                                 BQ2419X_CHRG_CTRL_REG, 0x6c);
277                 if (ret < 0) {
278                         dev_err(bq2419x->dev,
279                         "CHRG_CTRL_REG write failed %d\n", ret);
280                         return ret;
281                 }
282         }
283
284         if (bq2419x->chip_version == BQ24193_IC) {
285                 /* JEITA_ISET=20%*/
286                 ret = regmap_update_bits(bq2419x->regmap,
287                         BQ2419X_TIME_CTRL_REG, BQ2419X_JEITA_ISET_MASK,
288                         BQ2419X_JEITA_ISET_20);
289                 if (ret < 0) {
290                         dev_err(bq2419x->dev,
291                         "TIME_CTRL_REG update failed %d\n", ret);
292                         return ret;
293                 }
294                 /* JEITA_VSET=4.2V*/
295                 ret = regmap_update_bits(bq2419x->regmap,
296                         BQ2419X_MISC_OPER_REG, BQ2419X_JEITA_VSET_MASK,
297                         BQ2419X_JEITA_VSET_4_20V);
298                 if (ret < 0) {
299                         dev_err(bq2419x->dev,
300                         "MISC_OPER_REG update failed %d\n", ret);
301                         return ret;
302                 }
303         }
304
305         if (bq2419x->chg_complete_soc && bq2419x->soc_check) {
306                 /* Not use termination, will check SOC*/
307                 ret = regmap_update_bits(bq2419x->regmap,
308                         BQ2419X_TIME_CTRL_REG, 0x80, 0);
309                 if (ret < 0) {
310                         dev_err(bq2419x->dev,
311                         "TIME_CTRL_REG update failed %d\n", ret);
312                         return ret;
313                 }
314         }
315
316         /*
317          * Configure Input voltage limit reset to OTP value,
318          * and charging current to 500mA.
319          */
320         ret = regmap_write(bq2419x->regmap, BQ2419X_INPUT_SRC_REG, 0x32);
321         if (ret < 0)
322                 dev_err(bq2419x->dev, "INPUT_SRC_REG write failed %d\n", ret);
323
324         return ret;
325 }
326
327 static int bq2419x_set_charging_current(struct regulator_dev *rdev,
328                                         int min_uA, int max_uA)
329 {
330         struct bq2419x_chip *bq_charger = rdev_get_drvdata(rdev);
331         int ret = 0;
332         int val;
333
334         dev_info(bq_charger->dev, "Setting charging current %d\n", max_uA/1000);
335         msleep(200);
336         bq_charger->status = 0;
337
338         ret = bq2419x_charger_enable(bq_charger);
339         if (ret < 0)
340                 goto error;
341
342         ret = regmap_read(bq_charger->regmap, BQ2419X_SYS_STAT_REG, &val);
343         if (ret < 0)
344                 dev_err(bq_charger->dev, "error reading reg: 0x%x\n",
345                                 BQ2419X_SYS_STAT_REG);
346
347         if (max_uA == 0 && val != 0)
348                 return 0;
349
350         bq_charger->in_current_limit = max_uA/1000;
351         if ((val & BQ2419x_VBUS_STAT) == BQ2419x_VBUS_UNKNOWN) {
352                 bq_charger->in_current_limit = 500;
353                 bq_charger->status = 0;
354         } else {
355                 bq_charger->status = 1;
356         }
357         ret = bq2419x_init(bq_charger);
358         if (ret < 0)
359                 goto error;
360         if (bq_charger->update_status)
361                 bq_charger->update_status(bq_charger->status, 0);
362         return 0;
363 error:
364         dev_err(bq_charger->dev, "Charger enable failed, err = %d\n", ret);
365         return ret;
366 }
367
368 static struct regulator_ops bq2419x_tegra_regulator_ops = {
369         .set_current_limit = bq2419x_set_charging_current,
370 };
371
372 static int bq2419x_reset_wdt(struct bq2419x_chip *bq2419x, const char *from)
373 {
374         int ret = 0;
375         unsigned int reg01;
376
377         mutex_lock(&bq2419x->mutex);
378         if (bq2419x->suspended)
379                 goto scrub;
380
381         dev_info(bq2419x->dev, "%s() from %s()\n", __func__, from);
382
383         ret = regmap_read(bq2419x->regmap, BQ2419X_PWR_ON_REG, &reg01);
384         if (ret < 0) {
385                 dev_err(bq2419x->dev, "PWR_ON_REG read failed: %d\n", ret);
386                 goto scrub;
387         }
388
389         reg01 |= BIT(6);
390
391         /* Write two times to make sure reset WDT */
392         ret = regmap_write(bq2419x->regmap, BQ2419X_PWR_ON_REG, reg01);
393         if (ret < 0) {
394                 dev_err(bq2419x->dev, "PWR_ON_REG write failed: %d\n", ret);
395                 goto scrub;
396         }
397         ret = regmap_write(bq2419x->regmap, BQ2419X_PWR_ON_REG, reg01);
398         if (ret < 0) {
399                 dev_err(bq2419x->dev, "PWR_ON_REG write failed: %d\n", ret);
400                 goto scrub;
401         }
402
403 scrub:
404         mutex_unlock(&bq2419x->mutex);
405         return ret;
406 }
407
408 static int bq2419x_fault_clear_sts(struct bq2419x_chip *bq2419x)
409 {
410         int ret;
411         unsigned int reg09;
412
413         ret = regmap_read(bq2419x->regmap, BQ2419X_FAULT_REG, &reg09);
414         if (ret < 0) {
415                 dev_err(bq2419x->dev, "FAULT_REG read failed: %d\n", ret);
416                 return ret;
417         }
418
419         ret = regmap_read(bq2419x->regmap, BQ2419X_FAULT_REG, &reg09);
420         if (ret < 0)
421                 dev_err(bq2419x->dev, "FAULT_REG read failed: %d\n", ret);
422
423         return ret;
424 }
425
426 static int bq2419x_watchdog_init(struct bq2419x_chip *bq2419x,
427                         int timeout, const char *from)
428 {
429         int ret, val;
430         unsigned int reg05;
431
432         if (!timeout) {
433                 ret = regmap_update_bits(bq2419x->regmap, BQ2419X_TIME_CTRL_REG,
434                                 BQ2419X_WD_MASK, 0);
435                 if (ret < 0)
436                         dev_err(bq2419x->dev,
437                                 "TIME_CTRL_REG read failed: %d\n", ret);
438                 return ret;
439         }
440
441         if (timeout <= 60) {
442                 val = BQ2419X_WD_40ms;
443                 bq2419x->wdt_refresh_timeout = 25;
444         } else if (timeout <= 120) {
445                 val = BQ2419X_WD_80ms;
446                 bq2419x->wdt_refresh_timeout = 50;
447         } else {
448                 val = BQ2419X_WD_160ms;
449                 bq2419x->wdt_refresh_timeout = 125;
450         }
451
452         ret = regmap_read(bq2419x->regmap, BQ2419X_TIME_CTRL_REG, &reg05);
453         if (ret < 0) {
454                 dev_err(bq2419x->dev,
455                         "TIME_CTRL_REG read failed:%d\n", ret);
456                 return ret;
457         }
458
459         if ((reg05 & BQ2419X_WD_MASK) != val) {
460                 ret = regmap_update_bits(bq2419x->regmap, BQ2419X_TIME_CTRL_REG,
461                                 BQ2419X_WD_MASK, val);
462                 if (ret < 0) {
463                         dev_err(bq2419x->dev,
464                                 "TIME_CTRL_REG read failed: %d\n", ret);
465                         return ret;
466                 }
467         }
468
469         /* Clear EN_HIZ */
470         ret = regmap_update_bits(bq2419x->regmap,
471                 BQ2419X_INPUT_SRC_REG, BQ2419X_EN_HIZ, 0);
472         if (ret < 0)
473                 dev_err(bq2419x->dev, "INPUT_SRC_REG update failed:%d\n", ret);
474
475         ret = bq2419x_reset_wdt(bq2419x, from);
476         if (ret < 0)
477                 dev_err(bq2419x->dev, "bq2419x_reset_wdt failed: %d\n", ret);
478
479         return ret;
480 }
481
482 static void bq2419x_work_thread(struct kthread_work *work)
483 {
484         struct bq2419x_chip *bq2419x = container_of(work,
485                         struct bq2419x_chip, bq_wdt_work);
486         int ret;
487         int val = 0;
488         int soc;
489         int vcell;
490         s32 batt_current;
491
492         for (;;) {
493                 if (bq2419x->stop_thread)
494                         return;
495
496                 if (bq2419x->chg_restart_timeout) {
497                         mutex_lock(&bq2419x->mutex);
498                         bq2419x->chg_restart_timeout--;
499                         if (!bq2419x->chg_restart_timeout) {
500                                 dev_info(bq2419x->dev,
501                                         "Charging Restart by timer.\n");
502                                 ret = bq2419x_charger_disable(bq2419x);
503                                 if (ret < 0)
504                                         dev_err(bq2419x->dev,
505                                         "Charger disable failed %d", ret);
506                                 mdelay(100);
507                                 ret = bq2419x_charger_enable(bq2419x);
508                                 if (ret < 0)
509                                         dev_err(bq2419x->dev,
510                                         "Charger enable failed %d", ret);
511                                 ret = bq2419x_charger_init(bq2419x);
512                                 if (ret < 0)
513                                         dev_err(bq2419x->dev,
514                                         "Charger init failed: %d\n", ret);
515                                 ret = bq2419x_init(bq2419x);
516                                 if (ret < 0)
517                                         dev_err(bq2419x->dev,
518                                         "bq2419x init failed: %d\n", ret);
519
520                                 ret = regmap_read(bq2419x->regmap,
521                                         BQ2419X_SYS_STAT_REG, &val);
522                                 if (ret < 0)
523                                         dev_err(bq2419x->dev,
524                                         "SYS_STAT_REG read failed %d\n", ret);
525                                 /*
526                                 * Update Charging status based on STAT register
527                                 */
528                                 if ((val & BQ2419x_CHRG_STATE_MASK) ==
529                                         BQ2419x_CHRG_STATE_NOTCHARGING) {
530                                         bq2419x->status = 0;
531                                         if (bq2419x->update_status)
532                                                 bq2419x->update_status
533                                                         (bq2419x->status, 0);
534                                         bq2419x->chg_restart_timeout =
535                                                 bq2419x->chg_restart_time /
536                                                 bq2419x->wdt_refresh_timeout;
537                                 } else {
538                                         bq2419x->status = 1;
539                                         if (bq2419x->update_status)
540                                                 bq2419x->update_status
541                                                         (bq2419x->status, 0);
542                                 }
543                         }
544
545                         if (bq2419x->suspended)
546                                 bq2419x->chg_restart_timeout = 0;
547
548                         mutex_unlock(&bq2419x->mutex);
549                 }
550
551                 if (bq2419x->chg_complete_soc && bq2419x->soc_check) {
552                         mutex_lock(&bq2419x->mutex);
553                         soc = bq2419x->soc_check();
554
555                         /* Check voltage when complete soc is 100% */
556                         if ((bq2419x->chg_complete_soc >= FULL_CHARGE_SOC) &&
557                                 bq2419x->vcell_check)
558                                 vcell = bq2419x->vcell_check();
559                         else
560                                 vcell = MAX_CHARING_VOLTAGE;
561
562                         /* Check current when complete soc is 100% */
563                         if ((bq2419x->chg_complete_soc >= FULL_CHARGE_SOC) &&
564                                 bq2419x->current_check)
565                                 batt_current = bq2419x->current_check();
566                         else
567                                 batt_current = CHARING_COMPLETE_CURRENT;
568
569                         if ((soc >= bq2419x->chg_complete_soc) &&
570                                 (vcell >= MAX_CHARING_VOLTAGE) &&
571                                 (batt_current >= CHARING_COMPLETE_CURRENT) &&
572                                 !chg_complete_check) {
573                                 ret = regmap_read(bq2419x->regmap,
574                                         BQ2419X_SYS_STAT_REG, &val);
575                                 if (ret < 0)
576                                         dev_err(bq2419x->dev,
577                                         "SYS_STAT_REG read failed %d\n", ret);
578
579                                 if ((val & BQ2419x_CHRG_STATE_MASK) ==
580                                         BQ2419x_CHRG_STATE_POST_CHARGE) {
581                                         dev_info(bq2419x->dev,
582                                                 "Full charged Charging Stop\n");
583                                         ret = bq2419x_charger_disable(bq2419x);
584                                         if (ret < 0)
585                                                 dev_err(bq2419x->dev,
586                                                 "Charger disable failed %d",
587                                                 ret);
588
589                                         bq2419x->chg_restart_timeout =
590                                                 bq2419x->chg_restart_time /
591                                                 bq2419x->wdt_refresh_timeout;
592
593                                         chg_complete_check = 1;
594                                         mdelay(100);
595                                 }
596                         }
597
598                         if (chg_complete_check &&
599                                 (vcell <= RECHARING_VOLTAGE ||
600                                 soc < bq2419x->chg_complete_soc)) {
601                                 dev_info(bq2419x->dev,
602                                         "Charging Restart by voltage.\n");
603                                 ret = bq2419x_charger_enable(bq2419x);
604                                 if (ret < 0)
605                                         dev_err(bq2419x->dev,
606                                         "Charger enable failed %d", ret);
607                                 ret = bq2419x_charger_init(bq2419x);
608                                 if (ret < 0)
609                                         dev_err(bq2419x->dev,
610                                         "Charger init failed: %d\n", ret);
611                                 ret = bq2419x_init(bq2419x);
612                                 if (ret < 0)
613                                         dev_err(bq2419x->dev,
614                                         "bq2419x init failed: %d\n", ret);
615                         }
616                         mutex_unlock(&bq2419x->mutex);
617                 }
618
619                 /* When chg_complete_soc < 100 and charging done,
620                    cut charger power */
621                 if ((bq2419x->chg_complete_soc < FULL_CHARGE_SOC) &&
622                         chg_complete_check) {
623                         /* Set EN_HIZ for cut charger power */
624                         ret = regmap_update_bits(bq2419x->regmap,
625                                 BQ2419X_INPUT_SRC_REG, BQ2419X_EN_HIZ,
626                                 BQ2419X_EN_HIZ);
627                 } else {
628                         /* Clear EN_HIZ */
629                         ret = regmap_update_bits(bq2419x->regmap,
630                                 BQ2419X_INPUT_SRC_REG, BQ2419X_EN_HIZ, 0);
631                 }
632                 if (ret < 0)
633                         dev_err(bq2419x->dev,
634                                 "INPUT_SRC_REG update failed:%d\n", ret);
635
636                 ret = bq2419x_reset_wdt(bq2419x, "THREAD");
637                 if (ret < 0)
638                         dev_err(bq2419x->dev,
639                                 "bq2419x_reset_wdt failed: %d\n", ret);
640
641                 msleep(bq2419x->wdt_refresh_timeout * 1000);
642         }
643 }
644
645 static int bq2419x_reset_safety_timer(struct bq2419x_chip *bq2419x)
646 {
647         int ret;
648
649         ret = regmap_update_bits(bq2419x->regmap, BQ2419X_TIME_CTRL_REG,
650                         BQ2419X_EN_SFT_TIMER_MASK, 0);
651         if (ret < 0) {
652                 dev_err(bq2419x->dev,
653                                 "TIME_CTRL_REG update failed: %d\n", ret);
654                 return ret;
655         }
656
657         ret = regmap_update_bits(bq2419x->regmap, BQ2419X_TIME_CTRL_REG,
658                         BQ2419X_EN_SFT_TIMER_MASK, BQ2419X_EN_SFT_TIMER_MASK);
659         if (ret < 0)
660                 dev_err(bq2419x->dev,
661                                 "TIME_CTRL_REG update failed: %d\n", ret);
662         return ret;
663 }
664
665 static irqreturn_t bq2419x_irq(int irq, void *data)
666 {
667         struct bq2419x_chip *bq2419x = data;
668         int ret;
669         unsigned int val;
670         int check_chg_state = 0;
671
672         ret = regmap_read(bq2419x->regmap, BQ2419X_FAULT_REG, &val);
673         if (ret < 0) {
674                 dev_err(bq2419x->dev, "FAULT_REG read failed %d\n", ret);
675                 return ret;
676         }
677
678         dev_info(bq2419x->dev, "%s() Irq %d status 0x%02x\n",
679                 __func__, irq, val);
680
681         if (val & BQ2419x_FAULT_WATCHDOG_FAULT) {
682                 dev_err(bq2419x->dev,
683                         "Charging Fault: Watchdog Timer Expired\n");
684                 ret = bq2419x_watchdog_init(bq2419x, bq2419x->wdt_time_sec,
685                                                 "ISR");
686                 if (ret < 0) {
687                         dev_err(bq2419x->dev, "BQWDT init failed %d\n", ret);
688                         return ret;
689                 }
690
691                 ret = bq2419x_charger_init(bq2419x);
692                 if (ret < 0) {
693                         dev_err(bq2419x->dev, "Charger init failed: %d\n", ret);
694                         return ret;
695                 }
696
697                 ret = bq2419x_init(bq2419x);
698                 if (ret < 0) {
699                         dev_err(bq2419x->dev, "bq2419x init failed: %d\n", ret);
700                         return ret;
701                 }
702         }
703
704         if (val & BQ2419x_FAULT_BOOST_FAULT)
705                 dev_err(bq2419x->dev, "Charging Fault: VBUS Overloaded\n");
706
707         switch (val & BQ2419x_FAULT_CHRG_FAULT_MASK) {
708         case BQ2419x_FAULT_CHRG_INPUT:
709                 dev_err(bq2419x->dev, "Charging Fault: "
710                                 "Input Fault (VBUS OVP or VBAT<VBUS<3.8V)\n");
711                 chg_complete_check = 0;
712                 bq2419x->chg_restart_timeout = 0;
713                 break;
714         case BQ2419x_FAULT_CHRG_THERMAL:
715                 dev_err(bq2419x->dev, "Charging Fault: Thermal shutdown\n");
716                 check_chg_state = 1;
717                 break;
718         case BQ2419x_FAULT_CHRG_SAFTY:
719                 dev_err(bq2419x->dev,
720                         "Charging Fault: Safety timer expiration\n");
721                 bq2419x->chg_restart_timeout = bq2419x->chg_restart_time /
722                                                 bq2419x->wdt_refresh_timeout;
723                 ret = bq2419x_reset_safety_timer(bq2419x);
724                 if (ret < 0) {
725                         dev_err(bq2419x->dev, "Reset safety timer failed %d\n",
726                                                         ret);
727                         return ret;
728                 }
729
730                 check_chg_state = 1;
731                 break;
732         default:
733                 break;
734         }
735
736         if (val & BQ2419x_FAULT_NTC_FAULT) {
737                 dev_err(bq2419x->dev, "Charging Fault: NTC fault %d\n",
738                                 val & BQ2419x_FAULT_NTC_FAULT);
739                 check_chg_state = 1;
740         }
741
742         ret = bq2419x_fault_clear_sts(bq2419x);
743         if (ret < 0) {
744                 dev_err(bq2419x->dev, "fault clear status failed %d\n", ret);
745                 return ret;
746         }
747
748         ret = regmap_read(bq2419x->regmap, BQ2419X_SYS_STAT_REG, &val);
749         if (ret < 0) {
750                 dev_err(bq2419x->dev, "SYS_STAT_REG read failed %d\n", ret);
751                 return ret;
752         }
753
754         if ((val & BQ2419x_CHRG_STATE_MASK) ==
755                                 BQ2419x_CHRG_STATE_CHARGE_DONE) {
756                 bq2419x->chg_restart_timeout = bq2419x->chg_restart_time /
757                                                 bq2419x->wdt_refresh_timeout;
758                 dev_info(bq2419x->dev, "Charging completed\n");
759         }
760
761         /*
762         * Update Charging status based on STAT register
763         */
764         if (check_chg_state) {
765                 if ((val & BQ2419x_CHRG_STATE_MASK) ==
766                                 BQ2419x_CHRG_STATE_NOTCHARGING) {
767                         bq2419x->status = 0;
768                         if (bq2419x->update_status)
769                                 bq2419x->update_status(bq2419x->status, 0);
770                 }
771         }
772
773         return IRQ_HANDLED;
774 }
775
776 static int bq2419x_init_charger_regulator(struct bq2419x_chip *bq2419x,
777                 struct bq2419x_platform_data *pdata)
778 {
779         int ret = 0;
780
781         if (!pdata->bcharger_pdata) {
782                 dev_err(bq2419x->dev, "No charger platform data\n");
783                 return 0;
784         }
785
786         bq2419x->chg_reg_desc.name  = "bq2419x-charger";
787         bq2419x->chg_reg_desc.ops   = &bq2419x_tegra_regulator_ops;
788         bq2419x->chg_reg_desc.type  = REGULATOR_CURRENT;
789         bq2419x->chg_reg_desc.owner = THIS_MODULE;
790
791         bq2419x->chg_reg_init_data.supply_regulator     = NULL;
792         bq2419x->chg_reg_init_data.regulator_init       = NULL;
793         bq2419x->chg_reg_init_data.num_consumer_supplies =
794                                 pdata->bcharger_pdata->num_consumer_supplies;
795         bq2419x->chg_reg_init_data.consumer_supplies    =
796                                 pdata->bcharger_pdata->consumer_supplies;
797         bq2419x->chg_reg_init_data.driver_data          = bq2419x;
798         bq2419x->chg_reg_init_data.constraints.name     = "bq2419x-charger";
799         bq2419x->chg_reg_init_data.constraints.min_uA   = 0;
800         bq2419x->chg_reg_init_data.constraints.max_uA   =
801                         pdata->bcharger_pdata->max_charge_current_mA * 1000;
802
803         bq2419x->chg_reg_init_data.constraints.valid_modes_mask =
804                                                 REGULATOR_MODE_NORMAL |
805                                                 REGULATOR_MODE_STANDBY;
806
807         bq2419x->chg_reg_init_data.constraints.valid_ops_mask =
808                                                 REGULATOR_CHANGE_MODE |
809                                                 REGULATOR_CHANGE_STATUS |
810                                                 REGULATOR_CHANGE_CURRENT;
811
812         bq2419x->chg_rdev = regulator_register(&bq2419x->chg_reg_desc,
813                                 bq2419x->dev, &bq2419x->chg_reg_init_data,
814                                 bq2419x, NULL);
815         if (IS_ERR(bq2419x->chg_rdev)) {
816                 ret = PTR_ERR(bq2419x->chg_rdev);
817                 dev_err(bq2419x->dev,
818                         "vbus-charger regulator register failed %d\n", ret);
819         }
820         return ret;
821 }
822
823 static int bq2419x_init_vbus_regulator(struct bq2419x_chip *bq2419x,
824                 struct bq2419x_platform_data *pdata)
825 {
826         int ret = 0;
827
828         if (!pdata->vbus_pdata) {
829                 dev_err(bq2419x->dev, "No vbus platform data\n");
830                 return 0;
831         }
832
833         bq2419x->gpio_otg_iusb = pdata->vbus_pdata->gpio_otg_iusb;
834         bq2419x->vbus_reg_desc.name = "bq2419x-vbus";
835         bq2419x->vbus_reg_desc.ops = &bq2419x_vbus_ops;
836         bq2419x->vbus_reg_desc.type = REGULATOR_VOLTAGE;
837         bq2419x->vbus_reg_desc.owner = THIS_MODULE;
838
839         bq2419x->vbus_reg_init_data.supply_regulator    = NULL;
840         bq2419x->vbus_reg_init_data.regulator_init      = NULL;
841         bq2419x->vbus_reg_init_data.num_consumer_supplies       =
842                                 pdata->vbus_pdata->num_consumer_supplies;
843         bq2419x->vbus_reg_init_data.consumer_supplies   =
844                                 pdata->vbus_pdata->consumer_supplies;
845         bq2419x->vbus_reg_init_data.driver_data         = bq2419x;
846
847         bq2419x->vbus_reg_init_data.constraints.name    = "bq2419x-vbus";
848         bq2419x->vbus_reg_init_data.constraints.min_uV  = 0;
849         bq2419x->vbus_reg_init_data.constraints.max_uV  = 5000000,
850         bq2419x->vbus_reg_init_data.constraints.valid_modes_mask =
851                                         REGULATOR_MODE_NORMAL |
852                                         REGULATOR_MODE_STANDBY;
853         bq2419x->vbus_reg_init_data.constraints.valid_ops_mask =
854                                         REGULATOR_CHANGE_MODE |
855                                         REGULATOR_CHANGE_STATUS |
856                                         REGULATOR_CHANGE_VOLTAGE;
857
858         if (gpio_is_valid(bq2419x->gpio_otg_iusb)) {
859                 ret = gpio_request_one(bq2419x->gpio_otg_iusb,
860                                 GPIOF_OUT_INIT_HIGH, dev_name(bq2419x->dev));
861                 if (ret < 0) {
862                         dev_err(bq2419x->dev, "gpio request failed  %d\n", ret);
863                         return ret;
864                 }
865         }
866
867         /* Register the regulators */
868         bq2419x->vbus_rdev = regulator_register(&bq2419x->vbus_reg_desc,
869                         bq2419x->dev, &bq2419x->vbus_reg_init_data,
870                         bq2419x, NULL);
871         if (IS_ERR(bq2419x->vbus_rdev)) {
872                 ret = PTR_ERR(bq2419x->vbus_rdev);
873                 dev_err(bq2419x->dev,
874                         "VBUS regulator register failed %d\n", ret);
875                 goto scrub;
876         }
877
878         /* Disable the VBUS regulator and enable charging */
879         ret = bq2419x_charger_enable(bq2419x);
880         if (ret < 0) {
881                 dev_err(bq2419x->dev, "Charging enable failed %d", ret);
882                 goto scrub_reg;
883         }
884         return ret;
885
886 scrub_reg:
887         regulator_unregister(bq2419x->vbus_rdev);
888         bq2419x->vbus_rdev = NULL;
889 scrub:
890         if (gpio_is_valid(bq2419x->gpio_otg_iusb))
891                 gpio_free(bq2419x->gpio_otg_iusb);
892         return ret;
893 }
894
895 static int bq2419x_show_chip_version(struct bq2419x_chip *bq2419x)
896 {
897         int ret;
898         unsigned int val;
899
900         ret = regmap_read(bq2419x->regmap, BQ2419X_REVISION_REG, &val);
901         if (ret < 0) {
902                 dev_err(bq2419x->dev, "REVISION_REG read failed: %d\n", ret);
903                 return ret;
904         }
905
906         val &= BQ2419X_IC_VER_MASK;
907
908         if (val == BQ24190_IC_VER) {
909                 bq2419x->chip_version = BQ24190_IC;
910                 dev_info(bq2419x->dev, "chip type BQ24190 detected\n");
911         } else if (val == BQ24192_IC_VER) {
912                 bq2419x->chip_version = BQ24192_IC;
913                 dev_info(bq2419x->dev, "chip type BQ24192 detected\n");
914         } else if (val == BQ24193_IC_VER) {
915                 bq2419x->chip_version = BQ24193_IC;
916                 dev_info(bq2419x->dev, "chip type BQ24193 detected\n");
917         } else if (val == BQ24192i_IC_VER) {
918                 bq2419x->chip_version = BQ24192i_IC;
919                 dev_info(bq2419x->dev, "chip type BQ2419Xi detected\n");
920         }
921         return 0;
922 }
923
924 static int bq2419x_wakealarm(struct bq2419x_chip *bq2419x, int time_sec)
925 {
926         int ret;
927         unsigned long now;
928         struct rtc_wkalrm alm;
929         int alarm_time = time_sec;
930
931         if (!alarm_time)
932                 return 0;
933
934         alm.enabled = true;
935         ret = rtc_read_time(bq2419x->rtc, &alm.time);
936         if (ret < 0) {
937                 dev_err(bq2419x->dev, "RTC read time failed %d\n", ret);
938                 return ret;
939         }
940         rtc_tm_to_time(&alm.time, &now);
941
942         rtc_time_to_tm(now + alarm_time, &alm.time);
943         ret = rtc_set_alarm(bq2419x->rtc, &alm);
944         if (ret < 0) {
945                 dev_err(bq2419x->dev, "RTC set alarm failed %d\n", ret);
946                 alm.enabled = false;
947                 return ret;
948         }
949         alm.enabled = false;
950         return 0;
951 }
952
953 static int __devinit bq2419x_probe(struct i2c_client *client,
954                                 const struct i2c_device_id *id)
955 {
956         struct bq2419x_chip *bq2419x;
957         struct bq2419x_platform_data *pdata;
958         int ret = 0;
959
960         pdata = client->dev.platform_data;
961         if (!pdata) {
962                 dev_err(&client->dev, "No Platform data");
963                 return -EINVAL;
964         }
965
966         bq2419x = devm_kzalloc(&client->dev, sizeof(*bq2419x), GFP_KERNEL);
967         if (!bq2419x) {
968                 dev_err(&client->dev, "Memory allocation failed\n");
969                 return -ENOMEM;
970         }
971
972         bq2419x->regmap = devm_regmap_init_i2c(client, &bq2419x_regmap_config);
973         if (IS_ERR(bq2419x->regmap)) {
974                 ret = PTR_ERR(bq2419x->regmap);
975                 dev_err(&client->dev, "regmap init failed with err %d\n", ret);
976                 return ret;
977         }
978
979         bq2419x->dev = &client->dev;
980
981         if (pdata->bcharger_pdata) {
982                 bq2419x->update_status  = pdata->bcharger_pdata->update_status;
983                 bq2419x->soc_check      = pdata->bcharger_pdata->soc_check;
984                 bq2419x->vcell_check    = pdata->bcharger_pdata->vcell_check;
985                 bq2419x->current_check  = pdata->bcharger_pdata->current_check;
986                 bq2419x->rtc_alarm_time = pdata->bcharger_pdata->rtc_alarm_time;
987                 bq2419x->wdt_time_sec   = pdata->bcharger_pdata->wdt_timeout;
988                 bq2419x->chg_restart_time =
989                                 pdata->bcharger_pdata->chg_restart_time;
990                 bq2419x->chg_complete_soc =
991                                 pdata->bcharger_pdata->chg_complete_soc;
992                 bq2419x->chg_enable     = true;
993         }
994
995         bq2419x->wdt_refresh_timeout = 25;
996         i2c_set_clientdata(client, bq2419x);
997         bq2419x->irq = client->irq;
998
999         if (bq2419x->rtc_alarm_time)
1000                 bq2419x->rtc = alarmtimer_get_rtcdev();
1001
1002         mutex_init(&bq2419x->mutex);
1003         bq2419x->suspended = 0;
1004         bq2419x->chg_restart_timeout = 0;
1005
1006         ret = bq2419x_show_chip_version(bq2419x);
1007         if (ret < 0) {
1008                 dev_err(&client->dev, "version read failed %d\n", ret);
1009                 return ret;
1010         }
1011
1012         ret = bq2419x_charger_init(bq2419x);
1013         if (ret < 0) {
1014                 dev_err(bq2419x->dev, "Charger init failed: %d\n", ret);
1015                 return ret;
1016         }
1017
1018         ret = bq2419x_init_charger_regulator(bq2419x, pdata);
1019         if (ret < 0) {
1020                 dev_err(&client->dev,
1021                         "Charger regualtor init failed %d\n", ret);
1022                 return ret;
1023         }
1024
1025         ret = bq2419x_init_vbus_regulator(bq2419x, pdata);
1026         if (ret < 0) {
1027                 dev_err(&client->dev,
1028                         "VBUS regualtor init failed %d\n", ret);
1029                 goto scrub_chg_reg;
1030         }
1031
1032         init_kthread_worker(&bq2419x->bq_kworker);
1033         bq2419x->bq_kworker_task = kthread_run(kthread_worker_fn,
1034                                 &bq2419x->bq_kworker,
1035                                 dev_name(bq2419x->dev));
1036         if (IS_ERR(bq2419x->bq_kworker_task)) {
1037                 ret = PTR_ERR(bq2419x->bq_kworker_task);
1038                 dev_err(&client->dev, "Kworker task creation failed %d\n", ret);
1039                 goto scrub_vbus_reg;
1040         }
1041
1042         init_kthread_work(&bq2419x->bq_wdt_work, bq2419x_work_thread);
1043         sched_setscheduler(bq2419x->bq_kworker_task,
1044                         SCHED_FIFO, &bq2419x_param);
1045         queue_kthread_work(&bq2419x->bq_kworker, &bq2419x->bq_wdt_work);
1046
1047         ret = bq2419x_watchdog_init(bq2419x, bq2419x->wdt_time_sec, "PROBE");
1048         if (ret < 0) {
1049                 dev_err(bq2419x->dev, "BQWDT init failed %d\n", ret);
1050                 goto scrub_kthread;
1051         }
1052
1053         ret = bq2419x_fault_clear_sts(bq2419x);
1054         if (ret < 0) {
1055                 dev_err(bq2419x->dev, "fault clear status failed %d\n", ret);
1056                 goto scrub_kthread;
1057         }
1058
1059         ret = request_threaded_irq(bq2419x->irq, NULL,
1060                 bq2419x_irq, IRQF_TRIGGER_FALLING,
1061                         dev_name(bq2419x->dev), bq2419x);
1062         if (ret < 0) {
1063                 dev_err(bq2419x->dev, "request IRQ %d fail, err = %d\n",
1064                                 bq2419x->irq, ret);
1065                 goto scrub_kthread;
1066         }
1067
1068         /* enable charging */
1069         ret = bq2419x_charger_enable(bq2419x);
1070         if (ret < 0)
1071                 goto scrub_irq;
1072
1073         return 0;
1074 scrub_irq:
1075         free_irq(bq2419x->irq, bq2419x);
1076 scrub_kthread:
1077         bq2419x->stop_thread = true;
1078         flush_kthread_worker(&bq2419x->bq_kworker);
1079         kthread_stop(bq2419x->bq_kworker_task);
1080 scrub_vbus_reg:
1081         regulator_unregister(bq2419x->vbus_rdev);
1082 scrub_chg_reg:
1083         regulator_unregister(bq2419x->chg_rdev);
1084         mutex_destroy(&bq2419x->mutex);
1085         return ret;
1086 }
1087
1088 static int __devexit bq2419x_remove(struct i2c_client *client)
1089 {
1090         struct bq2419x_chip *bq2419x = i2c_get_clientdata(client);
1091
1092         free_irq(bq2419x->irq, bq2419x);
1093         bq2419x->stop_thread = true;
1094         flush_kthread_worker(&bq2419x->bq_kworker);
1095         kthread_stop(bq2419x->bq_kworker_task);
1096         regulator_unregister(bq2419x->vbus_rdev);
1097         regulator_unregister(bq2419x->chg_rdev);
1098         mutex_destroy(&bq2419x->mutex);
1099         return 0;
1100 }
1101
1102 static void bq2419x_shutdown(struct i2c_client *client)
1103 {
1104         int ret = 0;
1105         struct bq2419x_chip *bq2419x = i2c_get_clientdata(client);
1106         int alarm_time = bq2419x->rtc_alarm_time;
1107
1108         if (bq2419x->irq)
1109                 disable_irq(bq2419x->irq);
1110
1111         if (alarm_time && !bq2419x->rtc)
1112                 bq2419x->rtc = alarmtimer_get_rtcdev();
1113
1114         if (alarm_time && (bq2419x->in_current_limit > 500)) {
1115                 dev_info(bq2419x->dev, "HighCurrent %dmA charger is connectd\n",
1116                         bq2419x->in_current_limit);
1117                 /* Clear EN_HIZ */
1118                 ret = regmap_update_bits(bq2419x->regmap,
1119                         BQ2419X_INPUT_SRC_REG, BQ2419X_EN_HIZ, 0);
1120                 if (ret < 0)
1121                         dev_err(bq2419x->dev,
1122                                 "INPUT_SRC_REG update failed:%d\n", ret);
1123                 ret = bq2419x_reset_wdt(bq2419x, "shutdown");
1124                 if (ret < 0)
1125                         dev_err(bq2419x->dev,
1126                                 "bq2419x_reset_wdt failed: %d\n", ret);
1127                 alarm_time = 20;
1128         }
1129
1130         mutex_lock(&bq2419x->mutex);
1131         bq2419x->suspended = 1;
1132         mutex_unlock(&bq2419x->mutex);
1133
1134         ret = bq2419x_charger_enable(bq2419x);
1135         if (ret < 0)
1136                 dev_err(bq2419x->dev, "Charger enable failed %d", ret);
1137
1138         if (alarm_time && (bq2419x->in_current_limit <= 500)) {
1139                 /* Configure charging current to 500mA */
1140                 ret = regmap_write(bq2419x->regmap,
1141                                 BQ2419X_INPUT_SRC_REG, 0x32);
1142                 if (ret < 0)
1143                         dev_err(bq2419x->dev,
1144                                 "INPUT_SRC_REG write failed %d\n", ret);
1145         }
1146
1147         ret = bq2419x_wakealarm(bq2419x, alarm_time);
1148         if (ret < 0)
1149                 dev_err(bq2419x->dev, "RTC wake alarm config failed %d\n", ret);
1150 }
1151
1152 #ifdef CONFIG_PM_SLEEP
1153 static int bq2419x_suspend(struct device *dev)
1154 {
1155         int ret = 0;
1156         struct bq2419x_chip *bq2419x = dev_get_drvdata(dev);
1157
1158         mutex_lock(&bq2419x->mutex);
1159         bq2419x->suspended = 1;
1160         mutex_unlock(&bq2419x->mutex);
1161         disable_irq(bq2419x->irq);
1162         ret = bq2419x_charger_enable(bq2419x);
1163         if (ret < 0) {
1164                 dev_err(bq2419x->dev, "Charger enable failed %d", ret);
1165                 return ret;
1166         }
1167
1168         /* Configure charging current to 500mA */
1169         ret = regmap_write(bq2419x->regmap, BQ2419X_INPUT_SRC_REG, 0x32);
1170         if (ret < 0) {
1171                 dev_err(bq2419x->dev, "INPUT_SRC_REG write failed %d\n", ret);
1172                 return ret;
1173         }
1174
1175         return 0;
1176 }
1177
1178 static int bq2419x_resume(struct device *dev)
1179 {
1180         int ret = 0;
1181         struct bq2419x_chip *bq2419x = dev_get_drvdata(dev);
1182         unsigned int val;
1183
1184         ret = regmap_read(bq2419x->regmap, BQ2419X_FAULT_REG, &val);
1185         if (ret < 0) {
1186                 dev_err(bq2419x->dev, "FAULT_REG read failed %d\n", ret);
1187                 return ret;
1188         }
1189
1190         if (val & BQ2419x_FAULT_WATCHDOG_FAULT) {
1191                 dev_err(bq2419x->dev,
1192                         "Charging Fault: Watchdog Timer Expired\n");
1193
1194                 ret = bq2419x_watchdog_init(bq2419x, bq2419x->wdt_time_sec,
1195                                                 "RESUME");
1196                 if (ret < 0) {
1197                         dev_err(bq2419x->dev, "BQWDT init failed %d\n", ret);
1198                         return ret;
1199                 }
1200         }
1201
1202         ret = bq2419x_fault_clear_sts(bq2419x);
1203         if (ret < 0) {
1204                 dev_err(bq2419x->dev, "fault clear status failed %d\n", ret);
1205                 return ret;
1206         }
1207
1208         ret = bq2419x_charger_init(bq2419x);
1209         if (ret < 0) {
1210                 dev_err(bq2419x->dev, "Charger init failed: %d\n", ret);
1211                 return ret;
1212         }
1213
1214         ret = bq2419x_init(bq2419x);
1215         if (ret < 0) {
1216                 dev_err(bq2419x->dev, "bq2419x init failed: %d\n", ret);
1217                 return ret;
1218         }
1219
1220         mutex_lock(&bq2419x->mutex);
1221         bq2419x->suspended = 0;
1222         mutex_unlock(&bq2419x->mutex);
1223         if (gpio_is_valid(bq2419x->gpio_otg_iusb))
1224                 gpio_set_value(bq2419x->gpio_otg_iusb, 1);
1225
1226         enable_irq(bq2419x->irq);
1227         return 0;
1228 };
1229 #endif
1230
1231 static const struct dev_pm_ops bq2419x_pm_ops = {
1232         SET_SYSTEM_SLEEP_PM_OPS(bq2419x_suspend,
1233                                 bq2419x_resume)
1234 };
1235
1236 static const struct i2c_device_id bq2419x_id[] = {
1237         {.name = "bq2419x",},
1238         {},
1239 };
1240 MODULE_DEVICE_TABLE(i2c, bq2419x_id);
1241
1242 static struct i2c_driver bq2419x_i2c_driver = {
1243         .driver = {
1244                 .name   = "bq2419x",
1245                 .owner  = THIS_MODULE,
1246                 .pm = &bq2419x_pm_ops,
1247         },
1248         .probe          = bq2419x_probe,
1249         .remove         = __devexit_p(bq2419x_remove),
1250         .shutdown       = bq2419x_shutdown,
1251         .id_table       = bq2419x_id,
1252 };
1253
1254 static int __init bq2419x_module_init(void)
1255 {
1256         return i2c_add_driver(&bq2419x_i2c_driver);
1257 }
1258 subsys_initcall(bq2419x_module_init);
1259
1260 static void __exit bq2419x_cleanup(void)
1261 {
1262         i2c_del_driver(&bq2419x_i2c_driver);
1263 }
1264 module_exit(bq2419x_cleanup);
1265
1266 MODULE_DESCRIPTION("BQ24190/BQ24192/BQ24192i/BQ24193 battery charger driver");
1267 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1268 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com");
1269 MODULE_LICENSE("GPL v2");