tps6507x-ts.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Touchscreen driver for the tps6507x chip.
  3. *
  4. * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
  5. *
  6. * Credits:
  7. *
  8. * Using code from tsc2007, MtekVision Co., Ltd.
  9. *
  10. * For licencing details see kernel-base/COPYING
  11. *
  12. * TPS65070, TPS65073, TPS650731, and TPS650732 support
  13. * 10 bit touch screen interface.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/slab.h>
  18. #include <linux/input.h>
  19. #include <linux/input-polldev.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mfd/tps6507x.h>
  22. #include <linux/input/tps6507x-ts.h>
  23. #include <linux/delay.h>
  24. #define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
  25. #define TPS_DEFAULT_MIN_PRESSURE 0x30
  26. #define MAX_10BIT ((1 << 10) - 1)
  27. #define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \
  28. TPS6507X_ADCONFIG_START_CONVERSION | \
  29. TPS6507X_ADCONFIG_INPUT_REAL_TSC)
  30. #define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC)
  31. struct ts_event {
  32. u16 x;
  33. u16 y;
  34. u16 pressure;
  35. };
  36. struct tps6507x_ts {
  37. struct device *dev;
  38. struct input_polled_dev *poll_dev;
  39. struct tps6507x_dev *mfd;
  40. char phys[32];
  41. struct ts_event tc;
  42. u16 min_pressure;
  43. bool pendown;
  44. };
  45. static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
  46. {
  47. return tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
  48. }
  49. static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
  50. {
  51. return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
  52. }
  53. static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
  54. u8 tsc_mode, u16 *value)
  55. {
  56. s32 ret;
  57. u8 adc_status;
  58. u8 result;
  59. /* Route input signal to A/D converter */
  60. ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
  61. if (ret) {
  62. dev_err(tsc->dev, "TSC mode read failed\n");
  63. goto err;
  64. }
  65. /* Start A/D conversion */
  66. ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
  67. TPS6507X_ADCONFIG_CONVERT_TS);
  68. if (ret) {
  69. dev_err(tsc->dev, "ADC config write failed\n");
  70. return ret;
  71. }
  72. do {
  73. ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
  74. &adc_status);
  75. if (ret) {
  76. dev_err(tsc->dev, "ADC config read failed\n");
  77. goto err;
  78. }
  79. } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
  80. ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
  81. if (ret) {
  82. dev_err(tsc->dev, "ADC result 2 read failed\n");
  83. goto err;
  84. }
  85. *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
  86. ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
  87. if (ret) {
  88. dev_err(tsc->dev, "ADC result 1 read failed\n");
  89. goto err;
  90. }
  91. *value |= result;
  92. dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
  93. err:
  94. return ret;
  95. }
  96. /* Need to call tps6507x_adc_standby() after using A/D converter for the
  97. * touch screen interrupt to work properly.
  98. */
  99. static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
  100. {
  101. s32 ret;
  102. s32 loops = 0;
  103. u8 val;
  104. ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
  105. TPS6507X_ADCONFIG_INPUT_TSC);
  106. if (ret)
  107. return ret;
  108. ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
  109. TPS6507X_TSCMODE_STANDBY);
  110. if (ret)
  111. return ret;
  112. ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
  113. if (ret)
  114. return ret;
  115. while (val & TPS6507X_REG_TSC_INT) {
  116. mdelay(10);
  117. ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
  118. if (ret)
  119. return ret;
  120. loops++;
  121. }
  122. return ret;
  123. }
  124. static void tps6507x_ts_poll(struct input_polled_dev *poll_dev)
  125. {
  126. struct tps6507x_ts *tsc = poll_dev->private;
  127. struct input_dev *input_dev = poll_dev->input;
  128. bool pendown;
  129. s32 ret;
  130. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
  131. &tsc->tc.pressure);
  132. if (ret)
  133. goto done;
  134. pendown = tsc->tc.pressure > tsc->min_pressure;
  135. if (unlikely(!pendown && tsc->pendown)) {
  136. dev_dbg(tsc->dev, "UP\n");
  137. input_report_key(input_dev, BTN_TOUCH, 0);
  138. input_report_abs(input_dev, ABS_PRESSURE, 0);
  139. input_sync(input_dev);
  140. tsc->pendown = false;
  141. }
  142. if (pendown) {
  143. if (!tsc->pendown) {
  144. dev_dbg(tsc->dev, "DOWN\n");
  145. input_report_key(input_dev, BTN_TOUCH, 1);
  146. } else
  147. dev_dbg(tsc->dev, "still down\n");
  148. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
  149. &tsc->tc.x);
  150. if (ret)
  151. goto done;
  152. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
  153. &tsc->tc.y);
  154. if (ret)
  155. goto done;
  156. input_report_abs(input_dev, ABS_X, tsc->tc.x);
  157. input_report_abs(input_dev, ABS_Y, tsc->tc.y);
  158. input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
  159. input_sync(input_dev);
  160. tsc->pendown = true;
  161. }
  162. done:
  163. tps6507x_adc_standby(tsc);
  164. }
  165. static int tps6507x_ts_probe(struct platform_device *pdev)
  166. {
  167. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  168. const struct tps6507x_board *tps_board;
  169. const struct touchscreen_init_data *init_data;
  170. struct tps6507x_ts *tsc;
  171. struct input_polled_dev *poll_dev;
  172. struct input_dev *input_dev;
  173. int error;
  174. /*
  175. * tps_board points to pmic related constants
  176. * coming from the board-evm file.
  177. */
  178. tps_board = dev_get_platdata(tps6507x_dev->dev);
  179. if (!tps_board) {
  180. dev_err(tps6507x_dev->dev,
  181. "Could not find tps6507x platform data\n");
  182. return -ENODEV;
  183. }
  184. /*
  185. * init_data points to array of regulator_init structures
  186. * coming from the board-evm file.
  187. */
  188. init_data = tps_board->tps6507x_ts_init_data;
  189. tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
  190. if (!tsc) {
  191. dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
  192. return -ENOMEM;
  193. }
  194. tsc->mfd = tps6507x_dev;
  195. tsc->dev = tps6507x_dev->dev;
  196. tsc->min_pressure = init_data ?
  197. init_data->min_pressure : TPS_DEFAULT_MIN_PRESSURE;
  198. snprintf(tsc->phys, sizeof(tsc->phys),
  199. "%s/input0", dev_name(tsc->dev));
  200. poll_dev = input_allocate_polled_device();
  201. if (!poll_dev) {
  202. dev_err(tsc->dev, "Failed to allocate polled input device.\n");
  203. error = -ENOMEM;
  204. goto err_free_mem;
  205. }
  206. tsc->poll_dev = poll_dev;
  207. poll_dev->private = tsc;
  208. poll_dev->poll = tps6507x_ts_poll;
  209. poll_dev->poll_interval = init_data ?
  210. init_data->poll_period : TSC_DEFAULT_POLL_PERIOD;
  211. input_dev = poll_dev->input;
  212. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  213. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  214. input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
  215. input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
  216. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
  217. input_dev->name = "TPS6507x Touchscreen";
  218. input_dev->phys = tsc->phys;
  219. input_dev->dev.parent = tsc->dev;
  220. input_dev->id.bustype = BUS_I2C;
  221. if (init_data) {
  222. input_dev->id.vendor = init_data->vendor;
  223. input_dev->id.product = init_data->product;
  224. input_dev->id.version = init_data->version;
  225. }
  226. error = tps6507x_adc_standby(tsc);
  227. if (error)
  228. goto err_free_polled_dev;
  229. error = input_register_polled_device(poll_dev);
  230. if (error)
  231. goto err_free_polled_dev;
  232. platform_set_drvdata(pdev, tsc);
  233. return 0;
  234. err_free_polled_dev:
  235. input_free_polled_device(poll_dev);
  236. err_free_mem:
  237. kfree(tsc);
  238. return error;
  239. }
  240. static int tps6507x_ts_remove(struct platform_device *pdev)
  241. {
  242. struct tps6507x_ts *tsc = platform_get_drvdata(pdev);
  243. struct input_polled_dev *poll_dev = tsc->poll_dev;
  244. input_unregister_polled_device(poll_dev);
  245. input_free_polled_device(poll_dev);
  246. kfree(tsc);
  247. return 0;
  248. }
  249. static struct platform_driver tps6507x_ts_driver = {
  250. .driver = {
  251. .name = "tps6507x-ts",
  252. },
  253. .probe = tps6507x_ts_probe,
  254. .remove = tps6507x_ts_remove,
  255. };
  256. module_platform_driver(tps6507x_ts_driver);
  257. MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
  258. MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
  259. MODULE_LICENSE("GPL v2");
  260. MODULE_ALIAS("platform:tps6507x-ts");