mcs5000_ts.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * mcs5000_ts.c - Touchscreen driver for MELFAS MCS-5000 controller
  3. *
  4. * Copyright (C) 2009 Samsung Electronics Co.Ltd
  5. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6. *
  7. * Based on wm97xx-core.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/i2c/mcs.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/input.h>
  20. #include <linux/irq.h>
  21. #include <linux/slab.h>
  22. /* Registers */
  23. #define MCS5000_TS_STATUS 0x00
  24. #define STATUS_OFFSET 0
  25. #define STATUS_NO (0 << STATUS_OFFSET)
  26. #define STATUS_INIT (1 << STATUS_OFFSET)
  27. #define STATUS_SENSING (2 << STATUS_OFFSET)
  28. #define STATUS_COORD (3 << STATUS_OFFSET)
  29. #define STATUS_GESTURE (4 << STATUS_OFFSET)
  30. #define ERROR_OFFSET 4
  31. #define ERROR_NO (0 << ERROR_OFFSET)
  32. #define ERROR_POWER_ON_RESET (1 << ERROR_OFFSET)
  33. #define ERROR_INT_RESET (2 << ERROR_OFFSET)
  34. #define ERROR_EXT_RESET (3 << ERROR_OFFSET)
  35. #define ERROR_INVALID_REG_ADDRESS (8 << ERROR_OFFSET)
  36. #define ERROR_INVALID_REG_VALUE (9 << ERROR_OFFSET)
  37. #define MCS5000_TS_OP_MODE 0x01
  38. #define RESET_OFFSET 0
  39. #define RESET_NO (0 << RESET_OFFSET)
  40. #define RESET_EXT_SOFT (1 << RESET_OFFSET)
  41. #define OP_MODE_OFFSET 1
  42. #define OP_MODE_SLEEP (0 << OP_MODE_OFFSET)
  43. #define OP_MODE_ACTIVE (1 << OP_MODE_OFFSET)
  44. #define GESTURE_OFFSET 4
  45. #define GESTURE_DISABLE (0 << GESTURE_OFFSET)
  46. #define GESTURE_ENABLE (1 << GESTURE_OFFSET)
  47. #define PROXIMITY_OFFSET 5
  48. #define PROXIMITY_DISABLE (0 << PROXIMITY_OFFSET)
  49. #define PROXIMITY_ENABLE (1 << PROXIMITY_OFFSET)
  50. #define SCAN_MODE_OFFSET 6
  51. #define SCAN_MODE_INTERRUPT (0 << SCAN_MODE_OFFSET)
  52. #define SCAN_MODE_POLLING (1 << SCAN_MODE_OFFSET)
  53. #define REPORT_RATE_OFFSET 7
  54. #define REPORT_RATE_40 (0 << REPORT_RATE_OFFSET)
  55. #define REPORT_RATE_80 (1 << REPORT_RATE_OFFSET)
  56. #define MCS5000_TS_SENS_CTL 0x02
  57. #define MCS5000_TS_FILTER_CTL 0x03
  58. #define PRI_FILTER_OFFSET 0
  59. #define SEC_FILTER_OFFSET 4
  60. #define MCS5000_TS_X_SIZE_UPPER 0x08
  61. #define MCS5000_TS_X_SIZE_LOWER 0x09
  62. #define MCS5000_TS_Y_SIZE_UPPER 0x0A
  63. #define MCS5000_TS_Y_SIZE_LOWER 0x0B
  64. #define MCS5000_TS_INPUT_INFO 0x10
  65. #define INPUT_TYPE_OFFSET 0
  66. #define INPUT_TYPE_NONTOUCH (0 << INPUT_TYPE_OFFSET)
  67. #define INPUT_TYPE_SINGLE (1 << INPUT_TYPE_OFFSET)
  68. #define INPUT_TYPE_DUAL (2 << INPUT_TYPE_OFFSET)
  69. #define INPUT_TYPE_PALM (3 << INPUT_TYPE_OFFSET)
  70. #define INPUT_TYPE_PROXIMITY (7 << INPUT_TYPE_OFFSET)
  71. #define GESTURE_CODE_OFFSET 3
  72. #define GESTURE_CODE_NO (0 << GESTURE_CODE_OFFSET)
  73. #define MCS5000_TS_X_POS_UPPER 0x11
  74. #define MCS5000_TS_X_POS_LOWER 0x12
  75. #define MCS5000_TS_Y_POS_UPPER 0x13
  76. #define MCS5000_TS_Y_POS_LOWER 0x14
  77. #define MCS5000_TS_Z_POS 0x15
  78. #define MCS5000_TS_WIDTH 0x16
  79. #define MCS5000_TS_GESTURE_VAL 0x17
  80. #define MCS5000_TS_MODULE_REV 0x20
  81. #define MCS5000_TS_FIRMWARE_VER 0x21
  82. /* Touchscreen absolute values */
  83. #define MCS5000_MAX_XC 0x3ff
  84. #define MCS5000_MAX_YC 0x3ff
  85. enum mcs5000_ts_read_offset {
  86. READ_INPUT_INFO,
  87. READ_X_POS_UPPER,
  88. READ_X_POS_LOWER,
  89. READ_Y_POS_UPPER,
  90. READ_Y_POS_LOWER,
  91. READ_BLOCK_SIZE,
  92. };
  93. /* Each client has this additional data */
  94. struct mcs5000_ts_data {
  95. struct i2c_client *client;
  96. struct input_dev *input_dev;
  97. const struct mcs_platform_data *platform_data;
  98. };
  99. static irqreturn_t mcs5000_ts_interrupt(int irq, void *dev_id)
  100. {
  101. struct mcs5000_ts_data *data = dev_id;
  102. struct i2c_client *client = data->client;
  103. u8 buffer[READ_BLOCK_SIZE];
  104. int err;
  105. int x;
  106. int y;
  107. err = i2c_smbus_read_i2c_block_data(client, MCS5000_TS_INPUT_INFO,
  108. READ_BLOCK_SIZE, buffer);
  109. if (err < 0) {
  110. dev_err(&client->dev, "%s, err[%d]\n", __func__, err);
  111. goto out;
  112. }
  113. switch (buffer[READ_INPUT_INFO]) {
  114. case INPUT_TYPE_NONTOUCH:
  115. input_report_key(data->input_dev, BTN_TOUCH, 0);
  116. input_sync(data->input_dev);
  117. break;
  118. case INPUT_TYPE_SINGLE:
  119. x = (buffer[READ_X_POS_UPPER] << 8) | buffer[READ_X_POS_LOWER];
  120. y = (buffer[READ_Y_POS_UPPER] << 8) | buffer[READ_Y_POS_LOWER];
  121. input_report_key(data->input_dev, BTN_TOUCH, 1);
  122. input_report_abs(data->input_dev, ABS_X, x);
  123. input_report_abs(data->input_dev, ABS_Y, y);
  124. input_sync(data->input_dev);
  125. break;
  126. case INPUT_TYPE_DUAL:
  127. /* TODO */
  128. break;
  129. case INPUT_TYPE_PALM:
  130. /* TODO */
  131. break;
  132. case INPUT_TYPE_PROXIMITY:
  133. /* TODO */
  134. break;
  135. default:
  136. dev_err(&client->dev, "Unknown ts input type %d\n",
  137. buffer[READ_INPUT_INFO]);
  138. break;
  139. }
  140. out:
  141. return IRQ_HANDLED;
  142. }
  143. static void mcs5000_ts_phys_init(struct mcs5000_ts_data *data,
  144. const struct mcs_platform_data *platform_data)
  145. {
  146. struct i2c_client *client = data->client;
  147. /* Touch reset & sleep mode */
  148. i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE,
  149. RESET_EXT_SOFT | OP_MODE_SLEEP);
  150. /* Touch size */
  151. i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_UPPER,
  152. platform_data->x_size >> 8);
  153. i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_LOWER,
  154. platform_data->x_size & 0xff);
  155. i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_UPPER,
  156. platform_data->y_size >> 8);
  157. i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_LOWER,
  158. platform_data->y_size & 0xff);
  159. /* Touch active mode & 80 report rate */
  160. i2c_smbus_write_byte_data(data->client, MCS5000_TS_OP_MODE,
  161. OP_MODE_ACTIVE | REPORT_RATE_80);
  162. }
  163. static int mcs5000_ts_probe(struct i2c_client *client,
  164. const struct i2c_device_id *id)
  165. {
  166. const struct mcs_platform_data *pdata;
  167. struct mcs5000_ts_data *data;
  168. struct input_dev *input_dev;
  169. int error;
  170. pdata = dev_get_platdata(&client->dev);
  171. if (!pdata)
  172. return -EINVAL;
  173. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  174. if (!data) {
  175. dev_err(&client->dev, "Failed to allocate memory\n");
  176. return -ENOMEM;
  177. }
  178. data->client = client;
  179. input_dev = devm_input_allocate_device(&client->dev);
  180. if (!input_dev) {
  181. dev_err(&client->dev, "Failed to allocate input device\n");
  182. return -ENOMEM;
  183. }
  184. input_dev->name = "MELFAS MCS-5000 Touchscreen";
  185. input_dev->id.bustype = BUS_I2C;
  186. input_dev->dev.parent = &client->dev;
  187. __set_bit(EV_ABS, input_dev->evbit);
  188. __set_bit(EV_KEY, input_dev->evbit);
  189. __set_bit(BTN_TOUCH, input_dev->keybit);
  190. input_set_abs_params(input_dev, ABS_X, 0, MCS5000_MAX_XC, 0, 0);
  191. input_set_abs_params(input_dev, ABS_Y, 0, MCS5000_MAX_YC, 0, 0);
  192. input_set_drvdata(input_dev, data);
  193. data->input_dev = input_dev;
  194. if (pdata->cfg_pin)
  195. pdata->cfg_pin();
  196. error = devm_request_threaded_irq(&client->dev, client->irq,
  197. NULL, mcs5000_ts_interrupt,
  198. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  199. "mcs5000_ts", data);
  200. if (error) {
  201. dev_err(&client->dev, "Failed to register interrupt\n");
  202. return error;
  203. }
  204. error = input_register_device(data->input_dev);
  205. if (error) {
  206. dev_err(&client->dev, "Failed to register input device\n");
  207. return error;
  208. }
  209. mcs5000_ts_phys_init(data, pdata);
  210. i2c_set_clientdata(client, data);
  211. return 0;
  212. }
  213. static int __maybe_unused mcs5000_ts_suspend(struct device *dev)
  214. {
  215. struct i2c_client *client = to_i2c_client(dev);
  216. /* Touch sleep mode */
  217. i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE, OP_MODE_SLEEP);
  218. return 0;
  219. }
  220. static int __maybe_unused mcs5000_ts_resume(struct device *dev)
  221. {
  222. struct i2c_client *client = to_i2c_client(dev);
  223. struct mcs5000_ts_data *data = i2c_get_clientdata(client);
  224. const struct mcs_platform_data *pdata = dev_get_platdata(dev);
  225. mcs5000_ts_phys_init(data, pdata);
  226. return 0;
  227. }
  228. static SIMPLE_DEV_PM_OPS(mcs5000_ts_pm, mcs5000_ts_suspend, mcs5000_ts_resume);
  229. static const struct i2c_device_id mcs5000_ts_id[] = {
  230. { "mcs5000_ts", 0 },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(i2c, mcs5000_ts_id);
  234. static struct i2c_driver mcs5000_ts_driver = {
  235. .probe = mcs5000_ts_probe,
  236. .driver = {
  237. .name = "mcs5000_ts",
  238. .pm = &mcs5000_ts_pm,
  239. },
  240. .id_table = mcs5000_ts_id,
  241. };
  242. module_i2c_driver(mcs5000_ts_driver);
  243. /* Module information */
  244. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  245. MODULE_DESCRIPTION("Touchscreen driver for MELFAS MCS-5000 controller");
  246. MODULE_LICENSE("GPL");