tps6507x-ts.c 8.9 KB

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