0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/types.h>
0018 #include <linux/hwmon.h>
0019 #include <linux/err.h>
0020 #include <linux/sched.h>
0021 #include <linux/delay.h>
0022 #include <linux/input.h>
0023 #include <linux/input/touchscreen.h>
0024 #include <linux/interrupt.h>
0025 #include <linux/slab.h>
0026 #include <linux/pm.h>
0027 #include <linux/of.h>
0028 #include <linux/of_gpio.h>
0029 #include <linux/of_device.h>
0030 #include <linux/gpio.h>
0031 #include <linux/spi/spi.h>
0032 #include <linux/spi/ads7846.h>
0033 #include <linux/regulator/consumer.h>
0034 #include <linux/module.h>
0035 #include <asm/unaligned.h>
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 #define TS_POLL_DELAY 1
0060 #define TS_POLL_PERIOD 5
0061
0062
0063 #define SAMPLE_BITS (8 + 16 + 2 )
0064
0065 struct ads7846_buf {
0066 u8 cmd;
0067 __be16 data;
0068 } __packed;
0069
0070 struct ads7846_buf_layout {
0071 unsigned int offset;
0072 unsigned int count;
0073 unsigned int skip;
0074 };
0075
0076
0077
0078
0079
0080
0081 struct ads7846_packet {
0082 unsigned int count;
0083 unsigned int count_skip;
0084 unsigned int cmds;
0085 unsigned int last_cmd_idx;
0086 struct ads7846_buf_layout l[5];
0087 struct ads7846_buf *rx;
0088 struct ads7846_buf *tx;
0089
0090 struct ads7846_buf pwrdown_cmd;
0091
0092 bool ignore;
0093 u16 x, y, z1, z2;
0094 };
0095
0096 struct ads7846 {
0097 struct input_dev *input;
0098 char phys[32];
0099 char name[32];
0100
0101 struct spi_device *spi;
0102 struct regulator *reg;
0103
0104 u16 model;
0105 u16 vref_mv;
0106 u16 vref_delay_usecs;
0107 u16 x_plate_ohms;
0108 u16 pressure_max;
0109
0110 bool swap_xy;
0111 bool use_internal;
0112
0113 struct ads7846_packet *packet;
0114
0115 struct spi_transfer xfer[18];
0116 struct spi_message msg[5];
0117 int msg_count;
0118 wait_queue_head_t wait;
0119
0120 bool pendown;
0121
0122 int read_cnt;
0123 int read_rep;
0124 int last_read;
0125
0126 u16 debounce_max;
0127 u16 debounce_tol;
0128 u16 debounce_rep;
0129
0130 u16 penirq_recheck_delay_usecs;
0131
0132 struct touchscreen_properties core_prop;
0133
0134 struct mutex lock;
0135 bool stopped;
0136 bool disabled;
0137 bool suspended;
0138
0139 int (*filter)(void *data, int data_idx, int *val);
0140 void *filter_data;
0141 int (*get_pendown_state)(void);
0142 int gpio_pendown;
0143
0144 void (*wait_for_sync)(void);
0145 };
0146
0147 enum ads7846_filter {
0148 ADS7846_FILTER_OK,
0149 ADS7846_FILTER_REPEAT,
0150 ADS7846_FILTER_IGNORE,
0151 };
0152
0153
0154 #if 0
0155 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
0156 #else
0157 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
0158 #endif
0159
0160
0161
0162
0163
0164
0165 #define ADS_START (1 << 7)
0166 #define ADS_A2A1A0_d_y (1 << 4)
0167 #define ADS_A2A1A0_d_z1 (3 << 4)
0168 #define ADS_A2A1A0_d_z2 (4 << 4)
0169 #define ADS_A2A1A0_d_x (5 << 4)
0170 #define ADS_A2A1A0_temp0 (0 << 4)
0171 #define ADS_A2A1A0_vbatt (2 << 4)
0172 #define ADS_A2A1A0_vaux (6 << 4)
0173 #define ADS_A2A1A0_temp1 (7 << 4)
0174 #define ADS_8_BIT (1 << 3)
0175 #define ADS_12_BIT (0 << 3)
0176 #define ADS_SER (1 << 2)
0177 #define ADS_DFR (0 << 2)
0178 #define ADS_PD10_PDOWN (0 << 0)
0179 #define ADS_PD10_ADC_ON (1 << 0)
0180 #define ADS_PD10_REF_ON (2 << 0)
0181 #define ADS_PD10_ALL_ON (3 << 0)
0182
0183 #define MAX_12BIT ((1<<12)-1)
0184
0185
0186 #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
0187 | ADS_12_BIT | ADS_DFR | \
0188 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
0189
0190 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
0191 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
0192 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
0193 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
0194 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0))
0195
0196
0197
0198
0199 #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
0200 | ADS_12_BIT | ADS_SER)
0201
0202 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
0203 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
0204
0205
0206
0207
0208
0209
0210
0211
0212 enum ads7846_cmds {
0213 ADS7846_X,
0214 ADS7846_Y,
0215 ADS7846_Z1,
0216 ADS7846_Z2,
0217 ADS7846_PWDOWN,
0218 };
0219
0220 static int get_pendown_state(struct ads7846 *ts)
0221 {
0222 if (ts->get_pendown_state)
0223 return ts->get_pendown_state();
0224
0225 return !gpio_get_value(ts->gpio_pendown);
0226 }
0227
0228 static void ads7846_report_pen_up(struct ads7846 *ts)
0229 {
0230 struct input_dev *input = ts->input;
0231
0232 input_report_key(input, BTN_TOUCH, 0);
0233 input_report_abs(input, ABS_PRESSURE, 0);
0234 input_sync(input);
0235
0236 ts->pendown = false;
0237 dev_vdbg(&ts->spi->dev, "UP\n");
0238 }
0239
0240
0241 static void ads7846_stop(struct ads7846 *ts)
0242 {
0243 if (!ts->disabled && !ts->suspended) {
0244
0245 ts->stopped = true;
0246 mb();
0247 wake_up(&ts->wait);
0248 disable_irq(ts->spi->irq);
0249 }
0250 }
0251
0252
0253 static void ads7846_restart(struct ads7846 *ts)
0254 {
0255 if (!ts->disabled && !ts->suspended) {
0256
0257 if (ts->pendown && !get_pendown_state(ts))
0258 ads7846_report_pen_up(ts);
0259
0260
0261 ts->stopped = false;
0262 mb();
0263 enable_irq(ts->spi->irq);
0264 }
0265 }
0266
0267
0268 static void __ads7846_disable(struct ads7846 *ts)
0269 {
0270 ads7846_stop(ts);
0271 regulator_disable(ts->reg);
0272
0273
0274
0275
0276
0277 }
0278
0279
0280 static void __ads7846_enable(struct ads7846 *ts)
0281 {
0282 int error;
0283
0284 error = regulator_enable(ts->reg);
0285 if (error != 0)
0286 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
0287
0288 ads7846_restart(ts);
0289 }
0290
0291 static void ads7846_disable(struct ads7846 *ts)
0292 {
0293 mutex_lock(&ts->lock);
0294
0295 if (!ts->disabled) {
0296
0297 if (!ts->suspended)
0298 __ads7846_disable(ts);
0299
0300 ts->disabled = true;
0301 }
0302
0303 mutex_unlock(&ts->lock);
0304 }
0305
0306 static void ads7846_enable(struct ads7846 *ts)
0307 {
0308 mutex_lock(&ts->lock);
0309
0310 if (ts->disabled) {
0311
0312 ts->disabled = false;
0313
0314 if (!ts->suspended)
0315 __ads7846_enable(ts);
0316 }
0317
0318 mutex_unlock(&ts->lock);
0319 }
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329 struct ser_req {
0330 u8 ref_on;
0331 u8 command;
0332 u8 ref_off;
0333 u16 scratch;
0334 struct spi_message msg;
0335 struct spi_transfer xfer[6];
0336
0337
0338
0339
0340 __be16 sample ____cacheline_aligned;
0341 };
0342
0343 struct ads7845_ser_req {
0344 u8 command[3];
0345 struct spi_message msg;
0346 struct spi_transfer xfer[2];
0347
0348
0349
0350
0351 u8 sample[3] ____cacheline_aligned;
0352 };
0353
0354 static int ads7846_read12_ser(struct device *dev, unsigned command)
0355 {
0356 struct spi_device *spi = to_spi_device(dev);
0357 struct ads7846 *ts = dev_get_drvdata(dev);
0358 struct ser_req *req;
0359 int status;
0360
0361 req = kzalloc(sizeof *req, GFP_KERNEL);
0362 if (!req)
0363 return -ENOMEM;
0364
0365 spi_message_init(&req->msg);
0366
0367
0368 if (ts->use_internal) {
0369 req->ref_on = REF_ON;
0370 req->xfer[0].tx_buf = &req->ref_on;
0371 req->xfer[0].len = 1;
0372 spi_message_add_tail(&req->xfer[0], &req->msg);
0373
0374 req->xfer[1].rx_buf = &req->scratch;
0375 req->xfer[1].len = 2;
0376
0377
0378 req->xfer[1].delay.value = ts->vref_delay_usecs;
0379 req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
0380 spi_message_add_tail(&req->xfer[1], &req->msg);
0381
0382
0383 command |= ADS_PD10_REF_ON;
0384 }
0385
0386
0387 command |= ADS_PD10_ADC_ON;
0388
0389
0390 req->command = (u8) command;
0391 req->xfer[2].tx_buf = &req->command;
0392 req->xfer[2].len = 1;
0393 spi_message_add_tail(&req->xfer[2], &req->msg);
0394
0395 req->xfer[3].rx_buf = &req->sample;
0396 req->xfer[3].len = 2;
0397 spi_message_add_tail(&req->xfer[3], &req->msg);
0398
0399
0400
0401
0402 req->ref_off = PWRDOWN;
0403 req->xfer[4].tx_buf = &req->ref_off;
0404 req->xfer[4].len = 1;
0405 spi_message_add_tail(&req->xfer[4], &req->msg);
0406
0407 req->xfer[5].rx_buf = &req->scratch;
0408 req->xfer[5].len = 2;
0409 CS_CHANGE(req->xfer[5]);
0410 spi_message_add_tail(&req->xfer[5], &req->msg);
0411
0412 mutex_lock(&ts->lock);
0413 ads7846_stop(ts);
0414 status = spi_sync(spi, &req->msg);
0415 ads7846_restart(ts);
0416 mutex_unlock(&ts->lock);
0417
0418 if (status == 0) {
0419
0420 status = be16_to_cpu(req->sample);
0421 status = status >> 3;
0422 status &= 0x0fff;
0423 }
0424
0425 kfree(req);
0426 return status;
0427 }
0428
0429 static int ads7845_read12_ser(struct device *dev, unsigned command)
0430 {
0431 struct spi_device *spi = to_spi_device(dev);
0432 struct ads7846 *ts = dev_get_drvdata(dev);
0433 struct ads7845_ser_req *req;
0434 int status;
0435
0436 req = kzalloc(sizeof *req, GFP_KERNEL);
0437 if (!req)
0438 return -ENOMEM;
0439
0440 spi_message_init(&req->msg);
0441
0442 req->command[0] = (u8) command;
0443 req->xfer[0].tx_buf = req->command;
0444 req->xfer[0].rx_buf = req->sample;
0445 req->xfer[0].len = 3;
0446 spi_message_add_tail(&req->xfer[0], &req->msg);
0447
0448 mutex_lock(&ts->lock);
0449 ads7846_stop(ts);
0450 status = spi_sync(spi, &req->msg);
0451 ads7846_restart(ts);
0452 mutex_unlock(&ts->lock);
0453
0454 if (status == 0) {
0455
0456 status = get_unaligned_be16(&req->sample[1]);
0457 status = status >> 3;
0458 status &= 0x0fff;
0459 }
0460
0461 kfree(req);
0462 return status;
0463 }
0464
0465 #if IS_ENABLED(CONFIG_HWMON)
0466
0467 #define SHOW(name, var, adjust) static ssize_t \
0468 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
0469 { \
0470 struct ads7846 *ts = dev_get_drvdata(dev); \
0471 ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
0472 READ_12BIT_SER(var)); \
0473 if (v < 0) \
0474 return v; \
0475 return sprintf(buf, "%u\n", adjust(ts, v)); \
0476 } \
0477 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
0478
0479
0480
0481
0482
0483
0484
0485 static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
0486 {
0487 return v;
0488 }
0489
0490 SHOW(temp0, temp0, null_adjust)
0491 SHOW(temp1, temp1, null_adjust)
0492
0493
0494
0495
0496
0497
0498 static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
0499 {
0500 unsigned retval = v;
0501
0502
0503 retval *= ts->vref_mv;
0504 retval = retval >> 12;
0505
0506 return retval;
0507 }
0508
0509 static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
0510 {
0511 unsigned retval = vaux_adjust(ts, v);
0512
0513
0514 if (ts->model == 7846)
0515 retval *= 4;
0516
0517 return retval;
0518 }
0519
0520 SHOW(in0_input, vaux, vaux_adjust)
0521 SHOW(in1_input, vbatt, vbatt_adjust)
0522
0523 static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
0524 int index)
0525 {
0526 struct device *dev = kobj_to_dev(kobj);
0527 struct ads7846 *ts = dev_get_drvdata(dev);
0528
0529 if (ts->model == 7843 && index < 2)
0530 return 0;
0531 if (ts->model == 7845 && index != 2)
0532 return 0;
0533
0534 return attr->mode;
0535 }
0536
0537 static struct attribute *ads7846_attributes[] = {
0538 &dev_attr_temp0.attr,
0539 &dev_attr_temp1.attr,
0540 &dev_attr_in0_input.attr,
0541 &dev_attr_in1_input.attr,
0542 NULL,
0543 };
0544
0545 static const struct attribute_group ads7846_attr_group = {
0546 .attrs = ads7846_attributes,
0547 .is_visible = ads7846_is_visible,
0548 };
0549 __ATTRIBUTE_GROUPS(ads7846_attr);
0550
0551 static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
0552 {
0553 struct device *hwmon;
0554
0555
0556 switch (ts->model) {
0557 case 7846:
0558 if (!ts->vref_mv) {
0559 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
0560 ts->vref_mv = 2500;
0561 ts->use_internal = true;
0562 }
0563 break;
0564 case 7845:
0565 case 7843:
0566 if (!ts->vref_mv) {
0567 dev_warn(&spi->dev,
0568 "external vREF for ADS%d not specified\n",
0569 ts->model);
0570 return 0;
0571 }
0572 break;
0573 }
0574
0575 hwmon = devm_hwmon_device_register_with_groups(&spi->dev,
0576 spi->modalias, ts,
0577 ads7846_attr_groups);
0578
0579 return PTR_ERR_OR_ZERO(hwmon);
0580 }
0581
0582 #else
0583 static inline int ads784x_hwmon_register(struct spi_device *spi,
0584 struct ads7846 *ts)
0585 {
0586 return 0;
0587 }
0588 #endif
0589
0590 static ssize_t ads7846_pen_down_show(struct device *dev,
0591 struct device_attribute *attr, char *buf)
0592 {
0593 struct ads7846 *ts = dev_get_drvdata(dev);
0594
0595 return sprintf(buf, "%u\n", ts->pendown);
0596 }
0597
0598 static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
0599
0600 static ssize_t ads7846_disable_show(struct device *dev,
0601 struct device_attribute *attr, char *buf)
0602 {
0603 struct ads7846 *ts = dev_get_drvdata(dev);
0604
0605 return sprintf(buf, "%u\n", ts->disabled);
0606 }
0607
0608 static ssize_t ads7846_disable_store(struct device *dev,
0609 struct device_attribute *attr,
0610 const char *buf, size_t count)
0611 {
0612 struct ads7846 *ts = dev_get_drvdata(dev);
0613 unsigned int i;
0614 int err;
0615
0616 err = kstrtouint(buf, 10, &i);
0617 if (err)
0618 return err;
0619
0620 if (i)
0621 ads7846_disable(ts);
0622 else
0623 ads7846_enable(ts);
0624
0625 return count;
0626 }
0627
0628 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
0629
0630 static struct attribute *ads784x_attributes[] = {
0631 &dev_attr_pen_down.attr,
0632 &dev_attr_disable.attr,
0633 NULL,
0634 };
0635
0636 static const struct attribute_group ads784x_attr_group = {
0637 .attrs = ads784x_attributes,
0638 };
0639
0640
0641
0642 static void null_wait_for_sync(void)
0643 {
0644 }
0645
0646 static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
0647 {
0648 struct ads7846 *ts = ads;
0649
0650 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
0651
0652 ts->read_rep = 0;
0653
0654
0655
0656
0657 if (ts->read_cnt < ts->debounce_max) {
0658 ts->last_read = *val;
0659 ts->read_cnt++;
0660 return ADS7846_FILTER_REPEAT;
0661 } else {
0662
0663
0664
0665
0666
0667
0668 ts->read_cnt = 0;
0669 return ADS7846_FILTER_IGNORE;
0670 }
0671 } else {
0672 if (++ts->read_rep > ts->debounce_rep) {
0673
0674
0675
0676
0677 ts->read_cnt = 0;
0678 ts->read_rep = 0;
0679 return ADS7846_FILTER_OK;
0680 } else {
0681
0682 ts->read_cnt++;
0683 return ADS7846_FILTER_REPEAT;
0684 }
0685 }
0686 }
0687
0688 static int ads7846_no_filter(void *ads, int data_idx, int *val)
0689 {
0690 return ADS7846_FILTER_OK;
0691 }
0692
0693 static int ads7846_get_value(struct ads7846_buf *buf)
0694 {
0695 int value;
0696
0697 value = be16_to_cpup(&buf->data);
0698
0699
0700 return (value >> 3) & 0xfff;
0701 }
0702
0703 static void ads7846_set_cmd_val(struct ads7846 *ts, enum ads7846_cmds cmd_idx,
0704 u16 val)
0705 {
0706 struct ads7846_packet *packet = ts->packet;
0707
0708 switch (cmd_idx) {
0709 case ADS7846_Y:
0710 packet->y = val;
0711 break;
0712 case ADS7846_X:
0713 packet->x = val;
0714 break;
0715 case ADS7846_Z1:
0716 packet->z1 = val;
0717 break;
0718 case ADS7846_Z2:
0719 packet->z2 = val;
0720 break;
0721 default:
0722 WARN_ON_ONCE(1);
0723 }
0724 }
0725
0726 static u8 ads7846_get_cmd(enum ads7846_cmds cmd_idx, int vref)
0727 {
0728 switch (cmd_idx) {
0729 case ADS7846_Y:
0730 return READ_Y(vref);
0731 case ADS7846_X:
0732 return READ_X(vref);
0733
0734
0735 case ADS7846_Z1:
0736 return READ_Z1(vref);
0737 case ADS7846_Z2:
0738 return READ_Z2(vref);
0739 case ADS7846_PWDOWN:
0740 return PWRDOWN;
0741 default:
0742 WARN_ON_ONCE(1);
0743 }
0744
0745 return 0;
0746 }
0747
0748 static bool ads7846_cmd_need_settle(enum ads7846_cmds cmd_idx)
0749 {
0750 switch (cmd_idx) {
0751 case ADS7846_X:
0752 case ADS7846_Y:
0753 case ADS7846_Z1:
0754 case ADS7846_Z2:
0755 return true;
0756 case ADS7846_PWDOWN:
0757 return false;
0758 default:
0759 WARN_ON_ONCE(1);
0760 }
0761
0762 return false;
0763 }
0764
0765 static int ads7846_filter(struct ads7846 *ts)
0766 {
0767 struct ads7846_packet *packet = ts->packet;
0768 int action;
0769 int val;
0770 unsigned int cmd_idx, b;
0771
0772 packet->ignore = false;
0773 for (cmd_idx = packet->last_cmd_idx; cmd_idx < packet->cmds - 1; cmd_idx++) {
0774 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
0775
0776 packet->last_cmd_idx = cmd_idx;
0777
0778 for (b = l->skip; b < l->count; b++) {
0779 val = ads7846_get_value(&packet->rx[l->offset + b]);
0780
0781 action = ts->filter(ts->filter_data, cmd_idx, &val);
0782 if (action == ADS7846_FILTER_REPEAT) {
0783 if (b == l->count - 1)
0784 return -EAGAIN;
0785 } else if (action == ADS7846_FILTER_OK) {
0786 ads7846_set_cmd_val(ts, cmd_idx, val);
0787 break;
0788 } else {
0789 packet->ignore = true;
0790 return 0;
0791 }
0792 }
0793 }
0794
0795 return 0;
0796 }
0797
0798 static void ads7846_read_state(struct ads7846 *ts)
0799 {
0800 struct ads7846_packet *packet = ts->packet;
0801 struct spi_message *m;
0802 int msg_idx = 0;
0803 int error;
0804
0805 packet->last_cmd_idx = 0;
0806
0807 while (true) {
0808 ts->wait_for_sync();
0809
0810 m = &ts->msg[msg_idx];
0811 error = spi_sync(ts->spi, m);
0812 if (error) {
0813 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
0814 packet->ignore = true;
0815 return;
0816 }
0817
0818 error = ads7846_filter(ts);
0819 if (error)
0820 continue;
0821
0822 return;
0823 }
0824 }
0825
0826 static void ads7846_report_state(struct ads7846 *ts)
0827 {
0828 struct ads7846_packet *packet = ts->packet;
0829 unsigned int Rt;
0830 u16 x, y, z1, z2;
0831
0832 x = packet->x;
0833 y = packet->y;
0834 if (ts->model == 7845) {
0835 z1 = 0;
0836 z2 = 0;
0837 } else {
0838 z1 = packet->z1;
0839 z2 = packet->z2;
0840 }
0841
0842
0843 if (x == MAX_12BIT)
0844 x = 0;
0845
0846 if (ts->model == 7843) {
0847 Rt = ts->pressure_max / 2;
0848 } else if (ts->model == 7845) {
0849 if (get_pendown_state(ts))
0850 Rt = ts->pressure_max / 2;
0851 else
0852 Rt = 0;
0853 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
0854 } else if (likely(x && z1)) {
0855
0856 Rt = z2;
0857 Rt -= z1;
0858 Rt *= ts->x_plate_ohms;
0859 Rt = DIV_ROUND_CLOSEST(Rt, 16);
0860 Rt *= x;
0861 Rt /= z1;
0862 Rt = DIV_ROUND_CLOSEST(Rt, 256);
0863 } else {
0864 Rt = 0;
0865 }
0866
0867
0868
0869
0870
0871
0872 if (packet->ignore || Rt > ts->pressure_max) {
0873 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
0874 packet->ignore, Rt);
0875 return;
0876 }
0877
0878
0879
0880
0881
0882 if (ts->penirq_recheck_delay_usecs) {
0883 udelay(ts->penirq_recheck_delay_usecs);
0884 if (!get_pendown_state(ts))
0885 Rt = 0;
0886 }
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897 if (Rt) {
0898 struct input_dev *input = ts->input;
0899
0900 if (!ts->pendown) {
0901 input_report_key(input, BTN_TOUCH, 1);
0902 ts->pendown = true;
0903 dev_vdbg(&ts->spi->dev, "DOWN\n");
0904 }
0905
0906 touchscreen_report_pos(input, &ts->core_prop, x, y, false);
0907 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
0908
0909 input_sync(input);
0910 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
0911 }
0912 }
0913
0914 static irqreturn_t ads7846_hard_irq(int irq, void *handle)
0915 {
0916 struct ads7846 *ts = handle;
0917
0918 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
0919 }
0920
0921
0922 static irqreturn_t ads7846_irq(int irq, void *handle)
0923 {
0924 struct ads7846 *ts = handle;
0925
0926
0927 msleep(TS_POLL_DELAY);
0928
0929 while (!ts->stopped && get_pendown_state(ts)) {
0930
0931
0932 ads7846_read_state(ts);
0933
0934 if (!ts->stopped)
0935 ads7846_report_state(ts);
0936
0937 wait_event_timeout(ts->wait, ts->stopped,
0938 msecs_to_jiffies(TS_POLL_PERIOD));
0939 }
0940
0941 if (ts->pendown && !ts->stopped)
0942 ads7846_report_pen_up(ts);
0943
0944 return IRQ_HANDLED;
0945 }
0946
0947 static int __maybe_unused ads7846_suspend(struct device *dev)
0948 {
0949 struct ads7846 *ts = dev_get_drvdata(dev);
0950
0951 mutex_lock(&ts->lock);
0952
0953 if (!ts->suspended) {
0954
0955 if (!ts->disabled)
0956 __ads7846_disable(ts);
0957
0958 if (device_may_wakeup(&ts->spi->dev))
0959 enable_irq_wake(ts->spi->irq);
0960
0961 ts->suspended = true;
0962 }
0963
0964 mutex_unlock(&ts->lock);
0965
0966 return 0;
0967 }
0968
0969 static int __maybe_unused ads7846_resume(struct device *dev)
0970 {
0971 struct ads7846 *ts = dev_get_drvdata(dev);
0972
0973 mutex_lock(&ts->lock);
0974
0975 if (ts->suspended) {
0976
0977 ts->suspended = false;
0978
0979 if (device_may_wakeup(&ts->spi->dev))
0980 disable_irq_wake(ts->spi->irq);
0981
0982 if (!ts->disabled)
0983 __ads7846_enable(ts);
0984 }
0985
0986 mutex_unlock(&ts->lock);
0987
0988 return 0;
0989 }
0990
0991 static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
0992
0993 static int ads7846_setup_pendown(struct spi_device *spi,
0994 struct ads7846 *ts,
0995 const struct ads7846_platform_data *pdata)
0996 {
0997 int err;
0998
0999
1000
1001
1002
1003
1004
1005 if (pdata->get_pendown_state) {
1006 ts->get_pendown_state = pdata->get_pendown_state;
1007 } else if (gpio_is_valid(pdata->gpio_pendown)) {
1008
1009 err = devm_gpio_request_one(&spi->dev, pdata->gpio_pendown,
1010 GPIOF_IN, "ads7846_pendown");
1011 if (err) {
1012 dev_err(&spi->dev,
1013 "failed to request/setup pendown GPIO%d: %d\n",
1014 pdata->gpio_pendown, err);
1015 return err;
1016 }
1017
1018 ts->gpio_pendown = pdata->gpio_pendown;
1019
1020 if (pdata->gpio_pendown_debounce)
1021 gpio_set_debounce(pdata->gpio_pendown,
1022 pdata->gpio_pendown_debounce);
1023 } else {
1024 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
1025 return -EINVAL;
1026 }
1027
1028 return 0;
1029 }
1030
1031
1032
1033
1034
1035 static int ads7846_setup_spi_msg(struct ads7846 *ts,
1036 const struct ads7846_platform_data *pdata)
1037 {
1038 struct spi_message *m = &ts->msg[0];
1039 struct spi_transfer *x = ts->xfer;
1040 struct ads7846_packet *packet = ts->packet;
1041 int vref = pdata->keep_vref_on;
1042 unsigned int count, offset = 0;
1043 unsigned int cmd_idx, b;
1044 unsigned long time;
1045 size_t size = 0;
1046
1047
1048 time = NSEC_PER_SEC / ts->spi->max_speed_hz;
1049
1050 count = pdata->settle_delay_usecs * NSEC_PER_USEC / time;
1051 packet->count_skip = DIV_ROUND_UP(count, 24);
1052
1053 if (ts->debounce_max && ts->debounce_rep)
1054
1055
1056 packet->count = ts->debounce_rep + 2;
1057 else
1058 packet->count = 1;
1059
1060 if (ts->model == 7846)
1061 packet->cmds = 5;
1062 else
1063 packet->cmds = 3;
1064
1065 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1066 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1067 unsigned int max_count;
1068
1069 if (ads7846_cmd_need_settle(cmd_idx))
1070 max_count = packet->count + packet->count_skip;
1071 else
1072 max_count = packet->count;
1073
1074 l->offset = offset;
1075 offset += max_count;
1076 l->count = max_count;
1077 l->skip = packet->count_skip;
1078 size += sizeof(*packet->tx) * max_count;
1079 }
1080
1081 packet->tx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1082 if (!packet->tx)
1083 return -ENOMEM;
1084
1085 packet->rx = devm_kzalloc(&ts->spi->dev, size, GFP_KERNEL);
1086 if (!packet->rx)
1087 return -ENOMEM;
1088
1089 if (ts->model == 7873) {
1090
1091
1092
1093
1094
1095 ts->model = 7846;
1096 vref = 0;
1097 }
1098
1099 ts->msg_count = 1;
1100 spi_message_init(m);
1101 m->context = ts;
1102
1103 for (cmd_idx = 0; cmd_idx < packet->cmds; cmd_idx++) {
1104 struct ads7846_buf_layout *l = &packet->l[cmd_idx];
1105 u8 cmd = ads7846_get_cmd(cmd_idx, vref);
1106
1107 for (b = 0; b < l->count; b++)
1108 packet->tx[l->offset + b].cmd = cmd;
1109 }
1110
1111 x->tx_buf = packet->tx;
1112 x->rx_buf = packet->rx;
1113 x->len = size;
1114 spi_message_add_tail(x, m);
1115
1116 return 0;
1117 }
1118
1119 #ifdef CONFIG_OF
1120 static const struct of_device_id ads7846_dt_ids[] = {
1121 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1122 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1123 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1124 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1125 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1126 { }
1127 };
1128 MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1129
1130 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1131 {
1132 struct ads7846_platform_data *pdata;
1133 struct device_node *node = dev->of_node;
1134 const struct of_device_id *match;
1135 u32 value;
1136
1137 if (!node) {
1138 dev_err(dev, "Device does not have associated DT data\n");
1139 return ERR_PTR(-EINVAL);
1140 }
1141
1142 match = of_match_device(ads7846_dt_ids, dev);
1143 if (!match) {
1144 dev_err(dev, "Unknown device model\n");
1145 return ERR_PTR(-EINVAL);
1146 }
1147
1148 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1149 if (!pdata)
1150 return ERR_PTR(-ENOMEM);
1151
1152 pdata->model = (unsigned long)match->data;
1153
1154 of_property_read_u16(node, "ti,vref-delay-usecs",
1155 &pdata->vref_delay_usecs);
1156 of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1157 pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1158
1159 pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1160
1161 of_property_read_u16(node, "ti,settle-delay-usec",
1162 &pdata->settle_delay_usecs);
1163 of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1164 &pdata->penirq_recheck_delay_usecs);
1165
1166 of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1167 of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1168
1169 of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1170 of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1171 of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1172 of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1173
1174
1175
1176
1177
1178 of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1179 if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
1180 pdata->pressure_min = (u16) value;
1181 of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1182
1183 of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1184 if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
1185 pdata->debounce_max = (u16) value;
1186 of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1187 of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1188
1189 of_property_read_u32(node, "ti,pendown-gpio-debounce",
1190 &pdata->gpio_pendown_debounce);
1191
1192 pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
1193 of_property_read_bool(node, "linux,wakeup");
1194
1195 pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1196
1197 return pdata;
1198 }
1199 #else
1200 static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1201 {
1202 dev_err(dev, "no platform data defined\n");
1203 return ERR_PTR(-EINVAL);
1204 }
1205 #endif
1206
1207 static void ads7846_regulator_disable(void *regulator)
1208 {
1209 regulator_disable(regulator);
1210 }
1211
1212 static int ads7846_probe(struct spi_device *spi)
1213 {
1214 const struct ads7846_platform_data *pdata;
1215 struct ads7846 *ts;
1216 struct device *dev = &spi->dev;
1217 struct ads7846_packet *packet;
1218 struct input_dev *input_dev;
1219 unsigned long irq_flags;
1220 int err;
1221
1222 if (!spi->irq) {
1223 dev_dbg(dev, "no IRQ?\n");
1224 return -EINVAL;
1225 }
1226
1227
1228 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1229 dev_err(dev, "f(sample) %d KHz?\n",
1230 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1231 return -EINVAL;
1232 }
1233
1234
1235
1236
1237
1238
1239 spi->bits_per_word = 8;
1240 spi->mode &= ~SPI_MODE_X_MASK;
1241 spi->mode |= SPI_MODE_0;
1242 err = spi_setup(spi);
1243 if (err < 0)
1244 return err;
1245
1246 ts = devm_kzalloc(dev, sizeof(struct ads7846), GFP_KERNEL);
1247 if (!ts)
1248 return -ENOMEM;
1249
1250 packet = devm_kzalloc(dev, sizeof(struct ads7846_packet), GFP_KERNEL);
1251 if (!packet)
1252 return -ENOMEM;
1253
1254 input_dev = devm_input_allocate_device(dev);
1255 if (!input_dev)
1256 return -ENOMEM;
1257
1258 spi_set_drvdata(spi, ts);
1259
1260 ts->packet = packet;
1261 ts->spi = spi;
1262 ts->input = input_dev;
1263
1264 mutex_init(&ts->lock);
1265 init_waitqueue_head(&ts->wait);
1266
1267 pdata = dev_get_platdata(dev);
1268 if (!pdata) {
1269 pdata = ads7846_probe_dt(dev);
1270 if (IS_ERR(pdata))
1271 return PTR_ERR(pdata);
1272 }
1273
1274 ts->model = pdata->model ? : 7846;
1275 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1276 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1277 ts->vref_mv = pdata->vref_mv;
1278
1279 if (pdata->debounce_max) {
1280 ts->debounce_max = pdata->debounce_max;
1281 if (ts->debounce_max < 2)
1282 ts->debounce_max = 2;
1283 ts->debounce_tol = pdata->debounce_tol;
1284 ts->debounce_rep = pdata->debounce_rep;
1285 ts->filter = ads7846_debounce_filter;
1286 ts->filter_data = ts;
1287 } else {
1288 ts->filter = ads7846_no_filter;
1289 }
1290
1291 err = ads7846_setup_pendown(spi, ts, pdata);
1292 if (err)
1293 return err;
1294
1295 if (pdata->penirq_recheck_delay_usecs)
1296 ts->penirq_recheck_delay_usecs =
1297 pdata->penirq_recheck_delay_usecs;
1298
1299 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1300
1301 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
1302 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1303
1304 input_dev->name = ts->name;
1305 input_dev->phys = ts->phys;
1306
1307 input_dev->id.bustype = BUS_SPI;
1308 input_dev->id.product = pdata->model;
1309
1310 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
1311 input_set_abs_params(input_dev, ABS_X,
1312 pdata->x_min ? : 0,
1313 pdata->x_max ? : MAX_12BIT,
1314 0, 0);
1315 input_set_abs_params(input_dev, ABS_Y,
1316 pdata->y_min ? : 0,
1317 pdata->y_max ? : MAX_12BIT,
1318 0, 0);
1319 input_set_abs_params(input_dev, ABS_PRESSURE,
1320 pdata->pressure_min, pdata->pressure_max, 0, 0);
1321
1322
1323
1324
1325
1326
1327 touchscreen_parse_properties(ts->input, false, &ts->core_prop);
1328 ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
1329
1330
1331
1332
1333
1334 if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
1335 swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
1336 ts->core_prop.swap_x_y = true;
1337 }
1338
1339 ads7846_setup_spi_msg(ts, pdata);
1340
1341 ts->reg = devm_regulator_get(dev, "vcc");
1342 if (IS_ERR(ts->reg)) {
1343 err = PTR_ERR(ts->reg);
1344 dev_err(dev, "unable to get regulator: %d\n", err);
1345 return err;
1346 }
1347
1348 err = regulator_enable(ts->reg);
1349 if (err) {
1350 dev_err(dev, "unable to enable regulator: %d\n", err);
1351 return err;
1352 }
1353
1354 err = devm_add_action_or_reset(dev, ads7846_regulator_disable, ts->reg);
1355 if (err)
1356 return err;
1357
1358 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1359 irq_flags |= IRQF_ONESHOT;
1360
1361 err = devm_request_threaded_irq(dev, spi->irq,
1362 ads7846_hard_irq, ads7846_irq,
1363 irq_flags, dev->driver->name, ts);
1364 if (err && err != -EPROBE_DEFER && !pdata->irq_flags) {
1365 dev_info(dev,
1366 "trying pin change workaround on irq %d\n", spi->irq);
1367 irq_flags |= IRQF_TRIGGER_RISING;
1368 err = devm_request_threaded_irq(dev, spi->irq,
1369 ads7846_hard_irq, ads7846_irq,
1370 irq_flags, dev->driver->name,
1371 ts);
1372 }
1373
1374 if (err) {
1375 dev_dbg(dev, "irq %d busy?\n", spi->irq);
1376 return err;
1377 }
1378
1379 err = ads784x_hwmon_register(spi, ts);
1380 if (err)
1381 return err;
1382
1383 dev_info(dev, "touchscreen, irq %d\n", spi->irq);
1384
1385
1386
1387
1388
1389 if (ts->model == 7845)
1390 ads7845_read12_ser(dev, PWRDOWN);
1391 else
1392 (void) ads7846_read12_ser(dev, READ_12BIT_SER(vaux));
1393
1394 err = devm_device_add_group(dev, &ads784x_attr_group);
1395 if (err)
1396 return err;
1397
1398 err = input_register_device(input_dev);
1399 if (err)
1400 return err;
1401
1402 device_init_wakeup(dev, pdata->wakeup);
1403
1404
1405
1406
1407
1408 if (!dev_get_platdata(dev))
1409 devm_kfree(dev, (void *)pdata);
1410
1411 return 0;
1412 }
1413
1414 static void ads7846_remove(struct spi_device *spi)
1415 {
1416 struct ads7846 *ts = spi_get_drvdata(spi);
1417
1418 ads7846_stop(ts);
1419 }
1420
1421 static struct spi_driver ads7846_driver = {
1422 .driver = {
1423 .name = "ads7846",
1424 .pm = &ads7846_pm,
1425 .of_match_table = of_match_ptr(ads7846_dt_ids),
1426 },
1427 .probe = ads7846_probe,
1428 .remove = ads7846_remove,
1429 };
1430
1431 module_spi_driver(ads7846_driver);
1432
1433 MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1434 MODULE_LICENSE("GPL");
1435 MODULE_ALIAS("spi:ads7846");