xref: /openbmc/linux/drivers/iio/light/si1145.c (revision 6cd70754)
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
5  *
6  * Copyright 2014-16 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
7  * Copyright 2016 Crestez Dan Leonard <leonard.crestez@intel.com>
8  *
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)
12  */
13 
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>
20 
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>
28 
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
55 
56 #define SI1145_UCOEF1_DEFAULT		0x7b
57 #define SI1145_UCOEF2_DEFAULT		0x6b
58 #define SI1145_UCOEF3_DEFAULT		0x01
59 #define SI1145_UCOEF4_DEFAULT		0x00
60 
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)
66 
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
90 
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)
99 
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)
104 
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
119 
120 #define SI1145_RSP_INVALID_SETTING	0x80
121 #define SI1145_RSP_COUNTER_MASK		0x0F
122 
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
127 
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 */
131 
132 /* Interrupt enable masks for IRQ_ENABLE register */
133 #define SI1145_MASK_ALL_IE		(BIT(4) | BIT(3) | BIT(2) | BIT(0))
134 
135 #define SI1145_MUX_TEMP			0x65
136 #define SI1145_MUX_VDD			0x75
137 
138 /* Proximity LED current; see Table 2 in datasheet */
139 #define SI1145_LED_CURRENT_45mA		0x04
140 
141 enum {
142 	SI1132,
143 	SI1141,
144 	SI1142,
145 	SI1143,
146 	SI1145,
147 	SI1146,
148 	SI1147,
149 };
150 
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;
158 };
159 
160 /**
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.
172  */
173 struct si1145_data {
174 	struct i2c_client *client;
175 	struct mutex lock;
176 	struct mutex cmdlock;
177 	int rsp_seq;
178 	const struct si1145_part_info *part_info;
179 	unsigned long scan_mask;
180 	bool autonomous;
181 	struct iio_trigger *trig;
182 	int meas_rate;
183 	/*
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 +
188 	 *   8 bytes timestamp
189 	 */
190 	u8 buffer[24] __aligned(8);
191 };
192 
193 /*
194  * __si1145_command_reset() - Send CMD_NOP and wait for response 0
195  *
196  * Does not modify data->rsp_seq
197  *
198  * Return: 0 on success and -errno on error.
199  */
200 static int __si1145_command_reset(struct si1145_data *data)
201 {
202 	struct device *dev = &data->client->dev;
203 	unsigned long stop_jiffies;
204 	int ret;
205 
206 	ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
207 						      SI1145_CMD_NOP);
208 	if (ret < 0)
209 		return ret;
210 	msleep(SI1145_COMMAND_MINSLEEP_MS);
211 
212 	stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
213 	while (true) {
214 		ret = i2c_smbus_read_byte_data(data->client,
215 					       SI1145_REG_RESPONSE);
216 		if (ret <= 0)
217 			return ret;
218 		if (time_after(jiffies, stop_jiffies)) {
219 			dev_warn(dev, "timeout on reset\n");
220 			return -ETIMEDOUT;
221 		}
222 		msleep(SI1145_COMMAND_MINSLEEP_MS);
223 		continue;
224 	}
225 }
226 
227 /*
228  * si1145_command() - Execute a command and poll the response register
229  *
230  * All conversion overflows are reported as -EOVERFLOW
231  * INVALID_SETTING is reported as -EINVAL
232  * Timeouts are reported as -ETIMEDOUT
233  *
234  * Return: 0 on success or -errno on failure
235  */
236 static int si1145_command(struct si1145_data *data, u8 cmd)
237 {
238 	struct device *dev = &data->client->dev;
239 	unsigned long stop_jiffies;
240 	int ret;
241 
242 	mutex_lock(&data->cmdlock);
243 
244 	if (data->rsp_seq < 0) {
245 		ret = __si1145_command_reset(data);
246 		if (ret < 0) {
247 			dev_err(dev, "failed to reset command counter, ret=%d\n",
248 				ret);
249 			goto out;
250 		}
251 		data->rsp_seq = 0;
252 	}
253 
254 	ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
255 	if (ret) {
256 		dev_warn(dev, "failed to write command, ret=%d\n", ret);
257 		goto out;
258 	}
259 	/* Sleep a little to ensure the command is received */
260 	msleep(SI1145_COMMAND_MINSLEEP_MS);
261 
262 	stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
263 	while (true) {
264 		ret = i2c_smbus_read_byte_data(data->client,
265 					       SI1145_REG_RESPONSE);
266 		if (ret < 0) {
267 			dev_warn(dev, "failed to read response, ret=%d\n", ret);
268 			break;
269 		}
270 
271 		if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
272 			if (ret == data->rsp_seq) {
273 				if (time_after(jiffies, stop_jiffies)) {
274 					dev_warn(dev, "timeout on command %#02hhx\n",
275 						 cmd);
276 					ret = -ETIMEDOUT;
277 					break;
278 				}
279 				msleep(SI1145_COMMAND_MINSLEEP_MS);
280 				continue;
281 			}
282 			if (ret == ((data->rsp_seq + 1) &
283 				SI1145_RSP_COUNTER_MASK)) {
284 				data->rsp_seq = ret;
285 				ret = 0;
286 				break;
287 			}
288 			dev_warn(dev, "unexpected response counter %d instead of %d\n",
289 				 ret, (data->rsp_seq + 1) &
290 					SI1145_RSP_COUNTER_MASK);
291 			ret = -EIO;
292 		} else {
293 			if (ret == SI1145_RSP_INVALID_SETTING) {
294 				dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
295 					 cmd);
296 				ret = -EINVAL;
297 			} else {
298 				/* All overflows are treated identically */
299 				dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
300 					ret, cmd);
301 				ret = -EOVERFLOW;
302 			}
303 		}
304 
305 		/* Force a counter reset next time */
306 		data->rsp_seq = -1;
307 		break;
308 	}
309 
310 out:
311 	mutex_unlock(&data->cmdlock);
312 
313 	return ret;
314 }
315 
316 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
317 			       u8 value)
318 {
319 	int ret;
320 
321 	ret = i2c_smbus_write_byte_data(data->client,
322 		SI1145_REG_PARAM_WR, value);
323 	if (ret < 0)
324 		return ret;
325 
326 	return si1145_command(data, op | (param & 0x1F));
327 }
328 
329 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
330 {
331 	return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
332 }
333 
334 /* Set param. Returns negative errno or current value */
335 static int si1145_param_query(struct si1145_data *data, u8 param)
336 {
337 	int ret;
338 
339 	ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
340 	if (ret < 0)
341 		return ret;
342 
343 	return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
344 }
345 
346 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
347 static u16 si1145_uncompress(u8 x)
348 {
349 	u16 result = 0;
350 	u8 exponent = 0;
351 
352 	if (x < 8)
353 		return 0;
354 
355 	exponent = (x & 0xf0) >> 4;
356 	result = 0x10 | (x & 0x0f);
357 
358 	if (exponent >= 4)
359 		return result << (exponent - 4);
360 	return result >> (4 - exponent);
361 }
362 
363 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
364 static u8 si1145_compress(u16 x)
365 {
366 	u32 exponent = 0;
367 	u32 significand = 0;
368 	u32 tmp = x;
369 
370 	if (x == 0x0000)
371 		return 0x00;
372 	if (x == 0x0001)
373 		return 0x08;
374 
375 	while (1) {
376 		tmp >>= 1;
377 		exponent += 1;
378 		if (tmp == 1)
379 			break;
380 	}
381 
382 	if (exponent < 5) {
383 		significand = x << (4 - exponent);
384 		return (exponent << 4) | (significand & 0xF);
385 	}
386 
387 	significand = x >> (exponent - 5);
388 	if (significand & 1) {
389 		significand += 2;
390 		if (significand & 0x0040) {
391 			exponent += 1;
392 			significand >>= 1;
393 		}
394 	}
395 
396 	return (exponent << 4) | ((significand >> 1) & 0xF);
397 }
398 
399 /* Write meas_rate in hardware */
400 static int si1145_set_meas_rate(struct si1145_data *data, int interval)
401 {
402 	if (data->part_info->uncompressed_meas_rate)
403 		return i2c_smbus_write_word_data(data->client,
404 			SI1145_REG_MEAS_RATE, interval);
405 	else
406 		return i2c_smbus_write_byte_data(data->client,
407 			SI1145_REG_MEAS_RATE, interval);
408 }
409 
410 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
411 {
412 	*val = 32000;
413 	if (data->part_info->uncompressed_meas_rate)
414 		*val2 = data->meas_rate;
415 	else
416 		*val2 = si1145_uncompress(data->meas_rate);
417 	return IIO_VAL_FRACTIONAL;
418 }
419 
420 /* Set the samp freq in driver private data */
421 static int si1145_store_samp_freq(struct si1145_data *data, int val)
422 {
423 	int ret = 0;
424 	int meas_rate;
425 
426 	if (val <= 0 || val > 32000)
427 		return -ERANGE;
428 	meas_rate = 32000 / val;
429 
430 	mutex_lock(&data->lock);
431 	if (data->autonomous) {
432 		ret = si1145_set_meas_rate(data, meas_rate);
433 		if (ret)
434 			goto out;
435 	}
436 	if (data->part_info->uncompressed_meas_rate)
437 		data->meas_rate = meas_rate;
438 	else
439 		data->meas_rate = si1145_compress(meas_rate);
440 
441 out:
442 	mutex_unlock(&data->lock);
443 
444 	return ret;
445 }
446 
447 static irqreturn_t si1145_trigger_handler(int irq, void *private)
448 {
449 	struct iio_poll_func *pf = private;
450 	struct iio_dev *indio_dev = pf->indio_dev;
451 	struct si1145_data *data = iio_priv(indio_dev);
452 	int i, j = 0;
453 	int ret;
454 	u8 irq_status = 0;
455 
456 	if (!data->autonomous) {
457 		ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
458 		if (ret < 0 && ret != -EOVERFLOW)
459 			goto done;
460 	} else {
461 		irq_status = ret = i2c_smbus_read_byte_data(data->client,
462 				SI1145_REG_IRQ_STATUS);
463 		if (ret < 0)
464 			goto done;
465 		if (!(irq_status & SI1145_MASK_ALL_IE))
466 			goto done;
467 	}
468 
469 	for_each_set_bit(i, indio_dev->active_scan_mask,
470 		indio_dev->masklength) {
471 		int run = 1;
472 
473 		while (i + run < indio_dev->masklength) {
474 			if (!test_bit(i + run, indio_dev->active_scan_mask))
475 				break;
476 			if (indio_dev->channels[i + run].address !=
477 				indio_dev->channels[i].address + 2 * run)
478 				break;
479 			run++;
480 		}
481 
482 		ret = i2c_smbus_read_i2c_block_data_or_emulated(
483 				data->client, indio_dev->channels[i].address,
484 				sizeof(u16) * run, &data->buffer[j]);
485 		if (ret < 0)
486 			goto done;
487 		j += run * sizeof(u16);
488 		i += run - 1;
489 	}
490 
491 	if (data->autonomous) {
492 		ret = i2c_smbus_write_byte_data(data->client,
493 				SI1145_REG_IRQ_STATUS,
494 				irq_status & SI1145_MASK_ALL_IE);
495 		if (ret < 0)
496 			goto done;
497 	}
498 
499 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
500 		iio_get_time_ns(indio_dev));
501 
502 done:
503 	iio_trigger_notify_done(indio_dev->trig);
504 	return IRQ_HANDLED;
505 }
506 
507 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
508 {
509 	struct si1145_data *data = iio_priv(indio_dev);
510 	u8 reg = 0, mux;
511 	int ret;
512 	int i;
513 
514 	/* channel list already set, no need to reprogram */
515 	if (data->scan_mask == scan_mask)
516 		return 0;
517 
518 	for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
519 		switch (indio_dev->channels[i].address) {
520 		case SI1145_REG_ALSVIS_DATA:
521 			reg |= SI1145_CHLIST_EN_ALSVIS;
522 			break;
523 		case SI1145_REG_ALSIR_DATA:
524 			reg |= SI1145_CHLIST_EN_ALSIR;
525 			break;
526 		case SI1145_REG_PS1_DATA:
527 			reg |= SI1145_CHLIST_EN_PS1;
528 			break;
529 		case SI1145_REG_PS2_DATA:
530 			reg |= SI1145_CHLIST_EN_PS2;
531 			break;
532 		case SI1145_REG_PS3_DATA:
533 			reg |= SI1145_CHLIST_EN_PS3;
534 			break;
535 		case SI1145_REG_AUX_DATA:
536 			switch (indio_dev->channels[i].type) {
537 			case IIO_UVINDEX:
538 				reg |= SI1145_CHLIST_EN_UV;
539 				break;
540 			default:
541 				reg |= SI1145_CHLIST_EN_AUX;
542 				if (indio_dev->channels[i].type == IIO_TEMP)
543 					mux = SI1145_MUX_TEMP;
544 				else
545 					mux = SI1145_MUX_VDD;
546 				ret = si1145_param_set(data,
547 					SI1145_PARAM_AUX_ADC_MUX, mux);
548 				if (ret < 0)
549 					return ret;
550 
551 				break;
552 			}
553 		}
554 	}
555 
556 	data->scan_mask = scan_mask;
557 	ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
558 
559 	return ret < 0 ? ret : 0;
560 }
561 
562 static int si1145_measure(struct iio_dev *indio_dev,
563 			  struct iio_chan_spec const *chan)
564 {
565 	struct si1145_data *data = iio_priv(indio_dev);
566 	u8 cmd;
567 	int ret;
568 
569 	ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
570 	if (ret < 0)
571 		return ret;
572 
573 	cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
574 		SI1145_CMD_ALS_FORCE;
575 	ret = si1145_command(data, cmd);
576 	if (ret < 0 && ret != -EOVERFLOW)
577 		return ret;
578 
579 	return i2c_smbus_read_word_data(data->client, chan->address);
580 }
581 
582 /*
583  * Conversion between iio scale and ADC_GAIN values
584  * These could be further adjusted but proximity/intensity are dimensionless
585  */
586 static const int si1145_proximity_scale_available[] = {
587 	128, 64, 32, 16, 8, 4};
588 static const int si1145_intensity_scale_available[] = {
589 	128, 64, 32, 16, 8, 4, 2, 1};
590 static IIO_CONST_ATTR(in_proximity_scale_available,
591 	"128 64 32 16 8 4");
592 static IIO_CONST_ATTR(in_intensity_scale_available,
593 	"128 64 32 16 8 4 2 1");
594 static IIO_CONST_ATTR(in_intensity_ir_scale_available,
595 	"128 64 32 16 8 4 2 1");
596 
597 static int si1145_scale_from_adcgain(int regval)
598 {
599 	return 128 >> regval;
600 }
601 
602 static int si1145_proximity_adcgain_from_scale(int val, int val2)
603 {
604 	val = find_closest_descending(val, si1145_proximity_scale_available,
605 				ARRAY_SIZE(si1145_proximity_scale_available));
606 	if (val < 0 || val > 5 || val2 != 0)
607 		return -EINVAL;
608 
609 	return val;
610 }
611 
612 static int si1145_intensity_adcgain_from_scale(int val, int val2)
613 {
614 	val = find_closest_descending(val, si1145_intensity_scale_available,
615 				ARRAY_SIZE(si1145_intensity_scale_available));
616 	if (val < 0 || val > 7 || val2 != 0)
617 		return -EINVAL;
618 
619 	return val;
620 }
621 
622 static int si1145_read_raw(struct iio_dev *indio_dev,
623 				struct iio_chan_spec const *chan,
624 				int *val, int *val2, long mask)
625 {
626 	struct si1145_data *data = iio_priv(indio_dev);
627 	int ret;
628 	u8 reg;
629 
630 	switch (mask) {
631 	case IIO_CHAN_INFO_RAW:
632 		switch (chan->type) {
633 		case IIO_INTENSITY:
634 		case IIO_PROXIMITY:
635 		case IIO_VOLTAGE:
636 		case IIO_TEMP:
637 		case IIO_UVINDEX:
638 			ret = iio_device_claim_direct_mode(indio_dev);
639 			if (ret)
640 				return ret;
641 			ret = si1145_measure(indio_dev, chan);
642 			iio_device_release_direct_mode(indio_dev);
643 
644 			if (ret < 0)
645 				return ret;
646 
647 			*val = ret;
648 
649 			return IIO_VAL_INT;
650 		case IIO_CURRENT:
651 			ret = i2c_smbus_read_byte_data(data->client,
652 				SI1145_PS_LED_REG(chan->channel));
653 			if (ret < 0)
654 				return ret;
655 
656 			*val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
657 				& 0x0f;
658 
659 			return IIO_VAL_INT;
660 		default:
661 			return -EINVAL;
662 		}
663 	case IIO_CHAN_INFO_SCALE:
664 		switch (chan->type) {
665 		case IIO_PROXIMITY:
666 			reg = SI1145_PARAM_PS_ADC_GAIN;
667 			break;
668 		case IIO_INTENSITY:
669 			if (chan->channel2 == IIO_MOD_LIGHT_IR)
670 				reg = SI1145_PARAM_ALSIR_ADC_GAIN;
671 			else
672 				reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
673 			break;
674 		case IIO_TEMP:
675 			*val = 28;
676 			*val2 = 571429;
677 			return IIO_VAL_INT_PLUS_MICRO;
678 		case IIO_UVINDEX:
679 			*val = 0;
680 			*val2 = 10000;
681 			return IIO_VAL_INT_PLUS_MICRO;
682 		default:
683 			return -EINVAL;
684 		}
685 
686 		ret = si1145_param_query(data, reg);
687 		if (ret < 0)
688 			return ret;
689 
690 		*val = si1145_scale_from_adcgain(ret & 0x07);
691 
692 		return IIO_VAL_INT;
693 	case IIO_CHAN_INFO_OFFSET:
694 		switch (chan->type) {
695 		case IIO_TEMP:
696 			/*
697 			 * -ADC offset - ADC counts @ 25°C -
698 			 *   35 * ADC counts / °C
699 			 */
700 			*val = -256 - 11136 + 25 * 35;
701 			return IIO_VAL_INT;
702 		default:
703 			/*
704 			 * All ADC measurements have are by default offset
705 			 * by -256
706 			 * See AN498 5.6.3
707 			 */
708 			ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
709 			if (ret < 0)
710 				return ret;
711 			*val = -si1145_uncompress(ret);
712 			return IIO_VAL_INT;
713 		}
714 	case IIO_CHAN_INFO_SAMP_FREQ:
715 		return si1145_read_samp_freq(data, val, val2);
716 	default:
717 		return -EINVAL;
718 	}
719 }
720 
721 static int si1145_write_raw(struct iio_dev *indio_dev,
722 			       struct iio_chan_spec const *chan,
723 			       int val, int val2, long mask)
724 {
725 	struct si1145_data *data = iio_priv(indio_dev);
726 	u8 reg1, reg2, shift;
727 	int ret;
728 
729 	switch (mask) {
730 	case IIO_CHAN_INFO_SCALE:
731 		switch (chan->type) {
732 		case IIO_PROXIMITY:
733 			val = si1145_proximity_adcgain_from_scale(val, val2);
734 			if (val < 0)
735 				return val;
736 			reg1 = SI1145_PARAM_PS_ADC_GAIN;
737 			reg2 = SI1145_PARAM_PS_ADC_COUNTER;
738 			break;
739 		case IIO_INTENSITY:
740 			val = si1145_intensity_adcgain_from_scale(val, val2);
741 			if (val < 0)
742 				return val;
743 			if (chan->channel2 == IIO_MOD_LIGHT_IR) {
744 				reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
745 				reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
746 			} else {
747 				reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
748 				reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
749 			}
750 			break;
751 		default:
752 			return -EINVAL;
753 		}
754 
755 		ret = iio_device_claim_direct_mode(indio_dev);
756 		if (ret)
757 			return ret;
758 
759 		ret = si1145_param_set(data, reg1, val);
760 		if (ret < 0) {
761 			iio_device_release_direct_mode(indio_dev);
762 			return ret;
763 		}
764 		/* Set recovery period to one's complement of gain */
765 		ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
766 		iio_device_release_direct_mode(indio_dev);
767 		return ret;
768 	case IIO_CHAN_INFO_RAW:
769 		if (chan->type != IIO_CURRENT)
770 			return -EINVAL;
771 
772 		if (val < 0 || val > 15 || val2 != 0)
773 			return -EINVAL;
774 
775 		reg1 = SI1145_PS_LED_REG(chan->channel);
776 		shift = SI1145_PS_LED_SHIFT(chan->channel);
777 
778 		ret = iio_device_claim_direct_mode(indio_dev);
779 		if (ret)
780 			return ret;
781 
782 		ret = i2c_smbus_read_byte_data(data->client, reg1);
783 		if (ret < 0) {
784 			iio_device_release_direct_mode(indio_dev);
785 			return ret;
786 		}
787 		ret = i2c_smbus_write_byte_data(data->client, reg1,
788 			(ret & ~(0x0f << shift)) |
789 			((val & 0x0f) << shift));
790 		iio_device_release_direct_mode(indio_dev);
791 		return ret;
792 	case IIO_CHAN_INFO_SAMP_FREQ:
793 		return si1145_store_samp_freq(data, val);
794 	default:
795 		return -EINVAL;
796 	}
797 }
798 
799 #define SI1145_ST { \
800 	.sign = 'u', \
801 	.realbits = 16, \
802 	.storagebits = 16, \
803 	.endianness = IIO_LE, \
804 }
805 
806 #define SI1145_INTENSITY_CHANNEL(_si) { \
807 	.type = IIO_INTENSITY, \
808 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
809 			      BIT(IIO_CHAN_INFO_OFFSET) | \
810 			      BIT(IIO_CHAN_INFO_SCALE), \
811 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
812 	.scan_type = SI1145_ST, \
813 	.scan_index = _si, \
814 	.address = SI1145_REG_ALSVIS_DATA, \
815 }
816 
817 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
818 	.type = IIO_INTENSITY, \
819 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
820 			      BIT(IIO_CHAN_INFO_OFFSET) | \
821 			      BIT(IIO_CHAN_INFO_SCALE), \
822 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
823 	.modified = 1, \
824 	.channel2 = IIO_MOD_LIGHT_IR, \
825 	.scan_type = SI1145_ST, \
826 	.scan_index = _si, \
827 	.address = SI1145_REG_ALSIR_DATA, \
828 }
829 
830 #define SI1145_TEMP_CHANNEL(_si) { \
831 	.type = IIO_TEMP, \
832 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
833 			      BIT(IIO_CHAN_INFO_OFFSET) | \
834 			      BIT(IIO_CHAN_INFO_SCALE), \
835 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
836 	.scan_type = SI1145_ST, \
837 	.scan_index = _si, \
838 	.address = SI1145_REG_AUX_DATA, \
839 }
840 
841 #define SI1145_UV_CHANNEL(_si) { \
842 	.type = IIO_UVINDEX, \
843 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
844 			      BIT(IIO_CHAN_INFO_SCALE), \
845 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
846 	.scan_type = SI1145_ST, \
847 	.scan_index = _si, \
848 	.address = SI1145_REG_AUX_DATA, \
849 }
850 
851 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
852 	.type = IIO_PROXIMITY, \
853 	.indexed = 1, \
854 	.channel = _ch, \
855 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
856 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
857 				    BIT(IIO_CHAN_INFO_OFFSET), \
858 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
859 	.scan_type = SI1145_ST, \
860 	.scan_index = _si, \
861 	.address = SI1145_REG_PS1_DATA + _ch * 2, \
862 }
863 
864 #define SI1145_VOLTAGE_CHANNEL(_si) { \
865 	.type = IIO_VOLTAGE, \
866 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
867 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
868 	.scan_type = SI1145_ST, \
869 	.scan_index = _si, \
870 	.address = SI1145_REG_AUX_DATA, \
871 }
872 
873 #define SI1145_CURRENT_CHANNEL(_ch) { \
874 	.type = IIO_CURRENT, \
875 	.indexed = 1, \
876 	.channel = _ch, \
877 	.output = 1, \
878 	.scan_index = -1, \
879 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
880 }
881 
882 static const struct iio_chan_spec si1132_channels[] = {
883 	SI1145_INTENSITY_CHANNEL(0),
884 	SI1145_INTENSITY_IR_CHANNEL(1),
885 	SI1145_TEMP_CHANNEL(2),
886 	SI1145_VOLTAGE_CHANNEL(3),
887 	SI1145_UV_CHANNEL(4),
888 	IIO_CHAN_SOFT_TIMESTAMP(6),
889 };
890 
891 static const struct iio_chan_spec si1141_channels[] = {
892 	SI1145_INTENSITY_CHANNEL(0),
893 	SI1145_INTENSITY_IR_CHANNEL(1),
894 	SI1145_PROXIMITY_CHANNEL(2, 0),
895 	SI1145_TEMP_CHANNEL(3),
896 	SI1145_VOLTAGE_CHANNEL(4),
897 	IIO_CHAN_SOFT_TIMESTAMP(5),
898 	SI1145_CURRENT_CHANNEL(0),
899 };
900 
901 static const struct iio_chan_spec si1142_channels[] = {
902 	SI1145_INTENSITY_CHANNEL(0),
903 	SI1145_INTENSITY_IR_CHANNEL(1),
904 	SI1145_PROXIMITY_CHANNEL(2, 0),
905 	SI1145_PROXIMITY_CHANNEL(3, 1),
906 	SI1145_TEMP_CHANNEL(4),
907 	SI1145_VOLTAGE_CHANNEL(5),
908 	IIO_CHAN_SOFT_TIMESTAMP(6),
909 	SI1145_CURRENT_CHANNEL(0),
910 	SI1145_CURRENT_CHANNEL(1),
911 };
912 
913 static const struct iio_chan_spec si1143_channels[] = {
914 	SI1145_INTENSITY_CHANNEL(0),
915 	SI1145_INTENSITY_IR_CHANNEL(1),
916 	SI1145_PROXIMITY_CHANNEL(2, 0),
917 	SI1145_PROXIMITY_CHANNEL(3, 1),
918 	SI1145_PROXIMITY_CHANNEL(4, 2),
919 	SI1145_TEMP_CHANNEL(5),
920 	SI1145_VOLTAGE_CHANNEL(6),
921 	IIO_CHAN_SOFT_TIMESTAMP(7),
922 	SI1145_CURRENT_CHANNEL(0),
923 	SI1145_CURRENT_CHANNEL(1),
924 	SI1145_CURRENT_CHANNEL(2),
925 };
926 
927 static const struct iio_chan_spec si1145_channels[] = {
928 	SI1145_INTENSITY_CHANNEL(0),
929 	SI1145_INTENSITY_IR_CHANNEL(1),
930 	SI1145_PROXIMITY_CHANNEL(2, 0),
931 	SI1145_TEMP_CHANNEL(3),
932 	SI1145_VOLTAGE_CHANNEL(4),
933 	SI1145_UV_CHANNEL(5),
934 	IIO_CHAN_SOFT_TIMESTAMP(6),
935 	SI1145_CURRENT_CHANNEL(0),
936 };
937 
938 static const struct iio_chan_spec si1146_channels[] = {
939 	SI1145_INTENSITY_CHANNEL(0),
940 	SI1145_INTENSITY_IR_CHANNEL(1),
941 	SI1145_TEMP_CHANNEL(2),
942 	SI1145_VOLTAGE_CHANNEL(3),
943 	SI1145_UV_CHANNEL(4),
944 	SI1145_PROXIMITY_CHANNEL(5, 0),
945 	SI1145_PROXIMITY_CHANNEL(6, 1),
946 	IIO_CHAN_SOFT_TIMESTAMP(7),
947 	SI1145_CURRENT_CHANNEL(0),
948 	SI1145_CURRENT_CHANNEL(1),
949 };
950 
951 static const struct iio_chan_spec si1147_channels[] = {
952 	SI1145_INTENSITY_CHANNEL(0),
953 	SI1145_INTENSITY_IR_CHANNEL(1),
954 	SI1145_PROXIMITY_CHANNEL(2, 0),
955 	SI1145_PROXIMITY_CHANNEL(3, 1),
956 	SI1145_PROXIMITY_CHANNEL(4, 2),
957 	SI1145_TEMP_CHANNEL(5),
958 	SI1145_VOLTAGE_CHANNEL(6),
959 	SI1145_UV_CHANNEL(7),
960 	IIO_CHAN_SOFT_TIMESTAMP(8),
961 	SI1145_CURRENT_CHANNEL(0),
962 	SI1145_CURRENT_CHANNEL(1),
963 	SI1145_CURRENT_CHANNEL(2),
964 };
965 
966 static struct attribute *si1132_attributes[] = {
967 	&iio_const_attr_in_intensity_scale_available.dev_attr.attr,
968 	&iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
969 	NULL,
970 };
971 
972 static struct attribute *si114x_attributes[] = {
973 	&iio_const_attr_in_intensity_scale_available.dev_attr.attr,
974 	&iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
975 	&iio_const_attr_in_proximity_scale_available.dev_attr.attr,
976 	NULL,
977 };
978 
979 static const struct attribute_group si1132_attribute_group = {
980 	.attrs = si1132_attributes,
981 };
982 
983 static const struct attribute_group si114x_attribute_group = {
984 	.attrs = si114x_attributes,
985 };
986 
987 
988 static const struct iio_info si1132_info = {
989 	.read_raw = si1145_read_raw,
990 	.write_raw = si1145_write_raw,
991 	.attrs = &si1132_attribute_group,
992 };
993 
994 static const struct iio_info si114x_info = {
995 	.read_raw = si1145_read_raw,
996 	.write_raw = si1145_write_raw,
997 	.attrs = &si114x_attribute_group,
998 };
999 
1000 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1001 	{id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1002 
1003 static const struct si1145_part_info si1145_part_info[] = {
1004 	[SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1005 	[SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1006 	[SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1007 	[SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1008 	[SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1009 	[SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1010 	[SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1011 };
1012 
1013 static int si1145_initialize(struct si1145_data *data)
1014 {
1015 	struct i2c_client *client = data->client;
1016 	int ret;
1017 
1018 	ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1019 					SI1145_CMD_RESET);
1020 	if (ret < 0)
1021 		return ret;
1022 	msleep(SI1145_COMMAND_TIMEOUT_MS);
1023 
1024 	/* Hardware key, magic value */
1025 	ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1026 	if (ret < 0)
1027 		return ret;
1028 	msleep(SI1145_COMMAND_TIMEOUT_MS);
1029 
1030 	/* Turn off autonomous mode */
1031 	ret = si1145_set_meas_rate(data, 0);
1032 	if (ret < 0)
1033 		return ret;
1034 
1035 	/* Initialize sampling freq to 10 Hz */
1036 	ret = si1145_store_samp_freq(data, 10);
1037 	if (ret < 0)
1038 		return ret;
1039 
1040 	/* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1041 	switch (data->part_info->num_leds) {
1042 	case 3:
1043 		ret = i2c_smbus_write_byte_data(client,
1044 						SI1145_REG_PS_LED3,
1045 						SI1145_LED_CURRENT_45mA);
1046 		if (ret < 0)
1047 			return ret;
1048 		fallthrough;
1049 	case 2:
1050 		ret = i2c_smbus_write_byte_data(client,
1051 						SI1145_REG_PS_LED21,
1052 						(SI1145_LED_CURRENT_45mA << 4) |
1053 						SI1145_LED_CURRENT_45mA);
1054 		break;
1055 	case 1:
1056 		ret = i2c_smbus_write_byte_data(client,
1057 						SI1145_REG_PS_LED21,
1058 						SI1145_LED_CURRENT_45mA);
1059 		break;
1060 	default:
1061 		ret = 0;
1062 		break;
1063 	}
1064 	if (ret < 0)
1065 		return ret;
1066 
1067 	/* Set normal proximity measurement mode */
1068 	ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1069 			       SI1145_PS_ADC_MODE_NORMAL);
1070 	if (ret < 0)
1071 		return ret;
1072 
1073 	ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1074 	if (ret < 0)
1075 		return ret;
1076 
1077 	/* ADC_COUNTER should be one complement of ADC_GAIN */
1078 	ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1079 	if (ret < 0)
1080 		return ret;
1081 
1082 	/* Set ALS visible measurement mode */
1083 	ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1084 			       SI1145_ADC_MISC_RANGE);
1085 	if (ret < 0)
1086 		return ret;
1087 
1088 	ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1089 	if (ret < 0)
1090 		return ret;
1091 
1092 	ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1093 			       0x04 << 4);
1094 	if (ret < 0)
1095 		return ret;
1096 
1097 	/* Set ALS IR measurement mode */
1098 	ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1099 			       SI1145_ADC_MISC_RANGE);
1100 	if (ret < 0)
1101 		return ret;
1102 
1103 	ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1104 	if (ret < 0)
1105 		return ret;
1106 
1107 	ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1108 			       0x06 << 4);
1109 	if (ret < 0)
1110 		return ret;
1111 
1112 	/*
1113 	 * Initialize UCOEF to default values in datasheet
1114 	 * These registers are normally zero on reset
1115 	 */
1116 	if (data->part_info == &si1145_part_info[SI1132] ||
1117 		data->part_info == &si1145_part_info[SI1145] ||
1118 		data->part_info == &si1145_part_info[SI1146] ||
1119 		data->part_info == &si1145_part_info[SI1147]) {
1120 		ret = i2c_smbus_write_byte_data(data->client,
1121 						SI1145_REG_UCOEF1,
1122 						SI1145_UCOEF1_DEFAULT);
1123 		if (ret < 0)
1124 			return ret;
1125 		ret = i2c_smbus_write_byte_data(data->client,
1126 				SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1127 		if (ret < 0)
1128 			return ret;
1129 		ret = i2c_smbus_write_byte_data(data->client,
1130 				SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1131 		if (ret < 0)
1132 			return ret;
1133 		ret = i2c_smbus_write_byte_data(data->client,
1134 				SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1135 		if (ret < 0)
1136 			return ret;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 /*
1143  * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1144  * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1145  * mode reprograms the channels list anyway...
1146  */
1147 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1148 {
1149 	struct si1145_data *data = iio_priv(indio_dev);
1150 	int ret;
1151 
1152 	mutex_lock(&data->lock);
1153 	ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1154 	mutex_unlock(&data->lock);
1155 
1156 	return ret;
1157 }
1158 
1159 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1160 			       const unsigned long *scan_mask)
1161 {
1162 	struct si1145_data *data = iio_priv(indio_dev);
1163 	unsigned int count = 0;
1164 	int i;
1165 
1166 	/* Check that at most one AUX channel is enabled */
1167 	for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1168 		if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1169 			count++;
1170 	}
1171 
1172 	return count <= 1;
1173 }
1174 
1175 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1176 	.preenable = si1145_buffer_preenable,
1177 	.validate_scan_mask = si1145_validate_scan_mask,
1178 };
1179 
1180 /*
1181  * si1145_trigger_set_state() - Set trigger state
1182  *
1183  * When not using triggers interrupts are disabled and measurement rate is
1184  * set to zero in order to minimize power consumption.
1185  */
1186 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1187 {
1188 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1189 	struct si1145_data *data = iio_priv(indio_dev);
1190 	int err = 0, ret;
1191 
1192 	mutex_lock(&data->lock);
1193 
1194 	if (state) {
1195 		data->autonomous = true;
1196 		err = i2c_smbus_write_byte_data(data->client,
1197 				SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1198 		if (err < 0)
1199 			goto disable;
1200 		err = i2c_smbus_write_byte_data(data->client,
1201 				SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1202 		if (err < 0)
1203 			goto disable;
1204 		err = si1145_set_meas_rate(data, data->meas_rate);
1205 		if (err < 0)
1206 			goto disable;
1207 		err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1208 		if (err < 0)
1209 			goto disable;
1210 	} else {
1211 disable:
1212 		/* Disable as much as possible skipping errors */
1213 		ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1214 		if (ret < 0 && !err)
1215 			err = ret;
1216 		ret = si1145_set_meas_rate(data, 0);
1217 		if (ret < 0 && !err)
1218 			err = ret;
1219 		ret = i2c_smbus_write_byte_data(data->client,
1220 						SI1145_REG_IRQ_ENABLE, 0);
1221 		if (ret < 0 && !err)
1222 			err = ret;
1223 		ret = i2c_smbus_write_byte_data(data->client,
1224 						SI1145_REG_INT_CFG, 0);
1225 		if (ret < 0 && !err)
1226 			err = ret;
1227 		data->autonomous = false;
1228 	}
1229 
1230 	mutex_unlock(&data->lock);
1231 	return err;
1232 }
1233 
1234 static const struct iio_trigger_ops si1145_trigger_ops = {
1235 	.set_trigger_state = si1145_trigger_set_state,
1236 };
1237 
1238 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1239 {
1240 	struct si1145_data *data = iio_priv(indio_dev);
1241 	struct i2c_client *client = data->client;
1242 	struct iio_trigger *trig;
1243 	int ret;
1244 
1245 	trig = devm_iio_trigger_alloc(&client->dev,
1246 			"%s-dev%d", indio_dev->name, indio_dev->id);
1247 	if (!trig)
1248 		return -ENOMEM;
1249 
1250 	trig->dev.parent = &client->dev;
1251 	trig->ops = &si1145_trigger_ops;
1252 	iio_trigger_set_drvdata(trig, indio_dev);
1253 
1254 	ret = devm_request_irq(&client->dev, client->irq,
1255 			  iio_trigger_generic_data_rdy_poll,
1256 			  IRQF_TRIGGER_FALLING,
1257 			  "si1145_irq",
1258 			  trig);
1259 	if (ret < 0) {
1260 		dev_err(&client->dev, "irq request failed\n");
1261 		return ret;
1262 	}
1263 
1264 	ret = devm_iio_trigger_register(&client->dev, trig);
1265 	if (ret)
1266 		return ret;
1267 
1268 	data->trig = trig;
1269 	indio_dev->trig = iio_trigger_get(data->trig);
1270 
1271 	return 0;
1272 }
1273 
1274 static int si1145_probe(struct i2c_client *client,
1275 			const struct i2c_device_id *id)
1276 {
1277 	struct si1145_data *data;
1278 	struct iio_dev *indio_dev;
1279 	u8 part_id, rev_id, seq_id;
1280 	int ret;
1281 
1282 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1283 	if (!indio_dev)
1284 		return -ENOMEM;
1285 
1286 	data = iio_priv(indio_dev);
1287 	i2c_set_clientdata(client, indio_dev);
1288 	data->client = client;
1289 	data->part_info = &si1145_part_info[id->driver_data];
1290 
1291 	part_id = ret = i2c_smbus_read_byte_data(data->client,
1292 						 SI1145_REG_PART_ID);
1293 	if (ret < 0)
1294 		return ret;
1295 	rev_id = ret = i2c_smbus_read_byte_data(data->client,
1296 						SI1145_REG_REV_ID);
1297 	if (ret < 0)
1298 		return ret;
1299 	seq_id = ret = i2c_smbus_read_byte_data(data->client,
1300 						SI1145_REG_SEQ_ID);
1301 	if (ret < 0)
1302 		return ret;
1303 	dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1304 			part_id, rev_id, seq_id);
1305 	if (part_id != data->part_info->part) {
1306 		dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1307 				part_id, data->part_info->part);
1308 		return -ENODEV;
1309 	}
1310 
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;
1316 
1317 	mutex_init(&data->lock);
1318 	mutex_init(&data->cmdlock);
1319 
1320 	ret = si1145_initialize(data);
1321 	if (ret < 0)
1322 		return ret;
1323 
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;
1329 
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");
1336 	}
1337 
1338 	return devm_iio_device_register(&client->dev, indio_dev);
1339 }
1340 
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 },
1349 	{ }
1350 };
1351 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1352 
1353 static struct i2c_driver si1145_driver = {
1354 	.driver = {
1355 		.name   = "si1145",
1356 	},
1357 	.probe  = si1145_probe,
1358 	.id_table = si1145_ids,
1359 };
1360 
1361 module_i2c_driver(si1145_driver);
1362 
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");
1366