1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
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: irq: number of: wake:\n");
1518 for (line = 0; line < num_interrupt_lines; line++) {
1519 seq_printf(s, "%3i: %4i %6i %4i\n",
1522 num_interrupts[line],
1523 num_wake_interrupts[line]);
1531 DEFINE_SHOW_ATTRIBUTE(ab8500_interrupts);
1534 * - HWREG DB8500 formated routines
1536 static int ab8500_hwreg_print(struct seq_file *s, void *d)
1538 struct device *dev = s->private;
1542 ret = abx500_get_register_interruptible(dev,
1543 (u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, ®value);
1545 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1550 if (hwreg_cfg.shift >= 0)
1551 regvalue >>= hwreg_cfg.shift;
1553 regvalue <<= -hwreg_cfg.shift;
1554 regvalue &= hwreg_cfg.mask;
1556 if (REG_FMT_DEC(&hwreg_cfg))
1557 seq_printf(s, "%d\n", regvalue);
1559 seq_printf(s, "0x%02X\n", regvalue);
1563 static int ab8500_hwreg_open(struct inode *inode, struct file *file)
1565 return single_open(file, ab8500_hwreg_print, inode->i_private);
1568 #define AB8500_SUPPLY_CONTROL_CONFIG_1 0x01
1569 #define AB8500_SUPPLY_CONTROL_REG 0x00
1570 #define AB8500_FIRST_SIM_REG 0x80
1571 #define AB8500_LAST_SIM_REG 0x8B
1572 #define AB8505_LAST_SIM_REG 0x8C
1574 static int ab8500_modem_show(struct seq_file *s, void *p)
1576 struct device *dev = s->private;
1577 struct ab8500 *ab8500;
1581 u32 bank = AB8500_REGU_CTRL2;
1582 u32 last_sim_reg = AB8500_LAST_SIM_REG;
1585 ab8500 = dev_get_drvdata(dev->parent);
1586 dev_warn(dev, "WARNING! This operation can interfer with modem side\n"
1587 "and should only be done with care\n");
1589 err = abx500_get_register_interruptible(dev,
1590 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, &orig_value);
1592 goto report_read_failure;
1594 /* Config 1 will allow APE side to read SIM registers */
1595 err = abx500_set_register_interruptible(dev,
1596 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG,
1597 AB8500_SUPPLY_CONTROL_CONFIG_1);
1599 goto report_write_failure;
1601 seq_printf(s, " bank 0x%02X:\n", bank);
1603 if (is_ab9540(ab8500) || is_ab8505(ab8500))
1604 last_sim_reg = AB8505_LAST_SIM_REG;
1606 for (reg = AB8500_FIRST_SIM_REG; reg <= last_sim_reg; reg++) {
1607 err = abx500_get_register_interruptible(dev,
1610 goto report_read_failure;
1612 seq_printf(s, " [0x%02X/0x%02X]: 0x%02X\n", bank, reg, value);
1614 err = abx500_set_register_interruptible(dev,
1615 AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, orig_value);
1617 goto report_write_failure;
1621 report_read_failure:
1622 dev_err(dev, "ab->read fail %d\n", err);
1625 report_write_failure:
1626 dev_err(dev, "ab->write fail %d\n", err);
1630 DEFINE_SHOW_ATTRIBUTE(ab8500_modem);
1633 * return length of an ASCII numerical value, 0 is string is not a
1635 * string shall start at value 1st char.
1636 * string can be tailed with \0 or space or newline chars only.
1637 * value can be decimal or hexadecimal (prefixed 0x or 0X).
1639 static int strval_len(char *b)
1643 if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) {
1645 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1652 for (; *s && (*s != ' ') && (*s != '\n'); s++) {
1661 * parse hwreg input data.
1662 * update global hwreg_cfg only if input data syntax is ok.
1664 static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg,
1667 uint write, val = 0;
1670 struct hwreg_cfg loc = {
1671 .bank = 0, /* default: invalid phys addr */
1672 .addr = 0, /* default: invalid phys addr */
1673 .fmt = 0, /* default: 32bit access, hex output */
1674 .mask = 0xFFFFFFFF, /* default: no mask */
1675 .shift = 0, /* default: no bit shift */
1678 /* read or write ? */
1679 if (!strncmp(b, "read ", 5)) {
1682 } else if (!strncmp(b, "write ", 6)) {
1688 /* OPTIONS -l|-w|-b -s -m -o */
1689 while ((*b == ' ') || (*b == '-')) {
1690 if (*(b-1) != ' ') {
1694 if ((!strncmp(b, "-d ", 3)) ||
1695 (!strncmp(b, "-dec ", 5))) {
1696 b += (*(b+2) == ' ') ? 3 : 5;
1698 } else if ((!strncmp(b, "-h ", 3)) ||
1699 (!strncmp(b, "-hex ", 5))) {
1700 b += (*(b+2) == ' ') ? 3 : 5;
1702 } else if ((!strncmp(b, "-m ", 3)) ||
1703 (!strncmp(b, "-mask ", 6))) {
1704 b += (*(b+2) == ' ') ? 3 : 6;
1705 if (strval_len(b) == 0)
1707 ret = kstrtoul(b, 0, &loc.mask);
1710 } else if ((!strncmp(b, "-s ", 3)) ||
1711 (!strncmp(b, "-shift ", 7))) {
1712 b += (*(b+2) == ' ') ? 3 : 7;
1713 if (strval_len(b) == 0)
1715 ret = kstrtol(b, 0, &loc.shift);
1722 /* get arg BANK and ADDRESS */
1723 if (strval_len(b) == 0)
1725 ret = kstrtouint(b, 0, &loc.bank);
1730 if (strval_len(b) == 0)
1732 ret = kstrtoul(b, 0, &loc.addr);
1739 if (strval_len(b) == 0)
1741 ret = kstrtouint(b, 0, &val);
1746 /* args are ok, update target cfg (mainly for read) */
1749 #ifdef ABB_HWREG_DEBUG
1750 pr_warn("HWREG request: %s, %s,\n", (write) ? "write" : "read",
1751 REG_FMT_DEC(cfg) ? "decimal" : "hexa");
1752 pr_warn(" addr=0x%08X, mask=0x%X, shift=%d" "value=0x%X\n",
1753 cfg->addr, cfg->mask, cfg->shift, val);
1759 ret = abx500_get_register_interruptible(dev,
1760 (u8)cfg->bank, (u8)cfg->addr, ®value);
1762 dev_err(dev, "abx500_get_reg fail %d, %d\n",
1767 if (cfg->shift >= 0) {
1768 regvalue &= ~(cfg->mask << (cfg->shift));
1769 val = (val & cfg->mask) << (cfg->shift);
1771 regvalue &= ~(cfg->mask >> (-cfg->shift));
1772 val = (val & cfg->mask) >> (-cfg->shift);
1774 val = val | regvalue;
1776 ret = abx500_set_register_interruptible(dev,
1777 (u8)cfg->bank, (u8)cfg->addr, (u8)val);
1779 pr_err("abx500_set_reg failed %d, %d", ret, __LINE__);
1786 static ssize_t ab8500_hwreg_write(struct file *file,
1787 const char __user *user_buf, size_t count, loff_t *ppos)
1789 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1793 /* Get userspace string and assure termination */
1794 buf_size = min((int)count, (int)(sizeof(buf)-1));
1795 if (copy_from_user(buf, user_buf, buf_size))
1799 /* get args and process */
1800 ret = hwreg_common_write(buf, &hwreg_cfg, dev);
1801 return (ret) ? ret : buf_size;
1805 * - irq subscribe/unsubscribe stuff
1807 static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
1809 seq_printf(s, "%d\n", irq_first);
1814 static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
1817 return single_open(file, ab8500_subscribe_unsubscribe_print,
1822 * Userspace should use poll() on this file. When an event occur
1823 * the blocking poll will be released.
1825 static ssize_t show_irq(struct device *dev,
1826 struct device_attribute *attr, char *buf)
1829 unsigned int irq_index;
1832 err = kstrtoul(attr->attr.name, 0, &name);
1836 irq_index = name - irq_first;
1837 if (irq_index >= num_irqs)
1840 return sprintf(buf, "%u\n", irq_count[irq_index]);
1843 static ssize_t ab8500_subscribe_write(struct file *file,
1844 const char __user *user_buf,
1845 size_t count, loff_t *ppos)
1847 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1848 unsigned long user_val;
1850 unsigned int irq_index;
1852 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1856 if (user_val < irq_first) {
1857 dev_err(dev, "debugfs error input < %d\n", irq_first);
1860 if (user_val > irq_last) {
1861 dev_err(dev, "debugfs error input > %d\n", irq_last);
1865 irq_index = user_val - irq_first;
1866 if (irq_index >= num_irqs)
1870 * This will create a sysfs file named <irq-nr> which userspace can
1871 * use to select or poll and get the AB8500 events
1873 dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
1875 if (!dev_attr[irq_index])
1878 event_name[irq_index] = kasprintf(GFP_KERNEL, "%lu", user_val);
1879 if (!event_name[irq_index])
1882 dev_attr[irq_index]->show = show_irq;
1883 dev_attr[irq_index]->store = NULL;
1884 dev_attr[irq_index]->attr.name = event_name[irq_index];
1885 dev_attr[irq_index]->attr.mode = S_IRUGO;
1886 err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
1888 pr_info("sysfs_create_file failed %d\n", err);
1892 err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
1893 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
1894 "ab8500-debug", &dev->kobj);
1896 pr_info("request_threaded_irq failed %d, %lu\n",
1898 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1905 static ssize_t ab8500_unsubscribe_write(struct file *file,
1906 const char __user *user_buf,
1907 size_t count, loff_t *ppos)
1909 struct device *dev = ((struct seq_file *)(file->private_data))->private;
1910 unsigned long user_val;
1912 unsigned int irq_index;
1914 err = kstrtoul_from_user(user_buf, count, 0, &user_val);
1918 if (user_val < irq_first) {
1919 dev_err(dev, "debugfs error input < %d\n", irq_first);
1922 if (user_val > irq_last) {
1923 dev_err(dev, "debugfs error input > %d\n", irq_last);
1927 irq_index = user_val - irq_first;
1928 if (irq_index >= num_irqs)
1931 /* Set irq count to 0 when unsubscribe */
1932 irq_count[irq_index] = 0;
1934 if (dev_attr[irq_index])
1935 sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
1938 free_irq(user_val, &dev->kobj);
1939 kfree(event_name[irq_index]);
1940 kfree(dev_attr[irq_index]);
1946 * - several debugfs nodes fops
1949 static const struct file_operations ab8500_bank_fops = {
1950 .open = ab8500_bank_open,
1951 .write = ab8500_bank_write,
1953 .llseek = seq_lseek,
1954 .release = single_release,
1955 .owner = THIS_MODULE,
1958 static const struct file_operations ab8500_address_fops = {
1959 .open = ab8500_address_open,
1960 .write = ab8500_address_write,
1962 .llseek = seq_lseek,
1963 .release = single_release,
1964 .owner = THIS_MODULE,
1967 static const struct file_operations ab8500_val_fops = {
1968 .open = ab8500_val_open,
1969 .write = ab8500_val_write,
1971 .llseek = seq_lseek,
1972 .release = single_release,
1973 .owner = THIS_MODULE,
1976 static const struct file_operations ab8500_subscribe_fops = {
1977 .open = ab8500_subscribe_unsubscribe_open,
1978 .write = ab8500_subscribe_write,
1980 .llseek = seq_lseek,
1981 .release = single_release,
1982 .owner = THIS_MODULE,
1985 static const struct file_operations ab8500_unsubscribe_fops = {
1986 .open = ab8500_subscribe_unsubscribe_open,
1987 .write = ab8500_unsubscribe_write,
1989 .llseek = seq_lseek,
1990 .release = single_release,
1991 .owner = THIS_MODULE,
1994 static const struct file_operations ab8500_hwreg_fops = {
1995 .open = ab8500_hwreg_open,
1996 .write = ab8500_hwreg_write,
1998 .llseek = seq_lseek,
1999 .release = single_release,
2000 .owner = THIS_MODULE,
2003 static int ab8500_debug_probe(struct platform_device *plf)
2005 struct dentry *ab8500_dir;
2006 struct ab8500 *ab8500;
2007 struct resource *res;
2009 debug_bank = AB8500_MISC;
2010 debug_address = AB8500_REV_REG & 0x00FF;
2012 ab8500 = dev_get_drvdata(plf->dev.parent);
2013 num_irqs = ab8500->mask_size;
2015 irq_count = devm_kcalloc(&plf->dev,
2016 num_irqs, sizeof(*irq_count), GFP_KERNEL);
2020 dev_attr = devm_kcalloc(&plf->dev,
2021 num_irqs, sizeof(*dev_attr), GFP_KERNEL);
2025 event_name = devm_kcalloc(&plf->dev,
2026 num_irqs, sizeof(*event_name), GFP_KERNEL);
2030 res = platform_get_resource_byname(plf, 0, "IRQ_AB8500");
2032 dev_err(&plf->dev, "AB8500 irq not found, err %d\n", irq_first);
2035 irq_ab8500 = res->start;
2037 irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
2041 irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
2045 ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
2047 debugfs_create_file("all-bank-registers", S_IRUGO, ab8500_dir,
2048 &plf->dev, &ab8500_bank_registers_fops);
2049 debugfs_create_file("all-banks", S_IRUGO, ab8500_dir,
2050 &plf->dev, &ab8500_all_banks_fops);
2051 debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR | S_IWGRP),
2052 ab8500_dir, &plf->dev, &ab8500_bank_fops);
2053 debugfs_create_file("register-address", (S_IRUGO | S_IWUSR | S_IWGRP),
2054 ab8500_dir, &plf->dev, &ab8500_address_fops);
2055 debugfs_create_file("register-value", (S_IRUGO | S_IWUSR | S_IWGRP),
2056 ab8500_dir, &plf->dev, &ab8500_val_fops);
2057 debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
2058 ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
2060 if (is_ab8500(ab8500)) {
2061 debug_ranges = ab8500_debug_ranges;
2062 num_interrupt_lines = AB8500_NR_IRQS;
2063 } else if (is_ab8505(ab8500)) {
2064 debug_ranges = ab8505_debug_ranges;
2065 num_interrupt_lines = AB8505_NR_IRQS;
2066 } else if (is_ab9540(ab8500)) {
2067 debug_ranges = ab8505_debug_ranges;
2068 num_interrupt_lines = AB9540_NR_IRQS;
2069 } else if (is_ab8540(ab8500)) {
2070 debug_ranges = ab8540_debug_ranges;
2071 num_interrupt_lines = AB8540_NR_IRQS;
2074 debugfs_create_file("interrupts", (S_IRUGO), ab8500_dir, &plf->dev,
2075 &ab8500_interrupts_fops);
2076 debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR | S_IWGRP),
2077 ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
2078 debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR | S_IWGRP), ab8500_dir,
2079 &plf->dev, &ab8500_hwreg_fops);
2080 debugfs_create_file("all-modem-registers", (S_IRUGO | S_IWUSR | S_IWGRP),
2081 ab8500_dir, &plf->dev, &ab8500_modem_fops);
2086 static struct platform_driver ab8500_debug_driver = {
2088 .name = "ab8500-debug",
2089 .suppress_bind_attrs = true,
2091 .probe = ab8500_debug_probe,
2094 static int __init ab8500_debug_init(void)
2096 return platform_driver_register(&ab8500_debug_driver);
2098 subsys_initcall(ab8500_debug_init);