drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / battery / smb328_charger.c
1 /*
2  *  smb328_charger.c
3  *  Samsung SMB328 Charger Driver
4  *
5  *  Copyright (C) 2013 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 #include <linux/battery/sec_charger.h>
14 #include <linux/battery/charger/smb328_charger.h>
15 #include <linux/debugfs.h>
16 #include <linux/seq_file.h>
17 #include <linux/interrupt.h>
18
19 #include <linux/gpio-pxa.h>
20 #include <linux/of_device.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_irq.h>
23
24 #if defined(SIOP_CHARGING_CURRENT_LIMIT_FEATURE)
25 #define SIOP_CHARGING_LIMIT_CURRENT 800
26 static bool is_siop_limited;
27 #endif
28
29 static enum power_supply_property sec_charger_props[] = {
30         POWER_SUPPLY_PROP_STATUS,
31         POWER_SUPPLY_PROP_CHARGE_TYPE,
32         POWER_SUPPLY_PROP_HEALTH,
33         POWER_SUPPLY_PROP_ONLINE,
34         POWER_SUPPLY_PROP_CURRENT_MAX,
35         POWER_SUPPLY_PROP_CURRENT_AVG,
36         POWER_SUPPLY_PROP_CURRENT_NOW,
37         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
38 };
39
40 static int smb328_i2c_write(struct i2c_client *client,
41                                 int reg, u8 *buf)
42 {
43         int ret;
44         ret = i2c_smbus_write_i2c_block_data(client, reg, 1, buf);
45         if (ret < 0)
46                 pr_err("%s: Error(%d) : 0x%x\n", __func__, ret, reg);
47
48         return ret;
49 }
50
51 static int smb328_i2c_read(struct i2c_client *client,
52                                 int reg, u8 *buf)
53 {
54         int ret;
55         ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf);
56         if (ret < 0)
57                 pr_err("%s: Error(%d) : 0x%x\n", __func__, ret, reg);
58
59         return ret;
60 }
61
62 static void smb328_i2c_write_array(struct i2c_client *client,
63                                 u8 *buf, int size)
64 {
65         int i;
66         for (i = 0; i < size; i += 3)
67                 smb328_i2c_write(client, (u8) (*(buf + i)), (buf + i) + 1);
68 }
69
70 static void smb328_set_command(struct i2c_client *client,
71                                 int reg, int datum)
72 {
73         int val;
74         u8 data = 0;
75         val = smb328_i2c_read(client, reg, &data);
76         if (val >= 0) {
77                 pr_debug("%s : reg(0x%02x): 0x%02x", __func__, reg, data);
78                 if (data != datum) {
79                         data = datum;
80                         if (smb328_i2c_write(client, reg, &data) < 0)
81                                 pr_err("%s : error!\n", __func__);
82                         val = smb328_i2c_read(client, reg, &data);
83                         if (val >= 0)
84                                 pr_debug(" => 0x%02x\n", data);
85                 }
86         }
87 }
88
89 static void smb328_test_read(struct i2c_client *client)
90 {
91         u8 data = 0;
92         u32 addr = 0;
93         for (addr = 0; addr < 0x0c; addr++) {
94                 smb328_i2c_read(client, addr, &data);
95                 pr_info("smb328 addr : 0x%02x data : 0x%02x\n", addr, data);
96         }
97         for (addr = 0x31; addr < 0x3D; addr++) {
98                 smb328_i2c_read(client, addr, &data);
99                 pr_info("smb328 addr : 0x%02x data : 0x%02x\n", addr, data);
100         }
101 }
102
103 static void smb328_read_regs(struct i2c_client *client, char *str)
104 {
105         u8 data = 0;
106         u32 addr = 0;
107
108         for (addr = 0x0; addr < 0x0A; addr++) {
109                 smb328_i2c_read(client, addr, &data);
110                 sprintf(str+strlen(str), "0x%x, ", data);
111         }
112
113         /* "#" considered as new line in application */
114         sprintf(str+strlen(str), "#");
115
116         for (addr = 0x31; addr < 0x39; addr++) {
117                 smb328_i2c_read(client, addr, &data);
118                 sprintf(str+strlen(str), "0x%x, ", data);
119         }
120 }
121
122 static int smb328_get_charging_status(struct i2c_client *client)
123 {
124         int status = POWER_SUPPLY_STATUS_UNKNOWN;
125         u8 stat_c = 0;
126
127         smb328_i2c_read(client, SMB328_BATTERY_CHARGING_STATUS_C, &stat_c);
128         pr_debug("%s : Charging status C(0x%02x)\n", __func__, stat_c);
129
130         /* At least one charge cycle terminated,
131          * Charge current < Termination Current
132          */
133         if (stat_c & 0xc0) {
134                 /* top-off by full charging */
135                 status = POWER_SUPPLY_STATUS_FULL;
136                 return status;
137         }
138
139         /* Is enabled ? */
140         if (stat_c & 0x01) {
141                 /* check for 0x30 : 'safety timer' (0b01 or 0b10) or
142                  * 'waiting to begin charging' (0b11)
143                  * check for 0x06 : no charging (0b00)
144                  */
145                 /* not charging */
146                 if ((stat_c & 0x30) || !(stat_c & 0x06))
147                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
148                 else
149                         status = POWER_SUPPLY_STATUS_CHARGING;
150         } else {
151                 status = POWER_SUPPLY_STATUS_DISCHARGING;
152         }
153
154         return (int)status;
155 }
156
157 static int smb328_get_battery_present(struct i2c_client *client)
158 {
159         u8 reg_data, irq_data;
160
161         smb328_i2c_read(client, SMB328_BATTERY_CHARGING_STATUS_B, &reg_data);
162         smb328_i2c_read(client, SMB328_INTERRUPT_STATUS_C, &irq_data);
163
164         reg_data = ((reg_data & 0x01) && (irq_data & 0x10));
165
166         return !reg_data;
167 }
168
169 static void smb328_set_writable(struct i2c_client *client, int writable)
170 {
171         u8 reg_data;
172
173         smb328_i2c_read(client, SMB328_COMMAND, &reg_data);
174
175         if (writable)
176                 reg_data |= CMD_A_ALLOW_WRITE;
177         else
178                 reg_data &= ~CMD_A_ALLOW_WRITE;
179
180         smb328_i2c_write(client, SMB328_COMMAND, &reg_data);
181 }
182
183 static u8 smb328_set_charge_enable(struct i2c_client *client, int enable)
184 {
185         u8 chg_en;
186         u8 reg_data;
187
188         reg_data = 0x00;
189
190         smb328_i2c_write(client, SMB328_FUNCTION_CONTROL_C, &reg_data);
191
192         smb328_i2c_read(client, SMB328_COMMAND, &chg_en);
193         if (!enable)
194                 chg_en |= CMD_CHARGE_EN;
195         else
196                 chg_en &= ~CMD_CHARGE_EN;
197
198         smb328_i2c_write(client, SMB328_COMMAND, &chg_en);
199
200         return chg_en;
201 }
202
203 static u8 smb328_set_float_voltage(struct i2c_client *client, int float_voltage)
204 {
205         u8 reg_data, float_data;
206
207         float_voltage = 4350;
208
209         if (float_voltage < 3460)
210                 float_data = 0;
211         else if (float_voltage <= 4340)
212                 float_data = (float_voltage - 3500) / 20 + 2;
213         else if ((float_voltage == 4350) || (float_voltage == 4360))
214                 float_data = 0x2D; /* (4340 -3500)/20 + 1 */
215         else
216                 float_data = 0x3F;
217
218         smb328_i2c_read(client, SMB328_FLOAT_VOLTAGE, &reg_data);
219         reg_data &= ~CFG_FLOAT_VOLTAGE_MASK;
220         reg_data |= float_data << CFG_FLOAT_VOLTAGE_SHIFT;
221
222         smb328_i2c_write(client, SMB328_FLOAT_VOLTAGE, &reg_data);
223
224         return reg_data;
225 }
226
227 static u8 smb328_set_input_current_limit(struct i2c_client *client,
228                                 int input_current)
229 {
230         u8 curr_data, reg_data;
231
232         curr_data = input_current < 500 ? 0x0 :
233                 input_current > 1200 ? 0x7 :
234                 (input_current - 500) / 100;
235
236         smb328_i2c_read(client, SMB328_INPUT_CURRENTLIMIT, &reg_data);
237         reg_data &= ~CFG_INPUT_CURRENT_MASK;
238         reg_data |= curr_data << CFG_INPUT_CURRENT_SHIFT;
239
240         smb328_i2c_write(client, SMB328_INPUT_CURRENTLIMIT, &reg_data);
241
242         pr_info("%s: set current limit : 0x%x\n", __func__, reg_data);
243
244         return reg_data;
245 }
246
247 static u8 smb328_set_termination_current_limit(struct i2c_client *client,
248                                 int termination_current)
249 {
250         u8 reg_data, term_data;
251
252         term_data = termination_current < 25 ? 0x0 :
253                 termination_current > 200 ? 0x7 :
254                 (termination_current - 25) / 25;
255
256         /* Charge completion termination current */
257         smb328_i2c_read(client, SMB328_CHARGE_CURRENT, &reg_data);
258         reg_data &= ~CFG_TERMINATION_CURRENT_MASK;
259         reg_data |= term_data << CFG_TERMINATION_CURRENT_SHIFT;
260
261         smb328_i2c_write(client, SMB328_CHARGE_CURRENT, &reg_data);
262
263         /* set STAT assertion termination current */
264         smb328_i2c_read(client, SMB328_VARIOUS_FUNCTIONS, &reg_data);
265         reg_data &= ~CFG_STAT_ASSETION_TERM_MASK;
266         reg_data |= term_data << CFG_STAT_ASSETION_TERM_SHIFT;
267
268         smb328_i2c_write(client, SMB328_VARIOUS_FUNCTIONS, &reg_data);
269
270         return reg_data;
271 }
272
273 static u8 smb328_set_fast_charging_current(struct i2c_client *client,
274                                 int fast_charging_current)
275 {
276         u8 reg_data, chg_data;
277
278         chg_data = fast_charging_current < 500 ? 0x0 :
279                 fast_charging_current > 1200 ? 0x7 :
280                 (fast_charging_current - 500) / 100;
281
282         smb328_i2c_read(client, SMB328_CHARGE_CURRENT, &reg_data);
283         reg_data &= ~CFG_CHARGE_CURRENT_MASK;
284         reg_data |= chg_data << CFG_CHARGE_CURRENT_SHIFT;
285
286         smb328_i2c_write(client, SMB328_CHARGE_CURRENT, &reg_data);
287
288         pr_info("%s: Charge Current : 0x%x\n", __func__, reg_data);
289
290         return reg_data;
291 }
292
293 static void smb328_charger_function_control(struct sec_charger_info *charger)
294 {
295         u8 reg_data, charge_mode;
296
297         smb328_set_writable(charger->client, 1);
298         reg_data = 0x6E;
299         smb328_i2c_write(charger->client, SMB328_FUNCTION_CONTROL_B, &reg_data);
300
301         smb328_i2c_read(charger->client,
302                         SMB328_INTERRUPT_SIGNAL_SELECTION, &reg_data);
303         reg_data |= 0x1;
304         smb328_i2c_write(charger->client,
305                         SMB328_INTERRUPT_SIGNAL_SELECTION, &reg_data);
306
307         if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY) {
308                 /* turn off charger */
309                 smb328_set_charge_enable(charger->client, 0);
310         } else {
311                 pr_info("%s: Input : %d, Charge : %d\n", __func__,
312                         charger->pdata->charging_current[charger->cable_type].input_current_limit,
313                         charger->pdata->charging_current[charger->cable_type].fast_charging_current);
314                 /* AICL enable */
315                 smb328_i2c_read(charger->client,
316                                 SMB328_INPUT_CURRENTLIMIT, &reg_data);
317                 reg_data &= ~CFG_AICL_ENABLE;
318                 reg_data &= ~CFG_AICL_VOLTAGE; /* AICL enable voltage 4.25V */
319                 smb328_i2c_write(charger->client,
320                                 SMB328_INPUT_CURRENTLIMIT, &reg_data);
321
322                 /* Function control A */
323                 reg_data = 0xDA;
324                 smb328_i2c_write(charger->client,
325                                 SMB328_FUNCTION_CONTROL_A, &reg_data);
326
327                 /* 4.2V float voltage */
328                 smb328_set_float_voltage(charger->client,
329                         charger->pdata->chg_float_voltage);
330
331                 /* Set termination current */
332                 smb328_set_termination_current_limit(charger->client,
333                         charger->pdata->charging_current[charger->
334                         cable_type].full_check_current_1st);
335
336                 smb328_set_input_current_limit(charger->client,
337                         charger->pdata->charging_current
338                         [charger->cable_type].input_current_limit);
339
340                 smb328_set_fast_charging_current(charger->client,
341                         charger->pdata->charging_current
342                         [charger->cable_type].fast_charging_current);
343
344                 /* SET USB5/1, AC/USB Mode */
345                 charge_mode = (charger->cable_type == POWER_SUPPLY_TYPE_MAINS) ||
346                                         (charger->cable_type == POWER_SUPPLY_TYPE_UARTOFF) ?
347                                         0x3 : 0x2;
348                 smb328_i2c_read(charger->client, SMB328_COMMAND, &reg_data);
349                 reg_data &= ~0x0C;
350                 reg_data |= charge_mode << 2;
351                 smb328_i2c_write(charger->client, SMB328_COMMAND, &reg_data);
352
353                 smb328_set_charge_enable(charger->client, 1);
354         }
355         smb328_test_read(charger->client);
356         smb328_set_writable(charger->client, 0);
357 }
358
359 static int smb328_get_charging_health(struct i2c_client *client)
360 {
361         struct sec_charger_info *charger = i2c_get_clientdata(client);
362         int health = POWER_SUPPLY_HEALTH_GOOD;
363         u8 status_reg, data;
364
365         smb328_i2c_read(client, SMB328_INTERRUPT_STATUS_C, &status_reg);
366         pr_info("%s : Interrupt status C(0x%02x)\n", __func__, status_reg);
367
368         /* Is enabled ? */
369         if (status_reg & 0x2) {
370                 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
371         } else if (status_reg & 0x4) {
372                 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
373         } else if (status_reg & 0x80) { /* watchdog enable workaround */
374                 /* clear IRQ */
375                 data = 0xFF;
376                 smb328_i2c_write(client, 0x30, &data);
377                 /* reset function control */
378                 smb328_charger_function_control(charger);
379         }
380
381         return (int)health;
382 }
383
384 static int smb328_get_charge_now(struct i2c_client *client)
385 {
386         u8 chg_now, data, addr;
387
388         smb328_i2c_read(client, SMB328_BATTERY_CHARGING_STATUS_A, &chg_now);
389
390         for (addr = 0; addr < 0x0c; addr++) {
391                 smb328_i2c_read(client, addr, &data);
392                 pr_debug("smb328 addr : 0x%02x data : 0x%02x\n", addr, data);
393         }
394         for (addr = 0x31; addr <= 0x3D; addr++) {
395                 smb328_i2c_read(client, addr, &data);
396                 pr_debug("smb328 addr : 0x%02x data : 0x%02x\n", addr, data);
397         }
398
399         /* Clear IRQ */
400         data = 0xFF;
401         smb328_i2c_write(client, 0x30, &data);
402
403         return (chg_now & 0x2);
404 }
405
406 static void smb328_charger_otg_control(struct sec_charger_info *charger,
407                         int enable)
408 {
409         u8 reg_data;
410
411         smb328_i2c_read(charger->client, SMB328_OTG_POWER_AND_LDO, &reg_data);
412
413         if (!enable)
414                 reg_data |= CFG_OTG_ENABLE;
415         else
416                 reg_data &= ~CFG_OTG_ENABLE;
417
418         smb328_set_writable(charger->client, 1);
419         smb328_i2c_write(charger->client, SMB328_OTG_POWER_AND_LDO, &reg_data);
420         smb328_set_writable(charger->client, 0);
421 }
422
423 static void smb328_irq_enable(struct i2c_client *client)
424 {
425         u8 data;
426
427         smb328_set_writable(client, 1);
428         data = 0x6D;
429         smb328_i2c_write(client, 0x04, &data);
430
431         smb328_i2c_read(client,
432                         SMB328_INTERRUPT_SIGNAL_SELECTION, &data);
433         data |= 0x1;
434         smb328_i2c_write(client,
435                         SMB328_INTERRUPT_SIGNAL_SELECTION, &data);
436
437         smb328_set_writable(client, 0);
438 }
439
440 static void smb328_irq_disable(struct i2c_client *client)
441 {
442         u8 data;
443
444         smb328_set_writable(client, 1);
445         data = 0x6E;
446         smb328_i2c_write(client, 0x04, &data);
447
448         smb328_i2c_read(client,
449                         SMB328_INTERRUPT_SIGNAL_SELECTION, &data);
450         data &= 0xfe;
451         smb328_i2c_write(client,
452                         SMB328_INTERRUPT_SIGNAL_SELECTION, &data);
453
454         smb328_set_writable(client, 0);
455 }
456
457 static int smb328_debugfs_show(struct seq_file *s, void *data)
458 {
459         struct sec_charger_info *charger = s->private;
460         u8 reg;
461         u8 reg_data;
462
463         seq_printf(s, "SMB CHARGER IC :\n");
464         seq_printf(s, "==================\n");
465         for (reg = 0x00; reg <= 0x0A; reg++) {
466                 smb328_i2c_read(charger->client, reg, &reg_data);
467                 seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
468         }
469
470         for (reg = 0x30; reg <= 0x39; reg++) {
471                 smb328_i2c_read(charger->client, reg, &reg_data);
472                 seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
473         }
474
475         seq_printf(s, "\n");
476         return 0;
477 }
478
479 static int smb328_debugfs_open(struct inode *inode, struct file *file)
480 {
481         return single_open(file, smb328_debugfs_show, inode->i_private);
482 }
483
484 static const struct file_operations smb328_debugfs_fops = {
485         .open           = smb328_debugfs_open,
486         .read           = seq_read,
487         .llseek         = seq_lseek,
488         .release        = single_release,
489 };
490
491 static bool smb328_chg_init(struct sec_charger_info *charger)
492 {
493         smb328_irq_disable(charger->client);
494
495         (void) debugfs_create_file("smb328-regs",
496                 S_IRUGO, NULL, (void *)charger, &smb328_debugfs_fops);
497
498         return true;
499 }
500
501 static int sec_chg_get_property(struct power_supply *psy,
502                               enum power_supply_property psp,
503                               union power_supply_propval *val)
504 {
505         struct sec_charger_info *charger =
506                 container_of(psy, struct sec_charger_info, psy_chg);
507         u8 data;
508
509         switch (psp) {
510         case POWER_SUPPLY_PROP_STATUS:
511                 val->intval = smb328_get_charging_status(charger->client);
512                 break;
513         case POWER_SUPPLY_PROP_CHARGE_TYPE:
514                 if (!charger->is_charging)
515                         val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
516                 else
517                         val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
518                 break;
519         case POWER_SUPPLY_PROP_HEALTH:
520                 val->intval = smb328_get_charging_health(charger->client);
521                 break;
522         case POWER_SUPPLY_PROP_PRESENT:
523                 val->intval = smb328_get_battery_present(charger->client);
524                 break;
525         case POWER_SUPPLY_PROP_CURRENT_NOW:
526                 smb328_irq_disable(charger->client);
527                 if (charger->charging_current) {
528                         smb328_i2c_read(charger->client, SMB328_CHARGE_CURRENT, &data);
529                         switch (data >> 5) {
530                         case 0:
531                                 val->intval = 450;
532                                 break;
533                         case 1:
534                                 val->intval = 600;
535                                 break;
536                         case 2:
537                                 val->intval = 700;
538                                 break;
539                         case 3:
540                                 val->intval = 800;
541                                 break;
542                         case 4:
543                                 val->intval = 900;
544                                 break;
545                         case 5:
546                                 val->intval = 1000;
547                                 break;
548                         case 6:
549                                 val->intval = 1100;
550                                 break;
551                         case 7:
552                                 val->intval = 1200;
553                                 break;
554                         }
555                 } else
556                         val->intval = 0;
557                 break;
558         case POWER_SUPPLY_PROP_CHARGE_NOW:
559                 val->intval = smb328_get_charge_now(charger->client);
560                 break;
561 #if defined(CONFIG_FUELGAUGE_88PM822)
562         case POWER_SUPPLY_PROP_POWER_STATUS:
563                 val->intval = smb328_get_power_status(charger);
564                 break;
565 #endif          
566         default:
567                 return false;
568         }
569         return true;
570 }
571
572 static int sec_chg_set_property(struct power_supply *psy,
573                               enum power_supply_property psp,
574                               const union power_supply_propval *val)
575 {
576         struct sec_charger_info *charger =
577                 container_of(psy, struct sec_charger_info, psy_chg);
578         int cable_type, current_now;
579
580         switch (psp) {
581         /* val->intval : charging current */
582         case POWER_SUPPLY_PROP_CURRENT_NOW:
583                 smb328_set_writable(charger->client, 1);
584                 current_now =
585                         charger->charging_current * val->intval / 100;
586                 smb328_set_fast_charging_current(charger->client, current_now);
587                 smb328_set_writable(charger->client, 0);
588                 break;
589         /* val->intval : type */
590         case POWER_SUPPLY_PROP_ONLINE:
591                 if (charger->charging_current < 0)
592                         smb328_charger_otg_control(charger, 1);
593                 else if (charger->charging_current > 0) {
594                         smb328_charger_function_control(charger);
595                         smb328_charger_otg_control(charger, 1);
596                  } else {
597                         smb328_charger_function_control(charger);
598                         smb328_charger_otg_control(charger, 0);
599                 }
600                 break;
601         default:
602                 return false;
603         }
604         return true;
605 }
606
607 #ifdef CONFIG_OF
608 static int smb328_charger_parse_dt(struct device *dev,
609                 sec_battery_platform_data_t *pdata)
610 {
611         struct device_node *np = dev->of_node;
612         int ret = 0;
613         int i, len;
614         const u32 *p;
615
616         if (np == NULL) {
617                 pr_err("%s np NULL\n", __func__);
618         } else {
619                 ret = of_property_read_u32(np, "chg_float_voltage",
620                                            &pdata->chg_float_voltage);
621         }
622
623         np = of_find_node_by_name(NULL, "sec-battery");
624         if (!np) {
625                 pr_err("%s np NULL\n", __func__);
626         } else {
627                 p = of_get_property(np, "battery,input_current_limit", &len);
628                 if (!p)
629                         return 1;
630
631                 len = len / sizeof(u32);
632
633                 pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
634                                                   GFP_KERNEL);
635
636                 for(i = 0; i < len; i++) {
637                         ret = of_property_read_u32_index(np,
638                                  "battery,input_current_limit", i,
639                                  &pdata->charging_current[i].input_current_limit);
640                         ret = of_property_read_u32_index(np,
641                                  "battery,fast_charging_current", i,
642                                  &pdata->charging_current[i].fast_charging_current);
643                         ret = of_property_read_u32_index(np,
644                                  "battery,full_check_current_1st", i,
645                                  &pdata->charging_current[i].full_check_current_1st);
646                         ret = of_property_read_u32_index(np,
647                                  "battery,full_check_current_2nd", i,
648                                  &pdata->charging_current[i].full_check_current_2nd);
649                 }
650         }
651
652         return ret;
653 }
654
655 static struct of_device_id sec_charger_dt_ids[] = {
656         { .compatible = "QC,smb328", },
657         {}
658 };
659 MODULE_DEVICE_TABLE(of, sec_charger_dt_ids);
660
661 #else
662 static int max77843_charger_parse_dt(struct max77843_charger_data *charger)
663 {
664         return -ENOSYS;
665 }
666
667 #define rt5033_charger_match_table NULL
668 #endif 
669
670
671 static int smb328_charger_probe(struct i2c_client *client,
672                 const struct i2c_device_id *id)
673 {
674         sec_battery_platform_data_t *pdata;
675         struct sec_charger_info *charger;
676         int ret = 0;
677
678         dev_info(&client->dev,
679                         "%s: SEC Charger Driver Loading\n", __func__);
680
681         if (IS_ENABLED(CONFIG_OF)) {
682                 if (!pdata)
683                         pdata = kzalloc(sizeof(sec_battery_platform_data_t),
684                                         GFP_KERNEL);
685                 if (!pdata)
686                         return -ENOMEM;
687
688                 ret = smb328_charger_parse_dt(&client->dev, pdata);
689                 if (ret)
690                         return ret;
691         } else {
692                 pdata = client->dev.platform_data;
693         }
694
695         ret = i2c_check_functionality(client->adapter,
696                                 I2C_FUNC_SMBUS_BYTE_DATA |
697                                 I2C_FUNC_SMBUS_I2C_BLOCK);
698         if (ret < 0)
699                 return -EIO;
700
701         charger = kzalloc(sizeof(*charger), GFP_KERNEL);
702         if (!charger)
703                 return -ENOMEM;
704
705         charger->client = client;
706         charger->pdata = pdata;
707
708         i2c_set_clientdata(client, charger);
709
710         charger->psy_chg.name           = "sec-charger";
711         charger->psy_chg.type           = POWER_SUPPLY_TYPE_UNKNOWN;
712         charger->psy_chg.get_property   = sec_chg_get_property;
713         charger->psy_chg.set_property   = sec_chg_set_property;
714         charger->psy_chg.properties     = sec_charger_props;
715         charger->psy_chg.num_properties = ARRAY_SIZE(sec_charger_props);
716
717         if (charger->pdata->chg_gpio_init) {
718                 if (!charger->pdata->chg_gpio_init()) {
719                         dev_err(&client->dev,
720                                         "%s: Failed to Initialize GPIO\n", __func__);
721                         goto err_free;
722                 }
723         }
724
725         pr_info("%s: Clinet Address : 0x%x, Add : 0x%x\n", __func__, charger->client->addr, &charger->client->addr);
726         if (!smb328_chg_init(charger)) {
727                 dev_err(&client->dev,
728                                 "%s: Failed to Initialize Charger\n", __func__);
729                 goto err_free;
730         }
731
732         ret = power_supply_register(&client->dev, &charger->psy_chg);
733         if (ret) {
734                 dev_err(&client->dev,
735                                 "%s: Failed to Register psy_chg\n", __func__);
736                 goto err_free;
737         }
738
739         dev_dbg(&client->dev,
740                         "%s: SEC Charger Driver Loaded\n", __func__);
741         return 0;
742
743 err_supply_unreg:
744         power_supply_unregister(&charger->psy_chg);
745 err_free:
746         kfree(charger);
747         kfree(pdata);
748
749         return ret;
750 }
751
752 static int smb328_charger_remove(
753                 struct i2c_client *client)
754 {
755         return 0;
756 }
757
758 static int smb328_charger_suspend(struct i2c_client *client,
759                 pm_message_t state)
760 {
761         return 0;
762 }
763
764 static int smb328_charger_resume(struct i2c_client *client)
765 {
766         return 0;
767 }
768
769 static void smb328_charger_shutdown(struct i2c_client *client)
770 {
771 }
772
773 static const struct i2c_device_id sec_charger_id[] = {
774         {"smb328-charger", 0},
775         {}
776 };
777
778 MODULE_DEVICE_TABLE(i2c, sec_charger_id);
779
780 static struct i2c_driver sec_charger_driver = {
781         .driver = {
782                 .name   = "smb328-charger",
783                 .of_match_table = sec_charger_dt_ids,
784         },
785         .probe          = smb328_charger_probe,
786         .remove         = smb328_charger_remove,
787         .suspend        = smb328_charger_suspend,
788         .resume         = smb328_charger_resume,
789         .shutdown       = smb328_charger_shutdown,
790         .id_table       = sec_charger_id,
791 };
792
793 static int __init smb328_charger_init(void)
794 {
795         return i2c_add_driver(&sec_charger_driver);
796 }
797
798 static void __exit smb328_charger_exit(void)
799 {
800         i2c_del_driver(&sec_charger_driver);
801 }
802
803 module_init(smb328_charger_init);
804 module_exit(smb328_charger_exit);
805
806 MODULE_DESCRIPTION("SMB328 Charger Driver");
807 MODULE_AUTHOR("Samsung Electronics");
808 MODULE_LICENSE("GPL");