Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Touchscreen driver for Marvell 88PM860x
0004  *
0005  * Copyright (C) 2009 Marvell International Ltd.
0006  *  Haojian Zhuang <haojian.zhuang@marvell.com>
0007  */
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/i2c.h>
0013 #include <linux/input.h>
0014 #include <linux/mfd/88pm860x.h>
0015 #include <linux/slab.h>
0016 #include <linux/device.h>
0017 
0018 #define MEAS_LEN        (8)
0019 #define ACCURATE_BIT        (12)
0020 
0021 /* touch register */
0022 #define MEAS_EN3        (0x52)
0023 
0024 #define MEAS_TSIX_1     (0x8D)
0025 #define MEAS_TSIX_2     (0x8E)
0026 #define MEAS_TSIY_1     (0x8F)
0027 #define MEAS_TSIY_2     (0x90)
0028 #define MEAS_TSIZ1_1        (0x91)
0029 #define MEAS_TSIZ1_2        (0x92)
0030 #define MEAS_TSIZ2_1        (0x93)
0031 #define MEAS_TSIZ2_2        (0x94)
0032 
0033 /* bit definitions of touch */
0034 #define MEAS_PD_EN      (1 << 3)
0035 #define MEAS_TSIX_EN        (1 << 4)
0036 #define MEAS_TSIY_EN        (1 << 5)
0037 #define MEAS_TSIZ1_EN       (1 << 6)
0038 #define MEAS_TSIZ2_EN       (1 << 7)
0039 
0040 struct pm860x_touch {
0041     struct input_dev *idev;
0042     struct i2c_client *i2c;
0043     struct pm860x_chip *chip;
0044     int irq;
0045     int res_x;      /* resistor of Xplate */
0046 };
0047 
0048 static irqreturn_t pm860x_touch_handler(int irq, void *data)
0049 {
0050     struct pm860x_touch *touch = data;
0051     struct pm860x_chip *chip = touch->chip;
0052     unsigned char buf[MEAS_LEN];
0053     int x, y, pen_down;
0054     int z1, z2, rt = 0;
0055     int ret;
0056 
0057     ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
0058     if (ret < 0)
0059         goto out;
0060 
0061     pen_down = buf[1] & (1 << 6);
0062     x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
0063     y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
0064     z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
0065     z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
0066 
0067     if (pen_down) {
0068         if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
0069             rt = z2 / z1 - 1;
0070             rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
0071             dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
0072                 z1, z2, rt);
0073         }
0074         input_report_abs(touch->idev, ABS_X, x);
0075         input_report_abs(touch->idev, ABS_Y, y);
0076         input_report_abs(touch->idev, ABS_PRESSURE, rt);
0077         input_report_key(touch->idev, BTN_TOUCH, 1);
0078         dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
0079     } else {
0080         input_report_abs(touch->idev, ABS_PRESSURE, 0);
0081         input_report_key(touch->idev, BTN_TOUCH, 0);
0082         dev_dbg(chip->dev, "pen release\n");
0083     }
0084     input_sync(touch->idev);
0085 
0086 out:
0087     return IRQ_HANDLED;
0088 }
0089 
0090 static int pm860x_touch_open(struct input_dev *dev)
0091 {
0092     struct pm860x_touch *touch = input_get_drvdata(dev);
0093     int data, ret;
0094 
0095     data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
0096         | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
0097     ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
0098     if (ret < 0)
0099         goto out;
0100     return 0;
0101 out:
0102     return ret;
0103 }
0104 
0105 static void pm860x_touch_close(struct input_dev *dev)
0106 {
0107     struct pm860x_touch *touch = input_get_drvdata(dev);
0108     int data;
0109 
0110     data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
0111         | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
0112     pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
0113 }
0114 
0115 #ifdef CONFIG_OF
0116 static int pm860x_touch_dt_init(struct platform_device *pdev,
0117                       struct pm860x_chip *chip,
0118                       int *res_x)
0119 {
0120     struct device_node *np = pdev->dev.parent->of_node;
0121     struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
0122                  : chip->companion;
0123     int data, n, ret;
0124     if (!np)
0125         return -ENODEV;
0126     np = of_get_child_by_name(np, "touch");
0127     if (!np) {
0128         dev_err(&pdev->dev, "Can't find touch node\n");
0129         return -EINVAL;
0130     }
0131     /* set GPADC MISC1 register */
0132     data = 0;
0133     if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
0134         data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
0135     if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
0136         data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
0137     if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
0138         data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
0139     if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
0140         data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
0141     if (data) {
0142         ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
0143         if (ret < 0)
0144             goto err_put_node;
0145     }
0146     /* set tsi prebias time */
0147     if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
0148         ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
0149         if (ret < 0)
0150             goto err_put_node;
0151     }
0152     /* set prebias & prechg time of pen detect */
0153     data = 0;
0154     if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
0155         data |= n & PM8607_PD_PREBIAS_MASK;
0156     if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
0157         data |= n & PM8607_PD_PRECHG_MASK;
0158     if (data) {
0159         ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
0160         if (ret < 0)
0161             goto err_put_node;
0162     }
0163     of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
0164 
0165     of_node_put(np);
0166 
0167     return 0;
0168 
0169 err_put_node:
0170     of_node_put(np);
0171 
0172     return -EINVAL;
0173 }
0174 #else
0175 #define pm860x_touch_dt_init(x, y, z)   (-1)
0176 #endif
0177 
0178 static int pm860x_touch_probe(struct platform_device *pdev)
0179 {
0180     struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0181     struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
0182     struct pm860x_touch *touch;
0183     struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
0184                  : chip->companion;
0185     int irq, ret, res_x = 0, data = 0;
0186 
0187     irq = platform_get_irq(pdev, 0);
0188     if (irq < 0)
0189         return -EINVAL;
0190 
0191     if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
0192         if (pdata) {
0193             /* set GPADC MISC1 register */
0194             data = 0;
0195             data |= (pdata->gpadc_prebias << 1)
0196                 & PM8607_GPADC_PREBIAS_MASK;
0197             data |= (pdata->slot_cycle << 3)
0198                 & PM8607_GPADC_SLOT_CYCLE_MASK;
0199             data |= (pdata->off_scale << 5)
0200                 & PM8607_GPADC_OFF_SCALE_MASK;
0201             data |= (pdata->sw_cal << 7)
0202                 & PM8607_GPADC_SW_CAL_MASK;
0203             if (data) {
0204                 ret = pm860x_reg_write(i2c,
0205                     PM8607_GPADC_MISC1, data);
0206                 if (ret < 0)
0207                     return -EINVAL;
0208             }
0209             /* set tsi prebias time */
0210             if (pdata->tsi_prebias) {
0211                 data = pdata->tsi_prebias;
0212                 ret = pm860x_reg_write(i2c,
0213                     PM8607_TSI_PREBIAS, data);
0214                 if (ret < 0)
0215                     return -EINVAL;
0216             }
0217             /* set prebias & prechg time of pen detect */
0218             data = 0;
0219             data |= pdata->pen_prebias
0220                 & PM8607_PD_PREBIAS_MASK;
0221             data |= (pdata->pen_prechg << 5)
0222                 & PM8607_PD_PRECHG_MASK;
0223             if (data) {
0224                 ret = pm860x_reg_write(i2c,
0225                     PM8607_PD_PREBIAS, data);
0226                 if (ret < 0)
0227                     return -EINVAL;
0228             }
0229             res_x = pdata->res_x;
0230         } else {
0231             dev_err(&pdev->dev, "failed to get platform data\n");
0232             return -EINVAL;
0233         }
0234     }
0235     /* enable GPADC */
0236     ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
0237                   PM8607_GPADC_EN);
0238     if (ret)
0239         return ret;
0240 
0241     touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
0242                  GFP_KERNEL);
0243     if (!touch)
0244         return -ENOMEM;
0245 
0246     touch->idev = devm_input_allocate_device(&pdev->dev);
0247     if (!touch->idev) {
0248         dev_err(&pdev->dev, "Failed to allocate input device!\n");
0249         return -ENOMEM;
0250     }
0251 
0252     touch->idev->name = "88pm860x-touch";
0253     touch->idev->phys = "88pm860x/input0";
0254     touch->idev->id.bustype = BUS_I2C;
0255     touch->idev->dev.parent = &pdev->dev;
0256     touch->idev->open = pm860x_touch_open;
0257     touch->idev->close = pm860x_touch_close;
0258     touch->chip = chip;
0259     touch->i2c = i2c;
0260     touch->irq = irq;
0261     touch->res_x = res_x;
0262     input_set_drvdata(touch->idev, touch);
0263 
0264     ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
0265                     pm860x_touch_handler, IRQF_ONESHOT,
0266                     "touch", touch);
0267     if (ret < 0)
0268         return ret;
0269 
0270     __set_bit(EV_ABS, touch->idev->evbit);
0271     __set_bit(ABS_X, touch->idev->absbit);
0272     __set_bit(ABS_Y, touch->idev->absbit);
0273     __set_bit(ABS_PRESSURE, touch->idev->absbit);
0274     __set_bit(EV_SYN, touch->idev->evbit);
0275     __set_bit(EV_KEY, touch->idev->evbit);
0276     __set_bit(BTN_TOUCH, touch->idev->keybit);
0277 
0278     input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
0279     input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
0280     input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
0281                 0, 0);
0282 
0283     ret = input_register_device(touch->idev);
0284     if (ret < 0) {
0285         dev_err(chip->dev, "Failed to register touch!\n");
0286         return ret;
0287     }
0288 
0289     return 0;
0290 }
0291 
0292 static struct platform_driver pm860x_touch_driver = {
0293     .driver = {
0294         .name   = "88pm860x-touch",
0295     },
0296     .probe  = pm860x_touch_probe,
0297 };
0298 module_platform_driver(pm860x_touch_driver);
0299 
0300 MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
0301 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
0302 MODULE_LICENSE("GPL");
0303 MODULE_ALIAS("platform:88pm860x-touch");
0304