2 * si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient
3 * light, UV index and proximity sensors
5 * Copyright 2014-16 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
6 * Copyright 2016 Crestez Dan Leonard <leonard.crestez@intel.com>
8 * This file is subject to the terms and conditions of version 2 of
9 * the GNU General Public License. See the file COPYING in the main
10 * directory of this archive for more details.
12 * SI1132 (7-bit I2C slave address 0x60)
13 * SI1141/2/3 (7-bit I2C slave address 0x5a)
14 * SI1145/6/6 (7-bit I2C slave address 0x60)
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/irq.h>
23 #include <linux/gpio.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/util_macros.h>
33 #define SI1145_REG_PART_ID 0x00
34 #define SI1145_REG_REV_ID 0x01
35 #define SI1145_REG_SEQ_ID 0x02
36 #define SI1145_REG_INT_CFG 0x03
37 #define SI1145_REG_IRQ_ENABLE 0x04
38 #define SI1145_REG_IRQ_MODE 0x05
39 #define SI1145_REG_HW_KEY 0x07
40 #define SI1145_REG_MEAS_RATE 0x08
41 #define SI1145_REG_PS_LED21 0x0f
42 #define SI1145_REG_PS_LED3 0x10
43 #define SI1145_REG_UCOEF1 0x13
44 #define SI1145_REG_UCOEF2 0x14
45 #define SI1145_REG_UCOEF3 0x15
46 #define SI1145_REG_UCOEF4 0x16
47 #define SI1145_REG_PARAM_WR 0x17
48 #define SI1145_REG_COMMAND 0x18
49 #define SI1145_REG_RESPONSE 0x20
50 #define SI1145_REG_IRQ_STATUS 0x21
51 #define SI1145_REG_ALSVIS_DATA 0x22
52 #define SI1145_REG_ALSIR_DATA 0x24
53 #define SI1145_REG_PS1_DATA 0x26
54 #define SI1145_REG_PS2_DATA 0x28
55 #define SI1145_REG_PS3_DATA 0x2a
56 #define SI1145_REG_AUX_DATA 0x2c
57 #define SI1145_REG_PARAM_RD 0x2e
58 #define SI1145_REG_CHIP_STAT 0x30
60 #define SI1145_UCOEF1_DEFAULT 0x7b
61 #define SI1145_UCOEF2_DEFAULT 0x6b
62 #define SI1145_UCOEF3_DEFAULT 0x01
63 #define SI1145_UCOEF4_DEFAULT 0x00
65 /* Helper to figure out PS_LED register / shift per channel */
66 #define SI1145_PS_LED_REG(ch) \
67 (((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21)
68 #define SI1145_PS_LED_SHIFT(ch) \
71 /* Parameter offsets */
72 #define SI1145_PARAM_CHLIST 0x01
73 #define SI1145_PARAM_PSLED12_SELECT 0x02
74 #define SI1145_PARAM_PSLED3_SELECT 0x03
75 #define SI1145_PARAM_PS_ENCODING 0x05
76 #define SI1145_PARAM_ALS_ENCODING 0x06
77 #define SI1145_PARAM_PS1_ADC_MUX 0x07
78 #define SI1145_PARAM_PS2_ADC_MUX 0x08
79 #define SI1145_PARAM_PS3_ADC_MUX 0x09
80 #define SI1145_PARAM_PS_ADC_COUNTER 0x0a
81 #define SI1145_PARAM_PS_ADC_GAIN 0x0b
82 #define SI1145_PARAM_PS_ADC_MISC 0x0c
83 #define SI1145_PARAM_ALS_ADC_MUX 0x0d
84 #define SI1145_PARAM_ALSIR_ADC_MUX 0x0e
85 #define SI1145_PARAM_AUX_ADC_MUX 0x0f
86 #define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10
87 #define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11
88 #define SI1145_PARAM_ALSVIS_ADC_MISC 0x12
89 #define SI1145_PARAM_LED_RECOVERY 0x1c
90 #define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d
91 #define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e
92 #define SI1145_PARAM_ALSIR_ADC_MISC 0x1f
93 #define SI1145_PARAM_ADC_OFFSET 0x1a
95 /* Channel enable masks for CHLIST parameter */
96 #define SI1145_CHLIST_EN_PS1 BIT(0)
97 #define SI1145_CHLIST_EN_PS2 BIT(1)
98 #define SI1145_CHLIST_EN_PS3 BIT(2)
99 #define SI1145_CHLIST_EN_ALSVIS BIT(4)
100 #define SI1145_CHLIST_EN_ALSIR BIT(5)
101 #define SI1145_CHLIST_EN_AUX BIT(6)
102 #define SI1145_CHLIST_EN_UV BIT(7)
104 /* Proximity measurement mode for ADC_MISC parameter */
105 #define SI1145_PS_ADC_MODE_NORMAL BIT(2)
106 /* Signal range mask for ADC_MISC parameter */
107 #define SI1145_ADC_MISC_RANGE BIT(5)
109 /* Commands for REG_COMMAND */
110 #define SI1145_CMD_NOP 0x00
111 #define SI1145_CMD_RESET 0x01
112 #define SI1145_CMD_PS_FORCE 0x05
113 #define SI1145_CMD_ALS_FORCE 0x06
114 #define SI1145_CMD_PSALS_FORCE 0x07
115 #define SI1145_CMD_PS_PAUSE 0x09
116 #define SI1145_CMD_ALS_PAUSE 0x0a
117 #define SI1145_CMD_PSALS_PAUSE 0x0b
118 #define SI1145_CMD_PS_AUTO 0x0d
119 #define SI1145_CMD_ALS_AUTO 0x0e
120 #define SI1145_CMD_PSALS_AUTO 0x0f
121 #define SI1145_CMD_PARAM_QUERY 0x80
122 #define SI1145_CMD_PARAM_SET 0xa0
124 #define SI1145_RSP_INVALID_SETTING 0x80
125 #define SI1145_RSP_COUNTER_MASK 0x0F
127 /* Minimum sleep after each command to ensure it's received */
128 #define SI1145_COMMAND_MINSLEEP_MS 5
129 /* Return -ETIMEDOUT after this long */
130 #define SI1145_COMMAND_TIMEOUT_MS 25
132 /* Interrupt configuration masks for INT_CFG register */
133 #define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */
134 #define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */
136 /* Interrupt enable masks for IRQ_ENABLE register */
137 #define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0))
139 #define SI1145_MUX_TEMP 0x65
140 #define SI1145_MUX_VDD 0x75
142 /* Proximity LED current; see Table 2 in datasheet */
143 #define SI1145_LED_CURRENT_45mA 0x04
155 struct si1145_part_info
{
157 const struct iio_info
*iio_info
;
158 const struct iio_chan_spec
*channels
;
159 unsigned int num_channels
;
160 unsigned int num_leds
;
161 bool uncompressed_meas_rate
;
165 * struct si1145_data - si1145 chip state data
166 * @client: I2C client
167 * @lock: mutex to protect shared state.
168 * @cmdlock: Low-level mutex to protect command execution only
169 * @rsp_seq: Next expected response number or -1 if counter reset required
170 * @scan_mask: Saved scan mask to avoid duplicate set_chlist
171 * @autonomous: If automatic measurements are active (for buffer support)
172 * @part_info: Part information
173 * @trig: Pointer to iio trigger
174 * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode
177 struct i2c_client
*client
;
179 struct mutex cmdlock
;
181 const struct si1145_part_info
*part_info
;
182 unsigned long scan_mask
;
184 struct iio_trigger
*trig
;
189 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
191 * Does not modify data->rsp_seq
193 * Return: 0 on success and -errno on error.
195 static int __si1145_command_reset(struct si1145_data
*data
)
197 struct device
*dev
= &data
->client
->dev
;
198 unsigned long stop_jiffies
;
201 ret
= i2c_smbus_write_byte_data(data
->client
, SI1145_REG_COMMAND
,
205 msleep(SI1145_COMMAND_MINSLEEP_MS
);
207 stop_jiffies
= jiffies
+ SI1145_COMMAND_TIMEOUT_MS
* HZ
/ 1000;
209 ret
= i2c_smbus_read_byte_data(data
->client
,
210 SI1145_REG_RESPONSE
);
213 if (time_after(jiffies
, stop_jiffies
)) {
214 dev_warn(dev
, "timeout on reset\n");
217 msleep(SI1145_COMMAND_MINSLEEP_MS
);
223 * si1145_command() - Execute a command and poll the response register
225 * All conversion overflows are reported as -EOVERFLOW
226 * INVALID_SETTING is reported as -EINVAL
227 * Timeouts are reported as -ETIMEDOUT
229 * Return: 0 on success or -errno on failure
231 static int si1145_command(struct si1145_data
*data
, u8 cmd
)
233 struct device
*dev
= &data
->client
->dev
;
234 unsigned long stop_jiffies
;
237 mutex_lock(&data
->cmdlock
);
239 if (data
->rsp_seq
< 0) {
240 ret
= __si1145_command_reset(data
);
242 dev_err(dev
, "failed to reset command counter, ret=%d\n",
249 ret
= i2c_smbus_write_byte_data(data
->client
, SI1145_REG_COMMAND
, cmd
);
251 dev_warn(dev
, "failed to write command, ret=%d\n", ret
);
254 /* Sleep a little to ensure the command is received */
255 msleep(SI1145_COMMAND_MINSLEEP_MS
);
257 stop_jiffies
= jiffies
+ SI1145_COMMAND_TIMEOUT_MS
* HZ
/ 1000;
259 ret
= i2c_smbus_read_byte_data(data
->client
,
260 SI1145_REG_RESPONSE
);
262 dev_warn(dev
, "failed to read response, ret=%d\n", ret
);
266 if ((ret
& ~SI1145_RSP_COUNTER_MASK
) == 0) {
267 if (ret
== data
->rsp_seq
) {
268 if (time_after(jiffies
, stop_jiffies
)) {
269 dev_warn(dev
, "timeout on command %#02hhx\n",
274 msleep(SI1145_COMMAND_MINSLEEP_MS
);
277 if (ret
== ((data
->rsp_seq
+ 1) &
278 SI1145_RSP_COUNTER_MASK
)) {
283 dev_warn(dev
, "unexpected response counter %d instead of %d\n",
284 ret
, (data
->rsp_seq
+ 1) &
285 SI1145_RSP_COUNTER_MASK
);
288 if (ret
== SI1145_RSP_INVALID_SETTING
) {
289 dev_warn(dev
, "INVALID_SETTING error on command %#02hhx\n",
293 /* All overflows are treated identically */
294 dev_dbg(dev
, "overflow, ret=%d, cmd=%#02hhx\n",
300 /* Force a counter reset next time */
306 mutex_unlock(&data
->cmdlock
);
311 static int si1145_param_update(struct si1145_data
*data
, u8 op
, u8 param
,
316 ret
= i2c_smbus_write_byte_data(data
->client
,
317 SI1145_REG_PARAM_WR
, value
);
321 return si1145_command(data
, op
| (param
& 0x1F));
324 static int si1145_param_set(struct si1145_data
*data
, u8 param
, u8 value
)
326 return si1145_param_update(data
, SI1145_CMD_PARAM_SET
, param
, value
);
329 /* Set param. Returns negative errno or current value */
330 static int si1145_param_query(struct si1145_data
*data
, u8 param
)
334 ret
= si1145_command(data
, SI1145_CMD_PARAM_QUERY
| (param
& 0x1F));
338 return i2c_smbus_read_byte_data(data
->client
, SI1145_REG_PARAM_RD
);
341 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
342 static u16
si1145_uncompress(u8 x
)
350 exponent
= (x
& 0xf0) >> 4;
351 result
= 0x10 | (x
& 0x0f);
354 return result
<< (exponent
- 4);
355 return result
>> (4 - exponent
);
358 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
359 static u8
si1145_compress(u16 x
)
378 significand
= x
<< (4 - exponent
);
379 return (exponent
<< 4) | (significand
& 0xF);
382 significand
= x
>> (exponent
- 5);
383 if (significand
& 1) {
385 if (significand
& 0x0040) {
391 return (exponent
<< 4) | ((significand
>> 1) & 0xF);
394 /* Write meas_rate in hardware */
395 static int si1145_set_meas_rate(struct si1145_data
*data
, int interval
)
397 if (data
->part_info
->uncompressed_meas_rate
)
398 return i2c_smbus_write_word_data(data
->client
,
399 SI1145_REG_MEAS_RATE
, interval
);
401 return i2c_smbus_write_byte_data(data
->client
,
402 SI1145_REG_MEAS_RATE
, interval
);
405 static int si1145_read_samp_freq(struct si1145_data
*data
, int *val
, int *val2
)
408 if (data
->part_info
->uncompressed_meas_rate
)
409 *val2
= data
->meas_rate
;
411 *val2
= si1145_uncompress(data
->meas_rate
);
412 return IIO_VAL_FRACTIONAL
;
415 /* Set the samp freq in driver private data */
416 static int si1145_store_samp_freq(struct si1145_data
*data
, int val
)
421 if (val
<= 0 || val
> 32000)
423 meas_rate
= 32000 / val
;
425 mutex_lock(&data
->lock
);
426 if (data
->autonomous
) {
427 ret
= si1145_set_meas_rate(data
, meas_rate
);
431 if (data
->part_info
->uncompressed_meas_rate
)
432 data
->meas_rate
= meas_rate
;
434 data
->meas_rate
= si1145_compress(meas_rate
);
437 mutex_unlock(&data
->lock
);
442 static irqreturn_t
si1145_trigger_handler(int irq
, void *private)
444 struct iio_poll_func
*pf
= private;
445 struct iio_dev
*indio_dev
= pf
->indio_dev
;
446 struct si1145_data
*data
= iio_priv(indio_dev
);
448 * Maximum buffer size:
449 * 6*2 bytes channels data + 4 bytes alignment +
457 if (!data
->autonomous
) {
458 ret
= si1145_command(data
, SI1145_CMD_PSALS_FORCE
);
459 if (ret
< 0 && ret
!= -EOVERFLOW
)
462 irq_status
= ret
= i2c_smbus_read_byte_data(data
->client
,
463 SI1145_REG_IRQ_STATUS
);
466 if (!(irq_status
& SI1145_MASK_ALL_IE
))
470 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
471 indio_dev
->masklength
) {
474 while (i
+ run
< indio_dev
->masklength
) {
475 if (!test_bit(i
+ run
, indio_dev
->active_scan_mask
))
477 if (indio_dev
->channels
[i
+ run
].address
!=
478 indio_dev
->channels
[i
].address
+ 2 * run
)
483 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
484 data
->client
, indio_dev
->channels
[i
].address
,
485 sizeof(u16
) * run
, &buffer
[j
]);
488 j
+= run
* sizeof(u16
);
492 if (data
->autonomous
) {
493 ret
= i2c_smbus_write_byte_data(data
->client
,
494 SI1145_REG_IRQ_STATUS
,
495 irq_status
& SI1145_MASK_ALL_IE
);
500 iio_push_to_buffers_with_timestamp(indio_dev
, buffer
,
501 iio_get_time_ns(indio_dev
));
504 iio_trigger_notify_done(indio_dev
->trig
);
508 static int si1145_set_chlist(struct iio_dev
*indio_dev
, unsigned long scan_mask
)
510 struct si1145_data
*data
= iio_priv(indio_dev
);
515 /* channel list already set, no need to reprogram */
516 if (data
->scan_mask
== scan_mask
)
519 for_each_set_bit(i
, &scan_mask
, indio_dev
->masklength
) {
520 switch (indio_dev
->channels
[i
].address
) {
521 case SI1145_REG_ALSVIS_DATA
:
522 reg
|= SI1145_CHLIST_EN_ALSVIS
;
524 case SI1145_REG_ALSIR_DATA
:
525 reg
|= SI1145_CHLIST_EN_ALSIR
;
527 case SI1145_REG_PS1_DATA
:
528 reg
|= SI1145_CHLIST_EN_PS1
;
530 case SI1145_REG_PS2_DATA
:
531 reg
|= SI1145_CHLIST_EN_PS2
;
533 case SI1145_REG_PS3_DATA
:
534 reg
|= SI1145_CHLIST_EN_PS3
;
536 case SI1145_REG_AUX_DATA
:
537 switch (indio_dev
->channels
[i
].type
) {
539 reg
|= SI1145_CHLIST_EN_UV
;
542 reg
|= SI1145_CHLIST_EN_AUX
;
543 if (indio_dev
->channels
[i
].type
== IIO_TEMP
)
544 mux
= SI1145_MUX_TEMP
;
546 mux
= SI1145_MUX_VDD
;
547 ret
= si1145_param_set(data
,
548 SI1145_PARAM_AUX_ADC_MUX
, mux
);
557 data
->scan_mask
= scan_mask
;
558 ret
= si1145_param_set(data
, SI1145_PARAM_CHLIST
, reg
);
560 return ret
< 0 ? ret
: 0;
563 static int si1145_measure(struct iio_dev
*indio_dev
,
564 struct iio_chan_spec
const *chan
)
566 struct si1145_data
*data
= iio_priv(indio_dev
);
570 ret
= si1145_set_chlist(indio_dev
, BIT(chan
->scan_index
));
574 cmd
= (chan
->type
== IIO_PROXIMITY
) ? SI1145_CMD_PS_FORCE
:
575 SI1145_CMD_ALS_FORCE
;
576 ret
= si1145_command(data
, cmd
);
577 if (ret
< 0 && ret
!= -EOVERFLOW
)
580 return i2c_smbus_read_word_data(data
->client
, chan
->address
);
584 * Conversion between iio scale and ADC_GAIN values
585 * These could be further adjusted but proximity/intensity are dimensionless
587 static const int si1145_proximity_scale_available
[] = {
588 128, 64, 32, 16, 8, 4};
589 static const int si1145_intensity_scale_available
[] = {
590 128, 64, 32, 16, 8, 4, 2, 1};
591 static IIO_CONST_ATTR(in_proximity_scale_available
,
593 static IIO_CONST_ATTR(in_intensity_scale_available
,
594 "128 64 32 16 8 4 2 1");
595 static IIO_CONST_ATTR(in_intensity_ir_scale_available
,
596 "128 64 32 16 8 4 2 1");
598 static int si1145_scale_from_adcgain(int regval
)
600 return 128 >> regval
;
603 static int si1145_proximity_adcgain_from_scale(int val
, int val2
)
605 val
= find_closest_descending(val
, si1145_proximity_scale_available
,
606 ARRAY_SIZE(si1145_proximity_scale_available
));
607 if (val
< 0 || val
> 5 || val2
!= 0)
613 static int si1145_intensity_adcgain_from_scale(int val
, int val2
)
615 val
= find_closest_descending(val
, si1145_intensity_scale_available
,
616 ARRAY_SIZE(si1145_intensity_scale_available
));
617 if (val
< 0 || val
> 7 || val2
!= 0)
623 static int si1145_read_raw(struct iio_dev
*indio_dev
,
624 struct iio_chan_spec
const *chan
,
625 int *val
, int *val2
, long mask
)
627 struct si1145_data
*data
= iio_priv(indio_dev
);
632 case IIO_CHAN_INFO_RAW
:
633 switch (chan
->type
) {
639 ret
= iio_device_claim_direct_mode(indio_dev
);
642 ret
= si1145_measure(indio_dev
, chan
);
643 iio_device_release_direct_mode(indio_dev
);
652 ret
= i2c_smbus_read_byte_data(data
->client
,
653 SI1145_PS_LED_REG(chan
->channel
));
657 *val
= (ret
>> SI1145_PS_LED_SHIFT(chan
->channel
))
664 case IIO_CHAN_INFO_SCALE
:
665 switch (chan
->type
) {
667 reg
= SI1145_PARAM_PS_ADC_GAIN
;
670 if (chan
->channel2
== IIO_MOD_LIGHT_IR
)
671 reg
= SI1145_PARAM_ALSIR_ADC_GAIN
;
673 reg
= SI1145_PARAM_ALSVIS_ADC_GAIN
;
678 return IIO_VAL_INT_PLUS_MICRO
;
682 return IIO_VAL_INT_PLUS_MICRO
;
687 ret
= si1145_param_query(data
, reg
);
691 *val
= si1145_scale_from_adcgain(ret
& 0x07);
694 case IIO_CHAN_INFO_OFFSET
:
695 switch (chan
->type
) {
698 * -ADC offset - ADC counts @ 25°C -
699 * 35 * ADC counts / °C
701 *val
= -256 - 11136 + 25 * 35;
705 * All ADC measurements have are by default offset
709 ret
= si1145_param_query(data
, SI1145_PARAM_ADC_OFFSET
);
712 *val
= -si1145_uncompress(ret
);
715 case IIO_CHAN_INFO_SAMP_FREQ
:
716 return si1145_read_samp_freq(data
, val
, val2
);
722 static int si1145_write_raw(struct iio_dev
*indio_dev
,
723 struct iio_chan_spec
const *chan
,
724 int val
, int val2
, long mask
)
726 struct si1145_data
*data
= iio_priv(indio_dev
);
727 u8 reg1
, reg2
, shift
;
731 case IIO_CHAN_INFO_SCALE
:
732 switch (chan
->type
) {
734 val
= si1145_proximity_adcgain_from_scale(val
, val2
);
737 reg1
= SI1145_PARAM_PS_ADC_GAIN
;
738 reg2
= SI1145_PARAM_PS_ADC_COUNTER
;
741 val
= si1145_intensity_adcgain_from_scale(val
, val2
);
744 if (chan
->channel2
== IIO_MOD_LIGHT_IR
) {
745 reg1
= SI1145_PARAM_ALSIR_ADC_GAIN
;
746 reg2
= SI1145_PARAM_ALSIR_ADC_COUNTER
;
748 reg1
= SI1145_PARAM_ALSVIS_ADC_GAIN
;
749 reg2
= SI1145_PARAM_ALSVIS_ADC_COUNTER
;
756 ret
= iio_device_claim_direct_mode(indio_dev
);
760 ret
= si1145_param_set(data
, reg1
, val
);
762 iio_device_release_direct_mode(indio_dev
);
765 /* Set recovery period to one's complement of gain */
766 ret
= si1145_param_set(data
, reg2
, (~val
& 0x07) << 4);
767 iio_device_release_direct_mode(indio_dev
);
769 case IIO_CHAN_INFO_RAW
:
770 if (chan
->type
!= IIO_CURRENT
)
773 if (val
< 0 || val
> 15 || val2
!= 0)
776 reg1
= SI1145_PS_LED_REG(chan
->channel
);
777 shift
= SI1145_PS_LED_SHIFT(chan
->channel
);
779 ret
= iio_device_claim_direct_mode(indio_dev
);
783 ret
= i2c_smbus_read_byte_data(data
->client
, reg1
);
785 iio_device_release_direct_mode(indio_dev
);
788 ret
= i2c_smbus_write_byte_data(data
->client
, reg1
,
789 (ret
& ~(0x0f << shift
)) |
790 ((val
& 0x0f) << shift
));
791 iio_device_release_direct_mode(indio_dev
);
793 case IIO_CHAN_INFO_SAMP_FREQ
:
794 return si1145_store_samp_freq(data
, val
);
800 #define SI1145_ST { \
804 .endianness = IIO_LE, \
807 #define SI1145_INTENSITY_CHANNEL(_si) { \
808 .type = IIO_INTENSITY, \
809 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
810 BIT(IIO_CHAN_INFO_OFFSET) | \
811 BIT(IIO_CHAN_INFO_SCALE), \
812 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
813 .scan_type = SI1145_ST, \
815 .address = SI1145_REG_ALSVIS_DATA, \
818 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
819 .type = IIO_INTENSITY, \
820 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
821 BIT(IIO_CHAN_INFO_OFFSET) | \
822 BIT(IIO_CHAN_INFO_SCALE), \
823 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
825 .channel2 = IIO_MOD_LIGHT_IR, \
826 .scan_type = SI1145_ST, \
828 .address = SI1145_REG_ALSIR_DATA, \
831 #define SI1145_TEMP_CHANNEL(_si) { \
833 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
834 BIT(IIO_CHAN_INFO_OFFSET) | \
835 BIT(IIO_CHAN_INFO_SCALE), \
836 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
837 .scan_type = SI1145_ST, \
839 .address = SI1145_REG_AUX_DATA, \
842 #define SI1145_UV_CHANNEL(_si) { \
843 .type = IIO_UVINDEX, \
844 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
845 BIT(IIO_CHAN_INFO_SCALE), \
846 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
847 .scan_type = SI1145_ST, \
849 .address = SI1145_REG_AUX_DATA, \
852 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
853 .type = IIO_PROXIMITY, \
856 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
857 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
858 BIT(IIO_CHAN_INFO_OFFSET), \
859 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
860 .scan_type = SI1145_ST, \
862 .address = SI1145_REG_PS1_DATA + _ch * 2, \
865 #define SI1145_VOLTAGE_CHANNEL(_si) { \
866 .type = IIO_VOLTAGE, \
867 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
868 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
869 .scan_type = SI1145_ST, \
871 .address = SI1145_REG_AUX_DATA, \
874 #define SI1145_CURRENT_CHANNEL(_ch) { \
875 .type = IIO_CURRENT, \
880 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
883 static const struct iio_chan_spec si1132_channels
[] = {
884 SI1145_INTENSITY_CHANNEL(0),
885 SI1145_INTENSITY_IR_CHANNEL(1),
886 SI1145_TEMP_CHANNEL(2),
887 SI1145_VOLTAGE_CHANNEL(3),
888 SI1145_UV_CHANNEL(4),
889 IIO_CHAN_SOFT_TIMESTAMP(6),
892 static const struct iio_chan_spec si1141_channels
[] = {
893 SI1145_INTENSITY_CHANNEL(0),
894 SI1145_INTENSITY_IR_CHANNEL(1),
895 SI1145_PROXIMITY_CHANNEL(2, 0),
896 SI1145_TEMP_CHANNEL(3),
897 SI1145_VOLTAGE_CHANNEL(4),
898 IIO_CHAN_SOFT_TIMESTAMP(5),
899 SI1145_CURRENT_CHANNEL(0),
902 static const struct iio_chan_spec si1142_channels
[] = {
903 SI1145_INTENSITY_CHANNEL(0),
904 SI1145_INTENSITY_IR_CHANNEL(1),
905 SI1145_PROXIMITY_CHANNEL(2, 0),
906 SI1145_PROXIMITY_CHANNEL(3, 1),
907 SI1145_TEMP_CHANNEL(4),
908 SI1145_VOLTAGE_CHANNEL(5),
909 IIO_CHAN_SOFT_TIMESTAMP(6),
910 SI1145_CURRENT_CHANNEL(0),
911 SI1145_CURRENT_CHANNEL(1),
914 static const struct iio_chan_spec si1143_channels
[] = {
915 SI1145_INTENSITY_CHANNEL(0),
916 SI1145_INTENSITY_IR_CHANNEL(1),
917 SI1145_PROXIMITY_CHANNEL(2, 0),
918 SI1145_PROXIMITY_CHANNEL(3, 1),
919 SI1145_PROXIMITY_CHANNEL(4, 2),
920 SI1145_TEMP_CHANNEL(5),
921 SI1145_VOLTAGE_CHANNEL(6),
922 IIO_CHAN_SOFT_TIMESTAMP(7),
923 SI1145_CURRENT_CHANNEL(0),
924 SI1145_CURRENT_CHANNEL(1),
925 SI1145_CURRENT_CHANNEL(2),
928 static const struct iio_chan_spec si1145_channels
[] = {
929 SI1145_INTENSITY_CHANNEL(0),
930 SI1145_INTENSITY_IR_CHANNEL(1),
931 SI1145_PROXIMITY_CHANNEL(2, 0),
932 SI1145_TEMP_CHANNEL(3),
933 SI1145_VOLTAGE_CHANNEL(4),
934 SI1145_UV_CHANNEL(5),
935 IIO_CHAN_SOFT_TIMESTAMP(6),
936 SI1145_CURRENT_CHANNEL(0),
939 static const struct iio_chan_spec si1146_channels
[] = {
940 SI1145_INTENSITY_CHANNEL(0),
941 SI1145_INTENSITY_IR_CHANNEL(1),
942 SI1145_TEMP_CHANNEL(2),
943 SI1145_VOLTAGE_CHANNEL(3),
944 SI1145_UV_CHANNEL(4),
945 SI1145_PROXIMITY_CHANNEL(5, 0),
946 SI1145_PROXIMITY_CHANNEL(6, 1),
947 IIO_CHAN_SOFT_TIMESTAMP(7),
948 SI1145_CURRENT_CHANNEL(0),
949 SI1145_CURRENT_CHANNEL(1),
952 static const struct iio_chan_spec si1147_channels
[] = {
953 SI1145_INTENSITY_CHANNEL(0),
954 SI1145_INTENSITY_IR_CHANNEL(1),
955 SI1145_PROXIMITY_CHANNEL(2, 0),
956 SI1145_PROXIMITY_CHANNEL(3, 1),
957 SI1145_PROXIMITY_CHANNEL(4, 2),
958 SI1145_TEMP_CHANNEL(5),
959 SI1145_VOLTAGE_CHANNEL(6),
960 SI1145_UV_CHANNEL(7),
961 IIO_CHAN_SOFT_TIMESTAMP(8),
962 SI1145_CURRENT_CHANNEL(0),
963 SI1145_CURRENT_CHANNEL(1),
964 SI1145_CURRENT_CHANNEL(2),
967 static struct attribute
*si1132_attributes
[] = {
968 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
969 &iio_const_attr_in_intensity_ir_scale_available
.dev_attr
.attr
,
973 static struct attribute
*si114x_attributes
[] = {
974 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
975 &iio_const_attr_in_intensity_ir_scale_available
.dev_attr
.attr
,
976 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
980 static const struct attribute_group si1132_attribute_group
= {
981 .attrs
= si1132_attributes
,
984 static const struct attribute_group si114x_attribute_group
= {
985 .attrs
= si114x_attributes
,
989 static const struct iio_info si1132_info
= {
990 .read_raw
= si1145_read_raw
,
991 .write_raw
= si1145_write_raw
,
992 .attrs
= &si1132_attribute_group
,
995 static const struct iio_info si114x_info
= {
996 .read_raw
= si1145_read_raw
,
997 .write_raw
= si1145_write_raw
,
998 .attrs
= &si114x_attribute_group
,
1001 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1002 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1004 static const struct si1145_part_info si1145_part_info
[] = {
1005 [SI1132
] = SI1145_PART(0x32, &si1132_info
, si1132_channels
, 0, true),
1006 [SI1141
] = SI1145_PART(0x41, &si114x_info
, si1141_channels
, 1, false),
1007 [SI1142
] = SI1145_PART(0x42, &si114x_info
, si1142_channels
, 2, false),
1008 [SI1143
] = SI1145_PART(0x43, &si114x_info
, si1143_channels
, 3, false),
1009 [SI1145
] = SI1145_PART(0x45, &si114x_info
, si1145_channels
, 1, true),
1010 [SI1146
] = SI1145_PART(0x46, &si114x_info
, si1146_channels
, 2, true),
1011 [SI1147
] = SI1145_PART(0x47, &si114x_info
, si1147_channels
, 3, true),
1014 static int si1145_initialize(struct si1145_data
*data
)
1016 struct i2c_client
*client
= data
->client
;
1019 ret
= i2c_smbus_write_byte_data(client
, SI1145_REG_COMMAND
,
1023 msleep(SI1145_COMMAND_TIMEOUT_MS
);
1025 /* Hardware key, magic value */
1026 ret
= i2c_smbus_write_byte_data(client
, SI1145_REG_HW_KEY
, 0x17);
1029 msleep(SI1145_COMMAND_TIMEOUT_MS
);
1031 /* Turn off autonomous mode */
1032 ret
= si1145_set_meas_rate(data
, 0);
1036 /* Initialize sampling freq to 10 Hz */
1037 ret
= si1145_store_samp_freq(data
, 10);
1041 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1042 switch (data
->part_info
->num_leds
) {
1044 ret
= i2c_smbus_write_byte_data(client
,
1046 SI1145_LED_CURRENT_45mA
);
1051 ret
= i2c_smbus_write_byte_data(client
,
1052 SI1145_REG_PS_LED21
,
1053 (SI1145_LED_CURRENT_45mA
<< 4) |
1054 SI1145_LED_CURRENT_45mA
);
1057 ret
= i2c_smbus_write_byte_data(client
,
1058 SI1145_REG_PS_LED21
,
1059 SI1145_LED_CURRENT_45mA
);
1068 /* Set normal proximity measurement mode */
1069 ret
= si1145_param_set(data
, SI1145_PARAM_PS_ADC_MISC
,
1070 SI1145_PS_ADC_MODE_NORMAL
);
1074 ret
= si1145_param_set(data
, SI1145_PARAM_PS_ADC_GAIN
, 0x01);
1078 /* ADC_COUNTER should be one complement of ADC_GAIN */
1079 ret
= si1145_param_set(data
, SI1145_PARAM_PS_ADC_COUNTER
, 0x06 << 4);
1083 /* Set ALS visible measurement mode */
1084 ret
= si1145_param_set(data
, SI1145_PARAM_ALSVIS_ADC_MISC
,
1085 SI1145_ADC_MISC_RANGE
);
1089 ret
= si1145_param_set(data
, SI1145_PARAM_ALSVIS_ADC_GAIN
, 0x03);
1093 ret
= si1145_param_set(data
, SI1145_PARAM_ALSVIS_ADC_COUNTER
,
1098 /* Set ALS IR measurement mode */
1099 ret
= si1145_param_set(data
, SI1145_PARAM_ALSIR_ADC_MISC
,
1100 SI1145_ADC_MISC_RANGE
);
1104 ret
= si1145_param_set(data
, SI1145_PARAM_ALSIR_ADC_GAIN
, 0x01);
1108 ret
= si1145_param_set(data
, SI1145_PARAM_ALSIR_ADC_COUNTER
,
1114 * Initialize UCOEF to default values in datasheet
1115 * These registers are normally zero on reset
1117 if (data
->part_info
== &si1145_part_info
[SI1132
] ||
1118 data
->part_info
== &si1145_part_info
[SI1145
] ||
1119 data
->part_info
== &si1145_part_info
[SI1146
] ||
1120 data
->part_info
== &si1145_part_info
[SI1147
]) {
1121 ret
= i2c_smbus_write_byte_data(data
->client
,
1123 SI1145_UCOEF1_DEFAULT
);
1126 ret
= i2c_smbus_write_byte_data(data
->client
,
1127 SI1145_REG_UCOEF2
, SI1145_UCOEF2_DEFAULT
);
1130 ret
= i2c_smbus_write_byte_data(data
->client
,
1131 SI1145_REG_UCOEF3
, SI1145_UCOEF3_DEFAULT
);
1134 ret
= i2c_smbus_write_byte_data(data
->client
,
1135 SI1145_REG_UCOEF4
, SI1145_UCOEF4_DEFAULT
);
1144 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1145 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1146 * mode reprograms the channels list anyway...
1148 static int si1145_buffer_preenable(struct iio_dev
*indio_dev
)
1150 struct si1145_data
*data
= iio_priv(indio_dev
);
1153 mutex_lock(&data
->lock
);
1154 ret
= si1145_set_chlist(indio_dev
, *indio_dev
->active_scan_mask
);
1155 mutex_unlock(&data
->lock
);
1160 static bool si1145_validate_scan_mask(struct iio_dev
*indio_dev
,
1161 const unsigned long *scan_mask
)
1163 struct si1145_data
*data
= iio_priv(indio_dev
);
1164 unsigned int count
= 0;
1167 /* Check that at most one AUX channel is enabled */
1168 for_each_set_bit(i
, scan_mask
, data
->part_info
->num_channels
) {
1169 if (indio_dev
->channels
[i
].address
== SI1145_REG_AUX_DATA
)
1176 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops
= {
1177 .preenable
= si1145_buffer_preenable
,
1178 .postenable
= iio_triggered_buffer_postenable
,
1179 .predisable
= iio_triggered_buffer_predisable
,
1180 .validate_scan_mask
= si1145_validate_scan_mask
,
1184 * si1145_trigger_set_state() - Set trigger state
1186 * When not using triggers interrupts are disabled and measurement rate is
1187 * set to zero in order to minimize power consumption.
1189 static int si1145_trigger_set_state(struct iio_trigger
*trig
, bool state
)
1191 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1192 struct si1145_data
*data
= iio_priv(indio_dev
);
1195 mutex_lock(&data
->lock
);
1198 data
->autonomous
= true;
1199 err
= i2c_smbus_write_byte_data(data
->client
,
1200 SI1145_REG_INT_CFG
, SI1145_INT_CFG_OE
);
1203 err
= i2c_smbus_write_byte_data(data
->client
,
1204 SI1145_REG_IRQ_ENABLE
, SI1145_MASK_ALL_IE
);
1207 err
= si1145_set_meas_rate(data
, data
->meas_rate
);
1210 err
= si1145_command(data
, SI1145_CMD_PSALS_AUTO
);
1215 /* Disable as much as possible skipping errors */
1216 ret
= si1145_command(data
, SI1145_CMD_PSALS_PAUSE
);
1217 if (ret
< 0 && !err
)
1219 ret
= si1145_set_meas_rate(data
, 0);
1220 if (ret
< 0 && !err
)
1222 ret
= i2c_smbus_write_byte_data(data
->client
,
1223 SI1145_REG_IRQ_ENABLE
, 0);
1224 if (ret
< 0 && !err
)
1226 ret
= i2c_smbus_write_byte_data(data
->client
,
1227 SI1145_REG_INT_CFG
, 0);
1228 if (ret
< 0 && !err
)
1230 data
->autonomous
= false;
1233 mutex_unlock(&data
->lock
);
1237 static const struct iio_trigger_ops si1145_trigger_ops
= {
1238 .set_trigger_state
= si1145_trigger_set_state
,
1241 static int si1145_probe_trigger(struct iio_dev
*indio_dev
)
1243 struct si1145_data
*data
= iio_priv(indio_dev
);
1244 struct i2c_client
*client
= data
->client
;
1245 struct iio_trigger
*trig
;
1248 trig
= devm_iio_trigger_alloc(&client
->dev
,
1249 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
1253 trig
->dev
.parent
= &client
->dev
;
1254 trig
->ops
= &si1145_trigger_ops
;
1255 iio_trigger_set_drvdata(trig
, indio_dev
);
1257 ret
= devm_request_irq(&client
->dev
, client
->irq
,
1258 iio_trigger_generic_data_rdy_poll
,
1259 IRQF_TRIGGER_FALLING
,
1263 dev_err(&client
->dev
, "irq request failed\n");
1267 ret
= iio_trigger_register(trig
);
1272 indio_dev
->trig
= iio_trigger_get(data
->trig
);
1277 static void si1145_remove_trigger(struct iio_dev
*indio_dev
)
1279 struct si1145_data
*data
= iio_priv(indio_dev
);
1282 iio_trigger_unregister(data
->trig
);
1287 static int si1145_probe(struct i2c_client
*client
,
1288 const struct i2c_device_id
*id
)
1290 struct si1145_data
*data
;
1291 struct iio_dev
*indio_dev
;
1292 u8 part_id
, rev_id
, seq_id
;
1295 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1299 data
= iio_priv(indio_dev
);
1300 i2c_set_clientdata(client
, indio_dev
);
1301 data
->client
= client
;
1302 data
->part_info
= &si1145_part_info
[id
->driver_data
];
1304 part_id
= ret
= i2c_smbus_read_byte_data(data
->client
,
1305 SI1145_REG_PART_ID
);
1308 rev_id
= ret
= i2c_smbus_read_byte_data(data
->client
,
1312 seq_id
= ret
= i2c_smbus_read_byte_data(data
->client
,
1316 dev_info(&client
->dev
, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1317 part_id
, rev_id
, seq_id
);
1318 if (part_id
!= data
->part_info
->part
) {
1319 dev_err(&client
->dev
, "part ID mismatch got %#02hhx, expected %#02x\n",
1320 part_id
, data
->part_info
->part
);
1324 indio_dev
->dev
.parent
= &client
->dev
;
1325 indio_dev
->name
= id
->name
;
1326 indio_dev
->channels
= data
->part_info
->channels
;
1327 indio_dev
->num_channels
= data
->part_info
->num_channels
;
1328 indio_dev
->info
= data
->part_info
->iio_info
;
1329 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1331 mutex_init(&data
->lock
);
1332 mutex_init(&data
->cmdlock
);
1334 ret
= si1145_initialize(data
);
1338 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
1339 si1145_trigger_handler
, &si1145_buffer_setup_ops
);
1344 ret
= si1145_probe_trigger(indio_dev
);
1346 goto error_free_buffer
;
1348 dev_info(&client
->dev
, "no irq, using polling\n");
1351 ret
= iio_device_register(indio_dev
);
1353 goto error_free_trigger
;
1358 si1145_remove_trigger(indio_dev
);
1360 iio_triggered_buffer_cleanup(indio_dev
);
1365 static const struct i2c_device_id si1145_ids
[] = {
1366 { "si1132", SI1132
},
1367 { "si1141", SI1141
},
1368 { "si1142", SI1142
},
1369 { "si1143", SI1143
},
1370 { "si1145", SI1145
},
1371 { "si1146", SI1146
},
1372 { "si1147", SI1147
},
1375 MODULE_DEVICE_TABLE(i2c
, si1145_ids
);
1377 static int si1145_remove(struct i2c_client
*client
)
1379 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1381 iio_device_unregister(indio_dev
);
1382 si1145_remove_trigger(indio_dev
);
1383 iio_triggered_buffer_cleanup(indio_dev
);
1388 static struct i2c_driver si1145_driver
= {
1392 .probe
= si1145_probe
,
1393 .remove
= si1145_remove
,
1394 .id_table
= si1145_ids
,
1397 module_i2c_driver(si1145_driver
);
1399 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1400 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1401 MODULE_LICENSE("GPL");