0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/kernel.h>
0018 #include <linux/err.h>
0019 #include <linux/module.h>
0020 #include <linux/input.h>
0021 #include <linux/slab.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/clk.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/io.h>
0026 #include <linux/delay.h>
0027 #include <linux/of.h>
0028 #include <linux/of_device.h>
0029 #include <linux/sort.h>
0030 #include <linux/pm_wakeirq.h>
0031
0032 #include <linux/mfd/ti_am335x_tscadc.h>
0033
0034 #define ADCFSM_STEPID 0x10
0035 #define SEQ_SETTLE 275
0036 #define MAX_12BIT ((1 << 12) - 1)
0037
0038 #define TSC_IRQENB_MASK (IRQENB_FIFO0THRES | IRQENB_EOS | IRQENB_HW_PEN)
0039
0040 static const int config_pins[] = {
0041 STEPCONFIG_XPP,
0042 STEPCONFIG_XNN,
0043 STEPCONFIG_YPP,
0044 STEPCONFIG_YNN,
0045 };
0046
0047 struct titsc {
0048 struct input_dev *input;
0049 struct ti_tscadc_dev *mfd_tscadc;
0050 struct device *dev;
0051 unsigned int irq;
0052 unsigned int wires;
0053 unsigned int x_plate_resistance;
0054 bool pen_down;
0055 int coordinate_readouts;
0056 u32 config_inp[4];
0057 u32 bit_xp, bit_xn, bit_yp, bit_yn;
0058 u32 inp_xp, inp_xn, inp_yp, inp_yn;
0059 u32 step_mask;
0060 u32 charge_delay;
0061 };
0062
0063 static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
0064 {
0065 return readl(ts->mfd_tscadc->tscadc_base + reg);
0066 }
0067
0068 static void titsc_writel(struct titsc *tsc, unsigned int reg,
0069 unsigned int val)
0070 {
0071 writel(val, tsc->mfd_tscadc->tscadc_base + reg);
0072 }
0073
0074 static int titsc_config_wires(struct titsc *ts_dev)
0075 {
0076 u32 analog_line[4];
0077 u32 wire_order[4];
0078 int i, bit_cfg;
0079
0080 for (i = 0; i < 4; i++) {
0081
0082
0083
0084
0085 analog_line[i] = (ts_dev->config_inp[i] & 0xF0) >> 4;
0086 wire_order[i] = ts_dev->config_inp[i] & 0x0F;
0087 if (WARN_ON(analog_line[i] > 7))
0088 return -EINVAL;
0089 if (WARN_ON(wire_order[i] > ARRAY_SIZE(config_pins)))
0090 return -EINVAL;
0091 }
0092
0093 for (i = 0; i < 4; i++) {
0094 int an_line;
0095 int wi_order;
0096
0097 an_line = analog_line[i];
0098 wi_order = wire_order[i];
0099 bit_cfg = config_pins[wi_order];
0100 if (bit_cfg == 0)
0101 return -EINVAL;
0102 switch (wi_order) {
0103 case 0:
0104 ts_dev->bit_xp = bit_cfg;
0105 ts_dev->inp_xp = an_line;
0106 break;
0107
0108 case 1:
0109 ts_dev->bit_xn = bit_cfg;
0110 ts_dev->inp_xn = an_line;
0111 break;
0112
0113 case 2:
0114 ts_dev->bit_yp = bit_cfg;
0115 ts_dev->inp_yp = an_line;
0116 break;
0117 case 3:
0118 ts_dev->bit_yn = bit_cfg;
0119 ts_dev->inp_yn = an_line;
0120 break;
0121 }
0122 }
0123 return 0;
0124 }
0125
0126 static void titsc_step_config(struct titsc *ts_dev)
0127 {
0128 unsigned int config;
0129 int i, n;
0130 int end_step, first_step, tsc_steps;
0131 u32 stepenable;
0132
0133 config = STEPCONFIG_MODE_HWSYNC |
0134 STEPCONFIG_AVG_16 | ts_dev->bit_xp |
0135 STEPCONFIG_INM_ADCREFM;
0136 switch (ts_dev->wires) {
0137 case 4:
0138 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
0139 break;
0140 case 5:
0141 config |= ts_dev->bit_yn |
0142 STEPCONFIG_INP_AN4 | ts_dev->bit_xn |
0143 ts_dev->bit_yp;
0144 break;
0145 case 8:
0146 config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
0147 break;
0148 }
0149
0150 tsc_steps = ts_dev->coordinate_readouts * 2 + 2;
0151 first_step = TOTAL_STEPS - tsc_steps;
0152
0153 end_step = first_step + tsc_steps;
0154 n = 0;
0155 for (i = end_step - ts_dev->coordinate_readouts; i < end_step; i++) {
0156 titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
0157 titsc_writel(ts_dev, REG_STEPDELAY(i),
0158 n++ == 0 ? STEPCONFIG_OPENDLY : 0);
0159 }
0160
0161 config = 0;
0162 config = STEPCONFIG_MODE_HWSYNC |
0163 STEPCONFIG_AVG_16 | ts_dev->bit_yn |
0164 STEPCONFIG_INM_ADCREFM;
0165 switch (ts_dev->wires) {
0166 case 4:
0167 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
0168 break;
0169 case 5:
0170 config |= ts_dev->bit_xp | STEPCONFIG_INP_AN4 |
0171 STEPCONFIG_XNP | STEPCONFIG_YPN;
0172 break;
0173 case 8:
0174 config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
0175 break;
0176 }
0177
0178
0179 end_step = first_step + ts_dev->coordinate_readouts;
0180 n = 0;
0181 for (i = first_step; i < end_step; i++) {
0182 titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
0183 titsc_writel(ts_dev, REG_STEPDELAY(i),
0184 n++ == 0 ? STEPCONFIG_OPENDLY : 0);
0185 }
0186
0187
0188
0189 config = titsc_readl(ts_dev, REG_IDLECONFIG);
0190 titsc_writel(ts_dev, REG_CHARGECONFIG, config);
0191 titsc_writel(ts_dev, REG_CHARGEDELAY, ts_dev->charge_delay);
0192
0193
0194 config = STEPCONFIG_MODE_HWSYNC |
0195 STEPCONFIG_AVG_16 | ts_dev->bit_yp |
0196 ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
0197 STEPCONFIG_INP(ts_dev->inp_xp);
0198 titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
0199 titsc_writel(ts_dev, REG_STEPDELAY(end_step),
0200 STEPCONFIG_OPENDLY);
0201
0202 end_step++;
0203 config = STEPCONFIG_MODE_HWSYNC |
0204 STEPCONFIG_AVG_16 | ts_dev->bit_yp |
0205 ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
0206 STEPCONFIG_INP(ts_dev->inp_yn);
0207 titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
0208 titsc_writel(ts_dev, REG_STEPDELAY(end_step),
0209 STEPCONFIG_OPENDLY);
0210
0211
0212 stepenable = 1;
0213 for (i = 0; i < tsc_steps; i++)
0214 stepenable |= 1 << (first_step + i + 1);
0215
0216 ts_dev->step_mask = stepenable;
0217 am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);
0218 }
0219
0220 static int titsc_cmp_coord(const void *a, const void *b)
0221 {
0222 return *(int *)a - *(int *)b;
0223 }
0224
0225 static void titsc_read_coordinates(struct titsc *ts_dev,
0226 u32 *x, u32 *y, u32 *z1, u32 *z2)
0227 {
0228 unsigned int yvals[7], xvals[7];
0229 unsigned int i, xsum = 0, ysum = 0;
0230 unsigned int creads = ts_dev->coordinate_readouts;
0231
0232 for (i = 0; i < creads; i++) {
0233 yvals[i] = titsc_readl(ts_dev, REG_FIFO0);
0234 yvals[i] &= 0xfff;
0235 }
0236
0237 *z1 = titsc_readl(ts_dev, REG_FIFO0);
0238 *z1 &= 0xfff;
0239 *z2 = titsc_readl(ts_dev, REG_FIFO0);
0240 *z2 &= 0xfff;
0241
0242 for (i = 0; i < creads; i++) {
0243 xvals[i] = titsc_readl(ts_dev, REG_FIFO0);
0244 xvals[i] &= 0xfff;
0245 }
0246
0247
0248
0249
0250
0251
0252
0253
0254 if (creads <= 3) {
0255 for (i = 0; i < creads; i++) {
0256 ysum += yvals[i];
0257 xsum += xvals[i];
0258 }
0259 ysum /= creads;
0260 xsum /= creads;
0261 } else {
0262 sort(yvals, creads, sizeof(unsigned int),
0263 titsc_cmp_coord, NULL);
0264 sort(xvals, creads, sizeof(unsigned int),
0265 titsc_cmp_coord, NULL);
0266 for (i = 1; i < creads - 1; i++) {
0267 ysum += yvals[i];
0268 xsum += xvals[i];
0269 }
0270 ysum /= creads - 2;
0271 xsum /= creads - 2;
0272 }
0273 *y = ysum;
0274 *x = xsum;
0275 }
0276
0277 static irqreturn_t titsc_irq(int irq, void *dev)
0278 {
0279 struct titsc *ts_dev = dev;
0280 struct input_dev *input_dev = ts_dev->input;
0281 unsigned int fsm, status, irqclr = 0;
0282 unsigned int x = 0, y = 0;
0283 unsigned int z1, z2, z;
0284
0285 status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
0286 if (status & IRQENB_HW_PEN) {
0287 ts_dev->pen_down = true;
0288 irqclr |= IRQENB_HW_PEN;
0289 pm_stay_awake(ts_dev->dev);
0290 }
0291
0292 if (status & IRQENB_PENUP) {
0293 fsm = titsc_readl(ts_dev, REG_ADCFSM);
0294 if (fsm == ADCFSM_STEPID) {
0295 ts_dev->pen_down = false;
0296 input_report_key(input_dev, BTN_TOUCH, 0);
0297 input_report_abs(input_dev, ABS_PRESSURE, 0);
0298 input_sync(input_dev);
0299 pm_relax(ts_dev->dev);
0300 } else {
0301 ts_dev->pen_down = true;
0302 }
0303 irqclr |= IRQENB_PENUP;
0304 }
0305
0306 if (status & IRQENB_EOS)
0307 irqclr |= IRQENB_EOS;
0308
0309
0310
0311
0312
0313 if (status & IRQENB_FIFO0THRES) {
0314
0315 titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
0316
0317 if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
0318
0319
0320
0321
0322
0323 z = z1 - z2;
0324 z *= x;
0325 z *= ts_dev->x_plate_resistance;
0326 z /= z2;
0327 z = (z + 2047) >> 12;
0328
0329 if (z <= MAX_12BIT) {
0330 input_report_abs(input_dev, ABS_X, x);
0331 input_report_abs(input_dev, ABS_Y, y);
0332 input_report_abs(input_dev, ABS_PRESSURE, z);
0333 input_report_key(input_dev, BTN_TOUCH, 1);
0334 input_sync(input_dev);
0335 }
0336 }
0337 irqclr |= IRQENB_FIFO0THRES;
0338 }
0339 if (irqclr) {
0340 titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
0341 if (status & IRQENB_EOS)
0342 am335x_tsc_se_set_cache(ts_dev->mfd_tscadc,
0343 ts_dev->step_mask);
0344 return IRQ_HANDLED;
0345 }
0346 return IRQ_NONE;
0347 }
0348
0349 static int titsc_parse_dt(struct platform_device *pdev,
0350 struct titsc *ts_dev)
0351 {
0352 struct device_node *node = pdev->dev.of_node;
0353 int err;
0354
0355 if (!node)
0356 return -EINVAL;
0357
0358 err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
0359 if (err < 0)
0360 return err;
0361 switch (ts_dev->wires) {
0362 case 4:
0363 case 5:
0364 case 8:
0365 break;
0366 default:
0367 return -EINVAL;
0368 }
0369
0370 err = of_property_read_u32(node, "ti,x-plate-resistance",
0371 &ts_dev->x_plate_resistance);
0372 if (err < 0)
0373 return err;
0374
0375
0376
0377
0378
0379 err = of_property_read_u32(node, "ti,coordinate-readouts",
0380 &ts_dev->coordinate_readouts);
0381 if (err < 0) {
0382 dev_warn(&pdev->dev, "please use 'ti,coordinate-readouts' instead\n");
0383 err = of_property_read_u32(node, "ti,coordiante-readouts",
0384 &ts_dev->coordinate_readouts);
0385 }
0386
0387 if (err < 0)
0388 return err;
0389
0390 if (ts_dev->coordinate_readouts <= 0) {
0391 dev_warn(&pdev->dev,
0392 "invalid co-ordinate readouts, resetting it to 5\n");
0393 ts_dev->coordinate_readouts = 5;
0394 }
0395
0396 err = of_property_read_u32(node, "ti,charge-delay",
0397 &ts_dev->charge_delay);
0398
0399
0400
0401
0402 if (err < 0) {
0403 ts_dev->charge_delay = CHARGEDLY_OPENDLY;
0404 dev_warn(&pdev->dev, "ti,charge-delay not specified\n");
0405 }
0406
0407 return of_property_read_u32_array(node, "ti,wire-config",
0408 ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
0409 }
0410
0411
0412
0413
0414
0415 static int titsc_probe(struct platform_device *pdev)
0416 {
0417 struct titsc *ts_dev;
0418 struct input_dev *input_dev;
0419 struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
0420 int err;
0421
0422
0423 ts_dev = kzalloc(sizeof(*ts_dev), GFP_KERNEL);
0424 input_dev = input_allocate_device();
0425 if (!ts_dev || !input_dev) {
0426 dev_err(&pdev->dev, "failed to allocate memory.\n");
0427 err = -ENOMEM;
0428 goto err_free_mem;
0429 }
0430
0431 tscadc_dev->tsc = ts_dev;
0432 ts_dev->mfd_tscadc = tscadc_dev;
0433 ts_dev->input = input_dev;
0434 ts_dev->irq = tscadc_dev->irq;
0435 ts_dev->dev = &pdev->dev;
0436
0437 err = titsc_parse_dt(pdev, ts_dev);
0438 if (err) {
0439 dev_err(&pdev->dev, "Could not find valid DT data.\n");
0440 goto err_free_mem;
0441 }
0442
0443 err = request_irq(ts_dev->irq, titsc_irq,
0444 IRQF_SHARED, pdev->dev.driver->name, ts_dev);
0445 if (err) {
0446 dev_err(&pdev->dev, "failed to allocate irq.\n");
0447 goto err_free_mem;
0448 }
0449
0450 device_init_wakeup(&pdev->dev, true);
0451 err = dev_pm_set_wake_irq(&pdev->dev, ts_dev->irq);
0452 if (err)
0453 dev_err(&pdev->dev, "irq wake enable failed.\n");
0454
0455 titsc_writel(ts_dev, REG_IRQSTATUS, TSC_IRQENB_MASK);
0456 titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
0457 titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_EOS);
0458 err = titsc_config_wires(ts_dev);
0459 if (err) {
0460 dev_err(&pdev->dev, "wrong i/p wire configuration\n");
0461 goto err_free_irq;
0462 }
0463 titsc_step_config(ts_dev);
0464 titsc_writel(ts_dev, REG_FIFO0THR,
0465 ts_dev->coordinate_readouts * 2 + 2 - 1);
0466
0467 input_dev->name = "ti-tsc";
0468 input_dev->dev.parent = &pdev->dev;
0469
0470 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0471 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
0472
0473 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
0474 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
0475 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
0476
0477
0478 err = input_register_device(input_dev);
0479 if (err)
0480 goto err_free_irq;
0481
0482 platform_set_drvdata(pdev, ts_dev);
0483 return 0;
0484
0485 err_free_irq:
0486 dev_pm_clear_wake_irq(&pdev->dev);
0487 device_init_wakeup(&pdev->dev, false);
0488 free_irq(ts_dev->irq, ts_dev);
0489 err_free_mem:
0490 input_free_device(input_dev);
0491 kfree(ts_dev);
0492 return err;
0493 }
0494
0495 static int titsc_remove(struct platform_device *pdev)
0496 {
0497 struct titsc *ts_dev = platform_get_drvdata(pdev);
0498 u32 steps;
0499
0500 dev_pm_clear_wake_irq(&pdev->dev);
0501 device_init_wakeup(&pdev->dev, false);
0502 free_irq(ts_dev->irq, ts_dev);
0503
0504
0505 steps = 2 * ts_dev->coordinate_readouts + 2;
0506 steps = (1 << steps) - 1;
0507 am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
0508
0509 input_unregister_device(ts_dev->input);
0510
0511 kfree(ts_dev);
0512 return 0;
0513 }
0514
0515 static int __maybe_unused titsc_suspend(struct device *dev)
0516 {
0517 struct titsc *ts_dev = dev_get_drvdata(dev);
0518 unsigned int idle;
0519
0520 if (device_may_wakeup(dev)) {
0521 titsc_writel(ts_dev, REG_IRQSTATUS, TSC_IRQENB_MASK);
0522 idle = titsc_readl(ts_dev, REG_IRQENABLE);
0523 titsc_writel(ts_dev, REG_IRQENABLE,
0524 (idle | IRQENB_HW_PEN));
0525 titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
0526 }
0527 return 0;
0528 }
0529
0530 static int __maybe_unused titsc_resume(struct device *dev)
0531 {
0532 struct titsc *ts_dev = dev_get_drvdata(dev);
0533
0534 if (device_may_wakeup(dev)) {
0535 titsc_writel(ts_dev, REG_IRQWAKEUP,
0536 0x00);
0537 titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
0538 pm_relax(dev);
0539 }
0540 titsc_step_config(ts_dev);
0541 titsc_writel(ts_dev, REG_FIFO0THR,
0542 ts_dev->coordinate_readouts * 2 + 2 - 1);
0543 return 0;
0544 }
0545
0546 static SIMPLE_DEV_PM_OPS(titsc_pm_ops, titsc_suspend, titsc_resume);
0547
0548 static const struct of_device_id ti_tsc_dt_ids[] = {
0549 { .compatible = "ti,am3359-tsc", },
0550 { }
0551 };
0552 MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
0553
0554 static struct platform_driver ti_tsc_driver = {
0555 .probe = titsc_probe,
0556 .remove = titsc_remove,
0557 .driver = {
0558 .name = "TI-am335x-tsc",
0559 .pm = &titsc_pm_ops,
0560 .of_match_table = ti_tsc_dt_ids,
0561 },
0562 };
0563 module_platform_driver(ti_tsc_driver);
0564
0565 MODULE_DESCRIPTION("TI touchscreen controller driver");
0566 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
0567 MODULE_LICENSE("GPL");