drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / battery / smb358_charger.c
1 /*
2  *  smb358_charger.c
3  *  Samsung SMB358 Charger Driver
4  *
5  *  Copyright (C) 2012 Samsung Electronics
6  *
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #define DEBUG
14
15 #include <linux/battery/sec_charger.h>
16
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/of_gpio.h>
20
21 #ifndef SLOW_CHARGING_CURRENT_STANDARD
22
23 #if defined(CONFIG_MACH_HEAT_AIO)
24 #define SLOW_CHARGING_CURRENT_STANDARD 400
25 #else
26 #define SLOW_CHARGING_CURRENT_STANDARD 1000
27 #endif
28
29 #endif
30
31 static int smb358_i2c_write(struct i2c_client *client,
32                                 int reg, u8 *buf)
33 {
34         int ret;
35         ret = i2c_smbus_write_i2c_block_data(client, reg, 1, buf);
36         if (ret < 0)
37                 dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
38         return ret;
39 }
40
41 static int smb358_i2c_read(struct i2c_client *client,
42                                 int reg, u8 *buf)
43 {
44         int ret;
45         ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf);
46         if (ret < 0)
47                 dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret);
48         return ret;
49 }
50
51 /*static void smb358_i2c_write_array(struct i2c_client *client,
52                                 u8 *buf, int size)
53 {
54         int i;
55         for (i = 0; i < size; i += 3)
56                 smb358_i2c_write(client, (u8) (*(buf + i)), (buf + i) + 1);
57 }*/
58
59 static int smb358_update_reg(struct i2c_client *client, int reg, u8 data)
60 {
61         int ret;
62         u8 r_data = 0;
63         u8 w_data = 0;
64         u8 o_data = data;
65
66         ret = smb358_i2c_read(client, reg, &r_data);
67         if (ret < 0) {
68                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
69                 goto error;
70         }
71
72         w_data  = r_data | data;
73         ret = smb358_i2c_write(client, reg, &w_data);
74         if (ret < 0) {
75                 dev_err(&client->dev, "%s: error - write(%d)\n", __func__, ret);
76                 goto error;
77         }
78
79         ret = smb358_i2c_read(client, reg, &data);
80         if (ret < 0) {
81                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
82                 goto error;
83         }
84
85         dev_dbg(&client->dev,
86                 "%s: reg(0x%02x) 0x%02x : 0x%02x -> 0x%02x -> 0x%02x\n",
87                 __func__, reg, o_data, r_data, w_data, data);
88
89 error:
90         return ret;
91 }
92
93 static int smb358_clear_reg(struct i2c_client *client, int reg, u8 data)
94 {
95         int ret;
96         u8 r_data = 0;
97         u8 w_data = 0;
98         u8 o_data = data;
99
100         ret = smb358_i2c_read(client, reg, &r_data);
101         if (ret < 0) {
102                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
103                 goto error;
104         }
105
106         w_data  = r_data & (~data);
107         ret = smb358_i2c_write(client, reg, &w_data);
108         if (ret < 0) {
109                 dev_err(&client->dev, "%s: error - write(%d)\n", __func__, ret);
110                 goto error;
111         }
112
113         ret = smb358_i2c_read(client, reg, &data);
114         if (ret < 0) {
115                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
116                 goto error;
117         }
118
119         dev_dbg(&client->dev,
120                 "%s: reg(0x%02x)- 0x%02x : 0x%02x -> 0x%02x -> 0x%02x\n",
121                 __func__, reg, o_data, r_data, w_data, data);
122
123 error:
124         return ret;
125 }
126
127 static int smb358_volatile_writes(struct i2c_client *client, u8 value)
128 {
129         int ret = 0;
130
131         if (value == SMB358_ENABLE_WRITE) {
132                 ret = smb358_update_reg(client, SMB358_COMMAND_A, 0x80);
133                 if (ret < 0) {
134                         dev_err(&client->dev, "%s: error(%d)\n", __func__, ret);
135                         goto error;
136                 }
137                 dev_dbg(&client->dev, "%s: ENABLED\n", __func__);
138         } else {
139                 ret = smb358_clear_reg(client, SMB358_COMMAND_A, 0x80);
140                 if (ret < 0) {
141                         dev_err(&client->dev, "%s: error(%d)\n", __func__, ret);
142                         goto error;
143                 }
144                 dev_dbg(&client->dev, "%s: DISABLED\n", __func__);
145         }
146
147 error:
148         return ret;
149 }
150
151 static void smb358_set_command(struct i2c_client *client,
152                                 int reg, u8 datum)
153 {
154         int val;
155         u8 after_data;
156
157         if (smb358_i2c_write(client, reg, &datum) < 0)
158                 dev_err(&client->dev,
159                         "%s : error!\n", __func__);
160
161         val = smb358_i2c_read(client, reg, &after_data);
162         if (val >= 0)
163                 dev_info(&client->dev,
164                         "%s : reg(0x%02x) 0x%02x => 0x%02x\n",
165                         __func__, reg, datum, after_data);
166         else
167                 dev_err(&client->dev, "%s : error!\n", __func__);
168 }
169
170 #if 0
171 static void smb358_test_read(struct i2c_client *client)
172 {
173         u8 data = 0;
174         u32 addr = 0;
175         for (addr = 0; addr <= 0x0f; addr++) {
176                 smb358_i2c_read(client, addr, &data);
177                 dev_dbg(&client->dev,
178                         "%s : smb358 addr : 0x%02x data : 0x%02x\n",
179                                                 __func__, addr, data);
180         }
181         for (addr = 0x30; addr <= 0x3f; addr++) {
182                 smb358_i2c_read(client, addr, &data);
183                 dev_dbg(&client->dev,
184                         "%s : smb358 addr : 0x%02x data : 0x%02x\n",
185                                                 __func__, addr, data);
186         }
187 }
188 #endif
189
190 static void smb358_read_regs(struct i2c_client *client, char *str)
191 {
192         u8 data = 0;
193         u32 addr = 0;
194
195         for (addr = 0; addr <= 0x0f; addr++) {
196                 smb358_i2c_read(client, addr, &data);
197                 sprintf(str+strlen(str), "0x%x, ", data);
198         }
199
200         /* "#" considered as new line in application */
201         sprintf(str+strlen(str), "#");
202
203         for (addr = 0x30; addr <= 0x3f; addr++) {
204                 smb358_i2c_read(client, addr, &data);
205                 sprintf(str+strlen(str), "0x%x, ", data);
206         }
207 }
208
209 static int smb358_get_aicl_current(u8 aicl_current)
210 {
211         int data;
212
213         if (aicl_current <= 0x10)
214                 data = 300;
215         else if (aicl_current <= 0x11)
216                 data = 500;
217         else if (aicl_current <= 0x12)
218                 data = 700;
219         else if (aicl_current <= 0x13)
220                 data = 1000;
221         else if (aicl_current <= 0x14)
222                 data = 1200;
223         else if (aicl_current <= 0x15)
224                 data = 1300;
225         else if (aicl_current <= 0x16)
226                 data = 1800;
227         else
228                 data = 2000;
229
230         return data;
231 }
232
233 static int smb358_get_charging_status(struct i2c_client *client)
234 {
235         int status = POWER_SUPPLY_STATUS_UNKNOWN;
236         u8 data_a = 0;
237         u8 data_b = 0;
238         u8 data_c = 0;
239         u8 data_d = 0;
240         u8 data_e = 0;
241         u8 therm_control_a = 0;
242         u8 other_control_a = 0;
243
244
245         /*smb358_test_read(client);*/
246
247         smb358_i2c_read(client, SMB358_STATUS_A, &data_a);
248         dev_dbg(&client->dev,
249                 "%s : charger status A(0x%02x)\n", __func__, data_a);
250         smb358_i2c_read(client, SMB358_STATUS_B, &data_b);
251         dev_dbg(&client->dev,
252                 "%s : charger status B(0x%02x)\n", __func__, data_b);
253         smb358_i2c_read(client, SMB358_STATUS_C, &data_c);
254         dev_dbg(&client->dev,
255                 "%s : charger status C(0x%02x)\n", __func__, data_c);
256         smb358_i2c_read(client, SMB358_STATUS_D, &data_d);
257         dev_dbg(&client->dev,
258                 "%s : charger status D(0x%02x)\n", __func__, data_d);
259         smb358_i2c_read(client, SMB358_STATUS_E, &data_e);
260         dev_dbg(&client->dev,
261                 "%s : charger status E(0x%02x)\n", __func__, data_e);
262         smb358_i2c_read(client, SMB358_THERM_CONTROL_A, &therm_control_a);
263         dev_dbg(&client->dev,
264                 "%s : THERM_CONTROL_A(0x%02x)\n", __func__, therm_control_a);
265         smb358_i2c_read(client, SMB358_OTHER_CONTROL_A, &other_control_a);
266         dev_dbg(&client->dev,
267                 "%s : OTHER_CONTROL_A(0x%02x)\n", __func__, other_control_a);
268         /* At least one charge cycle terminated,
269          * Charge current < Termination Current
270          */
271         if (data_c & 0x20) {
272                 /* top-off by full charging */
273                 status = POWER_SUPPLY_STATUS_FULL;
274                 goto charging_status_end;
275         }
276
277         /* Is enabled ? */
278         if (data_c & 0x01) {
279                 /* check for 0x06 : no charging (0b00) */
280                 /* not charging */
281                 if (!(data_c & 0x06)) {
282                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
283                         goto charging_status_end;
284                 } else {
285                         status = POWER_SUPPLY_STATUS_CHARGING;
286                         goto charging_status_end;
287                 }
288         } else
289                 status = POWER_SUPPLY_STATUS_DISCHARGING;
290
291 charging_status_end:
292         return (int)status;
293 }
294
295 static int smb358_get_charging_health(struct i2c_client *client)
296 {
297         int health = POWER_SUPPLY_HEALTH_GOOD;
298         u8 data_a = 0;
299         u8 data_b = 0;
300         u8 data_c = 0;
301         u8 data_d = 0;
302         u8 data_e = 0;
303
304         smb358_i2c_read(client, SMB358_STATUS_A, &data_a);
305         dev_dbg(&client->dev,
306                 "%s : charger status A(0x%02x)\n", __func__, data_a);
307         smb358_i2c_read(client, SMB358_STATUS_B, &data_b);
308         dev_dbg(&client->dev,
309                 "%s : charger status B(0x%02x)\n", __func__, data_b);
310         smb358_i2c_read(client, SMB358_STATUS_C, &data_c);
311         dev_dbg(&client->dev,
312                 "%s : charger status C(0x%02x)\n", __func__, data_c);
313         smb358_i2c_read(client, SMB358_STATUS_D, &data_d);
314         dev_dbg(&client->dev,
315                 "%s : charger status D(0x%02x)\n", __func__, data_d);
316         smb358_i2c_read(client, SMB358_STATUS_E, &data_e);
317         dev_dbg(&client->dev,
318                 "%s : charger status E(0x%02x)\n", __func__, data_e);
319         smb358_i2c_read(client, SMB358_INTERRUPT_STATUS_E, &data_e);
320         dev_dbg(&client->dev,
321                 "%s : charger interrupt status E(0x%02x)\n", __func__, data_e);
322
323         if (data_e & 0x01)
324                 health = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
325         else if (data_e & 0x04)
326                 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
327
328         return (int)health;
329 }
330
331 /*static void smb358_allow_volatile_writes(struct i2c_client *client)
332 {
333         int val, reg;
334         u8 data;
335         reg = SMB358_COMMAND_A;
336         val = smb358_i2c_read(client, reg, &data);
337         if ((val >= 0) && !(data & 0x80)) {
338                 dev_dbg(&client->dev,
339                         "%s : reg(0x%02x): 0x%02x", __func__, reg, data);
340                 data |= (0x1 << 7);
341                 if (smb358_i2c_write(client, reg, &data) < 0)
342                         dev_err(&client->dev, "%s : error!\n", __func__);
343                 val = smb358_i2c_read(client, reg, &data);
344                 if (val >= 0) {
345                         data = (u8) data;
346                         dev_dbg(&client->dev, " => 0x%02x\n", data);
347                 }
348         }
349 }*/
350
351 static u8 smb358_get_float_voltage_data(int float_voltage)
352 {
353         u8 data;
354
355         if (float_voltage < 3500)
356                 data = 0;
357         else if(float_voltage <= 4340)
358                 data = (float_voltage - 3500) / 20;
359         else if(float_voltage == 4350)
360                 data = 0x2B; /* (4340 -3500)/20 + 1 */
361         else if(float_voltage <= 4500)
362                 data = (float_voltage - 3500) / 20 + 1;
363         else
364                 data = 51;
365
366         return data;
367 }
368
369 static u8 smb358_get_input_current_limit_data(
370                         struct sec_charger_info *charger, int input_current)
371 {
372         u8 data;
373
374         if (input_current <= 300)
375                 data = 0x00;
376         else if (input_current <= 500)
377                 data = 0x01;
378         else if (input_current <= 700)
379                 data = 0x02;
380         else if (input_current <= 1000)
381                 data = 0x03;
382         else if (input_current <= 1200)
383                 data = 0x04;
384         else if (input_current <= 1500)
385                 data = 0x05;
386         else if (input_current <= 1800)
387                 data = 0x06;
388         else
389                 data = 0x07;    /* set input current to 2000mA */
390
391         return (data << 4);
392 }
393
394 static u8 smb358_get_term_current_limit_data(
395                         int termination_current)
396 {
397         u8 data;
398
399         if (termination_current <= 30)
400                 data = 0x00;
401         else if (termination_current <= 40)
402                 data = 0x01;
403         else if (termination_current <= 60)
404                 data = 0x02;
405         else if (termination_current <= 80)
406                 data = 0x03;
407         else if (termination_current <= 100)
408                 data = 0x04;
409         else if (termination_current <= 125)
410                 data = 0x05;
411         else if (termination_current <= 150)
412                 data = 0x06;
413         else
414                 data = 0x07;    /* set termination current limit to 200mA */
415
416         return data;
417 }
418
419 static u8 smb358_get_fast_charging_current_data(
420                         int fast_charging_current)
421 {
422         u8 data;
423
424         if (fast_charging_current <= 200)
425                 data = 0x00;
426         else if (fast_charging_current <= 450)
427                 data = 0x01;
428         else if (fast_charging_current <= 600)
429                 data = 0x02;
430         else if (fast_charging_current <= 900)
431                 data = 0x03;
432         else if (fast_charging_current <= 1300)
433                 data = 0x04;
434         else if (fast_charging_current <= 1500)
435                 data = 0x05;
436         else if (fast_charging_current <= 1800)
437                 data = 0x06;
438         else
439                 data = 0x07;    /* set fast charging current to 2000mA */
440
441         return data << 5;
442 }
443
444 static void smb358_enter_suspend(struct i2c_client *client)
445 {
446         u8 data = 0;
447
448         pr_info("%s: ENTER SUSPEND\n", __func__);
449         smb358_update_reg(client, SMB358_COMMAND_A, 0x80);
450         smb358_set_command(client, SMB358_PIN_ENABLE_CONTROL, 0x18);
451         data = (data | 0x4);
452         smb358_set_command(client, SMB358_COMMAND_A, data);
453 }
454
455 #if 0
456 #if (defined(CONFIG_MACH_MILLET3G_EUR) || defined(CONFIG_MACH_MATISSE3G_OPEN) || defined(CONFIG_MACH_BERLUTI3G_EUR))
457 static void smb358_aicl_calibrate(struct i2c_client *client)
458 {
459
460         struct sec_charger_info *charger = i2c_get_clientdata(client);
461         int ret = 0;
462         u8 data = 0;
463         u8 count = 0;
464         u8 current_initial = 0;
465         u8 current_final = 0;
466
467         ret = smb358_i2c_read(client, SMB358_STATUS_E, &data);
468         if (ret < 0) {
469                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
470         }
471
472         /* check If AICL complete */
473         if(data & 0x10){
474                 current_initial = (data & 0x0F);
475
476                 if((current_initial >= 0x04) && (current_initial <= 0x05)){
477                         /* set 1000mA */
478                         ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
479                         data = data & 0x0F;
480                         data = data | 0x30;
481                         smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
482                 }else{
483                         /* set 1800mA */
484                         ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
485                         data = data & 0x0F;
486                         data = data | 0x60;
487                         smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
488                 }
489
490                 smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
491                 current_final = (data & 0x0F);
492
493                 dev_err(&charger->client->dev,
494                         "%s: AICL calibration success! input current (%dmA) -> (%dmA) ! \n",
495                         __func__,current_initial,current_final);
496                 return;
497
498         }else{
499         /* Incase of AICL not complete check three times */
500                 for(count=0 ;count < 3;count++)
501                 {
502                         msleep(300);
503                         ret = smb358_i2c_read(client, SMB358_STATUS_E, &data);
504                         if (ret < 0) {
505                                 dev_err(&client->dev, "%s: error - read(%d)\n", __func__, ret);
506                         }
507
508                         if(data & 0x10){
509                                 current_initial = (data & 0x0F);
510
511                                 if((current_initial >= 0x04) && (current_initial <= 0x05)){
512                                 /* set 1000mA */
513                                         ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
514                                         data = data & 0x0F;
515                                         data = data | 0x30;
516                                         smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
517                                 }else{
518                                 /* set 1800mA */
519                                         ret = smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
520                                         data = data & 0x0F;
521                                         data = data | 0x60;
522                                         smb358_set_command(client,SMB358_INPUT_CURRENTLIMIT, data);
523                                 }
524
525                                 smb358_i2c_read(client, SMB358_INPUT_CURRENTLIMIT, &data);
526                                 current_final = (data & 0x0F);
527
528                                 dev_err(&charger->client->dev,
529                                         "%s: AICL calibration success! input current (%dmA) -> (%dmA) ! \n",
530                                         __func__,current_initial,current_final);
531                                 return;
532                         }else{
533                                 dev_err(&charger->client->dev,
534                                         "%s: AICL not complete \n",__func__);
535                         }
536                 }
537         }
538
539         dev_err(&charger->client->dev,
540                 "%s: AICL calibration Failed! current (%dmA) ! \n",__func__,current_initial);
541         return;
542
543 }
544 #endif
545 #endif
546
547 static void smb358_check_slow_charging(struct work_struct *work)
548 {
549         struct sec_charger_info *charger =
550                 container_of(work, struct sec_charger_info, slow_work.work);
551
552         u8 i, aicl_data;
553         int aicl_current = 0;
554         union power_supply_propval val;
555
556         if (charger->pdata->chg_functions_setting &
557                         SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT) {
558                 pr_err("%s: aicl is disabled\n", __func__);
559                 return;
560         }
561         if (charger->pdata->charging_current
562                                 [charger->cable_type].input_current_limit <= SLOW_CHARGING_CURRENT_STANDARD) {
563                         charger->is_slow_charging = true;
564         } else {
565                 for(i = 0; i < 20; i++) {
566                         if (charger->cable_type ==
567                                         POWER_SUPPLY_TYPE_BATTERY) {
568                                 pr_info("%s: cable is removed\n", __func__);
569                                 return;
570                         }
571                         msleep(200);
572                         smb358_i2c_read(charger->client, SMB358_STATUS_E, &aicl_data);
573                         if (aicl_data & 0x10) { /* check AICL complete */
574                                 break;
575                         }
576                         if (i == 10) {
577                                 pr_info("%s: aicl not complete, retry\n", __func__);
578                                 /* disable AICL */
579                                 smb358_set_command(charger->client,
580                                                 SMB358_VARIOUS_FUNCTIONS, 0x81);
581                                 /* enable AICL */
582                                 smb358_set_command(charger->client,
583                                                 SMB358_VARIOUS_FUNCTIONS, 0x95);
584                         }
585                 }
586
587                 aicl_data &= 0xF; /* get only AICL result field */
588                 switch (aicl_data) {
589                 case 0: /* AICL result 300mA */
590                         aicl_current = 300;
591                         break;
592                 case 1: /* AICL result 500mA */
593                         aicl_current = 500;
594                         break;
595                 case 2: /* AICL result 700mA */
596                         aicl_current = 700;
597                         break;
598                 case 3: /* AICL result 1000mA */
599                         aicl_current = 1000;
600                         break;
601                 case 4: /* AICL result 1200mA */
602                         aicl_current = 1200;
603                         break;
604                 case 5: /* AICL result 1300mA */
605                         aicl_current = 1300;
606                         break;
607                 case 6: /* AICL result 1800mA */
608                         aicl_current = 1800;
609                         break;
610                 case 7: /* AICL result 2000mA */
611                         aicl_current = 2000;
612                         break;
613                 default: /* etc */
614                         aicl_current = 2000;
615                         break;
616                 }
617                 if (aicl_current <= SLOW_CHARGING_CURRENT_STANDARD)
618                         charger->is_slow_charging = true;
619                 else
620                         charger->is_slow_charging = false;
621         }
622
623         pr_info("%s: Slow(%d), aicl_current(%d), input_current(%d)\n",
624                 __func__, charger->is_slow_charging, aicl_current, charger->pdata->charging_current
625                         [charger->cable_type].input_current_limit);
626
627         psy_do_property("battery", set,
628                 POWER_SUPPLY_PROP_CHARGE_TYPE, val);
629
630 }
631 static void smb358_charger_function_control(
632                                 struct i2c_client *client)
633 {
634         struct sec_charger_info *charger = i2c_get_clientdata(client);
635         union power_supply_propval val, input_value;
636         int status;
637         u8 data;
638
639         psy_do_property("battery", get,
640                 POWER_SUPPLY_PROP_STATUS, input_value);
641         status = input_value.intval;
642
643         charger->charging_current_max =
644                 charger->pdata->charging_current[
645                 charger->cable_type].input_current_limit;
646
647         charger->charging_current =
648                 charger->pdata->charging_current[
649                 charger->cable_type].fast_charging_current;
650
651         if (charger->charging_current < 0) {
652                 dev_dbg(&client->dev,
653                         "%s : OTG is activated. Ignore command!\n", __func__);
654                 return;
655         }
656
657         psy_do_property("battery", get,
658                 POWER_SUPPLY_PROP_HEALTH, input_value);
659         if (input_value.intval ==
660                 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
661                 pr_info("[SMB358] Unspec_failure, charger suspend\n");
662                 smb358_enter_suspend(client);
663         }
664         else if (charger->cable_type ==
665                 POWER_SUPPLY_TYPE_BATTERY) {
666                 /* Charger Disabled */
667                 smb358_set_command(client, SMB358_COMMAND_A, 0xc0);
668                 if ((status == POWER_SUPPLY_STATUS_FULL) ||\
669                         (input_value.intval == POWER_SUPPLY_HEALTH_OVERHEAT) ||\
670                         (input_value.intval == POWER_SUPPLY_HEALTH_COLD))
671                         return;
672                 pr_info("[SMB358] Set the registers to the default configuration\n");
673                 /* Set the registers to the default configuration */
674                 smb358_set_command(client, SMB358_CHARGE_CURRENT, 0xFE);
675                 smb358_set_command(client, SMB358_INPUT_CURRENTLIMIT, 0x74);
676                 smb358_set_command(client, SMB358_VARIOUS_FUNCTIONS, 0xD7);
677                 data = 0x00;
678                 data |= smb358_get_float_voltage_data(charger->pdata->chg_float_voltage);
679                 smb358_set_command(client, SMB358_FLOAT_VOLTAGE, data);
680                 /* Disable Automatic Recharge */
681                 smb358_set_command(client, SMB358_CHARGE_CONTROL, 0x84);
682                 smb358_set_command(client, SMB358_PIN_ENABLE_CONTROL, 0x09);
683                 smb358_set_command(client, SMB358_THERM_CONTROL_A, 0xF0);
684                 smb358_set_command(client, SMB358_SYSOK_USB30_SELECTION, 0x08);
685                 smb358_set_command(client, SMB358_OTHER_CONTROL_A, 0x01);
686                 smb358_set_command(client, SMB358_OTG_TLIM_THERM_CONTROL, 0xF6);
687                 smb358_set_command(client, SMB358_LIMIT_CELL_TEMPERATURE_MONITOR, 0xA5);
688                 smb358_set_command(client, SMB358_STATUS_INTERRUPT, 0x00);
689                 smb358_set_command(client, SMB358_COMMAND_B, 0x00);
690                 if (charger->pdata->chg_irq) {
691                         smb358_set_command(client, SMB358_STAT_TIMERS_CONTROL, 0x1F);
692                         smb358_set_command(client, SMB358_FAULT_INTERRUPT, 0x0C);
693                 } else {
694                         smb358_set_command(client, SMB358_STAT_TIMERS_CONTROL, 0x0F);
695                         smb358_set_command(client, SMB358_FAULT_INTERRUPT, 0x00);
696                 }
697
698         } else {
699                 int full_check_type;
700                 psy_do_property("battery", get,
701                         POWER_SUPPLY_PROP_CHARGE_NOW, val);
702                 if (val.intval == SEC_BATTERY_CHARGING_1ST)
703                         full_check_type = charger->pdata->full_check_type;
704                 else
705                         full_check_type = charger->pdata->full_check_type_2nd;
706
707                 smb358_i2c_read(client, SMB358_COMMAND_A, &data);
708
709                 if ((data & 0x10) && charger->pdata->vbus_ctrl_gpio) {
710                         int level;
711                         /* disable otg & charging */
712                         smb358_clear_reg(client, SMB358_COMMAND_A, 0x12);
713
714                         /* turn off vbus */
715                         gpio_set_value(charger->pdata->vbus_ctrl_gpio, 1);
716                         msleep(30);
717
718                         level = gpio_get_value_cansleep(charger->pdata->vbus_ctrl_gpio);
719                         pr_info("[SMB358] vbus ctrl gpio %d level %d\n", charger->pdata->vbus_ctrl_gpio, level);
720
721                         /* turn on vbus */
722                         gpio_set_value(charger->pdata->vbus_ctrl_gpio, 0);
723                 }
724                 /* [STEP - 1] ================================================
725                  * Volatile write permission(bit 7) - allow(1)
726                  * Charging Enable(bit 1) - Disabled(0, default)
727                  * STAT Output(bit 0) - Enabled(0)
728                 */
729                 if (data & 0x02) {
730                         u8 status_c;
731                         u8 status_e;
732                         smb358_i2c_read(client, SMB358_STATUS_C, &status_c);
733                         smb358_i2c_read(client, SMB358_STATUS_E, &status_e);
734                         pr_info("[SMB358] status_c: 0x%x, status_e: 0x%x\n", status_c, status_e);
735
736                         /* no charge or aicl not complete*/
737                         if (((status_c & 0x06) == 0) || (status_e & 0x10) == 0)
738                                 smb358_set_command(client,
739                                                 SMB358_COMMAND_A, 0xC0);
740                 }
741
742                 /* [STEP - 2] ================================================
743                  * USB 5/1(9/1.5) Mode(bit 1) - USB1/USB1.5(0), USB5/USB9(1)
744                  * USB/HC Mode(bit 0) - USB5/1 or USB9/1.5 Mode(0)
745                  *                      High-Current Mode(1)
746                 */
747                 switch (charger->cable_type) {
748                 case POWER_SUPPLY_TYPE_UNKNOWN:
749                 case POWER_SUPPLY_TYPE_MAINS:
750                 case POWER_SUPPLY_TYPE_USB_CDP:
751                 case POWER_SUPPLY_TYPE_MISC:
752                 case POWER_SUPPLY_TYPE_WIRELESS:
753                 case POWER_SUPPLY_TYPE_CARDOCK:
754                 case POWER_SUPPLY_TYPE_UARTOFF:
755                 case POWER_SUPPLY_TYPE_LAN_HUB:
756                 case POWER_SUPPLY_TYPE_MHL_900:
757                 case POWER_SUPPLY_TYPE_MHL_1500:
758                 case POWER_SUPPLY_TYPE_SMART_NOTG:
759                     /* High-current mode */
760                     data = 0x03;
761                         break;
762                 case POWER_SUPPLY_TYPE_UPS:
763                 case POWER_SUPPLY_TYPE_USB:
764                 case POWER_SUPPLY_TYPE_USB_DCP:
765                 case POWER_SUPPLY_TYPE_USB_ACA:
766                 case POWER_SUPPLY_TYPE_MHL_500:
767                 case POWER_SUPPLY_TYPE_MHL_USB:
768                 case POWER_SUPPLY_TYPE_SMART_OTG:
769                 case POWER_SUPPLY_TYPE_POWER_SHARING:
770                         /* USB5 */
771                         data = 0x02;
772                         break;
773                 default:
774                         /* USB1 */
775                         data = 0x00;
776                         break;
777                 }
778                 smb358_set_command(client,
779                         SMB358_COMMAND_B, data);
780
781
782                 /* [STEP 3] Charge Current(0x00) ===============================
783                  * Set pre-charge current(bit 4:3) - 450mA(11)
784                  * Set fast charge current(bit 7:5)
785                  * Set termination current(bit 2:0)
786                 */
787                 if (charger->siop_level < 100) {
788                         charger->charging_current =
789                                 charger->charging_current * charger->siop_level / 100;
790                 }
791                 dev_info(&client->dev,
792                         "%s : fast charging current (%dmA)\n",
793                         __func__, charger->charging_current);
794
795                 data = 0x18;
796                 data |= smb358_get_fast_charging_current_data(
797                         charger->charging_current);
798                 switch (full_check_type) {
799                 case SEC_BATTERY_FULLCHARGED_CHGGPIO:
800                 case SEC_BATTERY_FULLCHARGED_CHGINT:
801                 case SEC_BATTERY_FULLCHARGED_CHGPSY:
802                         if (val.intval == SEC_BATTERY_CHARGING_1ST) {
803                                 dev_info(&client->dev,
804                                         "%s : termination current (%dmA)\n",
805                                         __func__, charger->pdata->charging_current[
806                                         charger->cable_type].full_check_current_1st);
807                                 data |= smb358_get_term_current_limit_data(
808                                         charger->pdata->charging_current[
809                                         charger->cable_type].full_check_current_1st);
810                         } else {
811                                 dev_info(&client->dev,
812                                         "%s : termination current (%dmA)\n",
813                                         __func__, charger->pdata->charging_current[
814                                         charger->cable_type].full_check_current_2nd);
815                                 data |= smb358_get_term_current_limit_data(
816                                         charger->pdata->charging_current[
817                                         charger->cable_type].full_check_current_2nd);
818                         }
819                         break;
820                 }
821                 smb358_set_command(client,
822                         SMB358_CHARGE_CURRENT, data);
823
824                 /* [STEP - 4] =================================================
825                  * Enable(EN) Pin Control(bit 6) - i2c(0), Pin(1)
826                  * Pin control(bit 5) - active high(0), active low(1)
827                  * USB5/1/HC input State(bit3) - Dual-state input(1)
828                  * USB Input Pre-bias(bit 0) - Enable(1)
829                 */
830                 data = 0x09;
831                 if (charger->pdata->chg_gpio_en)
832                         data |= 0x40;
833                 if (charger->pdata->chg_polarity_en)
834                         data |= 0x20;
835                 smb358_set_command(client,
836                         SMB358_PIN_ENABLE_CONTROL, data);
837
838                 /* [STEP - 5] =============================================== */
839                 dev_info(&client->dev, "%s : input current (%dmA)\n",
840                         __func__, charger->pdata->charging_current
841                         [charger->cable_type].input_current_limit);
842                 /* Input current limit */
843                 data = 0x00;
844                 data |= smb358_get_input_current_limit_data(
845                         charger,
846                         charger->pdata->charging_current
847                         [charger->cable_type].input_current_limit);
848                 smb358_set_command(client,
849                         SMB358_INPUT_CURRENTLIMIT, data);
850
851                 /* [STEP - 6] =================================================
852                  * Input to System FET(bit 7) - Controlled by Register(1)
853                  * Max System voltage(bit 5) -  Vflt + 0.1v(0)
854                  * AICL(bit 4) - Enabled(1)
855                  * VCHG Function(bit 0) - Enabled(1)
856                  */
857                 if (charger->pdata->chg_functions_setting &
858                         SEC_CHARGER_NO_GRADUAL_CHARGING_CURRENT)
859                         /* disable AICL */
860                         smb358_set_command(client,
861                                 SMB358_VARIOUS_FUNCTIONS, 0x81);
862                 else {
863                         /* disable AICL */
864                         smb358_set_command(client,
865                                 SMB358_VARIOUS_FUNCTIONS, 0x81);
866                         /* enable AICL */
867                         smb358_set_command(client,
868                                 SMB358_VARIOUS_FUNCTIONS, 0x95);
869                 }
870
871                 /* [STEP - 7] =================================================
872                  * Pre-charged to Fast-charge Voltage Threshold(Bit 7:6) - 2.3V
873                  * Float Voltage(bit 5:0)
874                 */
875                 dev_dbg(&client->dev, "%s : float voltage (%dmV)\n",
876                                 __func__, charger->pdata->chg_float_voltage);
877                 data = 0x00;
878                 data |= smb358_get_float_voltage_data(
879                         charger->pdata->chg_float_voltage);
880                 smb358_set_command(client,
881                         SMB358_FLOAT_VOLTAGE, data);
882
883                 /* [STEP - 8] =================================================
884                  * Charge control
885                  * Automatic Recharge disable(bit 7),
886                  * Current Termination disable(bit 6),
887                  * BMD disable(bit 5:4),
888                  * INOK Output Configuration : Push-pull(bit 3)
889                  * AICL glitch filter duration : 20msec(bit 0)
890                  * APSD disable
891                 */
892                 data = 0xC0;
893
894                 switch (full_check_type) {
895                 case SEC_BATTERY_FULLCHARGED_CHGGPIO:
896                 case SEC_BATTERY_FULLCHARGED_CHGINT:
897                 case SEC_BATTERY_FULLCHARGED_CHGPSY:
898                         /* Enable Current Termination */
899                         data &= 0xBF;
900                         break;
901                 }
902                 smb358_set_command(client,
903                         SMB358_CHARGE_CONTROL, data);
904
905                 /* [STEP - 9] =================================================
906                  *  STAT active low(bit 7),
907                  *  Complete charge Timeout(bit 3:2) - Disabled(11)
908                  *  Pre-charge Timeout(bit 1:0) - Disable(11)
909                 */
910                 smb358_set_command(client,
911                         SMB358_STAT_TIMERS_CONTROL, 0x1F);
912
913 #if defined(CONFIG_MACH_MATISSELTE_VZW)
914                 /* [STEP - 10] =================================================
915                  * Mininum System Voltage(bit 6) - 3.15v(0)
916                  * Therm monitor(bit 4) - Disabled(1)
917                  * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
918                  *   Charger Current + Float voltage Compensation(11)
919                 */
920                 smb358_set_command(client,
921                         SMB358_THERM_CONTROL_A, 0xB0);
922
923                 /* [STEP - 11] ================================================
924                  * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
925                  * Minimum System Voltage(bit 4) - 3.15V(0)
926                  * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
927                  *    if this bit is disabled,
928                  *    input current for system will be disabled
929                  */
930                 smb358_set_command(client,
931                         SMB358_OTHER_CONTROL_A, 0x11);
932 #elif defined(CONFIG_MACH_CHAGALL_USC)
933                 /* [STEP - 10] =================================================
934                  * Mininum System Voltage(bit 6) - 3.45v(0)
935                  * Therm monitor(bit 4) - Disabled(1)
936                  * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
937                  *   Charger Current + Float voltage Compensation(11)
938                  */
939                 smb358_set_command(client,
940                                 SMB358_THERM_CONTROL_A, 0xB0);
941
942                 /* [STEP - 11] ================================================
943                  * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
944                  * Minimum System Voltage(bit 4) - 3.45V(0)
945                  * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
946                  *    if this bit is disabled,
947                  *    input current for system will be disabled
948                  */
949                 smb358_set_command(client,
950                                 SMB358_OTHER_CONTROL_A, 0x01);
951 #else
952 {
953                 u8 therm_control_a, other_control_a;
954                 /* [STEP - 10] =================================================
955                 * Mininum System Voltage(bit 6)
956                 * 3.15V(0), 3.45V(0), 3.60V(1), 3.75V(1)
957                 * Therm monitor(bit 4) - Disabled(1)
958                 * Soft Cold/Hot Temp Limit Behavior(bit 3:2, bit 1:0) -
959                 *   Charger Current + Float voltage Compensation(11)
960                 */
961                 smb358_i2c_read(client, SMB358_THERM_CONTROL_A, &therm_control_a);
962                 pr_info("%s : THERM_CONTROL_A(0x%02x)\n", __func__, therm_control_a);
963                 smb358_i2c_read(client, SMB358_OTHER_CONTROL_A, &other_control_a);
964                 pr_info("%s : OTHER_CONTROL_A(0x%02x)\n", __func__, other_control_a);
965
966                 pr_info("%s: chg_min_system_voltage(%d)\n", __func__, charger->pdata->chg_min_system_voltage);
967
968                 if (charger->pdata->chg_min_system_voltage <= 3150) {
969                         therm_control_a &= ~(0x1 << 6);
970                         other_control_a |=  0x1 << 4;
971                 } else if (charger->pdata->chg_min_system_voltage <= 3450) {
972                         therm_control_a &= ~(0x1 << 6);
973                         other_control_a &= ~(0x1 << 4);
974                 } else if (charger->pdata->chg_min_system_voltage <= 3600) {
975                         therm_control_a |= 0x1 << 6;
976                         other_control_a &= ~(0x1 << 4);
977                 } else {
978                         therm_control_a |= 0x1 << 6;
979                         other_control_a |= 0x1 << 4;
980                 }
981
982                 pr_info("%s: therm_control_a(0x%02x)\n", __func__, therm_control_a);
983                 pr_info("%s: other_control_a(0x%02x)\n", __func__, other_control_a);
984
985                 smb358_set_command(client,
986                         SMB358_THERM_CONTROL_A, therm_control_a);
987
988                 /* [STEP - 11] ================================================
989                  * OTG/ID Pin Control(bit 7:6) - RID Disabled, OTG I2c(00)
990                  * Minimum System Voltage(bit 4)
991                  * 3.15V(1), 3.45V(0), 3.60V(0), 3.75(1)
992                  * Low-Battery/SYSOK Voltage threshold(bit 3:0) - 2.5V(0001)
993                  *   if this bit is disabled,
994                  *   input current for system will be disabled
995                  */
996                 smb358_set_command(client,
997                         SMB358_OTHER_CONTROL_A, other_control_a);
998 }
999 #endif
1000
1001                 /* [STEP - 12] ================================================
1002                  * Charge Current Compensation(bit 7:6) - 200mA(00)
1003                  * Digital Thermal Regulation Threshold(bit 5:4) - 130c
1004                  * OTG current Limit at USBIN(Bit 3:2) - 900mA(11)
1005                  * OTG Battery UVLO Threshold(Bit 1:0) - 3.3V(11)
1006                 */
1007                 smb358_set_command(client,
1008                         SMB358_OTG_TLIM_THERM_CONTROL, 0x3F);
1009
1010                 /* [STEP - 13] ================================================
1011                  * Hard/Soft Limit Cell temp monitor
1012                 */
1013                 smb358_set_command(client,
1014                         SMB358_LIMIT_CELL_TEMPERATURE_MONITOR, 0x01);
1015
1016                 /* [STEP - 14] ================================================
1017                  * FAULT interrupt - Disabled for non chg_irq, OVP enabled for chg_irq
1018                 */
1019                 if (charger->pdata->chg_irq) {
1020                         smb358_set_command(client,
1021                                 SMB358_FAULT_INTERRUPT, 0x0C);
1022                 } else {
1023                         smb358_set_command(client,
1024                                 SMB358_FAULT_INTERRUPT, 0x00);
1025                 }
1026
1027                 /* [STEP - 15] ================================================
1028                  * STATUS interrupt - Clear
1029                 */
1030                 smb358_set_command(client,
1031                         SMB358_STATUS_INTERRUPT, 0x00);
1032
1033                 /* [STEP - 16] ================================================
1034                  * Volatile write permission(bit 7) - allowed(1)
1035                  * Charging Enable(bit 1) - Enabled(1)
1036                  * STAT Output(bit 0) - Enabled(0)
1037                 */
1038                 smb358_set_command(client,
1039                         SMB358_COMMAND_A, 0xC2);
1040
1041                 schedule_delayed_work(&charger->slow_work, 0);
1042         }
1043 #if 0
1044 #if (defined(CONFIG_MACH_MILLET3G_EUR) || defined(CONFIG_MACH_MATISSE3G_OPEN) || defined(CONFIG_MACH_BERLUTI3G_EUR))
1045         /* Allow time for AICL to complete */
1046         msleep(1000);
1047         smb358_aicl_calibrate(client);
1048 #endif
1049 #endif
1050 }
1051
1052 static void smb358_charger_otg_control(
1053                                 struct i2c_client *client)
1054 {
1055         struct sec_charger_info *charger = i2c_get_clientdata(client);
1056
1057         if (charger->cable_type ==
1058                 POWER_SUPPLY_TYPE_BATTERY) {
1059                 dev_info(&client->dev, "%s : turn off OTG\n", __func__);
1060
1061                 /* disable otg */
1062                 smb358_clear_reg(client, SMB358_COMMAND_A, 0x10);
1063         } else {
1064                 /* Change "OTG output current limit" to 250mA */
1065                 smb358_clear_reg(client, SMB358_OTG_TLIM_THERM_CONTROL, 0x0C);
1066
1067                 /* OTG Enabled*/
1068                 smb358_update_reg(client, SMB358_COMMAND_A, 0x10);
1069
1070                 dev_info(&client->dev, "%s : turn on OTG\n", __func__);
1071                 smb358_set_command(client, SMB358_COMMAND_B, 0x00);
1072
1073                 /* Change "OTG output current limit" to 500mA */
1074                 smb358_update_reg(client, SMB358_OTG_TLIM_THERM_CONTROL, 0x84);
1075         }
1076
1077 }
1078
1079 static void smb358_set_charging_current(
1080                 struct i2c_client *client, int charging_current)
1081 {
1082         u8 data;
1083
1084         smb358_clear_reg(client, SMB358_COMMAND_A, 0x02);
1085
1086         if (!charging_current)
1087                 return;
1088
1089         smb358_i2c_read(client, SMB358_CHARGE_CURRENT, &data);
1090         data &= 0x1f;
1091         data |= smb358_get_fast_charging_current_data(charging_current);
1092         smb358_set_command(client, SMB358_CHARGE_CURRENT, data);
1093
1094         smb358_update_reg(client, SMB358_COMMAND_A, 0x02);
1095 }
1096
1097 static void smb358_set_charging_input_current_limit(
1098                 struct i2c_client *client, int input_current_limit)
1099 {
1100         struct sec_charger_info *charger = i2c_get_clientdata(client);
1101         u8 data;
1102
1103         /* Input current limit */
1104         data = 0;
1105         data = smb358_get_input_current_limit_data(
1106                 charger, input_current_limit);
1107         smb358_set_command(client, SMB358_INPUT_CURRENTLIMIT, data);
1108 }
1109
1110 void smb358_charger_shutdown(struct i2c_client *client)
1111 {
1112         pr_info("%s: smb358 Charging Disabled\n", __func__);
1113
1114         smb358_set_command(client, SMB358_THERM_CONTROL_A, 0xF0);
1115         smb358_set_command(client, SMB358_COMMAND_A, 0x80);
1116         smb358_volatile_writes(client, SMB358_DISABLE_WRITE);
1117 }
1118
1119 static int smb358_debugfs_show(struct seq_file *s, void *data)
1120 {
1121         struct sec_charger_info *charger = s->private;
1122         u8 reg;
1123         u8 reg_data;
1124
1125         seq_printf(s, "SMB CHARGER IC :\n");
1126         seq_printf(s, "==================\n");
1127         for (reg = 0x00; reg <= 0x0E; reg++) {
1128                 smb358_i2c_read(charger->client, reg, &reg_data);
1129                 seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
1130         }
1131
1132         for (reg = 0x30; reg <= 0x3F; reg++) {
1133                 smb358_i2c_read(charger->client, reg, &reg_data);
1134                 seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
1135         }
1136
1137         seq_printf(s, "\n");
1138         return 0;
1139 }
1140
1141 static int smb358_debugfs_open(struct inode *inode, struct file *file)
1142 {
1143         return single_open(file, smb358_debugfs_show, inode->i_private);
1144 }
1145
1146 static const struct file_operations smb358_debugfs_fops = {
1147         .open           = smb358_debugfs_open,
1148         .read           = seq_read,
1149         .llseek         = seq_lseek,
1150         .release        = single_release,
1151 };
1152
1153 bool smb358_hal_chg_init(struct i2c_client *client)
1154 {
1155         struct sec_charger_info *charger = i2c_get_clientdata(client);
1156
1157         dev_info(&client->dev,
1158                 "%s: SMB358 Charger init(Start)!!\n", __func__);
1159
1160         smb358_volatile_writes(client, SMB358_ENABLE_WRITE);
1161
1162         /*smb358_test_read(client);*/
1163         (void) debugfs_create_file("smb358_regs",
1164                 S_IRUGO, NULL, (void *)charger, &smb358_debugfs_fops);
1165
1166         return true;
1167 }
1168
1169 bool smb358_hal_chg_suspend(struct i2c_client *client)
1170 {
1171         return true;
1172 }
1173
1174 bool smb358_hal_chg_resume(struct i2c_client *client)
1175 {
1176         return true;
1177 }
1178
1179 bool smb358_hal_chg_get_property(struct i2c_client *client,
1180                               enum power_supply_property psp,
1181                               union power_supply_propval *val)
1182 {
1183         struct sec_charger_info *charger = i2c_get_clientdata(client);
1184         u8 data;
1185         switch (psp) {
1186         case POWER_SUPPLY_PROP_STATUS:
1187                 val->intval = smb358_get_charging_status(client);
1188                 break;
1189
1190         case POWER_SUPPLY_PROP_CHARGE_TYPE:
1191                 if (charger->is_charging) {
1192                         val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1193                         if (charger->is_slow_charging) {
1194                                 val->intval = POWER_SUPPLY_CHARGE_TYPE_SLOW;
1195                                 pr_info("%s: slow-charging mode\n", __func__);
1196                         }
1197                 }
1198                 else
1199                         val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1200                 break;
1201
1202         case POWER_SUPPLY_PROP_HEALTH:
1203                 val->intval = smb358_get_charging_health(client);
1204                 break;
1205         /* calculated input current limit value */
1206         case POWER_SUPPLY_PROP_CURRENT_NOW:
1207         case POWER_SUPPLY_PROP_CURRENT_AVG:     /* charging current */
1208                 if (charger->charging_current) {
1209                         smb358_i2c_read(client, SMB358_STATUS_B, &data);
1210                         if (data & 0x20)
1211                                 switch (data & 0x07) {
1212                                 case 0:
1213                                         val->intval = 100;
1214                                         break;
1215                                 case 1:
1216                                         val->intval = 200;
1217                                         break;
1218                                 case 2:
1219                                         val->intval = 450;
1220                                         break;
1221                                 case 3:
1222                                         val->intval = 600;
1223                                         break;
1224                                 case 4:
1225                                         val->intval = 900;
1226                                         break;
1227                                 case 5:
1228                                         val->intval = 1300;
1229                                         break;
1230                                 case 6:
1231                                         val->intval = 1500;
1232                                         break;
1233                                 case 7:
1234                                         val->intval = 1800;
1235                                         break;
1236                                 }
1237                         else
1238                                 switch ((data & 0x18) >> 3) {
1239                                 case 0:
1240                                         val->intval = 100;
1241                                         break;
1242                                 case 1:
1243                                         val->intval = 150;
1244                                         break;
1245                                 case 2:
1246                                         val->intval = 200;
1247                                         break;
1248                                 case 3:
1249                                         val->intval = 250;
1250                                         break;
1251                                 }
1252                 } else
1253                         val->intval = 0;
1254                 dev_dbg(&client->dev,
1255                         "%s : set-current(%dmA), current now(%dmA)\n",
1256                         __func__, charger->charging_current, val->intval);
1257                 break;
1258         default:
1259                 return false;
1260         }
1261         return true;
1262 }
1263
1264 bool smb358_hal_chg_set_property(struct i2c_client *client,
1265                               enum power_supply_property psp,
1266                               const union power_supply_propval *val)
1267 {
1268         struct sec_charger_info *charger = i2c_get_clientdata(client);
1269
1270         switch (psp) {
1271         /* val->intval : type */
1272         case POWER_SUPPLY_PROP_ONLINE:
1273                 if (val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
1274                         union power_supply_propval ps_status;
1275                         psy_do_property("ps", get,
1276                                 POWER_SUPPLY_PROP_STATUS, ps_status);
1277                         if (ps_status.intval) {
1278                                 charger->cable_type = POWER_SUPPLY_TYPE_OTG;
1279                                 pr_info("%s: ps enable\n", __func__);
1280                         } else {
1281                                 charger->cable_type = POWER_SUPPLY_TYPE_BATTERY;
1282                                 pr_info("%s: ps disable\n", __func__);
1283                         }
1284                 }
1285                 if (charger->cable_type == POWER_SUPPLY_TYPE_OTG) {
1286                         smb358_charger_otg_control(client);
1287                 } else if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
1288                         smb358_charger_function_control(client);
1289                         smb358_charger_otg_control(client);
1290                 } else {
1291                         smb358_charger_function_control(client);
1292                 }
1293                 /* smb358_test_read(client); */
1294                 break;
1295         case POWER_SUPPLY_PROP_CURRENT_MAX:     /* input current limit set */
1296         /* calculated input current limit value */
1297         case POWER_SUPPLY_PROP_CURRENT_NOW:
1298                 smb358_set_charging_input_current_limit(client, val->intval);
1299                 break;
1300         /* val->intval : charging current */
1301         case POWER_SUPPLY_PROP_CURRENT_AVG:
1302                 smb358_set_charging_current(client, val->intval);
1303                 break;
1304         default:
1305                 return false;
1306         }
1307         return true;
1308 }
1309
1310 ssize_t smb358_hal_chg_show_attrs(struct device *dev,
1311                                 const ptrdiff_t offset, char *buf)
1312 {
1313         struct power_supply *psy = dev_get_drvdata(dev);
1314         struct sec_charger_info *chg =
1315                 container_of(psy, struct sec_charger_info, psy_chg);
1316         int i = 0;
1317         char *str = NULL;
1318
1319         switch (offset) {
1320         case CHG_DATA:
1321                 i += scnprintf(buf + i, PAGE_SIZE - i, "%x\n",
1322                         chg->reg_data);
1323                 break;
1324         case CHG_REGS:
1325                 str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
1326                 if (!str)
1327                         return -ENOMEM;
1328
1329                 smb358_read_regs(chg->client, str);
1330                 i += scnprintf(buf + i, PAGE_SIZE - i, "%s\n",
1331                         str);
1332
1333                 kfree(str);
1334                 break;
1335         default:
1336                 i = -EINVAL;
1337                 break;
1338         }
1339
1340         return i;
1341 }
1342
1343 ssize_t smb358_hal_chg_store_attrs(struct device *dev,
1344                                 const ptrdiff_t offset,
1345                                 const char *buf, size_t count)
1346 {
1347         struct power_supply *psy = dev_get_drvdata(dev);
1348         struct sec_charger_info *chg =
1349                 container_of(psy, struct sec_charger_info, psy_chg);
1350         int ret = 0;
1351         int x = 0;
1352         u8 data = 0;
1353
1354         switch (offset) {
1355         case CHG_REG:
1356                 if (sscanf(buf, "%x\n", &x) == 1) {
1357                         chg->reg_addr = x;
1358                         smb358_i2c_read(chg->client,
1359                                 chg->reg_addr, &data);
1360                         chg->reg_data = data;
1361                         dev_dbg(dev, "%s: (read) addr = 0x%x, data = 0x%x\n",
1362                                 __func__, chg->reg_addr, chg->reg_data);
1363                         ret = count;
1364                 }
1365                 break;
1366         case CHG_DATA:
1367                 if (sscanf(buf, "%x\n", &x) == 1) {
1368                         data = (u8)x;
1369                         dev_dbg(dev, "%s: (write) addr = 0x%x, data = 0x%x\n",
1370                                 __func__, chg->reg_addr, data);
1371                         smb358_i2c_write(chg->client,
1372                                 chg->reg_addr, &data);
1373                         ret = count;
1374                 }
1375                 break;
1376         default:
1377                 ret = -EINVAL;
1378                 break;
1379         }
1380
1381         return ret;
1382 }
1383
1384 static struct device_attribute smb358_charger_attrs[] = {
1385         SMB358_CHARGER_ATTR(reg),
1386         SMB358_CHARGER_ATTR(data),
1387         SMB358_CHARGER_ATTR(regs),
1388 };
1389
1390 static enum power_supply_property smb358_charger_props[] = {
1391         POWER_SUPPLY_PROP_STATUS,
1392         POWER_SUPPLY_PROP_CHARGE_TYPE,
1393         POWER_SUPPLY_PROP_HEALTH,
1394         POWER_SUPPLY_PROP_ONLINE,
1395         POWER_SUPPLY_PROP_CURRENT_MAX,
1396         POWER_SUPPLY_PROP_CURRENT_AVG,
1397         POWER_SUPPLY_PROP_CURRENT_NOW,
1398         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1399 };
1400
1401 static int smb358_chg_get_property(struct power_supply *psy,
1402                             enum power_supply_property psp,
1403                             union power_supply_propval *val)
1404 {
1405         struct sec_charger_info *charger =
1406                 container_of(psy, struct sec_charger_info, psy_chg);
1407         u8 data = 0;
1408
1409         switch (psp) {
1410         case POWER_SUPPLY_PROP_CURRENT_MAX:     /* input current limit set */
1411                 smb358_i2c_read(charger->client, SMB358_STATUS_E, &data);
1412                 if (data & 0x10) {
1413                         int aicl_result = smb358_get_aicl_current(data);
1414                         dev_info(&charger->client->dev,
1415                                 "%s : AICL completed (%dmA)\n", __func__, aicl_result);
1416                         charger->charging_current_max = aicl_result;
1417                 } else {
1418                         dev_info(&charger->client->dev,
1419                                 "%s : AICL is not completed \n", __func__);
1420                         charger->charging_current_max = 300;
1421                 }
1422                 val->intval = charger->charging_current_max;
1423                 break;
1424
1425         case POWER_SUPPLY_PROP_ONLINE:
1426         case POWER_SUPPLY_PROP_STATUS:
1427         case POWER_SUPPLY_PROP_CHARGE_TYPE:
1428         case POWER_SUPPLY_PROP_HEALTH:
1429         case POWER_SUPPLY_PROP_CURRENT_AVG:     /* charging current */
1430         /* calculated input current limit value */
1431         case POWER_SUPPLY_PROP_CURRENT_NOW:
1432                 if (!smb358_hal_chg_get_property(charger->client, psp, val))
1433                         return -EINVAL;
1434                 break;
1435         default:
1436                 return -EINVAL;
1437         }
1438         return 0;
1439 }
1440
1441 static int smb358_chg_set_property(struct power_supply *psy,
1442                             enum power_supply_property psp,
1443                             const union power_supply_propval *val)
1444 {
1445         struct sec_charger_info *charger =
1446                 container_of(psy, struct sec_charger_info, psy_chg);
1447         union power_supply_propval input_value;
1448
1449         switch (psp) {
1450         case POWER_SUPPLY_PROP_STATUS:
1451                 charger->status = val->intval;
1452                 break;
1453
1454         /* val->intval : type */
1455         case POWER_SUPPLY_PROP_ONLINE:
1456                 charger->cable_type = val->intval;
1457                 if (val->intval == POWER_SUPPLY_TYPE_BATTERY || \
1458                                 val->intval == POWER_SUPPLY_TYPE_OTG || \
1459                                 val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
1460                         charger->is_charging = false;
1461                         charger->is_slow_charging = false;
1462                 }
1463                 else
1464                         charger->is_charging = true;
1465
1466                 if (!smb358_hal_chg_set_property(charger->client, psp, val))
1467                         return -EINVAL;
1468                 break;
1469
1470         /* val->intval : input current limit set */
1471         case POWER_SUPPLY_PROP_CURRENT_MAX:
1472                 charger->charging_current_max = val->intval;
1473         /* to control charging current,
1474          * use input current limit and set charging current as much as possible
1475          * so we only control input current limit to control charge current
1476          */
1477         case POWER_SUPPLY_PROP_CURRENT_NOW:
1478                 if (!smb358_hal_chg_set_property(charger->client, psp, val))
1479                         return -EINVAL;
1480                 break;
1481
1482         /* val->intval : charging current */
1483         case POWER_SUPPLY_PROP_CURRENT_AVG:
1484                 charger->charging_current = val->intval;
1485
1486                 if (!smb358_hal_chg_set_property(charger->client, psp, val))
1487                         return -EINVAL;
1488                 break;
1489
1490         /* val->intval : SIOP level (%)
1491          * SIOP charging current setting
1492          */
1493         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1494                 charger->siop_level = val->intval;
1495                 if (charger->is_charging) {
1496                         /* change val as charging current by SIOP level
1497                         * do NOT change initial charging current setting
1498                         */
1499                         input_value.intval =
1500                                         charger->pdata->charging_current[
1501                                         charger->cable_type].fast_charging_current * val->intval / 100;
1502
1503                         /* charging current should be over than USB charging current */
1504                         if (charger->pdata->chg_functions_setting &
1505                                 SEC_CHARGER_MINIMUM_SIOP_CHARGING_CURRENT) {
1506                                 if (input_value.intval > 0 &&
1507                                         input_value.intval <
1508                                         charger->pdata->charging_current[
1509                                         POWER_SUPPLY_TYPE_USB].fast_charging_current)
1510                                         input_value.intval =
1511                                         charger->pdata->charging_current[
1512                                         POWER_SUPPLY_TYPE_USB].fast_charging_current;
1513                         }
1514
1515                         /* set charging current as new value */
1516                         if (!smb358_hal_chg_set_property(charger->client,
1517                                 POWER_SUPPLY_PROP_CURRENT_AVG, &input_value))
1518                                 return -EINVAL;
1519                 }
1520                 break;
1521
1522         default:
1523                 return -EINVAL;
1524         }
1525         return 0;
1526 }
1527
1528 static void smb358_chg_isr_work(struct work_struct *work)
1529 {
1530         struct sec_charger_info *charger =
1531                 container_of(work, struct sec_charger_info, isr_work.work);
1532         union power_supply_propval val;
1533         int full_check_type;
1534
1535         dev_info(&charger->client->dev,
1536                 "%s: Charger Interrupt\n", __func__);
1537
1538         psy_do_property("battery", get,
1539                 POWER_SUPPLY_PROP_CHARGE_NOW, val);
1540         if (val.intval == SEC_BATTERY_CHARGING_1ST)
1541                 full_check_type = charger->pdata->full_check_type;
1542         else
1543                 full_check_type = charger->pdata->full_check_type_2nd;
1544
1545         if (full_check_type == SEC_BATTERY_FULLCHARGED_CHGINT) {
1546                 if (!smb358_hal_chg_get_property(charger->client,
1547                         POWER_SUPPLY_PROP_STATUS, &val))
1548                         return;
1549
1550                 switch (val.intval) {
1551                 case POWER_SUPPLY_STATUS_DISCHARGING:
1552                         dev_err(&charger->client->dev,
1553                                 "%s: Interrupted but Discharging\n", __func__);
1554                         break;
1555
1556                 case POWER_SUPPLY_STATUS_NOT_CHARGING:
1557                         dev_err(&charger->client->dev,
1558                                 "%s: Interrupted but NOT Charging\n", __func__);
1559                         break;
1560
1561                 case POWER_SUPPLY_STATUS_FULL:
1562                         dev_info(&charger->client->dev,
1563                                 "%s: Interrupted by Full\n", __func__);
1564                         psy_do_property("battery", set,
1565                                 POWER_SUPPLY_PROP_STATUS, val);
1566                         break;
1567
1568                 case POWER_SUPPLY_STATUS_CHARGING:
1569                         dev_err(&charger->client->dev,
1570                                 "%s: Interrupted but Charging\n", __func__);
1571                         break;
1572
1573                 case POWER_SUPPLY_STATUS_UNKNOWN:
1574                 default:
1575                         dev_err(&charger->client->dev,
1576                                 "%s: Invalid Charger Status\n", __func__);
1577                         break;
1578                 }
1579         }
1580
1581         if (charger->pdata->ovp_uvlo_check_type ==
1582                 SEC_BATTERY_OVP_UVLO_CHGINT) {
1583                 if (!smb358_hal_chg_get_property(charger->client,
1584                         POWER_SUPPLY_PROP_HEALTH, &val))
1585                         return;
1586
1587                 switch (val.intval) {
1588                 case POWER_SUPPLY_HEALTH_OVERHEAT:
1589                 case POWER_SUPPLY_HEALTH_COLD:
1590                         dev_err(&charger->client->dev,
1591                                 "%s: Interrupted but Hot/Cold\n", __func__);
1592                         break;
1593
1594                 case POWER_SUPPLY_HEALTH_DEAD:
1595                         dev_err(&charger->client->dev,
1596                                 "%s: Interrupted but Dead\n", __func__);
1597                         break;
1598
1599                 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1600                 case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
1601                         dev_info(&charger->client->dev,
1602                                 "%s: Interrupted by OVP/UVLO\n", __func__);
1603                         /* Do not set POWER_SUPPLY_PROP_HEALTH
1604                         * excute monitor work again.
1605                         * ovp/uvlo is checked by polling
1606                         */
1607                         psy_do_property("battery", set,
1608                                 POWER_SUPPLY_PROP_CHARGE_TYPE, val);
1609                         break;
1610
1611                 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1612                         dev_err(&charger->client->dev,
1613                                 "%s: Interrupted but Unspec\n", __func__);
1614                         break;
1615
1616                 case POWER_SUPPLY_HEALTH_GOOD:
1617                         dev_err(&charger->client->dev,
1618                                 "%s: Interrupted but Good\n", __func__);
1619                         /* Do not set POWER_SUPPLY_PROP_HEALTH
1620                         * excute monitor work again.
1621                         * ovp/uvlo is checked by polling
1622                         */
1623                         psy_do_property("battery", set,
1624                                 POWER_SUPPLY_PROP_CHARGE_TYPE, val);
1625                         break;
1626
1627                 case POWER_SUPPLY_HEALTH_UNKNOWN:
1628                 default:
1629                         dev_err(&charger->client->dev,
1630                                 "%s: Invalid Charger Health\n", __func__);
1631                         break;
1632                 }
1633         }
1634
1635         if (charger->pdata->cable_check_type & SEC_BATTERY_CABLE_CHECK_CHGINT) {
1636                 if (!smb358_hal_chg_get_property(charger->client,
1637                         POWER_SUPPLY_PROP_ONLINE, &val))
1638                         return;
1639
1640                 /* use SEC_BATTERY_CABLE_SOURCE_EXTERNAL for cable_source_type
1641                  * charger would call battery driver to set ONLINE property
1642                  * check battery driver loaded or not
1643                  */
1644                 if (get_power_supply_by_name("battery")) {
1645                         psy_do_property("battery", set,
1646                                 POWER_SUPPLY_PROP_ONLINE, val);
1647                 } else {
1648                         if (charger->pdata->check_cable_result_callback)
1649                                 charger->pdata->check_cable_result_callback(val.intval);
1650                 }
1651         }
1652 }
1653
1654 static irqreturn_t smb358_chg_irq_thread(int irq, void *irq_data)
1655 {
1656         struct sec_charger_info *charger = irq_data;
1657
1658         schedule_delayed_work(&charger->isr_work, 0);
1659
1660         return IRQ_HANDLED;
1661 }
1662
1663 static int smb358_chg_create_attrs(struct device *dev)
1664 {
1665         int i, rc;
1666
1667         for (i = 0; i < ARRAY_SIZE(smb358_charger_attrs); i++) {
1668                 rc = device_create_file(dev, &smb358_charger_attrs[i]);
1669                 if (rc)
1670                         goto create_attrs_failed;
1671         }
1672         goto create_attrs_succeed;
1673
1674 create_attrs_failed:
1675         dev_err(dev, "%s: failed (%d)\n", __func__, rc);
1676         while (i--)
1677                 device_remove_file(dev, &smb358_charger_attrs[i]);
1678 create_attrs_succeed:
1679         return rc;
1680 }
1681
1682 ssize_t smb358_chg_show_attrs(struct device *dev,
1683                                 struct device_attribute *attr, char *buf)
1684 {
1685         const ptrdiff_t offset = attr - smb358_charger_attrs;
1686         int i = 0;
1687
1688         switch (offset) {
1689         case CHG_REG:
1690         case CHG_DATA:
1691         case CHG_REGS:
1692                 i = smb358_hal_chg_show_attrs(dev, offset, buf);
1693                 break;
1694         default:
1695                 i = -EINVAL;
1696                 break;
1697         }
1698
1699         return i;
1700 }
1701
1702 ssize_t smb358_chg_store_attrs(struct device *dev,
1703                                 struct device_attribute *attr,
1704                                 const char *buf, size_t count)
1705 {
1706         const ptrdiff_t offset = attr - smb358_charger_attrs;
1707         int ret = 0;
1708
1709         switch (offset) {
1710         case CHG_REG:
1711         case CHG_DATA:
1712                 ret = smb358_hal_chg_store_attrs(dev, offset, buf, count);
1713                 break;
1714         default:
1715                 ret = -EINVAL;
1716                 break;
1717         }
1718
1719         return ret;
1720 }
1721 #ifdef CONFIG_OF
1722 static int smb358_charger_read_u32_index_dt(const struct device_node *np,
1723                                        const char *propname,
1724                                        u32 index, u32 *out_value)
1725 {
1726         struct property *prop = of_find_property(np, propname, NULL);
1727         u32 len = (index + 1) * sizeof(*out_value);
1728
1729         if (!prop)
1730                 return (-EINVAL);
1731         if (!prop->value)
1732                 return (-ENODATA);
1733         if (len > prop->length)
1734                 return (-EOVERFLOW);
1735
1736         *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
1737
1738         return 0;
1739 }
1740 extern unsigned int system_rev;
1741 static int smb358_charger_parse_dt(struct device *dev,
1742         struct sec_charger_info *charger)
1743 {
1744         struct device_node *np = dev->of_node;
1745         sec_battery_platform_data_t *pdata = charger->pdata;
1746         int ret = 0;
1747         int i, len;
1748         const u32 *p;
1749
1750         if (np == NULL) {
1751                 pr_err("%s np NULL\n", __func__);
1752                 return -1;
1753         } else {
1754 #if defined(CONFIG_MACH_VIENNAVZW) || defined(CONFIG_MACH_VIENNAATT)
1755         if (system_rev >= 0xC)
1756                 pdata->vbus_ctrl_gpio = 28;
1757         else
1758                 pdata->vbus_ctrl_gpio = 0;
1759         pr_info("%s reading vbus_ctrl_gpio = %d\n",
1760                 __func__, pdata->vbus_ctrl_gpio);
1761 #else
1762                 ret = of_get_named_gpio(np, "charger,vbus_ctrl_gpio", 0);
1763                 if (ret > 0) {
1764                         pdata->vbus_ctrl_gpio = ret;
1765                         pr_info("%s reading vbus_ctrl_gpio = %d\n", __func__, ret);
1766                 } else {
1767                         pdata->vbus_ctrl_gpio = 0;
1768                         pr_info("%s vbus_ctrl_gpio read fail\n", __func__);
1769                 }
1770 #endif
1771
1772                 np = of_find_node_by_name(NULL, "sec-battery");
1773                 if (!np) {
1774                         pr_err("%s np NULL\n", __func__);
1775                 }
1776                 ret = of_property_read_u32(np, "battery,chg_float_voltage",
1777                                         &pdata->chg_float_voltage);
1778                 if (ret < 0)
1779                         pr_err("%s: chg_float_voltage read failed (%d)\n", __func__, ret);
1780
1781                 ret = of_property_read_u32(np, "battery,chg_min_system_voltage",
1782                                         &pdata->chg_min_system_voltage);
1783                 if (ret < 0) {
1784                         pr_err("%s: chg_min_system_voltage read failed (%d)\n", __func__, ret);
1785                         pdata->chg_min_system_voltage = 3750;
1786                         pr_err("%s: chg_min_system_voltage is used as default setting (%d)\n",
1787                                         __func__, pdata->chg_min_system_voltage);
1788                 }
1789
1790                 ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
1791                                         &pdata->ovp_uvlo_check_type);
1792                 if (ret < 0)
1793                         pr_err("%s: ovp_uvlo_check_type read failed (%d)\n", __func__, ret);
1794
1795                 ret = of_get_named_gpio(np, "battery,chg_int", 0);
1796                 if (ret > 0) {
1797                         pdata->chg_irq = gpio_to_irq(ret);
1798                         pr_info("%s reading chg_int_gpio = %d\n", __func__, ret);
1799                 } else {
1800                         pr_info("%s reading chg_int_gpio is empty\n", __func__);
1801                 }
1802
1803                 ret = of_property_read_u32(np, "battery,chg_irq_attr",
1804                                         (unsigned int *)&pdata->chg_irq_attr);
1805                 if (ret)
1806                         pr_info("%s: chg_irq_attr is Empty\n", __func__);
1807
1808                 ret = of_property_read_u32(np, "battery,full_check_type",
1809                                         &pdata->full_check_type);
1810                 if (ret < 0)
1811                         pr_err("%s: full_check_type read failed (%d)\n", __func__, ret);
1812
1813                 p = of_get_property(np, "battery,input_current_limit", &len);
1814                 len = len / sizeof(u32);
1815                 pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
1816                                                   GFP_KERNEL);
1817
1818                 for(i = 0; i < len; i++) {
1819                         ret = smb358_charger_read_u32_index_dt(np,
1820                                          "battery,input_current_limit", i,
1821                                          &pdata->charging_current[i].input_current_limit);
1822                         ret = smb358_charger_read_u32_index_dt(np,
1823                                          "battery,fast_charging_current", i,
1824                                          &pdata->charging_current[i].fast_charging_current);
1825                         ret = smb358_charger_read_u32_index_dt(np,
1826                                          "battery,full_check_current_1st", i,
1827                                          &pdata->charging_current[i].full_check_current_1st);
1828                         ret = smb358_charger_read_u32_index_dt(np,
1829                                          "battery,full_check_current_2nd", i,
1830                                          &pdata->charging_current[i].full_check_current_2nd);
1831                 }
1832         }
1833         return ret;
1834 }
1835 #else
1836 static int smb358_charger_parse_dt(struct max77803_charger_data *charger)
1837 {
1838         return 0;
1839 }
1840 #endif
1841
1842 static int smb358_charger_probe(
1843                                                 struct i2c_client *client,
1844                                                 const struct i2c_device_id *id)
1845 {
1846         struct i2c_adapter *adapter =
1847                 to_i2c_adapter(client->dev.parent);
1848         struct sec_charger_info *charger;
1849         int ret = 0;
1850
1851         dev_info(&client->dev,
1852                 "%s: SMB358 Charger Driver Loading\n", __func__);
1853
1854         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1855                 return -EIO;
1856
1857         charger = kzalloc(sizeof(*charger), GFP_KERNEL);
1858         if (!charger)
1859                 return -ENOMEM;
1860
1861         charger->client = client;
1862         if (client->dev.of_node) {
1863                 void * pdata = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
1864                 if (!pdata)
1865                         goto err_free1;
1866                 charger->pdata = pdata;
1867                 if (smb358_charger_parse_dt(&client->dev, charger))
1868                         dev_err(&client->dev,
1869                                 "%s : Failed to get charger dt\n", __func__);
1870         } else
1871                 charger->pdata = client->dev.platform_data;
1872
1873         i2c_set_clientdata(client, charger);
1874
1875         charger->siop_level = 100;
1876         charger->psy_chg.name           = "sec-charger";
1877         charger->psy_chg.type           = POWER_SUPPLY_TYPE_UNKNOWN;
1878         charger->psy_chg.get_property   = smb358_chg_get_property;
1879         charger->psy_chg.set_property   = smb358_chg_set_property;
1880         charger->psy_chg.properties     = smb358_charger_props;
1881         charger->psy_chg.num_properties = ARRAY_SIZE(smb358_charger_props);
1882         charger->is_slow_charging = false;
1883
1884         if (charger->pdata->chg_gpio_init) {
1885                 if (!charger->pdata->chg_gpio_init()) {
1886                         dev_err(&client->dev,
1887                                         "%s: Failed to Initialize GPIO\n", __func__);
1888                         goto err_free;
1889                 }
1890         }
1891
1892         if (!smb358_hal_chg_init(charger->client)) {
1893                 dev_err(&client->dev,
1894                         "%s: Failed to Initialize Charger\n", __func__);
1895                 goto err_free;
1896         }
1897
1898         ret = power_supply_register(&client->dev, &charger->psy_chg);
1899         if (ret) {
1900                 dev_err(&client->dev,
1901                         "%s: Failed to Register psy_chg\n", __func__);
1902                 goto err_free;
1903         }
1904
1905         INIT_DELAYED_WORK(&charger->slow_work,
1906                 smb358_check_slow_charging);
1907
1908         if (charger->pdata->chg_irq) {
1909                 INIT_DELAYED_WORK(
1910                         &charger->isr_work, smb358_chg_isr_work);
1911
1912                 ret = request_threaded_irq(charger->pdata->chg_irq,
1913                                 NULL, smb358_chg_irq_thread,
1914                                 charger->pdata->chg_irq_attr,
1915                                 "charger-irq", charger);
1916                 if (ret) {
1917                         dev_err(&client->dev,
1918                                 "%s: Failed to Reqeust IRQ\n", __func__);
1919                         goto err_supply_unreg;
1920                 }
1921
1922                 ret = enable_irq_wake(charger->pdata->chg_irq);
1923                 if (ret < 0)
1924                         dev_err(&client->dev,
1925                                 "%s: Failed to Enable Wakeup Source(%d)\n",
1926                                 __func__, ret);
1927         }
1928
1929         ret = smb358_chg_create_attrs(charger->psy_chg.dev);
1930         if (ret) {
1931                 dev_err(&client->dev,
1932                         "%s : Failed to create_attrs\n", __func__);
1933                 goto err_req_irq;
1934         }
1935
1936         dev_info(&client->dev,
1937                 "%s: SMB358 Charger Driver Loaded\n", __func__);
1938         return 0;
1939
1940 err_req_irq:
1941         if (charger->pdata->chg_irq)
1942                 free_irq(charger->pdata->chg_irq, charger);
1943 err_supply_unreg:
1944         power_supply_unregister(&charger->psy_chg);
1945 err_free:
1946         kfree(charger->pdata);
1947 err_free1:
1948         kfree(charger);
1949
1950         return ret;
1951 }
1952
1953 static int smb358_charger_remove(
1954                                                 struct i2c_client *client)
1955 {
1956         return 0;
1957 }
1958
1959 static int smb358_charger_suspend(struct i2c_client *client,
1960                                 pm_message_t state)
1961 {
1962         if (!smb358_hal_chg_suspend(client))
1963                 dev_err(&client->dev,
1964                         "%s: Failed to Suspend Charger\n", __func__);
1965
1966         return 0;
1967 }
1968
1969 static int smb358_charger_resume(struct i2c_client *client)
1970 {
1971         dev_info(&client->dev,"%s: start\n", __func__);
1972
1973         if (!smb358_hal_chg_resume(client))
1974                 dev_err(&client->dev,
1975                         "%s: Failed to Resume Charger\n", __func__);
1976
1977         return 0;
1978 }
1979
1980 static const struct i2c_device_id smb358_charger_id[] = {
1981         {"smb358", 0},
1982         {}
1983 };
1984
1985 MODULE_DEVICE_TABLE(i2c, smb358_charger_id);
1986 static struct of_device_id smb358_i2c_match_table[] = {
1987         { .compatible = "smb358,i2c", },
1988         { },
1989 };
1990 MODULE_DEVICE_TABLE(i2c, smb358_i2c_match_table);
1991
1992 static struct i2c_driver smb358_charger_driver = {
1993         .driver = {
1994                 .name   = "smb358",
1995                 .owner = THIS_MODULE,
1996                 .of_match_table = smb358_i2c_match_table,
1997         },
1998         .probe  = smb358_charger_probe,
1999         .remove = smb358_charger_remove,
2000         .suspend        = smb358_charger_suspend,
2001         .resume         = smb358_charger_resume,
2002         .shutdown       = smb358_charger_shutdown,
2003         .id_table       = smb358_charger_id,
2004 };
2005
2006 static int __init smb358_charger_init(void)
2007 {
2008         return i2c_add_driver(&smb358_charger_driver);
2009 }
2010
2011 static void __exit smb358_charger_exit(void)
2012 {
2013         i2c_del_driver(&smb358_charger_driver);
2014 }
2015
2016 module_init(smb358_charger_init);
2017 module_exit(smb358_charger_exit);
2018
2019 MODULE_DESCRIPTION("Samsung SMB358 Charger Driver");
2020 MODULE_AUTHOR("Samsung Electronics");
2021 MODULE_LICENSE("GPL");
2022