3 * Copyright (C) 2012 Wolfram Sang, Pengutronix <kernel@pengutronix.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License.
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/irq.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/input/matrix_keypad.h>
16 #include <linux/input/lm8333.h>
18 #define LM8333_FIFO_READ 0x20
19 #define LM8333_DEBOUNCE 0x22
20 #define LM8333_READ_INT 0xD0
21 #define LM8333_ACTIVE 0xE4
22 #define LM8333_READ_ERROR 0xF0
24 #define LM8333_KEYPAD_IRQ (1 << 0)
25 #define LM8333_ERROR_IRQ (1 << 3)
27 #define LM8333_ERROR_KEYOVR 0x04
28 #define LM8333_ERROR_FIFOOVR 0x40
30 #define LM8333_FIFO_TRANSFER_SIZE 16
32 #define LM8333_NUM_ROWS 8
33 #define LM8333_NUM_COLS 16
34 #define LM8333_ROW_SHIFT 4
37 struct i2c_client
*client
;
38 struct input_dev
*input
;
39 unsigned short keycodes
[LM8333_NUM_ROWS
<< LM8333_ROW_SHIFT
];
42 /* The accessors try twice because the first access may be needed for wakeup */
43 #define LM8333_READ_RETRIES 2
45 int lm8333_read8(struct lm8333
*lm8333
, u8 cmd
)
50 ret
= i2c_smbus_read_byte_data(lm8333
->client
, cmd
);
51 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
56 int lm8333_write8(struct lm8333
*lm8333
, u8 cmd
, u8 val
)
61 ret
= i2c_smbus_write_byte_data(lm8333
->client
, cmd
, val
);
62 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
67 int lm8333_read_block(struct lm8333
*lm8333
, u8 cmd
, u8 len
, u8
*buf
)
72 ret
= i2c_smbus_read_i2c_block_data(lm8333
->client
,
74 } while (ret
< 0 && retries
++ < LM8333_READ_RETRIES
);
79 static void lm8333_key_handler(struct lm8333
*lm8333
)
81 struct input_dev
*input
= lm8333
->input
;
82 u8 keys
[LM8333_FIFO_TRANSFER_SIZE
];
86 ret
= lm8333_read_block(lm8333
, LM8333_FIFO_READ
,
87 LM8333_FIFO_TRANSFER_SIZE
, keys
);
88 if (ret
!= LM8333_FIFO_TRANSFER_SIZE
) {
89 dev_err(&lm8333
->client
->dev
,
90 "Error %d while reading FIFO\n", ret
);
94 for (i
= 0; i
< LM8333_FIFO_TRANSFER_SIZE
&& keys
[i
]; i
++) {
95 pressed
= keys
[i
] & 0x80;
96 code
= keys
[i
] & 0x7f;
98 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
99 input_report_key(input
, lm8333
->keycodes
[code
], pressed
);
105 static irqreturn_t
lm8333_irq_thread(int irq
, void *data
)
107 struct lm8333
*lm8333
= data
;
108 u8 status
= lm8333_read8(lm8333
, LM8333_READ_INT
);
113 if (status
& LM8333_ERROR_IRQ
) {
114 u8 err
= lm8333_read8(lm8333
, LM8333_READ_ERROR
);
116 if (err
& (LM8333_ERROR_KEYOVR
| LM8333_ERROR_FIFOOVR
)) {
117 u8 dummy
[LM8333_FIFO_TRANSFER_SIZE
];
119 lm8333_read_block(lm8333
, LM8333_FIFO_READ
,
120 LM8333_FIFO_TRANSFER_SIZE
, dummy
);
122 dev_err(&lm8333
->client
->dev
, "Got error %02x\n", err
);
125 if (status
& LM8333_KEYPAD_IRQ
)
126 lm8333_key_handler(lm8333
);
131 static int lm8333_probe(struct i2c_client
*client
,
132 const struct i2c_device_id
*id
)
134 const struct lm8333_platform_data
*pdata
=
135 dev_get_platdata(&client
->dev
);
136 struct lm8333
*lm8333
;
137 struct input_dev
*input
;
138 int err
, active_time
;
143 active_time
= pdata
->active_time
?: 500;
144 if (active_time
/ 3 <= pdata
->debounce_time
/ 3) {
145 dev_err(&client
->dev
, "Active time not big enough!\n");
149 lm8333
= kzalloc(sizeof(*lm8333
), GFP_KERNEL
);
150 input
= input_allocate_device();
151 if (!lm8333
|| !input
) {
156 lm8333
->client
= client
;
157 lm8333
->input
= input
;
159 input
->name
= client
->name
;
160 input
->dev
.parent
= &client
->dev
;
161 input
->id
.bustype
= BUS_I2C
;
163 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
165 err
= matrix_keypad_build_keymap(pdata
->matrix_data
, NULL
,
166 LM8333_NUM_ROWS
, LM8333_NUM_COLS
,
167 lm8333
->keycodes
, input
);
171 if (pdata
->debounce_time
) {
172 err
= lm8333_write8(lm8333
, LM8333_DEBOUNCE
,
173 pdata
->debounce_time
/ 3);
175 dev_warn(&client
->dev
, "Unable to set debounce time\n");
178 if (pdata
->active_time
) {
179 err
= lm8333_write8(lm8333
, LM8333_ACTIVE
,
180 pdata
->active_time
/ 3);
182 dev_warn(&client
->dev
, "Unable to set active time\n");
185 err
= request_threaded_irq(client
->irq
, NULL
, lm8333_irq_thread
,
186 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
191 err
= input_register_device(input
);
195 i2c_set_clientdata(client
, lm8333
);
199 free_irq(client
->irq
, lm8333
);
201 input_free_device(input
);
206 static int lm8333_remove(struct i2c_client
*client
)
208 struct lm8333
*lm8333
= i2c_get_clientdata(client
);
210 free_irq(client
->irq
, lm8333
);
211 input_unregister_device(lm8333
->input
);
217 static const struct i2c_device_id lm8333_id
[] = {
221 MODULE_DEVICE_TABLE(i2c
, lm8333_id
);
223 static struct i2c_driver lm8333_driver
= {
226 .owner
= THIS_MODULE
,
228 .probe
= lm8333_probe
,
229 .remove
= lm8333_remove
,
230 .id_table
= lm8333_id
,
232 module_i2c_driver(lm8333_driver
);
234 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
235 MODULE_DESCRIPTION("LM8333 keyboard driver");
236 MODULE_LICENSE("GPL v2");