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;
35 static unsigned char read_data;
37 static int reg_debug_open(struct inode *inode, struct file *file)
39 file->private_data = inode->i_private;
43 static int get_parameters(char *buf, long int *param1, int num_of_par)
48 token = strsep(&buf, " ");
50 for (cnt = 0; cnt < num_of_par; cnt++) {
52 if ((token[1] == 'x') || (token[1] == 'X'))
57 if (strict_strtoul(token, base, ¶m1[cnt]) != 0)
60 token = strsep(&buf, " ");
68 static ssize_t reg_debug_read(struct file *filp, char __user *ubuf,
69 size_t count, loff_t *ppos)
73 snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
74 return simple_read_from_buffer(ubuf, count, ppos, lbuf, strlen(lbuf));
77 static ssize_t reg_debug_write(struct file *filp,
78 const char __user *ubuf, size_t cnt, loff_t *ppos)
80 char *access_str = filp->private_data;
85 if (cnt > sizeof(lbuf) - 1)
88 rc = copy_from_user(lbuf, ubuf, cnt);
94 if (!strcmp(access_str, "poke")) {
96 rc = get_parameters(lbuf, param, 2);
97 if ((param[0] <= 0xFF) && (param[1] <= 0xFF) && (rc == 0))
99 rt5025_reg_write(client, param[0], (unsigned char)param[1]);
103 } else if (!strcmp(access_str, "peek")) {
105 rc = get_parameters(lbuf, param, 1);
106 if ((param[0] <= 0xFF) && (rc == 0))
108 read_data = rt5025_reg_read(client, param[0]);
120 static const struct file_operations reg_debug_ops = {
121 .open = reg_debug_open,
122 .write = reg_debug_write,
123 .read = reg_debug_read
126 static int __devinit rt5025_debug_probe(struct platform_device *pdev)
128 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
129 struct rt5025_debug_info *di;
131 di = kzalloc(sizeof(*di), GFP_KERNEL);
137 RTINFO("add debugfs for core RT5025");
139 debugfs_rt_dent = debugfs_create_dir("rt5025_dbg", 0);
140 if (!IS_ERR(debugfs_rt_dent)) {
141 debugfs_peek = debugfs_create_file("peek",
142 S_IFREG | S_IRUGO, debugfs_rt_dent,
143 (void *) "peek", ®_debug_ops);
145 debugfs_poke = debugfs_create_file("poke",
146 S_IFREG | S_IRUGO, debugfs_rt_dent,
147 (void *) "poke", ®_debug_ops);
150 platform_set_drvdata(pdev, di);
155 static int __devexit rt5025_debug_remove(struct platform_device *pdev)
157 struct rt5025_debug_info *di = platform_get_drvdata(pdev);
159 if (!IS_ERR(debugfs_rt_dent))
160 debugfs_remove_recursive(debugfs_rt_dent);
166 static struct platform_driver rt5025_debug_driver =
169 .name = RT5025_DEVICE_NAME "-debug",
170 .owner = THIS_MODULE,
172 .probe = rt5025_debug_probe,
173 .remove = __devexit_p(rt5025_debug_remove),
176 static int __init rt5025_debug_init(void)
178 return platform_driver_register(&rt5025_debug_driver);
180 module_init(rt5025_debug_init);
182 static void __exit rt5025_debug_exit(void)
184 platform_driver_unregister(&rt5025_debug_driver);
186 module_exit(rt5025_debug_exit);
188 MODULE_LICENSE("GPL v2");
189 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
190 MODULE_DESCRIPTION("Debug driver for RT5025");
191 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-debug");