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