blob: 9cd2b5c70519a63c8614c5995b5876845bb9d9fc [file] [log] [blame]
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -07001/*
2 * Coldfire generic GPIO support.
3 *
4 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
5 *
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.
9 *
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.
14 */
15
16#include <linux/kernel.h>
Steven Kingeac57942012-05-21 13:10:19 -070017#include <linux/module.h>
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070018#include <linux/init.h>
Kay Sieversa6ec0c02011-12-21 15:09:54 -080019#include <linux/device.h>
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070020
Steven Kingeac57942012-05-21 13:10:19 -070021#include <linux/io.h>
22#include <asm/coldfire.h>
23#include <asm/mcfsim.h>
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070024#include <asm/mcfgpio.h>
25
Steven Kingeac57942012-05-21 13:10:19 -070026int __mcfgpio_get_value(unsigned gpio)
27{
28 return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
29}
30EXPORT_SYMBOL(__mcfgpio_get_value);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070031
Steven Kingeac57942012-05-21 13:10:19 -070032void __mcfgpio_set_value(unsigned gpio, int value)
33{
34 if (gpio < MCFGPIO_SCR_START) {
35 unsigned long flags;
36 MCFGPIO_PORTTYPE data;
37
38 local_irq_save(flags);
39 data = mcfgpio_read(__mcfgpio_podr(gpio));
40 if (value)
41 data |= mcfgpio_bit(gpio);
42 else
43 data &= ~mcfgpio_bit(gpio);
44 mcfgpio_write(data, __mcfgpio_podr(gpio));
45 local_irq_restore(flags);
46 } else {
47 if (value)
48 mcfgpio_write(mcfgpio_bit(gpio),
49 MCFGPIO_SETR_PORT(gpio));
50 else
51 mcfgpio_write(~mcfgpio_bit(gpio),
52 MCFGPIO_CLRR_PORT(gpio));
53 }
54}
55EXPORT_SYMBOL(__mcfgpio_set_value);
56
57int __mcfgpio_direction_input(unsigned gpio)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070058{
59 unsigned long flags;
60 MCFGPIO_PORTTYPE dir;
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070061
62 local_irq_save(flags);
Steven Kingeac57942012-05-21 13:10:19 -070063 dir = mcfgpio_read(__mcfgpio_pddr(gpio));
64 dir &= ~mcfgpio_bit(gpio);
65 mcfgpio_write(dir, __mcfgpio_pddr(gpio));
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070066 local_irq_restore(flags);
67
68 return 0;
69}
Steven Kingeac57942012-05-21 13:10:19 -070070EXPORT_SYMBOL(__mcfgpio_direction_input);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070071
Steven Kingeac57942012-05-21 13:10:19 -070072int __mcfgpio_direction_output(unsigned gpio, int value)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070073{
74 unsigned long flags;
75 MCFGPIO_PORTTYPE data;
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070076
77 local_irq_save(flags);
Steven Kingeac57942012-05-21 13:10:19 -070078 data = mcfgpio_read(__mcfgpio_pddr(gpio));
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070079 if (value)
Steven Kingeac57942012-05-21 13:10:19 -070080 data |= mcfgpio_bit(gpio);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070081 else
Steven Kingeac57942012-05-21 13:10:19 -070082 data &= mcfgpio_bit(gpio);
83 mcfgpio_write(data, __mcfgpio_pddr(gpio));
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -070084
Steven Kingeac57942012-05-21 13:10:19 -070085 /* now set the data to output */
86 if (gpio < MCFGPIO_SCR_START) {
87 data = mcfgpio_read(__mcfgpio_podr(gpio));
88 if (value)
89 data |= mcfgpio_bit(gpio);
90 else
91 data &= ~mcfgpio_bit(gpio);
92 mcfgpio_write(data, __mcfgpio_podr(gpio));
93 } else {
94 if (value)
95 mcfgpio_write(mcfgpio_bit(gpio),
96 MCFGPIO_SETR_PORT(gpio));
97 else
98 mcfgpio_write(~mcfgpio_bit(gpio),
99 MCFGPIO_CLRR_PORT(gpio));
100 }
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700101 local_irq_restore(flags);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700102 return 0;
103}
Steven Kingeac57942012-05-21 13:10:19 -0700104EXPORT_SYMBOL(__mcfgpio_direction_output);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700105
Steven Kingeac57942012-05-21 13:10:19 -0700106int __mcfgpio_request(unsigned gpio)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700107{
Steven Kingeac57942012-05-21 13:10:19 -0700108 return 0;
109}
110EXPORT_SYMBOL(__mcfgpio_request);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700111
Steven Kingeac57942012-05-21 13:10:19 -0700112void __mcfgpio_free(unsigned gpio)
113{
114 __mcfgpio_direction_input(gpio);
115}
116EXPORT_SYMBOL(__mcfgpio_free);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700117
Steven Kingeac57942012-05-21 13:10:19 -0700118#ifdef CONFIG_GPIOLIB
119
120int mcfgpio_direction_input(struct gpio_chip *chip, unsigned offset)
121{
122 return __mcfgpio_direction_input(offset);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700123}
124
Steven Kingeac57942012-05-21 13:10:19 -0700125int mcfgpio_get_value(struct gpio_chip *chip, unsigned offset)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700126{
Steven Kingeac57942012-05-21 13:10:19 -0700127 return __mcfgpio_get_value(offset);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700128}
129
Steven Kingeac57942012-05-21 13:10:19 -0700130int mcfgpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700131{
Steven Kingeac57942012-05-21 13:10:19 -0700132 return __mcfgpio_direction_output(offset, value);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700133}
134
Steven Kingeac57942012-05-21 13:10:19 -0700135void mcfgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700136{
Steven Kingeac57942012-05-21 13:10:19 -0700137 __mcfgpio_set_value(offset, value);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700138}
139
Steven Kingeac57942012-05-21 13:10:19 -0700140int mcfgpio_request(struct gpio_chip *chip, unsigned offset)
141{
142 return __mcfgpio_request(offset);
143}
144
145void mcfgpio_free(struct gpio_chip *chip, unsigned offset)
146{
147 __mcfgpio_free(offset);
148}
149
150struct bus_type mcfgpio_subsys = {
Kay Sieversa6ec0c02011-12-21 15:09:54 -0800151 .name = "gpio",
152 .dev_name = "gpio",
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700153};
154
Steven Kingeac57942012-05-21 13:10:19 -0700155static struct gpio_chip mcfgpio_chip = {
156 .label = "mcfgpio",
157 .request = mcfgpio_request,
158 .free = mcfgpio_free,
159 .direction_input = mcfgpio_direction_input,
160 .direction_output = mcfgpio_direction_output,
161 .get = mcfgpio_get_value,
162 .set = mcfgpio_set_value,
163 .base = 0,
164 .ngpio = MCFGPIO_PIN_MAX,
165};
Greg Ungererf23c1442012-04-17 13:25:38 +1000166
Steven Kingeac57942012-05-21 13:10:19 -0700167static int __init mcfgpio_sysinit(void)
168{
169 gpiochip_add(&mcfgpio_chip);
170 return subsys_system_register(&mcfgpio_subsys, NULL);
sfking@fdwdc.comaf39bb82009-06-19 18:11:00 -0700171}
172
Steven Kingeac57942012-05-21 13:10:19 -0700173core_initcall(mcfgpio_sysinit);
174#endif