ab8500-debugfs.c 12 KB

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