max11801_ts.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/init.h>
  35. #include <linux/i2c.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/input.h>
  38. #include <linux/slab.h>
  39. #include <linux/bitops.h>
  40. /* Register Address define */
  41. #define GENERNAL_STATUS_REG 0x00
  42. #define GENERNAL_CONF_REG 0x01
  43. #define MESURE_RES_CONF_REG 0x02
  44. #define MESURE_AVER_CONF_REG 0x03
  45. #define ADC_SAMPLE_TIME_CONF_REG 0x04
  46. #define PANEL_SETUPTIME_CONF_REG 0x05
  47. #define DELAY_CONVERSION_CONF_REG 0x06
  48. #define TOUCH_DETECT_PULLUP_CONF_REG 0x07
  49. #define AUTO_MODE_TIME_CONF_REG 0x08 /* only for max11800/max11801 */
  50. #define APERTURE_CONF_REG 0x09 /* only for max11800/max11801 */
  51. #define AUX_MESURE_CONF_REG 0x0a
  52. #define OP_MODE_CONF_REG 0x0b
  53. /* FIFO is found only in max11800 and max11801 */
  54. #define FIFO_RD_CMD (0x50 << 1)
  55. #define MAX11801_FIFO_INT (1 << 2)
  56. #define MAX11801_FIFO_OVERFLOW (1 << 3)
  57. #define XY_BUFSIZE 4
  58. #define XY_BUF_OFFSET 4
  59. #define MAX11801_MAX_X 0xfff
  60. #define MAX11801_MAX_Y 0xfff
  61. #define MEASURE_TAG_OFFSET 2
  62. #define MEASURE_TAG_MASK (3 << MEASURE_TAG_OFFSET)
  63. #define EVENT_TAG_OFFSET 0
  64. #define EVENT_TAG_MASK (3 << EVENT_TAG_OFFSET)
  65. #define MEASURE_X_TAG (0 << MEASURE_TAG_OFFSET)
  66. #define MEASURE_Y_TAG (1 << MEASURE_TAG_OFFSET)
  67. /* These are the state of touch event state machine */
  68. enum {
  69. EVENT_INIT,
  70. EVENT_MIDDLE,
  71. EVENT_RELEASE,
  72. EVENT_FIFO_END
  73. };
  74. struct max11801_data {
  75. struct i2c_client *client;
  76. struct input_dev *input_dev;
  77. };
  78. static u8 read_register(struct i2c_client *client, int addr)
  79. {
  80. /* XXX: The chip ignores LSB of register address */
  81. return i2c_smbus_read_byte_data(client, addr << 1);
  82. }
  83. static int max11801_write_reg(struct i2c_client *client, int addr, int data)
  84. {
  85. /* XXX: The chip ignores LSB of register address */
  86. return i2c_smbus_write_byte_data(client, addr << 1, data);
  87. }
  88. static irqreturn_t max11801_ts_interrupt(int irq, void *dev_id)
  89. {
  90. struct max11801_data *data = dev_id;
  91. struct i2c_client *client = data->client;
  92. int status, i, ret;
  93. u8 buf[XY_BUFSIZE];
  94. int x = -1;
  95. int y = -1;
  96. status = read_register(data->client, GENERNAL_STATUS_REG);
  97. if (status & (MAX11801_FIFO_INT | MAX11801_FIFO_OVERFLOW)) {
  98. status = read_register(data->client, GENERNAL_STATUS_REG);
  99. ret = i2c_smbus_read_i2c_block_data(client, FIFO_RD_CMD,
  100. XY_BUFSIZE, buf);
  101. /*
  102. * We should get 4 bytes buffer that contains X,Y
  103. * and event tag
  104. */
  105. if (ret < XY_BUFSIZE)
  106. goto out;
  107. for (i = 0; i < XY_BUFSIZE; i += XY_BUFSIZE / 2) {
  108. if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_X_TAG)
  109. x = (buf[i] << XY_BUF_OFFSET) +
  110. (buf[i + 1] >> XY_BUF_OFFSET);
  111. else if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_Y_TAG)
  112. y = (buf[i] << XY_BUF_OFFSET) +
  113. (buf[i + 1] >> XY_BUF_OFFSET);
  114. }
  115. if ((buf[1] & EVENT_TAG_MASK) != (buf[3] & EVENT_TAG_MASK))
  116. goto out;
  117. switch (buf[1] & EVENT_TAG_MASK) {
  118. case EVENT_INIT:
  119. /* fall through */
  120. case EVENT_MIDDLE:
  121. input_report_abs(data->input_dev, ABS_X, x);
  122. input_report_abs(data->input_dev, ABS_Y, y);
  123. input_event(data->input_dev, EV_KEY, BTN_TOUCH, 1);
  124. input_sync(data->input_dev);
  125. break;
  126. case EVENT_RELEASE:
  127. input_event(data->input_dev, EV_KEY, BTN_TOUCH, 0);
  128. input_sync(data->input_dev);
  129. break;
  130. case EVENT_FIFO_END:
  131. break;
  132. }
  133. }
  134. out:
  135. return IRQ_HANDLED;
  136. }
  137. static void __devinit max11801_ts_phy_init(struct max11801_data *data)
  138. {
  139. struct i2c_client *client = data->client;
  140. /* Average X,Y, take 16 samples, average eight media sample */
  141. max11801_write_reg(client, MESURE_AVER_CONF_REG, 0xff);
  142. /* X,Y panel setup time set to 20us */
  143. max11801_write_reg(client, PANEL_SETUPTIME_CONF_REG, 0x11);
  144. /* Rough pullup time (2uS), Fine pullup time (10us) */
  145. max11801_write_reg(client, TOUCH_DETECT_PULLUP_CONF_REG, 0x10);
  146. /* Auto mode init period = 5ms , scan period = 5ms*/
  147. max11801_write_reg(client, AUTO_MODE_TIME_CONF_REG, 0xaa);
  148. /* Aperture X,Y set to +- 4LSB */
  149. max11801_write_reg(client, APERTURE_CONF_REG, 0x33);
  150. /* Enable Power, enable Automode, enable Aperture, enable Average X,Y */
  151. max11801_write_reg(client, OP_MODE_CONF_REG, 0x36);
  152. }
  153. static int __devinit max11801_ts_probe(struct i2c_client *client,
  154. const struct i2c_device_id *id)
  155. {
  156. struct max11801_data *data;
  157. struct input_dev *input_dev;
  158. int error;
  159. data = kzalloc(sizeof(struct max11801_data), GFP_KERNEL);
  160. input_dev = input_allocate_device();
  161. if (!data || !input_dev) {
  162. dev_err(&client->dev, "Failed to allocate memory\n");
  163. error = -ENOMEM;
  164. goto err_free_mem;
  165. }
  166. data->client = client;
  167. data->input_dev = input_dev;
  168. input_dev->name = "max11801_ts";
  169. input_dev->id.bustype = BUS_I2C;
  170. input_dev->dev.parent = &client->dev;
  171. __set_bit(EV_ABS, input_dev->evbit);
  172. __set_bit(EV_KEY, input_dev->evbit);
  173. __set_bit(BTN_TOUCH, input_dev->keybit);
  174. input_set_abs_params(input_dev, ABS_X, 0, MAX11801_MAX_X, 0, 0);
  175. input_set_abs_params(input_dev, ABS_Y, 0, MAX11801_MAX_Y, 0, 0);
  176. input_set_drvdata(input_dev, data);
  177. max11801_ts_phy_init(data);
  178. error = request_threaded_irq(client->irq, NULL, max11801_ts_interrupt,
  179. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  180. "max11801_ts", data);
  181. if (error) {
  182. dev_err(&client->dev, "Failed to register interrupt\n");
  183. goto err_free_mem;
  184. }
  185. error = input_register_device(data->input_dev);
  186. if (error)
  187. goto err_free_irq;
  188. i2c_set_clientdata(client, data);
  189. return 0;
  190. err_free_irq:
  191. free_irq(client->irq, data);
  192. err_free_mem:
  193. input_free_device(input_dev);
  194. kfree(data);
  195. return error;
  196. }
  197. static __devexit int max11801_ts_remove(struct i2c_client *client)
  198. {
  199. struct max11801_data *data = i2c_get_clientdata(client);
  200. free_irq(client->irq, data);
  201. input_unregister_device(data->input_dev);
  202. kfree(data);
  203. return 0;
  204. }
  205. static const struct i2c_device_id max11801_ts_id[] = {
  206. {"max11801", 0},
  207. { }
  208. };
  209. MODULE_DEVICE_TABLE(i2c, max11801_ts_id);
  210. static struct i2c_driver max11801_ts_driver = {
  211. .driver = {
  212. .name = "max11801_ts",
  213. .owner = THIS_MODULE,
  214. },
  215. .id_table = max11801_ts_id,
  216. .probe = max11801_ts_probe,
  217. .remove = __devexit_p(max11801_ts_remove),
  218. };
  219. module_i2c_driver(max11801_ts_driver);
  220. MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
  221. MODULE_DESCRIPTION("Touchscreen driver for MAXI MAX11801 controller");
  222. MODULE_LICENSE("GPL");