d879ee7efaa0bb463051752d720653b0ef719fb8
[linux-3.10.git] / drivers / iio / light / nvs_cm3217.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
17
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
23 #include <linux/ktime.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/workqueue.h>
26 #include <linux/of.h>
27 #include <linux/nvs.h>
28 #include <linux/nvs_light.h>
29
30 #define CM_VENDOR                       "Capella Microsystems, Inc."
31 #define CM_NAME                         "cm3217"
32 #define CM_LIGHT_VERSION                (1)
33 #define CM_LIGHT_MAX_RANGE_IVAL         (119154)
34 #define CM_LIGHT_MAX_RANGE_MICRO        (420000)
35 #define CM_LIGHT_RESOLUTION_IVAL        (0)
36 #define CM_LIGHT_RESOLUTION_MICRO       (18750)
37 #define CM_LIGHT_MILLIAMP_IVAL          (0)
38 #define CM_LIGHT_MILLIAMP_MICRO         (90000)
39 #define CM_LIGHT_SCALE_IVAL             (0)
40 #define CM_LIGHT_SCALE_MICRO            (10000)
41 #define CM_LIGHT_OFFSET_IVAL            (0)
42 #define CM_LIGHT_OFFSET_MICRO           (0)
43 #define CM_LIGHT_THRESHOLD_LO           (5)
44 #define CM_LIGHT_THRESHOLD_HI           (5)
45 #define CM_POLL_DLY_MS_MIN              (100)
46 #define CM_POLL_DLY_MS_MAX              (4000)
47 /* HW registers */
48 #define CM_I2C_ADDR_CMD1_WR             (0x10)
49 #define CM_I2C_ADDR_CMD2_WR             (0x11)
50 #define CM_I2C_ADDR_RD                  (0x10)
51 #define CM_HW_CMD1_DFLT                 (0x22)
52 #define CM_HW_CMD1_BIT_SD               (0)
53 #define CM_HW_CMD1_BIT_IT_T             (2)
54 #define CM_HW_CMD1_IT_T_MASK            (0x0C)
55 #define CM_HW_CMD2_BIT_FD_IT            (5)
56 #define CM_HW_CMD2_FD_IT_MASK           (0xE0)
57 #define CM_HW_DELAY_MS                  (10)
58
59
60 /* regulator names in order of powering on */
61 static char *cm_vregs[] = {
62         "vdd",
63 };
64
65 static struct nvs_light_dynamic cm_nld_tbl[] = {
66         {{0, 18750},  {1228,   780000}, {0, 90000}, 3210, (0 << 5) | (3 << 2)},
67         {{0, 37500},  {2457,   560000}, {0, 90000}, 1610, (0 << 5) | (2 << 2)},
68         {{0, 75000},  {4915,   120000}, {0, 90000}, 810,  (0 << 5) | (1 << 2)},
69         {{0, 150000}, {9830,   250000}, {0, 90000}, 410,  (1 << 5) | (1 << 2)},
70         {{0, 225564}, {14782,  70000},  {0, 90000}, 276,  (2 << 5) | (1 << 2)},
71         {{0, 300000}, {19660,  500000}, {0, 90000}, 210,  (3 << 5) | (1 << 2)},
72         {{0, 461539}, {30246,  360000}, {0, 90000}, 140,  (4 << 5) | (1 << 2)},
73         {{0, 600000}, {39321,  0},      {0, 90000}, 110,  (5 << 5) | (1 << 2)},
74         {{0, 750000}, {49151,  250000}, {0, 90000}, 90,   (6 << 5) | (1 << 2)},
75         {{0, 909091}, {59577,  210000}, {0, 90000}, 76,   (7 << 5) | (1 << 2)},
76         {{1, 818181}, {119154, 420000}, {0, 90000}, 43,   (7 << 5) | (0 << 2)}
77 };
78
79 struct cm_state {
80         struct i2c_client *i2c;
81         struct nvs_fn_if *nvs;
82         void *nvs_st;
83         struct sensor_cfg cfg;
84         struct delayed_work dw;
85         struct regulator_bulk_data vreg[ARRAY_SIZE(cm_vregs)];
86         struct nvs_light light;
87         unsigned int sts;               /* debug flags */
88         unsigned int errs;              /* error count */
89         unsigned int enabled;           /* enable status */
90         bool hw_change;                 /* HW changed so drop first sample */
91         u8 cmd1;                        /* store for register dump */
92         u8 cmd2;                        /* store for register dump */
93 };
94
95
96 static s64 cm_get_time_ns(void)
97 {
98         struct timespec ts;
99
100         ktime_get_ts(&ts);
101         return timespec_to_ns(&ts);
102 }
103
104 static void cm_err(struct cm_state *st)
105 {
106         st->errs++;
107         if (!st->errs)
108                 st->errs--;
109 }
110
111 static int cm_i2c_rd(struct cm_state *st, u16 *val)
112 {
113         struct i2c_msg msg[2];
114         u8 buf[2];
115
116         msg[0].addr = CM_I2C_ADDR_RD + 1;
117         msg[0].flags = I2C_M_RD;
118         msg[0].len = 1;
119         msg[0].buf = &buf[0];
120         msg[1].addr = CM_I2C_ADDR_RD;
121         msg[1].flags = I2C_M_RD;
122         msg[1].len = 1;
123         msg[1].buf = &buf[1];
124         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
125                 cm_err(st);
126                 return -EIO;
127         }
128
129         *val = (__u16)((buf[1] << 8) | buf[0]);
130         return 0;
131 }
132
133 static int cm_i2c_wr(struct cm_state *st, u8 cmd1, u8 cmd2)
134 {
135         struct i2c_msg msg[2];
136         u8 buf[2];
137
138         buf[0] = cmd1;
139         buf[1] = cmd2;
140         msg[0].addr = CM_I2C_ADDR_CMD1_WR;
141         msg[0].flags = 0;
142         msg[0].len = 1;
143         msg[0].buf = &buf[0];
144         msg[1].addr = CM_I2C_ADDR_CMD2_WR;
145         msg[1].flags = 0;
146         msg[1].len = 1;
147         msg[1].buf = &buf[1];
148         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
149                 cm_err(st);
150                 return -EIO;
151         }
152
153         st->cmd1 = cmd1;
154         st->cmd2 = cmd2;
155         return 0;
156 }
157
158 static int cm_pm(struct cm_state *st, bool enable)
159 {
160         int ret;
161
162         if (enable) {
163                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
164                                        ARRAY_SIZE(cm_vregs));
165                 if (ret)
166                         mdelay(CM_HW_DELAY_MS);
167         } else {
168                 ret = nvs_vregs_sts(st->vreg, ARRAY_SIZE(cm_vregs));
169                 if ((ret < 0) || (ret == ARRAY_SIZE(cm_vregs))) {
170                         ret = cm_i2c_wr(st, (CM_HW_CMD1_DFLT |
171                                              CM_HW_CMD1_BIT_SD), 0);
172                 } else if (ret > 0) {
173                         nvs_vregs_enable(&st->i2c->dev, st->vreg,
174                                          ARRAY_SIZE(cm_vregs));
175                         mdelay(CM_HW_DELAY_MS);
176                         ret = cm_i2c_wr(st, (CM_HW_CMD1_DFLT |
177                                              CM_HW_CMD1_BIT_SD), 0);
178                 }
179                 ret |= nvs_vregs_disable(&st->i2c->dev, st->vreg,
180                                          ARRAY_SIZE(cm_vregs));
181         }
182         if (ret > 0)
183                 ret = 0;
184         if (ret) {
185                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
186                         __func__, enable, ret);
187         } else {
188                 if (st->sts & NVS_STS_SPEW_MSG)
189                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
190                                  __func__, enable);
191         }
192         return ret;
193 }
194
195 static void cm_pm_exit(struct cm_state *st)
196 {
197         cm_pm(st, false);
198         nvs_vregs_exit(&st->i2c->dev, st->vreg, ARRAY_SIZE(cm_vregs));
199 }
200
201 static int cm_pm_init(struct cm_state *st)
202 {
203         int ret;
204
205         st->enabled = 0;
206         nvs_vregs_init(&st->i2c->dev,
207                        st->vreg, ARRAY_SIZE(cm_vregs), cm_vregs);
208         /* on off for low power mode if regulator still on */
209         ret = cm_pm(st, true);
210         ret |= cm_pm(st, false);
211         return ret;
212 }
213
214 static int cm_cmd_wr(struct cm_state *st)
215 {
216         u8 cmd1;
217         u8 cmd2;
218         int ret;
219
220         cmd1 = CM_HW_CMD1_DFLT;
221         cmd1 |= cm_nld_tbl[st->light.nld_i].driver_data & CM_HW_CMD1_IT_T_MASK;
222         cmd2 = cm_nld_tbl[st->light.nld_i].driver_data & CM_HW_CMD2_FD_IT_MASK;
223         ret = cm_i2c_wr(st, cmd1, cmd2);
224         if (!ret)
225                 st->hw_change = true;
226         if (st->sts & NVS_STS_SPEW_MSG)
227                 dev_info(&st->i2c->dev, "%s cmd1=%hhx cmd2=%hhx err=%d\n",
228                          __func__, cmd1, cmd2, ret);
229         return ret;
230 }
231
232 static int cm_rd(struct cm_state *st)
233 {
234         u16 hw;
235         s64 ts;
236         int ret;
237
238         if (st->hw_change) {
239                 /* drop first sample after HW change */
240                 st->hw_change = false;
241                 return 0;
242         }
243
244         ret = cm_i2c_rd(st, &hw);
245         if (ret)
246                 return ret;
247
248         ts = cm_get_time_ns();
249         if (st->sts & NVS_STS_SPEW_DATA)
250                 dev_info(&st->i2c->dev,
251                          "poll light hw %hu %lld  diff=%d %lldns  index=%u\n",
252                          hw, ts, hw - st->light.hw, ts - st->light.timestamp,
253                          st->light.nld_i);
254         st->light.hw = hw;
255         st->light.timestamp = ts;
256         nvs_light_read(&st->light);
257         if (st->light.nld_i_change)
258                 cm_cmd_wr(st);
259         return 0;
260 }
261
262 static void cm_read(struct cm_state *st)
263 {
264         st->nvs->nvs_mutex_lock(st->nvs_st);
265         if (st->enabled) {
266                 cm_rd(st);
267                 schedule_delayed_work(&st->dw,
268                                     msecs_to_jiffies(st->light.poll_delay_ms));
269         }
270         st->nvs->nvs_mutex_unlock(st->nvs_st);
271 }
272
273 static void cm_work(struct work_struct *ws)
274 {
275         struct cm_state *st = container_of((struct delayed_work *)ws,
276                                            struct cm_state, dw);
277
278         cm_read(st);
279 }
280
281 static int cm_disable(struct cm_state *st)
282 {
283         int ret;
284
285         cancel_delayed_work(&st->dw);
286         ret = cm_pm(st, false);
287         if (!ret)
288                 st->enabled = 0;
289         return ret;
290 }
291
292 static int cm_enable(void *client, int snsr_id, int enable)
293 {
294         struct cm_state *st = (struct cm_state *)client;
295         unsigned int ms;
296         int ret;
297
298         if (enable < 0)
299                 return st->enabled;
300
301         if (enable) {
302                 ret = cm_pm(st, true);
303                 if (!ret) {
304                         nvs_light_enable(&st->light);
305                         ret = cm_cmd_wr(st);
306                         if (ret) {
307                                 cm_disable(st);
308                         } else {
309                                 st->enabled = 1;
310                                 ms = st->light.poll_delay_ms;
311                                 schedule_delayed_work(&st->dw,
312                                                       msecs_to_jiffies(ms));
313                         }
314                 }
315         } else {
316                 ret = cm_disable(st);
317         }
318         return ret;
319 }
320
321 static int cm_batch(void *client, int snsr_id, int flags,
322                     unsigned int period, unsigned int timeout)
323 {
324         struct cm_state *st = (struct cm_state *)client;
325
326         if (timeout)
327                 /* timeout not supported (no HW FIFO) */
328                 return -EINVAL;
329
330         st->light.delay_us = period;
331         return 0;
332 }
333
334 static int cm_thresh_lo(void *client, int snsr_id, int thresh_lo)
335 {
336         struct cm_state *st = (struct cm_state *)client;
337
338         nvs_light_threshold_calibrate_lo(&st->light, thresh_lo);
339         return 0;
340 }
341
342 static int cm_thresh_hi(void *client, int snsr_id, int thresh_hi)
343 {
344         struct cm_state *st = (struct cm_state *)client;
345
346         nvs_light_threshold_calibrate_hi(&st->light, thresh_hi);
347         return 0;
348 }
349
350 static int cm_regs(void *client, int snsr_id, char *buf)
351 {
352         struct cm_state *st = (struct cm_state *)client;
353         ssize_t t;
354
355         t = sprintf(buf, "registers:\n");
356         t += sprintf(buf + t, "0x20=%#2x\n", st->cmd1);
357         t += sprintf(buf + t, "0x22=%#2x\n", st->cmd2);
358         return t;
359 }
360
361 static int cm_dbg(void *client, int snsr_id, char *buf)
362 {
363         struct cm_state *st = (struct cm_state *)client;
364         return nvs_light_dbg(&st->light, buf);
365 }
366
367 static struct nvs_fn_dev cm_fn_dev = {
368         .enable                         = cm_enable,
369         .batch                          = cm_batch,
370         .thresh_lo                      = cm_thresh_lo,
371         .thresh_hi                      = cm_thresh_hi,
372         .regs                           = cm_regs,
373         .nvs_read                       = cm_dbg,
374 };
375
376 static int cm_suspend(struct device *dev)
377 {
378         struct i2c_client *client = to_i2c_client(dev);
379         struct cm_state *st = i2c_get_clientdata(client);
380         int ret = 0;
381
382         st->sts |= NVS_STS_SUSPEND;
383         if (st->nvs && st->nvs_st)
384                 ret = st->nvs->suspend(st->nvs_st);
385         if (st->sts & NVS_STS_SPEW_MSG)
386                 dev_info(&client->dev, "%s\n", __func__);
387         return ret;
388 }
389
390 static int cm_resume(struct device *dev)
391 {
392         struct i2c_client *client = to_i2c_client(dev);
393         struct cm_state *st = i2c_get_clientdata(client);
394         int ret = 0;
395
396         if (st->nvs && st->nvs_st)
397                 ret = st->nvs->resume(st->nvs_st);
398         st->sts &= ~NVS_STS_SUSPEND;
399         if (st->sts & NVS_STS_SPEW_MSG)
400                 dev_info(&client->dev, "%s\n", __func__);
401         return ret;
402 }
403
404 static SIMPLE_DEV_PM_OPS(cm_pm_ops, cm_suspend, cm_resume);
405
406 static void cm_shutdown(struct i2c_client *client)
407 {
408         struct cm_state *st = i2c_get_clientdata(client);
409
410         st->sts |= NVS_STS_SHUTDOWN;
411         if (st->nvs && st->nvs_st)
412                 st->nvs->shutdown(st->nvs_st);
413         if (st->sts & NVS_STS_SPEW_MSG)
414                 dev_info(&client->dev, "%s\n", __func__);
415 }
416
417 static int cm_remove(struct i2c_client *client)
418 {
419         struct cm_state *st = i2c_get_clientdata(client);
420
421         if (st != NULL) {
422                 cm_shutdown(client);
423                 if (st->nvs && st->nvs_st)
424                         st->nvs->remove(st->nvs_st);
425                 if (st->dw.wq)
426                         destroy_workqueue(st->dw.wq);
427                 cm_pm_exit(st);
428         }
429         dev_info(&client->dev, "%s\n", __func__);
430         return 0;
431 }
432
433 static struct sensor_cfg cm_cfg_dflt = {
434         .name                   = NVS_LIGHT_STRING,
435         .ch_n                   = 1,
436         .ch_sz                  = 4,
437         .part                   = CM_NAME,
438         .vendor                 = CM_VENDOR,
439         .version                = CM_LIGHT_VERSION,
440         .max_range              = {
441                 .ival           = CM_LIGHT_MAX_RANGE_IVAL,
442                 .fval           = CM_LIGHT_MAX_RANGE_MICRO,
443         },
444         .resolution             = {
445                 .ival           = CM_LIGHT_RESOLUTION_IVAL,
446                 .fval           = CM_LIGHT_RESOLUTION_MICRO,
447         },
448         .milliamp               = {
449                 .ival           = CM_LIGHT_MILLIAMP_IVAL,
450                 .fval           = CM_LIGHT_MILLIAMP_MICRO,
451         },
452         .delay_us_min           = CM_POLL_DLY_MS_MIN * 1000,
453         .delay_us_max           = CM_POLL_DLY_MS_MAX * 1000,
454         .flags                  = SENSOR_FLAG_ON_CHANGE_MODE,
455         .scale                  = {
456                 .ival           = CM_LIGHT_SCALE_IVAL,
457                 .fval           = CM_LIGHT_SCALE_MICRO,
458         },
459         .thresh_lo              = CM_LIGHT_THRESHOLD_LO,
460         .thresh_hi              = CM_LIGHT_THRESHOLD_HI,
461 };
462
463 static int cm_of_dt(struct cm_state *st, struct device_node *dn)
464 {
465         unsigned int i;
466         int ret;
467
468         /* default NVS programmable parameters */
469         memcpy(&st->cfg, &cm_cfg_dflt, sizeof(st->cfg));
470         st->light.cfg = &st->cfg;
471         st->light.hw_mask = 0xFFFF;
472         st->light.nld_tbl = cm_nld_tbl;
473         /* device tree parameters */
474         ret = nvs_of_dt(dn, &st->cfg, NULL);
475         if (ret == -ENODEV)
476                 return -ENODEV;
477
478         if (nvs_light_of_dt(&st->light, dn, NULL)) {
479                 st->light.nld_i_lo = 0;
480                 st->light.nld_i_hi = ARRAY_SIZE(cm_nld_tbl) - 1;
481         }
482         if (st->light.nld_i_lo >= ARRAY_SIZE(cm_nld_tbl))
483                 st->light.nld_i_lo = ARRAY_SIZE(cm_nld_tbl) - 1;
484         if (st->light.nld_i_hi >= ARRAY_SIZE(cm_nld_tbl))
485                 st->light.nld_i_hi = ARRAY_SIZE(cm_nld_tbl) - 1;
486         i = st->light.nld_i_lo;
487         st->cfg.resolution.ival = cm_nld_tbl[i].resolution.ival;
488         st->cfg.resolution.fval = cm_nld_tbl[i].resolution.fval;
489         i = st->light.nld_i_hi;
490         st->cfg.max_range.ival = cm_nld_tbl[i].max_range.ival;
491         st->cfg.max_range.fval = cm_nld_tbl[i].max_range.fval;
492         st->cfg.delay_us_min = cm_nld_tbl[i].delay_min_ms * 1000;
493         return 0;
494 }
495
496 static int cm_probe(struct i2c_client *client,
497                     const struct i2c_device_id *id)
498 {
499         struct cm_state *st;
500         int ret;
501
502         dev_info(&client->dev, "%s\n", __func__);
503         st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
504         if (st == NULL) {
505                 dev_err(&client->dev, "%s devm_kzalloc ERR\n", __func__);
506                 return -ENOMEM;
507         }
508
509         i2c_set_clientdata(client, st);
510         st->i2c = client;
511         ret = cm_of_dt(st, client->dev.of_node);
512         if (ret) {
513                 if (ret == -ENODEV) {
514                         dev_info(&client->dev, "%s DT disabled\n", __func__);
515                 } else {
516                         dev_err(&client->dev, "%s _of_dt ERR\n", __func__);
517                         ret = -ENODEV;
518                 }
519                 goto cm_probe_exit;
520         }
521
522         ret = cm_pm_init(st);
523         if (ret) {
524                 dev_err(&client->dev, "%s _pm_init ERR\n", __func__);
525                 ret = -ENODEV;
526                 goto cm_probe_exit;
527         }
528
529         cm_fn_dev.errs = &st->errs;
530         cm_fn_dev.sts = &st->sts;
531         st->nvs = nvs_iio();
532         if (st->nvs == NULL) {
533                 dev_err(&client->dev, "%s nvs_iio ERR\n", __func__);
534                 ret = -ENODEV;
535                 goto cm_probe_exit;
536         }
537
538         st->light.handler = st->nvs->handler;
539         ret = st->nvs->probe(&st->nvs_st, st, &client->dev,
540                              &cm_fn_dev, &st->cfg);
541         if (ret) {
542                 dev_err(&client->dev, "%s nvs_probe ERR\n", __func__);
543                 ret = -ENODEV;
544                 goto cm_probe_exit;
545         }
546
547         st->light.nvs_st = st->nvs_st;
548         INIT_DELAYED_WORK(&st->dw, cm_work);
549         dev_info(&client->dev, "%s done\n", __func__);
550         return 0;
551
552 cm_probe_exit:
553         cm_remove(client);
554         return ret;
555 }
556
557 static const struct i2c_device_id cm_i2c_device_id[] = {
558         { CM_NAME, 0 },
559         {}
560 };
561
562 MODULE_DEVICE_TABLE(i2c, cm_i2c_device_id);
563
564 static const struct of_device_id cm_of_match[] = {
565         { .compatible = "capella,cm3217", },
566         {},
567 };
568
569 MODULE_DEVICE_TABLE(of, cm_of_match);
570
571 static struct i2c_driver cm_driver = {
572         .class          = I2C_CLASS_HWMON,
573         .probe          = cm_probe,
574         .remove         = cm_remove,
575         .shutdown       = cm_shutdown,
576         .driver = {
577                 .name           = CM_NAME,
578                 .owner          = THIS_MODULE,
579                 .of_match_table = of_match_ptr(cm_of_match),
580                 .pm             = &cm_pm_ops,
581         },
582         .id_table       = cm_i2c_device_id,
583 };
584 module_i2c_driver(cm_driver);
585
586 MODULE_LICENSE("GPL");
587 MODULE_DESCRIPTION("CM3217 driver");
588 MODULE_AUTHOR("NVIDIA Corporation");