psxpad-spi.c 11 KB

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