1 /* 2 * Copyright (c) 2014 Intel Corporation 3 * 4 * Driver for Semtech's SX9500 capacitive proximity/button solution. 5 * Datasheet available at 6 * <http://www.semtech.com/images/datasheet/sx9500.pdf>. 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 as published by 10 * the Free Software Foundation. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/slab.h> 15 #include <linux/module.h> 16 #include <linux/i2c.h> 17 #include <linux/irq.h> 18 #include <linux/acpi.h> 19 #include <linux/gpio/consumer.h> 20 #include <linux/regmap.h> 21 #include <linux/pm.h> 22 #include <linux/delay.h> 23 24 #include <linux/iio/iio.h> 25 #include <linux/iio/buffer.h> 26 #include <linux/iio/sysfs.h> 27 #include <linux/iio/events.h> 28 #include <linux/iio/trigger.h> 29 #include <linux/iio/triggered_buffer.h> 30 #include <linux/iio/trigger_consumer.h> 31 32 #define SX9500_DRIVER_NAME "sx9500" 33 #define SX9500_IRQ_NAME "sx9500_event" 34 35 #define SX9500_GPIO_INT "interrupt" 36 #define SX9500_GPIO_RESET "reset" 37 38 /* Register definitions. */ 39 #define SX9500_REG_IRQ_SRC 0x00 40 #define SX9500_REG_STAT 0x01 41 #define SX9500_REG_IRQ_MSK 0x03 42 43 #define SX9500_REG_PROX_CTRL0 0x06 44 #define SX9500_REG_PROX_CTRL1 0x07 45 #define SX9500_REG_PROX_CTRL2 0x08 46 #define SX9500_REG_PROX_CTRL3 0x09 47 #define SX9500_REG_PROX_CTRL4 0x0a 48 #define SX9500_REG_PROX_CTRL5 0x0b 49 #define SX9500_REG_PROX_CTRL6 0x0c 50 #define SX9500_REG_PROX_CTRL7 0x0d 51 #define SX9500_REG_PROX_CTRL8 0x0e 52 53 #define SX9500_REG_SENSOR_SEL 0x20 54 #define SX9500_REG_USE_MSB 0x21 55 #define SX9500_REG_USE_LSB 0x22 56 #define SX9500_REG_AVG_MSB 0x23 57 #define SX9500_REG_AVG_LSB 0x24 58 #define SX9500_REG_DIFF_MSB 0x25 59 #define SX9500_REG_DIFF_LSB 0x26 60 #define SX9500_REG_OFFSET_MSB 0x27 61 #define SX9500_REG_OFFSET_LSB 0x28 62 63 #define SX9500_REG_RESET 0x7f 64 65 /* Write this to REG_RESET to do a soft reset. */ 66 #define SX9500_SOFT_RESET 0xde 67 68 #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4) 69 #define SX9500_SCAN_PERIOD_SHIFT 4 70 71 /* 72 * These serve for identifying IRQ source in the IRQ_SRC register, and 73 * also for masking the IRQs in the IRQ_MSK register. 74 */ 75 #define SX9500_CLOSE_IRQ BIT(6) 76 #define SX9500_FAR_IRQ BIT(5) 77 #define SX9500_CONVDONE_IRQ BIT(3) 78 79 #define SX9500_PROXSTAT_SHIFT 4 80 #define SX9500_COMPSTAT_MASK GENMASK(3, 0) 81 82 #define SX9500_NUM_CHANNELS 4 83 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0) 84 85 struct sx9500_data { 86 struct mutex mutex; 87 struct i2c_client *client; 88 struct iio_trigger *trig; 89 struct regmap *regmap; 90 struct gpio_desc *gpiod_rst; 91 /* 92 * Last reading of the proximity status for each channel. We 93 * only send an event to user space when this changes. 94 */ 95 bool prox_stat[SX9500_NUM_CHANNELS]; 96 bool event_enabled[SX9500_NUM_CHANNELS]; 97 bool trigger_enabled; 98 u16 *buffer; 99 /* Remember enabled channels and sample rate during suspend. */ 100 unsigned int suspend_ctrl0; 101 struct completion completion; 102 int data_rdy_users, close_far_users; 103 int channel_users[SX9500_NUM_CHANNELS]; 104 }; 105 106 static const struct iio_event_spec sx9500_events[] = { 107 { 108 .type = IIO_EV_TYPE_THRESH, 109 .dir = IIO_EV_DIR_EITHER, 110 .mask_separate = BIT(IIO_EV_INFO_ENABLE), 111 }, 112 }; 113 114 #define SX9500_CHANNEL(idx) \ 115 { \ 116 .type = IIO_PROXIMITY, \ 117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 118 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 119 .indexed = 1, \ 120 .channel = idx, \ 121 .event_spec = sx9500_events, \ 122 .num_event_specs = ARRAY_SIZE(sx9500_events), \ 123 .scan_index = idx, \ 124 .scan_type = { \ 125 .sign = 'u', \ 126 .realbits = 16, \ 127 .storagebits = 16, \ 128 .shift = 0, \ 129 }, \ 130 } 131 132 static const struct iio_chan_spec sx9500_channels[] = { 133 SX9500_CHANNEL(0), 134 SX9500_CHANNEL(1), 135 SX9500_CHANNEL(2), 136 SX9500_CHANNEL(3), 137 IIO_CHAN_SOFT_TIMESTAMP(4), 138 }; 139 140 static const struct { 141 int val; 142 int val2; 143 } sx9500_samp_freq_table[] = { 144 {33, 333333}, 145 {16, 666666}, 146 {11, 111111}, 147 {8, 333333}, 148 {6, 666666}, 149 {5, 0}, 150 {3, 333333}, 151 {2, 500000}, 152 }; 153 154 static const unsigned int sx9500_scan_period_table[] = { 155 30, 60, 90, 120, 150, 200, 300, 400, 156 }; 157 158 static const struct regmap_range sx9500_writable_reg_ranges[] = { 159 regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK), 160 regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8), 161 regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL), 162 regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB), 163 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET), 164 }; 165 166 static const struct regmap_access_table sx9500_writeable_regs = { 167 .yes_ranges = sx9500_writable_reg_ranges, 168 .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges), 169 }; 170 171 /* 172 * All allocated registers are readable, so we just list unallocated 173 * ones. 174 */ 175 static const struct regmap_range sx9500_non_readable_reg_ranges[] = { 176 regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1), 177 regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1), 178 regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1), 179 regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1), 180 }; 181 182 static const struct regmap_access_table sx9500_readable_regs = { 183 .no_ranges = sx9500_non_readable_reg_ranges, 184 .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges), 185 }; 186 187 static const struct regmap_range sx9500_volatile_reg_ranges[] = { 188 regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT), 189 regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB), 190 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET), 191 }; 192 193 static const struct regmap_access_table sx9500_volatile_regs = { 194 .yes_ranges = sx9500_volatile_reg_ranges, 195 .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges), 196 }; 197 198 static const struct regmap_config sx9500_regmap_config = { 199 .reg_bits = 8, 200 .val_bits = 8, 201 202 .max_register = SX9500_REG_RESET, 203 .cache_type = REGCACHE_RBTREE, 204 205 .wr_table = &sx9500_writeable_regs, 206 .rd_table = &sx9500_readable_regs, 207 .volatile_table = &sx9500_volatile_regs, 208 }; 209 210 static int sx9500_inc_users(struct sx9500_data *data, int *counter, 211 unsigned int reg, unsigned int bitmask) 212 { 213 (*counter)++; 214 if (*counter != 1) 215 /* Bit is already active, nothing to do. */ 216 return 0; 217 218 return regmap_update_bits(data->regmap, reg, bitmask, bitmask); 219 } 220 221 static int sx9500_dec_users(struct sx9500_data *data, int *counter, 222 unsigned int reg, unsigned int bitmask) 223 { 224 (*counter)--; 225 if (*counter != 0) 226 /* There are more users, do not deactivate. */ 227 return 0; 228 229 return regmap_update_bits(data->regmap, reg, bitmask, 0); 230 } 231 232 static int sx9500_inc_chan_users(struct sx9500_data *data, int chan) 233 { 234 return sx9500_inc_users(data, &data->channel_users[chan], 235 SX9500_REG_PROX_CTRL0, BIT(chan)); 236 } 237 238 static int sx9500_dec_chan_users(struct sx9500_data *data, int chan) 239 { 240 return sx9500_dec_users(data, &data->channel_users[chan], 241 SX9500_REG_PROX_CTRL0, BIT(chan)); 242 } 243 244 static int sx9500_inc_data_rdy_users(struct sx9500_data *data) 245 { 246 return sx9500_inc_users(data, &data->data_rdy_users, 247 SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ); 248 } 249 250 static int sx9500_dec_data_rdy_users(struct sx9500_data *data) 251 { 252 return sx9500_dec_users(data, &data->data_rdy_users, 253 SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ); 254 } 255 256 static int sx9500_inc_close_far_users(struct sx9500_data *data) 257 { 258 return sx9500_inc_users(data, &data->close_far_users, 259 SX9500_REG_IRQ_MSK, 260 SX9500_CLOSE_IRQ | SX9500_FAR_IRQ); 261 } 262 263 static int sx9500_dec_close_far_users(struct sx9500_data *data) 264 { 265 return sx9500_dec_users(data, &data->close_far_users, 266 SX9500_REG_IRQ_MSK, 267 SX9500_CLOSE_IRQ | SX9500_FAR_IRQ); 268 } 269 270 static int sx9500_read_prox_data(struct sx9500_data *data, 271 const struct iio_chan_spec *chan, 272 int *val) 273 { 274 int ret; 275 __be16 regval; 276 277 ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel); 278 if (ret < 0) 279 return ret; 280 281 ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, ®val, 2); 282 if (ret < 0) 283 return ret; 284 285 *val = be16_to_cpu(regval); 286 287 return IIO_VAL_INT; 288 } 289 290 /* 291 * If we have no interrupt support, we have to wait for a scan period 292 * after enabling a channel to get a result. 293 */ 294 static int sx9500_wait_for_sample(struct sx9500_data *data) 295 { 296 int ret; 297 unsigned int val; 298 299 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val); 300 if (ret < 0) 301 return ret; 302 303 val = (val & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT; 304 305 msleep(sx9500_scan_period_table[val]); 306 307 return 0; 308 } 309 310 static int sx9500_read_proximity(struct sx9500_data *data, 311 const struct iio_chan_spec *chan, 312 int *val) 313 { 314 int ret; 315 316 mutex_lock(&data->mutex); 317 318 ret = sx9500_inc_chan_users(data, chan->channel); 319 if (ret < 0) 320 goto out; 321 322 ret = sx9500_inc_data_rdy_users(data); 323 if (ret < 0) 324 goto out_dec_chan; 325 326 mutex_unlock(&data->mutex); 327 328 if (data->client->irq > 0) 329 ret = wait_for_completion_interruptible(&data->completion); 330 else 331 ret = sx9500_wait_for_sample(data); 332 333 mutex_lock(&data->mutex); 334 335 if (ret < 0) 336 goto out_dec_data_rdy; 337 338 ret = sx9500_read_prox_data(data, chan, val); 339 if (ret < 0) 340 goto out_dec_data_rdy; 341 342 ret = sx9500_dec_data_rdy_users(data); 343 if (ret < 0) 344 goto out_dec_chan; 345 346 ret = sx9500_dec_chan_users(data, chan->channel); 347 if (ret < 0) 348 goto out; 349 350 ret = IIO_VAL_INT; 351 352 goto out; 353 354 out_dec_data_rdy: 355 sx9500_dec_data_rdy_users(data); 356 out_dec_chan: 357 sx9500_dec_chan_users(data, chan->channel); 358 out: 359 mutex_unlock(&data->mutex); 360 reinit_completion(&data->completion); 361 362 return ret; 363 } 364 365 static int sx9500_read_samp_freq(struct sx9500_data *data, 366 int *val, int *val2) 367 { 368 int ret; 369 unsigned int regval; 370 371 mutex_lock(&data->mutex); 372 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, ®val); 373 mutex_unlock(&data->mutex); 374 375 if (ret < 0) 376 return ret; 377 378 regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT; 379 *val = sx9500_samp_freq_table[regval].val; 380 *val2 = sx9500_samp_freq_table[regval].val2; 381 382 return IIO_VAL_INT_PLUS_MICRO; 383 } 384 385 static int sx9500_read_raw(struct iio_dev *indio_dev, 386 const struct iio_chan_spec *chan, 387 int *val, int *val2, long mask) 388 { 389 struct sx9500_data *data = iio_priv(indio_dev); 390 391 switch (chan->type) { 392 case IIO_PROXIMITY: 393 switch (mask) { 394 case IIO_CHAN_INFO_RAW: 395 if (iio_buffer_enabled(indio_dev)) 396 return -EBUSY; 397 return sx9500_read_proximity(data, chan, val); 398 case IIO_CHAN_INFO_SAMP_FREQ: 399 return sx9500_read_samp_freq(data, val, val2); 400 default: 401 return -EINVAL; 402 } 403 default: 404 return -EINVAL; 405 } 406 } 407 408 static int sx9500_set_samp_freq(struct sx9500_data *data, 409 int val, int val2) 410 { 411 int i, ret; 412 413 for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++) 414 if (val == sx9500_samp_freq_table[i].val && 415 val2 == sx9500_samp_freq_table[i].val2) 416 break; 417 418 if (i == ARRAY_SIZE(sx9500_samp_freq_table)) 419 return -EINVAL; 420 421 mutex_lock(&data->mutex); 422 423 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, 424 SX9500_SCAN_PERIOD_MASK, 425 i << SX9500_SCAN_PERIOD_SHIFT); 426 427 mutex_unlock(&data->mutex); 428 429 return ret; 430 } 431 432 static int sx9500_write_raw(struct iio_dev *indio_dev, 433 const struct iio_chan_spec *chan, 434 int val, int val2, long mask) 435 { 436 struct sx9500_data *data = iio_priv(indio_dev); 437 438 switch (chan->type) { 439 case IIO_PROXIMITY: 440 switch (mask) { 441 case IIO_CHAN_INFO_SAMP_FREQ: 442 return sx9500_set_samp_freq(data, val, val2); 443 default: 444 return -EINVAL; 445 } 446 default: 447 return -EINVAL; 448 } 449 } 450 451 static irqreturn_t sx9500_irq_handler(int irq, void *private) 452 { 453 struct iio_dev *indio_dev = private; 454 struct sx9500_data *data = iio_priv(indio_dev); 455 456 if (data->trigger_enabled) 457 iio_trigger_poll(data->trig); 458 459 /* 460 * Even if no event is enabled, we need to wake the thread to 461 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It 462 * is not possible to do that here because regmap_read takes a 463 * mutex. 464 */ 465 return IRQ_WAKE_THREAD; 466 } 467 468 static void sx9500_push_events(struct iio_dev *indio_dev) 469 { 470 int ret; 471 unsigned int val, chan; 472 struct sx9500_data *data = iio_priv(indio_dev); 473 474 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); 475 if (ret < 0) { 476 dev_err(&data->client->dev, "i2c transfer error in irq\n"); 477 return; 478 } 479 480 val >>= SX9500_PROXSTAT_SHIFT; 481 for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) { 482 int dir; 483 u64 ev; 484 bool new_prox = val & BIT(chan); 485 486 if (!data->event_enabled[chan]) 487 continue; 488 if (new_prox == data->prox_stat[chan]) 489 /* No change on this channel. */ 490 continue; 491 492 dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; 493 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, 494 IIO_EV_TYPE_THRESH, dir); 495 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev)); 496 data->prox_stat[chan] = new_prox; 497 } 498 } 499 500 static irqreturn_t sx9500_irq_thread_handler(int irq, void *private) 501 { 502 struct iio_dev *indio_dev = private; 503 struct sx9500_data *data = iio_priv(indio_dev); 504 int ret; 505 unsigned int val; 506 507 mutex_lock(&data->mutex); 508 509 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); 510 if (ret < 0) { 511 dev_err(&data->client->dev, "i2c transfer error in irq\n"); 512 goto out; 513 } 514 515 if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ)) 516 sx9500_push_events(indio_dev); 517 518 if (val & SX9500_CONVDONE_IRQ) 519 complete_all(&data->completion); 520 521 out: 522 mutex_unlock(&data->mutex); 523 524 return IRQ_HANDLED; 525 } 526 527 static int sx9500_read_event_config(struct iio_dev *indio_dev, 528 const struct iio_chan_spec *chan, 529 enum iio_event_type type, 530 enum iio_event_direction dir) 531 { 532 struct sx9500_data *data = iio_priv(indio_dev); 533 534 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || 535 dir != IIO_EV_DIR_EITHER) 536 return -EINVAL; 537 538 return data->event_enabled[chan->channel]; 539 } 540 541 static int sx9500_write_event_config(struct iio_dev *indio_dev, 542 const struct iio_chan_spec *chan, 543 enum iio_event_type type, 544 enum iio_event_direction dir, 545 int state) 546 { 547 struct sx9500_data *data = iio_priv(indio_dev); 548 int ret; 549 550 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || 551 dir != IIO_EV_DIR_EITHER) 552 return -EINVAL; 553 554 mutex_lock(&data->mutex); 555 556 if (state == 1) { 557 ret = sx9500_inc_chan_users(data, chan->channel); 558 if (ret < 0) 559 goto out_unlock; 560 ret = sx9500_inc_close_far_users(data); 561 if (ret < 0) 562 goto out_undo_chan; 563 } else { 564 ret = sx9500_dec_chan_users(data, chan->channel); 565 if (ret < 0) 566 goto out_unlock; 567 ret = sx9500_dec_close_far_users(data); 568 if (ret < 0) 569 goto out_undo_chan; 570 } 571 572 data->event_enabled[chan->channel] = state; 573 goto out_unlock; 574 575 out_undo_chan: 576 if (state == 1) 577 sx9500_dec_chan_users(data, chan->channel); 578 else 579 sx9500_inc_chan_users(data, chan->channel); 580 out_unlock: 581 mutex_unlock(&data->mutex); 582 return ret; 583 } 584 585 static int sx9500_update_scan_mode(struct iio_dev *indio_dev, 586 const unsigned long *scan_mask) 587 { 588 struct sx9500_data *data = iio_priv(indio_dev); 589 590 mutex_lock(&data->mutex); 591 kfree(data->buffer); 592 data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL); 593 mutex_unlock(&data->mutex); 594 595 if (data->buffer == NULL) 596 return -ENOMEM; 597 598 return 0; 599 } 600 601 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 602 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333"); 603 604 static struct attribute *sx9500_attributes[] = { 605 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 606 NULL, 607 }; 608 609 static const struct attribute_group sx9500_attribute_group = { 610 .attrs = sx9500_attributes, 611 }; 612 613 static const struct iio_info sx9500_info = { 614 .driver_module = THIS_MODULE, 615 .attrs = &sx9500_attribute_group, 616 .read_raw = &sx9500_read_raw, 617 .write_raw = &sx9500_write_raw, 618 .read_event_config = &sx9500_read_event_config, 619 .write_event_config = &sx9500_write_event_config, 620 .update_scan_mode = &sx9500_update_scan_mode, 621 }; 622 623 static int sx9500_set_trigger_state(struct iio_trigger *trig, 624 bool state) 625 { 626 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 627 struct sx9500_data *data = iio_priv(indio_dev); 628 int ret; 629 630 mutex_lock(&data->mutex); 631 632 if (state) 633 ret = sx9500_inc_data_rdy_users(data); 634 else 635 ret = sx9500_dec_data_rdy_users(data); 636 if (ret < 0) 637 goto out; 638 639 data->trigger_enabled = state; 640 641 out: 642 mutex_unlock(&data->mutex); 643 644 return ret; 645 } 646 647 static const struct iio_trigger_ops sx9500_trigger_ops = { 648 .set_trigger_state = sx9500_set_trigger_state, 649 .owner = THIS_MODULE, 650 }; 651 652 static irqreturn_t sx9500_trigger_handler(int irq, void *private) 653 { 654 struct iio_poll_func *pf = private; 655 struct iio_dev *indio_dev = pf->indio_dev; 656 struct sx9500_data *data = iio_priv(indio_dev); 657 int val, bit, ret, i = 0; 658 659 mutex_lock(&data->mutex); 660 661 for_each_set_bit(bit, indio_dev->active_scan_mask, 662 indio_dev->masklength) { 663 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit], 664 &val); 665 if (ret < 0) 666 goto out; 667 668 data->buffer[i++] = val; 669 } 670 671 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 672 iio_get_time_ns(indio_dev)); 673 674 out: 675 mutex_unlock(&data->mutex); 676 677 iio_trigger_notify_done(indio_dev->trig); 678 679 return IRQ_HANDLED; 680 } 681 682 static int sx9500_buffer_preenable(struct iio_dev *indio_dev) 683 { 684 struct sx9500_data *data = iio_priv(indio_dev); 685 int ret = 0, i; 686 687 mutex_lock(&data->mutex); 688 689 for (i = 0; i < SX9500_NUM_CHANNELS; i++) 690 if (test_bit(i, indio_dev->active_scan_mask)) { 691 ret = sx9500_inc_chan_users(data, i); 692 if (ret) 693 break; 694 } 695 696 if (ret) 697 for (i = i - 1; i >= 0; i--) 698 if (test_bit(i, indio_dev->active_scan_mask)) 699 sx9500_dec_chan_users(data, i); 700 701 mutex_unlock(&data->mutex); 702 703 return ret; 704 } 705 706 static int sx9500_buffer_predisable(struct iio_dev *indio_dev) 707 { 708 struct sx9500_data *data = iio_priv(indio_dev); 709 int ret = 0, i; 710 711 iio_triggered_buffer_predisable(indio_dev); 712 713 mutex_lock(&data->mutex); 714 715 for (i = 0; i < SX9500_NUM_CHANNELS; i++) 716 if (test_bit(i, indio_dev->active_scan_mask)) { 717 ret = sx9500_dec_chan_users(data, i); 718 if (ret) 719 break; 720 } 721 722 if (ret) 723 for (i = i - 1; i >= 0; i--) 724 if (test_bit(i, indio_dev->active_scan_mask)) 725 sx9500_inc_chan_users(data, i); 726 727 mutex_unlock(&data->mutex); 728 729 return ret; 730 } 731 732 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = { 733 .preenable = sx9500_buffer_preenable, 734 .postenable = iio_triggered_buffer_postenable, 735 .predisable = sx9500_buffer_predisable, 736 }; 737 738 struct sx9500_reg_default { 739 u8 reg; 740 u8 def; 741 }; 742 743 static const struct sx9500_reg_default sx9500_default_regs[] = { 744 { 745 .reg = SX9500_REG_PROX_CTRL1, 746 /* Shield enabled, small range. */ 747 .def = 0x43, 748 }, 749 { 750 .reg = SX9500_REG_PROX_CTRL2, 751 /* x8 gain, 167kHz frequency, finest resolution. */ 752 .def = 0x77, 753 }, 754 { 755 .reg = SX9500_REG_PROX_CTRL3, 756 /* Doze enabled, 2x scan period doze, no raw filter. */ 757 .def = 0x40, 758 }, 759 { 760 .reg = SX9500_REG_PROX_CTRL4, 761 /* Average threshold. */ 762 .def = 0x30, 763 }, 764 { 765 .reg = SX9500_REG_PROX_CTRL5, 766 /* 767 * Debouncer off, lowest average negative filter, 768 * highest average postive filter. 769 */ 770 .def = 0x0f, 771 }, 772 { 773 .reg = SX9500_REG_PROX_CTRL6, 774 /* Proximity detection threshold: 280 */ 775 .def = 0x0e, 776 }, 777 { 778 .reg = SX9500_REG_PROX_CTRL7, 779 /* 780 * No automatic compensation, compensate each pin 781 * independently, proximity hysteresis: 32, close 782 * debouncer off, far debouncer off. 783 */ 784 .def = 0x00, 785 }, 786 { 787 .reg = SX9500_REG_PROX_CTRL8, 788 /* No stuck timeout, no periodic compensation. */ 789 .def = 0x00, 790 }, 791 { 792 .reg = SX9500_REG_PROX_CTRL0, 793 /* Scan period: 30ms, all sensors disabled. */ 794 .def = 0x00, 795 }, 796 }; 797 798 /* Activate all channels and perform an initial compensation. */ 799 static int sx9500_init_compensation(struct iio_dev *indio_dev) 800 { 801 struct sx9500_data *data = iio_priv(indio_dev); 802 int i, ret; 803 unsigned int val; 804 805 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, 806 SX9500_CHAN_MASK, SX9500_CHAN_MASK); 807 if (ret < 0) 808 return ret; 809 810 for (i = 10; i >= 0; i--) { 811 usleep_range(10000, 20000); 812 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); 813 if (ret < 0) 814 goto out; 815 if (!(val & SX9500_COMPSTAT_MASK)) 816 break; 817 } 818 819 if (i < 0) { 820 dev_err(&data->client->dev, "initial compensation timed out"); 821 ret = -ETIMEDOUT; 822 } 823 824 out: 825 regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, 826 SX9500_CHAN_MASK, 0); 827 return ret; 828 } 829 830 static int sx9500_init_device(struct iio_dev *indio_dev) 831 { 832 struct sx9500_data *data = iio_priv(indio_dev); 833 int ret, i; 834 unsigned int val; 835 836 if (data->gpiod_rst) { 837 gpiod_set_value_cansleep(data->gpiod_rst, 0); 838 usleep_range(1000, 2000); 839 gpiod_set_value_cansleep(data->gpiod_rst, 1); 840 usleep_range(1000, 2000); 841 } 842 843 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0); 844 if (ret < 0) 845 return ret; 846 847 ret = regmap_write(data->regmap, SX9500_REG_RESET, 848 SX9500_SOFT_RESET); 849 if (ret < 0) 850 return ret; 851 852 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); 853 if (ret < 0) 854 return ret; 855 856 for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) { 857 ret = regmap_write(data->regmap, 858 sx9500_default_regs[i].reg, 859 sx9500_default_regs[i].def); 860 if (ret < 0) 861 return ret; 862 } 863 864 return sx9500_init_compensation(indio_dev); 865 } 866 867 static void sx9500_gpio_probe(struct i2c_client *client, 868 struct sx9500_data *data) 869 { 870 struct device *dev; 871 872 if (!client) 873 return; 874 875 dev = &client->dev; 876 877 data->gpiod_rst = devm_gpiod_get_index(dev, SX9500_GPIO_RESET, 878 0, GPIOD_OUT_HIGH); 879 if (IS_ERR(data->gpiod_rst)) { 880 dev_warn(dev, "gpio get reset pin failed\n"); 881 data->gpiod_rst = NULL; 882 } 883 } 884 885 static int sx9500_probe(struct i2c_client *client, 886 const struct i2c_device_id *id) 887 { 888 int ret; 889 struct iio_dev *indio_dev; 890 struct sx9500_data *data; 891 892 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 893 if (indio_dev == NULL) 894 return -ENOMEM; 895 896 data = iio_priv(indio_dev); 897 data->client = client; 898 mutex_init(&data->mutex); 899 init_completion(&data->completion); 900 data->trigger_enabled = false; 901 902 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config); 903 if (IS_ERR(data->regmap)) 904 return PTR_ERR(data->regmap); 905 906 indio_dev->dev.parent = &client->dev; 907 indio_dev->name = SX9500_DRIVER_NAME; 908 indio_dev->channels = sx9500_channels; 909 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels); 910 indio_dev->info = &sx9500_info; 911 indio_dev->modes = INDIO_DIRECT_MODE; 912 i2c_set_clientdata(client, indio_dev); 913 914 sx9500_gpio_probe(client, data); 915 916 ret = sx9500_init_device(indio_dev); 917 if (ret < 0) 918 return ret; 919 920 if (client->irq <= 0) 921 dev_warn(&client->dev, "no valid irq found\n"); 922 else { 923 ret = devm_request_threaded_irq(&client->dev, client->irq, 924 sx9500_irq_handler, sx9500_irq_thread_handler, 925 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 926 SX9500_IRQ_NAME, indio_dev); 927 if (ret < 0) 928 return ret; 929 930 data->trig = devm_iio_trigger_alloc(&client->dev, 931 "%s-dev%d", indio_dev->name, indio_dev->id); 932 if (!data->trig) 933 return -ENOMEM; 934 935 data->trig->dev.parent = &client->dev; 936 data->trig->ops = &sx9500_trigger_ops; 937 iio_trigger_set_drvdata(data->trig, indio_dev); 938 939 ret = iio_trigger_register(data->trig); 940 if (ret) 941 return ret; 942 } 943 944 ret = iio_triggered_buffer_setup(indio_dev, NULL, 945 sx9500_trigger_handler, 946 &sx9500_buffer_setup_ops); 947 if (ret < 0) 948 goto out_trigger_unregister; 949 950 ret = iio_device_register(indio_dev); 951 if (ret < 0) 952 goto out_buffer_cleanup; 953 954 return 0; 955 956 out_buffer_cleanup: 957 iio_triggered_buffer_cleanup(indio_dev); 958 out_trigger_unregister: 959 if (client->irq > 0) 960 iio_trigger_unregister(data->trig); 961 962 return ret; 963 } 964 965 static int sx9500_remove(struct i2c_client *client) 966 { 967 struct iio_dev *indio_dev = i2c_get_clientdata(client); 968 struct sx9500_data *data = iio_priv(indio_dev); 969 970 iio_device_unregister(indio_dev); 971 iio_triggered_buffer_cleanup(indio_dev); 972 if (client->irq > 0) 973 iio_trigger_unregister(data->trig); 974 kfree(data->buffer); 975 976 return 0; 977 } 978 979 #ifdef CONFIG_PM_SLEEP 980 static int sx9500_suspend(struct device *dev) 981 { 982 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 983 struct sx9500_data *data = iio_priv(indio_dev); 984 int ret; 985 986 mutex_lock(&data->mutex); 987 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, 988 &data->suspend_ctrl0); 989 if (ret < 0) 990 goto out; 991 992 /* 993 * Scan period doesn't matter because when all the sensors are 994 * deactivated the device is in sleep mode. 995 */ 996 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0); 997 998 out: 999 mutex_unlock(&data->mutex); 1000 return ret; 1001 } 1002 1003 static int sx9500_resume(struct device *dev) 1004 { 1005 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1006 struct sx9500_data *data = iio_priv(indio_dev); 1007 int ret; 1008 1009 mutex_lock(&data->mutex); 1010 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 1011 data->suspend_ctrl0); 1012 mutex_unlock(&data->mutex); 1013 1014 return ret; 1015 } 1016 #endif /* CONFIG_PM_SLEEP */ 1017 1018 static const struct dev_pm_ops sx9500_pm_ops = { 1019 SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume) 1020 }; 1021 1022 static const struct acpi_device_id sx9500_acpi_match[] = { 1023 {"SSX9500", 0}, 1024 { }, 1025 }; 1026 MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match); 1027 1028 static const struct i2c_device_id sx9500_id[] = { 1029 {"sx9500", 0}, 1030 { }, 1031 }; 1032 MODULE_DEVICE_TABLE(i2c, sx9500_id); 1033 1034 static struct i2c_driver sx9500_driver = { 1035 .driver = { 1036 .name = SX9500_DRIVER_NAME, 1037 .acpi_match_table = ACPI_PTR(sx9500_acpi_match), 1038 .pm = &sx9500_pm_ops, 1039 }, 1040 .probe = sx9500_probe, 1041 .remove = sx9500_remove, 1042 .id_table = sx9500_id, 1043 }; 1044 module_i2c_driver(sx9500_driver); 1045 1046 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 1047 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor"); 1048 MODULE_LICENSE("GPL v2"); 1049