mcs_touchkey.c 6.8 KB

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