ab8500-debugfs.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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. /*
  8. * AB8500 register access
  9. * ======================
  10. *
  11. * read:
  12. * # echo BANK > <debugfs>/ab8500/register-bank
  13. * # echo ADDR > <debugfs>/ab8500/register-address
  14. * # cat <debugfs>/ab8500/register-value
  15. *
  16. * write:
  17. * # echo BANK > <debugfs>/ab8500/register-bank
  18. * # echo ADDR > <debugfs>/ab8500/register-address
  19. * # echo VALUE > <debugfs>/ab8500/register-value
  20. *
  21. * read all registers from a bank:
  22. * # echo BANK > <debugfs>/ab8500/register-bank
  23. * # cat <debugfs>/ab8500/all-bank-register
  24. *
  25. * BANK target AB8500 register bank
  26. * ADDR target AB8500 register address
  27. * VALUE decimal or 0x-prefixed hexadecimal
  28. *
  29. *
  30. * User Space notification on AB8500 IRQ
  31. * =====================================
  32. *
  33. * Allows user space entity to be notified when target AB8500 IRQ occurs.
  34. * When subscribed, a sysfs entry is created in ab8500.i2c platform device.
  35. * One can pool this file to get target IRQ occurence information.
  36. *
  37. * subscribe to an AB8500 IRQ:
  38. * # echo IRQ > <debugfs>/ab8500/irq-subscribe
  39. *
  40. * unsubscribe from an AB8500 IRQ:
  41. * # echo IRQ > <debugfs>/ab8500/irq-unsubscribe
  42. *
  43. *
  44. * AB8500 register formated read/write access
  45. * ==========================================
  46. *
  47. * Read: read data, data>>SHIFT, data&=MASK, output data
  48. * [0xABCDEF98] shift=12 mask=0xFFF => 0x00000CDE
  49. * Write: read data, data &= ~(MASK<<SHIFT), data |= (VALUE<<SHIFT), write data
  50. * [0xABCDEF98] shift=12 mask=0xFFF value=0x123 => [0xAB123F98]
  51. *
  52. * Usage:
  53. * # echo "CMD [OPTIONS] BANK ADRESS [VALUE]" > $debugfs/ab8500/hwreg
  54. *
  55. * CMD read read access
  56. * write write access
  57. *
  58. * BANK target reg bank
  59. * ADDRESS target reg address
  60. * VALUE (write) value to be updated
  61. *
  62. * OPTIONS
  63. * -d|-dec (read) output in decimal
  64. * -h|-hexa (read) output in 0x-hexa (default)
  65. * -l|-w|-b 32bit (default), 16bit or 8bit reg access
  66. * -m|-mask MASK 0x-hexa mask (default 0xFFFFFFFF)
  67. * -s|-shift SHIFT bit shift value (read:left, write:right)
  68. * -o|-offset OFFSET address offset to add to ADDRESS value
  69. *
  70. * Warning: bit shift operation is applied to bit-mask.
  71. * Warning: bit shift direction depends on read or right command.
  72. */
  73. #include <linux/seq_file.h>
  74. #include <linux/uaccess.h>
  75. #include <linux/fs.h>
  76. #include <linux/module.h>
  77. #include <linux/debugfs.h>
  78. #include <linux/platform_device.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/kobject.h>
  81. #include <linux/slab.h>
  82. #include <linux/mfd/abx500.h>
  83. #include <linux/mfd/abx500/ab8500.h>
  84. #include <linux/mfd/abx500/ab8500-gpadc.h>
  85. #ifdef CONFIG_DEBUG_FS
  86. #include <linux/string.h>
  87. #include <linux/ctype.h>
  88. #endif
  89. static u32 debug_bank;
  90. static u32 debug_address;
  91. static int irq_first;
  92. static int irq_last;
  93. static u32 *irq_count;
  94. static int num_irqs;
  95. static struct device_attribute **dev_attr;
  96. static char **event_name;
  97. /**
  98. * struct ab8500_reg_range
  99. * @first: the first address of the range
  100. * @last: the last address of the range
  101. * @perm: access permissions for the range
  102. */
  103. struct ab8500_reg_range {
  104. u8 first;
  105. u8 last;
  106. u8 perm;
  107. };
  108. /**
  109. * struct ab8500_prcmu_ranges
  110. * @num_ranges: the number of ranges in the list
  111. * @bankid: bank identifier
  112. * @range: the list of register ranges
  113. */
  114. struct ab8500_prcmu_ranges {
  115. u8 num_ranges;
  116. u8 bankid;
  117. const struct ab8500_reg_range *range;
  118. };
  119. /* hwreg- "mask" and "shift" entries ressources */
  120. struct hwreg_cfg {
  121. u32 bank; /* target bank */
  122. u32 addr; /* target address */
  123. uint fmt; /* format */
  124. uint mask; /* read/write mask, applied before any bit shift */
  125. int shift; /* bit shift (read:right shift, write:left shift */
  126. };
  127. /* fmt bit #0: 0=hexa, 1=dec */
  128. #define REG_FMT_DEC(c) ((c)->fmt & 0x1)
  129. #define REG_FMT_HEX(c) (!REG_FMT_DEC(c))
  130. static struct hwreg_cfg hwreg_cfg = {
  131. .addr = 0, /* default: invalid phys addr */
  132. .fmt = 0, /* default: 32bit access, hex output */
  133. .mask = 0xFFFFFFFF, /* default: no mask */
  134. .shift = 0, /* default: no bit shift */
  135. };
  136. #define AB8500_NAME_STRING "ab8500"
  137. #define AB8500_ADC_NAME_STRING "gpadc"
  138. #define AB8500_NUM_BANKS 24
  139. #define AB8500_REV_REG 0x80
  140. static struct ab8500_prcmu_ranges debug_ranges[AB8500_NUM_BANKS] = {
  141. [0x0] = {
  142. .num_ranges = 0,
  143. .range = NULL,
  144. },
  145. [AB8500_SYS_CTRL1_BLOCK] = {
  146. .num_ranges = 3,
  147. .range = (struct ab8500_reg_range[]) {
  148. {
  149. .first = 0x00,
  150. .last = 0x02,
  151. },
  152. {
  153. .first = 0x42,
  154. .last = 0x42,
  155. },
  156. {
  157. .first = 0x80,
  158. .last = 0x81,
  159. },
  160. },
  161. },
  162. [AB8500_SYS_CTRL2_BLOCK] = {
  163. .num_ranges = 4,
  164. .range = (struct ab8500_reg_range[]) {
  165. {
  166. .first = 0x00,
  167. .last = 0x0D,
  168. },
  169. {
  170. .first = 0x0F,
  171. .last = 0x17,
  172. },
  173. {
  174. .first = 0x30,
  175. .last = 0x30,
  176. },
  177. {
  178. .first = 0x32,
  179. .last = 0x33,
  180. },
  181. },
  182. },
  183. [AB8500_REGU_CTRL1] = {
  184. .num_ranges = 3,
  185. .range = (struct ab8500_reg_range[]) {
  186. {
  187. .first = 0x00,
  188. .last = 0x00,
  189. },
  190. {
  191. .first = 0x03,
  192. .last = 0x10,
  193. },
  194. {
  195. .first = 0x80,
  196. .last = 0x84,
  197. },
  198. },
  199. },
  200. [AB8500_REGU_CTRL2] = {
  201. .num_ranges = 5,
  202. .range = (struct ab8500_reg_range[]) {
  203. {
  204. .first = 0x00,
  205. .last = 0x15,
  206. },
  207. {
  208. .first = 0x17,
  209. .last = 0x19,
  210. },
  211. {
  212. .first = 0x1B,
  213. .last = 0x1D,
  214. },
  215. {
  216. .first = 0x1F,
  217. .last = 0x22,
  218. },
  219. {
  220. .first = 0x40,
  221. .last = 0x44,
  222. },
  223. /* 0x80-0x8B is SIM registers and should
  224. * not be accessed from here */
  225. },
  226. },
  227. [AB8500_USB] = {
  228. .num_ranges = 2,
  229. .range = (struct ab8500_reg_range[]) {
  230. {
  231. .first = 0x80,
  232. .last = 0x83,
  233. },
  234. {
  235. .first = 0x87,
  236. .last = 0x8A,
  237. },
  238. },
  239. },
  240. [AB8500_TVOUT] = {
  241. .num_ranges = 9,
  242. .range = (struct ab8500_reg_range[]) {
  243. {
  244. .first = 0x00,
  245. .last = 0x12,
  246. },
  247. {
  248. .first = 0x15,
  249. .last = 0x17,
  250. },
  251. {
  252. .first = 0x19,
  253. .last = 0x21,
  254. },
  255. {
  256. .first = 0x27,
  257. .last = 0x2C,
  258. },
  259. {
  260. .first = 0x41,
  261. .last = 0x41,
  262. },
  263. {
  264. .first = 0x45,
  265. .last = 0x5B,
  266. },
  267. {
  268. .first = 0x5D,
  269. .last = 0x5D,
  270. },
  271. {
  272. .first = 0x69,
  273. .last = 0x69,
  274. },
  275. {
  276. .first = 0x80,
  277. .last = 0x81,
  278. },
  279. },
  280. },
  281. [AB8500_DBI] = {
  282. .num_ranges = 0,
  283. .range = NULL,
  284. },
  285. [AB8500_ECI_AV_ACC] = {
  286. .num_ranges = 1,
  287. .range = (struct ab8500_reg_range[]) {
  288. {
  289. .first = 0x80,
  290. .last = 0x82,
  291. },
  292. },
  293. },
  294. [0x9] = {
  295. .num_ranges = 0,
  296. .range = NULL,
  297. },
  298. [AB8500_GPADC] = {
  299. .num_ranges = 1,
  300. .range = (struct ab8500_reg_range[]) {
  301. {
  302. .first = 0x00,
  303. .last = 0x08,
  304. },
  305. },
  306. },
  307. [AB8500_CHARGER] = {
  308. .num_ranges = 9,
  309. .range = (struct ab8500_reg_range[]) {
  310. {
  311. .first = 0x00,
  312. .last = 0x03,
  313. },
  314. {
  315. .first = 0x05,
  316. .last = 0x05,
  317. },
  318. {
  319. .first = 0x40,
  320. .last = 0x40,
  321. },
  322. {
  323. .first = 0x42,
  324. .last = 0x42,
  325. },
  326. {
  327. .first = 0x44,
  328. .last = 0x44,
  329. },
  330. {
  331. .first = 0x50,
  332. .last = 0x55,
  333. },
  334. {
  335. .first = 0x80,
  336. .last = 0x82,
  337. },
  338. {
  339. .first = 0xC0,
  340. .last = 0xC2,
  341. },
  342. {
  343. .first = 0xf5,
  344. .last = 0xf6,
  345. },
  346. },
  347. },
  348. [AB8500_GAS_GAUGE] = {
  349. .num_ranges = 3,
  350. .range = (struct ab8500_reg_range[]) {
  351. {
  352. .first = 0x00,
  353. .last = 0x00,
  354. },
  355. {
  356. .first = 0x07,
  357. .last = 0x0A,
  358. },
  359. {
  360. .first = 0x10,
  361. .last = 0x14,
  362. },
  363. },
  364. },
  365. [AB8500_DEVELOPMENT] = {
  366. .num_ranges = 1,
  367. .range = (struct ab8500_reg_range[]) {
  368. {
  369. .first = 0x00,
  370. .last = 0x00,
  371. },
  372. },
  373. },
  374. [AB8500_DEBUG] = {
  375. .num_ranges = 1,
  376. .range = (struct ab8500_reg_range[]) {
  377. {
  378. .first = 0x05,
  379. .last = 0x07,
  380. },
  381. },
  382. },
  383. [AB8500_AUDIO] = {
  384. .num_ranges = 1,
  385. .range = (struct ab8500_reg_range[]) {
  386. {
  387. .first = 0x00,
  388. .last = 0x6F,
  389. },
  390. },
  391. },
  392. [AB8500_INTERRUPT] = {
  393. .num_ranges = 0,
  394. .range = NULL,
  395. },
  396. [AB8500_RTC] = {
  397. .num_ranges = 1,
  398. .range = (struct ab8500_reg_range[]) {
  399. {
  400. .first = 0x00,
  401. .last = 0x0F,
  402. },
  403. },
  404. },
  405. [AB8500_MISC] = {
  406. .num_ranges = 8,
  407. .range = (struct ab8500_reg_range[]) {
  408. {
  409. .first = 0x00,
  410. .last = 0x05,
  411. },
  412. {
  413. .first = 0x10,
  414. .last = 0x15,
  415. },
  416. {
  417. .first = 0x20,
  418. .last = 0x25,
  419. },
  420. {
  421. .first = 0x30,
  422. .last = 0x35,
  423. },
  424. {
  425. .first = 0x40,
  426. .last = 0x45,
  427. },
  428. {
  429. .first = 0x50,
  430. .last = 0x50,
  431. },
  432. {
  433. .first = 0x60,
  434. .last = 0x67,
  435. },
  436. {
  437. .first = 0x80,
  438. .last = 0x80,
  439. },
  440. },
  441. },
  442. [0x11] = {
  443. .num_ranges = 0,
  444. .range = NULL,
  445. },
  446. [0x12] = {
  447. .num_ranges = 0,
  448. .range = NULL,
  449. },
  450. [0x13] = {
  451. .num_ranges = 0,
  452. .range = NULL,
  453. },
  454. [0x14] = {
  455. .num_ranges = 0,
  456. .range = NULL,
  457. },
  458. [AB8500_OTP_EMUL] = {
  459. .num_ranges = 1,
  460. .range = (struct ab8500_reg_range[]) {
  461. {
  462. .first = 0x01,
  463. .last = 0x0F,
  464. },
  465. },
  466. },
  467. };
  468. static irqreturn_t ab8500_debug_handler(int irq, void *data)
  469. {
  470. char buf[16];
  471. struct kobject *kobj = (struct kobject *)data;
  472. unsigned int irq_abb = irq - irq_first;
  473. if (irq_abb < num_irqs)
  474. irq_count[irq_abb]++;
  475. /*
  476. * This makes it possible to use poll for events (POLLPRI | POLLERR)
  477. * from userspace on sysfs file named <irq-nr>
  478. */
  479. sprintf(buf, "%d", irq);
  480. sysfs_notify(kobj, NULL, buf);
  481. return IRQ_HANDLED;
  482. }
  483. /* Prints to seq_file or log_buf */
  484. static int ab8500_registers_print(struct device *dev, u32 bank,
  485. struct seq_file *s)
  486. {
  487. unsigned int i;
  488. for (i = 0; i < debug_ranges[bank].num_ranges; i++) {
  489. u32 reg;
  490. for (reg = debug_ranges[bank].range[i].first;
  491. reg <= debug_ranges[bank].range[i].last;
  492. reg++) {
  493. u8 value;
  494. int err;
  495. err = abx500_get_register_interruptible(dev,
  496. (u8)bank, (u8)reg, &value);
  497. if (err < 0) {
  498. dev_err(dev, "ab->read fail %d\n", err);
  499. return err;
  500. }
  501. if (s) {
  502. err = seq_printf(s, " [%u/0x%02X]: 0x%02X\n",
  503. bank, reg, value);
  504. if (err < 0) {
  505. dev_err(dev,
  506. "seq_printf overflow bank=%d reg=%d\n",
  507. bank, reg);
  508. /* Error is not returned here since
  509. * the output is wanted in any case */
  510. return 0;
  511. }
  512. } else {
  513. printk(KERN_INFO" [%u/0x%02X]: 0x%02X\n", bank,
  514. reg, value);
  515. }
  516. }
  517. }
  518. return 0;
  519. }
  520. static int ab8500_print_bank_registers(struct seq_file *s, void *p)
  521. {
  522. struct device *dev = s->private;
  523. u32 bank = debug_bank;
  524. seq_printf(s, AB8500_NAME_STRING " register values:\n");
  525. seq_printf(s, " bank %u:\n", bank);
  526. ab8500_registers_print(dev, bank, s);
  527. return 0;
  528. }
  529. static int ab8500_registers_open(struct inode *inode, struct file *file)
  530. {
  531. return single_open(file, ab8500_print_bank_registers, inode->i_private);
  532. }
  533. static const struct file_operations ab8500_registers_fops = {
  534. .open = ab8500_registers_open,
  535. .read = seq_read,
  536. .llseek = seq_lseek,
  537. .release = single_release,
  538. .owner = THIS_MODULE,
  539. };
  540. static int ab8500_print_all_banks(struct seq_file *s, void *p)
  541. {
  542. struct device *dev = s->private;
  543. unsigned int i;
  544. int err;
  545. seq_printf(s, AB8500_NAME_STRING " register values:\n");
  546. for (i = 1; i < AB8500_NUM_BANKS; i++) {
  547. err = seq_printf(s, " bank %u:\n", i);
  548. if (err < 0)
  549. dev_err(dev, "seq_printf overflow, bank=%d\n", i);
  550. ab8500_registers_print(dev, i, s);
  551. }
  552. return 0;
  553. }
  554. /* Dump registers to kernel log */
  555. void ab8500_dump_all_banks(struct device *dev)
  556. {
  557. unsigned int i;
  558. printk(KERN_INFO"ab8500 register values:\n");
  559. for (i = 1; i < AB8500_NUM_BANKS; i++) {
  560. printk(KERN_INFO" bank %u:\n", i);
  561. ab8500_registers_print(dev, i, NULL);
  562. }
  563. }
  564. static int ab8500_all_banks_open(struct inode *inode, struct file *file)
  565. {
  566. struct seq_file *s;
  567. int err;
  568. err = single_open(file, ab8500_print_all_banks, inode->i_private);
  569. if (!err) {
  570. /* Default buf size in seq_read is not enough */
  571. s = (struct seq_file *)file->private_data;
  572. s->size = (PAGE_SIZE * 2);
  573. s->buf = kmalloc(s->size, GFP_KERNEL);
  574. if (!s->buf) {
  575. single_release(inode, file);
  576. err = -ENOMEM;
  577. }
  578. }
  579. return err;
  580. }
  581. static const struct file_operations ab8500_all_banks_fops = {
  582. .open = ab8500_all_banks_open,
  583. .read = seq_read,
  584. .llseek = seq_lseek,
  585. .release = single_release,
  586. .owner = THIS_MODULE,
  587. };
  588. static int ab8500_bank_print(struct seq_file *s, void *p)
  589. {
  590. return seq_printf(s, "%d\n", debug_bank);
  591. }
  592. static int ab8500_bank_open(struct inode *inode, struct file *file)
  593. {
  594. return single_open(file, ab8500_bank_print, inode->i_private);
  595. }
  596. static ssize_t ab8500_bank_write(struct file *file,
  597. const char __user *user_buf,
  598. size_t count, loff_t *ppos)
  599. {
  600. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  601. unsigned long user_bank;
  602. int err;
  603. /* Get userspace string and assure termination */
  604. err = kstrtoul_from_user(user_buf, count, 0, &user_bank);
  605. if (err)
  606. return err;
  607. if (user_bank >= AB8500_NUM_BANKS) {
  608. dev_err(dev, "debugfs error input > number of banks\n");
  609. return -EINVAL;
  610. }
  611. debug_bank = user_bank;
  612. return count;
  613. }
  614. static int ab8500_address_print(struct seq_file *s, void *p)
  615. {
  616. return seq_printf(s, "0x%02X\n", debug_address);
  617. }
  618. static int ab8500_address_open(struct inode *inode, struct file *file)
  619. {
  620. return single_open(file, ab8500_address_print, inode->i_private);
  621. }
  622. static ssize_t ab8500_address_write(struct file *file,
  623. const char __user *user_buf,
  624. size_t count, loff_t *ppos)
  625. {
  626. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  627. unsigned long user_address;
  628. int err;
  629. /* Get userspace string and assure termination */
  630. err = kstrtoul_from_user(user_buf, count, 0, &user_address);
  631. if (err)
  632. return err;
  633. if (user_address > 0xff) {
  634. dev_err(dev, "debugfs error input > 0xff\n");
  635. return -EINVAL;
  636. }
  637. debug_address = user_address;
  638. return count;
  639. }
  640. static int ab8500_val_print(struct seq_file *s, void *p)
  641. {
  642. struct device *dev = s->private;
  643. int ret;
  644. u8 regvalue;
  645. ret = abx500_get_register_interruptible(dev,
  646. (u8)debug_bank, (u8)debug_address, &regvalue);
  647. if (ret < 0) {
  648. dev_err(dev, "abx500_get_reg fail %d, %d\n",
  649. ret, __LINE__);
  650. return -EINVAL;
  651. }
  652. seq_printf(s, "0x%02X\n", regvalue);
  653. return 0;
  654. }
  655. static int ab8500_val_open(struct inode *inode, struct file *file)
  656. {
  657. return single_open(file, ab8500_val_print, inode->i_private);
  658. }
  659. static ssize_t ab8500_val_write(struct file *file,
  660. const char __user *user_buf,
  661. size_t count, loff_t *ppos)
  662. {
  663. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  664. unsigned long user_val;
  665. int err;
  666. /* Get userspace string and assure termination */
  667. err = kstrtoul_from_user(user_buf, count, 0, &user_val);
  668. if (err)
  669. return err;
  670. if (user_val > 0xff) {
  671. dev_err(dev, "debugfs error input > 0xff\n");
  672. return -EINVAL;
  673. }
  674. err = abx500_set_register_interruptible(dev,
  675. (u8)debug_bank, debug_address, (u8)user_val);
  676. if (err < 0) {
  677. printk(KERN_ERR "abx500_set_reg failed %d, %d", err, __LINE__);
  678. return -EINVAL;
  679. }
  680. return count;
  681. }
  682. /*
  683. * Interrupt status
  684. */
  685. static u32 num_interrupts[AB8500_MAX_NR_IRQS];
  686. static int num_interrupt_lines;
  687. void ab8500_debug_register_interrupt(int line)
  688. {
  689. if (line < num_interrupt_lines)
  690. num_interrupts[line]++;
  691. }
  692. static int ab8500_interrupts_print(struct seq_file *s, void *p)
  693. {
  694. int line;
  695. seq_printf(s, "irq: number of\n");
  696. for (line = 0; line < num_interrupt_lines; line++)
  697. seq_printf(s, "%3i: %6i\n", line, num_interrupts[line]);
  698. return 0;
  699. }
  700. static int ab8500_interrupts_open(struct inode *inode, struct file *file)
  701. {
  702. return single_open(file, ab8500_interrupts_print, inode->i_private);
  703. }
  704. /*
  705. * - HWREG DB8500 formated routines
  706. */
  707. static int ab8500_hwreg_print(struct seq_file *s, void *d)
  708. {
  709. struct device *dev = s->private;
  710. int ret;
  711. u8 regvalue;
  712. ret = abx500_get_register_interruptible(dev,
  713. (u8)hwreg_cfg.bank, (u8)hwreg_cfg.addr, &regvalue);
  714. if (ret < 0) {
  715. dev_err(dev, "abx500_get_reg fail %d, %d\n",
  716. ret, __LINE__);
  717. return -EINVAL;
  718. }
  719. if (hwreg_cfg.shift >= 0)
  720. regvalue >>= hwreg_cfg.shift;
  721. else
  722. regvalue <<= -hwreg_cfg.shift;
  723. regvalue &= hwreg_cfg.mask;
  724. if (REG_FMT_DEC(&hwreg_cfg))
  725. seq_printf(s, "%d\n", regvalue);
  726. else
  727. seq_printf(s, "0x%02X\n", regvalue);
  728. return 0;
  729. }
  730. static int ab8500_hwreg_open(struct inode *inode, struct file *file)
  731. {
  732. return single_open(file, ab8500_hwreg_print, inode->i_private);
  733. }
  734. static int ab8500_gpadc_bat_ctrl_print(struct seq_file *s, void *p)
  735. {
  736. int bat_ctrl_raw;
  737. int bat_ctrl_convert;
  738. struct ab8500_gpadc *gpadc;
  739. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  740. bat_ctrl_raw = ab8500_gpadc_read_raw(gpadc, BAT_CTRL);
  741. bat_ctrl_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  742. BAT_CTRL, bat_ctrl_raw);
  743. return seq_printf(s, "%d,0x%X\n",
  744. bat_ctrl_convert, bat_ctrl_raw);
  745. }
  746. static int ab8500_gpadc_bat_ctrl_open(struct inode *inode, struct file *file)
  747. {
  748. return single_open(file, ab8500_gpadc_bat_ctrl_print, inode->i_private);
  749. }
  750. static const struct file_operations ab8500_gpadc_bat_ctrl_fops = {
  751. .open = ab8500_gpadc_bat_ctrl_open,
  752. .read = seq_read,
  753. .llseek = seq_lseek,
  754. .release = single_release,
  755. .owner = THIS_MODULE,
  756. };
  757. static int ab8500_gpadc_btemp_ball_print(struct seq_file *s, void *p)
  758. {
  759. int btemp_ball_raw;
  760. int btemp_ball_convert;
  761. struct ab8500_gpadc *gpadc;
  762. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  763. btemp_ball_raw = ab8500_gpadc_read_raw(gpadc, BTEMP_BALL);
  764. btemp_ball_convert = ab8500_gpadc_ad_to_voltage(gpadc, BTEMP_BALL,
  765. btemp_ball_raw);
  766. return seq_printf(s,
  767. "%d,0x%X\n", btemp_ball_convert, btemp_ball_raw);
  768. }
  769. static int ab8500_gpadc_btemp_ball_open(struct inode *inode,
  770. struct file *file)
  771. {
  772. return single_open(file, ab8500_gpadc_btemp_ball_print, inode->i_private);
  773. }
  774. static const struct file_operations ab8500_gpadc_btemp_ball_fops = {
  775. .open = ab8500_gpadc_btemp_ball_open,
  776. .read = seq_read,
  777. .llseek = seq_lseek,
  778. .release = single_release,
  779. .owner = THIS_MODULE,
  780. };
  781. static int ab8500_gpadc_main_charger_v_print(struct seq_file *s, void *p)
  782. {
  783. int main_charger_v_raw;
  784. int main_charger_v_convert;
  785. struct ab8500_gpadc *gpadc;
  786. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  787. main_charger_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_V);
  788. main_charger_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  789. MAIN_CHARGER_V, main_charger_v_raw);
  790. return seq_printf(s, "%d,0x%X\n",
  791. main_charger_v_convert, main_charger_v_raw);
  792. }
  793. static int ab8500_gpadc_main_charger_v_open(struct inode *inode,
  794. struct file *file)
  795. {
  796. return single_open(file, ab8500_gpadc_main_charger_v_print,
  797. inode->i_private);
  798. }
  799. static const struct file_operations ab8500_gpadc_main_charger_v_fops = {
  800. .open = ab8500_gpadc_main_charger_v_open,
  801. .read = seq_read,
  802. .llseek = seq_lseek,
  803. .release = single_release,
  804. .owner = THIS_MODULE,
  805. };
  806. static int ab8500_gpadc_acc_detect1_print(struct seq_file *s, void *p)
  807. {
  808. int acc_detect1_raw;
  809. int acc_detect1_convert;
  810. struct ab8500_gpadc *gpadc;
  811. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  812. acc_detect1_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT1);
  813. acc_detect1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ACC_DETECT1,
  814. acc_detect1_raw);
  815. return seq_printf(s, "%d,0x%X\n",
  816. acc_detect1_convert, acc_detect1_raw);
  817. }
  818. static int ab8500_gpadc_acc_detect1_open(struct inode *inode,
  819. struct file *file)
  820. {
  821. return single_open(file, ab8500_gpadc_acc_detect1_print,
  822. inode->i_private);
  823. }
  824. static const struct file_operations ab8500_gpadc_acc_detect1_fops = {
  825. .open = ab8500_gpadc_acc_detect1_open,
  826. .read = seq_read,
  827. .llseek = seq_lseek,
  828. .release = single_release,
  829. .owner = THIS_MODULE,
  830. };
  831. static int ab8500_gpadc_acc_detect2_print(struct seq_file *s, void *p)
  832. {
  833. int acc_detect2_raw;
  834. int acc_detect2_convert;
  835. struct ab8500_gpadc *gpadc;
  836. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  837. acc_detect2_raw = ab8500_gpadc_read_raw(gpadc, ACC_DETECT2);
  838. acc_detect2_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  839. ACC_DETECT2, acc_detect2_raw);
  840. return seq_printf(s, "%d,0x%X\n",
  841. acc_detect2_convert, acc_detect2_raw);
  842. }
  843. static int ab8500_gpadc_acc_detect2_open(struct inode *inode,
  844. struct file *file)
  845. {
  846. return single_open(file, ab8500_gpadc_acc_detect2_print,
  847. inode->i_private);
  848. }
  849. static const struct file_operations ab8500_gpadc_acc_detect2_fops = {
  850. .open = ab8500_gpadc_acc_detect2_open,
  851. .read = seq_read,
  852. .llseek = seq_lseek,
  853. .release = single_release,
  854. .owner = THIS_MODULE,
  855. };
  856. static int ab8500_gpadc_aux1_print(struct seq_file *s, void *p)
  857. {
  858. int aux1_raw;
  859. int aux1_convert;
  860. struct ab8500_gpadc *gpadc;
  861. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  862. aux1_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX1);
  863. aux1_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX1,
  864. aux1_raw);
  865. return seq_printf(s, "%d,0x%X\n",
  866. aux1_convert, aux1_raw);
  867. }
  868. static int ab8500_gpadc_aux1_open(struct inode *inode, struct file *file)
  869. {
  870. return single_open(file, ab8500_gpadc_aux1_print, inode->i_private);
  871. }
  872. static const struct file_operations ab8500_gpadc_aux1_fops = {
  873. .open = ab8500_gpadc_aux1_open,
  874. .read = seq_read,
  875. .llseek = seq_lseek,
  876. .release = single_release,
  877. .owner = THIS_MODULE,
  878. };
  879. static int ab8500_gpadc_aux2_print(struct seq_file *s, void *p)
  880. {
  881. int aux2_raw;
  882. int aux2_convert;
  883. struct ab8500_gpadc *gpadc;
  884. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  885. aux2_raw = ab8500_gpadc_read_raw(gpadc, ADC_AUX2);
  886. aux2_convert = ab8500_gpadc_ad_to_voltage(gpadc, ADC_AUX2,
  887. aux2_raw);
  888. return seq_printf(s, "%d,0x%X\n",
  889. aux2_convert, aux2_raw);
  890. }
  891. static int ab8500_gpadc_aux2_open(struct inode *inode, struct file *file)
  892. {
  893. return single_open(file, ab8500_gpadc_aux2_print, inode->i_private);
  894. }
  895. static const struct file_operations ab8500_gpadc_aux2_fops = {
  896. .open = ab8500_gpadc_aux2_open,
  897. .read = seq_read,
  898. .llseek = seq_lseek,
  899. .release = single_release,
  900. .owner = THIS_MODULE,
  901. };
  902. static int ab8500_gpadc_main_bat_v_print(struct seq_file *s, void *p)
  903. {
  904. int main_bat_v_raw;
  905. int main_bat_v_convert;
  906. struct ab8500_gpadc *gpadc;
  907. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  908. main_bat_v_raw = ab8500_gpadc_read_raw(gpadc, MAIN_BAT_V);
  909. main_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, MAIN_BAT_V,
  910. main_bat_v_raw);
  911. return seq_printf(s, "%d,0x%X\n",
  912. main_bat_v_convert, main_bat_v_raw);
  913. }
  914. static int ab8500_gpadc_main_bat_v_open(struct inode *inode,
  915. struct file *file)
  916. {
  917. return single_open(file, ab8500_gpadc_main_bat_v_print, inode->i_private);
  918. }
  919. static const struct file_operations ab8500_gpadc_main_bat_v_fops = {
  920. .open = ab8500_gpadc_main_bat_v_open,
  921. .read = seq_read,
  922. .llseek = seq_lseek,
  923. .release = single_release,
  924. .owner = THIS_MODULE,
  925. };
  926. static int ab8500_gpadc_vbus_v_print(struct seq_file *s, void *p)
  927. {
  928. int vbus_v_raw;
  929. int vbus_v_convert;
  930. struct ab8500_gpadc *gpadc;
  931. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  932. vbus_v_raw = ab8500_gpadc_read_raw(gpadc, VBUS_V);
  933. vbus_v_convert = ab8500_gpadc_ad_to_voltage(gpadc, VBUS_V,
  934. vbus_v_raw);
  935. return seq_printf(s, "%d,0x%X\n",
  936. vbus_v_convert, vbus_v_raw);
  937. }
  938. static int ab8500_gpadc_vbus_v_open(struct inode *inode, struct file *file)
  939. {
  940. return single_open(file, ab8500_gpadc_vbus_v_print, inode->i_private);
  941. }
  942. static const struct file_operations ab8500_gpadc_vbus_v_fops = {
  943. .open = ab8500_gpadc_vbus_v_open,
  944. .read = seq_read,
  945. .llseek = seq_lseek,
  946. .release = single_release,
  947. .owner = THIS_MODULE,
  948. };
  949. static int ab8500_gpadc_main_charger_c_print(struct seq_file *s, void *p)
  950. {
  951. int main_charger_c_raw;
  952. int main_charger_c_convert;
  953. struct ab8500_gpadc *gpadc;
  954. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  955. main_charger_c_raw = ab8500_gpadc_read_raw(gpadc, MAIN_CHARGER_C);
  956. main_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  957. MAIN_CHARGER_C, main_charger_c_raw);
  958. return seq_printf(s, "%d,0x%X\n",
  959. main_charger_c_convert, main_charger_c_raw);
  960. }
  961. static int ab8500_gpadc_main_charger_c_open(struct inode *inode,
  962. struct file *file)
  963. {
  964. return single_open(file, ab8500_gpadc_main_charger_c_print,
  965. inode->i_private);
  966. }
  967. static const struct file_operations ab8500_gpadc_main_charger_c_fops = {
  968. .open = ab8500_gpadc_main_charger_c_open,
  969. .read = seq_read,
  970. .llseek = seq_lseek,
  971. .release = single_release,
  972. .owner = THIS_MODULE,
  973. };
  974. static int ab8500_gpadc_usb_charger_c_print(struct seq_file *s, void *p)
  975. {
  976. int usb_charger_c_raw;
  977. int usb_charger_c_convert;
  978. struct ab8500_gpadc *gpadc;
  979. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  980. usb_charger_c_raw = ab8500_gpadc_read_raw(gpadc, USB_CHARGER_C);
  981. usb_charger_c_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  982. USB_CHARGER_C, usb_charger_c_raw);
  983. return seq_printf(s, "%d,0x%X\n",
  984. usb_charger_c_convert, usb_charger_c_raw);
  985. }
  986. static int ab8500_gpadc_usb_charger_c_open(struct inode *inode,
  987. struct file *file)
  988. {
  989. return single_open(file, ab8500_gpadc_usb_charger_c_print,
  990. inode->i_private);
  991. }
  992. static const struct file_operations ab8500_gpadc_usb_charger_c_fops = {
  993. .open = ab8500_gpadc_usb_charger_c_open,
  994. .read = seq_read,
  995. .llseek = seq_lseek,
  996. .release = single_release,
  997. .owner = THIS_MODULE,
  998. };
  999. static int ab8500_gpadc_bk_bat_v_print(struct seq_file *s, void *p)
  1000. {
  1001. int bk_bat_v_raw;
  1002. int bk_bat_v_convert;
  1003. struct ab8500_gpadc *gpadc;
  1004. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  1005. bk_bat_v_raw = ab8500_gpadc_read_raw(gpadc, BK_BAT_V);
  1006. bk_bat_v_convert = ab8500_gpadc_ad_to_voltage(gpadc,
  1007. BK_BAT_V, bk_bat_v_raw);
  1008. return seq_printf(s, "%d,0x%X\n",
  1009. bk_bat_v_convert, bk_bat_v_raw);
  1010. }
  1011. static int ab8500_gpadc_bk_bat_v_open(struct inode *inode, struct file *file)
  1012. {
  1013. return single_open(file, ab8500_gpadc_bk_bat_v_print, inode->i_private);
  1014. }
  1015. static const struct file_operations ab8500_gpadc_bk_bat_v_fops = {
  1016. .open = ab8500_gpadc_bk_bat_v_open,
  1017. .read = seq_read,
  1018. .llseek = seq_lseek,
  1019. .release = single_release,
  1020. .owner = THIS_MODULE,
  1021. };
  1022. static int ab8500_gpadc_die_temp_print(struct seq_file *s, void *p)
  1023. {
  1024. int die_temp_raw;
  1025. int die_temp_convert;
  1026. struct ab8500_gpadc *gpadc;
  1027. gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  1028. die_temp_raw = ab8500_gpadc_read_raw(gpadc, DIE_TEMP);
  1029. die_temp_convert = ab8500_gpadc_ad_to_voltage(gpadc, DIE_TEMP,
  1030. die_temp_raw);
  1031. return seq_printf(s, "%d,0x%X\n",
  1032. die_temp_convert, die_temp_raw);
  1033. }
  1034. static int ab8500_gpadc_die_temp_open(struct inode *inode, struct file *file)
  1035. {
  1036. return single_open(file, ab8500_gpadc_die_temp_print, inode->i_private);
  1037. }
  1038. static const struct file_operations ab8500_gpadc_die_temp_fops = {
  1039. .open = ab8500_gpadc_die_temp_open,
  1040. .read = seq_read,
  1041. .llseek = seq_lseek,
  1042. .release = single_release,
  1043. .owner = THIS_MODULE,
  1044. };
  1045. /*
  1046. * return length of an ASCII numerical value, 0 is string is not a
  1047. * numerical value.
  1048. * string shall start at value 1st char.
  1049. * string can be tailed with \0 or space or newline chars only.
  1050. * value can be decimal or hexadecimal (prefixed 0x or 0X).
  1051. */
  1052. static int strval_len(char *b)
  1053. {
  1054. char *s = b;
  1055. if ((*s == '0') && ((*(s+1) == 'x') || (*(s+1) == 'X'))) {
  1056. s += 2;
  1057. for (; *s && (*s != ' ') && (*s != '\n'); s++) {
  1058. if (!isxdigit(*s))
  1059. return 0;
  1060. }
  1061. } else {
  1062. if (*s == '-')
  1063. s++;
  1064. for (; *s && (*s != ' ') && (*s != '\n'); s++) {
  1065. if (!isdigit(*s))
  1066. return 0;
  1067. }
  1068. }
  1069. return (int) (s-b);
  1070. }
  1071. /*
  1072. * parse hwreg input data.
  1073. * update global hwreg_cfg only if input data syntax is ok.
  1074. */
  1075. static ssize_t hwreg_common_write(char *b, struct hwreg_cfg *cfg,
  1076. struct device *dev)
  1077. {
  1078. uint write, val = 0;
  1079. u8 regvalue;
  1080. int ret;
  1081. struct hwreg_cfg loc = {
  1082. .bank = 0, /* default: invalid phys addr */
  1083. .addr = 0, /* default: invalid phys addr */
  1084. .fmt = 0, /* default: 32bit access, hex output */
  1085. .mask = 0xFFFFFFFF, /* default: no mask */
  1086. .shift = 0, /* default: no bit shift */
  1087. };
  1088. /* read or write ? */
  1089. if (!strncmp(b, "read ", 5)) {
  1090. write = 0;
  1091. b += 5;
  1092. } else if (!strncmp(b, "write ", 6)) {
  1093. write = 1;
  1094. b += 6;
  1095. } else
  1096. return -EINVAL;
  1097. /* OPTIONS -l|-w|-b -s -m -o */
  1098. while ((*b == ' ') || (*b == '-')) {
  1099. if (*(b-1) != ' ') {
  1100. b++;
  1101. continue;
  1102. }
  1103. if ((!strncmp(b, "-d ", 3)) ||
  1104. (!strncmp(b, "-dec ", 5))) {
  1105. b += (*(b+2) == ' ') ? 3 : 5;
  1106. loc.fmt |= (1<<0);
  1107. } else if ((!strncmp(b, "-h ", 3)) ||
  1108. (!strncmp(b, "-hex ", 5))) {
  1109. b += (*(b+2) == ' ') ? 3 : 5;
  1110. loc.fmt &= ~(1<<0);
  1111. } else if ((!strncmp(b, "-m ", 3)) ||
  1112. (!strncmp(b, "-mask ", 6))) {
  1113. b += (*(b+2) == ' ') ? 3 : 6;
  1114. if (strval_len(b) == 0)
  1115. return -EINVAL;
  1116. loc.mask = simple_strtoul(b, &b, 0);
  1117. } else if ((!strncmp(b, "-s ", 3)) ||
  1118. (!strncmp(b, "-shift ", 7))) {
  1119. b += (*(b+2) == ' ') ? 3 : 7;
  1120. if (strval_len(b) == 0)
  1121. return -EINVAL;
  1122. loc.shift = simple_strtol(b, &b, 0);
  1123. } else {
  1124. return -EINVAL;
  1125. }
  1126. }
  1127. /* get arg BANK and ADDRESS */
  1128. if (strval_len(b) == 0)
  1129. return -EINVAL;
  1130. loc.bank = simple_strtoul(b, &b, 0);
  1131. while (*b == ' ')
  1132. b++;
  1133. if (strval_len(b) == 0)
  1134. return -EINVAL;
  1135. loc.addr = simple_strtoul(b, &b, 0);
  1136. if (write) {
  1137. while (*b == ' ')
  1138. b++;
  1139. if (strval_len(b) == 0)
  1140. return -EINVAL;
  1141. val = simple_strtoul(b, &b, 0);
  1142. }
  1143. /* args are ok, update target cfg (mainly for read) */
  1144. *cfg = loc;
  1145. #ifdef ABB_HWREG_DEBUG
  1146. pr_warn("HWREG request: %s, %s, addr=0x%08X, mask=0x%X, shift=%d"
  1147. "value=0x%X\n", (write) ? "write" : "read",
  1148. REG_FMT_DEC(cfg) ? "decimal" : "hexa",
  1149. cfg->addr, cfg->mask, cfg->shift, val);
  1150. #endif
  1151. if (!write)
  1152. return 0;
  1153. ret = abx500_get_register_interruptible(dev,
  1154. (u8)cfg->bank, (u8)cfg->addr, &regvalue);
  1155. if (ret < 0) {
  1156. dev_err(dev, "abx500_get_reg fail %d, %d\n",
  1157. ret, __LINE__);
  1158. return -EINVAL;
  1159. }
  1160. if (cfg->shift >= 0) {
  1161. regvalue &= ~(cfg->mask << (cfg->shift));
  1162. val = (val & cfg->mask) << (cfg->shift);
  1163. } else {
  1164. regvalue &= ~(cfg->mask >> (-cfg->shift));
  1165. val = (val & cfg->mask) >> (-cfg->shift);
  1166. }
  1167. val = val | regvalue;
  1168. ret = abx500_set_register_interruptible(dev,
  1169. (u8)cfg->bank, (u8)cfg->addr, (u8)val);
  1170. if (ret < 0) {
  1171. pr_err("abx500_set_reg failed %d, %d", ret, __LINE__);
  1172. return -EINVAL;
  1173. }
  1174. return 0;
  1175. }
  1176. static ssize_t ab8500_hwreg_write(struct file *file,
  1177. const char __user *user_buf, size_t count, loff_t *ppos)
  1178. {
  1179. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  1180. char buf[128];
  1181. int buf_size, ret;
  1182. /* Get userspace string and assure termination */
  1183. buf_size = min(count, (sizeof(buf)-1));
  1184. if (copy_from_user(buf, user_buf, buf_size))
  1185. return -EFAULT;
  1186. buf[buf_size] = 0;
  1187. /* get args and process */
  1188. ret = hwreg_common_write(buf, &hwreg_cfg, dev);
  1189. return (ret) ? ret : buf_size;
  1190. }
  1191. /*
  1192. * - irq subscribe/unsubscribe stuff
  1193. */
  1194. static int ab8500_subscribe_unsubscribe_print(struct seq_file *s, void *p)
  1195. {
  1196. seq_printf(s, "%d\n", irq_first);
  1197. return 0;
  1198. }
  1199. static int ab8500_subscribe_unsubscribe_open(struct inode *inode,
  1200. struct file *file)
  1201. {
  1202. return single_open(file, ab8500_subscribe_unsubscribe_print,
  1203. inode->i_private);
  1204. }
  1205. /*
  1206. * Userspace should use poll() on this file. When an event occur
  1207. * the blocking poll will be released.
  1208. */
  1209. static ssize_t show_irq(struct device *dev,
  1210. struct device_attribute *attr, char *buf)
  1211. {
  1212. unsigned long name;
  1213. unsigned int irq_index;
  1214. int err;
  1215. err = strict_strtoul(attr->attr.name, 0, &name);
  1216. if (err)
  1217. return err;
  1218. irq_index = name - irq_first;
  1219. if (irq_index >= num_irqs)
  1220. return -EINVAL;
  1221. else
  1222. return sprintf(buf, "%u\n", irq_count[irq_index]);
  1223. }
  1224. static ssize_t ab8500_subscribe_write(struct file *file,
  1225. const char __user *user_buf,
  1226. size_t count, loff_t *ppos)
  1227. {
  1228. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  1229. char buf[32];
  1230. int buf_size;
  1231. unsigned long user_val;
  1232. int err;
  1233. unsigned int irq_index;
  1234. /* Get userspace string and assure termination */
  1235. buf_size = min(count, (sizeof(buf)-1));
  1236. if (copy_from_user(buf, user_buf, buf_size))
  1237. return -EFAULT;
  1238. buf[buf_size] = 0;
  1239. err = strict_strtoul(buf, 0, &user_val);
  1240. if (err)
  1241. return -EINVAL;
  1242. if (user_val < irq_first) {
  1243. dev_err(dev, "debugfs error input < %d\n", irq_first);
  1244. return -EINVAL;
  1245. }
  1246. if (user_val > irq_last) {
  1247. dev_err(dev, "debugfs error input > %d\n", irq_last);
  1248. return -EINVAL;
  1249. }
  1250. irq_index = user_val - irq_first;
  1251. if (irq_index >= num_irqs)
  1252. return -EINVAL;
  1253. /*
  1254. * This will create a sysfs file named <irq-nr> which userspace can
  1255. * use to select or poll and get the AB8500 events
  1256. */
  1257. dev_attr[irq_index] = kmalloc(sizeof(struct device_attribute),
  1258. GFP_KERNEL);
  1259. event_name[irq_index] = kmalloc(buf_size, GFP_KERNEL);
  1260. sprintf(event_name[irq_index], "%lu", user_val);
  1261. dev_attr[irq_index]->show = show_irq;
  1262. dev_attr[irq_index]->store = NULL;
  1263. dev_attr[irq_index]->attr.name = event_name[irq_index];
  1264. dev_attr[irq_index]->attr.mode = S_IRUGO;
  1265. err = sysfs_create_file(&dev->kobj, &dev_attr[irq_index]->attr);
  1266. if (err < 0) {
  1267. printk(KERN_ERR "sysfs_create_file failed %d\n", err);
  1268. return err;
  1269. }
  1270. err = request_threaded_irq(user_val, NULL, ab8500_debug_handler,
  1271. IRQF_SHARED | IRQF_NO_SUSPEND,
  1272. "ab8500-debug", &dev->kobj);
  1273. if (err < 0) {
  1274. printk(KERN_ERR "request_threaded_irq failed %d, %lu\n",
  1275. err, user_val);
  1276. sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
  1277. return err;
  1278. }
  1279. return buf_size;
  1280. }
  1281. static ssize_t ab8500_unsubscribe_write(struct file *file,
  1282. const char __user *user_buf,
  1283. size_t count, loff_t *ppos)
  1284. {
  1285. struct device *dev = ((struct seq_file *)(file->private_data))->private;
  1286. char buf[32];
  1287. int buf_size;
  1288. unsigned long user_val;
  1289. int err;
  1290. unsigned int irq_index;
  1291. /* Get userspace string and assure termination */
  1292. buf_size = min(count, (sizeof(buf)-1));
  1293. if (copy_from_user(buf, user_buf, buf_size))
  1294. return -EFAULT;
  1295. buf[buf_size] = 0;
  1296. err = strict_strtoul(buf, 0, &user_val);
  1297. if (err)
  1298. return -EINVAL;
  1299. if (user_val < irq_first) {
  1300. dev_err(dev, "debugfs error input < %d\n", irq_first);
  1301. return -EINVAL;
  1302. }
  1303. if (user_val > irq_last) {
  1304. dev_err(dev, "debugfs error input > %d\n", irq_last);
  1305. return -EINVAL;
  1306. }
  1307. irq_index = user_val - irq_first;
  1308. if (irq_index >= num_irqs)
  1309. return -EINVAL;
  1310. /* Set irq count to 0 when unsubscribe */
  1311. irq_count[irq_index] = 0;
  1312. if (dev_attr[irq_index])
  1313. sysfs_remove_file(&dev->kobj, &dev_attr[irq_index]->attr);
  1314. free_irq(user_val, &dev->kobj);
  1315. kfree(event_name[irq_index]);
  1316. kfree(dev_attr[irq_index]);
  1317. return buf_size;
  1318. }
  1319. /*
  1320. * - several deubgfs nodes fops
  1321. */
  1322. static const struct file_operations ab8500_bank_fops = {
  1323. .open = ab8500_bank_open,
  1324. .write = ab8500_bank_write,
  1325. .read = seq_read,
  1326. .llseek = seq_lseek,
  1327. .release = single_release,
  1328. .owner = THIS_MODULE,
  1329. };
  1330. static const struct file_operations ab8500_address_fops = {
  1331. .open = ab8500_address_open,
  1332. .write = ab8500_address_write,
  1333. .read = seq_read,
  1334. .llseek = seq_lseek,
  1335. .release = single_release,
  1336. .owner = THIS_MODULE,
  1337. };
  1338. static const struct file_operations ab8500_val_fops = {
  1339. .open = ab8500_val_open,
  1340. .write = ab8500_val_write,
  1341. .read = seq_read,
  1342. .llseek = seq_lseek,
  1343. .release = single_release,
  1344. .owner = THIS_MODULE,
  1345. };
  1346. static const struct file_operations ab8500_interrupts_fops = {
  1347. .open = ab8500_interrupts_open,
  1348. .read = seq_read,
  1349. .llseek = seq_lseek,
  1350. .release = single_release,
  1351. .owner = THIS_MODULE,
  1352. };
  1353. static const struct file_operations ab8500_subscribe_fops = {
  1354. .open = ab8500_subscribe_unsubscribe_open,
  1355. .write = ab8500_subscribe_write,
  1356. .read = seq_read,
  1357. .llseek = seq_lseek,
  1358. .release = single_release,
  1359. .owner = THIS_MODULE,
  1360. };
  1361. static const struct file_operations ab8500_unsubscribe_fops = {
  1362. .open = ab8500_subscribe_unsubscribe_open,
  1363. .write = ab8500_unsubscribe_write,
  1364. .read = seq_read,
  1365. .llseek = seq_lseek,
  1366. .release = single_release,
  1367. .owner = THIS_MODULE,
  1368. };
  1369. static const struct file_operations ab8500_hwreg_fops = {
  1370. .open = ab8500_hwreg_open,
  1371. .write = ab8500_hwreg_write,
  1372. .read = seq_read,
  1373. .llseek = seq_lseek,
  1374. .release = single_release,
  1375. .owner = THIS_MODULE,
  1376. };
  1377. static struct dentry *ab8500_dir;
  1378. static struct dentry *ab8500_gpadc_dir;
  1379. static int ab8500_debug_probe(struct platform_device *plf)
  1380. {
  1381. struct dentry *file;
  1382. int ret = -ENOMEM;
  1383. struct ab8500 *ab8500;
  1384. debug_bank = AB8500_MISC;
  1385. debug_address = AB8500_REV_REG & 0x00FF;
  1386. ab8500 = dev_get_drvdata(plf->dev.parent);
  1387. num_irqs = ab8500->mask_size;
  1388. irq_count = kzalloc(sizeof(*irq_count)*num_irqs, GFP_KERNEL);
  1389. if (!irq_count)
  1390. return -ENOMEM;
  1391. dev_attr = kzalloc(sizeof(*dev_attr)*num_irqs,GFP_KERNEL);
  1392. if (!dev_attr)
  1393. goto out_freeirq_count;
  1394. event_name = kzalloc(sizeof(*event_name)*num_irqs, GFP_KERNEL);
  1395. if (!event_name)
  1396. goto out_freedev_attr;
  1397. irq_first = platform_get_irq_byname(plf, "IRQ_FIRST");
  1398. if (irq_first < 0) {
  1399. dev_err(&plf->dev, "First irq not found, err %d\n",
  1400. irq_first);
  1401. ret = irq_first;
  1402. goto out_freeevent_name;
  1403. }
  1404. irq_last = platform_get_irq_byname(plf, "IRQ_LAST");
  1405. if (irq_last < 0) {
  1406. dev_err(&plf->dev, "Last irq not found, err %d\n",
  1407. irq_last);
  1408. ret = irq_last;
  1409. goto out_freeevent_name;
  1410. }
  1411. ab8500_dir = debugfs_create_dir(AB8500_NAME_STRING, NULL);
  1412. if (!ab8500_dir)
  1413. goto err;
  1414. ab8500_gpadc_dir = debugfs_create_dir(AB8500_ADC_NAME_STRING,
  1415. ab8500_dir);
  1416. if (!ab8500_gpadc_dir)
  1417. goto err;
  1418. file = debugfs_create_file("all-bank-registers", S_IRUGO,
  1419. ab8500_dir, &plf->dev, &ab8500_registers_fops);
  1420. if (!file)
  1421. goto err;
  1422. file = debugfs_create_file("all-banks", S_IRUGO,
  1423. ab8500_dir, &plf->dev, &ab8500_all_banks_fops);
  1424. if (!file)
  1425. goto err;
  1426. file = debugfs_create_file("register-bank", (S_IRUGO | S_IWUSR),
  1427. ab8500_dir, &plf->dev, &ab8500_bank_fops);
  1428. if (!file)
  1429. goto err;
  1430. file = debugfs_create_file("register-address", (S_IRUGO | S_IWUSR),
  1431. ab8500_dir, &plf->dev, &ab8500_address_fops);
  1432. if (!file)
  1433. goto err;
  1434. file = debugfs_create_file("register-value", (S_IRUGO | S_IWUSR),
  1435. ab8500_dir, &plf->dev, &ab8500_val_fops);
  1436. if (!file)
  1437. goto err;
  1438. file = debugfs_create_file("irq-subscribe", (S_IRUGO | S_IWUSR),
  1439. ab8500_dir, &plf->dev, &ab8500_subscribe_fops);
  1440. if (!file)
  1441. goto err;
  1442. if (is_ab8500(ab8500))
  1443. num_interrupt_lines = AB8500_NR_IRQS;
  1444. else if (is_ab8505(ab8500))
  1445. num_interrupt_lines = AB8505_NR_IRQS;
  1446. else if (is_ab9540(ab8500))
  1447. num_interrupt_lines = AB9540_NR_IRQS;
  1448. file = debugfs_create_file("interrupts", (S_IRUGO),
  1449. ab8500_dir, &plf->dev, &ab8500_interrupts_fops);
  1450. if (!file)
  1451. goto err;
  1452. file = debugfs_create_file("irq-unsubscribe", (S_IRUGO | S_IWUSR),
  1453. ab8500_dir, &plf->dev, &ab8500_unsubscribe_fops);
  1454. if (!file)
  1455. goto err;
  1456. file = debugfs_create_file("hwreg", (S_IRUGO | S_IWUSR),
  1457. ab8500_dir, &plf->dev, &ab8500_hwreg_fops);
  1458. if (!file)
  1459. goto err;
  1460. file = debugfs_create_file("bat_ctrl", (S_IRUGO | S_IWUSR),
  1461. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bat_ctrl_fops);
  1462. if (!file)
  1463. goto err;
  1464. file = debugfs_create_file("btemp_ball", (S_IRUGO | S_IWUSR),
  1465. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_btemp_ball_fops);
  1466. if (!file)
  1467. goto err;
  1468. file = debugfs_create_file("main_charger_v", (S_IRUGO | S_IWUSR),
  1469. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_v_fops);
  1470. if (!file)
  1471. goto err;
  1472. file = debugfs_create_file("acc_detect1", (S_IRUGO | S_IWUSR),
  1473. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect1_fops);
  1474. if (!file)
  1475. goto err;
  1476. file = debugfs_create_file("acc_detect2", (S_IRUGO | S_IWUSR),
  1477. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_acc_detect2_fops);
  1478. if (!file)
  1479. goto err;
  1480. file = debugfs_create_file("adc_aux1", (S_IRUGO | S_IWUSR),
  1481. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux1_fops);
  1482. if (!file)
  1483. goto err;
  1484. file = debugfs_create_file("adc_aux2", (S_IRUGO | S_IWUSR),
  1485. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_aux2_fops);
  1486. if (!file)
  1487. goto err;
  1488. file = debugfs_create_file("main_bat_v", (S_IRUGO | S_IWUSR),
  1489. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_bat_v_fops);
  1490. if (!file)
  1491. goto err;
  1492. file = debugfs_create_file("vbus_v", (S_IRUGO | S_IWUSR),
  1493. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_vbus_v_fops);
  1494. if (!file)
  1495. goto err;
  1496. file = debugfs_create_file("main_charger_c", (S_IRUGO | S_IWUSR),
  1497. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_main_charger_c_fops);
  1498. if (!file)
  1499. goto err;
  1500. file = debugfs_create_file("usb_charger_c", (S_IRUGO | S_IWUSR),
  1501. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_usb_charger_c_fops);
  1502. if (!file)
  1503. goto err;
  1504. file = debugfs_create_file("bk_bat_v", (S_IRUGO | S_IWUSR),
  1505. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_bk_bat_v_fops);
  1506. if (!file)
  1507. goto err;
  1508. file = debugfs_create_file("die_temp", (S_IRUGO | S_IWUSR),
  1509. ab8500_gpadc_dir, &plf->dev, &ab8500_gpadc_die_temp_fops);
  1510. if (!file)
  1511. goto err;
  1512. return 0;
  1513. err:
  1514. if (ab8500_dir)
  1515. debugfs_remove_recursive(ab8500_dir);
  1516. dev_err(&plf->dev, "failed to create debugfs entries.\n");
  1517. out_freeevent_name:
  1518. kfree(event_name);
  1519. out_freedev_attr:
  1520. kfree(dev_attr);
  1521. out_freeirq_count:
  1522. kfree(irq_count);
  1523. return ret;
  1524. }
  1525. static int ab8500_debug_remove(struct platform_device *plf)
  1526. {
  1527. debugfs_remove_recursive(ab8500_dir);
  1528. kfree(event_name);
  1529. kfree(dev_attr);
  1530. kfree(irq_count);
  1531. return 0;
  1532. }
  1533. static struct platform_driver ab8500_debug_driver = {
  1534. .driver = {
  1535. .name = "ab8500-debug",
  1536. .owner = THIS_MODULE,
  1537. },
  1538. .probe = ab8500_debug_probe,
  1539. .remove = ab8500_debug_remove
  1540. };
  1541. static int __init ab8500_debug_init(void)
  1542. {
  1543. return platform_driver_register(&ab8500_debug_driver);
  1544. }
  1545. static void __exit ab8500_debug_exit(void)
  1546. {
  1547. platform_driver_unregister(&ab8500_debug_driver);
  1548. }
  1549. subsys_initcall(ab8500_debug_init);
  1550. module_exit(ab8500_debug_exit);
  1551. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  1552. MODULE_DESCRIPTION("AB8500 DEBUG");
  1553. MODULE_LICENSE("GPL v2");