Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 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> |
| 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 | |
| 21 | static struct dentry *regmap_debugfs_root; |
| 22 | |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 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 | |
| 30 | static int regmap_open_file(struct inode *inode, struct file *file) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 31 | { |
| 32 | file->private_data = inode->i_private; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, |
| 37 | size_t count, loff_t *ppos) |
| 38 | { |
Mark Brown | cb3c2dc | 2011-08-09 16:47:42 +0900 | [diff] [blame] | 39 | int reg_len, val_len, tot_len; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 40 | 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 Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 56 | reg_len = regmap_calc_reg_len(map->max_register, buf, count); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 57 | val_len = 2 * map->format.val_bytes; |
| 58 | tot_len = reg_len + val_len + 3; /* : \n */ |
| 59 | |
Mark Brown | d813ae9 | 2011-09-05 08:13:07 -0700 | [diff] [blame] | 60 | for (i = 0; i < map->max_register + 1; i++) { |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 61 | if (!regmap_readable(map, i)) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 62 | continue; |
| 63 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 64 | if (regmap_precious(map, i)) |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 65 | continue; |
| 66 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 67 | /* 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 | |
| 101 | out: |
| 102 | kfree(buf); |
| 103 | return ret; |
| 104 | } |
| 105 | |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame^] | 106 | #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 | */ |
| 114 | static 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 Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 147 | static const struct file_operations regmap_map_fops = { |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 148 | .open = regmap_open_file, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 149 | .read = regmap_map_read_file, |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame^] | 150 | .write = regmap_map_write_file, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 151 | .llseek = default_llseek, |
| 152 | }; |
| 153 | |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 154 | static 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 Brown | d813ae9 | 2011-09-05 08:13:07 -0700 | [diff] [blame] | 177 | for (i = 0; i < map->max_register + 1; i++) { |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 178 | /* 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 | |
| 211 | out: |
| 212 | kfree(buf); |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static const struct file_operations regmap_access_fops = { |
| 217 | .open = regmap_open_file, |
| 218 | .read = regmap_access_read_file, |
| 219 | .llseek = default_llseek, |
| 220 | }; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 221 | |
| 222 | void 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 Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 231 | if (map->max_register) { |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 232 | debugfs_create_file("registers", 0400, map->debugfs, |
| 233 | map, ®map_map_fops); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 234 | debugfs_create_file("access", 0400, map->debugfs, |
| 235 | map, ®map_access_fops); |
| 236 | } |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 237 | |
| 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 Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void regmap_debugfs_exit(struct regmap *map) |
| 249 | { |
| 250 | debugfs_remove_recursive(map->debugfs); |
| 251 | } |
| 252 | |
| 253 | void 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 | } |