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