psxpad-spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * PlayStation 1/2 joypads via SPI interface Driver
  3. *
  4. * Copyright (C) 2017 Tomohiro Yoshidomi <sylph23k@gmail.com>
  5. * Licensed under the GPL-2 or later.
  6. *
  7. * PlayStation 1/2 joypad's plug (not socket)
  8. * 123 456 789
  9. * (...|...|...)
  10. *
  11. * 1: DAT -> MISO (pullup with 1k owm to 3.3V)
  12. * 2: CMD -> MOSI
  13. * 3: 9V (for motor, if not use N.C.)
  14. * 4: GND
  15. * 5: 3.3V
  16. * 6: Attention -> CS(SS)
  17. * 7: SCK -> SCK
  18. * 8: N.C.
  19. * 9: ACK -> N.C.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/device.h>
  23. #include <linux/input.h>
  24. #include <linux/input-polldev.h>
  25. #include <linux/module.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/types.h>
  28. #include <linux/pm.h>
  29. #include <linux/pm_runtime.h>
  30. #define REVERSE_BIT(x) ((((x) & 0x80) >> 7) | (((x) & 0x40) >> 5) | \
  31. (((x) & 0x20) >> 3) | (((x) & 0x10) >> 1) | (((x) & 0x08) << 1) | \
  32. (((x) & 0x04) << 3) | (((x) & 0x02) << 5) | (((x) & 0x01) << 7))
  33. /* PlayStation 1/2 joypad command and response are LSBFIRST. */
  34. /*
  35. * 0x01, 0x42, 0x00, 0x00, 0x00,
  36. * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  38. */
  39. static const u8 PSX_CMD_POLL[] = {
  40. 0x80, 0x42, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  43. };
  44. /* 0x01, 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 */
  45. static const u8 PSX_CMD_ENTER_CFG[] = {
  46. 0x80, 0xC2, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
  47. };
  48. /* 0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A */
  49. static const u8 PSX_CMD_EXIT_CFG[] = {
  50. 0x80, 0xC2, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A
  51. };
  52. /* 0x01, 0x4D, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF */
  53. static const u8 PSX_CMD_ENABLE_MOTOR[] = {
  54. 0x80, 0xB2, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF
  55. };
  56. struct psxpad {
  57. struct spi_device *spi;
  58. struct input_polled_dev *pdev;
  59. char phys[0x20];
  60. bool motor1enable;
  61. bool motor2enable;
  62. u8 motor1level;
  63. u8 motor2level;
  64. u8 sendbuf[0x20] ____cacheline_aligned;
  65. u8 response[sizeof(PSX_CMD_POLL)] ____cacheline_aligned;
  66. };
  67. static int psxpad_command(struct psxpad *pad, const u8 sendcmdlen)
  68. {
  69. struct spi_transfer xfers = {
  70. .tx_buf = pad->sendbuf,
  71. .rx_buf = pad->response,
  72. .len = sendcmdlen,
  73. };
  74. int err;
  75. err = spi_sync_transfer(pad->spi, &xfers, 1);
  76. if (err) {
  77. dev_err(&pad->spi->dev,
  78. "%s: failed to SPI xfers mode: %d\n",
  79. __func__, err);
  80. return err;
  81. }
  82. return 0;
  83. }
  84. #ifdef CONFIG_JOYSTICK_PSXPAD_SPI_FF
  85. static void psxpad_control_motor(struct psxpad *pad,
  86. bool motor1enable, bool motor2enable)
  87. {
  88. int err;
  89. pad->motor1enable = motor1enable;
  90. pad->motor2enable = motor2enable;
  91. memcpy(pad->sendbuf, PSX_CMD_ENTER_CFG, sizeof(PSX_CMD_ENTER_CFG));
  92. err = psxpad_command(pad, sizeof(PSX_CMD_ENTER_CFG));
  93. if (err) {
  94. dev_err(&pad->spi->dev,
  95. "%s: failed to enter config mode: %d\n",
  96. __func__, err);
  97. return;
  98. }
  99. memcpy(pad->sendbuf, PSX_CMD_ENABLE_MOTOR,
  100. sizeof(PSX_CMD_ENABLE_MOTOR));
  101. pad->sendbuf[3] = pad->motor1enable ? 0x00 : 0xFF;
  102. pad->sendbuf[4] = pad->motor2enable ? 0x80 : 0xFF;
  103. err = psxpad_command(pad, sizeof(PSX_CMD_ENABLE_MOTOR));
  104. if (err) {
  105. dev_err(&pad->spi->dev,
  106. "%s: failed to enable motor mode: %d\n",
  107. __func__, err);
  108. return;
  109. }
  110. memcpy(pad->sendbuf, PSX_CMD_EXIT_CFG, sizeof(PSX_CMD_EXIT_CFG));
  111. err = psxpad_command(pad, sizeof(PSX_CMD_EXIT_CFG));
  112. if (err) {
  113. dev_err(&pad->spi->dev,
  114. "%s: failed to exit config mode: %d\n",
  115. __func__, err);
  116. return;
  117. }
  118. }
  119. static void psxpad_set_motor_level(struct psxpad *pad,
  120. u8 motor1level, u8 motor2level)
  121. {
  122. pad->motor1level = motor1level ? 0xFF : 0x00;
  123. pad->motor2level = REVERSE_BIT(motor2level);
  124. }
  125. static int psxpad_spi_play_effect(struct input_dev *idev,
  126. void *data, struct ff_effect *effect)
  127. {
  128. struct input_polled_dev *pdev = input_get_drvdata(idev);
  129. struct psxpad *pad = pdev->private;
  130. switch (effect->type) {
  131. case FF_RUMBLE:
  132. psxpad_set_motor_level(pad,
  133. (effect->u.rumble.weak_magnitude >> 8) & 0xFFU,
  134. (effect->u.rumble.strong_magnitude >> 8) & 0xFFU);
  135. break;
  136. }
  137. return 0;
  138. }
  139. static int psxpad_spi_init_ff(struct psxpad *pad)
  140. {
  141. int err;
  142. input_set_capability(pad->pdev->input, EV_FF, FF_RUMBLE);
  143. err = input_ff_create_memless(pad->pdev->input, NULL,
  144. psxpad_spi_play_effect);
  145. if (err) {
  146. dev_err(&pad->spi->dev,
  147. "input_ff_create_memless() failed: %d\n", err);
  148. return err;
  149. }
  150. return 0;
  151. }
  152. #else /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
  153. static void psxpad_control_motor(struct psxpad *pad,
  154. bool motor1enable, bool motor2enable)
  155. {
  156. }
  157. static void psxpad_set_motor_level(struct psxpad *pad,
  158. u8 motor1level, u8 motor2level)
  159. {
  160. }
  161. static inline int psxpad_spi_init_ff(struct psxpad *pad)
  162. {
  163. return 0;
  164. }
  165. #endif /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
  166. static void psxpad_spi_poll_open(struct input_polled_dev *pdev)
  167. {
  168. struct psxpad *pad = pdev->private;
  169. pm_runtime_get_sync(&pad->spi->dev);
  170. }
  171. static void psxpad_spi_poll_close(struct input_polled_dev *pdev)
  172. {
  173. struct psxpad *pad = pdev->private;
  174. pm_runtime_put_sync(&pad->spi->dev);
  175. }
  176. static void psxpad_spi_poll(struct input_polled_dev *pdev)
  177. {
  178. struct psxpad *pad = pdev->private;
  179. struct input_dev *input = pdev->input;
  180. u8 b_rsp3, b_rsp4;
  181. int err;
  182. psxpad_control_motor(pad, true, true);
  183. memcpy(pad->sendbuf, PSX_CMD_POLL, sizeof(PSX_CMD_POLL));
  184. pad->sendbuf[3] = pad->motor1enable ? pad->motor1level : 0x00;
  185. pad->sendbuf[4] = pad->motor2enable ? pad->motor2level : 0x00;
  186. err = psxpad_command(pad, sizeof(PSX_CMD_POLL));
  187. if (err) {
  188. dev_err(&pad->spi->dev,
  189. "%s: poll command failed mode: %d\n", __func__, err);
  190. return;
  191. }
  192. switch (pad->response[1]) {
  193. case 0xCE: /* 0x73 : analog 1 */
  194. /* button data is inverted */
  195. b_rsp3 = ~pad->response[3];
  196. b_rsp4 = ~pad->response[4];
  197. input_report_abs(input, ABS_X, REVERSE_BIT(pad->response[7]));
  198. input_report_abs(input, ABS_Y, REVERSE_BIT(pad->response[8]));
  199. input_report_abs(input, ABS_RX, REVERSE_BIT(pad->response[5]));
  200. input_report_abs(input, ABS_RY, REVERSE_BIT(pad->response[6]));
  201. input_report_key(input, BTN_DPAD_UP, b_rsp3 & BIT(3));
  202. input_report_key(input, BTN_DPAD_DOWN, b_rsp3 & BIT(1));
  203. input_report_key(input, BTN_DPAD_LEFT, b_rsp3 & BIT(0));
  204. input_report_key(input, BTN_DPAD_RIGHT, b_rsp3 & BIT(2));
  205. input_report_key(input, BTN_X, b_rsp4 & BIT(3));
  206. input_report_key(input, BTN_A, b_rsp4 & BIT(2));
  207. input_report_key(input, BTN_B, b_rsp4 & BIT(1));
  208. input_report_key(input, BTN_Y, b_rsp4 & BIT(0));
  209. input_report_key(input, BTN_TL, b_rsp4 & BIT(5));
  210. input_report_key(input, BTN_TR, b_rsp4 & BIT(4));
  211. input_report_key(input, BTN_TL2, b_rsp4 & BIT(7));
  212. input_report_key(input, BTN_TR2, b_rsp4 & BIT(6));
  213. input_report_key(input, BTN_THUMBL, b_rsp3 & BIT(6));
  214. input_report_key(input, BTN_THUMBR, b_rsp3 & BIT(5));
  215. input_report_key(input, BTN_SELECT, b_rsp3 & BIT(7));
  216. input_report_key(input, BTN_START, b_rsp3 & BIT(4));
  217. break;
  218. case 0x82: /* 0x41 : digital */
  219. /* button data is inverted */
  220. b_rsp3 = ~pad->response[3];
  221. b_rsp4 = ~pad->response[4];
  222. input_report_abs(input, ABS_X, 0x80);
  223. input_report_abs(input, ABS_Y, 0x80);
  224. input_report_abs(input, ABS_RX, 0x80);
  225. input_report_abs(input, ABS_RY, 0x80);
  226. input_report_key(input, BTN_DPAD_UP, b_rsp3 & BIT(3));
  227. input_report_key(input, BTN_DPAD_DOWN, b_rsp3 & BIT(1));
  228. input_report_key(input, BTN_DPAD_LEFT, b_rsp3 & BIT(0));
  229. input_report_key(input, BTN_DPAD_RIGHT, b_rsp3 & BIT(2));
  230. input_report_key(input, BTN_X, b_rsp4 & BIT(3));
  231. input_report_key(input, BTN_A, b_rsp4 & BIT(2));
  232. input_report_key(input, BTN_B, b_rsp4 & BIT(1));
  233. input_report_key(input, BTN_Y, b_rsp4 & BIT(0));
  234. input_report_key(input, BTN_TL, b_rsp4 & BIT(5));
  235. input_report_key(input, BTN_TR, b_rsp4 & BIT(4));
  236. input_report_key(input, BTN_TL2, b_rsp4 & BIT(7));
  237. input_report_key(input, BTN_TR2, b_rsp4 & BIT(6));
  238. input_report_key(input, BTN_THUMBL, false);
  239. input_report_key(input, BTN_THUMBR, false);
  240. input_report_key(input, BTN_SELECT, b_rsp3 & BIT(7));
  241. input_report_key(input, BTN_START, b_rsp3 & BIT(4));
  242. break;
  243. }
  244. input_sync(input);
  245. }
  246. static int psxpad_spi_probe(struct spi_device *spi)
  247. {
  248. struct psxpad *pad;
  249. struct input_polled_dev *pdev;
  250. struct input_dev *idev;
  251. int err;
  252. pad = devm_kzalloc(&spi->dev, sizeof(struct psxpad), GFP_KERNEL);
  253. if (!pad)
  254. return -ENOMEM;
  255. pdev = devm_input_allocate_polled_device(&spi->dev);
  256. if (!pdev) {
  257. dev_err(&spi->dev, "failed to allocate input device\n");
  258. return -ENOMEM;
  259. }
  260. /* input poll device settings */
  261. pad->pdev = pdev;
  262. pad->spi = spi;
  263. pdev->private = pad;
  264. pdev->open = psxpad_spi_poll_open;
  265. pdev->close = psxpad_spi_poll_close;
  266. pdev->poll = psxpad_spi_poll;
  267. /* poll interval is about 60fps */
  268. pdev->poll_interval = 16;
  269. pdev->poll_interval_min = 8;
  270. pdev->poll_interval_max = 32;
  271. /* input device settings */
  272. idev = pdev->input;
  273. idev->name = "PlayStation 1/2 joypad";
  274. snprintf(pad->phys, sizeof(pad->phys), "%s/input", dev_name(&spi->dev));
  275. idev->id.bustype = BUS_SPI;
  276. /* key/value map settings */
  277. input_set_abs_params(idev, ABS_X, 0, 255, 0, 0);
  278. input_set_abs_params(idev, ABS_Y, 0, 255, 0, 0);
  279. input_set_abs_params(idev, ABS_RX, 0, 255, 0, 0);
  280. input_set_abs_params(idev, ABS_RY, 0, 255, 0, 0);
  281. input_set_capability(idev, EV_KEY, BTN_DPAD_UP);
  282. input_set_capability(idev, EV_KEY, BTN_DPAD_DOWN);
  283. input_set_capability(idev, EV_KEY, BTN_DPAD_LEFT);
  284. input_set_capability(idev, EV_KEY, BTN_DPAD_RIGHT);
  285. input_set_capability(idev, EV_KEY, BTN_A);
  286. input_set_capability(idev, EV_KEY, BTN_B);
  287. input_set_capability(idev, EV_KEY, BTN_X);
  288. input_set_capability(idev, EV_KEY, BTN_Y);
  289. input_set_capability(idev, EV_KEY, BTN_TL);
  290. input_set_capability(idev, EV_KEY, BTN_TR);
  291. input_set_capability(idev, EV_KEY, BTN_TL2);
  292. input_set_capability(idev, EV_KEY, BTN_TR2);
  293. input_set_capability(idev, EV_KEY, BTN_THUMBL);
  294. input_set_capability(idev, EV_KEY, BTN_THUMBR);
  295. input_set_capability(idev, EV_KEY, BTN_SELECT);
  296. input_set_capability(idev, EV_KEY, BTN_START);
  297. err = psxpad_spi_init_ff(pad);
  298. if (err)
  299. return err;
  300. /* SPI settings */
  301. spi->mode = SPI_MODE_3;
  302. spi->bits_per_word = 8;
  303. /* (PlayStation 1/2 joypad might be possible works 250kHz/500kHz) */
  304. spi->master->min_speed_hz = 125000;
  305. spi->master->max_speed_hz = 125000;
  306. spi_setup(spi);
  307. /* pad settings */
  308. psxpad_set_motor_level(pad, 0, 0);
  309. /* register input poll device */
  310. err = input_register_polled_device(pdev);
  311. if (err) {
  312. dev_err(&spi->dev,
  313. "failed to register input poll device: %d\n", err);
  314. return err;
  315. }
  316. pm_runtime_enable(&spi->dev);
  317. return 0;
  318. }
  319. static int __maybe_unused psxpad_spi_suspend(struct device *dev)
  320. {
  321. struct spi_device *spi = to_spi_device(dev);
  322. struct psxpad *pad = spi_get_drvdata(spi);
  323. psxpad_set_motor_level(pad, 0, 0);
  324. return 0;
  325. }
  326. static SIMPLE_DEV_PM_OPS(psxpad_spi_pm, psxpad_spi_suspend, NULL);
  327. static const struct spi_device_id psxpad_spi_id[] = {
  328. { "psxpad-spi", 0 },
  329. { }
  330. };
  331. MODULE_DEVICE_TABLE(spi, psxpad_spi_id);
  332. static struct spi_driver psxpad_spi_driver = {
  333. .driver = {
  334. .name = "psxpad-spi",
  335. .pm = &psxpad_spi_pm,
  336. },
  337. .id_table = psxpad_spi_id,
  338. .probe = psxpad_spi_probe,
  339. };
  340. module_spi_driver(psxpad_spi_driver);
  341. MODULE_AUTHOR("Tomohiro Yoshidomi <sylph23k@gmail.com>");
  342. MODULE_DESCRIPTION("PlayStation 1/2 joypads via SPI interface Driver");
  343. MODULE_LICENSE("GPL");