1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for a keypad w/16 buttons connected to a PCF8574 I2C I/O expander
5 * Copyright 2005-2008 Analog Devices Inc.
8 #include <linux/module.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
15 #define DRV_NAME "pcf8574_keypad"
17 static const unsigned char pcf8574_kp_btncode
[] = {
38 unsigned short btncode
[ARRAY_SIZE(pcf8574_kp_btncode
)];
39 struct input_dev
*idev
;
40 struct i2c_client
*client
;
43 unsigned char laststate
;
46 static short read_state(struct kp_data
*lp
)
48 unsigned char x
, y
, a
, b
;
50 i2c_smbus_write_byte(lp
->client
, 240);
51 x
= 0xF & (~(i2c_smbus_read_byte(lp
->client
) >> 4));
53 i2c_smbus_write_byte(lp
->client
, 15);
54 y
= 0xF & (~i2c_smbus_read_byte(lp
->client
));
56 for (a
= 0; x
> 0; a
++)
58 for (b
= 0; y
> 0; b
++)
61 return ((a
- 1) * 4) + b
;
64 static irqreturn_t
pcf8574_kp_irq_handler(int irq
, void *dev_id
)
66 struct kp_data
*lp
= dev_id
;
67 unsigned char nextstate
= read_state(lp
);
69 if (lp
->laststate
!= nextstate
) {
70 int key_down
= nextstate
< ARRAY_SIZE(lp
->btncode
);
71 unsigned short keycode
= key_down
?
72 lp
->btncode
[nextstate
] : lp
->btncode
[lp
->laststate
];
74 input_report_key(lp
->idev
, keycode
, key_down
);
77 lp
->laststate
= nextstate
;
83 static int pcf8574_kp_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
86 struct input_dev
*idev
;
89 if (i2c_smbus_write_byte(client
, 240) < 0) {
90 dev_err(&client
->dev
, "probe: write fail\n");
94 lp
= kzalloc(sizeof(*lp
), GFP_KERNEL
);
98 idev
= input_allocate_device();
100 dev_err(&client
->dev
, "Can't allocate input device\n");
108 idev
->evbit
[0] = BIT_MASK(EV_KEY
);
109 idev
->keycode
= lp
->btncode
;
110 idev
->keycodesize
= sizeof(lp
->btncode
[0]);
111 idev
->keycodemax
= ARRAY_SIZE(lp
->btncode
);
113 for (i
= 0; i
< ARRAY_SIZE(pcf8574_kp_btncode
); i
++) {
114 if (lp
->btncode
[i
] <= KEY_MAX
) {
115 lp
->btncode
[i
] = pcf8574_kp_btncode
[i
];
116 __set_bit(lp
->btncode
[i
], idev
->keybit
);
119 __clear_bit(KEY_RESERVED
, idev
->keybit
);
121 sprintf(lp
->name
, DRV_NAME
);
122 sprintf(lp
->phys
, "kp_data/input0");
124 idev
->name
= lp
->name
;
125 idev
->phys
= lp
->phys
;
126 idev
->id
.bustype
= BUS_I2C
;
127 idev
->id
.vendor
= 0x0001;
128 idev
->id
.product
= 0x0001;
129 idev
->id
.version
= 0x0100;
131 lp
->laststate
= read_state(lp
);
133 ret
= request_threaded_irq(client
->irq
, NULL
, pcf8574_kp_irq_handler
,
134 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
137 dev_err(&client
->dev
, "IRQ %d is not free\n", client
->irq
);
138 goto fail_free_device
;
141 ret
= input_register_device(idev
);
143 dev_err(&client
->dev
, "input_register_device() failed\n");
147 i2c_set_clientdata(client
, lp
);
151 free_irq(client
->irq
, lp
);
153 input_free_device(idev
);
160 static int pcf8574_kp_remove(struct i2c_client
*client
)
162 struct kp_data
*lp
= i2c_get_clientdata(client
);
164 free_irq(client
->irq
, lp
);
166 input_unregister_device(lp
->idev
);
173 static int pcf8574_kp_resume(struct device
*dev
)
175 struct i2c_client
*client
= to_i2c_client(dev
);
177 enable_irq(client
->irq
);
182 static int pcf8574_kp_suspend(struct device
*dev
)
184 struct i2c_client
*client
= to_i2c_client(dev
);
186 disable_irq(client
->irq
);
191 static const struct dev_pm_ops pcf8574_kp_pm_ops
= {
192 .suspend
= pcf8574_kp_suspend
,
193 .resume
= pcf8574_kp_resume
,
197 # define pcf8574_kp_resume NULL
198 # define pcf8574_kp_suspend NULL
201 static const struct i2c_device_id pcf8574_kp_id
[] = {
205 MODULE_DEVICE_TABLE(i2c
, pcf8574_kp_id
);
207 static struct i2c_driver pcf8574_kp_driver
= {
211 .pm
= &pcf8574_kp_pm_ops
,
214 .probe
= pcf8574_kp_probe
,
215 .remove
= pcf8574_kp_remove
,
216 .id_table
= pcf8574_kp_id
,
219 module_i2c_driver(pcf8574_kp_driver
);
221 MODULE_AUTHOR("Michael Hennerich");
222 MODULE_DESCRIPTION("Keypad input driver for 16 keys connected to PCF8574");
223 MODULE_LICENSE("GPL");