Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: tsc2007 - add iio interface to read external ADC input and temperature

The tsc2007 chip not only has a resistive touch screen controller but
also an external AUX adc imput which can be used for an ambient
light sensor, battery voltage monitoring or any general purpose.

Additionally it can measure the chip temperature.

This extension provides an iio interface for these adc channels.

Since it is not wasting much resources and is very straightforward,
we simply provide all other adc channels as optional iio interfaces
as weel. This can be used for debugging or special applications.

This patch also splits the tsc2007 driver in several source files:
tsc2007.h -- constants, structs and stubs
tsc2007_core.c -- functional parts of the original driver
tsc2007_iio.c -- the optional iio stuff

Makefile magic allows to conditionally link the iio stuff
if CONFIG_IIO=y or =m in a way that it works with
CONFIG_TOUCHSCREEN_TSC2007=m.

Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

H. Nikolaus Schaller and committed by
Dmitry Torokhov
f1443404 934df231

+273 -66
+10
drivers/input/touchscreen/Kconfig
··· 1035 1035 To compile this driver as a module, choose M here: the 1036 1036 module will be called tsc2007. 1037 1037 1038 + config TOUCHSCREEN_TSC2007_IIO 1039 + bool "IIO interface for external ADC input and temperature" 1040 + depends on TOUCHSCREEN_TSC2007 1041 + depends on IIO=y || IIO=TOUCHSCREEN_TSC2007 1042 + help 1043 + Saying Y here adds an iio interface to the tsc2007 which 1044 + provides values for the AUX input (used for e.g. battery 1045 + or ambient light monitoring), temperature and raw input 1046 + values. 1047 + 1038 1048 config TOUCHSCREEN_W90X900 1039 1049 tristate "W90P910 touchscreen driver" 1040 1050 depends on ARCH_W90X900
+2
drivers/input/touchscreen/Makefile
··· 80 80 obj-$(CONFIG_TOUCHSCREEN_TSC200X_CORE) += tsc200x-core.o 81 81 obj-$(CONFIG_TOUCHSCREEN_TSC2004) += tsc2004.o 82 82 obj-$(CONFIG_TOUCHSCREEN_TSC2005) += tsc2005.o 83 + tsc2007-y := tsc2007_core.o 84 + tsc2007-$(CONFIG_TOUCHSCREEN_TSC2007_IIO) += tsc2007_iio.o 83 85 obj-$(CONFIG_TOUCHSCREEN_TSC2007) += tsc2007.o 84 86 obj-$(CONFIG_TOUCHSCREEN_UCB1400) += ucb1400_ts.o 85 87 obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001) += wacom_w8001.o
+19 -66
drivers/input/touchscreen/tsc2007.c drivers/input/touchscreen/tsc2007_core.c
··· 27 27 #include <linux/i2c.h> 28 28 #include <linux/i2c/tsc2007.h> 29 29 #include <linux/of_device.h> 30 - #include <linux/of.h> 31 30 #include <linux/of_gpio.h> 31 + #include "tsc2007.h" 32 32 33 - #define TSC2007_MEASURE_TEMP0 (0x0 << 4) 34 - #define TSC2007_MEASURE_AUX (0x2 << 4) 35 - #define TSC2007_MEASURE_TEMP1 (0x4 << 4) 36 - #define TSC2007_ACTIVATE_XN (0x8 << 4) 37 - #define TSC2007_ACTIVATE_YN (0x9 << 4) 38 - #define TSC2007_ACTIVATE_YP_XN (0xa << 4) 39 - #define TSC2007_SETUP (0xb << 4) 40 - #define TSC2007_MEASURE_X (0xc << 4) 41 - #define TSC2007_MEASURE_Y (0xd << 4) 42 - #define TSC2007_MEASURE_Z1 (0xe << 4) 43 - #define TSC2007_MEASURE_Z2 (0xf << 4) 44 - 45 - #define TSC2007_POWER_OFF_IRQ_EN (0x0 << 2) 46 - #define TSC2007_ADC_ON_IRQ_DIS0 (0x1 << 2) 47 - #define TSC2007_ADC_OFF_IRQ_EN (0x2 << 2) 48 - #define TSC2007_ADC_ON_IRQ_DIS1 (0x3 << 2) 49 - 50 - #define TSC2007_12BIT (0x0 << 1) 51 - #define TSC2007_8BIT (0x1 << 1) 52 - 53 - #define MAX_12BIT ((1 << 12) - 1) 54 - 55 - #define ADC_ON_12BIT (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0) 56 - 57 - #define READ_Y (ADC_ON_12BIT | TSC2007_MEASURE_Y) 58 - #define READ_Z1 (ADC_ON_12BIT | TSC2007_MEASURE_Z1) 59 - #define READ_Z2 (ADC_ON_12BIT | TSC2007_MEASURE_Z2) 60 - #define READ_X (ADC_ON_12BIT | TSC2007_MEASURE_X) 61 - #define PWRDOWN (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN) 62 - 63 - struct ts_event { 64 - u16 x; 65 - u16 y; 66 - u16 z1, z2; 67 - }; 68 - 69 - struct tsc2007 { 70 - struct input_dev *input; 71 - char phys[32]; 72 - 73 - struct i2c_client *client; 74 - 75 - u16 model; 76 - u16 x_plate_ohms; 77 - u16 max_rt; 78 - unsigned long poll_period; /* in jiffies */ 79 - int fuzzx; 80 - int fuzzy; 81 - int fuzzz; 82 - 83 - unsigned gpio; 84 - int irq; 85 - 86 - wait_queue_head_t wait; 87 - bool stopped; 88 - 89 - int (*get_pendown_state)(struct device *); 90 - void (*clear_penirq)(void); 91 - }; 92 - 93 - static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd) 33 + int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd) 94 34 { 95 35 s32 data; 96 36 u16 val; ··· 68 128 tsc2007_xfer(tsc, PWRDOWN); 69 129 } 70 130 71 - static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc) 131 + u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc) 72 132 { 73 133 u32 rt = 0; 74 134 ··· 88 148 return rt; 89 149 } 90 150 91 - static bool tsc2007_is_pen_down(struct tsc2007 *ts) 151 + bool tsc2007_is_pen_down(struct tsc2007 *ts) 92 152 { 93 153 /* 94 154 * NOTE: We can't rely on the pressure to determine the pen down ··· 120 180 while (!ts->stopped && tsc2007_is_pen_down(ts)) { 121 181 122 182 /* pen is down, continue with the measurement */ 183 + 184 + mutex_lock(&ts->mlock); 123 185 tsc2007_read_values(ts, &tc); 186 + mutex_unlock(&ts->mlock); 124 187 125 188 rt = tsc2007_calculate_pressure(ts, &tc); 126 189 ··· 318 375 static int tsc2007_probe(struct i2c_client *client, 319 376 const struct i2c_device_id *id) 320 377 { 321 - const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev); 378 + const struct tsc2007_platform_data *pdata = 379 + dev_get_platdata(&client->dev); 322 380 struct tsc2007 *ts; 323 381 struct input_dev *input_dev; 324 382 int err; ··· 348 404 ts->client = client; 349 405 ts->irq = client->irq; 350 406 ts->input = input_dev; 407 + 351 408 init_waitqueue_head(&ts->wait); 409 + mutex_init(&ts->mlock); 352 410 353 411 snprintf(ts->phys, sizeof(ts->phys), 354 412 "%s/input0", dev_name(&client->dev)); ··· 406 460 if (err < 0) { 407 461 dev_err(&client->dev, 408 462 "Failed to setup chip: %d\n", err); 409 - return err; /* usually, chip does not respond */ 463 + return err; /* chip does not respond */ 410 464 } 411 465 412 466 err = input_register_device(input_dev); 413 467 if (err) { 414 468 dev_err(&client->dev, 415 469 "Failed to register input device: %d\n", err); 470 + return err; 471 + } 472 + 473 + err = tsc2007_iio_configure(ts); 474 + if (err) { 475 + dev_err(&client->dev, 476 + "Failed to register with IIO: %d\n", err); 416 477 return err; 417 478 } 418 479
+102
drivers/input/touchscreen/tsc2007.h
··· 1 + 2 + /* 3 + * Copyright (c) 2008 MtekVision Co., Ltd. 4 + * Kwangwoo Lee <kwlee@mtekvision.com> 5 + * 6 + * Using code from: 7 + * - ads7846.c 8 + * Copyright (c) 2005 David Brownell 9 + * Copyright (c) 2006 Nokia Corporation 10 + * - corgi_ts.c 11 + * Copyright (C) 2004-2005 Richard Purdie 12 + * - omap_ts.[hc], ads7846.h, ts_osk.c 13 + * Copyright (C) 2002 MontaVista Software 14 + * Copyright (C) 2004 Texas Instruments 15 + * Copyright (C) 2005 Dirk Behme 16 + * 17 + * This program is free software; you can redistribute it and/or modify 18 + * it under the terms of the GNU General Public License version 2 as 19 + * published by the Free Software Foundation. 20 + */ 21 + 22 + #ifndef _TSC2007_H 23 + #define _TSC2007_H 24 + 25 + #define TSC2007_MEASURE_TEMP0 (0x0 << 4) 26 + #define TSC2007_MEASURE_AUX (0x2 << 4) 27 + #define TSC2007_MEASURE_TEMP1 (0x4 << 4) 28 + #define TSC2007_ACTIVATE_XN (0x8 << 4) 29 + #define TSC2007_ACTIVATE_YN (0x9 << 4) 30 + #define TSC2007_ACTIVATE_YP_XN (0xa << 4) 31 + #define TSC2007_SETUP (0xb << 4) 32 + #define TSC2007_MEASURE_X (0xc << 4) 33 + #define TSC2007_MEASURE_Y (0xd << 4) 34 + #define TSC2007_MEASURE_Z1 (0xe << 4) 35 + #define TSC2007_MEASURE_Z2 (0xf << 4) 36 + 37 + #define TSC2007_POWER_OFF_IRQ_EN (0x0 << 2) 38 + #define TSC2007_ADC_ON_IRQ_DIS0 (0x1 << 2) 39 + #define TSC2007_ADC_OFF_IRQ_EN (0x2 << 2) 40 + #define TSC2007_ADC_ON_IRQ_DIS1 (0x3 << 2) 41 + 42 + #define TSC2007_12BIT (0x0 << 1) 43 + #define TSC2007_8BIT (0x1 << 1) 44 + 45 + #define MAX_12BIT ((1 << 12) - 1) 46 + 47 + #define ADC_ON_12BIT (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0) 48 + 49 + #define READ_Y (ADC_ON_12BIT | TSC2007_MEASURE_Y) 50 + #define READ_Z1 (ADC_ON_12BIT | TSC2007_MEASURE_Z1) 51 + #define READ_Z2 (ADC_ON_12BIT | TSC2007_MEASURE_Z2) 52 + #define READ_X (ADC_ON_12BIT | TSC2007_MEASURE_X) 53 + #define PWRDOWN (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN) 54 + 55 + struct ts_event { 56 + u16 x; 57 + u16 y; 58 + u16 z1, z2; 59 + }; 60 + 61 + struct tsc2007 { 62 + struct input_dev *input; 63 + char phys[32]; 64 + 65 + struct i2c_client *client; 66 + 67 + u16 model; 68 + u16 x_plate_ohms; 69 + u16 max_rt; 70 + unsigned long poll_period; /* in jiffies */ 71 + int fuzzx; 72 + int fuzzy; 73 + int fuzzz; 74 + 75 + unsigned int gpio; 76 + int irq; 77 + 78 + wait_queue_head_t wait; 79 + bool stopped; 80 + 81 + int (*get_pendown_state)(struct device *); 82 + void (*clear_penirq)(void); 83 + 84 + struct mutex mlock; 85 + }; 86 + 87 + int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd); 88 + u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, 89 + struct ts_event *tc); 90 + bool tsc2007_is_pen_down(struct tsc2007 *ts); 91 + 92 + #if IS_ENABLED(CONFIG_TOUCHSCREEN_TSC2007_IIO) 93 + /* defined in tsc2007_iio.c */ 94 + int tsc2007_iio_configure(struct tsc2007 *ts); 95 + #else 96 + static inline int tsc2007_iio_configure(struct tsc2007 *ts) 97 + { 98 + return 0; 99 + } 100 + #endif /* CONFIG_TOUCHSCREEN_TSC2007_IIO */ 101 + 102 + #endif /* _TSC2007_H */
+140
drivers/input/touchscreen/tsc2007_iio.c
··· 1 + /* 2 + * Copyright (c) 2016 Golden Delicious Comp. GmbH&Co. KG 3 + * Nikolaus Schaller <hns@goldelico.com> 4 + * 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. 8 + */ 9 + 10 + #include <linux/i2c.h> 11 + #include <linux/iio/iio.h> 12 + #include "tsc2007.h" 13 + 14 + struct tsc2007_iio { 15 + struct tsc2007 *ts; 16 + }; 17 + 18 + #define TSC2007_CHAN_IIO(_chan, _name, _type, _chan_info) \ 19 + { \ 20 + .datasheet_name = _name, \ 21 + .type = _type, \ 22 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 23 + BIT(_chan_info), \ 24 + .indexed = 1, \ 25 + .channel = _chan, \ 26 + } 27 + 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), 38 + }; 39 + 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) 43 + { 44 + struct tsc2007_iio *iio = iio_priv(indio_dev); 45 + struct tsc2007 *tsc = iio->ts; 46 + int adc_chan = chan->channel; 47 + int ret = 0; 48 + 49 + if (adc_chan >= ARRAY_SIZE(tsc2007_iio_channel)) 50 + return -EINVAL; 51 + 52 + if (mask != IIO_CHAN_INFO_RAW) 53 + return -EINVAL; 54 + 55 + mutex_lock(&tsc->mlock); 56 + 57 + switch (chan->channel) { 58 + case 0: 59 + *val = tsc2007_xfer(tsc, READ_X); 60 + break; 61 + case 1: 62 + *val = tsc2007_xfer(tsc, READ_Y); 63 + break; 64 + case 2: 65 + *val = tsc2007_xfer(tsc, READ_Z1); 66 + break; 67 + case 3: 68 + *val = tsc2007_xfer(tsc, READ_Z2); 69 + break; 70 + case 4: 71 + *val = tsc2007_xfer(tsc, (ADC_ON_12BIT | TSC2007_MEASURE_AUX)); 72 + break; 73 + case 5: { 74 + struct ts_event tc; 75 + 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_pressure(tsc, &tc); 80 + break; 81 + } 82 + case 6: 83 + *val = tsc2007_is_pen_down(tsc); 84 + break; 85 + case 7: 86 + *val = tsc2007_xfer(tsc, 87 + (ADC_ON_12BIT | TSC2007_MEASURE_TEMP0)); 88 + break; 89 + case 8: 90 + *val = tsc2007_xfer(tsc, 91 + (ADC_ON_12BIT | TSC2007_MEASURE_TEMP1)); 92 + break; 93 + } 94 + 95 + /* Prepare for next touch reading - power down ADC, enable PENIRQ */ 96 + tsc2007_xfer(tsc, PWRDOWN); 97 + 98 + mutex_unlock(&tsc->mlock); 99 + 100 + ret = IIO_VAL_INT; 101 + 102 + return ret; 103 + } 104 + 105 + static const struct iio_info tsc2007_iio_info = { 106 + .read_raw = tsc2007_read_raw, 107 + .driver_module = THIS_MODULE, 108 + }; 109 + 110 + int tsc2007_iio_configure(struct tsc2007 *ts) 111 + { 112 + struct iio_dev *indio_dev; 113 + struct tsc2007_iio *iio; 114 + int error; 115 + 116 + indio_dev = devm_iio_device_alloc(&ts->client->dev, sizeof(*iio)); 117 + if (!indio_dev) { 118 + dev_err(&ts->client->dev, "iio_device_alloc failed\n"); 119 + return -ENOMEM; 120 + } 121 + 122 + iio = iio_priv(indio_dev); 123 + iio->ts = ts; 124 + 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); 131 + 132 + error = devm_iio_device_register(&ts->client->dev, indio_dev); 133 + if (error) { 134 + dev_err(&ts->client->dev, 135 + "iio_device_register() failed: %d\n", error); 136 + return error; 137 + } 138 + 139 + return 0; 140 + }