Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 * Copyright (C) ST-Ericsson SA 2010
  3 *
  4 * License Terms: GNU General Public License, version 2
  5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/slab.h>
 10#include <linux/input.h>
 11#include <linux/interrupt.h>
 12#include <linux/platform_device.h>
 13#include <linux/input/matrix_keypad.h>
 14#include <linux/mfd/stmpe.h>
 15
 16/* These are at the same addresses in all STMPE variants */
 17#define STMPE_KPC_COL			0x60
 18#define STMPE_KPC_ROW_MSB		0x61
 19#define STMPE_KPC_ROW_LSB		0x62
 20#define STMPE_KPC_CTRL_MSB		0x63
 21#define STMPE_KPC_CTRL_LSB		0x64
 22#define STMPE_KPC_COMBI_KEY_0		0x65
 23#define STMPE_KPC_COMBI_KEY_1		0x66
 24#define STMPE_KPC_COMBI_KEY_2		0x67
 25#define STMPE_KPC_DATA_BYTE0		0x68
 26#define STMPE_KPC_DATA_BYTE1		0x69
 27#define STMPE_KPC_DATA_BYTE2		0x6a
 28#define STMPE_KPC_DATA_BYTE3		0x6b
 29#define STMPE_KPC_DATA_BYTE4		0x6c
 30
 31#define STMPE_KPC_CTRL_LSB_SCAN		(0x1 << 0)
 32#define STMPE_KPC_CTRL_LSB_DEBOUNCE	(0x7f << 1)
 33#define STMPE_KPC_CTRL_MSB_SCAN_COUNT	(0xf << 4)
 34
 35#define STMPE_KPC_ROW_MSB_ROWS		0xff
 36
 37#define STMPE_KPC_DATA_UP		(0x1 << 7)
 38#define STMPE_KPC_DATA_ROW		(0xf << 3)
 39#define STMPE_KPC_DATA_COL		(0x7 << 0)
 40#define STMPE_KPC_DATA_NOKEY_MASK	0x78
 41
 42#define STMPE_KEYPAD_MAX_DEBOUNCE	127
 43#define STMPE_KEYPAD_MAX_SCAN_COUNT	15
 44
 45#define STMPE_KEYPAD_MAX_ROWS		8
 46#define STMPE_KEYPAD_MAX_COLS		8
 47#define STMPE_KEYPAD_ROW_SHIFT		3
 48#define STMPE_KEYPAD_KEYMAP_SIZE	\
 49	(STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
 50
 51/**
 52 * struct stmpe_keypad_variant - model-specific attributes
 53 * @auto_increment: whether the KPC_DATA_BYTE register address
 54 *		    auto-increments on multiple read
 
 55 * @num_data: number of data bytes
 56 * @num_normal_data: number of normal keys' data bytes
 57 * @max_cols: maximum number of columns supported
 58 * @max_rows: maximum number of rows supported
 59 * @col_gpios: bitmask of gpios which can be used for columns
 60 * @row_gpios: bitmask of gpios which can be used for rows
 61 */
 62struct stmpe_keypad_variant {
 63	bool		auto_increment;
 
 64	int		num_data;
 65	int		num_normal_data;
 66	int		max_cols;
 67	int		max_rows;
 68	unsigned int	col_gpios;
 69	unsigned int	row_gpios;
 70};
 71
 72static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
 73	[STMPE1601] = {
 74		.auto_increment		= true,
 75		.num_data		= 5,
 76		.num_normal_data	= 3,
 77		.max_cols		= 8,
 78		.max_rows		= 8,
 79		.col_gpios		= 0x000ff,	/* GPIO 0 - 7 */
 80		.row_gpios		= 0x0ff00,	/* GPIO 8 - 15 */
 81	},
 82	[STMPE2401] = {
 83		.auto_increment		= false,
 
 84		.num_data		= 3,
 85		.num_normal_data	= 2,
 86		.max_cols		= 8,
 87		.max_rows		= 12,
 88		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
 89		.row_gpios		= 0x1fef00,	/* GPIO 8-14, 16-20 */
 90	},
 91	[STMPE2403] = {
 92		.auto_increment		= true,
 
 93		.num_data		= 5,
 94		.num_normal_data	= 3,
 95		.max_cols		= 8,
 96		.max_rows		= 12,
 97		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
 98		.row_gpios		= 0x1fef00,	/* GPIO 8-14, 16-20 */
 99	},
100};
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102struct stmpe_keypad {
103	struct stmpe *stmpe;
104	struct input_dev *input;
105	const struct stmpe_keypad_variant *variant;
106	const struct stmpe_keypad_platform_data *plat;
107
 
108	unsigned int rows;
109	unsigned int cols;
110
111	unsigned short keymap[STMPE_KEYPAD_KEYMAP_SIZE];
112};
113
114static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
115{
116	const struct stmpe_keypad_variant *variant = keypad->variant;
117	struct stmpe *stmpe = keypad->stmpe;
118	int ret;
119	int i;
120
121	if (variant->auto_increment)
122		return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
123					variant->num_data, data);
124
125	for (i = 0; i < variant->num_data; i++) {
126		ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
127		if (ret < 0)
128			return ret;
129
130		data[i] = ret;
131	}
132
133	return 0;
134}
135
136static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
137{
138	struct stmpe_keypad *keypad = dev;
139	struct input_dev *input = keypad->input;
140	const struct stmpe_keypad_variant *variant = keypad->variant;
141	u8 fifo[variant->num_data];
142	int ret;
143	int i;
144
145	ret = stmpe_keypad_read_data(keypad, fifo);
146	if (ret < 0)
147		return IRQ_NONE;
148
149	for (i = 0; i < variant->num_normal_data; i++) {
150		u8 data = fifo[i];
151		int row = (data & STMPE_KPC_DATA_ROW) >> 3;
152		int col = data & STMPE_KPC_DATA_COL;
153		int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
154		bool up = data & STMPE_KPC_DATA_UP;
155
156		if ((data & STMPE_KPC_DATA_NOKEY_MASK)
157			== STMPE_KPC_DATA_NOKEY_MASK)
158			continue;
159
160		input_event(input, EV_MSC, MSC_SCAN, code);
161		input_report_key(input, keypad->keymap[code], !up);
162		input_sync(input);
163	}
164
165	return IRQ_HANDLED;
166}
167
168static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
169{
170	const struct stmpe_keypad_variant *variant = keypad->variant;
171	unsigned int col_gpios = variant->col_gpios;
172	unsigned int row_gpios = variant->row_gpios;
173	struct stmpe *stmpe = keypad->stmpe;
 
174	unsigned int pins = 0;
 
 
175	int i;
176
177	/*
178	 * Figure out which pins need to be set to the keypad alternate
179	 * function.
180	 *
181	 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
182	 * for the keypad.
183	 *
184	 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
185	 * for the keypad) are used on the board.
186	 */
187
188	for (i = 0; i < variant->max_cols; i++) {
189		int num = __ffs(col_gpios);
190
191		if (keypad->cols & (1 << i))
192			pins |= 1 << num;
 
 
193
194		col_gpios &= ~(1 << num);
195	}
196
197	for (i = 0; i < variant->max_rows; i++) {
198		int num = __ffs(row_gpios);
199
200		if (keypad->rows & (1 << i))
201			pins |= 1 << num;
202
203		row_gpios &= ~(1 << num);
204	}
205
206	return stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207}
208
209static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
210{
211	const struct stmpe_keypad_platform_data *plat = keypad->plat;
212	const struct stmpe_keypad_variant *variant = keypad->variant;
213	struct stmpe *stmpe = keypad->stmpe;
214	int ret;
215
216	if (plat->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
217		return -EINVAL;
218
219	if (plat->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
220		return -EINVAL;
221
222	ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
223	if (ret < 0)
224		return ret;
225
226	ret = stmpe_keypad_altfunc_init(keypad);
227	if (ret < 0)
228		return ret;
229
230	ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
231	if (ret < 0)
232		return ret;
233
234	ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
235	if (ret < 0)
236		return ret;
237
238	if (variant->max_rows > 8) {
239		ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
240				     STMPE_KPC_ROW_MSB_ROWS,
241				     keypad->rows >> 8);
242		if (ret < 0)
243			return ret;
244	}
245
246	ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
247			     STMPE_KPC_CTRL_MSB_SCAN_COUNT,
248			     plat->scan_count << 4);
249	if (ret < 0)
250		return ret;
251
252	return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
253			      STMPE_KPC_CTRL_LSB_SCAN |
254			      STMPE_KPC_CTRL_LSB_DEBOUNCE,
255			      STMPE_KPC_CTRL_LSB_SCAN |
256			      (plat->debounce_ms << 1));
257}
258
259static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad)
 
260{
261	int row, col;
262
263	for (row = 0; row < STMPE_KEYPAD_MAX_ROWS; row++) {
264		for (col = 0; col < STMPE_KEYPAD_MAX_COLS; col++) {
265			int code = MATRIX_SCAN_CODE(row, col,
266						STMPE_KEYPAD_ROW_SHIFT);
267			if (keypad->keymap[code] != KEY_RESERVED) {
268				keypad->rows |= 1 << row;
269				keypad->cols |= 1 << col;
270			}
271		}
272	}
273}
274
275#ifdef CONFIG_OF
276static const struct stmpe_keypad_platform_data *
277stmpe_keypad_of_probe(struct device *dev)
278{
279	struct device_node *np = dev->of_node;
280	struct stmpe_keypad_platform_data *plat;
281
282	if (!np)
283		return ERR_PTR(-ENODEV);
284
285	plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
286	if (!plat)
287		return ERR_PTR(-ENOMEM);
288
289	of_property_read_u32(np, "debounce-interval", &plat->debounce_ms);
290	of_property_read_u32(np, "st,scan-count", &plat->scan_count);
291
292	plat->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
293
294	return plat;
295}
296#else
297static inline const struct stmpe_keypad_platform_data *
298stmpe_keypad_of_probe(struct device *dev)
299{
300	return ERR_PTR(-EINVAL);
301}
302#endif
303
304static int stmpe_keypad_probe(struct platform_device *pdev)
305{
306	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
307	const struct stmpe_keypad_platform_data *plat;
308	struct stmpe_keypad *keypad;
309	struct input_dev *input;
 
 
310	int error;
311	int irq;
312
313	plat = stmpe->pdata->keypad;
314	if (!plat) {
315		plat = stmpe_keypad_of_probe(&pdev->dev);
316		if (IS_ERR(plat))
317			return PTR_ERR(plat);
318	}
319
320	irq = platform_get_irq(pdev, 0);
321	if (irq < 0)
322		return irq;
323
324	keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
325			      GFP_KERNEL);
326	if (!keypad)
327		return -ENOMEM;
328
 
 
 
 
 
 
 
329	input = devm_input_allocate_device(&pdev->dev);
330	if (!input)
331		return -ENOMEM;
332
333	input->name = "STMPE keypad";
334	input->id.bustype = BUS_I2C;
335	input->dev.parent = &pdev->dev;
336
337	error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
338					   STMPE_KEYPAD_MAX_ROWS,
339					   STMPE_KEYPAD_MAX_COLS,
 
 
340					   keypad->keymap, input);
341	if (error)
342		return error;
343
344	input_set_capability(input, EV_MSC, MSC_SCAN);
345	if (!plat->no_autorepeat)
346		__set_bit(EV_REP, input->evbit);
347
348	stmpe_keypad_fill_used_pins(keypad);
349
350	keypad->stmpe = stmpe;
351	keypad->plat = plat;
352	keypad->input = input;
353	keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
354
355	error = stmpe_keypad_chip_init(keypad);
356	if (error < 0)
357		return error;
358
359	error = devm_request_threaded_irq(&pdev->dev, irq,
360					  NULL, stmpe_keypad_irq,
361					  IRQF_ONESHOT, "stmpe-keypad", keypad);
362	if (error) {
363		dev_err(&pdev->dev, "unable to get irq: %d\n", error);
364		return error;
365	}
366
367	error = input_register_device(input);
368	if (error) {
369		dev_err(&pdev->dev,
370			"unable to register input device: %d\n", error);
371		return error;
372	}
373
374	platform_set_drvdata(pdev, keypad);
375
376	return 0;
377}
378
379static int stmpe_keypad_remove(struct platform_device *pdev)
380{
381	struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
382
383	stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
384
385	return 0;
386}
387
388static struct platform_driver stmpe_keypad_driver = {
389	.driver.name	= "stmpe-keypad",
390	.driver.owner	= THIS_MODULE,
391	.probe		= stmpe_keypad_probe,
392	.remove		= stmpe_keypad_remove,
393};
394module_platform_driver(stmpe_keypad_driver);
395
396MODULE_LICENSE("GPL v2");
397MODULE_DESCRIPTION("STMPExxxx keypad driver");
398MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");
v4.6
  1/*
  2 * Copyright (C) ST-Ericsson SA 2010
  3 *
  4 * License Terms: GNU General Public License, version 2
  5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/slab.h>
 10#include <linux/input.h>
 11#include <linux/interrupt.h>
 12#include <linux/platform_device.h>
 13#include <linux/input/matrix_keypad.h>
 14#include <linux/mfd/stmpe.h>
 15
 16/* These are at the same addresses in all STMPE variants */
 17#define STMPE_KPC_COL			0x60
 18#define STMPE_KPC_ROW_MSB		0x61
 19#define STMPE_KPC_ROW_LSB		0x62
 20#define STMPE_KPC_CTRL_MSB		0x63
 21#define STMPE_KPC_CTRL_LSB		0x64
 22#define STMPE_KPC_COMBI_KEY_0		0x65
 23#define STMPE_KPC_COMBI_KEY_1		0x66
 24#define STMPE_KPC_COMBI_KEY_2		0x67
 25#define STMPE_KPC_DATA_BYTE0		0x68
 26#define STMPE_KPC_DATA_BYTE1		0x69
 27#define STMPE_KPC_DATA_BYTE2		0x6a
 28#define STMPE_KPC_DATA_BYTE3		0x6b
 29#define STMPE_KPC_DATA_BYTE4		0x6c
 30
 31#define STMPE_KPC_CTRL_LSB_SCAN		(0x1 << 0)
 32#define STMPE_KPC_CTRL_LSB_DEBOUNCE	(0x7f << 1)
 33#define STMPE_KPC_CTRL_MSB_SCAN_COUNT	(0xf << 4)
 34
 35#define STMPE_KPC_ROW_MSB_ROWS		0xff
 36
 37#define STMPE_KPC_DATA_UP		(0x1 << 7)
 38#define STMPE_KPC_DATA_ROW		(0xf << 3)
 39#define STMPE_KPC_DATA_COL		(0x7 << 0)
 40#define STMPE_KPC_DATA_NOKEY_MASK	0x78
 41
 42#define STMPE_KEYPAD_MAX_DEBOUNCE	127
 43#define STMPE_KEYPAD_MAX_SCAN_COUNT	15
 44
 45#define STMPE_KEYPAD_MAX_ROWS		8
 46#define STMPE_KEYPAD_MAX_COLS		8
 47#define STMPE_KEYPAD_ROW_SHIFT		3
 48#define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
 49	(STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
 50
 51/**
 52 * struct stmpe_keypad_variant - model-specific attributes
 53 * @auto_increment: whether the KPC_DATA_BYTE register address
 54 *		    auto-increments on multiple read
 55 * @set_pullup: whether the pins need to have their pull-ups set
 56 * @num_data: number of data bytes
 57 * @num_normal_data: number of normal keys' data bytes
 58 * @max_cols: maximum number of columns supported
 59 * @max_rows: maximum number of rows supported
 60 * @col_gpios: bitmask of gpios which can be used for columns
 61 * @row_gpios: bitmask of gpios which can be used for rows
 62 */
 63struct stmpe_keypad_variant {
 64	bool		auto_increment;
 65	bool		set_pullup;
 66	int		num_data;
 67	int		num_normal_data;
 68	int		max_cols;
 69	int		max_rows;
 70	unsigned int	col_gpios;
 71	unsigned int	row_gpios;
 72};
 73
 74static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
 75	[STMPE1601] = {
 76		.auto_increment		= true,
 77		.num_data		= 5,
 78		.num_normal_data	= 3,
 79		.max_cols		= 8,
 80		.max_rows		= 8,
 81		.col_gpios		= 0x000ff,	/* GPIO 0 - 7 */
 82		.row_gpios		= 0x0ff00,	/* GPIO 8 - 15 */
 83	},
 84	[STMPE2401] = {
 85		.auto_increment		= false,
 86		.set_pullup		= true,
 87		.num_data		= 3,
 88		.num_normal_data	= 2,
 89		.max_cols		= 8,
 90		.max_rows		= 12,
 91		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
 92		.row_gpios		= 0x1f7f00,	/* GPIO 8-14, 16-20 */
 93	},
 94	[STMPE2403] = {
 95		.auto_increment		= true,
 96		.set_pullup		= true,
 97		.num_data		= 5,
 98		.num_normal_data	= 3,
 99		.max_cols		= 8,
100		.max_rows		= 12,
101		.col_gpios		= 0x0000ff,	/* GPIO 0 - 7*/
102		.row_gpios		= 0x1fef00,	/* GPIO 8-14, 16-20 */
103	},
104};
105
106/**
107 * struct stmpe_keypad - STMPE keypad state container
108 * @stmpe: pointer to parent STMPE device
109 * @input: spawned input device
110 * @variant: STMPE variant
111 * @debounce_ms: debounce interval, in ms.  Maximum is
112 *		 %STMPE_KEYPAD_MAX_DEBOUNCE.
113 * @scan_count: number of key scanning cycles to confirm key data.
114 *		Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
115 * @no_autorepeat: disable key autorepeat
116 * @rows: bitmask for the rows
117 * @cols: bitmask for the columns
118 * @keymap: the keymap
119 */
120struct stmpe_keypad {
121	struct stmpe *stmpe;
122	struct input_dev *input;
123	const struct stmpe_keypad_variant *variant;
124	unsigned int debounce_ms;
125	unsigned int scan_count;
126	bool no_autorepeat;
127	unsigned int rows;
128	unsigned int cols;
129	unsigned short keymap[STMPE_KEYPAD_KEYMAP_MAX_SIZE];
 
130};
131
132static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
133{
134	const struct stmpe_keypad_variant *variant = keypad->variant;
135	struct stmpe *stmpe = keypad->stmpe;
136	int ret;
137	int i;
138
139	if (variant->auto_increment)
140		return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
141					variant->num_data, data);
142
143	for (i = 0; i < variant->num_data; i++) {
144		ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
145		if (ret < 0)
146			return ret;
147
148		data[i] = ret;
149	}
150
151	return 0;
152}
153
154static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
155{
156	struct stmpe_keypad *keypad = dev;
157	struct input_dev *input = keypad->input;
158	const struct stmpe_keypad_variant *variant = keypad->variant;
159	u8 fifo[variant->num_data];
160	int ret;
161	int i;
162
163	ret = stmpe_keypad_read_data(keypad, fifo);
164	if (ret < 0)
165		return IRQ_NONE;
166
167	for (i = 0; i < variant->num_normal_data; i++) {
168		u8 data = fifo[i];
169		int row = (data & STMPE_KPC_DATA_ROW) >> 3;
170		int col = data & STMPE_KPC_DATA_COL;
171		int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
172		bool up = data & STMPE_KPC_DATA_UP;
173
174		if ((data & STMPE_KPC_DATA_NOKEY_MASK)
175			== STMPE_KPC_DATA_NOKEY_MASK)
176			continue;
177
178		input_event(input, EV_MSC, MSC_SCAN, code);
179		input_report_key(input, keypad->keymap[code], !up);
180		input_sync(input);
181	}
182
183	return IRQ_HANDLED;
184}
185
186static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
187{
188	const struct stmpe_keypad_variant *variant = keypad->variant;
189	unsigned int col_gpios = variant->col_gpios;
190	unsigned int row_gpios = variant->row_gpios;
191	struct stmpe *stmpe = keypad->stmpe;
192	u8 pureg = stmpe->regs[STMPE_IDX_GPPUR_LSB];
193	unsigned int pins = 0;
194	unsigned int pu_pins = 0;
195	int ret;
196	int i;
197
198	/*
199	 * Figure out which pins need to be set to the keypad alternate
200	 * function.
201	 *
202	 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
203	 * for the keypad.
204	 *
205	 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
206	 * for the keypad) are used on the board.
207	 */
208
209	for (i = 0; i < variant->max_cols; i++) {
210		int num = __ffs(col_gpios);
211
212		if (keypad->cols & (1 << i)) {
213			pins |= 1 << num;
214			pu_pins |= 1 << num;
215		}
216
217		col_gpios &= ~(1 << num);
218	}
219
220	for (i = 0; i < variant->max_rows; i++) {
221		int num = __ffs(row_gpios);
222
223		if (keypad->rows & (1 << i))
224			pins |= 1 << num;
225
226		row_gpios &= ~(1 << num);
227	}
228
229	ret = stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
230	if (ret)
231		return ret;
232
233	/*
234	 * On STMPE24xx, set pin bias to pull-up on all keypad input
235	 * pins (columns), this incidentally happen to be maximum 8 pins
236	 * and placed at GPIO0-7 so only the LSB of the pull up register
237	 * ever needs to be written.
238	 */
239	if (variant->set_pullup) {
240		u8 val;
241
242		ret = stmpe_reg_read(stmpe, pureg);
243		if (ret)
244			return ret;
245
246		/* Do not touch unused pins, may be used for GPIO */
247		val = ret & ~pu_pins;
248		val |= pu_pins;
249
250		ret = stmpe_reg_write(stmpe, pureg, val);
251	}
252
253	return 0;
254}
255
256static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
257{
 
258	const struct stmpe_keypad_variant *variant = keypad->variant;
259	struct stmpe *stmpe = keypad->stmpe;
260	int ret;
261
262	if (keypad->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
263		return -EINVAL;
264
265	if (keypad->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
266		return -EINVAL;
267
268	ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
269	if (ret < 0)
270		return ret;
271
272	ret = stmpe_keypad_altfunc_init(keypad);
273	if (ret < 0)
274		return ret;
275
276	ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
277	if (ret < 0)
278		return ret;
279
280	ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
281	if (ret < 0)
282		return ret;
283
284	if (variant->max_rows > 8) {
285		ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
286				     STMPE_KPC_ROW_MSB_ROWS,
287				     keypad->rows >> 8);
288		if (ret < 0)
289			return ret;
290	}
291
292	ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
293			     STMPE_KPC_CTRL_MSB_SCAN_COUNT,
294			     keypad->scan_count << 4);
295	if (ret < 0)
296		return ret;
297
298	return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
299			      STMPE_KPC_CTRL_LSB_SCAN |
300			      STMPE_KPC_CTRL_LSB_DEBOUNCE,
301			      STMPE_KPC_CTRL_LSB_SCAN |
302			      (keypad->debounce_ms << 1));
303}
304
305static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad,
306					u32 used_rows, u32 used_cols)
307{
308	int row, col;
309
310	for (row = 0; row < used_rows; row++) {
311		for (col = 0; col < used_cols; col++) {
312			int code = MATRIX_SCAN_CODE(row, col,
313						    STMPE_KEYPAD_ROW_SHIFT);
314			if (keypad->keymap[code] != KEY_RESERVED) {
315				keypad->rows |= 1 << row;
316				keypad->cols |= 1 << col;
317			}
318		}
319	}
320}
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322static int stmpe_keypad_probe(struct platform_device *pdev)
323{
324	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
325	struct device_node *np = pdev->dev.of_node;
326	struct stmpe_keypad *keypad;
327	struct input_dev *input;
328	u32 rows;
329	u32 cols;
330	int error;
331	int irq;
332
 
 
 
 
 
 
 
333	irq = platform_get_irq(pdev, 0);
334	if (irq < 0)
335		return irq;
336
337	keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
338			      GFP_KERNEL);
339	if (!keypad)
340		return -ENOMEM;
341
342	keypad->stmpe = stmpe;
343	keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
344
345	of_property_read_u32(np, "debounce-interval", &keypad->debounce_ms);
346	of_property_read_u32(np, "st,scan-count", &keypad->scan_count);
347	keypad->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
348
349	input = devm_input_allocate_device(&pdev->dev);
350	if (!input)
351		return -ENOMEM;
352
353	input->name = "STMPE keypad";
354	input->id.bustype = BUS_I2C;
355	input->dev.parent = &pdev->dev;
356
357	error = matrix_keypad_parse_of_params(&pdev->dev, &rows, &cols);
358	if (error)
359		return error;
360
361	error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
362					   keypad->keymap, input);
363	if (error)
364		return error;
365
366	input_set_capability(input, EV_MSC, MSC_SCAN);
367	if (!keypad->no_autorepeat)
368		__set_bit(EV_REP, input->evbit);
369
370	stmpe_keypad_fill_used_pins(keypad, rows, cols);
371
 
 
372	keypad->input = input;
 
373
374	error = stmpe_keypad_chip_init(keypad);
375	if (error < 0)
376		return error;
377
378	error = devm_request_threaded_irq(&pdev->dev, irq,
379					  NULL, stmpe_keypad_irq,
380					  IRQF_ONESHOT, "stmpe-keypad", keypad);
381	if (error) {
382		dev_err(&pdev->dev, "unable to get irq: %d\n", error);
383		return error;
384	}
385
386	error = input_register_device(input);
387	if (error) {
388		dev_err(&pdev->dev,
389			"unable to register input device: %d\n", error);
390		return error;
391	}
392
393	platform_set_drvdata(pdev, keypad);
394
395	return 0;
396}
397
398static int stmpe_keypad_remove(struct platform_device *pdev)
399{
400	struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
401
402	stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
403
404	return 0;
405}
406
407static struct platform_driver stmpe_keypad_driver = {
408	.driver.name	= "stmpe-keypad",
409	.driver.owner	= THIS_MODULE,
410	.probe		= stmpe_keypad_probe,
411	.remove		= stmpe_keypad_remove,
412};
413module_platform_driver(stmpe_keypad_driver);
414
415MODULE_LICENSE("GPL v2");
416MODULE_DESCRIPTION("STMPExxxx keypad driver");
417MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");