]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/staging/iio/light/cm3217.c
staging: iio: light: cm3217: check event's validity
[sojka/nv-tegra/linux-3.10.git] / drivers / staging / iio / light / cm3217.c
1 /* Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
2  *
3  * This software is licensed under the terms of the GNU General Public
4  * License version 2, as published by the Free Software Foundation, and
5  * may be copied, distributed, and modified under those terms.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/light/ls_sysfs.h>
23 #include <linux/iio/light/ls_dt.h>
24
25 /* IT = Integration Time.  The amount of time the photons hit the sensor.
26  * STEP = the value from HW which is the photon count during IT.
27  * LUX = STEP * (CM3217_RESOLUTION_STEP / IT) / CM3217_RESOLUTION_DIVIDER
28  * The above LUX reported as LUX * CM3217_INPUT_LUX_DIVISOR.
29  * The final value is done in user space to get a float value of
30  * LUX / CM3217_INPUT_LUX_DIVISOR.
31  */
32 #define CM3217_NAME                     "cm3217"
33 #define CM3217_I2C_ADDR_CMD1_WR         (0x10)
34 #define CM3217_I2C_ADDR_CMD2_WR         (0x11)
35 #define CM3217_I2C_ADDR_RD              (0x10)
36 #define CM3217_HW_CMD1_DFLT             (0x22)
37 #define CM3217_HW_CMD1_BIT_SD           (0)
38 #define CM3217_HW_CMD1_BIT_IT_T         (2)
39 #define CM3217_HW_CMD2_BIT_FD_IT        (5)
40 #define CM3217_HW_DELAY                 (10)
41 #define CM3217_POWER_UA                 (90)
42 #define CM3217_RESOLUTION               (1)
43 #define CM3217_RESOLUTION_STEP          (6000000L)
44 #define CM3217_RESOLUTION_DIVIDER       (10000L)
45 #define CM3217_POLL_DELAY_MS_DFLT       (1600)
46 #define CM3217_POLL_DELAY_MS_MIN        (33 + CM3217_HW_DELAY)
47 #define CM3217_INPUT_LUX_DIVISOR        (10)
48 #define CM3217_INPUT_LUX_MIN            (0)
49 #define CM3217_INPUT_LUX_MAX            (119156)
50 #define CM3217_INPUT_LUX_FUZZ           (0)
51 #define CM3217_INPUT_LUX_FLAT           (0)
52 #define CM3217_MAX_REGULATORS           (1)
53
54 enum als_state {
55         CHIP_POWER_OFF,
56         CHIP_POWER_ON_ALS_OFF,
57         CHIP_POWER_ON_ALS_ON,
58 };
59
60 enum i2c_state {
61         I2C_XFER_NOT_ENABLED,
62         I2c_XFER_OK_REG_NOT_SYNC,
63         I2c_XFER_OK_REG_SYNC,
64 };
65
66 struct cm3217_inf {
67         struct i2c_client *i2c;
68         struct workqueue_struct *wq;
69         struct delayed_work dw;
70         struct regulator_bulk_data vreg[CM3217_MAX_REGULATORS];
71         int raw_illuminance_val;
72         int als_state;
73 };
74
75 static int cm3217_i2c_rd(struct cm3217_inf *inf)
76 {
77         struct i2c_msg msg[2];
78         __u8 buf[2];
79
80         msg[0].addr = CM3217_I2C_ADDR_RD + 1;
81         msg[0].flags = I2C_M_RD;
82         msg[0].len = 1;
83         msg[0].buf = &buf[0];
84         msg[1].addr = CM3217_I2C_ADDR_RD;
85         msg[1].flags = I2C_M_RD;
86         msg[1].len = 1;
87         msg[1].buf = &buf[1];
88         if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
89                 return -EIO;
90
91         inf->raw_illuminance_val = (__u16)((buf[1] << 8) | buf[0]);
92         return 0;
93 }
94
95 static int cm3217_i2c_wr(struct cm3217_inf *inf, __u8 cmd1, __u8 cmd2)
96 {
97         struct i2c_msg msg[2];
98         __u8 buf[2];
99
100         buf[0] = cmd1;
101         buf[1] = cmd2;
102         msg[0].addr = CM3217_I2C_ADDR_CMD1_WR;
103         msg[0].flags = 0;
104         msg[0].len = 1;
105         msg[0].buf = &buf[0];
106         msg[1].addr = CM3217_I2C_ADDR_CMD2_WR;
107         msg[1].flags = 0;
108         msg[1].len = 1;
109         msg[1].buf = &buf[1];
110         if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
111                 return -EIO;
112
113         return 0;
114 }
115
116 static int cm3217_cmd_wr(struct cm3217_inf *inf, __u8 it_t, __u8 fd_it)
117 {
118         __u8 cmd1;
119         __u8 cmd2;
120         int err;
121
122         cmd1 = (CM3217_HW_CMD1_DFLT);
123         if (!inf->als_state)
124                 cmd1 |= (1 << CM3217_HW_CMD1_BIT_SD);
125         cmd1 |= (it_t << CM3217_HW_CMD1_BIT_IT_T);
126         cmd2 = fd_it << CM3217_HW_CMD2_BIT_FD_IT;
127         err = cm3217_i2c_wr(inf, cmd1, cmd2);
128         return err;
129 }
130
131 static int cm3217_vreg_dis(struct cm3217_inf *inf, unsigned int i)
132 {
133         int err = 0;
134
135         if (inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
136                 err = regulator_disable(inf->vreg[i].consumer);
137                 if (!err)
138                         dev_dbg(&inf->i2c->dev, "%s %s\n",
139                                 __func__, inf->vreg[i].supply);
140                 else
141                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
142                                 __func__, inf->vreg[i].supply);
143         }
144         inf->vreg[i].ret = 0;
145         return err;
146 }
147
148 static int cm3217_vreg_dis_all(struct cm3217_inf *inf)
149 {
150         unsigned int i;
151         int err = 0;
152
153         for (i = CM3217_MAX_REGULATORS; i > 0; i--)
154                 err |= cm3217_vreg_dis(inf, (i - 1));
155         return err;
156 }
157
158 static int cm3217_vreg_en(struct cm3217_inf *inf, unsigned int i)
159 {
160         int err = 0;
161
162         if (!inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
163                 err = regulator_enable(inf->vreg[i].consumer);
164                 if (!err) {
165                         inf->vreg[i].ret = 1;
166                         dev_dbg(&inf->i2c->dev, "%s %s\n",
167                                 __func__, inf->vreg[i].supply);
168                         err = 1; /* flag regulator state change */
169                 } else {
170                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
171                                 __func__, inf->vreg[i].supply);
172                 }
173         }
174         return err;
175 }
176
177 static int cm3217_vreg_en_all(struct cm3217_inf *inf)
178 {
179         unsigned i;
180         int err = 0;
181
182         for (i = 0; i < CM3217_MAX_REGULATORS; i++)
183                 err |= cm3217_vreg_en(inf, i);
184         return err;
185 }
186
187 static void cm3217_vreg_exit(struct cm3217_inf *inf)
188 {
189         int i;
190         for (i = 0; i < CM3217_MAX_REGULATORS; i++) {
191                 regulator_put(inf->vreg[i].consumer);
192                 inf->vreg[i].consumer = NULL;
193         }
194 }
195
196 static int cm3217_vreg_init(struct cm3217_inf *inf)
197 {
198         unsigned int i;
199         int err = 0;
200
201         /*
202          * regulator names in order of powering on.
203          * ARRAY_SIZE(cm3217_vregs) must be < CM3217_MAX_REGULATORS
204          */
205         char *cm3217_vregs[] = {
206                 "vdd",
207         };
208
209         for (i = 0; i < ARRAY_SIZE(cm3217_vregs); i++) {
210                 inf->vreg[i].supply = cm3217_vregs[i];
211                 inf->vreg[i].ret = 0;
212                 inf->vreg[i].consumer = regulator_get(&inf->i2c->dev,
213                                                         inf->vreg[i].supply);
214                 if (IS_ERR(inf->vreg[i].consumer)) {
215                         err = PTR_ERR(inf->vreg[i].consumer);
216                         dev_err(&inf->i2c->dev, "%s err %d for %s\n",
217                                 __func__, err, inf->vreg[i].supply);
218                         inf->vreg[i].consumer = NULL;
219                 }
220         }
221         for (; i < CM3217_MAX_REGULATORS; i++)
222                 inf->vreg[i].consumer = NULL;
223         return err;
224 }
225
226 static ssize_t cm3217_chan_regulator_enable_show(struct device *dev,
227                                 struct device_attribute *attr, char *buf)
228 {
229         struct iio_dev *indio_dev = dev_get_drvdata(dev);
230         struct cm3217_inf *inf = iio_priv(indio_dev);
231         unsigned int enable = 0;
232
233         if (inf->als_state != CHIP_POWER_OFF)
234                 enable = 1;
235         return sprintf(buf, "%d\n", inf->als_state);
236 }
237
238 static ssize_t cm3217_chan_regulator_enable(struct device *dev,
239                                         struct device_attribute *attr,
240                                         const char *buf, size_t count)
241 {
242         u8 enable;
243         int ret = 0;
244         struct iio_dev *indio_dev = dev_get_drvdata(dev);
245         struct cm3217_inf *inf = iio_priv(indio_dev);
246
247         if (kstrtou8(buf, 10, &enable))
248                 return -EINVAL;
249
250         if ((enable != 0) && (enable != 1))
251                 return -EINVAL;
252
253         if (enable == (inf->als_state != CHIP_POWER_OFF))
254                 return 1;
255
256         if (!inf->vreg)
257                 goto success;
258
259         if (enable)
260                 ret = cm3217_vreg_en_all(inf);
261         else
262                 ret = cm3217_vreg_dis_all(inf);
263
264         if (ret != enable) {
265                 dev_err(&inf->i2c->dev,
266                                 "func:%s line:%d err:%d fails\n",
267                                 __func__, __LINE__, ret);
268                 goto fail;
269         }
270
271 success:
272         inf->als_state = enable;
273 fail:
274         return ret ? ret : 1;
275 }
276
277 static void cm3217_work(struct work_struct *ws)
278 {
279         struct cm3217_inf *inf;
280         struct iio_dev *indio_dev;
281
282         inf = container_of(ws, struct cm3217_inf, dw.work);
283         indio_dev = iio_priv_to_dev(inf);
284         mutex_lock(&indio_dev->mlock);
285         cm3217_i2c_rd(inf);
286         mutex_unlock(&indio_dev->mlock);
287 }
288
289 static ssize_t cm3217_enable_show(struct device *dev,
290                                 struct device_attribute *attr, char *buf)
291 {
292         struct iio_dev *indio_dev = dev_get_drvdata(dev);
293         struct cm3217_inf *inf = iio_priv(indio_dev);
294         unsigned int enable = 0;
295
296         if (inf->als_state == CHIP_POWER_ON_ALS_ON)
297                 enable = 1;
298         return sprintf(buf, "%u\n", enable);
299 }
300
301 static ssize_t cm3217_enable_store(struct device *dev,
302                                         struct device_attribute *attr,
303                                         const char *buf, size_t count)
304 {
305         struct iio_dev *indio_dev = dev_get_drvdata(dev);
306         struct cm3217_inf *inf = iio_priv(indio_dev);
307         u8 enable;
308         int err = 0;
309
310         if (kstrtou8(buf, 10, &enable))
311                 return -EINVAL;
312
313         if ((enable != 0) && (enable != 1))
314                 return -EINVAL;
315
316         if (enable == (inf->als_state - 1))
317                 goto success;
318
319         mutex_lock(&indio_dev->mlock);
320         if (enable) {
321                 err = cm3217_cmd_wr(inf, 0, 0);
322                 inf->raw_illuminance_val = -EINVAL;
323                 queue_delayed_work(inf->wq, &inf->dw, CM3217_HW_DELAY);
324         } else {
325                 cancel_delayed_work_sync(&inf->dw);
326         }
327         mutex_unlock(&indio_dev->mlock);
328         if (err)
329                 return err;
330
331 success:
332         inf->als_state = enable + 1;
333         return count;
334 }
335
336 static ssize_t cm3217_raw_illuminance_val_show(struct device *dev,
337                                 struct device_attribute *attr, char *buf)
338 {
339         struct iio_dev *indio_dev = dev_get_drvdata(dev);
340         struct cm3217_inf *inf = iio_priv(indio_dev);
341
342         if (inf->als_state != CHIP_POWER_ON_ALS_ON)
343                 return sprintf(buf, "-1\n");
344         queue_delayed_work(inf->wq, &inf->dw, 0);
345         if (inf->raw_illuminance_val == -EINVAL)
346                 return sprintf(buf, "-1\n");
347         return sprintf(buf, "%d\n", inf->raw_illuminance_val);
348 }
349
350 static IIO_DEVICE_ATTR(in_illuminance_regulator_enable,
351                         S_IRUGO | S_IWUSR | S_IWOTH,
352                         cm3217_chan_regulator_enable_show,
353                         cm3217_chan_regulator_enable, 0);
354 static IIO_DEVICE_ATTR(in_illuminance_enable,
355                         S_IRUGO | S_IWUSR | S_IWOTH,
356                         cm3217_enable_show, cm3217_enable_store, 0);
357 static IIO_DEVICE_ATTR(in_illuminance_raw, S_IRUGO,
358                    cm3217_raw_illuminance_val_show, NULL, 0);
359 static IIO_CONST_ATTR(vendor, "Capella");
360 /* FD_IT = 000b, IT_TIMES = 1/2T i.e., 00b nano secs */
361 static IIO_CONST_ATTR(in_illuminance_integration_time, "480000");
362 /* WDM = 0b, IT_TIMES = 1/2T i.e., 00b raw_illuminance_val */
363 static IIO_CONST_ATTR(in_illuminance_max_range, "78643.2");
364 /* WDM = 0b, IT_TIMES = 1/2T i.e., 00b  mLux */
365 static IIO_CONST_ATTR(in_illuminance_resolution, "307");
366 static IIO_CONST_ATTR(in_illuminance_power_consumed, "1670"); /* milli Watt */
367
368 static struct attribute *cm3217_attrs[] = {
369         &iio_dev_attr_in_illuminance_enable.dev_attr.attr,
370         &iio_dev_attr_in_illuminance_regulator_enable.dev_attr.attr,
371         &iio_dev_attr_in_illuminance_raw.dev_attr.attr,
372         &iio_const_attr_vendor.dev_attr.attr,
373         &iio_const_attr_in_illuminance_integration_time.dev_attr.attr,
374         &iio_const_attr_in_illuminance_max_range.dev_attr.attr,
375         &iio_const_attr_in_illuminance_resolution.dev_attr.attr,
376         &iio_const_attr_in_illuminance_power_consumed.dev_attr.attr,
377         NULL
378 };
379
380 static struct attribute_group cm3217_attr_group = {
381         .name = CM3217_NAME,
382         .attrs = cm3217_attrs
383 };
384
385 static const struct iio_info cm3217_iio_info = {
386         .attrs = &cm3217_attr_group,
387         .driver_module = THIS_MODULE
388 };
389
390 #ifdef CONFIG_PM_SLEEP
391 static int cm3217_suspend(struct device *dev)
392 {
393         struct i2c_client *client = to_i2c_client(dev);
394         struct iio_dev *indio_dev = i2c_get_clientdata(client);
395         struct cm3217_inf *inf = iio_priv(indio_dev);
396         int ret = 0;
397
398         if (inf->als_state != CHIP_POWER_OFF)
399                 ret = cm3217_vreg_dis_all(inf);
400
401         if (ret)
402                 dev_err(&client->adapter->dev,
403                                 "%s err in reg enable\n", __func__);
404         return ret;
405 }
406
407 static int cm3217_resume(struct device *dev)
408 {
409         struct i2c_client *client = to_i2c_client(dev);
410         struct iio_dev *indio_dev = i2c_get_clientdata(client);
411         struct cm3217_inf *inf = iio_priv(indio_dev);
412         int ret = 0;
413
414         if (inf->als_state != CHIP_POWER_OFF)
415                 ret = cm3217_vreg_en_all(inf);
416
417         if (ret)
418                 dev_err(&client->adapter->dev,
419                                 "%s err in reg enable\n", __func__);
420         if (inf->als_state == CHIP_POWER_ON_ALS_ON)
421                 ret = cm3217_cmd_wr(inf, 0, 0);
422         if (ret)
423                 dev_err(&client->adapter->dev,
424                                 "%s err in cm3217 write\n", __func__);
425         return ret;
426 }
427
428 static SIMPLE_DEV_PM_OPS(cm3217_pm_ops, cm3217_suspend, cm3217_resume);
429 #define CM3217_PM_OPS (&cm3217_pm_ops)
430 #else
431 #define CM3217_PM_OPS NULL
432 #endif
433
434 static int cm3217_remove(struct i2c_client *client)
435 {
436         struct iio_dev *indio_dev = i2c_get_clientdata(client);
437         struct cm3217_inf *inf = iio_priv(indio_dev);
438
439         iio_device_unregister(indio_dev);
440         destroy_workqueue(inf->wq);
441         cm3217_vreg_exit(inf);
442         iio_device_free(indio_dev);
443         dev_dbg(&client->adapter->dev, "%s\n", __func__);
444         return 0;
445 }
446
447 static int cm3217_probe(struct i2c_client *client,
448                         const struct i2c_device_id *id)
449 {
450         struct cm3217_inf *inf;
451         struct iio_dev *indio_dev;
452         struct lightsensor_spec *ls_spec;
453         int err;
454
455         indio_dev = iio_device_alloc(sizeof(*inf));
456         if (indio_dev == NULL) {
457                 dev_err(&client->dev, "%s iio_device_alloc err\n", __func__);
458                 return -ENOMEM;
459         }
460
461         inf = iio_priv(indio_dev);
462
463         inf->wq = create_singlethread_workqueue(CM3217_NAME);
464         if (!inf->wq) {
465                 dev_err(&client->dev, "%s workqueue err\n", __func__);
466                 err = -ENOMEM;
467                 goto err_wq;
468         }
469
470         inf->i2c = client;
471         err = cm3217_vreg_init(inf);
472         if (err) {
473                 dev_info(&client->dev,
474                         "%s regulator init failed, assume always on", __func__);
475                 goto err_vreg_init;
476         }
477
478         INIT_DELAYED_WORK(&inf->dw, cm3217_work);
479         inf->als_state = 0;
480
481         i2c_set_clientdata(client, indio_dev);
482
483         ls_spec = of_get_ls_spec(&client->dev);
484         if (!ls_spec) {
485                 dev_warn(&client->dev,
486                         "devname:%s func:%s line:%d invalid meta data, use default\n",
487                         id->name, __func__, __LINE__);
488         } else {
489                 fill_ls_attrs(ls_spec, cm3217_attrs);
490         }
491         i2c_set_clientdata(client, indio_dev);
492         indio_dev->info = &cm3217_iio_info;
493         indio_dev->name = id->name;
494         indio_dev->dev.parent = &client->dev;
495         indio_dev->modes = INDIO_DIRECT_MODE;
496         err = iio_device_register(indio_dev);
497         if (err) {
498                 dev_err(&client->dev, "%s iio_device_register err\n", __func__);
499                 goto err_iio_register;
500         }
501
502         inf->raw_illuminance_val = -EINVAL;
503         dev_info(&client->dev, "%s success\n", __func__);
504         return 0;
505
506 err_iio_register:
507         cm3217_vreg_exit(inf);
508 err_vreg_init:
509         destroy_workqueue(inf->wq);
510 err_wq:
511         iio_device_free(indio_dev);
512         dev_err(&client->dev, "%s err=%d\n", __func__, err);
513         return err;
514 }
515
516 static const struct i2c_device_id cm3217_i2c_device_id[] = {
517         {"cm3217", 0},
518         {}
519 };
520
521 MODULE_DEVICE_TABLE(i2c, cm3217_i2c_device_id);
522
523 static void cm3217_shutdown(struct i2c_client *client)
524 {
525         struct iio_dev *indio_dev = i2c_get_clientdata(client);
526         struct cm3217_inf *inf = iio_priv(indio_dev);
527         inf->als_state = CHIP_POWER_OFF;
528         smp_wmb();
529         cancel_delayed_work_sync(&inf->dw);
530         cm3217_vreg_exit(inf);
531 }
532
533 #ifdef CONFIG_OF
534 static const struct of_device_id cm3217_of_match[] = {
535         { .compatible = "capella,cm3217", },
536         { },
537 };
538 MODULE_DEVICE_TABLE(of, cm3217_of_match);
539 #endif
540
541 static struct i2c_driver cm3217_driver = {
542         .probe          = cm3217_probe,
543         .remove         = cm3217_remove,
544         .id_table       = cm3217_i2c_device_id,
545         .driver = {
546                 .name   = "cm3217",
547                 .owner  = THIS_MODULE,
548                 .of_match_table = of_match_ptr(cm3217_of_match),
549                 .pm = CM3217_PM_OPS,
550         },
551         .shutdown = cm3217_shutdown,
552 };
553 module_i2c_driver(cm3217_driver);
554
555 MODULE_LICENSE("GPL");
556 MODULE_DESCRIPTION("CM3217 driver");
557 MODULE_AUTHOR("NVIDIA Corp");