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