]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/keyboard/tca8418_keypad.c
Input: tca8418_keypad - add support for device tree bindings
[karo-tx-linux.git] / drivers / input / keyboard / tca8418_keypad.c
1 /*
2  * Driver for TCA8418 I2C keyboard
3  *
4  * Copyright (C) 2011 Fuel7, Inc.  All rights reserved.
5  *
6  * Author: Kyle Manna <kyle.manna@fuel7.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License v2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  * If you can't comply with GPLv2, alternative licensing terms may be
23  * arranged. Please contact Fuel7, Inc. (http://fuel7.com/) for proprietary
24  * alternative licensing inquiries.
25  */
26
27 #include <linux/types.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/workqueue.h>
34 #include <linux/gpio.h>
35 #include <linux/i2c.h>
36 #include <linux/input.h>
37 #include <linux/input/tca8418_keypad.h>
38 #include <linux/of.h>
39
40 /* TCA8418 hardware limits */
41 #define TCA8418_MAX_ROWS        8
42 #define TCA8418_MAX_COLS        10
43
44 /* TCA8418 register offsets */
45 #define REG_CFG                 0x01
46 #define REG_INT_STAT            0x02
47 #define REG_KEY_LCK_EC          0x03
48 #define REG_KEY_EVENT_A         0x04
49 #define REG_KEY_EVENT_B         0x05
50 #define REG_KEY_EVENT_C         0x06
51 #define REG_KEY_EVENT_D         0x07
52 #define REG_KEY_EVENT_E         0x08
53 #define REG_KEY_EVENT_F         0x09
54 #define REG_KEY_EVENT_G         0x0A
55 #define REG_KEY_EVENT_H         0x0B
56 #define REG_KEY_EVENT_I         0x0C
57 #define REG_KEY_EVENT_J         0x0D
58 #define REG_KP_LCK_TIMER        0x0E
59 #define REG_UNLOCK1             0x0F
60 #define REG_UNLOCK2             0x10
61 #define REG_GPIO_INT_STAT1      0x11
62 #define REG_GPIO_INT_STAT2      0x12
63 #define REG_GPIO_INT_STAT3      0x13
64 #define REG_GPIO_DAT_STAT1      0x14
65 #define REG_GPIO_DAT_STAT2      0x15
66 #define REG_GPIO_DAT_STAT3      0x16
67 #define REG_GPIO_DAT_OUT1       0x17
68 #define REG_GPIO_DAT_OUT2       0x18
69 #define REG_GPIO_DAT_OUT3       0x19
70 #define REG_GPIO_INT_EN1        0x1A
71 #define REG_GPIO_INT_EN2        0x1B
72 #define REG_GPIO_INT_EN3        0x1C
73 #define REG_KP_GPIO1            0x1D
74 #define REG_KP_GPIO2            0x1E
75 #define REG_KP_GPIO3            0x1F
76 #define REG_GPI_EM1             0x20
77 #define REG_GPI_EM2             0x21
78 #define REG_GPI_EM3             0x22
79 #define REG_GPIO_DIR1           0x23
80 #define REG_GPIO_DIR2           0x24
81 #define REG_GPIO_DIR3           0x25
82 #define REG_GPIO_INT_LVL1       0x26
83 #define REG_GPIO_INT_LVL2       0x27
84 #define REG_GPIO_INT_LVL3       0x28
85 #define REG_DEBOUNCE_DIS1       0x29
86 #define REG_DEBOUNCE_DIS2       0x2A
87 #define REG_DEBOUNCE_DIS3       0x2B
88 #define REG_GPIO_PULL1          0x2C
89 #define REG_GPIO_PULL2          0x2D
90 #define REG_GPIO_PULL3          0x2E
91
92 /* TCA8418 bit definitions */
93 #define CFG_AI                  BIT(7)
94 #define CFG_GPI_E_CFG           BIT(6)
95 #define CFG_OVR_FLOW_M          BIT(5)
96 #define CFG_INT_CFG             BIT(4)
97 #define CFG_OVR_FLOW_IEN        BIT(3)
98 #define CFG_K_LCK_IEN           BIT(2)
99 #define CFG_GPI_IEN             BIT(1)
100 #define CFG_KE_IEN              BIT(0)
101
102 #define INT_STAT_CAD_INT        BIT(4)
103 #define INT_STAT_OVR_FLOW_INT   BIT(3)
104 #define INT_STAT_K_LCK_INT      BIT(2)
105 #define INT_STAT_GPI_INT        BIT(1)
106 #define INT_STAT_K_INT          BIT(0)
107
108 /* TCA8418 register masks */
109 #define KEY_LCK_EC_KEC          0x7
110 #define KEY_EVENT_CODE          0x7f
111 #define KEY_EVENT_VALUE         0x80
112
113
114 static const struct i2c_device_id tca8418_id[] = {
115         { TCA8418_NAME, 8418, },
116         { }
117 };
118 MODULE_DEVICE_TABLE(i2c, tca8418_id);
119
120 #ifdef CONFIG_OF
121 static const struct of_device_id tca8418_dt_ids[] __devinitconst = {
122         { .compatible = "ti,tca8418", },
123         { }
124 };
125 MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
126 #endif
127
128 struct tca8418_keypad {
129         unsigned int irq;
130         unsigned int row_shift;
131
132         struct i2c_client *client;
133         struct input_dev *input;
134
135         /* Flexible array member, must be at end of struct */
136         unsigned short keymap[];
137 };
138
139 /*
140  * Write a byte to the TCA8418
141  */
142 static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
143                               int reg, u8 val)
144 {
145         int error;
146
147         error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
148         if (error < 0) {
149                 dev_err(&keypad_data->client->dev,
150                         "%s failed, reg: %d, val: %d, error: %d\n",
151                         __func__, reg, val, error);
152                 return error;
153         }
154
155         return 0;
156 }
157
158 /*
159  * Read a byte from the TCA8418
160  */
161 static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
162                              int reg, u8 *val)
163 {
164         int error;
165
166         error = i2c_smbus_read_byte_data(keypad_data->client, reg);
167         if (error < 0) {
168                 dev_err(&keypad_data->client->dev,
169                                 "%s failed, reg: %d, error: %d\n",
170                                 __func__, reg, error);
171                 return error;
172         }
173
174         *val = (u8)error;
175
176         return 0;
177 }
178
179 static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
180 {
181         int error, col, row;
182         u8 reg, state, code;
183
184         /* Initial read of the key event FIFO */
185         error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
186
187         /* Assume that key code 0 signifies empty FIFO */
188         while (error >= 0 && reg > 0) {
189                 state = reg & KEY_EVENT_VALUE;
190                 code  = reg & KEY_EVENT_CODE;
191
192                 row = code / TCA8418_MAX_COLS;
193                 col = code % TCA8418_MAX_COLS;
194
195                 row = (col) ? row : row - 1;
196                 col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
197
198                 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
199                 input_event(keypad_data->input, EV_MSC, MSC_SCAN, code);
200                 input_report_key(keypad_data->input,
201                                 keypad_data->keymap[code], state);
202
203                 /* Read for next loop */
204                 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
205         }
206
207         if (error < 0)
208                 dev_err(&keypad_data->client->dev,
209                         "unable to read REG_KEY_EVENT_A\n");
210
211         input_sync(keypad_data->input);
212 }
213
214 /*
215  * Threaded IRQ handler and this can (and will) sleep.
216  */
217 static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
218 {
219         struct tca8418_keypad *keypad_data = dev_id;
220         u8 reg;
221         int error;
222
223         error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
224         if (error) {
225                 dev_err(&keypad_data->client->dev,
226                         "unable to read REG_INT_STAT\n");
227                 goto exit;
228         }
229
230         if (reg & INT_STAT_OVR_FLOW_INT)
231                 dev_warn(&keypad_data->client->dev, "overflow occurred\n");
232
233         if (reg & INT_STAT_K_INT)
234                 tca8418_read_keypad(keypad_data);
235
236 exit:
237         /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
238         reg = 0xff;
239         error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
240         if (error)
241                 dev_err(&keypad_data->client->dev,
242                         "unable to clear REG_INT_STAT\n");
243
244         return IRQ_HANDLED;
245 }
246
247 /*
248  * Configure the TCA8418 for keypad operation
249  */
250 static int tca8418_configure(struct tca8418_keypad *keypad_data,
251                              u32 rows, u32 cols)
252 {
253         int reg, error;
254
255         /* Write config register, if this fails assume device not present */
256         error = tca8418_write_byte(keypad_data, REG_CFG,
257                                 CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
258         if (error < 0)
259                 return -ENODEV;
260
261
262         /* Assemble a mask for row and column registers */
263         reg  =  ~(~0 << rows);
264         reg += (~(~0 << cols)) << 8;
265
266         /* Set registers to keypad mode */
267         error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
268         error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
269         error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
270
271         /* Enable column debouncing */
272         error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
273         error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
274         error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
275
276         return error;
277 }
278
279 static int tca8418_keypad_probe(struct i2c_client *client,
280                                           const struct i2c_device_id *id)
281 {
282         const struct tca8418_keypad_platform_data *pdata =
283                                                 client->dev.platform_data;
284         struct tca8418_keypad *keypad_data;
285         struct input_dev *input;
286         const struct matrix_keymap_data *keymap_data = NULL;
287         u32 rows = 0, cols = 0;
288         bool rep = false;
289         bool irq_is_gpio = false;
290         int error, row_shift, max_keys;
291
292         /* Copy the platform data */
293         if (pdata) {
294                 if (!pdata->keymap_data) {
295                         dev_err(&client->dev, "no keymap data defined\n");
296                         return -EINVAL;
297                 }
298                 keymap_data = pdata->keymap_data;
299                 rows = pdata->rows;
300                 cols = pdata->cols;
301                 rep  = pdata->rep;
302                 irq_is_gpio = pdata->irq_is_gpio;
303         } else {
304                 struct device_node *np = client->dev.of_node;
305                 of_property_read_u32(np, "keypad,num-rows", &rows);
306                 of_property_read_u32(np, "keypad,num-columns", &cols);
307                 rep = of_property_read_bool(np, "keypad,autorepeat");
308         }
309
310         if (!rows || rows > TCA8418_MAX_ROWS) {
311                 dev_err(&client->dev, "invalid rows\n");
312                 return -EINVAL;
313         }
314
315         if (!cols || cols > TCA8418_MAX_COLS) {
316                 dev_err(&client->dev, "invalid columns\n");
317                 return -EINVAL;
318         }
319
320         /* Check i2c driver capabilities */
321         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
322                 dev_err(&client->dev, "%s adapter not supported\n",
323                         dev_driver_string(&client->adapter->dev));
324                 return -ENODEV;
325         }
326
327         row_shift = get_count_order(cols);
328         max_keys = rows << row_shift;
329
330         /* Allocate memory for keypad_data, keymap and input device */
331         keypad_data = kzalloc(sizeof(*keypad_data) +
332                         max_keys * sizeof(keypad_data->keymap[0]), GFP_KERNEL);
333         if (!keypad_data)
334                 return -ENOMEM;
335
336         keypad_data->client = client;
337         keypad_data->row_shift = row_shift;
338
339         /* Initialize the chip or fail if chip isn't present */
340         error = tca8418_configure(keypad_data, rows, cols);
341         if (error < 0)
342                 goto fail1;
343
344         /* Configure input device */
345         input = input_allocate_device();
346         if (!input) {
347                 error = -ENOMEM;
348                 goto fail1;
349         }
350         keypad_data->input = input;
351
352         input->name = client->name;
353         input->dev.parent = &client->dev;
354
355         input->id.bustype = BUS_I2C;
356         input->id.vendor  = 0x0001;
357         input->id.product = 0x001;
358         input->id.version = 0x0001;
359
360         error = matrix_keypad_build_keymap(keymap_data, NULL, rows, cols,
361                                            keypad_data->keymap, input);
362         if (error) {
363                 dev_dbg(&client->dev, "Failed to build keymap\n");
364                 goto fail2;
365         }
366
367         if (rep)
368                 __set_bit(EV_REP, input->evbit);
369         input_set_capability(input, EV_MSC, MSC_SCAN);
370
371         input_set_drvdata(input, keypad_data);
372
373         if (irq_is_gpio)
374                 client->irq = gpio_to_irq(client->irq);
375
376         error = request_threaded_irq(client->irq, NULL, tca8418_irq_handler,
377                                      IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
378                                      client->name, keypad_data);
379         if (error) {
380                 dev_dbg(&client->dev,
381                         "Unable to claim irq %d; error %d\n",
382                         client->irq, error);
383                 goto fail2;
384         }
385
386         error = input_register_device(input);
387         if (error) {
388                 dev_dbg(&client->dev,
389                         "Unable to register input device, error: %d\n", error);
390                 goto fail3;
391         }
392
393         i2c_set_clientdata(client, keypad_data);
394         return 0;
395
396 fail3:
397         free_irq(client->irq, keypad_data);
398 fail2:
399         input_free_device(input);
400 fail1:
401         kfree(keypad_data);
402         return error;
403 }
404
405 static int tca8418_keypad_remove(struct i2c_client *client)
406 {
407         struct tca8418_keypad *keypad_data = i2c_get_clientdata(client);
408
409         free_irq(keypad_data->client->irq, keypad_data);
410
411         input_unregister_device(keypad_data->input);
412
413         kfree(keypad_data);
414
415         return 0;
416 }
417
418 static struct i2c_driver tca8418_keypad_driver = {
419         .driver = {
420                 .name   = TCA8418_NAME,
421                 .owner  = THIS_MODULE,
422                 .of_match_table = of_match_ptr(tca8418_dt_ids),
423         },
424         .probe          = tca8418_keypad_probe,
425         .remove         = tca8418_keypad_remove,
426         .id_table       = tca8418_id,
427 };
428
429 static int __init tca8418_keypad_init(void)
430 {
431         return i2c_add_driver(&tca8418_keypad_driver);
432 }
433 subsys_initcall(tca8418_keypad_init);
434
435 static void __exit tca8418_keypad_exit(void)
436 {
437         i2c_del_driver(&tca8418_keypad_driver);
438 }
439 module_exit(tca8418_keypad_exit);
440
441 MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
442 MODULE_DESCRIPTION("Keypad driver for TCA8418");
443 MODULE_LICENSE("GPL");