tps6507x-ts.c 8.7 KB

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