18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Register map access API - debugfs 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright 2011 Wolfson Microelectronics plc 68c2ecf20Sopenharmony_ci// 78c2ecf20Sopenharmony_ci// Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/slab.h> 108c2ecf20Sopenharmony_ci#include <linux/mutex.h> 118c2ecf20Sopenharmony_ci#include <linux/debugfs.h> 128c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 138c2ecf20Sopenharmony_ci#include <linux/device.h> 148c2ecf20Sopenharmony_ci#include <linux/list.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "internal.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistruct regmap_debugfs_node { 198c2ecf20Sopenharmony_ci struct regmap *map; 208c2ecf20Sopenharmony_ci struct list_head link; 218c2ecf20Sopenharmony_ci}; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic unsigned int dummy_index; 248c2ecf20Sopenharmony_cistatic struct dentry *regmap_debugfs_root; 258c2ecf20Sopenharmony_cistatic LIST_HEAD(regmap_debugfs_early_list); 268c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(regmap_debugfs_early_lock); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* Calculate the length of a fixed format */ 298c2ecf20Sopenharmony_cistatic size_t regmap_calc_reg_len(int max_val) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci return snprintf(NULL, 0, "%x", max_val); 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic ssize_t regmap_name_read_file(struct file *file, 358c2ecf20Sopenharmony_ci char __user *user_buf, size_t count, 368c2ecf20Sopenharmony_ci loff_t *ppos) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci struct regmap *map = file->private_data; 398c2ecf20Sopenharmony_ci const char *name = "nodev"; 408c2ecf20Sopenharmony_ci int ret; 418c2ecf20Sopenharmony_ci char *buf; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 448c2ecf20Sopenharmony_ci if (!buf) 458c2ecf20Sopenharmony_ci return -ENOMEM; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (map->dev && map->dev->driver) 488c2ecf20Sopenharmony_ci name = map->dev->driver->name; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci ret = snprintf(buf, PAGE_SIZE, "%s\n", name); 518c2ecf20Sopenharmony_ci if (ret >= PAGE_SIZE) { 528c2ecf20Sopenharmony_ci kfree(buf); 538c2ecf20Sopenharmony_ci return ret; 548c2ecf20Sopenharmony_ci } 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 578c2ecf20Sopenharmony_ci kfree(buf); 588c2ecf20Sopenharmony_ci return ret; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic const struct file_operations regmap_name_fops = { 628c2ecf20Sopenharmony_ci .open = simple_open, 638c2ecf20Sopenharmony_ci .read = regmap_name_read_file, 648c2ecf20Sopenharmony_ci .llseek = default_llseek, 658c2ecf20Sopenharmony_ci}; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic void regmap_debugfs_free_dump_cache(struct regmap *map) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct regmap_debugfs_off_cache *c; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci while (!list_empty(&map->debugfs_off_cache)) { 728c2ecf20Sopenharmony_ci c = list_first_entry(&map->debugfs_off_cache, 738c2ecf20Sopenharmony_ci struct regmap_debugfs_off_cache, 748c2ecf20Sopenharmony_ci list); 758c2ecf20Sopenharmony_ci list_del(&c->list); 768c2ecf20Sopenharmony_ci kfree(c); 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic bool regmap_printable(struct regmap *map, unsigned int reg) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci if (regmap_precious(map, reg)) 838c2ecf20Sopenharmony_ci return false; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (!regmap_readable(map, reg) && !regmap_cached(map, reg)) 868c2ecf20Sopenharmony_ci return false; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return true; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* 928c2ecf20Sopenharmony_ci * Work out where the start offset maps into register numbers, bearing 938c2ecf20Sopenharmony_ci * in mind that we suppress hidden registers. 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_cistatic unsigned int regmap_debugfs_get_dump_start(struct regmap *map, 968c2ecf20Sopenharmony_ci unsigned int base, 978c2ecf20Sopenharmony_ci loff_t from, 988c2ecf20Sopenharmony_ci loff_t *pos) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci struct regmap_debugfs_off_cache *c = NULL; 1018c2ecf20Sopenharmony_ci loff_t p = 0; 1028c2ecf20Sopenharmony_ci unsigned int i, ret; 1038c2ecf20Sopenharmony_ci unsigned int fpos_offset; 1048c2ecf20Sopenharmony_ci unsigned int reg_offset; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* Suppress the cache if we're using a subrange */ 1078c2ecf20Sopenharmony_ci if (base) 1088c2ecf20Sopenharmony_ci return base; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* 1118c2ecf20Sopenharmony_ci * If we don't have a cache build one so we don't have to do a 1128c2ecf20Sopenharmony_ci * linear scan each time. 1138c2ecf20Sopenharmony_ci */ 1148c2ecf20Sopenharmony_ci mutex_lock(&map->cache_lock); 1158c2ecf20Sopenharmony_ci i = base; 1168c2ecf20Sopenharmony_ci if (list_empty(&map->debugfs_off_cache)) { 1178c2ecf20Sopenharmony_ci for (; i <= map->max_register; i += map->reg_stride) { 1188c2ecf20Sopenharmony_ci /* Skip unprinted registers, closing off cache entry */ 1198c2ecf20Sopenharmony_ci if (!regmap_printable(map, i)) { 1208c2ecf20Sopenharmony_ci if (c) { 1218c2ecf20Sopenharmony_ci c->max = p - 1; 1228c2ecf20Sopenharmony_ci c->max_reg = i - map->reg_stride; 1238c2ecf20Sopenharmony_ci list_add_tail(&c->list, 1248c2ecf20Sopenharmony_ci &map->debugfs_off_cache); 1258c2ecf20Sopenharmony_ci c = NULL; 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci continue; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* No cache entry? Start a new one */ 1328c2ecf20Sopenharmony_ci if (!c) { 1338c2ecf20Sopenharmony_ci c = kzalloc(sizeof(*c), GFP_KERNEL); 1348c2ecf20Sopenharmony_ci if (!c) { 1358c2ecf20Sopenharmony_ci regmap_debugfs_free_dump_cache(map); 1368c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 1378c2ecf20Sopenharmony_ci return base; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci c->min = p; 1408c2ecf20Sopenharmony_ci c->base_reg = i; 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci p += map->debugfs_tot_len; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* Close the last entry off if we didn't scan beyond it */ 1488c2ecf20Sopenharmony_ci if (c) { 1498c2ecf20Sopenharmony_ci c->max = p - 1; 1508c2ecf20Sopenharmony_ci c->max_reg = i - map->reg_stride; 1518c2ecf20Sopenharmony_ci list_add_tail(&c->list, 1528c2ecf20Sopenharmony_ci &map->debugfs_off_cache); 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* 1568c2ecf20Sopenharmony_ci * This should never happen; we return above if we fail to 1578c2ecf20Sopenharmony_ci * allocate and we should never be in this code if there are 1588c2ecf20Sopenharmony_ci * no registers at all. 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_ci WARN_ON(list_empty(&map->debugfs_off_cache)); 1618c2ecf20Sopenharmony_ci ret = base; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* Find the relevant block:offset */ 1648c2ecf20Sopenharmony_ci list_for_each_entry(c, &map->debugfs_off_cache, list) { 1658c2ecf20Sopenharmony_ci if (from >= c->min && from <= c->max) { 1668c2ecf20Sopenharmony_ci fpos_offset = from - c->min; 1678c2ecf20Sopenharmony_ci reg_offset = fpos_offset / map->debugfs_tot_len; 1688c2ecf20Sopenharmony_ci *pos = c->min + (reg_offset * map->debugfs_tot_len); 1698c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 1708c2ecf20Sopenharmony_ci return c->base_reg + (reg_offset * map->reg_stride); 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci *pos = c->max; 1748c2ecf20Sopenharmony_ci ret = c->max_reg; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci return ret; 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic inline void regmap_calc_tot_len(struct regmap *map, 1828c2ecf20Sopenharmony_ci void *buf, size_t count) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci /* Calculate the length of a fixed format */ 1858c2ecf20Sopenharmony_ci if (!map->debugfs_tot_len) { 1868c2ecf20Sopenharmony_ci map->debugfs_reg_len = regmap_calc_reg_len(map->max_register); 1878c2ecf20Sopenharmony_ci map->debugfs_val_len = 2 * map->format.val_bytes; 1888c2ecf20Sopenharmony_ci map->debugfs_tot_len = map->debugfs_reg_len + 1898c2ecf20Sopenharmony_ci map->debugfs_val_len + 3; /* : \n */ 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic int regmap_next_readable_reg(struct regmap *map, int reg) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct regmap_debugfs_off_cache *c; 1968c2ecf20Sopenharmony_ci int ret = -EINVAL; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if (regmap_printable(map, reg + map->reg_stride)) { 1998c2ecf20Sopenharmony_ci ret = reg + map->reg_stride; 2008c2ecf20Sopenharmony_ci } else { 2018c2ecf20Sopenharmony_ci mutex_lock(&map->cache_lock); 2028c2ecf20Sopenharmony_ci list_for_each_entry(c, &map->debugfs_off_cache, list) { 2038c2ecf20Sopenharmony_ci if (reg > c->max_reg) 2048c2ecf20Sopenharmony_ci continue; 2058c2ecf20Sopenharmony_ci if (reg < c->base_reg) { 2068c2ecf20Sopenharmony_ci ret = c->base_reg; 2078c2ecf20Sopenharmony_ci break; 2088c2ecf20Sopenharmony_ci } 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci return ret; 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, 2168c2ecf20Sopenharmony_ci unsigned int to, char __user *user_buf, 2178c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci size_t buf_pos = 0; 2208c2ecf20Sopenharmony_ci loff_t p = *ppos; 2218c2ecf20Sopenharmony_ci ssize_t ret; 2228c2ecf20Sopenharmony_ci int i; 2238c2ecf20Sopenharmony_ci char *buf; 2248c2ecf20Sopenharmony_ci unsigned int val, start_reg; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci if (*ppos < 0 || !count) 2278c2ecf20Sopenharmony_ci return -EINVAL; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci if (count > (PAGE_SIZE << (MAX_ORDER - 1))) 2308c2ecf20Sopenharmony_ci count = PAGE_SIZE << (MAX_ORDER - 1); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci buf = kmalloc(count, GFP_KERNEL); 2338c2ecf20Sopenharmony_ci if (!buf) 2348c2ecf20Sopenharmony_ci return -ENOMEM; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci regmap_calc_tot_len(map, buf, count); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci /* Work out which register we're starting at */ 2398c2ecf20Sopenharmony_ci start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci for (i = start_reg; i >= 0 && i <= to; 2428c2ecf20Sopenharmony_ci i = regmap_next_readable_reg(map, i)) { 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* If we're in the region the user is trying to read */ 2458c2ecf20Sopenharmony_ci if (p >= *ppos) { 2468c2ecf20Sopenharmony_ci /* ...but not beyond it */ 2478c2ecf20Sopenharmony_ci if (buf_pos + map->debugfs_tot_len > count) 2488c2ecf20Sopenharmony_ci break; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci /* Format the register */ 2518c2ecf20Sopenharmony_ci snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", 2528c2ecf20Sopenharmony_ci map->debugfs_reg_len, i - from); 2538c2ecf20Sopenharmony_ci buf_pos += map->debugfs_reg_len + 2; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* Format the value, write all X if we can't read */ 2568c2ecf20Sopenharmony_ci ret = regmap_read(map, i, &val); 2578c2ecf20Sopenharmony_ci if (ret == 0) 2588c2ecf20Sopenharmony_ci snprintf(buf + buf_pos, count - buf_pos, 2598c2ecf20Sopenharmony_ci "%.*x", map->debugfs_val_len, val); 2608c2ecf20Sopenharmony_ci else 2618c2ecf20Sopenharmony_ci memset(buf + buf_pos, 'X', 2628c2ecf20Sopenharmony_ci map->debugfs_val_len); 2638c2ecf20Sopenharmony_ci buf_pos += 2 * map->format.val_bytes; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci buf[buf_pos++] = '\n'; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci p += map->debugfs_tot_len; 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci ret = buf_pos; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci if (copy_to_user(user_buf, buf, buf_pos)) { 2738c2ecf20Sopenharmony_ci ret = -EFAULT; 2748c2ecf20Sopenharmony_ci goto out; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci *ppos += buf_pos; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciout: 2808c2ecf20Sopenharmony_ci kfree(buf); 2818c2ecf20Sopenharmony_ci return ret; 2828c2ecf20Sopenharmony_ci} 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_cistatic ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, 2858c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci struct regmap *map = file->private_data; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci return regmap_read_debugfs(map, 0, map->max_register, user_buf, 2908c2ecf20Sopenharmony_ci count, ppos); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci#undef REGMAP_ALLOW_WRITE_DEBUGFS 2948c2ecf20Sopenharmony_ci#ifdef REGMAP_ALLOW_WRITE_DEBUGFS 2958c2ecf20Sopenharmony_ci/* 2968c2ecf20Sopenharmony_ci * This can be dangerous especially when we have clients such as 2978c2ecf20Sopenharmony_ci * PMICs, therefore don't provide any real compile time configuration option 2988c2ecf20Sopenharmony_ci * for this feature, people who want to use this will need to modify 2998c2ecf20Sopenharmony_ci * the source code directly. 3008c2ecf20Sopenharmony_ci */ 3018c2ecf20Sopenharmony_cistatic ssize_t regmap_map_write_file(struct file *file, 3028c2ecf20Sopenharmony_ci const char __user *user_buf, 3038c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci char buf[32]; 3068c2ecf20Sopenharmony_ci size_t buf_size; 3078c2ecf20Sopenharmony_ci char *start = buf; 3088c2ecf20Sopenharmony_ci unsigned long reg, value; 3098c2ecf20Sopenharmony_ci struct regmap *map = file->private_data; 3108c2ecf20Sopenharmony_ci int ret; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci buf_size = min(count, (sizeof(buf)-1)); 3138c2ecf20Sopenharmony_ci if (copy_from_user(buf, user_buf, buf_size)) 3148c2ecf20Sopenharmony_ci return -EFAULT; 3158c2ecf20Sopenharmony_ci buf[buf_size] = 0; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci while (*start == ' ') 3188c2ecf20Sopenharmony_ci start++; 3198c2ecf20Sopenharmony_ci reg = simple_strtoul(start, &start, 16); 3208c2ecf20Sopenharmony_ci while (*start == ' ') 3218c2ecf20Sopenharmony_ci start++; 3228c2ecf20Sopenharmony_ci if (kstrtoul(start, 16, &value)) 3238c2ecf20Sopenharmony_ci return -EINVAL; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci /* Userspace has been fiddling around behind the kernel's back */ 3268c2ecf20Sopenharmony_ci add_taint(TAINT_USER, LOCKDEP_STILL_OK); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci ret = regmap_write(map, reg, value); 3298c2ecf20Sopenharmony_ci if (ret < 0) 3308c2ecf20Sopenharmony_ci return ret; 3318c2ecf20Sopenharmony_ci return buf_size; 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci#else 3348c2ecf20Sopenharmony_ci#define regmap_map_write_file NULL 3358c2ecf20Sopenharmony_ci#endif 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_cistatic const struct file_operations regmap_map_fops = { 3388c2ecf20Sopenharmony_ci .open = simple_open, 3398c2ecf20Sopenharmony_ci .read = regmap_map_read_file, 3408c2ecf20Sopenharmony_ci .write = regmap_map_write_file, 3418c2ecf20Sopenharmony_ci .llseek = default_llseek, 3428c2ecf20Sopenharmony_ci}; 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, 3458c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct regmap_range_node *range = file->private_data; 3488c2ecf20Sopenharmony_ci struct regmap *map = range->map; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci return regmap_read_debugfs(map, range->range_min, range->range_max, 3518c2ecf20Sopenharmony_ci user_buf, count, ppos); 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic const struct file_operations regmap_range_fops = { 3558c2ecf20Sopenharmony_ci .open = simple_open, 3568c2ecf20Sopenharmony_ci .read = regmap_range_read_file, 3578c2ecf20Sopenharmony_ci .llseek = default_llseek, 3588c2ecf20Sopenharmony_ci}; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cistatic ssize_t regmap_reg_ranges_read_file(struct file *file, 3618c2ecf20Sopenharmony_ci char __user *user_buf, size_t count, 3628c2ecf20Sopenharmony_ci loff_t *ppos) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct regmap *map = file->private_data; 3658c2ecf20Sopenharmony_ci struct regmap_debugfs_off_cache *c; 3668c2ecf20Sopenharmony_ci loff_t p = 0; 3678c2ecf20Sopenharmony_ci size_t buf_pos = 0; 3688c2ecf20Sopenharmony_ci char *buf; 3698c2ecf20Sopenharmony_ci char *entry; 3708c2ecf20Sopenharmony_ci int ret; 3718c2ecf20Sopenharmony_ci unsigned entry_len; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci if (*ppos < 0 || !count) 3748c2ecf20Sopenharmony_ci return -EINVAL; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci if (count > (PAGE_SIZE << (MAX_ORDER - 1))) 3778c2ecf20Sopenharmony_ci count = PAGE_SIZE << (MAX_ORDER - 1); 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci buf = kmalloc(count, GFP_KERNEL); 3808c2ecf20Sopenharmony_ci if (!buf) 3818c2ecf20Sopenharmony_ci return -ENOMEM; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci entry = kmalloc(PAGE_SIZE, GFP_KERNEL); 3848c2ecf20Sopenharmony_ci if (!entry) { 3858c2ecf20Sopenharmony_ci kfree(buf); 3868c2ecf20Sopenharmony_ci return -ENOMEM; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci /* While we are at it, build the register dump cache 3908c2ecf20Sopenharmony_ci * now so the read() operation on the `registers' file 3918c2ecf20Sopenharmony_ci * can benefit from using the cache. We do not care 3928c2ecf20Sopenharmony_ci * about the file position information that is contained 3938c2ecf20Sopenharmony_ci * in the cache, just about the actual register blocks */ 3948c2ecf20Sopenharmony_ci regmap_calc_tot_len(map, buf, count); 3958c2ecf20Sopenharmony_ci regmap_debugfs_get_dump_start(map, 0, *ppos, &p); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci /* Reset file pointer as the fixed-format of the `registers' 3988c2ecf20Sopenharmony_ci * file is not compatible with the `range' file */ 3998c2ecf20Sopenharmony_ci p = 0; 4008c2ecf20Sopenharmony_ci mutex_lock(&map->cache_lock); 4018c2ecf20Sopenharmony_ci list_for_each_entry(c, &map->debugfs_off_cache, list) { 4028c2ecf20Sopenharmony_ci entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n", 4038c2ecf20Sopenharmony_ci c->base_reg, c->max_reg); 4048c2ecf20Sopenharmony_ci if (p >= *ppos) { 4058c2ecf20Sopenharmony_ci if (buf_pos + entry_len > count) 4068c2ecf20Sopenharmony_ci break; 4078c2ecf20Sopenharmony_ci memcpy(buf + buf_pos, entry, entry_len); 4088c2ecf20Sopenharmony_ci buf_pos += entry_len; 4098c2ecf20Sopenharmony_ci } 4108c2ecf20Sopenharmony_ci p += entry_len; 4118c2ecf20Sopenharmony_ci } 4128c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci kfree(entry); 4158c2ecf20Sopenharmony_ci ret = buf_pos; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci if (copy_to_user(user_buf, buf, buf_pos)) { 4188c2ecf20Sopenharmony_ci ret = -EFAULT; 4198c2ecf20Sopenharmony_ci goto out_buf; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci *ppos += buf_pos; 4238c2ecf20Sopenharmony_ciout_buf: 4248c2ecf20Sopenharmony_ci kfree(buf); 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci} 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_cistatic const struct file_operations regmap_reg_ranges_fops = { 4298c2ecf20Sopenharmony_ci .open = simple_open, 4308c2ecf20Sopenharmony_ci .read = regmap_reg_ranges_read_file, 4318c2ecf20Sopenharmony_ci .llseek = default_llseek, 4328c2ecf20Sopenharmony_ci}; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_cistatic int regmap_access_show(struct seq_file *s, void *ignored) 4358c2ecf20Sopenharmony_ci{ 4368c2ecf20Sopenharmony_ci struct regmap *map = s->private; 4378c2ecf20Sopenharmony_ci int i, reg_len; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci reg_len = regmap_calc_reg_len(map->max_register); 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci for (i = 0; i <= map->max_register; i += map->reg_stride) { 4428c2ecf20Sopenharmony_ci /* Ignore registers which are neither readable nor writable */ 4438c2ecf20Sopenharmony_ci if (!regmap_readable(map, i) && !regmap_writeable(map, i)) 4448c2ecf20Sopenharmony_ci continue; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci /* Format the register */ 4478c2ecf20Sopenharmony_ci seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i, 4488c2ecf20Sopenharmony_ci regmap_readable(map, i) ? 'y' : 'n', 4498c2ecf20Sopenharmony_ci regmap_writeable(map, i) ? 'y' : 'n', 4508c2ecf20Sopenharmony_ci regmap_volatile(map, i) ? 'y' : 'n', 4518c2ecf20Sopenharmony_ci regmap_precious(map, i) ? 'y' : 'n'); 4528c2ecf20Sopenharmony_ci } 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci return 0; 4558c2ecf20Sopenharmony_ci} 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(regmap_access); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_cistatic ssize_t regmap_cache_only_write_file(struct file *file, 4608c2ecf20Sopenharmony_ci const char __user *user_buf, 4618c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 4628c2ecf20Sopenharmony_ci{ 4638c2ecf20Sopenharmony_ci struct regmap *map = container_of(file->private_data, 4648c2ecf20Sopenharmony_ci struct regmap, cache_only); 4658c2ecf20Sopenharmony_ci bool new_val, require_sync = false; 4668c2ecf20Sopenharmony_ci int err; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci err = kstrtobool_from_user(user_buf, count, &new_val); 4698c2ecf20Sopenharmony_ci /* Ignore malforned data like debugfs_write_file_bool() */ 4708c2ecf20Sopenharmony_ci if (err) 4718c2ecf20Sopenharmony_ci return count; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci err = debugfs_file_get(file->f_path.dentry); 4748c2ecf20Sopenharmony_ci if (err) 4758c2ecf20Sopenharmony_ci return err; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci map->lock(map->lock_arg); 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci if (new_val && !map->cache_only) { 4808c2ecf20Sopenharmony_ci dev_warn(map->dev, "debugfs cache_only=Y forced\n"); 4818c2ecf20Sopenharmony_ci add_taint(TAINT_USER, LOCKDEP_STILL_OK); 4828c2ecf20Sopenharmony_ci } else if (!new_val && map->cache_only) { 4838c2ecf20Sopenharmony_ci dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n"); 4848c2ecf20Sopenharmony_ci require_sync = true; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci map->cache_only = new_val; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci map->unlock(map->lock_arg); 4898c2ecf20Sopenharmony_ci debugfs_file_put(file->f_path.dentry); 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci if (require_sync) { 4928c2ecf20Sopenharmony_ci err = regcache_sync(map); 4938c2ecf20Sopenharmony_ci if (err) 4948c2ecf20Sopenharmony_ci dev_err(map->dev, "Failed to sync cache %d\n", err); 4958c2ecf20Sopenharmony_ci } 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci return count; 4988c2ecf20Sopenharmony_ci} 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_cistatic const struct file_operations regmap_cache_only_fops = { 5018c2ecf20Sopenharmony_ci .open = simple_open, 5028c2ecf20Sopenharmony_ci .read = debugfs_read_file_bool, 5038c2ecf20Sopenharmony_ci .write = regmap_cache_only_write_file, 5048c2ecf20Sopenharmony_ci}; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_cistatic ssize_t regmap_cache_bypass_write_file(struct file *file, 5078c2ecf20Sopenharmony_ci const char __user *user_buf, 5088c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 5098c2ecf20Sopenharmony_ci{ 5108c2ecf20Sopenharmony_ci struct regmap *map = container_of(file->private_data, 5118c2ecf20Sopenharmony_ci struct regmap, cache_bypass); 5128c2ecf20Sopenharmony_ci bool new_val; 5138c2ecf20Sopenharmony_ci int err; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci err = kstrtobool_from_user(user_buf, count, &new_val); 5168c2ecf20Sopenharmony_ci /* Ignore malforned data like debugfs_write_file_bool() */ 5178c2ecf20Sopenharmony_ci if (err) 5188c2ecf20Sopenharmony_ci return count; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci err = debugfs_file_get(file->f_path.dentry); 5218c2ecf20Sopenharmony_ci if (err) 5228c2ecf20Sopenharmony_ci return err; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci map->lock(map->lock_arg); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci if (new_val && !map->cache_bypass) { 5278c2ecf20Sopenharmony_ci dev_warn(map->dev, "debugfs cache_bypass=Y forced\n"); 5288c2ecf20Sopenharmony_ci add_taint(TAINT_USER, LOCKDEP_STILL_OK); 5298c2ecf20Sopenharmony_ci } else if (!new_val && map->cache_bypass) { 5308c2ecf20Sopenharmony_ci dev_warn(map->dev, "debugfs cache_bypass=N forced\n"); 5318c2ecf20Sopenharmony_ci } 5328c2ecf20Sopenharmony_ci map->cache_bypass = new_val; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci map->unlock(map->lock_arg); 5358c2ecf20Sopenharmony_ci debugfs_file_put(file->f_path.dentry); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci return count; 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic const struct file_operations regmap_cache_bypass_fops = { 5418c2ecf20Sopenharmony_ci .open = simple_open, 5428c2ecf20Sopenharmony_ci .read = debugfs_read_file_bool, 5438c2ecf20Sopenharmony_ci .write = regmap_cache_bypass_write_file, 5448c2ecf20Sopenharmony_ci}; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_civoid regmap_debugfs_init(struct regmap *map) 5478c2ecf20Sopenharmony_ci{ 5488c2ecf20Sopenharmony_ci struct rb_node *next; 5498c2ecf20Sopenharmony_ci struct regmap_range_node *range_node; 5508c2ecf20Sopenharmony_ci const char *devname = "dummy"; 5518c2ecf20Sopenharmony_ci const char *name = map->name; 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci /* 5548c2ecf20Sopenharmony_ci * Userspace can initiate reads from the hardware over debugfs. 5558c2ecf20Sopenharmony_ci * Normally internal regmap structures and buffers are protected with 5568c2ecf20Sopenharmony_ci * a mutex or a spinlock, but if the regmap owner decided to disable 5578c2ecf20Sopenharmony_ci * all locking mechanisms, this is no longer the case. For safety: 5588c2ecf20Sopenharmony_ci * don't create the debugfs entries if locking is disabled. 5598c2ecf20Sopenharmony_ci */ 5608c2ecf20Sopenharmony_ci if (map->debugfs_disable) { 5618c2ecf20Sopenharmony_ci dev_dbg(map->dev, "regmap locking disabled - not creating debugfs entries\n"); 5628c2ecf20Sopenharmony_ci return; 5638c2ecf20Sopenharmony_ci } 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci /* If we don't have the debugfs root yet, postpone init */ 5668c2ecf20Sopenharmony_ci if (!regmap_debugfs_root) { 5678c2ecf20Sopenharmony_ci struct regmap_debugfs_node *node; 5688c2ecf20Sopenharmony_ci node = kzalloc(sizeof(*node), GFP_KERNEL); 5698c2ecf20Sopenharmony_ci if (!node) 5708c2ecf20Sopenharmony_ci return; 5718c2ecf20Sopenharmony_ci node->map = map; 5728c2ecf20Sopenharmony_ci mutex_lock(®map_debugfs_early_lock); 5738c2ecf20Sopenharmony_ci list_add(&node->link, ®map_debugfs_early_list); 5748c2ecf20Sopenharmony_ci mutex_unlock(®map_debugfs_early_lock); 5758c2ecf20Sopenharmony_ci return; 5768c2ecf20Sopenharmony_ci } 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&map->debugfs_off_cache); 5798c2ecf20Sopenharmony_ci mutex_init(&map->cache_lock); 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci if (map->dev) 5828c2ecf20Sopenharmony_ci devname = dev_name(map->dev); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci if (name) { 5858c2ecf20Sopenharmony_ci if (!map->debugfs_name) { 5868c2ecf20Sopenharmony_ci map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", 5878c2ecf20Sopenharmony_ci devname, name); 5888c2ecf20Sopenharmony_ci if (!map->debugfs_name) 5898c2ecf20Sopenharmony_ci return; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci name = map->debugfs_name; 5928c2ecf20Sopenharmony_ci } else { 5938c2ecf20Sopenharmony_ci name = devname; 5948c2ecf20Sopenharmony_ci } 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci if (!strcmp(name, "dummy")) { 5978c2ecf20Sopenharmony_ci kfree(map->debugfs_name); 5988c2ecf20Sopenharmony_ci map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d", 5998c2ecf20Sopenharmony_ci dummy_index); 6008c2ecf20Sopenharmony_ci if (!map->debugfs_name) 6018c2ecf20Sopenharmony_ci return; 6028c2ecf20Sopenharmony_ci name = map->debugfs_name; 6038c2ecf20Sopenharmony_ci dummy_index++; 6048c2ecf20Sopenharmony_ci } 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci debugfs_create_file("name", 0400, map->debugfs, 6098c2ecf20Sopenharmony_ci map, ®map_name_fops); 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci debugfs_create_file("range", 0400, map->debugfs, 6128c2ecf20Sopenharmony_ci map, ®map_reg_ranges_fops); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci if (map->max_register || regmap_readable(map, 0)) { 6158c2ecf20Sopenharmony_ci umode_t registers_mode; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci#if defined(REGMAP_ALLOW_WRITE_DEBUGFS) 6188c2ecf20Sopenharmony_ci registers_mode = 0600; 6198c2ecf20Sopenharmony_ci#else 6208c2ecf20Sopenharmony_ci registers_mode = 0400; 6218c2ecf20Sopenharmony_ci#endif 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci debugfs_create_file("registers", registers_mode, map->debugfs, 6248c2ecf20Sopenharmony_ci map, ®map_map_fops); 6258c2ecf20Sopenharmony_ci debugfs_create_file("access", 0400, map->debugfs, 6268c2ecf20Sopenharmony_ci map, ®map_access_fops); 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci if (map->cache_type) { 6308c2ecf20Sopenharmony_ci debugfs_create_file("cache_only", 0600, map->debugfs, 6318c2ecf20Sopenharmony_ci &map->cache_only, ®map_cache_only_fops); 6328c2ecf20Sopenharmony_ci debugfs_create_bool("cache_dirty", 0400, map->debugfs, 6338c2ecf20Sopenharmony_ci &map->cache_dirty); 6348c2ecf20Sopenharmony_ci debugfs_create_file("cache_bypass", 0600, map->debugfs, 6358c2ecf20Sopenharmony_ci &map->cache_bypass, 6368c2ecf20Sopenharmony_ci ®map_cache_bypass_fops); 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci next = rb_first(&map->range_tree); 6408c2ecf20Sopenharmony_ci while (next) { 6418c2ecf20Sopenharmony_ci range_node = rb_entry(next, struct regmap_range_node, node); 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci if (range_node->name) 6448c2ecf20Sopenharmony_ci debugfs_create_file(range_node->name, 0400, 6458c2ecf20Sopenharmony_ci map->debugfs, range_node, 6468c2ecf20Sopenharmony_ci ®map_range_fops); 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci next = rb_next(&range_node->node); 6498c2ecf20Sopenharmony_ci } 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci if (map->cache_ops && map->cache_ops->debugfs_init) 6528c2ecf20Sopenharmony_ci map->cache_ops->debugfs_init(map); 6538c2ecf20Sopenharmony_ci} 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_civoid regmap_debugfs_exit(struct regmap *map) 6568c2ecf20Sopenharmony_ci{ 6578c2ecf20Sopenharmony_ci if (map->debugfs) { 6588c2ecf20Sopenharmony_ci debugfs_remove_recursive(map->debugfs); 6598c2ecf20Sopenharmony_ci mutex_lock(&map->cache_lock); 6608c2ecf20Sopenharmony_ci regmap_debugfs_free_dump_cache(map); 6618c2ecf20Sopenharmony_ci mutex_unlock(&map->cache_lock); 6628c2ecf20Sopenharmony_ci kfree(map->debugfs_name); 6638c2ecf20Sopenharmony_ci map->debugfs_name = NULL; 6648c2ecf20Sopenharmony_ci } else { 6658c2ecf20Sopenharmony_ci struct regmap_debugfs_node *node, *tmp; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci mutex_lock(®map_debugfs_early_lock); 6688c2ecf20Sopenharmony_ci list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, 6698c2ecf20Sopenharmony_ci link) { 6708c2ecf20Sopenharmony_ci if (node->map == map) { 6718c2ecf20Sopenharmony_ci list_del(&node->link); 6728c2ecf20Sopenharmony_ci kfree(node); 6738c2ecf20Sopenharmony_ci } 6748c2ecf20Sopenharmony_ci } 6758c2ecf20Sopenharmony_ci mutex_unlock(®map_debugfs_early_lock); 6768c2ecf20Sopenharmony_ci } 6778c2ecf20Sopenharmony_ci} 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_civoid regmap_debugfs_initcall(void) 6808c2ecf20Sopenharmony_ci{ 6818c2ecf20Sopenharmony_ci struct regmap_debugfs_node *node, *tmp; 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci regmap_debugfs_root = debugfs_create_dir("regmap", NULL); 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci mutex_lock(®map_debugfs_early_lock); 6868c2ecf20Sopenharmony_ci list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) { 6878c2ecf20Sopenharmony_ci regmap_debugfs_init(node->map); 6888c2ecf20Sopenharmony_ci list_del(&node->link); 6898c2ecf20Sopenharmony_ci kfree(node); 6908c2ecf20Sopenharmony_ci } 6918c2ecf20Sopenharmony_ci mutex_unlock(®map_debugfs_early_lock); 6928c2ecf20Sopenharmony_ci} 693