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