1 /* 2 * Allwinner sunxi resistive touchscreen controller driver 3 * 4 * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com> 5 * 6 * The hwmon parts are based on work by Corentin LABBE which is: 7 * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 */ 19 20 /* 21 * The sun4i-ts controller is capable of detecting a second touch, but when a 22 * second touch is present then the accuracy becomes so bad the reported touch 23 * location is not useable. 24 * 25 * The original android driver contains some complicated heuristics using the 26 * aprox. distance between the 2 touches to see if the user is making a pinch 27 * open / close movement, and then reports emulated multi-touch events around 28 * the last touch coordinate (as the dual-touch coordinates are worthless). 29 * 30 * These kinds of heuristics are just asking for trouble (and don't belong 31 * in the kernel). So this driver offers straight forward, reliable single 32 * touch functionality only. 33 */ 34 35 #include <linux/err.h> 36 #include <linux/hwmon.h> 37 #include <linux/thermal.h> 38 #include <linux/init.h> 39 #include <linux/input.h> 40 #include <linux/interrupt.h> 41 #include <linux/io.h> 42 #include <linux/module.h> 43 #include <linux/of_platform.h> 44 #include <linux/platform_device.h> 45 #include <linux/slab.h> 46 47 #define TP_CTRL0 0x00 48 #define TP_CTRL1 0x04 49 #define TP_CTRL2 0x08 50 #define TP_CTRL3 0x0c 51 #define TP_INT_FIFOC 0x10 52 #define TP_INT_FIFOS 0x14 53 #define TP_TPR 0x18 54 #define TP_CDAT 0x1c 55 #define TEMP_DATA 0x20 56 #define TP_DATA 0x24 57 58 /* TP_CTRL0 bits */ 59 #define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */ 60 #define ADC_FIRST_DLY_MODE(x) ((x) << 23) 61 #define ADC_CLK_SEL(x) ((x) << 22) 62 #define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */ 63 #define FS_DIV(x) ((x) << 16) /* 4 bits */ 64 #define T_ACQ(x) ((x) << 0) /* 16 bits */ 65 66 /* TP_CTRL1 bits */ 67 #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */ 68 #define STYLUS_UP_DEBOUN_EN(x) ((x) << 9) 69 #define TOUCH_PAN_CALI_EN(x) ((x) << 6) 70 #define TP_DUAL_EN(x) ((x) << 5) 71 #define TP_MODE_EN(x) ((x) << 4) 72 #define TP_ADC_SELECT(x) ((x) << 3) 73 #define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */ 74 75 /* TP_CTRL2 bits */ 76 #define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */ 77 #define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */ 78 #define PRE_MEA_EN(x) ((x) << 24) 79 #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */ 80 81 /* TP_CTRL3 bits */ 82 #define FILTER_EN(x) ((x) << 2) 83 #define FILTER_TYPE(x) ((x) << 0) /* 2 bits */ 84 85 /* TP_INT_FIFOC irq and fifo mask / control bits */ 86 #define TEMP_IRQ_EN(x) ((x) << 18) 87 #define OVERRUN_IRQ_EN(x) ((x) << 17) 88 #define DATA_IRQ_EN(x) ((x) << 16) 89 #define TP_DATA_XY_CHANGE(x) ((x) << 13) 90 #define FIFO_TRIG(x) ((x) << 8) /* 5 bits */ 91 #define DATA_DRQ_EN(x) ((x) << 7) 92 #define FIFO_FLUSH(x) ((x) << 4) 93 #define TP_UP_IRQ_EN(x) ((x) << 1) 94 #define TP_DOWN_IRQ_EN(x) ((x) << 0) 95 96 /* TP_INT_FIFOS irq and fifo status bits */ 97 #define TEMP_DATA_PENDING BIT(18) 98 #define FIFO_OVERRUN_PENDING BIT(17) 99 #define FIFO_DATA_PENDING BIT(16) 100 #define TP_IDLE_FLG BIT(2) 101 #define TP_UP_PENDING BIT(1) 102 #define TP_DOWN_PENDING BIT(0) 103 104 /* TP_TPR bits */ 105 #define TEMP_ENABLE(x) ((x) << 16) 106 #define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */ 107 108 struct sun4i_ts_data { 109 struct device *dev; 110 struct input_dev *input; 111 struct thermal_zone_device *tz; 112 void __iomem *base; 113 unsigned int irq; 114 bool ignore_fifo_data; 115 int temp_data; 116 }; 117 118 static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val) 119 { 120 u32 x, y; 121 122 if (reg_val & FIFO_DATA_PENDING) { 123 x = readl(ts->base + TP_DATA); 124 y = readl(ts->base + TP_DATA); 125 /* The 1st location reported after an up event is unreliable */ 126 if (!ts->ignore_fifo_data) { 127 input_report_abs(ts->input, ABS_X, x); 128 input_report_abs(ts->input, ABS_Y, y); 129 /* 130 * The hardware has a separate down status bit, but 131 * that gets set before we get the first location, 132 * resulting in reporting a click on the old location. 133 */ 134 input_report_key(ts->input, BTN_TOUCH, 1); 135 input_sync(ts->input); 136 } else { 137 ts->ignore_fifo_data = false; 138 } 139 } 140 141 if (reg_val & TP_UP_PENDING) { 142 ts->ignore_fifo_data = true; 143 input_report_key(ts->input, BTN_TOUCH, 0); 144 input_sync(ts->input); 145 } 146 } 147 148 static irqreturn_t sun4i_ts_irq(int irq, void *dev_id) 149 { 150 struct sun4i_ts_data *ts = dev_id; 151 u32 reg_val; 152 153 reg_val = readl(ts->base + TP_INT_FIFOS); 154 155 if (reg_val & TEMP_DATA_PENDING) 156 ts->temp_data = readl(ts->base + TEMP_DATA); 157 158 if (ts->input) 159 sun4i_ts_irq_handle_input(ts, reg_val); 160 161 writel(reg_val, ts->base + TP_INT_FIFOS); 162 163 return IRQ_HANDLED; 164 } 165 166 static int sun4i_ts_open(struct input_dev *dev) 167 { 168 struct sun4i_ts_data *ts = input_get_drvdata(dev); 169 170 /* Flush, set trig level to 1, enable temp, data and up irqs */ 171 writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) | 172 TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC); 173 174 return 0; 175 } 176 177 static void sun4i_ts_close(struct input_dev *dev) 178 { 179 struct sun4i_ts_data *ts = input_get_drvdata(dev); 180 181 /* Deactivate all input IRQs */ 182 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC); 183 } 184 185 static int sun4i_get_temp(const struct sun4i_ts_data *ts, long *temp) 186 { 187 /* No temp_data until the first irq */ 188 if (ts->temp_data == -1) 189 return -EAGAIN; 190 191 /* 192 * The user manuals do not contain the formula for calculating 193 * the temperature. The formula used here is from the AXP209, 194 * which is designed by X-Powers, an affiliate of Allwinner: 195 * 196 * temperature = -144.7 + (value * 0.1) 197 * 198 * This should be replaced with the correct one if such information 199 * becomes available. 200 */ 201 *temp = (ts->temp_data - 1447) * 100; 202 203 return 0; 204 } 205 206 static int sun4i_get_tz_temp(void *data, long *temp) 207 { 208 return sun4i_get_temp(data, temp); 209 } 210 211 static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = { 212 .get_temp = sun4i_get_tz_temp, 213 }; 214 215 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, 216 char *buf) 217 { 218 struct sun4i_ts_data *ts = dev_get_drvdata(dev); 219 long temp; 220 int error; 221 222 error = sun4i_get_temp(ts, &temp); 223 if (error) 224 return error; 225 226 return sprintf(buf, "%ld\n", temp); 227 } 228 229 static ssize_t show_temp_label(struct device *dev, 230 struct device_attribute *devattr, char *buf) 231 { 232 return sprintf(buf, "SoC temperature\n"); 233 } 234 235 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); 236 static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL); 237 238 static struct attribute *sun4i_ts_attrs[] = { 239 &dev_attr_temp1_input.attr, 240 &dev_attr_temp1_label.attr, 241 NULL 242 }; 243 ATTRIBUTE_GROUPS(sun4i_ts); 244 245 static int sun4i_ts_probe(struct platform_device *pdev) 246 { 247 struct sun4i_ts_data *ts; 248 struct device *dev = &pdev->dev; 249 struct device_node *np = dev->of_node; 250 struct device *hwmon; 251 int error; 252 bool ts_attached; 253 254 ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL); 255 if (!ts) 256 return -ENOMEM; 257 258 ts->dev = dev; 259 ts->ignore_fifo_data = true; 260 ts->temp_data = -1; 261 262 ts_attached = of_property_read_bool(np, "allwinner,ts-attached"); 263 if (ts_attached) { 264 ts->input = devm_input_allocate_device(dev); 265 if (!ts->input) 266 return -ENOMEM; 267 268 ts->input->name = pdev->name; 269 ts->input->phys = "sun4i_ts/input0"; 270 ts->input->open = sun4i_ts_open; 271 ts->input->close = sun4i_ts_close; 272 ts->input->id.bustype = BUS_HOST; 273 ts->input->id.vendor = 0x0001; 274 ts->input->id.product = 0x0001; 275 ts->input->id.version = 0x0100; 276 ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS); 277 __set_bit(BTN_TOUCH, ts->input->keybit); 278 input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0); 279 input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0); 280 input_set_drvdata(ts->input, ts); 281 } 282 283 ts->base = devm_ioremap_resource(dev, 284 platform_get_resource(pdev, IORESOURCE_MEM, 0)); 285 if (IS_ERR(ts->base)) 286 return PTR_ERR(ts->base); 287 288 ts->irq = platform_get_irq(pdev, 0); 289 error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts); 290 if (error) 291 return error; 292 293 /* 294 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192, 295 * t_acq = clkin / (16 * 64) 296 */ 297 writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63), 298 ts->base + TP_CTRL0); 299 300 /* 301 * sensitive_adjust = 15 : max, which is not all that sensitive, 302 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch 303 */ 304 writel(TP_SENSITIVE_ADJUST(15) | TP_MODE_SELECT(0), 305 ts->base + TP_CTRL2); 306 307 /* Enable median filter, type 1 : 5/3 */ 308 writel(FILTER_EN(1) | FILTER_TYPE(1), ts->base + TP_CTRL3); 309 310 /* Enable temperature measurement, period 1953 (2 seconds) */ 311 writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR); 312 313 /* 314 * Set stylus up debounce to aprox 10 ms, enable debounce, and 315 * finally enable tp mode. 316 */ 317 writel(STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1) | TP_MODE_EN(1), 318 ts->base + TP_CTRL1); 319 320 /* 321 * The thermal core does not register hwmon devices for DT-based 322 * thermal zone sensors, such as this one. 323 */ 324 hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts", 325 ts, sun4i_ts_groups); 326 if (IS_ERR(hwmon)) 327 return PTR_ERR(hwmon); 328 329 ts->tz = thermal_zone_of_sensor_register(ts->dev, 0, ts, 330 &sun4i_ts_tz_ops); 331 if (IS_ERR(ts->tz)) 332 ts->tz = NULL; 333 334 writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC); 335 336 if (ts_attached) { 337 error = input_register_device(ts->input); 338 if (error) { 339 writel(0, ts->base + TP_INT_FIFOC); 340 thermal_zone_of_sensor_unregister(ts->dev, ts->tz); 341 return error; 342 } 343 } 344 345 platform_set_drvdata(pdev, ts); 346 return 0; 347 } 348 349 static int sun4i_ts_remove(struct platform_device *pdev) 350 { 351 struct sun4i_ts_data *ts = platform_get_drvdata(pdev); 352 353 /* Explicit unregister to avoid open/close changing the imask later */ 354 if (ts->input) 355 input_unregister_device(ts->input); 356 357 thermal_zone_of_sensor_unregister(ts->dev, ts->tz); 358 359 /* Deactivate all IRQs */ 360 writel(0, ts->base + TP_INT_FIFOC); 361 362 return 0; 363 } 364 365 static const struct of_device_id sun4i_ts_of_match[] = { 366 { .compatible = "allwinner,sun4i-a10-ts", }, 367 { /* sentinel */ } 368 }; 369 MODULE_DEVICE_TABLE(of, sun4i_ts_of_match); 370 371 static struct platform_driver sun4i_ts_driver = { 372 .driver = { 373 .name = "sun4i-ts", 374 .of_match_table = of_match_ptr(sun4i_ts_of_match), 375 }, 376 .probe = sun4i_ts_probe, 377 .remove = sun4i_ts_remove, 378 }; 379 380 module_platform_driver(sun4i_ts_driver); 381 382 MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver"); 383 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 384 MODULE_LICENSE("GPL"); 385