aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/crc32c.h
blob: bd8b44d96bdcbd38e764ddfa7b8d6cbed43ebe5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
#ifndef _LINUX_CRC32C_H
#define _LINUX_CRC32C_H

#include <linux/types.h>

extern u32 crc32c(u32 crc, const void *address, unsigned int length);

/* This macro exists for backwards-compatibility. */
#define crc32c_le crc32c

#endif	/* _LINUX_CRC32C_H */
0'>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 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
/*
 * Battery driver for Marvell 88PM860x PMIC
 *
 * Copyright (c) 2012 Marvell International Ltd.
 * Author:	Jett Zhou <jtzhou@marvell.com>
 *		Haojian Zhuang <haojian.zhuang@marvell.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <linux/power_supply.h>
#include <linux/mfd/88pm860x.h>
#include <linux/delay.h>

/* bit definitions of Status Query Interface 2 */
#define STATUS2_CHG			(1 << 2)
#define STATUS2_BAT			(1 << 3)
#define STATUS2_VBUS			(1 << 4)

/* bit definitions of Measurement Enable 1 Register */
#define MEAS1_TINT			(1 << 3)
#define MEAS1_GP1			(1 << 5)

/* bit definitions of Measurement Enable 3 Register */
#define MEAS3_IBAT			(1 << 0)
#define MEAS3_BAT_DET			(1 << 1)
#define MEAS3_CC			(1 << 2)

/* bit definitions of Measurement Off Time Register */
#define MEAS_OFF_SLEEP_EN		(1 << 1)

/* bit definitions of GPADC Bias Current 2 Register */
#define GPBIAS2_GPADC1_SET		(2 << 4)
/* GPADC1 Bias Current value in uA unit */
#define GPBIAS2_GPADC1_UA		((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)

/* bit definitions of GPADC Misc 1 Register */
#define GPMISC1_GPADC_EN		(1 << 0)

/* bit definitions of Charger Control 6 Register */
#define CC6_BAT_DET_GPADC1		1

/* bit definitions of Coulomb Counter Reading Register */
#define CCNT_AVG_SEL			(4 << 3)

/* bit definitions of RTC miscellaneous Register1 */
#define RTC_SOC_5LSB		(0x1F << 3)

/* bit definitions of RTC Register1 */
#define RTC_SOC_3MSB		(0x7)

/* bit definitions of Power up Log register */
#define BAT_WU_LOG			(1<<6)

/* coulomb counter index */
#define CCNT_POS1			0
#define CCNT_POS2			1
#define CCNT_NEG1			2
#define CCNT_NEG2			3
#define CCNT_SPOS			4
#define CCNT_SNEG			5

/* OCV -- Open Circuit Voltage */
#define OCV_MODE_ACTIVE			0
#define OCV_MODE_SLEEP			1

/* Vbat range of CC for measuring Rbat */
#define LOW_BAT_THRESHOLD		3600
#define VBATT_RESISTOR_MIN		3800
#define VBATT_RESISTOR_MAX		4100

/* TBAT for batt, TINT for chip itself */
#define PM860X_TEMP_TINT		(0)
#define PM860X_TEMP_TBAT		(1)

/*
 * Battery temperature based on NTC resistor, defined
 * corresponding resistor value  -- Ohm / C degeree.
 */
#define TBAT_NEG_25D		127773	/* -25 */
#define TBAT_NEG_10D		54564	/* -10 */
#define TBAT_0D			32330	/* 0 */
#define TBAT_10D		19785	/* 10 */
#define TBAT_20D		12468	/* 20 */
#define TBAT_30D		8072	/* 30 */
#define TBAT_40D		5356	/* 40 */

struct pm860x_battery_info {
	struct pm860x_chip *chip;
	struct i2c_client *i2c;
	struct device *dev;

	struct power_supply battery;
	struct mutex lock;
	int status;
	int irq_cc;
	int irq_batt;
	int max_capacity;
	int resistor;		/* Battery Internal Resistor */
	int last_capacity;
	int start_soc;
	unsigned present:1;
	unsigned temp_type:1;	/* TINT or TBAT */
};

struct ccnt {
	unsigned long long int pos;
	unsigned long long int neg;
	unsigned int spos;
	unsigned int sneg;

	int total_chg;		/* mAh(3.6C) */
	int total_dischg;	/* mAh(3.6C) */
};

/*
 * State of Charge.
 * The first number is mAh(=3.6C), and the second number is percent point.
 */
static int array_soc[][2] = {
	{4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
	{4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
	{4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
	{4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
	{3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
	{3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
	{3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
	{3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
	{3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
	{3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
	{3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
	{3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
	{3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
	{3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
	{3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
	{3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
	{3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
	{3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
	{3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
	{3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
};

static struct ccnt ccnt_data;

/*
 * register 1 bit[7:0] -- bit[11:4] of measured value of voltage
 * register 0 bit[3:0] -- bit[3:0] of measured value of voltage
 */
static int measure_12bit_voltage(struct pm860x_battery_info *info,
				 int offset, int *data)
{
	unsigned char buf[2];
	int ret;

	ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
	if (ret < 0)
		return ret;

	*data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
	/* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */
	*data = ((*data & 0xfff) * 9 * 25) >> 9;
	return 0;
}

static int measure_vbatt(struct pm860x_battery_info *info, int state,
			 int *data)
{
	unsigned char buf[5];
	int ret;

	switch (state) {
	case OCV_MODE_ACTIVE:
		ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data);
		if (ret)
			return ret;
		/* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
		*data *= 3;
		break;
	case OCV_MODE_SLEEP:
		/*
		 * voltage value of VBATT in sleep mode is saved in different
		 * registers.
		 * bit[11:10] -- bit[7:6] of LDO9(0x18)
		 * bit[9:8] -- bit[7:6] of LDO8(0x17)
		 * bit[7:6] -- bit[7:6] of LDO7(0x16)
		 * bit[5:4] -- bit[7:6] of LDO6(0x15)
		 * bit[3:0] -- bit[7:4] of LDO5(0x14)
		 */
		ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf);
		if (ret < 0)
			return ret;
		ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
		    | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
		    | (buf[0] >> 4);
		/* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
		*data = ((*data & 0xff) * 27 * 25) >> 9;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/*
 * Return value is signed data.
 * Negative value means discharging, and positive value means charging.
 */
static int measure_current(struct pm860x_battery_info *info, int *data)
{
	unsigned char buf[2];
	short s;
	int ret;

	ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
	if (ret < 0)
		return ret;

	s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
	/* current(mA) = value * 0.125 */
	*data = s >> 3;
	return 0;
}

static int set_charger_current(struct pm860x_battery_info *info, int data,
			       int *old)
{
	int ret;

	if (data < 50 || data > 1600 || !old)
		return -EINVAL;

	data = ((data - 50) / 50) & 0x1f;
	*old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2);
	*old = (*old & 0x1f) * 50 + 50;
	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data);
	if (ret < 0)
		return ret;
	return 0;
}

static int read_ccnt(struct pm860x_battery_info *info, int offset,
		     int *ccnt)
{
	unsigned char buf[2];
	int ret;

	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7);
	if (ret < 0)
		goto out;
	ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf);
	if (ret < 0)
		goto out;