max11801_ts.c 6.9 KB

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