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
];
237 if (!reg
->msb
&& value
> U8_MAX
)
243 mutex_lock(&isl29501
->lock
);
245 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
246 reg
->msb
, value
>> 8);
251 ret
= i2c_smbus_write_byte_data(isl29501
->client
, reg
->lsb
, value
);
254 mutex_unlock(&isl29501
->lock
);
258 static ssize_t
isl29501_read_ext(struct iio_dev
*indio_dev
,
260 const struct iio_chan_spec
*chan
,
263 struct isl29501_private
*isl29501
= iio_priv(indio_dev
);
264 enum isl29501_register_name reg
= private;
266 u32 value
, gain
, coeff
, exp
;
271 ret
= isl29501_register_read(isl29501
, reg
, &gain
);
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
);
285 ret
= isl29501_register_read(isl29501
, reg
, &coeff
);
289 value
= coeff
<< exp
;
295 return sprintf(buf
, "%u\n", value
);
298 static int isl29501_set_shadow_coeff(struct isl29501_private
*isl29501
,
299 enum isl29501_register_name reg
,
302 enum isl29501_correction_coeff coeff
;
305 case REG_CALIB_PHASE_TEMP_A
:
306 coeff
= COEFF_TEMP_A
;
308 case REG_CALIB_PHASE_TEMP_B
:
309 coeff
= COEFF_TEMP_B
;
311 case REG_CALIB_PHASE_LIGHT_A
:
312 coeff
= COEFF_LIGHT_A
;
314 case REG_CALIB_PHASE_LIGHT_B
:
315 coeff
= COEFF_LIGHT_B
;
320 isl29501
->shadow_coeffs
[coeff
] = val
;
325 static int isl29501_write_coeff(struct isl29501_private
*isl29501
,
326 enum isl29501_correction_coeff coeff
,
329 enum isl29501_register_name reg
;
333 reg
= REG_CALIB_PHASE_TEMP_A
;
336 reg
= REG_CALIB_PHASE_TEMP_B
;
339 reg
= REG_CALIB_PHASE_LIGHT_A
;
342 reg
= REG_CALIB_PHASE_LIGHT_B
;
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
)
370 while ((val
>> exp
) > max_mantissa
) {
380 static ssize_t
isl29501_write_ext(struct iio_dev
*indio_dev
,
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;
392 ret
= kstrtouint(buf
, 10, &val
);
401 ret
= isl29501_register_write(isl29501
, reg
, val
);
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
))
414 /* Store the correction coefficient under its exact form. */
415 ret
= isl29501_set_shadow_coeff(isl29501
, reg
, val
);
420 * Find the highest exponent needed to represent
421 * correction coefficients.
423 for (i
= 0; i
< COEFF_MAX
; i
++) {
427 corr
= isl29501
->shadow_coeffs
[i
];
428 corr_exp
= isl29501_find_corr_exp(corr
,
429 ISL29501_MAX_EXP_VAL
,
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
++) {
446 corr
= isl29501
->shadow_coeffs
[i
];
450 mantissa
= corr
>> max_exp
;
452 ret
= isl29501_write_coeff(isl29501
, i
, mantissa
);
457 ret
= isl29501_register_write(isl29501
, REG_PHASE_EXP
, max_exp
);
469 #define _ISL29501_EXT_INFO(_name, _ident) { \
471 .read = isl29501_read_ext, \
472 .write = isl29501_write_ext, \
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
),
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
),
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
,
511 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
512 BIT(IIO_CHAN_INFO_SCALE
),
518 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
519 BIT(IIO_CHAN_INFO_SCALE
),
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
,
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
)
543 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
544 ISL29501_COMMAND_REGISTER
,
545 ISL29501_RESET_ALL_REGISTERS
);
547 dev_err(&isl29501
->client
->dev
,
548 "cannot reset registers %d\n", ret
);
552 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
553 ISL29501_COMMAND_REGISTER
,
554 ISL29501_RESET_INT_SM
);
556 dev_err(&isl29501
->client
->dev
,
557 "cannot reset state machine %d\n", ret
);
562 static int isl29501_begin_acquisition(struct isl29501_private
*isl29501
)
566 ret
= i2c_smbus_write_byte_data(isl29501
->client
,
567 ISL29501_COMMAND_REGISTER
,
568 ISL29501_EMUL_SAMPLE_START_PIN
);
570 dev_err(&isl29501
->client
->dev
,
571 "cannot begin acquisition %d\n", 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
,
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
,
618 switch (chan
->type
) {
620 ret
= isl29501_register_read(isl29501
, REG_DISTANCE
, raw
);
626 ret
= isl29501_register_read(isl29501
,
634 ret
= isl29501_register_read(isl29501
, REG_PHASE
, raw
);
640 ret
= isl29501_register_read(isl29501
, REG_EMITTER_DAC
, raw
);
646 ret
= isl29501_register_read(isl29501
, REG_TEMPERATURE
, raw
);
656 static int isl29501_get_scale(struct isl29501_private
*isl29501
,
657 const struct iio_chan_spec
*chan
,
663 switch (chan
->type
) {
665 /* distance = raw_distance * 33.31 / 65536 (m) */
669 return IIO_VAL_FRACTIONAL
;
671 /* phase = raw_phase * 2pi / 65536 (rad) */
675 return IIO_VAL_INT_PLUS_NANO
;
677 /* light = raw_light * 35 / 10000 (mA) */
681 return IIO_VAL_FRACTIONAL
;
683 ret
= isl29501_register_read(isl29501
,
689 if (current_scale
> ARRAY_SIZE(isl29501_current_scale_table
))
692 if (!current_scale
) {
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
;
703 /* temperature = raw_temperature * 125 / 100000 (milli °C) */
707 return IIO_VAL_FRACTIONAL
;
713 static int isl29501_get_calibbias(struct isl29501_private
*isl29501
,
714 const struct iio_chan_spec
*chan
,
717 switch (chan
->type
) {
719 return isl29501_register_read(isl29501
,
723 return isl29501_register_read(isl29501
,
724 REG_TEMPERATURE_BIAS
,
731 static int isl29501_get_inttime(struct isl29501_private
*isl29501
,
737 ret
= isl29501_register_read(isl29501
, REG_INT_TIME
, &inttime
);
741 if (inttime
>= ARRAY_SIZE(isl29501_int_time
))
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
,
755 unsigned long long freq
;
758 ret
= isl29501_register_read(isl29501
, REG_SAMPLE_TIME
, &sample_time
);
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);
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
);
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
);
796 static int isl29501_set_raw(struct isl29501_private
*isl29501
,
797 const struct iio_chan_spec
*chan
,
800 switch (chan
->type
) {
802 return isl29501_register_write(isl29501
, REG_EMITTER_DAC
, raw
);
808 static int isl29501_set_inttime(struct isl29501_private
*isl29501
,
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
,
825 static int isl29501_set_scale(struct isl29501_private
*isl29501
,
826 const struct iio_chan_spec
*chan
,
831 if (chan
->type
!= IIO_CURRENT
)
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
,
846 static int isl29501_set_calibbias(struct isl29501_private
*isl29501
,
847 const struct iio_chan_spec
*chan
,
850 switch (chan
->type
) {
852 return isl29501_register_write(isl29501
,
856 return isl29501_register_write(isl29501
,
857 REG_TEMPERATURE_BIAS
,
864 static int isl29501_set_freq(struct isl29501_private
*isl29501
,
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
);
877 if (sample_time
> 255)
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
);
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
);
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
)
915 ret
= i2c_smbus_read_byte_data(isl29501
->client
, ISL29501_DEVICE_ID
);
917 dev_err(&isl29501
->client
->dev
, "Error reading device id\n");
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
);
928 ret
= isl29501_reset_registers(isl29501
);
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] = {}; /* 1x16-bit + 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
);
952 static int isl29501_probe(struct i2c_client
*client
,
953 const struct i2c_device_id
*id
)
955 struct iio_dev
*indio_dev
;
956 struct isl29501_private
*isl29501
;
959 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*isl29501
));
963 isl29501
= iio_priv(indio_dev
);
965 i2c_set_clientdata(client
, indio_dev
);
966 isl29501
->client
= client
;
968 mutex_init(&isl29501
->lock
);
970 ret
= isl29501_init_chip(isl29501
);
974 indio_dev
->modes
= INDIO_DIRECT_MODE
;
975 indio_dev
->dev
.parent
= &client
->dev
;
976 indio_dev
->channels
= isl29501_channels
;
977 indio_dev
->num_channels
= ARRAY_SIZE(isl29501_channels
);
978 indio_dev
->name
= client
->name
;
979 indio_dev
->info
= &isl29501_info
;
981 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
,
982 iio_pollfunc_store_time
,
983 isl29501_trigger_handler
,
986 dev_err(&client
->dev
, "unable to setup iio triggered buffer\n");
990 return devm_iio_device_register(&client
->dev
, indio_dev
);
993 static const struct i2c_device_id isl29501_id
[] = {
998 MODULE_DEVICE_TABLE(i2c
, isl29501_id
);
1000 #if defined(CONFIG_OF)
1001 static const struct of_device_id isl29501_i2c_matches
[] = {
1002 { .compatible
= "renesas,isl29501" },
1005 MODULE_DEVICE_TABLE(of
, isl29501_i2c_matches
);
1008 static struct i2c_driver isl29501_driver
= {
1012 .id_table
= isl29501_id
,
1013 .probe
= isl29501_probe
,
1015 module_i2c_driver(isl29501_driver
);
1017 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1018 MODULE_DESCRIPTION("ISL29501 Time of Flight sensor driver");
1019 MODULE_LICENSE("GPL v2");