Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / keyboard / tca8418_keypad.c
CommitLineData
fb6c721b
KM
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>
e89e29b8 38#include <linux/of.h>
fb6c721b
KM
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
fb6c721b 113struct tca8418_keypad {
fb6c721b
KM
114 struct i2c_client *client;
115 struct input_dev *input;
116
16ff7cb1 117 unsigned int row_shift;
fb6c721b
KM
118};
119
120/*
121 * Write a byte to the TCA8418
122 */
123static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
124 int reg, u8 val)
125{
126 int error;
127
128 error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
129 if (error < 0) {
130 dev_err(&keypad_data->client->dev,
131 "%s failed, reg: %d, val: %d, error: %d\n",
132 __func__, reg, val, error);
133 return error;
134 }
135
136 return 0;
137}
138
139/*
140 * Read a byte from the TCA8418
141 */
142static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
143 int reg, u8 *val)
144{
145 int error;
146
147 error = i2c_smbus_read_byte_data(keypad_data->client, reg);
148 if (error < 0) {
149 dev_err(&keypad_data->client->dev,
150 "%s failed, reg: %d, error: %d\n",
151 __func__, reg, error);
152 return error;
153 }
154
155 *val = (u8)error;
156
157 return 0;
158}
159
160static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
161{
16ff7cb1
DT
162 struct input_dev *input = keypad_data->input;
163 unsigned short *keymap = input->keycode;
fb6c721b
KM
164 int error, col, row;
165 u8 reg, state, code;
166
167 /* Initial read of the key event FIFO */
168 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
169
170 /* Assume that key code 0 signifies empty FIFO */
171 while (error >= 0 && reg > 0) {
172 state = reg & KEY_EVENT_VALUE;
173 code = reg & KEY_EVENT_CODE;
174
175 row = code / TCA8418_MAX_COLS;
176 col = code % TCA8418_MAX_COLS;
177
178 row = (col) ? row : row - 1;
179 col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
180
181 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
16ff7cb1
DT
182 input_event(input, EV_MSC, MSC_SCAN, code);
183 input_report_key(input, keymap[code], state);
fb6c721b
KM
184
185 /* Read for next loop */
186 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
187 }
188
189 if (error < 0)
190 dev_err(&keypad_data->client->dev,
191 "unable to read REG_KEY_EVENT_A\n");
192
16ff7cb1 193 input_sync(input);
fb6c721b
KM
194}
195
196/*
197 * Threaded IRQ handler and this can (and will) sleep.
198 */
199static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
200{
201 struct tca8418_keypad *keypad_data = dev_id;
202 u8 reg;
203 int error;
204
205 error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
206 if (error) {
207 dev_err(&keypad_data->client->dev,
208 "unable to read REG_INT_STAT\n");
bf7f5316 209 return IRQ_NONE;
fb6c721b
KM
210 }
211
bf7f5316
AB
212 if (!reg)
213 return IRQ_NONE;
214
fb6c721b
KM
215 if (reg & INT_STAT_OVR_FLOW_INT)
216 dev_warn(&keypad_data->client->dev, "overflow occurred\n");
217
218 if (reg & INT_STAT_K_INT)
219 tca8418_read_keypad(keypad_data);
220
fb6c721b
KM
221 /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
222 reg = 0xff;
223 error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
224 if (error)
225 dev_err(&keypad_data->client->dev,
226 "unable to clear REG_INT_STAT\n");
227
228 return IRQ_HANDLED;
229}
230
231/*
232 * Configure the TCA8418 for keypad operation
233 */
e89e29b8
AB
234static int tca8418_configure(struct tca8418_keypad *keypad_data,
235 u32 rows, u32 cols)
fb6c721b
KM
236{
237 int reg, error;
238
239 /* Write config register, if this fails assume device not present */
240 error = tca8418_write_byte(keypad_data, REG_CFG,
241 CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
242 if (error < 0)
243 return -ENODEV;
244
245
246 /* Assemble a mask for row and column registers */
e89e29b8
AB
247 reg = ~(~0 << rows);
248 reg += (~(~0 << cols)) << 8;
fb6c721b
KM
249
250 /* Set registers to keypad mode */
251 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
252 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
253 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
254
255 /* Enable column debouncing */
256 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
257 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
258 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
259
260 return error;
261}
262
5298cc4c 263static int tca8418_keypad_probe(struct i2c_client *client,
fb6c721b
KM
264 const struct i2c_device_id *id)
265{
efce8a41 266 struct device *dev = &client->dev;
fb6c721b 267 const struct tca8418_keypad_platform_data *pdata =
cdbe8a86 268 dev_get_platdata(dev);
fb6c721b
KM
269 struct tca8418_keypad *keypad_data;
270 struct input_dev *input;
e89e29b8
AB
271 const struct matrix_keymap_data *keymap_data = NULL;
272 u32 rows = 0, cols = 0;
273 bool rep = false;
274 bool irq_is_gpio = false;
16ff7cb1 275 int irq;
fb6c721b 276 int error, row_shift, max_keys;
4a878a06 277 unsigned long trigger = 0;
fb6c721b
KM
278
279 /* Copy the platform data */
e89e29b8
AB
280 if (pdata) {
281 if (!pdata->keymap_data) {
efce8a41 282 dev_err(dev, "no keymap data defined\n");
e89e29b8
AB
283 return -EINVAL;
284 }
285 keymap_data = pdata->keymap_data;
286 rows = pdata->rows;
287 cols = pdata->cols;
288 rep = pdata->rep;
289 irq_is_gpio = pdata->irq_is_gpio;
4a878a06 290 trigger = IRQF_TRIGGER_FALLING;
e89e29b8 291 } else {
efce8a41 292 struct device_node *np = dev->of_node;
43840415
SG
293 int err;
294
295 err = matrix_keypad_parse_of_params(dev, &rows, &cols);
296 if (err)
297 return err;
e89e29b8 298 rep = of_property_read_bool(np, "keypad,autorepeat");
fb6c721b
KM
299 }
300
e89e29b8 301 if (!rows || rows > TCA8418_MAX_ROWS) {
efce8a41 302 dev_err(dev, "invalid rows\n");
fb6c721b
KM
303 return -EINVAL;
304 }
305
e89e29b8 306 if (!cols || cols > TCA8418_MAX_COLS) {
efce8a41 307 dev_err(dev, "invalid columns\n");
fb6c721b
KM
308 return -EINVAL;
309 }
310
311 /* Check i2c driver capabilities */
312 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
efce8a41 313 dev_err(dev, "%s adapter not supported\n",
fb6c721b
KM
314 dev_driver_string(&client->adapter->dev));
315 return -ENODEV;
316 }
317
e89e29b8
AB
318 row_shift = get_count_order(cols);
319 max_keys = rows << row_shift;
fb6c721b 320
16ff7cb1
DT
321 /* Allocate memory for keypad_data and input device */
322 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
fb6c721b
KM
323 if (!keypad_data)
324 return -ENOMEM;
325
fb6c721b
KM
326 keypad_data->client = client;
327 keypad_data->row_shift = row_shift;
328
329 /* Initialize the chip or fail if chip isn't present */
e89e29b8 330 error = tca8418_configure(keypad_data, rows, cols);
fb6c721b 331 if (error < 0)
16ff7cb1 332 return error;
fb6c721b
KM
333
334 /* Configure input device */
16ff7cb1
DT
335 input = devm_input_allocate_device(dev);
336 if (!input)
337 return -ENOMEM;
338
fb6c721b
KM
339 keypad_data->input = input;
340
341 input->name = client->name;
fb6c721b
KM
342 input->id.bustype = BUS_I2C;
343 input->id.vendor = 0x0001;
344 input->id.product = 0x001;
345 input->id.version = 0x0001;
346
e89e29b8 347 error = matrix_keypad_build_keymap(keymap_data, NULL, rows, cols,
16ff7cb1 348 NULL, input);
1932811f 349 if (error) {
91c5d67f 350 dev_err(dev, "Failed to build keymap\n");
16ff7cb1 351 return error;
1932811f 352 }
fb6c721b 353
e89e29b8 354 if (rep)
fb6c721b 355 __set_bit(EV_REP, input->evbit);
fb6c721b
KM
356 input_set_capability(input, EV_MSC, MSC_SCAN);
357
358 input_set_drvdata(input, keypad_data);
359
16ff7cb1 360 irq = client->irq;
e89e29b8 361 if (irq_is_gpio)
16ff7cb1 362 irq = gpio_to_irq(irq);
fb6c721b 363
16ff7cb1 364 error = devm_request_threaded_irq(dev, irq, NULL, tca8418_irq_handler,
4a878a06 365 trigger | IRQF_SHARED | IRQF_ONESHOT,
16ff7cb1 366 client->name, keypad_data);
fb6c721b 367 if (error) {
91c5d67f 368 dev_err(dev, "Unable to claim irq %d; error %d\n",
fb6c721b 369 client->irq, error);
16ff7cb1 370 return error;
fb6c721b
KM
371 }
372
373 error = input_register_device(input);
374 if (error) {
91c5d67f 375 dev_err(dev, "Unable to register input device, error: %d\n",
efce8a41 376 error);
16ff7cb1 377 return error;
fb6c721b
KM
378 }
379
fb6c721b
KM
380 return 0;
381}
382
5cc0dfe0
DT
383static const struct i2c_device_id tca8418_id[] = {
384 { TCA8418_NAME, 8418, },
385 { }
386};
387MODULE_DEVICE_TABLE(i2c, tca8418_id);
388
389#ifdef CONFIG_OF
0fe763c5 390static const struct of_device_id tca8418_dt_ids[] = {
5cc0dfe0
DT
391 { .compatible = "ti,tca8418", },
392 { }
393};
394MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
395#endif
396
fb6c721b
KM
397static struct i2c_driver tca8418_keypad_driver = {
398 .driver = {
399 .name = TCA8418_NAME,
400 .owner = THIS_MODULE,
e89e29b8 401 .of_match_table = of_match_ptr(tca8418_dt_ids),
fb6c721b
KM
402 },
403 .probe = tca8418_keypad_probe,
fb6c721b
KM
404 .id_table = tca8418_id,
405};
406
407static int __init tca8418_keypad_init(void)
408{
409 return i2c_add_driver(&tca8418_keypad_driver);
410}
411subsys_initcall(tca8418_keypad_init);
412
413static void __exit tca8418_keypad_exit(void)
414{
415 i2c_del_driver(&tca8418_keypad_driver);
416}
417module_exit(tca8418_keypad_exit);
418
419MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
420MODULE_DESCRIPTION("Keypad driver for TCA8418");
421MODULE_LICENSE("GPL");