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,
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 *vio;*/
899 pr_info("%s %s\n", __func__, (onoff) ? "on" : "off");
901 led_vdd = devm_regulator_get(dev, "cm36672p,vdd");
902 if (IS_ERR(led_vdd)) {
903 pr_err("%s, cannot get led_vdd\n", __func__);
905 } else if (!regulator_get_voltage(led_vdd)) {
906 regulator_set_voltage(led_vdd, 3300000, 3300000);
911 ret = regulator_enable(led_vdd);
913 pr_err("%s: Failed to enable led_vdd.\n", __func__);
917 ret = regulator_disable(led_vdd);
919 pr_err("%s: Failed to disable led_vdd.\n", __func__);
924 devm_regulator_put(led_vdd);
932 /* device tree parsing function */
933 static int cm36672p_parse_dt(struct device *dev,
934 struct cm36672p_platform_data *pdata)
936 struct device_node *np = dev->of_node;
937 enum of_gpio_flags flags;
941 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
942 pdata->leden_gpio = of_get_named_gpio_flags(np, "cm36672p,leden_gpio",
944 if (pdata->leden_gpio < 0) {
945 pr_err("%s, get prox_leden_gpio error\n", __func__);
950 pdata->irq = of_get_named_gpio_flags(np, "cm36672p,irq_gpio", 0,
952 if (pdata->irq < 0) {
953 pr_err("%s, get prox_int error\n", __func__);
957 ret = of_property_read_u32(np, "cm36672p,default_hi_thd",
958 &pdata->default_hi_thd);
960 pr_err("%s, Cannot set default_hi_thd\n", __func__);
961 pdata->default_hi_thd = DEFAULT_HI_THD;
964 ret = of_property_read_u32(np, "cm36672p,default_low_thd",
965 &pdata->default_low_thd);
967 pr_err("%s, Cannot set default_low_thd\n", __func__);
968 pdata->default_low_thd = DEFAULT_LOW_THD;
971 ret = of_property_read_u32(np, "cm36672p,cancel_hi_thd",
972 &pdata->cancel_hi_thd);
974 pr_err("%s, Cannot set cancel_hi_thd\n", __func__);
975 pdata->cancel_hi_thd = CANCEL_HI_THD;
978 ret = of_property_read_u32(np, "cm36672p,cancel_low_thd",
979 &pdata->cancel_low_thd);
981 pr_err("%s, Cannot set cancel_low_thd\n", __func__);
982 ps_reg_init_setting[PS_THD_LOW][CMD] = CANCEL_LOW_THD;
985 ret = of_property_read_u32(np, "cm36672p,ps_it", &temp);
987 pr_err("%s, Cannot set ps_it\n", __func__);
988 ps_reg_init_setting[PS_CONF1][CMD] = DEFAULT_CONF1;
991 ps_reg_init_setting[PS_CONF1][CMD] |= temp;
994 ret = of_property_read_u32(np, "cm36672p,led_current", &temp);
996 pr_err("%s, Cannot set led_current\n", __func__);
997 ps_reg_init_setting[PS_CONF3][CMD] = DEFAULT_CONF3;
1000 ps_reg_init_setting[PS_CONF3][CMD] |= temp;
1003 ret = of_property_read_u32(np, "cm36672p,default_trim",
1004 &pdata->default_trim);
1006 pr_err("%s, Cannot set default_trim\n", __func__);
1007 pdata->default_trim = DEFAULT_TRIM;
1009 ps_reg_init_setting[PS_THD_LOW][CMD] = pdata->default_low_thd;
1010 ps_reg_init_setting[PS_THD_HIGH][CMD] = pdata->default_hi_thd;
1011 ps_reg_init_setting[PS_CANCEL][CMD] = pdata->default_trim;
1016 static int cm36672p_parse_dt(struct device *dev, struct cm36672p_platform_data)
1022 static int cm36672p_i2c_probe(struct i2c_client *client,
1023 const struct i2c_device_id *id)
1026 struct cm36672p_data *data = NULL;
1027 struct cm36672p_platform_data *pdata = NULL;
1029 pr_info("%s, Probe Start!\n", __func__);
1030 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1031 pr_err("%s, i2c functionality check failed!\n",
1036 data = kzalloc(sizeof(struct cm36672p_data), GFP_KERNEL);
1038 pr_err("%s, failed to alloc memory for RGB sensor module data\n",
1043 if (client->dev.of_node) {
1044 pdata = devm_kzalloc(&client->dev,
1045 sizeof(struct cm36672p_platform_data), GFP_KERNEL);
1047 dev_err(&client->dev, "Failed to allocate memory\n");
1051 ret = cm36672p_parse_dt(&client->dev, pdata);
1055 pdata = client->dev.platform_data;
1058 pr_err("%s, missing pdata!\n", __func__);
1063 data->pdata = pdata;
1064 data->i2c_client = client;
1065 i2c_set_clientdata(client, data);
1067 mutex_init(&data->read_lock);
1068 /* wake lock init for proximity sensor */
1069 wake_lock_init(&data->prx_wake_lock, WAKE_LOCK_SUSPEND,
1072 proximity_regulator_onoff(&client->dev, ON);
1074 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1075 /* setup led_en_gpio */
1076 ret = setup_leden_gpio(data);
1078 pr_err("%s, could not setup leden_gpio\n", __func__);
1079 goto err_setup_leden_gpio;
1081 leden_gpio_onoff(data, ON);
1084 /* Check if the device is there or not. */
1085 ret = cm36672p_i2c_write_word(data, REG_PS_CONF1, 0x0001);
1087 pr_err("%s, cm36672 is not connected.(%d)\n", __func__,
1092 /* setup initial registers */
1093 ret = setup_reg_cm36672p(data);
1095 pr_err("%s, could not setup regs\n", __func__);
1099 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1100 leden_gpio_onoff(data, OFF);
1103 /* allocate proximity input_device */
1104 data->proximity_input_dev = input_allocate_device();
1105 if (!data->proximity_input_dev) {
1106 pr_err("%s, could not allocate proximity input device\n",
1108 goto err_input_alloc_device;
1111 input_set_drvdata(data->proximity_input_dev, data);
1112 data->proximity_input_dev->name = "proximity_sensor";
1113 input_set_capability(data->proximity_input_dev, EV_ABS,
1115 input_set_abs_params(data->proximity_input_dev, ABS_DISTANCE, 0, 1,
1118 ret = input_register_device(data->proximity_input_dev);
1120 pr_err("%s, could not register input device\n",
1122 goto err_input_register_device;
1125 ret = sensors_create_symlink(&data->proximity_input_dev->dev.kobj,
1126 data->proximity_input_dev->name);
1128 pr_err("%s, create_symlink error\n", __func__);
1129 goto err_sensors_create_symlink_prox;
1132 ret = sysfs_create_group(&data->proximity_input_dev->dev.kobj,
1133 &proximity_attribute_group);
1135 pr_err("%s, could not create sysfs group\n", __func__);
1136 goto err_sysfs_create_group_proximity;
1140 ret = setup_irq_cm36672p(data);
1142 pr_err("%s, could not setup irq\n", __func__);
1146 /* For factory test mode, we use timer to get average proximity data. */
1147 /* prox_timer settings. we poll for light values using a timer. */
1148 hrtimer_init(&data->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1149 data->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
1150 data->prox_timer.function = cm36672_prox_timer_func;
1152 /* the timer just fires off a work queue request. we need a thread
1153 to read the i2c (can be slow and blocking). */
1154 data->prox_wq = create_singlethread_workqueue("cm36672_prox_wq");
1155 if (!data->prox_wq) {
1157 pr_err("%s, could not create prox workqueue\n",
1159 goto err_create_prox_workqueue;
1161 /* this is the thread function we run on the work queue */
1162 INIT_WORK(&data->work_prox, cm36672_work_func_prox);
1164 /* set sysfs for proximity sensor */
1165 ret = sensors_register(data->proximity_dev,
1166 data, prox_sensor_attrs,
1167 "proximity_sensor");
1169 pr_err("%s, cound not register proximity sensor device(%d).\n",
1171 goto prox_sensor_register_failed;
1174 #if defined(CONFIG_MACH_J1_VZW) || defined(CONFIG_MACH_E5_USA_TFN)
1175 proximity_regulator_onoff(&client->dev, OFF);
1178 pr_info("%s is success.\n", __func__);
1181 /* error, unwind it all */
1182 prox_sensor_register_failed:
1183 destroy_workqueue(data->prox_wq);
1184 err_create_prox_workqueue:
1185 free_irq(data->irq, data);
1186 gpio_free(data->pdata->irq);
1188 sysfs_remove_group(&data->proximity_input_dev->dev.kobj,
1189 &proximity_attribute_group);
1190 err_sysfs_create_group_proximity:
1191 sensors_remove_symlink(&data->proximity_input_dev->dev.kobj,
1192 data->proximity_input_dev->name);
1193 err_sensors_create_symlink_prox:
1194 input_unregister_device(data->proximity_input_dev);
1195 err_input_register_device:
1196 input_free_device(data->proximity_input_dev);
1197 err_input_alloc_device:
1199 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1200 leden_gpio_onoff(data, OFF);
1201 gpio_free(data->pdata->leden_gpio);
1202 err_setup_leden_gpio:
1204 wake_lock_destroy(&data->prx_wake_lock);
1205 mutex_destroy(&data->read_lock);
1206 proximity_regulator_onoff(&client->dev, OFF);
1210 pr_err("%s is FAILED (%d)\n", __func__, ret);
1214 static int cm36672p_i2c_remove(struct i2c_client *client)
1216 struct cm36672p_data *data = i2c_get_clientdata(client);
1219 if (atomic_read(&data->enable)) {
1220 disable_irq_wake(data->irq);
1221 disable_irq(data->irq);
1223 free_irq(data->irq, data);
1224 gpio_free(data->pdata->irq);
1227 if (atomic_read(&data->enable))
1228 cm36672p_i2c_write_word(data, REG_PS_CONF1,
1231 /* destroy workqueue */
1232 destroy_workqueue(data->prox_wq);
1235 sensors_unregister(data->proximity_dev, prox_sensor_attrs);
1236 sensors_remove_symlink(&data->proximity_input_dev->dev.kobj,
1237 data->proximity_input_dev->name);
1239 /* input device destroy */
1240 sysfs_remove_group(&data->proximity_input_dev->dev.kobj,
1241 &proximity_attribute_group);
1242 input_unregister_device(data->proximity_input_dev);
1244 #if defined(CONFIG_SENSORS_LEDA_EN_GPIO)
1245 leden_gpio_onoff(data, OFF);
1246 gpio_free(data->pdata->leden_gpio);
1250 mutex_destroy(&data->read_lock);
1251 wake_lock_destroy(&data->prx_wake_lock);
1254 #ifndef CONFIG_MACH_J1_VZW
1255 proximity_regulator_onoff(&client->dev, OFF);
1261 static int cm36672p_suspend(struct device *dev)
1263 pr_info("%s is called.\n", __func__);
1267 static int cm36672p_resume(struct device *dev)
1269 pr_info("%s is called.\n", __func__);
1274 static struct of_device_id cm36672p_match_table[] = {
1275 { .compatible = "cm36672p",},
1279 #define cm36672p_match_table NULL
1283 static const struct i2c_device_id cm36672p_device_id[] = {
1288 MODULE_DEVICE_TABLE(i2c, cm36672p_device_id);
1290 static const struct dev_pm_ops cm36672p_pm_ops = {
1291 .suspend = cm36672p_suspend,
1292 .resume = cm36672p_resume
1295 static struct i2c_driver cm36672p_i2c_driver = {
1298 .owner = THIS_MODULE,
1299 .of_match_table = cm36672p_match_table,
1300 .pm = &cm36672p_pm_ops
1302 .probe = cm36672p_i2c_probe,
1303 .remove = cm36672p_i2c_remove,
1304 .id_table = cm36672p_device_id,
1307 static int __init cm36672p_init(void)
1309 return i2c_add_driver(&cm36672p_i2c_driver);
1312 static void __exit cm36672p_exit(void)
1314 i2c_del_driver(&cm36672p_i2c_driver);
1317 module_init(cm36672p_init);
1318 module_exit(cm36672p_exit);
1320 MODULE_AUTHOR("Samsung Electronics");
1321 MODULE_DESCRIPTION("Proximity Sensor device driver for CM36672P");
1322 MODULE_LICENSE("GPL");