Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * GPIO based serio bus driver for bit banging the PS/2 protocol
0004  *
0005  * Author: Danilo Krummrich <danilokrummrich@dk-develop.de>
0006  */
0007 
0008 #include <linux/gpio/consumer.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/module.h>
0011 #include <linux/serio.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/completion.h>
0016 #include <linux/mutex.h>
0017 #include <linux/preempt.h>
0018 #include <linux/property.h>
0019 #include <linux/of.h>
0020 #include <linux/jiffies.h>
0021 #include <linux/delay.h>
0022 #include <linux/timekeeping.h>
0023 
0024 #define DRIVER_NAME     "ps2-gpio"
0025 
0026 #define PS2_MODE_RX     0
0027 #define PS2_MODE_TX     1
0028 
0029 #define PS2_START_BIT       0
0030 #define PS2_DATA_BIT0       1
0031 #define PS2_DATA_BIT1       2
0032 #define PS2_DATA_BIT2       3
0033 #define PS2_DATA_BIT3       4
0034 #define PS2_DATA_BIT4       5
0035 #define PS2_DATA_BIT5       6
0036 #define PS2_DATA_BIT6       7
0037 #define PS2_DATA_BIT7       8
0038 #define PS2_PARITY_BIT      9
0039 #define PS2_STOP_BIT        10
0040 #define PS2_ACK_BIT     11
0041 
0042 #define PS2_DEV_RET_ACK     0xfa
0043 #define PS2_DEV_RET_NACK    0xfe
0044 
0045 #define PS2_CMD_RESEND      0xfe
0046 
0047 /*
0048  * The PS2 protocol specifies a clock frequency between 10kHz and 16.7kHz,
0049  * therefore the maximal interrupt interval should be 100us and the minimum
0050  * interrupt interval should be ~60us. Let's allow +/- 20us for frequency
0051  * deviations and interrupt latency.
0052  *
0053  * The data line must be samples after ~30us to 50us after the falling edge,
0054  * since the device updates the data line at the rising edge.
0055  *
0056  * ___            ______            ______            ______            ___
0057  *    \          /      \          /      \          /      \          /
0058  *     \        /        \        /        \        /        \        /
0059  *      \______/          \______/          \______/          \______/
0060  *
0061  *     |-----------------|                 |--------|
0062  *          60us/100us                      30us/50us
0063  */
0064 #define PS2_CLK_FREQ_MIN_HZ     10000
0065 #define PS2_CLK_FREQ_MAX_HZ     16700
0066 #define PS2_CLK_MIN_INTERVAL_US     ((1000 * 1000) / PS2_CLK_FREQ_MAX_HZ)
0067 #define PS2_CLK_MAX_INTERVAL_US     ((1000 * 1000) / PS2_CLK_FREQ_MIN_HZ)
0068 #define PS2_IRQ_MIN_INTERVAL_US     (PS2_CLK_MIN_INTERVAL_US - 20)
0069 #define PS2_IRQ_MAX_INTERVAL_US     (PS2_CLK_MAX_INTERVAL_US + 20)
0070 
0071 struct ps2_gpio_data {
0072     struct device *dev;
0073     struct serio *serio;
0074     unsigned char mode;
0075     struct gpio_desc *gpio_clk;
0076     struct gpio_desc *gpio_data;
0077     bool write_enable;
0078     int irq;
0079     ktime_t t_irq_now;
0080     ktime_t t_irq_last;
0081     struct {
0082         unsigned char cnt;
0083         unsigned char byte;
0084     } rx;
0085     struct {
0086         unsigned char cnt;
0087         unsigned char byte;
0088         ktime_t t_xfer_start;
0089         ktime_t t_xfer_end;
0090         struct completion complete;
0091         struct mutex mutex;
0092         struct delayed_work work;
0093     } tx;
0094 };
0095 
0096 static int ps2_gpio_open(struct serio *serio)
0097 {
0098     struct ps2_gpio_data *drvdata = serio->port_data;
0099 
0100     drvdata->t_irq_last = 0;
0101     drvdata->tx.t_xfer_end = 0;
0102 
0103     enable_irq(drvdata->irq);
0104     return 0;
0105 }
0106 
0107 static void ps2_gpio_close(struct serio *serio)
0108 {
0109     struct ps2_gpio_data *drvdata = serio->port_data;
0110 
0111     flush_delayed_work(&drvdata->tx.work);
0112     disable_irq(drvdata->irq);
0113 }
0114 
0115 static int __ps2_gpio_write(struct serio *serio, unsigned char val)
0116 {
0117     struct ps2_gpio_data *drvdata = serio->port_data;
0118 
0119     disable_irq_nosync(drvdata->irq);
0120     gpiod_direction_output(drvdata->gpio_clk, 0);
0121 
0122     drvdata->mode = PS2_MODE_TX;
0123     drvdata->tx.byte = val;
0124 
0125     schedule_delayed_work(&drvdata->tx.work, usecs_to_jiffies(200));
0126 
0127     return 0;
0128 }
0129 
0130 static int ps2_gpio_write(struct serio *serio, unsigned char val)
0131 {
0132     struct ps2_gpio_data *drvdata = serio->port_data;
0133     int ret = 0;
0134 
0135     if (in_task()) {
0136         mutex_lock(&drvdata->tx.mutex);
0137         __ps2_gpio_write(serio, val);
0138         if (!wait_for_completion_timeout(&drvdata->tx.complete,
0139                          msecs_to_jiffies(10000)))
0140             ret = SERIO_TIMEOUT;
0141         mutex_unlock(&drvdata->tx.mutex);
0142     } else {
0143         __ps2_gpio_write(serio, val);
0144     }
0145 
0146     return ret;
0147 }
0148 
0149 static void ps2_gpio_tx_work_fn(struct work_struct *work)
0150 {
0151     struct delayed_work *dwork = to_delayed_work(work);
0152     struct ps2_gpio_data *drvdata = container_of(dwork,
0153                              struct ps2_gpio_data,
0154                              tx.work);
0155 
0156     drvdata->tx.t_xfer_start = ktime_get();
0157     enable_irq(drvdata->irq);
0158     gpiod_direction_output(drvdata->gpio_data, 0);
0159     gpiod_direction_input(drvdata->gpio_clk);
0160 }
0161 
0162 static irqreturn_t ps2_gpio_irq_rx(struct ps2_gpio_data *drvdata)
0163 {
0164     unsigned char byte, cnt;
0165     int data;
0166     int rxflags = 0;
0167     s64 us_delta;
0168 
0169     byte = drvdata->rx.byte;
0170     cnt = drvdata->rx.cnt;
0171 
0172     drvdata->t_irq_now = ktime_get();
0173 
0174     /*
0175      * We need to consider spurious interrupts happening right after
0176      * a TX xfer finished.
0177      */
0178     us_delta = ktime_us_delta(drvdata->t_irq_now, drvdata->tx.t_xfer_end);
0179     if (unlikely(us_delta < PS2_IRQ_MIN_INTERVAL_US))
0180         goto end;
0181 
0182     us_delta = ktime_us_delta(drvdata->t_irq_now, drvdata->t_irq_last);
0183     if (us_delta > PS2_IRQ_MAX_INTERVAL_US && cnt) {
0184         dev_err(drvdata->dev,
0185             "RX: timeout, probably we missed an interrupt\n");
0186         goto err;
0187     } else if (unlikely(us_delta < PS2_IRQ_MIN_INTERVAL_US)) {
0188         /* Ignore spurious IRQs. */
0189         goto end;
0190     }
0191     drvdata->t_irq_last = drvdata->t_irq_now;
0192 
0193     data = gpiod_get_value(drvdata->gpio_data);
0194     if (unlikely(data < 0)) {
0195         dev_err(drvdata->dev, "RX: failed to get data gpio val: %d\n",
0196             data);
0197         goto err;
0198     }
0199 
0200     switch (cnt) {
0201     case PS2_START_BIT:
0202         /* start bit should be low */
0203         if (unlikely(data)) {
0204             dev_err(drvdata->dev, "RX: start bit should be low\n");
0205             goto err;
0206         }
0207         break;
0208     case PS2_DATA_BIT0:
0209     case PS2_DATA_BIT1:
0210     case PS2_DATA_BIT2:
0211     case PS2_DATA_BIT3:
0212     case PS2_DATA_BIT4:
0213     case PS2_DATA_BIT5:
0214     case PS2_DATA_BIT6:
0215     case PS2_DATA_BIT7:
0216         /* processing data bits */
0217         if (data)
0218             byte |= (data << (cnt - 1));
0219         break;
0220     case PS2_PARITY_BIT:
0221         /* check odd parity */
0222         if (!((hweight8(byte) & 1) ^ data)) {
0223             rxflags |= SERIO_PARITY;
0224             dev_warn(drvdata->dev, "RX: parity error\n");
0225             if (!drvdata->write_enable)
0226                 goto err;
0227         }
0228         break;
0229     case PS2_STOP_BIT:
0230         /* stop bit should be high */
0231         if (unlikely(!data)) {
0232             dev_err(drvdata->dev, "RX: stop bit should be high\n");
0233             goto err;
0234         }
0235 
0236         /*
0237          * Do not send spurious ACK's and NACK's when write fn is
0238          * not provided.
0239          */
0240         if (!drvdata->write_enable) {
0241             if (byte == PS2_DEV_RET_NACK)
0242                 goto err;
0243             else if (byte == PS2_DEV_RET_ACK)
0244                 break;
0245         }
0246 
0247         serio_interrupt(drvdata->serio, byte, rxflags);
0248         dev_dbg(drvdata->dev, "RX: sending byte 0x%x\n", byte);
0249 
0250         cnt = byte = 0;
0251 
0252         goto end; /* success */
0253     default:
0254         dev_err(drvdata->dev, "RX: got out of sync with the device\n");
0255         goto err;
0256     }
0257 
0258     cnt++;
0259     goto end; /* success */
0260 
0261 err:
0262     cnt = byte = 0;
0263     __ps2_gpio_write(drvdata->serio, PS2_CMD_RESEND);
0264 end:
0265     drvdata->rx.cnt = cnt;
0266     drvdata->rx.byte = byte;
0267     return IRQ_HANDLED;
0268 }
0269 
0270 static irqreturn_t ps2_gpio_irq_tx(struct ps2_gpio_data *drvdata)
0271 {
0272     unsigned char byte, cnt;
0273     int data;
0274     s64 us_delta;
0275 
0276     cnt = drvdata->tx.cnt;
0277     byte = drvdata->tx.byte;
0278 
0279     drvdata->t_irq_now = ktime_get();
0280 
0281     /*
0282      * There might be pending IRQs since we disabled IRQs in
0283      * __ps2_gpio_write().  We can expect at least one clock period until
0284      * the device generates the first falling edge after releasing the
0285      * clock line.
0286      */
0287     us_delta = ktime_us_delta(drvdata->t_irq_now,
0288                   drvdata->tx.t_xfer_start);
0289     if (unlikely(us_delta < PS2_CLK_MIN_INTERVAL_US))
0290         goto end;
0291 
0292     us_delta = ktime_us_delta(drvdata->t_irq_now, drvdata->t_irq_last);
0293     if (us_delta > PS2_IRQ_MAX_INTERVAL_US && cnt > 1) {
0294         dev_err(drvdata->dev,
0295             "TX: timeout, probably we missed an interrupt\n");
0296         goto err;
0297     } else if (unlikely(us_delta < PS2_IRQ_MIN_INTERVAL_US)) {
0298         /* Ignore spurious IRQs. */
0299         goto end;
0300     }
0301     drvdata->t_irq_last = drvdata->t_irq_now;
0302 
0303     switch (cnt) {
0304     case PS2_START_BIT:
0305         /* should never happen */
0306         dev_err(drvdata->dev,
0307             "TX: start bit should have been sent already\n");
0308         goto err;
0309     case PS2_DATA_BIT0:
0310     case PS2_DATA_BIT1:
0311     case PS2_DATA_BIT2:
0312     case PS2_DATA_BIT3:
0313     case PS2_DATA_BIT4:
0314     case PS2_DATA_BIT5:
0315     case PS2_DATA_BIT6:
0316     case PS2_DATA_BIT7:
0317         data = byte & BIT(cnt - 1);
0318         gpiod_set_value(drvdata->gpio_data, data);
0319         break;
0320     case PS2_PARITY_BIT:
0321         /* do odd parity */
0322         data = !(hweight8(byte) & 1);
0323         gpiod_set_value(drvdata->gpio_data, data);
0324         break;
0325     case PS2_STOP_BIT:
0326         /* release data line to generate stop bit */
0327         gpiod_direction_input(drvdata->gpio_data);
0328         break;
0329     case PS2_ACK_BIT:
0330         data = gpiod_get_value(drvdata->gpio_data);
0331         if (data) {
0332             dev_warn(drvdata->dev, "TX: received NACK, retry\n");
0333             goto err;
0334         }
0335 
0336         drvdata->tx.t_xfer_end = ktime_get();
0337         drvdata->mode = PS2_MODE_RX;
0338         complete(&drvdata->tx.complete);
0339 
0340         cnt = 1;
0341         goto end; /* success */
0342     default:
0343         /*
0344          * Probably we missed the stop bit. Therefore we release data
0345          * line and try again.
0346          */
0347         gpiod_direction_input(drvdata->gpio_data);
0348         dev_err(drvdata->dev, "TX: got out of sync with the device\n");
0349         goto err;
0350     }
0351 
0352     cnt++;
0353     goto end; /* success */
0354 
0355 err:
0356     cnt = 1;
0357     gpiod_direction_input(drvdata->gpio_data);
0358     __ps2_gpio_write(drvdata->serio, drvdata->tx.byte);
0359 end:
0360     drvdata->tx.cnt = cnt;
0361     return IRQ_HANDLED;
0362 }
0363 
0364 static irqreturn_t ps2_gpio_irq(int irq, void *dev_id)
0365 {
0366     struct ps2_gpio_data *drvdata = dev_id;
0367 
0368     return drvdata->mode ? ps2_gpio_irq_tx(drvdata) :
0369         ps2_gpio_irq_rx(drvdata);
0370 }
0371 
0372 static int ps2_gpio_get_props(struct device *dev,
0373                  struct ps2_gpio_data *drvdata)
0374 {
0375     enum gpiod_flags gflags;
0376 
0377     /* Enforce open drain, since this is required by the PS/2 bus. */
0378     gflags = GPIOD_IN | GPIOD_FLAGS_BIT_OPEN_DRAIN;
0379 
0380     drvdata->gpio_data = devm_gpiod_get(dev, "data", gflags);
0381     if (IS_ERR(drvdata->gpio_data)) {
0382         dev_err(dev, "failed to request data gpio: %ld",
0383             PTR_ERR(drvdata->gpio_data));
0384         return PTR_ERR(drvdata->gpio_data);
0385     }
0386 
0387     drvdata->gpio_clk = devm_gpiod_get(dev, "clk", gflags);
0388     if (IS_ERR(drvdata->gpio_clk)) {
0389         dev_err(dev, "failed to request clock gpio: %ld",
0390             PTR_ERR(drvdata->gpio_clk));
0391         return PTR_ERR(drvdata->gpio_clk);
0392     }
0393 
0394     drvdata->write_enable = device_property_read_bool(dev,
0395                 "write-enable");
0396 
0397     return 0;
0398 }
0399 
0400 static int ps2_gpio_probe(struct platform_device *pdev)
0401 {
0402     struct ps2_gpio_data *drvdata;
0403     struct serio *serio;
0404     struct device *dev = &pdev->dev;
0405     int error;
0406 
0407     drvdata = devm_kzalloc(dev, sizeof(struct ps2_gpio_data), GFP_KERNEL);
0408     serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
0409     if (!drvdata || !serio) {
0410         error = -ENOMEM;
0411         goto err_free_serio;
0412     }
0413 
0414     error = ps2_gpio_get_props(dev, drvdata);
0415     if (error)
0416         goto err_free_serio;
0417 
0418     if (gpiod_cansleep(drvdata->gpio_data) ||
0419         gpiod_cansleep(drvdata->gpio_clk)) {
0420         dev_err(dev, "GPIO data or clk are connected via slow bus\n");
0421         error = -EINVAL;
0422         goto err_free_serio;
0423     }
0424 
0425     drvdata->irq = platform_get_irq(pdev, 0);
0426     if (drvdata->irq < 0) {
0427         error = drvdata->irq;
0428         goto err_free_serio;
0429     }
0430 
0431     error = devm_request_irq(dev, drvdata->irq, ps2_gpio_irq,
0432                  IRQF_NO_THREAD, DRIVER_NAME, drvdata);
0433     if (error) {
0434         dev_err(dev, "failed to request irq %d: %d\n",
0435             drvdata->irq, error);
0436         goto err_free_serio;
0437     }
0438 
0439     /* Keep irq disabled until serio->open is called. */
0440     disable_irq(drvdata->irq);
0441 
0442     serio->id.type = SERIO_8042;
0443     serio->open = ps2_gpio_open;
0444     serio->close = ps2_gpio_close;
0445     /*
0446      * Write can be enabled in platform/dt data, but possibly it will not
0447      * work because of the tough timings.
0448      */
0449     serio->write = drvdata->write_enable ? ps2_gpio_write : NULL;
0450     serio->port_data = drvdata;
0451     serio->dev.parent = dev;
0452     strlcpy(serio->name, dev_name(dev), sizeof(serio->name));
0453     strlcpy(serio->phys, dev_name(dev), sizeof(serio->phys));
0454 
0455     drvdata->serio = serio;
0456     drvdata->dev = dev;
0457     drvdata->mode = PS2_MODE_RX;
0458 
0459     /*
0460      * Tx count always starts at 1, as the start bit is sent implicitly by
0461      * host-to-device communication initialization.
0462      */
0463     drvdata->tx.cnt = 1;
0464 
0465     INIT_DELAYED_WORK(&drvdata->tx.work, ps2_gpio_tx_work_fn);
0466     init_completion(&drvdata->tx.complete);
0467     mutex_init(&drvdata->tx.mutex);
0468 
0469     serio_register_port(serio);
0470     platform_set_drvdata(pdev, drvdata);
0471 
0472     return 0;   /* success */
0473 
0474 err_free_serio:
0475     kfree(serio);
0476     return error;
0477 }
0478 
0479 static int ps2_gpio_remove(struct platform_device *pdev)
0480 {
0481     struct ps2_gpio_data *drvdata = platform_get_drvdata(pdev);
0482 
0483     serio_unregister_port(drvdata->serio);
0484     return 0;
0485 }
0486 
0487 #if defined(CONFIG_OF)
0488 static const struct of_device_id ps2_gpio_match[] = {
0489     { .compatible = "ps2-gpio", },
0490     { },
0491 };
0492 MODULE_DEVICE_TABLE(of, ps2_gpio_match);
0493 #endif
0494 
0495 static struct platform_driver ps2_gpio_driver = {
0496     .probe      = ps2_gpio_probe,
0497     .remove     = ps2_gpio_remove,
0498     .driver = {
0499         .name = DRIVER_NAME,
0500         .of_match_table = of_match_ptr(ps2_gpio_match),
0501     },
0502 };
0503 module_platform_driver(ps2_gpio_driver);
0504 
0505 MODULE_AUTHOR("Danilo Krummrich <danilokrummrich@dk-develop.de>");
0506 MODULE_DESCRIPTION("GPIO PS2 driver");
0507 MODULE_LICENSE("GPL v2");