mcs_touchkey.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * mcs_touchkey.c - 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/init.h>
  15. #include <linux/i2c.h>
  16. #include <linux/i2c/mcs.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/input.h>
  19. #include <linux/irq.h>
  20. #include <linux/slab.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. struct i2c_client *client;
  43. struct input_dev *input_dev;
  44. struct mcs_touchkey_chip chip;
  45. unsigned int key_code;
  46. unsigned int key_val;
  47. unsigned short keycodes[];
  48. };
  49. static irqreturn_t mcs_touchkey_interrupt(int irq, void *dev_id)
  50. {
  51. struct mcs_touchkey_data *data = dev_id;
  52. struct mcs_touchkey_chip *chip = &data->chip;
  53. struct i2c_client *client = data->client;
  54. struct input_dev *input = data->input_dev;
  55. unsigned int key_val;
  56. unsigned int pressed;
  57. int val;
  58. val = i2c_smbus_read_byte_data(client, chip->status_reg);
  59. if (val < 0) {
  60. dev_err(&client->dev, "i2c read error [%d]\n", val);
  61. goto out;
  62. }
  63. pressed = (val & (1 << chip->pressbit)) >> chip->pressbit;
  64. if (chip->press_invert)
  65. pressed ^= chip->press_invert;
  66. /* key_val is 0 when released, so we should use key_val of press. */
  67. if (pressed) {
  68. key_val = val & (0xff >> (8 - chip->pressbit));
  69. if (!key_val)
  70. goto out;
  71. key_val -= chip->baseval;
  72. data->key_code = data->keycodes[key_val];
  73. data->key_val = key_val;
  74. }
  75. input_event(input, EV_MSC, MSC_SCAN, data->key_val);
  76. input_report_key(input, data->key_code, pressed);
  77. input_sync(input);
  78. dev_dbg(&client->dev, "key %d %d %s\n", data->key_val, data->key_code,
  79. pressed ? "pressed" : "released");
  80. out:
  81. return IRQ_HANDLED;
  82. }
  83. static int __devinit mcs_touchkey_probe(struct i2c_client *client,
  84. const struct i2c_device_id *id)
  85. {
  86. const struct mcs_platform_data *pdata;
  87. struct mcs_touchkey_data *data;
  88. struct input_dev *input_dev;
  89. unsigned int fw_reg;
  90. int fw_ver;
  91. int error;
  92. int i;
  93. pdata = client->dev.platform_data;
  94. if (!pdata) {
  95. dev_err(&client->dev, "no platform data defined\n");
  96. return -EINVAL;
  97. }
  98. data = kzalloc(sizeof(struct mcs_touchkey_data) +
  99. sizeof(data->keycodes[0]) * (pdata->key_maxval + 1),
  100. GFP_KERNEL);
  101. input_dev = input_allocate_device();
  102. if (!data || !input_dev) {
  103. dev_err(&client->dev, "Failed to allocate memory\n");
  104. error = -ENOMEM;
  105. goto err_free_mem;
  106. }
  107. data->client = client;
  108. data->input_dev = input_dev;
  109. if (id->driver_data == MCS5000_TOUCHKEY) {
  110. data->chip.status_reg = MCS5000_TOUCHKEY_STATUS;
  111. data->chip.pressbit = MCS5000_TOUCHKEY_STATUS_PRESS;
  112. data->chip.baseval = MCS5000_TOUCHKEY_BASE_VAL;
  113. fw_reg = MCS5000_TOUCHKEY_FW;
  114. } else {
  115. data->chip.status_reg = MCS5080_TOUCHKEY_STATUS;
  116. data->chip.pressbit = MCS5080_TOUCHKEY_STATUS_PRESS;
  117. data->chip.press_invert = 1;
  118. data->chip.baseval = MCS5080_TOUCHKEY_BASE_VAL;
  119. fw_reg = MCS5080_TOUCHKEY_FW;
  120. }
  121. fw_ver = i2c_smbus_read_byte_data(client, fw_reg);
  122. if (fw_ver < 0) {
  123. error = fw_ver;
  124. dev_err(&client->dev, "i2c read error[%d]\n", error);
  125. goto err_free_mem;
  126. }
  127. dev_info(&client->dev, "Firmware version: %d\n", fw_ver);
  128. input_dev->name = "MELPAS MCS Touchkey";
  129. input_dev->id.bustype = BUS_I2C;
  130. input_dev->dev.parent = &client->dev;
  131. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  132. if (!pdata->no_autorepeat)
  133. input_dev->evbit[0] |= BIT_MASK(EV_REP);
  134. input_dev->keycode = data->keycodes;
  135. input_dev->keycodesize = sizeof(data->keycodes[0]);
  136. input_dev->keycodemax = pdata->key_maxval + 1;
  137. for (i = 0; i < pdata->keymap_size; i++) {
  138. unsigned int val = MCS_KEY_VAL(pdata->keymap[i]);
  139. unsigned int code = MCS_KEY_CODE(pdata->keymap[i]);
  140. data->keycodes[val] = code;
  141. __set_bit(code, input_dev->keybit);
  142. }
  143. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  144. input_set_drvdata(input_dev, data);
  145. if (pdata->cfg_pin)
  146. pdata->cfg_pin();
  147. error = request_threaded_irq(client->irq, NULL, mcs_touchkey_interrupt,
  148. IRQF_TRIGGER_FALLING, client->dev.driver->name, data);
  149. if (error) {
  150. dev_err(&client->dev, "Failed to register interrupt\n");
  151. goto err_free_mem;
  152. }
  153. error = input_register_device(input_dev);
  154. if (error)
  155. goto err_free_irq;
  156. i2c_set_clientdata(client, data);
  157. return 0;
  158. err_free_irq:
  159. free_irq(client->irq, data);
  160. err_free_mem:
  161. input_free_device(input_dev);
  162. kfree(data);
  163. return error;
  164. }
  165. static int __devexit mcs_touchkey_remove(struct i2c_client *client)
  166. {
  167. struct mcs_touchkey_data *data = i2c_get_clientdata(client);
  168. free_irq(client->irq, data);
  169. input_unregister_device(data->input_dev);
  170. kfree(data);
  171. return 0;
  172. }
  173. static const struct i2c_device_id mcs_touchkey_id[] = {
  174. { "mcs5000_touchkey", MCS5000_TOUCHKEY },
  175. { "mcs5080_touchkey", MCS5080_TOUCHKEY },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(i2c, mcs_touchkey_id);
  179. static struct i2c_driver mcs_touchkey_driver = {
  180. .driver = {
  181. .name = "mcs_touchkey",
  182. .owner = THIS_MODULE,
  183. },
  184. .probe = mcs_touchkey_probe,
  185. .remove = __devexit_p(mcs_touchkey_remove),
  186. .id_table = mcs_touchkey_id,
  187. };
  188. static int __init mcs_touchkey_init(void)
  189. {
  190. return i2c_add_driver(&mcs_touchkey_driver);
  191. }
  192. static void __exit mcs_touchkey_exit(void)
  193. {
  194. i2c_del_driver(&mcs_touchkey_driver);
  195. }
  196. module_init(mcs_touchkey_init);
  197. module_exit(mcs_touchkey_exit);
  198. /* Module information */
  199. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  200. MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
  201. MODULE_DESCRIPTION("Touchkey driver for MELFAS MCS5000/5080 controller");
  202. MODULE_LICENSE("GPL");