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