sabresd_battery.c 24.7 KB
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 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
/*
 * sabresd_battery.c - Maxim 8903 USB/Adapter Charger Driver
 *
 * Copyright (C) 2011 Samsung Electronics
 * Copyright (C) 2011-2014 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/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/power_supply.h>
#include <linux/platform_device.h>
#include <linux/power/sabresd_battery.h>
#include <linux/slab.h>

#define	BATTERY_UPDATE_INTERVAL	5 /*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;
	int first_delay_count;
	struct power_supply bat;
	struct power_supply	detect_usb;
};

typedef struct {
	u32 voltage;
	u32 percent;
} battery_capacity , *pbattery_capacity;

static int offset_discharger;
static int offset_charger;
static int offset_usb_charger;

static battery_capacity chargingTable[] = {
	{4050,	99},
	{4040,	98},
	{4020,	97},
	{4010,	96},
	{3990,	95},
	{3980,	94},
	{3970,	93},
	{3960,	92},
	{3950,	91},
	{3940,	90},
	{3930,	85},
	{3920,	81},
	{3910,	77},
	{3900,	73},
	{3890,	70},
	{3860,	65},
	{3830,	60},
	{3780,	55},
	{3760,	50},
	{3740,	45},
	{3720,	40},
	{3700,	35},
	{3680,	30},
	{3660,	25},
	{3640,	20},
	{3620,	17},
	{3600,	14},
	{3580,	13},
	{3560,	12},
	{3540,	11},
	{3520,	10},
	{3500,	9},
	{3480,	8},
	{3460,	7},
	{3440,	6},
	{3430,	5},
	{3420,	4},
	{3020,	0},
};

static battery_capacity dischargingTable[] = {
	{4050, 100},
	{4035,	99},
	{4020,	98},
	{4010,	97},
	{4000,	96},
	{3990,	96},
	{3980,	95},
	{3970,	92},
	{3960,	91},
	{3950,	90},
	{3940,	88},
	{3930,	86},
	{3920,	84},
	{3910,	82},
	{3900,	80},
	{3890,	74},
	{3860,	69},
	{3830,	64},
	{3780,	59},
	{3760,	54},
	{3740,	49},
	{3720,	44},
	{3700,	39},
	{3680,	34},
	{3660,	29},
	{3640,	24},
	{3620,	19},
	{3600,	14},
	{3580,	13},
	{3560,	12},
	{3540,	11},
	{3520,	10},
	{3500,	9},
	{3480,	8},
	{3460,	7},
	{3440,	6},
	{3430,	5},
	{3420,	4},
	{3020,	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,
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
};

extern u32 max11801_read_adc(void);

static void max8903_charger_update_status(struct max8903_data *data)
{
	if (data->ta_in) {
		data->charger_online = 1;
	} else if (data->usb_in) {
		data->usb_charger_online = 1;
	} else {
		data->charger_online = 0;
		data->usb_charger_online = 0;
	}

	if (!data->charger_online && !data->usb_charger_online) {
		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 || data->usb_in) &&
		gpio_get_value(data->pdata->chg) > 0) {
		if (!data->pdata->feature_flag) {
			if (data->percent >= 99)
				data->battery_status = POWER_SUPPLY_STATUS_FULL;
			else
				data->battery_status =
				POWER_SUPPLY_STATUS_NOT_CHARGING;
		} else {
			data->battery_status = POWER_SUPPLY_STATUS_FULL;
		}
	}
}

u32 calibration_voltage(struct max8903_data *data)
{
	u32 voltage_data = 0;
	int i;
	int offset;

	if (!data->charger_online && !data->usb_charger_online)
		offset = offset_discharger;
	else if (data->usb_charger_online)
		offset = offset_usb_charger;
	else if (data->charger_online)
		offset = offset_charger;

	/* simple average */
	for (i = 0; i < ADC_SAMPLE_COUNT; i++)
		voltage_data += max11801_read_adc()-offset;
	voltage_data = voltage_data / ADC_SAMPLE_COUNT;
	dev_dbg(data->dev, "volt: %d\n", voltage_data);

	return voltage_data;
}

static void max8903_battery_update_status(struct max8903_data *data)
{
	if (!data->pdata->feature_flag) {
		data->voltage_uV = calibration_voltage(data);
		data->percent = calibrate_battery_capability_percent(data);
		if (data->percent != data->old_percent) {
			data->old_percent = data->percent;
			power_supply_changed(&data->bat);
		}
		 /*
		  * because boot time gap between led framwork and charger
		  * framwork,when system boots with charger attatched,
		  * charger led framwork loses the first charger online event,
		  * add once extra power_supply_changed can fix this issure
		  */
		if (data->first_delay_count < 200) {
			data->first_delay_count = data->first_delay_count + 1;
			power_supply_changed(&data->bat);
		}
	}
}

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 || di->usb_in) &&
			gpio_get_value(di->pdata->chg) > 0) {
			if (!di->pdata->feature_flag) {
				if (di->percent >= 99)
					di->battery_status =
					POWER_SUPPLY_STATUS_FULL;
				else
					di->battery_status =
					POWER_SUPPLY_STATUS_NOT_CHARGING;
			} else {
				di->battery_status = POWER_SUPPLY_STATUS_FULL;
			}
		}
		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;
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
		if (di->battery_status == POWER_SUPPLY_STATUS_FULL)
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
		else if (di->percent <= 15)
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
		else
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
		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 = false;

	if (pdata->dok)
		ta_in = gpio_get_value(pdata->dok) ? false : true;

	if (ta_in == data->ta_in)
		return IRQ_HANDLED;

	data->ta_in = ta_in;
	dev_info(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ?
			"Connected" : "Disconnected");
	max8903_charger_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 = false;

	if (pdata->uok)
		usb_in = gpio_get_value(pdata->uok) ? false : true;
	if (usb_in == data->usb_in)
		return IRQ_HANDLED;
	data->usb_in = usb_in;
	dev_info(data->dev, "USB Charger %s.\n", usb_in ?
			"Connected" : "Disconnected");
	max8903_charger_update_status(data);
	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);
	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);
	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);
	dev_dbg(data->dev, "battery voltage: %4d mV\n", data->voltage_uV);
	dev_dbg(data->dev, "charger online status: %d\n",
		data->charger_online);
	dev_dbg(data->dev, "battery status : %d\n" , data->battery_status);
	dev_dbg(data->dev, "battery capacity percent: %3d\n", data->percent);
	dev_dbg(data->dev, "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)
{
	int ret;
	unsigned long data;

	ret = strict_strtoul(buf, 10, &data);
	offset_discharger = (int)data;
	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)
{
	int ret;
	unsigned long data;

	ret = strict_strtoul(buf, 10, &data);
	offset_charger = (int)data;
	pr_info("read offset_charger:%04d\n", offset_charger);
	return count;
}

static ssize_t max8903_voltage_offset_usb_charger_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "read offset_usb_charger:%04d\n",
		offset_usb_charger);
}

static ssize_t max8903_voltage_offset_usb_charger_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	int ret;
	unsigned long data;

	ret = strict_strtoul(buf, 10, &data);
	offset_usb_charger = (int)data;
	pr_info("read offset_charger:%04d\n", offset_usb_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 struct device_attribute max8903_usb_charger_dev_attr = {
	.attr = {
		 .name = "max8903_ctl_offset_usb_charger",
		 .mode = S_IRUSR | S_IWUSR,
		 },
	.show = max8903_voltage_offset_usb_charger_show,
	.store = max8903_voltage_offset_usb_charger_store,
};

#if defined(CONFIG_OF)
static const struct of_device_id max8903_dt_ids[] = {
	{ .compatible = "fsl,max8903-charger", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, max8903_dt_ids);

static struct max8903_pdata *max8903_of_populate_pdata(
		struct device *dev)
{
	struct device_node *of_node = dev->of_node;
	struct max8903_pdata *pdata = dev->platform_data;

	if (!of_node || pdata)
		return pdata;

	pdata = devm_kzalloc(dev, sizeof(struct max8903_pdata),
				GFP_KERNEL);
	if (!pdata)
		return pdata;

	if (of_get_property(of_node, "fsl,dcm_always_high", NULL))
		pdata->dcm_always_high = true;
	if (of_get_property(of_node, "fsl,dc_valid", NULL))
		pdata->dc_valid = true;
	if (of_get_property(of_node, "fsl,usb_valid", NULL))
		pdata->usb_valid = true;
	if (of_get_property(of_node, "fsl,adc_disable", NULL))
		pdata->feature_flag = true;

	if (pdata->dc_valid) {
		pdata->dok = of_get_named_gpio(of_node, "dok_input", 0);
		if (!gpio_is_valid(pdata->dok)) {
			dev_err(dev, "pin pdata->dok: invalid gpio %d\n", pdata->dok);
			return NULL;
		}
	}
	if (pdata->usb_valid) {
		pdata->uok = of_get_named_gpio(of_node, "uok_input", 0);
		if (!gpio_is_valid(pdata->uok)) {
			dev_err(dev, "pin pdata->uok: invalid gpio %d\n", pdata->uok);
			return NULL;
		}
	}
	pdata->chg = of_get_named_gpio(of_node, "chg_input", 0);
	if (!gpio_is_valid(pdata->chg)) {
		dev_err(dev, "pin pdata->chg: invalid gpio %d\n", pdata->chg);
		return NULL;
	}
	pdata->flt = of_get_named_gpio(of_node, "flt_input", 0);
	if (!gpio_is_valid(pdata->flt)) {
		dev_err(dev, "pin pdata->flt: invalid gpio %d\n", pdata->flt);
		return NULL;
	}
	/* no need check offset without adc converter */
	if (!pdata->feature_flag) {
		if (of_property_read_u32(of_node, "offset-charger",
			&offset_charger))
			dev_err(dev, "Not setting offset-charger in dts!\n");

		if (of_property_read_u32(of_node, "offset-discharger",
			&offset_discharger))
			dev_err(dev, "Not setting offset-discharger in dts!\n");

		if (of_property_read_u32(of_node, "offset-usb-charger",
			&offset_usb_charger))
			dev_err(dev, "Not setting offset-usb-charger in dts!\n");
	}

	return pdata;
}
#endif

static 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;

	data = devm_kzalloc(dev, sizeof(struct max8903_data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	pdata = pdev->dev.platform_data;
	if (!pdata) {
		pdata = max8903_of_populate_pdata(&pdev->dev);
		if (!pdata)
			return -EINVAL;
	}

	data->first_delay_count = 0;
	data->pdata = pdata;
	data->dev = dev;
	data->usb_in = 0;
	data->ta_in = 0;
	platform_set_drvdata(pdev, data);

	if (pdata->dc_valid == false && pdata->usb_valid == false) {
		dev_err(dev, "No valid power sources.\n");
		ret = -EINVAL;
		goto err;
	}
	if (pdata->dc_valid) {
		if (pdata->dok && pdata->dcm_always_high) {
			gpio = pdata->dok;
			ret = gpio_request_one(gpio, GPIOF_IN, "max8903-DOK");
			if (ret) {
				dev_err(dev, "request max8903-DOK error!!\n");
				goto err;
			}
			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 = pdata->uok;
			ret = gpio_request_one(gpio, GPIOF_IN, "max8903-UOK");
			if (ret) {
				dev_err(dev, "request max8903-UOK error!!\n");
				goto err;
			}
			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) {
		ret = gpio_request_one(pdata->chg, GPIOF_IN, "max8903-CHG");
		if (ret) {
			dev_err(dev, "request max8903-CHG error!!\n");
			goto err;
		}
	}

	if (pdata->flt) {
		ret = gpio_request_one(pdata->flt, GPIOF_IN, "max8903-FLT");
		if (ret) {
			dev_err(dev, "request max8903-FLT error!!\n");
			goto err;
		}
	}

	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 | IRQF_ONESHOT, "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_dc_irq;
		}
	}

	if (pdata->usb_valid) {
		ret = request_threaded_irq(gpio_to_irq(pdata->uok), NULL,
			max8903_usbin, IRQF_TRIGGER_FALLING |
			IRQF_TRIGGER_RISING | IRQF_ONESHOT, "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_usb_irq;
		}
	}

	if (pdata->flt) {
		ret = request_threaded_irq(gpio_to_irq(pdata->flt), NULL,
			max8903_fault, IRQF_TRIGGER_FALLING |
			IRQF_TRIGGER_RISING | IRQF_ONESHOT, "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 | IRQF_ONESHOT, "MAX8903 Status",
			data);
		if (ret) {
			dev_err(dev, "Cannot request irq %d for Status (%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");
	ret = device_create_file(&pdev->dev, &max8903_usb_charger_dev_attr);
	if (ret)
		dev_err(&pdev->dev, "create device file failed!\n");

	device_set_wakeup_capable(&pdev->dev, true);

	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);
	return ret;
}

static int max8903_remove(struct platform_device *pdev)
{
	struct max8903_data *data = platform_get_drvdata(pdev);
	if (data) {
		struct max8903_pdata *pdata = data->pdata;

		cancel_delayed_work_sync(&data->work);
		power_supply_unregister(&data->psy);
		power_supply_unregister(&data->usb);
		power_supply_unregister(&data->bat);

		if (pdata->flt) {
			free_irq(gpio_to_irq(pdata->flt), data);
			gpio_free(pdata->flt);
		}
		if (pdata->usb_valid && pdata->uok) {
			free_irq(gpio_to_irq(pdata->uok), data);
			gpio_free(pdata->uok);
		}
		if (pdata->dc_valid) {
			if (pdata->dok) {
				free_irq(gpio_to_irq(pdata->dok), data);
				gpio_free(pdata->dok);
			} else if (pdata->chg) {
				free_irq(gpio_to_irq(pdata->chg), data);
				gpio_free(pdata->chg);
			}
		}

		device_remove_file(&pdev->dev, &max8903_discharger_dev_attr);
		device_remove_file(&pdev->dev, &max8903_charger_dev_attr);
		device_remove_file(&pdev->dev, &max8903_usb_charger_dev_attr);

		platform_set_drvdata(pdev, NULL);
		kfree(data);
	}

	return 0;
}

static int max8903_suspend(struct platform_device *pdev,
				  pm_message_t state)
{
	struct max8903_data *data = platform_get_drvdata(pdev);
	int irq;
	if (data) {
		struct max8903_pdata *pdata = data->pdata;
		if (pdata) {
			if (pdata->dc_valid && device_may_wakeup(&pdev->dev)) {
				irq = gpio_to_irq(pdata->dok);
				enable_irq_wake(irq);
			}

			if (pdata->usb_valid && device_may_wakeup(&pdev->dev)) {
				irq = gpio_to_irq(pdata->uok);
				enable_irq_wake(irq);
			}
			cancel_delayed_work(&data->work);
		}
	}
	return 0;
}

static int max8903_resume(struct platform_device *pdev)
{
	struct max8903_data *data = platform_get_drvdata(pdev);
	bool ta_in = false;
	bool usb_in = false;
	int irq;

	if (data) {
		struct max8903_pdata *pdata = data->pdata;

		if (pdata) {
			if (pdata->dok)
				ta_in = gpio_get_value(pdata->dok) ? false : true;
			if (pdata->uok)
				usb_in = gpio_get_value(pdata->uok) ? false : true;

			if (ta_in != data->ta_in) {
				data->ta_in = ta_in;
				dev_info(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ?
				"Connected" : "Disconnected");
				max8903_charger_update_status(data);
				power_supply_changed(&data->psy);
			}

			if (usb_in != data->usb_in) {
				data->usb_in = usb_in;
				dev_info(data->dev, "USB Charger %s.\n", usb_in ?
				"Connected" : "Disconnected");
				max8903_charger_update_status(data);
				power_supply_changed(&data->usb);
			}

			if (pdata->dc_valid && device_may_wakeup(&pdev->dev)) {
				irq = gpio_to_irq(pdata->dok);
				disable_irq_wake(irq);
			}
			if (pdata->usb_valid && device_may_wakeup(&pdev->dev)) {
				irq = gpio_to_irq(pdata->uok);
				disable_irq_wake(irq);
			}

			schedule_delayed_work(&data->work,
			BATTERY_UPDATE_INTERVAL);
		}
	}

	return 0;
}

static struct platform_driver max8903_driver = {
	.probe	= max8903_probe,
	.remove	= max8903_remove,
	.suspend = max8903_suspend,
	.resume = max8903_resume,
	.driver = {
		.name	= "max8903-charger",
		.owner	= THIS_MODULE,
		.of_match_table = max8903_dt_ids,
	},
};
module_platform_driver(max8903_driver);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Sabresd Battery Driver");
MODULE_ALIAS("sabresd_battery");