mpr121_touchkey.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/input.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/bitops.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/i2c/mpr121_touchkey.h>
  22. /* Register definitions */
  23. #define ELE_TOUCH_STATUS_0_ADDR 0x0
  24. #define ELE_TOUCH_STATUS_1_ADDR 0X1
  25. #define MHD_RISING_ADDR 0x2b
  26. #define NHD_RISING_ADDR 0x2c
  27. #define NCL_RISING_ADDR 0x2d
  28. #define FDL_RISING_ADDR 0x2e
  29. #define MHD_FALLING_ADDR 0x2f
  30. #define NHD_FALLING_ADDR 0x30
  31. #define NCL_FALLING_ADDR 0x31
  32. #define FDL_FALLING_ADDR 0x32
  33. #define ELE0_TOUCH_THRESHOLD_ADDR 0x41
  34. #define ELE0_RELEASE_THRESHOLD_ADDR 0x42
  35. #define AFE_CONF_ADDR 0x5c
  36. #define FILTER_CONF_ADDR 0x5d
  37. /*
  38. * ELECTRODE_CONF_ADDR: This register configures the number of
  39. * enabled capacitance sensing inputs and its run/suspend mode.
  40. */
  41. #define ELECTRODE_CONF_ADDR 0x5e
  42. #define ELECTRODE_CONF_QUICK_CHARGE 0x80
  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 0x08
  49. #define RELEASE_THRESHOLD 0x05
  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[] = {
  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 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, eleconf;
  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. /*
  147. * Quick charge bit will let the capacitive charge to ready
  148. * state quickly, or the buttons may not function after system
  149. * boot.
  150. */
  151. eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
  152. ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
  153. eleconf);
  154. if (ret != 0)
  155. goto err_i2c_write;
  156. dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
  157. return 0;
  158. err_i2c_write:
  159. dev_err(&client->dev, "i2c write error: %d\n", ret);
  160. return ret;
  161. }
  162. static int mpr_touchkey_probe(struct i2c_client *client,
  163. const struct i2c_device_id *id)
  164. {
  165. const struct mpr121_platform_data *pdata =
  166. dev_get_platdata(&client->dev);
  167. struct mpr121_touchkey *mpr121;
  168. struct input_dev *input_dev;
  169. int error;
  170. int i;
  171. if (!pdata) {
  172. dev_err(&client->dev, "no platform data defined\n");
  173. return -EINVAL;
  174. }
  175. if (!pdata->keymap || !pdata->keymap_size) {
  176. dev_err(&client->dev, "missing keymap data\n");
  177. return -EINVAL;
  178. }
  179. if (pdata->keymap_size > MPR121_MAX_KEY_COUNT) {
  180. dev_err(&client->dev, "too many keys defined\n");
  181. return -EINVAL;
  182. }
  183. if (!client->irq) {
  184. dev_err(&client->dev, "irq number should not be zero\n");
  185. return -EINVAL;
  186. }
  187. mpr121 = devm_kzalloc(&client->dev, sizeof(*mpr121),
  188. GFP_KERNEL);
  189. if (!mpr121)
  190. return -ENOMEM;
  191. input_dev = devm_input_allocate_device(&client->dev);
  192. if (!input_dev)
  193. return -ENOMEM;
  194. mpr121->client = client;
  195. mpr121->input_dev = input_dev;
  196. mpr121->keycount = pdata->keymap_size;
  197. input_dev->name = "Freescale MPR121 Touchkey";
  198. input_dev->id.bustype = BUS_I2C;
  199. input_dev->dev.parent = &client->dev;
  200. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  201. input_dev->keycode = mpr121->keycodes;
  202. input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
  203. input_dev->keycodemax = mpr121->keycount;
  204. for (i = 0; i < pdata->keymap_size; i++) {
  205. input_set_capability(input_dev, EV_KEY, pdata->keymap[i]);
  206. mpr121->keycodes[i] = pdata->keymap[i];
  207. }
  208. error = mpr121_phys_init(pdata, mpr121, client);
  209. if (error) {
  210. dev_err(&client->dev, "Failed to init register\n");
  211. return error;
  212. }
  213. error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  214. mpr_touchkey_interrupt,
  215. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  216. client->dev.driver->name, mpr121);
  217. if (error) {
  218. dev_err(&client->dev, "Failed to register interrupt\n");
  219. return error;
  220. }
  221. error = input_register_device(input_dev);
  222. if (error)
  223. return error;
  224. i2c_set_clientdata(client, mpr121);
  225. device_init_wakeup(&client->dev, pdata->wakeup);
  226. return 0;
  227. }
  228. #ifdef CONFIG_PM_SLEEP
  229. static int mpr_suspend(struct device *dev)
  230. {
  231. struct i2c_client *client = to_i2c_client(dev);
  232. if (device_may_wakeup(&client->dev))
  233. enable_irq_wake(client->irq);
  234. i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
  235. return 0;
  236. }
  237. static int mpr_resume(struct device *dev)
  238. {
  239. struct i2c_client *client = to_i2c_client(dev);
  240. struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
  241. if (device_may_wakeup(&client->dev))
  242. disable_irq_wake(client->irq);
  243. i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
  244. mpr121->keycount);
  245. return 0;
  246. }
  247. #endif
  248. static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
  249. static const struct i2c_device_id mpr121_id[] = {
  250. { "mpr121_touchkey", 0 },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(i2c, mpr121_id);
  254. static struct i2c_driver mpr_touchkey_driver = {
  255. .driver = {
  256. .name = "mpr121",
  257. .owner = THIS_MODULE,
  258. .pm = &mpr121_touchkey_pm_ops,
  259. },
  260. .id_table = mpr121_id,
  261. .probe = mpr_touchkey_probe,
  262. };
  263. module_i2c_driver(mpr_touchkey_driver);
  264. MODULE_LICENSE("GPL");
  265. MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
  266. MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");