psxpad-spi.c 11 KB

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