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
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
171 * @buffer: Used to pack data read from sensor.
174 struct i2c_client *client;
176 struct mutex cmdlock;
178 const struct si1145_part_info *part_info;
179 unsigned long scan_mask;
181 struct iio_trigger *trig;
184 * Ensure timestamp will be naturally aligned if present.
185 * Maximum buffer size (may be only partly used if not all
186 * channels are enabled):
187 * 6*2 bytes channels data + 4 bytes alignment +
190 u8 buffer[24] __aligned(8);
194 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
196 * Does not modify data->rsp_seq
198 * Return: 0 on success and -errno on error.
200 static int __si1145_command_reset(struct si1145_data *data)
202 struct device *dev = &data->client->dev;
203 unsigned long stop_jiffies;
206 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
210 msleep(SI1145_COMMAND_MINSLEEP_MS);
212 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
214 ret = i2c_smbus_read_byte_data(data->client,
215 SI1145_REG_RESPONSE);
218 if (time_after(jiffies, stop_jiffies)) {
219 dev_warn(dev, "timeout on reset\n");
222 msleep(SI1145_COMMAND_MINSLEEP_MS);
227 * si1145_command() - Execute a command and poll the response register
229 * All conversion overflows are reported as -EOVERFLOW
230 * INVALID_SETTING is reported as -EINVAL
231 * Timeouts are reported as -ETIMEDOUT
233 * Return: 0 on success or -errno on failure
235 static int si1145_command(struct si1145_data *data, u8 cmd)
237 struct device *dev = &data->client->dev;
238 unsigned long stop_jiffies;
241 mutex_lock(&data->cmdlock);
243 if (data->rsp_seq < 0) {
244 ret = __si1145_command_reset(data);
246 dev_err(dev, "failed to reset command counter, ret=%d\n",
253 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
255 dev_warn(dev, "failed to write command, ret=%d\n", ret);
258 /* Sleep a little to ensure the command is received */
259 msleep(SI1145_COMMAND_MINSLEEP_MS);
261 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
263 ret = i2c_smbus_read_byte_data(data->client,
264 SI1145_REG_RESPONSE);
266 dev_warn(dev, "failed to read response, ret=%d\n", ret);
270 if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
271 if (ret == data->rsp_seq) {
272 if (time_after(jiffies, stop_jiffies)) {
273 dev_warn(dev, "timeout on command 0x%02x\n",
278 msleep(SI1145_COMMAND_MINSLEEP_MS);
281 if (ret == ((data->rsp_seq + 1) &
282 SI1145_RSP_COUNTER_MASK)) {
287 dev_warn(dev, "unexpected response counter %d instead of %d\n",
288 ret, (data->rsp_seq + 1) &
289 SI1145_RSP_COUNTER_MASK);
292 if (ret == SI1145_RSP_INVALID_SETTING) {
293 dev_warn(dev, "INVALID_SETTING error on command 0x%02x\n",
297 /* All overflows are treated identically */
298 dev_dbg(dev, "overflow, ret=%d, cmd=0x%02x\n",
304 /* Force a counter reset next time */
310 mutex_unlock(&data->cmdlock);
315 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
320 ret = i2c_smbus_write_byte_data(data->client,
321 SI1145_REG_PARAM_WR, value);
325 return si1145_command(data, op | (param & 0x1F));
328 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
330 return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
333 /* Set param. Returns negative errno or current value */
334 static int si1145_param_query(struct si1145_data *data, u8 param)
338 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
342 return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
345 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
346 static u16 si1145_uncompress(u8 x)
354 exponent = (x & 0xf0) >> 4;
355 result = 0x10 | (x & 0x0f);
358 return result << (exponent - 4);
359 return result >> (4 - exponent);
362 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
363 static u8 si1145_compress(u16 x)
382 significand = x << (4 - exponent);
383 return (exponent << 4) | (significand & 0xF);
386 significand = x >> (exponent - 5);
387 if (significand & 1) {
389 if (significand & 0x0040) {
395 return (exponent << 4) | ((significand >> 1) & 0xF);
398 /* Write meas_rate in hardware */
399 static int si1145_set_meas_rate(struct si1145_data *data, int interval)
401 if (data->part_info->uncompressed_meas_rate)
402 return i2c_smbus_write_word_data(data->client,
403 SI1145_REG_MEAS_RATE, interval);
405 return i2c_smbus_write_byte_data(data->client,
406 SI1145_REG_MEAS_RATE, interval);
409 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
412 if (data->part_info->uncompressed_meas_rate)
413 *val2 = data->meas_rate;
415 *val2 = si1145_uncompress(data->meas_rate);
416 return IIO_VAL_FRACTIONAL;
419 /* Set the samp freq in driver private data */
420 static int si1145_store_samp_freq(struct si1145_data *data, int val)
425 if (val <= 0 || val > 32000)
427 meas_rate = 32000 / val;
429 mutex_lock(&data->lock);
430 if (data->autonomous) {
431 ret = si1145_set_meas_rate(data, meas_rate);
435 if (data->part_info->uncompressed_meas_rate)
436 data->meas_rate = meas_rate;
438 data->meas_rate = si1145_compress(meas_rate);
441 mutex_unlock(&data->lock);
446 static irqreturn_t si1145_trigger_handler(int irq, void *private)
448 struct iio_poll_func *pf = private;
449 struct iio_dev *indio_dev = pf->indio_dev;
450 struct si1145_data *data = iio_priv(indio_dev);
455 if (!data->autonomous) {
456 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
457 if (ret < 0 && ret != -EOVERFLOW)
460 irq_status = ret = i2c_smbus_read_byte_data(data->client,
461 SI1145_REG_IRQ_STATUS);
464 if (!(irq_status & SI1145_MASK_ALL_IE))
468 for_each_set_bit(i, indio_dev->active_scan_mask,
469 indio_dev->masklength) {
472 while (i + run < indio_dev->masklength) {
473 if (!test_bit(i + run, indio_dev->active_scan_mask))
475 if (indio_dev->channels[i + run].address !=
476 indio_dev->channels[i].address + 2 * run)
481 ret = i2c_smbus_read_i2c_block_data_or_emulated(
482 data->client, indio_dev->channels[i].address,
483 sizeof(u16) * run, &data->buffer[j]);
486 j += run * sizeof(u16);
490 if (data->autonomous) {
491 ret = i2c_smbus_write_byte_data(data->client,
492 SI1145_REG_IRQ_STATUS,
493 irq_status & SI1145_MASK_ALL_IE);
498 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
499 iio_get_time_ns(indio_dev));
502 iio_trigger_notify_done(indio_dev->trig);
506 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
508 struct si1145_data *data = iio_priv(indio_dev);
513 /* channel list already set, no need to reprogram */
514 if (data->scan_mask == scan_mask)
517 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
518 switch (indio_dev->channels[i].address) {
519 case SI1145_REG_ALSVIS_DATA:
520 reg |= SI1145_CHLIST_EN_ALSVIS;
522 case SI1145_REG_ALSIR_DATA:
523 reg |= SI1145_CHLIST_EN_ALSIR;
525 case SI1145_REG_PS1_DATA:
526 reg |= SI1145_CHLIST_EN_PS1;
528 case SI1145_REG_PS2_DATA:
529 reg |= SI1145_CHLIST_EN_PS2;
531 case SI1145_REG_PS3_DATA:
532 reg |= SI1145_CHLIST_EN_PS3;
534 case SI1145_REG_AUX_DATA:
535 switch (indio_dev->channels[i].type) {
537 reg |= SI1145_CHLIST_EN_UV;
540 reg |= SI1145_CHLIST_EN_AUX;
541 if (indio_dev->channels[i].type == IIO_TEMP)
542 mux = SI1145_MUX_TEMP;
544 mux = SI1145_MUX_VDD;
545 ret = si1145_param_set(data,
546 SI1145_PARAM_AUX_ADC_MUX, mux);
555 data->scan_mask = scan_mask;
556 ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
558 return ret < 0 ? ret : 0;
561 static int si1145_measure(struct iio_dev *indio_dev,
562 struct iio_chan_spec const *chan)
564 struct si1145_data *data = iio_priv(indio_dev);
568 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
572 cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
573 SI1145_CMD_ALS_FORCE;
574 ret = si1145_command(data, cmd);
575 if (ret < 0 && ret != -EOVERFLOW)
578 return i2c_smbus_read_word_data(data->client, chan->address);
582 * Conversion between iio scale and ADC_GAIN values
583 * These could be further adjusted but proximity/intensity are dimensionless
585 static const int si1145_proximity_scale_available[] = {
586 128, 64, 32, 16, 8, 4};
587 static const int si1145_intensity_scale_available[] = {
588 128, 64, 32, 16, 8, 4, 2, 1};
589 static IIO_CONST_ATTR(in_proximity_scale_available,
591 static IIO_CONST_ATTR(in_intensity_scale_available,
592 "128 64 32 16 8 4 2 1");
593 static IIO_CONST_ATTR(in_intensity_ir_scale_available,
594 "128 64 32 16 8 4 2 1");
596 static int si1145_scale_from_adcgain(int regval)
598 return 128 >> regval;
601 static int si1145_proximity_adcgain_from_scale(int val, int val2)
603 val = find_closest_descending(val, si1145_proximity_scale_available,
604 ARRAY_SIZE(si1145_proximity_scale_available));
605 if (val < 0 || val > 5 || val2 != 0)
611 static int si1145_intensity_adcgain_from_scale(int val, int val2)
613 val = find_closest_descending(val, si1145_intensity_scale_available,
614 ARRAY_SIZE(si1145_intensity_scale_available));
615 if (val < 0 || val > 7 || val2 != 0)
621 static int si1145_read_raw(struct iio_dev *indio_dev,
622 struct iio_chan_spec const *chan,
623 int *val, int *val2, long mask)
625 struct si1145_data *data = iio_priv(indio_dev);
630 case IIO_CHAN_INFO_RAW:
631 switch (chan->type) {
637 ret = iio_device_claim_direct_mode(indio_dev);
640 ret = si1145_measure(indio_dev, chan);
641 iio_device_release_direct_mode(indio_dev);
650 ret = i2c_smbus_read_byte_data(data->client,
651 SI1145_PS_LED_REG(chan->channel));
655 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
662 case IIO_CHAN_INFO_SCALE:
663 switch (chan->type) {
665 reg = SI1145_PARAM_PS_ADC_GAIN;
668 if (chan->channel2 == IIO_MOD_LIGHT_IR)
669 reg = SI1145_PARAM_ALSIR_ADC_GAIN;
671 reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
676 return IIO_VAL_INT_PLUS_MICRO;
680 return IIO_VAL_INT_PLUS_MICRO;
685 ret = si1145_param_query(data, reg);
689 *val = si1145_scale_from_adcgain(ret & 0x07);
692 case IIO_CHAN_INFO_OFFSET:
693 switch (chan->type) {
696 * -ADC offset - ADC counts @ 25°C -
697 * 35 * ADC counts / °C
699 *val = -256 - 11136 + 25 * 35;
703 * All ADC measurements have are by default offset
707 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
710 *val = -si1145_uncompress(ret);
713 case IIO_CHAN_INFO_SAMP_FREQ:
714 return si1145_read_samp_freq(data, val, val2);
720 static int si1145_write_raw(struct iio_dev *indio_dev,
721 struct iio_chan_spec const *chan,
722 int val, int val2, long mask)
724 struct si1145_data *data = iio_priv(indio_dev);
725 u8 reg1, reg2, shift;
729 case IIO_CHAN_INFO_SCALE:
730 switch (chan->type) {
732 val = si1145_proximity_adcgain_from_scale(val, val2);
735 reg1 = SI1145_PARAM_PS_ADC_GAIN;
736 reg2 = SI1145_PARAM_PS_ADC_COUNTER;
739 val = si1145_intensity_adcgain_from_scale(val, val2);
742 if (chan->channel2 == IIO_MOD_LIGHT_IR) {
743 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
744 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
746 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
747 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
754 ret = iio_device_claim_direct_mode(indio_dev);
758 ret = si1145_param_set(data, reg1, val);
760 iio_device_release_direct_mode(indio_dev);
763 /* Set recovery period to one's complement of gain */
764 ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
765 iio_device_release_direct_mode(indio_dev);
767 case IIO_CHAN_INFO_RAW:
768 if (chan->type != IIO_CURRENT)
771 if (val < 0 || val > 15 || val2 != 0)
774 reg1 = SI1145_PS_LED_REG(chan->channel);
775 shift = SI1145_PS_LED_SHIFT(chan->channel);
777 ret = iio_device_claim_direct_mode(indio_dev);
781 ret = i2c_smbus_read_byte_data(data->client, reg1);
783 iio_device_release_direct_mode(indio_dev);
786 ret = i2c_smbus_write_byte_data(data->client, reg1,
787 (ret & ~(0x0f << shift)) |
788 ((val & 0x0f) << shift));
789 iio_device_release_direct_mode(indio_dev);
791 case IIO_CHAN_INFO_SAMP_FREQ:
792 return si1145_store_samp_freq(data, val);
798 #define SI1145_ST { \
802 .endianness = IIO_LE, \
805 #define SI1145_INTENSITY_CHANNEL(_si) { \
806 .type = IIO_INTENSITY, \
807 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
808 BIT(IIO_CHAN_INFO_OFFSET) | \
809 BIT(IIO_CHAN_INFO_SCALE), \
810 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
811 .scan_type = SI1145_ST, \
813 .address = SI1145_REG_ALSVIS_DATA, \
816 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
817 .type = IIO_INTENSITY, \
818 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
819 BIT(IIO_CHAN_INFO_OFFSET) | \
820 BIT(IIO_CHAN_INFO_SCALE), \
821 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
823 .channel2 = IIO_MOD_LIGHT_IR, \
824 .scan_type = SI1145_ST, \
826 .address = SI1145_REG_ALSIR_DATA, \
829 #define SI1145_TEMP_CHANNEL(_si) { \
831 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
832 BIT(IIO_CHAN_INFO_OFFSET) | \
833 BIT(IIO_CHAN_INFO_SCALE), \
834 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
835 .scan_type = SI1145_ST, \
837 .address = SI1145_REG_AUX_DATA, \
840 #define SI1145_UV_CHANNEL(_si) { \
841 .type = IIO_UVINDEX, \
842 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
843 BIT(IIO_CHAN_INFO_SCALE), \
844 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
845 .scan_type = SI1145_ST, \
847 .address = SI1145_REG_AUX_DATA, \
850 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
851 .type = IIO_PROXIMITY, \
854 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
855 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
856 BIT(IIO_CHAN_INFO_OFFSET), \
857 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
858 .scan_type = SI1145_ST, \
860 .address = SI1145_REG_PS1_DATA + _ch * 2, \
863 #define SI1145_VOLTAGE_CHANNEL(_si) { \
864 .type = IIO_VOLTAGE, \
865 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
866 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
867 .scan_type = SI1145_ST, \
869 .address = SI1145_REG_AUX_DATA, \
872 #define SI1145_CURRENT_CHANNEL(_ch) { \
873 .type = IIO_CURRENT, \
878 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
881 static const struct iio_chan_spec si1132_channels[] = {
882 SI1145_INTENSITY_CHANNEL(0),
883 SI1145_INTENSITY_IR_CHANNEL(1),
884 SI1145_TEMP_CHANNEL(2),
885 SI1145_VOLTAGE_CHANNEL(3),
886 SI1145_UV_CHANNEL(4),
887 IIO_CHAN_SOFT_TIMESTAMP(6),
890 static const struct iio_chan_spec si1141_channels[] = {
891 SI1145_INTENSITY_CHANNEL(0),
892 SI1145_INTENSITY_IR_CHANNEL(1),
893 SI1145_PROXIMITY_CHANNEL(2, 0),
894 SI1145_TEMP_CHANNEL(3),
895 SI1145_VOLTAGE_CHANNEL(4),
896 IIO_CHAN_SOFT_TIMESTAMP(5),
897 SI1145_CURRENT_CHANNEL(0),
900 static const struct iio_chan_spec si1142_channels[] = {
901 SI1145_INTENSITY_CHANNEL(0),
902 SI1145_INTENSITY_IR_CHANNEL(1),
903 SI1145_PROXIMITY_CHANNEL(2, 0),
904 SI1145_PROXIMITY_CHANNEL(3, 1),
905 SI1145_TEMP_CHANNEL(4),
906 SI1145_VOLTAGE_CHANNEL(5),
907 IIO_CHAN_SOFT_TIMESTAMP(6),
908 SI1145_CURRENT_CHANNEL(0),
909 SI1145_CURRENT_CHANNEL(1),
912 static const struct iio_chan_spec si1143_channels[] = {
913 SI1145_INTENSITY_CHANNEL(0),
914 SI1145_INTENSITY_IR_CHANNEL(1),
915 SI1145_PROXIMITY_CHANNEL(2, 0),
916 SI1145_PROXIMITY_CHANNEL(3, 1),
917 SI1145_PROXIMITY_CHANNEL(4, 2),
918 SI1145_TEMP_CHANNEL(5),
919 SI1145_VOLTAGE_CHANNEL(6),
920 IIO_CHAN_SOFT_TIMESTAMP(7),
921 SI1145_CURRENT_CHANNEL(0),
922 SI1145_CURRENT_CHANNEL(1),
923 SI1145_CURRENT_CHANNEL(2),
926 static const struct iio_chan_spec si1145_channels[] = {
927 SI1145_INTENSITY_CHANNEL(0),
928 SI1145_INTENSITY_IR_CHANNEL(1),
929 SI1145_PROXIMITY_CHANNEL(2, 0),
930 SI1145_TEMP_CHANNEL(3),
931 SI1145_VOLTAGE_CHANNEL(4),
932 SI1145_UV_CHANNEL(5),
933 IIO_CHAN_SOFT_TIMESTAMP(6),
934 SI1145_CURRENT_CHANNEL(0),
937 static const struct iio_chan_spec si1146_channels[] = {
938 SI1145_INTENSITY_CHANNEL(0),
939 SI1145_INTENSITY_IR_CHANNEL(1),
940 SI1145_TEMP_CHANNEL(2),
941 SI1145_VOLTAGE_CHANNEL(3),
942 SI1145_UV_CHANNEL(4),
943 SI1145_PROXIMITY_CHANNEL(5, 0),
944 SI1145_PROXIMITY_CHANNEL(6, 1),
945 IIO_CHAN_SOFT_TIMESTAMP(7),
946 SI1145_CURRENT_CHANNEL(0),
947 SI1145_CURRENT_CHANNEL(1),
950 static const struct iio_chan_spec si1147_channels[] = {
951 SI1145_INTENSITY_CHANNEL(0),
952 SI1145_INTENSITY_IR_CHANNEL(1),
953 SI1145_PROXIMITY_CHANNEL(2, 0),
954 SI1145_PROXIMITY_CHANNEL(3, 1),
955 SI1145_PROXIMITY_CHANNEL(4, 2),
956 SI1145_TEMP_CHANNEL(5),
957 SI1145_VOLTAGE_CHANNEL(6),
958 SI1145_UV_CHANNEL(7),
959 IIO_CHAN_SOFT_TIMESTAMP(8),
960 SI1145_CURRENT_CHANNEL(0),
961 SI1145_CURRENT_CHANNEL(1),
962 SI1145_CURRENT_CHANNEL(2),
965 static struct attribute *si1132_attributes[] = {
966 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
967 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
971 static struct attribute *si114x_attributes[] = {
972 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
973 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
974 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
978 static const struct attribute_group si1132_attribute_group = {
979 .attrs = si1132_attributes,
982 static const struct attribute_group si114x_attribute_group = {
983 .attrs = si114x_attributes,
987 static const struct iio_info si1132_info = {
988 .read_raw = si1145_read_raw,
989 .write_raw = si1145_write_raw,
990 .attrs = &si1132_attribute_group,
993 static const struct iio_info si114x_info = {
994 .read_raw = si1145_read_raw,
995 .write_raw = si1145_write_raw,
996 .attrs = &si114x_attribute_group,
999 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1000 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1002 static const struct si1145_part_info si1145_part_info[] = {
1003 [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1004 [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1005 [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1006 [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1007 [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1008 [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1009 [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1012 static int si1145_initialize(struct si1145_data *data)
1014 struct i2c_client *client = data->client;
1017 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1021 msleep(SI1145_COMMAND_TIMEOUT_MS);
1023 /* Hardware key, magic value */
1024 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1027 msleep(SI1145_COMMAND_TIMEOUT_MS);
1029 /* Turn off autonomous mode */
1030 ret = si1145_set_meas_rate(data, 0);
1034 /* Initialize sampling freq to 10 Hz */
1035 ret = si1145_store_samp_freq(data, 10);
1039 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1040 switch (data->part_info->num_leds) {
1042 ret = i2c_smbus_write_byte_data(client,
1044 SI1145_LED_CURRENT_45mA);
1049 ret = i2c_smbus_write_byte_data(client,
1050 SI1145_REG_PS_LED21,
1051 (SI1145_LED_CURRENT_45mA << 4) |
1052 SI1145_LED_CURRENT_45mA);
1055 ret = i2c_smbus_write_byte_data(client,
1056 SI1145_REG_PS_LED21,
1057 SI1145_LED_CURRENT_45mA);
1066 /* Set normal proximity measurement mode */
1067 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1068 SI1145_PS_ADC_MODE_NORMAL);
1072 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1076 /* ADC_COUNTER should be one complement of ADC_GAIN */
1077 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1081 /* Set ALS visible measurement mode */
1082 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1083 SI1145_ADC_MISC_RANGE);
1087 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1091 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1096 /* Set ALS IR measurement mode */
1097 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1098 SI1145_ADC_MISC_RANGE);
1102 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1106 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1112 * Initialize UCOEF to default values in datasheet
1113 * These registers are normally zero on reset
1115 if (data->part_info == &si1145_part_info[SI1132] ||
1116 data->part_info == &si1145_part_info[SI1145] ||
1117 data->part_info == &si1145_part_info[SI1146] ||
1118 data->part_info == &si1145_part_info[SI1147]) {
1119 ret = i2c_smbus_write_byte_data(data->client,
1121 SI1145_UCOEF1_DEFAULT);
1124 ret = i2c_smbus_write_byte_data(data->client,
1125 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1128 ret = i2c_smbus_write_byte_data(data->client,
1129 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1132 ret = i2c_smbus_write_byte_data(data->client,
1133 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1142 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1143 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1144 * mode reprograms the channels list anyway...
1146 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1148 struct si1145_data *data = iio_priv(indio_dev);
1151 mutex_lock(&data->lock);
1152 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1153 mutex_unlock(&data->lock);
1158 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1159 const unsigned long *scan_mask)
1161 struct si1145_data *data = iio_priv(indio_dev);
1162 unsigned int count = 0;
1165 /* Check that at most one AUX channel is enabled */
1166 for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1167 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1174 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1175 .preenable = si1145_buffer_preenable,
1176 .validate_scan_mask = si1145_validate_scan_mask,
1180 * si1145_trigger_set_state() - Set trigger state
1182 * When not using triggers interrupts are disabled and measurement rate is
1183 * set to zero in order to minimize power consumption.
1185 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1187 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1188 struct si1145_data *data = iio_priv(indio_dev);
1191 mutex_lock(&data->lock);
1194 data->autonomous = true;
1195 err = i2c_smbus_write_byte_data(data->client,
1196 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1199 err = i2c_smbus_write_byte_data(data->client,
1200 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1203 err = si1145_set_meas_rate(data, data->meas_rate);
1206 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1211 /* Disable as much as possible skipping errors */
1212 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1213 if (ret < 0 && !err)
1215 ret = si1145_set_meas_rate(data, 0);
1216 if (ret < 0 && !err)
1218 ret = i2c_smbus_write_byte_data(data->client,
1219 SI1145_REG_IRQ_ENABLE, 0);
1220 if (ret < 0 && !err)
1222 ret = i2c_smbus_write_byte_data(data->client,
1223 SI1145_REG_INT_CFG, 0);
1224 if (ret < 0 && !err)
1226 data->autonomous = false;
1229 mutex_unlock(&data->lock);
1233 static const struct iio_trigger_ops si1145_trigger_ops = {
1234 .set_trigger_state = si1145_trigger_set_state,
1237 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1239 struct si1145_data *data = iio_priv(indio_dev);
1240 struct i2c_client *client = data->client;
1241 struct iio_trigger *trig;
1244 trig = devm_iio_trigger_alloc(&client->dev,
1245 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev));
1249 trig->ops = &si1145_trigger_ops;
1250 iio_trigger_set_drvdata(trig, indio_dev);
1252 ret = devm_request_irq(&client->dev, client->irq,
1253 iio_trigger_generic_data_rdy_poll,
1254 IRQF_TRIGGER_FALLING,
1258 dev_err(&client->dev, "irq request failed\n");
1262 ret = devm_iio_trigger_register(&client->dev, trig);
1267 indio_dev->trig = iio_trigger_get(data->trig);
1272 static int si1145_probe(struct i2c_client *client)
1274 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1275 struct si1145_data *data;
1276 struct iio_dev *indio_dev;
1277 u8 part_id, rev_id, seq_id;
1280 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1284 data = iio_priv(indio_dev);
1285 i2c_set_clientdata(client, indio_dev);
1286 data->client = client;
1287 data->part_info = &si1145_part_info[id->driver_data];
1289 part_id = ret = i2c_smbus_read_byte_data(data->client,
1290 SI1145_REG_PART_ID);
1293 rev_id = ret = i2c_smbus_read_byte_data(data->client,
1297 seq_id = ret = i2c_smbus_read_byte_data(data->client,
1301 dev_info(&client->dev, "device ID part 0x%02x rev 0x%02x seq 0x%02x\n",
1302 part_id, rev_id, seq_id);
1303 if (part_id != data->part_info->part) {
1304 dev_err(&client->dev, "part ID mismatch got 0x%02x, expected 0x%02x\n",
1305 part_id, data->part_info->part);
1309 indio_dev->name = id->name;
1310 indio_dev->channels = data->part_info->channels;
1311 indio_dev->num_channels = data->part_info->num_channels;
1312 indio_dev->info = data->part_info->iio_info;
1313 indio_dev->modes = INDIO_DIRECT_MODE;
1315 mutex_init(&data->lock);
1316 mutex_init(&data->cmdlock);
1318 ret = si1145_initialize(data);
1322 ret = devm_iio_triggered_buffer_setup(&client->dev,
1324 si1145_trigger_handler, &si1145_buffer_setup_ops);
1329 ret = si1145_probe_trigger(indio_dev);
1333 dev_info(&client->dev, "no irq, using polling\n");
1336 return devm_iio_device_register(&client->dev, indio_dev);
1339 static const struct i2c_device_id si1145_ids[] = {
1340 { "si1132", SI1132 },
1341 { "si1141", SI1141 },
1342 { "si1142", SI1142 },
1343 { "si1143", SI1143 },
1344 { "si1145", SI1145 },
1345 { "si1146", SI1146 },
1346 { "si1147", SI1147 },
1349 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1351 static struct i2c_driver si1145_driver = {
1355 .probe_new = si1145_probe,
1356 .id_table = si1145_ids,
1359 module_i2c_driver(si1145_driver);
1362 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1363 MODULE_LICENSE("GPL");