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

#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
13
#include <linux/of.h>
R
Rabin Vincent 已提交
14
#include <linux/mfd/stmpe.h>
15
#include <linux/seq_file.h>
R
Rabin Vincent 已提交
16 17 18 19 20 21 22 23

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

#define CACHE_NR_REGS	3
24 25
/* No variant has more than 24 GPIOs */
#define CACHE_NR_BANKS	(24 / 8)
R
Rabin Vincent 已提交
26 27 28 29 30 31

struct stmpe_gpio {
	struct gpio_chip chip;
	struct stmpe *stmpe;
	struct device *dev;
	struct mutex irq_lock;
32
	u32 norequest_mask;
R
Rabin Vincent 已提交
33 34 35 36 37 38 39
	/* Caches of interrupt control registers for bus_lock */
	u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
	u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
};

static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
{
40
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
R
Rabin Vincent 已提交
41 42 43 44 45 46 47 48 49
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB] - (offset / 8);
	u8 mask = 1 << (offset % 8);
	int ret;

	ret = stmpe_reg_read(stmpe, reg);
	if (ret < 0)
		return ret;

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

static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
{
55
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
R
Rabin Vincent 已提交
56 57 58 59 60
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
	u8 reg = stmpe->regs[which] - (offset / 8);
	u8 mask = 1 << (offset % 8);

61 62 63 64 65 66 67 68
	/*
	 * Some variants have single register for gpio set/clear functionality.
	 * For them we need to write 0 to clear and 1 to set.
	 */
	if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
		stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
	else
		stmpe_reg_write(stmpe, reg, mask);
R
Rabin Vincent 已提交
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static int stmpe_gpio_get_direction(struct gpio_chip *chip,
				    unsigned offset)
{
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
	u8 mask = 1 << (offset % 8);
	int ret;

	ret = stmpe_reg_read(stmpe, reg);
	if (ret < 0)
		return ret;

	return !(ret & mask);
}

R
Rabin Vincent 已提交
87 88 89
static int stmpe_gpio_direction_output(struct gpio_chip *chip,
					 unsigned offset, int val)
{
90
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
R
Rabin Vincent 已提交
91 92 93 94 95 96 97 98 99 100 101 102
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
	u8 mask = 1 << (offset % 8);

	stmpe_gpio_set(chip, offset, val);

	return stmpe_set_bits(stmpe, reg, mask, mask);
}

static int stmpe_gpio_direction_input(struct gpio_chip *chip,
					unsigned offset)
{
103
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
R
Rabin Vincent 已提交
104 105 106 107 108 109 110 111 112
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
	u8 mask = 1 << (offset % 8);

	return stmpe_set_bits(stmpe, reg, mask, 0);
}

static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
{
113
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
R
Rabin Vincent 已提交
114 115
	struct stmpe *stmpe = stmpe_gpio->stmpe;

116 117 118
	if (stmpe_gpio->norequest_mask & (1 << offset))
		return -EINVAL;

R
Rabin Vincent 已提交
119 120 121 122 123 124
	return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO);
}

static struct gpio_chip template_chip = {
	.label			= "stmpe",
	.owner			= THIS_MODULE,
125
	.get_direction		= stmpe_gpio_get_direction,
R
Rabin Vincent 已提交
126 127 128 129 130
	.direction_input	= stmpe_gpio_direction_input,
	.get			= stmpe_gpio_get,
	.direction_output	= stmpe_gpio_direction_output,
	.set			= stmpe_gpio_set,
	.request		= stmpe_gpio_request,
131
	.can_sleep		= true,
R
Rabin Vincent 已提交
132 133
};

134
static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
R
Rabin Vincent 已提交
135
{
136
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
137
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
138
	int offset = d->hwirq;
R
Rabin Vincent 已提交
139 140 141
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

142
	if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
R
Rabin Vincent 已提交
143 144
		return -EINVAL;

145 146 147 148
	/* STMPE801 doesn't have RE and FE registers */
	if (stmpe_gpio->stmpe->partnum == STMPE801)
		return 0;

149
	if (type & IRQ_TYPE_EDGE_RISING)
R
Rabin Vincent 已提交
150 151 152 153
		stmpe_gpio->regs[REG_RE][regoffset] |= mask;
	else
		stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;

154
	if (type & IRQ_TYPE_EDGE_FALLING)
R
Rabin Vincent 已提交
155 156 157 158 159 160 161
		stmpe_gpio->regs[REG_FE][regoffset] |= mask;
	else
		stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;

	return 0;
}

162
static void stmpe_gpio_irq_lock(struct irq_data *d)
R
Rabin Vincent 已提交
163
{
164
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
165
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
R
Rabin Vincent 已提交
166 167 168 169

	mutex_lock(&stmpe_gpio->irq_lock);
}

170
static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
R
Rabin Vincent 已提交
171
{
172
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
173
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
R
Rabin Vincent 已提交
174 175 176 177 178 179 180 181 182 183
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
	static const u8 regmap[] = {
		[REG_RE]	= STMPE_IDX_GPRER_LSB,
		[REG_FE]	= STMPE_IDX_GPFER_LSB,
		[REG_IE]	= STMPE_IDX_IEGPIOR_LSB,
	};
	int i, j;

	for (i = 0; i < CACHE_NR_REGS; i++) {
184 185 186 187 188
		/* STMPE801 doesn't have RE and FE registers */
		if ((stmpe->partnum == STMPE801) &&
				(i != REG_IE))
			continue;

R
Rabin Vincent 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
		for (j = 0; j < num_banks; j++) {
			u8 old = stmpe_gpio->oldregs[i][j];
			u8 new = stmpe_gpio->regs[i][j];

			if (new == old)
				continue;

			stmpe_gpio->oldregs[i][j] = new;
			stmpe_reg_write(stmpe, stmpe->regs[regmap[i]] - j, new);
		}
	}

	mutex_unlock(&stmpe_gpio->irq_lock);
}

204
static void stmpe_gpio_irq_mask(struct irq_data *d)
R
Rabin Vincent 已提交
205
{
206
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
207
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
208
	int offset = d->hwirq;
R
Rabin Vincent 已提交
209 210 211 212 213 214
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

	stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
}

215
static void stmpe_gpio_irq_unmask(struct irq_data *d)
R
Rabin Vincent 已提交
216
{
217
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
218
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
219
	int offset = d->hwirq;
R
Rabin Vincent 已提交
220 221 222 223 224 225
	int regoffset = offset / 8;
	int mask = 1 << (offset % 8);

	stmpe_gpio->regs[REG_IE][regoffset] |= mask;
}

226 227 228 229
static void stmpe_dbg_show_one(struct seq_file *s,
			       struct gpio_chip *gc,
			       unsigned offset, unsigned gpio)
{
230
	struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	const char *label = gpiochip_is_requested(gc, offset);
	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
	bool val = !!stmpe_gpio_get(gc, offset);
	u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
	u8 mask = 1 << (offset % 8);
	int ret;
	u8 dir;

	ret = stmpe_reg_read(stmpe, dir_reg);
	if (ret < 0)
		return;
	dir = !!(ret & mask);

	if (dir) {
		seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
			   gpio, label ?: "(none)",
			   val ? "hi" : "lo");
	} else {
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
		u8 edge_det_reg;
		u8 rise_reg;
		u8 fall_reg;
		u8 irqen_reg;

		char *edge_det_values[] = {"edge-inactive",
					   "edge-asserted",
					   "not-supported"};
		char *rise_values[] = {"no-rising-edge-detection",
				       "rising-edge-detection",
				       "not-supported"};
		char *fall_values[] = {"no-falling-edge-detection",
				       "falling-edge-detection",
				       "not-supported"};
		#define NOT_SUPPORTED_IDX 2
		u8 edge_det = NOT_SUPPORTED_IDX;
		u8 rise = NOT_SUPPORTED_IDX;
		u8 fall = NOT_SUPPORTED_IDX;
268 269
		bool irqen;

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
		switch (stmpe->partnum) {
		case STMPE610:
		case STMPE811:
		case STMPE1601:
		case STMPE2401:
		case STMPE2403:
			edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_MSB] +
				       num_banks - 1 - (offset / 8);
			ret = stmpe_reg_read(stmpe, edge_det_reg);
			if (ret < 0)
				return;
			edge_det = !!(ret & mask);

		case STMPE1801:
			rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB] -
				   (offset / 8);
			fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB] -
				   (offset / 8);
			ret = stmpe_reg_read(stmpe, rise_reg);
			if (ret < 0)
				return;
			rise = !!(ret & mask);
			ret = stmpe_reg_read(stmpe, fall_reg);
			if (ret < 0)
				return;
			fall = !!(ret & mask);

		case STMPE801:
			irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB] -
				    (offset / 8);
			break;

		default:
303
			return;
304 305
		}

306 307 308 309 310
		ret = stmpe_reg_read(stmpe, irqen_reg);
		if (ret < 0)
			return;
		irqen = !!(ret & mask);

311
		seq_printf(s, " gpio-%-3d (%-20.20s) in  %s %13s %13s %25s %25s",
312 313
			   gpio, label ?: "(none)",
			   val ? "hi" : "lo",
314 315 316 317
			   edge_det_values[edge_det],
			   irqen ? "IRQ-enabled" : "IRQ-disabled",
			   rise_values[rise],
			   fall_values[fall]);
318 319 320 321 322 323 324 325 326 327 328 329 330 331
	}
}

static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
{
	unsigned i;
	unsigned gpio = gc->base;

	for (i = 0; i < gc->ngpio; i++, gpio++) {
		stmpe_dbg_show_one(s, gc, i, gpio);
		seq_printf(s, "\n");
	}
}

R
Rabin Vincent 已提交
332 333
static struct irq_chip stmpe_gpio_irq_chip = {
	.name			= "stmpe-gpio",
334 335 336 337 338
	.irq_bus_lock		= stmpe_gpio_irq_lock,
	.irq_bus_sync_unlock	= stmpe_gpio_irq_sync_unlock,
	.irq_mask		= stmpe_gpio_irq_mask,
	.irq_unmask		= stmpe_gpio_irq_unmask,
	.irq_set_type		= stmpe_gpio_irq_set_type,
R
Rabin Vincent 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
};

static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
{
	struct stmpe_gpio *stmpe_gpio = dev;
	struct stmpe *stmpe = stmpe_gpio->stmpe;
	u8 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
	int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
	u8 status[num_banks];
	int ret;
	int i;

	ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
	if (ret < 0)
		return IRQ_NONE;

	for (i = 0; i < num_banks; i++) {
		int bank = num_banks - i - 1;
		unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
		unsigned int stat = status[i];

		stat &= enabled;
		if (!stat)
			continue;

		while (stat) {
			int bit = __ffs(stat);
			int line = bank * 8 + bit;
367
			int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
368
							 line);
R
Rabin Vincent 已提交
369

370
			handle_nested_irq(child_irq);
R
Rabin Vincent 已提交
371 372 373 374
			stat &= ~(1 << bit);
		}

		stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
375

376 377
		/* Edge detect register is not present on 801 and 1801 */
		if (stmpe->partnum != STMPE801 || stmpe->partnum != STMPE1801)
378 379
			stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_GPEDR_MSB]
					+ i, status[i]);
R
Rabin Vincent 已提交
380 381 382 383 384
	}

	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
385
static int stmpe_gpio_probe(struct platform_device *pdev)
R
Rabin Vincent 已提交
386 387
{
	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
388
	struct device_node *np = pdev->dev.of_node;
R
Rabin Vincent 已提交
389 390
	struct stmpe_gpio *stmpe_gpio;
	int ret;
C
Chris Blair 已提交
391
	int irq = 0;
R
Rabin Vincent 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404

	irq = platform_get_irq(pdev, 0);

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

	mutex_init(&stmpe_gpio->irq_lock);

	stmpe_gpio->dev = &pdev->dev;
	stmpe_gpio->stmpe = stmpe;
	stmpe_gpio->chip = template_chip;
	stmpe_gpio->chip.ngpio = stmpe->num_gpios;
405
	stmpe_gpio->chip.parent = &pdev->dev;
406
	stmpe_gpio->chip.of_node = np;
407
	stmpe_gpio->chip.base = -1;
R
Rabin Vincent 已提交
408

409 410 411
	if (IS_ENABLED(CONFIG_DEBUG_FS))
                stmpe_gpio->chip.dbg_show = stmpe_dbg_show;

412 413
	of_property_read_u32(np, "st,norequest-mask",
			&stmpe_gpio->norequest_mask);
414

415
	if (irq < 0)
C
Chris Blair 已提交
416
		dev_info(&pdev->dev,
417
			"device configured in no-irq mode: "
C
Chris Blair 已提交
418
			"irqs are not available\n");
R
Rabin Vincent 已提交
419 420 421

	ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
	if (ret)
V
Vasiliy Kulikov 已提交
422
		goto out_free;
R
Rabin Vincent 已提交
423

424
	ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio);
425 426 427 428 429
	if (ret) {
		dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
		goto out_disable;
	}

430 431 432 433
	if (irq > 0) {
		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
				stmpe_gpio_irq, IRQF_ONESHOT,
				"stmpe-gpio", stmpe_gpio);
C
Chris Blair 已提交
434 435
		if (ret) {
			dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
436
			goto out_disable;
C
Chris Blair 已提交
437
		}
438 439 440 441 442 443 444 445
		ret =  gpiochip_irqchip_add(&stmpe_gpio->chip,
					    &stmpe_gpio_irq_chip,
					    0,
					    handle_simple_irq,
					    IRQ_TYPE_NONE);
		if (ret) {
			dev_err(&pdev->dev,
				"could not connect irqchip to gpiochip\n");
446
			goto out_disable;
447
		}
R
Rabin Vincent 已提交
448

449 450 451 452
		gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
					     &stmpe_gpio_irq_chip,
					     irq,
					     NULL);
R
Rabin Vincent 已提交
453 454 455 456 457 458
	}

	platform_set_drvdata(pdev, stmpe_gpio);

	return 0;

V
Vasiliy Kulikov 已提交
459 460
out_disable:
	stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
461
	gpiochip_remove(&stmpe_gpio->chip);
R
Rabin Vincent 已提交
462 463 464 465 466 467
out_free:
	kfree(stmpe_gpio);
	return ret;
}

static struct platform_driver stmpe_gpio_driver = {
468 469 470 471
	.driver = {
		.suppress_bind_attrs	= true,
		.name			= "stmpe-gpio",
	},
R
Rabin Vincent 已提交
472 473 474 475 476 477 478 479
	.probe		= stmpe_gpio_probe,
};

static int __init stmpe_gpio_init(void)
{
	return platform_driver_register(&stmpe_gpio_driver);
}
subsys_initcall(stmpe_gpio_init);