85076b29426e7f827c075232ea96de0d77d7190b
[linux-3.10.git] / drivers / iio / light / nvs_ltr659.c
1 /* Copyright (c) 2014-2015, 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 /* See nvs_iio.c and nvs.h for documentation */
15 /* See nvs_light.c and nvs_light.h for documentation */
16 /* See nvs_proximity.c and nvs_proximity.h for documentation */
17
18
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/ktime.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/workqueue.h>
27 #include <linux/interrupt.h>
28 #include <linux/of.h>
29 #include <linux/nvs.h>
30 #include <linux/nvs_light.h>
31 #include <linux/nvs_proximity.h>
32
33
34 #define LTR_VENDOR                      "Lite-On Technology Corp."
35 #define LTR_NAME                        "ltrX5X"
36 #define LTR_NAME_LTR558ALS              "ltr558als"
37 #define LTR_NAME_LTR659PS               "ltr659ps"
38 #define LTR_DEVID_558ALS                (0x80)
39 #define LTR_DEVID_659PS                 (0x90)
40 #define LTR_HW_DELAY_MS                 (600)
41 #define LTR_WAKEUP_DELAY_MS             (10)
42 #define LTR_POLL_DLY_MS_DFLT            (2000)
43 #define LTR_POLL_DLY_MS_MIN             (100)
44 #define LTR_POLL_DLY_MS_MAX             (60000)
45 #define LTR_REG_PS_CONTR_DFLT           (0x00)
46 #define LTR_REG_PS_LED_DFLT             (0x04)
47 #define LTR_REG_PS_N_PULSES_DFLT        (0x7F)
48 #define LTR_REG_PS_MEAS_RATE_DFLT       (0x05)
49 #define LTR_REG_ALS_MEAS_RATE_DFLT      (0x04)
50 #define LTR_REG_INTERRUPT_PERSIST_DFLT  (0x00)
51 /* light defines */
52 #define LTR_LIGHT_VERSION               (1)
53 #define LTR_LIGHT_MAX_RANGE_IVAL        (14323)
54 #define LTR_LIGHT_MAX_RANGE_MICRO       (0)
55 #define LTR_LIGHT_RESOLUTION_IVAL       (0)
56 #define LTR_LIGHT_RESOLUTION_MICRO      (14000)
57 #define LTR_LIGHT_MILLIAMP_IVAL         (0)
58 #define LTR_LIGHT_MILLIAMP_MICRO        (13500)
59 #define LTR_LIGHT_SCALE_IVAL            (0)
60 #define LTR_LIGHT_SCALE_MICRO           (10000)
61 #define LTR_LIGHT_THRESHOLD_DFLT        (50)
62 /* proximity defines */
63 #define LTR_PROX_THRESHOLD_LO           (1000)
64 #define LTR_PROX_THRESHOLD_HI           (2000)
65 #define LTR_PROX_VERSION                (1)
66 /* setting max_range and resolution to 1.0 = binary proximity */
67 #define LTR_PROX_MAX_RANGE_IVAL         (1)
68 #define LTR_PROX_MAX_RANGE_MICRO        (0)
69 #define LTR_PROX_RESOLUTION_IVAL        (1)
70 #define LTR_PROX_RESOLUTION_MICRO       (0)
71 #define LTR_PROX_MILLIAMP_IVAL          (10)
72 #define LTR_PROX_MILLIAMP_MICRO         (250000)
73 #define LTR_PROX_SCALE_IVAL             (0)
74 #define LTR_PROX_SCALE_MICRO            (0)
75 #define LTR_PROX_OFFSET_IVAL            (0)
76 #define LTR_PROX_OFFSET_MICRO           (0)
77 /* HW registers */
78 #define LTR_REG_ALS_CONTR               (0x80)
79 #define LTR_REG_ALS_CONTR_MODE          (1)
80 #define LTR_REG_ALS_CONTR_SW_RESET      (2)
81 #define LTR_REG_ALS_CONTR_GAIN          (3)
82 #define LTR_REG_PS_CONTR                (0x81)
83 #define LTR_REG_PS_CONTR_MODE           (1)
84 #define LTR_REG_PS_CONTR_GAIN           (2)
85 #define LTR_REG_PS_CONTR_SAT_EN         (5)
86 #define LTR_REG_PS_CONTR_POR_MASK       (0x2C)
87 #define LTR_REG_PS_LED                  (0x82)
88 #define LTR_REG_PS_N_PULSES             (0x83)
89 #define LTR_REG_PS_MEAS_RATE            (0x84)
90 #define LTR_REG_ALS_MEAS_RATE           (0x85)
91 #define LTR_REG_PART_ID                 (0x86)
92 #define LTR_REG_PART_ID_MASK            (0xF0)
93 #define LTR_REG_MANUFAC_ID              (0x87)
94 #define LTR_REG_MANUFAC_ID_VAL          (0x05)
95 #define LTR_REG_ALS_DATA_CH1_0          (0x88)
96 #define LTR_REG_ALS_DATA_CH1_1          (0x89)
97 #define LTR_REG_ALS_DATA_CH0_0          (0x8A)
98 #define LTR_REG_ALS_DATA_CH0_1          (0x8B)
99 #define LTR_REG_STATUS                  (0x8C)
100 #define LTR_REG_STATUS_DATA_PS          (0)
101 #define LTR_REG_STATUS_IRQ_PS           (1)
102 #define LTR_REG_STATUS_DATA_ALS         (2)
103 #define LTR_REG_STATUS_IRQ_ALS          (3)
104 #define LTR_REG_STATUS_DATA_MASK        (0x05)
105 #define LTR_REG_PS_DATA_0               (0x8D)
106 #define LTR_REG_PS_DATA_1               (0x8E)
107 #define LTR_REG_PS_DATA_MASK            (0x07FF)
108 #define LTR_REG_PS_DATA_SAT             (15)
109 #define LTR_REG_INTERRUPT               (0x8F)
110 #define LTR_REG_INTERRUPT_PS_EN         (0)
111 #define LTR_REG_INTERRUPT_ALS_EN        (1)
112 #define LTR_REG_INTERRUPT_MODE_MASK     (0x03)
113 #define LTR_REG_INTERRUPT_POLARITY      (2)
114 #define LTR_REG_PS_THRES_UP_0           (0x90)
115 #define LTR_REG_PS_THRES_UP_1           (0x91)
116 #define LTR_REG_PS_THRES_LOW_0          (0x92)
117 #define LTR_REG_PS_THRES_LOW_1          (0x93)
118 #define LTR_REG_PS_OFFSET_1             (0x94)
119 #define LTR_REG_PS_OFFSET_0             (0x95)
120 #define LTR_REG_ALS_THRES_UP_0          (0x97)
121 #define LTR_REG_ALS_THRES_UP_1          (0x98)
122 #define LTR_REG_ALS_THRES_LOW_0         (0x99)
123 #define LTR_REG_ALS_THRES_LOW_1         (0x9A)
124 #define LTR_REG_INTERRUPT_PERSIST       (0x9E)
125 #define LTR_REG_INTERRUPT_PERSIST_MASK  (0xF0)
126 /* devices */
127 #define LTR_DEV_LIGHT                   (0)
128 #define LTR_DEV_PROX                    (1)
129 #define LTR_DEV_N                       (2)
130
131
132 /* regulator names in order of powering on */
133 static char *ltr_vregs[] = {
134         "vdd",
135 };
136
137 static u8 ltr_ids[] = {
138         LTR_DEVID_558ALS,
139         LTR_DEVID_659PS,
140 };
141
142 static unsigned short ltr_i2c_addrs[] = {
143         0x23,
144 };
145
146 static struct nvs_light_dynamic ltr_nld_tbl[] = {
147         {{0, 10000}, {327,   670000}, {0, 90000}, 100, 0},
148         {{2, 0},     {65534, 0},      {0, 90000}, 100, 0},
149 };
150
151 struct ltr_state {
152         struct i2c_client *i2c;
153         struct nvs_fn_if *nvs;
154         void *nvs_st[LTR_DEV_N];
155         struct sensor_cfg cfg[LTR_DEV_N];
156         struct delayed_work dw;
157         struct regulator_bulk_data vreg[ARRAY_SIZE(ltr_vregs)];
158         struct nvs_light light;
159         struct nvs_proximity prox;
160         unsigned int sts;               /* status flags */
161         unsigned int errs;              /* error count */
162         unsigned int enabled;           /* enable status */
163         u16 i2c_addr;                   /* I2C address */
164         u8 dev_id;                      /* device ID */
165         u8 ps_contr;                    /* PS_CONTR register default */
166         u8 ps_led;                      /* PS_LED register default */
167         u8 ps_n_pulses;                 /* PS_N_PULSES register default */
168         u8 ps_meas_rate;                /* PS_MEAS_RATE register default */
169         u8 als_meas_rate;               /* ALS_CONTR register default */
170         u8 interrupt;                   /* INTERRUPT register default */
171         u8 interrupt_persist;           /* INTERRUPT_PERSIST reg default */
172         u8 rc_als_contr;                /* cache of ALS_CONTR */
173         u8 rc_ps_contr;                 /* cache of PS_CONTR */
174         u8 rc_interrupt;                /* cache of INTERRUPT */
175 };
176
177 static s64 ltr_get_time_ns(void)
178 {
179         struct timespec ts;
180
181         ktime_get_ts(&ts);
182         return timespec_to_ns(&ts);
183 }
184
185 static void ltr_err(struct ltr_state *st)
186 {
187         st->errs++;
188         if (!st->errs)
189                 st->errs--;
190 }
191
192 static void ltr_mutex_lock(struct ltr_state *st)
193 {
194         unsigned int i;
195
196         if (st->nvs) {
197                 for (i = 0; i < LTR_DEV_N; i++) {
198                         if (st->nvs_st[i])
199                                 st->nvs->nvs_mutex_lock(st->nvs_st[i]);
200                 }
201         }
202 }
203
204 static void ltr_mutex_unlock(struct ltr_state *st)
205 {
206         unsigned int i;
207
208         if (st->nvs) {
209                 for (i = 0; i < LTR_DEV_N; i++) {
210                         if (st->nvs_st[i])
211                                 st->nvs->nvs_mutex_unlock(st->nvs_st[i]);
212                 }
213         }
214 }
215
216 static int ltr_i2c_read(struct ltr_state *st, u8 reg, u16 len, u8 *val)
217 {
218         struct i2c_msg msg[2];
219
220         msg[0].addr = st->i2c_addr;
221         msg[0].flags = 0;
222         msg[0].len = 1;
223         msg[0].buf = &reg;
224         msg[1].addr = st->i2c_addr;
225         msg[1].flags = I2C_M_RD;
226         msg[1].len = len;
227         msg[1].buf = val;
228         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
229                 ltr_err(st);
230                 return -EIO;
231         }
232
233         return 0;
234 }
235
236 static int ltr_i2c_rd(struct ltr_state *st, u8 reg, u8 *val)
237 {
238         return ltr_i2c_read(st, reg, 1, val);
239 }
240
241 static int ltr_i2c_write(struct ltr_state *st, u16 len, u8 *buf)
242 {
243         struct i2c_msg msg;
244
245         if (st->i2c_addr) {
246                 msg.addr = st->i2c_addr;
247                 msg.flags = 0;
248                 msg.len = len;
249                 msg.buf = buf;
250                 if (i2c_transfer(st->i2c->adapter, &msg, 1) != 1) {
251                         ltr_err(st);
252                         return -EIO;
253                 }
254         }
255
256         return 0;
257 }
258
259 static int ltr_i2c_wr(struct ltr_state *st, u8 reg, u8 val)
260 {
261         u8 buf[2];
262
263         buf[0] = reg;
264         buf[1] = val;
265         return ltr_i2c_write(st, sizeof(buf), buf);
266 }
267
268 static int ltr_reset_sw(struct ltr_state *st)
269 {
270         u8 buf[5];
271         u8 reset;
272         int ret;
273
274         reset = 1 << LTR_REG_ALS_CONTR_SW_RESET;
275         if (st->dev_id == LTR_DEVID_659PS)
276                 reset >>= 1;
277         ret = ltr_i2c_wr(st, LTR_REG_ALS_CONTR, reset);
278         if (!ret) {
279                 mdelay(LTR_HW_DELAY_MS);
280                 st->rc_als_contr = 0;
281                 st->rc_ps_contr = 0;
282                 st->rc_interrupt = 0;
283         }
284         buf[0] = LTR_REG_PS_LED;
285         buf[1] = st->ps_led;
286         buf[2] = st->ps_n_pulses;
287         buf[3] = st->ps_meas_rate;
288         buf[4] = st->als_meas_rate;
289         ret |= ltr_i2c_write(st, sizeof(buf), buf);
290         ret |= ltr_i2c_wr(st, LTR_REG_INTERRUPT_PERSIST, st->interrupt_persist);
291         return ret;
292 }
293
294 static int ltr_pm(struct ltr_state *st, bool enable)
295 {
296         int ret = 0;
297
298         if (enable) {
299                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
300                                  ARRAY_SIZE(ltr_vregs));
301                 if (ret > 0)
302                         mdelay(LTR_HW_DELAY_MS);
303                 ret = ltr_reset_sw(st);
304         } else {
305                 ret = nvs_vregs_sts(st->vreg, ARRAY_SIZE(ltr_vregs));
306                 if ((ret < 0) || (ret == ARRAY_SIZE(ltr_vregs))) {
307                         ret = ltr_i2c_wr(st, LTR_REG_ALS_CONTR, 0);
308                         ret |= ltr_i2c_wr(st, LTR_REG_PS_CONTR, 0);
309                 } else if (ret > 0) {
310                         nvs_vregs_enable(&st->i2c->dev, st->vreg,
311                                          ARRAY_SIZE(ltr_vregs));
312                         mdelay(LTR_HW_DELAY_MS);
313                         ret = ltr_i2c_wr(st, LTR_REG_ALS_CONTR, 0);
314                         ret |= ltr_i2c_wr(st, LTR_REG_PS_CONTR, 0);
315                 }
316                 ret |= nvs_vregs_disable(&st->i2c->dev, st->vreg,
317                                          ARRAY_SIZE(ltr_vregs));
318         }
319         if (ret > 0)
320                 ret = 0;
321         if (ret) {
322                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
323                         __func__, enable, ret);
324         } else {
325                 if (st->sts & NVS_STS_SPEW_MSG)
326                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
327                                  __func__, enable);
328         }
329         return ret;
330 }
331
332 static void ltr_pm_exit(struct ltr_state *st)
333 {
334         ltr_pm(st, false);
335         nvs_vregs_exit(&st->i2c->dev, st->vreg, ARRAY_SIZE(ltr_vregs));
336 }
337
338 static int ltr_pm_init(struct ltr_state *st)
339 {
340         int ret;
341
342         st->enabled = 0;
343         nvs_vregs_init(&st->i2c->dev,
344                        st->vreg, ARRAY_SIZE(ltr_vregs), ltr_vregs);
345         ret = ltr_pm(st, true);
346         return ret;
347 }
348
349 static int ltr_interrupt_wr(struct ltr_state *st, u8 interrupt)
350 {
351         int ret = 0;
352
353         if (interrupt != st->interrupt) {
354                 ret = ltr_i2c_wr(st, LTR_REG_INTERRUPT, interrupt);
355                 if (st->sts & NVS_STS_SPEW_MSG)
356                         dev_info(&st->i2c->dev, "%s irq %hhx->%hhx err=%d\n",
357                                  __func__, st->rc_interrupt, interrupt, ret);
358                 if (!ret)
359                         st->rc_interrupt = interrupt;
360         }
361         return ret;
362 }
363
364 static int ltr_cmd_wr(struct ltr_state *st, unsigned int enable, bool irq_en)
365 {
366         u8 interrupt;
367         u8 als_contr;
368         u8 ps_contr;
369         int ret;
370         int ret_t = 0;
371
372         if (enable & (1 << LTR_DEV_LIGHT)) {
373                 als_contr = st->light.nld_i << LTR_REG_ALS_CONTR_GAIN;
374                 als_contr |= 1 << LTR_REG_ALS_CONTR_MODE;
375         } else {
376                 als_contr = 0;
377         }
378         if (st->rc_als_contr != als_contr) {
379                 ret = ltr_i2c_wr(st, LTR_REG_ALS_CONTR, als_contr);
380                 if (ret)
381                         ret_t |= ret;
382                 else
383                         st->rc_als_contr = als_contr;
384         }
385         ps_contr = st->ps_contr;
386         if (enable & (1 << LTR_DEV_PROX))
387                 ps_contr |= 1 << LTR_REG_PS_CONTR_MODE;
388         if (st->rc_ps_contr != ps_contr) {
389                 ret = ltr_i2c_wr(st, LTR_REG_PS_CONTR, ps_contr);
390                 if (ret)
391                         ret_t |= ret;
392                 else
393                         st->rc_ps_contr = ps_contr;
394         }
395         if (st->i2c->irq) {
396                 interrupt = st->interrupt;
397                 if (irq_en) {
398                         if (enable & (1 << LTR_DEV_LIGHT))
399                                 interrupt |= (1 << LTR_REG_INTERRUPT_ALS_EN);
400                         if (enable & (1 << LTR_DEV_PROX))
401                                 interrupt |= (1 << LTR_REG_INTERRUPT_PS_EN);
402                 }
403                 ret_t |= ltr_interrupt_wr(st, interrupt);
404         }
405         if ((st->rc_interrupt & LTR_REG_INTERRUPT_MODE_MASK) && !ret_t)
406                 ret_t = 1; /* flag IRQ enabled */
407         if (st->sts & NVS_STS_SPEW_MSG)
408                 dev_info(&st->i2c->dev, "%s als=%hhx ps=%hhx ret=%d\n",
409                          __func__, st->rc_als_contr, st->rc_ps_contr, ret_t);
410         return ret_t;
411 }
412
413 static int ltr_thr_wr(struct ltr_state *st, u8 reg, u16 thr_lo, u16 thr_hi)
414 {
415         u8 buf[5];
416         int ret;
417
418         ret = ltr_interrupt_wr(st, st->interrupt); /* irq disable */
419         if (st->i2c->irq) {
420                 buf[0] = reg;
421                 buf[1] = thr_hi & 0xFF;
422                 buf[2] = thr_hi >> 8;
423                 buf[3] = thr_lo & 0xFF;
424                 buf[4] = thr_lo >> 8;
425                 ret |= ltr_i2c_write(st, sizeof(buf), buf);
426                 if (st->sts & NVS_STS_SPEW_MSG)
427                         dev_info(&st->i2c->dev,
428                                  "%s reg=%hhx lo=%hd hi=%hd ret=%d\n",
429                                  __func__, reg, thr_lo, thr_hi, ret);
430         }
431         return ret;
432 }
433
434 static int ltr_rd_light(struct ltr_state *st, s64 ts)
435 {
436         u32 hw;
437         u16 hw0;
438         u16 hw1;
439         unsigned int divisor;
440         int ratio;
441         int ch0_coeff = 1;
442         int ch1_coeff = 1;
443         int ret;
444
445         ret = ltr_i2c_read(st, LTR_REG_ALS_DATA_CH0_0, 2, (u8 *)&hw0);
446         if (ret)
447                 return ret;
448
449         hw0 = be16_to_cpu(hw0);
450         ret = ltr_i2c_read(st, LTR_REG_ALS_DATA_CH1_0, 2, (u8 *)&hw1);
451         if (ret)
452                 return ret;
453
454         hw1 = be16_to_cpu(hw1);
455         /* The code from here to the next comment is from a previous driver.
456          * It appears to do all the resolution calculations that the NVS light
457          * module would do.  A 558 part wasn't available when this was written,
458          * so either this code is removed and use the nvs_light_dynamic table
459          * along with interpolation calibration or clear the
460          * nvs_light_dynamic.resolution to 1.0.
461          * Note that scale will divide the final result by 100 in user space.
462          */
463         divisor = hw0 + hw1;
464         if (divisor) {
465                 ratio = (hw1 * 100) / divisor;
466                 if (ratio < 45) {
467                         ch0_coeff = 17743;
468                         ch1_coeff = -11059;
469                 } else if ((ratio >= 45) && (ratio < 64)) {
470                         ch0_coeff = 37725;
471                         ch1_coeff = 13363;
472                 } else if ((ratio >= 64) && (ratio < 85)) {
473                         ch0_coeff = 16900;
474                         ch1_coeff = 1690;
475                 } else if (ratio >= 85) {
476                         ch0_coeff = 0;
477                         ch1_coeff = 0;
478                 }
479                 hw = ((hw0 * ch0_coeff) - (hw1 * ch1_coeff)) / 100;
480         } else {
481                 hw = 0;
482         }
483         /* next comment */
484         if (st->sts & NVS_STS_SPEW_DATA)
485                 dev_info(&st->i2c->dev,
486                          "poll light hw %u %lld  diff=%d %lldns  index=%u\n",
487                          hw, ts, hw - st->light.hw, ts - st->light.timestamp,
488                          st->light.nld_i);
489         st->light.hw = hw;
490         st->light.timestamp = ts;
491         ret = nvs_light_read(&st->light);
492         if (ret < 1)
493                 /* either poll or nothing to do */
494                 return ret;
495
496         ret = ltr_thr_wr(st, LTR_REG_ALS_THRES_UP_0,
497                          st->light.hw_thresh_lo, st->light.hw_thresh_hi);
498         return ret;
499 }
500
501 static int ltr_rd_prox(struct ltr_state *st, s64 ts)
502 {
503         u16 hw;
504         int ret;
505
506         ret = ltr_i2c_read(st, LTR_REG_PS_DATA_0, 2, (u8 *)&hw);
507         if (ret)
508                 return ret;
509
510         hw = le16_to_cpu(hw);
511         hw &= LTR_REG_PS_DATA_MASK;
512         if (st->sts & NVS_STS_SPEW_DATA)
513                 dev_info(&st->i2c->dev,
514                          "poll proximity hw %hu %lld  diff=%d %lldns\n",
515                          hw, ts, hw - st->prox.hw, ts - st->prox.timestamp);
516         st->prox.hw = hw;
517         st->prox.timestamp = ts;
518         ret = nvs_proximity_read(&st->prox);
519         if (ret < 1)
520                 /* either poll or nothing to do */
521                 return ret;
522
523         ret = ltr_thr_wr(st, LTR_REG_PS_THRES_UP_0,
524                          st->prox.hw_thresh_lo, st->prox.hw_thresh_hi);
525         return ret;
526 }
527
528 static int ltr_en(struct ltr_state *st, unsigned int enable)
529 {
530         if (enable & (1 << LTR_DEV_LIGHT))
531                 nvs_light_enable(&st->light);
532         if (enable & (1 << LTR_DEV_PROX))
533                 nvs_proximity_enable(&st->prox);
534         return ltr_cmd_wr(st, enable, false);
535 }
536
537 static int ltr_rd(struct ltr_state *st)
538 {
539         s64 ts;
540         u8 sts;
541         int ret = 0;
542
543         /* clear possible IRQ */
544         ret = ltr_i2c_rd(st, LTR_REG_STATUS, &sts);
545         if (ret)
546                 return ret;
547
548         if (sts & LTR_REG_STATUS_DATA_MASK) {
549                 ts = ltr_get_time_ns();
550                 if (st->enabled & (1 << LTR_DEV_PROX))
551                         ret |= ltr_rd_prox(st, ts);
552                 if (st->enabled & (1 << LTR_DEV_LIGHT))
553                         ret |= ltr_rd_light(st, ts);
554         } else {
555                 ret = RET_POLL_NEXT;
556         }
557         if (ret < 0)
558                 /* poll if error or more reporting */
559                 ret = ltr_cmd_wr(st, st->enabled, false);
560         else
561                 ret = ltr_cmd_wr(st, st->enabled, true);
562         return ret;
563 }
564
565 static unsigned int ltr_polldelay(struct ltr_state *st)
566 {
567         unsigned int poll_delay_ms = LTR_POLL_DLY_MS_DFLT;
568
569         if (st->enabled & (1 << LTR_DEV_LIGHT))
570                 poll_delay_ms = st->light.poll_delay_ms;
571         if (st->enabled & (1 << LTR_DEV_PROX)) {
572                 if (poll_delay_ms > st->prox.poll_delay_ms)
573                         poll_delay_ms = st->prox.poll_delay_ms;
574         }
575         return poll_delay_ms;
576 }
577
578 static void ltr_read(struct ltr_state *st)
579 {
580         int ret;
581
582         ltr_mutex_lock(st);
583         if (st->enabled) {
584                 ret = ltr_rd(st);
585                 if (ret < 1)
586                         schedule_delayed_work(&st->dw,
587                                           msecs_to_jiffies(ltr_polldelay(st)));
588         }
589         ltr_mutex_unlock(st);
590 }
591
592 static void ltr_work(struct work_struct *ws)
593 {
594         struct ltr_state *st = container_of((struct delayed_work *)ws,
595                                            struct ltr_state, dw);
596
597         ltr_read(st);
598 }
599
600 static irqreturn_t ltr_irq_thread(int irq, void *dev_id)
601 {
602         struct ltr_state *st = (struct ltr_state *)dev_id;
603
604         if (st->sts & NVS_STS_SPEW_IRQ)
605                 dev_info(&st->i2c->dev, "%s\n", __func__);
606         ltr_read(st);
607         return IRQ_HANDLED;
608 }
609
610 static int ltr_disable(struct ltr_state *st, int snsr_id)
611 {
612         bool disable = true;
613         int ret = 0;
614
615         if (snsr_id >= 0) {
616                 if (st->enabled & ~(1 << snsr_id)) {
617                         st->enabled &= ~(1 << snsr_id);
618                         disable = false;
619                         if (snsr_id == LTR_DEV_LIGHT)
620                                 ret = ltr_i2c_wr(st, LTR_REG_ALS_CONTR, 0);
621                         else if (snsr_id == LTR_DEV_PROX)
622                                 ret = ltr_i2c_wr(st, LTR_REG_PS_CONTR, 0);
623                 }
624         }
625         if (disable) {
626                 cancel_delayed_work(&st->dw);
627                 ret |= ltr_pm(st, false);
628                 if (!ret)
629                         st->enabled = 0;
630         }
631         return ret;
632 }
633
634 static int ltr_enable(void *client, int snsr_id, int enable)
635 {
636         struct ltr_state *st = (struct ltr_state *)client;
637         int ret;
638
639         if (enable < 0)
640                 return st->enabled & (1 << snsr_id);
641
642         if (enable) {
643                 enable = st->enabled | (1 << snsr_id);
644                 ret = ltr_pm(st, true);
645                 if (!ret) {
646                         ret = ltr_en(st, enable);
647                         if (ret < 0) {
648                                 ltr_disable(st, snsr_id);
649                         } else {
650                                 st->enabled = enable;
651                                 schedule_delayed_work(&st->dw,
652                                          msecs_to_jiffies(LTR_POLL_DLY_MS_MIN));
653                         }
654                 }
655         } else {
656                 ret = ltr_disable(st, snsr_id);
657         }
658         return ret;
659 }
660
661 static int ltr_batch(void *client, int snsr_id, int flags,
662                     unsigned int period, unsigned int timeout)
663 {
664         struct ltr_state *st = (struct ltr_state *)client;
665
666         if (timeout)
667                 /* timeout not supported (no HW FIFO) */
668                 return -EINVAL;
669
670         if (snsr_id == LTR_DEV_LIGHT)
671                 st->light.delay_us = period;
672         else if (snsr_id == LTR_DEV_PROX)
673                 st->prox.delay_us = period;
674         return 0;
675 }
676
677 static int ltr_thresh_lo(void *client, int snsr_id, int thresh_lo)
678 {
679         struct ltr_state *st = (struct ltr_state *)client;
680
681         if (snsr_id == LTR_DEV_LIGHT)
682                 nvs_light_threshold_calibrate_lo(&st->light, thresh_lo);
683         else if (snsr_id == LTR_DEV_PROX)
684                 nvs_proximity_threshold_calibrate_lo(&st->prox, thresh_lo);
685
686         return 0;
687 }
688
689 static int ltr_thresh_hi(void *client, int snsr_id, int thresh_hi)
690 {
691         struct ltr_state *st = (struct ltr_state *)client;
692
693         if (snsr_id == LTR_DEV_LIGHT)
694                 nvs_light_threshold_calibrate_hi(&st->light, thresh_hi);
695         else if (snsr_id == LTR_DEV_PROX)
696                 nvs_proximity_threshold_calibrate_hi(&st->prox, thresh_hi);
697         return 0;
698 }
699
700 static int ltr_regs(void *client, int snsr_id, char *buf)
701 {
702         struct ltr_state *st = (struct ltr_state *)client;
703         ssize_t t;
704         u8 val[2];
705         u8 n;
706         u8 i;
707         int ret;
708
709         t = sprintf(buf, "registers:\n");
710         for (i = LTR_REG_ALS_CONTR; i <= LTR_REG_MANUFAC_ID; i++) {
711                 ret = ltr_i2c_rd(st, i, val);
712                 if (ret)
713                         t += sprintf(buf + t, "0x%hhx=ERR\n", i);
714                 else
715                         t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
716                                      i, val[0]);
717         }
718         if (st->dev_id == LTR_DEVID_558ALS) {
719                 n = LTR_REG_ALS_DATA_CH0_1;
720                 for (i = LTR_REG_ALS_DATA_CH1_0; i < n; i += 2) {
721                         ret = ltr_i2c_read(st, i, 2, val);
722                         if (ret)
723                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=ERR\n",
724                                              i, i + 1);
725                         else
726                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
727                                              i, i + 1,
728                                              le16_to_cpup((__le16 *)val));
729                 }
730         }
731         ret = ltr_i2c_rd(st, LTR_REG_STATUS, val);
732         if (ret)
733                 t += sprintf(buf + t, "0x%hhx=ERR\n", LTR_REG_STATUS);
734         else
735                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
736                              LTR_REG_STATUS, val[0]);
737         ret = ltr_i2c_read(st, LTR_REG_PS_DATA_0, 2, val);
738         if (ret)
739                 t += sprintf(buf + t, "0x%hhx:0x%hhx=ERR\n",
740                              LTR_REG_PS_DATA_0, LTR_REG_PS_DATA_1);
741         else
742                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
743                              LTR_REG_PS_DATA_0, LTR_REG_PS_DATA_1,
744                              le16_to_cpup((__le16 *)val));
745         ret = ltr_i2c_rd(st, LTR_REG_INTERRUPT, val);
746         if (ret)
747                 t += sprintf(buf + t, "0x%hhx=ERR\n",
748                              LTR_REG_INTERRUPT);
749         else
750                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
751                              LTR_REG_INTERRUPT, val[0]);
752         for (i = LTR_REG_PS_THRES_UP_0; i < LTR_REG_PS_THRES_LOW_1; i += 2) {
753                 ret = ltr_i2c_read(st, i, 2, val);
754                 if (ret)
755                         t += sprintf(buf + t, "0x%hhx:0x%hhx=ERR\n", i, i + 1);
756                 else
757                         t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
758                                      i, i + 1, le16_to_cpup((__le16 *)val));
759         }
760         ret = ltr_i2c_read(st, LTR_REG_PS_OFFSET_1, 2, val);
761         if (ret)
762                 t += sprintf(buf + t, "0x%hhx:0x%hhx=ERR\n",
763                              LTR_REG_PS_OFFSET_1, LTR_REG_PS_OFFSET_0);
764         else
765                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
766                              i, i + 1, be16_to_cpup((__be16 *)val));
767         if (st->dev_id == LTR_DEVID_558ALS) {
768                 n = LTR_REG_ALS_THRES_LOW_1;
769                 for (i = LTR_REG_ALS_THRES_UP_0; i < n; i += 2) {
770                         ret = ltr_i2c_read(st, i, 2, val);
771                         if (ret)
772                                 t += sprintf(buf + t,
773                                              "0x%hhx:0x%hhx=ERR\n", i, i + 1);
774                         else
775                                 t += sprintf(buf + t, "0x%hhx:0x%hhx=0x%hx\n",
776                                              i, i + 1,
777                                              le16_to_cpup((__le16 *)val));
778                 }
779         }
780         ret = ltr_i2c_rd(st, LTR_REG_INTERRUPT_PERSIST, val);
781         if (ret)
782                 t += sprintf(buf + t, "0x%hhx=ERR\n",
783                              LTR_REG_INTERRUPT_PERSIST);
784         else
785                 t += sprintf(buf + t, "0x%hhx=0x%hhx\n",
786                              LTR_REG_INTERRUPT_PERSIST, val[0]);
787         return t;
788 }
789
790 static struct nvs_fn_dev ltr_fn_dev = {
791         .enable                         = ltr_enable,
792         .batch                          = ltr_batch,
793         .thresh_lo                      = ltr_thresh_lo,
794         .thresh_hi                      = ltr_thresh_hi,
795         .regs                           = ltr_regs,
796 };
797
798 static int ltr_suspend(struct device *dev)
799 {
800         struct i2c_client *client = to_i2c_client(dev);
801         struct ltr_state *st = i2c_get_clientdata(client);
802         unsigned int i;
803         int ret = 0;
804
805         st->sts |= NVS_STS_SUSPEND;
806         if (st->nvs) {
807                 for (i = 0; i < LTR_DEV_N; i++) {
808                         if (st->nvs_st[i])
809                                 ret |= st->nvs->suspend(st->nvs_st[i]);
810                 }
811         }
812         if (st->sts & NVS_STS_SPEW_MSG)
813                 dev_info(&client->dev, "%s\n", __func__);
814         return ret;
815 }
816
817 static int ltr_resume(struct device *dev)
818 {
819         struct i2c_client *client = to_i2c_client(dev);
820         struct ltr_state *st = i2c_get_clientdata(client);
821         unsigned int i;
822         int ret = 0;
823
824         if (st->nvs) {
825                 for (i = 0; i < LTR_DEV_N; i++) {
826                         if (st->nvs_st[i])
827                                 ret |= st->nvs->resume(st->nvs_st[i]);
828                 }
829         }
830         st->sts &= ~NVS_STS_SUSPEND;
831         if (st->sts & NVS_STS_SPEW_MSG)
832                 dev_info(&client->dev, "%s\n", __func__);
833         return ret;
834 }
835
836 static SIMPLE_DEV_PM_OPS(ltr_pm_ops, ltr_suspend, ltr_resume);
837
838 static void ltr_shutdown(struct i2c_client *client)
839 {
840         struct ltr_state *st = i2c_get_clientdata(client);
841         unsigned int i;
842
843         st->sts |= NVS_STS_SHUTDOWN;
844         if (st->nvs) {
845                 for (i = 0; i < LTR_DEV_N; i++) {
846                         if (st->nvs_st[i])
847                                 st->nvs->shutdown(st->nvs_st[i]);
848                 }
849         }
850         if (st->sts & NVS_STS_SPEW_MSG)
851                 dev_info(&client->dev, "%s\n", __func__);
852 }
853
854 static int ltr_remove(struct i2c_client *client)
855 {
856         struct ltr_state *st = i2c_get_clientdata(client);
857         unsigned int i;
858
859         if (st != NULL) {
860                 ltr_shutdown(client);
861                 if (st->nvs) {
862                         for (i = 0; i < LTR_DEV_N; i++) {
863                                 if (st->nvs_st[i])
864                                         st->nvs->remove(st->nvs_st[i]);
865                         }
866                 }
867                 if (st->dw.wq)
868                         destroy_workqueue(st->dw.wq);
869                 ltr_pm_exit(st);
870         }
871         dev_info(&client->dev, "%s\n", __func__);
872         return 0;
873 }
874
875 static void ltr_id_part(struct ltr_state *st, const char *part)
876 {
877         unsigned int i;
878
879         for (i = 0; i < LTR_DEV_N; i++)
880                 st->cfg[i].part = part;
881 }
882
883 static int ltr_id_dev(struct ltr_state *st, const char *name)
884 {
885         u8 val;
886         unsigned int i;
887         int ret;
888
889         if (!strcmp(name, LTR_NAME_LTR659PS)) {
890                 st->dev_id = LTR_DEVID_659PS;
891                 ltr_id_part(st, LTR_NAME_LTR659PS);
892         } else if (!strcmp(name, LTR_NAME_LTR558ALS)) {
893                 st->dev_id = LTR_DEVID_558ALS;
894                 ltr_id_part(st, LTR_NAME_LTR558ALS);
895         } else {
896                 ret = ltr_reset_sw(st);
897                 ret |= ltr_i2c_rd(st, LTR_REG_MANUFAC_ID, &val);
898                 if (ret)
899                         return -ENODEV;
900
901                 if (val != LTR_REG_MANUFAC_ID_VAL)
902                         return -ENODEV;
903
904                 ret = ltr_i2c_rd(st, LTR_REG_PART_ID, &val);
905                 if (ret)
906                         return -ENODEV;
907
908                 val &= LTR_REG_PART_ID_MASK;
909                 for (i = 0; i < ARRAY_SIZE(ltr_ids); i++) {
910                         if (val == ltr_ids[i]) {
911                                 st->dev_id = val;
912                                 break;
913                         }
914                 }
915                 if (i >= ARRAY_SIZE(ltr_ids)) {
916                         dev_err(&st->i2c->dev, "%s ERR: ID %x != %s\n",
917                                 __func__, val, name);
918                         return -ENODEV;
919                 }
920
921                 switch (st->dev_id) {
922                 case LTR_DEVID_659PS:
923                         ltr_id_part(st, LTR_NAME_LTR659PS);
924                         break;
925
926                 case LTR_DEVID_558ALS:
927                         ltr_id_part(st, LTR_NAME_LTR558ALS);
928                         break;
929
930                 default:
931                         return -ENODEV;
932                 }
933                 dev_info(&st->i2c->dev, "%s found %s for %s\n",
934                          __func__, st->cfg[0].part, name);
935         }
936
937         return 0;
938 }
939
940 static int ltr_id_i2c(struct ltr_state *st, const char *name)
941 {
942         int i;
943         int ret;
944
945         for (i = 0; i < ARRAY_SIZE(ltr_i2c_addrs); i++) {
946                 if (st->i2c->addr == ltr_i2c_addrs[i])
947                         break;
948         }
949
950         if (i < ARRAY_SIZE(ltr_i2c_addrs)) {
951                 st->i2c_addr = st->i2c->addr;
952                 ret = ltr_id_dev(st, name);
953         } else {
954                 name = LTR_NAME;
955                 for (i = 0; i < ARRAY_SIZE(ltr_i2c_addrs); i++) {
956                         st->i2c_addr = ltr_i2c_addrs[i];
957                         ret = ltr_id_dev(st, name);
958                         if (!ret)
959                                 break;
960                 }
961         }
962         if (ret)
963                 st->i2c_addr = 0;
964         return ret;
965 }
966
967 struct sensor_cfg ltr_cfg_dflt[] = {
968         {
969                 .name                   = NVS_LIGHT_STRING,
970                 .snsr_id                = LTR_DEV_LIGHT,
971                 .ch_n                   = 1,
972                 .ch_sz                  = 4,
973                 .part                   = LTR_NAME,
974                 .vendor                 = LTR_VENDOR,
975                 .version                = LTR_LIGHT_VERSION,
976                 .max_range              = {
977                         .ival           = LTR_LIGHT_MAX_RANGE_IVAL,
978                         .fval           = LTR_LIGHT_MAX_RANGE_MICRO,
979                 },
980                 .resolution             = {
981                         .ival           = LTR_LIGHT_RESOLUTION_IVAL,
982                         .fval           = LTR_LIGHT_RESOLUTION_MICRO,
983                 },
984                 .milliamp               = {
985                         .ival           = LTR_LIGHT_MILLIAMP_IVAL,
986                         .fval           = LTR_LIGHT_MILLIAMP_MICRO,
987                 },
988                 .delay_us_min           = LTR_POLL_DLY_MS_MIN * 1000,
989                 .delay_us_max           = LTR_POLL_DLY_MS_MAX * 1000,
990                 .flags                  = SENSOR_FLAG_ON_CHANGE_MODE,
991                 .scale                  = {
992                         .ival           = LTR_LIGHT_SCALE_IVAL,
993                         .fval           = LTR_LIGHT_SCALE_MICRO,
994                 },
995                 .thresh_lo              = LTR_LIGHT_THRESHOLD_DFLT,
996                 .thresh_hi              = LTR_LIGHT_THRESHOLD_DFLT,
997         },
998         {
999                 .name                   = NVS_PROXIMITY_STRING,
1000                 .snsr_id                = LTR_DEV_PROX,
1001                 .ch_n                   = 1,
1002                 .ch_sz                  = 4,
1003                 .part                   = LTR_NAME,
1004                 .vendor                 = LTR_VENDOR,
1005                 .version                = LTR_PROX_VERSION,
1006                 .max_range              = {
1007                         .ival           = LTR_PROX_MAX_RANGE_IVAL,
1008                         .fval           = LTR_PROX_MAX_RANGE_MICRO,
1009                 },
1010                 .resolution             = {
1011                         .ival           = LTR_PROX_RESOLUTION_IVAL,
1012                         .fval           = LTR_PROX_RESOLUTION_MICRO,
1013                 },
1014                 .milliamp               = {
1015                         .ival           = LTR_PROX_MILLIAMP_IVAL,
1016                         .fval           = LTR_PROX_MILLIAMP_MICRO,
1017                 },
1018                 .delay_us_min           = LTR_POLL_DLY_MS_MIN * 1000,
1019                 .delay_us_max           = LTR_POLL_DLY_MS_MAX * 1000,
1020                 .flags                  = SENSOR_FLAG_ON_CHANGE_MODE |
1021                                           SENSOR_FLAG_WAKE_UP,
1022                 .scale                  = {
1023                         .ival           = LTR_PROX_SCALE_IVAL,
1024                         .fval           = LTR_PROX_SCALE_MICRO,
1025                 },
1026                 .thresh_lo              = LTR_PROX_THRESHOLD_LO,
1027                 .thresh_hi              = LTR_PROX_THRESHOLD_HI,
1028         }
1029 };
1030
1031 static int ltr_of_dt(struct ltr_state *st, struct device_node *dn)
1032 {
1033         unsigned int i;
1034         int ret;
1035
1036         for (i = 0; i < LTR_DEV_N; i++)
1037                 memcpy(&st->cfg[i], &ltr_cfg_dflt[i], sizeof(st->cfg[0]));
1038         st->light.cfg = &st->cfg[LTR_DEV_LIGHT];
1039         st->light.hw_mask = 0xFFFF;
1040         st->light.nld_tbl = ltr_nld_tbl;
1041         st->prox.cfg = &st->cfg[LTR_DEV_PROX];
1042         st->prox.hw_mask = 0xFFFF;
1043         /* default device specific parameters */
1044         st->ps_contr = LTR_REG_PS_CONTR_DFLT;
1045         st->ps_led = LTR_REG_PS_LED_DFLT;
1046         st->ps_n_pulses = LTR_REG_PS_N_PULSES_DFLT;
1047         st->ps_meas_rate = LTR_REG_PS_MEAS_RATE_DFLT;
1048         st->als_meas_rate = LTR_REG_ALS_MEAS_RATE_DFLT;
1049         st->interrupt_persist = LTR_REG_INTERRUPT_PERSIST_DFLT;
1050         /* device tree parameters */
1051         if (dn) {
1052                 /* common NVS parameters */
1053                 for (i = 0; i < LTR_DEV_N; i++) {
1054                         ret = nvs_of_dt(dn, &st->cfg[i], NULL);
1055                         if (ret == -ENODEV)
1056                                 /* the entire device has been disabled */
1057                                 return -ENODEV;
1058                 }
1059
1060                 /* device specific parameters */
1061                 of_property_read_u8(dn, "register_ps_contr", &st->ps_contr);
1062                 st->ps_contr &= LTR_REG_PS_CONTR_POR_MASK;
1063                 of_property_read_u8(dn, "register_ps_led", &st->ps_led);
1064                 of_property_read_u8(dn, "register_ps_n_pulses",
1065                                     &st->ps_n_pulses);
1066                 of_property_read_u8(dn, "register_ps_meas_rate",
1067                                     &st->ps_meas_rate);
1068                 of_property_read_u8(dn, "register_als_meas_rate",
1069                                     &st->als_meas_rate);
1070                 of_property_read_u8(dn, "register_interrupt", &st->interrupt);
1071                 /* just interrupt polarity */
1072                 st->interrupt &= LTR_REG_INTERRUPT_POLARITY;
1073                 of_property_read_u8(dn, "register_interrupt_persist",
1074                                     &st->interrupt_persist);
1075         }
1076         /* this device supports these programmable parameters */
1077         if (nvs_light_of_dt(&st->light, dn, NULL)) {
1078                 st->light.nld_i_lo = 0;
1079                 st->light.nld_i_hi = ARRAY_SIZE(ltr_nld_tbl) - 1;
1080         }
1081         return 0;
1082 }
1083
1084 static int ltr_probe(struct i2c_client *client, const struct i2c_device_id *id)
1085 {
1086         struct ltr_state *st;
1087         unsigned long irqflags;
1088         unsigned int n;
1089         unsigned int i;
1090         int ret;
1091
1092         dev_info(&client->dev, "%s\n", __func__);
1093         st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
1094         if (st == NULL) {
1095                 dev_err(&client->dev, "%s devm_kzalloc ERR\n", __func__);
1096                 return -ENOMEM;
1097         }
1098
1099         i2c_set_clientdata(client, st);
1100         st->i2c = client;
1101         ret = ltr_of_dt(st, client->dev.of_node);
1102         if (ret) {
1103                 if (ret == -ENODEV) {
1104                         dev_info(&client->dev, "%s DT disabled\n", __func__);
1105                 } else {
1106                         dev_err(&client->dev, "%s _of_dt ERR\n", __func__);
1107                         ret = -ENODEV;
1108                 }
1109                 goto ltr_probe_exit;
1110         }
1111
1112         ltr_pm_init(st);
1113         ret = ltr_id_i2c(st, id->name);
1114         if (ret) {
1115                 dev_err(&client->dev, "%s _id_i2c ERR\n", __func__);
1116                 ret = -ENODEV;
1117                 goto ltr_probe_exit;
1118         }
1119
1120         ltr_pm(st, false);
1121         ltr_fn_dev.sts = &st->sts;
1122         ltr_fn_dev.errs = &st->errs;
1123         st->nvs = nvs_iio();
1124         if (st->nvs == NULL) {
1125                 ret = -ENODEV;
1126                 goto ltr_probe_exit;
1127         }
1128
1129         st->light.handler = st->nvs->handler;
1130         st->prox.handler = st->nvs->handler;
1131         n = 0;
1132         for (i = 0; i < LTR_DEV_N; i++) {
1133                 ret = st->nvs->probe(&st->nvs_st[i], st, &client->dev,
1134                                      &ltr_fn_dev, &st->cfg[i]);
1135                 if (!ret)
1136                         n++;
1137         }
1138         if (!n) {
1139                 dev_err(&client->dev, "%s nvs_probe ERR\n", __func__);
1140                 ret = -ENODEV;
1141                 goto ltr_probe_exit;
1142         }
1143
1144         st->light.nvs_st = st->nvs_st[LTR_DEV_LIGHT];
1145         st->prox.nvs_st = st->nvs_st[LTR_DEV_PROX];
1146         INIT_DELAYED_WORK(&st->dw, ltr_work);
1147         if (client->irq) {
1148                 irqflags = IRQF_ONESHOT;
1149                 if (st->interrupt & LTR_REG_INTERRUPT_POLARITY)
1150                         irqflags |= IRQF_TRIGGER_RISING;
1151                 else
1152                         irqflags |= IRQF_TRIGGER_FALLING;
1153                 for (i = 0; i < LTR_DEV_N; i++) {
1154                         if (st->cfg[i].snsr_id >= 0) {
1155                                 if (st->cfg[i].flags & SENSOR_FLAG_WAKE_UP)
1156                                         irqflags |= IRQF_NO_SUSPEND;
1157                         }
1158                 }
1159                 ret = request_threaded_irq(client->irq, NULL, ltr_irq_thread,
1160                                            irqflags, LTR_NAME, st);
1161                 if (ret) {
1162                         dev_err(&client->dev, "%s req_threaded_irq ERR %d\n",
1163                                 __func__, ret);
1164                         ret = -ENOMEM;
1165                         goto ltr_probe_exit;
1166                 }
1167         }
1168
1169         dev_info(&client->dev, "%s done\n", __func__);
1170         return 0;
1171
1172 ltr_probe_exit:
1173         ltr_remove(client);
1174         return ret;
1175 }
1176
1177 static const struct i2c_device_id ltr_i2c_device_id[] = {
1178         { LTR_NAME, 0 },
1179         { LTR_NAME_LTR659PS, 0 },
1180         { LTR_NAME_LTR558ALS, 0 },
1181         {}
1182 };
1183
1184 MODULE_DEVICE_TABLE(i2c, ltr_i2c_device_id);
1185
1186 static const struct of_device_id ltr_of_match[] = {
1187         { .compatible = "liteon,ltrX5X", },
1188         { .compatible = "liteon,ltr659ps", },
1189         { .compatible = "liteon,ltr558als", },
1190         {},
1191 };
1192
1193 MODULE_DEVICE_TABLE(of, ltr_of_match);
1194
1195 static struct i2c_driver ltr_driver = {
1196         .class          = I2C_CLASS_HWMON,
1197         .probe          = ltr_probe,
1198         .remove         = ltr_remove,
1199         .shutdown       = ltr_shutdown,
1200         .driver = {
1201                 .name           = LTR_NAME,
1202                 .owner          = THIS_MODULE,
1203                 .of_match_table = of_match_ptr(ltr_of_match),
1204                 .pm             = &ltr_pm_ops,
1205         },
1206         .id_table       = ltr_i2c_device_id,
1207 };
1208 module_i2c_driver(ltr_driver);
1209
1210 MODULE_LICENSE("GPL");
1211 MODULE_DESCRIPTION("LTR659PS driver");
1212 MODULE_AUTHOR("NVIDIA Corporation");