android_battery.c revision 711507934538462ab7c71f696f9112504795ce43
1/*
2 *  android_battery.c
3 *  Android Battery Driver
4 *
5 * Copyright (C) 2012 Google, Inc.
6 * Copyright (C) 2012 Samsung Electronics
7 *
8 * Based on work by himihee.seo@samsung.com, ms925.kim@samsung.com, and
9 * joshua.chang@samsung.com.
10 *
11 * This software is licensed under the terms of the GNU General Public
12 * License version 2, as published by the Free Software Foundation, and
13 * may be copied, distributed, and modified under those terms.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/types.h>
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/err.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/jiffies.h>
29#include <linux/platform_device.h>
30#include <linux/power_supply.h>
31#include <linux/slab.h>
32#include <linux/wakelock.h>
33#include <linux/workqueue.h>
34#include <linux/alarmtimer.h>
35#include <linux/timer.h>
36#include <linux/mutex.h>
37#include <linux/debugfs.h>
38#include <linux/platform_data/android_battery.h>
39
40#define FAST_POLL               (1 * 60)
41#define SLOW_POLL               (10 * 60)
42
43struct android_bat_data {
44	struct android_bat_platform_data *pdata;
45	struct android_bat_callbacks callbacks;
46
47	struct device		*dev;
48
49	struct power_supply	psy_bat;
50	struct power_supply	psy_usb;
51	struct power_supply	psy_ac;
52
53	struct wake_lock	monitor_wake_lock;
54	struct wake_lock	charger_wake_lock;
55
56	int			charge_source;
57
58	int			batt_temp;
59	int			batt_current;
60	unsigned int		batt_health;
61	unsigned int		batt_vcell;
62	unsigned int		batt_soc;
63	unsigned int		charging_status;
64	bool			recharging;
65	unsigned long		charging_start_time;
66
67	struct workqueue_struct *monitor_wqueue;
68	struct work_struct	monitor_work;
69	struct work_struct	charger_work;
70
71	struct alarm		monitor_alarm;
72	ktime_t			last_poll;
73
74	struct dentry		*debugfs_entry;
75};
76
77static char *supply_list[] = {
78	"android-battery",
79};
80
81static enum power_supply_property android_battery_props[] = {
82	POWER_SUPPLY_PROP_STATUS,
83	POWER_SUPPLY_PROP_HEALTH,
84	POWER_SUPPLY_PROP_PRESENT,
85	POWER_SUPPLY_PROP_TEMP,
86	POWER_SUPPLY_PROP_ONLINE,
87	POWER_SUPPLY_PROP_VOLTAGE_NOW,
88	POWER_SUPPLY_PROP_CAPACITY,
89	POWER_SUPPLY_PROP_TECHNOLOGY,
90	POWER_SUPPLY_PROP_CURRENT_NOW,
91};
92
93static enum power_supply_property android_power_props[] = {
94	POWER_SUPPLY_PROP_ONLINE,
95};
96
97static DEFINE_MUTEX(android_bat_state_lock);
98
99static void android_bat_update_data(struct android_bat_data *battery);
100static int android_bat_enable_charging(struct android_bat_data *battery,
101					bool enable);
102
103static char *charge_source_str(int charge_source)
104{
105	switch (charge_source) {
106	case CHARGE_SOURCE_NONE:
107		return "none";
108	case CHARGE_SOURCE_AC:
109		return "ac";
110	case CHARGE_SOURCE_USB:
111		return "usb";
112	default:
113		break;
114	}
115
116	return "?";
117}
118
119static int android_bat_get_property(struct power_supply *ps,
120				enum power_supply_property psp,
121				union power_supply_propval *val)
122{
123	struct android_bat_data *battery =
124		container_of(ps, struct android_bat_data, psy_bat);
125
126	switch (psp) {
127	case POWER_SUPPLY_PROP_STATUS:
128		val->intval = battery->charging_status;
129		break;
130	case POWER_SUPPLY_PROP_HEALTH:
131		val->intval = battery->batt_health;
132		break;
133	case POWER_SUPPLY_PROP_PRESENT:
134		val->intval = 1;
135		break;
136	case POWER_SUPPLY_PROP_TEMP:
137		val->intval = battery->batt_temp;
138		break;
139	case POWER_SUPPLY_PROP_ONLINE:
140		val->intval = 1;
141		break;
142	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
143		android_bat_update_data(battery);
144		val->intval = battery->batt_vcell;
145		if (val->intval == -1)
146			return -EINVAL;
147		break;
148	case POWER_SUPPLY_PROP_CAPACITY:
149		val->intval = battery->batt_soc;
150		if (val->intval == -1)
151			return -EINVAL;
152		break;
153	case POWER_SUPPLY_PROP_TECHNOLOGY:
154		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
155		break;
156	case POWER_SUPPLY_PROP_CURRENT_NOW:
157		android_bat_update_data(battery);
158		val->intval = battery->batt_current;
159		break;
160	default:
161		return -EINVAL;
162	}
163	return 0;
164}
165
166static int android_usb_get_property(struct power_supply *ps,
167		enum power_supply_property psp,
168		union power_supply_propval *val)
169{
170	struct android_bat_data *battery = container_of(ps,
171				struct android_bat_data, psy_usb);
172
173	if (psp != POWER_SUPPLY_PROP_ONLINE)
174		return -EINVAL;
175
176	val->intval = (battery->charge_source == CHARGE_SOURCE_USB);
177
178	return 0;
179}
180
181static int android_ac_get_property(struct power_supply *ps,
182		enum power_supply_property psp,
183		union power_supply_propval *val)
184{
185	struct android_bat_data *battery = container_of(ps,
186				struct android_bat_data, psy_ac);
187
188	if (psp != POWER_SUPPLY_PROP_ONLINE)
189		return -EINVAL;
190
191	val->intval = (battery->charge_source == CHARGE_SOURCE_AC);
192
193	return 0;
194}
195
196static void android_bat_get_temp(struct android_bat_data *battery)
197{
198	int batt_temp = 42; /* 4.2C */
199	int health = battery->batt_health;
200
201	if (battery->pdata->get_temperature)
202		battery->pdata->get_temperature(&batt_temp);
203
204	if (batt_temp >= battery->pdata->temp_high_threshold) {
205		if (health != POWER_SUPPLY_HEALTH_OVERHEAT &&
206				health != POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
207			pr_info("battery overheat (%d>=%d), charging unavailable\n",
208				batt_temp, battery->pdata->temp_high_threshold);
209			battery->batt_health = POWER_SUPPLY_HEALTH_OVERHEAT;
210		}
211	} else if (batt_temp <= battery->pdata->temp_high_recovery &&
212			batt_temp >= battery->pdata->temp_low_recovery) {
213		if (health == POWER_SUPPLY_HEALTH_OVERHEAT ||
214				health == POWER_SUPPLY_HEALTH_COLD) {
215			pr_info("battery recovery (%d,%d~%d), charging available\n",
216				batt_temp, battery->pdata->temp_low_recovery,
217				battery->pdata->temp_high_recovery);
218			battery->batt_health = POWER_SUPPLY_HEALTH_GOOD;
219		}
220	} else if (batt_temp <= battery->pdata->temp_low_threshold) {
221		if (health != POWER_SUPPLY_HEALTH_COLD &&
222				health != POWER_SUPPLY_HEALTH_UNSPEC_FAILURE) {
223			pr_info("battery cold (%d <= %d), charging unavailable\n",
224				batt_temp, battery->pdata->temp_low_threshold);
225			battery->batt_health = POWER_SUPPLY_HEALTH_COLD;
226		}
227	}
228
229	battery->batt_temp = batt_temp;
230}
231
232/*
233 * android_bat_state_lock not held, may call back into
234 * android_bat_charge_source_changed.  Gathering data here can be
235 * non-atomic; updating our state based on the data may need to be
236 * atomic.
237 */
238
239static void android_bat_update_data(struct android_bat_data *battery)
240{
241	int ret;
242	int v;
243
244	if (battery->pdata->poll_charge_source)
245		battery->charge_source = battery->pdata->poll_charge_source();
246
247	if (battery->pdata->get_voltage_now) {
248		ret = battery->pdata->get_voltage_now();
249		battery->batt_vcell = ret >= 0 ? ret : 4242000;
250	}
251
252	if (battery->pdata->get_capacity) {
253		ret = battery->pdata->get_capacity();
254		battery->batt_soc = ret >= 0 ? ret : 42;
255	}
256
257	if (battery->pdata->get_current_now) {
258		ret = battery->pdata->get_current_now(&v);
259
260		if (!ret)
261			battery->batt_current = v;
262	}
263
264	android_bat_get_temp(battery);
265}
266
267static void android_bat_set_charge_time(struct android_bat_data *battery,
268					bool enable)
269{
270	if (enable && !battery->charging_start_time) {
271		struct timespec cur_time;
272
273		get_monotonic_boottime(&cur_time);
274		/* record start time for charge timeout timer */
275		battery->charging_start_time = cur_time.tv_sec;
276	} else if (!enable) {
277		/* clear charge timeout timer */
278		battery->charging_start_time = 0;
279	}
280}
281
282static int android_bat_enable_charging(struct android_bat_data *battery,
283				       bool enable)
284{
285	if (enable && (battery->batt_health != POWER_SUPPLY_HEALTH_GOOD)) {
286		battery->charging_status =
287		    POWER_SUPPLY_STATUS_NOT_CHARGING;
288		return -EPERM;
289	}
290
291	if (enable) {
292		if (battery->pdata && battery->pdata->set_charging_current)
293			battery->pdata->set_charging_current
294			(battery->charge_source);
295	}
296
297	if (battery->pdata && battery->pdata->set_charging_enable)
298		battery->pdata->set_charging_enable(enable);
299
300	android_bat_set_charge_time(battery, enable);
301	pr_info("battery: enable=%d charger: %s\n", enable,
302		charge_source_str(battery->charge_source));
303	return 0;
304}
305
306static bool android_bat_charge_timeout(struct android_bat_data *battery,
307				       unsigned long timeout)
308{
309	struct timespec cur_time;
310
311	if (!battery->charging_start_time)
312		return 0;
313
314	get_monotonic_boottime(&cur_time);
315	pr_debug("%s: Start time: %ld, End time: %ld, current time: %ld\n",
316		 __func__, battery->charging_start_time,
317		 battery->charging_start_time + timeout,
318		 cur_time.tv_sec);
319	return cur_time.tv_sec >= battery->charging_start_time + timeout;
320}
321
322static void android_bat_charging_timer(struct android_bat_data *battery)
323{
324	if (!battery->charging_start_time &&
325	    battery->charging_status == POWER_SUPPLY_STATUS_CHARGING) {
326		android_bat_enable_charging(battery, true);
327		battery->recharging = true;
328		pr_debug("%s: charge status charging but timer is expired\n",
329			__func__);
330	} else if (battery->charging_start_time == 0) {
331		pr_debug("%s: charging_start_time never initialized\n",
332				__func__);
333		return;
334	}
335
336	if (android_bat_charge_timeout(
337		    battery,
338		    battery->recharging ? battery->pdata->recharging_time :
339		    battery->pdata->full_charging_time)) {
340		android_bat_enable_charging(battery, false);
341		if (battery->batt_vcell >
342		    battery->pdata->recharging_voltage &&
343		    battery->batt_soc == 100)
344			battery->charging_status =
345				POWER_SUPPLY_STATUS_FULL;
346		battery->recharging = false;
347		battery->charging_start_time = 0;
348		pr_info("battery: charging timer expired\n");
349	}
350
351	return;
352}
353
354static void android_bat_charge_source_changed(struct android_bat_callbacks *ptr,
355					      int charge_source)
356{
357	struct android_bat_data *battery =
358		container_of(ptr, struct android_bat_data, callbacks);
359
360	wake_lock(&battery->charger_wake_lock);
361	mutex_lock(&android_bat_state_lock);
362	battery->charge_source = charge_source;
363
364	pr_info("battery: charge source type was changed: %s\n",
365		charge_source_str(battery->charge_source));
366
367	mutex_unlock(&android_bat_state_lock);
368	queue_work(battery->monitor_wqueue, &battery->charger_work);
369}
370
371static void android_bat_set_full_status(struct android_bat_callbacks *ptr)
372{
373	struct android_bat_data *battery =
374		container_of(ptr, struct android_bat_data, callbacks);
375
376	mutex_lock(&android_bat_state_lock);
377	pr_info("battery: battery full\n");
378	battery->charging_status = POWER_SUPPLY_STATUS_FULL;
379	android_bat_enable_charging(battery, false);
380	battery->recharging = false;
381	mutex_unlock(&android_bat_state_lock);
382	power_supply_changed(&battery->psy_bat);
383}
384
385static void android_bat_charger_work(struct work_struct *work)
386{
387	struct android_bat_data *battery =
388		container_of(work, struct android_bat_data, charger_work);
389
390	mutex_lock(&android_bat_state_lock);
391
392	switch (battery->charge_source) {
393	case CHARGE_SOURCE_NONE:
394		battery->charging_status = POWER_SUPPLY_STATUS_DISCHARGING;
395		android_bat_enable_charging(battery, false);
396		battery->batt_health = POWER_SUPPLY_HEALTH_GOOD;
397		battery->recharging = false;
398		battery->charging_start_time = 0;
399		break;
400	case CHARGE_SOURCE_USB:
401	case CHARGE_SOURCE_AC:
402		/*
403		 * If charging status indicates a charger was already
404		 * connected prior to this and a non-charging status is
405		 * set, leave the status alone.
406		 */
407		if (battery->charging_status ==
408		    POWER_SUPPLY_STATUS_DISCHARGING ||
409		    battery->charging_status == POWER_SUPPLY_STATUS_UNKNOWN) {
410			battery->charging_status = POWER_SUPPLY_STATUS_CHARGING;
411			android_bat_enable_charging(battery, true);
412		}
413
414		break;
415	default:
416		pr_err("%s: Invalid charger type\n", __func__);
417		break;
418	}
419
420	mutex_unlock(&android_bat_state_lock);
421	wake_lock_timeout(&battery->charger_wake_lock, HZ * 2);
422	power_supply_changed(&battery->psy_ac);
423	power_supply_changed(&battery->psy_usb);
424}
425
426
427static void android_bat_monitor_set_alarm(struct android_bat_data *battery,
428					  int seconds)
429{
430	alarm_start(&battery->monitor_alarm,
431		    ktime_add(battery->last_poll, ktime_set(seconds, 0)));
432}
433
434static void android_bat_monitor_work(struct work_struct *work)
435{
436	struct android_bat_data *battery =
437		container_of(work, struct android_bat_data, monitor_work);
438	struct timespec cur_time;
439
440	wake_lock(&battery->monitor_wake_lock);
441	android_bat_update_data(battery);
442	mutex_lock(&android_bat_state_lock);
443
444	switch (battery->charging_status) {
445	case POWER_SUPPLY_STATUS_FULL:
446		if (battery->batt_vcell < battery->pdata->recharging_voltage &&
447		    !battery->recharging) {
448			battery->recharging = true;
449			android_bat_enable_charging(battery, true);
450			pr_info("battery: start recharging, v=%d\n",
451				battery->batt_vcell/1000);
452		}
453		break;
454	case POWER_SUPPLY_STATUS_DISCHARGING:
455		break;
456	case POWER_SUPPLY_STATUS_CHARGING:
457		switch (battery->batt_health) {
458		case POWER_SUPPLY_HEALTH_OVERHEAT:
459		case POWER_SUPPLY_HEALTH_COLD:
460		case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
461		case POWER_SUPPLY_HEALTH_DEAD:
462		case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
463			battery->charging_status =
464				POWER_SUPPLY_STATUS_NOT_CHARGING;
465			android_bat_enable_charging(battery, false);
466
467			pr_info("battery: Not charging, health=%d\n",
468				battery->batt_health);
469			break;
470		default:
471			break;
472		}
473		break;
474	case POWER_SUPPLY_STATUS_NOT_CHARGING:
475		if (battery->batt_health == POWER_SUPPLY_HEALTH_GOOD) {
476			pr_info("battery: battery health recovered\n");
477			if (battery->charge_source != CHARGE_SOURCE_NONE) {
478				android_bat_enable_charging(battery, true);
479				battery->charging_status
480					= POWER_SUPPLY_STATUS_CHARGING;
481			} else {
482				battery->charging_status
483					= POWER_SUPPLY_STATUS_DISCHARGING;
484			}
485		}
486		break;
487	default:
488		pr_err("%s: Undefined battery status: %d\n", __func__,
489		       battery->charging_status);
490		break;
491	}
492
493	android_bat_charging_timer(battery);
494	get_monotonic_boottime(&cur_time);
495	pr_info("battery: l=%d v=%d c=%d temp=%s%ld.%ld h=%d st=%d%s ct=%lu type=%s\n",
496		battery->batt_soc, battery->batt_vcell/1000,
497		battery->batt_current, battery->batt_temp < 0 ? "-" : "",
498		abs(battery->batt_temp / 10), abs(battery->batt_temp % 10),
499		battery->batt_health, battery->charging_status,
500		   battery->recharging ? "r" : "",
501		   battery->charging_start_time ?
502		   cur_time.tv_sec - battery->charging_start_time : 0,
503		charge_source_str(battery->charge_source));
504	mutex_unlock(&android_bat_state_lock);
505	power_supply_changed(&battery->psy_bat);
506	battery->last_poll = ktime_get_boottime();
507	android_bat_monitor_set_alarm(battery, FAST_POLL);
508	wake_unlock(&battery->monitor_wake_lock);
509	return;
510}
511
512static enum alarmtimer_restart android_bat_monitor_alarm(
513	struct alarm *alarm, ktime_t now)
514{
515	struct android_bat_data *battery =
516		container_of(alarm, struct android_bat_data, monitor_alarm);
517
518	wake_lock(&battery->monitor_wake_lock);
519	queue_work(battery->monitor_wqueue, &battery->monitor_work);
520	return ALARMTIMER_NORESTART;
521}
522
523static int android_power_debug_dump(struct seq_file *s, void *unused)
524{
525	struct android_bat_data *battery = s->private;
526	struct timespec cur_time;
527
528	android_bat_update_data(battery);
529	get_monotonic_boottime(&cur_time);
530	mutex_lock(&android_bat_state_lock);
531	seq_printf(s, "l=%d v=%d c=%d temp=%s%ld.%ld h=%d st=%d%s ct=%lu type=%s\n",
532		   battery->batt_soc, battery->batt_vcell/1000,
533		   battery->batt_current, battery->batt_temp < 0 ? "-" : "",
534		   abs(battery->batt_temp / 10), abs(battery->batt_temp % 10),
535		   battery->batt_health, battery->charging_status,
536		   battery->recharging ? "r" : "",
537		   battery->charging_start_time ?
538		   cur_time.tv_sec - battery->charging_start_time : 0,
539		   charge_source_str(battery->charge_source));
540	mutex_unlock(&android_bat_state_lock);
541	return 0;
542}
543
544static int android_power_debug_open(struct inode *inode, struct file *file)
545{
546	return single_open(file, android_power_debug_dump, inode->i_private);
547}
548
549static const struct file_operations android_power_debug_fops = {
550	.open = android_power_debug_open,
551	.read = seq_read,
552	.llseek = seq_lseek,
553	.release = single_release,
554};
555
556static __devinit int android_bat_probe(struct platform_device *pdev)
557{
558	struct android_bat_platform_data *pdata = dev_get_platdata(&pdev->dev);
559	struct android_bat_data *battery;
560	int ret = 0;
561
562	dev_info(&pdev->dev, "Android Battery Driver\n");
563	battery = kzalloc(sizeof(*battery), GFP_KERNEL);
564	if (!battery)
565		return -ENOMEM;
566
567	battery->pdata = pdata;
568	if (!battery->pdata) {
569		pr_err("%s : No platform data\n", __func__);
570		ret = -EINVAL;
571		goto err_pdata;
572	}
573
574	battery->dev = &pdev->dev;
575	platform_set_drvdata(pdev, battery);
576	battery->batt_health = POWER_SUPPLY_HEALTH_GOOD;
577
578	battery->psy_bat.name = "android-battery",
579	battery->psy_bat.type = POWER_SUPPLY_TYPE_BATTERY,
580	battery->psy_bat.properties = android_battery_props,
581	battery->psy_bat.num_properties = ARRAY_SIZE(android_battery_props),
582	battery->psy_bat.get_property = android_bat_get_property,
583
584	battery->psy_usb.name = "android-usb",
585	battery->psy_usb.type = POWER_SUPPLY_TYPE_USB,
586	battery->psy_usb.supplied_to = supply_list,
587	battery->psy_usb.num_supplicants = ARRAY_SIZE(supply_list),
588	battery->psy_usb.properties = android_power_props,
589	battery->psy_usb.num_properties = ARRAY_SIZE(android_power_props),
590	battery->psy_usb.get_property = android_usb_get_property,
591
592	battery->psy_ac.name = "android-ac",
593	battery->psy_ac.type = POWER_SUPPLY_TYPE_MAINS,
594	battery->psy_ac.supplied_to = supply_list,
595	battery->psy_ac.num_supplicants = ARRAY_SIZE(supply_list),
596	battery->psy_ac.properties = android_power_props,
597	battery->psy_ac.num_properties = ARRAY_SIZE(android_power_props),
598	battery->psy_ac.get_property = android_ac_get_property;
599
600	battery->batt_vcell = -1;
601	battery->batt_soc = -1;
602
603	wake_lock_init(&battery->monitor_wake_lock, WAKE_LOCK_SUSPEND,
604			"android-battery-monitor");
605	wake_lock_init(&battery->charger_wake_lock, WAKE_LOCK_SUSPEND,
606			"android-chargerdetect");
607
608	ret = power_supply_register(&pdev->dev, &battery->psy_bat);
609	if (ret) {
610		dev_err(battery->dev, "%s: failed to register psy_bat\n",
611			__func__);
612		goto err_psy_bat_reg;
613	}
614
615	ret = power_supply_register(&pdev->dev, &battery->psy_usb);
616	if (ret) {
617		dev_err(battery->dev, "%s: failed to register psy_usb\n",
618				__func__);
619		goto err_psy_usb_reg;
620	}
621
622	ret = power_supply_register(&pdev->dev, &battery->psy_ac);
623	if (ret) {
624		dev_err(battery->dev, "%s: failed to register psy_ac\n",
625				__func__);
626		goto err_psy_ac_reg;
627	}
628
629	battery->monitor_wqueue =
630		create_singlethread_workqueue(dev_name(&pdev->dev));
631	if (!battery->monitor_wqueue) {
632		dev_err(battery->dev, "%s: fail to create workqueue\n",
633				__func__);
634		goto err_wq;
635	}
636
637	INIT_WORK(&battery->monitor_work, android_bat_monitor_work);
638	INIT_WORK(&battery->charger_work, android_bat_charger_work);
639
640	battery->callbacks.charge_source_changed =
641		android_bat_charge_source_changed;
642	battery->callbacks.battery_set_full =
643		android_bat_set_full_status;
644	if (battery->pdata && battery->pdata->register_callbacks)
645		battery->pdata->register_callbacks(&battery->callbacks);
646
647	/* get initial charger status */
648	if (battery->pdata->poll_charge_source)
649		battery->charge_source = battery->pdata->poll_charge_source();
650
651	wake_lock(&battery->charger_wake_lock);
652	queue_work(battery->monitor_wqueue, &battery->charger_work);
653
654	wake_lock(&battery->monitor_wake_lock);
655	battery->last_poll = ktime_get_boottime();
656	alarm_init(&battery->monitor_alarm, ALARM_BOOTTIME,
657		   android_bat_monitor_alarm);
658	queue_work(battery->monitor_wqueue, &battery->monitor_work);
659
660	battery->debugfs_entry =
661		debugfs_create_file("android-power", S_IRUGO, NULL,
662				    battery, &android_power_debug_fops);
663	if (!battery->debugfs_entry)
664		pr_err("failed to create android-power debugfs entry\n");
665
666	return 0;
667
668err_wq:
669	power_supply_unregister(&battery->psy_ac);
670err_psy_ac_reg:
671	power_supply_unregister(&battery->psy_usb);
672err_psy_usb_reg:
673	power_supply_unregister(&battery->psy_bat);
674err_psy_bat_reg:
675	wake_lock_destroy(&battery->monitor_wake_lock);
676	wake_lock_destroy(&battery->charger_wake_lock);
677err_pdata:
678	kfree(battery);
679
680	return ret;
681}
682
683static int __devexit android_bat_remove(struct platform_device *pdev)
684{
685	struct android_bat_data *battery = platform_get_drvdata(pdev);
686
687	alarm_cancel(&battery->monitor_alarm);
688	flush_workqueue(battery->monitor_wqueue);
689	destroy_workqueue(battery->monitor_wqueue);
690	power_supply_unregister(&battery->psy_bat);
691	wake_lock_destroy(&battery->monitor_wake_lock);
692	wake_lock_destroy(&battery->charger_wake_lock);
693	debugfs_remove(battery->debugfs_entry);
694	kfree(battery);
695	return 0;
696}
697
698static int android_bat_suspend(struct device *dev)
699{
700	struct android_bat_data *battery = dev_get_drvdata(dev);
701
702	cancel_work_sync(&battery->monitor_work);
703	android_bat_monitor_set_alarm(
704		battery,
705		battery->charge_source == CHARGE_SOURCE_NONE ?
706		SLOW_POLL : FAST_POLL);
707	return 0;
708}
709
710static void android_bat_resume(struct device *dev)
711{
712	struct android_bat_data *battery = dev_get_drvdata(dev);
713
714	android_bat_monitor_set_alarm(battery, FAST_POLL);
715	return;
716}
717
718static const struct dev_pm_ops android_bat_pm_ops = {
719	.prepare = android_bat_suspend,
720	.complete = android_bat_resume,
721};
722
723static struct platform_driver android_bat_driver = {
724	.driver = {
725		.name = "android-battery",
726		.owner = THIS_MODULE,
727		.pm = &android_bat_pm_ops,
728	},
729	.probe = android_bat_probe,
730	.remove = __devexit_p(android_bat_remove),
731};
732
733static int __init android_bat_init(void)
734{
735	return platform_driver_register(&android_bat_driver);
736}
737
738static void __exit android_bat_exit(void)
739{
740	platform_driver_unregister(&android_bat_driver);
741}
742
743late_initcall(android_bat_init);
744module_exit(android_bat_exit);
745
746MODULE_DESCRIPTION("Android battery driver");
747MODULE_LICENSE("GPL");
748