1 /* Smedia Glamo 336x/337x gpio driver
3 * (C) 2009 Lars-Peter Clausen
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.
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.
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,
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
26 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/mfd/glamo.h>
31 #include "glamo-core.h"
32 #include "glamo-regs.h"
34 #define GLAMO_NR_GPIO 21
35 #define GLAMO_NR_GPIO_REGS DIV_ROUND_UP(GLAMO_NR_GPIO, 4)
37 #define GLAMO_REG_GPIO(x) (((x) * 2) + GLAMO_REG_GPIO_GEN1)
40 struct glamo_core
*glamo
;
41 struct gpio_chip chip
;
42 uint16_t saved_regs
[GLAMO_NR_GPIO_REGS
];
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))
53 static inline struct glamo_core
*chip_to_glamo(struct gpio_chip
*chip
)
55 return container_of(chip
, struct glamo_gpio
, chip
)->glamo
;
58 static void glamo_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
60 struct glamo_core
*glamo
= chip_to_glamo(chip
);
61 unsigned int reg
= REG_OF_GPIO(offset
);
64 spin_lock(&glamo
->lock
);
65 tmp
= readw(glamo
->base
+ reg
);
67 tmp
|= OUTPUT_BIT(offset
);
69 tmp
&= ~OUTPUT_BIT(offset
);
70 writew(tmp
, glamo
->base
+ reg
);
71 spin_unlock(&glamo
->lock
);
74 static int glamo_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
76 struct glamo_core
*glamo
= chip_to_glamo(chip
);
77 return readw(glamo
->base
+ REG_OF_GPIO(offset
)) & INPUT_BIT(offset
) ? 1 : 0;
80 static int glamo_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
82 struct glamo_core
*glamo
= chip_to_glamo(chip
);
83 unsigned int reg
= REG_OF_GPIO(offset
);
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
);
92 spin_unlock(&glamo
->lock
);
97 static void glamo_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
99 struct glamo_core
*glamo
= chip_to_glamo(chip
);
100 unsigned int reg
= REG_OF_GPIO(offset
);
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
);
109 spin_unlock(&glamo
->lock
);
112 static int glamo_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
115 struct glamo_core
*glamo
= chip_to_glamo(chip
);
116 unsigned int reg
= REG_OF_GPIO(offset
);
119 spin_lock(&glamo
->lock
);
120 tmp
= readw(glamo
->base
+ reg
);
121 tmp
&= ~DIRECTION_BIT(offset
);
124 tmp
|= OUTPUT_BIT(offset
);
126 tmp
&= ~OUTPUT_BIT(offset
);
128 writew(tmp
, glamo
->base
+ reg
);
129 spin_unlock(&glamo
->lock
);
134 static int glamo_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
136 struct glamo_core
*glamo
= chip_to_glamo(chip
);
137 unsigned int reg
= REG_OF_GPIO(offset
);
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
);
146 spin_unlock(&glamo
->lock
);
151 static const struct __devinit gpio_chip glamo_gpio_chip
= {
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
,
160 .ngpio
= GLAMO_NR_GPIO
,
162 .owner
= THIS_MODULE
,
165 static int __devinit
glamo_gpio_probe(struct platform_device
*pdev
)
167 struct glamo_platform_data
*pdata
= pdev
->dev
.parent
->platform_data
;
168 struct glamo_gpio
*glamo_gpio
;
171 glamo_gpio
= kzalloc(sizeof(struct glamo_gpio
), GFP_KERNEL
);
175 glamo_gpio
->glamo
= dev_get_drvdata(pdev
->dev
.parent
);
176 glamo_gpio
->chip
= glamo_gpio_chip
;
177 glamo_gpio
->chip
.dev
= &pdev
->dev
;
179 glamo_gpio
->chip
.base
= pdata
->gpio_base
;
181 ret
= gpiochip_add(&glamo_gpio
->chip
);
184 dev_err(&pdev
->dev
, "Could not register gpio chip: %d\n", ret
);
188 platform_set_drvdata(pdev
, glamo_gpio
);
195 static int __devexit
glamo_gpio_remove(struct platform_device
*pdev
)
197 struct glamo_gpio
*glamo_gpio
= platform_get_drvdata(pdev
);
200 ret
= gpiochip_remove(&glamo_gpio
->chip
);
204 platform_set_drvdata(pdev
, NULL
);
213 static int glamo_gpio_suspend(struct device
*dev
)
215 struct glamo_gpio
*glamo_gpio
= dev_get_drvdata(dev
);
216 struct glamo_core
*glamo
= glamo_gpio
->glamo
;
217 uint16_t *saved_regs
= glamo_gpio
->saved_regs
;
220 spin_lock(&glamo
->lock
);
221 for (i
= 0; i
< GLAMO_NR_GPIO
/ 4; ++i
)
222 saved_regs
[i
] = readw(glamo
->base
+ GLAMO_REG_GPIO(i
));
223 spin_unlock(&glamo
->lock
);
228 static int glamo_gpio_resume(struct device
*dev
)
230 struct glamo_gpio
*glamo_gpio
= dev_get_drvdata(dev
);
231 struct glamo_core
*glamo
= glamo_gpio
->glamo
;
232 uint16_t *saved_regs
= glamo_gpio
->saved_regs
;
235 spin_lock(&glamo
->lock
);
236 for (i
= 0; i
< GLAMO_NR_GPIO_REGS
; ++i
)
237 writew(saved_regs
[i
], glamo
->base
+ GLAMO_REG_GPIO(i
));
238 spin_unlock(&glamo
->lock
);
242 static const struct dev_pm_ops glamo_pm_ops
= {
243 .suspend
= glamo_gpio_suspend
,
244 .resume
= glamo_gpio_resume
,
245 .freeze
= glamo_gpio_suspend
,
246 .thaw
= glamo_gpio_resume
,
249 #define GLAMO_GPIO_PM_OPS (&glamo_pm_ops)
252 #define GLAMO_GPIO_PM_OPS NULL
255 static struct platform_driver glamo_gpio_driver
= {
257 .name
= "glamo-gpio",
258 .owner
= THIS_MODULE
,
259 .pm
= GLAMO_GPIO_PM_OPS
,
261 .probe
= glamo_gpio_probe
,
262 .remove
= __devexit_p(glamo_gpio_remove
),
265 static int __devinit
glamo_gpio_init(void)
267 return platform_driver_register(&glamo_gpio_driver
);
270 static void __exit
glamo_gpio_exit(void)
272 platform_driver_unregister(&glamo_gpio_driver
);
275 module_init(glamo_gpio_init
);
276 module_exit(glamo_gpio_exit
);
278 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
279 MODULE_DESCRIPTION("GPIO interface for the Glamo multimedia device");
280 MODULE_LICENSE("GPL");
281 MODULE_ALIAS("platform:glamo-gpio");