Skip to content

Commit 3eac5c7

Browse files
vdsaodtor
authored andcommitted
Input: ads7846 - extend the driver for ads7845 controller support
ADS7845 is a controller for 5-wire touch screens and somewhat different from 7846. It requires three serial communications to accomplish one complete conversion. Unlike 7846 it doesn't allow Z1-/Z2- position measurement. The patch extends the ads7846 driver to also support ads7845. The packet struct is extended to contain needed command and conversion buffers. ads7846_rx() and ads7846_rx_val() now differentiate between 7845 and 7846 case. ads7846_probe() is modified to setup ads7845 specific command and conversion messages and to switch ads7845 into power-down mode, since this is needed to be prepared to respond to pendown interrupts. Signed-off-by: Anatolij Gustschin <agust@denx.de> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 0f622bf commit 3eac5c7

1 file changed

Lines changed: 135 additions & 37 deletions

File tree

‎drivers/input/touchscreen/ads7846.c‎

Lines changed: 135 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ struct ts_event {
6868
u16 y;
6969
u16 z1, z2;
7070
int ignore;
71+
u8 x_buf[3];
72+
u8 y_buf[3];
7173
};
7274

7375
/*
@@ -79,6 +81,8 @@ struct ads7846_packet {
7981
u8 read_x, read_y, read_z1, read_z2, pwrdown;
8082
u16 dummy; /* for the pwrdown read */
8183
struct ts_event tc;
84+
/* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
85+
u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
8286
};
8387

8488
struct ads7846 {
@@ -207,6 +211,14 @@ struct ser_req {
207211
struct spi_transfer xfer[6];
208212
};
209213

214+
struct ads7845_ser_req {
215+
u8 command[3];
216+
u8 pwrdown[3];
217+
u8 sample[3];
218+
struct spi_message msg;
219+
struct spi_transfer xfer[2];
220+
};
221+
210222
static void ads7846_enable(struct ads7846 *ts);
211223
static void ads7846_disable(struct ads7846 *ts);
212224

@@ -287,6 +299,41 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
287299
return status;
288300
}
289301

302+
static int ads7845_read12_ser(struct device *dev, unsigned command)
303+
{
304+
struct spi_device *spi = to_spi_device(dev);
305+
struct ads7846 *ts = dev_get_drvdata(dev);
306+
struct ads7845_ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
307+
int status;
308+
309+
if (!req)
310+
return -ENOMEM;
311+
312+
spi_message_init(&req->msg);
313+
314+
req->command[0] = (u8) command;
315+
req->xfer[0].tx_buf = req->command;
316+
req->xfer[0].rx_buf = req->sample;
317+
req->xfer[0].len = 3;
318+
spi_message_add_tail(&req->xfer[0], &req->msg);
319+
320+
ts->irq_disabled = 1;
321+
disable_irq(spi->irq);
322+
status = spi_sync(spi, &req->msg);
323+
ts->irq_disabled = 0;
324+
enable_irq(spi->irq);
325+
326+
if (status == 0) {
327+
/* BE12 value, then padding */
328+
status = be16_to_cpu(*((u16 *)&req->sample[1]));
329+
status = status >> 3;
330+
status &= 0x0fff;
331+
}
332+
333+
kfree(req);
334+
return status;
335+
}
336+
290337
#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
291338

292339
#define SHOW(name, var, adjust) static ssize_t \
@@ -540,17 +587,30 @@ static void ads7846_rx(void *ads)
540587
/* ads7846_rx_val() did in-place conversion (including byteswap) from
541588
* on-the-wire format as part of debouncing to get stable readings.
542589
*/
543-
x = packet->tc.x;
544-
y = packet->tc.y;
545-
z1 = packet->tc.z1;
546-
z2 = packet->tc.z2;
590+
if (ts->model == 7845) {
591+
x = *(u16 *)packet->tc.x_buf;
592+
y = *(u16 *)packet->tc.y_buf;
593+
z1 = 0;
594+
z2 = 0;
595+
} else {
596+
x = packet->tc.x;
597+
y = packet->tc.y;
598+
z1 = packet->tc.z1;
599+
z2 = packet->tc.z2;
600+
}
547601

548602
/* range filtering */
549603
if (x == MAX_12BIT)
550604
x = 0;
551605

552606
if (ts->model == 7843) {
553607
Rt = ts->pressure_max / 2;
608+
} else if (ts->model == 7845) {
609+
if (get_pendown_state(ts))
610+
Rt = ts->pressure_max / 2;
611+
else
612+
Rt = 0;
613+
dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
554614
} else if (likely(x && z1)) {
555615
/* compute touch pressure resistance using equation #2 */
556616
Rt = z2;
@@ -671,10 +731,14 @@ static void ads7846_rx_val(void *ads)
671731
m = &ts->msg[ts->msg_idx];
672732
t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
673733

674-
/* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
675-
* built from two 8 bit values written msb-first.
676-
*/
677-
val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
734+
if (ts->model == 7845) {
735+
val = be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
736+
} else {
737+
/* adjust: on-wire is a must-ignore bit, a BE12 value, then
738+
* padding; built from two 8 bit values written msb-first.
739+
*/
740+
val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
741+
}
678742

679743
action = ts->filter(ts->filter_data, ts->msg_idx, &val);
680744
switch (action) {
@@ -1009,16 +1073,26 @@ static int __devinit ads7846_probe(struct spi_device *spi)
10091073

10101074
spi_message_init(m);
10111075

1012-
/* y- still on; turn on only y+ (and ADC) */
1013-
packet->read_y = READ_Y(vref);
1014-
x->tx_buf = &packet->read_y;
1015-
x->len = 1;
1016-
spi_message_add_tail(x, m);
1076+
if (ts->model == 7845) {
1077+
packet->read_y_cmd[0] = READ_Y(vref);
1078+
packet->read_y_cmd[1] = 0;
1079+
packet->read_y_cmd[2] = 0;
1080+
x->tx_buf = &packet->read_y_cmd[0];
1081+
x->rx_buf = &packet->tc.y_buf[0];
1082+
x->len = 3;
1083+
spi_message_add_tail(x, m);
1084+
} else {
1085+
/* y- still on; turn on only y+ (and ADC) */
1086+
packet->read_y = READ_Y(vref);
1087+
x->tx_buf = &packet->read_y;
1088+
x->len = 1;
1089+
spi_message_add_tail(x, m);
10171090

1018-
x++;
1019-
x->rx_buf = &packet->tc.y;
1020-
x->len = 2;
1021-
spi_message_add_tail(x, m);
1091+
x++;
1092+
x->rx_buf = &packet->tc.y;
1093+
x->len = 2;
1094+
spi_message_add_tail(x, m);
1095+
}
10221096

10231097
/* the first sample after switching drivers can be low quality;
10241098
* optionally discard it, using a second one after the signals
@@ -1044,17 +1118,28 @@ static int __devinit ads7846_probe(struct spi_device *spi)
10441118
m++;
10451119
spi_message_init(m);
10461120

1047-
/* turn y- off, x+ on, then leave in lowpower */
1048-
x++;
1049-
packet->read_x = READ_X(vref);
1050-
x->tx_buf = &packet->read_x;
1051-
x->len = 1;
1052-
spi_message_add_tail(x, m);
1121+
if (ts->model == 7845) {
1122+
x++;
1123+
packet->read_x_cmd[0] = READ_X(vref);
1124+
packet->read_x_cmd[1] = 0;
1125+
packet->read_x_cmd[2] = 0;
1126+
x->tx_buf = &packet->read_x_cmd[0];
1127+
x->rx_buf = &packet->tc.x_buf[0];
1128+
x->len = 3;
1129+
spi_message_add_tail(x, m);
1130+
} else {
1131+
/* turn y- off, x+ on, then leave in lowpower */
1132+
x++;
1133+
packet->read_x = READ_X(vref);
1134+
x->tx_buf = &packet->read_x;
1135+
x->len = 1;
1136+
spi_message_add_tail(x, m);
10531137

1054-
x++;
1055-
x->rx_buf = &packet->tc.x;
1056-
x->len = 2;
1057-
spi_message_add_tail(x, m);
1138+
x++;
1139+
x->rx_buf = &packet->tc.x;
1140+
x->len = 2;
1141+
spi_message_add_tail(x, m);
1142+
}
10581143

10591144
/* ... maybe discard first sample ... */
10601145
if (pdata->settle_delay_usecs) {
@@ -1145,15 +1230,25 @@ static int __devinit ads7846_probe(struct spi_device *spi)
11451230
m++;
11461231
spi_message_init(m);
11471232

1148-
x++;
1149-
packet->pwrdown = PWRDOWN;
1150-
x->tx_buf = &packet->pwrdown;
1151-
x->len = 1;
1152-
spi_message_add_tail(x, m);
1233+
if (ts->model == 7845) {
1234+
x++;
1235+
packet->pwrdown_cmd[0] = PWRDOWN;
1236+
packet->pwrdown_cmd[1] = 0;
1237+
packet->pwrdown_cmd[2] = 0;
1238+
x->tx_buf = &packet->pwrdown_cmd[0];
1239+
x->len = 3;
1240+
} else {
1241+
x++;
1242+
packet->pwrdown = PWRDOWN;
1243+
x->tx_buf = &packet->pwrdown;
1244+
x->len = 1;
1245+
spi_message_add_tail(x, m);
1246+
1247+
x++;
1248+
x->rx_buf = &packet->dummy;
1249+
x->len = 2;
1250+
}
11531251

1154-
x++;
1155-
x->rx_buf = &packet->dummy;
1156-
x->len = 2;
11571252
CS_CHANGE(*x);
11581253
spi_message_add_tail(x, m);
11591254

@@ -1202,8 +1297,11 @@ static int __devinit ads7846_probe(struct spi_device *spi)
12021297
/* take a first sample, leaving nPENIRQ active and vREF off; avoid
12031298
* the touchscreen, in case it's not connected.
12041299
*/
1205-
(void) ads7846_read12_ser(&spi->dev,
1206-
READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1300+
if (ts->model == 7845)
1301+
ads7845_read12_ser(&spi->dev, PWRDOWN);
1302+
else
1303+
(void) ads7846_read12_ser(&spi->dev,
1304+
READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
12071305

12081306
err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
12091307
if (err)

0 commit comments

Comments
 (0)