navpoint.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Synaptics NavPoint (PXA27x SSP/SPI) driver.
  3. *
  4. * Copyright (C) 2012 Paul Parsons <lost.distance@yahoo.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio.h>
  16. #include <linux/input.h>
  17. #include <linux/input/navpoint.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mutex.h>
  20. #include <linux/pxa2xx_ssp.h>
  21. #include <linux/slab.h>
  22. /*
  23. * Synaptics Modular Embedded Protocol: Module Packet Format.
  24. * Module header byte 2:0 = Length (# bytes that follow)
  25. * Module header byte 4:3 = Control
  26. * Module header byte 7:5 = Module Address
  27. */
  28. #define HEADER_LENGTH(byte) ((byte) & 0x07)
  29. #define HEADER_CONTROL(byte) (((byte) >> 3) & 0x03)
  30. #define HEADER_ADDRESS(byte) ((byte) >> 5)
  31. struct navpoint {
  32. struct ssp_device *ssp;
  33. struct input_dev *input;
  34. struct device *dev;
  35. int gpio;
  36. int index;
  37. u8 data[1 + HEADER_LENGTH(0xff)];
  38. };
  39. /*
  40. * Initialization values for SSCR0_x, SSCR1_x, SSSR_x.
  41. */
  42. static const u32 sscr0 = 0
  43. | SSCR0_TUM /* TIM = 1; No TUR interrupts */
  44. | SSCR0_RIM /* RIM = 1; No ROR interrupts */
  45. | SSCR0_SSE /* SSE = 1; SSP enabled */
  46. | SSCR0_Motorola /* FRF = 0; Motorola SPI */
  47. | SSCR0_DataSize(16) /* DSS = 15; Data size = 16-bit */
  48. ;
  49. static const u32 sscr1 = 0
  50. | SSCR1_SCFR /* SCFR = 1; SSPSCLK only during transfers */
  51. | SSCR1_SCLKDIR /* SCLKDIR = 1; Slave mode */
  52. | SSCR1_SFRMDIR /* SFRMDIR = 1; Slave mode */
  53. | SSCR1_RWOT /* RWOT = 1; Receive without transmit mode */
  54. | SSCR1_RxTresh(1) /* RFT = 0; Receive FIFO threshold = 1 */
  55. | SSCR1_SPH /* SPH = 1; SSPSCLK inactive 0.5 + 1 cycles */
  56. | SSCR1_RIE /* RIE = 1; Receive FIFO interrupt enabled */
  57. ;
  58. static const u32 sssr = 0
  59. | SSSR_BCE /* BCE = 1; Clear BCE */
  60. | SSSR_TUR /* TUR = 1; Clear TUR */
  61. | SSSR_EOC /* EOC = 1; Clear EOC */
  62. | SSSR_TINT /* TINT = 1; Clear TINT */
  63. | SSSR_PINT /* PINT = 1; Clear PINT */
  64. | SSSR_ROR /* ROR = 1; Clear ROR */
  65. ;
  66. /*
  67. * MEP Query $22: Touchpad Coordinate Range Query is not supported by
  68. * the NavPoint module, so sampled values provide the default limits.
  69. */
  70. #define NAVPOINT_X_MIN 1278
  71. #define NAVPOINT_X_MAX 5340
  72. #define NAVPOINT_Y_MIN 1572
  73. #define NAVPOINT_Y_MAX 4396
  74. #define NAVPOINT_PRESSURE_MIN 0
  75. #define NAVPOINT_PRESSURE_MAX 255
  76. static void navpoint_packet(struct navpoint *navpoint)
  77. {
  78. int finger;
  79. int gesture;
  80. int x, y, z;
  81. switch (navpoint->data[0]) {
  82. case 0xff: /* Garbage (packet?) between reset and Hello packet */
  83. case 0x00: /* Module 0, NULL packet */
  84. break;
  85. case 0x0e: /* Module 0, Absolute packet */
  86. finger = (navpoint->data[1] & 0x01);
  87. gesture = (navpoint->data[1] & 0x02);
  88. x = ((navpoint->data[2] & 0x1f) << 8) | navpoint->data[3];
  89. y = ((navpoint->data[4] & 0x1f) << 8) | navpoint->data[5];
  90. z = navpoint->data[6];
  91. input_report_key(navpoint->input, BTN_TOUCH, finger);
  92. input_report_abs(navpoint->input, ABS_X, x);
  93. input_report_abs(navpoint->input, ABS_Y, y);
  94. input_report_abs(navpoint->input, ABS_PRESSURE, z);
  95. input_report_key(navpoint->input, BTN_TOOL_FINGER, finger);
  96. input_report_key(navpoint->input, BTN_LEFT, gesture);
  97. input_sync(navpoint->input);
  98. break;
  99. case 0x19: /* Module 0, Hello packet */
  100. if ((navpoint->data[1] & 0xf0) == 0x10)
  101. break;
  102. /* FALLTHROUGH */
  103. default:
  104. dev_warn(navpoint->dev,
  105. "spurious packet: data=0x%02x,0x%02x,...\n",
  106. navpoint->data[0], navpoint->data[1]);
  107. break;
  108. }
  109. }
  110. static irqreturn_t navpoint_irq(int irq, void *dev_id)
  111. {
  112. struct navpoint *navpoint = dev_id;
  113. struct ssp_device *ssp = navpoint->ssp;
  114. irqreturn_t ret = IRQ_NONE;
  115. u32 status;
  116. status = pxa_ssp_read_reg(ssp, SSSR);
  117. if (status & sssr) {
  118. dev_warn(navpoint->dev,
  119. "unexpected interrupt: status=0x%08x\n", status);
  120. pxa_ssp_write_reg(ssp, SSSR, (status & sssr));
  121. ret = IRQ_HANDLED;
  122. }
  123. while (status & SSSR_RNE) {
  124. u32 data;
  125. data = pxa_ssp_read_reg(ssp, SSDR);
  126. navpoint->data[navpoint->index + 0] = (data >> 8);
  127. navpoint->data[navpoint->index + 1] = data;
  128. navpoint->index += 2;
  129. if (HEADER_LENGTH(navpoint->data[0]) < navpoint->index) {
  130. navpoint_packet(navpoint);
  131. navpoint->index = 0;
  132. }
  133. status = pxa_ssp_read_reg(ssp, SSSR);
  134. ret = IRQ_HANDLED;
  135. }
  136. return ret;
  137. }
  138. static void navpoint_up(struct navpoint *navpoint)
  139. {
  140. struct ssp_device *ssp = navpoint->ssp;
  141. int timeout;
  142. clk_prepare_enable(ssp->clk);
  143. pxa_ssp_write_reg(ssp, SSCR1, sscr1);
  144. pxa_ssp_write_reg(ssp, SSSR, sssr);
  145. pxa_ssp_write_reg(ssp, SSTO, 0);
  146. pxa_ssp_write_reg(ssp, SSCR0, sscr0); /* SSCR0_SSE written last */
  147. /* Wait until SSP port is ready for slave clock operations */
  148. for (timeout = 100; timeout != 0; --timeout) {
  149. if (!(pxa_ssp_read_reg(ssp, SSSR) & SSSR_CSS))
  150. break;
  151. msleep(1);
  152. }
  153. if (timeout == 0)
  154. dev_err(navpoint->dev,
  155. "timeout waiting for SSSR[CSS] to clear\n");
  156. if (gpio_is_valid(navpoint->gpio))
  157. gpio_set_value(navpoint->gpio, 1);
  158. }
  159. static void navpoint_down(struct navpoint *navpoint)
  160. {
  161. struct ssp_device *ssp = navpoint->ssp;
  162. if (gpio_is_valid(navpoint->gpio))
  163. gpio_set_value(navpoint->gpio, 0);
  164. pxa_ssp_write_reg(ssp, SSCR0, 0);
  165. clk_disable_unprepare(ssp->clk);
  166. }
  167. static int navpoint_open(struct input_dev *input)
  168. {
  169. struct navpoint *navpoint = input_get_drvdata(input);
  170. navpoint_up(navpoint);
  171. return 0;
  172. }
  173. static void navpoint_close(struct input_dev *input)
  174. {
  175. struct navpoint *navpoint = input_get_drvdata(input);
  176. navpoint_down(navpoint);
  177. }
  178. static int navpoint_probe(struct platform_device *pdev)
  179. {
  180. const struct navpoint_platform_data *pdata =
  181. dev_get_platdata(&pdev->dev);
  182. struct ssp_device *ssp;
  183. struct input_dev *input;
  184. struct navpoint *navpoint;
  185. int error;
  186. if (!pdata) {
  187. dev_err(&pdev->dev, "no platform data\n");
  188. return -EINVAL;
  189. }
  190. if (gpio_is_valid(pdata->gpio)) {
  191. error = gpio_request_one(pdata->gpio, GPIOF_OUT_INIT_LOW,
  192. "SYNAPTICS_ON");
  193. if (error)
  194. return error;
  195. }
  196. ssp = pxa_ssp_request(pdata->port, pdev->name);
  197. if (!ssp) {
  198. error = -ENODEV;
  199. goto err_free_gpio;
  200. }
  201. /* HaRET does not disable devices before jumping into Linux */
  202. if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
  203. pxa_ssp_write_reg(ssp, SSCR0, 0);
  204. dev_warn(&pdev->dev, "ssp%d already enabled\n", pdata->port);
  205. }
  206. navpoint = kzalloc(sizeof(*navpoint), GFP_KERNEL);
  207. input = input_allocate_device();
  208. if (!navpoint || !input) {
  209. error = -ENOMEM;
  210. goto err_free_mem;
  211. }
  212. navpoint->ssp = ssp;
  213. navpoint->input = input;
  214. navpoint->dev = &pdev->dev;
  215. navpoint->gpio = pdata->gpio;
  216. input->name = pdev->name;
  217. input->dev.parent = &pdev->dev;
  218. __set_bit(EV_KEY, input->evbit);
  219. __set_bit(EV_ABS, input->evbit);
  220. __set_bit(BTN_LEFT, input->keybit);
  221. __set_bit(BTN_TOUCH, input->keybit);
  222. __set_bit(BTN_TOOL_FINGER, input->keybit);
  223. input_set_abs_params(input, ABS_X,
  224. NAVPOINT_X_MIN, NAVPOINT_X_MAX, 0, 0);
  225. input_set_abs_params(input, ABS_Y,
  226. NAVPOINT_Y_MIN, NAVPOINT_Y_MAX, 0, 0);
  227. input_set_abs_params(input, ABS_PRESSURE,
  228. NAVPOINT_PRESSURE_MIN, NAVPOINT_PRESSURE_MAX,
  229. 0, 0);
  230. input->open = navpoint_open;
  231. input->close = navpoint_close;
  232. input_set_drvdata(input, navpoint);
  233. error = request_irq(ssp->irq, navpoint_irq, 0, pdev->name, navpoint);
  234. if (error)
  235. goto err_free_mem;
  236. error = input_register_device(input);
  237. if (error)
  238. goto err_free_irq;
  239. platform_set_drvdata(pdev, navpoint);
  240. dev_dbg(&pdev->dev, "ssp%d, irq %d\n", pdata->port, ssp->irq);
  241. return 0;
  242. err_free_irq:
  243. free_irq(ssp->irq, navpoint);
  244. err_free_mem:
  245. input_free_device(input);
  246. kfree(navpoint);
  247. pxa_ssp_free(ssp);
  248. err_free_gpio:
  249. if (gpio_is_valid(pdata->gpio))
  250. gpio_free(pdata->gpio);
  251. return error;
  252. }
  253. static int navpoint_remove(struct platform_device *pdev)
  254. {
  255. const struct navpoint_platform_data *pdata =
  256. dev_get_platdata(&pdev->dev);
  257. struct navpoint *navpoint = platform_get_drvdata(pdev);
  258. struct ssp_device *ssp = navpoint->ssp;
  259. free_irq(ssp->irq, navpoint);
  260. input_unregister_device(navpoint->input);
  261. kfree(navpoint);
  262. pxa_ssp_free(ssp);
  263. if (gpio_is_valid(pdata->gpio))
  264. gpio_free(pdata->gpio);
  265. return 0;
  266. }
  267. static int __maybe_unused navpoint_suspend(struct device *dev)
  268. {
  269. struct platform_device *pdev = to_platform_device(dev);
  270. struct navpoint *navpoint = platform_get_drvdata(pdev);
  271. struct input_dev *input = navpoint->input;
  272. mutex_lock(&input->mutex);
  273. if (input->users)
  274. navpoint_down(navpoint);
  275. mutex_unlock(&input->mutex);
  276. return 0;
  277. }
  278. static int __maybe_unused navpoint_resume(struct device *dev)
  279. {
  280. struct platform_device *pdev = to_platform_device(dev);
  281. struct navpoint *navpoint = platform_get_drvdata(pdev);
  282. struct input_dev *input = navpoint->input;
  283. mutex_lock(&input->mutex);
  284. if (input->users)
  285. navpoint_up(navpoint);
  286. mutex_unlock(&input->mutex);
  287. return 0;
  288. }
  289. static SIMPLE_DEV_PM_OPS(navpoint_pm_ops, navpoint_suspend, navpoint_resume);
  290. static struct platform_driver navpoint_driver = {
  291. .probe = navpoint_probe,
  292. .remove = navpoint_remove,
  293. .driver = {
  294. .name = "navpoint",
  295. .pm = &navpoint_pm_ops,
  296. },
  297. };
  298. module_platform_driver(navpoint_driver);
  299. MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
  300. MODULE_DESCRIPTION("Synaptics NavPoint (PXA27x SSP/SPI) driver");
  301. MODULE_LICENSE("GPL");
  302. MODULE_ALIAS("platform:navpoint");