mpr121_touchkey.c 9.5 KB

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