stmpe-keypad.c 10.6 KB
Newer Older
R
Rabin Vincent 已提交
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
/*
 * 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/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/input/matrix_keypad.h>
#include <linux/mfd/stmpe.h>

/* These are at the same addresses in all STMPE variants */
#define STMPE_KPC_COL			0x60
#define STMPE_KPC_ROW_MSB		0x61
#define STMPE_KPC_ROW_LSB		0x62
#define STMPE_KPC_CTRL_MSB		0x63
#define STMPE_KPC_CTRL_LSB		0x64
#define STMPE_KPC_COMBI_KEY_0		0x65
#define STMPE_KPC_COMBI_KEY_1		0x66
#define STMPE_KPC_COMBI_KEY_2		0x67
#define STMPE_KPC_DATA_BYTE0		0x68
#define STMPE_KPC_DATA_BYTE1		0x69
#define STMPE_KPC_DATA_BYTE2		0x6a
#define STMPE_KPC_DATA_BYTE3		0x6b
#define STMPE_KPC_DATA_BYTE4		0x6c

#define STMPE_KPC_CTRL_LSB_SCAN		(0x1 << 0)
#define STMPE_KPC_CTRL_LSB_DEBOUNCE	(0x7f << 1)
#define STMPE_KPC_CTRL_MSB_SCAN_COUNT	(0xf << 4)

#define STMPE_KPC_ROW_MSB_ROWS		0xff

#define STMPE_KPC_DATA_UP		(0x1 << 7)
#define STMPE_KPC_DATA_ROW		(0xf << 3)
#define STMPE_KPC_DATA_COL		(0x7 << 0)
#define STMPE_KPC_DATA_NOKEY_MASK	0x78

#define STMPE_KEYPAD_MAX_DEBOUNCE	127
#define STMPE_KEYPAD_MAX_SCAN_COUNT	15

#define STMPE_KEYPAD_MAX_ROWS		8
#define STMPE_KEYPAD_MAX_COLS		8
#define STMPE_KEYPAD_ROW_SHIFT		3
48
#define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
R
Rabin Vincent 已提交
49 50
	(STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)

51 52 53 54 55 56 57 58

#define STMPE1601_NUM_DATA	5
#define STMPE2401_NUM_DATA	3
#define STMPE2403_NUM_DATA	5

/* Make sure it covers all cases above */
#define MAX_NUM_DATA		5

R
Rabin Vincent 已提交
59 60 61 62
/**
 * struct stmpe_keypad_variant - model-specific attributes
 * @auto_increment: whether the KPC_DATA_BYTE register address
 *		    auto-increments on multiple read
63
 * @set_pullup: whether the pins need to have their pull-ups set
R
Rabin Vincent 已提交
64 65 66 67 68 69 70 71 72
 * @num_data: number of data bytes
 * @num_normal_data: number of normal keys' data bytes
 * @max_cols: maximum number of columns supported
 * @max_rows: maximum number of rows supported
 * @col_gpios: bitmask of gpios which can be used for columns
 * @row_gpios: bitmask of gpios which can be used for rows
 */
struct stmpe_keypad_variant {
	bool		auto_increment;
73
	bool		set_pullup;
R
Rabin Vincent 已提交
74 75 76 77 78 79 80 81 82 83 84
	int		num_data;
	int		num_normal_data;
	int		max_cols;
	int		max_rows;
	unsigned int	col_gpios;
	unsigned int	row_gpios;
};

static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
	[STMPE1601] = {
		.auto_increment		= true,
85
		.num_data		= STMPE1601_NUM_DATA,
R
Rabin Vincent 已提交
86 87 88 89 90 91 92 93
		.num_normal_data	= 3,
		.max_cols		= 8,
		.max_rows		= 8,
		.col_gpios		= 0x000ff,	/* GPIO 0 - 7 */
		.row_gpios		= 0x0ff00,	/* GPIO 8 - 15 */
	},
	[STMPE2401] = {
		.auto_increment		= false,
94
		.set_pullup		= true,
95
		.num_data		= STMPE2401_NUM_DATA,
R
Rabin Vincent 已提交
96 97 98 99
		.num_normal_data	= 2,
		.max_cols		= 8,
		.max_rows		= 12,
		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
100
		.row_gpios		= 0x1f7f00,	/* GPIO 8-14, 16-20 */
R
Rabin Vincent 已提交
101 102 103
	},
	[STMPE2403] = {
		.auto_increment		= true,
104
		.set_pullup		= true,
105
		.num_data		= STMPE2403_NUM_DATA,
R
Rabin Vincent 已提交
106 107 108 109 110 111 112 113
		.num_normal_data	= 3,
		.max_cols		= 8,
		.max_rows		= 12,
		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
		.row_gpios		= 0x1fef00,	/* GPIO 8-14, 16-20 */
	},
};

114 115 116 117 118 119 120 121 122 123 124 125 126 127
/**
 * struct stmpe_keypad - STMPE keypad state container
 * @stmpe: pointer to parent STMPE device
 * @input: spawned input device
 * @variant: STMPE variant
 * @debounce_ms: debounce interval, in ms.  Maximum is
 *		 %STMPE_KEYPAD_MAX_DEBOUNCE.
 * @scan_count: number of key scanning cycles to confirm key data.
 *		Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
 * @no_autorepeat: disable key autorepeat
 * @rows: bitmask for the rows
 * @cols: bitmask for the columns
 * @keymap: the keymap
 */
R
Rabin Vincent 已提交
128 129 130 131
struct stmpe_keypad {
	struct stmpe *stmpe;
	struct input_dev *input;
	const struct stmpe_keypad_variant *variant;
132 133 134
	unsigned int debounce_ms;
	unsigned int scan_count;
	bool no_autorepeat;
R
Rabin Vincent 已提交
135 136
	unsigned int rows;
	unsigned int cols;
137
	unsigned short keymap[STMPE_KEYPAD_KEYMAP_MAX_SIZE];
R
Rabin Vincent 已提交
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
};

static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
{
	const struct stmpe_keypad_variant *variant = keypad->variant;
	struct stmpe *stmpe = keypad->stmpe;
	int ret;
	int i;

	if (variant->auto_increment)
		return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
					variant->num_data, data);

	for (i = 0; i < variant->num_data; i++) {
		ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
		if (ret < 0)
			return ret;

		data[i] = ret;
	}

	return 0;
}

static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
{
	struct stmpe_keypad *keypad = dev;
	struct input_dev *input = keypad->input;
	const struct stmpe_keypad_variant *variant = keypad->variant;
167
	u8 fifo[MAX_NUM_DATA];
R
Rabin Vincent 已提交
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
	int ret;
	int i;

	ret = stmpe_keypad_read_data(keypad, fifo);
	if (ret < 0)
		return IRQ_NONE;

	for (i = 0; i < variant->num_normal_data; i++) {
		u8 data = fifo[i];
		int row = (data & STMPE_KPC_DATA_ROW) >> 3;
		int col = data & STMPE_KPC_DATA_COL;
		int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
		bool up = data & STMPE_KPC_DATA_UP;

		if ((data & STMPE_KPC_DATA_NOKEY_MASK)
			== STMPE_KPC_DATA_NOKEY_MASK)
			continue;

		input_event(input, EV_MSC, MSC_SCAN, code);
		input_report_key(input, keypad->keymap[code], !up);
		input_sync(input);
	}

	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
194
static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
R
Rabin Vincent 已提交
195 196 197 198 199
{
	const struct stmpe_keypad_variant *variant = keypad->variant;
	unsigned int col_gpios = variant->col_gpios;
	unsigned int row_gpios = variant->row_gpios;
	struct stmpe *stmpe = keypad->stmpe;
200
	u8 pureg = stmpe->regs[STMPE_IDX_GPPUR_LSB];
R
Rabin Vincent 已提交
201
	unsigned int pins = 0;
202 203
	unsigned int pu_pins = 0;
	int ret;
R
Rabin Vincent 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	int i;

	/*
	 * Figure out which pins need to be set to the keypad alternate
	 * function.
	 *
	 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
	 * for the keypad.
	 *
	 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
	 * for the keypad) are used on the board.
	 */

	for (i = 0; i < variant->max_cols; i++) {
		int num = __ffs(col_gpios);

220
		if (keypad->cols & (1 << i)) {
R
Rabin Vincent 已提交
221
			pins |= 1 << num;
222 223
			pu_pins |= 1 << num;
		}
R
Rabin Vincent 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236

		col_gpios &= ~(1 << num);
	}

	for (i = 0; i < variant->max_rows; i++) {
		int num = __ffs(row_gpios);

		if (keypad->rows & (1 << i))
			pins |= 1 << num;

		row_gpios &= ~(1 << num);
	}

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
	ret = stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
	if (ret)
		return ret;

	/*
	 * On STMPE24xx, set pin bias to pull-up on all keypad input
	 * pins (columns), this incidentally happen to be maximum 8 pins
	 * and placed at GPIO0-7 so only the LSB of the pull up register
	 * ever needs to be written.
	 */
	if (variant->set_pullup) {
		u8 val;

		ret = stmpe_reg_read(stmpe, pureg);
		if (ret)
			return ret;

		/* Do not touch unused pins, may be used for GPIO */
		val = ret & ~pu_pins;
		val |= pu_pins;

		ret = stmpe_reg_write(stmpe, pureg, val);
	}

	return 0;
R
Rabin Vincent 已提交
262 263
}

B
Bill Pemberton 已提交
264
static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
R
Rabin Vincent 已提交
265 266 267 268 269
{
	const struct stmpe_keypad_variant *variant = keypad->variant;
	struct stmpe *stmpe = keypad->stmpe;
	int ret;

270
	if (keypad->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
R
Rabin Vincent 已提交
271 272
		return -EINVAL;

273
	if (keypad->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
R
Rabin Vincent 已提交
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
		return -EINVAL;

	ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
	if (ret < 0)
		return ret;

	ret = stmpe_keypad_altfunc_init(keypad);
	if (ret < 0)
		return ret;

	ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
	if (ret < 0)
		return ret;

	ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
	if (ret < 0)
		return ret;

	if (variant->max_rows > 8) {
		ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
				     STMPE_KPC_ROW_MSB_ROWS,
				     keypad->rows >> 8);
		if (ret < 0)
			return ret;
	}

	ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
			     STMPE_KPC_CTRL_MSB_SCAN_COUNT,
302
			     keypad->scan_count << 4);
R
Rabin Vincent 已提交
303 304 305 306 307 308 309
	if (ret < 0)
		return ret;

	return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
			      STMPE_KPC_CTRL_LSB_SCAN |
			      STMPE_KPC_CTRL_LSB_DEBOUNCE,
			      STMPE_KPC_CTRL_LSB_SCAN |
310
			      (keypad->debounce_ms << 1));
R
Rabin Vincent 已提交
311 312
}

313 314
static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad,
					u32 used_rows, u32 used_cols)
315 316 317
{
	int row, col;

318 319
	for (row = 0; row < used_rows; row++) {
		for (col = 0; col < used_cols; col++) {
320
			int code = MATRIX_SCAN_CODE(row, col,
321
						    STMPE_KEYPAD_ROW_SHIFT);
322 323 324 325 326 327 328 329
			if (keypad->keymap[code] != KEY_RESERVED) {
				keypad->rows |= 1 << row;
				keypad->cols |= 1 << col;
			}
		}
	}
}

B
Bill Pemberton 已提交
330
static int stmpe_keypad_probe(struct platform_device *pdev)
R
Rabin Vincent 已提交
331 332
{
	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
333
	struct device_node *np = pdev->dev.of_node;
R
Rabin Vincent 已提交
334 335
	struct stmpe_keypad *keypad;
	struct input_dev *input;
336 337
	u32 rows;
	u32 cols;
338
	int error;
R
Rabin Vincent 已提交
339 340 341 342 343 344
	int irq;

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

345 346
	keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
			      GFP_KERNEL);
R
Rabin Vincent 已提交
347 348 349
	if (!keypad)
		return -ENOMEM;

350 351 352 353 354 355 356
	keypad->stmpe = stmpe;
	keypad->variant = &stmpe_keypad_variants[stmpe->partnum];

	of_property_read_u32(np, "debounce-interval", &keypad->debounce_ms);
	of_property_read_u32(np, "st,scan-count", &keypad->scan_count);
	keypad->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");

357 358 359
	input = devm_input_allocate_device(&pdev->dev);
	if (!input)
		return -ENOMEM;
R
Rabin Vincent 已提交
360 361 362 363 364

	input->name = "STMPE keypad";
	input->id.bustype = BUS_I2C;
	input->dev.parent = &pdev->dev;

365
	error = matrix_keypad_parse_properties(&pdev->dev, &rows, &cols);
366 367 368 369
	if (error)
		return error;

	error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
370 371 372
					   keypad->keymap, input);
	if (error)
		return error;
R
Rabin Vincent 已提交
373

374
	input_set_capability(input, EV_MSC, MSC_SCAN);
375
	if (!keypad->no_autorepeat)
R
Rabin Vincent 已提交
376 377
		__set_bit(EV_REP, input->evbit);

378
	stmpe_keypad_fill_used_pins(keypad, rows, cols);
R
Rabin Vincent 已提交
379 380 381

	keypad->input = input;

382 383 384
	error = stmpe_keypad_chip_init(keypad);
	if (error < 0)
		return error;
R
Rabin Vincent 已提交
385

386 387 388 389 390 391
	error = devm_request_threaded_irq(&pdev->dev, irq,
					  NULL, stmpe_keypad_irq,
					  IRQF_ONESHOT, "stmpe-keypad", keypad);
	if (error) {
		dev_err(&pdev->dev, "unable to get irq: %d\n", error);
		return error;
R
Rabin Vincent 已提交
392 393
	}

394 395 396 397 398
	error = input_register_device(input);
	if (error) {
		dev_err(&pdev->dev,
			"unable to register input device: %d\n", error);
		return error;
R
Rabin Vincent 已提交
399 400 401 402 403 404 405
	}

	platform_set_drvdata(pdev, keypad);

	return 0;
}

B
Bill Pemberton 已提交
406
static int stmpe_keypad_remove(struct platform_device *pdev)
R
Rabin Vincent 已提交
407 408 409
{
	struct stmpe_keypad *keypad = platform_get_drvdata(pdev);

410
	stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
R
Rabin Vincent 已提交
411 412 413 414 415 416 417 418

	return 0;
}

static struct platform_driver stmpe_keypad_driver = {
	.driver.name	= "stmpe-keypad",
	.driver.owner	= THIS_MODULE,
	.probe		= stmpe_keypad_probe,
B
Bill Pemberton 已提交
419
	.remove		= stmpe_keypad_remove,
R
Rabin Vincent 已提交
420
};
421
module_platform_driver(stmpe_keypad_driver);
R
Rabin Vincent 已提交
422 423 424 425

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("STMPExxxx keypad driver");
MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");