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 * AB8500 register access
9 * ======================
12 * # echo BANK > <debugfs>/ab8500/register-bank
13 * # echo ADDR > <debugfs>/ab8500/register-address
14 * # cat <debugfs>/ab8500/register-value
17 * # echo BANK > <debugfs>/ab8500/register-bank
18 * # echo ADDR > <debugfs>/ab8500/register-address
19 * # echo VALUE > <debugfs>/ab8500/register-value
21 * read all registers from a bank:
22 * # echo BANK > <debugfs>/ab8500/register-bank
23 * # cat <debugfs>/ab8500/all-bank-register
25 * BANK target AB8500 register bank
26 * ADDR target AB8500 register address
27 * VALUE decimal or 0x-prefixed hexadecimal
30 * User Space notification on AB8500 IRQ
31 * =====================================
33 * Allows user space entity to be notified when target AB8500 IRQ occurs.
34 * When subscribed, a sysfs entry is created in ab8500.i2c platform device.
35 * One can pool this file to get target IRQ occurence information.
37 * subscribe to an AB8500 IRQ:
38 * # echo IRQ > <debugfs>/ab8500/irq-subscribe
40 * unsubscribe from an AB8500 IRQ:
41 * # echo IRQ > <debugfs>/ab8500/irq-unsubscribe
44 * AB8500 register formated read/write access
45 * ==========================================
47 * Read: read data, data>>SHIFT, data&=MASK, output data
48 * [0xABCDEF98] shift=12 mask=0xFFF => 0x00000CDE
49 * Write: read data, data &= ~(MASK<<SHIFT), data |= (VALUE<<SHIFT), write data
50 * [0xABCDEF98] shift=12 mask=0xFFF value=0x123 => [0xAB123F98]
53 * # echo "CMD [OPTIONS] BANK ADRESS [VALUE]" > $debugfs/ab8500/hwreg
55 * CMD read read access
58 * BANK target reg bank
59 * ADDRESS target reg address
60 * VALUE (write) value to be updated
63 * -d|-dec (read) output in decimal
64 * -h|-hexa (read) output in 0x-hexa (default)
65 * -l|-w|-b 32bit (default), 16bit or 8bit reg access
66 * -m|-mask MASK 0x-hexa mask (default 0xFFFFFFFF)
67 * -s|-shift SHIFT bit shift value (read:left, write:right)
68 * -o|-offset OFFSET address offset to add to ADDRESS value
70 * Warning: bit shift operation is applied to bit-mask.
71 * Warning: bit shift direction depends on read or right command.
74 #include <linux/seq_file.h>
75 #include <linux/uaccess.h>
77 #include <linux/module.h>
78 #include <linux/debugfs.h>
79 #include <linux/platform_device.h>
80 #include <linux/interrupt.h>
81 #include <linux/kobject.h>
82 #include <linux/slab.h>
84 #include <linux/mfd/abx500.h>
85 #include <linux/mfd/abx500/ab8500-gpadc.h>
87 #ifdef CONFIG_DEBUG_FS
88 #include <linux/string.h>
89 #include <linux/ctype.h>
92 static u32 debug_bank;
93 static u32 debug_address;
97 static u32 *irq_count;
100 static struct device_attribute **dev_attr;
101 static char **event_name;
104 * struct ab8500_reg_range
105 * @first: the first address of the range
106 * @last: the last address of the range
107 * @perm: access permissions for the range
109 struct ab8500_reg_range {
116 * struct ab8500_prcmu_ranges
117 * @num_ranges: the number of ranges in the list
118 * @bankid: bank identifier
119 * @range: the list of register ranges
121 struct ab8500_prcmu_ranges {
124 const struct ab8500_reg_range *range;
127 /* hwreg- "mask" and "shift" entries ressources */
129 u32 bank; /* target bank */
130 u32 addr; /* target address */
131 uint fmt; /* format */
132 uint mask; /* read/write mask, applied before any bit shift */
133 int shift; /* bit shift (read:right shift, write:left shift */
135 /* fmt bit #0: 0=hexa, 1=dec */
136 #define REG_FMT_DEC(c) ((c)->fmt & 0x1)
137 #define REG_FMT_HEX(c) (!REG_FMT_DEC(c))
139 static struct hwreg_cfg hwreg_cfg = {
140 .addr = 0, /* default: invalid phys addr */
141 .fmt = 0, /* default: 32bit access, hex output */
142 .mask = 0xFFFFFFFF, /* default: no mask */
143 .shift = 0, /* default: no bit shift */
146 #define AB8500_NAME_STRING "ab8500"
147 #define AB8500_ADC_NAME_STRING "gpadc"
148 #define AB8500_NUM_BANKS 24
150 #define AB8500_REV_REG 0x80
152 static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = {
157 [AB8500_SYS_CTRL1_BLOCK] = {
159 .range = (struct ab8500_reg_range[]) {
174 [AB8500_SYS_CTRL2_BLOCK] = {
176 .range = (struct ab8500_reg_range[]) {
195 [AB8500_REGU_CTRL1] = {
197 .range = (struct ab8500_reg_range[]) {
212 [AB8500_REGU_CTRL2] = {
214 .range = (struct ab8500_reg_range[]) {
235 /* 0x80-0x8B is SIM registers and should
236 * not be accessed from here */
241 .range = (struct ab8500_reg_range[]) {
254 .range = (struct ab8500_reg_range[]) {
297 [AB8500_ECI_AV_ACC] = {
299 .range = (struct ab8500_reg_range[]) {
312 .range = (struct ab8500_reg_range[]) {
321 .range = (struct ab8500_reg_range[]) {
360 [AB8500_GAS_GAUGE] = {
362 .range = (struct ab8500_reg_range[]) {
377 [AB8500_DEVELOPMENT] = {
379 .range = (struct ab8500_reg_range[]) {
388 .range = (struct ab8500_reg_range[]) {
397 .range = (struct ab8500_reg_range[]) {
404 [AB8500_INTERRUPT] = {
410 .range = (struct ab8500_reg_range[]) {
419 .range = (struct ab8500_reg_range[]) {
470 [AB8500_OTP_EMUL] = {
472 .range = (struct ab8500_reg_range[]) {
481 static irqreturn_t ab8500_debug_handler(int irq, void *data)
484 struct kobject *kobj = (struct kobject *)data;
485 unsigned int irq_abb = irq - irq_first;
487 if (irq_abb < num_irqs)
488 irq_count[irq_abb]++;
490 * This makes it possible to use poll for events (POLLPRI | POLLERR)
491 * from userspace on sysfs file named <irq-nr>
493 sprintf(buf, "%d", irq);
494 sysfs_notify(kobj, NULL, buf);
499 /* Prints to seq_file or log_buf */
500 static int ab8500_registers_print(struct device *dev, u32 bank,
505 for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
508 for (reg = debug_ranges[bank].range[i].first;
509 reg <= debug_ranges[bank].range[i].last;
514 err = abx500_get_register_interruptible(dev,
515 (u8)bank, (u8)reg, &value);
517 dev_err(dev, "ab->read fail %d\n", err);
522 err = seq_printf(s, " [%u/0x%02X]: 0x%02X\n",
526 "seq_printf overflow bank=%d reg=%d\n",
528 /* Error is not returned here since
529 * the output is wanted in any case */
533 printk(KERN_INFO" [%u/0x%02X]: 0x%02X\n", bank,
541 static int ab8500_print_bank_registers(struct seq_file *s, void *p)
543 struct device *dev = s->private;
544 u32 bank = debug_bank;
546 seq_printf(s, AB8500_NAME_STRING " register values:\n");
548 seq_printf(s, " bank %u:\n", bank);
550 ab8500_registers_print(dev, bank, s);
554 static int ab8500_registers_open(struct inode *inode, struct file *file)
556 return single_open(file, ab8500_print_bank_registers, inode->i_private);
559 static const struct file_operations ab8500_registers_fops = {
560 .open = ab8500_registers_open,
563 .release = single_release,
564 .owner = THIS_MODULE,
567 static int ab8500_print_all_banks(struct seq_file *s, void *p)
569 struct device *dev = s->private;
573 seq_printf(s, AB8500_NAME_STRING " register values:\n");
575 for (i = 1; i < AB8500_NUM_BANKS; i++) {
576 err = seq_printf(s, " bank %u:\n", i);
578 dev_err(dev, "seq_printf overflow, bank=%d\n", i);
580 ab8500_registers_print(dev, i, s);
585 /* Dump registers to kernel log */
586 void ab8500_dump_all_banks(struct device *dev)
590 printk(KERN_INFO"ab8500 register values:\n");
592 for (i = 1; i < AB8500_NUM_BANKS; i++) {
593 printk(KERN_INFO" bank %u:\n", i);
594 ab8500_registers_print(dev, i, NULL);
598 static int ab8500_all_banks_open(struct inode *inode, struct file *file)
603 err = single_open(file, ab8500_print_all_banks, inode->i_private);
605 /* Default buf size in seq_read is not enough */
606 s = (struct seq_file *)file->private_data;
607 s->size = (PAGE_SIZE * 2);
608 s->buf = kmalloc(s->size, GFP_KERNEL);
610 single_release(inode, file);
617 static const struct file_operations ab8500_all_banks_fops = {
618 .open = ab8500_all_banks_open,
621 .release = single_release,
622 .owner = THIS_MODULE,
625 static int ab8500_bank_print(struct seq_file *s, void *p)
627 return seq_printf(s, "%d\n", debug_bank);
630 static int ab8500_bank_open(struct inode *inode, struct file *file)
632 return single_open(file, ab8500_bank_print, inode->i_private);
635 static ssize_t ab8500_bank_write(struct file *file,
636 const char __user *user_buf,
637 size_t count, loff_t *ppos)
639 struct device *dev = ((struct seq_file *)(file->private_data))->private;
640 unsigned long user_bank;
643 /* Get userspace string and assure termination */
644 err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
648 if (user_bank >= AB8500_NUM_BANKS) {
649 dev_err(dev, "debugfs error input > number of banks\n");
653 debug_bank = user_bank;
658 static int ab8500_address_print(struct seq_file *s, void *p)
660 return seq_printf(s, "0x%02X\n", debug_address);
663 static int ab8500_address_open(struct inode *inode, struct file *file)
665 return single_open(file, ab8500_address_print, inode->i_private);
668 static ssize_t ab8500_address_write(struct file *file,
669 const char __user *user_buf,
670 size_t count, loff_t *ppos)
672 struct device *dev = ((struct seq_file *)(file->private_data))->private;
673 unsigned long user_address;
676 /* Get userspace string and assure termination */
677 err = kstrtoul_from_user(user_buf, count, 0, &user_address);
681 if (user_address > 0xff) {
682 dev_err(dev, "debugfs error input > 0xff\n");
685 debug_address = user_address;
689 static int ab8500_val_print(struct seq_file *s, void *p)
691 struct device *dev = s->private;
695 ret = abx500_get_register_interruptible(dev,
696 (u8)debug_bank, (u8)debug_address, ®value);
698 dev_err(dev, "abx500_get_reg fail %d, %d\n",
702 seq_printf(s, "0x%02X\n", regvalue);
707 static int ab8500_val_open(struct inode *inode, struct file *file)
709 return single_open(file, ab8500_val_print, inode->i_private);
712 static ssize_t ab8500_val_write(struct file *file,
713 const char __user *user_buf,
714 size_t count, loff_t *ppos)
716 struct device *dev = ((struct seq_file *)(file->private_data))->private;
717 unsigned long user_val;
720 /* Get userspace string and assure termination */
721 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
725 if (user_val > 0xff) {
726 dev_err(dev, "debugfs error input > 0xff\n");
729 err = abx500_set_register_interruptible(dev,
730 (u8)debug_bank, debug_address, (u8)user_val);
732 printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
742 static u32 num_interrupts[AB8500_MAX_NR_IRQS];
743 static int num_interrupt_lines;
745 void ab8500_debug_register_interrupt(int line)
747 if (line < num_interrupt_lines)
748 num_interrupts[line]++;
751 static int ab8500_interrupts_print(struct seq_file *s, void *p)
755 seq_printf(s, "irq: number of\n");
757 for (line = 0; line < num_interrupt_lines; line++)
758 seq_printf(s, "%3i: %6i\n", line, num_interrupts[line]);
763 static int ab8500_interrupts_open(struct inode *inode, struct file *file)
765 return single_open(file, ab8500_interrupts_print, inode->i_private);
769 * - HWREG DB8500 formated routines
771 static int ab8500_hwreg_print(struct seq_file *s, void *d)
773 struct device *dev = s->private;
777 ret = abx500_get_register_interruptible(dev,
778 (u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, ®value);
780 dev_err(dev, "abx500_get_reg fail %d, %d\n",
785 if (hwreg_cfg.shift >= 0)
786 regvalue >>= hwreg_cfg.shift;
788 regvalue <<= -hwreg_cfg.shift;
789 regvalue &= hwreg_cfg.mask;
791 if (REG_FMT_DEC(&hwreg_cfg))
792 seq_printf(s, "%d\n", regvalue);
794 seq_printf(s, "0x%02X\n", regvalue);
798 static int ab8500_hwreg_open(struct inode *inode, struct file *file)
800 return single_open(file, ab8500_hwreg_print, inode->i_private);
803 static int ab8500_gpadc_bat_ctrl_print(struct seq_file *s, void *p)
806 int bat_ctrl_convert;
807 struct ab8500_gpadc *gpadc;
809 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
810 bat_ctrl_raw = ab8500_gpadc_read_raw(gpadc, BAT_CTRL);
811 bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc,
812 BAT_CTRL, bat_ctrl_raw);
814 return seq_printf(s, "%d,0x%X\n",
815 bat_ctrl_convert, bat_ctrl_raw);
818 static int ab8500_gpadc_bat_ctrl_open(struct inode *inode, struct file *file)
820 return single_open(file, ab8500_gpadc_bat_ctrl_print, inode->i_private);
823 static const struct file_operations ab8500_gpadc_bat_ctrl_fops = {
824 .open = ab8500_gpadc_bat_ctrl_open,
827 .release = single_release,
828 .owner = THIS_MODULE,
831 static int ab8500_gpadc_btemp_ball_print(struct seq_file *s, void *p)
834 int btemp_ball_convert;
835 struct ab8500_gpadc *gpadc;
837 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
838 btemp_ball_raw = ab8500_gpadc_read_raw(gpadc, BTEMP_BALL);
839 btemp_ball_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL,
843 "%d,0x%X\n", btemp_ball_convert, btemp_ball_raw);
846 static int ab8500_gpadc_btemp_ball_open(struct inode *inode,
849 return single_open(file, ab8500_gpadc_btemp_ball_print, inode->i_private);
852 static const struct file_operations ab8500_gpadc_btemp_ball_fops = {
853 .open = ab8500_gpadc_btemp_ball_open,
856 .release = single_release,
857 .owner = THIS_MODULE,
860 static int ab8500_gpadc_main_charger_v_print(struct seq_file *s, void *p)
862 int main_charger_v_raw;
863 int main_charger_v_convert;
864 struct ab8500_gpadc *gpadc;
866 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
867 main_charger_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_V);
868 main_charger_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
869 MAIN_CHARGER_V, main_charger_v_raw);
871 return seq_printf(s, "%d,0x%X\n",
872 main_charger_v_convert, main_charger_v_raw);
875 static int ab8500_gpadc_main_charger_v_open(struct inode *inode,
878 return single_open(file, ab8500_gpadc_main_charger_v_print,
882 static const struct file_operations ab8500_gpadc_main_charger_v_fops = {
883 .open = ab8500_gpadc_main_charger_v_open,
886 .release = single_release,
887 .owner = THIS_MODULE,
890 static int ab8500_gpadc_acc_detect1_print(struct seq_file *s, void *p)
893 int acc_detect1_convert;
894 struct ab8500_gpadc *gpadc;
896 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
897 acc_detect1_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT1);
898 acc_detect1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ACC_DETECT1,
901 return seq_printf(s, "%d,0x%X\n",
902 acc_detect1_convert, acc_detect1_raw);
905 static int ab8500_gpadc_acc_detect1_open(struct inode *inode,
908 return single_open(file, ab8500_gpadc_acc_detect1_print,
912 static const struct file_operations ab8500_gpadc_acc_detect1_fops = {
913 .open = ab8500_gpadc_acc_detect1_open,
916 .release = single_release,
917 .owner = THIS_MODULE,
920 static int ab8500_gpadc_acc_detect2_print(struct seq_file *s, void *p)
923 int acc_detect2_convert;
924 struct ab8500_gpadc *gpadc;
926 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
927 acc_detect2_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT2);
928 acc_detect2_convert = ab8500_gpadc_ad_to_voltage(gpadc,
929 ACC_DETECT2, acc_detect2_raw);
931 return seq_printf(s, "%d,0x%X\n",
932 acc_detect2_convert, acc_detect2_raw);
935 static int ab8500_gpadc_acc_detect2_open(struct inode *inode,
938 return single_open(file, ab8500_gpadc_acc_detect2_print,
942 static const struct file_operations ab8500_gpadc_acc_detect2_fops = {
943 .open = ab8500_gpadc_acc_detect2_open,
946 .release = single_release,
947 .owner = THIS_MODULE,
950 static int ab8500_gpadc_aux1_print(struct seq_file *s, void *p)
954 struct ab8500_gpadc *gpadc;
956 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
957 aux1_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX1);
958 aux1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX1,
961 return seq_printf(s, "%d,0x%X\n",
962 aux1_convert, aux1_raw);
965 static int ab8500_gpadc_aux1_open(struct inode *inode, struct file *file)
967 return single_open(file, ab8500_gpadc_aux1_print, inode->i_private);
970 static const struct file_operations ab8500_gpadc_aux1_fops = {
971 .open = ab8500_gpadc_aux1_open,
974 .release = single_release,
975 .owner = THIS_MODULE,
978 static int ab8500_gpadc_aux2_print(struct seq_file *s, void *p)
982 struct ab8500_gpadc *gpadc;
984 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
985 aux2_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX2);
986 aux2_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX2,
989 return seq_printf(s, "%d,0x%X\n",
990 aux2_convert, aux2_raw);
993 static int ab8500_gpadc_aux2_open(struct inode *inode, struct file *file)
995 return single_open(file, ab8500_gpadc_aux2_print, inode->i_private);
998 static const struct file_operations ab8500_gpadc_aux2_fops = {
999 .open = ab8500_gpadc_aux2_open,
1001 .llseek = seq_lseek,
1002 .release = single_release,
1003 .owner = THIS_MODULE,
1006 static int ab8500_gpadc_main_bat_v_print(struct seq_file *s, void *p)
1009 int main_bat_v_convert;
1010 struct ab8500_gpadc *gpadc;
1012 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1013 main_bat_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_BAT_V);
1014 main_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V,
1017 return seq_printf(s, "%d,0x%X\n",
1018 main_bat_v_convert, main_bat_v_raw);
1021 static int ab8500_gpadc_main_bat_v_open(struct inode *inode,
1024 return single_open(file, ab8500_gpadc_main_bat_v_print, inode->i_private);
1027 static const struct file_operations ab8500_gpadc_main_bat_v_fops = {
1028 .open = ab8500_gpadc_main_bat_v_open,
1030 .llseek = seq_lseek,
1031 .release = single_release,
1032 .owner = THIS_MODULE,
1035 static int ab8500_gpadc_vbus_v_print(struct seq_file *s, void *p)
1039 struct ab8500_gpadc *gpadc;
1041 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1042 vbus_v_raw = ab8500_gpadc_read_raw(gpadc, VBUS_V);
1043 vbus_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBUS_V,
1046 return seq_printf(s, "%d,0x%X\n",
1047 vbus_v_convert, vbus_v_raw);
1050 static int ab8500_gpadc_vbus_v_open(struct inode *inode, struct file *file)
1052 return single_open(file, ab8500_gpadc_vbus_v_print, inode->i_private);
1055 static const struct file_operations ab8500_gpadc_vbus_v_fops = {
1056 .open = ab8500_gpadc_vbus_v_open,
1058 .llseek = seq_lseek,
1059 .release = single_release,
1060 .owner = THIS_MODULE,
1063 static int ab8500_gpadc_main_charger_c_print(struct seq_file *s, void *p)
1065 int main_charger_c_raw;
1066 int main_charger_c_convert;
1067 struct ab8500_gpadc *gpadc;
1069 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1070 main_charger_c_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_C);
1071 main_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1072 MAIN_CHARGER_C, main_charger_c_raw);
1074 return seq_printf(s, "%d,0x%X\n",
1075 main_charger_c_convert, main_charger_c_raw);
1078 static int ab8500_gpadc_main_charger_c_open(struct inode *inode,
1081 return single_open(file, ab8500_gpadc_main_charger_c_print,
1085 static const struct file_operations ab8500_gpadc_main_charger_c_fops = {
1086 .open = ab8500_gpadc_main_charger_c_open,
1088 .llseek = seq_lseek,
1089 .release = single_release,
1090 .owner = THIS_MODULE,
1093 static int ab8500_gpadc_usb_charger_c_print(struct seq_file *s, void *p)
1095 int usb_charger_c_raw;
1096 int usb_charger_c_convert;
1097 struct ab8500_gpadc *gpadc;
1099 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1100 usb_charger_c_raw = ab8500_gpadc_read_raw(gpadc, USB_CHARGER_C);
1101 usb_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1102 USB_CHARGER_C, usb_charger_c_raw);
1104 return seq_printf(s, "%d,0x%X\n",
1105 usb_charger_c_convert, usb_charger_c_raw);
1108 static int ab8500_gpadc_usb_charger_c_open(struct inode *inode,
1111 return single_open(file, ab8500_gpadc_usb_charger_c_print,
1115 static const struct file_operations ab8500_gpadc_usb_charger_c_fops = {
1116 .open = ab8500_gpadc_usb_charger_c_open,
1118 .llseek = seq_lseek,
1119 .release = single_release,
1120 .owner = THIS_MODULE,
1123 static int ab8500_gpadc_bk_bat_v_print(struct seq_file *s, void *p)
1126 int bk_bat_v_convert;
1127 struct ab8500_gpadc *gpadc;
1129 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1130 bk_bat_v_raw = ab8500_gpadc_read_raw(gpadc, BK_BAT_V);
1131 bk_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
1132 BK_BAT_V, bk_bat_v_raw);
1134 return seq_printf(s, "%d,0x%X\n",
1135 bk_bat_v_convert, bk_bat_v_raw);
1138 static int ab8500_gpadc_bk_bat_v_open(struct inode *inode, struct file *file)
1140 return single_open(file, ab8500_gpadc_bk_bat_v_print, inode->i_private);
1143 static const struct file_operations ab8500_gpadc_bk_bat_v_fops = {
1144 .open = ab8500_gpadc_bk_bat_v_open,
1146 .llseek = seq_lseek,
1147 .release = single_release,
1148 .owner = THIS_MODULE,
1151 static int ab8500_gpadc_die_temp_print(struct seq_file *s, void *p)
1154 int die_temp_convert;
1155 struct ab8500_gpadc *gpadc;
1157 gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
1158 die_temp_raw = ab8500_gpadc_read_raw(gpadc, DIE_TEMP);
1159 die_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, DIE_TEMP,
1162 return seq_printf(s, "%d,0x%X\n",
1163 die_temp_convert, die_temp_raw);
1166 static int ab8500_gpadc_die_temp_open(struct inode *inode, struct file *file)
1168 return single_open(file, ab8500_gpadc_die_temp_print, inode->i_private);
1171 static const struct file_operations ab8500_gpadc_die_temp_fops = {
1172 .open = ab8500_gpadc_die_temp_open,
1174 .llseek = seq_lseek,
1175 .release = single_release,
1176 .owner = THIS_MODULE,
1180 * return length of an ASCII numerical value, 0 is string is not a
1182 * string shall start at value 1st char.
1183 * string can be tailed with \0 or space or newline chars only.
1184 * value can be decimal or hexadecimal (prefixed 0x or 0X).
1186 static int strval_len(char *b)
1189 if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) {
1191 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1198 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1207 * parse hwreg input data.
1208 * update global hwreg_cfg only if input data syntax is ok.
1210 static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg,
1213 uint write, val = 0;
1216 struct hwreg_cfg loc = {
1217 .bank = 0, /* default: invalid phys addr */
1218 .addr = 0, /* default: invalid phys addr */
1219 .fmt = 0, /* default: 32bit access, hex output */
1220 .mask = 0xFFFFFFFF, /* default: no mask */
1221 .shift = 0, /* default: no bit shift */
1224 /* read or write ? */
1225 if (!strncmp(b, "read ", 5)) {
1228 } else if (!strncmp(b, "write ", 6)) {
1234 /* OPTIONS -l|-w|-b -s -m -o */
1235 while ((*b == ' ') || (*b == '-')) {
1236 if (*(b-1) != ' ') {
1240 if ((!strncmp(b, "-d ", 3)) ||
1241 (!strncmp(b, "-dec ", 5))) {
1242 b += (*(b+2) == ' ') ? 3 : 5;
1244 } else if ((!strncmp(b, "-h ", 3)) ||
1245 (!strncmp(b, "-hex ", 5))) {
1246 b += (*(b+2) == ' ') ? 3 : 5;
1248 } else if ((!strncmp(b, "-m ", 3)) ||
1249 (!strncmp(b, "-mask ", 6))) {
1250 b += (*(b+2) == ' ') ? 3 : 6;
1251 if (strval_len(b) == 0)
1253 loc.mask = simple_strtoul(b, &b, 0);
1254 } else if ((!strncmp(b, "-s ", 3)) ||
1255 (!strncmp(b, "-shift ", 7))) {
1256 b += (*(b+2) == ' ') ? 3 : 7;
1257 if (strval_len(b) == 0)
1259 loc.shift = simple_strtol(b, &b, 0);
1264 /* get arg BANK and ADDRESS */
1265 if (strval_len(b) == 0)
1267 loc.bank = simple_strtoul(b, &b, 0);
1270 if (strval_len(b) == 0)
1272 loc.addr = simple_strtoul(b, &b, 0);
1277 if (strval_len(b) == 0)
1279 val = simple_strtoul(b, &b, 0);
1282 /* args are ok, update target cfg (mainly for read) */
1285 #ifdef ABB_HWREG_DEBUG
1286 pr_warn("HWREG request: %s, %s, addr=0x%08X, mask=0x%X, shift=%d"
1287 "value=0x%X\n", (write) ? "write" : "read",
1288 REG_FMT_DEC(cfg) ? "decimal" : "hexa",
1289 cfg->addr, cfg->mask, cfg->shift, val);
1295 ret = abx500_get_register_interruptible(dev,
1296 (u8)cfg->bank, (u8)cfg->addr, ®value);
1298 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1303 if (cfg->shift >= 0) {
1304 regvalue &= ~(cfg->mask << (cfg->shift));
1305 val = (val & cfg->mask) << (cfg->shift);
1307 regvalue &= ~(cfg->mask >> (-cfg->shift));
1308 val = (val & cfg->mask) >> (-cfg->shift);
1310 val = val | regvalue;
1312 ret = abx500_set_register_interruptible(dev,
1313 (u8)cfg->bank, (u8)cfg->addr, (u8)val);
1315 pr_err("abx500_set_reg failed %d, %d", ret, __LINE__);
1322 static ssize_t ab8500_hwreg_write(struct file *file,
1323 const char __user *user_buf, size_t count, loff_t *ppos)
1325 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1329 /* Get userspace string and assure termination */
1330 buf_size = min(count, (sizeof(buf)-1));
1331 if (copy_from_user(buf, user_buf, buf_size))
1335 /* get args and process */
1336 ret = hwreg_common_write(buf, &hwreg_cfg, dev);
1337 return (ret) ? ret : buf_size;
1341 * - irq subscribe/unsubscribe stuff
1343 static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
1345 seq_printf(s, "%d\n", irq_first);
1350 static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
1353 return single_open(file, ab8500_subscribe_unsubscribe_print,
1358 * Userspace should use poll() on this file. When an event occur
1359 * the blocking poll will be released.
1361 static ssize_t show_irq(struct device *dev,
1362 struct device_attribute *attr, char *buf)
1365 unsigned int irq_index;
1368 err = strict_strtoul(attr->attr.name, 0, &name);
1372 irq_index = name - irq_first;
1373 if (irq_index >= num_irqs)
1376 return sprintf(buf, "%u\n", irq_count[irq_index]);
1379 static ssize_t ab8500_subscribe_write(struct file *file,
1380 const char __user *user_buf,
1381 size_t count, loff_t *ppos)
1383 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1386 unsigned long user_val;
1388 unsigned int irq_index;
1390 /* Get userspace string and assure termination */
1391 buf_size = min(count, (sizeof(buf)-1));
1392 if (copy_from_user(buf, user_buf, buf_size))
1396 err = strict_strtoul(buf, 0, &user_val);
1399 if (user_val < irq_first) {
1400 dev_err(dev, "debugfs error input < %d\n", irq_first);
1403 if (user_val > irq_last) {
1404 dev_err(dev, "debugfs error input > %d\n", irq_last);
1408 irq_index = user_val - irq_first;
1409 if (irq_index >= num_irqs)
1413 * This will create a sysfs file named <irq-nr> which userspace can
1414 * use to select or poll and get the AB8500 events
1416 dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
1418 event_name[irq_index] = kmalloc(buf_size, GFP_KERNEL);
1419 sprintf(event_name[irq_index], "%lu", user_val);
1420 dev_attr[irq_index]->show = show_irq;
1421 dev_attr[irq_index]->store = NULL;
1422 dev_attr[irq_index]->attr.name = event_name[irq_index];
1423 dev_attr[irq_index]->attr.mode = S_IRUGO;
1424 err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
1426 printk(KERN_ERR "sysfs_create_file failed %d\n", err);
1430 err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
1431 IRQF_SHARED | IRQF_NO_SUSPEND,
1432 "ab8500-debug", &dev->kobj);
1434 printk(KERN_ERR "request_threaded_irq failed %d, %lu\n",
1436 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1443 static ssize_t ab8500_unsubscribe_write(struct file *file,
1444 const char __user *user_buf,
1445 size_t count, loff_t *ppos)
1447 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1450 unsigned long user_val;
1452 unsigned int irq_index;
1454 /* Get userspace string and assure termination */
1455 buf_size = min(count, (sizeof(buf)-1));
1456 if (copy_from_user(buf, user_buf, buf_size))
1460 err = strict_strtoul(buf, 0, &user_val);
1463 if (user_val < irq_first) {
1464 dev_err(dev, "debugfs error input < %d\n", irq_first);
1467 if (user_val > irq_last) {
1468 dev_err(dev, "debugfs error input > %d\n", irq_last);
1472 irq_index = user_val - irq_first;
1473 if (irq_index >= num_irqs)
1476 /* Set irq count to 0 when unsubscribe */
1477 irq_count[irq_index] = 0;
1479 if (dev_attr[irq_index])
1480 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1483 free_irq(user_val, &dev->kobj);
1484 kfree(event_name[irq_index]);
1485 kfree(dev_attr[irq_index]);
1491 * - several deubgfs nodes fops
1494 static const struct file_operations ab8500_bank_fops = {
1495 .open = ab8500_bank_open,
1496 .write = ab8500_bank_write,
1498 .llseek = seq_lseek,
1499 .release = single_release,
1500 .owner = THIS_MODULE,
1503 static const struct file_operations ab8500_address_fops = {
1504 .open = ab8500_address_open,
1505 .write = ab8500_address_write,
1507 .llseek = seq_lseek,
1508 .release = single_release,
1509 .owner = THIS_MODULE,
1512 static const struct file_operations ab8500_val_fops = {
1513 .open = ab8500_val_open,
1514 .write = ab8500_val_write,
1516 .llseek = seq_lseek,
1517 .release = single_release,
1518 .owner = THIS_MODULE,
1521 static const struct file_operations ab8500_interrupts_fops = {
1522 .open = ab8500_interrupts_open,
1524 .llseek = seq_lseek,
1525 .release = single_release,
1526 .owner = THIS_MODULE,
1529 static const struct file_operations ab8500_subscribe_fops = {
1530 .open = ab8500_subscribe_unsubscribe_open,
1531 .write = ab8500_subscribe_write,
1533 .llseek = seq_lseek,
1534 .release = single_release,
1535 .owner = THIS_MODULE,
1538 static const struct file_operations ab8500_unsubscribe_fops = {
1539 .open = ab8500_subscribe_unsubscribe_open,
1540 .write = ab8500_unsubscribe_write,
1542 .llseek = seq_lseek,
1543 .release = single_release,
1544 .owner = THIS_MODULE,
1547 static const struct file_operations ab8500_hwreg_fops = {
1548 .open = ab8500_hwreg_open,
1549 .write = ab8500_hwreg_write,
1551 .llseek = seq_lseek,
1552 .release = single_release,
1553 .owner = THIS_MODULE,
1556 static struct dentry *ab8500_dir;
1557 static struct dentry *ab8500_gpadc_dir;
1559 static int ab8500_debug_probe(struct platform_device *plf)
1561 struct dentry *file;
1563 struct ab8500 *ab8500;
1564 debug_bank = AB8500_MISC;
1565 debug_address = AB8500_REV_REG & 0x00FF;
1567 ab8500 = dev_get_drvdata(plf->dev.parent);
1568 num_irqs = ab8500->mask_size;
1570 irq_count = kzalloc(sizeof(*irq_count)*num_irqs, GFP_KERNEL);
1574 dev_attr = kzalloc(sizeof(*dev_attr)*num_irqs,GFP_KERNEL);
1576 goto out_freeirq_count;
1578 event_name = kzalloc(sizeof(*event_name)*num_irqs, GFP_KERNEL);
1580 goto out_freedev_attr;
1582 irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
1583 if (irq_first < 0) {
1584 dev_err(&plf->dev, "First irq not found, err %d\n",
1587 goto out_freeevent_name;
1590 irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
1592 dev_err(&plf->dev, "Last irq not found, err %d\n",
1595 goto out_freeevent_name;
1598 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
1602 ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING,
1604 if (!ab8500_gpadc_dir)
1607 file = debugfs_create_file("all-bank-registers", S_IRUGO,
1608 ab8500_dir, &plf->dev, &ab8500_registers_fops);
1612 file = debugfs_create_file("all-banks", S_IRUGO,
1613 ab8500_dir, &plf->dev, &ab8500_all_banks_fops);
1617 file = debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR),
1618 ab8500_dir, &plf->dev, &ab8500_bank_fops);
1622 file = debugfs_create_file("register-address", (S_IRUGO | S_IWUSR),
1623 ab8500_dir, &plf->dev, &ab8500_address_fops);
1627 file = debugfs_create_file("register-value", (S_IRUGO | S_IWUSR),
1628 ab8500_dir, &plf->dev, &ab8500_val_fops);
1632 file = debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR),
1633 ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
1637 if (is_ab8500(ab8500))
1638 num_interrupt_lines = AB8500_NR_IRQS;
1639 else if (is_ab8505(ab8500))
1640 num_interrupt_lines = AB8505_NR_IRQS;
1641 else if (is_ab9540(ab8500))
1642 num_interrupt_lines = AB9540_NR_IRQS;
1644 file = debugfs_create_file("interrupts", (S_IRUGO),
1645 ab8500_dir, &plf->dev, &ab8500_interrupts_fops);
1649 file = debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR),
1650 ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
1654 file = debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR),
1655 ab8500_dir, &plf->dev, &ab8500_hwreg_fops);
1659 file = debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR),
1660 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bat_ctrl_fops);
1664 file = debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR),
1665 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_btemp_ball_fops);
1669 file = debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR),
1670 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_v_fops);
1674 file = debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR),
1675 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect1_fops);
1679 file = debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR),
1680 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect2_fops);
1684 file = debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR),
1685 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux1_fops);
1689 file = debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR),
1690 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux2_fops);
1694 file = debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR),
1695 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_bat_v_fops);
1699 file = debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR),
1700 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_vbus_v_fops);
1704 file = debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR),
1705 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_c_fops);
1709 file = debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR),
1710 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_charger_c_fops);
1714 file = debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR),
1715 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bk_bat_v_fops);
1719 file = debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR),
1720 ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_die_temp_fops);
1728 debugfs_remove_recursive(ab8500_dir);
1729 dev_err(&plf->dev, "failed to create debugfs entries.\n");
1740 static int ab8500_debug_remove(struct platform_device *plf)
1742 debugfs_remove_recursive(ab8500_dir);
1750 static struct platform_driver ab8500_debug_driver = {
1752 .name = "ab8500-debug",
1753 .owner = THIS_MODULE,
1755 .probe = ab8500_debug_probe,
1756 .remove = ab8500_debug_remove
1759 static int __init ab8500_debug_init(void)
1761 return platform_driver_register(&ab8500_debug_driver);
1764 static void __exit ab8500_debug_exit(void)
1766 platform_driver_unregister(&ab8500_debug_driver);
1768 subsys_initcall(ab8500_debug_init);
1769 module_exit(ab8500_debug_exit);
1771 MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
1772 MODULE_DESCRIPTION("AB8500 DEBUG");
1773 MODULE_LICENSE("GPL v2");