1 /* Smedia Glamo 336x/337x gpio driver
3 * (C) 2009 Lars-Peter Clausen
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.
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.
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,
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/spinlock.h>
27 #include <linux/platform_device.h>
29 #include <linux/gpio.h>
30 #include <linux/mfd/glamo.h>
32 #include "glamo-core.h"
33 #include "glamo-regs.h"
35 #define GLAMO_NR_GPIOS 21
36 #define GLAMO_NR_GPIO_REGS DIV_ROUND_UP(GLAMO_NR_GPIOS, 4)
38 #define GLAMO_REG_GPIO(x) (((x) * 2) + GLAMO_REG_GPIO_GEN1)
41 struct glamo_core *glamo;
42 struct gpio_chip chip;
43 uint16_t saved_regs[GLAMO_NR_GPIO_REGS];
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)
55 static inline struct glamo_core *chip_to_glamo(struct gpio_chip *chip)
57 return container_of(chip, struct glamo_gpio, chip)->glamo;
60 static void glamo_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
62 struct glamo_core *glamo = chip_to_glamo(chip);
63 unsigned int reg = REG_OF_GPIO(offset);
65 glamo_reg_set_bit_mask(reg, OUTPUT_VALUE(offset, !!value), OUTPUT_BIT(offset));
68 static int glamo_gpio_get(struct gpio_chip *chip, unsigned offset)
70 struct glamo_core *glamo = chip_to_glamo(chip);
71 return !!(glamo_reg_read(glamo, REG_OF_GPIO(offset)) & INPUT_BIT(offset));
74 static int glamo_gpio_request(struct gpio_chip *chip, unsigned offset)
76 struct glamo_core *glamo = chip_to_glamo(chip);
77 unsigned int reg = REG_OF_GPIO(offset);
79 glamo_reg_set_bit(glamo, reg, FUNC_BIT(offset));
84 static void glamo_gpio_free(struct gpio_chip *chip, unsigned offset)
86 struct glamo_core *glamo = chip_to_glamo(chip);
87 unsigned int reg = REG_OF_GPIO(offset);
89 glamo_reg_clear_bit(glamo, reg, FUNC_BIT(offset));
92 static int glamo_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
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);
101 value |= OUTPUT_BIT(offset);
103 glamo_reg_set_bit_mask(glamo, reg, mask, value);
108 static int glamo_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
110 struct glamo_core *glamo = chip_to_glamo(chip);
111 unsigned int reg = REG_OF_GPIO(offset);
113 glamo_reg_set_bit(glamo, reg, DIRECTION_BIT(offset));
118 static const struct __devinit gpio_chip glamo_gpio_chip = {
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,
127 .ngpio = GLAMO_NR_GPIOS,
129 .owner = THIS_MODULE,
132 static int __devinit glamo_gpio_probe(struct platform_device *pdev)
134 struct glamo_platform_data *pdata = pdev->dev.parent->platform_data;
135 struct glamo_gpio *glamo_gpio;
138 glamo_gpio = kzalloc(sizeof(struct glamo_gpio), GFP_KERNEL);
142 glamo_gpio->glamo = dev_get_drvdata(pdev->dev.parent);
143 glamo_gpio->chip = glamo_gpio_chip;
144 glamo_gpio->chip.dev = &pdev->dev;
146 glamo_gpio->chip.base = pdata->gpio_base;
148 ret = gpiochip_add(&glamo_gpio->chip);
151 dev_err(&pdev->dev, "Could not register gpio chip: %d\n", ret);
155 platform_set_drvdata(pdev, glamo_gpio);
162 static int __devexit glamo_gpio_remove(struct platform_device *pdev)
164 struct glamo_gpio *glamo_gpio = platform_get_drvdata(pdev);
167 ret = gpiochip_remove(&glamo_gpio->chip);
171 platform_set_drvdata(pdev, NULL);
180 static int glamo_gpio_suspend(struct device *dev)
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;
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);
195 static int glamo_gpio_resume(struct device *dev)
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;
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);
207 printk("glamo-gpio resume\n");
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,
218 #define GLAMO_GPIO_PM_OPS (&glamo_pm_ops)
221 #define GLAMO_GPIO_PM_OPS NULL
224 static struct platform_driver glamo_gpio_driver = {
226 .name = "glamo-gpio",
227 .owner = THIS_MODULE,
228 .pm = GLAMO_GPIO_PM_OPS,
230 .probe = glamo_gpio_probe,
231 .remove = __devexit_p(glamo_gpio_remove),
234 static int __devinit glamo_gpio_init(void)
236 return platform_driver_register(&glamo_gpio_driver);
239 static void __exit glamo_gpio_exit(void)
241 platform_driver_unregister(&glamo_gpio_driver);
244 module_init(glamo_gpio_init);
245 module_exit(glamo_gpio_exit);
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");