1 /* Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
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.
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.
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>
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.
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)
56 CHIP_POWER_ON_ALS_OFF,
62 I2c_XFER_OK_REG_NOT_SYNC,
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;
75 static int cm3217_i2c_rd(struct cm3217_inf *inf)
77 struct i2c_msg msg[2];
80 msg[0].addr = CM3217_I2C_ADDR_RD + 1;
81 msg[0].flags = I2C_M_RD;
84 msg[1].addr = CM3217_I2C_ADDR_RD;
85 msg[1].flags = I2C_M_RD;
88 if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
91 inf->raw_illuminance_val = (__u16)((buf[1] << 8) | buf[0]);
95 static int cm3217_i2c_wr(struct cm3217_inf *inf, __u8 cmd1, __u8 cmd2)
97 struct i2c_msg msg[2];
102 msg[0].addr = CM3217_I2C_ADDR_CMD1_WR;
105 msg[0].buf = &buf[0];
106 msg[1].addr = CM3217_I2C_ADDR_CMD2_WR;
109 msg[1].buf = &buf[1];
110 if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
116 static int cm3217_cmd_wr(struct cm3217_inf *inf, __u8 it_t, __u8 fd_it)
122 cmd1 = (CM3217_HW_CMD1_DFLT);
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);
131 static int cm3217_vreg_dis(struct cm3217_inf *inf, unsigned int i)
135 if (inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
136 err = regulator_disable(inf->vreg[i].consumer);
138 dev_dbg(&inf->i2c->dev, "%s %s\n",
139 __func__, inf->vreg[i].supply);
141 dev_err(&inf->i2c->dev, "%s %s ERR\n",
142 __func__, inf->vreg[i].supply);
144 inf->vreg[i].ret = 0;
148 static int cm3217_vreg_dis_all(struct cm3217_inf *inf)
153 for (i = CM3217_MAX_REGULATORS; i > 0; i--)
154 err |= cm3217_vreg_dis(inf, (i - 1));
158 static int cm3217_vreg_en(struct cm3217_inf *inf, unsigned int i)
162 if (!inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
163 err = regulator_enable(inf->vreg[i].consumer);
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 */
170 dev_err(&inf->i2c->dev, "%s %s ERR\n",
171 __func__, inf->vreg[i].supply);
177 static int cm3217_vreg_en_all(struct cm3217_inf *inf)
182 for (i = 0; i < CM3217_MAX_REGULATORS; i++)
183 err |= cm3217_vreg_en(inf, i);
187 static void cm3217_vreg_exit(struct cm3217_inf *inf)
190 for (i = 0; i < CM3217_MAX_REGULATORS; i++) {
191 regulator_put(inf->vreg[i].consumer);
192 inf->vreg[i].consumer = NULL;
196 static int cm3217_vreg_init(struct cm3217_inf *inf)
202 * regulator names in order of powering on.
203 * ARRAY_SIZE(cm3217_vregs) must be < CM3217_MAX_REGULATORS
205 char *cm3217_vregs[] = {
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;
221 for (; i < CM3217_MAX_REGULATORS; i++)
222 inf->vreg[i].consumer = NULL;
226 static ssize_t cm3217_chan_regulator_enable_show(struct device *dev,
227 struct device_attribute *attr, char *buf)
229 struct iio_dev *indio_dev = dev_get_drvdata(dev);
230 struct cm3217_inf *inf = iio_priv(indio_dev);
231 unsigned int enable = 0;
233 if (inf->als_state != CHIP_POWER_OFF)
235 return sprintf(buf, "%d\n", inf->als_state);
238 static ssize_t cm3217_chan_regulator_enable(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf, size_t count)
244 struct iio_dev *indio_dev = dev_get_drvdata(dev);
245 struct cm3217_inf *inf = iio_priv(indio_dev);
247 if (kstrtou8(buf, 10, &enable))
250 if ((enable != 0) && (enable != 1))
253 if (enable == (inf->als_state != CHIP_POWER_OFF))
260 ret = cm3217_vreg_en_all(inf);
262 ret = cm3217_vreg_dis_all(inf);
265 dev_err(&inf->i2c->dev,
266 "func:%s line:%d err:%d fails\n",
267 __func__, __LINE__, ret);
272 inf->als_state = enable;
274 return ret ? ret : 1;
277 static void cm3217_work(struct work_struct *ws)
279 struct cm3217_inf *inf;
280 struct iio_dev *indio_dev;
282 inf = container_of(ws, struct cm3217_inf, dw.work);
283 indio_dev = iio_priv_to_dev(inf);
284 mutex_lock(&indio_dev->mlock);
286 mutex_unlock(&indio_dev->mlock);
289 static ssize_t cm3217_enable_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct iio_dev *indio_dev = dev_get_drvdata(dev);
293 struct cm3217_inf *inf = iio_priv(indio_dev);
294 unsigned int enable = 0;
296 if (inf->als_state == CHIP_POWER_ON_ALS_ON)
298 return sprintf(buf, "%u\n", enable);
301 static ssize_t cm3217_enable_store(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
305 struct iio_dev *indio_dev = dev_get_drvdata(dev);
306 struct cm3217_inf *inf = iio_priv(indio_dev);
310 if (kstrtou8(buf, 10, &enable))
313 if ((enable != 0) && (enable != 1))
316 if (enable == (inf->als_state - 1))
319 mutex_lock(&indio_dev->mlock);
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);
325 cancel_delayed_work_sync(&inf->dw);
327 mutex_unlock(&indio_dev->mlock);
332 inf->als_state = enable + 1;
336 static ssize_t cm3217_raw_illuminance_val_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct iio_dev *indio_dev = dev_get_drvdata(dev);
340 struct cm3217_inf *inf = iio_priv(indio_dev);
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);
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 */
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,
380 static struct attribute_group cm3217_attr_group = {
382 .attrs = cm3217_attrs
385 static const struct iio_info cm3217_iio_info = {
386 .attrs = &cm3217_attr_group,
387 .driver_module = THIS_MODULE
390 #ifdef CONFIG_PM_SLEEP
391 static int cm3217_suspend(struct device *dev)
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);
398 if (inf->als_state != CHIP_POWER_OFF)
399 ret = cm3217_vreg_dis_all(inf);
402 dev_err(&client->adapter->dev,
403 "%s err in reg enable\n", __func__);
407 static int cm3217_resume(struct device *dev)
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);
414 if (inf->als_state != CHIP_POWER_OFF)
415 ret = cm3217_vreg_en_all(inf);
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);
423 dev_err(&client->adapter->dev,
424 "%s err in cm3217 write\n", __func__);
428 static SIMPLE_DEV_PM_OPS(cm3217_pm_ops, cm3217_suspend, cm3217_resume);
429 #define CM3217_PM_OPS (&cm3217_pm_ops)
431 #define CM3217_PM_OPS NULL
434 static int cm3217_remove(struct i2c_client *client)
436 struct iio_dev *indio_dev = i2c_get_clientdata(client);
437 struct cm3217_inf *inf = iio_priv(indio_dev);
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__);
447 static int cm3217_probe(struct i2c_client *client,
448 const struct i2c_device_id *id)
450 struct cm3217_inf *inf;
451 struct iio_dev *indio_dev;
452 struct lightsensor_spec *ls_spec;
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__);
461 inf = iio_priv(indio_dev);
463 inf->wq = create_singlethread_workqueue(CM3217_NAME);
465 dev_err(&client->dev, "%s workqueue err\n", __func__);
471 err = cm3217_vreg_init(inf);
473 dev_info(&client->dev,
474 "%s regulator init failed, assume always on", __func__);
478 INIT_DELAYED_WORK(&inf->dw, cm3217_work);
481 i2c_set_clientdata(client, indio_dev);
483 ls_spec = of_get_ls_spec(&client->dev);
485 dev_warn(&client->dev,
486 "devname:%s func:%s line:%d invalid meta data, use default\n",
487 id->name, __func__, __LINE__);
489 fill_ls_attrs(ls_spec, cm3217_attrs);
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);
498 dev_err(&client->dev, "%s iio_device_register err\n", __func__);
499 goto err_iio_register;
502 inf->raw_illuminance_val = -EINVAL;
503 dev_info(&client->dev, "%s success\n", __func__);
507 cm3217_vreg_exit(inf);
509 destroy_workqueue(inf->wq);
511 iio_device_free(indio_dev);
512 dev_err(&client->dev, "%s err=%d\n", __func__, err);
516 static const struct i2c_device_id cm3217_i2c_device_id[] = {
521 MODULE_DEVICE_TABLE(i2c, cm3217_i2c_device_id);
523 static void cm3217_shutdown(struct i2c_client *client)
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;
529 cancel_delayed_work_sync(&inf->dw);
530 cm3217_vreg_exit(inf);
534 static const struct of_device_id cm3217_of_match[] = {
535 { .compatible = "capella,cm3217", },
538 MODULE_DEVICE_TABLE(of, cm3217_of_match);
541 static struct i2c_driver cm3217_driver = {
542 .probe = cm3217_probe,
543 .remove = cm3217_remove,
544 .id_table = cm3217_i2c_device_id,
547 .owner = THIS_MODULE,
548 .of_match_table = of_match_ptr(cm3217_of_match),
551 .shutdown = cm3217_shutdown,
553 module_i2c_driver(cm3217_driver);
555 MODULE_LICENSE("GPL");
556 MODULE_DESCRIPTION("CM3217 driver");
557 MODULE_AUTHOR("NVIDIA Corp");