Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Synaptics NavPoint (PXA27x SSP/SPI) driver.
0004  *
0005  * Copyright (C) 2012 Paul Parsons <lost.distance@yahoo.com>
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/clk.h>
0012 #include <linux/delay.h>
0013 #include <linux/gpio.h>
0014 #include <linux/input.h>
0015 #include <linux/input/navpoint.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/mutex.h>
0018 #include <linux/pxa2xx_ssp.h>
0019 #include <linux/slab.h>
0020 
0021 /*
0022  * Synaptics Modular Embedded Protocol: Module Packet Format.
0023  * Module header byte 2:0 = Length (# bytes that follow)
0024  * Module header byte 4:3 = Control
0025  * Module header byte 7:5 = Module Address
0026  */
0027 #define HEADER_LENGTH(byte) ((byte) & 0x07)
0028 #define HEADER_CONTROL(byte)    (((byte) >> 3) & 0x03)
0029 #define HEADER_ADDRESS(byte)    ((byte) >> 5)
0030 
0031 struct navpoint {
0032     struct ssp_device   *ssp;
0033     struct input_dev    *input;
0034     struct device       *dev;
0035     int         gpio;
0036     int         index;
0037     u8          data[1 + HEADER_LENGTH(0xff)];
0038 };
0039 
0040 /*
0041  * Initialization values for SSCR0_x, SSCR1_x, SSSR_x.
0042  */
0043 static const u32 sscr0 = 0
0044     | SSCR0_TUM     /* TIM = 1; No TUR interrupts */
0045     | SSCR0_RIM     /* RIM = 1; No ROR interrupts */
0046     | SSCR0_SSE     /* SSE = 1; SSP enabled */
0047     | SSCR0_Motorola    /* FRF = 0; Motorola SPI */
0048     | SSCR0_DataSize(16)    /* DSS = 15; Data size = 16-bit */
0049     ;
0050 static const u32 sscr1 = 0
0051     | SSCR1_SCFR        /* SCFR = 1; SSPSCLK only during transfers */
0052     | SSCR1_SCLKDIR     /* SCLKDIR = 1; Slave mode */
0053     | SSCR1_SFRMDIR     /* SFRMDIR = 1; Slave mode */
0054     | SSCR1_RWOT        /* RWOT = 1; Receive without transmit mode */
0055     | SSCR1_RxTresh(1)  /* RFT = 0; Receive FIFO threshold = 1 */
0056     | SSCR1_SPH     /* SPH = 1; SSPSCLK inactive 0.5 + 1 cycles */
0057     | SSCR1_RIE     /* RIE = 1; Receive FIFO interrupt enabled */
0058     ;
0059 static const u32 sssr = 0
0060     | SSSR_BCE      /* BCE = 1; Clear BCE */
0061     | SSSR_TUR      /* TUR = 1; Clear TUR */
0062     | SSSR_EOC      /* EOC = 1; Clear EOC */
0063     | SSSR_TINT     /* TINT = 1; Clear TINT */
0064     | SSSR_PINT     /* PINT = 1; Clear PINT */
0065     | SSSR_ROR      /* ROR = 1; Clear ROR */
0066     ;
0067 
0068 /*
0069  * MEP Query $22: Touchpad Coordinate Range Query is not supported by
0070  * the NavPoint module, so sampled values provide the default limits.
0071  */
0072 #define NAVPOINT_X_MIN      1278
0073 #define NAVPOINT_X_MAX      5340
0074 #define NAVPOINT_Y_MIN      1572
0075 #define NAVPOINT_Y_MAX      4396
0076 #define NAVPOINT_PRESSURE_MIN   0
0077 #define NAVPOINT_PRESSURE_MAX   255
0078 
0079 static void navpoint_packet(struct navpoint *navpoint)
0080 {
0081     int finger;
0082     int gesture;
0083     int x, y, z;
0084 
0085     switch (navpoint->data[0]) {
0086     case 0xff:  /* Garbage (packet?) between reset and Hello packet */
0087     case 0x00:  /* Module 0, NULL packet */
0088         break;
0089 
0090     case 0x0e:  /* Module 0, Absolute packet */
0091         finger = (navpoint->data[1] & 0x01);
0092         gesture = (navpoint->data[1] & 0x02);
0093         x = ((navpoint->data[2] & 0x1f) << 8) | navpoint->data[3];
0094         y = ((navpoint->data[4] & 0x1f) << 8) | navpoint->data[5];
0095         z = navpoint->data[6];
0096         input_report_key(navpoint->input, BTN_TOUCH, finger);
0097         input_report_abs(navpoint->input, ABS_X, x);
0098         input_report_abs(navpoint->input, ABS_Y, y);
0099         input_report_abs(navpoint->input, ABS_PRESSURE, z);
0100         input_report_key(navpoint->input, BTN_TOOL_FINGER, finger);
0101         input_report_key(navpoint->input, BTN_LEFT, gesture);
0102         input_sync(navpoint->input);
0103         break;
0104 
0105     case 0x19:  /* Module 0, Hello packet */
0106         if ((navpoint->data[1] & 0xf0) == 0x10)
0107             break;
0108         fallthrough;
0109     default:
0110         dev_warn(navpoint->dev,
0111              "spurious packet: data=0x%02x,0x%02x,...\n",
0112              navpoint->data[0], navpoint->data[1]);
0113         break;
0114     }
0115 }
0116 
0117 static irqreturn_t navpoint_irq(int irq, void *dev_id)
0118 {
0119     struct navpoint *navpoint = dev_id;
0120     struct ssp_device *ssp = navpoint->ssp;
0121     irqreturn_t ret = IRQ_NONE;
0122     u32 status;
0123 
0124     status = pxa_ssp_read_reg(ssp, SSSR);
0125     if (status & sssr) {
0126         dev_warn(navpoint->dev,
0127              "unexpected interrupt: status=0x%08x\n", status);
0128         pxa_ssp_write_reg(ssp, SSSR, (status & sssr));
0129         ret = IRQ_HANDLED;
0130     }
0131 
0132     while (status & SSSR_RNE) {
0133         u32 data;
0134 
0135         data = pxa_ssp_read_reg(ssp, SSDR);
0136         navpoint->data[navpoint->index + 0] = (data >> 8);
0137         navpoint->data[navpoint->index + 1] = data;
0138         navpoint->index += 2;
0139         if (HEADER_LENGTH(navpoint->data[0]) < navpoint->index) {
0140             navpoint_packet(navpoint);
0141             navpoint->index = 0;
0142         }
0143         status = pxa_ssp_read_reg(ssp, SSSR);
0144         ret = IRQ_HANDLED;
0145     }
0146 
0147     return ret;
0148 }
0149 
0150 static void navpoint_up(struct navpoint *navpoint)
0151 {
0152     struct ssp_device *ssp = navpoint->ssp;
0153     int timeout;
0154 
0155     clk_prepare_enable(ssp->clk);
0156 
0157     pxa_ssp_write_reg(ssp, SSCR1, sscr1);
0158     pxa_ssp_write_reg(ssp, SSSR, sssr);
0159     pxa_ssp_write_reg(ssp, SSTO, 0);
0160     pxa_ssp_write_reg(ssp, SSCR0, sscr0);   /* SSCR0_SSE written last */
0161 
0162     /* Wait until SSP port is ready for slave clock operations */
0163     for (timeout = 100; timeout != 0; --timeout) {
0164         if (!(pxa_ssp_read_reg(ssp, SSSR) & SSSR_CSS))
0165             break;
0166         msleep(1);
0167     }
0168 
0169     if (timeout == 0)
0170         dev_err(navpoint->dev,
0171             "timeout waiting for SSSR[CSS] to clear\n");
0172 
0173     if (gpio_is_valid(navpoint->gpio))
0174         gpio_set_value(navpoint->gpio, 1);
0175 }
0176 
0177 static void navpoint_down(struct navpoint *navpoint)
0178 {
0179     struct ssp_device *ssp = navpoint->ssp;
0180 
0181     if (gpio_is_valid(navpoint->gpio))
0182         gpio_set_value(navpoint->gpio, 0);
0183 
0184     pxa_ssp_write_reg(ssp, SSCR0, 0);
0185 
0186     clk_disable_unprepare(ssp->clk);
0187 }
0188 
0189 static int navpoint_open(struct input_dev *input)
0190 {
0191     struct navpoint *navpoint = input_get_drvdata(input);
0192 
0193     navpoint_up(navpoint);
0194 
0195     return 0;
0196 }
0197 
0198 static void navpoint_close(struct input_dev *input)
0199 {
0200     struct navpoint *navpoint = input_get_drvdata(input);
0201 
0202     navpoint_down(navpoint);
0203 }
0204 
0205 static int navpoint_probe(struct platform_device *pdev)
0206 {
0207     const struct navpoint_platform_data *pdata =
0208                     dev_get_platdata(&pdev->dev);
0209     struct ssp_device *ssp;
0210     struct input_dev *input;
0211     struct navpoint *navpoint;
0212     int error;
0213 
0214     if (!pdata) {
0215         dev_err(&pdev->dev, "no platform data\n");
0216         return -EINVAL;
0217     }
0218 
0219     if (gpio_is_valid(pdata->gpio)) {
0220         error = gpio_request_one(pdata->gpio, GPIOF_OUT_INIT_LOW,
0221                      "SYNAPTICS_ON");
0222         if (error)
0223             return error;
0224     }
0225 
0226     ssp = pxa_ssp_request(pdata->port, pdev->name);
0227     if (!ssp) {
0228         error = -ENODEV;
0229         goto err_free_gpio;
0230     }
0231 
0232     /* HaRET does not disable devices before jumping into Linux */
0233     if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
0234         pxa_ssp_write_reg(ssp, SSCR0, 0);
0235         dev_warn(&pdev->dev, "ssp%d already enabled\n", pdata->port);
0236     }
0237 
0238     navpoint = kzalloc(sizeof(*navpoint), GFP_KERNEL);
0239     input = input_allocate_device();
0240     if (!navpoint || !input) {
0241         error = -ENOMEM;
0242         goto err_free_mem;
0243     }
0244 
0245     navpoint->ssp = ssp;
0246     navpoint->input = input;
0247     navpoint->dev = &pdev->dev;
0248     navpoint->gpio = pdata->gpio;
0249 
0250     input->name = pdev->name;
0251     input->dev.parent = &pdev->dev;
0252 
0253     __set_bit(EV_KEY, input->evbit);
0254     __set_bit(EV_ABS, input->evbit);
0255     __set_bit(BTN_LEFT, input->keybit);
0256     __set_bit(BTN_TOUCH, input->keybit);
0257     __set_bit(BTN_TOOL_FINGER, input->keybit);
0258 
0259     input_set_abs_params(input, ABS_X,
0260                  NAVPOINT_X_MIN, NAVPOINT_X_MAX, 0, 0);
0261     input_set_abs_params(input, ABS_Y,
0262                  NAVPOINT_Y_MIN, NAVPOINT_Y_MAX, 0, 0);
0263     input_set_abs_params(input, ABS_PRESSURE,
0264                  NAVPOINT_PRESSURE_MIN, NAVPOINT_PRESSURE_MAX,
0265                  0, 0);
0266 
0267     input->open = navpoint_open;
0268     input->close = navpoint_close;
0269 
0270     input_set_drvdata(input, navpoint);
0271 
0272     error = request_irq(ssp->irq, navpoint_irq, 0, pdev->name, navpoint);
0273     if (error)
0274         goto err_free_mem;
0275 
0276     error = input_register_device(input);
0277     if (error)
0278         goto err_free_irq;
0279 
0280     platform_set_drvdata(pdev, navpoint);
0281     dev_dbg(&pdev->dev, "ssp%d, irq %d\n", pdata->port, ssp->irq);
0282 
0283     return 0;
0284 
0285 err_free_irq:
0286     free_irq(ssp->irq, navpoint);
0287 err_free_mem:
0288     input_free_device(input);
0289     kfree(navpoint);
0290     pxa_ssp_free(ssp);
0291 err_free_gpio:
0292     if (gpio_is_valid(pdata->gpio))
0293         gpio_free(pdata->gpio);
0294 
0295     return error;
0296 }
0297 
0298 static int navpoint_remove(struct platform_device *pdev)
0299 {
0300     const struct navpoint_platform_data *pdata =
0301                     dev_get_platdata(&pdev->dev);
0302     struct navpoint *navpoint = platform_get_drvdata(pdev);
0303     struct ssp_device *ssp = navpoint->ssp;
0304 
0305     free_irq(ssp->irq, navpoint);
0306 
0307     input_unregister_device(navpoint->input);
0308     kfree(navpoint);
0309 
0310     pxa_ssp_free(ssp);
0311 
0312     if (gpio_is_valid(pdata->gpio))
0313         gpio_free(pdata->gpio);
0314 
0315     return 0;
0316 }
0317 
0318 static int __maybe_unused navpoint_suspend(struct device *dev)
0319 {
0320     struct platform_device *pdev = to_platform_device(dev);
0321     struct navpoint *navpoint = platform_get_drvdata(pdev);
0322     struct input_dev *input = navpoint->input;
0323 
0324     mutex_lock(&input->mutex);
0325     if (input_device_enabled(input))
0326         navpoint_down(navpoint);
0327     mutex_unlock(&input->mutex);
0328 
0329     return 0;
0330 }
0331 
0332 static int __maybe_unused navpoint_resume(struct device *dev)
0333 {
0334     struct platform_device *pdev = to_platform_device(dev);
0335     struct navpoint *navpoint = platform_get_drvdata(pdev);
0336     struct input_dev *input = navpoint->input;
0337 
0338     mutex_lock(&input->mutex);
0339     if (input_device_enabled(input))
0340         navpoint_up(navpoint);
0341     mutex_unlock(&input->mutex);
0342 
0343     return 0;
0344 }
0345 
0346 static SIMPLE_DEV_PM_OPS(navpoint_pm_ops, navpoint_suspend, navpoint_resume);
0347 
0348 static struct platform_driver navpoint_driver = {
0349     .probe      = navpoint_probe,
0350     .remove     = navpoint_remove,
0351     .driver = {
0352         .name   = "navpoint",
0353         .pm = &navpoint_pm_ops,
0354     },
0355 };
0356 
0357 module_platform_driver(navpoint_driver);
0358 
0359 MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
0360 MODULE_DESCRIPTION("Synaptics NavPoint (PXA27x SSP/SPI) driver");
0361 MODULE_LICENSE("GPL");
0362 MODULE_ALIAS("platform:navpoint");