1 /* drivers/input/misc/cm3218.c - cm3218 Ambient Light Sensor driver
3 * Copyright (c) 2012-2013, NVIDIA CORPORATION. All Rights Reserved.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/regmap.h>
22 #include <linux/delay.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/notifier.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
30 #define CM3218_REG_CONFIGURE 0x00
31 #define CM3218_REG_ALS_DATA 0x04
32 #define CM3218_REG_MAX 0x04
35 #define CONFIGURE_ALS_MASK 0x01
36 #define CONFIGURE_ALS_EN 0x00
38 #define CONFIGURE_SHDN_MASK CONFIGURE_ALS_MASK
39 #define CONFIGURE_SHDN_EN 0x01
41 #define LS_POLLING_DELAY 1000 /* mSec */
42 #define I2C_MAX_TIMEOUT msecs_to_jiffies(20) /* 20 mSec */
46 CHIP_POWER_ON_ALS_OFF,
51 struct i2c_client *client;
52 struct i2c_device_id *id;
54 struct regulator_bulk_data *consumers;
55 struct notifier_block regulator_nb;
56 wait_queue_head_t i2c_wait_queue;
58 struct regmap *regmap;
61 bool is_als_on_before_suspend;
62 int shutdown_complete;
65 /* regulators used by the device */
66 static struct regulator_bulk_data cm3218_consumers[] = {
72 /* device's regmap configuration for i2c communication */
73 /* non cacheable registers*/
74 bool cm3218_volatile_reg(struct device *dev, unsigned int reg)
76 return reg == CM3218_REG_ALS_DATA;
79 static const struct reg_default cm3218_reg_defaults = {
84 /* TODO * in linux-next we have to add
85 * val_format_endian to take care of endianness
86 * use regmap_access_table instead of volatile_reg call backs
88 static const struct regmap_config cm3218_regmap_config = {
91 .volatile_reg = &cm3218_volatile_reg,
92 .max_register = CM3218_REG_MAX,
93 .reg_defaults = &cm3218_reg_defaults,
94 .num_reg_defaults = 1,
95 .cache_type = REGCACHE_RBTREE,
98 /* device's read/write functionality and a helper */
99 static void change_endianness_16(int *val)
107 static int _cm3218_register_read(struct cm3218_chip *chip, int reg, int *val)
114 ret = wait_event_timeout(chip->i2c_wait_queue,
115 chip->i2c_xfer_ready, I2C_MAX_TIMEOUT);
117 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
118 "error_msg: device not ready for i2c xfer\n",
119 chip->id->name, __func__, __LINE__);
123 mutex_lock(&chip->lock);
124 ret = regmap_read(chip->regmap, reg, val);
126 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
127 "error_msg:regmap_read fails\n",
128 chip->id->name, __func__, __LINE__);
130 change_endianness_16(val);
132 temp = i2c_smbus_read_word_data(chip->client, CM3218_REG_ALS_DATA);
133 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
134 "als_i2c_data = %d\n",
135 chip->id->name, __func__, __LINE__, temp);
136 */ mutex_unlock(&chip->lock);
140 static int _cm3218_register_write(struct cm3218_chip *chip, int reg, int mask,
148 ret = wait_event_timeout(chip->i2c_wait_queue,
149 chip->i2c_xfer_ready, I2C_MAX_TIMEOUT);
151 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
152 "error_msg: device not ready for i2c xfer\n",
153 chip->id->name, __func__, __LINE__);
157 mutex_lock(&chip->lock);
158 change_endianness_16(&val);
159 ret = regmap_update_bits(chip->regmap, reg, mask, val);
161 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
162 "error_msg:regmap_write fails\n",
163 chip->id->name, __func__, __LINE__);
164 mutex_unlock(&chip->lock);
169 /* device's registration with iio to facilitate user operations */
170 static ssize_t cm3218_chan_regulator_enable(struct iio_dev *indio_dev,
171 uintptr_t private, struct iio_chan_spec const *chan,
172 const char *buf, size_t len)
176 struct cm3218_chip *chip = iio_priv(indio_dev);
178 if (kstrtou8(buf, 10, &enable))
181 if ((enable != 0) && (enable != 1))
184 if (chan->type != IIO_LIGHT)
187 if (enable == (chip->als_state != CHIP_POWER_OFF))
190 if (!chip->consumers)
194 ret = regulator_bulk_enable(1, chip->consumers);
196 ret = regulator_bulk_disable(1, chip->consumers);
199 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
200 "error_msg:_cm3218_register_read fails\n",
201 chip->id->name, __func__, __LINE__);
206 chip->als_state = enable;
208 return ret ? ret : 1;
211 static ssize_t cm3218_chan_enable(struct iio_dev *indio_dev, uintptr_t private,
212 struct iio_chan_spec const *chan, const char *buf, size_t len)
216 struct cm3218_chip *chip = iio_priv(indio_dev);
218 if (kstrtou8(buf, 10, &enable))
221 if ((enable != 0) && (enable != 1))
224 if (chip->als_state == CHIP_POWER_OFF) {
225 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
226 "error_msg:please enable regulator first\n",
227 chip->id->name, __func__, __LINE__);
231 if (!((enable && (chip->als_state == CHIP_POWER_ON_ALS_OFF)) ||
232 (!enable && (chip->als_state == CHIP_POWER_ON_ALS_ON))))
235 if (chan->type != IIO_LIGHT)
238 /* a small optimization*/
239 if (enable == (chip->als_state - 1))
243 ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
249 ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
257 /* a small optimization*/
258 chip->als_state = enable + 1;
259 return ret ? ret : 1;
263 /* chan_regulator_enable is used to enable regulators used by
264 * particular channel.
265 * chan_enable actually configures various registers to activate
266 * a particular channel.
268 static const struct iio_chan_spec_ext_info cm3218_ext_info[] = {
270 .name = "regulator_enable",
271 .write = cm3218_chan_regulator_enable,
275 .write = cm3218_chan_enable,
281 static const struct iio_chan_spec cm3218_channels[] = {
284 .ext_info = cm3218_ext_info,
288 static int cm3218_read_raw(struct iio_dev *indio_dev,
289 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
291 struct cm3218_chip *chip = iio_priv(indio_dev);
295 if (chip->als_state != CHIP_POWER_ON_ALS_ON)
298 if (chan->type != IIO_LIGHT)
301 ret = _cm3218_register_read(chip, CM3218_REG_ALS_DATA, &value);
303 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
304 "error_msg:_cm3218_register_read fails\n",
305 chip->id->name, __func__, __LINE__);
314 /* read_raw is used to report a channel's data to user
317 static const struct iio_info cm3218_iio_info = {
318 .driver_module = THIS_MODULE,
319 .read_raw = &cm3218_read_raw,
322 /* chip's power management helpers */
323 static int cm3218_activate_standby_mode(struct cm3218_chip *chip)
326 ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
332 /* this detects the regulator enable/disable event and puts
333 * the device to low power state if this device does not use the regulator */
334 static int cm3218_power_manager(struct notifier_block *regulator_nb,
335 unsigned long event, void *v)
337 struct cm3218_chip *chip;
339 chip = container_of(regulator_nb, struct cm3218_chip, regulator_nb);
341 if (event & (REGULATOR_EVENT_POST_ENABLE |
342 REGULATOR_EVENT_OUT_POSTCHANGE)) {
343 chip->i2c_xfer_ready = 1;
344 cm3218_activate_standby_mode(chip);
345 } else if (event & (REGULATOR_EVENT_DISABLE |
346 REGULATOR_EVENT_FORCE_DISABLE)) {
347 chip->i2c_xfer_ready = 0;
352 #ifdef CONFIG_PM_SLEEP
353 static int cm3218_suspend(struct device *dev)
355 struct i2c_client *client = to_i2c_client(dev);
356 struct iio_dev *indio_dev = i2c_get_clientdata(client);
357 struct cm3218_chip *chip = iio_priv(indio_dev);
360 if (!chip->consumers)
364 /* assumes all other devices stop using this regulator */
365 if (chip->als_state != CHIP_POWER_OFF)
366 ret = regulator_bulk_disable(1, chip->consumers);
369 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
370 "error_msg:regulator_bulk_disable fails" \
371 "\ncm3218_suspend fails\n",
372 chip->id->name, __func__, __LINE__);
376 if (!ret && regulator_is_enabled(chip->consumers[0].consumer))
377 ret = cm3218_activate_standby_mode(chip);
379 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
380 "error_msg:cm3218_activate_standby_mode fails" \
381 "\ncm3218_suspend fails\n",
382 chip->id->name, __func__, __LINE__);
386 static int cm3218_resume(struct device *dev)
388 struct i2c_client *client = to_i2c_client(dev);
389 struct iio_dev *indio_dev = i2c_get_clientdata(client);
390 struct cm3218_chip *chip = iio_priv(indio_dev);
394 if (chip->als_state == CHIP_POWER_OFF)
397 ret = regulator_bulk_enable(1, cm3218_consumers);
399 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
400 "error_msg:regulator_bulk_enable fails" \
401 "\ncm3218_suspend fails\n",
402 chip->id->name, __func__, __LINE__);
406 mutex_lock(&chip->lock);
407 if (chip->als_state == CHIP_POWER_ON_ALS_ON)
408 ret = _cm3218_register_write(chip, CM3218_REG_CONFIGURE,
412 dev_err(&chip->client->dev, "idname:%s func:%s line:%d " \
413 "error_msg:_cm3218_register_write fails" \
414 "\ncm3218_suspend fails\n",
415 chip->id->name, __func__, __LINE__);
416 /* fall back to CHIP_POWER_OFF, we don't need partial resume */
418 temp = regulator_bulk_disable(1, cm3218_consumers);
421 mutex_unlock(&chip->lock);
425 static SIMPLE_DEV_PM_OPS(cm3218_pm_ops, cm3218_suspend, cm3218_resume);
426 #define CM3218_PM_OPS (&cm3218_pm_ops)
428 #define CM3218_PM_OPS NULL
432 /* device's i2c registration */
433 static int cm3218_probe(struct i2c_client *client,
434 const struct i2c_device_id *id)
437 struct cm3218_chip *chip;
438 struct iio_dev *indio_dev;
439 struct regmap *regmap;
441 indio_dev = iio_device_alloc(sizeof(*chip));
442 if (indio_dev == NULL) {
443 dev_err(&client->dev, "idname:%s func:%s line:%d " \
444 "error_msg:iio_device_alloc fails\n",
445 id->name, __func__, __LINE__);
448 chip = iio_priv(indio_dev);
450 i2c_set_clientdata(client, indio_dev);
451 chip->client = client;
452 mutex_init(&chip->lock);
454 regmap = devm_regmap_init_i2c(client, &cm3218_regmap_config);
455 if (IS_ERR_OR_NULL(regmap)) {
456 dev_err(&client->dev, "idname:%s func:%s line:%d " \
457 "error_msg:devm_regmap_init_i2c fails\n",
458 id->name, __func__, __LINE__);
461 chip->regmap = regmap;
463 ret = devm_regulator_bulk_get(&client->dev,
464 ARRAY_SIZE(cm3218_consumers),
467 dev_err(&client->dev, "idname:%s func:%s line:%d " \
468 "error_msg:regulator_get fails\n",
469 id->name, __func__, __LINE__);
471 chip->consumers = cm3218_consumers;
473 if (chip->consumers) {
474 chip->regulator_nb.notifier_call = cm3218_power_manager;
475 ret = regulator_register_notifier(chip->consumers[0].consumer,
476 &chip->regulator_nb);
478 dev_err(&client->dev, "idname:%s func:%s line:%d " \
479 "error_msg:regulator_register_notifier fails\n",
480 id->name, __func__, __LINE__);
483 indio_dev->info = &cm3218_iio_info;
484 indio_dev->channels = cm3218_channels;
485 indio_dev->num_channels = 1;
486 indio_dev->name = id->name;
487 indio_dev->dev.parent = &client->dev;
488 indio_dev->modes = INDIO_DIRECT_MODE;
489 ret = iio_device_register(indio_dev);
491 dev_err(&client->dev, "idname:%s func:%s line:%d " \
492 "error_msg:iio_device_register fails\n",
493 id->name, __func__, __LINE__);
497 init_waitqueue_head(&chip->i2c_wait_queue);
499 if (regulator_is_enabled(chip->consumers[0].consumer)) {
500 chip->i2c_xfer_ready = 1;
501 cm3218_activate_standby_mode(chip);
504 dev_info(&client->dev, "idname:%s func:%s line:%d " \
506 id->name, __func__, __LINE__);
511 mutex_destroy(&chip->lock);
512 iio_device_free(indio_dev);
516 static void cm3218_shutdown(struct i2c_client *client)
518 struct iio_dev *indio_dev = i2c_get_clientdata(client);
519 struct cm3218_chip *chip = iio_priv(indio_dev);
520 mutex_lock(&chip->lock);
521 chip->shutdown_complete = 1;
522 mutex_unlock(&chip->lock);
525 static int __devexit cm3218_remove(struct i2c_client *client)
527 struct iio_dev *indio_dev = i2c_get_clientdata(client);
528 struct cm3218_chip *chip = iio_priv(indio_dev);
530 mutex_destroy(&chip->lock);
531 iio_device_unregister(indio_dev);
532 iio_device_free(indio_dev);
536 static const struct i2c_device_id cm3218_id[] = {
540 MODULE_DEVICE_TABLE(i2c, cm3218_id);
542 static const struct of_device_id cm3218_of_match[] = {
543 { .compatible = "capella,cm3218", },
546 MODULE_DEVICE_TABLE(of, cm3218_of_match);
548 static struct i2c_driver cm3218_driver = {
549 .class = I2C_CLASS_HWMON,
552 .owner = THIS_MODULE,
553 .of_match_table = cm3218_of_match,
556 .id_table = cm3218_id,
557 .probe = cm3218_probe,
558 .remove = __devexit_p(cm3218_remove),
559 .shutdown = cm3218_shutdown,
562 static int __init cm3218_init(void)
564 return i2c_add_driver(&cm3218_driver);
567 static void __exit cm3218_exit(void)
569 i2c_del_driver(&cm3218_driver);
572 module_init(cm3218_init);
573 module_exit(cm3218_exit);
575 MODULE_LICENSE("GPL");
576 MODULE_DESCRIPTION("CM3218 Driver");
577 MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");