]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/input/keyboard/tca8418_keypad.c
Input: stmfts - fix setting ABS_MT_POSITION_* maximum size
[mirror_ubuntu-jammy-kernel.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
fb6c721b 27#include <linux/delay.h>
fb6c721b 28#include <linux/i2c.h>
8b1a315b 29#include <linux/init.h>
fb6c721b 30#include <linux/input.h>
8b1a315b
DT
31#include <linux/input/matrix_keypad.h>
32#include <linux/interrupt.h>
33#include <linux/module.h>
e89e29b8 34#include <linux/of.h>
8b1a315b
DT
35#include <linux/property.h>
36#include <linux/slab.h>
37#include <linux/types.h>
fb6c721b
KM
38
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
fb6c721b 112struct tca8418_keypad {
fb6c721b
KM
113 struct i2c_client *client;
114 struct input_dev *input;
115
16ff7cb1 116 unsigned int row_shift;
fb6c721b
KM
117};
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{
16ff7cb1
DT
161 struct input_dev *input = keypad_data->input;
162 unsigned short *keymap = input->keycode;
fb6c721b
KM
163 int error, col, row;
164 u8 reg, state, code;
165
ea4348c8
AB
166 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 }
173
174 /* Assume that key code 0 signifies empty FIFO */
175 if (reg <= 0)
176 break;
fb6c721b 177
fb6c721b
KM
178 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);
16ff7cb1
DT
188 input_event(input, EV_MSC, MSC_SCAN, code);
189 input_report_key(input, keymap[code], state);
fb6c721b 190
ea4348c8 191 } while (1);
fb6c721b 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,
8b1a315b 264 const struct i2c_device_id *id)
fb6c721b 265{
efce8a41 266 struct device *dev = &client->dev;
fb6c721b
KM
267 struct tca8418_keypad *keypad_data;
268 struct input_dev *input;
e89e29b8 269 u32 rows = 0, cols = 0;
fb6c721b
KM
270 int error, row_shift, max_keys;
271
8b1a315b
DT
272 /* Check i2c driver capabilities */
273 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
274 dev_err(dev, "%s adapter not supported\n",
275 dev_driver_string(&client->adapter->dev));
276 return -ENODEV;
fb6c721b
KM
277 }
278
8b1a315b
DT
279 error = matrix_keypad_parse_properties(dev, &rows, &cols);
280 if (error)
281 return error;
282
e89e29b8 283 if (!rows || rows > TCA8418_MAX_ROWS) {
efce8a41 284 dev_err(dev, "invalid rows\n");
fb6c721b
KM
285 return -EINVAL;
286 }
287
e89e29b8 288 if (!cols || cols > TCA8418_MAX_COLS) {
efce8a41 289 dev_err(dev, "invalid columns\n");
fb6c721b
KM
290 return -EINVAL;
291 }
292
e89e29b8
AB
293 row_shift = get_count_order(cols);
294 max_keys = rows << row_shift;
fb6c721b 295
16ff7cb1
DT
296 /* Allocate memory for keypad_data and input device */
297 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
fb6c721b
KM
298 if (!keypad_data)
299 return -ENOMEM;
300
fb6c721b
KM
301 keypad_data->client = client;
302 keypad_data->row_shift = row_shift;
303
304 /* Initialize the chip or fail if chip isn't present */
e89e29b8 305 error = tca8418_configure(keypad_data, rows, cols);
fb6c721b 306 if (error < 0)
16ff7cb1 307 return error;
fb6c721b
KM
308
309 /* Configure input device */
16ff7cb1
DT
310 input = devm_input_allocate_device(dev);
311 if (!input)
312 return -ENOMEM;
313
fb6c721b
KM
314 keypad_data->input = input;
315
316 input->name = client->name;
fb6c721b
KM
317 input->id.bustype = BUS_I2C;
318 input->id.vendor = 0x0001;
319 input->id.product = 0x001;
320 input->id.version = 0x0001;
321
8b1a315b 322 error = matrix_keypad_build_keymap(NULL, NULL, rows, cols, NULL, input);
1932811f 323 if (error) {
91c5d67f 324 dev_err(dev, "Failed to build keymap\n");
16ff7cb1 325 return error;
1932811f 326 }
fb6c721b 327
8b1a315b 328 if (device_property_read_bool(dev, "keypad,autorepeat"))
fb6c721b 329 __set_bit(EV_REP, input->evbit);
fb6c721b 330
8b1a315b 331 input_set_capability(input, EV_MSC, MSC_SCAN);
fb6c721b 332
8b1a315b
DT
333 error = devm_request_threaded_irq(dev, client->irq,
334 NULL, tca8418_irq_handler,
335 IRQF_SHARED | IRQF_ONESHOT,
16ff7cb1 336 client->name, keypad_data);
fb6c721b 337 if (error) {
91c5d67f 338 dev_err(dev, "Unable to claim irq %d; error %d\n",
fb6c721b 339 client->irq, error);
16ff7cb1 340 return error;
fb6c721b
KM
341 }
342
343 error = input_register_device(input);
344 if (error) {
91c5d67f 345 dev_err(dev, "Unable to register input device, error: %d\n",
efce8a41 346 error);
16ff7cb1 347 return error;
fb6c721b
KM
348 }
349
fb6c721b
KM
350 return 0;
351}
352
5cc0dfe0 353static const struct i2c_device_id tca8418_id[] = {
8b1a315b 354 { "tca8418", 8418, },
5cc0dfe0
DT
355 { }
356};
357MODULE_DEVICE_TABLE(i2c, tca8418_id);
358
0fe763c5 359static const struct of_device_id tca8418_dt_ids[] = {
5cc0dfe0
DT
360 { .compatible = "ti,tca8418", },
361 { }
362};
363MODULE_DEVICE_TABLE(of, tca8418_dt_ids);
c1613497 364
fb6c721b
KM
365static struct i2c_driver tca8418_keypad_driver = {
366 .driver = {
8b1a315b
DT
367 .name = "tca8418_keypad",
368 .of_match_table = tca8418_dt_ids,
fb6c721b
KM
369 },
370 .probe = tca8418_keypad_probe,
fb6c721b
KM
371 .id_table = tca8418_id,
372};
373
374static int __init tca8418_keypad_init(void)
375{
376 return i2c_add_driver(&tca8418_keypad_driver);
377}
378subsys_initcall(tca8418_keypad_init);
379
380static void __exit tca8418_keypad_exit(void)
381{
382 i2c_del_driver(&tca8418_keypad_driver);
383}
384module_exit(tca8418_keypad_exit);
385
386MODULE_AUTHOR("Kyle Manna <kyle.manna@fuel7.com>");
387MODULE_DESCRIPTION("Keypad driver for TCA8418");
388MODULE_LICENSE("GPL");