0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
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
0054 #define ADC_FIRST_DLY(x) ((x) << 24)
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)
0058 #define FS_DIV(x) ((x) << 16)
0059 #define T_ACQ(x) ((x) << 0)
0060
0061
0062 #define STYLUS_UP_DEBOUN(x) ((x) << 12)
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)
0069
0070
0071 #define SUN6I_TP_MODE_EN(x) ((x) << 5)
0072
0073
0074 #define TP_SENSITIVE_ADJUST(x) ((x) << 28)
0075 #define TP_MODE_SELECT(x) ((x) << 26)
0076 #define PRE_MEA_EN(x) ((x) << 24)
0077 #define PRE_MEA_THRE_CNT(x) ((x) << 0)
0078
0079
0080 #define FILTER_EN(x) ((x) << 2)
0081 #define FILTER_TYPE(x) ((x) << 0)
0082
0083
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)
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
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
0103 #define TEMP_ENABLE(x) ((x) << 16)
0104 #define TEMP_PERIOD(x) ((x) << 0)
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
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
0130
0131
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
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
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
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
0256 ts->temp_offset = 271000;
0257 ts->temp_step = 167;
0258 } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
0259
0260
0261
0262
0263
0264
0265 ts->temp_offset = 257000;
0266 ts->temp_step = 133;
0267 } else {
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
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
0315
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
0322
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
0331
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
0337 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
0338
0339
0340
0341
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
0352
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
0383 if (ts->input)
0384 input_unregister_device(ts->input);
0385
0386
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 { }
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");