Commit | Line | Data |
---|---|---|
4193c0f1 VD |
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> | |
7840ffee | 21 | #include <linux/pm.h> |
59bd0427 | 22 | #include <linux/delay.h> |
4193c0f1 VD |
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" | |
63de9f92 | 34 | |
821ace29 | 35 | #define SX9500_GPIO_INT "interrupt" |
45fd5f8e | 36 | #define SX9500_GPIO_RESET "reset" |
4193c0f1 VD |
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 | |
59bd0427 | 80 | #define SX9500_COMPSTAT_MASK GENMASK(3, 0) |
4193c0f1 VD |
81 | |
82 | #define SX9500_NUM_CHANNELS 4 | |
83 | ||
84 | struct sx9500_data { | |
85 | struct mutex mutex; | |
86 | struct i2c_client *client; | |
87 | struct iio_trigger *trig; | |
88 | struct regmap *regmap; | |
45fd5f8e | 89 | struct gpio_desc *gpiod_rst; |
4193c0f1 VD |
90 | /* |
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; | |
7840ffee VD |
98 | /* Remember enabled channels and sample rate during suspend. */ |
99 | unsigned int suspend_ctrl0; | |
59bd0427 VD |
100 | struct completion completion; |
101 | int data_rdy_users, close_far_users; | |
102 | int channel_users[SX9500_NUM_CHANNELS]; | |
4193c0f1 VD |
103 | }; |
104 | ||
105 | static 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 | ||
131 | static 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 | ||
139 | static 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 | ||
59bd0427 VD |
153 | static const unsigned int sx9500_scan_period_table[] = { |
154 | 30, 60, 90, 120, 150, 200, 300, 400, | |
155 | }; | |
156 | ||
4193c0f1 VD |
157 | static 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 | ||
165 | static 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 | */ | |
174 | static 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 | ||
181 | static 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 | ||
186 | static 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 | ||
192 | static 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 | ||
197 | static 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 | ||
59bd0427 VD |
209 | static 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 | ||
220 | static 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 | ||
231 | static 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 | ||
237 | static 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 | ||
243 | static 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 | ||
249 | static 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 | ||
255 | static 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 | ||
262 | static 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 | ||
269 | static int sx9500_read_prox_data(struct sx9500_data *data, | |
4193c0f1 VD |
270 | 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, ®val, 2); | |
281 | if (ret < 0) | |
282 | return ret; | |
283 | ||
86d24f04 | 284 | *val = be16_to_cpu(regval); |
4193c0f1 VD |
285 | |
286 | return IIO_VAL_INT; | |
287 | } | |
288 | ||
59bd0427 VD |
289 | /* |
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 | */ | |
293 | static 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 | ||
309 | static 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 | ||
59bd0427 VD |
332 | mutex_lock(&data->mutex); |
333 | ||
59bd0427 | 334 | if (ret < 0) |
498adaeb | 335 | goto out_dec_data_rdy; |
59bd0427 | 336 | |
498adaeb | 337 | ret = sx9500_read_prox_data(data, chan, val); |
59bd0427 | 338 | if (ret < 0) |
498adaeb | 339 | goto out_dec_data_rdy; |
59bd0427 VD |
340 | |
341 | ret = sx9500_dec_data_rdy_users(data); | |
498adaeb VD |
342 | if (ret < 0) |
343 | goto out_dec_chan; | |
344 | ||
345 | ret = sx9500_dec_chan_users(data, chan->channel); | |
59bd0427 VD |
346 | if (ret < 0) |
347 | goto out; | |
348 | ||
349 | ret = IIO_VAL_INT; | |
350 | ||
351 | goto out; | |
352 | ||
498adaeb VD |
353 | out_dec_data_rdy: |
354 | sx9500_dec_data_rdy_users(data); | |
59bd0427 VD |
355 | out_dec_chan: |
356 | sx9500_dec_chan_users(data, chan->channel); | |
357 | out: | |
358 | mutex_unlock(&data->mutex); | |
359 | reinit_completion(&data->completion); | |
360 | ||
361 | return ret; | |
362 | } | |
363 | ||
4193c0f1 VD |
364 | static int sx9500_read_samp_freq(struct sx9500_data *data, |
365 | int *val, int *val2) | |
366 | { | |
367 | int ret; | |
368 | unsigned int regval; | |
369 | ||
370 | mutex_lock(&data->mutex); | |
371 | ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, ®val); | |
372 | mutex_unlock(&data->mutex); | |
373 | ||
374 | if (ret < 0) | |
375 | return ret; | |
376 | ||
377 | regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT; | |
378 | *val = sx9500_samp_freq_table[regval].val; | |
379 | *val2 = sx9500_samp_freq_table[regval].val2; | |
380 | ||
381 | return IIO_VAL_INT_PLUS_MICRO; | |
382 | } | |
383 | ||
384 | static int sx9500_read_raw(struct iio_dev *indio_dev, | |
385 | const struct iio_chan_spec *chan, | |
386 | int *val, int *val2, long mask) | |
387 | { | |
388 | struct sx9500_data *data = iio_priv(indio_dev); | |
4193c0f1 VD |
389 | |
390 | switch (chan->type) { | |
391 | case IIO_PROXIMITY: | |
392 | switch (mask) { | |
393 | case IIO_CHAN_INFO_RAW: | |
394 | if (iio_buffer_enabled(indio_dev)) | |
395 | return -EBUSY; | |
59bd0427 | 396 | return sx9500_read_proximity(data, chan, val); |
4193c0f1 VD |
397 | case IIO_CHAN_INFO_SAMP_FREQ: |
398 | return sx9500_read_samp_freq(data, val, val2); | |
399 | default: | |
400 | return -EINVAL; | |
401 | } | |
402 | default: | |
403 | return -EINVAL; | |
404 | } | |
405 | } | |
406 | ||
407 | static int sx9500_set_samp_freq(struct sx9500_data *data, | |
408 | int val, int val2) | |
409 | { | |
410 | int i, ret; | |
411 | ||
412 | for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++) | |
413 | if (val == sx9500_samp_freq_table[i].val && | |
414 | val2 == sx9500_samp_freq_table[i].val2) | |
415 | break; | |
416 | ||
417 | if (i == ARRAY_SIZE(sx9500_samp_freq_table)) | |
418 | return -EINVAL; | |
419 | ||
420 | mutex_lock(&data->mutex); | |
421 | ||
422 | ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, | |
423 | SX9500_SCAN_PERIOD_MASK, | |
424 | i << SX9500_SCAN_PERIOD_SHIFT); | |
425 | ||
426 | mutex_unlock(&data->mutex); | |
427 | ||
428 | return ret; | |
429 | } | |
430 | ||
431 | static int sx9500_write_raw(struct iio_dev *indio_dev, | |
432 | const struct iio_chan_spec *chan, | |
433 | int val, int val2, long mask) | |
434 | { | |
435 | struct sx9500_data *data = iio_priv(indio_dev); | |
436 | ||
437 | switch (chan->type) { | |
438 | case IIO_PROXIMITY: | |
439 | switch (mask) { | |
440 | case IIO_CHAN_INFO_SAMP_FREQ: | |
441 | return sx9500_set_samp_freq(data, val, val2); | |
442 | default: | |
443 | return -EINVAL; | |
444 | } | |
445 | default: | |
446 | return -EINVAL; | |
447 | } | |
448 | } | |
449 | ||
450 | static irqreturn_t sx9500_irq_handler(int irq, void *private) | |
451 | { | |
452 | struct iio_dev *indio_dev = private; | |
453 | struct sx9500_data *data = iio_priv(indio_dev); | |
454 | ||
455 | if (data->trigger_enabled) | |
456 | iio_trigger_poll(data->trig); | |
457 | ||
458 | /* | |
459 | * Even if no event is enabled, we need to wake the thread to | |
460 | * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It | |
461 | * is not possible to do that here because regmap_read takes a | |
462 | * mutex. | |
463 | */ | |
464 | return IRQ_WAKE_THREAD; | |
465 | } | |
466 | ||
59bd0427 | 467 | static void sx9500_push_events(struct iio_dev *indio_dev) |
4193c0f1 | 468 | { |
4193c0f1 VD |
469 | int ret; |
470 | unsigned int val, chan; | |
59bd0427 | 471 | struct sx9500_data *data = iio_priv(indio_dev); |
4193c0f1 VD |
472 | |
473 | ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); | |
474 | if (ret < 0) { | |
475 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); | |
59bd0427 | 476 | return; |
4193c0f1 VD |
477 | } |
478 | ||
479 | val >>= SX9500_PROXSTAT_SHIFT; | |
480 | for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) { | |
481 | int dir; | |
482 | u64 ev; | |
483 | bool new_prox = val & BIT(chan); | |
484 | ||
485 | if (!data->event_enabled[chan]) | |
486 | continue; | |
487 | if (new_prox == data->prox_stat[chan]) | |
488 | /* No change on this channel. */ | |
489 | continue; | |
490 | ||
59bd0427 VD |
491 | dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; |
492 | ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, | |
493 | IIO_EV_TYPE_THRESH, dir); | |
4193c0f1 VD |
494 | iio_push_event(indio_dev, ev, iio_get_time_ns()); |
495 | data->prox_stat[chan] = new_prox; | |
496 | } | |
59bd0427 VD |
497 | } |
498 | ||
499 | static irqreturn_t sx9500_irq_thread_handler(int irq, void *private) | |
500 | { | |
501 | struct iio_dev *indio_dev = private; | |
502 | struct sx9500_data *data = iio_priv(indio_dev); | |
503 | int ret; | |
504 | unsigned int val; | |
505 | ||
506 | mutex_lock(&data->mutex); | |
507 | ||
508 | ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); | |
509 | if (ret < 0) { | |
510 | dev_err(&data->client->dev, "i2c transfer error in irq\n"); | |
511 | goto out; | |
512 | } | |
513 | ||
514 | if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ)) | |
515 | sx9500_push_events(indio_dev); | |
516 | ||
517 | if (val & SX9500_CONVDONE_IRQ) | |
518 | complete_all(&data->completion); | |
4193c0f1 VD |
519 | |
520 | out: | |
521 | mutex_unlock(&data->mutex); | |
522 | ||
523 | return IRQ_HANDLED; | |
524 | } | |
525 | ||
526 | static int sx9500_read_event_config(struct iio_dev *indio_dev, | |
527 | const struct iio_chan_spec *chan, | |
528 | enum iio_event_type type, | |
529 | enum iio_event_direction dir) | |
530 | { | |
531 | struct sx9500_data *data = iio_priv(indio_dev); | |
532 | ||
533 | if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || | |
534 | dir != IIO_EV_DIR_EITHER) | |
535 | return -EINVAL; | |
536 | ||
537 | return data->event_enabled[chan->channel]; | |
538 | } | |
539 | ||
540 | static int sx9500_write_event_config(struct iio_dev *indio_dev, | |
541 | const struct iio_chan_spec *chan, | |
542 | enum iio_event_type type, | |
543 | enum iio_event_direction dir, | |
544 | int state) | |
545 | { | |
546 | struct sx9500_data *data = iio_priv(indio_dev); | |
59bd0427 | 547 | int ret; |
4193c0f1 VD |
548 | |
549 | if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || | |
550 | dir != IIO_EV_DIR_EITHER) | |
551 | return -EINVAL; | |
552 | ||
553 | mutex_lock(&data->mutex); | |
554 | ||
59bd0427 VD |
555 | if (state == 1) { |
556 | ret = sx9500_inc_chan_users(data, chan->channel); | |
557 | if (ret < 0) | |
558 | goto out_unlock; | |
559 | ret = sx9500_inc_close_far_users(data); | |
560 | if (ret < 0) | |
561 | goto out_undo_chan; | |
562 | } else { | |
563 | ret = sx9500_dec_chan_users(data, chan->channel); | |
564 | if (ret < 0) | |
565 | goto out_unlock; | |
566 | ret = sx9500_dec_close_far_users(data); | |
567 | if (ret < 0) | |
568 | goto out_undo_chan; | |
569 | } | |
4193c0f1 | 570 | |
59bd0427 VD |
571 | data->event_enabled[chan->channel] = state; |
572 | goto out_unlock; | |
4193c0f1 | 573 | |
59bd0427 VD |
574 | out_undo_chan: |
575 | if (state == 1) | |
576 | sx9500_dec_chan_users(data, chan->channel); | |
4193c0f1 | 577 | else |
59bd0427 VD |
578 | sx9500_inc_chan_users(data, chan->channel); |
579 | out_unlock: | |
4193c0f1 | 580 | mutex_unlock(&data->mutex); |
4193c0f1 VD |
581 | return ret; |
582 | } | |
583 | ||
584 | static int sx9500_update_scan_mode(struct iio_dev *indio_dev, | |
585 | const unsigned long *scan_mask) | |
586 | { | |
587 | struct sx9500_data *data = iio_priv(indio_dev); | |
588 | ||
589 | mutex_lock(&data->mutex); | |
590 | kfree(data->buffer); | |
591 | data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL); | |
592 | mutex_unlock(&data->mutex); | |
593 | ||
594 | if (data->buffer == NULL) | |
595 | return -ENOMEM; | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( | |
601 | "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333"); | |
602 | ||
603 | static struct attribute *sx9500_attributes[] = { | |
604 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | |
605 | NULL, | |
606 | }; | |
607 | ||
608 | static const struct attribute_group sx9500_attribute_group = { | |
609 | .attrs = sx9500_attributes, | |
610 | }; | |
611 | ||
612 | static const struct iio_info sx9500_info = { | |
613 | .driver_module = THIS_MODULE, | |
614 | .attrs = &sx9500_attribute_group, | |
615 | .read_raw = &sx9500_read_raw, | |
616 | .write_raw = &sx9500_write_raw, | |
617 | .read_event_config = &sx9500_read_event_config, | |
618 | .write_event_config = &sx9500_write_event_config, | |
619 | .update_scan_mode = &sx9500_update_scan_mode, | |
620 | }; | |
621 | ||
622 | static int sx9500_set_trigger_state(struct iio_trigger *trig, | |
623 | bool state) | |
624 | { | |
625 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); | |
626 | struct sx9500_data *data = iio_priv(indio_dev); | |
627 | int ret; | |
628 | ||
629 | mutex_lock(&data->mutex); | |
630 | ||
59bd0427 VD |
631 | if (state) |
632 | ret = sx9500_inc_data_rdy_users(data); | |
633 | else | |
634 | ret = sx9500_dec_data_rdy_users(data); | |
635 | if (ret < 0) | |
636 | goto out; | |
637 | ||
638 | data->trigger_enabled = state; | |
4193c0f1 | 639 | |
59bd0427 | 640 | out: |
4193c0f1 VD |
641 | mutex_unlock(&data->mutex); |
642 | ||
643 | return ret; | |
644 | } | |
645 | ||
646 | static const struct iio_trigger_ops sx9500_trigger_ops = { | |
647 | .set_trigger_state = sx9500_set_trigger_state, | |
648 | .owner = THIS_MODULE, | |
649 | }; | |
650 | ||
651 | static irqreturn_t sx9500_trigger_handler(int irq, void *private) | |
652 | { | |
653 | struct iio_poll_func *pf = private; | |
654 | struct iio_dev *indio_dev = pf->indio_dev; | |
655 | struct sx9500_data *data = iio_priv(indio_dev); | |
656 | int val, bit, ret, i = 0; | |
657 | ||
658 | mutex_lock(&data->mutex); | |
659 | ||
70dddeee | 660 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
4193c0f1 | 661 | indio_dev->masklength) { |
59bd0427 | 662 | ret = sx9500_read_prox_data(data, &indio_dev->channels[bit], |
4193c0f1 VD |
663 | &val); |
664 | if (ret < 0) | |
665 | goto out; | |
666 | ||
667 | data->buffer[i++] = val; | |
668 | } | |
669 | ||
670 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, | |
671 | iio_get_time_ns()); | |
672 | ||
673 | out: | |
674 | mutex_unlock(&data->mutex); | |
675 | ||
676 | iio_trigger_notify_done(indio_dev->trig); | |
677 | ||
678 | return IRQ_HANDLED; | |
679 | } | |
680 | ||
59bd0427 VD |
681 | static int sx9500_buffer_preenable(struct iio_dev *indio_dev) |
682 | { | |
683 | struct sx9500_data *data = iio_priv(indio_dev); | |
684 | int ret, i; | |
685 | ||
686 | mutex_lock(&data->mutex); | |
687 | ||
688 | for (i = 0; i < SX9500_NUM_CHANNELS; i++) | |
689 | if (test_bit(i, indio_dev->active_scan_mask)) { | |
690 | ret = sx9500_inc_chan_users(data, i); | |
691 | if (ret) | |
692 | break; | |
693 | } | |
694 | ||
695 | if (ret) | |
696 | for (i = i - 1; i >= 0; i--) | |
697 | if (test_bit(i, indio_dev->active_scan_mask)) | |
698 | sx9500_dec_chan_users(data, i); | |
699 | ||
700 | mutex_unlock(&data->mutex); | |
701 | ||
702 | return ret; | |
703 | } | |
704 | ||
705 | static int sx9500_buffer_predisable(struct iio_dev *indio_dev) | |
706 | { | |
707 | struct sx9500_data *data = iio_priv(indio_dev); | |
708 | int ret, i; | |
709 | ||
710 | iio_triggered_buffer_predisable(indio_dev); | |
711 | ||
712 | mutex_lock(&data->mutex); | |
713 | ||
714 | for (i = 0; i < SX9500_NUM_CHANNELS; i++) | |
715 | if (test_bit(i, indio_dev->active_scan_mask)) { | |
716 | ret = sx9500_dec_chan_users(data, i); | |
717 | if (ret) | |
718 | break; | |
719 | } | |
720 | ||
721 | if (ret) | |
722 | for (i = i - 1; i >= 0; i--) | |
723 | if (test_bit(i, indio_dev->active_scan_mask)) | |
724 | sx9500_inc_chan_users(data, i); | |
725 | ||
726 | mutex_unlock(&data->mutex); | |
727 | ||
728 | return ret; | |
729 | } | |
730 | ||
731 | static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = { | |
732 | .preenable = sx9500_buffer_preenable, | |
733 | .postenable = iio_triggered_buffer_postenable, | |
734 | .predisable = sx9500_buffer_predisable, | |
735 | }; | |
736 | ||
4193c0f1 VD |
737 | struct sx9500_reg_default { |
738 | u8 reg; | |
739 | u8 def; | |
740 | }; | |
741 | ||
742 | static const struct sx9500_reg_default sx9500_default_regs[] = { | |
743 | { | |
744 | .reg = SX9500_REG_PROX_CTRL1, | |
745 | /* Shield enabled, small range. */ | |
746 | .def = 0x43, | |
747 | }, | |
748 | { | |
749 | .reg = SX9500_REG_PROX_CTRL2, | |
750 | /* x8 gain, 167kHz frequency, finest resolution. */ | |
751 | .def = 0x77, | |
752 | }, | |
753 | { | |
754 | .reg = SX9500_REG_PROX_CTRL3, | |
755 | /* Doze enabled, 2x scan period doze, no raw filter. */ | |
756 | .def = 0x40, | |
757 | }, | |
758 | { | |
759 | .reg = SX9500_REG_PROX_CTRL4, | |
760 | /* Average threshold. */ | |
761 | .def = 0x30, | |
762 | }, | |
763 | { | |
764 | .reg = SX9500_REG_PROX_CTRL5, | |
765 | /* | |
766 | * Debouncer off, lowest average negative filter, | |
767 | * highest average postive filter. | |
768 | */ | |
769 | .def = 0x0f, | |
770 | }, | |
771 | { | |
772 | .reg = SX9500_REG_PROX_CTRL6, | |
773 | /* Proximity detection threshold: 280 */ | |
774 | .def = 0x0e, | |
775 | }, | |
776 | { | |
777 | .reg = SX9500_REG_PROX_CTRL7, | |
778 | /* | |
779 | * No automatic compensation, compensate each pin | |
780 | * independently, proximity hysteresis: 32, close | |
781 | * debouncer off, far debouncer off. | |
782 | */ | |
783 | .def = 0x00, | |
784 | }, | |
785 | { | |
786 | .reg = SX9500_REG_PROX_CTRL8, | |
787 | /* No stuck timeout, no periodic compensation. */ | |
788 | .def = 0x00, | |
789 | }, | |
790 | { | |
791 | .reg = SX9500_REG_PROX_CTRL0, | |
59bd0427 VD |
792 | /* Scan period: 30ms, all sensors disabled. */ |
793 | .def = 0x00, | |
4193c0f1 VD |
794 | }, |
795 | }; | |
796 | ||
59bd0427 VD |
797 | /* Activate all channels and perform an initial compensation. */ |
798 | static int sx9500_init_compensation(struct iio_dev *indio_dev) | |
799 | { | |
800 | struct sx9500_data *data = iio_priv(indio_dev); | |
801 | int i, ret; | |
802 | unsigned int val; | |
803 | ||
804 | ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, | |
805 | GENMASK(SX9500_NUM_CHANNELS, 0), | |
806 | GENMASK(SX9500_NUM_CHANNELS, 0)); | |
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 | GENMASK(SX9500_NUM_CHANNELS, 0), 0); | |
827 | return ret; | |
828 | } | |
829 | ||
4193c0f1 VD |
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 | ||
45fd5f8e VD |
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 | ||
4193c0f1 VD |
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 | ||
1a30295a | 864 | return sx9500_init_compensation(indio_dev); |
4193c0f1 VD |
865 | } |
866 | ||
821ace29 VD |
867 | static void sx9500_gpio_probe(struct i2c_client *client, |
868 | struct sx9500_data *data) | |
4193c0f1 VD |
869 | { |
870 | struct device *dev; | |
871 | struct gpio_desc *gpio; | |
4193c0f1 VD |
872 | |
873 | if (!client) | |
821ace29 | 874 | return; |
4193c0f1 VD |
875 | |
876 | dev = &client->dev; | |
877 | ||
821ace29 VD |
878 | if (client->irq <= 0) { |
879 | gpio = devm_gpiod_get_index(dev, SX9500_GPIO_INT, 0, GPIOD_IN); | |
880 | if (IS_ERR(gpio)) | |
881 | dev_err(dev, "gpio get irq failed\n"); | |
882 | else | |
883 | client->irq = gpiod_to_irq(gpio); | |
4193c0f1 | 884 | } |
45fd5f8e VD |
885 | |
886 | data->gpiod_rst = devm_gpiod_get_index(dev, SX9500_GPIO_RESET, | |
887 | 0, GPIOD_OUT_HIGH); | |
888 | if (IS_ERR(data->gpiod_rst)) { | |
889 | dev_warn(dev, "gpio get reset pin failed\n"); | |
890 | data->gpiod_rst = NULL; | |
891 | } | |
4193c0f1 VD |
892 | } |
893 | ||
894 | static int sx9500_probe(struct i2c_client *client, | |
895 | const struct i2c_device_id *id) | |
896 | { | |
897 | int ret; | |
898 | struct iio_dev *indio_dev; | |
899 | struct sx9500_data *data; | |
900 | ||
901 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
902 | if (indio_dev == NULL) | |
903 | return -ENOMEM; | |
904 | ||
905 | data = iio_priv(indio_dev); | |
906 | data->client = client; | |
907 | mutex_init(&data->mutex); | |
59bd0427 | 908 | init_completion(&data->completion); |
4193c0f1 VD |
909 | data->trigger_enabled = false; |
910 | ||
911 | data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config); | |
912 | if (IS_ERR(data->regmap)) | |
913 | return PTR_ERR(data->regmap); | |
914 | ||
4193c0f1 VD |
915 | indio_dev->dev.parent = &client->dev; |
916 | indio_dev->name = SX9500_DRIVER_NAME; | |
917 | indio_dev->channels = sx9500_channels; | |
918 | indio_dev->num_channels = ARRAY_SIZE(sx9500_channels); | |
919 | indio_dev->info = &sx9500_info; | |
920 | indio_dev->modes = INDIO_DIRECT_MODE; | |
921 | i2c_set_clientdata(client, indio_dev); | |
922 | ||
821ace29 | 923 | sx9500_gpio_probe(client, data); |
4193c0f1 | 924 | |
45fd5f8e VD |
925 | ret = sx9500_init_device(indio_dev); |
926 | if (ret < 0) | |
927 | return ret; | |
928 | ||
59bd0427 VD |
929 | if (client->irq <= 0) |
930 | dev_warn(&client->dev, "no valid irq found\n"); | |
931 | else { | |
4193c0f1 VD |
932 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
933 | sx9500_irq_handler, sx9500_irq_thread_handler, | |
934 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, | |
935 | SX9500_IRQ_NAME, indio_dev); | |
936 | if (ret < 0) | |
937 | return ret; | |
938 | ||
939 | data->trig = devm_iio_trigger_alloc(&client->dev, | |
940 | "%s-dev%d", indio_dev->name, indio_dev->id); | |
941 | if (!data->trig) | |
942 | return -ENOMEM; | |
943 | ||
944 | data->trig->dev.parent = &client->dev; | |
945 | data->trig->ops = &sx9500_trigger_ops; | |
946 | iio_trigger_set_drvdata(data->trig, indio_dev); | |
947 | ||
948 | ret = iio_trigger_register(data->trig); | |
949 | if (ret) | |
950 | return ret; | |
951 | } | |
952 | ||
953 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | |
59bd0427 VD |
954 | sx9500_trigger_handler, |
955 | &sx9500_buffer_setup_ops); | |
4193c0f1 VD |
956 | if (ret < 0) |
957 | goto out_trigger_unregister; | |
958 | ||
959 | ret = iio_device_register(indio_dev); | |
960 | if (ret < 0) | |
961 | goto out_buffer_cleanup; | |
962 | ||
963 | return 0; | |
964 | ||
965 | out_buffer_cleanup: | |
966 | iio_triggered_buffer_cleanup(indio_dev); | |
967 | out_trigger_unregister: | |
968 | if (client->irq > 0) | |
969 | iio_trigger_unregister(data->trig); | |
970 | ||
971 | return ret; | |
972 | } | |
973 | ||
974 | static int sx9500_remove(struct i2c_client *client) | |
975 | { | |
976 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
977 | struct sx9500_data *data = iio_priv(indio_dev); | |
978 | ||
979 | iio_device_unregister(indio_dev); | |
980 | iio_triggered_buffer_cleanup(indio_dev); | |
981 | if (client->irq > 0) | |
982 | iio_trigger_unregister(data->trig); | |
983 | kfree(data->buffer); | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
7840ffee VD |
988 | #ifdef CONFIG_PM_SLEEP |
989 | static int sx9500_suspend(struct device *dev) | |
990 | { | |
991 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
992 | struct sx9500_data *data = iio_priv(indio_dev); | |
993 | int ret; | |
994 | ||
995 | mutex_lock(&data->mutex); | |
996 | ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, | |
997 | &data->suspend_ctrl0); | |
998 | if (ret < 0) | |
999 | goto out; | |
1000 | ||
1001 | /* | |
1002 | * Scan period doesn't matter because when all the sensors are | |
1003 | * deactivated the device is in sleep mode. | |
1004 | */ | |
1005 | ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0); | |
1006 | ||
1007 | out: | |
1008 | mutex_unlock(&data->mutex); | |
1009 | return ret; | |
1010 | } | |
1011 | ||
1012 | static int sx9500_resume(struct device *dev) | |
1013 | { | |
1014 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | |
1015 | struct sx9500_data *data = iio_priv(indio_dev); | |
1016 | int ret; | |
1017 | ||
1018 | mutex_lock(&data->mutex); | |
1019 | ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, | |
1020 | data->suspend_ctrl0); | |
1021 | mutex_unlock(&data->mutex); | |
1022 | ||
1023 | return ret; | |
1024 | } | |
1025 | #endif /* CONFIG_PM_SLEEP */ | |
1026 | ||
1027 | static const struct dev_pm_ops sx9500_pm_ops = { | |
1028 | SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume) | |
1029 | }; | |
1030 | ||
4193c0f1 VD |
1031 | static const struct acpi_device_id sx9500_acpi_match[] = { |
1032 | {"SSX9500", 0}, | |
1033 | { }, | |
1034 | }; | |
1035 | MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match); | |
1036 | ||
1037 | static const struct i2c_device_id sx9500_id[] = { | |
1038 | {"sx9500", 0}, | |
a40c0ac1 | 1039 | { }, |
4193c0f1 VD |
1040 | }; |
1041 | MODULE_DEVICE_TABLE(i2c, sx9500_id); | |
1042 | ||
1043 | static struct i2c_driver sx9500_driver = { | |
1044 | .driver = { | |
1045 | .name = SX9500_DRIVER_NAME, | |
1046 | .acpi_match_table = ACPI_PTR(sx9500_acpi_match), | |
7840ffee | 1047 | .pm = &sx9500_pm_ops, |
4193c0f1 VD |
1048 | }, |
1049 | .probe = sx9500_probe, | |
1050 | .remove = sx9500_remove, | |
1051 | .id_table = sx9500_id, | |
1052 | }; | |
1053 | module_i2c_driver(sx9500_driver); | |
1054 | ||
1055 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); | |
1056 | MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor"); | |
1057 | MODULE_LICENSE("GPL v2"); |