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
);
190 dev_info(&pdev
->dev
, "Glamo GPIO registered\n");
192 if (pdata
->registered
)
193 pdata
->registered(&pdev
->dev
);
201 static int __devexit
glamo_gpio_remove(struct platform_device
*pdev
)
203 struct glamo_gpio
*glamo_gpio
= platform_get_drvdata(pdev
);
206 ret
= gpiochip_remove(&glamo_gpio
->chip
);
210 platform_set_drvdata(pdev
, NULL
);
219 static int glamo_gpio_suspend(struct device
*dev
)
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
;
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
);
234 static int glamo_gpio_resume(struct device
*dev
)
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
;
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
);
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
,
255 #define GLAMO_GPIO_PM_OPS (&glamo_pm_ops)
258 #define GLAMO_GPIO_PM_OPS NULL
261 static struct platform_driver glamo_gpio_driver
= {
263 .name
= "glamo-gpio",
264 .owner
= THIS_MODULE
,
265 .pm
= GLAMO_GPIO_PM_OPS
,
267 .probe
= glamo_gpio_probe
,
268 .remove
= __devexit_p(glamo_gpio_remove
),
271 static int __devinit
glamo_gpio_init(void)
273 return platform_driver_register(&glamo_gpio_driver
);
276 static void __exit
glamo_gpio_exit(void)
278 platform_driver_unregister(&glamo_gpio_driver
);
281 module_init(glamo_gpio_init
);
282 module_exit(glamo_gpio_exit
);
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");