[s3c24xx] Add 2.6.31 patches
[openwrt.git] / target / linux / s3c24xx / files-2.6.31 / drivers / mfd / glamo / glamo-gpio.c
1 /* Smedia Glamo 336x/337x gpio driver
2 *
3 * (C) 2009 Lars-Peter Clausen
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/io.h>
26 #include <linux/platform_device.h>
27
28 #include <linux/gpio.h>
29 #include <linux/mfd/glamo.h>
30
31 #include "glamo-core.h"
32 #include "glamo-regs.h"
33
34 #define GLAMO_NR_GPIO 21
35 #define GLAMO_NR_GPIO_REGS DIV_ROUND_UP(GLAMO_NR_GPIO, 4)
36
37 #define GLAMO_REG_GPIO(x) (((x) * 2) + GLAMO_REG_GPIO_GEN1)
38
39 struct glamo_gpio {
40 struct glamo_core *glamo;
41 struct gpio_chip chip;
42 uint16_t saved_regs[GLAMO_NR_GPIO_REGS];
43 };
44
45 #define REG_OF_GPIO(gpio) (GLAMO_REG_GPIO(gpio >> 2))
46 #define NUM_OF_GPIO(gpio) (gpio & 0x3)
47 #define DIRECTION_BIT(gpio) (1 << (NUM_OF_GPIO(gpio) + 0))
48 #define OUTPUT_BIT(gpio) (1 << (NUM_OF_GPIO(gpio) + 4))
49 #define INPUT_BIT(gpio) (1 << (NUM_OF_GPIO(gpio) + 8))
50 #define FUNC_BIT(gpio) (1 << (NUM_OF_GPIO(gpio) + 12))
51
52
53 static inline struct glamo_core *chip_to_glamo(struct gpio_chip *chip)
54 {
55 return container_of(chip, struct glamo_gpio, chip)->glamo;
56 }
57
58 static void glamo_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
59 {
60 struct glamo_core *glamo = chip_to_glamo(chip);
61 unsigned int reg = REG_OF_GPIO(offset);
62 u_int16_t tmp;
63
64 spin_lock(&glamo->lock);
65 tmp = readw(glamo->base + reg);
66 if (value)
67 tmp |= OUTPUT_BIT(offset);
68 else
69 tmp &= ~OUTPUT_BIT(offset);
70 writew(tmp, glamo->base + reg);
71 spin_unlock(&glamo->lock);
72 }
73
74 static int glamo_gpio_get(struct gpio_chip *chip, unsigned offset)
75 {
76 struct glamo_core *glamo = chip_to_glamo(chip);
77 return readw(glamo->base + REG_OF_GPIO(offset)) & INPUT_BIT(offset) ? 1 : 0;
78 }
79
80 static int glamo_gpio_request(struct gpio_chip *chip, unsigned offset)
81 {
82 struct glamo_core *glamo = chip_to_glamo(chip);
83 unsigned int reg = REG_OF_GPIO(offset);
84 u_int16_t tmp;
85
86 spin_lock(&glamo->lock);
87 tmp = readw(glamo->base + reg);
88 if ((tmp & FUNC_BIT(offset)) == 0) {
89 tmp |= FUNC_BIT(offset);
90 writew(tmp, glamo->base + reg);
91 }
92 spin_unlock(&glamo->lock);
93
94 return 0;
95 }
96
97 static void glamo_gpio_free(struct gpio_chip *chip, unsigned offset)
98 {
99 struct glamo_core *glamo = chip_to_glamo(chip);
100 unsigned int reg = REG_OF_GPIO(offset);
101 u_int16_t tmp;
102
103 spin_lock(&glamo->lock);
104 tmp = readw(glamo->base + reg);
105 if ((tmp & FUNC_BIT(offset)) == 1) {
106 tmp &= ~FUNC_BIT(offset);
107 writew(tmp, glamo->base + reg);
108 }
109 spin_unlock(&glamo->lock);
110 }
111
112 static int glamo_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
113 int value)
114 {
115 struct glamo_core *glamo = chip_to_glamo(chip);
116 unsigned int reg = REG_OF_GPIO(offset);
117 u_int16_t tmp;
118
119 spin_lock(&glamo->lock);
120 tmp = readw(glamo->base + reg);
121 tmp &= ~DIRECTION_BIT(offset);
122
123 if (value)
124 tmp |= OUTPUT_BIT(offset);
125 else
126 tmp &= ~OUTPUT_BIT(offset);
127
128 writew(tmp, glamo->base + reg);
129 spin_unlock(&glamo->lock);
130
131 return 0;
132 }
133
134 static int glamo_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
135 {
136 struct glamo_core *glamo = chip_to_glamo(chip);
137 unsigned int reg = REG_OF_GPIO(offset);
138 u_int16_t tmp;
139
140 spin_lock(&glamo->lock);
141 tmp = readw(glamo->base + reg);
142 if ((tmp & DIRECTION_BIT(offset)) == 0) {
143 tmp |= DIRECTION_BIT(offset);
144 writew(tmp, glamo->base + reg);
145 }
146 spin_unlock(&glamo->lock);
147
148 return 0;
149 }
150
151 static const struct __devinit gpio_chip glamo_gpio_chip = {
152 .label = "glamo",
153 .request = glamo_gpio_request,
154 .free = glamo_gpio_free,
155 .direction_input = glamo_gpio_direction_input,
156 .get = glamo_gpio_get,
157 .direction_output = glamo_gpio_direction_output,
158 .set = glamo_gpio_set,
159 .base = -1,
160 .ngpio = GLAMO_NR_GPIO,
161 .can_sleep = 0,
162 .owner = THIS_MODULE,
163 };
164
165 static int __devinit glamo_gpio_probe(struct platform_device *pdev)
166 {
167 struct glamo_platform_data *pdata = pdev->dev.parent->platform_data;
168 struct glamo_gpio *glamo_gpio;
169 int ret;
170
171 glamo_gpio = kzalloc(sizeof(struct glamo_gpio), GFP_KERNEL);
172 if (!glamo_gpio)
173 return -ENOMEM;
174
175 glamo_gpio->glamo = dev_get_drvdata(pdev->dev.parent);
176 glamo_gpio->chip = glamo_gpio_chip;
177 glamo_gpio->chip.dev = &pdev->dev;
178 if (pdata)
179 glamo_gpio->chip.base = pdata->gpio_base;
180
181 ret = gpiochip_add(&glamo_gpio->chip);
182
183 if (ret) {
184 dev_err(&pdev->dev, "Could not register gpio chip: %d\n", ret);
185 goto err;
186 }
187
188 platform_set_drvdata(pdev, glamo_gpio);
189
190 dev_info(&pdev->dev, "Glamo GPIO registered\n");
191
192 if (pdata->registered)
193 pdata->registered(&pdev->dev);
194
195 return 0;
196 err:
197 kfree(glamo_gpio);
198 return ret;
199 }
200
201 static int __devexit glamo_gpio_remove(struct platform_device *pdev)
202 {
203 struct glamo_gpio *glamo_gpio = platform_get_drvdata(pdev);
204 int ret;
205
206 ret = gpiochip_remove(&glamo_gpio->chip);
207 if (!ret)
208 goto done;
209
210 platform_set_drvdata(pdev, NULL);
211 kfree(glamo_gpio);
212
213 done:
214 return ret;
215 }
216
217 #ifdef CONFIG_PM
218
219 static int glamo_gpio_suspend(struct device *dev)
220 {
221 struct glamo_gpio *glamo_gpio = dev_get_drvdata(dev);
222 struct glamo_core *glamo = glamo_gpio->glamo;
223 uint16_t *saved_regs = glamo_gpio->saved_regs;
224 int i;
225
226 spin_lock(&glamo->lock);
227 for (i = 0; i < GLAMO_NR_GPIO / 4; ++i)
228 saved_regs[i] = readw(glamo->base + GLAMO_REG_GPIO(i));
229 spin_unlock(&glamo->lock);
230
231 return 0;
232 }
233
234 static int glamo_gpio_resume(struct device *dev)
235 {
236 struct glamo_gpio *glamo_gpio = dev_get_drvdata(dev);
237 struct glamo_core *glamo = glamo_gpio->glamo;
238 uint16_t *saved_regs = glamo_gpio->saved_regs;
239 int i;
240
241 spin_lock(&glamo->lock);
242 for (i = 0; i < GLAMO_NR_GPIO_REGS; ++i)
243 writew(saved_regs[i], glamo->base + GLAMO_REG_GPIO(i));
244 spin_unlock(&glamo->lock);
245 return 0;
246 }
247
248 static const struct dev_pm_ops glamo_pm_ops = {
249 .suspend = glamo_gpio_suspend,
250 .resume = glamo_gpio_resume,
251 .freeze = glamo_gpio_suspend,
252 .thaw = glamo_gpio_resume,
253 };
254
255 #define GLAMO_GPIO_PM_OPS (&glamo_pm_ops)
256
257 #else
258 #define GLAMO_GPIO_PM_OPS NULL
259 #endif
260
261 static struct platform_driver glamo_gpio_driver = {
262 .driver = {
263 .name = "glamo-gpio",
264 .owner = THIS_MODULE,
265 .pm = GLAMO_GPIO_PM_OPS,
266 },
267 .probe = glamo_gpio_probe,
268 .remove = __devexit_p(glamo_gpio_remove),
269 };
270
271 static int __devinit glamo_gpio_init(void)
272 {
273 return platform_driver_register(&glamo_gpio_driver);
274 }
275
276 static void __exit glamo_gpio_exit(void)
277 {
278 platform_driver_unregister(&glamo_gpio_driver);
279 }
280
281 module_init(glamo_gpio_init);
282 module_exit(glamo_gpio_exit);
283
284 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
285 MODULE_DESCRIPTION("GPIO interface for the Glamo multimedia device");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("platform:glamo-gpio");
This page took 0.073281 seconds and 5 git commands to generate.