Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android' into...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rt5025-debug.c
1 /*
2  *  drivers/mfd/rt5025-debug.c
3  *  Driver foo Richtek RT5025 PMIC Debug
4  *
5  *  Copyright (C) 2013 Richtek Electronics
6  *  cy_huang <cy_huang@richtek.com>
7  *
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.
11  */
12
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>
19
20 #include <linux/uaccess.h>
21 #include <linux/debugfs.h>
22 #include <linux/string.h>
23
24 #include <linux/mfd/rt5025.h>
25
26 struct rt5025_debug_info {
27         struct i2c_client *i2c;
28 };
29
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;
36
37 static unsigned char read_data[10];
38
39 static int reg_debug_open(struct inode *inode, struct file *file)
40 {
41         file->private_data = inode->i_private;
42         return 0;
43 }
44
45 static int get_parameters(char *buf, long int *param1, int num_of_par)
46 {
47         char *token;
48         int base, cnt;
49
50         token = strsep(&buf, " ");
51
52         for (cnt = 0; cnt < num_of_par; cnt++) {
53                 if (token != NULL) {
54                         if ((token[1] == 'x') || (token[1] == 'X'))
55                                 base = 16;
56                         else
57                                 base = 10;
58
59                         if (strict_strtoul(token, base, &param1[cnt]) != 0)
60                                 return -EINVAL;
61
62                         token = strsep(&buf, " ");
63                         }
64                 else
65                         return -EINVAL;
66         }
67         return 0;
68 }
69
70 #define LOG_FORMAT "0x%02x\n0x%02x\n0x%02x\n0x%02x\n0x%02x\n"
71
72 static ssize_t reg_debug_read(struct file *filp, char __user *ubuf,
73                                 size_t count, loff_t *ppos)
74 {
75         char *access_str = filp->private_data;
76         char lbuf[150];
77         if (!strcmp(access_str, "regs"))
78         {
79                 RTINFO("read regs file\n");
80                 /* read regs */
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]);
84         }
85         else
86                 snprintf(lbuf, sizeof(lbuf), "0x%02x\n", read_data[0]);
87         return simple_read_from_buffer(ubuf, count, ppos, lbuf, strlen(lbuf));
88 }
89
90 static ssize_t reg_debug_write(struct file *filp,
91         const char __user *ubuf, size_t cnt, loff_t *ppos)
92 {
93         char *access_str = filp->private_data;
94         char lbuf[32];
95         int rc;
96         long int param[5];
97
98         if (cnt > sizeof(lbuf) - 1)
99                 return -EINVAL;
100
101         rc = copy_from_user(lbuf, ubuf, cnt);
102         if (rc)
103                 return -EFAULT;
104
105         lbuf[cnt] = '\0';
106
107         if (!strcmp(access_str, "poke")) {
108                 /* write */
109                 rc = get_parameters(lbuf, param, 2);
110                 if ((param[0] <= 0xFF) && (param[1] <= 0xFF) && (rc == 0))
111                 {
112                         rt5025_reg_write(client, param[0], (unsigned char)param[1]);
113                 }
114                 else
115                         rc = -EINVAL;
116         } else if (!strcmp(access_str, "peek")) {
117                 /* read */
118                 rc = get_parameters(lbuf, param, 1);
119                 if ((param[0] <= 0xFF) && (rc == 0))
120                 {
121                         read_data[0] = rt5025_reg_read(client, param[0]);
122                 }
123                 else
124                         rc = -EINVAL;
125         } else if (!strcmp(access_str, "regs")) {
126                 /* read */
127                 rc = get_parameters(lbuf, param, 1);
128                 if ((param[0] <= 0xFF) && (rc == 0))
129                 {
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]);
141                 }
142                 else
143                         rc = -EINVAL;
144         } else if (!strcmp(access_str, "reset_b")) {
145                 /* read */
146                 rc = get_parameters(lbuf, param, 1);
147                 if (param[0] == 1 && rc == 0)
148                 {
149                         memset(lbuf, 0, 15);
150                         rt5025_reg_block_write(client, 0x21, 15, lbuf);
151                 }
152         }
153
154         if (rc == 0)
155                 rc = cnt;
156
157         return rc;
158 }
159
160 static const struct file_operations reg_debug_ops = {
161         .open = reg_debug_open,
162         .write = reg_debug_write,
163         .read = reg_debug_read
164 };
165
166 static int __devinit rt5025_debug_probe(struct platform_device *pdev)
167 {
168         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
169         struct rt5025_debug_info *di;
170
171         di = kzalloc(sizeof(*di), GFP_KERNEL);
172         if (!di)
173                 return -ENOMEM;
174
175         di->i2c = chip->i2c;
176
177         RTINFO("add debugfs for core RT5025");
178         client = chip->i2c;
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", &reg_debug_ops);
184
185                 debugfs_poke = debugfs_create_file("poke",
186                 S_IFREG | S_IRUGO, debugfs_rt_dent,
187                 (void *) "poke", &reg_debug_ops);
188
189                 debugfs_regs = debugfs_create_file("regs",
190                 S_IFREG | S_IRUGO, debugfs_rt_dent,
191                 (void *) "regs", &reg_debug_ops);
192
193                 debugfs_reset_b = debugfs_create_file("reset_b",
194                 S_IFREG | S_IRUGO, debugfs_rt_dent,
195                 (void *) "reset_b", &reg_debug_ops);
196         }
197
198         platform_set_drvdata(pdev, di);
199
200         return 0;
201 }
202
203 static int __devexit rt5025_debug_remove(struct platform_device *pdev)
204 {
205         struct rt5025_debug_info *di = platform_get_drvdata(pdev);
206
207         if (!IS_ERR(debugfs_rt_dent))
208                 debugfs_remove_recursive(debugfs_rt_dent);
209
210         kfree(di);
211         return 0;
212 }
213
214 static struct platform_driver rt5025_debug_driver = 
215 {
216         .driver = {
217                 .name = RT5025_DEVICE_NAME "-debug",
218                 .owner = THIS_MODULE,
219         },
220         .probe = rt5025_debug_probe,
221         .remove = __devexit_p(rt5025_debug_remove),
222 };
223
224 static int __init rt5025_debug_init(void)
225 {
226         return platform_driver_register(&rt5025_debug_driver);
227 }
228 module_init(rt5025_debug_init);
229
230 static void __exit rt5025_debug_exit(void)
231 {
232         platform_driver_unregister(&rt5025_debug_driver);
233 }
234 module_exit(rt5025_debug_exit);
235
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);