1
2
3
4
5
6
7
8
9
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>
21
22#define TCA6416_INPUT 0
23#define TCA6416_OUTPUT 1
24#define TCA6416_INVERT 2
25#define TCA6416_DIRECTION 3
26
27static const struct i2c_device_id tca6416_id[] = {
28 { "tca6416-keys", 16, },
29 { "tca6408-keys", 8, },
30 { }
31};
32MODULE_DEVICE_TABLE(i2c, tca6416_id);
33
34struct tca6416_drv_data {
35 struct input_dev *input;
36 struct tca6416_button data[0];
37};
38
39struct tca6416_keypad_chip {
40 uint16_t reg_output;
41 uint16_t reg_direction;
42 uint16_t reg_input;
43
44 struct i2c_client *client;
45 struct input_dev *input;
46 struct delayed_work dwork;
47 int io_size;
48 int irqnum;
49 u16 pinmask;
50 bool use_polling;
51 struct tca6416_button buttons[0];
52};
53
54static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
55{
56 int error;
57
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);
61 if (error < 0) {
62 dev_err(&chip->client->dev,
63 "%s failed, reg: %d, val: %d, error: %d\n",
64 __func__, reg, val, error);
65 return error;
66 }
67
68 return 0;
69}
70
71static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
72{
73 int retval;
74
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);
78 if (retval < 0) {
79 dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
80 __func__, reg, retval);
81 return retval;
82 }
83
84 *val = (u16)retval;
85 return 0;
86}
87
88static void tca6416_keys_scan(struct tca6416_keypad_chip *chip)
89{
90 struct input_dev *input = chip->input;
91 u16 reg_val, val;
92 int error, i, pin_index;
93
94 error = tca6416_read_reg(chip, TCA6416_INPUT, ®_val);
95 if (error)
96 return;
97
98 reg_val &= chip->pinmask;
99
100
101 val = reg_val ^ chip->reg_input;
102 chip->reg_input = reg_val;
103
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;
110
111 input_event(input, type, button->code, !!state);
112 input_sync(input);
113 }
114
115 if (chip->pinmask & (1 << i))
116 pin_index++;
117 }
118}
119
120
121
122
123static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
124{
125 struct tca6416_keypad_chip *chip = dev_id;
126
127 tca6416_keys_scan(chip);
128
129 return IRQ_HANDLED;
130}
131
132static void tca6416_keys_work_func(struct work_struct *work)
133{
134 struct tca6416_keypad_chip *chip =
135 container_of(work, struct tca6416_keypad_chip, dwork.work);
136
137 tca6416_keys_scan(chip);
138 schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
139}
140
141static int tca6416_keys_open(struct input_dev *dev)
142{
143 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
144
145
146 tca6416_keys_scan(chip);
147
148 if (chip->use_polling)
149 schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
150 else
151 enable_irq(chip->irqnum);
152
153 return 0;
154}
155
156static void tca6416_keys_close(struct input_dev *dev)
157{
158 struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
159
160 if (chip->use_polling)
161 cancel_delayed_work_sync(&chip->dwork);
162 else
163 disable_irq(chip->irqnum);
164}
165
166static int tca6416_setup_registers(struct tca6416_keypad_chip *chip)
167{
168 int error;
169
170 error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
171 if (error)
172 return error;
173
174 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
175 if (error)
176 return error;
177
178
179 error = tca6416_write_reg(chip, TCA6416_DIRECTION,
180 chip->reg_direction | chip->pinmask);
181 if (error)
182 return error;
183
184 error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
185 if (error)
186 return error;
187
188 error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
189 if (error)
190 return error;
191
192 chip->reg_input &= chip->pinmask;
193
194 return 0;
195}
196
197static int tca6416_keypad_probe(struct i2c_client *client,
198 const struct i2c_device_id *id)
199{
200 struct tca6416_keys_platform_data *pdata;
201 struct tca6416_keypad_chip *chip;
202 struct input_dev *input;
203 int error;
204 int i;
205
206
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));
210 return -ENODEV;
211 }
212
213 pdata = dev_get_platdata(&client->dev);
214 if (!pdata) {
215 dev_dbg(&client->dev, "no platform data\n");
216 return -EINVAL;
217 }
218
219 chip = kzalloc(sizeof(struct tca6416_keypad_chip) +
220 pdata->nbuttons * sizeof(struct tca6416_button),
221 GFP_KERNEL);
222 input = input_allocate_device();
223 if (!chip || !input) {
224 error = -ENOMEM;
225 goto fail1;
226 }
227
228 chip->client = client;
229 chip->input = input;
230 chip->io_size = id->driver_data;
231 chip->pinmask = pdata->pinmask;
232 chip->use_polling = pdata->use_polling;
233
234 INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);
235
236 input->phys = "tca6416-keys/input0";
237 input->name = client->name;
238 input->dev.parent = &client->dev;
239
240 input->open = tca6416_keys_open;
241 input->close = tca6416_keys_close;
242
243 input->id.bustype = BUS_HOST;
244 input->id.vendor = 0x0001;
245 input->id.product = 0x0001;
246 input->id.version = 0x0100;
247
248
249 if (pdata->rep)
250 __set_bit(EV_REP, input->evbit);
251
252 for (i = 0; i < pdata->nbuttons; i++) {
253 unsigned int type;
254
255 chip->buttons[i] = pdata->buttons[i];
256 type = (pdata->buttons[i].type) ?: EV_KEY;
257 input_set_capability(input, type, pdata->buttons[i].code);
258 }
259
260 input_set_drvdata(input, chip);
261
262
263
264
265
266 error = tca6416_setup_registers(chip);
267 if (error)
268 goto fail1;
269
270 if (!chip->use_polling) {
271 if (pdata->irq_is_gpio)
272 chip->irqnum = gpio_to_irq(client->irq);
273 else
274 chip->irqnum = client->irq;
275
276 error = request_threaded_irq(chip->irqnum, NULL,
277 tca6416_keys_isr,
278 IRQF_TRIGGER_FALLING |
279 IRQF_ONESHOT,
280 "tca6416-keypad", chip);
281 if (error) {
282 dev_dbg(&client->dev,
283 "Unable to claim irq %d; error %d\n",
284 chip->irqnum, error);
285 goto fail1;
286 }
287 disable_irq(chip->irqnum);
288 }
289
290 error = input_register_device(input);
291 if (error) {
292 dev_dbg(&client->dev,
293 "Unable to register input device, error: %d\n", error);
294 goto fail2;
295 }
296
297 i2c_set_clientdata(client, chip);
298 device_init_wakeup(&client->dev, 1);
299
300 return 0;
301
302fail2:
303 if (!chip->use_polling) {
304 free_irq(chip->irqnum, chip);
305 enable_irq(chip->irqnum);
306 }
307fail1:
308 input_free_device(input);
309 kfree(chip);
310 return error;
311}
312
313static int tca6416_keypad_remove(struct i2c_client *client)
314{
315 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
316
317 if (!chip->use_polling) {
318 free_irq(chip->irqnum, chip);
319 enable_irq(chip->irqnum);
320 }
321
322 input_unregister_device(chip->input);
323 kfree(chip);
324
325 return 0;
326}
327
328#ifdef CONFIG_PM_SLEEP
329static int tca6416_keypad_suspend(struct device *dev)
330{
331 struct i2c_client *client = to_i2c_client(dev);
332 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
333
334 if (device_may_wakeup(dev))
335 enable_irq_wake(chip->irqnum);
336
337 return 0;
338}
339
340static int tca6416_keypad_resume(struct device *dev)
341{
342 struct i2c_client *client = to_i2c_client(dev);
343 struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
344
345 if (device_may_wakeup(dev))
346 disable_irq_wake(chip->irqnum);
347
348 return 0;
349}
350#endif
351
352static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops,
353 tca6416_keypad_suspend, tca6416_keypad_resume);
354
355static struct i2c_driver tca6416_keypad_driver = {
356 .driver = {
357 .name = "tca6416-keypad",
358 .pm = &tca6416_keypad_dev_pm_ops,
359 },
360 .probe = tca6416_keypad_probe,
361 .remove = tca6416_keypad_remove,
362 .id_table = tca6416_id,
363};
364
365static int __init tca6416_keypad_init(void)
366{
367 return i2c_add_driver(&tca6416_keypad_driver);
368}
369
370subsys_initcall(tca6416_keypad_init);
371
372static void __exit tca6416_keypad_exit(void)
373{
374 i2c_del_driver(&tca6416_keypad_driver);
375}
376module_exit(tca6416_keypad_exit);
377
378MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
379MODULE_DESCRIPTION("Keypad driver over tca6146 IO expander");
380MODULE_LICENSE("GPL");
381