treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / light / si1145.c
blob0476c2bc813841530b5e3977bfa2012baec51921
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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) \
65 (((ch) == 1) ? 4 : 0)
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
141 enum {
142 SI1132,
143 SI1141,
144 SI1142,
145 SI1143,
146 SI1145,
147 SI1146,
148 SI1147,
151 struct si1145_part_info {
152 u8 part;
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
172 struct si1145_data {
173 struct i2c_client *client;
174 struct mutex lock;
175 struct mutex cmdlock;
176 int rsp_seq;
177 const struct si1145_part_info *part_info;
178 unsigned long scan_mask;
179 bool autonomous;
180 struct iio_trigger *trig;
181 int meas_rate;
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;
195 int ret;
197 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
198 SI1145_CMD_NOP);
199 if (ret < 0)
200 return ret;
201 msleep(SI1145_COMMAND_MINSLEEP_MS);
203 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
204 while (true) {
205 ret = i2c_smbus_read_byte_data(data->client,
206 SI1145_REG_RESPONSE);
207 if (ret <= 0)
208 return ret;
209 if (time_after(jiffies, stop_jiffies)) {
210 dev_warn(dev, "timeout on reset\n");
211 return -ETIMEDOUT;
213 msleep(SI1145_COMMAND_MINSLEEP_MS);
214 continue;
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;
231 int ret;
233 mutex_lock(&data->cmdlock);
235 if (data->rsp_seq < 0) {
236 ret = __si1145_command_reset(data);
237 if (ret < 0) {
238 dev_err(dev, "failed to reset command counter, ret=%d\n",
239 ret);
240 goto out;
242 data->rsp_seq = 0;
245 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
246 if (ret) {
247 dev_warn(dev, "failed to write command, ret=%d\n", ret);
248 goto out;
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;
254 while (true) {
255 ret = i2c_smbus_read_byte_data(data->client,
256 SI1145_REG_RESPONSE);
257 if (ret < 0) {
258 dev_warn(dev, "failed to read response, ret=%d\n", ret);
259 break;
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",
266 cmd);
267 ret = -ETIMEDOUT;
268 break;
270 msleep(SI1145_COMMAND_MINSLEEP_MS);
271 continue;
273 if (ret == ((data->rsp_seq + 1) &
274 SI1145_RSP_COUNTER_MASK)) {
275 data->rsp_seq = ret;
276 ret = 0;
277 break;
279 dev_warn(dev, "unexpected response counter %d instead of %d\n",
280 ret, (data->rsp_seq + 1) &
281 SI1145_RSP_COUNTER_MASK);
282 ret = -EIO;
283 } else {
284 if (ret == SI1145_RSP_INVALID_SETTING) {
285 dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
286 cmd);
287 ret = -EINVAL;
288 } else {
289 /* All overflows are treated identically */
290 dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
291 ret, cmd);
292 ret = -EOVERFLOW;
296 /* Force a counter reset next time */
297 data->rsp_seq = -1;
298 break;
301 out:
302 mutex_unlock(&data->cmdlock);
304 return ret;
307 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
308 u8 value)
310 int ret;
312 ret = i2c_smbus_write_byte_data(data->client,
313 SI1145_REG_PARAM_WR, value);
314 if (ret < 0)
315 return ret;
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)
328 int ret;
330 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
331 if (ret < 0)
332 return ret;
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)
340 u16 result = 0;
341 u8 exponent = 0;
343 if (x < 8)
344 return 0;
346 exponent = (x & 0xf0) >> 4;
347 result = 0x10 | (x & 0x0f);
349 if (exponent >= 4)
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)
357 u32 exponent = 0;
358 u32 significand = 0;
359 u32 tmp = x;
361 if (x == 0x0000)
362 return 0x00;
363 if (x == 0x0001)
364 return 0x08;
366 while (1) {
367 tmp >>= 1;
368 exponent += 1;
369 if (tmp == 1)
370 break;
373 if (exponent < 5) {
374 significand = x << (4 - exponent);
375 return (exponent << 4) | (significand & 0xF);
378 significand = x >> (exponent - 5);
379 if (significand & 1) {
380 significand += 2;
381 if (significand & 0x0040) {
382 exponent += 1;
383 significand >>= 1;
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);
396 else
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)
403 *val = 32000;
404 if (data->part_info->uncompressed_meas_rate)
405 *val2 = data->meas_rate;
406 else
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)
414 int ret = 0;
415 int meas_rate;
417 if (val <= 0 || val > 32000)
418 return -ERANGE;
419 meas_rate = 32000 / val;
421 mutex_lock(&data->lock);
422 if (data->autonomous) {
423 ret = si1145_set_meas_rate(data, meas_rate);
424 if (ret)
425 goto out;
427 if (data->part_info->uncompressed_meas_rate)
428 data->meas_rate = meas_rate;
429 else
430 data->meas_rate = si1145_compress(meas_rate);
432 out:
433 mutex_unlock(&data->lock);
435 return ret;
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 +
446 * 8 bytes timestamp
448 u8 buffer[24];
449 int i, j = 0;
450 int ret;
451 u8 irq_status = 0;
453 if (!data->autonomous) {
454 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
455 if (ret < 0 && ret != -EOVERFLOW)
456 goto done;
457 } else {
458 irq_status = ret = i2c_smbus_read_byte_data(data->client,
459 SI1145_REG_IRQ_STATUS);
460 if (ret < 0)
461 goto done;
462 if (!(irq_status & SI1145_MASK_ALL_IE))
463 goto done;
466 for_each_set_bit(i, indio_dev->active_scan_mask,
467 indio_dev->masklength) {
468 int run = 1;
470 while (i + run < indio_dev->masklength) {
471 if (!test_bit(i + run, indio_dev->active_scan_mask))
472 break;
473 if (indio_dev->channels[i + run].address !=
474 indio_dev->channels[i].address + 2 * run)
475 break;
476 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]);
482 if (ret < 0)
483 goto done;
484 j += run * sizeof(u16);
485 i += run - 1;
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);
492 if (ret < 0)
493 goto done;
496 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
497 iio_get_time_ns(indio_dev));
499 done:
500 iio_trigger_notify_done(indio_dev->trig);
501 return IRQ_HANDLED;
504 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
506 struct si1145_data *data = iio_priv(indio_dev);
507 u8 reg = 0, mux;
508 int ret;
509 int i;
511 /* channel list already set, no need to reprogram */
512 if (data->scan_mask == scan_mask)
513 return 0;
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;
519 break;
520 case SI1145_REG_ALSIR_DATA:
521 reg |= SI1145_CHLIST_EN_ALSIR;
522 break;
523 case SI1145_REG_PS1_DATA:
524 reg |= SI1145_CHLIST_EN_PS1;
525 break;
526 case SI1145_REG_PS2_DATA:
527 reg |= SI1145_CHLIST_EN_PS2;
528 break;
529 case SI1145_REG_PS3_DATA:
530 reg |= SI1145_CHLIST_EN_PS3;
531 break;
532 case SI1145_REG_AUX_DATA:
533 switch (indio_dev->channels[i].type) {
534 case IIO_UVINDEX:
535 reg |= SI1145_CHLIST_EN_UV;
536 break;
537 default:
538 reg |= SI1145_CHLIST_EN_AUX;
539 if (indio_dev->channels[i].type == IIO_TEMP)
540 mux = SI1145_MUX_TEMP;
541 else
542 mux = SI1145_MUX_VDD;
543 ret = si1145_param_set(data,
544 SI1145_PARAM_AUX_ADC_MUX, mux);
545 if (ret < 0)
546 return ret;
548 break;
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);
563 u8 cmd;
564 int ret;
566 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
567 if (ret < 0)
568 return ret;
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)
574 return ret;
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,
588 "128 64 32 16 8 4");
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)
604 return -EINVAL;
606 return val;
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)
614 return -EINVAL;
616 return val;
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);
624 int ret;
625 u8 reg;
627 switch (mask) {
628 case IIO_CHAN_INFO_RAW:
629 switch (chan->type) {
630 case IIO_INTENSITY:
631 case IIO_PROXIMITY:
632 case IIO_VOLTAGE:
633 case IIO_TEMP:
634 case IIO_UVINDEX:
635 ret = iio_device_claim_direct_mode(indio_dev);
636 if (ret)
637 return ret;
638 ret = si1145_measure(indio_dev, chan);
639 iio_device_release_direct_mode(indio_dev);
641 if (ret < 0)
642 return ret;
644 *val = ret;
646 return IIO_VAL_INT;
647 case IIO_CURRENT:
648 ret = i2c_smbus_read_byte_data(data->client,
649 SI1145_PS_LED_REG(chan->channel));
650 if (ret < 0)
651 return ret;
653 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
654 & 0x0f;
656 return IIO_VAL_INT;
657 default:
658 return -EINVAL;
660 case IIO_CHAN_INFO_SCALE:
661 switch (chan->type) {
662 case IIO_PROXIMITY:
663 reg = SI1145_PARAM_PS_ADC_GAIN;
664 break;
665 case IIO_INTENSITY:
666 if (chan->channel2 == IIO_MOD_LIGHT_IR)
667 reg = SI1145_PARAM_ALSIR_ADC_GAIN;
668 else
669 reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
670 break;
671 case IIO_TEMP:
672 *val = 28;
673 *val2 = 571429;
674 return IIO_VAL_INT_PLUS_MICRO;
675 case IIO_UVINDEX:
676 *val = 0;
677 *val2 = 10000;
678 return IIO_VAL_INT_PLUS_MICRO;
679 default:
680 return -EINVAL;
683 ret = si1145_param_query(data, reg);
684 if (ret < 0)
685 return ret;
687 *val = si1145_scale_from_adcgain(ret & 0x07);
689 return IIO_VAL_INT;
690 case IIO_CHAN_INFO_OFFSET:
691 switch (chan->type) {
692 case IIO_TEMP:
694 * -ADC offset - ADC counts @ 25°C -
695 * 35 * ADC counts / °C
697 *val = -256 - 11136 + 25 * 35;
698 return IIO_VAL_INT;
699 default:
701 * All ADC measurements have are by default offset
702 * by -256
703 * See AN498 5.6.3
705 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
706 if (ret < 0)
707 return ret;
708 *val = -si1145_uncompress(ret);
709 return IIO_VAL_INT;
711 case IIO_CHAN_INFO_SAMP_FREQ:
712 return si1145_read_samp_freq(data, val, val2);
713 default:
714 return -EINVAL;
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;
724 int ret;
726 switch (mask) {
727 case IIO_CHAN_INFO_SCALE:
728 switch (chan->type) {
729 case IIO_PROXIMITY:
730 val = si1145_proximity_adcgain_from_scale(val, val2);
731 if (val < 0)
732 return val;
733 reg1 = SI1145_PARAM_PS_ADC_GAIN;
734 reg2 = SI1145_PARAM_PS_ADC_COUNTER;
735 break;
736 case IIO_INTENSITY:
737 val = si1145_intensity_adcgain_from_scale(val, val2);
738 if (val < 0)
739 return val;
740 if (chan->channel2 == IIO_MOD_LIGHT_IR) {
741 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
742 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
743 } else {
744 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
745 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
747 break;
748 default:
749 return -EINVAL;
752 ret = iio_device_claim_direct_mode(indio_dev);
753 if (ret)
754 return ret;
756 ret = si1145_param_set(data, reg1, val);
757 if (ret < 0) {
758 iio_device_release_direct_mode(indio_dev);
759 return ret;
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);
764 return ret;
765 case IIO_CHAN_INFO_RAW:
766 if (chan->type != IIO_CURRENT)
767 return -EINVAL;
769 if (val < 0 || val > 15 || val2 != 0)
770 return -EINVAL;
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);
776 if (ret)
777 return ret;
779 ret = i2c_smbus_read_byte_data(data->client, reg1);
780 if (ret < 0) {
781 iio_device_release_direct_mode(indio_dev);
782 return ret;
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);
788 return ret;
789 case IIO_CHAN_INFO_SAMP_FREQ:
790 return si1145_store_samp_freq(data, val);
791 default:
792 return -EINVAL;
796 #define SI1145_ST { \
797 .sign = 'u', \
798 .realbits = 16, \
799 .storagebits = 16, \
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, \
810 .scan_index = _si, \
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), \
820 .modified = 1, \
821 .channel2 = IIO_MOD_LIGHT_IR, \
822 .scan_type = SI1145_ST, \
823 .scan_index = _si, \
824 .address = SI1145_REG_ALSIR_DATA, \
827 #define SI1145_TEMP_CHANNEL(_si) { \
828 .type = IIO_TEMP, \
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, \
834 .scan_index = _si, \
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, \
844 .scan_index = _si, \
845 .address = SI1145_REG_AUX_DATA, \
848 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
849 .type = IIO_PROXIMITY, \
850 .indexed = 1, \
851 .channel = _ch, \
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, \
857 .scan_index = _si, \
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, \
866 .scan_index = _si, \
867 .address = SI1145_REG_AUX_DATA, \
870 #define SI1145_CURRENT_CHANNEL(_ch) { \
871 .type = IIO_CURRENT, \
872 .indexed = 1, \
873 .channel = _ch, \
874 .output = 1, \
875 .scan_index = -1, \
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,
966 NULL,
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,
973 NULL,
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;
1013 int ret;
1015 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1016 SI1145_CMD_RESET);
1017 if (ret < 0)
1018 return ret;
1019 msleep(SI1145_COMMAND_TIMEOUT_MS);
1021 /* Hardware key, magic value */
1022 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1023 if (ret < 0)
1024 return ret;
1025 msleep(SI1145_COMMAND_TIMEOUT_MS);
1027 /* Turn off autonomous mode */
1028 ret = si1145_set_meas_rate(data, 0);
1029 if (ret < 0)
1030 return ret;
1032 /* Initialize sampling freq to 10 Hz */
1033 ret = si1145_store_samp_freq(data, 10);
1034 if (ret < 0)
1035 return ret;
1037 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1038 switch (data->part_info->num_leds) {
1039 case 3:
1040 ret = i2c_smbus_write_byte_data(client,
1041 SI1145_REG_PS_LED3,
1042 SI1145_LED_CURRENT_45mA);
1043 if (ret < 0)
1044 return ret;
1045 /* fallthrough */
1046 case 2:
1047 ret = i2c_smbus_write_byte_data(client,
1048 SI1145_REG_PS_LED21,
1049 (SI1145_LED_CURRENT_45mA << 4) |
1050 SI1145_LED_CURRENT_45mA);
1051 break;
1052 case 1:
1053 ret = i2c_smbus_write_byte_data(client,
1054 SI1145_REG_PS_LED21,
1055 SI1145_LED_CURRENT_45mA);
1056 break;
1057 default:
1058 ret = 0;
1059 break;
1061 if (ret < 0)
1062 return ret;
1064 /* Set normal proximity measurement mode */
1065 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1066 SI1145_PS_ADC_MODE_NORMAL);
1067 if (ret < 0)
1068 return ret;
1070 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1071 if (ret < 0)
1072 return ret;
1074 /* ADC_COUNTER should be one complement of ADC_GAIN */
1075 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1076 if (ret < 0)
1077 return ret;
1079 /* Set ALS visible measurement mode */
1080 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1081 SI1145_ADC_MISC_RANGE);
1082 if (ret < 0)
1083 return ret;
1085 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1086 if (ret < 0)
1087 return ret;
1089 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1090 0x04 << 4);
1091 if (ret < 0)
1092 return ret;
1094 /* Set ALS IR measurement mode */
1095 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1096 SI1145_ADC_MISC_RANGE);
1097 if (ret < 0)
1098 return ret;
1100 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1101 if (ret < 0)
1102 return ret;
1104 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1105 0x06 << 4);
1106 if (ret < 0)
1107 return ret;
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,
1118 SI1145_REG_UCOEF1,
1119 SI1145_UCOEF1_DEFAULT);
1120 if (ret < 0)
1121 return ret;
1122 ret = i2c_smbus_write_byte_data(data->client,
1123 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1124 if (ret < 0)
1125 return ret;
1126 ret = i2c_smbus_write_byte_data(data->client,
1127 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1128 if (ret < 0)
1129 return ret;
1130 ret = i2c_smbus_write_byte_data(data->client,
1131 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1132 if (ret < 0)
1133 return ret;
1136 return 0;
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);
1147 int ret;
1149 mutex_lock(&data->lock);
1150 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1151 mutex_unlock(&data->lock);
1153 return ret;
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;
1161 int i;
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)
1166 count++;
1169 return count <= 1;
1172 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1173 .preenable = si1145_buffer_preenable,
1174 .postenable = iio_triggered_buffer_postenable,
1175 .predisable = iio_triggered_buffer_predisable,
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);
1189 int err = 0, ret;
1191 mutex_lock(&data->lock);
1193 if (state) {
1194 data->autonomous = true;
1195 err = i2c_smbus_write_byte_data(data->client,
1196 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1197 if (err < 0)
1198 goto disable;
1199 err = i2c_smbus_write_byte_data(data->client,
1200 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1201 if (err < 0)
1202 goto disable;
1203 err = si1145_set_meas_rate(data, data->meas_rate);
1204 if (err < 0)
1205 goto disable;
1206 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1207 if (err < 0)
1208 goto disable;
1209 } else {
1210 disable:
1211 /* Disable as much as possible skipping errors */
1212 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1213 if (ret < 0 && !err)
1214 err = ret;
1215 ret = si1145_set_meas_rate(data, 0);
1216 if (ret < 0 && !err)
1217 err = ret;
1218 ret = i2c_smbus_write_byte_data(data->client,
1219 SI1145_REG_IRQ_ENABLE, 0);
1220 if (ret < 0 && !err)
1221 err = ret;
1222 ret = i2c_smbus_write_byte_data(data->client,
1223 SI1145_REG_INT_CFG, 0);
1224 if (ret < 0 && !err)
1225 err = ret;
1226 data->autonomous = false;
1229 mutex_unlock(&data->lock);
1230 return err;
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;
1242 int ret;
1244 trig = devm_iio_trigger_alloc(&client->dev,
1245 "%s-dev%d", indio_dev->name, indio_dev->id);
1246 if (!trig)
1247 return -ENOMEM;
1249 trig->dev.parent = &client->dev;
1250 trig->ops = &si1145_trigger_ops;
1251 iio_trigger_set_drvdata(trig, indio_dev);
1253 ret = devm_request_irq(&client->dev, client->irq,
1254 iio_trigger_generic_data_rdy_poll,
1255 IRQF_TRIGGER_FALLING,
1256 "si1145_irq",
1257 trig);
1258 if (ret < 0) {
1259 dev_err(&client->dev, "irq request failed\n");
1260 return ret;
1263 ret = devm_iio_trigger_register(&client->dev, trig);
1264 if (ret)
1265 return ret;
1267 data->trig = trig;
1268 indio_dev->trig = iio_trigger_get(data->trig);
1270 return 0;
1273 static int si1145_probe(struct i2c_client *client,
1274 const struct i2c_device_id *id)
1276 struct si1145_data *data;
1277 struct iio_dev *indio_dev;
1278 u8 part_id, rev_id, seq_id;
1279 int ret;
1281 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1282 if (!indio_dev)
1283 return -ENOMEM;
1285 data = iio_priv(indio_dev);
1286 i2c_set_clientdata(client, indio_dev);
1287 data->client = client;
1288 data->part_info = &si1145_part_info[id->driver_data];
1290 part_id = ret = i2c_smbus_read_byte_data(data->client,
1291 SI1145_REG_PART_ID);
1292 if (ret < 0)
1293 return ret;
1294 rev_id = ret = i2c_smbus_read_byte_data(data->client,
1295 SI1145_REG_REV_ID);
1296 if (ret < 0)
1297 return ret;
1298 seq_id = ret = i2c_smbus_read_byte_data(data->client,
1299 SI1145_REG_SEQ_ID);
1300 if (ret < 0)
1301 return ret;
1302 dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1303 part_id, rev_id, seq_id);
1304 if (part_id != data->part_info->part) {
1305 dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1306 part_id, data->part_info->part);
1307 return -ENODEV;
1310 indio_dev->dev.parent = &client->dev;
1311 indio_dev->name = id->name;
1312 indio_dev->channels = data->part_info->channels;
1313 indio_dev->num_channels = data->part_info->num_channels;
1314 indio_dev->info = data->part_info->iio_info;
1315 indio_dev->modes = INDIO_DIRECT_MODE;
1317 mutex_init(&data->lock);
1318 mutex_init(&data->cmdlock);
1320 ret = si1145_initialize(data);
1321 if (ret < 0)
1322 return ret;
1324 ret = devm_iio_triggered_buffer_setup(&client->dev,
1325 indio_dev, NULL,
1326 si1145_trigger_handler, &si1145_buffer_setup_ops);
1327 if (ret < 0)
1328 return ret;
1330 if (client->irq) {
1331 ret = si1145_probe_trigger(indio_dev);
1332 if (ret < 0)
1333 return ret;
1334 } else {
1335 dev_info(&client->dev, "no irq, using polling\n");
1338 return devm_iio_device_register(&client->dev, indio_dev);
1341 static const struct i2c_device_id si1145_ids[] = {
1342 { "si1132", SI1132 },
1343 { "si1141", SI1141 },
1344 { "si1142", SI1142 },
1345 { "si1143", SI1143 },
1346 { "si1145", SI1145 },
1347 { "si1146", SI1146 },
1348 { "si1147", SI1147 },
1351 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1353 static struct i2c_driver si1145_driver = {
1354 .driver = {
1355 .name = "si1145",
1357 .probe = si1145_probe,
1358 .id_table = si1145_ids,
1361 module_i2c_driver(si1145_driver);
1363 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1364 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1365 MODULE_LICENSE("GPL");