xref: /linux/drivers/input/keyboard/lm8333.c (revision 9ea370f3416ecc4b22d49b24e2c7fdc9c9ba3a0e)
184a14ae8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20bf25a45SWolfram Sang /*
30bf25a45SWolfram Sang  * LM8333 keypad driver
4c9eeb508SWolfram Sang  * Copyright (C) 2012 Wolfram Sang, Pengutronix <kernel@pengutronix.de>
50bf25a45SWolfram Sang  */
60bf25a45SWolfram Sang 
70bf25a45SWolfram Sang #include <linux/i2c.h>
8ddc9589dSDmitry Torokhov #include <linux/input.h>
90bf25a45SWolfram Sang #include <linux/input/matrix_keypad.h>
100bf25a45SWolfram Sang #include <linux/input/lm8333.h>
11ddc9589dSDmitry Torokhov #include <linux/interrupt.h>
12ddc9589dSDmitry Torokhov #include <linux/module.h>
13ddc9589dSDmitry Torokhov #include <linux/slab.h>
140bf25a45SWolfram Sang 
150bf25a45SWolfram Sang #define LM8333_FIFO_READ		0x20
160bf25a45SWolfram Sang #define LM8333_DEBOUNCE			0x22
170bf25a45SWolfram Sang #define LM8333_READ_INT			0xD0
180bf25a45SWolfram Sang #define LM8333_ACTIVE			0xE4
190bf25a45SWolfram Sang #define LM8333_READ_ERROR		0xF0
200bf25a45SWolfram Sang 
210bf25a45SWolfram Sang #define LM8333_KEYPAD_IRQ		(1 << 0)
220bf25a45SWolfram Sang #define LM8333_ERROR_IRQ		(1 << 3)
230bf25a45SWolfram Sang 
240bf25a45SWolfram Sang #define LM8333_ERROR_KEYOVR		0x04
250bf25a45SWolfram Sang #define LM8333_ERROR_FIFOOVR		0x40
260bf25a45SWolfram Sang 
270bf25a45SWolfram Sang #define LM8333_FIFO_TRANSFER_SIZE	16
280bf25a45SWolfram Sang 
290bf25a45SWolfram Sang #define LM8333_NUM_ROWS		8
301932811fSDmitry Torokhov #define LM8333_NUM_COLS		16
311932811fSDmitry Torokhov #define LM8333_ROW_SHIFT	4
320bf25a45SWolfram Sang 
330bf25a45SWolfram Sang struct lm8333 {
340bf25a45SWolfram Sang 	struct i2c_client *client;
350bf25a45SWolfram Sang 	struct input_dev *input;
360bf25a45SWolfram Sang 	unsigned short keycodes[LM8333_NUM_ROWS << LM8333_ROW_SHIFT];
370bf25a45SWolfram Sang };
380bf25a45SWolfram Sang 
390bf25a45SWolfram Sang /* The accessors try twice because the first access may be needed for wakeup */
400bf25a45SWolfram Sang #define LM8333_READ_RETRIES 2
410bf25a45SWolfram Sang 
lm8333_read8(struct lm8333 * lm8333,u8 cmd)420bf25a45SWolfram Sang int lm8333_read8(struct lm8333 *lm8333, u8 cmd)
430bf25a45SWolfram Sang {
440bf25a45SWolfram Sang 	int retries = 0, ret;
450bf25a45SWolfram Sang 
460bf25a45SWolfram Sang 	do {
470bf25a45SWolfram Sang 		ret = i2c_smbus_read_byte_data(lm8333->client, cmd);
480bf25a45SWolfram Sang 	} while (ret < 0 && retries++ < LM8333_READ_RETRIES);
490bf25a45SWolfram Sang 
500bf25a45SWolfram Sang 	return ret;
510bf25a45SWolfram Sang }
520bf25a45SWolfram Sang 
lm8333_write8(struct lm8333 * lm8333,u8 cmd,u8 val)530bf25a45SWolfram Sang int lm8333_write8(struct lm8333 *lm8333, u8 cmd, u8 val)
540bf25a45SWolfram Sang {
550bf25a45SWolfram Sang 	int retries = 0, ret;
560bf25a45SWolfram Sang 
570bf25a45SWolfram Sang 	do {
580bf25a45SWolfram Sang 		ret = i2c_smbus_write_byte_data(lm8333->client, cmd, val);
590bf25a45SWolfram Sang 	} while (ret < 0 && retries++ < LM8333_READ_RETRIES);
600bf25a45SWolfram Sang 
610bf25a45SWolfram Sang 	return ret;
620bf25a45SWolfram Sang }
630bf25a45SWolfram Sang 
lm8333_read_block(struct lm8333 * lm8333,u8 cmd,u8 len,u8 * buf)640bf25a45SWolfram Sang int lm8333_read_block(struct lm8333 *lm8333, u8 cmd, u8 len, u8 *buf)
650bf25a45SWolfram Sang {
660bf25a45SWolfram Sang 	int retries = 0, ret;
670bf25a45SWolfram Sang 
680bf25a45SWolfram Sang 	do {
690bf25a45SWolfram Sang 		ret = i2c_smbus_read_i2c_block_data(lm8333->client,
700bf25a45SWolfram Sang 						    cmd, len, buf);
710bf25a45SWolfram Sang 	} while (ret < 0 && retries++ < LM8333_READ_RETRIES);
720bf25a45SWolfram Sang 
730bf25a45SWolfram Sang 	return ret;
740bf25a45SWolfram Sang }
750bf25a45SWolfram Sang 
lm8333_key_handler(struct lm8333 * lm8333)760bf25a45SWolfram Sang static void lm8333_key_handler(struct lm8333 *lm8333)
770bf25a45SWolfram Sang {
780bf25a45SWolfram Sang 	struct input_dev *input = lm8333->input;
790bf25a45SWolfram Sang 	u8 keys[LM8333_FIFO_TRANSFER_SIZE];
800bf25a45SWolfram Sang 	u8 code, pressed;
810bf25a45SWolfram Sang 	int i, ret;
820bf25a45SWolfram Sang 
830bf25a45SWolfram Sang 	ret = lm8333_read_block(lm8333, LM8333_FIFO_READ,
840bf25a45SWolfram Sang 				LM8333_FIFO_TRANSFER_SIZE, keys);
850bf25a45SWolfram Sang 	if (ret != LM8333_FIFO_TRANSFER_SIZE) {
860bf25a45SWolfram Sang 		dev_err(&lm8333->client->dev,
870bf25a45SWolfram Sang 			"Error %d while reading FIFO\n", ret);
880bf25a45SWolfram Sang 		return;
890bf25a45SWolfram Sang 	}
900bf25a45SWolfram Sang 
91954bd6d1SAlan Cox 	for (i = 0; i < LM8333_FIFO_TRANSFER_SIZE && keys[i]; i++) {
920bf25a45SWolfram Sang 		pressed = keys[i] & 0x80;
930bf25a45SWolfram Sang 		code = keys[i] & 0x7f;
940bf25a45SWolfram Sang 
950bf25a45SWolfram Sang 		input_event(input, EV_MSC, MSC_SCAN, code);
960bf25a45SWolfram Sang 		input_report_key(input, lm8333->keycodes[code], pressed);
970bf25a45SWolfram Sang 	}
980bf25a45SWolfram Sang 
990bf25a45SWolfram Sang 	input_sync(input);
1000bf25a45SWolfram Sang }
1010bf25a45SWolfram Sang 
lm8333_irq_thread(int irq,void * data)1020bf25a45SWolfram Sang static irqreturn_t lm8333_irq_thread(int irq, void *data)
1030bf25a45SWolfram Sang {
1040bf25a45SWolfram Sang 	struct lm8333 *lm8333 = data;
1050bf25a45SWolfram Sang 	u8 status = lm8333_read8(lm8333, LM8333_READ_INT);
1060bf25a45SWolfram Sang 
1070bf25a45SWolfram Sang 	if (!status)
1080bf25a45SWolfram Sang 		return IRQ_NONE;
1090bf25a45SWolfram Sang 
1100bf25a45SWolfram Sang 	if (status & LM8333_ERROR_IRQ) {
1110bf25a45SWolfram Sang 		u8 err = lm8333_read8(lm8333, LM8333_READ_ERROR);
1120bf25a45SWolfram Sang 
1130bf25a45SWolfram Sang 		if (err & (LM8333_ERROR_KEYOVR | LM8333_ERROR_FIFOOVR)) {
1140bf25a45SWolfram Sang 			u8 dummy[LM8333_FIFO_TRANSFER_SIZE];
1150bf25a45SWolfram Sang 
1160bf25a45SWolfram Sang 			lm8333_read_block(lm8333, LM8333_FIFO_READ,
1170bf25a45SWolfram Sang 					LM8333_FIFO_TRANSFER_SIZE, dummy);
1180bf25a45SWolfram Sang 		}
1190bf25a45SWolfram Sang 		dev_err(&lm8333->client->dev, "Got error %02x\n", err);
1200bf25a45SWolfram Sang 	}
1210bf25a45SWolfram Sang 
1220bf25a45SWolfram Sang 	if (status & LM8333_KEYPAD_IRQ)
1230bf25a45SWolfram Sang 		lm8333_key_handler(lm8333);
1240bf25a45SWolfram Sang 
1250bf25a45SWolfram Sang 	return IRQ_HANDLED;
1260bf25a45SWolfram Sang }
1270bf25a45SWolfram Sang 
lm8333_probe(struct i2c_client * client)12837bbce75SUwe Kleine-König static int lm8333_probe(struct i2c_client *client)
1290bf25a45SWolfram Sang {
130c838cb3dSJingoo Han 	const struct lm8333_platform_data *pdata =
131c838cb3dSJingoo Han 			dev_get_platdata(&client->dev);
1320bf25a45SWolfram Sang 	struct lm8333 *lm8333;
1330bf25a45SWolfram Sang 	struct input_dev *input;
1340bf25a45SWolfram Sang 	int err, active_time;
1350bf25a45SWolfram Sang 
1360bf25a45SWolfram Sang 	if (!pdata)
1370bf25a45SWolfram Sang 		return -EINVAL;
1380bf25a45SWolfram Sang 
1390bf25a45SWolfram Sang 	active_time = pdata->active_time ?: 500;
1400bf25a45SWolfram Sang 	if (active_time / 3 <= pdata->debounce_time / 3) {
1410bf25a45SWolfram Sang 		dev_err(&client->dev, "Active time not big enough!\n");
1420bf25a45SWolfram Sang 		return -EINVAL;
1430bf25a45SWolfram Sang 	}
1440bf25a45SWolfram Sang 
1452b6aa9e7SYangtao Li 	lm8333 = devm_kzalloc(&client->dev, sizeof(*lm8333), GFP_KERNEL);
1462b6aa9e7SYangtao Li 	if (!lm8333)
1472b6aa9e7SYangtao Li 		return -ENOMEM;
1482b6aa9e7SYangtao Li 
1492b6aa9e7SYangtao Li 	input = devm_input_allocate_device(&client->dev);
1502b6aa9e7SYangtao Li 	if (!input)
1512b6aa9e7SYangtao Li 		return -ENOMEM;
1520bf25a45SWolfram Sang 
1530bf25a45SWolfram Sang 	lm8333->client = client;
1540bf25a45SWolfram Sang 	lm8333->input = input;
1550bf25a45SWolfram Sang 
1560bf25a45SWolfram Sang 	input->name = client->name;
1570bf25a45SWolfram Sang 	input->id.bustype = BUS_I2C;
1580bf25a45SWolfram Sang 
1590bf25a45SWolfram Sang 	input_set_capability(input, EV_MSC, MSC_SCAN);
1600bf25a45SWolfram Sang 
1611932811fSDmitry Torokhov 	err = matrix_keypad_build_keymap(pdata->matrix_data, NULL,
1621932811fSDmitry Torokhov 					 LM8333_NUM_ROWS, LM8333_NUM_COLS,
1631932811fSDmitry Torokhov 					 lm8333->keycodes, input);
1641932811fSDmitry Torokhov 	if (err)
1652b6aa9e7SYangtao Li 		return err;
1660bf25a45SWolfram Sang 
1670bf25a45SWolfram Sang 	if (pdata->debounce_time) {
1680bf25a45SWolfram Sang 		err = lm8333_write8(lm8333, LM8333_DEBOUNCE,
1690bf25a45SWolfram Sang 				    pdata->debounce_time / 3);
1700bf25a45SWolfram Sang 		if (err)
1710bf25a45SWolfram Sang 			dev_warn(&client->dev, "Unable to set debounce time\n");
1720bf25a45SWolfram Sang 	}
1730bf25a45SWolfram Sang 
1740bf25a45SWolfram Sang 	if (pdata->active_time) {
1750bf25a45SWolfram Sang 		err = lm8333_write8(lm8333, LM8333_ACTIVE,
1760bf25a45SWolfram Sang 				    pdata->active_time / 3);
1770bf25a45SWolfram Sang 		if (err)
1780bf25a45SWolfram Sang 			dev_warn(&client->dev, "Unable to set active time\n");
1790bf25a45SWolfram Sang 	}
1800bf25a45SWolfram Sang 
1812b6aa9e7SYangtao Li 	err = devm_request_threaded_irq(&client->dev, client->irq,
1822b6aa9e7SYangtao Li 					NULL, lm8333_irq_thread,
1830bf25a45SWolfram Sang 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1840bf25a45SWolfram Sang 					"lm8333", lm8333);
1850bf25a45SWolfram Sang 	if (err)
1862b6aa9e7SYangtao Li 		return err;
1870bf25a45SWolfram Sang 
1880bf25a45SWolfram Sang 	err = input_register_device(input);
1890bf25a45SWolfram Sang 	if (err)
1902b6aa9e7SYangtao Li 		return err;
1910bf25a45SWolfram Sang 
1920bf25a45SWolfram Sang 	i2c_set_clientdata(client, lm8333);
1930bf25a45SWolfram Sang 	return 0;
1940bf25a45SWolfram Sang }
1950bf25a45SWolfram Sang 
1960bf25a45SWolfram Sang static const struct i2c_device_id lm8333_id[] = {
197*5852f2afSUwe Kleine-König 	{ "lm8333" },
1980bf25a45SWolfram Sang 	{ }
1990bf25a45SWolfram Sang };
2000bf25a45SWolfram Sang MODULE_DEVICE_TABLE(i2c, lm8333_id);
2010bf25a45SWolfram Sang 
2020bf25a45SWolfram Sang static struct i2c_driver lm8333_driver = {
2030bf25a45SWolfram Sang 	.driver = {
2040bf25a45SWolfram Sang 		.name		= "lm8333",
2050bf25a45SWolfram Sang 	},
206d8bde56dSUwe Kleine-König 	.probe		= lm8333_probe,
2070bf25a45SWolfram Sang 	.id_table	= lm8333_id,
2080bf25a45SWolfram Sang };
2090bf25a45SWolfram Sang module_i2c_driver(lm8333_driver);
2100bf25a45SWolfram Sang 
211c9eeb508SWolfram Sang MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
2120bf25a45SWolfram Sang MODULE_DESCRIPTION("LM8333 keyboard driver");
2130bf25a45SWolfram Sang MODULE_LICENSE("GPL v2");
214