1 // SPDX-License-Identifier: GPL-2.0
3 * isl29501.c: ISL29501 Time of Flight sensor driver.
6 * Author: Mathieu Othacehe <m.othacehe@gmail.com>
8 * 7-bit I2C slave address: 0x57
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/err.h>
15 #include <linux/of_device.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
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 " \
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
{
106 struct isl29501_private
{
107 struct i2c_client
*client
;
109 /* Exact representation of correction coefficients. */
110 unsigned int shadow_coeffs
[COEFF_MAX
];
113 enum isl29501_register_name
{
121 REG_CALIB_PHASE_TEMP_A
,
122 REG_CALIB_PHASE_TEMP_B
,
123 REG_CALIB_PHASE_LIGHT_A
,
124 REG_CALIB_PHASE_LIGHT_B
,
126 REG_TEMPERATURE_BIAS
,
133 struct isl29501_register_desc
{
138 static const struct isl29501_register_desc isl29501_registers
[] = {
140 .msb
= ISL29501_DISTANCE_MSB_DATA
,
141 .lsb
= ISL29501_DISTANCE_LSB_DATA
,
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
,
154 .msb
= ISL29501_GAIN_MSB
,
155 .lsb
= ISL29501_GAIN_LSB
,
158 .msb
= ISL29501_CROSSTALK_GAIN_MSB
,
159 .lsb
= ISL29501_CROSSTALK_GAIN_LSB
,
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
,
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
,
201 const struct isl29501_register_desc
*reg
= &isl29501_registers
[name
];
205 mutex_lock(&isl29501
->lock
);
207 ret
= i2c_smbus_read_byte_data(isl29501
->client
, reg
->msb
);
214 ret
= i2c_smbus_read_byte_data(isl29501
->client
, reg
->lsb
);
219 mutex_unlock(&isl29501
->lock
);
221 *val
= (msb
<< 8) + lsb
;
225 mutex_unlock(&isl29501
->lock
);
230 static u32
isl29501_register_write(struct isl29501_private
*isl29501
,
231 enum isl29501_register_name name
,
234 const struct isl29501_register_desc
*reg
= &isl29501_registers
[name
];
238 if (!reg
->msb
&& value
> U8_MAX
)
247 msb
= (value
>> 8) & 0xFF;
251 mutex_lock(&isl29501
->lock
);
253 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
259 ret
= i2c_smbus_write_byte_data(isl29501
->client
, reg
->lsb
, lsb
);
262 mutex_unlock(&isl29501
->lock
);
266 static ssize_t
isl29501_read_ext(struct iio_dev
*indio_dev
,
268 const struct iio_chan_spec
*chan
,
271 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
272 enum isl29501_register_name reg
= private;
274 u32 value
, gain
, coeff
, exp
;
279 ret
= isl29501_register_read(isl29501
, reg
, &gain
);
285 case REG_CALIB_PHASE_TEMP_A
:
286 case REG_CALIB_PHASE_TEMP_B
:
287 case REG_CALIB_PHASE_LIGHT_A
:
288 case REG_CALIB_PHASE_LIGHT_B
:
289 ret
= isl29501_register_read(isl29501
, REG_PHASE_EXP
, &exp
);
293 ret
= isl29501_register_read(isl29501
, reg
, &coeff
);
297 value
= coeff
<< exp
;
303 return sprintf(buf
, "%u\n", value
);
306 static int isl29501_set_shadow_coeff(struct isl29501_private
*isl29501
,
307 enum isl29501_register_name reg
,
310 enum isl29501_correction_coeff coeff
;
313 case REG_CALIB_PHASE_TEMP_A
:
314 coeff
= COEFF_TEMP_A
;
316 case REG_CALIB_PHASE_TEMP_B
:
317 coeff
= COEFF_TEMP_B
;
319 case REG_CALIB_PHASE_LIGHT_A
:
320 coeff
= COEFF_LIGHT_A
;
322 case REG_CALIB_PHASE_LIGHT_B
:
323 coeff
= COEFF_LIGHT_B
;
328 isl29501
->shadow_coeffs
[coeff
] = val
;
333 static int isl29501_write_coeff(struct isl29501_private
*isl29501
,
334 enum isl29501_correction_coeff coeff
,
337 enum isl29501_register_name reg
;
341 reg
= REG_CALIB_PHASE_TEMP_A
;
344 reg
= REG_CALIB_PHASE_TEMP_B
;
347 reg
= REG_CALIB_PHASE_LIGHT_A
;
350 reg
= REG_CALIB_PHASE_LIGHT_B
;
356 return isl29501_register_write(isl29501
, reg
, val
);
359 static unsigned int isl29501_find_corr_exp(unsigned int val
,
360 unsigned int max_exp
,
361 unsigned int max_mantissa
)
363 unsigned int exp
= 1;
366 * Correction coefficients are represented under
367 * mantissa * 2^exponent form, where mantissa and exponent
368 * are stored in two separate registers of the sensor.
370 * Compute and return the lowest exponent such as:
371 * mantissa = value / 2^exponent
373 * where mantissa < max_mantissa.
375 if (val
<= max_mantissa
)
378 while ((val
>> exp
) > max_mantissa
) {
388 static ssize_t
isl29501_write_ext(struct iio_dev
*indio_dev
,
390 const struct iio_chan_spec
*chan
,
391 const char *buf
, size_t len
)
393 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
394 enum isl29501_register_name reg
= private;
400 ret
= kstrtouint(buf
, 10, &val
);
409 ret
= isl29501_register_write(isl29501
, reg
, val
);
414 case REG_CALIB_PHASE_TEMP_A
:
415 case REG_CALIB_PHASE_TEMP_B
:
416 case REG_CALIB_PHASE_LIGHT_A
:
417 case REG_CALIB_PHASE_LIGHT_B
:
419 if (val
> (U8_MAX
<< ISL29501_MAX_EXP_VAL
))
422 /* Store the correction coefficient under its exact form. */
423 ret
= isl29501_set_shadow_coeff(isl29501
, reg
, val
);
428 * Find the highest exponent needed to represent
429 * correction coefficients.
431 for (i
= 0; i
< COEFF_MAX
; i
++) {
435 corr
= isl29501
->shadow_coeffs
[i
];
436 corr_exp
= isl29501_find_corr_exp(corr
,
437 ISL29501_MAX_EXP_VAL
,
439 dev_dbg(&isl29501
->client
->dev
,
440 "found exp of corr(%d) = %d\n", corr
, corr_exp
);
442 max_exp
= max(max_exp
, corr_exp
);
446 * Represent every correction coefficient under
447 * mantissa * 2^max_exponent form and force the
448 * writing of those coefficients on the sensor.
450 for (i
= 0; i
< COEFF_MAX
; i
++) {
454 corr
= isl29501
->shadow_coeffs
[i
];
458 mantissa
= corr
>> max_exp
;
460 ret
= isl29501_write_coeff(isl29501
, i
, mantissa
);
465 ret
= isl29501_register_write(isl29501
, REG_PHASE_EXP
, max_exp
);
477 #define _ISL29501_EXT_INFO(_name, _ident) { \
479 .read = isl29501_read_ext, \
480 .write = isl29501_write_ext, \
482 .shared = IIO_SEPARATE, \
485 static const struct iio_chan_spec_ext_info isl29501_ext_info
[] = {
486 _ISL29501_EXT_INFO("agc_gain", REG_GAIN
),
487 _ISL29501_EXT_INFO("agc_gain_bias", REG_GAIN_BIAS
),
488 _ISL29501_EXT_INFO("calib_phase_temp_a", REG_CALIB_PHASE_TEMP_A
),
489 _ISL29501_EXT_INFO("calib_phase_temp_b", REG_CALIB_PHASE_TEMP_B
),
490 _ISL29501_EXT_INFO("calib_phase_light_a", REG_CALIB_PHASE_LIGHT_A
),
491 _ISL29501_EXT_INFO("calib_phase_light_b", REG_CALIB_PHASE_LIGHT_B
),
495 #define ISL29501_DISTANCE_SCAN_INDEX 0
496 #define ISL29501_TIMESTAMP_SCAN_INDEX 1
498 static const struct iio_chan_spec isl29501_channels
[] = {
500 .type
= IIO_PROXIMITY
,
501 .scan_index
= ISL29501_DISTANCE_SCAN_INDEX
,
502 .info_mask_separate
=
503 BIT(IIO_CHAN_INFO_RAW
) |
504 BIT(IIO_CHAN_INFO_SCALE
) |
505 BIT(IIO_CHAN_INFO_CALIBBIAS
),
510 .endianness
= IIO_CPU
,
512 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
) |
513 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
514 .ext_info
= isl29501_ext_info
,
519 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
520 BIT(IIO_CHAN_INFO_SCALE
),
526 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
527 BIT(IIO_CHAN_INFO_SCALE
),
532 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
533 BIT(IIO_CHAN_INFO_SCALE
) |
534 BIT(IIO_CHAN_INFO_CALIBBIAS
),
537 .type
= IIO_INTENSITY
,
540 .channel2
= IIO_MOD_LIGHT_CLEAR
,
541 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
542 BIT(IIO_CHAN_INFO_SCALE
),
544 IIO_CHAN_SOFT_TIMESTAMP(ISL29501_TIMESTAMP_SCAN_INDEX
),
547 static int isl29501_reset_registers(struct isl29501_private
*isl29501
)
551 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
552 ISL29501_COMMAND_REGISTER
,
553 ISL29501_RESET_ALL_REGISTERS
);
555 dev_err(&isl29501
->client
->dev
,
556 "cannot reset registers %d\n", ret
);
560 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
561 ISL29501_COMMAND_REGISTER
,
562 ISL29501_RESET_INT_SM
);
564 dev_err(&isl29501
->client
->dev
,
565 "cannot reset state machine %d\n", ret
);
570 static int isl29501_begin_acquisition(struct isl29501_private
*isl29501
)
574 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
575 ISL29501_COMMAND_REGISTER
,
576 ISL29501_EMUL_SAMPLE_START_PIN
);
578 dev_err(&isl29501
->client
->dev
,
579 "cannot begin acquisition %d\n", ret
);
584 static IIO_CONST_ATTR_INT_TIME_AVAIL(ISL29501_INT_TIME_AVAILABLE
);
585 static IIO_CONST_ATTR(out_current_scale_available
,
586 ISL29501_CURRENT_SCALE_AVAILABLE
);
588 static struct attribute
*isl29501_attributes
[] = {
589 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
590 &iio_const_attr_out_current_scale_available
.dev_attr
.attr
,
594 static const struct attribute_group isl29501_attribute_group
= {
595 .attrs
= isl29501_attributes
,
598 static const int isl29501_current_scale_table
[][2] = {
599 {0, 3900}, {0, 7800}, {0, 11800}, {0, 15700},
600 {0, 19600}, {0, 23500}, {0, 27500}, {0, 31400},
601 {0, 35200}, {0, 39200}, {0, 43100}, {0, 47100},
602 {0, 51000}, {0, 54900}, {0, 58800},
605 static const int isl29501_int_time
[][2] = {
606 {0, 70}, /* 0.07 ms */
607 {0, 140}, /* 0.14 ms */
608 {0, 280}, /* 0.28 ms */
609 {0, 570}, /* 0.57 ms */
610 {0, 1140}, /* 1.14 ms */
611 {0, 2280}, /* 2.28 ms */
612 {0, 4550}, /* 4.55 ms */
613 {0, 9100}, /* 9.11 ms */
614 {0, 18200}, /* 18.2 ms */
615 {0, 36400}, /* 36.4 ms */
616 {0, 72810}, /* 72.81 ms */
617 {0, 145610} /* 145.28 ms */
620 static int isl29501_get_raw(struct isl29501_private
*isl29501
,
621 const struct iio_chan_spec
*chan
,
626 switch (chan
->type
) {
628 ret
= isl29501_register_read(isl29501
, REG_DISTANCE
, raw
);
634 ret
= isl29501_register_read(isl29501
,
642 ret
= isl29501_register_read(isl29501
, REG_PHASE
, raw
);
648 ret
= isl29501_register_read(isl29501
, REG_EMITTER_DAC
, raw
);
654 ret
= isl29501_register_read(isl29501
, REG_TEMPERATURE
, raw
);
664 static int isl29501_get_scale(struct isl29501_private
*isl29501
,
665 const struct iio_chan_spec
*chan
,
671 switch (chan
->type
) {
673 /* distance = raw_distance * 33.31 / 65536 (m) */
677 return IIO_VAL_FRACTIONAL
;
679 /* phase = raw_phase * 2pi / 65536 (rad) */
683 return IIO_VAL_INT_PLUS_NANO
;
685 /* light = raw_light * 35 / 10000 (mA) */
689 return IIO_VAL_FRACTIONAL
;
691 ret
= isl29501_register_read(isl29501
,
697 if (current_scale
> ARRAY_SIZE(isl29501_current_scale_table
))
700 if (!current_scale
) {
706 *val
= isl29501_current_scale_table
[current_scale
- 1][0];
707 *val2
= isl29501_current_scale_table
[current_scale
- 1][1];
709 return IIO_VAL_INT_PLUS_MICRO
;
711 /* temperature = raw_temperature * 125 / 100000 (milli °C) */
715 return IIO_VAL_FRACTIONAL
;
721 static int isl29501_get_calibbias(struct isl29501_private
*isl29501
,
722 const struct iio_chan_spec
*chan
,
725 switch (chan
->type
) {
727 return isl29501_register_read(isl29501
,
731 return isl29501_register_read(isl29501
,
732 REG_TEMPERATURE_BIAS
,
739 static int isl29501_get_inttime(struct isl29501_private
*isl29501
,
745 ret
= isl29501_register_read(isl29501
, REG_INT_TIME
, &inttime
);
749 if (inttime
>= ARRAY_SIZE(isl29501_int_time
))
752 *val
= isl29501_int_time
[inttime
][0];
753 *val2
= isl29501_int_time
[inttime
][1];
755 return IIO_VAL_INT_PLUS_MICRO
;
758 static int isl29501_get_freq(struct isl29501_private
*isl29501
,
763 unsigned long long freq
;
766 ret
= isl29501_register_read(isl29501
, REG_SAMPLE_TIME
, &sample_time
);
770 /* freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
771 freq
= 1000000ULL * 1000000ULL;
773 do_div(freq
, 450 * (sample_time
+ 1));
775 temp
= do_div(freq
, 1000000);
779 return IIO_VAL_INT_PLUS_MICRO
;
782 static int isl29501_read_raw(struct iio_dev
*indio_dev
,
783 struct iio_chan_spec
const *chan
, int *val
,
784 int *val2
, long mask
)
786 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
789 case IIO_CHAN_INFO_RAW
:
790 return isl29501_get_raw(isl29501
, chan
, val
);
791 case IIO_CHAN_INFO_SCALE
:
792 return isl29501_get_scale(isl29501
, chan
, val
, val2
);
793 case IIO_CHAN_INFO_INT_TIME
:
794 return isl29501_get_inttime(isl29501
, val
, val2
);
795 case IIO_CHAN_INFO_SAMP_FREQ
:
796 return isl29501_get_freq(isl29501
, val
, val2
);
797 case IIO_CHAN_INFO_CALIBBIAS
:
798 return isl29501_get_calibbias(isl29501
, chan
, val
);
804 static int isl29501_set_raw(struct isl29501_private
*isl29501
,
805 const struct iio_chan_spec
*chan
,
808 switch (chan
->type
) {
810 return isl29501_register_write(isl29501
, REG_EMITTER_DAC
, raw
);
816 static int isl29501_set_inttime(struct isl29501_private
*isl29501
,
821 for (i
= 0; i
< ARRAY_SIZE(isl29501_int_time
); i
++) {
822 if (isl29501_int_time
[i
][0] == val
&&
823 isl29501_int_time
[i
][1] == val2
) {
824 return isl29501_register_write(isl29501
,
833 static int isl29501_set_scale(struct isl29501_private
*isl29501
,
834 const struct iio_chan_spec
*chan
,
839 if (chan
->type
!= IIO_CURRENT
)
842 for (i
= 0; i
< ARRAY_SIZE(isl29501_current_scale_table
); i
++) {
843 if (isl29501_current_scale_table
[i
][0] == val
&&
844 isl29501_current_scale_table
[i
][1] == val2
) {
845 return isl29501_register_write(isl29501
,
854 static int isl29501_set_calibbias(struct isl29501_private
*isl29501
,
855 const struct iio_chan_spec
*chan
,
858 switch (chan
->type
) {
860 return isl29501_register_write(isl29501
,
864 return isl29501_register_write(isl29501
,
865 REG_TEMPERATURE_BIAS
,
872 static int isl29501_set_freq(struct isl29501_private
*isl29501
,
876 unsigned long long sample_time
;
878 /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
879 freq
= val
* 1000000 + val2
% 1000000;
880 sample_time
= 2222ULL * 1000000ULL;
881 do_div(sample_time
, freq
);
885 if (sample_time
> 255)
888 return isl29501_register_write(isl29501
, REG_SAMPLE_TIME
, sample_time
);
891 static int isl29501_write_raw(struct iio_dev
*indio_dev
,
892 struct iio_chan_spec
const *chan
,
893 int val
, int val2
, long mask
)
895 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
898 case IIO_CHAN_INFO_RAW
:
899 return isl29501_set_raw(isl29501
, chan
, val
);
900 case IIO_CHAN_INFO_INT_TIME
:
901 return isl29501_set_inttime(isl29501
, val
, val2
);
902 case IIO_CHAN_INFO_SAMP_FREQ
:
903 return isl29501_set_freq(isl29501
, val
, val2
);
904 case IIO_CHAN_INFO_SCALE
:
905 return isl29501_set_scale(isl29501
, chan
, val
, val2
);
906 case IIO_CHAN_INFO_CALIBBIAS
:
907 return isl29501_set_calibbias(isl29501
, chan
, val
);
913 static const struct iio_info isl29501_info
= {
914 .read_raw
= &isl29501_read_raw
,
915 .write_raw
= &isl29501_write_raw
,
916 .attrs
= &isl29501_attribute_group
,
919 static int isl29501_init_chip(struct isl29501_private
*isl29501
)
923 ret
= i2c_smbus_read_byte_data(isl29501
->client
, ISL29501_DEVICE_ID
);
925 dev_err(&isl29501
->client
->dev
, "Error reading device id\n");
929 if (ret
!= ISL29501_ID
) {
930 dev_err(&isl29501
->client
->dev
,
931 "Wrong chip id, got %x expected %x\n",
932 ret
, ISL29501_DEVICE_ID
);
936 ret
= isl29501_reset_registers(isl29501
);
940 return isl29501_begin_acquisition(isl29501
);
943 static irqreturn_t
isl29501_trigger_handler(int irq
, void *p
)
945 struct iio_poll_func
*pf
= p
;
946 struct iio_dev
*indio_dev
= pf
->indio_dev
;
947 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
948 const unsigned long *active_mask
= indio_dev
->active_scan_mask
;
949 u32 buffer
[4] = {}; /* 1x16-bit + ts */
951 if (test_bit(ISL29501_DISTANCE_SCAN_INDEX
, active_mask
))
952 isl29501_register_read(isl29501
, REG_DISTANCE
, buffer
);
954 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
, pf
->timestamp
);
955 iio_trigger_notify_done(indio_dev
->trig
);
960 static int isl29501_probe(struct i2c_client
*client
,
961 const struct i2c_device_id
*id
)
963 struct iio_dev
*indio_dev
;
964 struct isl29501_private
*isl29501
;
967 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*isl29501
));
971 isl29501
= iio_priv(indio_dev
);
973 i2c_set_clientdata(client
, indio_dev
);
974 isl29501
->client
= client
;
976 mutex_init(&isl29501
->lock
);
978 ret
= isl29501_init_chip(isl29501
);
982 indio_dev
->modes
= INDIO_DIRECT_MODE
;
983 indio_dev
->dev
.parent
= &client
->dev
;
984 indio_dev
->channels
= isl29501_channels
;
985 indio_dev
->num_channels
= ARRAY_SIZE(isl29501_channels
);
986 indio_dev
->name
= client
->name
;
987 indio_dev
->info
= &isl29501_info
;
989 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
,
990 iio_pollfunc_store_time
,
991 isl29501_trigger_handler
,
994 dev_err(&client
->dev
, "unable to setup iio triggered buffer\n");
998 return devm_iio_device_register(&client
->dev
, indio_dev
);
1001 static const struct i2c_device_id isl29501_id
[] = {
1006 MODULE_DEVICE_TABLE(i2c
, isl29501_id
);
1008 #if defined(CONFIG_OF)
1009 static const struct of_device_id isl29501_i2c_matches
[] = {
1010 { .compatible
= "renesas,isl29501" },
1013 MODULE_DEVICE_TABLE(of
, isl29501_i2c_matches
);
1016 static struct i2c_driver isl29501_driver
= {
1020 .id_table
= isl29501_id
,
1021 .probe
= isl29501_probe
,
1023 module_i2c_driver(isl29501_driver
);
1025 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1026 MODULE_DESCRIPTION("ISL29501 Time of Flight sensor driver");
1027 MODULE_LICENSE("GPL v2");