tps6507x-ts.c 8.2 KB

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