tps6507x-ts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Touchscreen driver for the tps6507x chip.
  3. *
  4. * Copyright (c) 2009 RidgeRun ([email protected])
  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. s32 loops = 0;
  102. u8 val;
  103. ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
  104. TPS6507X_ADCONFIG_INPUT_TSC);
  105. if (ret)
  106. return ret;
  107. ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
  108. TPS6507X_TSCMODE_STANDBY);
  109. if (ret)
  110. return ret;
  111. ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
  112. if (ret)
  113. return ret;
  114. while (val & TPS6507X_REG_TSC_INT) {
  115. mdelay(10);
  116. ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
  117. if (ret)
  118. return ret;
  119. loops++;
  120. }
  121. return ret;
  122. }
  123. static void tps6507x_ts_poll(struct input_dev *input_dev)
  124. {
  125. struct tps6507x_ts *tsc = input_get_drvdata(input_dev);
  126. bool pendown;
  127. s32 ret;
  128. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
  129. &tsc->tc.pressure);
  130. if (ret)
  131. goto done;
  132. pendown = tsc->tc.pressure > tsc->min_pressure;
  133. if (unlikely(!pendown && tsc->pendown)) {
  134. dev_dbg(tsc->dev, "UP\n");
  135. input_report_key(input_dev, BTN_TOUCH, 0);
  136. input_report_abs(input_dev, ABS_PRESSURE, 0);
  137. input_sync(input_dev);
  138. tsc->pendown = false;
  139. }
  140. if (pendown) {
  141. if (!tsc->pendown) {
  142. dev_dbg(tsc->dev, "DOWN\n");
  143. input_report_key(input_dev, BTN_TOUCH, 1);
  144. } else
  145. dev_dbg(tsc->dev, "still down\n");
  146. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
  147. &tsc->tc.x);
  148. if (ret)
  149. goto done;
  150. ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
  151. &tsc->tc.y);
  152. if (ret)
  153. goto done;
  154. input_report_abs(input_dev, ABS_X, tsc->tc.x);
  155. input_report_abs(input_dev, ABS_Y, tsc->tc.y);
  156. input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
  157. input_sync(input_dev);
  158. tsc->pendown = true;
  159. }
  160. done:
  161. tps6507x_adc_standby(tsc);
  162. }
  163. static int tps6507x_ts_probe(struct platform_device *pdev)
  164. {
  165. struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
  166. const struct tps6507x_board *tps_board;
  167. const struct touchscreen_init_data *init_data;
  168. struct tps6507x_ts *tsc;
  169. struct input_dev *input_dev;
  170. int error;
  171. /*
  172. * tps_board points to pmic related constants
  173. * coming from the board-evm file.
  174. */
  175. tps_board = dev_get_platdata(tps6507x_dev->dev);
  176. if (!tps_board) {
  177. dev_err(tps6507x_dev->dev,
  178. "Could not find tps6507x platform data\n");
  179. return -ENODEV;
  180. }
  181. /*
  182. * init_data points to array of regulator_init structures
  183. * coming from the board-evm file.
  184. */
  185. init_data = tps_board->tps6507x_ts_init_data;
  186. tsc = devm_kzalloc(&pdev->dev, sizeof(struct tps6507x_ts), GFP_KERNEL);
  187. if (!tsc) {
  188. dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
  189. return -ENOMEM;
  190. }
  191. tsc->mfd = tps6507x_dev;
  192. tsc->dev = tps6507x_dev->dev;
  193. tsc->min_pressure = init_data ?
  194. init_data->min_pressure : TPS_DEFAULT_MIN_PRESSURE;
  195. snprintf(tsc->phys, sizeof(tsc->phys),
  196. "%s/input0", dev_name(tsc->dev));
  197. input_dev = devm_input_allocate_device(&pdev->dev);
  198. if (!input_dev) {
  199. dev_err(tsc->dev, "Failed to allocate polled input device.\n");
  200. return -ENOMEM;
  201. }
  202. tsc->input = input_dev;
  203. input_set_drvdata(input_dev, tsc);
  204. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  205. input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
  206. input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
  207. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
  208. input_dev->name = "TPS6507x Touchscreen";
  209. input_dev->phys = tsc->phys;
  210. input_dev->dev.parent = tsc->dev;
  211. input_dev->id.bustype = BUS_I2C;
  212. if (init_data) {
  213. input_dev->id.vendor = init_data->vendor;
  214. input_dev->id.product = init_data->product;
  215. input_dev->id.version = init_data->version;
  216. }
  217. error = tps6507x_adc_standby(tsc);
  218. if (error)
  219. return error;
  220. error = input_setup_polling(input_dev, tps6507x_ts_poll);
  221. if (error)
  222. return error;
  223. input_set_poll_interval(input_dev,
  224. init_data ? init_data->poll_period :
  225. TSC_DEFAULT_POLL_PERIOD);
  226. error = input_register_device(input_dev);
  227. if (error)
  228. return error;
  229. return 0;
  230. }
  231. static struct platform_driver tps6507x_ts_driver = {
  232. .driver = {
  233. .name = "tps6507x-ts",
  234. },
  235. .probe = tps6507x_ts_probe,
  236. };
  237. module_platform_driver(tps6507x_ts_driver);
  238. MODULE_AUTHOR("Todd Fischer <[email protected]>");
  239. MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
  240. MODULE_LICENSE("GPL v2");
  241. MODULE_ALIAS("platform:tps6507x-ts");