1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f1443404SH. Nikolaus Schaller /*
3f1443404SH. Nikolaus Schaller * Copyright (c) 2016 Golden Delicious Comp. GmbH&Co. KG
4f1443404SH. Nikolaus Schaller * Nikolaus Schaller <hns@goldelico.com>
5f1443404SH. Nikolaus Schaller */
6f1443404SH. Nikolaus Schaller
7f1443404SH. Nikolaus Schaller #include <linux/i2c.h>
8f1443404SH. Nikolaus Schaller #include <linux/iio/iio.h>
9f1443404SH. Nikolaus Schaller #include "tsc2007.h"
10f1443404SH. Nikolaus Schaller
11f1443404SH. Nikolaus Schaller struct tsc2007_iio {
12f1443404SH. Nikolaus Schaller struct tsc2007 *ts;
13f1443404SH. Nikolaus Schaller };
14f1443404SH. Nikolaus Schaller
15f1443404SH. Nikolaus Schaller #define TSC2007_CHAN_IIO(_chan, _name, _type, _chan_info) \
16f1443404SH. Nikolaus Schaller { \
17f1443404SH. Nikolaus Schaller .datasheet_name = _name, \
18f1443404SH. Nikolaus Schaller .type = _type, \
19f1443404SH. Nikolaus Schaller .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
20f1443404SH. Nikolaus Schaller BIT(_chan_info), \
21f1443404SH. Nikolaus Schaller .indexed = 1, \
22f1443404SH. Nikolaus Schaller .channel = _chan, \
23f1443404SH. Nikolaus Schaller }
24f1443404SH. Nikolaus Schaller
25f1443404SH. Nikolaus Schaller static const struct iio_chan_spec tsc2007_iio_channel[] = {
26f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(0, "x", IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
27f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(1, "y", IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
28f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(2, "z1", IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
29f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(3, "z2", IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
30f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(4, "adc", IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
31f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(5, "rt", IIO_VOLTAGE, IIO_CHAN_INFO_RAW), /* Ohms? */
32f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(6, "pen", IIO_PRESSURE, IIO_CHAN_INFO_RAW),
33f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(7, "temp0", IIO_TEMP, IIO_CHAN_INFO_RAW),
34f1443404SH. Nikolaus Schaller TSC2007_CHAN_IIO(8, "temp1", IIO_TEMP, IIO_CHAN_INFO_RAW),
35f1443404SH. Nikolaus Schaller };
36f1443404SH. Nikolaus Schaller
tsc2007_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)37f1443404SH. Nikolaus Schaller static int tsc2007_read_raw(struct iio_dev *indio_dev,
38f1443404SH. Nikolaus Schaller struct iio_chan_spec const *chan,
39f1443404SH. Nikolaus Schaller int *val, int *val2, long mask)
40f1443404SH. Nikolaus Schaller {
41f1443404SH. Nikolaus Schaller struct tsc2007_iio *iio = iio_priv(indio_dev);
42f1443404SH. Nikolaus Schaller struct tsc2007 *tsc = iio->ts;
43f1443404SH. Nikolaus Schaller int adc_chan = chan->channel;
44f1443404SH. Nikolaus Schaller int ret = 0;
45f1443404SH. Nikolaus Schaller
46f1443404SH. Nikolaus Schaller if (adc_chan >= ARRAY_SIZE(tsc2007_iio_channel))
47f1443404SH. Nikolaus Schaller return -EINVAL;
48f1443404SH. Nikolaus Schaller
49f1443404SH. Nikolaus Schaller if (mask != IIO_CHAN_INFO_RAW)
50f1443404SH. Nikolaus Schaller return -EINVAL;
51f1443404SH. Nikolaus Schaller
52f1443404SH. Nikolaus Schaller mutex_lock(&tsc->mlock);
53f1443404SH. Nikolaus Schaller
54f1443404SH. Nikolaus Schaller switch (chan->channel) {
55f1443404SH. Nikolaus Schaller case 0:
56f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc, READ_X);
57f1443404SH. Nikolaus Schaller break;
58f1443404SH. Nikolaus Schaller case 1:
59f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc, READ_Y);
60f1443404SH. Nikolaus Schaller break;
61f1443404SH. Nikolaus Schaller case 2:
62f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc, READ_Z1);
63f1443404SH. Nikolaus Schaller break;
64f1443404SH. Nikolaus Schaller case 3:
65f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc, READ_Z2);
66f1443404SH. Nikolaus Schaller break;
67f1443404SH. Nikolaus Schaller case 4:
68f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc, (ADC_ON_12BIT | TSC2007_MEASURE_AUX));
69f1443404SH. Nikolaus Schaller break;
70f1443404SH. Nikolaus Schaller case 5: {
71f1443404SH. Nikolaus Schaller struct ts_event tc;
72f1443404SH. Nikolaus Schaller
73f1443404SH. Nikolaus Schaller tc.x = tsc2007_xfer(tsc, READ_X);
74f1443404SH. Nikolaus Schaller tc.z1 = tsc2007_xfer(tsc, READ_Z1);
75f1443404SH. Nikolaus Schaller tc.z2 = tsc2007_xfer(tsc, READ_Z2);
76deec586dSH. Nikolaus Schaller *val = tsc2007_calculate_resistance(tsc, &tc);
77f1443404SH. Nikolaus Schaller break;
78f1443404SH. Nikolaus Schaller }
79f1443404SH. Nikolaus Schaller case 6:
80f1443404SH. Nikolaus Schaller *val = tsc2007_is_pen_down(tsc);
81f1443404SH. Nikolaus Schaller break;
82f1443404SH. Nikolaus Schaller case 7:
83f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc,
84f1443404SH. Nikolaus Schaller (ADC_ON_12BIT | TSC2007_MEASURE_TEMP0));
85f1443404SH. Nikolaus Schaller break;
86f1443404SH. Nikolaus Schaller case 8:
87f1443404SH. Nikolaus Schaller *val = tsc2007_xfer(tsc,
88f1443404SH. Nikolaus Schaller (ADC_ON_12BIT | TSC2007_MEASURE_TEMP1));
89f1443404SH. Nikolaus Schaller break;
90f1443404SH. Nikolaus Schaller }
91f1443404SH. Nikolaus Schaller
92f1443404SH. Nikolaus Schaller /* Prepare for next touch reading - power down ADC, enable PENIRQ */
93f1443404SH. Nikolaus Schaller tsc2007_xfer(tsc, PWRDOWN);
94f1443404SH. Nikolaus Schaller
95f1443404SH. Nikolaus Schaller mutex_unlock(&tsc->mlock);
96f1443404SH. Nikolaus Schaller
97f1443404SH. Nikolaus Schaller ret = IIO_VAL_INT;
98f1443404SH. Nikolaus Schaller
99f1443404SH. Nikolaus Schaller return ret;
100f1443404SH. Nikolaus Schaller }
101f1443404SH. Nikolaus Schaller
102f1443404SH. Nikolaus Schaller static const struct iio_info tsc2007_iio_info = {
103f1443404SH. Nikolaus Schaller .read_raw = tsc2007_read_raw,
104f1443404SH. Nikolaus Schaller };
105f1443404SH. Nikolaus Schaller
tsc2007_iio_configure(struct tsc2007 * ts)106f1443404SH. Nikolaus Schaller int tsc2007_iio_configure(struct tsc2007 *ts)
107f1443404SH. Nikolaus Schaller {
108f1443404SH. Nikolaus Schaller struct iio_dev *indio_dev;
109f1443404SH. Nikolaus Schaller struct tsc2007_iio *iio;
110f1443404SH. Nikolaus Schaller int error;
111f1443404SH. Nikolaus Schaller
112f1443404SH. Nikolaus Schaller indio_dev = devm_iio_device_alloc(&ts->client->dev, sizeof(*iio));
113f1443404SH. Nikolaus Schaller if (!indio_dev) {
114f1443404SH. Nikolaus Schaller dev_err(&ts->client->dev, "iio_device_alloc failed\n");
115f1443404SH. Nikolaus Schaller return -ENOMEM;
116f1443404SH. Nikolaus Schaller }
117f1443404SH. Nikolaus Schaller
118f1443404SH. Nikolaus Schaller iio = iio_priv(indio_dev);
119f1443404SH. Nikolaus Schaller iio->ts = ts;
120f1443404SH. Nikolaus Schaller
121f1443404SH. Nikolaus Schaller indio_dev->name = "tsc2007";
122f1443404SH. Nikolaus Schaller indio_dev->info = &tsc2007_iio_info;
123f1443404SH. Nikolaus Schaller indio_dev->modes = INDIO_DIRECT_MODE;
124f1443404SH. Nikolaus Schaller indio_dev->channels = tsc2007_iio_channel;
125f1443404SH. Nikolaus Schaller indio_dev->num_channels = ARRAY_SIZE(tsc2007_iio_channel);
126f1443404SH. Nikolaus Schaller
127f1443404SH. Nikolaus Schaller error = devm_iio_device_register(&ts->client->dev, indio_dev);
128f1443404SH. Nikolaus Schaller if (error) {
129f1443404SH. Nikolaus Schaller dev_err(&ts->client->dev,
130f1443404SH. Nikolaus Schaller "iio_device_register() failed: %d\n", error);
131f1443404SH. Nikolaus Schaller return error;
132f1443404SH. Nikolaus Schaller }
133f1443404SH. Nikolaus Schaller
134f1443404SH. Nikolaus Schaller return 0;
135f1443404SH. Nikolaus Schaller }
136