Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / iio / proximity / isl29501.c
blobdc66ca9bba6b4676bebaa7f6a7582d9667cfc70a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * isl29501.c: ISL29501 Time of Flight sensor driver.
5 * Copyright (C) 2018
6 * Author: Mathieu Othacehe <m.othacehe@gmail.com>
8 * 7-bit I2C slave address: 0x57
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/err.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/triggered_buffer.h>
23 /* Control, setting and status registers */
24 #define ISL29501_DEVICE_ID 0x00
25 #define ISL29501_ID 0x0A
27 /* Sampling control registers */
28 #define ISL29501_INTEGRATION_PERIOD 0x10
29 #define ISL29501_SAMPLE_PERIOD 0x11
31 /* Closed loop calibration registers */
32 #define ISL29501_CROSSTALK_I_MSB 0x24
33 #define ISL29501_CROSSTALK_I_LSB 0x25
34 #define ISL29501_CROSSTALK_I_EXPONENT 0x26
35 #define ISL29501_CROSSTALK_Q_MSB 0x27
36 #define ISL29501_CROSSTALK_Q_LSB 0x28
37 #define ISL29501_CROSSTALK_Q_EXPONENT 0x29
38 #define ISL29501_CROSSTALK_GAIN_MSB 0x2A
39 #define ISL29501_CROSSTALK_GAIN_LSB 0x2B
40 #define ISL29501_MAGNITUDE_REF_EXP 0x2C
41 #define ISL29501_MAGNITUDE_REF_MSB 0x2D
42 #define ISL29501_MAGNITUDE_REF_LSB 0x2E
43 #define ISL29501_PHASE_OFFSET_MSB 0x2F
44 #define ISL29501_PHASE_OFFSET_LSB 0x30
46 /* Analog control registers */
47 #define ISL29501_DRIVER_RANGE 0x90
48 #define ISL29501_EMITTER_DAC 0x91
50 #define ISL29501_COMMAND_REGISTER 0xB0
52 /* Commands */
53 #define ISL29501_EMUL_SAMPLE_START_PIN 0x49
54 #define ISL29501_RESET_ALL_REGISTERS 0xD7
55 #define ISL29501_RESET_INT_SM 0xD1
57 /* Ambiant light and temperature corrections */
58 #define ISL29501_TEMP_REFERENCE 0x31
59 #define ISL29501_PHASE_EXPONENT 0x33
60 #define ISL29501_TEMP_COEFF_A 0x34
61 #define ISL29501_TEMP_COEFF_B 0x39
62 #define ISL29501_AMBIANT_COEFF_A 0x36
63 #define ISL29501_AMBIANT_COEFF_B 0x3B
65 /* Data output registers */
66 #define ISL29501_DISTANCE_MSB_DATA 0xD1
67 #define ISL29501_DISTANCE_LSB_DATA 0xD2
68 #define ISL29501_PRECISION_MSB 0xD3
69 #define ISL29501_PRECISION_LSB 0xD4
70 #define ISL29501_MAGNITUDE_EXPONENT 0xD5
71 #define ISL29501_MAGNITUDE_MSB 0xD6
72 #define ISL29501_MAGNITUDE_LSB 0xD7
73 #define ISL29501_PHASE_MSB 0xD8
74 #define ISL29501_PHASE_LSB 0xD9
75 #define ISL29501_I_RAW_EXPONENT 0xDA
76 #define ISL29501_I_RAW_MSB 0xDB
77 #define ISL29501_I_RAW_LSB 0xDC
78 #define ISL29501_Q_RAW_EXPONENT 0xDD
79 #define ISL29501_Q_RAW_MSB 0xDE
80 #define ISL29501_Q_RAW_LSB 0xDF
81 #define ISL29501_DIE_TEMPERATURE 0xE2
82 #define ISL29501_AMBIENT_LIGHT 0xE3
83 #define ISL29501_GAIN_MSB 0xE6
84 #define ISL29501_GAIN_LSB 0xE7
86 #define ISL29501_MAX_EXP_VAL 15
88 #define ISL29501_INT_TIME_AVAILABLE \
89 "0.00007 0.00014 0.00028 0.00057 0.00114 " \
90 "0.00228 0.00455 0.00910 0.01820 0.03640 " \
91 "0.07281 0.14561"
93 #define ISL29501_CURRENT_SCALE_AVAILABLE \
94 "0.0039 0.0078 0.0118 0.0157 0.0196 " \
95 "0.0235 0.0275 0.0314 0.0352 0.0392 " \
96 "0.0431 0.0471 0.0510 0.0549 0.0588"
98 enum isl29501_correction_coeff {
99 COEFF_TEMP_A,
100 COEFF_TEMP_B,
101 COEFF_LIGHT_A,
102 COEFF_LIGHT_B,
103 COEFF_MAX,
106 struct isl29501_private {
107 struct i2c_client *client;
108 struct mutex lock;
109 /* Exact representation of correction coefficients. */
110 unsigned int shadow_coeffs[COEFF_MAX];
113 enum isl29501_register_name {
114 REG_DISTANCE,
115 REG_PHASE,
116 REG_TEMPERATURE,
117 REG_AMBIENT_LIGHT,
118 REG_GAIN,
119 REG_GAIN_BIAS,
120 REG_PHASE_EXP,
121 REG_CALIB_PHASE_TEMP_A,
122 REG_CALIB_PHASE_TEMP_B,
123 REG_CALIB_PHASE_LIGHT_A,
124 REG_CALIB_PHASE_LIGHT_B,
125 REG_DISTANCE_BIAS,
126 REG_TEMPERATURE_BIAS,
127 REG_INT_TIME,
128 REG_SAMPLE_TIME,
129 REG_DRIVER_RANGE,
130 REG_EMITTER_DAC,
133 struct isl29501_register_desc {
134 u8 msb;
135 u8 lsb;
138 static const struct isl29501_register_desc isl29501_registers[] = {
139 [REG_DISTANCE] = {
140 .msb = ISL29501_DISTANCE_MSB_DATA,
141 .lsb = ISL29501_DISTANCE_LSB_DATA,
143 [REG_PHASE] = {
144 .msb = ISL29501_PHASE_MSB,
145 .lsb = ISL29501_PHASE_LSB,
147 [REG_TEMPERATURE] = {
148 .lsb = ISL29501_DIE_TEMPERATURE,
150 [REG_AMBIENT_LIGHT] = {
151 .lsb = ISL29501_AMBIENT_LIGHT,
153 [REG_GAIN] = {
154 .msb = ISL29501_GAIN_MSB,
155 .lsb = ISL29501_GAIN_LSB,
157 [REG_GAIN_BIAS] = {
158 .msb = ISL29501_CROSSTALK_GAIN_MSB,
159 .lsb = ISL29501_CROSSTALK_GAIN_LSB,
161 [REG_PHASE_EXP] = {
162 .lsb = ISL29501_PHASE_EXPONENT,
164 [REG_CALIB_PHASE_TEMP_A] = {
165 .lsb = ISL29501_TEMP_COEFF_A,
167 [REG_CALIB_PHASE_TEMP_B] = {
168 .lsb = ISL29501_TEMP_COEFF_B,
170 [REG_CALIB_PHASE_LIGHT_A] = {
171 .lsb = ISL29501_AMBIANT_COEFF_A,
173 [REG_CALIB_PHASE_LIGHT_B] = {
174 .lsb = ISL29501_AMBIANT_COEFF_B,
176 [REG_DISTANCE_BIAS] = {
177 .msb = ISL29501_PHASE_OFFSET_MSB,
178 .lsb = ISL29501_PHASE_OFFSET_LSB,
180 [REG_TEMPERATURE_BIAS] = {
181 .lsb = ISL29501_TEMP_REFERENCE,
183 [REG_INT_TIME] = {
184 .lsb = ISL29501_INTEGRATION_PERIOD,
186 [REG_SAMPLE_TIME] = {
187 .lsb = ISL29501_SAMPLE_PERIOD,
189 [REG_DRIVER_RANGE] = {
190 .lsb = ISL29501_DRIVER_RANGE,
192 [REG_EMITTER_DAC] = {
193 .lsb = ISL29501_EMITTER_DAC,
197 static int isl29501_register_read(struct isl29501_private *isl29501,
198 enum isl29501_register_name name,
199 u32 *val)
201 const struct isl29501_register_desc *reg = &isl29501_registers[name];
202 u8 msb = 0, lsb = 0;
203 s32 ret;
205 mutex_lock(&isl29501->lock);
206 if (reg->msb) {
207 ret = i2c_smbus_read_byte_data(isl29501->client, reg->msb);
208 if (ret < 0)
209 goto err;
210 msb = ret;
213 if (reg->lsb) {
214 ret = i2c_smbus_read_byte_data(isl29501->client, reg->lsb);
215 if (ret < 0)
216 goto err;
217 lsb = ret;
219 mutex_unlock(&isl29501->lock);
221 *val = (msb << 8) + lsb;
223 return 0;
224 err:
225 mutex_unlock(&isl29501->lock);
227 return ret;
230 static u32 isl29501_register_write(struct isl29501_private *isl29501,
231 enum isl29501_register_name name,
232 u32 value)
234 const struct isl29501_register_desc *reg = &isl29501_registers[name];
235 int ret;
237 if (!reg->msb && value > U8_MAX)
238 return -ERANGE;
240 if (value > U16_MAX)
241 return -ERANGE;
243 mutex_lock(&isl29501->lock);
244 if (reg->msb) {
245 ret = i2c_smbus_write_byte_data(isl29501->client,
246 reg->msb, value >> 8);
247 if (ret < 0)
248 goto err;
251 ret = i2c_smbus_write_byte_data(isl29501->client, reg->lsb, value);
253 err:
254 mutex_unlock(&isl29501->lock);
255 return ret;
258 static ssize_t isl29501_read_ext(struct iio_dev *indio_dev,
259 uintptr_t private,
260 const struct iio_chan_spec *chan,
261 char *buf)
263 struct isl29501_private *isl29501 = iio_priv(indio_dev);
264 enum isl29501_register_name reg = private;
265 int ret;
266 u32 value, gain, coeff, exp;
268 switch (reg) {
269 case REG_GAIN:
270 case REG_GAIN_BIAS:
271 ret = isl29501_register_read(isl29501, reg, &gain);
272 if (ret < 0)
273 return ret;
275 value = gain;
276 break;
277 case REG_CALIB_PHASE_TEMP_A:
278 case REG_CALIB_PHASE_TEMP_B:
279 case REG_CALIB_PHASE_LIGHT_A:
280 case REG_CALIB_PHASE_LIGHT_B:
281 ret = isl29501_register_read(isl29501, REG_PHASE_EXP, &exp);
282 if (ret < 0)
283 return ret;
285 ret = isl29501_register_read(isl29501, reg, &coeff);
286 if (ret < 0)
287 return ret;
289 value = coeff << exp;
290 break;
291 default:
292 return -EINVAL;
295 return sprintf(buf, "%u\n", value);
298 static int isl29501_set_shadow_coeff(struct isl29501_private *isl29501,
299 enum isl29501_register_name reg,
300 unsigned int val)
302 enum isl29501_correction_coeff coeff;
304 switch (reg) {
305 case REG_CALIB_PHASE_TEMP_A:
306 coeff = COEFF_TEMP_A;
307 break;
308 case REG_CALIB_PHASE_TEMP_B:
309 coeff = COEFF_TEMP_B;
310 break;
311 case REG_CALIB_PHASE_LIGHT_A:
312 coeff = COEFF_LIGHT_A;
313 break;
314 case REG_CALIB_PHASE_LIGHT_B:
315 coeff = COEFF_LIGHT_B;
316 break;
317 default:
318 return -EINVAL;
320 isl29501->shadow_coeffs[coeff] = val;
322 return 0;
325 static int isl29501_write_coeff(struct isl29501_private *isl29501,
326 enum isl29501_correction_coeff coeff,
327 int val)
329 enum isl29501_register_name reg;
331 switch (coeff) {
332 case COEFF_TEMP_A:
333 reg = REG_CALIB_PHASE_TEMP_A;
334 break;
335 case COEFF_TEMP_B:
336 reg = REG_CALIB_PHASE_TEMP_B;
337 break;
338 case COEFF_LIGHT_A:
339 reg = REG_CALIB_PHASE_LIGHT_A;
340 break;
341 case COEFF_LIGHT_B:
342 reg = REG_CALIB_PHASE_LIGHT_B;
343 break;
344 default:
345 return -EINVAL;
348 return isl29501_register_write(isl29501, reg, val);
351 static unsigned int isl29501_find_corr_exp(unsigned int val,
352 unsigned int max_exp,
353 unsigned int max_mantissa)
355 unsigned int exp = 1;
358 * Correction coefficients are represented under
359 * mantissa * 2^exponent form, where mantissa and exponent
360 * are stored in two separate registers of the sensor.
362 * Compute and return the lowest exponent such as:
363 * mantissa = value / 2^exponent
365 * where mantissa < max_mantissa.
367 if (val <= max_mantissa)
368 return 0;
370 while ((val >> exp) > max_mantissa) {
371 exp++;
373 if (exp > max_exp)
374 return max_exp;
377 return exp;
380 static ssize_t isl29501_write_ext(struct iio_dev *indio_dev,
381 uintptr_t private,
382 const struct iio_chan_spec *chan,
383 const char *buf, size_t len)
385 struct isl29501_private *isl29501 = iio_priv(indio_dev);
386 enum isl29501_register_name reg = private;
387 unsigned int val;
388 int max_exp = 0;
389 int ret;
390 int i;
392 ret = kstrtouint(buf, 10, &val);
393 if (ret)
394 return ret;
396 switch (reg) {
397 case REG_GAIN_BIAS:
398 if (val > U16_MAX)
399 return -ERANGE;
401 ret = isl29501_register_write(isl29501, reg, val);
402 if (ret < 0)
403 return ret;
405 break;
406 case REG_CALIB_PHASE_TEMP_A:
407 case REG_CALIB_PHASE_TEMP_B:
408 case REG_CALIB_PHASE_LIGHT_A:
409 case REG_CALIB_PHASE_LIGHT_B:
411 if (val > (U8_MAX << ISL29501_MAX_EXP_VAL))
412 return -ERANGE;
414 /* Store the correction coefficient under its exact form. */
415 ret = isl29501_set_shadow_coeff(isl29501, reg, val);
416 if (ret < 0)
417 return ret;
420 * Find the highest exponent needed to represent
421 * correction coefficients.
423 for (i = 0; i < COEFF_MAX; i++) {
424 int corr;
425 int corr_exp;
427 corr = isl29501->shadow_coeffs[i];
428 corr_exp = isl29501_find_corr_exp(corr,
429 ISL29501_MAX_EXP_VAL,
430 U8_MAX / 2);
431 dev_dbg(&isl29501->client->dev,
432 "found exp of corr(%d) = %d\n", corr, corr_exp);
434 max_exp = max(max_exp, corr_exp);
438 * Represent every correction coefficient under
439 * mantissa * 2^max_exponent form and force the
440 * writing of those coefficients on the sensor.
442 for (i = 0; i < COEFF_MAX; i++) {
443 int corr;
444 int mantissa;
446 corr = isl29501->shadow_coeffs[i];
447 if (!corr)
448 continue;
450 mantissa = corr >> max_exp;
452 ret = isl29501_write_coeff(isl29501, i, mantissa);
453 if (ret < 0)
454 return ret;
457 ret = isl29501_register_write(isl29501, REG_PHASE_EXP, max_exp);
458 if (ret < 0)
459 return ret;
461 break;
462 default:
463 return -EINVAL;
466 return len;
469 #define _ISL29501_EXT_INFO(_name, _ident) { \
470 .name = _name, \
471 .read = isl29501_read_ext, \
472 .write = isl29501_write_ext, \
473 .private = _ident, \
474 .shared = IIO_SEPARATE, \
477 static const struct iio_chan_spec_ext_info isl29501_ext_info[] = {
478 _ISL29501_EXT_INFO("agc_gain", REG_GAIN),
479 _ISL29501_EXT_INFO("agc_gain_bias", REG_GAIN_BIAS),
480 _ISL29501_EXT_INFO("calib_phase_temp_a", REG_CALIB_PHASE_TEMP_A),
481 _ISL29501_EXT_INFO("calib_phase_temp_b", REG_CALIB_PHASE_TEMP_B),
482 _ISL29501_EXT_INFO("calib_phase_light_a", REG_CALIB_PHASE_LIGHT_A),
483 _ISL29501_EXT_INFO("calib_phase_light_b", REG_CALIB_PHASE_LIGHT_B),
484 { },
487 #define ISL29501_DISTANCE_SCAN_INDEX 0
488 #define ISL29501_TIMESTAMP_SCAN_INDEX 1
490 static const struct iio_chan_spec isl29501_channels[] = {
492 .type = IIO_PROXIMITY,
493 .scan_index = ISL29501_DISTANCE_SCAN_INDEX,
494 .info_mask_separate =
495 BIT(IIO_CHAN_INFO_RAW) |
496 BIT(IIO_CHAN_INFO_SCALE) |
497 BIT(IIO_CHAN_INFO_CALIBBIAS),
498 .scan_type = {
499 .sign = 'u',
500 .realbits = 16,
501 .storagebits = 16,
502 .endianness = IIO_CPU,
504 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
505 BIT(IIO_CHAN_INFO_SAMP_FREQ),
506 .ext_info = isl29501_ext_info,
509 .type = IIO_PHASE,
510 .scan_index = -1,
511 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
512 BIT(IIO_CHAN_INFO_SCALE),
515 .type = IIO_CURRENT,
516 .scan_index = -1,
517 .output = 1,
518 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
519 BIT(IIO_CHAN_INFO_SCALE),
522 .type = IIO_TEMP,
523 .scan_index = -1,
524 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
525 BIT(IIO_CHAN_INFO_SCALE) |
526 BIT(IIO_CHAN_INFO_CALIBBIAS),
529 .type = IIO_INTENSITY,
530 .scan_index = -1,
531 .modified = 1,
532 .channel2 = IIO_MOD_LIGHT_CLEAR,
533 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
534 BIT(IIO_CHAN_INFO_SCALE),
536 IIO_CHAN_SOFT_TIMESTAMP(ISL29501_TIMESTAMP_SCAN_INDEX),
539 static int isl29501_reset_registers(struct isl29501_private *isl29501)
541 int ret;
543 ret = i2c_smbus_write_byte_data(isl29501->client,
544 ISL29501_COMMAND_REGISTER,
545 ISL29501_RESET_ALL_REGISTERS);
546 if (ret < 0) {
547 dev_err(&isl29501->client->dev,
548 "cannot reset registers %d\n", ret);
549 return ret;
552 ret = i2c_smbus_write_byte_data(isl29501->client,
553 ISL29501_COMMAND_REGISTER,
554 ISL29501_RESET_INT_SM);
555 if (ret < 0)
556 dev_err(&isl29501->client->dev,
557 "cannot reset state machine %d\n", ret);
559 return ret;
562 static int isl29501_begin_acquisition(struct isl29501_private *isl29501)
564 int ret;
566 ret = i2c_smbus_write_byte_data(isl29501->client,
567 ISL29501_COMMAND_REGISTER,
568 ISL29501_EMUL_SAMPLE_START_PIN);
569 if (ret < 0)
570 dev_err(&isl29501->client->dev,
571 "cannot begin acquisition %d\n", ret);
573 return ret;
576 static IIO_CONST_ATTR_INT_TIME_AVAIL(ISL29501_INT_TIME_AVAILABLE);
577 static IIO_CONST_ATTR(out_current_scale_available,
578 ISL29501_CURRENT_SCALE_AVAILABLE);
580 static struct attribute *isl29501_attributes[] = {
581 &iio_const_attr_integration_time_available.dev_attr.attr,
582 &iio_const_attr_out_current_scale_available.dev_attr.attr,
583 NULL
586 static const struct attribute_group isl29501_attribute_group = {
587 .attrs = isl29501_attributes,
590 static const int isl29501_current_scale_table[][2] = {
591 {0, 3900}, {0, 7800}, {0, 11800}, {0, 15700},
592 {0, 19600}, {0, 23500}, {0, 27500}, {0, 31400},
593 {0, 35200}, {0, 39200}, {0, 43100}, {0, 47100},
594 {0, 51000}, {0, 54900}, {0, 58800},
597 static const int isl29501_int_time[][2] = {
598 {0, 70}, /* 0.07 ms */
599 {0, 140}, /* 0.14 ms */
600 {0, 280}, /* 0.28 ms */
601 {0, 570}, /* 0.57 ms */
602 {0, 1140}, /* 1.14 ms */
603 {0, 2280}, /* 2.28 ms */
604 {0, 4550}, /* 4.55 ms */
605 {0, 9100}, /* 9.11 ms */
606 {0, 18200}, /* 18.2 ms */
607 {0, 36400}, /* 36.4 ms */
608 {0, 72810}, /* 72.81 ms */
609 {0, 145610} /* 145.28 ms */
612 static int isl29501_get_raw(struct isl29501_private *isl29501,
613 const struct iio_chan_spec *chan,
614 int *raw)
616 int ret;
618 switch (chan->type) {
619 case IIO_PROXIMITY:
620 ret = isl29501_register_read(isl29501, REG_DISTANCE, raw);
621 if (ret < 0)
622 return ret;
624 return IIO_VAL_INT;
625 case IIO_INTENSITY:
626 ret = isl29501_register_read(isl29501,
627 REG_AMBIENT_LIGHT,
628 raw);
629 if (ret < 0)
630 return ret;
632 return IIO_VAL_INT;
633 case IIO_PHASE:
634 ret = isl29501_register_read(isl29501, REG_PHASE, raw);
635 if (ret < 0)
636 return ret;
638 return IIO_VAL_INT;
639 case IIO_CURRENT:
640 ret = isl29501_register_read(isl29501, REG_EMITTER_DAC, raw);
641 if (ret < 0)
642 return ret;
644 return IIO_VAL_INT;
645 case IIO_TEMP:
646 ret = isl29501_register_read(isl29501, REG_TEMPERATURE, raw);
647 if (ret < 0)
648 return ret;
650 return IIO_VAL_INT;
651 default:
652 return -EINVAL;
656 static int isl29501_get_scale(struct isl29501_private *isl29501,
657 const struct iio_chan_spec *chan,
658 int *val, int *val2)
660 int ret;
661 u32 current_scale;
663 switch (chan->type) {
664 case IIO_PROXIMITY:
665 /* distance = raw_distance * 33.31 / 65536 (m) */
666 *val = 3331;
667 *val2 = 6553600;
669 return IIO_VAL_FRACTIONAL;
670 case IIO_PHASE:
671 /* phase = raw_phase * 2pi / 65536 (rad) */
672 *val = 0;
673 *val2 = 95874;
675 return IIO_VAL_INT_PLUS_NANO;
676 case IIO_INTENSITY:
677 /* light = raw_light * 35 / 10000 (mA) */
678 *val = 35;
679 *val2 = 10000;
681 return IIO_VAL_FRACTIONAL;
682 case IIO_CURRENT:
683 ret = isl29501_register_read(isl29501,
684 REG_DRIVER_RANGE,
685 &current_scale);
686 if (ret < 0)
687 return ret;
689 if (current_scale > ARRAY_SIZE(isl29501_current_scale_table))
690 return -EINVAL;
692 if (!current_scale) {
693 *val = 0;
694 *val2 = 0;
695 return IIO_VAL_INT;
698 *val = isl29501_current_scale_table[current_scale - 1][0];
699 *val2 = isl29501_current_scale_table[current_scale - 1][1];
701 return IIO_VAL_INT_PLUS_MICRO;
702 case IIO_TEMP:
703 /* temperature = raw_temperature * 125 / 100000 (milli °C) */
704 *val = 125;
705 *val2 = 100000;
707 return IIO_VAL_FRACTIONAL;
708 default:
709 return -EINVAL;
713 static int isl29501_get_calibbias(struct isl29501_private *isl29501,
714 const struct iio_chan_spec *chan,
715 int *bias)
717 switch (chan->type) {
718 case IIO_PROXIMITY:
719 return isl29501_register_read(isl29501,
720 REG_DISTANCE_BIAS,
721 bias);
722 case IIO_TEMP:
723 return isl29501_register_read(isl29501,
724 REG_TEMPERATURE_BIAS,
725 bias);
726 default:
727 return -EINVAL;
731 static int isl29501_get_inttime(struct isl29501_private *isl29501,
732 int *val, int *val2)
734 int ret;
735 u32 inttime;
737 ret = isl29501_register_read(isl29501, REG_INT_TIME, &inttime);
738 if (ret < 0)
739 return ret;
741 if (inttime >= ARRAY_SIZE(isl29501_int_time))
742 return -EINVAL;
744 *val = isl29501_int_time[inttime][0];
745 *val2 = isl29501_int_time[inttime][1];
747 return IIO_VAL_INT_PLUS_MICRO;
750 static int isl29501_get_freq(struct isl29501_private *isl29501,
751 int *val, int *val2)
753 int ret;
754 int sample_time;
755 unsigned long long freq;
756 u32 temp;
758 ret = isl29501_register_read(isl29501, REG_SAMPLE_TIME, &sample_time);
759 if (ret < 0)
760 return ret;
762 /* freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
763 freq = 1000000ULL * 1000000ULL;
765 do_div(freq, 450 * (sample_time + 1));
767 temp = do_div(freq, 1000000);
768 *val = freq;
769 *val2 = temp;
771 return IIO_VAL_INT_PLUS_MICRO;
774 static int isl29501_read_raw(struct iio_dev *indio_dev,
775 struct iio_chan_spec const *chan, int *val,
776 int *val2, long mask)
778 struct isl29501_private *isl29501 = iio_priv(indio_dev);
780 switch (mask) {
781 case IIO_CHAN_INFO_RAW:
782 return isl29501_get_raw(isl29501, chan, val);
783 case IIO_CHAN_INFO_SCALE:
784 return isl29501_get_scale(isl29501, chan, val, val2);
785 case IIO_CHAN_INFO_INT_TIME:
786 return isl29501_get_inttime(isl29501, val, val2);
787 case IIO_CHAN_INFO_SAMP_FREQ:
788 return isl29501_get_freq(isl29501, val, val2);
789 case IIO_CHAN_INFO_CALIBBIAS:
790 return isl29501_get_calibbias(isl29501, chan, val);
791 default:
792 return -EINVAL;
796 static int isl29501_set_raw(struct isl29501_private *isl29501,
797 const struct iio_chan_spec *chan,
798 int raw)
800 switch (chan->type) {
801 case IIO_CURRENT:
802 return isl29501_register_write(isl29501, REG_EMITTER_DAC, raw);
803 default:
804 return -EINVAL;
808 static int isl29501_set_inttime(struct isl29501_private *isl29501,
809 int val, int val2)
811 int i;
813 for (i = 0; i < ARRAY_SIZE(isl29501_int_time); i++) {
814 if (isl29501_int_time[i][0] == val &&
815 isl29501_int_time[i][1] == val2) {
816 return isl29501_register_write(isl29501,
817 REG_INT_TIME,
822 return -EINVAL;
825 static int isl29501_set_scale(struct isl29501_private *isl29501,
826 const struct iio_chan_spec *chan,
827 int val, int val2)
829 int i;
831 if (chan->type != IIO_CURRENT)
832 return -EINVAL;
834 for (i = 0; i < ARRAY_SIZE(isl29501_current_scale_table); i++) {
835 if (isl29501_current_scale_table[i][0] == val &&
836 isl29501_current_scale_table[i][1] == val2) {
837 return isl29501_register_write(isl29501,
838 REG_DRIVER_RANGE,
839 i + 1);
843 return -EINVAL;
846 static int isl29501_set_calibbias(struct isl29501_private *isl29501,
847 const struct iio_chan_spec *chan,
848 int bias)
850 switch (chan->type) {
851 case IIO_PROXIMITY:
852 return isl29501_register_write(isl29501,
853 REG_DISTANCE_BIAS,
854 bias);
855 case IIO_TEMP:
856 return isl29501_register_write(isl29501,
857 REG_TEMPERATURE_BIAS,
858 bias);
859 default:
860 return -EINVAL;
864 static int isl29501_set_freq(struct isl29501_private *isl29501,
865 int val, int val2)
867 int freq;
868 unsigned long long sample_time;
870 /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
871 freq = val * 1000000 + val2 % 1000000;
872 sample_time = 2222ULL * 1000000ULL;
873 do_div(sample_time, freq);
875 sample_time -= 1;
877 if (sample_time > 255)
878 return -ERANGE;
880 return isl29501_register_write(isl29501, REG_SAMPLE_TIME, sample_time);
883 static int isl29501_write_raw(struct iio_dev *indio_dev,
884 struct iio_chan_spec const *chan,
885 int val, int val2, long mask)
887 struct isl29501_private *isl29501 = iio_priv(indio_dev);
889 switch (mask) {
890 case IIO_CHAN_INFO_RAW:
891 return isl29501_set_raw(isl29501, chan, val);
892 case IIO_CHAN_INFO_INT_TIME:
893 return isl29501_set_inttime(isl29501, val, val2);
894 case IIO_CHAN_INFO_SAMP_FREQ:
895 return isl29501_set_freq(isl29501, val, val2);
896 case IIO_CHAN_INFO_SCALE:
897 return isl29501_set_scale(isl29501, chan, val, val2);
898 case IIO_CHAN_INFO_CALIBBIAS:
899 return isl29501_set_calibbias(isl29501, chan, val);
900 default:
901 return -EINVAL;
905 static const struct iio_info isl29501_info = {
906 .read_raw = &isl29501_read_raw,
907 .write_raw = &isl29501_write_raw,
908 .attrs = &isl29501_attribute_group,
911 static int isl29501_init_chip(struct isl29501_private *isl29501)
913 int ret;
915 ret = i2c_smbus_read_byte_data(isl29501->client, ISL29501_DEVICE_ID);
916 if (ret < 0) {
917 dev_err(&isl29501->client->dev, "Error reading device id\n");
918 return ret;
921 if (ret != ISL29501_ID) {
922 dev_err(&isl29501->client->dev,
923 "Wrong chip id, got %x expected %x\n",
924 ret, ISL29501_DEVICE_ID);
925 return -ENODEV;
928 ret = isl29501_reset_registers(isl29501);
929 if (ret < 0)
930 return ret;
932 return isl29501_begin_acquisition(isl29501);
935 static irqreturn_t isl29501_trigger_handler(int irq, void *p)
937 struct iio_poll_func *pf = p;
938 struct iio_dev *indio_dev = pf->indio_dev;
939 struct isl29501_private *isl29501 = iio_priv(indio_dev);
940 const unsigned long *active_mask = indio_dev->active_scan_mask;
941 u32 buffer[4] __aligned(8) = {}; /* 1x16-bit + naturally aligned ts */
943 if (test_bit(ISL29501_DISTANCE_SCAN_INDEX, active_mask))
944 isl29501_register_read(isl29501, REG_DISTANCE, buffer);
946 iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
947 iio_trigger_notify_done(indio_dev->trig);
949 return IRQ_HANDLED;
952 static int isl29501_probe(struct i2c_client *client)
954 struct iio_dev *indio_dev;
955 struct isl29501_private *isl29501;
956 int ret;
958 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*isl29501));
959 if (!indio_dev)
960 return -ENOMEM;
962 isl29501 = iio_priv(indio_dev);
964 i2c_set_clientdata(client, indio_dev);
965 isl29501->client = client;
967 mutex_init(&isl29501->lock);
969 ret = isl29501_init_chip(isl29501);
970 if (ret < 0)
971 return ret;
973 indio_dev->modes = INDIO_DIRECT_MODE;
974 indio_dev->channels = isl29501_channels;
975 indio_dev->num_channels = ARRAY_SIZE(isl29501_channels);
976 indio_dev->name = client->name;
977 indio_dev->info = &isl29501_info;
979 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
980 iio_pollfunc_store_time,
981 isl29501_trigger_handler,
982 NULL);
983 if (ret < 0) {
984 dev_err(&client->dev, "unable to setup iio triggered buffer\n");
985 return ret;
988 return devm_iio_device_register(&client->dev, indio_dev);
991 static const struct i2c_device_id isl29501_id[] = {
992 { "isl29501" },
996 MODULE_DEVICE_TABLE(i2c, isl29501_id);
998 static const struct of_device_id isl29501_i2c_matches[] = {
999 { .compatible = "renesas,isl29501" },
1002 MODULE_DEVICE_TABLE(of, isl29501_i2c_matches);
1004 static struct i2c_driver isl29501_driver = {
1005 .driver = {
1006 .name = "isl29501",
1007 .of_match_table = isl29501_i2c_matches,
1009 .id_table = isl29501_id,
1010 .probe = isl29501_probe,
1012 module_i2c_driver(isl29501_driver);
1014 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1015 MODULE_DESCRIPTION("ISL29501 Time of Flight sensor driver");
1016 MODULE_LICENSE("GPL v2");