1 // SPDX-License-Identifier: GPL-2.0-only
3 * si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient
4 * light, UV index and proximity sensors
6 * Copyright 2014-16 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
7 * Copyright 2016 Crestez Dan Leonard <leonard.crestez@intel.com>
9 * SI1132 (7-bit I2C slave address 0x60)
10 * SI1141/2/3 (7-bit I2C slave address 0x5a)
11 * SI1145/6/6 (7-bit I2C slave address 0x60)
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/irq.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/util_macros.h>
29 #define SI1145_REG_PART_ID 0x00
30 #define SI1145_REG_REV_ID 0x01
31 #define SI1145_REG_SEQ_ID 0x02
32 #define SI1145_REG_INT_CFG 0x03
33 #define SI1145_REG_IRQ_ENABLE 0x04
34 #define SI1145_REG_IRQ_MODE 0x05
35 #define SI1145_REG_HW_KEY 0x07
36 #define SI1145_REG_MEAS_RATE 0x08
37 #define SI1145_REG_PS_LED21 0x0f
38 #define SI1145_REG_PS_LED3 0x10
39 #define SI1145_REG_UCOEF1 0x13
40 #define SI1145_REG_UCOEF2 0x14
41 #define SI1145_REG_UCOEF3 0x15
42 #define SI1145_REG_UCOEF4 0x16
43 #define SI1145_REG_PARAM_WR 0x17
44 #define SI1145_REG_COMMAND 0x18
45 #define SI1145_REG_RESPONSE 0x20
46 #define SI1145_REG_IRQ_STATUS 0x21
47 #define SI1145_REG_ALSVIS_DATA 0x22
48 #define SI1145_REG_ALSIR_DATA 0x24
49 #define SI1145_REG_PS1_DATA 0x26
50 #define SI1145_REG_PS2_DATA 0x28
51 #define SI1145_REG_PS3_DATA 0x2a
52 #define SI1145_REG_AUX_DATA 0x2c
53 #define SI1145_REG_PARAM_RD 0x2e
54 #define SI1145_REG_CHIP_STAT 0x30
56 #define SI1145_UCOEF1_DEFAULT 0x7b
57 #define SI1145_UCOEF2_DEFAULT 0x6b
58 #define SI1145_UCOEF3_DEFAULT 0x01
59 #define SI1145_UCOEF4_DEFAULT 0x00
61 /* Helper to figure out PS_LED register / shift per channel */
62 #define SI1145_PS_LED_REG(ch) \
63 (((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21)
64 #define SI1145_PS_LED_SHIFT(ch) \
67 /* Parameter offsets */
68 #define SI1145_PARAM_CHLIST 0x01
69 #define SI1145_PARAM_PSLED12_SELECT 0x02
70 #define SI1145_PARAM_PSLED3_SELECT 0x03
71 #define SI1145_PARAM_PS_ENCODING 0x05
72 #define SI1145_PARAM_ALS_ENCODING 0x06
73 #define SI1145_PARAM_PS1_ADC_MUX 0x07
74 #define SI1145_PARAM_PS2_ADC_MUX 0x08
75 #define SI1145_PARAM_PS3_ADC_MUX 0x09
76 #define SI1145_PARAM_PS_ADC_COUNTER 0x0a
77 #define SI1145_PARAM_PS_ADC_GAIN 0x0b
78 #define SI1145_PARAM_PS_ADC_MISC 0x0c
79 #define SI1145_PARAM_ALS_ADC_MUX 0x0d
80 #define SI1145_PARAM_ALSIR_ADC_MUX 0x0e
81 #define SI1145_PARAM_AUX_ADC_MUX 0x0f
82 #define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10
83 #define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11
84 #define SI1145_PARAM_ALSVIS_ADC_MISC 0x12
85 #define SI1145_PARAM_LED_RECOVERY 0x1c
86 #define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d
87 #define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e
88 #define SI1145_PARAM_ALSIR_ADC_MISC 0x1f
89 #define SI1145_PARAM_ADC_OFFSET 0x1a
91 /* Channel enable masks for CHLIST parameter */
92 #define SI1145_CHLIST_EN_PS1 BIT(0)
93 #define SI1145_CHLIST_EN_PS2 BIT(1)
94 #define SI1145_CHLIST_EN_PS3 BIT(2)
95 #define SI1145_CHLIST_EN_ALSVIS BIT(4)
96 #define SI1145_CHLIST_EN_ALSIR BIT(5)
97 #define SI1145_CHLIST_EN_AUX BIT(6)
98 #define SI1145_CHLIST_EN_UV BIT(7)
100 /* Proximity measurement mode for ADC_MISC parameter */
101 #define SI1145_PS_ADC_MODE_NORMAL BIT(2)
102 /* Signal range mask for ADC_MISC parameter */
103 #define SI1145_ADC_MISC_RANGE BIT(5)
105 /* Commands for REG_COMMAND */
106 #define SI1145_CMD_NOP 0x00
107 #define SI1145_CMD_RESET 0x01
108 #define SI1145_CMD_PS_FORCE 0x05
109 #define SI1145_CMD_ALS_FORCE 0x06
110 #define SI1145_CMD_PSALS_FORCE 0x07
111 #define SI1145_CMD_PS_PAUSE 0x09
112 #define SI1145_CMD_ALS_PAUSE 0x0a
113 #define SI1145_CMD_PSALS_PAUSE 0x0b
114 #define SI1145_CMD_PS_AUTO 0x0d
115 #define SI1145_CMD_ALS_AUTO 0x0e
116 #define SI1145_CMD_PSALS_AUTO 0x0f
117 #define SI1145_CMD_PARAM_QUERY 0x80
118 #define SI1145_CMD_PARAM_SET 0xa0
120 #define SI1145_RSP_INVALID_SETTING 0x80
121 #define SI1145_RSP_COUNTER_MASK 0x0F
123 /* Minimum sleep after each command to ensure it's received */
124 #define SI1145_COMMAND_MINSLEEP_MS 5
125 /* Return -ETIMEDOUT after this long */
126 #define SI1145_COMMAND_TIMEOUT_MS 25
128 /* Interrupt configuration masks for INT_CFG register */
129 #define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */
130 #define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */
132 /* Interrupt enable masks for IRQ_ENABLE register */
133 #define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0))
135 #define SI1145_MUX_TEMP 0x65
136 #define SI1145_MUX_VDD 0x75
138 /* Proximity LED current; see Table 2 in datasheet */
139 #define SI1145_LED_CURRENT_45mA 0x04
151 struct si1145_part_info {
153 const struct iio_info *iio_info;
154 const struct iio_chan_spec *channels;
155 unsigned int num_channels;
156 unsigned int num_leds;
157 bool uncompressed_meas_rate;
161 * struct si1145_data - si1145 chip state data
162 * @client: I2C client
163 * @lock: mutex to protect shared state.
164 * @cmdlock: Low-level mutex to protect command execution only
165 * @rsp_seq: Next expected response number or -1 if counter reset required
166 * @scan_mask: Saved scan mask to avoid duplicate set_chlist
167 * @autonomous: If automatic measurements are active (for buffer support)
168 * @part_info: Part information
169 * @trig: Pointer to iio trigger
170 * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode
173 struct i2c_client *client;
175 struct mutex cmdlock;
177 const struct si1145_part_info *part_info;
178 unsigned long scan_mask;
180 struct iio_trigger *trig;
185 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
187 * Does not modify data->rsp_seq
189 * Return: 0 on success and -errno on error.
191 static int __si1145_command_reset(struct si1145_data *data)
193 struct device *dev = &data->client->dev;
194 unsigned long stop_jiffies;
197 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
201 msleep(SI1145_COMMAND_MINSLEEP_MS);
203 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
205 ret = i2c_smbus_read_byte_data(data->client,
206 SI1145_REG_RESPONSE);
209 if (time_after(jiffies, stop_jiffies)) {
210 dev_warn(dev, "timeout on reset\n");
213 msleep(SI1145_COMMAND_MINSLEEP_MS);
219 * si1145_command() - Execute a command and poll the response register
221 * All conversion overflows are reported as -EOVERFLOW
222 * INVALID_SETTING is reported as -EINVAL
223 * Timeouts are reported as -ETIMEDOUT
225 * Return: 0 on success or -errno on failure
227 static int si1145_command(struct si1145_data *data, u8 cmd)
229 struct device *dev = &data->client->dev;
230 unsigned long stop_jiffies;
233 mutex_lock(&data->cmdlock);
235 if (data->rsp_seq < 0) {
236 ret = __si1145_command_reset(data);
238 dev_err(dev, "failed to reset command counter, ret=%d\n",
245 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
247 dev_warn(dev, "failed to write command, ret=%d\n", ret);
250 /* Sleep a little to ensure the command is received */
251 msleep(SI1145_COMMAND_MINSLEEP_MS);
253 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
255 ret = i2c_smbus_read_byte_data(data->client,
256 SI1145_REG_RESPONSE);
258 dev_warn(dev, "failed to read response, ret=%d\n", ret);
262 if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
263 if (ret == data->rsp_seq) {
264 if (time_after(jiffies, stop_jiffies)) {
265 dev_warn(dev, "timeout on command %#02hhx\n",
270 msleep(SI1145_COMMAND_MINSLEEP_MS);
273 if (ret == ((data->rsp_seq + 1) &
274 SI1145_RSP_COUNTER_MASK)) {
279 dev_warn(dev, "unexpected response counter %d instead of %d\n",
280 ret, (data->rsp_seq + 1) &
281 SI1145_RSP_COUNTER_MASK);
284 if (ret == SI1145_RSP_INVALID_SETTING) {
285 dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
289 /* All overflows are treated identically */
290 dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
296 /* Force a counter reset next time */
302 mutex_unlock(&data->cmdlock);
307 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
312 ret = i2c_smbus_write_byte_data(data->client,
313 SI1145_REG_PARAM_WR, value);
317 return si1145_command(data, op | (param & 0x1F));
320 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
322 return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
325 /* Set param. Returns negative errno or current value */
326 static int si1145_param_query(struct si1145_data *data, u8 param)
330 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
334 return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
337 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
338 static u16 si1145_uncompress(u8 x)
346 exponent = (x & 0xf0) >> 4;
347 result = 0x10 | (x & 0x0f);
350 return result << (exponent - 4);
351 return result >> (4 - exponent);
354 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
355 static u8 si1145_compress(u16 x)
374 significand = x << (4 - exponent);
375 return (exponent << 4) | (significand & 0xF);
378 significand = x >> (exponent - 5);
379 if (significand & 1) {
381 if (significand & 0x0040) {
387 return (exponent << 4) | ((significand >> 1) & 0xF);
390 /* Write meas_rate in hardware */
391 static int si1145_set_meas_rate(struct si1145_data *data, int interval)
393 if (data->part_info->uncompressed_meas_rate)
394 return i2c_smbus_write_word_data(data->client,
395 SI1145_REG_MEAS_RATE, interval);
397 return i2c_smbus_write_byte_data(data->client,
398 SI1145_REG_MEAS_RATE, interval);
401 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
404 if (data->part_info->uncompressed_meas_rate)
405 *val2 = data->meas_rate;
407 *val2 = si1145_uncompress(data->meas_rate);
408 return IIO_VAL_FRACTIONAL;
411 /* Set the samp freq in driver private data */
412 static int si1145_store_samp_freq(struct si1145_data *data, int val)
417 if (val <= 0 || val > 32000)
419 meas_rate = 32000 / val;
421 mutex_lock(&data->lock);
422 if (data->autonomous) {
423 ret = si1145_set_meas_rate(data, meas_rate);
427 if (data->part_info->uncompressed_meas_rate)
428 data->meas_rate = meas_rate;
430 data->meas_rate = si1145_compress(meas_rate);
433 mutex_unlock(&data->lock);
438 static irqreturn_t si1145_trigger_handler(int irq, void *private)
440 struct iio_poll_func *pf = private;
441 struct iio_dev *indio_dev = pf->indio_dev;
442 struct si1145_data *data = iio_priv(indio_dev);
444 * Maximum buffer size:
445 * 6*2 bytes channels data + 4 bytes alignment +
453 if (!data->autonomous) {
454 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
455 if (ret < 0 && ret != -EOVERFLOW)
458 irq_status = ret = i2c_smbus_read_byte_data(data->client,
459 SI1145_REG_IRQ_STATUS);
462 if (!(irq_status & SI1145_MASK_ALL_IE))
466 for_each_set_bit(i, indio_dev->active_scan_mask,
467 indio_dev->masklength) {
470 while (i + run < indio_dev->masklength) {
471 if (!test_bit(i + run, indio_dev->active_scan_mask))
473 if (indio_dev->channels[i + run].address !=
474 indio_dev->channels[i].address + 2 * run)
479 ret = i2c_smbus_read_i2c_block_data_or_emulated(
480 data->client, indio_dev->channels[i].address,
481 sizeof(u16) * run, &buffer[j]);
484 j += run * sizeof(u16);
488 if (data->autonomous) {
489 ret = i2c_smbus_write_byte_data(data->client,
490 SI1145_REG_IRQ_STATUS,
491 irq_status & SI1145_MASK_ALL_IE);
496 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
497 iio_get_time_ns(indio_dev));
500 iio_trigger_notify_done(indio_dev->trig);
504 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
506 struct si1145_data *data = iio_priv(indio_dev);
511 /* channel list already set, no need to reprogram */
512 if (data->scan_mask == scan_mask)
515 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
516 switch (indio_dev->channels[i].address) {
517 case SI1145_REG_ALSVIS_DATA:
518 reg |= SI1145_CHLIST_EN_ALSVIS;
520 case SI1145_REG_ALSIR_DATA:
521 reg |= SI1145_CHLIST_EN_ALSIR;
523 case SI1145_REG_PS1_DATA:
524 reg |= SI1145_CHLIST_EN_PS1;
526 case SI1145_REG_PS2_DATA:
527 reg |= SI1145_CHLIST_EN_PS2;
529 case SI1145_REG_PS3_DATA:
530 reg |= SI1145_CHLIST_EN_PS3;
532 case SI1145_REG_AUX_DATA:
533 switch (indio_dev->channels[i].type) {
535 reg |= SI1145_CHLIST_EN_UV;
538 reg |= SI1145_CHLIST_EN_AUX;
539 if (indio_dev->channels[i].type == IIO_TEMP)
540 mux = SI1145_MUX_TEMP;
542 mux = SI1145_MUX_VDD;
543 ret = si1145_param_set(data,
544 SI1145_PARAM_AUX_ADC_MUX, mux);
553 data->scan_mask = scan_mask;
554 ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
556 return ret < 0 ? ret : 0;
559 static int si1145_measure(struct iio_dev *indio_dev,
560 struct iio_chan_spec const *chan)
562 struct si1145_data *data = iio_priv(indio_dev);
566 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
570 cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
571 SI1145_CMD_ALS_FORCE;
572 ret = si1145_command(data, cmd);
573 if (ret < 0 && ret != -EOVERFLOW)
576 return i2c_smbus_read_word_data(data->client, chan->address);
580 * Conversion between iio scale and ADC_GAIN values
581 * These could be further adjusted but proximity/intensity are dimensionless
583 static const int si1145_proximity_scale_available[] = {
584 128, 64, 32, 16, 8, 4};
585 static const int si1145_intensity_scale_available[] = {
586 128, 64, 32, 16, 8, 4, 2, 1};
587 static IIO_CONST_ATTR(in_proximity_scale_available,
589 static IIO_CONST_ATTR(in_intensity_scale_available,
590 "128 64 32 16 8 4 2 1");
591 static IIO_CONST_ATTR(in_intensity_ir_scale_available,
592 "128 64 32 16 8 4 2 1");
594 static int si1145_scale_from_adcgain(int regval)
596 return 128 >> regval;
599 static int si1145_proximity_adcgain_from_scale(int val, int val2)
601 val = find_closest_descending(val, si1145_proximity_scale_available,
602 ARRAY_SIZE(si1145_proximity_scale_available));
603 if (val < 0 || val > 5 || val2 != 0)
609 static int si1145_intensity_adcgain_from_scale(int val, int val2)
611 val = find_closest_descending(val, si1145_intensity_scale_available,
612 ARRAY_SIZE(si1145_intensity_scale_available));
613 if (val < 0 || val > 7 || val2 != 0)
619 static int si1145_read_raw(struct iio_dev *indio_dev,
620 struct iio_chan_spec const *chan,
621 int *val, int *val2, long mask)
623 struct si1145_data *data = iio_priv(indio_dev);
628 case IIO_CHAN_INFO_RAW:
629 switch (chan->type) {
635 ret = iio_device_claim_direct_mode(indio_dev);
638 ret = si1145_measure(indio_dev, chan);
639 iio_device_release_direct_mode(indio_dev);
648 ret = i2c_smbus_read_byte_data(data->client,
649 SI1145_PS_LED_REG(chan->channel));
653 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
660 case IIO_CHAN_INFO_SCALE:
661 switch (chan->type) {
663 reg = SI1145_PARAM_PS_ADC_GAIN;
666 if (chan->channel2 == IIO_MOD_LIGHT_IR)
667 reg = SI1145_PARAM_ALSIR_ADC_GAIN;
669 reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
674 return IIO_VAL_INT_PLUS_MICRO;
678 return IIO_VAL_INT_PLUS_MICRO;
683 ret = si1145_param_query(data, reg);
687 *val = si1145_scale_from_adcgain(ret & 0x07);
690 case IIO_CHAN_INFO_OFFSET:
691 switch (chan->type) {
694 * -ADC offset - ADC counts @ 25°C -
695 * 35 * ADC counts / °C
697 *val = -256 - 11136 + 25 * 35;
701 * All ADC measurements have are by default offset
705 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
708 *val = -si1145_uncompress(ret);
711 case IIO_CHAN_INFO_SAMP_FREQ:
712 return si1145_read_samp_freq(data, val, val2);
718 static int si1145_write_raw(struct iio_dev *indio_dev,
719 struct iio_chan_spec const *chan,
720 int val, int val2, long mask)
722 struct si1145_data *data = iio_priv(indio_dev);
723 u8 reg1, reg2, shift;
727 case IIO_CHAN_INFO_SCALE:
728 switch (chan->type) {
730 val = si1145_proximity_adcgain_from_scale(val, val2);
733 reg1 = SI1145_PARAM_PS_ADC_GAIN;
734 reg2 = SI1145_PARAM_PS_ADC_COUNTER;
737 val = si1145_intensity_adcgain_from_scale(val, val2);
740 if (chan->channel2 == IIO_MOD_LIGHT_IR) {
741 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
742 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
744 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
745 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
752 ret = iio_device_claim_direct_mode(indio_dev);
756 ret = si1145_param_set(data, reg1, val);
758 iio_device_release_direct_mode(indio_dev);
761 /* Set recovery period to one's complement of gain */
762 ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
763 iio_device_release_direct_mode(indio_dev);
765 case IIO_CHAN_INFO_RAW:
766 if (chan->type != IIO_CURRENT)
769 if (val < 0 || val > 15 || val2 != 0)
772 reg1 = SI1145_PS_LED_REG(chan->channel);
773 shift = SI1145_PS_LED_SHIFT(chan->channel);
775 ret = iio_device_claim_direct_mode(indio_dev);
779 ret = i2c_smbus_read_byte_data(data->client, reg1);
781 iio_device_release_direct_mode(indio_dev);
784 ret = i2c_smbus_write_byte_data(data->client, reg1,
785 (ret & ~(0x0f << shift)) |
786 ((val & 0x0f) << shift));
787 iio_device_release_direct_mode(indio_dev);
789 case IIO_CHAN_INFO_SAMP_FREQ:
790 return si1145_store_samp_freq(data, val);
796 #define SI1145_ST { \
800 .endianness = IIO_LE, \
803 #define SI1145_INTENSITY_CHANNEL(_si) { \
804 .type = IIO_INTENSITY, \
805 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
806 BIT(IIO_CHAN_INFO_OFFSET) | \
807 BIT(IIO_CHAN_INFO_SCALE), \
808 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
809 .scan_type = SI1145_ST, \
811 .address = SI1145_REG_ALSVIS_DATA, \
814 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
815 .type = IIO_INTENSITY, \
816 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
817 BIT(IIO_CHAN_INFO_OFFSET) | \
818 BIT(IIO_CHAN_INFO_SCALE), \
819 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
821 .channel2 = IIO_MOD_LIGHT_IR, \
822 .scan_type = SI1145_ST, \
824 .address = SI1145_REG_ALSIR_DATA, \
827 #define SI1145_TEMP_CHANNEL(_si) { \
829 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
830 BIT(IIO_CHAN_INFO_OFFSET) | \
831 BIT(IIO_CHAN_INFO_SCALE), \
832 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
833 .scan_type = SI1145_ST, \
835 .address = SI1145_REG_AUX_DATA, \
838 #define SI1145_UV_CHANNEL(_si) { \
839 .type = IIO_UVINDEX, \
840 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
841 BIT(IIO_CHAN_INFO_SCALE), \
842 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
843 .scan_type = SI1145_ST, \
845 .address = SI1145_REG_AUX_DATA, \
848 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
849 .type = IIO_PROXIMITY, \
852 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
853 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
854 BIT(IIO_CHAN_INFO_OFFSET), \
855 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
856 .scan_type = SI1145_ST, \
858 .address = SI1145_REG_PS1_DATA + _ch * 2, \
861 #define SI1145_VOLTAGE_CHANNEL(_si) { \
862 .type = IIO_VOLTAGE, \
863 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
864 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
865 .scan_type = SI1145_ST, \
867 .address = SI1145_REG_AUX_DATA, \
870 #define SI1145_CURRENT_CHANNEL(_ch) { \
871 .type = IIO_CURRENT, \
876 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
879 static const struct iio_chan_spec si1132_channels[] = {
880 SI1145_INTENSITY_CHANNEL(0),
881 SI1145_INTENSITY_IR_CHANNEL(1),
882 SI1145_TEMP_CHANNEL(2),
883 SI1145_VOLTAGE_CHANNEL(3),
884 SI1145_UV_CHANNEL(4),
885 IIO_CHAN_SOFT_TIMESTAMP(6),
888 static const struct iio_chan_spec si1141_channels[] = {
889 SI1145_INTENSITY_CHANNEL(0),
890 SI1145_INTENSITY_IR_CHANNEL(1),
891 SI1145_PROXIMITY_CHANNEL(2, 0),
892 SI1145_TEMP_CHANNEL(3),
893 SI1145_VOLTAGE_CHANNEL(4),
894 IIO_CHAN_SOFT_TIMESTAMP(5),
895 SI1145_CURRENT_CHANNEL(0),
898 static const struct iio_chan_spec si1142_channels[] = {
899 SI1145_INTENSITY_CHANNEL(0),
900 SI1145_INTENSITY_IR_CHANNEL(1),
901 SI1145_PROXIMITY_CHANNEL(2, 0),
902 SI1145_PROXIMITY_CHANNEL(3, 1),
903 SI1145_TEMP_CHANNEL(4),
904 SI1145_VOLTAGE_CHANNEL(5),
905 IIO_CHAN_SOFT_TIMESTAMP(6),
906 SI1145_CURRENT_CHANNEL(0),
907 SI1145_CURRENT_CHANNEL(1),
910 static const struct iio_chan_spec si1143_channels[] = {
911 SI1145_INTENSITY_CHANNEL(0),
912 SI1145_INTENSITY_IR_CHANNEL(1),
913 SI1145_PROXIMITY_CHANNEL(2, 0),
914 SI1145_PROXIMITY_CHANNEL(3, 1),
915 SI1145_PROXIMITY_CHANNEL(4, 2),
916 SI1145_TEMP_CHANNEL(5),
917 SI1145_VOLTAGE_CHANNEL(6),
918 IIO_CHAN_SOFT_TIMESTAMP(7),
919 SI1145_CURRENT_CHANNEL(0),
920 SI1145_CURRENT_CHANNEL(1),
921 SI1145_CURRENT_CHANNEL(2),
924 static const struct iio_chan_spec si1145_channels[] = {
925 SI1145_INTENSITY_CHANNEL(0),
926 SI1145_INTENSITY_IR_CHANNEL(1),
927 SI1145_PROXIMITY_CHANNEL(2, 0),
928 SI1145_TEMP_CHANNEL(3),
929 SI1145_VOLTAGE_CHANNEL(4),
930 SI1145_UV_CHANNEL(5),
931 IIO_CHAN_SOFT_TIMESTAMP(6),
932 SI1145_CURRENT_CHANNEL(0),
935 static const struct iio_chan_spec si1146_channels[] = {
936 SI1145_INTENSITY_CHANNEL(0),
937 SI1145_INTENSITY_IR_CHANNEL(1),
938 SI1145_TEMP_CHANNEL(2),
939 SI1145_VOLTAGE_CHANNEL(3),
940 SI1145_UV_CHANNEL(4),
941 SI1145_PROXIMITY_CHANNEL(5, 0),
942 SI1145_PROXIMITY_CHANNEL(6, 1),
943 IIO_CHAN_SOFT_TIMESTAMP(7),
944 SI1145_CURRENT_CHANNEL(0),
945 SI1145_CURRENT_CHANNEL(1),
948 static const struct iio_chan_spec si1147_channels[] = {
949 SI1145_INTENSITY_CHANNEL(0),
950 SI1145_INTENSITY_IR_CHANNEL(1),
951 SI1145_PROXIMITY_CHANNEL(2, 0),
952 SI1145_PROXIMITY_CHANNEL(3, 1),
953 SI1145_PROXIMITY_CHANNEL(4, 2),
954 SI1145_TEMP_CHANNEL(5),
955 SI1145_VOLTAGE_CHANNEL(6),
956 SI1145_UV_CHANNEL(7),
957 IIO_CHAN_SOFT_TIMESTAMP(8),
958 SI1145_CURRENT_CHANNEL(0),
959 SI1145_CURRENT_CHANNEL(1),
960 SI1145_CURRENT_CHANNEL(2),
963 static struct attribute *si1132_attributes[] = {
964 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
965 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
969 static struct attribute *si114x_attributes[] = {
970 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
971 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
972 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
976 static const struct attribute_group si1132_attribute_group = {
977 .attrs = si1132_attributes,
980 static const struct attribute_group si114x_attribute_group = {
981 .attrs = si114x_attributes,
985 static const struct iio_info si1132_info = {
986 .read_raw = si1145_read_raw,
987 .write_raw = si1145_write_raw,
988 .attrs = &si1132_attribute_group,
991 static const struct iio_info si114x_info = {
992 .read_raw = si1145_read_raw,
993 .write_raw = si1145_write_raw,
994 .attrs = &si114x_attribute_group,
997 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
998 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1000 static const struct si1145_part_info si1145_part_info[] = {
1001 [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1002 [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1003 [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1004 [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1005 [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1006 [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1007 [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1010 static int si1145_initialize(struct si1145_data *data)
1012 struct i2c_client *client = data->client;
1015 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1019 msleep(SI1145_COMMAND_TIMEOUT_MS);
1021 /* Hardware key, magic value */
1022 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1025 msleep(SI1145_COMMAND_TIMEOUT_MS);
1027 /* Turn off autonomous mode */
1028 ret = si1145_set_meas_rate(data, 0);
1032 /* Initialize sampling freq to 10 Hz */
1033 ret = si1145_store_samp_freq(data, 10);
1037 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1038 switch (data->part_info->num_leds) {
1040 ret = i2c_smbus_write_byte_data(client,
1042 SI1145_LED_CURRENT_45mA);
1047 ret = i2c_smbus_write_byte_data(client,
1048 SI1145_REG_PS_LED21,
1049 (SI1145_LED_CURRENT_45mA << 4) |
1050 SI1145_LED_CURRENT_45mA);
1053 ret = i2c_smbus_write_byte_data(client,
1054 SI1145_REG_PS_LED21,
1055 SI1145_LED_CURRENT_45mA);
1064 /* Set normal proximity measurement mode */
1065 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1066 SI1145_PS_ADC_MODE_NORMAL);
1070 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1074 /* ADC_COUNTER should be one complement of ADC_GAIN */
1075 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1079 /* Set ALS visible measurement mode */
1080 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1081 SI1145_ADC_MISC_RANGE);
1085 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1089 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1094 /* Set ALS IR measurement mode */
1095 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1096 SI1145_ADC_MISC_RANGE);
1100 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1104 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1110 * Initialize UCOEF to default values in datasheet
1111 * These registers are normally zero on reset
1113 if (data->part_info == &si1145_part_info[SI1132] ||
1114 data->part_info == &si1145_part_info[SI1145] ||
1115 data->part_info == &si1145_part_info[SI1146] ||
1116 data->part_info == &si1145_part_info[SI1147]) {
1117 ret = i2c_smbus_write_byte_data(data->client,
1119 SI1145_UCOEF1_DEFAULT);
1122 ret = i2c_smbus_write_byte_data(data->client,
1123 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1126 ret = i2c_smbus_write_byte_data(data->client,
1127 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1130 ret = i2c_smbus_write_byte_data(data->client,
1131 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1140 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1141 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1142 * mode reprograms the channels list anyway...
1144 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1146 struct si1145_data *data = iio_priv(indio_dev);
1149 mutex_lock(&data->lock);
1150 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1151 mutex_unlock(&data->lock);
1156 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1157 const unsigned long *scan_mask)
1159 struct si1145_data *data = iio_priv(indio_dev);
1160 unsigned int count = 0;
1163 /* Check that at most one AUX channel is enabled */
1164 for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1165 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1172 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1173 .preenable = si1145_buffer_preenable,
1174 .validate_scan_mask = si1145_validate_scan_mask,
1178 * si1145_trigger_set_state() - Set trigger state
1180 * When not using triggers interrupts are disabled and measurement rate is
1181 * set to zero in order to minimize power consumption.
1183 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1185 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1186 struct si1145_data *data = iio_priv(indio_dev);
1189 mutex_lock(&data->lock);
1192 data->autonomous = true;
1193 err = i2c_smbus_write_byte_data(data->client,
1194 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1197 err = i2c_smbus_write_byte_data(data->client,
1198 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1201 err = si1145_set_meas_rate(data, data->meas_rate);
1204 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1209 /* Disable as much as possible skipping errors */
1210 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1211 if (ret < 0 && !err)
1213 ret = si1145_set_meas_rate(data, 0);
1214 if (ret < 0 && !err)
1216 ret = i2c_smbus_write_byte_data(data->client,
1217 SI1145_REG_IRQ_ENABLE, 0);
1218 if (ret < 0 && !err)
1220 ret = i2c_smbus_write_byte_data(data->client,
1221 SI1145_REG_INT_CFG, 0);
1222 if (ret < 0 && !err)
1224 data->autonomous = false;
1227 mutex_unlock(&data->lock);
1231 static const struct iio_trigger_ops si1145_trigger_ops = {
1232 .set_trigger_state = si1145_trigger_set_state,
1235 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1237 struct si1145_data *data = iio_priv(indio_dev);
1238 struct i2c_client *client = data->client;
1239 struct iio_trigger *trig;
1242 trig = devm_iio_trigger_alloc(&client->dev,
1243 "%s-dev%d", indio_dev->name, indio_dev->id);
1247 trig->dev.parent = &client->dev;
1248 trig->ops = &si1145_trigger_ops;
1249 iio_trigger_set_drvdata(trig, indio_dev);
1251 ret = devm_request_irq(&client->dev, client->irq,
1252 iio_trigger_generic_data_rdy_poll,
1253 IRQF_TRIGGER_FALLING,
1257 dev_err(&client->dev, "irq request failed\n");
1261 ret = devm_iio_trigger_register(&client->dev, trig);
1266 indio_dev->trig = iio_trigger_get(data->trig);
1271 static int si1145_probe(struct i2c_client *client,
1272 const struct i2c_device_id *id)
1274 struct si1145_data *data;
1275 struct iio_dev *indio_dev;
1276 u8 part_id, rev_id, seq_id;
1279 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1283 data = iio_priv(indio_dev);
1284 i2c_set_clientdata(client, indio_dev);
1285 data->client = client;
1286 data->part_info = &si1145_part_info[id->driver_data];
1288 part_id = ret = i2c_smbus_read_byte_data(data->client,
1289 SI1145_REG_PART_ID);
1292 rev_id = ret = i2c_smbus_read_byte_data(data->client,
1296 seq_id = ret = i2c_smbus_read_byte_data(data->client,
1300 dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1301 part_id, rev_id, seq_id);
1302 if (part_id != data->part_info->part) {
1303 dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1304 part_id, data->part_info->part);
1308 indio_dev->name = id->name;
1309 indio_dev->channels = data->part_info->channels;
1310 indio_dev->num_channels = data->part_info->num_channels;
1311 indio_dev->info = data->part_info->iio_info;
1312 indio_dev->modes = INDIO_DIRECT_MODE;
1314 mutex_init(&data->lock);
1315 mutex_init(&data->cmdlock);
1317 ret = si1145_initialize(data);
1321 ret = devm_iio_triggered_buffer_setup(&client->dev,
1323 si1145_trigger_handler, &si1145_buffer_setup_ops);
1328 ret = si1145_probe_trigger(indio_dev);
1332 dev_info(&client->dev, "no irq, using polling\n");
1335 return devm_iio_device_register(&client->dev, indio_dev);
1338 static const struct i2c_device_id si1145_ids[] = {
1339 { "si1132", SI1132 },
1340 { "si1141", SI1141 },
1341 { "si1142", SI1142 },
1342 { "si1143", SI1143 },
1343 { "si1145", SI1145 },
1344 { "si1146", SI1146 },
1345 { "si1147", SI1147 },
1348 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1350 static struct i2c_driver si1145_driver = {
1354 .probe = si1145_probe,
1355 .id_table = si1145_ids,
1358 module_i2c_driver(si1145_driver);
1360 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1361 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1362 MODULE_LICENSE("GPL");