Vlad Dogaru | 4193c0f | 2014-12-29 14:41:14 +0200 | [diff] [blame] | 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 | |
| 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/buffer.h> |
| 24 | #include <linux/iio/sysfs.h> |
| 25 | #include <linux/iio/events.h> |
| 26 | #include <linux/iio/trigger.h> |
| 27 | #include <linux/iio/triggered_buffer.h> |
| 28 | #include <linux/iio/trigger_consumer.h> |
| 29 | |
| 30 | #define SX9500_DRIVER_NAME "sx9500" |
| 31 | #define SX9500_IRQ_NAME "sx9500_event" |
| 32 | #define SX9500_GPIO_NAME "sx9500_gpio" |
| 33 | |
| 34 | /* Register definitions. */ |
| 35 | #define SX9500_REG_IRQ_SRC 0x00 |
| 36 | #define SX9500_REG_STAT 0x01 |
| 37 | #define SX9500_REG_IRQ_MSK 0x03 |
| 38 | |
| 39 | #define SX9500_REG_PROX_CTRL0 0x06 |
| 40 | #define SX9500_REG_PROX_CTRL1 0x07 |
| 41 | #define SX9500_REG_PROX_CTRL2 0x08 |
| 42 | #define SX9500_REG_PROX_CTRL3 0x09 |
| 43 | #define SX9500_REG_PROX_CTRL4 0x0a |
| 44 | #define SX9500_REG_PROX_CTRL5 0x0b |
| 45 | #define SX9500_REG_PROX_CTRL6 0x0c |
| 46 | #define SX9500_REG_PROX_CTRL7 0x0d |
| 47 | #define SX9500_REG_PROX_CTRL8 0x0e |
| 48 | |
| 49 | #define SX9500_REG_SENSOR_SEL 0x20 |
| 50 | #define SX9500_REG_USE_MSB 0x21 |
| 51 | #define SX9500_REG_USE_LSB 0x22 |
| 52 | #define SX9500_REG_AVG_MSB 0x23 |
| 53 | #define SX9500_REG_AVG_LSB 0x24 |
| 54 | #define SX9500_REG_DIFF_MSB 0x25 |
| 55 | #define SX9500_REG_DIFF_LSB 0x26 |
| 56 | #define SX9500_REG_OFFSET_MSB 0x27 |
| 57 | #define SX9500_REG_OFFSET_LSB 0x28 |
| 58 | |
| 59 | #define SX9500_REG_RESET 0x7f |
| 60 | |
| 61 | /* Write this to REG_RESET to do a soft reset. */ |
| 62 | #define SX9500_SOFT_RESET 0xde |
| 63 | |
| 64 | #define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4) |
| 65 | #define SX9500_SCAN_PERIOD_SHIFT 4 |
| 66 | |
| 67 | /* |
| 68 | * These serve for identifying IRQ source in the IRQ_SRC register, and |
| 69 | * also for masking the IRQs in the IRQ_MSK register. |
| 70 | */ |
| 71 | #define SX9500_CLOSE_IRQ BIT(6) |
| 72 | #define SX9500_FAR_IRQ BIT(5) |
| 73 | #define SX9500_CONVDONE_IRQ BIT(3) |
| 74 | |
| 75 | #define SX9500_PROXSTAT_SHIFT 4 |
| 76 | |
| 77 | #define SX9500_NUM_CHANNELS 4 |
| 78 | |
| 79 | struct sx9500_data { |
| 80 | struct mutex mutex; |
| 81 | struct i2c_client *client; |
| 82 | struct iio_trigger *trig; |
| 83 | struct regmap *regmap; |
| 84 | /* |
| 85 | * Last reading of the proximity status for each channel. We |
| 86 | * only send an event to user space when this changes. |
| 87 | */ |
| 88 | bool prox_stat[SX9500_NUM_CHANNELS]; |
| 89 | bool event_enabled[SX9500_NUM_CHANNELS]; |
| 90 | bool trigger_enabled; |
| 91 | u16 *buffer; |
| 92 | }; |
| 93 | |
| 94 | static const struct iio_event_spec sx9500_events[] = { |
| 95 | { |
| 96 | .type = IIO_EV_TYPE_THRESH, |
| 97 | .dir = IIO_EV_DIR_EITHER, |
| 98 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 99 | }, |
| 100 | }; |
| 101 | |
| 102 | #define SX9500_CHANNEL(idx) \ |
| 103 | { \ |
| 104 | .type = IIO_PROXIMITY, \ |
| 105 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 106 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 107 | .indexed = 1, \ |
| 108 | .channel = idx, \ |
| 109 | .event_spec = sx9500_events, \ |
| 110 | .num_event_specs = ARRAY_SIZE(sx9500_events), \ |
| 111 | .scan_index = idx, \ |
| 112 | .scan_type = { \ |
| 113 | .sign = 'u', \ |
| 114 | .realbits = 16, \ |
| 115 | .storagebits = 16, \ |
| 116 | .shift = 0, \ |
| 117 | }, \ |
| 118 | } |
| 119 | |
| 120 | static const struct iio_chan_spec sx9500_channels[] = { |
| 121 | SX9500_CHANNEL(0), |
| 122 | SX9500_CHANNEL(1), |
| 123 | SX9500_CHANNEL(2), |
| 124 | SX9500_CHANNEL(3), |
| 125 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 126 | }; |
| 127 | |
| 128 | static const struct { |
| 129 | int val; |
| 130 | int val2; |
| 131 | } sx9500_samp_freq_table[] = { |
| 132 | {33, 333333}, |
| 133 | {16, 666666}, |
| 134 | {11, 111111}, |
| 135 | {8, 333333}, |
| 136 | {6, 666666}, |
| 137 | {5, 0}, |
| 138 | {3, 333333}, |
| 139 | {2, 500000}, |
| 140 | }; |
| 141 | |
| 142 | static const struct regmap_range sx9500_writable_reg_ranges[] = { |
| 143 | regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK), |
| 144 | regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8), |
| 145 | regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL), |
| 146 | regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB), |
| 147 | regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET), |
| 148 | }; |
| 149 | |
| 150 | static const struct regmap_access_table sx9500_writeable_regs = { |
| 151 | .yes_ranges = sx9500_writable_reg_ranges, |
| 152 | .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges), |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * All allocated registers are readable, so we just list unallocated |
| 157 | * ones. |
| 158 | */ |
| 159 | static const struct regmap_range sx9500_non_readable_reg_ranges[] = { |
| 160 | regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1), |
| 161 | regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1), |
| 162 | regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1), |
| 163 | regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1), |
| 164 | }; |
| 165 | |
| 166 | static const struct regmap_access_table sx9500_readable_regs = { |
| 167 | .no_ranges = sx9500_non_readable_reg_ranges, |
| 168 | .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges), |
| 169 | }; |
| 170 | |
| 171 | static const struct regmap_range sx9500_volatile_reg_ranges[] = { |
| 172 | regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT), |
| 173 | regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB), |
| 174 | regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET), |
| 175 | }; |
| 176 | |
| 177 | static const struct regmap_access_table sx9500_volatile_regs = { |
| 178 | .yes_ranges = sx9500_volatile_reg_ranges, |
| 179 | .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges), |
| 180 | }; |
| 181 | |
| 182 | static const struct regmap_config sx9500_regmap_config = { |
| 183 | .reg_bits = 8, |
| 184 | .val_bits = 8, |
| 185 | |
| 186 | .max_register = SX9500_REG_RESET, |
| 187 | .cache_type = REGCACHE_RBTREE, |
| 188 | |
| 189 | .wr_table = &sx9500_writeable_regs, |
| 190 | .rd_table = &sx9500_readable_regs, |
| 191 | .volatile_table = &sx9500_volatile_regs, |
| 192 | }; |
| 193 | |
| 194 | static int sx9500_read_proximity(struct sx9500_data *data, |
| 195 | const struct iio_chan_spec *chan, |
| 196 | int *val) |
| 197 | { |
| 198 | int ret; |
| 199 | __be16 regval; |
| 200 | |
| 201 | ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel); |
| 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | |
| 205 | ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, ®val, 2); |
| 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
| 209 | *val = 32767 - (s16)be16_to_cpu(regval); |
| 210 | |
| 211 | return IIO_VAL_INT; |
| 212 | } |
| 213 | |
| 214 | static int sx9500_read_samp_freq(struct sx9500_data *data, |
| 215 | int *val, int *val2) |
| 216 | { |
| 217 | int ret; |
| 218 | unsigned int regval; |
| 219 | |
| 220 | mutex_lock(&data->mutex); |
| 221 | ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, ®val); |
| 222 | mutex_unlock(&data->mutex); |
| 223 | |
| 224 | if (ret < 0) |
| 225 | return ret; |
| 226 | |
| 227 | regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT; |
| 228 | *val = sx9500_samp_freq_table[regval].val; |
| 229 | *val2 = sx9500_samp_freq_table[regval].val2; |
| 230 | |
| 231 | return IIO_VAL_INT_PLUS_MICRO; |
| 232 | } |
| 233 | |
| 234 | static int sx9500_read_raw(struct iio_dev *indio_dev, |
| 235 | const struct iio_chan_spec *chan, |
| 236 | int *val, int *val2, long mask) |
| 237 | { |
| 238 | struct sx9500_data *data = iio_priv(indio_dev); |
| 239 | int ret; |
| 240 | |
| 241 | switch (chan->type) { |
| 242 | case IIO_PROXIMITY: |
| 243 | switch (mask) { |
| 244 | case IIO_CHAN_INFO_RAW: |
| 245 | if (iio_buffer_enabled(indio_dev)) |
| 246 | return -EBUSY; |
| 247 | mutex_lock(&data->mutex); |
| 248 | ret = sx9500_read_proximity(data, chan, val); |
| 249 | mutex_unlock(&data->mutex); |
| 250 | return ret; |
| 251 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 252 | return sx9500_read_samp_freq(data, val, val2); |
| 253 | default: |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | default: |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static int sx9500_set_samp_freq(struct sx9500_data *data, |
| 262 | int val, int val2) |
| 263 | { |
| 264 | int i, ret; |
| 265 | |
| 266 | for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++) |
| 267 | if (val == sx9500_samp_freq_table[i].val && |
| 268 | val2 == sx9500_samp_freq_table[i].val2) |
| 269 | break; |
| 270 | |
| 271 | if (i == ARRAY_SIZE(sx9500_samp_freq_table)) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | mutex_lock(&data->mutex); |
| 275 | |
| 276 | ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, |
| 277 | SX9500_SCAN_PERIOD_MASK, |
| 278 | i << SX9500_SCAN_PERIOD_SHIFT); |
| 279 | |
| 280 | mutex_unlock(&data->mutex); |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int sx9500_write_raw(struct iio_dev *indio_dev, |
| 286 | const struct iio_chan_spec *chan, |
| 287 | int val, int val2, long mask) |
| 288 | { |
| 289 | struct sx9500_data *data = iio_priv(indio_dev); |
| 290 | |
| 291 | switch (chan->type) { |
| 292 | case IIO_PROXIMITY: |
| 293 | switch (mask) { |
| 294 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 295 | return sx9500_set_samp_freq(data, val, val2); |
| 296 | default: |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | default: |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static irqreturn_t sx9500_irq_handler(int irq, void *private) |
| 305 | { |
| 306 | struct iio_dev *indio_dev = private; |
| 307 | struct sx9500_data *data = iio_priv(indio_dev); |
| 308 | |
| 309 | if (data->trigger_enabled) |
| 310 | iio_trigger_poll(data->trig); |
| 311 | |
| 312 | /* |
| 313 | * Even if no event is enabled, we need to wake the thread to |
| 314 | * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It |
| 315 | * is not possible to do that here because regmap_read takes a |
| 316 | * mutex. |
| 317 | */ |
| 318 | return IRQ_WAKE_THREAD; |
| 319 | } |
| 320 | |
| 321 | static irqreturn_t sx9500_irq_thread_handler(int irq, void *private) |
| 322 | { |
| 323 | struct iio_dev *indio_dev = private; |
| 324 | struct sx9500_data *data = iio_priv(indio_dev); |
| 325 | int ret; |
| 326 | unsigned int val, chan; |
| 327 | |
| 328 | mutex_lock(&data->mutex); |
| 329 | |
| 330 | ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); |
| 331 | if (ret < 0) { |
| 332 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | if (!(val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))) |
| 337 | goto out; |
| 338 | |
| 339 | ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); |
| 340 | if (ret < 0) { |
| 341 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); |
| 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | val >>= SX9500_PROXSTAT_SHIFT; |
| 346 | for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) { |
| 347 | int dir; |
| 348 | u64 ev; |
| 349 | bool new_prox = val & BIT(chan); |
| 350 | |
| 351 | if (!data->event_enabled[chan]) |
| 352 | continue; |
| 353 | if (new_prox == data->prox_stat[chan]) |
| 354 | /* No change on this channel. */ |
| 355 | continue; |
| 356 | |
| 357 | dir = new_prox ? IIO_EV_DIR_FALLING : |
| 358 | IIO_EV_DIR_RISING; |
| 359 | ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, |
| 360 | chan, |
| 361 | IIO_EV_TYPE_THRESH, |
| 362 | dir); |
| 363 | iio_push_event(indio_dev, ev, iio_get_time_ns()); |
| 364 | data->prox_stat[chan] = new_prox; |
| 365 | } |
| 366 | |
| 367 | out: |
| 368 | mutex_unlock(&data->mutex); |
| 369 | |
| 370 | return IRQ_HANDLED; |
| 371 | } |
| 372 | |
| 373 | static int sx9500_read_event_config(struct iio_dev *indio_dev, |
| 374 | const struct iio_chan_spec *chan, |
| 375 | enum iio_event_type type, |
| 376 | enum iio_event_direction dir) |
| 377 | { |
| 378 | struct sx9500_data *data = iio_priv(indio_dev); |
| 379 | |
| 380 | if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || |
| 381 | dir != IIO_EV_DIR_EITHER) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | return data->event_enabled[chan->channel]; |
| 385 | } |
| 386 | |
| 387 | static int sx9500_write_event_config(struct iio_dev *indio_dev, |
| 388 | const struct iio_chan_spec *chan, |
| 389 | enum iio_event_type type, |
| 390 | enum iio_event_direction dir, |
| 391 | int state) |
| 392 | { |
| 393 | struct sx9500_data *data = iio_priv(indio_dev); |
| 394 | int ret, i; |
| 395 | bool any_active = false; |
| 396 | unsigned int irqmask; |
| 397 | |
| 398 | if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || |
| 399 | dir != IIO_EV_DIR_EITHER) |
| 400 | return -EINVAL; |
| 401 | |
| 402 | mutex_lock(&data->mutex); |
| 403 | |
| 404 | data->event_enabled[chan->channel] = state; |
| 405 | |
| 406 | for (i = 0; i < SX9500_NUM_CHANNELS; i++) |
| 407 | if (data->event_enabled[i]) { |
| 408 | any_active = true; |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | irqmask = SX9500_CLOSE_IRQ | SX9500_FAR_IRQ; |
| 413 | if (any_active) |
| 414 | ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK, |
| 415 | irqmask, irqmask); |
| 416 | else |
| 417 | ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK, |
| 418 | irqmask, 0); |
| 419 | |
| 420 | mutex_unlock(&data->mutex); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static int sx9500_update_scan_mode(struct iio_dev *indio_dev, |
| 426 | const unsigned long *scan_mask) |
| 427 | { |
| 428 | struct sx9500_data *data = iio_priv(indio_dev); |
| 429 | |
| 430 | mutex_lock(&data->mutex); |
| 431 | kfree(data->buffer); |
| 432 | data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL); |
| 433 | mutex_unlock(&data->mutex); |
| 434 | |
| 435 | if (data->buffer == NULL) |
| 436 | return -ENOMEM; |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( |
| 442 | "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333"); |
| 443 | |
| 444 | static struct attribute *sx9500_attributes[] = { |
| 445 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 446 | NULL, |
| 447 | }; |
| 448 | |
| 449 | static const struct attribute_group sx9500_attribute_group = { |
| 450 | .attrs = sx9500_attributes, |
| 451 | }; |
| 452 | |
| 453 | static const struct iio_info sx9500_info = { |
| 454 | .driver_module = THIS_MODULE, |
| 455 | .attrs = &sx9500_attribute_group, |
| 456 | .read_raw = &sx9500_read_raw, |
| 457 | .write_raw = &sx9500_write_raw, |
| 458 | .read_event_config = &sx9500_read_event_config, |
| 459 | .write_event_config = &sx9500_write_event_config, |
| 460 | .update_scan_mode = &sx9500_update_scan_mode, |
| 461 | }; |
| 462 | |
| 463 | static int sx9500_set_trigger_state(struct iio_trigger *trig, |
| 464 | bool state) |
| 465 | { |
| 466 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 467 | struct sx9500_data *data = iio_priv(indio_dev); |
| 468 | int ret; |
| 469 | |
| 470 | mutex_lock(&data->mutex); |
| 471 | |
| 472 | ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK, |
| 473 | SX9500_CONVDONE_IRQ, |
| 474 | state ? SX9500_CONVDONE_IRQ : 0); |
| 475 | if (ret == 0) |
| 476 | data->trigger_enabled = state; |
| 477 | |
| 478 | mutex_unlock(&data->mutex); |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static const struct iio_trigger_ops sx9500_trigger_ops = { |
| 484 | .set_trigger_state = sx9500_set_trigger_state, |
| 485 | .owner = THIS_MODULE, |
| 486 | }; |
| 487 | |
| 488 | static irqreturn_t sx9500_trigger_handler(int irq, void *private) |
| 489 | { |
| 490 | struct iio_poll_func *pf = private; |
| 491 | struct iio_dev *indio_dev = pf->indio_dev; |
| 492 | struct sx9500_data *data = iio_priv(indio_dev); |
| 493 | int val, bit, ret, i = 0; |
| 494 | |
| 495 | mutex_lock(&data->mutex); |
| 496 | |
Octavian Purdila | 70dddee | 2015-03-02 21:03:05 +0200 | [diff] [blame^] | 497 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
Vlad Dogaru | 4193c0f | 2014-12-29 14:41:14 +0200 | [diff] [blame] | 498 | indio_dev->masklength) { |
| 499 | ret = sx9500_read_proximity(data, &indio_dev->channels[bit], |
| 500 | &val); |
| 501 | if (ret < 0) |
| 502 | goto out; |
| 503 | |
| 504 | data->buffer[i++] = val; |
| 505 | } |
| 506 | |
| 507 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 508 | iio_get_time_ns()); |
| 509 | |
| 510 | out: |
| 511 | mutex_unlock(&data->mutex); |
| 512 | |
| 513 | iio_trigger_notify_done(indio_dev->trig); |
| 514 | |
| 515 | return IRQ_HANDLED; |
| 516 | } |
| 517 | |
| 518 | struct sx9500_reg_default { |
| 519 | u8 reg; |
| 520 | u8 def; |
| 521 | }; |
| 522 | |
| 523 | static const struct sx9500_reg_default sx9500_default_regs[] = { |
| 524 | { |
| 525 | .reg = SX9500_REG_PROX_CTRL1, |
| 526 | /* Shield enabled, small range. */ |
| 527 | .def = 0x43, |
| 528 | }, |
| 529 | { |
| 530 | .reg = SX9500_REG_PROX_CTRL2, |
| 531 | /* x8 gain, 167kHz frequency, finest resolution. */ |
| 532 | .def = 0x77, |
| 533 | }, |
| 534 | { |
| 535 | .reg = SX9500_REG_PROX_CTRL3, |
| 536 | /* Doze enabled, 2x scan period doze, no raw filter. */ |
| 537 | .def = 0x40, |
| 538 | }, |
| 539 | { |
| 540 | .reg = SX9500_REG_PROX_CTRL4, |
| 541 | /* Average threshold. */ |
| 542 | .def = 0x30, |
| 543 | }, |
| 544 | { |
| 545 | .reg = SX9500_REG_PROX_CTRL5, |
| 546 | /* |
| 547 | * Debouncer off, lowest average negative filter, |
| 548 | * highest average postive filter. |
| 549 | */ |
| 550 | .def = 0x0f, |
| 551 | }, |
| 552 | { |
| 553 | .reg = SX9500_REG_PROX_CTRL6, |
| 554 | /* Proximity detection threshold: 280 */ |
| 555 | .def = 0x0e, |
| 556 | }, |
| 557 | { |
| 558 | .reg = SX9500_REG_PROX_CTRL7, |
| 559 | /* |
| 560 | * No automatic compensation, compensate each pin |
| 561 | * independently, proximity hysteresis: 32, close |
| 562 | * debouncer off, far debouncer off. |
| 563 | */ |
| 564 | .def = 0x00, |
| 565 | }, |
| 566 | { |
| 567 | .reg = SX9500_REG_PROX_CTRL8, |
| 568 | /* No stuck timeout, no periodic compensation. */ |
| 569 | .def = 0x00, |
| 570 | }, |
| 571 | { |
| 572 | .reg = SX9500_REG_PROX_CTRL0, |
| 573 | /* Scan period: 30ms, all sensors enabled. */ |
| 574 | .def = 0x0f, |
| 575 | }, |
| 576 | }; |
| 577 | |
| 578 | static int sx9500_init_device(struct iio_dev *indio_dev) |
| 579 | { |
| 580 | struct sx9500_data *data = iio_priv(indio_dev); |
| 581 | int ret, i; |
| 582 | unsigned int val; |
| 583 | |
| 584 | ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0); |
| 585 | if (ret < 0) |
| 586 | return ret; |
| 587 | |
| 588 | ret = regmap_write(data->regmap, SX9500_REG_RESET, |
| 589 | SX9500_SOFT_RESET); |
| 590 | if (ret < 0) |
| 591 | return ret; |
| 592 | |
| 593 | ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); |
| 594 | if (ret < 0) |
| 595 | return ret; |
| 596 | |
| 597 | for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) { |
| 598 | ret = regmap_write(data->regmap, |
| 599 | sx9500_default_regs[i].reg, |
| 600 | sx9500_default_regs[i].def); |
| 601 | if (ret < 0) |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int sx9500_gpio_probe(struct i2c_client *client, |
| 609 | struct sx9500_data *data) |
| 610 | { |
| 611 | struct device *dev; |
| 612 | struct gpio_desc *gpio; |
| 613 | int ret; |
| 614 | |
| 615 | if (!client) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | dev = &client->dev; |
| 619 | |
| 620 | /* data ready gpio interrupt pin */ |
| 621 | gpio = devm_gpiod_get_index(dev, SX9500_GPIO_NAME, 0); |
| 622 | if (IS_ERR(gpio)) { |
| 623 | dev_err(dev, "acpi gpio get index failed\n"); |
| 624 | return PTR_ERR(gpio); |
| 625 | } |
| 626 | |
| 627 | ret = gpiod_direction_input(gpio); |
| 628 | if (ret) |
| 629 | return ret; |
| 630 | |
| 631 | ret = gpiod_to_irq(gpio); |
| 632 | |
| 633 | dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); |
| 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int sx9500_probe(struct i2c_client *client, |
| 639 | const struct i2c_device_id *id) |
| 640 | { |
| 641 | int ret; |
| 642 | struct iio_dev *indio_dev; |
| 643 | struct sx9500_data *data; |
| 644 | |
| 645 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 646 | if (indio_dev == NULL) |
| 647 | return -ENOMEM; |
| 648 | |
| 649 | data = iio_priv(indio_dev); |
| 650 | data->client = client; |
| 651 | mutex_init(&data->mutex); |
| 652 | data->trigger_enabled = false; |
| 653 | |
| 654 | data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config); |
| 655 | if (IS_ERR(data->regmap)) |
| 656 | return PTR_ERR(data->regmap); |
| 657 | |
| 658 | sx9500_init_device(indio_dev); |
| 659 | |
| 660 | indio_dev->dev.parent = &client->dev; |
| 661 | indio_dev->name = SX9500_DRIVER_NAME; |
| 662 | indio_dev->channels = sx9500_channels; |
| 663 | indio_dev->num_channels = ARRAY_SIZE(sx9500_channels); |
| 664 | indio_dev->info = &sx9500_info; |
| 665 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 666 | i2c_set_clientdata(client, indio_dev); |
| 667 | |
| 668 | if (client->irq <= 0) |
| 669 | client->irq = sx9500_gpio_probe(client, data); |
| 670 | |
| 671 | if (client->irq > 0) { |
| 672 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 673 | sx9500_irq_handler, sx9500_irq_thread_handler, |
| 674 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 675 | SX9500_IRQ_NAME, indio_dev); |
| 676 | if (ret < 0) |
| 677 | return ret; |
| 678 | |
| 679 | data->trig = devm_iio_trigger_alloc(&client->dev, |
| 680 | "%s-dev%d", indio_dev->name, indio_dev->id); |
| 681 | if (!data->trig) |
| 682 | return -ENOMEM; |
| 683 | |
| 684 | data->trig->dev.parent = &client->dev; |
| 685 | data->trig->ops = &sx9500_trigger_ops; |
| 686 | iio_trigger_set_drvdata(data->trig, indio_dev); |
| 687 | |
| 688 | ret = iio_trigger_register(data->trig); |
| 689 | if (ret) |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 694 | sx9500_trigger_handler, NULL); |
| 695 | if (ret < 0) |
| 696 | goto out_trigger_unregister; |
| 697 | |
| 698 | ret = iio_device_register(indio_dev); |
| 699 | if (ret < 0) |
| 700 | goto out_buffer_cleanup; |
| 701 | |
| 702 | return 0; |
| 703 | |
| 704 | out_buffer_cleanup: |
| 705 | iio_triggered_buffer_cleanup(indio_dev); |
| 706 | out_trigger_unregister: |
| 707 | if (client->irq > 0) |
| 708 | iio_trigger_unregister(data->trig); |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | static int sx9500_remove(struct i2c_client *client) |
| 714 | { |
| 715 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 716 | struct sx9500_data *data = iio_priv(indio_dev); |
| 717 | |
| 718 | iio_device_unregister(indio_dev); |
| 719 | iio_triggered_buffer_cleanup(indio_dev); |
| 720 | if (client->irq > 0) |
| 721 | iio_trigger_unregister(data->trig); |
| 722 | kfree(data->buffer); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static const struct acpi_device_id sx9500_acpi_match[] = { |
| 728 | {"SSX9500", 0}, |
| 729 | { }, |
| 730 | }; |
| 731 | MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match); |
| 732 | |
| 733 | static const struct i2c_device_id sx9500_id[] = { |
| 734 | {"sx9500", 0}, |
| 735 | {} |
| 736 | }; |
| 737 | MODULE_DEVICE_TABLE(i2c, sx9500_id); |
| 738 | |
| 739 | static struct i2c_driver sx9500_driver = { |
| 740 | .driver = { |
| 741 | .name = SX9500_DRIVER_NAME, |
| 742 | .acpi_match_table = ACPI_PTR(sx9500_acpi_match), |
| 743 | }, |
| 744 | .probe = sx9500_probe, |
| 745 | .remove = sx9500_remove, |
| 746 | .id_table = sx9500_id, |
| 747 | }; |
| 748 | module_i2c_driver(sx9500_driver); |
| 749 | |
| 750 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); |
| 751 | MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor"); |
| 752 | MODULE_LICENSE("GPL v2"); |