cc6ce5ac2c
This cycle we had quite a few series that applied similar changes to lots of drivers. To keep this description manageable I have called those out in their own section rather than per driver. Particularly pleased to see the long running AFE precision series going in this cycle. Series includes some late breaking fixes. New device support * adi,ada4250 amplifier - New driver and dt bindings for this programmable gain amplifier. * adi,admv1014 microwave down-converter - New driver, dt bindings and some device specific ABI that may be generalized as more drivers for devices similar to this are added. * adi,admv4420 K Band down-converter. - New driver and dt bindings. * adi,adxl367 accelerometer driver. - New driver, dt-bindings + some new IIO ABI definitions to support reference magnitude events where an estimate of the acceleration due to gravity has been removed. - A few fixes as follow up patches. * adi,ltc2688 DAC with toggle and dither modes. - New driver and bindings. Includes some new driver specific (for now) ABI for handling toggle mode and the addition of a dither waveform to the DAC output. * AFE (analog front end) add support for additional types of analog device in front of an ADC. - RTD temperature sensors with dt bindings. - Temperature transducers wit dt bindings. - Related cleanup and features listed in other sections below. * maxim,ds3502 potentiometer. - Add support to ds1803 driver which required significant rework. * mediatek,mt2701-auxadc driver - Add mediatek,mt8186-auxadc - id table and chip specific info only. * semtech,sx9324, semtech,ax9360 - Substantial refactoring of sx9310 to extract core logic for reuse into a separate module - New driver using this supporting sx9324 proximity sensors. - New driver using this supporting sx9360 proximity sensors. * silan,sc7a20 - Compatible with the st,lis2dh (or nearly anyway) so add ID and chip specific info to enable support. Also silan vendor ID added for dt-bindings. Staging graduation * adi,ad7280a monitoring ADC for stacked lithium-ion batteries in electric cars and similar. - Substantial rework of driver required to bring inline with current IIO best practice. An unusual device in IIO so some interesting features we may see more of in future. Multiple driver/core cleanup - Use sysfs_emit() in simple locations where there is no path to change to various core created attributes. - Trivial white space fixes around inconsistency between space after { and before } in id tables. - Introduce new handling for fractional types to avoid repeated similar implementations. Use this in 3 drivers. Note this is also targeted at future use in the AFE driver and was motivated by discussions around the precision related work on that driver. - of related header cleanups - drop of*.h and add mod_devicetable.h as appropriate. - Move a number of symbol exports into IIO_* namespaces. Two categories, 1) Library used by multiple drivers e.g. st_sensors 2) Core driver module exporting functions used by bus specific modules. A few related cleanups in this set. - Switch from CONFIG_PM_* guards to new DEFINE_SIMPLE_DEV_PM_OPS() and similar to simplify drivers and take advantage of these new macros allowing the compiler to do the job or removing unused code without the need for __maybe_unused markings. Conversion of other drivers to these new macros ongoing. Features * adi,adf4350 - Switch from of specific to generic device properties enabling use with other firmware types. * adi,adx345 - Switch from of specific to generic device properties. - Add ACPI ID ADS0345 - Related driver cleanup. * adi,hmc425a - Switch from of specific to generic device properties. * afe analog rescaler driver - Wider range of types supported for scale. - Support offset. - Kunit tests. * atlas,ezo-sensor - Convert from of to device properties. * fsl,mma8452 - Support mount matrix. * infineon,dps310: - Add ACPI ID IFX3100. * invensense,mpu6050 - Convert to generic device properties. * maxim,ds1803 - Add out_raw_available before supporting more devices. - Convert from of specific to device properties. * samsung,ssp_sensors - Convert from of specific to device properties. * st,stm32-timer trigger - Convert from of specific to device properties. * ti,hdc101x - Add ACPI ID TXNW1010. * ti,tsc2046: - Add read_raw support to enable use of iio_hwmon and similar. Fixes / cleanup. * mailmap - Update for Cai Huoqing * MAINTAINERS - Fix Analog Devices related links. - Add entry for ADRF6780 - Add entry for ADMV1013 - Add entry for AD7293 - Add entry for ADMV8818 - Update files listed for adis-lib * iio core: - Fix wrong comment about current_mode being something a driver should ever access. - Use struct_size() rather than open coding in industrialio-hw-consumer * adi,axl355 - Use units.h definitions instead of local versions. * adi,adis-lib - Simplify *updated_bits() macro - Whitespace cleanup. * afe - Note many of these fixes only apply to particular configurations so the problems have probably not been seen in the wild, but will be visible with new usecases enabled this cycle. - Fix application of consumer scale for IIO_VAL_INT. - Apply a scale of 1 when no scale is provided. - Make best effort to establish a valid offset value for fractional cases. - Use s64 for scale calculations where parameters may be signed. - Tidy up include order. - Improve accuracy for small fractional sales - Reduce risk of integer overflow. * ams,as3935 - Use devm_delayed_work_autocancel() to replace open coded equivalent. * aspeed,adc - Fix wrong use of divider flag. * atmel,sama5d2-adc - Relax atmel,trigger-edge-type to optional. - Drop Ludovic Desroches from listed maintainers of the dt-binding inline with previous MAINTAINERS entry update. * fsl,mma8452 - Fix probing when i2c_device_id used. - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev. Use dev_to_iio_dev() instead. Note the original path in here worked more by luck than design. * invensense,mpu6050 - Drop ACPI_PTR() protection to avoid an unused warning. - Use fact ACPI_COMPANION() returns null when ACPI_HANDLE() does to simplify handling. * motorola,cpcap-adc - Drop unused assignment. * qcom,spmi-adc - Fix wrong example of 'reg' in binding document. * renesas,rzg2l-adc - Trivial typo fix. * semtech,sx9360 - Fix wrong register handling for event generation. * st_sensors - Allow manual disabling of I2C or SPI module if not needed for a particular board. Default is still to enable the bus specific module if appropriate bus is supported. * st,lsm6dsx - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev. Use dev_to_iio_dev() instead. * ti,palmas-gpadc - Split the interrupt fields in the dt-binding example * ti,tsc2046 - Rework state machine to improve readability after recent debugging of an issue fixed elsewhere. - Add a sanity check to avoid very large memory allocations if a crazy delay is specified. * ti,twl6030 - Add error handling if devm_request_threaded_irq() fails. * xilinx,ams - Use devm_delayed_work_autocancel() instead of open coding equivalent. - Fix missing required clock entry in dt-binding. - Fix miss counting of channels resulting in ps channels not being enabled. - Fix incorrect values written to sequencer registers. - Fix sequence for single channel reading. -----BEGIN PGP SIGNATURE----- iQJFBAABCAAvFiEEbilms4eEBlKRJoGxVIU0mcT0FogFAmIfdM8RHGppYzIzQGtl cm5lbC5vcmcACgkQVIU0mcT0FoiGlQ//UyRpMX9Bv97LAbMDnqIHLYroTLJA3WFQ AaL/DKB1cVjBCoHlp24qaQrmncvifPF7sKJGKWf7yCHL5fraAYL/kHsCo/jECTho QOk9QaPAMP9ChOoVoP8iz5qrdF2qyoFUG69bX+QYeKhSKzcK1QPRTQ13LIL43d9p OJX47Cu7FfFwuAs5VKSVgpcII0tctv+Fdo6BkkeI+6w/vx2sFSzRaqRtc1ZU4Uav s51dM9JMos52e/G8yQAEOC24QUId4EHxo7QR8WjzZ47yIHRulpYwM6pWAtvOqEy9 eV++yz581+Uqs/qaDDk8nJdpa8aEv/NvfAK6gufB9UOWziMoR3G1pPFWoOLbcyIt IcUG+QyyEiIlmlwDE/m2OcSMzsxgrkEHNb3SE7ZkWZKP8OasGdVMHa7yEKCgLmzM S8EY9TsNA50A2VtowAPrdk74TVG2WeIDvEH2MMAUMjgW2DzsW9cmwFrziyj7ZPLX onoEjd/kpL2zzAArEadvzD1z1lLJcOUWn8ST2kbPQG8n/rp5y2u5PvgWRoO9zJlD ztX614XYRgRUhMrgb0q0nCTi07mnBZrR3P8Hnx1HOoZon/DIqPSL7NumITG09cQc fHqewQOU/WqoTH4tNvfywnBL/VAcxKFlc0B2rWIvp6dD5b0TU34ZdebcjLT1zYeC 6YQKbRaRjVg= =UnWv -----END PGP SIGNATURE----- Merge tag 'iio-for-5.18a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-next Jonathan writes: First set of new device support, fixes, cleanups and features for IIO in 5.18 This cycle we had quite a few series that applied similar changes to lots of drivers. To keep this description manageable I have called those out in their own section rather than per driver. Particularly pleased to see the long running AFE precision series going in this cycle. Series includes some late breaking fixes. New device support * adi,ada4250 amplifier - New driver and dt bindings for this programmable gain amplifier. * adi,admv1014 microwave down-converter - New driver, dt bindings and some device specific ABI that may be generalized as more drivers for devices similar to this are added. * adi,admv4420 K Band down-converter. - New driver and dt bindings. * adi,adxl367 accelerometer driver. - New driver, dt-bindings + some new IIO ABI definitions to support reference magnitude events where an estimate of the acceleration due to gravity has been removed. - A few fixes as follow up patches. * adi,ltc2688 DAC with toggle and dither modes. - New driver and bindings. Includes some new driver specific (for now) ABI for handling toggle mode and the addition of a dither waveform to the DAC output. * AFE (analog front end) add support for additional types of analog device in front of an ADC. - RTD temperature sensors with dt bindings. - Temperature transducers wit dt bindings. - Related cleanup and features listed in other sections below. * maxim,ds3502 potentiometer. - Add support to ds1803 driver which required significant rework. * mediatek,mt2701-auxadc driver - Add mediatek,mt8186-auxadc - id table and chip specific info only. * semtech,sx9324, semtech,ax9360 - Substantial refactoring of sx9310 to extract core logic for reuse into a separate module - New driver using this supporting sx9324 proximity sensors. - New driver using this supporting sx9360 proximity sensors. * silan,sc7a20 - Compatible with the st,lis2dh (or nearly anyway) so add ID and chip specific info to enable support. Also silan vendor ID added for dt-bindings. Staging graduation * adi,ad7280a monitoring ADC for stacked lithium-ion batteries in electric cars and similar. - Substantial rework of driver required to bring inline with current IIO best practice. An unusual device in IIO so some interesting features we may see more of in future. Multiple driver/core cleanup - Use sysfs_emit() in simple locations where there is no path to change to various core created attributes. - Trivial white space fixes around inconsistency between space after { and before } in id tables. - Introduce new handling for fractional types to avoid repeated similar implementations. Use this in 3 drivers. Note this is also targeted at future use in the AFE driver and was motivated by discussions around the precision related work on that driver. - of related header cleanups - drop of*.h and add mod_devicetable.h as appropriate. - Move a number of symbol exports into IIO_* namespaces. Two categories, 1) Library used by multiple drivers e.g. st_sensors 2) Core driver module exporting functions used by bus specific modules. A few related cleanups in this set. - Switch from CONFIG_PM_* guards to new DEFINE_SIMPLE_DEV_PM_OPS() and similar to simplify drivers and take advantage of these new macros allowing the compiler to do the job or removing unused code without the need for __maybe_unused markings. Conversion of other drivers to these new macros ongoing. Features * adi,adf4350 - Switch from of specific to generic device properties enabling use with other firmware types. * adi,adx345 - Switch from of specific to generic device properties. - Add ACPI ID ADS0345 - Related driver cleanup. * adi,hmc425a - Switch from of specific to generic device properties. * afe analog rescaler driver - Wider range of types supported for scale. - Support offset. - Kunit tests. * atlas,ezo-sensor - Convert from of to device properties. * fsl,mma8452 - Support mount matrix. * infineon,dps310: - Add ACPI ID IFX3100. * invensense,mpu6050 - Convert to generic device properties. * maxim,ds1803 - Add out_raw_available before supporting more devices. - Convert from of specific to device properties. * samsung,ssp_sensors - Convert from of specific to device properties. * st,stm32-timer trigger - Convert from of specific to device properties. * ti,hdc101x - Add ACPI ID TXNW1010. * ti,tsc2046: - Add read_raw support to enable use of iio_hwmon and similar. Fixes / cleanup. * mailmap - Update for Cai Huoqing * MAINTAINERS - Fix Analog Devices related links. - Add entry for ADRF6780 - Add entry for ADMV1013 - Add entry for AD7293 - Add entry for ADMV8818 - Update files listed for adis-lib * iio core: - Fix wrong comment about current_mode being something a driver should ever access. - Use struct_size() rather than open coding in industrialio-hw-consumer * adi,axl355 - Use units.h definitions instead of local versions. * adi,adis-lib - Simplify *updated_bits() macro - Whitespace cleanup. * afe - Note many of these fixes only apply to particular configurations so the problems have probably not been seen in the wild, but will be visible with new usecases enabled this cycle. - Fix application of consumer scale for IIO_VAL_INT. - Apply a scale of 1 when no scale is provided. - Make best effort to establish a valid offset value for fractional cases. - Use s64 for scale calculations where parameters may be signed. - Tidy up include order. - Improve accuracy for small fractional sales - Reduce risk of integer overflow. * ams,as3935 - Use devm_delayed_work_autocancel() to replace open coded equivalent. * aspeed,adc - Fix wrong use of divider flag. * atmel,sama5d2-adc - Relax atmel,trigger-edge-type to optional. - Drop Ludovic Desroches from listed maintainers of the dt-binding inline with previous MAINTAINERS entry update. * fsl,mma8452 - Fix probing when i2c_device_id used. - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev. Use dev_to_iio_dev() instead. Note the original path in here worked more by luck than design. * invensense,mpu6050 - Drop ACPI_PTR() protection to avoid an unused warning. - Use fact ACPI_COMPANION() returns null when ACPI_HANDLE() does to simplify handling. * motorola,cpcap-adc - Drop unused assignment. * qcom,spmi-adc - Fix wrong example of 'reg' in binding document. * renesas,rzg2l-adc - Trivial typo fix. * semtech,sx9360 - Fix wrong register handling for event generation. * st_sensors - Allow manual disabling of I2C or SPI module if not needed for a particular board. Default is still to enable the bus specific module if appropriate bus is supported. * st,lsm6dsx - dev_get_drvdata() on the iio_dev->dev, no longer returns iio_dev. Use dev_to_iio_dev() instead. * ti,palmas-gpadc - Split the interrupt fields in the dt-binding example * ti,tsc2046 - Rework state machine to improve readability after recent debugging of an issue fixed elsewhere. - Add a sanity check to avoid very large memory allocations if a crazy delay is specified. * ti,twl6030 - Add error handling if devm_request_threaded_irq() fails. * xilinx,ams - Use devm_delayed_work_autocancel() instead of open coding equivalent. - Fix missing required clock entry in dt-binding. - Fix miss counting of channels resulting in ps channels not being enabled. - Fix incorrect values written to sequencer registers. - Fix sequence for single channel reading. * tag 'iio-for-5.18a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (245 commits) iio: adc: xilinx-ams: Fix single channel switching sequence iio: adc: xilinx-ams: Fixed wrong sequencer register settings iio: adc: xilinx-ams: Fixed missing PS channels dt-bindings: iio: adc: zynqmp_ams: Add clock entry iio: accel: mma8452: use the correct logic to get mma8452_data iio: adc: aspeed: Add divider flag to fix incorrect voltage reading. iio: imu: st_lsm6dsx: use dev_to_iio_dev() to get iio_dev struct dt-bindings: iio: Add ltc2688 documentation iio: ABI: add ABI file for the LTC2688 DAC iio: dac: add support for ltc2688 dt-bindings: iio: afe: add bindings for temperature transducers dt-bindings: iio: afe: add bindings for temperature-sense-rtd iio: afe: rescale: add temperature transducers iio: afe: rescale: add RTD temperature sensor support iio: test: add basic tests for the iio-rescale driver iio: afe: rescale: reduce risk of integer overflow iio: afe: rescale: fix accuracy for small fractional scales iio: afe: rescale: add offset support iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support iio: afe: rescale: expose scale processing function ...
848 lines
22 KiB
C
848 lines
22 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Texas Instruments TSC2046 SPI ADC driver
|
|
*
|
|
* Copyright (c) 2021 Oleksij Rempel <kernel@pengutronix.de>, Pengutronix
|
|
*/
|
|
|
|
#include <linux/bitfield.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/module.h>
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
#include <linux/iio/buffer.h>
|
|
#include <linux/iio/trigger_consumer.h>
|
|
#include <linux/iio/triggered_buffer.h>
|
|
#include <linux/iio/trigger.h>
|
|
|
|
/*
|
|
* The PENIRQ of TSC2046 controller is implemented as level shifter attached to
|
|
* the X+ line. If voltage of the X+ line reaches a specific level the IRQ will
|
|
* be activated or deactivated.
|
|
* To make this kind of IRQ reusable as trigger following additions were
|
|
* implemented:
|
|
* - rate limiting:
|
|
* For typical touchscreen use case, we need to trigger about each 10ms.
|
|
* - hrtimer:
|
|
* Continue triggering at least once after the IRQ was deactivated. Then
|
|
* deactivate this trigger to stop sampling in order to reduce power
|
|
* consumption.
|
|
*/
|
|
|
|
#define TI_TSC2046_NAME "tsc2046"
|
|
|
|
/* This driver doesn't aim at the peak continuous sample rate */
|
|
#define TI_TSC2046_MAX_SAMPLE_RATE 125000
|
|
#define TI_TSC2046_SAMPLE_BITS \
|
|
BITS_PER_TYPE(struct tsc2046_adc_atom)
|
|
#define TI_TSC2046_MAX_CLK_FREQ \
|
|
(TI_TSC2046_MAX_SAMPLE_RATE * TI_TSC2046_SAMPLE_BITS)
|
|
|
|
#define TI_TSC2046_SAMPLE_INTERVAL_US 10000
|
|
|
|
#define TI_TSC2046_START BIT(7)
|
|
#define TI_TSC2046_ADDR GENMASK(6, 4)
|
|
#define TI_TSC2046_ADDR_TEMP1 7
|
|
#define TI_TSC2046_ADDR_AUX 6
|
|
#define TI_TSC2046_ADDR_X 5
|
|
#define TI_TSC2046_ADDR_Z2 4
|
|
#define TI_TSC2046_ADDR_Z1 3
|
|
#define TI_TSC2046_ADDR_VBAT 2
|
|
#define TI_TSC2046_ADDR_Y 1
|
|
#define TI_TSC2046_ADDR_TEMP0 0
|
|
|
|
/*
|
|
* The mode bit sets the resolution of the ADC. With this bit low, the next
|
|
* conversion has 12-bit resolution, whereas with this bit high, the next
|
|
* conversion has 8-bit resolution. This driver is optimized for 12-bit mode.
|
|
* So, for this driver, this bit should stay zero.
|
|
*/
|
|
#define TI_TSC2046_8BIT_MODE BIT(3)
|
|
|
|
/*
|
|
* SER/DFR - The SER/DFR bit controls the reference mode, either single-ended
|
|
* (high) or differential (low).
|
|
*/
|
|
#define TI_TSC2046_SER BIT(2)
|
|
|
|
/*
|
|
* If VREF_ON and ADC_ON are both zero, then the chip operates in
|
|
* auto-wake/suspend mode. In most case this bits should stay zero.
|
|
*/
|
|
#define TI_TSC2046_PD1_VREF_ON BIT(1)
|
|
#define TI_TSC2046_PD0_ADC_ON BIT(0)
|
|
|
|
/*
|
|
* All supported devices can do 8 or 12bit resolution. This driver
|
|
* supports only 12bit mode, here we have a 16bit data transfer, where
|
|
* the MSB and the 3 LSB are 0.
|
|
*/
|
|
#define TI_TSC2046_DATA_12BIT GENMASK(14, 3)
|
|
|
|
#define TI_TSC2046_MAX_CHAN 8
|
|
#define TI_TSC2046_MIN_POLL_CNT 3
|
|
#define TI_TSC2046_EXT_POLL_CNT 3
|
|
#define TI_TSC2046_POLL_CNT \
|
|
(TI_TSC2046_MIN_POLL_CNT + TI_TSC2046_EXT_POLL_CNT)
|
|
#define TI_TSC2046_INT_VREF 2500
|
|
|
|
/* Represents a HW sample */
|
|
struct tsc2046_adc_atom {
|
|
/*
|
|
* Command transmitted to the controller. This field is empty on the RX
|
|
* buffer.
|
|
*/
|
|
u8 cmd;
|
|
/*
|
|
* Data received from the controller. This field is empty for the TX
|
|
* buffer
|
|
*/
|
|
__be16 data;
|
|
} __packed;
|
|
|
|
/* Layout of atomic buffers within big buffer */
|
|
struct tsc2046_adc_group_layout {
|
|
/* Group offset within the SPI RX buffer */
|
|
unsigned int offset;
|
|
/*
|
|
* Amount of tsc2046_adc_atom structs within the same command gathered
|
|
* within same group.
|
|
*/
|
|
unsigned int count;
|
|
/*
|
|
* Settling samples (tsc2046_adc_atom structs) which should be skipped
|
|
* before good samples will start.
|
|
*/
|
|
unsigned int skip;
|
|
};
|
|
|
|
struct tsc2046_adc_dcfg {
|
|
const struct iio_chan_spec *channels;
|
|
unsigned int num_channels;
|
|
};
|
|
|
|
struct tsc2046_adc_ch_cfg {
|
|
unsigned int settling_time_us;
|
|
unsigned int oversampling_ratio;
|
|
};
|
|
|
|
enum tsc2046_state {
|
|
TSC2046_STATE_SHUTDOWN,
|
|
TSC2046_STATE_STANDBY,
|
|
TSC2046_STATE_POLL,
|
|
TSC2046_STATE_POLL_IRQ_DISABLE,
|
|
TSC2046_STATE_ENABLE_IRQ,
|
|
};
|
|
|
|
struct tsc2046_adc_priv {
|
|
struct spi_device *spi;
|
|
const struct tsc2046_adc_dcfg *dcfg;
|
|
|
|
struct iio_trigger *trig;
|
|
struct hrtimer trig_timer;
|
|
enum tsc2046_state state;
|
|
int poll_cnt;
|
|
spinlock_t state_lock;
|
|
|
|
struct spi_transfer xfer;
|
|
struct spi_message msg;
|
|
|
|
struct {
|
|
/* Scan data for each channel */
|
|
u16 data[TI_TSC2046_MAX_CHAN];
|
|
/* Timestamp */
|
|
s64 ts __aligned(8);
|
|
} scan_buf;
|
|
|
|
/*
|
|
* Lock to protect the layout and the SPI transfer buffer.
|
|
* tsc2046_adc_group_layout can be changed within update_scan_mode(),
|
|
* in this case the l[] and tx/rx buffer will be out of sync to each
|
|
* other.
|
|
*/
|
|
struct mutex slock;
|
|
struct tsc2046_adc_group_layout l[TI_TSC2046_MAX_CHAN];
|
|
struct tsc2046_adc_atom *rx;
|
|
struct tsc2046_adc_atom *tx;
|
|
|
|
unsigned int count;
|
|
unsigned int groups;
|
|
u32 effective_speed_hz;
|
|
u32 scan_interval_us;
|
|
u32 time_per_scan_us;
|
|
u32 time_per_bit_ns;
|
|
|
|
struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
|
|
};
|
|
|
|
#define TI_TSC2046_V_CHAN(index, bits, name) \
|
|
{ \
|
|
.type = IIO_VOLTAGE, \
|
|
.indexed = 1, \
|
|
.channel = index, \
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
|
|
.datasheet_name = "#name", \
|
|
.scan_index = index, \
|
|
.scan_type = { \
|
|
.sign = 'u', \
|
|
.realbits = bits, \
|
|
.storagebits = 16, \
|
|
.endianness = IIO_CPU, \
|
|
}, \
|
|
}
|
|
|
|
#define DECLARE_TI_TSC2046_8_CHANNELS(name, bits) \
|
|
const struct iio_chan_spec name ## _channels[] = { \
|
|
TI_TSC2046_V_CHAN(0, bits, TEMP0), \
|
|
TI_TSC2046_V_CHAN(1, bits, Y), \
|
|
TI_TSC2046_V_CHAN(2, bits, VBAT), \
|
|
TI_TSC2046_V_CHAN(3, bits, Z1), \
|
|
TI_TSC2046_V_CHAN(4, bits, Z2), \
|
|
TI_TSC2046_V_CHAN(5, bits, X), \
|
|
TI_TSC2046_V_CHAN(6, bits, AUX), \
|
|
TI_TSC2046_V_CHAN(7, bits, TEMP1), \
|
|
IIO_CHAN_SOFT_TIMESTAMP(8), \
|
|
}
|
|
|
|
static DECLARE_TI_TSC2046_8_CHANNELS(tsc2046_adc, 12);
|
|
|
|
static const struct tsc2046_adc_dcfg tsc2046_adc_dcfg_tsc2046e = {
|
|
.channels = tsc2046_adc_channels,
|
|
.num_channels = ARRAY_SIZE(tsc2046_adc_channels),
|
|
};
|
|
|
|
/*
|
|
* Convert time to a number of samples which can be transferred within this
|
|
* time.
|
|
*/
|
|
static unsigned int tsc2046_adc_time_to_count(struct tsc2046_adc_priv *priv,
|
|
unsigned long time)
|
|
{
|
|
unsigned int bit_count, sample_count;
|
|
|
|
bit_count = DIV_ROUND_UP(time * NSEC_PER_USEC, priv->time_per_bit_ns);
|
|
sample_count = DIV_ROUND_UP(bit_count, TI_TSC2046_SAMPLE_BITS);
|
|
|
|
dev_dbg(&priv->spi->dev, "Effective speed %u, time per bit: %u, count bits: %u, count samples: %u\n",
|
|
priv->effective_speed_hz, priv->time_per_bit_ns,
|
|
bit_count, sample_count);
|
|
|
|
return sample_count;
|
|
}
|
|
|
|
static u8 tsc2046_adc_get_cmd(struct tsc2046_adc_priv *priv, int ch_idx,
|
|
bool keep_power)
|
|
{
|
|
u32 pd;
|
|
|
|
/*
|
|
* if PD bits are 0, controller will automatically disable ADC, VREF and
|
|
* enable IRQ.
|
|
*/
|
|
if (keep_power)
|
|
pd = TI_TSC2046_PD0_ADC_ON;
|
|
else
|
|
pd = 0;
|
|
|
|
switch (ch_idx) {
|
|
case TI_TSC2046_ADDR_TEMP1:
|
|
case TI_TSC2046_ADDR_AUX:
|
|
case TI_TSC2046_ADDR_VBAT:
|
|
case TI_TSC2046_ADDR_TEMP0:
|
|
pd |= TI_TSC2046_SER | TI_TSC2046_PD1_VREF_ON;
|
|
}
|
|
|
|
return TI_TSC2046_START | FIELD_PREP(TI_TSC2046_ADDR, ch_idx) | pd;
|
|
}
|
|
|
|
static u16 tsc2046_adc_get_value(struct tsc2046_adc_atom *buf)
|
|
{
|
|
return FIELD_GET(TI_TSC2046_DATA_12BIT, get_unaligned_be16(&buf->data));
|
|
}
|
|
|
|
static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
|
|
u32 *effective_speed_hz)
|
|
{
|
|
struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
|
|
struct tsc2046_adc_atom *rx_buf, *tx_buf;
|
|
unsigned int val, val_normalized = 0;
|
|
int ret, i, count_skip = 0, max_count;
|
|
struct spi_transfer xfer;
|
|
struct spi_message msg;
|
|
u8 cmd;
|
|
|
|
if (!effective_speed_hz) {
|
|
count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
|
|
max_count = count_skip + ch->oversampling_ratio;
|
|
} else {
|
|
max_count = 1;
|
|
}
|
|
|
|
if (sizeof(*tx_buf) * max_count > PAGE_SIZE)
|
|
return -ENOSPC;
|
|
|
|
tx_buf = kcalloc(max_count, sizeof(*tx_buf), GFP_KERNEL);
|
|
if (!tx_buf)
|
|
return -ENOMEM;
|
|
|
|
rx_buf = kcalloc(max_count, sizeof(*rx_buf), GFP_KERNEL);
|
|
if (!rx_buf) {
|
|
ret = -ENOMEM;
|
|
goto free_tx;
|
|
}
|
|
|
|
/*
|
|
* Do not enable automatic power down on working samples. Otherwise the
|
|
* plates will never be completely charged.
|
|
*/
|
|
cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
|
|
|
|
for (i = 0; i < max_count - 1; i++)
|
|
tx_buf[i].cmd = cmd;
|
|
|
|
/* automatically power down on last sample */
|
|
tx_buf[i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
|
|
|
|
memset(&xfer, 0, sizeof(xfer));
|
|
xfer.tx_buf = tx_buf;
|
|
xfer.rx_buf = rx_buf;
|
|
xfer.len = sizeof(*tx_buf) * max_count;
|
|
spi_message_init_with_transfers(&msg, &xfer, 1);
|
|
|
|
/*
|
|
* We aren't using spi_write_then_read() because we need to be able
|
|
* to get hold of the effective_speed_hz from the xfer
|
|
*/
|
|
ret = spi_sync(priv->spi, &msg);
|
|
if (ret) {
|
|
dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n",
|
|
ERR_PTR(ret));
|
|
goto free_bufs;
|
|
}
|
|
|
|
if (effective_speed_hz)
|
|
*effective_speed_hz = xfer.effective_speed_hz;
|
|
|
|
for (i = 0; i < max_count - count_skip; i++) {
|
|
val = tsc2046_adc_get_value(&rx_buf[count_skip + i]);
|
|
val_normalized += val;
|
|
}
|
|
|
|
ret = DIV_ROUND_UP(val_normalized, max_count - count_skip);
|
|
|
|
free_bufs:
|
|
kfree(rx_buf);
|
|
free_tx:
|
|
kfree(tx_buf);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv,
|
|
unsigned int group,
|
|
unsigned int ch_idx)
|
|
{
|
|
struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
|
|
struct tsc2046_adc_group_layout *cur;
|
|
unsigned int max_count, count_skip;
|
|
unsigned int offset = 0;
|
|
|
|
if (group)
|
|
offset = priv->l[group - 1].offset + priv->l[group - 1].count;
|
|
|
|
count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
|
|
max_count = count_skip + ch->oversampling_ratio;
|
|
|
|
cur = &priv->l[group];
|
|
cur->offset = offset;
|
|
cur->count = max_count;
|
|
cur->skip = count_skip;
|
|
|
|
return sizeof(*priv->tx) * max_count;
|
|
}
|
|
|
|
static void tsc2046_adc_group_set_cmd(struct tsc2046_adc_priv *priv,
|
|
unsigned int group, int ch_idx)
|
|
{
|
|
struct tsc2046_adc_group_layout *l = &priv->l[group];
|
|
unsigned int i;
|
|
u8 cmd;
|
|
|
|
/*
|
|
* Do not enable automatic power down on working samples. Otherwise the
|
|
* plates will never be completely charged.
|
|
*/
|
|
cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
|
|
|
|
for (i = 0; i < l->count - 1; i++)
|
|
priv->tx[l->offset + i].cmd = cmd;
|
|
|
|
/* automatically power down on last sample */
|
|
priv->tx[l->offset + i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
|
|
}
|
|
|
|
static u16 tsc2046_adc_get_val(struct tsc2046_adc_priv *priv, int group)
|
|
{
|
|
struct tsc2046_adc_group_layout *l;
|
|
unsigned int val, val_normalized = 0;
|
|
int valid_count, i;
|
|
|
|
l = &priv->l[group];
|
|
valid_count = l->count - l->skip;
|
|
|
|
for (i = 0; i < valid_count; i++) {
|
|
val = tsc2046_adc_get_value(&priv->rx[l->offset + l->skip + i]);
|
|
val_normalized += val;
|
|
}
|
|
|
|
return DIV_ROUND_UP(val_normalized, valid_count);
|
|
}
|
|
|
|
static int tsc2046_adc_scan(struct iio_dev *indio_dev)
|
|
{
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
struct device *dev = &priv->spi->dev;
|
|
int group;
|
|
int ret;
|
|
|
|
ret = spi_sync(priv->spi, &priv->msg);
|
|
if (ret < 0) {
|
|
dev_err_ratelimited(dev, "SPI transfer failed: %pe\n", ERR_PTR(ret));
|
|
return ret;
|
|
}
|
|
|
|
for (group = 0; group < priv->groups; group++)
|
|
priv->scan_buf.data[group] = tsc2046_adc_get_val(priv, group);
|
|
|
|
ret = iio_push_to_buffers_with_timestamp(indio_dev, &priv->scan_buf,
|
|
iio_get_time_ns(indio_dev));
|
|
/* If the consumer is kfifo, we may get a EBUSY here - ignore it. */
|
|
if (ret < 0 && ret != -EBUSY) {
|
|
dev_err_ratelimited(dev, "Failed to push scan buffer %pe\n",
|
|
ERR_PTR(ret));
|
|
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static irqreturn_t tsc2046_adc_trigger_handler(int irq, void *p)
|
|
{
|
|
struct iio_poll_func *pf = p;
|
|
struct iio_dev *indio_dev = pf->indio_dev;
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
|
|
mutex_lock(&priv->slock);
|
|
tsc2046_adc_scan(indio_dev);
|
|
mutex_unlock(&priv->slock);
|
|
|
|
iio_trigger_notify_done(indio_dev->trig);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
static int tsc2046_adc_read_raw(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
int *val, int *val2, long m)
|
|
{
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
switch (m) {
|
|
case IIO_CHAN_INFO_RAW:
|
|
ret = tsc2046_adc_read_one(priv, chan->channel, NULL);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
*val = ret;
|
|
|
|
return IIO_VAL_INT;
|
|
case IIO_CHAN_INFO_SCALE:
|
|
/*
|
|
* Note: the TSC2046 has internal voltage divider on the VBAT
|
|
* line. This divider can be influenced by external divider.
|
|
* So, it is better to use external voltage-divider driver
|
|
* instead, which is calculating complete chain.
|
|
*/
|
|
*val = TI_TSC2046_INT_VREF;
|
|
*val2 = chan->scan_type.realbits;
|
|
return IIO_VAL_FRACTIONAL_LOG2;
|
|
}
|
|
|
|
return -EINVAL;
|
|
}
|
|
|
|
static int tsc2046_adc_update_scan_mode(struct iio_dev *indio_dev,
|
|
const unsigned long *active_scan_mask)
|
|
{
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
unsigned int ch_idx, group = 0;
|
|
size_t size;
|
|
|
|
mutex_lock(&priv->slock);
|
|
|
|
size = 0;
|
|
for_each_set_bit(ch_idx, active_scan_mask, ARRAY_SIZE(priv->l)) {
|
|
size += tsc2046_adc_group_set_layout(priv, group, ch_idx);
|
|
tsc2046_adc_group_set_cmd(priv, group, ch_idx);
|
|
group++;
|
|
}
|
|
|
|
priv->groups = group;
|
|
priv->xfer.len = size;
|
|
priv->time_per_scan_us = size * 8 * priv->time_per_bit_ns / NSEC_PER_USEC;
|
|
|
|
if (priv->scan_interval_us < priv->time_per_scan_us)
|
|
dev_warn(&priv->spi->dev, "The scan interval (%d) is less then calculated scan time (%d)\n",
|
|
priv->scan_interval_us, priv->time_per_scan_us);
|
|
|
|
mutex_unlock(&priv->slock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct iio_info tsc2046_adc_info = {
|
|
.read_raw = tsc2046_adc_read_raw,
|
|
.update_scan_mode = tsc2046_adc_update_scan_mode,
|
|
};
|
|
|
|
static enum hrtimer_restart tsc2046_adc_timer(struct hrtimer *hrtimer)
|
|
{
|
|
struct tsc2046_adc_priv *priv = container_of(hrtimer,
|
|
struct tsc2046_adc_priv,
|
|
trig_timer);
|
|
unsigned long flags;
|
|
|
|
/*
|
|
* This state machine should address following challenges :
|
|
* - the interrupt source is based on level shifter attached to the X
|
|
* channel of ADC. It will change the state every time we switch
|
|
* between channels. So, we need to disable IRQ if we do
|
|
* iio_trigger_poll().
|
|
* - we should do iio_trigger_poll() at some reduced sample rate
|
|
* - we should still trigger for some amount of time after last
|
|
* interrupt with enabled IRQ was processed.
|
|
*/
|
|
|
|
spin_lock_irqsave(&priv->state_lock, flags);
|
|
switch (priv->state) {
|
|
case TSC2046_STATE_ENABLE_IRQ:
|
|
if (priv->poll_cnt < TI_TSC2046_POLL_CNT) {
|
|
priv->poll_cnt++;
|
|
hrtimer_start(&priv->trig_timer,
|
|
ns_to_ktime(priv->scan_interval_us *
|
|
NSEC_PER_USEC),
|
|
HRTIMER_MODE_REL_SOFT);
|
|
|
|
if (priv->poll_cnt >= TI_TSC2046_MIN_POLL_CNT) {
|
|
priv->state = TSC2046_STATE_POLL_IRQ_DISABLE;
|
|
enable_irq(priv->spi->irq);
|
|
} else {
|
|
priv->state = TSC2046_STATE_POLL;
|
|
}
|
|
} else {
|
|
priv->state = TSC2046_STATE_STANDBY;
|
|
enable_irq(priv->spi->irq);
|
|
}
|
|
break;
|
|
case TSC2046_STATE_POLL_IRQ_DISABLE:
|
|
disable_irq_nosync(priv->spi->irq);
|
|
fallthrough;
|
|
case TSC2046_STATE_POLL:
|
|
priv->state = TSC2046_STATE_ENABLE_IRQ;
|
|
/* iio_trigger_poll() starts hrtimer */
|
|
iio_trigger_poll(priv->trig);
|
|
break;
|
|
case TSC2046_STATE_SHUTDOWN:
|
|
break;
|
|
case TSC2046_STATE_STANDBY:
|
|
fallthrough;
|
|
default:
|
|
dev_warn(&priv->spi->dev, "Got unexpected state: %i\n",
|
|
priv->state);
|
|
break;
|
|
}
|
|
spin_unlock_irqrestore(&priv->state_lock, flags);
|
|
|
|
return HRTIMER_NORESTART;
|
|
}
|
|
|
|
static irqreturn_t tsc2046_adc_irq(int irq, void *dev_id)
|
|
{
|
|
struct iio_dev *indio_dev = dev_id;
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
unsigned long flags;
|
|
|
|
hrtimer_try_to_cancel(&priv->trig_timer);
|
|
|
|
spin_lock_irqsave(&priv->state_lock, flags);
|
|
if (priv->state != TSC2046_STATE_SHUTDOWN) {
|
|
priv->state = TSC2046_STATE_ENABLE_IRQ;
|
|
priv->poll_cnt = 0;
|
|
|
|
/* iio_trigger_poll() starts hrtimer */
|
|
disable_irq_nosync(priv->spi->irq);
|
|
iio_trigger_poll(priv->trig);
|
|
}
|
|
spin_unlock_irqrestore(&priv->state_lock, flags);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
static void tsc2046_adc_reenable_trigger(struct iio_trigger *trig)
|
|
{
|
|
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
ktime_t tim;
|
|
|
|
/*
|
|
* We can sample it as fast as we can, but usually we do not need so
|
|
* many samples. Reduce the sample rate for default (touchscreen) use
|
|
* case.
|
|
*/
|
|
tim = ns_to_ktime((priv->scan_interval_us - priv->time_per_scan_us) *
|
|
NSEC_PER_USEC);
|
|
hrtimer_start(&priv->trig_timer, tim, HRTIMER_MODE_REL_SOFT);
|
|
}
|
|
|
|
static int tsc2046_adc_set_trigger_state(struct iio_trigger *trig, bool enable)
|
|
{
|
|
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
|
|
struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
|
|
unsigned long flags;
|
|
|
|
if (enable) {
|
|
spin_lock_irqsave(&priv->state_lock, flags);
|
|
if (priv->state == TSC2046_STATE_SHUTDOWN) {
|
|
priv->state = TSC2046_STATE_STANDBY;
|
|
enable_irq(priv->spi->irq);
|
|
}
|
|
spin_unlock_irqrestore(&priv->state_lock, flags);
|
|
} else {
|
|
spin_lock_irqsave(&priv->state_lock, flags);
|
|
|
|
if (priv->state == TSC2046_STATE_STANDBY ||
|
|
priv->state == TSC2046_STATE_POLL_IRQ_DISABLE)
|
|
disable_irq_nosync(priv->spi->irq);
|
|
|
|
priv->state = TSC2046_STATE_SHUTDOWN;
|
|
spin_unlock_irqrestore(&priv->state_lock, flags);
|
|
|
|
hrtimer_cancel(&priv->trig_timer);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct iio_trigger_ops tsc2046_adc_trigger_ops = {
|
|
.set_trigger_state = tsc2046_adc_set_trigger_state,
|
|
.reenable = tsc2046_adc_reenable_trigger,
|
|
};
|
|
|
|
static int tsc2046_adc_setup_spi_msg(struct tsc2046_adc_priv *priv)
|
|
{
|
|
unsigned int ch_idx;
|
|
size_t size;
|
|
int ret;
|
|
|
|
/*
|
|
* Make dummy read to set initial power state and get real SPI clock
|
|
* freq. It seems to be not important which channel is used for this
|
|
* case.
|
|
*/
|
|
ret = tsc2046_adc_read_one(priv, TI_TSC2046_ADDR_TEMP0,
|
|
&priv->effective_speed_hz);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
/*
|
|
* In case SPI controller do not report effective_speed_hz, use
|
|
* configure value and hope it will match.
|
|
*/
|
|
if (!priv->effective_speed_hz)
|
|
priv->effective_speed_hz = priv->spi->max_speed_hz;
|
|
|
|
|
|
priv->scan_interval_us = TI_TSC2046_SAMPLE_INTERVAL_US;
|
|
priv->time_per_bit_ns = DIV_ROUND_UP(NSEC_PER_SEC,
|
|
priv->effective_speed_hz);
|
|
|
|
/*
|
|
* Calculate and allocate maximal size buffer if all channels are
|
|
* enabled.
|
|
*/
|
|
size = 0;
|
|
for (ch_idx = 0; ch_idx < ARRAY_SIZE(priv->l); ch_idx++)
|
|
size += tsc2046_adc_group_set_layout(priv, ch_idx, ch_idx);
|
|
|
|
if (size > PAGE_SIZE) {
|
|
dev_err(&priv->spi->dev,
|
|
"Calculated scan buffer is too big. Try to reduce spi-max-frequency, settling-time-us or oversampling-ratio\n");
|
|
return -ENOSPC;
|
|
}
|
|
|
|
priv->tx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
|
|
if (!priv->tx)
|
|
return -ENOMEM;
|
|
|
|
priv->rx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
|
|
if (!priv->rx)
|
|
return -ENOMEM;
|
|
|
|
priv->xfer.tx_buf = priv->tx;
|
|
priv->xfer.rx_buf = priv->rx;
|
|
priv->xfer.len = size;
|
|
spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv *priv)
|
|
{
|
|
struct fwnode_handle *child;
|
|
struct device *dev = &priv->spi->dev;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(priv->ch_cfg); i++) {
|
|
priv->ch_cfg[i].settling_time_us = 1;
|
|
priv->ch_cfg[i].oversampling_ratio = 1;
|
|
}
|
|
|
|
device_for_each_child_node(dev, child) {
|
|
u32 stl, overs, reg;
|
|
int ret;
|
|
|
|
ret = fwnode_property_read_u32(child, "reg", ®);
|
|
if (ret) {
|
|
dev_err(dev, "invalid reg on %pfw, err: %pe\n", child,
|
|
ERR_PTR(ret));
|
|
continue;
|
|
}
|
|
|
|
if (reg >= ARRAY_SIZE(priv->ch_cfg)) {
|
|
dev_err(dev, "%pfw: Unsupported reg value: %i, max supported is: %zu.\n",
|
|
child, reg, ARRAY_SIZE(priv->ch_cfg));
|
|
continue;
|
|
}
|
|
|
|
ret = fwnode_property_read_u32(child, "settling-time-us", &stl);
|
|
if (!ret)
|
|
priv->ch_cfg[reg].settling_time_us = stl;
|
|
|
|
ret = fwnode_property_read_u32(child, "oversampling-ratio",
|
|
&overs);
|
|
if (!ret)
|
|
priv->ch_cfg[reg].oversampling_ratio = overs;
|
|
}
|
|
}
|
|
|
|
static int tsc2046_adc_probe(struct spi_device *spi)
|
|
{
|
|
const struct tsc2046_adc_dcfg *dcfg;
|
|
struct device *dev = &spi->dev;
|
|
struct tsc2046_adc_priv *priv;
|
|
struct iio_dev *indio_dev;
|
|
struct iio_trigger *trig;
|
|
int ret;
|
|
|
|
if (spi->max_speed_hz > TI_TSC2046_MAX_CLK_FREQ) {
|
|
dev_err(dev, "SPI max_speed_hz is too high: %d Hz. Max supported freq is %zu Hz\n",
|
|
spi->max_speed_hz, TI_TSC2046_MAX_CLK_FREQ);
|
|
return -EINVAL;
|
|
}
|
|
|
|
dcfg = device_get_match_data(dev);
|
|
if (!dcfg)
|
|
return -EINVAL;
|
|
|
|
spi->bits_per_word = 8;
|
|
spi->mode &= ~SPI_MODE_X_MASK;
|
|
spi->mode |= SPI_MODE_0;
|
|
ret = spi_setup(spi);
|
|
if (ret < 0)
|
|
return dev_err_probe(dev, ret, "Error in SPI setup\n");
|
|
|
|
indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
|
|
if (!indio_dev)
|
|
return -ENOMEM;
|
|
|
|
priv = iio_priv(indio_dev);
|
|
priv->dcfg = dcfg;
|
|
|
|
priv->spi = spi;
|
|
|
|
indio_dev->name = TI_TSC2046_NAME;
|
|
indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED;
|
|
indio_dev->channels = dcfg->channels;
|
|
indio_dev->num_channels = dcfg->num_channels;
|
|
indio_dev->info = &tsc2046_adc_info;
|
|
|
|
tsc2046_adc_parse_fwnode(priv);
|
|
|
|
ret = tsc2046_adc_setup_spi_msg(priv);
|
|
if (ret)
|
|
return ret;
|
|
|
|
mutex_init(&priv->slock);
|
|
|
|
ret = devm_request_irq(dev, spi->irq, &tsc2046_adc_irq,
|
|
IRQF_NO_AUTOEN, indio_dev->name, indio_dev);
|
|
if (ret)
|
|
return ret;
|
|
|
|
trig = devm_iio_trigger_alloc(dev, "touchscreen-%s", indio_dev->name);
|
|
if (!trig)
|
|
return -ENOMEM;
|
|
|
|
priv->trig = trig;
|
|
iio_trigger_set_drvdata(trig, indio_dev);
|
|
trig->ops = &tsc2046_adc_trigger_ops;
|
|
|
|
spin_lock_init(&priv->state_lock);
|
|
priv->state = TSC2046_STATE_SHUTDOWN;
|
|
hrtimer_init(&priv->trig_timer, CLOCK_MONOTONIC,
|
|
HRTIMER_MODE_REL_SOFT);
|
|
priv->trig_timer.function = tsc2046_adc_timer;
|
|
|
|
ret = devm_iio_trigger_register(dev, trig);
|
|
if (ret) {
|
|
dev_err(dev, "failed to register trigger\n");
|
|
return ret;
|
|
}
|
|
|
|
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
|
|
&tsc2046_adc_trigger_handler, NULL);
|
|
if (ret) {
|
|
dev_err(dev, "Failed to setup triggered buffer\n");
|
|
return ret;
|
|
}
|
|
|
|
/* set default trigger */
|
|
indio_dev->trig = iio_trigger_get(priv->trig);
|
|
|
|
return devm_iio_device_register(dev, indio_dev);
|
|
}
|
|
|
|
static const struct of_device_id ads7950_of_table[] = {
|
|
{ .compatible = "ti,tsc2046e-adc", .data = &tsc2046_adc_dcfg_tsc2046e },
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(of, ads7950_of_table);
|
|
|
|
static struct spi_driver tsc2046_adc_driver = {
|
|
.driver = {
|
|
.name = "tsc2046",
|
|
.of_match_table = ads7950_of_table,
|
|
},
|
|
.probe = tsc2046_adc_probe,
|
|
};
|
|
module_spi_driver(tsc2046_adc_driver);
|
|
|
|
MODULE_AUTHOR("Oleksij Rempel <kernel@pengutronix.de>");
|
|
MODULE_DESCRIPTION("TI TSC2046 ADC");
|
|
MODULE_LICENSE("GPL v2");
|