tps6507x-ts.c 8.4 KB

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