xref: /openbmc/linux/drivers/iio/light/si1145.c (revision 29c37341)
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  */
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;
182 };
183 
184 /*
185  * __si1145_command_reset() - Send CMD_NOP and wait for response 0
186  *
187  * Does not modify data->rsp_seq
188  *
189  * Return: 0 on success and -errno on error.
190  */
191 static int __si1145_command_reset(struct si1145_data *data)
192 {
193 	struct device *dev = &data->client->dev;
194 	unsigned long stop_jiffies;
195 	int ret;
196 
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);
202 
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;
212 		}
213 		msleep(SI1145_COMMAND_MINSLEEP_MS);
214 		continue;
215 	}
216 }
217 
218 /*
219  * si1145_command() - Execute a command and poll the response register
220  *
221  * All conversion overflows are reported as -EOVERFLOW
222  * INVALID_SETTING is reported as -EINVAL
223  * Timeouts are reported as -ETIMEDOUT
224  *
225  * Return: 0 on success or -errno on failure
226  */
227 static int si1145_command(struct si1145_data *data, u8 cmd)
228 {
229 	struct device *dev = &data->client->dev;
230 	unsigned long stop_jiffies;
231 	int ret;
232 
233 	mutex_lock(&data->cmdlock);
234 
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;
241 		}
242 		data->rsp_seq = 0;
243 	}
244 
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;
249 	}
250 	/* Sleep a little to ensure the command is received */
251 	msleep(SI1145_COMMAND_MINSLEEP_MS);
252 
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;
260 		}
261 
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;
269 				}
270 				msleep(SI1145_COMMAND_MINSLEEP_MS);
271 				continue;
272 			}
273 			if (ret == ((data->rsp_seq + 1) &
274 				SI1145_RSP_COUNTER_MASK)) {
275 				data->rsp_seq = ret;
276 				ret = 0;
277 				break;
278 			}
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;
293 			}
294 		}
295 
296 		/* Force a counter reset next time */
297 		data->rsp_seq = -1;
298 		break;
299 	}
300 
301 out:
302 	mutex_unlock(&data->cmdlock);
303 
304 	return ret;
305 }
306 
307 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
308 			       u8 value)
309 {
310 	int ret;
311 
312 	ret = i2c_smbus_write_byte_data(data->client,
313 		SI1145_REG_PARAM_WR, value);
314 	if (ret < 0)
315 		return ret;
316 
317 	return si1145_command(data, op | (param & 0x1F));
318 }
319 
320 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
321 {
322 	return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
323 }
324 
325 /* Set param. Returns negative errno or current value */
326 static int si1145_param_query(struct si1145_data *data, u8 param)
327 {
328 	int ret;
329 
330 	ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
331 	if (ret < 0)
332 		return ret;
333 
334 	return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
335 }
336 
337 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
338 static u16 si1145_uncompress(u8 x)
339 {
340 	u16 result = 0;
341 	u8 exponent = 0;
342 
343 	if (x < 8)
344 		return 0;
345 
346 	exponent = (x & 0xf0) >> 4;
347 	result = 0x10 | (x & 0x0f);
348 
349 	if (exponent >= 4)
350 		return result << (exponent - 4);
351 	return result >> (4 - exponent);
352 }
353 
354 /* Compress 16 bit value to 8 bit, see Silabs AN498 */
355 static u8 si1145_compress(u16 x)
356 {
357 	u32 exponent = 0;
358 	u32 significand = 0;
359 	u32 tmp = x;
360 
361 	if (x == 0x0000)
362 		return 0x00;
363 	if (x == 0x0001)
364 		return 0x08;
365 
366 	while (1) {
367 		tmp >>= 1;
368 		exponent += 1;
369 		if (tmp == 1)
370 			break;
371 	}
372 
373 	if (exponent < 5) {
374 		significand = x << (4 - exponent);
375 		return (exponent << 4) | (significand & 0xF);
376 	}
377 
378 	significand = x >> (exponent - 5);
379 	if (significand & 1) {
380 		significand += 2;
381 		if (significand & 0x0040) {
382 			exponent += 1;
383 			significand >>= 1;
384 		}
385 	}
386 
387 	return (exponent << 4) | ((significand >> 1) & 0xF);
388 }
389 
390 /* Write meas_rate in hardware */
391 static int si1145_set_meas_rate(struct si1145_data *data, int interval)
392 {
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);
399 }
400 
401 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
402 {
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;
409 }
410 
411 /* Set the samp freq in driver private data */
412 static int si1145_store_samp_freq(struct si1145_data *data, int val)
413 {
414 	int ret = 0;
415 	int meas_rate;
416 
417 	if (val <= 0 || val > 32000)
418 		return -ERANGE;
419 	meas_rate = 32000 / val;
420 
421 	mutex_lock(&data->lock);
422 	if (data->autonomous) {
423 		ret = si1145_set_meas_rate(data, meas_rate);
424 		if (ret)
425 			goto out;
426 	}
427 	if (data->part_info->uncompressed_meas_rate)
428 		data->meas_rate = meas_rate;
429 	else
430 		data->meas_rate = si1145_compress(meas_rate);
431 
432 out:
433 	mutex_unlock(&data->lock);
434 
435 	return ret;
436 }
437 
438 static irqreturn_t si1145_trigger_handler(int irq, void *private)
439 {
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);
443 	/*
444 	 * Maximum buffer size:
445 	 *   6*2 bytes channels data + 4 bytes alignment +
446 	 *   8 bytes timestamp
447 	 */
448 	u8 buffer[24];
449 	int i, j = 0;
450 	int ret;
451 	u8 irq_status = 0;
452 
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;
464 	}
465 
466 	for_each_set_bit(i, indio_dev->active_scan_mask,
467 		indio_dev->masklength) {
468 		int run = 1;
469 
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++;
477 		}
478 
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;
486 	}
487 
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;
494 	}
495 
496 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
497 		iio_get_time_ns(indio_dev));
498 
499 done:
500 	iio_trigger_notify_done(indio_dev->trig);
501 	return IRQ_HANDLED;
502 }
503 
504 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
505 {
506 	struct si1145_data *data = iio_priv(indio_dev);
507 	u8 reg = 0, mux;
508 	int ret;
509 	int i;
510 
511 	/* channel list already set, no need to reprogram */
512 	if (data->scan_mask == scan_mask)
513 		return 0;
514 
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;
547 
548 				break;
549 			}
550 		}
551 	}
552 
553 	data->scan_mask = scan_mask;
554 	ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
555 
556 	return ret < 0 ? ret : 0;
557 }
558 
559 static int si1145_measure(struct iio_dev *indio_dev,
560 			  struct iio_chan_spec const *chan)
561 {
562 	struct si1145_data *data = iio_priv(indio_dev);
563 	u8 cmd;
564 	int ret;
565 
566 	ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
567 	if (ret < 0)
568 		return ret;
569 
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;
575 
576 	return i2c_smbus_read_word_data(data->client, chan->address);
577 }
578 
579 /*
580  * Conversion between iio scale and ADC_GAIN values
581  * These could be further adjusted but proximity/intensity are dimensionless
582  */
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");
593 
594 static int si1145_scale_from_adcgain(int regval)
595 {
596 	return 128 >> regval;
597 }
598 
599 static int si1145_proximity_adcgain_from_scale(int val, int val2)
600 {
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;
605 
606 	return val;
607 }
608 
609 static int si1145_intensity_adcgain_from_scale(int val, int val2)
610 {
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;
615 
616 	return val;
617 }
618 
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)
622 {
623 	struct si1145_data *data = iio_priv(indio_dev);
624 	int ret;
625 	u8 reg;
626 
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);
640 
641 			if (ret < 0)
642 				return ret;
643 
644 			*val = ret;
645 
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;
652 
653 			*val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
654 				& 0x0f;
655 
656 			return IIO_VAL_INT;
657 		default:
658 			return -EINVAL;
659 		}
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;
681 		}
682 
683 		ret = si1145_param_query(data, reg);
684 		if (ret < 0)
685 			return ret;
686 
687 		*val = si1145_scale_from_adcgain(ret & 0x07);
688 
689 		return IIO_VAL_INT;
690 	case IIO_CHAN_INFO_OFFSET:
691 		switch (chan->type) {
692 		case IIO_TEMP:
693 			/*
694 			 * -ADC offset - ADC counts @ 25°C -
695 			 *   35 * ADC counts / °C
696 			 */
697 			*val = -256 - 11136 + 25 * 35;
698 			return IIO_VAL_INT;
699 		default:
700 			/*
701 			 * All ADC measurements have are by default offset
702 			 * by -256
703 			 * See AN498 5.6.3
704 			 */
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;
710 		}
711 	case IIO_CHAN_INFO_SAMP_FREQ:
712 		return si1145_read_samp_freq(data, val, val2);
713 	default:
714 		return -EINVAL;
715 	}
716 }
717 
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)
721 {
722 	struct si1145_data *data = iio_priv(indio_dev);
723 	u8 reg1, reg2, shift;
724 	int ret;
725 
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;
746 			}
747 			break;
748 		default:
749 			return -EINVAL;
750 		}
751 
752 		ret = iio_device_claim_direct_mode(indio_dev);
753 		if (ret)
754 			return ret;
755 
756 		ret = si1145_param_set(data, reg1, val);
757 		if (ret < 0) {
758 			iio_device_release_direct_mode(indio_dev);
759 			return ret;
760 		}
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;
768 
769 		if (val < 0 || val > 15 || val2 != 0)
770 			return -EINVAL;
771 
772 		reg1 = SI1145_PS_LED_REG(chan->channel);
773 		shift = SI1145_PS_LED_SHIFT(chan->channel);
774 
775 		ret = iio_device_claim_direct_mode(indio_dev);
776 		if (ret)
777 			return ret;
778 
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;
783 		}
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;
793 	}
794 }
795 
796 #define SI1145_ST { \
797 	.sign = 'u', \
798 	.realbits = 16, \
799 	.storagebits = 16, \
800 	.endianness = IIO_LE, \
801 }
802 
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, \
812 }
813 
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, \
825 }
826 
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, \
836 }
837 
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, \
846 }
847 
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, \
859 }
860 
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, \
868 }
869 
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), \
877 }
878 
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),
886 };
887 
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),
896 };
897 
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),
908 };
909 
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),
922 };
923 
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),
933 };
934 
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),
946 };
947 
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),
961 };
962 
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,
967 };
968 
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,
974 };
975 
976 static const struct attribute_group si1132_attribute_group = {
977 	.attrs = si1132_attributes,
978 };
979 
980 static const struct attribute_group si114x_attribute_group = {
981 	.attrs = si114x_attributes,
982 };
983 
984 
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,
989 };
990 
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,
995 };
996 
997 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
998 	{id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
999 
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),
1008 };
1009 
1010 static int si1145_initialize(struct si1145_data *data)
1011 {
1012 	struct i2c_client *client = data->client;
1013 	int ret;
1014 
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);
1020 
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);
1026 
1027 	/* Turn off autonomous mode */
1028 	ret = si1145_set_meas_rate(data, 0);
1029 	if (ret < 0)
1030 		return ret;
1031 
1032 	/* Initialize sampling freq to 10 Hz */
1033 	ret = si1145_store_samp_freq(data, 10);
1034 	if (ret < 0)
1035 		return ret;
1036 
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;
1060 	}
1061 	if (ret < 0)
1062 		return ret;
1063 
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;
1069 
1070 	ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1071 	if (ret < 0)
1072 		return ret;
1073 
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;
1078 
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;
1084 
1085 	ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1086 	if (ret < 0)
1087 		return ret;
1088 
1089 	ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1090 			       0x04 << 4);
1091 	if (ret < 0)
1092 		return ret;
1093 
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;
1099 
1100 	ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1101 	if (ret < 0)
1102 		return ret;
1103 
1104 	ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1105 			       0x06 << 4);
1106 	if (ret < 0)
1107 		return ret;
1108 
1109 	/*
1110 	 * Initialize UCOEF to default values in datasheet
1111 	 * These registers are normally zero on reset
1112 	 */
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;
1134 	}
1135 
1136 	return 0;
1137 }
1138 
1139 /*
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...
1143  */
1144 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1145 {
1146 	struct si1145_data *data = iio_priv(indio_dev);
1147 	int ret;
1148 
1149 	mutex_lock(&data->lock);
1150 	ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1151 	mutex_unlock(&data->lock);
1152 
1153 	return ret;
1154 }
1155 
1156 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1157 			       const unsigned long *scan_mask)
1158 {
1159 	struct si1145_data *data = iio_priv(indio_dev);
1160 	unsigned int count = 0;
1161 	int i;
1162 
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++;
1167 	}
1168 
1169 	return count <= 1;
1170 }
1171 
1172 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1173 	.preenable = si1145_buffer_preenable,
1174 	.validate_scan_mask = si1145_validate_scan_mask,
1175 };
1176 
1177 /*
1178  * si1145_trigger_set_state() - Set trigger state
1179  *
1180  * When not using triggers interrupts are disabled and measurement rate is
1181  * set to zero in order to minimize power consumption.
1182  */
1183 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1184 {
1185 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1186 	struct si1145_data *data = iio_priv(indio_dev);
1187 	int err = 0, ret;
1188 
1189 	mutex_lock(&data->lock);
1190 
1191 	if (state) {
1192 		data->autonomous = true;
1193 		err = i2c_smbus_write_byte_data(data->client,
1194 				SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1195 		if (err < 0)
1196 			goto disable;
1197 		err = i2c_smbus_write_byte_data(data->client,
1198 				SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1199 		if (err < 0)
1200 			goto disable;
1201 		err = si1145_set_meas_rate(data, data->meas_rate);
1202 		if (err < 0)
1203 			goto disable;
1204 		err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1205 		if (err < 0)
1206 			goto disable;
1207 	} else {
1208 disable:
1209 		/* Disable as much as possible skipping errors */
1210 		ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1211 		if (ret < 0 && !err)
1212 			err = ret;
1213 		ret = si1145_set_meas_rate(data, 0);
1214 		if (ret < 0 && !err)
1215 			err = ret;
1216 		ret = i2c_smbus_write_byte_data(data->client,
1217 						SI1145_REG_IRQ_ENABLE, 0);
1218 		if (ret < 0 && !err)
1219 			err = ret;
1220 		ret = i2c_smbus_write_byte_data(data->client,
1221 						SI1145_REG_INT_CFG, 0);
1222 		if (ret < 0 && !err)
1223 			err = ret;
1224 		data->autonomous = false;
1225 	}
1226 
1227 	mutex_unlock(&data->lock);
1228 	return err;
1229 }
1230 
1231 static const struct iio_trigger_ops si1145_trigger_ops = {
1232 	.set_trigger_state = si1145_trigger_set_state,
1233 };
1234 
1235 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1236 {
1237 	struct si1145_data *data = iio_priv(indio_dev);
1238 	struct i2c_client *client = data->client;
1239 	struct iio_trigger *trig;
1240 	int ret;
1241 
1242 	trig = devm_iio_trigger_alloc(&client->dev,
1243 			"%s-dev%d", indio_dev->name, indio_dev->id);
1244 	if (!trig)
1245 		return -ENOMEM;
1246 
1247 	trig->dev.parent = &client->dev;
1248 	trig->ops = &si1145_trigger_ops;
1249 	iio_trigger_set_drvdata(trig, indio_dev);
1250 
1251 	ret = devm_request_irq(&client->dev, client->irq,
1252 			  iio_trigger_generic_data_rdy_poll,
1253 			  IRQF_TRIGGER_FALLING,
1254 			  "si1145_irq",
1255 			  trig);
1256 	if (ret < 0) {
1257 		dev_err(&client->dev, "irq request failed\n");
1258 		return ret;
1259 	}
1260 
1261 	ret = devm_iio_trigger_register(&client->dev, trig);
1262 	if (ret)
1263 		return ret;
1264 
1265 	data->trig = trig;
1266 	indio_dev->trig = iio_trigger_get(data->trig);
1267 
1268 	return 0;
1269 }
1270 
1271 static int si1145_probe(struct i2c_client *client,
1272 			const struct i2c_device_id *id)
1273 {
1274 	struct si1145_data *data;
1275 	struct iio_dev *indio_dev;
1276 	u8 part_id, rev_id, seq_id;
1277 	int ret;
1278 
1279 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1280 	if (!indio_dev)
1281 		return -ENOMEM;
1282 
1283 	data = iio_priv(indio_dev);
1284 	i2c_set_clientdata(client, indio_dev);
1285 	data->client = client;
1286 	data->part_info = &si1145_part_info[id->driver_data];
1287 
1288 	part_id = ret = i2c_smbus_read_byte_data(data->client,
1289 						 SI1145_REG_PART_ID);
1290 	if (ret < 0)
1291 		return ret;
1292 	rev_id = ret = i2c_smbus_read_byte_data(data->client,
1293 						SI1145_REG_REV_ID);
1294 	if (ret < 0)
1295 		return ret;
1296 	seq_id = ret = i2c_smbus_read_byte_data(data->client,
1297 						SI1145_REG_SEQ_ID);
1298 	if (ret < 0)
1299 		return ret;
1300 	dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1301 			part_id, rev_id, seq_id);
1302 	if (part_id != data->part_info->part) {
1303 		dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1304 				part_id, data->part_info->part);
1305 		return -ENODEV;
1306 	}
1307 
1308 	indio_dev->name = id->name;
1309 	indio_dev->channels = data->part_info->channels;
1310 	indio_dev->num_channels = data->part_info->num_channels;
1311 	indio_dev->info = data->part_info->iio_info;
1312 	indio_dev->modes = INDIO_DIRECT_MODE;
1313 
1314 	mutex_init(&data->lock);
1315 	mutex_init(&data->cmdlock);
1316 
1317 	ret = si1145_initialize(data);
1318 	if (ret < 0)
1319 		return ret;
1320 
1321 	ret = devm_iio_triggered_buffer_setup(&client->dev,
1322 		indio_dev, NULL,
1323 		si1145_trigger_handler, &si1145_buffer_setup_ops);
1324 	if (ret < 0)
1325 		return ret;
1326 
1327 	if (client->irq) {
1328 		ret = si1145_probe_trigger(indio_dev);
1329 		if (ret < 0)
1330 			return ret;
1331 	} else {
1332 		dev_info(&client->dev, "no irq, using polling\n");
1333 	}
1334 
1335 	return devm_iio_device_register(&client->dev, indio_dev);
1336 }
1337 
1338 static const struct i2c_device_id si1145_ids[] = {
1339 	{ "si1132", SI1132 },
1340 	{ "si1141", SI1141 },
1341 	{ "si1142", SI1142 },
1342 	{ "si1143", SI1143 },
1343 	{ "si1145", SI1145 },
1344 	{ "si1146", SI1146 },
1345 	{ "si1147", SI1147 },
1346 	{ }
1347 };
1348 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1349 
1350 static struct i2c_driver si1145_driver = {
1351 	.driver = {
1352 		.name   = "si1145",
1353 	},
1354 	.probe  = si1145_probe,
1355 	.id_table = si1145_ids,
1356 };
1357 
1358 module_i2c_driver(si1145_driver);
1359 
1360 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1361 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1362 MODULE_LICENSE("GPL");
1363