2 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
5 * License Terms: GNU General Public License v2
8 #include <linux/seq_file.h>
9 #include <linux/uaccess.h>
11 #include <linux/module.h>
12 #include <linux/debugfs.h>
13 #include <linux/platform_device.h>
15 #include <linux/mfd/abx500.h>
16 #include <linux/mfd/abx500/ab8500.h>
18 static u32 debug_bank
;
19 static u32 debug_address
;
22 * struct ab8500_reg_range
23 * @first: the first address of the range
24 * @last: the last address of the range
25 * @perm: access permissions for the range
27 struct ab8500_reg_range
{
34 * struct ab8500_i2c_ranges
35 * @num_ranges: the number of ranges in the list
36 * @bankid: bank identifier
37 * @range: the list of register ranges
39 struct ab8500_i2c_ranges
{
42 const struct ab8500_reg_range
*range
;
45 #define AB8500_NAME_STRING "ab8500"
46 #define AB8500_NUM_BANKS 22
48 #define AB8500_REV_REG 0x80
50 static struct ab8500_i2c_ranges debug_ranges
[AB8500_NUM_BANKS
] = {
55 [AB8500_SYS_CTRL1_BLOCK
] = {
57 .range
= (struct ab8500_reg_range
[]) {
72 [AB8500_SYS_CTRL2_BLOCK
] = {
74 .range
= (struct ab8500_reg_range
[]) {
93 [AB8500_REGU_CTRL1
] = {
95 .range
= (struct ab8500_reg_range
[]) {
110 [AB8500_REGU_CTRL2
] = {
112 .range
= (struct ab8500_reg_range
[]) {
133 /* 0x80-0x8B is SIM registers and should
134 * not be accessed from here */
139 .range
= (struct ab8500_reg_range
[]) {
152 .range
= (struct ab8500_reg_range
[]) {
195 [AB8500_ECI_AV_ACC
] = {
197 .range
= (struct ab8500_reg_range
[]) {
210 .range
= (struct ab8500_reg_range
[]) {
219 .range
= (struct ab8500_reg_range
[]) {
254 [AB8500_GAS_GAUGE
] = {
256 .range
= (struct ab8500_reg_range
[]) {
273 .range
= (struct ab8500_reg_range
[]) {
280 [AB8500_INTERRUPT
] = {
286 .range
= (struct ab8500_reg_range
[]) {
295 .range
= (struct ab8500_reg_range
[]) {
346 [AB8500_OTP_EMUL
] = {
348 .range
= (struct ab8500_reg_range
[]) {
357 static int ab8500_registers_print(struct seq_file
*s
, void *p
)
359 struct device
*dev
= s
->private;
361 u32 bank
= debug_bank
;
363 seq_printf(s
, AB8500_NAME_STRING
" register values:\n");
365 seq_printf(s
, " bank %u:\n", bank
);
366 for (i
= 0; i
< debug_ranges
[bank
].num_ranges
; i
++) {
369 for (reg
= debug_ranges
[bank
].range
[i
].first
;
370 reg
<= debug_ranges
[bank
].range
[i
].last
;
375 err
= abx500_get_register_interruptible(dev
,
376 (u8
)bank
, (u8
)reg
, &value
);
378 dev_err(dev
, "ab->read fail %d\n", err
);
382 err
= seq_printf(s
, " [%u/0x%02X]: 0x%02X\n", bank
,
385 dev_err(dev
, "seq_printf overflow\n");
386 /* Error is not returned here since
387 * the output is wanted in any case */
395 static int ab8500_registers_open(struct inode
*inode
, struct file
*file
)
397 return single_open(file
, ab8500_registers_print
, inode
->i_private
);
400 static const struct file_operations ab8500_registers_fops
= {
401 .open
= ab8500_registers_open
,
404 .release
= single_release
,
405 .owner
= THIS_MODULE
,
408 static int ab8500_bank_print(struct seq_file
*s
, void *p
)
410 return seq_printf(s
, "%d\n", debug_bank
);
413 static int ab8500_bank_open(struct inode
*inode
, struct file
*file
)
415 return single_open(file
, ab8500_bank_print
, inode
->i_private
);
418 static ssize_t
ab8500_bank_write(struct file
*file
,
419 const char __user
*user_buf
,
420 size_t count
, loff_t
*ppos
)
422 struct device
*dev
= ((struct seq_file
*)(file
->private_data
))->private;
423 unsigned long user_bank
;
426 /* Get userspace string and assure termination */
427 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_bank
);
431 if (user_bank
>= AB8500_NUM_BANKS
) {
432 dev_err(dev
, "debugfs error input > number of banks\n");
436 debug_bank
= user_bank
;
441 static int ab8500_address_print(struct seq_file
*s
, void *p
)
443 return seq_printf(s
, "0x%02X\n", debug_address
);
446 static int ab8500_address_open(struct inode
*inode
, struct file
*file
)
448 return single_open(file
, ab8500_address_print
, inode
->i_private
);
451 static ssize_t
ab8500_address_write(struct file
*file
,
452 const char __user
*user_buf
,
453 size_t count
, loff_t
*ppos
)
455 struct device
*dev
= ((struct seq_file
*)(file
->private_data
))->private;
456 unsigned long user_address
;
459 /* Get userspace string and assure termination */
460 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_address
);
464 if (user_address
> 0xff) {
465 dev_err(dev
, "debugfs error input > 0xff\n");
468 debug_address
= user_address
;
472 static int ab8500_val_print(struct seq_file
*s
, void *p
)
474 struct device
*dev
= s
->private;
478 ret
= abx500_get_register_interruptible(dev
,
479 (u8
)debug_bank
, (u8
)debug_address
, ®value
);
481 dev_err(dev
, "abx500_get_reg fail %d, %d\n",
485 seq_printf(s
, "0x%02X\n", regvalue
);
490 static int ab8500_val_open(struct inode
*inode
, struct file
*file
)
492 return single_open(file
, ab8500_val_print
, inode
->i_private
);
495 static ssize_t
ab8500_val_write(struct file
*file
,
496 const char __user
*user_buf
,
497 size_t count
, loff_t
*ppos
)
499 struct device
*dev
= ((struct seq_file
*)(file
->private_data
))->private;
500 unsigned long user_val
;
503 /* Get userspace string and assure termination */
504 err
= kstrtoul_from_user(user_buf
, count
, 0, &user_val
);
508 if (user_val
> 0xff) {
509 dev_err(dev
, "debugfs error input > 0xff\n");
512 err
= abx500_set_register_interruptible(dev
,
513 (u8
)debug_bank
, debug_address
, (u8
)user_val
);
515 printk(KERN_ERR
"abx500_set_reg failed %d, %d", err
, __LINE__
);
522 static const struct file_operations ab8500_bank_fops
= {
523 .open
= ab8500_bank_open
,
524 .write
= ab8500_bank_write
,
527 .release
= single_release
,
528 .owner
= THIS_MODULE
,
531 static const struct file_operations ab8500_address_fops
= {
532 .open
= ab8500_address_open
,
533 .write
= ab8500_address_write
,
536 .release
= single_release
,
537 .owner
= THIS_MODULE
,
540 static const struct file_operations ab8500_val_fops
= {
541 .open
= ab8500_val_open
,
542 .write
= ab8500_val_write
,
545 .release
= single_release
,
546 .owner
= THIS_MODULE
,
549 static struct dentry
*ab8500_dir
;
550 static struct dentry
*ab8500_reg_file
;
551 static struct dentry
*ab8500_bank_file
;
552 static struct dentry
*ab8500_address_file
;
553 static struct dentry
*ab8500_val_file
;
555 static int __devinit
ab8500_debug_probe(struct platform_device
*plf
)
557 debug_bank
= AB8500_MISC
;
558 debug_address
= AB8500_REV_REG
& 0x00FF;
560 ab8500_dir
= debugfs_create_dir(AB8500_NAME_STRING
, NULL
);
562 goto exit_no_debugfs
;
564 ab8500_reg_file
= debugfs_create_file("all-bank-registers",
565 S_IRUGO
, ab8500_dir
, &plf
->dev
, &ab8500_registers_fops
);
566 if (!ab8500_reg_file
)
567 goto exit_destroy_dir
;
569 ab8500_bank_file
= debugfs_create_file("register-bank",
570 (S_IRUGO
| S_IWUSR
), ab8500_dir
, &plf
->dev
, &ab8500_bank_fops
);
571 if (!ab8500_bank_file
)
572 goto exit_destroy_reg
;
574 ab8500_address_file
= debugfs_create_file("register-address",
575 (S_IRUGO
| S_IWUSR
), ab8500_dir
, &plf
->dev
,
576 &ab8500_address_fops
);
577 if (!ab8500_address_file
)
578 goto exit_destroy_bank
;
580 ab8500_val_file
= debugfs_create_file("register-value",
581 (S_IRUGO
| S_IWUSR
), ab8500_dir
, &plf
->dev
, &ab8500_val_fops
);
582 if (!ab8500_val_file
)
583 goto exit_destroy_address
;
587 exit_destroy_address
:
588 debugfs_remove(ab8500_address_file
);
590 debugfs_remove(ab8500_bank_file
);
592 debugfs_remove(ab8500_reg_file
);
594 debugfs_remove(ab8500_dir
);
596 dev_err(&plf
->dev
, "failed to create debugfs entries.\n");
600 static int __devexit
ab8500_debug_remove(struct platform_device
*plf
)
602 debugfs_remove(ab8500_val_file
);
603 debugfs_remove(ab8500_address_file
);
604 debugfs_remove(ab8500_bank_file
);
605 debugfs_remove(ab8500_reg_file
);
606 debugfs_remove(ab8500_dir
);
611 static struct platform_driver ab8500_debug_driver
= {
613 .name
= "ab8500-debug",
614 .owner
= THIS_MODULE
,
616 .probe
= ab8500_debug_probe
,
617 .remove
= __devexit_p(ab8500_debug_remove
)
620 static int __init
ab8500_debug_init(void)
622 return platform_driver_register(&ab8500_debug_driver
);
625 static void __exit
ab8500_debug_exit(void)
627 platform_driver_unregister(&ab8500_debug_driver
);
629 subsys_initcall(ab8500_debug_init
);
630 module_exit(ab8500_debug_exit
);
632 MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
633 MODULE_DESCRIPTION("AB8500 DEBUG");
634 MODULE_LICENSE("GPL v2");