1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for keys on TCA6416 I2C IO expander
5 * Copyright (C) 2010 Texas Instruments
7 * Author : Sriramakrishnan.A.G. <srk@ti.com>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/workqueue.h>
17 #include <linux/gpio.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/tca6416_keypad.h>
22 #define TCA6416_INPUT 0
23 #define TCA6416_OUTPUT 1
24 #define TCA6416_INVERT 2
25 #define TCA6416_DIRECTION 3
27 #define TCA6416_POLL_INTERVAL 100 /* msec */
29 static const struct i2c_device_id tca6416_id[] = {
30 { "tca6416-keys", 16, },
31 { "tca6408-keys", 8, },
34 MODULE_DEVICE_TABLE(i2c, tca6416_id);
36 struct tca6416_drv_data {
37 struct input_dev *input;
38 struct tca6416_button data[];
41 struct tca6416_keypad_chip {
43 uint16_t reg_direction;
46 struct i2c_client *client;
47 struct input_dev *input;
52 struct tca6416_button buttons[];
55 static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
59 error = chip->io_size > 8 ?
60 i2c_smbus_write_word_data(chip->client, reg << 1, val) :
61 i2c_smbus_write_byte_data(chip->client, reg, val);
63 dev_err(&chip->client->dev,
64 "%s failed, reg: %d, val: %d, error: %d\n",
65 __func__, reg, val, error);
72 static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
76 retval = chip->io_size > 8 ?
77 i2c_smbus_read_word_data(chip->client, reg << 1) :
78 i2c_smbus_read_byte_data(chip->client, reg);
80 dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
81 __func__, reg, retval);
89 static void tca6416_keys_scan(struct input_dev *input)
91 struct tca6416_keypad_chip *chip = input_get_drvdata(input);
93 int error, i, pin_index;
95 error = tca6416_read_reg(chip, TCA6416_INPUT, ®_val);
99 reg_val &= chip->pinmask;
101 /* Figure out which lines have changed */
102 val = reg_val ^ chip->reg_input;
103 chip->reg_input = reg_val;
105 for (i = 0, pin_index = 0; i < 16; i++) {
106 if (val & (1 << i)) {
107 struct tca6416_button *button = &chip->buttons[pin_index];
108 unsigned int type = button->type ?: EV_KEY;
109 int state = ((reg_val & (1 << i)) ? 1 : 0)
110 ^ button->active_low;
112 input_event(input, type, button->code, !!state);
116 if (chip->pinmask & (1 << i))
122 * This is threaded IRQ handler and this can (and will) sleep.
124 static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
126 tca6416_keys_scan(dev_id);
131 static int tca6416_keys_open(struct input_dev *dev)
133 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
135 if (!chip->use_polling) {
136 /* Get initial device state in case it has switches */
137 tca6416_keys_scan(dev);
138 enable_irq(chip->client->irq);
144 static void tca6416_keys_close(struct input_dev *dev)
146 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
148 if (!chip->use_polling)
149 disable_irq(chip->client->irq);
152 static int tca6416_setup_registers(struct tca6416_keypad_chip *chip)
156 error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
160 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
164 /* ensure that keypad pins are set to input */
165 error = tca6416_write_reg(chip, TCA6416_DIRECTION,
166 chip->reg_direction | chip->pinmask);
170 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
174 error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
178 chip->reg_input &= chip->pinmask;
183 static int tca6416_keypad_probe(struct i2c_client *client)
185 const struct i2c_device_id *id = i2c_client_get_device_id(client);
186 struct tca6416_keys_platform_data *pdata;
187 struct tca6416_keypad_chip *chip;
188 struct input_dev *input;
192 /* Check functionality */
193 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
194 dev_err(&client->dev, "%s adapter not supported\n",
195 dev_driver_string(&client->adapter->dev));
199 pdata = dev_get_platdata(&client->dev);
201 dev_dbg(&client->dev, "no platform data\n");
205 chip = devm_kzalloc(&client->dev,
206 struct_size(chip, buttons, pdata->nbuttons),
211 input = devm_input_allocate_device(&client->dev);
215 chip->client = client;
217 chip->io_size = id->driver_data;
218 chip->pinmask = pdata->pinmask;
219 chip->use_polling = pdata->use_polling;
221 input->phys = "tca6416-keys/input0";
222 input->name = client->name;
224 input->open = tca6416_keys_open;
225 input->close = tca6416_keys_close;
227 input->id.bustype = BUS_HOST;
228 input->id.vendor = 0x0001;
229 input->id.product = 0x0001;
230 input->id.version = 0x0100;
232 /* Enable auto repeat feature of Linux input subsystem */
234 __set_bit(EV_REP, input->evbit);
236 for (i = 0; i < pdata->nbuttons; i++) {
239 chip->buttons[i] = pdata->buttons[i];
240 type = (pdata->buttons[i].type) ?: EV_KEY;
241 input_set_capability(input, type, pdata->buttons[i].code);
244 input_set_drvdata(input, chip);
247 * Initialize cached registers from their original values.
248 * we can't share this chip with another i2c master.
250 error = tca6416_setup_registers(chip);
254 if (chip->use_polling) {
255 error = input_setup_polling(input, tca6416_keys_scan);
257 dev_err(&client->dev, "Failed to setup polling\n");
261 input_set_poll_interval(input, TCA6416_POLL_INTERVAL);
263 error = devm_request_threaded_irq(&client->dev, client->irq,
264 NULL, tca6416_keys_isr,
265 IRQF_TRIGGER_FALLING |
268 "tca6416-keypad", input);
270 dev_dbg(&client->dev,
271 "Unable to claim irq %d; error %d\n",
277 error = input_register_device(input);
279 dev_dbg(&client->dev,
280 "Unable to register input device, error: %d\n", error);
284 i2c_set_clientdata(client, chip);
289 static struct i2c_driver tca6416_keypad_driver = {
291 .name = "tca6416-keypad",
293 .probe = tca6416_keypad_probe,
294 .id_table = tca6416_id,
297 static int __init tca6416_keypad_init(void)
299 return i2c_add_driver(&tca6416_keypad_driver);
302 subsys_initcall(tca6416_keypad_init);
304 static void __exit tca6416_keypad_exit(void)
306 i2c_del_driver(&tca6416_keypad_driver);
308 module_exit(tca6416_keypad_exit);
310 MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
311 MODULE_DESCRIPTION("Keypad driver over tca6416 IO expander");
312 MODULE_LICENSE("GPL");