tps6507x-ts.c 7.5 KB

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