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