ab8500-debugfs.c 69.4 KB
Newer Older
M
Mattias Wallin 已提交
1 2 3 4 5 6
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
 * License Terms: GNU General Public License v2
 */
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
/*
 * AB8500 register access
 * ======================
 *
 * read:
 * # echo BANK  >  <debugfs>/ab8500/register-bank
 * # echo ADDR  >  <debugfs>/ab8500/register-address
 * # cat <debugfs>/ab8500/register-value
 *
 * write:
 * # echo BANK  >  <debugfs>/ab8500/register-bank
 * # echo ADDR  >  <debugfs>/ab8500/register-address
 * # echo VALUE >  <debugfs>/ab8500/register-value
 *
 * read all registers from a bank:
 * # echo BANK  >  <debugfs>/ab8500/register-bank
 * # cat <debugfs>/ab8500/all-bank-register
 *
 * BANK   target AB8500 register bank
 * ADDR   target AB8500 register address
 * VALUE  decimal or 0x-prefixed hexadecimal
 *
 *
 * User Space notification on AB8500 IRQ
 * =====================================
 *
 * Allows user space entity to be notified when target AB8500 IRQ occurs.
 * When subscribed, a sysfs entry is created in ab8500.i2c platform device.
 * One can pool this file to get target IRQ occurence information.
 *
 * subscribe to an AB8500 IRQ:
 * # echo IRQ  >  <debugfs>/ab8500/irq-subscribe
 *
 * unsubscribe from an AB8500 IRQ:
 * # echo IRQ  >  <debugfs>/ab8500/irq-unsubscribe
 *
 *
 * AB8500 register formated read/write access
 * ==========================================
 *
 * Read:  read data, data>>SHIFT, data&=MASK, output data
 *        [0xABCDEF98] shift=12 mask=0xFFF => 0x00000CDE
 * Write: read data, data &= ~(MASK<<SHIFT), data |= (VALUE<<SHIFT), write data
 *        [0xABCDEF98] shift=12 mask=0xFFF value=0x123 => [0xAB123F98]
 *
 * Usage:
 * # echo "CMD [OPTIONS] BANK ADRESS [VALUE]" > $debugfs/ab8500/hwreg
 *
 * CMD      read      read access
 *          write     write access
 *
 * BANK     target reg bank
 * ADDRESS  target reg address
 * VALUE    (write) value to be updated
 *
 * OPTIONS
 *  -d|-dec            (read) output in decimal
 *  -h|-hexa           (read) output in 0x-hexa (default)
 *  -l|-w|-b           32bit (default), 16bit or 8bit reg access
 *  -m|-mask MASK      0x-hexa mask (default 0xFFFFFFFF)
 *  -s|-shift SHIFT    bit shift value (read:left, write:right)
 *  -o|-offset OFFSET  address offset to add to ADDRESS value
 *
 * Warning: bit shift operation is applied to bit-mask.
 * Warning: bit shift direction depends on read or right command.
 */
M
Mattias Wallin 已提交
73 74 75 76

#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
77
#include <linux/module.h>
M
Mattias Wallin 已提交
78 79
#include <linux/debugfs.h>
#include <linux/platform_device.h>
80 81 82
#include <linux/interrupt.h>
#include <linux/kobject.h>
#include <linux/slab.h>
83
#include <linux/irq.h>
M
Mattias Wallin 已提交
84 85

#include <linux/mfd/abx500.h>
L
Linus Walleij 已提交
86
#include <linux/mfd/abx500/ab8500.h>
87
#include <linux/mfd/abx500/ab8500-gpadc.h>
M
Mattias Wallin 已提交
88

89 90 91 92 93
#ifdef CONFIG_DEBUG_FS
#include <linux/string.h>
#include <linux/ctype.h>
#endif

M
Mattias Wallin 已提交
94 95 96
static u32 debug_bank;
static u32 debug_address;

97
static int irq_ab8500;
98 99
static int irq_first;
static int irq_last;
100 101
static u32 *irq_count;
static int num_irqs;
102

103 104
static struct device_attribute **dev_attr;
static char **event_name;
105

106 107 108 109 110
static u8 avg_sample = SAMPLE_16;
static u8 trig_edge = RISING_EDGE;
static u8 conv_type = ADC_SW;
static u8 trig_timer;

M
Mattias Wallin 已提交
111 112 113 114 115 116 117
/**
 * struct ab8500_reg_range
 * @first: the first address of the range
 * @last: the last address of the range
 * @perm: access permissions for the range
 */
struct ab8500_reg_range {
118 119 120
	u8 first;
	u8 last;
	u8 perm;
M
Mattias Wallin 已提交
121 122 123
};

/**
124
 * struct ab8500_prcmu_ranges
M
Mattias Wallin 已提交
125 126 127 128
 * @num_ranges: the number of ranges in the list
 * @bankid: bank identifier
 * @range: the list of register ranges
 */
129
struct ab8500_prcmu_ranges {
130 131 132
	u8 num_ranges;
	u8 bankid;
	const struct ab8500_reg_range *range;
M
Mattias Wallin 已提交
133 134
};

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* hwreg- "mask" and "shift" entries ressources */
struct hwreg_cfg {
	u32  bank;      /* target bank */
	u32  addr;      /* target address */
	uint fmt;       /* format */
	uint mask;      /* read/write mask, applied before any bit shift */
	int  shift;     /* bit shift (read:right shift, write:left shift */
};
/* fmt bit #0: 0=hexa, 1=dec */
#define REG_FMT_DEC(c) ((c)->fmt & 0x1)
#define REG_FMT_HEX(c) (!REG_FMT_DEC(c))

static struct hwreg_cfg hwreg_cfg = {
	.addr = 0,			/* default: invalid phys addr */
	.fmt = 0,			/* default: 32bit access, hex output */
	.mask = 0xFFFFFFFF,	/* default: no mask */
	.shift = 0,			/* default: no bit shift */
};

M
Mattias Wallin 已提交
154
#define AB8500_NAME_STRING "ab8500"
155
#define AB8500_ADC_NAME_STRING "gpadc"
156
#define AB8500_NUM_BANKS 24
M
Mattias Wallin 已提交
157 158 159

#define AB8500_REV_REG 0x80

160 161 162
static struct ab8500_prcmu_ranges *debug_ranges;

struct ab8500_prcmu_ranges ab8500_debug_ranges[AB8500_NUM_BANKS] = {
163 164
	[0x0] = {
		.num_ranges = 0,
165
		.range = NULL,
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
	},
	[AB8500_SYS_CTRL1_BLOCK] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x02,
			},
			{
				.first = 0x42,
				.last = 0x42,
			},
			{
				.first = 0x80,
				.last = 0x81,
			},
		},
	},
	[AB8500_SYS_CTRL2_BLOCK] = {
		.num_ranges = 4,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x0D,
			},
			{
				.first = 0x0F,
				.last = 0x17,
			},
			{
				.first = 0x30,
				.last = 0x30,
			},
			{
				.first = 0x32,
				.last = 0x33,
			},
		},
	},
	[AB8500_REGU_CTRL1] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x03,
				.last = 0x10,
			},
			{
				.first = 0x80,
				.last = 0x84,
			},
		},
	},
	[AB8500_REGU_CTRL2] = {
		.num_ranges = 5,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x15,
			},
			{
				.first = 0x17,
				.last = 0x19,
			},
			{
				.first = 0x1B,
				.last = 0x1D,
			},
			{
				.first = 0x1F,
				.last = 0x22,
			},
			{
				.first = 0x40,
				.last = 0x44,
			},
			/* 0x80-0x8B is SIM registers and should
			 * not be accessed from here */
		},
	},
	[AB8500_USB] = {
		.num_ranges = 2,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x83,
			},
			{
				.first = 0x87,
				.last = 0x8A,
			},
		},
	},
	[AB8500_TVOUT] = {
		.num_ranges = 9,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x12,
			},
			{
				.first = 0x15,
				.last = 0x17,
			},
			{
				.first = 0x19,
				.last = 0x21,
			},
			{
				.first = 0x27,
				.last = 0x2C,
			},
			{
				.first = 0x41,
				.last = 0x41,
			},
			{
				.first = 0x45,
				.last = 0x5B,
			},
			{
				.first = 0x5D,
				.last = 0x5D,
			},
			{
				.first = 0x69,
				.last = 0x69,
			},
			{
				.first = 0x80,
				.last = 0x81,
			},
		},
	},
	[AB8500_DBI] = {
		.num_ranges = 0,
305
		.range = NULL,
306 307 308 309 310 311 312 313 314 315 316 317
	},
	[AB8500_ECI_AV_ACC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[0x9] = {
		.num_ranges = 0,
318
		.range = NULL,
319 320 321 322 323 324 325 326 327 328 329
	},
	[AB8500_GPADC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x08,
			},
		},
	},
	[AB8500_CHARGER] = {
330
		.num_ranges = 9,
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
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x03,
			},
			{
				.first = 0x05,
				.last = 0x05,
			},
			{
				.first = 0x40,
				.last = 0x40,
			},
			{
				.first = 0x42,
				.last = 0x42,
			},
			{
				.first = 0x44,
				.last = 0x44,
			},
			{
				.first = 0x50,
				.last = 0x55,
			},
			{
				.first = 0x80,
				.last = 0x82,
			},
			{
				.first = 0xC0,
				.last = 0xC2,
			},
364 365
			{
				.first = 0xf5,
366
				.last = 0xf6,
367
			},
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		},
	},
	[AB8500_GAS_GAUGE] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x07,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	[AB8500_DEVELOPMENT] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
		},
	},
	[AB8500_DEBUG] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x05,
				.last = 0x07,
			},
		},
	},
405 406 407 408 409 410 411 412 413 414 415
	[AB8500_AUDIO] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x6F,
			},
		},
	},
	[AB8500_INTERRUPT] = {
		.num_ranges = 0,
416
		.range = NULL,
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
	},
	[AB8500_RTC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x0F,
			},
		},
	},
	[AB8500_MISC] = {
		.num_ranges = 8,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x05,
			},
			{
				.first = 0x10,
				.last = 0x15,
			},
			{
				.first = 0x20,
				.last = 0x25,
			},
			{
				.first = 0x30,
				.last = 0x35,
			},
			{
				.first = 0x40,
				.last = 0x45,
			},
			{
				.first = 0x50,
				.last = 0x50,
			},
			{
				.first = 0x60,
				.last = 0x67,
			},
			{
				.first = 0x80,
				.last = 0x80,
			},
		},
	},
	[0x11] = {
		.num_ranges = 0,
466
		.range = NULL,
467 468 469
	},
	[0x12] = {
		.num_ranges = 0,
470
		.range = NULL,
471 472 473
	},
	[0x13] = {
		.num_ranges = 0,
474
		.range = NULL,
475 476 477
	},
	[0x14] = {
		.num_ranges = 0,
478
		.range = NULL,
479 480 481 482 483 484 485 486 487 488
	},
	[AB8500_OTP_EMUL] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x01,
				.last = 0x0F,
			},
		},
	},
M
Mattias Wallin 已提交
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
struct ab8500_prcmu_ranges ab8505_debug_ranges[AB8500_NUM_BANKS] = {
	[0x0] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_SYS_CTRL1_BLOCK] = {
		.num_ranges = 5,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x04,
			},
			{
				.first = 0x42,
				.last = 0x42,
			},
			{
				.first = 0x52,
				.last = 0x52,
			},
			{
				.first = 0x54,
				.last = 0x57,
			},
			{
				.first = 0x80,
				.last = 0x83,
			},
		},
	},
	[AB8500_SYS_CTRL2_BLOCK] = {
		.num_ranges = 5,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x0D,
			},
			{
				.first = 0x0F,
				.last = 0x17,
			},
			{
				.first = 0x20,
				.last = 0x20,
			},
			{
				.first = 0x30,
				.last = 0x30,
			},
			{
				.first = 0x32,
				.last = 0x3A,
			},
		},
	},
	[AB8500_REGU_CTRL1] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x03,
				.last = 0x11,
			},
			{
				.first = 0x80,
				.last = 0x86,
			},
		},
	},
	[AB8500_REGU_CTRL2] = {
		.num_ranges = 6,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x06,
			},
			{
				.first = 0x08,
				.last = 0x15,
			},
			{
				.first = 0x17,
				.last = 0x19,
			},
			{
				.first = 0x1B,
				.last = 0x1D,
			},
			{
				.first = 0x1F,
				.last = 0x30,
			},
			{
				.first = 0x40,
				.last = 0x48,
			},
			/* 0x80-0x8B is SIM registers and should
			 * not be accessed from here */
		},
	},
	[AB8500_USB] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x83,
			},
			{
				.first = 0x87,
				.last = 0x8A,
			},
			{
				.first = 0x91,
				.last = 0x94,
			},
		},
	},
	[AB8500_TVOUT] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_DBI] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_ECI_AV_ACC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[AB8500_RESERVED] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_GPADC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x08,
			},
		},
	},
	[AB8500_CHARGER] = {
		.num_ranges = 9,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x02,
				.last = 0x03,
			},
			{
				.first = 0x05,
				.last = 0x05,
			},
			{
				.first = 0x40,
				.last = 0x44,
			},
			{
				.first = 0x50,
				.last = 0x57,
			},
			{
				.first = 0x60,
				.last = 0x60,
			},
			{
				.first = 0xA0,
				.last = 0xA7,
			},
			{
				.first = 0xAF,
				.last = 0xB2,
			},
			{
				.first = 0xC0,
				.last = 0xC2,
			},
			{
				.first = 0xF5,
				.last = 0xF5,
			},
		},
	},
	[AB8500_GAS_GAUGE] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x07,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
	[AB8500_AUDIO] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x83,
			},
		},
	},
	[AB8500_INTERRUPT] = {
		.num_ranges = 11,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x04,
			},
			{
				.first = 0x06,
				.last = 0x07,
			},
			{
				.first = 0x09,
				.last = 0x09,
			},
			{
				.first = 0x0B,
				.last = 0x0C,
			},
			{
				.first = 0x12,
				.last = 0x15,
			},
			{
				.first = 0x18,
				.last = 0x18,
			},
			/* Latch registers should not be read here */
			{
				.first = 0x40,
				.last = 0x44,
			},
			{
				.first = 0x46,
				.last = 0x49,
			},
			{
				.first = 0x4B,
				.last = 0x4D,
			},
			{
				.first = 0x52,
				.last = 0x55,
			},
			{
				.first = 0x58,
				.last = 0x58,
			},
			/* LatchHier registers should not be read here */
		},
	},
	[AB8500_RTC] = {
		.num_ranges = 2,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x14,
			},
			{
				.first = 0x16,
				.last = 0x17,
			},
		},
	},
	[AB8500_MISC] = {
		.num_ranges = 8,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x06,
			},
			{
				.first = 0x10,
				.last = 0x16,
			},
			{
				.first = 0x20,
				.last = 0x26,
			},
			{
				.first = 0x30,
				.last = 0x36,
			},
			{
				.first = 0x40,
				.last = 0x46,
			},
			{
				.first = 0x50,
				.last = 0x50,
			},
			{
				.first = 0x60,
				.last = 0x6B,
			},
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[AB8500_DEVELOPMENT] = {
		.num_ranges = 2,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x05,
				.last = 0x05,
			},
		},
	},
	[AB8500_DEBUG] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x05,
				.last = 0x07,
			},
		},
	},
	[AB8500_PROD_TEST] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_STE_TEST] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_OTP_EMUL] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x01,
				.last = 0x15,
			},
		},
	},
};

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 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
struct ab8500_prcmu_ranges ab8540_debug_ranges[AB8500_NUM_BANKS] = {
	[AB8500_M_FSM_RANK] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x0B,
			},
		},
	},
	[AB8500_SYS_CTRL1_BLOCK] = {
		.num_ranges = 6,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x04,
			},
			{
				.first = 0x42,
				.last = 0x42,
			},
			{
				.first = 0x50,
				.last = 0x54,
			},
			{
				.first = 0x57,
				.last = 0x57,
			},
			{
				.first = 0x80,
				.last = 0x83,
			},
			{
				.first = 0x90,
				.last = 0x90,
			},
		},
	},
	[AB8500_SYS_CTRL2_BLOCK] = {
		.num_ranges = 5,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x0D,
			},
			{
				.first = 0x0F,
				.last = 0x10,
			},
			{
				.first = 0x20,
				.last = 0x21,
			},
			{
				.first = 0x32,
				.last = 0x3C,
			},
			{
				.first = 0x40,
				.last = 0x42,
			},
		},
	},
	[AB8500_REGU_CTRL1] = {
		.num_ranges = 4,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x03,
				.last = 0x15,
			},
			{
				.first = 0x20,
				.last = 0x20,
			},
			{
				.first = 0x80,
				.last = 0x85,
			},
			{
				.first = 0x87,
				.last = 0x88,
			},
		},
	},
	[AB8500_REGU_CTRL2] = {
		.num_ranges = 8,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x06,
			},
			{
				.first = 0x08,
				.last = 0x15,
			},
			{
				.first = 0x17,
				.last = 0x19,
			},
			{
				.first = 0x1B,
				.last = 0x1D,
			},
			{
				.first = 0x1F,
				.last = 0x2F,
			},
			{
				.first = 0x31,
				.last = 0x3A,
			},
			{
				.first = 0x43,
				.last = 0x44,
			},
			{
				.first = 0x48,
				.last = 0x49,
			},
		},
	},
	[AB8500_USB] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x83,
			},
			{
				.first = 0x87,
				.last = 0x8A,
			},
			{
				.first = 0x91,
				.last = 0x94,
			},
		},
	},
	[AB8500_TVOUT] = {
		.num_ranges = 0,
		.range = NULL
	},
	[AB8500_DBI] = {
		.num_ranges = 4,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x07,
			},
			{
				.first = 0x10,
				.last = 0x11,
			},
			{
				.first = 0x20,
				.last = 0x21,
			},
			{
				.first = 0x30,
				.last = 0x43,
			},
		},
	},
	[AB8500_ECI_AV_ACC] = {
		.num_ranges = 2,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x03,
			},
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[AB8500_RESERVED] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_GPADC] = {
		.num_ranges = 4,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x01,
			},
			{
				.first = 0x04,
				.last = 0x06,
			},
			{
				.first = 0x09,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
	[AB8500_CHARGER] = {
		.num_ranges = 10,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x02,
				.last = 0x05,
			},
			{
				.first = 0x40,
				.last = 0x44,
			},
			{
				.first = 0x50,
				.last = 0x57,
			},
			{
				.first = 0x60,
				.last = 0x60,
			},
			{
				.first = 0x70,
				.last = 0x70,
			},
			{
				.first = 0xA0,
				.last = 0xA9,
			},
			{
				.first = 0xAF,
				.last = 0xB2,
			},
			{
				.first = 0xC0,
				.last = 0xC6,
			},
			{
				.first = 0xF5,
				.last = 0xF5,
			},
		},
	},
	[AB8500_GAS_GAUGE] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x07,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
	[AB8500_AUDIO] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x9f,
			},
		},
	},
	[AB8500_INTERRUPT] = {
		.num_ranges = 6,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x05,
			},
			{
				.first = 0x0B,
				.last = 0x0D,
			},
			{
				.first = 0x12,
				.last = 0x20,
			},
			/* Latch registers should not be read here */
			{
				.first = 0x40,
				.last = 0x45,
			},
			{
				.first = 0x4B,
				.last = 0x4D,
			},
			{
				.first = 0x52,
				.last = 0x60,
			},
			/* LatchHier registers should not be read here */
		},
	},
	[AB8500_RTC] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x07,
			},
			{
				.first = 0x0B,
				.last = 0x18,
			},
			{
				.first = 0x20,
				.last = 0x25,
			},
		},
	},
	[AB8500_MISC] = {
		.num_ranges = 9,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x06,
			},
			{
				.first = 0x10,
				.last = 0x16,
			},
			{
				.first = 0x20,
				.last = 0x26,
			},
			{
				.first = 0x30,
				.last = 0x36,
			},
			{
				.first = 0x40,
				.last = 0x49,
			},
			{
				.first = 0x50,
				.last = 0x50,
			},
			{
				.first = 0x60,
				.last = 0x6B,
			},
			{
				.first = 0x70,
				.last = 0x74,
			},
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[AB8500_DEVELOPMENT] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x01,
			},
			{
				.first = 0x06,
				.last = 0x06,
			},
			{
				.first = 0x10,
				.last = 0x21,
			},
		},
	},
	[AB8500_DEBUG] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x01,
				.last = 0x0C,
			},
			{
				.first = 0x0E,
				.last = 0x11,
			},
			{
				.first = 0x80,
				.last = 0x81,
			},
		},
	},
	[AB8500_PROD_TEST] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_STE_TEST] = {
		.num_ranges = 0,
		.range = NULL,
	},
	[AB8500_OTP_EMUL] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x3F,
			},
		},
	},
};


1266 1267 1268 1269
static irqreturn_t ab8500_debug_handler(int irq, void *data)
{
	char buf[16];
	struct kobject *kobj = (struct kobject *)data;
1270
	unsigned int irq_abb = irq - irq_first;
1271

1272
	if (irq_abb < num_irqs)
1273
		irq_count[irq_abb]++;
1274 1275
	/*
	 * This makes it possible to use poll for events (POLLPRI | POLLERR)
1276
	 * from userspace on sysfs file named <irq-nr>
1277
	 */
1278
	sprintf(buf, "%d", irq);
1279 1280 1281 1282 1283
	sysfs_notify(kobj, NULL, buf);

	return IRQ_HANDLED;
}

1284 1285 1286
/* Prints to seq_file or log_buf */
static int ab8500_registers_print(struct device *dev, u32 bank,
				struct seq_file *s)
M
Mattias Wallin 已提交
1287
{
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	unsigned int i;

	for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
		u32 reg;

		for (reg = debug_ranges[bank].range[i].first;
			reg <= debug_ranges[bank].range[i].last;
			reg++) {
			u8 value;
			int err;

			err = abx500_get_register_interruptible(dev,
				(u8)bank, (u8)reg, &value);
			if (err < 0) {
				dev_err(dev, "ab->read fail %d\n", err);
				return err;
			}

1306
			if (s) {
1307
				err = seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
1308 1309 1310 1311 1312 1313 1314
					bank, reg, value);
				if (err < 0) {
					/* Error is not returned here since
					 * the output is wanted in any case */
					return 0;
				}
			} else {
1315 1316
				printk(KERN_INFO" [0x%02X/0x%02X]: 0x%02X\n",
					bank, reg, value);
1317 1318 1319 1320
			}
		}
	}
	return 0;
M
Mattias Wallin 已提交
1321 1322
}

1323 1324 1325 1326 1327 1328 1329
static int ab8500_print_bank_registers(struct seq_file *s, void *p)
{
	struct device *dev = s->private;
	u32 bank = debug_bank;

	seq_printf(s, AB8500_NAME_STRING " register values:\n");

1330
	seq_printf(s, " bank 0x%02X:\n", bank);
1331 1332 1333 1334 1335

	ab8500_registers_print(dev, bank, s);
	return 0;
}

M
Mattias Wallin 已提交
1336 1337
static int ab8500_registers_open(struct inode *inode, struct file *file)
{
1338
	return single_open(file, ab8500_print_bank_registers, inode->i_private);
M
Mattias Wallin 已提交
1339 1340 1341
}

static const struct file_operations ab8500_registers_fops = {
1342 1343 1344 1345 1346
	.open = ab8500_registers_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
1347 1348
};

1349 1350 1351 1352 1353 1354 1355 1356
static int ab8500_print_all_banks(struct seq_file *s, void *p)
{
	struct device *dev = s->private;
	unsigned int i;
	int err;

	seq_printf(s, AB8500_NAME_STRING " register values:\n");

1357
	for (i = 0; i < AB8500_NUM_BANKS; i++) {
1358
		err = seq_printf(s, " bank 0x%02X:\n", i);
1359 1360 1361 1362 1363 1364

		ab8500_registers_print(dev, i, s);
	}
	return 0;
}

1365 1366 1367 1368 1369 1370 1371 1372
/* Dump registers to kernel log */
void ab8500_dump_all_banks(struct device *dev)
{
	unsigned int i;

	printk(KERN_INFO"ab8500 register values:\n");

	for (i = 1; i < AB8500_NUM_BANKS; i++) {
1373
		printk(KERN_INFO" bank 0x%02X:\n", i);
1374 1375 1376 1377
		ab8500_registers_print(dev, i, NULL);
	}
}

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
/* Space for 500 registers. */
#define DUMP_MAX_REGS 700
struct ab8500_register_dump
{
	u8 bank;
	u8 reg;
	u8 value;
} ab8500_complete_register_dump[DUMP_MAX_REGS];

extern int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size);

/* This shall only be called upon kernel panic! */
void ab8500_dump_all_banks_to_mem(void)
{
	int i, r = 0;
	u8 bank;
1394
	int err = 0;
1395 1396 1397 1398

	pr_info("Saving all ABB registers at \"ab8500_complete_register_dump\" "
		"for crash analyze.\n");

1399
	for (bank = 0; bank < AB8500_NUM_BANKS; bank++) {
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
		for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
			u8 reg;

			for (reg = debug_ranges[bank].range[i].first;
			     reg <= debug_ranges[bank].range[i].last;
			     reg++) {
				u8 value;

				err = prcmu_abb_read(bank, reg, &value, 1);

1410 1411 1412
				if (err < 0)
					goto out;

1413 1414 1415 1416 1417 1418 1419 1420 1421
				ab8500_complete_register_dump[r].bank = bank;
				ab8500_complete_register_dump[r].reg = reg;
				ab8500_complete_register_dump[r].value = value;

				r++;

				if (r >= DUMP_MAX_REGS) {
					pr_err("%s: too many register to dump!\n",
						__func__);
1422 1423
					err = -EINVAL;
					goto out;
1424 1425 1426 1427
				}
			}
		}
	}
1428 1429 1430 1431 1432
out:
	if (err >= 0)
		pr_info("Saved all ABB registers.\n");
	else
		pr_info("Failed to save all ABB registers.\n");
1433 1434
}

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
static int ab8500_all_banks_open(struct inode *inode, struct file *file)
{
	struct seq_file *s;
	int err;

	err = single_open(file, ab8500_print_all_banks, inode->i_private);
	if (!err) {
		/* Default buf size in seq_read is not enough */
		s = (struct seq_file *)file->private_data;
		s->size = (PAGE_SIZE * 2);
		s->buf = kmalloc(s->size, GFP_KERNEL);
		if (!s->buf) {
			single_release(inode, file);
			err = -ENOMEM;
		}
	}
	return err;
}

static const struct file_operations ab8500_all_banks_fops = {
	.open = ab8500_all_banks_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

M
Mattias Wallin 已提交
1462 1463
static int ab8500_bank_print(struct seq_file *s, void *p)
{
1464
	return seq_printf(s, "0x%02X\n", debug_bank);
M
Mattias Wallin 已提交
1465 1466 1467 1468
}

static int ab8500_bank_open(struct inode *inode, struct file *file)
{
1469
	return single_open(file, ab8500_bank_print, inode->i_private);
M
Mattias Wallin 已提交
1470 1471 1472
}

static ssize_t ab8500_bank_write(struct file *file,
1473 1474
	const char __user *user_buf,
	size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
1475
{
1476 1477 1478 1479
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_bank;
	int err;

1480
	err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
1481
	if (err)
1482
		return err;
1483 1484 1485 1486 1487 1488 1489 1490

	if (user_bank >= AB8500_NUM_BANKS) {
		dev_err(dev, "debugfs error input > number of banks\n");
		return -EINVAL;
	}

	debug_bank = user_bank;

1491
	return count;
M
Mattias Wallin 已提交
1492 1493 1494 1495
}

static int ab8500_address_print(struct seq_file *s, void *p)
{
1496
	return seq_printf(s, "0x%02X\n", debug_address);
M
Mattias Wallin 已提交
1497 1498 1499 1500
}

static int ab8500_address_open(struct inode *inode, struct file *file)
{
1501
	return single_open(file, ab8500_address_print, inode->i_private);
M
Mattias Wallin 已提交
1502 1503 1504
}

static ssize_t ab8500_address_write(struct file *file,
1505 1506
				    const char __user *user_buf,
				    size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
1507
{
1508 1509 1510 1511
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_address;
	int err;

1512
	err = kstrtoul_from_user(user_buf, count, 0, &user_address);
1513
	if (err)
1514 1515
		return err;

1516 1517 1518 1519 1520
	if (user_address > 0xff) {
		dev_err(dev, "debugfs error input > 0xff\n");
		return -EINVAL;
	}
	debug_address = user_address;
1521

1522
	return count;
M
Mattias Wallin 已提交
1523 1524 1525 1526
}

static int ab8500_val_print(struct seq_file *s, void *p)
{
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
	struct device *dev = s->private;
	int ret;
	u8 regvalue;

	ret = abx500_get_register_interruptible(dev,
		(u8)debug_bank, (u8)debug_address, &regvalue);
	if (ret < 0) {
		dev_err(dev, "abx500_get_reg fail %d, %d\n",
			ret, __LINE__);
		return -EINVAL;
	}
	seq_printf(s, "0x%02X\n", regvalue);

	return 0;
M
Mattias Wallin 已提交
1541 1542 1543 1544
}

static int ab8500_val_open(struct inode *inode, struct file *file)
{
1545
	return single_open(file, ab8500_val_print, inode->i_private);
M
Mattias Wallin 已提交
1546 1547 1548
}

static ssize_t ab8500_val_write(struct file *file,
1549 1550
				const char __user *user_buf,
				size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
1551
{
1552 1553 1554 1555
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_val;
	int err;

1556
	err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1557
	if (err)
1558 1559
		return err;

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	if (user_val > 0xff) {
		dev_err(dev, "debugfs error input > 0xff\n");
		return -EINVAL;
	}
	err = abx500_set_register_interruptible(dev,
		(u8)debug_bank, debug_address, (u8)user_val);
	if (err < 0) {
		printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
		return -EINVAL;
	}

1571
	return count;
M
Mattias Wallin 已提交
1572 1573
}

1574 1575 1576 1577
/*
 * Interrupt status
 */
static u32 num_interrupts[AB8500_MAX_NR_IRQS];
1578
static u32 num_wake_interrupts[AB8500_MAX_NR_IRQS];
1579 1580
static int num_interrupt_lines;

1581 1582 1583 1584 1585
bool __attribute__((weak)) suspend_test_wake_cause_interrupt_is_mine(u32 my_int)
{
	return false;
}

1586 1587
void ab8500_debug_register_interrupt(int line)
{
1588
	if (line < num_interrupt_lines) {
1589
		num_interrupts[line]++;
1590
		if (suspend_test_wake_cause_interrupt_is_mine(irq_ab8500))
1591 1592
			num_wake_interrupts[line]++;
	}
1593 1594 1595 1596 1597 1598
}

static int ab8500_interrupts_print(struct seq_file *s, void *p)
{
	int line;

1599 1600 1601 1602 1603
	seq_printf(s, "name: number:  number of: wake:\n");

	for (line = 0; line < num_interrupt_lines; line++) {
		struct irq_desc *desc = irq_to_desc(line + irq_first);
		struct irqaction *action = desc->action;
1604

1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		seq_printf(s, "%3i:  %6i %4i", line,
			   num_interrupts[line],
			   num_wake_interrupts[line]);

		if (desc && desc->name)
			seq_printf(s, "-%-8s", desc->name);
		if (action) {
			seq_printf(s, "  %s", action->name);
			while ((action = action->next) != NULL)
				seq_printf(s, ", %s", action->name);
		}
		seq_putc(s, '\n');
	}
1618 1619 1620 1621 1622 1623 1624 1625 1626

	return 0;
}

static int ab8500_interrupts_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_interrupts_print, inode->i_private);
}

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
/*
 * - HWREG DB8500 formated routines
 */
static int ab8500_hwreg_print(struct seq_file *s, void *d)
{
	struct device *dev = s->private;
	int ret;
	u8 regvalue;

	ret = abx500_get_register_interruptible(dev,
		(u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, &regvalue);
	if (ret < 0) {
		dev_err(dev, "abx500_get_reg fail %d, %d\n",
			ret, __LINE__);
		return -EINVAL;
	}

	if (hwreg_cfg.shift >= 0)
		regvalue >>= hwreg_cfg.shift;
	else
		regvalue <<= -hwreg_cfg.shift;
	regvalue &= hwreg_cfg.mask;

	if (REG_FMT_DEC(&hwreg_cfg))
		seq_printf(s, "%d\n", regvalue);
	else
		seq_printf(s, "0x%02X\n", regvalue);
	return 0;
}

static int ab8500_hwreg_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_hwreg_print, inode->i_private);
}

1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
#define AB8500_SUPPLY_CONTROL_CONFIG_1 0x01
#define AB8500_SUPPLY_CONTROL_REG 0x00
#define AB8500_FIRST_SIM_REG 0x80
#define AB8500_LAST_SIM_REG 0x8B
#define AB8505_LAST_SIM_REG 0x8C

static int ab8500_print_modem_registers(struct seq_file *s, void *p)
{
	struct device *dev = s->private;
	struct ab8500 *ab8500;
	int err;
	u8 value;
	u8 orig_value;
	u32 bank = AB8500_REGU_CTRL2;
	u32 last_sim_reg = AB8500_LAST_SIM_REG;
	u32 reg;

	ab8500 = dev_get_drvdata(dev->parent);
	dev_warn(dev, "WARNING! This operation can interfer with modem side\n"
		"and should only be done with care\n");

	err = abx500_get_register_interruptible(dev,
		AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, &orig_value);
	if (err < 0) {
		dev_err(dev, "ab->read fail %d\n", err);
		return err;
	}
	/* Config 1 will allow APE side to read SIM registers */
	err = abx500_set_register_interruptible(dev,
		AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG,
		AB8500_SUPPLY_CONTROL_CONFIG_1);
	if (err < 0) {
		dev_err(dev, "ab->write fail %d\n", err);
		return err;
	}

	seq_printf(s, " bank 0x%02X:\n", bank);

	if (is_ab9540(ab8500) || is_ab8505(ab8500))
		last_sim_reg = AB8505_LAST_SIM_REG;

	for (reg = AB8500_FIRST_SIM_REG; reg <= last_sim_reg; reg++) {
		err = abx500_get_register_interruptible(dev,
			bank, reg, &value);
		if (err < 0) {
			dev_err(dev, "ab->read fail %d\n", err);
			return err;
		}
		err = seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
			bank, reg, value);
	}
	err = abx500_set_register_interruptible(dev,
		AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, orig_value);
	if (err < 0) {
		dev_err(dev, "ab->write fail %d\n", err);
		return err;
	}
	return 0;
}

static int ab8500_modem_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_print_modem_registers, inode->i_private);
}

static const struct file_operations ab8500_modem_fops = {
	.open = ab8500_modem_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

1735 1736 1737 1738 1739 1740
static int ab8500_gpadc_bat_ctrl_print(struct seq_file *s, void *p)
{
	int bat_ctrl_raw;
	int bat_ctrl_convert;
	struct ab8500_gpadc *gpadc;

1741
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1742 1743
	bat_ctrl_raw = ab8500_gpadc_read_raw(gpadc, BAT_CTRL,
		avg_sample, trig_edge, trig_timer, conv_type);
1744
	bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1745
		BAT_CTRL, bat_ctrl_raw);
1746 1747

	return seq_printf(s, "%d,0x%X\n",
1748
		bat_ctrl_convert, bat_ctrl_raw);
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
}

static int ab8500_gpadc_bat_ctrl_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_bat_ctrl_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_bat_ctrl_fops = {
	.open = ab8500_gpadc_bat_ctrl_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_btemp_ball_print(struct seq_file *s, void *p)
{
	int btemp_ball_raw;
	int btemp_ball_convert;
	struct ab8500_gpadc *gpadc;

1770
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1771 1772
	btemp_ball_raw = ab8500_gpadc_read_raw(gpadc, BTEMP_BALL,
		avg_sample, trig_edge, trig_timer, conv_type);
1773
	btemp_ball_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL,
1774
		btemp_ball_raw);
1775 1776

	return seq_printf(s,
1777
		"%d,0x%X\n", btemp_ball_convert, btemp_ball_raw);
1778 1779 1780
}

static int ab8500_gpadc_btemp_ball_open(struct inode *inode,
1781
					struct file *file)
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
{
	return single_open(file, ab8500_gpadc_btemp_ball_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_btemp_ball_fops = {
	.open = ab8500_gpadc_btemp_ball_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_main_charger_v_print(struct seq_file *s, void *p)
{
	int main_charger_v_raw;
	int main_charger_v_convert;
	struct ab8500_gpadc *gpadc;

1800
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1801 1802
	main_charger_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_V,
		avg_sample, trig_edge, trig_timer, conv_type);
1803
	main_charger_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1804
		MAIN_CHARGER_V, main_charger_v_raw);
1805 1806 1807 1808 1809 1810

	return seq_printf(s, "%d,0x%X\n",
			main_charger_v_convert, main_charger_v_raw);
}

static int ab8500_gpadc_main_charger_v_open(struct inode *inode,
1811
					    struct file *file)
1812 1813
{
	return single_open(file, ab8500_gpadc_main_charger_v_print,
1814
		inode->i_private);
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
}

static const struct file_operations ab8500_gpadc_main_charger_v_fops = {
	.open = ab8500_gpadc_main_charger_v_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_acc_detect1_print(struct seq_file *s, void *p)
{
	int acc_detect1_raw;
	int acc_detect1_convert;
	struct ab8500_gpadc *gpadc;

1831
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1832 1833
	acc_detect1_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT1,
		avg_sample, trig_edge, trig_timer, conv_type);
1834
	acc_detect1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ACC_DETECT1,
1835
		acc_detect1_raw);
1836 1837

	return seq_printf(s, "%d,0x%X\n",
1838
		acc_detect1_convert, acc_detect1_raw);
1839 1840 1841
}

static int ab8500_gpadc_acc_detect1_open(struct inode *inode,
1842
					 struct file *file)
1843 1844
{
	return single_open(file, ab8500_gpadc_acc_detect1_print,
1845
		inode->i_private);
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
}

static const struct file_operations ab8500_gpadc_acc_detect1_fops = {
	.open = ab8500_gpadc_acc_detect1_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_acc_detect2_print(struct seq_file *s, void *p)
{
	int acc_detect2_raw;
	int acc_detect2_convert;
	struct ab8500_gpadc *gpadc;

1862
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1863 1864
	acc_detect2_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT2,
		avg_sample, trig_edge, trig_timer, conv_type);
1865
	acc_detect2_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1866
		ACC_DETECT2, acc_detect2_raw);
1867 1868

	return seq_printf(s, "%d,0x%X\n",
1869
		acc_detect2_convert, acc_detect2_raw);
1870 1871 1872 1873 1874 1875
}

static int ab8500_gpadc_acc_detect2_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8500_gpadc_acc_detect2_print,
1876
		inode->i_private);
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
}

static const struct file_operations ab8500_gpadc_acc_detect2_fops = {
	.open = ab8500_gpadc_acc_detect2_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_aux1_print(struct seq_file *s, void *p)
{
	int aux1_raw;
	int aux1_convert;
	struct ab8500_gpadc *gpadc;

1893
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1894 1895
	aux1_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX1,
		avg_sample, trig_edge, trig_timer, conv_type);
1896
	aux1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX1,
1897
		aux1_raw);
1898 1899

	return seq_printf(s, "%d,0x%X\n",
1900
		aux1_convert, aux1_raw);
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
}

static int ab8500_gpadc_aux1_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_aux1_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_aux1_fops = {
	.open = ab8500_gpadc_aux1_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_aux2_print(struct seq_file *s, void *p)
{
	int aux2_raw;
	int aux2_convert;
	struct ab8500_gpadc *gpadc;

1922
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1923 1924
	aux2_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX2,
		avg_sample, trig_edge, trig_timer, conv_type);
1925
	aux2_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX2,
1926
		aux2_raw);
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950

	return seq_printf(s, "%d,0x%X\n",
			aux2_convert, aux2_raw);
}

static int ab8500_gpadc_aux2_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_aux2_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_aux2_fops = {
	.open = ab8500_gpadc_aux2_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_main_bat_v_print(struct seq_file *s, void *p)
{
	int main_bat_v_raw;
	int main_bat_v_convert;
	struct ab8500_gpadc *gpadc;

1951
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1952 1953
	main_bat_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_BAT_V,
		avg_sample, trig_edge, trig_timer, conv_type);
1954
	main_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V,
1955
		main_bat_v_raw);
1956 1957

	return seq_printf(s, "%d,0x%X\n",
1958
		main_bat_v_convert, main_bat_v_raw);
1959 1960 1961
}

static int ab8500_gpadc_main_bat_v_open(struct inode *inode,
1962
					struct file *file)
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
{
	return single_open(file, ab8500_gpadc_main_bat_v_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_main_bat_v_fops = {
	.open = ab8500_gpadc_main_bat_v_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_vbus_v_print(struct seq_file *s, void *p)
{
	int vbus_v_raw;
	int vbus_v_convert;
	struct ab8500_gpadc *gpadc;

1981
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1982 1983
	vbus_v_raw =  ab8500_gpadc_read_raw(gpadc, VBUS_V,
		avg_sample, trig_edge, trig_timer, conv_type);
1984
	vbus_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBUS_V,
1985
		vbus_v_raw);
1986 1987

	return seq_printf(s, "%d,0x%X\n",
1988
		vbus_v_convert, vbus_v_raw);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
}

static int ab8500_gpadc_vbus_v_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_vbus_v_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_vbus_v_fops = {
	.open = ab8500_gpadc_vbus_v_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_main_charger_c_print(struct seq_file *s, void *p)
{
	int main_charger_c_raw;
	int main_charger_c_convert;
	struct ab8500_gpadc *gpadc;

2010
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2011 2012
	main_charger_c_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_C,
		avg_sample, trig_edge, trig_timer, conv_type);
2013
	main_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
2014
		MAIN_CHARGER_C, main_charger_c_raw);
2015 2016

	return seq_printf(s, "%d,0x%X\n",
2017
		main_charger_c_convert, main_charger_c_raw);
2018 2019 2020 2021 2022 2023
}

static int ab8500_gpadc_main_charger_c_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8500_gpadc_main_charger_c_print,
2024
		inode->i_private);
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
}

static const struct file_operations ab8500_gpadc_main_charger_c_fops = {
	.open = ab8500_gpadc_main_charger_c_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_usb_charger_c_print(struct seq_file *s, void *p)
{
	int usb_charger_c_raw;
	int usb_charger_c_convert;
	struct ab8500_gpadc *gpadc;

2041
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2042 2043
	usb_charger_c_raw = ab8500_gpadc_read_raw(gpadc, USB_CHARGER_C,
		avg_sample, trig_edge, trig_timer, conv_type);
2044
	usb_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
2045
		USB_CHARGER_C, usb_charger_c_raw);
2046 2047

	return seq_printf(s, "%d,0x%X\n",
2048
		usb_charger_c_convert, usb_charger_c_raw);
2049 2050 2051 2052 2053 2054
}

static int ab8500_gpadc_usb_charger_c_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8500_gpadc_usb_charger_c_print,
2055
		inode->i_private);
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
}

static const struct file_operations ab8500_gpadc_usb_charger_c_fops = {
	.open = ab8500_gpadc_usb_charger_c_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_bk_bat_v_print(struct seq_file *s, void *p)
{
	int bk_bat_v_raw;
	int bk_bat_v_convert;
	struct ab8500_gpadc *gpadc;

2072
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2073 2074
	bk_bat_v_raw = ab8500_gpadc_read_raw(gpadc, BK_BAT_V,
		avg_sample, trig_edge, trig_timer, conv_type);
2075
	bk_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
2076
		BK_BAT_V, bk_bat_v_raw);
2077 2078

	return seq_printf(s, "%d,0x%X\n",
2079
		bk_bat_v_convert, bk_bat_v_raw);
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
}

static int ab8500_gpadc_bk_bat_v_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_bk_bat_v_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_bk_bat_v_fops = {
	.open = ab8500_gpadc_bk_bat_v_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_die_temp_print(struct seq_file *s, void *p)
{
	int die_temp_raw;
	int die_temp_convert;
	struct ab8500_gpadc *gpadc;

2101
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2102 2103
	die_temp_raw = ab8500_gpadc_read_raw(gpadc, DIE_TEMP,
		avg_sample, trig_edge, trig_timer, conv_type);
2104
	die_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, DIE_TEMP,
2105
		die_temp_raw);
2106 2107

	return seq_printf(s, "%d,0x%X\n",
2108
		die_temp_convert, die_temp_raw);
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
}

static int ab8500_gpadc_die_temp_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_die_temp_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_die_temp_fops = {
	.open = ab8500_gpadc_die_temp_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
static int ab8500_gpadc_usb_id_print(struct seq_file *s, void *p)
{
	int usb_id_raw;
	int usb_id_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	usb_id_raw = ab8500_gpadc_read_raw(gpadc, USB_ID,
		avg_sample, trig_edge, trig_timer, conv_type);
	usb_id_convert = ab8500_gpadc_ad_to_voltage(gpadc, USB_ID,
		usb_id_raw);

	return seq_printf(s, "%d,0x%X\n",
2137
		usb_id_convert, usb_id_raw);
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
}

static int ab8500_gpadc_usb_id_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_usb_id_print, inode->i_private);
}

static const struct file_operations ab8500_gpadc_usb_id_fops = {
	.open = ab8500_gpadc_usb_id_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
static int ab8540_gpadc_xtal_temp_print(struct seq_file *s, void *p)
{
	int xtal_temp_raw;
	int xtal_temp_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	xtal_temp_raw = ab8500_gpadc_read_raw(gpadc, XTAL_TEMP,
		avg_sample, trig_edge, trig_timer, conv_type);
	xtal_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, XTAL_TEMP,
		xtal_temp_raw);

	return seq_printf(s, "%d,0x%X\n",
2166
		xtal_temp_convert, xtal_temp_raw);
2167 2168 2169 2170 2171
}

static int ab8540_gpadc_xtal_temp_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8540_gpadc_xtal_temp_print,
2172
		inode->i_private);
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
}

static const struct file_operations ab8540_gpadc_xtal_temp_fops = {
	.open = ab8540_gpadc_xtal_temp_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_vbat_true_meas_print(struct seq_file *s, void *p)
{
	int vbat_true_meas_raw;
	int vbat_true_meas_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	vbat_true_meas_raw = ab8500_gpadc_read_raw(gpadc, VBAT_TRUE_MEAS,
		avg_sample, trig_edge, trig_timer, conv_type);
	vbat_true_meas_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBAT_TRUE_MEAS,
		vbat_true_meas_raw);

	return seq_printf(s, "%d,0x%X\n",
2196
		vbat_true_meas_convert, vbat_true_meas_raw);
2197 2198 2199 2200 2201 2202
}

static int ab8540_gpadc_vbat_true_meas_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_vbat_true_meas_print,
2203
		inode->i_private);
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
}

static const struct file_operations ab8540_gpadc_vbat_true_meas_fops = {
	.open = ab8540_gpadc_vbat_true_meas_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_bat_ctrl_and_ibat_print(struct seq_file *s, void *p)
{
	int bat_ctrl_raw;
	int bat_ctrl_convert;
	int ibat_raw;
	int ibat_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	bat_ctrl_raw = ab8500_gpadc_double_read_raw(gpadc, BAT_CTRL_AND_IBAT,
		avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw);

	bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc, BAT_CTRL,
		bat_ctrl_raw);
	ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL,
		ibat_raw);

	return seq_printf(s, "%d,0x%X\n"  "%d,0x%X\n",
2232 2233
		bat_ctrl_convert, bat_ctrl_raw,
		ibat_convert, ibat_raw);
2234 2235 2236 2237 2238 2239
}

static int ab8540_gpadc_bat_ctrl_and_ibat_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_bat_ctrl_and_ibat_print,
2240
		inode->i_private);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
}

static const struct file_operations ab8540_gpadc_bat_ctrl_and_ibat_fops = {
	.open = ab8540_gpadc_bat_ctrl_and_ibat_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_vbat_meas_and_ibat_print(struct seq_file *s, void *p)
{
	int vbat_meas_raw;
	int vbat_meas_convert;
	int ibat_raw;
	int ibat_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	vbat_meas_raw = ab8500_gpadc_double_read_raw(gpadc, VBAT_MEAS_AND_IBAT,
		avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw);
	vbat_meas_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V,
		vbat_meas_raw);
	ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL,
		ibat_raw);

	return seq_printf(s, "%d,0x%X\n"  "%d,0x%X\n",
2268 2269
		vbat_meas_convert, vbat_meas_raw,
		ibat_convert, ibat_raw);
2270 2271 2272 2273 2274 2275
}

static int ab8540_gpadc_vbat_meas_and_ibat_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_vbat_meas_and_ibat_print,
2276
		inode->i_private);
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
}

static const struct file_operations ab8540_gpadc_vbat_meas_and_ibat_fops = {
	.open = ab8540_gpadc_vbat_meas_and_ibat_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_vbat_true_meas_and_ibat_print(struct seq_file *s, void *p)
{
	int vbat_true_meas_raw;
	int vbat_true_meas_convert;
	int ibat_raw;
	int ibat_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	vbat_true_meas_raw = ab8500_gpadc_double_read_raw(gpadc,
			VBAT_TRUE_MEAS_AND_IBAT, avg_sample, trig_edge,
			trig_timer, conv_type, &ibat_raw);
	vbat_true_meas_convert = ab8500_gpadc_ad_to_voltage(gpadc,
			VBAT_TRUE_MEAS, vbat_true_meas_raw);
	ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL,
		ibat_raw);

	return seq_printf(s, "%d,0x%X\n"  "%d,0x%X\n",
2305 2306
		vbat_true_meas_convert, vbat_true_meas_raw,
		ibat_convert, ibat_raw);
2307 2308 2309 2310 2311 2312
}

static int ab8540_gpadc_vbat_true_meas_and_ibat_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_vbat_true_meas_and_ibat_print,
2313
		inode->i_private);
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
}

static const struct file_operations ab8540_gpadc_vbat_true_meas_and_ibat_fops = {
	.open = ab8540_gpadc_vbat_true_meas_and_ibat_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_bat_temp_and_ibat_print(struct seq_file *s, void *p)
{
	int bat_temp_raw;
	int bat_temp_convert;
	int ibat_raw;
	int ibat_convert;
	struct ab8500_gpadc *gpadc;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	bat_temp_raw = ab8500_gpadc_double_read_raw(gpadc, BAT_TEMP_AND_IBAT,
		avg_sample, trig_edge, trig_timer, conv_type, &ibat_raw);
	bat_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL,
		bat_temp_raw);
	ibat_convert = ab8500_gpadc_ad_to_voltage(gpadc, IBAT_VIRTUAL_CHANNEL,
		ibat_raw);

	return seq_printf(s, "%d,0x%X\n"  "%d,0x%X\n",
2341 2342
		bat_temp_convert, bat_temp_raw,
		ibat_convert, ibat_raw);
2343 2344 2345 2346 2347 2348
}

static int ab8540_gpadc_bat_temp_and_ibat_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_bat_temp_and_ibat_print,
2349
		inode->i_private);
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
}

static const struct file_operations ab8540_gpadc_bat_temp_and_ibat_fops = {
	.open = ab8540_gpadc_bat_temp_and_ibat_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8540_gpadc_otp_cal_print(struct seq_file *s, void *p)
{
	struct ab8500_gpadc *gpadc;
	u16 vmain_l, vmain_h, btemp_l, btemp_h;
	u16 vbat_l, vbat_h, ibat_l, ibat_h;

	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
	ab8540_gpadc_get_otp(gpadc, &vmain_l, &vmain_h, &btemp_l, &btemp_h,
			&vbat_l, &vbat_h, &ibat_l, &ibat_h);
	return seq_printf(s, "VMAIN_L:0x%X\n"
2370 2371 2372 2373 2374 2375 2376 2377
		"VMAIN_H:0x%X\n"
		"BTEMP_L:0x%X\n"
		"BTEMP_H:0x%X\n"
		"VBAT_L:0x%X\n"
		"VBAT_H:0x%X\n"
		"IBAT_L:0x%X\n"
		"IBAT_H:0x%X\n",
		vmain_l, vmain_h, btemp_l, btemp_h, vbat_l, vbat_h, ibat_l, ibat_h);
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
}

static int ab8540_gpadc_otp_cal_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8540_gpadc_otp_cal_print, inode->i_private);
}

static const struct file_operations ab8540_gpadc_otp_calib_fops = {
	.open = ab8540_gpadc_otp_cal_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2393 2394 2395 2396 2397 2398 2399 2400
static int ab8500_gpadc_avg_sample_print(struct seq_file *s, void *p)
{
	return seq_printf(s, "%d\n", avg_sample);
}

static int ab8500_gpadc_avg_sample_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_avg_sample_print,
2401
		inode->i_private);
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
}

static ssize_t ab8500_gpadc_avg_sample_write(struct file *file,
	const char __user *user_buf,
	size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_avg_sample;
	int err;

2412
	err = kstrtoul_from_user(user_buf, count, 0, &user_avg_sample);
2413
	if (err)
2414 2415
		return err;

2416 2417 2418 2419 2420 2421 2422 2423 2424
	if ((user_avg_sample == SAMPLE_1) || (user_avg_sample == SAMPLE_4)
			|| (user_avg_sample == SAMPLE_8)
			|| (user_avg_sample == SAMPLE_16)) {
		avg_sample = (u8) user_avg_sample;
	} else {
		dev_err(dev, "debugfs error input: "
			"should be egal to 1, 4, 8 or 16\n");
		return -EINVAL;
	}
2425 2426

	return count;
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
}

static const struct file_operations ab8500_gpadc_avg_sample_fops = {
	.open = ab8500_gpadc_avg_sample_open,
	.read = seq_read,
	.write = ab8500_gpadc_avg_sample_write,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_trig_edge_print(struct seq_file *s, void *p)
{
	return seq_printf(s, "%d\n", trig_edge);
}

static int ab8500_gpadc_trig_edge_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_trig_edge_print,
2446
		inode->i_private);
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
}

static ssize_t ab8500_gpadc_trig_edge_write(struct file *file,
	const char __user *user_buf,
	size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_trig_edge;
	int err;

2457
	err = kstrtoul_from_user(user_buf, count, 0, &user_trig_edge);
2458
	if (err)
2459 2460
		return err;

2461 2462 2463 2464 2465 2466 2467 2468 2469
	if ((user_trig_edge == RISING_EDGE)
			|| (user_trig_edge == FALLING_EDGE)) {
		trig_edge = (u8) user_trig_edge;
	} else {
		dev_err(dev, "Wrong input:\n"
			"Enter 0. Rising edge\n"
			"Enter 1. Falling edge\n");
		return -EINVAL;
	}
2470 2471

	return count;
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
}

static const struct file_operations ab8500_gpadc_trig_edge_fops = {
	.open = ab8500_gpadc_trig_edge_open,
	.read = seq_read,
	.write = ab8500_gpadc_trig_edge_write,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_trig_timer_print(struct seq_file *s, void *p)
{
	return seq_printf(s, "%d\n", trig_timer);
}

static int ab8500_gpadc_trig_timer_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_trig_timer_print,
2491
		inode->i_private);
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
}

static ssize_t ab8500_gpadc_trig_timer_write(struct file *file,
	const char __user *user_buf,
	size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_trig_timer;
	int err;

2502
	err = kstrtoul_from_user(user_buf, count, 0, &user_trig_timer);
2503
	if (err)
2504 2505
		return err;

2506 2507 2508 2509 2510 2511 2512
	if ((user_trig_timer >= 0) && (user_trig_timer <= 255)) {
		trig_timer = (u8) user_trig_timer;
	} else {
		dev_err(dev, "debugfs error input: "
			"should be beetween 0 to 255\n");
		return -EINVAL;
	}
2513 2514

	return count;
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
}

static const struct file_operations ab8500_gpadc_trig_timer_fops = {
	.open = ab8500_gpadc_trig_timer_open,
	.read = seq_read,
	.write = ab8500_gpadc_trig_timer_write,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int ab8500_gpadc_conv_type_print(struct seq_file *s, void *p)
{
	return seq_printf(s, "%d\n", conv_type);
}

static int ab8500_gpadc_conv_type_open(struct inode *inode, struct file *file)
{
	return single_open(file, ab8500_gpadc_conv_type_print,
2534
		inode->i_private);
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
}

static ssize_t ab8500_gpadc_conv_type_write(struct file *file,
	const char __user *user_buf,
	size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_conv_type;
	int err;

2545
	err = kstrtoul_from_user(user_buf, count, 0, &user_conv_type);
2546
	if (err)
2547 2548
		return err;

2549 2550 2551 2552 2553 2554 2555 2556 2557
	if ((user_conv_type == ADC_SW)
			|| (user_conv_type == ADC_HW)) {
		conv_type = (u8) user_conv_type;
	} else {
		dev_err(dev, "Wrong input:\n"
			"Enter 0. ADC SW conversion\n"
			"Enter 1. ADC HW conversion\n");
		return -EINVAL;
	}
2558 2559

	return count;
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
}

static const struct file_operations ab8500_gpadc_conv_type_fops = {
	.open = ab8500_gpadc_conv_type_open,
	.read = seq_read,
	.write = ab8500_gpadc_conv_type_write,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
/*
 * return length of an ASCII numerical value, 0 is string is not a
 * numerical value.
 * string shall start at value 1st char.
 * string can be tailed with \0 or space or newline chars only.
 * value can be decimal or hexadecimal (prefixed 0x or 0X).
 */
static int strval_len(char *b)
{
	char *s = b;
	if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) {
		s += 2;
		for (; *s && (*s != ' ') && (*s != '\n'); s++) {
			if (!isxdigit(*s))
				return 0;
		}
	} else {
		if (*s == '-')
			s++;
		for (; *s && (*s != ' ') && (*s != '\n'); s++) {
			if (!isdigit(*s))
				return 0;
		}
	}
	return (int) (s-b);
}

/*
 * parse hwreg input data.
 * update global hwreg_cfg only if input data syntax is ok.
 */
static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg,
		struct device *dev)
{
	uint write, val = 0;
	u8  regvalue;
	int ret;
	struct hwreg_cfg loc = {
		.bank = 0,          /* default: invalid phys addr */
		.addr = 0,          /* default: invalid phys addr */
		.fmt = 0,           /* default: 32bit access, hex output */
		.mask = 0xFFFFFFFF, /* default: no mask */
		.shift = 0,         /* default: no bit shift */
	};

	/* read or write ? */
	if (!strncmp(b, "read ", 5)) {
		write = 0;
		b += 5;
	} else if (!strncmp(b, "write ", 6)) {
		write = 1;
		b += 6;
	} else
		return -EINVAL;

	/* OPTIONS -l|-w|-b -s -m -o */
	while ((*b == ' ') || (*b == '-')) {
		if (*(b-1) != ' ') {
			b++;
			continue;
		}
		if ((!strncmp(b, "-d ", 3)) ||
				(!strncmp(b, "-dec ", 5))) {
			b += (*(b+2) == ' ') ? 3 : 5;
			loc.fmt |= (1<<0);
		} else if ((!strncmp(b, "-h ", 3)) ||
				(!strncmp(b, "-hex ", 5))) {
			b += (*(b+2) == ' ') ? 3 : 5;
			loc.fmt &= ~(1<<0);
		} else if ((!strncmp(b, "-m ", 3)) ||
				(!strncmp(b, "-mask ", 6))) {
			b += (*(b+2) == ' ') ? 3 : 6;
			if (strval_len(b) == 0)
				return -EINVAL;
			loc.mask = simple_strtoul(b, &b, 0);
		} else if ((!strncmp(b, "-s ", 3)) ||
				(!strncmp(b, "-shift ", 7))) {
			b += (*(b+2) == ' ') ? 3 : 7;
			if (strval_len(b) == 0)
				return -EINVAL;
			loc.shift = simple_strtol(b, &b, 0);
		} else {
			return -EINVAL;
		}
	}
	/* get arg BANK and ADDRESS */
	if (strval_len(b) == 0)
		return -EINVAL;
	loc.bank = simple_strtoul(b, &b, 0);
	while (*b == ' ')
		b++;
	if (strval_len(b) == 0)
		return -EINVAL;
	loc.addr = simple_strtoul(b, &b, 0);

	if (write) {
		while (*b == ' ')
			b++;
		if (strval_len(b) == 0)
			return -EINVAL;
		val = simple_strtoul(b, &b, 0);
	}

	/* args are ok, update target cfg (mainly for read) */
	*cfg = loc;

#ifdef ABB_HWREG_DEBUG
	pr_warn("HWREG request: %s, %s, addr=0x%08X, mask=0x%X, shift=%d"
			"value=0x%X\n", (write) ? "write" : "read",
			REG_FMT_DEC(cfg) ? "decimal" : "hexa",
			cfg->addr, cfg->mask, cfg->shift, val);
#endif

	if (!write)
		return 0;

	ret = abx500_get_register_interruptible(dev,
			(u8)cfg->bank, (u8)cfg->addr, &regvalue);
	if (ret < 0) {
		dev_err(dev, "abx500_get_reg fail %d, %d\n",
			ret, __LINE__);
		return -EINVAL;
	}

	if (cfg->shift >= 0) {
		regvalue &= ~(cfg->mask << (cfg->shift));
		val = (val & cfg->mask) << (cfg->shift);
	} else {
		regvalue &= ~(cfg->mask >> (-cfg->shift));
		val = (val & cfg->mask) >> (-cfg->shift);
	}
	val = val | regvalue;

	ret = abx500_set_register_interruptible(dev,
			(u8)cfg->bank, (u8)cfg->addr, (u8)val);
	if (ret < 0) {
		pr_err("abx500_set_reg failed %d, %d", ret, __LINE__);
		return -EINVAL;
	}

	return 0;
}

static ssize_t ab8500_hwreg_write(struct file *file,
	const char __user *user_buf, size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	char buf[128];
	int buf_size, ret;

	/* Get userspace string and assure termination */
	buf_size = min(count, (sizeof(buf)-1));
	if (copy_from_user(buf, user_buf, buf_size))
		return -EFAULT;
	buf[buf_size] = 0;

	/* get args and process */
	ret = hwreg_common_write(buf, &hwreg_cfg, dev);
	return (ret) ? ret : buf_size;
}

/*
 * - irq subscribe/unsubscribe stuff
 */
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
{
	seq_printf(s, "%d\n", irq_first);

	return 0;
}

static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
					     struct file *file)
{
	return single_open(file, ab8500_subscribe_unsubscribe_print,
2746
		inode->i_private);
2747 2748 2749
}

/*
2750
 * Userspace should use poll() on this file. When an event occur
2751 2752 2753 2754 2755
 * the blocking poll will be released.
 */
static ssize_t show_irq(struct device *dev,
			struct device_attribute *attr, char *buf)
{
2756 2757 2758
	unsigned long name;
	unsigned int irq_index;
	int err;
2759

2760 2761 2762 2763 2764
	err = strict_strtoul(attr->attr.name, 0, &name);
	if (err)
		return err;

	irq_index = name - irq_first;
2765
	if (irq_index >= num_irqs)
2766 2767 2768 2769
		return -EINVAL;
	else
		return sprintf(buf, "%u\n", irq_count[irq_index]);
}
2770 2771 2772 2773 2774 2775 2776 2777

static ssize_t ab8500_subscribe_write(struct file *file,
				      const char __user *user_buf,
				      size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_val;
	int err;
2778
	unsigned int irq_index;
2779

2780
	err = kstrtoul_from_user(user_buf, count, 0, &user_val);
2781
	if (err)
2782 2783
		return err;

2784 2785 2786 2787 2788 2789 2790 2791 2792
	if (user_val < irq_first) {
		dev_err(dev, "debugfs error input < %d\n", irq_first);
		return -EINVAL;
	}
	if (user_val > irq_last) {
		dev_err(dev, "debugfs error input > %d\n", irq_last);
		return -EINVAL;
	}

2793
	irq_index = user_val - irq_first;
2794
	if (irq_index >= num_irqs)
2795 2796
		return -EINVAL;

2797
	/*
2798
	 * This will create a sysfs file named <irq-nr> which userspace can
2799 2800
	 * use to select or poll and get the AB8500 events
	 */
2801 2802
	dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
		GFP_KERNEL);
2803
	event_name[irq_index] = kmalloc(count, GFP_KERNEL);
2804 2805 2806 2807 2808 2809
	sprintf(event_name[irq_index], "%lu", user_val);
	dev_attr[irq_index]->show = show_irq;
	dev_attr[irq_index]->store = NULL;
	dev_attr[irq_index]->attr.name = event_name[irq_index];
	dev_attr[irq_index]->attr.mode = S_IRUGO;
	err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
	if (err < 0) {
		printk(KERN_ERR "sysfs_create_file failed %d\n", err);
		return err;
	}

	err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
				   IRQF_SHARED | IRQF_NO_SUSPEND,
				   "ab8500-debug", &dev->kobj);
	if (err < 0) {
		printk(KERN_ERR "request_threaded_irq failed %d, %lu\n",
                       err, user_val);
2821
		sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
2822 2823 2824
		return err;
	}

2825
	return count;
2826 2827 2828 2829 2830 2831 2832 2833 2834
}

static ssize_t ab8500_unsubscribe_write(struct file *file,
					const char __user *user_buf,
					size_t count, loff_t *ppos)
{
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_val;
	int err;
2835
	unsigned int irq_index;
2836

2837
	err = kstrtoul_from_user(user_buf, count, 0, &user_val);
2838
	if (err)
2839 2840
		return err;

2841 2842 2843 2844 2845 2846 2847 2848 2849
	if (user_val < irq_first) {
		dev_err(dev, "debugfs error input < %d\n", irq_first);
		return -EINVAL;
	}
	if (user_val > irq_last) {
		dev_err(dev, "debugfs error input > %d\n", irq_last);
		return -EINVAL;
	}

2850
	irq_index = user_val - irq_first;
2851
	if (irq_index >= num_irqs)
2852 2853 2854 2855 2856 2857 2858
		return -EINVAL;

	/* Set irq count to 0 when unsubscribe */
	irq_count[irq_index] = 0;

	if (dev_attr[irq_index])
		sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
2859

2860 2861 2862 2863

	free_irq(user_val, &dev->kobj);
	kfree(event_name[irq_index]);
	kfree(dev_attr[irq_index]);
2864

2865
	return count;
2866 2867
}

2868 2869 2870 2871
/*
 * - several deubgfs nodes fops
 */

M
Mattias Wallin 已提交
2872
static const struct file_operations ab8500_bank_fops = {
2873 2874 2875 2876 2877 2878
	.open = ab8500_bank_open,
	.write = ab8500_bank_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
2879 2880 2881
};

static const struct file_operations ab8500_address_fops = {
2882 2883 2884 2885 2886 2887
	.open = ab8500_address_open,
	.write = ab8500_address_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
2888 2889 2890
};

static const struct file_operations ab8500_val_fops = {
2891 2892 2893 2894 2895 2896
	.open = ab8500_val_open,
	.write = ab8500_val_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
2897 2898
};

2899 2900 2901 2902 2903 2904 2905 2906
static const struct file_operations ab8500_interrupts_fops = {
	.open = ab8500_interrupts_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
static const struct file_operations ab8500_subscribe_fops = {
	.open = ab8500_subscribe_unsubscribe_open,
	.write = ab8500_subscribe_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static const struct file_operations ab8500_unsubscribe_fops = {
	.open = ab8500_subscribe_unsubscribe_open,
	.write = ab8500_unsubscribe_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

2925 2926 2927 2928 2929 2930 2931 2932 2933
static const struct file_operations ab8500_hwreg_fops = {
	.open = ab8500_hwreg_open,
	.write = ab8500_hwreg_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

M
Mattias Wallin 已提交
2934
static struct dentry *ab8500_dir;
2935
static struct dentry *ab8500_gpadc_dir;
M
Mattias Wallin 已提交
2936

B
Bill Pemberton 已提交
2937
static int ab8500_debug_probe(struct platform_device *plf)
M
Mattias Wallin 已提交
2938
{
2939
	struct dentry *file;
2940 2941
	int ret = -ENOMEM;
	struct ab8500 *ab8500;
2942
	struct resource *res;
2943 2944
	debug_bank = AB8500_MISC;
	debug_address = AB8500_REV_REG & 0x00FF;
M
Mattias Wallin 已提交
2945

2946 2947 2948
	ab8500 = dev_get_drvdata(plf->dev.parent);
	num_irqs = ab8500->mask_size;

2949
	irq_count = kzalloc(sizeof(*irq_count)*num_irqs, GFP_KERNEL);
2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
	if (!irq_count)
		return -ENOMEM;

	dev_attr = kzalloc(sizeof(*dev_attr)*num_irqs,GFP_KERNEL);
	if (!dev_attr)
		goto out_freeirq_count;

	event_name = kzalloc(sizeof(*event_name)*num_irqs, GFP_KERNEL);
	if (!event_name)
		goto out_freedev_attr;

2961 2962 2963 2964 2965 2966 2967 2968 2969
	res = platform_get_resource_byname(plf, 0, "IRQ_AB8500");
	if (!res) {
		dev_err(&plf->dev, "AB8500 irq not found, err %d\n",
			irq_first);
		ret = -ENXIO;
		goto out_freeevent_name;
	}
	irq_ab8500 = res->start;

2970 2971 2972
	irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
	if (irq_first < 0) {
		dev_err(&plf->dev, "First irq not found, err %d\n",
2973
			irq_first);
2974 2975
		ret = irq_first;
		goto out_freeevent_name;
2976 2977 2978 2979 2980
	}

	irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
	if (irq_last < 0) {
		dev_err(&plf->dev, "Last irq not found, err %d\n",
2981
			irq_last);
2982
		ret = irq_last;
2983
		goto out_freeevent_name;
2984 2985
	}

2986 2987
	ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
	if (!ab8500_dir)
2988
		goto err;
M
Mattias Wallin 已提交
2989

2990
	ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING,
2991
		ab8500_dir);
2992 2993 2994 2995
	if (!ab8500_gpadc_dir)
		goto err;

	file = debugfs_create_file("all-bank-registers", S_IRUGO,
2996
		ab8500_dir, &plf->dev, &ab8500_registers_fops);
2997 2998 2999
	if (!file)
		goto err;

3000
	file = debugfs_create_file("all-banks", S_IRUGO,
3001
		ab8500_dir, &plf->dev, &ab8500_all_banks_fops);
3002 3003 3004
	if (!file)
		goto err;

3005
	file = debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR | S_IWGRP),
3006
		ab8500_dir, &plf->dev, &ab8500_bank_fops);
3007 3008 3009
	if (!file)
		goto err;

3010
	file = debugfs_create_file("register-address", (S_IRUGO | S_IWUSR | S_IWGRP),
3011
		ab8500_dir, &plf->dev, &ab8500_address_fops);
3012 3013 3014
	if (!file)
		goto err;

3015
	file = debugfs_create_file("register-value", (S_IRUGO | S_IWUSR | S_IWGRP),
3016
		ab8500_dir, &plf->dev, &ab8500_val_fops);
3017 3018 3019
	if (!file)
		goto err;

3020
	file = debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
3021
		ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
3022 3023 3024
	if (!file)
		goto err;

3025 3026
	if (is_ab8500(ab8500)) {
		debug_ranges = ab8500_debug_ranges;
3027
		num_interrupt_lines = AB8500_NR_IRQS;
3028 3029
	} else if (is_ab8505(ab8500)) {
		debug_ranges = ab8505_debug_ranges;
3030
		num_interrupt_lines = AB8505_NR_IRQS;
3031 3032
	} else if (is_ab9540(ab8500)) {
		debug_ranges = ab8505_debug_ranges;
3033
		num_interrupt_lines = AB9540_NR_IRQS;
3034
	} else if (is_ab8540(ab8500)) {
3035
		debug_ranges = ab8540_debug_ranges;
3036
		num_interrupt_lines = AB8540_NR_IRQS;
3037
	}
3038 3039

	file = debugfs_create_file("interrupts", (S_IRUGO),
3040
		ab8500_dir, &plf->dev, &ab8500_interrupts_fops);
3041 3042 3043
	if (!file)
		goto err;

3044
	file = debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
3045
		ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
3046 3047 3048
	if (!file)
		goto err;

3049
	file = debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR | S_IWGRP),
3050
		ab8500_dir, &plf->dev, &ab8500_hwreg_fops);
3051 3052 3053
	if (!file)
		goto err;

3054
	file = debugfs_create_file("all-modem-registers", (S_IRUGO | S_IWUSR | S_IWGRP),
3055 3056 3057 3058
		ab8500_dir, &plf->dev, &ab8500_modem_fops);
	if (!file)
		goto err;

3059
	file = debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR | S_IWGRP),
3060
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bat_ctrl_fops);
3061 3062 3063
	if (!file)
		goto err;

3064
	file = debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR | S_IWGRP),
3065
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_btemp_ball_fops);
3066 3067 3068
	if (!file)
		goto err;

3069
	file = debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3070
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_v_fops);
3071 3072 3073
	if (!file)
		goto err;

3074
	file = debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR | S_IWGRP),
3075
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect1_fops);
3076 3077 3078
	if (!file)
		goto err;

3079
	file = debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR | S_IWGRP),
3080
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect2_fops);
3081 3082 3083
	if (!file)
		goto err;

3084
	file = debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR | S_IWGRP),
3085
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux1_fops);
3086 3087 3088
	if (!file)
		goto err;

3089
	file = debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR | S_IWGRP),
3090
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux2_fops);
3091 3092
	if (!file)
		goto err;
M
Mattias Wallin 已提交
3093

3094
	file = debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3095
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_bat_v_fops);
3096 3097
	if (!file)
		goto err;
M
Mattias Wallin 已提交
3098

3099
	file = debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3100
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_vbus_v_fops);
3101 3102
	if (!file)
		goto err;
M
Mattias Wallin 已提交
3103

3104
	file = debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP),
3105
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_c_fops);
3106 3107 3108
	if (!file)
		goto err;

3109
	file = debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP),
3110
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_charger_c_fops);
3111 3112 3113
	if (!file)
		goto err;

3114
	file = debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3115
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bk_bat_v_fops);
3116 3117 3118
	if (!file)
		goto err;

3119
	file = debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR | S_IWGRP),
3120
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_die_temp_fops);
3121 3122
	if (!file)
		goto err;
3123

3124
	file = debugfs_create_file("usb_id", (S_IRUGO | S_IWUSR | S_IWGRP),
3125 3126 3127 3128
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_id_fops);
	if (!file)
		goto err;

3129
	if (is_ab8540(ab8500)) {
3130
		file = debugfs_create_file("xtal_temp", (S_IRUGO | S_IWUSR | S_IWGRP),
3131 3132 3133
			ab8500_gpadc_dir, &plf->dev, &ab8540_gpadc_xtal_temp_fops);
		if (!file)
			goto err;
3134
		file = debugfs_create_file("vbattruemeas", (S_IRUGO | S_IWUSR | S_IWGRP),
3135 3136 3137 3138 3139
			ab8500_gpadc_dir, &plf->dev,
			&ab8540_gpadc_vbat_true_meas_fops);
		if (!file)
			goto err;
		file = debugfs_create_file("batctrl_and_ibat",
3140 3141
			(S_IRUGO | S_IWUGO), ab8500_gpadc_dir,
			&plf->dev, &ab8540_gpadc_bat_ctrl_and_ibat_fops);
3142 3143 3144
		if (!file)
			goto err;
		file = debugfs_create_file("vbatmeas_and_ibat",
3145 3146 3147
			(S_IRUGO | S_IWUGO), ab8500_gpadc_dir,
			&plf->dev,
			&ab8540_gpadc_vbat_meas_and_ibat_fops);
3148 3149 3150
		if (!file)
			goto err;
		file = debugfs_create_file("vbattruemeas_and_ibat",
3151 3152 3153
			(S_IRUGO | S_IWUGO), ab8500_gpadc_dir,
			&plf->dev,
			&ab8540_gpadc_vbat_true_meas_and_ibat_fops);
3154 3155 3156
		if (!file)
			goto err;
		file = debugfs_create_file("battemp_and_ibat",
3157 3158
			(S_IRUGO | S_IWUGO), ab8500_gpadc_dir,
			&plf->dev, &ab8540_gpadc_bat_temp_and_ibat_fops);
3159 3160
		if (!file)
			goto err;
3161
		file = debugfs_create_file("otp_calib", (S_IRUGO | S_IWUSR | S_IWGRP),
3162 3163 3164 3165
			ab8500_gpadc_dir, &plf->dev, &ab8540_gpadc_otp_calib_fops);
		if (!file)
			goto err;
	}
3166
	file = debugfs_create_file("avg_sample", (S_IRUGO | S_IWUSR | S_IWGRP),
3167 3168 3169 3170
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_avg_sample_fops);
	if (!file)
		goto err;

3171
	file = debugfs_create_file("trig_edge", (S_IRUGO | S_IWUSR | S_IWGRP),
3172 3173 3174 3175
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_trig_edge_fops);
	if (!file)
		goto err;

3176
	file = debugfs_create_file("trig_timer", (S_IRUGO | S_IWUSR | S_IWGRP),
3177 3178 3179 3180
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_trig_timer_fops);
	if (!file)
		goto err;

3181
	file = debugfs_create_file("conv_type", (S_IRUGO | S_IWUSR | S_IWGRP),
3182 3183 3184
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_conv_type_fops);
	if (!file)
		goto err;
3185

3186
	return 0;
M
Mattias Wallin 已提交
3187

3188 3189 3190
err:
	if (ab8500_dir)
		debugfs_remove_recursive(ab8500_dir);
3191
	dev_err(&plf->dev, "failed to create debugfs entries.\n");
3192 3193 3194 3195 3196 3197 3198 3199
out_freeevent_name:
	kfree(event_name);
out_freedev_attr:
	kfree(dev_attr);
out_freeirq_count:
	kfree(irq_count);

	return ret;
M
Mattias Wallin 已提交
3200 3201
}

B
Bill Pemberton 已提交
3202
static int ab8500_debug_remove(struct platform_device *plf)
M
Mattias Wallin 已提交
3203
{
3204
	debugfs_remove_recursive(ab8500_dir);
3205 3206 3207 3208
	kfree(event_name);
	kfree(dev_attr);
	kfree(irq_count);

3209
	return 0;
M
Mattias Wallin 已提交
3210 3211 3212
}

static struct platform_driver ab8500_debug_driver = {
3213 3214 3215 3216 3217
	.driver = {
		.name = "ab8500-debug",
		.owner = THIS_MODULE,
	},
	.probe  = ab8500_debug_probe,
B
Bill Pemberton 已提交
3218
	.remove = ab8500_debug_remove
M
Mattias Wallin 已提交
3219 3220 3221 3222
};

static int __init ab8500_debug_init(void)
{
3223
	return platform_driver_register(&ab8500_debug_driver);
M
Mattias Wallin 已提交
3224 3225 3226 3227
}

static void __exit ab8500_debug_exit(void)
{
3228
	platform_driver_unregister(&ab8500_debug_driver);
M
Mattias Wallin 已提交
3229 3230 3231 3232 3233 3234 3235
}
subsys_initcall(ab8500_debug_init);
module_exit(ab8500_debug_exit);

MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
MODULE_DESCRIPTION("AB8500 DEBUG");
MODULE_LICENSE("GPL v2");