ft6236.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * FocalTech FT6236 TouchScreen driver.
  3. *
  4. * Copyright (c) 2010 Focal tech Ltd.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/property.h>
  23. #define FT6236_MAX_TOUCH_POINTS 2
  24. #define FT6236_REG_TH_GROUP 0x80
  25. #define FT6236_REG_PERIODACTIVE 0x88
  26. #define FT6236_REG_LIB_VER_H 0xa1
  27. #define FT6236_REG_LIB_VER_L 0xa2
  28. #define FT6236_REG_CIPHER 0xa3
  29. #define FT6236_REG_FIRMID 0xa6
  30. #define FT6236_REG_FOCALTECH_ID 0xa8
  31. #define FT6236_REG_RELEASE_CODE_ID 0xaf
  32. #define FT6236_EVENT_PRESS_DOWN 0
  33. #define FT6236_EVENT_LIFT_UP 1
  34. #define FT6236_EVENT_CONTACT 2
  35. #define FT6236_EVENT_NO_EVENT 3
  36. struct ft6236_data {
  37. struct i2c_client *client;
  38. struct input_dev *input;
  39. struct gpio_desc *reset_gpio;
  40. u32 max_x;
  41. u32 max_y;
  42. bool invert_x;
  43. bool invert_y;
  44. bool swap_xy;
  45. };
  46. /*
  47. * This struct is a touchpoint as stored in hardware. Note that the id,
  48. * as well as the event, are stored in the upper nybble of the hi byte.
  49. */
  50. struct ft6236_touchpoint {
  51. union {
  52. u8 xhi;
  53. u8 event;
  54. };
  55. u8 xlo;
  56. union {
  57. u8 yhi;
  58. u8 id;
  59. };
  60. u8 ylo;
  61. u8 weight;
  62. u8 misc;
  63. } __packed;
  64. /* This packet represents the register map as read from offset 0 */
  65. struct ft6236_packet {
  66. u8 dev_mode;
  67. u8 gest_id;
  68. u8 touches;
  69. struct ft6236_touchpoint points[FT6236_MAX_TOUCH_POINTS];
  70. } __packed;
  71. static int ft6236_read(struct i2c_client *client, u8 reg, u8 len, void *data)
  72. {
  73. int error;
  74. error = i2c_smbus_read_i2c_block_data(client, reg, len, data);
  75. if (error < 0)
  76. return error;
  77. if (error != len)
  78. return -EIO;
  79. return 0;
  80. }
  81. static irqreturn_t ft6236_interrupt(int irq, void *dev_id)
  82. {
  83. struct ft6236_data *ft6236 = dev_id;
  84. struct device *dev = &ft6236->client->dev;
  85. struct input_dev *input = ft6236->input;
  86. struct ft6236_packet buf;
  87. u8 touches;
  88. int i, error;
  89. error = ft6236_read(ft6236->client, 0, sizeof(buf), &buf);
  90. if (error) {
  91. dev_err(dev, "read touchdata failed %d\n", error);
  92. return IRQ_HANDLED;
  93. }
  94. touches = buf.touches & 0xf;
  95. if (touches > FT6236_MAX_TOUCH_POINTS) {
  96. dev_dbg(dev,
  97. "%d touch points reported, only %d are supported\n",
  98. touches, FT6236_MAX_TOUCH_POINTS);
  99. touches = FT6236_MAX_TOUCH_POINTS;
  100. }
  101. for (i = 0; i < touches; i++) {
  102. struct ft6236_touchpoint *point = &buf.points[i];
  103. u16 x = ((point->xhi & 0xf) << 8) | buf.points[i].xlo;
  104. u16 y = ((point->yhi & 0xf) << 8) | buf.points[i].ylo;
  105. u8 event = point->event >> 6;
  106. u8 id = point->id >> 4;
  107. bool act = (event == FT6236_EVENT_PRESS_DOWN ||
  108. event == FT6236_EVENT_CONTACT);
  109. input_mt_slot(input, id);
  110. input_mt_report_slot_state(input, MT_TOOL_FINGER, act);
  111. if (!act)
  112. continue;
  113. if (ft6236->invert_x)
  114. x = ft6236->max_x - x;
  115. if (ft6236->invert_y)
  116. y = ft6236->max_y - y;
  117. if (ft6236->swap_xy) {
  118. input_report_abs(input, ABS_MT_POSITION_X, y);
  119. input_report_abs(input, ABS_MT_POSITION_Y, x);
  120. } else {
  121. input_report_abs(input, ABS_MT_POSITION_X, x);
  122. input_report_abs(input, ABS_MT_POSITION_Y, y);
  123. }
  124. }
  125. input_mt_sync_frame(input);
  126. input_sync(input);
  127. return IRQ_HANDLED;
  128. }
  129. static u8 ft6236_debug_read_byte(struct ft6236_data *ft6236, u8 reg)
  130. {
  131. struct i2c_client *client = ft6236->client;
  132. u8 val = 0;
  133. int error;
  134. error = ft6236_read(client, reg, 1, &val);
  135. if (error)
  136. dev_dbg(&client->dev,
  137. "error reading register 0x%02x: %d\n", reg, error);
  138. return val;
  139. }
  140. static void ft6236_debug_info(struct ft6236_data *ft6236)
  141. {
  142. struct device *dev = &ft6236->client->dev;
  143. dev_dbg(dev, "Touch threshold is %d\n",
  144. ft6236_debug_read_byte(ft6236, FT6236_REG_TH_GROUP) * 4);
  145. dev_dbg(dev, "Report rate is %dHz\n",
  146. ft6236_debug_read_byte(ft6236, FT6236_REG_PERIODACTIVE) * 10);
  147. dev_dbg(dev, "Firmware library version 0x%02x%02x\n",
  148. ft6236_debug_read_byte(ft6236, FT6236_REG_LIB_VER_H),
  149. ft6236_debug_read_byte(ft6236, FT6236_REG_LIB_VER_L));
  150. dev_dbg(dev, "Firmware version 0x%02x\n",
  151. ft6236_debug_read_byte(ft6236, FT6236_REG_FIRMID));
  152. dev_dbg(dev, "Chip vendor ID 0x%02x\n",
  153. ft6236_debug_read_byte(ft6236, FT6236_REG_CIPHER));
  154. dev_dbg(dev, "CTPM vendor ID 0x%02x\n",
  155. ft6236_debug_read_byte(ft6236, FT6236_REG_FOCALTECH_ID));
  156. dev_dbg(dev, "Release code version 0x%02x\n",
  157. ft6236_debug_read_byte(ft6236, FT6236_REG_RELEASE_CODE_ID));
  158. }
  159. static void ft6236_reset(struct ft6236_data *ft6236)
  160. {
  161. if (!ft6236->reset_gpio)
  162. return;
  163. gpiod_set_value_cansleep(ft6236->reset_gpio, 1);
  164. usleep_range(5000, 20000);
  165. gpiod_set_value_cansleep(ft6236->reset_gpio, 0);
  166. msleep(300);
  167. }
  168. static int ft6236_probe(struct i2c_client *client,
  169. const struct i2c_device_id *id)
  170. {
  171. struct device *dev = &client->dev;
  172. struct ft6236_data *ft6236;
  173. struct input_dev *input;
  174. u32 fuzz_x = 0, fuzz_y = 0;
  175. u8 val;
  176. int error;
  177. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  178. return -ENXIO;
  179. if (!client->irq) {
  180. dev_err(dev, "irq is missing\n");
  181. return -EINVAL;
  182. }
  183. ft6236 = devm_kzalloc(dev, sizeof(*ft6236), GFP_KERNEL);
  184. if (!ft6236)
  185. return -ENOMEM;
  186. ft6236->client = client;
  187. ft6236->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  188. GPIOD_OUT_LOW);
  189. if (IS_ERR(ft6236->reset_gpio)) {
  190. error = PTR_ERR(ft6236->reset_gpio);
  191. if (error != -EPROBE_DEFER)
  192. dev_err(dev, "error getting reset gpio: %d\n", error);
  193. return error;
  194. }
  195. ft6236_reset(ft6236);
  196. /* verify that the controller is present */
  197. error = ft6236_read(client, 0x00, 1, &val);
  198. if (error) {
  199. dev_err(dev, "failed to read from controller: %d\n", error);
  200. return error;
  201. }
  202. ft6236_debug_info(ft6236);
  203. input = devm_input_allocate_device(dev);
  204. if (!input)
  205. return -ENOMEM;
  206. ft6236->input = input;
  207. input->name = client->name;
  208. input->id.bustype = BUS_I2C;
  209. if (device_property_read_u32(dev, "touchscreen-size-x",
  210. &ft6236->max_x) ||
  211. device_property_read_u32(dev, "touchscreen-size-y",
  212. &ft6236->max_y)) {
  213. dev_err(dev, "touchscreen-size-x and/or -y missing\n");
  214. return -EINVAL;
  215. }
  216. device_property_read_u32(dev, "touchscreen-fuzz-x", &fuzz_x);
  217. device_property_read_u32(dev, "touchscreen-fuzz-y", &fuzz_y);
  218. ft6236->invert_x = device_property_read_bool(dev,
  219. "touchscreen-inverted-x");
  220. ft6236->invert_y = device_property_read_bool(dev,
  221. "touchscreen-inverted-y");
  222. ft6236->swap_xy = device_property_read_bool(dev,
  223. "touchscreen-swapped-x-y");
  224. if (ft6236->swap_xy) {
  225. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  226. ft6236->max_y, fuzz_y, 0);
  227. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  228. ft6236->max_x, fuzz_x, 0);
  229. } else {
  230. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  231. ft6236->max_x, fuzz_x, 0);
  232. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  233. ft6236->max_y, fuzz_y, 0);
  234. }
  235. error = input_mt_init_slots(input, FT6236_MAX_TOUCH_POINTS,
  236. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  237. if (error)
  238. return error;
  239. error = devm_request_threaded_irq(dev, client->irq, NULL,
  240. ft6236_interrupt, IRQF_ONESHOT,
  241. client->name, ft6236);
  242. if (error) {
  243. dev_err(dev, "request irq %d failed: %d\n", client->irq, error);
  244. return error;
  245. }
  246. error = input_register_device(input);
  247. if (error) {
  248. dev_err(dev, "failed to register input device: %d\n", error);
  249. return error;
  250. }
  251. return 0;
  252. }
  253. #ifdef CONFIG_OF
  254. static const struct of_device_id ft6236_of_match[] = {
  255. { .compatible = "focaltech,ft6236", },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(of, ft6236_of_match);
  259. #endif
  260. static const struct i2c_device_id ft6236_id[] = {
  261. { "ft6236", },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(i2c, ft6236_id);
  265. static struct i2c_driver ft6236_driver = {
  266. .driver = {
  267. .name = "ft6236",
  268. .of_match_table = of_match_ptr(ft6236_of_match),
  269. },
  270. .probe = ft6236_probe,
  271. .id_table = ft6236_id,
  272. };
  273. module_i2c_driver(ft6236_driver);
  274. MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
  275. MODULE_AUTHOR("Noralf Trønnes <noralf@tronnes.org>");
  276. MODULE_DESCRIPTION("FocalTech FT6236 TouchScreen driver");
  277. MODULE_LICENSE("GPL v2");