1 /* driver/sensor/cm36672p.c
2 * Copyright (c) 2011 SAMSUNG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/i2c.h>
23 #include <linux/errno.h>
24 #include <linux/device.h>
25 #include <linux/gpio.h>
26 #include <linux/wakelock.h>
27 #include <linux/input.h>
28 #include <linux/workqueue.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/uaccess.h>
34 #include <linux/of_gpio.h>
35 #include <linux/regulator/consumer.h>
37 #include "sensors_core.h"
38 #include <linux/sensor/cm36672p.h>
41 #define CM36672P_DEBUG
43 #define VENDOR "CAPELLA"
44 #define CHIP_ID "CM36672P"
46 #define I2C_M_WR 0 /* for i2c Write */
47 #define I2c_M_RD 1 /* for i2c Read */
49 /* register addresses */
50 /* Proximity sensor */
51 #define REG_PS_CONF1 0x03
52 #define REG_PS_CONF3 0x04
53 #define REG_PS_CANC 0x05
54 #define REG_PS_THD_LOW 0x06
55 #define REG_PS_THD_HIGH 0x07
56 #define REG_PS_DATA 0x08
58 /* Intelligent Cancelation*/
59 #define PROXIMITY_CANCELATION
60 #ifdef PROXIMITY_CANCELATION
64 #define CAL_SKIP_ADC 8
65 #define CAL_FAIL_ADC 18
66 #define CANCELATION_FILE_PATH "/csa/sensor/prox_cal"
69 #define PROX_READ_NUM 40
71 /* proximity sensor default value for register */
72 #define DEFAULT_HI_THD 0x0022
73 #define DEFAULT_LOW_THD 0x001E
74 #define CANCEL_HI_THD 0x0022
75 #define CANCEL_LOW_THD 0x001E
77 #define DEFAULT_CONF1 0x03A0
78 #define DEFAULT_CONF3 0x4010
79 #define DEFAULT_TRIM 0x0000
103 * Since PS integration time and LED current would be different by HW rev or Project,
104 * we move the setting value to device tree. Please refer to the value below.
105 * PS_IT (CONF1, 0x03_L)
106 * 1T = 0, 1.5T = 1, 2T = 2, 2.5T = 3, 3T = 4, 3.5T = 5, 4T = 6, 8T = 7
108 * LED_I (CONF3, 0x04_H)
109 * 50mA = 0, 75mA = 1, 100mA = 2, 120mA = 3, 140mA = 4, 160mA = 5, 180mA = 6, 200mA = 7
111 static u16 ps_reg_init_setting[PS_REG_NUM][2] = {
112 {REG_PS_CONF1, DEFAULT_CONF1},
113 {REG_PS_CONF3, DEFAULT_CONF3},
114 {REG_PS_THD_LOW, DEFAULT_LOW_THD},
115 {REG_PS_THD_HIGH, DEFAULT_HI_THD},
116 {REG_PS_CANC, DEFAULT_TRIM},
120 struct cm36672p_data {
121 struct i2c_client *i2c_client;
122 struct wake_lock prx_wake_lock;
123 struct input_dev *proximity_input_dev;
124 struct cm36672p_platform_data *pdata;
125 struct mutex read_lock;
126 struct hrtimer prox_timer;
127 struct workqueue_struct *prox_wq;
128 struct work_struct work_prox;
129 struct device *proximity_dev;
130 ktime_t prox_poll_delay;
134 unsigned int uProxCalResult;
137 static int proximity_regulator_onoff(struct device *dev, bool onoff);
139 int cm36672p_i2c_read_word(struct cm36672p_data *data, u8 command, u16 *val)
143 struct i2c_client *client = data->i2c_client;
144 struct i2c_msg msg[2];
145 unsigned char tmp[2] = {0,};
148 if ((client == NULL) || (!client->adapter))
152 /* send slave address & command */
153 msg[0].addr = client->addr;
154 msg[0].flags = I2C_M_WR;
156 msg[0].buf = &command;
159 msg[1].addr = client->addr;
160 msg[1].flags = I2C_M_RD;
164 err = i2c_transfer(client->adapter, msg, 2);
168 *val = (value << 8) | (u16)tmp[0];
172 pr_err("%s, i2c transfer error ret=%d\n", __func__, err);
176 int cm36672p_i2c_write_word(struct cm36672p_data *data, u8 command,
180 struct i2c_client *client = data->i2c_client;
183 if ((client == NULL) || (!client->adapter))
187 err = i2c_smbus_write_word_data(client, command, val);
191 pr_err("%s, i2c transfer error(%d)\n", __func__, err);
195 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
196 static int leden_gpio_onoff(struct cm36672p_data *data, bool onoff)
198 struct cm36672p_platform_data *pdata = data->pdata;
200 gpio_set_value(pdata->leden_gpio, onoff);
208 #ifdef PROXIMITY_CANCELATION
209 static int proximity_open_cancelation(struct cm36672p_data *data)
211 struct file *cancel_filp = NULL;
218 cancel_filp = filp_open(CANCELATION_FILE_PATH, O_RDONLY, 0666);
219 if (IS_ERR(cancel_filp)) {
220 err = PTR_ERR(cancel_filp);
222 pr_err("%s, Can't open cancelation file\n",
228 err = cancel_filp->f_op->read(cancel_filp,
229 (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
230 sizeof(u16), &cancel_filp->f_pos);
231 if (err != sizeof(u16)) {
232 pr_err("%s, Can't read the cancel data from file\n", __func__);
236 /*If there is an offset cal data. */
237 if (ps_reg_init_setting[PS_CANCEL][CMD] != data->pdata->default_trim) {
238 ps_reg_init_setting[PS_THD_HIGH][CMD] =
239 data->pdata->cancel_hi_thd ?
240 data->pdata->cancel_hi_thd :
242 ps_reg_init_setting[PS_THD_LOW][CMD] =
243 data->pdata->cancel_low_thd ?
244 data->pdata->cancel_low_thd :
248 pr_info("%s, prox_cal= 0x%x, high_thresh= 0x%x, low_thresh= 0x%x\n",
250 ps_reg_init_setting[PS_CANCEL][CMD],
251 ps_reg_init_setting[PS_THD_HIGH][CMD],
252 ps_reg_init_setting[PS_THD_LOW][CMD]);
254 filp_close(cancel_filp, current->files);
260 static int proximity_store_cancelation(struct device *dev, bool do_calib)
262 struct cm36672p_data *data = dev_get_drvdata(dev);
263 struct file *cancel_filp = NULL;
269 mutex_lock(&data->read_lock);
270 cm36672p_i2c_read_word(data,
271 REG_PS_DATA, &ps_data);
272 mutex_unlock(&data->read_lock);
274 if (ps_data < CAL_SKIP_ADC) {
276 ps_reg_init_setting[PS_CANCEL][CMD] =
277 data->pdata->default_trim;
278 pr_info("%s:crosstalk <= %d\n", __func__, CAL_SKIP_ADC);
279 data->uProxCalResult = 2;
280 } else if (ps_data < CAL_FAIL_ADC) {
282 ps_reg_init_setting[PS_CANCEL][CMD] =
283 ps_data + data->pdata->default_trim;
284 pr_info("%s:crosstalk_offset = %u", __func__, ps_data);
285 data->uProxCalResult = 1;
288 ps_reg_init_setting[PS_CANCEL][CMD] =
289 data->pdata->default_trim;
290 pr_info("%s:crosstalk > %d\n", __func__, CAL_FAIL_ADC);
291 data->uProxCalResult = 0;
294 if (data->uProxCalResult == 1) {
295 ps_reg_init_setting[PS_THD_HIGH][CMD] =
296 data->pdata->cancel_hi_thd ?
297 data->pdata->cancel_hi_thd :
299 ps_reg_init_setting[PS_THD_LOW][CMD] =
300 data->pdata->cancel_low_thd ?
301 data->pdata->cancel_low_thd :
304 ps_reg_init_setting[PS_THD_HIGH][CMD] =
305 data->pdata->default_hi_thd ?
306 data->pdata->default_hi_thd :
308 ps_reg_init_setting[PS_THD_LOW][CMD] =
309 data->pdata->default_low_thd ?
310 data->pdata->default_low_thd :
314 ps_reg_init_setting[PS_CANCEL][CMD] =
315 data->pdata->default_trim;
316 ps_reg_init_setting[PS_THD_HIGH][CMD] =
317 data->pdata->default_hi_thd ?
318 data->pdata->default_hi_thd :
320 ps_reg_init_setting[PS_THD_LOW][CMD] =
321 data->pdata->default_low_thd ?
322 data->pdata->default_low_thd :
326 if ((data->uProxCalResult == 1) || !do_calib) {
327 err = cm36672p_i2c_write_word(data, REG_PS_CANC,
328 ps_reg_init_setting[PS_CANCEL][CMD]);
330 pr_err("%s, ps_canc_reg is failed. %d\n", __func__,
332 err = cm36672p_i2c_write_word(data, REG_PS_THD_HIGH,
333 ps_reg_init_setting[PS_THD_HIGH][CMD]);
335 pr_err("%s: ps_high_reg is failed. %d\n", __func__,
337 err = cm36672p_i2c_write_word(data, REG_PS_THD_LOW,
338 ps_reg_init_setting[PS_THD_LOW][CMD]);
340 pr_err("%s: ps_low_reg is failed. %d\n", __func__,
344 pr_info("%s: prox_cal = 0x%x, ps_high_thresh = 0x%x, ps_low_thresh = 0x%x\n",
346 ps_reg_init_setting[PS_CANCEL][CMD],
347 ps_reg_init_setting[PS_THD_HIGH][CMD],
348 ps_reg_init_setting[PS_THD_LOW][CMD]);
353 cancel_filp = filp_open(CANCELATION_FILE_PATH,
354 O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
355 if (IS_ERR(cancel_filp)) {
356 pr_err("%s, Can't open cancelation file\n", __func__);
358 err = PTR_ERR(cancel_filp);
362 err = cancel_filp->f_op->write(cancel_filp,
363 (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
364 sizeof(u16), &cancel_filp->f_pos);
365 if (err != sizeof(u16)) {
366 pr_err("%s, Can't write the cancel data to file\n", __func__);
370 filp_close(cancel_filp, current->files);
373 if (!do_calib) /* delay for clearing */
379 static ssize_t proximity_cancel_store(struct device *dev,
380 struct device_attribute *attr, const char *buf, size_t size)
385 if (sysfs_streq(buf, "1")) /* calibrate cancelation value */
387 else if (sysfs_streq(buf, "0")) /* reset cancelation value */
390 pr_err("%s, invalid value %d\n", __func__, *buf);
394 err = proximity_store_cancelation(dev, do_calib);
396 pr_err("%s, proximity_store_cancelation() failed\n",
404 static ssize_t proximity_cancel_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct cm36672p_data *data = dev_get_drvdata(dev);
408 return snprintf(buf, PAGE_SIZE, "%u,%u,%u\n",
409 ps_reg_init_setting[PS_CANCEL][CMD] - data->pdata->default_trim,
410 ps_reg_init_setting[PS_THD_HIGH][CMD],
411 ps_reg_init_setting[PS_THD_LOW][CMD]);
414 static ssize_t proximity_cancel_pass_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 struct cm36672p_data *data = dev_get_drvdata(dev);
419 pr_info("%s, %u\n", __func__, data->uProxCalResult);
420 return snprintf(buf, PAGE_SIZE, "%u\n", data->uProxCalResult);
424 static ssize_t proximity_enable_store(struct device *dev,
425 struct device_attribute *attr, const char *buf, size_t size)
427 struct cm36672p_data *data = dev_get_drvdata(dev);
431 if (sysfs_streq(buf, "1"))
433 else if (sysfs_streq(buf, "0"))
436 pr_err("%s, invalid value %d\n", __func__, *buf);
440 pre_enable = atomic_read(&data->enable);
441 pr_info("%s, new_value = %d, pre_enable = %d\n",
442 __func__, new_value, pre_enable);
444 if (new_value && !pre_enable) {
447 atomic_set(&data->enable, ON);
448 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
449 leden_gpio_onoff(data, ON);
451 #ifdef PROXIMITY_CANCELATION
452 /* open cancelation data */
453 ret = proximity_open_cancelation(data);
454 if (ret < 0 && ret != -ENOENT)
455 pr_err("%s, proximity_open_cancelation() failed\n",
458 /* enable settings */
459 for (i = 0; i < PS_REG_NUM; i++) {
460 cm36672p_i2c_write_word(data,
461 ps_reg_init_setting[i][REG_ADDR],
462 ps_reg_init_setting[i][CMD]);
465 /* 0 is close, 1 is far */
466 input_report_abs(data->proximity_input_dev, ABS_DISTANCE, 1);
467 input_sync(data->proximity_input_dev);
469 enable_irq(data->irq);
470 enable_irq_wake(data->irq);
471 } else if (!new_value && pre_enable) {
472 disable_irq_wake(data->irq);
473 disable_irq(data->irq);
474 /* disable settings */
475 cm36672p_i2c_write_word(data, REG_PS_CONF1, 0x0001);
476 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
477 leden_gpio_onoff(data, OFF);
479 atomic_set(&data->enable, OFF);
481 pr_info("%s, enable = %d\n", __func__, atomic_read(&data->enable));
486 static ssize_t proximity_enable_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct cm36672p_data *data = dev_get_drvdata(dev);
491 return snprintf(buf, PAGE_SIZE, "%d\n",
492 atomic_read(&data->enable));
495 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
496 proximity_enable_show, proximity_enable_store);
498 static struct attribute *proximity_sysfs_attrs[] = {
499 &dev_attr_enable.attr,
503 static struct attribute_group proximity_attribute_group = {
504 .attrs = proximity_sysfs_attrs,
507 /* proximity sysfs */
508 static ssize_t proximity_avg_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
511 struct cm36672p_data *data = dev_get_drvdata(dev);
513 return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", data->avg[0],
514 data->avg[1], data->avg[2]);
517 static ssize_t proximity_avg_store(struct device *dev,
518 struct device_attribute *attr, const char *buf, size_t size)
520 struct cm36672p_data *data = dev_get_drvdata(dev);
521 bool new_value = false;
523 if (sysfs_streq(buf, "1"))
525 else if (sysfs_streq(buf, "0"))
528 pr_err("%s, invalid value %d\n", __func__, *buf);
532 pr_info("%s, average enable = %d\n", __func__, new_value);
534 if (atomic_read(&data->enable) == OFF) {
535 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
536 leden_gpio_onoff(data, ON);
538 cm36672p_i2c_write_word(data, REG_PS_CONF1,
539 ps_reg_init_setting[PS_CONF1][CMD]);
541 hrtimer_start(&data->prox_timer, data->prox_poll_delay,
543 } else if (!new_value) {
544 hrtimer_cancel(&data->prox_timer);
545 cancel_work_sync(&data->work_prox);
546 if (atomic_read(&data->enable) == OFF) {
547 cm36672p_i2c_write_word(data, REG_PS_CONF1,
549 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
550 leden_gpio_onoff(data, OFF);
558 static ssize_t proximity_trim_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
561 struct cm36672p_data *data = dev_get_drvdata(dev);
563 return snprintf(buf, PAGE_SIZE, "%u\n",
564 data->pdata->default_trim);
567 static ssize_t proximity_thresh_high_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 pr_info("%s = %u,%u\n", __func__,
571 ps_reg_init_setting[PS_THD_HIGH][CMD],
572 ps_reg_init_setting[PS_THD_LOW][CMD]);
573 return snprintf(buf, PAGE_SIZE, "%u,%u\n",
574 ps_reg_init_setting[PS_THD_HIGH][CMD],
575 ps_reg_init_setting[PS_THD_LOW][CMD]);
578 static ssize_t proximity_thresh_high_store(struct device *dev,
579 struct device_attribute *attr, const char *buf, size_t size)
581 struct cm36672p_data *data = dev_get_drvdata(dev);
582 u16 thresh_value = ps_reg_init_setting[PS_THD_HIGH][CMD];
585 err = kstrtou16(buf, 10, &thresh_value);
587 pr_err("%s, kstrtoint failed.\n", __func__);
589 if (thresh_value > 2) {
590 ps_reg_init_setting[PS_THD_HIGH][CMD] = thresh_value;
591 err = cm36672p_i2c_write_word(data, REG_PS_THD_HIGH,
592 ps_reg_init_setting[PS_THD_HIGH][CMD]);
594 pr_err("%s, cm36672_ps_high_reg is failed. %d\n",
596 pr_info("%s, new high threshold = 0x%x\n",
597 __func__, thresh_value);
600 pr_err("%s, wrong high threshold value(0x%x)!!\n",
601 __func__, thresh_value);
606 static ssize_t proximity_thresh_low_show(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 pr_info("%s = %u,%u\n", __func__,
610 ps_reg_init_setting[PS_THD_HIGH][CMD],
611 ps_reg_init_setting[PS_THD_LOW][CMD]);
613 return snprintf(buf, PAGE_SIZE, "%u,%u\n",
614 ps_reg_init_setting[PS_THD_HIGH][CMD],
615 ps_reg_init_setting[PS_THD_LOW][CMD]);
618 static ssize_t proximity_thresh_low_store(struct device *dev,
619 struct device_attribute *attr, const char *buf, size_t size)
621 struct cm36672p_data *data = dev_get_drvdata(dev);
622 u16 thresh_value = ps_reg_init_setting[PS_THD_LOW][CMD];
625 err = kstrtou16(buf, 10, &thresh_value);
627 pr_err("%s, kstrtoint failed.", __func__);
629 if (thresh_value > 2) {
630 ps_reg_init_setting[PS_THD_LOW][CMD] = thresh_value;
631 err = cm36672p_i2c_write_word(data, REG_PS_THD_LOW,
632 ps_reg_init_setting[PS_THD_LOW][CMD]);
634 pr_err("%s, cm36672_ps_low_reg is failed. %d\n",
636 pr_info("%s, new low threshold = 0x%x\n",
637 __func__, thresh_value);
640 pr_err("%s, wrong low threshold value(0x%x)!!\n",
641 __func__, thresh_value);
646 static ssize_t proximity_state_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
649 struct cm36672p_data *data = dev_get_drvdata(dev);
652 if (atomic_read(&data->enable) == OFF) {
653 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
654 leden_gpio_onoff(data, ON);
656 cm36672p_i2c_write_word(data, REG_PS_CONF1,
657 ps_reg_init_setting[PS_CONF1][CMD]);
660 mutex_lock(&data->read_lock);
661 cm36672p_i2c_read_word(data, REG_PS_DATA, &ps_data);
662 mutex_unlock(&data->read_lock);
664 if (atomic_read(&data->enable) == OFF) {
665 cm36672p_i2c_write_word(data, REG_PS_CONF1, 0x0001);
666 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
667 leden_gpio_onoff(data, OFF);
671 return snprintf(buf, PAGE_SIZE, "%u\n", ps_data);
674 /* sysfs for vendor & name */
675 static ssize_t cm36672p_vendor_show(struct device *dev,
676 struct device_attribute *attr, char *buf)
678 return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
681 static ssize_t cm36672p_name_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_ID);
688 #ifdef PROXIMITY_CANCELATION
689 static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
690 proximity_cancel_show, proximity_cancel_store);
691 static DEVICE_ATTR(prox_offset_pass, S_IRUGO, proximity_cancel_pass_show,
694 static DEVICE_ATTR(prox_avg, S_IRUGO | S_IWUSR | S_IWGRP,
695 proximity_avg_show, proximity_avg_store);
696 static DEVICE_ATTR(prox_trim, S_IRUSR | S_IRGRP,
697 proximity_trim_show, NULL);
698 static DEVICE_ATTR(thresh_high, S_IRUGO | S_IWUSR | S_IWGRP,
699 proximity_thresh_high_show, proximity_thresh_high_store);
700 static DEVICE_ATTR(thresh_low, S_IRUGO | S_IWUSR | S_IWGRP,
701 proximity_thresh_low_show, proximity_thresh_low_store);
702 static DEVICE_ATTR(state, S_IRUGO, proximity_state_show, NULL);
703 static DEVICE_ATTR(raw_data, S_IRUGO, proximity_state_show, NULL);
704 static DEVICE_ATTR(vendor, S_IRUSR | S_IRGRP, cm36672p_vendor_show, NULL);
705 static DEVICE_ATTR(name, S_IRUSR | S_IRGRP, cm36672p_name_show, NULL);
707 static struct device_attribute *prox_sensor_attrs[] = {
708 #ifdef PROXIMITY_CANCELATION
710 &dev_attr_prox_offset_pass,
714 &dev_attr_thresh_high,
715 &dev_attr_thresh_low,
723 /* interrupt happened due to transition/change of near/far proximity state */
724 irqreturn_t proximity_irq_thread_fn(int irq, void *user_data)
726 struct cm36672p_data *data = user_data;
730 #ifdef CM36672P_DEBUG
732 pr_info("%s\n", __func__);
735 enabled = atomic_read(&data->enable);
736 val = gpio_get_value(data->pdata->irq);
737 cm36672p_i2c_read_word(data, REG_PS_DATA, &ps_data);
738 #ifdef CM36672P_DEBUG
739 pr_info("%s, enabled = %d, count = %d\n", __func__, enabled, count++);
743 /* 0 is close, 1 is far */
744 input_report_abs(data->proximity_input_dev, ABS_DISTANCE,
746 input_sync(data->proximity_input_dev);
749 wake_lock_timeout(&data->prx_wake_lock, 3 * HZ);
751 pr_info("%s, val = %u, ps_data = %u (close:0, far:1)\n",
752 __func__, val, ps_data);
757 static void proximity_get_avg_val(struct cm36672p_data *data)
759 int min = 0, max = 0, avg = 0;
763 for (i = 0; i < PROX_READ_NUM; i++) {
765 cm36672p_i2c_read_word(data, REG_PS_DATA,
771 else if (ps_data < min)
777 avg /= PROX_READ_NUM;
784 static void cm36672_work_func_prox(struct work_struct *work)
786 struct cm36672p_data *data = container_of(work,
787 struct cm36672p_data, work_prox);
788 proximity_get_avg_val(data);
791 static enum hrtimer_restart cm36672_prox_timer_func(struct hrtimer *timer)
793 struct cm36672p_data *data = container_of(timer,
794 struct cm36672p_data, prox_timer);
795 queue_work(data->prox_wq, &data->work_prox);
796 hrtimer_forward_now(&data->prox_timer, data->prox_poll_delay);
797 return HRTIMER_RESTART;
800 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
801 static int setup_leden_gpio(struct cm36672p_data *data)
804 struct cm36672p_platform_data *pdata = data->pdata;
806 ret = gpio_request(pdata->leden_gpio, "prox_en");
808 pr_err("%s, gpio %d request failed (%d)\n",
809 __func__, pdata->leden_gpio, ret);
811 ret = gpio_direction_output(pdata->leden_gpio, 1);
813 pr_err("%s: unable to set_direction for prox_en [%d]\n",
814 __func__, pdata->leden_gpio);
820 static int setup_reg_cm36672p(struct cm36672p_data *data)
825 /* PS initialization */
826 for (i = 0; i < PS_REG_NUM; i++) {
827 ret = cm36672p_i2c_write_word(data,
828 ps_reg_init_setting[i][REG_ADDR],
829 ps_reg_init_setting[i][CMD]);
831 pr_err("%s, cm36672_ps_reg is failed. %d\n",
837 /* printing the inital proximity value with no contact */
839 mutex_lock(&data->read_lock);
840 ret = cm36672p_i2c_read_word(data, REG_PS_DATA, &tmp);
841 mutex_unlock(&data->read_lock);
843 pr_err("%s, read ps_data failed\n", __func__);
848 cm36672p_i2c_write_word(data, REG_PS_CONF1, 0x0001);
849 cm36672p_i2c_write_word(data, REG_PS_CONF3, 0x0000);
855 static int setup_irq_cm36672p(struct cm36672p_data *data)
858 struct cm36672p_platform_data *pdata = data->pdata;
860 ret = gpio_request(pdata->irq, "gpio_proximity_out");
862 pr_err("%s, gpio %d request failed (%d)\n",
863 __func__, pdata->irq, ret);
867 ret = gpio_direction_input(pdata->irq);
869 pr_err("%s, failed to set gpio %d as input (%d)\n",
870 __func__, pdata->irq, ret);
871 gpio_free(pdata->irq);
875 data->irq = gpio_to_irq(pdata->irq);
876 ret = request_threaded_irq(data->irq, NULL, proximity_irq_thread_fn,
877 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
878 "proximity_int", data);
880 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
881 __func__, data->irq, pdata->irq, ret);
882 gpio_free(pdata->irq);
886 /* start with interrupts disabled */
887 disable_irq(data->irq);
893 static int proximity_regulator_onoff(struct device *dev, bool onoff)
895 struct regulator *led_vdd;
896 struct regulator *vdd;
899 pr_info("%s %s\n", __func__, (onoff) ? "on" : "off");
901 vdd = devm_regulator_get(dev, "cm36672p,vdd");
903 pr_err("%s, cannot get vdd\n", __func__);
906 regulator_set_voltage(vdd, 3000000, 3000000);
908 led_vdd = devm_regulator_get(dev, "cm36672p,led");
909 if (IS_ERR(led_vdd)) {
910 pr_err("%s, cannot get led_vdd\n", __func__);
913 regulator_set_voltage(led_vdd, 3300000, 3300000);
916 ret = regulator_enable(vdd);
918 pr_err("%s: Failed to enable vdd.\n", __func__);
921 ret = regulator_enable(led_vdd);
923 pr_err("%s: Failed to enable led_vdd.\n", __func__);
927 ret = regulator_disable(led_vdd);
929 pr_err("%s: Failed to disable led_vdd.\n", __func__);
932 ret = regulator_disable(vdd);
934 pr_err("%s: Failed to disable vdd.\n", __func__);
939 devm_regulator_put(vdd);
940 devm_regulator_put(led_vdd);
948 /* device tree parsing function */
949 static int cm36672p_parse_dt(struct device *dev,
950 struct cm36672p_platform_data *pdata)
952 struct device_node *np = dev->of_node;
953 enum of_gpio_flags flags;
957 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
958 pdata->leden_gpio = of_get_named_gpio_flags(np, "cm36672p,leden_gpio",
960 if (pdata->leden_gpio < 0) {
961 pr_err("%s, get prox_leden_gpio error\n", __func__);
966 pdata->irq = of_get_named_gpio_flags(np, "cm36672p,irq_gpio", 0,
968 if (pdata->irq < 0) {
969 pr_err("%s, get prox_int error\n", __func__);
973 ret = of_property_read_u32(np, "cm36672p,default_hi_thd",
974 &pdata->default_hi_thd);
976 pr_err("%s, Cannot set default_hi_thd\n", __func__);
977 pdata->default_hi_thd = DEFAULT_HI_THD;
980 ret = of_property_read_u32(np, "cm36672p,default_low_thd",
981 &pdata->default_low_thd);
983 pr_err("%s, Cannot set default_low_thd\n", __func__);
984 pdata->default_low_thd = DEFAULT_LOW_THD;
987 ret = of_property_read_u32(np, "cm36672p,cancel_hi_thd",
988 &pdata->cancel_hi_thd);
990 pr_err("%s, Cannot set cancel_hi_thd\n", __func__);
991 pdata->cancel_hi_thd = CANCEL_HI_THD;
994 ret = of_property_read_u32(np, "cm36672p,cancel_low_thd",
995 &pdata->cancel_low_thd);
997 pr_err("%s, Cannot set cancel_low_thd\n", __func__);
998 ps_reg_init_setting[PS_THD_LOW][CMD] = CANCEL_LOW_THD;
1001 ret = of_property_read_u32(np, "cm36672p,ps_it", &temp);
1003 pr_err("%s, Cannot set ps_it\n", __func__);
1004 ps_reg_init_setting[PS_CONF1][CMD] = DEFAULT_CONF1;
1007 ps_reg_init_setting[PS_CONF1][CMD] |= temp;
1010 ret = of_property_read_u32(np, "cm36672p,led_current", &temp);
1012 pr_err("%s, Cannot set led_current\n", __func__);
1013 ps_reg_init_setting[PS_CONF3][CMD] = DEFAULT_CONF3;
1016 ps_reg_init_setting[PS_CONF3][CMD] |= temp;
1019 ret = of_property_read_u32(np, "cm36672p,default_trim",
1020 &pdata->default_trim);
1022 pr_err("%s, Cannot set default_trim\n", __func__);
1023 pdata->default_trim = DEFAULT_TRIM;
1025 ps_reg_init_setting[PS_THD_LOW][CMD] = pdata->default_low_thd;
1026 ps_reg_init_setting[PS_THD_HIGH][CMD] = pdata->default_hi_thd;
1027 ps_reg_init_setting[PS_CANCEL][CMD] = pdata->default_trim;
1032 static int cm36672p_parse_dt(struct device *dev, struct cm36672p_platform_data)
1038 static int cm36672p_i2c_probe(struct i2c_client *client,
1039 const struct i2c_device_id *id)
1042 struct cm36672p_data *data = NULL;
1043 struct cm36672p_platform_data *pdata = NULL;
1045 pr_info("%s, Probe Start!\n", __func__);
1046 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1047 pr_err("%s, i2c functionality check failed!\n",
1052 data = kzalloc(sizeof(struct cm36672p_data), GFP_KERNEL);
1054 pr_err("%s, failed to alloc memory for RGB sensor module data\n",
1059 if (client->dev.of_node) {
1060 pdata = devm_kzalloc(&client->dev,
1061 sizeof(struct cm36672p_platform_data), GFP_KERNEL);
1063 dev_err(&client->dev, "Failed to allocate memory\n");
1067 ret = cm36672p_parse_dt(&client->dev, pdata);
1071 pdata = client->dev.platform_data;
1074 pr_err("%s, missing pdata!\n", __func__);
1079 data->pdata = pdata;
1080 data->i2c_client = client;
1081 i2c_set_clientdata(client, data);
1083 mutex_init(&data->read_lock);
1084 /* wake lock init for proximity sensor */
1085 wake_lock_init(&data->prx_wake_lock, WAKE_LOCK_SUSPEND,
1088 proximity_regulator_onoff(&client->dev, ON);
1090 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1091 /* setup led_en_gpio */
1092 ret = setup_leden_gpio(data);
1094 pr_err("%s, could not setup leden_gpio\n", __func__);
1095 goto err_setup_leden_gpio;
1097 leden_gpio_onoff(data, ON);
1100 /* Check if the device is there or not. */
1101 ret = cm36672p_i2c_write_word(data, REG_PS_CONF1, 0x0001);
1103 pr_err("%s, cm36672 is not connected.(%d)\n", __func__,
1108 /* setup initial registers */
1109 ret = setup_reg_cm36672p(data);
1111 pr_err("%s, could not setup regs\n", __func__);
1115 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1116 leden_gpio_onoff(data, OFF);
1119 /* allocate proximity input_device */
1120 data->proximity_input_dev = input_allocate_device();
1121 if (!data->proximity_input_dev) {
1122 pr_err("%s, could not allocate proximity input device\n",
1124 goto err_input_alloc_device;
1127 input_set_drvdata(data->proximity_input_dev, data);
1128 data->proximity_input_dev->name = "proximity_sensor";
1129 input_set_capability(data->proximity_input_dev, EV_ABS,
1131 input_set_abs_params(data->proximity_input_dev, ABS_DISTANCE, 0, 1,
1134 ret = input_register_device(data->proximity_input_dev);
1136 pr_err("%s, could not register input device\n",
1138 goto err_input_register_device;
1141 ret = sensors_create_symlink(&data->proximity_input_dev->dev.kobj,
1142 data->proximity_input_dev->name);
1144 pr_err("%s, create_symlink error\n", __func__);
1145 goto err_sensors_create_symlink_prox;
1148 ret = sysfs_create_group(&data->proximity_input_dev->dev.kobj,
1149 &proximity_attribute_group);
1151 pr_err("%s, could not create sysfs group\n", __func__);
1152 goto err_sysfs_create_group_proximity;
1156 ret = setup_irq_cm36672p(data);
1158 pr_err("%s, could not setup irq\n", __func__);
1162 /* For factory test mode, we use timer to get average proximity data. */
1163 /* prox_timer settings. we poll for light values using a timer. */
1164 hrtimer_init(&data->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1165 data->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
1166 data->prox_timer.function = cm36672_prox_timer_func;
1168 /* the timer just fires off a work queue request. we need a thread
1169 to read the i2c (can be slow and blocking). */
1170 data->prox_wq = create_singlethread_workqueue("cm36672_prox_wq");
1171 if (!data->prox_wq) {
1173 pr_err("%s, could not create prox workqueue\n",
1175 goto err_create_prox_workqueue;
1177 /* this is the thread function we run on the work queue */
1178 INIT_WORK(&data->work_prox, cm36672_work_func_prox);
1180 /* set sysfs for proximity sensor */
1181 ret = sensors_register(data->proximity_dev,
1182 data, prox_sensor_attrs,
1183 "proximity_sensor");
1185 pr_err("%s, cound not register proximity sensor device(%d).\n",
1187 goto prox_sensor_register_failed;
1190 #if defined(CONFIG_MACH_J1_VZW) || defined(CONFIG_MACH_E5_USA_TFN)
1191 proximity_regulator_onoff(&client->dev, OFF);
1194 pr_info("%s is success.\n", __func__);
1197 /* error, unwind it all */
1198 prox_sensor_register_failed:
1199 destroy_workqueue(data->prox_wq);
1200 err_create_prox_workqueue:
1201 free_irq(data->irq, data);
1202 gpio_free(data->pdata->irq);
1204 sysfs_remove_group(&data->proximity_input_dev->dev.kobj,
1205 &proximity_attribute_group);
1206 err_sysfs_create_group_proximity:
1207 sensors_remove_symlink(&data->proximity_input_dev->dev.kobj,
1208 data->proximity_input_dev->name);
1209 err_sensors_create_symlink_prox:
1210 input_unregister_device(data->proximity_input_dev);
1211 err_input_register_device:
1212 input_free_device(data->proximity_input_dev);
1213 err_input_alloc_device:
1215 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1216 leden_gpio_onoff(data, OFF);
1217 gpio_free(data->pdata->leden_gpio);
1218 err_setup_leden_gpio:
1220 wake_lock_destroy(&data->prx_wake_lock);
1221 mutex_destroy(&data->read_lock);
1222 proximity_regulator_onoff(&client->dev, OFF);
1226 pr_err("%s is FAILED (%d)\n", __func__, ret);
1230 static int cm36672p_i2c_remove(struct i2c_client *client)
1232 struct cm36672p_data *data = i2c_get_clientdata(client);
1235 if (atomic_read(&data->enable)) {
1236 disable_irq_wake(data->irq);
1237 disable_irq(data->irq);
1239 free_irq(data->irq, data);
1240 gpio_free(data->pdata->irq);
1243 if (atomic_read(&data->enable))
1244 cm36672p_i2c_write_word(data, REG_PS_CONF1,
1247 /* destroy workqueue */
1248 destroy_workqueue(data->prox_wq);
1251 sensors_unregister(data->proximity_dev, prox_sensor_attrs);
1252 sensors_remove_symlink(&data->proximity_input_dev->dev.kobj,
1253 data->proximity_input_dev->name);
1255 /* input device destroy */
1256 sysfs_remove_group(&data->proximity_input_dev->dev.kobj,
1257 &proximity_attribute_group);
1258 input_unregister_device(data->proximity_input_dev);
1260 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1261 leden_gpio_onoff(data, OFF);
1262 gpio_free(data->pdata->leden_gpio);
1266 mutex_destroy(&data->read_lock);
1267 wake_lock_destroy(&data->prx_wake_lock);
1270 #ifndef CONFIG_MACH_J1_VZW
1271 proximity_regulator_onoff(&client->dev, OFF);
1277 static int cm36672p_suspend(struct device *dev)
1279 pr_info("%s is called.\n", __func__);
1283 static int cm36672p_resume(struct device *dev)
1285 pr_info("%s is called.\n", __func__);
1290 static struct of_device_id cm36672p_match_table[] = {
1291 { .compatible = "cm36672p",},
1295 #define cm36672p_match_table NULL
1299 static const struct i2c_device_id cm36672p_device_id[] = {
1304 MODULE_DEVICE_TABLE(i2c, cm36672p_device_id);
1306 static const struct dev_pm_ops cm36672p_pm_ops = {
1307 .suspend = cm36672p_suspend,
1308 .resume = cm36672p_resume
1311 static struct i2c_driver cm36672p_i2c_driver = {
1314 .owner = THIS_MODULE,
1315 .of_match_table = cm36672p_match_table,
1316 .pm = &cm36672p_pm_ops
1318 .probe = cm36672p_i2c_probe,
1319 .remove = cm36672p_i2c_remove,
1320 .id_table = cm36672p_device_id,
1323 static int __init cm36672p_init(void)
1325 return i2c_add_driver(&cm36672p_i2c_driver);
1328 static void __exit cm36672p_exit(void)
1330 i2c_del_driver(&cm36672p_i2c_driver);
1333 module_init(cm36672p_init);
1334 module_exit(cm36672p_exit);
1336 MODULE_AUTHOR("Samsung Electronics");
1337 MODULE_DESCRIPTION("Proximity Sensor device driver for CM36672P");
1338 MODULE_LICENSE("GPL");