1 // SPDX-License-Identifier: GPL-2.0-only
3 * max7359_keypad.c - MAX7359 Key Switch Controller Driver
5 * Copyright (C) 2009 Samsung Electronics
6 * Kim Kyuwon <q1.kim@samsung.com>
8 * Based on pxa27x_keypad.c
10 * Datasheet: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/5456
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/input/matrix_keypad.h>
21 #define MAX7359_MAX_KEY_ROWS 8
22 #define MAX7359_MAX_KEY_COLS 8
23 #define MAX7359_MAX_KEY_NUM (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
24 #define MAX7359_ROW_SHIFT 3
29 #define MAX7359_REG_KEYFIFO 0x00
30 #define MAX7359_REG_CONFIG 0x01
31 #define MAX7359_REG_DEBOUNCE 0x02
32 #define MAX7359_REG_INTERRUPT 0x03
33 #define MAX7359_REG_PORTS 0x04
34 #define MAX7359_REG_KEYREP 0x05
35 #define MAX7359_REG_SLEEP 0x06
38 * Configuration register bits
40 #define MAX7359_CFG_SLEEP (1 << 7)
41 #define MAX7359_CFG_INTERRUPT (1 << 5)
42 #define MAX7359_CFG_KEY_RELEASE (1 << 3)
43 #define MAX7359_CFG_WAKEUP (1 << 1)
44 #define MAX7359_CFG_TIMEOUT (1 << 0)
47 * Autosleep register values (ms)
49 #define MAX7359_AUTOSLEEP_8192 0x01
50 #define MAX7359_AUTOSLEEP_4096 0x02
51 #define MAX7359_AUTOSLEEP_2048 0x03
52 #define MAX7359_AUTOSLEEP_1024 0x04
53 #define MAX7359_AUTOSLEEP_512 0x05
54 #define MAX7359_AUTOSLEEP_256 0x06
56 struct max7359_keypad
{
57 /* matrix key code map */
58 unsigned short keycodes
[MAX7359_MAX_KEY_NUM
];
60 struct input_dev
*input_dev
;
61 struct i2c_client
*client
;
64 static int max7359_write_reg(struct i2c_client
*client
, u8 reg
, u8 val
)
66 int ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
69 dev_err(&client
->dev
, "%s: reg 0x%x, val 0x%x, err %d\n",
70 __func__
, reg
, val
, ret
);
74 static int max7359_read_reg(struct i2c_client
*client
, int reg
)
76 int ret
= i2c_smbus_read_byte_data(client
, reg
);
79 dev_err(&client
->dev
, "%s: reg 0x%x, err %d\n",
84 /* runs in an IRQ thread -- can (and will!) sleep */
85 static irqreturn_t
max7359_interrupt(int irq
, void *dev_id
)
87 struct max7359_keypad
*keypad
= dev_id
;
88 struct input_dev
*input_dev
= keypad
->input_dev
;
89 int val
, row
, col
, release
, code
;
91 val
= max7359_read_reg(keypad
->client
, MAX7359_REG_KEYFIFO
);
93 col
= (val
>> 3) & 0x7;
96 code
= MATRIX_SCAN_CODE(row
, col
, MAX7359_ROW_SHIFT
);
98 dev_dbg(&keypad
->client
->dev
,
99 "key[%d:%d] %s\n", row
, col
, release
? "release" : "press");
101 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
102 input_report_key(input_dev
, keypad
->keycodes
[code
], !release
);
103 input_sync(input_dev
);
109 * Let MAX7359 fall into a deep sleep:
110 * If no keys are pressed, enter sleep mode for 8192 ms. And if any
111 * key is pressed, the MAX7359 returns to normal operating mode.
113 static inline void max7359_fall_deepsleep(struct i2c_client
*client
)
115 max7359_write_reg(client
, MAX7359_REG_SLEEP
, MAX7359_AUTOSLEEP_8192
);
119 * Let MAX7359 take a catnap:
120 * Autosleep just for 256 ms.
122 static inline void max7359_take_catnap(struct i2c_client
*client
)
124 max7359_write_reg(client
, MAX7359_REG_SLEEP
, MAX7359_AUTOSLEEP_256
);
127 static int max7359_open(struct input_dev
*dev
)
129 struct max7359_keypad
*keypad
= input_get_drvdata(dev
);
131 max7359_take_catnap(keypad
->client
);
136 static void max7359_close(struct input_dev
*dev
)
138 struct max7359_keypad
*keypad
= input_get_drvdata(dev
);
140 max7359_fall_deepsleep(keypad
->client
);
143 static void max7359_initialize(struct i2c_client
*client
)
145 max7359_write_reg(client
, MAX7359_REG_CONFIG
,
146 MAX7359_CFG_KEY_RELEASE
| /* Key release enable */
147 MAX7359_CFG_WAKEUP
); /* Key press wakeup enable */
149 /* Full key-scan functionality */
150 max7359_write_reg(client
, MAX7359_REG_DEBOUNCE
, 0x1F);
152 /* nINT asserts every debounce cycles */
153 max7359_write_reg(client
, MAX7359_REG_INTERRUPT
, 0x01);
155 max7359_fall_deepsleep(client
);
158 static int max7359_probe(struct i2c_client
*client
,
159 const struct i2c_device_id
*id
)
161 const struct matrix_keymap_data
*keymap_data
=
162 dev_get_platdata(&client
->dev
);
163 struct max7359_keypad
*keypad
;
164 struct input_dev
*input_dev
;
169 dev_err(&client
->dev
, "The irq number should not be zero\n");
173 /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
174 ret
= max7359_read_reg(client
, MAX7359_REG_KEYFIFO
);
176 dev_err(&client
->dev
, "failed to detect device\n");
180 dev_dbg(&client
->dev
, "keys FIFO is 0x%02x\n", ret
);
182 keypad
= devm_kzalloc(&client
->dev
, sizeof(struct max7359_keypad
),
185 dev_err(&client
->dev
, "failed to allocate memory\n");
189 input_dev
= devm_input_allocate_device(&client
->dev
);
191 dev_err(&client
->dev
, "failed to allocate input device\n");
195 keypad
->client
= client
;
196 keypad
->input_dev
= input_dev
;
198 input_dev
->name
= client
->name
;
199 input_dev
->id
.bustype
= BUS_I2C
;
200 input_dev
->open
= max7359_open
;
201 input_dev
->close
= max7359_close
;
202 input_dev
->dev
.parent
= &client
->dev
;
204 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
);
205 input_dev
->keycodesize
= sizeof(keypad
->keycodes
[0]);
206 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
207 input_dev
->keycode
= keypad
->keycodes
;
209 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
210 input_set_drvdata(input_dev
, keypad
);
212 error
= matrix_keypad_build_keymap(keymap_data
, NULL
,
213 MAX7359_MAX_KEY_ROWS
,
214 MAX7359_MAX_KEY_COLS
,
218 dev_err(&client
->dev
, "failed to build keymap\n");
222 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
224 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
225 client
->name
, keypad
);
227 dev_err(&client
->dev
, "failed to register interrupt\n");
231 /* Register the input device */
232 error
= input_register_device(input_dev
);
234 dev_err(&client
->dev
, "failed to register input device\n");
238 /* Initialize MAX7359 */
239 max7359_initialize(client
);
241 device_init_wakeup(&client
->dev
, 1);
246 #ifdef CONFIG_PM_SLEEP
247 static int max7359_suspend(struct device
*dev
)
249 struct i2c_client
*client
= to_i2c_client(dev
);
251 max7359_fall_deepsleep(client
);
253 if (device_may_wakeup(&client
->dev
))
254 enable_irq_wake(client
->irq
);
259 static int max7359_resume(struct device
*dev
)
261 struct i2c_client
*client
= to_i2c_client(dev
);
263 if (device_may_wakeup(&client
->dev
))
264 disable_irq_wake(client
->irq
);
266 /* Restore the default setting */
267 max7359_take_catnap(client
);
273 static SIMPLE_DEV_PM_OPS(max7359_pm
, max7359_suspend
, max7359_resume
);
275 static const struct i2c_device_id max7359_ids
[] = {
279 MODULE_DEVICE_TABLE(i2c
, max7359_ids
);
281 static struct i2c_driver max7359_i2c_driver
= {
286 .probe
= max7359_probe
,
287 .id_table
= max7359_ids
,
290 module_i2c_driver(max7359_i2c_driver
);
292 MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
293 MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
294 MODULE_LICENSE("GPL v2");