^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ADS7846 based touchscreen and sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2005 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2006 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Various changes: Imre Deak <imre.deak@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Using code from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * - corgi_ts.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2004-2005 Richard Purdie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * - omap_ts.[hc], ads7846.h, ts_osk.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (C) 2002 MontaVista Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (C) 2004 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (C) 2005 Dirk Behme
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/input/touchscreen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/spi/ads7846.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * This code has been heavily tested on a Nokia 770, and lightly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * TSC2046 is just newer ads7846 silicon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Support for ads7843 tested on Atmel at91sam926x-EK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Support for ads7845 has only been stubbed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Support for Analog Devices AD7873 and AD7843 tested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * IRQ handling needs a workaround because of a shortcoming in handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * edge triggered IRQs on some platforms like the OMAP1/2. These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * platforms don't handle the ARM lazy IRQ disabling properly, thus we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * have to maintain our own SW IRQ disabled status. This should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * removed as soon as the affected platform's IRQ handling is fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * App note sbaa036 talks in more detail about accurate sampling...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * that ought to help in situations like LCDs inducing noise (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * can also be helped by using synch signals) and more generally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * This driver tries to utilize the measures described in the app
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * note. The strength of filtering can be set in the board-* specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define TS_POLL_DELAY 1 /* ms delay before the first sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define TS_POLL_PERIOD 5 /* ms delay between samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* this driver doesn't aim at the peak continuous sample rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct ts_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * For portability, we can't read 12 bit values using SPI (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * would make the controller deliver them as native byte order u16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * with msbs zeroed). Instead, we read them as two 8-bit values,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * *** WHICH NEED BYTESWAPPING *** and range adjustment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u16 x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) u16 y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u16 z1, z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) bool ignore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u8 x_buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u8 y_buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * We allocate this separately to avoid cache line sharing issues when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * driver is used with DMA-based SPI controllers (like atmel_spi) on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * systems where main memory is not DMA-coherent (most non-x86 boards).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct ads7846_packet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u8 read_x, read_y, read_z1, read_z2, pwrdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u16 dummy; /* for the pwrdown read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct ts_event tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct ads7846 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) char name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct spi_device *spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct regulator *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #if IS_ENABLED(CONFIG_HWMON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct device *hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) u16 model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u16 vref_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u16 vref_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u16 x_plate_ohms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u16 pressure_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bool swap_xy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) bool use_internal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct ads7846_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct spi_transfer xfer[18];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct spi_message msg[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int msg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) bool pendown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int read_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int read_rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int last_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) u16 debounce_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) u16 debounce_tol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u16 debounce_rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) u16 penirq_recheck_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct touchscreen_properties core_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) bool stopped; /* P: lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) bool disabled; /* P: lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) bool suspended; /* P: lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int (*filter)(void *data, int data_idx, int *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void *filter_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void (*filter_cleanup)(void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int (*get_pendown_state)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int gpio_pendown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void (*wait_for_sync)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* leave chip selected when we're done, for quicker re-select? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*--------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* The ADS7846 has touchscreen and other sensors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * Earlier ads784x chips are somewhat compatible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define ADS_START (1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #define ADS_A2A1A0_d_y (1 << 4) /* differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define ADS_A2A1A0_d_x (5 << 4) /* differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define ADS_8_BIT (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define ADS_12_BIT (0 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #define ADS_SER (1 << 2) /* non-differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #define ADS_DFR (0 << 2) /* differential */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #define MAX_12BIT ((1<<12)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* leave ADC powered up (disables penirq) between differential samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) | ADS_12_BIT | ADS_DFR | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* single-ended samples need to first power up reference voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * we leave both ADC and VREF powered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) | ADS_12_BIT | ADS_SER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #define REF_ON (READ_12BIT_DFR(x, 1, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int get_pendown_state(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (ts->get_pendown_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return ts->get_pendown_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return !gpio_get_value(ts->gpio_pendown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void ads7846_report_pen_up(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct input_dev *input = ts->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) input_report_key(input, BTN_TOUCH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) input_report_abs(input, ABS_PRESSURE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ts->pendown = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_vdbg(&ts->spi->dev, "UP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Must be called with ts->lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void ads7846_stop(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!ts->disabled && !ts->suspended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* Signal IRQ thread to stop polling and disable the handler. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ts->stopped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) wake_up(&ts->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) disable_irq(ts->spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Must be called with ts->lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void ads7846_restart(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!ts->disabled && !ts->suspended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* Check if pen was released since last stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ts->pendown && !get_pendown_state(ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ads7846_report_pen_up(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Tell IRQ thread that it may poll the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ts->stopped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) enable_irq(ts->spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* Must be called with ts->lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void __ads7846_disable(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ads7846_stop(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) regulator_disable(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * We know the chip's in low power mode since we always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * leave it that way after every request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Must be called with ts->lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void __ads7846_enable(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) error = regulator_enable(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (error != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ads7846_restart(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void ads7846_disable(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!ts->disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!ts->suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) __ads7846_disable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ts->disabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static void ads7846_enable(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (ts->disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ts->disabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!ts->suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) __ads7846_enable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /*--------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Non-touchscreen sensors only use single-ended conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * ads7846 lets that pin be unconnected, to use internal vREF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct ser_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) u8 ref_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) u8 command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) u8 ref_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) u16 scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct spi_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct spi_transfer xfer[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * DMA (thus cache coherency maintenance) requires the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * transfer buffers to live in their own cache lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) __be16 sample ____cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct ads7845_ser_req {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) u8 command[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct spi_message msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct spi_transfer xfer[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * DMA (thus cache coherency maintenance) requires the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * transfer buffers to live in their own cache lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) u8 sample[3] ____cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int ads7846_read12_ser(struct device *dev, unsigned command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct ser_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) req = kzalloc(sizeof *req, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) spi_message_init(&req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* maybe turn on internal vREF, and let it settle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (ts->use_internal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) req->ref_on = REF_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) req->xfer[0].tx_buf = &req->ref_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) req->xfer[0].len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) spi_message_add_tail(&req->xfer[0], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) req->xfer[1].rx_buf = &req->scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) req->xfer[1].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* for 1uF, settle for 800 usec; no cap, 100 usec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) req->xfer[1].delay.value = ts->vref_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) spi_message_add_tail(&req->xfer[1], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Enable reference voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) command |= ADS_PD10_REF_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* Enable ADC in every case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) command |= ADS_PD10_ADC_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /* take sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) req->command = (u8) command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) req->xfer[2].tx_buf = &req->command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) req->xfer[2].len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) spi_message_add_tail(&req->xfer[2], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) req->xfer[3].rx_buf = &req->sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) req->xfer[3].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) spi_message_add_tail(&req->xfer[3], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /* REVISIT: take a few more samples, and compare ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* converter in low power mode & enable PENIRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) req->ref_off = PWRDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) req->xfer[4].tx_buf = &req->ref_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) req->xfer[4].len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) spi_message_add_tail(&req->xfer[4], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) req->xfer[5].rx_buf = &req->scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) req->xfer[5].len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) CS_CHANGE(req->xfer[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) spi_message_add_tail(&req->xfer[5], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ads7846_stop(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) status = spi_sync(spi, &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ads7846_restart(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* on-wire is a must-ignore bit, a BE12 value, then padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) status = be16_to_cpu(req->sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) status = status >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) status &= 0x0fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int ads7845_read12_ser(struct device *dev, unsigned command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct spi_device *spi = to_spi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct ads7845_ser_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) req = kzalloc(sizeof *req, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) spi_message_init(&req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) req->command[0] = (u8) command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) req->xfer[0].tx_buf = req->command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) req->xfer[0].rx_buf = req->sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) req->xfer[0].len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) spi_message_add_tail(&req->xfer[0], &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ads7846_stop(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) status = spi_sync(spi, &req->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) ads7846_restart(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /* BE12 value, then padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) status = get_unaligned_be16(&req->sample[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) status = status >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) status &= 0x0fff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) #if IS_ENABLED(CONFIG_HWMON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) #define SHOW(name, var, adjust) static ssize_t \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct ads7846 *ts = dev_get_drvdata(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ssize_t v = ads7846_read12_ser(&ts->spi->dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) READ_12BIT_SER(var)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (v < 0) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return v; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return sprintf(buf, "%u\n", adjust(ts, v)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Sysfs conventions report temperatures in millidegrees Celsius.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * accuracy scheme without calibration data. For now we won't try either;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * userspace sees raw sensor values, and must scale/calibrate appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) SHOW(temp0, temp0, null_adjust) /* temp1_input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) SHOW(temp1, temp1, null_adjust) /* temp2_input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* sysfs conventions report voltages in millivolts. We can convert voltages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * if we know vREF. userspace may need to scale vAUX to match the board's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * external resistors; we assume that vBATT only uses the internal ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) unsigned retval = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* external resistors may scale vAUX into 0..vREF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) retval *= ts->vref_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) retval = retval >> 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) unsigned retval = vaux_adjust(ts, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /* ads7846 has a resistor ladder to scale this signal down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (ts->model == 7846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) retval *= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) SHOW(in0_input, vaux, vaux_adjust)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) SHOW(in1_input, vbatt, vbatt_adjust)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (ts->model == 7843 && index < 2) /* in0, in1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (ts->model == 7845 && index != 2) /* in0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static struct attribute *ads7846_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) &dev_attr_temp0.attr, /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) &dev_attr_temp1.attr, /* 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) &dev_attr_in0_input.attr, /* 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) &dev_attr_in1_input.attr, /* 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static const struct attribute_group ads7846_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) .attrs = ads7846_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) .is_visible = ads7846_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) __ATTRIBUTE_GROUPS(ads7846_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /* hwmon sensors need a reference voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) switch (ts->model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) case 7846:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (!ts->vref_mv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ts->vref_mv = 2500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) ts->use_internal = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) case 7845:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) case 7843:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (!ts->vref_mv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) dev_warn(&spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) "external vREF for ADS%d not specified\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) ts->model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ts, ads7846_attr_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return PTR_ERR_OR_ZERO(ts->hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) static void ads784x_hwmon_unregister(struct spi_device *spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (ts->hwmon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) hwmon_device_unregister(ts->hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static inline int ads784x_hwmon_register(struct spi_device *spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static inline void ads784x_hwmon_unregister(struct spi_device *spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static ssize_t ads7846_pen_down_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return sprintf(buf, "%u\n", ts->pendown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static ssize_t ads7846_disable_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return sprintf(buf, "%u\n", ts->disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static ssize_t ads7846_disable_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) err = kstrtouint(buf, 10, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) ads7846_disable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ads7846_enable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static struct attribute *ads784x_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) &dev_attr_pen_down.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) &dev_attr_disable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static const struct attribute_group ads784x_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) .attrs = ads784x_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) /*--------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static void null_wait_for_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) struct ads7846 *ts = ads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /* Start over collecting consistent readings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ts->read_rep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * Repeat it, if this was the first read or the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * wasn't consistent enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (ts->read_cnt < ts->debounce_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ts->last_read = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ts->read_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return ADS7846_FILTER_REPEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * Maximum number of debouncing reached and still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * not enough number of consistent readings. Abort
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * the whole sample, repeat it in the next sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) ts->read_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return ADS7846_FILTER_IGNORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (++ts->read_rep > ts->debounce_rep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * Got a good reading for this coordinate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * go for the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) ts->read_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ts->read_rep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return ADS7846_FILTER_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) /* Read more values that are consistent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) ts->read_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return ADS7846_FILTER_REPEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) static int ads7846_no_filter(void *ads, int data_idx, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return ADS7846_FILTER_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) struct spi_transfer *t =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) value = be16_to_cpup((__be16 *)&(((char *)t->rx_buf)[1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * adjust: on-wire is a must-ignore bit, a BE12 value, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * padding; built from two 8 bit values written msb-first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) value = be16_to_cpup((__be16 *)t->rx_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /* enforce ADC output is 12 bits width */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return (value >> 3) & 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static void ads7846_update_value(struct spi_message *m, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct spi_transfer *t =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) *(u16 *)t->rx_buf = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static void ads7846_read_state(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct ads7846_packet *packet = ts->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct spi_message *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) int msg_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) while (msg_idx < ts->msg_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) ts->wait_for_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) m = &ts->msg[msg_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) error = spi_sync(ts->spi, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) packet->tc.ignore = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * Last message is power down request, no need to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * or filter the value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (msg_idx < ts->msg_count - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) val = ads7846_get_value(ts, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) action = ts->filter(ts->filter_data, msg_idx, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) case ADS7846_FILTER_REPEAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) case ADS7846_FILTER_IGNORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) packet->tc.ignore = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) msg_idx = ts->msg_count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) case ADS7846_FILTER_OK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ads7846_update_value(m, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) packet->tc.ignore = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) msg_idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) msg_idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static void ads7846_report_state(struct ads7846 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct ads7846_packet *packet = ts->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) unsigned int Rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) u16 x, y, z1, z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) * ads7846_get_value() does in-place conversion (including byte swap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * from on-the-wire format as part of debouncing to get stable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * readings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) x = *(u16 *)packet->tc.x_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) y = *(u16 *)packet->tc.y_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) z1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) z2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) x = packet->tc.x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) y = packet->tc.y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) z1 = packet->tc.z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) z2 = packet->tc.z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /* range filtering */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (x == MAX_12BIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) x = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (ts->model == 7843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) Rt = ts->pressure_max / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) } else if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (get_pendown_state(ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) Rt = ts->pressure_max / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) Rt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) } else if (likely(x && z1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /* compute touch pressure resistance using equation #2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) Rt = z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) Rt -= z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) Rt *= ts->x_plate_ohms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) Rt = DIV_ROUND_CLOSEST(Rt, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) Rt *= x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) Rt /= z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) Rt = DIV_ROUND_CLOSEST(Rt, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) Rt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * Sample found inconsistent by debouncing or pressure is beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * the maximum. Don't report it to user space, repeat at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * once more the measurement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (packet->tc.ignore || Rt > ts->pressure_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) packet->tc.ignore, Rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * Maybe check the pendown state before reporting. This discards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * false readings when the pen is lifted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (ts->penirq_recheck_delay_usecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) udelay(ts->penirq_recheck_delay_usecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (!get_pendown_state(ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) Rt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * NOTE: We can't rely on the pressure to determine the pen down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * state, even this controller has a pressure sensor. The pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * value can fluctuate for quite a while after lifting the pen and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * in some cases may not even settle at the expected value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) * The only safe way to check for the pen up condition is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (Rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct input_dev *input = ts->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (!ts->pendown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) input_report_key(input, BTN_TOUCH, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) ts->pendown = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) dev_vdbg(&ts->spi->dev, "DOWN\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) touchscreen_report_pos(input, &ts->core_prop, x, y, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) static irqreturn_t ads7846_hard_irq(int irq, void *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) struct ads7846 *ts = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) static irqreturn_t ads7846_irq(int irq, void *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) struct ads7846 *ts = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* Start with a small delay before checking pendown state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) msleep(TS_POLL_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) while (!ts->stopped && get_pendown_state(ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) /* pen is down, continue with the measurement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) ads7846_read_state(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (!ts->stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) ads7846_report_state(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) wait_event_timeout(ts->wait, ts->stopped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) msecs_to_jiffies(TS_POLL_PERIOD));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (ts->pendown && !ts->stopped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) ads7846_report_pen_up(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) static int __maybe_unused ads7846_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (!ts->suspended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (!ts->disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) __ads7846_disable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (device_may_wakeup(&ts->spi->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) enable_irq_wake(ts->spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ts->suspended = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static int __maybe_unused ads7846_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct ads7846 *ts = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) mutex_lock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (ts->suspended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) ts->suspended = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (device_may_wakeup(&ts->spi->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) disable_irq_wake(ts->spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (!ts->disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) __ads7846_enable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) mutex_unlock(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static int ads7846_setup_pendown(struct spi_device *spi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) struct ads7846 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) const struct ads7846_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * REVISIT when the irq can be triggered active-low, or if for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * reason the touchscreen isn't hooked up, we don't need to access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * the pendown state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (pdata->get_pendown_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) ts->get_pendown_state = pdata->get_pendown_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) } else if (gpio_is_valid(pdata->gpio_pendown)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) "ads7846_pendown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) dev_err(&spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) "failed to request/setup pendown GPIO%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) pdata->gpio_pendown, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) ts->gpio_pendown = pdata->gpio_pendown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (pdata->gpio_pendown_debounce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) gpio_set_debounce(pdata->gpio_pendown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) pdata->gpio_pendown_debounce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * Set up the transfers to read touchscreen state; this assumes we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * use formula #2 for pressure, not #3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) static void ads7846_setup_spi_msg(struct ads7846 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) const struct ads7846_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct spi_message *m = &ts->msg[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) struct spi_transfer *x = ts->xfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct ads7846_packet *packet = ts->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) int vref = pdata->keep_vref_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (ts->model == 7873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * The AD7873 is almost identical to the ADS7846
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * keep VREF off during differential/ratiometric
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * conversion modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) ts->model = 7846;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) vref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) ts->msg_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) spi_message_init(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) m->context = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) packet->read_y_cmd[0] = READ_Y(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) packet->read_y_cmd[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) packet->read_y_cmd[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) x->tx_buf = &packet->read_y_cmd[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) x->rx_buf = &packet->tc.y_buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) x->len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /* y- still on; turn on only y+ (and ADC) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) packet->read_y = READ_Y(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) x->tx_buf = &packet->read_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) x->rx_buf = &packet->tc.y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * The first sample after switching drivers can be low quality;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * optionally discard it, using a second one after the signals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * have had enough time to stabilize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (pdata->settle_delay_usecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) x->delay.value = pdata->settle_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) x->delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) x->tx_buf = &packet->read_y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) x->rx_buf = &packet->tc.y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) ts->msg_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) spi_message_init(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) m->context = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) packet->read_x_cmd[0] = READ_X(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) packet->read_x_cmd[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) packet->read_x_cmd[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) x->tx_buf = &packet->read_x_cmd[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) x->rx_buf = &packet->tc.x_buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) x->len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) /* turn y- off, x+ on, then leave in lowpower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) packet->read_x = READ_X(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) x->tx_buf = &packet->read_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) x->rx_buf = &packet->tc.x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /* ... maybe discard first sample ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (pdata->settle_delay_usecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) x->delay.value = pdata->settle_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) x->delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) x->tx_buf = &packet->read_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) x->rx_buf = &packet->tc.x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) /* turn y+ off, x- on; we'll use formula #2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (ts->model == 7846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) ts->msg_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) spi_message_init(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) m->context = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) packet->read_z1 = READ_Z1(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) x->tx_buf = &packet->read_z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) x->rx_buf = &packet->tc.z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) /* ... maybe discard first sample ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (pdata->settle_delay_usecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) x->delay.value = pdata->settle_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) x->delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) x->tx_buf = &packet->read_z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) x->rx_buf = &packet->tc.z1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) ts->msg_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) spi_message_init(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) m->context = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) packet->read_z2 = READ_Z2(vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) x->tx_buf = &packet->read_z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) x->rx_buf = &packet->tc.z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) /* ... maybe discard first sample ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (pdata->settle_delay_usecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) x->delay.value = pdata->settle_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) x->delay.unit = SPI_DELAY_UNIT_USECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) x->tx_buf = &packet->read_z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) x->rx_buf = &packet->tc.z2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) /* power down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) ts->msg_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) spi_message_init(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) m->context = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (ts->model == 7845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) packet->pwrdown_cmd[0] = PWRDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) packet->pwrdown_cmd[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) packet->pwrdown_cmd[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) x->tx_buf = &packet->pwrdown_cmd[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) x->len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) packet->pwrdown = PWRDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) x->tx_buf = &packet->pwrdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) x->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) x++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) x->rx_buf = &packet->dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) x->len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) CS_CHANGE(*x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) spi_message_add_tail(x, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static const struct of_device_id ads7846_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) { .compatible = "ti,tsc2046", .data = (void *) 7846 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) { .compatible = "ti,ads7843", .data = (void *) 7843 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) { .compatible = "ti,ads7845", .data = (void *) 7845 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) { .compatible = "ti,ads7846", .data = (void *) 7846 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) { .compatible = "ti,ads7873", .data = (void *) 7873 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) struct ads7846_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) struct device_node *node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) const struct of_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) dev_err(dev, "Device does not have associated DT data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) match = of_match_device(ads7846_dt_ids, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) if (!match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) dev_err(dev, "Unknown device model\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) pdata->model = (unsigned long)match->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) of_property_read_u16(node, "ti,vref-delay-usecs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) &pdata->vref_delay_usecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) of_property_read_u16(node, "ti,settle-delay-usec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) &pdata->settle_delay_usecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) &pdata->penirq_recheck_delay_usecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) of_property_read_u16(node, "ti,x-min", &pdata->x_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) of_property_read_u16(node, "ti,y-min", &pdata->y_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) of_property_read_u16(node, "ti,x-max", &pdata->x_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) of_property_read_u16(node, "ti,y-max", &pdata->y_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * touchscreen-max-pressure gets parsed during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * touchscreen_parse_properties()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (!of_property_read_u32(node, "touchscreen-min-pressure", &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) pdata->pressure_min = (u16) value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) if (!of_property_read_u32(node, "touchscreen-average-samples", &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) pdata->debounce_max = (u16) value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) of_property_read_u32(node, "ti,pendown-gpio-debounce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) &pdata->gpio_pendown_debounce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) pdata->wakeup = of_property_read_bool(node, "wakeup-source") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) of_property_read_bool(node, "linux,wakeup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) return pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) dev_err(dev, "no platform data defined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) static int ads7846_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) const struct ads7846_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) struct ads7846 *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) struct ads7846_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (!spi->irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) dev_dbg(&spi->dev, "no IRQ?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /* don't exceed max specified sample rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) dev_err(&spi->dev, "f(sample) %d KHz?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) (spi->max_speed_hz/SAMPLE_BITS)/1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) * We'd set TX word size 8 bits and RX word size to 13 bits ... except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) * that even if the hardware can do that, the SPI controller driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) * may not. So we stick to very-portable 8 bit words, both RX and TX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) spi->bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) spi->mode = SPI_MODE_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) err = spi_setup(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) if (!ts || !packet || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) spi_set_drvdata(spi, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) ts->packet = packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) ts->spi = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) ts->input = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) mutex_init(&ts->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) init_waitqueue_head(&ts->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) pdata = dev_get_platdata(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) pdata = ads7846_probe_dt(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (IS_ERR(pdata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) err = PTR_ERR(pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) ts->model = pdata->model ? : 7846;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) ts->vref_mv = pdata->vref_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (pdata->filter != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (pdata->filter_init != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) err = pdata->filter_init(pdata, &ts->filter_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) ts->filter = pdata->filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) ts->filter_cleanup = pdata->filter_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) } else if (pdata->debounce_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) ts->debounce_max = pdata->debounce_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) if (ts->debounce_max < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) ts->debounce_max = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) ts->debounce_tol = pdata->debounce_tol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) ts->debounce_rep = pdata->debounce_rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) ts->filter = ads7846_debounce_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) ts->filter_data = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) ts->filter = ads7846_no_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) err = ads7846_setup_pendown(spi, ts, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) goto err_cleanup_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) if (pdata->penirq_recheck_delay_usecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) ts->penirq_recheck_delay_usecs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) pdata->penirq_recheck_delay_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) input_dev->name = ts->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) input_dev->phys = ts->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) input_dev->dev.parent = &spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) input_set_abs_params(input_dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) pdata->x_min ? : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) pdata->x_max ? : MAX_12BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) input_set_abs_params(input_dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) pdata->y_min ? : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) pdata->y_max ? : MAX_12BIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) input_set_abs_params(input_dev, ABS_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) pdata->pressure_min, pdata->pressure_max, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) * Parse common framework properties. Must be done here to ensure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) * correct behaviour in case of using the legacy vendor bindings. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) * general binding value overrides the vendor specific one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) touchscreen_parse_properties(ts->input, false, &ts->core_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) ts->pressure_max = input_abs_get_max(input_dev, ABS_PRESSURE) ? : ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * Check if legacy ti,swap-xy binding is used instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) * touchscreen-swapped-x-y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (!ts->core_prop.swap_x_y && pdata->swap_xy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) swap(input_dev->absinfo[ABS_X], input_dev->absinfo[ABS_Y]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) ts->core_prop.swap_x_y = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) ads7846_setup_spi_msg(ts, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) ts->reg = regulator_get(&spi->dev, "vcc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (IS_ERR(ts->reg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) err = PTR_ERR(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) dev_err(&spi->dev, "unable to get regulator: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) goto err_free_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) err = regulator_enable(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) goto err_put_regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) irq_flags |= IRQF_ONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) irq_flags, spi->dev.driver->name, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) if (err && !pdata->irq_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) dev_info(&spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) "trying pin change workaround on irq %d\n", spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) irq_flags |= IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) err = request_threaded_irq(spi->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) ads7846_hard_irq, ads7846_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) irq_flags, spi->dev.driver->name, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) goto err_disable_regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) err = ads784x_hwmon_register(spi, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) goto err_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * Take a first sample, leaving nPENIRQ active and vREF off; avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * the touchscreen, in case it's not connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) if (ts->model == 7845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) ads7845_read12_ser(&spi->dev, PWRDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) goto err_remove_hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) err = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) goto err_remove_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) device_init_wakeup(&spi->dev, pdata->wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) * If device does not carry platform data we must have allocated it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) * when parsing DT data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) if (!dev_get_platdata(&spi->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) devm_kfree(&spi->dev, (void *)pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) err_remove_attr_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) err_remove_hwmon:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) ads784x_hwmon_unregister(spi, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) err_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) free_irq(spi->irq, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) err_disable_regulator:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) regulator_disable(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) err_put_regulator:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) regulator_put(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) err_free_gpio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (!ts->get_pendown_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) gpio_free(ts->gpio_pendown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) err_cleanup_filter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) if (ts->filter_cleanup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) ts->filter_cleanup(ts->filter_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) kfree(packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) kfree(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) static int ads7846_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) struct ads7846 *ts = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) ads7846_disable(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) free_irq(ts->spi->irq, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) input_unregister_device(ts->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) ads784x_hwmon_unregister(spi, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) regulator_put(ts->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (!ts->get_pendown_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) * If we are not using specialized pendown method we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) * have been relying on gpio we set up ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) gpio_free(ts->gpio_pendown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (ts->filter_cleanup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) ts->filter_cleanup(ts->filter_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) kfree(ts->packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) kfree(ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) dev_dbg(&spi->dev, "unregistered touchscreen\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) static struct spi_driver ads7846_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) .name = "ads7846",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) .pm = &ads7846_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) .of_match_table = of_match_ptr(ads7846_dt_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) .probe = ads7846_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) .remove = ads7846_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) module_spi_driver(ads7846_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) MODULE_ALIAS("spi:ads7846");