ab8500-debugfs.c 40 KB

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