iio: sensors: NVS 0.7 drivers
[linux-3.10.git] / drivers / iio / light / nvs_max4400x.c
1 /* Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
2  *
3  * This software is licensed under the terms of the GNU General Public
4  * License version 2, as published by the Free Software Foundation, and
5  * may be copied, distributed, and modified under those terms.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 /* The NVS = NVidia Sensor framework */
14 /* The NVS implementation of scan_elements enable/disable works as follows
15  * (See NVS HAL for further explaination):
16  * To enable, the NVS HAL will:
17  * 1. Disable buffer
18  * 2. Enable channels
19  * 3. Calculate buffer alignments based on enabled channels
20  * 4. Enable buffer
21  * It is expected that the NVS kernel driver will detect the channels enabled
22  * and enable the device using the IIO iio_buffer_setup_ops.
23  * To disable, the NVS HAL will:
24  * 1. Disable buffer
25  * 2. Disable channels
26  * 3. Calculate buffer alignments based on enabled channels
27  * 4. If (one or more channels are enabled)
28  *        4a. Enable buffer
29  *    else
30  *        4b. Disable master enable
31  * It is expected that the master enable will be enabled as part of the
32  * iio_buffer_setup_ops.
33  * The NVS sysfs attribute for the master enable is "enable" without any
34  * channel name.
35  */
36 /* The NVS HAL will use the IIO scale and offset sysfs attributes to modify the
37  * data using the following formula: (data * scale) + offset
38  * A scale value of 0 disables scale.
39  * A scale value of 1 puts the NVS HAL into calibration mode where the scale
40  * and offset are read everytime the data is read to allow realtime calibration
41  * of the scale and offset values to be used in the device tree parameters.
42  * Keep in mind the data is buffered but the NVS HAL will display the data and
43  * scale/offset parameters in the log.  See calibration steps below.
44  */
45 /* NVS light/proximity drivers have two calibration mechanisms:
46  * Method 1 (preferred):
47  * This method uses interpolation and requires a low and high uncalibrated
48  * value along with the corresponding low and high calibrated values.  The
49  * uncalibrated values are what is read from the sensor in the steps below.
50  * The corresponding calibrated values are what the correct value should be.
51  * All values are programmed into the device tree settings.
52  * 1. Read scale sysfs attribute.  This value will need to be written back.
53  * 2. Disable device.
54  * 3. Write 1 to the scale sysfs attribute.
55  * 4. Enable device.
56  * 5. The NVS HAL will announce in the log that calibration mode is enabled and
57  *    display the data along with the scale and offset parameters applied.
58  * 6. Write the scale value read in step 1 back to the scale sysfs attribute.
59  * 7. Put the device into a state where the data read is a low value.
60  * 8. Note the values displayed in the log.  Separately measure the actual
61  *    value.  The value from the sensor will be the uncalibrated value and the
62  *    separately measured value will be the calibrated value for the current
63  *    state (low or high values).
64  * 9. Put the device into a state where the data read is a high value.
65  * 10. Repeat step 8.
66  * 11. Enter the values in the device tree settings for the device.  Both
67  *     calibrated and uncalibrated values will be the values before scale and
68  *     offset are applied.
69  *     For example, a light sensor has the following device tree parameters:
70  *     light_uncalibrated_lo
71  *     light_calibrated_lo
72  *     light_uncalibrated_hi
73  *     light_calibrated_hi
74  *     The proximity sensor parameters are:
75  *     proximity_uncalibrated_lo
76  *     proximity_calibrated_lo
77  *     proximity_uncalibrated_hi
78  *     proximity_calibrated_hi
79  *
80  * Method 2:
81  * 1. Disable device.
82  * 2. Write 1 to the scale sysfs attribute.
83  * 3. Enable device.
84  * 4. The NVS HAL will announce in the log that calibration mode is enabled and
85  *    display the data along with the scale and offset parameters applied.
86  * 5. Write to scale and offset sysfs attributes as needed to get the data
87  *    modified as desired.
88  * 6. Disabling the device disables calibration mode.
89  * 7. Set the new scale and offset parameters in the device tree:
90  *    <IIO channel>_scale_val
91  *    <IIO channel>_scale_val2
92  *    <IIO channel>_offset_val
93  *    <IIO channel>_offset_val2
94  *    The values are in IIO IIO_VAL_INT_PLUS_MICRO format.
95  */
96 /* NVS proximity drivers can be configured for binary output.  If the
97  * proximity_binary_threshold setting in the device tree is set, the driver
98  * will configure the rest of the settings so that a 1 is reported for
99  * "far away" and 0 for "near".  The threshold is typically set for maximum
100  * range allowing the minimal LED drive power to determine the actual range.
101  * To disable binary output, set the proximity_binary_threshold to 0.  The
102  * driver will then require the interpolation calibration for reporting actual
103  * distances.
104  */
105 /* Calibration for proximity_binary_threshold is done with the following steps:
106  * 1. Disable proximity.
107  * 2. Write 1 to the scale sysfs attribute.
108  * 3. Enable device.
109  * 4. The NVS HAL will announce in the log that calibration mode is enabled and
110  *    display the raw HW proximity values instead of the 0 or 1.
111  * 5. Move a surface within the range of the proximity where the output
112  *    state should change.  There should be two places: the transition from
113  *    1 to 0 and vice versa.  The middle of these two points will be the
114  *    proximity_binary_threshold and the difference between each point and
115  *    the proximity_binary_threshold will be the proximity_threshold_lo and
116  *    proximity_threshold_hi settings in the device tree which will allow
117  *    hysteresis around the threshold and can be skewed as such.
118  * 6. Write 0 to the scale sysfs attribute.
119  * 7. Disabling the device will turn off this calibration mode.
120  * Note that proximity_threshold_lo and proximity_threshold_hi do not have to
121  * be identical and can in fact result in being outside the HW range:
122  * Example: HW values range from 0 to 255.
123  *          proximity_binary_threshold is set to 1.
124  *          proximity_threshold_lo is set to 10.
125  *          proximity_threshold_hi is set to 20.
126  *          In this configuration, the low state changes at 0 and the
127  *          high at 20.
128  * The driver will automatically handle out of bounds values.
129  * Also, the proximity_thresh_falling_value and proximity_thresh_rising_value
130  * sysfs attributes can change the proximity_threshold_lo/hi values at runtime
131  * for further calibration flexibility.
132  */
133
134 #include <linux/i2c.h>
135 #include <linux/module.h>
136 #include <linux/slab.h>
137 #include <linux/err.h>
138 #include <linux/delay.h>
139 #include <linux/ktime.h>
140 #include <linux/regulator/consumer.h>
141 #include <linux/workqueue.h>
142 #include <linux/interrupt.h>
143 #include <linux/of.h>
144 #include <linux/iio/iio.h>
145 #include <linux/iio/sysfs.h>
146 #include <linux/iio/buffer.h>
147 #include <linux/iio/kfifo_buf.h>
148 #include <linux/iio/trigger.h>
149
150
151 #define MX_VERSION_DRIVER               (102)
152 #define MX_VENDOR                       "Maxim"
153 #define MX_NAME                         "max4400x"
154 #define MX_NAME_MAX44005                "max44005"
155 #define MX_NAME_MAX44006                "max44006"
156 #define MX_NAME_MAX44008                "max44008"
157 #define MX_DEVID_MAX44005               (0x05)
158 #define MX_DEVID_MAX44006               (0x06)
159 #define MX_DEVID_MAX44008               (0x08)
160 #define MX_HW_DELAY_MS                  (1)
161 #define MX_POLL_DLY_MS_DFLT             (2000)
162 #define MX_POLL_DLY_MS_MIN              (100)
163 /* setting _REPORT_N to 2 causes an extra reading after crossing the threshold
164  * allowing a more accurate settled reported value.
165  */
166 #define MX_REPORT_N                     (2)
167 #define MX_AMB_CFG_DFLT                 (0x43)
168 #define MX_PRX_CFG_DFLT                 (0x12)
169 /* light defines */
170 #define MX_LIGHT_THRESHOLD_DFLT         (50)
171 #define MX_LIGHT_VERSION                (1)
172 #define MX_LIGHT_MAX_RANGE_IVAL         (14323)
173 #define MX_LIGHT_MAX_RANGE_MICRO        (0)
174 #define MX_LIGHT_RESOLUTION_IVAL        (0)
175 #define MX_LIGHT_RESOLUTION_MICRO       (14000)
176 #define MX_LIGHT_SCALE_IVAL             (0)
177 #define MX_LIGHT_SCALE_MICRO            (1000)
178 #define MX_LIGHT_OFFSET_IVAL            (0)
179 #define MX_LIGHT_OFFSET_MICRO           (0)
180 #define MX_LIGHT_MILLIAMP               "0.0135"
181 /* temp defines */
182 #define MX_TEMP_THRESHOLD_DFLT          (2)
183 #define MX_TEMP_VERSION                 (1)
184 #define MX_TEMP_MAX_RANGE_IVAL          (125)
185 #define MX_TEMP_MAX_RANGE_MICRO         (0)
186 #define MX_TEMP_RESOLUTION_IVAL         (0)
187 #define MX_TEMP_RESOLUTION_MICRO        (250000)
188 #define MX_TEMP_SCALE_IVAL              (0)
189 #define MX_TEMP_SCALE_MICRO             (10000)
190 #define MX_TEMP_OFFSET_IVAL             (0)
191 #define MX_TEMP_OFFSET_MICRO            (0)
192 #define MX_TEMP_MILLIAMP                "0.0135"
193 /* proximity defines */
194 #define MX_PROX_THRESHOLD_DFLT          (10)
195 #define MX_PROX_THRESHOLD_BINARY_DFLT   (1) /* > 0 = enable */
196 #define MX_PROX_VERSION                 (1)
197 #define MX_PROX_MAX_RANGE_IVAL          (1)
198 #define MX_PROX_MAX_RANGE_MICRO         (0)
199 #define MX_PROX_RESOLUTION_IVAL         (1)
200 #define MX_PROX_RESOLUTION_MICRO        (0)
201 #define MX_PROX_SCALE_IVAL              (0)
202 #define MX_PROX_SCALE_MICRO             (0)
203 #define MX_PROX_OFFSET_IVAL             (0)
204 #define MX_PROX_OFFSET_MICRO            (0)
205 #define MX_PROX_MILLIAMP                "10.0195"
206 /* HW registers */
207 #define MX_REG_STS                      (0x00)
208 #define MX_REG_STS_POR                  (0x04)
209 #define MX_REG_STS_RESET                (4)
210 #define MX_REG_STS_SHDN                 (3)
211 #define MX_REG_STS_PWRON                (2)
212 #define MX_REG_STS_PRXINTS              (1)
213 #define MX_REG_STS_AMBINTS              (0)
214 #define MX_REG_CFG_MAIN                 (0x01)
215 #define MX_REG_CFG_MAIN_POR             (0x00)
216 #define MX_REG_CFG_MAIN_MODE            (4)
217 #define MX_REG_CFG_MAIN_AMBSEL          (2)
218 #define MX_REG_CFG_MAIN_PRXINTE         (1)
219 #define MX_REG_CFG_MAIN_AMBINTE         (0)
220 #define MX_REG_CFG_MAIN_INTE_MASK       (0x03)
221 #define MX_REG_CFG_AMB                  (0x02)
222 #define MX_REG_CFG_AMB_POR              (0x20)
223 #define MX_REG_CFG_AMB_TRIM             (7)
224 #define MX_REG_CFG_AMB_COMPEN           (6)
225 #define MX_REG_CFG_AMB_TEMPEN           (5)
226 #define MX_REG_CFG_AMB_AMBTIM           (2)
227 #define MX_REG_CFG_AMB_AMBTIM_MASK      (0x1C)
228 #define MX_REG_CFG_AMB_AMBPGA           (0)
229 #define MX_REG_CFG_AMB_AMBPGA_MASK      (0x03)
230 #define MX_REG_CFG_PRX                  (0x03)
231 #define MX_REG_CFG_PRX_DRV              (4)
232 #define MX_REG_CFG_PRX_PRXTIM           (1)
233 #define MX_REG_CFG_PRX_PRXPGA           (0)
234 #define MX_REG_DATA_AMB_CLEAR           (0x04)
235 #define MX_REG_DATA_AMB_CLEAR_H         (0x04)
236 #define MX_REG_DATA_AMB_CLEAR_L         (0x05)
237 #define MX_REG_DATA_AMB_RED_H           (0x06)
238 #define MX_REG_DATA_AMB_RED_L           (0x07)
239 #define MX_REG_DATA_AMB_GREEN_H         (0x08)
240 #define MX_REG_DATA_AMB_GREEN_L         (0x09)
241 #define MX_REG_DATA_AMB_BLUE_H          (0x0A)
242 #define MX_REG_DATA_AMB_BLUE_L          (0x0B)
243 #define MX_REG_DATA_AMB_IR_H            (0x0C)
244 #define MX_REG_DATA_AMB_IR_L            (0x0D)
245 #define MX_REG_DATA_AMB_IRCOMP_H        (0x0E)
246 #define MX_REG_DATA_AMB_IRCOMP_L        (0x0F)
247 #define MX_REG_DATA_PROX_H              (0x10)
248 #define MX_REG_DATA_PROX_L              (0x11)
249 #define MX_REG_DATA_TEMP_H              (0x12)
250 #define MX_REG_DATA_TEMP_L              (0x13)
251 #define MX_REG_AMB_UPTHR_H              (0x14)
252 #define MX_REG_AMB_UPTHR_L              (0x15)
253 #define MX_REG_AMB_LOTHR_H              (0x16)
254 #define MX_REG_AMB_LOTHR_L              (0x17)
255 #define MX_REG_CFG_THR                  (0x18)
256 #define MX_REG_CFG_THR_PRXPST           (2)
257 #define MX_REG_CFG_THR_AMBPST           (0)
258 #define MX_REG_PRX_UPTHR_H              (0x19)
259 #define MX_REG_PRX_UPTHR_L              (0x1A)
260 #define MX_REG_PRX_LOTHR_H              (0x1B)
261 #define MX_REG_PRX_LOTHR_L              (0x1C)
262 #define MX_REG_TRIM_CLEAR               (0x1D)
263 #define MX_REG_TRIM_RED                 (0x1E)
264 #define MX_REG_TRIM_GREEN               (0x1F)
265 #define MX_REG_TRIM_BLUE                (0x20)
266 #define MX_REG_TRIM_IR                  (0x21)
267 /* _buf_push expects this scan order */
268 #define MX_SCAN_LIGHT                   (0)
269 #define MX_SCAN_TEMP                    (1)
270 #define MX_SCAN_PROX                    (2)
271 #define MX_SCAN_TIMESTAMP               (3)
272 #define MX_DEV_LIGHT                    (MX_SCAN_LIGHT)
273 #define MX_DEV_TEMP                     (MX_SCAN_TEMP)
274 #define MX_DEV_PROX                     (MX_SCAN_PROX)
275 #define MX_DEV_N                        (MX_SCAN_TIMESTAMP)
276 /* debug flags */
277 #define MX_DBG_SPEW_MSG                 (1 << 0)
278 #define MX_DBG_SPEW_LIGHT               (1 << 1)
279 #define MX_DBG_SPEW_LIGHT_POLL          (1 << 2)
280 #define MX_DBG_SPEW_TEMP                (1 << 3)
281 #define MX_DBG_SPEW_TEMP_POLL           (1 << 4)
282 #define MX_DBG_SPEW_PROX                (1 << 5)
283 #define MX_DBG_SPEW_PROX_POLL           (1 << 6)
284 #define MX_DBG_IRQ                      (1 << 7)
285 #define MX_DBG_VAL_LIGHT                (1 << 8)
286 #define MX_DBG_VAL_TEMP                 (1 << 9)
287 #define MX_DBG_VAL_PROX                 (1 << 10)
288
289 enum MX_ATTR {
290         MX_ATTR_ENABLE,
291         MX_ATTR_VENDOR,
292         MX_ATTR_LIGHT_PART,
293         MX_ATTR_LIGHT_VERSION,
294         MX_ATTR_LIGHT_MILLIAMP,
295         MX_ATTR_LIGHT_THRESH_LO,
296         MX_ATTR_LIGHT_THRESH_HI,
297         MX_ATTR_TEMP_PART,
298         MX_ATTR_TEMP_VERSION,
299         MX_ATTR_TEMP_MILLIAMP,
300         MX_ATTR_TEMP_THRESH_LO,
301         MX_ATTR_TEMP_THRESH_HI,
302         MX_ATTR_PROX_PART,
303         MX_ATTR_PROX_VERSION,
304         MX_ATTR_PROX_MILLIAMP,
305         MX_ATTR_PROX_THRESH_LO,
306         MX_ATTR_PROX_THRESH_HI,
307 };
308
309 enum MX_INFO {
310         MX_INFO_DATA = 0,
311         MX_INFO_VER,
312         MX_INFO_ERRS,
313         MX_INFO_REGS,
314         MX_INFO_DBG,
315         MX_INFO_LIGHT_SPEW,
316         MX_INFO_LIGHT_POLL_SPEW,
317         MX_INFO_TEMP_SPEW,
318         MX_INFO_TEMP_POLL_SPEW,
319         MX_INFO_PROXIMITY_SPEW,
320         MX_INFO_PROXIMITY_POLL_SPEW,
321         MX_INFO_DBG_IRQ,
322         MX_INFO_LIMIT_MAX,
323 };
324
325 /* regulator names in order of powering on */
326 static char *mx_vregs[] = {
327         "vdd",
328 };
329
330 static unsigned short mx_i2c_addrs[] = {
331         0x40,
332         0x41,
333         0x44,
334         0x45,
335 };
336
337 struct mx_iio_float {
338         int ival;
339         int micro;
340 };
341
342 static u8 mx_mode_tbl[] = {             /* device enable */
343         0x00,                           /* nothing */
344         0x00,                           /* light */
345         0x00,                           /* temp */
346         0x00,                           /* temp + light */
347         0x50,                           /* proximity */
348         0x30,                           /* proximity + light */
349         0x30,                           /* proximity + temp */
350         0x30                            /* proximity + temp + light */
351 };
352
353 /* 1 nW/cm^2 = 0.00683 lux */
354 static unsigned int mx_ambpga_44005[] = {
355         /* values to be scaled by val2 */
356         20490,                          /* 0.02049 lux / LSb */
357         81960,                          /* 0.08196 lux / LSb */
358         327680,                         /* 0.32768 lux / LSb */
359         5245440                         /* 5.24544 lux / LSb */
360 };
361
362 static unsigned int mx_ambpga_44006[] = {
363         /* values to be scaled by val2 */
364         13660,                          /* 0.01366 lux / LSb */
365         54640,                          /* 0.05464 lux / LSb */
366         218560,                         /* 0.21856 lux / LSb */
367         3496960                         /* 3.49696 lux / LSb */
368 };
369
370 static unsigned int mx_ambtim_mask[] = {
371         0x3FFF,                         /* 14 bits */
372         0x0FFF,                         /* 12 bits */
373         0x03FF,                         /* 10 bits */
374         0x00FF,                         /* 8 bits */
375         0x3FFF,                         /* 14 bits */
376         0x3FFF,                         /* N/A */
377         0x3FFF,                         /* N/A */
378         0x3FFF,                         /* N/A */
379 };
380
381 struct mx_state {
382         struct i2c_client *i2c;
383         struct iio_trigger *trig;
384         struct delayed_work dw;
385         struct regulator_bulk_data vreg[ARRAY_SIZE(mx_vregs)];
386         unsigned int info;              /* info data to return */
387         unsigned int dbg;               /* debug flags */
388         unsigned int errs;              /* error count */
389         unsigned int enable;            /* enable status */
390         unsigned int poll_delay_ms;     /* requested sampling delay (ms) */
391         unsigned int delay_us[MX_DEV_N]; /* device sampling delay */
392         unsigned int scale_i;           /* index into HW IT settings table */
393         int scale_val[MX_DEV_N];        /* user scale val */
394         int scale_val2[MX_DEV_N];       /* user scale val2 */
395         int offset_val[MX_DEV_N];       /* user offset val */
396         int offset_val2[MX_DEV_N];      /* user offset val2 */
397         int peak_val[MX_DEV_N];         /* IIO_VAL_INT_PLUS_MICRO max range */
398         int peak_val2[MX_DEV_N];        /* IIO_VAL_INT_PLUS_MICRO max range */
399         int peak_scale_val[MX_DEV_N];   /* IIO_VAL_INT_PLUS_MICRO resolution */
400         int peak_scale_val2[MX_DEV_N];  /* IIO_VAL_INT_PLUS_MICRO resolution */
401         int i_uc_lo[MX_DEV_N];          /* interpolation x1 uncalibrated lo */
402         int i_uc_hi[MX_DEV_N];          /* interpolation x3 uncalibrated hi */
403         int i_c_lo[MX_DEV_N];           /* interpolation y1 calibrated lo */
404         int i_c_hi[MX_DEV_N];           /* interpolation y3 calibrated hi */
405         int thr_lo[MX_DEV_N];           /* report when reported value < this */
406         int thr_hi[MX_DEV_N];           /* report when reported value > this */
407         unsigned int prx_thr_bin;       /* proximity binary mode threshold */
408         unsigned int report;            /* report count */
409         unsigned int report_n;          /* this many on-change data reports */
410         bool iio_ts_en;                 /* use IIO timestamps */
411         bool shutdown;                  /* shutdown active flag */
412         bool suspend;                   /* suspend active flag */
413         const char *part;               /* part name */
414         u16 i2c_addr;                   /* I2C address */
415         u8 dev_id;                      /* device ID */
416         u8 amb_cfg;                     /* ambient configuration register */
417         u8 prx_cfg;                     /* proximity configuration register */
418         u8 thr_cfg;                     /* threshold persist register */
419         u8 rc_main_cfg;                 /* cache of main configuration */
420         u8 rc_amb_cfg;                  /* cache of ambient configuration */
421         u32 light;                      /* sample light data */
422         s32 temp;                       /* sample temp data */
423         u32 proximity;                  /* reported proximity data */
424         u32 prox;                       /* actual proximity data */
425         s64 ts;                         /* sample data timestamp */
426 };
427
428
429 static s64 mx_get_time_ns(struct mx_state *st)
430 {
431         struct timespec ts;
432
433         if (st->iio_ts_en)
434                 return iio_get_time_ns();
435
436         ktime_get_ts(&ts);
437         return timespec_to_ns(&ts);
438 }
439
440 static void mx_err(struct mx_state *st)
441 {
442         st->errs++;
443         if (!st->errs)
444                 st->errs--;
445 }
446
447 static int mx_i2c_read(struct mx_state *st, u8 reg, u16 len, u8 *val)
448 {
449         struct i2c_msg msg[2];
450
451         msg[0].addr = st->i2c_addr;
452         msg[0].flags = 0;
453         msg[0].len = 1;
454         msg[0].buf = &reg;
455         msg[1].addr = st->i2c_addr;
456         msg[1].flags = I2C_M_RD;
457         msg[1].len = len;
458         msg[1].buf = val;
459         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
460                 mx_err(st);
461                 return -EIO;
462         }
463
464         return 0;
465 }
466
467 static int mx_i2c_rd(struct mx_state *st, u8 reg, u8 *val)
468 {
469         return mx_i2c_read(st, reg, 1, val);
470 }
471
472 static int mx_i2c_write(struct mx_state *st, u16 len, u8 *buf)
473 {
474         struct i2c_msg msg;
475
476         if (st->i2c_addr) {
477                 msg.addr = st->i2c_addr;
478                 msg.flags = 0;
479                 msg.len = len;
480                 msg.buf = buf;
481                 if (i2c_transfer(st->i2c->adapter, &msg, 1) != 1) {
482                         mx_err(st);
483                         return -EIO;
484                 }
485         }
486
487         return 0;
488 }
489
490 static int mx_i2c_wr(struct mx_state *st, u8 reg, u8 val)
491 {
492         u8 buf[2];
493
494         buf[0] = reg;
495         buf[1] = val;
496         return mx_i2c_write(st, sizeof(buf), buf);
497 }
498
499 static int mx_vreg_dis(struct mx_state *st, unsigned int i)
500 {
501         int ret = 0;
502
503         if (st->vreg[i].ret && (st->vreg[i].consumer != NULL)) {
504                 ret = regulator_disable(st->vreg[i].consumer);
505                 if (ret)
506                         dev_err(&st->i2c->dev, "%s %s ERR\n",
507                                 __func__, st->vreg[i].supply);
508                 else
509                         st->vreg[i].ret = 0;
510                         dev_dbg(&st->i2c->dev, "%s %s\n",
511                                 __func__, st->vreg[i].supply);
512         }
513         return ret;
514 }
515
516 static int mx_vreg_dis_all(struct mx_state *st)
517 {
518         unsigned int i;
519         int ret = 0;
520
521         for (i = ARRAY_SIZE(mx_vregs); i > 0; i--)
522                 ret |= mx_vreg_dis(st, (i - 1));
523         return ret;
524 }
525
526 static int mx_vreg_en(struct mx_state *st, unsigned int i)
527 {
528         int ret = 0;
529
530         if ((!st->vreg[i].ret) && (st->vreg[i].consumer != NULL)) {
531                 ret = regulator_enable(st->vreg[i].consumer);
532                 if (ret) {
533                         dev_err(&st->i2c->dev, "%s %s ERR\n",
534                                 __func__, st->vreg[i].supply);
535                 } else {
536                         st->vreg[i].ret = 1;
537                         dev_dbg(&st->i2c->dev, "%s %s\n",
538                                 __func__, st->vreg[i].supply);
539                         ret = 1; /* flag regulator state change */
540                 }
541         }
542         return ret;
543 }
544
545 static int mx_vreg_en_all(struct mx_state *st)
546 {
547         unsigned int i;
548         int ret = 0;
549
550         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++)
551                 ret |= mx_vreg_en(st, i);
552         return ret;
553 }
554
555 static void mx_vreg_exit(struct mx_state *st)
556 {
557         unsigned int i;
558
559         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
560                 if (st->vreg[i].consumer != NULL) {
561                         devm_regulator_put(st->vreg[i].consumer);
562                         st->vreg[i].consumer = NULL;
563                         dev_dbg(&st->i2c->dev, "%s %s\n",
564                                 __func__, st->vreg[i].supply);
565                 }
566         }
567 }
568
569 static int mx_vreg_init(struct mx_state *st)
570 {
571         unsigned int i;
572         int ret = 0;
573
574         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
575                 st->vreg[i].supply = mx_vregs[i];
576                 st->vreg[i].ret = 0;
577                 st->vreg[i].consumer = devm_regulator_get(&st->i2c->dev,
578                                                           st->vreg[i].supply);
579                 if (IS_ERR(st->vreg[i].consumer)) {
580                         ret |= PTR_ERR(st->vreg[i].consumer);
581                         dev_err(&st->i2c->dev, "%s ret %d for %s\n",
582                                 __func__, ret, st->vreg[i].supply);
583                         st->vreg[i].consumer = NULL;
584                 } else {
585                         dev_dbg(&st->i2c->dev, "%s %s\n",
586                                 __func__, st->vreg[i].supply);
587                 }
588         }
589         return ret;
590 }
591
592 static int mx_vreg_sts(struct mx_state *st)
593 {
594         unsigned int i;
595         int ret = 0;
596
597         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
598                 if (st->vreg[i].consumer != NULL)
599                         break;
600         }
601         if (i < ARRAY_SIZE(mx_vregs)) {
602                 /* ret == number of regulators on */
603                 for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
604                         if (st->vreg[i].ret)
605                                 ret++;
606                 }
607         } else {
608                 /* no regulator support (can assume always on) */
609                 ret = -EINVAL;
610         }
611         return ret;
612 }
613
614 static int mx_pm(struct mx_state *st, bool enable)
615 {
616         int ret = 0;
617
618         if (enable) {
619                 ret = mx_vreg_en_all(st);
620                 if (ret)
621                         mdelay(MX_HW_DELAY_MS);
622                 mx_i2c_wr(st, MX_REG_STS, (1 << MX_REG_STS_RESET));
623                 mdelay(MX_HW_DELAY_MS);
624                 st->rc_main_cfg = MX_REG_CFG_MAIN_POR;
625                 st->rc_amb_cfg = MX_REG_CFG_AMB_POR;
626                 mx_i2c_wr(st, MX_REG_STS, 0);
627         } else {
628                 ret = mx_vreg_sts(st);
629                 if ((ret < 0) || (ret == ARRAY_SIZE(mx_vregs))) {
630                         ret = mx_i2c_wr(st, MX_REG_STS, 1 << MX_REG_STS_SHDN);
631                 } else if (ret > 0) {
632                         mx_vreg_en_all(st);
633                         mdelay(MX_HW_DELAY_MS);
634                         ret = mx_i2c_wr(st, MX_REG_STS, 1 << MX_REG_STS_SHDN);
635                 }
636                 ret |= mx_vreg_dis_all(st);
637         }
638         if (ret > 0)
639                 ret = 0;
640         if (ret) {
641                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
642                         __func__, enable, ret);
643         } else {
644                 if (st->dbg & MX_DBG_SPEW_MSG)
645                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
646                                  __func__, enable);
647         }
648         return ret;
649 }
650
651 static void mx_pm_exit(struct mx_state *st)
652 {
653         mx_pm(st, false);
654         mx_vreg_exit(st);
655 }
656
657 static int mx_pm_init(struct mx_state *st)
658 {
659         int ret;
660
661         st->enable = 0;
662         st->poll_delay_ms = MX_POLL_DLY_MS_DFLT;
663         mx_vreg_init(st);
664         ret = mx_pm(st, true);
665         return ret;
666 }
667
668 static unsigned int mx_buf_index(unsigned int size, unsigned int *bytes)
669 {
670         unsigned int index;
671
672         if (!(*bytes % size))
673                 index = *bytes;
674         else
675                 index = *bytes - *bytes % size + size;
676         *bytes = index + size;
677         return index;
678 }
679
680 static void mx_buf_push(struct iio_dev *indio_dev)
681 {
682         struct mx_state *st = iio_priv(indio_dev);
683         unsigned char buf[24];
684         unsigned int n;
685         unsigned int i;
686         unsigned int bytes = 0;
687
688         if (!iio_buffer_enabled(indio_dev))
689                 return;
690
691         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_LIGHT)) {
692                 n = sizeof(st->light);
693                 i = mx_buf_index(n, &bytes);
694                 memcpy(&buf[i], &st->light, n);
695                 if (st->dbg & MX_DBG_SPEW_LIGHT)
696                         dev_info(&st->i2c->dev, "light: %u %lld\n",
697                                  st->light, st->ts);
698         }
699         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_TEMP)) {
700                 n = sizeof(st->temp);
701                 i = mx_buf_index(n, &bytes);
702                 memcpy(&buf[i], &st->temp, n);
703                 if (st->dbg & MX_DBG_SPEW_TEMP)
704                         dev_info(&st->i2c->dev, "temp: %d %lld\n",
705                                  st->temp, st->ts);
706         }
707         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_PROX)) {
708                 n = sizeof(st->proximity);
709                 i = mx_buf_index(n, &bytes);
710                 memcpy(&buf[i], &st->proximity, n);
711                 if (st->dbg & MX_DBG_SPEW_PROX)
712                         dev_info(&st->i2c->dev, "proximity: %u %lld\n",
713                                  st->proximity, st->ts);
714         }
715         if (indio_dev->buffer->scan_timestamp) {
716                 n = sizeof(st->ts);
717                 i = mx_buf_index(n, &bytes);
718                 memcpy(&buf[i], &st->ts, n);
719         }
720         iio_push_to_buffers(indio_dev, buf);
721 }
722
723 static int mx_cmd_wr(struct mx_state *st, unsigned int enable, bool irq_en)
724 {
725         u8 amb_cfg = st->amb_cfg;
726         u8 main_cfg = 0;
727         int ret;
728         int ret_t = 0;
729
730         if (enable & (1 << MX_DEV_TEMP))
731                 amb_cfg |= (1 << MX_REG_CFG_AMB_TEMPEN);
732         if (amb_cfg != st->rc_amb_cfg) {
733                 ret = mx_i2c_wr(st, MX_REG_CFG_AMB, amb_cfg);
734                 if (ret)
735                         ret_t |= ret;
736                 else
737                         st->rc_amb_cfg = amb_cfg;
738                 if (st->dbg & MX_DBG_SPEW_MSG)
739                         dev_info(&st->i2c->dev, "%s amb_cfg=%hhx err=%d\n",
740                                  __func__, amb_cfg, ret);
741         }
742         main_cfg = mx_mode_tbl[enable];
743         if (irq_en && st->i2c->irq) {
744                 if (enable & (1 << MX_DEV_LIGHT))
745                         main_cfg |= (1 << MX_REG_CFG_MAIN_AMBINTE);
746                 if (enable & (1 << MX_DEV_PROX))
747                         main_cfg |= (1 << MX_REG_CFG_MAIN_PRXINTE);
748         }
749         if (main_cfg != st->rc_main_cfg) {
750                 ret = mx_i2c_wr(st, MX_REG_CFG_MAIN, main_cfg);
751                 if (ret)
752                         ret_t |= ret;
753                 else
754                         st->rc_main_cfg = main_cfg;
755                 if (st->dbg & MX_DBG_SPEW_MSG)
756                         dev_info(&st->i2c->dev, "%s main_cfg=%hhx err=%d\n",
757                                  __func__, main_cfg, ret);
758         }
759         if (st->rc_main_cfg & MX_REG_CFG_MAIN_INTE_MASK)
760                 ret_t = 1; /* flag IRQ enabled */
761         return ret_t;
762 }
763
764 static void mx_delay(struct mx_state *st)
765 {
766         unsigned int i;
767         unsigned int delay_us = ~0;
768
769         for (i = 0; i < MX_DEV_N; i++) {
770                 if (st->enable & (1 << i)) {
771                         if (st->delay_us[i] && (st->delay_us[i] < delay_us))
772                                 delay_us = st->delay_us[i];
773                 }
774         }
775         if (delay_us == ~0)
776                 delay_us = MX_POLL_DLY_MS_DFLT * 1000;
777         else if (delay_us < (MX_POLL_DLY_MS_MIN * 1000))
778                 delay_us = MX_POLL_DLY_MS_MIN * 1000;
779         st->poll_delay_ms = delay_us / 1000;
780 }
781
782 static int mx_interpolate(int x1, int x2, int x3, int y1, int *y2, int y3)
783 {
784         int dividend;
785         int divisor;
786
787         /* y2 = ((x2 - x1)(y3 - y1)/(x3 - x1)) + y1 */
788         divisor = (x3 - x1);
789         if (!divisor)
790                 return -EINVAL;
791
792         dividend = (x2 - x1) * (y3 - y1);
793         *y2 = (dividend / divisor) + y1;
794         return 0;
795 }
796
797 static void mx_report_init(struct mx_state *st)
798 {
799         st->ts = 0;
800         st->report = st->report_n;
801 }
802
803 static int mx_thr_wr(struct mx_state *st, u8 reg, u16 thr_lo, u16 thr_hi)
804 {
805         u8 buf[5];
806         u16 thr_be;
807         int ret = 0;
808
809         if (st->i2c->irq) {
810                 buf[0] = reg;
811                 thr_be = cpu_to_be16(thr_hi);
812                 buf[1] = thr_be & 0xFF;
813                 buf[2] = thr_be >> 8;
814                 thr_be = cpu_to_be16(thr_lo);
815                 buf[3] = thr_be & 0xFF;
816                 buf[4] = thr_be >> 8;
817                 ret = mx_i2c_write(st, sizeof(buf), buf);
818                 if (st->dbg & MX_DBG_SPEW_MSG)
819                         dev_info(&st->i2c->dev,
820                                  "%s reg=%hhx lo=%hd hi=%hd ret=%d\n",
821                                  __func__, reg, thr_lo, thr_hi, ret);
822         }
823         return ret;
824 }
825
826 static int mx_thr(struct mx_state *st, unsigned int dev, u8 reg,
827                   unsigned int lsb, int val_new, int *val_old, u16 hw_max)
828 {
829         u64 calc;
830         int thr_lo;
831         int thr_hi;
832         int ret = 1;
833
834         /* get calibrated value */
835         mx_interpolate(st->i_uc_lo[dev], val_new,
836                        st->i_uc_hi[dev],
837                        st->i_c_lo[dev], &val_new,
838                        st->i_c_hi[dev]);
839         if ((st->scale_val[dev] == 1) && (st->scale_val2[dev] == 0)) {
840                 /* calibration mode */
841                 st->report = 2; /* 2 causes polling after reporting */
842                 *val_old = val_new;
843                 return 0;
844         }
845
846         if (val_new > (*val_old + st->thr_hi[dev])) {
847                 ret = 0;
848         } else if (*val_old > st->thr_lo[dev]) {
849                 if (val_new < (*val_old - st->thr_lo[dev]))
850                         ret = 0;
851         }
852         /* if already reporting IIO buffer, might as well set new thresholds */
853         if (st->report || !ret) {
854                 if (!st->report)
855                         st->report = st->report_n;
856                 *val_old = val_new;
857                 if (st->i2c->irq && reg) {
858                         thr_lo = val_new - st->thr_lo[dev];
859                         /* get the uncalibrated value */
860                         mx_interpolate(st->i_c_lo[dev], thr_lo,
861                                        st->i_c_hi[dev],
862                                        st->i_uc_lo[dev], &thr_lo,
863                                        st->i_uc_hi[dev]);
864                         if (thr_lo < 0)
865                                 thr_lo = 0;
866                         /* convert to HW value */
867                         calc = thr_lo;
868                         if (st->scale_val2[dev])
869                                 calc *= st->scale_val2[dev];
870                         if (lsb)
871                                 do_div(calc, lsb);
872                         thr_lo = calc;
873                         thr_hi = val_new + st->thr_hi[dev];
874                         /* get the uncalibrated value */
875                         mx_interpolate(st->i_c_lo[dev], thr_hi,
876                                        st->i_c_hi[dev],
877                                        st->i_uc_lo[dev], &thr_hi,
878                                        st->i_uc_hi[dev]);
879                         /* convert to HW value */
880                         calc = thr_hi;
881                         if (st->scale_val2[dev])
882                                 calc *= st->scale_val2[dev];
883                         if (lsb)
884                                 do_div(calc, lsb);
885                         thr_hi = calc;
886                         if (thr_hi > hw_max)
887                                 thr_hi = hw_max;
888                         ret = mx_thr_wr(st, reg, thr_lo, thr_hi);
889                 }
890         }
891         return ret; /* ret == 0: report, ret < 0: report && poll (error) */
892 }
893
894 static int mx_rd_light(struct mx_state *st, s64 ts)
895 {
896         u64 calc;
897         u32 light;
898         u16 hw;
899         unsigned int lsb;
900         int i;
901         int ret;
902
903         ret = mx_i2c_read(st, MX_REG_DATA_AMB_CLEAR, 2, (u8 *)&hw);
904         if (ret)
905                 return ret;
906
907         hw = be16_to_cpu(hw);
908         i = st->rc_amb_cfg & MX_REG_CFG_AMB_AMBPGA_MASK;
909         if (st->dev_id == MX_DEVID_MAX44005)
910                 lsb = mx_ambpga_44005[i];
911         else
912                 lsb = mx_ambpga_44006[i];
913         calc = lsb;
914         calc *= hw;
915         if (st->scale_val2[MX_DEV_LIGHT])
916                 do_div(calc, st->scale_val2[MX_DEV_LIGHT]);
917         light = calc;
918         if (st->dbg & MX_DBG_SPEW_LIGHT_POLL)
919                 dev_info(&st->i2c->dev,
920                          "poll light %u %lld  diff=%d %lldns  hw=%hu\n",
921                          light, ts, light - st->light, ts - st->ts, hw);
922         i = st->rc_amb_cfg & MX_REG_CFG_AMB_AMBTIM_MASK;
923         i >>= MX_REG_CFG_AMB_AMBTIM;
924         ret = mx_thr(st, MX_DEV_LIGHT, MX_REG_AMB_UPTHR_H, lsb, light,
925                      &st->light, mx_ambtim_mask[i]);
926         return ret;
927 }
928
929 static int mx_rd_temp(struct mx_state *st, s64 ts)
930 {
931         s32 temp;
932         s16 hw;
933         int ret;
934
935         ret = mx_i2c_read(st, MX_REG_DATA_TEMP_H, 2, (u8 *)&hw);
936         if (ret)
937                 return ret;
938
939         hw = be16_to_cpu(hw);
940         temp = hw;
941         temp *= (MX_TEMP_RESOLUTION_MICRO / 10000);
942         if (st->dbg & MX_DBG_SPEW_TEMP_POLL)
943                 dev_info(&st->i2c->dev,
944                          "poll temp %d %lld  diff=%d %lldns  hw=%hd\n",
945                          temp, ts, temp - st->temp, ts - st->ts, hw);
946         ret = mx_thr(st, MX_DEV_TEMP, 0, 0, temp, &st->temp, 0);
947         return ret;
948 }
949
950 static int mx_rd_prox(struct mx_state *st, s64 ts)
951 {
952         u16 hw;
953         u16 hw_lo;
954         u16 hw_hi;
955         unsigned int proximity;
956         int ret = 0;
957
958         ret = mx_i2c_read(st, MX_REG_DATA_PROX_H, 2, (u8 *)&hw);
959         if (ret)
960                 return ret;
961
962         hw = be16_to_cpu(hw);
963         if (st->prx_cfg & (1 << MX_REG_CFG_PRX_PRXTIM))
964                 hw_hi = 0x00FF;
965         else
966                 hw_hi = 0x03FF;
967         if (st->prx_thr_bin) {
968                 /* proximity has binary threshold */
969                 if (st->dbg & MX_DBG_SPEW_PROX_POLL)
970                         dev_info(&st->i2c->dev,
971                                  "poll proximity %hu %lld  diff=%d %lldns\n",
972                                  hw, ts, hw - st->prox, ts - st->ts);
973                 if ((st->scale_val[MX_DEV_PROX] == 1) &&
974                                           (st->scale_val2[MX_DEV_PROX] == 0)) {
975                         st->report = 2; /* 2 causes polling after reporting */
976                         st->proximity = hw; /* display raw data */
977                 } else {
978                         /* the proximity threshold lo/hi are reversed */
979                         if (hw > st->prx_thr_bin) {
980                                 proximity = 0;
981                                 if (st->prx_thr_bin > st->thr_hi[MX_DEV_PROX])
982                                         hw_lo = st->prx_thr_bin -
983                                                        st->thr_hi[MX_DEV_PROX];
984                                 else
985                                         hw_lo = 1;
986                                 /* prox_hi already disabled */
987                         } else {
988                                 proximity = 1;
989                                 hw_lo = 0; /* disable */
990                                 hw = st->prx_thr_bin + st->thr_lo[MX_DEV_PROX];
991                                 if (hw < hw_hi)
992                                         hw_hi = hw;
993                                 else
994                                         hw_hi--;
995                         }
996                         if (proximity != st->proximity) {
997                                 if (!st->report)
998                                         st->report = st->report_n;
999                                 st->proximity = proximity;
1000                                 st->prox = hw;
1001                                 ret = mx_thr_wr(st, MX_REG_PRX_UPTHR_H,
1002                                                 hw_lo, hw_hi);
1003                         }
1004                 }
1005         } else {
1006                 /* reverse the high/low order */
1007                 proximity = hw_hi - hw;
1008                 /* get calibrated value with reversed lo/hi */
1009                 mx_interpolate(st->i_uc_lo[MX_DEV_PROX], proximity,
1010                                st->i_uc_hi[MX_DEV_PROX],
1011                                st->i_c_lo[MX_DEV_PROX], &proximity,
1012                                st->i_c_hi[MX_DEV_PROX]);
1013                 if (st->dbg & MX_DBG_SPEW_PROX_POLL)
1014                         dev_info(&st->i2c->dev,
1015                                  "poll prox %u %lld  diff=%d %lldns  hw=%hu\n",
1016                                  proximity, ts,
1017                                  proximity - st->proximity, ts - st->ts, hw);
1018                 /* the proximity threshold lo/hi are reversed */
1019                 ret = mx_thr(st, MX_DEV_PROX, MX_REG_PRX_UPTHR_H, 0,
1020                              hw, &st->prox, hw_hi);
1021                 if (ret <= 0)
1022                         st->proximity = proximity;
1023         }
1024         return ret;
1025 }
1026
1027 static int mx_en(struct mx_state *st, unsigned int enable)
1028 {
1029         int ret;
1030
1031         mx_report_init(st);
1032         ret = mx_i2c_wr(st, MX_REG_CFG_THR, st->thr_cfg);
1033         if (enable & (1 << MX_DEV_PROX))
1034                 ret |= mx_i2c_wr(st, MX_REG_CFG_PRX, st->prx_cfg);
1035         ret |= mx_cmd_wr(st, enable, false);
1036         if (st->dbg & MX_DBG_SPEW_MSG) {
1037                 if (enable & (1 << MX_DEV_PROX))
1038                         dev_info(&st->i2c->dev,
1039                                  "%s thr_cfg=%hhx prx_cfg=%hhx err=%d\n",
1040                                  __func__, st->thr_cfg, st->prx_cfg, ret);
1041                 else
1042                         dev_info(&st->i2c->dev, "%s thr_cfg=%hhx err=%d\n",
1043                                  __func__, st->thr_cfg, ret);
1044         }
1045         return ret;
1046 }
1047
1048 static int mx_rd(struct iio_dev *indio_dev)
1049 {
1050         struct mx_state *st = iio_priv(indio_dev);
1051         u8 sts;
1052         s64 ts;
1053         int ret;
1054
1055         /* clear possible IRQ */
1056         ret = mx_i2c_rd(st, MX_REG_STS, &sts);
1057         if (ret)
1058                 return ret;
1059
1060         if (sts & (1 << MX_REG_STS_PWRON)) {
1061                 /* restart */
1062                 mx_en(st, st->enable);
1063                 return -1;
1064         }
1065
1066         ts = mx_get_time_ns(st);
1067         if (st->report < st->report_n) {
1068                 /* calculate elapsed time for allowed report rate */
1069                 if ((ts - st->ts) < st->poll_delay_ms * 1000000)
1070                         /* data changes are happening faster than allowed to
1071                          * report so we poll for the next data at an allowed
1072                          * rate with interrupts disabled.
1073                          */
1074                         return mx_cmd_wr(st, st->enable, false);
1075         }
1076         if ((st->enable & (1 << MX_DEV_PROX)) &&
1077                                                 (!(st->dbg & MX_DBG_VAL_PROX)))
1078                 ret |= mx_rd_prox(st, ts);
1079         if ((st->enable & (1 << MX_DEV_TEMP)) &&
1080                                                 (!(st->dbg & MX_DBG_VAL_TEMP)))
1081                 ret |= mx_rd_temp(st, ts);
1082         if ((st->enable & (1 << MX_DEV_LIGHT)) &&
1083                                                (!(st->dbg & MX_DBG_VAL_LIGHT)))
1084                 ret |= mx_rd_light(st, ts);
1085         if (st->report) {
1086                 if (st->report)
1087                         st->report--;
1088                 st->ts = ts;
1089                 mx_buf_push(indio_dev);
1090         }
1091         if (st->report || ret < 0)
1092                 /* poll if error or more reporting */
1093                 ret = mx_cmd_wr(st, st->enable, false);
1094         else
1095                 ret = mx_cmd_wr(st, st->enable, true);
1096         return ret;
1097 }
1098
1099 static void mx_read(struct iio_dev *indio_dev)
1100 {
1101         struct mx_state *st = iio_priv(indio_dev);
1102         int ret;
1103
1104         mutex_lock(&indio_dev->mlock);
1105         if (st->enable) {
1106                 ret = mx_rd(indio_dev);
1107                 if (ret < 1)
1108                         schedule_delayed_work(&st->dw,
1109                                           msecs_to_jiffies(st->poll_delay_ms));
1110         }
1111         mutex_unlock(&indio_dev->mlock);
1112 }
1113
1114 static void mx_work(struct work_struct *ws)
1115 {
1116         struct mx_state *st = container_of((struct delayed_work *)ws,
1117                                            struct mx_state, dw);
1118         struct iio_dev *indio_dev = iio_priv_to_dev(st);
1119
1120         mx_read(indio_dev);
1121 }
1122
1123 static irqreturn_t mx_irq_thread(int irq, void *dev_id)
1124 {
1125         struct mx_state *st = (struct mx_state *)dev_id;
1126         struct iio_dev *indio_dev = iio_priv_to_dev(st);
1127
1128         if (st->dbg & MX_DBG_IRQ)
1129                 dev_info(&st->i2c->dev, "%s\n", __func__);
1130         mx_read(indio_dev);
1131         return IRQ_HANDLED;
1132 }
1133
1134 static int mx_disable(struct iio_dev *indio_dev)
1135 {
1136         struct mx_state *st = iio_priv(indio_dev);
1137         int ret = 0;
1138
1139         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1140                                   MX_SCAN_LIGHT)))
1141                 st->dbg &= ~MX_DBG_VAL_LIGHT;
1142         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1143                                   MX_SCAN_TEMP)))
1144                 st->dbg &= ~MX_DBG_VAL_TEMP;
1145         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1146                                   MX_SCAN_PROX)))
1147                 st->dbg &= ~MX_DBG_VAL_PROX;
1148         cancel_delayed_work_sync(&st->dw);
1149         if (st->enable & (1 << MX_DEV_PROX))
1150                 ret = mx_i2c_wr(st, MX_REG_CFG_PRX, 0);
1151         ret |= mx_pm(st, false);
1152         if (!ret)
1153                 st->enable = 0;
1154         return ret;
1155 }
1156
1157 static int mx_enable(struct iio_dev *indio_dev)
1158 {
1159         struct mx_state *st = iio_priv(indio_dev);
1160         unsigned int enable = 0;
1161         int ret = -EINVAL;
1162
1163         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_LIGHT))
1164                 enable |= (1 << MX_DEV_LIGHT);
1165         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_TEMP))
1166                 enable |= (1 << MX_DEV_TEMP);
1167         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_PROX))
1168                 enable |= (1 << MX_DEV_PROX);
1169         if (enable) {
1170                 ret = mx_pm(st, true);
1171                 if (!ret) {
1172                         mx_delay(st);
1173                         ret = mx_en(st, enable);
1174                         if (ret < 0) {
1175                                 mx_disable(indio_dev);
1176                         } else {
1177                                 st->enable = enable;
1178                                 schedule_delayed_work(&st->dw,
1179                                          msecs_to_jiffies(MX_POLL_DLY_MS_MIN));
1180                         }
1181                 }
1182         }
1183         return ret;
1184 }
1185
1186 static ssize_t mx_attr_store(struct device *dev,
1187                              struct device_attribute *attr,
1188                              const char *buf, size_t count)
1189 {
1190         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1191         struct mx_state *st = iio_priv(indio_dev);
1192         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1193         const char *msg;
1194         unsigned int new;
1195         unsigned int old = 0;
1196         int ret;
1197
1198         ret = kstrtouint(buf, 10, &new);
1199         if (ret)
1200                 return -EINVAL;
1201
1202         mutex_lock(&indio_dev->mlock);
1203         if (st->shutdown || st->suspend) {
1204                 mutex_unlock(&indio_dev->mlock);
1205                 return -EPERM;
1206         }
1207
1208         switch (this_attr->address) {
1209         case MX_ATTR_ENABLE:
1210                 msg = "ATTR_ENABLE";
1211                 if (st->enable)
1212                         old = 1;
1213                 if (new)
1214                         ret = mx_enable(indio_dev);
1215                 else
1216                         ret = mx_disable(indio_dev);
1217                 break;
1218
1219         case MX_ATTR_LIGHT_THRESH_LO:
1220                 msg = "ATTR_THRESH_LO";
1221                 old = st->thr_lo[MX_DEV_LIGHT];
1222                 st->thr_lo[MX_DEV_LIGHT] = new;
1223                 break;
1224
1225         case MX_ATTR_LIGHT_THRESH_HI:
1226                 msg = "ATTR_LIGHT_THRESH_HI";
1227                 old = st->thr_hi[MX_DEV_LIGHT];
1228                 st->thr_hi[MX_DEV_LIGHT] = new;
1229                 break;
1230
1231         case MX_ATTR_TEMP_THRESH_LO:
1232                 msg = "ATTR_TEMP_THRESH_LO";
1233                 old = st->thr_lo[MX_DEV_TEMP];
1234                 st->thr_lo[MX_DEV_TEMP] = new;
1235                 break;
1236
1237         case MX_ATTR_TEMP_THRESH_HI:
1238                 msg = "ATTR_TEMP_THRESH_HI";
1239                 old = st->thr_hi[MX_DEV_TEMP];
1240                 st->thr_hi[MX_DEV_TEMP] = new;
1241                 break;
1242
1243         case MX_ATTR_PROX_THRESH_LO:
1244                 msg = "ATTR_PROX_THRESH_LO";
1245                 /* proximity threshold lo/hi are reversed */
1246                 old = st->thr_hi[MX_DEV_PROX];
1247                 st->thr_hi[MX_DEV_PROX] = new;
1248                 break;
1249
1250         case MX_ATTR_PROX_THRESH_HI:
1251                 msg = "ATTR_PROX_THRESH_HI";
1252                 /* proximity threshold lo/hi are reversed */
1253                 old = st->thr_lo[MX_DEV_PROX];
1254                 st->thr_lo[MX_DEV_PROX] = new;
1255                 break;
1256
1257         default:
1258                 msg = "ATTR_UNKNOWN";
1259                 ret = -EINVAL;
1260         }
1261
1262         mx_report_init(st);
1263         mutex_unlock(&indio_dev->mlock);
1264         mx_read(indio_dev);
1265         if (st->dbg & MX_DBG_SPEW_MSG) {
1266                 if (ret)
1267                         dev_err(&st->i2c->dev, "%s %s %d->%d ERR=%d\n",
1268                                 __func__, msg, old, new, ret);
1269                 else
1270                         dev_info(&st->i2c->dev, "%s %s %d->%d\n",
1271                                  __func__, msg, old, new);
1272         }
1273         if (ret)
1274                 return ret;
1275
1276         return count;
1277 }
1278
1279 static ssize_t mx_attr_show(struct device *dev, struct device_attribute *attr,
1280                              char *buf)
1281 {
1282         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1283         struct mx_state *st = iio_priv(indio_dev);
1284         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1285
1286         switch (this_attr->address) {
1287         case MX_ATTR_ENABLE:
1288                 return sprintf(buf, "%x\n", st->enable);
1289
1290         case MX_ATTR_VENDOR:
1291                 return sprintf(buf, "%s\n", MX_VENDOR);
1292
1293         case MX_ATTR_LIGHT_PART:
1294                 return sprintf(buf, "%s light\n", st->part);
1295
1296         case MX_ATTR_LIGHT_VERSION:
1297                 return sprintf(buf, "%u\n", MX_LIGHT_VERSION);
1298
1299         case MX_ATTR_LIGHT_MILLIAMP:
1300                 return sprintf(buf, "%s\n", MX_LIGHT_MILLIAMP);
1301
1302         case MX_ATTR_LIGHT_THRESH_LO:
1303                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_LIGHT]);
1304
1305         case MX_ATTR_LIGHT_THRESH_HI:
1306                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_LIGHT]);
1307
1308         case MX_ATTR_TEMP_PART:
1309                 return sprintf(buf, "%s temperature\n", st->part);
1310
1311         case MX_ATTR_TEMP_VERSION:
1312                 return sprintf(buf, "%u\n", MX_TEMP_VERSION);
1313
1314         case MX_ATTR_TEMP_MILLIAMP:
1315                 return sprintf(buf, "%s\n", MX_TEMP_MILLIAMP);
1316
1317         case MX_ATTR_TEMP_THRESH_LO:
1318                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_TEMP]);
1319
1320         case MX_ATTR_TEMP_THRESH_HI:
1321                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_TEMP]);
1322
1323         case MX_ATTR_PROX_PART:
1324                 return sprintf(buf, "%s proximity\n", st->part);
1325
1326         case MX_ATTR_PROX_VERSION:
1327                 return sprintf(buf, "%u\n", MX_PROX_VERSION);
1328
1329         case MX_ATTR_PROX_MILLIAMP:
1330                 return sprintf(buf, "%s\n", MX_PROX_MILLIAMP);
1331
1332         case MX_ATTR_PROX_THRESH_LO:
1333                 /* proximity threshold lo/hi are reversed */
1334                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_PROX]);
1335
1336         case MX_ATTR_PROX_THRESH_HI:
1337                 /* proximity threshold lo/hi are reversed */
1338                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_PROX]);
1339
1340         default:
1341                 return -EINVAL;
1342         }
1343
1344         return -EINVAL;
1345 }
1346
1347 static ssize_t mx_data_store(struct device *dev,
1348                              struct device_attribute *attr,
1349                              const char *buf, size_t count)
1350 {
1351         struct mx_state *st = iio_priv(dev_get_drvdata(dev));
1352         unsigned int info;
1353         int ret;
1354
1355         ret = kstrtouint(buf, 10, &info);
1356         if (ret)
1357                 return -EINVAL;
1358
1359         if (info >= MX_INFO_LIMIT_MAX)
1360                 return -EINVAL;
1361
1362         st->info = info;
1363         switch (info) {
1364         case MX_INFO_DATA:
1365                 st->dbg = 0;
1366                 break;
1367
1368         case MX_INFO_DBG:
1369                 st->dbg ^= MX_DBG_SPEW_MSG;
1370                 break;
1371
1372         case MX_INFO_LIGHT_SPEW:
1373                 st->dbg ^= MX_DBG_SPEW_LIGHT;
1374                 break;
1375
1376         case MX_INFO_LIGHT_POLL_SPEW:
1377                 st->dbg ^= MX_DBG_SPEW_LIGHT_POLL;
1378                 break;
1379
1380         case MX_INFO_TEMP_SPEW:
1381                 st->dbg ^= MX_DBG_SPEW_TEMP;
1382                 break;
1383
1384         case MX_INFO_TEMP_POLL_SPEW:
1385                 st->dbg ^= MX_DBG_SPEW_TEMP_POLL;
1386                 break;
1387
1388         case MX_INFO_PROXIMITY_SPEW:
1389                 st->dbg ^= MX_DBG_SPEW_PROX;
1390                 break;
1391
1392         case MX_INFO_PROXIMITY_POLL_SPEW:
1393                 st->dbg ^= MX_DBG_SPEW_PROX_POLL;
1394                 break;
1395
1396         case MX_INFO_DBG_IRQ:
1397                 st->dbg ^= MX_DBG_IRQ;
1398                 break;
1399
1400         default:
1401                 break;
1402         }
1403
1404         return count;
1405 }
1406
1407 static ssize_t mx_data_show(struct device *dev,
1408                             struct device_attribute *attr, char *buf)
1409 {
1410         struct mx_state *st = iio_priv(dev_get_drvdata(dev));
1411         enum MX_INFO info;
1412         ssize_t t;
1413         u8 val[2];
1414         u8 i;
1415         int ret;
1416
1417         info = st->info;
1418         st->info = MX_INFO_DATA;
1419         switch (info) {
1420         case MX_INFO_DATA:
1421                 t = sprintf(buf, "light: %u\n", st->light);
1422                 t += sprintf(buf + t, "temp: %d\n", st->temp);
1423                 if (st->dev_id == MX_DEVID_MAX44005)
1424                         t += sprintf(buf + t, "proximity: %u\n",
1425                                      st->proximity);
1426                 t += sprintf(buf + t, "timestamp: %lld\n", st->ts);
1427                 return t;
1428
1429         case MX_INFO_VER:
1430                 return sprintf(buf, "version=%u\n", MX_VERSION_DRIVER);
1431
1432         case MX_INFO_ERRS:
1433                 t = sprintf(buf, "error count=%u\n", st->errs);
1434                 st->errs = 0;
1435                 return t;
1436
1437         case MX_INFO_REGS:
1438                 t = sprintf(buf, "registers:\n");
1439                 for (i = 0; i <= MX_REG_CFG_PRX; i++) {
1440                         ret = mx_i2c_rd(st, i, val);
1441                         if (!ret)
1442                                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1443                                              i, val[0]);
1444                 }
1445                 for (i = MX_REG_DATA_AMB_CLEAR; i < MX_REG_CFG_THR; i += 2) {
1446                         ret = mx_i2c_read(st, i, 2, val);
1447                         if (!ret)
1448                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
1449                                         i, i + 1, be16_to_cpup((__be16 *)val));
1450                 }
1451                 ret = mx_i2c_rd(st, MX_REG_CFG_THR, val);
1452                 if (!ret)
1453                         t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1454                                      MX_REG_CFG_THR, val[0]);
1455                 for (i = MX_REG_PRX_UPTHR_H; i < MX_REG_TRIM_CLEAR; i += 2) {
1456                         ret = mx_i2c_read(st, i, 2, val);
1457                         if (!ret)
1458                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
1459                                         i, i + 1, be16_to_cpup((__be16 *)val));
1460                 }
1461                 for (i = MX_REG_TRIM_CLEAR; i <= MX_REG_TRIM_IR; i++) {
1462                         ret = mx_i2c_rd(st, i, val);
1463                         if (!ret)
1464                                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1465                                              i, val[0]);
1466                 }
1467                 return t;
1468
1469         case MX_INFO_DBG:
1470                 return sprintf(buf, "debug spew=%x\n",
1471                                st->dbg & MX_DBG_SPEW_MSG);
1472
1473         case MX_INFO_LIGHT_SPEW:
1474                 return sprintf(buf, "light_ts spew=%x\n",
1475                                !!(st->dbg & MX_DBG_SPEW_LIGHT));
1476
1477         case MX_INFO_LIGHT_POLL_SPEW:
1478                 return sprintf(buf, "light_poll_ts spew=%x\n",
1479                                !!(st->dbg & MX_DBG_SPEW_LIGHT_POLL));
1480
1481         case MX_INFO_TEMP_SPEW:
1482                 return sprintf(buf, "temp_ts spew=%x\n",
1483                                !!(st->dbg & MX_DBG_SPEW_TEMP));
1484
1485         case MX_INFO_TEMP_POLL_SPEW:
1486                 return sprintf(buf, "temp_poll_ts spew=%x\n",
1487                                !!(st->dbg & MX_DBG_SPEW_TEMP_POLL));
1488
1489         case MX_INFO_PROXIMITY_SPEW:
1490                 return sprintf(buf, "proximity_ts spew=%x\n",
1491                                !!(st->dbg & MX_DBG_SPEW_PROX));
1492
1493         case MX_INFO_PROXIMITY_POLL_SPEW:
1494                 return sprintf(buf, "proximity_poll_ts spew=%x\n",
1495                                !!(st->dbg & MX_DBG_SPEW_PROX_POLL));
1496
1497         case MX_INFO_DBG_IRQ:
1498                 return sprintf(buf, "debug IRQ=%x\n",
1499                                !!(st->dbg & MX_DBG_IRQ));
1500
1501         default:
1502                 break;
1503         }
1504
1505         return -EINVAL;
1506 }
1507
1508 static IIO_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1509                        mx_attr_show, mx_attr_store, MX_ATTR_ENABLE);
1510 static IIO_DEVICE_ATTR(illuminance_part, S_IRUGO,
1511                        mx_attr_show, NULL, MX_ATTR_LIGHT_PART);
1512 static IIO_DEVICE_ATTR(illuminance_vendor, S_IRUGO,
1513                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1514 static IIO_DEVICE_ATTR(illuminance_version, S_IRUGO,
1515                        mx_attr_show, NULL, MX_ATTR_LIGHT_VERSION);
1516 static IIO_DEVICE_ATTR(illuminance_milliamp, S_IRUGO,
1517                        mx_attr_show, NULL, MX_ATTR_LIGHT_MILLIAMP);
1518 static IIO_DEVICE_ATTR(illuminance_thresh_rising_value,
1519                        S_IRUGO | S_IWUSR | S_IWGRP,
1520                        mx_attr_show, mx_attr_store, MX_ATTR_LIGHT_THRESH_HI);
1521 static IIO_DEVICE_ATTR(illuminance_thresh_falling_value,
1522                        S_IRUGO | S_IWUSR | S_IWGRP,
1523                        mx_attr_show, mx_attr_store, MX_ATTR_LIGHT_THRESH_LO);
1524 static IIO_DEVICE_ATTR(temp_part, S_IRUGO,
1525                        mx_attr_show, NULL, MX_ATTR_TEMP_PART);
1526 static IIO_DEVICE_ATTR(temp_vendor, S_IRUGO,
1527                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1528 static IIO_DEVICE_ATTR(temp_version, S_IRUGO,
1529                        mx_attr_show, NULL, MX_ATTR_TEMP_VERSION);
1530 static IIO_DEVICE_ATTR(temp_milliamp, S_IRUGO,
1531                        mx_attr_show, NULL, MX_ATTR_TEMP_MILLIAMP);
1532 static IIO_DEVICE_ATTR(temp_thresh_rising_value,
1533                        S_IRUGO | S_IWUSR | S_IWGRP,
1534                        mx_attr_show, mx_attr_store, MX_ATTR_TEMP_THRESH_HI);
1535 static IIO_DEVICE_ATTR(temp_thresh_falling_value,
1536                        S_IRUGO | S_IWUSR | S_IWGRP,
1537                        mx_attr_show, mx_attr_store, MX_ATTR_TEMP_THRESH_LO);
1538 static IIO_DEVICE_ATTR(proximity_part, S_IRUGO,
1539                        mx_attr_show, NULL, MX_ATTR_PROX_PART);
1540 static IIO_DEVICE_ATTR(proximity_vendor, S_IRUGO,
1541                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1542 static IIO_DEVICE_ATTR(proximity_version, S_IRUGO,
1543                        mx_attr_show, NULL, MX_ATTR_PROX_VERSION);
1544 static IIO_DEVICE_ATTR(proximity_milliamp, S_IRUGO,
1545                        mx_attr_show, NULL, MX_ATTR_PROX_MILLIAMP);
1546 static IIO_DEVICE_ATTR(proximity_thresh_rising_value,
1547                        S_IRUGO | S_IWUSR | S_IWGRP,
1548                        mx_attr_show, mx_attr_store, MX_ATTR_PROX_THRESH_HI);
1549 static IIO_DEVICE_ATTR(proximity_thresh_falling_value,
1550                        S_IRUGO | S_IWUSR | S_IWGRP,
1551                        mx_attr_show, mx_attr_store, MX_ATTR_PROX_THRESH_LO);
1552 static DEVICE_ATTR(data, S_IRUGO | S_IWUSR | S_IWGRP,
1553                    mx_data_show, mx_data_store);
1554
1555 static struct attribute *mx_attrs[] = {
1556         &dev_attr_data.attr,
1557         &iio_dev_attr_enable.dev_attr.attr,
1558         &iio_dev_attr_illuminance_part.dev_attr.attr,
1559         &iio_dev_attr_illuminance_vendor.dev_attr.attr,
1560         &iio_dev_attr_illuminance_version.dev_attr.attr,
1561         &iio_dev_attr_illuminance_milliamp.dev_attr.attr,
1562         &iio_dev_attr_illuminance_thresh_rising_value.dev_attr.attr,
1563         &iio_dev_attr_illuminance_thresh_falling_value.dev_attr.attr,
1564         &iio_dev_attr_temp_part.dev_attr.attr,
1565         &iio_dev_attr_temp_vendor.dev_attr.attr,
1566         &iio_dev_attr_temp_version.dev_attr.attr,
1567         &iio_dev_attr_temp_milliamp.dev_attr.attr,
1568         &iio_dev_attr_temp_thresh_rising_value.dev_attr.attr,
1569         &iio_dev_attr_temp_thresh_falling_value.dev_attr.attr,
1570         /* proximity_part must be the first proximity attribute */
1571         &iio_dev_attr_proximity_part.dev_attr.attr,
1572         &iio_dev_attr_proximity_vendor.dev_attr.attr,
1573         &iio_dev_attr_proximity_version.dev_attr.attr,
1574         &iio_dev_attr_proximity_milliamp.dev_attr.attr,
1575         &iio_dev_attr_proximity_thresh_rising_value.dev_attr.attr,
1576         &iio_dev_attr_proximity_thresh_falling_value.dev_attr.attr,
1577         NULL
1578 };
1579
1580 static struct attribute_group mx_attr_group = {
1581         .name = MX_NAME,
1582         .attrs = mx_attrs
1583 };
1584
1585 static int mx_read_raw(struct iio_dev *indio_dev,
1586                        struct iio_chan_spec const *chan,
1587                        int *val, int *val2, long mask)
1588 {
1589         struct mx_state *st = iio_priv(indio_dev);
1590         int i = chan->scan_index;
1591         int ret = -EINVAL;
1592
1593         switch (mask) {
1594         case IIO_CHAN_INFO_RAW:
1595                 switch (chan->type) {
1596                 case IIO_LIGHT:
1597                         *val = st->light;
1598                         return IIO_VAL_INT;
1599
1600                 case IIO_TEMP:
1601                         *val = st->temp;
1602                         return IIO_VAL_INT;
1603
1604                 case IIO_PROXIMITY:
1605                         *val = st->proximity;
1606                         return IIO_VAL_INT;
1607
1608                 default:
1609                         return -EINVAL;
1610                 }
1611
1612         case IIO_CHAN_INFO_SAMP_FREQ:
1613                 if (st->enable & (1 << i))
1614                         *val = st->delay_us[i];
1615                 else
1616                         *val = MX_POLL_DLY_MS_MIN * 1000;
1617                 return IIO_VAL_INT;
1618
1619         case IIO_CHAN_INFO_SCALE:
1620                 *val = st->scale_val[i];
1621                 *val2 = st->scale_val2[i];
1622                 return IIO_VAL_INT_PLUS_MICRO;
1623
1624         case IIO_CHAN_INFO_OFFSET:
1625                 *val = st->offset_val[i];
1626                 *val2 = st->offset_val2[i];
1627                 return IIO_VAL_INT_PLUS_MICRO;
1628
1629         case IIO_CHAN_INFO_PEAK:
1630                 *val = st->peak_val[i];
1631                 *val2 = st->peak_val2[i];
1632                 return IIO_VAL_INT_PLUS_MICRO;
1633
1634         case IIO_CHAN_INFO_PEAK_SCALE:
1635                 *val = st->peak_scale_val[i];
1636                 *val2 = st->peak_scale_val2[i];
1637                 return IIO_VAL_INT_PLUS_MICRO;
1638
1639         default:
1640                 return -EINVAL;
1641         }
1642
1643         return ret;
1644 }
1645
1646 static int mx_write_raw(struct iio_dev *indio_dev,
1647                         struct iio_chan_spec const *chan,
1648                         int val, int val2, long mask)
1649 {
1650         struct mx_state *st = iio_priv(indio_dev);
1651         char *msg;
1652         int i = chan->scan_index;
1653         int old = 0;
1654         int old2 = 0;
1655         int ret = 0;
1656
1657         mutex_lock(&indio_dev->mlock);
1658         if (st->shutdown || st->suspend) {
1659                 mutex_unlock(&indio_dev->mlock);
1660                 return -EPERM;
1661         }
1662
1663         switch (mask) {
1664         case IIO_CHAN_INFO_SAMP_FREQ:
1665                 msg = "IIO_CHAN_INFO_SAMP_FREQ";
1666                 old = st->delay_us[i];
1667                 st->delay_us[i] = val;
1668                 mx_delay(st);
1669                 break;
1670
1671         case IIO_CHAN_INFO_SCALE:
1672                 msg = "IIO_CHAN_INFO_SCALE";
1673                 old = st->scale_val[i];
1674                 old2 = st->scale_val2[i];
1675                 st->scale_val[i] = val;
1676                 st->scale_val2[i] = val2;
1677                 break;
1678
1679         case IIO_CHAN_INFO_OFFSET:
1680                 msg = "IIO_CHAN_INFO_SCALE";
1681                 old = st->offset_val[i];
1682                 old2 = st->offset_val2[i];
1683                 st->offset_val[i] = val;
1684                 st->offset_val2[i] = val2;
1685                 break;
1686
1687         case IIO_CHAN_INFO_PEAK:
1688                 msg = "IIO_CHAN_INFO_PEAK";
1689                 old = st->peak_val[i];
1690                 old2 = st->peak_val2[i];
1691                 st->peak_val[i] = val;
1692                 st->peak_val2[i] = val2;
1693                 break;
1694
1695         case IIO_CHAN_INFO_PEAK_SCALE:
1696                 msg = "IIO_CHAN_INFO_PEAK_SCALE";
1697                 old = st->peak_scale_val[i];
1698                 old2 = st->peak_scale_val2[i];
1699                 st->peak_scale_val[i] = val;
1700                 st->peak_scale_val2[i] = val2;
1701                 break;
1702
1703         case IIO_CHAN_INFO_RAW:
1704                 msg = "IIO_CHAN_INFO_RAW";
1705                 switch (chan->type) {
1706                 case IIO_LIGHT:
1707                         old = st->light;
1708                         st->light = val;
1709                         st->dbg |= MX_DBG_VAL_LIGHT;
1710                         if ((st->enable & (1 << MX_DEV_LIGHT)) && !st->report)
1711                                 st->report = 1;
1712                         break;
1713
1714                 case IIO_TEMP:
1715                         old = st->temp;
1716                         st->temp = val;
1717                         st->dbg |= MX_DBG_VAL_TEMP;
1718                         if ((st->enable & (1 << MX_DEV_TEMP)) && !st->report)
1719                                 st->report = 1;
1720                         break;
1721
1722                 case IIO_PROXIMITY:
1723                         old = st->proximity;
1724                         st->proximity = val;
1725                         st->dbg |= MX_DBG_VAL_PROX;
1726                         if ((st->enable & (1 << MX_DEV_PROX)) && !st->report)
1727                                 st->report = 1;
1728                         break;
1729
1730                 default:
1731                         ret = -EINVAL;
1732                 }
1733
1734                 break;
1735
1736         default:
1737                 msg = "IIO_CHAN_INFO_UNKNOWN";
1738                 ret = -EINVAL;
1739         }
1740
1741         mx_report_init(st);
1742         mutex_unlock(&indio_dev->mlock);
1743         mx_read(indio_dev);
1744         if (st->dbg & MX_DBG_SPEW_MSG) {
1745                 if (ret)
1746                         dev_err(&st->i2c->dev, "%s c=%d %d:%d->%d:%d ERR=%d\n",
1747                                 msg, chan->scan_index,
1748                                 old, old2, val, val2, ret);
1749                 else
1750                         dev_info(&st->i2c->dev, "%s %s chan=%d %d:%d->%d:%d\n",
1751                                  __func__, msg, chan->scan_index,
1752                                  old, old2, val, val2);
1753         }
1754         return ret;
1755 }
1756
1757 static const struct iio_info mx_iio_info = {
1758         .driver_module = THIS_MODULE,
1759         .attrs = &mx_attr_group,
1760         .read_raw = &mx_read_raw,
1761         .write_raw = &mx_write_raw,
1762 };
1763
1764 static struct iio_chan_spec mx_channels[] = {
1765         {
1766                 .type                   = IIO_LIGHT,
1767                 .scan_index             = MX_SCAN_LIGHT,
1768                 .scan_type              = IIO_ST('u', 32, 32, 0),
1769                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1770                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1771                                           BIT(IIO_CHAN_INFO_PEAK) |
1772                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1773                                           BIT(IIO_CHAN_INFO_SCALE) |
1774                                           BIT(IIO_CHAN_INFO_OFFSET),
1775                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1776                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1777                                           BIT(IIO_CHAN_INFO_PEAK) |
1778                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1779                                           BIT(IIO_CHAN_INFO_SCALE) |
1780                                           BIT(IIO_CHAN_INFO_OFFSET),
1781         },
1782         {
1783                 .type                   = IIO_TEMP,
1784                 .scan_index             = MX_SCAN_TEMP,
1785                 .scan_type              = IIO_ST('s', 32, 32, 0),
1786                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1787                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1788                                           BIT(IIO_CHAN_INFO_PEAK) |
1789                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1790                                           BIT(IIO_CHAN_INFO_SCALE) |
1791                                           BIT(IIO_CHAN_INFO_OFFSET),
1792                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1793                                           BIT(IIO_CHAN_INFO_PEAK) |
1794                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1795                                           BIT(IIO_CHAN_INFO_SCALE) |
1796                                           BIT(IIO_CHAN_INFO_OFFSET),
1797                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1798         },
1799         {
1800                 .type                   = IIO_PROXIMITY,
1801                 .scan_index             = MX_SCAN_PROX,
1802                 .scan_type              = IIO_ST('u', 32, 32, 0),
1803                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1804                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1805                                           BIT(IIO_CHAN_INFO_PEAK) |
1806                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1807                                           BIT(IIO_CHAN_INFO_SCALE) |
1808                                           BIT(IIO_CHAN_INFO_OFFSET),
1809                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1810                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1811                                           BIT(IIO_CHAN_INFO_PEAK) |
1812                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1813                                           BIT(IIO_CHAN_INFO_SCALE) |
1814                                           BIT(IIO_CHAN_INFO_OFFSET),
1815         },
1816         IIO_CHAN_SOFT_TIMESTAMP(MX_SCAN_TIMESTAMP)
1817 };
1818
1819 static int mx_buffer_preenable(struct iio_dev *indio_dev)
1820 {
1821         struct mx_state *st = iio_priv(indio_dev);
1822
1823         if (st->shutdown || st->suspend)
1824                 return -EINVAL;
1825
1826         return iio_sw_buffer_preenable(indio_dev);
1827 }
1828
1829 static int mx_buffer_postenable(struct iio_dev *indio_dev)
1830 {
1831         int ret;
1832
1833         ret = mx_enable(indio_dev);
1834         /* never return > 0 to IIO buffer engine */
1835         if (ret > 0)
1836                 ret = 0;
1837         return ret;
1838 }
1839
1840 static const struct iio_buffer_setup_ops mx_buffer_setup_ops = {
1841         /* iio_sw_buffer_preenable:
1842          * Generic function for equal sized ring elements + 64 bit timestamp
1843          * Assumes that any combination of channels can be enabled.
1844          * Typically replaced to implement restrictions on what combinations
1845          * can be captured (hardware scan modes).
1846          */
1847         .preenable = &mx_buffer_preenable,
1848         /* iio_triggered_buffer_postenable:
1849          * Generic function that simply attaches the pollfunc to the trigger.
1850          * Replace this to mess with hardware state before we attach the
1851          * trigger.
1852          */
1853         .postenable = &mx_buffer_postenable,
1854         /* this driver relies on the NVS HAL to power off this device with the
1855          * master enable.
1856          *.predisable = N/A
1857          *.postdisable = N/A
1858          */
1859 };
1860
1861 static const struct iio_trigger_ops mx_trigger_ops = {
1862         .owner = THIS_MODULE,
1863 };
1864
1865 static int mx_suspend(struct device *dev)
1866 {
1867         struct i2c_client *client = to_i2c_client(dev);
1868         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1869         struct mx_state *st = iio_priv(indio_dev);
1870         int ret = 0;
1871
1872         mutex_lock(&indio_dev->mlock);
1873         if (st->enable && !(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1874                                                 MX_SCAN_PROX))) {
1875                 ret = mx_disable(indio_dev);
1876                 st->suspend = true;
1877         }
1878         mutex_unlock(&indio_dev->mlock);
1879         if (st->dbg & MX_DBG_SPEW_MSG)
1880                 dev_info(&client->dev, "%s\n", __func__);
1881         return ret;
1882 }
1883
1884 static int mx_resume(struct device *dev)
1885 {
1886         struct i2c_client *client = to_i2c_client(dev);
1887         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1888         struct mx_state *st = iio_priv(indio_dev);
1889
1890         st->suspend = false;
1891         if (st->dbg & MX_DBG_SPEW_MSG)
1892                 dev_info(&client->dev, "%s\n", __func__);
1893         return 0;
1894 }
1895
1896 static SIMPLE_DEV_PM_OPS(mx_pm_ops, mx_suspend, mx_resume);
1897
1898 static int mx_id_dev(struct iio_dev *indio_dev, const char *name)
1899 {
1900         struct mx_state *st = iio_priv(indio_dev);
1901         u8 val;
1902         int ret = 0;
1903
1904         if (!strcmp(name, MX_NAME_MAX44008)) {
1905                 st->dev_id = MX_DEVID_MAX44008;
1906                 st->part = MX_NAME_MAX44008;
1907         } else if (!strcmp(name, MX_NAME_MAX44006)) {
1908                 st->dev_id = MX_DEVID_MAX44006;
1909                 st->part = MX_NAME_MAX44006;
1910         } else if (!strcmp(name, MX_NAME_MAX44005)) {
1911                 st->dev_id = MX_DEVID_MAX44005;
1912                 st->part = MX_NAME_MAX44005;
1913         } else if (!strcmp(name, MX_NAME)) {
1914                 /* There is no way to auto-detect the device since the
1915                  * MX44006/8 has actual proximity HW that works but just
1916                  * doesn't have the undetectable LED driver HW.  And of
1917                  * course there isn't an ID register either.  So we'll
1918                  * just confirm that our device exists and default to the
1919                  * MX44005 with proximity support if using max4400x.
1920                  */
1921                 st->dev_id = MX_DEVID_MAX44005;
1922                 st->part = MX_NAME_MAX44005;
1923                 ret = mx_i2c_rd(st, MX_REG_STS, &val);
1924                 if (ret)
1925                         return -ENODEV;
1926
1927                 if (val != MX_REG_STS_POR)
1928                         return -ENODEV;
1929         }
1930
1931         return ret;
1932 }
1933
1934 static int mx_id_i2c(struct iio_dev *indio_dev, const char *name)
1935 {
1936         struct mx_state *st = iio_priv(indio_dev);
1937         int i;
1938         int ret;
1939
1940         for (i = 0; i < ARRAY_SIZE(mx_i2c_addrs); i++) {
1941                 if (st->i2c->addr == mx_i2c_addrs[i])
1942                         break;
1943         }
1944
1945         if (i < ARRAY_SIZE(mx_i2c_addrs)) {
1946                 st->i2c_addr = st->i2c->addr;
1947                 ret = mx_id_dev(indio_dev, name);
1948         } else {
1949                 for (i = 0; i < ARRAY_SIZE(mx_i2c_addrs); i++) {
1950                         st->i2c_addr = mx_i2c_addrs[i];
1951                         ret = mx_id_dev(indio_dev, name);
1952                         if (!ret)
1953                                 break;
1954                 }
1955         }
1956         if (ret)
1957                 st->i2c_addr = 0;
1958         return ret;
1959 }
1960
1961 static void mx_shutdown(struct i2c_client *client)
1962 {
1963         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1964         struct mx_state *st = iio_priv(indio_dev);
1965
1966         mutex_lock(&indio_dev->mlock);
1967         if (st->enable)
1968                 mx_disable(indio_dev);
1969         st->shutdown = true;
1970         mutex_unlock(&indio_dev->mlock);
1971         if (st->dbg & MX_DBG_SPEW_MSG)
1972                 dev_info(&client->dev, "%s\n", __func__);
1973 }
1974
1975 static int mx_remove(struct i2c_client *client)
1976 {
1977         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1978         struct mx_state *st = iio_priv(indio_dev);
1979
1980         if (st != NULL) {
1981                 mx_shutdown(client);
1982                 if (indio_dev->dev.devt)
1983                         iio_device_unregister(indio_dev);
1984                 if (st->trig != NULL) {
1985                         if (client->irq)
1986                                 free_irq(client->irq, st);
1987                         iio_trigger_unregister(st->trig);
1988                         iio_trigger_free(st->trig);
1989                 }
1990                 if (indio_dev->buffer != NULL) {
1991                         iio_buffer_unregister(indio_dev);
1992                         iio_kfifo_free(indio_dev->buffer);
1993                 }
1994                 if (st->dw.wq)
1995                         destroy_workqueue(st->dw.wq);
1996                 mx_pm_exit(st);
1997                 iio_device_free(indio_dev);
1998         }
1999         dev_info(&client->dev, "%s\n", __func__);
2000         return 0;
2001 }
2002
2003 static const struct mx_iio_float mx_scale_dflt[] = {
2004         {
2005                 .ival                   = MX_LIGHT_SCALE_IVAL,
2006                 .micro                  = MX_LIGHT_SCALE_MICRO,
2007         },
2008         {
2009                 .ival                   = MX_TEMP_SCALE_IVAL,
2010                 .micro                  = MX_TEMP_SCALE_MICRO,
2011         },
2012         {
2013                 .ival                   = MX_PROX_SCALE_IVAL,
2014                 .micro                  = MX_PROX_SCALE_MICRO,
2015         },
2016 };
2017
2018 static const struct mx_iio_float mx_offset_dflt[] = {
2019         {
2020                 .ival                   = MX_LIGHT_OFFSET_IVAL,
2021                 .micro                  = MX_LIGHT_OFFSET_MICRO,
2022         },
2023         {
2024                 .ival                   = MX_TEMP_OFFSET_IVAL,
2025                 .micro                  = MX_TEMP_OFFSET_MICRO,
2026         },
2027         {
2028                 .ival                   = MX_PROX_OFFSET_IVAL,
2029                 .micro                  = MX_PROX_OFFSET_MICRO,
2030         },
2031 };
2032
2033 static const struct mx_iio_float mx_peak_dflt[] = {
2034         {
2035                 .ival                   = MX_LIGHT_MAX_RANGE_IVAL,
2036                 .micro                  = MX_LIGHT_MAX_RANGE_MICRO,
2037         },
2038         {
2039                 .ival                   = MX_TEMP_MAX_RANGE_IVAL,
2040                 .micro                  = MX_TEMP_MAX_RANGE_MICRO,
2041         },
2042         {
2043                 .ival                   = MX_PROX_MAX_RANGE_IVAL,
2044                 .micro                  = MX_PROX_MAX_RANGE_MICRO,
2045         },
2046 };
2047
2048 static const struct mx_iio_float mx_peak_scale_dflt[] = {
2049         {
2050                 .ival                   = MX_LIGHT_RESOLUTION_IVAL,
2051                 .micro                  = MX_LIGHT_RESOLUTION_MICRO,
2052         },
2053         {
2054                 .ival                   = MX_TEMP_RESOLUTION_IVAL,
2055                 .micro                  = MX_TEMP_RESOLUTION_MICRO,
2056         },
2057         {
2058                 .ival                   = MX_PROX_RESOLUTION_IVAL,
2059                 .micro                  = MX_PROX_RESOLUTION_MICRO,
2060         },
2061 };
2062
2063 static const unsigned int mx_threshold_dflt[] = {
2064         MX_LIGHT_THRESHOLD_DFLT,
2065         MX_TEMP_THRESHOLD_DFLT,
2066         MX_PROX_THRESHOLD_DFLT,
2067 };
2068
2069 static int mx_parse_dt(struct i2c_client *client, struct mx_state *st)
2070 {
2071         struct device_node *dn = client->dev.of_node;
2072         unsigned int thresh;
2073         unsigned int i;
2074
2075         /* default device specific parameters */
2076         st->amb_cfg = MX_AMB_CFG_DFLT;
2077         st->prx_cfg = MX_PRX_CFG_DFLT;
2078         /* default NVS ALS programmable parameters */
2079         for (i = 0; i < MX_DEV_N; i++) {
2080                 st->scale_val[i] = mx_scale_dflt[i].ival;
2081                 st->scale_val2[i] = mx_scale_dflt[i].micro;
2082                 st->offset_val[i] = mx_offset_dflt[i].ival;
2083                 st->offset_val2[i] = mx_offset_dflt[i].micro;
2084                 st->peak_val[i] = mx_peak_dflt[i].ival;
2085                 st->peak_val2[i] = mx_peak_dflt[i].micro;
2086                 st->peak_scale_val[i] = mx_peak_scale_dflt[i].ival;
2087                 st->peak_scale_val2[i] = mx_peak_scale_dflt[i].micro;
2088                 if (st->scale_val2[i]) {
2089                         thresh = 1000000 / st->scale_val2[i];
2090                         thresh *= mx_threshold_dflt[i];
2091                 } else {
2092                         thresh = mx_threshold_dflt[i];
2093                 }
2094                 st->thr_lo[i] = thresh;
2095                 st->thr_hi[i] = thresh;
2096         }
2097         st->prx_thr_bin = MX_PROX_THRESHOLD_BINARY_DFLT;
2098         /* device tree parameters */
2099         if (client->dev.of_node) {
2100                 /* device specific parameters */
2101                 of_property_read_u8(dn, "ambient_cfg_reg", &st->amb_cfg);
2102                 of_property_read_u8(dn, "proximity_cfg_reg", &st->prx_cfg);
2103                 of_property_read_u8(dn, "threshold_persist_reg", &st->thr_cfg);
2104                 /* common NVS programmable parameters */
2105                 st->iio_ts_en = of_property_read_bool(dn, "iio_timestamps");
2106                 of_property_read_u32(dn, "report_count", &st->report_n);
2107                 /* common NVS ALS programmable parameters */
2108                 of_property_read_s32(dn, "light_uncalibrated_lo",
2109                                      &st->i_uc_lo[MX_DEV_LIGHT]);
2110                 of_property_read_s32(dn, "light_uncalibrated_hi",
2111                                      &st->i_uc_hi[MX_DEV_LIGHT]);
2112                 of_property_read_s32(dn, "light_calibrated_lo",
2113                                      &st->i_c_lo[MX_DEV_LIGHT]);
2114                 of_property_read_s32(dn, "light_calibrated_hi",
2115                                      &st->i_c_hi[MX_DEV_LIGHT]);
2116                 of_property_read_s32(dn, "light_max_range_val",
2117                                      &st->peak_val[MX_DEV_LIGHT]);
2118                 of_property_read_s32(dn, "light_max_range_val2",
2119                                      &st->peak_val2[MX_DEV_LIGHT]);
2120                 of_property_read_s32(dn, "light_resolution_val",
2121                                      &st->peak_scale_val[MX_DEV_LIGHT]);
2122                 of_property_read_s32(dn, "light_resolution_val2",
2123                                      &st->peak_scale_val2[MX_DEV_LIGHT]);
2124                 of_property_read_s32(dn, "light_scale_val",
2125                                      &st->scale_val[MX_DEV_LIGHT]);
2126                 of_property_read_s32(dn, "light_scale_val2",
2127                                      &st->scale_val2[MX_DEV_LIGHT]);
2128                 of_property_read_s32(dn, "light_offset_val",
2129                                      &st->offset_val[MX_DEV_LIGHT]);
2130                 of_property_read_s32(dn, "light_offset_val2",
2131                                      &st->offset_val2[MX_DEV_LIGHT]);
2132                 of_property_read_u32(dn, "light_threshold_lo",
2133                                      &st->thr_lo[MX_DEV_LIGHT]);
2134                 of_property_read_u32(dn, "light_threshold_hi",
2135                                      &st->thr_hi[MX_DEV_LIGHT]);
2136                 /* common NVS temp programmable parameters */
2137                 of_property_read_s32(dn, "temp_max_range_val",
2138                                      &st->peak_val[MX_DEV_TEMP]);
2139                 of_property_read_s32(dn, "temp_max_range_val2",
2140                                      &st->peak_val2[MX_DEV_TEMP]);
2141                 of_property_read_s32(dn, "temp_resolution_val",
2142                                      &st->peak_scale_val[MX_DEV_TEMP]);
2143                 of_property_read_s32(dn, "temp_resolution_val2",
2144                                      &st->peak_scale_val2[MX_DEV_TEMP]);
2145                 of_property_read_s32(dn, "temp_scale_val",
2146                                      &st->scale_val[MX_DEV_TEMP]);
2147                 of_property_read_s32(dn, "temp_scale_val2",
2148                                      &st->scale_val2[MX_DEV_TEMP]);
2149                 of_property_read_s32(dn, "temp_offset_val",
2150                                      &st->offset_val[MX_DEV_TEMP]);
2151                 of_property_read_s32(dn, "temp_offset_val2",
2152                                      &st->offset_val2[MX_DEV_TEMP]);
2153                 of_property_read_u32(dn, "temp_threshold_lo",
2154                                      &st->thr_lo[MX_DEV_TEMP]);
2155                 of_property_read_u32(dn, "temp_threshold_hi",
2156                                      &st->thr_hi[MX_DEV_TEMP]);
2157                 /* common NVS proximity programmable parameters */
2158                 of_property_read_s32(dn, "proximity_uncalibrated_lo",
2159                                      &st->i_uc_lo[MX_DEV_PROX]);
2160                 of_property_read_s32(dn, "proximity_uncalibrated_hi",
2161                                      &st->i_uc_hi[MX_DEV_PROX]);
2162                 of_property_read_s32(dn, "proximity_calibrated_lo",
2163                                      &st->i_c_lo[MX_DEV_PROX]);
2164                 of_property_read_s32(dn, "proximity_calibrated_hi",
2165                                      &st->i_c_hi[MX_DEV_PROX]);
2166                 of_property_read_s32(dn, "proximity_max_range_val",
2167                                      &st->peak_val[MX_DEV_PROX]);
2168                 of_property_read_s32(dn, "proximity_max_range_val2",
2169                                      &st->peak_val2[MX_DEV_PROX]);
2170                 of_property_read_s32(dn, "proximity_resolution_val",
2171                                      &st->peak_scale_val[MX_DEV_PROX]);
2172                 of_property_read_s32(dn, "proximity_resolution_val2",
2173                                      &st->peak_scale_val2[MX_DEV_PROX]);
2174                 of_property_read_s32(dn, "proximity_scale_val",
2175                                      &st->scale_val[MX_DEV_PROX]);
2176                 of_property_read_s32(dn, "proximity_scale_val2",
2177                                      &st->scale_val2[MX_DEV_PROX]);
2178                 of_property_read_s32(dn, "proximity_offset_val",
2179                                      &st->offset_val[MX_DEV_PROX]);
2180                 of_property_read_s32(dn, "proximity_offset_val2",
2181                                      &st->offset_val2[MX_DEV_PROX]);
2182                 /* proximity threshold lo/hi are reversed */
2183                 of_property_read_u32(dn, "proximity_threshold_lo",
2184                                      &st->thr_hi[MX_DEV_PROX]);
2185                 of_property_read_u32(dn, "proximity_threshold_hi",
2186                                      &st->thr_lo[MX_DEV_PROX]);
2187                 of_property_read_u32(dn, "proximity_binary_threshold",
2188                                      &st->prx_thr_bin);
2189                 if (st->prx_thr_bin) {
2190                         /* proximity has binary threshold */
2191                         st->peak_val[MX_DEV_PROX] = 1;
2192                         st->peak_val2[MX_DEV_PROX] = 0;
2193                         st->peak_scale_val[MX_DEV_PROX] = 1;
2194                         st->peak_scale_val2[MX_DEV_PROX] = 0;
2195                         st->scale_val[MX_DEV_PROX] = 0;
2196                         st->scale_val2[MX_DEV_PROX] = 0;
2197                         st->offset_val[MX_DEV_PROX] = 0;
2198                         st->offset_val2[MX_DEV_PROX] = 0;
2199                 }
2200         }
2201         if (!st->report_n)
2202                 st->report_n = MX_REPORT_N;
2203         return 0;
2204 }
2205
2206 static int mx_probe(struct i2c_client *client, const struct i2c_device_id *id)
2207 {
2208         struct iio_dev *indio_dev;
2209         struct mx_state *st;
2210         unsigned long irqflags;
2211         unsigned int i;
2212         int ret;
2213
2214         dev_info(&client->dev, "%s\n", __func__);
2215         indio_dev = iio_device_alloc(sizeof(*st));
2216         if (indio_dev == NULL) {
2217                 dev_err(&client->dev, "%s iio_device_alloc ERR\n", __func__);
2218                 return -ENOMEM;
2219         }
2220
2221         st = iio_priv(indio_dev);
2222         st->i2c = client;
2223         i2c_set_clientdata(client, indio_dev);
2224         ret = mx_parse_dt(client, st);
2225         if (ret) {
2226                 ret = -ENODEV;
2227                 goto mx_probe_err;
2228         }
2229
2230         mx_pm_init(st);
2231         ret = mx_id_i2c(indio_dev, id->name);
2232         if (ret) {
2233                 ret = -ENODEV;
2234                 goto mx_probe_exit;
2235         }
2236
2237         mx_pm(st, false);
2238         indio_dev->buffer = iio_kfifo_allocate(indio_dev);
2239         if (!indio_dev->buffer) {
2240                 dev_err(&client->dev, "%s iio_kfifo_allocate ERR\n", __func__);
2241                 ret = -ENOMEM;
2242                 goto mx_probe_err;
2243         }
2244
2245         indio_dev->buffer->scan_timestamp = true;
2246         indio_dev->modes = INDIO_DIRECT_MODE;
2247         indio_dev->currentmode = INDIO_DIRECT_MODE;
2248         indio_dev->dev.parent = &client->dev;
2249         indio_dev->channels = mx_channels;
2250         if (st->dev_id != MX_DEVID_MAX44005) {
2251                 /* remove proximity IIO channel */
2252                 indio_dev->num_channels = (ARRAY_SIZE(mx_channels) - 1);
2253                 memcpy(&mx_channels[MX_SCAN_PROX],
2254                        &mx_channels[MX_SCAN_TIMESTAMP],
2255                        sizeof(struct iio_chan_spec));
2256                 /* remove proximity attributes */
2257                 for (i = 0; i < ARRAY_SIZE(mx_attrs); i++) {
2258                         if (mx_attrs[i] == &iio_dev_attr_proximity_part.
2259                                            dev_attr.attr) {
2260                                 mx_attrs[i] = NULL;
2261                                 break;
2262                         }
2263                 }
2264         } else {
2265                 indio_dev->num_channels = ARRAY_SIZE(mx_channels);
2266         }
2267         indio_dev->name = MX_NAME;
2268         indio_dev->info = &mx_iio_info;
2269         indio_dev->setup_ops = &mx_buffer_setup_ops;
2270         ret = iio_buffer_register(indio_dev, indio_dev->channels,
2271                                   indio_dev->num_channels);
2272         if (ret)
2273                 goto mx_probe_err;
2274
2275         INIT_DELAYED_WORK(&st->dw, mx_work);
2276         if (client->irq) {
2277                 irqflags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2278                 if (st->dev_id == MX_DEVID_MAX44005)
2279                         irqflags |= IRQF_NO_SUSPEND; /* for proximity */
2280                 ret = request_threaded_irq(client->irq, NULL, mx_irq_thread,
2281                                            irqflags, MX_NAME, st);
2282                 if (ret) {
2283                         dev_err(&client->dev, "%s req_threaded_irq ERR %d\n",
2284                                 __func__, ret);
2285                         ret = -ENOMEM;
2286                         goto mx_probe_err;
2287                 }
2288         }
2289
2290         st->trig = iio_trigger_alloc("%s-dev%d",
2291                                      indio_dev->name, indio_dev->id);
2292         if (st->trig == NULL) {
2293                 dev_err(&client->dev, "%s iio_allocate_trigger ERR\n",
2294                         __func__);
2295                 ret = -ENOMEM;
2296                 goto mx_probe_err;
2297         }
2298
2299         st->trig->dev.parent = &st->i2c->dev;
2300         st->trig->ops = &mx_trigger_ops;
2301         ret = iio_trigger_register(st->trig);
2302         if (ret) {
2303                 dev_err(&client->dev, "%s iio_trigger_register ERR\n",
2304                         __func__);
2305                 ret = -ENOMEM;
2306                 goto mx_probe_err;
2307         }
2308
2309         indio_dev->trig = st->trig;
2310         indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
2311         ret = iio_device_register(indio_dev);
2312         if (ret)
2313                 goto mx_probe_err;
2314
2315         dev_info(&client->dev, "%s done\n", __func__);
2316         return 0;
2317
2318 mx_probe_err:
2319         dev_err(&client->dev, "%s ERR %d\n", __func__, ret);
2320 mx_probe_exit:
2321         mx_remove(client);
2322         return ret;
2323 }
2324
2325 static const struct i2c_device_id mx_i2c_device_id[] = {
2326         { MX_NAME, 0 },
2327         { MX_NAME_MAX44005, 0 },
2328         { MX_NAME_MAX44006, 0 },
2329         { MX_NAME_MAX44008, 0 },
2330         {}
2331 };
2332
2333 MODULE_DEVICE_TABLE(i2c, mx_i2c_device_id);
2334
2335 static const struct of_device_id mx_of_match[] = {
2336         { .compatible = "maxim,max4400x", },
2337         { .compatible = "maxim,max44005", },
2338         { .compatible = "maxim,max44006", },
2339         { .compatible = "maxim,max44008", },
2340         {},
2341 };
2342
2343 MODULE_DEVICE_TABLE(of, mx_of_match);
2344
2345 static struct i2c_driver mx_driver = {
2346         .class          = I2C_CLASS_HWMON,
2347         .probe          = mx_probe,
2348         .remove         = mx_remove,
2349         .shutdown       = mx_shutdown,
2350         .driver = {
2351                 .name           = MX_NAME,
2352                 .owner          = THIS_MODULE,
2353                 .of_match_table = of_match_ptr(mx_of_match),
2354                 .pm             = &mx_pm_ops,
2355         },
2356         .id_table       = mx_i2c_device_id,
2357 };
2358 module_i2c_driver(mx_driver);
2359
2360 MODULE_LICENSE("GPL");
2361 MODULE_DESCRIPTION("MAX4400x driver");
2362 MODULE_AUTHOR("NVIDIA Corporation");