tps6507x-ts.c 6.8 KB

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