1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
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/init.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>
83 #include <linux/irq.h>
85 #include <linux/mfd/abx500.h>
86 #include <linux/mfd/abx500/ab8500.h>
88 #ifdef CONFIG_DEBUG_FS
89 #include <linux/string.h>
90 #include <linux/ctype.h>
93 static u32 debug_bank;
94 static u32 debug_address;
96 static int irq_ab8500;
99 static u32 *irq_count;
102 static struct device_attribute **dev_attr;
103 static char **event_name;
106 * struct ab8500_reg_range
107 * @first: the first address of the range
108 * @last: the last address of the range
109 * @perm: access permissions for the range
111 struct ab8500_reg_range {
118 * struct ab8500_prcmu_ranges
119 * @num_ranges: the number of ranges in the list
120 * @bankid: bank identifier
121 * @range: the list of register ranges
123 struct ab8500_prcmu_ranges {
126 const struct ab8500_reg_range *range;
129 /* hwreg- "mask" and "shift" entries ressources */
131 u32 bank; /* target bank */
132 unsigned long addr; /* target address */
133 uint fmt; /* format */
134 unsigned long mask; /* read/write mask, applied before any bit shift */
135 long shift; /* bit shift (read:right shift, write:left shift */
137 /* fmt bit #0: 0=hexa, 1=dec */
138 #define REG_FMT_DEC(c) ((c)->fmt & 0x1)
139 #define REG_FMT_HEX(c) (!REG_FMT_DEC(c))
141 static struct hwreg_cfg hwreg_cfg = {
142 .addr = 0, /* default: invalid phys addr */
143 .fmt = 0, /* default: 32bit access, hex output */
144 .mask = 0xFFFFFFFF, /* default: no mask */
145 .shift = 0, /* default: no bit shift */
148 #define AB8500_NAME_STRING "ab8500"
149 #define AB8500_NUM_BANKS AB8500_DEBUG_FIELD_LAST
151 #define AB8500_REV_REG 0x80
153 static struct ab8500_prcmu_ranges *debug_ranges;
155 static struct ab8500_prcmu_ranges ab8500_debug_ranges[AB8500_NUM_BANKS] = {
156 [AB8500_M_FSM_RANK] = {
160 [AB8500_SYS_CTRL1_BLOCK] = {
162 .range = (struct ab8500_reg_range[]) {
177 [AB8500_SYS_CTRL2_BLOCK] = {
179 .range = (struct ab8500_reg_range[]) {
198 [AB8500_REGU_CTRL1] = {
200 .range = (struct ab8500_reg_range[]) {
215 [AB8500_REGU_CTRL2] = {
217 .range = (struct ab8500_reg_range[]) {
239 * 0x80-0x8B are SIM registers and should
240 * not be accessed from here
246 .range = (struct ab8500_reg_range[]) {
259 .range = (struct ab8500_reg_range[]) {
302 [AB8500_ECI_AV_ACC] = {
304 .range = (struct ab8500_reg_range[]) {
311 [AB8500_RESERVED] = {
317 .range = (struct ab8500_reg_range[]) {
326 .range = (struct ab8500_reg_range[]) {
365 [AB8500_GAS_GAUGE] = {
367 .range = (struct ab8500_reg_range[]) {
384 .range = (struct ab8500_reg_range[]) {
391 [AB8500_INTERRUPT] = {
397 .range = (struct ab8500_reg_range[]) {
406 .range = (struct ab8500_reg_range[]) {
441 [AB8500_DEVELOPMENT] = {
443 .range = (struct ab8500_reg_range[]) {
452 .range = (struct ab8500_reg_range[]) {
459 [AB8500_PROD_TEST] = {
463 [AB8500_STE_TEST] = {
467 [AB8500_OTP_EMUL] = {
469 .range = (struct ab8500_reg_range[]) {
478 static struct ab8500_prcmu_ranges ab8505_debug_ranges[AB8500_NUM_BANKS] = {
483 [AB8500_SYS_CTRL1_BLOCK] = {
485 .range = (struct ab8500_reg_range[]) {
508 [AB8500_SYS_CTRL2_BLOCK] = {
510 .range = (struct ab8500_reg_range[]) {
533 [AB8500_REGU_CTRL1] = {
535 .range = (struct ab8500_reg_range[]) {
550 [AB8500_REGU_CTRL2] = {
552 .range = (struct ab8500_reg_range[]) {
578 * 0x80-0x8B are SIM registers and should
579 * not be accessed from here
585 .range = (struct ab8500_reg_range[]) {
608 [AB8500_ECI_AV_ACC] = {
610 .range = (struct ab8500_reg_range[]) {
617 [AB8500_RESERVED] = {
623 .range = (struct ab8500_reg_range[]) {
632 .range = (struct ab8500_reg_range[]) {
671 [AB8500_GAS_GAUGE] = {
673 .range = (struct ab8500_reg_range[]) {
690 .range = (struct ab8500_reg_range[]) {
697 [AB8500_INTERRUPT] = {
699 .range = (struct ab8500_reg_range[]) {
724 /* Latch registers should not be read here */
745 /* LatchHier registers should not be read here */
750 .range = (struct ab8500_reg_range[]) {
763 .range = (struct ab8500_reg_range[]) {
798 [AB8500_DEVELOPMENT] = {
800 .range = (struct ab8500_reg_range[]) {
813 .range = (struct ab8500_reg_range[]) {
820 [AB8500_PROD_TEST] = {
824 [AB8500_STE_TEST] = {
828 [AB8500_OTP_EMUL] = {
830 .range = (struct ab8500_reg_range[]) {
839 static struct ab8500_prcmu_ranges ab8540_debug_ranges[AB8500_NUM_BANKS] = {
840 [AB8500_M_FSM_RANK] = {
842 .range = (struct ab8500_reg_range[]) {
849 [AB8500_SYS_CTRL1_BLOCK] = {
851 .range = (struct ab8500_reg_range[]) {
878 [AB8500_SYS_CTRL2_BLOCK] = {
880 .range = (struct ab8500_reg_range[]) {
903 [AB8500_REGU_CTRL1] = {
905 .range = (struct ab8500_reg_range[]) {
924 [AB8500_REGU_CTRL2] = {
926 .range = (struct ab8500_reg_range[]) {
963 .range = (struct ab8500_reg_range[]) {
984 .range = (struct ab8500_reg_range[]) {
1003 [AB8500_ECI_AV_ACC] = {
1005 .range = (struct ab8500_reg_range[]) {
1016 [AB8500_RESERVED] = {
1022 .range = (struct ab8500_reg_range[]) {
1041 [AB8500_CHARGER] = {
1043 .range = (struct ab8500_reg_range[]) {
1086 [AB8500_GAS_GAUGE] = {
1088 .range = (struct ab8500_reg_range[]) {
1105 .range = (struct ab8500_reg_range[]) {
1112 [AB8500_INTERRUPT] = {
1114 .range = (struct ab8500_reg_range[]) {
1127 /* Latch registers should not be read here */
1140 /* LatchHier registers should not be read here */
1145 .range = (struct ab8500_reg_range[]) {
1162 .range = (struct ab8500_reg_range[]) {
1201 [AB8500_DEVELOPMENT] = {
1203 .range = (struct ab8500_reg_range[]) {
1220 .range = (struct ab8500_reg_range[]) {
1235 [AB8500_PROD_TEST] = {
1239 [AB8500_STE_TEST] = {
1243 [AB8500_OTP_EMUL] = {
1245 .range = (struct ab8500_reg_range[]) {
1254 static irqreturn_t ab8500_debug_handler(int irq, void *data)
1257 struct kobject *kobj = (struct kobject *)data;
1258 unsigned int irq_abb = irq - irq_first;
1260 if (irq_abb < num_irqs)
1261 irq_count[irq_abb]++;
1263 * This makes it possible to use poll for events (EPOLLPRI | EPOLLERR)
1264 * from userspace on sysfs file named <irq-nr>
1266 sprintf(buf, "%d", irq);
1267 sysfs_notify(kobj, NULL, buf);
1272 /* Prints to seq_file or log_buf */
1273 static int ab8500_registers_print(struct device *dev, u32 bank,
1278 for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
1281 for (reg = debug_ranges[bank].range[i].first;
1282 reg <= debug_ranges[bank].range[i].last;
1287 err = abx500_get_register_interruptible(dev,
1288 (u8)bank, (u8)reg, &value);
1290 dev_err(dev, "ab->read fail %d\n", err);
1295 seq_printf(s, " [0x%02X/0x%02X]: 0x%02X\n",
1298 * Error is not returned here since
1299 * the output is wanted in any case
1301 if (seq_has_overflowed(s))
1304 dev_info(dev, " [0x%02X/0x%02X]: 0x%02X\n",
1313 static int ab8500_bank_registers_show(struct seq_file *s, void *p)
1315 struct device *dev = s->private;
1316 u32 bank = debug_bank;
1318 seq_puts(s, AB8500_NAME_STRING " register values:\n");
1320 seq_printf(s, " bank 0x%02X:\n", bank);
1322 return ab8500_registers_print(dev, bank, s);
1325 DEFINE_SHOW_ATTRIBUTE(ab8500_bank_registers);
1327 static int ab8500_print_all_banks(struct seq_file *s, void *p)
1329 struct device *dev = s->private;
1332 seq_puts(s, AB8500_NAME_STRING " register values:\n");
1334 for (i = 0; i < AB8500_NUM_BANKS; i++) {
1337 seq_printf(s, " bank 0x%02X:\n", i);
1338 err = ab8500_registers_print(dev, i, s);
1345 /* Dump registers to kernel log */
1346 void ab8500_dump_all_banks(struct device *dev)
1350 dev_info(dev, "ab8500 register values:\n");
1352 for (i = 1; i < AB8500_NUM_BANKS; i++) {
1353 dev_info(dev, " bank 0x%02X:\n", i);
1354 ab8500_registers_print(dev, i, NULL);
1358 static int ab8500_all_banks_open(struct inode *inode, struct file *file)
1363 err = single_open(file, ab8500_print_all_banks, inode->i_private);
1365 /* Default buf size in seq_read is not enough */
1366 s = (struct seq_file *)file->private_data;
1367 s->size = (PAGE_SIZE * 2);
1368 s->buf = kmalloc(s->size, GFP_KERNEL);
1370 single_release(inode, file);
1377 static const struct file_operations ab8500_all_banks_fops = {
1378 .open = ab8500_all_banks_open,
1380 .llseek = seq_lseek,
1381 .release = single_release,
1382 .owner = THIS_MODULE,
1385 static int ab8500_bank_print(struct seq_file *s, void *p)
1387 seq_printf(s, "0x%02X\n", debug_bank);
1391 static int ab8500_bank_open(struct inode *inode, struct file *file)
1393 return single_open(file, ab8500_bank_print, inode->i_private);
1396 static ssize_t ab8500_bank_write(struct file *file,
1397 const char __user *user_buf,
1398 size_t count, loff_t *ppos)
1400 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1401 unsigned long user_bank;
1404 err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
1408 if (user_bank >= AB8500_NUM_BANKS) {
1409 dev_err(dev, "debugfs error input > number of banks\n");
1413 debug_bank = user_bank;
1418 static int ab8500_address_print(struct seq_file *s, void *p)
1420 seq_printf(s, "0x%02X\n", debug_address);
1424 static int ab8500_address_open(struct inode *inode, struct file *file)
1426 return single_open(file, ab8500_address_print, inode->i_private);
1429 static ssize_t ab8500_address_write(struct file *file,
1430 const char __user *user_buf,
1431 size_t count, loff_t *ppos)
1433 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1434 unsigned long user_address;
1437 err = kstrtoul_from_user(user_buf, count, 0, &user_address);
1441 if (user_address > 0xff) {
1442 dev_err(dev, "debugfs error input > 0xff\n");
1445 debug_address = user_address;
1450 static int ab8500_val_print(struct seq_file *s, void *p)
1452 struct device *dev = s->private;
1456 ret = abx500_get_register_interruptible(dev,
1457 (u8)debug_bank, (u8)debug_address, ®value);
1459 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1463 seq_printf(s, "0x%02X\n", regvalue);
1468 static int ab8500_val_open(struct inode *inode, struct file *file)
1470 return single_open(file, ab8500_val_print, inode->i_private);
1473 static ssize_t ab8500_val_write(struct file *file,
1474 const char __user *user_buf,
1475 size_t count, loff_t *ppos)
1477 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1478 unsigned long user_val;
1481 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1485 if (user_val > 0xff) {
1486 dev_err(dev, "debugfs error input > 0xff\n");
1489 err = abx500_set_register_interruptible(dev,
1490 (u8)debug_bank, debug_address, (u8)user_val);
1492 pr_err("abx500_set_reg failed %d, %d", err, __LINE__);
1502 static u32 num_interrupts[AB8500_MAX_NR_IRQS];
1503 static u32 num_wake_interrupts[AB8500_MAX_NR_IRQS];
1504 static int num_interrupt_lines;
1506 void ab8500_debug_register_interrupt(int line)
1508 if (line < num_interrupt_lines)
1509 num_interrupts[line]++;
1512 static int ab8500_interrupts_show(struct seq_file *s, void *p)
1516 seq_puts(s, "name: number: number of: wake:\n");
1518 for (line = 0; line < num_interrupt_lines; line++) {
1519 struct irq_desc *desc = irq_to_desc(line + irq_first);
1521 seq_printf(s, "%3i: %6i %4i",
1523 num_interrupts[line],
1524 num_wake_interrupts[line]);
1526 if (desc && desc->name)
1527 seq_printf(s, "-%-8s", desc->name);
1528 if (desc && desc->action) {
1529 struct irqaction *action = desc->action;
1531 seq_printf(s, " %s", action->name);
1532 while ((action = action->next) != NULL)
1533 seq_printf(s, ", %s", action->name);
1541 DEFINE_SHOW_ATTRIBUTE(ab8500_interrupts);
1544 * - HWREG DB8500 formated routines
1546 static int ab8500_hwreg_print(struct seq_file *s, void *d)
1548 struct device *dev = s->private;
1552 ret = abx500_get_register_interruptible(dev,
1553 (u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, ®value);
1555 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1560 if (hwreg_cfg.shift >= 0)
1561 regvalue >>= hwreg_cfg.shift;
1563 regvalue <<= -hwreg_cfg.shift;
1564 regvalue &= hwreg_cfg.mask;
1566 if (REG_FMT_DEC(&hwreg_cfg))
1567 seq_printf(s, "%d\n", regvalue);
1569 seq_printf(s, "0x%02X\n", regvalue);
1573 static int ab8500_hwreg_open(struct inode *inode, struct file *file)
1575 return single_open(file, ab8500_hwreg_print, inode->i_private);
1578 #define AB8500_SUPPLY_CONTROL_CONFIG_1 0x01
1579 #define AB8500_SUPPLY_CONTROL_REG 0x00
1580 #define AB8500_FIRST_SIM_REG 0x80
1581 #define AB8500_LAST_SIM_REG 0x8B
1582 #define AB8505_LAST_SIM_REG 0x8C
1584 static int ab8500_modem_show(struct seq_file *s, void *p)
1586 struct device *dev = s->private;
1587 struct ab8500 *ab8500;
1591 u32 bank = AB8500_REGU_CTRL2;
1592 u32 last_sim_reg = AB8500_LAST_SIM_REG;
1595 ab8500 = dev_get_drvdata(dev->parent);
1596 dev_warn(dev, "WARNING! This operation can interfer with modem side\n"
1597 "and should only be done with care\n");
1599 err = abx500_get_register_interruptible(dev,
1600 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, &orig_value);
1602 goto report_read_failure;
1604 /* Config 1 will allow APE side to read SIM registers */
1605 err = abx500_set_register_interruptible(dev,
1606 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG,
1607 AB8500_SUPPLY_CONTROL_CONFIG_1);
1609 goto report_write_failure;
1611 seq_printf(s, " bank 0x%02X:\n", bank);
1613 if (is_ab9540(ab8500) || is_ab8505(ab8500))
1614 last_sim_reg = AB8505_LAST_SIM_REG;
1616 for (reg = AB8500_FIRST_SIM_REG; reg <= last_sim_reg; reg++) {
1617 err = abx500_get_register_interruptible(dev,
1620 goto report_read_failure;
1622 seq_printf(s, " [0x%02X/0x%02X]: 0x%02X\n", bank, reg, value);
1624 err = abx500_set_register_interruptible(dev,
1625 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, orig_value);
1627 goto report_write_failure;
1631 report_read_failure:
1632 dev_err(dev, "ab->read fail %d\n", err);
1635 report_write_failure:
1636 dev_err(dev, "ab->write fail %d\n", err);
1640 DEFINE_SHOW_ATTRIBUTE(ab8500_modem);
1643 * return length of an ASCII numerical value, 0 is string is not a
1645 * string shall start at value 1st char.
1646 * string can be tailed with \0 or space or newline chars only.
1647 * value can be decimal or hexadecimal (prefixed 0x or 0X).
1649 static int strval_len(char *b)
1653 if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) {
1655 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1662 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1671 * parse hwreg input data.
1672 * update global hwreg_cfg only if input data syntax is ok.
1674 static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg,
1677 uint write, val = 0;
1680 struct hwreg_cfg loc = {
1681 .bank = 0, /* default: invalid phys addr */
1682 .addr = 0, /* default: invalid phys addr */
1683 .fmt = 0, /* default: 32bit access, hex output */
1684 .mask = 0xFFFFFFFF, /* default: no mask */
1685 .shift = 0, /* default: no bit shift */
1688 /* read or write ? */
1689 if (!strncmp(b, "read ", 5)) {
1692 } else if (!strncmp(b, "write ", 6)) {
1698 /* OPTIONS -l|-w|-b -s -m -o */
1699 while ((*b == ' ') || (*b == '-')) {
1700 if (*(b-1) != ' ') {
1704 if ((!strncmp(b, "-d ", 3)) ||
1705 (!strncmp(b, "-dec ", 5))) {
1706 b += (*(b+2) == ' ') ? 3 : 5;
1708 } else if ((!strncmp(b, "-h ", 3)) ||
1709 (!strncmp(b, "-hex ", 5))) {
1710 b += (*(b+2) == ' ') ? 3 : 5;
1712 } else if ((!strncmp(b, "-m ", 3)) ||
1713 (!strncmp(b, "-mask ", 6))) {
1714 b += (*(b+2) == ' ') ? 3 : 6;
1715 if (strval_len(b) == 0)
1717 ret = kstrtoul(b, 0, &loc.mask);
1720 } else if ((!strncmp(b, "-s ", 3)) ||
1721 (!strncmp(b, "-shift ", 7))) {
1722 b += (*(b+2) == ' ') ? 3 : 7;
1723 if (strval_len(b) == 0)
1725 ret = kstrtol(b, 0, &loc.shift);
1732 /* get arg BANK and ADDRESS */
1733 if (strval_len(b) == 0)
1735 ret = kstrtouint(b, 0, &loc.bank);
1740 if (strval_len(b) == 0)
1742 ret = kstrtoul(b, 0, &loc.addr);
1749 if (strval_len(b) == 0)
1751 ret = kstrtouint(b, 0, &val);
1756 /* args are ok, update target cfg (mainly for read) */
1759 #ifdef ABB_HWREG_DEBUG
1760 pr_warn("HWREG request: %s, %s,\n", (write) ? "write" : "read",
1761 REG_FMT_DEC(cfg) ? "decimal" : "hexa");
1762 pr_warn(" addr=0x%08X, mask=0x%X, shift=%d" "value=0x%X\n",
1763 cfg->addr, cfg->mask, cfg->shift, val);
1769 ret = abx500_get_register_interruptible(dev,
1770 (u8)cfg->bank, (u8)cfg->addr, ®value);
1772 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1777 if (cfg->shift >= 0) {
1778 regvalue &= ~(cfg->mask << (cfg->shift));
1779 val = (val & cfg->mask) << (cfg->shift);
1781 regvalue &= ~(cfg->mask >> (-cfg->shift));
1782 val = (val & cfg->mask) >> (-cfg->shift);
1784 val = val | regvalue;
1786 ret = abx500_set_register_interruptible(dev,
1787 (u8)cfg->bank, (u8)cfg->addr, (u8)val);
1789 pr_err("abx500_set_reg failed %d, %d", ret, __LINE__);
1796 static ssize_t ab8500_hwreg_write(struct file *file,
1797 const char __user *user_buf, size_t count, loff_t *ppos)
1799 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1803 /* Get userspace string and assure termination */
1804 buf_size = min(count, (sizeof(buf)-1));
1805 if (copy_from_user(buf, user_buf, buf_size))
1809 /* get args and process */
1810 ret = hwreg_common_write(buf, &hwreg_cfg, dev);
1811 return (ret) ? ret : buf_size;
1815 * - irq subscribe/unsubscribe stuff
1817 static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
1819 seq_printf(s, "%d\n", irq_first);
1824 static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
1827 return single_open(file, ab8500_subscribe_unsubscribe_print,
1832 * Userspace should use poll() on this file. When an event occur
1833 * the blocking poll will be released.
1835 static ssize_t show_irq(struct device *dev,
1836 struct device_attribute *attr, char *buf)
1839 unsigned int irq_index;
1842 err = kstrtoul(attr->attr.name, 0, &name);
1846 irq_index = name - irq_first;
1847 if (irq_index >= num_irqs)
1850 return sprintf(buf, "%u\n", irq_count[irq_index]);
1853 static ssize_t ab8500_subscribe_write(struct file *file,
1854 const char __user *user_buf,
1855 size_t count, loff_t *ppos)
1857 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1858 unsigned long user_val;
1860 unsigned int irq_index;
1862 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1866 if (user_val < irq_first) {
1867 dev_err(dev, "debugfs error input < %d\n", irq_first);
1870 if (user_val > irq_last) {
1871 dev_err(dev, "debugfs error input > %d\n", irq_last);
1875 irq_index = user_val - irq_first;
1876 if (irq_index >= num_irqs)
1880 * This will create a sysfs file named <irq-nr> which userspace can
1881 * use to select or poll and get the AB8500 events
1883 dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
1885 if (!dev_attr[irq_index])
1888 event_name[irq_index] = kasprintf(GFP_KERNEL, "%lu", user_val);
1889 if (!event_name[irq_index])
1892 dev_attr[irq_index]->show = show_irq;
1893 dev_attr[irq_index]->store = NULL;
1894 dev_attr[irq_index]->attr.name = event_name[irq_index];
1895 dev_attr[irq_index]->attr.mode = S_IRUGO;
1896 err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
1898 pr_info("sysfs_create_file failed %d\n", err);
1902 err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
1903 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
1904 "ab8500-debug", &dev->kobj);
1906 pr_info("request_threaded_irq failed %d, %lu\n",
1908 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1915 static ssize_t ab8500_unsubscribe_write(struct file *file,
1916 const char __user *user_buf,
1917 size_t count, loff_t *ppos)
1919 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1920 unsigned long user_val;
1922 unsigned int irq_index;
1924 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1928 if (user_val < irq_first) {
1929 dev_err(dev, "debugfs error input < %d\n", irq_first);
1932 if (user_val > irq_last) {
1933 dev_err(dev, "debugfs error input > %d\n", irq_last);
1937 irq_index = user_val - irq_first;
1938 if (irq_index >= num_irqs)
1941 /* Set irq count to 0 when unsubscribe */
1942 irq_count[irq_index] = 0;
1944 if (dev_attr[irq_index])
1945 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1948 free_irq(user_val, &dev->kobj);
1949 kfree(event_name[irq_index]);
1950 kfree(dev_attr[irq_index]);
1956 * - several debugfs nodes fops
1959 static const struct file_operations ab8500_bank_fops = {
1960 .open = ab8500_bank_open,
1961 .write = ab8500_bank_write,
1963 .llseek = seq_lseek,
1964 .release = single_release,
1965 .owner = THIS_MODULE,
1968 static const struct file_operations ab8500_address_fops = {
1969 .open = ab8500_address_open,
1970 .write = ab8500_address_write,
1972 .llseek = seq_lseek,
1973 .release = single_release,
1974 .owner = THIS_MODULE,
1977 static const struct file_operations ab8500_val_fops = {
1978 .open = ab8500_val_open,
1979 .write = ab8500_val_write,
1981 .llseek = seq_lseek,
1982 .release = single_release,
1983 .owner = THIS_MODULE,
1986 static const struct file_operations ab8500_subscribe_fops = {
1987 .open = ab8500_subscribe_unsubscribe_open,
1988 .write = ab8500_subscribe_write,
1990 .llseek = seq_lseek,
1991 .release = single_release,
1992 .owner = THIS_MODULE,
1995 static const struct file_operations ab8500_unsubscribe_fops = {
1996 .open = ab8500_subscribe_unsubscribe_open,
1997 .write = ab8500_unsubscribe_write,
1999 .llseek = seq_lseek,
2000 .release = single_release,
2001 .owner = THIS_MODULE,
2004 static const struct file_operations ab8500_hwreg_fops = {
2005 .open = ab8500_hwreg_open,
2006 .write = ab8500_hwreg_write,
2008 .llseek = seq_lseek,
2009 .release = single_release,
2010 .owner = THIS_MODULE,
2013 static int ab8500_debug_probe(struct platform_device *plf)
2015 struct dentry *ab8500_dir;
2016 struct ab8500 *ab8500;
2017 struct resource *res;
2019 debug_bank = AB8500_MISC;
2020 debug_address = AB8500_REV_REG & 0x00FF;
2022 ab8500 = dev_get_drvdata(plf->dev.parent);
2023 num_irqs = ab8500->mask_size;
2025 irq_count = devm_kcalloc(&plf->dev,
2026 num_irqs, sizeof(*irq_count), GFP_KERNEL);
2030 dev_attr = devm_kcalloc(&plf->dev,
2031 num_irqs, sizeof(*dev_attr), GFP_KERNEL);
2035 event_name = devm_kcalloc(&plf->dev,
2036 num_irqs, sizeof(*event_name), GFP_KERNEL);
2040 res = platform_get_resource_byname(plf, 0, "IRQ_AB8500");
2042 dev_err(&plf->dev, "AB8500 irq not found, err %d\n", irq_first);
2045 irq_ab8500 = res->start;
2047 irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
2051 irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
2055 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
2057 debugfs_create_file("all-bank-registers", S_IRUGO, ab8500_dir,
2058 &plf->dev, &ab8500_bank_registers_fops);
2059 debugfs_create_file("all-banks", S_IRUGO, ab8500_dir,
2060 &plf->dev, &ab8500_all_banks_fops);
2061 debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR | S_IWGRP),
2062 ab8500_dir, &plf->dev, &ab8500_bank_fops);
2063 debugfs_create_file("register-address", (S_IRUGO | S_IWUSR | S_IWGRP),
2064 ab8500_dir, &plf->dev, &ab8500_address_fops);
2065 debugfs_create_file("register-value", (S_IRUGO | S_IWUSR | S_IWGRP),
2066 ab8500_dir, &plf->dev, &ab8500_val_fops);
2067 debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
2068 ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
2070 if (is_ab8500(ab8500)) {
2071 debug_ranges = ab8500_debug_ranges;
2072 num_interrupt_lines = AB8500_NR_IRQS;
2073 } else if (is_ab8505(ab8500)) {
2074 debug_ranges = ab8505_debug_ranges;
2075 num_interrupt_lines = AB8505_NR_IRQS;
2076 } else if (is_ab9540(ab8500)) {
2077 debug_ranges = ab8505_debug_ranges;
2078 num_interrupt_lines = AB9540_NR_IRQS;
2079 } else if (is_ab8540(ab8500)) {
2080 debug_ranges = ab8540_debug_ranges;
2081 num_interrupt_lines = AB8540_NR_IRQS;
2084 debugfs_create_file("interrupts", (S_IRUGO), ab8500_dir, &plf->dev,
2085 &ab8500_interrupts_fops);
2086 debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
2087 ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
2088 debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR | S_IWGRP), ab8500_dir,
2089 &plf->dev, &ab8500_hwreg_fops);
2090 debugfs_create_file("all-modem-registers", (S_IRUGO | S_IWUSR | S_IWGRP),
2091 ab8500_dir, &plf->dev, &ab8500_modem_fops);
2096 static struct platform_driver ab8500_debug_driver = {
2098 .name = "ab8500-debug",
2099 .suppress_bind_attrs = true,
2101 .probe = ab8500_debug_probe,
2104 static int __init ab8500_debug_init(void)
2106 return platform_driver_register(&ab8500_debug_driver);
2108 subsys_initcall(ab8500_debug_init);