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
175 * @buffer: Used to pack data read from sensor.
178 struct i2c_client *client;
180 struct mutex cmdlock;
182 const struct si1145_part_info *part_info;
183 unsigned long scan_mask;
185 struct iio_trigger *trig;
188 * Ensure timestamp will be naturally aligned if present.
189 * Maximum buffer size (may be only partly used if not all
190 * channels are enabled):
191 * 6*2 bytes channels data + 4 bytes alignment +
194 u8 buffer[24] __aligned(8);
198 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
200 * Does not modify data->rsp_seq
202 * Return: 0 on success and -errno on error.
204 static int __si1145_command_reset(struct si1145_data *data)
206 struct device *dev = &data->client->dev;
207 unsigned long stop_jiffies;
210 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
214 msleep(SI1145_COMMAND_MINSLEEP_MS);
216 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
218 ret = i2c_smbus_read_byte_data(data->client,
219 SI1145_REG_RESPONSE);
222 if (time_after(jiffies, stop_jiffies)) {
223 dev_warn(dev, "timeout on reset\n");
226 msleep(SI1145_COMMAND_MINSLEEP_MS);
232 * si1145_command() - Execute a command and poll the response register
234 * All conversion overflows are reported as -EOVERFLOW
235 * INVALID_SETTING is reported as -EINVAL
236 * Timeouts are reported as -ETIMEDOUT
238 * Return: 0 on success or -errno on failure
240 static int si1145_command(struct si1145_data *data, u8 cmd)
242 struct device *dev = &data->client->dev;
243 unsigned long stop_jiffies;
246 mutex_lock(&data->cmdlock);
248 if (data->rsp_seq < 0) {
249 ret = __si1145_command_reset(data);
251 dev_err(dev, "failed to reset command counter, ret=%d\n",
258 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
260 dev_warn(dev, "failed to write command, ret=%d\n", ret);
263 /* Sleep a little to ensure the command is received */
264 msleep(SI1145_COMMAND_MINSLEEP_MS);
266 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
268 ret = i2c_smbus_read_byte_data(data->client,
269 SI1145_REG_RESPONSE);
271 dev_warn(dev, "failed to read response, ret=%d\n", ret);
275 if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
276 if (ret == data->rsp_seq) {
277 if (time_after(jiffies, stop_jiffies)) {
278 dev_warn(dev, "timeout on command %#02hhx\n",
283 msleep(SI1145_COMMAND_MINSLEEP_MS);
286 if (ret == ((data->rsp_seq + 1) &
287 SI1145_RSP_COUNTER_MASK)) {
292 dev_warn(dev, "unexpected response counter %d instead of %d\n",
293 ret, (data->rsp_seq + 1) &
294 SI1145_RSP_COUNTER_MASK);
297 if (ret == SI1145_RSP_INVALID_SETTING) {
298 dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
302 /* All overflows are treated identically */
303 dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
309 /* Force a counter reset next time */
315 mutex_unlock(&data->cmdlock);
320 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
325 ret = i2c_smbus_write_byte_data(data->client,
326 SI1145_REG_PARAM_WR, value);
330 return si1145_command(data, op | (param & 0x1F));
333 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
335 return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
338 /* Set param. Returns negative errno or current value */
339 static int si1145_param_query(struct si1145_data *data, u8 param)
343 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
347 return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
350 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
351 static u16 si1145_uncompress(u8 x)
359 exponent = (x & 0xf0) >> 4;
360 result = 0x10 | (x & 0x0f);
363 return result << (exponent - 4);
364 return result >> (4 - exponent);
367 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
368 static u8 si1145_compress(u16 x)
387 significand = x << (4 - exponent);
388 return (exponent << 4) | (significand & 0xF);
391 significand = x >> (exponent - 5);
392 if (significand & 1) {
394 if (significand & 0x0040) {
400 return (exponent << 4) | ((significand >> 1) & 0xF);
403 /* Write meas_rate in hardware */
404 static int si1145_set_meas_rate(struct si1145_data *data, int interval)
406 if (data->part_info->uncompressed_meas_rate)
407 return i2c_smbus_write_word_data(data->client,
408 SI1145_REG_MEAS_RATE, interval);
410 return i2c_smbus_write_byte_data(data->client,
411 SI1145_REG_MEAS_RATE, interval);
414 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
417 if (data->part_info->uncompressed_meas_rate)
418 *val2 = data->meas_rate;
420 *val2 = si1145_uncompress(data->meas_rate);
421 return IIO_VAL_FRACTIONAL;
424 /* Set the samp freq in driver private data */
425 static int si1145_store_samp_freq(struct si1145_data *data, int val)
430 if (val <= 0 || val > 32000)
432 meas_rate = 32000 / val;
434 mutex_lock(&data->lock);
435 if (data->autonomous) {
436 ret = si1145_set_meas_rate(data, meas_rate);
440 if (data->part_info->uncompressed_meas_rate)
441 data->meas_rate = meas_rate;
443 data->meas_rate = si1145_compress(meas_rate);
446 mutex_unlock(&data->lock);
451 static irqreturn_t si1145_trigger_handler(int irq, void *private)
453 struct iio_poll_func *pf = private;
454 struct iio_dev *indio_dev = pf->indio_dev;
455 struct si1145_data *data = iio_priv(indio_dev);
460 if (!data->autonomous) {
461 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
462 if (ret < 0 && ret != -EOVERFLOW)
465 irq_status = ret = i2c_smbus_read_byte_data(data->client,
466 SI1145_REG_IRQ_STATUS);
469 if (!(irq_status & SI1145_MASK_ALL_IE))
473 for_each_set_bit(i, indio_dev->active_scan_mask,
474 indio_dev->masklength) {
477 while (i + run < indio_dev->masklength) {
478 if (!test_bit(i + run, indio_dev->active_scan_mask))
480 if (indio_dev->channels[i + run].address !=
481 indio_dev->channels[i].address + 2 * run)
486 ret = i2c_smbus_read_i2c_block_data_or_emulated(
487 data->client, indio_dev->channels[i].address,
488 sizeof(u16) * run, &data->buffer[j]);
491 j += run * sizeof(u16);
495 if (data->autonomous) {
496 ret = i2c_smbus_write_byte_data(data->client,
497 SI1145_REG_IRQ_STATUS,
498 irq_status & SI1145_MASK_ALL_IE);
503 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
504 iio_get_time_ns(indio_dev));
507 iio_trigger_notify_done(indio_dev->trig);
511 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
513 struct si1145_data *data = iio_priv(indio_dev);
518 /* channel list already set, no need to reprogram */
519 if (data->scan_mask == scan_mask)
522 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
523 switch (indio_dev->channels[i].address) {
524 case SI1145_REG_ALSVIS_DATA:
525 reg |= SI1145_CHLIST_EN_ALSVIS;
527 case SI1145_REG_ALSIR_DATA:
528 reg |= SI1145_CHLIST_EN_ALSIR;
530 case SI1145_REG_PS1_DATA:
531 reg |= SI1145_CHLIST_EN_PS1;
533 case SI1145_REG_PS2_DATA:
534 reg |= SI1145_CHLIST_EN_PS2;
536 case SI1145_REG_PS3_DATA:
537 reg |= SI1145_CHLIST_EN_PS3;
539 case SI1145_REG_AUX_DATA:
540 switch (indio_dev->channels[i].type) {
542 reg |= SI1145_CHLIST_EN_UV;
545 reg |= SI1145_CHLIST_EN_AUX;
546 if (indio_dev->channels[i].type == IIO_TEMP)
547 mux = SI1145_MUX_TEMP;
549 mux = SI1145_MUX_VDD;
550 ret = si1145_param_set(data,
551 SI1145_PARAM_AUX_ADC_MUX, mux);
560 data->scan_mask = scan_mask;
561 ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
563 return ret < 0 ? ret : 0;
566 static int si1145_measure(struct iio_dev *indio_dev,
567 struct iio_chan_spec const *chan)
569 struct si1145_data *data = iio_priv(indio_dev);
573 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
577 cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
578 SI1145_CMD_ALS_FORCE;
579 ret = si1145_command(data, cmd);
580 if (ret < 0 && ret != -EOVERFLOW)
583 return i2c_smbus_read_word_data(data->client, chan->address);
587 * Conversion between iio scale and ADC_GAIN values
588 * These could be further adjusted but proximity/intensity are dimensionless
590 static const int si1145_proximity_scale_available[] = {
591 128, 64, 32, 16, 8, 4};
592 static const int si1145_intensity_scale_available[] = {
593 128, 64, 32, 16, 8, 4, 2, 1};
594 static IIO_CONST_ATTR(in_proximity_scale_available,
596 static IIO_CONST_ATTR(in_intensity_scale_available,
597 "128 64 32 16 8 4 2 1");
598 static IIO_CONST_ATTR(in_intensity_ir_scale_available,
599 "128 64 32 16 8 4 2 1");
601 static int si1145_scale_from_adcgain(int regval)
603 return 128 >> regval;
606 static int si1145_proximity_adcgain_from_scale(int val, int val2)
608 val = find_closest_descending(val, si1145_proximity_scale_available,
609 ARRAY_SIZE(si1145_proximity_scale_available));
610 if (val < 0 || val > 5 || val2 != 0)
616 static int si1145_intensity_adcgain_from_scale(int val, int val2)
618 val = find_closest_descending(val, si1145_intensity_scale_available,
619 ARRAY_SIZE(si1145_intensity_scale_available));
620 if (val < 0 || val > 7 || val2 != 0)
626 static int si1145_read_raw(struct iio_dev *indio_dev,
627 struct iio_chan_spec const *chan,
628 int *val, int *val2, long mask)
630 struct si1145_data *data = iio_priv(indio_dev);
635 case IIO_CHAN_INFO_RAW:
636 switch (chan->type) {
642 ret = iio_device_claim_direct_mode(indio_dev);
645 ret = si1145_measure(indio_dev, chan);
646 iio_device_release_direct_mode(indio_dev);
655 ret = i2c_smbus_read_byte_data(data->client,
656 SI1145_PS_LED_REG(chan->channel));
660 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
667 case IIO_CHAN_INFO_SCALE:
668 switch (chan->type) {
670 reg = SI1145_PARAM_PS_ADC_GAIN;
673 if (chan->channel2 == IIO_MOD_LIGHT_IR)
674 reg = SI1145_PARAM_ALSIR_ADC_GAIN;
676 reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
681 return IIO_VAL_INT_PLUS_MICRO;
685 return IIO_VAL_INT_PLUS_MICRO;
690 ret = si1145_param_query(data, reg);
694 *val = si1145_scale_from_adcgain(ret & 0x07);
697 case IIO_CHAN_INFO_OFFSET:
698 switch (chan->type) {
701 * -ADC offset - ADC counts @ 25°C -
702 * 35 * ADC counts / °C
704 *val = -256 - 11136 + 25 * 35;
708 * All ADC measurements have are by default offset
712 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
715 *val = -si1145_uncompress(ret);
718 case IIO_CHAN_INFO_SAMP_FREQ:
719 return si1145_read_samp_freq(data, val, val2);
725 static int si1145_write_raw(struct iio_dev *indio_dev,
726 struct iio_chan_spec const *chan,
727 int val, int val2, long mask)
729 struct si1145_data *data = iio_priv(indio_dev);
730 u8 reg1, reg2, shift;
734 case IIO_CHAN_INFO_SCALE:
735 switch (chan->type) {
737 val = si1145_proximity_adcgain_from_scale(val, val2);
740 reg1 = SI1145_PARAM_PS_ADC_GAIN;
741 reg2 = SI1145_PARAM_PS_ADC_COUNTER;
744 val = si1145_intensity_adcgain_from_scale(val, val2);
747 if (chan->channel2 == IIO_MOD_LIGHT_IR) {
748 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
749 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
751 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
752 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
759 ret = iio_device_claim_direct_mode(indio_dev);
763 ret = si1145_param_set(data, reg1, val);
765 iio_device_release_direct_mode(indio_dev);
768 /* Set recovery period to one's complement of gain */
769 ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
770 iio_device_release_direct_mode(indio_dev);
772 case IIO_CHAN_INFO_RAW:
773 if (chan->type != IIO_CURRENT)
776 if (val < 0 || val > 15 || val2 != 0)
779 reg1 = SI1145_PS_LED_REG(chan->channel);
780 shift = SI1145_PS_LED_SHIFT(chan->channel);
782 ret = iio_device_claim_direct_mode(indio_dev);
786 ret = i2c_smbus_read_byte_data(data->client, reg1);
788 iio_device_release_direct_mode(indio_dev);
791 ret = i2c_smbus_write_byte_data(data->client, reg1,
792 (ret & ~(0x0f << shift)) |
793 ((val & 0x0f) << shift));
794 iio_device_release_direct_mode(indio_dev);
796 case IIO_CHAN_INFO_SAMP_FREQ:
797 return si1145_store_samp_freq(data, val);
803 #define SI1145_ST { \
807 .endianness = IIO_LE, \
810 #define SI1145_INTENSITY_CHANNEL(_si) { \
811 .type = IIO_INTENSITY, \
812 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
813 BIT(IIO_CHAN_INFO_OFFSET) | \
814 BIT(IIO_CHAN_INFO_SCALE), \
815 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
816 .scan_type = SI1145_ST, \
818 .address = SI1145_REG_ALSVIS_DATA, \
821 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
822 .type = IIO_INTENSITY, \
823 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
824 BIT(IIO_CHAN_INFO_OFFSET) | \
825 BIT(IIO_CHAN_INFO_SCALE), \
826 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
828 .channel2 = IIO_MOD_LIGHT_IR, \
829 .scan_type = SI1145_ST, \
831 .address = SI1145_REG_ALSIR_DATA, \
834 #define SI1145_TEMP_CHANNEL(_si) { \
836 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
837 BIT(IIO_CHAN_INFO_OFFSET) | \
838 BIT(IIO_CHAN_INFO_SCALE), \
839 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
840 .scan_type = SI1145_ST, \
842 .address = SI1145_REG_AUX_DATA, \
845 #define SI1145_UV_CHANNEL(_si) { \
846 .type = IIO_UVINDEX, \
847 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
848 BIT(IIO_CHAN_INFO_SCALE), \
849 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
850 .scan_type = SI1145_ST, \
852 .address = SI1145_REG_AUX_DATA, \
855 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
856 .type = IIO_PROXIMITY, \
859 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
860 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
861 BIT(IIO_CHAN_INFO_OFFSET), \
862 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
863 .scan_type = SI1145_ST, \
865 .address = SI1145_REG_PS1_DATA + _ch * 2, \
868 #define SI1145_VOLTAGE_CHANNEL(_si) { \
869 .type = IIO_VOLTAGE, \
870 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
871 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
872 .scan_type = SI1145_ST, \
874 .address = SI1145_REG_AUX_DATA, \
877 #define SI1145_CURRENT_CHANNEL(_ch) { \
878 .type = IIO_CURRENT, \
883 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
886 static const struct iio_chan_spec si1132_channels[] = {
887 SI1145_INTENSITY_CHANNEL(0),
888 SI1145_INTENSITY_IR_CHANNEL(1),
889 SI1145_TEMP_CHANNEL(2),
890 SI1145_VOLTAGE_CHANNEL(3),
891 SI1145_UV_CHANNEL(4),
892 IIO_CHAN_SOFT_TIMESTAMP(6),
895 static const struct iio_chan_spec si1141_channels[] = {
896 SI1145_INTENSITY_CHANNEL(0),
897 SI1145_INTENSITY_IR_CHANNEL(1),
898 SI1145_PROXIMITY_CHANNEL(2, 0),
899 SI1145_TEMP_CHANNEL(3),
900 SI1145_VOLTAGE_CHANNEL(4),
901 IIO_CHAN_SOFT_TIMESTAMP(5),
902 SI1145_CURRENT_CHANNEL(0),
905 static const struct iio_chan_spec si1142_channels[] = {
906 SI1145_INTENSITY_CHANNEL(0),
907 SI1145_INTENSITY_IR_CHANNEL(1),
908 SI1145_PROXIMITY_CHANNEL(2, 0),
909 SI1145_PROXIMITY_CHANNEL(3, 1),
910 SI1145_TEMP_CHANNEL(4),
911 SI1145_VOLTAGE_CHANNEL(5),
912 IIO_CHAN_SOFT_TIMESTAMP(6),
913 SI1145_CURRENT_CHANNEL(0),
914 SI1145_CURRENT_CHANNEL(1),
917 static const struct iio_chan_spec si1143_channels[] = {
918 SI1145_INTENSITY_CHANNEL(0),
919 SI1145_INTENSITY_IR_CHANNEL(1),
920 SI1145_PROXIMITY_CHANNEL(2, 0),
921 SI1145_PROXIMITY_CHANNEL(3, 1),
922 SI1145_PROXIMITY_CHANNEL(4, 2),
923 SI1145_TEMP_CHANNEL(5),
924 SI1145_VOLTAGE_CHANNEL(6),
925 IIO_CHAN_SOFT_TIMESTAMP(7),
926 SI1145_CURRENT_CHANNEL(0),
927 SI1145_CURRENT_CHANNEL(1),
928 SI1145_CURRENT_CHANNEL(2),
931 static const struct iio_chan_spec si1145_channels[] = {
932 SI1145_INTENSITY_CHANNEL(0),
933 SI1145_INTENSITY_IR_CHANNEL(1),
934 SI1145_PROXIMITY_CHANNEL(2, 0),
935 SI1145_TEMP_CHANNEL(3),
936 SI1145_VOLTAGE_CHANNEL(4),
937 SI1145_UV_CHANNEL(5),
938 IIO_CHAN_SOFT_TIMESTAMP(6),
939 SI1145_CURRENT_CHANNEL(0),
942 static const struct iio_chan_spec si1146_channels[] = {
943 SI1145_INTENSITY_CHANNEL(0),
944 SI1145_INTENSITY_IR_CHANNEL(1),
945 SI1145_TEMP_CHANNEL(2),
946 SI1145_VOLTAGE_CHANNEL(3),
947 SI1145_UV_CHANNEL(4),
948 SI1145_PROXIMITY_CHANNEL(5, 0),
949 SI1145_PROXIMITY_CHANNEL(6, 1),
950 IIO_CHAN_SOFT_TIMESTAMP(7),
951 SI1145_CURRENT_CHANNEL(0),
952 SI1145_CURRENT_CHANNEL(1),
955 static const struct iio_chan_spec si1147_channels[] = {
956 SI1145_INTENSITY_CHANNEL(0),
957 SI1145_INTENSITY_IR_CHANNEL(1),
958 SI1145_PROXIMITY_CHANNEL(2, 0),
959 SI1145_PROXIMITY_CHANNEL(3, 1),
960 SI1145_PROXIMITY_CHANNEL(4, 2),
961 SI1145_TEMP_CHANNEL(5),
962 SI1145_VOLTAGE_CHANNEL(6),
963 SI1145_UV_CHANNEL(7),
964 IIO_CHAN_SOFT_TIMESTAMP(8),
965 SI1145_CURRENT_CHANNEL(0),
966 SI1145_CURRENT_CHANNEL(1),
967 SI1145_CURRENT_CHANNEL(2),
970 static struct attribute *si1132_attributes[] = {
971 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
972 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
976 static struct attribute *si114x_attributes[] = {
977 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
978 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
979 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
983 static const struct attribute_group si1132_attribute_group = {
984 .attrs = si1132_attributes,
987 static const struct attribute_group si114x_attribute_group = {
988 .attrs = si114x_attributes,
992 static const struct iio_info si1132_info = {
993 .read_raw = si1145_read_raw,
994 .write_raw = si1145_write_raw,
995 .attrs = &si1132_attribute_group,
998 static const struct iio_info si114x_info = {
999 .read_raw = si1145_read_raw,
1000 .write_raw = si1145_write_raw,
1001 .attrs = &si114x_attribute_group,
1004 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1005 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1007 static const struct si1145_part_info si1145_part_info[] = {
1008 [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1009 [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1010 [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1011 [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1012 [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1013 [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1014 [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1017 static int si1145_initialize(struct si1145_data *data)
1019 struct i2c_client *client = data->client;
1022 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1026 msleep(SI1145_COMMAND_TIMEOUT_MS);
1028 /* Hardware key, magic value */
1029 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1032 msleep(SI1145_COMMAND_TIMEOUT_MS);
1034 /* Turn off autonomous mode */
1035 ret = si1145_set_meas_rate(data, 0);
1039 /* Initialize sampling freq to 10 Hz */
1040 ret = si1145_store_samp_freq(data, 10);
1044 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1045 switch (data->part_info->num_leds) {
1047 ret = i2c_smbus_write_byte_data(client,
1049 SI1145_LED_CURRENT_45mA);
1054 ret = i2c_smbus_write_byte_data(client,
1055 SI1145_REG_PS_LED21,
1056 (SI1145_LED_CURRENT_45mA << 4) |
1057 SI1145_LED_CURRENT_45mA);
1060 ret = i2c_smbus_write_byte_data(client,
1061 SI1145_REG_PS_LED21,
1062 SI1145_LED_CURRENT_45mA);
1071 /* Set normal proximity measurement mode */
1072 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1073 SI1145_PS_ADC_MODE_NORMAL);
1077 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1081 /* ADC_COUNTER should be one complement of ADC_GAIN */
1082 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1086 /* Set ALS visible measurement mode */
1087 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1088 SI1145_ADC_MISC_RANGE);
1092 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1096 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1101 /* Set ALS IR measurement mode */
1102 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1103 SI1145_ADC_MISC_RANGE);
1107 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1111 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1117 * Initialize UCOEF to default values in datasheet
1118 * These registers are normally zero on reset
1120 if (data->part_info == &si1145_part_info[SI1132] ||
1121 data->part_info == &si1145_part_info[SI1145] ||
1122 data->part_info == &si1145_part_info[SI1146] ||
1123 data->part_info == &si1145_part_info[SI1147]) {
1124 ret = i2c_smbus_write_byte_data(data->client,
1126 SI1145_UCOEF1_DEFAULT);
1129 ret = i2c_smbus_write_byte_data(data->client,
1130 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1133 ret = i2c_smbus_write_byte_data(data->client,
1134 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1137 ret = i2c_smbus_write_byte_data(data->client,
1138 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1147 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1148 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1149 * mode reprograms the channels list anyway...
1151 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1153 struct si1145_data *data = iio_priv(indio_dev);
1156 mutex_lock(&data->lock);
1157 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1158 mutex_unlock(&data->lock);
1163 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1164 const unsigned long *scan_mask)
1166 struct si1145_data *data = iio_priv(indio_dev);
1167 unsigned int count = 0;
1170 /* Check that at most one AUX channel is enabled */
1171 for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1172 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1179 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1180 .preenable = si1145_buffer_preenable,
1181 .postenable = iio_triggered_buffer_postenable,
1182 .predisable = iio_triggered_buffer_predisable,
1183 .validate_scan_mask = si1145_validate_scan_mask,
1187 * si1145_trigger_set_state() - Set trigger state
1189 * When not using triggers interrupts are disabled and measurement rate is
1190 * set to zero in order to minimize power consumption.
1192 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1194 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1195 struct si1145_data *data = iio_priv(indio_dev);
1198 mutex_lock(&data->lock);
1201 data->autonomous = true;
1202 err = i2c_smbus_write_byte_data(data->client,
1203 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1206 err = i2c_smbus_write_byte_data(data->client,
1207 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1210 err = si1145_set_meas_rate(data, data->meas_rate);
1213 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1218 /* Disable as much as possible skipping errors */
1219 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1220 if (ret < 0 && !err)
1222 ret = si1145_set_meas_rate(data, 0);
1223 if (ret < 0 && !err)
1225 ret = i2c_smbus_write_byte_data(data->client,
1226 SI1145_REG_IRQ_ENABLE, 0);
1227 if (ret < 0 && !err)
1229 ret = i2c_smbus_write_byte_data(data->client,
1230 SI1145_REG_INT_CFG, 0);
1231 if (ret < 0 && !err)
1233 data->autonomous = false;
1236 mutex_unlock(&data->lock);
1240 static const struct iio_trigger_ops si1145_trigger_ops = {
1241 .set_trigger_state = si1145_trigger_set_state,
1244 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1246 struct si1145_data *data = iio_priv(indio_dev);
1247 struct i2c_client *client = data->client;
1248 struct iio_trigger *trig;
1251 trig = devm_iio_trigger_alloc(&client->dev,
1252 "%s-dev%d", indio_dev->name, indio_dev->id);
1256 trig->dev.parent = &client->dev;
1257 trig->ops = &si1145_trigger_ops;
1258 iio_trigger_set_drvdata(trig, indio_dev);
1260 ret = devm_request_irq(&client->dev, client->irq,
1261 iio_trigger_generic_data_rdy_poll,
1262 IRQF_TRIGGER_FALLING,
1266 dev_err(&client->dev, "irq request failed\n");
1270 ret = iio_trigger_register(trig);
1275 indio_dev->trig = iio_trigger_get(data->trig);
1280 static void si1145_remove_trigger(struct iio_dev *indio_dev)
1282 struct si1145_data *data = iio_priv(indio_dev);
1285 iio_trigger_unregister(data->trig);
1290 static int si1145_probe(struct i2c_client *client,
1291 const struct i2c_device_id *id)
1293 struct si1145_data *data;
1294 struct iio_dev *indio_dev;
1295 u8 part_id, rev_id, seq_id;
1298 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1302 data = iio_priv(indio_dev);
1303 i2c_set_clientdata(client, indio_dev);
1304 data->client = client;
1305 data->part_info = &si1145_part_info[id->driver_data];
1307 part_id = ret = i2c_smbus_read_byte_data(data->client,
1308 SI1145_REG_PART_ID);
1311 rev_id = ret = i2c_smbus_read_byte_data(data->client,
1315 seq_id = ret = i2c_smbus_read_byte_data(data->client,
1319 dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1320 part_id, rev_id, seq_id);
1321 if (part_id != data->part_info->part) {
1322 dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1323 part_id, data->part_info->part);
1327 indio_dev->dev.parent = &client->dev;
1328 indio_dev->name = id->name;
1329 indio_dev->channels = data->part_info->channels;
1330 indio_dev->num_channels = data->part_info->num_channels;
1331 indio_dev->info = data->part_info->iio_info;
1332 indio_dev->modes = INDIO_DIRECT_MODE;
1334 mutex_init(&data->lock);
1335 mutex_init(&data->cmdlock);
1337 ret = si1145_initialize(data);
1341 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1342 si1145_trigger_handler, &si1145_buffer_setup_ops);
1347 ret = si1145_probe_trigger(indio_dev);
1349 goto error_free_buffer;
1351 dev_info(&client->dev, "no irq, using polling\n");
1354 ret = iio_device_register(indio_dev);
1356 goto error_free_trigger;
1361 si1145_remove_trigger(indio_dev);
1363 iio_triggered_buffer_cleanup(indio_dev);
1368 static const struct i2c_device_id si1145_ids[] = {
1369 { "si1132", SI1132 },
1370 { "si1141", SI1141 },
1371 { "si1142", SI1142 },
1372 { "si1143", SI1143 },
1373 { "si1145", SI1145 },
1374 { "si1146", SI1146 },
1375 { "si1147", SI1147 },
1378 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1380 static int si1145_remove(struct i2c_client *client)
1382 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1384 iio_device_unregister(indio_dev);
1385 si1145_remove_trigger(indio_dev);
1386 iio_triggered_buffer_cleanup(indio_dev);
1391 static struct i2c_driver si1145_driver = {
1395 .probe = si1145_probe,
1396 .remove = si1145_remove,
1397 .id_table = si1145_ids,
1400 module_i2c_driver(si1145_driver);
1402 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1403 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1404 MODULE_LICENSE("GPL");