drm/panel: simple: add Multi-Inno Technology MI0700A2T-30
[drm/drm-misc.git] / drivers / input / keyboard / max7359_keypad.c
blobc10726b5e4d104778c5daa60a0871597f935d743
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
17 #include <linux/pm.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
27 * MAX7359 registers
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);
68 if (ret < 0)
69 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
70 __func__, reg, val, ret);
71 return ret;
74 static int max7359_read_reg(struct i2c_client *client, int reg)
76 int ret = i2c_smbus_read_byte_data(client, reg);
78 if (ret < 0)
79 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
80 __func__, reg, ret);
81 return ret;
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);
92 row = val & 0x7;
93 col = (val >> 3) & 0x7;
94 release = val & 0x40;
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);
105 return IRQ_HANDLED;
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);
133 return 0;
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)
160 const struct matrix_keymap_data *keymap_data =
161 dev_get_platdata(&client->dev);
162 struct max7359_keypad *keypad;
163 struct input_dev *input_dev;
164 int ret;
165 int error;
167 if (!client->irq) {
168 dev_err(&client->dev, "The irq number should not be zero\n");
169 return -EINVAL;
172 /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
173 ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
174 if (ret < 0) {
175 dev_err(&client->dev, "failed to detect device\n");
176 return -ENODEV;
179 dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);
181 keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad),
182 GFP_KERNEL);
183 if (!keypad) {
184 dev_err(&client->dev, "failed to allocate memory\n");
185 return -ENOMEM;
188 input_dev = devm_input_allocate_device(&client->dev);
189 if (!input_dev) {
190 dev_err(&client->dev, "failed to allocate input device\n");
191 return -ENOMEM;
194 keypad->client = client;
195 keypad->input_dev = input_dev;
197 input_dev->name = client->name;
198 input_dev->id.bustype = BUS_I2C;
199 input_dev->open = max7359_open;
200 input_dev->close = max7359_close;
201 input_dev->dev.parent = &client->dev;
203 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
204 input_dev->keycodesize = sizeof(keypad->keycodes[0]);
205 input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
206 input_dev->keycode = keypad->keycodes;
208 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
209 input_set_drvdata(input_dev, keypad);
211 error = matrix_keypad_build_keymap(keymap_data, NULL,
212 MAX7359_MAX_KEY_ROWS,
213 MAX7359_MAX_KEY_COLS,
214 keypad->keycodes,
215 input_dev);
216 if (error) {
217 dev_err(&client->dev, "failed to build keymap\n");
218 return error;
221 error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
222 max7359_interrupt,
223 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
224 client->name, keypad);
225 if (error) {
226 dev_err(&client->dev, "failed to register interrupt\n");
227 return error;
230 /* Register the input device */
231 error = input_register_device(input_dev);
232 if (error) {
233 dev_err(&client->dev, "failed to register input device\n");
234 return error;
237 /* Initialize MAX7359 */
238 max7359_initialize(client);
240 device_init_wakeup(&client->dev, 1);
242 return 0;
245 static int max7359_suspend(struct device *dev)
247 struct i2c_client *client = to_i2c_client(dev);
249 max7359_fall_deepsleep(client);
251 if (device_may_wakeup(&client->dev))
252 enable_irq_wake(client->irq);
254 return 0;
257 static int max7359_resume(struct device *dev)
259 struct i2c_client *client = to_i2c_client(dev);
261 if (device_may_wakeup(&client->dev))
262 disable_irq_wake(client->irq);
264 /* Restore the default setting */
265 max7359_take_catnap(client);
267 return 0;
270 static DEFINE_SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);
272 static const struct i2c_device_id max7359_ids[] = {
273 { "max7359" },
276 MODULE_DEVICE_TABLE(i2c, max7359_ids);
278 static struct i2c_driver max7359_i2c_driver = {
279 .driver = {
280 .name = "max7359",
281 .pm = pm_sleep_ptr(&max7359_pm),
283 .probe = max7359_probe,
284 .id_table = max7359_ids,
287 module_i2c_driver(max7359_i2c_driver);
289 MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
290 MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
291 MODULE_LICENSE("GPL v2");