blob: 44dd7689c5711c281f4e9df93428c24aeff9e4ed [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
Kyle Mannafb6c7212011-10-29 12:31:35 -070027#include <linux/delay.h>
Kyle Mannafb6c7212011-10-29 12:31:35 -070028#include <linux/i2c.h>
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -080029#include <linux/init.h>
Kyle Mannafb6c7212011-10-29 12:31:35 -070030#include <linux/input.h>
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -080031#include <linux/input/matrix_keypad.h>
32#include <linux/interrupt.h>
33#include <linux/module.h>
Alban Bedele89e29b2012-11-05 10:55:25 -080034#include <linux/of.h>
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -080035#include <linux/property.h>
36#include <linux/slab.h>
37#include <linux/types.h>
Kyle Mannafb6c7212011-10-29 12:31:35 -070038
39/* TCA8418 hardware limits */
40#define TCA8418_MAX_ROWS 8
41#define TCA8418_MAX_COLS 10
42
43/* TCA8418 register offsets */
44#define REG_CFG 0x01
45#define REG_INT_STAT 0x02
46#define REG_KEY_LCK_EC 0x03
47#define REG_KEY_EVENT_A 0x04
48#define REG_KEY_EVENT_B 0x05
49#define REG_KEY_EVENT_C 0x06
50#define REG_KEY_EVENT_D 0x07
51#define REG_KEY_EVENT_E 0x08
52#define REG_KEY_EVENT_F 0x09
53#define REG_KEY_EVENT_G 0x0A
54#define REG_KEY_EVENT_H 0x0B
55#define REG_KEY_EVENT_I 0x0C
56#define REG_KEY_EVENT_J 0x0D
57#define REG_KP_LCK_TIMER 0x0E
58#define REG_UNLOCK1 0x0F
59#define REG_UNLOCK2 0x10
60#define REG_GPIO_INT_STAT1 0x11
61#define REG_GPIO_INT_STAT2 0x12
62#define REG_GPIO_INT_STAT3 0x13
63#define REG_GPIO_DAT_STAT1 0x14
64#define REG_GPIO_DAT_STAT2 0x15
65#define REG_GPIO_DAT_STAT3 0x16
66#define REG_GPIO_DAT_OUT1 0x17
67#define REG_GPIO_DAT_OUT2 0x18
68#define REG_GPIO_DAT_OUT3 0x19
69#define REG_GPIO_INT_EN1 0x1A
70#define REG_GPIO_INT_EN2 0x1B
71#define REG_GPIO_INT_EN3 0x1C
72#define REG_KP_GPIO1 0x1D
73#define REG_KP_GPIO2 0x1E
74#define REG_KP_GPIO3 0x1F
75#define REG_GPI_EM1 0x20
76#define REG_GPI_EM2 0x21
77#define REG_GPI_EM3 0x22
78#define REG_GPIO_DIR1 0x23
79#define REG_GPIO_DIR2 0x24
80#define REG_GPIO_DIR3 0x25
81#define REG_GPIO_INT_LVL1 0x26
82#define REG_GPIO_INT_LVL2 0x27
83#define REG_GPIO_INT_LVL3 0x28
84#define REG_DEBOUNCE_DIS1 0x29
85#define REG_DEBOUNCE_DIS2 0x2A
86#define REG_DEBOUNCE_DIS3 0x2B
87#define REG_GPIO_PULL1 0x2C
88#define REG_GPIO_PULL2 0x2D
89#define REG_GPIO_PULL3 0x2E
90
91/* TCA8418 bit definitions */
92#define CFG_AI BIT(7)
93#define CFG_GPI_E_CFG BIT(6)
94#define CFG_OVR_FLOW_M BIT(5)
95#define CFG_INT_CFG BIT(4)
96#define CFG_OVR_FLOW_IEN BIT(3)
97#define CFG_K_LCK_IEN BIT(2)
98#define CFG_GPI_IEN BIT(1)
99#define CFG_KE_IEN BIT(0)
100
101#define INT_STAT_CAD_INT BIT(4)
102#define INT_STAT_OVR_FLOW_INT BIT(3)
103#define INT_STAT_K_LCK_INT BIT(2)
104#define INT_STAT_GPI_INT BIT(1)
105#define INT_STAT_K_INT BIT(0)
106
107/* TCA8418 register masks */
108#define KEY_LCK_EC_KEC 0x7
109#define KEY_EVENT_CODE 0x7f
110#define KEY_EVENT_VALUE 0x80
111
Kyle Mannafb6c7212011-10-29 12:31:35 -0700112struct tca8418_keypad {
Kyle Mannafb6c7212011-10-29 12:31:35 -0700113 struct i2c_client *client;
114 struct input_dev *input;
115
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800116 unsigned int row_shift;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700117};
118
119/*
120 * Write a byte to the TCA8418
121 */
122static int tca8418_write_byte(struct tca8418_keypad *keypad_data,
123 int reg, u8 val)
124{
125 int error;
126
127 error = i2c_smbus_write_byte_data(keypad_data->client, reg, val);
128 if (error < 0) {
129 dev_err(&keypad_data->client->dev,
130 "%s failed, reg: %d, val: %d, error: %d\n",
131 __func__, reg, val, error);
132 return error;
133 }
134
135 return 0;
136}
137
138/*
139 * Read a byte from the TCA8418
140 */
141static int tca8418_read_byte(struct tca8418_keypad *keypad_data,
142 int reg, u8 *val)
143{
144 int error;
145
146 error = i2c_smbus_read_byte_data(keypad_data->client, reg);
147 if (error < 0) {
148 dev_err(&keypad_data->client->dev,
149 "%s failed, reg: %d, error: %d\n",
150 __func__, reg, error);
151 return error;
152 }
153
154 *val = (u8)error;
155
156 return 0;
157}
158
159static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
160{
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800161 struct input_dev *input = keypad_data->input;
162 unsigned short *keymap = input->keycode;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700163 int error, col, row;
164 u8 reg, state, code;
165
Arnd Bergmannea4348c2016-10-26 15:55:02 -0700166 do {
167 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
168 if (error < 0) {
169 dev_err(&keypad_data->client->dev,
170 "unable to read REG_KEY_EVENT_A\n");
171 break;
172 }
Kyle Mannafb6c7212011-10-29 12:31:35 -0700173
Arnd Bergmannea4348c2016-10-26 15:55:02 -0700174 /* Assume that key code 0 signifies empty FIFO */
175 if (reg <= 0)
176 break;
177
Kyle Mannafb6c7212011-10-29 12:31:35 -0700178 state = reg & KEY_EVENT_VALUE;
179 code = reg & KEY_EVENT_CODE;
180
181 row = code / TCA8418_MAX_COLS;
182 col = code % TCA8418_MAX_COLS;
183
184 row = (col) ? row : row - 1;
185 col = (col) ? col - 1 : TCA8418_MAX_COLS - 1;
186
187 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800188 input_event(input, EV_MSC, MSC_SCAN, code);
189 input_report_key(input, keymap[code], state);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700190
191 /* Read for next loop */
192 error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
Arnd Bergmannea4348c2016-10-26 15:55:02 -0700193 } while (1);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700194
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800195 input_sync(input);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700196}
197
198/*
199 * Threaded IRQ handler and this can (and will) sleep.
200 */
201static irqreturn_t tca8418_irq_handler(int irq, void *dev_id)
202{
203 struct tca8418_keypad *keypad_data = dev_id;
204 u8 reg;
205 int error;
206
207 error = tca8418_read_byte(keypad_data, REG_INT_STAT, &reg);
208 if (error) {
209 dev_err(&keypad_data->client->dev,
210 "unable to read REG_INT_STAT\n");
Alban Bedelbf7f5312012-11-08 08:57:55 -0800211 return IRQ_NONE;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700212 }
213
Alban Bedelbf7f5312012-11-08 08:57:55 -0800214 if (!reg)
215 return IRQ_NONE;
216
Kyle Mannafb6c7212011-10-29 12:31:35 -0700217 if (reg & INT_STAT_OVR_FLOW_INT)
218 dev_warn(&keypad_data->client->dev, "overflow occurred\n");
219
220 if (reg & INT_STAT_K_INT)
221 tca8418_read_keypad(keypad_data);
222
Kyle Mannafb6c7212011-10-29 12:31:35 -0700223 /* Clear all interrupts, even IRQs we didn't check (GPI, CAD, LCK) */
224 reg = 0xff;
225 error = tca8418_write_byte(keypad_data, REG_INT_STAT, reg);
226 if (error)
227 dev_err(&keypad_data->client->dev,
228 "unable to clear REG_INT_STAT\n");
229
230 return IRQ_HANDLED;
231}
232
233/*
234 * Configure the TCA8418 for keypad operation
235 */
Alban Bedele89e29b2012-11-05 10:55:25 -0800236static int tca8418_configure(struct tca8418_keypad *keypad_data,
237 u32 rows, u32 cols)
Kyle Mannafb6c7212011-10-29 12:31:35 -0700238{
239 int reg, error;
240
241 /* Write config register, if this fails assume device not present */
242 error = tca8418_write_byte(keypad_data, REG_CFG,
243 CFG_INT_CFG | CFG_OVR_FLOW_IEN | CFG_KE_IEN);
244 if (error < 0)
245 return -ENODEV;
246
247
248 /* Assemble a mask for row and column registers */
Alban Bedele89e29b2012-11-05 10:55:25 -0800249 reg = ~(~0 << rows);
250 reg += (~(~0 << cols)) << 8;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700251
252 /* Set registers to keypad mode */
253 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO1, reg);
254 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO2, reg >> 8);
255 error |= tca8418_write_byte(keypad_data, REG_KP_GPIO3, reg >> 16);
256
257 /* Enable column debouncing */
258 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS1, reg);
259 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS2, reg >> 8);
260 error |= tca8418_write_byte(keypad_data, REG_DEBOUNCE_DIS3, reg >> 16);
261
262 return error;
263}
264
Bill Pemberton5298cc42012-11-23 21:38:25 -0800265static int tca8418_keypad_probe(struct i2c_client *client,
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800266 const struct i2c_device_id *id)
Kyle Mannafb6c7212011-10-29 12:31:35 -0700267{
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800268 struct device *dev = &client->dev;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700269 struct tca8418_keypad *keypad_data;
270 struct input_dev *input;
Alban Bedele89e29b2012-11-05 10:55:25 -0800271 u32 rows = 0, cols = 0;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700272 int error, row_shift, max_keys;
273
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800274 /* Check i2c driver capabilities */
275 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
276 dev_err(dev, "%s adapter not supported\n",
277 dev_driver_string(&client->adapter->dev));
278 return -ENODEV;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700279 }
280
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800281 error = matrix_keypad_parse_properties(dev, &rows, &cols);
282 if (error)
283 return error;
284
Alban Bedele89e29b2012-11-05 10:55:25 -0800285 if (!rows || rows > TCA8418_MAX_ROWS) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800286 dev_err(dev, "invalid rows\n");
Kyle Mannafb6c7212011-10-29 12:31:35 -0700287 return -EINVAL;
288 }
289
Alban Bedele89e29b2012-11-05 10:55:25 -0800290 if (!cols || cols > TCA8418_MAX_COLS) {
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800291 dev_err(dev, "invalid columns\n");
Kyle Mannafb6c7212011-10-29 12:31:35 -0700292 return -EINVAL;
293 }
294
Alban Bedele89e29b2012-11-05 10:55:25 -0800295 row_shift = get_count_order(cols);
296 max_keys = rows << row_shift;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700297
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800298 /* Allocate memory for keypad_data and input device */
299 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700300 if (!keypad_data)
301 return -ENOMEM;
302
Kyle Mannafb6c7212011-10-29 12:31:35 -0700303 keypad_data->client = client;
304 keypad_data->row_shift = row_shift;
305
306 /* Initialize the chip or fail if chip isn't present */
Alban Bedele89e29b2012-11-05 10:55:25 -0800307 error = tca8418_configure(keypad_data, rows, cols);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700308 if (error < 0)
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800309 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700310
311 /* Configure input device */
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800312 input = devm_input_allocate_device(dev);
313 if (!input)
314 return -ENOMEM;
315
Kyle Mannafb6c7212011-10-29 12:31:35 -0700316 keypad_data->input = input;
317
318 input->name = client->name;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700319 input->id.bustype = BUS_I2C;
320 input->id.vendor = 0x0001;
321 input->id.product = 0x001;
322 input->id.version = 0x0001;
323
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800324 error = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL, input);
Dmitry Torokhov19328112012-05-10 22:37:08 -0700325 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800326 dev_err(dev, "Failed to build keymap\n");
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800327 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700328 }
Kyle Mannafb6c7212011-10-29 12:31:35 -0700329
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800330 if (device_property_read_bool(dev, "keypad,autorepeat"))
Kyle Mannafb6c7212011-10-29 12:31:35 -0700331 __set_bit(EV_REP, input->evbit);
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800332
Kyle Mannafb6c7212011-10-29 12:31:35 -0700333 input_set_capability(input, EV_MSC, MSC_SCAN);
334
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800335 error = devm_request_threaded_irq(dev, client->irq,
336 NULL, tca8418_irq_handler,
337 IRQF_SHARED | IRQF_ONESHOT,
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800338 client->name, keypad_data);
Kyle Mannafb6c7212011-10-29 12:31:35 -0700339 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800340 dev_err(dev, "Unable to claim irq %d; error %d\n",
Kyle Mannafb6c7212011-10-29 12:31:35 -0700341 client->irq, error);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800342 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700343 }
344
345 error = input_register_device(input);
346 if (error) {
Dmitry Torokhov91c5d672012-11-14 08:20:21 -0800347 dev_err(dev, "Unable to register input device, error: %d\n",
Dmitry Torokhovefce8a42012-11-14 08:06:44 -0800348 error);
Dmitry Torokhov16ff7cb2012-11-05 11:13:11 -0800349 return error;
Kyle Mannafb6c7212011-10-29 12:31:35 -0700350 }
351
Kyle Mannafb6c7212011-10-29 12:31:35 -0700352 return 0;
353}
354
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800355static const struct i2c_device_id tca8418_id[] = {
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800356 { "tca8418", 8418, },
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800357 { }
358};
359MODULE_DEVICE_TABLE(i2c, tca8418_id);
360
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800361static const struct of_device_id tca8418_dt_ids[] = {
Dmitry Torokhov5cc0dfe2012-11-14 08:16:15 -0800362 { .compatible = "ti,tca8418", },
363 { }
364};
365MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
Dr. H. Nikolaus Schallerc1613492014-04-24 23:43:36 -0700366
Kyle Mannafb6c7212011-10-29 12:31:35 -0700367static struct i2c_driver tca8418_keypad_driver = {
368 .driver = {
Dmitry Torokhov8b1a3152017-01-17 14:18:50 -0800369 .name = "tca8418_keypad",
370 .of_match_table = tca8418_dt_ids,
Kyle Mannafb6c7212011-10-29 12:31:35 -0700371 },
372 .probe = tca8418_keypad_probe,
Kyle Mannafb6c7212011-10-29 12:31:35 -0700373 .id_table = tca8418_id,
374};
375
376static int __init tca8418_keypad_init(void)
377{
378 return i2c_add_driver(&tca8418_keypad_driver);
379}
380subsys_initcall(tca8418_keypad_init);
381
382static void __exit tca8418_keypad_exit(void)
383{
384 i2c_del_driver(&tca8418_keypad_driver);
385}
386module_exit(tca8418_keypad_exit);
387
388MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
389MODULE_DESCRIPTION("Keypad driver for TCA8418");
390MODULE_LICENSE("GPL");