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