2 * Copyright (c) 2016 Golden Delicious Comp. GmbH&Co. KG
3 * Nikolaus Schaller <hns@goldelico.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/i2c.h>
11 #include <linux/iio/iio.h>
18 #define TSC2007_CHAN_IIO(_chan, _name, _type, _chan_info) \
20 .datasheet_name = _name, \
22 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
28 static const struct iio_chan_spec tsc2007_iio_channel
[] = {
29 TSC2007_CHAN_IIO(0, "x", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
30 TSC2007_CHAN_IIO(1, "y", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
31 TSC2007_CHAN_IIO(2, "z1", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
32 TSC2007_CHAN_IIO(3, "z2", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
33 TSC2007_CHAN_IIO(4, "adc", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
),
34 TSC2007_CHAN_IIO(5, "rt", IIO_VOLTAGE
, IIO_CHAN_INFO_RAW
), /* Ohms? */
35 TSC2007_CHAN_IIO(6, "pen", IIO_PRESSURE
, IIO_CHAN_INFO_RAW
),
36 TSC2007_CHAN_IIO(7, "temp0", IIO_TEMP
, IIO_CHAN_INFO_RAW
),
37 TSC2007_CHAN_IIO(8, "temp1", IIO_TEMP
, IIO_CHAN_INFO_RAW
),
40 static int tsc2007_read_raw(struct iio_dev
*indio_dev
,
41 struct iio_chan_spec
const *chan
,
42 int *val
, int *val2
, long mask
)
44 struct tsc2007_iio
*iio
= iio_priv(indio_dev
);
45 struct tsc2007
*tsc
= iio
->ts
;
46 int adc_chan
= chan
->channel
;
49 if (adc_chan
>= ARRAY_SIZE(tsc2007_iio_channel
))
52 if (mask
!= IIO_CHAN_INFO_RAW
)
55 mutex_lock(&tsc
->mlock
);
57 switch (chan
->channel
) {
59 *val
= tsc2007_xfer(tsc
, READ_X
);
62 *val
= tsc2007_xfer(tsc
, READ_Y
);
65 *val
= tsc2007_xfer(tsc
, READ_Z1
);
68 *val
= tsc2007_xfer(tsc
, READ_Z2
);
71 *val
= tsc2007_xfer(tsc
, (ADC_ON_12BIT
| TSC2007_MEASURE_AUX
));
76 tc
.x
= tsc2007_xfer(tsc
, READ_X
);
77 tc
.z1
= tsc2007_xfer(tsc
, READ_Z1
);
78 tc
.z2
= tsc2007_xfer(tsc
, READ_Z2
);
79 *val
= tsc2007_calculate_resistance(tsc
, &tc
);
83 *val
= tsc2007_is_pen_down(tsc
);
86 *val
= tsc2007_xfer(tsc
,
87 (ADC_ON_12BIT
| TSC2007_MEASURE_TEMP0
));
90 *val
= tsc2007_xfer(tsc
,
91 (ADC_ON_12BIT
| TSC2007_MEASURE_TEMP1
));
95 /* Prepare for next touch reading - power down ADC, enable PENIRQ */
96 tsc2007_xfer(tsc
, PWRDOWN
);
98 mutex_unlock(&tsc
->mlock
);
105 static const struct iio_info tsc2007_iio_info
= {
106 .read_raw
= tsc2007_read_raw
,
107 .driver_module
= THIS_MODULE
,
110 int tsc2007_iio_configure(struct tsc2007
*ts
)
112 struct iio_dev
*indio_dev
;
113 struct tsc2007_iio
*iio
;
116 indio_dev
= devm_iio_device_alloc(&ts
->client
->dev
, sizeof(*iio
));
118 dev_err(&ts
->client
->dev
, "iio_device_alloc failed\n");
122 iio
= iio_priv(indio_dev
);
125 indio_dev
->name
= "tsc2007";
126 indio_dev
->dev
.parent
= &ts
->client
->dev
;
127 indio_dev
->info
= &tsc2007_iio_info
;
128 indio_dev
->modes
= INDIO_DIRECT_MODE
;
129 indio_dev
->channels
= tsc2007_iio_channel
;
130 indio_dev
->num_channels
= ARRAY_SIZE(tsc2007_iio_channel
);
132 error
= devm_iio_device_register(&ts
->client
->dev
, indio_dev
);
134 dev_err(&ts
->client
->dev
,
135 "iio_device_register() failed: %d\n", error
);