blob: f7f3e9a9fd3f5c4b63234fd63b750f10b327afb0 [file] [log] [blame]
Zhang Jiejing36682b82011-05-11 14:32:01 -07001/*
2 * Touchkey driver for Freescale MPR121 Controllor
3 *
4 * Copyright (C) 2011 Freescale Semiconductor, Inc.
5 * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
6 *
7 * Based on mcs_touchkey.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 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/input.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/bitops.h>
22#include <linux/interrupt.h>
23#include <linux/i2c/mpr121_touchkey.h>
24
25/* Register definitions */
26#define ELE_TOUCH_STATUS_0_ADDR 0x0
27#define ELE_TOUCH_STATUS_1_ADDR 0X1
28#define MHD_RISING_ADDR 0x2b
29#define NHD_RISING_ADDR 0x2c
30#define NCL_RISING_ADDR 0x2d
31#define FDL_RISING_ADDR 0x2e
32#define MHD_FALLING_ADDR 0x2f
33#define NHD_FALLING_ADDR 0x30
34#define NCL_FALLING_ADDR 0x31
35#define FDL_FALLING_ADDR 0x32
36#define ELE0_TOUCH_THRESHOLD_ADDR 0x41
37#define ELE0_RELEASE_THRESHOLD_ADDR 0x42
38#define AFE_CONF_ADDR 0x5c
39#define FILTER_CONF_ADDR 0x5d
40
41/*
42 * ELECTRODE_CONF_ADDR: This register configures the number of
43 * enabled capacitance sensing inputs and its run/suspend mode.
44 */
45#define ELECTRODE_CONF_ADDR 0x5e
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -070046#define ELECTRODE_CONF_QUICK_CHARGE 0x80
Zhang Jiejing36682b82011-05-11 14:32:01 -070047#define AUTO_CONFIG_CTRL_ADDR 0x7b
48#define AUTO_CONFIG_USL_ADDR 0x7d
49#define AUTO_CONFIG_LSL_ADDR 0x7e
50#define AUTO_CONFIG_TL_ADDR 0x7f
51
52/* Threshold of touch/release trigger */
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -070053#define TOUCH_THRESHOLD 0x08
54#define RELEASE_THRESHOLD 0x05
Zhang Jiejing36682b82011-05-11 14:32:01 -070055/* Masks for touch and release triggers */
56#define TOUCH_STATUS_MASK 0xfff
57/* MPR121 has 12 keys */
58#define MPR121_MAX_KEY_COUNT 12
59
60struct mpr121_touchkey {
61 struct i2c_client *client;
62 struct input_dev *input_dev;
63 unsigned int key_val;
64 unsigned int statusbits;
65 unsigned int keycount;
66 u16 keycodes[MPR121_MAX_KEY_COUNT];
67};
68
69struct mpr121_init_register {
70 int addr;
71 u8 val;
72};
73
Bill Pemberton78f50c22012-11-23 21:31:00 -080074static const struct mpr121_init_register init_reg_table[] = {
Zhang Jiejing36682b82011-05-11 14:32:01 -070075 { MHD_RISING_ADDR, 0x1 },
76 { NHD_RISING_ADDR, 0x1 },
77 { MHD_FALLING_ADDR, 0x1 },
78 { NHD_FALLING_ADDR, 0x1 },
79 { NCL_FALLING_ADDR, 0xff },
80 { FDL_FALLING_ADDR, 0x02 },
81 { FILTER_CONF_ADDR, 0x04 },
82 { AFE_CONF_ADDR, 0x0b },
83 { AUTO_CONFIG_CTRL_ADDR, 0x0b },
84};
85
86static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
87{
88 struct mpr121_touchkey *mpr121 = dev_id;
89 struct i2c_client *client = mpr121->client;
90 struct input_dev *input = mpr121->input_dev;
91 unsigned int key_num, key_val, pressed;
92 int reg;
93
94 reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
95 if (reg < 0) {
96 dev_err(&client->dev, "i2c read error [%d]\n", reg);
97 goto out;
98 }
99
100 reg <<= 8;
101 reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
102 if (reg < 0) {
103 dev_err(&client->dev, "i2c read error [%d]\n", reg);
104 goto out;
105 }
106
107 reg &= TOUCH_STATUS_MASK;
108 /* use old press bit to figure out which bit changed */
109 key_num = ffs(reg ^ mpr121->statusbits) - 1;
110 pressed = reg & (1 << key_num);
111 mpr121->statusbits = reg;
112
113 key_val = mpr121->keycodes[key_num];
114
115 input_event(input, EV_MSC, MSC_SCAN, key_num);
116 input_report_key(input, key_val, pressed);
117 input_sync(input);
118
119 dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
120 pressed ? "pressed" : "released");
121
122out:
123 return IRQ_HANDLED;
124}
125
Bill Pemberton5298cc42012-11-23 21:38:25 -0800126static int mpr121_phys_init(const struct mpr121_platform_data *pdata,
Zhang Jiejing36682b82011-05-11 14:32:01 -0700127 struct mpr121_touchkey *mpr121,
128 struct i2c_client *client)
129{
130 const struct mpr121_init_register *reg;
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700131 unsigned char usl, lsl, tl, eleconf;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700132 int i, t, vdd, ret;
133
134 /* Set up touch/release threshold for ele0-ele11 */
135 for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
136 t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
137 ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
138 if (ret < 0)
139 goto err_i2c_write;
140 ret = i2c_smbus_write_byte_data(client, t + 1,
141 RELEASE_THRESHOLD);
142 if (ret < 0)
143 goto err_i2c_write;
144 }
145
146 /* Set up init register */
147 for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
148 reg = &init_reg_table[i];
149 ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
150 if (ret < 0)
151 goto err_i2c_write;
152 }
153
154
155 /*
156 * Capacitance on sensing input varies and needs to be compensated.
157 * The internal MPR121-auto-configuration can do this if it's
158 * registers are set properly (based on pdata->vdd_uv).
159 */
160 vdd = pdata->vdd_uv / 1000;
161 usl = ((vdd - 700) * 256) / vdd;
162 lsl = (usl * 65) / 100;
163 tl = (usl * 90) / 100;
164 ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
165 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
166 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700167
168 /*
169 * Quick charge bit will let the capacitive charge to ready
170 * state quickly, or the buttons may not function after system
171 * boot.
172 */
173 eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700174 ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700175 eleconf);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700176 if (ret != 0)
177 goto err_i2c_write;
178
179 dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
180
181 return 0;
182
183err_i2c_write:
184 dev_err(&client->dev, "i2c write error: %d\n", ret);
185 return ret;
186}
187
Bill Pemberton5298cc42012-11-23 21:38:25 -0800188static int mpr_touchkey_probe(struct i2c_client *client,
189 const struct i2c_device_id *id)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700190{
191 const struct mpr121_platform_data *pdata = client->dev.platform_data;
192 struct mpr121_touchkey *mpr121;
193 struct input_dev *input_dev;
194 int error;
195 int i;
196
197 if (!pdata) {
198 dev_err(&client->dev, "no platform data defined\n");
199 return -EINVAL;
200 }
201
202 if (!pdata->keymap || !pdata->keymap_size) {
203 dev_err(&client->dev, "missing keymap data\n");
204 return -EINVAL;
205 }
206
207 if (pdata->keymap_size > MPR121_MAX_KEY_COUNT) {
208 dev_err(&client->dev, "too many keys defined\n");
209 return -EINVAL;
210 }
211
212 if (!client->irq) {
213 dev_err(&client->dev, "irq number should not be zero\n");
214 return -EINVAL;
215 }
216
217 mpr121 = kzalloc(sizeof(struct mpr121_touchkey), GFP_KERNEL);
218 input_dev = input_allocate_device();
219 if (!mpr121 || !input_dev) {
220 dev_err(&client->dev, "Failed to allocate memory\n");
221 error = -ENOMEM;
222 goto err_free_mem;
223 }
224
225 mpr121->client = client;
226 mpr121->input_dev = input_dev;
227 mpr121->keycount = pdata->keymap_size;
228
229 input_dev->name = "Freescale MPR121 Touchkey";
230 input_dev->id.bustype = BUS_I2C;
231 input_dev->dev.parent = &client->dev;
232 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
233
234 input_dev->keycode = mpr121->keycodes;
235 input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
236 input_dev->keycodemax = mpr121->keycount;
237
238 for (i = 0; i < pdata->keymap_size; i++) {
239 input_set_capability(input_dev, EV_KEY, pdata->keymap[i]);
240 mpr121->keycodes[i] = pdata->keymap[i];
241 }
242
243 error = mpr121_phys_init(pdata, mpr121, client);
244 if (error) {
245 dev_err(&client->dev, "Failed to init register\n");
246 goto err_free_mem;
247 }
248
249 error = request_threaded_irq(client->irq, NULL,
250 mpr_touchkey_interrupt,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -0700251 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Zhang Jiejing36682b82011-05-11 14:32:01 -0700252 client->dev.driver->name, mpr121);
253 if (error) {
254 dev_err(&client->dev, "Failed to register interrupt\n");
255 goto err_free_mem;
256 }
257
258 error = input_register_device(input_dev);
259 if (error)
260 goto err_free_irq;
261
262 i2c_set_clientdata(client, mpr121);
263 device_init_wakeup(&client->dev, pdata->wakeup);
264
265 return 0;
266
267err_free_irq:
268 free_irq(client->irq, mpr121);
269err_free_mem:
270 input_free_device(input_dev);
271 kfree(mpr121);
272 return error;
273}
274
Bill Pembertone2619cf2012-11-23 21:50:47 -0800275static int mpr_touchkey_remove(struct i2c_client *client)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700276{
277 struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
278
279 free_irq(client->irq, mpr121);
280 input_unregister_device(mpr121->input_dev);
281 kfree(mpr121);
282
283 return 0;
284}
285
286#ifdef CONFIG_PM_SLEEP
287static int mpr_suspend(struct device *dev)
288{
289 struct i2c_client *client = to_i2c_client(dev);
290
291 if (device_may_wakeup(&client->dev))
292 enable_irq_wake(client->irq);
293
294 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
295
296 return 0;
297}
298
299static int mpr_resume(struct device *dev)
300{
301 struct i2c_client *client = to_i2c_client(dev);
302 struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
303
304 if (device_may_wakeup(&client->dev))
305 disable_irq_wake(client->irq);
306
307 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
308 mpr121->keycount);
309
310 return 0;
311}
312#endif
313
314static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
315
316static const struct i2c_device_id mpr121_id[] = {
317 { "mpr121_touchkey", 0 },
318 { }
319};
320MODULE_DEVICE_TABLE(i2c, mpr121_id);
321
322static struct i2c_driver mpr_touchkey_driver = {
323 .driver = {
324 .name = "mpr121",
325 .owner = THIS_MODULE,
326 .pm = &mpr121_touchkey_pm_ops,
327 },
328 .id_table = mpr121_id,
329 .probe = mpr_touchkey_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800330 .remove = mpr_touchkey_remove,
Zhang Jiejing36682b82011-05-11 14:32:01 -0700331};
332
Axel Lin1b92c1c2012-03-16 23:05:41 -0700333module_i2c_driver(mpr_touchkey_driver);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700334
335MODULE_LICENSE("GPL");
336MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
337MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");