summaryrefslogtreecommitdiff
path: root/cleopatre/linux-2.6.25.10-spc300/arch/arm/mach-spc300/spc300-device-gpio.c
blob: a6b6c0f539a4c2a00d24abc8532706034477e82a (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
/*
 * arch/arm/mach-spc300/spc300_device_gpio.c
 *
 * (C) Copyright 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>

#include <asm/hardware.h>
#include <asm/arch/nvram.h>
#include <asm/gpio.h>

static char spc300_gpio_labels[MAX_INTERNAL_GPIOS][16];
static struct gpio_chip spc300_gpio_chips[MAX_INTERNAL_GPIOS];

/**
 * Set a gpio direction to input.
 *
 * \param  chip  gpio number chip.
 * \param  offset  number offset for this chip (should be 0).
 * \return  error code.
 */
int
spc300_gpio_direction_input (struct gpio_chip *chip, unsigned offset)
{
    GPIO_SWPORTA_DDR_VA &= ~(1 << chip->base);
    return (GPIO_SWPORTA_DDR_VA & (1<<chip->base)) ? -1 : 0;
}

/**
 * Set a gpio direction to output.
 *
 * \param  chip  gpio number chip.
 * \param  offset  number offset for this chip (should be 0).
 * \param  value  initial value to set.
 * \return  error code.
 */
int
spc300_gpio_direction_output (struct gpio_chip *chip, unsigned offset, int value)
{
    /* Set initial value before setting direction
     * to be sure that initial value is set when gpio is configured
     * it's important for reset pin for example. */
    if (value)
    {
        GPIO_SWPORTA_DR_VA |= (1 << chip->base);
        value = 1;
    }
    else
    {
        GPIO_SWPORTA_DR_VA &= ~(1 << chip->base);
    }

    GPIO_SWPORTA_DDR_VA |= (1 << chip->base);

    /* Check direction */
    if (!(GPIO_SWPORTA_DDR_VA & (1<<chip->base)))
        return -1;
    /* check initial value */
    return (((GPIO_SWPORTA_DR_VA & (1 << chip->base)) >> chip->base) == value) ? 0 : -1;
}

/**
 * Get a gpio value.
 *
 * \param  chip  gpio number chip.
 * \param  offset  number offset for this chip (should be 0).
 * \return  value of this gpio.
 */
int
spc300_gpio_get_value (struct gpio_chip *chip, unsigned offset)
{
    return (GPIO_EXT_PORTA_VA & (1 << chip->base)) ? 1 : 0;
}

/**
 * Set a gpio value.
 *
 * \param  chip  gpio number chip.
 * \param  offset  number offset for this chip (should be 0).
 * \param  value  value to set.
 */
void
spc300_gpio_set_value (struct gpio_chip *chip, unsigned offset, int value)
{
    if (value)
        GPIO_SWPORTA_DR_VA |= (1 << chip->base);
    else
        GPIO_SWPORTA_DR_VA &= ~(1 << chip->base);
}

/**
 * Get Interrupt enable register.
 *
 * \return  contents of this register
 */
uint32_t
spc300_gpio_get_interrupt_enable (void)
{
    return GPIO_INTEN_VA;
}

/**
 * Disable a GPIO interrupt.
 *
 * \param  num  GPIO number
 */
void
spc300_gpio_interrupt_disable (int num)
{
    GPIO_INTEN_VA &= ~(1 << num);
}

/**
 * Get Interrupt mask register.
 *
 * \return  contents of this register
 */
uint32_t
spc300_gpio_get_interrupt_mask (void)
{
    return GPIO_INTMASK_VA;
}

/**
 * Mask a GPIO interrupt.
 *
 * \param  num  GPIO number
 */
void
spc300_gpio_interrupt_mask (int num)
{
    GPIO_INTMASK_VA |= (1 << num);
}

/**
 * Unmask a GPIO interrupt.
 *
 * \param  num  GPIO number
 */
void
spc300_gpio_interrupt_unmask (int num)
{
    GPIO_INTMASK_VA &= ~(1 << num);
}

/**
 * Get Interrupt level register.
 *
 * \return  contents of this register
 */
uint32_t
spc300_gpio_get_interrupt_level (void)
{
    return GPIO_INTTYPE_VA;
}

/**
 * Get Interrupt polarity register.
 *
 * \return  contents of this register
 */
uint32_t
spc300_gpio_get_interrupt_polarity (void)
{
    return GPIO_INT_POLARITY_VA;
}

/**
 * Configure and activate a GPIO interrupt:
 * - set level
 * - set polarity
 * - clear interrupt
 * - enable interrupt
 *
 * \param  num  GPIO number
 * \param  level  level to set
 * \param  polarity  polarity to set
 */
void
spc300_gpio_set_interrupt (int num, int level, int polarity)
{
    /* set interrupt level */
    if (level) /* edge-sensitive */
        GPIO_INTTYPE_VA |= (1 << num);
    else /* level-sensitive */
        GPIO_INTTYPE_VA &= ~(1 << num);

    /* set polarity */
    if (polarity) /* active-high */
        GPIO_INT_POLARITY_VA |= (1 << num);
    else /* active-low */
        GPIO_INT_POLARITY_VA &= ~(1 << num);

    /* clear interrupt */
    GPIO_PORTA_EOI_VA |= (1 << num);

    /* enable interrupt */
    GPIO_INTEN_VA |= (1 << num);
}

/**
 * Get Interrupt status of a Port A GPIO interrupt.
 *
 * \return  status of this GPIO interrupt
 */
uint32_t
spc300_gpio_get_interrupt_status (int num)
{
    return ((GPIO_INTSTATUS_VA & (1 << num)) ? 1 : 0);
}

/**
 * Get Raw interrupt status of Port A (premasking).
 *
 * \return  contents of this register
 */
uint32_t
spc300_gpio_get_interrupt_raw_status (void)
{
    return GPIO_RAW_INTSTATUS_VA;
}

/**
 * Clear several Port A GPIO interrupts.
 *
 * \param  mask  GPIO interrupts to clear (0b1:"to clear" - 0b0:"no action")
 */
void
spc300_gpio_clear_interrupts (int mask)
{
    GPIO_PORTA_EOI_VA |= mask;
}

/**
 * Setup only one GPIO through libgpio.
 *
 * \param  num  gpio number.
 */
static void
spc300_gpio_one_setup (int num)
{
    uint32_t gpio_config;
    struct gpio_chip *chip = &spc300_gpio_chips[num];

    chip->can_sleep = 0;
    chip->base = num;
    chip->ngpio = 1;
    chip->label = spc300_gpio_labels[num];
    chip->get = NULL;
    chip->set = NULL;
    chip->direction_input  = NULL;
    chip->direction_output = NULL;

#ifdef CONFIG_CHIP_FEATURE_SPCPIO
    if (num < MIN_INTERNAL_GPIOS)
        gpio_config = spc300_gpio_cfg(spidcom_nvram.io.spcpio.gpio_0_7_cfg, num);
    else
        gpio_config = spc300_gpio_cfg(spidcom_nvram.io.spcpio.gpio_8_15_cfg, num - MIN_INTERNAL_GPIOS);

    /* Check if gpio_config is ARM gpios */
    if (gpio_config == 0)
#endif /* CONFIG_CHIP_FEATURE_SPCPIO */
    {
#if defined(CONFIG_CHIP_FEATURE_SPCPIO) || defined(CONFIG_CHIP_FEATURE_IOMUX)
        /* check allowed direction */
        switch (spc300_gpio_direction (spidcom_nvram.gpio_allow_dir, num))
        {
        case SPC300_GPIO_DIRECTION_BIDIR:
            chip->get = &spc300_gpio_get_value;
            chip->set = &spc300_gpio_set_value;
            chip->direction_input  = &spc300_gpio_direction_input;
            chip->direction_output = &spc300_gpio_direction_output;
            gpiochip_add(chip);
            break;
        case SPC300_GPIO_DIRECTION_OUTPUT:
            chip->get = &spc300_gpio_get_value;
            chip->set = &spc300_gpio_set_value;
            chip->direction_output = &spc300_gpio_direction_output;
            gpiochip_add(chip);
            break;
        case SPC300_GPIO_DIRECTION_INPUT:
            chip->get = &spc300_gpio_get_value;
            chip->direction_input  = &spc300_gpio_direction_input;
            gpiochip_add(chip);
            break;
        }
#endif /* CONFIG_CHIP_FEATURE_SPCPIO || CONFIG_CHIP_FEATURE_IOMUX */

    }
}

/**
 * Setup all GPIOs.
 */
void
spc300_gpio_setup (void)
{
    int i;

    /* Register allowed gpios through gpio_lib */
    for (i=0 ; i<spc300_gpio_max_nb (spidcom_nvram.pkg_cfg); i++)
    {
        spc300_gpio_one_setup (i);
    }
}