max11801_ts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for MAXI MAX11801 - A Resistive touch screen controller with
  4. * i2c interface
  5. *
  6. * Copyright (C) 2011 Freescale Semiconductor, Inc.
  7. * Author: Zhang Jiejing <[email protected]>
  8. *
  9. * Based on mcs5000_ts.c
  10. */
  11. /*
  12. * This driver aims to support the series of MAXI touch chips max11801
  13. * through max11803. The main difference between these 4 chips can be
  14. * found in the table below:
  15. * -----------------------------------------------------
  16. * | CHIP | AUTO MODE SUPPORT(FIFO) | INTERFACE |
  17. * |----------------------------------------------------|
  18. * | max11800 | YES | SPI |
  19. * | max11801 | YES | I2C |
  20. * | max11802 | NO | SPI |
  21. * | max11803 | NO | I2C |
  22. * ------------------------------------------------------
  23. *
  24. * Currently, this driver only supports max11801.
  25. *
  26. * Data Sheet:
  27. * http://www.maxim-ic.com/datasheet/index.mvp/id/5943
  28. */
  29. #include <linux/module.h>
  30. #include <linux/i2c.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/input.h>
  33. #include <linux/slab.h>
  34. #include <linux/bitops.h>
  35. /* Register Address define */
  36. #define GENERNAL_STATUS_REG 0x00
  37. #define GENERNAL_CONF_REG 0x01
  38. #define MESURE_RES_CONF_REG 0x02
  39. #define MESURE_AVER_CONF_REG 0x03
  40. #define ADC_SAMPLE_TIME_CONF_REG 0x04
  41. #define PANEL_SETUPTIME_CONF_REG 0x05
  42. #define DELAY_CONVERSION_CONF_REG 0x06
  43. #define TOUCH_DETECT_PULLUP_CONF_REG 0x07
  44. #define AUTO_MODE_TIME_CONF_REG 0x08 /* only for max11800/max11801 */
  45. #define APERTURE_CONF_REG 0x09 /* only for max11800/max11801 */
  46. #define AUX_MESURE_CONF_REG 0x0a
  47. #define OP_MODE_CONF_REG 0x0b
  48. /* FIFO is found only in max11800 and max11801 */
  49. #define FIFO_RD_CMD (0x50 << 1)
  50. #define MAX11801_FIFO_INT (1 << 2)
  51. #define MAX11801_FIFO_OVERFLOW (1 << 3)
  52. #define XY_BUFSIZE 4
  53. #define XY_BUF_OFFSET 4
  54. #define MAX11801_MAX_X 0xfff
  55. #define MAX11801_MAX_Y 0xfff
  56. #define MEASURE_TAG_OFFSET 2
  57. #define MEASURE_TAG_MASK (3 << MEASURE_TAG_OFFSET)
  58. #define EVENT_TAG_OFFSET 0
  59. #define EVENT_TAG_MASK (3 << EVENT_TAG_OFFSET)
  60. #define MEASURE_X_TAG (0 << MEASURE_TAG_OFFSET)
  61. #define MEASURE_Y_TAG (1 << MEASURE_TAG_OFFSET)
  62. /* These are the state of touch event state machine */
  63. enum {
  64. EVENT_INIT,
  65. EVENT_MIDDLE,
  66. EVENT_RELEASE,
  67. EVENT_FIFO_END
  68. };
  69. struct max11801_data {
  70. struct i2c_client *client;
  71. struct input_dev *input_dev;
  72. };
  73. static u8 read_register(struct i2c_client *client, int addr)
  74. {
  75. /* XXX: The chip ignores LSB of register address */
  76. return i2c_smbus_read_byte_data(client, addr << 1);
  77. }
  78. static int max11801_write_reg(struct i2c_client *client, int addr, int data)
  79. {
  80. /* XXX: The chip ignores LSB of register address */
  81. return i2c_smbus_write_byte_data(client, addr << 1, data);
  82. }
  83. static irqreturn_t max11801_ts_interrupt(int irq, void *dev_id)
  84. {
  85. struct max11801_data *data = dev_id;
  86. struct i2c_client *client = data->client;
  87. int status, i, ret;
  88. u8 buf[XY_BUFSIZE];
  89. int x = -1;
  90. int y = -1;
  91. status = read_register(data->client, GENERNAL_STATUS_REG);
  92. if (status & (MAX11801_FIFO_INT | MAX11801_FIFO_OVERFLOW)) {
  93. status = read_register(data->client, GENERNAL_STATUS_REG);
  94. ret = i2c_smbus_read_i2c_block_data(client, FIFO_RD_CMD,
  95. XY_BUFSIZE, buf);
  96. /*
  97. * We should get 4 bytes buffer that contains X,Y
  98. * and event tag
  99. */
  100. if (ret < XY_BUFSIZE)
  101. goto out;
  102. for (i = 0; i < XY_BUFSIZE; i += XY_BUFSIZE / 2) {
  103. if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_X_TAG)
  104. x = (buf[i] << XY_BUF_OFFSET) +
  105. (buf[i + 1] >> XY_BUF_OFFSET);
  106. else if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_Y_TAG)
  107. y = (buf[i] << XY_BUF_OFFSET) +
  108. (buf[i + 1] >> XY_BUF_OFFSET);
  109. }
  110. if ((buf[1] & EVENT_TAG_MASK) != (buf[3] & EVENT_TAG_MASK))
  111. goto out;
  112. switch (buf[1] & EVENT_TAG_MASK) {
  113. case EVENT_INIT:
  114. case EVENT_MIDDLE:
  115. input_report_abs(data->input_dev, ABS_X, x);
  116. input_report_abs(data->input_dev, ABS_Y, y);
  117. input_event(data->input_dev, EV_KEY, BTN_TOUCH, 1);
  118. input_sync(data->input_dev);
  119. break;
  120. case EVENT_RELEASE:
  121. input_event(data->input_dev, EV_KEY, BTN_TOUCH, 0);
  122. input_sync(data->input_dev);
  123. break;
  124. case EVENT_FIFO_END:
  125. break;
  126. }
  127. }
  128. out:
  129. return IRQ_HANDLED;
  130. }
  131. static void max11801_ts_phy_init(struct max11801_data *data)
  132. {
  133. struct i2c_client *client = data->client;
  134. /* Average X,Y, take 16 samples, average eight media sample */
  135. max11801_write_reg(client, MESURE_AVER_CONF_REG, 0xff);
  136. /* X,Y panel setup time set to 20us */
  137. max11801_write_reg(client, PANEL_SETUPTIME_CONF_REG, 0x11);
  138. /* Rough pullup time (2uS), Fine pullup time (10us) */
  139. max11801_write_reg(client, TOUCH_DETECT_PULLUP_CONF_REG, 0x10);
  140. /* Auto mode init period = 5ms , scan period = 5ms*/
  141. max11801_write_reg(client, AUTO_MODE_TIME_CONF_REG, 0xaa);
  142. /* Aperture X,Y set to +- 4LSB */
  143. max11801_write_reg(client, APERTURE_CONF_REG, 0x33);
  144. /* Enable Power, enable Automode, enable Aperture, enable Average X,Y */
  145. max11801_write_reg(client, OP_MODE_CONF_REG, 0x36);
  146. }
  147. static int max11801_ts_probe(struct i2c_client *client,
  148. const struct i2c_device_id *id)
  149. {
  150. struct max11801_data *data;
  151. struct input_dev *input_dev;
  152. int error;
  153. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  154. input_dev = devm_input_allocate_device(&client->dev);
  155. if (!data || !input_dev) {
  156. dev_err(&client->dev, "Failed to allocate memory\n");
  157. return -ENOMEM;
  158. }
  159. data->client = client;
  160. data->input_dev = input_dev;
  161. input_dev->name = "max11801_ts";
  162. input_dev->id.bustype = BUS_I2C;
  163. input_dev->dev.parent = &client->dev;
  164. __set_bit(EV_ABS, input_dev->evbit);
  165. __set_bit(EV_KEY, input_dev->evbit);
  166. __set_bit(BTN_TOUCH, input_dev->keybit);
  167. input_set_abs_params(input_dev, ABS_X, 0, MAX11801_MAX_X, 0, 0);
  168. input_set_abs_params(input_dev, ABS_Y, 0, MAX11801_MAX_Y, 0, 0);
  169. max11801_ts_phy_init(data);
  170. error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  171. max11801_ts_interrupt,
  172. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  173. "max11801_ts", data);
  174. if (error) {
  175. dev_err(&client->dev, "Failed to register interrupt\n");
  176. return error;
  177. }
  178. error = input_register_device(data->input_dev);
  179. if (error)
  180. return error;
  181. return 0;
  182. }
  183. static const struct i2c_device_id max11801_ts_id[] = {
  184. {"max11801", 0},
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(i2c, max11801_ts_id);
  188. static const struct of_device_id max11801_ts_dt_ids[] = {
  189. { .compatible = "maxim,max11801" },
  190. { /* sentinel */ }
  191. };
  192. MODULE_DEVICE_TABLE(of, max11801_ts_dt_ids);
  193. static struct i2c_driver max11801_ts_driver = {
  194. .driver = {
  195. .name = "max11801_ts",
  196. .of_match_table = max11801_ts_dt_ids,
  197. },
  198. .id_table = max11801_ts_id,
  199. .probe = max11801_ts_probe,
  200. };
  201. module_i2c_driver(max11801_ts_driver);
  202. MODULE_AUTHOR("Zhang Jiejing <[email protected]>");
  203. MODULE_DESCRIPTION("Touchscreen driver for MAXI MAX11801 controller");
  204. MODULE_LICENSE("GPL");