1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
/*
* sabresd_battery.c - Maxim 8903 USB/Adapter Charger Driver
*
* Copyright (C) 2011 Samsung Electronics
* Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
* Based on max8903_charger.c
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/power_supply.h>
#include <linux/platform_device.h>
#include <linux/power/sabresd_battery.h>
#include <linux/sort.h>
#define BATTERY_UPDATE_INTERVAL 120 /*seconds*/
#define LOW_VOLT_THRESHOLD 2800000
#define HIGH_VOLT_THRESHOLD 4200000
#define ADC_SAMPLE_COUNT 6
struct max8903_data {
struct max8903_pdata *pdata;
struct device *dev;
struct power_supply psy;
struct power_supply usb;
bool fault;
bool usb_in;
bool ta_in;
bool chg_state;
struct delayed_work work;
unsigned int interval;
unsigned short thermal_raw;
int voltage_uV;
int current_uA;
int battery_status;
int charger_online;
int charger_voltage_uV;
int real_capacity;
int percent;
int old_percent;
int usb_charger_online;
struct power_supply bat;
struct power_supply detect_usb;
struct mutex work_lock;
};
typedef struct {
u32 voltage;
u32 percent;
} battery_capacity , *pbattery_capacity;
static int cpu_type_flag;
static int offset_discharger;
static int offset_charger;
static battery_capacity chargingTable[] = {
{4060, 99},
{4035, 98},
{4010, 97},
{3980, 96},
{3950, 95},
{3920, 94},
{3890, 93},
{3860, 92},
{3830, 91},
{3780, 90},
{3750, 85},
{3690, 80},
{3660, 75},
{3630, 70},
{3600, 65},
{3580, 60},
{3550, 55},
{3500, 50},
{3450, 45},
{3400, 40},
{3350, 35},
{3300, 30},
{3250, 25},
{3200, 20},
{3000, 15},
{2900, 10},
{2850, 5},
{2800, 0},
{0, 0}
};
static battery_capacity dischargingTable[] = {
{4050, 100},
{4020, 99},
{3950, 98},
{3920, 97},
{3890, 96},
{3860, 96},
{3830, 95},
{3800, 94},
{3760, 93},
{3730, 92},
{3700, 91},
{3670, 90},
{3630, 85},
{3600, 80},
{3570, 75},
{3545, 70},
{3515, 65},
{3480, 60},
{3445, 55},
{3410, 50},
{3375, 45},
{3340, 40},
{3300, 35},
{3250, 30},
{3200, 25},
{3150, 20},
{3090, 15},
{3020, 10},
{2820, 5},
{2800, 0},
{0, 0}
};
u32 calibrate_battery_capability_percent(struct max8903_data *data)
{
u8 i;
pbattery_capacity pTable;
u32 tableSize;
if (data->battery_status == POWER_SUPPLY_STATUS_DISCHARGING) {
pTable = dischargingTable;
tableSize = sizeof(dischargingTable)/sizeof(dischargingTable[0]);
} else {
pTable = chargingTable;
tableSize = sizeof(chargingTable)/sizeof(chargingTable[0]);
}
for (i = 0; i < tableSize; i++) {
if (data->voltage_uV >= pTable[i].voltage)
return pTable[i].percent;
}
return 0;
}
static enum power_supply_property max8903_charger_props[] = {
POWER_SUPPLY_PROP_ONLINE,
};
static enum power_supply_property max8903_battery_props[] = {
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_HEALTH,
};
extern u32 max11801_read_adc(void);
static void max8903_charger_update_status(struct max8903_data *data)
{
if (data->usb_in || data->ta_in) {
if (data->ta_in)
data->charger_online = 1;
if (data->usb_in)
data->usb_charger_online = 1;
} else {
data->charger_online = 0;
data->usb_charger_online = 0;
}
if (data->charger_online == 0 && data->usb_charger_online == 0) {
data->battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
} else {
if (gpio_get_value(data->pdata->chg) == 0) {
data->battery_status = POWER_SUPPLY_STATUS_CHARGING;
} else if (data->ta_in &&
gpio_get_value(data->pdata->chg) == 1) {
if (data->percent >= 99)
data->battery_status = POWER_SUPPLY_STATUS_FULL;
else
data->battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
}
else if (data->usb_in &&
gpio_get_value(data->pdata->chg) == 1) {
if (data->percent >= 99)
data->battery_status = POWER_SUPPLY_STATUS_FULL;
else
data->battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
}
}
pr_debug("chg: %d \n", gpio_get_value(data->pdata->chg));
}
static int cmp_func(const void *_a, const void *_b)
{
const int *a = _a, *b = _b;
if (*a > *b)
return 1;
if (*a < *b)
return -1;
return 0;
}
u32 calibration_voltage(struct max8903_data *data)
{
int volt[ADC_SAMPLE_COUNT];
u32 voltage_data;
int i;
for (i = 0; i < ADC_SAMPLE_COUNT; i++) {
if (cpu_type_flag == 1) {
if (data->charger_online == 0 && data->usb_charger_online == 0) {
/* ADC offset when battery is discharger*/
volt[i] = max11801_read_adc()-offset_discharger;
} else {
volt[i] = max11801_read_adc()-offset_charger;
}
}
if (cpu_type_flag == 0) {
if (data->charger_online == 0 && data->usb_charger_online == 0) {
/* ADC offset when battery is discharger*/
volt[i] = max11801_read_adc()-offset_discharger;
} else {
volt[i] = max11801_read_adc()-offset_charger;
}
}
}
sort(volt, i, 4, cmp_func, NULL);
for (i = 0; i < ADC_SAMPLE_COUNT; i++)
pr_debug("volt_sorted[%2d]: %d\n", i, volt[i]);
/* get the average of second max/min of remained. */
voltage_data = (volt[2] + volt[ADC_SAMPLE_COUNT - 3]) / 2;
return voltage_data;
}
static void max8903_battery_update_status(struct max8903_data *data)
{
int temp;
static int temp_last;
bool changed_flag;
changed_flag = false;
mutex_lock(&data->work_lock);
temp = calibration_voltage(data);
if (temp_last == 0) {
data->voltage_uV = temp;
temp_last = temp;
}
if (data->charger_online == 0 && temp_last != 0) {
if (temp < temp_last) {
temp_last = temp;
data->voltage_uV = temp;
} else {
data->voltage_uV = temp_last;
}
}
if (data->charger_online == 1 || data->usb_charger_online == 1) {
data->voltage_uV = temp;
temp_last = temp;
}
data->percent = calibrate_battery_capability_percent(data);
if (data->percent != data->old_percent) {
data->old_percent = data->percent;
changed_flag = true;
}
if (changed_flag) {
changed_flag = false;
power_supply_changed(&data->bat);
}
mutex_unlock(&data->work_lock);
}
static int max8903_battery_get_property(struct power_supply *bat,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max8903_data *di = container_of(bat,
struct max8903_data, bat);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
if (gpio_get_value(di->pdata->chg) == 0) {
di->battery_status = POWER_SUPPLY_STATUS_CHARGING;
} else if (di->ta_in &&
gpio_get_value(di->pdata->chg) == 1) {
if (di->percent >= 99)
di->battery_status = POWER_SUPPLY_STATUS_FULL;
else
di->battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
}
else if (di->usb_in &&
gpio_get_value(di->pdata->chg) == 1) {
if (di->percent >= 99)
di->battery_status = POWER_SUPPLY_STATUS_FULL;
else
di->battery_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
}
val->intval = di->battery_status;
return 0;
default:
break;
}
switch (psp) {
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
val->intval = di->voltage_uV;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
val->intval = 0;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = HIGH_VOLT_THRESHOLD;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = LOW_VOLT_THRESHOLD;
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = 1;
break;
case POWER_SUPPLY_PROP_CAPACITY:
val->intval = di->percent < 0 ? 0 :
(di->percent > 100 ? 100 : di->percent);
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = POWER_SUPPLY_HEALTH_GOOD;
if (di->fault)
val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
break;
default:
return -EINVAL;
}
return 0;
}
static int max8903_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max8903_data *data = container_of(psy,
struct max8903_data, psy);
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
val->intval = 0;
if (data->ta_in)
val->intval = 1;
data->charger_online = val->intval;
break;
default:
return -EINVAL;
}
return 0;
}
static int max8903_get_usb_property(struct power_supply *usb,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct max8903_data *data = container_of(usb,
struct max8903_data, usb);
switch (psp) {
case POWER_SUPPLY_PROP_ONLINE:
val->intval = 0;
if (data->usb_in)
val->intval = 1;
data->usb_charger_online = val->intval;
break;
default:
return -EINVAL;
}
return 0;
}
static irqreturn_t max8903_dcin(int irq, void *_data)
{
struct max8903_data *data = _data;
struct max8903_pdata *pdata = data->pdata;
bool ta_in;
ta_in = gpio_get_value(pdata->dok) ? false : true;
if (ta_in == data->ta_in)
return IRQ_HANDLED;
data->ta_in = ta_in;
pr_info("TA(DC-IN) Charger %s.\n", ta_in ?
"Connected" : "Disconnected");
max8903_charger_update_status(data);
max8903_battery_update_status(data);
power_supply_changed(&data->psy);
power_supply_changed(&data->bat);
return IRQ_HANDLED;
}
static irqreturn_t max8903_usbin(int irq, void *_data)
{
struct max8903_data *data = _data;
struct max8903_pdata *pdata = data->pdata;
bool usb_in;
usb_in = gpio_get_value(pdata->uok) ? false : true;
if (usb_in == data->usb_in)
return IRQ_HANDLED;
data->usb_in = usb_in;
max8903_charger_update_status(data);
max8903_battery_update_status(data);
pr_info("USB Charger %s.\n", usb_in ?
"Connected" : "Disconnected");
power_supply_changed(&data->bat);
power_supply_changed(&data->usb);
return IRQ_HANDLED;
}
static irqreturn_t max8903_fault(int irq, void *_data)
{
struct max8903_data *data = _data;
struct max8903_pdata *pdata = data->pdata;
bool fault;
fault = gpio_get_value(pdata->flt) ? false : true;
if (fault == data->fault)
return IRQ_HANDLED;
data->fault = fault;
if (fault)
dev_err(data->dev, "Charger suffers a fault and stops.\n");
else
dev_err(data->dev, "Charger recovered from a fault.\n");
max8903_charger_update_status(data);
max8903_battery_update_status(data);
power_supply_changed(&data->psy);
power_supply_changed(&data->bat);
power_supply_changed(&data->usb);
return IRQ_HANDLED;
}
static irqreturn_t max8903_chg(int irq, void *_data)
{
struct max8903_data *data = _data;
struct max8903_pdata *pdata = data->pdata;
int chg_state;
chg_state = gpio_get_value(pdata->chg) ? false : true;
if (chg_state == data->chg_state)
return IRQ_HANDLED;
data->chg_state = chg_state;
max8903_charger_update_status(data);
max8903_battery_update_status(data);
power_supply_changed(&data->psy);
power_supply_changed(&data->bat);
power_supply_changed(&data->usb);
return IRQ_HANDLED;
}
static void max8903_battery_work(struct work_struct *work)
{
struct max8903_data *data;
data = container_of(work, struct max8903_data, work.work);
data->interval = HZ * BATTERY_UPDATE_INTERVAL;
max8903_charger_update_status(data);
max8903_battery_update_status(data);
pr_debug("battery voltage: %4d mV\n" , data->voltage_uV);
pr_debug("charger online status: %d\n" , data->charger_online);
pr_debug("battery status : %d\n" , data->battery_status);
pr_debug("battery capacity percent: %3d\n" , data->percent);
pr_debug("data->usb_in: %x , data->ta_in: %x \n" , data->usb_in, data->ta_in);
/* reschedule for the next time */
schedule_delayed_work(&data->work, data->interval);
}
static ssize_t max8903_voltage_offset_discharger_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "read offset_discharger:%04d\n",
offset_discharger);
}
static ssize_t max8903_voltage_offset_discharger_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
offset_discharger = simple_strtoul(buf, NULL, 10);
pr_info("read offset_discharger:%04d\n", offset_discharger);
return count;
}
static ssize_t max8903_voltage_offset_charger_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "read offset_charger:%04d\n",
offset_charger);
}
static ssize_t max8903_voltage_offset_charger_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
offset_charger = simple_strtoul(buf, NULL, 10);
pr_info("read offset_charger:%04d\n", offset_charger);
return count;
}
static struct device_attribute max8903_discharger_dev_attr = {
.attr = {
.name = "max8903_ctl_offset_discharger",
.mode = S_IRUSR | S_IWUSR,
},
.show = max8903_voltage_offset_discharger_show,
.store = max8903_voltage_offset_discharger_store,
};
static struct device_attribute max8903_charger_dev_attr = {
.attr = {
.name = "max8903_ctl_offset_charger",
.mode = S_IRUSR | S_IWUSR,
},
.show = max8903_voltage_offset_charger_show,
.store = max8903_voltage_offset_charger_store,
};
static __devinit int max8903_probe(struct platform_device *pdev)
{
struct max8903_data *data;
struct device *dev = &pdev->dev;
struct max8903_pdata *pdata = pdev->dev.platform_data;
int ret = 0;
int gpio = 0;
int ta_in = 0;
int usb_in = 0;
int retval;
cpu_type_flag = 0;
if (cpu_is_mx6q())
cpu_type_flag = 1;
if (cpu_is_mx6dl())
cpu_type_flag = 0;
data = kzalloc(sizeof(struct max8903_data), GFP_KERNEL);
if (data == NULL) {
dev_err(dev, "Cannot allocate memory.\n");
return -ENOMEM;
}
data->pdata = pdata;
data->dev = dev;
platform_set_drvdata(pdev, data);
data->usb_in = 0;
data->ta_in = 0;
if (pdata->dc_valid == false && pdata->usb_valid == false) {
dev_err(dev, "No valid power sources.\n");
printk(KERN_INFO "No valid power sources.\n");
ret = -EINVAL;
goto err;
}
if (pdata->dc_valid) {
if (pdata->dok && gpio_is_valid(pdata->dok)) {
gpio = pdata->dok; /* PULL_UPed Interrupt */
/* set DOK gpio input */
ret = gpio_request(gpio, "max8903-DOK");
if (ret) {
printk(KERN_ERR"request max8903-DOK error!!\n");
goto err;
} else {
gpio_direction_input(gpio);
}
ta_in = gpio_get_value(gpio) ? 0 : 1;
} else if (pdata->dok && gpio_is_valid(pdata->dok) && pdata->dcm_always_high) {
ta_in = pdata->dok; /* PULL_UPed Interrupt */
ta_in = gpio_get_value(gpio) ? 0 : 1;
} else {
dev_err(dev, "When DC is wired, DOK and DCM should"
" be wired as well."
" or set dcm always high\n");
ret = -EINVAL;
goto err;
}
}
if (pdata->usb_valid) {
if (pdata->uok && gpio_is_valid(pdata->uok)) {
gpio = pdata->uok;
/* set UOK gpio input */
ret = gpio_request(gpio, "max8903-UOK");
if (ret) {
printk(KERN_ERR"request max8903-UOK error!!\n");
goto err;
} else {
gpio_direction_input(gpio);
}
usb_in = gpio_get_value(gpio) ? 0 : 1;
} else {
dev_err(dev, "When USB is wired, UOK should be wired."
"as well.\n");
ret = -EINVAL;
goto err;
}
}
if (pdata->chg) {
if (!gpio_is_valid(pdata->chg)) {
dev_err(dev, "Invalid pin: chg.\n");
ret = -EINVAL;
goto err;
}
/* set CHG gpio input */
ret = gpio_request(pdata->chg, "max8903-CHG");
if (ret) {
printk(KERN_ERR"request max8903-CHG error!!\n");
goto err;
} else {
gpio_direction_input(pdata->chg);
}
}
if (pdata->flt) {
if (!gpio_is_valid(pdata->flt)) {
dev_err(dev, "Invalid pin: flt.\n");
ret = -EINVAL;
goto err;
}
/* set FLT gpio input */
ret = gpio_request(pdata->flt, "max8903-FLT");
if (ret) {
printk(KERN_ERR"request max8903-FLT error!!\n");
goto err;
} else {
gpio_direction_input(pdata->flt);
}
}
if (pdata->usus) {
if (!gpio_is_valid(pdata->usus)) {
dev_err(dev, "Invalid pin: usus.\n");
ret = -EINVAL;
goto err;
}
}
mutex_init(&data->work_lock);
data->fault = false;
data->ta_in = ta_in;
data->usb_in = usb_in;
data->psy.name = "max8903-ac";
data->psy.type = POWER_SUPPLY_TYPE_MAINS;
data->psy.get_property = max8903_get_property;
data->psy.properties = max8903_charger_props;
data->psy.num_properties = ARRAY_SIZE(max8903_charger_props);
ret = power_supply_register(dev, &data->psy);
if (ret) {
dev_err(dev, "failed: power supply register.\n");
goto err_psy;
}
data->usb.name = "max8903-usb";
data->usb.type = POWER_SUPPLY_TYPE_USB;
data->usb.get_property = max8903_get_usb_property;
data->usb.properties = max8903_charger_props;
data->usb.num_properties = ARRAY_SIZE(max8903_charger_props);
ret = power_supply_register(dev, &data->usb);
if (ret) {
dev_err(dev, "failed: power supply register.\n");
goto err_psy;
}
data->bat.name = "max8903-charger";
data->bat.type = POWER_SUPPLY_TYPE_BATTERY;
data->bat.properties = max8903_battery_props;
data->bat.num_properties = ARRAY_SIZE(max8903_battery_props);
data->bat.get_property = max8903_battery_get_property;
data->bat.use_for_apm = 1;
retval = power_supply_register(&pdev->dev, &data->bat);
if (retval) {
dev_err(data->dev, "failed to register battery\n");
goto battery_failed;
}
INIT_DELAYED_WORK(&data->work, max8903_battery_work);
schedule_delayed_work(&data->work, data->interval);
if (pdata->dc_valid) {
ret = request_threaded_irq(gpio_to_irq(pdata->dok),
NULL, max8903_dcin,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"MAX8903 DC IN", data);
if (ret) {
dev_err(dev, "Cannot request irq %d for DC (%d)\n",
gpio_to_irq(pdata->dok), ret);
goto err_usb_irq;
}
}
if (pdata->usb_valid) {
ret = request_threaded_irq(gpio_to_irq(pdata->uok),
NULL, max8903_usbin,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"MAX8903 USB IN", data);
if (ret) {
dev_err(dev, "Cannot request irq %d for USB (%d)\n",
gpio_to_irq(pdata->uok), ret);
goto err_dc_irq;
}
}
if (pdata->flt) {
ret = request_threaded_irq(gpio_to_irq(pdata->flt),
NULL, max8903_fault,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"MAX8903 Fault", data);
if (ret) {
dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
gpio_to_irq(pdata->flt), ret);
goto err_flt_irq;
}
}
if (pdata->chg) {
ret = request_threaded_irq(gpio_to_irq(pdata->chg),
NULL, max8903_chg,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"MAX8903 Fault", data);
if (ret) {
dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
gpio_to_irq(pdata->flt), ret);
goto err_chg_irq;
}
}
ret = device_create_file(&pdev->dev, &max8903_discharger_dev_attr);
if (ret)
dev_err(&pdev->dev, "create device file failed!\n");
ret = device_create_file(&pdev->dev, &max8903_charger_dev_attr);
if (ret)
dev_err(&pdev->dev, "create device file failed!\n");
if (cpu_type_flag == 1) {
offset_discharger = 1694;
offset_charger = 1900;
}
if (cpu_type_flag == 0) {
offset_discharger = 1464;
offset_charger = 1485;
}
max8903_charger_update_status(data);
max8903_battery_update_status(data);
return 0;
err_psy:
power_supply_unregister(&data->psy);
battery_failed:
power_supply_unregister(&data->bat);
err_usb_irq:
if (pdata->usb_valid)
free_irq(gpio_to_irq(pdata->uok), data);
cancel_delayed_work(&data->work);
err_dc_irq:
if (pdata->dc_valid)
free_irq(gpio_to_irq(pdata->dok), data);
cancel_delayed_work(&data->work);
err_flt_irq:
if (pdata->usb_valid)
free_irq(gpio_to_irq(pdata->uok), data);
cancel_delayed_work(&data->work);
err_chg_irq:
if (pdata->dc_valid)
free_irq(gpio_to_irq(pdata->dok), data);
cancel_delayed_work(&data->work);
err:
if (pdata->uok)
gpio_free(pdata->uok);
if (pdata->dok)
gpio_free(pdata->dok);
if (pdata->flt)
gpio_free(pdata->flt);
if (pdata->chg)
gpio_free(pdata->chg);
kfree(data);
return ret;
}
static __devexit int max8903_remove(struct platform_device *pdev)
{
struct max8903_data *data = platform_get_drvdata(pdev);
if (data) {
struct max8903_pdata *pdata = data->pdata;
if (pdata->flt)
free_irq(gpio_to_irq(pdata->flt), data);
if (pdata->usb_valid)
free_irq(gpio_to_irq(pdata->uok), data);
if (pdata->dc_valid)
free_irq(gpio_to_irq(pdata->dok), data);
if (pdata->dc_valid)
free_irq(gpio_to_irq(pdata->chg), data);
cancel_delayed_work_sync(&data->work);
power_supply_unregister(&data->psy);
power_supply_unregister(&data->bat);
kfree(data);
}
return 0;
}
static int max8903_suspend(struct platform_device *pdev,
pm_message_t state)
{
struct max8903_data *data = platform_get_drvdata(pdev);
cancel_delayed_work(&data->work);
return 0;
}
static int max8903_resume(struct platform_device *pdev)
{
struct max8903_data *data = platform_get_drvdata(pdev);
schedule_delayed_work(&data->work, BATTERY_UPDATE_INTERVAL);
return 0;
}
static struct platform_driver max8903_driver = {
.probe = max8903_probe,
.remove = __devexit_p(max8903_remove),
.suspend = max8903_suspend,
.resume = max8903_resume,
.driver = {
.name = "max8903-charger",
.owner = THIS_MODULE,
},
};
static int __init max8903_init(void)
{
return platform_driver_register(&max8903_driver);
}
module_init(max8903_init);
static void __exit max8903_exit(void)
{
platform_driver_unregister(&max8903_driver);
}
module_exit(max8903_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Sabresd Battery Driver");
MODULE_ALIAS("sabresd_battery");
|