gpio-tc3589x.c 9.6 KB
Newer Older
R
Rabin Vincent 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
L
Linus Walleij 已提交
12
#include <linux/gpio/driver.h>
13
#include <linux/of.h>
R
Rabin Vincent 已提交
14
#include <linux/interrupt.h>
15
#include <linux/mfd/tc3589x.h>
L
Linus Walleij 已提交
16
#include <linux/bitops.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
	struct device *dev;
	struct mutex irq_lock;
	/* Caches of interrupt control registers for bus_lock */
	u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
	u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
};

37
static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned int offset)
R
Rabin Vincent 已提交
38
{
39
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
40 41
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
L
Linus Walleij 已提交
42
	u8 mask = BIT(offset % 8);
R
Rabin Vincent 已提交
43 44
	int ret;

45
	ret = tc3589x_reg_read(tc3589x, reg);
R
Rabin Vincent 已提交
46 47 48
	if (ret < 0)
		return ret;

49
	return !!(ret & mask);
R
Rabin Vincent 已提交
50 51
}

52
static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned int offset, int val)
R
Rabin Vincent 已提交
53
{
54
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
55 56
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
57
	unsigned int pos = offset % 8;
L
Linus Walleij 已提交
58
	u8 data[] = {val ? BIT(pos) : 0, BIT(pos)};
R
Rabin Vincent 已提交
59

60
	tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data);
R
Rabin Vincent 已提交
61 62
}

63
static int tc3589x_gpio_direction_output(struct gpio_chip *chip,
64
					 unsigned int offset, int val)
R
Rabin Vincent 已提交
65
{
66
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
67 68
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODIR0 + offset / 8;
69
	unsigned int pos = offset % 8;
R
Rabin Vincent 已提交
70

71
	tc3589x_gpio_set(chip, offset, val);
R
Rabin Vincent 已提交
72

L
Linus Walleij 已提交
73
	return tc3589x_set_bits(tc3589x, reg, BIT(pos), BIT(pos));
R
Rabin Vincent 已提交
74 75
}

76
static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
77
					unsigned int offset)
R
Rabin Vincent 已提交
78
{
79
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
80 81
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODIR0 + offset / 8;
82
	unsigned int pos = offset % 8;
R
Rabin Vincent 已提交
83

L
Linus Walleij 已提交
84
	return tc3589x_set_bits(tc3589x, reg, BIT(pos), 0);
R
Rabin Vincent 已提交
85 86
}

87
static int tc3589x_gpio_get_direction(struct gpio_chip *chip,
88
				      unsigned int offset)
89 90 91 92
{
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	u8 reg = TC3589x_GPIODIR0 + offset / 8;
93
	unsigned int pos = offset % 8;
94 95 96 97 98 99
	int ret;

	ret = tc3589x_reg_read(tc3589x, reg);
	if (ret < 0)
		return ret;

100
	return !(ret & BIT(pos));
101 102 103
}

static int tc3589x_gpio_set_single_ended(struct gpio_chip *chip,
104
					 unsigned int offset,
105
					 enum single_ended_mode mode)
106 107 108 109 110 111 112 113 114 115
{
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(chip);
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
	/*
	 * These registers are alterated at each second address
	 * ODM bit 0 = drive to GND or Hi-Z (open drain)
	 * ODM bit 1 = drive to VDD or Hi-Z (open source)
	 */
	u8 odmreg = TC3589x_GPIOODM0 + (offset / 8) * 2;
	u8 odereg = TC3589x_GPIOODE0 + (offset / 8) * 2;
116
	unsigned int pos = offset % 8;
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
	int ret;

	switch(mode) {
	case LINE_MODE_OPEN_DRAIN:
		/* Set open drain mode */
		ret = tc3589x_set_bits(tc3589x, odmreg, BIT(pos), 0);
		if (ret)
			return ret;
		/* Enable open drain/source mode */
		return tc3589x_set_bits(tc3589x, odereg, BIT(pos), BIT(pos));
	case LINE_MODE_OPEN_SOURCE:
		/* Set open source mode */
		ret = tc3589x_set_bits(tc3589x, odmreg, BIT(pos), BIT(pos));
		if (ret)
			return ret;
		/* Enable open drain/source mode */
		return tc3589x_set_bits(tc3589x, odereg, BIT(pos), BIT(pos));
	case LINE_MODE_PUSH_PULL:
		/* Disable open drain/source mode */
		return tc3589x_set_bits(tc3589x, odereg, BIT(pos), 0);
	default:
		break;
	}
	return -ENOTSUPP;
}

143
static const struct gpio_chip template_chip = {
144
	.label			= "tc3589x",
R
Rabin Vincent 已提交
145
	.owner			= THIS_MODULE,
146 147
	.get			= tc3589x_gpio_get,
	.set			= tc3589x_gpio_set,
148 149 150 151
	.direction_output	= tc3589x_gpio_direction_output,
	.direction_input	= tc3589x_gpio_direction_input,
	.get_direction		= tc3589x_gpio_get_direction,
	.set_single_ended	= tc3589x_gpio_set_single_ended,
152
	.can_sleep		= true,
R
Rabin Vincent 已提交
153 154
};

155
static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type)
R
Rabin Vincent 已提交
156
{
157
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
158
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(gc);
159
	int offset = d->hwirq;
R
Rabin Vincent 已提交
160
	int regoffset = offset / 8;
L
Linus Walleij 已提交
161
	int mask = BIT(offset % 8);
R
Rabin Vincent 已提交
162 163

	if (type == IRQ_TYPE_EDGE_BOTH) {
164
		tc3589x_gpio->regs[REG_IBE][regoffset] |= mask;
R
Rabin Vincent 已提交
165 166 167
		return 0;
	}

168
	tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask;
R
Rabin Vincent 已提交
169 170

	if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
171
		tc3589x_gpio->regs[REG_IS][regoffset] |= mask;
R
Rabin Vincent 已提交
172
	else
173
		tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask;
R
Rabin Vincent 已提交
174 175

	if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
176
		tc3589x_gpio->regs[REG_IEV][regoffset] |= mask;
R
Rabin Vincent 已提交
177
	else
178
		tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask;
R
Rabin Vincent 已提交
179 180 181 182

	return 0;
}

183
static void tc3589x_gpio_irq_lock(struct irq_data *d)
R
Rabin Vincent 已提交
184
{
185
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
186
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(gc);
R
Rabin Vincent 已提交
187

188
	mutex_lock(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
189 190
}

191
static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
R
Rabin Vincent 已提交
192
{
193
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
194
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(gc);
195
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
R
Rabin Vincent 已提交
196
	static const u8 regmap[] = {
197 198 199 200
		[REG_IBE]	= TC3589x_GPIOIBE0,
		[REG_IEV]	= TC3589x_GPIOIEV0,
		[REG_IS]	= TC3589x_GPIOIS0,
		[REG_IE]	= TC3589x_GPIOIE0,
R
Rabin Vincent 已提交
201 202 203 204 205
	};
	int i, j;

	for (i = 0; i < CACHE_NR_REGS; i++) {
		for (j = 0; j < CACHE_NR_BANKS; j++) {
206 207
			u8 old = tc3589x_gpio->oldregs[i][j];
			u8 new = tc3589x_gpio->regs[i][j];
R
Rabin Vincent 已提交
208 209 210 211

			if (new == old)
				continue;

212 213
			tc3589x_gpio->oldregs[i][j] = new;
			tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new);
R
Rabin Vincent 已提交
214 215 216
		}
	}

217
	mutex_unlock(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
218 219
}

220
static void tc3589x_gpio_irq_mask(struct irq_data *d)
R
Rabin Vincent 已提交
221
{
222
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
223
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(gc);
224
	int offset = d->hwirq;
R
Rabin Vincent 已提交
225
	int regoffset = offset / 8;
L
Linus Walleij 已提交
226
	int mask = BIT(offset % 8);
R
Rabin Vincent 已提交
227

228
	tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
R
Rabin Vincent 已提交
229 230
}

231
static void tc3589x_gpio_irq_unmask(struct irq_data *d)
R
Rabin Vincent 已提交
232
{
233
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
234
	struct tc3589x_gpio *tc3589x_gpio = gpiochip_get_data(gc);
235
	int offset = d->hwirq;
R
Rabin Vincent 已提交
236
	int regoffset = offset / 8;
L
Linus Walleij 已提交
237
	int mask = BIT(offset % 8);
R
Rabin Vincent 已提交
238

239
	tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
R
Rabin Vincent 已提交
240 241
}

242 243
static struct irq_chip tc3589x_gpio_irq_chip = {
	.name			= "tc3589x-gpio",
244 245 246 247 248
	.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 已提交
249 250
};

251
static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
R
Rabin Vincent 已提交
252
{
253 254
	struct tc3589x_gpio *tc3589x_gpio = dev;
	struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
R
Rabin Vincent 已提交
255 256 257 258
	u8 status[CACHE_NR_BANKS];
	int ret;
	int i;

259
	ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0,
R
Rabin Vincent 已提交
260 261 262 263 264 265 266 267 268 269 270 271
				 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;
272 273
			int irq = irq_find_mapping(tc3589x_gpio->chip.irqdomain,
						   line);
R
Rabin Vincent 已提交
274

275
			handle_nested_irq(irq);
R
Rabin Vincent 已提交
276 277 278
			stat &= ~(1 << bit);
		}

279
		tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]);
R
Rabin Vincent 已提交
280 281 282 283 284
	}

	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
285
static int tc3589x_gpio_probe(struct platform_device *pdev)
R
Rabin Vincent 已提交
286
{
287
	struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
288
	struct device_node *np = pdev->dev.of_node;
289
	struct tc3589x_gpio *tc3589x_gpio;
R
Rabin Vincent 已提交
290 291 292
	int ret;
	int irq;

293 294
	if (!np) {
		dev_err(&pdev->dev, "No Device Tree node found\n");
295 296
		return -EINVAL;
	}
R
Rabin Vincent 已提交
297 298 299 300 301

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

302 303
	tc3589x_gpio = devm_kzalloc(&pdev->dev, sizeof(struct tc3589x_gpio),
				    GFP_KERNEL);
304
	if (!tc3589x_gpio)
R
Rabin Vincent 已提交
305 306
		return -ENOMEM;

307
	mutex_init(&tc3589x_gpio->irq_lock);
R
Rabin Vincent 已提交
308

309 310
	tc3589x_gpio->dev = &pdev->dev;
	tc3589x_gpio->tc3589x = tc3589x;
R
Rabin Vincent 已提交
311

312 313
	tc3589x_gpio->chip = template_chip;
	tc3589x_gpio->chip.ngpio = tc3589x->num_gpio;
314
	tc3589x_gpio->chip.parent = &pdev->dev;
315
	tc3589x_gpio->chip.base = -1;
316
	tc3589x_gpio->chip.of_node = np;
R
Rabin Vincent 已提交
317 318

	/* Bring the GPIO module out of reset */
319 320
	ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL,
			       TC3589x_RSTCTRL_GPIRST, 0);
R
Rabin Vincent 已提交
321
	if (ret < 0)
322
		return ret;
R
Rabin Vincent 已提交
323

324 325 326 327
	ret = devm_request_threaded_irq(&pdev->dev,
					irq, NULL, tc3589x_gpio_irq,
					IRQF_ONESHOT, "tc3589x-gpio",
					tc3589x_gpio);
R
Rabin Vincent 已提交
328 329
	if (ret) {
		dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
330
		return ret;
R
Rabin Vincent 已提交
331 332
	}

333 334
	ret = devm_gpiochip_add_data(&pdev->dev, &tc3589x_gpio->chip,
				     tc3589x_gpio);
R
Rabin Vincent 已提交
335 336
	if (ret) {
		dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
337
		return ret;
R
Rabin Vincent 已提交
338 339
	}

340 341 342 343 344
	ret =  gpiochip_irqchip_add_nested(&tc3589x_gpio->chip,
					   &tc3589x_gpio_irq_chip,
					   0,
					   handle_simple_irq,
					   IRQ_TYPE_NONE);
345 346 347 348 349 350
	if (ret) {
		dev_err(&pdev->dev,
			"could not connect irqchip to gpiochip\n");
		return ret;
	}

351 352 353
	gpiochip_set_nested_irqchip(&tc3589x_gpio->chip,
				    &tc3589x_gpio_irq_chip,
				    irq);
354

355
	platform_set_drvdata(pdev, tc3589x_gpio);
R
Rabin Vincent 已提交
356 357 358 359

	return 0;
}

360 361 362
static struct platform_driver tc3589x_gpio_driver = {
	.driver.name	= "tc3589x-gpio",
	.probe		= tc3589x_gpio_probe,
R
Rabin Vincent 已提交
363 364
};

365
static int __init tc3589x_gpio_init(void)
R
Rabin Vincent 已提交
366
{
367
	return platform_driver_register(&tc3589x_gpio_driver);
R
Rabin Vincent 已提交
368
}
369
subsys_initcall(tc3589x_gpio_init);