blob: 7accb81dd94e1e8d6fb8a59741887cc2a24fccca [file] [log] [blame]
Mark Brown31244e32011-07-20 22:56:53 +01001/*
2 * Register map access API - debugfs
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.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/slab.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/debugfs.h>
17#include <linux/uaccess.h>
18
19#include "internal.h"
20
21static struct dentry *regmap_debugfs_root;
22
Mark Brown21f55542011-08-10 17:15:31 +090023/* Calculate the length of a fixed format */
24static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
25{
26 snprintf(buf, buf_size, "%x", max_val);
27 return strlen(buf);
28}
29
30static int regmap_open_file(struct inode *inode, struct file *file)
Mark Brown31244e32011-07-20 22:56:53 +010031{
32 file->private_data = inode->i_private;
33 return 0;
34}
35
36static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
37 size_t count, loff_t *ppos)
38{
Mark Browncb3c2dc2011-08-09 16:47:42 +090039 int reg_len, val_len, tot_len;
Mark Brown31244e32011-07-20 22:56:53 +010040 size_t buf_pos = 0;
41 loff_t p = 0;
42 ssize_t ret;
43 int i;
44 struct regmap *map = file->private_data;
45 char *buf;
46 unsigned int val;
47
48 if (*ppos < 0 || !count)
49 return -EINVAL;
50
51 buf = kmalloc(count, GFP_KERNEL);
52 if (!buf)
53 return -ENOMEM;
54
55 /* Calculate the length of a fixed format */
Mark Brown21f55542011-08-10 17:15:31 +090056 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
Mark Brown31244e32011-07-20 22:56:53 +010057 val_len = 2 * map->format.val_bytes;
58 tot_len = reg_len + val_len + 3; /* : \n */
59
Mark Brownd813ae92011-09-05 08:13:07 -070060 for (i = 0; i < map->max_register + 1; i++) {
Mark Brown8de2f082011-08-10 17:14:41 +090061 if (!regmap_readable(map, i))
Mark Brown31244e32011-07-20 22:56:53 +010062 continue;
63
Mark Brown8de2f082011-08-10 17:14:41 +090064 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +090065 continue;
66
Mark Brown31244e32011-07-20 22:56:53 +010067 /* If we're in the region the user is trying to read */
68 if (p >= *ppos) {
69 /* ...but not beyond it */
70 if (buf_pos >= count - 1 - tot_len)
71 break;
72
73 /* Format the register */
74 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
75 reg_len, i);
76 buf_pos += reg_len + 2;
77
78 /* Format the value, write all X if we can't read */
79 ret = regmap_read(map, i, &val);
80 if (ret == 0)
81 snprintf(buf + buf_pos, count - buf_pos,
82 "%.*x", val_len, val);
83 else
84 memset(buf + buf_pos, 'X', val_len);
85 buf_pos += 2 * map->format.val_bytes;
86
87 buf[buf_pos++] = '\n';
88 }
89 p += tot_len;
90 }
91
92 ret = buf_pos;
93
94 if (copy_to_user(user_buf, buf, buf_pos)) {
95 ret = -EFAULT;
96 goto out;
97 }
98
99 *ppos += buf_pos;
100
101out:
102 kfree(buf);
103 return ret;
104}
105
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000106#undef REGMAP_ALLOW_WRITE_DEBUGFS
107#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
108/*
109 * This can be dangerous especially when we have clients such as
110 * PMICs, therefore don't provide any real compile time configuration option
111 * for this feature, people who want to use this will need to modify
112 * the source code directly.
113 */
114static ssize_t regmap_map_write_file(struct file *file,
115 const char __user *user_buf,
116 size_t count, loff_t *ppos)
117{
118 char buf[32];
119 size_t buf_size;
120 char *start = buf;
121 unsigned long reg, value;
122 struct regmap *map = file->private_data;
123
124 buf_size = min(count, (sizeof(buf)-1));
125 if (copy_from_user(buf, user_buf, buf_size))
126 return -EFAULT;
127 buf[buf_size] = 0;
128
129 while (*start == ' ')
130 start++;
131 reg = simple_strtoul(start, &start, 16);
132 while (*start == ' ')
133 start++;
134 if (strict_strtoul(start, 16, &value))
135 return -EINVAL;
136
137 /* Userspace has been fiddling around behind the kernel's back */
138 add_taint(TAINT_USER);
139
140 regmap_write(map, reg, value);
141 return buf_size;
142}
143#else
144#define regmap_map_write_file NULL
145#endif
146
Mark Brown31244e32011-07-20 22:56:53 +0100147static const struct file_operations regmap_map_fops = {
Mark Brown21f55542011-08-10 17:15:31 +0900148 .open = regmap_open_file,
Mark Brown31244e32011-07-20 22:56:53 +0100149 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000150 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100151 .llseek = default_llseek,
152};
153
Mark Brown449e3842011-08-10 17:28:04 +0900154static ssize_t regmap_access_read_file(struct file *file,
155 char __user *user_buf, size_t count,
156 loff_t *ppos)
157{
158 int reg_len, tot_len;
159 size_t buf_pos = 0;
160 loff_t p = 0;
161 ssize_t ret;
162 int i;
163 struct regmap *map = file->private_data;
164 char *buf;
165
166 if (*ppos < 0 || !count)
167 return -EINVAL;
168
169 buf = kmalloc(count, GFP_KERNEL);
170 if (!buf)
171 return -ENOMEM;
172
173 /* Calculate the length of a fixed format */
174 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
175 tot_len = reg_len + 10; /* ': R W V P\n' */
176
Mark Brownd813ae92011-09-05 08:13:07 -0700177 for (i = 0; i < map->max_register + 1; i++) {
Mark Brown449e3842011-08-10 17:28:04 +0900178 /* Ignore registers which are neither readable nor writable */
179 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
180 continue;
181
182 /* If we're in the region the user is trying to read */
183 if (p >= *ppos) {
184 /* ...but not beyond it */
185 if (buf_pos >= count - 1 - tot_len)
186 break;
187
188 /* Format the register */
189 snprintf(buf + buf_pos, count - buf_pos,
190 "%.*x: %c %c %c %c\n",
191 reg_len, i,
192 regmap_readable(map, i) ? 'y' : 'n',
193 regmap_writeable(map, i) ? 'y' : 'n',
194 regmap_volatile(map, i) ? 'y' : 'n',
195 regmap_precious(map, i) ? 'y' : 'n');
196
197 buf_pos += tot_len;
198 }
199 p += tot_len;
200 }
201
202 ret = buf_pos;
203
204 if (copy_to_user(user_buf, buf, buf_pos)) {
205 ret = -EFAULT;
206 goto out;
207 }
208
209 *ppos += buf_pos;
210
211out:
212 kfree(buf);
213 return ret;
214}
215
216static const struct file_operations regmap_access_fops = {
217 .open = regmap_open_file,
218 .read = regmap_access_read_file,
219 .llseek = default_llseek,
220};
Mark Brown31244e32011-07-20 22:56:53 +0100221
222void regmap_debugfs_init(struct regmap *map)
223{
224 map->debugfs = debugfs_create_dir(dev_name(map->dev),
225 regmap_debugfs_root);
226 if (!map->debugfs) {
227 dev_warn(map->dev, "Failed to create debugfs directory\n");
228 return;
229 }
230
Mark Brown449e3842011-08-10 17:28:04 +0900231 if (map->max_register) {
Mark Brown31244e32011-07-20 22:56:53 +0100232 debugfs_create_file("registers", 0400, map->debugfs,
233 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900234 debugfs_create_file("access", 0400, map->debugfs,
235 map, &regmap_access_fops);
236 }
Mark Brown028a01e2012-02-06 18:02:06 +0000237
238 if (map->cache_type) {
239 debugfs_create_bool("cache_only", 0400, map->debugfs,
240 &map->cache_only);
241 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
242 &map->cache_dirty);
243 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
244 &map->cache_bypass);
245 }
Mark Brown31244e32011-07-20 22:56:53 +0100246}
247
248void regmap_debugfs_exit(struct regmap *map)
249{
250 debugfs_remove_recursive(map->debugfs);
251}
252
253void regmap_debugfs_initcall(void)
254{
255 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
256 if (!regmap_debugfs_root) {
257 pr_warn("regmap: Failed to create debugfs root\n");
258 return;
259 }
260}