Blame view

kernel/linux-imx6_3.14.28/drivers/power/da9052-battery.c 15.3 KB
6b13f685e   김민수   BSP 최초 추가
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
  /*
   * Batttery Driver for Dialog DA9052 PMICs
   *
   * Copyright(c) 2011 Dialog Semiconductor Ltd.
   *
   * Author: David Dajun Chen <dchen@diasemi.com>
   *
   * 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.
   */
  
  #include <linux/delay.h>
  #include <linux/freezer.h>
  #include <linux/fs.h>
  #include <linux/jiffies.h>
  #include <linux/module.h>
  #include <linux/timer.h>
  #include <linux/uaccess.h>
  #include <linux/platform_device.h>
  #include <linux/power_supply.h>
  
  #include <linux/mfd/da9052/da9052.h>
  #include <linux/mfd/da9052/pdata.h>
  #include <linux/mfd/da9052/reg.h>
  
  /* STATIC CONFIGURATION */
  #define DA9052_BAT_CUTOFF_VOLT		2800
  #define DA9052_BAT_TSH			62000
  #define DA9052_BAT_LOW_CAP		4
  #define DA9052_AVG_SZ			4
  #define DA9052_VC_TBL_SZ		68
  #define DA9052_VC_TBL_REF_SZ		3
  
  #define DA9052_ISET_USB_MASK		0x0F
  #define DA9052_CHG_USB_ILIM_MASK	0x40
  #define DA9052_CHG_LIM_COLS		16
  
  #define DA9052_MEAN(x, y)		((x + y) / 2)
  
  enum charger_type_enum {
  	DA9052_NOCHARGER = 1,
  	DA9052_CHARGER,
  };
  
  static const u16 da9052_chg_current_lim[2][DA9052_CHG_LIM_COLS] = {
  	{70,  80,  90,  100, 110, 120, 400,  450,
  	 500, 550, 600, 650, 700, 900, 1100, 1300},
  	{80,  90,  100, 110,  120,  400,  450,  500,
  	 550, 600, 800, 1000, 1200, 1400, 1600, 1800},
  };
  
  static const u16 vc_tbl_ref[3] = {10, 25, 40};
  /* Lookup table for voltage vs capacity */
  static u32 const vc_tbl[3][68][2] = {
  	/* For temperature 10 degree Celsius */
  	{
  	{4082, 100}, {4036, 98},
  	{4020, 96}, {4008, 95},
  	{3997, 93}, {3983, 91},
  	{3964, 90}, {3943, 88},
  	{3926, 87}, {3912, 85},
  	{3900, 84}, {3890, 82},
  	{3881, 80}, {3873, 79},
  	{3865, 77}, {3857, 76},
  	{3848, 74}, {3839, 73},
  	{3829, 71}, {3820, 70},
  	{3811, 68}, {3802, 67},
  	{3794, 65}, {3785, 64},
  	{3778, 62}, {3770, 61},
  	{3763, 59}, {3756, 58},
  	{3750, 56}, {3744, 55},
  	{3738, 53}, {3732, 52},
  	{3727, 50}, {3722, 49},
  	{3717, 47}, {3712, 46},
  	{3708, 44}, {3703, 43},
  	{3700, 41}, {3696, 40},
  	{3693, 38}, {3691, 37},
  	{3688, 35}, {3686, 34},
  	{3683, 32}, {3681, 31},
  	{3678, 29}, {3675, 28},
  	{3672, 26}, {3669, 25},
  	{3665, 23}, {3661, 22},
  	{3656, 21}, {3651, 19},
  	{3645, 18}, {3639, 16},
  	{3631, 15}, {3622, 13},
  	{3611, 12}, {3600, 10},
  	{3587, 9}, {3572, 7},
  	{3548, 6}, {3503, 5},
  	{3420, 3}, {3268, 2},
  	{2992, 1}, {2746, 0}
  	},
  	/* For temperature 25 degree Celsius */
  	{
  	{4102, 100}, {4065, 98},
  	{4048, 96}, {4034, 95},
  	{4021, 93}, {4011, 92},
  	{4001, 90}, {3986, 88},
  	{3968, 87}, {3952, 85},
  	{3938, 84}, {3926, 82},
  	{3916, 81}, {3908, 79},
  	{3900, 77}, {3892, 76},
  	{3883, 74}, {3874, 73},
  	{3864, 71}, {3855, 70},
  	{3846, 68}, {3836, 67},
  	{3827, 65}, {3819, 64},
  	{3810, 62}, {3801, 61},
  	{3793, 59}, {3786, 58},
  	{3778, 56}, {3772, 55},
  	{3765, 53}, {3759, 52},
  	{3754, 50}, {3748, 49},
  	{3743, 47}, {3738, 46},
  	{3733, 44}, {3728, 43},
  	{3724, 41}, {3720, 40},
  	{3716, 38}, {3712, 37},
  	{3709, 35}, {3706, 34},
  	{3703, 33}, {3701, 31},
  	{3698, 30}, {3696, 28},
  	{3693, 27}, {3690, 25},
  	{3687, 24}, {3683, 22},
  	{3680, 21}, {3675, 19},
  	{3671, 18}, {3666, 17},
  	{3660, 15}, {3654, 14},
  	{3647, 12}, {3639, 11},
  	{3630, 9}, {3621, 8},
  	{3613, 6}, {3606, 5},
  	{3597, 4}, {3582, 2},
  	{3546, 1}, {2747, 0}
  	},
  	/* For temperature 40 degree Celsius */
  	{
  	{4114, 100}, {4081, 98},
  	{4065, 96}, {4050, 95},
  	{4036, 93}, {4024, 92},
  	{4013, 90}, {4002, 88},
  	{3990, 87}, {3976, 85},
  	{3962, 84}, {3950, 82},
  	{3939, 81}, {3930, 79},
  	{3921, 77}, {3912, 76},
  	{3902, 74}, {3893, 73},
  	{3883, 71}, {3874, 70},
  	{3865, 68}, {3856, 67},
  	{3847, 65}, {3838, 64},
  	{3829, 62}, {3820, 61},
  	{3812, 59}, {3803, 58},
  	{3795, 56}, {3787, 55},
  	{3780, 53}, {3773, 52},
  	{3767, 50}, {3761, 49},
  	{3756, 47}, {3751, 46},
  	{3746, 44}, {3741, 43},
  	{3736, 41}, {3732, 40},
  	{3728, 38}, {3724, 37},
  	{3720, 35}, {3716, 34},
  	{3713, 33}, {3710, 31},
  	{3707, 30}, {3704, 28},
  	{3701, 27}, {3698, 25},
  	{3695, 24}, {3691, 22},
  	{3686, 21}, {3681, 19},
  	{3676, 18}, {3671, 17},
  	{3666, 15}, {3661, 14},
  	{3655, 12}, {3648, 11},
  	{3640, 9}, {3632, 8},
  	{3622, 6}, {3616, 5},
  	{3611, 4}, {3604, 2},
  	{3594, 1}, {2747, 0}
  	}
  };
  
  struct da9052_battery {
  	struct da9052 *da9052;
  	struct power_supply psy;
  	struct notifier_block nb;
  	int charger_type;
  	int status;
  	int health;
  };
  
  static inline int volt_reg_to_mV(int value)
  {
  	return ((value * 1000) / 512) + 2500;
  }
  
  static inline int ichg_reg_to_mA(int value)
  {
  	return (value * 3900) / 1000;
  }
  
  static int da9052_read_chgend_current(struct da9052_battery *bat,
  				       int *current_mA)
  {
  	int ret;
  
  	if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  		return -EINVAL;
  
  	ret = da9052_reg_read(bat->da9052, DA9052_ICHG_END_REG);
  	if (ret < 0)
  		return ret;
  
  	*current_mA = ichg_reg_to_mA(ret & DA9052_ICHGEND_ICHGEND);
  
  	return 0;
  }
  
  static int da9052_read_chg_current(struct da9052_battery *bat, int *current_mA)
  {
  	int ret;
  
  	if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  		return -EINVAL;
  
  	ret = da9052_reg_read(bat->da9052, DA9052_ICHG_AV_REG);
  	if (ret < 0)
  		return ret;
  
  	*current_mA = ichg_reg_to_mA(ret & DA9052_ICHGAV_ICHGAV);
  
  	return 0;
  }
  
  static int da9052_bat_check_status(struct da9052_battery *bat, int *status)
  {
  	u8 v[2] = {0, 0};
  	u8 bat_status;
  	u8 chg_end;
  	int ret;
  	int chg_current;
  	int chg_end_current;
  	bool dcinsel;
  	bool dcindet;
  	bool vbussel;
  	bool vbusdet;
  	bool dc;
  	bool vbus;
  
  	ret = da9052_group_read(bat->da9052, DA9052_STATUS_A_REG, 2, v);
  	if (ret < 0)
  		return ret;
  
  	bat_status = v[0];
  	chg_end = v[1];
  
  	dcinsel = bat_status & DA9052_STATUSA_DCINSEL;
  	dcindet = bat_status & DA9052_STATUSA_DCINDET;
  	vbussel = bat_status & DA9052_STATUSA_VBUSSEL;
  	vbusdet = bat_status & DA9052_STATUSA_VBUSDET;
  	dc = dcinsel && dcindet;
  	vbus = vbussel && vbusdet;
  
  	/* Preference to WALL(DCIN) charger unit */
  	if (dc || vbus) {
  		bat->charger_type = DA9052_CHARGER;
  
  		/* If charging end flag is set and Charging current is greater
  		 * than charging end limit then battery is charging
  		*/
  		if ((chg_end & DA9052_STATUSB_CHGEND) != 0) {
  			ret = da9052_read_chg_current(bat, &chg_current);
  			if (ret < 0)
  				return ret;
  			ret = da9052_read_chgend_current(bat, &chg_end_current);
  			if (ret < 0)
  				return ret;
  
  			if (chg_current >= chg_end_current)
  				bat->status = POWER_SUPPLY_STATUS_CHARGING;
  			else
  				bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  		} else {
  			/* If Charging end flag is cleared then battery is
  			 * charging
  			*/
  			bat->status = POWER_SUPPLY_STATUS_CHARGING;
  		}
  	} else if (dcindet || vbusdet) {
  			bat->charger_type = DA9052_CHARGER;
  			bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  	} else {
  		bat->charger_type = DA9052_NOCHARGER;
  		bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  	}
  
  	if (status != NULL)
  		*status = bat->status;
  	return 0;
  }
  
  static int da9052_bat_read_volt(struct da9052_battery *bat, int *volt_mV)
  {
  	int volt;
  
  	volt = da9052_adc_manual_read(bat->da9052, DA9052_ADC_MAN_MUXSEL_VBAT);
  	if (volt < 0)
  		return volt;
  
  	*volt_mV = volt_reg_to_mV(volt);
  
  	return 0;
  }
  
  static int da9052_bat_check_presence(struct da9052_battery *bat, int *illegal)
  {
  	int bat_temp;
  
  	bat_temp = da9052_adc_read_temp(bat->da9052);
  	if (bat_temp < 0)
  		return bat_temp;
  
  	if (bat_temp > DA9052_BAT_TSH)
  		*illegal = 1;
  	else
  		*illegal = 0;
  
  	return 0;
  }
  
  static int da9052_bat_interpolate(int vbat_lower, int  vbat_upper,
  				   int level_lower, int level_upper,
  				   int bat_voltage)
  {
  	int tmp;
  
  	tmp = ((level_upper - level_lower) * 1000) / (vbat_upper - vbat_lower);
  	tmp = level_lower + (((bat_voltage - vbat_lower) * tmp) / 1000);
  
  	return tmp;
  }
  
  static unsigned char da9052_determine_vc_tbl_index(unsigned char adc_temp)
  {
  	int i;
  
  	if (adc_temp <= vc_tbl_ref[0])
  		return 0;
  
  	if (adc_temp > vc_tbl_ref[DA9052_VC_TBL_REF_SZ - 1])
  		return DA9052_VC_TBL_REF_SZ - 1;
  
  	for (i = 0; i < DA9052_VC_TBL_REF_SZ - 1; i++) {
  		if ((adc_temp > vc_tbl_ref[i]) &&
  		    (adc_temp <= DA9052_MEAN(vc_tbl_ref[i], vc_tbl_ref[i + 1])))
  				return i;
  		if ((adc_temp > DA9052_MEAN(vc_tbl_ref[i], vc_tbl_ref[i + 1]))
  		     && (adc_temp <= vc_tbl_ref[i]))
  				return i + 1;
  	}
  	/*
  	 * For some reason authors of the driver didn't presume that we can
  	 * end up here. It might be OK, but might be not, no one knows for
  	 * sure. Go check your battery, is it on fire?
  	 */
  	WARN_ON(1);
  	return 0;
  }
  
  static int da9052_bat_read_capacity(struct da9052_battery *bat, int *capacity)
  {
  	int adc_temp;
  	int bat_voltage;
  	int vbat_lower;
  	int vbat_upper;
  	int level_upper;
  	int level_lower;
  	int ret;
  	int flag;
  	int i = 0;
  	int j;
  
  	ret = da9052_bat_read_volt(bat, &bat_voltage);
  	if (ret < 0)
  		return ret;
  
  	adc_temp = da9052_adc_read_temp(bat->da9052);
  	if (adc_temp < 0)
  		return adc_temp;
  
  	i = da9052_determine_vc_tbl_index(adc_temp);
  
  	if (bat_voltage >= vc_tbl[i][0][0]) {
  		*capacity = 100;
  		return 0;
  	}
  	if (bat_voltage <= vc_tbl[i][DA9052_VC_TBL_SZ - 1][0]) {
  		*capacity = 0;
  		return 0;
  	}
  	flag = 0;
  
  	for (j = 0; j < (DA9052_VC_TBL_SZ-1); j++) {
  		if ((bat_voltage <= vc_tbl[i][j][0]) &&
  		    (bat_voltage >= vc_tbl[i][j + 1][0])) {
  			vbat_upper = vc_tbl[i][j][0];
  			vbat_lower = vc_tbl[i][j + 1][0];
  			level_upper = vc_tbl[i][j][1];
  			level_lower = vc_tbl[i][j + 1][1];
  			flag = 1;
  			break;
  		}
  	}
  	if (!flag)
  		return -EIO;
  
  	*capacity = da9052_bat_interpolate(vbat_lower, vbat_upper, level_lower,
  					   level_upper, bat_voltage);
  
  	return 0;
  }
  
  static int da9052_bat_check_health(struct da9052_battery *bat, int *health)
  {
  	int ret;
  	int bat_illegal;
  	int capacity;
  
  	ret = da9052_bat_check_presence(bat, &bat_illegal);
  	if (ret < 0)
  		return ret;
  
  	if (bat_illegal) {
  		bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  		return 0;
  	}
  
  	if (bat->health != POWER_SUPPLY_HEALTH_OVERHEAT) {
  		ret = da9052_bat_read_capacity(bat, &capacity);
  		if (ret < 0)
  			return ret;
  		if (capacity < DA9052_BAT_LOW_CAP)
  			bat->health = POWER_SUPPLY_HEALTH_DEAD;
  		else
  			bat->health = POWER_SUPPLY_HEALTH_GOOD;
  	}
  
  	*health = bat->health;
  
  	return 0;
  }
  
  static irqreturn_t da9052_bat_irq(int irq, void *data)
  {
  	struct da9052_battery *bat = data;
  	int virq;
  
  	virq = regmap_irq_get_virq(bat->da9052->irq_data, irq);
  	irq -= virq;
  
  	if (irq == DA9052_IRQ_CHGEND)
  		bat->status = POWER_SUPPLY_STATUS_FULL;
  	else
  		da9052_bat_check_status(bat, NULL);
  
  	if (irq == DA9052_IRQ_CHGEND || irq == DA9052_IRQ_DCIN ||
  	    irq == DA9052_IRQ_VBUS || irq == DA9052_IRQ_TBAT) {
  		power_supply_changed(&bat->psy);
  	}
  
  	return IRQ_HANDLED;
  }
  
  static int da9052_USB_current_notifier(struct notifier_block *nb,
  					unsigned long events, void *data)
  {
  	u8 row;
  	u8 col;
  	int *current_mA = data;
  	int ret;
  	struct da9052_battery *bat = container_of(nb, struct da9052_battery,
  						  nb);
  
  	if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING)
  		return -EPERM;
  
  	ret = da9052_reg_read(bat->da9052, DA9052_CHGBUCK_REG);
  	if (ret & DA9052_CHG_USB_ILIM_MASK)
  		return -EPERM;
  
  	if (bat->da9052->chip_id == DA9052)
  		row = 0;
  	else
  		row = 1;
  
  	if (*current_mA < da9052_chg_current_lim[row][0] ||
  	    *current_mA > da9052_chg_current_lim[row][DA9052_CHG_LIM_COLS - 1])
  		return -EINVAL;
  
  	for (col = 0; col <= DA9052_CHG_LIM_COLS - 1 ; col++) {
  		if (*current_mA <= da9052_chg_current_lim[row][col])
  			break;
  	}
  
  	return da9052_reg_update(bat->da9052, DA9052_ISET_REG,
  				 DA9052_ISET_USB_MASK, col);
  }
  
  static int da9052_bat_get_property(struct power_supply *psy,
  				    enum power_supply_property psp,
  				    union power_supply_propval *val)
  {
  	int ret;
  	int illegal;
  	struct da9052_battery *bat = container_of(psy, struct da9052_battery,
  						  psy);
  
  	ret = da9052_bat_check_presence(bat, &illegal);
  	if (ret < 0)
  		return ret;
  
  	if (illegal && psp != POWER_SUPPLY_PROP_PRESENT)
  		return -ENODEV;
  
  	switch (psp) {
  	case POWER_SUPPLY_PROP_STATUS:
  		ret = da9052_bat_check_status(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_ONLINE:
  		val->intval =
  			(bat->charger_type == DA9052_NOCHARGER) ? 0 : 1;
  		break;
  	case POWER_SUPPLY_PROP_PRESENT:
  		ret = da9052_bat_check_presence(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_HEALTH:
  		ret = da9052_bat_check_health(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  		val->intval = DA9052_BAT_CUTOFF_VOLT * 1000;
  		break;
  	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  		ret = da9052_bat_read_volt(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_CURRENT_AVG:
  		ret = da9052_read_chg_current(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_CAPACITY:
  		ret = da9052_bat_read_capacity(bat, &val->intval);
  		break;
  	case POWER_SUPPLY_PROP_TEMP:
  		val->intval = da9052_adc_read_temp(bat->da9052);
  		ret = val->intval;
  		break;
  	case POWER_SUPPLY_PROP_TECHNOLOGY:
  		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  		break;
  	default:
  		return -EINVAL;
  	}
  	return ret;
  }
  
  static enum power_supply_property da9052_bat_props[] = {
  	POWER_SUPPLY_PROP_STATUS,
  	POWER_SUPPLY_PROP_ONLINE,
  	POWER_SUPPLY_PROP_PRESENT,
  	POWER_SUPPLY_PROP_HEALTH,
  	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  	POWER_SUPPLY_PROP_VOLTAGE_AVG,
  	POWER_SUPPLY_PROP_CURRENT_AVG,
  	POWER_SUPPLY_PROP_CAPACITY,
  	POWER_SUPPLY_PROP_TEMP,
  	POWER_SUPPLY_PROP_TECHNOLOGY,
  };
  
  static struct power_supply template_battery = {
  	.name		= "da9052-bat",
  	.type		= POWER_SUPPLY_TYPE_BATTERY,
  	.properties	= da9052_bat_props,
  	.num_properties	= ARRAY_SIZE(da9052_bat_props),
  	.get_property	= da9052_bat_get_property,
  };
  
  static char *da9052_bat_irqs[] = {
  	"BATT TEMP",
  	"DCIN DET",
  	"DCIN REM",
  	"VBUS DET",
  	"VBUS REM",
  	"CHG END",
  };
  
  static int da9052_bat_irq_bits[] = {
  	DA9052_IRQ_TBAT,
  	DA9052_IRQ_DCIN,
  	DA9052_IRQ_DCINREM,
  	DA9052_IRQ_VBUS,
  	DA9052_IRQ_VBUSREM,
  	DA9052_IRQ_CHGEND,
  };
  
  static s32 da9052_bat_probe(struct platform_device *pdev)
  {
  	struct da9052_pdata *pdata;
  	struct da9052_battery *bat;
  	int ret;
  	int i;
  
  	bat = devm_kzalloc(&pdev->dev, sizeof(struct da9052_battery),
  				GFP_KERNEL);
  	if (!bat)
  		return -ENOMEM;
  
  	bat->da9052 = dev_get_drvdata(pdev->dev.parent);
  	bat->psy = template_battery;
  	bat->charger_type = DA9052_NOCHARGER;
  	bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  	bat->health = POWER_SUPPLY_HEALTH_UNKNOWN;
  	bat->nb.notifier_call = da9052_USB_current_notifier;
  
  	pdata = bat->da9052->dev->platform_data;
  	if (pdata != NULL && pdata->use_for_apm)
  		bat->psy.use_for_apm = pdata->use_for_apm;
  	else
  		bat->psy.use_for_apm = 1;
  
  	for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++) {
  		ret = da9052_request_irq(bat->da9052,
  				da9052_bat_irq_bits[i], da9052_bat_irqs[i],
  				da9052_bat_irq, bat);
  
  		if (ret != 0) {
  			dev_err(bat->da9052->dev,
  				"DA9052 failed to request %s IRQ: %d
  ",
  				da9052_bat_irqs[i], ret);
  			goto err;
  		}
  	}
  
  	ret = power_supply_register(&pdev->dev, &bat->psy);
  	 if (ret)
  		goto err;
  
  	platform_set_drvdata(pdev, bat);
  	return 0;
  
  err:
  	while (--i >= 0)
  		da9052_free_irq(bat->da9052, da9052_bat_irq_bits[i], bat);
  
  	return ret;
  }
  static int da9052_bat_remove(struct platform_device *pdev)
  {
  	int i;
  	struct da9052_battery *bat = platform_get_drvdata(pdev);
  
  	for (i = 0; i < ARRAY_SIZE(da9052_bat_irqs); i++)
  		da9052_free_irq(bat->da9052, da9052_bat_irq_bits[i], bat);
  
  	power_supply_unregister(&bat->psy);
  
  	return 0;
  }
  
  static struct platform_driver da9052_bat_driver = {
  	.probe = da9052_bat_probe,
  	.remove = da9052_bat_remove,
  	.driver = {
  		.name = "da9052-bat",
  		.owner = THIS_MODULE,
  	},
  };
  module_platform_driver(da9052_bat_driver);
  
  MODULE_DESCRIPTION("DA9052 BAT Device Driver");
  MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS("platform:da9052-bat");