ab8500-debugfs.c 40.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>
M
Mattias Wallin 已提交
83 84

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

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

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

96 97
static int irq_first;
static int irq_last;
98 99
static u32 *irq_count;
static int num_irqs;
100

101 102
static struct device_attribute **dev_attr;
static char **event_name;
103

M
Mattias Wallin 已提交
104 105 106 107 108 109 110
/**
 * 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 {
111 112 113
	u8 first;
	u8 last;
	u8 perm;
M
Mattias Wallin 已提交
114 115 116
};

/**
117
 * struct ab8500_prcmu_ranges
M
Mattias Wallin 已提交
118 119 120 121
 * @num_ranges: the number of ranges in the list
 * @bankid: bank identifier
 * @range: the list of register ranges
 */
122
struct ab8500_prcmu_ranges {
123 124 125
	u8 num_ranges;
	u8 bankid;
	const struct ab8500_reg_range *range;
M
Mattias Wallin 已提交
126 127
};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* 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 已提交
147
#define AB8500_NAME_STRING "ab8500"
148
#define AB8500_ADC_NAME_STRING "gpadc"
149
#define AB8500_NUM_BANKS 24
M
Mattias Wallin 已提交
150 151 152

#define AB8500_REV_REG 0x80

153
static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = {
154 155
	[0x0] = {
		.num_ranges = 0,
156
		.range = NULL,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	},
	[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,
296
		.range = NULL,
297 298 299 300 301 302 303 304 305 306 307 308
	},
	[AB8500_ECI_AV_ACC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x80,
				.last = 0x82,
			},
		},
	},
	[0x9] = {
		.num_ranges = 0,
309
		.range = NULL,
310 311 312 313 314 315 316 317 318 319 320
	},
	[AB8500_GPADC] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x08,
			},
		},
	},
	[AB8500_CHARGER] = {
321
		.num_ranges = 9,
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		.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,
			},
355 356 357 358
			{
				.first = 0xf5,
				.last =	0xf6,
			},
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		},
	},
	[AB8500_GAS_GAUGE] = {
		.num_ranges = 3,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x00,
			},
			{
				.first = 0x07,
				.last = 0x0A,
			},
			{
				.first = 0x10,
				.last = 0x14,
			},
		},
	},
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	[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,
			},
		},
	},
396 397 398 399 400 401 402 403 404 405 406
	[AB8500_AUDIO] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x00,
				.last = 0x6F,
			},
		},
	},
	[AB8500_INTERRUPT] = {
		.num_ranges = 0,
407
		.range = NULL,
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	},
	[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,
457
		.range = NULL,
458 459 460
	},
	[0x12] = {
		.num_ranges = 0,
461
		.range = NULL,
462 463 464
	},
	[0x13] = {
		.num_ranges = 0,
465
		.range = NULL,
466 467 468
	},
	[0x14] = {
		.num_ranges = 0,
469
		.range = NULL,
470 471 472 473 474 475 476 477 478 479
	},
	[AB8500_OTP_EMUL] = {
		.num_ranges = 1,
		.range = (struct ab8500_reg_range[]) {
			{
				.first = 0x01,
				.last = 0x0F,
			},
		},
	},
M
Mattias Wallin 已提交
480 481
};

482 483 484 485
static irqreturn_t ab8500_debug_handler(int irq, void *data)
{
	char buf[16];
	struct kobject *kobj = (struct kobject *)data;
486
	unsigned int irq_abb = irq - irq_first;
487

488
	if (irq_abb < num_irqs)
489
		irq_count[irq_abb]++;
490 491
	/*
	 * This makes it possible to use poll for events (POLLPRI | POLLERR)
492
	 * from userspace on sysfs file named <irq-nr>
493
	 */
494
	sprintf(buf, "%d", irq);
495 496 497 498 499
	sysfs_notify(kobj, NULL, buf);

	return IRQ_HANDLED;
}

500 501 502
/* Prints to seq_file or log_buf */
static int ab8500_registers_print(struct device *dev, u32 bank,
				struct seq_file *s)
M
Mattias Wallin 已提交
503
{
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	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;
			}

522 523 524 525 526 527 528 529 530 531 532 533 534 535
			if (s) {
				err = seq_printf(s, "  [%u/0x%02X]: 0x%02X\n",
					bank, reg, value);
				if (err < 0) {
					dev_err(dev,
					"seq_printf overflow bank=%d reg=%d\n",
						bank, reg);
					/* Error is not returned here since
					 * the output is wanted in any case */
					return 0;
				}
			} else {
				printk(KERN_INFO" [%u/0x%02X]: 0x%02X\n", bank,
					reg, value);
536 537 538 539
			}
		}
	}
	return 0;
M
Mattias Wallin 已提交
540 541
}

542 543 544 545 546 547 548 549 550 551 552 553 554
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");

	seq_printf(s, " bank %u:\n", bank);

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

M
Mattias Wallin 已提交
555 556
static int ab8500_registers_open(struct inode *inode, struct file *file)
{
557
	return single_open(file, ab8500_print_bank_registers, inode->i_private);
M
Mattias Wallin 已提交
558 559 560
}

static const struct file_operations ab8500_registers_fops = {
561 562 563 564 565
	.open = ab8500_registers_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
566 567
};

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
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");

	for (i = 1; i < AB8500_NUM_BANKS; i++) {
		err = seq_printf(s, " bank %u:\n", i);
		if (err < 0)
			dev_err(dev, "seq_printf overflow, bank=%d\n", i);

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

586 587 588 589 590 591 592 593 594 595 596 597 598
/* 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++) {
		printk(KERN_INFO" bank %u:\n", i);
		ab8500_registers_print(dev, i, NULL);
	}
}

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
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 已提交
626 627
static int ab8500_bank_print(struct seq_file *s, void *p)
{
628
	return seq_printf(s, "%d\n", debug_bank);
M
Mattias Wallin 已提交
629 630 631 632
}

static int ab8500_bank_open(struct inode *inode, struct file *file)
{
633
	return single_open(file, ab8500_bank_print, inode->i_private);
M
Mattias Wallin 已提交
634 635 636
}

static ssize_t ab8500_bank_write(struct file *file,
637 638
	const char __user *user_buf,
	size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
639
{
640 641 642 643 644
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_bank;
	int err;

	/* Get userspace string and assure termination */
645
	err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
646
	if (err)
647
		return err;
648 649 650 651 652 653 654 655

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

	debug_bank = user_bank;

656
	return count;
M
Mattias Wallin 已提交
657 658 659 660
}

static int ab8500_address_print(struct seq_file *s, void *p)
{
661
	return seq_printf(s, "0x%02X\n", debug_address);
M
Mattias Wallin 已提交
662 663 664 665
}

static int ab8500_address_open(struct inode *inode, struct file *file)
{
666
	return single_open(file, ab8500_address_print, inode->i_private);
M
Mattias Wallin 已提交
667 668 669
}

static ssize_t ab8500_address_write(struct file *file,
670 671
	const char __user *user_buf,
	size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
672
{
673 674 675 676 677
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_address;
	int err;

	/* Get userspace string and assure termination */
678
	err = kstrtoul_from_user(user_buf, count, 0, &user_address);
679
	if (err)
680 681
		return err;

682 683 684 685 686
	if (user_address > 0xff) {
		dev_err(dev, "debugfs error input > 0xff\n");
		return -EINVAL;
	}
	debug_address = user_address;
687
	return count;
M
Mattias Wallin 已提交
688 689 690 691
}

static int ab8500_val_print(struct seq_file *s, void *p)
{
692 693 694 695 696 697 698 699 700 701 702 703 704 705
	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 已提交
706 707 708 709
}

static int ab8500_val_open(struct inode *inode, struct file *file)
{
710
	return single_open(file, ab8500_val_print, inode->i_private);
M
Mattias Wallin 已提交
711 712 713
}

static ssize_t ab8500_val_write(struct file *file,
714 715
	const char __user *user_buf,
	size_t count, loff_t *ppos)
M
Mattias Wallin 已提交
716
{
717 718 719 720 721
	struct device *dev = ((struct seq_file *)(file->private_data))->private;
	unsigned long user_val;
	int err;

	/* Get userspace string and assure termination */
722
	err = kstrtoul_from_user(user_buf, count, 0, &user_val);
723
	if (err)
724 725
		return err;

726 727 728 729 730 731 732 733 734 735 736
	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;
	}

737
	return count;
M
Mattias Wallin 已提交
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
/*
 * Interrupt status
 */
static u32 num_interrupts[AB8500_MAX_NR_IRQS];
static int num_interrupt_lines;

void ab8500_debug_register_interrupt(int line)
{
	if (line < num_interrupt_lines)
		num_interrupts[line]++;
}

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

	seq_printf(s, "irq:  number of\n");

	for (line = 0; line < num_interrupt_lines; line++)
		seq_printf(s, "%3i:  %6i\n", line, num_interrupts[line]);

	return 0;
}

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

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

804 805 806 807 808 809
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;

810
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	bat_ctrl_raw = ab8500_gpadc_read_raw(gpadc, BAT_CTRL);
	bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc,
			BAT_CTRL, bat_ctrl_raw);

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

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;

838
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
	btemp_ball_raw = ab8500_gpadc_read_raw(gpadc, BTEMP_BALL);
	btemp_ball_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL,
			btemp_ball_raw);

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

static int ab8500_gpadc_btemp_ball_open(struct inode *inode,
		struct file *file)
{
	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;

867
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	main_charger_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_V);
	main_charger_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
			MAIN_CHARGER_V, main_charger_v_raw);

	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,
		struct file *file)
{
	return single_open(file, ab8500_gpadc_main_charger_v_print,
			inode->i_private);
}

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;

897
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	acc_detect1_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT1);
	acc_detect1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ACC_DETECT1,
			acc_detect1_raw);

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

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

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;

927
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	acc_detect2_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT2);
	acc_detect2_convert = ab8500_gpadc_ad_to_voltage(gpadc,
	    ACC_DETECT2, acc_detect2_raw);

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

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

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;

957
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	aux1_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX1);
	aux1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX1,
			aux1_raw);

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

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;

985
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	aux2_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX2);
	aux2_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX2,
			aux2_raw);

	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;

1013
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	main_bat_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_BAT_V);
	main_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V,
			main_bat_v_raw);

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

static int ab8500_gpadc_main_bat_v_open(struct inode *inode,
		struct file *file)
{
	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;

1042
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	vbus_v_raw = ab8500_gpadc_read_raw(gpadc, VBUS_V);
	vbus_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBUS_V,
			vbus_v_raw);

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

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;

1070
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	main_charger_c_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_C);
	main_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
			MAIN_CHARGER_C, main_charger_c_raw);

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

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

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;

1100
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	usb_charger_c_raw = ab8500_gpadc_read_raw(gpadc, USB_CHARGER_C);
	usb_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
	    USB_CHARGER_C, usb_charger_c_raw);

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

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

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;

1130
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
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
	bk_bat_v_raw = ab8500_gpadc_read_raw(gpadc, BK_BAT_V);
	bk_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
			BK_BAT_V, bk_bat_v_raw);

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

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;

1158
	gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	die_temp_raw = ab8500_gpadc_read_raw(gpadc, DIE_TEMP);
	die_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, DIE_TEMP,
			die_temp_raw);

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

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

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 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
/*
 * 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
 */
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
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,
			   inode->i_private);
}

/*
1359
 * Userspace should use poll() on this file. When an event occur
1360 1361 1362 1363 1364
 * the blocking poll will be released.
 */
static ssize_t show_irq(struct device *dev,
			struct device_attribute *attr, char *buf)
{
1365 1366 1367
	unsigned long name;
	unsigned int irq_index;
	int err;
1368

1369 1370 1371 1372 1373
	err = strict_strtoul(attr->attr.name, 0, &name);
	if (err)
		return err;

	irq_index = name - irq_first;
1374
	if (irq_index >= num_irqs)
1375 1376 1377 1378
		return -EINVAL;
	else
		return sprintf(buf, "%u\n", irq_count[irq_index]);
}
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

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;
	char buf[32];
	int buf_size;
	unsigned long user_val;
	int err;
1389
	unsigned int irq_index;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408

	/* 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;

	err = strict_strtoul(buf, 0, &user_val);
	if (err)
		return -EINVAL;
	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;
	}

1409
	irq_index = user_val - irq_first;
1410
	if (irq_index >= num_irqs)
1411 1412
		return -EINVAL;

1413
	/*
1414
	 * This will create a sysfs file named <irq-nr> which userspace can
1415 1416
	 * use to select or poll and get the AB8500 events
	 */
1417 1418 1419 1420 1421 1422 1423 1424 1425
	dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
		GFP_KERNEL);
	event_name[irq_index] = kmalloc(buf_size, GFP_KERNEL);
	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);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	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);
1437
		sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
		return err;
	}

	return buf_size;
}

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;
	char buf[32];
	int buf_size;
	unsigned long user_val;
	int err;
1453
	unsigned int irq_index;
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472

	/* 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;

	err = strict_strtoul(buf, 0, &user_val);
	if (err)
		return -EINVAL;
	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;
	}

1473
	irq_index = user_val - irq_first;
1474
	if (irq_index >= num_irqs)
1475 1476 1477 1478 1479 1480 1481
		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);
1482

1483 1484 1485 1486

	free_irq(user_val, &dev->kobj);
	kfree(event_name[irq_index]);
	kfree(dev_attr[irq_index]);
1487 1488 1489 1490

	return buf_size;
}

1491 1492 1493 1494
/*
 * - several deubgfs nodes fops
 */

M
Mattias Wallin 已提交
1495
static const struct file_operations ab8500_bank_fops = {
1496 1497 1498 1499 1500 1501
	.open = ab8500_bank_open,
	.write = ab8500_bank_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
1502 1503 1504
};

static const struct file_operations ab8500_address_fops = {
1505 1506 1507 1508 1509 1510
	.open = ab8500_address_open,
	.write = ab8500_address_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
1511 1512 1513
};

static const struct file_operations ab8500_val_fops = {
1514 1515 1516 1517 1518 1519
	.open = ab8500_val_open,
	.write = ab8500_val_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
M
Mattias Wallin 已提交
1520 1521
};

1522 1523 1524 1525 1526 1527 1528 1529
static const struct file_operations ab8500_interrupts_fops = {
	.open = ab8500_interrupts_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
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,
};

1548 1549 1550 1551 1552 1553 1554 1555 1556
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 已提交
1557
static struct dentry *ab8500_dir;
1558
static struct dentry *ab8500_gpadc_dir;
M
Mattias Wallin 已提交
1559

B
Bill Pemberton 已提交
1560
static int ab8500_debug_probe(struct platform_device *plf)
M
Mattias Wallin 已提交
1561
{
1562
	struct dentry *file;
1563 1564
	int ret = -ENOMEM;
	struct ab8500 *ab8500;
1565 1566
	debug_bank = AB8500_MISC;
	debug_address = AB8500_REV_REG & 0x00FF;
M
Mattias Wallin 已提交
1567

1568 1569 1570
	ab8500 = dev_get_drvdata(plf->dev.parent);
	num_irqs = ab8500->mask_size;

1571
	irq_count = kzalloc(sizeof(*irq_count)*num_irqs, GFP_KERNEL);
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	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;

1583 1584 1585
	irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
	if (irq_first < 0) {
		dev_err(&plf->dev, "First irq not found, err %d\n",
1586
				irq_first);
1587 1588
		ret = irq_first;
		goto out_freeevent_name;
1589 1590 1591 1592 1593
	}

	irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
	if (irq_last < 0) {
		dev_err(&plf->dev, "Last irq not found, err %d\n",
1594
				irq_last);
1595 1596
		ret = irq_last;
                goto out_freeevent_name;
1597 1598
	}

1599 1600
	ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
	if (!ab8500_dir)
1601
		goto err;
M
Mattias Wallin 已提交
1602

1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
	ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING,
	    ab8500_dir);
	if (!ab8500_gpadc_dir)
		goto err;

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

1613 1614 1615 1616 1617
	file = debugfs_create_file("all-banks", S_IRUGO,
	    ab8500_dir, &plf->dev, &ab8500_all_banks_fops);
	if (!file)
		goto err;

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
	file = debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR),
	    ab8500_dir, &plf->dev, &ab8500_bank_fops);
	if (!file)
		goto err;

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

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

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

1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	if (is_ab8500(ab8500))
		num_interrupt_lines = AB8500_NR_IRQS;
	else if (is_ab8505(ab8500))
		num_interrupt_lines = AB8505_NR_IRQS;
	else if (is_ab9540(ab8500))
		num_interrupt_lines = AB9540_NR_IRQS;

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

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 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
	file = debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR),
	    ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR),
	    ab8500_dir, &plf->dev, &ab8500_hwreg_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bat_ctrl_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_btemp_ball_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_v_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect1_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect2_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux1_fops);
	if (!file)
		goto err;

	file = debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux2_fops);
1692 1693
	if (!file)
		goto err;
M
Mattias Wallin 已提交
1694

1695 1696
	file = debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_bat_v_fops);
1697 1698
	if (!file)
		goto err;
M
Mattias Wallin 已提交
1699

1700 1701
	file = debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_vbus_v_fops);
1702 1703
	if (!file)
		goto err;
M
Mattias Wallin 已提交
1704

1705 1706
	file = debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_c_fops);
1707 1708 1709
	if (!file)
		goto err;

1710 1711
	file = debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_charger_c_fops);
1712 1713 1714
	if (!file)
		goto err;

1715 1716
	file = debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bk_bat_v_fops);
1717 1718 1719
	if (!file)
		goto err;

1720 1721
	file = debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR),
	    ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_die_temp_fops);
1722 1723
	if (!file)
		goto err;
1724

1725
	return 0;
M
Mattias Wallin 已提交
1726

1727 1728 1729
err:
	if (ab8500_dir)
		debugfs_remove_recursive(ab8500_dir);
1730
	dev_err(&plf->dev, "failed to create debugfs entries.\n");
1731 1732 1733 1734 1735 1736 1737 1738
out_freeevent_name:
	kfree(event_name);
out_freedev_attr:
	kfree(dev_attr);
out_freeirq_count:
	kfree(irq_count);

	return ret;
M
Mattias Wallin 已提交
1739 1740
}

B
Bill Pemberton 已提交
1741
static int ab8500_debug_remove(struct platform_device *plf)
M
Mattias Wallin 已提交
1742
{
1743
	debugfs_remove_recursive(ab8500_dir);
1744 1745 1746 1747
	kfree(event_name);
	kfree(dev_attr);
	kfree(irq_count);

1748
	return 0;
M
Mattias Wallin 已提交
1749 1750 1751
}

static struct platform_driver ab8500_debug_driver = {
1752 1753 1754 1755 1756
	.driver = {
		.name = "ab8500-debug",
		.owner = THIS_MODULE,
	},
	.probe  = ab8500_debug_probe,
B
Bill Pemberton 已提交
1757
	.remove = ab8500_debug_remove
M
Mattias Wallin 已提交
1758 1759 1760 1761
};

static int __init ab8500_debug_init(void)
{
1762
	return platform_driver_register(&ab8500_debug_driver);
M
Mattias Wallin 已提交
1763 1764 1765 1766
}

static void __exit ab8500_debug_exit(void)
{
1767
	platform_driver_unregister(&ab8500_debug_driver);
M
Mattias Wallin 已提交
1768 1769 1770 1771 1772 1773 1774
}
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");