blob: a5e8998047fec543031acec6195f5f483a452fd1 [file] [log] [blame]
Kyle Mannafb6c7212011-10-29 12:31:35 -07001/*
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>
Alban Bedele89e29b2012-11-05 10:55:25 -080038#include <linux/of.h>
Kyle Mannafb6c7212011-10-29 12:31:35 -070039
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
Kyle Mannafb6c7212011-10-29 12:31:35 -0700113struct tca8418_keypad {
Kyle Mannafb6c7212011-10-29 12:31:35 -0700114 struct i2c_client *client;
115 struct input_dev *input;
116
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800117 unsigned int row_shift;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700118};
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{
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800162 struct input_dev *input = keypad_data->input;
163 unsigned short *keymap = input->keycode;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700164 int error, col, row;
165 u8 reg, state, code;
166
Arnd Bergmannb7c3e5d2016-10-26 15:55:02 -0700167 do {
168 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
169 if (error < 0) {
170 dev_err(&keypad_data->client->dev,
171 "unable to read REG_KEY_EVENT_A\n");
172 break;
173 }
Kyle Mannafb6c7212011-10-29 12:31:35 -0700174
Arnd Bergmannb7c3e5d2016-10-26 15:55:02 -0700175 /* Assume that key code 0 signifies empty FIFO */
176 if (reg <= 0)
177 break;
178
Kyle Mannafb6c7212011-10-29 12:31:35 -0700179 state = reg & KEY_EVENT_VALUE;
180 code = reg & KEY_EVENT_CODE;
181
182 row = code / TCA8418_MAX_COLS;
183 col = code % TCA8418_MAX_COLS;
184
185 row = (col) ? row : row - 1;
186 col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
187
188 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800189 input_event(input, EV_MSC, MSC_SCAN, code);
190 input_report_key(input, keymap[code], state);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700191
Arnd Bergmannb7c3e5d2016-10-26 15:55:02 -0700192 } while (1);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700193
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800194 input_sync(input);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700195}
196
197/*
198 * Threaded IRQ handler and this can (and will) sleep.
199 */
200static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
201{
202 struct tca8418_keypad *keypad_data = dev_id;
203 u8 reg;
204 int error;
205
206 error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
207 if (error) {
208 dev_err(&keypad_data->client->dev,
209 "unable to read REG_INT_STAT\n");
Alban Bedelbf7f5312012-11-08 08:57:55 -0800210 return IRQ_NONE;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700211 }
212
Alban Bedelbf7f5312012-11-08 08:57:55 -0800213 if (!reg)
214 return IRQ_NONE;
215
Kyle Mannafb6c7212011-10-29 12:31:35 -0700216 if (reg & INT_STAT_OVR_FLOW_INT)
217 dev_warn(&keypad_data->client->dev, "overflow occurred\n");
218
219 if (reg & INT_STAT_K_INT)
220 tca8418_read_keypad(keypad_data);
221
Kyle Mannafb6c7212011-10-29 12:31:35 -0700222 /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
223 reg = 0xff;
224 error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
225 if (error)
226 dev_err(&keypad_data->client->dev,
227 "unable to clear REG_INT_STAT\n");
228
229 return IRQ_HANDLED;
230}
231
232/*
233 * Configure the TCA8418 for keypad operation
234 */
Alban Bedele89e29b2012-11-05 10:55:25 -0800235static int tca8418_configure(struct tca8418_keypad *keypad_data,
236 u32 rows, u32 cols)
Kyle Mannafb6c7212011-10-29 12:31:35 -0700237{
238 int reg, error;
239
240 /* Write config register, if this fails assume device not present */
241 error = tca8418_write_byte(keypad_data, REG_CFG,
242 CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
243 if (error < 0)
244 return -ENODEV;
245
246
247 /* Assemble a mask for row and column registers */
Alban Bedele89e29b2012-11-05 10:55:25 -0800248 reg = ~(~0 << rows);
249 reg += (~(~0 << cols)) << 8;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700250
251 /* Set registers to keypad mode */
252 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
253 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
254 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
255
256 /* Enable column debouncing */
257 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
258 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
259 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
260
261 return error;
262}
263
Bill Pemberton5298cc42012-11-23 21:38:25 -0800264static int tca8418_keypad_probe(struct i2c_client *client,
Kyle Mannafb6c7212011-10-29 12:31:35 -0700265 const struct i2c_device_id *id)
266{
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800267 struct device *dev = &client->dev;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700268 const struct tca8418_keypad_platform_data *pdata =
Dmitry Torokhovcdbe8a82012-11-14 08:12:05 -0800269 dev_get_platdata(dev);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700270 struct tca8418_keypad *keypad_data;
271 struct input_dev *input;
Alban Bedele89e29b2012-11-05 10:55:25 -0800272 const struct matrix_keymap_data *keymap_data = NULL;
273 u32 rows = 0, cols = 0;
274 bool rep = false;
275 bool irq_is_gpio = false;
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800276 int irq;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700277 int error, row_shift, max_keys;
278
279 /* Copy the platform data */
Alban Bedele89e29b2012-11-05 10:55:25 -0800280 if (pdata) {
281 if (!pdata->keymap_data) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800282 dev_err(dev, "no keymap data defined\n");
Alban Bedele89e29b2012-11-05 10:55:25 -0800283 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;
290 } else {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800291 struct device_node *np = dev->of_node;
Simon Glass43840412013-02-25 14:08:40 -0800292 int err;
293
294 err = matrix_keypad_parse_of_params(dev, &rows, &cols);
295 if (err)
296 return err;
Alban Bedele89e29b2012-11-05 10:55:25 -0800297 rep = of_property_read_bool(np, "keypad,autorepeat");
Kyle Mannafb6c7212011-10-29 12:31:35 -0700298 }
299
Alban Bedele89e29b2012-11-05 10:55:25 -0800300 if (!rows || rows > TCA8418_MAX_ROWS) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800301 dev_err(dev, "invalid rows\n");
Kyle Mannafb6c7212011-10-29 12:31:35 -0700302 return -EINVAL;
303 }
304
Alban Bedele89e29b2012-11-05 10:55:25 -0800305 if (!cols || cols > TCA8418_MAX_COLS) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800306 dev_err(dev, "invalid columns\n");
Kyle Mannafb6c7212011-10-29 12:31:35 -0700307 return -EINVAL;
308 }
309
310 /* Check i2c driver capabilities */
311 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800312 dev_err(dev, "%s adapter not supported\n",
Kyle Mannafb6c7212011-10-29 12:31:35 -0700313 dev_driver_string(&client->adapter->dev));
314 return -ENODEV;
315 }
316
Alban Bedele89e29b2012-11-05 10:55:25 -0800317 row_shift = get_count_order(cols);
318 max_keys = rows << row_shift;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700319
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800320 /* Allocate memory for keypad_data and input device */
321 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700322 if (!keypad_data)
323 return -ENOMEM;
324
Kyle Mannafb6c7212011-10-29 12:31:35 -0700325 keypad_data->client = client;
326 keypad_data->row_shift = row_shift;
327
328 /* Initialize the chip or fail if chip isn't present */
Alban Bedele89e29b2012-11-05 10:55:25 -0800329 error = tca8418_configure(keypad_data, rows, cols);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700330 if (error < 0)
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800331 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700332
333 /* Configure input device */
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800334 input = devm_input_allocate_device(dev);
335 if (!input)
336 return -ENOMEM;
337
Kyle Mannafb6c7212011-10-29 12:31:35 -0700338 keypad_data->input = input;
339
340 input->name = client->name;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700341 input->id.bustype = BUS_I2C;
342 input->id.vendor = 0x0001;
343 input->id.product = 0x001;
344 input->id.version = 0x0001;
345
Alban Bedele89e29b2012-11-05 10:55:25 -0800346 error = matrix_keypad_build_keymap(keymap_data, NULL, rows, cols,
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800347 NULL, input);
Dmitry Torokhov19328112012-05-10 22:37:08 -0700348 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800349 dev_err(dev, "Failed to build keymap\n");
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800350 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700351 }
Kyle Mannafb6c7212011-10-29 12:31:35 -0700352
Alban Bedele89e29b2012-11-05 10:55:25 -0800353 if (rep)
Kyle Mannafb6c7212011-10-29 12:31:35 -0700354 __set_bit(EV_REP, input->evbit);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700355 input_set_capability(input, EV_MSC, MSC_SCAN);
356
357 input_set_drvdata(input, keypad_data);
358
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800359 irq = client->irq;
Alban Bedele89e29b2012-11-05 10:55:25 -0800360 if (irq_is_gpio)
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800361 irq = gpio_to_irq(irq);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700362
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800363 error = devm_request_threaded_irq(dev, irq, NULL, tca8418_irq_handler,
364 IRQF_TRIGGER_FALLING |
365 IRQF_SHARED |
366 IRQF_ONESHOT,
367 client->name, keypad_data);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700368 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800369 dev_err(dev, "Unable to claim irq %d; error %d\n",
Kyle Mannafb6c7212011-10-29 12:31:35 -0700370 client->irq, error);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800371 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700372 }
373
374 error = input_register_device(input);
375 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800376 dev_err(dev, "Unable to register input device, error: %d\n",
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800377 error);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800378 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700379 }
380
Kyle Mannafb6c7212011-10-29 12:31:35 -0700381 return 0;
382}
383
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800384static const struct i2c_device_id tca8418_id[] = {
385 { TCA8418_NAME, 8418, },
386 { }
387};
388MODULE_DEVICE_TABLE(i2c, tca8418_id);
389
390#ifdef CONFIG_OF
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800391static const struct of_device_id tca8418_dt_ids[] = {
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800392 { .compatible = "ti,tca8418", },
393 { }
394};
395MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
Dr. H. Nikolaus Schallerc1613492014-04-24 23:43:36 -0700396
397/*
398 * The device tree based i2c loader looks for
399 * "i2c:" + second_component_of(property("compatible"))
400 * and therefore we need an alias to be found.
401 */
402MODULE_ALIAS("i2c:tca8418");
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800403#endif
404
Kyle Mannafb6c7212011-10-29 12:31:35 -0700405static struct i2c_driver tca8418_keypad_driver = {
406 .driver = {
407 .name = TCA8418_NAME,
Alban Bedele89e29b2012-11-05 10:55:25 -0800408 .of_match_table = of_match_ptr(tca8418_dt_ids),
Kyle Mannafb6c7212011-10-29 12:31:35 -0700409 },
410 .probe = tca8418_keypad_probe,
Kyle Mannafb6c7212011-10-29 12:31:35 -0700411 .id_table = tca8418_id,
412};
413
414static int __init tca8418_keypad_init(void)
415{
416 return i2c_add_driver(&tca8418_keypad_driver);
417}
418subsys_initcall(tca8418_keypad_init);
419
420static void __exit tca8418_keypad_exit(void)
421{
422 i2c_del_driver(&tca8418_keypad_driver);
423}
424module_exit(tca8418_keypad_exit);
425
426MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
427MODULE_DESCRIPTION("Keypad driver for TCA8418");
428MODULE_LICENSE("GPL");