Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Allwinner sunxi resistive touchscreen controller driver
0004  *
0005  * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
0006  *
0007  * The hwmon parts are based on work by Corentin LABBE which is:
0008  * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
0009  */
0010 
0011 /*
0012  * The sun4i-ts controller is capable of detecting a second touch, but when a
0013  * second touch is present then the accuracy becomes so bad the reported touch
0014  * location is not useable.
0015  *
0016  * The original android driver contains some complicated heuristics using the
0017  * aprox. distance between the 2 touches to see if the user is making a pinch
0018  * open / close movement, and then reports emulated multi-touch events around
0019  * the last touch coordinate (as the dual-touch coordinates are worthless).
0020  *
0021  * These kinds of heuristics are just asking for trouble (and don't belong
0022  * in the kernel). So this driver offers straight forward, reliable single
0023  * touch functionality only.
0024  *
0025  * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi.rst)
0026  * (looks like the description in the A20 User Manual v1.3 is better
0027  * than the one in the A10 User Manual v.1.5)
0028  */
0029 
0030 #include <linux/err.h>
0031 #include <linux/hwmon.h>
0032 #include <linux/thermal.h>
0033 #include <linux/init.h>
0034 #include <linux/input.h>
0035 #include <linux/interrupt.h>
0036 #include <linux/io.h>
0037 #include <linux/module.h>
0038 #include <linux/of_platform.h>
0039 #include <linux/platform_device.h>
0040 #include <linux/slab.h>
0041 
0042 #define TP_CTRL0        0x00
0043 #define TP_CTRL1        0x04
0044 #define TP_CTRL2        0x08
0045 #define TP_CTRL3        0x0c
0046 #define TP_INT_FIFOC        0x10
0047 #define TP_INT_FIFOS        0x14
0048 #define TP_TPR          0x18
0049 #define TP_CDAT         0x1c
0050 #define TEMP_DATA       0x20
0051 #define TP_DATA         0x24
0052 
0053 /* TP_CTRL0 bits */
0054 #define ADC_FIRST_DLY(x)    ((x) << 24) /* 8 bits */
0055 #define ADC_FIRST_DLY_MODE(x)   ((x) << 23)
0056 #define ADC_CLK_SEL(x)      ((x) << 22)
0057 #define ADC_CLK_DIV(x)      ((x) << 20) /* 3 bits */
0058 #define FS_DIV(x)       ((x) << 16) /* 4 bits */
0059 #define T_ACQ(x)        ((x) << 0) /* 16 bits */
0060 
0061 /* TP_CTRL1 bits */
0062 #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
0063 #define STYLUS_UP_DEBOUN_EN(x)  ((x) << 9)
0064 #define TOUCH_PAN_CALI_EN(x)    ((x) << 6)
0065 #define TP_DUAL_EN(x)       ((x) << 5)
0066 #define TP_MODE_EN(x)       ((x) << 4)
0067 #define TP_ADC_SELECT(x)    ((x) << 3)
0068 #define ADC_CHAN_SELECT(x)  ((x) << 0)  /* 3 bits */
0069 
0070 /* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
0071 #define SUN6I_TP_MODE_EN(x) ((x) << 5)
0072 
0073 /* TP_CTRL2 bits */
0074 #define TP_SENSITIVE_ADJUST(x)  ((x) << 28) /* 4 bits */
0075 #define TP_MODE_SELECT(x)   ((x) << 26) /* 2 bits */
0076 #define PRE_MEA_EN(x)       ((x) << 24)
0077 #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
0078 
0079 /* TP_CTRL3 bits */
0080 #define FILTER_EN(x)        ((x) << 2)
0081 #define FILTER_TYPE(x)      ((x) << 0)  /* 2 bits */
0082 
0083 /* TP_INT_FIFOC irq and fifo mask / control bits */
0084 #define TEMP_IRQ_EN(x)      ((x) << 18)
0085 #define OVERRUN_IRQ_EN(x)   ((x) << 17)
0086 #define DATA_IRQ_EN(x)      ((x) << 16)
0087 #define TP_DATA_XY_CHANGE(x)    ((x) << 13)
0088 #define FIFO_TRIG(x)        ((x) << 8)  /* 5 bits */
0089 #define DATA_DRQ_EN(x)      ((x) << 7)
0090 #define FIFO_FLUSH(x)       ((x) << 4)
0091 #define TP_UP_IRQ_EN(x)     ((x) << 1)
0092 #define TP_DOWN_IRQ_EN(x)   ((x) << 0)
0093 
0094 /* TP_INT_FIFOS irq and fifo status bits */
0095 #define TEMP_DATA_PENDING   BIT(18)
0096 #define FIFO_OVERRUN_PENDING    BIT(17)
0097 #define FIFO_DATA_PENDING   BIT(16)
0098 #define TP_IDLE_FLG     BIT(2)
0099 #define TP_UP_PENDING       BIT(1)
0100 #define TP_DOWN_PENDING     BIT(0)
0101 
0102 /* TP_TPR bits */
0103 #define TEMP_ENABLE(x)      ((x) << 16)
0104 #define TEMP_PERIOD(x)      ((x) << 0)  /* t = x * 256 * 16 / clkin */
0105 
0106 struct sun4i_ts_data {
0107     struct device *dev;
0108     struct input_dev *input;
0109     void __iomem *base;
0110     unsigned int irq;
0111     bool ignore_fifo_data;
0112     int temp_data;
0113     int temp_offset;
0114     int temp_step;
0115 };
0116 
0117 static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
0118 {
0119     u32 x, y;
0120 
0121     if (reg_val & FIFO_DATA_PENDING) {
0122         x = readl(ts->base + TP_DATA);
0123         y = readl(ts->base + TP_DATA);
0124         /* The 1st location reported after an up event is unreliable */
0125         if (!ts->ignore_fifo_data) {
0126             input_report_abs(ts->input, ABS_X, x);
0127             input_report_abs(ts->input, ABS_Y, y);
0128             /*
0129              * The hardware has a separate down status bit, but
0130              * that gets set before we get the first location,
0131              * resulting in reporting a click on the old location.
0132              */
0133             input_report_key(ts->input, BTN_TOUCH, 1);
0134             input_sync(ts->input);
0135         } else {
0136             ts->ignore_fifo_data = false;
0137         }
0138     }
0139 
0140     if (reg_val & TP_UP_PENDING) {
0141         ts->ignore_fifo_data = true;
0142         input_report_key(ts->input, BTN_TOUCH, 0);
0143         input_sync(ts->input);
0144     }
0145 }
0146 
0147 static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
0148 {
0149     struct sun4i_ts_data *ts = dev_id;
0150     u32 reg_val;
0151 
0152     reg_val  = readl(ts->base + TP_INT_FIFOS);
0153 
0154     if (reg_val & TEMP_DATA_PENDING)
0155         ts->temp_data = readl(ts->base + TEMP_DATA);
0156 
0157     if (ts->input)
0158         sun4i_ts_irq_handle_input(ts, reg_val);
0159 
0160     writel(reg_val, ts->base + TP_INT_FIFOS);
0161 
0162     return IRQ_HANDLED;
0163 }
0164 
0165 static int sun4i_ts_open(struct input_dev *dev)
0166 {
0167     struct sun4i_ts_data *ts = input_get_drvdata(dev);
0168 
0169     /* Flush, set trig level to 1, enable temp, data and up irqs */
0170     writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
0171         TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
0172 
0173     return 0;
0174 }
0175 
0176 static void sun4i_ts_close(struct input_dev *dev)
0177 {
0178     struct sun4i_ts_data *ts = input_get_drvdata(dev);
0179 
0180     /* Deactivate all input IRQs */
0181     writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
0182 }
0183 
0184 static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
0185 {
0186     /* No temp_data until the first irq */
0187     if (ts->temp_data == -1)
0188         return -EAGAIN;
0189 
0190     *temp = ts->temp_data * ts->temp_step - ts->temp_offset;
0191 
0192     return 0;
0193 }
0194 
0195 static int sun4i_get_tz_temp(void *data, int *temp)
0196 {
0197     return sun4i_get_temp(data, temp);
0198 }
0199 
0200 static const struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
0201     .get_temp = sun4i_get_tz_temp,
0202 };
0203 
0204 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
0205              char *buf)
0206 {
0207     struct sun4i_ts_data *ts = dev_get_drvdata(dev);
0208     int temp;
0209     int error;
0210 
0211     error = sun4i_get_temp(ts, &temp);
0212     if (error)
0213         return error;
0214 
0215     return sprintf(buf, "%d\n", temp);
0216 }
0217 
0218 static ssize_t show_temp_label(struct device *dev,
0219                   struct device_attribute *devattr, char *buf)
0220 {
0221     return sprintf(buf, "SoC temperature\n");
0222 }
0223 
0224 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
0225 static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
0226 
0227 static struct attribute *sun4i_ts_attrs[] = {
0228     &dev_attr_temp1_input.attr,
0229     &dev_attr_temp1_label.attr,
0230     NULL
0231 };
0232 ATTRIBUTE_GROUPS(sun4i_ts);
0233 
0234 static int sun4i_ts_probe(struct platform_device *pdev)
0235 {
0236     struct sun4i_ts_data *ts;
0237     struct device *dev = &pdev->dev;
0238     struct device_node *np = dev->of_node;
0239     struct device *hwmon;
0240     struct thermal_zone_device *thermal;
0241     int error;
0242     u32 reg;
0243     bool ts_attached;
0244     u32 tp_sensitive_adjust = 15;
0245     u32 filter_type = 1;
0246 
0247     ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
0248     if (!ts)
0249         return -ENOMEM;
0250 
0251     ts->dev = dev;
0252     ts->ignore_fifo_data = true;
0253     ts->temp_data = -1;
0254     if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
0255         /* Allwinner SDK has temperature (C) = (value / 6) - 271 */
0256         ts->temp_offset = 271000;
0257         ts->temp_step = 167;
0258     } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
0259         /*
0260          * The A10 temperature sensor has quite a wide spread, these
0261          * parameters are based on the averaging of the calibration
0262          * results of 4 completely different boards, with a spread of
0263          * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
0264          */
0265         ts->temp_offset = 257000;
0266         ts->temp_step = 133;
0267     } else {
0268         /*
0269          * The user manuals do not contain the formula for calculating
0270          * the temperature. The formula used here is from the AXP209,
0271          * which is designed by X-Powers, an affiliate of Allwinner:
0272          *
0273          *     temperature (C) = (value * 0.1) - 144.7
0274          *
0275          * Allwinner does not have any documentation whatsoever for
0276          * this hardware. Moreover, it is claimed that the sensor
0277          * is inaccurate and cannot work properly.
0278          */
0279         ts->temp_offset = 144700;
0280         ts->temp_step = 100;
0281     }
0282 
0283     ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
0284     if (ts_attached) {
0285         ts->input = devm_input_allocate_device(dev);
0286         if (!ts->input)
0287             return -ENOMEM;
0288 
0289         ts->input->name = pdev->name;
0290         ts->input->phys = "sun4i_ts/input0";
0291         ts->input->open = sun4i_ts_open;
0292         ts->input->close = sun4i_ts_close;
0293         ts->input->id.bustype = BUS_HOST;
0294         ts->input->id.vendor = 0x0001;
0295         ts->input->id.product = 0x0001;
0296         ts->input->id.version = 0x0100;
0297         ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
0298         __set_bit(BTN_TOUCH, ts->input->keybit);
0299         input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
0300         input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
0301         input_set_drvdata(ts->input, ts);
0302     }
0303 
0304     ts->base = devm_platform_ioremap_resource(pdev, 0);
0305     if (IS_ERR(ts->base))
0306         return PTR_ERR(ts->base);
0307 
0308     ts->irq = platform_get_irq(pdev, 0);
0309     error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
0310     if (error)
0311         return error;
0312 
0313     /*
0314      * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
0315      * t_acq = clkin / (16 * 64)
0316      */
0317     writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
0318            ts->base + TP_CTRL0);
0319 
0320     /*
0321      * tp_sensitive_adjust is an optional property
0322      * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
0323      */
0324     of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
0325                  &tp_sensitive_adjust);
0326     writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
0327            ts->base + TP_CTRL2);
0328 
0329     /*
0330      * Enable median and averaging filter, optional property for
0331      * filter type.
0332      */
0333     of_property_read_u32(np, "allwinner,filter-type", &filter_type);
0334     writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
0335 
0336     /* Enable temperature measurement, period 1953 (2 seconds) */
0337     writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
0338 
0339     /*
0340      * Set stylus up debounce to aprox 10 ms, enable debounce, and
0341      * finally enable tp mode.
0342      */
0343     reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
0344     if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
0345         reg |= SUN6I_TP_MODE_EN(1);
0346     else
0347         reg |= TP_MODE_EN(1);
0348     writel(reg, ts->base + TP_CTRL1);
0349 
0350     /*
0351      * The thermal core does not register hwmon devices for DT-based
0352      * thermal zone sensors, such as this one.
0353      */
0354     hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
0355                                ts, sun4i_ts_groups);
0356     if (IS_ERR(hwmon))
0357         return PTR_ERR(hwmon);
0358 
0359     thermal = devm_thermal_zone_of_sensor_register(ts->dev, 0, ts,
0360                                &sun4i_ts_tz_ops);
0361     if (IS_ERR(thermal))
0362         return PTR_ERR(thermal);
0363 
0364     writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
0365 
0366     if (ts_attached) {
0367         error = input_register_device(ts->input);
0368         if (error) {
0369             writel(0, ts->base + TP_INT_FIFOC);
0370             return error;
0371         }
0372     }
0373 
0374     platform_set_drvdata(pdev, ts);
0375     return 0;
0376 }
0377 
0378 static int sun4i_ts_remove(struct platform_device *pdev)
0379 {
0380     struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
0381 
0382     /* Explicit unregister to avoid open/close changing the imask later */
0383     if (ts->input)
0384         input_unregister_device(ts->input);
0385 
0386     /* Deactivate all IRQs */
0387     writel(0, ts->base + TP_INT_FIFOC);
0388 
0389     return 0;
0390 }
0391 
0392 static const struct of_device_id sun4i_ts_of_match[] = {
0393     { .compatible = "allwinner,sun4i-a10-ts", },
0394     { .compatible = "allwinner,sun5i-a13-ts", },
0395     { .compatible = "allwinner,sun6i-a31-ts", },
0396     { /* sentinel */ }
0397 };
0398 MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
0399 
0400 static struct platform_driver sun4i_ts_driver = {
0401     .driver = {
0402         .name   = "sun4i-ts",
0403         .of_match_table = of_match_ptr(sun4i_ts_of_match),
0404     },
0405     .probe  = sun4i_ts_probe,
0406     .remove = sun4i_ts_remove,
0407 };
0408 
0409 module_platform_driver(sun4i_ts_driver);
0410 
0411 MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
0412 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0413 MODULE_LICENSE("GPL");