iio: sensors: NVS 0.8
[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               (103)
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_reset_sw(struct mx_state *st)
500 {
501         int ret;
502
503         ret = mx_i2c_wr(st, MX_REG_STS, (1 << MX_REG_STS_RESET));
504         if (!ret) {
505                 mdelay(MX_HW_DELAY_MS);
506                 st->rc_main_cfg = MX_REG_CFG_MAIN_POR;
507                 st->rc_amb_cfg = MX_REG_CFG_AMB_POR;
508         }
509         return ret;
510 }
511
512 static int mx_vreg_dis(struct mx_state *st, unsigned int i)
513 {
514         int ret = 0;
515
516         if (st->vreg[i].ret && (st->vreg[i].consumer != NULL)) {
517                 ret = regulator_disable(st->vreg[i].consumer);
518                 if (ret)
519                         dev_err(&st->i2c->dev, "%s %s ERR\n",
520                                 __func__, st->vreg[i].supply);
521                 else
522                         st->vreg[i].ret = 0;
523                         dev_dbg(&st->i2c->dev, "%s %s\n",
524                                 __func__, st->vreg[i].supply);
525         }
526         return ret;
527 }
528
529 static int mx_vreg_dis_all(struct mx_state *st)
530 {
531         unsigned int i;
532         int ret = 0;
533
534         for (i = ARRAY_SIZE(mx_vregs); i > 0; i--)
535                 ret |= mx_vreg_dis(st, (i - 1));
536         return ret;
537 }
538
539 static int mx_vreg_en(struct mx_state *st, unsigned int i)
540 {
541         int ret = 0;
542
543         if ((!st->vreg[i].ret) && (st->vreg[i].consumer != NULL)) {
544                 ret = regulator_enable(st->vreg[i].consumer);
545                 if (ret) {
546                         dev_err(&st->i2c->dev, "%s %s ERR\n",
547                                 __func__, st->vreg[i].supply);
548                 } else {
549                         st->vreg[i].ret = 1;
550                         dev_dbg(&st->i2c->dev, "%s %s\n",
551                                 __func__, st->vreg[i].supply);
552                         ret = 1; /* flag regulator state change */
553                 }
554         }
555         return ret;
556 }
557
558 static int mx_vreg_en_all(struct mx_state *st)
559 {
560         unsigned int i;
561         int ret = 0;
562
563         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++)
564                 ret |= mx_vreg_en(st, i);
565         return ret;
566 }
567
568 static void mx_vreg_exit(struct mx_state *st)
569 {
570         unsigned int i;
571
572         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
573                 if (st->vreg[i].consumer != NULL) {
574                         devm_regulator_put(st->vreg[i].consumer);
575                         st->vreg[i].consumer = NULL;
576                         dev_dbg(&st->i2c->dev, "%s %s\n",
577                                 __func__, st->vreg[i].supply);
578                 }
579         }
580 }
581
582 static int mx_vreg_init(struct mx_state *st)
583 {
584         unsigned int i;
585         int ret = 0;
586
587         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
588                 st->vreg[i].supply = mx_vregs[i];
589                 st->vreg[i].ret = 0;
590                 st->vreg[i].consumer = devm_regulator_get(&st->i2c->dev,
591                                                           st->vreg[i].supply);
592                 if (IS_ERR(st->vreg[i].consumer)) {
593                         ret |= PTR_ERR(st->vreg[i].consumer);
594                         dev_err(&st->i2c->dev, "%s ret %d for %s\n",
595                                 __func__, ret, st->vreg[i].supply);
596                         st->vreg[i].consumer = NULL;
597                 } else {
598                         dev_dbg(&st->i2c->dev, "%s %s\n",
599                                 __func__, st->vreg[i].supply);
600                 }
601         }
602         return ret;
603 }
604
605 static int mx_vreg_sts(struct mx_state *st)
606 {
607         unsigned int i;
608         int ret = 0;
609
610         for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
611                 if (st->vreg[i].consumer != NULL)
612                         break;
613         }
614         if (i < ARRAY_SIZE(mx_vregs)) {
615                 /* ret == number of regulators on */
616                 for (i = 0; i < ARRAY_SIZE(mx_vregs); i++) {
617                         if (st->vreg[i].ret)
618                                 ret++;
619                 }
620         } else {
621                 /* no regulator support (can assume always on) */
622                 ret = -EINVAL;
623         }
624         return ret;
625 }
626
627 static int mx_pm(struct mx_state *st, bool enable)
628 {
629         int ret = 0;
630
631         if (enable) {
632                 ret = mx_vreg_en_all(st);
633                 if (ret)
634                         mdelay(MX_HW_DELAY_MS);
635                 ret = mx_reset_sw(st);
636         } else {
637                 ret = mx_vreg_sts(st);
638                 if ((ret < 0) || (ret == ARRAY_SIZE(mx_vregs))) {
639                         ret = mx_i2c_wr(st, MX_REG_STS, 1 << MX_REG_STS_SHDN);
640                 } else if (ret > 0) {
641                         mx_vreg_en_all(st);
642                         mdelay(MX_HW_DELAY_MS);
643                         ret = mx_i2c_wr(st, MX_REG_STS, 1 << MX_REG_STS_SHDN);
644                 }
645                 ret |= mx_vreg_dis_all(st);
646         }
647         if (ret > 0)
648                 ret = 0;
649         if (ret) {
650                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
651                         __func__, enable, ret);
652         } else {
653                 if (st->dbg & MX_DBG_SPEW_MSG)
654                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
655                                  __func__, enable);
656         }
657         return ret;
658 }
659
660 static void mx_pm_exit(struct mx_state *st)
661 {
662         mx_pm(st, false);
663         mx_vreg_exit(st);
664 }
665
666 static int mx_pm_init(struct mx_state *st)
667 {
668         int ret;
669
670         st->enable = 0;
671         st->poll_delay_ms = MX_POLL_DLY_MS_DFLT;
672         mx_vreg_init(st);
673         ret = mx_pm(st, true);
674         return ret;
675 }
676
677 static unsigned int mx_buf_index(unsigned int size, unsigned int *bytes)
678 {
679         unsigned int index;
680
681         if (!(*bytes % size))
682                 index = *bytes;
683         else
684                 index = *bytes - *bytes % size + size;
685         *bytes = index + size;
686         return index;
687 }
688
689 static void mx_buf_push(struct iio_dev *indio_dev)
690 {
691         struct mx_state *st = iio_priv(indio_dev);
692         unsigned char buf[24];
693         unsigned int n;
694         unsigned int i;
695         unsigned int bytes = 0;
696
697         if (!iio_buffer_enabled(indio_dev))
698                 return;
699
700         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_LIGHT)) {
701                 n = sizeof(st->light);
702                 i = mx_buf_index(n, &bytes);
703                 memcpy(&buf[i], &st->light, n);
704                 if (st->dbg & MX_DBG_SPEW_LIGHT)
705                         dev_info(&st->i2c->dev, "light: %u %lld\n",
706                                  st->light, st->ts);
707         }
708         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_TEMP)) {
709                 n = sizeof(st->temp);
710                 i = mx_buf_index(n, &bytes);
711                 memcpy(&buf[i], &st->temp, n);
712                 if (st->dbg & MX_DBG_SPEW_TEMP)
713                         dev_info(&st->i2c->dev, "temp: %d %lld\n",
714                                  st->temp, st->ts);
715         }
716         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_PROX)) {
717                 n = sizeof(st->proximity);
718                 i = mx_buf_index(n, &bytes);
719                 memcpy(&buf[i], &st->proximity, n);
720                 if (st->dbg & MX_DBG_SPEW_PROX)
721                         dev_info(&st->i2c->dev, "proximity: %u %lld\n",
722                                  st->proximity, st->ts);
723         }
724         if (indio_dev->buffer->scan_timestamp) {
725                 n = sizeof(st->ts);
726                 i = mx_buf_index(n, &bytes);
727                 memcpy(&buf[i], &st->ts, n);
728         }
729         iio_push_to_buffers(indio_dev, buf);
730 }
731
732 static int mx_cmd_wr(struct mx_state *st, unsigned int enable, bool irq_en)
733 {
734         u8 amb_cfg = st->amb_cfg;
735         u8 main_cfg = 0;
736         int ret;
737         int ret_t = 0;
738
739         if (enable & (1 << MX_DEV_TEMP))
740                 amb_cfg |= (1 << MX_REG_CFG_AMB_TEMPEN);
741         if (amb_cfg != st->rc_amb_cfg) {
742                 ret = mx_i2c_wr(st, MX_REG_CFG_AMB, amb_cfg);
743                 if (ret)
744                         ret_t |= ret;
745                 else
746                         st->rc_amb_cfg = amb_cfg;
747                 if (st->dbg & MX_DBG_SPEW_MSG)
748                         dev_info(&st->i2c->dev, "%s amb_cfg=%hhx err=%d\n",
749                                  __func__, amb_cfg, ret);
750         }
751         main_cfg = mx_mode_tbl[enable];
752         if (irq_en && st->i2c->irq) {
753                 if (enable & (1 << MX_DEV_LIGHT))
754                         main_cfg |= (1 << MX_REG_CFG_MAIN_AMBINTE);
755                 if (enable & (1 << MX_DEV_PROX))
756                         main_cfg |= (1 << MX_REG_CFG_MAIN_PRXINTE);
757         }
758         if (main_cfg != st->rc_main_cfg) {
759                 ret = mx_i2c_wr(st, MX_REG_CFG_MAIN, main_cfg);
760                 if (ret)
761                         ret_t |= ret;
762                 else
763                         st->rc_main_cfg = main_cfg;
764                 if (st->dbg & MX_DBG_SPEW_MSG)
765                         dev_info(&st->i2c->dev, "%s main_cfg=%hhx err=%d\n",
766                                  __func__, main_cfg, ret);
767         }
768         if (st->rc_main_cfg & MX_REG_CFG_MAIN_INTE_MASK)
769                 ret_t = 1; /* flag IRQ enabled */
770         return ret_t;
771 }
772
773 static void mx_delay(struct mx_state *st)
774 {
775         unsigned int i;
776         unsigned int delay_us = ~0;
777
778         for (i = 0; i < MX_DEV_N; i++) {
779                 if (st->enable & (1 << i)) {
780                         if (st->delay_us[i] && (st->delay_us[i] < delay_us))
781                                 delay_us = st->delay_us[i];
782                 }
783         }
784         if (delay_us == ~0)
785                 delay_us = MX_POLL_DLY_MS_DFLT * 1000;
786         else if (delay_us < (MX_POLL_DLY_MS_MIN * 1000))
787                 delay_us = MX_POLL_DLY_MS_MIN * 1000;
788         st->poll_delay_ms = delay_us / 1000;
789 }
790
791 static int mx_interpolate(int x1, int x2, int x3, int y1, int *y2, int y3)
792 {
793         int dividend;
794         int divisor;
795
796         /* y2 = ((x2 - x1)(y3 - y1)/(x3 - x1)) + y1 */
797         divisor = (x3 - x1);
798         if (!divisor)
799                 return -EINVAL;
800
801         dividend = (x2 - x1) * (y3 - y1);
802         *y2 = (dividend / divisor) + y1;
803         return 0;
804 }
805
806 static void mx_report_init(struct mx_state *st)
807 {
808         st->ts = 0;
809         st->report = st->report_n;
810 }
811
812 static int mx_thr_wr(struct mx_state *st, u8 reg, u16 thr_lo, u16 thr_hi)
813 {
814         u8 buf[5];
815         u16 thr_be;
816         int ret = 0;
817
818         if (st->i2c->irq) {
819                 buf[0] = reg;
820                 thr_be = cpu_to_be16(thr_hi);
821                 buf[1] = thr_be & 0xFF;
822                 buf[2] = thr_be >> 8;
823                 thr_be = cpu_to_be16(thr_lo);
824                 buf[3] = thr_be & 0xFF;
825                 buf[4] = thr_be >> 8;
826                 ret = mx_i2c_write(st, sizeof(buf), buf);
827                 if (st->dbg & MX_DBG_SPEW_MSG)
828                         dev_info(&st->i2c->dev,
829                                  "%s reg=%hhx lo=%hd hi=%hd ret=%d\n",
830                                  __func__, reg, thr_lo, thr_hi, ret);
831         }
832         return ret;
833 }
834
835 static int mx_thr(struct mx_state *st, unsigned int dev, u8 reg,
836                   unsigned int lsb, int val_new, int *val_old, u16 hw_max)
837 {
838         u64 calc;
839         int thr_lo;
840         int thr_hi;
841         int ret = 1;
842
843         /* get calibrated value */
844         mx_interpolate(st->i_uc_lo[dev], val_new,
845                        st->i_uc_hi[dev],
846                        st->i_c_lo[dev], &val_new,
847                        st->i_c_hi[dev]);
848         if ((st->scale_val[dev] == 1) && (st->scale_val2[dev] == 0)) {
849                 /* calibration mode */
850                 st->report = 2; /* 2 causes polling after reporting */
851                 *val_old = val_new;
852                 return 0;
853         }
854
855         if (val_new > (*val_old + st->thr_hi[dev])) {
856                 ret = 0;
857         } else if (*val_old > st->thr_lo[dev]) {
858                 if (val_new < (*val_old - st->thr_lo[dev]))
859                         ret = 0;
860         }
861         /* if already reporting IIO buffer, might as well set new thresholds */
862         if (st->report || !ret) {
863                 if (!st->report)
864                         st->report = st->report_n;
865                 *val_old = val_new;
866                 if (st->i2c->irq && reg) {
867                         thr_lo = val_new - st->thr_lo[dev];
868                         /* get the uncalibrated value */
869                         mx_interpolate(st->i_c_lo[dev], thr_lo,
870                                        st->i_c_hi[dev],
871                                        st->i_uc_lo[dev], &thr_lo,
872                                        st->i_uc_hi[dev]);
873                         if (thr_lo < 0)
874                                 thr_lo = 0;
875                         /* convert to HW value */
876                         calc = thr_lo;
877                         if (st->scale_val2[dev])
878                                 calc *= st->scale_val2[dev];
879                         if (lsb)
880                                 do_div(calc, lsb);
881                         thr_lo = calc;
882                         thr_hi = val_new + st->thr_hi[dev];
883                         /* get the uncalibrated value */
884                         mx_interpolate(st->i_c_lo[dev], thr_hi,
885                                        st->i_c_hi[dev],
886                                        st->i_uc_lo[dev], &thr_hi,
887                                        st->i_uc_hi[dev]);
888                         /* convert to HW value */
889                         calc = thr_hi;
890                         if (st->scale_val2[dev])
891                                 calc *= st->scale_val2[dev];
892                         if (lsb)
893                                 do_div(calc, lsb);
894                         thr_hi = calc;
895                         if (thr_hi > hw_max)
896                                 thr_hi = hw_max;
897                         ret = mx_thr_wr(st, reg, thr_lo, thr_hi);
898                 }
899         }
900         return ret; /* ret == 0: report, ret < 0: report && poll (error) */
901 }
902
903 static int mx_rd_light(struct mx_state *st, s64 ts)
904 {
905         u64 calc;
906         u32 light;
907         u16 hw;
908         unsigned int lsb;
909         int i;
910         int ret;
911
912         ret = mx_i2c_read(st, MX_REG_DATA_AMB_CLEAR, 2, (u8 *)&hw);
913         if (ret)
914                 return ret;
915
916         hw = be16_to_cpu(hw);
917         i = st->rc_amb_cfg & MX_REG_CFG_AMB_AMBPGA_MASK;
918         if (st->dev_id == MX_DEVID_MAX44005)
919                 lsb = mx_ambpga_44005[i];
920         else
921                 lsb = mx_ambpga_44006[i];
922         calc = lsb;
923         calc *= hw;
924         if (st->scale_val2[MX_DEV_LIGHT])
925                 do_div(calc, st->scale_val2[MX_DEV_LIGHT]);
926         light = calc;
927         if (st->dbg & MX_DBG_SPEW_LIGHT_POLL)
928                 dev_info(&st->i2c->dev,
929                          "poll light %u %lld  diff=%d %lldns  hw=%hu\n",
930                          light, ts, light - st->light, ts - st->ts, hw);
931         i = st->rc_amb_cfg & MX_REG_CFG_AMB_AMBTIM_MASK;
932         i >>= MX_REG_CFG_AMB_AMBTIM;
933         ret = mx_thr(st, MX_DEV_LIGHT, MX_REG_AMB_UPTHR_H, lsb, light,
934                      &st->light, mx_ambtim_mask[i]);
935         return ret;
936 }
937
938 static int mx_rd_temp(struct mx_state *st, s64 ts)
939 {
940         s32 temp;
941         s16 hw;
942         int ret;
943
944         ret = mx_i2c_read(st, MX_REG_DATA_TEMP_H, 2, (u8 *)&hw);
945         if (ret)
946                 return ret;
947
948         hw = be16_to_cpu(hw);
949         temp = hw;
950         temp *= (MX_TEMP_RESOLUTION_MICRO / 10000);
951         if (st->dbg & MX_DBG_SPEW_TEMP_POLL)
952                 dev_info(&st->i2c->dev,
953                          "poll temp %d %lld  diff=%d %lldns  hw=%hd\n",
954                          temp, ts, temp - st->temp, ts - st->ts, hw);
955         ret = mx_thr(st, MX_DEV_TEMP, 0, 0, temp, &st->temp, 0);
956         return ret;
957 }
958
959 static int mx_rd_prox(struct mx_state *st, s64 ts)
960 {
961         u16 hw;
962         u16 hw_lo;
963         u16 hw_hi;
964         unsigned int proximity;
965         int ret = 0;
966
967         ret = mx_i2c_read(st, MX_REG_DATA_PROX_H, 2, (u8 *)&hw);
968         if (ret)
969                 return ret;
970
971         hw = be16_to_cpu(hw);
972         if (st->prx_cfg & (1 << MX_REG_CFG_PRX_PRXTIM))
973                 hw_hi = 0x00FF;
974         else
975                 hw_hi = 0x03FF;
976         if (st->prx_thr_bin) {
977                 /* proximity has binary threshold */
978                 if (st->dbg & MX_DBG_SPEW_PROX_POLL)
979                         dev_info(&st->i2c->dev,
980                                  "poll proximity %hu %lld  diff=%d %lldns\n",
981                                  hw, ts, hw - st->prox, ts - st->ts);
982                 if ((st->scale_val[MX_DEV_PROX] == 1) &&
983                                           (st->scale_val2[MX_DEV_PROX] == 0)) {
984                         st->report = 2; /* 2 causes polling after reporting */
985                         st->proximity = hw; /* display raw data */
986                 } else {
987                         /* the proximity threshold lo/hi are reversed */
988                         if (hw > st->prx_thr_bin) {
989                                 proximity = 0;
990                                 if (st->prx_thr_bin > st->thr_hi[MX_DEV_PROX])
991                                         hw_lo = st->prx_thr_bin -
992                                                        st->thr_hi[MX_DEV_PROX];
993                                 else
994                                         hw_lo = 1;
995                                 /* prox_hi already disabled */
996                         } else {
997                                 proximity = 1;
998                                 hw_lo = st->prx_thr_bin +
999                                                        st->thr_lo[MX_DEV_PROX];
1000                                 if (hw_lo < hw_hi)
1001                                         hw_hi = hw_lo;
1002                                 else
1003                                         hw_hi--;
1004                                 hw_lo = 0; /* disable */
1005                         }
1006                         if ((proximity != st->proximity) || st->report) {
1007                                 if (!st->report)
1008                                         st->report = st->report_n;
1009                                 st->proximity = proximity;
1010                                 st->prox = hw;
1011                                 ret = mx_thr_wr(st, MX_REG_PRX_UPTHR_H,
1012                                                 hw_lo, hw_hi);
1013                         }
1014                 }
1015         } else {
1016                 /* reverse the high/low order */
1017                 proximity = hw_hi - hw;
1018                 /* get calibrated value with reversed lo/hi */
1019                 mx_interpolate(st->i_uc_lo[MX_DEV_PROX], proximity,
1020                                st->i_uc_hi[MX_DEV_PROX],
1021                                st->i_c_lo[MX_DEV_PROX], &proximity,
1022                                st->i_c_hi[MX_DEV_PROX]);
1023                 if (st->dbg & MX_DBG_SPEW_PROX_POLL)
1024                         dev_info(&st->i2c->dev,
1025                                  "poll prox %u %lld  diff=%d %lldns  hw=%hu\n",
1026                                  proximity, ts,
1027                                  proximity - st->proximity, ts - st->ts, hw);
1028                 /* the proximity threshold lo/hi are reversed */
1029                 ret = mx_thr(st, MX_DEV_PROX, MX_REG_PRX_UPTHR_H, 0,
1030                              hw, &st->prox, hw_hi);
1031                 if (ret <= 0)
1032                         st->proximity = proximity;
1033         }
1034         return ret;
1035 }
1036
1037 static int mx_en(struct mx_state *st, unsigned int enable)
1038 {
1039         int ret;
1040
1041         mx_report_init(st);
1042         ret = mx_i2c_wr(st, MX_REG_CFG_THR, st->thr_cfg);
1043         if (enable & (1 << MX_DEV_PROX))
1044                 ret |= mx_i2c_wr(st, MX_REG_CFG_PRX, st->prx_cfg);
1045         ret |= mx_cmd_wr(st, enable, false);
1046         if (st->dbg & MX_DBG_SPEW_MSG) {
1047                 if (enable & (1 << MX_DEV_PROX))
1048                         dev_info(&st->i2c->dev,
1049                                  "%s thr_cfg=%hhx prx_cfg=%hhx err=%d\n",
1050                                  __func__, st->thr_cfg, st->prx_cfg, ret);
1051                 else
1052                         dev_info(&st->i2c->dev, "%s thr_cfg=%hhx err=%d\n",
1053                                  __func__, st->thr_cfg, ret);
1054         }
1055         return ret;
1056 }
1057
1058 static int mx_rd(struct iio_dev *indio_dev)
1059 {
1060         struct mx_state *st = iio_priv(indio_dev);
1061         u8 sts;
1062         s64 ts;
1063         int ret;
1064
1065         /* clear possible IRQ */
1066         ret = mx_i2c_rd(st, MX_REG_STS, &sts);
1067         if (ret)
1068                 return ret;
1069
1070         if (sts & (1 << MX_REG_STS_PWRON)) {
1071                 /* restart */
1072                 mx_en(st, st->enable);
1073                 return -1;
1074         }
1075
1076         ts = mx_get_time_ns(st);
1077         if (st->report < st->report_n) {
1078                 /* calculate elapsed time for allowed report rate */
1079                 if ((ts - st->ts) < st->poll_delay_ms * 1000000)
1080                         /* data changes are happening faster than allowed to
1081                          * report so we poll for the next data at an allowed
1082                          * rate with interrupts disabled.
1083                          */
1084                         return mx_cmd_wr(st, st->enable, false);
1085         }
1086         if ((st->enable & (1 << MX_DEV_PROX)) &&
1087                                                 (!(st->dbg & MX_DBG_VAL_PROX)))
1088                 ret |= mx_rd_prox(st, ts);
1089         if ((st->enable & (1 << MX_DEV_TEMP)) &&
1090                                                 (!(st->dbg & MX_DBG_VAL_TEMP)))
1091                 ret |= mx_rd_temp(st, ts);
1092         if ((st->enable & (1 << MX_DEV_LIGHT)) &&
1093                                                (!(st->dbg & MX_DBG_VAL_LIGHT)))
1094                 ret |= mx_rd_light(st, ts);
1095         if (st->report) {
1096                 if (st->report)
1097                         st->report--;
1098                 st->ts = ts;
1099                 mx_buf_push(indio_dev);
1100         }
1101         if (st->report || ret < 0)
1102                 /* poll if error or more reporting */
1103                 ret = mx_cmd_wr(st, st->enable, false);
1104         else
1105                 ret = mx_cmd_wr(st, st->enable, true);
1106         return ret;
1107 }
1108
1109 static void mx_read(struct iio_dev *indio_dev)
1110 {
1111         struct mx_state *st = iio_priv(indio_dev);
1112         int ret;
1113
1114         mutex_lock(&indio_dev->mlock);
1115         if (st->enable) {
1116                 ret = mx_rd(indio_dev);
1117                 if (ret < 1)
1118                         schedule_delayed_work(&st->dw,
1119                                           msecs_to_jiffies(st->poll_delay_ms));
1120         }
1121         mutex_unlock(&indio_dev->mlock);
1122 }
1123
1124 static void mx_work(struct work_struct *ws)
1125 {
1126         struct mx_state *st = container_of((struct delayed_work *)ws,
1127                                            struct mx_state, dw);
1128         struct iio_dev *indio_dev = iio_priv_to_dev(st);
1129
1130         mx_read(indio_dev);
1131 }
1132
1133 static irqreturn_t mx_irq_thread(int irq, void *dev_id)
1134 {
1135         struct mx_state *st = (struct mx_state *)dev_id;
1136         struct iio_dev *indio_dev = iio_priv_to_dev(st);
1137
1138         if (st->dbg & MX_DBG_IRQ)
1139                 dev_info(&st->i2c->dev, "%s\n", __func__);
1140         mx_read(indio_dev);
1141         return IRQ_HANDLED;
1142 }
1143
1144 static int mx_disable(struct iio_dev *indio_dev)
1145 {
1146         struct mx_state *st = iio_priv(indio_dev);
1147         int ret = 0;
1148
1149         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1150                                   MX_SCAN_LIGHT)))
1151                 st->dbg &= ~MX_DBG_VAL_LIGHT;
1152         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1153                                   MX_SCAN_TEMP)))
1154                 st->dbg &= ~MX_DBG_VAL_TEMP;
1155         if (!(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1156                                   MX_SCAN_PROX)))
1157                 st->dbg &= ~MX_DBG_VAL_PROX;
1158         cancel_delayed_work(&st->dw);
1159         if (st->enable & (1 << MX_DEV_PROX))
1160                 ret = mx_i2c_wr(st, MX_REG_CFG_PRX, 0);
1161         ret |= mx_pm(st, false);
1162         if (!ret)
1163                 st->enable = 0;
1164         return ret;
1165 }
1166
1167 static int mx_enable(struct iio_dev *indio_dev)
1168 {
1169         struct mx_state *st = iio_priv(indio_dev);
1170         unsigned int enable = 0;
1171         int ret = -EINVAL;
1172
1173         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_LIGHT))
1174                 enable |= (1 << MX_DEV_LIGHT);
1175         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_TEMP))
1176                 enable |= (1 << MX_DEV_TEMP);
1177         if (iio_scan_mask_query(indio_dev, indio_dev->buffer, MX_SCAN_PROX))
1178                 enable |= (1 << MX_DEV_PROX);
1179         if (enable) {
1180                 ret = mx_pm(st, true);
1181                 if (!ret) {
1182                         mx_delay(st);
1183                         ret = mx_en(st, enable);
1184                         if (ret < 0) {
1185                                 mx_disable(indio_dev);
1186                         } else {
1187                                 st->enable = enable;
1188                                 schedule_delayed_work(&st->dw,
1189                                          msecs_to_jiffies(MX_POLL_DLY_MS_MIN));
1190                         }
1191                 }
1192         }
1193         return ret;
1194 }
1195
1196 static ssize_t mx_attr_store(struct device *dev,
1197                              struct device_attribute *attr,
1198                              const char *buf, size_t count)
1199 {
1200         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1201         struct mx_state *st = iio_priv(indio_dev);
1202         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1203         const char *msg;
1204         unsigned int new;
1205         unsigned int old = 0;
1206         int ret;
1207
1208         ret = kstrtouint(buf, 10, &new);
1209         if (ret)
1210                 return -EINVAL;
1211
1212         mutex_lock(&indio_dev->mlock);
1213         if (st->shutdown || st->suspend) {
1214                 mutex_unlock(&indio_dev->mlock);
1215                 return -EPERM;
1216         }
1217
1218         switch (this_attr->address) {
1219         case MX_ATTR_ENABLE:
1220                 msg = "ATTR_ENABLE";
1221                 if (st->enable)
1222                         old = 1;
1223                 if (new)
1224                         ret = mx_enable(indio_dev);
1225                 else
1226                         ret = mx_disable(indio_dev);
1227                 break;
1228
1229         case MX_ATTR_LIGHT_THRESH_LO:
1230                 msg = "ATTR_THRESH_LO";
1231                 old = st->thr_lo[MX_DEV_LIGHT];
1232                 st->thr_lo[MX_DEV_LIGHT] = new;
1233                 break;
1234
1235         case MX_ATTR_LIGHT_THRESH_HI:
1236                 msg = "ATTR_LIGHT_THRESH_HI";
1237                 old = st->thr_hi[MX_DEV_LIGHT];
1238                 st->thr_hi[MX_DEV_LIGHT] = new;
1239                 break;
1240
1241         case MX_ATTR_TEMP_THRESH_LO:
1242                 msg = "ATTR_TEMP_THRESH_LO";
1243                 old = st->thr_lo[MX_DEV_TEMP];
1244                 st->thr_lo[MX_DEV_TEMP] = new;
1245                 break;
1246
1247         case MX_ATTR_TEMP_THRESH_HI:
1248                 msg = "ATTR_TEMP_THRESH_HI";
1249                 old = st->thr_hi[MX_DEV_TEMP];
1250                 st->thr_hi[MX_DEV_TEMP] = new;
1251                 break;
1252
1253         case MX_ATTR_PROX_THRESH_LO:
1254                 msg = "ATTR_PROX_THRESH_LO";
1255                 /* proximity threshold lo/hi are reversed */
1256                 old = st->thr_hi[MX_DEV_PROX];
1257                 st->thr_hi[MX_DEV_PROX] = new;
1258                 break;
1259
1260         case MX_ATTR_PROX_THRESH_HI:
1261                 msg = "ATTR_PROX_THRESH_HI";
1262                 /* proximity threshold lo/hi are reversed */
1263                 old = st->thr_lo[MX_DEV_PROX];
1264                 st->thr_lo[MX_DEV_PROX] = new;
1265                 break;
1266
1267         default:
1268                 msg = "ATTR_UNKNOWN";
1269                 ret = -EINVAL;
1270         }
1271
1272         mx_report_init(st);
1273         mutex_unlock(&indio_dev->mlock);
1274         mx_read(indio_dev);
1275         if (st->dbg & MX_DBG_SPEW_MSG) {
1276                 if (ret)
1277                         dev_err(&st->i2c->dev, "%s %s %d->%d ERR=%d\n",
1278                                 __func__, msg, old, new, ret);
1279                 else
1280                         dev_info(&st->i2c->dev, "%s %s %d->%d\n",
1281                                  __func__, msg, old, new);
1282         }
1283         if (ret)
1284                 return ret;
1285
1286         return count;
1287 }
1288
1289 static ssize_t mx_attr_show(struct device *dev, struct device_attribute *attr,
1290                              char *buf)
1291 {
1292         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1293         struct mx_state *st = iio_priv(indio_dev);
1294         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1295
1296         switch (this_attr->address) {
1297         case MX_ATTR_ENABLE:
1298                 return sprintf(buf, "%x\n", st->enable);
1299
1300         case MX_ATTR_VENDOR:
1301                 return sprintf(buf, "%s\n", MX_VENDOR);
1302
1303         case MX_ATTR_LIGHT_PART:
1304                 return sprintf(buf, "%s light\n", st->part);
1305
1306         case MX_ATTR_LIGHT_VERSION:
1307                 return sprintf(buf, "%u\n", MX_LIGHT_VERSION);
1308
1309         case MX_ATTR_LIGHT_MILLIAMP:
1310                 return sprintf(buf, "%s\n", MX_LIGHT_MILLIAMP);
1311
1312         case MX_ATTR_LIGHT_THRESH_LO:
1313                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_LIGHT]);
1314
1315         case MX_ATTR_LIGHT_THRESH_HI:
1316                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_LIGHT]);
1317
1318         case MX_ATTR_TEMP_PART:
1319                 return sprintf(buf, "%s temperature\n", st->part);
1320
1321         case MX_ATTR_TEMP_VERSION:
1322                 return sprintf(buf, "%u\n", MX_TEMP_VERSION);
1323
1324         case MX_ATTR_TEMP_MILLIAMP:
1325                 return sprintf(buf, "%s\n", MX_TEMP_MILLIAMP);
1326
1327         case MX_ATTR_TEMP_THRESH_LO:
1328                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_TEMP]);
1329
1330         case MX_ATTR_TEMP_THRESH_HI:
1331                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_TEMP]);
1332
1333         case MX_ATTR_PROX_PART:
1334                 return sprintf(buf, "%s proximity\n", st->part);
1335
1336         case MX_ATTR_PROX_VERSION:
1337                 return sprintf(buf, "%u\n", MX_PROX_VERSION);
1338
1339         case MX_ATTR_PROX_MILLIAMP:
1340                 return sprintf(buf, "%s\n", MX_PROX_MILLIAMP);
1341
1342         case MX_ATTR_PROX_THRESH_LO:
1343                 /* proximity threshold lo/hi are reversed */
1344                 return sprintf(buf, "%u\n", st->thr_hi[MX_DEV_PROX]);
1345
1346         case MX_ATTR_PROX_THRESH_HI:
1347                 /* proximity threshold lo/hi are reversed */
1348                 return sprintf(buf, "%u\n", st->thr_lo[MX_DEV_PROX]);
1349
1350         default:
1351                 return -EINVAL;
1352         }
1353
1354         return -EINVAL;
1355 }
1356
1357 static ssize_t mx_data_store(struct device *dev,
1358                              struct device_attribute *attr,
1359                              const char *buf, size_t count)
1360 {
1361         struct mx_state *st = iio_priv(dev_get_drvdata(dev));
1362         unsigned int info;
1363         int ret;
1364
1365         ret = kstrtouint(buf, 10, &info);
1366         if (ret)
1367                 return -EINVAL;
1368
1369         if (info >= MX_INFO_LIMIT_MAX)
1370                 return -EINVAL;
1371
1372         st->info = info;
1373         switch (info) {
1374         case MX_INFO_DATA:
1375                 st->dbg = 0;
1376                 break;
1377
1378         case MX_INFO_DBG:
1379                 st->dbg ^= MX_DBG_SPEW_MSG;
1380                 break;
1381
1382         case MX_INFO_LIGHT_SPEW:
1383                 st->dbg ^= MX_DBG_SPEW_LIGHT;
1384                 break;
1385
1386         case MX_INFO_LIGHT_POLL_SPEW:
1387                 st->dbg ^= MX_DBG_SPEW_LIGHT_POLL;
1388                 break;
1389
1390         case MX_INFO_TEMP_SPEW:
1391                 st->dbg ^= MX_DBG_SPEW_TEMP;
1392                 break;
1393
1394         case MX_INFO_TEMP_POLL_SPEW:
1395                 st->dbg ^= MX_DBG_SPEW_TEMP_POLL;
1396                 break;
1397
1398         case MX_INFO_PROXIMITY_SPEW:
1399                 st->dbg ^= MX_DBG_SPEW_PROX;
1400                 break;
1401
1402         case MX_INFO_PROXIMITY_POLL_SPEW:
1403                 st->dbg ^= MX_DBG_SPEW_PROX_POLL;
1404                 break;
1405
1406         case MX_INFO_DBG_IRQ:
1407                 st->dbg ^= MX_DBG_IRQ;
1408                 break;
1409
1410         default:
1411                 break;
1412         }
1413
1414         return count;
1415 }
1416
1417 static ssize_t mx_data_show(struct device *dev,
1418                             struct device_attribute *attr, char *buf)
1419 {
1420         struct mx_state *st = iio_priv(dev_get_drvdata(dev));
1421         enum MX_INFO info;
1422         ssize_t t;
1423         u8 val[2];
1424         u8 i;
1425         int ret;
1426
1427         info = st->info;
1428         st->info = MX_INFO_DATA;
1429         switch (info) {
1430         case MX_INFO_DATA:
1431                 t = sprintf(buf, "light: %u\n", st->light);
1432                 t += sprintf(buf + t, "temp: %d\n", st->temp);
1433                 if (st->dev_id == MX_DEVID_MAX44005)
1434                         t += sprintf(buf + t, "proximity: %u\n",
1435                                      st->proximity);
1436                 t += sprintf(buf + t, "timestamp: %lld\n", st->ts);
1437                 return t;
1438
1439         case MX_INFO_VER:
1440                 return sprintf(buf, "version=%u\n", MX_VERSION_DRIVER);
1441
1442         case MX_INFO_ERRS:
1443                 t = sprintf(buf, "error count=%u\n", st->errs);
1444                 st->errs = 0;
1445                 return t;
1446
1447         case MX_INFO_REGS:
1448                 t = sprintf(buf, "registers:\n");
1449                 for (i = 0; i <= MX_REG_CFG_PRX; i++) {
1450                         ret = mx_i2c_rd(st, i, val);
1451                         if (!ret)
1452                                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1453                                              i, val[0]);
1454                 }
1455                 for (i = MX_REG_DATA_AMB_CLEAR; i < MX_REG_CFG_THR; 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                 ret = mx_i2c_rd(st, MX_REG_CFG_THR, val);
1462                 if (!ret)
1463                         t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1464                                      MX_REG_CFG_THR, val[0]);
1465                 for (i = MX_REG_PRX_UPTHR_H; i < MX_REG_TRIM_CLEAR; i += 2) {
1466                         ret = mx_i2c_read(st, i, 2, val);
1467                         if (!ret)
1468                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
1469                                         i, i + 1, be16_to_cpup((__be16 *)val));
1470                 }
1471                 for (i = MX_REG_TRIM_CLEAR; i <= MX_REG_TRIM_IR; i++) {
1472                         ret = mx_i2c_rd(st, i, val);
1473                         if (!ret)
1474                                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
1475                                              i, val[0]);
1476                 }
1477                 return t;
1478
1479         case MX_INFO_DBG:
1480                 return sprintf(buf, "debug spew=%x\n",
1481                                st->dbg & MX_DBG_SPEW_MSG);
1482
1483         case MX_INFO_LIGHT_SPEW:
1484                 return sprintf(buf, "light_ts spew=%x\n",
1485                                !!(st->dbg & MX_DBG_SPEW_LIGHT));
1486
1487         case MX_INFO_LIGHT_POLL_SPEW:
1488                 return sprintf(buf, "light_poll_ts spew=%x\n",
1489                                !!(st->dbg & MX_DBG_SPEW_LIGHT_POLL));
1490
1491         case MX_INFO_TEMP_SPEW:
1492                 return sprintf(buf, "temp_ts spew=%x\n",
1493                                !!(st->dbg & MX_DBG_SPEW_TEMP));
1494
1495         case MX_INFO_TEMP_POLL_SPEW:
1496                 return sprintf(buf, "temp_poll_ts spew=%x\n",
1497                                !!(st->dbg & MX_DBG_SPEW_TEMP_POLL));
1498
1499         case MX_INFO_PROXIMITY_SPEW:
1500                 return sprintf(buf, "proximity_ts spew=%x\n",
1501                                !!(st->dbg & MX_DBG_SPEW_PROX));
1502
1503         case MX_INFO_PROXIMITY_POLL_SPEW:
1504                 return sprintf(buf, "proximity_poll_ts spew=%x\n",
1505                                !!(st->dbg & MX_DBG_SPEW_PROX_POLL));
1506
1507         case MX_INFO_DBG_IRQ:
1508                 return sprintf(buf, "debug IRQ=%x\n",
1509                                !!(st->dbg & MX_DBG_IRQ));
1510
1511         default:
1512                 break;
1513         }
1514
1515         return -EINVAL;
1516 }
1517
1518 static IIO_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1519                        mx_attr_show, mx_attr_store, MX_ATTR_ENABLE);
1520 static IIO_DEVICE_ATTR(illuminance_part, S_IRUGO,
1521                        mx_attr_show, NULL, MX_ATTR_LIGHT_PART);
1522 static IIO_DEVICE_ATTR(illuminance_vendor, S_IRUGO,
1523                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1524 static IIO_DEVICE_ATTR(illuminance_version, S_IRUGO,
1525                        mx_attr_show, NULL, MX_ATTR_LIGHT_VERSION);
1526 static IIO_DEVICE_ATTR(illuminance_milliamp, S_IRUGO,
1527                        mx_attr_show, NULL, MX_ATTR_LIGHT_MILLIAMP);
1528 static IIO_DEVICE_ATTR(illuminance_thresh_rising_value,
1529                        S_IRUGO | S_IWUSR | S_IWGRP,
1530                        mx_attr_show, mx_attr_store, MX_ATTR_LIGHT_THRESH_HI);
1531 static IIO_DEVICE_ATTR(illuminance_thresh_falling_value,
1532                        S_IRUGO | S_IWUSR | S_IWGRP,
1533                        mx_attr_show, mx_attr_store, MX_ATTR_LIGHT_THRESH_LO);
1534 static IIO_DEVICE_ATTR(temp_part, S_IRUGO,
1535                        mx_attr_show, NULL, MX_ATTR_TEMP_PART);
1536 static IIO_DEVICE_ATTR(temp_vendor, S_IRUGO,
1537                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1538 static IIO_DEVICE_ATTR(temp_version, S_IRUGO,
1539                        mx_attr_show, NULL, MX_ATTR_TEMP_VERSION);
1540 static IIO_DEVICE_ATTR(temp_milliamp, S_IRUGO,
1541                        mx_attr_show, NULL, MX_ATTR_TEMP_MILLIAMP);
1542 static IIO_DEVICE_ATTR(temp_thresh_rising_value,
1543                        S_IRUGO | S_IWUSR | S_IWGRP,
1544                        mx_attr_show, mx_attr_store, MX_ATTR_TEMP_THRESH_HI);
1545 static IIO_DEVICE_ATTR(temp_thresh_falling_value,
1546                        S_IRUGO | S_IWUSR | S_IWGRP,
1547                        mx_attr_show, mx_attr_store, MX_ATTR_TEMP_THRESH_LO);
1548 static IIO_DEVICE_ATTR(proximity_part, S_IRUGO,
1549                        mx_attr_show, NULL, MX_ATTR_PROX_PART);
1550 static IIO_DEVICE_ATTR(proximity_vendor, S_IRUGO,
1551                        mx_attr_show, NULL, MX_ATTR_VENDOR);
1552 static IIO_DEVICE_ATTR(proximity_version, S_IRUGO,
1553                        mx_attr_show, NULL, MX_ATTR_PROX_VERSION);
1554 static IIO_DEVICE_ATTR(proximity_milliamp, S_IRUGO,
1555                        mx_attr_show, NULL, MX_ATTR_PROX_MILLIAMP);
1556 static IIO_DEVICE_ATTR(proximity_thresh_rising_value,
1557                        S_IRUGO | S_IWUSR | S_IWGRP,
1558                        mx_attr_show, mx_attr_store, MX_ATTR_PROX_THRESH_HI);
1559 static IIO_DEVICE_ATTR(proximity_thresh_falling_value,
1560                        S_IRUGO | S_IWUSR | S_IWGRP,
1561                        mx_attr_show, mx_attr_store, MX_ATTR_PROX_THRESH_LO);
1562 static DEVICE_ATTR(data, S_IRUGO | S_IWUSR | S_IWGRP,
1563                    mx_data_show, mx_data_store);
1564
1565 static struct attribute *mx_attrs[] = {
1566         &dev_attr_data.attr,
1567         &iio_dev_attr_enable.dev_attr.attr,
1568         &iio_dev_attr_illuminance_part.dev_attr.attr,
1569         &iio_dev_attr_illuminance_vendor.dev_attr.attr,
1570         &iio_dev_attr_illuminance_version.dev_attr.attr,
1571         &iio_dev_attr_illuminance_milliamp.dev_attr.attr,
1572         &iio_dev_attr_illuminance_thresh_rising_value.dev_attr.attr,
1573         &iio_dev_attr_illuminance_thresh_falling_value.dev_attr.attr,
1574         &iio_dev_attr_temp_part.dev_attr.attr,
1575         &iio_dev_attr_temp_vendor.dev_attr.attr,
1576         &iio_dev_attr_temp_version.dev_attr.attr,
1577         &iio_dev_attr_temp_milliamp.dev_attr.attr,
1578         &iio_dev_attr_temp_thresh_rising_value.dev_attr.attr,
1579         &iio_dev_attr_temp_thresh_falling_value.dev_attr.attr,
1580         /* proximity_part must be the first proximity attribute */
1581         &iio_dev_attr_proximity_part.dev_attr.attr,
1582         &iio_dev_attr_proximity_vendor.dev_attr.attr,
1583         &iio_dev_attr_proximity_version.dev_attr.attr,
1584         &iio_dev_attr_proximity_milliamp.dev_attr.attr,
1585         &iio_dev_attr_proximity_thresh_rising_value.dev_attr.attr,
1586         &iio_dev_attr_proximity_thresh_falling_value.dev_attr.attr,
1587         NULL
1588 };
1589
1590 static struct attribute_group mx_attr_group = {
1591         .name = MX_NAME,
1592         .attrs = mx_attrs
1593 };
1594
1595 static int mx_read_raw(struct iio_dev *indio_dev,
1596                        struct iio_chan_spec const *chan,
1597                        int *val, int *val2, long mask)
1598 {
1599         struct mx_state *st = iio_priv(indio_dev);
1600         int i = chan->scan_index;
1601         int ret = -EINVAL;
1602
1603         switch (mask) {
1604         case IIO_CHAN_INFO_RAW:
1605                 switch (chan->type) {
1606                 case IIO_LIGHT:
1607                         *val = st->light;
1608                         return IIO_VAL_INT;
1609
1610                 case IIO_TEMP:
1611                         *val = st->temp;
1612                         return IIO_VAL_INT;
1613
1614                 case IIO_PROXIMITY:
1615                         *val = st->proximity;
1616                         return IIO_VAL_INT;
1617
1618                 default:
1619                         return -EINVAL;
1620                 }
1621
1622         case IIO_CHAN_INFO_SAMP_FREQ:
1623                 if (st->enable & (1 << i))
1624                         *val = st->delay_us[i];
1625                 else
1626                         *val = MX_POLL_DLY_MS_MIN * 1000;
1627                 return IIO_VAL_INT;
1628
1629         case IIO_CHAN_INFO_SCALE:
1630                 *val = st->scale_val[i];
1631                 *val2 = st->scale_val2[i];
1632                 return IIO_VAL_INT_PLUS_MICRO;
1633
1634         case IIO_CHAN_INFO_OFFSET:
1635                 *val = st->offset_val[i];
1636                 *val2 = st->offset_val2[i];
1637                 return IIO_VAL_INT_PLUS_MICRO;
1638
1639         case IIO_CHAN_INFO_PEAK:
1640                 *val = st->peak_val[i];
1641                 *val2 = st->peak_val2[i];
1642                 return IIO_VAL_INT_PLUS_MICRO;
1643
1644         case IIO_CHAN_INFO_PEAK_SCALE:
1645                 *val = st->peak_scale_val[i];
1646                 *val2 = st->peak_scale_val2[i];
1647                 return IIO_VAL_INT_PLUS_MICRO;
1648
1649         default:
1650                 return -EINVAL;
1651         }
1652
1653         return ret;
1654 }
1655
1656 static int mx_write_raw(struct iio_dev *indio_dev,
1657                         struct iio_chan_spec const *chan,
1658                         int val, int val2, long mask)
1659 {
1660         struct mx_state *st = iio_priv(indio_dev);
1661         char *msg;
1662         int i = chan->scan_index;
1663         int old = 0;
1664         int old2 = 0;
1665         int ret = 0;
1666
1667         mutex_lock(&indio_dev->mlock);
1668         if (st->shutdown || st->suspend) {
1669                 mutex_unlock(&indio_dev->mlock);
1670                 return -EPERM;
1671         }
1672
1673         switch (mask) {
1674         case IIO_CHAN_INFO_SAMP_FREQ:
1675                 msg = "IIO_CHAN_INFO_SAMP_FREQ";
1676                 old = st->delay_us[i];
1677                 st->delay_us[i] = val;
1678                 mx_delay(st);
1679                 break;
1680
1681         case IIO_CHAN_INFO_SCALE:
1682                 msg = "IIO_CHAN_INFO_SCALE";
1683                 old = st->scale_val[i];
1684                 old2 = st->scale_val2[i];
1685                 st->scale_val[i] = val;
1686                 st->scale_val2[i] = val2;
1687                 break;
1688
1689         case IIO_CHAN_INFO_OFFSET:
1690                 msg = "IIO_CHAN_INFO_SCALE";
1691                 old = st->offset_val[i];
1692                 old2 = st->offset_val2[i];
1693                 st->offset_val[i] = val;
1694                 st->offset_val2[i] = val2;
1695                 break;
1696
1697         case IIO_CHAN_INFO_PEAK:
1698                 msg = "IIO_CHAN_INFO_PEAK";
1699                 old = st->peak_val[i];
1700                 old2 = st->peak_val2[i];
1701                 st->peak_val[i] = val;
1702                 st->peak_val2[i] = val2;
1703                 break;
1704
1705         case IIO_CHAN_INFO_PEAK_SCALE:
1706                 msg = "IIO_CHAN_INFO_PEAK_SCALE";
1707                 old = st->peak_scale_val[i];
1708                 old2 = st->peak_scale_val2[i];
1709                 st->peak_scale_val[i] = val;
1710                 st->peak_scale_val2[i] = val2;
1711                 break;
1712
1713         case IIO_CHAN_INFO_RAW:
1714                 msg = "IIO_CHAN_INFO_RAW";
1715                 switch (chan->type) {
1716                 case IIO_LIGHT:
1717                         old = st->light;
1718                         st->light = val;
1719                         st->dbg |= MX_DBG_VAL_LIGHT;
1720                         if ((st->enable & (1 << MX_DEV_LIGHT)) && !st->report)
1721                                 st->report = 1;
1722                         break;
1723
1724                 case IIO_TEMP:
1725                         old = st->temp;
1726                         st->temp = val;
1727                         st->dbg |= MX_DBG_VAL_TEMP;
1728                         if ((st->enable & (1 << MX_DEV_TEMP)) && !st->report)
1729                                 st->report = 1;
1730                         break;
1731
1732                 case IIO_PROXIMITY:
1733                         old = st->proximity;
1734                         st->proximity = val;
1735                         st->dbg |= MX_DBG_VAL_PROX;
1736                         if ((st->enable & (1 << MX_DEV_PROX)) && !st->report)
1737                                 st->report = 1;
1738                         break;
1739
1740                 default:
1741                         ret = -EINVAL;
1742                 }
1743
1744                 break;
1745
1746         default:
1747                 msg = "IIO_CHAN_INFO_UNKNOWN";
1748                 ret = -EINVAL;
1749         }
1750
1751         mx_report_init(st);
1752         mutex_unlock(&indio_dev->mlock);
1753         mx_read(indio_dev);
1754         if (st->dbg & MX_DBG_SPEW_MSG) {
1755                 if (ret)
1756                         dev_err(&st->i2c->dev, "%s c=%d %d:%d->%d:%d ERR=%d\n",
1757                                 msg, chan->scan_index,
1758                                 old, old2, val, val2, ret);
1759                 else
1760                         dev_info(&st->i2c->dev, "%s %s chan=%d %d:%d->%d:%d\n",
1761                                  __func__, msg, chan->scan_index,
1762                                  old, old2, val, val2);
1763         }
1764         return ret;
1765 }
1766
1767 static const struct iio_info mx_iio_info = {
1768         .driver_module = THIS_MODULE,
1769         .attrs = &mx_attr_group,
1770         .read_raw = &mx_read_raw,
1771         .write_raw = &mx_write_raw,
1772 };
1773
1774 static struct iio_chan_spec mx_channels[] = {
1775         {
1776                 .type                   = IIO_LIGHT,
1777                 .scan_index             = MX_SCAN_LIGHT,
1778                 .scan_type              = IIO_ST('u', 32, 32, 0),
1779                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1780                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1781                                           BIT(IIO_CHAN_INFO_PEAK) |
1782                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1783                                           BIT(IIO_CHAN_INFO_SCALE) |
1784                                           BIT(IIO_CHAN_INFO_OFFSET),
1785                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1786                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1787                                           BIT(IIO_CHAN_INFO_PEAK) |
1788                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1789                                           BIT(IIO_CHAN_INFO_SCALE) |
1790                                           BIT(IIO_CHAN_INFO_OFFSET),
1791         },
1792         {
1793                 .type                   = IIO_TEMP,
1794                 .scan_index             = MX_SCAN_TEMP,
1795                 .scan_type              = IIO_ST('s', 32, 32, 0),
1796                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1797                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1798                                           BIT(IIO_CHAN_INFO_PEAK) |
1799                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1800                                           BIT(IIO_CHAN_INFO_SCALE) |
1801                                           BIT(IIO_CHAN_INFO_OFFSET),
1802                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1803                                           BIT(IIO_CHAN_INFO_PEAK) |
1804                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1805                                           BIT(IIO_CHAN_INFO_SCALE) |
1806                                           BIT(IIO_CHAN_INFO_OFFSET),
1807                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1808         },
1809         {
1810                 .type                   = IIO_PROXIMITY,
1811                 .scan_index             = MX_SCAN_PROX,
1812                 .scan_type              = IIO_ST('u', 32, 32, 0),
1813                 .info_mask              = BIT(IIO_CHAN_INFO_RAW) |
1814                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1815                                           BIT(IIO_CHAN_INFO_PEAK) |
1816                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1817                                           BIT(IIO_CHAN_INFO_SCALE) |
1818                                           BIT(IIO_CHAN_INFO_OFFSET),
1819                 .info_mask_separate     = BIT(IIO_CHAN_INFO_RAW) |
1820                                           BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1821                                           BIT(IIO_CHAN_INFO_PEAK) |
1822                                           BIT(IIO_CHAN_INFO_PEAK_SCALE) |
1823                                           BIT(IIO_CHAN_INFO_SCALE) |
1824                                           BIT(IIO_CHAN_INFO_OFFSET),
1825         },
1826         IIO_CHAN_SOFT_TIMESTAMP(MX_SCAN_TIMESTAMP)
1827 };
1828
1829 static int mx_buffer_preenable(struct iio_dev *indio_dev)
1830 {
1831         struct mx_state *st = iio_priv(indio_dev);
1832
1833         if (st->shutdown || st->suspend)
1834                 return -EINVAL;
1835
1836         return iio_sw_buffer_preenable(indio_dev);
1837 }
1838
1839 static int mx_buffer_postenable(struct iio_dev *indio_dev)
1840 {
1841         int ret;
1842
1843         ret = mx_enable(indio_dev);
1844         /* never return > 0 to IIO buffer engine */
1845         if (ret > 0)
1846                 ret = 0;
1847         return ret;
1848 }
1849
1850 static const struct iio_buffer_setup_ops mx_buffer_setup_ops = {
1851         /* iio_sw_buffer_preenable:
1852          * Generic function for equal sized ring elements + 64 bit timestamp
1853          * Assumes that any combination of channels can be enabled.
1854          * Typically replaced to implement restrictions on what combinations
1855          * can be captured (hardware scan modes).
1856          */
1857         .preenable = &mx_buffer_preenable,
1858         /* iio_triggered_buffer_postenable:
1859          * Generic function that simply attaches the pollfunc to the trigger.
1860          * Replace this to mess with hardware state before we attach the
1861          * trigger.
1862          */
1863         .postenable = &mx_buffer_postenable,
1864         /* this driver relies on the NVS HAL to power off this device with the
1865          * master enable.
1866          *.predisable = N/A
1867          *.postdisable = N/A
1868          */
1869 };
1870
1871 static const struct iio_trigger_ops mx_trigger_ops = {
1872         .owner = THIS_MODULE,
1873 };
1874
1875 static int mx_suspend(struct device *dev)
1876 {
1877         struct i2c_client *client = to_i2c_client(dev);
1878         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1879         struct mx_state *st = iio_priv(indio_dev);
1880         int ret = 0;
1881
1882         mutex_lock(&indio_dev->mlock);
1883         if (st->enable && !(iio_scan_mask_query(indio_dev, indio_dev->buffer,
1884                                                 MX_SCAN_PROX))) {
1885                 ret = mx_disable(indio_dev);
1886                 st->suspend = true;
1887         }
1888         mutex_unlock(&indio_dev->mlock);
1889         if (st->dbg & MX_DBG_SPEW_MSG)
1890                 dev_info(&client->dev, "%s\n", __func__);
1891         return ret;
1892 }
1893
1894 static int mx_resume(struct device *dev)
1895 {
1896         struct i2c_client *client = to_i2c_client(dev);
1897         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1898         struct mx_state *st = iio_priv(indio_dev);
1899
1900         st->suspend = false;
1901         if (st->dbg & MX_DBG_SPEW_MSG)
1902                 dev_info(&client->dev, "%s\n", __func__);
1903         return 0;
1904 }
1905
1906 static SIMPLE_DEV_PM_OPS(mx_pm_ops, mx_suspend, mx_resume);
1907
1908 static int mx_id_dev(struct iio_dev *indio_dev, const char *name)
1909 {
1910         struct mx_state *st = iio_priv(indio_dev);
1911         u8 val;
1912         int ret = 0;
1913
1914         if (!strcmp(name, MX_NAME_MAX44008)) {
1915                 st->dev_id = MX_DEVID_MAX44008;
1916                 st->part = MX_NAME_MAX44008;
1917         } else if (!strcmp(name, MX_NAME_MAX44006)) {
1918                 st->dev_id = MX_DEVID_MAX44006;
1919                 st->part = MX_NAME_MAX44006;
1920         } else if (!strcmp(name, MX_NAME_MAX44005)) {
1921                 st->dev_id = MX_DEVID_MAX44005;
1922                 st->part = MX_NAME_MAX44005;
1923         } else if (!strcmp(name, MX_NAME)) {
1924                 /* There is no way to auto-detect the device since the
1925                  * MX44006/8 has actual proximity HW that works but just
1926                  * doesn't have the undetectable LED driver HW.  And of
1927                  * course there isn't an ID register either.  So we'll
1928                  * just confirm that our device exists and default to the
1929                  * MX44005 with proximity support if using max4400x.
1930                  */
1931                 st->dev_id = MX_DEVID_MAX44005;
1932                 st->part = MX_NAME_MAX44005;
1933                 ret = mx_reset_sw(st);
1934                 ret |= mx_i2c_rd(st, MX_REG_STS, &val);
1935                 if (ret)
1936                         return -ENODEV;
1937
1938                 if (val != MX_REG_STS_POR)
1939                         return -ENODEV;
1940         }
1941
1942         return ret;
1943 }
1944
1945 static int mx_id_i2c(struct iio_dev *indio_dev, const char *name)
1946 {
1947         struct mx_state *st = iio_priv(indio_dev);
1948         int i;
1949         int ret;
1950
1951         for (i = 0; i < ARRAY_SIZE(mx_i2c_addrs); i++) {
1952                 if (st->i2c->addr == mx_i2c_addrs[i])
1953                         break;
1954         }
1955
1956         if (i < ARRAY_SIZE(mx_i2c_addrs)) {
1957                 st->i2c_addr = st->i2c->addr;
1958                 ret = mx_id_dev(indio_dev, name);
1959         } else {
1960                 name = MX_NAME;
1961                 for (i = 0; i < ARRAY_SIZE(mx_i2c_addrs); i++) {
1962                         st->i2c_addr = mx_i2c_addrs[i];
1963                         ret = mx_id_dev(indio_dev, name);
1964                         if (!ret)
1965                                 break;
1966                 }
1967         }
1968         if (ret)
1969                 st->i2c_addr = 0;
1970         return ret;
1971 }
1972
1973 static void mx_shutdown(struct i2c_client *client)
1974 {
1975         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1976         struct mx_state *st = iio_priv(indio_dev);
1977
1978         mutex_lock(&indio_dev->mlock);
1979         if (st->enable)
1980                 mx_disable(indio_dev);
1981         st->shutdown = true;
1982         mutex_unlock(&indio_dev->mlock);
1983         if (st->dbg & MX_DBG_SPEW_MSG)
1984                 dev_info(&client->dev, "%s\n", __func__);
1985 }
1986
1987 static int mx_remove(struct i2c_client *client)
1988 {
1989         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1990         struct mx_state *st = iio_priv(indio_dev);
1991
1992         if (st != NULL) {
1993                 mx_shutdown(client);
1994                 if (indio_dev->dev.devt)
1995                         iio_device_unregister(indio_dev);
1996                 if (st->trig != NULL) {
1997                         if (client->irq)
1998                                 free_irq(client->irq, st);
1999                         iio_trigger_unregister(st->trig);
2000                         iio_trigger_free(st->trig);
2001                 }
2002                 if (indio_dev->buffer != NULL) {
2003                         iio_buffer_unregister(indio_dev);
2004                         iio_kfifo_free(indio_dev->buffer);
2005                 }
2006                 if (st->dw.wq)
2007                         destroy_workqueue(st->dw.wq);
2008                 mx_pm_exit(st);
2009                 iio_device_free(indio_dev);
2010         }
2011         dev_info(&client->dev, "%s\n", __func__);
2012         return 0;
2013 }
2014
2015 static const struct mx_iio_float mx_scale_dflt[] = {
2016         {
2017                 .ival                   = MX_LIGHT_SCALE_IVAL,
2018                 .micro                  = MX_LIGHT_SCALE_MICRO,
2019         },
2020         {
2021                 .ival                   = MX_TEMP_SCALE_IVAL,
2022                 .micro                  = MX_TEMP_SCALE_MICRO,
2023         },
2024         {
2025                 .ival                   = MX_PROX_SCALE_IVAL,
2026                 .micro                  = MX_PROX_SCALE_MICRO,
2027         },
2028 };
2029
2030 static const struct mx_iio_float mx_offset_dflt[] = {
2031         {
2032                 .ival                   = MX_LIGHT_OFFSET_IVAL,
2033                 .micro                  = MX_LIGHT_OFFSET_MICRO,
2034         },
2035         {
2036                 .ival                   = MX_TEMP_OFFSET_IVAL,
2037                 .micro                  = MX_TEMP_OFFSET_MICRO,
2038         },
2039         {
2040                 .ival                   = MX_PROX_OFFSET_IVAL,
2041                 .micro                  = MX_PROX_OFFSET_MICRO,
2042         },
2043 };
2044
2045 static const struct mx_iio_float mx_peak_dflt[] = {
2046         {
2047                 .ival                   = MX_LIGHT_MAX_RANGE_IVAL,
2048                 .micro                  = MX_LIGHT_MAX_RANGE_MICRO,
2049         },
2050         {
2051                 .ival                   = MX_TEMP_MAX_RANGE_IVAL,
2052                 .micro                  = MX_TEMP_MAX_RANGE_MICRO,
2053         },
2054         {
2055                 .ival                   = MX_PROX_MAX_RANGE_IVAL,
2056                 .micro                  = MX_PROX_MAX_RANGE_MICRO,
2057         },
2058 };
2059
2060 static const struct mx_iio_float mx_peak_scale_dflt[] = {
2061         {
2062                 .ival                   = MX_LIGHT_RESOLUTION_IVAL,
2063                 .micro                  = MX_LIGHT_RESOLUTION_MICRO,
2064         },
2065         {
2066                 .ival                   = MX_TEMP_RESOLUTION_IVAL,
2067                 .micro                  = MX_TEMP_RESOLUTION_MICRO,
2068         },
2069         {
2070                 .ival                   = MX_PROX_RESOLUTION_IVAL,
2071                 .micro                  = MX_PROX_RESOLUTION_MICRO,
2072         },
2073 };
2074
2075 static const unsigned int mx_threshold_dflt[] = {
2076         MX_LIGHT_THRESHOLD_DFLT,
2077         MX_TEMP_THRESHOLD_DFLT,
2078         MX_PROX_THRESHOLD_DFLT,
2079 };
2080
2081 static int mx_of_dt(struct i2c_client *client, struct mx_state *st)
2082 {
2083         struct device_node *dn = client->dev.of_node;
2084         unsigned int thresh;
2085         unsigned int i;
2086
2087         /* default device specific parameters */
2088         st->amb_cfg = MX_AMB_CFG_DFLT;
2089         st->prx_cfg = MX_PRX_CFG_DFLT;
2090         /* default NVS ALS programmable parameters */
2091         for (i = 0; i < MX_DEV_N; i++) {
2092                 st->scale_val[i] = mx_scale_dflt[i].ival;
2093                 st->scale_val2[i] = mx_scale_dflt[i].micro;
2094                 st->offset_val[i] = mx_offset_dflt[i].ival;
2095                 st->offset_val2[i] = mx_offset_dflt[i].micro;
2096                 st->peak_val[i] = mx_peak_dflt[i].ival;
2097                 st->peak_val2[i] = mx_peak_dflt[i].micro;
2098                 st->peak_scale_val[i] = mx_peak_scale_dflt[i].ival;
2099                 st->peak_scale_val2[i] = mx_peak_scale_dflt[i].micro;
2100                 if (st->scale_val2[i]) {
2101                         thresh = 1000000 / st->scale_val2[i];
2102                         thresh *= mx_threshold_dflt[i];
2103                 } else {
2104                         thresh = mx_threshold_dflt[i];
2105                 }
2106                 st->thr_lo[i] = thresh;
2107                 st->thr_hi[i] = thresh;
2108         }
2109         st->prx_thr_bin = MX_PROX_THRESHOLD_BINARY_DFLT;
2110         /* device tree parameters */
2111         if (client->dev.of_node) {
2112                 /* device specific parameters */
2113                 of_property_read_u8(dn, "ambient_cfg_reg", &st->amb_cfg);
2114                 of_property_read_u8(dn, "proximity_cfg_reg", &st->prx_cfg);
2115                 of_property_read_u8(dn, "threshold_persist_reg", &st->thr_cfg);
2116                 /* common NVS programmable parameters */
2117                 st->iio_ts_en = of_property_read_bool(dn, "iio_timestamps");
2118                 of_property_read_u32(dn, "report_count", &st->report_n);
2119                 /* common NVS ALS programmable parameters */
2120                 of_property_read_s32(dn, "light_uncalibrated_lo",
2121                                      &st->i_uc_lo[MX_DEV_LIGHT]);
2122                 of_property_read_s32(dn, "light_uncalibrated_hi",
2123                                      &st->i_uc_hi[MX_DEV_LIGHT]);
2124                 of_property_read_s32(dn, "light_calibrated_lo",
2125                                      &st->i_c_lo[MX_DEV_LIGHT]);
2126                 of_property_read_s32(dn, "light_calibrated_hi",
2127                                      &st->i_c_hi[MX_DEV_LIGHT]);
2128                 of_property_read_s32(dn, "light_max_range_val",
2129                                      &st->peak_val[MX_DEV_LIGHT]);
2130                 of_property_read_s32(dn, "light_max_range_val2",
2131                                      &st->peak_val2[MX_DEV_LIGHT]);
2132                 of_property_read_s32(dn, "light_resolution_val",
2133                                      &st->peak_scale_val[MX_DEV_LIGHT]);
2134                 of_property_read_s32(dn, "light_resolution_val2",
2135                                      &st->peak_scale_val2[MX_DEV_LIGHT]);
2136                 of_property_read_s32(dn, "light_scale_val",
2137                                      &st->scale_val[MX_DEV_LIGHT]);
2138                 of_property_read_s32(dn, "light_scale_val2",
2139                                      &st->scale_val2[MX_DEV_LIGHT]);
2140                 of_property_read_s32(dn, "light_offset_val",
2141                                      &st->offset_val[MX_DEV_LIGHT]);
2142                 of_property_read_s32(dn, "light_offset_val2",
2143                                      &st->offset_val2[MX_DEV_LIGHT]);
2144                 of_property_read_u32(dn, "light_threshold_lo",
2145                                      &st->thr_lo[MX_DEV_LIGHT]);
2146                 of_property_read_u32(dn, "light_threshold_hi",
2147                                      &st->thr_hi[MX_DEV_LIGHT]);
2148                 /* common NVS temp programmable parameters */
2149                 of_property_read_s32(dn, "temp_max_range_val",
2150                                      &st->peak_val[MX_DEV_TEMP]);
2151                 of_property_read_s32(dn, "temp_max_range_val2",
2152                                      &st->peak_val2[MX_DEV_TEMP]);
2153                 of_property_read_s32(dn, "temp_resolution_val",
2154                                      &st->peak_scale_val[MX_DEV_TEMP]);
2155                 of_property_read_s32(dn, "temp_resolution_val2",
2156                                      &st->peak_scale_val2[MX_DEV_TEMP]);
2157                 of_property_read_s32(dn, "temp_scale_val",
2158                                      &st->scale_val[MX_DEV_TEMP]);
2159                 of_property_read_s32(dn, "temp_scale_val2",
2160                                      &st->scale_val2[MX_DEV_TEMP]);
2161                 of_property_read_s32(dn, "temp_offset_val",
2162                                      &st->offset_val[MX_DEV_TEMP]);
2163                 of_property_read_s32(dn, "temp_offset_val2",
2164                                      &st->offset_val2[MX_DEV_TEMP]);
2165                 of_property_read_u32(dn, "temp_threshold_lo",
2166                                      &st->thr_lo[MX_DEV_TEMP]);
2167                 of_property_read_u32(dn, "temp_threshold_hi",
2168                                      &st->thr_hi[MX_DEV_TEMP]);
2169                 /* common NVS proximity programmable parameters */
2170                 of_property_read_s32(dn, "proximity_uncalibrated_lo",
2171                                      &st->i_uc_lo[MX_DEV_PROX]);
2172                 of_property_read_s32(dn, "proximity_uncalibrated_hi",
2173                                      &st->i_uc_hi[MX_DEV_PROX]);
2174                 of_property_read_s32(dn, "proximity_calibrated_lo",
2175                                      &st->i_c_lo[MX_DEV_PROX]);
2176                 of_property_read_s32(dn, "proximity_calibrated_hi",
2177                                      &st->i_c_hi[MX_DEV_PROX]);
2178                 of_property_read_s32(dn, "proximity_max_range_val",
2179                                      &st->peak_val[MX_DEV_PROX]);
2180                 of_property_read_s32(dn, "proximity_max_range_val2",
2181                                      &st->peak_val2[MX_DEV_PROX]);
2182                 of_property_read_s32(dn, "proximity_resolution_val",
2183                                      &st->peak_scale_val[MX_DEV_PROX]);
2184                 of_property_read_s32(dn, "proximity_resolution_val2",
2185                                      &st->peak_scale_val2[MX_DEV_PROX]);
2186                 of_property_read_s32(dn, "proximity_scale_val",
2187                                      &st->scale_val[MX_DEV_PROX]);
2188                 of_property_read_s32(dn, "proximity_scale_val2",
2189                                      &st->scale_val2[MX_DEV_PROX]);
2190                 of_property_read_s32(dn, "proximity_offset_val",
2191                                      &st->offset_val[MX_DEV_PROX]);
2192                 of_property_read_s32(dn, "proximity_offset_val2",
2193                                      &st->offset_val2[MX_DEV_PROX]);
2194                 /* proximity threshold lo/hi are reversed */
2195                 of_property_read_u32(dn, "proximity_threshold_lo",
2196                                      &st->thr_hi[MX_DEV_PROX]);
2197                 of_property_read_u32(dn, "proximity_threshold_hi",
2198                                      &st->thr_lo[MX_DEV_PROX]);
2199                 of_property_read_u32(dn, "proximity_binary_threshold",
2200                                      &st->prx_thr_bin);
2201                 if (st->prx_thr_bin) {
2202                         /* proximity has binary threshold */
2203                         st->peak_val[MX_DEV_PROX] = 1;
2204                         st->peak_val2[MX_DEV_PROX] = 0;
2205                         st->peak_scale_val[MX_DEV_PROX] = 1;
2206                         st->peak_scale_val2[MX_DEV_PROX] = 0;
2207                         st->scale_val[MX_DEV_PROX] = 0;
2208                         st->scale_val2[MX_DEV_PROX] = 0;
2209                         st->offset_val[MX_DEV_PROX] = 0;
2210                         st->offset_val2[MX_DEV_PROX] = 0;
2211                 }
2212         }
2213         if (!st->report_n)
2214                 st->report_n = MX_REPORT_N;
2215         return 0;
2216 }
2217
2218 static int mx_probe(struct i2c_client *client, const struct i2c_device_id *id)
2219 {
2220         struct iio_dev *indio_dev;
2221         struct mx_state *st;
2222         unsigned long irqflags;
2223         unsigned int i;
2224         int ret;
2225
2226         dev_info(&client->dev, "%s\n", __func__);
2227         indio_dev = iio_device_alloc(sizeof(*st));
2228         if (indio_dev == NULL) {
2229                 dev_err(&client->dev, "%s iio_device_alloc ERR\n", __func__);
2230                 return -ENOMEM;
2231         }
2232
2233         st = iio_priv(indio_dev);
2234         st->i2c = client;
2235         i2c_set_clientdata(client, indio_dev);
2236         ret = mx_of_dt(client, st);
2237         if (ret) {
2238                 dev_err(&client->dev, "%s _of_dt ERR\n", __func__);
2239                 ret = -ENODEV;
2240                 goto mx_probe_err;
2241         }
2242
2243         mx_pm_init(st);
2244         ret = mx_id_i2c(indio_dev, id->name);
2245         if (ret) {
2246                 dev_err(&client->dev, "%s _id_i2c ERR\n", __func__);
2247                 ret = -ENODEV;
2248                 goto mx_probe_exit;
2249         }
2250
2251         mx_pm(st, false);
2252         indio_dev->buffer = iio_kfifo_allocate(indio_dev);
2253         if (!indio_dev->buffer) {
2254                 dev_err(&client->dev, "%s iio_kfifo_allocate ERR\n", __func__);
2255                 ret = -ENOMEM;
2256                 goto mx_probe_err;
2257         }
2258
2259         indio_dev->buffer->scan_timestamp = true;
2260         indio_dev->modes = INDIO_DIRECT_MODE;
2261         indio_dev->currentmode = INDIO_DIRECT_MODE;
2262         indio_dev->dev.parent = &client->dev;
2263         indio_dev->channels = mx_channels;
2264         if (st->dev_id != MX_DEVID_MAX44005) {
2265                 /* remove proximity IIO channel */
2266                 indio_dev->num_channels = (ARRAY_SIZE(mx_channels) - 1);
2267                 memcpy(&mx_channels[MX_SCAN_PROX],
2268                        &mx_channels[MX_SCAN_TIMESTAMP],
2269                        sizeof(struct iio_chan_spec));
2270                 /* remove proximity attributes */
2271                 for (i = 0; i < ARRAY_SIZE(mx_attrs); i++) {
2272                         if (mx_attrs[i] == &iio_dev_attr_proximity_part.
2273                                            dev_attr.attr) {
2274                                 mx_attrs[i] = NULL;
2275                                 break;
2276                         }
2277                 }
2278         } else {
2279                 indio_dev->num_channels = ARRAY_SIZE(mx_channels);
2280         }
2281         indio_dev->name = MX_NAME;
2282         indio_dev->info = &mx_iio_info;
2283         indio_dev->setup_ops = &mx_buffer_setup_ops;
2284         ret = iio_buffer_register(indio_dev, indio_dev->channels,
2285                                   indio_dev->num_channels);
2286         if (ret) {
2287                 dev_err(&client->dev, "%s iio_buffer_register ERR\n",
2288                         __func__);
2289                 goto mx_probe_err;
2290         }
2291
2292         INIT_DELAYED_WORK(&st->dw, mx_work);
2293         if (client->irq) {
2294                 irqflags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2295                 if (st->dev_id == MX_DEVID_MAX44005)
2296                         irqflags |= IRQF_NO_SUSPEND; /* for proximity */
2297                 ret = request_threaded_irq(client->irq, NULL, mx_irq_thread,
2298                                            irqflags, MX_NAME, st);
2299                 if (ret) {
2300                         dev_err(&client->dev, "%s req_threaded_irq ERR %d\n",
2301                                 __func__, ret);
2302                         ret = -ENOMEM;
2303                         goto mx_probe_err;
2304                 }
2305         }
2306
2307         st->trig = iio_trigger_alloc("%s-dev%d",
2308                                      indio_dev->name, indio_dev->id);
2309         if (st->trig == NULL) {
2310                 dev_err(&client->dev, "%s iio_allocate_trigger ERR\n",
2311                         __func__);
2312                 ret = -ENOMEM;
2313                 goto mx_probe_err;
2314         }
2315
2316         st->trig->dev.parent = &st->i2c->dev;
2317         st->trig->ops = &mx_trigger_ops;
2318         ret = iio_trigger_register(st->trig);
2319         if (ret) {
2320                 dev_err(&client->dev, "%s iio_trigger_register ERR\n",
2321                         __func__);
2322                 ret = -ENOMEM;
2323                 goto mx_probe_err;
2324         }
2325
2326         indio_dev->trig = st->trig;
2327         indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
2328         ret = iio_device_register(indio_dev);
2329         if (ret) {
2330                 dev_err(&client->dev, "%s iio_device_register ERR\n",
2331                         __func__);
2332                 goto mx_probe_err;
2333         }
2334
2335         dev_info(&client->dev, "%s done\n", __func__);
2336         return 0;
2337
2338 mx_probe_err:
2339         dev_err(&client->dev, "%s ERR %d\n", __func__, ret);
2340 mx_probe_exit:
2341         mx_remove(client);
2342         return ret;
2343 }
2344
2345 static const struct i2c_device_id mx_i2c_device_id[] = {
2346         { MX_NAME, 0 },
2347         { MX_NAME_MAX44005, 0 },
2348         { MX_NAME_MAX44006, 0 },
2349         { MX_NAME_MAX44008, 0 },
2350         {}
2351 };
2352
2353 MODULE_DEVICE_TABLE(i2c, mx_i2c_device_id);
2354
2355 static const struct of_device_id mx_of_match[] = {
2356         { .compatible = "maxim,max4400x", },
2357         { .compatible = "maxim,max44005", },
2358         { .compatible = "maxim,max44006", },
2359         { .compatible = "maxim,max44008", },
2360         {},
2361 };
2362
2363 MODULE_DEVICE_TABLE(of, mx_of_match);
2364
2365 static struct i2c_driver mx_driver = {
2366         .class          = I2C_CLASS_HWMON,
2367         .probe          = mx_probe,
2368         .remove         = mx_remove,
2369         .shutdown       = mx_shutdown,
2370         .driver = {
2371                 .name           = MX_NAME,
2372                 .owner          = THIS_MODULE,
2373                 .of_match_table = of_match_ptr(mx_of_match),
2374                 .pm             = &mx_pm_ops,
2375         },
2376         .id_table       = mx_i2c_device_id,
2377 };
2378 module_i2c_driver(mx_driver);
2379
2380 MODULE_LICENSE("GPL");
2381 MODULE_DESCRIPTION("MAX4400x driver");
2382 MODULE_AUTHOR("NVIDIA Corporation");