1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2012 Wolfram Sang, Pengutronix <kernel@pengutronix.de>
8 #include <linux/input.h>
9 #include <linux/input/matrix_keypad.h>
10 #include <linux/input/lm8333.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #define LM8333_FIFO_READ 0x20
16 #define LM8333_DEBOUNCE 0x22
17 #define LM8333_READ_INT 0xD0
18 #define LM8333_ACTIVE 0xE4
19 #define LM8333_READ_ERROR 0xF0
21 #define LM8333_KEYPAD_IRQ (1 << 0)
22 #define LM8333_ERROR_IRQ (1 << 3)
24 #define LM8333_ERROR_KEYOVR 0x04
25 #define LM8333_ERROR_FIFOOVR 0x40
27 #define LM8333_FIFO_TRANSFER_SIZE 16
29 #define LM8333_NUM_ROWS 8
30 #define LM8333_NUM_COLS 16
31 #define LM8333_ROW_SHIFT 4
34 struct i2c_client
*client
;
35 struct input_dev
*input
;
36 unsigned short keycodes
[LM8333_NUM_ROWS
<< LM8333_ROW_SHIFT
];
39 /* The accessors try twice because the first access may be needed for wakeup */
40 #define LM8333_READ_RETRIES 2
42 int lm8333_read8(struct lm8333
*lm8333
, u8 cmd
)
47 ret
= i2c_smbus_read_byte_data(lm8333
->client
, cmd
);
48 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
53 int lm8333_write8(struct lm8333
*lm8333
, u8 cmd
, u8 val
)
58 ret
= i2c_smbus_write_byte_data(lm8333
->client
, cmd
, val
);
59 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
64 int lm8333_read_block(struct lm8333
*lm8333
, u8 cmd
, u8 len
, u8
*buf
)
69 ret
= i2c_smbus_read_i2c_block_data(lm8333
->client
,
71 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
76 static void lm8333_key_handler(struct lm8333
*lm8333
)
78 struct input_dev
*input
= lm8333
->input
;
79 u8 keys
[LM8333_FIFO_TRANSFER_SIZE
];
83 ret
= lm8333_read_block(lm8333
, LM8333_FIFO_READ
,
84 LM8333_FIFO_TRANSFER_SIZE
, keys
);
85 if (ret
!= LM8333_FIFO_TRANSFER_SIZE
) {
86 dev_err(&lm8333
->client
->dev
,
87 "Error %d while reading FIFO\n", ret
);
91 for (i
= 0; i
< LM8333_FIFO_TRANSFER_SIZE
&& keys
[i
]; i
++) {
92 pressed
= keys
[i
] & 0x80;
93 code
= keys
[i
] & 0x7f;
95 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
96 input_report_key(input
, lm8333
->keycodes
[code
], pressed
);
102 static irqreturn_t
lm8333_irq_thread(int irq
, void *data
)
104 struct lm8333
*lm8333
= data
;
105 u8 status
= lm8333_read8(lm8333
, LM8333_READ_INT
);
110 if (status
& LM8333_ERROR_IRQ
) {
111 u8 err
= lm8333_read8(lm8333
, LM8333_READ_ERROR
);
113 if (err
& (LM8333_ERROR_KEYOVR
| LM8333_ERROR_FIFOOVR
)) {
114 u8 dummy
[LM8333_FIFO_TRANSFER_SIZE
];
116 lm8333_read_block(lm8333
, LM8333_FIFO_READ
,
117 LM8333_FIFO_TRANSFER_SIZE
, dummy
);
119 dev_err(&lm8333
->client
->dev
, "Got error %02x\n", err
);
122 if (status
& LM8333_KEYPAD_IRQ
)
123 lm8333_key_handler(lm8333
);
128 static int lm8333_probe(struct i2c_client
*client
)
130 const struct lm8333_platform_data
*pdata
=
131 dev_get_platdata(&client
->dev
);
132 struct lm8333
*lm8333
;
133 struct input_dev
*input
;
134 int err
, active_time
;
139 active_time
= pdata
->active_time
?: 500;
140 if (active_time
/ 3 <= pdata
->debounce_time
/ 3) {
141 dev_err(&client
->dev
, "Active time not big enough!\n");
145 lm8333
= devm_kzalloc(&client
->dev
, sizeof(*lm8333
), GFP_KERNEL
);
149 input
= devm_input_allocate_device(&client
->dev
);
153 lm8333
->client
= client
;
154 lm8333
->input
= input
;
156 input
->name
= client
->name
;
157 input
->id
.bustype
= BUS_I2C
;
159 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
161 err
= matrix_keypad_build_keymap(pdata
->matrix_data
, NULL
,
162 LM8333_NUM_ROWS
, LM8333_NUM_COLS
,
163 lm8333
->keycodes
, input
);
167 if (pdata
->debounce_time
) {
168 err
= lm8333_write8(lm8333
, LM8333_DEBOUNCE
,
169 pdata
->debounce_time
/ 3);
171 dev_warn(&client
->dev
, "Unable to set debounce time\n");
174 if (pdata
->active_time
) {
175 err
= lm8333_write8(lm8333
, LM8333_ACTIVE
,
176 pdata
->active_time
/ 3);
178 dev_warn(&client
->dev
, "Unable to set active time\n");
181 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
182 NULL
, lm8333_irq_thread
,
183 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
188 err
= input_register_device(input
);
192 i2c_set_clientdata(client
, lm8333
);
196 static const struct i2c_device_id lm8333_id
[] = {
200 MODULE_DEVICE_TABLE(i2c
, lm8333_id
);
202 static struct i2c_driver lm8333_driver
= {
206 .probe
= lm8333_probe
,
207 .id_table
= lm8333_id
,
209 module_i2c_driver(lm8333_driver
);
211 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
212 MODULE_DESCRIPTION("LM8333 keyboard driver");
213 MODULE_LICENSE("GPL v2");