2 * Driver for a keypad w/16 buttons connected to a PCF8574 I2C I/O expander
4 * Copyright 2005-2008 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
17 #define DRV_NAME "pcf8574_keypad"
19 static const unsigned char pcf8574_kp_btncode[] = {
40 unsigned short btncode[ARRAY_SIZE(pcf8574_kp_btncode)];
41 struct input_dev *idev;
42 struct i2c_client *client;
45 unsigned char laststate;
48 static short read_state(struct kp_data *lp)
50 unsigned char x, y, a, b;
52 i2c_smbus_write_byte(lp->client, 240);
53 x = 0xF & (~(i2c_smbus_read_byte(lp->client) >> 4));
55 i2c_smbus_write_byte(lp->client, 15);
56 y = 0xF & (~i2c_smbus_read_byte(lp->client));
58 for (a = 0; x > 0; a++)
60 for (b = 0; y > 0; b++)
63 return ((a - 1) * 4) + b;
66 static irqreturn_t pcf8574_kp_irq_handler(int irq, void *dev_id)
68 struct kp_data *lp = dev_id;
69 unsigned char nextstate = read_state(lp);
71 if (lp->laststate != nextstate) {
72 int key_down = nextstate <= ARRAY_SIZE(lp->btncode);
73 unsigned short keycode = key_down ?
74 lp->btncode[nextstate] : lp->btncode[lp->laststate];
76 input_report_key(lp->idev, keycode, key_down);
79 lp->laststate = nextstate;
85 static int __devinit pcf8574_kp_probe(struct i2c_client *client, const struct i2c_device_id *id)
88 struct input_dev *idev;
91 if (i2c_smbus_write_byte(client, 240) < 0) {
92 dev_err(&client->dev, "probe: write fail\n");
96 lp = kzalloc(sizeof(*lp), GFP_KERNEL);
100 idev = input_allocate_device();
102 dev_err(&client->dev, "Can't allocate input device\n");
110 idev->evbit[0] = BIT_MASK(EV_KEY);
111 idev->keycode = lp->btncode;
112 idev->keycodesize = sizeof(lp->btncode[0]);
113 idev->keycodemax = ARRAY_SIZE(lp->btncode);
115 for (i = 0; i < ARRAY_SIZE(pcf8574_kp_btncode); i++) {
116 lp->btncode[i] = pcf8574_kp_btncode[i];
117 __set_bit(lp->btncode[i] & KEY_MAX, idev->keybit);
120 sprintf(lp->name, DRV_NAME);
121 sprintf(lp->phys, "kp_data/input0");
123 idev->name = lp->name;
124 idev->phys = lp->phys;
125 idev->id.bustype = BUS_I2C;
126 idev->id.vendor = 0x0001;
127 idev->id.product = 0x0001;
128 idev->id.version = 0x0100;
130 input_set_drvdata(idev, lp);
132 ret = input_register_device(idev);
134 dev_err(&client->dev, "input_register_device() failed\n");
138 lp->laststate = read_state(lp);
140 ret = request_threaded_irq(client->irq, NULL, pcf8574_kp_irq_handler,
141 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
144 dev_err(&client->dev, "IRQ %d is not free\n", client->irq);
148 i2c_set_clientdata(client, lp);
152 input_unregister_device(idev);
154 input_set_drvdata(idev, NULL);
155 input_free_device(idev);
162 static int __devexit pcf8574_kp_remove(struct i2c_client *client)
164 struct kp_data *lp = i2c_get_clientdata(client);
166 free_irq(client->irq, lp);
168 input_unregister_device(lp->idev);
171 i2c_set_clientdata(client, NULL);
177 static int pcf8574_kp_resume(struct i2c_client *client)
179 enable_irq(client->irq);
184 static int pcf8574_kp_suspend(struct i2c_client *client, pm_message_t mesg)
186 disable_irq(client->irq);
191 # define pcf8574_kp_resume NULL
192 # define pcf8574_kp_suspend NULL
195 static const struct i2c_device_id pcf8574_kp_id[] = {
199 MODULE_DEVICE_TABLE(i2c, pcf8574_kp_id);
201 static struct i2c_driver pcf8574_kp_driver = {
204 .owner = THIS_MODULE,
206 .probe = pcf8574_kp_probe,
207 .remove = __devexit_p(pcf8574_kp_remove),
208 .suspend = pcf8574_kp_suspend,
209 .resume = pcf8574_kp_resume,
210 .id_table = pcf8574_kp_id,
213 static int __init pcf8574_kp_init(void)
215 return i2c_add_driver(&pcf8574_kp_driver);
217 module_init(pcf8574_kp_init);
219 static void __exit pcf8574_kp_exit(void)
221 i2c_del_driver(&pcf8574_kp_driver);
223 module_exit(pcf8574_kp_exit);
225 MODULE_AUTHOR("Michael Hennerich");
226 MODULE_DESCRIPTION("Keypad input driver for 16 keys connected to PCF8574");
227 MODULE_LICENSE("GPL");