regmap-debugfs.c 13 KB

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