Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003 * Copyright (C) 2015 Broadcom Corporation
0004 *
0005 */
0006 #include <linux/module.h>
0007 #include <linux/init.h>
0008 #include <linux/input.h>
0009 #include <linux/delay.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/keyboard.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 #include <linux/of.h>
0015 #include <asm/irq.h>
0016 #include <linux/io.h>
0017 #include <linux/clk.h>
0018 #include <linux/serio.h>
0019 #include <linux/mfd/syscon.h>
0020 #include <linux/regmap.h>
0021 
0022 #define IPROC_TS_NAME "iproc-ts"
0023 
0024 #define PEN_DOWN_STATUS     1
0025 #define PEN_UP_STATUS       0
0026 
0027 #define X_MIN               0
0028 #define Y_MIN               0
0029 #define X_MAX               0xFFF
0030 #define Y_MAX               0xFFF
0031 
0032 /* Value given by controller for invalid coordinate. */
0033 #define INVALID_COORD       0xFFFFFFFF
0034 
0035 /* Register offsets */
0036 #define REGCTL1             0x00
0037 #define REGCTL2             0x04
0038 #define INTERRUPT_THRES     0x08
0039 #define INTERRUPT_MASK      0x0c
0040 
0041 #define INTERRUPT_STATUS    0x10
0042 #define CONTROLLER_STATUS   0x14
0043 #define FIFO_DATA           0x18
0044 #define FIFO_DATA_X_Y_MASK  0xFFFF
0045 #define ANALOG_CONTROL      0x1c
0046 
0047 #define AUX_DATA            0x20
0048 #define DEBOUNCE_CNTR_STAT  0x24
0049 #define SCAN_CNTR_STAT      0x28
0050 #define REM_CNTR_STAT       0x2c
0051 
0052 #define SETTLING_TIMER_STAT 0x30
0053 #define SPARE_REG           0x34
0054 #define SOFT_BYPASS_CONTROL 0x38
0055 #define SOFT_BYPASS_DATA    0x3c
0056 
0057 
0058 /* Bit values for INTERRUPT_MASK and INTERRUPT_STATUS regs */
0059 #define TS_PEN_INTR_MASK        BIT(0)
0060 #define TS_FIFO_INTR_MASK       BIT(2)
0061 
0062 /* Bit values for CONTROLLER_STATUS reg1 */
0063 #define TS_PEN_DOWN             BIT(0)
0064 
0065 /* Shift values for control reg1 */
0066 #define SCANNING_PERIOD_SHIFT   24
0067 #define DEBOUNCE_TIMEOUT_SHIFT  16
0068 #define SETTLING_TIMEOUT_SHIFT  8
0069 #define TOUCH_TIMEOUT_SHIFT     0
0070 
0071 /* Shift values for coordinates from fifo */
0072 #define X_COORD_SHIFT  0
0073 #define Y_COORD_SHIFT  16
0074 
0075 /* Bit values for REGCTL2 */
0076 #define TS_CONTROLLER_EN_BIT    BIT(16)
0077 #define TS_CONTROLLER_AVGDATA_SHIFT 8
0078 #define TS_CONTROLLER_AVGDATA_MASK (0x7 << TS_CONTROLLER_AVGDATA_SHIFT)
0079 #define TS_CONTROLLER_PWR_LDO   BIT(5)
0080 #define TS_CONTROLLER_PWR_ADC   BIT(4)
0081 #define TS_CONTROLLER_PWR_BGP   BIT(3)
0082 #define TS_CONTROLLER_PWR_TS    BIT(2)
0083 #define TS_WIRE_MODE_BIT        BIT(1)
0084 
0085 #define dbg_reg(dev, priv, reg) \
0086 do { \
0087     u32 val; \
0088     regmap_read(priv->regmap, reg, &val); \
0089     dev_dbg(dev, "%20s= 0x%08x\n", #reg, val); \
0090 } while (0)
0091 
0092 struct tsc_param {
0093     /* Each step is 1024 us.  Valid 1-256 */
0094     u32 scanning_period;
0095 
0096     /*  Each step is 512 us.  Valid 0-255 */
0097     u32 debounce_timeout;
0098 
0099     /*
0100      * The settling duration (in ms) is the amount of time the tsc
0101      * waits to allow the voltage to settle after turning on the
0102      * drivers in detection mode. Valid values: 0-11
0103      *   0 =  0.008 ms
0104      *   1 =  0.01 ms
0105      *   2 =  0.02 ms
0106      *   3 =  0.04 ms
0107      *   4 =  0.08 ms
0108      *   5 =  0.16 ms
0109      *   6 =  0.32 ms
0110      *   7 =  0.64 ms
0111      *   8 =  1.28 ms
0112      *   9 =  2.56 ms
0113      *   10 = 5.12 ms
0114      *   11 = 10.24 ms
0115      */
0116     u32 settling_timeout;
0117 
0118     /* touch timeout in sample counts */
0119     u32 touch_timeout;
0120 
0121     /*
0122      * Number of data samples which are averaged before a final data point
0123      * is placed into the FIFO
0124      */
0125     u32 average_data;
0126 
0127     /* FIFO threshold */
0128     u32 fifo_threshold;
0129 
0130     /* Optional standard touchscreen properties. */
0131     u32 max_x;
0132     u32 max_y;
0133     u32 fuzz_x;
0134     u32 fuzz_y;
0135     bool invert_x;
0136     bool invert_y;
0137 };
0138 
0139 struct iproc_ts_priv {
0140     struct platform_device *pdev;
0141     struct input_dev *idev;
0142 
0143     struct regmap *regmap;
0144     struct clk *tsc_clk;
0145 
0146     int  pen_status;
0147     struct tsc_param cfg_params;
0148 };
0149 
0150 /*
0151  * Set default values the same as hardware reset values
0152  * except for fifo_threshold with is set to 1.
0153  */
0154 static const struct tsc_param iproc_default_config = {
0155     .scanning_period  = 0x5,  /* 1 to 256 */
0156     .debounce_timeout = 0x28, /* 0 to 255 */
0157     .settling_timeout = 0x7,  /* 0 to 11 */
0158     .touch_timeout    = 0xa,  /* 0 to 255 */
0159     .average_data     = 5,    /* entry 5 = 32 pts */
0160     .fifo_threshold   = 1,    /* 0 to 31 */
0161     .max_x            = X_MAX,
0162     .max_y            = Y_MAX,
0163 };
0164 
0165 static void ts_reg_dump(struct iproc_ts_priv *priv)
0166 {
0167     struct device *dev = &priv->pdev->dev;
0168 
0169     dbg_reg(dev, priv, REGCTL1);
0170     dbg_reg(dev, priv, REGCTL2);
0171     dbg_reg(dev, priv, INTERRUPT_THRES);
0172     dbg_reg(dev, priv, INTERRUPT_MASK);
0173     dbg_reg(dev, priv, INTERRUPT_STATUS);
0174     dbg_reg(dev, priv, CONTROLLER_STATUS);
0175     dbg_reg(dev, priv, FIFO_DATA);
0176     dbg_reg(dev, priv, ANALOG_CONTROL);
0177     dbg_reg(dev, priv, AUX_DATA);
0178     dbg_reg(dev, priv, DEBOUNCE_CNTR_STAT);
0179     dbg_reg(dev, priv, SCAN_CNTR_STAT);
0180     dbg_reg(dev, priv, REM_CNTR_STAT);
0181     dbg_reg(dev, priv, SETTLING_TIMER_STAT);
0182     dbg_reg(dev, priv, SPARE_REG);
0183     dbg_reg(dev, priv, SOFT_BYPASS_CONTROL);
0184     dbg_reg(dev, priv, SOFT_BYPASS_DATA);
0185 }
0186 
0187 static irqreturn_t iproc_touchscreen_interrupt(int irq, void *data)
0188 {
0189     struct platform_device *pdev = data;
0190     struct iproc_ts_priv *priv = platform_get_drvdata(pdev);
0191     u32 intr_status;
0192     u32 raw_coordinate;
0193     u16 x;
0194     u16 y;
0195     int i;
0196     bool needs_sync = false;
0197 
0198     regmap_read(priv->regmap, INTERRUPT_STATUS, &intr_status);
0199     intr_status &= TS_PEN_INTR_MASK | TS_FIFO_INTR_MASK;
0200     if (intr_status == 0)
0201         return IRQ_NONE;
0202 
0203     /* Clear all interrupt status bits, write-1-clear */
0204     regmap_write(priv->regmap, INTERRUPT_STATUS, intr_status);
0205     /* Pen up/down */
0206     if (intr_status & TS_PEN_INTR_MASK) {
0207         regmap_read(priv->regmap, CONTROLLER_STATUS, &priv->pen_status);
0208         if (priv->pen_status & TS_PEN_DOWN)
0209             priv->pen_status = PEN_DOWN_STATUS;
0210         else
0211             priv->pen_status = PEN_UP_STATUS;
0212 
0213         input_report_key(priv->idev, BTN_TOUCH, priv->pen_status);
0214         needs_sync = true;
0215 
0216         dev_dbg(&priv->pdev->dev,
0217             "pen up-down (%d)\n", priv->pen_status);
0218     }
0219 
0220     /* coordinates in FIFO exceed the theshold */
0221     if (intr_status & TS_FIFO_INTR_MASK) {
0222         for (i = 0; i < priv->cfg_params.fifo_threshold; i++) {
0223             regmap_read(priv->regmap, FIFO_DATA, &raw_coordinate);
0224             if (raw_coordinate == INVALID_COORD)
0225                 continue;
0226 
0227             /*
0228              * The x and y coordinate are 16 bits each
0229              * with the x in the lower 16 bits and y in the
0230              * upper 16 bits.
0231              */
0232             x = (raw_coordinate >> X_COORD_SHIFT) &
0233                 FIFO_DATA_X_Y_MASK;
0234             y = (raw_coordinate >> Y_COORD_SHIFT) &
0235                 FIFO_DATA_X_Y_MASK;
0236 
0237             /* We only want to retain the 12 msb of the 16 */
0238             x = (x >> 4) & 0x0FFF;
0239             y = (y >> 4) & 0x0FFF;
0240 
0241             /* Adjust x y according to LCD tsc mount angle. */
0242             if (priv->cfg_params.invert_x)
0243                 x = priv->cfg_params.max_x - x;
0244 
0245             if (priv->cfg_params.invert_y)
0246                 y = priv->cfg_params.max_y - y;
0247 
0248             input_report_abs(priv->idev, ABS_X, x);
0249             input_report_abs(priv->idev, ABS_Y, y);
0250             needs_sync = true;
0251 
0252             dev_dbg(&priv->pdev->dev, "xy (0x%x 0x%x)\n", x, y);
0253         }
0254     }
0255 
0256     if (needs_sync)
0257         input_sync(priv->idev);
0258 
0259     return IRQ_HANDLED;
0260 }
0261 
0262 static int iproc_ts_start(struct input_dev *idev)
0263 {
0264     u32 val;
0265     u32 mask;
0266     int error;
0267     struct iproc_ts_priv *priv = input_get_drvdata(idev);
0268 
0269     /* Enable clock */
0270     error = clk_prepare_enable(priv->tsc_clk);
0271     if (error) {
0272         dev_err(&priv->pdev->dev, "%s clk_prepare_enable failed %d\n",
0273             __func__, error);
0274         return error;
0275     }
0276 
0277     /*
0278      * Interrupt is generated when:
0279      *  FIFO reaches the int_th value, and pen event(up/down)
0280      */
0281     val = TS_PEN_INTR_MASK | TS_FIFO_INTR_MASK;
0282     regmap_update_bits(priv->regmap, INTERRUPT_MASK, val, val);
0283 
0284     val = priv->cfg_params.fifo_threshold;
0285     regmap_write(priv->regmap, INTERRUPT_THRES, val);
0286 
0287     /* Initialize control reg1 */
0288     val = 0;
0289     val |= priv->cfg_params.scanning_period << SCANNING_PERIOD_SHIFT;
0290     val |= priv->cfg_params.debounce_timeout << DEBOUNCE_TIMEOUT_SHIFT;
0291     val |= priv->cfg_params.settling_timeout << SETTLING_TIMEOUT_SHIFT;
0292     val |= priv->cfg_params.touch_timeout << TOUCH_TIMEOUT_SHIFT;
0293     regmap_write(priv->regmap, REGCTL1, val);
0294 
0295     /* Try to clear all interrupt status */
0296     val = TS_FIFO_INTR_MASK | TS_PEN_INTR_MASK;
0297     regmap_update_bits(priv->regmap, INTERRUPT_STATUS, val, val);
0298 
0299     /* Initialize control reg2 */
0300     val = TS_CONTROLLER_EN_BIT | TS_WIRE_MODE_BIT;
0301     val |= priv->cfg_params.average_data << TS_CONTROLLER_AVGDATA_SHIFT;
0302 
0303     mask = (TS_CONTROLLER_AVGDATA_MASK);
0304     mask |= (TS_CONTROLLER_PWR_LDO |    /* PWR up LDO */
0305            TS_CONTROLLER_PWR_ADC |  /* PWR up ADC */
0306            TS_CONTROLLER_PWR_BGP |  /* PWR up BGP */
0307            TS_CONTROLLER_PWR_TS);   /* PWR up TS */
0308     mask |= val;
0309     regmap_update_bits(priv->regmap, REGCTL2, mask, val);
0310 
0311     ts_reg_dump(priv);
0312 
0313     return 0;
0314 }
0315 
0316 static void iproc_ts_stop(struct input_dev *dev)
0317 {
0318     u32 val;
0319     struct iproc_ts_priv *priv = input_get_drvdata(dev);
0320 
0321     /*
0322      * Disable FIFO int_th and pen event(up/down)Interrupts only
0323      * as the interrupt mask register is shared between ADC, TS and
0324      * flextimer.
0325      */
0326     val = TS_PEN_INTR_MASK | TS_FIFO_INTR_MASK;
0327     regmap_update_bits(priv->regmap, INTERRUPT_MASK, val, 0);
0328 
0329     /* Only power down touch screen controller */
0330     val = TS_CONTROLLER_PWR_TS;
0331     regmap_update_bits(priv->regmap, REGCTL2, val, val);
0332 
0333     clk_disable(priv->tsc_clk);
0334 }
0335 
0336 static int iproc_get_tsc_config(struct device *dev, struct iproc_ts_priv *priv)
0337 {
0338     struct device_node *np = dev->of_node;
0339     u32 val;
0340 
0341     priv->cfg_params = iproc_default_config;
0342 
0343     if (!np)
0344         return 0;
0345 
0346     if (of_property_read_u32(np, "scanning_period", &val) >= 0) {
0347         if (val < 1 || val > 256) {
0348             dev_err(dev, "scanning_period (%u) must be [1-256]\n",
0349                 val);
0350             return -EINVAL;
0351         }
0352         priv->cfg_params.scanning_period = val;
0353     }
0354 
0355     if (of_property_read_u32(np, "debounce_timeout", &val) >= 0) {
0356         if (val > 255) {
0357             dev_err(dev, "debounce_timeout (%u) must be [0-255]\n",
0358                 val);
0359             return -EINVAL;
0360         }
0361         priv->cfg_params.debounce_timeout = val;
0362     }
0363 
0364     if (of_property_read_u32(np, "settling_timeout", &val) >= 0) {
0365         if (val > 11) {
0366             dev_err(dev, "settling_timeout (%u) must be [0-11]\n",
0367                 val);
0368             return -EINVAL;
0369         }
0370         priv->cfg_params.settling_timeout = val;
0371     }
0372 
0373     if (of_property_read_u32(np, "touch_timeout", &val) >= 0) {
0374         if (val > 255) {
0375             dev_err(dev, "touch_timeout (%u) must be [0-255]\n",
0376                 val);
0377             return -EINVAL;
0378         }
0379         priv->cfg_params.touch_timeout = val;
0380     }
0381 
0382     if (of_property_read_u32(np, "average_data", &val) >= 0) {
0383         if (val > 8) {
0384             dev_err(dev, "average_data (%u) must be [0-8]\n", val);
0385             return -EINVAL;
0386         }
0387         priv->cfg_params.average_data = val;
0388     }
0389 
0390     if (of_property_read_u32(np, "fifo_threshold", &val) >= 0) {
0391         if (val > 31) {
0392             dev_err(dev, "fifo_threshold (%u)) must be [0-31]\n",
0393                 val);
0394             return -EINVAL;
0395         }
0396         priv->cfg_params.fifo_threshold = val;
0397     }
0398 
0399     /* Parse optional properties. */
0400     of_property_read_u32(np, "touchscreen-size-x", &priv->cfg_params.max_x);
0401     of_property_read_u32(np, "touchscreen-size-y", &priv->cfg_params.max_y);
0402 
0403     of_property_read_u32(np, "touchscreen-fuzz-x",
0404                  &priv->cfg_params.fuzz_x);
0405     of_property_read_u32(np, "touchscreen-fuzz-y",
0406                  &priv->cfg_params.fuzz_y);
0407 
0408     priv->cfg_params.invert_x =
0409         of_property_read_bool(np, "touchscreen-inverted-x");
0410     priv->cfg_params.invert_y =
0411         of_property_read_bool(np, "touchscreen-inverted-y");
0412 
0413     return 0;
0414 }
0415 
0416 static int iproc_ts_probe(struct platform_device *pdev)
0417 {
0418     struct iproc_ts_priv *priv;
0419     struct input_dev *idev;
0420     int irq;
0421     int error;
0422 
0423     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0424     if (!priv)
0425         return -ENOMEM;
0426 
0427     /* touchscreen controller memory mapped regs via syscon*/
0428     priv->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
0429                             "ts_syscon");
0430     if (IS_ERR(priv->regmap)) {
0431         error = PTR_ERR(priv->regmap);
0432         dev_err(&pdev->dev, "unable to map I/O memory:%d\n", error);
0433         return error;
0434     }
0435 
0436     priv->tsc_clk = devm_clk_get(&pdev->dev, "tsc_clk");
0437     if (IS_ERR(priv->tsc_clk)) {
0438         error = PTR_ERR(priv->tsc_clk);
0439         dev_err(&pdev->dev,
0440             "failed getting clock tsc_clk: %d\n", error);
0441         return error;
0442     }
0443 
0444     priv->pdev = pdev;
0445     error = iproc_get_tsc_config(&pdev->dev, priv);
0446     if (error) {
0447         dev_err(&pdev->dev, "get_tsc_config failed: %d\n", error);
0448         return error;
0449     }
0450 
0451     idev = devm_input_allocate_device(&pdev->dev);
0452     if (!idev) {
0453         dev_err(&pdev->dev, "failed to allocate input device\n");
0454         return -ENOMEM;
0455     }
0456 
0457     priv->idev = idev;
0458     priv->pen_status = PEN_UP_STATUS;
0459 
0460     /* Set input device info  */
0461     idev->name = IPROC_TS_NAME;
0462     idev->dev.parent = &pdev->dev;
0463 
0464     idev->id.bustype = BUS_HOST;
0465     idev->id.vendor = SERIO_UNKNOWN;
0466     idev->id.product = 0;
0467     idev->id.version = 0;
0468 
0469     idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0470     __set_bit(BTN_TOUCH, idev->keybit);
0471 
0472     input_set_abs_params(idev, ABS_X, X_MIN, priv->cfg_params.max_x,
0473                  priv->cfg_params.fuzz_x, 0);
0474     input_set_abs_params(idev, ABS_Y, Y_MIN, priv->cfg_params.max_y,
0475                  priv->cfg_params.fuzz_y, 0);
0476 
0477     idev->open = iproc_ts_start;
0478     idev->close = iproc_ts_stop;
0479 
0480     input_set_drvdata(idev, priv);
0481     platform_set_drvdata(pdev, priv);
0482 
0483     /* get interrupt */
0484     irq = platform_get_irq(pdev, 0);
0485     if (irq < 0)
0486         return irq;
0487 
0488     error = devm_request_irq(&pdev->dev, irq,
0489                  iproc_touchscreen_interrupt,
0490                  IRQF_SHARED, IPROC_TS_NAME, pdev);
0491     if (error)
0492         return error;
0493 
0494     error = input_register_device(priv->idev);
0495     if (error) {
0496         dev_err(&pdev->dev,
0497             "failed to register input device: %d\n", error);
0498         return error;
0499     }
0500 
0501     return 0;
0502 }
0503 
0504 static const struct of_device_id iproc_ts_of_match[] = {
0505     {.compatible = "brcm,iproc-touchscreen", },
0506     { },
0507 };
0508 MODULE_DEVICE_TABLE(of, iproc_ts_of_match);
0509 
0510 static struct platform_driver iproc_ts_driver = {
0511     .probe = iproc_ts_probe,
0512     .driver = {
0513         .name   = IPROC_TS_NAME,
0514         .of_match_table = of_match_ptr(iproc_ts_of_match),
0515     },
0516 };
0517 
0518 module_platform_driver(iproc_ts_driver);
0519 
0520 MODULE_DESCRIPTION("IPROC Touchscreen driver");
0521 MODULE_AUTHOR("Broadcom");
0522 MODULE_LICENSE("GPL v2");