2 * Coldfire generic GPIO support.
4 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
22 #include <asm/coldfire.h>
23 #include <asm/mcfsim.h>
24 #include <asm/mcfgpio.h>
26 int __mcfgpio_get_value(unsigned gpio
)
28 return mcfgpio_read(__mcfgpio_ppdr(gpio
)) & mcfgpio_bit(gpio
);
30 EXPORT_SYMBOL(__mcfgpio_get_value
);
32 void __mcfgpio_set_value(unsigned gpio
, int value
)
34 if (gpio
< MCFGPIO_SCR_START
) {
36 MCFGPIO_PORTTYPE data
;
38 local_irq_save(flags
);
39 data
= mcfgpio_read(__mcfgpio_podr(gpio
));
41 data
|= mcfgpio_bit(gpio
);
43 data
&= ~mcfgpio_bit(gpio
);
44 mcfgpio_write(data
, __mcfgpio_podr(gpio
));
45 local_irq_restore(flags
);
48 mcfgpio_write(mcfgpio_bit(gpio
),
49 MCFGPIO_SETR_PORT(gpio
));
51 mcfgpio_write(~mcfgpio_bit(gpio
),
52 MCFGPIO_CLRR_PORT(gpio
));
55 EXPORT_SYMBOL(__mcfgpio_set_value
);
57 int __mcfgpio_direction_input(unsigned gpio
)
62 local_irq_save(flags
);
63 dir
= mcfgpio_read(__mcfgpio_pddr(gpio
));
64 dir
&= ~mcfgpio_bit(gpio
);
65 mcfgpio_write(dir
, __mcfgpio_pddr(gpio
));
66 local_irq_restore(flags
);
70 EXPORT_SYMBOL(__mcfgpio_direction_input
);
72 int __mcfgpio_direction_output(unsigned gpio
, int value
)
75 MCFGPIO_PORTTYPE data
;
77 local_irq_save(flags
);
78 data
= mcfgpio_read(__mcfgpio_pddr(gpio
));
79 data
|= mcfgpio_bit(gpio
);
80 mcfgpio_write(data
, __mcfgpio_pddr(gpio
));
82 /* now set the data to output */
83 if (gpio
< MCFGPIO_SCR_START
) {
84 data
= mcfgpio_read(__mcfgpio_podr(gpio
));
86 data
|= mcfgpio_bit(gpio
);
88 data
&= ~mcfgpio_bit(gpio
);
89 mcfgpio_write(data
, __mcfgpio_podr(gpio
));
92 mcfgpio_write(mcfgpio_bit(gpio
),
93 MCFGPIO_SETR_PORT(gpio
));
95 mcfgpio_write(~mcfgpio_bit(gpio
),
96 MCFGPIO_CLRR_PORT(gpio
));
98 local_irq_restore(flags
);
101 EXPORT_SYMBOL(__mcfgpio_direction_output
);
103 int __mcfgpio_request(unsigned gpio
)
107 EXPORT_SYMBOL(__mcfgpio_request
);
109 void __mcfgpio_free(unsigned gpio
)
111 __mcfgpio_direction_input(gpio
);
113 EXPORT_SYMBOL(__mcfgpio_free
);
115 #ifdef CONFIG_GPIOLIB
117 static int mcfgpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
119 return __mcfgpio_direction_input(offset
);
122 static int mcfgpio_get_value(struct gpio_chip
*chip
, unsigned offset
)
124 return __mcfgpio_get_value(offset
);
127 static int mcfgpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
130 return __mcfgpio_direction_output(offset
, value
);
133 static void mcfgpio_set_value(struct gpio_chip
*chip
, unsigned offset
,
136 __mcfgpio_set_value(offset
, value
);
139 static int mcfgpio_request(struct gpio_chip
*chip
, unsigned offset
)
141 return __mcfgpio_request(offset
);
144 static void mcfgpio_free(struct gpio_chip
*chip
, unsigned offset
)
146 __mcfgpio_free(offset
);
149 static int mcfgpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
151 #if defined(MCFGPIO_IRQ_MIN)
152 if ((offset
>= MCFGPIO_IRQ_MIN
) && (offset
< MCFGPIO_IRQ_MAX
))
154 if (offset
< MCFGPIO_IRQ_MAX
)
156 return MCFGPIO_IRQ_VECBASE
+ offset
;
161 static struct bus_type mcfgpio_subsys
= {
166 static struct gpio_chip mcfgpio_chip
= {
168 .request
= mcfgpio_request
,
169 .free
= mcfgpio_free
,
170 .direction_input
= mcfgpio_direction_input
,
171 .direction_output
= mcfgpio_direction_output
,
172 .get
= mcfgpio_get_value
,
173 .set
= mcfgpio_set_value
,
174 .to_irq
= mcfgpio_to_irq
,
176 .ngpio
= MCFGPIO_PIN_MAX
,
179 static int __init
mcfgpio_sysinit(void)
181 gpiochip_add(&mcfgpio_chip
);
182 return subsys_system_register(&mcfgpio_subsys
, NULL
);
185 core_initcall(mcfgpio_sysinit
);