gpio-tc3589x.c 9.6 KB
Newer Older
R
Rabin Vincent 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * License Terms: GNU General Public License, version 2
 * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
16
#include <linux/mfd/tc3589x.h>
R
Rabin Vincent 已提交
17 18 19 20 21 22 23 24 25 26

/*
 * These registers are modified under the irq bus lock and cached to avoid
 * unnecessary writes in bus_sync_unlock.
 */
enum { REG_IBE, REG_IEV, REG_IS, REG_IE };

#define CACHE_NR_REGS	4
#define CACHE_NR_BANKS	3

27
struct tc3589x_gpio {
R
Rabin Vincent 已提交
28
	struct gpio_chip chip;
29
	struct tc3589x *tc3589x;
R
Rabin Vincent 已提交
30 31 32 33 34 35 36 37 38 39
	struct device *dev;
	struct mutex irq_lock;

	int irq_base;

	/* Caches of interrupt control registers for bus_lock */
	u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
	u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
};

40
static inline struct tc3589x_gpio *to_tc3589x_gpio(struct gpio_chip *chip)
R
Rabin Vincent 已提交
41
{
42
	return container_of(chip, struct tc3589x_gpio, chip);
R
Rabin Vincent 已提交
43 44
}

45
static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned offset)
R
Rabin Vincent 已提交
46
{
47 48 49
	struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
R
Rabin Vincent 已提交
50 51 52
	u8 mask = 1 << (offset % 8);
	int ret;

53
	ret = tc3589x_reg_read(tc3589x, reg);
R
Rabin Vincent 已提交
54 55 56 57 58 59
	if (ret < 0)
		return ret;

	return ret & mask;
}

60
static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
R
Rabin Vincent 已提交
61
{
62 63 64
	struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
R
Rabin Vincent 已提交
65 66 67
	unsigned pos = offset % 8;
	u8 data[] = {!!val << pos, 1 << pos};

68
	tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data);
R
Rabin Vincent 已提交
69 70
}

71
static int tc3589x_gpio_direction_output(struct gpio_chip *chip,
R
Rabin Vincent 已提交
72 73
					 unsigned offset, int val)
{
74 75 76
	struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODIR0 + offset / 8;
R
Rabin Vincent 已提交
77 78
	unsigned pos = offset % 8;

79
	tc3589x_gpio_set(chip, offset, val);
R
Rabin Vincent 已提交
80

81
	return tc3589x_set_bits(tc3589x, reg, 1 << pos, 1 << pos);
R
Rabin Vincent 已提交
82 83
}

84
static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
R
Rabin Vincent 已提交
85 86
					unsigned offset)
{
87 88 89
	struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODIR0 + offset / 8;
R
Rabin Vincent 已提交
90 91
	unsigned pos = offset % 8;

92
	return tc3589x_set_bits(tc3589x, reg, 1 << pos, 0);
R
Rabin Vincent 已提交
93 94
}

95
static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
R
Rabin Vincent 已提交
96
{
97
	struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
R
Rabin Vincent 已提交
98

99
	return tc3589x_gpio->irq_base + offset;
R
Rabin Vincent 已提交
100 101 102
}

static struct gpio_chip template_chip = {
103
	.label			= "tc3589x",
R
Rabin Vincent 已提交
104
	.owner			= THIS_MODULE,
105 106 107 108 109
	.direction_input	= tc3589x_gpio_direction_input,
	.get			= tc3589x_gpio_get,
	.direction_output	= tc3589x_gpio_direction_output,
	.set			= tc3589x_gpio_set,
	.to_irq			= tc3589x_gpio_to_irq,
R
Rabin Vincent 已提交
110 111 112
	.can_sleep		= 1,
};

113
static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type)
R
Rabin Vincent 已提交
114
{
115 116
	struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tc3589x_gpio->irq_base;
R
Rabin Vincent 已提交
117 118 119 120
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

	if (type == IRQ_TYPE_EDGE_BOTH) {
121
		tc3589x_gpio->regs[REG_IBE][regoffset] |= mask;
R
Rabin Vincent 已提交
122 123 124
		return 0;
	}

125
	tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask;
R
Rabin Vincent 已提交
126 127

	if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
128
		tc3589x_gpio->regs[REG_IS][regoffset] |= mask;
R
Rabin Vincent 已提交
129
	else
130
		tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask;
R
Rabin Vincent 已提交
131 132

	if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
133
		tc3589x_gpio->regs[REG_IEV][regoffset] |= mask;
R
Rabin Vincent 已提交
134
	else
135
		tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask;
R
Rabin Vincent 已提交
136 137 138 139

	return 0;
}

140
static void tc3589x_gpio_irq_lock(struct irq_data *d)
R
Rabin Vincent 已提交
141
{
142
	struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
R
Rabin Vincent 已提交
143

144
	mutex_lock(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
145 146
}

147
static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
R
Rabin Vincent 已提交
148
{
149
	struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
150
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
R
Rabin Vincent 已提交
151
	static const u8 regmap[] = {
152 153 154 155
		[REG_IBE]	= TC3589x_GPIOIBE0,
		[REG_IEV]	= TC3589x_GPIOIEV0,
		[REG_IS]	= TC3589x_GPIOIS0,
		[REG_IE]	= TC3589x_GPIOIE0,
R
Rabin Vincent 已提交
156 157 158 159 160
	};
	int i, j;

	for (i = 0; i < CACHE_NR_REGS; i++) {
		for (j = 0; j < CACHE_NR_BANKS; j++) {
161 162
			u8 old = tc3589x_gpio->oldregs[i][j];
			u8 new = tc3589x_gpio->regs[i][j];
R
Rabin Vincent 已提交
163 164 165 166

			if (new == old)
				continue;

167 168
			tc3589x_gpio->oldregs[i][j] = new;
			tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new);
R
Rabin Vincent 已提交
169 170 171
		}
	}

172
	mutex_unlock(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
173 174
}

175
static void tc3589x_gpio_irq_mask(struct irq_data *d)
R
Rabin Vincent 已提交
176
{
177 178
	struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tc3589x_gpio->irq_base;
R
Rabin Vincent 已提交
179 180 181
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

182
	tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
R
Rabin Vincent 已提交
183 184
}

185
static void tc3589x_gpio_irq_unmask(struct irq_data *d)
R
Rabin Vincent 已提交
186
{
187 188
	struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tc3589x_gpio->irq_base;
R
Rabin Vincent 已提交
189 190 191
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

192
	tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
R
Rabin Vincent 已提交
193 194
}

195 196
static struct irq_chip tc3589x_gpio_irq_chip = {
	.name			= "tc3589x-gpio",
197 198 199 200 201
	.irq_bus_lock		= tc3589x_gpio_irq_lock,
	.irq_bus_sync_unlock	= tc3589x_gpio_irq_sync_unlock,
	.irq_mask		= tc3589x_gpio_irq_mask,
	.irq_unmask		= tc3589x_gpio_irq_unmask,
	.irq_set_type		= tc3589x_gpio_irq_set_type,
R
Rabin Vincent 已提交
202 203
};

204
static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
R
Rabin Vincent 已提交
205
{
206 207
	struct tc3589x_gpio *tc3589x_gpio = dev;
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
R
Rabin Vincent 已提交
208 209 210 211
	u8 status[CACHE_NR_BANKS];
	int ret;
	int i;

212
	ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0,
R
Rabin Vincent 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225
				 ARRAY_SIZE(status), status);
	if (ret < 0)
		return IRQ_NONE;

	for (i = 0; i < ARRAY_SIZE(status); i++) {
		unsigned int stat = status[i];
		if (!stat)
			continue;

		while (stat) {
			int bit = __ffs(stat);
			int line = i * 8 + bit;

226
			handle_nested_irq(tc3589x_gpio->irq_base + line);
R
Rabin Vincent 已提交
227 228 229
			stat &= ~(1 << bit);
		}

230
		tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]);
R
Rabin Vincent 已提交
231 232 233 234 235
	}

	return IRQ_HANDLED;
}

236
static int tc3589x_gpio_irq_init(struct tc3589x_gpio *tc3589x_gpio)
R
Rabin Vincent 已提交
237
{
238
	int base = tc3589x_gpio->irq_base;
R
Rabin Vincent 已提交
239 240
	int irq;

241
	for (irq = base; irq < base + tc3589x_gpio->chip.ngpio; irq++) {
T
Thomas Gleixner 已提交
242 243
		irq_set_chip_data(irq, tc3589x_gpio);
		irq_set_chip_and_handler(irq, &tc3589x_gpio_irq_chip,
R
Rabin Vincent 已提交
244
					 handle_simple_irq);
T
Thomas Gleixner 已提交
245
		irq_set_nested_thread(irq, 1);
R
Rabin Vincent 已提交
246 247 248
#ifdef CONFIG_ARM
		set_irq_flags(irq, IRQF_VALID);
#else
T
Thomas Gleixner 已提交
249
		irq_set_noprobe(irq);
R
Rabin Vincent 已提交
250 251 252 253 254 255
#endif
	}

	return 0;
}

256
static void tc3589x_gpio_irq_remove(struct tc3589x_gpio *tc3589x_gpio)
R
Rabin Vincent 已提交
257
{
258
	int base = tc3589x_gpio->irq_base;
R
Rabin Vincent 已提交
259 260
	int irq;

261
	for (irq = base; irq < base + tc3589x_gpio->chip.ngpio; irq++) {
R
Rabin Vincent 已提交
262 263 264
#ifdef CONFIG_ARM
		set_irq_flags(irq, 0);
#endif
T
Thomas Gleixner 已提交
265 266
		irq_set_chip_and_handler(irq, NULL, NULL);
		irq_set_chip_data(irq, NULL);
R
Rabin Vincent 已提交
267 268 269
	}
}

270
static int __devinit tc3589x_gpio_probe(struct platform_device *pdev)
R
Rabin Vincent 已提交
271
{
272 273 274
	struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
	struct tc3589x_gpio_platform_data *pdata;
	struct tc3589x_gpio *tc3589x_gpio;
R
Rabin Vincent 已提交
275 276 277
	int ret;
	int irq;

278
	pdata = tc3589x->pdata->gpio;
R
Rabin Vincent 已提交
279 280 281 282 283 284 285
	if (!pdata)
		return -ENODEV;

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;

286 287
	tc3589x_gpio = kzalloc(sizeof(struct tc3589x_gpio), GFP_KERNEL);
	if (!tc3589x_gpio)
R
Rabin Vincent 已提交
288 289
		return -ENOMEM;

290
	mutex_init(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
291

292 293
	tc3589x_gpio->dev = &pdev->dev;
	tc3589x_gpio->tc3589x = tc3589x;
R
Rabin Vincent 已提交
294

295 296 297 298
	tc3589x_gpio->chip = template_chip;
	tc3589x_gpio->chip.ngpio = tc3589x->num_gpio;
	tc3589x_gpio->chip.dev = &pdev->dev;
	tc3589x_gpio->chip.base = pdata->gpio_base;
R
Rabin Vincent 已提交
299

300
	tc3589x_gpio->irq_base = tc3589x->irq_base + TC3589x_INT_GPIO(0);
R
Rabin Vincent 已提交
301 302

	/* Bring the GPIO module out of reset */
303 304
	ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL,
			       TC3589x_RSTCTRL_GPIRST, 0);
R
Rabin Vincent 已提交
305 306 307
	if (ret < 0)
		goto out_free;

308
	ret = tc3589x_gpio_irq_init(tc3589x_gpio);
R
Rabin Vincent 已提交
309 310 311
	if (ret)
		goto out_free;

312 313
	ret = request_threaded_irq(irq, NULL, tc3589x_gpio_irq, IRQF_ONESHOT,
				   "tc3589x-gpio", tc3589x_gpio);
R
Rabin Vincent 已提交
314 315 316 317 318
	if (ret) {
		dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
		goto out_removeirq;
	}

319
	ret = gpiochip_add(&tc3589x_gpio->chip);
R
Rabin Vincent 已提交
320 321 322 323 324
	if (ret) {
		dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
		goto out_freeirq;
	}

325
	if (pdata->setup)
326
		pdata->setup(tc3589x, tc3589x_gpio->chip.base);
327

328
	platform_set_drvdata(pdev, tc3589x_gpio);
R
Rabin Vincent 已提交
329 330 331 332

	return 0;

out_freeirq:
333
	free_irq(irq, tc3589x_gpio);
R
Rabin Vincent 已提交
334
out_removeirq:
335
	tc3589x_gpio_irq_remove(tc3589x_gpio);
R
Rabin Vincent 已提交
336
out_free:
337
	kfree(tc3589x_gpio);
R
Rabin Vincent 已提交
338 339 340
	return ret;
}

341
static int __devexit tc3589x_gpio_remove(struct platform_device *pdev)
R
Rabin Vincent 已提交
342
{
343 344 345
	struct tc3589x_gpio *tc3589x_gpio = platform_get_drvdata(pdev);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	struct tc3589x_gpio_platform_data *pdata = tc3589x->pdata->gpio;
R
Rabin Vincent 已提交
346 347 348
	int irq = platform_get_irq(pdev, 0);
	int ret;

349
	if (pdata->remove)
350
		pdata->remove(tc3589x, tc3589x_gpio->chip.base);
351

352
	ret = gpiochip_remove(&tc3589x_gpio->chip);
R
Rabin Vincent 已提交
353
	if (ret < 0) {
354
		dev_err(tc3589x_gpio->dev,
R
Rabin Vincent 已提交
355 356 357 358
			"unable to remove gpiochip: %d\n", ret);
		return ret;
	}

359 360
	free_irq(irq, tc3589x_gpio);
	tc3589x_gpio_irq_remove(tc3589x_gpio);
R
Rabin Vincent 已提交
361 362

	platform_set_drvdata(pdev, NULL);
363
	kfree(tc3589x_gpio);
R
Rabin Vincent 已提交
364 365 366 367

	return 0;
}

368 369
static struct platform_driver tc3589x_gpio_driver = {
	.driver.name	= "tc3589x-gpio",
R
Rabin Vincent 已提交
370
	.driver.owner	= THIS_MODULE,
371 372
	.probe		= tc3589x_gpio_probe,
	.remove		= __devexit_p(tc3589x_gpio_remove),
R
Rabin Vincent 已提交
373 374
};

375
static int __init tc3589x_gpio_init(void)
R
Rabin Vincent 已提交
376
{
377
	return platform_driver_register(&tc3589x_gpio_driver);
R
Rabin Vincent 已提交
378
}
379
subsys_initcall(tc3589x_gpio_init);
R
Rabin Vincent 已提交
380

381
static void __exit tc3589x_gpio_exit(void)
R
Rabin Vincent 已提交
382
{
383
	platform_driver_unregister(&tc3589x_gpio_driver);
R
Rabin Vincent 已提交
384
}
385
module_exit(tc3589x_gpio_exit);
R
Rabin Vincent 已提交
386 387

MODULE_LICENSE("GPL v2");
388
MODULE_DESCRIPTION("TC3589x GPIO driver");
R
Rabin Vincent 已提交
389
MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");