USB: usb-storage: unusual_devs update for Super TOP SATA bridge
[linux/fpc-iii.git] / drivers / base / regmap / regmap-debugfs.c
blob8ab1eab90be7c0cf5cb34f380f4cc45c6b11631c
1 /*
2 * Register map access API - debugfs
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.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/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/debugfs.h>
16 #include <linux/uaccess.h>
17 #include <linux/device.h>
19 #include "internal.h"
21 static struct dentry *regmap_debugfs_root;
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)
26 snprintf(buf, buf_size, "%x", max_val);
27 return strlen(buf);
30 static ssize_t regmap_name_read_file(struct file *file,
31 char __user *user_buf, size_t count,
32 loff_t *ppos)
34 struct regmap *map = file->private_data;
35 int ret;
36 char *buf;
38 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
39 if (!buf)
40 return -ENOMEM;
42 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
43 if (ret < 0) {
44 kfree(buf);
45 return ret;
48 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
49 kfree(buf);
50 return ret;
53 static const struct file_operations regmap_name_fops = {
54 .open = simple_open,
55 .read = regmap_name_read_file,
56 .llseek = default_llseek,
59 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
60 size_t count, loff_t *ppos)
62 int reg_len, val_len, tot_len;
63 size_t buf_pos = 0;
64 loff_t p = 0;
65 ssize_t ret;
66 int i;
67 struct regmap *map = file->private_data;
68 char *buf;
69 unsigned int val;
71 if (*ppos < 0 || !count)
72 return -EINVAL;
74 buf = kmalloc(count, GFP_KERNEL);
75 if (!buf)
76 return -ENOMEM;
78 /* Calculate the length of a fixed format */
79 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
80 val_len = 2 * map->format.val_bytes;
81 tot_len = reg_len + val_len + 3; /* : \n */
83 for (i = 0; i < map->max_register + 1; i++) {
84 if (!regmap_readable(map, i))
85 continue;
87 if (regmap_precious(map, i))
88 continue;
90 /* If we're in the region the user is trying to read */
91 if (p >= *ppos) {
92 /* ...but not beyond it */
93 if (buf_pos + 1 + tot_len >= count)
94 break;
96 /* Format the register */
97 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
98 reg_len, i);
99 buf_pos += reg_len + 2;
101 /* Format the value, write all X if we can't read */
102 ret = regmap_read(map, i, &val);
103 if (ret == 0)
104 snprintf(buf + buf_pos, count - buf_pos,
105 "%.*x", val_len, val);
106 else
107 memset(buf + buf_pos, 'X', val_len);
108 buf_pos += 2 * map->format.val_bytes;
110 buf[buf_pos++] = '\n';
112 p += tot_len;
115 ret = buf_pos;
117 if (copy_to_user(user_buf, buf, buf_pos)) {
118 ret = -EFAULT;
119 goto out;
122 *ppos += buf_pos;
124 out:
125 kfree(buf);
126 return ret;
129 #undef REGMAP_ALLOW_WRITE_DEBUGFS
130 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
132 * This can be dangerous especially when we have clients such as
133 * PMICs, therefore don't provide any real compile time configuration option
134 * for this feature, people who want to use this will need to modify
135 * the source code directly.
137 static ssize_t regmap_map_write_file(struct file *file,
138 const char __user *user_buf,
139 size_t count, loff_t *ppos)
141 char buf[32];
142 size_t buf_size;
143 char *start = buf;
144 unsigned long reg, value;
145 struct regmap *map = file->private_data;
147 buf_size = min(count, (sizeof(buf)-1));
148 if (copy_from_user(buf, user_buf, buf_size))
149 return -EFAULT;
150 buf[buf_size] = 0;
152 while (*start == ' ')
153 start++;
154 reg = simple_strtoul(start, &start, 16);
155 while (*start == ' ')
156 start++;
157 if (strict_strtoul(start, 16, &value))
158 return -EINVAL;
160 /* Userspace has been fiddling around behind the kernel's back */
161 add_taint(TAINT_USER);
163 regmap_write(map, reg, value);
164 return buf_size;
166 #else
167 #define regmap_map_write_file NULL
168 #endif
170 static const struct file_operations regmap_map_fops = {
171 .open = simple_open,
172 .read = regmap_map_read_file,
173 .write = regmap_map_write_file,
174 .llseek = default_llseek,
177 static ssize_t regmap_access_read_file(struct file *file,
178 char __user *user_buf, size_t count,
179 loff_t *ppos)
181 int reg_len, tot_len;
182 size_t buf_pos = 0;
183 loff_t p = 0;
184 ssize_t ret;
185 int i;
186 struct regmap *map = file->private_data;
187 char *buf;
189 if (*ppos < 0 || !count)
190 return -EINVAL;
192 buf = kmalloc(count, GFP_KERNEL);
193 if (!buf)
194 return -ENOMEM;
196 /* Calculate the length of a fixed format */
197 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
198 tot_len = reg_len + 10; /* ': R W V P\n' */
200 for (i = 0; i < map->max_register + 1; i++) {
201 /* Ignore registers which are neither readable nor writable */
202 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
203 continue;
205 /* If we're in the region the user is trying to read */
206 if (p >= *ppos) {
207 /* ...but not beyond it */
208 if (buf_pos >= count - 1 - tot_len)
209 break;
211 /* Format the register */
212 snprintf(buf + buf_pos, count - buf_pos,
213 "%.*x: %c %c %c %c\n",
214 reg_len, i,
215 regmap_readable(map, i) ? 'y' : 'n',
216 regmap_writeable(map, i) ? 'y' : 'n',
217 regmap_volatile(map, i) ? 'y' : 'n',
218 regmap_precious(map, i) ? 'y' : 'n');
220 buf_pos += tot_len;
222 p += tot_len;
225 ret = buf_pos;
227 if (copy_to_user(user_buf, buf, buf_pos)) {
228 ret = -EFAULT;
229 goto out;
232 *ppos += buf_pos;
234 out:
235 kfree(buf);
236 return ret;
239 static const struct file_operations regmap_access_fops = {
240 .open = simple_open,
241 .read = regmap_access_read_file,
242 .llseek = default_llseek,
245 void regmap_debugfs_init(struct regmap *map)
247 map->debugfs = debugfs_create_dir(dev_name(map->dev),
248 regmap_debugfs_root);
249 if (!map->debugfs) {
250 dev_warn(map->dev, "Failed to create debugfs directory\n");
251 return;
254 debugfs_create_file("name", 0400, map->debugfs,
255 map, &regmap_name_fops);
257 if (map->max_register) {
258 debugfs_create_file("registers", 0400, map->debugfs,
259 map, &regmap_map_fops);
260 debugfs_create_file("access", 0400, map->debugfs,
261 map, &regmap_access_fops);
264 if (map->cache_type) {
265 debugfs_create_bool("cache_only", 0400, map->debugfs,
266 &map->cache_only);
267 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
268 &map->cache_dirty);
269 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
270 &map->cache_bypass);
274 void regmap_debugfs_exit(struct regmap *map)
276 debugfs_remove_recursive(map->debugfs);
279 void regmap_debugfs_initcall(void)
281 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
282 if (!regmap_debugfs_root) {
283 pr_warn("regmap: Failed to create debugfs root\n");
284 return;