ab8500-debugfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. char buf[32];
  404. int buf_size;
  405. unsigned long user_bank;
  406. int err;
  407. /* Get userspace string and assure termination */
  408. buf_size = min(count, (sizeof(buf) - 1));
  409. if (copy_from_user(buf, user_buf, buf_size))
  410. return -EFAULT;
  411. buf[buf_size] = 0;
  412. err = strict_strtoul(buf, 0, &user_bank);
  413. if (err)
  414. return -EINVAL;
  415. if (user_bank >= AB8500_NUM_BANKS) {
  416. dev_err(dev, "debugfs error input > number of banks\n");
  417. return -EINVAL;
  418. }
  419. debug_bank = user_bank;
  420. return buf_size;
  421. }
  422. static int ab8500_address_print(struct seq_file *s, void *p)
  423. {
  424. return seq_printf(s, "0x%02X\n", debug_address);
  425. }
  426. static int ab8500_address_open(struct inode *inode, struct file *file)
  427. {
  428. return single_open(file, ab8500_address_print, inode->i_private);
  429. }
  430. static ssize_t ab8500_address_write(struct file *file,
  431. const char __user *user_buf,
  432. size_t count, loff_t *ppos)
  433. {
  434. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  435. char buf[32];
  436. int buf_size;
  437. unsigned long user_address;
  438. int err;
  439. /* Get userspace string and assure termination */
  440. buf_size = min(count, (sizeof(buf) - 1));
  441. if (copy_from_user(buf, user_buf, buf_size))
  442. return -EFAULT;
  443. buf[buf_size] = 0;
  444. err = strict_strtoul(buf, 0, &user_address);
  445. if (err)
  446. return -EINVAL;
  447. if (user_address > 0xff) {
  448. dev_err(dev, "debugfs error input > 0xff\n");
  449. return -EINVAL;
  450. }
  451. debug_address = user_address;
  452. return buf_size;
  453. }
  454. static int ab8500_val_print(struct seq_file *s, void *p)
  455. {
  456. struct device *dev = s->private;
  457. int ret;
  458. u8 regvalue;
  459. ret = abx500_get_register_interruptible(dev,
  460. (u8)debug_bank, (u8)debug_address, &regvalue);
  461. if (ret < 0) {
  462. dev_err(dev, "abx500_get_reg fail %d, %d\n",
  463. ret, __LINE__);
  464. return -EINVAL;
  465. }
  466. seq_printf(s, "0x%02X\n", regvalue);
  467. return 0;
  468. }
  469. static int ab8500_val_open(struct inode *inode, struct file *file)
  470. {
  471. return single_open(file, ab8500_val_print, inode->i_private);
  472. }
  473. static ssize_t ab8500_val_write(struct file *file,
  474. const char __user *user_buf,
  475. size_t count, loff_t *ppos)
  476. {
  477. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  478. char buf[32];
  479. int buf_size;
  480. unsigned long user_val;
  481. int err;
  482. /* Get userspace string and assure termination */
  483. buf_size = min(count, (sizeof(buf)-1));
  484. if (copy_from_user(buf, user_buf, buf_size))
  485. return -EFAULT;
  486. buf[buf_size] = 0;
  487. err = strict_strtoul(buf, 0, &user_val);
  488. if (err)
  489. return -EINVAL;
  490. if (user_val > 0xff) {
  491. dev_err(dev, "debugfs error input > 0xff\n");
  492. return -EINVAL;
  493. }
  494. err = abx500_set_register_interruptible(dev,
  495. (u8)debug_bank, debug_address, (u8)user_val);
  496. if (err < 0) {
  497. printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
  498. return -EINVAL;
  499. }
  500. return buf_size;
  501. }
  502. static const struct file_operations ab8500_bank_fops = {
  503. .open = ab8500_bank_open,
  504. .write = ab8500_bank_write,
  505. .read = seq_read,
  506. .llseek = seq_lseek,
  507. .release = single_release,
  508. .owner = THIS_MODULE,
  509. };
  510. static const struct file_operations ab8500_address_fops = {
  511. .open = ab8500_address_open,
  512. .write = ab8500_address_write,
  513. .read = seq_read,
  514. .llseek = seq_lseek,
  515. .release = single_release,
  516. .owner = THIS_MODULE,
  517. };
  518. static const struct file_operations ab8500_val_fops = {
  519. .open = ab8500_val_open,
  520. .write = ab8500_val_write,
  521. .read = seq_read,
  522. .llseek = seq_lseek,
  523. .release = single_release,
  524. .owner = THIS_MODULE,
  525. };
  526. static struct dentry *ab8500_dir;
  527. static struct dentry *ab8500_reg_file;
  528. static struct dentry *ab8500_bank_file;
  529. static struct dentry *ab8500_address_file;
  530. static struct dentry *ab8500_val_file;
  531. static int __devinit ab8500_debug_probe(struct platform_device *plf)
  532. {
  533. debug_bank = AB8500_MISC;
  534. debug_address = AB8500_REV_REG & 0x00FF;
  535. ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
  536. if (!ab8500_dir)
  537. goto exit_no_debugfs;
  538. ab8500_reg_file = debugfs_create_file("all-bank-registers",
  539. S_IRUGO, ab8500_dir, &plf->dev, &ab8500_registers_fops);
  540. if (!ab8500_reg_file)
  541. goto exit_destroy_dir;
  542. ab8500_bank_file = debugfs_create_file("register-bank",
  543. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_bank_fops);
  544. if (!ab8500_bank_file)
  545. goto exit_destroy_reg;
  546. ab8500_address_file = debugfs_create_file("register-address",
  547. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev,
  548. &ab8500_address_fops);
  549. if (!ab8500_address_file)
  550. goto exit_destroy_bank;
  551. ab8500_val_file = debugfs_create_file("register-value",
  552. (S_IRUGO | S_IWUSR), ab8500_dir, &plf->dev, &ab8500_val_fops);
  553. if (!ab8500_val_file)
  554. goto exit_destroy_address;
  555. return 0;
  556. exit_destroy_address:
  557. debugfs_remove(ab8500_address_file);
  558. exit_destroy_bank:
  559. debugfs_remove(ab8500_bank_file);
  560. exit_destroy_reg:
  561. debugfs_remove(ab8500_reg_file);
  562. exit_destroy_dir:
  563. debugfs_remove(ab8500_dir);
  564. exit_no_debugfs:
  565. dev_err(&plf->dev, "failed to create debugfs entries.\n");
  566. return -ENOMEM;
  567. }
  568. static int __devexit ab8500_debug_remove(struct platform_device *plf)
  569. {
  570. debugfs_remove(ab8500_val_file);
  571. debugfs_remove(ab8500_address_file);
  572. debugfs_remove(ab8500_bank_file);
  573. debugfs_remove(ab8500_reg_file);
  574. debugfs_remove(ab8500_dir);
  575. return 0;
  576. }
  577. static struct platform_driver ab8500_debug_driver = {
  578. .driver = {
  579. .name = "ab8500-debug",
  580. .owner = THIS_MODULE,
  581. },
  582. .probe = ab8500_debug_probe,
  583. .remove = __devexit_p(ab8500_debug_remove)
  584. };
  585. static int __init ab8500_debug_init(void)
  586. {
  587. return platform_driver_register(&ab8500_debug_driver);
  588. }
  589. static void __exit ab8500_debug_exit(void)
  590. {
  591. platform_driver_unregister(&ab8500_debug_driver);
  592. }
  593. subsys_initcall(ab8500_debug_init);
  594. module_exit(ab8500_debug_exit);
  595. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  596. MODULE_DESCRIPTION("AB8500 DEBUG");
  597. MODULE_LICENSE("GPL v2");