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