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 static const struct i2c_device_id tca6416_id
[] = {
28 { "tca6416-keys", 16, },
29 { "tca6408-keys", 8, },
32 MODULE_DEVICE_TABLE(i2c
, tca6416_id
);
34 struct tca6416_drv_data
{
35 struct input_dev
*input
;
36 struct tca6416_button data
[0];
39 struct tca6416_keypad_chip
{
41 uint16_t reg_direction
;
44 struct i2c_client
*client
;
45 struct input_dev
*input
;
46 struct delayed_work dwork
;
51 struct tca6416_button buttons
[0];
54 static int tca6416_write_reg(struct tca6416_keypad_chip
*chip
, int reg
, u16 val
)
58 error
= chip
->io_size
> 8 ?
59 i2c_smbus_write_word_data(chip
->client
, reg
<< 1, val
) :
60 i2c_smbus_write_byte_data(chip
->client
, reg
, val
);
62 dev_err(&chip
->client
->dev
,
63 "%s failed, reg: %d, val: %d, error: %d\n",
64 __func__
, reg
, val
, error
);
71 static int tca6416_read_reg(struct tca6416_keypad_chip
*chip
, int reg
, u16
*val
)
75 retval
= chip
->io_size
> 8 ?
76 i2c_smbus_read_word_data(chip
->client
, reg
<< 1) :
77 i2c_smbus_read_byte_data(chip
->client
, reg
);
79 dev_err(&chip
->client
->dev
, "%s failed, reg: %d, error: %d\n",
80 __func__
, reg
, retval
);
88 static void tca6416_keys_scan(struct tca6416_keypad_chip
*chip
)
90 struct input_dev
*input
= chip
->input
;
92 int error
, i
, pin_index
;
94 error
= tca6416_read_reg(chip
, TCA6416_INPUT
, ®_val
);
98 reg_val
&= chip
->pinmask
;
100 /* Figure out which lines have changed */
101 val
= reg_val
^ chip
->reg_input
;
102 chip
->reg_input
= reg_val
;
104 for (i
= 0, pin_index
= 0; i
< 16; i
++) {
105 if (val
& (1 << i
)) {
106 struct tca6416_button
*button
= &chip
->buttons
[pin_index
];
107 unsigned int type
= button
->type
?: EV_KEY
;
108 int state
= ((reg_val
& (1 << i
)) ? 1 : 0)
109 ^ button
->active_low
;
111 input_event(input
, type
, button
->code
, !!state
);
115 if (chip
->pinmask
& (1 << i
))
121 * This is threaded IRQ handler and this can (and will) sleep.
123 static irqreturn_t
tca6416_keys_isr(int irq
, void *dev_id
)
125 struct tca6416_keypad_chip
*chip
= dev_id
;
127 tca6416_keys_scan(chip
);
132 static void tca6416_keys_work_func(struct work_struct
*work
)
134 struct tca6416_keypad_chip
*chip
=
135 container_of(work
, struct tca6416_keypad_chip
, dwork
.work
);
137 tca6416_keys_scan(chip
);
138 schedule_delayed_work(&chip
->dwork
, msecs_to_jiffies(100));
141 static int tca6416_keys_open(struct input_dev
*dev
)
143 struct tca6416_keypad_chip
*chip
= input_get_drvdata(dev
);
145 /* Get initial device state in case it has switches */
146 tca6416_keys_scan(chip
);
148 if (chip
->use_polling
)
149 schedule_delayed_work(&chip
->dwork
, msecs_to_jiffies(100));
151 enable_irq(chip
->irqnum
);
156 static void tca6416_keys_close(struct input_dev
*dev
)
158 struct tca6416_keypad_chip
*chip
= input_get_drvdata(dev
);
160 if (chip
->use_polling
)
161 cancel_delayed_work_sync(&chip
->dwork
);
163 disable_irq(chip
->irqnum
);
166 static int tca6416_setup_registers(struct tca6416_keypad_chip
*chip
)
170 error
= tca6416_read_reg(chip
, TCA6416_OUTPUT
, &chip
->reg_output
);
174 error
= tca6416_read_reg(chip
, TCA6416_DIRECTION
, &chip
->reg_direction
);
178 /* ensure that keypad pins are set to input */
179 error
= tca6416_write_reg(chip
, TCA6416_DIRECTION
,
180 chip
->reg_direction
| chip
->pinmask
);
184 error
= tca6416_read_reg(chip
, TCA6416_DIRECTION
, &chip
->reg_direction
);
188 error
= tca6416_read_reg(chip
, TCA6416_INPUT
, &chip
->reg_input
);
192 chip
->reg_input
&= chip
->pinmask
;
197 static int tca6416_keypad_probe(struct i2c_client
*client
,
198 const struct i2c_device_id
*id
)
200 struct tca6416_keys_platform_data
*pdata
;
201 struct tca6416_keypad_chip
*chip
;
202 struct input_dev
*input
;
206 /* Check functionality */
207 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE
)) {
208 dev_err(&client
->dev
, "%s adapter not supported\n",
209 dev_driver_string(&client
->adapter
->dev
));
213 pdata
= dev_get_platdata(&client
->dev
);
215 dev_dbg(&client
->dev
, "no platform data\n");
219 chip
= kzalloc(struct_size(chip
, buttons
, pdata
->nbuttons
), GFP_KERNEL
);
220 input
= input_allocate_device();
221 if (!chip
|| !input
) {
226 chip
->client
= client
;
228 chip
->io_size
= id
->driver_data
;
229 chip
->pinmask
= pdata
->pinmask
;
230 chip
->use_polling
= pdata
->use_polling
;
232 INIT_DELAYED_WORK(&chip
->dwork
, tca6416_keys_work_func
);
234 input
->phys
= "tca6416-keys/input0";
235 input
->name
= client
->name
;
236 input
->dev
.parent
= &client
->dev
;
238 input
->open
= tca6416_keys_open
;
239 input
->close
= tca6416_keys_close
;
241 input
->id
.bustype
= BUS_HOST
;
242 input
->id
.vendor
= 0x0001;
243 input
->id
.product
= 0x0001;
244 input
->id
.version
= 0x0100;
246 /* Enable auto repeat feature of Linux input subsystem */
248 __set_bit(EV_REP
, input
->evbit
);
250 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
253 chip
->buttons
[i
] = pdata
->buttons
[i
];
254 type
= (pdata
->buttons
[i
].type
) ?: EV_KEY
;
255 input_set_capability(input
, type
, pdata
->buttons
[i
].code
);
258 input_set_drvdata(input
, chip
);
261 * Initialize cached registers from their original values.
262 * we can't share this chip with another i2c master.
264 error
= tca6416_setup_registers(chip
);
268 if (!chip
->use_polling
) {
269 if (pdata
->irq_is_gpio
)
270 chip
->irqnum
= gpio_to_irq(client
->irq
);
272 chip
->irqnum
= client
->irq
;
274 error
= request_threaded_irq(chip
->irqnum
, NULL
,
276 IRQF_TRIGGER_FALLING
|
278 "tca6416-keypad", chip
);
280 dev_dbg(&client
->dev
,
281 "Unable to claim irq %d; error %d\n",
282 chip
->irqnum
, error
);
285 disable_irq(chip
->irqnum
);
288 error
= input_register_device(input
);
290 dev_dbg(&client
->dev
,
291 "Unable to register input device, error: %d\n", error
);
295 i2c_set_clientdata(client
, chip
);
296 device_init_wakeup(&client
->dev
, 1);
301 if (!chip
->use_polling
) {
302 free_irq(chip
->irqnum
, chip
);
303 enable_irq(chip
->irqnum
);
306 input_free_device(input
);
311 static int tca6416_keypad_remove(struct i2c_client
*client
)
313 struct tca6416_keypad_chip
*chip
= i2c_get_clientdata(client
);
315 if (!chip
->use_polling
) {
316 free_irq(chip
->irqnum
, chip
);
317 enable_irq(chip
->irqnum
);
320 input_unregister_device(chip
->input
);
326 #ifdef CONFIG_PM_SLEEP
327 static int tca6416_keypad_suspend(struct device
*dev
)
329 struct i2c_client
*client
= to_i2c_client(dev
);
330 struct tca6416_keypad_chip
*chip
= i2c_get_clientdata(client
);
332 if (device_may_wakeup(dev
))
333 enable_irq_wake(chip
->irqnum
);
338 static int tca6416_keypad_resume(struct device
*dev
)
340 struct i2c_client
*client
= to_i2c_client(dev
);
341 struct tca6416_keypad_chip
*chip
= i2c_get_clientdata(client
);
343 if (device_may_wakeup(dev
))
344 disable_irq_wake(chip
->irqnum
);
350 static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops
,
351 tca6416_keypad_suspend
, tca6416_keypad_resume
);
353 static struct i2c_driver tca6416_keypad_driver
= {
355 .name
= "tca6416-keypad",
356 .pm
= &tca6416_keypad_dev_pm_ops
,
358 .probe
= tca6416_keypad_probe
,
359 .remove
= tca6416_keypad_remove
,
360 .id_table
= tca6416_id
,
363 static int __init
tca6416_keypad_init(void)
365 return i2c_add_driver(&tca6416_keypad_driver
);
368 subsys_initcall(tca6416_keypad_init
);
370 static void __exit
tca6416_keypad_exit(void)
372 i2c_del_driver(&tca6416_keypad_driver
);
374 module_exit(tca6416_keypad_exit
);
376 MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
377 MODULE_DESCRIPTION("Keypad driver over tca6146 IO expander");
378 MODULE_LICENSE("GPL");