18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright 2018 Google LLC. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution. 68c2ecf20Sopenharmony_ci * Based on SX9500 driver and Semtech driver using the input framework 78c2ecf20Sopenharmony_ci * <https://my.syncplicity.com/share/teouwsim8niiaud/ 88c2ecf20Sopenharmony_ci * linux-driver-SX9310_NoSmartHSensing>. 98c2ecf20Sopenharmony_ci * Reworked in April 2019 by Evan Green <evgreen@chromium.org> 108c2ecf20Sopenharmony_ci * and in January 2020 by Daniel Campello <campello@chromium.org>. 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/acpi.h> 148c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 158c2ecf20Sopenharmony_ci#include <linux/delay.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci#include <linux/irq.h> 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 208c2ecf20Sopenharmony_ci#include <linux/module.h> 218c2ecf20Sopenharmony_ci#include <linux/pm.h> 228c2ecf20Sopenharmony_ci#include <linux/regmap.h> 238c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 278c2ecf20Sopenharmony_ci#include <linux/iio/events.h> 288c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 298c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 308c2ecf20Sopenharmony_ci#include <linux/iio/trigger.h> 318c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 328c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Register definitions. */ 358c2ecf20Sopenharmony_ci#define SX9310_REG_IRQ_SRC 0x00 368c2ecf20Sopenharmony_ci#define SX9310_REG_STAT0 0x01 378c2ecf20Sopenharmony_ci#define SX9310_REG_STAT1 0x02 388c2ecf20Sopenharmony_ci#define SX9310_REG_STAT1_COMPSTAT_MASK GENMASK(3, 0) 398c2ecf20Sopenharmony_ci#define SX9310_REG_IRQ_MSK 0x03 408c2ecf20Sopenharmony_ci#define SX9310_CONVDONE_IRQ BIT(3) 418c2ecf20Sopenharmony_ci#define SX9310_FAR_IRQ BIT(5) 428c2ecf20Sopenharmony_ci#define SX9310_CLOSE_IRQ BIT(6) 438c2ecf20Sopenharmony_ci#define SX9310_REG_IRQ_FUNC 0x04 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL0 0x10 468c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL0_SENSOREN_MASK GENMASK(3, 0) 478c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK GENMASK(7, 4) 488c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS 0x01 498c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL1 0x11 508c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL2 0x12 518c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 (0x02 << 6) 528c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC (0x01 << 2) 538c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL3 0x13 548c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL3_GAIN0_X8 (0x03 << 2) 558c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL3_GAIN12_X4 0x02 568c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL4 0x14 578c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST 0x07 588c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL5 0x15 598c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL5_RANGE_SMALL (0x03 << 6) 608c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 (0x01 << 2) 618c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL5_RAWFILT_1P25 0x02 628c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL6 0x16 638c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT 0x20 648c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL7 0x17 658c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 (0x01 << 3) 668c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 0x05 678c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL8 0x18 688c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL9 0x19 698c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL8_9_PTHRESH_28 (0x08 << 3) 708c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 (0x11 << 3) 718c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 0x03 728c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 0x05 738c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL10 0x1a 748c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL10_HYST_6PCT (0x01 << 4) 758c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 0x01 768c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL11 0x1b 778c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL12 0x1c 788c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL13 0x1d 798c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL14 0x1e 808c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL15 0x1f 818c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL16 0x20 828c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL17 0x21 838c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL18 0x22 848c2ecf20Sopenharmony_ci#define SX9310_REG_PROX_CTRL19 0x23 858c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL0 0x2a 868c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES (0x02 << 5) 878c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL0_SARHYST_8 (0x02 << 3) 888c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL1 0x2b 898c2ecf20Sopenharmony_ci/* Each increment of the slope register is 0.0078125. */ 908c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL1_SLOPE(_hnslope) (_hnslope / 78125) 918c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL2 0x2c 928c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define SX9310_REG_SENSOR_SEL 0x30 958c2ecf20Sopenharmony_ci#define SX9310_REG_USE_MSB 0x31 968c2ecf20Sopenharmony_ci#define SX9310_REG_USE_LSB 0x32 978c2ecf20Sopenharmony_ci#define SX9310_REG_AVG_MSB 0x33 988c2ecf20Sopenharmony_ci#define SX9310_REG_AVG_LSB 0x34 998c2ecf20Sopenharmony_ci#define SX9310_REG_DIFF_MSB 0x35 1008c2ecf20Sopenharmony_ci#define SX9310_REG_DIFF_LSB 0x36 1018c2ecf20Sopenharmony_ci#define SX9310_REG_OFFSET_MSB 0x37 1028c2ecf20Sopenharmony_ci#define SX9310_REG_OFFSET_LSB 0x38 1038c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_MSB 0x39 1048c2ecf20Sopenharmony_ci#define SX9310_REG_SAR_LSB 0x3a 1058c2ecf20Sopenharmony_ci#define SX9310_REG_I2C_ADDR 0x40 1068c2ecf20Sopenharmony_ci#define SX9310_REG_PAUSE 0x41 1078c2ecf20Sopenharmony_ci#define SX9310_REG_WHOAMI 0x42 1088c2ecf20Sopenharmony_ci#define SX9310_WHOAMI_VALUE 0x01 1098c2ecf20Sopenharmony_ci#define SX9311_WHOAMI_VALUE 0x02 1108c2ecf20Sopenharmony_ci#define SX9310_REG_RESET 0x7f 1118c2ecf20Sopenharmony_ci#define SX9310_SOFT_RESET 0xde 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */ 1158c2ecf20Sopenharmony_ci#define SX9310_NUM_CHANNELS 4 1168c2ecf20Sopenharmony_cistatic_assert(SX9310_NUM_CHANNELS < BITS_PER_LONG); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistruct sx9310_data { 1198c2ecf20Sopenharmony_ci /* Serialize access to registers and channel configuration */ 1208c2ecf20Sopenharmony_ci struct mutex mutex; 1218c2ecf20Sopenharmony_ci struct i2c_client *client; 1228c2ecf20Sopenharmony_ci struct iio_trigger *trig; 1238c2ecf20Sopenharmony_ci struct regmap *regmap; 1248c2ecf20Sopenharmony_ci struct regulator_bulk_data supplies[2]; 1258c2ecf20Sopenharmony_ci /* 1268c2ecf20Sopenharmony_ci * Last reading of the proximity status for each channel. 1278c2ecf20Sopenharmony_ci * We only send an event to user space when this changes. 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ci unsigned long chan_prox_stat; 1308c2ecf20Sopenharmony_ci bool trigger_enabled; 1318c2ecf20Sopenharmony_ci /* Ensure correct alignment of timestamp when present. */ 1328c2ecf20Sopenharmony_ci struct { 1338c2ecf20Sopenharmony_ci __be16 channels[SX9310_NUM_CHANNELS]; 1348c2ecf20Sopenharmony_ci s64 ts __aligned(8); 1358c2ecf20Sopenharmony_ci } buffer; 1368c2ecf20Sopenharmony_ci /* Remember enabled channels and sample rate during suspend. */ 1378c2ecf20Sopenharmony_ci unsigned int suspend_ctrl0; 1388c2ecf20Sopenharmony_ci struct completion completion; 1398c2ecf20Sopenharmony_ci unsigned long chan_read; 1408c2ecf20Sopenharmony_ci unsigned long chan_event; 1418c2ecf20Sopenharmony_ci unsigned int whoami; 1428c2ecf20Sopenharmony_ci}; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic const struct iio_event_spec sx9310_events[] = { 1458c2ecf20Sopenharmony_ci { 1468c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 1478c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_EITHER, 1488c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_ENABLE), 1498c2ecf20Sopenharmony_ci }, 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci#define SX9310_NAMED_CHANNEL(idx, name) \ 1538c2ecf20Sopenharmony_ci { \ 1548c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, \ 1558c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 1568c2ecf20Sopenharmony_ci .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1578c2ecf20Sopenharmony_ci .indexed = 1, \ 1588c2ecf20Sopenharmony_ci .channel = idx, \ 1598c2ecf20Sopenharmony_ci .extend_name = name, \ 1608c2ecf20Sopenharmony_ci .address = SX9310_REG_DIFF_MSB, \ 1618c2ecf20Sopenharmony_ci .event_spec = sx9310_events, \ 1628c2ecf20Sopenharmony_ci .num_event_specs = ARRAY_SIZE(sx9310_events), \ 1638c2ecf20Sopenharmony_ci .scan_index = idx, \ 1648c2ecf20Sopenharmony_ci .scan_type = { \ 1658c2ecf20Sopenharmony_ci .sign = 's', \ 1668c2ecf20Sopenharmony_ci .realbits = 12, \ 1678c2ecf20Sopenharmony_ci .storagebits = 16, \ 1688c2ecf20Sopenharmony_ci .endianness = IIO_BE, \ 1698c2ecf20Sopenharmony_ci }, \ 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci#define SX9310_CHANNEL(idx) SX9310_NAMED_CHANNEL(idx, NULL) 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic const struct iio_chan_spec sx9310_channels[] = { 1748c2ecf20Sopenharmony_ci SX9310_CHANNEL(0), /* CS0 */ 1758c2ecf20Sopenharmony_ci SX9310_CHANNEL(1), /* CS1 */ 1768c2ecf20Sopenharmony_ci SX9310_CHANNEL(2), /* CS2 */ 1778c2ecf20Sopenharmony_ci SX9310_NAMED_CHANNEL(3, "comb"), /* COMB */ 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci IIO_CHAN_SOFT_TIMESTAMP(4), 1808c2ecf20Sopenharmony_ci}; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/* 1838c2ecf20Sopenharmony_ci * Each entry contains the integer part (val) and the fractional part, in micro 1848c2ecf20Sopenharmony_ci * seconds. It conforms to the IIO output IIO_VAL_INT_PLUS_MICRO. 1858c2ecf20Sopenharmony_ci */ 1868c2ecf20Sopenharmony_cistatic const struct { 1878c2ecf20Sopenharmony_ci int val; 1888c2ecf20Sopenharmony_ci int val2; 1898c2ecf20Sopenharmony_ci} sx9310_samp_freq_table[] = { 1908c2ecf20Sopenharmony_ci { 500, 0 }, /* 0000: Min (no idle time) */ 1918c2ecf20Sopenharmony_ci { 66, 666666 }, /* 0001: 15 ms */ 1928c2ecf20Sopenharmony_ci { 33, 333333 }, /* 0010: 30 ms (Typ.) */ 1938c2ecf20Sopenharmony_ci { 22, 222222 }, /* 0011: 45 ms */ 1948c2ecf20Sopenharmony_ci { 16, 666666 }, /* 0100: 60 ms */ 1958c2ecf20Sopenharmony_ci { 11, 111111 }, /* 0101: 90 ms */ 1968c2ecf20Sopenharmony_ci { 8, 333333 }, /* 0110: 120 ms */ 1978c2ecf20Sopenharmony_ci { 5, 0 }, /* 0111: 200 ms */ 1988c2ecf20Sopenharmony_ci { 2, 500000 }, /* 1000: 400 ms */ 1998c2ecf20Sopenharmony_ci { 1, 666666 }, /* 1001: 600 ms */ 2008c2ecf20Sopenharmony_ci { 1, 250000 }, /* 1010: 800 ms */ 2018c2ecf20Sopenharmony_ci { 1, 0 }, /* 1011: 1 s */ 2028c2ecf20Sopenharmony_ci { 0, 500000 }, /* 1100: 2 s */ 2038c2ecf20Sopenharmony_ci { 0, 333333 }, /* 1101: 3 s */ 2048c2ecf20Sopenharmony_ci { 0, 250000 }, /* 1110: 4 s */ 2058c2ecf20Sopenharmony_ci { 0, 200000 }, /* 1111: 5 s */ 2068c2ecf20Sopenharmony_ci}; 2078c2ecf20Sopenharmony_cistatic const unsigned int sx9310_scan_period_table[] = { 2088c2ecf20Sopenharmony_ci 2, 15, 30, 45, 60, 90, 120, 200, 2098c2ecf20Sopenharmony_ci 400, 600, 800, 1000, 2000, 3000, 4000, 5000, 2108c2ecf20Sopenharmony_ci}; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_cistatic ssize_t sx9310_show_samp_freq_avail(struct device *dev, 2138c2ecf20Sopenharmony_ci struct device_attribute *attr, 2148c2ecf20Sopenharmony_ci char *buf) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci size_t len = 0; 2178c2ecf20Sopenharmony_ci int i; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++) 2208c2ecf20Sopenharmony_ci len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%d ", 2218c2ecf20Sopenharmony_ci sx9310_samp_freq_table[i].val, 2228c2ecf20Sopenharmony_ci sx9310_samp_freq_table[i].val2); 2238c2ecf20Sopenharmony_ci buf[len - 1] = '\n'; 2248c2ecf20Sopenharmony_ci return len; 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_cistatic IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sx9310_show_samp_freq_avail); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic const struct regmap_range sx9310_writable_reg_ranges[] = { 2298c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_IRQ_MSK, SX9310_REG_IRQ_FUNC), 2308c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19), 2318c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2), 2328c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SENSOR_SEL), 2338c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_OFFSET_MSB, SX9310_REG_OFFSET_LSB), 2348c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_PAUSE, SX9310_REG_PAUSE), 2358c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), 2368c2ecf20Sopenharmony_ci}; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic const struct regmap_access_table sx9310_writeable_regs = { 2398c2ecf20Sopenharmony_ci .yes_ranges = sx9310_writable_reg_ranges, 2408c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(sx9310_writable_reg_ranges), 2418c2ecf20Sopenharmony_ci}; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic const struct regmap_range sx9310_readable_reg_ranges[] = { 2448c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_IRQ_FUNC), 2458c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19), 2468c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2), 2478c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB), 2488c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI), 2498c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), 2508c2ecf20Sopenharmony_ci}; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic const struct regmap_access_table sx9310_readable_regs = { 2538c2ecf20Sopenharmony_ci .yes_ranges = sx9310_readable_reg_ranges, 2548c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(sx9310_readable_reg_ranges), 2558c2ecf20Sopenharmony_ci}; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic const struct regmap_range sx9310_volatile_reg_ranges[] = { 2588c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_STAT1), 2598c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_USE_MSB, SX9310_REG_DIFF_LSB), 2608c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_SAR_MSB, SX9310_REG_SAR_LSB), 2618c2ecf20Sopenharmony_ci regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), 2628c2ecf20Sopenharmony_ci}; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_cistatic const struct regmap_access_table sx9310_volatile_regs = { 2658c2ecf20Sopenharmony_ci .yes_ranges = sx9310_volatile_reg_ranges, 2668c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(sx9310_volatile_reg_ranges), 2678c2ecf20Sopenharmony_ci}; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic const struct regmap_config sx9310_regmap_config = { 2708c2ecf20Sopenharmony_ci .reg_bits = 8, 2718c2ecf20Sopenharmony_ci .val_bits = 8, 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci .max_register = SX9310_REG_RESET, 2748c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci .wr_table = &sx9310_writeable_regs, 2778c2ecf20Sopenharmony_ci .rd_table = &sx9310_readable_regs, 2788c2ecf20Sopenharmony_ci .volatile_table = &sx9310_volatile_regs, 2798c2ecf20Sopenharmony_ci}; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_cistatic int sx9310_update_chan_en(struct sx9310_data *data, 2828c2ecf20Sopenharmony_ci unsigned long chan_read, 2838c2ecf20Sopenharmony_ci unsigned long chan_event) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci int ret; 2868c2ecf20Sopenharmony_ci unsigned long channels = chan_read | chan_event; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci if ((data->chan_read | data->chan_event) != channels) { 2898c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0, 2908c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL0_SENSOREN_MASK, 2918c2ecf20Sopenharmony_ci channels); 2928c2ecf20Sopenharmony_ci if (ret) 2938c2ecf20Sopenharmony_ci return ret; 2948c2ecf20Sopenharmony_ci } 2958c2ecf20Sopenharmony_ci data->chan_read = chan_read; 2968c2ecf20Sopenharmony_ci data->chan_event = chan_event; 2978c2ecf20Sopenharmony_ci return 0; 2988c2ecf20Sopenharmony_ci} 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_cistatic int sx9310_get_read_channel(struct sx9310_data *data, int channel) 3018c2ecf20Sopenharmony_ci{ 3028c2ecf20Sopenharmony_ci return sx9310_update_chan_en(data, data->chan_read | BIT(channel), 3038c2ecf20Sopenharmony_ci data->chan_event); 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic int sx9310_put_read_channel(struct sx9310_data *data, int channel) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci return sx9310_update_chan_en(data, data->chan_read & ~BIT(channel), 3098c2ecf20Sopenharmony_ci data->chan_event); 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic int sx9310_get_event_channel(struct sx9310_data *data, int channel) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci return sx9310_update_chan_en(data, data->chan_read, 3158c2ecf20Sopenharmony_ci data->chan_event | BIT(channel)); 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic int sx9310_put_event_channel(struct sx9310_data *data, int channel) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci return sx9310_update_chan_en(data, data->chan_read, 3218c2ecf20Sopenharmony_ci data->chan_event & ~BIT(channel)); 3228c2ecf20Sopenharmony_ci} 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_cistatic int sx9310_enable_irq(struct sx9310_data *data, unsigned int irq) 3258c2ecf20Sopenharmony_ci{ 3268c2ecf20Sopenharmony_ci if (!data->client->irq) 3278c2ecf20Sopenharmony_ci return 0; 3288c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, irq); 3298c2ecf20Sopenharmony_ci} 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_cistatic int sx9310_disable_irq(struct sx9310_data *data, unsigned int irq) 3328c2ecf20Sopenharmony_ci{ 3338c2ecf20Sopenharmony_ci if (!data->client->irq) 3348c2ecf20Sopenharmony_ci return 0; 3358c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, 0); 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic int sx9310_read_prox_data(struct sx9310_data *data, 3398c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, __be16 *val) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci int ret; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel); 3448c2ecf20Sopenharmony_ci if (ret) 3458c2ecf20Sopenharmony_ci return ret; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val)); 3488c2ecf20Sopenharmony_ci} 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci/* 3518c2ecf20Sopenharmony_ci * If we have no interrupt support, we have to wait for a scan period 3528c2ecf20Sopenharmony_ci * after enabling a channel to get a result. 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_cistatic int sx9310_wait_for_sample(struct sx9310_data *data) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci int ret; 3578c2ecf20Sopenharmony_ci unsigned int val; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val); 3608c2ecf20Sopenharmony_ci if (ret) 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci val = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, val); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci msleep(sx9310_scan_period_table[val]); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci return 0; 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic int sx9310_read_proximity(struct sx9310_data *data, 3718c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, int *val) 3728c2ecf20Sopenharmony_ci{ 3738c2ecf20Sopenharmony_ci int ret; 3748c2ecf20Sopenharmony_ci __be16 rawval; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci ret = sx9310_get_read_channel(data, chan->channel); 3798c2ecf20Sopenharmony_ci if (ret) 3808c2ecf20Sopenharmony_ci goto out; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ); 3838c2ecf20Sopenharmony_ci if (ret) 3848c2ecf20Sopenharmony_ci goto out_put_channel; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci if (data->client->irq) { 3898c2ecf20Sopenharmony_ci ret = wait_for_completion_interruptible(&data->completion); 3908c2ecf20Sopenharmony_ci reinit_completion(&data->completion); 3918c2ecf20Sopenharmony_ci } else { 3928c2ecf20Sopenharmony_ci ret = sx9310_wait_for_sample(data); 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci if (ret) 3988c2ecf20Sopenharmony_ci goto out_disable_irq; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci ret = sx9310_read_prox_data(data, chan, &rawval); 4018c2ecf20Sopenharmony_ci if (ret) 4028c2ecf20Sopenharmony_ci goto out_disable_irq; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci *val = sign_extend32(be16_to_cpu(rawval), 4058c2ecf20Sopenharmony_ci chan->address == SX9310_REG_DIFF_MSB ? 11 : 15); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); 4088c2ecf20Sopenharmony_ci if (ret) 4098c2ecf20Sopenharmony_ci goto out_put_channel; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci ret = sx9310_put_read_channel(data, chan->channel); 4128c2ecf20Sopenharmony_ci if (ret) 4138c2ecf20Sopenharmony_ci goto out; 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci return IIO_VAL_INT; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ciout_disable_irq: 4208c2ecf20Sopenharmony_ci sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); 4218c2ecf20Sopenharmony_ciout_put_channel: 4228c2ecf20Sopenharmony_ci sx9310_put_read_channel(data, chan->channel); 4238c2ecf20Sopenharmony_ciout: 4248c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci return ret; 4278c2ecf20Sopenharmony_ci} 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_cistatic int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci unsigned int regval; 4328c2ecf20Sopenharmony_ci int ret; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, ®val); 4358c2ecf20Sopenharmony_ci if (ret) 4368c2ecf20Sopenharmony_ci return ret; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci regval = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, regval); 4398c2ecf20Sopenharmony_ci *val = sx9310_samp_freq_table[regval].val; 4408c2ecf20Sopenharmony_ci *val2 = sx9310_samp_freq_table[regval].val2; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_cistatic int sx9310_read_raw(struct iio_dev *indio_dev, 4468c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, int *val, 4478c2ecf20Sopenharmony_ci int *val2, long mask) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 4508c2ecf20Sopenharmony_ci int ret; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci if (chan->type != IIO_PROXIMITY) 4538c2ecf20Sopenharmony_ci return -EINVAL; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci switch (mask) { 4568c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 4578c2ecf20Sopenharmony_ci ret = iio_device_claim_direct_mode(indio_dev); 4588c2ecf20Sopenharmony_ci if (ret) 4598c2ecf20Sopenharmony_ci return ret; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci ret = sx9310_read_proximity(data, chan, val); 4628c2ecf20Sopenharmony_ci iio_device_release_direct_mode(indio_dev); 4638c2ecf20Sopenharmony_ci return ret; 4648c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 4658c2ecf20Sopenharmony_ci return sx9310_read_samp_freq(data, val, val2); 4668c2ecf20Sopenharmony_ci default: 4678c2ecf20Sopenharmony_ci return -EINVAL; 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci int i, ret; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++) 4768c2ecf20Sopenharmony_ci if (val == sx9310_samp_freq_table[i].val && 4778c2ecf20Sopenharmony_ci val2 == sx9310_samp_freq_table[i].val2) 4788c2ecf20Sopenharmony_ci break; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (i == ARRAY_SIZE(sx9310_samp_freq_table)) 4818c2ecf20Sopenharmony_ci return -EINVAL; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci ret = regmap_update_bits( 4868c2ecf20Sopenharmony_ci data->regmap, SX9310_REG_PROX_CTRL0, 4878c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, 4888c2ecf20Sopenharmony_ci FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i)); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci return ret; 4938c2ecf20Sopenharmony_ci} 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_cistatic int sx9310_write_raw(struct iio_dev *indio_dev, 4968c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, int val, int val2, 4978c2ecf20Sopenharmony_ci long mask) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci if (chan->type != IIO_PROXIMITY) 5028c2ecf20Sopenharmony_ci return -EINVAL; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci if (mask != IIO_CHAN_INFO_SAMP_FREQ) 5058c2ecf20Sopenharmony_ci return -EINVAL; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci return sx9310_set_samp_freq(data, val, val2); 5088c2ecf20Sopenharmony_ci} 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_cistatic irqreturn_t sx9310_irq_handler(int irq, void *private) 5118c2ecf20Sopenharmony_ci{ 5128c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 5138c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci if (data->trigger_enabled) 5168c2ecf20Sopenharmony_ci iio_trigger_poll(data->trig); 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci /* 5198c2ecf20Sopenharmony_ci * Even if no event is enabled, we need to wake the thread to clear the 5208c2ecf20Sopenharmony_ci * interrupt state by reading SX9310_REG_IRQ_SRC. 5218c2ecf20Sopenharmony_ci * It is not possible to do that here because regmap_read takes a mutex. 5228c2ecf20Sopenharmony_ci */ 5238c2ecf20Sopenharmony_ci return IRQ_WAKE_THREAD; 5248c2ecf20Sopenharmony_ci} 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_cistatic void sx9310_push_events(struct iio_dev *indio_dev) 5278c2ecf20Sopenharmony_ci{ 5288c2ecf20Sopenharmony_ci int ret; 5298c2ecf20Sopenharmony_ci unsigned int val, chan; 5308c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 5318c2ecf20Sopenharmony_ci s64 timestamp = iio_get_time_ns(indio_dev); 5328c2ecf20Sopenharmony_ci unsigned long prox_changed; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci /* Read proximity state on all channels */ 5358c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val); 5368c2ecf20Sopenharmony_ci if (ret) { 5378c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "i2c transfer error in irq\n"); 5388c2ecf20Sopenharmony_ci return; 5398c2ecf20Sopenharmony_ci } 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci /* 5428c2ecf20Sopenharmony_ci * Only iterate over channels with changes on proximity status that have 5438c2ecf20Sopenharmony_ci * events enabled. 5448c2ecf20Sopenharmony_ci */ 5458c2ecf20Sopenharmony_ci prox_changed = (data->chan_prox_stat ^ val) & data->chan_event; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) { 5488c2ecf20Sopenharmony_ci int dir; 5498c2ecf20Sopenharmony_ci u64 ev; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; 5528c2ecf20Sopenharmony_ci ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, 5538c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, dir); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci iio_push_event(indio_dev, ev, timestamp); 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci data->chan_prox_stat = val; 5588c2ecf20Sopenharmony_ci} 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_cistatic irqreturn_t sx9310_irq_thread_handler(int irq, void *private) 5618c2ecf20Sopenharmony_ci{ 5628c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 5638c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 5648c2ecf20Sopenharmony_ci int ret; 5658c2ecf20Sopenharmony_ci unsigned int val; 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val); 5708c2ecf20Sopenharmony_ci if (ret) { 5718c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "i2c transfer error in irq\n"); 5728c2ecf20Sopenharmony_ci goto out; 5738c2ecf20Sopenharmony_ci } 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ)) 5768c2ecf20Sopenharmony_ci sx9310_push_events(indio_dev); 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci if (val & SX9310_CONVDONE_IRQ) 5798c2ecf20Sopenharmony_ci complete(&data->completion); 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ciout: 5828c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5858c2ecf20Sopenharmony_ci} 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_cistatic int sx9310_read_event_config(struct iio_dev *indio_dev, 5888c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5898c2ecf20Sopenharmony_ci enum iio_event_type type, 5908c2ecf20Sopenharmony_ci enum iio_event_direction dir) 5918c2ecf20Sopenharmony_ci{ 5928c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci return !!(data->chan_event & BIT(chan->channel)); 5958c2ecf20Sopenharmony_ci} 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_cistatic int sx9310_write_event_config(struct iio_dev *indio_dev, 5988c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5998c2ecf20Sopenharmony_ci enum iio_event_type type, 6008c2ecf20Sopenharmony_ci enum iio_event_direction dir, int state) 6018c2ecf20Sopenharmony_ci{ 6028c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 6038c2ecf20Sopenharmony_ci unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ; 6048c2ecf20Sopenharmony_ci int ret; 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci /* If the state hasn't changed, there's nothing to do. */ 6078c2ecf20Sopenharmony_ci if (!!(data->chan_event & BIT(chan->channel)) == state) 6088c2ecf20Sopenharmony_ci return 0; 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 6118c2ecf20Sopenharmony_ci if (state) { 6128c2ecf20Sopenharmony_ci ret = sx9310_get_event_channel(data, chan->channel); 6138c2ecf20Sopenharmony_ci if (ret) 6148c2ecf20Sopenharmony_ci goto out_unlock; 6158c2ecf20Sopenharmony_ci if (!(data->chan_event & ~BIT(chan->channel))) { 6168c2ecf20Sopenharmony_ci ret = sx9310_enable_irq(data, eventirq); 6178c2ecf20Sopenharmony_ci if (ret) 6188c2ecf20Sopenharmony_ci sx9310_put_event_channel(data, chan->channel); 6198c2ecf20Sopenharmony_ci } 6208c2ecf20Sopenharmony_ci } else { 6218c2ecf20Sopenharmony_ci ret = sx9310_put_event_channel(data, chan->channel); 6228c2ecf20Sopenharmony_ci if (ret) 6238c2ecf20Sopenharmony_ci goto out_unlock; 6248c2ecf20Sopenharmony_ci if (!data->chan_event) { 6258c2ecf20Sopenharmony_ci ret = sx9310_disable_irq(data, eventirq); 6268c2ecf20Sopenharmony_ci if (ret) 6278c2ecf20Sopenharmony_ci sx9310_get_event_channel(data, chan->channel); 6288c2ecf20Sopenharmony_ci } 6298c2ecf20Sopenharmony_ci } 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ciout_unlock: 6328c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 6338c2ecf20Sopenharmony_ci return ret; 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cistatic struct attribute *sx9310_attributes[] = { 6378c2ecf20Sopenharmony_ci &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 6388c2ecf20Sopenharmony_ci NULL 6398c2ecf20Sopenharmony_ci}; 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_cistatic const struct attribute_group sx9310_attribute_group = { 6428c2ecf20Sopenharmony_ci .attrs = sx9310_attributes, 6438c2ecf20Sopenharmony_ci}; 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_cistatic const struct iio_info sx9310_info = { 6468c2ecf20Sopenharmony_ci .attrs = &sx9310_attribute_group, 6478c2ecf20Sopenharmony_ci .read_raw = sx9310_read_raw, 6488c2ecf20Sopenharmony_ci .write_raw = sx9310_write_raw, 6498c2ecf20Sopenharmony_ci .read_event_config = sx9310_read_event_config, 6508c2ecf20Sopenharmony_ci .write_event_config = sx9310_write_event_config, 6518c2ecf20Sopenharmony_ci}; 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_cistatic int sx9310_set_trigger_state(struct iio_trigger *trig, bool state) 6548c2ecf20Sopenharmony_ci{ 6558c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 6568c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 6578c2ecf20Sopenharmony_ci int ret = 0; 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci if (state) 6628c2ecf20Sopenharmony_ci ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ); 6638c2ecf20Sopenharmony_ci else if (!data->chan_read) 6648c2ecf20Sopenharmony_ci ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); 6658c2ecf20Sopenharmony_ci if (ret) 6668c2ecf20Sopenharmony_ci goto out; 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci data->trigger_enabled = state; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ciout: 6718c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci return ret; 6748c2ecf20Sopenharmony_ci} 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_cistatic const struct iio_trigger_ops sx9310_trigger_ops = { 6778c2ecf20Sopenharmony_ci .set_trigger_state = sx9310_set_trigger_state, 6788c2ecf20Sopenharmony_ci}; 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_cistatic irqreturn_t sx9310_trigger_handler(int irq, void *private) 6818c2ecf20Sopenharmony_ci{ 6828c2ecf20Sopenharmony_ci struct iio_poll_func *pf = private; 6838c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = pf->indio_dev; 6848c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 6858c2ecf20Sopenharmony_ci __be16 val; 6868c2ecf20Sopenharmony_ci int bit, ret, i = 0; 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci for_each_set_bit(bit, indio_dev->active_scan_mask, 6918c2ecf20Sopenharmony_ci indio_dev->masklength) { 6928c2ecf20Sopenharmony_ci ret = sx9310_read_prox_data(data, &indio_dev->channels[bit], 6938c2ecf20Sopenharmony_ci &val); 6948c2ecf20Sopenharmony_ci if (ret) 6958c2ecf20Sopenharmony_ci goto out; 6968c2ecf20Sopenharmony_ci 6978c2ecf20Sopenharmony_ci data->buffer.channels[i++] = val; 6988c2ecf20Sopenharmony_ci } 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, 7018c2ecf20Sopenharmony_ci pf->timestamp); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ciout: 7048c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci return IRQ_HANDLED; 7098c2ecf20Sopenharmony_ci} 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_cistatic int sx9310_buffer_preenable(struct iio_dev *indio_dev) 7128c2ecf20Sopenharmony_ci{ 7138c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 7148c2ecf20Sopenharmony_ci unsigned long channels = 0; 7158c2ecf20Sopenharmony_ci int bit, ret; 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 7188c2ecf20Sopenharmony_ci for_each_set_bit(bit, indio_dev->active_scan_mask, 7198c2ecf20Sopenharmony_ci indio_dev->masklength) 7208c2ecf20Sopenharmony_ci __set_bit(indio_dev->channels[bit].channel, &channels); 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_ci ret = sx9310_update_chan_en(data, channels, data->chan_event); 7238c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 7248c2ecf20Sopenharmony_ci return ret; 7258c2ecf20Sopenharmony_ci} 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_cistatic int sx9310_buffer_postdisable(struct iio_dev *indio_dev) 7288c2ecf20Sopenharmony_ci{ 7298c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 7308c2ecf20Sopenharmony_ci int ret; 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 7338c2ecf20Sopenharmony_ci ret = sx9310_update_chan_en(data, 0, data->chan_event); 7348c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 7358c2ecf20Sopenharmony_ci return ret; 7368c2ecf20Sopenharmony_ci} 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_cistatic const struct iio_buffer_setup_ops sx9310_buffer_setup_ops = { 7398c2ecf20Sopenharmony_ci .preenable = sx9310_buffer_preenable, 7408c2ecf20Sopenharmony_ci .postdisable = sx9310_buffer_postdisable, 7418c2ecf20Sopenharmony_ci}; 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_cistruct sx9310_reg_default { 7448c2ecf20Sopenharmony_ci u8 reg; 7458c2ecf20Sopenharmony_ci u8 def; 7468c2ecf20Sopenharmony_ci}; 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_cistatic const struct sx9310_reg_default sx9310_default_regs[] = { 7498c2ecf20Sopenharmony_ci { SX9310_REG_IRQ_MSK, 0x00 }, 7508c2ecf20Sopenharmony_ci { SX9310_REG_IRQ_FUNC, 0x00 }, 7518c2ecf20Sopenharmony_ci /* 7528c2ecf20Sopenharmony_ci * The lower 4 bits should not be set as it enable sensors measurements. 7538c2ecf20Sopenharmony_ci * Turning the detection on before the configuration values are set to 7548c2ecf20Sopenharmony_ci * good values can cause the device to return erroneous readings. 7558c2ecf20Sopenharmony_ci */ 7568c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS }, 7578c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL1, 0x00 }, 7588c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 | 7598c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC }, 7608c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 | 7618c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL3_GAIN12_X4 }, 7628c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST }, 7638c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL | 7648c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 | 7658c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL5_RAWFILT_1P25 }, 7668c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT }, 7678c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 | 7688c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 }, 7698c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 | 7708c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 }, 7718c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 | 7728c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 }, 7738c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT | 7748c2ecf20Sopenharmony_ci SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 }, 7758c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL11, 0x00 }, 7768c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL12, 0x00 }, 7778c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL13, 0x00 }, 7788c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL14, 0x00 }, 7798c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL15, 0x00 }, 7808c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL16, 0x00 }, 7818c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL17, 0x00 }, 7828c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL18, 0x00 }, 7838c2ecf20Sopenharmony_ci { SX9310_REG_PROX_CTRL19, 0x00 }, 7848c2ecf20Sopenharmony_ci { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES | 7858c2ecf20Sopenharmony_ci SX9310_REG_SAR_CTRL0_SARHYST_8 }, 7868c2ecf20Sopenharmony_ci { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) }, 7878c2ecf20Sopenharmony_ci { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT }, 7888c2ecf20Sopenharmony_ci}; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci/* Activate all channels and perform an initial compensation. */ 7918c2ecf20Sopenharmony_cistatic int sx9310_init_compensation(struct iio_dev *indio_dev) 7928c2ecf20Sopenharmony_ci{ 7938c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 7948c2ecf20Sopenharmony_ci int ret; 7958c2ecf20Sopenharmony_ci unsigned int val; 7968c2ecf20Sopenharmony_ci unsigned int ctrl0; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0); 7998c2ecf20Sopenharmony_ci if (ret) 8008c2ecf20Sopenharmony_ci return ret; 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci /* run the compensation phase on all channels */ 8038c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, 8048c2ecf20Sopenharmony_ci ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK); 8058c2ecf20Sopenharmony_ci if (ret) 8068c2ecf20Sopenharmony_ci return ret; 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val, 8098c2ecf20Sopenharmony_ci !(val & SX9310_REG_STAT1_COMPSTAT_MASK), 8108c2ecf20Sopenharmony_ci 20000, 2000000); 8118c2ecf20Sopenharmony_ci if (ret) { 8128c2ecf20Sopenharmony_ci if (ret == -ETIMEDOUT) 8138c2ecf20Sopenharmony_ci dev_err(&data->client->dev, 8148c2ecf20Sopenharmony_ci "initial compensation timed out: 0x%02x\n", 8158c2ecf20Sopenharmony_ci val); 8168c2ecf20Sopenharmony_ci return ret; 8178c2ecf20Sopenharmony_ci } 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0); 8208c2ecf20Sopenharmony_ci return ret; 8218c2ecf20Sopenharmony_ci} 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_cistatic int sx9310_init_device(struct iio_dev *indio_dev) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 8268c2ecf20Sopenharmony_ci const struct sx9310_reg_default *initval; 8278c2ecf20Sopenharmony_ci int ret; 8288c2ecf20Sopenharmony_ci unsigned int i, val; 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET); 8318c2ecf20Sopenharmony_ci if (ret) 8328c2ecf20Sopenharmony_ci return ret; 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci usleep_range(1000, 2000); /* power-up time is ~1ms. */ 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */ 8378c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val); 8388c2ecf20Sopenharmony_ci if (ret) 8398c2ecf20Sopenharmony_ci return ret; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci /* Program some sane defaults. */ 8428c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) { 8438c2ecf20Sopenharmony_ci initval = &sx9310_default_regs[i]; 8448c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, initval->reg, initval->def); 8458c2ecf20Sopenharmony_ci if (ret) 8468c2ecf20Sopenharmony_ci return ret; 8478c2ecf20Sopenharmony_ci } 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci return sx9310_init_compensation(indio_dev); 8508c2ecf20Sopenharmony_ci} 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_cistatic int sx9310_set_indio_dev_name(struct device *dev, 8538c2ecf20Sopenharmony_ci struct iio_dev *indio_dev, 8548c2ecf20Sopenharmony_ci unsigned int whoami) 8558c2ecf20Sopenharmony_ci{ 8568c2ecf20Sopenharmony_ci unsigned int long ddata; 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci ddata = (uintptr_t)device_get_match_data(dev); 8598c2ecf20Sopenharmony_ci if (ddata != whoami) { 8608c2ecf20Sopenharmony_ci dev_err(dev, "WHOAMI does not match device data: %u\n", whoami); 8618c2ecf20Sopenharmony_ci return -ENODEV; 8628c2ecf20Sopenharmony_ci } 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci switch (whoami) { 8658c2ecf20Sopenharmony_ci case SX9310_WHOAMI_VALUE: 8668c2ecf20Sopenharmony_ci indio_dev->name = "sx9310"; 8678c2ecf20Sopenharmony_ci break; 8688c2ecf20Sopenharmony_ci case SX9311_WHOAMI_VALUE: 8698c2ecf20Sopenharmony_ci indio_dev->name = "sx9311"; 8708c2ecf20Sopenharmony_ci break; 8718c2ecf20Sopenharmony_ci default: 8728c2ecf20Sopenharmony_ci dev_err(dev, "unexpected WHOAMI response: %u\n", whoami); 8738c2ecf20Sopenharmony_ci return -ENODEV; 8748c2ecf20Sopenharmony_ci } 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci return 0; 8778c2ecf20Sopenharmony_ci} 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_cistatic void sx9310_regulator_disable(void *_data) 8808c2ecf20Sopenharmony_ci{ 8818c2ecf20Sopenharmony_ci struct sx9310_data *data = _data; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies); 8848c2ecf20Sopenharmony_ci} 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_cistatic int sx9310_probe(struct i2c_client *client) 8878c2ecf20Sopenharmony_ci{ 8888c2ecf20Sopenharmony_ci int ret; 8898c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 8908c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 8918c2ecf20Sopenharmony_ci struct sx9310_data *data; 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 8948c2ecf20Sopenharmony_ci if (!indio_dev) 8958c2ecf20Sopenharmony_ci return -ENOMEM; 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 8988c2ecf20Sopenharmony_ci data->client = client; 8998c2ecf20Sopenharmony_ci data->supplies[0].supply = "vdd"; 9008c2ecf20Sopenharmony_ci data->supplies[1].supply = "svdd"; 9018c2ecf20Sopenharmony_ci mutex_init(&data->mutex); 9028c2ecf20Sopenharmony_ci init_completion(&data->completion); 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config); 9058c2ecf20Sopenharmony_ci if (IS_ERR(data->regmap)) 9068c2ecf20Sopenharmony_ci return PTR_ERR(data->regmap); 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies), 9098c2ecf20Sopenharmony_ci data->supplies); 9108c2ecf20Sopenharmony_ci if (ret) 9118c2ecf20Sopenharmony_ci return ret; 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies); 9148c2ecf20Sopenharmony_ci if (ret) 9158c2ecf20Sopenharmony_ci return ret; 9168c2ecf20Sopenharmony_ci /* Must wait for Tpor time after initial power up */ 9178c2ecf20Sopenharmony_ci usleep_range(1000, 1100); 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci ret = devm_add_action_or_reset(dev, sx9310_regulator_disable, data); 9208c2ecf20Sopenharmony_ci if (ret) 9218c2ecf20Sopenharmony_ci return ret; 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami); 9248c2ecf20Sopenharmony_ci if (ret) { 9258c2ecf20Sopenharmony_ci dev_err(dev, "error in reading WHOAMI register: %d", ret); 9268c2ecf20Sopenharmony_ci return ret; 9278c2ecf20Sopenharmony_ci } 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami); 9308c2ecf20Sopenharmony_ci if (ret) 9318c2ecf20Sopenharmony_ci return ret; 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev)); 9348c2ecf20Sopenharmony_ci indio_dev->channels = sx9310_channels; 9358c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(sx9310_channels); 9368c2ecf20Sopenharmony_ci indio_dev->info = &sx9310_info; 9378c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 9388c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci ret = sx9310_init_device(indio_dev); 9418c2ecf20Sopenharmony_ci if (ret) 9428c2ecf20Sopenharmony_ci return ret; 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci if (client->irq) { 9458c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(dev, client->irq, 9468c2ecf20Sopenharmony_ci sx9310_irq_handler, 9478c2ecf20Sopenharmony_ci sx9310_irq_thread_handler, 9488c2ecf20Sopenharmony_ci IRQF_ONESHOT, 9498c2ecf20Sopenharmony_ci "sx9310_event", indio_dev); 9508c2ecf20Sopenharmony_ci if (ret) 9518c2ecf20Sopenharmony_ci return ret; 9528c2ecf20Sopenharmony_ci 9538c2ecf20Sopenharmony_ci data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", 9548c2ecf20Sopenharmony_ci indio_dev->name, 9558c2ecf20Sopenharmony_ci indio_dev->id); 9568c2ecf20Sopenharmony_ci if (!data->trig) 9578c2ecf20Sopenharmony_ci return -ENOMEM; 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ci data->trig->dev.parent = dev; 9608c2ecf20Sopenharmony_ci data->trig->ops = &sx9310_trigger_ops; 9618c2ecf20Sopenharmony_ci iio_trigger_set_drvdata(data->trig, indio_dev); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci ret = devm_iio_trigger_register(dev, data->trig); 9648c2ecf20Sopenharmony_ci if (ret) 9658c2ecf20Sopenharmony_ci return ret; 9668c2ecf20Sopenharmony_ci } 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 9698c2ecf20Sopenharmony_ci iio_pollfunc_store_time, 9708c2ecf20Sopenharmony_ci sx9310_trigger_handler, 9718c2ecf20Sopenharmony_ci &sx9310_buffer_setup_ops); 9728c2ecf20Sopenharmony_ci if (ret) 9738c2ecf20Sopenharmony_ci return ret; 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci return devm_iio_device_register(dev, indio_dev); 9768c2ecf20Sopenharmony_ci} 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_cistatic int __maybe_unused sx9310_suspend(struct device *dev) 9798c2ecf20Sopenharmony_ci{ 9808c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 9818c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 9828c2ecf20Sopenharmony_ci u8 ctrl0; 9838c2ecf20Sopenharmony_ci int ret; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci disable_irq_nosync(data->client->irq); 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 9888c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, 9898c2ecf20Sopenharmony_ci &data->suspend_ctrl0); 9908c2ecf20Sopenharmony_ci if (ret) 9918c2ecf20Sopenharmony_ci goto out; 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK; 9948c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0); 9958c2ecf20Sopenharmony_ci if (ret) 9968c2ecf20Sopenharmony_ci goto out; 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0); 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ciout: 10018c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 10028c2ecf20Sopenharmony_ci return ret; 10038c2ecf20Sopenharmony_ci} 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_cistatic int __maybe_unused sx9310_resume(struct device *dev) 10068c2ecf20Sopenharmony_ci{ 10078c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 10088c2ecf20Sopenharmony_ci struct sx9310_data *data = iio_priv(indio_dev); 10098c2ecf20Sopenharmony_ci int ret; 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci mutex_lock(&data->mutex); 10128c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1); 10138c2ecf20Sopenharmony_ci if (ret) 10148c2ecf20Sopenharmony_ci goto out; 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, 10178c2ecf20Sopenharmony_ci data->suspend_ctrl0); 10188c2ecf20Sopenharmony_ci 10198c2ecf20Sopenharmony_ciout: 10208c2ecf20Sopenharmony_ci mutex_unlock(&data->mutex); 10218c2ecf20Sopenharmony_ci if (ret) 10228c2ecf20Sopenharmony_ci return ret; 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci enable_irq(data->client->irq); 10258c2ecf20Sopenharmony_ci return 0; 10268c2ecf20Sopenharmony_ci} 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_cistatic const struct dev_pm_ops sx9310_pm_ops = { 10298c2ecf20Sopenharmony_ci SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume) 10308c2ecf20Sopenharmony_ci}; 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_cistatic const struct acpi_device_id sx9310_acpi_match[] = { 10338c2ecf20Sopenharmony_ci { "STH9310", SX9310_WHOAMI_VALUE }, 10348c2ecf20Sopenharmony_ci { "STH9311", SX9311_WHOAMI_VALUE }, 10358c2ecf20Sopenharmony_ci {} 10368c2ecf20Sopenharmony_ci}; 10378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, sx9310_acpi_match); 10388c2ecf20Sopenharmony_ci 10398c2ecf20Sopenharmony_cistatic const struct of_device_id sx9310_of_match[] = { 10408c2ecf20Sopenharmony_ci { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE }, 10418c2ecf20Sopenharmony_ci { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE }, 10428c2ecf20Sopenharmony_ci {} 10438c2ecf20Sopenharmony_ci}; 10448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sx9310_of_match); 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_cistatic const struct i2c_device_id sx9310_id[] = { 10478c2ecf20Sopenharmony_ci { "sx9310", SX9310_WHOAMI_VALUE }, 10488c2ecf20Sopenharmony_ci { "sx9311", SX9311_WHOAMI_VALUE }, 10498c2ecf20Sopenharmony_ci {} 10508c2ecf20Sopenharmony_ci}; 10518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sx9310_id); 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_cistatic struct i2c_driver sx9310_driver = { 10548c2ecf20Sopenharmony_ci .driver = { 10558c2ecf20Sopenharmony_ci .name = "sx9310", 10568c2ecf20Sopenharmony_ci .acpi_match_table = sx9310_acpi_match, 10578c2ecf20Sopenharmony_ci .of_match_table = sx9310_of_match, 10588c2ecf20Sopenharmony_ci .pm = &sx9310_pm_ops, 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci /* 10618c2ecf20Sopenharmony_ci * Lots of i2c transfers in probe + over 200 ms waiting in 10628c2ecf20Sopenharmony_ci * sx9310_init_compensation() mean a slow probe; prefer async 10638c2ecf20Sopenharmony_ci * so we don't delay boot if we're builtin to the kernel. 10648c2ecf20Sopenharmony_ci */ 10658c2ecf20Sopenharmony_ci .probe_type = PROBE_PREFER_ASYNCHRONOUS, 10668c2ecf20Sopenharmony_ci }, 10678c2ecf20Sopenharmony_ci .probe_new = sx9310_probe, 10688c2ecf20Sopenharmony_ci .id_table = sx9310_id, 10698c2ecf20Sopenharmony_ci}; 10708c2ecf20Sopenharmony_cimodule_i2c_driver(sx9310_driver); 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ciMODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>"); 10738c2ecf20Sopenharmony_ciMODULE_AUTHOR("Daniel Campello <campello@chromium.org>"); 10748c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor"); 10758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1076