2 * drivers/mfd/rt5025-debug.c
3 * Driver foo Richtek RT5025 PMIC Debug
5 * Copyright (C) 2013 Richtek Electronics
6 * cy_huang <cy_huang@richtek.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/debugfs.h>
22 #include <linux/string.h>
24 #include <linux/mfd/rt5025.h>
26 struct rt5025_debug_info {
27 struct i2c_client *i2c;
30 static struct i2c_client *client;
31 static struct dentry *debugfs_rt_dent;
32 static struct dentry *debugfs_peek;
33 static struct dentry *debugfs_poke;
34 static struct dentry *debugfs_regs;
35 static struct dentry *debugfs_reset_b;
37 static unsigned char read_data[10];
39 static int reg_debug_open(struct inode *inode, struct file *file)
41 file->private_data = inode->i_private;
45 static int get_parameters(char *buf, long int *param1, int num_of_par)
50 token = strsep(&buf, " ");
52 for (cnt = 0; cnt < num_of_par; cnt++) {
54 if ((token[1] == 'x') || (token[1] == 'X'))
59 if (strict_strtoul(token, base, ¶m1[cnt]) != 0)
62 token = strsep(&buf, " ");
70 #define LOG_FORMAT "0x%02x\n0x%02x\n0x%02x\n0x%02x\n0x%02x\n"
72 static ssize_t reg_debug_read(struct file *filp, char __user *ubuf,
73 size_t count, loff_t *ppos)
75 char *access_str = filp->private_data;
77 if (!strcmp(access_str, "regs"))
79 RTINFO("read regs file\n");
81 snprintf(lbuf, sizeof(lbuf), LOG_FORMAT LOG_FORMAT, read_data[0], \
82 read_data[1], read_data[2], read_data[3], read_data[4], read_data[5], \
83 read_data[6], read_data[7], read_data[8], read_data[9]);
86 snprintf(lbuf, sizeof(lbuf), "0x%02x\n", read_data[0]);
87 return simple_read_from_buffer(ubuf, count, ppos, lbuf, strlen(lbuf));
90 static ssize_t reg_debug_write(struct file *filp,
91 const char __user *ubuf, size_t cnt, loff_t *ppos)
93 char *access_str = filp->private_data;
98 if (cnt > sizeof(lbuf) - 1)
101 rc = copy_from_user(lbuf, ubuf, cnt);
107 if (!strcmp(access_str, "poke")) {
109 rc = get_parameters(lbuf, param, 2);
110 if ((param[0] <= 0xFF) && (param[1] <= 0xFF) && (rc == 0))
112 rt5025_reg_write(client, param[0], (unsigned char)param[1]);
116 } else if (!strcmp(access_str, "peek")) {
118 rc = get_parameters(lbuf, param, 1);
119 if ((param[0] <= 0xFF) && (rc == 0))
121 read_data[0] = rt5025_reg_read(client, param[0]);
125 } else if (!strcmp(access_str, "regs")) {
127 rc = get_parameters(lbuf, param, 1);
128 if ((param[0] <= 0xFF) && (rc == 0))
130 rt5025_reg_block_read(client, param[0], 10, read_data);
131 RTINFO("regs 0 = 0x%02x\n", read_data[0]);
132 RTINFO("regs 1 = 0x%02x\n", read_data[1]);
133 RTINFO("regs 2 = 0x%02x\n", read_data[2]);
134 RTINFO("regs 3 = 0x%02x\n", read_data[3]);
135 RTINFO("regs 4 = 0x%02x\n", read_data[4]);
136 RTINFO("regs 5 = 0x%02x\n", read_data[5]);
137 RTINFO("regs 6 = 0x%02x\n", read_data[6]);
138 RTINFO("regs 7 = 0x%02x\n", read_data[7]);
139 RTINFO("regs 8 = 0x%02x\n", read_data[8]);
140 RTINFO("regs 9 = 0x%02x\n", read_data[9]);
144 } else if (!strcmp(access_str, "reset_b")) {
146 rc = get_parameters(lbuf, param, 1);
147 if (param[0] == 1 && rc == 0)
150 rt5025_reg_block_write(client, 0x21, 15, lbuf);
160 static const struct file_operations reg_debug_ops = {
161 .open = reg_debug_open,
162 .write = reg_debug_write,
163 .read = reg_debug_read
166 static int __devinit rt5025_debug_probe(struct platform_device *pdev)
168 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
169 struct rt5025_debug_info *di;
171 di = kzalloc(sizeof(*di), GFP_KERNEL);
177 RTINFO("add debugfs for core RT5025");
179 debugfs_rt_dent = debugfs_create_dir("rt5025_dbg", 0);
180 if (!IS_ERR(debugfs_rt_dent)) {
181 debugfs_peek = debugfs_create_file("peek",
182 S_IFREG | S_IRUGO, debugfs_rt_dent,
183 (void *) "peek", ®_debug_ops);
185 debugfs_poke = debugfs_create_file("poke",
186 S_IFREG | S_IRUGO, debugfs_rt_dent,
187 (void *) "poke", ®_debug_ops);
189 debugfs_regs = debugfs_create_file("regs",
190 S_IFREG | S_IRUGO, debugfs_rt_dent,
191 (void *) "regs", ®_debug_ops);
193 debugfs_reset_b = debugfs_create_file("reset_b",
194 S_IFREG | S_IRUGO, debugfs_rt_dent,
195 (void *) "reset_b", ®_debug_ops);
198 platform_set_drvdata(pdev, di);
203 static int __devexit rt5025_debug_remove(struct platform_device *pdev)
205 struct rt5025_debug_info *di = platform_get_drvdata(pdev);
207 if (!IS_ERR(debugfs_rt_dent))
208 debugfs_remove_recursive(debugfs_rt_dent);
214 static struct platform_driver rt5025_debug_driver =
217 .name = RT5025_DEVICE_NAME "-debug",
218 .owner = THIS_MODULE,
220 .probe = rt5025_debug_probe,
221 .remove = __devexit_p(rt5025_debug_remove),
224 static int __init rt5025_debug_init(void)
226 return platform_driver_register(&rt5025_debug_driver);
228 module_init(rt5025_debug_init);
230 static void __exit rt5025_debug_exit(void)
232 platform_driver_unregister(&rt5025_debug_driver);
234 module_exit(rt5025_debug_exit);
236 MODULE_LICENSE("GPL v2");
237 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
238 MODULE_DESCRIPTION("Debug driver for RT5025");
239 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-debug");
240 MODULE_VERSION(RT5025_DRV_VER);