ab8500-debugfs.c 69.2 KB
Newer Older
Mattias Wallin's avatar
Mattias Wallin committed
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.
 */
Mattias Wallin's avatar
Mattias Wallin committed
73 74 75 76

#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
77
#include <linux/module.h>
Mattias Wallin's avatar
Mattias Wallin committed
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>
Mattias Wallin's avatar
Mattias Wallin committed
84 85

#include <linux/mfd/abx500.h>
86
#include <linux/mfd/abx500/ab8500.h>
87
#include <linux/mfd/abx500/ab8500-gpadc.h>
Mattias Wallin's avatar
Mattias Wallin committed
88

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

94 95 96
/* TODO: this file should not reference IRQ_DB8500_AB8500! */
#include <mach/irqs.h>

Mattias Wallin's avatar
Mattias Wallin committed
97 98 99
static u32 debug_bank;
static u32 debug_address;

100 101
static int irq_first;
static int irq_last;
102 103
static u32 *irq_count;
static int num_irqs;
104

105 106
static struct device_attribute **dev_attr;
static char **event_name;
107

108 109 110 111 112
static u8 avg_sample = SAMPLE_16;
static u8 trig_edge = RISING_EDGE;
static u8 conv_type = ADC_SW;
static u8 trig_timer;

Mattias Wallin's avatar
Mattias Wallin committed
113 114 115 116 117 118 119
/**
 * 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 {
120 121 122
	u8 first;
	u8 last;
	u8 perm;
Mattias Wallin's avatar
Mattias Wallin committed
123 124 125
};

/**
126
 * struct ab8500_prcmu_ranges
Mattias Wallin's avatar
Mattias Wallin committed
127 128 129 130
 * @num_ranges: the number of ranges in the list
 * @bankid: bank identifier
 * @range: the list of register ranges
 */
131
struct ab8500_prcmu_ranges {
132 133 134
	u8 num_ranges;
	u8 bankid;
	const struct ab8500_reg_range *range;
Mattias Wallin's avatar
Mattias Wallin committed
135 136
};

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/* 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 */
};

Mattias Wallin's avatar
Mattias Wallin committed
156
#define AB8500_NAME_STRING "ab8500"
157
#define AB8500_ADC_NAME_STRING "gpadc"
158
#define AB8500_NUM_BANKS 24
Mattias Wallin's avatar
Mattias Wallin committed
159 160 161

#define AB8500_REV_REG 0x80

162 163 164
static struct ab8500_prcmu_ranges *debug_ranges;

struct ab8500_prcmu_ranges ab8500_debug_ranges[AB8500_NUM_BANKS] = {
165 166
	[0x0] = {
		.num_ranges = 0,
167
		.range = NULL,
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	},
	[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,
307
		.range = NULL,
308 309 310 311 312 313 314 315 316 317 318 319
	},
	[AB8500_ECI_AV_ACC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[0x9] = {
		.num_ranges = 0,
320
		.range = NULL,
321 322 323 324 325 326 327 328 329 330 331
	},
	[AB8500_GPADC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x08,
			},
		},
	},
	[AB8500_CHARGER] = {
332
		.num_ranges = 9,
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
		.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,
			},
366 367
			{
				.first = 0xf5,
368
				.last = 0xf6,
369
			},
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		},
	},
	[AB8500_GAS_GAUGE] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x07,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	[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,
			},
		},
	},
407 408 409 410 411 412 413 414 415 416 417
	[AB8500_AUDIO] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x6F,
			},
		},
	},
	[AB8500_INTERRUPT] = {
		.num_ranges = 0,
418
		.range = NULL,
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
	},
	[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,
468
		.range = NULL,
469 470 471
	},
	[0x12] = {
		.num_ranges = 0,
472
		.range = NULL,
473 474 475
	},
	[0x13] = {
		.num_ranges = 0,
476
		.range = NULL,
477 478 479
	},
	[0x14] = {
		.num_ranges = 0,
480
		.range = NULL,
481 482 483 484 485 486 487 488 489 490
	},
	[AB8500_OTP_EMUL] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x01,
				.last = 0x0F,
			},
		},
	},
Mattias Wallin's avatar
Mattias Wallin committed
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
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,
			},
		},
	},
};

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 1266 1267
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,
			},
		},
	},
};


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

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

	return IRQ_HANDLED;
}

1286 1287 1288
/* Prints to seq_file or log_buf */
static int ab8500_registers_print(struct device *dev, u32 bank,
				struct seq_file *s)
Mattias Wallin's avatar
Mattias Wallin committed
1289
{
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	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;
			}

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

1325 1326 1327 1328 1329 1330 1331
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");

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

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

Mattias Wallin's avatar
Mattias Wallin committed
1338 1339
static int ab8500_registers_open(struct inode *inode, struct file *file)
{
1340
	return single_open(file, ab8500_print_bank_registers, inode->i_private);
Mattias Wallin's avatar
Mattias Wallin committed
1341 1342 1343
}

static const struct file_operations ab8500_registers_fops = {
1344 1345 1346 1347 1348
	.open = ab8500_registers_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
Mattias Wallin's avatar
Mattias Wallin committed
1349 1350
};

1351 1352 1353 1354 1355 1356 1357 1358
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");

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

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

1367 1368 1369 1370 1371 1372 1373 1374
/* 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++) {
1375
		printk(KERN_INFO" bank 0x%02X:\n", i);
1376 1377 1378 1379
		ab8500_registers_print(dev, i, NULL);
	}
}

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
/* 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;
1396
	int err = 0;
1397 1398 1399 1400

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

1401
	for (bank = 0; bank < AB8500_NUM_BANKS; bank++) {
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
		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);

1412 1413 1414
				if (err < 0)
					goto out;

1415 1416 1417 1418 1419 1420 1421 1422 1423
				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__);
1424 1425
					err = -EINVAL;
					goto out;
1426 1427 1428 1429
				}
			}
		}
	}
1430 1431 1432 1433 1434
out:
	if (err >= 0)
		pr_info("Saved all ABB registers.\n");
	else
		pr_info("Failed to save all ABB registers.\n");
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 1462 1463
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,
};

Mattias Wallin's avatar
Mattias Wallin committed
1464 1465
static int ab8500_bank_print(struct seq_file *s, void *p)
{
1466
	return seq_printf(s, "0x%02X\n", debug_bank);
Mattias Wallin's avatar
Mattias Wallin committed
1467 1468 1469 1470
}

static int ab8500_bank_open(struct inode *inode, struct file *file)
{
1471
	return single_open(file, ab8500_bank_print, inode->i_private);
Mattias Wallin's avatar
Mattias Wallin committed
1472 1473 1474
}

static ssize_t ab8500_bank_write(struct file *file,
1475 1476
	const char __user *user_buf,
	size_t count, loff_t *ppos)
Mattias Wallin's avatar
Mattias Wallin committed
1477
{
1478 1479 1480 1481
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_bank;
	int err;

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

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

	debug_bank = user_bank;

1493
	return count;
Mattias Wallin's avatar
Mattias Wallin committed
1494 1495 1496 1497
}

static int ab8500_address_print(struct seq_file *s, void *p)
{
1498
	return seq_printf(s, "0x%02X\n", debug_address);
Mattias Wallin's avatar
Mattias Wallin committed
1499 1500 1501 1502
}

static int ab8500_address_open(struct inode *inode, struct file *file)
{
1503
	return single_open(file, ab8500_address_print, inode->i_private);
Mattias Wallin's avatar
Mattias Wallin committed
1504 1505 1506
}

static ssize_t ab8500_address_write(struct file *file,
1507 1508
				    const char __user *user_buf,
				    size_t count, loff_t *ppos)
Mattias Wallin's avatar
Mattias Wallin committed
1509
{
1510 1511 1512 1513
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_address;
	int err;

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

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

1524
	return count;
Mattias Wallin's avatar
Mattias Wallin committed
1525 1526 1527 1528
}

static int ab8500_val_print(struct seq_file *s, void *p)
{
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
	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;
Mattias Wallin's avatar
Mattias Wallin committed
1543 1544 1545 1546
}

static int ab8500_val_open(struct inode *inode, struct file *file)
{
1547
	return single_open(file, ab8500_val_print, inode->i_private);
Mattias Wallin's avatar
Mattias Wallin committed
1548 1549 1550
}

static ssize_t ab8500_val_write(struct file *file,
1551 1552
				const char __user *user_buf,
				size_t count, loff_t *ppos)
Mattias Wallin's avatar
Mattias Wallin committed
1553
{
1554 1555 1556 1557
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_val;
	int err;

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

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
	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;
	}

1573
	return count;
Mattias Wallin's avatar
Mattias Wallin committed
1574 1575
}

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

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

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

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

1601 1602 1603 1604 1605
	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;
1606

1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
		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');
	}
1620 1621 1622 1623 1624 1625 1626 1627 1628

	return 0;
}

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

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 1662 1663
/*
 * - 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);
}

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 1735 1736
#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,
};

1737 1738 1739 1740 1741 1742
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;

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

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

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;

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

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

static int ab8500_gpadc_btemp_ball_open(struct inode *inode,
1783
					struct file *file)
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
{
	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;

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

	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,
1813
					    struct file *file)
1814 1815
{
	return single_open(file, ab8500_gpadc_main_charger_v_print,
1816
		inode->i_private);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
}

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;

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

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

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

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;

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

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

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

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;

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

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

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;

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

	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;

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

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

static int ab8500_gpadc_main_bat_v_open(struct inode *inode,
1964
					struct file *file)
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
{
	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;

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

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

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;

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

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

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

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;

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

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

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

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;

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

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

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;

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

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

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,
};

2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
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",
2139
		usb_id_convert, usb_id_raw);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
}

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,
};

2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
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",
2168
		xtal_temp_convert, xtal_temp_raw);
2169 2170 2171 2172 2173
}

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

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",
2198
		vbat_true_meas_convert, vbat_true_meas_raw);
2199 2200 2201 2202 2203 2204
}

static int ab8540_gpadc_vbat_true_meas_open(struct inode *inode,
		struct file *file)
{
	return single_open(file, ab8540_gpadc_vbat_true_meas_print,
2205
		inode->i_private);
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 2232 2233
}

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",
2234 2235
		bat_ctrl_convert, bat_ctrl_raw,
		ibat_convert, ibat_raw);
2236 2237 2238 2239 2240 2241
}

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,
2242
		inode->i_private);
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 2268 2269
}

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",
2270 2271
		vbat_meas_convert, vbat_meas_raw,
		ibat_convert, ibat_raw);
2272 2273 2274 2275 2276 2277
}

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,
2278
		inode->i_private);
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 2305 2306
}

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",
2307 2308
		vbat_true_meas_convert, vbat_true_meas_raw,
		ibat_convert, ibat_raw);
2309 2310 2311 2312 2313 2314
}

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,
2315
		inode->i_private);
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 2341 2342
}

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",
2343 2344
		bat_temp_convert, bat_temp_raw,
		ibat_convert, ibat_raw);
2345 2346 2347 2348 2349 2350
}

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,
2351
		inode->i_private);
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
}

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"
2372 2373 2374 2375 2376 2377 2378 2379
		"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);
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
}

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,
};

2395 2396 2397 2398 2399 2400 2401 2402
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,
2403
		inode->i_private);
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
}

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;

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

2418 2419 2420 2421 2422 2423 2424 2425 2426
	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;
	}
2427 2428

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

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,
2448
		inode->i_private);
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
}

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;

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

2463 2464 2465 2466 2467 2468 2469 2470 2471
	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;
	}
2472 2473

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

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,
2493
		inode->i_private);
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
}

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;

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

2508 2509 2510 2511 2512 2513 2514
	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;
	}
2515 2516

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

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,
2536
		inode->i_private);
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
}

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;

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

2551 2552 2553 2554 2555 2556 2557 2558 2559
	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;
	}
2560 2561

	return count;
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
}

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,
};

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 2735 2736
/*
 * 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
 */
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
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,
2748
		inode->i_private);
2749 2750 2751
}

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

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

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

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;
2780
	unsigned int irq_index;
2781

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

2786 2787 2788 2789 2790 2791 2792 2793 2794
	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;
	}

2795
	irq_index = user_val - irq_first;
2796
	if (irq_index >= num_irqs)
2797 2798
		return -EINVAL;

2799
	/*
2800
	 * This will create a sysfs file named <irq-nr> which userspace can
2801 2802
	 * use to select or poll and get the AB8500 events
	 */
2803 2804
	dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
		GFP_KERNEL);
2805
	event_name[irq_index] = kmalloc(count, GFP_KERNEL);
2806 2807 2808 2809 2810 2811
	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);
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
	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);
2823
		sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
2824 2825 2826
		return err;
	}

2827
	return count;
2828 2829 2830 2831 2832 2833 2834 2835 2836
}

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;
2837
	unsigned int irq_index;
2838

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

2843 2844 2845 2846 2847 2848 2849 2850 2851
	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;
	}

2852
	irq_index = user_val - irq_first;
2853
	if (irq_index >= num_irqs)
2854 2855 2856 2857 2858 2859 2860
		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);
2861

2862 2863 2864 2865

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

2867
	return count;
2868 2869
}

2870 2871 2872 2873
/*
 * - several deubgfs nodes fops
 */

Mattias Wallin's avatar
Mattias Wallin committed
2874
static const struct file_operations ab8500_bank_fops = {
2875 2876 2877 2878 2879 2880
	.open = ab8500_bank_open,
	.write = ab8500_bank_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
Mattias Wallin's avatar
Mattias Wallin committed
2881 2882 2883
};

static const struct file_operations ab8500_address_fops = {
2884 2885 2886 2887 2888 2889
	.open = ab8500_address_open,
	.write = ab8500_address_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
Mattias Wallin's avatar
Mattias Wallin committed
2890 2891 2892
};

static const struct file_operations ab8500_val_fops = {
2893 2894 2895 2896 2897 2898
	.open = ab8500_val_open,
	.write = ab8500_val_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
Mattias Wallin's avatar
Mattias Wallin committed
2899 2900
};

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

2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
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,
};

2927 2928 2929 2930 2931 2932 2933 2934 2935
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,
};

Mattias Wallin's avatar
Mattias Wallin committed
2936
static struct dentry *ab8500_dir;
2937
static struct dentry *ab8500_gpadc_dir;
Mattias Wallin's avatar
Mattias Wallin committed
2938

2939
static int ab8500_debug_probe(struct platform_device *plf)
Mattias Wallin's avatar
Mattias Wallin committed
2940
{
2941
	struct dentry *file;
2942 2943
	int ret = -ENOMEM;
	struct ab8500 *ab8500;
2944 2945
	debug_bank = AB8500_MISC;
	debug_address = AB8500_REV_REG & 0x00FF;
Mattias Wallin's avatar
Mattias Wallin committed
2946

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

2950
	irq_count = kzalloc(sizeof(*irq_count)*num_irqs, GFP_KERNEL);
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
	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;

2962 2963 2964
	irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
	if (irq_first < 0) {
		dev_err(&plf->dev, "First irq not found, err %d\n",
2965
			irq_first);
2966 2967
		ret = irq_first;
		goto out_freeevent_name;
2968 2969 2970 2971 2972
	}

	irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
	if (irq_last < 0) {
		dev_err(&plf->dev, "Last irq not found, err %d\n",
2973
			irq_last);
2974
		ret = irq_last;
2975
		goto out_freeevent_name;
2976 2977
	}

2978 2979
	ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
	if (!ab8500_dir)
2980
		goto err;
Mattias Wallin's avatar
Mattias Wallin committed
2981

2982
	ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING,
2983
		ab8500_dir);
2984 2985 2986 2987
	if (!ab8500_gpadc_dir)
		goto err;

	file = debugfs_create_file("all-bank-registers", S_IRUGO,
2988
		ab8500_dir, &plf->dev, &ab8500_registers_fops);
2989 2990 2991
	if (!file)
		goto err;

2992
	file = debugfs_create_file("all-banks", S_IRUGO,
2993
		ab8500_dir, &plf->dev, &ab8500_all_banks_fops);
2994 2995 2996
	if (!file)
		goto err;

2997
	file = debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR | S_IWGRP),
2998
		ab8500_dir, &plf->dev, &ab8500_bank_fops);
2999 3000 3001
	if (!file)
		goto err;

3002
	file = debugfs_create_file("register-address", (S_IRUGO | S_IWUSR | S_IWGRP),
3003
		ab8500_dir, &plf->dev, &ab8500_address_fops);
3004 3005 3006
	if (!file)
		goto err;

3007
	file = debugfs_create_file("register-value", (S_IRUGO | S_IWUSR | S_IWGRP),
3008
		ab8500_dir, &plf->dev, &ab8500_val_fops);
3009 3010 3011
	if (!file)
		goto err;

3012
	file = debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
3013
		ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
3014 3015 3016
	if (!file)
		goto err;

3017 3018
	if (is_ab8500(ab8500)) {
		debug_ranges = ab8500_debug_ranges;
3019
		num_interrupt_lines = AB8500_NR_IRQS;
3020 3021
	} else if (is_ab8505(ab8500)) {
		debug_ranges = ab8505_debug_ranges;
3022
		num_interrupt_lines = AB8505_NR_IRQS;
3023 3024
	} else if (is_ab9540(ab8500)) {
		debug_ranges = ab8505_debug_ranges;
3025
		num_interrupt_lines = AB9540_NR_IRQS;
3026
	} else if (is_ab8540(ab8500)) {
3027
		debug_ranges = ab8540_debug_ranges;
3028
		num_interrupt_lines = AB8540_NR_IRQS;
3029
	}
3030 3031

	file = debugfs_create_file("interrupts", (S_IRUGO),
3032
		ab8500_dir, &plf->dev, &ab8500_interrupts_fops);
3033 3034 3035
	if (!file)
		goto err;

3036
	file = debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
3037
		ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
3038 3039 3040
	if (!file)
		goto err;

3041
	file = debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR | S_IWGRP),
3042
		ab8500_dir, &plf->dev, &ab8500_hwreg_fops);
3043 3044 3045
	if (!file)
		goto err;

3046
	file = debugfs_create_file("all-modem-registers", (S_IRUGO | S_IWUSR | S_IWGRP),
3047 3048 3049 3050
		ab8500_dir, &plf->dev, &ab8500_modem_fops);
	if (!file)
		goto err;

3051
	file = debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR | S_IWGRP),
3052
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bat_ctrl_fops);
3053 3054 3055
	if (!file)
		goto err;

3056
	file = debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR | S_IWGRP),
3057
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_btemp_ball_fops);
3058 3059 3060
	if (!file)
		goto err;

3061
	file = debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3062
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_v_fops);
3063 3064 3065
	if (!file)
		goto err;

3066
	file = debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR | S_IWGRP),
3067
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect1_fops);
3068 3069 3070
	if (!file)
		goto err;

3071
	file = debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR | S_IWGRP),
3072
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect2_fops);
3073 3074 3075
	if (!file)
		goto err;

3076
	file = debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR | S_IWGRP),
3077
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux1_fops);
3078 3079 3080
	if (!file)
		goto err;

3081
	file = debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR | S_IWGRP),
3082
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux2_fops);
3083 3084
	if (!file)
		goto err;
Mattias Wallin's avatar
Mattias Wallin committed
3085

3086
	file = debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3087
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_bat_v_fops);
3088 3089
	if (!file)
		goto err;
Mattias Wallin's avatar
Mattias Wallin committed
3090

3091
	file = debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3092
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_vbus_v_fops);
3093 3094
	if (!file)
		goto err;
Mattias Wallin's avatar
Mattias Wallin committed
3095

3096
	file = debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP),
3097
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_c_fops);
3098 3099 3100
	if (!file)
		goto err;

3101
	file = debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR | S_IWGRP),
3102
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_charger_c_fops);
3103 3104 3105
	if (!file)
		goto err;

3106
	file = debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR | S_IWGRP),
3107
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bk_bat_v_fops);
3108 3109 3110
	if (!file)
		goto err;

3111
	file = debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR | S_IWGRP),
3112
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_die_temp_fops);
3113 3114
	if (!file)
		goto err;
3115

3116
	file = debugfs_create_file("usb_id", (S_IRUGO | S_IWUSR | S_IWGRP),
3117 3118 3119 3120
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_id_fops);
	if (!file)
		goto err;

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

3163
	file = debugfs_create_file("trig_edge", (S_IRUGO | S_IWUSR | S_IWGRP),
3164 3165 3166 3167
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_trig_edge_fops);
	if (!file)
		goto err;

3168
	file = debugfs_create_file("trig_timer", (S_IRUGO | S_IWUSR | S_IWGRP),
3169 3170 3171 3172
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_trig_timer_fops);
	if (!file)
		goto err;

3173
	file = debugfs_create_file("conv_type", (S_IRUGO | S_IWUSR | S_IWGRP),
3174 3175 3176
		ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_conv_type_fops);
	if (!file)
		goto err;
3177

3178
	return 0;
Mattias Wallin's avatar
Mattias Wallin committed
3179

3180 3181 3182
err:
	if (ab8500_dir)
		debugfs_remove_recursive(ab8500_dir);
3183
	dev_err(&plf->dev, "failed to create debugfs entries.\n");
3184 3185 3186 3187 3188 3189 3190 3191
out_freeevent_name:
	kfree(event_name);
out_freedev_attr:
	kfree(dev_attr);
out_freeirq_count:
	kfree(irq_count);

	return ret;
Mattias Wallin's avatar
Mattias Wallin committed
3192 3193
}

3194
static int ab8500_debug_remove(struct platform_device *plf)
Mattias Wallin's avatar
Mattias Wallin committed
3195
{
3196
	debugfs_remove_recursive(ab8500_dir);
3197 3198 3199 3200
	kfree(event_name);
	kfree(dev_attr);
	kfree(irq_count);

3201
	return 0;
Mattias Wallin's avatar
Mattias Wallin committed
3202 3203 3204
}

static struct platform_driver ab8500_debug_driver = {
3205 3206 3207 3208 3209
	.driver = {
		.name = "ab8500-debug",
		.owner = THIS_MODULE,
	},
	.probe  = ab8500_debug_probe,
3210
	.remove = ab8500_debug_remove
Mattias Wallin's avatar
Mattias Wallin committed
3211 3212 3213 3214
};

static int __init ab8500_debug_init(void)
{
3215
	return platform_driver_register(&ab8500_debug_driver);
Mattias Wallin's avatar
Mattias Wallin committed
3216 3217 3218 3219
}

static void __exit ab8500_debug_exit(void)
{
3220
	platform_driver_unregister(&ab8500_debug_driver);
Mattias Wallin's avatar
Mattias Wallin committed
3221 3222 3223 3224 3225 3226 3227
}
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");