ab8500-debugfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
  5. * License Terms: GNU General Public License v2
  6. */
  7. #include <linux/seq_file.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/fs.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mfd/abx500.h>
  13. #include <linux/mfd/ab8500.h>
  14. static u32 debug_bank;
  15. static u32 debug_address;
  16. /**
  17. * struct ab8500_reg_range
  18. * @first: the first address of the range
  19. * @last: the last address of the range
  20. * @perm: access permissions for the range
  21. */
  22. struct ab8500_reg_range {
  23. u8 first;
  24. u8 last;
  25. u8 perm;
  26. };
  27. /**
  28. * struct ab8500_i2c_ranges
  29. * @num_ranges: the number of ranges in the list
  30. * @bankid: bank identifier
  31. * @range: the list of register ranges
  32. */
  33. struct ab8500_i2c_ranges {
  34. u8 num_ranges;
  35. u8 bankid;
  36. const struct ab8500_reg_range *range;
  37. };
  38. #define AB8500_NAME_STRING "ab8500"
  39. #define AB8500_NUM_BANKS 22
  40. #define AB8500_REV_REG 0x80
  41. static struct ab8500_i2c_ranges debug_ranges[AB8500_NUM_BANKS] = {
  42. [0x0] = {
  43. .num_ranges = 0,
  44. .range = 0,
  45. },
  46. [AB8500_SYS_CTRL1_BLOCK] = {
  47. .num_ranges = 3,
  48. .range = (struct ab8500_reg_range[]) {
  49. {
  50. .first = 0x00,
  51. .last = 0x02,
  52. },
  53. {
  54. .first = 0x42,
  55. .last = 0x42,
  56. },
  57. {
  58. .first = 0x80,
  59. .last = 0x81,
  60. },
  61. },
  62. },
  63. [AB8500_SYS_CTRL2_BLOCK] = {
  64. .num_ranges = 4,
  65. .range = (struct ab8500_reg_range[]) {
  66. {
  67. .first = 0x00,
  68. .last = 0x0D,
  69. },
  70. {
  71. .first = 0x0F,
  72. .last = 0x17,
  73. },
  74. {
  75. .first = 0x30,
  76. .last = 0x30,
  77. },
  78. {
  79. .first = 0x32,
  80. .last = 0x33,
  81. },
  82. },
  83. },
  84. [AB8500_REGU_CTRL1] = {
  85. .num_ranges = 3,
  86. .range = (struct ab8500_reg_range[]) {
  87. {
  88. .first = 0x00,
  89. .last = 0x00,
  90. },
  91. {
  92. .first = 0x03,
  93. .last = 0x10,
  94. },
  95. {
  96. .first = 0x80,
  97. .last = 0x84,
  98. },
  99. },
  100. },
  101. [AB8500_REGU_CTRL2] = {
  102. .num_ranges = 5,
  103. .range = (struct ab8500_reg_range[]) {
  104. {
  105. .first = 0x00,
  106. .last = 0x15,
  107. },
  108. {
  109. .first = 0x17,
  110. .last = 0x19,
  111. },
  112. {
  113. .first = 0x1B,
  114. .last = 0x1D,
  115. },
  116. {
  117. .first = 0x1F,
  118. .last = 0x22,
  119. },
  120. {
  121. .first = 0x40,
  122. .last = 0x44,
  123. },
  124. /* 0x80-0x8B is SIM registers and should
  125. * not be accessed from here */
  126. },
  127. },
  128. [AB8500_USB] = {
  129. .num_ranges = 2,
  130. .range = (struct ab8500_reg_range[]) {
  131. {
  132. .first = 0x80,
  133. .last = 0x83,
  134. },
  135. {
  136. .first = 0x87,
  137. .last = 0x8A,
  138. },
  139. },
  140. },
  141. [AB8500_TVOUT] = {
  142. .num_ranges = 9,
  143. .range = (struct ab8500_reg_range[]) {
  144. {
  145. .first = 0x00,
  146. .last = 0x12,
  147. },
  148. {
  149. .first = 0x15,
  150. .last = 0x17,
  151. },
  152. {
  153. .first = 0x19,
  154. .last = 0x21,
  155. },
  156. {
  157. .first = 0x27,
  158. .last = 0x2C,
  159. },
  160. {
  161. .first = 0x41,
  162. .last = 0x41,
  163. },
  164. {
  165. .first = 0x45,
  166. .last = 0x5B,
  167. },
  168. {
  169. .first = 0x5D,
  170. .last = 0x5D,
  171. },
  172. {
  173. .first = 0x69,
  174. .last = 0x69,
  175. },
  176. {
  177. .first = 0x80,
  178. .last = 0x81,
  179. },
  180. },
  181. },
  182. [AB8500_DBI] = {
  183. .num_ranges = 0,
  184. .range = NULL,
  185. },
  186. [AB8500_ECI_AV_ACC] = {
  187. .num_ranges = 1,
  188. .range = (struct ab8500_reg_range[]) {
  189. {
  190. .first = 0x80,
  191. .last = 0x82,
  192. },
  193. },
  194. },
  195. [0x9] = {
  196. .num_ranges = 0,
  197. .range = NULL,
  198. },
  199. [AB8500_GPADC] = {
  200. .num_ranges = 1,
  201. .range = (struct ab8500_reg_range[]) {
  202. {
  203. .first = 0x00,
  204. .last = 0x08,
  205. },
  206. },
  207. },
  208. [AB8500_CHARGER] = {
  209. .num_ranges = 8,
  210. .range = (struct ab8500_reg_range[]) {
  211. {
  212. .first = 0x00,
  213. .last = 0x03,
  214. },
  215. {
  216. .first = 0x05,
  217. .last = 0x05,
  218. },
  219. {
  220. .first = 0x40,
  221. .last = 0x40,
  222. },
  223. {
  224. .first = 0x42,
  225. .last = 0x42,
  226. },
  227. {
  228. .first = 0x44,
  229. .last = 0x44,
  230. },
  231. {
  232. .first = 0x50,
  233. .last = 0x55,
  234. },
  235. {
  236. .first = 0x80,
  237. .last = 0x82,
  238. },
  239. {
  240. .first = 0xC0,
  241. .last = 0xC2,
  242. },
  243. },
  244. },
  245. [AB8500_GAS_GAUGE] = {
  246. .num_ranges = 3,
  247. .range = (struct ab8500_reg_range[]) {
  248. {
  249. .first = 0x00,
  250. .last = 0x00,
  251. },
  252. {
  253. .first = 0x07,
  254. .last = 0x0A,
  255. },
  256. {
  257. .first = 0x10,
  258. .last = 0x14,
  259. },
  260. },
  261. },
  262. [AB8500_AUDIO] = {
  263. .num_ranges = 1,
  264. .range = (struct ab8500_reg_range[]) {
  265. {
  266. .first = 0x00,
  267. .last = 0x6F,
  268. },
  269. },
  270. },
  271. [AB8500_INTERRUPT] = {
  272. .num_ranges = 0,
  273. .range = NULL,
  274. },
  275. [AB8500_RTC] = {
  276. .num_ranges = 1,
  277. .range = (struct ab8500_reg_range[]) {
  278. {
  279. .first = 0x00,
  280. .last = 0x0F,
  281. },
  282. },
  283. },
  284. [AB8500_MISC] = {
  285. .num_ranges = 8,
  286. .range = (struct ab8500_reg_range[]) {
  287. {
  288. .first = 0x00,
  289. .last = 0x05,
  290. },
  291. {
  292. .first = 0x10,
  293. .last = 0x15,
  294. },
  295. {
  296. .first = 0x20,
  297. .last = 0x25,
  298. },
  299. {
  300. .first = 0x30,
  301. .last = 0x35,
  302. },
  303. {
  304. .first = 0x40,
  305. .last = 0x45,
  306. },
  307. {
  308. .first = 0x50,
  309. .last = 0x50,
  310. },
  311. {
  312. .first = 0x60,
  313. .last = 0x67,
  314. },
  315. {
  316. .first = 0x80,
  317. .last = 0x80,
  318. },
  319. },
  320. },
  321. [0x11] = {
  322. .num_ranges = 0,
  323. .range = NULL,
  324. },
  325. [0x12] = {
  326. .num_ranges = 0,
  327. .range = NULL,
  328. },
  329. [0x13] = {
  330. .num_ranges = 0,
  331. .range = NULL,
  332. },
  333. [0x14] = {
  334. .num_ranges = 0,
  335. .range = NULL,
  336. },
  337. [AB8500_OTP_EMUL] = {
  338. .num_ranges = 1,
  339. .range = (struct ab8500_reg_range[]) {
  340. {
  341. .first = 0x01,
  342. .last = 0x0F,
  343. },
  344. },
  345. },
  346. };
  347. static int ab8500_registers_print(struct seq_file *s, void *p)
  348. {
  349. struct device *dev = s->private;
  350. unsigned int i;
  351. u32 bank = debug_bank;
  352. seq_printf(s, AB8500_NAME_STRING " register values:\n");
  353. seq_printf(s, " bank %u:\n", bank);
  354. for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
  355. u32 reg;
  356. for (reg = debug_ranges[bank].range[i].first;
  357. reg <= debug_ranges[bank].range[i].last;
  358. reg++) {
  359. u8 value;
  360. int err;
  361. err = abx500_get_register_interruptible(dev,
  362. (u8)bank, (u8)reg, &value);
  363. if (err < 0) {
  364. dev_err(dev, "ab->read fail %d\n", err);
  365. return err;
  366. }
  367. err = seq_printf(s, " [%u/0x%02X]: 0x%02X\n", bank,
  368. reg, value);
  369. if (err < 0) {
  370. dev_err(dev, "seq_printf overflow\n");
  371. /* Error is not returned here since
  372. * the output is wanted in any case */
  373. return 0;
  374. }
  375. }
  376. }
  377. return 0;
  378. }
  379. static int ab8500_registers_open(struct inode *inode, struct file *file)
  380. {
  381. return single_open(file, ab8500_registers_print, inode->i_private);
  382. }
  383. static const struct file_operations ab8500_registers_fops = {
  384. .open = ab8500_registers_open,
  385. .read = seq_read,
  386. .llseek = seq_lseek,
  387. .release = single_release,
  388. .owner = THIS_MODULE,
  389. };
  390. static int ab8500_bank_print(struct seq_file *s, void *p)
  391. {
  392. return seq_printf(s, "%d\n", debug_bank);
  393. }
  394. static int ab8500_bank_open(struct inode *inode, struct file *file)
  395. {
  396. return single_open(file, ab8500_bank_print, inode->i_private);
  397. }
  398. static ssize_t ab8500_bank_write(struct file *file,
  399. const char __user *user_buf,
  400. size_t count, loff_t *ppos)
  401. {
  402. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  403. unsigned long user_bank;
  404. int err;
  405. /* Get userspace string and assure termination */
  406. err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
  407. if (err)
  408. return err;
  409. if (user_bank >= AB8500_NUM_BANKS) {
  410. dev_err(dev, "debugfs error input > number of banks\n");
  411. return -EINVAL;
  412. }
  413. debug_bank = user_bank;
  414. return count;
  415. }
  416. static int ab8500_address_print(struct seq_file *s, void *p)
  417. {
  418. return seq_printf(s, "0x%02X\n", debug_address);
  419. }
  420. static int ab8500_address_open(struct inode *inode, struct file *file)
  421. {
  422. return single_open(file, ab8500_address_print, inode->i_private);
  423. }
  424. static ssize_t ab8500_address_write(struct file *file,
  425. const char __user *user_buf,
  426. size_t count, loff_t *ppos)
  427. {
  428. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  429. unsigned long user_address;
  430. int err;
  431. /* Get userspace string and assure termination */
  432. err = kstrtoul_from_user(user_buf, count, 0, &user_address);
  433. if (err)
  434. return err;
  435. if (user_address > 0xff) {
  436. dev_err(dev, "debugfs error input > 0xff\n");
  437. return -EINVAL;
  438. }
  439. debug_address = user_address;
  440. return count;
  441. }
  442. static int ab8500_val_print(struct seq_file *s, void *p)
  443. {
  444. struct device *dev = s->private;
  445. int ret;
  446. u8 regvalue;
  447. ret = abx500_get_register_interruptible(dev,
  448. (u8)debug_bank, (u8)debug_address, &regvalue);
  449. if (ret < 0) {
  450. dev_err(dev, "abx500_get_reg fail %d, %d\n",
  451. ret, __LINE__);
  452. return -EINVAL;
  453. }
  454. seq_printf(s, "0x%02X\n", regvalue);
  455. return 0;
  456. }
  457. static int ab8500_val_open(struct inode *inode, struct file *file)
  458. {
  459. return single_open(file, ab8500_val_print, inode->i_private);
  460. }
  461. static ssize_t ab8500_val_write(struct file *file,
  462. const char __user *user_buf,
  463. size_t count, loff_t *ppos)
  464. {
  465. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  466. unsigned long user_val;
  467. int err;
  468. /* Get userspace string and assure termination */
  469. err = kstrtoul_from_user(user_buf, count, 0, &user_val);
  470. if (err)
  471. return err;
  472. if (user_val > 0xff) {
  473. dev_err(dev, "debugfs error input > 0xff\n");
  474. return -EINVAL;
  475. }
  476. err = abx500_set_register_interruptible(dev,
  477. (u8)debug_bank, debug_address, (u8)user_val);
  478. if (err < 0) {
  479. printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
  480. return -EINVAL;
  481. }
  482. return count;
  483. }
  484. static const struct file_operations ab8500_bank_fops = {
  485. .open = ab8500_bank_open,
  486. .write = ab8500_bank_write,
  487. .read = seq_read,
  488. .llseek = seq_lseek,
  489. .release = single_release,
  490. .owner = THIS_MODULE,
  491. };
  492. static const struct file_operations ab8500_address_fops = {
  493. .open = ab8500_address_open,
  494. .write = ab8500_address_write,
  495. .read = seq_read,
  496. .llseek = seq_lseek,
  497. .release = single_release,
  498. .owner = THIS_MODULE,
  499. };
  500. static const struct file_operations ab8500_val_fops = {
  501. .open = ab8500_val_open,
  502. .write = ab8500_val_write,
  503. .read = seq_read,
  504. .llseek = seq_lseek,
  505. .release = single_release,
  506. .owner = THIS_MODULE,
  507. };
  508. static struct dentry *ab8500_dir;
  509. static struct dentry *ab8500_reg_file;
  510. static struct dentry *ab8500_bank_file;
  511. static struct dentry *ab8500_address_file;
  512. static struct dentry *ab8500_val_file;
  513. static int __devinit ab8500_debug_probe(struct platform_device *plf)
  514. {
  515. debug_bank = AB8500_MISC;
  516. debug_address = AB8500_REV_REG & 0x00FF;
  517. ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
  518. if (!ab8500_dir)
  519. goto exit_no_debugfs;
  520. ab8500_reg_file = debugfs_create_file("all-bank-registers",
  521. S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops);
  522. if (!ab8500_reg_file)
  523. goto exit_destroy_dir;
  524. ab8500_bank_file = debugfs_create_file("register-bank",
  525. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops);
  526. if (!ab8500_bank_file)
  527. goto exit_destroy_reg;
  528. ab8500_address_file = debugfs_create_file("register-address",
  529. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev,
  530. &ab8500_address_fops);
  531. if (!ab8500_address_file)
  532. goto exit_destroy_bank;
  533. ab8500_val_file = debugfs_create_file("register-value",
  534. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops);
  535. if (!ab8500_val_file)
  536. goto exit_destroy_address;
  537. return 0;
  538. exit_destroy_address:
  539. debugfs_remove(ab8500_address_file);
  540. exit_destroy_bank:
  541. debugfs_remove(ab8500_bank_file);
  542. exit_destroy_reg:
  543. debugfs_remove(ab8500_reg_file);
  544. exit_destroy_dir:
  545. debugfs_remove(ab8500_dir);
  546. exit_no_debugfs:
  547. dev_err(&plf->dev, "failed to create debugfs entries.\n");
  548. return -ENOMEM;
  549. }
  550. static int __devexit ab8500_debug_remove(struct platform_device *plf)
  551. {
  552. debugfs_remove(ab8500_val_file);
  553. debugfs_remove(ab8500_address_file);
  554. debugfs_remove(ab8500_bank_file);
  555. debugfs_remove(ab8500_reg_file);
  556. debugfs_remove(ab8500_dir);
  557. return 0;
  558. }
  559. static struct platform_driver ab8500_debug_driver = {
  560. .driver = {
  561. .name = "ab8500-debug",
  562. .owner = THIS_MODULE,
  563. },
  564. .probe = ab8500_debug_probe,
  565. .remove = __devexit_p(ab8500_debug_remove)
  566. };
  567. static int __init ab8500_debug_init(void)
  568. {
  569. return platform_driver_register(&ab8500_debug_driver);
  570. }
  571. static void __exit ab8500_debug_exit(void)
  572. {
  573. platform_driver_unregister(&ab8500_debug_driver);
  574. }
  575. subsys_initcall(ab8500_debug_init);
  576. module_exit(ab8500_debug_exit);
  577. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  578. MODULE_DESCRIPTION("AB8500 DEBUG");
  579. MODULE_LICENSE("GPL v2");