blob: faa6da53eba8fe4d16182725fa67e1d40cdf2c6a [file] [log] [blame]
Kim Kyuwon0baf81b2009-09-21 22:17:04 -07001/*
2 * max7359_keypad.c - MAX7359 Key Switch Controller Driver
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 * Kim Kyuwon <q1.kim@samsung.com>
6 *
7 * Based on pxa27x_keypad.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Datasheet: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/5456
14 */
15
16#include <linux/module.h>
17#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Kim Kyuwon0baf81b2009-09-21 22:17:04 -070019#include <linux/interrupt.h>
Mark Brown76e2c682011-02-11 08:51:13 -080020#include <linux/pm.h>
Kim Kyuwon0baf81b2009-09-21 22:17:04 -070021#include <linux/input.h>
22#include <linux/input/matrix_keypad.h>
23
24#define MAX7359_MAX_KEY_ROWS 8
25#define MAX7359_MAX_KEY_COLS 8
26#define MAX7359_MAX_KEY_NUM (MAX7359_MAX_KEY_ROWS * MAX7359_MAX_KEY_COLS)
27#define MAX7359_ROW_SHIFT 3
28
29/*
30 * MAX7359 registers
31 */
32#define MAX7359_REG_KEYFIFO 0x00
33#define MAX7359_REG_CONFIG 0x01
34#define MAX7359_REG_DEBOUNCE 0x02
35#define MAX7359_REG_INTERRUPT 0x03
36#define MAX7359_REG_PORTS 0x04
37#define MAX7359_REG_KEYREP 0x05
38#define MAX7359_REG_SLEEP 0x06
39
40/*
41 * Configuration register bits
42 */
43#define MAX7359_CFG_SLEEP (1 << 7)
44#define MAX7359_CFG_INTERRUPT (1 << 5)
45#define MAX7359_CFG_KEY_RELEASE (1 << 3)
46#define MAX7359_CFG_WAKEUP (1 << 1)
47#define MAX7359_CFG_TIMEOUT (1 << 0)
48
49/*
50 * Autosleep register values (ms)
51 */
52#define MAX7359_AUTOSLEEP_8192 0x01
53#define MAX7359_AUTOSLEEP_4096 0x02
54#define MAX7359_AUTOSLEEP_2048 0x03
55#define MAX7359_AUTOSLEEP_1024 0x04
56#define MAX7359_AUTOSLEEP_512 0x05
57#define MAX7359_AUTOSLEEP_256 0x06
58
59struct max7359_keypad {
60 /* matrix key code map */
61 unsigned short keycodes[MAX7359_MAX_KEY_NUM];
62
Kim Kyuwon0baf81b2009-09-21 22:17:04 -070063 struct input_dev *input_dev;
64 struct i2c_client *client;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -070065};
66
67static int max7359_write_reg(struct i2c_client *client, u8 reg, u8 val)
68{
69 int ret = i2c_smbus_write_byte_data(client, reg, val);
70
71 if (ret < 0)
72 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
73 __func__, reg, val, ret);
74 return ret;
75}
76
77static int max7359_read_reg(struct i2c_client *client, int reg)
78{
79 int ret = i2c_smbus_read_byte_data(client, reg);
80
81 if (ret < 0)
82 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
83 __func__, reg, ret);
84 return ret;
85}
86
87static void max7359_build_keycode(struct max7359_keypad *keypad,
88 const struct matrix_keymap_data *keymap_data)
89{
90 struct input_dev *input_dev = keypad->input_dev;
91 int i;
92
93 for (i = 0; i < keymap_data->keymap_size; i++) {
94 unsigned int key = keymap_data->keymap[i];
95 unsigned int row = KEY_ROW(key);
96 unsigned int col = KEY_COL(key);
97 unsigned int scancode = MATRIX_SCAN_CODE(row, col,
98 MAX7359_ROW_SHIFT);
99 unsigned short keycode = KEY_VAL(key);
100
101 keypad->keycodes[scancode] = keycode;
102 __set_bit(keycode, input_dev->keybit);
103 }
104 __clear_bit(KEY_RESERVED, input_dev->keybit);
105}
106
Dmitry Torokhov44ca3972009-09-21 22:17:57 -0700107/* runs in an IRQ thread -- can (and will!) sleep */
108static irqreturn_t max7359_interrupt(int irq, void *dev_id)
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700109{
Dmitry Torokhov44ca3972009-09-21 22:17:57 -0700110 struct max7359_keypad *keypad = dev_id;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700111 struct input_dev *input_dev = keypad->input_dev;
112 int val, row, col, release, code;
113
114 val = max7359_read_reg(keypad->client, MAX7359_REG_KEYFIFO);
115 row = val & 0x7;
116 col = (val >> 3) & 0x7;
117 release = val & 0x40;
118
119 code = MATRIX_SCAN_CODE(row, col, MAX7359_ROW_SHIFT);
120
Dmitry Torokhov44ca3972009-09-21 22:17:57 -0700121 dev_dbg(&keypad->client->dev,
122 "key[%d:%d] %s\n", row, col, release ? "release" : "press");
123
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700124 input_event(input_dev, EV_MSC, MSC_SCAN, code);
125 input_report_key(input_dev, keypad->keycodes[code], !release);
126 input_sync(input_dev);
127
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700128 return IRQ_HANDLED;
129}
130
131/*
132 * Let MAX7359 fall into a deep sleep:
133 * If no keys are pressed, enter sleep mode for 8192 ms. And if any
134 * key is pressed, the MAX7359 returns to normal operating mode.
135 */
136static inline void max7359_fall_deepsleep(struct i2c_client *client)
137{
138 max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_8192);
139}
140
141/*
142 * Let MAX7359 take a catnap:
143 * Autosleep just for 256 ms.
144 */
145static inline void max7359_take_catnap(struct i2c_client *client)
146{
147 max7359_write_reg(client, MAX7359_REG_SLEEP, MAX7359_AUTOSLEEP_256);
148}
149
150static int max7359_open(struct input_dev *dev)
151{
152 struct max7359_keypad *keypad = input_get_drvdata(dev);
153
154 max7359_take_catnap(keypad->client);
155
156 return 0;
157}
158
159static void max7359_close(struct input_dev *dev)
160{
161 struct max7359_keypad *keypad = input_get_drvdata(dev);
162
163 max7359_fall_deepsleep(keypad->client);
164}
165
166static void max7359_initialize(struct i2c_client *client)
167{
168 max7359_write_reg(client, MAX7359_REG_CONFIG,
169 MAX7359_CFG_INTERRUPT | /* Irq clears after host read */
170 MAX7359_CFG_KEY_RELEASE | /* Key release enable */
171 MAX7359_CFG_WAKEUP); /* Key press wakeup enable */
172
173 /* Full key-scan functionality */
174 max7359_write_reg(client, MAX7359_REG_DEBOUNCE, 0x1F);
175
176 /* nINT asserts every debounce cycles */
177 max7359_write_reg(client, MAX7359_REG_INTERRUPT, 0x01);
178
179 max7359_fall_deepsleep(client);
180}
181
Bill Pemberton5298cc42012-11-23 21:38:25 -0800182static int max7359_probe(struct i2c_client *client,
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700183 const struct i2c_device_id *id)
184{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800185 const struct matrix_keymap_data *keymap_data =
186 dev_get_platdata(&client->dev);
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700187 struct max7359_keypad *keypad;
188 struct input_dev *input_dev;
189 int ret;
190 int error;
191
192 if (!client->irq) {
193 dev_err(&client->dev, "The irq number should not be zero\n");
194 return -EINVAL;
195 }
196
197 /* Detect MAX7359: The initial Keys FIFO value is '0x3F' */
198 ret = max7359_read_reg(client, MAX7359_REG_KEYFIFO);
199 if (ret < 0) {
200 dev_err(&client->dev, "failed to detect device\n");
201 return -ENODEV;
202 }
203
204 dev_dbg(&client->dev, "keys FIFO is 0x%02x\n", ret);
205
Himangi Saraogi997502f2014-07-19 16:16:02 -0700206 keypad = devm_kzalloc(&client->dev, sizeof(struct max7359_keypad),
207 GFP_KERNEL);
208 if (!keypad) {
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700209 dev_err(&client->dev, "failed to allocate memory\n");
Himangi Saraogi997502f2014-07-19 16:16:02 -0700210 return -ENOMEM;
211 }
212
213 input_dev = devm_input_allocate_device(&client->dev);
214 if (!input_dev) {
215 dev_err(&client->dev, "failed to allocate input device\n");
216 return -ENOMEM;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700217 }
218
219 keypad->client = client;
220 keypad->input_dev = input_dev;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700221
222 input_dev->name = client->name;
223 input_dev->id.bustype = BUS_I2C;
224 input_dev->open = max7359_open;
225 input_dev->close = max7359_close;
226 input_dev->dev.parent = &client->dev;
227
228 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
229 input_dev->keycodesize = sizeof(keypad->keycodes[0]);
230 input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
231 input_dev->keycode = keypad->keycodes;
232
233 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
234 input_set_drvdata(input_dev, keypad);
235
236 max7359_build_keycode(keypad, keymap_data);
237
Himangi Saraogi997502f2014-07-19 16:16:02 -0700238 error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
239 max7359_interrupt,
240 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
241 client->name, keypad);
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700242 if (error) {
243 dev_err(&client->dev, "failed to register interrupt\n");
Himangi Saraogi997502f2014-07-19 16:16:02 -0700244 return error;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700245 }
246
247 /* Register the input device */
248 error = input_register_device(input_dev);
249 if (error) {
250 dev_err(&client->dev, "failed to register input device\n");
Himangi Saraogi997502f2014-07-19 16:16:02 -0700251 return error;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700252 }
253
254 /* Initialize MAX7359 */
255 max7359_initialize(client);
256
257 i2c_set_clientdata(client, keypad);
258 device_init_wakeup(&client->dev, 1);
259
260 return 0;
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700261}
262
Jingoo Han1bc75452013-08-05 09:04:18 -0700263#ifdef CONFIG_PM_SLEEP
Mark Brown76e2c682011-02-11 08:51:13 -0800264static int max7359_suspend(struct device *dev)
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700265{
Mark Brown76e2c682011-02-11 08:51:13 -0800266 struct i2c_client *client = to_i2c_client(dev);
267
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700268 max7359_fall_deepsleep(client);
269
270 if (device_may_wakeup(&client->dev))
Dmitry Torokhov44ca3972009-09-21 22:17:57 -0700271 enable_irq_wake(client->irq);
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700272
273 return 0;
274}
275
Mark Brown76e2c682011-02-11 08:51:13 -0800276static int max7359_resume(struct device *dev)
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700277{
Mark Brown76e2c682011-02-11 08:51:13 -0800278 struct i2c_client *client = to_i2c_client(dev);
279
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700280 if (device_may_wakeup(&client->dev))
Dmitry Torokhov44ca3972009-09-21 22:17:57 -0700281 disable_irq_wake(client->irq);
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700282
283 /* Restore the default setting */
284 max7359_take_catnap(client);
285
286 return 0;
287}
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700288#endif
289
Mark Brown76e2c682011-02-11 08:51:13 -0800290static SIMPLE_DEV_PM_OPS(max7359_pm, max7359_suspend, max7359_resume);
291
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700292static const struct i2c_device_id max7359_ids[] = {
293 { "max7359", 0 },
294 { }
295};
296MODULE_DEVICE_TABLE(i2c, max7359_ids);
297
298static struct i2c_driver max7359_i2c_driver = {
299 .driver = {
300 .name = "max7359",
Mark Brown76e2c682011-02-11 08:51:13 -0800301 .pm = &max7359_pm,
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700302 },
303 .probe = max7359_probe,
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700304 .id_table = max7359_ids,
305};
306
Axel Lin1b92c1c2012-03-16 23:05:41 -0700307module_i2c_driver(max7359_i2c_driver);
Kim Kyuwon0baf81b2009-09-21 22:17:04 -0700308
309MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
310MODULE_DESCRIPTION("MAX7359 Key Switch Controller Driver");
311MODULE_LICENSE("GPL v2");