mcs_touchkey.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Touchkey driver for MELFAS MCS5000/5080 controller
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co.Ltd
  5. * Author: HeungJun Kim <riverful.kim@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/i2c/mcs.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/input.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. #include <linux/pm.h>
  21. /* MCS5000 Touchkey */
  22. #define MCS5000_TOUCHKEY_STATUS 0x04
  23. #define MCS5000_TOUCHKEY_STATUS_PRESS 7
  24. #define MCS5000_TOUCHKEY_FW 0x0a
  25. #define MCS5000_TOUCHKEY_BASE_VAL 0x61
  26. /* MCS5080 Touchkey */
  27. #define MCS5080_TOUCHKEY_STATUS 0x00
  28. #define MCS5080_TOUCHKEY_STATUS_PRESS 3
  29. #define MCS5080_TOUCHKEY_FW 0x01
  30. #define MCS5080_TOUCHKEY_BASE_VAL 0x1
  31. enum mcs_touchkey_type {
  32. MCS5000_TOUCHKEY,
  33. MCS5080_TOUCHKEY,
  34. };
  35. struct mcs_touchkey_chip {
  36. unsigned int status_reg;
  37. unsigned int pressbit;
  38. unsigned int press_invert;
  39. unsigned int baseval;
  40. };
  41. struct mcs_touchkey_data {
  42. void (*poweron)(bool);
  43. struct i2c_client *client;
  44. struct input_dev *input_dev;
  45. struct mcs_touchkey_chip chip;
  46. unsigned int key_code;
  47. unsigned int key_val;
  48. unsigned short keycodes[];
  49. };
  50. static irqreturn_t mcs_touchkey_interrupt(int irq, void *dev_id)
  51. {
  52. struct mcs_touchkey_data *data = dev_id;
  53. struct mcs_touchkey_chip *chip = &data->chip;
  54. struct i2c_client *client = data->client;
  55. struct input_dev *input = data->input_dev;
  56. unsigned int key_val;
  57. unsigned int pressed;
  58. int val;
  59. val = i2c_smbus_read_byte_data(client, chip->status_reg);
  60. if (val < 0) {
  61. dev_err(&client->dev, "i2c read error [%d]\n", val);
  62. goto out;
  63. }
  64. pressed = (val & (1 << chip->pressbit)) >> chip->pressbit;
  65. if (chip->press_invert)
  66. pressed ^= chip->press_invert;
  67. /* key_val is 0 when released, so we should use key_val of press. */
  68. if (pressed) {
  69. key_val = val & (0xff >> (8 - chip->pressbit));
  70. if (!key_val)
  71. goto out;
  72. key_val -= chip->baseval;
  73. data->key_code = data->keycodes[key_val];
  74. data->key_val = key_val;
  75. }
  76. input_event(input, EV_MSC, MSC_SCAN, data->key_val);
  77. input_report_key(input, data->key_code, pressed);
  78. input_sync(input);
  79. dev_dbg(&client->dev, "key %d %d %s\n", data->key_val, data->key_code,
  80. pressed ? "pressed" : "released");
  81. out:
  82. return IRQ_HANDLED;
  83. }
  84. static int mcs_touchkey_probe(struct i2c_client *client,
  85. const struct i2c_device_id *id)
  86. {
  87. const struct mcs_platform_data *pdata;
  88. struct mcs_touchkey_data *data;
  89. struct input_dev *input_dev;
  90. unsigned int fw_reg;
  91. int fw_ver;
  92. int error;
  93. int i;
  94. pdata = dev_get_platdata(&client->dev);
  95. if (!pdata) {
  96. dev_err(&client->dev, "no platform data defined\n");
  97. return -EINVAL;
  98. }
  99. data = kzalloc(sizeof(struct mcs_touchkey_data) +
  100. sizeof(data->keycodes[0]) * (pdata->key_maxval + 1),
  101. GFP_KERNEL);
  102. input_dev = input_allocate_device();
  103. if (!data || !input_dev) {
  104. dev_err(&client->dev, "Failed to allocate memory\n");
  105. error = -ENOMEM;
  106. goto err_free_mem;
  107. }
  108. data->client = client;
  109. data->input_dev = input_dev;
  110. if (id->driver_data == MCS5000_TOUCHKEY) {
  111. data->chip.status_reg = MCS5000_TOUCHKEY_STATUS;
  112. data->chip.pressbit = MCS5000_TOUCHKEY_STATUS_PRESS;
  113. data->chip.baseval = MCS5000_TOUCHKEY_BASE_VAL;
  114. fw_reg = MCS5000_TOUCHKEY_FW;
  115. } else {
  116. data->chip.status_reg = MCS5080_TOUCHKEY_STATUS;
  117. data->chip.pressbit = MCS5080_TOUCHKEY_STATUS_PRESS;
  118. data->chip.press_invert = 1;
  119. data->chip.baseval = MCS5080_TOUCHKEY_BASE_VAL;
  120. fw_reg = MCS5080_TOUCHKEY_FW;
  121. }
  122. fw_ver = i2c_smbus_read_byte_data(client, fw_reg);
  123. if (fw_ver < 0) {
  124. error = fw_ver;
  125. dev_err(&client->dev, "i2c read error[%d]\n", error);
  126. goto err_free_mem;
  127. }
  128. dev_info(&client->dev, "Firmware version: %d\n", fw_ver);
  129. input_dev->name = "MELFAS MCS Touchkey";
  130. input_dev->id.bustype = BUS_I2C;
  131. input_dev->dev.parent = &client->dev;
  132. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  133. if (!pdata->no_autorepeat)
  134. input_dev->evbit[0] |= BIT_MASK(EV_REP);
  135. input_dev->keycode = data->keycodes;
  136. input_dev->keycodesize = sizeof(data->keycodes[0]);
  137. input_dev->keycodemax = pdata->key_maxval + 1;
  138. for (i = 0; i < pdata->keymap_size; i++) {
  139. unsigned int val = MCS_KEY_VAL(pdata->keymap[i]);
  140. unsigned int code = MCS_KEY_CODE(pdata->keymap[i]);
  141. data->keycodes[val] = code;
  142. __set_bit(code, input_dev->keybit);
  143. }
  144. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  145. input_set_drvdata(input_dev, data);
  146. if (pdata->cfg_pin)
  147. pdata->cfg_pin();
  148. if (pdata->poweron) {
  149. data->poweron = pdata->poweron;
  150. data->poweron(true);
  151. }
  152. error = request_threaded_irq(client->irq, NULL, mcs_touchkey_interrupt,
  153. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  154. client->dev.driver->name, data);
  155. if (error) {
  156. dev_err(&client->dev, "Failed to register interrupt\n");
  157. goto err_free_mem;
  158. }
  159. error = input_register_device(input_dev);
  160. if (error)
  161. goto err_free_irq;
  162. i2c_set_clientdata(client, data);
  163. return 0;
  164. err_free_irq:
  165. free_irq(client->irq, data);
  166. err_free_mem:
  167. input_free_device(input_dev);
  168. kfree(data);
  169. return error;
  170. }
  171. static int mcs_touchkey_remove(struct i2c_client *client)
  172. {
  173. struct mcs_touchkey_data *data = i2c_get_clientdata(client);
  174. free_irq(client->irq, data);
  175. if (data->poweron)
  176. data->poweron(false);
  177. input_unregister_device(data->input_dev);
  178. kfree(data);
  179. return 0;
  180. }
  181. static void mcs_touchkey_shutdown(struct i2c_client *client)
  182. {
  183. struct mcs_touchkey_data *data = i2c_get_clientdata(client);
  184. if (data->poweron)
  185. data->poweron(false);
  186. }
  187. #ifdef CONFIG_PM_SLEEP
  188. static int mcs_touchkey_suspend(struct device *dev)
  189. {
  190. struct mcs_touchkey_data *data = dev_get_drvdata(dev);
  191. struct i2c_client *client = data->client;
  192. /* Disable the work */
  193. disable_irq(client->irq);
  194. /* Finally turn off the power */
  195. if (data->poweron)
  196. data->poweron(false);
  197. return 0;
  198. }
  199. static int mcs_touchkey_resume(struct device *dev)
  200. {
  201. struct mcs_touchkey_data *data = dev_get_drvdata(dev);
  202. struct i2c_client *client = data->client;
  203. /* Enable the device first */
  204. if (data->poweron)
  205. data->poweron(true);
  206. /* Enable irq again */
  207. enable_irq(client->irq);
  208. return 0;
  209. }
  210. #endif
  211. static SIMPLE_DEV_PM_OPS(mcs_touchkey_pm_ops,
  212. mcs_touchkey_suspend, mcs_touchkey_resume);
  213. static const struct i2c_device_id mcs_touchkey_id[] = {
  214. { "mcs5000_touchkey", MCS5000_TOUCHKEY },
  215. { "mcs5080_touchkey", MCS5080_TOUCHKEY },
  216. { }
  217. };
  218. MODULE_DEVICE_TABLE(i2c, mcs_touchkey_id);
  219. static struct i2c_driver mcs_touchkey_driver = {
  220. .driver = {
  221. .name = "mcs_touchkey",
  222. .pm = &mcs_touchkey_pm_ops,
  223. },
  224. .probe = mcs_touchkey_probe,
  225. .remove = mcs_touchkey_remove,
  226. .shutdown = mcs_touchkey_shutdown,
  227. .id_table = mcs_touchkey_id,
  228. };
  229. module_i2c_driver(mcs_touchkey_driver);
  230. /* Module information */
  231. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  232. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  233. MODULE_DESCRIPTION("Touchkey driver for MELFAS MCS5000/5080 controller");
  234. MODULE_LICENSE("GPL");