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