aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/s3c24xx/files-2.6.30/drivers/mfd/glamo/glamo-gpio.c
blob: 6978e6223850f6fff5ced37da20df229bd9f6d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Smedia Glamo 336x/337x gpio driver
 *
 * (C) 2009 Lars-Peter Clausen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/platform_device.h>

#include <linux/gpio.h>
#include <linux/mfd/glamo.h>

#include "glamo-core.h"
#include "glamo-regs.h"

#define GLAMO_NR_GPIO 21
#define GLAMO_NR_GPIO_REGS DIV_ROUND_UP(GLAMO_NR_GPIO, 4)

#define GLAMO_REG_GPIO(x) (((x) * 2) + GLAMO_REG_GPIO_GEN1)

struct glamo_gpio {
	struct glamo_core *glamo;
	struct gpio_chip chip;
	uint16_t saved_regs[GLAMO_NR_GPIO_REGS];
};

#define REG_OF_GPIO(gpio)   (GLAMO_REG_GPIO(gpio >> 2))
#define NUM_OF_GPIO(gpio)	(gpio & 0x3)
#define DIRECTION_BIT(gpio)	(1 << (NUM_OF_GPIO(gpio) + 0))
#define OUTPUT_BIT(gpio)	(1 << (NUM_OF_GPIO(gpio) + 4))
#define INPUT_BIT(gpio)		(1 << (NUM_OF_GPIO(gpio) + 8))
#define FUNC_BIT(gpio)		(1 << (NUM_OF_GPIO(gpio) + 12))


static inline struct glamo_core *chip_to_glamo(struct gpio_chip *chip)
{
	return container_of(chip, struct glamo_gpio, chip)->glamo;
}

static void glamo_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	unsigned int reg = REG_OF_GPIO(offset);
	u_int16_t tmp;

	spin_lock(&glamo->lock);
	tmp = readw(glamo->base + reg);
	if (value)
		tmp |= OUTPUT_BIT(offset);
	else
		tmp &= ~OUTPUT_BIT(offset);
	writew(tmp, glamo->base + reg);
	spin_unlock(&glamo->lock);
}

static int glamo_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	return readw(glamo->base + REG_OF_GPIO(offset)) & INPUT_BIT(offset) ? 1 : 0;
}

static int glamo_gpio_request(struct gpio_chip *chip, unsigned offset)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	unsigned int reg = REG_OF_GPIO(offset);
	u_int16_t tmp;

	spin_lock(&glamo->lock);
	tmp = readw(glamo->base + reg);
	if ((tmp & FUNC_BIT(offset)) == 0) {
		tmp |= FUNC_BIT(offset);
		writew(tmp, glamo->base + reg);
	}
	spin_unlock(&glamo->lock);

	return 0;
}

static void glamo_gpio_free(struct gpio_chip *chip, unsigned offset)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	unsigned int reg = REG_OF_GPIO(offset);
	u_int16_t tmp;

	spin_lock(&glamo->lock);
	tmp = readw(glamo->base + reg);
	if ((tmp & FUNC_BIT(offset)) == 1) {
		tmp &= ~FUNC_BIT(offset);
		writew(tmp, glamo->base + reg);
	}
	spin_unlock(&glamo->lock);
}

static int glamo_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
						int value)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	unsigned int reg = REG_OF_GPIO(offset);
	u_int16_t tmp;

	spin_lock(&glamo->lock);
	tmp = readw(glamo->base + reg);
    tmp &= ~DIRECTION_BIT(offset);

	if (value)
		tmp |= OUTPUT_BIT(offset);
	else
		tmp &= ~OUTPUT_BIT(offset);

	writew(tmp, glamo->base + reg);
	spin_unlock(&glamo->lock);

	return 0;
}

static int glamo_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	struct glamo_core *glamo = chip_to_glamo(chip);
	unsigned int reg = REG_OF_GPIO(offset);
	u_int16_t tmp;

	spin_lock(&glamo->lock);
	tmp = readw(glamo->base + reg);
	if ((tmp & DIRECTION_BIT(offset)) == 0) {
		tmp |= DIRECTION_BIT(offset);
		writew(tmp, glamo->base + reg);
	}
	spin_unlock(&glamo->lock);

	return 0;
}

static const struct __devinit gpio_chip glamo_gpio_chip = {
	.label				= "glamo",
	.request			= glamo_gpio_request,
	.free				= glamo_gpio_free,
	.direction_input	= glamo_gpio_direction_input,
	.get				= glamo_gpio_get,
	.direction_output	= glamo_gpio_direction_output,
	.set				= glamo_gpio_set,
	.base				= -1,
	.ngpio				= GLAMO_NR_GPIO,
	.can_sleep			= 0,
	.owner             = THIS_MODULE,
};

static int __devinit glamo_gpio_probe(struct platform_device *pdev)
{
	struct glamo_platform_data *pdata = pdev->dev.parent->platform_data;
	struct glamo_gpio *glamo_gpio;
	int ret;

	glamo_gpio = kzalloc(sizeof(struct glamo_gpio), GFP_KERNEL);
	if (!glamo_gpio)
		return -ENOMEM;

	glamo_gpio->glamo = dev_get_drvdata(pdev->dev.parent);
	glamo_gpio->chip = glamo_gpio_chip;
	glamo_gpio->chip.dev = &pdev->dev;
	if (pdata)
		glamo_gpio->chip.base = pdata->gpio_base;

	ret = gpiochip_add(&glamo_gpio->chip);

	if (ret) {
		dev_err(&pdev->dev, "Could not register gpio chip: %d\n", ret);
		goto err;
	}

	platform_set_drvdata(pdev, glamo_gpio);
	return 0;
err:
	kfree(glamo_gpio);
	return ret;
}

static int __devexit glamo_gpio_remove(struct platform_device *pdev)
{
	struct glamo_gpio *glamo_gpio = platform_get_drvdata(pdev);
	int ret;

	ret = gpiochip_remove(&glamo_gpio->chip);
	if (!ret)
		goto done;

	platform_set_drvdata(pdev, NULL);
	kfree(glamo_gpio);

done:
	return ret;
}

#ifdef CONFIG_PM

static int glamo_gpio_suspend(struct device *dev)
{
	struct glamo_gpio *glamo_gpio = dev_get_drvdata(dev);
	struct glamo_core *glamo = glamo_gpio->glamo;
	uint16_t *saved_regs = glamo_gpio->saved_regs;
	int i;

	spin_lock(&glamo->lock);
	for (i = 0; i < GLAMO_NR_GPIO / 4; ++i)
		saved_regs[i] = readw(glamo->base + GLAMO_REG_GPIO(i));
	spin_unlock(&glamo->lock);

	return 0;
}

static int glamo_gpio_resume(struct device *dev)
{
	struct glamo_gpio *glamo_gpio = dev_get_drvdata(dev);
	struct glamo_core *glamo = glamo_gpio->glamo;
	uint16_t *saved_regs = glamo_gpio->saved_regs;
	int i;

	spin_lock(&glamo->lock);
	for (i = 0; i < GLAMO_NR_GPIO_REGS; ++i)
		writew(saved_regs[i], glamo->base + GLAMO_REG_GPIO(i));
	spin_unlock(&glamo->lock);
	return 0;
}

static const struct dev_pm_ops glamo_pm_ops = {
	.suspend = glamo_gpio_suspend,
	.resume  = glamo_gpio_resume,
	.freeze  = glamo_gpio_suspend,
	.thaw    = glamo_gpio_resume,
};

#define GLAMO_GPIO_PM_OPS (&glamo_pm_ops)

#else
#define GLAMO_GPIO_PM_OPS NULL
#endif

static struct platform_driver glamo_gpio_driver = {
	.driver = {
		.name  = "glamo-gpio",
		.owner = THIS_MODULE,
		.pm    = GLAMO_GPIO_PM_OPS,
	},
	.probe = glamo_gpio_probe,
	.remove = __devexit_p(glamo_gpio_remove),
};

static int __devinit glamo_gpio_init(void)
{
	return platform_driver_register(&glamo_gpio_driver);
}

static void __exit glamo_gpio_exit(void)
{
	platform_driver_unregister(&glamo_gpio_driver);
}

module_init(glamo_gpio_init);
module_exit(glamo_gpio_exit);

MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
MODULE_DESCRIPTION("GPIO interface for the Glamo multimedia device");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:glamo-gpio");