e688663ac4540e430cd481fd7256898bc9c0f22d
[linux-3.10.git] / drivers / iio / light / nvs_bh1730fvc.c
1 /* Copyright (c) 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
17
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/debugfs.h>
23 #include <linux/fs.h>
24 #include <asm/uaccess.h>
25 #include <linux/delay.h>
26 #include <linux/ktime.h>
27 #include <linux/string.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/workqueue.h>
30 #include <linux/of.h>
31 #include <linux/nvs.h>
32 #include <linux/nvs_light.h>
33
34 #define BH1730_VENDOR                   "ROHM"
35 #define BH1730_NAME                     "bh1730fvc"
36 #define BH1730_LIGHT_VERSION            (1)
37 #define BH1730_LIGHT_MAX_RANGE_IVAL     (100000)
38 #define BH1730_LIGHT_MAX_RANGE_MICRO    (0)
39 #define BH1730_LIGHT_RESOLUTION_IVAL    (1)
40 #define BH1730_LIGHT_RESOLUTION_MICRO   (0)
41 #define BH1730_LIGHT_MILLIAMP_IVAL      (0)
42 #define BH1730_LIGHT_MILLIAMP_MICRO     (90000)
43 #define BH1730_LIGHT_SCALE_IVAL         (0)
44 #define BH1730_LIGHT_SCALE_MICRO        (0)
45 #define BH1730_LIGHT_OFFSET_IVAL        (0)
46 #define BH1730_LIGHT_OFFSET_MICRO       (0)
47 #define BH1730_LIGHT_THRESHOLD_LO       (1)
48 #define BH1730_LIGHT_THRESHOLD_HI       (1)
49 #define BH1730_POLL_DLY_US_MIN          (2700)
50 #define BH1730_POLL_DLY_US_MAX          (688500)
51
52 /* HW registers */
53 #define BH1730_COMMAND                  (0x80)
54
55 #define BH1730_REG_CONTROL              (0x00)
56 #define BH1730_REG_TIMING               (0x01)
57 #define BH1730_REG_INTERRUPT            (0x02)
58 #define BH1730_REG_THLLOW               (0x03)
59 #define BH1730_REG_THLHIGH              (0x04)
60 #define BH1730_REG_THHLOW               (0x05)
61 #define BH1730_REG_THHHIGH              (0x06)
62 #define BH1730_REG_GAIN                 (0x07)
63 #define BH1730_REG_ID                   (0x12)
64 #define BH1730_REG_DATA0LOW             (0x14)
65 #define BH1730_REG_DATA0HIGH            (0x15)
66 #define BH1730_REG_DATA1LOW             (0x16)
67 #define BH1730_REG_DATA1HIGH            (0x17)
68
69 #define BIT_ADC_INTR    0x20
70 #define BIT_ADC_VALID   0x10
71 #define BIT_ONE_TIME    0x08
72 #define BIT_DATA_SEL    0x04
73 #define BIT_ADC_EN      0x02
74 #define BIT_POWER       0x01
75
76 #define BH1730_HW_DELAY_MS              (10)
77
78 enum BH1730_GAIN {
79         BH1730_GAIN_X1 = 0,
80         BH1730_GAIN_X2,
81         BH1730_GAIN_X64,
82         BH1730_GAIN_X128,
83 };
84
85 /* regulator names in order of powering on */
86 static char *bh1730_vregs[] = {
87         "vdd",
88         "vid",
89 };
90
91 struct bh1730_state {
92         struct i2c_client *i2c;
93         struct nvs_fn_if *nvs;
94         void *nvs_st;
95         struct sensor_cfg cfg;
96         struct delayed_work dw;
97         struct regulator_bulk_data vreg[ARRAY_SIZE(bh1730_vregs)];
98         struct nvs_light light;
99         unsigned int sts;               /* debug flags */
100         unsigned int errs;              /* error count */
101         unsigned int enabled;           /* enable status */
102         bool hw_change;                 /* HW changed so drop first sample */
103         u32 itime_us;
104         u32 gain;
105         u16 data0;
106         u16 data1;
107         u8 cached_reg_addr;
108         int always_on;
109 };
110
111 static s64 bh1730_get_time_ns(void)
112 {
113         struct timespec ts;
114
115         ktime_get_ts(&ts);
116         return timespec_to_ns(&ts);
117 }
118
119 static int bh1730_i2c_wr(struct bh1730_state *st, u8 reg, u8 val)
120 {
121         int ret;
122         ret = i2c_smbus_write_byte_data(st->i2c, reg | BH1730_COMMAND, val);
123         if (ret)
124                 return ret;
125
126         if (reg == BH1730_REG_GAIN) {
127                 if (val == BH1730_GAIN_X1)
128                         st->gain = 1;
129                 else if (val == BH1730_GAIN_X2)
130                         st->gain = 2;
131                 else if (val == BH1730_GAIN_X64)
132                         st->gain = 64;
133                 else if (val == BH1730_GAIN_X128)
134                         st->gain = 128;
135         } else if (reg == BH1730_REG_TIMING)
136                 st->itime_us = (256 - val) * 2700;
137
138         return 0;
139 }
140
141 static int bh1730_i2c_rd(struct bh1730_state *st, u8 reg, u8 *val)
142 {
143         int ret;
144         ret = i2c_smbus_read_byte_data(st->i2c, reg | BH1730_COMMAND);
145         if (ret < 0)
146                 return -EIO;
147         *val = (u8)ret;
148         return 0;
149 }
150
151 static int bh1730_read_n(struct bh1730_state *st, u8 reg, u8 *val, int n)
152 {
153         int ret;
154         ret = i2c_smbus_read_i2c_block_data(st->i2c, reg | BH1730_COMMAND,
155                                             n, val);
156         if (ret != n)
157                 return -EIO;
158         return 0;
159 }
160
161 static int bh1730_rd_data(struct bh1730_state *st, u16 *val)
162 {
163         u8 data[4];
164         if (bh1730_read_n(st, BH1730_REG_DATA0LOW, data, 4)) {
165                 dev_err(&st->i2c->dev, "%s: bh1730_read_n failure\n", __func__);
166                 return -EIO;
167         } else {
168                 val[0] = ((data[1] << 8) | data[0]);
169                 val[1] = ((data[3] << 8) | data[2]);
170                 st->data0 = val[0];
171                 st->data1 = val[1];
172         }
173
174         return 0;
175 }
176
177 static int bh1730_pm(struct bh1730_state *st, bool enable)
178 {
179         int ret;
180
181         if (enable) {
182                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
183                                        ARRAY_SIZE(bh1730_vregs));
184                 if (ret)
185                         mdelay(BH1730_HW_DELAY_MS);
186                 ret = bh1730_i2c_wr(st, BH1730_REG_CONTROL,
187                                     BIT_ADC_EN | BIT_POWER);
188         } else {
189                 ret = nvs_vregs_sts(st->vreg, ARRAY_SIZE(bh1730_vregs));
190                 if ((ret < 0) || (ret == ARRAY_SIZE(bh1730_vregs))) {
191                         ret = bh1730_i2c_wr(st, BH1730_REG_CONTROL, 0);
192                 } else if (ret > 0) {
193                         nvs_vregs_enable(&st->i2c->dev, st->vreg,
194                                          ARRAY_SIZE(bh1730_vregs));
195                         mdelay(BH1730_HW_DELAY_MS);
196                         ret = bh1730_i2c_wr(st, BH1730_REG_CONTROL, 0);
197                 }
198                 ret |= nvs_vregs_disable(&st->i2c->dev, st->vreg,
199                                          ARRAY_SIZE(bh1730_vregs));
200         }
201         if (ret > 0)
202                 ret = 0;
203         if (ret) {
204                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
205                         __func__, enable, ret);
206         } else {
207                 if (st->sts & NVS_STS_SPEW_MSG)
208                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
209                                  __func__, enable);
210         }
211         return ret;
212 }
213
214 static void bh1730_pm_exit(struct bh1730_state *st)
215 {
216         bh1730_pm(st, false);
217         nvs_vregs_exit(&st->i2c->dev, st->vreg, ARRAY_SIZE(bh1730_vregs));
218 }
219
220 static int bh1730_pm_init(struct bh1730_state *st)
221 {
222         int ret;
223
224         st->enabled = 0;
225         nvs_vregs_init(&st->i2c->dev,
226                        st->vreg, ARRAY_SIZE(bh1730_vregs), bh1730_vregs);
227         /* on off for low power mode if regulator still on */
228         ret = bh1730_pm(st, true);
229         ret |= bh1730_pm(st, false);
230         return ret;
231 }
232
233 static int bh1730_init(struct bh1730_state *st)
234 {
235         int ret;
236
237         /* itime 100ms */
238         ret = bh1730_i2c_wr(st, BH1730_REG_TIMING, 0xDA);
239         /* gain X1 */
240         ret |= bh1730_i2c_wr(st, BH1730_REG_GAIN, BH1730_GAIN_X1);
241         if (!ret)
242                 st->hw_change = true;
243         if (st->sts & NVS_STS_SPEW_MSG)
244                 dev_info(&st->i2c->dev, "%s itime_us=%d gain=%d err=%d\n",
245                          __func__, st->itime_us, st->gain, ret);
246         return ret;
247 }
248
249 static u32 bh1730_get_lux(struct bh1730_state *st, u16 *data)
250 {
251         u32 lux;
252         if (data[1] * 1000 / data[0] < 260)
253                 lux = 1290 * data[0] - 2733 * data[1];
254         else if (data[1] * 1000 / data[0] < 550)
255                 lux = 795 * data[0] - 859 * data[1];
256         else if (data[1] * 1000 / data[0] < 1090)
257                 lux = 510 * data[0] - 345 * data[1];
258         else if (data[1] * 1000 / data[0] < 2130)
259                 lux = 276 * data[0] - 130 * data[1];
260         else
261                 return 0;
262         lux = lux * 100 / (st->gain * st->itime_us);
263
264         return lux;
265 }
266
267 static int bh1730_rd(struct bh1730_state *st)
268 {
269         u16 hw[2];
270         s64 ts;
271         struct nvs_light *nl = &st->light;
272         int ret;
273
274         if (st->hw_change) {
275                 /* drop first sample after HW change */
276                 st->hw_change = false;
277                 return 0;
278         }
279
280         ret = bh1730_rd_data(st, hw);
281         if (ret)
282                 return ret;
283
284         ts = bh1730_get_time_ns();
285         nl->hw = bh1730_get_lux(st, hw);
286         nl->timestamp = ts;
287         nvs_light_read(nl);
288
289         if (nl->nld_i_change)
290                 bh1730_init(st);
291         return 0;
292 }
293
294 static void bh1730_read(struct bh1730_state *st)
295 {
296         st->nvs->nvs_mutex_lock(st->nvs_st);
297         if (st->enabled) {
298                 bh1730_rd(st);
299                 schedule_delayed_work(&st->dw,
300                                     msecs_to_jiffies(st->light.poll_delay_ms));
301         }
302         st->nvs->nvs_mutex_unlock(st->nvs_st);
303 }
304
305 static void bh1730_work(struct work_struct *ws)
306 {
307         struct bh1730_state *st = container_of((struct delayed_work *)ws,
308                                            struct bh1730_state, dw);
309
310         bh1730_read(st);
311 }
312
313 static int bh1730_disable(struct bh1730_state *st)
314 {
315         int ret;
316
317         if (st->always_on)
318                 return 0;
319
320         cancel_delayed_work(&st->dw);
321         ret = bh1730_pm(st, false);
322         if (!ret)
323                 st->enabled = 0;
324         return ret;
325 }
326
327 static int bh1730_enable(void *client, int snsr_id, int enable)
328 {
329         struct bh1730_state *st = (struct bh1730_state *)client;
330         unsigned int ms;
331         int ret;
332
333         if (enable < 0)
334                 return st->enabled;
335
336         if (enable) {
337                 ret = bh1730_pm(st, true);
338                 if (!ret) {
339                         ret = bh1730_init(st);
340                         st->light.cfg->delay_us_min = st->itime_us;
341                         st->light.cfg->delay_us_max = st->itime_us;
342                         nvs_light_enable(&st->light);
343                         if (ret) {
344                                 bh1730_disable(st);
345                         } else {
346                                 st->enabled = 1;
347                                 ms = st->light.poll_delay_ms;
348                                 schedule_delayed_work(&st->dw,
349                                                       msecs_to_jiffies(ms));
350                         }
351                 }
352         } else {
353                 ret = bh1730_disable(st);
354         }
355         return ret;
356 }
357
358 static int bh1730_batch(void *client, int snsr_id, int flags,
359                     unsigned int period, unsigned int timeout)
360 {
361         struct bh1730_state *st = (struct bh1730_state *)client;
362
363         if (timeout)
364                 /* timeout not supported (no HW FIFO) */
365                 return -EINVAL;
366
367         st->light.delay_us = period;
368         return 0;
369 }
370
371 static int bh1730_regs(void *client, int snsr_id, char *buf)
372 {
373         struct bh1730_state *st = (struct bh1730_state *)client;
374         ssize_t t;
375         int ret;
376         u8 readval;
377         u8 i;
378         u8 regs[] = {
379                 BH1730_REG_CONTROL,
380                 BH1730_REG_TIMING,
381                 BH1730_REG_INTERRUPT,
382                 BH1730_REG_THLLOW,
383                 BH1730_REG_THLHIGH,
384                 BH1730_REG_THHLOW,
385                 BH1730_REG_THHHIGH,
386                 BH1730_REG_GAIN,
387                 BH1730_REG_ID,
388                 BH1730_REG_DATA0LOW,
389                 BH1730_REG_DATA0HIGH,
390                 BH1730_REG_DATA1LOW,
391                 BH1730_REG_DATA1HIGH
392         };
393
394         t = sprintf(buf, "lux: %u\n", st->light.lux);
395         t += sprintf(buf, "registers:\n");
396         for (i = 0; i < ARRAY_SIZE(regs); i++) {
397                 ret = bh1730_i2c_rd(st, regs[i], &readval);
398                 if (ret)
399                         t += sprintf(buf + t, "0x%hhx=ERR\n", i);
400                 else
401                         t += sprintf(buf + t, "0x%hhx=0x%hhx\n", i, readval);
402         }
403         return t;
404 }
405
406 static int bh1730_nvs_write(void *client, int snsr_id, unsigned int nvs)
407 {
408         struct bh1730_state *st = (struct bh1730_state *)client;
409         u8 reg;
410         u8 val;
411
412         reg = (nvs >> 16) && 0xff;
413         val = (nvs >> 8) && 0xff;
414         return bh1730_i2c_wr(st, reg, val);
415 }
416
417 static int bh1730_nvs_read(void *client, int snsr_id, char *buf)
418 {
419         struct bh1730_state *st = (struct bh1730_state *)client;
420
421         return nvs_light_dbg(&st->light, buf);
422 }
423
424 static struct nvs_fn_dev bh1730_fn_dev = {
425         .enable                         = bh1730_enable,
426         .batch                          = bh1730_batch,
427         .regs                           = bh1730_regs,
428         .nvs_write                      = bh1730_nvs_write,
429         .nvs_read                       = bh1730_nvs_read,
430 };
431
432 static int bh1730_suspend(struct device *dev)
433 {
434         struct i2c_client *client = to_i2c_client(dev);
435         struct bh1730_state *st = i2c_get_clientdata(client);
436         int ret = 0;
437
438         if (st->always_on)
439                 return 0;
440
441         st->sts |= NVS_STS_SUSPEND;
442         if (st->nvs && st->nvs_st)
443                 ret = st->nvs->suspend(st->nvs_st);
444         if (st->sts & NVS_STS_SPEW_MSG)
445                 dev_info(&client->dev, "%s\n", __func__);
446         return ret;
447 }
448
449 static int bh1730_resume(struct device *dev)
450 {
451         struct i2c_client *client = to_i2c_client(dev);
452         struct bh1730_state *st = i2c_get_clientdata(client);
453         int ret = 0;
454
455         if (st->always_on)
456                 return 0;
457
458         if (st->nvs && st->nvs_st)
459                 ret = st->nvs->resume(st->nvs_st);
460         st->sts &= ~NVS_STS_SUSPEND;
461         if (st->sts & NVS_STS_SPEW_MSG)
462                 dev_info(&client->dev, "%s\n", __func__);
463         return ret;
464 }
465
466 static SIMPLE_DEV_PM_OPS(bh1730_pm_ops, bh1730_suspend, bh1730_resume);
467
468 static void bh1730_shutdown(struct i2c_client *client)
469 {
470         struct bh1730_state *st = i2c_get_clientdata(client);
471
472         st->sts |= NVS_STS_SHUTDOWN;
473         if (st->nvs && st->nvs_st)
474                 st->nvs->shutdown(st->nvs_st);
475         if (st->sts & NVS_STS_SPEW_MSG)
476                 dev_info(&client->dev, "%s\n", __func__);
477 }
478
479 static int bh1730_remove(struct i2c_client *client)
480 {
481         struct bh1730_state *st = i2c_get_clientdata(client);
482
483         if (st != NULL) {
484                 bh1730_shutdown(client);
485                 if (st->nvs && st->nvs_st)
486                         st->nvs->remove(st->nvs_st);
487                 if (st->dw.wq)
488                         destroy_workqueue(st->dw.wq);
489                 bh1730_pm_exit(st);
490         }
491         dev_info(&client->dev, "%s\n", __func__);
492         return 0;
493 }
494
495 static struct sensor_cfg bh1730_cfg_dflt = {
496         .name                   = NVS_LIGHT_STRING,
497         .ch_n                   = 1,
498         .ch_sz                  = 4,
499         .part                   = BH1730_NAME,
500         .vendor                 = BH1730_VENDOR,
501         .version                = BH1730_LIGHT_VERSION,
502         .max_range              = {
503                 .ival           = BH1730_LIGHT_MAX_RANGE_IVAL,
504                 .fval           = BH1730_LIGHT_MAX_RANGE_MICRO,
505         },
506         .resolution             = {
507                 .ival           = BH1730_LIGHT_RESOLUTION_IVAL,
508                 .fval           = BH1730_LIGHT_RESOLUTION_MICRO,
509         },
510         .milliamp               = {
511                 .ival           = BH1730_LIGHT_MILLIAMP_IVAL,
512                 .fval           = BH1730_LIGHT_MILLIAMP_MICRO,
513         },
514         .delay_us_min           = BH1730_POLL_DLY_US_MIN,
515         .delay_us_max           = BH1730_POLL_DLY_US_MAX,
516         .flags                  = SENSOR_FLAG_ON_CHANGE_MODE,
517         .scale                  = {
518                 .ival           = BH1730_LIGHT_SCALE_IVAL,
519                 .fval           = BH1730_LIGHT_SCALE_MICRO,
520         },
521         .thresh_lo              = BH1730_LIGHT_THRESHOLD_LO,
522         .thresh_hi              = BH1730_LIGHT_THRESHOLD_HI,
523 };
524
525 static int bh1730_of_dt(struct bh1730_state *st, struct device_node *dn)
526 {
527         int ret;
528
529         /* default NVS programmable parameters */
530         memcpy(&st->cfg, &bh1730_cfg_dflt, sizeof(st->cfg));
531         st->light.cfg = &st->cfg;
532         st->light.hw_mask = 0xFFFF;
533         /* device tree parameters */
534
535         if (dn) {
536                 ret = nvs_of_dt(dn, &st->cfg, NULL);
537                 if (ret == -ENODEV)
538                         return ret;
539         }
540
541         return 0;
542 }
543
544 static ssize_t bh1730_debugfs_read_lux(struct file *file,
545                         char __user *userbuf, size_t count, loff_t *ppos)
546 {
547         struct bh1730_state *st = file->private_data;
548         char buf[20];
549         ssize_t len;
550
551         len = snprintf(buf, sizeof(buf), "lux: %u\n", st->light.hw);
552
553         return simple_read_from_buffer(userbuf, count, ppos, buf, len);
554 }
555
556 static const struct file_operations bh1730_debugfs_lux_fops = {
557         .open = simple_open,
558         .read = bh1730_debugfs_read_lux,
559 };
560
561 static ssize_t bh1730_debugfs_read_reg(struct file *file,
562                         char __user *userbuf, size_t count, loff_t *ppos)
563 {
564         struct bh1730_state *st = file->private_data;
565         char buf[256];
566         u8 val = 0;
567         ssize_t len;
568         int ret, i;
569
570         u8 regs[] = {
571                 BH1730_REG_CONTROL,
572                 BH1730_REG_TIMING,
573                 BH1730_REG_INTERRUPT,
574                 BH1730_REG_THLLOW,
575                 BH1730_REG_THLHIGH,
576                 BH1730_REG_THHLOW,
577                 BH1730_REG_THHHIGH,
578                 BH1730_REG_GAIN,
579                 BH1730_REG_ID,
580                 BH1730_REG_DATA0LOW,
581                 BH1730_REG_DATA0HIGH,
582                 BH1730_REG_DATA1LOW,
583                 BH1730_REG_DATA1HIGH
584         };
585
586         len = sprintf(buf, "lux: %u\n", st->light.hw);
587         len += sprintf(buf + len, "registers:\n");
588         for (i = 0; i < ARRAY_SIZE(regs); i++) {
589                 ret = bh1730_i2c_rd(st, regs[i], &val);
590                 if (ret)
591                         len += sprintf(buf + len, "0x%02hhx=ERR\n", regs[i]);
592                 else
593                         len += sprintf(buf + len, "0x%02hhx=0x%02hhx\n",
594                                         regs[i], val);
595         }
596         buf[len++] = 0;
597
598         return simple_read_from_buffer(userbuf, count, ppos, buf, len);
599 }
600
601 static ssize_t bh1730_debugfs_write_reg(struct file *file,
602                      const char __user *userbuf, size_t count, loff_t *ppos)
603 {
604         struct bh1730_state *st = file->private_data;
605         unsigned reg, val;
606         char buf[80];
607         int ret;
608
609         count = min_t(size_t, count, (sizeof(buf)-1));
610         if (copy_from_user(buf, userbuf, count))
611                 return -EFAULT;
612
613         buf[count] = 0;
614
615         ret = sscanf(buf, "%i %i", &reg, &val);
616
617         switch (ret) {
618         case 1:
619                 st->cached_reg_addr = reg;
620                 break;
621         case 2:
622                 st->cached_reg_addr = reg;
623                 ret = bh1730_i2c_wr(st, (u8)reg, (u8)val);
624                 if (ret) {
625                         dev_err(&st->i2c->dev, "%s: write failed\n", __func__);
626                         return ret;
627                 }
628                 break;
629         default:
630                 return -EINVAL;
631         }
632
633         return count;
634 }
635
636 static const struct file_operations bh1730_debugfs_reg_fops = {
637         .open = simple_open,
638         .read = bh1730_debugfs_read_reg,
639         .write = bh1730_debugfs_write_reg,
640 };
641
642 static ssize_t bh1730_debugfs_read_alwayson(struct file *file,
643                         char __user *userbuf, size_t count, loff_t *ppos)
644 {
645         struct bh1730_state *st = file->private_data;
646         char buf[16];
647         ssize_t len;
648
649         len = snprintf(buf, sizeof(buf), "%d\n", st->always_on);
650
651         return simple_read_from_buffer(userbuf, count, ppos, buf, len);
652 }
653
654 static ssize_t bh1730_debugfs_write_alwayson(struct file *file,
655                      const char __user *userbuf, size_t count, loff_t *ppos)
656 {
657         struct bh1730_state *st = file->private_data;
658         int val;
659         char buf[16];
660
661         count = min_t(size_t, count, (sizeof(buf)-1));
662         if (copy_from_user(buf, userbuf, count))
663                 return -EFAULT;
664
665         buf[count] = 0;
666
667         if (!kstrtoint(buf, 0, &val))
668                 st->always_on = val;
669
670         if (st->always_on && !st->enabled)
671                 bh1730_enable(st, 0, 1);
672
673         return count;
674 }
675
676 static const struct file_operations bh1730_debugfs_alwayson_fops = {
677         .open = simple_open,
678         .read = bh1730_debugfs_read_alwayson,
679         .write = bh1730_debugfs_write_alwayson,
680 };
681
682 static int bh1730_probe(struct i2c_client *client,
683                     const struct i2c_device_id *id)
684 {
685         struct bh1730_state *st;
686         struct dentry *debugfs_root;
687         int ret;
688
689         dev_info(&client->dev, "%s\n", __func__);
690         st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
691         if (st == NULL) {
692                 dev_err(&client->dev, "%s devm_kzalloc ERR\n", __func__);
693                 return -ENOMEM;
694         }
695
696         i2c_set_clientdata(client, st);
697         st->i2c = client;
698         ret = bh1730_of_dt(st, client->dev.of_node);
699         if (ret) {
700                 if (ret == -ENODEV) {
701                         dev_info(&client->dev, "%s DT disabled\n", __func__);
702                 } else {
703                         dev_err(&client->dev, "%s _of_dt ERR\n", __func__);
704                         ret = -ENODEV;
705                 }
706                 goto bh1730_probe_exit;
707         }
708
709         ret = bh1730_pm_init(st);
710         if (ret) {
711                 dev_err(&client->dev, "%s _pm_init ERR\n", __func__);
712                 ret = -ENODEV;
713                 goto bh1730_probe_exit;
714         }
715
716         bh1730_fn_dev.errs = &st->errs;
717         bh1730_fn_dev.sts = &st->sts;
718         st->nvs = nvs_iio();
719         if (st->nvs == NULL) {
720                 dev_err(&client->dev, "%s nvs_iio ERR\n", __func__);
721                 ret = -ENODEV;
722                 goto bh1730_probe_exit;
723         }
724
725         st->light.handler = st->nvs->handler;
726         ret = st->nvs->probe(&st->nvs_st, st, &client->dev,
727                              &bh1730_fn_dev, &st->cfg);
728         if (ret) {
729                 dev_err(&client->dev, "%s nvs_probe ERR\n", __func__);
730                 ret = -ENODEV;
731                 goto bh1730_probe_exit;
732         }
733
734         debugfs_root = debugfs_create_dir(BH1730_NAME, NULL);
735         if (debugfs_root) {
736                 debugfs_create_file("lux", 0444, debugfs_root, st,
737                                     &bh1730_debugfs_lux_fops);
738                 debugfs_create_file("reg", 0644, debugfs_root, st,
739                                     &bh1730_debugfs_reg_fops);
740                 debugfs_create_file("alwayson", 0644, debugfs_root, st,
741                                     &bh1730_debugfs_alwayson_fops);
742         }
743
744         st->light.nvs_st = st->nvs_st;
745         INIT_DELAYED_WORK(&st->dw, bh1730_work);
746         dev_info(&client->dev, "%s done\n", __func__);
747         return 0;
748
749 bh1730_probe_exit:
750         bh1730_remove(client);
751         return ret;
752 }
753
754 static const struct i2c_device_id bh1730_i2c_device_id[] = {
755         { BH1730_NAME, 0 },
756         {}
757 };
758
759 MODULE_DEVICE_TABLE(i2c, bh1730_i2c_device_id);
760
761 static const struct of_device_id bh1730_of_match[] = {
762         { .compatible = "rohm,bh1730fvc", },
763         {},
764 };
765
766 MODULE_DEVICE_TABLE(of, bh1730_of_match);
767
768 static struct i2c_driver bh1730_driver = {
769         .class          = I2C_CLASS_HWMON,
770         .probe          = bh1730_probe,
771         .remove         = bh1730_remove,
772         .shutdown       = bh1730_shutdown,
773         .driver = {
774                 .name           = BH1730_NAME,
775                 .owner          = THIS_MODULE,
776                 .of_match_table = of_match_ptr(bh1730_of_match),
777                 .pm             = &bh1730_pm_ops,
778         },
779         .id_table       = bh1730_i2c_device_id,
780 };
781 module_i2c_driver(bh1730_driver);
782
783 MODULE_LICENSE("GPL");
784 MODULE_DESCRIPTION("BH1730FVC driver");
785 MODULE_AUTHOR("NVIDIA Corporation");