1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Golden Delicious Comp. GmbH&Co. KG
4 * Nikolaus Schaller <hns@goldelico.com>
8 #include <linux/iio/iio.h>
15 #define TSC2007_CHAN_IIO(_chan, _name, _type, _chan_info) \
17 .datasheet_name = _name, \
19 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
25 static 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
), /* Ohms? */
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
),
37 static int tsc2007_read_raw(struct iio_dev
*indio_dev
,
38 struct iio_chan_spec
const *chan
,
39 int *val
, int *val2
, long mask
)
41 struct tsc2007_iio
*iio
= iio_priv(indio_dev
);
42 struct tsc2007
*tsc
= iio
->ts
;
43 int adc_chan
= chan
->channel
;
46 if (adc_chan
>= ARRAY_SIZE(tsc2007_iio_channel
))
49 if (mask
!= IIO_CHAN_INFO_RAW
)
52 mutex_lock(&tsc
->mlock
);
54 switch (chan
->channel
) {
56 *val
= tsc2007_xfer(tsc
, READ_X
);
59 *val
= tsc2007_xfer(tsc
, READ_Y
);
62 *val
= tsc2007_xfer(tsc
, READ_Z1
);
65 *val
= tsc2007_xfer(tsc
, READ_Z2
);
68 *val
= tsc2007_xfer(tsc
, (ADC_ON_12BIT
| TSC2007_MEASURE_AUX
));
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
);
80 *val
= tsc2007_is_pen_down(tsc
);
83 *val
= tsc2007_xfer(tsc
,
84 (ADC_ON_12BIT
| TSC2007_MEASURE_TEMP0
));
87 *val
= tsc2007_xfer(tsc
,
88 (ADC_ON_12BIT
| TSC2007_MEASURE_TEMP1
));
92 /* Prepare for next touch reading - power down ADC, enable PENIRQ */
93 tsc2007_xfer(tsc
, PWRDOWN
);
95 mutex_unlock(&tsc
->mlock
);
102 static const struct iio_info tsc2007_iio_info
= {
103 .read_raw
= tsc2007_read_raw
,
106 int tsc2007_iio_configure(struct tsc2007
*ts
)
108 struct iio_dev
*indio_dev
;
109 struct tsc2007_iio
*iio
;
112 indio_dev
= devm_iio_device_alloc(&ts
->client
->dev
, sizeof(*iio
));
114 dev_err(&ts
->client
->dev
, "iio_device_alloc failed\n");
118 iio
= iio_priv(indio_dev
);
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
);
127 error
= devm_iio_device_register(&ts
->client
->dev
, indio_dev
);
129 dev_err(&ts
->client
->dev
,
130 "iio_device_register() failed: %d\n", error
);