mpr121_touchkey.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  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. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/bitops.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/i2c/mpr121_touchkey.h>
  23. /* Register definitions */
  24. #define ELE_TOUCH_STATUS_0_ADDR 0x0
  25. #define ELE_TOUCH_STATUS_1_ADDR 0X1
  26. #define MHD_RISING_ADDR 0x2b
  27. #define NHD_RISING_ADDR 0x2c
  28. #define NCL_RISING_ADDR 0x2d
  29. #define FDL_RISING_ADDR 0x2e
  30. #define MHD_FALLING_ADDR 0x2f
  31. #define NHD_FALLING_ADDR 0x30
  32. #define NCL_FALLING_ADDR 0x31
  33. #define FDL_FALLING_ADDR 0x32
  34. #define ELE0_TOUCH_THRESHOLD_ADDR 0x41
  35. #define ELE0_RELEASE_THRESHOLD_ADDR 0x42
  36. #define AFE_CONF_ADDR 0x5c
  37. #define FILTER_CONF_ADDR 0x5d
  38. /*
  39. * ELECTRODE_CONF_ADDR: This register configures the number of
  40. * enabled capacitance sensing inputs and its run/suspend mode.
  41. */
  42. #define ELECTRODE_CONF_ADDR 0x5e
  43. #define AUTO_CONFIG_CTRL_ADDR 0x7b
  44. #define AUTO_CONFIG_USL_ADDR 0x7d
  45. #define AUTO_CONFIG_LSL_ADDR 0x7e
  46. #define AUTO_CONFIG_TL_ADDR 0x7f
  47. /* Threshold of touch/release trigger */
  48. #define TOUCH_THRESHOLD 0x0f
  49. #define RELEASE_THRESHOLD 0x0a
  50. /* Masks for touch and release triggers */
  51. #define TOUCH_STATUS_MASK 0xfff
  52. /* MPR121 has 12 keys */
  53. #define MPR121_MAX_KEY_COUNT 12
  54. struct mpr121_touchkey {
  55. struct i2c_client *client;
  56. struct input_dev *input_dev;
  57. unsigned int key_val;
  58. unsigned int statusbits;
  59. unsigned int keycount;
  60. u16 keycodes[MPR121_MAX_KEY_COUNT];
  61. };
  62. struct mpr121_init_register {
  63. int addr;
  64. u8 val;
  65. };
  66. static const struct mpr121_init_register init_reg_table[] __devinitconst = {
  67. { MHD_RISING_ADDR, 0x1 },
  68. { NHD_RISING_ADDR, 0x1 },
  69. { MHD_FALLING_ADDR, 0x1 },
  70. { NHD_FALLING_ADDR, 0x1 },
  71. { NCL_FALLING_ADDR, 0xff },
  72. { FDL_FALLING_ADDR, 0x02 },
  73. { FILTER_CONF_ADDR, 0x04 },
  74. { AFE_CONF_ADDR, 0x0b },
  75. { AUTO_CONFIG_CTRL_ADDR, 0x0b },
  76. };
  77. static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
  78. {
  79. struct mpr121_touchkey *mpr121 = dev_id;
  80. struct i2c_client *client = mpr121->client;
  81. struct input_dev *input = mpr121->input_dev;
  82. unsigned int key_num, key_val, pressed;
  83. int reg;
  84. reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
  85. if (reg < 0) {
  86. dev_err(&client->dev, "i2c read error [%d]\n", reg);
  87. goto out;
  88. }
  89. reg <<= 8;
  90. reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
  91. if (reg < 0) {
  92. dev_err(&client->dev, "i2c read error [%d]\n", reg);
  93. goto out;
  94. }
  95. reg &= TOUCH_STATUS_MASK;
  96. /* use old press bit to figure out which bit changed */
  97. key_num = ffs(reg ^ mpr121->statusbits) - 1;
  98. pressed = reg & (1 << key_num);
  99. mpr121->statusbits = reg;
  100. key_val = mpr121->keycodes[key_num];
  101. input_event(input, EV_MSC, MSC_SCAN, key_num);
  102. input_report_key(input, key_val, pressed);
  103. input_sync(input);
  104. dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
  105. pressed ? "pressed" : "released");
  106. out:
  107. return IRQ_HANDLED;
  108. }
  109. static int __devinit mpr121_phys_init(const struct mpr121_platform_data *pdata,
  110. struct mpr121_touchkey *mpr121,
  111. struct i2c_client *client)
  112. {
  113. const struct mpr121_init_register *reg;
  114. unsigned char usl, lsl, tl;
  115. int i, t, vdd, ret;
  116. /* Set up touch/release threshold for ele0-ele11 */
  117. for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
  118. t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
  119. ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
  120. if (ret < 0)
  121. goto err_i2c_write;
  122. ret = i2c_smbus_write_byte_data(client, t + 1,
  123. RELEASE_THRESHOLD);
  124. if (ret < 0)
  125. goto err_i2c_write;
  126. }
  127. /* Set up init register */
  128. for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
  129. reg = &init_reg_table[i];
  130. ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
  131. if (ret < 0)
  132. goto err_i2c_write;
  133. }
  134. /*
  135. * Capacitance on sensing input varies and needs to be compensated.
  136. * The internal MPR121-auto-configuration can do this if it's
  137. * registers are set properly (based on pdata->vdd_uv).
  138. */
  139. vdd = pdata->vdd_uv / 1000;
  140. usl = ((vdd - 700) * 256) / vdd;
  141. lsl = (usl * 65) / 100;
  142. tl = (usl * 90) / 100;
  143. ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
  144. ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
  145. ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
  146. ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
  147. mpr121->keycount);
  148. if (ret != 0)
  149. goto err_i2c_write;
  150. dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
  151. return 0;
  152. err_i2c_write:
  153. dev_err(&client->dev, "i2c write error: %d\n", ret);
  154. return ret;
  155. }
  156. static int __devinit mpr_touchkey_probe(struct i2c_client *client,
  157. const struct i2c_device_id *id)
  158. {
  159. const struct mpr121_platform_data *pdata = client->dev.platform_data;
  160. struct mpr121_touchkey *mpr121;
  161. struct input_dev *input_dev;
  162. int error;
  163. int i;
  164. if (!pdata) {
  165. dev_err(&client->dev, "no platform data defined\n");
  166. return -EINVAL;
  167. }
  168. if (!pdata->keymap || !pdata->keymap_size) {
  169. dev_err(&client->dev, "missing keymap data\n");
  170. return -EINVAL;
  171. }
  172. if (pdata->keymap_size > MPR121_MAX_KEY_COUNT) {
  173. dev_err(&client->dev, "too many keys defined\n");
  174. return -EINVAL;
  175. }
  176. if (!client->irq) {
  177. dev_err(&client->dev, "irq number should not be zero\n");
  178. return -EINVAL;
  179. }
  180. mpr121 = kzalloc(sizeof(struct mpr121_touchkey), GFP_KERNEL);
  181. input_dev = input_allocate_device();
  182. if (!mpr121 || !input_dev) {
  183. dev_err(&client->dev, "Failed to allocate memory\n");
  184. error = -ENOMEM;
  185. goto err_free_mem;
  186. }
  187. mpr121->client = client;
  188. mpr121->input_dev = input_dev;
  189. mpr121->keycount = pdata->keymap_size;
  190. input_dev->name = "Freescale MPR121 Touchkey";
  191. input_dev->id.bustype = BUS_I2C;
  192. input_dev->dev.parent = &client->dev;
  193. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  194. input_dev->keycode = mpr121->keycodes;
  195. input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
  196. input_dev->keycodemax = mpr121->keycount;
  197. for (i = 0; i < pdata->keymap_size; i++) {
  198. input_set_capability(input_dev, EV_KEY, pdata->keymap[i]);
  199. mpr121->keycodes[i] = pdata->keymap[i];
  200. }
  201. error = mpr121_phys_init(pdata, mpr121, client);
  202. if (error) {
  203. dev_err(&client->dev, "Failed to init register\n");
  204. goto err_free_mem;
  205. }
  206. error = request_threaded_irq(client->irq, NULL,
  207. mpr_touchkey_interrupt,
  208. IRQF_TRIGGER_FALLING,
  209. client->dev.driver->name, mpr121);
  210. if (error) {
  211. dev_err(&client->dev, "Failed to register interrupt\n");
  212. goto err_free_mem;
  213. }
  214. error = input_register_device(input_dev);
  215. if (error)
  216. goto err_free_irq;
  217. i2c_set_clientdata(client, mpr121);
  218. device_init_wakeup(&client->dev, pdata->wakeup);
  219. return 0;
  220. err_free_irq:
  221. free_irq(client->irq, mpr121);
  222. err_free_mem:
  223. input_free_device(input_dev);
  224. kfree(mpr121);
  225. return error;
  226. }
  227. static int __devexit mpr_touchkey_remove(struct i2c_client *client)
  228. {
  229. struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
  230. free_irq(client->irq, mpr121);
  231. input_unregister_device(mpr121->input_dev);
  232. kfree(mpr121);
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM_SLEEP
  236. static int mpr_suspend(struct device *dev)
  237. {
  238. struct i2c_client *client = to_i2c_client(dev);
  239. if (device_may_wakeup(&client->dev))
  240. enable_irq_wake(client->irq);
  241. i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
  242. return 0;
  243. }
  244. static int mpr_resume(struct device *dev)
  245. {
  246. struct i2c_client *client = to_i2c_client(dev);
  247. struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
  248. if (device_may_wakeup(&client->dev))
  249. disable_irq_wake(client->irq);
  250. i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
  251. mpr121->keycount);
  252. return 0;
  253. }
  254. #endif
  255. static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
  256. static const struct i2c_device_id mpr121_id[] = {
  257. { "mpr121_touchkey", 0 },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(i2c, mpr121_id);
  261. static struct i2c_driver mpr_touchkey_driver = {
  262. .driver = {
  263. .name = "mpr121",
  264. .owner = THIS_MODULE,
  265. .pm = &mpr121_touchkey_pm_ops,
  266. },
  267. .id_table = mpr121_id,
  268. .probe = mpr_touchkey_probe,
  269. .remove = __devexit_p(mpr_touchkey_remove),
  270. };
  271. static int __init mpr_touchkey_init(void)
  272. {
  273. return i2c_add_driver(&mpr_touchkey_driver);
  274. }
  275. module_init(mpr_touchkey_init);
  276. static void __exit mpr_touchkey_exit(void)
  277. {
  278. i2c_del_driver(&mpr_touchkey_driver);
  279. }
  280. module_exit(mpr_touchkey_exit);
  281. MODULE_LICENSE("GPL");
  282. MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
  283. MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");