Commit | Line | Data |
---|---|---|
31244e39 MB |
1 | /* |
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> | |
31244e39 MB |
14 | #include <linux/mutex.h> |
15 | #include <linux/debugfs.h> | |
16 | #include <linux/uaccess.h> | |
51990e82 | 17 | #include <linux/device.h> |
31244e39 MB |
18 | |
19 | #include "internal.h" | |
20 | ||
21 | static struct dentry *regmap_debugfs_root; | |
22 | ||
21f55544 MB |
23 | /* Calculate the length of a fixed format */ |
24 | static 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 | ||
f0c2319f DP |
30 | static ssize_t regmap_name_read_file(struct file *file, |
31 | char __user *user_buf, size_t count, | |
32 | loff_t *ppos) | |
33 | { | |
34 | struct regmap *map = file->private_data; | |
35 | int ret; | |
36 | char *buf; | |
37 | ||
38 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
39 | if (!buf) | |
40 | return -ENOMEM; | |
41 | ||
42 | ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name); | |
43 | if (ret < 0) { | |
44 | kfree(buf); | |
45 | return ret; | |
46 | } | |
47 | ||
48 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
49 | kfree(buf); | |
50 | return ret; | |
51 | } | |
52 | ||
53 | static const struct file_operations regmap_name_fops = { | |
234e3405 | 54 | .open = simple_open, |
f0c2319f DP |
55 | .read = regmap_name_read_file, |
56 | .llseek = default_llseek, | |
57 | }; | |
58 | ||
95f971c7 MB |
59 | static void regmap_debugfs_free_dump_cache(struct regmap *map) |
60 | { | |
61 | struct regmap_debugfs_off_cache *c; | |
62 | ||
63 | while (!list_empty(&map->debugfs_off_cache)) { | |
64 | c = list_first_entry(&map->debugfs_off_cache, | |
65 | struct regmap_debugfs_off_cache, | |
66 | list); | |
67 | list_del(&c->list); | |
68 | kfree(c); | |
69 | } | |
70 | } | |
71 | ||
afab2f7b MB |
72 | /* |
73 | * Work out where the start offset maps into register numbers, bearing | |
74 | * in mind that we suppress hidden registers. | |
75 | */ | |
76 | static unsigned int regmap_debugfs_get_dump_start(struct regmap *map, | |
77 | unsigned int base, | |
78 | loff_t from, | |
79 | loff_t *pos) | |
80 | { | |
5166b7c0 MB |
81 | struct regmap_debugfs_off_cache *c = NULL; |
82 | loff_t p = 0; | |
83 | unsigned int i, ret; | |
84 | ||
85 | /* | |
86 | * If we don't have a cache build one so we don't have to do a | |
87 | * linear scan each time. | |
88 | */ | |
89 | if (list_empty(&map->debugfs_off_cache)) { | |
90 | for (i = base; i <= map->max_register; i += map->reg_stride) { | |
91 | /* Skip unprinted registers, closing off cache entry */ | |
92 | if (!regmap_readable(map, i) || | |
93 | regmap_precious(map, i)) { | |
94 | if (c) { | |
95 | c->max = p - 1; | |
96 | list_add_tail(&c->list, | |
97 | &map->debugfs_off_cache); | |
98 | c = NULL; | |
99 | } | |
100 | ||
101 | continue; | |
102 | } | |
103 | ||
104 | /* No cache entry? Start a new one */ | |
105 | if (!c) { | |
106 | c = kzalloc(sizeof(*c), GFP_KERNEL); | |
95f971c7 MB |
107 | if (!c) { |
108 | regmap_debugfs_free_dump_cache(map); | |
109 | return base; | |
110 | } | |
5166b7c0 MB |
111 | c->min = p; |
112 | c->base_reg = i; | |
113 | } | |
114 | ||
115 | p += map->debugfs_tot_len; | |
116 | } | |
117 | } | |
afab2f7b | 118 | |
e8d6539c MB |
119 | /* Close the last entry off if we didn't scan beyond it */ |
120 | if (c) { | |
121 | c->max = p - 1; | |
122 | list_add_tail(&c->list, | |
123 | &map->debugfs_off_cache); | |
e8d6539c MB |
124 | } |
125 | ||
5bd9f4bb MB |
126 | /* |
127 | * This should never happen; we return above if we fail to | |
128 | * allocate and we should never be in this code if there are | |
129 | * no registers at all. | |
130 | */ | |
a3471469 RK |
131 | WARN_ON(list_empty(&map->debugfs_off_cache)); |
132 | ret = base; | |
5bd9f4bb | 133 | |
5166b7c0 MB |
134 | /* Find the relevant block */ |
135 | list_for_each_entry(c, &map->debugfs_off_cache, list) { | |
5a1d6d17 | 136 | if (from >= c->min && from <= c->max) { |
5166b7c0 MB |
137 | *pos = c->min; |
138 | return c->base_reg; | |
afab2f7b MB |
139 | } |
140 | ||
120f8051 MB |
141 | *pos = c->min; |
142 | ret = c->base_reg; | |
afab2f7b MB |
143 | } |
144 | ||
5166b7c0 | 145 | return ret; |
afab2f7b MB |
146 | } |
147 | ||
bd9cc12f MB |
148 | static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, |
149 | unsigned int to, char __user *user_buf, | |
150 | size_t count, loff_t *ppos) | |
31244e39 | 151 | { |
31244e39 | 152 | size_t buf_pos = 0; |
afab2f7b | 153 | loff_t p = *ppos; |
31244e39 MB |
154 | ssize_t ret; |
155 | int i; | |
31244e39 | 156 | char *buf; |
afab2f7b | 157 | unsigned int val, start_reg; |
31244e39 MB |
158 | |
159 | if (*ppos < 0 || !count) | |
160 | return -EINVAL; | |
161 | ||
162 | buf = kmalloc(count, GFP_KERNEL); | |
163 | if (!buf) | |
164 | return -ENOMEM; | |
165 | ||
166 | /* Calculate the length of a fixed format */ | |
cbc1938b MB |
167 | if (!map->debugfs_tot_len) { |
168 | map->debugfs_reg_len = regmap_calc_reg_len(map->max_register, | |
169 | buf, count); | |
170 | map->debugfs_val_len = 2 * map->format.val_bytes; | |
171 | map->debugfs_tot_len = map->debugfs_reg_len + | |
172 | map->debugfs_val_len + 3; /* : \n */ | |
173 | } | |
31244e39 | 174 | |
afab2f7b MB |
175 | /* Work out which register we're starting at */ |
176 | start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); | |
177 | ||
178 | for (i = start_reg; i <= to; i += map->reg_stride) { | |
8de2f081 | 179 | if (!regmap_readable(map, i)) |
31244e39 MB |
180 | continue; |
181 | ||
8de2f081 | 182 | if (regmap_precious(map, i)) |
2efe1642 MB |
183 | continue; |
184 | ||
31244e39 MB |
185 | /* If we're in the region the user is trying to read */ |
186 | if (p >= *ppos) { | |
187 | /* ...but not beyond it */ | |
f3eb8399 | 188 | if (buf_pos + map->debugfs_tot_len > count) |
31244e39 MB |
189 | break; |
190 | ||
191 | /* Format the register */ | |
192 | snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", | |
cbc1938b MB |
193 | map->debugfs_reg_len, i - from); |
194 | buf_pos += map->debugfs_reg_len + 2; | |
31244e39 MB |
195 | |
196 | /* Format the value, write all X if we can't read */ | |
197 | ret = regmap_read(map, i, &val); | |
198 | if (ret == 0) | |
199 | snprintf(buf + buf_pos, count - buf_pos, | |
cbc1938b | 200 | "%.*x", map->debugfs_val_len, val); |
31244e39 | 201 | else |
cbc1938b MB |
202 | memset(buf + buf_pos, 'X', |
203 | map->debugfs_val_len); | |
31244e39 MB |
204 | buf_pos += 2 * map->format.val_bytes; |
205 | ||
206 | buf[buf_pos++] = '\n'; | |
207 | } | |
cbc1938b | 208 | p += map->debugfs_tot_len; |
31244e39 MB |
209 | } |
210 | ||
211 | ret = buf_pos; | |
212 | ||
213 | if (copy_to_user(user_buf, buf, buf_pos)) { | |
214 | ret = -EFAULT; | |
215 | goto out; | |
216 | } | |
217 | ||
218 | *ppos += buf_pos; | |
219 | ||
220 | out: | |
221 | kfree(buf); | |
222 | return ret; | |
223 | } | |
224 | ||
bd9cc12f MB |
225 | static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, |
226 | size_t count, loff_t *ppos) | |
227 | { | |
228 | struct regmap *map = file->private_data; | |
229 | ||
230 | return regmap_read_debugfs(map, 0, map->max_register, user_buf, | |
231 | count, ppos); | |
232 | } | |
233 | ||
09c6ecd3 DP |
234 | #undef REGMAP_ALLOW_WRITE_DEBUGFS |
235 | #ifdef REGMAP_ALLOW_WRITE_DEBUGFS | |
236 | /* | |
237 | * This can be dangerous especially when we have clients such as | |
238 | * PMICs, therefore don't provide any real compile time configuration option | |
239 | * for this feature, people who want to use this will need to modify | |
240 | * the source code directly. | |
241 | */ | |
242 | static ssize_t regmap_map_write_file(struct file *file, | |
243 | const char __user *user_buf, | |
244 | size_t count, loff_t *ppos) | |
245 | { | |
246 | char buf[32]; | |
247 | size_t buf_size; | |
248 | char *start = buf; | |
249 | unsigned long reg, value; | |
250 | struct regmap *map = file->private_data; | |
251 | ||
252 | buf_size = min(count, (sizeof(buf)-1)); | |
253 | if (copy_from_user(buf, user_buf, buf_size)) | |
254 | return -EFAULT; | |
255 | buf[buf_size] = 0; | |
256 | ||
257 | while (*start == ' ') | |
258 | start++; | |
259 | reg = simple_strtoul(start, &start, 16); | |
260 | while (*start == ' ') | |
261 | start++; | |
262 | if (strict_strtoul(start, 16, &value)) | |
263 | return -EINVAL; | |
264 | ||
265 | /* Userspace has been fiddling around behind the kernel's back */ | |
266 | add_taint(TAINT_USER); | |
267 | ||
268 | regmap_write(map, reg, value); | |
269 | return buf_size; | |
270 | } | |
271 | #else | |
272 | #define regmap_map_write_file NULL | |
273 | #endif | |
274 | ||
31244e39 | 275 | static const struct file_operations regmap_map_fops = { |
234e3405 | 276 | .open = simple_open, |
31244e39 | 277 | .read = regmap_map_read_file, |
09c6ecd3 | 278 | .write = regmap_map_write_file, |
31244e39 MB |
279 | .llseek = default_llseek, |
280 | }; | |
281 | ||
4b020b3f MB |
282 | static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, |
283 | size_t count, loff_t *ppos) | |
284 | { | |
285 | struct regmap_range_node *range = file->private_data; | |
286 | struct regmap *map = range->map; | |
287 | ||
288 | return regmap_read_debugfs(map, range->range_min, range->range_max, | |
289 | user_buf, count, ppos); | |
290 | } | |
291 | ||
292 | static const struct file_operations regmap_range_fops = { | |
293 | .open = simple_open, | |
294 | .read = regmap_range_read_file, | |
295 | .llseek = default_llseek, | |
296 | }; | |
297 | ||
449e3842 MB |
298 | static ssize_t regmap_access_read_file(struct file *file, |
299 | char __user *user_buf, size_t count, | |
300 | loff_t *ppos) | |
301 | { | |
302 | int reg_len, tot_len; | |
303 | size_t buf_pos = 0; | |
304 | loff_t p = 0; | |
305 | ssize_t ret; | |
306 | int i; | |
307 | struct regmap *map = file->private_data; | |
308 | char *buf; | |
309 | ||
310 | if (*ppos < 0 || !count) | |
311 | return -EINVAL; | |
312 | ||
313 | buf = kmalloc(count, GFP_KERNEL); | |
314 | if (!buf) | |
315 | return -ENOMEM; | |
316 | ||
317 | /* Calculate the length of a fixed format */ | |
318 | reg_len = regmap_calc_reg_len(map->max_register, buf, count); | |
319 | tot_len = reg_len + 10; /* ': R W V P\n' */ | |
320 | ||
f01ee60f | 321 | for (i = 0; i <= map->max_register; i += map->reg_stride) { |
449e3842 MB |
322 | /* Ignore registers which are neither readable nor writable */ |
323 | if (!regmap_readable(map, i) && !regmap_writeable(map, i)) | |
324 | continue; | |
325 | ||
326 | /* If we're in the region the user is trying to read */ | |
327 | if (p >= *ppos) { | |
328 | /* ...but not beyond it */ | |
329 | if (buf_pos >= count - 1 - tot_len) | |
330 | break; | |
331 | ||
332 | /* Format the register */ | |
333 | snprintf(buf + buf_pos, count - buf_pos, | |
334 | "%.*x: %c %c %c %c\n", | |
335 | reg_len, i, | |
336 | regmap_readable(map, i) ? 'y' : 'n', | |
337 | regmap_writeable(map, i) ? 'y' : 'n', | |
338 | regmap_volatile(map, i) ? 'y' : 'n', | |
339 | regmap_precious(map, i) ? 'y' : 'n'); | |
340 | ||
341 | buf_pos += tot_len; | |
342 | } | |
343 | p += tot_len; | |
344 | } | |
345 | ||
346 | ret = buf_pos; | |
347 | ||
348 | if (copy_to_user(user_buf, buf, buf_pos)) { | |
349 | ret = -EFAULT; | |
350 | goto out; | |
351 | } | |
352 | ||
353 | *ppos += buf_pos; | |
354 | ||
355 | out: | |
356 | kfree(buf); | |
357 | return ret; | |
358 | } | |
359 | ||
360 | static const struct file_operations regmap_access_fops = { | |
234e3405 | 361 | .open = simple_open, |
449e3842 MB |
362 | .read = regmap_access_read_file, |
363 | .llseek = default_llseek, | |
364 | }; | |
31244e39 | 365 | |
d3c242e1 | 366 | void regmap_debugfs_init(struct regmap *map, const char *name) |
31244e39 | 367 | { |
4b020b3f MB |
368 | struct rb_node *next; |
369 | struct regmap_range_node *range_node; | |
370 | ||
5166b7c0 MB |
371 | INIT_LIST_HEAD(&map->debugfs_off_cache); |
372 | ||
d3c242e1 SW |
373 | if (name) { |
374 | map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", | |
375 | dev_name(map->dev), name); | |
376 | name = map->debugfs_name; | |
377 | } else { | |
378 | name = dev_name(map->dev); | |
379 | } | |
380 | ||
381 | map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); | |
31244e39 MB |
382 | if (!map->debugfs) { |
383 | dev_warn(map->dev, "Failed to create debugfs directory\n"); | |
384 | return; | |
385 | } | |
386 | ||
f0c2319f DP |
387 | debugfs_create_file("name", 0400, map->debugfs, |
388 | map, ®map_name_fops); | |
389 | ||
449e3842 | 390 | if (map->max_register) { |
31244e39 MB |
391 | debugfs_create_file("registers", 0400, map->debugfs, |
392 | map, ®map_map_fops); | |
449e3842 MB |
393 | debugfs_create_file("access", 0400, map->debugfs, |
394 | map, ®map_access_fops); | |
395 | } | |
028a01e6 MB |
396 | |
397 | if (map->cache_type) { | |
398 | debugfs_create_bool("cache_only", 0400, map->debugfs, | |
399 | &map->cache_only); | |
400 | debugfs_create_bool("cache_dirty", 0400, map->debugfs, | |
401 | &map->cache_dirty); | |
402 | debugfs_create_bool("cache_bypass", 0400, map->debugfs, | |
403 | &map->cache_bypass); | |
404 | } | |
4b020b3f MB |
405 | |
406 | next = rb_first(&map->range_tree); | |
407 | while (next) { | |
408 | range_node = rb_entry(next, struct regmap_range_node, node); | |
409 | ||
410 | if (range_node->name) | |
411 | debugfs_create_file(range_node->name, 0400, | |
412 | map->debugfs, range_node, | |
413 | ®map_range_fops); | |
414 | ||
415 | next = rb_next(&range_node->node); | |
416 | } | |
31244e39 MB |
417 | } |
418 | ||
419 | void regmap_debugfs_exit(struct regmap *map) | |
420 | { | |
421 | debugfs_remove_recursive(map->debugfs); | |
95f971c7 | 422 | regmap_debugfs_free_dump_cache(map); |
d3c242e1 | 423 | kfree(map->debugfs_name); |
31244e39 MB |
424 | } |
425 | ||
426 | void regmap_debugfs_initcall(void) | |
427 | { | |
428 | regmap_debugfs_root = debugfs_create_dir("regmap", NULL); | |
429 | if (!regmap_debugfs_root) { | |
430 | pr_warn("regmap: Failed to create debugfs root\n"); | |
431 | return; | |
432 | } | |
433 | } |