GNU Linux-libre 4.14.290-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         .driver_module = THIS_MODULE,
996         .attrs = &si1132_attribute_group,
997 };
998
999 static const struct iio_info si114x_info = {
1000         .read_raw = si1145_read_raw,
1001         .write_raw = si1145_write_raw,
1002         .driver_module = THIS_MODULE,
1003         .attrs = &si114x_attribute_group,
1004 };
1005
1006 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1007         {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1008
1009 static const struct si1145_part_info si1145_part_info[] = {
1010         [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1011         [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1012         [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1013         [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1014         [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1015         [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1016         [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1017 };
1018
1019 static int si1145_initialize(struct si1145_data *data)
1020 {
1021         struct i2c_client *client = data->client;
1022         int ret;
1023
1024         ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1025                                         SI1145_CMD_RESET);
1026         if (ret < 0)
1027                 return ret;
1028         msleep(SI1145_COMMAND_TIMEOUT_MS);
1029
1030         /* Hardware key, magic value */
1031         ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1032         if (ret < 0)
1033                 return ret;
1034         msleep(SI1145_COMMAND_TIMEOUT_MS);
1035
1036         /* Turn off autonomous mode */
1037         ret = si1145_set_meas_rate(data, 0);
1038         if (ret < 0)
1039                 return ret;
1040
1041         /* Initialize sampling freq to 10 Hz */
1042         ret = si1145_store_samp_freq(data, 10);
1043         if (ret < 0)
1044                 return ret;
1045
1046         /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1047         switch (data->part_info->num_leds) {
1048         case 3:
1049                 ret = i2c_smbus_write_byte_data(client,
1050                                                 SI1145_REG_PS_LED3,
1051                                                 SI1145_LED_CURRENT_45mA);
1052                 if (ret < 0)
1053                         return ret;
1054                 /* fallthrough */
1055         case 2:
1056                 ret = i2c_smbus_write_byte_data(client,
1057                                                 SI1145_REG_PS_LED21,
1058                                                 (SI1145_LED_CURRENT_45mA << 4) |
1059                                                 SI1145_LED_CURRENT_45mA);
1060                 break;
1061         case 1:
1062                 ret = i2c_smbus_write_byte_data(client,
1063                                                 SI1145_REG_PS_LED21,
1064                                                 SI1145_LED_CURRENT_45mA);
1065                 break;
1066         default:
1067                 ret = 0;
1068                 break;
1069         }
1070         if (ret < 0)
1071                 return ret;
1072
1073         /* Set normal proximity measurement mode */
1074         ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1075                                SI1145_PS_ADC_MODE_NORMAL);
1076         if (ret < 0)
1077                 return ret;
1078
1079         ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1080         if (ret < 0)
1081                 return ret;
1082
1083         /* ADC_COUNTER should be one complement of ADC_GAIN */
1084         ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1085         if (ret < 0)
1086                 return ret;
1087
1088         /* Set ALS visible measurement mode */
1089         ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1090                                SI1145_ADC_MISC_RANGE);
1091         if (ret < 0)
1092                 return ret;
1093
1094         ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1095         if (ret < 0)
1096                 return ret;
1097
1098         ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1099                                0x04 << 4);
1100         if (ret < 0)
1101                 return ret;
1102
1103         /* Set ALS IR measurement mode */
1104         ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1105                                SI1145_ADC_MISC_RANGE);
1106         if (ret < 0)
1107                 return ret;
1108
1109         ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1110         if (ret < 0)
1111                 return ret;
1112
1113         ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1114                                0x06 << 4);
1115         if (ret < 0)
1116                 return ret;
1117
1118         /*
1119          * Initialize UCOEF to default values in datasheet
1120          * These registers are normally zero on reset
1121          */
1122         if (data->part_info == &si1145_part_info[SI1132] ||
1123                 data->part_info == &si1145_part_info[SI1145] ||
1124                 data->part_info == &si1145_part_info[SI1146] ||
1125                 data->part_info == &si1145_part_info[SI1147]) {
1126                 ret = i2c_smbus_write_byte_data(data->client,
1127                                                 SI1145_REG_UCOEF1,
1128                                                 SI1145_UCOEF1_DEFAULT);
1129                 if (ret < 0)
1130                         return ret;
1131                 ret = i2c_smbus_write_byte_data(data->client,
1132                                 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1133                 if (ret < 0)
1134                         return ret;
1135                 ret = i2c_smbus_write_byte_data(data->client,
1136                                 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1137                 if (ret < 0)
1138                         return ret;
1139                 ret = i2c_smbus_write_byte_data(data->client,
1140                                 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1141                 if (ret < 0)
1142                         return ret;
1143         }
1144
1145         return 0;
1146 }
1147
1148 /*
1149  * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1150  * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1151  * mode reprograms the channels list anyway...
1152  */
1153 static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1154 {
1155         struct si1145_data *data = iio_priv(indio_dev);
1156         int ret;
1157
1158         mutex_lock(&data->lock);
1159         ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1160         mutex_unlock(&data->lock);
1161
1162         return ret;
1163 }
1164
1165 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1166                                const unsigned long *scan_mask)
1167 {
1168         struct si1145_data *data = iio_priv(indio_dev);
1169         unsigned int count = 0;
1170         int i;
1171
1172         /* Check that at most one AUX channel is enabled */
1173         for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1174                 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1175                         count++;
1176         }
1177
1178         return count <= 1;
1179 }
1180
1181 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1182         .preenable = si1145_buffer_preenable,
1183         .postenable = iio_triggered_buffer_postenable,
1184         .predisable = iio_triggered_buffer_predisable,
1185         .validate_scan_mask = si1145_validate_scan_mask,
1186 };
1187
1188 /**
1189  * si1145_trigger_set_state() - Set trigger state
1190  *
1191  * When not using triggers interrupts are disabled and measurement rate is
1192  * set to zero in order to minimize power consumption.
1193  */
1194 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1195 {
1196         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1197         struct si1145_data *data = iio_priv(indio_dev);
1198         int err = 0, ret;
1199
1200         mutex_lock(&data->lock);
1201
1202         if (state) {
1203                 data->autonomous = true;
1204                 err = i2c_smbus_write_byte_data(data->client,
1205                                 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1206                 if (err < 0)
1207                         goto disable;
1208                 err = i2c_smbus_write_byte_data(data->client,
1209                                 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1210                 if (err < 0)
1211                         goto disable;
1212                 err = si1145_set_meas_rate(data, data->meas_rate);
1213                 if (err < 0)
1214                         goto disable;
1215                 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1216                 if (err < 0)
1217                         goto disable;
1218         } else {
1219 disable:
1220                 /* Disable as much as possible skipping errors */
1221                 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1222                 if (ret < 0 && !err)
1223                         err = ret;
1224                 ret = si1145_set_meas_rate(data, 0);
1225                 if (ret < 0 && !err)
1226                         err = ret;
1227                 ret = i2c_smbus_write_byte_data(data->client,
1228                                                 SI1145_REG_IRQ_ENABLE, 0);
1229                 if (ret < 0 && !err)
1230                         err = ret;
1231                 ret = i2c_smbus_write_byte_data(data->client,
1232                                                 SI1145_REG_INT_CFG, 0);
1233                 if (ret < 0 && !err)
1234                         err = ret;
1235                 data->autonomous = false;
1236         }
1237
1238         mutex_unlock(&data->lock);
1239         return err;
1240 }
1241
1242 static const struct iio_trigger_ops si1145_trigger_ops = {
1243         .owner = THIS_MODULE,
1244         .set_trigger_state = si1145_trigger_set_state,
1245 };
1246
1247 static int si1145_probe_trigger(struct iio_dev *indio_dev)
1248 {
1249         struct si1145_data *data = iio_priv(indio_dev);
1250         struct i2c_client *client = data->client;
1251         struct iio_trigger *trig;
1252         int ret;
1253
1254         trig = devm_iio_trigger_alloc(&client->dev,
1255                         "%s-dev%d", indio_dev->name, indio_dev->id);
1256         if (!trig)
1257                 return -ENOMEM;
1258
1259         trig->dev.parent = &client->dev;
1260         trig->ops = &si1145_trigger_ops;
1261         iio_trigger_set_drvdata(trig, indio_dev);
1262
1263         ret = devm_request_irq(&client->dev, client->irq,
1264                           iio_trigger_generic_data_rdy_poll,
1265                           IRQF_TRIGGER_FALLING,
1266                           "si1145_irq",
1267                           trig);
1268         if (ret < 0) {
1269                 dev_err(&client->dev, "irq request failed\n");
1270                 return ret;
1271         }
1272
1273         ret = iio_trigger_register(trig);
1274         if (ret)
1275                 return ret;
1276
1277         data->trig = trig;
1278         indio_dev->trig = iio_trigger_get(data->trig);
1279
1280         return 0;
1281 }
1282
1283 static void si1145_remove_trigger(struct iio_dev *indio_dev)
1284 {
1285         struct si1145_data *data = iio_priv(indio_dev);
1286
1287         if (data->trig) {
1288                 iio_trigger_unregister(data->trig);
1289                 data->trig = NULL;
1290         }
1291 }
1292
1293 static int si1145_probe(struct i2c_client *client,
1294                         const struct i2c_device_id *id)
1295 {
1296         struct si1145_data *data;
1297         struct iio_dev *indio_dev;
1298         u8 part_id, rev_id, seq_id;
1299         int ret;
1300
1301         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1302         if (!indio_dev)
1303                 return -ENOMEM;
1304
1305         data = iio_priv(indio_dev);
1306         i2c_set_clientdata(client, indio_dev);
1307         data->client = client;
1308         data->part_info = &si1145_part_info[id->driver_data];
1309
1310         part_id = ret = i2c_smbus_read_byte_data(data->client,
1311                                                  SI1145_REG_PART_ID);
1312         if (ret < 0)
1313                 return ret;
1314         rev_id = ret = i2c_smbus_read_byte_data(data->client,
1315                                                 SI1145_REG_REV_ID);
1316         if (ret < 0)
1317                 return ret;
1318         seq_id = ret = i2c_smbus_read_byte_data(data->client,
1319                                                 SI1145_REG_SEQ_ID);
1320         if (ret < 0)
1321                 return ret;
1322         dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1323                         part_id, rev_id, seq_id);
1324         if (part_id != data->part_info->part) {
1325                 dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1326                                 part_id, data->part_info->part);
1327                 return -ENODEV;
1328         }
1329
1330         indio_dev->dev.parent = &client->dev;
1331         indio_dev->name = id->name;
1332         indio_dev->channels = data->part_info->channels;
1333         indio_dev->num_channels = data->part_info->num_channels;
1334         indio_dev->info = data->part_info->iio_info;
1335         indio_dev->modes = INDIO_DIRECT_MODE;
1336
1337         mutex_init(&data->lock);
1338         mutex_init(&data->cmdlock);
1339
1340         ret = si1145_initialize(data);
1341         if (ret < 0)
1342                 return ret;
1343
1344         ret = iio_triggered_buffer_setup(indio_dev, NULL,
1345                 si1145_trigger_handler, &si1145_buffer_setup_ops);
1346         if (ret < 0)
1347                 return ret;
1348
1349         if (client->irq) {
1350                 ret = si1145_probe_trigger(indio_dev);
1351                 if (ret < 0)
1352                         goto error_free_buffer;
1353         } else {
1354                 dev_info(&client->dev, "no irq, using polling\n");
1355         }
1356
1357         ret = iio_device_register(indio_dev);
1358         if (ret < 0)
1359                 goto error_free_trigger;
1360
1361         return 0;
1362
1363 error_free_trigger:
1364         si1145_remove_trigger(indio_dev);
1365 error_free_buffer:
1366         iio_triggered_buffer_cleanup(indio_dev);
1367
1368         return ret;
1369 }
1370
1371 static const struct i2c_device_id si1145_ids[] = {
1372         { "si1132", SI1132 },
1373         { "si1141", SI1141 },
1374         { "si1142", SI1142 },
1375         { "si1143", SI1143 },
1376         { "si1145", SI1145 },
1377         { "si1146", SI1146 },
1378         { "si1147", SI1147 },
1379         { }
1380 };
1381 MODULE_DEVICE_TABLE(i2c, si1145_ids);
1382
1383 static int si1145_remove(struct i2c_client *client)
1384 {
1385         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1386
1387         iio_device_unregister(indio_dev);
1388         si1145_remove_trigger(indio_dev);
1389         iio_triggered_buffer_cleanup(indio_dev);
1390
1391         return 0;
1392 }
1393
1394 static struct i2c_driver si1145_driver = {
1395         .driver = {
1396                 .name   = "si1145",
1397         },
1398         .probe  = si1145_probe,
1399         .remove = si1145_remove,
1400         .id_table = si1145_ids,
1401 };
1402
1403 module_i2c_driver(si1145_driver);
1404
1405 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1406 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1407 MODULE_LICENSE("GPL");