summaryrefslogtreecommitdiff
path: root/cleopatre/linux-2.6.25.10-spc300/drivers/gpio/spc300_gpio.c
blob: 849dd4ef2bd1ef7536289a326ff0820e57639ac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 * Copyright (C) 2009 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/gpio.h>
#include <linux/poll.h>

#define SPC300_GPIO_PID_MAX_NB 8

static int
spc300_gpio_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
                   unsigned long arg);
static unsigned int
spc300_gpio_poll (struct file *filp, poll_table *wait);
static int
spc300_gpio_flush (struct inode *inode, struct file *filp);

/** Private data structure */
struct spc300_gpio
{
    int irq;
    struct platform_device *pdev;
    uint32_t allowed;           /* 1 for each allowed gpio */
    uint32_t already_in_use;    /* 1 for each allowed gpio already in used
                                 * by kernel but that can be used */
    uint32_t is_init;           /* 1 when gpio direction is set */
    /** Private data for interrupt handler and poll function */
    struct
    {
        wait_queue_head_t queue;
        spinlock_t lock;
        /* Contains registered PID-s waiting for a GPIO interrupt,
         * and their associated GPIO raw interrupt status
         * when interrupt is handled */
        pid_t pid[SPC300_GPIO_PID_MAX_NB][2];
    } interrupt;
};

/** Misc device structures */
static const struct file_operations spc300_gpio_fops =
{
    .owner = THIS_MODULE,
    .ioctl = spc300_gpio_ioctl,
    .poll = spc300_gpio_poll,
    .flush = spc300_gpio_flush,
};
static struct miscdevice spc300_gpio_miscdev =
{
    .minor = GPIO_MINOR,
    .name  = "gpio user mode",
    .fops  = &spc300_gpio_fops,
};

/**
 * Set gpio direction from ioctl.
 *
 * This function will set a gpio direction through libgpio.
 * But this direction will be set only if gpio number is :
 * Allowed, if not set direction is stop
 * Not Already_in_use, in this case direction can't be changed
 * Choosen direction can't be accepted.
 * Once direction is set Is_init flag is also set for get/set procedure
 *
 * WARNING: currently nb gpios cannot exceed 32.
 *
 * \param  gpio  spc300_gpio structure.
 * \param  gd  gpio_direction structure.
 * \return  error code.
 */
static int
ioctl_set_direction (struct spc300_gpio *gpio, struct gpio_direction *gd)
{
    struct device *dev = &(gpio->pdev)->dev;

    /* Check if gpio already in used by kernel */
    if ((gpio->already_in_use >> gd->num) & 1)
    {
        dev_err (dev, "GPIO %d direction already set by Kernel, you cannot change it\n", gd->num);
        return 0;
    }

    /* Check if gpio is allowed */
    if ((gpio->allowed >> gd->num) & 1)
    {
        /* Set direction */
        if (SPC300_GPIO_DIRECTION_OUTPUT == gd->dir)
        {
            /* Wanted direction = OUTPUT */
            if (gpio_direction_output (gd->num, gd->val))
            {
                /* GPIO cannot be in OUTPUT */
                dev_err (dev, "GPIO %d cannot accept OUTPUT direction\n", gd->num);
            }
        }
        else if (SPC300_GPIO_DIRECTION_INPUT == gd->dir)
        {
            /* Wanted direction = INPUT */
            if (gpio_direction_input (gd->num))
            {
                /* GPIO cannot be in INPUT */
                dev_err (dev, "GPIO %d cannot accept INPUT direction\n", gd->num);
            }
        }
        else if (SPC300_GPIO_DIRECTION_BIDIR == gd->dir)
        {
            /* GPIO cannot be in BIDIR */
            dev_err (dev, "GPIO %d cannot accept BIDIR direction\n", gd->num);
        }
        else /* NONE */
        {
            return 0;
        }

        /* Set gpio number as initialize */
        gpio->is_init |= (1 << gd->num);
        return 0;
    }
    else
    {
        /* GPIO not allowed */
        dev_err (dev, "You cannot access to GPIO %d\n", gd->num);
        return -EFAULT;
    }
}

/**
 * Set gpio value from ioctl.
 *
 * \param  gpio  spc300_gpio structure.
 * \param  gv  gpio_value structure.
 * \return  error code.
 */
static int
ioctl_set_value (struct spc300_gpio *gpio, struct gpio_value *gv)
{
    struct device *dev = &(gpio->pdev)->dev;

    /* Check if gpio number is initialized */
    if ((gpio->is_init >> gv->num) & 1)
    {
        /* Set gpio number value */
        gpio_set_value (gv->num, gv->val);
        return 0;
    }
    else
    {
        /* Gpio direction is not set */
        dev_dbg (dev, "Set GPIO %d direction before\n", gv->num);
        return -EFAULT;
    }
}

/**
 * Get gpio value from ioctl.
 *
 * \param  gpio  spc300_gpio structure.
 * \param  gv  gpio_value structure.
 * \return  error code.
 */
static int
ioctl_get_value (struct spc300_gpio *gpio, struct gpio_value *gv)
{
    struct device *dev = &(gpio->pdev)->dev;

    /* Check if gpio number is initialized */
    if ((gpio->is_init >> gv->num) & 1)
    {
        /* Find gpio number value */
        gv->val = gpio_get_value (gv->num);
        return 0;
    }
    else
    {
        /* Gpio direction is not set */
        dev_err (dev, "Set GPIO %d direction before\n", gv->num);
        return -EFAULT;
    }
}

/**
 * Request allowed GPIO and fill spc300_gpio fields.
 *
 * This function will request a gpios through libgpio and set the differents
 * spc300_gpio fields depending on request result and SPC300_GPIO_USABLE value.
 * Allowed : will be set if request and GPIO_USABLE are ok.
 * Already_in_use : will be set if request returns busy that means that a
 *                  kernel part has already this gpio number in use.
 * Is_init : will be set when direction is configured or when Already_in_use
 *           flag is set.
 *
 * WARNING: currently nb gpios cannot exceed 32.
 *
 * \param  gpio  spc300_gpio structure.
 */
static void
request_allowed_gpios (struct spc300_gpio *gpio)
{
    int i, status;
    struct device *dev = &(gpio->pdev)->dev;

    gpio->allowed = 0;
    gpio->already_in_use = 0;
    gpio->is_init = 0;

    for (i=0 ; i<ARCH_NR_GPIOS && i<32 ; i++)
    {
        if ((CONFIG_SPC300_GPIO_USABLE >> i) & 1)
        {
            status = gpio_request (i, "IN USE");
            if (status == 0)
                gpio->allowed |= (1<<i);
            else if (status == -EBUSY)
            {
                /* gpio is already in use by the kernel, we can use directly
                 * gpio_get/set_value functions without directions functions */
                gpio->already_in_use |= (1<<i);
                gpio->is_init |= (1<<i);
            }
            else
                dev_dbg (dev, "GPIO %d cannot be used\n", i);
        }
    }
}

/**
 * Free previous requested allowed GPIOs.
 *
 * \param  gpio  spc300_gpio structure.
 */
static void
free_allowed_gpios (struct spc300_gpio *gpio)
{
    int i;

    for (i=0 ; i<ARCH_NR_GPIOS && i<32 ; i++)
    {
        if ((gpio->allowed >> i) & 1)
            gpio_free (i);
    }
}

/**
 * Get number of PID-s registered in the GPIO interrupt PID table.
 *
 * \param  gpio  device structure
 * \return  count if not empty, 0 if empty, -1 if error
 */
int
spc300_gpio_pid_count (struct spc300_gpio *gpio)
{
    int count = 0, i = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (-1 != gpio->interrupt.pid[i][0])
            count++;
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return count;
}

/**
 * Indicate if the current PID is registered in the GPIO interrupt PID table.
 *
 * \param  gpio  device structure
 * \return  1 if registered, 0 if not registered, -1 if error
 */
int
spc300_gpio_pid_registered (struct spc300_gpio *gpio)
{
    int registered = 0, i = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (current->pid == gpio->interrupt.pid[i][0])
        {
            registered = 1;
            break;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return registered;
}

/**
 * Register a PID to the GPIO interrupt PID table.
 *
 * \param  gpio  device structure
 * \return  index if ok, -1 if error
 */
int
spc300_gpio_pid_add (struct spc300_gpio *gpio)
{
    int index = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (index = 0; index < SPC300_GPIO_PID_MAX_NB; index++)
    {
        if (-1 == gpio->interrupt.pid[index][0])
        {
            /* Register PID */
            gpio->interrupt.pid[index][0] = current->pid;
            break;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    if (SPC300_GPIO_PID_MAX_NB <= index)
        /* Not enough space */
        index = -1;

    return index;
}

/**
 * Unregister the current PID from the GPIO interrupt PID table.
 *
 * \param  gpio  device structure
 * \return  0 if ok, -1 if error
 */
int
spc300_gpio_pid_remove (struct spc300_gpio *gpio)
{
    int i = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (current->pid == gpio->interrupt.pid[i][0])
        {
            /* Unregister PID */
            gpio->interrupt.pid[i][0] = -1;
            gpio->interrupt.pid[i][1] = 0;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return 0;
}

int
spc300_gpio_pid_set_raw_status (struct spc300_gpio *gpio, int raw_status)
{
    int i = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (-1 != gpio->interrupt.pid[i][0])
        {
            gpio->interrupt.pid[i][1] |= raw_status;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return 0;
}

int
spc300_gpio_pid_get_raw_status (struct spc300_gpio *gpio)
{
    int i = 0, raw_status = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (current->pid == gpio->interrupt.pid[i][0])
        {
            raw_status = gpio->interrupt.pid[i][1];
            break;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return raw_status;
}

int
spc300_gpio_pid_reset_raw_status (struct spc300_gpio *gpio)
{
    int i = 0;
    unsigned long flags;

    if (NULL == gpio)
        return -1;

    spin_lock_irqsave (&gpio->interrupt.lock, flags);
    for (i = 0; i < SPC300_GPIO_PID_MAX_NB; i++)
    {
        if (current->pid == gpio->interrupt.pid[i][0])
        {
            gpio->interrupt.pid[i][1] = 0;
        }
    }
    spin_unlock_irqrestore (&gpio->interrupt.lock, flags);

    return 0;
}

/**
 * IOCTL commands for GPIO User Mode device.
 *
 * \param  inode  inode structure.
 * \param  file  user exchange structure.
 * \param  cmd  command to execute.
 * \param  arg  arguments.
 * \return  error code.
 */
static int
spc300_gpio_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    void __user *argp = (void __user *)arg;
    struct spc300_gpio *gpio;
    union gpio_info info;

    /* Find gpio private structure pointer */
    if (spc300_gpio_miscdev.parent)
    {
        gpio = dev_get_drvdata (spc300_gpio_miscdev.parent);
        if (gpio == NULL)
            return -EINVAL;
    }
    else
    {
        return -EFAULT;
    }

    switch (cmd)
    {
        case GPIOIOC_GETVALUE:
            /* Get gpio number given by user */
            if (copy_from_user (&info, argp, sizeof (info)))
                return -EFAULT;

            /* Get gpio number value */
            if (ioctl_get_value (gpio, &info.gpioval))
                return -EFAULT;

            /* Give the response to user */
            return copy_to_user (argp, &info, sizeof (info)) ? -EFAULT : 0;

        case GPIOIOC_SETVALUE:
            /* Get gpio number and it value given by user */
            if (copy_from_user(&info, argp, sizeof (info)))
                return -EFAULT;

            /* Set gpio number value */
            if (ioctl_set_value (gpio, &info.gpioval))
                return -EFAULT;

            return 0;

        case GPIOIOC_SETDIRECTION:
            /* Get gpio number and it direction given by user */
            if (copy_from_user (&info, argp, sizeof (info)))
                return -EFAULT;

            /* Set gpio number direction */
            if (ioctl_set_direction (gpio, &info.gpiodir))
                return -EFAULT;

            return 0;

        case GPIOIOC_SETIT:
            if (NULL == file)
                return -EFAULT;

            /* Get gpio mask given by user */
            if (copy_from_user (&info, argp, sizeof (info)))
                return -EFAULT;

            if (info.gpioit.enable)
            {
                if (!spc300_gpio_pid_registered (gpio))
                    /* Register current PID to PID-s table */
                    if (0 > spc300_gpio_pid_add (gpio))
                        return -EFAULT;

                /* Set gpio user mask */
                file->private_data = (void *) info.gpioit.mask;
            }
            else
            {
                if (spc300_gpio_pid_registered (gpio))
                    /* Unregister current PID from PID-s table */
                    if (0 > spc300_gpio_pid_remove (gpio))
                        return -EFAULT;

                /* Reset gpio user mask */
                file->private_data = 0;
            }

            return 0;

        default:
            return -EFAULT;
    }
}

/**
 * Interrupt Handler GPIO procedure.
 *
 * \param  irq  interrupt number
 * \param  dev_id  device structure
 * \return  error code
 */
irqreturn_t
spc300_gpio_interrupt_handler (int irq, void *dev_id)
{
    struct spc300_gpio *dev = (struct spc300_gpio *) dev_id;
    unsigned int raw_status = 0;

    if (NULL == dev)
        return IRQ_NONE;
    if (dev->irq != irq)
        return IRQ_NONE;
    printk (KERN_DEBUG "Handle SPC300 GPIO interrupt\n");

    /* Get GPIO raw interrupt status */
    raw_status = spc300_gpio_get_interrupt_raw_status ();

    /* Reset raised interrupt(s) */
    spc300_gpio_clear_interrupts (raw_status);

    /* Check if any PID is waiting for a GPIO interrupt */
    if (0 != spc300_gpio_pid_count (dev))
    {
        /* Save GPIO raw interrupt status */
        if (0 > spc300_gpio_pid_set_raw_status (dev, raw_status))
            return IRQ_NONE;

        /* Wake up waiting queue */
        wake_up (&dev->interrupt.queue);
    }

    return IRQ_HANDLED;
}

/**
 * Poll GPIO interrupts.
  *
 * \param  filp  file structure
 * \param  wait  poll table structure
 * \return  error code
 *
 * This function is called
 * when a user process calls poll() or select() functions on "/dev/gpio".
 * To be able to warn all waiting processes, a (un)registration system is needed.
 * That's why GPIOIOC_SETIT has to be called by user process before polling.
 * To determinate calling PID, "struct task_struct current pid_t pid" field is used.
 */
static unsigned int
spc300_gpio_poll (struct file *filp, poll_table *wait)
{
    struct spc300_gpio *gpio = NULL;
    unsigned int it_mask = 0, raw_status = 0;

    if (NULL == filp)
        return POLLERR;

    /* Find gpio private structure pointer */
    if (spc300_gpio_miscdev.parent)
    {
        gpio = dev_get_drvdata (spc300_gpio_miscdev.parent);
        if (NULL == gpio)
            return POLLERR;
    }

    poll_wait (filp, &gpio->interrupt.queue, wait);

    /* Get GPIO raw interrupt status and GPIO user interrupt mask */
    raw_status = spc300_gpio_pid_get_raw_status (gpio);
    it_mask = (unsigned int) filp->private_data;

    if (spc300_gpio_pid_registered (gpio)) /* current PID registration status */
    {
        if (raw_status)
        {
            /* Reset GPIO raw interrupt status of the caller */
            if (0 > spc300_gpio_pid_reset_raw_status (gpio))
                return POLLERR;

            /* If already registered, and an interrupt occured,
             * check GPIO raw interrupt status and GPIO user interrupt mask */
            if (raw_status & it_mask)
            {
                /* Inform caller */
                return POLLPRI;
            }
        }
    }
    else
    {
        /* Not registered */
        printk (KERN_WARNING "You have to configure interruption via GPIOIOC_SETIT before polling\n");
    }

    /* - PID not registered, or
     * - no interrupt => continue waiting for interrupt or timeout, or
     * - interrupt masked by user => continue waiting for interrupt or timeout, or
     * - timeout */
    return 0;
}

/**
 * Flush function is called
 * each time a user process calls close() function on "/dev/gpio".
 *
 * \param  inode  inode structure
 * \param  file  user exchange structure
 * \return  error code
 */
static int
spc300_gpio_flush (struct inode *inode, struct file *file)
{
    struct spc300_gpio *gpio;

    printk (KERN_DEBUG "Flush SPC300 GPIO driver\n");

    /* Find gpio private structure pointer */
    if (spc300_gpio_miscdev.parent)
    {
        gpio = dev_get_drvdata (spc300_gpio_miscdev.parent);
        if (NULL == gpio)
            return -EINVAL;
    }
    else
        return -EFAULT;

    if (spc300_gpio_pid_registered (gpio))
        /* Unregister current PID from PID-s table */
        if (0 > spc300_gpio_pid_remove (gpio))
            return -EFAULT;

    /* Reset gpio user mask */
    file->private_data = 0;

    return 0;
}

/**
 * Initialise GPIO driver.
 *
 * \param  pdev  platform device structure.
 * \return  error code.
 */
static int
__init spc300_gpio_probe (struct platform_device *pdev)
{
    struct spc300_gpio *gpio;
    int res, num = 0;

    /* Check arguments */
    if (!pdev)
        return -EFAULT;

    /* Allocate private data */
    gpio = kzalloc (sizeof *gpio, GFP_KERNEL);
    if (!gpio)
        return -ENOMEM;

    /* Link pdev with private data */
    gpio->pdev = pdev;
    dev_set_drvdata (&pdev->dev, gpio);

    /* Get IRQ number */
    gpio->irq = platform_get_irq (pdev, 0);
    if (gpio->irq < 0)
    {
        res = gpio->irq;
        goto fail;
    }

    /* Check if someone has already register a spc300_gpio device */
    if (spc300_gpio_miscdev.parent)
    {
        res = -EBUSY;
        goto fail;
    }
    spc300_gpio_miscdev.parent = &pdev->dev;

    /* Register a Misc device for ioctl... management */
    if ((res = misc_register (&spc300_gpio_miscdev)) != 0)
        goto fail;

    /* Request allowed gpios */
    request_allowed_gpios (gpio);

    /* Initialize structure for poll/select management */
    init_waitqueue_head (&gpio->interrupt.queue);
    spin_lock_init (&gpio->interrupt.lock);
    for (num = 0; num < SPC300_GPIO_PID_MAX_NB; num++)
    {
        gpio->interrupt.pid[num][0] = -1;
        gpio->interrupt.pid[num][1] = 0;
    }

    /* Request GPIO IRQ */
    if (0 != request_irq (gpio->irq, &spc300_gpio_interrupt_handler, 0,
                          spc300_gpio_miscdev.name, gpio))
    {
        printk (KERN_ERR "%s - interrupt %d request fail\n",
                spc300_gpio_miscdev.name, gpio->irq);
        res = -ENODEV;
        goto fail;
    }

    /* Setup GPIO interrupts */
    for (num = 0; num < spc300_gpio_max_nb (spidcom_nvram.pkg_cfg); num++)
    {
        if (SPC300_GPIO_DIRECTION_INPUT == \
            spc300_gpio_direction (spidcom_nvram.gpio_allow_dir, num))
            spc300_gpio_set_interrupt (num, 1, 1);
    }

    dev_info (&pdev->dev, "SPC300 GPIO Driver enabled\n");
    return 0;

    fail:
        dev_dbg (&pdev->dev, "probe error %d for SPC300 GPIO Driver\n",res);
        kfree (gpio);
        return res;
}

/**
 * Uninitialise GPIO driver.
 *
 * \param  pdev  platform device structure.
 * \return  error code.
 */
static int
__exit spc300_gpio_remove (struct platform_device *pdev)
{
    struct spc300_gpio *gpio = dev_get_drvdata (&pdev->dev);
    int res;

    /* Check argument */
    if (gpio == NULL)
        return -EINVAL;

    /* Freeing IRQ */
    //free_irq (gpio->irq, pdev);

    /* Free requested gpios */
    free_allowed_gpios (gpio);

    /* Freeing private data */
    kfree (gpio);

    /* Unregister Misc device */
    res = misc_deregister (&spc300_gpio_miscdev);
    if (!res)
        spc300_gpio_miscdev.parent = NULL;

    return res;
}

/** Module structure */
static struct platform_driver spc300_gpio_driver =
{
    .driver     =
    {
        .name   = "spc300gpio",
        .owner  = THIS_MODULE,
    },
    .suspend    = NULL,
    .resume     = NULL,
    .remove     = __exit_p (spc300_gpio_remove),
};

/**
 * Module initialization.
 *
 * \return  error code.
 */
static int
__init spc300_gpio_init (void)
{
    return platform_driver_probe (&spc300_gpio_driver, spc300_gpio_probe);
}

/**
 * Module uninitialization.
 *
 * \return  error code.
 */
static void
__exit spc300_gpio_exit (void)
{
    platform_driver_unregister (&spc300_gpio_driver);
}

module_init (spc300_gpio_init);
module_exit (spc300_gpio_exit);

MODULE_DESCRIPTION ("SPiDCOM SPC300 GPIO driver");
MODULE_AUTHOR ("SPiDCOM Technologies");
MODULE_LICENSE ("GPL");
MODULE_ALIAS_MISCDEV (GPIO_MINOR);