Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2018 Google LLC. |
| 4 | * |
| 5 | * Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution. |
| 6 | * Based on SX9500 driver and Semtech driver using the input framework |
| 7 | * <https://my.syncplicity.com/share/teouwsim8niiaud/ |
| 8 | * linux-driver-SX9310_NoSmartHSensing>. |
| 9 | * Reworked April 2019 by Evan Green <evgreen@chromium.org> |
| 10 | * and January 2020 by Daniel Campello <campello@chromium.org> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/acpi.h> |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 14 | #include <linux/bitfield.h> |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/kernel.h> |
Daniel Campello | ef5bdba | 2020-08-03 17:58:04 -0600 | [diff] [blame] | 19 | #include <linux/mod_devicetable.h> |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 20 | #include <linux/module.h> |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 21 | #include <linux/pm.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/slab.h> |
| 24 | |
| 25 | #include <linux/iio/buffer.h> |
| 26 | #include <linux/iio/events.h> |
| 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/sysfs.h> |
| 29 | #include <linux/iio/trigger.h> |
| 30 | #include <linux/iio/triggered_buffer.h> |
| 31 | #include <linux/iio/trigger_consumer.h> |
| 32 | |
| 33 | /* Register definitions. */ |
| 34 | #define SX9310_REG_IRQ_SRC 0x00 |
| 35 | #define SX9310_REG_STAT0 0x01 |
| 36 | #define SX9310_REG_STAT1 0x02 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 37 | #define SX9310_REG_STAT1_COMPSTAT_MASK GENMASK(3, 0) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 38 | #define SX9310_REG_IRQ_MSK 0x03 |
| 39 | #define SX9310_CONVDONE_IRQ BIT(3) |
| 40 | #define SX9310_FAR_IRQ BIT(5) |
| 41 | #define SX9310_CLOSE_IRQ BIT(6) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 42 | #define SX9310_REG_IRQ_FUNC 0x04 |
| 43 | |
| 44 | #define SX9310_REG_PROX_CTRL0 0x10 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 45 | #define SX9310_REG_PROX_CTRL0_SENSOREN_MASK GENMASK(3, 0) |
| 46 | #define SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK GENMASK(7, 4) |
| 47 | #define SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS 0x01 |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 48 | #define SX9310_REG_PROX_CTRL1 0x11 |
| 49 | #define SX9310_REG_PROX_CTRL2 0x12 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 50 | #define SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 (0x02 << 6) |
| 51 | #define SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC (0x01 << 2) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 52 | #define SX9310_REG_PROX_CTRL3 0x13 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 53 | #define SX9310_REG_PROX_CTRL3_GAIN0_X8 (0x03 << 2) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 54 | #define SX9310_REG_PROX_CTRL3_GAIN12_X4 0x02 |
| 55 | #define SX9310_REG_PROX_CTRL4 0x14 |
| 56 | #define SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST 0x07 |
| 57 | #define SX9310_REG_PROX_CTRL5 0x15 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 58 | #define SX9310_REG_PROX_CTRL5_RANGE_SMALL (0x03 << 6) |
| 59 | #define SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 (0x01 << 2) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 60 | #define SX9310_REG_PROX_CTRL5_RAWFILT_1P25 0x02 |
| 61 | #define SX9310_REG_PROX_CTRL6 0x16 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 62 | #define SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT 0x20 |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 63 | #define SX9310_REG_PROX_CTRL7 0x17 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 64 | #define SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 (0x01 << 3) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 65 | #define SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 0x05 |
| 66 | #define SX9310_REG_PROX_CTRL8 0x18 |
| 67 | #define SX9310_REG_PROX_CTRL9 0x19 |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 68 | #define SX9310_REG_PROX_CTRL8_9_PTHRESH_28 (0x08 << 3) |
| 69 | #define SX9310_REG_PROX_CTRL8_9_PTHRESH_96 (0x11 << 3) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 70 | #define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 0x03 |
| 71 | #define SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 0x05 |
| 72 | #define SX9310_REG_PROX_CTRL10 0x1a |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 73 | #define SX9310_REG_PROX_CTRL10_HYST_6PCT (0x01 << 4) |
| 74 | #define SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 0x01 |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 75 | #define SX9310_REG_PROX_CTRL11 0x1b |
| 76 | #define SX9310_REG_PROX_CTRL12 0x1c |
| 77 | #define SX9310_REG_PROX_CTRL13 0x1d |
| 78 | #define SX9310_REG_PROX_CTRL14 0x1e |
| 79 | #define SX9310_REG_PROX_CTRL15 0x1f |
| 80 | #define SX9310_REG_PROX_CTRL16 0x20 |
| 81 | #define SX9310_REG_PROX_CTRL17 0x21 |
| 82 | #define SX9310_REG_PROX_CTRL18 0x22 |
| 83 | #define SX9310_REG_PROX_CTRL19 0x23 |
| 84 | #define SX9310_REG_SAR_CTRL0 0x2a |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 85 | #define SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES (0x02 << 5) |
| 86 | #define SX9310_REG_SAR_CTRL0_SARHYST_8 (0x02 << 3) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 87 | #define SX9310_REG_SAR_CTRL1 0x2b |
| 88 | /* Each increment of the slope register is 0.0078125. */ |
| 89 | #define SX9310_REG_SAR_CTRL1_SLOPE(_hnslope) (_hnslope / 78125) |
| 90 | #define SX9310_REG_SAR_CTRL2 0x2c |
| 91 | #define SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT 0x3c |
| 92 | |
| 93 | #define SX9310_REG_SENSOR_SEL 0x30 |
| 94 | |
| 95 | #define SX9310_REG_USE_MSB 0x31 |
| 96 | #define SX9310_REG_USE_LSB 0x32 |
| 97 | |
| 98 | #define SX9310_REG_AVG_MSB 0x33 |
| 99 | #define SX9310_REG_AVG_LSB 0x34 |
| 100 | |
| 101 | #define SX9310_REG_DIFF_MSB 0x35 |
| 102 | #define SX9310_REG_DIFF_LSB 0x36 |
| 103 | |
| 104 | #define SX9310_REG_OFFSET_MSB 0x37 |
| 105 | #define SX9310_REG_OFFSET_LSB 0x38 |
| 106 | |
| 107 | #define SX9310_REG_SAR_MSB 0x39 |
| 108 | #define SX9310_REG_SAR_LSB 0x3a |
| 109 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 110 | #define SX9310_REG_I2C_ADDR 0x40 |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 111 | #define SX9310_REG_PAUSE 0x41 |
| 112 | #define SX9310_REG_WHOAMI 0x42 |
| 113 | #define SX9310_WHOAMI_VALUE 0x01 |
| 114 | #define SX9311_WHOAMI_VALUE 0x02 |
| 115 | |
| 116 | #define SX9310_REG_RESET 0x7f |
| 117 | #define SX9310_SOFT_RESET 0xde |
| 118 | |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 119 | |
| 120 | /* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */ |
| 121 | #define SX9310_NUM_CHANNELS 4 |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 122 | static_assert(SX9310_NUM_CHANNELS < BITS_PER_LONG); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 123 | |
| 124 | struct sx9310_data { |
| 125 | /* Serialize access to registers and channel configuration */ |
| 126 | struct mutex mutex; |
| 127 | struct i2c_client *client; |
| 128 | struct iio_trigger *trig; |
| 129 | struct regmap *regmap; |
| 130 | /* |
| 131 | * Last reading of the proximity status for each channel. |
| 132 | * We only send an event to user space when this changes. |
| 133 | */ |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 134 | unsigned long chan_prox_stat; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 135 | bool trigger_enabled; |
Daniel Campello | 01b9cb0 | 2020-08-03 17:58:06 -0600 | [diff] [blame] | 136 | /* Ensure correct alignment of timestamp when present. */ |
| 137 | struct { |
| 138 | __be16 channels[SX9310_NUM_CHANNELS]; |
| 139 | s64 ts __aligned(8); |
| 140 | } buffer; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 141 | /* Remember enabled channels and sample rate during suspend. */ |
| 142 | unsigned int suspend_ctrl0; |
| 143 | struct completion completion; |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 144 | unsigned long chan_read; |
| 145 | unsigned long chan_event; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 146 | int channel_users[SX9310_NUM_CHANNELS]; |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 147 | unsigned int whoami; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static const struct iio_event_spec sx9310_events[] = { |
| 151 | { |
| 152 | .type = IIO_EV_TYPE_THRESH, |
| 153 | .dir = IIO_EV_DIR_EITHER, |
| 154 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | #define SX9310_NAMED_CHANNEL(idx, name) \ |
| 159 | { \ |
| 160 | .type = IIO_PROXIMITY, \ |
| 161 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 162 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 163 | .indexed = 1, \ |
| 164 | .channel = idx, \ |
| 165 | .extend_name = name, \ |
| 166 | .address = SX9310_REG_DIFF_MSB, \ |
| 167 | .event_spec = sx9310_events, \ |
| 168 | .num_event_specs = ARRAY_SIZE(sx9310_events), \ |
| 169 | .scan_index = idx, \ |
| 170 | .scan_type = { \ |
| 171 | .sign = 's', \ |
| 172 | .realbits = 12, \ |
| 173 | .storagebits = 16, \ |
| 174 | .endianness = IIO_BE, \ |
| 175 | }, \ |
| 176 | } |
| 177 | #define SX9310_CHANNEL(idx) SX9310_NAMED_CHANNEL(idx, NULL) |
| 178 | |
| 179 | static const struct iio_chan_spec sx9310_channels[] = { |
| 180 | SX9310_CHANNEL(0), /* CS0 */ |
| 181 | SX9310_CHANNEL(1), /* CS1 */ |
| 182 | SX9310_CHANNEL(2), /* CS2 */ |
| 183 | SX9310_NAMED_CHANNEL(3, "comb"), /* COMB */ |
| 184 | |
| 185 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 186 | }; |
| 187 | |
| 188 | /* |
| 189 | * Each entry contains the integer part (val) and the fractional part, in micro |
| 190 | * seconds. It conforms to the IIO output IIO_VAL_INT_PLUS_MICRO. |
| 191 | */ |
| 192 | static const struct { |
| 193 | int val; |
| 194 | int val2; |
| 195 | } sx9310_samp_freq_table[] = { |
| 196 | { 500, 0 }, /* 0000: Min (no idle time) */ |
| 197 | { 66, 666666 }, /* 0001: 15 ms */ |
| 198 | { 33, 333333 }, /* 0010: 30 ms (Typ.) */ |
| 199 | { 22, 222222 }, /* 0011: 45 ms */ |
| 200 | { 16, 666666 }, /* 0100: 60 ms */ |
| 201 | { 11, 111111 }, /* 0101: 90 ms */ |
| 202 | { 8, 333333 }, /* 0110: 120 ms */ |
| 203 | { 5, 0 }, /* 0111: 200 ms */ |
| 204 | { 2, 500000 }, /* 1000: 400 ms */ |
| 205 | { 1, 666666 }, /* 1001: 600 ms */ |
| 206 | { 1, 250000 }, /* 1010: 800 ms */ |
| 207 | { 1, 0 }, /* 1011: 1 s */ |
| 208 | { 0, 500000 }, /* 1100: 2 s */ |
| 209 | { 0, 333333 }, /* 1101: 3 s */ |
| 210 | { 0, 250000 }, /* 1110: 4 s */ |
| 211 | { 0, 200000 }, /* 1111: 5 s */ |
| 212 | }; |
| 213 | static const unsigned int sx9310_scan_period_table[] = { |
| 214 | 2, 15, 30, 45, 60, 90, 120, 200, |
| 215 | 400, 600, 800, 1000, 2000, 3000, 4000, 5000, |
| 216 | }; |
| 217 | |
| 218 | static ssize_t sx9310_show_samp_freq_avail(struct device *dev, |
| 219 | struct device_attribute *attr, |
| 220 | char *buf) |
| 221 | { |
| 222 | size_t len = 0; |
| 223 | int i; |
| 224 | |
| 225 | for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++) |
| 226 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%d ", |
| 227 | sx9310_samp_freq_table[i].val, |
| 228 | sx9310_samp_freq_table[i].val2); |
| 229 | buf[len - 1] = '\n'; |
| 230 | return len; |
| 231 | } |
| 232 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sx9310_show_samp_freq_avail); |
| 233 | |
| 234 | static const struct regmap_range sx9310_writable_reg_ranges[] = { |
| 235 | regmap_reg_range(SX9310_REG_IRQ_MSK, SX9310_REG_IRQ_FUNC), |
| 236 | regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19), |
| 237 | regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2), |
| 238 | regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SENSOR_SEL), |
| 239 | regmap_reg_range(SX9310_REG_OFFSET_MSB, SX9310_REG_OFFSET_LSB), |
| 240 | regmap_reg_range(SX9310_REG_PAUSE, SX9310_REG_PAUSE), |
| 241 | regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), |
| 242 | }; |
| 243 | |
| 244 | static const struct regmap_access_table sx9310_writeable_regs = { |
| 245 | .yes_ranges = sx9310_writable_reg_ranges, |
| 246 | .n_yes_ranges = ARRAY_SIZE(sx9310_writable_reg_ranges), |
| 247 | }; |
| 248 | |
| 249 | static const struct regmap_range sx9310_readable_reg_ranges[] = { |
| 250 | regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_IRQ_FUNC), |
| 251 | regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19), |
| 252 | regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2), |
| 253 | regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB), |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 254 | regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI), |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 255 | regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), |
| 256 | }; |
| 257 | |
| 258 | static const struct regmap_access_table sx9310_readable_regs = { |
| 259 | .yes_ranges = sx9310_readable_reg_ranges, |
| 260 | .n_yes_ranges = ARRAY_SIZE(sx9310_readable_reg_ranges), |
| 261 | }; |
| 262 | |
| 263 | static const struct regmap_range sx9310_volatile_reg_ranges[] = { |
| 264 | regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_STAT1), |
| 265 | regmap_reg_range(SX9310_REG_USE_MSB, SX9310_REG_DIFF_LSB), |
| 266 | regmap_reg_range(SX9310_REG_SAR_MSB, SX9310_REG_SAR_LSB), |
| 267 | regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET), |
| 268 | }; |
| 269 | |
| 270 | static const struct regmap_access_table sx9310_volatile_regs = { |
| 271 | .yes_ranges = sx9310_volatile_reg_ranges, |
| 272 | .n_yes_ranges = ARRAY_SIZE(sx9310_volatile_reg_ranges), |
| 273 | }; |
| 274 | |
| 275 | static const struct regmap_config sx9310_regmap_config = { |
| 276 | .reg_bits = 8, |
| 277 | .val_bits = 8, |
| 278 | |
| 279 | .max_register = SX9310_REG_RESET, |
| 280 | .cache_type = REGCACHE_RBTREE, |
| 281 | |
| 282 | .wr_table = &sx9310_writeable_regs, |
| 283 | .rd_table = &sx9310_readable_regs, |
| 284 | .volatile_table = &sx9310_volatile_regs, |
| 285 | }; |
| 286 | |
| 287 | static int sx9310_update_chan_en(struct sx9310_data *data, |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 288 | unsigned long chan_read, |
| 289 | unsigned long chan_event) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 290 | { |
| 291 | int ret; |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 292 | unsigned long channels = chan_read | chan_event; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 293 | |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 294 | if ((data->chan_read | data->chan_event) != channels) { |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 295 | ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0, |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 296 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK, |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 297 | channels); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 298 | if (ret) |
| 299 | return ret; |
| 300 | } |
| 301 | data->chan_read = chan_read; |
| 302 | data->chan_event = chan_event; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int sx9310_get_read_channel(struct sx9310_data *data, int channel) |
| 307 | { |
| 308 | return sx9310_update_chan_en(data, data->chan_read | BIT(channel), |
| 309 | data->chan_event); |
| 310 | } |
| 311 | |
| 312 | static int sx9310_put_read_channel(struct sx9310_data *data, int channel) |
| 313 | { |
| 314 | return sx9310_update_chan_en(data, data->chan_read & ~BIT(channel), |
| 315 | data->chan_event); |
| 316 | } |
| 317 | |
| 318 | static int sx9310_get_event_channel(struct sx9310_data *data, int channel) |
| 319 | { |
| 320 | return sx9310_update_chan_en(data, data->chan_read, |
| 321 | data->chan_event | BIT(channel)); |
| 322 | } |
| 323 | |
| 324 | static int sx9310_put_event_channel(struct sx9310_data *data, int channel) |
| 325 | { |
| 326 | return sx9310_update_chan_en(data, data->chan_read, |
| 327 | data->chan_event & ~BIT(channel)); |
| 328 | } |
| 329 | |
| 330 | static int sx9310_enable_irq(struct sx9310_data *data, unsigned int irq) |
| 331 | { |
Daniel Campello | 364e853 | 2020-08-03 17:58:03 -0600 | [diff] [blame] | 332 | if (!data->client->irq) |
| 333 | return 0; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 334 | return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, irq); |
| 335 | } |
| 336 | |
| 337 | static int sx9310_disable_irq(struct sx9310_data *data, unsigned int irq) |
| 338 | { |
Daniel Campello | 364e853 | 2020-08-03 17:58:03 -0600 | [diff] [blame] | 339 | if (!data->client->irq) |
| 340 | return 0; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 341 | return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, 0); |
| 342 | } |
| 343 | |
| 344 | static int sx9310_read_prox_data(struct sx9310_data *data, |
| 345 | const struct iio_chan_spec *chan, __be16 *val) |
| 346 | { |
| 347 | int ret; |
| 348 | |
| 349 | ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel); |
| 350 | if (ret < 0) |
| 351 | return ret; |
| 352 | |
Daniel Campello | 01b9cb0 | 2020-08-03 17:58:06 -0600 | [diff] [blame] | 353 | return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val)); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * If we have no interrupt support, we have to wait for a scan period |
| 358 | * after enabling a channel to get a result. |
| 359 | */ |
| 360 | static int sx9310_wait_for_sample(struct sx9310_data *data) |
| 361 | { |
| 362 | int ret; |
| 363 | unsigned int val; |
| 364 | |
| 365 | ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val); |
| 366 | if (ret < 0) |
| 367 | return ret; |
| 368 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 369 | val = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, val); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 370 | |
| 371 | msleep(sx9310_scan_period_table[val]); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int sx9310_read_proximity(struct sx9310_data *data, |
| 377 | const struct iio_chan_spec *chan, int *val) |
| 378 | { |
| 379 | int ret = 0; |
| 380 | __be16 rawval; |
| 381 | |
| 382 | mutex_lock(&data->mutex); |
| 383 | |
| 384 | ret = sx9310_get_read_channel(data, chan->channel); |
| 385 | if (ret < 0) |
| 386 | goto out; |
| 387 | |
| 388 | ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ); |
| 389 | if (ret < 0) |
| 390 | goto out_put_channel; |
| 391 | |
| 392 | mutex_unlock(&data->mutex); |
| 393 | |
Daniel Campello | 364e853 | 2020-08-03 17:58:03 -0600 | [diff] [blame] | 394 | if (data->client->irq) { |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 395 | ret = wait_for_completion_interruptible(&data->completion); |
| 396 | reinit_completion(&data->completion); |
| 397 | } else { |
| 398 | ret = sx9310_wait_for_sample(data); |
| 399 | } |
| 400 | |
| 401 | mutex_lock(&data->mutex); |
| 402 | |
| 403 | if (ret < 0) |
| 404 | goto out_disable_irq; |
| 405 | |
| 406 | ret = sx9310_read_prox_data(data, chan, &rawval); |
| 407 | if (ret < 0) |
| 408 | goto out_disable_irq; |
| 409 | |
| 410 | *val = sign_extend32(be16_to_cpu(rawval), |
| 411 | (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15)); |
| 412 | |
| 413 | ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); |
| 414 | if (ret < 0) |
| 415 | goto out_put_channel; |
| 416 | |
| 417 | ret = sx9310_put_read_channel(data, chan->channel); |
| 418 | if (ret < 0) |
| 419 | goto out; |
| 420 | |
| 421 | mutex_unlock(&data->mutex); |
| 422 | |
| 423 | return IIO_VAL_INT; |
| 424 | |
| 425 | out_disable_irq: |
| 426 | sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); |
| 427 | out_put_channel: |
| 428 | sx9310_put_read_channel(data, chan->channel); |
| 429 | out: |
| 430 | mutex_unlock(&data->mutex); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2) |
| 436 | { |
| 437 | unsigned int regval; |
| 438 | int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, ®val); |
| 439 | |
| 440 | if (ret < 0) |
| 441 | return ret; |
| 442 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 443 | regval = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, regval); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 444 | *val = sx9310_samp_freq_table[regval].val; |
| 445 | *val2 = sx9310_samp_freq_table[regval].val2; |
| 446 | |
| 447 | return IIO_VAL_INT_PLUS_MICRO; |
| 448 | } |
| 449 | |
| 450 | static int sx9310_read_raw(struct iio_dev *indio_dev, |
| 451 | const struct iio_chan_spec *chan, int *val, |
| 452 | int *val2, long mask) |
| 453 | { |
| 454 | struct sx9310_data *data = iio_priv(indio_dev); |
| 455 | int ret; |
| 456 | |
| 457 | if (chan->type != IIO_PROXIMITY) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | switch (mask) { |
| 461 | case IIO_CHAN_INFO_RAW: |
| 462 | ret = iio_device_claim_direct_mode(indio_dev); |
| 463 | if (ret) |
| 464 | return ret; |
| 465 | |
| 466 | ret = sx9310_read_proximity(data, chan, val); |
| 467 | iio_device_release_direct_mode(indio_dev); |
| 468 | return ret; |
| 469 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 470 | return sx9310_read_samp_freq(data, val, val2); |
| 471 | default: |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2) |
| 477 | { |
| 478 | int i, ret; |
| 479 | |
| 480 | for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++) |
| 481 | if (val == sx9310_samp_freq_table[i].val && |
| 482 | val2 == sx9310_samp_freq_table[i].val2) |
| 483 | break; |
| 484 | |
| 485 | if (i == ARRAY_SIZE(sx9310_samp_freq_table)) |
| 486 | return -EINVAL; |
| 487 | |
| 488 | mutex_lock(&data->mutex); |
| 489 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 490 | ret = regmap_update_bits( |
| 491 | data->regmap, SX9310_REG_PROX_CTRL0, |
| 492 | SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, |
| 493 | FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i)); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 494 | |
| 495 | mutex_unlock(&data->mutex); |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int sx9310_write_raw(struct iio_dev *indio_dev, |
| 501 | const struct iio_chan_spec *chan, int val, int val2, |
| 502 | long mask) |
| 503 | { |
| 504 | struct sx9310_data *data = iio_priv(indio_dev); |
| 505 | |
| 506 | if (chan->type != IIO_PROXIMITY) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | if (mask != IIO_CHAN_INFO_SAMP_FREQ) |
| 510 | return -EINVAL; |
| 511 | |
| 512 | return sx9310_set_samp_freq(data, val, val2); |
| 513 | } |
| 514 | |
| 515 | static irqreturn_t sx9310_irq_handler(int irq, void *private) |
| 516 | { |
| 517 | struct iio_dev *indio_dev = private; |
| 518 | struct sx9310_data *data = iio_priv(indio_dev); |
| 519 | |
| 520 | if (data->trigger_enabled) |
| 521 | iio_trigger_poll(data->trig); |
| 522 | |
| 523 | /* |
| 524 | * Even if no event is enabled, we need to wake the thread to |
| 525 | * clear the interrupt state by reading SX9310_REG_IRQ_SRC. It |
| 526 | * is not possible to do that here because regmap_read takes a |
| 527 | * mutex. |
| 528 | */ |
| 529 | return IRQ_WAKE_THREAD; |
| 530 | } |
| 531 | |
| 532 | static void sx9310_push_events(struct iio_dev *indio_dev) |
| 533 | { |
| 534 | int ret; |
| 535 | unsigned int val, chan; |
| 536 | struct sx9310_data *data = iio_priv(indio_dev); |
| 537 | s64 timestamp = iio_get_time_ns(indio_dev); |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 538 | unsigned long prox_changed; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 539 | |
| 540 | /* Read proximity state on all channels */ |
| 541 | ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val); |
| 542 | if (ret < 0) { |
| 543 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); |
| 544 | return; |
| 545 | } |
| 546 | |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 547 | /* |
| 548 | * Only iterate over channels with changes on proximity status that have |
| 549 | * events enabled. |
| 550 | */ |
| 551 | prox_changed = (data->chan_prox_stat ^ val) & data->chan_event; |
| 552 | |
| 553 | for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) { |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 554 | int dir; |
| 555 | u64 ev; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 556 | |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 557 | dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 558 | ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, |
| 559 | IIO_EV_TYPE_THRESH, dir); |
| 560 | |
| 561 | iio_push_event(indio_dev, ev, timestamp); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 562 | } |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 563 | data->chan_prox_stat = val; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static irqreturn_t sx9310_irq_thread_handler(int irq, void *private) |
| 567 | { |
| 568 | struct iio_dev *indio_dev = private; |
| 569 | struct sx9310_data *data = iio_priv(indio_dev); |
| 570 | int ret; |
| 571 | unsigned int val; |
| 572 | |
| 573 | mutex_lock(&data->mutex); |
| 574 | |
| 575 | ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val); |
| 576 | if (ret < 0) { |
| 577 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); |
| 578 | goto out; |
| 579 | } |
| 580 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 581 | if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ)) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 582 | sx9310_push_events(indio_dev); |
| 583 | |
| 584 | if (val & SX9310_CONVDONE_IRQ) |
| 585 | complete(&data->completion); |
| 586 | |
| 587 | out: |
| 588 | mutex_unlock(&data->mutex); |
| 589 | |
| 590 | return IRQ_HANDLED; |
| 591 | } |
| 592 | |
| 593 | static int sx9310_read_event_config(struct iio_dev *indio_dev, |
| 594 | const struct iio_chan_spec *chan, |
| 595 | enum iio_event_type type, |
| 596 | enum iio_event_direction dir) |
| 597 | { |
| 598 | struct sx9310_data *data = iio_priv(indio_dev); |
| 599 | |
| 600 | return !!(data->chan_event & BIT(chan->channel)); |
| 601 | } |
| 602 | |
| 603 | static int sx9310_write_event_config(struct iio_dev *indio_dev, |
| 604 | const struct iio_chan_spec *chan, |
| 605 | enum iio_event_type type, |
| 606 | enum iio_event_direction dir, int state) |
| 607 | { |
| 608 | struct sx9310_data *data = iio_priv(indio_dev); |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 609 | unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 610 | int ret; |
| 611 | |
| 612 | /* If the state hasn't changed, there's nothing to do. */ |
| 613 | if (!!(data->chan_event & BIT(chan->channel)) == state) |
| 614 | return 0; |
| 615 | |
| 616 | mutex_lock(&data->mutex); |
| 617 | if (state) { |
| 618 | ret = sx9310_get_event_channel(data, chan->channel); |
| 619 | if (ret < 0) |
| 620 | goto out_unlock; |
| 621 | if (!(data->chan_event & ~BIT(chan->channel))) { |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 622 | ret = sx9310_enable_irq(data, eventirq); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 623 | if (ret < 0) |
| 624 | sx9310_put_event_channel(data, chan->channel); |
| 625 | } |
| 626 | } else { |
| 627 | ret = sx9310_put_event_channel(data, chan->channel); |
| 628 | if (ret < 0) |
| 629 | goto out_unlock; |
| 630 | if (!data->chan_event) { |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 631 | ret = sx9310_disable_irq(data, eventirq); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 632 | if (ret < 0) |
| 633 | sx9310_get_event_channel(data, chan->channel); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | out_unlock: |
| 638 | mutex_unlock(&data->mutex); |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | static struct attribute *sx9310_attributes[] = { |
| 643 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
| 644 | NULL, |
| 645 | }; |
| 646 | |
| 647 | static const struct attribute_group sx9310_attribute_group = { |
| 648 | .attrs = sx9310_attributes, |
| 649 | }; |
| 650 | |
| 651 | static const struct iio_info sx9310_info = { |
| 652 | .attrs = &sx9310_attribute_group, |
| 653 | .read_raw = sx9310_read_raw, |
| 654 | .write_raw = sx9310_write_raw, |
| 655 | .read_event_config = sx9310_read_event_config, |
| 656 | .write_event_config = sx9310_write_event_config, |
| 657 | }; |
| 658 | |
| 659 | static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state) |
| 660 | { |
| 661 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 662 | struct sx9310_data *data = iio_priv(indio_dev); |
| 663 | int ret = 0; |
| 664 | |
| 665 | mutex_lock(&data->mutex); |
| 666 | |
| 667 | if (state) |
| 668 | ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ); |
| 669 | else if (!data->chan_read) |
| 670 | ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ); |
| 671 | if (ret < 0) |
| 672 | goto out; |
| 673 | |
| 674 | data->trigger_enabled = state; |
| 675 | |
| 676 | out: |
| 677 | mutex_unlock(&data->mutex); |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static const struct iio_trigger_ops sx9310_trigger_ops = { |
| 683 | .set_trigger_state = sx9310_set_trigger_state, |
| 684 | }; |
| 685 | |
| 686 | static irqreturn_t sx9310_trigger_handler(int irq, void *private) |
| 687 | { |
| 688 | struct iio_poll_func *pf = private; |
| 689 | struct iio_dev *indio_dev = pf->indio_dev; |
| 690 | struct sx9310_data *data = iio_priv(indio_dev); |
| 691 | __be16 val; |
| 692 | int bit, ret, i = 0; |
| 693 | |
| 694 | mutex_lock(&data->mutex); |
| 695 | |
| 696 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 697 | indio_dev->masklength) { |
| 698 | ret = sx9310_read_prox_data(data, &indio_dev->channels[bit], |
| 699 | &val); |
| 700 | if (ret < 0) |
| 701 | goto out; |
| 702 | |
Daniel Campello | 01b9cb0 | 2020-08-03 17:58:06 -0600 | [diff] [blame] | 703 | data->buffer.channels[i++] = val; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 704 | } |
| 705 | |
Daniel Campello | 01b9cb0 | 2020-08-03 17:58:06 -0600 | [diff] [blame] | 706 | iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 707 | pf->timestamp); |
| 708 | |
| 709 | out: |
| 710 | mutex_unlock(&data->mutex); |
| 711 | |
| 712 | iio_trigger_notify_done(indio_dev->trig); |
| 713 | |
| 714 | return IRQ_HANDLED; |
| 715 | } |
| 716 | |
| 717 | static int sx9310_buffer_preenable(struct iio_dev *indio_dev) |
| 718 | { |
| 719 | struct sx9310_data *data = iio_priv(indio_dev); |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 720 | unsigned long channels = 0; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 721 | int bit, ret; |
| 722 | |
| 723 | mutex_lock(&data->mutex); |
| 724 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 725 | indio_dev->masklength) |
Daniel Campello | 68aa360 | 2020-08-03 17:58:07 -0600 | [diff] [blame] | 726 | __set_bit(indio_dev->channels[bit].channel, &channels); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 727 | |
| 728 | ret = sx9310_update_chan_en(data, channels, data->chan_event); |
| 729 | mutex_unlock(&data->mutex); |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | static int sx9310_buffer_postdisable(struct iio_dev *indio_dev) |
| 734 | { |
| 735 | struct sx9310_data *data = iio_priv(indio_dev); |
| 736 | int ret; |
| 737 | |
| 738 | mutex_lock(&data->mutex); |
| 739 | ret = sx9310_update_chan_en(data, 0, data->chan_event); |
| 740 | mutex_unlock(&data->mutex); |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | static const struct iio_buffer_setup_ops sx9310_buffer_setup_ops = { |
| 745 | .preenable = sx9310_buffer_preenable, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 746 | .postdisable = sx9310_buffer_postdisable, |
| 747 | }; |
| 748 | |
| 749 | struct sx9310_reg_default { |
| 750 | u8 reg; |
| 751 | u8 def; |
| 752 | }; |
| 753 | |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 754 | static const struct sx9310_reg_default sx9310_default_regs[] = { |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 755 | { SX9310_REG_IRQ_MSK, 0x00 }, |
| 756 | { SX9310_REG_IRQ_FUNC, 0x00 }, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 757 | /* |
| 758 | * The lower 4 bits should not be set as it enable sensors measurements. |
| 759 | * Turning the detection on before the configuration values are set to |
| 760 | * good values can cause the device to return erroneous readings. |
| 761 | */ |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 762 | { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS }, |
| 763 | { SX9310_REG_PROX_CTRL1, 0x00 }, |
| 764 | { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 | |
| 765 | SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC }, |
| 766 | { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 | |
| 767 | SX9310_REG_PROX_CTRL3_GAIN12_X4 }, |
| 768 | { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST }, |
| 769 | { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL | |
| 770 | SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 | |
| 771 | SX9310_REG_PROX_CTRL5_RAWFILT_1P25 }, |
| 772 | { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT }, |
| 773 | { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 | |
| 774 | SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 }, |
| 775 | { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 | |
| 776 | SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 }, |
| 777 | { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 | |
| 778 | SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 }, |
| 779 | { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT | |
| 780 | SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 }, |
| 781 | { SX9310_REG_PROX_CTRL11, 0x00 }, |
| 782 | { SX9310_REG_PROX_CTRL12, 0x00 }, |
| 783 | { SX9310_REG_PROX_CTRL13, 0x00 }, |
| 784 | { SX9310_REG_PROX_CTRL14, 0x00 }, |
| 785 | { SX9310_REG_PROX_CTRL15, 0x00 }, |
| 786 | { SX9310_REG_PROX_CTRL16, 0x00 }, |
| 787 | { SX9310_REG_PROX_CTRL17, 0x00 }, |
| 788 | { SX9310_REG_PROX_CTRL18, 0x00 }, |
| 789 | { SX9310_REG_PROX_CTRL19, 0x00 }, |
| 790 | { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES | |
| 791 | SX9310_REG_SAR_CTRL0_SARHYST_8 }, |
| 792 | { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) }, |
| 793 | { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT }, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | /* Activate all channels and perform an initial compensation. */ |
| 797 | static int sx9310_init_compensation(struct iio_dev *indio_dev) |
| 798 | { |
| 799 | struct sx9310_data *data = iio_priv(indio_dev); |
Daniel Campello | dc46198 | 2020-08-03 17:58:08 -0600 | [diff] [blame^] | 800 | int ret; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 801 | unsigned int val; |
| 802 | unsigned int ctrl0; |
| 803 | |
| 804 | ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0); |
| 805 | if (ret < 0) |
| 806 | return ret; |
| 807 | |
| 808 | /* run the compensation phase on all channels */ |
| 809 | ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 810 | ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 811 | if (ret < 0) |
| 812 | return ret; |
| 813 | |
Daniel Campello | dc46198 | 2020-08-03 17:58:08 -0600 | [diff] [blame^] | 814 | ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val, |
| 815 | !(val & SX9310_REG_STAT1_COMPSTAT_MASK), |
| 816 | 20000, 2000000); |
| 817 | if (ret) { |
| 818 | if (ret == -ETIMEDOUT) |
| 819 | dev_err(&data->client->dev, |
| 820 | "initial compensation timed out: 0x%02x\n", |
| 821 | val); |
| 822 | return ret; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 823 | } |
| 824 | |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 825 | regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0); |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | static int sx9310_init_device(struct iio_dev *indio_dev) |
| 830 | { |
| 831 | struct sx9310_data *data = iio_priv(indio_dev); |
| 832 | const struct sx9310_reg_default *initval; |
| 833 | int ret; |
| 834 | unsigned int i, val; |
| 835 | |
| 836 | ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET); |
| 837 | if (ret < 0) |
| 838 | return ret; |
| 839 | |
| 840 | usleep_range(1000, 2000); /* power-up time is ~1ms. */ |
| 841 | |
| 842 | /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */ |
| 843 | ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val); |
| 844 | if (ret < 0) |
| 845 | return ret; |
| 846 | |
| 847 | /* Program some sane defaults. */ |
| 848 | for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) { |
| 849 | initval = &sx9310_default_regs[i]; |
| 850 | ret = regmap_write(data->regmap, initval->reg, initval->def); |
| 851 | if (ret < 0) |
| 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | return sx9310_init_compensation(indio_dev); |
| 856 | } |
| 857 | |
| 858 | static int sx9310_set_indio_dev_name(struct device *dev, |
| 859 | struct iio_dev *indio_dev, |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 860 | unsigned int whoami) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 861 | { |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 862 | unsigned int long ddata; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 863 | |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 864 | ddata = (uintptr_t)device_get_match_data(dev); |
| 865 | if (ddata != whoami) { |
| 866 | dev_err(dev, "WHOAMI does not match device data: %u\n", whoami); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 867 | return -ENODEV; |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 868 | } |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 869 | |
| 870 | switch (whoami) { |
| 871 | case SX9310_WHOAMI_VALUE: |
| 872 | indio_dev->name = "sx9310"; |
| 873 | break; |
| 874 | case SX9311_WHOAMI_VALUE: |
| 875 | indio_dev->name = "sx9311"; |
| 876 | break; |
| 877 | default: |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 878 | dev_err(dev, "unexpected WHOAMI response: %u\n", whoami); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 879 | return -ENODEV; |
| 880 | } |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 885 | static int sx9310_probe(struct i2c_client *client) |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 886 | { |
| 887 | int ret; |
| 888 | struct iio_dev *indio_dev; |
| 889 | struct sx9310_data *data; |
| 890 | |
| 891 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 892 | if (indio_dev == NULL) |
| 893 | return -ENOMEM; |
| 894 | |
| 895 | data = iio_priv(indio_dev); |
| 896 | data->client = client; |
| 897 | mutex_init(&data->mutex); |
| 898 | init_completion(&data->completion); |
| 899 | |
| 900 | data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config); |
| 901 | if (IS_ERR(data->regmap)) |
| 902 | return PTR_ERR(data->regmap); |
| 903 | |
| 904 | ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami); |
| 905 | if (ret < 0) { |
| 906 | dev_err(&client->dev, "error in reading WHOAMI register: %d", |
| 907 | ret); |
| 908 | return ret; |
| 909 | } |
| 910 | |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 911 | ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, data->whoami); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 912 | if (ret < 0) |
| 913 | return ret; |
| 914 | |
| 915 | ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev)); |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 916 | indio_dev->channels = sx9310_channels; |
| 917 | indio_dev->num_channels = ARRAY_SIZE(sx9310_channels); |
| 918 | indio_dev->info = &sx9310_info; |
| 919 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 920 | i2c_set_clientdata(client, indio_dev); |
| 921 | |
| 922 | ret = sx9310_init_device(indio_dev); |
| 923 | if (ret < 0) |
| 924 | return ret; |
| 925 | |
| 926 | if (client->irq) { |
| 927 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 928 | sx9310_irq_handler, |
| 929 | sx9310_irq_thread_handler, |
| 930 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 931 | "sx9310_event", indio_dev); |
| 932 | if (ret < 0) |
| 933 | return ret; |
| 934 | |
| 935 | data->trig = |
| 936 | devm_iio_trigger_alloc(&client->dev, "%s-dev%d", |
| 937 | indio_dev->name, indio_dev->id); |
| 938 | if (!data->trig) |
| 939 | return -ENOMEM; |
| 940 | |
| 941 | data->trig->dev.parent = &client->dev; |
| 942 | data->trig->ops = &sx9310_trigger_ops; |
| 943 | iio_trigger_set_drvdata(data->trig, indio_dev); |
| 944 | |
| 945 | ret = devm_iio_trigger_register(&client->dev, data->trig); |
| 946 | if (ret) |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, |
| 951 | iio_pollfunc_store_time, |
| 952 | sx9310_trigger_handler, |
| 953 | &sx9310_buffer_setup_ops); |
| 954 | if (ret < 0) |
| 955 | return ret; |
| 956 | |
| 957 | return devm_iio_device_register(&client->dev, indio_dev); |
| 958 | } |
| 959 | |
| 960 | static int __maybe_unused sx9310_suspend(struct device *dev) |
| 961 | { |
| 962 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 963 | struct sx9310_data *data = iio_priv(indio_dev); |
| 964 | u8 ctrl0; |
| 965 | int ret; |
| 966 | |
| 967 | disable_irq_nosync(data->client->irq); |
| 968 | |
| 969 | mutex_lock(&data->mutex); |
| 970 | ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, |
| 971 | &data->suspend_ctrl0); |
| 972 | |
| 973 | if (ret) |
| 974 | goto out; |
| 975 | |
Daniel Campello | d9f753f | 2020-08-03 17:58:02 -0600 | [diff] [blame] | 976 | ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 977 | ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0); |
| 978 | if (ret) |
| 979 | goto out; |
| 980 | |
| 981 | ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0); |
| 982 | |
| 983 | out: |
| 984 | mutex_unlock(&data->mutex); |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | static int __maybe_unused sx9310_resume(struct device *dev) |
| 989 | { |
| 990 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 991 | struct sx9310_data *data = iio_priv(indio_dev); |
| 992 | int ret; |
| 993 | |
| 994 | mutex_lock(&data->mutex); |
| 995 | ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1); |
| 996 | if (ret) |
| 997 | goto out; |
| 998 | |
| 999 | ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, |
| 1000 | data->suspend_ctrl0); |
| 1001 | |
| 1002 | out: |
| 1003 | mutex_unlock(&data->mutex); |
Daniel Campello | 364e853 | 2020-08-03 17:58:03 -0600 | [diff] [blame] | 1004 | if (ret) |
| 1005 | return ret; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1006 | |
| 1007 | enable_irq(data->client->irq); |
Daniel Campello | 364e853 | 2020-08-03 17:58:03 -0600 | [diff] [blame] | 1008 | return 0; |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static const struct dev_pm_ops sx9310_pm_ops = { |
| 1012 | SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume) |
| 1013 | }; |
| 1014 | |
| 1015 | static const struct acpi_device_id sx9310_acpi_match[] = { |
| 1016 | { "STH9310", SX9310_WHOAMI_VALUE }, |
| 1017 | { "STH9311", SX9311_WHOAMI_VALUE }, |
| 1018 | {}, |
| 1019 | }; |
| 1020 | MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match); |
| 1021 | |
| 1022 | static const struct of_device_id sx9310_of_match[] = { |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 1023 | { .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE }, |
| 1024 | { .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE }, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1025 | {}, |
| 1026 | }; |
| 1027 | MODULE_DEVICE_TABLE(of, sx9310_of_match); |
| 1028 | |
| 1029 | static const struct i2c_device_id sx9310_id[] = { |
| 1030 | { "sx9310", SX9310_WHOAMI_VALUE }, |
| 1031 | { "sx9311", SX9311_WHOAMI_VALUE }, |
| 1032 | {}, |
| 1033 | }; |
| 1034 | MODULE_DEVICE_TABLE(i2c, sx9310_id); |
| 1035 | |
| 1036 | static struct i2c_driver sx9310_driver = { |
| 1037 | .driver = { |
| 1038 | .name = "sx9310", |
Daniel Campello | ef5bdba | 2020-08-03 17:58:04 -0600 | [diff] [blame] | 1039 | .acpi_match_table = sx9310_acpi_match, |
| 1040 | .of_match_table = sx9310_of_match, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1041 | .pm = &sx9310_pm_ops, |
| 1042 | }, |
Daniel Campello | 9b2cac9 | 2020-08-03 17:58:05 -0600 | [diff] [blame] | 1043 | .probe_new = sx9310_probe, |
Daniel Campello | 72ad02b | 2020-03-10 14:06:59 -0600 | [diff] [blame] | 1044 | .id_table = sx9310_id, |
| 1045 | }; |
| 1046 | module_i2c_driver(sx9310_driver); |
| 1047 | |
| 1048 | MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>"); |
| 1049 | MODULE_AUTHOR("Daniel Campello <campello@chromium.org>"); |
| 1050 | MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor"); |
| 1051 | MODULE_LICENSE("GPL v2"); |